Merge remote-tracking branch 'qemu/master'
[qemu/ar7.git] / vl.c
blobe75b962d2205461b618a50256cc3554c7bd3f3a5
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/xen/xen.h"
76 #include "hw/qdev.h"
77 #include "hw/loader.h"
78 #include "monitor/qdev.h"
79 #include "sysemu/bt.h"
80 #include "net/net.h"
81 #include "net/slirp.h"
82 #include "monitor/monitor.h"
83 #include "ui/console.h"
84 #include "ui/input.h"
85 #include "sysemu/sysemu.h"
86 #include "sysemu/numa.h"
87 #include "exec/gdbstub.h"
88 #include "qemu/timer.h"
89 #include "sysemu/char.h"
90 #include "qemu/bitmap.h"
91 #include "qemu/log.h"
92 #include "sysemu/blockdev.h"
93 #include "hw/block/block.h"
94 #include "migration/block.h"
95 #include "sysemu/tpm.h"
96 #include "sysemu/dma.h"
97 #include "audio/audio.h"
98 #include "migration/migration.h"
99 #include "sysemu/cpus.h"
100 #include "migration/colo.h"
101 #include "sysemu/kvm.h"
102 #include "sysemu/hax.h"
103 #include "qapi/qmp/qjson.h"
104 #include "qemu/option.h"
105 #include "qemu/config-file.h"
106 #include "qemu-options.h"
107 #include "qmp-commands.h"
108 #include "qemu/main-loop.h"
109 #ifdef CONFIG_VIRTFS
110 #include "fsdev/qemu-fsdev.h"
111 #endif
112 #include "sysemu/qtest.h"
114 #include "disas/disas.h"
117 #include "slirp/libslirp.h"
119 #include "trace.h"
120 #include "trace/control.h"
121 #include "qemu/queue.h"
122 #include "sysemu/arch_init.h"
124 #include "ui/qemu-spice.h"
125 #include "qapi/string-input-visitor.h"
126 #include "qapi/opts-visitor.h"
127 #include "qom/object_interfaces.h"
128 #include "qapi-event.h"
129 #include "exec/semihost.h"
130 #include "crypto/init.h"
131 #include "sysemu/replay.h"
132 #include "qapi/qmp/qerror.h"
133 #include "sysemu/iothread.h"
135 #define MAX_VIRTIO_CONSOLES 1
136 #define MAX_SCLP_CONSOLES 1
138 static const char *data_dir[16];
139 static int data_dir_idx;
140 const char *bios_name = NULL;
141 enum vga_retrace_method vga_retrace_method = VGA_RETRACE_DUMB;
142 int request_opengl = -1;
143 int display_opengl;
144 const char* keyboard_layout = NULL;
145 ram_addr_t ram_size;
146 const char *mem_path = NULL;
147 int mem_prealloc = 0; /* force preallocation of physical target memory */
148 bool enable_mlock = false;
149 int nb_nics;
150 NICInfo nd_table[MAX_NICS];
151 int autostart;
152 static int rtc_utc = 1;
153 static int rtc_date_offset = -1; /* -1 means no change */
154 QEMUClockType rtc_clock;
155 int vga_interface_type = VGA_NONE;
156 static int full_screen = 0;
157 static int no_frame = 0;
158 int no_quit = 0;
159 static bool grab_on_hover;
160 CharDriverState *serial_hds[MAX_SERIAL_PORTS];
161 CharDriverState *parallel_hds[MAX_PARALLEL_PORTS];
162 CharDriverState *virtcon_hds[MAX_VIRTIO_CONSOLES];
163 CharDriverState *sclp_hds[MAX_SCLP_CONSOLES];
164 int win2k_install_hack = 0;
165 int singlestep = 0;
166 int smp_cpus = 1;
167 int max_cpus = 1;
168 int smp_cores = 1;
169 int smp_threads = 1;
170 int acpi_enabled = 1;
171 int no_hpet = 0;
172 int fd_bootchk = 1;
173 static int no_reboot;
174 int no_shutdown = 0;
175 int cursor_hide = 1;
176 int graphic_rotate = 0;
177 const char *watchdog;
178 QEMUOptionRom option_rom[MAX_OPTION_ROMS];
179 int nb_option_roms;
180 int old_param = 0;
181 const char *qemu_name;
182 int alt_grab = 0;
183 int ctrl_grab = 0;
184 unsigned int nb_prom_envs = 0;
185 const char *prom_envs[MAX_PROM_ENVS];
186 int boot_menu;
187 bool boot_strict;
188 uint8_t *boot_splash_filedata;
189 size_t boot_splash_filedata_size;
190 uint8_t qemu_extra_params_fw[2];
192 int icount_align_option;
194 /* The bytes in qemu_uuid are in the order specified by RFC4122, _not_ in the
195 * little-endian "wire format" described in the SMBIOS 2.6 specification.
197 QemuUUID qemu_uuid;
198 bool qemu_uuid_set;
200 /* Trace unassigned memory or i/o accesses. */
201 bool trace_unassigned;
203 static NotifierList exit_notifiers =
204 NOTIFIER_LIST_INITIALIZER(exit_notifiers);
206 static NotifierList machine_init_done_notifiers =
207 NOTIFIER_LIST_INITIALIZER(machine_init_done_notifiers);
209 bool xen_allowed;
210 uint32_t xen_domid;
211 enum xen_mode xen_mode = XEN_EMULATE;
213 static int has_defaults = 1;
214 static int default_serial = 1;
215 static int default_parallel = 1;
216 static int default_virtcon = 1;
217 static int default_sclp = 1;
218 static int default_monitor = 1;
219 static int default_floppy = 1;
220 static int default_cdrom = 1;
221 static int default_sdcard = 1;
222 static int default_vga = 1;
223 static int default_net = 1;
225 static struct {
226 const char *driver;
227 int *flag;
228 } default_list[] = {
229 { .driver = "isa-serial", .flag = &default_serial },
230 { .driver = "isa-parallel", .flag = &default_parallel },
231 { .driver = "isa-fdc", .flag = &default_floppy },
232 { .driver = "floppy", .flag = &default_floppy },
233 { .driver = "ide-cd", .flag = &default_cdrom },
234 { .driver = "ide-hd", .flag = &default_cdrom },
235 { .driver = "ide-drive", .flag = &default_cdrom },
236 { .driver = "scsi-cd", .flag = &default_cdrom },
237 { .driver = "virtio-serial-pci", .flag = &default_virtcon },
238 { .driver = "virtio-serial", .flag = &default_virtcon },
239 { .driver = "VGA", .flag = &default_vga },
240 { .driver = "isa-vga", .flag = &default_vga },
241 { .driver = "cirrus-vga", .flag = &default_vga },
242 { .driver = "isa-cirrus-vga", .flag = &default_vga },
243 { .driver = "vmware-svga", .flag = &default_vga },
244 { .driver = "qxl-vga", .flag = &default_vga },
245 { .driver = "virtio-vga", .flag = &default_vga },
248 static QemuOptsList qemu_rtc_opts = {
249 .name = "rtc",
250 .head = QTAILQ_HEAD_INITIALIZER(qemu_rtc_opts.head),
251 .desc = {
253 .name = "base",
254 .type = QEMU_OPT_STRING,
256 .name = "clock",
257 .type = QEMU_OPT_STRING,
259 .name = "driftfix",
260 .type = QEMU_OPT_STRING,
262 { /* end of list */ }
266 static QemuOptsList qemu_sandbox_opts = {
267 .name = "sandbox",
268 .implied_opt_name = "enable",
269 .head = QTAILQ_HEAD_INITIALIZER(qemu_sandbox_opts.head),
270 .desc = {
272 .name = "enable",
273 .type = QEMU_OPT_BOOL,
275 { /* end of list */ }
279 static QemuOptsList qemu_option_rom_opts = {
280 .name = "option-rom",
281 .implied_opt_name = "romfile",
282 .head = QTAILQ_HEAD_INITIALIZER(qemu_option_rom_opts.head),
283 .desc = {
285 .name = "bootindex",
286 .type = QEMU_OPT_NUMBER,
287 }, {
288 .name = "romfile",
289 .type = QEMU_OPT_STRING,
291 { /* end of list */ }
295 static QemuOptsList qemu_machine_opts = {
296 .name = "machine",
297 .implied_opt_name = "type",
298 .merge_lists = true,
299 .head = QTAILQ_HEAD_INITIALIZER(qemu_machine_opts.head),
300 .desc = {
302 * no elements => accept any
303 * sanity checking will happen later
304 * when setting machine properties
310 static QemuOptsList qemu_boot_opts = {
311 .name = "boot-opts",
312 .implied_opt_name = "order",
313 .merge_lists = true,
314 .head = QTAILQ_HEAD_INITIALIZER(qemu_boot_opts.head),
315 .desc = {
317 .name = "order",
318 .type = QEMU_OPT_STRING,
319 }, {
320 .name = "once",
321 .type = QEMU_OPT_STRING,
322 }, {
323 .name = "menu",
324 .type = QEMU_OPT_BOOL,
325 }, {
326 .name = "splash",
327 .type = QEMU_OPT_STRING,
328 }, {
329 .name = "splash-time",
330 .type = QEMU_OPT_STRING,
331 }, {
332 .name = "reboot-timeout",
333 .type = QEMU_OPT_STRING,
334 }, {
335 .name = "strict",
336 .type = QEMU_OPT_BOOL,
338 { /*End of list */ }
342 static QemuOptsList qemu_add_fd_opts = {
343 .name = "add-fd",
344 .head = QTAILQ_HEAD_INITIALIZER(qemu_add_fd_opts.head),
345 .desc = {
347 .name = "fd",
348 .type = QEMU_OPT_NUMBER,
349 .help = "file descriptor of which a duplicate is added to fd set",
351 .name = "set",
352 .type = QEMU_OPT_NUMBER,
353 .help = "ID of the fd set to add fd to",
355 .name = "opaque",
356 .type = QEMU_OPT_STRING,
357 .help = "free-form string used to describe fd",
359 { /* end of list */ }
363 static QemuOptsList qemu_object_opts = {
364 .name = "object",
365 .implied_opt_name = "qom-type",
366 .head = QTAILQ_HEAD_INITIALIZER(qemu_object_opts.head),
367 .desc = {
372 static QemuOptsList qemu_tpmdev_opts = {
373 .name = "tpmdev",
374 .implied_opt_name = "type",
375 .head = QTAILQ_HEAD_INITIALIZER(qemu_tpmdev_opts.head),
376 .desc = {
377 /* options are defined in the TPM backends */
378 { /* end of list */ }
382 static QemuOptsList qemu_realtime_opts = {
383 .name = "realtime",
384 .head = QTAILQ_HEAD_INITIALIZER(qemu_realtime_opts.head),
385 .desc = {
387 .name = "mlock",
388 .type = QEMU_OPT_BOOL,
390 { /* end of list */ }
394 static QemuOptsList qemu_msg_opts = {
395 .name = "msg",
396 .head = QTAILQ_HEAD_INITIALIZER(qemu_msg_opts.head),
397 .desc = {
399 .name = "timestamp",
400 .type = QEMU_OPT_BOOL,
402 { /* end of list */ }
406 static QemuOptsList qemu_name_opts = {
407 .name = "name",
408 .implied_opt_name = "guest",
409 .merge_lists = true,
410 .head = QTAILQ_HEAD_INITIALIZER(qemu_name_opts.head),
411 .desc = {
413 .name = "guest",
414 .type = QEMU_OPT_STRING,
415 .help = "Sets the name of the guest.\n"
416 "This name will be displayed in the SDL window caption.\n"
417 "The name will also be used for the VNC server",
418 }, {
419 .name = "process",
420 .type = QEMU_OPT_STRING,
421 .help = "Sets the name of the QEMU process, as shown in top etc",
422 }, {
423 .name = "debug-threads",
424 .type = QEMU_OPT_BOOL,
425 .help = "When enabled, name the individual threads; defaults off.\n"
426 "NOTE: The thread names are for debugging and not a\n"
427 "stable API.",
429 { /* End of list */ }
433 static QemuOptsList qemu_mem_opts = {
434 .name = "memory",
435 .implied_opt_name = "size",
436 .head = QTAILQ_HEAD_INITIALIZER(qemu_mem_opts.head),
437 .merge_lists = true,
438 .desc = {
440 .name = "size",
441 .type = QEMU_OPT_SIZE,
444 .name = "slots",
445 .type = QEMU_OPT_NUMBER,
448 .name = "maxmem",
449 .type = QEMU_OPT_SIZE,
451 { /* end of list */ }
455 static QemuOptsList qemu_icount_opts = {
456 .name = "icount",
457 .implied_opt_name = "shift",
458 .merge_lists = true,
459 .head = QTAILQ_HEAD_INITIALIZER(qemu_icount_opts.head),
460 .desc = {
462 .name = "shift",
463 .type = QEMU_OPT_STRING,
464 }, {
465 .name = "align",
466 .type = QEMU_OPT_BOOL,
467 }, {
468 .name = "sleep",
469 .type = QEMU_OPT_BOOL,
470 }, {
471 .name = "rr",
472 .type = QEMU_OPT_STRING,
473 }, {
474 .name = "rrfile",
475 .type = QEMU_OPT_STRING,
477 { /* end of list */ }
481 static QemuOptsList qemu_semihosting_config_opts = {
482 .name = "semihosting-config",
483 .implied_opt_name = "enable",
484 .head = QTAILQ_HEAD_INITIALIZER(qemu_semihosting_config_opts.head),
485 .desc = {
487 .name = "enable",
488 .type = QEMU_OPT_BOOL,
489 }, {
490 .name = "target",
491 .type = QEMU_OPT_STRING,
492 }, {
493 .name = "arg",
494 .type = QEMU_OPT_STRING,
496 { /* end of list */ }
500 static QemuOptsList qemu_fw_cfg_opts = {
501 .name = "fw_cfg",
502 .implied_opt_name = "name",
503 .head = QTAILQ_HEAD_INITIALIZER(qemu_fw_cfg_opts.head),
504 .desc = {
506 .name = "name",
507 .type = QEMU_OPT_STRING,
508 .help = "Sets the fw_cfg name of the blob to be inserted",
509 }, {
510 .name = "file",
511 .type = QEMU_OPT_STRING,
512 .help = "Sets the name of the file from which\n"
513 "the fw_cfg blob will be loaded",
514 }, {
515 .name = "string",
516 .type = QEMU_OPT_STRING,
517 .help = "Sets content of the blob to be inserted from a string",
519 { /* end of list */ }
523 #ifdef CONFIG_LIBISCSI
524 static QemuOptsList qemu_iscsi_opts = {
525 .name = "iscsi",
526 .head = QTAILQ_HEAD_INITIALIZER(qemu_iscsi_opts.head),
527 .desc = {
529 .name = "user",
530 .type = QEMU_OPT_STRING,
531 .help = "username for CHAP authentication to target",
533 .name = "password",
534 .type = QEMU_OPT_STRING,
535 .help = "password for CHAP authentication to target",
537 .name = "password-secret",
538 .type = QEMU_OPT_STRING,
539 .help = "ID of the secret providing password for CHAP "
540 "authentication to target",
542 .name = "header-digest",
543 .type = QEMU_OPT_STRING,
544 .help = "HeaderDigest setting. "
545 "{CRC32C|CRC32C-NONE|NONE-CRC32C|NONE}",
547 .name = "initiator-name",
548 .type = QEMU_OPT_STRING,
549 .help = "Initiator iqn name to use when connecting",
551 .name = "timeout",
552 .type = QEMU_OPT_NUMBER,
553 .help = "Request timeout in seconds (default 0 = no timeout)",
555 { /* end of list */ }
558 #endif
561 * Get machine options
563 * Returns: machine options (never null).
565 QemuOpts *qemu_get_machine_opts(void)
567 return qemu_find_opts_singleton("machine");
570 const char *qemu_get_vm_name(void)
572 return qemu_name;
575 static void res_free(void)
577 g_free(boot_splash_filedata);
578 boot_splash_filedata = NULL;
581 static int default_driver_check(void *opaque, QemuOpts *opts, Error **errp)
583 const char *driver = qemu_opt_get(opts, "driver");
584 int i;
586 if (!driver)
587 return 0;
588 for (i = 0; i < ARRAY_SIZE(default_list); i++) {
589 if (strcmp(default_list[i].driver, driver) != 0)
590 continue;
591 *(default_list[i].flag) = 0;
593 return 0;
596 /***********************************************************/
597 /* QEMU state */
599 static RunState current_run_state = RUN_STATE_PRELAUNCH;
601 /* We use RUN_STATE__MAX but any invalid value will do */
602 static RunState vmstop_requested = RUN_STATE__MAX;
603 static QemuMutex vmstop_lock;
605 typedef struct {
606 RunState from;
607 RunState to;
608 } RunStateTransition;
610 static const RunStateTransition runstate_transitions_def[] = {
611 /* from -> to */
612 { RUN_STATE_DEBUG, RUN_STATE_RUNNING },
613 { RUN_STATE_DEBUG, RUN_STATE_FINISH_MIGRATE },
614 { RUN_STATE_DEBUG, RUN_STATE_PRELAUNCH },
616 { RUN_STATE_INMIGRATE, RUN_STATE_INTERNAL_ERROR },
617 { RUN_STATE_INMIGRATE, RUN_STATE_IO_ERROR },
618 { RUN_STATE_INMIGRATE, RUN_STATE_PAUSED },
619 { RUN_STATE_INMIGRATE, RUN_STATE_RUNNING },
620 { RUN_STATE_INMIGRATE, RUN_STATE_SHUTDOWN },
621 { RUN_STATE_INMIGRATE, RUN_STATE_SUSPENDED },
622 { RUN_STATE_INMIGRATE, RUN_STATE_WATCHDOG },
623 { RUN_STATE_INMIGRATE, RUN_STATE_GUEST_PANICKED },
624 { RUN_STATE_INMIGRATE, RUN_STATE_FINISH_MIGRATE },
625 { RUN_STATE_INMIGRATE, RUN_STATE_PRELAUNCH },
626 { RUN_STATE_INMIGRATE, RUN_STATE_POSTMIGRATE },
627 { RUN_STATE_INMIGRATE, RUN_STATE_COLO },
629 { RUN_STATE_INTERNAL_ERROR, RUN_STATE_PAUSED },
630 { RUN_STATE_INTERNAL_ERROR, RUN_STATE_FINISH_MIGRATE },
631 { RUN_STATE_INTERNAL_ERROR, RUN_STATE_PRELAUNCH },
633 { RUN_STATE_IO_ERROR, RUN_STATE_RUNNING },
634 { RUN_STATE_IO_ERROR, RUN_STATE_FINISH_MIGRATE },
635 { RUN_STATE_IO_ERROR, RUN_STATE_PRELAUNCH },
637 { RUN_STATE_PAUSED, RUN_STATE_RUNNING },
638 { RUN_STATE_PAUSED, RUN_STATE_FINISH_MIGRATE },
639 { RUN_STATE_PAUSED, RUN_STATE_PRELAUNCH },
640 { RUN_STATE_PAUSED, RUN_STATE_COLO},
642 { RUN_STATE_POSTMIGRATE, RUN_STATE_RUNNING },
643 { RUN_STATE_POSTMIGRATE, RUN_STATE_FINISH_MIGRATE },
644 { RUN_STATE_POSTMIGRATE, RUN_STATE_PRELAUNCH },
646 { RUN_STATE_PRELAUNCH, RUN_STATE_RUNNING },
647 { RUN_STATE_PRELAUNCH, RUN_STATE_FINISH_MIGRATE },
648 { RUN_STATE_PRELAUNCH, RUN_STATE_INMIGRATE },
650 { RUN_STATE_FINISH_MIGRATE, RUN_STATE_RUNNING },
651 { RUN_STATE_FINISH_MIGRATE, RUN_STATE_POSTMIGRATE },
652 { RUN_STATE_FINISH_MIGRATE, RUN_STATE_PRELAUNCH },
653 { RUN_STATE_FINISH_MIGRATE, RUN_STATE_COLO},
655 { RUN_STATE_RESTORE_VM, RUN_STATE_RUNNING },
656 { RUN_STATE_RESTORE_VM, RUN_STATE_PRELAUNCH },
658 { RUN_STATE_COLO, RUN_STATE_RUNNING },
660 { RUN_STATE_RUNNING, RUN_STATE_DEBUG },
661 { RUN_STATE_RUNNING, RUN_STATE_INTERNAL_ERROR },
662 { RUN_STATE_RUNNING, RUN_STATE_IO_ERROR },
663 { RUN_STATE_RUNNING, RUN_STATE_PAUSED },
664 { RUN_STATE_RUNNING, RUN_STATE_FINISH_MIGRATE },
665 { RUN_STATE_RUNNING, RUN_STATE_RESTORE_VM },
666 { RUN_STATE_RUNNING, RUN_STATE_SAVE_VM },
667 { RUN_STATE_RUNNING, RUN_STATE_SHUTDOWN },
668 { RUN_STATE_RUNNING, RUN_STATE_WATCHDOG },
669 { RUN_STATE_RUNNING, RUN_STATE_GUEST_PANICKED },
670 { RUN_STATE_RUNNING, RUN_STATE_COLO},
672 { RUN_STATE_SAVE_VM, RUN_STATE_RUNNING },
674 { RUN_STATE_SHUTDOWN, RUN_STATE_PAUSED },
675 { RUN_STATE_SHUTDOWN, RUN_STATE_FINISH_MIGRATE },
676 { RUN_STATE_SHUTDOWN, RUN_STATE_PRELAUNCH },
678 { RUN_STATE_DEBUG, RUN_STATE_SUSPENDED },
679 { RUN_STATE_RUNNING, RUN_STATE_SUSPENDED },
680 { RUN_STATE_SUSPENDED, RUN_STATE_RUNNING },
681 { RUN_STATE_SUSPENDED, RUN_STATE_FINISH_MIGRATE },
682 { RUN_STATE_SUSPENDED, RUN_STATE_PRELAUNCH },
683 { RUN_STATE_SUSPENDED, RUN_STATE_COLO},
685 { RUN_STATE_WATCHDOG, RUN_STATE_RUNNING },
686 { RUN_STATE_WATCHDOG, RUN_STATE_FINISH_MIGRATE },
687 { RUN_STATE_WATCHDOG, RUN_STATE_PRELAUNCH },
688 { RUN_STATE_WATCHDOG, RUN_STATE_COLO},
690 { RUN_STATE_GUEST_PANICKED, RUN_STATE_RUNNING },
691 { RUN_STATE_GUEST_PANICKED, RUN_STATE_FINISH_MIGRATE },
692 { RUN_STATE_GUEST_PANICKED, RUN_STATE_PRELAUNCH },
694 { RUN_STATE__MAX, RUN_STATE__MAX },
697 static bool runstate_valid_transitions[RUN_STATE__MAX][RUN_STATE__MAX];
699 bool runstate_check(RunState state)
701 return current_run_state == state;
704 bool runstate_store(char *str, size_t size)
706 const char *state = RunState_lookup[current_run_state];
707 size_t len = strlen(state) + 1;
709 if (len > size) {
710 return false;
712 memcpy(str, state, len);
713 return true;
716 static void runstate_init(void)
718 const RunStateTransition *p;
720 memset(&runstate_valid_transitions, 0, sizeof(runstate_valid_transitions));
721 for (p = &runstate_transitions_def[0]; p->from != RUN_STATE__MAX; p++) {
722 runstate_valid_transitions[p->from][p->to] = true;
725 qemu_mutex_init(&vmstop_lock);
728 /* This function will abort() on invalid state transitions */
729 void runstate_set(RunState new_state)
731 assert(new_state < RUN_STATE__MAX);
733 if (current_run_state == new_state) {
734 return;
737 if (!runstate_valid_transitions[current_run_state][new_state]) {
738 error_report("invalid runstate transition: '%s' -> '%s'",
739 RunState_lookup[current_run_state],
740 RunState_lookup[new_state]);
741 abort();
743 trace_runstate_set(new_state);
744 current_run_state = new_state;
747 int runstate_is_running(void)
749 return runstate_check(RUN_STATE_RUNNING);
752 bool runstate_needs_reset(void)
754 return runstate_check(RUN_STATE_INTERNAL_ERROR) ||
755 runstate_check(RUN_STATE_SHUTDOWN);
758 StatusInfo *qmp_query_status(Error **errp)
760 StatusInfo *info = g_malloc0(sizeof(*info));
762 info->running = runstate_is_running();
763 info->singlestep = singlestep;
764 info->status = current_run_state;
766 return info;
769 static bool qemu_vmstop_requested(RunState *r)
771 qemu_mutex_lock(&vmstop_lock);
772 *r = vmstop_requested;
773 vmstop_requested = RUN_STATE__MAX;
774 qemu_mutex_unlock(&vmstop_lock);
775 return *r < RUN_STATE__MAX;
778 void qemu_system_vmstop_request_prepare(void)
780 qemu_mutex_lock(&vmstop_lock);
783 void qemu_system_vmstop_request(RunState state)
785 vmstop_requested = state;
786 qemu_mutex_unlock(&vmstop_lock);
787 qemu_notify_event();
790 void vm_start(void)
792 RunState requested;
794 qemu_vmstop_requested(&requested);
795 if (runstate_is_running() && requested == RUN_STATE__MAX) {
796 return;
799 /* Ensure that a STOP/RESUME pair of events is emitted if a
800 * vmstop request was pending. The BLOCK_IO_ERROR event, for
801 * example, according to documentation is always followed by
802 * the STOP event.
804 if (runstate_is_running()) {
805 qapi_event_send_stop(&error_abort);
806 } else {
807 replay_enable_events();
808 cpu_enable_ticks();
809 runstate_set(RUN_STATE_RUNNING);
810 vm_state_notify(1, RUN_STATE_RUNNING);
811 resume_all_vcpus();
814 qapi_event_send_resume(&error_abort);
818 /***********************************************************/
819 /* real time host monotonic timer */
821 static time_t qemu_time(void)
823 return qemu_clock_get_ms(QEMU_CLOCK_HOST) / 1000;
826 /***********************************************************/
827 /* host time/date access */
828 void qemu_get_timedate(struct tm *tm, int offset)
830 time_t ti = qemu_time();
832 ti += offset;
833 if (rtc_date_offset == -1) {
834 if (rtc_utc)
835 gmtime_r(&ti, tm);
836 else
837 localtime_r(&ti, tm);
838 } else {
839 ti -= rtc_date_offset;
840 gmtime_r(&ti, tm);
844 int qemu_timedate_diff(struct tm *tm)
846 time_t seconds;
848 if (rtc_date_offset == -1)
849 if (rtc_utc)
850 seconds = mktimegm(tm);
851 else {
852 struct tm tmp = *tm;
853 tmp.tm_isdst = -1; /* use timezone to figure it out */
854 seconds = mktime(&tmp);
856 else
857 seconds = mktimegm(tm) + rtc_date_offset;
859 return seconds - qemu_time();
862 static void configure_rtc_date_offset(const char *startdate, int legacy)
864 time_t rtc_start_date;
865 struct tm tm;
867 if (!strcmp(startdate, "now") && legacy) {
868 rtc_date_offset = -1;
869 } else {
870 if (sscanf(startdate, "%d-%d-%dT%d:%d:%d",
871 &tm.tm_year,
872 &tm.tm_mon,
873 &tm.tm_mday,
874 &tm.tm_hour,
875 &tm.tm_min,
876 &tm.tm_sec) == 6) {
877 /* OK */
878 } else if (sscanf(startdate, "%d-%d-%d",
879 &tm.tm_year,
880 &tm.tm_mon,
881 &tm.tm_mday) == 3) {
882 tm.tm_hour = 0;
883 tm.tm_min = 0;
884 tm.tm_sec = 0;
885 } else {
886 goto date_fail;
888 tm.tm_year -= 1900;
889 tm.tm_mon--;
890 rtc_start_date = mktimegm(&tm);
891 if (rtc_start_date == -1) {
892 date_fail:
893 error_report("invalid date format");
894 error_printf("valid formats: "
895 "'2006-06-17T16:01:21' or '2006-06-17'\n");
896 exit(1);
898 rtc_date_offset = qemu_time() - rtc_start_date;
902 static void configure_rtc(QemuOpts *opts)
904 const char *value;
906 value = qemu_opt_get(opts, "base");
907 if (value) {
908 if (!strcmp(value, "utc")) {
909 rtc_utc = 1;
910 } else if (!strcmp(value, "localtime")) {
911 Error *blocker = NULL;
912 rtc_utc = 0;
913 error_setg(&blocker, QERR_REPLAY_NOT_SUPPORTED,
914 "-rtc base=localtime");
915 replay_add_blocker(blocker);
916 } else {
917 configure_rtc_date_offset(value, 0);
920 value = qemu_opt_get(opts, "clock");
921 if (value) {
922 if (!strcmp(value, "host")) {
923 rtc_clock = QEMU_CLOCK_HOST;
924 } else if (!strcmp(value, "rt")) {
925 rtc_clock = QEMU_CLOCK_REALTIME;
926 } else if (!strcmp(value, "vm")) {
927 rtc_clock = QEMU_CLOCK_VIRTUAL;
928 } else {
929 error_report("invalid option value '%s'", value);
930 exit(1);
933 value = qemu_opt_get(opts, "driftfix");
934 if (value) {
935 if (!strcmp(value, "slew")) {
936 static GlobalProperty slew_lost_ticks = {
937 .driver = "mc146818rtc",
938 .property = "lost_tick_policy",
939 .value = "slew",
942 qdev_prop_register_global(&slew_lost_ticks);
943 } else if (!strcmp(value, "none")) {
944 /* discard is default */
945 } else {
946 error_report("invalid option value '%s'", value);
947 exit(1);
952 /***********************************************************/
953 /* Bluetooth support */
954 static int nb_hcis;
955 static int cur_hci;
956 static struct HCIInfo *hci_table[MAX_NICS];
958 struct HCIInfo *qemu_next_hci(void)
960 if (cur_hci == nb_hcis)
961 return &null_hci;
963 return hci_table[cur_hci++];
966 static int bt_hci_parse(const char *str)
968 struct HCIInfo *hci;
969 bdaddr_t bdaddr;
971 if (nb_hcis >= MAX_NICS) {
972 error_report("too many bluetooth HCIs (max %i)", MAX_NICS);
973 return -1;
976 hci = hci_init(str);
977 if (!hci)
978 return -1;
980 bdaddr.b[0] = 0x52;
981 bdaddr.b[1] = 0x54;
982 bdaddr.b[2] = 0x00;
983 bdaddr.b[3] = 0x12;
984 bdaddr.b[4] = 0x34;
985 bdaddr.b[5] = 0x56 + nb_hcis;
986 hci->bdaddr_set(hci, bdaddr.b);
988 hci_table[nb_hcis++] = hci;
990 return 0;
993 static void bt_vhci_add(int vlan_id)
995 struct bt_scatternet_s *vlan = qemu_find_bt_vlan(vlan_id);
997 if (!vlan->slave)
998 error_report("warning: adding a VHCI to an empty scatternet %i",
999 vlan_id);
1001 bt_vhci_init(bt_new_hci(vlan));
1004 static struct bt_device_s *bt_device_add(const char *opt)
1006 struct bt_scatternet_s *vlan;
1007 int vlan_id = 0;
1008 char *endp = strstr(opt, ",vlan=");
1009 int len = (endp ? endp - opt : strlen(opt)) + 1;
1010 char devname[10];
1012 pstrcpy(devname, MIN(sizeof(devname), len), opt);
1014 if (endp) {
1015 vlan_id = strtol(endp + 6, &endp, 0);
1016 if (*endp) {
1017 error_report("unrecognised bluetooth vlan Id");
1018 return 0;
1022 vlan = qemu_find_bt_vlan(vlan_id);
1024 if (!vlan->slave)
1025 error_report("warning: adding a slave device to an empty scatternet %i",
1026 vlan_id);
1028 if (!strcmp(devname, "keyboard"))
1029 return bt_keyboard_init(vlan);
1031 error_report("unsupported bluetooth device '%s'", devname);
1032 return 0;
1035 static int bt_parse(const char *opt)
1037 const char *endp, *p;
1038 int vlan;
1040 if (strstart(opt, "hci", &endp)) {
1041 if (!*endp || *endp == ',') {
1042 if (*endp)
1043 if (!strstart(endp, ",vlan=", 0))
1044 opt = endp + 1;
1046 return bt_hci_parse(opt);
1048 } else if (strstart(opt, "vhci", &endp)) {
1049 if (!*endp || *endp == ',') {
1050 if (*endp) {
1051 if (strstart(endp, ",vlan=", &p)) {
1052 vlan = strtol(p, (char **) &endp, 0);
1053 if (*endp) {
1054 error_report("bad scatternet '%s'", p);
1055 return 1;
1057 } else {
1058 error_report("bad parameter '%s'", endp + 1);
1059 return 1;
1061 } else
1062 vlan = 0;
1064 bt_vhci_add(vlan);
1065 return 0;
1067 } else if (strstart(opt, "device:", &endp))
1068 return !bt_device_add(endp);
1070 error_report("bad bluetooth parameter '%s'", opt);
1071 return 1;
1074 static int parse_sandbox(void *opaque, QemuOpts *opts, Error **errp)
1076 /* FIXME: change this to true for 1.3 */
1077 if (qemu_opt_get_bool(opts, "enable", false)) {
1078 #ifdef CONFIG_SECCOMP
1079 if (seccomp_start() < 0) {
1080 error_report("failed to install seccomp syscall filter "
1081 "in the kernel");
1082 return -1;
1084 #else
1085 error_report("seccomp support is disabled");
1086 return -1;
1087 #endif
1090 return 0;
1093 static int parse_name(void *opaque, QemuOpts *opts, Error **errp)
1095 const char *proc_name;
1097 if (qemu_opt_get(opts, "debug-threads")) {
1098 qemu_thread_naming(qemu_opt_get_bool(opts, "debug-threads", false));
1100 qemu_name = qemu_opt_get(opts, "guest");
1102 proc_name = qemu_opt_get(opts, "process");
1103 if (proc_name) {
1104 os_set_proc_name(proc_name);
1107 return 0;
1110 bool defaults_enabled(void)
1112 return has_defaults;
1115 #ifndef _WIN32
1116 static int parse_add_fd(void *opaque, QemuOpts *opts, Error **errp)
1118 int fd, dupfd, flags;
1119 int64_t fdset_id;
1120 const char *fd_opaque = NULL;
1121 AddfdInfo *fdinfo;
1123 fd = qemu_opt_get_number(opts, "fd", -1);
1124 fdset_id = qemu_opt_get_number(opts, "set", -1);
1125 fd_opaque = qemu_opt_get(opts, "opaque");
1127 if (fd < 0) {
1128 error_report("fd option is required and must be non-negative");
1129 return -1;
1132 if (fd <= STDERR_FILENO) {
1133 error_report("fd cannot be a standard I/O stream");
1134 return -1;
1138 * All fds inherited across exec() necessarily have FD_CLOEXEC
1139 * clear, while qemu sets FD_CLOEXEC on all other fds used internally.
1141 flags = fcntl(fd, F_GETFD);
1142 if (flags == -1 || (flags & FD_CLOEXEC)) {
1143 error_report("fd is not valid or already in use");
1144 return -1;
1147 if (fdset_id < 0) {
1148 error_report("set option is required and must be non-negative");
1149 return -1;
1152 #ifdef F_DUPFD_CLOEXEC
1153 dupfd = fcntl(fd, F_DUPFD_CLOEXEC, 0);
1154 #else
1155 dupfd = dup(fd);
1156 if (dupfd != -1) {
1157 qemu_set_cloexec(dupfd);
1159 #endif
1160 if (dupfd == -1) {
1161 error_report("error duplicating fd: %s", strerror(errno));
1162 return -1;
1165 /* add the duplicate fd, and optionally the opaque string, to the fd set */
1166 fdinfo = monitor_fdset_add_fd(dupfd, true, fdset_id, !!fd_opaque, fd_opaque,
1167 &error_abort);
1168 g_free(fdinfo);
1170 return 0;
1173 static int cleanup_add_fd(void *opaque, QemuOpts *opts, Error **errp)
1175 int fd;
1177 fd = qemu_opt_get_number(opts, "fd", -1);
1178 close(fd);
1180 return 0;
1182 #endif
1184 /***********************************************************/
1185 /* QEMU Block devices */
1187 #define HD_OPTS "media=disk"
1188 #define CDROM_OPTS "media=cdrom"
1189 #define FD_OPTS ""
1190 #define PFLASH_OPTS ""
1191 #define MTD_OPTS ""
1192 #define SD_OPTS ""
1194 static int drive_init_func(void *opaque, QemuOpts *opts, Error **errp)
1196 BlockInterfaceType *block_default_type = opaque;
1198 return drive_new(opts, *block_default_type) == NULL;
1201 static int drive_enable_snapshot(void *opaque, QemuOpts *opts, Error **errp)
1203 if (qemu_opt_get(opts, "snapshot") == NULL) {
1204 qemu_opt_set(opts, "snapshot", "on", &error_abort);
1206 return 0;
1209 static void default_drive(int enable, int snapshot, BlockInterfaceType type,
1210 int index, const char *optstr)
1212 QemuOpts *opts;
1213 DriveInfo *dinfo;
1215 if (!enable || drive_get_by_index(type, index)) {
1216 return;
1219 opts = drive_add(type, index, NULL, optstr);
1220 if (snapshot) {
1221 drive_enable_snapshot(NULL, opts, NULL);
1224 dinfo = drive_new(opts, type);
1225 if (!dinfo) {
1226 exit(1);
1228 dinfo->is_default = true;
1232 static QemuOptsList qemu_smp_opts = {
1233 .name = "smp-opts",
1234 .implied_opt_name = "cpus",
1235 .merge_lists = true,
1236 .head = QTAILQ_HEAD_INITIALIZER(qemu_smp_opts.head),
1237 .desc = {
1239 .name = "cpus",
1240 .type = QEMU_OPT_NUMBER,
1241 }, {
1242 .name = "sockets",
1243 .type = QEMU_OPT_NUMBER,
1244 }, {
1245 .name = "cores",
1246 .type = QEMU_OPT_NUMBER,
1247 }, {
1248 .name = "threads",
1249 .type = QEMU_OPT_NUMBER,
1250 }, {
1251 .name = "maxcpus",
1252 .type = QEMU_OPT_NUMBER,
1254 { /*End of list */ }
1258 static void smp_parse(QemuOpts *opts)
1260 if (opts) {
1261 unsigned cpus = qemu_opt_get_number(opts, "cpus", 0);
1262 unsigned sockets = qemu_opt_get_number(opts, "sockets", 0);
1263 unsigned cores = qemu_opt_get_number(opts, "cores", 0);
1264 unsigned threads = qemu_opt_get_number(opts, "threads", 0);
1266 /* compute missing values, prefer sockets over cores over threads */
1267 if (cpus == 0 || sockets == 0) {
1268 sockets = sockets > 0 ? sockets : 1;
1269 cores = cores > 0 ? cores : 1;
1270 threads = threads > 0 ? threads : 1;
1271 if (cpus == 0) {
1272 cpus = cores * threads * sockets;
1274 } else if (cores == 0) {
1275 threads = threads > 0 ? threads : 1;
1276 cores = cpus / (sockets * threads);
1277 cores = cores > 0 ? cores : 1;
1278 } else if (threads == 0) {
1279 threads = cpus / (cores * sockets);
1280 threads = threads > 0 ? threads : 1;
1281 } else if (sockets * cores * threads < cpus) {
1282 error_report("cpu topology: "
1283 "sockets (%u) * cores (%u) * threads (%u) < "
1284 "smp_cpus (%u)",
1285 sockets, cores, threads, cpus);
1286 exit(1);
1289 max_cpus = qemu_opt_get_number(opts, "maxcpus", cpus);
1291 if (max_cpus > MAX_CPUMASK_BITS) {
1292 error_report("unsupported number of maxcpus");
1293 exit(1);
1296 if (max_cpus < cpus) {
1297 error_report("maxcpus must be equal to or greater than smp");
1298 exit(1);
1301 if (sockets * cores * threads > max_cpus) {
1302 error_report("cpu topology: "
1303 "sockets (%u) * cores (%u) * threads (%u) > "
1304 "maxcpus (%u)",
1305 sockets, cores, threads, max_cpus);
1306 exit(1);
1309 smp_cpus = cpus;
1310 smp_cores = cores;
1311 smp_threads = threads;
1314 if (smp_cpus > 1) {
1315 Error *blocker = NULL;
1316 error_setg(&blocker, QERR_REPLAY_NOT_SUPPORTED, "smp");
1317 replay_add_blocker(blocker);
1321 static void realtime_init(void)
1323 if (enable_mlock) {
1324 if (os_mlock() < 0) {
1325 error_report("locking memory failed");
1326 exit(1);
1332 static void configure_msg(QemuOpts *opts)
1334 enable_timestamp_msg = qemu_opt_get_bool(opts, "timestamp", true);
1337 /***********************************************************/
1338 /* Semihosting */
1340 typedef struct SemihostingConfig {
1341 bool enabled;
1342 SemihostingTarget target;
1343 const char **argv;
1344 int argc;
1345 const char *cmdline; /* concatenated argv */
1346 } SemihostingConfig;
1348 static SemihostingConfig semihosting;
1350 bool semihosting_enabled(void)
1352 return semihosting.enabled;
1355 SemihostingTarget semihosting_get_target(void)
1357 return semihosting.target;
1360 const char *semihosting_get_arg(int i)
1362 if (i >= semihosting.argc) {
1363 return NULL;
1365 return semihosting.argv[i];
1368 int semihosting_get_argc(void)
1370 return semihosting.argc;
1373 const char *semihosting_get_cmdline(void)
1375 if (semihosting.cmdline == NULL && semihosting.argc > 0) {
1376 semihosting.cmdline = g_strjoinv(" ", (gchar **)semihosting.argv);
1378 return semihosting.cmdline;
1381 static int add_semihosting_arg(void *opaque,
1382 const char *name, const char *val,
1383 Error **errp)
1385 SemihostingConfig *s = opaque;
1386 if (strcmp(name, "arg") == 0) {
1387 s->argc++;
1388 /* one extra element as g_strjoinv() expects NULL-terminated array */
1389 s->argv = g_realloc(s->argv, (s->argc + 1) * sizeof(void *));
1390 s->argv[s->argc - 1] = val;
1391 s->argv[s->argc] = NULL;
1393 return 0;
1396 /* Use strings passed via -kernel/-append to initialize semihosting.argv[] */
1397 static inline void semihosting_arg_fallback(const char *file, const char *cmd)
1399 char *cmd_token;
1401 /* argv[0] */
1402 add_semihosting_arg(&semihosting, "arg", file, NULL);
1404 /* split -append and initialize argv[1..n] */
1405 cmd_token = strtok(g_strdup(cmd), " ");
1406 while (cmd_token) {
1407 add_semihosting_arg(&semihosting, "arg", cmd_token, NULL);
1408 cmd_token = strtok(NULL, " ");
1412 /* Now we still need this for compatibility with XEN. */
1413 bool has_igd_gfx_passthru;
1414 static void igd_gfx_passthru(void)
1416 has_igd_gfx_passthru = current_machine->igd_gfx_passthru;
1419 /***********************************************************/
1420 /* USB devices */
1422 static int usb_device_add(const char *devname)
1424 USBDevice *dev = NULL;
1425 #ifndef CONFIG_LINUX
1426 const char *p;
1427 #endif
1429 if (!machine_usb(current_machine)) {
1430 return -1;
1433 /* drivers with .usbdevice_name entry in USBDeviceInfo */
1434 dev = usbdevice_create(devname);
1435 if (dev)
1436 goto done;
1438 /* the other ones */
1439 #ifndef CONFIG_LINUX
1440 /* only the linux version is qdev-ified, usb-bsd still needs this */
1441 if (strstart(devname, "host:", &p)) {
1442 dev = usb_host_device_open(usb_bus_find(-1), p);
1444 #endif
1445 if (!dev)
1446 return -1;
1448 done:
1449 return 0;
1452 static int usb_device_del(const char *devname)
1454 int bus_num, addr;
1455 const char *p;
1457 if (strstart(devname, "host:", &p)) {
1458 return -1;
1461 if (!machine_usb(current_machine)) {
1462 return -1;
1465 p = strchr(devname, '.');
1466 if (!p)
1467 return -1;
1468 bus_num = strtoul(devname, NULL, 0);
1469 addr = strtoul(p + 1, NULL, 0);
1471 return usb_device_delete_addr(bus_num, addr);
1474 static int usb_parse(const char *cmdline)
1476 int r;
1477 r = usb_device_add(cmdline);
1478 if (r < 0) {
1479 error_report("could not add USB device '%s'", cmdline);
1481 return r;
1484 void hmp_usb_add(Monitor *mon, const QDict *qdict)
1486 const char *devname = qdict_get_str(qdict, "devname");
1487 if (usb_device_add(devname) < 0) {
1488 error_report("could not add USB device '%s'", devname);
1492 void hmp_usb_del(Monitor *mon, const QDict *qdict)
1494 const char *devname = qdict_get_str(qdict, "devname");
1495 if (usb_device_del(devname) < 0) {
1496 error_report("could not delete USB device '%s'", devname);
1500 /***********************************************************/
1501 /* machine registration */
1503 MachineState *current_machine;
1505 static MachineClass *find_machine(const char *name)
1507 GSList *el, *machines = object_class_get_list(TYPE_MACHINE, false);
1508 MachineClass *mc = NULL;
1510 for (el = machines; el; el = el->next) {
1511 MachineClass *temp = el->data;
1513 if (!strcmp(temp->name, name)) {
1514 mc = temp;
1515 break;
1517 if (temp->alias &&
1518 !strcmp(temp->alias, name)) {
1519 mc = temp;
1520 break;
1524 g_slist_free(machines);
1525 return mc;
1528 MachineClass *find_default_machine(void)
1530 GSList *el, *machines = object_class_get_list(TYPE_MACHINE, false);
1531 MachineClass *mc = NULL;
1533 for (el = machines; el; el = el->next) {
1534 MachineClass *temp = el->data;
1536 if (temp->is_default) {
1537 mc = temp;
1538 break;
1542 g_slist_free(machines);
1543 return mc;
1546 MachineInfoList *qmp_query_machines(Error **errp)
1548 GSList *el, *machines = object_class_get_list(TYPE_MACHINE, false);
1549 MachineInfoList *mach_list = NULL;
1551 for (el = machines; el; el = el->next) {
1552 MachineClass *mc = el->data;
1553 MachineInfoList *entry;
1554 MachineInfo *info;
1556 info = g_malloc0(sizeof(*info));
1557 if (mc->is_default) {
1558 info->has_is_default = true;
1559 info->is_default = true;
1562 if (mc->alias) {
1563 info->has_alias = true;
1564 info->alias = g_strdup(mc->alias);
1567 info->name = g_strdup(mc->name);
1568 info->cpu_max = !mc->max_cpus ? 1 : mc->max_cpus;
1569 info->hotpluggable_cpus = !!mc->query_hotpluggable_cpus;
1571 entry = g_malloc0(sizeof(*entry));
1572 entry->value = info;
1573 entry->next = mach_list;
1574 mach_list = entry;
1577 g_slist_free(machines);
1578 return mach_list;
1581 static int machine_help_func(QemuOpts *opts, MachineState *machine)
1583 ObjectProperty *prop;
1584 ObjectPropertyIterator iter;
1586 if (!qemu_opt_has_help_opt(opts)) {
1587 return 0;
1590 object_property_iter_init(&iter, OBJECT(machine));
1591 while ((prop = object_property_iter_next(&iter))) {
1592 if (!prop->set) {
1593 continue;
1596 error_printf("%s.%s=%s", MACHINE_GET_CLASS(machine)->name,
1597 prop->name, prop->type);
1598 if (prop->description) {
1599 error_printf(" (%s)\n", prop->description);
1600 } else {
1601 error_printf("\n");
1605 return 1;
1608 /***********************************************************/
1609 /* main execution loop */
1611 struct vm_change_state_entry {
1612 VMChangeStateHandler *cb;
1613 void *opaque;
1614 QLIST_ENTRY (vm_change_state_entry) entries;
1617 static QLIST_HEAD(vm_change_state_head, vm_change_state_entry) vm_change_state_head;
1619 VMChangeStateEntry *qemu_add_vm_change_state_handler(VMChangeStateHandler *cb,
1620 void *opaque)
1622 VMChangeStateEntry *e;
1624 e = g_malloc0(sizeof (*e));
1626 e->cb = cb;
1627 e->opaque = opaque;
1628 QLIST_INSERT_HEAD(&vm_change_state_head, e, entries);
1629 return e;
1632 void qemu_del_vm_change_state_handler(VMChangeStateEntry *e)
1634 QLIST_REMOVE (e, entries);
1635 g_free (e);
1638 void vm_state_notify(int running, RunState state)
1640 VMChangeStateEntry *e, *next;
1642 trace_vm_state_notify(running, state);
1644 QLIST_FOREACH_SAFE(e, &vm_change_state_head, entries, next) {
1645 e->cb(e->opaque, running, state);
1649 /* reset/shutdown handler */
1651 typedef struct QEMUResetEntry {
1652 QTAILQ_ENTRY(QEMUResetEntry) entry;
1653 QEMUResetHandler *func;
1654 void *opaque;
1655 } QEMUResetEntry;
1657 static QTAILQ_HEAD(reset_handlers, QEMUResetEntry) reset_handlers =
1658 QTAILQ_HEAD_INITIALIZER(reset_handlers);
1659 static int reset_requested;
1660 static int shutdown_requested, shutdown_signal = -1;
1661 static pid_t shutdown_pid;
1662 static int powerdown_requested;
1663 static int debug_requested;
1664 static int suspend_requested;
1665 static WakeupReason wakeup_reason;
1666 static NotifierList powerdown_notifiers =
1667 NOTIFIER_LIST_INITIALIZER(powerdown_notifiers);
1668 static NotifierList suspend_notifiers =
1669 NOTIFIER_LIST_INITIALIZER(suspend_notifiers);
1670 static NotifierList wakeup_notifiers =
1671 NOTIFIER_LIST_INITIALIZER(wakeup_notifiers);
1672 static uint32_t wakeup_reason_mask = ~(1 << QEMU_WAKEUP_REASON_NONE);
1674 int qemu_shutdown_requested_get(void)
1676 return shutdown_requested;
1679 int qemu_reset_requested_get(void)
1681 return reset_requested;
1684 static int qemu_shutdown_requested(void)
1686 return atomic_xchg(&shutdown_requested, 0);
1689 static void qemu_kill_report(void)
1691 if (!qtest_driver() && shutdown_signal != -1) {
1692 if (shutdown_pid == 0) {
1693 /* This happens for eg ^C at the terminal, so it's worth
1694 * avoiding printing an odd message in that case.
1696 error_report("terminating on signal %d", shutdown_signal);
1697 } else {
1698 char *shutdown_cmd = qemu_get_pid_name(shutdown_pid);
1700 error_report("terminating on signal %d from pid " FMT_pid " (%s)",
1701 shutdown_signal, shutdown_pid,
1702 shutdown_cmd ? shutdown_cmd : "<unknown process>");
1703 g_free(shutdown_cmd);
1705 shutdown_signal = -1;
1709 static int qemu_reset_requested(void)
1711 int r = reset_requested;
1712 if (r && replay_checkpoint(CHECKPOINT_RESET_REQUESTED)) {
1713 reset_requested = 0;
1714 return r;
1716 return false;
1719 static int qemu_suspend_requested(void)
1721 int r = suspend_requested;
1722 if (r && replay_checkpoint(CHECKPOINT_SUSPEND_REQUESTED)) {
1723 suspend_requested = 0;
1724 return r;
1726 return false;
1729 static WakeupReason qemu_wakeup_requested(void)
1731 return wakeup_reason;
1734 static int qemu_powerdown_requested(void)
1736 int r = powerdown_requested;
1737 powerdown_requested = 0;
1738 return r;
1741 static int qemu_debug_requested(void)
1743 int r = debug_requested;
1744 debug_requested = 0;
1745 return r;
1748 void qemu_register_reset(QEMUResetHandler *func, void *opaque)
1750 QEMUResetEntry *re = g_malloc0(sizeof(QEMUResetEntry));
1752 re->func = func;
1753 re->opaque = opaque;
1754 QTAILQ_INSERT_TAIL(&reset_handlers, re, entry);
1757 void qemu_unregister_reset(QEMUResetHandler *func, void *opaque)
1759 QEMUResetEntry *re;
1761 QTAILQ_FOREACH(re, &reset_handlers, entry) {
1762 if (re->func == func && re->opaque == opaque) {
1763 QTAILQ_REMOVE(&reset_handlers, re, entry);
1764 g_free(re);
1765 return;
1770 void qemu_devices_reset(void)
1772 QEMUResetEntry *re, *nre;
1774 /* reset all devices */
1775 QTAILQ_FOREACH_SAFE(re, &reset_handlers, entry, nre) {
1776 re->func(re->opaque);
1780 void qemu_system_reset(bool report)
1782 MachineClass *mc;
1784 mc = current_machine ? MACHINE_GET_CLASS(current_machine) : NULL;
1786 cpu_synchronize_all_states();
1788 if (mc && mc->reset) {
1789 mc->reset();
1790 } else {
1791 qemu_devices_reset();
1793 if (report) {
1794 qapi_event_send_reset(&error_abort);
1796 cpu_synchronize_all_post_reset();
1799 void qemu_system_guest_panicked(void)
1801 if (current_cpu) {
1802 current_cpu->crash_occurred = true;
1804 qapi_event_send_guest_panicked(GUEST_PANIC_ACTION_PAUSE, &error_abort);
1805 vm_stop(RUN_STATE_GUEST_PANICKED);
1806 if (!no_shutdown) {
1807 qapi_event_send_guest_panicked(GUEST_PANIC_ACTION_POWEROFF,
1808 &error_abort);
1809 qemu_system_shutdown_request();
1813 void qemu_system_reset_request(void)
1815 if (no_reboot) {
1816 shutdown_requested = 1;
1817 } else {
1818 reset_requested = 1;
1820 cpu_stop_current();
1821 qemu_notify_event();
1824 static void qemu_system_suspend(void)
1826 pause_all_vcpus();
1827 notifier_list_notify(&suspend_notifiers, NULL);
1828 runstate_set(RUN_STATE_SUSPENDED);
1829 qapi_event_send_suspend(&error_abort);
1832 void qemu_system_suspend_request(void)
1834 if (runstate_check(RUN_STATE_SUSPENDED)) {
1835 return;
1837 suspend_requested = 1;
1838 cpu_stop_current();
1839 qemu_notify_event();
1842 void qemu_register_suspend_notifier(Notifier *notifier)
1844 notifier_list_add(&suspend_notifiers, notifier);
1847 void qemu_system_wakeup_request(WakeupReason reason)
1849 trace_system_wakeup_request(reason);
1851 if (!runstate_check(RUN_STATE_SUSPENDED)) {
1852 return;
1854 if (!(wakeup_reason_mask & (1 << reason))) {
1855 return;
1857 runstate_set(RUN_STATE_RUNNING);
1858 wakeup_reason = reason;
1859 qemu_notify_event();
1862 void qemu_system_wakeup_enable(WakeupReason reason, bool enabled)
1864 if (enabled) {
1865 wakeup_reason_mask |= (1 << reason);
1866 } else {
1867 wakeup_reason_mask &= ~(1 << reason);
1871 void qemu_register_wakeup_notifier(Notifier *notifier)
1873 notifier_list_add(&wakeup_notifiers, notifier);
1876 void qemu_system_killed(int signal, pid_t pid)
1878 shutdown_signal = signal;
1879 shutdown_pid = pid;
1880 no_shutdown = 0;
1882 /* Cannot call qemu_system_shutdown_request directly because
1883 * we are in a signal handler.
1885 shutdown_requested = 1;
1886 qemu_notify_event();
1889 void qemu_system_shutdown_request(void)
1891 trace_qemu_system_shutdown_request();
1892 replay_shutdown_request();
1893 shutdown_requested = 1;
1894 qemu_notify_event();
1897 static void qemu_system_powerdown(void)
1899 qapi_event_send_powerdown(&error_abort);
1900 notifier_list_notify(&powerdown_notifiers, NULL);
1903 void qemu_system_powerdown_request(void)
1905 trace_qemu_system_powerdown_request();
1906 powerdown_requested = 1;
1907 qemu_notify_event();
1910 void qemu_register_powerdown_notifier(Notifier *notifier)
1912 notifier_list_add(&powerdown_notifiers, notifier);
1915 void qemu_system_debug_request(void)
1917 debug_requested = 1;
1918 qemu_notify_event();
1921 static bool main_loop_should_exit(void)
1923 RunState r;
1924 if (qemu_debug_requested()) {
1925 vm_stop(RUN_STATE_DEBUG);
1927 if (qemu_suspend_requested()) {
1928 qemu_system_suspend();
1930 if (qemu_shutdown_requested()) {
1931 qemu_kill_report();
1932 qapi_event_send_shutdown(&error_abort);
1933 if (no_shutdown) {
1934 vm_stop(RUN_STATE_SHUTDOWN);
1935 } else {
1936 return true;
1939 if (qemu_reset_requested()) {
1940 pause_all_vcpus();
1941 qemu_system_reset(VMRESET_REPORT);
1942 resume_all_vcpus();
1943 if (!runstate_check(RUN_STATE_RUNNING) &&
1944 !runstate_check(RUN_STATE_INMIGRATE)) {
1945 runstate_set(RUN_STATE_PRELAUNCH);
1948 if (qemu_wakeup_requested()) {
1949 pause_all_vcpus();
1950 qemu_system_reset(VMRESET_SILENT);
1951 notifier_list_notify(&wakeup_notifiers, &wakeup_reason);
1952 wakeup_reason = QEMU_WAKEUP_REASON_NONE;
1953 resume_all_vcpus();
1954 qapi_event_send_wakeup(&error_abort);
1956 if (qemu_powerdown_requested()) {
1957 qemu_system_powerdown();
1959 if (qemu_vmstop_requested(&r)) {
1960 vm_stop(r);
1962 return false;
1965 static void main_loop(void)
1967 bool nonblocking;
1968 int last_io = 0;
1969 #ifdef CONFIG_PROFILER
1970 int64_t ti;
1971 #endif
1972 do {
1973 nonblocking = tcg_enabled() && last_io > 0;
1974 #ifdef CONFIG_PROFILER
1975 ti = profile_getclock();
1976 #endif
1977 last_io = main_loop_wait(nonblocking);
1978 #ifdef CONFIG_PROFILER
1979 dev_time += profile_getclock() - ti;
1980 #endif
1981 } while (!main_loop_should_exit());
1984 static void version(void)
1986 printf("QEMU emulator version " QEMU_VERSION QEMU_PKGVERSION "\n"
1987 QEMU_COPYRIGHT "\n");
1990 static void QEMU_NORETURN help(int exitcode)
1992 version();
1993 printf("usage: %s [options] [disk_image]\n\n"
1994 "'disk_image' is a raw hard disk image for IDE hard disk 0\n\n",
1995 error_get_progname());
1997 #define QEMU_OPTIONS_GENERATE_HELP
1998 #include "qemu-options-wrapper.h"
2000 printf("\nDuring emulation, the following keys are useful:\n"
2001 "ctrl-alt-f toggle full screen\n"
2002 "ctrl-alt-n switch to virtual console 'n'\n"
2003 "ctrl-alt toggle mouse and keyboard grab\n"
2004 "\n"
2005 "When using -nographic, press 'ctrl-a h' to get some help.\n");
2007 exit(exitcode);
2010 #define HAS_ARG 0x0001
2012 typedef struct QEMUOption {
2013 const char *name;
2014 int flags;
2015 int index;
2016 uint32_t arch_mask;
2017 } QEMUOption;
2019 static const QEMUOption qemu_options[] = {
2020 { "h", 0, QEMU_OPTION_h, QEMU_ARCH_ALL },
2021 #define QEMU_OPTIONS_GENERATE_OPTIONS
2022 #include "qemu-options-wrapper.h"
2023 { NULL },
2026 typedef struct VGAInterfaceInfo {
2027 const char *opt_name; /* option name */
2028 const char *name; /* human-readable name */
2029 /* Class names indicating that support is available.
2030 * If no class is specified, the interface is always available */
2031 const char *class_names[2];
2032 } VGAInterfaceInfo;
2034 static VGAInterfaceInfo vga_interfaces[VGA_TYPE_MAX] = {
2035 [VGA_NONE] = {
2036 .opt_name = "none",
2038 [VGA_STD] = {
2039 .opt_name = "std",
2040 .name = "standard VGA",
2041 .class_names = { "VGA", "isa-vga" },
2043 [VGA_CIRRUS] = {
2044 .opt_name = "cirrus",
2045 .name = "Cirrus VGA",
2046 .class_names = { "cirrus-vga", "isa-cirrus-vga" },
2048 [VGA_VMWARE] = {
2049 .opt_name = "vmware",
2050 .name = "VMWare SVGA",
2051 .class_names = { "vmware-svga" },
2053 [VGA_VIRTIO] = {
2054 .opt_name = "virtio",
2055 .name = "Virtio VGA",
2056 .class_names = { "virtio-vga" },
2058 [VGA_QXL] = {
2059 .opt_name = "qxl",
2060 .name = "QXL VGA",
2061 .class_names = { "qxl-vga" },
2063 [VGA_TCX] = {
2064 .opt_name = "tcx",
2065 .name = "TCX framebuffer",
2066 .class_names = { "SUNW,tcx" },
2068 [VGA_CG3] = {
2069 .opt_name = "cg3",
2070 .name = "CG3 framebuffer",
2071 .class_names = { "cgthree" },
2073 [VGA_XENFB] = {
2074 .opt_name = "xenfb",
2078 static bool vga_interface_available(VGAInterfaceType t)
2080 VGAInterfaceInfo *ti = &vga_interfaces[t];
2082 assert(t < VGA_TYPE_MAX);
2083 return !ti->class_names[0] ||
2084 object_class_by_name(ti->class_names[0]) ||
2085 object_class_by_name(ti->class_names[1]);
2088 static void select_vgahw(const char *p)
2090 const char *opts;
2091 int t;
2093 assert(vga_interface_type == VGA_NONE);
2094 for (t = 0; t < VGA_TYPE_MAX; t++) {
2095 VGAInterfaceInfo *ti = &vga_interfaces[t];
2096 if (ti->opt_name && strstart(p, ti->opt_name, &opts)) {
2097 if (!vga_interface_available(t)) {
2098 error_report("%s not available", ti->name);
2099 exit(1);
2101 vga_interface_type = t;
2102 break;
2105 if (t == VGA_TYPE_MAX) {
2106 invalid_vga:
2107 error_report("unknown vga type: %s", p);
2108 exit(1);
2110 while (*opts) {
2111 const char *nextopt;
2113 if (strstart(opts, ",retrace=", &nextopt)) {
2114 opts = nextopt;
2115 if (strstart(opts, "dumb", &nextopt))
2116 vga_retrace_method = VGA_RETRACE_DUMB;
2117 else if (strstart(opts, "precise", &nextopt))
2118 vga_retrace_method = VGA_RETRACE_PRECISE;
2119 else goto invalid_vga;
2120 } else goto invalid_vga;
2121 opts = nextopt;
2125 typedef enum DisplayType {
2126 DT_DEFAULT,
2127 DT_CURSES,
2128 DT_SDL,
2129 DT_COCOA,
2130 DT_GTK,
2131 DT_NONE,
2132 } DisplayType;
2134 static DisplayType select_display(const char *p)
2136 const char *opts;
2137 DisplayType display = DT_DEFAULT;
2139 if (strstart(p, "sdl", &opts)) {
2140 #ifdef CONFIG_SDL
2141 display = DT_SDL;
2142 while (*opts) {
2143 const char *nextopt;
2145 if (strstart(opts, ",frame=", &nextopt)) {
2146 opts = nextopt;
2147 if (strstart(opts, "on", &nextopt)) {
2148 no_frame = 0;
2149 } else if (strstart(opts, "off", &nextopt)) {
2150 no_frame = 1;
2151 } else {
2152 goto invalid_sdl_args;
2154 } else if (strstart(opts, ",alt_grab=", &nextopt)) {
2155 opts = nextopt;
2156 if (strstart(opts, "on", &nextopt)) {
2157 alt_grab = 1;
2158 } else if (strstart(opts, "off", &nextopt)) {
2159 alt_grab = 0;
2160 } else {
2161 goto invalid_sdl_args;
2163 } else if (strstart(opts, ",ctrl_grab=", &nextopt)) {
2164 opts = nextopt;
2165 if (strstart(opts, "on", &nextopt)) {
2166 ctrl_grab = 1;
2167 } else if (strstart(opts, "off", &nextopt)) {
2168 ctrl_grab = 0;
2169 } else {
2170 goto invalid_sdl_args;
2172 } else if (strstart(opts, ",window_close=", &nextopt)) {
2173 opts = nextopt;
2174 if (strstart(opts, "on", &nextopt)) {
2175 no_quit = 0;
2176 } else if (strstart(opts, "off", &nextopt)) {
2177 no_quit = 1;
2178 } else {
2179 goto invalid_sdl_args;
2181 } else if (strstart(opts, ",gl=", &nextopt)) {
2182 opts = nextopt;
2183 if (strstart(opts, "on", &nextopt)) {
2184 request_opengl = 1;
2185 } else if (strstart(opts, "off", &nextopt)) {
2186 request_opengl = 0;
2187 } else {
2188 goto invalid_sdl_args;
2190 } else {
2191 invalid_sdl_args:
2192 error_report("invalid SDL option string");
2193 exit(1);
2195 opts = nextopt;
2197 #else
2198 error_report("SDL support is disabled");
2199 exit(1);
2200 #endif
2201 } else if (strstart(p, "vnc", &opts)) {
2202 if (*opts == '=') {
2203 vnc_parse(opts + 1, &error_fatal);
2204 } else {
2205 error_report("VNC requires a display argument vnc=<display>");
2206 exit(1);
2208 } else if (strstart(p, "curses", &opts)) {
2209 #ifdef CONFIG_CURSES
2210 display = DT_CURSES;
2211 #else
2212 error_report("curses support is disabled");
2213 exit(1);
2214 #endif
2215 } else if (strstart(p, "gtk", &opts)) {
2216 #ifdef CONFIG_GTK
2217 display = DT_GTK;
2218 while (*opts) {
2219 const char *nextopt;
2221 if (strstart(opts, ",grab_on_hover=", &nextopt)) {
2222 opts = nextopt;
2223 if (strstart(opts, "on", &nextopt)) {
2224 grab_on_hover = true;
2225 } else if (strstart(opts, "off", &nextopt)) {
2226 grab_on_hover = false;
2227 } else {
2228 goto invalid_gtk_args;
2230 } else if (strstart(opts, ",gl=", &nextopt)) {
2231 opts = nextopt;
2232 if (strstart(opts, "on", &nextopt)) {
2233 request_opengl = 1;
2234 } else if (strstart(opts, "off", &nextopt)) {
2235 request_opengl = 0;
2236 } else {
2237 goto invalid_gtk_args;
2239 } else {
2240 invalid_gtk_args:
2241 error_report("invalid GTK option string");
2242 exit(1);
2244 opts = nextopt;
2246 #else
2247 error_report("GTK support is disabled");
2248 exit(1);
2249 #endif
2250 } else if (strstart(p, "none", &opts)) {
2251 display = DT_NONE;
2252 } else {
2253 error_report("unknown display type");
2254 exit(1);
2257 return display;
2260 static int balloon_parse(const char *arg)
2262 QemuOpts *opts;
2264 if (strcmp(arg, "none") == 0) {
2265 return 0;
2268 if (!strncmp(arg, "virtio", 6)) {
2269 if (arg[6] == ',') {
2270 /* have params -> parse them */
2271 opts = qemu_opts_parse_noisily(qemu_find_opts("device"), arg + 7,
2272 false);
2273 if (!opts)
2274 return -1;
2275 } else {
2276 /* create empty opts */
2277 opts = qemu_opts_create(qemu_find_opts("device"), NULL, 0,
2278 &error_abort);
2280 qemu_opt_set(opts, "driver", "virtio-balloon", &error_abort);
2281 return 0;
2284 return -1;
2287 #if defined(CONFIG_RUBY)
2288 /* Ruby interface for QEMU. See README.EXT for programming example. */
2289 #warning mit ruby
2290 #include <ruby.h>
2292 static VALUE qemu_open(int argc, VALUE *argv, VALUE klass)
2294 int i;
2295 for (i = 0; i < argc; i++) {
2296 argv[i] = StringValue(argv[i]);
2297 fprintf(stderr, "argv[%d] = %s\n", i, StringValuePtr(argv[i]));
2299 //~ if (rb_scan_args(argc, argv, "11", &file, &vmode) == 1) {
2300 //~ mode = 0666; /* default value */
2301 //~ }
2302 return INT2FIX(argc);
2305 void Init_qemu(void)
2307 printf("%s\n", __func__);
2308 VALUE cQEMU = rb_define_class("QEMU", rb_cObject);
2309 rb_define_singleton_method(cQEMU, "run", qemu_open, -1);
2310 #if 0
2311 rb_define_method();
2312 #endif
2314 #endif /* CONFIG_RUBY */
2316 #if defined(CONFIG_DLL)
2317 BOOL APIENTRY DllMain(HINSTANCE hModule, DWORD ul_reason_for_call, LPVOID data)
2319 return FALSE;
2321 #endif /* CONFIG_DLL */
2323 char *qemu_find_file(int type, const char *name)
2325 int i;
2326 const char *subdir;
2327 char *buf;
2329 /* Try the name as a straight path first */
2330 if (access(name, R_OK) == 0) {
2331 trace_load_file(name, name);
2332 return g_strdup(name);
2335 switch (type) {
2336 case QEMU_FILE_TYPE_BIOS:
2337 subdir = "";
2338 break;
2339 case QEMU_FILE_TYPE_KEYMAP:
2340 subdir = "keymaps/";
2341 break;
2342 default:
2343 abort();
2346 for (i = 0; i < data_dir_idx; i++) {
2347 buf = g_strdup_printf("%s/%s%s", data_dir[i], subdir, name);
2348 if (access(buf, R_OK) == 0) {
2349 trace_load_file(name, buf);
2350 return buf;
2352 g_free(buf);
2354 return NULL;
2357 static inline bool nonempty_str(const char *str)
2359 return str && *str;
2362 static int parse_fw_cfg(void *opaque, QemuOpts *opts, Error **errp)
2364 gchar *buf;
2365 size_t size;
2366 const char *name, *file, *str;
2367 FWCfgState *fw_cfg = (FWCfgState *) opaque;
2369 if (fw_cfg == NULL) {
2370 error_report("fw_cfg device not available");
2371 return -1;
2373 name = qemu_opt_get(opts, "name");
2374 file = qemu_opt_get(opts, "file");
2375 str = qemu_opt_get(opts, "string");
2377 /* we need name and either a file or the content string */
2378 if (!(nonempty_str(name) && (nonempty_str(file) || nonempty_str(str)))) {
2379 error_report("invalid argument(s)");
2380 return -1;
2382 if (nonempty_str(file) && nonempty_str(str)) {
2383 error_report("file and string are mutually exclusive");
2384 return -1;
2386 if (strlen(name) > FW_CFG_MAX_FILE_PATH - 1) {
2387 error_report("name too long (max. %d char)", FW_CFG_MAX_FILE_PATH - 1);
2388 return -1;
2390 if (strncmp(name, "opt/", 4) != 0) {
2391 error_report("warning: externally provided fw_cfg item names "
2392 "should be prefixed with \"opt/\"");
2394 if (nonempty_str(str)) {
2395 size = strlen(str); /* NUL terminator NOT included in fw_cfg blob */
2396 buf = g_memdup(str, size);
2397 } else {
2398 if (!g_file_get_contents(file, &buf, &size, NULL)) {
2399 error_report("can't load %s", file);
2400 return -1;
2403 /* For legacy, keep user files in a specific global order. */
2404 fw_cfg_set_order_override(fw_cfg, FW_CFG_ORDER_OVERRIDE_USER);
2405 fw_cfg_add_file(fw_cfg, name, buf, size);
2406 fw_cfg_reset_order_override(fw_cfg);
2407 return 0;
2410 static int device_help_func(void *opaque, QemuOpts *opts, Error **errp)
2412 return qdev_device_help(opts);
2415 static int device_init_func(void *opaque, QemuOpts *opts, Error **errp)
2417 Error *err = NULL;
2418 DeviceState *dev;
2420 dev = qdev_device_add(opts, &err);
2421 if (!dev) {
2422 error_report_err(err);
2423 return -1;
2425 object_unref(OBJECT(dev));
2426 return 0;
2429 static int chardev_init_func(void *opaque, QemuOpts *opts, Error **errp)
2431 Error *local_err = NULL;
2433 qemu_chr_new_from_opts(opts, &local_err);
2434 if (local_err) {
2435 error_report_err(local_err);
2436 return -1;
2438 return 0;
2441 #ifdef CONFIG_VIRTFS
2442 static int fsdev_init_func(void *opaque, QemuOpts *opts, Error **errp)
2444 return qemu_fsdev_add(opts);
2446 #endif
2448 static int mon_init_func(void *opaque, QemuOpts *opts, Error **errp)
2450 CharDriverState *chr;
2451 const char *chardev;
2452 const char *mode;
2453 int flags;
2455 mode = qemu_opt_get(opts, "mode");
2456 if (mode == NULL) {
2457 mode = "readline";
2459 if (strcmp(mode, "readline") == 0) {
2460 flags = MONITOR_USE_READLINE;
2461 } else if (strcmp(mode, "control") == 0) {
2462 flags = MONITOR_USE_CONTROL;
2463 } else {
2464 error_report("unknown monitor mode \"%s\"", mode);
2465 exit(1);
2468 if (qemu_opt_get_bool(opts, "pretty", 0))
2469 flags |= MONITOR_USE_PRETTY;
2471 if (qemu_opt_get_bool(opts, "default", 0)) {
2472 error_report("option 'default' does nothing and is deprecated");
2475 chardev = qemu_opt_get(opts, "chardev");
2476 chr = qemu_chr_find(chardev);
2477 if (chr == NULL) {
2478 error_report("chardev \"%s\" not found", chardev);
2479 exit(1);
2482 monitor_init(chr, flags);
2483 return 0;
2486 static void monitor_parse(const char *optarg, const char *mode, bool pretty)
2488 static int monitor_device_index = 0;
2489 QemuOpts *opts;
2490 const char *p;
2491 char label[32];
2493 if (strstart(optarg, "chardev:", &p)) {
2494 snprintf(label, sizeof(label), "%s", p);
2495 } else {
2496 snprintf(label, sizeof(label), "compat_monitor%d",
2497 monitor_device_index);
2498 opts = qemu_chr_parse_compat(label, optarg);
2499 if (!opts) {
2500 error_report("parse error: %s", optarg);
2501 exit(1);
2505 opts = qemu_opts_create(qemu_find_opts("mon"), label, 1, &error_fatal);
2506 qemu_opt_set(opts, "mode", mode, &error_abort);
2507 qemu_opt_set(opts, "chardev", label, &error_abort);
2508 qemu_opt_set_bool(opts, "pretty", pretty, &error_abort);
2509 monitor_device_index++;
2512 struct device_config {
2513 enum {
2514 DEV_USB, /* -usbdevice */
2515 DEV_BT, /* -bt */
2516 DEV_SERIAL, /* -serial */
2517 DEV_PARALLEL, /* -parallel */
2518 DEV_VIRTCON, /* -virtioconsole */
2519 DEV_DEBUGCON, /* -debugcon */
2520 DEV_GDB, /* -gdb, -s */
2521 DEV_SCLP, /* s390 sclp */
2522 } type;
2523 const char *cmdline;
2524 Location loc;
2525 QTAILQ_ENTRY(device_config) next;
2528 static QTAILQ_HEAD(, device_config) device_configs =
2529 QTAILQ_HEAD_INITIALIZER(device_configs);
2531 static void add_device_config(int type, const char *cmdline)
2533 struct device_config *conf;
2535 conf = g_malloc0(sizeof(*conf));
2536 conf->type = type;
2537 conf->cmdline = cmdline;
2538 loc_save(&conf->loc);
2539 QTAILQ_INSERT_TAIL(&device_configs, conf, next);
2542 static int foreach_device_config(int type, int (*func)(const char *cmdline))
2544 struct device_config *conf;
2545 int rc;
2547 QTAILQ_FOREACH(conf, &device_configs, next) {
2548 if (conf->type != type)
2549 continue;
2550 loc_push_restore(&conf->loc);
2551 rc = func(conf->cmdline);
2552 loc_pop(&conf->loc);
2553 if (rc) {
2554 return rc;
2557 return 0;
2560 static int serial_parse(const char *devname)
2562 static int index = 0;
2563 char label[32];
2565 if (strcmp(devname, "none") == 0)
2566 return 0;
2567 if (index == MAX_SERIAL_PORTS) {
2568 error_report("too many serial ports");
2569 exit(1);
2571 snprintf(label, sizeof(label), "serial%d", index);
2572 serial_hds[index] = qemu_chr_new(label, devname);
2573 if (!serial_hds[index]) {
2574 error_report("could not connect serial device"
2575 " to character backend '%s'", devname);
2576 return -1;
2578 index++;
2579 return 0;
2582 static int parallel_parse(const char *devname)
2584 static int index = 0;
2585 char label[32];
2587 if (strcmp(devname, "none") == 0)
2588 return 0;
2589 if (index == MAX_PARALLEL_PORTS) {
2590 error_report("too many parallel ports");
2591 exit(1);
2593 snprintf(label, sizeof(label), "parallel%d", index);
2594 parallel_hds[index] = qemu_chr_new(label, devname);
2595 if (!parallel_hds[index]) {
2596 error_report("could not connect parallel device"
2597 " to character backend '%s'", devname);
2598 return -1;
2600 index++;
2601 return 0;
2604 static int virtcon_parse(const char *devname)
2606 QemuOptsList *device = qemu_find_opts("device");
2607 static int index = 0;
2608 char label[32];
2609 QemuOpts *bus_opts, *dev_opts;
2611 if (strcmp(devname, "none") == 0)
2612 return 0;
2613 if (index == MAX_VIRTIO_CONSOLES) {
2614 error_report("too many virtio consoles");
2615 exit(1);
2618 bus_opts = qemu_opts_create(device, NULL, 0, &error_abort);
2619 qemu_opt_set(bus_opts, "driver", "virtio-serial", &error_abort);
2621 dev_opts = qemu_opts_create(device, NULL, 0, &error_abort);
2622 qemu_opt_set(dev_opts, "driver", "virtconsole", &error_abort);
2624 snprintf(label, sizeof(label), "virtcon%d", index);
2625 virtcon_hds[index] = qemu_chr_new(label, devname);
2626 if (!virtcon_hds[index]) {
2627 error_report("could not connect virtio console"
2628 " to character backend '%s'", devname);
2629 return -1;
2631 qemu_opt_set(dev_opts, "chardev", label, &error_abort);
2633 index++;
2634 return 0;
2637 static int sclp_parse(const char *devname)
2639 QemuOptsList *device = qemu_find_opts("device");
2640 static int index = 0;
2641 char label[32];
2642 QemuOpts *dev_opts;
2644 if (strcmp(devname, "none") == 0) {
2645 return 0;
2647 if (index == MAX_SCLP_CONSOLES) {
2648 error_report("too many sclp consoles");
2649 exit(1);
2652 assert(arch_type == QEMU_ARCH_S390X);
2654 dev_opts = qemu_opts_create(device, NULL, 0, NULL);
2655 qemu_opt_set(dev_opts, "driver", "sclpconsole", &error_abort);
2657 snprintf(label, sizeof(label), "sclpcon%d", index);
2658 sclp_hds[index] = qemu_chr_new(label, devname);
2659 if (!sclp_hds[index]) {
2660 error_report("could not connect sclp console"
2661 " to character backend '%s'", devname);
2662 return -1;
2664 qemu_opt_set(dev_opts, "chardev", label, &error_abort);
2666 index++;
2667 return 0;
2670 static int debugcon_parse(const char *devname)
2672 QemuOpts *opts;
2674 if (!qemu_chr_new("debugcon", devname)) {
2675 exit(1);
2677 opts = qemu_opts_create(qemu_find_opts("device"), "debugcon", 1, NULL);
2678 if (!opts) {
2679 error_report("already have a debugcon device");
2680 exit(1);
2682 qemu_opt_set(opts, "driver", "isa-debugcon", &error_abort);
2683 qemu_opt_set(opts, "chardev", "debugcon", &error_abort);
2684 return 0;
2687 static gint machine_class_cmp(gconstpointer a, gconstpointer b)
2689 const MachineClass *mc1 = a, *mc2 = b;
2690 int res;
2692 if (mc1->family == NULL) {
2693 if (mc2->family == NULL) {
2694 /* Compare standalone machine types against each other; they sort
2695 * in increasing order.
2697 return strcmp(object_class_get_name(OBJECT_CLASS(mc1)),
2698 object_class_get_name(OBJECT_CLASS(mc2)));
2701 /* Standalone machine types sort after families. */
2702 return 1;
2705 if (mc2->family == NULL) {
2706 /* Families sort before standalone machine types. */
2707 return -1;
2710 /* Families sort between each other alphabetically increasingly. */
2711 res = strcmp(mc1->family, mc2->family);
2712 if (res != 0) {
2713 return res;
2716 /* Within the same family, machine types sort in decreasing order. */
2717 return strcmp(object_class_get_name(OBJECT_CLASS(mc2)),
2718 object_class_get_name(OBJECT_CLASS(mc1)));
2721 static MachineClass *machine_parse(const char *name)
2723 MachineClass *mc = NULL;
2724 GSList *el, *machines = object_class_get_list(TYPE_MACHINE, false);
2726 if (name) {
2727 mc = find_machine(name);
2729 if (mc) {
2730 g_slist_free(machines);
2731 return mc;
2733 if (name && !is_help_option(name)) {
2734 error_report("unsupported machine type");
2735 error_printf("Use -machine help to list supported machines\n");
2736 } else {
2737 printf("Supported machines are:\n");
2738 machines = g_slist_sort(machines, machine_class_cmp);
2739 for (el = machines; el; el = el->next) {
2740 MachineClass *mc = el->data;
2741 if (mc->alias) {
2742 printf("%-20s %s (alias of %s)\n", mc->alias, mc->desc, mc->name);
2744 printf("%-20s %s%s\n", mc->name, mc->desc,
2745 mc->is_default ? " (default)" : "");
2749 g_slist_free(machines);
2750 exit(!name || !is_help_option(name));
2753 void qemu_add_exit_notifier(Notifier *notify)
2755 notifier_list_add(&exit_notifiers, notify);
2758 void qemu_remove_exit_notifier(Notifier *notify)
2760 notifier_remove(notify);
2763 static void qemu_run_exit_notifiers(void)
2765 notifier_list_notify(&exit_notifiers, NULL);
2768 static bool machine_init_done;
2770 void qemu_add_machine_init_done_notifier(Notifier *notify)
2772 notifier_list_add(&machine_init_done_notifiers, notify);
2773 if (machine_init_done) {
2774 notify->notify(notify, NULL);
2778 void qemu_remove_machine_init_done_notifier(Notifier *notify)
2780 notifier_remove(notify);
2783 static void qemu_run_machine_init_done_notifiers(void)
2785 notifier_list_notify(&machine_init_done_notifiers, NULL);
2786 machine_init_done = true;
2789 static const QEMUOption *lookup_opt(int argc, char **argv,
2790 const char **poptarg, int *poptind)
2792 const QEMUOption *popt;
2793 int optind = *poptind;
2794 char *r = argv[optind];
2795 const char *optarg;
2797 loc_set_cmdline(argv, optind, 1);
2798 optind++;
2799 /* Treat --foo the same as -foo. */
2800 if (r[1] == '-')
2801 r++;
2802 popt = qemu_options;
2803 for(;;) {
2804 if (!popt->name) {
2805 error_report("invalid option");
2806 exit(1);
2808 if (!strcmp(popt->name, r + 1))
2809 break;
2810 popt++;
2812 if (popt->flags & HAS_ARG) {
2813 if (optind >= argc) {
2814 error_report("requires an argument");
2815 exit(1);
2817 optarg = argv[optind++];
2818 loc_set_cmdline(argv, optind - 2, 2);
2819 } else {
2820 optarg = NULL;
2823 *poptarg = optarg;
2824 *poptind = optind;
2826 return popt;
2829 static MachineClass *select_machine(void)
2831 MachineClass *machine_class = find_default_machine();
2832 const char *optarg;
2833 QemuOpts *opts;
2834 Location loc;
2836 loc_push_none(&loc);
2838 opts = qemu_get_machine_opts();
2839 qemu_opts_loc_restore(opts);
2841 optarg = qemu_opt_get(opts, "type");
2842 if (optarg) {
2843 machine_class = machine_parse(optarg);
2846 if (!machine_class) {
2847 error_report("No machine specified, and there is no default");
2848 error_printf("Use -machine help to list supported machines\n");
2849 exit(1);
2852 loc_pop(&loc);
2853 return machine_class;
2856 static int machine_set_property(void *opaque,
2857 const char *name, const char *value,
2858 Error **errp)
2860 Object *obj = OBJECT(opaque);
2861 Error *local_err = NULL;
2862 char *p, *qom_name;
2864 if (strcmp(name, "type") == 0) {
2865 return 0;
2868 qom_name = g_strdup(name);
2869 for (p = qom_name; *p; p++) {
2870 if (*p == '_') {
2871 *p = '-';
2875 object_property_parse(obj, value, qom_name, &local_err);
2876 g_free(qom_name);
2878 if (local_err) {
2879 error_report_err(local_err);
2880 return -1;
2883 return 0;
2888 * Initial object creation happens before all other
2889 * QEMU data types are created. The majority of objects
2890 * can be created at this point. The rng-egd object
2891 * cannot be created here, as it depends on the chardev
2892 * already existing.
2894 static bool object_create_initial(const char *type)
2896 if (g_str_equal(type, "rng-egd")) {
2897 return false;
2901 * return false for concrete netfilters since
2902 * they depend on netdevs already existing
2904 if (g_str_equal(type, "filter-buffer") ||
2905 g_str_equal(type, "filter-dump") ||
2906 g_str_equal(type, "filter-mirror") ||
2907 g_str_equal(type, "filter-redirector") ||
2908 g_str_equal(type, "colo-compare") ||
2909 g_str_equal(type, "filter-rewriter")) {
2910 return false;
2913 /* Memory allocation by backends needs to be done
2914 * after configure_accelerator() (due to the tcg_enabled()
2915 * checks at memory_region_init_*()).
2917 * Also, allocation of large amounts of memory may delay
2918 * chardev initialization for too long, and trigger timeouts
2919 * on software that waits for a monitor socket to be created
2920 * (e.g. libvirt).
2922 if (g_str_has_prefix(type, "memory-backend-")) {
2923 return false;
2926 return true;
2931 * The remainder of object creation happens after the
2932 * creation of chardev, fsdev, net clients and device data types.
2934 static bool object_create_delayed(const char *type)
2936 return !object_create_initial(type);
2940 static void set_memory_options(uint64_t *ram_slots, ram_addr_t *maxram_size,
2941 MachineClass *mc)
2943 uint64_t sz;
2944 const char *mem_str;
2945 const char *maxmem_str, *slots_str;
2946 const ram_addr_t default_ram_size = mc->default_ram_size;
2947 QemuOpts *opts = qemu_find_opts_singleton("memory");
2948 Location loc;
2950 loc_push_none(&loc);
2951 qemu_opts_loc_restore(opts);
2953 sz = 0;
2954 mem_str = qemu_opt_get(opts, "size");
2955 if (mem_str) {
2956 if (!*mem_str) {
2957 error_report("missing 'size' option value");
2958 exit(EXIT_FAILURE);
2961 sz = qemu_opt_get_size(opts, "size", ram_size);
2963 /* Fix up legacy suffix-less format */
2964 if (g_ascii_isdigit(mem_str[strlen(mem_str) - 1])) {
2965 uint64_t overflow_check = sz;
2967 sz <<= 20;
2968 if ((sz >> 20) != overflow_check) {
2969 error_report("too large 'size' option value");
2970 exit(EXIT_FAILURE);
2975 /* backward compatibility behaviour for case "-m 0" */
2976 if (sz == 0) {
2977 sz = default_ram_size;
2980 sz = QEMU_ALIGN_UP(sz, 8192);
2981 ram_size = sz;
2982 if (ram_size != sz) {
2983 error_report("ram size too large");
2984 exit(EXIT_FAILURE);
2987 /* store value for the future use */
2988 qemu_opt_set_number(opts, "size", ram_size, &error_abort);
2989 *maxram_size = ram_size;
2991 maxmem_str = qemu_opt_get(opts, "maxmem");
2992 slots_str = qemu_opt_get(opts, "slots");
2993 if (maxmem_str && slots_str) {
2994 uint64_t slots;
2996 sz = qemu_opt_get_size(opts, "maxmem", 0);
2997 slots = qemu_opt_get_number(opts, "slots", 0);
2998 if (sz < ram_size) {
2999 error_report("invalid value of -m option maxmem: "
3000 "maximum memory size (0x%" PRIx64 ") must be at least "
3001 "the initial memory size (0x" RAM_ADDR_FMT ")",
3002 sz, ram_size);
3003 exit(EXIT_FAILURE);
3004 } else if (sz > ram_size) {
3005 if (!slots) {
3006 error_report("invalid value of -m option: maxmem was "
3007 "specified, but no hotplug slots were specified");
3008 exit(EXIT_FAILURE);
3010 } else if (slots) {
3011 error_report("invalid value of -m option maxmem: "
3012 "memory slots were specified but maximum memory size "
3013 "(0x%" PRIx64 ") is equal to the initial memory size "
3014 "(0x" RAM_ADDR_FMT ")", sz, ram_size);
3015 exit(EXIT_FAILURE);
3018 *maxram_size = sz;
3019 *ram_slots = slots;
3020 } else if ((!maxmem_str && slots_str) ||
3021 (maxmem_str && !slots_str)) {
3022 error_report("invalid -m option value: missing "
3023 "'%s' option", slots_str ? "maxmem" : "slots");
3024 exit(EXIT_FAILURE);
3027 loc_pop(&loc);
3030 static int global_init_func(void *opaque, QemuOpts *opts, Error **errp)
3032 GlobalProperty *g;
3034 g = g_malloc0(sizeof(*g));
3035 g->driver = qemu_opt_get(opts, "driver");
3036 g->property = qemu_opt_get(opts, "property");
3037 g->value = qemu_opt_get(opts, "value");
3038 g->user_provided = true;
3039 g->errp = &error_fatal;
3040 qdev_prop_register_global(g);
3041 return 0;
3044 int main(int argc, char **argv)
3046 int i;
3047 int snapshot, linux_boot;
3048 const char *initrd_filename;
3049 const char *kernel_filename, *kernel_cmdline;
3050 const char *boot_order = NULL;
3051 const char *boot_once = NULL;
3052 DisplayState *ds;
3053 int cyls, heads, secs, translation;
3054 QemuOpts *hda_opts = NULL, *opts, *machine_opts, *icount_opts = NULL;
3055 QemuOptsList *olist;
3056 int optind;
3057 const char *optarg;
3058 const char *loadvm = NULL;
3059 MachineClass *machine_class;
3060 const char *cpu_model;
3061 const char *vga_model = NULL;
3062 const char *qtest_chrdev = NULL;
3063 const char *qtest_log = NULL;
3064 const char *pid_file = NULL;
3065 const char *incoming = NULL;
3066 bool defconfig = true;
3067 bool userconfig = true;
3068 bool nographic = false;
3069 DisplayType display_type = DT_DEFAULT;
3070 int display_remote = 0;
3071 const char *log_mask = NULL;
3072 const char *log_file = NULL;
3073 char *trace_file = NULL;
3074 ram_addr_t maxram_size;
3075 uint64_t ram_slots = 0;
3076 FILE *vmstate_dump_file = NULL;
3077 Error *main_loop_err = NULL;
3078 Error *err = NULL;
3079 bool list_data_dirs = false;
3081 module_call_init(MODULE_INIT_TRACE);
3083 qemu_init_cpu_list();
3084 qemu_init_cpu_loop();
3085 qemu_mutex_lock_iothread();
3087 atexit(qemu_run_exit_notifiers);
3088 error_set_progname(argv[0]);
3089 qemu_init_exec_dir(argv[0]);
3091 module_call_init(MODULE_INIT_QOM);
3092 module_call_init(MODULE_INIT_QAPI);
3094 qemu_add_opts(&qemu_drive_opts);
3095 qemu_add_drive_opts(&qemu_legacy_drive_opts);
3096 qemu_add_drive_opts(&qemu_common_drive_opts);
3097 qemu_add_drive_opts(&qemu_drive_opts);
3098 qemu_add_drive_opts(&bdrv_runtime_opts);
3099 qemu_add_opts(&qemu_chardev_opts);
3100 qemu_add_opts(&qemu_device_opts);
3101 qemu_add_opts(&qemu_netdev_opts);
3102 qemu_add_opts(&qemu_net_opts);
3103 qemu_add_opts(&qemu_rtc_opts);
3104 qemu_add_opts(&qemu_global_opts);
3105 qemu_add_opts(&qemu_mon_opts);
3106 qemu_add_opts(&qemu_trace_opts);
3107 qemu_add_opts(&qemu_option_rom_opts);
3108 qemu_add_opts(&qemu_machine_opts);
3109 qemu_add_opts(&qemu_mem_opts);
3110 qemu_add_opts(&qemu_smp_opts);
3111 qemu_add_opts(&qemu_boot_opts);
3112 qemu_add_opts(&qemu_sandbox_opts);
3113 qemu_add_opts(&qemu_add_fd_opts);
3114 qemu_add_opts(&qemu_object_opts);
3115 qemu_add_opts(&qemu_tpmdev_opts);
3116 qemu_add_opts(&qemu_realtime_opts);
3117 qemu_add_opts(&qemu_msg_opts);
3118 qemu_add_opts(&qemu_name_opts);
3119 qemu_add_opts(&qemu_numa_opts);
3120 qemu_add_opts(&qemu_icount_opts);
3121 qemu_add_opts(&qemu_semihosting_config_opts);
3122 qemu_add_opts(&qemu_fw_cfg_opts);
3123 #ifdef CONFIG_LIBISCSI
3124 qemu_add_opts(&qemu_iscsi_opts);
3125 #endif
3126 module_call_init(MODULE_INIT_OPTS);
3128 runstate_init();
3130 if (qcrypto_init(&err) < 0) {
3131 error_reportf_err(err, "cannot initialize crypto: ");
3132 exit(1);
3134 rtc_clock = QEMU_CLOCK_HOST;
3136 QLIST_INIT (&vm_change_state_head);
3137 os_setup_early_signal_handling();
3139 cpu_model = NULL;
3140 snapshot = 0;
3141 cyls = heads = secs = 0;
3142 translation = BIOS_ATA_TRANSLATION_AUTO;
3144 nb_nics = 0;
3146 bdrv_init_with_whitelist();
3148 autostart = 1;
3150 /* first pass of option parsing */
3151 optind = 1;
3152 while (optind < argc) {
3153 if (argv[optind][0] != '-') {
3154 /* disk image */
3155 optind++;
3156 } else {
3157 const QEMUOption *popt;
3159 popt = lookup_opt(argc, argv, &optarg, &optind);
3160 switch (popt->index) {
3161 case QEMU_OPTION_nodefconfig:
3162 defconfig = false;
3163 break;
3164 case QEMU_OPTION_nouserconfig:
3165 userconfig = false;
3166 break;
3171 if (defconfig) {
3172 int ret;
3173 ret = qemu_read_default_config_files(userconfig);
3174 if (ret < 0) {
3175 exit(1);
3179 /* second pass of option parsing */
3180 optind = 1;
3181 for(;;) {
3182 if (optind >= argc)
3183 break;
3184 if (argv[optind][0] != '-') {
3185 hda_opts = drive_add(IF_DEFAULT, 0, argv[optind++], HD_OPTS);
3186 } else {
3187 const QEMUOption *popt;
3189 popt = lookup_opt(argc, argv, &optarg, &optind);
3190 if (!(popt->arch_mask & arch_type)) {
3191 error_report("Option not supported for this target");
3192 exit(1);
3194 switch(popt->index) {
3195 case QEMU_OPTION_no_kvm_irqchip: {
3196 olist = qemu_find_opts("machine");
3197 qemu_opts_parse_noisily(olist, "kernel_irqchip=off", false);
3198 break;
3200 case QEMU_OPTION_cpu:
3201 /* hw initialization will check this */
3202 cpu_model = optarg;
3203 break;
3204 case QEMU_OPTION_hda:
3206 char buf[256];
3207 if (cyls == 0)
3208 snprintf(buf, sizeof(buf), "%s", HD_OPTS);
3209 else
3210 snprintf(buf, sizeof(buf),
3211 "%s,cyls=%d,heads=%d,secs=%d%s",
3212 HD_OPTS , cyls, heads, secs,
3213 translation == BIOS_ATA_TRANSLATION_LBA ?
3214 ",trans=lba" :
3215 translation == BIOS_ATA_TRANSLATION_NONE ?
3216 ",trans=none" : "");
3217 drive_add(IF_DEFAULT, 0, optarg, buf);
3218 break;
3220 case QEMU_OPTION_hdb:
3221 case QEMU_OPTION_hdc:
3222 case QEMU_OPTION_hdd:
3223 drive_add(IF_DEFAULT, popt->index - QEMU_OPTION_hda, optarg,
3224 HD_OPTS);
3225 break;
3226 case QEMU_OPTION_drive:
3227 if (drive_def(optarg) == NULL) {
3228 exit(1);
3230 break;
3231 case QEMU_OPTION_set:
3232 if (qemu_set_option(optarg) != 0)
3233 exit(1);
3234 break;
3235 case QEMU_OPTION_global:
3236 if (qemu_global_option(optarg) != 0)
3237 exit(1);
3238 break;
3239 case QEMU_OPTION_mtdblock:
3240 drive_add(IF_MTD, -1, optarg, MTD_OPTS);
3241 break;
3242 case QEMU_OPTION_sd:
3243 drive_add(IF_SD, -1, optarg, SD_OPTS);
3244 break;
3245 case QEMU_OPTION_pflash:
3246 drive_add(IF_PFLASH, -1, optarg, PFLASH_OPTS);
3247 break;
3248 case QEMU_OPTION_snapshot:
3249 snapshot = 1;
3250 break;
3251 case QEMU_OPTION_hdachs:
3253 const char *p;
3254 p = optarg;
3255 cyls = strtol(p, (char **)&p, 0);
3256 if (cyls < 1 || cyls > 16383)
3257 goto chs_fail;
3258 if (*p != ',')
3259 goto chs_fail;
3260 p++;
3261 heads = strtol(p, (char **)&p, 0);
3262 if (heads < 1 || heads > 16)
3263 goto chs_fail;
3264 if (*p != ',')
3265 goto chs_fail;
3266 p++;
3267 secs = strtol(p, (char **)&p, 0);
3268 if (secs < 1 || secs > 63)
3269 goto chs_fail;
3270 if (*p == ',') {
3271 p++;
3272 if (!strcmp(p, "large")) {
3273 translation = BIOS_ATA_TRANSLATION_LARGE;
3274 } else if (!strcmp(p, "rechs")) {
3275 translation = BIOS_ATA_TRANSLATION_RECHS;
3276 } else if (!strcmp(p, "none")) {
3277 translation = BIOS_ATA_TRANSLATION_NONE;
3278 } else if (!strcmp(p, "lba")) {
3279 translation = BIOS_ATA_TRANSLATION_LBA;
3280 } else if (!strcmp(p, "auto")) {
3281 translation = BIOS_ATA_TRANSLATION_AUTO;
3282 } else {
3283 goto chs_fail;
3285 } else if (*p != '\0') {
3286 chs_fail:
3287 error_report("invalid physical CHS format");
3288 exit(1);
3290 if (hda_opts != NULL) {
3291 qemu_opt_set_number(hda_opts, "cyls", cyls,
3292 &error_abort);
3293 qemu_opt_set_number(hda_opts, "heads", heads,
3294 &error_abort);
3295 qemu_opt_set_number(hda_opts, "secs", secs,
3296 &error_abort);
3297 if (translation == BIOS_ATA_TRANSLATION_LARGE) {
3298 qemu_opt_set(hda_opts, "trans", "large",
3299 &error_abort);
3300 } else if (translation == BIOS_ATA_TRANSLATION_RECHS) {
3301 qemu_opt_set(hda_opts, "trans", "rechs",
3302 &error_abort);
3303 } else if (translation == BIOS_ATA_TRANSLATION_LBA) {
3304 qemu_opt_set(hda_opts, "trans", "lba",
3305 &error_abort);
3306 } else if (translation == BIOS_ATA_TRANSLATION_NONE) {
3307 qemu_opt_set(hda_opts, "trans", "none",
3308 &error_abort);
3312 break;
3313 case QEMU_OPTION_numa:
3314 opts = qemu_opts_parse_noisily(qemu_find_opts("numa"),
3315 optarg, true);
3316 if (!opts) {
3317 exit(1);
3319 break;
3320 case QEMU_OPTION_display:
3321 display_type = select_display(optarg);
3322 break;
3323 case QEMU_OPTION_nographic:
3324 olist = qemu_find_opts("machine");
3325 qemu_opts_parse_noisily(olist, "graphics=off", false);
3326 nographic = true;
3327 display_type = DT_NONE;
3328 break;
3329 case QEMU_OPTION_curses:
3330 #ifdef CONFIG_CURSES
3331 display_type = DT_CURSES;
3332 #else
3333 error_report("curses support is disabled");
3334 exit(1);
3335 #endif
3336 break;
3337 case QEMU_OPTION_portrait:
3338 graphic_rotate = 90;
3339 break;
3340 case QEMU_OPTION_rotate:
3341 graphic_rotate = strtol(optarg, (char **) &optarg, 10);
3342 if (graphic_rotate != 0 && graphic_rotate != 90 &&
3343 graphic_rotate != 180 && graphic_rotate != 270) {
3344 error_report("only 90, 180, 270 deg rotation is available");
3345 exit(1);
3347 break;
3348 case QEMU_OPTION_kernel:
3349 qemu_opts_set(qemu_find_opts("machine"), 0, "kernel", optarg,
3350 &error_abort);
3351 break;
3352 case QEMU_OPTION_initrd:
3353 qemu_opts_set(qemu_find_opts("machine"), 0, "initrd", optarg,
3354 &error_abort);
3355 break;
3356 case QEMU_OPTION_append:
3357 qemu_opts_set(qemu_find_opts("machine"), 0, "append", optarg,
3358 &error_abort);
3359 break;
3360 case QEMU_OPTION_dtb:
3361 qemu_opts_set(qemu_find_opts("machine"), 0, "dtb", optarg,
3362 &error_abort);
3363 break;
3364 case QEMU_OPTION_cdrom:
3365 drive_add(IF_DEFAULT, 2, optarg, CDROM_OPTS);
3366 break;
3367 case QEMU_OPTION_boot:
3368 opts = qemu_opts_parse_noisily(qemu_find_opts("boot-opts"),
3369 optarg, true);
3370 if (!opts) {
3371 exit(1);
3373 break;
3374 case QEMU_OPTION_fda:
3375 case QEMU_OPTION_fdb:
3376 drive_add(IF_FLOPPY, popt->index - QEMU_OPTION_fda,
3377 optarg, FD_OPTS);
3378 break;
3379 case QEMU_OPTION_no_fd_bootchk:
3380 fd_bootchk = 0;
3381 break;
3382 case QEMU_OPTION_netdev:
3383 default_net = 0;
3384 if (net_client_parse(qemu_find_opts("netdev"), optarg) == -1) {
3385 exit(1);
3387 break;
3388 case QEMU_OPTION_net:
3389 default_net = 0;
3390 if (net_client_parse(qemu_find_opts("net"), optarg) == -1) {
3391 exit(1);
3393 break;
3394 #ifdef CONFIG_LIBISCSI
3395 case QEMU_OPTION_iscsi:
3396 opts = qemu_opts_parse_noisily(qemu_find_opts("iscsi"),
3397 optarg, false);
3398 if (!opts) {
3399 exit(1);
3401 break;
3402 #endif
3403 #ifdef CONFIG_SLIRP
3404 case QEMU_OPTION_tftp:
3405 error_report("The -tftp option is deprecated. "
3406 "Please use '-netdev user,tftp=...' instead.");
3407 legacy_tftp_prefix = optarg;
3408 break;
3409 case QEMU_OPTION_bootp:
3410 error_report("The -bootp option is deprecated. "
3411 "Please use '-netdev user,bootfile=...' instead.");
3412 legacy_bootp_filename = optarg;
3413 break;
3414 case QEMU_OPTION_redir:
3415 error_report("The -redir option is deprecated. "
3416 "Please use '-netdev user,hostfwd=...' instead.");
3417 if (net_slirp_redir(optarg) < 0)
3418 exit(1);
3419 break;
3420 #endif
3421 case QEMU_OPTION_bt:
3422 add_device_config(DEV_BT, optarg);
3423 break;
3424 case QEMU_OPTION_audio_help:
3425 AUD_help ();
3426 exit (0);
3427 break;
3428 case QEMU_OPTION_soundhw:
3429 select_soundhw (optarg);
3430 break;
3431 case QEMU_OPTION_h:
3432 help(0);
3433 break;
3434 case QEMU_OPTION_version:
3435 version();
3436 exit(0);
3437 break;
3438 case QEMU_OPTION_m:
3439 opts = qemu_opts_parse_noisily(qemu_find_opts("memory"),
3440 optarg, true);
3441 if (!opts) {
3442 exit(EXIT_FAILURE);
3444 break;
3445 #ifdef CONFIG_TPM
3446 case QEMU_OPTION_tpmdev:
3447 if (tpm_config_parse(qemu_find_opts("tpmdev"), optarg) < 0) {
3448 exit(1);
3450 break;
3451 #endif
3452 case QEMU_OPTION_mempath:
3453 mem_path = optarg;
3454 break;
3455 case QEMU_OPTION_mem_prealloc:
3456 mem_prealloc = 1;
3457 break;
3458 case QEMU_OPTION_d:
3459 log_mask = optarg;
3460 break;
3461 case QEMU_OPTION_D:
3462 log_file = optarg;
3463 break;
3464 case QEMU_OPTION_DFILTER:
3465 qemu_set_dfilter_ranges(optarg, &error_fatal);
3466 break;
3467 case QEMU_OPTION_s:
3468 add_device_config(DEV_GDB, "tcp::" DEFAULT_GDBSTUB_PORT);
3469 break;
3470 case QEMU_OPTION_gdb:
3471 add_device_config(DEV_GDB, optarg);
3472 break;
3473 case QEMU_OPTION_L:
3474 if (is_help_option(optarg)) {
3475 list_data_dirs = true;
3476 } else if (data_dir_idx < ARRAY_SIZE(data_dir)) {
3477 data_dir[data_dir_idx++] = optarg;
3479 break;
3480 case QEMU_OPTION_bios:
3481 qemu_opts_set(qemu_find_opts("machine"), 0, "firmware", optarg,
3482 &error_abort);
3483 break;
3484 case QEMU_OPTION_singlestep:
3485 singlestep = 1;
3486 break;
3487 case QEMU_OPTION_S:
3488 autostart = 0;
3489 break;
3490 case QEMU_OPTION_k:
3491 keyboard_layout = optarg;
3492 break;
3493 case QEMU_OPTION_localtime:
3494 rtc_utc = 0;
3495 break;
3496 case QEMU_OPTION_vga:
3497 vga_model = optarg;
3498 default_vga = 0;
3499 break;
3500 case QEMU_OPTION_g:
3502 const char *p;
3503 int w, h, depth;
3504 p = optarg;
3505 w = strtol(p, (char **)&p, 10);
3506 if (w <= 0) {
3507 graphic_error:
3508 error_report("invalid resolution or depth");
3509 exit(1);
3511 if (*p != 'x')
3512 goto graphic_error;
3513 p++;
3514 h = strtol(p, (char **)&p, 10);
3515 if (h <= 0)
3516 goto graphic_error;
3517 if (*p == 'x') {
3518 p++;
3519 depth = strtol(p, (char **)&p, 10);
3520 if (depth != 8 && depth != 15 && depth != 16 &&
3521 depth != 24 && depth != 32)
3522 goto graphic_error;
3523 } else if (*p == '\0') {
3524 depth = graphic_depth;
3525 } else {
3526 goto graphic_error;
3529 graphic_width = w;
3530 graphic_height = h;
3531 graphic_depth = depth;
3533 break;
3534 case QEMU_OPTION_echr:
3536 char *r;
3537 term_escape_char = strtol(optarg, &r, 0);
3538 if (r == optarg)
3539 printf("Bad argument to echr\n");
3540 break;
3542 case QEMU_OPTION_monitor:
3543 default_monitor = 0;
3544 if (strncmp(optarg, "none", 4)) {
3545 monitor_parse(optarg, "readline", false);
3547 break;
3548 case QEMU_OPTION_qmp:
3549 monitor_parse(optarg, "control", false);
3550 default_monitor = 0;
3551 break;
3552 case QEMU_OPTION_qmp_pretty:
3553 monitor_parse(optarg, "control", true);
3554 default_monitor = 0;
3555 break;
3556 case QEMU_OPTION_mon:
3557 opts = qemu_opts_parse_noisily(qemu_find_opts("mon"), optarg,
3558 true);
3559 if (!opts) {
3560 exit(1);
3562 default_monitor = 0;
3563 break;
3564 case QEMU_OPTION_chardev:
3565 opts = qemu_opts_parse_noisily(qemu_find_opts("chardev"),
3566 optarg, true);
3567 if (!opts) {
3568 exit(1);
3570 break;
3571 case QEMU_OPTION_fsdev:
3572 olist = qemu_find_opts("fsdev");
3573 if (!olist) {
3574 error_report("fsdev support is disabled");
3575 exit(1);
3577 opts = qemu_opts_parse_noisily(olist, optarg, true);
3578 if (!opts) {
3579 exit(1);
3581 break;
3582 case QEMU_OPTION_virtfs: {
3583 QemuOpts *fsdev;
3584 QemuOpts *device;
3585 const char *writeout, *sock_fd, *socket;
3587 olist = qemu_find_opts("virtfs");
3588 if (!olist) {
3589 error_report("virtfs support is disabled");
3590 exit(1);
3592 opts = qemu_opts_parse_noisily(olist, optarg, true);
3593 if (!opts) {
3594 exit(1);
3597 if (qemu_opt_get(opts, "fsdriver") == NULL ||
3598 qemu_opt_get(opts, "mount_tag") == NULL) {
3599 error_report("Usage: -virtfs fsdriver,mount_tag=tag");
3600 exit(1);
3602 fsdev = qemu_opts_create(qemu_find_opts("fsdev"),
3603 qemu_opt_get(opts, "mount_tag"),
3604 1, NULL);
3605 if (!fsdev) {
3606 error_report("duplicate fsdev id: %s",
3607 qemu_opt_get(opts, "mount_tag"));
3608 exit(1);
3611 writeout = qemu_opt_get(opts, "writeout");
3612 if (writeout) {
3613 #ifdef CONFIG_SYNC_FILE_RANGE
3614 qemu_opt_set(fsdev, "writeout", writeout, &error_abort);
3615 #else
3616 error_report("writeout=immediate not supported "
3617 "on this platform");
3618 exit(1);
3619 #endif
3621 qemu_opt_set(fsdev, "fsdriver",
3622 qemu_opt_get(opts, "fsdriver"), &error_abort);
3623 qemu_opt_set(fsdev, "path", qemu_opt_get(opts, "path"),
3624 &error_abort);
3625 qemu_opt_set(fsdev, "security_model",
3626 qemu_opt_get(opts, "security_model"),
3627 &error_abort);
3628 socket = qemu_opt_get(opts, "socket");
3629 if (socket) {
3630 qemu_opt_set(fsdev, "socket", socket, &error_abort);
3632 sock_fd = qemu_opt_get(opts, "sock_fd");
3633 if (sock_fd) {
3634 qemu_opt_set(fsdev, "sock_fd", sock_fd, &error_abort);
3637 qemu_opt_set_bool(fsdev, "readonly",
3638 qemu_opt_get_bool(opts, "readonly", 0),
3639 &error_abort);
3640 device = qemu_opts_create(qemu_find_opts("device"), NULL, 0,
3641 &error_abort);
3642 qemu_opt_set(device, "driver", "virtio-9p-pci", &error_abort);
3643 qemu_opt_set(device, "fsdev",
3644 qemu_opt_get(opts, "mount_tag"), &error_abort);
3645 qemu_opt_set(device, "mount_tag",
3646 qemu_opt_get(opts, "mount_tag"), &error_abort);
3647 break;
3649 case QEMU_OPTION_virtfs_synth: {
3650 QemuOpts *fsdev;
3651 QemuOpts *device;
3653 fsdev = qemu_opts_create(qemu_find_opts("fsdev"), "v_synth",
3654 1, NULL);
3655 if (!fsdev) {
3656 error_report("duplicate option: %s", "virtfs_synth");
3657 exit(1);
3659 qemu_opt_set(fsdev, "fsdriver", "synth", &error_abort);
3661 device = qemu_opts_create(qemu_find_opts("device"), NULL, 0,
3662 &error_abort);
3663 qemu_opt_set(device, "driver", "virtio-9p-pci", &error_abort);
3664 qemu_opt_set(device, "fsdev", "v_synth", &error_abort);
3665 qemu_opt_set(device, "mount_tag", "v_synth", &error_abort);
3666 break;
3668 case QEMU_OPTION_serial:
3669 add_device_config(DEV_SERIAL, optarg);
3670 default_serial = 0;
3671 if (strncmp(optarg, "mon:", 4) == 0) {
3672 default_monitor = 0;
3674 break;
3675 case QEMU_OPTION_watchdog:
3676 if (watchdog) {
3677 error_report("only one watchdog option may be given");
3678 return 1;
3680 watchdog = optarg;
3681 break;
3682 case QEMU_OPTION_watchdog_action:
3683 if (select_watchdog_action(optarg) == -1) {
3684 error_report("unknown -watchdog-action parameter");
3685 exit(1);
3687 break;
3688 case QEMU_OPTION_virtiocon:
3689 add_device_config(DEV_VIRTCON, optarg);
3690 default_virtcon = 0;
3691 if (strncmp(optarg, "mon:", 4) == 0) {
3692 default_monitor = 0;
3694 break;
3695 case QEMU_OPTION_parallel:
3696 add_device_config(DEV_PARALLEL, optarg);
3697 default_parallel = 0;
3698 if (strncmp(optarg, "mon:", 4) == 0) {
3699 default_monitor = 0;
3701 break;
3702 case QEMU_OPTION_debugcon:
3703 add_device_config(DEV_DEBUGCON, optarg);
3704 break;
3705 case QEMU_OPTION_loadvm:
3706 loadvm = optarg;
3707 break;
3708 case QEMU_OPTION_full_screen:
3709 full_screen = 1;
3710 break;
3711 case QEMU_OPTION_no_frame:
3712 no_frame = 1;
3713 break;
3714 case QEMU_OPTION_alt_grab:
3715 alt_grab = 1;
3716 break;
3717 case QEMU_OPTION_ctrl_grab:
3718 ctrl_grab = 1;
3719 break;
3720 case QEMU_OPTION_no_quit:
3721 no_quit = 1;
3722 break;
3723 case QEMU_OPTION_sdl:
3724 #ifdef CONFIG_SDL
3725 display_type = DT_SDL;
3726 break;
3727 #else
3728 error_report("SDL support is disabled");
3729 exit(1);
3730 #endif
3731 case QEMU_OPTION_pidfile:
3732 pid_file = optarg;
3733 break;
3734 case QEMU_OPTION_win2k_hack:
3735 win2k_install_hack = 1;
3736 break;
3737 case QEMU_OPTION_rtc_td_hack: {
3738 static GlobalProperty slew_lost_ticks = {
3739 .driver = "mc146818rtc",
3740 .property = "lost_tick_policy",
3741 .value = "slew",
3744 qdev_prop_register_global(&slew_lost_ticks);
3745 break;
3747 case QEMU_OPTION_acpitable:
3748 opts = qemu_opts_parse_noisily(qemu_find_opts("acpi"),
3749 optarg, true);
3750 if (!opts) {
3751 exit(1);
3753 do_acpitable_option(opts);
3754 break;
3755 case QEMU_OPTION_smbios:
3756 opts = qemu_opts_parse_noisily(qemu_find_opts("smbios"),
3757 optarg, false);
3758 if (!opts) {
3759 exit(1);
3761 do_smbios_option(opts);
3762 break;
3763 case QEMU_OPTION_fwcfg:
3764 opts = qemu_opts_parse_noisily(qemu_find_opts("fw_cfg"),
3765 optarg, true);
3766 if (opts == NULL) {
3767 exit(1);
3769 break;
3770 case QEMU_OPTION_enable_kvm:
3771 olist = qemu_find_opts("machine");
3772 qemu_opts_parse_noisily(olist, "accel=kvm", false);
3773 break;
3774 case QEMU_OPTION_enable_hax:
3775 olist = qemu_find_opts("machine");
3776 qemu_opts_parse_noisily(olist, "accel=hax", false);
3777 break;
3778 case QEMU_OPTION_M:
3779 case QEMU_OPTION_machine:
3780 olist = qemu_find_opts("machine");
3781 opts = qemu_opts_parse_noisily(olist, optarg, true);
3782 if (!opts) {
3783 exit(1);
3785 break;
3786 case QEMU_OPTION_no_kvm:
3787 olist = qemu_find_opts("machine");
3788 qemu_opts_parse_noisily(olist, "accel=tcg", false);
3789 break;
3790 case QEMU_OPTION_no_kvm_pit: {
3791 error_report("warning: ignoring deprecated option");
3792 break;
3794 case QEMU_OPTION_no_kvm_pit_reinjection: {
3795 static GlobalProperty kvm_pit_lost_tick_policy = {
3796 .driver = "kvm-pit",
3797 .property = "lost_tick_policy",
3798 .value = "discard",
3801 error_report("warning: deprecated, replaced by "
3802 "-global kvm-pit.lost_tick_policy=discard");
3803 qdev_prop_register_global(&kvm_pit_lost_tick_policy);
3804 break;
3806 case QEMU_OPTION_usb:
3807 olist = qemu_find_opts("machine");
3808 qemu_opts_parse_noisily(olist, "usb=on", false);
3809 break;
3810 case QEMU_OPTION_usbdevice:
3811 olist = qemu_find_opts("machine");
3812 qemu_opts_parse_noisily(olist, "usb=on", false);
3813 add_device_config(DEV_USB, optarg);
3814 break;
3815 case QEMU_OPTION_device:
3816 if (!qemu_opts_parse_noisily(qemu_find_opts("device"),
3817 optarg, true)) {
3818 exit(1);
3820 break;
3821 case QEMU_OPTION_smp:
3822 if (!qemu_opts_parse_noisily(qemu_find_opts("smp-opts"),
3823 optarg, true)) {
3824 exit(1);
3826 break;
3827 case QEMU_OPTION_vnc:
3828 vnc_parse(optarg, &error_fatal);
3829 break;
3830 case QEMU_OPTION_no_acpi:
3831 acpi_enabled = 0;
3832 break;
3833 case QEMU_OPTION_no_hpet:
3834 no_hpet = 1;
3835 break;
3836 case QEMU_OPTION_balloon:
3837 if (balloon_parse(optarg) < 0) {
3838 error_report("unknown -balloon argument %s", optarg);
3839 exit(1);
3841 break;
3842 case QEMU_OPTION_no_reboot:
3843 no_reboot = 1;
3844 break;
3845 case QEMU_OPTION_no_shutdown:
3846 no_shutdown = 1;
3847 break;
3848 case QEMU_OPTION_show_cursor:
3849 cursor_hide = 0;
3850 break;
3851 case QEMU_OPTION_uuid:
3852 if (qemu_uuid_parse(optarg, &qemu_uuid) < 0) {
3853 error_report("failed to parse UUID string: wrong format");
3854 exit(1);
3856 qemu_uuid_set = true;
3857 break;
3858 case QEMU_OPTION_option_rom:
3859 if (nb_option_roms >= MAX_OPTION_ROMS) {
3860 error_report("too many option ROMs");
3861 exit(1);
3863 opts = qemu_opts_parse_noisily(qemu_find_opts("option-rom"),
3864 optarg, true);
3865 if (!opts) {
3866 exit(1);
3868 option_rom[nb_option_roms].name = qemu_opt_get(opts, "romfile");
3869 option_rom[nb_option_roms].bootindex =
3870 qemu_opt_get_number(opts, "bootindex", -1);
3871 if (!option_rom[nb_option_roms].name) {
3872 error_report("Option ROM file is not specified");
3873 exit(1);
3875 nb_option_roms++;
3876 break;
3877 case QEMU_OPTION_semihosting:
3878 semihosting.enabled = true;
3879 semihosting.target = SEMIHOSTING_TARGET_AUTO;
3880 break;
3881 case QEMU_OPTION_semihosting_config:
3882 semihosting.enabled = true;
3883 opts = qemu_opts_parse_noisily(qemu_find_opts("semihosting-config"),
3884 optarg, false);
3885 if (opts != NULL) {
3886 semihosting.enabled = qemu_opt_get_bool(opts, "enable",
3887 true);
3888 const char *target = qemu_opt_get(opts, "target");
3889 if (target != NULL) {
3890 if (strcmp("native", target) == 0) {
3891 semihosting.target = SEMIHOSTING_TARGET_NATIVE;
3892 } else if (strcmp("gdb", target) == 0) {
3893 semihosting.target = SEMIHOSTING_TARGET_GDB;
3894 } else if (strcmp("auto", target) == 0) {
3895 semihosting.target = SEMIHOSTING_TARGET_AUTO;
3896 } else {
3897 error_report("unsupported semihosting-config %s",
3898 optarg);
3899 exit(1);
3901 } else {
3902 semihosting.target = SEMIHOSTING_TARGET_AUTO;
3904 /* Set semihosting argument count and vector */
3905 qemu_opt_foreach(opts, add_semihosting_arg,
3906 &semihosting, NULL);
3907 } else {
3908 error_report("unsupported semihosting-config %s", optarg);
3909 exit(1);
3911 break;
3912 case QEMU_OPTION_tdf:
3913 error_report("warning: ignoring deprecated option");
3914 break;
3915 case QEMU_OPTION_name:
3916 opts = qemu_opts_parse_noisily(qemu_find_opts("name"),
3917 optarg, true);
3918 if (!opts) {
3919 exit(1);
3921 break;
3922 case QEMU_OPTION_prom_env:
3923 if (nb_prom_envs >= MAX_PROM_ENVS) {
3924 error_report("too many prom variables");
3925 exit(1);
3927 prom_envs[nb_prom_envs] = optarg;
3928 nb_prom_envs++;
3929 break;
3930 case QEMU_OPTION_old_param:
3931 old_param = 1;
3932 break;
3933 case QEMU_OPTION_clock:
3934 /* Clock options no longer exist. Keep this option for
3935 * backward compatibility.
3937 break;
3938 case QEMU_OPTION_startdate:
3939 configure_rtc_date_offset(optarg, 1);
3940 break;
3941 case QEMU_OPTION_rtc:
3942 opts = qemu_opts_parse_noisily(qemu_find_opts("rtc"), optarg,
3943 false);
3944 if (!opts) {
3945 exit(1);
3947 configure_rtc(opts);
3948 break;
3949 case QEMU_OPTION_tb_size:
3950 tcg_tb_size = strtol(optarg, NULL, 0);
3951 if (tcg_tb_size < 0) {
3952 tcg_tb_size = 0;
3954 break;
3955 case QEMU_OPTION_icount:
3956 icount_opts = qemu_opts_parse_noisily(qemu_find_opts("icount"),
3957 optarg, true);
3958 if (!icount_opts) {
3959 exit(1);
3961 break;
3962 case QEMU_OPTION_incoming:
3963 if (!incoming) {
3964 runstate_set(RUN_STATE_INMIGRATE);
3966 incoming = optarg;
3967 break;
3968 case QEMU_OPTION_nodefaults:
3969 has_defaults = 0;
3970 break;
3971 case QEMU_OPTION_xen_domid:
3972 if (!(xen_available())) {
3973 error_report("Option not supported for this target");
3974 exit(1);
3976 xen_domid = atoi(optarg);
3977 break;
3978 case QEMU_OPTION_xen_create:
3979 if (!(xen_available())) {
3980 error_report("Option not supported for this target");
3981 exit(1);
3983 xen_mode = XEN_CREATE;
3984 break;
3985 case QEMU_OPTION_xen_attach:
3986 if (!(xen_available())) {
3987 error_report("Option not supported for this target");
3988 exit(1);
3990 xen_mode = XEN_ATTACH;
3991 break;
3992 case QEMU_OPTION_trace:
3993 g_free(trace_file);
3994 trace_file = trace_opt_parse(optarg);
3995 break;
3996 case QEMU_OPTION_trace_unassigned:
3997 trace_unassigned = true;
3998 break;
3999 case QEMU_OPTION_readconfig:
4001 int ret = qemu_read_config_file(optarg);
4002 if (ret < 0) {
4003 error_report("read config %s: %s", optarg,
4004 strerror(-ret));
4005 exit(1);
4007 break;
4009 case QEMU_OPTION_spice:
4010 olist = qemu_find_opts("spice");
4011 if (!olist) {
4012 error_report("spice support is disabled");
4013 exit(1);
4015 opts = qemu_opts_parse_noisily(olist, optarg, false);
4016 if (!opts) {
4017 exit(1);
4019 display_remote++;
4020 break;
4021 case QEMU_OPTION_writeconfig:
4023 FILE *fp;
4024 if (strcmp(optarg, "-") == 0) {
4025 fp = stdout;
4026 } else {
4027 fp = fopen(optarg, "w");
4028 if (fp == NULL) {
4029 error_report("open %s: %s", optarg,
4030 strerror(errno));
4031 exit(1);
4034 qemu_config_write(fp);
4035 if (fp != stdout) {
4036 fclose(fp);
4038 break;
4040 case QEMU_OPTION_qtest:
4041 qtest_chrdev = optarg;
4042 break;
4043 case QEMU_OPTION_qtest_log:
4044 qtest_log = optarg;
4045 break;
4046 case QEMU_OPTION_sandbox:
4047 opts = qemu_opts_parse_noisily(qemu_find_opts("sandbox"),
4048 optarg, true);
4049 if (!opts) {
4050 exit(1);
4052 break;
4053 case QEMU_OPTION_add_fd:
4054 #ifndef _WIN32
4055 opts = qemu_opts_parse_noisily(qemu_find_opts("add-fd"),
4056 optarg, false);
4057 if (!opts) {
4058 exit(1);
4060 #else
4061 error_report("File descriptor passing is disabled on this "
4062 "platform");
4063 exit(1);
4064 #endif
4065 break;
4066 case QEMU_OPTION_object:
4067 opts = qemu_opts_parse_noisily(qemu_find_opts("object"),
4068 optarg, true);
4069 if (!opts) {
4070 exit(1);
4072 break;
4073 case QEMU_OPTION_realtime:
4074 opts = qemu_opts_parse_noisily(qemu_find_opts("realtime"),
4075 optarg, false);
4076 if (!opts) {
4077 exit(1);
4079 enable_mlock = qemu_opt_get_bool(opts, "mlock", true);
4080 break;
4081 case QEMU_OPTION_msg:
4082 opts = qemu_opts_parse_noisily(qemu_find_opts("msg"), optarg,
4083 false);
4084 if (!opts) {
4085 exit(1);
4087 configure_msg(opts);
4088 break;
4089 case QEMU_OPTION_dump_vmstate:
4090 if (vmstate_dump_file) {
4091 error_report("only one '-dump-vmstate' "
4092 "option may be given");
4093 exit(1);
4095 vmstate_dump_file = fopen(optarg, "w");
4096 if (vmstate_dump_file == NULL) {
4097 error_report("open %s: %s", optarg, strerror(errno));
4098 exit(1);
4100 break;
4101 default:
4102 os_parse_cmd_args(popt->index, optarg);
4107 * Clear error location left behind by the loop.
4108 * Best done right after the loop. Do not insert code here!
4110 loc_set_none();
4112 replay_configure(icount_opts);
4114 machine_class = select_machine();
4116 set_memory_options(&ram_slots, &maxram_size, machine_class);
4118 os_daemonize();
4120 if (pid_file && qemu_create_pidfile(pid_file) != 0) {
4121 error_report("could not acquire pid file: %s", strerror(errno));
4122 exit(1);
4125 if (qemu_init_main_loop(&main_loop_err)) {
4126 error_report_err(main_loop_err);
4127 exit(1);
4130 if (qemu_opts_foreach(qemu_find_opts("sandbox"),
4131 parse_sandbox, NULL, NULL)) {
4132 exit(1);
4135 if (qemu_opts_foreach(qemu_find_opts("name"),
4136 parse_name, NULL, NULL)) {
4137 exit(1);
4140 #ifndef _WIN32
4141 if (qemu_opts_foreach(qemu_find_opts("add-fd"),
4142 parse_add_fd, NULL, NULL)) {
4143 exit(1);
4146 if (qemu_opts_foreach(qemu_find_opts("add-fd"),
4147 cleanup_add_fd, NULL, NULL)) {
4148 exit(1);
4150 #endif
4152 current_machine = MACHINE(object_new(object_class_get_name(
4153 OBJECT_CLASS(machine_class))));
4154 if (machine_help_func(qemu_get_machine_opts(), current_machine)) {
4155 exit(0);
4157 object_property_add_child(object_get_root(), "machine",
4158 OBJECT(current_machine), &error_abort);
4160 if (machine_class->minimum_page_bits) {
4161 if (!set_preferred_target_page_bits(machine_class->minimum_page_bits)) {
4162 /* This would be a board error: specifying a minimum smaller than
4163 * a target's compile-time fixed setting.
4165 g_assert_not_reached();
4169 cpu_exec_init_all();
4171 if (machine_class->hw_version) {
4172 qemu_set_hw_version(machine_class->hw_version);
4175 if (cpu_model && is_help_option(cpu_model)) {
4176 list_cpus(stdout, &fprintf, cpu_model);
4177 exit(0);
4180 if (!trace_init_backends()) {
4181 exit(1);
4183 trace_init_file(trace_file);
4185 /* Open the logfile at this point and set the log mask if necessary.
4187 if (log_file) {
4188 qemu_set_log_filename(log_file, &error_fatal);
4191 if (log_mask) {
4192 int mask;
4193 mask = qemu_str_to_log_mask(log_mask);
4194 if (!mask) {
4195 qemu_print_log_usage(stdout);
4196 exit(1);
4198 qemu_set_log(mask);
4199 } else {
4200 qemu_set_log(0);
4203 /* If no data_dir is specified then try to find it relative to the
4204 executable path. */
4205 if (data_dir_idx < ARRAY_SIZE(data_dir)) {
4206 data_dir[data_dir_idx] = os_find_datadir();
4207 if (data_dir[data_dir_idx] != NULL) {
4208 data_dir_idx++;
4211 /* If all else fails use the install path specified when building. */
4212 if (data_dir_idx < ARRAY_SIZE(data_dir)) {
4213 data_dir[data_dir_idx++] = CONFIG_QEMU_DATADIR;
4216 /* -L help lists the data directories and exits. */
4217 if (list_data_dirs) {
4218 for (i = 0; i < data_dir_idx; i++) {
4219 printf("%s\n", data_dir[i]);
4221 exit(0);
4224 smp_parse(qemu_opts_find(qemu_find_opts("smp-opts"), NULL));
4226 machine_class->max_cpus = machine_class->max_cpus ?: 1; /* Default to UP */
4227 if (max_cpus > machine_class->max_cpus) {
4228 error_report("Number of SMP CPUs requested (%d) exceeds max CPUs "
4229 "supported by machine '%s' (%d)", max_cpus,
4230 machine_class->name, machine_class->max_cpus);
4231 exit(1);
4235 * Get the default machine options from the machine if it is not already
4236 * specified either by the configuration file or by the command line.
4238 if (machine_class->default_machine_opts) {
4239 qemu_opts_set_defaults(qemu_find_opts("machine"),
4240 machine_class->default_machine_opts, 0);
4243 qemu_opts_foreach(qemu_find_opts("device"),
4244 default_driver_check, NULL, NULL);
4245 qemu_opts_foreach(qemu_find_opts("global"),
4246 default_driver_check, NULL, NULL);
4248 if (!vga_model && !default_vga) {
4249 vga_interface_type = VGA_DEVICE;
4251 if (!has_defaults || machine_class->no_serial) {
4252 default_serial = 0;
4254 if (!has_defaults || machine_class->no_parallel) {
4255 default_parallel = 0;
4257 if (!has_defaults || !machine_class->use_virtcon) {
4258 default_virtcon = 0;
4260 if (!has_defaults || !machine_class->use_sclp) {
4261 default_sclp = 0;
4263 if (!has_defaults || machine_class->no_floppy) {
4264 default_floppy = 0;
4266 if (!has_defaults || machine_class->no_cdrom) {
4267 default_cdrom = 0;
4269 if (!has_defaults || machine_class->no_sdcard) {
4270 default_sdcard = 0;
4272 if (!has_defaults) {
4273 default_monitor = 0;
4274 default_net = 0;
4275 default_vga = 0;
4278 if (is_daemonized()) {
4279 /* According to documentation and historically, -nographic redirects
4280 * serial port, parallel port and monitor to stdio, which does not work
4281 * with -daemonize. We can redirect these to null instead, but since
4282 * -nographic is legacy, let's just error out.
4283 * We disallow -nographic only if all other ports are not redirected
4284 * explicitly, to not break existing legacy setups which uses
4285 * -nographic _and_ redirects all ports explicitly - this is valid
4286 * usage, -nographic is just a no-op in this case.
4288 if (nographic
4289 && (default_parallel || default_serial
4290 || default_monitor || default_virtcon)) {
4291 error_report("-nographic cannot be used with -daemonize");
4292 exit(1);
4294 #ifdef CONFIG_CURSES
4295 if (display_type == DT_CURSES) {
4296 error_report("curses display cannot be used with -daemonize");
4297 exit(1);
4299 #endif
4302 if (nographic) {
4303 if (default_parallel)
4304 add_device_config(DEV_PARALLEL, "null");
4305 if (default_serial && default_monitor) {
4306 add_device_config(DEV_SERIAL, "mon:stdio");
4307 } else if (default_virtcon && default_monitor) {
4308 add_device_config(DEV_VIRTCON, "mon:stdio");
4309 } else if (default_sclp && default_monitor) {
4310 add_device_config(DEV_SCLP, "mon:stdio");
4311 } else {
4312 if (default_serial)
4313 add_device_config(DEV_SERIAL, "stdio");
4314 if (default_virtcon)
4315 add_device_config(DEV_VIRTCON, "stdio");
4316 if (default_sclp) {
4317 add_device_config(DEV_SCLP, "stdio");
4319 if (default_monitor)
4320 monitor_parse("stdio", "readline", false);
4322 } else {
4323 if (default_serial)
4324 add_device_config(DEV_SERIAL, "vc:80Cx24C");
4325 if (default_parallel)
4326 add_device_config(DEV_PARALLEL, "vc:80Cx24C");
4327 if (default_monitor)
4328 monitor_parse("vc:80Cx24C", "readline", false);
4329 if (default_virtcon)
4330 add_device_config(DEV_VIRTCON, "vc:80Cx24C");
4331 if (default_sclp) {
4332 add_device_config(DEV_SCLP, "vc:80Cx24C");
4336 #if defined(CONFIG_VNC)
4337 if (!QTAILQ_EMPTY(&(qemu_find_opts("vnc")->head))) {
4338 display_remote++;
4340 #endif
4341 if (display_type == DT_DEFAULT && !display_remote) {
4342 #if defined(CONFIG_GTK)
4343 display_type = DT_GTK;
4344 #elif defined(CONFIG_SDL)
4345 display_type = DT_SDL;
4346 #elif defined(CONFIG_COCOA)
4347 display_type = DT_COCOA;
4348 #elif defined(CONFIG_VNC)
4349 vnc_parse("localhost:0,to=99,id=default", &error_abort);
4350 #else
4351 display_type = DT_NONE;
4352 #endif
4355 if ((no_frame || alt_grab || ctrl_grab) && display_type != DT_SDL) {
4356 error_report("-no-frame, -alt-grab and -ctrl-grab are only valid "
4357 "for SDL, ignoring option");
4359 if (no_quit && (display_type != DT_GTK && display_type != DT_SDL)) {
4360 error_report("-no-quit is only valid for GTK and SDL, "
4361 "ignoring option");
4364 if (display_type == DT_GTK) {
4365 early_gtk_display_init(request_opengl);
4368 if (display_type == DT_SDL) {
4369 sdl_display_early_init(request_opengl);
4372 if (request_opengl == 1 && display_opengl == 0) {
4373 #if defined(CONFIG_OPENGL)
4374 error_report("OpenGL is not supported by the display");
4375 #else
4376 error_report("OpenGL support is disabled");
4377 #endif
4378 exit(1);
4381 page_size_init();
4382 socket_init();
4384 if (qemu_opts_foreach(qemu_find_opts("object"),
4385 user_creatable_add_opts_foreach,
4386 object_create_initial, NULL)) {
4387 exit(1);
4390 if (qemu_opts_foreach(qemu_find_opts("chardev"),
4391 chardev_init_func, NULL, NULL)) {
4392 exit(1);
4395 #ifdef CONFIG_VIRTFS
4396 if (qemu_opts_foreach(qemu_find_opts("fsdev"),
4397 fsdev_init_func, NULL, NULL)) {
4398 exit(1);
4400 #endif
4402 if (qemu_opts_foreach(qemu_find_opts("device"),
4403 device_help_func, NULL, NULL)) {
4404 exit(0);
4407 machine_opts = qemu_get_machine_opts();
4408 if (qemu_opt_foreach(machine_opts, machine_set_property, current_machine,
4409 NULL)) {
4410 object_unref(OBJECT(current_machine));
4411 exit(1);
4414 configure_accelerator(current_machine);
4416 if (qtest_chrdev) {
4417 qtest_init(qtest_chrdev, qtest_log, &error_fatal);
4420 machine_opts = qemu_get_machine_opts();
4421 kernel_filename = qemu_opt_get(machine_opts, "kernel");
4422 initrd_filename = qemu_opt_get(machine_opts, "initrd");
4423 kernel_cmdline = qemu_opt_get(machine_opts, "append");
4424 bios_name = qemu_opt_get(machine_opts, "firmware");
4426 opts = qemu_opts_find(qemu_find_opts("boot-opts"), NULL);
4427 if (opts) {
4428 boot_order = qemu_opt_get(opts, "order");
4429 if (boot_order) {
4430 validate_bootdevices(boot_order, &error_fatal);
4433 boot_once = qemu_opt_get(opts, "once");
4434 if (boot_once) {
4435 validate_bootdevices(boot_once, &error_fatal);
4438 boot_menu = qemu_opt_get_bool(opts, "menu", boot_menu);
4439 boot_strict = qemu_opt_get_bool(opts, "strict", false);
4442 if (!boot_order) {
4443 boot_order = machine_class->default_boot_order;
4446 if (!kernel_cmdline) {
4447 kernel_cmdline = "";
4448 current_machine->kernel_cmdline = (char *)kernel_cmdline;
4451 linux_boot = (kernel_filename != NULL);
4453 if (!linux_boot && *kernel_cmdline != '\0') {
4454 error_report("-append only allowed with -kernel option");
4455 exit(1);
4458 if (!linux_boot && initrd_filename != NULL) {
4459 error_report("-initrd only allowed with -kernel option");
4460 exit(1);
4463 if (semihosting_enabled() && !semihosting_get_argc() && kernel_filename) {
4464 /* fall back to the -kernel/-append */
4465 semihosting_arg_fallback(kernel_filename, kernel_cmdline);
4468 os_set_line_buffering();
4470 /* spice needs the timers to be initialized by this point */
4471 qemu_spice_init();
4473 cpu_ticks_init();
4474 if (icount_opts) {
4475 if (!tcg_enabled()) {
4476 error_report("-icount is not allowed with hardware virtualization");
4477 exit(1);
4479 configure_icount(icount_opts, &error_abort);
4480 qemu_opts_del(icount_opts);
4483 if (default_net) {
4484 QemuOptsList *net = qemu_find_opts("net");
4485 qemu_opts_set(net, NULL, "type", "nic", &error_abort);
4486 #ifdef CONFIG_SLIRP
4487 qemu_opts_set(net, NULL, "type", "user", &error_abort);
4488 #endif
4491 colo_info_init();
4493 if (net_init_clients() < 0) {
4494 exit(1);
4497 if (qemu_opts_foreach(qemu_find_opts("object"),
4498 user_creatable_add_opts_foreach,
4499 object_create_delayed, NULL)) {
4500 exit(1);
4503 #ifdef CONFIG_TPM
4504 if (tpm_init() < 0) {
4505 exit(1);
4507 #endif
4509 /* init the bluetooth world */
4510 if (foreach_device_config(DEV_BT, bt_parse))
4511 exit(1);
4513 if (!xen_enabled()) {
4514 /* On 32-bit hosts, QEMU is limited by virtual address space */
4515 if (ram_size > (2047 << 20) && HOST_LONG_BITS == 32) {
4516 error_report("at most 2047 MB RAM can be simulated");
4517 exit(1);
4521 blk_mig_init();
4522 ram_mig_init();
4524 /* If the currently selected machine wishes to override the units-per-bus
4525 * property of its default HBA interface type, do so now. */
4526 if (machine_class->units_per_default_bus) {
4527 override_max_devs(machine_class->block_default_type,
4528 machine_class->units_per_default_bus);
4531 /* open the virtual block devices */
4532 if (snapshot || replay_mode != REPLAY_MODE_NONE) {
4533 qemu_opts_foreach(qemu_find_opts("drive"), drive_enable_snapshot,
4534 NULL, NULL);
4536 if (qemu_opts_foreach(qemu_find_opts("drive"), drive_init_func,
4537 &machine_class->block_default_type, NULL)) {
4538 exit(1);
4541 default_drive(default_cdrom, snapshot, machine_class->block_default_type, 2,
4542 CDROM_OPTS);
4543 default_drive(default_floppy, snapshot, IF_FLOPPY, 0, FD_OPTS);
4544 default_drive(default_sdcard, snapshot, IF_SD, 0, SD_OPTS);
4546 parse_numa_opts(machine_class);
4548 if (qemu_opts_foreach(qemu_find_opts("mon"),
4549 mon_init_func, NULL, NULL)) {
4550 exit(1);
4553 if (foreach_device_config(DEV_SERIAL, serial_parse) < 0)
4554 exit(1);
4555 if (foreach_device_config(DEV_PARALLEL, parallel_parse) < 0)
4556 exit(1);
4557 if (foreach_device_config(DEV_VIRTCON, virtcon_parse) < 0)
4558 exit(1);
4559 if (foreach_device_config(DEV_SCLP, sclp_parse) < 0) {
4560 exit(1);
4562 if (foreach_device_config(DEV_DEBUGCON, debugcon_parse) < 0)
4563 exit(1);
4565 /* If no default VGA is requested, the default is "none". */
4566 if (default_vga) {
4567 if (machine_class->default_display) {
4568 vga_model = machine_class->default_display;
4569 } else if (vga_interface_available(VGA_CIRRUS)) {
4570 vga_model = "cirrus";
4571 } else if (vga_interface_available(VGA_STD)) {
4572 vga_model = "std";
4575 if (vga_model) {
4576 select_vgahw(vga_model);
4579 if (watchdog) {
4580 i = select_watchdog(watchdog);
4581 if (i > 0)
4582 exit (i == 1 ? 1 : 0);
4585 machine_register_compat_props(current_machine);
4587 qemu_opts_foreach(qemu_find_opts("global"),
4588 global_init_func, NULL, NULL);
4590 /* This checkpoint is required by replay to separate prior clock
4591 reading from the other reads, because timer polling functions query
4592 clock values from the log. */
4593 replay_checkpoint(CHECKPOINT_INIT);
4594 qdev_machine_init();
4596 current_machine->ram_size = ram_size;
4597 current_machine->maxram_size = maxram_size;
4598 current_machine->ram_slots = ram_slots;
4599 current_machine->boot_order = boot_order;
4600 current_machine->cpu_model = cpu_model;
4602 machine_class->init(current_machine);
4604 realtime_init();
4606 audio_init();
4608 cpu_synchronize_all_post_init();
4610 numa_post_machine_init();
4612 if (hax_enabled()) {
4613 hax_sync_vcpus();
4616 if (qemu_opts_foreach(qemu_find_opts("fw_cfg"),
4617 parse_fw_cfg, fw_cfg_find(), NULL) != 0) {
4618 exit(1);
4621 /* init USB devices */
4622 if (machine_usb(current_machine)) {
4623 if (foreach_device_config(DEV_USB, usb_parse) < 0)
4624 exit(1);
4627 /* Check if IGD GFX passthrough. */
4628 igd_gfx_passthru();
4630 /* init generic devices */
4631 rom_set_order_override(FW_CFG_ORDER_OVERRIDE_DEVICE);
4632 if (qemu_opts_foreach(qemu_find_opts("device"),
4633 device_init_func, NULL, NULL)) {
4634 exit(1);
4636 rom_reset_order_override();
4638 /* Did we create any drives that we failed to create a device for? */
4639 drive_check_orphaned();
4641 /* Don't warn about the default network setup that you get if
4642 * no command line -net or -netdev options are specified. There
4643 * are two cases that we would otherwise complain about:
4644 * (1) board doesn't support a NIC but the implicit "-net nic"
4645 * requested one
4646 * (2) CONFIG_SLIRP not set, in which case the implicit "-net nic"
4647 * sets up a nic that isn't connected to anything.
4649 if (!default_net) {
4650 net_check_clients();
4654 if (boot_once) {
4655 qemu_boot_set(boot_once, &error_fatal);
4656 qemu_register_reset(restore_boot_order, g_strdup(boot_order));
4659 ds = init_displaystate();
4661 /* init local displays */
4662 switch (display_type) {
4663 case DT_CURSES:
4664 curses_display_init(ds, full_screen);
4665 break;
4666 case DT_SDL:
4667 sdl_display_init(ds, full_screen, no_frame);
4668 break;
4669 case DT_COCOA:
4670 cocoa_display_init(ds, full_screen);
4671 break;
4672 case DT_GTK:
4673 gtk_display_init(ds, full_screen, grab_on_hover);
4674 break;
4675 default:
4676 break;
4679 /* must be after terminal init, SDL library changes signal handlers */
4680 os_setup_signal_handling();
4682 /* init remote displays */
4683 #ifdef CONFIG_VNC
4684 qemu_opts_foreach(qemu_find_opts("vnc"),
4685 vnc_init_func, NULL, NULL);
4686 #endif
4688 if (using_spice) {
4689 qemu_spice_display_init();
4692 if (foreach_device_config(DEV_GDB, gdbserver_start) < 0) {
4693 exit(1);
4696 qdev_machine_creation_done();
4698 /* TODO: once all bus devices are qdevified, this should be done
4699 * when bus is created by qdev.c */
4700 qemu_register_reset(qbus_reset_all_fn, sysbus_get_default());
4701 qemu_run_machine_init_done_notifiers();
4703 if (rom_check_and_register_reset() != 0) {
4704 error_report("rom check and register reset failed");
4705 exit(1);
4708 replay_start();
4710 /* This checkpoint is required by replay to separate prior clock
4711 reading from the other reads, because timer polling functions query
4712 clock values from the log. */
4713 replay_checkpoint(CHECKPOINT_RESET);
4714 qemu_system_reset(VMRESET_SILENT);
4715 register_global_state();
4716 if (loadvm) {
4717 if (load_vmstate(loadvm) < 0) {
4718 autostart = 0;
4722 qdev_prop_check_globals();
4723 if (vmstate_dump_file) {
4724 /* dump and exit */
4725 dump_vmstate_json_to_file(vmstate_dump_file);
4726 return 0;
4729 if (incoming) {
4730 Error *local_err = NULL;
4731 qemu_start_incoming_migration(incoming, &local_err);
4732 if (local_err) {
4733 error_reportf_err(local_err, "-incoming %s: ", incoming);
4734 exit(1);
4736 } else if (autostart) {
4737 vm_start();
4740 os_setup_post();
4742 main_loop();
4743 replay_disable_events();
4744 iothread_stop_all();
4746 bdrv_close_all();
4747 pause_all_vcpus();
4748 res_free();
4750 /* vhost-user must be cleaned up before chardevs. */
4751 net_cleanup();
4752 audio_cleanup();
4753 monitor_cleanup();
4754 qemu_chr_cleanup();
4756 return 0;