sdl2: Fix build problem
[qemu/ar7.git] / vl.c
blob4760eb73d0da791310926e6b213013ee297d6383
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 g_str_equal(type, "filter-replay")) {
2911 return false;
2914 /* Memory allocation by backends needs to be done
2915 * after configure_accelerator() (due to the tcg_enabled()
2916 * checks at memory_region_init_*()).
2918 * Also, allocation of large amounts of memory may delay
2919 * chardev initialization for too long, and trigger timeouts
2920 * on software that waits for a monitor socket to be created
2921 * (e.g. libvirt).
2923 if (g_str_has_prefix(type, "memory-backend-")) {
2924 return false;
2927 return true;
2932 * The remainder of object creation happens after the
2933 * creation of chardev, fsdev, net clients and device data types.
2935 static bool object_create_delayed(const char *type)
2937 return !object_create_initial(type);
2941 static void set_memory_options(uint64_t *ram_slots, ram_addr_t *maxram_size,
2942 MachineClass *mc)
2944 uint64_t sz;
2945 const char *mem_str;
2946 const char *maxmem_str, *slots_str;
2947 const ram_addr_t default_ram_size = mc->default_ram_size;
2948 QemuOpts *opts = qemu_find_opts_singleton("memory");
2949 Location loc;
2951 loc_push_none(&loc);
2952 qemu_opts_loc_restore(opts);
2954 sz = 0;
2955 mem_str = qemu_opt_get(opts, "size");
2956 if (mem_str) {
2957 if (!*mem_str) {
2958 error_report("missing 'size' option value");
2959 exit(EXIT_FAILURE);
2962 sz = qemu_opt_get_size(opts, "size", ram_size);
2964 /* Fix up legacy suffix-less format */
2965 if (g_ascii_isdigit(mem_str[strlen(mem_str) - 1])) {
2966 uint64_t overflow_check = sz;
2968 sz <<= 20;
2969 if ((sz >> 20) != overflow_check) {
2970 error_report("too large 'size' option value");
2971 exit(EXIT_FAILURE);
2976 /* backward compatibility behaviour for case "-m 0" */
2977 if (sz == 0) {
2978 sz = default_ram_size;
2981 sz = QEMU_ALIGN_UP(sz, 8192);
2982 ram_size = sz;
2983 if (ram_size != sz) {
2984 error_report("ram size too large");
2985 exit(EXIT_FAILURE);
2988 /* store value for the future use */
2989 qemu_opt_set_number(opts, "size", ram_size, &error_abort);
2990 *maxram_size = ram_size;
2992 maxmem_str = qemu_opt_get(opts, "maxmem");
2993 slots_str = qemu_opt_get(opts, "slots");
2994 if (maxmem_str && slots_str) {
2995 uint64_t slots;
2997 sz = qemu_opt_get_size(opts, "maxmem", 0);
2998 slots = qemu_opt_get_number(opts, "slots", 0);
2999 if (sz < ram_size) {
3000 error_report("invalid value of -m option maxmem: "
3001 "maximum memory size (0x%" PRIx64 ") must be at least "
3002 "the initial memory size (0x" RAM_ADDR_FMT ")",
3003 sz, ram_size);
3004 exit(EXIT_FAILURE);
3005 } else if (sz > ram_size) {
3006 if (!slots) {
3007 error_report("invalid value of -m option: maxmem was "
3008 "specified, but no hotplug slots were specified");
3009 exit(EXIT_FAILURE);
3011 } else if (slots) {
3012 error_report("invalid value of -m option maxmem: "
3013 "memory slots were specified but maximum memory size "
3014 "(0x%" PRIx64 ") is equal to the initial memory size "
3015 "(0x" RAM_ADDR_FMT ")", sz, ram_size);
3016 exit(EXIT_FAILURE);
3019 *maxram_size = sz;
3020 *ram_slots = slots;
3021 } else if ((!maxmem_str && slots_str) ||
3022 (maxmem_str && !slots_str)) {
3023 error_report("invalid -m option value: missing "
3024 "'%s' option", slots_str ? "maxmem" : "slots");
3025 exit(EXIT_FAILURE);
3028 loc_pop(&loc);
3031 static int global_init_func(void *opaque, QemuOpts *opts, Error **errp)
3033 GlobalProperty *g;
3035 g = g_malloc0(sizeof(*g));
3036 g->driver = qemu_opt_get(opts, "driver");
3037 g->property = qemu_opt_get(opts, "property");
3038 g->value = qemu_opt_get(opts, "value");
3039 g->user_provided = true;
3040 g->errp = &error_fatal;
3041 qdev_prop_register_global(g);
3042 return 0;
3045 int main(int argc, char **argv)
3047 int i;
3048 int snapshot, linux_boot;
3049 const char *initrd_filename;
3050 const char *kernel_filename, *kernel_cmdline;
3051 const char *boot_order = NULL;
3052 const char *boot_once = NULL;
3053 DisplayState *ds;
3054 int cyls, heads, secs, translation;
3055 QemuOpts *hda_opts = NULL, *opts, *machine_opts, *icount_opts = NULL;
3056 QemuOptsList *olist;
3057 int optind;
3058 const char *optarg;
3059 const char *loadvm = NULL;
3060 MachineClass *machine_class;
3061 const char *cpu_model;
3062 const char *vga_model = NULL;
3063 const char *qtest_chrdev = NULL;
3064 const char *qtest_log = NULL;
3065 const char *pid_file = NULL;
3066 const char *incoming = NULL;
3067 bool defconfig = true;
3068 bool userconfig = true;
3069 bool nographic = false;
3070 DisplayType display_type = DT_DEFAULT;
3071 int display_remote = 0;
3072 const char *log_mask = NULL;
3073 const char *log_file = NULL;
3074 char *trace_file = NULL;
3075 ram_addr_t maxram_size;
3076 uint64_t ram_slots = 0;
3077 FILE *vmstate_dump_file = NULL;
3078 Error *main_loop_err = NULL;
3079 Error *err = NULL;
3080 bool list_data_dirs = false;
3082 module_call_init(MODULE_INIT_TRACE);
3084 qemu_init_cpu_list();
3085 qemu_init_cpu_loop();
3086 qemu_mutex_lock_iothread();
3088 atexit(qemu_run_exit_notifiers);
3089 error_set_progname(argv[0]);
3090 qemu_init_exec_dir(argv[0]);
3092 module_call_init(MODULE_INIT_QOM);
3093 module_call_init(MODULE_INIT_QAPI);
3095 qemu_add_opts(&qemu_drive_opts);
3096 qemu_add_drive_opts(&qemu_legacy_drive_opts);
3097 qemu_add_drive_opts(&qemu_common_drive_opts);
3098 qemu_add_drive_opts(&qemu_drive_opts);
3099 qemu_add_drive_opts(&bdrv_runtime_opts);
3100 qemu_add_opts(&qemu_chardev_opts);
3101 qemu_add_opts(&qemu_device_opts);
3102 qemu_add_opts(&qemu_netdev_opts);
3103 qemu_add_opts(&qemu_net_opts);
3104 qemu_add_opts(&qemu_rtc_opts);
3105 qemu_add_opts(&qemu_global_opts);
3106 qemu_add_opts(&qemu_mon_opts);
3107 qemu_add_opts(&qemu_trace_opts);
3108 qemu_add_opts(&qemu_option_rom_opts);
3109 qemu_add_opts(&qemu_machine_opts);
3110 qemu_add_opts(&qemu_mem_opts);
3111 qemu_add_opts(&qemu_smp_opts);
3112 qemu_add_opts(&qemu_boot_opts);
3113 qemu_add_opts(&qemu_sandbox_opts);
3114 qemu_add_opts(&qemu_add_fd_opts);
3115 qemu_add_opts(&qemu_object_opts);
3116 qemu_add_opts(&qemu_tpmdev_opts);
3117 qemu_add_opts(&qemu_realtime_opts);
3118 qemu_add_opts(&qemu_msg_opts);
3119 qemu_add_opts(&qemu_name_opts);
3120 qemu_add_opts(&qemu_numa_opts);
3121 qemu_add_opts(&qemu_icount_opts);
3122 qemu_add_opts(&qemu_semihosting_config_opts);
3123 qemu_add_opts(&qemu_fw_cfg_opts);
3124 #ifdef CONFIG_LIBISCSI
3125 qemu_add_opts(&qemu_iscsi_opts);
3126 #endif
3127 module_call_init(MODULE_INIT_OPTS);
3129 runstate_init();
3131 if (qcrypto_init(&err) < 0) {
3132 error_reportf_err(err, "cannot initialize crypto: ");
3133 exit(1);
3135 rtc_clock = QEMU_CLOCK_HOST;
3137 QLIST_INIT (&vm_change_state_head);
3138 os_setup_early_signal_handling();
3140 cpu_model = NULL;
3141 snapshot = 0;
3142 cyls = heads = secs = 0;
3143 translation = BIOS_ATA_TRANSLATION_AUTO;
3145 nb_nics = 0;
3147 bdrv_init_with_whitelist();
3149 autostart = 1;
3151 /* first pass of option parsing */
3152 optind = 1;
3153 while (optind < argc) {
3154 if (argv[optind][0] != '-') {
3155 /* disk image */
3156 optind++;
3157 } else {
3158 const QEMUOption *popt;
3160 popt = lookup_opt(argc, argv, &optarg, &optind);
3161 switch (popt->index) {
3162 case QEMU_OPTION_nodefconfig:
3163 defconfig = false;
3164 break;
3165 case QEMU_OPTION_nouserconfig:
3166 userconfig = false;
3167 break;
3172 if (defconfig) {
3173 int ret;
3174 ret = qemu_read_default_config_files(userconfig);
3175 if (ret < 0) {
3176 exit(1);
3180 /* second pass of option parsing */
3181 optind = 1;
3182 for(;;) {
3183 if (optind >= argc)
3184 break;
3185 if (argv[optind][0] != '-') {
3186 hda_opts = drive_add(IF_DEFAULT, 0, argv[optind++], HD_OPTS);
3187 } else {
3188 const QEMUOption *popt;
3190 popt = lookup_opt(argc, argv, &optarg, &optind);
3191 if (!(popt->arch_mask & arch_type)) {
3192 error_report("Option not supported for this target");
3193 exit(1);
3195 switch(popt->index) {
3196 case QEMU_OPTION_no_kvm_irqchip: {
3197 olist = qemu_find_opts("machine");
3198 qemu_opts_parse_noisily(olist, "kernel_irqchip=off", false);
3199 break;
3201 case QEMU_OPTION_cpu:
3202 /* hw initialization will check this */
3203 cpu_model = optarg;
3204 break;
3205 case QEMU_OPTION_hda:
3207 char buf[256];
3208 if (cyls == 0)
3209 snprintf(buf, sizeof(buf), "%s", HD_OPTS);
3210 else
3211 snprintf(buf, sizeof(buf),
3212 "%s,cyls=%d,heads=%d,secs=%d%s",
3213 HD_OPTS , cyls, heads, secs,
3214 translation == BIOS_ATA_TRANSLATION_LBA ?
3215 ",trans=lba" :
3216 translation == BIOS_ATA_TRANSLATION_NONE ?
3217 ",trans=none" : "");
3218 drive_add(IF_DEFAULT, 0, optarg, buf);
3219 break;
3221 case QEMU_OPTION_hdb:
3222 case QEMU_OPTION_hdc:
3223 case QEMU_OPTION_hdd:
3224 drive_add(IF_DEFAULT, popt->index - QEMU_OPTION_hda, optarg,
3225 HD_OPTS);
3226 break;
3227 case QEMU_OPTION_drive:
3228 if (drive_def(optarg) == NULL) {
3229 exit(1);
3231 break;
3232 case QEMU_OPTION_set:
3233 if (qemu_set_option(optarg) != 0)
3234 exit(1);
3235 break;
3236 case QEMU_OPTION_global:
3237 if (qemu_global_option(optarg) != 0)
3238 exit(1);
3239 break;
3240 case QEMU_OPTION_mtdblock:
3241 drive_add(IF_MTD, -1, optarg, MTD_OPTS);
3242 break;
3243 case QEMU_OPTION_sd:
3244 drive_add(IF_SD, -1, optarg, SD_OPTS);
3245 break;
3246 case QEMU_OPTION_pflash:
3247 drive_add(IF_PFLASH, -1, optarg, PFLASH_OPTS);
3248 break;
3249 case QEMU_OPTION_snapshot:
3250 snapshot = 1;
3251 break;
3252 case QEMU_OPTION_hdachs:
3254 const char *p;
3255 p = optarg;
3256 cyls = strtol(p, (char **)&p, 0);
3257 if (cyls < 1 || cyls > 16383)
3258 goto chs_fail;
3259 if (*p != ',')
3260 goto chs_fail;
3261 p++;
3262 heads = strtol(p, (char **)&p, 0);
3263 if (heads < 1 || heads > 16)
3264 goto chs_fail;
3265 if (*p != ',')
3266 goto chs_fail;
3267 p++;
3268 secs = strtol(p, (char **)&p, 0);
3269 if (secs < 1 || secs > 63)
3270 goto chs_fail;
3271 if (*p == ',') {
3272 p++;
3273 if (!strcmp(p, "large")) {
3274 translation = BIOS_ATA_TRANSLATION_LARGE;
3275 } else if (!strcmp(p, "rechs")) {
3276 translation = BIOS_ATA_TRANSLATION_RECHS;
3277 } else if (!strcmp(p, "none")) {
3278 translation = BIOS_ATA_TRANSLATION_NONE;
3279 } else if (!strcmp(p, "lba")) {
3280 translation = BIOS_ATA_TRANSLATION_LBA;
3281 } else if (!strcmp(p, "auto")) {
3282 translation = BIOS_ATA_TRANSLATION_AUTO;
3283 } else {
3284 goto chs_fail;
3286 } else if (*p != '\0') {
3287 chs_fail:
3288 error_report("invalid physical CHS format");
3289 exit(1);
3291 if (hda_opts != NULL) {
3292 qemu_opt_set_number(hda_opts, "cyls", cyls,
3293 &error_abort);
3294 qemu_opt_set_number(hda_opts, "heads", heads,
3295 &error_abort);
3296 qemu_opt_set_number(hda_opts, "secs", secs,
3297 &error_abort);
3298 if (translation == BIOS_ATA_TRANSLATION_LARGE) {
3299 qemu_opt_set(hda_opts, "trans", "large",
3300 &error_abort);
3301 } else if (translation == BIOS_ATA_TRANSLATION_RECHS) {
3302 qemu_opt_set(hda_opts, "trans", "rechs",
3303 &error_abort);
3304 } else if (translation == BIOS_ATA_TRANSLATION_LBA) {
3305 qemu_opt_set(hda_opts, "trans", "lba",
3306 &error_abort);
3307 } else if (translation == BIOS_ATA_TRANSLATION_NONE) {
3308 qemu_opt_set(hda_opts, "trans", "none",
3309 &error_abort);
3313 break;
3314 case QEMU_OPTION_numa:
3315 opts = qemu_opts_parse_noisily(qemu_find_opts("numa"),
3316 optarg, true);
3317 if (!opts) {
3318 exit(1);
3320 break;
3321 case QEMU_OPTION_display:
3322 display_type = select_display(optarg);
3323 break;
3324 case QEMU_OPTION_nographic:
3325 olist = qemu_find_opts("machine");
3326 qemu_opts_parse_noisily(olist, "graphics=off", false);
3327 nographic = true;
3328 display_type = DT_NONE;
3329 break;
3330 case QEMU_OPTION_curses:
3331 #ifdef CONFIG_CURSES
3332 display_type = DT_CURSES;
3333 #else
3334 error_report("curses support is disabled");
3335 exit(1);
3336 #endif
3337 break;
3338 case QEMU_OPTION_portrait:
3339 graphic_rotate = 90;
3340 break;
3341 case QEMU_OPTION_rotate:
3342 graphic_rotate = strtol(optarg, (char **) &optarg, 10);
3343 if (graphic_rotate != 0 && graphic_rotate != 90 &&
3344 graphic_rotate != 180 && graphic_rotate != 270) {
3345 error_report("only 90, 180, 270 deg rotation is available");
3346 exit(1);
3348 break;
3349 case QEMU_OPTION_kernel:
3350 qemu_opts_set(qemu_find_opts("machine"), 0, "kernel", optarg,
3351 &error_abort);
3352 break;
3353 case QEMU_OPTION_initrd:
3354 qemu_opts_set(qemu_find_opts("machine"), 0, "initrd", optarg,
3355 &error_abort);
3356 break;
3357 case QEMU_OPTION_append:
3358 qemu_opts_set(qemu_find_opts("machine"), 0, "append", optarg,
3359 &error_abort);
3360 break;
3361 case QEMU_OPTION_dtb:
3362 qemu_opts_set(qemu_find_opts("machine"), 0, "dtb", optarg,
3363 &error_abort);
3364 break;
3365 case QEMU_OPTION_cdrom:
3366 drive_add(IF_DEFAULT, 2, optarg, CDROM_OPTS);
3367 break;
3368 case QEMU_OPTION_boot:
3369 opts = qemu_opts_parse_noisily(qemu_find_opts("boot-opts"),
3370 optarg, true);
3371 if (!opts) {
3372 exit(1);
3374 break;
3375 case QEMU_OPTION_fda:
3376 case QEMU_OPTION_fdb:
3377 drive_add(IF_FLOPPY, popt->index - QEMU_OPTION_fda,
3378 optarg, FD_OPTS);
3379 break;
3380 case QEMU_OPTION_no_fd_bootchk:
3381 fd_bootchk = 0;
3382 break;
3383 case QEMU_OPTION_netdev:
3384 default_net = 0;
3385 if (net_client_parse(qemu_find_opts("netdev"), optarg) == -1) {
3386 exit(1);
3388 break;
3389 case QEMU_OPTION_net:
3390 default_net = 0;
3391 if (net_client_parse(qemu_find_opts("net"), optarg) == -1) {
3392 exit(1);
3394 break;
3395 #ifdef CONFIG_LIBISCSI
3396 case QEMU_OPTION_iscsi:
3397 opts = qemu_opts_parse_noisily(qemu_find_opts("iscsi"),
3398 optarg, false);
3399 if (!opts) {
3400 exit(1);
3402 break;
3403 #endif
3404 #ifdef CONFIG_SLIRP
3405 case QEMU_OPTION_tftp:
3406 error_report("The -tftp option is deprecated. "
3407 "Please use '-netdev user,tftp=...' instead.");
3408 legacy_tftp_prefix = optarg;
3409 break;
3410 case QEMU_OPTION_bootp:
3411 error_report("The -bootp option is deprecated. "
3412 "Please use '-netdev user,bootfile=...' instead.");
3413 legacy_bootp_filename = optarg;
3414 break;
3415 case QEMU_OPTION_redir:
3416 error_report("The -redir option is deprecated. "
3417 "Please use '-netdev user,hostfwd=...' instead.");
3418 if (net_slirp_redir(optarg) < 0)
3419 exit(1);
3420 break;
3421 #endif
3422 case QEMU_OPTION_bt:
3423 add_device_config(DEV_BT, optarg);
3424 break;
3425 case QEMU_OPTION_audio_help:
3426 AUD_help ();
3427 exit (0);
3428 break;
3429 case QEMU_OPTION_soundhw:
3430 select_soundhw (optarg);
3431 break;
3432 case QEMU_OPTION_h:
3433 help(0);
3434 break;
3435 case QEMU_OPTION_version:
3436 version();
3437 exit(0);
3438 break;
3439 case QEMU_OPTION_m:
3440 opts = qemu_opts_parse_noisily(qemu_find_opts("memory"),
3441 optarg, true);
3442 if (!opts) {
3443 exit(EXIT_FAILURE);
3445 break;
3446 #ifdef CONFIG_TPM
3447 case QEMU_OPTION_tpmdev:
3448 if (tpm_config_parse(qemu_find_opts("tpmdev"), optarg) < 0) {
3449 exit(1);
3451 break;
3452 #endif
3453 case QEMU_OPTION_mempath:
3454 mem_path = optarg;
3455 break;
3456 case QEMU_OPTION_mem_prealloc:
3457 mem_prealloc = 1;
3458 break;
3459 case QEMU_OPTION_d:
3460 log_mask = optarg;
3461 break;
3462 case QEMU_OPTION_D:
3463 log_file = optarg;
3464 break;
3465 case QEMU_OPTION_DFILTER:
3466 qemu_set_dfilter_ranges(optarg, &error_fatal);
3467 break;
3468 case QEMU_OPTION_s:
3469 add_device_config(DEV_GDB, "tcp::" DEFAULT_GDBSTUB_PORT);
3470 break;
3471 case QEMU_OPTION_gdb:
3472 add_device_config(DEV_GDB, optarg);
3473 break;
3474 case QEMU_OPTION_L:
3475 if (is_help_option(optarg)) {
3476 list_data_dirs = true;
3477 } else if (data_dir_idx < ARRAY_SIZE(data_dir)) {
3478 data_dir[data_dir_idx++] = optarg;
3480 break;
3481 case QEMU_OPTION_bios:
3482 qemu_opts_set(qemu_find_opts("machine"), 0, "firmware", optarg,
3483 &error_abort);
3484 break;
3485 case QEMU_OPTION_singlestep:
3486 singlestep = 1;
3487 break;
3488 case QEMU_OPTION_S:
3489 autostart = 0;
3490 break;
3491 case QEMU_OPTION_k:
3492 keyboard_layout = optarg;
3493 break;
3494 case QEMU_OPTION_localtime:
3495 rtc_utc = 0;
3496 break;
3497 case QEMU_OPTION_vga:
3498 vga_model = optarg;
3499 default_vga = 0;
3500 break;
3501 case QEMU_OPTION_g:
3503 const char *p;
3504 int w, h, depth;
3505 p = optarg;
3506 w = strtol(p, (char **)&p, 10);
3507 if (w <= 0) {
3508 graphic_error:
3509 error_report("invalid resolution or depth");
3510 exit(1);
3512 if (*p != 'x')
3513 goto graphic_error;
3514 p++;
3515 h = strtol(p, (char **)&p, 10);
3516 if (h <= 0)
3517 goto graphic_error;
3518 if (*p == 'x') {
3519 p++;
3520 depth = strtol(p, (char **)&p, 10);
3521 if (depth != 8 && depth != 15 && depth != 16 &&
3522 depth != 24 && depth != 32)
3523 goto graphic_error;
3524 } else if (*p == '\0') {
3525 depth = graphic_depth;
3526 } else {
3527 goto graphic_error;
3530 graphic_width = w;
3531 graphic_height = h;
3532 graphic_depth = depth;
3534 break;
3535 case QEMU_OPTION_echr:
3537 char *r;
3538 term_escape_char = strtol(optarg, &r, 0);
3539 if (r == optarg)
3540 printf("Bad argument to echr\n");
3541 break;
3543 case QEMU_OPTION_monitor:
3544 default_monitor = 0;
3545 if (strncmp(optarg, "none", 4)) {
3546 monitor_parse(optarg, "readline", false);
3548 break;
3549 case QEMU_OPTION_qmp:
3550 monitor_parse(optarg, "control", false);
3551 default_monitor = 0;
3552 break;
3553 case QEMU_OPTION_qmp_pretty:
3554 monitor_parse(optarg, "control", true);
3555 default_monitor = 0;
3556 break;
3557 case QEMU_OPTION_mon:
3558 opts = qemu_opts_parse_noisily(qemu_find_opts("mon"), optarg,
3559 true);
3560 if (!opts) {
3561 exit(1);
3563 default_monitor = 0;
3564 break;
3565 case QEMU_OPTION_chardev:
3566 opts = qemu_opts_parse_noisily(qemu_find_opts("chardev"),
3567 optarg, true);
3568 if (!opts) {
3569 exit(1);
3571 break;
3572 case QEMU_OPTION_fsdev:
3573 olist = qemu_find_opts("fsdev");
3574 if (!olist) {
3575 error_report("fsdev support is disabled");
3576 exit(1);
3578 opts = qemu_opts_parse_noisily(olist, optarg, true);
3579 if (!opts) {
3580 exit(1);
3582 break;
3583 case QEMU_OPTION_virtfs: {
3584 QemuOpts *fsdev;
3585 QemuOpts *device;
3586 const char *writeout, *sock_fd, *socket;
3588 olist = qemu_find_opts("virtfs");
3589 if (!olist) {
3590 error_report("virtfs support is disabled");
3591 exit(1);
3593 opts = qemu_opts_parse_noisily(olist, optarg, true);
3594 if (!opts) {
3595 exit(1);
3598 if (qemu_opt_get(opts, "fsdriver") == NULL ||
3599 qemu_opt_get(opts, "mount_tag") == NULL) {
3600 error_report("Usage: -virtfs fsdriver,mount_tag=tag");
3601 exit(1);
3603 fsdev = qemu_opts_create(qemu_find_opts("fsdev"),
3604 qemu_opt_get(opts, "mount_tag"),
3605 1, NULL);
3606 if (!fsdev) {
3607 error_report("duplicate fsdev id: %s",
3608 qemu_opt_get(opts, "mount_tag"));
3609 exit(1);
3612 writeout = qemu_opt_get(opts, "writeout");
3613 if (writeout) {
3614 #ifdef CONFIG_SYNC_FILE_RANGE
3615 qemu_opt_set(fsdev, "writeout", writeout, &error_abort);
3616 #else
3617 error_report("writeout=immediate not supported "
3618 "on this platform");
3619 exit(1);
3620 #endif
3622 qemu_opt_set(fsdev, "fsdriver",
3623 qemu_opt_get(opts, "fsdriver"), &error_abort);
3624 qemu_opt_set(fsdev, "path", qemu_opt_get(opts, "path"),
3625 &error_abort);
3626 qemu_opt_set(fsdev, "security_model",
3627 qemu_opt_get(opts, "security_model"),
3628 &error_abort);
3629 socket = qemu_opt_get(opts, "socket");
3630 if (socket) {
3631 qemu_opt_set(fsdev, "socket", socket, &error_abort);
3633 sock_fd = qemu_opt_get(opts, "sock_fd");
3634 if (sock_fd) {
3635 qemu_opt_set(fsdev, "sock_fd", sock_fd, &error_abort);
3638 qemu_opt_set_bool(fsdev, "readonly",
3639 qemu_opt_get_bool(opts, "readonly", 0),
3640 &error_abort);
3641 device = qemu_opts_create(qemu_find_opts("device"), NULL, 0,
3642 &error_abort);
3643 qemu_opt_set(device, "driver", "virtio-9p-pci", &error_abort);
3644 qemu_opt_set(device, "fsdev",
3645 qemu_opt_get(opts, "mount_tag"), &error_abort);
3646 qemu_opt_set(device, "mount_tag",
3647 qemu_opt_get(opts, "mount_tag"), &error_abort);
3648 break;
3650 case QEMU_OPTION_virtfs_synth: {
3651 QemuOpts *fsdev;
3652 QemuOpts *device;
3654 fsdev = qemu_opts_create(qemu_find_opts("fsdev"), "v_synth",
3655 1, NULL);
3656 if (!fsdev) {
3657 error_report("duplicate option: %s", "virtfs_synth");
3658 exit(1);
3660 qemu_opt_set(fsdev, "fsdriver", "synth", &error_abort);
3662 device = qemu_opts_create(qemu_find_opts("device"), NULL, 0,
3663 &error_abort);
3664 qemu_opt_set(device, "driver", "virtio-9p-pci", &error_abort);
3665 qemu_opt_set(device, "fsdev", "v_synth", &error_abort);
3666 qemu_opt_set(device, "mount_tag", "v_synth", &error_abort);
3667 break;
3669 case QEMU_OPTION_serial:
3670 add_device_config(DEV_SERIAL, optarg);
3671 default_serial = 0;
3672 if (strncmp(optarg, "mon:", 4) == 0) {
3673 default_monitor = 0;
3675 break;
3676 case QEMU_OPTION_watchdog:
3677 if (watchdog) {
3678 error_report("only one watchdog option may be given");
3679 return 1;
3681 watchdog = optarg;
3682 break;
3683 case QEMU_OPTION_watchdog_action:
3684 if (select_watchdog_action(optarg) == -1) {
3685 error_report("unknown -watchdog-action parameter");
3686 exit(1);
3688 break;
3689 case QEMU_OPTION_virtiocon:
3690 add_device_config(DEV_VIRTCON, optarg);
3691 default_virtcon = 0;
3692 if (strncmp(optarg, "mon:", 4) == 0) {
3693 default_monitor = 0;
3695 break;
3696 case QEMU_OPTION_parallel:
3697 add_device_config(DEV_PARALLEL, optarg);
3698 default_parallel = 0;
3699 if (strncmp(optarg, "mon:", 4) == 0) {
3700 default_monitor = 0;
3702 break;
3703 case QEMU_OPTION_debugcon:
3704 add_device_config(DEV_DEBUGCON, optarg);
3705 break;
3706 case QEMU_OPTION_loadvm:
3707 loadvm = optarg;
3708 break;
3709 case QEMU_OPTION_full_screen:
3710 full_screen = 1;
3711 break;
3712 case QEMU_OPTION_no_frame:
3713 no_frame = 1;
3714 break;
3715 case QEMU_OPTION_alt_grab:
3716 alt_grab = 1;
3717 break;
3718 case QEMU_OPTION_ctrl_grab:
3719 ctrl_grab = 1;
3720 break;
3721 case QEMU_OPTION_no_quit:
3722 no_quit = 1;
3723 break;
3724 case QEMU_OPTION_sdl:
3725 #ifdef CONFIG_SDL
3726 display_type = DT_SDL;
3727 break;
3728 #else
3729 error_report("SDL support is disabled");
3730 exit(1);
3731 #endif
3732 case QEMU_OPTION_pidfile:
3733 pid_file = optarg;
3734 break;
3735 case QEMU_OPTION_win2k_hack:
3736 win2k_install_hack = 1;
3737 break;
3738 case QEMU_OPTION_rtc_td_hack: {
3739 static GlobalProperty slew_lost_ticks = {
3740 .driver = "mc146818rtc",
3741 .property = "lost_tick_policy",
3742 .value = "slew",
3745 qdev_prop_register_global(&slew_lost_ticks);
3746 break;
3748 case QEMU_OPTION_acpitable:
3749 opts = qemu_opts_parse_noisily(qemu_find_opts("acpi"),
3750 optarg, true);
3751 if (!opts) {
3752 exit(1);
3754 do_acpitable_option(opts);
3755 break;
3756 case QEMU_OPTION_smbios:
3757 opts = qemu_opts_parse_noisily(qemu_find_opts("smbios"),
3758 optarg, false);
3759 if (!opts) {
3760 exit(1);
3762 do_smbios_option(opts);
3763 break;
3764 case QEMU_OPTION_fwcfg:
3765 opts = qemu_opts_parse_noisily(qemu_find_opts("fw_cfg"),
3766 optarg, true);
3767 if (opts == NULL) {
3768 exit(1);
3770 break;
3771 case QEMU_OPTION_enable_kvm:
3772 olist = qemu_find_opts("machine");
3773 qemu_opts_parse_noisily(olist, "accel=kvm", false);
3774 break;
3775 case QEMU_OPTION_enable_hax:
3776 olist = qemu_find_opts("machine");
3777 qemu_opts_parse_noisily(olist, "accel=hax", false);
3778 break;
3779 case QEMU_OPTION_M:
3780 case QEMU_OPTION_machine:
3781 olist = qemu_find_opts("machine");
3782 opts = qemu_opts_parse_noisily(olist, optarg, true);
3783 if (!opts) {
3784 exit(1);
3786 break;
3787 case QEMU_OPTION_no_kvm:
3788 olist = qemu_find_opts("machine");
3789 qemu_opts_parse_noisily(olist, "accel=tcg", false);
3790 break;
3791 case QEMU_OPTION_no_kvm_pit: {
3792 error_report("warning: ignoring deprecated option");
3793 break;
3795 case QEMU_OPTION_no_kvm_pit_reinjection: {
3796 static GlobalProperty kvm_pit_lost_tick_policy = {
3797 .driver = "kvm-pit",
3798 .property = "lost_tick_policy",
3799 .value = "discard",
3802 error_report("warning: deprecated, replaced by "
3803 "-global kvm-pit.lost_tick_policy=discard");
3804 qdev_prop_register_global(&kvm_pit_lost_tick_policy);
3805 break;
3807 case QEMU_OPTION_usb:
3808 olist = qemu_find_opts("machine");
3809 qemu_opts_parse_noisily(olist, "usb=on", false);
3810 break;
3811 case QEMU_OPTION_usbdevice:
3812 olist = qemu_find_opts("machine");
3813 qemu_opts_parse_noisily(olist, "usb=on", false);
3814 add_device_config(DEV_USB, optarg);
3815 break;
3816 case QEMU_OPTION_device:
3817 if (!qemu_opts_parse_noisily(qemu_find_opts("device"),
3818 optarg, true)) {
3819 exit(1);
3821 break;
3822 case QEMU_OPTION_smp:
3823 if (!qemu_opts_parse_noisily(qemu_find_opts("smp-opts"),
3824 optarg, true)) {
3825 exit(1);
3827 break;
3828 case QEMU_OPTION_vnc:
3829 vnc_parse(optarg, &error_fatal);
3830 break;
3831 case QEMU_OPTION_no_acpi:
3832 acpi_enabled = 0;
3833 break;
3834 case QEMU_OPTION_no_hpet:
3835 no_hpet = 1;
3836 break;
3837 case QEMU_OPTION_balloon:
3838 if (balloon_parse(optarg) < 0) {
3839 error_report("unknown -balloon argument %s", optarg);
3840 exit(1);
3842 break;
3843 case QEMU_OPTION_no_reboot:
3844 no_reboot = 1;
3845 break;
3846 case QEMU_OPTION_no_shutdown:
3847 no_shutdown = 1;
3848 break;
3849 case QEMU_OPTION_show_cursor:
3850 cursor_hide = 0;
3851 break;
3852 case QEMU_OPTION_uuid:
3853 if (qemu_uuid_parse(optarg, &qemu_uuid) < 0) {
3854 error_report("failed to parse UUID string: wrong format");
3855 exit(1);
3857 qemu_uuid_set = true;
3858 break;
3859 case QEMU_OPTION_option_rom:
3860 if (nb_option_roms >= MAX_OPTION_ROMS) {
3861 error_report("too many option ROMs");
3862 exit(1);
3864 opts = qemu_opts_parse_noisily(qemu_find_opts("option-rom"),
3865 optarg, true);
3866 if (!opts) {
3867 exit(1);
3869 option_rom[nb_option_roms].name = qemu_opt_get(opts, "romfile");
3870 option_rom[nb_option_roms].bootindex =
3871 qemu_opt_get_number(opts, "bootindex", -1);
3872 if (!option_rom[nb_option_roms].name) {
3873 error_report("Option ROM file is not specified");
3874 exit(1);
3876 nb_option_roms++;
3877 break;
3878 case QEMU_OPTION_semihosting:
3879 semihosting.enabled = true;
3880 semihosting.target = SEMIHOSTING_TARGET_AUTO;
3881 break;
3882 case QEMU_OPTION_semihosting_config:
3883 semihosting.enabled = true;
3884 opts = qemu_opts_parse_noisily(qemu_find_opts("semihosting-config"),
3885 optarg, false);
3886 if (opts != NULL) {
3887 semihosting.enabled = qemu_opt_get_bool(opts, "enable",
3888 true);
3889 const char *target = qemu_opt_get(opts, "target");
3890 if (target != NULL) {
3891 if (strcmp("native", target) == 0) {
3892 semihosting.target = SEMIHOSTING_TARGET_NATIVE;
3893 } else if (strcmp("gdb", target) == 0) {
3894 semihosting.target = SEMIHOSTING_TARGET_GDB;
3895 } else if (strcmp("auto", target) == 0) {
3896 semihosting.target = SEMIHOSTING_TARGET_AUTO;
3897 } else {
3898 error_report("unsupported semihosting-config %s",
3899 optarg);
3900 exit(1);
3902 } else {
3903 semihosting.target = SEMIHOSTING_TARGET_AUTO;
3905 /* Set semihosting argument count and vector */
3906 qemu_opt_foreach(opts, add_semihosting_arg,
3907 &semihosting, NULL);
3908 } else {
3909 error_report("unsupported semihosting-config %s", optarg);
3910 exit(1);
3912 break;
3913 case QEMU_OPTION_tdf:
3914 error_report("warning: ignoring deprecated option");
3915 break;
3916 case QEMU_OPTION_name:
3917 opts = qemu_opts_parse_noisily(qemu_find_opts("name"),
3918 optarg, true);
3919 if (!opts) {
3920 exit(1);
3922 break;
3923 case QEMU_OPTION_prom_env:
3924 if (nb_prom_envs >= MAX_PROM_ENVS) {
3925 error_report("too many prom variables");
3926 exit(1);
3928 prom_envs[nb_prom_envs] = optarg;
3929 nb_prom_envs++;
3930 break;
3931 case QEMU_OPTION_old_param:
3932 old_param = 1;
3933 break;
3934 case QEMU_OPTION_clock:
3935 /* Clock options no longer exist. Keep this option for
3936 * backward compatibility.
3938 break;
3939 case QEMU_OPTION_startdate:
3940 configure_rtc_date_offset(optarg, 1);
3941 break;
3942 case QEMU_OPTION_rtc:
3943 opts = qemu_opts_parse_noisily(qemu_find_opts("rtc"), optarg,
3944 false);
3945 if (!opts) {
3946 exit(1);
3948 configure_rtc(opts);
3949 break;
3950 case QEMU_OPTION_tb_size:
3951 tcg_tb_size = strtol(optarg, NULL, 0);
3952 if (tcg_tb_size < 0) {
3953 tcg_tb_size = 0;
3955 break;
3956 case QEMU_OPTION_icount:
3957 icount_opts = qemu_opts_parse_noisily(qemu_find_opts("icount"),
3958 optarg, true);
3959 if (!icount_opts) {
3960 exit(1);
3962 break;
3963 case QEMU_OPTION_incoming:
3964 if (!incoming) {
3965 runstate_set(RUN_STATE_INMIGRATE);
3967 incoming = optarg;
3968 break;
3969 case QEMU_OPTION_nodefaults:
3970 has_defaults = 0;
3971 break;
3972 case QEMU_OPTION_xen_domid:
3973 if (!(xen_available())) {
3974 error_report("Option not supported for this target");
3975 exit(1);
3977 xen_domid = atoi(optarg);
3978 break;
3979 case QEMU_OPTION_xen_create:
3980 if (!(xen_available())) {
3981 error_report("Option not supported for this target");
3982 exit(1);
3984 xen_mode = XEN_CREATE;
3985 break;
3986 case QEMU_OPTION_xen_attach:
3987 if (!(xen_available())) {
3988 error_report("Option not supported for this target");
3989 exit(1);
3991 xen_mode = XEN_ATTACH;
3992 break;
3993 case QEMU_OPTION_trace:
3994 g_free(trace_file);
3995 trace_file = trace_opt_parse(optarg);
3996 break;
3997 case QEMU_OPTION_trace_unassigned:
3998 trace_unassigned = true;
3999 break;
4000 case QEMU_OPTION_readconfig:
4002 int ret = qemu_read_config_file(optarg);
4003 if (ret < 0) {
4004 error_report("read config %s: %s", optarg,
4005 strerror(-ret));
4006 exit(1);
4008 break;
4010 case QEMU_OPTION_spice:
4011 olist = qemu_find_opts("spice");
4012 if (!olist) {
4013 error_report("spice support is disabled");
4014 exit(1);
4016 opts = qemu_opts_parse_noisily(olist, optarg, false);
4017 if (!opts) {
4018 exit(1);
4020 display_remote++;
4021 break;
4022 case QEMU_OPTION_writeconfig:
4024 FILE *fp;
4025 if (strcmp(optarg, "-") == 0) {
4026 fp = stdout;
4027 } else {
4028 fp = fopen(optarg, "w");
4029 if (fp == NULL) {
4030 error_report("open %s: %s", optarg,
4031 strerror(errno));
4032 exit(1);
4035 qemu_config_write(fp);
4036 if (fp != stdout) {
4037 fclose(fp);
4039 break;
4041 case QEMU_OPTION_qtest:
4042 qtest_chrdev = optarg;
4043 break;
4044 case QEMU_OPTION_qtest_log:
4045 qtest_log = optarg;
4046 break;
4047 case QEMU_OPTION_sandbox:
4048 opts = qemu_opts_parse_noisily(qemu_find_opts("sandbox"),
4049 optarg, true);
4050 if (!opts) {
4051 exit(1);
4053 break;
4054 case QEMU_OPTION_add_fd:
4055 #ifndef _WIN32
4056 opts = qemu_opts_parse_noisily(qemu_find_opts("add-fd"),
4057 optarg, false);
4058 if (!opts) {
4059 exit(1);
4061 #else
4062 error_report("File descriptor passing is disabled on this "
4063 "platform");
4064 exit(1);
4065 #endif
4066 break;
4067 case QEMU_OPTION_object:
4068 opts = qemu_opts_parse_noisily(qemu_find_opts("object"),
4069 optarg, true);
4070 if (!opts) {
4071 exit(1);
4073 break;
4074 case QEMU_OPTION_realtime:
4075 opts = qemu_opts_parse_noisily(qemu_find_opts("realtime"),
4076 optarg, false);
4077 if (!opts) {
4078 exit(1);
4080 enable_mlock = qemu_opt_get_bool(opts, "mlock", true);
4081 break;
4082 case QEMU_OPTION_msg:
4083 opts = qemu_opts_parse_noisily(qemu_find_opts("msg"), optarg,
4084 false);
4085 if (!opts) {
4086 exit(1);
4088 configure_msg(opts);
4089 break;
4090 case QEMU_OPTION_dump_vmstate:
4091 if (vmstate_dump_file) {
4092 error_report("only one '-dump-vmstate' "
4093 "option may be given");
4094 exit(1);
4096 vmstate_dump_file = fopen(optarg, "w");
4097 if (vmstate_dump_file == NULL) {
4098 error_report("open %s: %s", optarg, strerror(errno));
4099 exit(1);
4101 break;
4102 default:
4103 os_parse_cmd_args(popt->index, optarg);
4108 * Clear error location left behind by the loop.
4109 * Best done right after the loop. Do not insert code here!
4111 loc_set_none();
4113 replay_configure(icount_opts);
4115 machine_class = select_machine();
4117 set_memory_options(&ram_slots, &maxram_size, machine_class);
4119 os_daemonize();
4121 if (pid_file && qemu_create_pidfile(pid_file) != 0) {
4122 error_report("could not acquire pid file: %s", strerror(errno));
4123 exit(1);
4126 if (qemu_init_main_loop(&main_loop_err)) {
4127 error_report_err(main_loop_err);
4128 exit(1);
4131 if (qemu_opts_foreach(qemu_find_opts("sandbox"),
4132 parse_sandbox, NULL, NULL)) {
4133 exit(1);
4136 if (qemu_opts_foreach(qemu_find_opts("name"),
4137 parse_name, NULL, NULL)) {
4138 exit(1);
4141 #ifndef _WIN32
4142 if (qemu_opts_foreach(qemu_find_opts("add-fd"),
4143 parse_add_fd, NULL, NULL)) {
4144 exit(1);
4147 if (qemu_opts_foreach(qemu_find_opts("add-fd"),
4148 cleanup_add_fd, NULL, NULL)) {
4149 exit(1);
4151 #endif
4153 current_machine = MACHINE(object_new(object_class_get_name(
4154 OBJECT_CLASS(machine_class))));
4155 if (machine_help_func(qemu_get_machine_opts(), current_machine)) {
4156 exit(0);
4158 object_property_add_child(object_get_root(), "machine",
4159 OBJECT(current_machine), &error_abort);
4161 if (machine_class->minimum_page_bits) {
4162 if (!set_preferred_target_page_bits(machine_class->minimum_page_bits)) {
4163 /* This would be a board error: specifying a minimum smaller than
4164 * a target's compile-time fixed setting.
4166 g_assert_not_reached();
4170 cpu_exec_init_all();
4172 if (machine_class->hw_version) {
4173 qemu_set_hw_version(machine_class->hw_version);
4176 if (cpu_model && is_help_option(cpu_model)) {
4177 list_cpus(stdout, &fprintf, cpu_model);
4178 exit(0);
4181 if (!trace_init_backends()) {
4182 exit(1);
4184 trace_init_file(trace_file);
4186 /* Open the logfile at this point and set the log mask if necessary.
4188 if (log_file) {
4189 qemu_set_log_filename(log_file, &error_fatal);
4192 if (log_mask) {
4193 int mask;
4194 mask = qemu_str_to_log_mask(log_mask);
4195 if (!mask) {
4196 qemu_print_log_usage(stdout);
4197 exit(1);
4199 qemu_set_log(mask);
4200 } else {
4201 qemu_set_log(0);
4204 /* If no data_dir is specified then try to find it relative to the
4205 executable path. */
4206 if (data_dir_idx < ARRAY_SIZE(data_dir)) {
4207 data_dir[data_dir_idx] = os_find_datadir();
4208 if (data_dir[data_dir_idx] != NULL) {
4209 data_dir_idx++;
4212 /* If all else fails use the install path specified when building. */
4213 if (data_dir_idx < ARRAY_SIZE(data_dir)) {
4214 data_dir[data_dir_idx++] = CONFIG_QEMU_DATADIR;
4217 /* -L help lists the data directories and exits. */
4218 if (list_data_dirs) {
4219 for (i = 0; i < data_dir_idx; i++) {
4220 printf("%s\n", data_dir[i]);
4222 exit(0);
4225 smp_parse(qemu_opts_find(qemu_find_opts("smp-opts"), NULL));
4227 machine_class->max_cpus = machine_class->max_cpus ?: 1; /* Default to UP */
4228 if (max_cpus > machine_class->max_cpus) {
4229 error_report("Number of SMP CPUs requested (%d) exceeds max CPUs "
4230 "supported by machine '%s' (%d)", max_cpus,
4231 machine_class->name, machine_class->max_cpus);
4232 exit(1);
4236 * Get the default machine options from the machine if it is not already
4237 * specified either by the configuration file or by the command line.
4239 if (machine_class->default_machine_opts) {
4240 qemu_opts_set_defaults(qemu_find_opts("machine"),
4241 machine_class->default_machine_opts, 0);
4244 qemu_opts_foreach(qemu_find_opts("device"),
4245 default_driver_check, NULL, NULL);
4246 qemu_opts_foreach(qemu_find_opts("global"),
4247 default_driver_check, NULL, NULL);
4249 if (!vga_model && !default_vga) {
4250 vga_interface_type = VGA_DEVICE;
4252 if (!has_defaults || machine_class->no_serial) {
4253 default_serial = 0;
4255 if (!has_defaults || machine_class->no_parallel) {
4256 default_parallel = 0;
4258 if (!has_defaults || !machine_class->use_virtcon) {
4259 default_virtcon = 0;
4261 if (!has_defaults || !machine_class->use_sclp) {
4262 default_sclp = 0;
4264 if (!has_defaults || machine_class->no_floppy) {
4265 default_floppy = 0;
4267 if (!has_defaults || machine_class->no_cdrom) {
4268 default_cdrom = 0;
4270 if (!has_defaults || machine_class->no_sdcard) {
4271 default_sdcard = 0;
4273 if (!has_defaults) {
4274 default_monitor = 0;
4275 default_net = 0;
4276 default_vga = 0;
4279 if (is_daemonized()) {
4280 /* According to documentation and historically, -nographic redirects
4281 * serial port, parallel port and monitor to stdio, which does not work
4282 * with -daemonize. We can redirect these to null instead, but since
4283 * -nographic is legacy, let's just error out.
4284 * We disallow -nographic only if all other ports are not redirected
4285 * explicitly, to not break existing legacy setups which uses
4286 * -nographic _and_ redirects all ports explicitly - this is valid
4287 * usage, -nographic is just a no-op in this case.
4289 if (nographic
4290 && (default_parallel || default_serial
4291 || default_monitor || default_virtcon)) {
4292 error_report("-nographic cannot be used with -daemonize");
4293 exit(1);
4295 #ifdef CONFIG_CURSES
4296 if (display_type == DT_CURSES) {
4297 error_report("curses display cannot be used with -daemonize");
4298 exit(1);
4300 #endif
4303 if (nographic) {
4304 if (default_parallel)
4305 add_device_config(DEV_PARALLEL, "null");
4306 if (default_serial && default_monitor) {
4307 add_device_config(DEV_SERIAL, "mon:stdio");
4308 } else if (default_virtcon && default_monitor) {
4309 add_device_config(DEV_VIRTCON, "mon:stdio");
4310 } else if (default_sclp && default_monitor) {
4311 add_device_config(DEV_SCLP, "mon:stdio");
4312 } else {
4313 if (default_serial)
4314 add_device_config(DEV_SERIAL, "stdio");
4315 if (default_virtcon)
4316 add_device_config(DEV_VIRTCON, "stdio");
4317 if (default_sclp) {
4318 add_device_config(DEV_SCLP, "stdio");
4320 if (default_monitor)
4321 monitor_parse("stdio", "readline", false);
4323 } else {
4324 if (default_serial)
4325 add_device_config(DEV_SERIAL, "vc:80Cx24C");
4326 if (default_parallel)
4327 add_device_config(DEV_PARALLEL, "vc:80Cx24C");
4328 if (default_monitor)
4329 monitor_parse("vc:80Cx24C", "readline", false);
4330 if (default_virtcon)
4331 add_device_config(DEV_VIRTCON, "vc:80Cx24C");
4332 if (default_sclp) {
4333 add_device_config(DEV_SCLP, "vc:80Cx24C");
4337 #if defined(CONFIG_VNC)
4338 if (!QTAILQ_EMPTY(&(qemu_find_opts("vnc")->head))) {
4339 display_remote++;
4341 #endif
4342 if (display_type == DT_DEFAULT && !display_remote) {
4343 #if defined(CONFIG_GTK)
4344 display_type = DT_GTK;
4345 #elif defined(CONFIG_SDL)
4346 display_type = DT_SDL;
4347 #elif defined(CONFIG_COCOA)
4348 display_type = DT_COCOA;
4349 #elif defined(CONFIG_VNC)
4350 vnc_parse("localhost:0,to=99,id=default", &error_abort);
4351 #else
4352 display_type = DT_NONE;
4353 #endif
4356 if ((no_frame || alt_grab || ctrl_grab) && display_type != DT_SDL) {
4357 error_report("-no-frame, -alt-grab and -ctrl-grab are only valid "
4358 "for SDL, ignoring option");
4360 if (no_quit && (display_type != DT_GTK && display_type != DT_SDL)) {
4361 error_report("-no-quit is only valid for GTK and SDL, "
4362 "ignoring option");
4365 if (display_type == DT_GTK) {
4366 early_gtk_display_init(request_opengl);
4369 if (display_type == DT_SDL) {
4370 sdl_display_early_init(request_opengl);
4373 if (request_opengl == 1 && display_opengl == 0) {
4374 #if defined(CONFIG_OPENGL)
4375 error_report("OpenGL is not supported by the display");
4376 #else
4377 error_report("OpenGL support is disabled");
4378 #endif
4379 exit(1);
4382 page_size_init();
4383 socket_init();
4385 if (qemu_opts_foreach(qemu_find_opts("object"),
4386 user_creatable_add_opts_foreach,
4387 object_create_initial, NULL)) {
4388 exit(1);
4391 if (qemu_opts_foreach(qemu_find_opts("chardev"),
4392 chardev_init_func, NULL, NULL)) {
4393 exit(1);
4396 #ifdef CONFIG_VIRTFS
4397 if (qemu_opts_foreach(qemu_find_opts("fsdev"),
4398 fsdev_init_func, NULL, NULL)) {
4399 exit(1);
4401 #endif
4403 if (qemu_opts_foreach(qemu_find_opts("device"),
4404 device_help_func, NULL, NULL)) {
4405 exit(0);
4408 machine_opts = qemu_get_machine_opts();
4409 if (qemu_opt_foreach(machine_opts, machine_set_property, current_machine,
4410 NULL)) {
4411 object_unref(OBJECT(current_machine));
4412 exit(1);
4415 configure_accelerator(current_machine);
4417 if (qtest_chrdev) {
4418 qtest_init(qtest_chrdev, qtest_log, &error_fatal);
4421 machine_opts = qemu_get_machine_opts();
4422 kernel_filename = qemu_opt_get(machine_opts, "kernel");
4423 initrd_filename = qemu_opt_get(machine_opts, "initrd");
4424 kernel_cmdline = qemu_opt_get(machine_opts, "append");
4425 bios_name = qemu_opt_get(machine_opts, "firmware");
4427 opts = qemu_opts_find(qemu_find_opts("boot-opts"), NULL);
4428 if (opts) {
4429 boot_order = qemu_opt_get(opts, "order");
4430 if (boot_order) {
4431 validate_bootdevices(boot_order, &error_fatal);
4434 boot_once = qemu_opt_get(opts, "once");
4435 if (boot_once) {
4436 validate_bootdevices(boot_once, &error_fatal);
4439 boot_menu = qemu_opt_get_bool(opts, "menu", boot_menu);
4440 boot_strict = qemu_opt_get_bool(opts, "strict", false);
4443 if (!boot_order) {
4444 boot_order = machine_class->default_boot_order;
4447 if (!kernel_cmdline) {
4448 kernel_cmdline = "";
4449 current_machine->kernel_cmdline = (char *)kernel_cmdline;
4452 linux_boot = (kernel_filename != NULL);
4454 if (!linux_boot && *kernel_cmdline != '\0') {
4455 error_report("-append only allowed with -kernel option");
4456 exit(1);
4459 if (!linux_boot && initrd_filename != NULL) {
4460 error_report("-initrd only allowed with -kernel option");
4461 exit(1);
4464 if (semihosting_enabled() && !semihosting_get_argc() && kernel_filename) {
4465 /* fall back to the -kernel/-append */
4466 semihosting_arg_fallback(kernel_filename, kernel_cmdline);
4469 os_set_line_buffering();
4471 /* spice needs the timers to be initialized by this point */
4472 qemu_spice_init();
4474 cpu_ticks_init();
4475 if (icount_opts) {
4476 if (!tcg_enabled()) {
4477 error_report("-icount is not allowed with hardware virtualization");
4478 exit(1);
4480 configure_icount(icount_opts, &error_abort);
4481 qemu_opts_del(icount_opts);
4484 if (default_net) {
4485 QemuOptsList *net = qemu_find_opts("net");
4486 qemu_opts_set(net, NULL, "type", "nic", &error_abort);
4487 #ifdef CONFIG_SLIRP
4488 qemu_opts_set(net, NULL, "type", "user", &error_abort);
4489 #endif
4492 colo_info_init();
4494 if (net_init_clients() < 0) {
4495 exit(1);
4498 if (qemu_opts_foreach(qemu_find_opts("object"),
4499 user_creatable_add_opts_foreach,
4500 object_create_delayed, NULL)) {
4501 exit(1);
4504 #ifdef CONFIG_TPM
4505 if (tpm_init() < 0) {
4506 exit(1);
4508 #endif
4510 /* init the bluetooth world */
4511 if (foreach_device_config(DEV_BT, bt_parse))
4512 exit(1);
4514 if (!xen_enabled()) {
4515 /* On 32-bit hosts, QEMU is limited by virtual address space */
4516 if (ram_size > (2047 << 20) && HOST_LONG_BITS == 32) {
4517 error_report("at most 2047 MB RAM can be simulated");
4518 exit(1);
4522 blk_mig_init();
4523 ram_mig_init();
4525 /* If the currently selected machine wishes to override the units-per-bus
4526 * property of its default HBA interface type, do so now. */
4527 if (machine_class->units_per_default_bus) {
4528 override_max_devs(machine_class->block_default_type,
4529 machine_class->units_per_default_bus);
4532 /* open the virtual block devices */
4533 if (snapshot || replay_mode != REPLAY_MODE_NONE) {
4534 qemu_opts_foreach(qemu_find_opts("drive"), drive_enable_snapshot,
4535 NULL, NULL);
4537 if (qemu_opts_foreach(qemu_find_opts("drive"), drive_init_func,
4538 &machine_class->block_default_type, NULL)) {
4539 exit(1);
4542 default_drive(default_cdrom, snapshot, machine_class->block_default_type, 2,
4543 CDROM_OPTS);
4544 default_drive(default_floppy, snapshot, IF_FLOPPY, 0, FD_OPTS);
4545 default_drive(default_sdcard, snapshot, IF_SD, 0, SD_OPTS);
4547 parse_numa_opts(machine_class);
4549 if (qemu_opts_foreach(qemu_find_opts("mon"),
4550 mon_init_func, NULL, NULL)) {
4551 exit(1);
4554 if (foreach_device_config(DEV_SERIAL, serial_parse) < 0)
4555 exit(1);
4556 if (foreach_device_config(DEV_PARALLEL, parallel_parse) < 0)
4557 exit(1);
4558 if (foreach_device_config(DEV_VIRTCON, virtcon_parse) < 0)
4559 exit(1);
4560 if (foreach_device_config(DEV_SCLP, sclp_parse) < 0) {
4561 exit(1);
4563 if (foreach_device_config(DEV_DEBUGCON, debugcon_parse) < 0)
4564 exit(1);
4566 /* If no default VGA is requested, the default is "none". */
4567 if (default_vga) {
4568 if (machine_class->default_display) {
4569 vga_model = machine_class->default_display;
4570 } else if (vga_interface_available(VGA_CIRRUS)) {
4571 vga_model = "cirrus";
4572 } else if (vga_interface_available(VGA_STD)) {
4573 vga_model = "std";
4576 if (vga_model) {
4577 select_vgahw(vga_model);
4580 if (watchdog) {
4581 i = select_watchdog(watchdog);
4582 if (i > 0)
4583 exit (i == 1 ? 1 : 0);
4586 machine_register_compat_props(current_machine);
4588 qemu_opts_foreach(qemu_find_opts("global"),
4589 global_init_func, NULL, NULL);
4591 /* This checkpoint is required by replay to separate prior clock
4592 reading from the other reads, because timer polling functions query
4593 clock values from the log. */
4594 replay_checkpoint(CHECKPOINT_INIT);
4595 qdev_machine_init();
4597 current_machine->ram_size = ram_size;
4598 current_machine->maxram_size = maxram_size;
4599 current_machine->ram_slots = ram_slots;
4600 current_machine->boot_order = boot_order;
4601 current_machine->cpu_model = cpu_model;
4603 machine_class->init(current_machine);
4605 realtime_init();
4607 audio_init();
4609 cpu_synchronize_all_post_init();
4611 numa_post_machine_init();
4613 if (hax_enabled()) {
4614 hax_sync_vcpus();
4617 if (qemu_opts_foreach(qemu_find_opts("fw_cfg"),
4618 parse_fw_cfg, fw_cfg_find(), NULL) != 0) {
4619 exit(1);
4622 /* init USB devices */
4623 if (machine_usb(current_machine)) {
4624 if (foreach_device_config(DEV_USB, usb_parse) < 0)
4625 exit(1);
4628 /* Check if IGD GFX passthrough. */
4629 igd_gfx_passthru();
4631 /* init generic devices */
4632 rom_set_order_override(FW_CFG_ORDER_OVERRIDE_DEVICE);
4633 if (qemu_opts_foreach(qemu_find_opts("device"),
4634 device_init_func, NULL, NULL)) {
4635 exit(1);
4637 rom_reset_order_override();
4639 /* Did we create any drives that we failed to create a device for? */
4640 drive_check_orphaned();
4642 /* Don't warn about the default network setup that you get if
4643 * no command line -net or -netdev options are specified. There
4644 * are two cases that we would otherwise complain about:
4645 * (1) board doesn't support a NIC but the implicit "-net nic"
4646 * requested one
4647 * (2) CONFIG_SLIRP not set, in which case the implicit "-net nic"
4648 * sets up a nic that isn't connected to anything.
4650 if (!default_net) {
4651 net_check_clients();
4655 if (boot_once) {
4656 qemu_boot_set(boot_once, &error_fatal);
4657 qemu_register_reset(restore_boot_order, g_strdup(boot_order));
4660 ds = init_displaystate();
4662 /* init local displays */
4663 switch (display_type) {
4664 case DT_CURSES:
4665 curses_display_init(ds, full_screen);
4666 break;
4667 case DT_SDL:
4668 sdl_display_init(ds, full_screen, no_frame);
4669 break;
4670 case DT_COCOA:
4671 cocoa_display_init(ds, full_screen);
4672 break;
4673 case DT_GTK:
4674 gtk_display_init(ds, full_screen, grab_on_hover);
4675 break;
4676 default:
4677 break;
4680 /* must be after terminal init, SDL library changes signal handlers */
4681 os_setup_signal_handling();
4683 /* init remote displays */
4684 #ifdef CONFIG_VNC
4685 qemu_opts_foreach(qemu_find_opts("vnc"),
4686 vnc_init_func, NULL, NULL);
4687 #endif
4689 if (using_spice) {
4690 qemu_spice_display_init();
4693 if (foreach_device_config(DEV_GDB, gdbserver_start) < 0) {
4694 exit(1);
4697 qdev_machine_creation_done();
4699 /* TODO: once all bus devices are qdevified, this should be done
4700 * when bus is created by qdev.c */
4701 qemu_register_reset(qbus_reset_all_fn, sysbus_get_default());
4702 qemu_run_machine_init_done_notifiers();
4704 if (rom_check_and_register_reset() != 0) {
4705 error_report("rom check and register reset failed");
4706 exit(1);
4709 replay_start();
4711 /* This checkpoint is required by replay to separate prior clock
4712 reading from the other reads, because timer polling functions query
4713 clock values from the log. */
4714 replay_checkpoint(CHECKPOINT_RESET);
4715 qemu_system_reset(VMRESET_SILENT);
4716 register_global_state();
4717 if (loadvm) {
4718 if (load_vmstate(loadvm) < 0) {
4719 autostart = 0;
4723 qdev_prop_check_globals();
4724 if (vmstate_dump_file) {
4725 /* dump and exit */
4726 dump_vmstate_json_to_file(vmstate_dump_file);
4727 return 0;
4730 if (incoming) {
4731 Error *local_err = NULL;
4732 qemu_start_incoming_migration(incoming, &local_err);
4733 if (local_err) {
4734 error_reportf_err(local_err, "-incoming %s: ", incoming);
4735 exit(1);
4737 } else if (autostart) {
4738 vm_start();
4741 os_setup_post();
4743 main_loop();
4744 replay_disable_events();
4745 iothread_stop_all();
4747 bdrv_close_all();
4748 pause_all_vcpus();
4749 res_free();
4751 /* vhost-user must be cleaned up before chardevs. */
4752 net_cleanup();
4753 audio_cleanup();
4754 monitor_cleanup();
4755 qemu_chr_cleanup();
4757 return 0;