qapi2texi: Suppress unwanted commas in generated output
[qemu/ar7.git] / vl.c
blob9988f2403f3ba7d9719a7452af654c75dc0a5313
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 < cpus) {
1292 error_report("maxcpus must be equal to or greater than smp");
1293 exit(1);
1296 if (sockets * cores * threads > max_cpus) {
1297 error_report("cpu topology: "
1298 "sockets (%u) * cores (%u) * threads (%u) > "
1299 "maxcpus (%u)",
1300 sockets, cores, threads, max_cpus);
1301 exit(1);
1304 smp_cpus = cpus;
1305 smp_cores = cores;
1306 smp_threads = threads;
1309 if (smp_cpus > 1) {
1310 Error *blocker = NULL;
1311 error_setg(&blocker, QERR_REPLAY_NOT_SUPPORTED, "smp");
1312 replay_add_blocker(blocker);
1316 static void realtime_init(void)
1318 if (enable_mlock) {
1319 if (os_mlock() < 0) {
1320 error_report("locking memory failed");
1321 exit(1);
1327 static void configure_msg(QemuOpts *opts)
1329 enable_timestamp_msg = qemu_opt_get_bool(opts, "timestamp", true);
1332 /***********************************************************/
1333 /* Semihosting */
1335 typedef struct SemihostingConfig {
1336 bool enabled;
1337 SemihostingTarget target;
1338 const char **argv;
1339 int argc;
1340 const char *cmdline; /* concatenated argv */
1341 } SemihostingConfig;
1343 static SemihostingConfig semihosting;
1345 bool semihosting_enabled(void)
1347 return semihosting.enabled;
1350 SemihostingTarget semihosting_get_target(void)
1352 return semihosting.target;
1355 const char *semihosting_get_arg(int i)
1357 if (i >= semihosting.argc) {
1358 return NULL;
1360 return semihosting.argv[i];
1363 int semihosting_get_argc(void)
1365 return semihosting.argc;
1368 const char *semihosting_get_cmdline(void)
1370 if (semihosting.cmdline == NULL && semihosting.argc > 0) {
1371 semihosting.cmdline = g_strjoinv(" ", (gchar **)semihosting.argv);
1373 return semihosting.cmdline;
1376 static int add_semihosting_arg(void *opaque,
1377 const char *name, const char *val,
1378 Error **errp)
1380 SemihostingConfig *s = opaque;
1381 if (strcmp(name, "arg") == 0) {
1382 s->argc++;
1383 /* one extra element as g_strjoinv() expects NULL-terminated array */
1384 s->argv = g_realloc(s->argv, (s->argc + 1) * sizeof(void *));
1385 s->argv[s->argc - 1] = val;
1386 s->argv[s->argc] = NULL;
1388 return 0;
1391 /* Use strings passed via -kernel/-append to initialize semihosting.argv[] */
1392 static inline void semihosting_arg_fallback(const char *file, const char *cmd)
1394 char *cmd_token;
1396 /* argv[0] */
1397 add_semihosting_arg(&semihosting, "arg", file, NULL);
1399 /* split -append and initialize argv[1..n] */
1400 cmd_token = strtok(g_strdup(cmd), " ");
1401 while (cmd_token) {
1402 add_semihosting_arg(&semihosting, "arg", cmd_token, NULL);
1403 cmd_token = strtok(NULL, " ");
1407 /* Now we still need this for compatibility with XEN. */
1408 bool has_igd_gfx_passthru;
1409 static void igd_gfx_passthru(void)
1411 has_igd_gfx_passthru = current_machine->igd_gfx_passthru;
1414 /***********************************************************/
1415 /* USB devices */
1417 static int usb_device_add(const char *devname)
1419 USBDevice *dev = NULL;
1420 #ifndef CONFIG_LINUX
1421 const char *p;
1422 #endif
1424 if (!machine_usb(current_machine)) {
1425 return -1;
1428 /* drivers with .usbdevice_name entry in USBDeviceInfo */
1429 dev = usbdevice_create(devname);
1430 if (dev)
1431 goto done;
1433 /* the other ones */
1434 #ifndef CONFIG_LINUX
1435 /* only the linux version is qdev-ified, usb-bsd still needs this */
1436 if (strstart(devname, "host:", &p)) {
1437 dev = usb_host_device_open(usb_bus_find(-1), p);
1439 #endif
1440 if (!dev)
1441 return -1;
1443 done:
1444 return 0;
1447 static int usb_device_del(const char *devname)
1449 int bus_num, addr;
1450 const char *p;
1452 if (strstart(devname, "host:", &p)) {
1453 return -1;
1456 if (!machine_usb(current_machine)) {
1457 return -1;
1460 p = strchr(devname, '.');
1461 if (!p)
1462 return -1;
1463 bus_num = strtoul(devname, NULL, 0);
1464 addr = strtoul(p + 1, NULL, 0);
1466 return usb_device_delete_addr(bus_num, addr);
1469 static int usb_parse(const char *cmdline)
1471 int r;
1472 r = usb_device_add(cmdline);
1473 if (r < 0) {
1474 error_report("could not add USB device '%s'", cmdline);
1476 return r;
1479 void hmp_usb_add(Monitor *mon, const QDict *qdict)
1481 const char *devname = qdict_get_str(qdict, "devname");
1482 if (usb_device_add(devname) < 0) {
1483 error_report("could not add USB device '%s'", devname);
1487 void hmp_usb_del(Monitor *mon, const QDict *qdict)
1489 const char *devname = qdict_get_str(qdict, "devname");
1490 if (usb_device_del(devname) < 0) {
1491 error_report("could not delete USB device '%s'", devname);
1495 /***********************************************************/
1496 /* machine registration */
1498 MachineState *current_machine;
1500 static MachineClass *find_machine(const char *name)
1502 GSList *el, *machines = object_class_get_list(TYPE_MACHINE, false);
1503 MachineClass *mc = NULL;
1505 for (el = machines; el; el = el->next) {
1506 MachineClass *temp = el->data;
1508 if (!strcmp(temp->name, name)) {
1509 mc = temp;
1510 break;
1512 if (temp->alias &&
1513 !strcmp(temp->alias, name)) {
1514 mc = temp;
1515 break;
1519 g_slist_free(machines);
1520 return mc;
1523 MachineClass *find_default_machine(void)
1525 GSList *el, *machines = object_class_get_list(TYPE_MACHINE, false);
1526 MachineClass *mc = NULL;
1528 for (el = machines; el; el = el->next) {
1529 MachineClass *temp = el->data;
1531 if (temp->is_default) {
1532 mc = temp;
1533 break;
1537 g_slist_free(machines);
1538 return mc;
1541 MachineInfoList *qmp_query_machines(Error **errp)
1543 GSList *el, *machines = object_class_get_list(TYPE_MACHINE, false);
1544 MachineInfoList *mach_list = NULL;
1546 for (el = machines; el; el = el->next) {
1547 MachineClass *mc = el->data;
1548 MachineInfoList *entry;
1549 MachineInfo *info;
1551 info = g_malloc0(sizeof(*info));
1552 if (mc->is_default) {
1553 info->has_is_default = true;
1554 info->is_default = true;
1557 if (mc->alias) {
1558 info->has_alias = true;
1559 info->alias = g_strdup(mc->alias);
1562 info->name = g_strdup(mc->name);
1563 info->cpu_max = !mc->max_cpus ? 1 : mc->max_cpus;
1564 info->hotpluggable_cpus = !!mc->query_hotpluggable_cpus;
1566 entry = g_malloc0(sizeof(*entry));
1567 entry->value = info;
1568 entry->next = mach_list;
1569 mach_list = entry;
1572 g_slist_free(machines);
1573 return mach_list;
1576 static int machine_help_func(QemuOpts *opts, MachineState *machine)
1578 ObjectProperty *prop;
1579 ObjectPropertyIterator iter;
1581 if (!qemu_opt_has_help_opt(opts)) {
1582 return 0;
1585 object_property_iter_init(&iter, OBJECT(machine));
1586 while ((prop = object_property_iter_next(&iter))) {
1587 if (!prop->set) {
1588 continue;
1591 error_printf("%s.%s=%s", MACHINE_GET_CLASS(machine)->name,
1592 prop->name, prop->type);
1593 if (prop->description) {
1594 error_printf(" (%s)\n", prop->description);
1595 } else {
1596 error_printf("\n");
1600 return 1;
1603 /***********************************************************/
1604 /* main execution loop */
1606 struct vm_change_state_entry {
1607 VMChangeStateHandler *cb;
1608 void *opaque;
1609 QLIST_ENTRY (vm_change_state_entry) entries;
1612 static QLIST_HEAD(vm_change_state_head, vm_change_state_entry) vm_change_state_head;
1614 VMChangeStateEntry *qemu_add_vm_change_state_handler(VMChangeStateHandler *cb,
1615 void *opaque)
1617 VMChangeStateEntry *e;
1619 e = g_malloc0(sizeof (*e));
1621 e->cb = cb;
1622 e->opaque = opaque;
1623 QLIST_INSERT_HEAD(&vm_change_state_head, e, entries);
1624 return e;
1627 void qemu_del_vm_change_state_handler(VMChangeStateEntry *e)
1629 QLIST_REMOVE (e, entries);
1630 g_free (e);
1633 void vm_state_notify(int running, RunState state)
1635 VMChangeStateEntry *e, *next;
1637 trace_vm_state_notify(running, state);
1639 QLIST_FOREACH_SAFE(e, &vm_change_state_head, entries, next) {
1640 e->cb(e->opaque, running, state);
1644 /* reset/shutdown handler */
1646 typedef struct QEMUResetEntry {
1647 QTAILQ_ENTRY(QEMUResetEntry) entry;
1648 QEMUResetHandler *func;
1649 void *opaque;
1650 } QEMUResetEntry;
1652 static QTAILQ_HEAD(reset_handlers, QEMUResetEntry) reset_handlers =
1653 QTAILQ_HEAD_INITIALIZER(reset_handlers);
1654 static int reset_requested;
1655 static int shutdown_requested, shutdown_signal = -1;
1656 static pid_t shutdown_pid;
1657 static int powerdown_requested;
1658 static int debug_requested;
1659 static int suspend_requested;
1660 static WakeupReason wakeup_reason;
1661 static NotifierList powerdown_notifiers =
1662 NOTIFIER_LIST_INITIALIZER(powerdown_notifiers);
1663 static NotifierList suspend_notifiers =
1664 NOTIFIER_LIST_INITIALIZER(suspend_notifiers);
1665 static NotifierList wakeup_notifiers =
1666 NOTIFIER_LIST_INITIALIZER(wakeup_notifiers);
1667 static uint32_t wakeup_reason_mask = ~(1 << QEMU_WAKEUP_REASON_NONE);
1669 int qemu_shutdown_requested_get(void)
1671 return shutdown_requested;
1674 int qemu_reset_requested_get(void)
1676 return reset_requested;
1679 static int qemu_shutdown_requested(void)
1681 return atomic_xchg(&shutdown_requested, 0);
1684 static void qemu_kill_report(void)
1686 if (!qtest_driver() && shutdown_signal != -1) {
1687 if (shutdown_pid == 0) {
1688 /* This happens for eg ^C at the terminal, so it's worth
1689 * avoiding printing an odd message in that case.
1691 error_report("terminating on signal %d", shutdown_signal);
1692 } else {
1693 char *shutdown_cmd = qemu_get_pid_name(shutdown_pid);
1695 error_report("terminating on signal %d from pid " FMT_pid " (%s)",
1696 shutdown_signal, shutdown_pid,
1697 shutdown_cmd ? shutdown_cmd : "<unknown process>");
1698 g_free(shutdown_cmd);
1700 shutdown_signal = -1;
1704 static int qemu_reset_requested(void)
1706 int r = reset_requested;
1707 if (r && replay_checkpoint(CHECKPOINT_RESET_REQUESTED)) {
1708 reset_requested = 0;
1709 return r;
1711 return false;
1714 static int qemu_suspend_requested(void)
1716 int r = suspend_requested;
1717 if (r && replay_checkpoint(CHECKPOINT_SUSPEND_REQUESTED)) {
1718 suspend_requested = 0;
1719 return r;
1721 return false;
1724 static WakeupReason qemu_wakeup_requested(void)
1726 return wakeup_reason;
1729 static int qemu_powerdown_requested(void)
1731 int r = powerdown_requested;
1732 powerdown_requested = 0;
1733 return r;
1736 static int qemu_debug_requested(void)
1738 int r = debug_requested;
1739 debug_requested = 0;
1740 return r;
1743 void qemu_register_reset(QEMUResetHandler *func, void *opaque)
1745 QEMUResetEntry *re = g_malloc0(sizeof(QEMUResetEntry));
1747 re->func = func;
1748 re->opaque = opaque;
1749 QTAILQ_INSERT_TAIL(&reset_handlers, re, entry);
1752 void qemu_unregister_reset(QEMUResetHandler *func, void *opaque)
1754 QEMUResetEntry *re;
1756 QTAILQ_FOREACH(re, &reset_handlers, entry) {
1757 if (re->func == func && re->opaque == opaque) {
1758 QTAILQ_REMOVE(&reset_handlers, re, entry);
1759 g_free(re);
1760 return;
1765 void qemu_devices_reset(void)
1767 QEMUResetEntry *re, *nre;
1769 /* reset all devices */
1770 QTAILQ_FOREACH_SAFE(re, &reset_handlers, entry, nre) {
1771 re->func(re->opaque);
1775 void qemu_system_reset(bool report)
1777 MachineClass *mc;
1779 mc = current_machine ? MACHINE_GET_CLASS(current_machine) : NULL;
1781 cpu_synchronize_all_states();
1783 if (mc && mc->reset) {
1784 mc->reset();
1785 } else {
1786 qemu_devices_reset();
1788 if (report) {
1789 qapi_event_send_reset(&error_abort);
1791 cpu_synchronize_all_post_reset();
1794 void qemu_system_guest_panicked(void)
1796 if (current_cpu) {
1797 current_cpu->crash_occurred = true;
1799 qapi_event_send_guest_panicked(GUEST_PANIC_ACTION_PAUSE, &error_abort);
1800 vm_stop(RUN_STATE_GUEST_PANICKED);
1801 if (!no_shutdown) {
1802 qapi_event_send_guest_panicked(GUEST_PANIC_ACTION_POWEROFF,
1803 &error_abort);
1804 qemu_system_shutdown_request();
1808 void qemu_system_reset_request(void)
1810 if (no_reboot) {
1811 shutdown_requested = 1;
1812 } else {
1813 reset_requested = 1;
1815 cpu_stop_current();
1816 qemu_notify_event();
1819 static void qemu_system_suspend(void)
1821 pause_all_vcpus();
1822 notifier_list_notify(&suspend_notifiers, NULL);
1823 runstate_set(RUN_STATE_SUSPENDED);
1824 qapi_event_send_suspend(&error_abort);
1827 void qemu_system_suspend_request(void)
1829 if (runstate_check(RUN_STATE_SUSPENDED)) {
1830 return;
1832 suspend_requested = 1;
1833 cpu_stop_current();
1834 qemu_notify_event();
1837 void qemu_register_suspend_notifier(Notifier *notifier)
1839 notifier_list_add(&suspend_notifiers, notifier);
1842 void qemu_system_wakeup_request(WakeupReason reason)
1844 trace_system_wakeup_request(reason);
1846 if (!runstate_check(RUN_STATE_SUSPENDED)) {
1847 return;
1849 if (!(wakeup_reason_mask & (1 << reason))) {
1850 return;
1852 runstate_set(RUN_STATE_RUNNING);
1853 wakeup_reason = reason;
1854 qemu_notify_event();
1857 void qemu_system_wakeup_enable(WakeupReason reason, bool enabled)
1859 if (enabled) {
1860 wakeup_reason_mask |= (1 << reason);
1861 } else {
1862 wakeup_reason_mask &= ~(1 << reason);
1866 void qemu_register_wakeup_notifier(Notifier *notifier)
1868 notifier_list_add(&wakeup_notifiers, notifier);
1871 void qemu_system_killed(int signal, pid_t pid)
1873 shutdown_signal = signal;
1874 shutdown_pid = pid;
1875 no_shutdown = 0;
1877 /* Cannot call qemu_system_shutdown_request directly because
1878 * we are in a signal handler.
1880 shutdown_requested = 1;
1881 qemu_notify_event();
1884 void qemu_system_shutdown_request(void)
1886 trace_qemu_system_shutdown_request();
1887 replay_shutdown_request();
1888 shutdown_requested = 1;
1889 qemu_notify_event();
1892 static void qemu_system_powerdown(void)
1894 qapi_event_send_powerdown(&error_abort);
1895 notifier_list_notify(&powerdown_notifiers, NULL);
1898 void qemu_system_powerdown_request(void)
1900 trace_qemu_system_powerdown_request();
1901 powerdown_requested = 1;
1902 qemu_notify_event();
1905 void qemu_register_powerdown_notifier(Notifier *notifier)
1907 notifier_list_add(&powerdown_notifiers, notifier);
1910 void qemu_system_debug_request(void)
1912 debug_requested = 1;
1913 qemu_notify_event();
1916 static bool main_loop_should_exit(void)
1918 RunState r;
1919 if (qemu_debug_requested()) {
1920 vm_stop(RUN_STATE_DEBUG);
1922 if (qemu_suspend_requested()) {
1923 qemu_system_suspend();
1925 if (qemu_shutdown_requested()) {
1926 qemu_kill_report();
1927 qapi_event_send_shutdown(&error_abort);
1928 if (no_shutdown) {
1929 vm_stop(RUN_STATE_SHUTDOWN);
1930 } else {
1931 return true;
1934 if (qemu_reset_requested()) {
1935 pause_all_vcpus();
1936 qemu_system_reset(VMRESET_REPORT);
1937 resume_all_vcpus();
1938 if (!runstate_check(RUN_STATE_RUNNING) &&
1939 !runstate_check(RUN_STATE_INMIGRATE)) {
1940 runstate_set(RUN_STATE_PRELAUNCH);
1943 if (qemu_wakeup_requested()) {
1944 pause_all_vcpus();
1945 qemu_system_reset(VMRESET_SILENT);
1946 notifier_list_notify(&wakeup_notifiers, &wakeup_reason);
1947 wakeup_reason = QEMU_WAKEUP_REASON_NONE;
1948 resume_all_vcpus();
1949 qapi_event_send_wakeup(&error_abort);
1951 if (qemu_powerdown_requested()) {
1952 qemu_system_powerdown();
1954 if (qemu_vmstop_requested(&r)) {
1955 vm_stop(r);
1957 return false;
1960 static void main_loop(void)
1962 bool nonblocking;
1963 int last_io = 0;
1964 #ifdef CONFIG_PROFILER
1965 int64_t ti;
1966 #endif
1967 do {
1968 nonblocking = tcg_enabled() && last_io > 0;
1969 #ifdef CONFIG_PROFILER
1970 ti = profile_getclock();
1971 #endif
1972 last_io = main_loop_wait(nonblocking);
1973 #ifdef CONFIG_PROFILER
1974 dev_time += profile_getclock() - ti;
1975 #endif
1976 } while (!main_loop_should_exit());
1979 static void version(void)
1981 printf("QEMU emulator version " QEMU_VERSION QEMU_PKGVERSION "\n"
1982 QEMU_COPYRIGHT "\n");
1985 static void QEMU_NORETURN help(int exitcode)
1987 version();
1988 printf("usage: %s [options] [disk_image]\n\n"
1989 "'disk_image' is a raw hard disk image for IDE hard disk 0\n\n",
1990 error_get_progname());
1992 #define QEMU_OPTIONS_GENERATE_HELP
1993 #include "qemu-options-wrapper.h"
1995 printf("\nDuring emulation, the following keys are useful:\n"
1996 "ctrl-alt-f toggle full screen\n"
1997 "ctrl-alt-n switch to virtual console 'n'\n"
1998 "ctrl-alt toggle mouse and keyboard grab\n"
1999 "\n"
2000 "When using -nographic, press 'ctrl-a h' to get some help.\n");
2002 exit(exitcode);
2005 #define HAS_ARG 0x0001
2007 typedef struct QEMUOption {
2008 const char *name;
2009 int flags;
2010 int index;
2011 uint32_t arch_mask;
2012 } QEMUOption;
2014 static const QEMUOption qemu_options[] = {
2015 { "h", 0, QEMU_OPTION_h, QEMU_ARCH_ALL },
2016 #define QEMU_OPTIONS_GENERATE_OPTIONS
2017 #include "qemu-options-wrapper.h"
2018 { NULL },
2021 typedef struct VGAInterfaceInfo {
2022 const char *opt_name; /* option name */
2023 const char *name; /* human-readable name */
2024 /* Class names indicating that support is available.
2025 * If no class is specified, the interface is always available */
2026 const char *class_names[2];
2027 } VGAInterfaceInfo;
2029 static VGAInterfaceInfo vga_interfaces[VGA_TYPE_MAX] = {
2030 [VGA_NONE] = {
2031 .opt_name = "none",
2033 [VGA_STD] = {
2034 .opt_name = "std",
2035 .name = "standard VGA",
2036 .class_names = { "VGA", "isa-vga" },
2038 [VGA_CIRRUS] = {
2039 .opt_name = "cirrus",
2040 .name = "Cirrus VGA",
2041 .class_names = { "cirrus-vga", "isa-cirrus-vga" },
2043 [VGA_VMWARE] = {
2044 .opt_name = "vmware",
2045 .name = "VMWare SVGA",
2046 .class_names = { "vmware-svga" },
2048 [VGA_VIRTIO] = {
2049 .opt_name = "virtio",
2050 .name = "Virtio VGA",
2051 .class_names = { "virtio-vga" },
2053 [VGA_QXL] = {
2054 .opt_name = "qxl",
2055 .name = "QXL VGA",
2056 .class_names = { "qxl-vga" },
2058 [VGA_TCX] = {
2059 .opt_name = "tcx",
2060 .name = "TCX framebuffer",
2061 .class_names = { "SUNW,tcx" },
2063 [VGA_CG3] = {
2064 .opt_name = "cg3",
2065 .name = "CG3 framebuffer",
2066 .class_names = { "cgthree" },
2068 [VGA_XENFB] = {
2069 .opt_name = "xenfb",
2073 static bool vga_interface_available(VGAInterfaceType t)
2075 VGAInterfaceInfo *ti = &vga_interfaces[t];
2077 assert(t < VGA_TYPE_MAX);
2078 return !ti->class_names[0] ||
2079 object_class_by_name(ti->class_names[0]) ||
2080 object_class_by_name(ti->class_names[1]);
2083 static void select_vgahw(const char *p)
2085 const char *opts;
2086 int t;
2088 assert(vga_interface_type == VGA_NONE);
2089 for (t = 0; t < VGA_TYPE_MAX; t++) {
2090 VGAInterfaceInfo *ti = &vga_interfaces[t];
2091 if (ti->opt_name && strstart(p, ti->opt_name, &opts)) {
2092 if (!vga_interface_available(t)) {
2093 error_report("%s not available", ti->name);
2094 exit(1);
2096 vga_interface_type = t;
2097 break;
2100 if (t == VGA_TYPE_MAX) {
2101 invalid_vga:
2102 error_report("unknown vga type: %s", p);
2103 exit(1);
2105 while (*opts) {
2106 const char *nextopt;
2108 if (strstart(opts, ",retrace=", &nextopt)) {
2109 opts = nextopt;
2110 if (strstart(opts, "dumb", &nextopt))
2111 vga_retrace_method = VGA_RETRACE_DUMB;
2112 else if (strstart(opts, "precise", &nextopt))
2113 vga_retrace_method = VGA_RETRACE_PRECISE;
2114 else goto invalid_vga;
2115 } else goto invalid_vga;
2116 opts = nextopt;
2120 typedef enum DisplayType {
2121 DT_DEFAULT,
2122 DT_CURSES,
2123 DT_SDL,
2124 DT_COCOA,
2125 DT_GTK,
2126 DT_NONE,
2127 } DisplayType;
2129 static DisplayType select_display(const char *p)
2131 const char *opts;
2132 DisplayType display = DT_DEFAULT;
2134 if (strstart(p, "sdl", &opts)) {
2135 #ifdef CONFIG_SDL
2136 display = DT_SDL;
2137 while (*opts) {
2138 const char *nextopt;
2140 if (strstart(opts, ",frame=", &nextopt)) {
2141 opts = nextopt;
2142 if (strstart(opts, "on", &nextopt)) {
2143 no_frame = 0;
2144 } else if (strstart(opts, "off", &nextopt)) {
2145 no_frame = 1;
2146 } else {
2147 goto invalid_sdl_args;
2149 } else if (strstart(opts, ",alt_grab=", &nextopt)) {
2150 opts = nextopt;
2151 if (strstart(opts, "on", &nextopt)) {
2152 alt_grab = 1;
2153 } else if (strstart(opts, "off", &nextopt)) {
2154 alt_grab = 0;
2155 } else {
2156 goto invalid_sdl_args;
2158 } else if (strstart(opts, ",ctrl_grab=", &nextopt)) {
2159 opts = nextopt;
2160 if (strstart(opts, "on", &nextopt)) {
2161 ctrl_grab = 1;
2162 } else if (strstart(opts, "off", &nextopt)) {
2163 ctrl_grab = 0;
2164 } else {
2165 goto invalid_sdl_args;
2167 } else if (strstart(opts, ",window_close=", &nextopt)) {
2168 opts = nextopt;
2169 if (strstart(opts, "on", &nextopt)) {
2170 no_quit = 0;
2171 } else if (strstart(opts, "off", &nextopt)) {
2172 no_quit = 1;
2173 } else {
2174 goto invalid_sdl_args;
2176 } else if (strstart(opts, ",gl=", &nextopt)) {
2177 opts = nextopt;
2178 if (strstart(opts, "on", &nextopt)) {
2179 request_opengl = 1;
2180 } else if (strstart(opts, "off", &nextopt)) {
2181 request_opengl = 0;
2182 } else {
2183 goto invalid_sdl_args;
2185 } else {
2186 invalid_sdl_args:
2187 error_report("invalid SDL option string");
2188 exit(1);
2190 opts = nextopt;
2192 #else
2193 error_report("SDL support is disabled");
2194 exit(1);
2195 #endif
2196 } else if (strstart(p, "vnc", &opts)) {
2197 if (*opts == '=') {
2198 vnc_parse(opts + 1, &error_fatal);
2199 } else {
2200 error_report("VNC requires a display argument vnc=<display>");
2201 exit(1);
2203 } else if (strstart(p, "curses", &opts)) {
2204 #ifdef CONFIG_CURSES
2205 display = DT_CURSES;
2206 #else
2207 error_report("curses support is disabled");
2208 exit(1);
2209 #endif
2210 } else if (strstart(p, "gtk", &opts)) {
2211 #ifdef CONFIG_GTK
2212 display = DT_GTK;
2213 while (*opts) {
2214 const char *nextopt;
2216 if (strstart(opts, ",grab_on_hover=", &nextopt)) {
2217 opts = nextopt;
2218 if (strstart(opts, "on", &nextopt)) {
2219 grab_on_hover = true;
2220 } else if (strstart(opts, "off", &nextopt)) {
2221 grab_on_hover = false;
2222 } else {
2223 goto invalid_gtk_args;
2225 } else if (strstart(opts, ",gl=", &nextopt)) {
2226 opts = nextopt;
2227 if (strstart(opts, "on", &nextopt)) {
2228 request_opengl = 1;
2229 } else if (strstart(opts, "off", &nextopt)) {
2230 request_opengl = 0;
2231 } else {
2232 goto invalid_gtk_args;
2234 } else {
2235 invalid_gtk_args:
2236 error_report("invalid GTK option string");
2237 exit(1);
2239 opts = nextopt;
2241 #else
2242 error_report("GTK support is disabled");
2243 exit(1);
2244 #endif
2245 } else if (strstart(p, "none", &opts)) {
2246 display = DT_NONE;
2247 } else {
2248 error_report("unknown display type");
2249 exit(1);
2252 return display;
2255 static int balloon_parse(const char *arg)
2257 QemuOpts *opts;
2259 if (strcmp(arg, "none") == 0) {
2260 return 0;
2263 if (!strncmp(arg, "virtio", 6)) {
2264 if (arg[6] == ',') {
2265 /* have params -> parse them */
2266 opts = qemu_opts_parse_noisily(qemu_find_opts("device"), arg + 7,
2267 false);
2268 if (!opts)
2269 return -1;
2270 } else {
2271 /* create empty opts */
2272 opts = qemu_opts_create(qemu_find_opts("device"), NULL, 0,
2273 &error_abort);
2275 qemu_opt_set(opts, "driver", "virtio-balloon", &error_abort);
2276 return 0;
2279 return -1;
2282 #if defined(CONFIG_RUBY)
2283 /* Ruby interface for QEMU. See README.EXT for programming example. */
2284 #warning mit ruby
2285 #include <ruby.h>
2287 static VALUE qemu_open(int argc, VALUE *argv, VALUE klass)
2289 int i;
2290 for (i = 0; i < argc; i++) {
2291 argv[i] = StringValue(argv[i]);
2292 fprintf(stderr, "argv[%d] = %s\n", i, StringValuePtr(argv[i]));
2294 //~ if (rb_scan_args(argc, argv, "11", &file, &vmode) == 1) {
2295 //~ mode = 0666; /* default value */
2296 //~ }
2297 return INT2FIX(argc);
2300 void Init_qemu(void)
2302 printf("%s\n", __func__);
2303 VALUE cQEMU = rb_define_class("QEMU", rb_cObject);
2304 rb_define_singleton_method(cQEMU, "run", qemu_open, -1);
2305 #if 0
2306 rb_define_method();
2307 #endif
2309 #endif /* CONFIG_RUBY */
2311 #if defined(CONFIG_DLL)
2312 BOOL APIENTRY DllMain(HINSTANCE hModule, DWORD ul_reason_for_call, LPVOID data)
2314 return FALSE;
2316 #endif /* CONFIG_DLL */
2318 char *qemu_find_file(int type, const char *name)
2320 int i;
2321 const char *subdir;
2322 char *buf;
2324 /* Try the name as a straight path first */
2325 if (access(name, R_OK) == 0) {
2326 trace_load_file(name, name);
2327 return g_strdup(name);
2330 switch (type) {
2331 case QEMU_FILE_TYPE_BIOS:
2332 subdir = "";
2333 break;
2334 case QEMU_FILE_TYPE_KEYMAP:
2335 subdir = "keymaps/";
2336 break;
2337 default:
2338 abort();
2341 for (i = 0; i < data_dir_idx; i++) {
2342 buf = g_strdup_printf("%s/%s%s", data_dir[i], subdir, name);
2343 if (access(buf, R_OK) == 0) {
2344 trace_load_file(name, buf);
2345 return buf;
2347 g_free(buf);
2349 return NULL;
2352 static inline bool nonempty_str(const char *str)
2354 return str && *str;
2357 static int parse_fw_cfg(void *opaque, QemuOpts *opts, Error **errp)
2359 gchar *buf;
2360 size_t size;
2361 const char *name, *file, *str;
2362 FWCfgState *fw_cfg = (FWCfgState *) opaque;
2364 if (fw_cfg == NULL) {
2365 error_report("fw_cfg device not available");
2366 return -1;
2368 name = qemu_opt_get(opts, "name");
2369 file = qemu_opt_get(opts, "file");
2370 str = qemu_opt_get(opts, "string");
2372 /* we need name and either a file or the content string */
2373 if (!(nonempty_str(name) && (nonempty_str(file) || nonempty_str(str)))) {
2374 error_report("invalid argument(s)");
2375 return -1;
2377 if (nonempty_str(file) && nonempty_str(str)) {
2378 error_report("file and string are mutually exclusive");
2379 return -1;
2381 if (strlen(name) > FW_CFG_MAX_FILE_PATH - 1) {
2382 error_report("name too long (max. %d char)", FW_CFG_MAX_FILE_PATH - 1);
2383 return -1;
2385 if (strncmp(name, "opt/", 4) != 0) {
2386 error_report("warning: externally provided fw_cfg item names "
2387 "should be prefixed with \"opt/\"");
2389 if (nonempty_str(str)) {
2390 size = strlen(str); /* NUL terminator NOT included in fw_cfg blob */
2391 buf = g_memdup(str, size);
2392 } else {
2393 if (!g_file_get_contents(file, &buf, &size, NULL)) {
2394 error_report("can't load %s", file);
2395 return -1;
2398 /* For legacy, keep user files in a specific global order. */
2399 fw_cfg_set_order_override(fw_cfg, FW_CFG_ORDER_OVERRIDE_USER);
2400 fw_cfg_add_file(fw_cfg, name, buf, size);
2401 fw_cfg_reset_order_override(fw_cfg);
2402 return 0;
2405 static int device_help_func(void *opaque, QemuOpts *opts, Error **errp)
2407 return qdev_device_help(opts);
2410 static int device_init_func(void *opaque, QemuOpts *opts, Error **errp)
2412 Error *err = NULL;
2413 DeviceState *dev;
2415 dev = qdev_device_add(opts, &err);
2416 if (!dev) {
2417 error_report_err(err);
2418 return -1;
2420 object_unref(OBJECT(dev));
2421 return 0;
2424 static int chardev_init_func(void *opaque, QemuOpts *opts, Error **errp)
2426 Error *local_err = NULL;
2428 qemu_chr_new_from_opts(opts, &local_err);
2429 if (local_err) {
2430 error_report_err(local_err);
2431 return -1;
2433 return 0;
2436 #ifdef CONFIG_VIRTFS
2437 static int fsdev_init_func(void *opaque, QemuOpts *opts, Error **errp)
2439 return qemu_fsdev_add(opts);
2441 #endif
2443 static int mon_init_func(void *opaque, QemuOpts *opts, Error **errp)
2445 CharDriverState *chr;
2446 const char *chardev;
2447 const char *mode;
2448 int flags;
2450 mode = qemu_opt_get(opts, "mode");
2451 if (mode == NULL) {
2452 mode = "readline";
2454 if (strcmp(mode, "readline") == 0) {
2455 flags = MONITOR_USE_READLINE;
2456 } else if (strcmp(mode, "control") == 0) {
2457 flags = MONITOR_USE_CONTROL;
2458 } else {
2459 error_report("unknown monitor mode \"%s\"", mode);
2460 exit(1);
2463 if (qemu_opt_get_bool(opts, "pretty", 0))
2464 flags |= MONITOR_USE_PRETTY;
2466 if (qemu_opt_get_bool(opts, "default", 0)) {
2467 error_report("option 'default' does nothing and is deprecated");
2470 chardev = qemu_opt_get(opts, "chardev");
2471 chr = qemu_chr_find(chardev);
2472 if (chr == NULL) {
2473 error_report("chardev \"%s\" not found", chardev);
2474 exit(1);
2477 monitor_init(chr, flags);
2478 return 0;
2481 static void monitor_parse(const char *optarg, const char *mode, bool pretty)
2483 static int monitor_device_index = 0;
2484 QemuOpts *opts;
2485 const char *p;
2486 char label[32];
2488 if (strstart(optarg, "chardev:", &p)) {
2489 snprintf(label, sizeof(label), "%s", p);
2490 } else {
2491 snprintf(label, sizeof(label), "compat_monitor%d",
2492 monitor_device_index);
2493 opts = qemu_chr_parse_compat(label, optarg);
2494 if (!opts) {
2495 error_report("parse error: %s", optarg);
2496 exit(1);
2500 opts = qemu_opts_create(qemu_find_opts("mon"), label, 1, &error_fatal);
2501 qemu_opt_set(opts, "mode", mode, &error_abort);
2502 qemu_opt_set(opts, "chardev", label, &error_abort);
2503 qemu_opt_set_bool(opts, "pretty", pretty, &error_abort);
2504 monitor_device_index++;
2507 struct device_config {
2508 enum {
2509 DEV_USB, /* -usbdevice */
2510 DEV_BT, /* -bt */
2511 DEV_SERIAL, /* -serial */
2512 DEV_PARALLEL, /* -parallel */
2513 DEV_VIRTCON, /* -virtioconsole */
2514 DEV_DEBUGCON, /* -debugcon */
2515 DEV_GDB, /* -gdb, -s */
2516 DEV_SCLP, /* s390 sclp */
2517 } type;
2518 const char *cmdline;
2519 Location loc;
2520 QTAILQ_ENTRY(device_config) next;
2523 static QTAILQ_HEAD(, device_config) device_configs =
2524 QTAILQ_HEAD_INITIALIZER(device_configs);
2526 static void add_device_config(int type, const char *cmdline)
2528 struct device_config *conf;
2530 conf = g_malloc0(sizeof(*conf));
2531 conf->type = type;
2532 conf->cmdline = cmdline;
2533 loc_save(&conf->loc);
2534 QTAILQ_INSERT_TAIL(&device_configs, conf, next);
2537 static int foreach_device_config(int type, int (*func)(const char *cmdline))
2539 struct device_config *conf;
2540 int rc;
2542 QTAILQ_FOREACH(conf, &device_configs, next) {
2543 if (conf->type != type)
2544 continue;
2545 loc_push_restore(&conf->loc);
2546 rc = func(conf->cmdline);
2547 loc_pop(&conf->loc);
2548 if (rc) {
2549 return rc;
2552 return 0;
2555 static int serial_parse(const char *devname)
2557 static int index = 0;
2558 char label[32];
2560 if (strcmp(devname, "none") == 0)
2561 return 0;
2562 if (index == MAX_SERIAL_PORTS) {
2563 error_report("too many serial ports");
2564 exit(1);
2566 snprintf(label, sizeof(label), "serial%d", index);
2567 serial_hds[index] = qemu_chr_new(label, devname);
2568 if (!serial_hds[index]) {
2569 error_report("could not connect serial device"
2570 " to character backend '%s'", devname);
2571 return -1;
2573 index++;
2574 return 0;
2577 static int parallel_parse(const char *devname)
2579 static int index = 0;
2580 char label[32];
2582 if (strcmp(devname, "none") == 0)
2583 return 0;
2584 if (index == MAX_PARALLEL_PORTS) {
2585 error_report("too many parallel ports");
2586 exit(1);
2588 snprintf(label, sizeof(label), "parallel%d", index);
2589 parallel_hds[index] = qemu_chr_new(label, devname);
2590 if (!parallel_hds[index]) {
2591 error_report("could not connect parallel device"
2592 " to character backend '%s'", devname);
2593 return -1;
2595 index++;
2596 return 0;
2599 static int virtcon_parse(const char *devname)
2601 QemuOptsList *device = qemu_find_opts("device");
2602 static int index = 0;
2603 char label[32];
2604 QemuOpts *bus_opts, *dev_opts;
2606 if (strcmp(devname, "none") == 0)
2607 return 0;
2608 if (index == MAX_VIRTIO_CONSOLES) {
2609 error_report("too many virtio consoles");
2610 exit(1);
2613 bus_opts = qemu_opts_create(device, NULL, 0, &error_abort);
2614 qemu_opt_set(bus_opts, "driver", "virtio-serial", &error_abort);
2616 dev_opts = qemu_opts_create(device, NULL, 0, &error_abort);
2617 qemu_opt_set(dev_opts, "driver", "virtconsole", &error_abort);
2619 snprintf(label, sizeof(label), "virtcon%d", index);
2620 virtcon_hds[index] = qemu_chr_new(label, devname);
2621 if (!virtcon_hds[index]) {
2622 error_report("could not connect virtio console"
2623 " to character backend '%s'", devname);
2624 return -1;
2626 qemu_opt_set(dev_opts, "chardev", label, &error_abort);
2628 index++;
2629 return 0;
2632 static int sclp_parse(const char *devname)
2634 QemuOptsList *device = qemu_find_opts("device");
2635 static int index = 0;
2636 char label[32];
2637 QemuOpts *dev_opts;
2639 if (strcmp(devname, "none") == 0) {
2640 return 0;
2642 if (index == MAX_SCLP_CONSOLES) {
2643 error_report("too many sclp consoles");
2644 exit(1);
2647 assert(arch_type == QEMU_ARCH_S390X);
2649 dev_opts = qemu_opts_create(device, NULL, 0, NULL);
2650 qemu_opt_set(dev_opts, "driver", "sclpconsole", &error_abort);
2652 snprintf(label, sizeof(label), "sclpcon%d", index);
2653 sclp_hds[index] = qemu_chr_new(label, devname);
2654 if (!sclp_hds[index]) {
2655 error_report("could not connect sclp console"
2656 " to character backend '%s'", devname);
2657 return -1;
2659 qemu_opt_set(dev_opts, "chardev", label, &error_abort);
2661 index++;
2662 return 0;
2665 static int debugcon_parse(const char *devname)
2667 QemuOpts *opts;
2669 if (!qemu_chr_new("debugcon", devname)) {
2670 exit(1);
2672 opts = qemu_opts_create(qemu_find_opts("device"), "debugcon", 1, NULL);
2673 if (!opts) {
2674 error_report("already have a debugcon device");
2675 exit(1);
2677 qemu_opt_set(opts, "driver", "isa-debugcon", &error_abort);
2678 qemu_opt_set(opts, "chardev", "debugcon", &error_abort);
2679 return 0;
2682 static gint machine_class_cmp(gconstpointer a, gconstpointer b)
2684 const MachineClass *mc1 = a, *mc2 = b;
2685 int res;
2687 if (mc1->family == NULL) {
2688 if (mc2->family == NULL) {
2689 /* Compare standalone machine types against each other; they sort
2690 * in increasing order.
2692 return strcmp(object_class_get_name(OBJECT_CLASS(mc1)),
2693 object_class_get_name(OBJECT_CLASS(mc2)));
2696 /* Standalone machine types sort after families. */
2697 return 1;
2700 if (mc2->family == NULL) {
2701 /* Families sort before standalone machine types. */
2702 return -1;
2705 /* Families sort between each other alphabetically increasingly. */
2706 res = strcmp(mc1->family, mc2->family);
2707 if (res != 0) {
2708 return res;
2711 /* Within the same family, machine types sort in decreasing order. */
2712 return strcmp(object_class_get_name(OBJECT_CLASS(mc2)),
2713 object_class_get_name(OBJECT_CLASS(mc1)));
2716 static MachineClass *machine_parse(const char *name)
2718 MachineClass *mc = NULL;
2719 GSList *el, *machines = object_class_get_list(TYPE_MACHINE, false);
2721 if (name) {
2722 mc = find_machine(name);
2724 if (mc) {
2725 g_slist_free(machines);
2726 return mc;
2728 if (name && !is_help_option(name)) {
2729 error_report("unsupported machine type");
2730 error_printf("Use -machine help to list supported machines\n");
2731 } else {
2732 printf("Supported machines are:\n");
2733 machines = g_slist_sort(machines, machine_class_cmp);
2734 for (el = machines; el; el = el->next) {
2735 MachineClass *mc = el->data;
2736 if (mc->alias) {
2737 printf("%-20s %s (alias of %s)\n", mc->alias, mc->desc, mc->name);
2739 printf("%-20s %s%s\n", mc->name, mc->desc,
2740 mc->is_default ? " (default)" : "");
2744 g_slist_free(machines);
2745 exit(!name || !is_help_option(name));
2748 void qemu_add_exit_notifier(Notifier *notify)
2750 notifier_list_add(&exit_notifiers, notify);
2753 void qemu_remove_exit_notifier(Notifier *notify)
2755 notifier_remove(notify);
2758 static void qemu_run_exit_notifiers(void)
2760 notifier_list_notify(&exit_notifiers, NULL);
2763 static bool machine_init_done;
2765 void qemu_add_machine_init_done_notifier(Notifier *notify)
2767 notifier_list_add(&machine_init_done_notifiers, notify);
2768 if (machine_init_done) {
2769 notify->notify(notify, NULL);
2773 void qemu_remove_machine_init_done_notifier(Notifier *notify)
2775 notifier_remove(notify);
2778 static void qemu_run_machine_init_done_notifiers(void)
2780 notifier_list_notify(&machine_init_done_notifiers, NULL);
2781 machine_init_done = true;
2784 static const QEMUOption *lookup_opt(int argc, char **argv,
2785 const char **poptarg, int *poptind)
2787 const QEMUOption *popt;
2788 int optind = *poptind;
2789 char *r = argv[optind];
2790 const char *optarg;
2792 loc_set_cmdline(argv, optind, 1);
2793 optind++;
2794 /* Treat --foo the same as -foo. */
2795 if (r[1] == '-')
2796 r++;
2797 popt = qemu_options;
2798 for(;;) {
2799 if (!popt->name) {
2800 error_report("invalid option");
2801 exit(1);
2803 if (!strcmp(popt->name, r + 1))
2804 break;
2805 popt++;
2807 if (popt->flags & HAS_ARG) {
2808 if (optind >= argc) {
2809 error_report("requires an argument");
2810 exit(1);
2812 optarg = argv[optind++];
2813 loc_set_cmdline(argv, optind - 2, 2);
2814 } else {
2815 optarg = NULL;
2818 *poptarg = optarg;
2819 *poptind = optind;
2821 return popt;
2824 static MachineClass *select_machine(void)
2826 MachineClass *machine_class = find_default_machine();
2827 const char *optarg;
2828 QemuOpts *opts;
2829 Location loc;
2831 loc_push_none(&loc);
2833 opts = qemu_get_machine_opts();
2834 qemu_opts_loc_restore(opts);
2836 optarg = qemu_opt_get(opts, "type");
2837 if (optarg) {
2838 machine_class = machine_parse(optarg);
2841 if (!machine_class) {
2842 error_report("No machine specified, and there is no default");
2843 error_printf("Use -machine help to list supported machines\n");
2844 exit(1);
2847 loc_pop(&loc);
2848 return machine_class;
2851 static int machine_set_property(void *opaque,
2852 const char *name, const char *value,
2853 Error **errp)
2855 Object *obj = OBJECT(opaque);
2856 Error *local_err = NULL;
2857 char *p, *qom_name;
2859 if (strcmp(name, "type") == 0) {
2860 return 0;
2863 qom_name = g_strdup(name);
2864 for (p = qom_name; *p; p++) {
2865 if (*p == '_') {
2866 *p = '-';
2870 object_property_parse(obj, value, qom_name, &local_err);
2871 g_free(qom_name);
2873 if (local_err) {
2874 error_report_err(local_err);
2875 return -1;
2878 return 0;
2883 * Initial object creation happens before all other
2884 * QEMU data types are created. The majority of objects
2885 * can be created at this point. The rng-egd object
2886 * cannot be created here, as it depends on the chardev
2887 * already existing.
2889 static bool object_create_initial(const char *type)
2891 if (g_str_equal(type, "rng-egd")) {
2892 return false;
2896 * return false for concrete netfilters since
2897 * they depend on netdevs already existing
2899 if (g_str_equal(type, "filter-buffer") ||
2900 g_str_equal(type, "filter-dump") ||
2901 g_str_equal(type, "filter-mirror") ||
2902 g_str_equal(type, "filter-redirector") ||
2903 g_str_equal(type, "colo-compare") ||
2904 g_str_equal(type, "filter-rewriter") ||
2905 g_str_equal(type, "filter-replay")) {
2906 return false;
2909 /* Memory allocation by backends needs to be done
2910 * after configure_accelerator() (due to the tcg_enabled()
2911 * checks at memory_region_init_*()).
2913 * Also, allocation of large amounts of memory may delay
2914 * chardev initialization for too long, and trigger timeouts
2915 * on software that waits for a monitor socket to be created
2916 * (e.g. libvirt).
2918 if (g_str_has_prefix(type, "memory-backend-")) {
2919 return false;
2922 return true;
2927 * The remainder of object creation happens after the
2928 * creation of chardev, fsdev, net clients and device data types.
2930 static bool object_create_delayed(const char *type)
2932 return !object_create_initial(type);
2936 static void set_memory_options(uint64_t *ram_slots, ram_addr_t *maxram_size,
2937 MachineClass *mc)
2939 uint64_t sz;
2940 const char *mem_str;
2941 const char *maxmem_str, *slots_str;
2942 const ram_addr_t default_ram_size = mc->default_ram_size;
2943 QemuOpts *opts = qemu_find_opts_singleton("memory");
2944 Location loc;
2946 loc_push_none(&loc);
2947 qemu_opts_loc_restore(opts);
2949 sz = 0;
2950 mem_str = qemu_opt_get(opts, "size");
2951 if (mem_str) {
2952 if (!*mem_str) {
2953 error_report("missing 'size' option value");
2954 exit(EXIT_FAILURE);
2957 sz = qemu_opt_get_size(opts, "size", ram_size);
2959 /* Fix up legacy suffix-less format */
2960 if (g_ascii_isdigit(mem_str[strlen(mem_str) - 1])) {
2961 uint64_t overflow_check = sz;
2963 sz <<= 20;
2964 if ((sz >> 20) != overflow_check) {
2965 error_report("too large 'size' option value");
2966 exit(EXIT_FAILURE);
2971 /* backward compatibility behaviour for case "-m 0" */
2972 if (sz == 0) {
2973 sz = default_ram_size;
2976 sz = QEMU_ALIGN_UP(sz, 8192);
2977 ram_size = sz;
2978 if (ram_size != sz) {
2979 error_report("ram size too large");
2980 exit(EXIT_FAILURE);
2983 /* store value for the future use */
2984 qemu_opt_set_number(opts, "size", ram_size, &error_abort);
2985 *maxram_size = ram_size;
2987 maxmem_str = qemu_opt_get(opts, "maxmem");
2988 slots_str = qemu_opt_get(opts, "slots");
2989 if (maxmem_str && slots_str) {
2990 uint64_t slots;
2992 sz = qemu_opt_get_size(opts, "maxmem", 0);
2993 slots = qemu_opt_get_number(opts, "slots", 0);
2994 if (sz < ram_size) {
2995 error_report("invalid value of -m option maxmem: "
2996 "maximum memory size (0x%" PRIx64 ") must be at least "
2997 "the initial memory size (0x" RAM_ADDR_FMT ")",
2998 sz, ram_size);
2999 exit(EXIT_FAILURE);
3000 } else if (sz > ram_size) {
3001 if (!slots) {
3002 error_report("invalid value of -m option: maxmem was "
3003 "specified, but no hotplug slots were specified");
3004 exit(EXIT_FAILURE);
3006 } else if (slots) {
3007 error_report("invalid value of -m option maxmem: "
3008 "memory slots were specified but maximum memory size "
3009 "(0x%" PRIx64 ") is equal to the initial memory size "
3010 "(0x" RAM_ADDR_FMT ")", sz, ram_size);
3011 exit(EXIT_FAILURE);
3014 *maxram_size = sz;
3015 *ram_slots = slots;
3016 } else if ((!maxmem_str && slots_str) ||
3017 (maxmem_str && !slots_str)) {
3018 error_report("invalid -m option value: missing "
3019 "'%s' option", slots_str ? "maxmem" : "slots");
3020 exit(EXIT_FAILURE);
3023 loc_pop(&loc);
3026 static int global_init_func(void *opaque, QemuOpts *opts, Error **errp)
3028 GlobalProperty *g;
3030 g = g_malloc0(sizeof(*g));
3031 g->driver = qemu_opt_get(opts, "driver");
3032 g->property = qemu_opt_get(opts, "property");
3033 g->value = qemu_opt_get(opts, "value");
3034 g->user_provided = true;
3035 g->errp = &error_fatal;
3036 qdev_prop_register_global(g);
3037 return 0;
3040 int main(int argc, char **argv)
3042 int i;
3043 int snapshot, linux_boot;
3044 const char *initrd_filename;
3045 const char *kernel_filename, *kernel_cmdline;
3046 const char *boot_order = NULL;
3047 const char *boot_once = NULL;
3048 DisplayState *ds;
3049 int cyls, heads, secs, translation;
3050 QemuOpts *hda_opts = NULL, *opts, *machine_opts, *icount_opts = NULL;
3051 QemuOptsList *olist;
3052 int optind;
3053 const char *optarg;
3054 const char *loadvm = NULL;
3055 MachineClass *machine_class;
3056 const char *cpu_model;
3057 const char *vga_model = NULL;
3058 const char *qtest_chrdev = NULL;
3059 const char *qtest_log = NULL;
3060 const char *pid_file = NULL;
3061 const char *incoming = NULL;
3062 bool defconfig = true;
3063 bool userconfig = true;
3064 bool nographic = false;
3065 DisplayType display_type = DT_DEFAULT;
3066 int display_remote = 0;
3067 const char *log_mask = NULL;
3068 const char *log_file = NULL;
3069 char *trace_file = NULL;
3070 ram_addr_t maxram_size;
3071 uint64_t ram_slots = 0;
3072 FILE *vmstate_dump_file = NULL;
3073 Error *main_loop_err = NULL;
3074 Error *err = NULL;
3075 bool list_data_dirs = false;
3077 module_call_init(MODULE_INIT_TRACE);
3079 qemu_init_cpu_list();
3080 qemu_init_cpu_loop();
3081 qemu_mutex_lock_iothread();
3083 atexit(qemu_run_exit_notifiers);
3084 error_set_progname(argv[0]);
3085 qemu_init_exec_dir(argv[0]);
3087 module_call_init(MODULE_INIT_QOM);
3088 module_call_init(MODULE_INIT_QAPI);
3090 qemu_add_opts(&qemu_drive_opts);
3091 qemu_add_drive_opts(&qemu_legacy_drive_opts);
3092 qemu_add_drive_opts(&qemu_common_drive_opts);
3093 qemu_add_drive_opts(&qemu_drive_opts);
3094 qemu_add_drive_opts(&bdrv_runtime_opts);
3095 qemu_add_opts(&qemu_chardev_opts);
3096 qemu_add_opts(&qemu_device_opts);
3097 qemu_add_opts(&qemu_netdev_opts);
3098 qemu_add_opts(&qemu_net_opts);
3099 qemu_add_opts(&qemu_rtc_opts);
3100 qemu_add_opts(&qemu_global_opts);
3101 qemu_add_opts(&qemu_mon_opts);
3102 qemu_add_opts(&qemu_trace_opts);
3103 qemu_add_opts(&qemu_option_rom_opts);
3104 qemu_add_opts(&qemu_machine_opts);
3105 qemu_add_opts(&qemu_mem_opts);
3106 qemu_add_opts(&qemu_smp_opts);
3107 qemu_add_opts(&qemu_boot_opts);
3108 qemu_add_opts(&qemu_sandbox_opts);
3109 qemu_add_opts(&qemu_add_fd_opts);
3110 qemu_add_opts(&qemu_object_opts);
3111 qemu_add_opts(&qemu_tpmdev_opts);
3112 qemu_add_opts(&qemu_realtime_opts);
3113 qemu_add_opts(&qemu_msg_opts);
3114 qemu_add_opts(&qemu_name_opts);
3115 qemu_add_opts(&qemu_numa_opts);
3116 qemu_add_opts(&qemu_icount_opts);
3117 qemu_add_opts(&qemu_semihosting_config_opts);
3118 qemu_add_opts(&qemu_fw_cfg_opts);
3119 #ifdef CONFIG_LIBISCSI
3120 qemu_add_opts(&qemu_iscsi_opts);
3121 #endif
3122 module_call_init(MODULE_INIT_OPTS);
3124 runstate_init();
3126 if (qcrypto_init(&err) < 0) {
3127 error_reportf_err(err, "cannot initialize crypto: ");
3128 exit(1);
3130 rtc_clock = QEMU_CLOCK_HOST;
3132 QLIST_INIT (&vm_change_state_head);
3133 os_setup_early_signal_handling();
3135 cpu_model = NULL;
3136 snapshot = 0;
3137 cyls = heads = secs = 0;
3138 translation = BIOS_ATA_TRANSLATION_AUTO;
3140 nb_nics = 0;
3142 bdrv_init_with_whitelist();
3144 autostart = 1;
3146 /* first pass of option parsing */
3147 optind = 1;
3148 while (optind < argc) {
3149 if (argv[optind][0] != '-') {
3150 /* disk image */
3151 optind++;
3152 } else {
3153 const QEMUOption *popt;
3155 popt = lookup_opt(argc, argv, &optarg, &optind);
3156 switch (popt->index) {
3157 case QEMU_OPTION_nodefconfig:
3158 defconfig = false;
3159 break;
3160 case QEMU_OPTION_nouserconfig:
3161 userconfig = false;
3162 break;
3167 if (defconfig) {
3168 int ret;
3169 ret = qemu_read_default_config_files(userconfig);
3170 if (ret < 0) {
3171 exit(1);
3175 /* second pass of option parsing */
3176 optind = 1;
3177 for(;;) {
3178 if (optind >= argc)
3179 break;
3180 if (argv[optind][0] != '-') {
3181 hda_opts = drive_add(IF_DEFAULT, 0, argv[optind++], HD_OPTS);
3182 } else {
3183 const QEMUOption *popt;
3185 popt = lookup_opt(argc, argv, &optarg, &optind);
3186 if (!(popt->arch_mask & arch_type)) {
3187 error_report("Option not supported for this target");
3188 exit(1);
3190 switch(popt->index) {
3191 case QEMU_OPTION_no_kvm_irqchip: {
3192 olist = qemu_find_opts("machine");
3193 qemu_opts_parse_noisily(olist, "kernel_irqchip=off", false);
3194 break;
3196 case QEMU_OPTION_cpu:
3197 /* hw initialization will check this */
3198 cpu_model = optarg;
3199 break;
3200 case QEMU_OPTION_hda:
3202 char buf[256];
3203 if (cyls == 0)
3204 snprintf(buf, sizeof(buf), "%s", HD_OPTS);
3205 else
3206 snprintf(buf, sizeof(buf),
3207 "%s,cyls=%d,heads=%d,secs=%d%s",
3208 HD_OPTS , cyls, heads, secs,
3209 translation == BIOS_ATA_TRANSLATION_LBA ?
3210 ",trans=lba" :
3211 translation == BIOS_ATA_TRANSLATION_NONE ?
3212 ",trans=none" : "");
3213 drive_add(IF_DEFAULT, 0, optarg, buf);
3214 break;
3216 case QEMU_OPTION_hdb:
3217 case QEMU_OPTION_hdc:
3218 case QEMU_OPTION_hdd:
3219 drive_add(IF_DEFAULT, popt->index - QEMU_OPTION_hda, optarg,
3220 HD_OPTS);
3221 break;
3222 case QEMU_OPTION_drive:
3223 if (drive_def(optarg) == NULL) {
3224 exit(1);
3226 break;
3227 case QEMU_OPTION_set:
3228 if (qemu_set_option(optarg) != 0)
3229 exit(1);
3230 break;
3231 case QEMU_OPTION_global:
3232 if (qemu_global_option(optarg) != 0)
3233 exit(1);
3234 break;
3235 case QEMU_OPTION_mtdblock:
3236 drive_add(IF_MTD, -1, optarg, MTD_OPTS);
3237 break;
3238 case QEMU_OPTION_sd:
3239 drive_add(IF_SD, -1, optarg, SD_OPTS);
3240 break;
3241 case QEMU_OPTION_pflash:
3242 drive_add(IF_PFLASH, -1, optarg, PFLASH_OPTS);
3243 break;
3244 case QEMU_OPTION_snapshot:
3245 snapshot = 1;
3246 break;
3247 case QEMU_OPTION_hdachs:
3249 const char *p;
3250 p = optarg;
3251 cyls = strtol(p, (char **)&p, 0);
3252 if (cyls < 1 || cyls > 16383)
3253 goto chs_fail;
3254 if (*p != ',')
3255 goto chs_fail;
3256 p++;
3257 heads = strtol(p, (char **)&p, 0);
3258 if (heads < 1 || heads > 16)
3259 goto chs_fail;
3260 if (*p != ',')
3261 goto chs_fail;
3262 p++;
3263 secs = strtol(p, (char **)&p, 0);
3264 if (secs < 1 || secs > 63)
3265 goto chs_fail;
3266 if (*p == ',') {
3267 p++;
3268 if (!strcmp(p, "large")) {
3269 translation = BIOS_ATA_TRANSLATION_LARGE;
3270 } else if (!strcmp(p, "rechs")) {
3271 translation = BIOS_ATA_TRANSLATION_RECHS;
3272 } else if (!strcmp(p, "none")) {
3273 translation = BIOS_ATA_TRANSLATION_NONE;
3274 } else if (!strcmp(p, "lba")) {
3275 translation = BIOS_ATA_TRANSLATION_LBA;
3276 } else if (!strcmp(p, "auto")) {
3277 translation = BIOS_ATA_TRANSLATION_AUTO;
3278 } else {
3279 goto chs_fail;
3281 } else if (*p != '\0') {
3282 chs_fail:
3283 error_report("invalid physical CHS format");
3284 exit(1);
3286 if (hda_opts != NULL) {
3287 qemu_opt_set_number(hda_opts, "cyls", cyls,
3288 &error_abort);
3289 qemu_opt_set_number(hda_opts, "heads", heads,
3290 &error_abort);
3291 qemu_opt_set_number(hda_opts, "secs", secs,
3292 &error_abort);
3293 if (translation == BIOS_ATA_TRANSLATION_LARGE) {
3294 qemu_opt_set(hda_opts, "trans", "large",
3295 &error_abort);
3296 } else if (translation == BIOS_ATA_TRANSLATION_RECHS) {
3297 qemu_opt_set(hda_opts, "trans", "rechs",
3298 &error_abort);
3299 } else if (translation == BIOS_ATA_TRANSLATION_LBA) {
3300 qemu_opt_set(hda_opts, "trans", "lba",
3301 &error_abort);
3302 } else if (translation == BIOS_ATA_TRANSLATION_NONE) {
3303 qemu_opt_set(hda_opts, "trans", "none",
3304 &error_abort);
3308 break;
3309 case QEMU_OPTION_numa:
3310 opts = qemu_opts_parse_noisily(qemu_find_opts("numa"),
3311 optarg, true);
3312 if (!opts) {
3313 exit(1);
3315 break;
3316 case QEMU_OPTION_display:
3317 display_type = select_display(optarg);
3318 break;
3319 case QEMU_OPTION_nographic:
3320 olist = qemu_find_opts("machine");
3321 qemu_opts_parse_noisily(olist, "graphics=off", false);
3322 nographic = true;
3323 display_type = DT_NONE;
3324 break;
3325 case QEMU_OPTION_curses:
3326 #ifdef CONFIG_CURSES
3327 display_type = DT_CURSES;
3328 #else
3329 error_report("curses support is disabled");
3330 exit(1);
3331 #endif
3332 break;
3333 case QEMU_OPTION_portrait:
3334 graphic_rotate = 90;
3335 break;
3336 case QEMU_OPTION_rotate:
3337 graphic_rotate = strtol(optarg, (char **) &optarg, 10);
3338 if (graphic_rotate != 0 && graphic_rotate != 90 &&
3339 graphic_rotate != 180 && graphic_rotate != 270) {
3340 error_report("only 90, 180, 270 deg rotation is available");
3341 exit(1);
3343 break;
3344 case QEMU_OPTION_kernel:
3345 qemu_opts_set(qemu_find_opts("machine"), 0, "kernel", optarg,
3346 &error_abort);
3347 break;
3348 case QEMU_OPTION_initrd:
3349 qemu_opts_set(qemu_find_opts("machine"), 0, "initrd", optarg,
3350 &error_abort);
3351 break;
3352 case QEMU_OPTION_append:
3353 qemu_opts_set(qemu_find_opts("machine"), 0, "append", optarg,
3354 &error_abort);
3355 break;
3356 case QEMU_OPTION_dtb:
3357 qemu_opts_set(qemu_find_opts("machine"), 0, "dtb", optarg,
3358 &error_abort);
3359 break;
3360 case QEMU_OPTION_cdrom:
3361 drive_add(IF_DEFAULT, 2, optarg, CDROM_OPTS);
3362 break;
3363 case QEMU_OPTION_boot:
3364 opts = qemu_opts_parse_noisily(qemu_find_opts("boot-opts"),
3365 optarg, true);
3366 if (!opts) {
3367 exit(1);
3369 break;
3370 case QEMU_OPTION_fda:
3371 case QEMU_OPTION_fdb:
3372 drive_add(IF_FLOPPY, popt->index - QEMU_OPTION_fda,
3373 optarg, FD_OPTS);
3374 break;
3375 case QEMU_OPTION_no_fd_bootchk:
3376 fd_bootchk = 0;
3377 break;
3378 case QEMU_OPTION_netdev:
3379 default_net = 0;
3380 if (net_client_parse(qemu_find_opts("netdev"), optarg) == -1) {
3381 exit(1);
3383 break;
3384 case QEMU_OPTION_net:
3385 default_net = 0;
3386 if (net_client_parse(qemu_find_opts("net"), optarg) == -1) {
3387 exit(1);
3389 break;
3390 #ifdef CONFIG_LIBISCSI
3391 case QEMU_OPTION_iscsi:
3392 opts = qemu_opts_parse_noisily(qemu_find_opts("iscsi"),
3393 optarg, false);
3394 if (!opts) {
3395 exit(1);
3397 break;
3398 #endif
3399 #ifdef CONFIG_SLIRP
3400 case QEMU_OPTION_tftp:
3401 error_report("The -tftp option is deprecated. "
3402 "Please use '-netdev user,tftp=...' instead.");
3403 legacy_tftp_prefix = optarg;
3404 break;
3405 case QEMU_OPTION_bootp:
3406 error_report("The -bootp option is deprecated. "
3407 "Please use '-netdev user,bootfile=...' instead.");
3408 legacy_bootp_filename = optarg;
3409 break;
3410 case QEMU_OPTION_redir:
3411 error_report("The -redir option is deprecated. "
3412 "Please use '-netdev user,hostfwd=...' instead.");
3413 if (net_slirp_redir(optarg) < 0)
3414 exit(1);
3415 break;
3416 #endif
3417 case QEMU_OPTION_bt:
3418 add_device_config(DEV_BT, optarg);
3419 break;
3420 case QEMU_OPTION_audio_help:
3421 AUD_help ();
3422 exit (0);
3423 break;
3424 case QEMU_OPTION_soundhw:
3425 select_soundhw (optarg);
3426 break;
3427 case QEMU_OPTION_h:
3428 help(0);
3429 break;
3430 case QEMU_OPTION_version:
3431 version();
3432 exit(0);
3433 break;
3434 case QEMU_OPTION_m:
3435 opts = qemu_opts_parse_noisily(qemu_find_opts("memory"),
3436 optarg, true);
3437 if (!opts) {
3438 exit(EXIT_FAILURE);
3440 break;
3441 #ifdef CONFIG_TPM
3442 case QEMU_OPTION_tpmdev:
3443 if (tpm_config_parse(qemu_find_opts("tpmdev"), optarg) < 0) {
3444 exit(1);
3446 break;
3447 #endif
3448 case QEMU_OPTION_mempath:
3449 mem_path = optarg;
3450 break;
3451 case QEMU_OPTION_mem_prealloc:
3452 mem_prealloc = 1;
3453 break;
3454 case QEMU_OPTION_d:
3455 log_mask = optarg;
3456 break;
3457 case QEMU_OPTION_D:
3458 log_file = optarg;
3459 break;
3460 case QEMU_OPTION_DFILTER:
3461 qemu_set_dfilter_ranges(optarg, &error_fatal);
3462 break;
3463 case QEMU_OPTION_s:
3464 add_device_config(DEV_GDB, "tcp::" DEFAULT_GDBSTUB_PORT);
3465 break;
3466 case QEMU_OPTION_gdb:
3467 add_device_config(DEV_GDB, optarg);
3468 break;
3469 case QEMU_OPTION_L:
3470 if (is_help_option(optarg)) {
3471 list_data_dirs = true;
3472 } else if (data_dir_idx < ARRAY_SIZE(data_dir)) {
3473 data_dir[data_dir_idx++] = optarg;
3475 break;
3476 case QEMU_OPTION_bios:
3477 qemu_opts_set(qemu_find_opts("machine"), 0, "firmware", optarg,
3478 &error_abort);
3479 break;
3480 case QEMU_OPTION_singlestep:
3481 singlestep = 1;
3482 break;
3483 case QEMU_OPTION_S:
3484 autostart = 0;
3485 break;
3486 case QEMU_OPTION_k:
3487 keyboard_layout = optarg;
3488 break;
3489 case QEMU_OPTION_localtime:
3490 rtc_utc = 0;
3491 break;
3492 case QEMU_OPTION_vga:
3493 vga_model = optarg;
3494 default_vga = 0;
3495 break;
3496 case QEMU_OPTION_g:
3498 const char *p;
3499 int w, h, depth;
3500 p = optarg;
3501 w = strtol(p, (char **)&p, 10);
3502 if (w <= 0) {
3503 graphic_error:
3504 error_report("invalid resolution or depth");
3505 exit(1);
3507 if (*p != 'x')
3508 goto graphic_error;
3509 p++;
3510 h = strtol(p, (char **)&p, 10);
3511 if (h <= 0)
3512 goto graphic_error;
3513 if (*p == 'x') {
3514 p++;
3515 depth = strtol(p, (char **)&p, 10);
3516 if (depth != 8 && depth != 15 && depth != 16 &&
3517 depth != 24 && depth != 32)
3518 goto graphic_error;
3519 } else if (*p == '\0') {
3520 depth = graphic_depth;
3521 } else {
3522 goto graphic_error;
3525 graphic_width = w;
3526 graphic_height = h;
3527 graphic_depth = depth;
3529 break;
3530 case QEMU_OPTION_echr:
3532 char *r;
3533 term_escape_char = strtol(optarg, &r, 0);
3534 if (r == optarg)
3535 printf("Bad argument to echr\n");
3536 break;
3538 case QEMU_OPTION_monitor:
3539 default_monitor = 0;
3540 if (strncmp(optarg, "none", 4)) {
3541 monitor_parse(optarg, "readline", false);
3543 break;
3544 case QEMU_OPTION_qmp:
3545 monitor_parse(optarg, "control", false);
3546 default_monitor = 0;
3547 break;
3548 case QEMU_OPTION_qmp_pretty:
3549 monitor_parse(optarg, "control", true);
3550 default_monitor = 0;
3551 break;
3552 case QEMU_OPTION_mon:
3553 opts = qemu_opts_parse_noisily(qemu_find_opts("mon"), optarg,
3554 true);
3555 if (!opts) {
3556 exit(1);
3558 default_monitor = 0;
3559 break;
3560 case QEMU_OPTION_chardev:
3561 opts = qemu_opts_parse_noisily(qemu_find_opts("chardev"),
3562 optarg, true);
3563 if (!opts) {
3564 exit(1);
3566 break;
3567 case QEMU_OPTION_fsdev:
3568 olist = qemu_find_opts("fsdev");
3569 if (!olist) {
3570 error_report("fsdev support is disabled");
3571 exit(1);
3573 opts = qemu_opts_parse_noisily(olist, optarg, true);
3574 if (!opts) {
3575 exit(1);
3577 break;
3578 case QEMU_OPTION_virtfs: {
3579 QemuOpts *fsdev;
3580 QemuOpts *device;
3581 const char *writeout, *sock_fd, *socket;
3583 olist = qemu_find_opts("virtfs");
3584 if (!olist) {
3585 error_report("virtfs support is disabled");
3586 exit(1);
3588 opts = qemu_opts_parse_noisily(olist, optarg, true);
3589 if (!opts) {
3590 exit(1);
3593 if (qemu_opt_get(opts, "fsdriver") == NULL ||
3594 qemu_opt_get(opts, "mount_tag") == NULL) {
3595 error_report("Usage: -virtfs fsdriver,mount_tag=tag");
3596 exit(1);
3598 fsdev = qemu_opts_create(qemu_find_opts("fsdev"),
3599 qemu_opt_get(opts, "mount_tag"),
3600 1, NULL);
3601 if (!fsdev) {
3602 error_report("duplicate fsdev id: %s",
3603 qemu_opt_get(opts, "mount_tag"));
3604 exit(1);
3607 writeout = qemu_opt_get(opts, "writeout");
3608 if (writeout) {
3609 #ifdef CONFIG_SYNC_FILE_RANGE
3610 qemu_opt_set(fsdev, "writeout", writeout, &error_abort);
3611 #else
3612 error_report("writeout=immediate not supported "
3613 "on this platform");
3614 exit(1);
3615 #endif
3617 qemu_opt_set(fsdev, "fsdriver",
3618 qemu_opt_get(opts, "fsdriver"), &error_abort);
3619 qemu_opt_set(fsdev, "path", qemu_opt_get(opts, "path"),
3620 &error_abort);
3621 qemu_opt_set(fsdev, "security_model",
3622 qemu_opt_get(opts, "security_model"),
3623 &error_abort);
3624 socket = qemu_opt_get(opts, "socket");
3625 if (socket) {
3626 qemu_opt_set(fsdev, "socket", socket, &error_abort);
3628 sock_fd = qemu_opt_get(opts, "sock_fd");
3629 if (sock_fd) {
3630 qemu_opt_set(fsdev, "sock_fd", sock_fd, &error_abort);
3633 qemu_opt_set_bool(fsdev, "readonly",
3634 qemu_opt_get_bool(opts, "readonly", 0),
3635 &error_abort);
3636 device = qemu_opts_create(qemu_find_opts("device"), NULL, 0,
3637 &error_abort);
3638 qemu_opt_set(device, "driver", "virtio-9p-pci", &error_abort);
3639 qemu_opt_set(device, "fsdev",
3640 qemu_opt_get(opts, "mount_tag"), &error_abort);
3641 qemu_opt_set(device, "mount_tag",
3642 qemu_opt_get(opts, "mount_tag"), &error_abort);
3643 break;
3645 case QEMU_OPTION_virtfs_synth: {
3646 QemuOpts *fsdev;
3647 QemuOpts *device;
3649 fsdev = qemu_opts_create(qemu_find_opts("fsdev"), "v_synth",
3650 1, NULL);
3651 if (!fsdev) {
3652 error_report("duplicate option: %s", "virtfs_synth");
3653 exit(1);
3655 qemu_opt_set(fsdev, "fsdriver", "synth", &error_abort);
3657 device = qemu_opts_create(qemu_find_opts("device"), NULL, 0,
3658 &error_abort);
3659 qemu_opt_set(device, "driver", "virtio-9p-pci", &error_abort);
3660 qemu_opt_set(device, "fsdev", "v_synth", &error_abort);
3661 qemu_opt_set(device, "mount_tag", "v_synth", &error_abort);
3662 break;
3664 case QEMU_OPTION_serial:
3665 add_device_config(DEV_SERIAL, optarg);
3666 default_serial = 0;
3667 if (strncmp(optarg, "mon:", 4) == 0) {
3668 default_monitor = 0;
3670 break;
3671 case QEMU_OPTION_watchdog:
3672 if (watchdog) {
3673 error_report("only one watchdog option may be given");
3674 return 1;
3676 watchdog = optarg;
3677 break;
3678 case QEMU_OPTION_watchdog_action:
3679 if (select_watchdog_action(optarg) == -1) {
3680 error_report("unknown -watchdog-action parameter");
3681 exit(1);
3683 break;
3684 case QEMU_OPTION_virtiocon:
3685 add_device_config(DEV_VIRTCON, optarg);
3686 default_virtcon = 0;
3687 if (strncmp(optarg, "mon:", 4) == 0) {
3688 default_monitor = 0;
3690 break;
3691 case QEMU_OPTION_parallel:
3692 add_device_config(DEV_PARALLEL, optarg);
3693 default_parallel = 0;
3694 if (strncmp(optarg, "mon:", 4) == 0) {
3695 default_monitor = 0;
3697 break;
3698 case QEMU_OPTION_debugcon:
3699 add_device_config(DEV_DEBUGCON, optarg);
3700 break;
3701 case QEMU_OPTION_loadvm:
3702 loadvm = optarg;
3703 break;
3704 case QEMU_OPTION_full_screen:
3705 full_screen = 1;
3706 break;
3707 case QEMU_OPTION_no_frame:
3708 no_frame = 1;
3709 break;
3710 case QEMU_OPTION_alt_grab:
3711 alt_grab = 1;
3712 break;
3713 case QEMU_OPTION_ctrl_grab:
3714 ctrl_grab = 1;
3715 break;
3716 case QEMU_OPTION_no_quit:
3717 no_quit = 1;
3718 break;
3719 case QEMU_OPTION_sdl:
3720 #ifdef CONFIG_SDL
3721 display_type = DT_SDL;
3722 break;
3723 #else
3724 error_report("SDL support is disabled");
3725 exit(1);
3726 #endif
3727 case QEMU_OPTION_pidfile:
3728 pid_file = optarg;
3729 break;
3730 case QEMU_OPTION_win2k_hack:
3731 win2k_install_hack = 1;
3732 break;
3733 case QEMU_OPTION_rtc_td_hack: {
3734 static GlobalProperty slew_lost_ticks = {
3735 .driver = "mc146818rtc",
3736 .property = "lost_tick_policy",
3737 .value = "slew",
3740 qdev_prop_register_global(&slew_lost_ticks);
3741 break;
3743 case QEMU_OPTION_acpitable:
3744 opts = qemu_opts_parse_noisily(qemu_find_opts("acpi"),
3745 optarg, true);
3746 if (!opts) {
3747 exit(1);
3749 do_acpitable_option(opts);
3750 break;
3751 case QEMU_OPTION_smbios:
3752 opts = qemu_opts_parse_noisily(qemu_find_opts("smbios"),
3753 optarg, false);
3754 if (!opts) {
3755 exit(1);
3757 do_smbios_option(opts);
3758 break;
3759 case QEMU_OPTION_fwcfg:
3760 opts = qemu_opts_parse_noisily(qemu_find_opts("fw_cfg"),
3761 optarg, true);
3762 if (opts == NULL) {
3763 exit(1);
3765 break;
3766 case QEMU_OPTION_enable_kvm:
3767 olist = qemu_find_opts("machine");
3768 qemu_opts_parse_noisily(olist, "accel=kvm", false);
3769 break;
3770 case QEMU_OPTION_enable_hax:
3771 olist = qemu_find_opts("machine");
3772 qemu_opts_parse_noisily(olist, "accel=hax", false);
3773 break;
3774 case QEMU_OPTION_M:
3775 case QEMU_OPTION_machine:
3776 olist = qemu_find_opts("machine");
3777 opts = qemu_opts_parse_noisily(olist, optarg, true);
3778 if (!opts) {
3779 exit(1);
3781 break;
3782 case QEMU_OPTION_no_kvm:
3783 olist = qemu_find_opts("machine");
3784 qemu_opts_parse_noisily(olist, "accel=tcg", false);
3785 break;
3786 case QEMU_OPTION_no_kvm_pit: {
3787 error_report("warning: ignoring deprecated option");
3788 break;
3790 case QEMU_OPTION_no_kvm_pit_reinjection: {
3791 static GlobalProperty kvm_pit_lost_tick_policy = {
3792 .driver = "kvm-pit",
3793 .property = "lost_tick_policy",
3794 .value = "discard",
3797 error_report("warning: deprecated, replaced by "
3798 "-global kvm-pit.lost_tick_policy=discard");
3799 qdev_prop_register_global(&kvm_pit_lost_tick_policy);
3800 break;
3802 case QEMU_OPTION_usb:
3803 olist = qemu_find_opts("machine");
3804 qemu_opts_parse_noisily(olist, "usb=on", false);
3805 break;
3806 case QEMU_OPTION_usbdevice:
3807 olist = qemu_find_opts("machine");
3808 qemu_opts_parse_noisily(olist, "usb=on", false);
3809 add_device_config(DEV_USB, optarg);
3810 break;
3811 case QEMU_OPTION_device:
3812 if (!qemu_opts_parse_noisily(qemu_find_opts("device"),
3813 optarg, true)) {
3814 exit(1);
3816 break;
3817 case QEMU_OPTION_smp:
3818 if (!qemu_opts_parse_noisily(qemu_find_opts("smp-opts"),
3819 optarg, true)) {
3820 exit(1);
3822 break;
3823 case QEMU_OPTION_vnc:
3824 vnc_parse(optarg, &error_fatal);
3825 break;
3826 case QEMU_OPTION_no_acpi:
3827 acpi_enabled = 0;
3828 break;
3829 case QEMU_OPTION_no_hpet:
3830 no_hpet = 1;
3831 break;
3832 case QEMU_OPTION_balloon:
3833 if (balloon_parse(optarg) < 0) {
3834 error_report("unknown -balloon argument %s", optarg);
3835 exit(1);
3837 break;
3838 case QEMU_OPTION_no_reboot:
3839 no_reboot = 1;
3840 break;
3841 case QEMU_OPTION_no_shutdown:
3842 no_shutdown = 1;
3843 break;
3844 case QEMU_OPTION_show_cursor:
3845 cursor_hide = 0;
3846 break;
3847 case QEMU_OPTION_uuid:
3848 if (qemu_uuid_parse(optarg, &qemu_uuid) < 0) {
3849 error_report("failed to parse UUID string: wrong format");
3850 exit(1);
3852 qemu_uuid_set = true;
3853 break;
3854 case QEMU_OPTION_option_rom:
3855 if (nb_option_roms >= MAX_OPTION_ROMS) {
3856 error_report("too many option ROMs");
3857 exit(1);
3859 opts = qemu_opts_parse_noisily(qemu_find_opts("option-rom"),
3860 optarg, true);
3861 if (!opts) {
3862 exit(1);
3864 option_rom[nb_option_roms].name = qemu_opt_get(opts, "romfile");
3865 option_rom[nb_option_roms].bootindex =
3866 qemu_opt_get_number(opts, "bootindex", -1);
3867 if (!option_rom[nb_option_roms].name) {
3868 error_report("Option ROM file is not specified");
3869 exit(1);
3871 nb_option_roms++;
3872 break;
3873 case QEMU_OPTION_semihosting:
3874 semihosting.enabled = true;
3875 semihosting.target = SEMIHOSTING_TARGET_AUTO;
3876 break;
3877 case QEMU_OPTION_semihosting_config:
3878 semihosting.enabled = true;
3879 opts = qemu_opts_parse_noisily(qemu_find_opts("semihosting-config"),
3880 optarg, false);
3881 if (opts != NULL) {
3882 semihosting.enabled = qemu_opt_get_bool(opts, "enable",
3883 true);
3884 const char *target = qemu_opt_get(opts, "target");
3885 if (target != NULL) {
3886 if (strcmp("native", target) == 0) {
3887 semihosting.target = SEMIHOSTING_TARGET_NATIVE;
3888 } else if (strcmp("gdb", target) == 0) {
3889 semihosting.target = SEMIHOSTING_TARGET_GDB;
3890 } else if (strcmp("auto", target) == 0) {
3891 semihosting.target = SEMIHOSTING_TARGET_AUTO;
3892 } else {
3893 error_report("unsupported semihosting-config %s",
3894 optarg);
3895 exit(1);
3897 } else {
3898 semihosting.target = SEMIHOSTING_TARGET_AUTO;
3900 /* Set semihosting argument count and vector */
3901 qemu_opt_foreach(opts, add_semihosting_arg,
3902 &semihosting, NULL);
3903 } else {
3904 error_report("unsupported semihosting-config %s", optarg);
3905 exit(1);
3907 break;
3908 case QEMU_OPTION_tdf:
3909 error_report("warning: ignoring deprecated option");
3910 break;
3911 case QEMU_OPTION_name:
3912 opts = qemu_opts_parse_noisily(qemu_find_opts("name"),
3913 optarg, true);
3914 if (!opts) {
3915 exit(1);
3917 break;
3918 case QEMU_OPTION_prom_env:
3919 if (nb_prom_envs >= MAX_PROM_ENVS) {
3920 error_report("too many prom variables");
3921 exit(1);
3923 prom_envs[nb_prom_envs] = optarg;
3924 nb_prom_envs++;
3925 break;
3926 case QEMU_OPTION_old_param:
3927 old_param = 1;
3928 break;
3929 case QEMU_OPTION_clock:
3930 /* Clock options no longer exist. Keep this option for
3931 * backward compatibility.
3933 break;
3934 case QEMU_OPTION_startdate:
3935 configure_rtc_date_offset(optarg, 1);
3936 break;
3937 case QEMU_OPTION_rtc:
3938 opts = qemu_opts_parse_noisily(qemu_find_opts("rtc"), optarg,
3939 false);
3940 if (!opts) {
3941 exit(1);
3943 configure_rtc(opts);
3944 break;
3945 case QEMU_OPTION_tb_size:
3946 tcg_tb_size = strtol(optarg, NULL, 0);
3947 if (tcg_tb_size < 0) {
3948 tcg_tb_size = 0;
3950 break;
3951 case QEMU_OPTION_icount:
3952 icount_opts = qemu_opts_parse_noisily(qemu_find_opts("icount"),
3953 optarg, true);
3954 if (!icount_opts) {
3955 exit(1);
3957 break;
3958 case QEMU_OPTION_incoming:
3959 if (!incoming) {
3960 runstate_set(RUN_STATE_INMIGRATE);
3962 incoming = optarg;
3963 break;
3964 case QEMU_OPTION_nodefaults:
3965 has_defaults = 0;
3966 break;
3967 case QEMU_OPTION_xen_domid:
3968 if (!(xen_available())) {
3969 error_report("Option not supported for this target");
3970 exit(1);
3972 xen_domid = atoi(optarg);
3973 break;
3974 case QEMU_OPTION_xen_create:
3975 if (!(xen_available())) {
3976 error_report("Option not supported for this target");
3977 exit(1);
3979 xen_mode = XEN_CREATE;
3980 break;
3981 case QEMU_OPTION_xen_attach:
3982 if (!(xen_available())) {
3983 error_report("Option not supported for this target");
3984 exit(1);
3986 xen_mode = XEN_ATTACH;
3987 break;
3988 case QEMU_OPTION_trace:
3989 g_free(trace_file);
3990 trace_file = trace_opt_parse(optarg);
3991 break;
3992 case QEMU_OPTION_trace_unassigned:
3993 trace_unassigned = true;
3994 break;
3995 case QEMU_OPTION_readconfig:
3997 int ret = qemu_read_config_file(optarg);
3998 if (ret < 0) {
3999 error_report("read config %s: %s", optarg,
4000 strerror(-ret));
4001 exit(1);
4003 break;
4005 case QEMU_OPTION_spice:
4006 olist = qemu_find_opts("spice");
4007 if (!olist) {
4008 error_report("spice support is disabled");
4009 exit(1);
4011 opts = qemu_opts_parse_noisily(olist, optarg, false);
4012 if (!opts) {
4013 exit(1);
4015 display_remote++;
4016 break;
4017 case QEMU_OPTION_writeconfig:
4019 FILE *fp;
4020 if (strcmp(optarg, "-") == 0) {
4021 fp = stdout;
4022 } else {
4023 fp = fopen(optarg, "w");
4024 if (fp == NULL) {
4025 error_report("open %s: %s", optarg,
4026 strerror(errno));
4027 exit(1);
4030 qemu_config_write(fp);
4031 if (fp != stdout) {
4032 fclose(fp);
4034 break;
4036 case QEMU_OPTION_qtest:
4037 qtest_chrdev = optarg;
4038 break;
4039 case QEMU_OPTION_qtest_log:
4040 qtest_log = optarg;
4041 break;
4042 case QEMU_OPTION_sandbox:
4043 opts = qemu_opts_parse_noisily(qemu_find_opts("sandbox"),
4044 optarg, true);
4045 if (!opts) {
4046 exit(1);
4048 break;
4049 case QEMU_OPTION_add_fd:
4050 #ifndef _WIN32
4051 opts = qemu_opts_parse_noisily(qemu_find_opts("add-fd"),
4052 optarg, false);
4053 if (!opts) {
4054 exit(1);
4056 #else
4057 error_report("File descriptor passing is disabled on this "
4058 "platform");
4059 exit(1);
4060 #endif
4061 break;
4062 case QEMU_OPTION_object:
4063 opts = qemu_opts_parse_noisily(qemu_find_opts("object"),
4064 optarg, true);
4065 if (!opts) {
4066 exit(1);
4068 break;
4069 case QEMU_OPTION_realtime:
4070 opts = qemu_opts_parse_noisily(qemu_find_opts("realtime"),
4071 optarg, false);
4072 if (!opts) {
4073 exit(1);
4075 enable_mlock = qemu_opt_get_bool(opts, "mlock", true);
4076 break;
4077 case QEMU_OPTION_msg:
4078 opts = qemu_opts_parse_noisily(qemu_find_opts("msg"), optarg,
4079 false);
4080 if (!opts) {
4081 exit(1);
4083 configure_msg(opts);
4084 break;
4085 case QEMU_OPTION_dump_vmstate:
4086 if (vmstate_dump_file) {
4087 error_report("only one '-dump-vmstate' "
4088 "option may be given");
4089 exit(1);
4091 vmstate_dump_file = fopen(optarg, "w");
4092 if (vmstate_dump_file == NULL) {
4093 error_report("open %s: %s", optarg, strerror(errno));
4094 exit(1);
4096 break;
4097 default:
4098 os_parse_cmd_args(popt->index, optarg);
4103 * Clear error location left behind by the loop.
4104 * Best done right after the loop. Do not insert code here!
4106 loc_set_none();
4108 replay_configure(icount_opts);
4110 machine_class = select_machine();
4112 set_memory_options(&ram_slots, &maxram_size, machine_class);
4114 os_daemonize();
4116 if (pid_file && qemu_create_pidfile(pid_file) != 0) {
4117 error_report("could not acquire pid file: %s", strerror(errno));
4118 exit(1);
4121 if (qemu_init_main_loop(&main_loop_err)) {
4122 error_report_err(main_loop_err);
4123 exit(1);
4126 if (qemu_opts_foreach(qemu_find_opts("sandbox"),
4127 parse_sandbox, NULL, NULL)) {
4128 exit(1);
4131 if (qemu_opts_foreach(qemu_find_opts("name"),
4132 parse_name, NULL, NULL)) {
4133 exit(1);
4136 #ifndef _WIN32
4137 if (qemu_opts_foreach(qemu_find_opts("add-fd"),
4138 parse_add_fd, NULL, NULL)) {
4139 exit(1);
4142 if (qemu_opts_foreach(qemu_find_opts("add-fd"),
4143 cleanup_add_fd, NULL, NULL)) {
4144 exit(1);
4146 #endif
4148 current_machine = MACHINE(object_new(object_class_get_name(
4149 OBJECT_CLASS(machine_class))));
4150 if (machine_help_func(qemu_get_machine_opts(), current_machine)) {
4151 exit(0);
4153 object_property_add_child(object_get_root(), "machine",
4154 OBJECT(current_machine), &error_abort);
4156 if (machine_class->minimum_page_bits) {
4157 if (!set_preferred_target_page_bits(machine_class->minimum_page_bits)) {
4158 /* This would be a board error: specifying a minimum smaller than
4159 * a target's compile-time fixed setting.
4161 g_assert_not_reached();
4165 cpu_exec_init_all();
4167 if (machine_class->hw_version) {
4168 qemu_set_hw_version(machine_class->hw_version);
4171 if (cpu_model && is_help_option(cpu_model)) {
4172 list_cpus(stdout, &fprintf, cpu_model);
4173 exit(0);
4176 if (!trace_init_backends()) {
4177 exit(1);
4179 trace_init_file(trace_file);
4181 /* Open the logfile at this point and set the log mask if necessary.
4183 if (log_file) {
4184 qemu_set_log_filename(log_file, &error_fatal);
4187 if (log_mask) {
4188 int mask;
4189 mask = qemu_str_to_log_mask(log_mask);
4190 if (!mask) {
4191 qemu_print_log_usage(stdout);
4192 exit(1);
4194 qemu_set_log(mask);
4195 } else {
4196 qemu_set_log(0);
4199 /* If no data_dir is specified then try to find it relative to the
4200 executable path. */
4201 if (data_dir_idx < ARRAY_SIZE(data_dir)) {
4202 data_dir[data_dir_idx] = os_find_datadir();
4203 if (data_dir[data_dir_idx] != NULL) {
4204 data_dir_idx++;
4207 /* If all else fails use the install path specified when building. */
4208 if (data_dir_idx < ARRAY_SIZE(data_dir)) {
4209 data_dir[data_dir_idx++] = CONFIG_QEMU_DATADIR;
4212 /* -L help lists the data directories and exits. */
4213 if (list_data_dirs) {
4214 for (i = 0; i < data_dir_idx; i++) {
4215 printf("%s\n", data_dir[i]);
4217 exit(0);
4220 smp_parse(qemu_opts_find(qemu_find_opts("smp-opts"), NULL));
4222 machine_class->max_cpus = machine_class->max_cpus ?: 1; /* Default to UP */
4223 if (max_cpus > machine_class->max_cpus) {
4224 error_report("Number of SMP CPUs requested (%d) exceeds max CPUs "
4225 "supported by machine '%s' (%d)", max_cpus,
4226 machine_class->name, machine_class->max_cpus);
4227 exit(1);
4231 * Get the default machine options from the machine if it is not already
4232 * specified either by the configuration file or by the command line.
4234 if (machine_class->default_machine_opts) {
4235 qemu_opts_set_defaults(qemu_find_opts("machine"),
4236 machine_class->default_machine_opts, 0);
4239 qemu_opts_foreach(qemu_find_opts("device"),
4240 default_driver_check, NULL, NULL);
4241 qemu_opts_foreach(qemu_find_opts("global"),
4242 default_driver_check, NULL, NULL);
4244 if (!vga_model && !default_vga) {
4245 vga_interface_type = VGA_DEVICE;
4247 if (!has_defaults || machine_class->no_serial) {
4248 default_serial = 0;
4250 if (!has_defaults || machine_class->no_parallel) {
4251 default_parallel = 0;
4253 if (!has_defaults || !machine_class->use_virtcon) {
4254 default_virtcon = 0;
4256 if (!has_defaults || !machine_class->use_sclp) {
4257 default_sclp = 0;
4259 if (!has_defaults || machine_class->no_floppy) {
4260 default_floppy = 0;
4262 if (!has_defaults || machine_class->no_cdrom) {
4263 default_cdrom = 0;
4265 if (!has_defaults || machine_class->no_sdcard) {
4266 default_sdcard = 0;
4268 if (!has_defaults) {
4269 default_monitor = 0;
4270 default_net = 0;
4271 default_vga = 0;
4274 if (is_daemonized()) {
4275 /* According to documentation and historically, -nographic redirects
4276 * serial port, parallel port and monitor to stdio, which does not work
4277 * with -daemonize. We can redirect these to null instead, but since
4278 * -nographic is legacy, let's just error out.
4279 * We disallow -nographic only if all other ports are not redirected
4280 * explicitly, to not break existing legacy setups which uses
4281 * -nographic _and_ redirects all ports explicitly - this is valid
4282 * usage, -nographic is just a no-op in this case.
4284 if (nographic
4285 && (default_parallel || default_serial
4286 || default_monitor || default_virtcon)) {
4287 error_report("-nographic cannot be used with -daemonize");
4288 exit(1);
4290 #ifdef CONFIG_CURSES
4291 if (display_type == DT_CURSES) {
4292 error_report("curses display cannot be used with -daemonize");
4293 exit(1);
4295 #endif
4298 if (nographic) {
4299 if (default_parallel)
4300 add_device_config(DEV_PARALLEL, "null");
4301 if (default_serial && default_monitor) {
4302 add_device_config(DEV_SERIAL, "mon:stdio");
4303 } else if (default_virtcon && default_monitor) {
4304 add_device_config(DEV_VIRTCON, "mon:stdio");
4305 } else if (default_sclp && default_monitor) {
4306 add_device_config(DEV_SCLP, "mon:stdio");
4307 } else {
4308 if (default_serial)
4309 add_device_config(DEV_SERIAL, "stdio");
4310 if (default_virtcon)
4311 add_device_config(DEV_VIRTCON, "stdio");
4312 if (default_sclp) {
4313 add_device_config(DEV_SCLP, "stdio");
4315 if (default_monitor)
4316 monitor_parse("stdio", "readline", false);
4318 } else {
4319 if (default_serial)
4320 add_device_config(DEV_SERIAL, "vc:80Cx24C");
4321 if (default_parallel)
4322 add_device_config(DEV_PARALLEL, "vc:80Cx24C");
4323 if (default_monitor)
4324 monitor_parse("vc:80Cx24C", "readline", false);
4325 if (default_virtcon)
4326 add_device_config(DEV_VIRTCON, "vc:80Cx24C");
4327 if (default_sclp) {
4328 add_device_config(DEV_SCLP, "vc:80Cx24C");
4332 #if defined(CONFIG_VNC)
4333 if (!QTAILQ_EMPTY(&(qemu_find_opts("vnc")->head))) {
4334 display_remote++;
4336 #endif
4337 if (display_type == DT_DEFAULT && !display_remote) {
4338 #if defined(CONFIG_GTK)
4339 display_type = DT_GTK;
4340 #elif defined(CONFIG_SDL)
4341 display_type = DT_SDL;
4342 #elif defined(CONFIG_COCOA)
4343 display_type = DT_COCOA;
4344 #elif defined(CONFIG_VNC)
4345 vnc_parse("localhost:0,to=99,id=default", &error_abort);
4346 #else
4347 display_type = DT_NONE;
4348 #endif
4351 if ((no_frame || alt_grab || ctrl_grab) && display_type != DT_SDL) {
4352 error_report("-no-frame, -alt-grab and -ctrl-grab are only valid "
4353 "for SDL, ignoring option");
4355 if (no_quit && (display_type != DT_GTK && display_type != DT_SDL)) {
4356 error_report("-no-quit is only valid for GTK and SDL, "
4357 "ignoring option");
4360 if (display_type == DT_GTK) {
4361 early_gtk_display_init(request_opengl);
4364 if (display_type == DT_SDL) {
4365 sdl_display_early_init(request_opengl);
4368 if (request_opengl == 1 && display_opengl == 0) {
4369 #if defined(CONFIG_OPENGL)
4370 error_report("OpenGL is not supported by the display");
4371 #else
4372 error_report("OpenGL support is disabled");
4373 #endif
4374 exit(1);
4377 page_size_init();
4378 socket_init();
4380 if (qemu_opts_foreach(qemu_find_opts("object"),
4381 user_creatable_add_opts_foreach,
4382 object_create_initial, NULL)) {
4383 exit(1);
4386 if (qemu_opts_foreach(qemu_find_opts("chardev"),
4387 chardev_init_func, NULL, NULL)) {
4388 exit(1);
4391 #ifdef CONFIG_VIRTFS
4392 if (qemu_opts_foreach(qemu_find_opts("fsdev"),
4393 fsdev_init_func, NULL, NULL)) {
4394 exit(1);
4396 #endif
4398 if (qemu_opts_foreach(qemu_find_opts("device"),
4399 device_help_func, NULL, NULL)) {
4400 exit(0);
4403 machine_opts = qemu_get_machine_opts();
4404 if (qemu_opt_foreach(machine_opts, machine_set_property, current_machine,
4405 NULL)) {
4406 object_unref(OBJECT(current_machine));
4407 exit(1);
4410 configure_accelerator(current_machine);
4412 if (qtest_chrdev) {
4413 qtest_init(qtest_chrdev, qtest_log, &error_fatal);
4416 machine_opts = qemu_get_machine_opts();
4417 kernel_filename = qemu_opt_get(machine_opts, "kernel");
4418 initrd_filename = qemu_opt_get(machine_opts, "initrd");
4419 kernel_cmdline = qemu_opt_get(machine_opts, "append");
4420 bios_name = qemu_opt_get(machine_opts, "firmware");
4422 opts = qemu_opts_find(qemu_find_opts("boot-opts"), NULL);
4423 if (opts) {
4424 boot_order = qemu_opt_get(opts, "order");
4425 if (boot_order) {
4426 validate_bootdevices(boot_order, &error_fatal);
4429 boot_once = qemu_opt_get(opts, "once");
4430 if (boot_once) {
4431 validate_bootdevices(boot_once, &error_fatal);
4434 boot_menu = qemu_opt_get_bool(opts, "menu", boot_menu);
4435 boot_strict = qemu_opt_get_bool(opts, "strict", false);
4438 if (!boot_order) {
4439 boot_order = machine_class->default_boot_order;
4442 if (!kernel_cmdline) {
4443 kernel_cmdline = "";
4444 current_machine->kernel_cmdline = (char *)kernel_cmdline;
4447 linux_boot = (kernel_filename != NULL);
4449 if (!linux_boot && *kernel_cmdline != '\0') {
4450 error_report("-append only allowed with -kernel option");
4451 exit(1);
4454 if (!linux_boot && initrd_filename != NULL) {
4455 error_report("-initrd only allowed with -kernel option");
4456 exit(1);
4459 if (semihosting_enabled() && !semihosting_get_argc() && kernel_filename) {
4460 /* fall back to the -kernel/-append */
4461 semihosting_arg_fallback(kernel_filename, kernel_cmdline);
4464 os_set_line_buffering();
4466 /* spice needs the timers to be initialized by this point */
4467 qemu_spice_init();
4469 cpu_ticks_init();
4470 if (icount_opts) {
4471 if (!tcg_enabled()) {
4472 error_report("-icount is not allowed with hardware virtualization");
4473 exit(1);
4475 configure_icount(icount_opts, &error_abort);
4476 qemu_opts_del(icount_opts);
4479 if (default_net) {
4480 QemuOptsList *net = qemu_find_opts("net");
4481 qemu_opts_set(net, NULL, "type", "nic", &error_abort);
4482 #ifdef CONFIG_SLIRP
4483 qemu_opts_set(net, NULL, "type", "user", &error_abort);
4484 #endif
4487 colo_info_init();
4489 if (net_init_clients() < 0) {
4490 exit(1);
4493 if (qemu_opts_foreach(qemu_find_opts("object"),
4494 user_creatable_add_opts_foreach,
4495 object_create_delayed, NULL)) {
4496 exit(1);
4499 #ifdef CONFIG_TPM
4500 if (tpm_init() < 0) {
4501 exit(1);
4503 #endif
4505 /* init the bluetooth world */
4506 if (foreach_device_config(DEV_BT, bt_parse))
4507 exit(1);
4509 if (!xen_enabled()) {
4510 /* On 32-bit hosts, QEMU is limited by virtual address space */
4511 if (ram_size > (2047 << 20) && HOST_LONG_BITS == 32) {
4512 error_report("at most 2047 MB RAM can be simulated");
4513 exit(1);
4517 blk_mig_init();
4518 ram_mig_init();
4520 /* If the currently selected machine wishes to override the units-per-bus
4521 * property of its default HBA interface type, do so now. */
4522 if (machine_class->units_per_default_bus) {
4523 override_max_devs(machine_class->block_default_type,
4524 machine_class->units_per_default_bus);
4527 /* open the virtual block devices */
4528 if (snapshot || replay_mode != REPLAY_MODE_NONE) {
4529 qemu_opts_foreach(qemu_find_opts("drive"), drive_enable_snapshot,
4530 NULL, NULL);
4532 if (qemu_opts_foreach(qemu_find_opts("drive"), drive_init_func,
4533 &machine_class->block_default_type, NULL)) {
4534 exit(1);
4537 default_drive(default_cdrom, snapshot, machine_class->block_default_type, 2,
4538 CDROM_OPTS);
4539 default_drive(default_floppy, snapshot, IF_FLOPPY, 0, FD_OPTS);
4540 default_drive(default_sdcard, snapshot, IF_SD, 0, SD_OPTS);
4542 parse_numa_opts(machine_class);
4544 if (qemu_opts_foreach(qemu_find_opts("mon"),
4545 mon_init_func, NULL, NULL)) {
4546 exit(1);
4549 if (foreach_device_config(DEV_SERIAL, serial_parse) < 0)
4550 exit(1);
4551 if (foreach_device_config(DEV_PARALLEL, parallel_parse) < 0)
4552 exit(1);
4553 if (foreach_device_config(DEV_VIRTCON, virtcon_parse) < 0)
4554 exit(1);
4555 if (foreach_device_config(DEV_SCLP, sclp_parse) < 0) {
4556 exit(1);
4558 if (foreach_device_config(DEV_DEBUGCON, debugcon_parse) < 0)
4559 exit(1);
4561 /* If no default VGA is requested, the default is "none". */
4562 if (default_vga) {
4563 if (machine_class->default_display) {
4564 vga_model = machine_class->default_display;
4565 } else if (vga_interface_available(VGA_CIRRUS)) {
4566 vga_model = "cirrus";
4567 } else if (vga_interface_available(VGA_STD)) {
4568 vga_model = "std";
4571 if (vga_model) {
4572 select_vgahw(vga_model);
4575 if (watchdog) {
4576 i = select_watchdog(watchdog);
4577 if (i > 0)
4578 exit (i == 1 ? 1 : 0);
4581 machine_register_compat_props(current_machine);
4583 qemu_opts_foreach(qemu_find_opts("global"),
4584 global_init_func, NULL, NULL);
4586 /* This checkpoint is required by replay to separate prior clock
4587 reading from the other reads, because timer polling functions query
4588 clock values from the log. */
4589 replay_checkpoint(CHECKPOINT_INIT);
4590 qdev_machine_init();
4592 current_machine->ram_size = ram_size;
4593 current_machine->maxram_size = maxram_size;
4594 current_machine->ram_slots = ram_slots;
4595 current_machine->boot_order = boot_order;
4596 current_machine->cpu_model = cpu_model;
4598 machine_class->init(current_machine);
4600 realtime_init();
4602 audio_init();
4604 cpu_synchronize_all_post_init();
4606 numa_post_machine_init();
4608 if (hax_enabled()) {
4609 hax_sync_vcpus();
4612 if (qemu_opts_foreach(qemu_find_opts("fw_cfg"),
4613 parse_fw_cfg, fw_cfg_find(), NULL) != 0) {
4614 exit(1);
4617 /* init USB devices */
4618 if (machine_usb(current_machine)) {
4619 if (foreach_device_config(DEV_USB, usb_parse) < 0)
4620 exit(1);
4623 /* Check if IGD GFX passthrough. */
4624 igd_gfx_passthru();
4626 /* init generic devices */
4627 rom_set_order_override(FW_CFG_ORDER_OVERRIDE_DEVICE);
4628 if (qemu_opts_foreach(qemu_find_opts("device"),
4629 device_init_func, NULL, NULL)) {
4630 exit(1);
4632 rom_reset_order_override();
4634 /* Did we create any drives that we failed to create a device for? */
4635 drive_check_orphaned();
4637 /* Don't warn about the default network setup that you get if
4638 * no command line -net or -netdev options are specified. There
4639 * are two cases that we would otherwise complain about:
4640 * (1) board doesn't support a NIC but the implicit "-net nic"
4641 * requested one
4642 * (2) CONFIG_SLIRP not set, in which case the implicit "-net nic"
4643 * sets up a nic that isn't connected to anything.
4645 if (!default_net) {
4646 net_check_clients();
4650 if (boot_once) {
4651 qemu_boot_set(boot_once, &error_fatal);
4652 qemu_register_reset(restore_boot_order, g_strdup(boot_order));
4655 ds = init_displaystate();
4657 /* init local displays */
4658 switch (display_type) {
4659 case DT_CURSES:
4660 curses_display_init(ds, full_screen);
4661 break;
4662 case DT_SDL:
4663 sdl_display_init(ds, full_screen, no_frame);
4664 break;
4665 case DT_COCOA:
4666 cocoa_display_init(ds, full_screen);
4667 break;
4668 case DT_GTK:
4669 gtk_display_init(ds, full_screen, grab_on_hover);
4670 break;
4671 default:
4672 break;
4675 /* must be after terminal init, SDL library changes signal handlers */
4676 os_setup_signal_handling();
4678 /* init remote displays */
4679 #ifdef CONFIG_VNC
4680 qemu_opts_foreach(qemu_find_opts("vnc"),
4681 vnc_init_func, NULL, NULL);
4682 #endif
4684 if (using_spice) {
4685 qemu_spice_display_init();
4688 if (foreach_device_config(DEV_GDB, gdbserver_start) < 0) {
4689 exit(1);
4692 qdev_machine_creation_done();
4694 /* TODO: once all bus devices are qdevified, this should be done
4695 * when bus is created by qdev.c */
4696 qemu_register_reset(qbus_reset_all_fn, sysbus_get_default());
4697 qemu_run_machine_init_done_notifiers();
4699 if (rom_check_and_register_reset() != 0) {
4700 error_report("rom check and register reset failed");
4701 exit(1);
4704 replay_start();
4706 /* This checkpoint is required by replay to separate prior clock
4707 reading from the other reads, because timer polling functions query
4708 clock values from the log. */
4709 replay_checkpoint(CHECKPOINT_RESET);
4710 qemu_system_reset(VMRESET_SILENT);
4711 register_global_state();
4712 if (loadvm) {
4713 if (load_vmstate(loadvm) < 0) {
4714 autostart = 0;
4718 qdev_prop_check_globals();
4719 if (vmstate_dump_file) {
4720 /* dump and exit */
4721 dump_vmstate_json_to_file(vmstate_dump_file);
4722 return 0;
4725 if (incoming) {
4726 Error *local_err = NULL;
4727 qemu_start_incoming_migration(incoming, &local_err);
4728 if (local_err) {
4729 error_reportf_err(local_err, "-incoming %s: ", incoming);
4730 exit(1);
4732 } else if (autostart) {
4733 vm_start();
4736 os_setup_post();
4738 main_loop();
4739 replay_disable_events();
4740 iothread_stop_all();
4742 bdrv_close_all();
4743 pause_all_vcpus();
4744 res_free();
4746 /* vhost-user must be cleaned up before chardevs. */
4747 net_cleanup();
4748 audio_cleanup();
4749 monitor_cleanup();
4750 qemu_chr_cleanup();
4752 return 0;