Merge remote-tracking branch 'qemu/master'
[qemu/ar7.git] / vl.c
blob8ea9d34f81d7d1e53bd6b4564e3707f116c725a2
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 #define SDL_MAIN_HANDLED
41 #include <SDL.h>
42 #if SDL_MAJOR_VERSION == 1
43 int qemu_main(int argc, char **argv);
44 int main(int argc, char **argv)
46 return qemu_main(argc, argv);
48 #undef main
49 #define main qemu_main
50 #elif SDL_MAJOR_VERSION == 2
51 #undef main
52 #else
53 #error undefined SDL version
54 #endif
55 #endif
56 #endif /* CONFIG_SDL */
58 #ifdef CONFIG_COCOA
59 #undef main
60 #define main qemu_main
61 #endif /* CONFIG_COCOA */
64 #include "qemu/error-report.h"
65 #include "qemu/sockets.h"
66 #include "hw/hw.h"
67 #include "hw/boards.h"
68 #include "sysemu/accel.h"
69 #include "hw/usb.h"
70 #include "hw/i386/pc.h"
71 #include "hw/isa/isa.h"
72 #include "hw/bt.h"
73 #include "sysemu/watchdog.h"
74 #include "hw/smbios/smbios.h"
75 #include "hw/acpi/acpi.h"
76 #include "hw/xen/xen.h"
77 #include "hw/qdev.h"
78 #include "hw/loader.h"
79 #include "monitor/qdev.h"
80 #include "sysemu/bt.h"
81 #include "net/net.h"
82 #include "net/slirp.h"
83 #include "monitor/monitor.h"
84 #include "ui/console.h"
85 #include "ui/input.h"
86 #include "sysemu/sysemu.h"
87 #include "sysemu/numa.h"
88 #include "exec/gdbstub.h"
89 #include "qemu/timer.h"
90 #include "sysemu/char.h"
91 #include "qemu/bitmap.h"
92 #include "qemu/log.h"
93 #include "sysemu/blockdev.h"
94 #include "hw/block/block.h"
95 #include "migration/block.h"
96 #include "sysemu/tpm.h"
97 #include "sysemu/dma.h"
98 #include "audio/audio.h"
99 #include "migration/migration.h"
100 #include "sysemu/cpus.h"
101 #include "migration/colo.h"
102 #include "sysemu/kvm.h"
103 #include "sysemu/hax.h"
104 #include "qapi/qmp/qjson.h"
105 #include "qemu/option.h"
106 #include "qemu/config-file.h"
107 #include "qemu-options.h"
108 #include "qmp-commands.h"
109 #include "qemu/main-loop.h"
110 #ifdef CONFIG_VIRTFS
111 #include "fsdev/qemu-fsdev.h"
112 #endif
113 #include "sysemu/qtest.h"
115 #include "disas/disas.h"
118 #include "slirp/libslirp.h"
120 #include "trace.h"
121 #include "trace/control.h"
122 #include "qemu/queue.h"
123 #include "sysemu/arch_init.h"
125 #include "ui/qemu-spice.h"
126 #include "qapi/string-input-visitor.h"
127 #include "qapi/opts-visitor.h"
128 #include "qom/object_interfaces.h"
129 #include "qapi-event.h"
130 #include "exec/semihost.h"
131 #include "crypto/init.h"
132 #include "sysemu/replay.h"
133 #include "qapi/qmp/qerror.h"
134 #include "sysemu/iothread.h"
136 #define MAX_VIRTIO_CONSOLES 1
137 #define MAX_SCLP_CONSOLES 1
139 static const char *data_dir[16];
140 static int data_dir_idx;
141 const char *bios_name = NULL;
142 enum vga_retrace_method vga_retrace_method = VGA_RETRACE_DUMB;
143 int request_opengl = -1;
144 int display_opengl;
145 const char* keyboard_layout = NULL;
146 ram_addr_t ram_size;
147 const char *mem_path = NULL;
148 int mem_prealloc = 0; /* force preallocation of physical target memory */
149 bool enable_mlock = false;
150 int nb_nics;
151 NICInfo nd_table[MAX_NICS];
152 int autostart;
153 static int rtc_utc = 1;
154 static int rtc_date_offset = -1; /* -1 means no change */
155 QEMUClockType rtc_clock;
156 int vga_interface_type = VGA_NONE;
157 static int full_screen = 0;
158 static int no_frame = 0;
159 int no_quit = 0;
160 static bool grab_on_hover;
161 CharDriverState *serial_hds[MAX_SERIAL_PORTS];
162 CharDriverState *parallel_hds[MAX_PARALLEL_PORTS];
163 CharDriverState *virtcon_hds[MAX_VIRTIO_CONSOLES];
164 CharDriverState *sclp_hds[MAX_SCLP_CONSOLES];
165 int win2k_install_hack = 0;
166 int singlestep = 0;
167 int smp_cpus = 1;
168 int max_cpus = 1;
169 int smp_cores = 1;
170 int smp_threads = 1;
171 int acpi_enabled = 1;
172 int no_hpet = 0;
173 int fd_bootchk = 1;
174 static int no_reboot;
175 int no_shutdown = 0;
176 int cursor_hide = 1;
177 int graphic_rotate = 0;
178 const char *watchdog;
179 QEMUOptionRom option_rom[MAX_OPTION_ROMS];
180 int nb_option_roms;
181 int old_param = 0;
182 const char *qemu_name;
183 int alt_grab = 0;
184 int ctrl_grab = 0;
185 unsigned int nb_prom_envs = 0;
186 const char *prom_envs[MAX_PROM_ENVS];
187 int boot_menu;
188 bool boot_strict;
189 uint8_t *boot_splash_filedata;
190 size_t boot_splash_filedata_size;
191 uint8_t qemu_extra_params_fw[2];
193 int icount_align_option;
195 /* The bytes in qemu_uuid are in the order specified by RFC4122, _not_ in the
196 * little-endian "wire format" described in the SMBIOS 2.6 specification.
198 QemuUUID qemu_uuid;
199 bool qemu_uuid_set;
201 /* Trace unassigned memory or i/o accesses. */
202 bool trace_unassigned;
204 static NotifierList exit_notifiers =
205 NOTIFIER_LIST_INITIALIZER(exit_notifiers);
207 static NotifierList machine_init_done_notifiers =
208 NOTIFIER_LIST_INITIALIZER(machine_init_done_notifiers);
210 bool xen_allowed;
211 uint32_t xen_domid;
212 enum xen_mode xen_mode = XEN_EMULATE;
214 static int has_defaults = 1;
215 static int default_serial = 1;
216 static int default_parallel = 1;
217 static int default_virtcon = 1;
218 static int default_sclp = 1;
219 static int default_monitor = 1;
220 static int default_floppy = 1;
221 static int default_cdrom = 1;
222 static int default_sdcard = 1;
223 static int default_vga = 1;
224 static int default_net = 1;
226 static struct {
227 const char *driver;
228 int *flag;
229 } default_list[] = {
230 { .driver = "isa-serial", .flag = &default_serial },
231 { .driver = "isa-parallel", .flag = &default_parallel },
232 { .driver = "isa-fdc", .flag = &default_floppy },
233 { .driver = "floppy", .flag = &default_floppy },
234 { .driver = "ide-cd", .flag = &default_cdrom },
235 { .driver = "ide-hd", .flag = &default_cdrom },
236 { .driver = "ide-drive", .flag = &default_cdrom },
237 { .driver = "scsi-cd", .flag = &default_cdrom },
238 { .driver = "virtio-serial-pci", .flag = &default_virtcon },
239 { .driver = "virtio-serial", .flag = &default_virtcon },
240 { .driver = "VGA", .flag = &default_vga },
241 { .driver = "isa-vga", .flag = &default_vga },
242 { .driver = "cirrus-vga", .flag = &default_vga },
243 { .driver = "isa-cirrus-vga", .flag = &default_vga },
244 { .driver = "vmware-svga", .flag = &default_vga },
245 { .driver = "qxl-vga", .flag = &default_vga },
246 { .driver = "virtio-vga", .flag = &default_vga },
249 static QemuOptsList qemu_rtc_opts = {
250 .name = "rtc",
251 .head = QTAILQ_HEAD_INITIALIZER(qemu_rtc_opts.head),
252 .desc = {
254 .name = "base",
255 .type = QEMU_OPT_STRING,
257 .name = "clock",
258 .type = QEMU_OPT_STRING,
260 .name = "driftfix",
261 .type = QEMU_OPT_STRING,
263 { /* end of list */ }
267 static QemuOptsList qemu_sandbox_opts = {
268 .name = "sandbox",
269 .implied_opt_name = "enable",
270 .head = QTAILQ_HEAD_INITIALIZER(qemu_sandbox_opts.head),
271 .desc = {
273 .name = "enable",
274 .type = QEMU_OPT_BOOL,
276 { /* end of list */ }
280 static QemuOptsList qemu_option_rom_opts = {
281 .name = "option-rom",
282 .implied_opt_name = "romfile",
283 .head = QTAILQ_HEAD_INITIALIZER(qemu_option_rom_opts.head),
284 .desc = {
286 .name = "bootindex",
287 .type = QEMU_OPT_NUMBER,
288 }, {
289 .name = "romfile",
290 .type = QEMU_OPT_STRING,
292 { /* end of list */ }
296 static QemuOptsList qemu_machine_opts = {
297 .name = "machine",
298 .implied_opt_name = "type",
299 .merge_lists = true,
300 .head = QTAILQ_HEAD_INITIALIZER(qemu_machine_opts.head),
301 .desc = {
303 * no elements => accept any
304 * sanity checking will happen later
305 * when setting machine properties
311 static QemuOptsList qemu_boot_opts = {
312 .name = "boot-opts",
313 .implied_opt_name = "order",
314 .merge_lists = true,
315 .head = QTAILQ_HEAD_INITIALIZER(qemu_boot_opts.head),
316 .desc = {
318 .name = "order",
319 .type = QEMU_OPT_STRING,
320 }, {
321 .name = "once",
322 .type = QEMU_OPT_STRING,
323 }, {
324 .name = "menu",
325 .type = QEMU_OPT_BOOL,
326 }, {
327 .name = "splash",
328 .type = QEMU_OPT_STRING,
329 }, {
330 .name = "splash-time",
331 .type = QEMU_OPT_STRING,
332 }, {
333 .name = "reboot-timeout",
334 .type = QEMU_OPT_STRING,
335 }, {
336 .name = "strict",
337 .type = QEMU_OPT_BOOL,
339 { /*End of list */ }
343 static QemuOptsList qemu_add_fd_opts = {
344 .name = "add-fd",
345 .head = QTAILQ_HEAD_INITIALIZER(qemu_add_fd_opts.head),
346 .desc = {
348 .name = "fd",
349 .type = QEMU_OPT_NUMBER,
350 .help = "file descriptor of which a duplicate is added to fd set",
352 .name = "set",
353 .type = QEMU_OPT_NUMBER,
354 .help = "ID of the fd set to add fd to",
356 .name = "opaque",
357 .type = QEMU_OPT_STRING,
358 .help = "free-form string used to describe fd",
360 { /* end of list */ }
364 static QemuOptsList qemu_object_opts = {
365 .name = "object",
366 .implied_opt_name = "qom-type",
367 .head = QTAILQ_HEAD_INITIALIZER(qemu_object_opts.head),
368 .desc = {
373 static QemuOptsList qemu_tpmdev_opts = {
374 .name = "tpmdev",
375 .implied_opt_name = "type",
376 .head = QTAILQ_HEAD_INITIALIZER(qemu_tpmdev_opts.head),
377 .desc = {
378 /* options are defined in the TPM backends */
379 { /* end of list */ }
383 static QemuOptsList qemu_realtime_opts = {
384 .name = "realtime",
385 .head = QTAILQ_HEAD_INITIALIZER(qemu_realtime_opts.head),
386 .desc = {
388 .name = "mlock",
389 .type = QEMU_OPT_BOOL,
391 { /* end of list */ }
395 static QemuOptsList qemu_msg_opts = {
396 .name = "msg",
397 .head = QTAILQ_HEAD_INITIALIZER(qemu_msg_opts.head),
398 .desc = {
400 .name = "timestamp",
401 .type = QEMU_OPT_BOOL,
403 { /* end of list */ }
407 static QemuOptsList qemu_name_opts = {
408 .name = "name",
409 .implied_opt_name = "guest",
410 .merge_lists = true,
411 .head = QTAILQ_HEAD_INITIALIZER(qemu_name_opts.head),
412 .desc = {
414 .name = "guest",
415 .type = QEMU_OPT_STRING,
416 .help = "Sets the name of the guest.\n"
417 "This name will be displayed in the SDL window caption.\n"
418 "The name will also be used for the VNC server",
419 }, {
420 .name = "process",
421 .type = QEMU_OPT_STRING,
422 .help = "Sets the name of the QEMU process, as shown in top etc",
423 }, {
424 .name = "debug-threads",
425 .type = QEMU_OPT_BOOL,
426 .help = "When enabled, name the individual threads; defaults off.\n"
427 "NOTE: The thread names are for debugging and not a\n"
428 "stable API.",
430 { /* End of list */ }
434 static QemuOptsList qemu_mem_opts = {
435 .name = "memory",
436 .implied_opt_name = "size",
437 .head = QTAILQ_HEAD_INITIALIZER(qemu_mem_opts.head),
438 .merge_lists = true,
439 .desc = {
441 .name = "size",
442 .type = QEMU_OPT_SIZE,
445 .name = "slots",
446 .type = QEMU_OPT_NUMBER,
449 .name = "maxmem",
450 .type = QEMU_OPT_SIZE,
452 { /* end of list */ }
456 static QemuOptsList qemu_icount_opts = {
457 .name = "icount",
458 .implied_opt_name = "shift",
459 .merge_lists = true,
460 .head = QTAILQ_HEAD_INITIALIZER(qemu_icount_opts.head),
461 .desc = {
463 .name = "shift",
464 .type = QEMU_OPT_STRING,
465 }, {
466 .name = "align",
467 .type = QEMU_OPT_BOOL,
468 }, {
469 .name = "sleep",
470 .type = QEMU_OPT_BOOL,
471 }, {
472 .name = "rr",
473 .type = QEMU_OPT_STRING,
474 }, {
475 .name = "rrfile",
476 .type = QEMU_OPT_STRING,
478 { /* end of list */ }
482 static QemuOptsList qemu_semihosting_config_opts = {
483 .name = "semihosting-config",
484 .implied_opt_name = "enable",
485 .head = QTAILQ_HEAD_INITIALIZER(qemu_semihosting_config_opts.head),
486 .desc = {
488 .name = "enable",
489 .type = QEMU_OPT_BOOL,
490 }, {
491 .name = "target",
492 .type = QEMU_OPT_STRING,
493 }, {
494 .name = "arg",
495 .type = QEMU_OPT_STRING,
497 { /* end of list */ }
501 static QemuOptsList qemu_fw_cfg_opts = {
502 .name = "fw_cfg",
503 .implied_opt_name = "name",
504 .head = QTAILQ_HEAD_INITIALIZER(qemu_fw_cfg_opts.head),
505 .desc = {
507 .name = "name",
508 .type = QEMU_OPT_STRING,
509 .help = "Sets the fw_cfg name of the blob to be inserted",
510 }, {
511 .name = "file",
512 .type = QEMU_OPT_STRING,
513 .help = "Sets the name of the file from which\n"
514 "the fw_cfg blob will be loaded",
515 }, {
516 .name = "string",
517 .type = QEMU_OPT_STRING,
518 .help = "Sets content of the blob to be inserted from a string",
520 { /* end of list */ }
524 #ifdef CONFIG_LIBISCSI
525 static QemuOptsList qemu_iscsi_opts = {
526 .name = "iscsi",
527 .head = QTAILQ_HEAD_INITIALIZER(qemu_iscsi_opts.head),
528 .desc = {
530 .name = "user",
531 .type = QEMU_OPT_STRING,
532 .help = "username for CHAP authentication to target",
534 .name = "password",
535 .type = QEMU_OPT_STRING,
536 .help = "password for CHAP authentication to target",
538 .name = "password-secret",
539 .type = QEMU_OPT_STRING,
540 .help = "ID of the secret providing password for CHAP "
541 "authentication to target",
543 .name = "header-digest",
544 .type = QEMU_OPT_STRING,
545 .help = "HeaderDigest setting. "
546 "{CRC32C|CRC32C-NONE|NONE-CRC32C|NONE}",
548 .name = "initiator-name",
549 .type = QEMU_OPT_STRING,
550 .help = "Initiator iqn name to use when connecting",
552 .name = "timeout",
553 .type = QEMU_OPT_NUMBER,
554 .help = "Request timeout in seconds (default 0 = no timeout)",
556 { /* end of list */ }
559 #endif
562 * Get machine options
564 * Returns: machine options (never null).
566 QemuOpts *qemu_get_machine_opts(void)
568 return qemu_find_opts_singleton("machine");
571 const char *qemu_get_vm_name(void)
573 return qemu_name;
576 static void res_free(void)
578 g_free(boot_splash_filedata);
579 boot_splash_filedata = NULL;
582 static int default_driver_check(void *opaque, QemuOpts *opts, Error **errp)
584 const char *driver = qemu_opt_get(opts, "driver");
585 int i;
587 if (!driver)
588 return 0;
589 for (i = 0; i < ARRAY_SIZE(default_list); i++) {
590 if (strcmp(default_list[i].driver, driver) != 0)
591 continue;
592 *(default_list[i].flag) = 0;
594 return 0;
597 /***********************************************************/
598 /* QEMU state */
600 static RunState current_run_state = RUN_STATE_PRELAUNCH;
602 /* We use RUN_STATE__MAX but any invalid value will do */
603 static RunState vmstop_requested = RUN_STATE__MAX;
604 static QemuMutex vmstop_lock;
606 typedef struct {
607 RunState from;
608 RunState to;
609 } RunStateTransition;
611 static const RunStateTransition runstate_transitions_def[] = {
612 /* from -> to */
613 { RUN_STATE_DEBUG, RUN_STATE_RUNNING },
614 { RUN_STATE_DEBUG, RUN_STATE_FINISH_MIGRATE },
615 { RUN_STATE_DEBUG, RUN_STATE_PRELAUNCH },
617 { RUN_STATE_INMIGRATE, RUN_STATE_INTERNAL_ERROR },
618 { RUN_STATE_INMIGRATE, RUN_STATE_IO_ERROR },
619 { RUN_STATE_INMIGRATE, RUN_STATE_PAUSED },
620 { RUN_STATE_INMIGRATE, RUN_STATE_RUNNING },
621 { RUN_STATE_INMIGRATE, RUN_STATE_SHUTDOWN },
622 { RUN_STATE_INMIGRATE, RUN_STATE_SUSPENDED },
623 { RUN_STATE_INMIGRATE, RUN_STATE_WATCHDOG },
624 { RUN_STATE_INMIGRATE, RUN_STATE_GUEST_PANICKED },
625 { RUN_STATE_INMIGRATE, RUN_STATE_FINISH_MIGRATE },
626 { RUN_STATE_INMIGRATE, RUN_STATE_PRELAUNCH },
627 { RUN_STATE_INMIGRATE, RUN_STATE_POSTMIGRATE },
628 { RUN_STATE_INMIGRATE, RUN_STATE_COLO },
630 { RUN_STATE_INTERNAL_ERROR, RUN_STATE_PAUSED },
631 { RUN_STATE_INTERNAL_ERROR, RUN_STATE_FINISH_MIGRATE },
632 { RUN_STATE_INTERNAL_ERROR, RUN_STATE_PRELAUNCH },
634 { RUN_STATE_IO_ERROR, RUN_STATE_RUNNING },
635 { RUN_STATE_IO_ERROR, RUN_STATE_FINISH_MIGRATE },
636 { RUN_STATE_IO_ERROR, RUN_STATE_PRELAUNCH },
638 { RUN_STATE_PAUSED, RUN_STATE_RUNNING },
639 { RUN_STATE_PAUSED, RUN_STATE_FINISH_MIGRATE },
640 { RUN_STATE_PAUSED, RUN_STATE_PRELAUNCH },
641 { RUN_STATE_PAUSED, RUN_STATE_COLO},
643 { RUN_STATE_POSTMIGRATE, RUN_STATE_RUNNING },
644 { RUN_STATE_POSTMIGRATE, RUN_STATE_FINISH_MIGRATE },
645 { RUN_STATE_POSTMIGRATE, RUN_STATE_PRELAUNCH },
647 { RUN_STATE_PRELAUNCH, RUN_STATE_RUNNING },
648 { RUN_STATE_PRELAUNCH, RUN_STATE_FINISH_MIGRATE },
649 { RUN_STATE_PRELAUNCH, RUN_STATE_INMIGRATE },
651 { RUN_STATE_FINISH_MIGRATE, RUN_STATE_RUNNING },
652 { RUN_STATE_FINISH_MIGRATE, RUN_STATE_POSTMIGRATE },
653 { RUN_STATE_FINISH_MIGRATE, RUN_STATE_PRELAUNCH },
654 { RUN_STATE_FINISH_MIGRATE, RUN_STATE_COLO},
656 { RUN_STATE_RESTORE_VM, RUN_STATE_RUNNING },
657 { RUN_STATE_RESTORE_VM, RUN_STATE_PRELAUNCH },
659 { RUN_STATE_COLO, RUN_STATE_RUNNING },
661 { RUN_STATE_RUNNING, RUN_STATE_DEBUG },
662 { RUN_STATE_RUNNING, RUN_STATE_INTERNAL_ERROR },
663 { RUN_STATE_RUNNING, RUN_STATE_IO_ERROR },
664 { RUN_STATE_RUNNING, RUN_STATE_PAUSED },
665 { RUN_STATE_RUNNING, RUN_STATE_FINISH_MIGRATE },
666 { RUN_STATE_RUNNING, RUN_STATE_RESTORE_VM },
667 { RUN_STATE_RUNNING, RUN_STATE_SAVE_VM },
668 { RUN_STATE_RUNNING, RUN_STATE_SHUTDOWN },
669 { RUN_STATE_RUNNING, RUN_STATE_WATCHDOG },
670 { RUN_STATE_RUNNING, RUN_STATE_GUEST_PANICKED },
671 { RUN_STATE_RUNNING, RUN_STATE_COLO},
673 { RUN_STATE_SAVE_VM, RUN_STATE_RUNNING },
675 { RUN_STATE_SHUTDOWN, RUN_STATE_PAUSED },
676 { RUN_STATE_SHUTDOWN, RUN_STATE_FINISH_MIGRATE },
677 { RUN_STATE_SHUTDOWN, RUN_STATE_PRELAUNCH },
679 { RUN_STATE_DEBUG, RUN_STATE_SUSPENDED },
680 { RUN_STATE_RUNNING, RUN_STATE_SUSPENDED },
681 { RUN_STATE_SUSPENDED, RUN_STATE_RUNNING },
682 { RUN_STATE_SUSPENDED, RUN_STATE_FINISH_MIGRATE },
683 { RUN_STATE_SUSPENDED, RUN_STATE_PRELAUNCH },
684 { RUN_STATE_SUSPENDED, RUN_STATE_COLO},
686 { RUN_STATE_WATCHDOG, RUN_STATE_RUNNING },
687 { RUN_STATE_WATCHDOG, RUN_STATE_FINISH_MIGRATE },
688 { RUN_STATE_WATCHDOG, RUN_STATE_PRELAUNCH },
689 { RUN_STATE_WATCHDOG, RUN_STATE_COLO},
691 { RUN_STATE_GUEST_PANICKED, RUN_STATE_RUNNING },
692 { RUN_STATE_GUEST_PANICKED, RUN_STATE_FINISH_MIGRATE },
693 { RUN_STATE_GUEST_PANICKED, RUN_STATE_PRELAUNCH },
695 { RUN_STATE__MAX, RUN_STATE__MAX },
698 static bool runstate_valid_transitions[RUN_STATE__MAX][RUN_STATE__MAX];
700 bool runstate_check(RunState state)
702 return current_run_state == state;
705 bool runstate_store(char *str, size_t size)
707 const char *state = RunState_lookup[current_run_state];
708 size_t len = strlen(state) + 1;
710 if (len > size) {
711 return false;
713 memcpy(str, state, len);
714 return true;
717 static void runstate_init(void)
719 const RunStateTransition *p;
721 memset(&runstate_valid_transitions, 0, sizeof(runstate_valid_transitions));
722 for (p = &runstate_transitions_def[0]; p->from != RUN_STATE__MAX; p++) {
723 runstate_valid_transitions[p->from][p->to] = true;
726 qemu_mutex_init(&vmstop_lock);
729 /* This function will abort() on invalid state transitions */
730 void runstate_set(RunState new_state)
732 assert(new_state < RUN_STATE__MAX);
734 if (current_run_state == new_state) {
735 return;
738 if (!runstate_valid_transitions[current_run_state][new_state]) {
739 error_report("invalid runstate transition: '%s' -> '%s'",
740 RunState_lookup[current_run_state],
741 RunState_lookup[new_state]);
742 abort();
744 trace_runstate_set(new_state);
745 current_run_state = new_state;
748 int runstate_is_running(void)
750 return runstate_check(RUN_STATE_RUNNING);
753 bool runstate_needs_reset(void)
755 return runstate_check(RUN_STATE_INTERNAL_ERROR) ||
756 runstate_check(RUN_STATE_SHUTDOWN);
759 StatusInfo *qmp_query_status(Error **errp)
761 StatusInfo *info = g_malloc0(sizeof(*info));
763 info->running = runstate_is_running();
764 info->singlestep = singlestep;
765 info->status = current_run_state;
767 return info;
770 static bool qemu_vmstop_requested(RunState *r)
772 qemu_mutex_lock(&vmstop_lock);
773 *r = vmstop_requested;
774 vmstop_requested = RUN_STATE__MAX;
775 qemu_mutex_unlock(&vmstop_lock);
776 return *r < RUN_STATE__MAX;
779 void qemu_system_vmstop_request_prepare(void)
781 qemu_mutex_lock(&vmstop_lock);
784 void qemu_system_vmstop_request(RunState state)
786 vmstop_requested = state;
787 qemu_mutex_unlock(&vmstop_lock);
788 qemu_notify_event();
791 void vm_start(void)
793 RunState requested;
795 qemu_vmstop_requested(&requested);
796 if (runstate_is_running() && requested == RUN_STATE__MAX) {
797 return;
800 /* Ensure that a STOP/RESUME pair of events is emitted if a
801 * vmstop request was pending. The BLOCK_IO_ERROR event, for
802 * example, according to documentation is always followed by
803 * the STOP event.
805 if (runstate_is_running()) {
806 qapi_event_send_stop(&error_abort);
807 } else {
808 replay_enable_events();
809 cpu_enable_ticks();
810 runstate_set(RUN_STATE_RUNNING);
811 vm_state_notify(1, RUN_STATE_RUNNING);
812 resume_all_vcpus();
815 qapi_event_send_resume(&error_abort);
819 /***********************************************************/
820 /* real time host monotonic timer */
822 static time_t qemu_time(void)
824 return qemu_clock_get_ms(QEMU_CLOCK_HOST) / 1000;
827 /***********************************************************/
828 /* host time/date access */
829 void qemu_get_timedate(struct tm *tm, int offset)
831 time_t ti = qemu_time();
833 ti += offset;
834 if (rtc_date_offset == -1) {
835 if (rtc_utc)
836 gmtime_r(&ti, tm);
837 else
838 localtime_r(&ti, tm);
839 } else {
840 ti -= rtc_date_offset;
841 gmtime_r(&ti, tm);
845 int qemu_timedate_diff(struct tm *tm)
847 time_t seconds;
849 if (rtc_date_offset == -1)
850 if (rtc_utc)
851 seconds = mktimegm(tm);
852 else {
853 struct tm tmp = *tm;
854 tmp.tm_isdst = -1; /* use timezone to figure it out */
855 seconds = mktime(&tmp);
857 else
858 seconds = mktimegm(tm) + rtc_date_offset;
860 return seconds - qemu_time();
863 static void configure_rtc_date_offset(const char *startdate, int legacy)
865 time_t rtc_start_date;
866 struct tm tm;
868 if (!strcmp(startdate, "now") && legacy) {
869 rtc_date_offset = -1;
870 } else {
871 if (sscanf(startdate, "%d-%d-%dT%d:%d:%d",
872 &tm.tm_year,
873 &tm.tm_mon,
874 &tm.tm_mday,
875 &tm.tm_hour,
876 &tm.tm_min,
877 &tm.tm_sec) == 6) {
878 /* OK */
879 } else if (sscanf(startdate, "%d-%d-%d",
880 &tm.tm_year,
881 &tm.tm_mon,
882 &tm.tm_mday) == 3) {
883 tm.tm_hour = 0;
884 tm.tm_min = 0;
885 tm.tm_sec = 0;
886 } else {
887 goto date_fail;
889 tm.tm_year -= 1900;
890 tm.tm_mon--;
891 rtc_start_date = mktimegm(&tm);
892 if (rtc_start_date == -1) {
893 date_fail:
894 error_report("invalid date format");
895 error_printf("valid formats: "
896 "'2006-06-17T16:01:21' or '2006-06-17'\n");
897 exit(1);
899 rtc_date_offset = qemu_time() - rtc_start_date;
903 static void configure_rtc(QemuOpts *opts)
905 const char *value;
907 value = qemu_opt_get(opts, "base");
908 if (value) {
909 if (!strcmp(value, "utc")) {
910 rtc_utc = 1;
911 } else if (!strcmp(value, "localtime")) {
912 Error *blocker = NULL;
913 rtc_utc = 0;
914 error_setg(&blocker, QERR_REPLAY_NOT_SUPPORTED,
915 "-rtc base=localtime");
916 replay_add_blocker(blocker);
917 } else {
918 configure_rtc_date_offset(value, 0);
921 value = qemu_opt_get(opts, "clock");
922 if (value) {
923 if (!strcmp(value, "host")) {
924 rtc_clock = QEMU_CLOCK_HOST;
925 } else if (!strcmp(value, "rt")) {
926 rtc_clock = QEMU_CLOCK_REALTIME;
927 } else if (!strcmp(value, "vm")) {
928 rtc_clock = QEMU_CLOCK_VIRTUAL;
929 } else {
930 error_report("invalid option value '%s'", value);
931 exit(1);
934 value = qemu_opt_get(opts, "driftfix");
935 if (value) {
936 if (!strcmp(value, "slew")) {
937 static GlobalProperty slew_lost_ticks = {
938 .driver = "mc146818rtc",
939 .property = "lost_tick_policy",
940 .value = "slew",
943 qdev_prop_register_global(&slew_lost_ticks);
944 } else if (!strcmp(value, "none")) {
945 /* discard is default */
946 } else {
947 error_report("invalid option value '%s'", value);
948 exit(1);
953 /***********************************************************/
954 /* Bluetooth support */
955 static int nb_hcis;
956 static int cur_hci;
957 static struct HCIInfo *hci_table[MAX_NICS];
959 struct HCIInfo *qemu_next_hci(void)
961 if (cur_hci == nb_hcis)
962 return &null_hci;
964 return hci_table[cur_hci++];
967 static int bt_hci_parse(const char *str)
969 struct HCIInfo *hci;
970 bdaddr_t bdaddr;
972 if (nb_hcis >= MAX_NICS) {
973 error_report("too many bluetooth HCIs (max %i)", MAX_NICS);
974 return -1;
977 hci = hci_init(str);
978 if (!hci)
979 return -1;
981 bdaddr.b[0] = 0x52;
982 bdaddr.b[1] = 0x54;
983 bdaddr.b[2] = 0x00;
984 bdaddr.b[3] = 0x12;
985 bdaddr.b[4] = 0x34;
986 bdaddr.b[5] = 0x56 + nb_hcis;
987 hci->bdaddr_set(hci, bdaddr.b);
989 hci_table[nb_hcis++] = hci;
991 return 0;
994 static void bt_vhci_add(int vlan_id)
996 struct bt_scatternet_s *vlan = qemu_find_bt_vlan(vlan_id);
998 if (!vlan->slave)
999 error_report("warning: adding a VHCI to an empty scatternet %i",
1000 vlan_id);
1002 bt_vhci_init(bt_new_hci(vlan));
1005 static struct bt_device_s *bt_device_add(const char *opt)
1007 struct bt_scatternet_s *vlan;
1008 int vlan_id = 0;
1009 char *endp = strstr(opt, ",vlan=");
1010 int len = (endp ? endp - opt : strlen(opt)) + 1;
1011 char devname[10];
1013 pstrcpy(devname, MIN(sizeof(devname), len), opt);
1015 if (endp) {
1016 vlan_id = strtol(endp + 6, &endp, 0);
1017 if (*endp) {
1018 error_report("unrecognised bluetooth vlan Id");
1019 return 0;
1023 vlan = qemu_find_bt_vlan(vlan_id);
1025 if (!vlan->slave)
1026 error_report("warning: adding a slave device to an empty scatternet %i",
1027 vlan_id);
1029 if (!strcmp(devname, "keyboard"))
1030 return bt_keyboard_init(vlan);
1032 error_report("unsupported bluetooth device '%s'", devname);
1033 return 0;
1036 static int bt_parse(const char *opt)
1038 const char *endp, *p;
1039 int vlan;
1041 if (strstart(opt, "hci", &endp)) {
1042 if (!*endp || *endp == ',') {
1043 if (*endp)
1044 if (!strstart(endp, ",vlan=", 0))
1045 opt = endp + 1;
1047 return bt_hci_parse(opt);
1049 } else if (strstart(opt, "vhci", &endp)) {
1050 if (!*endp || *endp == ',') {
1051 if (*endp) {
1052 if (strstart(endp, ",vlan=", &p)) {
1053 vlan = strtol(p, (char **) &endp, 0);
1054 if (*endp) {
1055 error_report("bad scatternet '%s'", p);
1056 return 1;
1058 } else {
1059 error_report("bad parameter '%s'", endp + 1);
1060 return 1;
1062 } else
1063 vlan = 0;
1065 bt_vhci_add(vlan);
1066 return 0;
1068 } else if (strstart(opt, "device:", &endp))
1069 return !bt_device_add(endp);
1071 error_report("bad bluetooth parameter '%s'", opt);
1072 return 1;
1075 static int parse_sandbox(void *opaque, QemuOpts *opts, Error **errp)
1077 /* FIXME: change this to true for 1.3 */
1078 if (qemu_opt_get_bool(opts, "enable", false)) {
1079 #ifdef CONFIG_SECCOMP
1080 if (seccomp_start() < 0) {
1081 error_report("failed to install seccomp syscall filter "
1082 "in the kernel");
1083 return -1;
1085 #else
1086 error_report("seccomp support is disabled");
1087 return -1;
1088 #endif
1091 return 0;
1094 static int parse_name(void *opaque, QemuOpts *opts, Error **errp)
1096 const char *proc_name;
1098 if (qemu_opt_get(opts, "debug-threads")) {
1099 qemu_thread_naming(qemu_opt_get_bool(opts, "debug-threads", false));
1101 qemu_name = qemu_opt_get(opts, "guest");
1103 proc_name = qemu_opt_get(opts, "process");
1104 if (proc_name) {
1105 os_set_proc_name(proc_name);
1108 return 0;
1111 bool defaults_enabled(void)
1113 return has_defaults;
1116 #ifndef _WIN32
1117 static int parse_add_fd(void *opaque, QemuOpts *opts, Error **errp)
1119 int fd, dupfd, flags;
1120 int64_t fdset_id;
1121 const char *fd_opaque = NULL;
1122 AddfdInfo *fdinfo;
1124 fd = qemu_opt_get_number(opts, "fd", -1);
1125 fdset_id = qemu_opt_get_number(opts, "set", -1);
1126 fd_opaque = qemu_opt_get(opts, "opaque");
1128 if (fd < 0) {
1129 error_report("fd option is required and must be non-negative");
1130 return -1;
1133 if (fd <= STDERR_FILENO) {
1134 error_report("fd cannot be a standard I/O stream");
1135 return -1;
1139 * All fds inherited across exec() necessarily have FD_CLOEXEC
1140 * clear, while qemu sets FD_CLOEXEC on all other fds used internally.
1142 flags = fcntl(fd, F_GETFD);
1143 if (flags == -1 || (flags & FD_CLOEXEC)) {
1144 error_report("fd is not valid or already in use");
1145 return -1;
1148 if (fdset_id < 0) {
1149 error_report("set option is required and must be non-negative");
1150 return -1;
1153 #ifdef F_DUPFD_CLOEXEC
1154 dupfd = fcntl(fd, F_DUPFD_CLOEXEC, 0);
1155 #else
1156 dupfd = dup(fd);
1157 if (dupfd != -1) {
1158 qemu_set_cloexec(dupfd);
1160 #endif
1161 if (dupfd == -1) {
1162 error_report("error duplicating fd: %s", strerror(errno));
1163 return -1;
1166 /* add the duplicate fd, and optionally the opaque string, to the fd set */
1167 fdinfo = monitor_fdset_add_fd(dupfd, true, fdset_id, !!fd_opaque, fd_opaque,
1168 &error_abort);
1169 g_free(fdinfo);
1171 return 0;
1174 static int cleanup_add_fd(void *opaque, QemuOpts *opts, Error **errp)
1176 int fd;
1178 fd = qemu_opt_get_number(opts, "fd", -1);
1179 close(fd);
1181 return 0;
1183 #endif
1185 /***********************************************************/
1186 /* QEMU Block devices */
1188 #define HD_OPTS "media=disk"
1189 #define CDROM_OPTS "media=cdrom"
1190 #define FD_OPTS ""
1191 #define PFLASH_OPTS ""
1192 #define MTD_OPTS ""
1193 #define SD_OPTS ""
1195 static int drive_init_func(void *opaque, QemuOpts *opts, Error **errp)
1197 BlockInterfaceType *block_default_type = opaque;
1199 return drive_new(opts, *block_default_type) == NULL;
1202 static int drive_enable_snapshot(void *opaque, QemuOpts *opts, Error **errp)
1204 if (qemu_opt_get(opts, "snapshot") == NULL) {
1205 qemu_opt_set(opts, "snapshot", "on", &error_abort);
1207 return 0;
1210 static void default_drive(int enable, int snapshot, BlockInterfaceType type,
1211 int index, const char *optstr)
1213 QemuOpts *opts;
1214 DriveInfo *dinfo;
1216 if (!enable || drive_get_by_index(type, index)) {
1217 return;
1220 opts = drive_add(type, index, NULL, optstr);
1221 if (snapshot) {
1222 drive_enable_snapshot(NULL, opts, NULL);
1225 dinfo = drive_new(opts, type);
1226 if (!dinfo) {
1227 exit(1);
1229 dinfo->is_default = true;
1233 static QemuOptsList qemu_smp_opts = {
1234 .name = "smp-opts",
1235 .implied_opt_name = "cpus",
1236 .merge_lists = true,
1237 .head = QTAILQ_HEAD_INITIALIZER(qemu_smp_opts.head),
1238 .desc = {
1240 .name = "cpus",
1241 .type = QEMU_OPT_NUMBER,
1242 }, {
1243 .name = "sockets",
1244 .type = QEMU_OPT_NUMBER,
1245 }, {
1246 .name = "cores",
1247 .type = QEMU_OPT_NUMBER,
1248 }, {
1249 .name = "threads",
1250 .type = QEMU_OPT_NUMBER,
1251 }, {
1252 .name = "maxcpus",
1253 .type = QEMU_OPT_NUMBER,
1255 { /*End of list */ }
1259 static void smp_parse(QemuOpts *opts)
1261 if (opts) {
1262 unsigned cpus = qemu_opt_get_number(opts, "cpus", 0);
1263 unsigned sockets = qemu_opt_get_number(opts, "sockets", 0);
1264 unsigned cores = qemu_opt_get_number(opts, "cores", 0);
1265 unsigned threads = qemu_opt_get_number(opts, "threads", 0);
1267 /* compute missing values, prefer sockets over cores over threads */
1268 if (cpus == 0 || sockets == 0) {
1269 sockets = sockets > 0 ? sockets : 1;
1270 cores = cores > 0 ? cores : 1;
1271 threads = threads > 0 ? threads : 1;
1272 if (cpus == 0) {
1273 cpus = cores * threads * sockets;
1275 } else if (cores == 0) {
1276 threads = threads > 0 ? threads : 1;
1277 cores = cpus / (sockets * threads);
1278 cores = cores > 0 ? cores : 1;
1279 } else if (threads == 0) {
1280 threads = cpus / (cores * sockets);
1281 threads = threads > 0 ? threads : 1;
1282 } else if (sockets * cores * threads < cpus) {
1283 error_report("cpu topology: "
1284 "sockets (%u) * cores (%u) * threads (%u) < "
1285 "smp_cpus (%u)",
1286 sockets, cores, threads, cpus);
1287 exit(1);
1290 max_cpus = qemu_opt_get_number(opts, "maxcpus", cpus);
1292 if (max_cpus < cpus) {
1293 error_report("maxcpus must be equal to or greater than smp");
1294 exit(1);
1297 if (sockets * cores * threads > max_cpus) {
1298 error_report("cpu topology: "
1299 "sockets (%u) * cores (%u) * threads (%u) > "
1300 "maxcpus (%u)",
1301 sockets, cores, threads, max_cpus);
1302 exit(1);
1305 smp_cpus = cpus;
1306 smp_cores = cores;
1307 smp_threads = threads;
1310 if (smp_cpus > 1) {
1311 Error *blocker = NULL;
1312 error_setg(&blocker, QERR_REPLAY_NOT_SUPPORTED, "smp");
1313 replay_add_blocker(blocker);
1317 static void realtime_init(void)
1319 if (enable_mlock) {
1320 if (os_mlock() < 0) {
1321 error_report("locking memory failed");
1322 exit(1);
1328 static void configure_msg(QemuOpts *opts)
1330 enable_timestamp_msg = qemu_opt_get_bool(opts, "timestamp", true);
1333 /***********************************************************/
1334 /* Semihosting */
1336 typedef struct SemihostingConfig {
1337 bool enabled;
1338 SemihostingTarget target;
1339 const char **argv;
1340 int argc;
1341 const char *cmdline; /* concatenated argv */
1342 } SemihostingConfig;
1344 static SemihostingConfig semihosting;
1346 bool semihosting_enabled(void)
1348 return semihosting.enabled;
1351 SemihostingTarget semihosting_get_target(void)
1353 return semihosting.target;
1356 const char *semihosting_get_arg(int i)
1358 if (i >= semihosting.argc) {
1359 return NULL;
1361 return semihosting.argv[i];
1364 int semihosting_get_argc(void)
1366 return semihosting.argc;
1369 const char *semihosting_get_cmdline(void)
1371 if (semihosting.cmdline == NULL && semihosting.argc > 0) {
1372 semihosting.cmdline = g_strjoinv(" ", (gchar **)semihosting.argv);
1374 return semihosting.cmdline;
1377 static int add_semihosting_arg(void *opaque,
1378 const char *name, const char *val,
1379 Error **errp)
1381 SemihostingConfig *s = opaque;
1382 if (strcmp(name, "arg") == 0) {
1383 s->argc++;
1384 /* one extra element as g_strjoinv() expects NULL-terminated array */
1385 s->argv = g_realloc(s->argv, (s->argc + 1) * sizeof(void *));
1386 s->argv[s->argc - 1] = val;
1387 s->argv[s->argc] = NULL;
1389 return 0;
1392 /* Use strings passed via -kernel/-append to initialize semihosting.argv[] */
1393 static inline void semihosting_arg_fallback(const char *file, const char *cmd)
1395 char *cmd_token;
1397 /* argv[0] */
1398 add_semihosting_arg(&semihosting, "arg", file, NULL);
1400 /* split -append and initialize argv[1..n] */
1401 cmd_token = strtok(g_strdup(cmd), " ");
1402 while (cmd_token) {
1403 add_semihosting_arg(&semihosting, "arg", cmd_token, NULL);
1404 cmd_token = strtok(NULL, " ");
1408 /* Now we still need this for compatibility with XEN. */
1409 bool has_igd_gfx_passthru;
1410 static void igd_gfx_passthru(void)
1412 has_igd_gfx_passthru = current_machine->igd_gfx_passthru;
1415 /***********************************************************/
1416 /* USB devices */
1418 static int usb_device_add(const char *devname)
1420 USBDevice *dev = NULL;
1421 #ifndef CONFIG_LINUX
1422 const char *p;
1423 #endif
1425 if (!machine_usb(current_machine)) {
1426 return -1;
1429 /* drivers with .usbdevice_name entry in USBDeviceInfo */
1430 dev = usbdevice_create(devname);
1431 if (dev)
1432 goto done;
1434 /* the other ones */
1435 #ifndef CONFIG_LINUX
1436 /* only the linux version is qdev-ified, usb-bsd still needs this */
1437 if (strstart(devname, "host:", &p)) {
1438 dev = usb_host_device_open(usb_bus_find(-1), p);
1440 #endif
1441 if (!dev)
1442 return -1;
1444 done:
1445 return 0;
1448 static int usb_device_del(const char *devname)
1450 int bus_num, addr;
1451 const char *p;
1453 if (strstart(devname, "host:", &p)) {
1454 return -1;
1457 if (!machine_usb(current_machine)) {
1458 return -1;
1461 p = strchr(devname, '.');
1462 if (!p)
1463 return -1;
1464 bus_num = strtoul(devname, NULL, 0);
1465 addr = strtoul(p + 1, NULL, 0);
1467 return usb_device_delete_addr(bus_num, addr);
1470 static int usb_parse(const char *cmdline)
1472 int r;
1473 r = usb_device_add(cmdline);
1474 if (r < 0) {
1475 error_report("could not add USB device '%s'", cmdline);
1477 return r;
1480 void hmp_usb_add(Monitor *mon, const QDict *qdict)
1482 const char *devname = qdict_get_str(qdict, "devname");
1483 if (usb_device_add(devname) < 0) {
1484 error_report("could not add USB device '%s'", devname);
1488 void hmp_usb_del(Monitor *mon, const QDict *qdict)
1490 const char *devname = qdict_get_str(qdict, "devname");
1491 if (usb_device_del(devname) < 0) {
1492 error_report("could not delete USB device '%s'", devname);
1496 /***********************************************************/
1497 /* machine registration */
1499 MachineState *current_machine;
1501 static MachineClass *find_machine(const char *name)
1503 GSList *el, *machines = object_class_get_list(TYPE_MACHINE, false);
1504 MachineClass *mc = NULL;
1506 for (el = machines; el; el = el->next) {
1507 MachineClass *temp = el->data;
1509 if (!strcmp(temp->name, name)) {
1510 mc = temp;
1511 break;
1513 if (temp->alias &&
1514 !strcmp(temp->alias, name)) {
1515 mc = temp;
1516 break;
1520 g_slist_free(machines);
1521 return mc;
1524 MachineClass *find_default_machine(void)
1526 GSList *el, *machines = object_class_get_list(TYPE_MACHINE, false);
1527 MachineClass *mc = NULL;
1529 for (el = machines; el; el = el->next) {
1530 MachineClass *temp = el->data;
1532 if (temp->is_default) {
1533 mc = temp;
1534 break;
1538 g_slist_free(machines);
1539 return mc;
1542 MachineInfoList *qmp_query_machines(Error **errp)
1544 GSList *el, *machines = object_class_get_list(TYPE_MACHINE, false);
1545 MachineInfoList *mach_list = NULL;
1547 for (el = machines; el; el = el->next) {
1548 MachineClass *mc = el->data;
1549 MachineInfoList *entry;
1550 MachineInfo *info;
1552 info = g_malloc0(sizeof(*info));
1553 if (mc->is_default) {
1554 info->has_is_default = true;
1555 info->is_default = true;
1558 if (mc->alias) {
1559 info->has_alias = true;
1560 info->alias = g_strdup(mc->alias);
1563 info->name = g_strdup(mc->name);
1564 info->cpu_max = !mc->max_cpus ? 1 : mc->max_cpus;
1565 info->hotpluggable_cpus = !!mc->query_hotpluggable_cpus;
1567 entry = g_malloc0(sizeof(*entry));
1568 entry->value = info;
1569 entry->next = mach_list;
1570 mach_list = entry;
1573 g_slist_free(machines);
1574 return mach_list;
1577 static int machine_help_func(QemuOpts *opts, MachineState *machine)
1579 ObjectProperty *prop;
1580 ObjectPropertyIterator iter;
1582 if (!qemu_opt_has_help_opt(opts)) {
1583 return 0;
1586 object_property_iter_init(&iter, OBJECT(machine));
1587 while ((prop = object_property_iter_next(&iter))) {
1588 if (!prop->set) {
1589 continue;
1592 error_printf("%s.%s=%s", MACHINE_GET_CLASS(machine)->name,
1593 prop->name, prop->type);
1594 if (prop->description) {
1595 error_printf(" (%s)\n", prop->description);
1596 } else {
1597 error_printf("\n");
1601 return 1;
1604 /***********************************************************/
1605 /* main execution loop */
1607 struct vm_change_state_entry {
1608 VMChangeStateHandler *cb;
1609 void *opaque;
1610 QLIST_ENTRY (vm_change_state_entry) entries;
1613 static QLIST_HEAD(vm_change_state_head, vm_change_state_entry) vm_change_state_head;
1615 VMChangeStateEntry *qemu_add_vm_change_state_handler(VMChangeStateHandler *cb,
1616 void *opaque)
1618 VMChangeStateEntry *e;
1620 e = g_malloc0(sizeof (*e));
1622 e->cb = cb;
1623 e->opaque = opaque;
1624 QLIST_INSERT_HEAD(&vm_change_state_head, e, entries);
1625 return e;
1628 void qemu_del_vm_change_state_handler(VMChangeStateEntry *e)
1630 QLIST_REMOVE (e, entries);
1631 g_free (e);
1634 void vm_state_notify(int running, RunState state)
1636 VMChangeStateEntry *e, *next;
1638 trace_vm_state_notify(running, state);
1640 QLIST_FOREACH_SAFE(e, &vm_change_state_head, entries, next) {
1641 e->cb(e->opaque, running, state);
1645 static int reset_requested;
1646 static int shutdown_requested, shutdown_signal = -1;
1647 static pid_t shutdown_pid;
1648 static int powerdown_requested;
1649 static int debug_requested;
1650 static int suspend_requested;
1651 static WakeupReason wakeup_reason;
1652 static NotifierList powerdown_notifiers =
1653 NOTIFIER_LIST_INITIALIZER(powerdown_notifiers);
1654 static NotifierList suspend_notifiers =
1655 NOTIFIER_LIST_INITIALIZER(suspend_notifiers);
1656 static NotifierList wakeup_notifiers =
1657 NOTIFIER_LIST_INITIALIZER(wakeup_notifiers);
1658 static uint32_t wakeup_reason_mask = ~(1 << QEMU_WAKEUP_REASON_NONE);
1660 int qemu_shutdown_requested_get(void)
1662 return shutdown_requested;
1665 int qemu_reset_requested_get(void)
1667 return reset_requested;
1670 static int qemu_shutdown_requested(void)
1672 return atomic_xchg(&shutdown_requested, 0);
1675 static void qemu_kill_report(void)
1677 if (!qtest_driver() && shutdown_signal != -1) {
1678 if (shutdown_pid == 0) {
1679 /* This happens for eg ^C at the terminal, so it's worth
1680 * avoiding printing an odd message in that case.
1682 error_report("terminating on signal %d", shutdown_signal);
1683 } else {
1684 char *shutdown_cmd = qemu_get_pid_name(shutdown_pid);
1686 error_report("terminating on signal %d from pid " FMT_pid " (%s)",
1687 shutdown_signal, shutdown_pid,
1688 shutdown_cmd ? shutdown_cmd : "<unknown process>");
1689 g_free(shutdown_cmd);
1691 shutdown_signal = -1;
1695 static int qemu_reset_requested(void)
1697 int r = reset_requested;
1698 if (r && replay_checkpoint(CHECKPOINT_RESET_REQUESTED)) {
1699 reset_requested = 0;
1700 return r;
1702 return false;
1705 static int qemu_suspend_requested(void)
1707 int r = suspend_requested;
1708 if (r && replay_checkpoint(CHECKPOINT_SUSPEND_REQUESTED)) {
1709 suspend_requested = 0;
1710 return r;
1712 return false;
1715 static WakeupReason qemu_wakeup_requested(void)
1717 return wakeup_reason;
1720 static int qemu_powerdown_requested(void)
1722 int r = powerdown_requested;
1723 powerdown_requested = 0;
1724 return r;
1727 static int qemu_debug_requested(void)
1729 int r = debug_requested;
1730 debug_requested = 0;
1731 return r;
1734 void qemu_system_reset(bool report)
1736 MachineClass *mc;
1738 mc = current_machine ? MACHINE_GET_CLASS(current_machine) : NULL;
1740 cpu_synchronize_all_states();
1742 if (mc && mc->reset) {
1743 mc->reset();
1744 } else {
1745 qemu_devices_reset();
1747 if (report) {
1748 qapi_event_send_reset(&error_abort);
1750 cpu_synchronize_all_post_reset();
1753 void qemu_system_guest_panicked(void)
1755 if (current_cpu) {
1756 current_cpu->crash_occurred = true;
1758 qapi_event_send_guest_panicked(GUEST_PANIC_ACTION_PAUSE, &error_abort);
1759 vm_stop(RUN_STATE_GUEST_PANICKED);
1760 if (!no_shutdown) {
1761 qapi_event_send_guest_panicked(GUEST_PANIC_ACTION_POWEROFF,
1762 &error_abort);
1763 qemu_system_shutdown_request();
1767 void qemu_system_reset_request(void)
1769 if (no_reboot) {
1770 shutdown_requested = 1;
1771 } else {
1772 reset_requested = 1;
1774 cpu_stop_current();
1775 qemu_notify_event();
1778 static void qemu_system_suspend(void)
1780 pause_all_vcpus();
1781 notifier_list_notify(&suspend_notifiers, NULL);
1782 runstate_set(RUN_STATE_SUSPENDED);
1783 qapi_event_send_suspend(&error_abort);
1786 void qemu_system_suspend_request(void)
1788 if (runstate_check(RUN_STATE_SUSPENDED)) {
1789 return;
1791 suspend_requested = 1;
1792 cpu_stop_current();
1793 qemu_notify_event();
1796 void qemu_register_suspend_notifier(Notifier *notifier)
1798 notifier_list_add(&suspend_notifiers, notifier);
1801 void qemu_system_wakeup_request(WakeupReason reason)
1803 trace_system_wakeup_request(reason);
1805 if (!runstate_check(RUN_STATE_SUSPENDED)) {
1806 return;
1808 if (!(wakeup_reason_mask & (1 << reason))) {
1809 return;
1811 runstate_set(RUN_STATE_RUNNING);
1812 wakeup_reason = reason;
1813 qemu_notify_event();
1816 void qemu_system_wakeup_enable(WakeupReason reason, bool enabled)
1818 if (enabled) {
1819 wakeup_reason_mask |= (1 << reason);
1820 } else {
1821 wakeup_reason_mask &= ~(1 << reason);
1825 void qemu_register_wakeup_notifier(Notifier *notifier)
1827 notifier_list_add(&wakeup_notifiers, notifier);
1830 void qemu_system_killed(int signal, pid_t pid)
1832 shutdown_signal = signal;
1833 shutdown_pid = pid;
1834 no_shutdown = 0;
1836 /* Cannot call qemu_system_shutdown_request directly because
1837 * we are in a signal handler.
1839 shutdown_requested = 1;
1840 qemu_notify_event();
1843 void qemu_system_shutdown_request(void)
1845 trace_qemu_system_shutdown_request();
1846 replay_shutdown_request();
1847 shutdown_requested = 1;
1848 qemu_notify_event();
1851 static void qemu_system_powerdown(void)
1853 qapi_event_send_powerdown(&error_abort);
1854 notifier_list_notify(&powerdown_notifiers, NULL);
1857 void qemu_system_powerdown_request(void)
1859 trace_qemu_system_powerdown_request();
1860 powerdown_requested = 1;
1861 qemu_notify_event();
1864 void qemu_register_powerdown_notifier(Notifier *notifier)
1866 notifier_list_add(&powerdown_notifiers, notifier);
1869 void qemu_system_debug_request(void)
1871 debug_requested = 1;
1872 qemu_notify_event();
1875 static bool main_loop_should_exit(void)
1877 RunState r;
1878 if (qemu_debug_requested()) {
1879 vm_stop(RUN_STATE_DEBUG);
1881 if (qemu_suspend_requested()) {
1882 qemu_system_suspend();
1884 if (qemu_shutdown_requested()) {
1885 qemu_kill_report();
1886 qapi_event_send_shutdown(&error_abort);
1887 if (no_shutdown) {
1888 vm_stop(RUN_STATE_SHUTDOWN);
1889 } else {
1890 return true;
1893 if (qemu_reset_requested()) {
1894 pause_all_vcpus();
1895 qemu_system_reset(VMRESET_REPORT);
1896 resume_all_vcpus();
1897 if (!runstate_check(RUN_STATE_RUNNING) &&
1898 !runstate_check(RUN_STATE_INMIGRATE)) {
1899 runstate_set(RUN_STATE_PRELAUNCH);
1902 if (qemu_wakeup_requested()) {
1903 pause_all_vcpus();
1904 qemu_system_reset(VMRESET_SILENT);
1905 notifier_list_notify(&wakeup_notifiers, &wakeup_reason);
1906 wakeup_reason = QEMU_WAKEUP_REASON_NONE;
1907 resume_all_vcpus();
1908 qapi_event_send_wakeup(&error_abort);
1910 if (qemu_powerdown_requested()) {
1911 qemu_system_powerdown();
1913 if (qemu_vmstop_requested(&r)) {
1914 vm_stop(r);
1916 return false;
1919 static void main_loop(void)
1921 bool nonblocking;
1922 int last_io = 0;
1923 #ifdef CONFIG_PROFILER
1924 int64_t ti;
1925 #endif
1926 do {
1927 nonblocking = tcg_enabled() && last_io > 0;
1928 #ifdef CONFIG_PROFILER
1929 ti = profile_getclock();
1930 #endif
1931 last_io = main_loop_wait(nonblocking);
1932 #ifdef CONFIG_PROFILER
1933 dev_time += profile_getclock() - ti;
1934 #endif
1935 } while (!main_loop_should_exit());
1938 static void version(void)
1940 printf("QEMU emulator version " QEMU_VERSION QEMU_PKGVERSION "\n"
1941 QEMU_COPYRIGHT "\n");
1944 static void QEMU_NORETURN help(int exitcode)
1946 version();
1947 printf("usage: %s [options] [disk_image]\n\n"
1948 "'disk_image' is a raw hard disk image for IDE hard disk 0\n\n",
1949 error_get_progname());
1951 #define QEMU_OPTIONS_GENERATE_HELP
1952 #include "qemu-options-wrapper.h"
1954 printf("\nDuring emulation, the following keys are useful:\n"
1955 "ctrl-alt-f toggle full screen\n"
1956 "ctrl-alt-n switch to virtual console 'n'\n"
1957 "ctrl-alt toggle mouse and keyboard grab\n"
1958 "\n"
1959 "When using -nographic, press 'ctrl-a h' to get some help.\n");
1961 exit(exitcode);
1964 #define HAS_ARG 0x0001
1966 typedef struct QEMUOption {
1967 const char *name;
1968 int flags;
1969 int index;
1970 uint32_t arch_mask;
1971 } QEMUOption;
1973 static const QEMUOption qemu_options[] = {
1974 { "h", 0, QEMU_OPTION_h, QEMU_ARCH_ALL },
1975 #define QEMU_OPTIONS_GENERATE_OPTIONS
1976 #include "qemu-options-wrapper.h"
1977 { NULL },
1980 typedef struct VGAInterfaceInfo {
1981 const char *opt_name; /* option name */
1982 const char *name; /* human-readable name */
1983 /* Class names indicating that support is available.
1984 * If no class is specified, the interface is always available */
1985 const char *class_names[2];
1986 } VGAInterfaceInfo;
1988 static VGAInterfaceInfo vga_interfaces[VGA_TYPE_MAX] = {
1989 [VGA_NONE] = {
1990 .opt_name = "none",
1992 [VGA_STD] = {
1993 .opt_name = "std",
1994 .name = "standard VGA",
1995 .class_names = { "VGA", "isa-vga" },
1997 [VGA_CIRRUS] = {
1998 .opt_name = "cirrus",
1999 .name = "Cirrus VGA",
2000 .class_names = { "cirrus-vga", "isa-cirrus-vga" },
2002 [VGA_VMWARE] = {
2003 .opt_name = "vmware",
2004 .name = "VMWare SVGA",
2005 .class_names = { "vmware-svga" },
2007 [VGA_VIRTIO] = {
2008 .opt_name = "virtio",
2009 .name = "Virtio VGA",
2010 .class_names = { "virtio-vga" },
2012 [VGA_QXL] = {
2013 .opt_name = "qxl",
2014 .name = "QXL VGA",
2015 .class_names = { "qxl-vga" },
2017 [VGA_TCX] = {
2018 .opt_name = "tcx",
2019 .name = "TCX framebuffer",
2020 .class_names = { "SUNW,tcx" },
2022 [VGA_CG3] = {
2023 .opt_name = "cg3",
2024 .name = "CG3 framebuffer",
2025 .class_names = { "cgthree" },
2027 [VGA_XENFB] = {
2028 .opt_name = "xenfb",
2032 static bool vga_interface_available(VGAInterfaceType t)
2034 VGAInterfaceInfo *ti = &vga_interfaces[t];
2036 assert(t < VGA_TYPE_MAX);
2037 return !ti->class_names[0] ||
2038 object_class_by_name(ti->class_names[0]) ||
2039 object_class_by_name(ti->class_names[1]);
2042 static void select_vgahw(const char *p)
2044 const char *opts;
2045 int t;
2047 assert(vga_interface_type == VGA_NONE);
2048 for (t = 0; t < VGA_TYPE_MAX; t++) {
2049 VGAInterfaceInfo *ti = &vga_interfaces[t];
2050 if (ti->opt_name && strstart(p, ti->opt_name, &opts)) {
2051 if (!vga_interface_available(t)) {
2052 error_report("%s not available", ti->name);
2053 exit(1);
2055 vga_interface_type = t;
2056 break;
2059 if (t == VGA_TYPE_MAX) {
2060 invalid_vga:
2061 error_report("unknown vga type: %s", p);
2062 exit(1);
2064 while (*opts) {
2065 const char *nextopt;
2067 if (strstart(opts, ",retrace=", &nextopt)) {
2068 opts = nextopt;
2069 if (strstart(opts, "dumb", &nextopt))
2070 vga_retrace_method = VGA_RETRACE_DUMB;
2071 else if (strstart(opts, "precise", &nextopt))
2072 vga_retrace_method = VGA_RETRACE_PRECISE;
2073 else goto invalid_vga;
2074 } else goto invalid_vga;
2075 opts = nextopt;
2079 typedef enum DisplayType {
2080 DT_DEFAULT,
2081 DT_CURSES,
2082 DT_SDL,
2083 DT_COCOA,
2084 DT_GTK,
2085 DT_NONE,
2086 } DisplayType;
2088 static DisplayType select_display(const char *p)
2090 const char *opts;
2091 DisplayType display = DT_DEFAULT;
2093 if (strstart(p, "sdl", &opts)) {
2094 #ifdef CONFIG_SDL
2095 display = DT_SDL;
2096 while (*opts) {
2097 const char *nextopt;
2099 if (strstart(opts, ",frame=", &nextopt)) {
2100 opts = nextopt;
2101 if (strstart(opts, "on", &nextopt)) {
2102 no_frame = 0;
2103 } else if (strstart(opts, "off", &nextopt)) {
2104 no_frame = 1;
2105 } else {
2106 goto invalid_sdl_args;
2108 } else if (strstart(opts, ",alt_grab=", &nextopt)) {
2109 opts = nextopt;
2110 if (strstart(opts, "on", &nextopt)) {
2111 alt_grab = 1;
2112 } else if (strstart(opts, "off", &nextopt)) {
2113 alt_grab = 0;
2114 } else {
2115 goto invalid_sdl_args;
2117 } else if (strstart(opts, ",ctrl_grab=", &nextopt)) {
2118 opts = nextopt;
2119 if (strstart(opts, "on", &nextopt)) {
2120 ctrl_grab = 1;
2121 } else if (strstart(opts, "off", &nextopt)) {
2122 ctrl_grab = 0;
2123 } else {
2124 goto invalid_sdl_args;
2126 } else if (strstart(opts, ",window_close=", &nextopt)) {
2127 opts = nextopt;
2128 if (strstart(opts, "on", &nextopt)) {
2129 no_quit = 0;
2130 } else if (strstart(opts, "off", &nextopt)) {
2131 no_quit = 1;
2132 } else {
2133 goto invalid_sdl_args;
2135 } else if (strstart(opts, ",gl=", &nextopt)) {
2136 opts = nextopt;
2137 if (strstart(opts, "on", &nextopt)) {
2138 request_opengl = 1;
2139 } else if (strstart(opts, "off", &nextopt)) {
2140 request_opengl = 0;
2141 } else {
2142 goto invalid_sdl_args;
2144 } else {
2145 invalid_sdl_args:
2146 error_report("invalid SDL option string");
2147 exit(1);
2149 opts = nextopt;
2151 #else
2152 error_report("SDL support is disabled");
2153 exit(1);
2154 #endif
2155 } else if (strstart(p, "vnc", &opts)) {
2156 if (*opts == '=') {
2157 vnc_parse(opts + 1, &error_fatal);
2158 } else {
2159 error_report("VNC requires a display argument vnc=<display>");
2160 exit(1);
2162 } else if (strstart(p, "curses", &opts)) {
2163 #ifdef CONFIG_CURSES
2164 display = DT_CURSES;
2165 #else
2166 error_report("curses support is disabled");
2167 exit(1);
2168 #endif
2169 } else if (strstart(p, "gtk", &opts)) {
2170 #ifdef CONFIG_GTK
2171 display = DT_GTK;
2172 while (*opts) {
2173 const char *nextopt;
2175 if (strstart(opts, ",grab_on_hover=", &nextopt)) {
2176 opts = nextopt;
2177 if (strstart(opts, "on", &nextopt)) {
2178 grab_on_hover = true;
2179 } else if (strstart(opts, "off", &nextopt)) {
2180 grab_on_hover = false;
2181 } else {
2182 goto invalid_gtk_args;
2184 } else if (strstart(opts, ",gl=", &nextopt)) {
2185 opts = nextopt;
2186 if (strstart(opts, "on", &nextopt)) {
2187 request_opengl = 1;
2188 } else if (strstart(opts, "off", &nextopt)) {
2189 request_opengl = 0;
2190 } else {
2191 goto invalid_gtk_args;
2193 } else {
2194 invalid_gtk_args:
2195 error_report("invalid GTK option string");
2196 exit(1);
2198 opts = nextopt;
2200 #else
2201 error_report("GTK support is disabled");
2202 exit(1);
2203 #endif
2204 } else if (strstart(p, "none", &opts)) {
2205 display = DT_NONE;
2206 } else {
2207 error_report("unknown display type");
2208 exit(1);
2211 return display;
2214 static int balloon_parse(const char *arg)
2216 QemuOpts *opts;
2218 if (strcmp(arg, "none") == 0) {
2219 return 0;
2222 if (!strncmp(arg, "virtio", 6)) {
2223 if (arg[6] == ',') {
2224 /* have params -> parse them */
2225 opts = qemu_opts_parse_noisily(qemu_find_opts("device"), arg + 7,
2226 false);
2227 if (!opts)
2228 return -1;
2229 } else {
2230 /* create empty opts */
2231 opts = qemu_opts_create(qemu_find_opts("device"), NULL, 0,
2232 &error_abort);
2234 qemu_opt_set(opts, "driver", "virtio-balloon", &error_abort);
2235 return 0;
2238 return -1;
2241 #if defined(CONFIG_RUBY)
2242 /* Ruby interface for QEMU. See README.EXT for programming example. */
2243 #warning mit ruby
2244 #include <ruby.h>
2246 static VALUE qemu_open(int argc, VALUE *argv, VALUE klass)
2248 int i;
2249 for (i = 0; i < argc; i++) {
2250 argv[i] = StringValue(argv[i]);
2251 fprintf(stderr, "argv[%d] = %s\n", i, StringValuePtr(argv[i]));
2253 //~ if (rb_scan_args(argc, argv, "11", &file, &vmode) == 1) {
2254 //~ mode = 0666; /* default value */
2255 //~ }
2256 return INT2FIX(argc);
2259 void Init_qemu(void)
2261 printf("%s\n", __func__);
2262 VALUE cQEMU = rb_define_class("QEMU", rb_cObject);
2263 rb_define_singleton_method(cQEMU, "run", qemu_open, -1);
2264 #if 0
2265 rb_define_method();
2266 #endif
2268 #endif /* CONFIG_RUBY */
2270 #if defined(CONFIG_DLL)
2271 BOOL APIENTRY DllMain(HINSTANCE hModule, DWORD ul_reason_for_call, LPVOID data)
2273 return FALSE;
2275 #endif /* CONFIG_DLL */
2277 char *qemu_find_file(int type, const char *name)
2279 int i;
2280 const char *subdir;
2281 char *buf;
2283 /* Try the name as a straight path first */
2284 if (access(name, R_OK) == 0) {
2285 trace_load_file(name, name);
2286 return g_strdup(name);
2289 switch (type) {
2290 case QEMU_FILE_TYPE_BIOS:
2291 subdir = "";
2292 break;
2293 case QEMU_FILE_TYPE_KEYMAP:
2294 subdir = "keymaps/";
2295 break;
2296 default:
2297 abort();
2300 for (i = 0; i < data_dir_idx; i++) {
2301 buf = g_strdup_printf("%s/%s%s", data_dir[i], subdir, name);
2302 if (access(buf, R_OK) == 0) {
2303 trace_load_file(name, buf);
2304 return buf;
2306 g_free(buf);
2308 return NULL;
2311 static inline bool nonempty_str(const char *str)
2313 return str && *str;
2316 static int parse_fw_cfg(void *opaque, QemuOpts *opts, Error **errp)
2318 gchar *buf;
2319 size_t size;
2320 const char *name, *file, *str;
2321 FWCfgState *fw_cfg = (FWCfgState *) opaque;
2323 if (fw_cfg == NULL) {
2324 error_report("fw_cfg device not available");
2325 return -1;
2327 name = qemu_opt_get(opts, "name");
2328 file = qemu_opt_get(opts, "file");
2329 str = qemu_opt_get(opts, "string");
2331 /* we need name and either a file or the content string */
2332 if (!(nonempty_str(name) && (nonempty_str(file) || nonempty_str(str)))) {
2333 error_report("invalid argument(s)");
2334 return -1;
2336 if (nonempty_str(file) && nonempty_str(str)) {
2337 error_report("file and string are mutually exclusive");
2338 return -1;
2340 if (strlen(name) > FW_CFG_MAX_FILE_PATH - 1) {
2341 error_report("name too long (max. %d char)", FW_CFG_MAX_FILE_PATH - 1);
2342 return -1;
2344 if (strncmp(name, "opt/", 4) != 0) {
2345 error_report("warning: externally provided fw_cfg item names "
2346 "should be prefixed with \"opt/\"");
2348 if (nonempty_str(str)) {
2349 size = strlen(str); /* NUL terminator NOT included in fw_cfg blob */
2350 buf = g_memdup(str, size);
2351 } else {
2352 if (!g_file_get_contents(file, &buf, &size, NULL)) {
2353 error_report("can't load %s", file);
2354 return -1;
2357 /* For legacy, keep user files in a specific global order. */
2358 fw_cfg_set_order_override(fw_cfg, FW_CFG_ORDER_OVERRIDE_USER);
2359 fw_cfg_add_file(fw_cfg, name, buf, size);
2360 fw_cfg_reset_order_override(fw_cfg);
2361 return 0;
2364 static int device_help_func(void *opaque, QemuOpts *opts, Error **errp)
2366 return qdev_device_help(opts);
2369 static int device_init_func(void *opaque, QemuOpts *opts, Error **errp)
2371 Error *err = NULL;
2372 DeviceState *dev;
2374 dev = qdev_device_add(opts, &err);
2375 if (!dev) {
2376 error_report_err(err);
2377 return -1;
2379 object_unref(OBJECT(dev));
2380 return 0;
2383 static int chardev_init_func(void *opaque, QemuOpts *opts, Error **errp)
2385 Error *local_err = NULL;
2387 qemu_chr_new_from_opts(opts, &local_err);
2388 if (local_err) {
2389 error_report_err(local_err);
2390 return -1;
2392 return 0;
2395 #ifdef CONFIG_VIRTFS
2396 static int fsdev_init_func(void *opaque, QemuOpts *opts, Error **errp)
2398 return qemu_fsdev_add(opts);
2400 #endif
2402 static int mon_init_func(void *opaque, QemuOpts *opts, Error **errp)
2404 CharDriverState *chr;
2405 const char *chardev;
2406 const char *mode;
2407 int flags;
2409 mode = qemu_opt_get(opts, "mode");
2410 if (mode == NULL) {
2411 mode = "readline";
2413 if (strcmp(mode, "readline") == 0) {
2414 flags = MONITOR_USE_READLINE;
2415 } else if (strcmp(mode, "control") == 0) {
2416 flags = MONITOR_USE_CONTROL;
2417 } else {
2418 error_report("unknown monitor mode \"%s\"", mode);
2419 exit(1);
2422 if (qemu_opt_get_bool(opts, "pretty", 0))
2423 flags |= MONITOR_USE_PRETTY;
2425 if (qemu_opt_get_bool(opts, "default", 0)) {
2426 error_report("option 'default' does nothing and is deprecated");
2429 chardev = qemu_opt_get(opts, "chardev");
2430 chr = qemu_chr_find(chardev);
2431 if (chr == NULL) {
2432 error_report("chardev \"%s\" not found", chardev);
2433 exit(1);
2436 monitor_init(chr, flags);
2437 return 0;
2440 static void monitor_parse(const char *optarg, const char *mode, bool pretty)
2442 static int monitor_device_index = 0;
2443 QemuOpts *opts;
2444 const char *p;
2445 char label[32];
2447 if (strstart(optarg, "chardev:", &p)) {
2448 snprintf(label, sizeof(label), "%s", p);
2449 } else {
2450 snprintf(label, sizeof(label), "compat_monitor%d",
2451 monitor_device_index);
2452 opts = qemu_chr_parse_compat(label, optarg);
2453 if (!opts) {
2454 error_report("parse error: %s", optarg);
2455 exit(1);
2459 opts = qemu_opts_create(qemu_find_opts("mon"), label, 1, &error_fatal);
2460 qemu_opt_set(opts, "mode", mode, &error_abort);
2461 qemu_opt_set(opts, "chardev", label, &error_abort);
2462 qemu_opt_set_bool(opts, "pretty", pretty, &error_abort);
2463 monitor_device_index++;
2466 struct device_config {
2467 enum {
2468 DEV_USB, /* -usbdevice */
2469 DEV_BT, /* -bt */
2470 DEV_SERIAL, /* -serial */
2471 DEV_PARALLEL, /* -parallel */
2472 DEV_VIRTCON, /* -virtioconsole */
2473 DEV_DEBUGCON, /* -debugcon */
2474 DEV_GDB, /* -gdb, -s */
2475 DEV_SCLP, /* s390 sclp */
2476 } type;
2477 const char *cmdline;
2478 Location loc;
2479 QTAILQ_ENTRY(device_config) next;
2482 static QTAILQ_HEAD(, device_config) device_configs =
2483 QTAILQ_HEAD_INITIALIZER(device_configs);
2485 static void add_device_config(int type, const char *cmdline)
2487 struct device_config *conf;
2489 conf = g_malloc0(sizeof(*conf));
2490 conf->type = type;
2491 conf->cmdline = cmdline;
2492 loc_save(&conf->loc);
2493 QTAILQ_INSERT_TAIL(&device_configs, conf, next);
2496 static int foreach_device_config(int type, int (*func)(const char *cmdline))
2498 struct device_config *conf;
2499 int rc;
2501 QTAILQ_FOREACH(conf, &device_configs, next) {
2502 if (conf->type != type)
2503 continue;
2504 loc_push_restore(&conf->loc);
2505 rc = func(conf->cmdline);
2506 loc_pop(&conf->loc);
2507 if (rc) {
2508 return rc;
2511 return 0;
2514 static int serial_parse(const char *devname)
2516 static int index = 0;
2517 char label[32];
2519 if (strcmp(devname, "none") == 0)
2520 return 0;
2521 if (index == MAX_SERIAL_PORTS) {
2522 error_report("too many serial ports");
2523 exit(1);
2525 snprintf(label, sizeof(label), "serial%d", index);
2526 serial_hds[index] = qemu_chr_new(label, devname);
2527 if (!serial_hds[index]) {
2528 error_report("could not connect serial device"
2529 " to character backend '%s'", devname);
2530 return -1;
2532 index++;
2533 return 0;
2536 static int parallel_parse(const char *devname)
2538 static int index = 0;
2539 char label[32];
2541 if (strcmp(devname, "none") == 0)
2542 return 0;
2543 if (index == MAX_PARALLEL_PORTS) {
2544 error_report("too many parallel ports");
2545 exit(1);
2547 snprintf(label, sizeof(label), "parallel%d", index);
2548 parallel_hds[index] = qemu_chr_new(label, devname);
2549 if (!parallel_hds[index]) {
2550 error_report("could not connect parallel device"
2551 " to character backend '%s'", devname);
2552 return -1;
2554 index++;
2555 return 0;
2558 static int virtcon_parse(const char *devname)
2560 QemuOptsList *device = qemu_find_opts("device");
2561 static int index = 0;
2562 char label[32];
2563 QemuOpts *bus_opts, *dev_opts;
2565 if (strcmp(devname, "none") == 0)
2566 return 0;
2567 if (index == MAX_VIRTIO_CONSOLES) {
2568 error_report("too many virtio consoles");
2569 exit(1);
2572 bus_opts = qemu_opts_create(device, NULL, 0, &error_abort);
2573 qemu_opt_set(bus_opts, "driver", "virtio-serial", &error_abort);
2575 dev_opts = qemu_opts_create(device, NULL, 0, &error_abort);
2576 qemu_opt_set(dev_opts, "driver", "virtconsole", &error_abort);
2578 snprintf(label, sizeof(label), "virtcon%d", index);
2579 virtcon_hds[index] = qemu_chr_new(label, devname);
2580 if (!virtcon_hds[index]) {
2581 error_report("could not connect virtio console"
2582 " to character backend '%s'", devname);
2583 return -1;
2585 qemu_opt_set(dev_opts, "chardev", label, &error_abort);
2587 index++;
2588 return 0;
2591 static int sclp_parse(const char *devname)
2593 QemuOptsList *device = qemu_find_opts("device");
2594 static int index = 0;
2595 char label[32];
2596 QemuOpts *dev_opts;
2598 if (strcmp(devname, "none") == 0) {
2599 return 0;
2601 if (index == MAX_SCLP_CONSOLES) {
2602 error_report("too many sclp consoles");
2603 exit(1);
2606 assert(arch_type == QEMU_ARCH_S390X);
2608 dev_opts = qemu_opts_create(device, NULL, 0, NULL);
2609 qemu_opt_set(dev_opts, "driver", "sclpconsole", &error_abort);
2611 snprintf(label, sizeof(label), "sclpcon%d", index);
2612 sclp_hds[index] = qemu_chr_new(label, devname);
2613 if (!sclp_hds[index]) {
2614 error_report("could not connect sclp console"
2615 " to character backend '%s'", devname);
2616 return -1;
2618 qemu_opt_set(dev_opts, "chardev", label, &error_abort);
2620 index++;
2621 return 0;
2624 static int debugcon_parse(const char *devname)
2626 QemuOpts *opts;
2628 if (!qemu_chr_new("debugcon", devname)) {
2629 exit(1);
2631 opts = qemu_opts_create(qemu_find_opts("device"), "debugcon", 1, NULL);
2632 if (!opts) {
2633 error_report("already have a debugcon device");
2634 exit(1);
2636 qemu_opt_set(opts, "driver", "isa-debugcon", &error_abort);
2637 qemu_opt_set(opts, "chardev", "debugcon", &error_abort);
2638 return 0;
2641 static gint machine_class_cmp(gconstpointer a, gconstpointer b)
2643 const MachineClass *mc1 = a, *mc2 = b;
2644 int res;
2646 if (mc1->family == NULL) {
2647 if (mc2->family == NULL) {
2648 /* Compare standalone machine types against each other; they sort
2649 * in increasing order.
2651 return strcmp(object_class_get_name(OBJECT_CLASS(mc1)),
2652 object_class_get_name(OBJECT_CLASS(mc2)));
2655 /* Standalone machine types sort after families. */
2656 return 1;
2659 if (mc2->family == NULL) {
2660 /* Families sort before standalone machine types. */
2661 return -1;
2664 /* Families sort between each other alphabetically increasingly. */
2665 res = strcmp(mc1->family, mc2->family);
2666 if (res != 0) {
2667 return res;
2670 /* Within the same family, machine types sort in decreasing order. */
2671 return strcmp(object_class_get_name(OBJECT_CLASS(mc2)),
2672 object_class_get_name(OBJECT_CLASS(mc1)));
2675 static MachineClass *machine_parse(const char *name)
2677 MachineClass *mc = NULL;
2678 GSList *el, *machines = object_class_get_list(TYPE_MACHINE, false);
2680 if (name) {
2681 mc = find_machine(name);
2683 if (mc) {
2684 g_slist_free(machines);
2685 return mc;
2687 if (name && !is_help_option(name)) {
2688 error_report("unsupported machine type");
2689 error_printf("Use -machine help to list supported machines\n");
2690 } else {
2691 printf("Supported machines are:\n");
2692 machines = g_slist_sort(machines, machine_class_cmp);
2693 for (el = machines; el; el = el->next) {
2694 MachineClass *mc = el->data;
2695 if (mc->alias) {
2696 printf("%-20s %s (alias of %s)\n", mc->alias, mc->desc, mc->name);
2698 printf("%-20s %s%s\n", mc->name, mc->desc,
2699 mc->is_default ? " (default)" : "");
2703 g_slist_free(machines);
2704 exit(!name || !is_help_option(name));
2707 void qemu_add_exit_notifier(Notifier *notify)
2709 notifier_list_add(&exit_notifiers, notify);
2712 void qemu_remove_exit_notifier(Notifier *notify)
2714 notifier_remove(notify);
2717 static void qemu_run_exit_notifiers(void)
2719 notifier_list_notify(&exit_notifiers, NULL);
2722 static bool machine_init_done;
2724 void qemu_add_machine_init_done_notifier(Notifier *notify)
2726 notifier_list_add(&machine_init_done_notifiers, notify);
2727 if (machine_init_done) {
2728 notify->notify(notify, NULL);
2732 void qemu_remove_machine_init_done_notifier(Notifier *notify)
2734 notifier_remove(notify);
2737 static void qemu_run_machine_init_done_notifiers(void)
2739 notifier_list_notify(&machine_init_done_notifiers, NULL);
2740 machine_init_done = true;
2743 static const QEMUOption *lookup_opt(int argc, char **argv,
2744 const char **poptarg, int *poptind)
2746 const QEMUOption *popt;
2747 int optind = *poptind;
2748 char *r = argv[optind];
2749 const char *optarg;
2751 loc_set_cmdline(argv, optind, 1);
2752 optind++;
2753 /* Treat --foo the same as -foo. */
2754 if (r[1] == '-')
2755 r++;
2756 popt = qemu_options;
2757 for(;;) {
2758 if (!popt->name) {
2759 error_report("invalid option");
2760 exit(1);
2762 if (!strcmp(popt->name, r + 1))
2763 break;
2764 popt++;
2766 if (popt->flags & HAS_ARG) {
2767 if (optind >= argc) {
2768 error_report("requires an argument");
2769 exit(1);
2771 optarg = argv[optind++];
2772 loc_set_cmdline(argv, optind - 2, 2);
2773 } else {
2774 optarg = NULL;
2777 *poptarg = optarg;
2778 *poptind = optind;
2780 return popt;
2783 static MachineClass *select_machine(void)
2785 MachineClass *machine_class = find_default_machine();
2786 const char *optarg;
2787 QemuOpts *opts;
2788 Location loc;
2790 loc_push_none(&loc);
2792 opts = qemu_get_machine_opts();
2793 qemu_opts_loc_restore(opts);
2795 optarg = qemu_opt_get(opts, "type");
2796 if (optarg) {
2797 machine_class = machine_parse(optarg);
2800 if (!machine_class) {
2801 error_report("No machine specified, and there is no default");
2802 error_printf("Use -machine help to list supported machines\n");
2803 exit(1);
2806 loc_pop(&loc);
2807 return machine_class;
2810 static int machine_set_property(void *opaque,
2811 const char *name, const char *value,
2812 Error **errp)
2814 Object *obj = OBJECT(opaque);
2815 Error *local_err = NULL;
2816 char *p, *qom_name;
2818 if (strcmp(name, "type") == 0) {
2819 return 0;
2822 qom_name = g_strdup(name);
2823 for (p = qom_name; *p; p++) {
2824 if (*p == '_') {
2825 *p = '-';
2829 object_property_parse(obj, value, qom_name, &local_err);
2830 g_free(qom_name);
2832 if (local_err) {
2833 error_report_err(local_err);
2834 return -1;
2837 return 0;
2842 * Initial object creation happens before all other
2843 * QEMU data types are created. The majority of objects
2844 * can be created at this point. The rng-egd object
2845 * cannot be created here, as it depends on the chardev
2846 * already existing.
2848 static bool object_create_initial(const char *type)
2850 if (g_str_equal(type, "rng-egd")) {
2851 return false;
2855 * return false for concrete netfilters since
2856 * they depend on netdevs already existing
2858 if (g_str_equal(type, "filter-buffer") ||
2859 g_str_equal(type, "filter-dump") ||
2860 g_str_equal(type, "filter-mirror") ||
2861 g_str_equal(type, "filter-redirector") ||
2862 g_str_equal(type, "colo-compare") ||
2863 g_str_equal(type, "filter-rewriter") ||
2864 g_str_equal(type, "filter-replay")) {
2865 return false;
2868 /* Memory allocation by backends needs to be done
2869 * after configure_accelerator() (due to the tcg_enabled()
2870 * checks at memory_region_init_*()).
2872 * Also, allocation of large amounts of memory may delay
2873 * chardev initialization for too long, and trigger timeouts
2874 * on software that waits for a monitor socket to be created
2875 * (e.g. libvirt).
2877 if (g_str_has_prefix(type, "memory-backend-")) {
2878 return false;
2881 return true;
2886 * The remainder of object creation happens after the
2887 * creation of chardev, fsdev, net clients and device data types.
2889 static bool object_create_delayed(const char *type)
2891 return !object_create_initial(type);
2895 static void set_memory_options(uint64_t *ram_slots, ram_addr_t *maxram_size,
2896 MachineClass *mc)
2898 uint64_t sz;
2899 const char *mem_str;
2900 const char *maxmem_str, *slots_str;
2901 const ram_addr_t default_ram_size = mc->default_ram_size;
2902 QemuOpts *opts = qemu_find_opts_singleton("memory");
2903 Location loc;
2905 loc_push_none(&loc);
2906 qemu_opts_loc_restore(opts);
2908 sz = 0;
2909 mem_str = qemu_opt_get(opts, "size");
2910 if (mem_str) {
2911 if (!*mem_str) {
2912 error_report("missing 'size' option value");
2913 exit(EXIT_FAILURE);
2916 sz = qemu_opt_get_size(opts, "size", ram_size);
2918 /* Fix up legacy suffix-less format */
2919 if (g_ascii_isdigit(mem_str[strlen(mem_str) - 1])) {
2920 uint64_t overflow_check = sz;
2922 sz <<= 20;
2923 if ((sz >> 20) != overflow_check) {
2924 error_report("too large 'size' option value");
2925 exit(EXIT_FAILURE);
2930 /* backward compatibility behaviour for case "-m 0" */
2931 if (sz == 0) {
2932 sz = default_ram_size;
2935 sz = QEMU_ALIGN_UP(sz, 8192);
2936 ram_size = sz;
2937 if (ram_size != sz) {
2938 error_report("ram size too large");
2939 exit(EXIT_FAILURE);
2942 /* store value for the future use */
2943 qemu_opt_set_number(opts, "size", ram_size, &error_abort);
2944 *maxram_size = ram_size;
2946 maxmem_str = qemu_opt_get(opts, "maxmem");
2947 slots_str = qemu_opt_get(opts, "slots");
2948 if (maxmem_str && slots_str) {
2949 uint64_t slots;
2951 sz = qemu_opt_get_size(opts, "maxmem", 0);
2952 slots = qemu_opt_get_number(opts, "slots", 0);
2953 if (sz < ram_size) {
2954 error_report("invalid value of -m option maxmem: "
2955 "maximum memory size (0x%" PRIx64 ") must be at least "
2956 "the initial memory size (0x" RAM_ADDR_FMT ")",
2957 sz, ram_size);
2958 exit(EXIT_FAILURE);
2959 } else if (sz > ram_size) {
2960 if (!slots) {
2961 error_report("invalid value of -m option: maxmem was "
2962 "specified, but no hotplug slots were specified");
2963 exit(EXIT_FAILURE);
2965 } else if (slots) {
2966 error_report("invalid value of -m option maxmem: "
2967 "memory slots were specified but maximum memory size "
2968 "(0x%" PRIx64 ") is equal to the initial memory size "
2969 "(0x" RAM_ADDR_FMT ")", sz, ram_size);
2970 exit(EXIT_FAILURE);
2973 *maxram_size = sz;
2974 *ram_slots = slots;
2975 } else if ((!maxmem_str && slots_str) ||
2976 (maxmem_str && !slots_str)) {
2977 error_report("invalid -m option value: missing "
2978 "'%s' option", slots_str ? "maxmem" : "slots");
2979 exit(EXIT_FAILURE);
2982 loc_pop(&loc);
2985 static int global_init_func(void *opaque, QemuOpts *opts, Error **errp)
2987 GlobalProperty *g;
2989 g = g_malloc0(sizeof(*g));
2990 g->driver = qemu_opt_get(opts, "driver");
2991 g->property = qemu_opt_get(opts, "property");
2992 g->value = qemu_opt_get(opts, "value");
2993 g->user_provided = true;
2994 g->errp = &error_fatal;
2995 qdev_prop_register_global(g);
2996 return 0;
2999 int main(int argc, char **argv)
3001 int i;
3002 int snapshot, linux_boot;
3003 const char *initrd_filename;
3004 const char *kernel_filename, *kernel_cmdline;
3005 const char *boot_order = NULL;
3006 const char *boot_once = NULL;
3007 DisplayState *ds;
3008 int cyls, heads, secs, translation;
3009 QemuOpts *hda_opts = NULL, *opts, *machine_opts, *icount_opts = NULL;
3010 QemuOptsList *olist;
3011 int optind;
3012 const char *optarg;
3013 const char *loadvm = NULL;
3014 MachineClass *machine_class;
3015 const char *cpu_model;
3016 const char *vga_model = NULL;
3017 const char *qtest_chrdev = NULL;
3018 const char *qtest_log = NULL;
3019 const char *pid_file = NULL;
3020 const char *incoming = NULL;
3021 bool defconfig = true;
3022 bool userconfig = true;
3023 bool nographic = false;
3024 DisplayType display_type = DT_DEFAULT;
3025 int display_remote = 0;
3026 const char *log_mask = NULL;
3027 const char *log_file = NULL;
3028 char *trace_file = NULL;
3029 ram_addr_t maxram_size;
3030 uint64_t ram_slots = 0;
3031 FILE *vmstate_dump_file = NULL;
3032 Error *main_loop_err = NULL;
3033 Error *err = NULL;
3034 bool list_data_dirs = false;
3036 module_call_init(MODULE_INIT_TRACE);
3038 qemu_init_cpu_list();
3039 qemu_init_cpu_loop();
3040 qemu_mutex_lock_iothread();
3042 atexit(qemu_run_exit_notifiers);
3043 error_set_progname(argv[0]);
3044 qemu_init_exec_dir(argv[0]);
3046 module_call_init(MODULE_INIT_QOM);
3047 module_call_init(MODULE_INIT_QAPI);
3049 qemu_add_opts(&qemu_drive_opts);
3050 qemu_add_drive_opts(&qemu_legacy_drive_opts);
3051 qemu_add_drive_opts(&qemu_common_drive_opts);
3052 qemu_add_drive_opts(&qemu_drive_opts);
3053 qemu_add_drive_opts(&bdrv_runtime_opts);
3054 qemu_add_opts(&qemu_chardev_opts);
3055 qemu_add_opts(&qemu_device_opts);
3056 qemu_add_opts(&qemu_netdev_opts);
3057 qemu_add_opts(&qemu_net_opts);
3058 qemu_add_opts(&qemu_rtc_opts);
3059 qemu_add_opts(&qemu_global_opts);
3060 qemu_add_opts(&qemu_mon_opts);
3061 qemu_add_opts(&qemu_trace_opts);
3062 qemu_add_opts(&qemu_option_rom_opts);
3063 qemu_add_opts(&qemu_machine_opts);
3064 qemu_add_opts(&qemu_mem_opts);
3065 qemu_add_opts(&qemu_smp_opts);
3066 qemu_add_opts(&qemu_boot_opts);
3067 qemu_add_opts(&qemu_sandbox_opts);
3068 qemu_add_opts(&qemu_add_fd_opts);
3069 qemu_add_opts(&qemu_object_opts);
3070 qemu_add_opts(&qemu_tpmdev_opts);
3071 qemu_add_opts(&qemu_realtime_opts);
3072 qemu_add_opts(&qemu_msg_opts);
3073 qemu_add_opts(&qemu_name_opts);
3074 qemu_add_opts(&qemu_numa_opts);
3075 qemu_add_opts(&qemu_icount_opts);
3076 qemu_add_opts(&qemu_semihosting_config_opts);
3077 qemu_add_opts(&qemu_fw_cfg_opts);
3078 #ifdef CONFIG_LIBISCSI
3079 qemu_add_opts(&qemu_iscsi_opts);
3080 #endif
3081 module_call_init(MODULE_INIT_OPTS);
3083 runstate_init();
3085 if (qcrypto_init(&err) < 0) {
3086 error_reportf_err(err, "cannot initialize crypto: ");
3087 exit(1);
3089 rtc_clock = QEMU_CLOCK_HOST;
3091 QLIST_INIT (&vm_change_state_head);
3092 os_setup_early_signal_handling();
3094 cpu_model = NULL;
3095 snapshot = 0;
3096 cyls = heads = secs = 0;
3097 translation = BIOS_ATA_TRANSLATION_AUTO;
3099 nb_nics = 0;
3101 bdrv_init_with_whitelist();
3103 autostart = 1;
3105 /* first pass of option parsing */
3106 optind = 1;
3107 while (optind < argc) {
3108 if (argv[optind][0] != '-') {
3109 /* disk image */
3110 optind++;
3111 } else {
3112 const QEMUOption *popt;
3114 popt = lookup_opt(argc, argv, &optarg, &optind);
3115 switch (popt->index) {
3116 case QEMU_OPTION_nodefconfig:
3117 defconfig = false;
3118 break;
3119 case QEMU_OPTION_nouserconfig:
3120 userconfig = false;
3121 break;
3126 if (defconfig) {
3127 int ret;
3128 ret = qemu_read_default_config_files(userconfig);
3129 if (ret < 0) {
3130 exit(1);
3134 /* second pass of option parsing */
3135 optind = 1;
3136 for(;;) {
3137 if (optind >= argc)
3138 break;
3139 if (argv[optind][0] != '-') {
3140 hda_opts = drive_add(IF_DEFAULT, 0, argv[optind++], HD_OPTS);
3141 } else {
3142 const QEMUOption *popt;
3144 popt = lookup_opt(argc, argv, &optarg, &optind);
3145 if (!(popt->arch_mask & arch_type)) {
3146 error_report("Option not supported for this target");
3147 exit(1);
3149 switch(popt->index) {
3150 case QEMU_OPTION_no_kvm_irqchip: {
3151 olist = qemu_find_opts("machine");
3152 qemu_opts_parse_noisily(olist, "kernel_irqchip=off", false);
3153 break;
3155 case QEMU_OPTION_cpu:
3156 /* hw initialization will check this */
3157 cpu_model = optarg;
3158 break;
3159 case QEMU_OPTION_hda:
3161 char buf[256];
3162 if (cyls == 0)
3163 snprintf(buf, sizeof(buf), "%s", HD_OPTS);
3164 else
3165 snprintf(buf, sizeof(buf),
3166 "%s,cyls=%d,heads=%d,secs=%d%s",
3167 HD_OPTS , cyls, heads, secs,
3168 translation == BIOS_ATA_TRANSLATION_LBA ?
3169 ",trans=lba" :
3170 translation == BIOS_ATA_TRANSLATION_NONE ?
3171 ",trans=none" : "");
3172 drive_add(IF_DEFAULT, 0, optarg, buf);
3173 break;
3175 case QEMU_OPTION_hdb:
3176 case QEMU_OPTION_hdc:
3177 case QEMU_OPTION_hdd:
3178 drive_add(IF_DEFAULT, popt->index - QEMU_OPTION_hda, optarg,
3179 HD_OPTS);
3180 break;
3181 case QEMU_OPTION_drive:
3182 if (drive_def(optarg) == NULL) {
3183 exit(1);
3185 break;
3186 case QEMU_OPTION_set:
3187 if (qemu_set_option(optarg) != 0)
3188 exit(1);
3189 break;
3190 case QEMU_OPTION_global:
3191 if (qemu_global_option(optarg) != 0)
3192 exit(1);
3193 break;
3194 case QEMU_OPTION_mtdblock:
3195 drive_add(IF_MTD, -1, optarg, MTD_OPTS);
3196 break;
3197 case QEMU_OPTION_sd:
3198 drive_add(IF_SD, -1, optarg, SD_OPTS);
3199 break;
3200 case QEMU_OPTION_pflash:
3201 drive_add(IF_PFLASH, -1, optarg, PFLASH_OPTS);
3202 break;
3203 case QEMU_OPTION_snapshot:
3204 snapshot = 1;
3205 break;
3206 case QEMU_OPTION_hdachs:
3208 const char *p;
3209 p = optarg;
3210 cyls = strtol(p, (char **)&p, 0);
3211 if (cyls < 1 || cyls > 16383)
3212 goto chs_fail;
3213 if (*p != ',')
3214 goto chs_fail;
3215 p++;
3216 heads = strtol(p, (char **)&p, 0);
3217 if (heads < 1 || heads > 16)
3218 goto chs_fail;
3219 if (*p != ',')
3220 goto chs_fail;
3221 p++;
3222 secs = strtol(p, (char **)&p, 0);
3223 if (secs < 1 || secs > 63)
3224 goto chs_fail;
3225 if (*p == ',') {
3226 p++;
3227 if (!strcmp(p, "large")) {
3228 translation = BIOS_ATA_TRANSLATION_LARGE;
3229 } else if (!strcmp(p, "rechs")) {
3230 translation = BIOS_ATA_TRANSLATION_RECHS;
3231 } else if (!strcmp(p, "none")) {
3232 translation = BIOS_ATA_TRANSLATION_NONE;
3233 } else if (!strcmp(p, "lba")) {
3234 translation = BIOS_ATA_TRANSLATION_LBA;
3235 } else if (!strcmp(p, "auto")) {
3236 translation = BIOS_ATA_TRANSLATION_AUTO;
3237 } else {
3238 goto chs_fail;
3240 } else if (*p != '\0') {
3241 chs_fail:
3242 error_report("invalid physical CHS format");
3243 exit(1);
3245 if (hda_opts != NULL) {
3246 qemu_opt_set_number(hda_opts, "cyls", cyls,
3247 &error_abort);
3248 qemu_opt_set_number(hda_opts, "heads", heads,
3249 &error_abort);
3250 qemu_opt_set_number(hda_opts, "secs", secs,
3251 &error_abort);
3252 if (translation == BIOS_ATA_TRANSLATION_LARGE) {
3253 qemu_opt_set(hda_opts, "trans", "large",
3254 &error_abort);
3255 } else if (translation == BIOS_ATA_TRANSLATION_RECHS) {
3256 qemu_opt_set(hda_opts, "trans", "rechs",
3257 &error_abort);
3258 } else if (translation == BIOS_ATA_TRANSLATION_LBA) {
3259 qemu_opt_set(hda_opts, "trans", "lba",
3260 &error_abort);
3261 } else if (translation == BIOS_ATA_TRANSLATION_NONE) {
3262 qemu_opt_set(hda_opts, "trans", "none",
3263 &error_abort);
3267 break;
3268 case QEMU_OPTION_numa:
3269 opts = qemu_opts_parse_noisily(qemu_find_opts("numa"),
3270 optarg, true);
3271 if (!opts) {
3272 exit(1);
3274 break;
3275 case QEMU_OPTION_display:
3276 display_type = select_display(optarg);
3277 break;
3278 case QEMU_OPTION_nographic:
3279 olist = qemu_find_opts("machine");
3280 qemu_opts_parse_noisily(olist, "graphics=off", false);
3281 nographic = true;
3282 display_type = DT_NONE;
3283 break;
3284 case QEMU_OPTION_curses:
3285 #ifdef CONFIG_CURSES
3286 display_type = DT_CURSES;
3287 #else
3288 error_report("curses support is disabled");
3289 exit(1);
3290 #endif
3291 break;
3292 case QEMU_OPTION_portrait:
3293 graphic_rotate = 90;
3294 break;
3295 case QEMU_OPTION_rotate:
3296 graphic_rotate = strtol(optarg, (char **) &optarg, 10);
3297 if (graphic_rotate != 0 && graphic_rotate != 90 &&
3298 graphic_rotate != 180 && graphic_rotate != 270) {
3299 error_report("only 90, 180, 270 deg rotation is available");
3300 exit(1);
3302 break;
3303 case QEMU_OPTION_kernel:
3304 qemu_opts_set(qemu_find_opts("machine"), 0, "kernel", optarg,
3305 &error_abort);
3306 break;
3307 case QEMU_OPTION_initrd:
3308 qemu_opts_set(qemu_find_opts("machine"), 0, "initrd", optarg,
3309 &error_abort);
3310 break;
3311 case QEMU_OPTION_append:
3312 qemu_opts_set(qemu_find_opts("machine"), 0, "append", optarg,
3313 &error_abort);
3314 break;
3315 case QEMU_OPTION_dtb:
3316 qemu_opts_set(qemu_find_opts("machine"), 0, "dtb", optarg,
3317 &error_abort);
3318 break;
3319 case QEMU_OPTION_cdrom:
3320 drive_add(IF_DEFAULT, 2, optarg, CDROM_OPTS);
3321 break;
3322 case QEMU_OPTION_boot:
3323 opts = qemu_opts_parse_noisily(qemu_find_opts("boot-opts"),
3324 optarg, true);
3325 if (!opts) {
3326 exit(1);
3328 break;
3329 case QEMU_OPTION_fda:
3330 case QEMU_OPTION_fdb:
3331 drive_add(IF_FLOPPY, popt->index - QEMU_OPTION_fda,
3332 optarg, FD_OPTS);
3333 break;
3334 case QEMU_OPTION_no_fd_bootchk:
3335 fd_bootchk = 0;
3336 break;
3337 case QEMU_OPTION_netdev:
3338 default_net = 0;
3339 if (net_client_parse(qemu_find_opts("netdev"), optarg) == -1) {
3340 exit(1);
3342 break;
3343 case QEMU_OPTION_net:
3344 default_net = 0;
3345 if (net_client_parse(qemu_find_opts("net"), optarg) == -1) {
3346 exit(1);
3348 break;
3349 #ifdef CONFIG_LIBISCSI
3350 case QEMU_OPTION_iscsi:
3351 opts = qemu_opts_parse_noisily(qemu_find_opts("iscsi"),
3352 optarg, false);
3353 if (!opts) {
3354 exit(1);
3356 break;
3357 #endif
3358 #ifdef CONFIG_SLIRP
3359 case QEMU_OPTION_tftp:
3360 error_report("The -tftp option is deprecated. "
3361 "Please use '-netdev user,tftp=...' instead.");
3362 legacy_tftp_prefix = optarg;
3363 break;
3364 case QEMU_OPTION_bootp:
3365 error_report("The -bootp option is deprecated. "
3366 "Please use '-netdev user,bootfile=...' instead.");
3367 legacy_bootp_filename = optarg;
3368 break;
3369 case QEMU_OPTION_redir:
3370 error_report("The -redir option is deprecated. "
3371 "Please use '-netdev user,hostfwd=...' instead.");
3372 if (net_slirp_redir(optarg) < 0)
3373 exit(1);
3374 break;
3375 #endif
3376 case QEMU_OPTION_bt:
3377 add_device_config(DEV_BT, optarg);
3378 break;
3379 case QEMU_OPTION_audio_help:
3380 AUD_help ();
3381 exit (0);
3382 break;
3383 case QEMU_OPTION_soundhw:
3384 select_soundhw (optarg);
3385 break;
3386 case QEMU_OPTION_h:
3387 help(0);
3388 break;
3389 case QEMU_OPTION_version:
3390 version();
3391 exit(0);
3392 break;
3393 case QEMU_OPTION_m:
3394 opts = qemu_opts_parse_noisily(qemu_find_opts("memory"),
3395 optarg, true);
3396 if (!opts) {
3397 exit(EXIT_FAILURE);
3399 break;
3400 #ifdef CONFIG_TPM
3401 case QEMU_OPTION_tpmdev:
3402 if (tpm_config_parse(qemu_find_opts("tpmdev"), optarg) < 0) {
3403 exit(1);
3405 break;
3406 #endif
3407 case QEMU_OPTION_mempath:
3408 mem_path = optarg;
3409 break;
3410 case QEMU_OPTION_mem_prealloc:
3411 mem_prealloc = 1;
3412 break;
3413 case QEMU_OPTION_d:
3414 log_mask = optarg;
3415 break;
3416 case QEMU_OPTION_D:
3417 log_file = optarg;
3418 break;
3419 case QEMU_OPTION_DFILTER:
3420 qemu_set_dfilter_ranges(optarg, &error_fatal);
3421 break;
3422 case QEMU_OPTION_s:
3423 add_device_config(DEV_GDB, "tcp::" DEFAULT_GDBSTUB_PORT);
3424 break;
3425 case QEMU_OPTION_gdb:
3426 add_device_config(DEV_GDB, optarg);
3427 break;
3428 case QEMU_OPTION_L:
3429 if (is_help_option(optarg)) {
3430 list_data_dirs = true;
3431 } else if (data_dir_idx < ARRAY_SIZE(data_dir)) {
3432 data_dir[data_dir_idx++] = optarg;
3434 break;
3435 case QEMU_OPTION_bios:
3436 qemu_opts_set(qemu_find_opts("machine"), 0, "firmware", optarg,
3437 &error_abort);
3438 break;
3439 case QEMU_OPTION_singlestep:
3440 singlestep = 1;
3441 break;
3442 case QEMU_OPTION_S:
3443 autostart = 0;
3444 break;
3445 case QEMU_OPTION_k:
3446 keyboard_layout = optarg;
3447 break;
3448 case QEMU_OPTION_localtime:
3449 rtc_utc = 0;
3450 break;
3451 case QEMU_OPTION_vga:
3452 vga_model = optarg;
3453 default_vga = 0;
3454 break;
3455 case QEMU_OPTION_g:
3457 const char *p;
3458 int w, h, depth;
3459 p = optarg;
3460 w = strtol(p, (char **)&p, 10);
3461 if (w <= 0) {
3462 graphic_error:
3463 error_report("invalid resolution or depth");
3464 exit(1);
3466 if (*p != 'x')
3467 goto graphic_error;
3468 p++;
3469 h = strtol(p, (char **)&p, 10);
3470 if (h <= 0)
3471 goto graphic_error;
3472 if (*p == 'x') {
3473 p++;
3474 depth = strtol(p, (char **)&p, 10);
3475 if (depth != 8 && depth != 15 && depth != 16 &&
3476 depth != 24 && depth != 32)
3477 goto graphic_error;
3478 } else if (*p == '\0') {
3479 depth = graphic_depth;
3480 } else {
3481 goto graphic_error;
3484 graphic_width = w;
3485 graphic_height = h;
3486 graphic_depth = depth;
3488 break;
3489 case QEMU_OPTION_echr:
3491 char *r;
3492 term_escape_char = strtol(optarg, &r, 0);
3493 if (r == optarg)
3494 printf("Bad argument to echr\n");
3495 break;
3497 case QEMU_OPTION_monitor:
3498 default_monitor = 0;
3499 if (strncmp(optarg, "none", 4)) {
3500 monitor_parse(optarg, "readline", false);
3502 break;
3503 case QEMU_OPTION_qmp:
3504 monitor_parse(optarg, "control", false);
3505 default_monitor = 0;
3506 break;
3507 case QEMU_OPTION_qmp_pretty:
3508 monitor_parse(optarg, "control", true);
3509 default_monitor = 0;
3510 break;
3511 case QEMU_OPTION_mon:
3512 opts = qemu_opts_parse_noisily(qemu_find_opts("mon"), optarg,
3513 true);
3514 if (!opts) {
3515 exit(1);
3517 default_monitor = 0;
3518 break;
3519 case QEMU_OPTION_chardev:
3520 opts = qemu_opts_parse_noisily(qemu_find_opts("chardev"),
3521 optarg, true);
3522 if (!opts) {
3523 exit(1);
3525 break;
3526 case QEMU_OPTION_fsdev:
3527 olist = qemu_find_opts("fsdev");
3528 if (!olist) {
3529 error_report("fsdev support is disabled");
3530 exit(1);
3532 opts = qemu_opts_parse_noisily(olist, optarg, true);
3533 if (!opts) {
3534 exit(1);
3536 break;
3537 case QEMU_OPTION_virtfs: {
3538 QemuOpts *fsdev;
3539 QemuOpts *device;
3540 const char *writeout, *sock_fd, *socket;
3542 olist = qemu_find_opts("virtfs");
3543 if (!olist) {
3544 error_report("virtfs support is disabled");
3545 exit(1);
3547 opts = qemu_opts_parse_noisily(olist, optarg, true);
3548 if (!opts) {
3549 exit(1);
3552 if (qemu_opt_get(opts, "fsdriver") == NULL ||
3553 qemu_opt_get(opts, "mount_tag") == NULL) {
3554 error_report("Usage: -virtfs fsdriver,mount_tag=tag");
3555 exit(1);
3557 fsdev = qemu_opts_create(qemu_find_opts("fsdev"),
3558 qemu_opt_get(opts, "mount_tag"),
3559 1, NULL);
3560 if (!fsdev) {
3561 error_report("duplicate fsdev id: %s",
3562 qemu_opt_get(opts, "mount_tag"));
3563 exit(1);
3566 writeout = qemu_opt_get(opts, "writeout");
3567 if (writeout) {
3568 #ifdef CONFIG_SYNC_FILE_RANGE
3569 qemu_opt_set(fsdev, "writeout", writeout, &error_abort);
3570 #else
3571 error_report("writeout=immediate not supported "
3572 "on this platform");
3573 exit(1);
3574 #endif
3576 qemu_opt_set(fsdev, "fsdriver",
3577 qemu_opt_get(opts, "fsdriver"), &error_abort);
3578 qemu_opt_set(fsdev, "path", qemu_opt_get(opts, "path"),
3579 &error_abort);
3580 qemu_opt_set(fsdev, "security_model",
3581 qemu_opt_get(opts, "security_model"),
3582 &error_abort);
3583 socket = qemu_opt_get(opts, "socket");
3584 if (socket) {
3585 qemu_opt_set(fsdev, "socket", socket, &error_abort);
3587 sock_fd = qemu_opt_get(opts, "sock_fd");
3588 if (sock_fd) {
3589 qemu_opt_set(fsdev, "sock_fd", sock_fd, &error_abort);
3592 qemu_opt_set_bool(fsdev, "readonly",
3593 qemu_opt_get_bool(opts, "readonly", 0),
3594 &error_abort);
3595 device = qemu_opts_create(qemu_find_opts("device"), NULL, 0,
3596 &error_abort);
3597 qemu_opt_set(device, "driver", "virtio-9p-pci", &error_abort);
3598 qemu_opt_set(device, "fsdev",
3599 qemu_opt_get(opts, "mount_tag"), &error_abort);
3600 qemu_opt_set(device, "mount_tag",
3601 qemu_opt_get(opts, "mount_tag"), &error_abort);
3602 break;
3604 case QEMU_OPTION_virtfs_synth: {
3605 QemuOpts *fsdev;
3606 QemuOpts *device;
3608 fsdev = qemu_opts_create(qemu_find_opts("fsdev"), "v_synth",
3609 1, NULL);
3610 if (!fsdev) {
3611 error_report("duplicate option: %s", "virtfs_synth");
3612 exit(1);
3614 qemu_opt_set(fsdev, "fsdriver", "synth", &error_abort);
3616 device = qemu_opts_create(qemu_find_opts("device"), NULL, 0,
3617 &error_abort);
3618 qemu_opt_set(device, "driver", "virtio-9p-pci", &error_abort);
3619 qemu_opt_set(device, "fsdev", "v_synth", &error_abort);
3620 qemu_opt_set(device, "mount_tag", "v_synth", &error_abort);
3621 break;
3623 case QEMU_OPTION_serial:
3624 add_device_config(DEV_SERIAL, optarg);
3625 default_serial = 0;
3626 if (strncmp(optarg, "mon:", 4) == 0) {
3627 default_monitor = 0;
3629 break;
3630 case QEMU_OPTION_watchdog:
3631 if (watchdog) {
3632 error_report("only one watchdog option may be given");
3633 return 1;
3635 watchdog = optarg;
3636 break;
3637 case QEMU_OPTION_watchdog_action:
3638 if (select_watchdog_action(optarg) == -1) {
3639 error_report("unknown -watchdog-action parameter");
3640 exit(1);
3642 break;
3643 case QEMU_OPTION_virtiocon:
3644 add_device_config(DEV_VIRTCON, optarg);
3645 default_virtcon = 0;
3646 if (strncmp(optarg, "mon:", 4) == 0) {
3647 default_monitor = 0;
3649 break;
3650 case QEMU_OPTION_parallel:
3651 add_device_config(DEV_PARALLEL, optarg);
3652 default_parallel = 0;
3653 if (strncmp(optarg, "mon:", 4) == 0) {
3654 default_monitor = 0;
3656 break;
3657 case QEMU_OPTION_debugcon:
3658 add_device_config(DEV_DEBUGCON, optarg);
3659 break;
3660 case QEMU_OPTION_loadvm:
3661 loadvm = optarg;
3662 break;
3663 case QEMU_OPTION_full_screen:
3664 full_screen = 1;
3665 break;
3666 case QEMU_OPTION_no_frame:
3667 no_frame = 1;
3668 break;
3669 case QEMU_OPTION_alt_grab:
3670 alt_grab = 1;
3671 break;
3672 case QEMU_OPTION_ctrl_grab:
3673 ctrl_grab = 1;
3674 break;
3675 case QEMU_OPTION_no_quit:
3676 no_quit = 1;
3677 break;
3678 case QEMU_OPTION_sdl:
3679 #ifdef CONFIG_SDL
3680 display_type = DT_SDL;
3681 break;
3682 #else
3683 error_report("SDL support is disabled");
3684 exit(1);
3685 #endif
3686 case QEMU_OPTION_pidfile:
3687 pid_file = optarg;
3688 break;
3689 case QEMU_OPTION_win2k_hack:
3690 win2k_install_hack = 1;
3691 break;
3692 case QEMU_OPTION_rtc_td_hack: {
3693 static GlobalProperty slew_lost_ticks = {
3694 .driver = "mc146818rtc",
3695 .property = "lost_tick_policy",
3696 .value = "slew",
3699 qdev_prop_register_global(&slew_lost_ticks);
3700 break;
3702 case QEMU_OPTION_acpitable:
3703 opts = qemu_opts_parse_noisily(qemu_find_opts("acpi"),
3704 optarg, true);
3705 if (!opts) {
3706 exit(1);
3708 acpi_table_add(opts, &error_fatal);
3709 break;
3710 case QEMU_OPTION_smbios:
3711 opts = qemu_opts_parse_noisily(qemu_find_opts("smbios"),
3712 optarg, false);
3713 if (!opts) {
3714 exit(1);
3716 smbios_entry_add(opts, &error_fatal);
3717 break;
3718 case QEMU_OPTION_fwcfg:
3719 opts = qemu_opts_parse_noisily(qemu_find_opts("fw_cfg"),
3720 optarg, true);
3721 if (opts == NULL) {
3722 exit(1);
3724 break;
3725 case QEMU_OPTION_enable_kvm:
3726 olist = qemu_find_opts("machine");
3727 qemu_opts_parse_noisily(olist, "accel=kvm", false);
3728 break;
3729 case QEMU_OPTION_enable_hax:
3730 olist = qemu_find_opts("machine");
3731 qemu_opts_parse_noisily(olist, "accel=hax", false);
3732 break;
3733 case QEMU_OPTION_M:
3734 case QEMU_OPTION_machine:
3735 olist = qemu_find_opts("machine");
3736 opts = qemu_opts_parse_noisily(olist, optarg, true);
3737 if (!opts) {
3738 exit(1);
3740 break;
3741 case QEMU_OPTION_no_kvm:
3742 olist = qemu_find_opts("machine");
3743 qemu_opts_parse_noisily(olist, "accel=tcg", false);
3744 break;
3745 case QEMU_OPTION_no_kvm_pit: {
3746 error_report("warning: ignoring deprecated option");
3747 break;
3749 case QEMU_OPTION_no_kvm_pit_reinjection: {
3750 static GlobalProperty kvm_pit_lost_tick_policy = {
3751 .driver = "kvm-pit",
3752 .property = "lost_tick_policy",
3753 .value = "discard",
3756 error_report("warning: deprecated, replaced by "
3757 "-global kvm-pit.lost_tick_policy=discard");
3758 qdev_prop_register_global(&kvm_pit_lost_tick_policy);
3759 break;
3761 case QEMU_OPTION_usb:
3762 olist = qemu_find_opts("machine");
3763 qemu_opts_parse_noisily(olist, "usb=on", false);
3764 break;
3765 case QEMU_OPTION_usbdevice:
3766 olist = qemu_find_opts("machine");
3767 qemu_opts_parse_noisily(olist, "usb=on", false);
3768 add_device_config(DEV_USB, optarg);
3769 break;
3770 case QEMU_OPTION_device:
3771 if (!qemu_opts_parse_noisily(qemu_find_opts("device"),
3772 optarg, true)) {
3773 exit(1);
3775 break;
3776 case QEMU_OPTION_smp:
3777 if (!qemu_opts_parse_noisily(qemu_find_opts("smp-opts"),
3778 optarg, true)) {
3779 exit(1);
3781 break;
3782 case QEMU_OPTION_vnc:
3783 vnc_parse(optarg, &error_fatal);
3784 break;
3785 case QEMU_OPTION_no_acpi:
3786 acpi_enabled = 0;
3787 break;
3788 case QEMU_OPTION_no_hpet:
3789 no_hpet = 1;
3790 break;
3791 case QEMU_OPTION_balloon:
3792 if (balloon_parse(optarg) < 0) {
3793 error_report("unknown -balloon argument %s", optarg);
3794 exit(1);
3796 break;
3797 case QEMU_OPTION_no_reboot:
3798 no_reboot = 1;
3799 break;
3800 case QEMU_OPTION_no_shutdown:
3801 no_shutdown = 1;
3802 break;
3803 case QEMU_OPTION_show_cursor:
3804 cursor_hide = 0;
3805 break;
3806 case QEMU_OPTION_uuid:
3807 if (qemu_uuid_parse(optarg, &qemu_uuid) < 0) {
3808 error_report("failed to parse UUID string: wrong format");
3809 exit(1);
3811 qemu_uuid_set = true;
3812 break;
3813 case QEMU_OPTION_option_rom:
3814 if (nb_option_roms >= MAX_OPTION_ROMS) {
3815 error_report("too many option ROMs");
3816 exit(1);
3818 opts = qemu_opts_parse_noisily(qemu_find_opts("option-rom"),
3819 optarg, true);
3820 if (!opts) {
3821 exit(1);
3823 option_rom[nb_option_roms].name = qemu_opt_get(opts, "romfile");
3824 option_rom[nb_option_roms].bootindex =
3825 qemu_opt_get_number(opts, "bootindex", -1);
3826 if (!option_rom[nb_option_roms].name) {
3827 error_report("Option ROM file is not specified");
3828 exit(1);
3830 nb_option_roms++;
3831 break;
3832 case QEMU_OPTION_semihosting:
3833 semihosting.enabled = true;
3834 semihosting.target = SEMIHOSTING_TARGET_AUTO;
3835 break;
3836 case QEMU_OPTION_semihosting_config:
3837 semihosting.enabled = true;
3838 opts = qemu_opts_parse_noisily(qemu_find_opts("semihosting-config"),
3839 optarg, false);
3840 if (opts != NULL) {
3841 semihosting.enabled = qemu_opt_get_bool(opts, "enable",
3842 true);
3843 const char *target = qemu_opt_get(opts, "target");
3844 if (target != NULL) {
3845 if (strcmp("native", target) == 0) {
3846 semihosting.target = SEMIHOSTING_TARGET_NATIVE;
3847 } else if (strcmp("gdb", target) == 0) {
3848 semihosting.target = SEMIHOSTING_TARGET_GDB;
3849 } else if (strcmp("auto", target) == 0) {
3850 semihosting.target = SEMIHOSTING_TARGET_AUTO;
3851 } else {
3852 error_report("unsupported semihosting-config %s",
3853 optarg);
3854 exit(1);
3856 } else {
3857 semihosting.target = SEMIHOSTING_TARGET_AUTO;
3859 /* Set semihosting argument count and vector */
3860 qemu_opt_foreach(opts, add_semihosting_arg,
3861 &semihosting, NULL);
3862 } else {
3863 error_report("unsupported semihosting-config %s", optarg);
3864 exit(1);
3866 break;
3867 case QEMU_OPTION_tdf:
3868 error_report("warning: ignoring deprecated option");
3869 break;
3870 case QEMU_OPTION_name:
3871 opts = qemu_opts_parse_noisily(qemu_find_opts("name"),
3872 optarg, true);
3873 if (!opts) {
3874 exit(1);
3876 break;
3877 case QEMU_OPTION_prom_env:
3878 if (nb_prom_envs >= MAX_PROM_ENVS) {
3879 error_report("too many prom variables");
3880 exit(1);
3882 prom_envs[nb_prom_envs] = optarg;
3883 nb_prom_envs++;
3884 break;
3885 case QEMU_OPTION_old_param:
3886 old_param = 1;
3887 break;
3888 case QEMU_OPTION_clock:
3889 /* Clock options no longer exist. Keep this option for
3890 * backward compatibility.
3892 break;
3893 case QEMU_OPTION_startdate:
3894 configure_rtc_date_offset(optarg, 1);
3895 break;
3896 case QEMU_OPTION_rtc:
3897 opts = qemu_opts_parse_noisily(qemu_find_opts("rtc"), optarg,
3898 false);
3899 if (!opts) {
3900 exit(1);
3902 configure_rtc(opts);
3903 break;
3904 case QEMU_OPTION_tb_size:
3905 tcg_tb_size = strtol(optarg, NULL, 0);
3906 if (tcg_tb_size < 0) {
3907 tcg_tb_size = 0;
3909 break;
3910 case QEMU_OPTION_icount:
3911 icount_opts = qemu_opts_parse_noisily(qemu_find_opts("icount"),
3912 optarg, true);
3913 if (!icount_opts) {
3914 exit(1);
3916 break;
3917 case QEMU_OPTION_incoming:
3918 if (!incoming) {
3919 runstate_set(RUN_STATE_INMIGRATE);
3921 incoming = optarg;
3922 break;
3923 case QEMU_OPTION_nodefaults:
3924 has_defaults = 0;
3925 break;
3926 case QEMU_OPTION_xen_domid:
3927 if (!(xen_available())) {
3928 error_report("Option not supported for this target");
3929 exit(1);
3931 xen_domid = atoi(optarg);
3932 break;
3933 case QEMU_OPTION_xen_create:
3934 if (!(xen_available())) {
3935 error_report("Option not supported for this target");
3936 exit(1);
3938 xen_mode = XEN_CREATE;
3939 break;
3940 case QEMU_OPTION_xen_attach:
3941 if (!(xen_available())) {
3942 error_report("Option not supported for this target");
3943 exit(1);
3945 xen_mode = XEN_ATTACH;
3946 break;
3947 case QEMU_OPTION_trace:
3948 g_free(trace_file);
3949 trace_file = trace_opt_parse(optarg);
3950 break;
3951 case QEMU_OPTION_trace_unassigned:
3952 trace_unassigned = true;
3953 break;
3954 case QEMU_OPTION_readconfig:
3956 int ret = qemu_read_config_file(optarg);
3957 if (ret < 0) {
3958 error_report("read config %s: %s", optarg,
3959 strerror(-ret));
3960 exit(1);
3962 break;
3964 case QEMU_OPTION_spice:
3965 olist = qemu_find_opts("spice");
3966 if (!olist) {
3967 error_report("spice support is disabled");
3968 exit(1);
3970 opts = qemu_opts_parse_noisily(olist, optarg, false);
3971 if (!opts) {
3972 exit(1);
3974 display_remote++;
3975 break;
3976 case QEMU_OPTION_writeconfig:
3978 FILE *fp;
3979 if (strcmp(optarg, "-") == 0) {
3980 fp = stdout;
3981 } else {
3982 fp = fopen(optarg, "w");
3983 if (fp == NULL) {
3984 error_report("open %s: %s", optarg,
3985 strerror(errno));
3986 exit(1);
3989 qemu_config_write(fp);
3990 if (fp != stdout) {
3991 fclose(fp);
3993 break;
3995 case QEMU_OPTION_qtest:
3996 qtest_chrdev = optarg;
3997 break;
3998 case QEMU_OPTION_qtest_log:
3999 qtest_log = optarg;
4000 break;
4001 case QEMU_OPTION_sandbox:
4002 opts = qemu_opts_parse_noisily(qemu_find_opts("sandbox"),
4003 optarg, true);
4004 if (!opts) {
4005 exit(1);
4007 break;
4008 case QEMU_OPTION_add_fd:
4009 #ifndef _WIN32
4010 opts = qemu_opts_parse_noisily(qemu_find_opts("add-fd"),
4011 optarg, false);
4012 if (!opts) {
4013 exit(1);
4015 #else
4016 error_report("File descriptor passing is disabled on this "
4017 "platform");
4018 exit(1);
4019 #endif
4020 break;
4021 case QEMU_OPTION_object:
4022 opts = qemu_opts_parse_noisily(qemu_find_opts("object"),
4023 optarg, true);
4024 if (!opts) {
4025 exit(1);
4027 break;
4028 case QEMU_OPTION_realtime:
4029 opts = qemu_opts_parse_noisily(qemu_find_opts("realtime"),
4030 optarg, false);
4031 if (!opts) {
4032 exit(1);
4034 enable_mlock = qemu_opt_get_bool(opts, "mlock", true);
4035 break;
4036 case QEMU_OPTION_msg:
4037 opts = qemu_opts_parse_noisily(qemu_find_opts("msg"), optarg,
4038 false);
4039 if (!opts) {
4040 exit(1);
4042 configure_msg(opts);
4043 break;
4044 case QEMU_OPTION_dump_vmstate:
4045 if (vmstate_dump_file) {
4046 error_report("only one '-dump-vmstate' "
4047 "option may be given");
4048 exit(1);
4050 vmstate_dump_file = fopen(optarg, "w");
4051 if (vmstate_dump_file == NULL) {
4052 error_report("open %s: %s", optarg, strerror(errno));
4053 exit(1);
4055 break;
4056 default:
4057 os_parse_cmd_args(popt->index, optarg);
4062 * Clear error location left behind by the loop.
4063 * Best done right after the loop. Do not insert code here!
4065 loc_set_none();
4067 replay_configure(icount_opts);
4069 machine_class = select_machine();
4071 set_memory_options(&ram_slots, &maxram_size, machine_class);
4073 os_daemonize();
4075 if (pid_file && qemu_create_pidfile(pid_file) != 0) {
4076 error_report("could not acquire pid file: %s", strerror(errno));
4077 exit(1);
4080 if (qemu_init_main_loop(&main_loop_err)) {
4081 error_report_err(main_loop_err);
4082 exit(1);
4085 if (qemu_opts_foreach(qemu_find_opts("sandbox"),
4086 parse_sandbox, NULL, NULL)) {
4087 exit(1);
4090 if (qemu_opts_foreach(qemu_find_opts("name"),
4091 parse_name, NULL, NULL)) {
4092 exit(1);
4095 #ifndef _WIN32
4096 if (qemu_opts_foreach(qemu_find_opts("add-fd"),
4097 parse_add_fd, NULL, NULL)) {
4098 exit(1);
4101 if (qemu_opts_foreach(qemu_find_opts("add-fd"),
4102 cleanup_add_fd, NULL, NULL)) {
4103 exit(1);
4105 #endif
4107 current_machine = MACHINE(object_new(object_class_get_name(
4108 OBJECT_CLASS(machine_class))));
4109 if (machine_help_func(qemu_get_machine_opts(), current_machine)) {
4110 exit(0);
4112 object_property_add_child(object_get_root(), "machine",
4113 OBJECT(current_machine), &error_abort);
4115 if (machine_class->minimum_page_bits) {
4116 if (!set_preferred_target_page_bits(machine_class->minimum_page_bits)) {
4117 /* This would be a board error: specifying a minimum smaller than
4118 * a target's compile-time fixed setting.
4120 g_assert_not_reached();
4124 cpu_exec_init_all();
4126 if (machine_class->hw_version) {
4127 qemu_set_hw_version(machine_class->hw_version);
4130 if (cpu_model && is_help_option(cpu_model)) {
4131 list_cpus(stdout, &fprintf, cpu_model);
4132 exit(0);
4135 if (!trace_init_backends()) {
4136 exit(1);
4138 trace_init_file(trace_file);
4140 /* Open the logfile at this point and set the log mask if necessary.
4142 if (log_file) {
4143 qemu_set_log_filename(log_file, &error_fatal);
4146 if (log_mask) {
4147 int mask;
4148 mask = qemu_str_to_log_mask(log_mask);
4149 if (!mask) {
4150 qemu_print_log_usage(stdout);
4151 exit(1);
4153 qemu_set_log(mask);
4154 } else {
4155 qemu_set_log(0);
4158 /* If no data_dir is specified then try to find it relative to the
4159 executable path. */
4160 if (data_dir_idx < ARRAY_SIZE(data_dir)) {
4161 data_dir[data_dir_idx] = os_find_datadir();
4162 if (data_dir[data_dir_idx] != NULL) {
4163 data_dir_idx++;
4166 /* If all else fails use the install path specified when building. */
4167 if (data_dir_idx < ARRAY_SIZE(data_dir)) {
4168 data_dir[data_dir_idx++] = CONFIG_QEMU_DATADIR;
4171 /* -L help lists the data directories and exits. */
4172 if (list_data_dirs) {
4173 for (i = 0; i < data_dir_idx; i++) {
4174 printf("%s\n", data_dir[i]);
4176 exit(0);
4179 smp_parse(qemu_opts_find(qemu_find_opts("smp-opts"), NULL));
4181 machine_class->max_cpus = machine_class->max_cpus ?: 1; /* Default to UP */
4182 if (max_cpus > machine_class->max_cpus) {
4183 error_report("Number of SMP CPUs requested (%d) exceeds max CPUs "
4184 "supported by machine '%s' (%d)", max_cpus,
4185 machine_class->name, machine_class->max_cpus);
4186 exit(1);
4190 * Get the default machine options from the machine if it is not already
4191 * specified either by the configuration file or by the command line.
4193 if (machine_class->default_machine_opts) {
4194 qemu_opts_set_defaults(qemu_find_opts("machine"),
4195 machine_class->default_machine_opts, 0);
4198 qemu_opts_foreach(qemu_find_opts("device"),
4199 default_driver_check, NULL, NULL);
4200 qemu_opts_foreach(qemu_find_opts("global"),
4201 default_driver_check, NULL, NULL);
4203 if (!vga_model && !default_vga) {
4204 vga_interface_type = VGA_DEVICE;
4206 if (!has_defaults || machine_class->no_serial) {
4207 default_serial = 0;
4209 if (!has_defaults || machine_class->no_parallel) {
4210 default_parallel = 0;
4212 if (!has_defaults || !machine_class->use_virtcon) {
4213 default_virtcon = 0;
4215 if (!has_defaults || !machine_class->use_sclp) {
4216 default_sclp = 0;
4218 if (!has_defaults || machine_class->no_floppy) {
4219 default_floppy = 0;
4221 if (!has_defaults || machine_class->no_cdrom) {
4222 default_cdrom = 0;
4224 if (!has_defaults || machine_class->no_sdcard) {
4225 default_sdcard = 0;
4227 if (!has_defaults) {
4228 default_monitor = 0;
4229 default_net = 0;
4230 default_vga = 0;
4233 if (is_daemonized()) {
4234 /* According to documentation and historically, -nographic redirects
4235 * serial port, parallel port and monitor to stdio, which does not work
4236 * with -daemonize. We can redirect these to null instead, but since
4237 * -nographic is legacy, let's just error out.
4238 * We disallow -nographic only if all other ports are not redirected
4239 * explicitly, to not break existing legacy setups which uses
4240 * -nographic _and_ redirects all ports explicitly - this is valid
4241 * usage, -nographic is just a no-op in this case.
4243 if (nographic
4244 && (default_parallel || default_serial
4245 || default_monitor || default_virtcon)) {
4246 error_report("-nographic cannot be used with -daemonize");
4247 exit(1);
4249 #ifdef CONFIG_CURSES
4250 if (display_type == DT_CURSES) {
4251 error_report("curses display cannot be used with -daemonize");
4252 exit(1);
4254 #endif
4257 if (nographic) {
4258 if (default_parallel)
4259 add_device_config(DEV_PARALLEL, "null");
4260 if (default_serial && default_monitor) {
4261 add_device_config(DEV_SERIAL, "mon:stdio");
4262 } else if (default_virtcon && default_monitor) {
4263 add_device_config(DEV_VIRTCON, "mon:stdio");
4264 } else if (default_sclp && default_monitor) {
4265 add_device_config(DEV_SCLP, "mon:stdio");
4266 } else {
4267 if (default_serial)
4268 add_device_config(DEV_SERIAL, "stdio");
4269 if (default_virtcon)
4270 add_device_config(DEV_VIRTCON, "stdio");
4271 if (default_sclp) {
4272 add_device_config(DEV_SCLP, "stdio");
4274 if (default_monitor)
4275 monitor_parse("stdio", "readline", false);
4277 } else {
4278 if (default_serial)
4279 add_device_config(DEV_SERIAL, "vc:80Cx24C");
4280 if (default_parallel)
4281 add_device_config(DEV_PARALLEL, "vc:80Cx24C");
4282 if (default_monitor)
4283 monitor_parse("vc:80Cx24C", "readline", false);
4284 if (default_virtcon)
4285 add_device_config(DEV_VIRTCON, "vc:80Cx24C");
4286 if (default_sclp) {
4287 add_device_config(DEV_SCLP, "vc:80Cx24C");
4291 #if defined(CONFIG_VNC)
4292 if (!QTAILQ_EMPTY(&(qemu_find_opts("vnc")->head))) {
4293 display_remote++;
4295 #endif
4296 if (display_type == DT_DEFAULT && !display_remote) {
4297 #if defined(CONFIG_GTK)
4298 display_type = DT_GTK;
4299 #elif defined(CONFIG_SDL)
4300 display_type = DT_SDL;
4301 #elif defined(CONFIG_COCOA)
4302 display_type = DT_COCOA;
4303 #elif defined(CONFIG_VNC)
4304 vnc_parse("localhost:0,to=99,id=default", &error_abort);
4305 #else
4306 display_type = DT_NONE;
4307 #endif
4310 if ((no_frame || alt_grab || ctrl_grab) && display_type != DT_SDL) {
4311 error_report("-no-frame, -alt-grab and -ctrl-grab are only valid "
4312 "for SDL, ignoring option");
4314 if (no_quit && (display_type != DT_GTK && display_type != DT_SDL)) {
4315 error_report("-no-quit is only valid for GTK and SDL, "
4316 "ignoring option");
4319 if (display_type == DT_GTK) {
4320 early_gtk_display_init(request_opengl);
4323 if (display_type == DT_SDL) {
4324 sdl_display_early_init(request_opengl);
4327 if (request_opengl == 1 && display_opengl == 0) {
4328 #if defined(CONFIG_OPENGL)
4329 error_report("OpenGL is not supported by the display");
4330 #else
4331 error_report("OpenGL support is disabled");
4332 #endif
4333 exit(1);
4336 page_size_init();
4337 socket_init();
4339 if (qemu_opts_foreach(qemu_find_opts("object"),
4340 user_creatable_add_opts_foreach,
4341 object_create_initial, NULL)) {
4342 exit(1);
4345 if (qemu_opts_foreach(qemu_find_opts("chardev"),
4346 chardev_init_func, NULL, NULL)) {
4347 exit(1);
4350 #ifdef CONFIG_VIRTFS
4351 if (qemu_opts_foreach(qemu_find_opts("fsdev"),
4352 fsdev_init_func, NULL, NULL)) {
4353 exit(1);
4355 #endif
4357 if (qemu_opts_foreach(qemu_find_opts("device"),
4358 device_help_func, NULL, NULL)) {
4359 exit(0);
4362 machine_opts = qemu_get_machine_opts();
4363 if (qemu_opt_foreach(machine_opts, machine_set_property, current_machine,
4364 NULL)) {
4365 object_unref(OBJECT(current_machine));
4366 exit(1);
4369 configure_accelerator(current_machine);
4371 if (qtest_chrdev) {
4372 qtest_init(qtest_chrdev, qtest_log, &error_fatal);
4375 machine_opts = qemu_get_machine_opts();
4376 kernel_filename = qemu_opt_get(machine_opts, "kernel");
4377 initrd_filename = qemu_opt_get(machine_opts, "initrd");
4378 kernel_cmdline = qemu_opt_get(machine_opts, "append");
4379 bios_name = qemu_opt_get(machine_opts, "firmware");
4381 opts = qemu_opts_find(qemu_find_opts("boot-opts"), NULL);
4382 if (opts) {
4383 boot_order = qemu_opt_get(opts, "order");
4384 if (boot_order) {
4385 validate_bootdevices(boot_order, &error_fatal);
4388 boot_once = qemu_opt_get(opts, "once");
4389 if (boot_once) {
4390 validate_bootdevices(boot_once, &error_fatal);
4393 boot_menu = qemu_opt_get_bool(opts, "menu", boot_menu);
4394 boot_strict = qemu_opt_get_bool(opts, "strict", false);
4397 if (!boot_order) {
4398 boot_order = machine_class->default_boot_order;
4401 if (!kernel_cmdline) {
4402 kernel_cmdline = "";
4403 current_machine->kernel_cmdline = (char *)kernel_cmdline;
4406 linux_boot = (kernel_filename != NULL);
4408 if (!linux_boot && *kernel_cmdline != '\0') {
4409 error_report("-append only allowed with -kernel option");
4410 exit(1);
4413 if (!linux_boot && initrd_filename != NULL) {
4414 error_report("-initrd only allowed with -kernel option");
4415 exit(1);
4418 if (semihosting_enabled() && !semihosting_get_argc() && kernel_filename) {
4419 /* fall back to the -kernel/-append */
4420 semihosting_arg_fallback(kernel_filename, kernel_cmdline);
4423 os_set_line_buffering();
4425 /* spice needs the timers to be initialized by this point */
4426 qemu_spice_init();
4428 cpu_ticks_init();
4429 if (icount_opts) {
4430 if (!tcg_enabled()) {
4431 error_report("-icount is not allowed with hardware virtualization");
4432 exit(1);
4434 configure_icount(icount_opts, &error_abort);
4435 qemu_opts_del(icount_opts);
4438 if (default_net) {
4439 QemuOptsList *net = qemu_find_opts("net");
4440 qemu_opts_set(net, NULL, "type", "nic", &error_abort);
4441 #ifdef CONFIG_SLIRP
4442 qemu_opts_set(net, NULL, "type", "user", &error_abort);
4443 #endif
4446 colo_info_init();
4448 if (net_init_clients() < 0) {
4449 exit(1);
4452 if (qemu_opts_foreach(qemu_find_opts("object"),
4453 user_creatable_add_opts_foreach,
4454 object_create_delayed, NULL)) {
4455 exit(1);
4458 #ifdef CONFIG_TPM
4459 if (tpm_init() < 0) {
4460 exit(1);
4462 #endif
4464 /* init the bluetooth world */
4465 if (foreach_device_config(DEV_BT, bt_parse))
4466 exit(1);
4468 if (!xen_enabled()) {
4469 /* On 32-bit hosts, QEMU is limited by virtual address space */
4470 if (ram_size > (2047 << 20) && HOST_LONG_BITS == 32) {
4471 error_report("at most 2047 MB RAM can be simulated");
4472 exit(1);
4476 blk_mig_init();
4477 ram_mig_init();
4479 /* If the currently selected machine wishes to override the units-per-bus
4480 * property of its default HBA interface type, do so now. */
4481 if (machine_class->units_per_default_bus) {
4482 override_max_devs(machine_class->block_default_type,
4483 machine_class->units_per_default_bus);
4486 /* open the virtual block devices */
4487 if (snapshot || replay_mode != REPLAY_MODE_NONE) {
4488 qemu_opts_foreach(qemu_find_opts("drive"), drive_enable_snapshot,
4489 NULL, NULL);
4491 if (qemu_opts_foreach(qemu_find_opts("drive"), drive_init_func,
4492 &machine_class->block_default_type, NULL)) {
4493 exit(1);
4496 default_drive(default_cdrom, snapshot, machine_class->block_default_type, 2,
4497 CDROM_OPTS);
4498 default_drive(default_floppy, snapshot, IF_FLOPPY, 0, FD_OPTS);
4499 default_drive(default_sdcard, snapshot, IF_SD, 0, SD_OPTS);
4501 parse_numa_opts(machine_class);
4503 if (qemu_opts_foreach(qemu_find_opts("mon"),
4504 mon_init_func, NULL, NULL)) {
4505 exit(1);
4508 if (foreach_device_config(DEV_SERIAL, serial_parse) < 0)
4509 exit(1);
4510 if (foreach_device_config(DEV_PARALLEL, parallel_parse) < 0)
4511 exit(1);
4512 if (foreach_device_config(DEV_VIRTCON, virtcon_parse) < 0)
4513 exit(1);
4514 if (foreach_device_config(DEV_SCLP, sclp_parse) < 0) {
4515 exit(1);
4517 if (foreach_device_config(DEV_DEBUGCON, debugcon_parse) < 0)
4518 exit(1);
4520 /* If no default VGA is requested, the default is "none". */
4521 if (default_vga) {
4522 if (machine_class->default_display) {
4523 vga_model = machine_class->default_display;
4524 } else if (vga_interface_available(VGA_CIRRUS)) {
4525 vga_model = "cirrus";
4526 } else if (vga_interface_available(VGA_STD)) {
4527 vga_model = "std";
4530 if (vga_model) {
4531 select_vgahw(vga_model);
4534 if (watchdog) {
4535 i = select_watchdog(watchdog);
4536 if (i > 0)
4537 exit (i == 1 ? 1 : 0);
4540 machine_register_compat_props(current_machine);
4542 qemu_opts_foreach(qemu_find_opts("global"),
4543 global_init_func, NULL, NULL);
4545 /* This checkpoint is required by replay to separate prior clock
4546 reading from the other reads, because timer polling functions query
4547 clock values from the log. */
4548 replay_checkpoint(CHECKPOINT_INIT);
4549 qdev_machine_init();
4551 current_machine->ram_size = ram_size;
4552 current_machine->maxram_size = maxram_size;
4553 current_machine->ram_slots = ram_slots;
4554 current_machine->boot_order = boot_order;
4555 current_machine->cpu_model = cpu_model;
4557 machine_class->init(current_machine);
4559 realtime_init();
4561 audio_init();
4563 cpu_synchronize_all_post_init();
4565 numa_post_machine_init();
4567 if (hax_enabled()) {
4568 hax_sync_vcpus();
4571 if (qemu_opts_foreach(qemu_find_opts("fw_cfg"),
4572 parse_fw_cfg, fw_cfg_find(), NULL) != 0) {
4573 exit(1);
4576 /* init USB devices */
4577 if (machine_usb(current_machine)) {
4578 if (foreach_device_config(DEV_USB, usb_parse) < 0)
4579 exit(1);
4582 /* Check if IGD GFX passthrough. */
4583 igd_gfx_passthru();
4585 /* init generic devices */
4586 rom_set_order_override(FW_CFG_ORDER_OVERRIDE_DEVICE);
4587 if (qemu_opts_foreach(qemu_find_opts("device"),
4588 device_init_func, NULL, NULL)) {
4589 exit(1);
4591 rom_reset_order_override();
4593 /* Did we create any drives that we failed to create a device for? */
4594 drive_check_orphaned();
4596 /* Don't warn about the default network setup that you get if
4597 * no command line -net or -netdev options are specified. There
4598 * are two cases that we would otherwise complain about:
4599 * (1) board doesn't support a NIC but the implicit "-net nic"
4600 * requested one
4601 * (2) CONFIG_SLIRP not set, in which case the implicit "-net nic"
4602 * sets up a nic that isn't connected to anything.
4604 if (!default_net) {
4605 net_check_clients();
4609 if (boot_once) {
4610 qemu_boot_set(boot_once, &error_fatal);
4611 qemu_register_reset(restore_boot_order, g_strdup(boot_order));
4614 ds = init_displaystate();
4616 /* init local displays */
4617 switch (display_type) {
4618 case DT_CURSES:
4619 curses_display_init(ds, full_screen);
4620 break;
4621 case DT_SDL:
4622 sdl_display_init(ds, full_screen, no_frame);
4623 break;
4624 case DT_COCOA:
4625 cocoa_display_init(ds, full_screen);
4626 break;
4627 case DT_GTK:
4628 gtk_display_init(ds, full_screen, grab_on_hover);
4629 break;
4630 default:
4631 break;
4634 /* must be after terminal init, SDL library changes signal handlers */
4635 os_setup_signal_handling();
4637 /* init remote displays */
4638 #ifdef CONFIG_VNC
4639 qemu_opts_foreach(qemu_find_opts("vnc"),
4640 vnc_init_func, NULL, NULL);
4641 #endif
4643 if (using_spice) {
4644 qemu_spice_display_init();
4647 if (foreach_device_config(DEV_GDB, gdbserver_start) < 0) {
4648 exit(1);
4651 qdev_machine_creation_done();
4653 /* TODO: once all bus devices are qdevified, this should be done
4654 * when bus is created by qdev.c */
4655 qemu_register_reset(qbus_reset_all_fn, sysbus_get_default());
4656 qemu_run_machine_init_done_notifiers();
4658 if (rom_check_and_register_reset() != 0) {
4659 error_report("rom check and register reset failed");
4660 exit(1);
4663 replay_start();
4665 /* This checkpoint is required by replay to separate prior clock
4666 reading from the other reads, because timer polling functions query
4667 clock values from the log. */
4668 replay_checkpoint(CHECKPOINT_RESET);
4669 qemu_system_reset(VMRESET_SILENT);
4670 register_global_state();
4671 if (loadvm) {
4672 if (load_vmstate(loadvm) < 0) {
4673 autostart = 0;
4677 qdev_prop_check_globals();
4678 if (vmstate_dump_file) {
4679 /* dump and exit */
4680 dump_vmstate_json_to_file(vmstate_dump_file);
4681 return 0;
4684 if (incoming) {
4685 Error *local_err = NULL;
4686 qemu_start_incoming_migration(incoming, &local_err);
4687 if (local_err) {
4688 error_reportf_err(local_err, "-incoming %s: ", incoming);
4689 exit(1);
4691 } else if (autostart) {
4692 vm_start();
4695 os_setup_post();
4697 main_loop();
4698 replay_disable_events();
4699 iothread_stop_all();
4701 bdrv_close_all();
4702 pause_all_vcpus();
4703 res_free();
4705 /* vhost-user must be cleaned up before chardevs. */
4706 net_cleanup();
4707 audio_cleanup();
4708 monitor_cleanup();
4709 qemu_chr_cleanup();
4711 return 0;