util/mmap-alloc: check parameter before using
[qemu/ar7.git] / vl.c
blobabb0900fe4ea4ea22f19e97fcc8495b2574c6d77
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/bt.h"
66 #include "sysemu/watchdog.h"
67 #include "hw/smbios/smbios.h"
68 #include "hw/acpi/acpi.h"
69 #include "hw/xen/xen.h"
70 #include "hw/qdev.h"
71 #include "hw/loader.h"
72 #include "monitor/qdev.h"
73 #include "sysemu/bt.h"
74 #include "net/net.h"
75 #include "net/slirp.h"
76 #include "monitor/monitor.h"
77 #include "ui/console.h"
78 #include "ui/input.h"
79 #include "sysemu/sysemu.h"
80 #include "sysemu/numa.h"
81 #include "exec/gdbstub.h"
82 #include "qemu/timer.h"
83 #include "sysemu/char.h"
84 #include "qemu/bitmap.h"
85 #include "qemu/log.h"
86 #include "sysemu/blockdev.h"
87 #include "hw/block/block.h"
88 #include "migration/block.h"
89 #include "sysemu/tpm.h"
90 #include "sysemu/dma.h"
91 #include "audio/audio.h"
92 #include "migration/migration.h"
93 #include "sysemu/cpus.h"
94 #include "migration/colo.h"
95 #include "sysemu/kvm.h"
96 #include "sysemu/hax.h"
97 #include "qapi/qmp/qjson.h"
98 #include "qemu/option.h"
99 #include "qemu/config-file.h"
100 #include "qemu-options.h"
101 #include "qmp-commands.h"
102 #include "qemu/main-loop.h"
103 #ifdef CONFIG_VIRTFS
104 #include "fsdev/qemu-fsdev.h"
105 #endif
106 #include "sysemu/qtest.h"
108 #include "disas/disas.h"
111 #include "slirp/libslirp.h"
113 #include "trace.h"
114 #include "trace/control.h"
115 #include "qemu/queue.h"
116 #include "sysemu/arch_init.h"
118 #include "ui/qemu-spice.h"
119 #include "qapi/string-input-visitor.h"
120 #include "qapi/opts-visitor.h"
121 #include "qom/object_interfaces.h"
122 #include "qapi-event.h"
123 #include "exec/semihost.h"
124 #include "crypto/init.h"
125 #include "sysemu/replay.h"
126 #include "qapi/qmp/qerror.h"
127 #include "sysemu/iothread.h"
129 #define MAX_VIRTIO_CONSOLES 1
130 #define MAX_SCLP_CONSOLES 1
132 static const char *data_dir[16];
133 static int data_dir_idx;
134 const char *bios_name = NULL;
135 enum vga_retrace_method vga_retrace_method = VGA_RETRACE_DUMB;
136 int request_opengl = -1;
137 int display_opengl;
138 const char* keyboard_layout = NULL;
139 ram_addr_t ram_size;
140 const char *mem_path = NULL;
141 int mem_prealloc = 0; /* force preallocation of physical target memory */
142 bool enable_mlock = false;
143 int nb_nics;
144 NICInfo nd_table[MAX_NICS];
145 int autostart;
146 static int rtc_utc = 1;
147 static int rtc_date_offset = -1; /* -1 means no change */
148 QEMUClockType rtc_clock;
149 int vga_interface_type = VGA_NONE;
150 static int full_screen = 0;
151 static int no_frame = 0;
152 int no_quit = 0;
153 static bool grab_on_hover;
154 CharDriverState *serial_hds[MAX_SERIAL_PORTS];
155 CharDriverState *parallel_hds[MAX_PARALLEL_PORTS];
156 CharDriverState *virtcon_hds[MAX_VIRTIO_CONSOLES];
157 CharDriverState *sclp_hds[MAX_SCLP_CONSOLES];
158 int win2k_install_hack = 0;
159 int singlestep = 0;
160 int smp_cpus = 1;
161 int max_cpus = 1;
162 int smp_cores = 1;
163 int smp_threads = 1;
164 int acpi_enabled = 1;
165 int no_hpet = 0;
166 int fd_bootchk = 1;
167 static int no_reboot;
168 int no_shutdown = 0;
169 int cursor_hide = 1;
170 int graphic_rotate = 0;
171 const char *watchdog;
172 QEMUOptionRom option_rom[MAX_OPTION_ROMS];
173 int nb_option_roms;
174 int old_param = 0;
175 const char *qemu_name;
176 int alt_grab = 0;
177 int ctrl_grab = 0;
178 unsigned int nb_prom_envs = 0;
179 const char *prom_envs[MAX_PROM_ENVS];
180 int boot_menu;
181 bool boot_strict;
182 uint8_t *boot_splash_filedata;
183 size_t boot_splash_filedata_size;
184 uint8_t qemu_extra_params_fw[2];
186 int icount_align_option;
188 /* The bytes in qemu_uuid are in the order specified by RFC4122, _not_ in the
189 * little-endian "wire format" described in the SMBIOS 2.6 specification.
191 QemuUUID qemu_uuid;
192 bool qemu_uuid_set;
194 static NotifierList exit_notifiers =
195 NOTIFIER_LIST_INITIALIZER(exit_notifiers);
197 static NotifierList machine_init_done_notifiers =
198 NOTIFIER_LIST_INITIALIZER(machine_init_done_notifiers);
200 bool xen_allowed;
201 uint32_t xen_domid;
202 enum xen_mode xen_mode = XEN_EMULATE;
204 static int has_defaults = 1;
205 static int default_serial = 1;
206 static int default_parallel = 1;
207 static int default_virtcon = 1;
208 static int default_sclp = 1;
209 static int default_monitor = 1;
210 static int default_floppy = 1;
211 static int default_cdrom = 1;
212 static int default_sdcard = 1;
213 static int default_vga = 1;
214 static int default_net = 1;
216 static struct {
217 const char *driver;
218 int *flag;
219 } default_list[] = {
220 { .driver = "isa-serial", .flag = &default_serial },
221 { .driver = "isa-parallel", .flag = &default_parallel },
222 { .driver = "isa-fdc", .flag = &default_floppy },
223 { .driver = "floppy", .flag = &default_floppy },
224 { .driver = "ide-cd", .flag = &default_cdrom },
225 { .driver = "ide-hd", .flag = &default_cdrom },
226 { .driver = "ide-drive", .flag = &default_cdrom },
227 { .driver = "scsi-cd", .flag = &default_cdrom },
228 { .driver = "virtio-serial-pci", .flag = &default_virtcon },
229 { .driver = "virtio-serial", .flag = &default_virtcon },
230 { .driver = "VGA", .flag = &default_vga },
231 { .driver = "isa-vga", .flag = &default_vga },
232 { .driver = "cirrus-vga", .flag = &default_vga },
233 { .driver = "isa-cirrus-vga", .flag = &default_vga },
234 { .driver = "vmware-svga", .flag = &default_vga },
235 { .driver = "qxl-vga", .flag = &default_vga },
236 { .driver = "virtio-vga", .flag = &default_vga },
239 static QemuOptsList qemu_rtc_opts = {
240 .name = "rtc",
241 .head = QTAILQ_HEAD_INITIALIZER(qemu_rtc_opts.head),
242 .desc = {
244 .name = "base",
245 .type = QEMU_OPT_STRING,
247 .name = "clock",
248 .type = QEMU_OPT_STRING,
250 .name = "driftfix",
251 .type = QEMU_OPT_STRING,
253 { /* end of list */ }
257 static QemuOptsList qemu_sandbox_opts = {
258 .name = "sandbox",
259 .implied_opt_name = "enable",
260 .head = QTAILQ_HEAD_INITIALIZER(qemu_sandbox_opts.head),
261 .desc = {
263 .name = "enable",
264 .type = QEMU_OPT_BOOL,
266 { /* end of list */ }
270 static QemuOptsList qemu_option_rom_opts = {
271 .name = "option-rom",
272 .implied_opt_name = "romfile",
273 .head = QTAILQ_HEAD_INITIALIZER(qemu_option_rom_opts.head),
274 .desc = {
276 .name = "bootindex",
277 .type = QEMU_OPT_NUMBER,
278 }, {
279 .name = "romfile",
280 .type = QEMU_OPT_STRING,
282 { /* end of list */ }
286 static QemuOptsList qemu_machine_opts = {
287 .name = "machine",
288 .implied_opt_name = "type",
289 .merge_lists = true,
290 .head = QTAILQ_HEAD_INITIALIZER(qemu_machine_opts.head),
291 .desc = {
293 * no elements => accept any
294 * sanity checking will happen later
295 * when setting machine properties
301 static QemuOptsList qemu_boot_opts = {
302 .name = "boot-opts",
303 .implied_opt_name = "order",
304 .merge_lists = true,
305 .head = QTAILQ_HEAD_INITIALIZER(qemu_boot_opts.head),
306 .desc = {
308 .name = "order",
309 .type = QEMU_OPT_STRING,
310 }, {
311 .name = "once",
312 .type = QEMU_OPT_STRING,
313 }, {
314 .name = "menu",
315 .type = QEMU_OPT_BOOL,
316 }, {
317 .name = "splash",
318 .type = QEMU_OPT_STRING,
319 }, {
320 .name = "splash-time",
321 .type = QEMU_OPT_STRING,
322 }, {
323 .name = "reboot-timeout",
324 .type = QEMU_OPT_STRING,
325 }, {
326 .name = "strict",
327 .type = QEMU_OPT_BOOL,
329 { /*End of list */ }
333 static QemuOptsList qemu_add_fd_opts = {
334 .name = "add-fd",
335 .head = QTAILQ_HEAD_INITIALIZER(qemu_add_fd_opts.head),
336 .desc = {
338 .name = "fd",
339 .type = QEMU_OPT_NUMBER,
340 .help = "file descriptor of which a duplicate is added to fd set",
342 .name = "set",
343 .type = QEMU_OPT_NUMBER,
344 .help = "ID of the fd set to add fd to",
346 .name = "opaque",
347 .type = QEMU_OPT_STRING,
348 .help = "free-form string used to describe fd",
350 { /* end of list */ }
354 static QemuOptsList qemu_object_opts = {
355 .name = "object",
356 .implied_opt_name = "qom-type",
357 .head = QTAILQ_HEAD_INITIALIZER(qemu_object_opts.head),
358 .desc = {
363 static QemuOptsList qemu_tpmdev_opts = {
364 .name = "tpmdev",
365 .implied_opt_name = "type",
366 .head = QTAILQ_HEAD_INITIALIZER(qemu_tpmdev_opts.head),
367 .desc = {
368 /* options are defined in the TPM backends */
369 { /* end of list */ }
373 static QemuOptsList qemu_realtime_opts = {
374 .name = "realtime",
375 .head = QTAILQ_HEAD_INITIALIZER(qemu_realtime_opts.head),
376 .desc = {
378 .name = "mlock",
379 .type = QEMU_OPT_BOOL,
381 { /* end of list */ }
385 static QemuOptsList qemu_msg_opts = {
386 .name = "msg",
387 .head = QTAILQ_HEAD_INITIALIZER(qemu_msg_opts.head),
388 .desc = {
390 .name = "timestamp",
391 .type = QEMU_OPT_BOOL,
393 { /* end of list */ }
397 static QemuOptsList qemu_name_opts = {
398 .name = "name",
399 .implied_opt_name = "guest",
400 .merge_lists = true,
401 .head = QTAILQ_HEAD_INITIALIZER(qemu_name_opts.head),
402 .desc = {
404 .name = "guest",
405 .type = QEMU_OPT_STRING,
406 .help = "Sets the name of the guest.\n"
407 "This name will be displayed in the SDL window caption.\n"
408 "The name will also be used for the VNC server",
409 }, {
410 .name = "process",
411 .type = QEMU_OPT_STRING,
412 .help = "Sets the name of the QEMU process, as shown in top etc",
413 }, {
414 .name = "debug-threads",
415 .type = QEMU_OPT_BOOL,
416 .help = "When enabled, name the individual threads; defaults off.\n"
417 "NOTE: The thread names are for debugging and not a\n"
418 "stable API.",
420 { /* End of list */ }
424 static QemuOptsList qemu_mem_opts = {
425 .name = "memory",
426 .implied_opt_name = "size",
427 .head = QTAILQ_HEAD_INITIALIZER(qemu_mem_opts.head),
428 .merge_lists = true,
429 .desc = {
431 .name = "size",
432 .type = QEMU_OPT_SIZE,
435 .name = "slots",
436 .type = QEMU_OPT_NUMBER,
439 .name = "maxmem",
440 .type = QEMU_OPT_SIZE,
442 { /* end of list */ }
446 static QemuOptsList qemu_icount_opts = {
447 .name = "icount",
448 .implied_opt_name = "shift",
449 .merge_lists = true,
450 .head = QTAILQ_HEAD_INITIALIZER(qemu_icount_opts.head),
451 .desc = {
453 .name = "shift",
454 .type = QEMU_OPT_STRING,
455 }, {
456 .name = "align",
457 .type = QEMU_OPT_BOOL,
458 }, {
459 .name = "sleep",
460 .type = QEMU_OPT_BOOL,
461 }, {
462 .name = "rr",
463 .type = QEMU_OPT_STRING,
464 }, {
465 .name = "rrfile",
466 .type = QEMU_OPT_STRING,
468 { /* end of list */ }
472 static QemuOptsList qemu_semihosting_config_opts = {
473 .name = "semihosting-config",
474 .implied_opt_name = "enable",
475 .head = QTAILQ_HEAD_INITIALIZER(qemu_semihosting_config_opts.head),
476 .desc = {
478 .name = "enable",
479 .type = QEMU_OPT_BOOL,
480 }, {
481 .name = "target",
482 .type = QEMU_OPT_STRING,
483 }, {
484 .name = "arg",
485 .type = QEMU_OPT_STRING,
487 { /* end of list */ }
491 static QemuOptsList qemu_fw_cfg_opts = {
492 .name = "fw_cfg",
493 .implied_opt_name = "name",
494 .head = QTAILQ_HEAD_INITIALIZER(qemu_fw_cfg_opts.head),
495 .desc = {
497 .name = "name",
498 .type = QEMU_OPT_STRING,
499 .help = "Sets the fw_cfg name of the blob to be inserted",
500 }, {
501 .name = "file",
502 .type = QEMU_OPT_STRING,
503 .help = "Sets the name of the file from which\n"
504 "the fw_cfg blob will be loaded",
505 }, {
506 .name = "string",
507 .type = QEMU_OPT_STRING,
508 .help = "Sets content of the blob to be inserted from a string",
510 { /* end of list */ }
514 #ifdef CONFIG_LIBISCSI
515 static QemuOptsList qemu_iscsi_opts = {
516 .name = "iscsi",
517 .head = QTAILQ_HEAD_INITIALIZER(qemu_iscsi_opts.head),
518 .desc = {
520 .name = "user",
521 .type = QEMU_OPT_STRING,
522 .help = "username for CHAP authentication to target",
524 .name = "password",
525 .type = QEMU_OPT_STRING,
526 .help = "password for CHAP authentication to target",
528 .name = "password-secret",
529 .type = QEMU_OPT_STRING,
530 .help = "ID of the secret providing password for CHAP "
531 "authentication to target",
533 .name = "header-digest",
534 .type = QEMU_OPT_STRING,
535 .help = "HeaderDigest setting. "
536 "{CRC32C|CRC32C-NONE|NONE-CRC32C|NONE}",
538 .name = "initiator-name",
539 .type = QEMU_OPT_STRING,
540 .help = "Initiator iqn name to use when connecting",
542 .name = "timeout",
543 .type = QEMU_OPT_NUMBER,
544 .help = "Request timeout in seconds (default 0 = no timeout)",
546 { /* end of list */ }
549 #endif
552 * Get machine options
554 * Returns: machine options (never null).
556 QemuOpts *qemu_get_machine_opts(void)
558 return qemu_find_opts_singleton("machine");
561 const char *qemu_get_vm_name(void)
563 return qemu_name;
566 static void res_free(void)
568 g_free(boot_splash_filedata);
569 boot_splash_filedata = NULL;
572 static int default_driver_check(void *opaque, QemuOpts *opts, Error **errp)
574 const char *driver = qemu_opt_get(opts, "driver");
575 int i;
577 if (!driver)
578 return 0;
579 for (i = 0; i < ARRAY_SIZE(default_list); i++) {
580 if (strcmp(default_list[i].driver, driver) != 0)
581 continue;
582 *(default_list[i].flag) = 0;
584 return 0;
587 /***********************************************************/
588 /* QEMU state */
590 static RunState current_run_state = RUN_STATE_PRELAUNCH;
592 /* We use RUN_STATE__MAX but any invalid value will do */
593 static RunState vmstop_requested = RUN_STATE__MAX;
594 static QemuMutex vmstop_lock;
596 typedef struct {
597 RunState from;
598 RunState to;
599 } RunStateTransition;
601 static const RunStateTransition runstate_transitions_def[] = {
602 /* from -> to */
603 { RUN_STATE_DEBUG, RUN_STATE_RUNNING },
604 { RUN_STATE_DEBUG, RUN_STATE_FINISH_MIGRATE },
605 { RUN_STATE_DEBUG, RUN_STATE_PRELAUNCH },
607 { RUN_STATE_INMIGRATE, RUN_STATE_INTERNAL_ERROR },
608 { RUN_STATE_INMIGRATE, RUN_STATE_IO_ERROR },
609 { RUN_STATE_INMIGRATE, RUN_STATE_PAUSED },
610 { RUN_STATE_INMIGRATE, RUN_STATE_RUNNING },
611 { RUN_STATE_INMIGRATE, RUN_STATE_SHUTDOWN },
612 { RUN_STATE_INMIGRATE, RUN_STATE_SUSPENDED },
613 { RUN_STATE_INMIGRATE, RUN_STATE_WATCHDOG },
614 { RUN_STATE_INMIGRATE, RUN_STATE_GUEST_PANICKED },
615 { RUN_STATE_INMIGRATE, RUN_STATE_FINISH_MIGRATE },
616 { RUN_STATE_INMIGRATE, RUN_STATE_PRELAUNCH },
617 { RUN_STATE_INMIGRATE, RUN_STATE_POSTMIGRATE },
618 { RUN_STATE_INMIGRATE, RUN_STATE_COLO },
620 { RUN_STATE_INTERNAL_ERROR, RUN_STATE_PAUSED },
621 { RUN_STATE_INTERNAL_ERROR, RUN_STATE_FINISH_MIGRATE },
622 { RUN_STATE_INTERNAL_ERROR, RUN_STATE_PRELAUNCH },
624 { RUN_STATE_IO_ERROR, RUN_STATE_RUNNING },
625 { RUN_STATE_IO_ERROR, RUN_STATE_FINISH_MIGRATE },
626 { RUN_STATE_IO_ERROR, RUN_STATE_PRELAUNCH },
628 { RUN_STATE_PAUSED, RUN_STATE_RUNNING },
629 { RUN_STATE_PAUSED, RUN_STATE_FINISH_MIGRATE },
630 { RUN_STATE_PAUSED, RUN_STATE_PRELAUNCH },
631 { RUN_STATE_PAUSED, RUN_STATE_COLO},
633 { RUN_STATE_POSTMIGRATE, RUN_STATE_RUNNING },
634 { RUN_STATE_POSTMIGRATE, RUN_STATE_FINISH_MIGRATE },
635 { RUN_STATE_POSTMIGRATE, RUN_STATE_PRELAUNCH },
637 { RUN_STATE_PRELAUNCH, RUN_STATE_RUNNING },
638 { RUN_STATE_PRELAUNCH, RUN_STATE_FINISH_MIGRATE },
639 { RUN_STATE_PRELAUNCH, RUN_STATE_INMIGRATE },
641 { RUN_STATE_FINISH_MIGRATE, RUN_STATE_RUNNING },
642 { RUN_STATE_FINISH_MIGRATE, RUN_STATE_POSTMIGRATE },
643 { RUN_STATE_FINISH_MIGRATE, RUN_STATE_PRELAUNCH },
644 { RUN_STATE_FINISH_MIGRATE, RUN_STATE_COLO},
646 { RUN_STATE_RESTORE_VM, RUN_STATE_RUNNING },
647 { RUN_STATE_RESTORE_VM, RUN_STATE_PRELAUNCH },
649 { RUN_STATE_COLO, RUN_STATE_RUNNING },
651 { RUN_STATE_RUNNING, RUN_STATE_DEBUG },
652 { RUN_STATE_RUNNING, RUN_STATE_INTERNAL_ERROR },
653 { RUN_STATE_RUNNING, RUN_STATE_IO_ERROR },
654 { RUN_STATE_RUNNING, RUN_STATE_PAUSED },
655 { RUN_STATE_RUNNING, RUN_STATE_FINISH_MIGRATE },
656 { RUN_STATE_RUNNING, RUN_STATE_RESTORE_VM },
657 { RUN_STATE_RUNNING, RUN_STATE_SAVE_VM },
658 { RUN_STATE_RUNNING, RUN_STATE_SHUTDOWN },
659 { RUN_STATE_RUNNING, RUN_STATE_WATCHDOG },
660 { RUN_STATE_RUNNING, RUN_STATE_GUEST_PANICKED },
661 { RUN_STATE_RUNNING, RUN_STATE_COLO},
663 { RUN_STATE_SAVE_VM, RUN_STATE_RUNNING },
665 { RUN_STATE_SHUTDOWN, RUN_STATE_PAUSED },
666 { RUN_STATE_SHUTDOWN, RUN_STATE_FINISH_MIGRATE },
667 { RUN_STATE_SHUTDOWN, RUN_STATE_PRELAUNCH },
669 { RUN_STATE_DEBUG, RUN_STATE_SUSPENDED },
670 { RUN_STATE_RUNNING, RUN_STATE_SUSPENDED },
671 { RUN_STATE_SUSPENDED, RUN_STATE_RUNNING },
672 { RUN_STATE_SUSPENDED, RUN_STATE_FINISH_MIGRATE },
673 { RUN_STATE_SUSPENDED, RUN_STATE_PRELAUNCH },
674 { RUN_STATE_SUSPENDED, RUN_STATE_COLO},
676 { RUN_STATE_WATCHDOG, RUN_STATE_RUNNING },
677 { RUN_STATE_WATCHDOG, RUN_STATE_FINISH_MIGRATE },
678 { RUN_STATE_WATCHDOG, RUN_STATE_PRELAUNCH },
679 { RUN_STATE_WATCHDOG, RUN_STATE_COLO},
681 { RUN_STATE_GUEST_PANICKED, RUN_STATE_RUNNING },
682 { RUN_STATE_GUEST_PANICKED, RUN_STATE_FINISH_MIGRATE },
683 { RUN_STATE_GUEST_PANICKED, RUN_STATE_PRELAUNCH },
685 { RUN_STATE__MAX, RUN_STATE__MAX },
688 static bool runstate_valid_transitions[RUN_STATE__MAX][RUN_STATE__MAX];
690 bool runstate_check(RunState state)
692 return current_run_state == state;
695 bool runstate_store(char *str, size_t size)
697 const char *state = RunState_lookup[current_run_state];
698 size_t len = strlen(state) + 1;
700 if (len > size) {
701 return false;
703 memcpy(str, state, len);
704 return true;
707 static void runstate_init(void)
709 const RunStateTransition *p;
711 memset(&runstate_valid_transitions, 0, sizeof(runstate_valid_transitions));
712 for (p = &runstate_transitions_def[0]; p->from != RUN_STATE__MAX; p++) {
713 runstate_valid_transitions[p->from][p->to] = true;
716 qemu_mutex_init(&vmstop_lock);
719 /* This function will abort() on invalid state transitions */
720 void runstate_set(RunState new_state)
722 assert(new_state < RUN_STATE__MAX);
724 if (current_run_state == new_state) {
725 return;
728 if (!runstate_valid_transitions[current_run_state][new_state]) {
729 error_report("invalid runstate transition: '%s' -> '%s'",
730 RunState_lookup[current_run_state],
731 RunState_lookup[new_state]);
732 abort();
734 trace_runstate_set(new_state);
735 current_run_state = new_state;
738 int runstate_is_running(void)
740 return runstate_check(RUN_STATE_RUNNING);
743 bool runstate_needs_reset(void)
745 return runstate_check(RUN_STATE_INTERNAL_ERROR) ||
746 runstate_check(RUN_STATE_SHUTDOWN);
749 StatusInfo *qmp_query_status(Error **errp)
751 StatusInfo *info = g_malloc0(sizeof(*info));
753 info->running = runstate_is_running();
754 info->singlestep = singlestep;
755 info->status = current_run_state;
757 return info;
760 static bool qemu_vmstop_requested(RunState *r)
762 qemu_mutex_lock(&vmstop_lock);
763 *r = vmstop_requested;
764 vmstop_requested = RUN_STATE__MAX;
765 qemu_mutex_unlock(&vmstop_lock);
766 return *r < RUN_STATE__MAX;
769 void qemu_system_vmstop_request_prepare(void)
771 qemu_mutex_lock(&vmstop_lock);
774 void qemu_system_vmstop_request(RunState state)
776 vmstop_requested = state;
777 qemu_mutex_unlock(&vmstop_lock);
778 qemu_notify_event();
781 void vm_start(void)
783 RunState requested;
785 qemu_vmstop_requested(&requested);
786 if (runstate_is_running() && requested == RUN_STATE__MAX) {
787 return;
790 /* Ensure that a STOP/RESUME pair of events is emitted if a
791 * vmstop request was pending. The BLOCK_IO_ERROR event, for
792 * example, according to documentation is always followed by
793 * the STOP event.
795 if (runstate_is_running()) {
796 qapi_event_send_stop(&error_abort);
797 } else {
798 replay_enable_events();
799 cpu_enable_ticks();
800 runstate_set(RUN_STATE_RUNNING);
801 vm_state_notify(1, RUN_STATE_RUNNING);
802 resume_all_vcpus();
805 qapi_event_send_resume(&error_abort);
809 /***********************************************************/
810 /* real time host monotonic timer */
812 static time_t qemu_time(void)
814 return qemu_clock_get_ms(QEMU_CLOCK_HOST) / 1000;
817 /***********************************************************/
818 /* host time/date access */
819 void qemu_get_timedate(struct tm *tm, int offset)
821 time_t ti = qemu_time();
823 ti += offset;
824 if (rtc_date_offset == -1) {
825 if (rtc_utc)
826 gmtime_r(&ti, tm);
827 else
828 localtime_r(&ti, tm);
829 } else {
830 ti -= rtc_date_offset;
831 gmtime_r(&ti, tm);
835 int qemu_timedate_diff(struct tm *tm)
837 time_t seconds;
839 if (rtc_date_offset == -1)
840 if (rtc_utc)
841 seconds = mktimegm(tm);
842 else {
843 struct tm tmp = *tm;
844 tmp.tm_isdst = -1; /* use timezone to figure it out */
845 seconds = mktime(&tmp);
847 else
848 seconds = mktimegm(tm) + rtc_date_offset;
850 return seconds - qemu_time();
853 static void configure_rtc_date_offset(const char *startdate, int legacy)
855 time_t rtc_start_date;
856 struct tm tm;
858 if (!strcmp(startdate, "now") && legacy) {
859 rtc_date_offset = -1;
860 } else {
861 if (sscanf(startdate, "%d-%d-%dT%d:%d:%d",
862 &tm.tm_year,
863 &tm.tm_mon,
864 &tm.tm_mday,
865 &tm.tm_hour,
866 &tm.tm_min,
867 &tm.tm_sec) == 6) {
868 /* OK */
869 } else if (sscanf(startdate, "%d-%d-%d",
870 &tm.tm_year,
871 &tm.tm_mon,
872 &tm.tm_mday) == 3) {
873 tm.tm_hour = 0;
874 tm.tm_min = 0;
875 tm.tm_sec = 0;
876 } else {
877 goto date_fail;
879 tm.tm_year -= 1900;
880 tm.tm_mon--;
881 rtc_start_date = mktimegm(&tm);
882 if (rtc_start_date == -1) {
883 date_fail:
884 error_report("invalid date format");
885 error_printf("valid formats: "
886 "'2006-06-17T16:01:21' or '2006-06-17'\n");
887 exit(1);
889 rtc_date_offset = qemu_time() - rtc_start_date;
893 static void configure_rtc(QemuOpts *opts)
895 const char *value;
897 value = qemu_opt_get(opts, "base");
898 if (value) {
899 if (!strcmp(value, "utc")) {
900 rtc_utc = 1;
901 } else if (!strcmp(value, "localtime")) {
902 Error *blocker = NULL;
903 rtc_utc = 0;
904 error_setg(&blocker, QERR_REPLAY_NOT_SUPPORTED,
905 "-rtc base=localtime");
906 replay_add_blocker(blocker);
907 } else {
908 configure_rtc_date_offset(value, 0);
911 value = qemu_opt_get(opts, "clock");
912 if (value) {
913 if (!strcmp(value, "host")) {
914 rtc_clock = QEMU_CLOCK_HOST;
915 } else if (!strcmp(value, "rt")) {
916 rtc_clock = QEMU_CLOCK_REALTIME;
917 } else if (!strcmp(value, "vm")) {
918 rtc_clock = QEMU_CLOCK_VIRTUAL;
919 } else {
920 error_report("invalid option value '%s'", value);
921 exit(1);
924 value = qemu_opt_get(opts, "driftfix");
925 if (value) {
926 if (!strcmp(value, "slew")) {
927 static GlobalProperty slew_lost_ticks = {
928 .driver = "mc146818rtc",
929 .property = "lost_tick_policy",
930 .value = "slew",
933 qdev_prop_register_global(&slew_lost_ticks);
934 } else if (!strcmp(value, "none")) {
935 /* discard is default */
936 } else {
937 error_report("invalid option value '%s'", value);
938 exit(1);
943 /***********************************************************/
944 /* Bluetooth support */
945 static int nb_hcis;
946 static int cur_hci;
947 static struct HCIInfo *hci_table[MAX_NICS];
949 struct HCIInfo *qemu_next_hci(void)
951 if (cur_hci == nb_hcis)
952 return &null_hci;
954 return hci_table[cur_hci++];
957 static int bt_hci_parse(const char *str)
959 struct HCIInfo *hci;
960 bdaddr_t bdaddr;
962 if (nb_hcis >= MAX_NICS) {
963 error_report("too many bluetooth HCIs (max %i)", MAX_NICS);
964 return -1;
967 hci = hci_init(str);
968 if (!hci)
969 return -1;
971 bdaddr.b[0] = 0x52;
972 bdaddr.b[1] = 0x54;
973 bdaddr.b[2] = 0x00;
974 bdaddr.b[3] = 0x12;
975 bdaddr.b[4] = 0x34;
976 bdaddr.b[5] = 0x56 + nb_hcis;
977 hci->bdaddr_set(hci, bdaddr.b);
979 hci_table[nb_hcis++] = hci;
981 return 0;
984 static void bt_vhci_add(int vlan_id)
986 struct bt_scatternet_s *vlan = qemu_find_bt_vlan(vlan_id);
988 if (!vlan->slave)
989 error_report("warning: adding a VHCI to an empty scatternet %i",
990 vlan_id);
992 bt_vhci_init(bt_new_hci(vlan));
995 static struct bt_device_s *bt_device_add(const char *opt)
997 struct bt_scatternet_s *vlan;
998 int vlan_id = 0;
999 char *endp = strstr(opt, ",vlan=");
1000 int len = (endp ? endp - opt : strlen(opt)) + 1;
1001 char devname[10];
1003 pstrcpy(devname, MIN(sizeof(devname), len), opt);
1005 if (endp) {
1006 vlan_id = strtol(endp + 6, &endp, 0);
1007 if (*endp) {
1008 error_report("unrecognised bluetooth vlan Id");
1009 return 0;
1013 vlan = qemu_find_bt_vlan(vlan_id);
1015 if (!vlan->slave)
1016 error_report("warning: adding a slave device to an empty scatternet %i",
1017 vlan_id);
1019 if (!strcmp(devname, "keyboard"))
1020 return bt_keyboard_init(vlan);
1022 error_report("unsupported bluetooth device '%s'", devname);
1023 return 0;
1026 static int bt_parse(const char *opt)
1028 const char *endp, *p;
1029 int vlan;
1031 if (strstart(opt, "hci", &endp)) {
1032 if (!*endp || *endp == ',') {
1033 if (*endp)
1034 if (!strstart(endp, ",vlan=", 0))
1035 opt = endp + 1;
1037 return bt_hci_parse(opt);
1039 } else if (strstart(opt, "vhci", &endp)) {
1040 if (!*endp || *endp == ',') {
1041 if (*endp) {
1042 if (strstart(endp, ",vlan=", &p)) {
1043 vlan = strtol(p, (char **) &endp, 0);
1044 if (*endp) {
1045 error_report("bad scatternet '%s'", p);
1046 return 1;
1048 } else {
1049 error_report("bad parameter '%s'", endp + 1);
1050 return 1;
1052 } else
1053 vlan = 0;
1055 bt_vhci_add(vlan);
1056 return 0;
1058 } else if (strstart(opt, "device:", &endp))
1059 return !bt_device_add(endp);
1061 error_report("bad bluetooth parameter '%s'", opt);
1062 return 1;
1065 static int parse_sandbox(void *opaque, QemuOpts *opts, Error **errp)
1067 /* FIXME: change this to true for 1.3 */
1068 if (qemu_opt_get_bool(opts, "enable", false)) {
1069 #ifdef CONFIG_SECCOMP
1070 if (seccomp_start() < 0) {
1071 error_report("failed to install seccomp syscall filter "
1072 "in the kernel");
1073 return -1;
1075 #else
1076 error_report("seccomp support is disabled");
1077 return -1;
1078 #endif
1081 return 0;
1084 static int parse_name(void *opaque, QemuOpts *opts, Error **errp)
1086 const char *proc_name;
1088 if (qemu_opt_get(opts, "debug-threads")) {
1089 qemu_thread_naming(qemu_opt_get_bool(opts, "debug-threads", false));
1091 qemu_name = qemu_opt_get(opts, "guest");
1093 proc_name = qemu_opt_get(opts, "process");
1094 if (proc_name) {
1095 os_set_proc_name(proc_name);
1098 return 0;
1101 bool defaults_enabled(void)
1103 return has_defaults;
1106 #ifndef _WIN32
1107 static int parse_add_fd(void *opaque, QemuOpts *opts, Error **errp)
1109 int fd, dupfd, flags;
1110 int64_t fdset_id;
1111 const char *fd_opaque = NULL;
1112 AddfdInfo *fdinfo;
1114 fd = qemu_opt_get_number(opts, "fd", -1);
1115 fdset_id = qemu_opt_get_number(opts, "set", -1);
1116 fd_opaque = qemu_opt_get(opts, "opaque");
1118 if (fd < 0) {
1119 error_report("fd option is required and must be non-negative");
1120 return -1;
1123 if (fd <= STDERR_FILENO) {
1124 error_report("fd cannot be a standard I/O stream");
1125 return -1;
1129 * All fds inherited across exec() necessarily have FD_CLOEXEC
1130 * clear, while qemu sets FD_CLOEXEC on all other fds used internally.
1132 flags = fcntl(fd, F_GETFD);
1133 if (flags == -1 || (flags & FD_CLOEXEC)) {
1134 error_report("fd is not valid or already in use");
1135 return -1;
1138 if (fdset_id < 0) {
1139 error_report("set option is required and must be non-negative");
1140 return -1;
1143 #ifdef F_DUPFD_CLOEXEC
1144 dupfd = fcntl(fd, F_DUPFD_CLOEXEC, 0);
1145 #else
1146 dupfd = dup(fd);
1147 if (dupfd != -1) {
1148 qemu_set_cloexec(dupfd);
1150 #endif
1151 if (dupfd == -1) {
1152 error_report("error duplicating fd: %s", strerror(errno));
1153 return -1;
1156 /* add the duplicate fd, and optionally the opaque string, to the fd set */
1157 fdinfo = monitor_fdset_add_fd(dupfd, true, fdset_id, !!fd_opaque, fd_opaque,
1158 &error_abort);
1159 g_free(fdinfo);
1161 return 0;
1164 static int cleanup_add_fd(void *opaque, QemuOpts *opts, Error **errp)
1166 int fd;
1168 fd = qemu_opt_get_number(opts, "fd", -1);
1169 close(fd);
1171 return 0;
1173 #endif
1175 /***********************************************************/
1176 /* QEMU Block devices */
1178 #define HD_OPTS "media=disk"
1179 #define CDROM_OPTS "media=cdrom"
1180 #define FD_OPTS ""
1181 #define PFLASH_OPTS ""
1182 #define MTD_OPTS ""
1183 #define SD_OPTS ""
1185 static int drive_init_func(void *opaque, QemuOpts *opts, Error **errp)
1187 BlockInterfaceType *block_default_type = opaque;
1189 return drive_new(opts, *block_default_type) == NULL;
1192 static int drive_enable_snapshot(void *opaque, QemuOpts *opts, Error **errp)
1194 if (qemu_opt_get(opts, "snapshot") == NULL) {
1195 qemu_opt_set(opts, "snapshot", "on", &error_abort);
1197 return 0;
1200 static void default_drive(int enable, int snapshot, BlockInterfaceType type,
1201 int index, const char *optstr)
1203 QemuOpts *opts;
1204 DriveInfo *dinfo;
1206 if (!enable || drive_get_by_index(type, index)) {
1207 return;
1210 opts = drive_add(type, index, NULL, optstr);
1211 if (snapshot) {
1212 drive_enable_snapshot(NULL, opts, NULL);
1215 dinfo = drive_new(opts, type);
1216 if (!dinfo) {
1217 exit(1);
1219 dinfo->is_default = true;
1223 static QemuOptsList qemu_smp_opts = {
1224 .name = "smp-opts",
1225 .implied_opt_name = "cpus",
1226 .merge_lists = true,
1227 .head = QTAILQ_HEAD_INITIALIZER(qemu_smp_opts.head),
1228 .desc = {
1230 .name = "cpus",
1231 .type = QEMU_OPT_NUMBER,
1232 }, {
1233 .name = "sockets",
1234 .type = QEMU_OPT_NUMBER,
1235 }, {
1236 .name = "cores",
1237 .type = QEMU_OPT_NUMBER,
1238 }, {
1239 .name = "threads",
1240 .type = QEMU_OPT_NUMBER,
1241 }, {
1242 .name = "maxcpus",
1243 .type = QEMU_OPT_NUMBER,
1245 { /*End of list */ }
1249 static void smp_parse(QemuOpts *opts)
1251 if (opts) {
1252 unsigned cpus = qemu_opt_get_number(opts, "cpus", 0);
1253 unsigned sockets = qemu_opt_get_number(opts, "sockets", 0);
1254 unsigned cores = qemu_opt_get_number(opts, "cores", 0);
1255 unsigned threads = qemu_opt_get_number(opts, "threads", 0);
1257 /* compute missing values, prefer sockets over cores over threads */
1258 if (cpus == 0 || sockets == 0) {
1259 sockets = sockets > 0 ? sockets : 1;
1260 cores = cores > 0 ? cores : 1;
1261 threads = threads > 0 ? threads : 1;
1262 if (cpus == 0) {
1263 cpus = cores * threads * sockets;
1265 } else if (cores == 0) {
1266 threads = threads > 0 ? threads : 1;
1267 cores = cpus / (sockets * threads);
1268 cores = cores > 0 ? cores : 1;
1269 } else if (threads == 0) {
1270 threads = cpus / (cores * sockets);
1271 threads = threads > 0 ? threads : 1;
1272 } else if (sockets * cores * threads < cpus) {
1273 error_report("cpu topology: "
1274 "sockets (%u) * cores (%u) * threads (%u) < "
1275 "smp_cpus (%u)",
1276 sockets, cores, threads, cpus);
1277 exit(1);
1280 max_cpus = qemu_opt_get_number(opts, "maxcpus", cpus);
1282 if (max_cpus < cpus) {
1283 error_report("maxcpus must be equal to or greater than smp");
1284 exit(1);
1287 if (sockets * cores * threads > max_cpus) {
1288 error_report("cpu topology: "
1289 "sockets (%u) * cores (%u) * threads (%u) > "
1290 "maxcpus (%u)",
1291 sockets, cores, threads, max_cpus);
1292 exit(1);
1295 smp_cpus = cpus;
1296 smp_cores = cores;
1297 smp_threads = threads;
1300 if (smp_cpus > 1) {
1301 Error *blocker = NULL;
1302 error_setg(&blocker, QERR_REPLAY_NOT_SUPPORTED, "smp");
1303 replay_add_blocker(blocker);
1307 static void realtime_init(void)
1309 if (enable_mlock) {
1310 if (os_mlock() < 0) {
1311 error_report("locking memory failed");
1312 exit(1);
1318 static void configure_msg(QemuOpts *opts)
1320 enable_timestamp_msg = qemu_opt_get_bool(opts, "timestamp", true);
1323 /***********************************************************/
1324 /* Semihosting */
1326 typedef struct SemihostingConfig {
1327 bool enabled;
1328 SemihostingTarget target;
1329 const char **argv;
1330 int argc;
1331 const char *cmdline; /* concatenated argv */
1332 } SemihostingConfig;
1334 static SemihostingConfig semihosting;
1336 bool semihosting_enabled(void)
1338 return semihosting.enabled;
1341 SemihostingTarget semihosting_get_target(void)
1343 return semihosting.target;
1346 const char *semihosting_get_arg(int i)
1348 if (i >= semihosting.argc) {
1349 return NULL;
1351 return semihosting.argv[i];
1354 int semihosting_get_argc(void)
1356 return semihosting.argc;
1359 const char *semihosting_get_cmdline(void)
1361 if (semihosting.cmdline == NULL && semihosting.argc > 0) {
1362 semihosting.cmdline = g_strjoinv(" ", (gchar **)semihosting.argv);
1364 return semihosting.cmdline;
1367 static int add_semihosting_arg(void *opaque,
1368 const char *name, const char *val,
1369 Error **errp)
1371 SemihostingConfig *s = opaque;
1372 if (strcmp(name, "arg") == 0) {
1373 s->argc++;
1374 /* one extra element as g_strjoinv() expects NULL-terminated array */
1375 s->argv = g_realloc(s->argv, (s->argc + 1) * sizeof(void *));
1376 s->argv[s->argc - 1] = val;
1377 s->argv[s->argc] = NULL;
1379 return 0;
1382 /* Use strings passed via -kernel/-append to initialize semihosting.argv[] */
1383 static inline void semihosting_arg_fallback(const char *file, const char *cmd)
1385 char *cmd_token;
1387 /* argv[0] */
1388 add_semihosting_arg(&semihosting, "arg", file, NULL);
1390 /* split -append and initialize argv[1..n] */
1391 cmd_token = strtok(g_strdup(cmd), " ");
1392 while (cmd_token) {
1393 add_semihosting_arg(&semihosting, "arg", cmd_token, NULL);
1394 cmd_token = strtok(NULL, " ");
1398 /* Now we still need this for compatibility with XEN. */
1399 bool has_igd_gfx_passthru;
1400 static void igd_gfx_passthru(void)
1402 has_igd_gfx_passthru = current_machine->igd_gfx_passthru;
1405 /***********************************************************/
1406 /* USB devices */
1408 static int usb_device_add(const char *devname)
1410 USBDevice *dev = NULL;
1411 #ifndef CONFIG_LINUX
1412 const char *p;
1413 #endif
1415 if (!machine_usb(current_machine)) {
1416 return -1;
1419 /* drivers with .usbdevice_name entry in USBDeviceInfo */
1420 dev = usbdevice_create(devname);
1421 if (dev)
1422 goto done;
1424 /* the other ones */
1425 #ifndef CONFIG_LINUX
1426 /* only the linux version is qdev-ified, usb-bsd still needs this */
1427 if (strstart(devname, "host:", &p)) {
1428 dev = usb_host_device_open(usb_bus_find(-1), p);
1430 #endif
1431 if (!dev)
1432 return -1;
1434 done:
1435 return 0;
1438 static int usb_device_del(const char *devname)
1440 int bus_num, addr;
1441 const char *p;
1443 if (strstart(devname, "host:", &p)) {
1444 return -1;
1447 if (!machine_usb(current_machine)) {
1448 return -1;
1451 p = strchr(devname, '.');
1452 if (!p)
1453 return -1;
1454 bus_num = strtoul(devname, NULL, 0);
1455 addr = strtoul(p + 1, NULL, 0);
1457 return usb_device_delete_addr(bus_num, addr);
1460 static int usb_parse(const char *cmdline)
1462 int r;
1463 r = usb_device_add(cmdline);
1464 if (r < 0) {
1465 error_report("could not add USB device '%s'", cmdline);
1467 return r;
1470 void hmp_usb_add(Monitor *mon, const QDict *qdict)
1472 const char *devname = qdict_get_str(qdict, "devname");
1473 if (usb_device_add(devname) < 0) {
1474 error_report("could not add USB device '%s'", devname);
1478 void hmp_usb_del(Monitor *mon, const QDict *qdict)
1480 const char *devname = qdict_get_str(qdict, "devname");
1481 if (usb_device_del(devname) < 0) {
1482 error_report("could not delete USB device '%s'", devname);
1486 /***********************************************************/
1487 /* machine registration */
1489 MachineState *current_machine;
1491 static MachineClass *find_machine(const char *name)
1493 GSList *el, *machines = object_class_get_list(TYPE_MACHINE, false);
1494 MachineClass *mc = NULL;
1496 for (el = machines; el; el = el->next) {
1497 MachineClass *temp = el->data;
1499 if (!strcmp(temp->name, name)) {
1500 mc = temp;
1501 break;
1503 if (temp->alias &&
1504 !strcmp(temp->alias, name)) {
1505 mc = temp;
1506 break;
1510 g_slist_free(machines);
1511 return mc;
1514 MachineClass *find_default_machine(void)
1516 GSList *el, *machines = object_class_get_list(TYPE_MACHINE, false);
1517 MachineClass *mc = NULL;
1519 for (el = machines; el; el = el->next) {
1520 MachineClass *temp = el->data;
1522 if (temp->is_default) {
1523 mc = temp;
1524 break;
1528 g_slist_free(machines);
1529 return mc;
1532 MachineInfoList *qmp_query_machines(Error **errp)
1534 GSList *el, *machines = object_class_get_list(TYPE_MACHINE, false);
1535 MachineInfoList *mach_list = NULL;
1537 for (el = machines; el; el = el->next) {
1538 MachineClass *mc = el->data;
1539 MachineInfoList *entry;
1540 MachineInfo *info;
1542 info = g_malloc0(sizeof(*info));
1543 if (mc->is_default) {
1544 info->has_is_default = true;
1545 info->is_default = true;
1548 if (mc->alias) {
1549 info->has_alias = true;
1550 info->alias = g_strdup(mc->alias);
1553 info->name = g_strdup(mc->name);
1554 info->cpu_max = !mc->max_cpus ? 1 : mc->max_cpus;
1555 info->hotpluggable_cpus = !!mc->query_hotpluggable_cpus;
1557 entry = g_malloc0(sizeof(*entry));
1558 entry->value = info;
1559 entry->next = mach_list;
1560 mach_list = entry;
1563 g_slist_free(machines);
1564 return mach_list;
1567 static int machine_help_func(QemuOpts *opts, MachineState *machine)
1569 ObjectProperty *prop;
1570 ObjectPropertyIterator iter;
1572 if (!qemu_opt_has_help_opt(opts)) {
1573 return 0;
1576 object_property_iter_init(&iter, OBJECT(machine));
1577 while ((prop = object_property_iter_next(&iter))) {
1578 if (!prop->set) {
1579 continue;
1582 error_printf("%s.%s=%s", MACHINE_GET_CLASS(machine)->name,
1583 prop->name, prop->type);
1584 if (prop->description) {
1585 error_printf(" (%s)\n", prop->description);
1586 } else {
1587 error_printf("\n");
1591 return 1;
1594 /***********************************************************/
1595 /* main execution loop */
1597 struct vm_change_state_entry {
1598 VMChangeStateHandler *cb;
1599 void *opaque;
1600 QLIST_ENTRY (vm_change_state_entry) entries;
1603 static QLIST_HEAD(vm_change_state_head, vm_change_state_entry) vm_change_state_head;
1605 VMChangeStateEntry *qemu_add_vm_change_state_handler(VMChangeStateHandler *cb,
1606 void *opaque)
1608 VMChangeStateEntry *e;
1610 e = g_malloc0(sizeof (*e));
1612 e->cb = cb;
1613 e->opaque = opaque;
1614 QLIST_INSERT_HEAD(&vm_change_state_head, e, entries);
1615 return e;
1618 void qemu_del_vm_change_state_handler(VMChangeStateEntry *e)
1620 QLIST_REMOVE (e, entries);
1621 g_free (e);
1624 void vm_state_notify(int running, RunState state)
1626 VMChangeStateEntry *e, *next;
1628 trace_vm_state_notify(running, state);
1630 QLIST_FOREACH_SAFE(e, &vm_change_state_head, entries, next) {
1631 e->cb(e->opaque, running, state);
1635 static int reset_requested;
1636 static int shutdown_requested, shutdown_signal = -1;
1637 static pid_t shutdown_pid;
1638 static int powerdown_requested;
1639 static int debug_requested;
1640 static int suspend_requested;
1641 static WakeupReason wakeup_reason;
1642 static NotifierList powerdown_notifiers =
1643 NOTIFIER_LIST_INITIALIZER(powerdown_notifiers);
1644 static NotifierList suspend_notifiers =
1645 NOTIFIER_LIST_INITIALIZER(suspend_notifiers);
1646 static NotifierList wakeup_notifiers =
1647 NOTIFIER_LIST_INITIALIZER(wakeup_notifiers);
1648 static uint32_t wakeup_reason_mask = ~(1 << QEMU_WAKEUP_REASON_NONE);
1650 int qemu_shutdown_requested_get(void)
1652 return shutdown_requested;
1655 int qemu_reset_requested_get(void)
1657 return reset_requested;
1660 static int qemu_shutdown_requested(void)
1662 return atomic_xchg(&shutdown_requested, 0);
1665 static void qemu_kill_report(void)
1667 if (!qtest_driver() && shutdown_signal != -1) {
1668 if (shutdown_pid == 0) {
1669 /* This happens for eg ^C at the terminal, so it's worth
1670 * avoiding printing an odd message in that case.
1672 error_report("terminating on signal %d", shutdown_signal);
1673 } else {
1674 char *shutdown_cmd = qemu_get_pid_name(shutdown_pid);
1676 error_report("terminating on signal %d from pid " FMT_pid " (%s)",
1677 shutdown_signal, shutdown_pid,
1678 shutdown_cmd ? shutdown_cmd : "<unknown process>");
1679 g_free(shutdown_cmd);
1681 shutdown_signal = -1;
1685 static int qemu_reset_requested(void)
1687 int r = reset_requested;
1688 if (r && replay_checkpoint(CHECKPOINT_RESET_REQUESTED)) {
1689 reset_requested = 0;
1690 return r;
1692 return false;
1695 static int qemu_suspend_requested(void)
1697 int r = suspend_requested;
1698 if (r && replay_checkpoint(CHECKPOINT_SUSPEND_REQUESTED)) {
1699 suspend_requested = 0;
1700 return r;
1702 return false;
1705 static WakeupReason qemu_wakeup_requested(void)
1707 return wakeup_reason;
1710 static int qemu_powerdown_requested(void)
1712 int r = powerdown_requested;
1713 powerdown_requested = 0;
1714 return r;
1717 static int qemu_debug_requested(void)
1719 int r = debug_requested;
1720 debug_requested = 0;
1721 return r;
1724 void qemu_system_reset(bool report)
1726 MachineClass *mc;
1728 mc = current_machine ? MACHINE_GET_CLASS(current_machine) : NULL;
1730 cpu_synchronize_all_states();
1732 if (mc && mc->reset) {
1733 mc->reset();
1734 } else {
1735 qemu_devices_reset();
1737 if (report) {
1738 qapi_event_send_reset(&error_abort);
1740 cpu_synchronize_all_post_reset();
1743 void qemu_system_guest_panicked(void)
1745 if (current_cpu) {
1746 current_cpu->crash_occurred = true;
1748 qapi_event_send_guest_panicked(GUEST_PANIC_ACTION_PAUSE, &error_abort);
1749 vm_stop(RUN_STATE_GUEST_PANICKED);
1750 if (!no_shutdown) {
1751 qapi_event_send_guest_panicked(GUEST_PANIC_ACTION_POWEROFF,
1752 &error_abort);
1753 qemu_system_shutdown_request();
1757 void qemu_system_reset_request(void)
1759 if (no_reboot) {
1760 shutdown_requested = 1;
1761 } else {
1762 reset_requested = 1;
1764 cpu_stop_current();
1765 qemu_notify_event();
1768 static void qemu_system_suspend(void)
1770 pause_all_vcpus();
1771 notifier_list_notify(&suspend_notifiers, NULL);
1772 runstate_set(RUN_STATE_SUSPENDED);
1773 qapi_event_send_suspend(&error_abort);
1776 void qemu_system_suspend_request(void)
1778 if (runstate_check(RUN_STATE_SUSPENDED)) {
1779 return;
1781 suspend_requested = 1;
1782 cpu_stop_current();
1783 qemu_notify_event();
1786 void qemu_register_suspend_notifier(Notifier *notifier)
1788 notifier_list_add(&suspend_notifiers, notifier);
1791 void qemu_system_wakeup_request(WakeupReason reason)
1793 trace_system_wakeup_request(reason);
1795 if (!runstate_check(RUN_STATE_SUSPENDED)) {
1796 return;
1798 if (!(wakeup_reason_mask & (1 << reason))) {
1799 return;
1801 runstate_set(RUN_STATE_RUNNING);
1802 wakeup_reason = reason;
1803 qemu_notify_event();
1806 void qemu_system_wakeup_enable(WakeupReason reason, bool enabled)
1808 if (enabled) {
1809 wakeup_reason_mask |= (1 << reason);
1810 } else {
1811 wakeup_reason_mask &= ~(1 << reason);
1815 void qemu_register_wakeup_notifier(Notifier *notifier)
1817 notifier_list_add(&wakeup_notifiers, notifier);
1820 void qemu_system_killed(int signal, pid_t pid)
1822 shutdown_signal = signal;
1823 shutdown_pid = pid;
1824 no_shutdown = 0;
1826 /* Cannot call qemu_system_shutdown_request directly because
1827 * we are in a signal handler.
1829 shutdown_requested = 1;
1830 qemu_notify_event();
1833 void qemu_system_shutdown_request(void)
1835 trace_qemu_system_shutdown_request();
1836 replay_shutdown_request();
1837 shutdown_requested = 1;
1838 qemu_notify_event();
1841 static void qemu_system_powerdown(void)
1843 qapi_event_send_powerdown(&error_abort);
1844 notifier_list_notify(&powerdown_notifiers, NULL);
1847 void qemu_system_powerdown_request(void)
1849 trace_qemu_system_powerdown_request();
1850 powerdown_requested = 1;
1851 qemu_notify_event();
1854 void qemu_register_powerdown_notifier(Notifier *notifier)
1856 notifier_list_add(&powerdown_notifiers, notifier);
1859 void qemu_system_debug_request(void)
1861 debug_requested = 1;
1862 qemu_notify_event();
1865 static bool main_loop_should_exit(void)
1867 RunState r;
1868 if (qemu_debug_requested()) {
1869 vm_stop(RUN_STATE_DEBUG);
1871 if (qemu_suspend_requested()) {
1872 qemu_system_suspend();
1874 if (qemu_shutdown_requested()) {
1875 qemu_kill_report();
1876 qapi_event_send_shutdown(&error_abort);
1877 if (no_shutdown) {
1878 vm_stop(RUN_STATE_SHUTDOWN);
1879 } else {
1880 return true;
1883 if (qemu_reset_requested()) {
1884 pause_all_vcpus();
1885 qemu_system_reset(VMRESET_REPORT);
1886 resume_all_vcpus();
1887 if (!runstate_check(RUN_STATE_RUNNING) &&
1888 !runstate_check(RUN_STATE_INMIGRATE)) {
1889 runstate_set(RUN_STATE_PRELAUNCH);
1892 if (qemu_wakeup_requested()) {
1893 pause_all_vcpus();
1894 qemu_system_reset(VMRESET_SILENT);
1895 notifier_list_notify(&wakeup_notifiers, &wakeup_reason);
1896 wakeup_reason = QEMU_WAKEUP_REASON_NONE;
1897 resume_all_vcpus();
1898 qapi_event_send_wakeup(&error_abort);
1900 if (qemu_powerdown_requested()) {
1901 qemu_system_powerdown();
1903 if (qemu_vmstop_requested(&r)) {
1904 vm_stop(r);
1906 return false;
1909 static void main_loop(void)
1911 bool nonblocking;
1912 int last_io = 0;
1913 #ifdef CONFIG_PROFILER
1914 int64_t ti;
1915 #endif
1916 do {
1917 nonblocking = tcg_enabled() && last_io > 0;
1918 #ifdef CONFIG_PROFILER
1919 ti = profile_getclock();
1920 #endif
1921 last_io = main_loop_wait(nonblocking);
1922 #ifdef CONFIG_PROFILER
1923 dev_time += profile_getclock() - ti;
1924 #endif
1925 } while (!main_loop_should_exit());
1928 static void version(void)
1930 printf("QEMU emulator version " QEMU_VERSION QEMU_PKGVERSION "\n"
1931 QEMU_COPYRIGHT "\n");
1934 static void help(int exitcode)
1936 version();
1937 printf("usage: %s [options] [disk_image]\n\n"
1938 "'disk_image' is a raw hard disk image for IDE hard disk 0\n\n",
1939 error_get_progname());
1941 #define QEMU_OPTIONS_GENERATE_HELP
1942 #include "qemu-options-wrapper.h"
1944 printf("\nDuring emulation, the following keys are useful:\n"
1945 "ctrl-alt-f toggle full screen\n"
1946 "ctrl-alt-n switch to virtual console 'n'\n"
1947 "ctrl-alt toggle mouse and keyboard grab\n"
1948 "\n"
1949 "When using -nographic, press 'ctrl-a h' to get some help.\n");
1951 exit(exitcode);
1954 #define HAS_ARG 0x0001
1956 typedef struct QEMUOption {
1957 const char *name;
1958 int flags;
1959 int index;
1960 uint32_t arch_mask;
1961 } QEMUOption;
1963 static const QEMUOption qemu_options[] = {
1964 { "h", 0, QEMU_OPTION_h, QEMU_ARCH_ALL },
1965 #define QEMU_OPTIONS_GENERATE_OPTIONS
1966 #include "qemu-options-wrapper.h"
1967 { NULL },
1970 typedef struct VGAInterfaceInfo {
1971 const char *opt_name; /* option name */
1972 const char *name; /* human-readable name */
1973 /* Class names indicating that support is available.
1974 * If no class is specified, the interface is always available */
1975 const char *class_names[2];
1976 } VGAInterfaceInfo;
1978 static VGAInterfaceInfo vga_interfaces[VGA_TYPE_MAX] = {
1979 [VGA_NONE] = {
1980 .opt_name = "none",
1982 [VGA_STD] = {
1983 .opt_name = "std",
1984 .name = "standard VGA",
1985 .class_names = { "VGA", "isa-vga" },
1987 [VGA_CIRRUS] = {
1988 .opt_name = "cirrus",
1989 .name = "Cirrus VGA",
1990 .class_names = { "cirrus-vga", "isa-cirrus-vga" },
1992 [VGA_VMWARE] = {
1993 .opt_name = "vmware",
1994 .name = "VMWare SVGA",
1995 .class_names = { "vmware-svga" },
1997 [VGA_VIRTIO] = {
1998 .opt_name = "virtio",
1999 .name = "Virtio VGA",
2000 .class_names = { "virtio-vga" },
2002 [VGA_QXL] = {
2003 .opt_name = "qxl",
2004 .name = "QXL VGA",
2005 .class_names = { "qxl-vga" },
2007 [VGA_TCX] = {
2008 .opt_name = "tcx",
2009 .name = "TCX framebuffer",
2010 .class_names = { "SUNW,tcx" },
2012 [VGA_CG3] = {
2013 .opt_name = "cg3",
2014 .name = "CG3 framebuffer",
2015 .class_names = { "cgthree" },
2017 [VGA_XENFB] = {
2018 .opt_name = "xenfb",
2022 static bool vga_interface_available(VGAInterfaceType t)
2024 VGAInterfaceInfo *ti = &vga_interfaces[t];
2026 assert(t < VGA_TYPE_MAX);
2027 return !ti->class_names[0] ||
2028 object_class_by_name(ti->class_names[0]) ||
2029 object_class_by_name(ti->class_names[1]);
2032 static void select_vgahw(const char *p)
2034 const char *opts;
2035 int t;
2037 assert(vga_interface_type == VGA_NONE);
2038 for (t = 0; t < VGA_TYPE_MAX; t++) {
2039 VGAInterfaceInfo *ti = &vga_interfaces[t];
2040 if (ti->opt_name && strstart(p, ti->opt_name, &opts)) {
2041 if (!vga_interface_available(t)) {
2042 error_report("%s not available", ti->name);
2043 exit(1);
2045 vga_interface_type = t;
2046 break;
2049 if (t == VGA_TYPE_MAX) {
2050 invalid_vga:
2051 error_report("unknown vga type: %s", p);
2052 exit(1);
2054 while (*opts) {
2055 const char *nextopt;
2057 if (strstart(opts, ",retrace=", &nextopt)) {
2058 opts = nextopt;
2059 if (strstart(opts, "dumb", &nextopt))
2060 vga_retrace_method = VGA_RETRACE_DUMB;
2061 else if (strstart(opts, "precise", &nextopt))
2062 vga_retrace_method = VGA_RETRACE_PRECISE;
2063 else goto invalid_vga;
2064 } else goto invalid_vga;
2065 opts = nextopt;
2069 typedef enum DisplayType {
2070 DT_DEFAULT,
2071 DT_CURSES,
2072 DT_SDL,
2073 DT_COCOA,
2074 DT_GTK,
2075 DT_NONE,
2076 } DisplayType;
2078 static DisplayType select_display(const char *p)
2080 const char *opts;
2081 DisplayType display = DT_DEFAULT;
2083 if (strstart(p, "sdl", &opts)) {
2084 #ifdef CONFIG_SDL
2085 display = DT_SDL;
2086 while (*opts) {
2087 const char *nextopt;
2089 if (strstart(opts, ",frame=", &nextopt)) {
2090 opts = nextopt;
2091 if (strstart(opts, "on", &nextopt)) {
2092 no_frame = 0;
2093 } else if (strstart(opts, "off", &nextopt)) {
2094 no_frame = 1;
2095 } else {
2096 goto invalid_sdl_args;
2098 } else if (strstart(opts, ",alt_grab=", &nextopt)) {
2099 opts = nextopt;
2100 if (strstart(opts, "on", &nextopt)) {
2101 alt_grab = 1;
2102 } else if (strstart(opts, "off", &nextopt)) {
2103 alt_grab = 0;
2104 } else {
2105 goto invalid_sdl_args;
2107 } else if (strstart(opts, ",ctrl_grab=", &nextopt)) {
2108 opts = nextopt;
2109 if (strstart(opts, "on", &nextopt)) {
2110 ctrl_grab = 1;
2111 } else if (strstart(opts, "off", &nextopt)) {
2112 ctrl_grab = 0;
2113 } else {
2114 goto invalid_sdl_args;
2116 } else if (strstart(opts, ",window_close=", &nextopt)) {
2117 opts = nextopt;
2118 if (strstart(opts, "on", &nextopt)) {
2119 no_quit = 0;
2120 } else if (strstart(opts, "off", &nextopt)) {
2121 no_quit = 1;
2122 } else {
2123 goto invalid_sdl_args;
2125 } else if (strstart(opts, ",gl=", &nextopt)) {
2126 opts = nextopt;
2127 if (strstart(opts, "on", &nextopt)) {
2128 request_opengl = 1;
2129 } else if (strstart(opts, "off", &nextopt)) {
2130 request_opengl = 0;
2131 } else {
2132 goto invalid_sdl_args;
2134 } else {
2135 invalid_sdl_args:
2136 error_report("invalid SDL option string");
2137 exit(1);
2139 opts = nextopt;
2141 #else
2142 error_report("SDL support is disabled");
2143 exit(1);
2144 #endif
2145 } else if (strstart(p, "vnc", &opts)) {
2146 if (*opts == '=') {
2147 vnc_parse(opts + 1, &error_fatal);
2148 } else {
2149 error_report("VNC requires a display argument vnc=<display>");
2150 exit(1);
2152 } else if (strstart(p, "curses", &opts)) {
2153 #ifdef CONFIG_CURSES
2154 display = DT_CURSES;
2155 #else
2156 error_report("curses support is disabled");
2157 exit(1);
2158 #endif
2159 } else if (strstart(p, "gtk", &opts)) {
2160 #ifdef CONFIG_GTK
2161 display = DT_GTK;
2162 while (*opts) {
2163 const char *nextopt;
2165 if (strstart(opts, ",grab_on_hover=", &nextopt)) {
2166 opts = nextopt;
2167 if (strstart(opts, "on", &nextopt)) {
2168 grab_on_hover = true;
2169 } else if (strstart(opts, "off", &nextopt)) {
2170 grab_on_hover = false;
2171 } else {
2172 goto invalid_gtk_args;
2174 } else if (strstart(opts, ",gl=", &nextopt)) {
2175 opts = nextopt;
2176 if (strstart(opts, "on", &nextopt)) {
2177 request_opengl = 1;
2178 } else if (strstart(opts, "off", &nextopt)) {
2179 request_opengl = 0;
2180 } else {
2181 goto invalid_gtk_args;
2183 } else {
2184 invalid_gtk_args:
2185 error_report("invalid GTK option string");
2186 exit(1);
2188 opts = nextopt;
2190 #else
2191 error_report("GTK support is disabled");
2192 exit(1);
2193 #endif
2194 } else if (strstart(p, "none", &opts)) {
2195 display = DT_NONE;
2196 } else {
2197 error_report("unknown display type");
2198 exit(1);
2201 return display;
2204 static int balloon_parse(const char *arg)
2206 QemuOpts *opts;
2208 if (strcmp(arg, "none") == 0) {
2209 return 0;
2212 if (!strncmp(arg, "virtio", 6)) {
2213 if (arg[6] == ',') {
2214 /* have params -> parse them */
2215 opts = qemu_opts_parse_noisily(qemu_find_opts("device"), arg + 7,
2216 false);
2217 if (!opts)
2218 return -1;
2219 } else {
2220 /* create empty opts */
2221 opts = qemu_opts_create(qemu_find_opts("device"), NULL, 0,
2222 &error_abort);
2224 qemu_opt_set(opts, "driver", "virtio-balloon", &error_abort);
2225 return 0;
2228 return -1;
2231 char *qemu_find_file(int type, const char *name)
2233 int i;
2234 const char *subdir;
2235 char *buf;
2237 /* Try the name as a straight path first */
2238 if (access(name, R_OK) == 0) {
2239 trace_load_file(name, name);
2240 return g_strdup(name);
2243 switch (type) {
2244 case QEMU_FILE_TYPE_BIOS:
2245 subdir = "";
2246 break;
2247 case QEMU_FILE_TYPE_KEYMAP:
2248 subdir = "keymaps/";
2249 break;
2250 default:
2251 abort();
2254 for (i = 0; i < data_dir_idx; i++) {
2255 buf = g_strdup_printf("%s/%s%s", data_dir[i], subdir, name);
2256 if (access(buf, R_OK) == 0) {
2257 trace_load_file(name, buf);
2258 return buf;
2260 g_free(buf);
2262 return NULL;
2265 static inline bool nonempty_str(const char *str)
2267 return str && *str;
2270 static int parse_fw_cfg(void *opaque, QemuOpts *opts, Error **errp)
2272 gchar *buf;
2273 size_t size;
2274 const char *name, *file, *str;
2275 FWCfgState *fw_cfg = (FWCfgState *) opaque;
2277 if (fw_cfg == NULL) {
2278 error_report("fw_cfg device not available");
2279 return -1;
2281 name = qemu_opt_get(opts, "name");
2282 file = qemu_opt_get(opts, "file");
2283 str = qemu_opt_get(opts, "string");
2285 /* we need name and either a file or the content string */
2286 if (!(nonempty_str(name) && (nonempty_str(file) || nonempty_str(str)))) {
2287 error_report("invalid argument(s)");
2288 return -1;
2290 if (nonempty_str(file) && nonempty_str(str)) {
2291 error_report("file and string are mutually exclusive");
2292 return -1;
2294 if (strlen(name) > FW_CFG_MAX_FILE_PATH - 1) {
2295 error_report("name too long (max. %d char)", FW_CFG_MAX_FILE_PATH - 1);
2296 return -1;
2298 if (strncmp(name, "opt/", 4) != 0) {
2299 error_report("warning: externally provided fw_cfg item names "
2300 "should be prefixed with \"opt/\"");
2302 if (nonempty_str(str)) {
2303 size = strlen(str); /* NUL terminator NOT included in fw_cfg blob */
2304 buf = g_memdup(str, size);
2305 } else {
2306 if (!g_file_get_contents(file, &buf, &size, NULL)) {
2307 error_report("can't load %s", file);
2308 return -1;
2311 /* For legacy, keep user files in a specific global order. */
2312 fw_cfg_set_order_override(fw_cfg, FW_CFG_ORDER_OVERRIDE_USER);
2313 fw_cfg_add_file(fw_cfg, name, buf, size);
2314 fw_cfg_reset_order_override(fw_cfg);
2315 return 0;
2318 static int device_help_func(void *opaque, QemuOpts *opts, Error **errp)
2320 return qdev_device_help(opts);
2323 static int device_init_func(void *opaque, QemuOpts *opts, Error **errp)
2325 Error *err = NULL;
2326 DeviceState *dev;
2328 dev = qdev_device_add(opts, &err);
2329 if (!dev) {
2330 error_report_err(err);
2331 return -1;
2333 object_unref(OBJECT(dev));
2334 return 0;
2337 static int chardev_init_func(void *opaque, QemuOpts *opts, Error **errp)
2339 Error *local_err = NULL;
2341 qemu_chr_new_from_opts(opts, &local_err);
2342 if (local_err) {
2343 error_report_err(local_err);
2344 return -1;
2346 return 0;
2349 #ifdef CONFIG_VIRTFS
2350 static int fsdev_init_func(void *opaque, QemuOpts *opts, Error **errp)
2352 return qemu_fsdev_add(opts);
2354 #endif
2356 static int mon_init_func(void *opaque, QemuOpts *opts, Error **errp)
2358 CharDriverState *chr;
2359 const char *chardev;
2360 const char *mode;
2361 int flags;
2363 mode = qemu_opt_get(opts, "mode");
2364 if (mode == NULL) {
2365 mode = "readline";
2367 if (strcmp(mode, "readline") == 0) {
2368 flags = MONITOR_USE_READLINE;
2369 } else if (strcmp(mode, "control") == 0) {
2370 flags = MONITOR_USE_CONTROL;
2371 } else {
2372 error_report("unknown monitor mode \"%s\"", mode);
2373 exit(1);
2376 if (qemu_opt_get_bool(opts, "pretty", 0))
2377 flags |= MONITOR_USE_PRETTY;
2379 if (qemu_opt_get_bool(opts, "default", 0)) {
2380 error_report("option 'default' does nothing and is deprecated");
2383 chardev = qemu_opt_get(opts, "chardev");
2384 chr = qemu_chr_find(chardev);
2385 if (chr == NULL) {
2386 error_report("chardev \"%s\" not found", chardev);
2387 exit(1);
2390 monitor_init(chr, flags);
2391 return 0;
2394 static void monitor_parse(const char *optarg, const char *mode, bool pretty)
2396 static int monitor_device_index = 0;
2397 QemuOpts *opts;
2398 const char *p;
2399 char label[32];
2401 if (strstart(optarg, "chardev:", &p)) {
2402 snprintf(label, sizeof(label), "%s", p);
2403 } else {
2404 snprintf(label, sizeof(label), "compat_monitor%d",
2405 monitor_device_index);
2406 opts = qemu_chr_parse_compat(label, optarg);
2407 if (!opts) {
2408 error_report("parse error: %s", optarg);
2409 exit(1);
2413 opts = qemu_opts_create(qemu_find_opts("mon"), label, 1, &error_fatal);
2414 qemu_opt_set(opts, "mode", mode, &error_abort);
2415 qemu_opt_set(opts, "chardev", label, &error_abort);
2416 qemu_opt_set_bool(opts, "pretty", pretty, &error_abort);
2417 monitor_device_index++;
2420 struct device_config {
2421 enum {
2422 DEV_USB, /* -usbdevice */
2423 DEV_BT, /* -bt */
2424 DEV_SERIAL, /* -serial */
2425 DEV_PARALLEL, /* -parallel */
2426 DEV_VIRTCON, /* -virtioconsole */
2427 DEV_DEBUGCON, /* -debugcon */
2428 DEV_GDB, /* -gdb, -s */
2429 DEV_SCLP, /* s390 sclp */
2430 } type;
2431 const char *cmdline;
2432 Location loc;
2433 QTAILQ_ENTRY(device_config) next;
2436 static QTAILQ_HEAD(, device_config) device_configs =
2437 QTAILQ_HEAD_INITIALIZER(device_configs);
2439 static void add_device_config(int type, const char *cmdline)
2441 struct device_config *conf;
2443 conf = g_malloc0(sizeof(*conf));
2444 conf->type = type;
2445 conf->cmdline = cmdline;
2446 loc_save(&conf->loc);
2447 QTAILQ_INSERT_TAIL(&device_configs, conf, next);
2450 static int foreach_device_config(int type, int (*func)(const char *cmdline))
2452 struct device_config *conf;
2453 int rc;
2455 QTAILQ_FOREACH(conf, &device_configs, next) {
2456 if (conf->type != type)
2457 continue;
2458 loc_push_restore(&conf->loc);
2459 rc = func(conf->cmdline);
2460 loc_pop(&conf->loc);
2461 if (rc) {
2462 return rc;
2465 return 0;
2468 static int serial_parse(const char *devname)
2470 static int index = 0;
2471 char label[32];
2473 if (strcmp(devname, "none") == 0)
2474 return 0;
2475 if (index == MAX_SERIAL_PORTS) {
2476 error_report("too many serial ports");
2477 exit(1);
2479 snprintf(label, sizeof(label), "serial%d", index);
2480 serial_hds[index] = qemu_chr_new(label, devname);
2481 if (!serial_hds[index]) {
2482 error_report("could not connect serial device"
2483 " to character backend '%s'", devname);
2484 return -1;
2486 index++;
2487 return 0;
2490 static int parallel_parse(const char *devname)
2492 static int index = 0;
2493 char label[32];
2495 if (strcmp(devname, "none") == 0)
2496 return 0;
2497 if (index == MAX_PARALLEL_PORTS) {
2498 error_report("too many parallel ports");
2499 exit(1);
2501 snprintf(label, sizeof(label), "parallel%d", index);
2502 parallel_hds[index] = qemu_chr_new(label, devname);
2503 if (!parallel_hds[index]) {
2504 error_report("could not connect parallel device"
2505 " to character backend '%s'", devname);
2506 return -1;
2508 index++;
2509 return 0;
2512 static int virtcon_parse(const char *devname)
2514 QemuOptsList *device = qemu_find_opts("device");
2515 static int index = 0;
2516 char label[32];
2517 QemuOpts *bus_opts, *dev_opts;
2519 if (strcmp(devname, "none") == 0)
2520 return 0;
2521 if (index == MAX_VIRTIO_CONSOLES) {
2522 error_report("too many virtio consoles");
2523 exit(1);
2526 bus_opts = qemu_opts_create(device, NULL, 0, &error_abort);
2527 qemu_opt_set(bus_opts, "driver", "virtio-serial", &error_abort);
2529 dev_opts = qemu_opts_create(device, NULL, 0, &error_abort);
2530 qemu_opt_set(dev_opts, "driver", "virtconsole", &error_abort);
2532 snprintf(label, sizeof(label), "virtcon%d", index);
2533 virtcon_hds[index] = qemu_chr_new(label, devname);
2534 if (!virtcon_hds[index]) {
2535 error_report("could not connect virtio console"
2536 " to character backend '%s'", devname);
2537 return -1;
2539 qemu_opt_set(dev_opts, "chardev", label, &error_abort);
2541 index++;
2542 return 0;
2545 static int sclp_parse(const char *devname)
2547 QemuOptsList *device = qemu_find_opts("device");
2548 static int index = 0;
2549 char label[32];
2550 QemuOpts *dev_opts;
2552 if (strcmp(devname, "none") == 0) {
2553 return 0;
2555 if (index == MAX_SCLP_CONSOLES) {
2556 error_report("too many sclp consoles");
2557 exit(1);
2560 assert(arch_type == QEMU_ARCH_S390X);
2562 dev_opts = qemu_opts_create(device, NULL, 0, NULL);
2563 qemu_opt_set(dev_opts, "driver", "sclpconsole", &error_abort);
2565 snprintf(label, sizeof(label), "sclpcon%d", index);
2566 sclp_hds[index] = qemu_chr_new(label, devname);
2567 if (!sclp_hds[index]) {
2568 error_report("could not connect sclp console"
2569 " to character backend '%s'", devname);
2570 return -1;
2572 qemu_opt_set(dev_opts, "chardev", label, &error_abort);
2574 index++;
2575 return 0;
2578 static int debugcon_parse(const char *devname)
2580 QemuOpts *opts;
2582 if (!qemu_chr_new("debugcon", devname)) {
2583 exit(1);
2585 opts = qemu_opts_create(qemu_find_opts("device"), "debugcon", 1, NULL);
2586 if (!opts) {
2587 error_report("already have a debugcon device");
2588 exit(1);
2590 qemu_opt_set(opts, "driver", "isa-debugcon", &error_abort);
2591 qemu_opt_set(opts, "chardev", "debugcon", &error_abort);
2592 return 0;
2595 static gint machine_class_cmp(gconstpointer a, gconstpointer b)
2597 const MachineClass *mc1 = a, *mc2 = b;
2598 int res;
2600 if (mc1->family == NULL) {
2601 if (mc2->family == NULL) {
2602 /* Compare standalone machine types against each other; they sort
2603 * in increasing order.
2605 return strcmp(object_class_get_name(OBJECT_CLASS(mc1)),
2606 object_class_get_name(OBJECT_CLASS(mc2)));
2609 /* Standalone machine types sort after families. */
2610 return 1;
2613 if (mc2->family == NULL) {
2614 /* Families sort before standalone machine types. */
2615 return -1;
2618 /* Families sort between each other alphabetically increasingly. */
2619 res = strcmp(mc1->family, mc2->family);
2620 if (res != 0) {
2621 return res;
2624 /* Within the same family, machine types sort in decreasing order. */
2625 return strcmp(object_class_get_name(OBJECT_CLASS(mc2)),
2626 object_class_get_name(OBJECT_CLASS(mc1)));
2629 static MachineClass *machine_parse(const char *name)
2631 MachineClass *mc = NULL;
2632 GSList *el, *machines = object_class_get_list(TYPE_MACHINE, false);
2634 if (name) {
2635 mc = find_machine(name);
2637 if (mc) {
2638 g_slist_free(machines);
2639 return mc;
2641 if (name && !is_help_option(name)) {
2642 error_report("unsupported machine type");
2643 error_printf("Use -machine help to list supported machines\n");
2644 } else {
2645 printf("Supported machines are:\n");
2646 machines = g_slist_sort(machines, machine_class_cmp);
2647 for (el = machines; el; el = el->next) {
2648 MachineClass *mc = el->data;
2649 if (mc->alias) {
2650 printf("%-20s %s (alias of %s)\n", mc->alias, mc->desc, mc->name);
2652 printf("%-20s %s%s\n", mc->name, mc->desc,
2653 mc->is_default ? " (default)" : "");
2657 g_slist_free(machines);
2658 exit(!name || !is_help_option(name));
2661 void qemu_add_exit_notifier(Notifier *notify)
2663 notifier_list_add(&exit_notifiers, notify);
2666 void qemu_remove_exit_notifier(Notifier *notify)
2668 notifier_remove(notify);
2671 static void qemu_run_exit_notifiers(void)
2673 notifier_list_notify(&exit_notifiers, NULL);
2676 static bool machine_init_done;
2678 void qemu_add_machine_init_done_notifier(Notifier *notify)
2680 notifier_list_add(&machine_init_done_notifiers, notify);
2681 if (machine_init_done) {
2682 notify->notify(notify, NULL);
2686 void qemu_remove_machine_init_done_notifier(Notifier *notify)
2688 notifier_remove(notify);
2691 static void qemu_run_machine_init_done_notifiers(void)
2693 notifier_list_notify(&machine_init_done_notifiers, NULL);
2694 machine_init_done = true;
2697 static const QEMUOption *lookup_opt(int argc, char **argv,
2698 const char **poptarg, int *poptind)
2700 const QEMUOption *popt;
2701 int optind = *poptind;
2702 char *r = argv[optind];
2703 const char *optarg;
2705 loc_set_cmdline(argv, optind, 1);
2706 optind++;
2707 /* Treat --foo the same as -foo. */
2708 if (r[1] == '-')
2709 r++;
2710 popt = qemu_options;
2711 for(;;) {
2712 if (!popt->name) {
2713 error_report("invalid option");
2714 exit(1);
2716 if (!strcmp(popt->name, r + 1))
2717 break;
2718 popt++;
2720 if (popt->flags & HAS_ARG) {
2721 if (optind >= argc) {
2722 error_report("requires an argument");
2723 exit(1);
2725 optarg = argv[optind++];
2726 loc_set_cmdline(argv, optind - 2, 2);
2727 } else {
2728 optarg = NULL;
2731 *poptarg = optarg;
2732 *poptind = optind;
2734 return popt;
2737 static MachineClass *select_machine(void)
2739 MachineClass *machine_class = find_default_machine();
2740 const char *optarg;
2741 QemuOpts *opts;
2742 Location loc;
2744 loc_push_none(&loc);
2746 opts = qemu_get_machine_opts();
2747 qemu_opts_loc_restore(opts);
2749 optarg = qemu_opt_get(opts, "type");
2750 if (optarg) {
2751 machine_class = machine_parse(optarg);
2754 if (!machine_class) {
2755 error_report("No machine specified, and there is no default");
2756 error_printf("Use -machine help to list supported machines\n");
2757 exit(1);
2760 loc_pop(&loc);
2761 return machine_class;
2764 static int machine_set_property(void *opaque,
2765 const char *name, const char *value,
2766 Error **errp)
2768 Object *obj = OBJECT(opaque);
2769 Error *local_err = NULL;
2770 char *p, *qom_name;
2772 if (strcmp(name, "type") == 0) {
2773 return 0;
2776 qom_name = g_strdup(name);
2777 for (p = qom_name; *p; p++) {
2778 if (*p == '_') {
2779 *p = '-';
2783 object_property_parse(obj, value, qom_name, &local_err);
2784 g_free(qom_name);
2786 if (local_err) {
2787 error_report_err(local_err);
2788 return -1;
2791 return 0;
2796 * Initial object creation happens before all other
2797 * QEMU data types are created. The majority of objects
2798 * can be created at this point. The rng-egd object
2799 * cannot be created here, as it depends on the chardev
2800 * already existing.
2802 static bool object_create_initial(const char *type)
2804 if (g_str_equal(type, "rng-egd")) {
2805 return false;
2809 * return false for concrete netfilters since
2810 * they depend on netdevs already existing
2812 if (g_str_equal(type, "filter-buffer") ||
2813 g_str_equal(type, "filter-dump") ||
2814 g_str_equal(type, "filter-mirror") ||
2815 g_str_equal(type, "filter-redirector") ||
2816 g_str_equal(type, "colo-compare") ||
2817 g_str_equal(type, "filter-rewriter") ||
2818 g_str_equal(type, "filter-replay")) {
2819 return false;
2822 /* Memory allocation by backends needs to be done
2823 * after configure_accelerator() (due to the tcg_enabled()
2824 * checks at memory_region_init_*()).
2826 * Also, allocation of large amounts of memory may delay
2827 * chardev initialization for too long, and trigger timeouts
2828 * on software that waits for a monitor socket to be created
2829 * (e.g. libvirt).
2831 if (g_str_has_prefix(type, "memory-backend-")) {
2832 return false;
2835 return true;
2840 * The remainder of object creation happens after the
2841 * creation of chardev, fsdev, net clients and device data types.
2843 static bool object_create_delayed(const char *type)
2845 return !object_create_initial(type);
2849 static void set_memory_options(uint64_t *ram_slots, ram_addr_t *maxram_size,
2850 MachineClass *mc)
2852 uint64_t sz;
2853 const char *mem_str;
2854 const char *maxmem_str, *slots_str;
2855 const ram_addr_t default_ram_size = mc->default_ram_size;
2856 QemuOpts *opts = qemu_find_opts_singleton("memory");
2857 Location loc;
2859 loc_push_none(&loc);
2860 qemu_opts_loc_restore(opts);
2862 sz = 0;
2863 mem_str = qemu_opt_get(opts, "size");
2864 if (mem_str) {
2865 if (!*mem_str) {
2866 error_report("missing 'size' option value");
2867 exit(EXIT_FAILURE);
2870 sz = qemu_opt_get_size(opts, "size", ram_size);
2872 /* Fix up legacy suffix-less format */
2873 if (g_ascii_isdigit(mem_str[strlen(mem_str) - 1])) {
2874 uint64_t overflow_check = sz;
2876 sz <<= 20;
2877 if ((sz >> 20) != overflow_check) {
2878 error_report("too large 'size' option value");
2879 exit(EXIT_FAILURE);
2884 /* backward compatibility behaviour for case "-m 0" */
2885 if (sz == 0) {
2886 sz = default_ram_size;
2889 sz = QEMU_ALIGN_UP(sz, 8192);
2890 ram_size = sz;
2891 if (ram_size != sz) {
2892 error_report("ram size too large");
2893 exit(EXIT_FAILURE);
2896 /* store value for the future use */
2897 qemu_opt_set_number(opts, "size", ram_size, &error_abort);
2898 *maxram_size = ram_size;
2900 maxmem_str = qemu_opt_get(opts, "maxmem");
2901 slots_str = qemu_opt_get(opts, "slots");
2902 if (maxmem_str && slots_str) {
2903 uint64_t slots;
2905 sz = qemu_opt_get_size(opts, "maxmem", 0);
2906 slots = qemu_opt_get_number(opts, "slots", 0);
2907 if (sz < ram_size) {
2908 error_report("invalid value of -m option maxmem: "
2909 "maximum memory size (0x%" PRIx64 ") must be at least "
2910 "the initial memory size (0x" RAM_ADDR_FMT ")",
2911 sz, ram_size);
2912 exit(EXIT_FAILURE);
2913 } else if (sz > ram_size) {
2914 if (!slots) {
2915 error_report("invalid value of -m option: maxmem was "
2916 "specified, but no hotplug slots were specified");
2917 exit(EXIT_FAILURE);
2919 } else if (slots) {
2920 error_report("invalid value of -m option maxmem: "
2921 "memory slots were specified but maximum memory size "
2922 "(0x%" PRIx64 ") is equal to the initial memory size "
2923 "(0x" RAM_ADDR_FMT ")", sz, ram_size);
2924 exit(EXIT_FAILURE);
2927 *maxram_size = sz;
2928 *ram_slots = slots;
2929 } else if ((!maxmem_str && slots_str) ||
2930 (maxmem_str && !slots_str)) {
2931 error_report("invalid -m option value: missing "
2932 "'%s' option", slots_str ? "maxmem" : "slots");
2933 exit(EXIT_FAILURE);
2936 loc_pop(&loc);
2939 static int global_init_func(void *opaque, QemuOpts *opts, Error **errp)
2941 GlobalProperty *g;
2943 g = g_malloc0(sizeof(*g));
2944 g->driver = qemu_opt_get(opts, "driver");
2945 g->property = qemu_opt_get(opts, "property");
2946 g->value = qemu_opt_get(opts, "value");
2947 g->user_provided = true;
2948 g->errp = &error_fatal;
2949 qdev_prop_register_global(g);
2950 return 0;
2953 static int qemu_read_default_config_file(void)
2955 int ret;
2957 ret = qemu_read_config_file(CONFIG_QEMU_CONFDIR "/qemu.conf");
2958 if (ret < 0 && ret != -ENOENT) {
2959 return ret;
2962 return 0;
2965 int main(int argc, char **argv, char **envp)
2967 int i;
2968 int snapshot, linux_boot;
2969 const char *initrd_filename;
2970 const char *kernel_filename, *kernel_cmdline;
2971 const char *boot_order = NULL;
2972 const char *boot_once = NULL;
2973 DisplayState *ds;
2974 int cyls, heads, secs, translation;
2975 QemuOpts *hda_opts = NULL, *opts, *machine_opts, *icount_opts = NULL;
2976 QemuOptsList *olist;
2977 int optind;
2978 const char *optarg;
2979 const char *loadvm = NULL;
2980 MachineClass *machine_class;
2981 const char *cpu_model;
2982 const char *vga_model = NULL;
2983 const char *qtest_chrdev = NULL;
2984 const char *qtest_log = NULL;
2985 const char *pid_file = NULL;
2986 const char *incoming = NULL;
2987 bool defconfig = true;
2988 bool userconfig = true;
2989 bool nographic = false;
2990 DisplayType display_type = DT_DEFAULT;
2991 int display_remote = 0;
2992 const char *log_mask = NULL;
2993 const char *log_file = NULL;
2994 char *trace_file = NULL;
2995 ram_addr_t maxram_size;
2996 uint64_t ram_slots = 0;
2997 FILE *vmstate_dump_file = NULL;
2998 Error *main_loop_err = NULL;
2999 Error *err = NULL;
3000 bool list_data_dirs = false;
3002 module_call_init(MODULE_INIT_TRACE);
3004 qemu_init_cpu_list();
3005 qemu_init_cpu_loop();
3006 qemu_mutex_lock_iothread();
3008 atexit(qemu_run_exit_notifiers);
3009 error_set_progname(argv[0]);
3010 qemu_init_exec_dir(argv[0]);
3012 module_call_init(MODULE_INIT_QOM);
3013 module_call_init(MODULE_INIT_QAPI);
3015 qemu_add_opts(&qemu_drive_opts);
3016 qemu_add_drive_opts(&qemu_legacy_drive_opts);
3017 qemu_add_drive_opts(&qemu_common_drive_opts);
3018 qemu_add_drive_opts(&qemu_drive_opts);
3019 qemu_add_drive_opts(&bdrv_runtime_opts);
3020 qemu_add_opts(&qemu_chardev_opts);
3021 qemu_add_opts(&qemu_device_opts);
3022 qemu_add_opts(&qemu_netdev_opts);
3023 qemu_add_opts(&qemu_net_opts);
3024 qemu_add_opts(&qemu_rtc_opts);
3025 qemu_add_opts(&qemu_global_opts);
3026 qemu_add_opts(&qemu_mon_opts);
3027 qemu_add_opts(&qemu_trace_opts);
3028 qemu_add_opts(&qemu_option_rom_opts);
3029 qemu_add_opts(&qemu_machine_opts);
3030 qemu_add_opts(&qemu_mem_opts);
3031 qemu_add_opts(&qemu_smp_opts);
3032 qemu_add_opts(&qemu_boot_opts);
3033 qemu_add_opts(&qemu_sandbox_opts);
3034 qemu_add_opts(&qemu_add_fd_opts);
3035 qemu_add_opts(&qemu_object_opts);
3036 qemu_add_opts(&qemu_tpmdev_opts);
3037 qemu_add_opts(&qemu_realtime_opts);
3038 qemu_add_opts(&qemu_msg_opts);
3039 qemu_add_opts(&qemu_name_opts);
3040 qemu_add_opts(&qemu_numa_opts);
3041 qemu_add_opts(&qemu_icount_opts);
3042 qemu_add_opts(&qemu_semihosting_config_opts);
3043 qemu_add_opts(&qemu_fw_cfg_opts);
3044 #ifdef CONFIG_LIBISCSI
3045 qemu_add_opts(&qemu_iscsi_opts);
3046 #endif
3047 module_call_init(MODULE_INIT_OPTS);
3049 runstate_init();
3051 if (qcrypto_init(&err) < 0) {
3052 error_reportf_err(err, "cannot initialize crypto: ");
3053 exit(1);
3055 rtc_clock = QEMU_CLOCK_HOST;
3057 QLIST_INIT (&vm_change_state_head);
3058 os_setup_early_signal_handling();
3060 cpu_model = NULL;
3061 snapshot = 0;
3062 cyls = heads = secs = 0;
3063 translation = BIOS_ATA_TRANSLATION_AUTO;
3065 nb_nics = 0;
3067 bdrv_init_with_whitelist();
3069 autostart = 1;
3071 /* first pass of option parsing */
3072 optind = 1;
3073 while (optind < argc) {
3074 if (argv[optind][0] != '-') {
3075 /* disk image */
3076 optind++;
3077 } else {
3078 const QEMUOption *popt;
3080 popt = lookup_opt(argc, argv, &optarg, &optind);
3081 switch (popt->index) {
3082 case QEMU_OPTION_nodefconfig:
3083 defconfig = false;
3084 break;
3085 case QEMU_OPTION_nouserconfig:
3086 userconfig = false;
3087 break;
3092 if (defconfig && userconfig) {
3093 if (qemu_read_default_config_file() < 0) {
3094 exit(1);
3098 /* second pass of option parsing */
3099 optind = 1;
3100 for(;;) {
3101 if (optind >= argc)
3102 break;
3103 if (argv[optind][0] != '-') {
3104 hda_opts = drive_add(IF_DEFAULT, 0, argv[optind++], HD_OPTS);
3105 } else {
3106 const QEMUOption *popt;
3108 popt = lookup_opt(argc, argv, &optarg, &optind);
3109 if (!(popt->arch_mask & arch_type)) {
3110 error_report("Option not supported for this target");
3111 exit(1);
3113 switch(popt->index) {
3114 case QEMU_OPTION_no_kvm_irqchip: {
3115 olist = qemu_find_opts("machine");
3116 qemu_opts_parse_noisily(olist, "kernel_irqchip=off", false);
3117 break;
3119 case QEMU_OPTION_cpu:
3120 /* hw initialization will check this */
3121 cpu_model = optarg;
3122 break;
3123 case QEMU_OPTION_hda:
3125 char buf[256];
3126 if (cyls == 0)
3127 snprintf(buf, sizeof(buf), "%s", HD_OPTS);
3128 else
3129 snprintf(buf, sizeof(buf),
3130 "%s,cyls=%d,heads=%d,secs=%d%s",
3131 HD_OPTS , cyls, heads, secs,
3132 translation == BIOS_ATA_TRANSLATION_LBA ?
3133 ",trans=lba" :
3134 translation == BIOS_ATA_TRANSLATION_NONE ?
3135 ",trans=none" : "");
3136 drive_add(IF_DEFAULT, 0, optarg, buf);
3137 break;
3139 case QEMU_OPTION_hdb:
3140 case QEMU_OPTION_hdc:
3141 case QEMU_OPTION_hdd:
3142 drive_add(IF_DEFAULT, popt->index - QEMU_OPTION_hda, optarg,
3143 HD_OPTS);
3144 break;
3145 case QEMU_OPTION_drive:
3146 if (drive_def(optarg) == NULL) {
3147 exit(1);
3149 break;
3150 case QEMU_OPTION_set:
3151 if (qemu_set_option(optarg) != 0)
3152 exit(1);
3153 break;
3154 case QEMU_OPTION_global:
3155 if (qemu_global_option(optarg) != 0)
3156 exit(1);
3157 break;
3158 case QEMU_OPTION_mtdblock:
3159 drive_add(IF_MTD, -1, optarg, MTD_OPTS);
3160 break;
3161 case QEMU_OPTION_sd:
3162 drive_add(IF_SD, -1, optarg, SD_OPTS);
3163 break;
3164 case QEMU_OPTION_pflash:
3165 drive_add(IF_PFLASH, -1, optarg, PFLASH_OPTS);
3166 break;
3167 case QEMU_OPTION_snapshot:
3168 snapshot = 1;
3169 break;
3170 case QEMU_OPTION_hdachs:
3172 const char *p;
3173 p = optarg;
3174 cyls = strtol(p, (char **)&p, 0);
3175 if (cyls < 1 || cyls > 16383)
3176 goto chs_fail;
3177 if (*p != ',')
3178 goto chs_fail;
3179 p++;
3180 heads = strtol(p, (char **)&p, 0);
3181 if (heads < 1 || heads > 16)
3182 goto chs_fail;
3183 if (*p != ',')
3184 goto chs_fail;
3185 p++;
3186 secs = strtol(p, (char **)&p, 0);
3187 if (secs < 1 || secs > 63)
3188 goto chs_fail;
3189 if (*p == ',') {
3190 p++;
3191 if (!strcmp(p, "large")) {
3192 translation = BIOS_ATA_TRANSLATION_LARGE;
3193 } else if (!strcmp(p, "rechs")) {
3194 translation = BIOS_ATA_TRANSLATION_RECHS;
3195 } else if (!strcmp(p, "none")) {
3196 translation = BIOS_ATA_TRANSLATION_NONE;
3197 } else if (!strcmp(p, "lba")) {
3198 translation = BIOS_ATA_TRANSLATION_LBA;
3199 } else if (!strcmp(p, "auto")) {
3200 translation = BIOS_ATA_TRANSLATION_AUTO;
3201 } else {
3202 goto chs_fail;
3204 } else if (*p != '\0') {
3205 chs_fail:
3206 error_report("invalid physical CHS format");
3207 exit(1);
3209 if (hda_opts != NULL) {
3210 qemu_opt_set_number(hda_opts, "cyls", cyls,
3211 &error_abort);
3212 qemu_opt_set_number(hda_opts, "heads", heads,
3213 &error_abort);
3214 qemu_opt_set_number(hda_opts, "secs", secs,
3215 &error_abort);
3216 if (translation == BIOS_ATA_TRANSLATION_LARGE) {
3217 qemu_opt_set(hda_opts, "trans", "large",
3218 &error_abort);
3219 } else if (translation == BIOS_ATA_TRANSLATION_RECHS) {
3220 qemu_opt_set(hda_opts, "trans", "rechs",
3221 &error_abort);
3222 } else if (translation == BIOS_ATA_TRANSLATION_LBA) {
3223 qemu_opt_set(hda_opts, "trans", "lba",
3224 &error_abort);
3225 } else if (translation == BIOS_ATA_TRANSLATION_NONE) {
3226 qemu_opt_set(hda_opts, "trans", "none",
3227 &error_abort);
3231 break;
3232 case QEMU_OPTION_numa:
3233 opts = qemu_opts_parse_noisily(qemu_find_opts("numa"),
3234 optarg, true);
3235 if (!opts) {
3236 exit(1);
3238 break;
3239 case QEMU_OPTION_display:
3240 display_type = select_display(optarg);
3241 break;
3242 case QEMU_OPTION_nographic:
3243 olist = qemu_find_opts("machine");
3244 qemu_opts_parse_noisily(olist, "graphics=off", false);
3245 nographic = true;
3246 display_type = DT_NONE;
3247 break;
3248 case QEMU_OPTION_curses:
3249 #ifdef CONFIG_CURSES
3250 display_type = DT_CURSES;
3251 #else
3252 error_report("curses support is disabled");
3253 exit(1);
3254 #endif
3255 break;
3256 case QEMU_OPTION_portrait:
3257 graphic_rotate = 90;
3258 break;
3259 case QEMU_OPTION_rotate:
3260 graphic_rotate = strtol(optarg, (char **) &optarg, 10);
3261 if (graphic_rotate != 0 && graphic_rotate != 90 &&
3262 graphic_rotate != 180 && graphic_rotate != 270) {
3263 error_report("only 90, 180, 270 deg rotation is available");
3264 exit(1);
3266 break;
3267 case QEMU_OPTION_kernel:
3268 qemu_opts_set(qemu_find_opts("machine"), 0, "kernel", optarg,
3269 &error_abort);
3270 break;
3271 case QEMU_OPTION_initrd:
3272 qemu_opts_set(qemu_find_opts("machine"), 0, "initrd", optarg,
3273 &error_abort);
3274 break;
3275 case QEMU_OPTION_append:
3276 qemu_opts_set(qemu_find_opts("machine"), 0, "append", optarg,
3277 &error_abort);
3278 break;
3279 case QEMU_OPTION_dtb:
3280 qemu_opts_set(qemu_find_opts("machine"), 0, "dtb", optarg,
3281 &error_abort);
3282 break;
3283 case QEMU_OPTION_cdrom:
3284 drive_add(IF_DEFAULT, 2, optarg, CDROM_OPTS);
3285 break;
3286 case QEMU_OPTION_boot:
3287 opts = qemu_opts_parse_noisily(qemu_find_opts("boot-opts"),
3288 optarg, true);
3289 if (!opts) {
3290 exit(1);
3292 break;
3293 case QEMU_OPTION_fda:
3294 case QEMU_OPTION_fdb:
3295 drive_add(IF_FLOPPY, popt->index - QEMU_OPTION_fda,
3296 optarg, FD_OPTS);
3297 break;
3298 case QEMU_OPTION_no_fd_bootchk:
3299 fd_bootchk = 0;
3300 break;
3301 case QEMU_OPTION_netdev:
3302 default_net = 0;
3303 if (net_client_parse(qemu_find_opts("netdev"), optarg) == -1) {
3304 exit(1);
3306 break;
3307 case QEMU_OPTION_net:
3308 default_net = 0;
3309 if (net_client_parse(qemu_find_opts("net"), optarg) == -1) {
3310 exit(1);
3312 break;
3313 #ifdef CONFIG_LIBISCSI
3314 case QEMU_OPTION_iscsi:
3315 opts = qemu_opts_parse_noisily(qemu_find_opts("iscsi"),
3316 optarg, false);
3317 if (!opts) {
3318 exit(1);
3320 break;
3321 #endif
3322 #ifdef CONFIG_SLIRP
3323 case QEMU_OPTION_tftp:
3324 error_report("The -tftp option is deprecated. "
3325 "Please use '-netdev user,tftp=...' instead.");
3326 legacy_tftp_prefix = optarg;
3327 break;
3328 case QEMU_OPTION_bootp:
3329 error_report("The -bootp option is deprecated. "
3330 "Please use '-netdev user,bootfile=...' instead.");
3331 legacy_bootp_filename = optarg;
3332 break;
3333 case QEMU_OPTION_redir:
3334 error_report("The -redir option is deprecated. "
3335 "Please use '-netdev user,hostfwd=...' instead.");
3336 if (net_slirp_redir(optarg) < 0)
3337 exit(1);
3338 break;
3339 #endif
3340 case QEMU_OPTION_bt:
3341 add_device_config(DEV_BT, optarg);
3342 break;
3343 case QEMU_OPTION_audio_help:
3344 AUD_help ();
3345 exit (0);
3346 break;
3347 case QEMU_OPTION_soundhw:
3348 select_soundhw (optarg);
3349 break;
3350 case QEMU_OPTION_h:
3351 help(0);
3352 break;
3353 case QEMU_OPTION_version:
3354 version();
3355 exit(0);
3356 break;
3357 case QEMU_OPTION_m:
3358 opts = qemu_opts_parse_noisily(qemu_find_opts("memory"),
3359 optarg, true);
3360 if (!opts) {
3361 exit(EXIT_FAILURE);
3363 break;
3364 #ifdef CONFIG_TPM
3365 case QEMU_OPTION_tpmdev:
3366 if (tpm_config_parse(qemu_find_opts("tpmdev"), optarg) < 0) {
3367 exit(1);
3369 break;
3370 #endif
3371 case QEMU_OPTION_mempath:
3372 mem_path = optarg;
3373 break;
3374 case QEMU_OPTION_mem_prealloc:
3375 mem_prealloc = 1;
3376 break;
3377 case QEMU_OPTION_d:
3378 log_mask = optarg;
3379 break;
3380 case QEMU_OPTION_D:
3381 log_file = optarg;
3382 break;
3383 case QEMU_OPTION_DFILTER:
3384 qemu_set_dfilter_ranges(optarg, &error_fatal);
3385 break;
3386 case QEMU_OPTION_s:
3387 add_device_config(DEV_GDB, "tcp::" DEFAULT_GDBSTUB_PORT);
3388 break;
3389 case QEMU_OPTION_gdb:
3390 add_device_config(DEV_GDB, optarg);
3391 break;
3392 case QEMU_OPTION_L:
3393 if (is_help_option(optarg)) {
3394 list_data_dirs = true;
3395 } else if (data_dir_idx < ARRAY_SIZE(data_dir)) {
3396 data_dir[data_dir_idx++] = optarg;
3398 break;
3399 case QEMU_OPTION_bios:
3400 qemu_opts_set(qemu_find_opts("machine"), 0, "firmware", optarg,
3401 &error_abort);
3402 break;
3403 case QEMU_OPTION_singlestep:
3404 singlestep = 1;
3405 break;
3406 case QEMU_OPTION_S:
3407 autostart = 0;
3408 break;
3409 case QEMU_OPTION_k:
3410 keyboard_layout = optarg;
3411 break;
3412 case QEMU_OPTION_localtime:
3413 rtc_utc = 0;
3414 break;
3415 case QEMU_OPTION_vga:
3416 vga_model = optarg;
3417 default_vga = 0;
3418 break;
3419 case QEMU_OPTION_g:
3421 const char *p;
3422 int w, h, depth;
3423 p = optarg;
3424 w = strtol(p, (char **)&p, 10);
3425 if (w <= 0) {
3426 graphic_error:
3427 error_report("invalid resolution or depth");
3428 exit(1);
3430 if (*p != 'x')
3431 goto graphic_error;
3432 p++;
3433 h = strtol(p, (char **)&p, 10);
3434 if (h <= 0)
3435 goto graphic_error;
3436 if (*p == 'x') {
3437 p++;
3438 depth = strtol(p, (char **)&p, 10);
3439 if (depth != 8 && depth != 15 && depth != 16 &&
3440 depth != 24 && depth != 32)
3441 goto graphic_error;
3442 } else if (*p == '\0') {
3443 depth = graphic_depth;
3444 } else {
3445 goto graphic_error;
3448 graphic_width = w;
3449 graphic_height = h;
3450 graphic_depth = depth;
3452 break;
3453 case QEMU_OPTION_echr:
3455 char *r;
3456 term_escape_char = strtol(optarg, &r, 0);
3457 if (r == optarg)
3458 printf("Bad argument to echr\n");
3459 break;
3461 case QEMU_OPTION_monitor:
3462 default_monitor = 0;
3463 if (strncmp(optarg, "none", 4)) {
3464 monitor_parse(optarg, "readline", false);
3466 break;
3467 case QEMU_OPTION_qmp:
3468 monitor_parse(optarg, "control", false);
3469 default_monitor = 0;
3470 break;
3471 case QEMU_OPTION_qmp_pretty:
3472 monitor_parse(optarg, "control", true);
3473 default_monitor = 0;
3474 break;
3475 case QEMU_OPTION_mon:
3476 opts = qemu_opts_parse_noisily(qemu_find_opts("mon"), optarg,
3477 true);
3478 if (!opts) {
3479 exit(1);
3481 default_monitor = 0;
3482 break;
3483 case QEMU_OPTION_chardev:
3484 opts = qemu_opts_parse_noisily(qemu_find_opts("chardev"),
3485 optarg, true);
3486 if (!opts) {
3487 exit(1);
3489 break;
3490 case QEMU_OPTION_fsdev:
3491 olist = qemu_find_opts("fsdev");
3492 if (!olist) {
3493 error_report("fsdev support is disabled");
3494 exit(1);
3496 opts = qemu_opts_parse_noisily(olist, optarg, true);
3497 if (!opts) {
3498 exit(1);
3500 break;
3501 case QEMU_OPTION_virtfs: {
3502 QemuOpts *fsdev;
3503 QemuOpts *device;
3504 const char *writeout, *sock_fd, *socket;
3506 olist = qemu_find_opts("virtfs");
3507 if (!olist) {
3508 error_report("virtfs support is disabled");
3509 exit(1);
3511 opts = qemu_opts_parse_noisily(olist, optarg, true);
3512 if (!opts) {
3513 exit(1);
3516 if (qemu_opt_get(opts, "fsdriver") == NULL ||
3517 qemu_opt_get(opts, "mount_tag") == NULL) {
3518 error_report("Usage: -virtfs fsdriver,mount_tag=tag");
3519 exit(1);
3521 fsdev = qemu_opts_create(qemu_find_opts("fsdev"),
3522 qemu_opt_get(opts, "mount_tag"),
3523 1, NULL);
3524 if (!fsdev) {
3525 error_report("duplicate fsdev id: %s",
3526 qemu_opt_get(opts, "mount_tag"));
3527 exit(1);
3530 writeout = qemu_opt_get(opts, "writeout");
3531 if (writeout) {
3532 #ifdef CONFIG_SYNC_FILE_RANGE
3533 qemu_opt_set(fsdev, "writeout", writeout, &error_abort);
3534 #else
3535 error_report("writeout=immediate not supported "
3536 "on this platform");
3537 exit(1);
3538 #endif
3540 qemu_opt_set(fsdev, "fsdriver",
3541 qemu_opt_get(opts, "fsdriver"), &error_abort);
3542 qemu_opt_set(fsdev, "path", qemu_opt_get(opts, "path"),
3543 &error_abort);
3544 qemu_opt_set(fsdev, "security_model",
3545 qemu_opt_get(opts, "security_model"),
3546 &error_abort);
3547 socket = qemu_opt_get(opts, "socket");
3548 if (socket) {
3549 qemu_opt_set(fsdev, "socket", socket, &error_abort);
3551 sock_fd = qemu_opt_get(opts, "sock_fd");
3552 if (sock_fd) {
3553 qemu_opt_set(fsdev, "sock_fd", sock_fd, &error_abort);
3556 qemu_opt_set_bool(fsdev, "readonly",
3557 qemu_opt_get_bool(opts, "readonly", 0),
3558 &error_abort);
3559 device = qemu_opts_create(qemu_find_opts("device"), NULL, 0,
3560 &error_abort);
3561 qemu_opt_set(device, "driver", "virtio-9p-pci", &error_abort);
3562 qemu_opt_set(device, "fsdev",
3563 qemu_opt_get(opts, "mount_tag"), &error_abort);
3564 qemu_opt_set(device, "mount_tag",
3565 qemu_opt_get(opts, "mount_tag"), &error_abort);
3566 break;
3568 case QEMU_OPTION_virtfs_synth: {
3569 QemuOpts *fsdev;
3570 QemuOpts *device;
3572 fsdev = qemu_opts_create(qemu_find_opts("fsdev"), "v_synth",
3573 1, NULL);
3574 if (!fsdev) {
3575 error_report("duplicate option: %s", "virtfs_synth");
3576 exit(1);
3578 qemu_opt_set(fsdev, "fsdriver", "synth", &error_abort);
3580 device = qemu_opts_create(qemu_find_opts("device"), NULL, 0,
3581 &error_abort);
3582 qemu_opt_set(device, "driver", "virtio-9p-pci", &error_abort);
3583 qemu_opt_set(device, "fsdev", "v_synth", &error_abort);
3584 qemu_opt_set(device, "mount_tag", "v_synth", &error_abort);
3585 break;
3587 case QEMU_OPTION_serial:
3588 add_device_config(DEV_SERIAL, optarg);
3589 default_serial = 0;
3590 if (strncmp(optarg, "mon:", 4) == 0) {
3591 default_monitor = 0;
3593 break;
3594 case QEMU_OPTION_watchdog:
3595 if (watchdog) {
3596 error_report("only one watchdog option may be given");
3597 return 1;
3599 watchdog = optarg;
3600 break;
3601 case QEMU_OPTION_watchdog_action:
3602 if (select_watchdog_action(optarg) == -1) {
3603 error_report("unknown -watchdog-action parameter");
3604 exit(1);
3606 break;
3607 case QEMU_OPTION_virtiocon:
3608 add_device_config(DEV_VIRTCON, optarg);
3609 default_virtcon = 0;
3610 if (strncmp(optarg, "mon:", 4) == 0) {
3611 default_monitor = 0;
3613 break;
3614 case QEMU_OPTION_parallel:
3615 add_device_config(DEV_PARALLEL, optarg);
3616 default_parallel = 0;
3617 if (strncmp(optarg, "mon:", 4) == 0) {
3618 default_monitor = 0;
3620 break;
3621 case QEMU_OPTION_debugcon:
3622 add_device_config(DEV_DEBUGCON, optarg);
3623 break;
3624 case QEMU_OPTION_loadvm:
3625 loadvm = optarg;
3626 break;
3627 case QEMU_OPTION_full_screen:
3628 full_screen = 1;
3629 break;
3630 case QEMU_OPTION_no_frame:
3631 no_frame = 1;
3632 break;
3633 case QEMU_OPTION_alt_grab:
3634 alt_grab = 1;
3635 break;
3636 case QEMU_OPTION_ctrl_grab:
3637 ctrl_grab = 1;
3638 break;
3639 case QEMU_OPTION_no_quit:
3640 no_quit = 1;
3641 break;
3642 case QEMU_OPTION_sdl:
3643 #ifdef CONFIG_SDL
3644 display_type = DT_SDL;
3645 break;
3646 #else
3647 error_report("SDL support is disabled");
3648 exit(1);
3649 #endif
3650 case QEMU_OPTION_pidfile:
3651 pid_file = optarg;
3652 break;
3653 case QEMU_OPTION_win2k_hack:
3654 win2k_install_hack = 1;
3655 break;
3656 case QEMU_OPTION_rtc_td_hack: {
3657 static GlobalProperty slew_lost_ticks = {
3658 .driver = "mc146818rtc",
3659 .property = "lost_tick_policy",
3660 .value = "slew",
3663 qdev_prop_register_global(&slew_lost_ticks);
3664 break;
3666 case QEMU_OPTION_acpitable:
3667 opts = qemu_opts_parse_noisily(qemu_find_opts("acpi"),
3668 optarg, true);
3669 if (!opts) {
3670 exit(1);
3672 acpi_table_add(opts, &error_fatal);
3673 break;
3674 case QEMU_OPTION_smbios:
3675 opts = qemu_opts_parse_noisily(qemu_find_opts("smbios"),
3676 optarg, false);
3677 if (!opts) {
3678 exit(1);
3680 smbios_entry_add(opts, &error_fatal);
3681 break;
3682 case QEMU_OPTION_fwcfg:
3683 opts = qemu_opts_parse_noisily(qemu_find_opts("fw_cfg"),
3684 optarg, true);
3685 if (opts == NULL) {
3686 exit(1);
3688 break;
3689 case QEMU_OPTION_enable_kvm:
3690 olist = qemu_find_opts("machine");
3691 qemu_opts_parse_noisily(olist, "accel=kvm", false);
3692 break;
3693 case QEMU_OPTION_enable_hax:
3694 olist = qemu_find_opts("machine");
3695 qemu_opts_parse_noisily(olist, "accel=hax", false);
3696 break;
3697 case QEMU_OPTION_M:
3698 case QEMU_OPTION_machine:
3699 olist = qemu_find_opts("machine");
3700 opts = qemu_opts_parse_noisily(olist, optarg, true);
3701 if (!opts) {
3702 exit(1);
3704 break;
3705 case QEMU_OPTION_no_kvm:
3706 olist = qemu_find_opts("machine");
3707 qemu_opts_parse_noisily(olist, "accel=tcg", false);
3708 break;
3709 case QEMU_OPTION_no_kvm_pit: {
3710 error_report("warning: ignoring deprecated option");
3711 break;
3713 case QEMU_OPTION_no_kvm_pit_reinjection: {
3714 static GlobalProperty kvm_pit_lost_tick_policy = {
3715 .driver = "kvm-pit",
3716 .property = "lost_tick_policy",
3717 .value = "discard",
3720 error_report("warning: deprecated, replaced by "
3721 "-global kvm-pit.lost_tick_policy=discard");
3722 qdev_prop_register_global(&kvm_pit_lost_tick_policy);
3723 break;
3725 case QEMU_OPTION_usb:
3726 olist = qemu_find_opts("machine");
3727 qemu_opts_parse_noisily(olist, "usb=on", false);
3728 break;
3729 case QEMU_OPTION_usbdevice:
3730 olist = qemu_find_opts("machine");
3731 qemu_opts_parse_noisily(olist, "usb=on", false);
3732 add_device_config(DEV_USB, optarg);
3733 break;
3734 case QEMU_OPTION_device:
3735 if (!qemu_opts_parse_noisily(qemu_find_opts("device"),
3736 optarg, true)) {
3737 exit(1);
3739 break;
3740 case QEMU_OPTION_smp:
3741 if (!qemu_opts_parse_noisily(qemu_find_opts("smp-opts"),
3742 optarg, true)) {
3743 exit(1);
3745 break;
3746 case QEMU_OPTION_vnc:
3747 vnc_parse(optarg, &error_fatal);
3748 break;
3749 case QEMU_OPTION_no_acpi:
3750 acpi_enabled = 0;
3751 break;
3752 case QEMU_OPTION_no_hpet:
3753 no_hpet = 1;
3754 break;
3755 case QEMU_OPTION_balloon:
3756 if (balloon_parse(optarg) < 0) {
3757 error_report("unknown -balloon argument %s", optarg);
3758 exit(1);
3760 break;
3761 case QEMU_OPTION_no_reboot:
3762 no_reboot = 1;
3763 break;
3764 case QEMU_OPTION_no_shutdown:
3765 no_shutdown = 1;
3766 break;
3767 case QEMU_OPTION_show_cursor:
3768 cursor_hide = 0;
3769 break;
3770 case QEMU_OPTION_uuid:
3771 if (qemu_uuid_parse(optarg, &qemu_uuid) < 0) {
3772 error_report("failed to parse UUID string: wrong format");
3773 exit(1);
3775 qemu_uuid_set = true;
3776 break;
3777 case QEMU_OPTION_option_rom:
3778 if (nb_option_roms >= MAX_OPTION_ROMS) {
3779 error_report("too many option ROMs");
3780 exit(1);
3782 opts = qemu_opts_parse_noisily(qemu_find_opts("option-rom"),
3783 optarg, true);
3784 if (!opts) {
3785 exit(1);
3787 option_rom[nb_option_roms].name = qemu_opt_get(opts, "romfile");
3788 option_rom[nb_option_roms].bootindex =
3789 qemu_opt_get_number(opts, "bootindex", -1);
3790 if (!option_rom[nb_option_roms].name) {
3791 error_report("Option ROM file is not specified");
3792 exit(1);
3794 nb_option_roms++;
3795 break;
3796 case QEMU_OPTION_semihosting:
3797 semihosting.enabled = true;
3798 semihosting.target = SEMIHOSTING_TARGET_AUTO;
3799 break;
3800 case QEMU_OPTION_semihosting_config:
3801 semihosting.enabled = true;
3802 opts = qemu_opts_parse_noisily(qemu_find_opts("semihosting-config"),
3803 optarg, false);
3804 if (opts != NULL) {
3805 semihosting.enabled = qemu_opt_get_bool(opts, "enable",
3806 true);
3807 const char *target = qemu_opt_get(opts, "target");
3808 if (target != NULL) {
3809 if (strcmp("native", target) == 0) {
3810 semihosting.target = SEMIHOSTING_TARGET_NATIVE;
3811 } else if (strcmp("gdb", target) == 0) {
3812 semihosting.target = SEMIHOSTING_TARGET_GDB;
3813 } else if (strcmp("auto", target) == 0) {
3814 semihosting.target = SEMIHOSTING_TARGET_AUTO;
3815 } else {
3816 error_report("unsupported semihosting-config %s",
3817 optarg);
3818 exit(1);
3820 } else {
3821 semihosting.target = SEMIHOSTING_TARGET_AUTO;
3823 /* Set semihosting argument count and vector */
3824 qemu_opt_foreach(opts, add_semihosting_arg,
3825 &semihosting, NULL);
3826 } else {
3827 error_report("unsupported semihosting-config %s", optarg);
3828 exit(1);
3830 break;
3831 case QEMU_OPTION_tdf:
3832 error_report("warning: ignoring deprecated option");
3833 break;
3834 case QEMU_OPTION_name:
3835 opts = qemu_opts_parse_noisily(qemu_find_opts("name"),
3836 optarg, true);
3837 if (!opts) {
3838 exit(1);
3840 break;
3841 case QEMU_OPTION_prom_env:
3842 if (nb_prom_envs >= MAX_PROM_ENVS) {
3843 error_report("too many prom variables");
3844 exit(1);
3846 prom_envs[nb_prom_envs] = optarg;
3847 nb_prom_envs++;
3848 break;
3849 case QEMU_OPTION_old_param:
3850 old_param = 1;
3851 break;
3852 case QEMU_OPTION_clock:
3853 /* Clock options no longer exist. Keep this option for
3854 * backward compatibility.
3856 break;
3857 case QEMU_OPTION_startdate:
3858 configure_rtc_date_offset(optarg, 1);
3859 break;
3860 case QEMU_OPTION_rtc:
3861 opts = qemu_opts_parse_noisily(qemu_find_opts("rtc"), optarg,
3862 false);
3863 if (!opts) {
3864 exit(1);
3866 configure_rtc(opts);
3867 break;
3868 case QEMU_OPTION_tb_size:
3869 tcg_tb_size = strtol(optarg, NULL, 0);
3870 if (tcg_tb_size < 0) {
3871 tcg_tb_size = 0;
3873 break;
3874 case QEMU_OPTION_icount:
3875 icount_opts = qemu_opts_parse_noisily(qemu_find_opts("icount"),
3876 optarg, true);
3877 if (!icount_opts) {
3878 exit(1);
3880 break;
3881 case QEMU_OPTION_incoming:
3882 if (!incoming) {
3883 runstate_set(RUN_STATE_INMIGRATE);
3885 incoming = optarg;
3886 break;
3887 case QEMU_OPTION_nodefaults:
3888 has_defaults = 0;
3889 break;
3890 case QEMU_OPTION_xen_domid:
3891 if (!(xen_available())) {
3892 error_report("Option not supported for this target");
3893 exit(1);
3895 xen_domid = atoi(optarg);
3896 break;
3897 case QEMU_OPTION_xen_create:
3898 if (!(xen_available())) {
3899 error_report("Option not supported for this target");
3900 exit(1);
3902 xen_mode = XEN_CREATE;
3903 break;
3904 case QEMU_OPTION_xen_attach:
3905 if (!(xen_available())) {
3906 error_report("Option not supported for this target");
3907 exit(1);
3909 xen_mode = XEN_ATTACH;
3910 break;
3911 case QEMU_OPTION_trace:
3912 g_free(trace_file);
3913 trace_file = trace_opt_parse(optarg);
3914 break;
3915 case QEMU_OPTION_readconfig:
3917 int ret = qemu_read_config_file(optarg);
3918 if (ret < 0) {
3919 error_report("read config %s: %s", optarg,
3920 strerror(-ret));
3921 exit(1);
3923 break;
3925 case QEMU_OPTION_spice:
3926 olist = qemu_find_opts("spice");
3927 if (!olist) {
3928 error_report("spice support is disabled");
3929 exit(1);
3931 opts = qemu_opts_parse_noisily(olist, optarg, false);
3932 if (!opts) {
3933 exit(1);
3935 display_remote++;
3936 break;
3937 case QEMU_OPTION_writeconfig:
3939 FILE *fp;
3940 if (strcmp(optarg, "-") == 0) {
3941 fp = stdout;
3942 } else {
3943 fp = fopen(optarg, "w");
3944 if (fp == NULL) {
3945 error_report("open %s: %s", optarg,
3946 strerror(errno));
3947 exit(1);
3950 qemu_config_write(fp);
3951 if (fp != stdout) {
3952 fclose(fp);
3954 break;
3956 case QEMU_OPTION_qtest:
3957 qtest_chrdev = optarg;
3958 break;
3959 case QEMU_OPTION_qtest_log:
3960 qtest_log = optarg;
3961 break;
3962 case QEMU_OPTION_sandbox:
3963 opts = qemu_opts_parse_noisily(qemu_find_opts("sandbox"),
3964 optarg, true);
3965 if (!opts) {
3966 exit(1);
3968 break;
3969 case QEMU_OPTION_add_fd:
3970 #ifndef _WIN32
3971 opts = qemu_opts_parse_noisily(qemu_find_opts("add-fd"),
3972 optarg, false);
3973 if (!opts) {
3974 exit(1);
3976 #else
3977 error_report("File descriptor passing is disabled on this "
3978 "platform");
3979 exit(1);
3980 #endif
3981 break;
3982 case QEMU_OPTION_object:
3983 opts = qemu_opts_parse_noisily(qemu_find_opts("object"),
3984 optarg, true);
3985 if (!opts) {
3986 exit(1);
3988 break;
3989 case QEMU_OPTION_realtime:
3990 opts = qemu_opts_parse_noisily(qemu_find_opts("realtime"),
3991 optarg, false);
3992 if (!opts) {
3993 exit(1);
3995 enable_mlock = qemu_opt_get_bool(opts, "mlock", true);
3996 break;
3997 case QEMU_OPTION_msg:
3998 opts = qemu_opts_parse_noisily(qemu_find_opts("msg"), optarg,
3999 false);
4000 if (!opts) {
4001 exit(1);
4003 configure_msg(opts);
4004 break;
4005 case QEMU_OPTION_dump_vmstate:
4006 if (vmstate_dump_file) {
4007 error_report("only one '-dump-vmstate' "
4008 "option may be given");
4009 exit(1);
4011 vmstate_dump_file = fopen(optarg, "w");
4012 if (vmstate_dump_file == NULL) {
4013 error_report("open %s: %s", optarg, strerror(errno));
4014 exit(1);
4016 break;
4017 default:
4018 os_parse_cmd_args(popt->index, optarg);
4023 * Clear error location left behind by the loop.
4024 * Best done right after the loop. Do not insert code here!
4026 loc_set_none();
4028 replay_configure(icount_opts);
4030 machine_class = select_machine();
4032 set_memory_options(&ram_slots, &maxram_size, machine_class);
4034 os_daemonize();
4036 if (pid_file && qemu_create_pidfile(pid_file) != 0) {
4037 error_report("could not acquire pid file: %s", strerror(errno));
4038 exit(1);
4041 if (qemu_init_main_loop(&main_loop_err)) {
4042 error_report_err(main_loop_err);
4043 exit(1);
4046 if (qemu_opts_foreach(qemu_find_opts("sandbox"),
4047 parse_sandbox, NULL, NULL)) {
4048 exit(1);
4051 if (qemu_opts_foreach(qemu_find_opts("name"),
4052 parse_name, NULL, NULL)) {
4053 exit(1);
4056 #ifndef _WIN32
4057 if (qemu_opts_foreach(qemu_find_opts("add-fd"),
4058 parse_add_fd, NULL, NULL)) {
4059 exit(1);
4062 if (qemu_opts_foreach(qemu_find_opts("add-fd"),
4063 cleanup_add_fd, NULL, NULL)) {
4064 exit(1);
4066 #endif
4068 current_machine = MACHINE(object_new(object_class_get_name(
4069 OBJECT_CLASS(machine_class))));
4070 if (machine_help_func(qemu_get_machine_opts(), current_machine)) {
4071 exit(0);
4073 object_property_add_child(object_get_root(), "machine",
4074 OBJECT(current_machine), &error_abort);
4076 if (machine_class->minimum_page_bits) {
4077 if (!set_preferred_target_page_bits(machine_class->minimum_page_bits)) {
4078 /* This would be a board error: specifying a minimum smaller than
4079 * a target's compile-time fixed setting.
4081 g_assert_not_reached();
4085 cpu_exec_init_all();
4087 if (machine_class->hw_version) {
4088 qemu_set_hw_version(machine_class->hw_version);
4091 if (cpu_model && is_help_option(cpu_model)) {
4092 list_cpus(stdout, &fprintf, cpu_model);
4093 exit(0);
4096 if (!trace_init_backends()) {
4097 exit(1);
4099 trace_init_file(trace_file);
4101 /* Open the logfile at this point and set the log mask if necessary.
4103 if (log_file) {
4104 qemu_set_log_filename(log_file, &error_fatal);
4107 if (log_mask) {
4108 int mask;
4109 mask = qemu_str_to_log_mask(log_mask);
4110 if (!mask) {
4111 qemu_print_log_usage(stdout);
4112 exit(1);
4114 qemu_set_log(mask);
4115 } else {
4116 qemu_set_log(0);
4119 /* If no data_dir is specified then try to find it relative to the
4120 executable path. */
4121 if (data_dir_idx < ARRAY_SIZE(data_dir)) {
4122 data_dir[data_dir_idx] = os_find_datadir();
4123 if (data_dir[data_dir_idx] != NULL) {
4124 data_dir_idx++;
4127 /* If all else fails use the install path specified when building. */
4128 if (data_dir_idx < ARRAY_SIZE(data_dir)) {
4129 data_dir[data_dir_idx++] = CONFIG_QEMU_DATADIR;
4132 /* -L help lists the data directories and exits. */
4133 if (list_data_dirs) {
4134 for (i = 0; i < data_dir_idx; i++) {
4135 printf("%s\n", data_dir[i]);
4137 exit(0);
4140 smp_parse(qemu_opts_find(qemu_find_opts("smp-opts"), NULL));
4142 machine_class->max_cpus = machine_class->max_cpus ?: 1; /* Default to UP */
4143 if (max_cpus > machine_class->max_cpus) {
4144 error_report("Number of SMP CPUs requested (%d) exceeds max CPUs "
4145 "supported by machine '%s' (%d)", max_cpus,
4146 machine_class->name, machine_class->max_cpus);
4147 exit(1);
4151 * Get the default machine options from the machine if it is not already
4152 * specified either by the configuration file or by the command line.
4154 if (machine_class->default_machine_opts) {
4155 qemu_opts_set_defaults(qemu_find_opts("machine"),
4156 machine_class->default_machine_opts, 0);
4159 qemu_opts_foreach(qemu_find_opts("device"),
4160 default_driver_check, NULL, NULL);
4161 qemu_opts_foreach(qemu_find_opts("global"),
4162 default_driver_check, NULL, NULL);
4164 if (!vga_model && !default_vga) {
4165 vga_interface_type = VGA_DEVICE;
4167 if (!has_defaults || machine_class->no_serial) {
4168 default_serial = 0;
4170 if (!has_defaults || machine_class->no_parallel) {
4171 default_parallel = 0;
4173 if (!has_defaults || !machine_class->use_virtcon) {
4174 default_virtcon = 0;
4176 if (!has_defaults || !machine_class->use_sclp) {
4177 default_sclp = 0;
4179 if (!has_defaults || machine_class->no_floppy) {
4180 default_floppy = 0;
4182 if (!has_defaults || machine_class->no_cdrom) {
4183 default_cdrom = 0;
4185 if (!has_defaults || machine_class->no_sdcard) {
4186 default_sdcard = 0;
4188 if (!has_defaults) {
4189 default_monitor = 0;
4190 default_net = 0;
4191 default_vga = 0;
4194 if (is_daemonized()) {
4195 /* According to documentation and historically, -nographic redirects
4196 * serial port, parallel port and monitor to stdio, which does not work
4197 * with -daemonize. We can redirect these to null instead, but since
4198 * -nographic is legacy, let's just error out.
4199 * We disallow -nographic only if all other ports are not redirected
4200 * explicitly, to not break existing legacy setups which uses
4201 * -nographic _and_ redirects all ports explicitly - this is valid
4202 * usage, -nographic is just a no-op in this case.
4204 if (nographic
4205 && (default_parallel || default_serial
4206 || default_monitor || default_virtcon)) {
4207 error_report("-nographic cannot be used with -daemonize");
4208 exit(1);
4210 #ifdef CONFIG_CURSES
4211 if (display_type == DT_CURSES) {
4212 error_report("curses display cannot be used with -daemonize");
4213 exit(1);
4215 #endif
4218 if (nographic) {
4219 if (default_parallel)
4220 add_device_config(DEV_PARALLEL, "null");
4221 if (default_serial && default_monitor) {
4222 add_device_config(DEV_SERIAL, "mon:stdio");
4223 } else if (default_virtcon && default_monitor) {
4224 add_device_config(DEV_VIRTCON, "mon:stdio");
4225 } else if (default_sclp && default_monitor) {
4226 add_device_config(DEV_SCLP, "mon:stdio");
4227 } else {
4228 if (default_serial)
4229 add_device_config(DEV_SERIAL, "stdio");
4230 if (default_virtcon)
4231 add_device_config(DEV_VIRTCON, "stdio");
4232 if (default_sclp) {
4233 add_device_config(DEV_SCLP, "stdio");
4235 if (default_monitor)
4236 monitor_parse("stdio", "readline", false);
4238 } else {
4239 if (default_serial)
4240 add_device_config(DEV_SERIAL, "vc:80Cx24C");
4241 if (default_parallel)
4242 add_device_config(DEV_PARALLEL, "vc:80Cx24C");
4243 if (default_monitor)
4244 monitor_parse("vc:80Cx24C", "readline", false);
4245 if (default_virtcon)
4246 add_device_config(DEV_VIRTCON, "vc:80Cx24C");
4247 if (default_sclp) {
4248 add_device_config(DEV_SCLP, "vc:80Cx24C");
4252 #if defined(CONFIG_VNC)
4253 if (!QTAILQ_EMPTY(&(qemu_find_opts("vnc")->head))) {
4254 display_remote++;
4256 #endif
4257 if (display_type == DT_DEFAULT && !display_remote) {
4258 #if defined(CONFIG_GTK)
4259 display_type = DT_GTK;
4260 #elif defined(CONFIG_SDL)
4261 display_type = DT_SDL;
4262 #elif defined(CONFIG_COCOA)
4263 display_type = DT_COCOA;
4264 #elif defined(CONFIG_VNC)
4265 vnc_parse("localhost:0,to=99,id=default", &error_abort);
4266 #else
4267 display_type = DT_NONE;
4268 #endif
4271 if ((no_frame || alt_grab || ctrl_grab) && display_type != DT_SDL) {
4272 error_report("-no-frame, -alt-grab and -ctrl-grab are only valid "
4273 "for SDL, ignoring option");
4275 if (no_quit && (display_type != DT_GTK && display_type != DT_SDL)) {
4276 error_report("-no-quit is only valid for GTK and SDL, "
4277 "ignoring option");
4280 if (display_type == DT_GTK) {
4281 early_gtk_display_init(request_opengl);
4284 if (display_type == DT_SDL) {
4285 sdl_display_early_init(request_opengl);
4288 if (request_opengl == 1 && display_opengl == 0) {
4289 #if defined(CONFIG_OPENGL)
4290 error_report("OpenGL is not supported by the display");
4291 #else
4292 error_report("OpenGL support is disabled");
4293 #endif
4294 exit(1);
4297 page_size_init();
4298 socket_init();
4300 if (qemu_opts_foreach(qemu_find_opts("object"),
4301 user_creatable_add_opts_foreach,
4302 object_create_initial, NULL)) {
4303 exit(1);
4306 if (qemu_opts_foreach(qemu_find_opts("chardev"),
4307 chardev_init_func, NULL, NULL)) {
4308 exit(1);
4311 #ifdef CONFIG_VIRTFS
4312 if (qemu_opts_foreach(qemu_find_opts("fsdev"),
4313 fsdev_init_func, NULL, NULL)) {
4314 exit(1);
4316 #endif
4318 if (qemu_opts_foreach(qemu_find_opts("device"),
4319 device_help_func, NULL, NULL)) {
4320 exit(0);
4323 machine_opts = qemu_get_machine_opts();
4324 if (qemu_opt_foreach(machine_opts, machine_set_property, current_machine,
4325 NULL)) {
4326 object_unref(OBJECT(current_machine));
4327 exit(1);
4330 configure_accelerator(current_machine);
4332 if (qtest_chrdev) {
4333 qtest_init(qtest_chrdev, qtest_log, &error_fatal);
4336 machine_opts = qemu_get_machine_opts();
4337 kernel_filename = qemu_opt_get(machine_opts, "kernel");
4338 initrd_filename = qemu_opt_get(machine_opts, "initrd");
4339 kernel_cmdline = qemu_opt_get(machine_opts, "append");
4340 bios_name = qemu_opt_get(machine_opts, "firmware");
4342 opts = qemu_opts_find(qemu_find_opts("boot-opts"), NULL);
4343 if (opts) {
4344 boot_order = qemu_opt_get(opts, "order");
4345 if (boot_order) {
4346 validate_bootdevices(boot_order, &error_fatal);
4349 boot_once = qemu_opt_get(opts, "once");
4350 if (boot_once) {
4351 validate_bootdevices(boot_once, &error_fatal);
4354 boot_menu = qemu_opt_get_bool(opts, "menu", boot_menu);
4355 boot_strict = qemu_opt_get_bool(opts, "strict", false);
4358 if (!boot_order) {
4359 boot_order = machine_class->default_boot_order;
4362 if (!kernel_cmdline) {
4363 kernel_cmdline = "";
4364 current_machine->kernel_cmdline = (char *)kernel_cmdline;
4367 linux_boot = (kernel_filename != NULL);
4369 if (!linux_boot && *kernel_cmdline != '\0') {
4370 error_report("-append only allowed with -kernel option");
4371 exit(1);
4374 if (!linux_boot && initrd_filename != NULL) {
4375 error_report("-initrd only allowed with -kernel option");
4376 exit(1);
4379 if (semihosting_enabled() && !semihosting_get_argc() && kernel_filename) {
4380 /* fall back to the -kernel/-append */
4381 semihosting_arg_fallback(kernel_filename, kernel_cmdline);
4384 os_set_line_buffering();
4386 /* spice needs the timers to be initialized by this point */
4387 qemu_spice_init();
4389 cpu_ticks_init();
4390 if (icount_opts) {
4391 if (!tcg_enabled()) {
4392 error_report("-icount is not allowed with hardware virtualization");
4393 exit(1);
4395 configure_icount(icount_opts, &error_abort);
4396 qemu_opts_del(icount_opts);
4399 if (default_net) {
4400 QemuOptsList *net = qemu_find_opts("net");
4401 qemu_opts_set(net, NULL, "type", "nic", &error_abort);
4402 #ifdef CONFIG_SLIRP
4403 qemu_opts_set(net, NULL, "type", "user", &error_abort);
4404 #endif
4407 colo_info_init();
4409 if (net_init_clients() < 0) {
4410 exit(1);
4413 if (qemu_opts_foreach(qemu_find_opts("object"),
4414 user_creatable_add_opts_foreach,
4415 object_create_delayed, NULL)) {
4416 exit(1);
4419 #ifdef CONFIG_TPM
4420 if (tpm_init() < 0) {
4421 exit(1);
4423 #endif
4425 /* init the bluetooth world */
4426 if (foreach_device_config(DEV_BT, bt_parse))
4427 exit(1);
4429 if (!xen_enabled()) {
4430 /* On 32-bit hosts, QEMU is limited by virtual address space */
4431 if (ram_size > (2047 << 20) && HOST_LONG_BITS == 32) {
4432 error_report("at most 2047 MB RAM can be simulated");
4433 exit(1);
4437 blk_mig_init();
4438 ram_mig_init();
4440 /* If the currently selected machine wishes to override the units-per-bus
4441 * property of its default HBA interface type, do so now. */
4442 if (machine_class->units_per_default_bus) {
4443 override_max_devs(machine_class->block_default_type,
4444 machine_class->units_per_default_bus);
4447 /* open the virtual block devices */
4448 if (snapshot || replay_mode != REPLAY_MODE_NONE) {
4449 qemu_opts_foreach(qemu_find_opts("drive"), drive_enable_snapshot,
4450 NULL, NULL);
4452 if (qemu_opts_foreach(qemu_find_opts("drive"), drive_init_func,
4453 &machine_class->block_default_type, NULL)) {
4454 exit(1);
4457 default_drive(default_cdrom, snapshot, machine_class->block_default_type, 2,
4458 CDROM_OPTS);
4459 default_drive(default_floppy, snapshot, IF_FLOPPY, 0, FD_OPTS);
4460 default_drive(default_sdcard, snapshot, IF_SD, 0, SD_OPTS);
4462 parse_numa_opts(machine_class);
4464 if (qemu_opts_foreach(qemu_find_opts("mon"),
4465 mon_init_func, NULL, NULL)) {
4466 exit(1);
4469 if (foreach_device_config(DEV_SERIAL, serial_parse) < 0)
4470 exit(1);
4471 if (foreach_device_config(DEV_PARALLEL, parallel_parse) < 0)
4472 exit(1);
4473 if (foreach_device_config(DEV_VIRTCON, virtcon_parse) < 0)
4474 exit(1);
4475 if (foreach_device_config(DEV_SCLP, sclp_parse) < 0) {
4476 exit(1);
4478 if (foreach_device_config(DEV_DEBUGCON, debugcon_parse) < 0)
4479 exit(1);
4481 /* If no default VGA is requested, the default is "none". */
4482 if (default_vga) {
4483 if (machine_class->default_display) {
4484 vga_model = machine_class->default_display;
4485 } else if (vga_interface_available(VGA_CIRRUS)) {
4486 vga_model = "cirrus";
4487 } else if (vga_interface_available(VGA_STD)) {
4488 vga_model = "std";
4491 if (vga_model) {
4492 select_vgahw(vga_model);
4495 if (watchdog) {
4496 i = select_watchdog(watchdog);
4497 if (i > 0)
4498 exit (i == 1 ? 1 : 0);
4501 machine_register_compat_props(current_machine);
4503 qemu_opts_foreach(qemu_find_opts("global"),
4504 global_init_func, NULL, NULL);
4506 /* This checkpoint is required by replay to separate prior clock
4507 reading from the other reads, because timer polling functions query
4508 clock values from the log. */
4509 replay_checkpoint(CHECKPOINT_INIT);
4510 qdev_machine_init();
4512 current_machine->ram_size = ram_size;
4513 current_machine->maxram_size = maxram_size;
4514 current_machine->ram_slots = ram_slots;
4515 current_machine->boot_order = boot_order;
4516 current_machine->cpu_model = cpu_model;
4518 machine_class->init(current_machine);
4520 realtime_init();
4522 audio_init();
4524 cpu_synchronize_all_post_init();
4526 if (hax_enabled()) {
4527 hax_sync_vcpus();
4530 if (qemu_opts_foreach(qemu_find_opts("fw_cfg"),
4531 parse_fw_cfg, fw_cfg_find(), NULL) != 0) {
4532 exit(1);
4535 /* init USB devices */
4536 if (machine_usb(current_machine)) {
4537 if (foreach_device_config(DEV_USB, usb_parse) < 0)
4538 exit(1);
4541 /* Check if IGD GFX passthrough. */
4542 igd_gfx_passthru();
4544 /* init generic devices */
4545 rom_set_order_override(FW_CFG_ORDER_OVERRIDE_DEVICE);
4546 if (qemu_opts_foreach(qemu_find_opts("device"),
4547 device_init_func, NULL, NULL)) {
4548 exit(1);
4551 numa_post_machine_init();
4553 rom_reset_order_override();
4555 /* Did we create any drives that we failed to create a device for? */
4556 drive_check_orphaned();
4558 /* Don't warn about the default network setup that you get if
4559 * no command line -net or -netdev options are specified. There
4560 * are two cases that we would otherwise complain about:
4561 * (1) board doesn't support a NIC but the implicit "-net nic"
4562 * requested one
4563 * (2) CONFIG_SLIRP not set, in which case the implicit "-net nic"
4564 * sets up a nic that isn't connected to anything.
4566 if (!default_net) {
4567 net_check_clients();
4571 if (boot_once) {
4572 qemu_boot_set(boot_once, &error_fatal);
4573 qemu_register_reset(restore_boot_order, g_strdup(boot_order));
4576 ds = init_displaystate();
4578 /* init local displays */
4579 switch (display_type) {
4580 case DT_CURSES:
4581 curses_display_init(ds, full_screen);
4582 break;
4583 case DT_SDL:
4584 sdl_display_init(ds, full_screen, no_frame);
4585 break;
4586 case DT_COCOA:
4587 cocoa_display_init(ds, full_screen);
4588 break;
4589 case DT_GTK:
4590 gtk_display_init(ds, full_screen, grab_on_hover);
4591 break;
4592 default:
4593 break;
4596 /* must be after terminal init, SDL library changes signal handlers */
4597 os_setup_signal_handling();
4599 /* init remote displays */
4600 #ifdef CONFIG_VNC
4601 qemu_opts_foreach(qemu_find_opts("vnc"),
4602 vnc_init_func, NULL, NULL);
4603 #endif
4605 if (using_spice) {
4606 qemu_spice_display_init();
4609 if (foreach_device_config(DEV_GDB, gdbserver_start) < 0) {
4610 exit(1);
4613 qdev_machine_creation_done();
4615 /* TODO: once all bus devices are qdevified, this should be done
4616 * when bus is created by qdev.c */
4617 qemu_register_reset(qbus_reset_all_fn, sysbus_get_default());
4618 qemu_run_machine_init_done_notifiers();
4620 if (rom_check_and_register_reset() != 0) {
4621 error_report("rom check and register reset failed");
4622 exit(1);
4625 replay_start();
4627 /* This checkpoint is required by replay to separate prior clock
4628 reading from the other reads, because timer polling functions query
4629 clock values from the log. */
4630 replay_checkpoint(CHECKPOINT_RESET);
4631 qemu_system_reset(VMRESET_SILENT);
4632 register_global_state();
4633 if (loadvm) {
4634 if (load_vmstate(loadvm) < 0) {
4635 autostart = 0;
4639 qdev_prop_check_globals();
4640 if (vmstate_dump_file) {
4641 /* dump and exit */
4642 dump_vmstate_json_to_file(vmstate_dump_file);
4643 return 0;
4646 if (incoming) {
4647 Error *local_err = NULL;
4648 qemu_start_incoming_migration(incoming, &local_err);
4649 if (local_err) {
4650 error_reportf_err(local_err, "-incoming %s: ", incoming);
4651 exit(1);
4653 } else if (autostart) {
4654 vm_start();
4657 os_setup_post();
4659 main_loop();
4660 replay_disable_events();
4661 iothread_stop_all();
4663 bdrv_close_all();
4664 pause_all_vcpus();
4665 res_free();
4667 /* vhost-user must be cleaned up before chardevs. */
4668 net_cleanup();
4669 audio_cleanup();
4670 monitor_cleanup();
4671 qemu_chr_cleanup();
4673 return 0;