usb-hub: set PORT_STAT_C_SUSPEND on host-initiated wake-up
[qemu/ar7.git] / vl.c
blob92d1310b996161a91e070c48b3b2896b065c1754
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");
1440 error_report("usb_add is deprecated, please use device_add instead");
1442 if (usb_device_add(devname) < 0) {
1443 error_report("could not add USB device '%s'", devname);
1447 void hmp_usb_del(Monitor *mon, const QDict *qdict)
1449 const char *devname = qdict_get_str(qdict, "devname");
1451 error_report("usb_del is deprecated, please use device_del instead");
1453 if (usb_device_del(devname) < 0) {
1454 error_report("could not delete USB device '%s'", devname);
1458 /***********************************************************/
1459 /* machine registration */
1461 MachineState *current_machine;
1463 static MachineClass *find_machine(const char *name)
1465 GSList *el, *machines = object_class_get_list(TYPE_MACHINE, false);
1466 MachineClass *mc = NULL;
1468 for (el = machines; el; el = el->next) {
1469 MachineClass *temp = el->data;
1471 if (!strcmp(temp->name, name)) {
1472 mc = temp;
1473 break;
1475 if (temp->alias &&
1476 !strcmp(temp->alias, name)) {
1477 mc = temp;
1478 break;
1482 g_slist_free(machines);
1483 return mc;
1486 MachineClass *find_default_machine(void)
1488 GSList *el, *machines = object_class_get_list(TYPE_MACHINE, false);
1489 MachineClass *mc = NULL;
1491 for (el = machines; el; el = el->next) {
1492 MachineClass *temp = el->data;
1494 if (temp->is_default) {
1495 mc = temp;
1496 break;
1500 g_slist_free(machines);
1501 return mc;
1504 MachineInfoList *qmp_query_machines(Error **errp)
1506 GSList *el, *machines = object_class_get_list(TYPE_MACHINE, false);
1507 MachineInfoList *mach_list = NULL;
1509 for (el = machines; el; el = el->next) {
1510 MachineClass *mc = el->data;
1511 MachineInfoList *entry;
1512 MachineInfo *info;
1514 info = g_malloc0(sizeof(*info));
1515 if (mc->is_default) {
1516 info->has_is_default = true;
1517 info->is_default = true;
1520 if (mc->alias) {
1521 info->has_alias = true;
1522 info->alias = g_strdup(mc->alias);
1525 info->name = g_strdup(mc->name);
1526 info->cpu_max = !mc->max_cpus ? 1 : mc->max_cpus;
1527 info->hotpluggable_cpus = mc->has_hotpluggable_cpus;
1529 entry = g_malloc0(sizeof(*entry));
1530 entry->value = info;
1531 entry->next = mach_list;
1532 mach_list = entry;
1535 g_slist_free(machines);
1536 return mach_list;
1539 static int machine_help_func(QemuOpts *opts, MachineState *machine)
1541 ObjectProperty *prop;
1542 ObjectPropertyIterator iter;
1544 if (!qemu_opt_has_help_opt(opts)) {
1545 return 0;
1548 object_property_iter_init(&iter, OBJECT(machine));
1549 while ((prop = object_property_iter_next(&iter))) {
1550 if (!prop->set) {
1551 continue;
1554 error_printf("%s.%s=%s", MACHINE_GET_CLASS(machine)->name,
1555 prop->name, prop->type);
1556 if (prop->description) {
1557 error_printf(" (%s)\n", prop->description);
1558 } else {
1559 error_printf("\n");
1563 return 1;
1566 /***********************************************************/
1567 /* main execution loop */
1569 struct vm_change_state_entry {
1570 VMChangeStateHandler *cb;
1571 void *opaque;
1572 QLIST_ENTRY (vm_change_state_entry) entries;
1575 static QLIST_HEAD(vm_change_state_head, vm_change_state_entry) vm_change_state_head;
1577 VMChangeStateEntry *qemu_add_vm_change_state_handler(VMChangeStateHandler *cb,
1578 void *opaque)
1580 VMChangeStateEntry *e;
1582 e = g_malloc0(sizeof (*e));
1584 e->cb = cb;
1585 e->opaque = opaque;
1586 QLIST_INSERT_HEAD(&vm_change_state_head, e, entries);
1587 return e;
1590 void qemu_del_vm_change_state_handler(VMChangeStateEntry *e)
1592 QLIST_REMOVE (e, entries);
1593 g_free (e);
1596 void vm_state_notify(int running, RunState state)
1598 VMChangeStateEntry *e, *next;
1600 trace_vm_state_notify(running, state);
1602 QLIST_FOREACH_SAFE(e, &vm_change_state_head, entries, next) {
1603 e->cb(e->opaque, running, state);
1607 static int reset_requested;
1608 static int shutdown_requested, shutdown_signal = -1;
1609 static pid_t shutdown_pid;
1610 static int powerdown_requested;
1611 static int debug_requested;
1612 static int suspend_requested;
1613 static WakeupReason wakeup_reason;
1614 static NotifierList powerdown_notifiers =
1615 NOTIFIER_LIST_INITIALIZER(powerdown_notifiers);
1616 static NotifierList suspend_notifiers =
1617 NOTIFIER_LIST_INITIALIZER(suspend_notifiers);
1618 static NotifierList wakeup_notifiers =
1619 NOTIFIER_LIST_INITIALIZER(wakeup_notifiers);
1620 static uint32_t wakeup_reason_mask = ~(1 << QEMU_WAKEUP_REASON_NONE);
1622 int qemu_shutdown_requested_get(void)
1624 return shutdown_requested;
1627 int qemu_reset_requested_get(void)
1629 return reset_requested;
1632 static int qemu_shutdown_requested(void)
1634 return atomic_xchg(&shutdown_requested, 0);
1637 static void qemu_kill_report(void)
1639 if (!qtest_driver() && shutdown_signal != -1) {
1640 if (shutdown_pid == 0) {
1641 /* This happens for eg ^C at the terminal, so it's worth
1642 * avoiding printing an odd message in that case.
1644 error_report("terminating on signal %d", shutdown_signal);
1645 } else {
1646 char *shutdown_cmd = qemu_get_pid_name(shutdown_pid);
1648 error_report("terminating on signal %d from pid " FMT_pid " (%s)",
1649 shutdown_signal, shutdown_pid,
1650 shutdown_cmd ? shutdown_cmd : "<unknown process>");
1651 g_free(shutdown_cmd);
1653 shutdown_signal = -1;
1657 static int qemu_reset_requested(void)
1659 int r = reset_requested;
1660 if (r && replay_checkpoint(CHECKPOINT_RESET_REQUESTED)) {
1661 reset_requested = 0;
1662 return r;
1664 return false;
1667 static int qemu_suspend_requested(void)
1669 int r = suspend_requested;
1670 if (r && replay_checkpoint(CHECKPOINT_SUSPEND_REQUESTED)) {
1671 suspend_requested = 0;
1672 return r;
1674 return false;
1677 static WakeupReason qemu_wakeup_requested(void)
1679 return wakeup_reason;
1682 static int qemu_powerdown_requested(void)
1684 int r = powerdown_requested;
1685 powerdown_requested = 0;
1686 return r;
1689 static int qemu_debug_requested(void)
1691 int r = debug_requested;
1692 debug_requested = 0;
1693 return r;
1696 void qemu_system_reset(bool report)
1698 MachineClass *mc;
1700 mc = current_machine ? MACHINE_GET_CLASS(current_machine) : NULL;
1702 cpu_synchronize_all_states();
1704 if (mc && mc->reset) {
1705 mc->reset();
1706 } else {
1707 qemu_devices_reset();
1709 if (report) {
1710 qapi_event_send_reset(&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();
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(void)
1747 if (no_reboot) {
1748 shutdown_requested = 1;
1749 } else {
1750 reset_requested = 1;
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 = 1;
1818 qemu_notify_event();
1821 void qemu_system_shutdown_request(void)
1823 trace_qemu_system_shutdown_request();
1824 replay_shutdown_request();
1825 shutdown_requested = 1;
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 if (qemu_debug_requested()) {
1857 vm_stop(RUN_STATE_DEBUG);
1859 if (qemu_suspend_requested()) {
1860 qemu_system_suspend();
1862 if (qemu_shutdown_requested()) {
1863 qemu_kill_report();
1864 qapi_event_send_shutdown(&error_abort);
1865 if (no_shutdown) {
1866 vm_stop(RUN_STATE_SHUTDOWN);
1867 } else {
1868 return true;
1871 if (qemu_reset_requested()) {
1872 pause_all_vcpus();
1873 qemu_system_reset(VMRESET_REPORT);
1874 resume_all_vcpus();
1875 if (!runstate_check(RUN_STATE_RUNNING) &&
1876 !runstate_check(RUN_STATE_INMIGRATE)) {
1877 runstate_set(RUN_STATE_PRELAUNCH);
1880 if (qemu_wakeup_requested()) {
1881 pause_all_vcpus();
1882 qemu_system_reset(VMRESET_SILENT);
1883 notifier_list_notify(&wakeup_notifiers, &wakeup_reason);
1884 wakeup_reason = QEMU_WAKEUP_REASON_NONE;
1885 resume_all_vcpus();
1886 qapi_event_send_wakeup(&error_abort);
1888 if (qemu_powerdown_requested()) {
1889 qemu_system_powerdown();
1891 if (qemu_vmstop_requested(&r)) {
1892 vm_stop(r);
1894 return false;
1897 static void main_loop(void)
1899 #ifdef CONFIG_PROFILER
1900 int64_t ti;
1901 #endif
1902 do {
1903 #ifdef CONFIG_PROFILER
1904 ti = profile_getclock();
1905 #endif
1906 main_loop_wait(false);
1907 #ifdef CONFIG_PROFILER
1908 dev_time += profile_getclock() - ti;
1909 #endif
1910 } while (!main_loop_should_exit());
1913 static void version(void)
1915 printf("QEMU emulator version " QEMU_VERSION QEMU_PKGVERSION "\n"
1916 QEMU_COPYRIGHT "\n");
1919 static void help(int exitcode)
1921 version();
1922 printf("usage: %s [options] [disk_image]\n\n"
1923 "'disk_image' is a raw hard disk image for IDE hard disk 0\n\n",
1924 error_get_progname());
1926 #define QEMU_OPTIONS_GENERATE_HELP
1927 #include "qemu-options-wrapper.h"
1929 printf("\nDuring emulation, the following keys are useful:\n"
1930 "ctrl-alt-f toggle full screen\n"
1931 "ctrl-alt-n switch to virtual console 'n'\n"
1932 "ctrl-alt toggle mouse and keyboard grab\n"
1933 "\n"
1934 "When using -nographic, press 'ctrl-a h' to get some help.\n");
1936 exit(exitcode);
1939 #define HAS_ARG 0x0001
1941 typedef struct QEMUOption {
1942 const char *name;
1943 int flags;
1944 int index;
1945 uint32_t arch_mask;
1946 } QEMUOption;
1948 static const QEMUOption qemu_options[] = {
1949 { "h", 0, QEMU_OPTION_h, QEMU_ARCH_ALL },
1950 #define QEMU_OPTIONS_GENERATE_OPTIONS
1951 #include "qemu-options-wrapper.h"
1952 { NULL },
1955 typedef struct VGAInterfaceInfo {
1956 const char *opt_name; /* option name */
1957 const char *name; /* human-readable name */
1958 /* Class names indicating that support is available.
1959 * If no class is specified, the interface is always available */
1960 const char *class_names[2];
1961 } VGAInterfaceInfo;
1963 static VGAInterfaceInfo vga_interfaces[VGA_TYPE_MAX] = {
1964 [VGA_NONE] = {
1965 .opt_name = "none",
1967 [VGA_STD] = {
1968 .opt_name = "std",
1969 .name = "standard VGA",
1970 .class_names = { "VGA", "isa-vga" },
1972 [VGA_CIRRUS] = {
1973 .opt_name = "cirrus",
1974 .name = "Cirrus VGA",
1975 .class_names = { "cirrus-vga", "isa-cirrus-vga" },
1977 [VGA_VMWARE] = {
1978 .opt_name = "vmware",
1979 .name = "VMWare SVGA",
1980 .class_names = { "vmware-svga" },
1982 [VGA_VIRTIO] = {
1983 .opt_name = "virtio",
1984 .name = "Virtio VGA",
1985 .class_names = { "virtio-vga" },
1987 [VGA_QXL] = {
1988 .opt_name = "qxl",
1989 .name = "QXL VGA",
1990 .class_names = { "qxl-vga" },
1992 [VGA_TCX] = {
1993 .opt_name = "tcx",
1994 .name = "TCX framebuffer",
1995 .class_names = { "SUNW,tcx" },
1997 [VGA_CG3] = {
1998 .opt_name = "cg3",
1999 .name = "CG3 framebuffer",
2000 .class_names = { "cgthree" },
2002 [VGA_XENFB] = {
2003 .opt_name = "xenfb",
2007 static bool vga_interface_available(VGAInterfaceType t)
2009 VGAInterfaceInfo *ti = &vga_interfaces[t];
2011 assert(t < VGA_TYPE_MAX);
2012 return !ti->class_names[0] ||
2013 object_class_by_name(ti->class_names[0]) ||
2014 object_class_by_name(ti->class_names[1]);
2017 static void select_vgahw(const char *p)
2019 const char *opts;
2020 int t;
2022 assert(vga_interface_type == VGA_NONE);
2023 for (t = 0; t < VGA_TYPE_MAX; t++) {
2024 VGAInterfaceInfo *ti = &vga_interfaces[t];
2025 if (ti->opt_name && strstart(p, ti->opt_name, &opts)) {
2026 if (!vga_interface_available(t)) {
2027 error_report("%s not available", ti->name);
2028 exit(1);
2030 vga_interface_type = t;
2031 break;
2034 if (t == VGA_TYPE_MAX) {
2035 invalid_vga:
2036 error_report("unknown vga type: %s", p);
2037 exit(1);
2039 while (*opts) {
2040 const char *nextopt;
2042 if (strstart(opts, ",retrace=", &nextopt)) {
2043 opts = nextopt;
2044 if (strstart(opts, "dumb", &nextopt))
2045 vga_retrace_method = VGA_RETRACE_DUMB;
2046 else if (strstart(opts, "precise", &nextopt))
2047 vga_retrace_method = VGA_RETRACE_PRECISE;
2048 else goto invalid_vga;
2049 } else goto invalid_vga;
2050 opts = nextopt;
2054 typedef enum DisplayType {
2055 DT_DEFAULT,
2056 DT_CURSES,
2057 DT_SDL,
2058 DT_COCOA,
2059 DT_GTK,
2060 DT_EGL,
2061 DT_NONE,
2062 } DisplayType;
2064 static DisplayType select_display(const char *p)
2066 const char *opts;
2067 DisplayType display = DT_DEFAULT;
2069 if (strstart(p, "sdl", &opts)) {
2070 #ifdef CONFIG_SDL
2071 display = DT_SDL;
2072 while (*opts) {
2073 const char *nextopt;
2075 if (strstart(opts, ",frame=", &nextopt)) {
2076 opts = nextopt;
2077 if (strstart(opts, "on", &nextopt)) {
2078 no_frame = 0;
2079 } else if (strstart(opts, "off", &nextopt)) {
2080 no_frame = 1;
2081 } else {
2082 goto invalid_sdl_args;
2084 } else if (strstart(opts, ",alt_grab=", &nextopt)) {
2085 opts = nextopt;
2086 if (strstart(opts, "on", &nextopt)) {
2087 alt_grab = 1;
2088 } else if (strstart(opts, "off", &nextopt)) {
2089 alt_grab = 0;
2090 } else {
2091 goto invalid_sdl_args;
2093 } else if (strstart(opts, ",ctrl_grab=", &nextopt)) {
2094 opts = nextopt;
2095 if (strstart(opts, "on", &nextopt)) {
2096 ctrl_grab = 1;
2097 } else if (strstart(opts, "off", &nextopt)) {
2098 ctrl_grab = 0;
2099 } else {
2100 goto invalid_sdl_args;
2102 } else if (strstart(opts, ",window_close=", &nextopt)) {
2103 opts = nextopt;
2104 if (strstart(opts, "on", &nextopt)) {
2105 no_quit = 0;
2106 } else if (strstart(opts, "off", &nextopt)) {
2107 no_quit = 1;
2108 } else {
2109 goto invalid_sdl_args;
2111 } else if (strstart(opts, ",gl=", &nextopt)) {
2112 opts = nextopt;
2113 if (strstart(opts, "on", &nextopt)) {
2114 request_opengl = 1;
2115 } else if (strstart(opts, "off", &nextopt)) {
2116 request_opengl = 0;
2117 } else {
2118 goto invalid_sdl_args;
2120 } else {
2121 invalid_sdl_args:
2122 error_report("invalid SDL option string");
2123 exit(1);
2125 opts = nextopt;
2127 #else
2128 error_report("SDL support is disabled");
2129 exit(1);
2130 #endif
2131 } else if (strstart(p, "vnc", &opts)) {
2132 if (*opts == '=') {
2133 vnc_parse(opts + 1, &error_fatal);
2134 } else {
2135 error_report("VNC requires a display argument vnc=<display>");
2136 exit(1);
2138 } else if (strstart(p, "egl-headless", &opts)) {
2139 #ifdef CONFIG_OPENGL_DMABUF
2140 request_opengl = 1;
2141 display_opengl = 1;
2142 display = DT_EGL;
2143 #else
2144 fprintf(stderr, "egl support is disabled\n");
2145 exit(1);
2146 #endif
2147 } else if (strstart(p, "curses", &opts)) {
2148 #ifdef CONFIG_CURSES
2149 display = DT_CURSES;
2150 #else
2151 error_report("curses support is disabled");
2152 exit(1);
2153 #endif
2154 } else if (strstart(p, "gtk", &opts)) {
2155 #ifdef CONFIG_GTK
2156 display = DT_GTK;
2157 while (*opts) {
2158 const char *nextopt;
2160 if (strstart(opts, ",grab_on_hover=", &nextopt)) {
2161 opts = nextopt;
2162 if (strstart(opts, "on", &nextopt)) {
2163 grab_on_hover = true;
2164 } else if (strstart(opts, "off", &nextopt)) {
2165 grab_on_hover = false;
2166 } else {
2167 goto invalid_gtk_args;
2169 } else if (strstart(opts, ",gl=", &nextopt)) {
2170 opts = nextopt;
2171 if (strstart(opts, "on", &nextopt)) {
2172 request_opengl = 1;
2173 } else if (strstart(opts, "off", &nextopt)) {
2174 request_opengl = 0;
2175 } else {
2176 goto invalid_gtk_args;
2178 } else {
2179 invalid_gtk_args:
2180 error_report("invalid GTK option string");
2181 exit(1);
2183 opts = nextopt;
2185 #else
2186 error_report("GTK support is disabled");
2187 exit(1);
2188 #endif
2189 } else if (strstart(p, "none", &opts)) {
2190 display = DT_NONE;
2191 } else {
2192 error_report("unknown display type");
2193 exit(1);
2196 return display;
2199 static int balloon_parse(const char *arg)
2201 QemuOpts *opts;
2203 if (strcmp(arg, "none") == 0) {
2204 return 0;
2207 if (!strncmp(arg, "virtio", 6)) {
2208 if (arg[6] == ',') {
2209 /* have params -> parse them */
2210 opts = qemu_opts_parse_noisily(qemu_find_opts("device"), arg + 7,
2211 false);
2212 if (!opts)
2213 return -1;
2214 } else {
2215 /* create empty opts */
2216 opts = qemu_opts_create(qemu_find_opts("device"), NULL, 0,
2217 &error_abort);
2219 qemu_opt_set(opts, "driver", "virtio-balloon", &error_abort);
2220 return 0;
2223 return -1;
2226 char *qemu_find_file(int type, const char *name)
2228 int i;
2229 const char *subdir;
2230 char *buf;
2232 /* Try the name as a straight path first */
2233 if (access(name, R_OK) == 0) {
2234 trace_load_file(name, name);
2235 return g_strdup(name);
2238 switch (type) {
2239 case QEMU_FILE_TYPE_BIOS:
2240 subdir = "";
2241 break;
2242 case QEMU_FILE_TYPE_KEYMAP:
2243 subdir = "keymaps/";
2244 break;
2245 default:
2246 abort();
2249 for (i = 0; i < data_dir_idx; i++) {
2250 buf = g_strdup_printf("%s/%s%s", data_dir[i], subdir, name);
2251 if (access(buf, R_OK) == 0) {
2252 trace_load_file(name, buf);
2253 return buf;
2255 g_free(buf);
2257 return NULL;
2260 static inline bool nonempty_str(const char *str)
2262 return str && *str;
2265 static int parse_fw_cfg(void *opaque, QemuOpts *opts, Error **errp)
2267 gchar *buf;
2268 size_t size;
2269 const char *name, *file, *str;
2270 FWCfgState *fw_cfg = (FWCfgState *) opaque;
2272 if (fw_cfg == NULL) {
2273 error_report("fw_cfg device not available");
2274 return -1;
2276 name = qemu_opt_get(opts, "name");
2277 file = qemu_opt_get(opts, "file");
2278 str = qemu_opt_get(opts, "string");
2280 /* we need name and either a file or the content string */
2281 if (!(nonempty_str(name) && (nonempty_str(file) || nonempty_str(str)))) {
2282 error_report("invalid argument(s)");
2283 return -1;
2285 if (nonempty_str(file) && nonempty_str(str)) {
2286 error_report("file and string are mutually exclusive");
2287 return -1;
2289 if (strlen(name) > FW_CFG_MAX_FILE_PATH - 1) {
2290 error_report("name too long (max. %d char)", FW_CFG_MAX_FILE_PATH - 1);
2291 return -1;
2293 if (strncmp(name, "opt/", 4) != 0) {
2294 error_report("warning: externally provided fw_cfg item names "
2295 "should be prefixed with \"opt/\"");
2297 if (nonempty_str(str)) {
2298 size = strlen(str); /* NUL terminator NOT included in fw_cfg blob */
2299 buf = g_memdup(str, size);
2300 } else {
2301 if (!g_file_get_contents(file, &buf, &size, NULL)) {
2302 error_report("can't load %s", file);
2303 return -1;
2306 /* For legacy, keep user files in a specific global order. */
2307 fw_cfg_set_order_override(fw_cfg, FW_CFG_ORDER_OVERRIDE_USER);
2308 fw_cfg_add_file(fw_cfg, name, buf, size);
2309 fw_cfg_reset_order_override(fw_cfg);
2310 return 0;
2313 static int device_help_func(void *opaque, QemuOpts *opts, Error **errp)
2315 return qdev_device_help(opts);
2318 static int device_init_func(void *opaque, QemuOpts *opts, Error **errp)
2320 Error *err = NULL;
2321 DeviceState *dev;
2323 dev = qdev_device_add(opts, &err);
2324 if (!dev) {
2325 error_report_err(err);
2326 return -1;
2328 object_unref(OBJECT(dev));
2329 return 0;
2332 static int chardev_init_func(void *opaque, QemuOpts *opts, Error **errp)
2334 Error *local_err = NULL;
2336 qemu_chr_new_from_opts(opts, &local_err);
2337 if (local_err) {
2338 error_report_err(local_err);
2339 return -1;
2341 return 0;
2344 #ifdef CONFIG_VIRTFS
2345 static int fsdev_init_func(void *opaque, QemuOpts *opts, Error **errp)
2347 return qemu_fsdev_add(opts);
2349 #endif
2351 static int mon_init_func(void *opaque, QemuOpts *opts, Error **errp)
2353 Chardev *chr;
2354 const char *chardev;
2355 const char *mode;
2356 int flags;
2358 mode = qemu_opt_get(opts, "mode");
2359 if (mode == NULL) {
2360 mode = "readline";
2362 if (strcmp(mode, "readline") == 0) {
2363 flags = MONITOR_USE_READLINE;
2364 } else if (strcmp(mode, "control") == 0) {
2365 flags = MONITOR_USE_CONTROL;
2366 } else {
2367 error_report("unknown monitor mode \"%s\"", mode);
2368 exit(1);
2371 if (qemu_opt_get_bool(opts, "pretty", 0))
2372 flags |= MONITOR_USE_PRETTY;
2374 if (qemu_opt_get_bool(opts, "default", 0)) {
2375 error_report("option 'default' does nothing and is deprecated");
2378 chardev = qemu_opt_get(opts, "chardev");
2379 chr = qemu_chr_find(chardev);
2380 if (chr == NULL) {
2381 error_report("chardev \"%s\" not found", chardev);
2382 exit(1);
2385 monitor_init(chr, flags);
2386 return 0;
2389 static void monitor_parse(const char *optarg, const char *mode, bool pretty)
2391 static int monitor_device_index = 0;
2392 QemuOpts *opts;
2393 const char *p;
2394 char label[32];
2396 if (strstart(optarg, "chardev:", &p)) {
2397 snprintf(label, sizeof(label), "%s", p);
2398 } else {
2399 snprintf(label, sizeof(label), "compat_monitor%d",
2400 monitor_device_index);
2401 opts = qemu_chr_parse_compat(label, optarg);
2402 if (!opts) {
2403 error_report("parse error: %s", optarg);
2404 exit(1);
2408 opts = qemu_opts_create(qemu_find_opts("mon"), label, 1, &error_fatal);
2409 qemu_opt_set(opts, "mode", mode, &error_abort);
2410 qemu_opt_set(opts, "chardev", label, &error_abort);
2411 qemu_opt_set_bool(opts, "pretty", pretty, &error_abort);
2412 monitor_device_index++;
2415 struct device_config {
2416 enum {
2417 DEV_USB, /* -usbdevice */
2418 DEV_BT, /* -bt */
2419 DEV_SERIAL, /* -serial */
2420 DEV_PARALLEL, /* -parallel */
2421 DEV_VIRTCON, /* -virtioconsole */
2422 DEV_DEBUGCON, /* -debugcon */
2423 DEV_GDB, /* -gdb, -s */
2424 DEV_SCLP, /* s390 sclp */
2425 } type;
2426 const char *cmdline;
2427 Location loc;
2428 QTAILQ_ENTRY(device_config) next;
2431 static QTAILQ_HEAD(, device_config) device_configs =
2432 QTAILQ_HEAD_INITIALIZER(device_configs);
2434 static void add_device_config(int type, const char *cmdline)
2436 struct device_config *conf;
2438 conf = g_malloc0(sizeof(*conf));
2439 conf->type = type;
2440 conf->cmdline = cmdline;
2441 loc_save(&conf->loc);
2442 QTAILQ_INSERT_TAIL(&device_configs, conf, next);
2445 static int foreach_device_config(int type, int (*func)(const char *cmdline))
2447 struct device_config *conf;
2448 int rc;
2450 QTAILQ_FOREACH(conf, &device_configs, next) {
2451 if (conf->type != type)
2452 continue;
2453 loc_push_restore(&conf->loc);
2454 rc = func(conf->cmdline);
2455 loc_pop(&conf->loc);
2456 if (rc) {
2457 return rc;
2460 return 0;
2463 static int serial_parse(const char *devname)
2465 static int index = 0;
2466 char label[32];
2468 if (strcmp(devname, "none") == 0)
2469 return 0;
2470 if (index == MAX_SERIAL_PORTS) {
2471 error_report("too many serial ports");
2472 exit(1);
2474 snprintf(label, sizeof(label), "serial%d", index);
2475 serial_hds[index] = qemu_chr_new(label, devname);
2476 if (!serial_hds[index]) {
2477 error_report("could not connect serial device"
2478 " to character backend '%s'", devname);
2479 return -1;
2481 index++;
2482 return 0;
2485 static int parallel_parse(const char *devname)
2487 static int index = 0;
2488 char label[32];
2490 if (strcmp(devname, "none") == 0)
2491 return 0;
2492 if (index == MAX_PARALLEL_PORTS) {
2493 error_report("too many parallel ports");
2494 exit(1);
2496 snprintf(label, sizeof(label), "parallel%d", index);
2497 parallel_hds[index] = qemu_chr_new(label, devname);
2498 if (!parallel_hds[index]) {
2499 error_report("could not connect parallel device"
2500 " to character backend '%s'", devname);
2501 return -1;
2503 index++;
2504 return 0;
2507 static int virtcon_parse(const char *devname)
2509 QemuOptsList *device = qemu_find_opts("device");
2510 static int index = 0;
2511 char label[32];
2512 QemuOpts *bus_opts, *dev_opts;
2514 if (strcmp(devname, "none") == 0)
2515 return 0;
2516 if (index == MAX_VIRTIO_CONSOLES) {
2517 error_report("too many virtio consoles");
2518 exit(1);
2521 bus_opts = qemu_opts_create(device, NULL, 0, &error_abort);
2522 qemu_opt_set(bus_opts, "driver", "virtio-serial", &error_abort);
2524 dev_opts = qemu_opts_create(device, NULL, 0, &error_abort);
2525 qemu_opt_set(dev_opts, "driver", "virtconsole", &error_abort);
2527 snprintf(label, sizeof(label), "virtcon%d", index);
2528 virtcon_hds[index] = qemu_chr_new(label, devname);
2529 if (!virtcon_hds[index]) {
2530 error_report("could not connect virtio console"
2531 " to character backend '%s'", devname);
2532 return -1;
2534 qemu_opt_set(dev_opts, "chardev", label, &error_abort);
2536 index++;
2537 return 0;
2540 static int sclp_parse(const char *devname)
2542 QemuOptsList *device = qemu_find_opts("device");
2543 static int index = 0;
2544 char label[32];
2545 QemuOpts *dev_opts;
2547 if (strcmp(devname, "none") == 0) {
2548 return 0;
2550 if (index == MAX_SCLP_CONSOLES) {
2551 error_report("too many sclp consoles");
2552 exit(1);
2555 assert(arch_type == QEMU_ARCH_S390X);
2557 dev_opts = qemu_opts_create(device, NULL, 0, NULL);
2558 qemu_opt_set(dev_opts, "driver", "sclpconsole", &error_abort);
2560 snprintf(label, sizeof(label), "sclpcon%d", index);
2561 sclp_hds[index] = qemu_chr_new(label, devname);
2562 if (!sclp_hds[index]) {
2563 error_report("could not connect sclp console"
2564 " to character backend '%s'", devname);
2565 return -1;
2567 qemu_opt_set(dev_opts, "chardev", label, &error_abort);
2569 index++;
2570 return 0;
2573 static int debugcon_parse(const char *devname)
2575 QemuOpts *opts;
2577 if (!qemu_chr_new("debugcon", devname)) {
2578 exit(1);
2580 opts = qemu_opts_create(qemu_find_opts("device"), "debugcon", 1, NULL);
2581 if (!opts) {
2582 error_report("already have a debugcon device");
2583 exit(1);
2585 qemu_opt_set(opts, "driver", "isa-debugcon", &error_abort);
2586 qemu_opt_set(opts, "chardev", "debugcon", &error_abort);
2587 return 0;
2590 static gint machine_class_cmp(gconstpointer a, gconstpointer b)
2592 const MachineClass *mc1 = a, *mc2 = b;
2593 int res;
2595 if (mc1->family == NULL) {
2596 if (mc2->family == NULL) {
2597 /* Compare standalone machine types against each other; they sort
2598 * in increasing order.
2600 return strcmp(object_class_get_name(OBJECT_CLASS(mc1)),
2601 object_class_get_name(OBJECT_CLASS(mc2)));
2604 /* Standalone machine types sort after families. */
2605 return 1;
2608 if (mc2->family == NULL) {
2609 /* Families sort before standalone machine types. */
2610 return -1;
2613 /* Families sort between each other alphabetically increasingly. */
2614 res = strcmp(mc1->family, mc2->family);
2615 if (res != 0) {
2616 return res;
2619 /* Within the same family, machine types sort in decreasing order. */
2620 return strcmp(object_class_get_name(OBJECT_CLASS(mc2)),
2621 object_class_get_name(OBJECT_CLASS(mc1)));
2624 static MachineClass *machine_parse(const char *name)
2626 MachineClass *mc = NULL;
2627 GSList *el, *machines = object_class_get_list(TYPE_MACHINE, false);
2629 if (name) {
2630 mc = find_machine(name);
2632 if (mc) {
2633 g_slist_free(machines);
2634 return mc;
2636 if (name && !is_help_option(name)) {
2637 error_report("unsupported machine type");
2638 error_printf("Use -machine help to list supported machines\n");
2639 } else {
2640 printf("Supported machines are:\n");
2641 machines = g_slist_sort(machines, machine_class_cmp);
2642 for (el = machines; el; el = el->next) {
2643 MachineClass *mc = el->data;
2644 if (mc->alias) {
2645 printf("%-20s %s (alias of %s)\n", mc->alias, mc->desc, mc->name);
2647 printf("%-20s %s%s\n", mc->name, mc->desc,
2648 mc->is_default ? " (default)" : "");
2652 g_slist_free(machines);
2653 exit(!name || !is_help_option(name));
2656 void qemu_add_exit_notifier(Notifier *notify)
2658 notifier_list_add(&exit_notifiers, notify);
2661 void qemu_remove_exit_notifier(Notifier *notify)
2663 notifier_remove(notify);
2666 static void qemu_run_exit_notifiers(void)
2668 notifier_list_notify(&exit_notifiers, NULL);
2671 static bool machine_init_done;
2673 void qemu_add_machine_init_done_notifier(Notifier *notify)
2675 notifier_list_add(&machine_init_done_notifiers, notify);
2676 if (machine_init_done) {
2677 notify->notify(notify, NULL);
2681 void qemu_remove_machine_init_done_notifier(Notifier *notify)
2683 notifier_remove(notify);
2686 static void qemu_run_machine_init_done_notifiers(void)
2688 notifier_list_notify(&machine_init_done_notifiers, NULL);
2689 machine_init_done = true;
2692 static const QEMUOption *lookup_opt(int argc, char **argv,
2693 const char **poptarg, int *poptind)
2695 const QEMUOption *popt;
2696 int optind = *poptind;
2697 char *r = argv[optind];
2698 const char *optarg;
2700 loc_set_cmdline(argv, optind, 1);
2701 optind++;
2702 /* Treat --foo the same as -foo. */
2703 if (r[1] == '-')
2704 r++;
2705 popt = qemu_options;
2706 for(;;) {
2707 if (!popt->name) {
2708 error_report("invalid option");
2709 exit(1);
2711 if (!strcmp(popt->name, r + 1))
2712 break;
2713 popt++;
2715 if (popt->flags & HAS_ARG) {
2716 if (optind >= argc) {
2717 error_report("requires an argument");
2718 exit(1);
2720 optarg = argv[optind++];
2721 loc_set_cmdline(argv, optind - 2, 2);
2722 } else {
2723 optarg = NULL;
2726 *poptarg = optarg;
2727 *poptind = optind;
2729 return popt;
2732 static MachineClass *select_machine(void)
2734 MachineClass *machine_class = find_default_machine();
2735 const char *optarg;
2736 QemuOpts *opts;
2737 Location loc;
2739 loc_push_none(&loc);
2741 opts = qemu_get_machine_opts();
2742 qemu_opts_loc_restore(opts);
2744 optarg = qemu_opt_get(opts, "type");
2745 if (optarg) {
2746 machine_class = machine_parse(optarg);
2749 if (!machine_class) {
2750 error_report("No machine specified, and there is no default");
2751 error_printf("Use -machine help to list supported machines\n");
2752 exit(1);
2755 loc_pop(&loc);
2756 return machine_class;
2759 static int machine_set_property(void *opaque,
2760 const char *name, const char *value,
2761 Error **errp)
2763 Object *obj = OBJECT(opaque);
2764 Error *local_err = NULL;
2765 char *p, *qom_name;
2767 if (strcmp(name, "type") == 0) {
2768 return 0;
2771 qom_name = g_strdup(name);
2772 for (p = qom_name; *p; p++) {
2773 if (*p == '_') {
2774 *p = '-';
2778 object_property_parse(obj, value, qom_name, &local_err);
2779 g_free(qom_name);
2781 if (local_err) {
2782 error_report_err(local_err);
2783 return -1;
2786 return 0;
2791 * Initial object creation happens before all other
2792 * QEMU data types are created. The majority of objects
2793 * can be created at this point. The rng-egd object
2794 * cannot be created here, as it depends on the chardev
2795 * already existing.
2797 static bool object_create_initial(const char *type)
2799 if (g_str_equal(type, "rng-egd")) {
2800 return false;
2804 * return false for concrete netfilters since
2805 * they depend on netdevs already existing
2807 if (g_str_equal(type, "filter-buffer") ||
2808 g_str_equal(type, "filter-dump") ||
2809 g_str_equal(type, "filter-mirror") ||
2810 g_str_equal(type, "filter-redirector") ||
2811 g_str_equal(type, "colo-compare") ||
2812 g_str_equal(type, "filter-rewriter") ||
2813 g_str_equal(type, "filter-replay")) {
2814 return false;
2817 /* Memory allocation by backends needs to be done
2818 * after configure_accelerator() (due to the tcg_enabled()
2819 * checks at memory_region_init_*()).
2821 * Also, allocation of large amounts of memory may delay
2822 * chardev initialization for too long, and trigger timeouts
2823 * on software that waits for a monitor socket to be created
2824 * (e.g. libvirt).
2826 if (g_str_has_prefix(type, "memory-backend-")) {
2827 return false;
2830 return true;
2835 * The remainder of object creation happens after the
2836 * creation of chardev, fsdev, net clients and device data types.
2838 static bool object_create_delayed(const char *type)
2840 return !object_create_initial(type);
2844 static void set_memory_options(uint64_t *ram_slots, ram_addr_t *maxram_size,
2845 MachineClass *mc)
2847 uint64_t sz;
2848 const char *mem_str;
2849 const char *maxmem_str, *slots_str;
2850 const ram_addr_t default_ram_size = mc->default_ram_size;
2851 QemuOpts *opts = qemu_find_opts_singleton("memory");
2852 Location loc;
2854 loc_push_none(&loc);
2855 qemu_opts_loc_restore(opts);
2857 sz = 0;
2858 mem_str = qemu_opt_get(opts, "size");
2859 if (mem_str) {
2860 if (!*mem_str) {
2861 error_report("missing 'size' option value");
2862 exit(EXIT_FAILURE);
2865 sz = qemu_opt_get_size(opts, "size", ram_size);
2867 /* Fix up legacy suffix-less format */
2868 if (g_ascii_isdigit(mem_str[strlen(mem_str) - 1])) {
2869 uint64_t overflow_check = sz;
2871 sz <<= 20;
2872 if ((sz >> 20) != overflow_check) {
2873 error_report("too large 'size' option value");
2874 exit(EXIT_FAILURE);
2879 /* backward compatibility behaviour for case "-m 0" */
2880 if (sz == 0) {
2881 sz = default_ram_size;
2884 sz = QEMU_ALIGN_UP(sz, 8192);
2885 ram_size = sz;
2886 if (ram_size != sz) {
2887 error_report("ram size too large");
2888 exit(EXIT_FAILURE);
2891 /* store value for the future use */
2892 qemu_opt_set_number(opts, "size", ram_size, &error_abort);
2893 *maxram_size = ram_size;
2895 maxmem_str = qemu_opt_get(opts, "maxmem");
2896 slots_str = qemu_opt_get(opts, "slots");
2897 if (maxmem_str && slots_str) {
2898 uint64_t slots;
2900 sz = qemu_opt_get_size(opts, "maxmem", 0);
2901 slots = qemu_opt_get_number(opts, "slots", 0);
2902 if (sz < ram_size) {
2903 error_report("invalid value of -m option maxmem: "
2904 "maximum memory size (0x%" PRIx64 ") must be at least "
2905 "the initial memory size (0x" RAM_ADDR_FMT ")",
2906 sz, ram_size);
2907 exit(EXIT_FAILURE);
2908 } else if (sz > ram_size) {
2909 if (!slots) {
2910 error_report("invalid value of -m option: maxmem was "
2911 "specified, but no hotplug slots were specified");
2912 exit(EXIT_FAILURE);
2914 } else if (slots) {
2915 error_report("invalid value of -m option maxmem: "
2916 "memory slots were specified but maximum memory size "
2917 "(0x%" PRIx64 ") is equal to the initial memory size "
2918 "(0x" RAM_ADDR_FMT ")", sz, ram_size);
2919 exit(EXIT_FAILURE);
2922 *maxram_size = sz;
2923 *ram_slots = slots;
2924 } else if ((!maxmem_str && slots_str) ||
2925 (maxmem_str && !slots_str)) {
2926 error_report("invalid -m option value: missing "
2927 "'%s' option", slots_str ? "maxmem" : "slots");
2928 exit(EXIT_FAILURE);
2931 loc_pop(&loc);
2934 static int global_init_func(void *opaque, QemuOpts *opts, Error **errp)
2936 GlobalProperty *g;
2938 g = g_malloc0(sizeof(*g));
2939 g->driver = qemu_opt_get(opts, "driver");
2940 g->property = qemu_opt_get(opts, "property");
2941 g->value = qemu_opt_get(opts, "value");
2942 g->user_provided = true;
2943 g->errp = &error_fatal;
2944 qdev_prop_register_global(g);
2945 return 0;
2948 static int qemu_read_default_config_file(void)
2950 int ret;
2952 ret = qemu_read_config_file(CONFIG_QEMU_CONFDIR "/qemu.conf");
2953 if (ret < 0 && ret != -ENOENT) {
2954 return ret;
2957 return 0;
2960 int main(int argc, char **argv, char **envp)
2962 int i;
2963 int snapshot, linux_boot;
2964 const char *initrd_filename;
2965 const char *kernel_filename, *kernel_cmdline;
2966 const char *boot_order = NULL;
2967 const char *boot_once = NULL;
2968 DisplayState *ds;
2969 int cyls, heads, secs, translation;
2970 QemuOpts *opts, *machine_opts;
2971 QemuOpts *hda_opts = NULL, *icount_opts = NULL, *accel_opts = NULL;
2972 QemuOptsList *olist;
2973 int optind;
2974 const char *optarg;
2975 const char *loadvm = NULL;
2976 MachineClass *machine_class;
2977 const char *cpu_model;
2978 const char *vga_model = NULL;
2979 const char *qtest_chrdev = NULL;
2980 const char *qtest_log = NULL;
2981 const char *pid_file = NULL;
2982 const char *incoming = NULL;
2983 bool defconfig = true;
2984 bool userconfig = true;
2985 bool nographic = false;
2986 DisplayType display_type = DT_DEFAULT;
2987 int display_remote = 0;
2988 const char *log_mask = NULL;
2989 const char *log_file = NULL;
2990 char *trace_file = NULL;
2991 ram_addr_t maxram_size;
2992 uint64_t ram_slots = 0;
2993 FILE *vmstate_dump_file = NULL;
2994 Error *main_loop_err = NULL;
2995 Error *err = NULL;
2996 bool list_data_dirs = false;
2997 typedef struct BlockdevOptions_queue {
2998 BlockdevOptions *bdo;
2999 Location loc;
3000 QSIMPLEQ_ENTRY(BlockdevOptions_queue) entry;
3001 } BlockdevOptions_queue;
3002 QSIMPLEQ_HEAD(, BlockdevOptions_queue) bdo_queue
3003 = QSIMPLEQ_HEAD_INITIALIZER(bdo_queue);
3005 module_call_init(MODULE_INIT_TRACE);
3007 qemu_init_cpu_list();
3008 qemu_init_cpu_loop();
3009 qemu_mutex_lock_iothread();
3011 atexit(qemu_run_exit_notifiers);
3012 error_set_progname(argv[0]);
3013 qemu_init_exec_dir(argv[0]);
3015 module_call_init(MODULE_INIT_QOM);
3016 monitor_init_qmp_commands();
3018 qemu_add_opts(&qemu_drive_opts);
3019 qemu_add_drive_opts(&qemu_legacy_drive_opts);
3020 qemu_add_drive_opts(&qemu_common_drive_opts);
3021 qemu_add_drive_opts(&qemu_drive_opts);
3022 qemu_add_drive_opts(&bdrv_runtime_opts);
3023 qemu_add_opts(&qemu_chardev_opts);
3024 qemu_add_opts(&qemu_device_opts);
3025 qemu_add_opts(&qemu_netdev_opts);
3026 qemu_add_opts(&qemu_net_opts);
3027 qemu_add_opts(&qemu_rtc_opts);
3028 qemu_add_opts(&qemu_global_opts);
3029 qemu_add_opts(&qemu_mon_opts);
3030 qemu_add_opts(&qemu_trace_opts);
3031 qemu_add_opts(&qemu_option_rom_opts);
3032 qemu_add_opts(&qemu_machine_opts);
3033 qemu_add_opts(&qemu_accel_opts);
3034 qemu_add_opts(&qemu_mem_opts);
3035 qemu_add_opts(&qemu_smp_opts);
3036 qemu_add_opts(&qemu_boot_opts);
3037 qemu_add_opts(&qemu_sandbox_opts);
3038 qemu_add_opts(&qemu_add_fd_opts);
3039 qemu_add_opts(&qemu_object_opts);
3040 qemu_add_opts(&qemu_tpmdev_opts);
3041 qemu_add_opts(&qemu_realtime_opts);
3042 qemu_add_opts(&qemu_msg_opts);
3043 qemu_add_opts(&qemu_name_opts);
3044 qemu_add_opts(&qemu_numa_opts);
3045 qemu_add_opts(&qemu_icount_opts);
3046 qemu_add_opts(&qemu_semihosting_config_opts);
3047 qemu_add_opts(&qemu_fw_cfg_opts);
3048 module_call_init(MODULE_INIT_OPTS);
3050 runstate_init();
3052 if (qcrypto_init(&err) < 0) {
3053 error_reportf_err(err, "cannot initialize crypto: ");
3054 exit(1);
3056 rtc_clock = QEMU_CLOCK_HOST;
3058 QLIST_INIT (&vm_change_state_head);
3059 os_setup_early_signal_handling();
3061 cpu_model = NULL;
3062 snapshot = 0;
3063 cyls = heads = secs = 0;
3064 translation = BIOS_ATA_TRANSLATION_AUTO;
3066 nb_nics = 0;
3068 bdrv_init_with_whitelist();
3070 autostart = 1;
3072 /* first pass of option parsing */
3073 optind = 1;
3074 while (optind < argc) {
3075 if (argv[optind][0] != '-') {
3076 /* disk image */
3077 optind++;
3078 } else {
3079 const QEMUOption *popt;
3081 popt = lookup_opt(argc, argv, &optarg, &optind);
3082 switch (popt->index) {
3083 case QEMU_OPTION_nodefconfig:
3084 defconfig = false;
3085 break;
3086 case QEMU_OPTION_nouserconfig:
3087 userconfig = false;
3088 break;
3093 if (defconfig && userconfig) {
3094 if (qemu_read_default_config_file() < 0) {
3095 exit(1);
3099 /* second pass of option parsing */
3100 optind = 1;
3101 for(;;) {
3102 if (optind >= argc)
3103 break;
3104 if (argv[optind][0] != '-') {
3105 hda_opts = drive_add(IF_DEFAULT, 0, argv[optind++], HD_OPTS);
3106 } else {
3107 const QEMUOption *popt;
3109 popt = lookup_opt(argc, argv, &optarg, &optind);
3110 if (!(popt->arch_mask & arch_type)) {
3111 error_report("Option not supported for this target");
3112 exit(1);
3114 switch(popt->index) {
3115 case QEMU_OPTION_no_kvm_irqchip: {
3116 olist = qemu_find_opts("machine");
3117 qemu_opts_parse_noisily(olist, "kernel_irqchip=off", false);
3118 break;
3120 case QEMU_OPTION_cpu:
3121 /* hw initialization will check this */
3122 cpu_model = optarg;
3123 break;
3124 case QEMU_OPTION_hda:
3126 char buf[256];
3127 if (cyls == 0)
3128 snprintf(buf, sizeof(buf), "%s", HD_OPTS);
3129 else
3130 snprintf(buf, sizeof(buf),
3131 "%s,cyls=%d,heads=%d,secs=%d%s",
3132 HD_OPTS , cyls, heads, secs,
3133 translation == BIOS_ATA_TRANSLATION_LBA ?
3134 ",trans=lba" :
3135 translation == BIOS_ATA_TRANSLATION_NONE ?
3136 ",trans=none" : "");
3137 drive_add(IF_DEFAULT, 0, optarg, buf);
3138 break;
3140 case QEMU_OPTION_hdb:
3141 case QEMU_OPTION_hdc:
3142 case QEMU_OPTION_hdd:
3143 drive_add(IF_DEFAULT, popt->index - QEMU_OPTION_hda, optarg,
3144 HD_OPTS);
3145 break;
3146 case QEMU_OPTION_blockdev:
3148 Visitor *v;
3149 BlockdevOptions_queue *bdo;
3151 v = qobject_input_visitor_new_str(optarg, "driver", &err);
3152 if (!v) {
3153 error_report_err(err);
3154 exit(1);
3157 bdo = g_new(BlockdevOptions_queue, 1);
3158 visit_type_BlockdevOptions(v, NULL, &bdo->bdo,
3159 &error_fatal);
3160 visit_free(v);
3161 loc_save(&bdo->loc);
3162 QSIMPLEQ_INSERT_TAIL(&bdo_queue, bdo, entry);
3163 break;
3165 case QEMU_OPTION_drive:
3166 if (drive_def(optarg) == NULL) {
3167 exit(1);
3169 break;
3170 case QEMU_OPTION_set:
3171 if (qemu_set_option(optarg) != 0)
3172 exit(1);
3173 break;
3174 case QEMU_OPTION_global:
3175 if (qemu_global_option(optarg) != 0)
3176 exit(1);
3177 break;
3178 case QEMU_OPTION_mtdblock:
3179 drive_add(IF_MTD, -1, optarg, MTD_OPTS);
3180 break;
3181 case QEMU_OPTION_sd:
3182 drive_add(IF_SD, -1, optarg, SD_OPTS);
3183 break;
3184 case QEMU_OPTION_pflash:
3185 drive_add(IF_PFLASH, -1, optarg, PFLASH_OPTS);
3186 break;
3187 case QEMU_OPTION_snapshot:
3188 snapshot = 1;
3189 break;
3190 case QEMU_OPTION_hdachs:
3192 const char *p;
3193 p = optarg;
3194 cyls = strtol(p, (char **)&p, 0);
3195 if (cyls < 1 || cyls > 16383)
3196 goto chs_fail;
3197 if (*p != ',')
3198 goto chs_fail;
3199 p++;
3200 heads = strtol(p, (char **)&p, 0);
3201 if (heads < 1 || heads > 16)
3202 goto chs_fail;
3203 if (*p != ',')
3204 goto chs_fail;
3205 p++;
3206 secs = strtol(p, (char **)&p, 0);
3207 if (secs < 1 || secs > 63)
3208 goto chs_fail;
3209 if (*p == ',') {
3210 p++;
3211 if (!strcmp(p, "large")) {
3212 translation = BIOS_ATA_TRANSLATION_LARGE;
3213 } else if (!strcmp(p, "rechs")) {
3214 translation = BIOS_ATA_TRANSLATION_RECHS;
3215 } else if (!strcmp(p, "none")) {
3216 translation = BIOS_ATA_TRANSLATION_NONE;
3217 } else if (!strcmp(p, "lba")) {
3218 translation = BIOS_ATA_TRANSLATION_LBA;
3219 } else if (!strcmp(p, "auto")) {
3220 translation = BIOS_ATA_TRANSLATION_AUTO;
3221 } else {
3222 goto chs_fail;
3224 } else if (*p != '\0') {
3225 chs_fail:
3226 error_report("invalid physical CHS format");
3227 exit(1);
3229 if (hda_opts != NULL) {
3230 qemu_opt_set_number(hda_opts, "cyls", cyls,
3231 &error_abort);
3232 qemu_opt_set_number(hda_opts, "heads", heads,
3233 &error_abort);
3234 qemu_opt_set_number(hda_opts, "secs", secs,
3235 &error_abort);
3236 if (translation == BIOS_ATA_TRANSLATION_LARGE) {
3237 qemu_opt_set(hda_opts, "trans", "large",
3238 &error_abort);
3239 } else if (translation == BIOS_ATA_TRANSLATION_RECHS) {
3240 qemu_opt_set(hda_opts, "trans", "rechs",
3241 &error_abort);
3242 } else if (translation == BIOS_ATA_TRANSLATION_LBA) {
3243 qemu_opt_set(hda_opts, "trans", "lba",
3244 &error_abort);
3245 } else if (translation == BIOS_ATA_TRANSLATION_NONE) {
3246 qemu_opt_set(hda_opts, "trans", "none",
3247 &error_abort);
3251 error_report("'-hdachs' is deprecated, please use '-device"
3252 " ide-hd,cyls=c,heads=h,secs=s,...' instead");
3253 break;
3254 case QEMU_OPTION_numa:
3255 opts = qemu_opts_parse_noisily(qemu_find_opts("numa"),
3256 optarg, true);
3257 if (!opts) {
3258 exit(1);
3260 break;
3261 case QEMU_OPTION_display:
3262 display_type = select_display(optarg);
3263 break;
3264 case QEMU_OPTION_nographic:
3265 olist = qemu_find_opts("machine");
3266 qemu_opts_parse_noisily(olist, "graphics=off", false);
3267 nographic = true;
3268 display_type = DT_NONE;
3269 break;
3270 case QEMU_OPTION_curses:
3271 #ifdef CONFIG_CURSES
3272 display_type = DT_CURSES;
3273 #else
3274 error_report("curses support is disabled");
3275 exit(1);
3276 #endif
3277 break;
3278 case QEMU_OPTION_portrait:
3279 graphic_rotate = 90;
3280 break;
3281 case QEMU_OPTION_rotate:
3282 graphic_rotate = strtol(optarg, (char **) &optarg, 10);
3283 if (graphic_rotate != 0 && graphic_rotate != 90 &&
3284 graphic_rotate != 180 && graphic_rotate != 270) {
3285 error_report("only 90, 180, 270 deg rotation is available");
3286 exit(1);
3288 break;
3289 case QEMU_OPTION_kernel:
3290 qemu_opts_set(qemu_find_opts("machine"), 0, "kernel", optarg,
3291 &error_abort);
3292 break;
3293 case QEMU_OPTION_initrd:
3294 qemu_opts_set(qemu_find_opts("machine"), 0, "initrd", optarg,
3295 &error_abort);
3296 break;
3297 case QEMU_OPTION_append:
3298 qemu_opts_set(qemu_find_opts("machine"), 0, "append", optarg,
3299 &error_abort);
3300 break;
3301 case QEMU_OPTION_dtb:
3302 qemu_opts_set(qemu_find_opts("machine"), 0, "dtb", optarg,
3303 &error_abort);
3304 break;
3305 case QEMU_OPTION_cdrom:
3306 drive_add(IF_DEFAULT, 2, optarg, CDROM_OPTS);
3307 break;
3308 case QEMU_OPTION_boot:
3309 opts = qemu_opts_parse_noisily(qemu_find_opts("boot-opts"),
3310 optarg, true);
3311 if (!opts) {
3312 exit(1);
3314 break;
3315 case QEMU_OPTION_fda:
3316 case QEMU_OPTION_fdb:
3317 drive_add(IF_FLOPPY, popt->index - QEMU_OPTION_fda,
3318 optarg, FD_OPTS);
3319 break;
3320 case QEMU_OPTION_no_fd_bootchk:
3321 fd_bootchk = 0;
3322 break;
3323 case QEMU_OPTION_netdev:
3324 default_net = 0;
3325 if (net_client_parse(qemu_find_opts("netdev"), optarg) == -1) {
3326 exit(1);
3328 break;
3329 case QEMU_OPTION_net:
3330 default_net = 0;
3331 if (net_client_parse(qemu_find_opts("net"), optarg) == -1) {
3332 exit(1);
3334 break;
3335 #ifdef CONFIG_LIBISCSI
3336 case QEMU_OPTION_iscsi:
3337 opts = qemu_opts_parse_noisily(qemu_find_opts("iscsi"),
3338 optarg, false);
3339 if (!opts) {
3340 exit(1);
3342 break;
3343 #endif
3344 #ifdef CONFIG_SLIRP
3345 case QEMU_OPTION_tftp:
3346 error_report("The -tftp option is deprecated. "
3347 "Please use '-netdev user,tftp=...' instead.");
3348 legacy_tftp_prefix = optarg;
3349 break;
3350 case QEMU_OPTION_bootp:
3351 error_report("The -bootp option is deprecated. "
3352 "Please use '-netdev user,bootfile=...' instead.");
3353 legacy_bootp_filename = optarg;
3354 break;
3355 case QEMU_OPTION_redir:
3356 error_report("The -redir option is deprecated. "
3357 "Please use '-netdev user,hostfwd=...' instead.");
3358 if (net_slirp_redir(optarg) < 0)
3359 exit(1);
3360 break;
3361 #endif
3362 case QEMU_OPTION_bt:
3363 add_device_config(DEV_BT, optarg);
3364 break;
3365 case QEMU_OPTION_audio_help:
3366 AUD_help ();
3367 exit (0);
3368 break;
3369 case QEMU_OPTION_soundhw:
3370 select_soundhw (optarg);
3371 break;
3372 case QEMU_OPTION_h:
3373 help(0);
3374 break;
3375 case QEMU_OPTION_version:
3376 version();
3377 exit(0);
3378 break;
3379 case QEMU_OPTION_m:
3380 opts = qemu_opts_parse_noisily(qemu_find_opts("memory"),
3381 optarg, true);
3382 if (!opts) {
3383 exit(EXIT_FAILURE);
3385 break;
3386 #ifdef CONFIG_TPM
3387 case QEMU_OPTION_tpmdev:
3388 if (tpm_config_parse(qemu_find_opts("tpmdev"), optarg) < 0) {
3389 exit(1);
3391 break;
3392 #endif
3393 case QEMU_OPTION_mempath:
3394 mem_path = optarg;
3395 break;
3396 case QEMU_OPTION_mem_prealloc:
3397 mem_prealloc = 1;
3398 break;
3399 case QEMU_OPTION_d:
3400 log_mask = optarg;
3401 break;
3402 case QEMU_OPTION_D:
3403 log_file = optarg;
3404 break;
3405 case QEMU_OPTION_DFILTER:
3406 qemu_set_dfilter_ranges(optarg, &error_fatal);
3407 break;
3408 case QEMU_OPTION_s:
3409 add_device_config(DEV_GDB, "tcp::" DEFAULT_GDBSTUB_PORT);
3410 break;
3411 case QEMU_OPTION_gdb:
3412 add_device_config(DEV_GDB, optarg);
3413 break;
3414 case QEMU_OPTION_L:
3415 if (is_help_option(optarg)) {
3416 list_data_dirs = true;
3417 } else if (data_dir_idx < ARRAY_SIZE(data_dir)) {
3418 data_dir[data_dir_idx++] = optarg;
3420 break;
3421 case QEMU_OPTION_bios:
3422 qemu_opts_set(qemu_find_opts("machine"), 0, "firmware", optarg,
3423 &error_abort);
3424 break;
3425 case QEMU_OPTION_singlestep:
3426 singlestep = 1;
3427 break;
3428 case QEMU_OPTION_S:
3429 autostart = 0;
3430 break;
3431 case QEMU_OPTION_k:
3432 keyboard_layout = optarg;
3433 break;
3434 case QEMU_OPTION_localtime:
3435 rtc_utc = 0;
3436 break;
3437 case QEMU_OPTION_vga:
3438 vga_model = optarg;
3439 default_vga = 0;
3440 break;
3441 case QEMU_OPTION_g:
3443 const char *p;
3444 int w, h, depth;
3445 p = optarg;
3446 w = strtol(p, (char **)&p, 10);
3447 if (w <= 0) {
3448 graphic_error:
3449 error_report("invalid resolution or depth");
3450 exit(1);
3452 if (*p != 'x')
3453 goto graphic_error;
3454 p++;
3455 h = strtol(p, (char **)&p, 10);
3456 if (h <= 0)
3457 goto graphic_error;
3458 if (*p == 'x') {
3459 p++;
3460 depth = strtol(p, (char **)&p, 10);
3461 if (depth != 8 && depth != 15 && depth != 16 &&
3462 depth != 24 && depth != 32)
3463 goto graphic_error;
3464 } else if (*p == '\0') {
3465 depth = graphic_depth;
3466 } else {
3467 goto graphic_error;
3470 graphic_width = w;
3471 graphic_height = h;
3472 graphic_depth = depth;
3474 break;
3475 case QEMU_OPTION_echr:
3477 char *r;
3478 term_escape_char = strtol(optarg, &r, 0);
3479 if (r == optarg)
3480 printf("Bad argument to echr\n");
3481 break;
3483 case QEMU_OPTION_monitor:
3484 default_monitor = 0;
3485 if (strncmp(optarg, "none", 4)) {
3486 monitor_parse(optarg, "readline", false);
3488 break;
3489 case QEMU_OPTION_qmp:
3490 monitor_parse(optarg, "control", false);
3491 default_monitor = 0;
3492 break;
3493 case QEMU_OPTION_qmp_pretty:
3494 monitor_parse(optarg, "control", true);
3495 default_monitor = 0;
3496 break;
3497 case QEMU_OPTION_mon:
3498 opts = qemu_opts_parse_noisily(qemu_find_opts("mon"), optarg,
3499 true);
3500 if (!opts) {
3501 exit(1);
3503 default_monitor = 0;
3504 break;
3505 case QEMU_OPTION_chardev:
3506 opts = qemu_opts_parse_noisily(qemu_find_opts("chardev"),
3507 optarg, true);
3508 if (!opts) {
3509 exit(1);
3511 break;
3512 case QEMU_OPTION_fsdev:
3513 olist = qemu_find_opts("fsdev");
3514 if (!olist) {
3515 error_report("fsdev support is disabled");
3516 exit(1);
3518 opts = qemu_opts_parse_noisily(olist, optarg, true);
3519 if (!opts) {
3520 exit(1);
3522 break;
3523 case QEMU_OPTION_virtfs: {
3524 QemuOpts *fsdev;
3525 QemuOpts *device;
3526 const char *writeout, *sock_fd, *socket;
3528 olist = qemu_find_opts("virtfs");
3529 if (!olist) {
3530 error_report("virtfs support is disabled");
3531 exit(1);
3533 opts = qemu_opts_parse_noisily(olist, optarg, true);
3534 if (!opts) {
3535 exit(1);
3538 if (qemu_opt_get(opts, "fsdriver") == NULL ||
3539 qemu_opt_get(opts, "mount_tag") == NULL) {
3540 error_report("Usage: -virtfs fsdriver,mount_tag=tag");
3541 exit(1);
3543 fsdev = qemu_opts_create(qemu_find_opts("fsdev"),
3544 qemu_opts_id(opts) ?:
3545 qemu_opt_get(opts, "mount_tag"),
3546 1, NULL);
3547 if (!fsdev) {
3548 error_report("duplicate or invalid fsdev id: %s",
3549 qemu_opt_get(opts, "mount_tag"));
3550 exit(1);
3553 writeout = qemu_opt_get(opts, "writeout");
3554 if (writeout) {
3555 #ifdef CONFIG_SYNC_FILE_RANGE
3556 qemu_opt_set(fsdev, "writeout", writeout, &error_abort);
3557 #else
3558 error_report("writeout=immediate not supported "
3559 "on this platform");
3560 exit(1);
3561 #endif
3563 qemu_opt_set(fsdev, "fsdriver",
3564 qemu_opt_get(opts, "fsdriver"), &error_abort);
3565 qemu_opt_set(fsdev, "path", qemu_opt_get(opts, "path"),
3566 &error_abort);
3567 qemu_opt_set(fsdev, "security_model",
3568 qemu_opt_get(opts, "security_model"),
3569 &error_abort);
3570 socket = qemu_opt_get(opts, "socket");
3571 if (socket) {
3572 qemu_opt_set(fsdev, "socket", socket, &error_abort);
3574 sock_fd = qemu_opt_get(opts, "sock_fd");
3575 if (sock_fd) {
3576 qemu_opt_set(fsdev, "sock_fd", sock_fd, &error_abort);
3579 qemu_opt_set_bool(fsdev, "readonly",
3580 qemu_opt_get_bool(opts, "readonly", 0),
3581 &error_abort);
3582 device = qemu_opts_create(qemu_find_opts("device"), NULL, 0,
3583 &error_abort);
3584 qemu_opt_set(device, "driver", "virtio-9p-pci", &error_abort);
3585 qemu_opt_set(device, "fsdev",
3586 qemu_opts_id(fsdev), &error_abort);
3587 qemu_opt_set(device, "mount_tag",
3588 qemu_opt_get(opts, "mount_tag"), &error_abort);
3589 break;
3591 case QEMU_OPTION_virtfs_synth: {
3592 QemuOpts *fsdev;
3593 QemuOpts *device;
3595 fsdev = qemu_opts_create(qemu_find_opts("fsdev"), "v_synth",
3596 1, NULL);
3597 if (!fsdev) {
3598 error_report("duplicate option: %s", "virtfs_synth");
3599 exit(1);
3601 qemu_opt_set(fsdev, "fsdriver", "synth", &error_abort);
3603 device = qemu_opts_create(qemu_find_opts("device"), NULL, 0,
3604 &error_abort);
3605 qemu_opt_set(device, "driver", "virtio-9p-pci", &error_abort);
3606 qemu_opt_set(device, "fsdev", "v_synth", &error_abort);
3607 qemu_opt_set(device, "mount_tag", "v_synth", &error_abort);
3608 break;
3610 case QEMU_OPTION_serial:
3611 add_device_config(DEV_SERIAL, optarg);
3612 default_serial = 0;
3613 if (strncmp(optarg, "mon:", 4) == 0) {
3614 default_monitor = 0;
3616 break;
3617 case QEMU_OPTION_watchdog:
3618 if (watchdog) {
3619 error_report("only one watchdog option may be given");
3620 return 1;
3622 watchdog = optarg;
3623 break;
3624 case QEMU_OPTION_watchdog_action:
3625 if (select_watchdog_action(optarg) == -1) {
3626 error_report("unknown -watchdog-action parameter");
3627 exit(1);
3629 break;
3630 case QEMU_OPTION_virtiocon:
3631 add_device_config(DEV_VIRTCON, optarg);
3632 default_virtcon = 0;
3633 if (strncmp(optarg, "mon:", 4) == 0) {
3634 default_monitor = 0;
3636 break;
3637 case QEMU_OPTION_parallel:
3638 add_device_config(DEV_PARALLEL, optarg);
3639 default_parallel = 0;
3640 if (strncmp(optarg, "mon:", 4) == 0) {
3641 default_monitor = 0;
3643 break;
3644 case QEMU_OPTION_debugcon:
3645 add_device_config(DEV_DEBUGCON, optarg);
3646 break;
3647 case QEMU_OPTION_loadvm:
3648 loadvm = optarg;
3649 break;
3650 case QEMU_OPTION_full_screen:
3651 full_screen = 1;
3652 break;
3653 case QEMU_OPTION_no_frame:
3654 no_frame = 1;
3655 break;
3656 case QEMU_OPTION_alt_grab:
3657 alt_grab = 1;
3658 break;
3659 case QEMU_OPTION_ctrl_grab:
3660 ctrl_grab = 1;
3661 break;
3662 case QEMU_OPTION_no_quit:
3663 no_quit = 1;
3664 break;
3665 case QEMU_OPTION_sdl:
3666 #ifdef CONFIG_SDL
3667 display_type = DT_SDL;
3668 break;
3669 #else
3670 error_report("SDL support is disabled");
3671 exit(1);
3672 #endif
3673 case QEMU_OPTION_pidfile:
3674 pid_file = optarg;
3675 break;
3676 case QEMU_OPTION_win2k_hack:
3677 win2k_install_hack = 1;
3678 break;
3679 case QEMU_OPTION_rtc_td_hack: {
3680 static GlobalProperty slew_lost_ticks = {
3681 .driver = "mc146818rtc",
3682 .property = "lost_tick_policy",
3683 .value = "slew",
3686 qdev_prop_register_global(&slew_lost_ticks);
3687 break;
3689 case QEMU_OPTION_acpitable:
3690 opts = qemu_opts_parse_noisily(qemu_find_opts("acpi"),
3691 optarg, true);
3692 if (!opts) {
3693 exit(1);
3695 acpi_table_add(opts, &error_fatal);
3696 break;
3697 case QEMU_OPTION_smbios:
3698 opts = qemu_opts_parse_noisily(qemu_find_opts("smbios"),
3699 optarg, false);
3700 if (!opts) {
3701 exit(1);
3703 smbios_entry_add(opts, &error_fatal);
3704 break;
3705 case QEMU_OPTION_fwcfg:
3706 opts = qemu_opts_parse_noisily(qemu_find_opts("fw_cfg"),
3707 optarg, true);
3708 if (opts == NULL) {
3709 exit(1);
3711 break;
3712 case QEMU_OPTION_enable_kvm:
3713 olist = qemu_find_opts("machine");
3714 qemu_opts_parse_noisily(olist, "accel=kvm", false);
3715 break;
3716 case QEMU_OPTION_enable_hax:
3717 olist = qemu_find_opts("machine");
3718 qemu_opts_parse_noisily(olist, "accel=hax", false);
3719 break;
3720 case QEMU_OPTION_M:
3721 case QEMU_OPTION_machine:
3722 olist = qemu_find_opts("machine");
3723 opts = qemu_opts_parse_noisily(olist, optarg, true);
3724 if (!opts) {
3725 exit(1);
3727 break;
3728 case QEMU_OPTION_no_kvm:
3729 olist = qemu_find_opts("machine");
3730 qemu_opts_parse_noisily(olist, "accel=tcg", false);
3731 break;
3732 case QEMU_OPTION_no_kvm_pit: {
3733 error_report("warning: ignoring deprecated option");
3734 break;
3736 case QEMU_OPTION_no_kvm_pit_reinjection: {
3737 static GlobalProperty kvm_pit_lost_tick_policy = {
3738 .driver = "kvm-pit",
3739 .property = "lost_tick_policy",
3740 .value = "discard",
3743 error_report("warning: deprecated, replaced by "
3744 "-global kvm-pit.lost_tick_policy=discard");
3745 qdev_prop_register_global(&kvm_pit_lost_tick_policy);
3746 break;
3748 case QEMU_OPTION_accel: {
3749 QemuOpts *accel_opts;
3751 accel_opts = qemu_opts_parse_noisily(qemu_find_opts("accel"),
3752 optarg, true);
3753 optarg = qemu_opt_get(accel_opts, "accel");
3754 if (!optarg || is_help_option(optarg)) {
3755 error_printf("Possible accelerators: kvm, xen, hax, tcg\n");
3756 exit(1);
3758 accel_opts = qemu_opts_create(qemu_find_opts("machine"), NULL,
3759 false, &error_abort);
3760 qemu_opt_set(accel_opts, "accel", optarg, &error_abort);
3761 break;
3763 case QEMU_OPTION_usb:
3764 olist = qemu_find_opts("machine");
3765 qemu_opts_parse_noisily(olist, "usb=on", false);
3766 break;
3767 case QEMU_OPTION_usbdevice:
3768 error_report("'-usbdevice' is deprecated, please use "
3769 "'-device usb-...' instead");
3770 olist = qemu_find_opts("machine");
3771 qemu_opts_parse_noisily(olist, "usb=on", false);
3772 add_device_config(DEV_USB, optarg);
3773 break;
3774 case QEMU_OPTION_device:
3775 if (!qemu_opts_parse_noisily(qemu_find_opts("device"),
3776 optarg, true)) {
3777 exit(1);
3779 break;
3780 case QEMU_OPTION_smp:
3781 if (!qemu_opts_parse_noisily(qemu_find_opts("smp-opts"),
3782 optarg, true)) {
3783 exit(1);
3785 break;
3786 case QEMU_OPTION_vnc:
3787 vnc_parse(optarg, &error_fatal);
3788 break;
3789 case QEMU_OPTION_no_acpi:
3790 acpi_enabled = 0;
3791 break;
3792 case QEMU_OPTION_no_hpet:
3793 no_hpet = 1;
3794 break;
3795 case QEMU_OPTION_balloon:
3796 if (balloon_parse(optarg) < 0) {
3797 error_report("unknown -balloon argument %s", optarg);
3798 exit(1);
3800 break;
3801 case QEMU_OPTION_no_reboot:
3802 no_reboot = 1;
3803 break;
3804 case QEMU_OPTION_no_shutdown:
3805 no_shutdown = 1;
3806 break;
3807 case QEMU_OPTION_show_cursor:
3808 cursor_hide = 0;
3809 break;
3810 case QEMU_OPTION_uuid:
3811 if (qemu_uuid_parse(optarg, &qemu_uuid) < 0) {
3812 error_report("failed to parse UUID string: wrong format");
3813 exit(1);
3815 qemu_uuid_set = true;
3816 break;
3817 case QEMU_OPTION_option_rom:
3818 if (nb_option_roms >= MAX_OPTION_ROMS) {
3819 error_report("too many option ROMs");
3820 exit(1);
3822 opts = qemu_opts_parse_noisily(qemu_find_opts("option-rom"),
3823 optarg, true);
3824 if (!opts) {
3825 exit(1);
3827 option_rom[nb_option_roms].name = qemu_opt_get(opts, "romfile");
3828 option_rom[nb_option_roms].bootindex =
3829 qemu_opt_get_number(opts, "bootindex", -1);
3830 if (!option_rom[nb_option_roms].name) {
3831 error_report("Option ROM file is not specified");
3832 exit(1);
3834 nb_option_roms++;
3835 break;
3836 case QEMU_OPTION_semihosting:
3837 semihosting.enabled = true;
3838 semihosting.target = SEMIHOSTING_TARGET_AUTO;
3839 break;
3840 case QEMU_OPTION_semihosting_config:
3841 semihosting.enabled = true;
3842 opts = qemu_opts_parse_noisily(qemu_find_opts("semihosting-config"),
3843 optarg, false);
3844 if (opts != NULL) {
3845 semihosting.enabled = qemu_opt_get_bool(opts, "enable",
3846 true);
3847 const char *target = qemu_opt_get(opts, "target");
3848 if (target != NULL) {
3849 if (strcmp("native", target) == 0) {
3850 semihosting.target = SEMIHOSTING_TARGET_NATIVE;
3851 } else if (strcmp("gdb", target) == 0) {
3852 semihosting.target = SEMIHOSTING_TARGET_GDB;
3853 } else if (strcmp("auto", target) == 0) {
3854 semihosting.target = SEMIHOSTING_TARGET_AUTO;
3855 } else {
3856 error_report("unsupported semihosting-config %s",
3857 optarg);
3858 exit(1);
3860 } else {
3861 semihosting.target = SEMIHOSTING_TARGET_AUTO;
3863 /* Set semihosting argument count and vector */
3864 qemu_opt_foreach(opts, add_semihosting_arg,
3865 &semihosting, NULL);
3866 } else {
3867 error_report("unsupported semihosting-config %s", optarg);
3868 exit(1);
3870 break;
3871 case QEMU_OPTION_tdf:
3872 error_report("warning: ignoring deprecated option");
3873 break;
3874 case QEMU_OPTION_name:
3875 opts = qemu_opts_parse_noisily(qemu_find_opts("name"),
3876 optarg, true);
3877 if (!opts) {
3878 exit(1);
3880 break;
3881 case QEMU_OPTION_prom_env:
3882 if (nb_prom_envs >= MAX_PROM_ENVS) {
3883 error_report("too many prom variables");
3884 exit(1);
3886 prom_envs[nb_prom_envs] = optarg;
3887 nb_prom_envs++;
3888 break;
3889 case QEMU_OPTION_old_param:
3890 old_param = 1;
3891 break;
3892 case QEMU_OPTION_clock:
3893 /* Clock options no longer exist. Keep this option for
3894 * backward compatibility.
3896 break;
3897 case QEMU_OPTION_startdate:
3898 configure_rtc_date_offset(optarg, 1);
3899 break;
3900 case QEMU_OPTION_rtc:
3901 opts = qemu_opts_parse_noisily(qemu_find_opts("rtc"), optarg,
3902 false);
3903 if (!opts) {
3904 exit(1);
3906 configure_rtc(opts);
3907 break;
3908 case QEMU_OPTION_tb_size:
3909 tcg_tb_size = strtol(optarg, NULL, 0);
3910 if (tcg_tb_size < 0) {
3911 tcg_tb_size = 0;
3913 break;
3914 case QEMU_OPTION_icount:
3915 icount_opts = qemu_opts_parse_noisily(qemu_find_opts("icount"),
3916 optarg, true);
3917 if (!icount_opts) {
3918 exit(1);
3920 break;
3921 case QEMU_OPTION_incoming:
3922 if (!incoming) {
3923 runstate_set(RUN_STATE_INMIGRATE);
3925 incoming = optarg;
3926 break;
3927 case QEMU_OPTION_only_migratable:
3928 only_migratable = 1;
3929 break;
3930 case QEMU_OPTION_nodefaults:
3931 has_defaults = 0;
3932 break;
3933 case QEMU_OPTION_xen_domid:
3934 if (!(xen_available())) {
3935 error_report("Option not supported for this target");
3936 exit(1);
3938 xen_domid = atoi(optarg);
3939 break;
3940 case QEMU_OPTION_xen_create:
3941 if (!(xen_available())) {
3942 error_report("Option not supported for this target");
3943 exit(1);
3945 xen_mode = XEN_CREATE;
3946 break;
3947 case QEMU_OPTION_xen_attach:
3948 if (!(xen_available())) {
3949 error_report("Option not supported for this target");
3950 exit(1);
3952 xen_mode = XEN_ATTACH;
3953 break;
3954 case QEMU_OPTION_xen_domid_restrict:
3955 if (!(xen_available())) {
3956 error_report("Option not supported for this target");
3957 exit(1);
3959 xen_domid_restrict = true;
3960 break;
3961 case QEMU_OPTION_trace:
3962 g_free(trace_file);
3963 trace_file = trace_opt_parse(optarg);
3964 break;
3965 case QEMU_OPTION_readconfig:
3967 int ret = qemu_read_config_file(optarg);
3968 if (ret < 0) {
3969 error_report("read config %s: %s", optarg,
3970 strerror(-ret));
3971 exit(1);
3973 break;
3975 case QEMU_OPTION_spice:
3976 olist = qemu_find_opts("spice");
3977 if (!olist) {
3978 error_report("spice support is disabled");
3979 exit(1);
3981 opts = qemu_opts_parse_noisily(olist, optarg, false);
3982 if (!opts) {
3983 exit(1);
3985 display_remote++;
3986 break;
3987 case QEMU_OPTION_writeconfig:
3989 FILE *fp;
3990 if (strcmp(optarg, "-") == 0) {
3991 fp = stdout;
3992 } else {
3993 fp = fopen(optarg, "w");
3994 if (fp == NULL) {
3995 error_report("open %s: %s", optarg,
3996 strerror(errno));
3997 exit(1);
4000 qemu_config_write(fp);
4001 if (fp != stdout) {
4002 fclose(fp);
4004 break;
4006 case QEMU_OPTION_qtest:
4007 qtest_chrdev = optarg;
4008 break;
4009 case QEMU_OPTION_qtest_log:
4010 qtest_log = optarg;
4011 break;
4012 case QEMU_OPTION_sandbox:
4013 opts = qemu_opts_parse_noisily(qemu_find_opts("sandbox"),
4014 optarg, true);
4015 if (!opts) {
4016 exit(1);
4018 break;
4019 case QEMU_OPTION_add_fd:
4020 #ifndef _WIN32
4021 opts = qemu_opts_parse_noisily(qemu_find_opts("add-fd"),
4022 optarg, false);
4023 if (!opts) {
4024 exit(1);
4026 #else
4027 error_report("File descriptor passing is disabled on this "
4028 "platform");
4029 exit(1);
4030 #endif
4031 break;
4032 case QEMU_OPTION_object:
4033 opts = qemu_opts_parse_noisily(qemu_find_opts("object"),
4034 optarg, true);
4035 if (!opts) {
4036 exit(1);
4038 break;
4039 case QEMU_OPTION_realtime:
4040 opts = qemu_opts_parse_noisily(qemu_find_opts("realtime"),
4041 optarg, false);
4042 if (!opts) {
4043 exit(1);
4045 enable_mlock = qemu_opt_get_bool(opts, "mlock", true);
4046 break;
4047 case QEMU_OPTION_msg:
4048 opts = qemu_opts_parse_noisily(qemu_find_opts("msg"), optarg,
4049 false);
4050 if (!opts) {
4051 exit(1);
4053 configure_msg(opts);
4054 break;
4055 case QEMU_OPTION_dump_vmstate:
4056 if (vmstate_dump_file) {
4057 error_report("only one '-dump-vmstate' "
4058 "option may be given");
4059 exit(1);
4061 vmstate_dump_file = fopen(optarg, "w");
4062 if (vmstate_dump_file == NULL) {
4063 error_report("open %s: %s", optarg, strerror(errno));
4064 exit(1);
4066 break;
4067 default:
4068 os_parse_cmd_args(popt->index, optarg);
4073 * Clear error location left behind by the loop.
4074 * Best done right after the loop. Do not insert code here!
4076 loc_set_none();
4078 replay_configure(icount_opts);
4080 machine_class = select_machine();
4082 set_memory_options(&ram_slots, &maxram_size, machine_class);
4084 os_daemonize();
4086 if (pid_file && qemu_create_pidfile(pid_file) != 0) {
4087 error_report("could not acquire pid file: %s", strerror(errno));
4088 exit(1);
4091 if (qemu_init_main_loop(&main_loop_err)) {
4092 error_report_err(main_loop_err);
4093 exit(1);
4096 if (qemu_opts_foreach(qemu_find_opts("sandbox"),
4097 parse_sandbox, NULL, NULL)) {
4098 exit(1);
4101 if (qemu_opts_foreach(qemu_find_opts("name"),
4102 parse_name, NULL, NULL)) {
4103 exit(1);
4106 #ifndef _WIN32
4107 if (qemu_opts_foreach(qemu_find_opts("add-fd"),
4108 parse_add_fd, NULL, NULL)) {
4109 exit(1);
4112 if (qemu_opts_foreach(qemu_find_opts("add-fd"),
4113 cleanup_add_fd, NULL, NULL)) {
4114 exit(1);
4116 #endif
4118 current_machine = MACHINE(object_new(object_class_get_name(
4119 OBJECT_CLASS(machine_class))));
4120 if (machine_help_func(qemu_get_machine_opts(), current_machine)) {
4121 exit(0);
4123 object_property_add_child(object_get_root(), "machine",
4124 OBJECT(current_machine), &error_abort);
4126 if (machine_class->minimum_page_bits) {
4127 if (!set_preferred_target_page_bits(machine_class->minimum_page_bits)) {
4128 /* This would be a board error: specifying a minimum smaller than
4129 * a target's compile-time fixed setting.
4131 g_assert_not_reached();
4135 cpu_exec_init_all();
4137 if (machine_class->hw_version) {
4138 qemu_set_hw_version(machine_class->hw_version);
4141 if (cpu_model && is_help_option(cpu_model)) {
4142 list_cpus(stdout, &fprintf, cpu_model);
4143 exit(0);
4146 if (!trace_init_backends()) {
4147 exit(1);
4149 trace_init_file(trace_file);
4151 /* Open the logfile at this point and set the log mask if necessary.
4153 if (log_file) {
4154 qemu_set_log_filename(log_file, &error_fatal);
4157 if (log_mask) {
4158 int mask;
4159 mask = qemu_str_to_log_mask(log_mask);
4160 if (!mask) {
4161 qemu_print_log_usage(stdout);
4162 exit(1);
4164 qemu_set_log(mask);
4165 } else {
4166 qemu_set_log(0);
4169 /* If no data_dir is specified then try to find it relative to the
4170 executable path. */
4171 if (data_dir_idx < ARRAY_SIZE(data_dir)) {
4172 data_dir[data_dir_idx] = os_find_datadir();
4173 if (data_dir[data_dir_idx] != NULL) {
4174 data_dir_idx++;
4177 /* If all else fails use the install path specified when building. */
4178 if (data_dir_idx < ARRAY_SIZE(data_dir)) {
4179 data_dir[data_dir_idx++] = CONFIG_QEMU_DATADIR;
4182 /* -L help lists the data directories and exits. */
4183 if (list_data_dirs) {
4184 for (i = 0; i < data_dir_idx; i++) {
4185 printf("%s\n", data_dir[i]);
4187 exit(0);
4190 smp_parse(qemu_opts_find(qemu_find_opts("smp-opts"), NULL));
4192 machine_class->max_cpus = machine_class->max_cpus ?: 1; /* Default to UP */
4193 if (max_cpus > machine_class->max_cpus) {
4194 error_report("Number of SMP CPUs requested (%d) exceeds max CPUs "
4195 "supported by machine '%s' (%d)", max_cpus,
4196 machine_class->name, machine_class->max_cpus);
4197 exit(1);
4201 * Get the default machine options from the machine if it is not already
4202 * specified either by the configuration file or by the command line.
4204 if (machine_class->default_machine_opts) {
4205 qemu_opts_set_defaults(qemu_find_opts("machine"),
4206 machine_class->default_machine_opts, 0);
4209 qemu_opts_foreach(qemu_find_opts("device"),
4210 default_driver_check, NULL, NULL);
4211 qemu_opts_foreach(qemu_find_opts("global"),
4212 default_driver_check, NULL, NULL);
4214 if (!vga_model && !default_vga) {
4215 vga_interface_type = VGA_DEVICE;
4217 if (!has_defaults || machine_class->no_serial) {
4218 default_serial = 0;
4220 if (!has_defaults || machine_class->no_parallel) {
4221 default_parallel = 0;
4223 if (!has_defaults || !machine_class->use_virtcon) {
4224 default_virtcon = 0;
4226 if (!has_defaults || !machine_class->use_sclp) {
4227 default_sclp = 0;
4229 if (!has_defaults || machine_class->no_floppy) {
4230 default_floppy = 0;
4232 if (!has_defaults || machine_class->no_cdrom) {
4233 default_cdrom = 0;
4235 if (!has_defaults || machine_class->no_sdcard) {
4236 default_sdcard = 0;
4238 if (!has_defaults) {
4239 default_monitor = 0;
4240 default_net = 0;
4241 default_vga = 0;
4244 if (is_daemonized()) {
4245 /* According to documentation and historically, -nographic redirects
4246 * serial port, parallel port and monitor to stdio, which does not work
4247 * with -daemonize. We can redirect these to null instead, but since
4248 * -nographic is legacy, let's just error out.
4249 * We disallow -nographic only if all other ports are not redirected
4250 * explicitly, to not break existing legacy setups which uses
4251 * -nographic _and_ redirects all ports explicitly - this is valid
4252 * usage, -nographic is just a no-op in this case.
4254 if (nographic
4255 && (default_parallel || default_serial
4256 || default_monitor || default_virtcon)) {
4257 error_report("-nographic cannot be used with -daemonize");
4258 exit(1);
4260 #ifdef CONFIG_CURSES
4261 if (display_type == DT_CURSES) {
4262 error_report("curses display cannot be used with -daemonize");
4263 exit(1);
4265 #endif
4268 if (nographic) {
4269 if (default_parallel)
4270 add_device_config(DEV_PARALLEL, "null");
4271 if (default_serial && default_monitor) {
4272 add_device_config(DEV_SERIAL, "mon:stdio");
4273 } else if (default_virtcon && default_monitor) {
4274 add_device_config(DEV_VIRTCON, "mon:stdio");
4275 } else if (default_sclp && default_monitor) {
4276 add_device_config(DEV_SCLP, "mon:stdio");
4277 } else {
4278 if (default_serial)
4279 add_device_config(DEV_SERIAL, "stdio");
4280 if (default_virtcon)
4281 add_device_config(DEV_VIRTCON, "stdio");
4282 if (default_sclp) {
4283 add_device_config(DEV_SCLP, "stdio");
4285 if (default_monitor)
4286 monitor_parse("stdio", "readline", false);
4288 } else {
4289 if (default_serial)
4290 add_device_config(DEV_SERIAL, "vc:80Cx24C");
4291 if (default_parallel)
4292 add_device_config(DEV_PARALLEL, "vc:80Cx24C");
4293 if (default_monitor)
4294 monitor_parse("vc:80Cx24C", "readline", false);
4295 if (default_virtcon)
4296 add_device_config(DEV_VIRTCON, "vc:80Cx24C");
4297 if (default_sclp) {
4298 add_device_config(DEV_SCLP, "vc:80Cx24C");
4302 #if defined(CONFIG_VNC)
4303 if (!QTAILQ_EMPTY(&(qemu_find_opts("vnc")->head))) {
4304 display_remote++;
4306 #endif
4307 if (display_type == DT_DEFAULT && !display_remote) {
4308 #if defined(CONFIG_GTK)
4309 display_type = DT_GTK;
4310 #elif defined(CONFIG_SDL)
4311 display_type = DT_SDL;
4312 #elif defined(CONFIG_COCOA)
4313 display_type = DT_COCOA;
4314 #elif defined(CONFIG_VNC)
4315 vnc_parse("localhost:0,to=99,id=default", &error_abort);
4316 #else
4317 display_type = DT_NONE;
4318 #endif
4321 if ((no_frame || alt_grab || ctrl_grab) && display_type != DT_SDL) {
4322 error_report("-no-frame, -alt-grab and -ctrl-grab are only valid "
4323 "for SDL, ignoring option");
4325 if (no_quit && (display_type != DT_GTK && display_type != DT_SDL)) {
4326 error_report("-no-quit is only valid for GTK and SDL, "
4327 "ignoring option");
4330 if (display_type == DT_GTK) {
4331 early_gtk_display_init(request_opengl);
4334 if (display_type == DT_SDL) {
4335 sdl_display_early_init(request_opengl);
4338 qemu_console_early_init();
4340 if (request_opengl == 1 && display_opengl == 0) {
4341 #if defined(CONFIG_OPENGL)
4342 error_report("OpenGL is not supported by the display");
4343 #else
4344 error_report("OpenGL support is disabled");
4345 #endif
4346 exit(1);
4349 page_size_init();
4350 socket_init();
4352 if (qemu_opts_foreach(qemu_find_opts("object"),
4353 user_creatable_add_opts_foreach,
4354 object_create_initial, NULL)) {
4355 exit(1);
4358 if (qemu_opts_foreach(qemu_find_opts("chardev"),
4359 chardev_init_func, NULL, NULL)) {
4360 exit(1);
4363 #ifdef CONFIG_VIRTFS
4364 if (qemu_opts_foreach(qemu_find_opts("fsdev"),
4365 fsdev_init_func, NULL, NULL)) {
4366 exit(1);
4368 #endif
4370 if (qemu_opts_foreach(qemu_find_opts("device"),
4371 device_help_func, NULL, NULL)) {
4372 exit(0);
4375 machine_opts = qemu_get_machine_opts();
4376 if (qemu_opt_foreach(machine_opts, machine_set_property, current_machine,
4377 NULL)) {
4378 object_unref(OBJECT(current_machine));
4379 exit(1);
4382 configure_accelerator(current_machine);
4384 if (qtest_chrdev) {
4385 qtest_init(qtest_chrdev, qtest_log, &error_fatal);
4388 machine_opts = qemu_get_machine_opts();
4389 kernel_filename = qemu_opt_get(machine_opts, "kernel");
4390 initrd_filename = qemu_opt_get(machine_opts, "initrd");
4391 kernel_cmdline = qemu_opt_get(machine_opts, "append");
4392 bios_name = qemu_opt_get(machine_opts, "firmware");
4394 opts = qemu_opts_find(qemu_find_opts("boot-opts"), NULL);
4395 if (opts) {
4396 boot_order = qemu_opt_get(opts, "order");
4397 if (boot_order) {
4398 validate_bootdevices(boot_order, &error_fatal);
4401 boot_once = qemu_opt_get(opts, "once");
4402 if (boot_once) {
4403 validate_bootdevices(boot_once, &error_fatal);
4406 boot_menu = qemu_opt_get_bool(opts, "menu", boot_menu);
4407 boot_strict = qemu_opt_get_bool(opts, "strict", false);
4410 if (!boot_order) {
4411 boot_order = machine_class->default_boot_order;
4414 if (!kernel_cmdline) {
4415 kernel_cmdline = "";
4416 current_machine->kernel_cmdline = (char *)kernel_cmdline;
4419 linux_boot = (kernel_filename != NULL);
4421 if (!linux_boot && *kernel_cmdline != '\0') {
4422 error_report("-append only allowed with -kernel option");
4423 exit(1);
4426 if (!linux_boot && initrd_filename != NULL) {
4427 error_report("-initrd only allowed with -kernel option");
4428 exit(1);
4431 if (semihosting_enabled() && !semihosting_get_argc() && kernel_filename) {
4432 /* fall back to the -kernel/-append */
4433 semihosting_arg_fallback(kernel_filename, kernel_cmdline);
4436 os_set_line_buffering();
4438 /* spice needs the timers to be initialized by this point */
4439 qemu_spice_init();
4441 cpu_ticks_init();
4442 if (icount_opts) {
4443 if (!tcg_enabled()) {
4444 error_report("-icount is not allowed with hardware virtualization");
4445 exit(1);
4447 configure_icount(icount_opts, &error_abort);
4448 qemu_opts_del(icount_opts);
4451 qemu_tcg_configure(accel_opts, &error_fatal);
4453 if (default_net) {
4454 QemuOptsList *net = qemu_find_opts("net");
4455 qemu_opts_set(net, NULL, "type", "nic", &error_abort);
4456 #ifdef CONFIG_SLIRP
4457 qemu_opts_set(net, NULL, "type", "user", &error_abort);
4458 #endif
4461 colo_info_init();
4463 if (net_init_clients() < 0) {
4464 exit(1);
4467 if (qemu_opts_foreach(qemu_find_opts("object"),
4468 user_creatable_add_opts_foreach,
4469 object_create_delayed, NULL)) {
4470 exit(1);
4473 #ifdef CONFIG_TPM
4474 if (tpm_init() < 0) {
4475 exit(1);
4477 #endif
4479 /* init the bluetooth world */
4480 if (foreach_device_config(DEV_BT, bt_parse))
4481 exit(1);
4483 if (!xen_enabled()) {
4484 /* On 32-bit hosts, QEMU is limited by virtual address space */
4485 if (ram_size > (2047 << 20) && HOST_LONG_BITS == 32) {
4486 error_report("at most 2047 MB RAM can be simulated");
4487 exit(1);
4491 blk_mig_init();
4492 ram_mig_init();
4494 /* If the currently selected machine wishes to override the units-per-bus
4495 * property of its default HBA interface type, do so now. */
4496 if (machine_class->units_per_default_bus) {
4497 override_max_devs(machine_class->block_default_type,
4498 machine_class->units_per_default_bus);
4501 /* open the virtual block devices */
4502 while (!QSIMPLEQ_EMPTY(&bdo_queue)) {
4503 BlockdevOptions_queue *bdo = QSIMPLEQ_FIRST(&bdo_queue);
4505 QSIMPLEQ_REMOVE_HEAD(&bdo_queue, entry);
4506 loc_push_restore(&bdo->loc);
4507 qmp_blockdev_add(bdo->bdo, &error_fatal);
4508 loc_pop(&bdo->loc);
4509 qapi_free_BlockdevOptions(bdo->bdo);
4510 g_free(bdo);
4512 if (snapshot || replay_mode != REPLAY_MODE_NONE) {
4513 qemu_opts_foreach(qemu_find_opts("drive"), drive_enable_snapshot,
4514 NULL, NULL);
4516 if (qemu_opts_foreach(qemu_find_opts("drive"), drive_init_func,
4517 &machine_class->block_default_type, NULL)) {
4518 exit(1);
4521 default_drive(default_cdrom, snapshot, machine_class->block_default_type, 2,
4522 CDROM_OPTS);
4523 default_drive(default_floppy, snapshot, IF_FLOPPY, 0, FD_OPTS);
4524 default_drive(default_sdcard, snapshot, IF_SD, 0, SD_OPTS);
4526 parse_numa_opts(current_machine);
4528 if (qemu_opts_foreach(qemu_find_opts("mon"),
4529 mon_init_func, NULL, NULL)) {
4530 exit(1);
4533 if (foreach_device_config(DEV_SERIAL, serial_parse) < 0)
4534 exit(1);
4535 if (foreach_device_config(DEV_PARALLEL, parallel_parse) < 0)
4536 exit(1);
4537 if (foreach_device_config(DEV_VIRTCON, virtcon_parse) < 0)
4538 exit(1);
4539 if (foreach_device_config(DEV_SCLP, sclp_parse) < 0) {
4540 exit(1);
4542 if (foreach_device_config(DEV_DEBUGCON, debugcon_parse) < 0)
4543 exit(1);
4545 /* If no default VGA is requested, the default is "none". */
4546 if (default_vga) {
4547 if (machine_class->default_display) {
4548 vga_model = machine_class->default_display;
4549 } else if (vga_interface_available(VGA_CIRRUS)) {
4550 vga_model = "cirrus";
4551 } else if (vga_interface_available(VGA_STD)) {
4552 vga_model = "std";
4555 if (vga_model) {
4556 select_vgahw(vga_model);
4559 if (watchdog) {
4560 i = select_watchdog(watchdog);
4561 if (i > 0)
4562 exit (i == 1 ? 1 : 0);
4565 machine_register_compat_props(current_machine);
4567 qemu_opts_foreach(qemu_find_opts("global"),
4568 global_init_func, NULL, NULL);
4570 /* This checkpoint is required by replay to separate prior clock
4571 reading from the other reads, because timer polling functions query
4572 clock values from the log. */
4573 replay_checkpoint(CHECKPOINT_INIT);
4574 qdev_machine_init();
4576 current_machine->ram_size = ram_size;
4577 current_machine->maxram_size = maxram_size;
4578 current_machine->ram_slots = ram_slots;
4579 current_machine->boot_order = boot_order;
4580 current_machine->cpu_model = cpu_model;
4582 machine_run_board_init(current_machine);
4584 realtime_init();
4586 soundhw_init();
4588 if (hax_enabled()) {
4589 hax_sync_vcpus();
4592 if (qemu_opts_foreach(qemu_find_opts("fw_cfg"),
4593 parse_fw_cfg, fw_cfg_find(), NULL) != 0) {
4594 exit(1);
4597 /* init USB devices */
4598 if (machine_usb(current_machine)) {
4599 if (foreach_device_config(DEV_USB, usb_parse) < 0)
4600 exit(1);
4603 /* Check if IGD GFX passthrough. */
4604 igd_gfx_passthru();
4606 /* init generic devices */
4607 rom_set_order_override(FW_CFG_ORDER_OVERRIDE_DEVICE);
4608 if (qemu_opts_foreach(qemu_find_opts("device"),
4609 device_init_func, NULL, NULL)) {
4610 exit(1);
4613 cpu_synchronize_all_post_init();
4615 rom_reset_order_override();
4618 * Create frontends for -drive if=scsi leftovers.
4619 * Normally, frontends for -drive get created by machine
4620 * initialization for onboard SCSI HBAs. However, we create a few
4621 * more ever since SCSI qdevification, but this is pretty much an
4622 * implementation accident, and deprecated.
4624 scsi_legacy_handle_cmdline();
4626 /* Did we create any drives that we failed to create a device for? */
4627 drive_check_orphaned();
4629 /* Don't warn about the default network setup that you get if
4630 * no command line -net or -netdev options are specified. There
4631 * are two cases that we would otherwise complain about:
4632 * (1) board doesn't support a NIC but the implicit "-net nic"
4633 * requested one
4634 * (2) CONFIG_SLIRP not set, in which case the implicit "-net nic"
4635 * sets up a nic that isn't connected to anything.
4637 if (!default_net) {
4638 net_check_clients();
4642 if (boot_once) {
4643 qemu_boot_set(boot_once, &error_fatal);
4644 qemu_register_reset(restore_boot_order, g_strdup(boot_order));
4647 ds = init_displaystate();
4649 /* init local displays */
4650 switch (display_type) {
4651 case DT_CURSES:
4652 curses_display_init(ds, full_screen);
4653 break;
4654 case DT_SDL:
4655 sdl_display_init(ds, full_screen, no_frame);
4656 break;
4657 case DT_COCOA:
4658 cocoa_display_init(ds, full_screen);
4659 break;
4660 case DT_GTK:
4661 gtk_display_init(ds, full_screen, grab_on_hover);
4662 break;
4663 default:
4664 break;
4667 /* must be after terminal init, SDL library changes signal handlers */
4668 os_setup_signal_handling();
4670 /* init remote displays */
4671 #ifdef CONFIG_VNC
4672 qemu_opts_foreach(qemu_find_opts("vnc"),
4673 vnc_init_func, NULL, NULL);
4674 #endif
4676 if (using_spice) {
4677 qemu_spice_display_init();
4680 #ifdef CONFIG_OPENGL_DMABUF
4681 if (display_type == DT_EGL) {
4682 egl_headless_init();
4684 #endif
4686 if (foreach_device_config(DEV_GDB, gdbserver_start) < 0) {
4687 exit(1);
4690 qdev_machine_creation_done();
4692 /* TODO: once all bus devices are qdevified, this should be done
4693 * when bus is created by qdev.c */
4694 qemu_register_reset(qbus_reset_all_fn, sysbus_get_default());
4695 qemu_run_machine_init_done_notifiers();
4697 if (rom_check_and_register_reset() != 0) {
4698 error_report("rom check and register reset failed");
4699 exit(1);
4702 replay_start();
4704 /* This checkpoint is required by replay to separate prior clock
4705 reading from the other reads, because timer polling functions query
4706 clock values from the log. */
4707 replay_checkpoint(CHECKPOINT_RESET);
4708 qemu_system_reset(VMRESET_SILENT);
4709 register_global_state();
4710 if (replay_mode != REPLAY_MODE_NONE) {
4711 replay_vmstate_init();
4712 } else if (loadvm) {
4713 Error *local_err = NULL;
4714 if (load_vmstate(loadvm, &local_err) < 0) {
4715 error_report_err(local_err);
4716 autostart = 0;
4720 qdev_prop_check_globals();
4721 if (vmstate_dump_file) {
4722 /* dump and exit */
4723 dump_vmstate_json_to_file(vmstate_dump_file);
4724 return 0;
4727 if (incoming) {
4728 Error *local_err = NULL;
4729 qemu_start_incoming_migration(incoming, &local_err);
4730 if (local_err) {
4731 error_reportf_err(local_err, "-incoming %s: ", incoming);
4732 exit(1);
4734 } else if (autostart) {
4735 vm_start();
4738 os_setup_post();
4740 main_loop();
4741 replay_disable_events();
4742 iothread_stop_all();
4744 bdrv_close_all();
4745 pause_all_vcpus();
4746 res_free();
4748 /* vhost-user must be cleaned up before chardevs. */
4749 net_cleanup();
4750 audio_cleanup();
4751 monitor_cleanup();
4752 qemu_chr_cleanup();
4753 /* TODO: unref root container, check all devices are ok */
4755 return 0;