Merge tag 'v2.8.0-rc3'
[qemu/ar7.git] / vl.c
blobc7e99184d1e80c62f0a3f770e4cadd9913f2ab80
1 /*
2 * QEMU System Emulator
4 * Copyright (c) 2003-2008 Fabrice Bellard
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 * THE SOFTWARE.
24 #include "qemu/osdep.h"
25 #include "qemu-version.h"
26 #include "qemu/cutils.h"
27 #include "qemu/help_option.h"
28 #include "qemu/uuid.h"
30 #ifdef CONFIG_SECCOMP
31 #include "sysemu/seccomp.h"
32 #endif
34 #if defined(CONFIG_VDE)
35 #include <libvdeplug.h>
36 #endif
38 #ifdef CONFIG_SDL
39 #if defined(__APPLE__) || defined(main)
40 #include <SDL.h>
41 int qemu_main(int argc, char **argv);
42 int main(int argc, char **argv)
44 return qemu_main(argc, argv);
46 #undef main
47 #define main qemu_main
48 #endif
49 #endif /* CONFIG_SDL */
51 #ifdef CONFIG_COCOA
52 #undef main
53 #define main qemu_main
54 #endif /* CONFIG_COCOA */
57 #include "qemu/error-report.h"
58 #include "qemu/sockets.h"
59 #include "hw/hw.h"
60 #include "hw/boards.h"
61 #include "sysemu/accel.h"
62 #include "hw/usb.h"
63 #include "hw/i386/pc.h"
64 #include "hw/isa/isa.h"
65 #include "hw/bt.h"
66 #include "sysemu/watchdog.h"
67 #include "hw/smbios/smbios.h"
68 #include "hw/xen/xen.h"
69 #include "hw/qdev.h"
70 #include "hw/loader.h"
71 #include "monitor/qdev.h"
72 #include "sysemu/bt.h"
73 #include "net/net.h"
74 #include "net/slirp.h"
75 #include "monitor/monitor.h"
76 #include "ui/console.h"
77 #include "ui/input.h"
78 #include "sysemu/sysemu.h"
79 #include "sysemu/numa.h"
80 #include "exec/gdbstub.h"
81 #include "qemu/timer.h"
82 #include "sysemu/char.h"
83 #include "qemu/bitmap.h"
84 #include "qemu/log.h"
85 #include "sysemu/blockdev.h"
86 #include "hw/block/block.h"
87 #include "migration/block.h"
88 #include "sysemu/tpm.h"
89 #include "sysemu/dma.h"
90 #include "audio/audio.h"
91 #include "migration/migration.h"
92 #include "sysemu/cpus.h"
93 #include "migration/colo.h"
94 #include "sysemu/kvm.h"
95 #include "sysemu/hax.h"
96 #include "qapi/qmp/qjson.h"
97 #include "qemu/option.h"
98 #include "qemu/config-file.h"
99 #include "qemu-options.h"
100 #include "qmp-commands.h"
101 #include "qemu/main-loop.h"
102 #ifdef CONFIG_VIRTFS
103 #include "fsdev/qemu-fsdev.h"
104 #endif
105 #include "sysemu/qtest.h"
107 #include "disas/disas.h"
110 #include "slirp/libslirp.h"
112 #include "trace.h"
113 #include "trace/control.h"
114 #include "qemu/queue.h"
115 #include "sysemu/arch_init.h"
117 #include "ui/qemu-spice.h"
118 #include "qapi/string-input-visitor.h"
119 #include "qapi/opts-visitor.h"
120 #include "qom/object_interfaces.h"
121 #include "qapi-event.h"
122 #include "exec/semihost.h"
123 #include "crypto/init.h"
124 #include "sysemu/replay.h"
125 #include "qapi/qmp/qerror.h"
126 #include "sysemu/iothread.h"
128 #define MAX_VIRTIO_CONSOLES 1
129 #define MAX_SCLP_CONSOLES 1
131 static const char *data_dir[16];
132 static int data_dir_idx;
133 const char *bios_name = NULL;
134 enum vga_retrace_method vga_retrace_method = VGA_RETRACE_DUMB;
135 int request_opengl = -1;
136 int display_opengl;
137 const char* keyboard_layout = NULL;
138 ram_addr_t ram_size;
139 const char *mem_path = NULL;
140 int mem_prealloc = 0; /* force preallocation of physical target memory */
141 bool enable_mlock = false;
142 int nb_nics;
143 NICInfo nd_table[MAX_NICS];
144 int autostart;
145 static int rtc_utc = 1;
146 static int rtc_date_offset = -1; /* -1 means no change */
147 QEMUClockType rtc_clock;
148 int vga_interface_type = VGA_NONE;
149 static int full_screen = 0;
150 static int no_frame = 0;
151 int no_quit = 0;
152 static bool grab_on_hover;
153 CharDriverState *serial_hds[MAX_SERIAL_PORTS];
154 CharDriverState *parallel_hds[MAX_PARALLEL_PORTS];
155 CharDriverState *virtcon_hds[MAX_VIRTIO_CONSOLES];
156 CharDriverState *sclp_hds[MAX_SCLP_CONSOLES];
157 int win2k_install_hack = 0;
158 int singlestep = 0;
159 int smp_cpus = 1;
160 int max_cpus = 1;
161 int smp_cores = 1;
162 int smp_threads = 1;
163 int acpi_enabled = 1;
164 int no_hpet = 0;
165 int fd_bootchk = 1;
166 static int no_reboot;
167 int no_shutdown = 0;
168 int cursor_hide = 1;
169 int graphic_rotate = 0;
170 const char *watchdog;
171 QEMUOptionRom option_rom[MAX_OPTION_ROMS];
172 int nb_option_roms;
173 int old_param = 0;
174 const char *qemu_name;
175 int alt_grab = 0;
176 int ctrl_grab = 0;
177 unsigned int nb_prom_envs = 0;
178 const char *prom_envs[MAX_PROM_ENVS];
179 int boot_menu;
180 bool boot_strict;
181 uint8_t *boot_splash_filedata;
182 size_t boot_splash_filedata_size;
183 uint8_t qemu_extra_params_fw[2];
185 int icount_align_option;
187 /* The bytes in qemu_uuid are in the order specified by RFC4122, _not_ in the
188 * little-endian "wire format" described in the SMBIOS 2.6 specification.
190 QemuUUID qemu_uuid;
191 bool qemu_uuid_set;
193 /* Trace unassigned memory or i/o accesses. */
194 bool trace_unassigned;
196 static NotifierList exit_notifiers =
197 NOTIFIER_LIST_INITIALIZER(exit_notifiers);
199 static NotifierList machine_init_done_notifiers =
200 NOTIFIER_LIST_INITIALIZER(machine_init_done_notifiers);
202 bool xen_allowed;
203 uint32_t xen_domid;
204 enum xen_mode xen_mode = XEN_EMULATE;
206 static int has_defaults = 1;
207 static int default_serial = 1;
208 static int default_parallel = 1;
209 static int default_virtcon = 1;
210 static int default_sclp = 1;
211 static int default_monitor = 1;
212 static int default_floppy = 1;
213 static int default_cdrom = 1;
214 static int default_sdcard = 1;
215 static int default_vga = 1;
216 static int default_net = 1;
218 static struct {
219 const char *driver;
220 int *flag;
221 } default_list[] = {
222 { .driver = "isa-serial", .flag = &default_serial },
223 { .driver = "isa-parallel", .flag = &default_parallel },
224 { .driver = "isa-fdc", .flag = &default_floppy },
225 { .driver = "floppy", .flag = &default_floppy },
226 { .driver = "ide-cd", .flag = &default_cdrom },
227 { .driver = "ide-hd", .flag = &default_cdrom },
228 { .driver = "ide-drive", .flag = &default_cdrom },
229 { .driver = "scsi-cd", .flag = &default_cdrom },
230 { .driver = "virtio-serial-pci", .flag = &default_virtcon },
231 { .driver = "virtio-serial", .flag = &default_virtcon },
232 { .driver = "VGA", .flag = &default_vga },
233 { .driver = "isa-vga", .flag = &default_vga },
234 { .driver = "cirrus-vga", .flag = &default_vga },
235 { .driver = "isa-cirrus-vga", .flag = &default_vga },
236 { .driver = "vmware-svga", .flag = &default_vga },
237 { .driver = "qxl-vga", .flag = &default_vga },
238 { .driver = "virtio-vga", .flag = &default_vga },
241 static QemuOptsList qemu_rtc_opts = {
242 .name = "rtc",
243 .head = QTAILQ_HEAD_INITIALIZER(qemu_rtc_opts.head),
244 .desc = {
246 .name = "base",
247 .type = QEMU_OPT_STRING,
249 .name = "clock",
250 .type = QEMU_OPT_STRING,
252 .name = "driftfix",
253 .type = QEMU_OPT_STRING,
255 { /* end of list */ }
259 static QemuOptsList qemu_sandbox_opts = {
260 .name = "sandbox",
261 .implied_opt_name = "enable",
262 .head = QTAILQ_HEAD_INITIALIZER(qemu_sandbox_opts.head),
263 .desc = {
265 .name = "enable",
266 .type = QEMU_OPT_BOOL,
268 { /* end of list */ }
272 static QemuOptsList qemu_option_rom_opts = {
273 .name = "option-rom",
274 .implied_opt_name = "romfile",
275 .head = QTAILQ_HEAD_INITIALIZER(qemu_option_rom_opts.head),
276 .desc = {
278 .name = "bootindex",
279 .type = QEMU_OPT_NUMBER,
280 }, {
281 .name = "romfile",
282 .type = QEMU_OPT_STRING,
284 { /* end of list */ }
288 static QemuOptsList qemu_machine_opts = {
289 .name = "machine",
290 .implied_opt_name = "type",
291 .merge_lists = true,
292 .head = QTAILQ_HEAD_INITIALIZER(qemu_machine_opts.head),
293 .desc = {
295 * no elements => accept any
296 * sanity checking will happen later
297 * when setting machine properties
303 static QemuOptsList qemu_boot_opts = {
304 .name = "boot-opts",
305 .implied_opt_name = "order",
306 .merge_lists = true,
307 .head = QTAILQ_HEAD_INITIALIZER(qemu_boot_opts.head),
308 .desc = {
310 .name = "order",
311 .type = QEMU_OPT_STRING,
312 }, {
313 .name = "once",
314 .type = QEMU_OPT_STRING,
315 }, {
316 .name = "menu",
317 .type = QEMU_OPT_BOOL,
318 }, {
319 .name = "splash",
320 .type = QEMU_OPT_STRING,
321 }, {
322 .name = "splash-time",
323 .type = QEMU_OPT_STRING,
324 }, {
325 .name = "reboot-timeout",
326 .type = QEMU_OPT_STRING,
327 }, {
328 .name = "strict",
329 .type = QEMU_OPT_BOOL,
331 { /*End of list */ }
335 static QemuOptsList qemu_add_fd_opts = {
336 .name = "add-fd",
337 .head = QTAILQ_HEAD_INITIALIZER(qemu_add_fd_opts.head),
338 .desc = {
340 .name = "fd",
341 .type = QEMU_OPT_NUMBER,
342 .help = "file descriptor of which a duplicate is added to fd set",
344 .name = "set",
345 .type = QEMU_OPT_NUMBER,
346 .help = "ID of the fd set to add fd to",
348 .name = "opaque",
349 .type = QEMU_OPT_STRING,
350 .help = "free-form string used to describe fd",
352 { /* end of list */ }
356 static QemuOptsList qemu_object_opts = {
357 .name = "object",
358 .implied_opt_name = "qom-type",
359 .head = QTAILQ_HEAD_INITIALIZER(qemu_object_opts.head),
360 .desc = {
365 static QemuOptsList qemu_tpmdev_opts = {
366 .name = "tpmdev",
367 .implied_opt_name = "type",
368 .head = QTAILQ_HEAD_INITIALIZER(qemu_tpmdev_opts.head),
369 .desc = {
370 /* options are defined in the TPM backends */
371 { /* end of list */ }
375 static QemuOptsList qemu_realtime_opts = {
376 .name = "realtime",
377 .head = QTAILQ_HEAD_INITIALIZER(qemu_realtime_opts.head),
378 .desc = {
380 .name = "mlock",
381 .type = QEMU_OPT_BOOL,
383 { /* end of list */ }
387 static QemuOptsList qemu_msg_opts = {
388 .name = "msg",
389 .head = QTAILQ_HEAD_INITIALIZER(qemu_msg_opts.head),
390 .desc = {
392 .name = "timestamp",
393 .type = QEMU_OPT_BOOL,
395 { /* end of list */ }
399 static QemuOptsList qemu_name_opts = {
400 .name = "name",
401 .implied_opt_name = "guest",
402 .merge_lists = true,
403 .head = QTAILQ_HEAD_INITIALIZER(qemu_name_opts.head),
404 .desc = {
406 .name = "guest",
407 .type = QEMU_OPT_STRING,
408 .help = "Sets the name of the guest.\n"
409 "This name will be displayed in the SDL window caption.\n"
410 "The name will also be used for the VNC server",
411 }, {
412 .name = "process",
413 .type = QEMU_OPT_STRING,
414 .help = "Sets the name of the QEMU process, as shown in top etc",
415 }, {
416 .name = "debug-threads",
417 .type = QEMU_OPT_BOOL,
418 .help = "When enabled, name the individual threads; defaults off.\n"
419 "NOTE: The thread names are for debugging and not a\n"
420 "stable API.",
422 { /* End of list */ }
426 static QemuOptsList qemu_mem_opts = {
427 .name = "memory",
428 .implied_opt_name = "size",
429 .head = QTAILQ_HEAD_INITIALIZER(qemu_mem_opts.head),
430 .merge_lists = true,
431 .desc = {
433 .name = "size",
434 .type = QEMU_OPT_SIZE,
437 .name = "slots",
438 .type = QEMU_OPT_NUMBER,
441 .name = "maxmem",
442 .type = QEMU_OPT_SIZE,
444 { /* end of list */ }
448 static QemuOptsList qemu_icount_opts = {
449 .name = "icount",
450 .implied_opt_name = "shift",
451 .merge_lists = true,
452 .head = QTAILQ_HEAD_INITIALIZER(qemu_icount_opts.head),
453 .desc = {
455 .name = "shift",
456 .type = QEMU_OPT_STRING,
457 }, {
458 .name = "align",
459 .type = QEMU_OPT_BOOL,
460 }, {
461 .name = "sleep",
462 .type = QEMU_OPT_BOOL,
463 }, {
464 .name = "rr",
465 .type = QEMU_OPT_STRING,
466 }, {
467 .name = "rrfile",
468 .type = QEMU_OPT_STRING,
470 { /* end of list */ }
474 static QemuOptsList qemu_semihosting_config_opts = {
475 .name = "semihosting-config",
476 .implied_opt_name = "enable",
477 .head = QTAILQ_HEAD_INITIALIZER(qemu_semihosting_config_opts.head),
478 .desc = {
480 .name = "enable",
481 .type = QEMU_OPT_BOOL,
482 }, {
483 .name = "target",
484 .type = QEMU_OPT_STRING,
485 }, {
486 .name = "arg",
487 .type = QEMU_OPT_STRING,
489 { /* end of list */ }
493 static QemuOptsList qemu_fw_cfg_opts = {
494 .name = "fw_cfg",
495 .implied_opt_name = "name",
496 .head = QTAILQ_HEAD_INITIALIZER(qemu_fw_cfg_opts.head),
497 .desc = {
499 .name = "name",
500 .type = QEMU_OPT_STRING,
501 .help = "Sets the fw_cfg name of the blob to be inserted",
502 }, {
503 .name = "file",
504 .type = QEMU_OPT_STRING,
505 .help = "Sets the name of the file from which\n"
506 "the fw_cfg blob will be loaded",
507 }, {
508 .name = "string",
509 .type = QEMU_OPT_STRING,
510 .help = "Sets content of the blob to be inserted from a string",
512 { /* end of list */ }
516 #ifdef CONFIG_LIBISCSI
517 static QemuOptsList qemu_iscsi_opts = {
518 .name = "iscsi",
519 .head = QTAILQ_HEAD_INITIALIZER(qemu_iscsi_opts.head),
520 .desc = {
522 .name = "user",
523 .type = QEMU_OPT_STRING,
524 .help = "username for CHAP authentication to target",
526 .name = "password",
527 .type = QEMU_OPT_STRING,
528 .help = "password for CHAP authentication to target",
530 .name = "password-secret",
531 .type = QEMU_OPT_STRING,
532 .help = "ID of the secret providing password for CHAP "
533 "authentication to target",
535 .name = "header-digest",
536 .type = QEMU_OPT_STRING,
537 .help = "HeaderDigest setting. "
538 "{CRC32C|CRC32C-NONE|NONE-CRC32C|NONE}",
540 .name = "initiator-name",
541 .type = QEMU_OPT_STRING,
542 .help = "Initiator iqn name to use when connecting",
544 .name = "timeout",
545 .type = QEMU_OPT_NUMBER,
546 .help = "Request timeout in seconds (default 0 = no timeout)",
548 { /* end of list */ }
551 #endif
554 * Get machine options
556 * Returns: machine options (never null).
558 QemuOpts *qemu_get_machine_opts(void)
560 return qemu_find_opts_singleton("machine");
563 const char *qemu_get_vm_name(void)
565 return qemu_name;
568 static void res_free(void)
570 g_free(boot_splash_filedata);
571 boot_splash_filedata = NULL;
574 static int default_driver_check(void *opaque, QemuOpts *opts, Error **errp)
576 const char *driver = qemu_opt_get(opts, "driver");
577 int i;
579 if (!driver)
580 return 0;
581 for (i = 0; i < ARRAY_SIZE(default_list); i++) {
582 if (strcmp(default_list[i].driver, driver) != 0)
583 continue;
584 *(default_list[i].flag) = 0;
586 return 0;
589 /***********************************************************/
590 /* QEMU state */
592 static RunState current_run_state = RUN_STATE_PRELAUNCH;
594 /* We use RUN_STATE__MAX but any invalid value will do */
595 static RunState vmstop_requested = RUN_STATE__MAX;
596 static QemuMutex vmstop_lock;
598 typedef struct {
599 RunState from;
600 RunState to;
601 } RunStateTransition;
603 static const RunStateTransition runstate_transitions_def[] = {
604 /* from -> to */
605 { RUN_STATE_DEBUG, RUN_STATE_RUNNING },
606 { RUN_STATE_DEBUG, RUN_STATE_FINISH_MIGRATE },
607 { RUN_STATE_DEBUG, RUN_STATE_PRELAUNCH },
609 { RUN_STATE_INMIGRATE, RUN_STATE_INTERNAL_ERROR },
610 { RUN_STATE_INMIGRATE, RUN_STATE_IO_ERROR },
611 { RUN_STATE_INMIGRATE, RUN_STATE_PAUSED },
612 { RUN_STATE_INMIGRATE, RUN_STATE_RUNNING },
613 { RUN_STATE_INMIGRATE, RUN_STATE_SHUTDOWN },
614 { RUN_STATE_INMIGRATE, RUN_STATE_SUSPENDED },
615 { RUN_STATE_INMIGRATE, RUN_STATE_WATCHDOG },
616 { RUN_STATE_INMIGRATE, RUN_STATE_GUEST_PANICKED },
617 { RUN_STATE_INMIGRATE, RUN_STATE_FINISH_MIGRATE },
618 { RUN_STATE_INMIGRATE, RUN_STATE_PRELAUNCH },
619 { RUN_STATE_INMIGRATE, RUN_STATE_POSTMIGRATE },
620 { RUN_STATE_INMIGRATE, RUN_STATE_COLO },
622 { RUN_STATE_INTERNAL_ERROR, RUN_STATE_PAUSED },
623 { RUN_STATE_INTERNAL_ERROR, RUN_STATE_FINISH_MIGRATE },
624 { RUN_STATE_INTERNAL_ERROR, RUN_STATE_PRELAUNCH },
626 { RUN_STATE_IO_ERROR, RUN_STATE_RUNNING },
627 { RUN_STATE_IO_ERROR, RUN_STATE_FINISH_MIGRATE },
628 { RUN_STATE_IO_ERROR, RUN_STATE_PRELAUNCH },
630 { RUN_STATE_PAUSED, RUN_STATE_RUNNING },
631 { RUN_STATE_PAUSED, RUN_STATE_FINISH_MIGRATE },
632 { RUN_STATE_PAUSED, RUN_STATE_PRELAUNCH },
633 { RUN_STATE_PAUSED, RUN_STATE_COLO},
635 { RUN_STATE_POSTMIGRATE, RUN_STATE_RUNNING },
636 { RUN_STATE_POSTMIGRATE, RUN_STATE_FINISH_MIGRATE },
637 { RUN_STATE_POSTMIGRATE, RUN_STATE_PRELAUNCH },
639 { RUN_STATE_PRELAUNCH, RUN_STATE_RUNNING },
640 { RUN_STATE_PRELAUNCH, RUN_STATE_FINISH_MIGRATE },
641 { RUN_STATE_PRELAUNCH, RUN_STATE_INMIGRATE },
643 { RUN_STATE_FINISH_MIGRATE, RUN_STATE_RUNNING },
644 { RUN_STATE_FINISH_MIGRATE, RUN_STATE_POSTMIGRATE },
645 { RUN_STATE_FINISH_MIGRATE, RUN_STATE_PRELAUNCH },
646 { RUN_STATE_FINISH_MIGRATE, RUN_STATE_COLO},
648 { RUN_STATE_RESTORE_VM, RUN_STATE_RUNNING },
649 { RUN_STATE_RESTORE_VM, RUN_STATE_PRELAUNCH },
651 { RUN_STATE_COLO, RUN_STATE_RUNNING },
653 { RUN_STATE_RUNNING, RUN_STATE_DEBUG },
654 { RUN_STATE_RUNNING, RUN_STATE_INTERNAL_ERROR },
655 { RUN_STATE_RUNNING, RUN_STATE_IO_ERROR },
656 { RUN_STATE_RUNNING, RUN_STATE_PAUSED },
657 { RUN_STATE_RUNNING, RUN_STATE_FINISH_MIGRATE },
658 { RUN_STATE_RUNNING, RUN_STATE_RESTORE_VM },
659 { RUN_STATE_RUNNING, RUN_STATE_SAVE_VM },
660 { RUN_STATE_RUNNING, RUN_STATE_SHUTDOWN },
661 { RUN_STATE_RUNNING, RUN_STATE_WATCHDOG },
662 { RUN_STATE_RUNNING, RUN_STATE_GUEST_PANICKED },
663 { RUN_STATE_RUNNING, RUN_STATE_COLO},
665 { RUN_STATE_SAVE_VM, RUN_STATE_RUNNING },
667 { RUN_STATE_SHUTDOWN, RUN_STATE_PAUSED },
668 { RUN_STATE_SHUTDOWN, RUN_STATE_FINISH_MIGRATE },
669 { RUN_STATE_SHUTDOWN, RUN_STATE_PRELAUNCH },
671 { RUN_STATE_DEBUG, RUN_STATE_SUSPENDED },
672 { RUN_STATE_RUNNING, RUN_STATE_SUSPENDED },
673 { RUN_STATE_SUSPENDED, RUN_STATE_RUNNING },
674 { RUN_STATE_SUSPENDED, RUN_STATE_FINISH_MIGRATE },
675 { RUN_STATE_SUSPENDED, RUN_STATE_PRELAUNCH },
676 { RUN_STATE_SUSPENDED, RUN_STATE_COLO},
678 { RUN_STATE_WATCHDOG, RUN_STATE_RUNNING },
679 { RUN_STATE_WATCHDOG, RUN_STATE_FINISH_MIGRATE },
680 { RUN_STATE_WATCHDOG, RUN_STATE_PRELAUNCH },
681 { RUN_STATE_WATCHDOG, RUN_STATE_COLO},
683 { RUN_STATE_GUEST_PANICKED, RUN_STATE_RUNNING },
684 { RUN_STATE_GUEST_PANICKED, RUN_STATE_FINISH_MIGRATE },
685 { RUN_STATE_GUEST_PANICKED, RUN_STATE_PRELAUNCH },
687 { RUN_STATE__MAX, RUN_STATE__MAX },
690 static bool runstate_valid_transitions[RUN_STATE__MAX][RUN_STATE__MAX];
692 bool runstate_check(RunState state)
694 return current_run_state == state;
697 bool runstate_store(char *str, size_t size)
699 const char *state = RunState_lookup[current_run_state];
700 size_t len = strlen(state) + 1;
702 if (len > size) {
703 return false;
705 memcpy(str, state, len);
706 return true;
709 static void runstate_init(void)
711 const RunStateTransition *p;
713 memset(&runstate_valid_transitions, 0, sizeof(runstate_valid_transitions));
714 for (p = &runstate_transitions_def[0]; p->from != RUN_STATE__MAX; p++) {
715 runstate_valid_transitions[p->from][p->to] = true;
718 qemu_mutex_init(&vmstop_lock);
721 /* This function will abort() on invalid state transitions */
722 void runstate_set(RunState new_state)
724 assert(new_state < RUN_STATE__MAX);
726 if (current_run_state == new_state) {
727 return;
730 if (!runstate_valid_transitions[current_run_state][new_state]) {
731 error_report("invalid runstate transition: '%s' -> '%s'",
732 RunState_lookup[current_run_state],
733 RunState_lookup[new_state]);
734 abort();
736 trace_runstate_set(new_state);
737 current_run_state = new_state;
740 int runstate_is_running(void)
742 return runstate_check(RUN_STATE_RUNNING);
745 bool runstate_needs_reset(void)
747 return runstate_check(RUN_STATE_INTERNAL_ERROR) ||
748 runstate_check(RUN_STATE_SHUTDOWN);
751 StatusInfo *qmp_query_status(Error **errp)
753 StatusInfo *info = g_malloc0(sizeof(*info));
755 info->running = runstate_is_running();
756 info->singlestep = singlestep;
757 info->status = current_run_state;
759 return info;
762 static bool qemu_vmstop_requested(RunState *r)
764 qemu_mutex_lock(&vmstop_lock);
765 *r = vmstop_requested;
766 vmstop_requested = RUN_STATE__MAX;
767 qemu_mutex_unlock(&vmstop_lock);
768 return *r < RUN_STATE__MAX;
771 void qemu_system_vmstop_request_prepare(void)
773 qemu_mutex_lock(&vmstop_lock);
776 void qemu_system_vmstop_request(RunState state)
778 vmstop_requested = state;
779 qemu_mutex_unlock(&vmstop_lock);
780 qemu_notify_event();
783 void vm_start(void)
785 RunState requested;
787 qemu_vmstop_requested(&requested);
788 if (runstate_is_running() && requested == RUN_STATE__MAX) {
789 return;
792 /* Ensure that a STOP/RESUME pair of events is emitted if a
793 * vmstop request was pending. The BLOCK_IO_ERROR event, for
794 * example, according to documentation is always followed by
795 * the STOP event.
797 if (runstate_is_running()) {
798 qapi_event_send_stop(&error_abort);
799 } else {
800 replay_enable_events();
801 cpu_enable_ticks();
802 runstate_set(RUN_STATE_RUNNING);
803 vm_state_notify(1, RUN_STATE_RUNNING);
804 resume_all_vcpus();
807 qapi_event_send_resume(&error_abort);
811 /***********************************************************/
812 /* real time host monotonic timer */
814 static time_t qemu_time(void)
816 return qemu_clock_get_ms(QEMU_CLOCK_HOST) / 1000;
819 /***********************************************************/
820 /* host time/date access */
821 void qemu_get_timedate(struct tm *tm, int offset)
823 time_t ti = qemu_time();
825 ti += offset;
826 if (rtc_date_offset == -1) {
827 if (rtc_utc)
828 gmtime_r(&ti, tm);
829 else
830 localtime_r(&ti, tm);
831 } else {
832 ti -= rtc_date_offset;
833 gmtime_r(&ti, tm);
837 int qemu_timedate_diff(struct tm *tm)
839 time_t seconds;
841 if (rtc_date_offset == -1)
842 if (rtc_utc)
843 seconds = mktimegm(tm);
844 else {
845 struct tm tmp = *tm;
846 tmp.tm_isdst = -1; /* use timezone to figure it out */
847 seconds = mktime(&tmp);
849 else
850 seconds = mktimegm(tm) + rtc_date_offset;
852 return seconds - qemu_time();
855 static void configure_rtc_date_offset(const char *startdate, int legacy)
857 time_t rtc_start_date;
858 struct tm tm;
860 if (!strcmp(startdate, "now") && legacy) {
861 rtc_date_offset = -1;
862 } else {
863 if (sscanf(startdate, "%d-%d-%dT%d:%d:%d",
864 &tm.tm_year,
865 &tm.tm_mon,
866 &tm.tm_mday,
867 &tm.tm_hour,
868 &tm.tm_min,
869 &tm.tm_sec) == 6) {
870 /* OK */
871 } else if (sscanf(startdate, "%d-%d-%d",
872 &tm.tm_year,
873 &tm.tm_mon,
874 &tm.tm_mday) == 3) {
875 tm.tm_hour = 0;
876 tm.tm_min = 0;
877 tm.tm_sec = 0;
878 } else {
879 goto date_fail;
881 tm.tm_year -= 1900;
882 tm.tm_mon--;
883 rtc_start_date = mktimegm(&tm);
884 if (rtc_start_date == -1) {
885 date_fail:
886 error_report("invalid date format");
887 error_printf("valid formats: "
888 "'2006-06-17T16:01:21' or '2006-06-17'\n");
889 exit(1);
891 rtc_date_offset = qemu_time() - rtc_start_date;
895 static void configure_rtc(QemuOpts *opts)
897 const char *value;
899 value = qemu_opt_get(opts, "base");
900 if (value) {
901 if (!strcmp(value, "utc")) {
902 rtc_utc = 1;
903 } else if (!strcmp(value, "localtime")) {
904 Error *blocker = NULL;
905 rtc_utc = 0;
906 error_setg(&blocker, QERR_REPLAY_NOT_SUPPORTED,
907 "-rtc base=localtime");
908 replay_add_blocker(blocker);
909 } else {
910 configure_rtc_date_offset(value, 0);
913 value = qemu_opt_get(opts, "clock");
914 if (value) {
915 if (!strcmp(value, "host")) {
916 rtc_clock = QEMU_CLOCK_HOST;
917 } else if (!strcmp(value, "rt")) {
918 rtc_clock = QEMU_CLOCK_REALTIME;
919 } else if (!strcmp(value, "vm")) {
920 rtc_clock = QEMU_CLOCK_VIRTUAL;
921 } else {
922 error_report("invalid option value '%s'", value);
923 exit(1);
926 value = qemu_opt_get(opts, "driftfix");
927 if (value) {
928 if (!strcmp(value, "slew")) {
929 static GlobalProperty slew_lost_ticks = {
930 .driver = "mc146818rtc",
931 .property = "lost_tick_policy",
932 .value = "slew",
935 qdev_prop_register_global(&slew_lost_ticks);
936 } else if (!strcmp(value, "none")) {
937 /* discard is default */
938 } else {
939 error_report("invalid option value '%s'", value);
940 exit(1);
945 /***********************************************************/
946 /* Bluetooth support */
947 static int nb_hcis;
948 static int cur_hci;
949 static struct HCIInfo *hci_table[MAX_NICS];
951 struct HCIInfo *qemu_next_hci(void)
953 if (cur_hci == nb_hcis)
954 return &null_hci;
956 return hci_table[cur_hci++];
959 static int bt_hci_parse(const char *str)
961 struct HCIInfo *hci;
962 bdaddr_t bdaddr;
964 if (nb_hcis >= MAX_NICS) {
965 error_report("too many bluetooth HCIs (max %i)", MAX_NICS);
966 return -1;
969 hci = hci_init(str);
970 if (!hci)
971 return -1;
973 bdaddr.b[0] = 0x52;
974 bdaddr.b[1] = 0x54;
975 bdaddr.b[2] = 0x00;
976 bdaddr.b[3] = 0x12;
977 bdaddr.b[4] = 0x34;
978 bdaddr.b[5] = 0x56 + nb_hcis;
979 hci->bdaddr_set(hci, bdaddr.b);
981 hci_table[nb_hcis++] = hci;
983 return 0;
986 static void bt_vhci_add(int vlan_id)
988 struct bt_scatternet_s *vlan = qemu_find_bt_vlan(vlan_id);
990 if (!vlan->slave)
991 error_report("warning: adding a VHCI to an empty scatternet %i",
992 vlan_id);
994 bt_vhci_init(bt_new_hci(vlan));
997 static struct bt_device_s *bt_device_add(const char *opt)
999 struct bt_scatternet_s *vlan;
1000 int vlan_id = 0;
1001 char *endp = strstr(opt, ",vlan=");
1002 int len = (endp ? endp - opt : strlen(opt)) + 1;
1003 char devname[10];
1005 pstrcpy(devname, MIN(sizeof(devname), len), opt);
1007 if (endp) {
1008 vlan_id = strtol(endp + 6, &endp, 0);
1009 if (*endp) {
1010 error_report("unrecognised bluetooth vlan Id");
1011 return 0;
1015 vlan = qemu_find_bt_vlan(vlan_id);
1017 if (!vlan->slave)
1018 error_report("warning: adding a slave device to an empty scatternet %i",
1019 vlan_id);
1021 if (!strcmp(devname, "keyboard"))
1022 return bt_keyboard_init(vlan);
1024 error_report("unsupported bluetooth device '%s'", devname);
1025 return 0;
1028 static int bt_parse(const char *opt)
1030 const char *endp, *p;
1031 int vlan;
1033 if (strstart(opt, "hci", &endp)) {
1034 if (!*endp || *endp == ',') {
1035 if (*endp)
1036 if (!strstart(endp, ",vlan=", 0))
1037 opt = endp + 1;
1039 return bt_hci_parse(opt);
1041 } else if (strstart(opt, "vhci", &endp)) {
1042 if (!*endp || *endp == ',') {
1043 if (*endp) {
1044 if (strstart(endp, ",vlan=", &p)) {
1045 vlan = strtol(p, (char **) &endp, 0);
1046 if (*endp) {
1047 error_report("bad scatternet '%s'", p);
1048 return 1;
1050 } else {
1051 error_report("bad parameter '%s'", endp + 1);
1052 return 1;
1054 } else
1055 vlan = 0;
1057 bt_vhci_add(vlan);
1058 return 0;
1060 } else if (strstart(opt, "device:", &endp))
1061 return !bt_device_add(endp);
1063 error_report("bad bluetooth parameter '%s'", opt);
1064 return 1;
1067 static int parse_sandbox(void *opaque, QemuOpts *opts, Error **errp)
1069 /* FIXME: change this to true for 1.3 */
1070 if (qemu_opt_get_bool(opts, "enable", false)) {
1071 #ifdef CONFIG_SECCOMP
1072 if (seccomp_start() < 0) {
1073 error_report("failed to install seccomp syscall filter "
1074 "in the kernel");
1075 return -1;
1077 #else
1078 error_report("seccomp support is disabled");
1079 return -1;
1080 #endif
1083 return 0;
1086 static int parse_name(void *opaque, QemuOpts *opts, Error **errp)
1088 const char *proc_name;
1090 if (qemu_opt_get(opts, "debug-threads")) {
1091 qemu_thread_naming(qemu_opt_get_bool(opts, "debug-threads", false));
1093 qemu_name = qemu_opt_get(opts, "guest");
1095 proc_name = qemu_opt_get(opts, "process");
1096 if (proc_name) {
1097 os_set_proc_name(proc_name);
1100 return 0;
1103 bool defaults_enabled(void)
1105 return has_defaults;
1108 #ifndef _WIN32
1109 static int parse_add_fd(void *opaque, QemuOpts *opts, Error **errp)
1111 int fd, dupfd, flags;
1112 int64_t fdset_id;
1113 const char *fd_opaque = NULL;
1114 AddfdInfo *fdinfo;
1116 fd = qemu_opt_get_number(opts, "fd", -1);
1117 fdset_id = qemu_opt_get_number(opts, "set", -1);
1118 fd_opaque = qemu_opt_get(opts, "opaque");
1120 if (fd < 0) {
1121 error_report("fd option is required and must be non-negative");
1122 return -1;
1125 if (fd <= STDERR_FILENO) {
1126 error_report("fd cannot be a standard I/O stream");
1127 return -1;
1131 * All fds inherited across exec() necessarily have FD_CLOEXEC
1132 * clear, while qemu sets FD_CLOEXEC on all other fds used internally.
1134 flags = fcntl(fd, F_GETFD);
1135 if (flags == -1 || (flags & FD_CLOEXEC)) {
1136 error_report("fd is not valid or already in use");
1137 return -1;
1140 if (fdset_id < 0) {
1141 error_report("set option is required and must be non-negative");
1142 return -1;
1145 #ifdef F_DUPFD_CLOEXEC
1146 dupfd = fcntl(fd, F_DUPFD_CLOEXEC, 0);
1147 #else
1148 dupfd = dup(fd);
1149 if (dupfd != -1) {
1150 qemu_set_cloexec(dupfd);
1152 #endif
1153 if (dupfd == -1) {
1154 error_report("error duplicating fd: %s", strerror(errno));
1155 return -1;
1158 /* add the duplicate fd, and optionally the opaque string, to the fd set */
1159 fdinfo = monitor_fdset_add_fd(dupfd, true, fdset_id, !!fd_opaque, fd_opaque,
1160 &error_abort);
1161 g_free(fdinfo);
1163 return 0;
1166 static int cleanup_add_fd(void *opaque, QemuOpts *opts, Error **errp)
1168 int fd;
1170 fd = qemu_opt_get_number(opts, "fd", -1);
1171 close(fd);
1173 return 0;
1175 #endif
1177 /***********************************************************/
1178 /* QEMU Block devices */
1180 #define HD_OPTS "media=disk"
1181 #define CDROM_OPTS "media=cdrom"
1182 #define FD_OPTS ""
1183 #define PFLASH_OPTS ""
1184 #define MTD_OPTS ""
1185 #define SD_OPTS ""
1187 static int drive_init_func(void *opaque, QemuOpts *opts, Error **errp)
1189 BlockInterfaceType *block_default_type = opaque;
1191 return drive_new(opts, *block_default_type) == NULL;
1194 static int drive_enable_snapshot(void *opaque, QemuOpts *opts, Error **errp)
1196 if (qemu_opt_get(opts, "snapshot") == NULL) {
1197 qemu_opt_set(opts, "snapshot", "on", &error_abort);
1199 return 0;
1202 static void default_drive(int enable, int snapshot, BlockInterfaceType type,
1203 int index, const char *optstr)
1205 QemuOpts *opts;
1206 DriveInfo *dinfo;
1208 if (!enable || drive_get_by_index(type, index)) {
1209 return;
1212 opts = drive_add(type, index, NULL, optstr);
1213 if (snapshot) {
1214 drive_enable_snapshot(NULL, opts, NULL);
1217 dinfo = drive_new(opts, type);
1218 if (!dinfo) {
1219 exit(1);
1221 dinfo->is_default = true;
1225 static QemuOptsList qemu_smp_opts = {
1226 .name = "smp-opts",
1227 .implied_opt_name = "cpus",
1228 .merge_lists = true,
1229 .head = QTAILQ_HEAD_INITIALIZER(qemu_smp_opts.head),
1230 .desc = {
1232 .name = "cpus",
1233 .type = QEMU_OPT_NUMBER,
1234 }, {
1235 .name = "sockets",
1236 .type = QEMU_OPT_NUMBER,
1237 }, {
1238 .name = "cores",
1239 .type = QEMU_OPT_NUMBER,
1240 }, {
1241 .name = "threads",
1242 .type = QEMU_OPT_NUMBER,
1243 }, {
1244 .name = "maxcpus",
1245 .type = QEMU_OPT_NUMBER,
1247 { /*End of list */ }
1251 static void smp_parse(QemuOpts *opts)
1253 if (opts) {
1254 unsigned cpus = qemu_opt_get_number(opts, "cpus", 0);
1255 unsigned sockets = qemu_opt_get_number(opts, "sockets", 0);
1256 unsigned cores = qemu_opt_get_number(opts, "cores", 0);
1257 unsigned threads = qemu_opt_get_number(opts, "threads", 0);
1259 /* compute missing values, prefer sockets over cores over threads */
1260 if (cpus == 0 || sockets == 0) {
1261 sockets = sockets > 0 ? sockets : 1;
1262 cores = cores > 0 ? cores : 1;
1263 threads = threads > 0 ? threads : 1;
1264 if (cpus == 0) {
1265 cpus = cores * threads * sockets;
1267 } else if (cores == 0) {
1268 threads = threads > 0 ? threads : 1;
1269 cores = cpus / (sockets * threads);
1270 cores = cores > 0 ? cores : 1;
1271 } else if (threads == 0) {
1272 threads = cpus / (cores * sockets);
1273 threads = threads > 0 ? threads : 1;
1274 } else if (sockets * cores * threads < cpus) {
1275 error_report("cpu topology: "
1276 "sockets (%u) * cores (%u) * threads (%u) < "
1277 "smp_cpus (%u)",
1278 sockets, cores, threads, cpus);
1279 exit(1);
1282 max_cpus = qemu_opt_get_number(opts, "maxcpus", cpus);
1284 if (max_cpus > MAX_CPUMASK_BITS) {
1285 error_report("unsupported number of maxcpus");
1286 exit(1);
1289 if (max_cpus < cpus) {
1290 error_report("maxcpus must be equal to or greater than smp");
1291 exit(1);
1294 if (sockets * cores * threads > max_cpus) {
1295 error_report("cpu topology: "
1296 "sockets (%u) * cores (%u) * threads (%u) > "
1297 "maxcpus (%u)",
1298 sockets, cores, threads, max_cpus);
1299 exit(1);
1302 smp_cpus = cpus;
1303 smp_cores = cores;
1304 smp_threads = threads;
1307 if (smp_cpus > 1) {
1308 Error *blocker = NULL;
1309 error_setg(&blocker, QERR_REPLAY_NOT_SUPPORTED, "smp");
1310 replay_add_blocker(blocker);
1314 static void realtime_init(void)
1316 if (enable_mlock) {
1317 if (os_mlock() < 0) {
1318 error_report("locking memory failed");
1319 exit(1);
1325 static void configure_msg(QemuOpts *opts)
1327 enable_timestamp_msg = qemu_opt_get_bool(opts, "timestamp", true);
1330 /***********************************************************/
1331 /* Semihosting */
1333 typedef struct SemihostingConfig {
1334 bool enabled;
1335 SemihostingTarget target;
1336 const char **argv;
1337 int argc;
1338 const char *cmdline; /* concatenated argv */
1339 } SemihostingConfig;
1341 static SemihostingConfig semihosting;
1343 bool semihosting_enabled(void)
1345 return semihosting.enabled;
1348 SemihostingTarget semihosting_get_target(void)
1350 return semihosting.target;
1353 const char *semihosting_get_arg(int i)
1355 if (i >= semihosting.argc) {
1356 return NULL;
1358 return semihosting.argv[i];
1361 int semihosting_get_argc(void)
1363 return semihosting.argc;
1366 const char *semihosting_get_cmdline(void)
1368 if (semihosting.cmdline == NULL && semihosting.argc > 0) {
1369 semihosting.cmdline = g_strjoinv(" ", (gchar **)semihosting.argv);
1371 return semihosting.cmdline;
1374 static int add_semihosting_arg(void *opaque,
1375 const char *name, const char *val,
1376 Error **errp)
1378 SemihostingConfig *s = opaque;
1379 if (strcmp(name, "arg") == 0) {
1380 s->argc++;
1381 /* one extra element as g_strjoinv() expects NULL-terminated array */
1382 s->argv = g_realloc(s->argv, (s->argc + 1) * sizeof(void *));
1383 s->argv[s->argc - 1] = val;
1384 s->argv[s->argc] = NULL;
1386 return 0;
1389 /* Use strings passed via -kernel/-append to initialize semihosting.argv[] */
1390 static inline void semihosting_arg_fallback(const char *file, const char *cmd)
1392 char *cmd_token;
1394 /* argv[0] */
1395 add_semihosting_arg(&semihosting, "arg", file, NULL);
1397 /* split -append and initialize argv[1..n] */
1398 cmd_token = strtok(g_strdup(cmd), " ");
1399 while (cmd_token) {
1400 add_semihosting_arg(&semihosting, "arg", cmd_token, NULL);
1401 cmd_token = strtok(NULL, " ");
1405 /* Now we still need this for compatibility with XEN. */
1406 bool has_igd_gfx_passthru;
1407 static void igd_gfx_passthru(void)
1409 has_igd_gfx_passthru = current_machine->igd_gfx_passthru;
1412 /***********************************************************/
1413 /* USB devices */
1415 static int usb_device_add(const char *devname)
1417 USBDevice *dev = NULL;
1418 #ifndef CONFIG_LINUX
1419 const char *p;
1420 #endif
1422 if (!machine_usb(current_machine)) {
1423 return -1;
1426 /* drivers with .usbdevice_name entry in USBDeviceInfo */
1427 dev = usbdevice_create(devname);
1428 if (dev)
1429 goto done;
1431 /* the other ones */
1432 #ifndef CONFIG_LINUX
1433 /* only the linux version is qdev-ified, usb-bsd still needs this */
1434 if (strstart(devname, "host:", &p)) {
1435 dev = usb_host_device_open(usb_bus_find(-1), p);
1437 #endif
1438 if (!dev)
1439 return -1;
1441 done:
1442 return 0;
1445 static int usb_device_del(const char *devname)
1447 int bus_num, addr;
1448 const char *p;
1450 if (strstart(devname, "host:", &p)) {
1451 return -1;
1454 if (!machine_usb(current_machine)) {
1455 return -1;
1458 p = strchr(devname, '.');
1459 if (!p)
1460 return -1;
1461 bus_num = strtoul(devname, NULL, 0);
1462 addr = strtoul(p + 1, NULL, 0);
1464 return usb_device_delete_addr(bus_num, addr);
1467 static int usb_parse(const char *cmdline)
1469 int r;
1470 r = usb_device_add(cmdline);
1471 if (r < 0) {
1472 error_report("could not add USB device '%s'", cmdline);
1474 return r;
1477 void hmp_usb_add(Monitor *mon, const QDict *qdict)
1479 const char *devname = qdict_get_str(qdict, "devname");
1480 if (usb_device_add(devname) < 0) {
1481 error_report("could not add USB device '%s'", devname);
1485 void hmp_usb_del(Monitor *mon, const QDict *qdict)
1487 const char *devname = qdict_get_str(qdict, "devname");
1488 if (usb_device_del(devname) < 0) {
1489 error_report("could not delete USB device '%s'", devname);
1493 /***********************************************************/
1494 /* machine registration */
1496 MachineState *current_machine;
1498 static MachineClass *find_machine(const char *name)
1500 GSList *el, *machines = object_class_get_list(TYPE_MACHINE, false);
1501 MachineClass *mc = NULL;
1503 for (el = machines; el; el = el->next) {
1504 MachineClass *temp = el->data;
1506 if (!strcmp(temp->name, name)) {
1507 mc = temp;
1508 break;
1510 if (temp->alias &&
1511 !strcmp(temp->alias, name)) {
1512 mc = temp;
1513 break;
1517 g_slist_free(machines);
1518 return mc;
1521 MachineClass *find_default_machine(void)
1523 GSList *el, *machines = object_class_get_list(TYPE_MACHINE, false);
1524 MachineClass *mc = NULL;
1526 for (el = machines; el; el = el->next) {
1527 MachineClass *temp = el->data;
1529 if (temp->is_default) {
1530 mc = temp;
1531 break;
1535 g_slist_free(machines);
1536 return mc;
1539 MachineInfoList *qmp_query_machines(Error **errp)
1541 GSList *el, *machines = object_class_get_list(TYPE_MACHINE, false);
1542 MachineInfoList *mach_list = NULL;
1544 for (el = machines; el; el = el->next) {
1545 MachineClass *mc = el->data;
1546 MachineInfoList *entry;
1547 MachineInfo *info;
1549 info = g_malloc0(sizeof(*info));
1550 if (mc->is_default) {
1551 info->has_is_default = true;
1552 info->is_default = true;
1555 if (mc->alias) {
1556 info->has_alias = true;
1557 info->alias = g_strdup(mc->alias);
1560 info->name = g_strdup(mc->name);
1561 info->cpu_max = !mc->max_cpus ? 1 : mc->max_cpus;
1562 info->hotpluggable_cpus = !!mc->query_hotpluggable_cpus;
1564 entry = g_malloc0(sizeof(*entry));
1565 entry->value = info;
1566 entry->next = mach_list;
1567 mach_list = entry;
1570 g_slist_free(machines);
1571 return mach_list;
1574 static int machine_help_func(QemuOpts *opts, MachineState *machine)
1576 ObjectProperty *prop;
1577 ObjectPropertyIterator iter;
1579 if (!qemu_opt_has_help_opt(opts)) {
1580 return 0;
1583 object_property_iter_init(&iter, OBJECT(machine));
1584 while ((prop = object_property_iter_next(&iter))) {
1585 if (!prop->set) {
1586 continue;
1589 error_printf("%s.%s=%s", MACHINE_GET_CLASS(machine)->name,
1590 prop->name, prop->type);
1591 if (prop->description) {
1592 error_printf(" (%s)\n", prop->description);
1593 } else {
1594 error_printf("\n");
1598 return 1;
1601 /***********************************************************/
1602 /* main execution loop */
1604 struct vm_change_state_entry {
1605 VMChangeStateHandler *cb;
1606 void *opaque;
1607 QLIST_ENTRY (vm_change_state_entry) entries;
1610 static QLIST_HEAD(vm_change_state_head, vm_change_state_entry) vm_change_state_head;
1612 VMChangeStateEntry *qemu_add_vm_change_state_handler(VMChangeStateHandler *cb,
1613 void *opaque)
1615 VMChangeStateEntry *e;
1617 e = g_malloc0(sizeof (*e));
1619 e->cb = cb;
1620 e->opaque = opaque;
1621 QLIST_INSERT_HEAD(&vm_change_state_head, e, entries);
1622 return e;
1625 void qemu_del_vm_change_state_handler(VMChangeStateEntry *e)
1627 QLIST_REMOVE (e, entries);
1628 g_free (e);
1631 void vm_state_notify(int running, RunState state)
1633 VMChangeStateEntry *e, *next;
1635 trace_vm_state_notify(running, state);
1637 QLIST_FOREACH_SAFE(e, &vm_change_state_head, entries, next) {
1638 e->cb(e->opaque, running, state);
1642 /* reset/shutdown handler */
1644 typedef struct QEMUResetEntry {
1645 QTAILQ_ENTRY(QEMUResetEntry) entry;
1646 QEMUResetHandler *func;
1647 void *opaque;
1648 } QEMUResetEntry;
1650 static QTAILQ_HEAD(reset_handlers, QEMUResetEntry) reset_handlers =
1651 QTAILQ_HEAD_INITIALIZER(reset_handlers);
1652 static int reset_requested;
1653 static int shutdown_requested, shutdown_signal = -1;
1654 static pid_t shutdown_pid;
1655 static int powerdown_requested;
1656 static int debug_requested;
1657 static int suspend_requested;
1658 static WakeupReason wakeup_reason;
1659 static NotifierList powerdown_notifiers =
1660 NOTIFIER_LIST_INITIALIZER(powerdown_notifiers);
1661 static NotifierList suspend_notifiers =
1662 NOTIFIER_LIST_INITIALIZER(suspend_notifiers);
1663 static NotifierList wakeup_notifiers =
1664 NOTIFIER_LIST_INITIALIZER(wakeup_notifiers);
1665 static uint32_t wakeup_reason_mask = ~(1 << QEMU_WAKEUP_REASON_NONE);
1667 int qemu_shutdown_requested_get(void)
1669 return shutdown_requested;
1672 int qemu_reset_requested_get(void)
1674 return reset_requested;
1677 static int qemu_shutdown_requested(void)
1679 return atomic_xchg(&shutdown_requested, 0);
1682 static void qemu_kill_report(void)
1684 if (!qtest_driver() && shutdown_signal != -1) {
1685 if (shutdown_pid == 0) {
1686 /* This happens for eg ^C at the terminal, so it's worth
1687 * avoiding printing an odd message in that case.
1689 error_report("terminating on signal %d", shutdown_signal);
1690 } else {
1691 char *shutdown_cmd = qemu_get_pid_name(shutdown_pid);
1693 error_report("terminating on signal %d from pid " FMT_pid " (%s)",
1694 shutdown_signal, shutdown_pid,
1695 shutdown_cmd ? shutdown_cmd : "<unknown process>");
1696 g_free(shutdown_cmd);
1698 shutdown_signal = -1;
1702 static int qemu_reset_requested(void)
1704 int r = reset_requested;
1705 if (r && replay_checkpoint(CHECKPOINT_RESET_REQUESTED)) {
1706 reset_requested = 0;
1707 return r;
1709 return false;
1712 static int qemu_suspend_requested(void)
1714 int r = suspend_requested;
1715 if (r && replay_checkpoint(CHECKPOINT_SUSPEND_REQUESTED)) {
1716 suspend_requested = 0;
1717 return r;
1719 return false;
1722 static WakeupReason qemu_wakeup_requested(void)
1724 return wakeup_reason;
1727 static int qemu_powerdown_requested(void)
1729 int r = powerdown_requested;
1730 powerdown_requested = 0;
1731 return r;
1734 static int qemu_debug_requested(void)
1736 int r = debug_requested;
1737 debug_requested = 0;
1738 return r;
1741 void qemu_register_reset(QEMUResetHandler *func, void *opaque)
1743 QEMUResetEntry *re = g_malloc0(sizeof(QEMUResetEntry));
1745 re->func = func;
1746 re->opaque = opaque;
1747 QTAILQ_INSERT_TAIL(&reset_handlers, re, entry);
1750 void qemu_unregister_reset(QEMUResetHandler *func, void *opaque)
1752 QEMUResetEntry *re;
1754 QTAILQ_FOREACH(re, &reset_handlers, entry) {
1755 if (re->func == func && re->opaque == opaque) {
1756 QTAILQ_REMOVE(&reset_handlers, re, entry);
1757 g_free(re);
1758 return;
1763 void qemu_devices_reset(void)
1765 QEMUResetEntry *re, *nre;
1767 /* reset all devices */
1768 QTAILQ_FOREACH_SAFE(re, &reset_handlers, entry, nre) {
1769 re->func(re->opaque);
1773 void qemu_system_reset(bool report)
1775 MachineClass *mc;
1777 mc = current_machine ? MACHINE_GET_CLASS(current_machine) : NULL;
1779 cpu_synchronize_all_states();
1781 if (mc && mc->reset) {
1782 mc->reset();
1783 } else {
1784 qemu_devices_reset();
1786 if (report) {
1787 qapi_event_send_reset(&error_abort);
1789 cpu_synchronize_all_post_reset();
1792 void qemu_system_guest_panicked(void)
1794 if (current_cpu) {
1795 current_cpu->crash_occurred = true;
1797 qapi_event_send_guest_panicked(GUEST_PANIC_ACTION_PAUSE, &error_abort);
1798 vm_stop(RUN_STATE_GUEST_PANICKED);
1799 if (!no_shutdown) {
1800 qapi_event_send_guest_panicked(GUEST_PANIC_ACTION_POWEROFF,
1801 &error_abort);
1802 qemu_system_shutdown_request();
1806 void qemu_system_reset_request(void)
1808 if (no_reboot) {
1809 shutdown_requested = 1;
1810 } else {
1811 reset_requested = 1;
1813 cpu_stop_current();
1814 qemu_notify_event();
1817 static void qemu_system_suspend(void)
1819 pause_all_vcpus();
1820 notifier_list_notify(&suspend_notifiers, NULL);
1821 runstate_set(RUN_STATE_SUSPENDED);
1822 qapi_event_send_suspend(&error_abort);
1825 void qemu_system_suspend_request(void)
1827 if (runstate_check(RUN_STATE_SUSPENDED)) {
1828 return;
1830 suspend_requested = 1;
1831 cpu_stop_current();
1832 qemu_notify_event();
1835 void qemu_register_suspend_notifier(Notifier *notifier)
1837 notifier_list_add(&suspend_notifiers, notifier);
1840 void qemu_system_wakeup_request(WakeupReason reason)
1842 trace_system_wakeup_request(reason);
1844 if (!runstate_check(RUN_STATE_SUSPENDED)) {
1845 return;
1847 if (!(wakeup_reason_mask & (1 << reason))) {
1848 return;
1850 runstate_set(RUN_STATE_RUNNING);
1851 wakeup_reason = reason;
1852 qemu_notify_event();
1855 void qemu_system_wakeup_enable(WakeupReason reason, bool enabled)
1857 if (enabled) {
1858 wakeup_reason_mask |= (1 << reason);
1859 } else {
1860 wakeup_reason_mask &= ~(1 << reason);
1864 void qemu_register_wakeup_notifier(Notifier *notifier)
1866 notifier_list_add(&wakeup_notifiers, notifier);
1869 void qemu_system_killed(int signal, pid_t pid)
1871 shutdown_signal = signal;
1872 shutdown_pid = pid;
1873 no_shutdown = 0;
1875 /* Cannot call qemu_system_shutdown_request directly because
1876 * we are in a signal handler.
1878 shutdown_requested = 1;
1879 qemu_notify_event();
1882 void qemu_system_shutdown_request(void)
1884 trace_qemu_system_shutdown_request();
1885 replay_shutdown_request();
1886 shutdown_requested = 1;
1887 qemu_notify_event();
1890 static void qemu_system_powerdown(void)
1892 qapi_event_send_powerdown(&error_abort);
1893 notifier_list_notify(&powerdown_notifiers, NULL);
1896 void qemu_system_powerdown_request(void)
1898 trace_qemu_system_powerdown_request();
1899 powerdown_requested = 1;
1900 qemu_notify_event();
1903 void qemu_register_powerdown_notifier(Notifier *notifier)
1905 notifier_list_add(&powerdown_notifiers, notifier);
1908 void qemu_system_debug_request(void)
1910 debug_requested = 1;
1911 qemu_notify_event();
1914 static bool main_loop_should_exit(void)
1916 RunState r;
1917 if (qemu_debug_requested()) {
1918 vm_stop(RUN_STATE_DEBUG);
1920 if (qemu_suspend_requested()) {
1921 qemu_system_suspend();
1923 if (qemu_shutdown_requested()) {
1924 qemu_kill_report();
1925 qapi_event_send_shutdown(&error_abort);
1926 if (no_shutdown) {
1927 vm_stop(RUN_STATE_SHUTDOWN);
1928 } else {
1929 return true;
1932 if (qemu_reset_requested()) {
1933 pause_all_vcpus();
1934 qemu_system_reset(VMRESET_REPORT);
1935 resume_all_vcpus();
1936 if (!runstate_check(RUN_STATE_RUNNING) &&
1937 !runstate_check(RUN_STATE_INMIGRATE)) {
1938 runstate_set(RUN_STATE_PRELAUNCH);
1941 if (qemu_wakeup_requested()) {
1942 pause_all_vcpus();
1943 qemu_system_reset(VMRESET_SILENT);
1944 notifier_list_notify(&wakeup_notifiers, &wakeup_reason);
1945 wakeup_reason = QEMU_WAKEUP_REASON_NONE;
1946 resume_all_vcpus();
1947 qapi_event_send_wakeup(&error_abort);
1949 if (qemu_powerdown_requested()) {
1950 qemu_system_powerdown();
1952 if (qemu_vmstop_requested(&r)) {
1953 vm_stop(r);
1955 return false;
1958 static void main_loop(void)
1960 bool nonblocking;
1961 int last_io = 0;
1962 #ifdef CONFIG_PROFILER
1963 int64_t ti;
1964 #endif
1965 do {
1966 nonblocking = tcg_enabled() && last_io > 0;
1967 #ifdef CONFIG_PROFILER
1968 ti = profile_getclock();
1969 #endif
1970 last_io = main_loop_wait(nonblocking);
1971 #ifdef CONFIG_PROFILER
1972 dev_time += profile_getclock() - ti;
1973 #endif
1974 } while (!main_loop_should_exit());
1977 static void version(void)
1979 printf("QEMU emulator version " QEMU_VERSION QEMU_PKGVERSION "\n"
1980 QEMU_COPYRIGHT "\n");
1983 static void QEMU_NORETURN help(int exitcode)
1985 version();
1986 printf("usage: %s [options] [disk_image]\n\n"
1987 "'disk_image' is a raw hard disk image for IDE hard disk 0\n\n",
1988 error_get_progname());
1990 #define QEMU_OPTIONS_GENERATE_HELP
1991 #include "qemu-options-wrapper.h"
1993 printf("\nDuring emulation, the following keys are useful:\n"
1994 "ctrl-alt-f toggle full screen\n"
1995 "ctrl-alt-n switch to virtual console 'n'\n"
1996 "ctrl-alt toggle mouse and keyboard grab\n"
1997 "\n"
1998 "When using -nographic, press 'ctrl-a h' to get some help.\n");
2000 exit(exitcode);
2003 #define HAS_ARG 0x0001
2005 typedef struct QEMUOption {
2006 const char *name;
2007 int flags;
2008 int index;
2009 uint32_t arch_mask;
2010 } QEMUOption;
2012 static const QEMUOption qemu_options[] = {
2013 { "h", 0, QEMU_OPTION_h, QEMU_ARCH_ALL },
2014 #define QEMU_OPTIONS_GENERATE_OPTIONS
2015 #include "qemu-options-wrapper.h"
2016 { NULL },
2019 typedef struct VGAInterfaceInfo {
2020 const char *opt_name; /* option name */
2021 const char *name; /* human-readable name */
2022 /* Class names indicating that support is available.
2023 * If no class is specified, the interface is always available */
2024 const char *class_names[2];
2025 } VGAInterfaceInfo;
2027 static VGAInterfaceInfo vga_interfaces[VGA_TYPE_MAX] = {
2028 [VGA_NONE] = {
2029 .opt_name = "none",
2031 [VGA_STD] = {
2032 .opt_name = "std",
2033 .name = "standard VGA",
2034 .class_names = { "VGA", "isa-vga" },
2036 [VGA_CIRRUS] = {
2037 .opt_name = "cirrus",
2038 .name = "Cirrus VGA",
2039 .class_names = { "cirrus-vga", "isa-cirrus-vga" },
2041 [VGA_VMWARE] = {
2042 .opt_name = "vmware",
2043 .name = "VMWare SVGA",
2044 .class_names = { "vmware-svga" },
2046 [VGA_VIRTIO] = {
2047 .opt_name = "virtio",
2048 .name = "Virtio VGA",
2049 .class_names = { "virtio-vga" },
2051 [VGA_QXL] = {
2052 .opt_name = "qxl",
2053 .name = "QXL VGA",
2054 .class_names = { "qxl-vga" },
2056 [VGA_TCX] = {
2057 .opt_name = "tcx",
2058 .name = "TCX framebuffer",
2059 .class_names = { "SUNW,tcx" },
2061 [VGA_CG3] = {
2062 .opt_name = "cg3",
2063 .name = "CG3 framebuffer",
2064 .class_names = { "cgthree" },
2066 [VGA_XENFB] = {
2067 .opt_name = "xenfb",
2071 static bool vga_interface_available(VGAInterfaceType t)
2073 VGAInterfaceInfo *ti = &vga_interfaces[t];
2075 assert(t < VGA_TYPE_MAX);
2076 return !ti->class_names[0] ||
2077 object_class_by_name(ti->class_names[0]) ||
2078 object_class_by_name(ti->class_names[1]);
2081 static void select_vgahw(const char *p)
2083 const char *opts;
2084 int t;
2086 assert(vga_interface_type == VGA_NONE);
2087 for (t = 0; t < VGA_TYPE_MAX; t++) {
2088 VGAInterfaceInfo *ti = &vga_interfaces[t];
2089 if (ti->opt_name && strstart(p, ti->opt_name, &opts)) {
2090 if (!vga_interface_available(t)) {
2091 error_report("%s not available", ti->name);
2092 exit(1);
2094 vga_interface_type = t;
2095 break;
2098 if (t == VGA_TYPE_MAX) {
2099 invalid_vga:
2100 error_report("unknown vga type: %s", p);
2101 exit(1);
2103 while (*opts) {
2104 const char *nextopt;
2106 if (strstart(opts, ",retrace=", &nextopt)) {
2107 opts = nextopt;
2108 if (strstart(opts, "dumb", &nextopt))
2109 vga_retrace_method = VGA_RETRACE_DUMB;
2110 else if (strstart(opts, "precise", &nextopt))
2111 vga_retrace_method = VGA_RETRACE_PRECISE;
2112 else goto invalid_vga;
2113 } else goto invalid_vga;
2114 opts = nextopt;
2118 typedef enum DisplayType {
2119 DT_DEFAULT,
2120 DT_CURSES,
2121 DT_SDL,
2122 DT_COCOA,
2123 DT_GTK,
2124 DT_NONE,
2125 } DisplayType;
2127 static DisplayType select_display(const char *p)
2129 const char *opts;
2130 DisplayType display = DT_DEFAULT;
2132 if (strstart(p, "sdl", &opts)) {
2133 #ifdef CONFIG_SDL
2134 display = DT_SDL;
2135 while (*opts) {
2136 const char *nextopt;
2138 if (strstart(opts, ",frame=", &nextopt)) {
2139 opts = nextopt;
2140 if (strstart(opts, "on", &nextopt)) {
2141 no_frame = 0;
2142 } else if (strstart(opts, "off", &nextopt)) {
2143 no_frame = 1;
2144 } else {
2145 goto invalid_sdl_args;
2147 } else if (strstart(opts, ",alt_grab=", &nextopt)) {
2148 opts = nextopt;
2149 if (strstart(opts, "on", &nextopt)) {
2150 alt_grab = 1;
2151 } else if (strstart(opts, "off", &nextopt)) {
2152 alt_grab = 0;
2153 } else {
2154 goto invalid_sdl_args;
2156 } else if (strstart(opts, ",ctrl_grab=", &nextopt)) {
2157 opts = nextopt;
2158 if (strstart(opts, "on", &nextopt)) {
2159 ctrl_grab = 1;
2160 } else if (strstart(opts, "off", &nextopt)) {
2161 ctrl_grab = 0;
2162 } else {
2163 goto invalid_sdl_args;
2165 } else if (strstart(opts, ",window_close=", &nextopt)) {
2166 opts = nextopt;
2167 if (strstart(opts, "on", &nextopt)) {
2168 no_quit = 0;
2169 } else if (strstart(opts, "off", &nextopt)) {
2170 no_quit = 1;
2171 } else {
2172 goto invalid_sdl_args;
2174 } else if (strstart(opts, ",gl=", &nextopt)) {
2175 opts = nextopt;
2176 if (strstart(opts, "on", &nextopt)) {
2177 request_opengl = 1;
2178 } else if (strstart(opts, "off", &nextopt)) {
2179 request_opengl = 0;
2180 } else {
2181 goto invalid_sdl_args;
2183 } else {
2184 invalid_sdl_args:
2185 error_report("invalid SDL option string");
2186 exit(1);
2188 opts = nextopt;
2190 #else
2191 error_report("SDL support is disabled");
2192 exit(1);
2193 #endif
2194 } else if (strstart(p, "vnc", &opts)) {
2195 if (*opts == '=') {
2196 vnc_parse(opts + 1, &error_fatal);
2197 } else {
2198 error_report("VNC requires a display argument vnc=<display>");
2199 exit(1);
2201 } else if (strstart(p, "curses", &opts)) {
2202 #ifdef CONFIG_CURSES
2203 display = DT_CURSES;
2204 #else
2205 error_report("curses support is disabled");
2206 exit(1);
2207 #endif
2208 } else if (strstart(p, "gtk", &opts)) {
2209 #ifdef CONFIG_GTK
2210 display = DT_GTK;
2211 while (*opts) {
2212 const char *nextopt;
2214 if (strstart(opts, ",grab_on_hover=", &nextopt)) {
2215 opts = nextopt;
2216 if (strstart(opts, "on", &nextopt)) {
2217 grab_on_hover = true;
2218 } else if (strstart(opts, "off", &nextopt)) {
2219 grab_on_hover = false;
2220 } else {
2221 goto invalid_gtk_args;
2223 } else if (strstart(opts, ",gl=", &nextopt)) {
2224 opts = nextopt;
2225 if (strstart(opts, "on", &nextopt)) {
2226 request_opengl = 1;
2227 } else if (strstart(opts, "off", &nextopt)) {
2228 request_opengl = 0;
2229 } else {
2230 goto invalid_gtk_args;
2232 } else {
2233 invalid_gtk_args:
2234 error_report("invalid GTK option string");
2235 exit(1);
2237 opts = nextopt;
2239 #else
2240 error_report("GTK support is disabled");
2241 exit(1);
2242 #endif
2243 } else if (strstart(p, "none", &opts)) {
2244 display = DT_NONE;
2245 } else {
2246 error_report("unknown display type");
2247 exit(1);
2250 return display;
2253 static int balloon_parse(const char *arg)
2255 QemuOpts *opts;
2257 if (strcmp(arg, "none") == 0) {
2258 return 0;
2261 if (!strncmp(arg, "virtio", 6)) {
2262 if (arg[6] == ',') {
2263 /* have params -> parse them */
2264 opts = qemu_opts_parse_noisily(qemu_find_opts("device"), arg + 7,
2265 false);
2266 if (!opts)
2267 return -1;
2268 } else {
2269 /* create empty opts */
2270 opts = qemu_opts_create(qemu_find_opts("device"), NULL, 0,
2271 &error_abort);
2273 qemu_opt_set(opts, "driver", "virtio-balloon", &error_abort);
2274 return 0;
2277 return -1;
2280 #if defined(CONFIG_RUBY)
2281 /* Ruby interface for QEMU. See README.EXT for programming example. */
2282 #warning mit ruby
2283 #include <ruby.h>
2285 static VALUE qemu_open(int argc, VALUE *argv, VALUE klass)
2287 int i;
2288 for (i = 0; i < argc; i++) {
2289 argv[i] = StringValue(argv[i]);
2290 fprintf(stderr, "argv[%d] = %s\n", i, StringValuePtr(argv[i]));
2292 //~ if (rb_scan_args(argc, argv, "11", &file, &vmode) == 1) {
2293 //~ mode = 0666; /* default value */
2294 //~ }
2295 return INT2FIX(argc);
2298 void Init_qemu(void)
2300 printf("%s\n", __func__);
2301 VALUE cQEMU = rb_define_class("QEMU", rb_cObject);
2302 rb_define_singleton_method(cQEMU, "run", qemu_open, -1);
2303 #if 0
2304 rb_define_method();
2305 #endif
2307 #endif /* CONFIG_RUBY */
2309 #if defined(CONFIG_DLL)
2310 BOOL APIENTRY DllMain(HINSTANCE hModule, DWORD ul_reason_for_call, LPVOID data)
2312 return FALSE;
2314 #endif /* CONFIG_DLL */
2316 char *qemu_find_file(int type, const char *name)
2318 int i;
2319 const char *subdir;
2320 char *buf;
2322 /* Try the name as a straight path first */
2323 if (access(name, R_OK) == 0) {
2324 trace_load_file(name, name);
2325 return g_strdup(name);
2328 switch (type) {
2329 case QEMU_FILE_TYPE_BIOS:
2330 subdir = "";
2331 break;
2332 case QEMU_FILE_TYPE_KEYMAP:
2333 subdir = "keymaps/";
2334 break;
2335 default:
2336 abort();
2339 for (i = 0; i < data_dir_idx; i++) {
2340 buf = g_strdup_printf("%s/%s%s", data_dir[i], subdir, name);
2341 if (access(buf, R_OK) == 0) {
2342 trace_load_file(name, buf);
2343 return buf;
2345 g_free(buf);
2347 return NULL;
2350 static inline bool nonempty_str(const char *str)
2352 return str && *str;
2355 static int parse_fw_cfg(void *opaque, QemuOpts *opts, Error **errp)
2357 gchar *buf;
2358 size_t size;
2359 const char *name, *file, *str;
2360 FWCfgState *fw_cfg = (FWCfgState *) opaque;
2362 if (fw_cfg == NULL) {
2363 error_report("fw_cfg device not available");
2364 return -1;
2366 name = qemu_opt_get(opts, "name");
2367 file = qemu_opt_get(opts, "file");
2368 str = qemu_opt_get(opts, "string");
2370 /* we need name and either a file or the content string */
2371 if (!(nonempty_str(name) && (nonempty_str(file) || nonempty_str(str)))) {
2372 error_report("invalid argument(s)");
2373 return -1;
2375 if (nonempty_str(file) && nonempty_str(str)) {
2376 error_report("file and string are mutually exclusive");
2377 return -1;
2379 if (strlen(name) > FW_CFG_MAX_FILE_PATH - 1) {
2380 error_report("name too long (max. %d char)", FW_CFG_MAX_FILE_PATH - 1);
2381 return -1;
2383 if (strncmp(name, "opt/", 4) != 0) {
2384 error_report("warning: externally provided fw_cfg item names "
2385 "should be prefixed with \"opt/\"");
2387 if (nonempty_str(str)) {
2388 size = strlen(str); /* NUL terminator NOT included in fw_cfg blob */
2389 buf = g_memdup(str, size);
2390 } else {
2391 if (!g_file_get_contents(file, &buf, &size, NULL)) {
2392 error_report("can't load %s", file);
2393 return -1;
2396 /* For legacy, keep user files in a specific global order. */
2397 fw_cfg_set_order_override(fw_cfg, FW_CFG_ORDER_OVERRIDE_USER);
2398 fw_cfg_add_file(fw_cfg, name, buf, size);
2399 fw_cfg_reset_order_override(fw_cfg);
2400 return 0;
2403 static int device_help_func(void *opaque, QemuOpts *opts, Error **errp)
2405 return qdev_device_help(opts);
2408 static int device_init_func(void *opaque, QemuOpts *opts, Error **errp)
2410 Error *err = NULL;
2411 DeviceState *dev;
2413 dev = qdev_device_add(opts, &err);
2414 if (!dev) {
2415 error_report_err(err);
2416 return -1;
2418 object_unref(OBJECT(dev));
2419 return 0;
2422 static int chardev_init_func(void *opaque, QemuOpts *opts, Error **errp)
2424 Error *local_err = NULL;
2426 qemu_chr_new_from_opts(opts, &local_err);
2427 if (local_err) {
2428 error_report_err(local_err);
2429 return -1;
2431 return 0;
2434 #ifdef CONFIG_VIRTFS
2435 static int fsdev_init_func(void *opaque, QemuOpts *opts, Error **errp)
2437 return qemu_fsdev_add(opts);
2439 #endif
2441 static int mon_init_func(void *opaque, QemuOpts *opts, Error **errp)
2443 CharDriverState *chr;
2444 const char *chardev;
2445 const char *mode;
2446 int flags;
2448 mode = qemu_opt_get(opts, "mode");
2449 if (mode == NULL) {
2450 mode = "readline";
2452 if (strcmp(mode, "readline") == 0) {
2453 flags = MONITOR_USE_READLINE;
2454 } else if (strcmp(mode, "control") == 0) {
2455 flags = MONITOR_USE_CONTROL;
2456 } else {
2457 error_report("unknown monitor mode \"%s\"", mode);
2458 exit(1);
2461 if (qemu_opt_get_bool(opts, "pretty", 0))
2462 flags |= MONITOR_USE_PRETTY;
2464 if (qemu_opt_get_bool(opts, "default", 0)) {
2465 error_report("option 'default' does nothing and is deprecated");
2468 chardev = qemu_opt_get(opts, "chardev");
2469 chr = qemu_chr_find(chardev);
2470 if (chr == NULL) {
2471 error_report("chardev \"%s\" not found", chardev);
2472 exit(1);
2475 monitor_init(chr, flags);
2476 return 0;
2479 static void monitor_parse(const char *optarg, const char *mode, bool pretty)
2481 static int monitor_device_index = 0;
2482 QemuOpts *opts;
2483 const char *p;
2484 char label[32];
2486 if (strstart(optarg, "chardev:", &p)) {
2487 snprintf(label, sizeof(label), "%s", p);
2488 } else {
2489 snprintf(label, sizeof(label), "compat_monitor%d",
2490 monitor_device_index);
2491 opts = qemu_chr_parse_compat(label, optarg);
2492 if (!opts) {
2493 error_report("parse error: %s", optarg);
2494 exit(1);
2498 opts = qemu_opts_create(qemu_find_opts("mon"), label, 1, &error_fatal);
2499 qemu_opt_set(opts, "mode", mode, &error_abort);
2500 qemu_opt_set(opts, "chardev", label, &error_abort);
2501 qemu_opt_set_bool(opts, "pretty", pretty, &error_abort);
2502 monitor_device_index++;
2505 struct device_config {
2506 enum {
2507 DEV_USB, /* -usbdevice */
2508 DEV_BT, /* -bt */
2509 DEV_SERIAL, /* -serial */
2510 DEV_PARALLEL, /* -parallel */
2511 DEV_VIRTCON, /* -virtioconsole */
2512 DEV_DEBUGCON, /* -debugcon */
2513 DEV_GDB, /* -gdb, -s */
2514 DEV_SCLP, /* s390 sclp */
2515 } type;
2516 const char *cmdline;
2517 Location loc;
2518 QTAILQ_ENTRY(device_config) next;
2521 static QTAILQ_HEAD(, device_config) device_configs =
2522 QTAILQ_HEAD_INITIALIZER(device_configs);
2524 static void add_device_config(int type, const char *cmdline)
2526 struct device_config *conf;
2528 conf = g_malloc0(sizeof(*conf));
2529 conf->type = type;
2530 conf->cmdline = cmdline;
2531 loc_save(&conf->loc);
2532 QTAILQ_INSERT_TAIL(&device_configs, conf, next);
2535 static int foreach_device_config(int type, int (*func)(const char *cmdline))
2537 struct device_config *conf;
2538 int rc;
2540 QTAILQ_FOREACH(conf, &device_configs, next) {
2541 if (conf->type != type)
2542 continue;
2543 loc_push_restore(&conf->loc);
2544 rc = func(conf->cmdline);
2545 loc_pop(&conf->loc);
2546 if (rc) {
2547 return rc;
2550 return 0;
2553 static int serial_parse(const char *devname)
2555 static int index = 0;
2556 char label[32];
2558 if (strcmp(devname, "none") == 0)
2559 return 0;
2560 if (index == MAX_SERIAL_PORTS) {
2561 error_report("too many serial ports");
2562 exit(1);
2564 snprintf(label, sizeof(label), "serial%d", index);
2565 serial_hds[index] = qemu_chr_new(label, devname);
2566 if (!serial_hds[index]) {
2567 error_report("could not connect serial device"
2568 " to character backend '%s'", devname);
2569 return -1;
2571 index++;
2572 return 0;
2575 static int parallel_parse(const char *devname)
2577 static int index = 0;
2578 char label[32];
2580 if (strcmp(devname, "none") == 0)
2581 return 0;
2582 if (index == MAX_PARALLEL_PORTS) {
2583 error_report("too many parallel ports");
2584 exit(1);
2586 snprintf(label, sizeof(label), "parallel%d", index);
2587 parallel_hds[index] = qemu_chr_new(label, devname);
2588 if (!parallel_hds[index]) {
2589 error_report("could not connect parallel device"
2590 " to character backend '%s'", devname);
2591 return -1;
2593 index++;
2594 return 0;
2597 static int virtcon_parse(const char *devname)
2599 QemuOptsList *device = qemu_find_opts("device");
2600 static int index = 0;
2601 char label[32];
2602 QemuOpts *bus_opts, *dev_opts;
2604 if (strcmp(devname, "none") == 0)
2605 return 0;
2606 if (index == MAX_VIRTIO_CONSOLES) {
2607 error_report("too many virtio consoles");
2608 exit(1);
2611 bus_opts = qemu_opts_create(device, NULL, 0, &error_abort);
2612 qemu_opt_set(bus_opts, "driver", "virtio-serial", &error_abort);
2614 dev_opts = qemu_opts_create(device, NULL, 0, &error_abort);
2615 qemu_opt_set(dev_opts, "driver", "virtconsole", &error_abort);
2617 snprintf(label, sizeof(label), "virtcon%d", index);
2618 virtcon_hds[index] = qemu_chr_new(label, devname);
2619 if (!virtcon_hds[index]) {
2620 error_report("could not connect virtio console"
2621 " to character backend '%s'", devname);
2622 return -1;
2624 qemu_opt_set(dev_opts, "chardev", label, &error_abort);
2626 index++;
2627 return 0;
2630 static int sclp_parse(const char *devname)
2632 QemuOptsList *device = qemu_find_opts("device");
2633 static int index = 0;
2634 char label[32];
2635 QemuOpts *dev_opts;
2637 if (strcmp(devname, "none") == 0) {
2638 return 0;
2640 if (index == MAX_SCLP_CONSOLES) {
2641 error_report("too many sclp consoles");
2642 exit(1);
2645 assert(arch_type == QEMU_ARCH_S390X);
2647 dev_opts = qemu_opts_create(device, NULL, 0, NULL);
2648 qemu_opt_set(dev_opts, "driver", "sclpconsole", &error_abort);
2650 snprintf(label, sizeof(label), "sclpcon%d", index);
2651 sclp_hds[index] = qemu_chr_new(label, devname);
2652 if (!sclp_hds[index]) {
2653 error_report("could not connect sclp console"
2654 " to character backend '%s'", devname);
2655 return -1;
2657 qemu_opt_set(dev_opts, "chardev", label, &error_abort);
2659 index++;
2660 return 0;
2663 static int debugcon_parse(const char *devname)
2665 QemuOpts *opts;
2667 if (!qemu_chr_new("debugcon", devname)) {
2668 exit(1);
2670 opts = qemu_opts_create(qemu_find_opts("device"), "debugcon", 1, NULL);
2671 if (!opts) {
2672 error_report("already have a debugcon device");
2673 exit(1);
2675 qemu_opt_set(opts, "driver", "isa-debugcon", &error_abort);
2676 qemu_opt_set(opts, "chardev", "debugcon", &error_abort);
2677 return 0;
2680 static gint machine_class_cmp(gconstpointer a, gconstpointer b)
2682 const MachineClass *mc1 = a, *mc2 = b;
2683 int res;
2685 if (mc1->family == NULL) {
2686 if (mc2->family == NULL) {
2687 /* Compare standalone machine types against each other; they sort
2688 * in increasing order.
2690 return strcmp(object_class_get_name(OBJECT_CLASS(mc1)),
2691 object_class_get_name(OBJECT_CLASS(mc2)));
2694 /* Standalone machine types sort after families. */
2695 return 1;
2698 if (mc2->family == NULL) {
2699 /* Families sort before standalone machine types. */
2700 return -1;
2703 /* Families sort between each other alphabetically increasingly. */
2704 res = strcmp(mc1->family, mc2->family);
2705 if (res != 0) {
2706 return res;
2709 /* Within the same family, machine types sort in decreasing order. */
2710 return strcmp(object_class_get_name(OBJECT_CLASS(mc2)),
2711 object_class_get_name(OBJECT_CLASS(mc1)));
2714 static MachineClass *machine_parse(const char *name)
2716 MachineClass *mc = NULL;
2717 GSList *el, *machines = object_class_get_list(TYPE_MACHINE, false);
2719 if (name) {
2720 mc = find_machine(name);
2722 if (mc) {
2723 g_slist_free(machines);
2724 return mc;
2726 if (name && !is_help_option(name)) {
2727 error_report("unsupported machine type");
2728 error_printf("Use -machine help to list supported machines\n");
2729 } else {
2730 printf("Supported machines are:\n");
2731 machines = g_slist_sort(machines, machine_class_cmp);
2732 for (el = machines; el; el = el->next) {
2733 MachineClass *mc = el->data;
2734 if (mc->alias) {
2735 printf("%-20s %s (alias of %s)\n", mc->alias, mc->desc, mc->name);
2737 printf("%-20s %s%s\n", mc->name, mc->desc,
2738 mc->is_default ? " (default)" : "");
2742 g_slist_free(machines);
2743 exit(!name || !is_help_option(name));
2746 void qemu_add_exit_notifier(Notifier *notify)
2748 notifier_list_add(&exit_notifiers, notify);
2751 void qemu_remove_exit_notifier(Notifier *notify)
2753 notifier_remove(notify);
2756 static void qemu_run_exit_notifiers(void)
2758 notifier_list_notify(&exit_notifiers, NULL);
2761 static bool machine_init_done;
2763 void qemu_add_machine_init_done_notifier(Notifier *notify)
2765 notifier_list_add(&machine_init_done_notifiers, notify);
2766 if (machine_init_done) {
2767 notify->notify(notify, NULL);
2771 void qemu_remove_machine_init_done_notifier(Notifier *notify)
2773 notifier_remove(notify);
2776 static void qemu_run_machine_init_done_notifiers(void)
2778 notifier_list_notify(&machine_init_done_notifiers, NULL);
2779 machine_init_done = true;
2782 static const QEMUOption *lookup_opt(int argc, char **argv,
2783 const char **poptarg, int *poptind)
2785 const QEMUOption *popt;
2786 int optind = *poptind;
2787 char *r = argv[optind];
2788 const char *optarg;
2790 loc_set_cmdline(argv, optind, 1);
2791 optind++;
2792 /* Treat --foo the same as -foo. */
2793 if (r[1] == '-')
2794 r++;
2795 popt = qemu_options;
2796 for(;;) {
2797 if (!popt->name) {
2798 error_report("invalid option");
2799 exit(1);
2801 if (!strcmp(popt->name, r + 1))
2802 break;
2803 popt++;
2805 if (popt->flags & HAS_ARG) {
2806 if (optind >= argc) {
2807 error_report("requires an argument");
2808 exit(1);
2810 optarg = argv[optind++];
2811 loc_set_cmdline(argv, optind - 2, 2);
2812 } else {
2813 optarg = NULL;
2816 *poptarg = optarg;
2817 *poptind = optind;
2819 return popt;
2822 static MachineClass *select_machine(void)
2824 MachineClass *machine_class = find_default_machine();
2825 const char *optarg;
2826 QemuOpts *opts;
2827 Location loc;
2829 loc_push_none(&loc);
2831 opts = qemu_get_machine_opts();
2832 qemu_opts_loc_restore(opts);
2834 optarg = qemu_opt_get(opts, "type");
2835 if (optarg) {
2836 machine_class = machine_parse(optarg);
2839 if (!machine_class) {
2840 error_report("No machine specified, and there is no default");
2841 error_printf("Use -machine help to list supported machines\n");
2842 exit(1);
2845 loc_pop(&loc);
2846 return machine_class;
2849 static int machine_set_property(void *opaque,
2850 const char *name, const char *value,
2851 Error **errp)
2853 Object *obj = OBJECT(opaque);
2854 Error *local_err = NULL;
2855 char *p, *qom_name;
2857 if (strcmp(name, "type") == 0) {
2858 return 0;
2861 qom_name = g_strdup(name);
2862 for (p = qom_name; *p; p++) {
2863 if (*p == '_') {
2864 *p = '-';
2868 object_property_parse(obj, value, qom_name, &local_err);
2869 g_free(qom_name);
2871 if (local_err) {
2872 error_report_err(local_err);
2873 return -1;
2876 return 0;
2881 * Initial object creation happens before all other
2882 * QEMU data types are created. The majority of objects
2883 * can be created at this point. The rng-egd object
2884 * cannot be created here, as it depends on the chardev
2885 * already existing.
2887 static bool object_create_initial(const char *type)
2889 if (g_str_equal(type, "rng-egd")) {
2890 return false;
2894 * return false for concrete netfilters since
2895 * they depend on netdevs already existing
2897 if (g_str_equal(type, "filter-buffer") ||
2898 g_str_equal(type, "filter-dump") ||
2899 g_str_equal(type, "filter-mirror") ||
2900 g_str_equal(type, "filter-redirector") ||
2901 g_str_equal(type, "colo-compare") ||
2902 g_str_equal(type, "filter-rewriter")) {
2903 return false;
2906 /* Memory allocation by backends needs to be done
2907 * after configure_accelerator() (due to the tcg_enabled()
2908 * checks at memory_region_init_*()).
2910 * Also, allocation of large amounts of memory may delay
2911 * chardev initialization for too long, and trigger timeouts
2912 * on software that waits for a monitor socket to be created
2913 * (e.g. libvirt).
2915 if (g_str_has_prefix(type, "memory-backend-")) {
2916 return false;
2919 return true;
2924 * The remainder of object creation happens after the
2925 * creation of chardev, fsdev, net clients and device data types.
2927 static bool object_create_delayed(const char *type)
2929 return !object_create_initial(type);
2933 static void set_memory_options(uint64_t *ram_slots, ram_addr_t *maxram_size,
2934 MachineClass *mc)
2936 uint64_t sz;
2937 const char *mem_str;
2938 const char *maxmem_str, *slots_str;
2939 const ram_addr_t default_ram_size = mc->default_ram_size;
2940 QemuOpts *opts = qemu_find_opts_singleton("memory");
2941 Location loc;
2943 loc_push_none(&loc);
2944 qemu_opts_loc_restore(opts);
2946 sz = 0;
2947 mem_str = qemu_opt_get(opts, "size");
2948 if (mem_str) {
2949 if (!*mem_str) {
2950 error_report("missing 'size' option value");
2951 exit(EXIT_FAILURE);
2954 sz = qemu_opt_get_size(opts, "size", ram_size);
2956 /* Fix up legacy suffix-less format */
2957 if (g_ascii_isdigit(mem_str[strlen(mem_str) - 1])) {
2958 uint64_t overflow_check = sz;
2960 sz <<= 20;
2961 if ((sz >> 20) != overflow_check) {
2962 error_report("too large 'size' option value");
2963 exit(EXIT_FAILURE);
2968 /* backward compatibility behaviour for case "-m 0" */
2969 if (sz == 0) {
2970 sz = default_ram_size;
2973 sz = QEMU_ALIGN_UP(sz, 8192);
2974 ram_size = sz;
2975 if (ram_size != sz) {
2976 error_report("ram size too large");
2977 exit(EXIT_FAILURE);
2980 /* store value for the future use */
2981 qemu_opt_set_number(opts, "size", ram_size, &error_abort);
2982 *maxram_size = ram_size;
2984 maxmem_str = qemu_opt_get(opts, "maxmem");
2985 slots_str = qemu_opt_get(opts, "slots");
2986 if (maxmem_str && slots_str) {
2987 uint64_t slots;
2989 sz = qemu_opt_get_size(opts, "maxmem", 0);
2990 slots = qemu_opt_get_number(opts, "slots", 0);
2991 if (sz < ram_size) {
2992 error_report("invalid value of -m option maxmem: "
2993 "maximum memory size (0x%" PRIx64 ") must be at least "
2994 "the initial memory size (0x" RAM_ADDR_FMT ")",
2995 sz, ram_size);
2996 exit(EXIT_FAILURE);
2997 } else if (sz > ram_size) {
2998 if (!slots) {
2999 error_report("invalid value of -m option: maxmem was "
3000 "specified, but no hotplug slots were specified");
3001 exit(EXIT_FAILURE);
3003 } else if (slots) {
3004 error_report("invalid value of -m option maxmem: "
3005 "memory slots were specified but maximum memory size "
3006 "(0x%" PRIx64 ") is equal to the initial memory size "
3007 "(0x" RAM_ADDR_FMT ")", sz, ram_size);
3008 exit(EXIT_FAILURE);
3011 *maxram_size = sz;
3012 *ram_slots = slots;
3013 } else if ((!maxmem_str && slots_str) ||
3014 (maxmem_str && !slots_str)) {
3015 error_report("invalid -m option value: missing "
3016 "'%s' option", slots_str ? "maxmem" : "slots");
3017 exit(EXIT_FAILURE);
3020 loc_pop(&loc);
3023 static int global_init_func(void *opaque, QemuOpts *opts, Error **errp)
3025 GlobalProperty *g;
3027 g = g_malloc0(sizeof(*g));
3028 g->driver = qemu_opt_get(opts, "driver");
3029 g->property = qemu_opt_get(opts, "property");
3030 g->value = qemu_opt_get(opts, "value");
3031 g->user_provided = true;
3032 g->errp = &error_fatal;
3033 qdev_prop_register_global(g);
3034 return 0;
3037 int main(int argc, char **argv)
3039 int i;
3040 int snapshot, linux_boot;
3041 const char *initrd_filename;
3042 const char *kernel_filename, *kernel_cmdline;
3043 const char *boot_order = NULL;
3044 const char *boot_once = NULL;
3045 DisplayState *ds;
3046 int cyls, heads, secs, translation;
3047 QemuOpts *hda_opts = NULL, *opts, *machine_opts, *icount_opts = NULL;
3048 QemuOptsList *olist;
3049 int optind;
3050 const char *optarg;
3051 const char *loadvm = NULL;
3052 MachineClass *machine_class;
3053 const char *cpu_model;
3054 const char *vga_model = NULL;
3055 const char *qtest_chrdev = NULL;
3056 const char *qtest_log = NULL;
3057 const char *pid_file = NULL;
3058 const char *incoming = NULL;
3059 bool defconfig = true;
3060 bool userconfig = true;
3061 bool nographic = false;
3062 DisplayType display_type = DT_DEFAULT;
3063 int display_remote = 0;
3064 const char *log_mask = NULL;
3065 const char *log_file = NULL;
3066 char *trace_file = NULL;
3067 ram_addr_t maxram_size;
3068 uint64_t ram_slots = 0;
3069 FILE *vmstate_dump_file = NULL;
3070 Error *main_loop_err = NULL;
3071 Error *err = NULL;
3072 bool list_data_dirs = false;
3074 module_call_init(MODULE_INIT_TRACE);
3076 qemu_init_cpu_list();
3077 qemu_init_cpu_loop();
3078 qemu_mutex_lock_iothread();
3080 atexit(qemu_run_exit_notifiers);
3081 error_set_progname(argv[0]);
3082 qemu_init_exec_dir(argv[0]);
3084 module_call_init(MODULE_INIT_QOM);
3085 module_call_init(MODULE_INIT_QAPI);
3087 qemu_add_opts(&qemu_drive_opts);
3088 qemu_add_drive_opts(&qemu_legacy_drive_opts);
3089 qemu_add_drive_opts(&qemu_common_drive_opts);
3090 qemu_add_drive_opts(&qemu_drive_opts);
3091 qemu_add_drive_opts(&bdrv_runtime_opts);
3092 qemu_add_opts(&qemu_chardev_opts);
3093 qemu_add_opts(&qemu_device_opts);
3094 qemu_add_opts(&qemu_netdev_opts);
3095 qemu_add_opts(&qemu_net_opts);
3096 qemu_add_opts(&qemu_rtc_opts);
3097 qemu_add_opts(&qemu_global_opts);
3098 qemu_add_opts(&qemu_mon_opts);
3099 qemu_add_opts(&qemu_trace_opts);
3100 qemu_add_opts(&qemu_option_rom_opts);
3101 qemu_add_opts(&qemu_machine_opts);
3102 qemu_add_opts(&qemu_mem_opts);
3103 qemu_add_opts(&qemu_smp_opts);
3104 qemu_add_opts(&qemu_boot_opts);
3105 qemu_add_opts(&qemu_sandbox_opts);
3106 qemu_add_opts(&qemu_add_fd_opts);
3107 qemu_add_opts(&qemu_object_opts);
3108 qemu_add_opts(&qemu_tpmdev_opts);
3109 qemu_add_opts(&qemu_realtime_opts);
3110 qemu_add_opts(&qemu_msg_opts);
3111 qemu_add_opts(&qemu_name_opts);
3112 qemu_add_opts(&qemu_numa_opts);
3113 qemu_add_opts(&qemu_icount_opts);
3114 qemu_add_opts(&qemu_semihosting_config_opts);
3115 qemu_add_opts(&qemu_fw_cfg_opts);
3116 #ifdef CONFIG_LIBISCSI
3117 qemu_add_opts(&qemu_iscsi_opts);
3118 #endif
3119 module_call_init(MODULE_INIT_OPTS);
3121 runstate_init();
3123 if (qcrypto_init(&err) < 0) {
3124 error_reportf_err(err, "cannot initialize crypto: ");
3125 exit(1);
3127 rtc_clock = QEMU_CLOCK_HOST;
3129 QLIST_INIT (&vm_change_state_head);
3130 os_setup_early_signal_handling();
3132 cpu_model = NULL;
3133 snapshot = 0;
3134 cyls = heads = secs = 0;
3135 translation = BIOS_ATA_TRANSLATION_AUTO;
3137 nb_nics = 0;
3139 bdrv_init_with_whitelist();
3141 autostart = 1;
3143 /* first pass of option parsing */
3144 optind = 1;
3145 while (optind < argc) {
3146 if (argv[optind][0] != '-') {
3147 /* disk image */
3148 optind++;
3149 } else {
3150 const QEMUOption *popt;
3152 popt = lookup_opt(argc, argv, &optarg, &optind);
3153 switch (popt->index) {
3154 case QEMU_OPTION_nodefconfig:
3155 defconfig = false;
3156 break;
3157 case QEMU_OPTION_nouserconfig:
3158 userconfig = false;
3159 break;
3164 if (defconfig) {
3165 int ret;
3166 ret = qemu_read_default_config_files(userconfig);
3167 if (ret < 0) {
3168 exit(1);
3172 /* second pass of option parsing */
3173 optind = 1;
3174 for(;;) {
3175 if (optind >= argc)
3176 break;
3177 if (argv[optind][0] != '-') {
3178 hda_opts = drive_add(IF_DEFAULT, 0, argv[optind++], HD_OPTS);
3179 } else {
3180 const QEMUOption *popt;
3182 popt = lookup_opt(argc, argv, &optarg, &optind);
3183 if (!(popt->arch_mask & arch_type)) {
3184 error_report("Option not supported for this target");
3185 exit(1);
3187 switch(popt->index) {
3188 case QEMU_OPTION_no_kvm_irqchip: {
3189 olist = qemu_find_opts("machine");
3190 qemu_opts_parse_noisily(olist, "kernel_irqchip=off", false);
3191 break;
3193 case QEMU_OPTION_cpu:
3194 /* hw initialization will check this */
3195 cpu_model = optarg;
3196 break;
3197 case QEMU_OPTION_hda:
3199 char buf[256];
3200 if (cyls == 0)
3201 snprintf(buf, sizeof(buf), "%s", HD_OPTS);
3202 else
3203 snprintf(buf, sizeof(buf),
3204 "%s,cyls=%d,heads=%d,secs=%d%s",
3205 HD_OPTS , cyls, heads, secs,
3206 translation == BIOS_ATA_TRANSLATION_LBA ?
3207 ",trans=lba" :
3208 translation == BIOS_ATA_TRANSLATION_NONE ?
3209 ",trans=none" : "");
3210 drive_add(IF_DEFAULT, 0, optarg, buf);
3211 break;
3213 case QEMU_OPTION_hdb:
3214 case QEMU_OPTION_hdc:
3215 case QEMU_OPTION_hdd:
3216 drive_add(IF_DEFAULT, popt->index - QEMU_OPTION_hda, optarg,
3217 HD_OPTS);
3218 break;
3219 case QEMU_OPTION_drive:
3220 if (drive_def(optarg) == NULL) {
3221 exit(1);
3223 break;
3224 case QEMU_OPTION_set:
3225 if (qemu_set_option(optarg) != 0)
3226 exit(1);
3227 break;
3228 case QEMU_OPTION_global:
3229 if (qemu_global_option(optarg) != 0)
3230 exit(1);
3231 break;
3232 case QEMU_OPTION_mtdblock:
3233 drive_add(IF_MTD, -1, optarg, MTD_OPTS);
3234 break;
3235 case QEMU_OPTION_sd:
3236 drive_add(IF_SD, -1, optarg, SD_OPTS);
3237 break;
3238 case QEMU_OPTION_pflash:
3239 drive_add(IF_PFLASH, -1, optarg, PFLASH_OPTS);
3240 break;
3241 case QEMU_OPTION_snapshot:
3242 snapshot = 1;
3243 break;
3244 case QEMU_OPTION_hdachs:
3246 const char *p;
3247 p = optarg;
3248 cyls = strtol(p, (char **)&p, 0);
3249 if (cyls < 1 || cyls > 16383)
3250 goto chs_fail;
3251 if (*p != ',')
3252 goto chs_fail;
3253 p++;
3254 heads = strtol(p, (char **)&p, 0);
3255 if (heads < 1 || heads > 16)
3256 goto chs_fail;
3257 if (*p != ',')
3258 goto chs_fail;
3259 p++;
3260 secs = strtol(p, (char **)&p, 0);
3261 if (secs < 1 || secs > 63)
3262 goto chs_fail;
3263 if (*p == ',') {
3264 p++;
3265 if (!strcmp(p, "large")) {
3266 translation = BIOS_ATA_TRANSLATION_LARGE;
3267 } else if (!strcmp(p, "rechs")) {
3268 translation = BIOS_ATA_TRANSLATION_RECHS;
3269 } else if (!strcmp(p, "none")) {
3270 translation = BIOS_ATA_TRANSLATION_NONE;
3271 } else if (!strcmp(p, "lba")) {
3272 translation = BIOS_ATA_TRANSLATION_LBA;
3273 } else if (!strcmp(p, "auto")) {
3274 translation = BIOS_ATA_TRANSLATION_AUTO;
3275 } else {
3276 goto chs_fail;
3278 } else if (*p != '\0') {
3279 chs_fail:
3280 error_report("invalid physical CHS format");
3281 exit(1);
3283 if (hda_opts != NULL) {
3284 qemu_opt_set_number(hda_opts, "cyls", cyls,
3285 &error_abort);
3286 qemu_opt_set_number(hda_opts, "heads", heads,
3287 &error_abort);
3288 qemu_opt_set_number(hda_opts, "secs", secs,
3289 &error_abort);
3290 if (translation == BIOS_ATA_TRANSLATION_LARGE) {
3291 qemu_opt_set(hda_opts, "trans", "large",
3292 &error_abort);
3293 } else if (translation == BIOS_ATA_TRANSLATION_RECHS) {
3294 qemu_opt_set(hda_opts, "trans", "rechs",
3295 &error_abort);
3296 } else if (translation == BIOS_ATA_TRANSLATION_LBA) {
3297 qemu_opt_set(hda_opts, "trans", "lba",
3298 &error_abort);
3299 } else if (translation == BIOS_ATA_TRANSLATION_NONE) {
3300 qemu_opt_set(hda_opts, "trans", "none",
3301 &error_abort);
3305 break;
3306 case QEMU_OPTION_numa:
3307 opts = qemu_opts_parse_noisily(qemu_find_opts("numa"),
3308 optarg, true);
3309 if (!opts) {
3310 exit(1);
3312 break;
3313 case QEMU_OPTION_display:
3314 display_type = select_display(optarg);
3315 break;
3316 case QEMU_OPTION_nographic:
3317 olist = qemu_find_opts("machine");
3318 qemu_opts_parse_noisily(olist, "graphics=off", false);
3319 nographic = true;
3320 display_type = DT_NONE;
3321 break;
3322 case QEMU_OPTION_curses:
3323 #ifdef CONFIG_CURSES
3324 display_type = DT_CURSES;
3325 #else
3326 error_report("curses support is disabled");
3327 exit(1);
3328 #endif
3329 break;
3330 case QEMU_OPTION_portrait:
3331 graphic_rotate = 90;
3332 break;
3333 case QEMU_OPTION_rotate:
3334 graphic_rotate = strtol(optarg, (char **) &optarg, 10);
3335 if (graphic_rotate != 0 && graphic_rotate != 90 &&
3336 graphic_rotate != 180 && graphic_rotate != 270) {
3337 error_report("only 90, 180, 270 deg rotation is available");
3338 exit(1);
3340 break;
3341 case QEMU_OPTION_kernel:
3342 qemu_opts_set(qemu_find_opts("machine"), 0, "kernel", optarg,
3343 &error_abort);
3344 break;
3345 case QEMU_OPTION_initrd:
3346 qemu_opts_set(qemu_find_opts("machine"), 0, "initrd", optarg,
3347 &error_abort);
3348 break;
3349 case QEMU_OPTION_append:
3350 qemu_opts_set(qemu_find_opts("machine"), 0, "append", optarg,
3351 &error_abort);
3352 break;
3353 case QEMU_OPTION_dtb:
3354 qemu_opts_set(qemu_find_opts("machine"), 0, "dtb", optarg,
3355 &error_abort);
3356 break;
3357 case QEMU_OPTION_cdrom:
3358 drive_add(IF_DEFAULT, 2, optarg, CDROM_OPTS);
3359 break;
3360 case QEMU_OPTION_boot:
3361 opts = qemu_opts_parse_noisily(qemu_find_opts("boot-opts"),
3362 optarg, true);
3363 if (!opts) {
3364 exit(1);
3366 break;
3367 case QEMU_OPTION_fda:
3368 case QEMU_OPTION_fdb:
3369 drive_add(IF_FLOPPY, popt->index - QEMU_OPTION_fda,
3370 optarg, FD_OPTS);
3371 break;
3372 case QEMU_OPTION_no_fd_bootchk:
3373 fd_bootchk = 0;
3374 break;
3375 case QEMU_OPTION_netdev:
3376 default_net = 0;
3377 if (net_client_parse(qemu_find_opts("netdev"), optarg) == -1) {
3378 exit(1);
3380 break;
3381 case QEMU_OPTION_net:
3382 default_net = 0;
3383 if (net_client_parse(qemu_find_opts("net"), optarg) == -1) {
3384 exit(1);
3386 break;
3387 #ifdef CONFIG_LIBISCSI
3388 case QEMU_OPTION_iscsi:
3389 opts = qemu_opts_parse_noisily(qemu_find_opts("iscsi"),
3390 optarg, false);
3391 if (!opts) {
3392 exit(1);
3394 break;
3395 #endif
3396 #ifdef CONFIG_SLIRP
3397 case QEMU_OPTION_tftp:
3398 error_report("The -tftp option is deprecated. "
3399 "Please use '-netdev user,tftp=...' instead.");
3400 legacy_tftp_prefix = optarg;
3401 break;
3402 case QEMU_OPTION_bootp:
3403 error_report("The -bootp option is deprecated. "
3404 "Please use '-netdev user,bootfile=...' instead.");
3405 legacy_bootp_filename = optarg;
3406 break;
3407 case QEMU_OPTION_redir:
3408 error_report("The -redir option is deprecated. "
3409 "Please use '-netdev user,hostfwd=...' instead.");
3410 if (net_slirp_redir(optarg) < 0)
3411 exit(1);
3412 break;
3413 #endif
3414 case QEMU_OPTION_bt:
3415 add_device_config(DEV_BT, optarg);
3416 break;
3417 case QEMU_OPTION_audio_help:
3418 AUD_help ();
3419 exit (0);
3420 break;
3421 case QEMU_OPTION_soundhw:
3422 select_soundhw (optarg);
3423 break;
3424 case QEMU_OPTION_h:
3425 help(0);
3426 break;
3427 case QEMU_OPTION_version:
3428 version();
3429 exit(0);
3430 break;
3431 case QEMU_OPTION_m:
3432 opts = qemu_opts_parse_noisily(qemu_find_opts("memory"),
3433 optarg, true);
3434 if (!opts) {
3435 exit(EXIT_FAILURE);
3437 break;
3438 #ifdef CONFIG_TPM
3439 case QEMU_OPTION_tpmdev:
3440 if (tpm_config_parse(qemu_find_opts("tpmdev"), optarg) < 0) {
3441 exit(1);
3443 break;
3444 #endif
3445 case QEMU_OPTION_mempath:
3446 mem_path = optarg;
3447 break;
3448 case QEMU_OPTION_mem_prealloc:
3449 mem_prealloc = 1;
3450 break;
3451 case QEMU_OPTION_d:
3452 log_mask = optarg;
3453 break;
3454 case QEMU_OPTION_D:
3455 log_file = optarg;
3456 break;
3457 case QEMU_OPTION_DFILTER:
3458 qemu_set_dfilter_ranges(optarg, &error_fatal);
3459 break;
3460 case QEMU_OPTION_s:
3461 add_device_config(DEV_GDB, "tcp::" DEFAULT_GDBSTUB_PORT);
3462 break;
3463 case QEMU_OPTION_gdb:
3464 add_device_config(DEV_GDB, optarg);
3465 break;
3466 case QEMU_OPTION_L:
3467 if (is_help_option(optarg)) {
3468 list_data_dirs = true;
3469 } else if (data_dir_idx < ARRAY_SIZE(data_dir)) {
3470 data_dir[data_dir_idx++] = optarg;
3472 break;
3473 case QEMU_OPTION_bios:
3474 qemu_opts_set(qemu_find_opts("machine"), 0, "firmware", optarg,
3475 &error_abort);
3476 break;
3477 case QEMU_OPTION_singlestep:
3478 singlestep = 1;
3479 break;
3480 case QEMU_OPTION_S:
3481 autostart = 0;
3482 break;
3483 case QEMU_OPTION_k:
3484 keyboard_layout = optarg;
3485 break;
3486 case QEMU_OPTION_localtime:
3487 rtc_utc = 0;
3488 break;
3489 case QEMU_OPTION_vga:
3490 vga_model = optarg;
3491 default_vga = 0;
3492 break;
3493 case QEMU_OPTION_g:
3495 const char *p;
3496 int w, h, depth;
3497 p = optarg;
3498 w = strtol(p, (char **)&p, 10);
3499 if (w <= 0) {
3500 graphic_error:
3501 error_report("invalid resolution or depth");
3502 exit(1);
3504 if (*p != 'x')
3505 goto graphic_error;
3506 p++;
3507 h = strtol(p, (char **)&p, 10);
3508 if (h <= 0)
3509 goto graphic_error;
3510 if (*p == 'x') {
3511 p++;
3512 depth = strtol(p, (char **)&p, 10);
3513 if (depth != 8 && depth != 15 && depth != 16 &&
3514 depth != 24 && depth != 32)
3515 goto graphic_error;
3516 } else if (*p == '\0') {
3517 depth = graphic_depth;
3518 } else {
3519 goto graphic_error;
3522 graphic_width = w;
3523 graphic_height = h;
3524 graphic_depth = depth;
3526 break;
3527 case QEMU_OPTION_echr:
3529 char *r;
3530 term_escape_char = strtol(optarg, &r, 0);
3531 if (r == optarg)
3532 printf("Bad argument to echr\n");
3533 break;
3535 case QEMU_OPTION_monitor:
3536 default_monitor = 0;
3537 if (strncmp(optarg, "none", 4)) {
3538 monitor_parse(optarg, "readline", false);
3540 break;
3541 case QEMU_OPTION_qmp:
3542 monitor_parse(optarg, "control", false);
3543 default_monitor = 0;
3544 break;
3545 case QEMU_OPTION_qmp_pretty:
3546 monitor_parse(optarg, "control", true);
3547 default_monitor = 0;
3548 break;
3549 case QEMU_OPTION_mon:
3550 opts = qemu_opts_parse_noisily(qemu_find_opts("mon"), optarg,
3551 true);
3552 if (!opts) {
3553 exit(1);
3555 default_monitor = 0;
3556 break;
3557 case QEMU_OPTION_chardev:
3558 opts = qemu_opts_parse_noisily(qemu_find_opts("chardev"),
3559 optarg, true);
3560 if (!opts) {
3561 exit(1);
3563 break;
3564 case QEMU_OPTION_fsdev:
3565 olist = qemu_find_opts("fsdev");
3566 if (!olist) {
3567 error_report("fsdev support is disabled");
3568 exit(1);
3570 opts = qemu_opts_parse_noisily(olist, optarg, true);
3571 if (!opts) {
3572 exit(1);
3574 break;
3575 case QEMU_OPTION_virtfs: {
3576 QemuOpts *fsdev;
3577 QemuOpts *device;
3578 const char *writeout, *sock_fd, *socket;
3580 olist = qemu_find_opts("virtfs");
3581 if (!olist) {
3582 error_report("virtfs support is disabled");
3583 exit(1);
3585 opts = qemu_opts_parse_noisily(olist, optarg, true);
3586 if (!opts) {
3587 exit(1);
3590 if (qemu_opt_get(opts, "fsdriver") == NULL ||
3591 qemu_opt_get(opts, "mount_tag") == NULL) {
3592 error_report("Usage: -virtfs fsdriver,mount_tag=tag");
3593 exit(1);
3595 fsdev = qemu_opts_create(qemu_find_opts("fsdev"),
3596 qemu_opt_get(opts, "mount_tag"),
3597 1, NULL);
3598 if (!fsdev) {
3599 error_report("duplicate fsdev id: %s",
3600 qemu_opt_get(opts, "mount_tag"));
3601 exit(1);
3604 writeout = qemu_opt_get(opts, "writeout");
3605 if (writeout) {
3606 #ifdef CONFIG_SYNC_FILE_RANGE
3607 qemu_opt_set(fsdev, "writeout", writeout, &error_abort);
3608 #else
3609 error_report("writeout=immediate not supported "
3610 "on this platform");
3611 exit(1);
3612 #endif
3614 qemu_opt_set(fsdev, "fsdriver",
3615 qemu_opt_get(opts, "fsdriver"), &error_abort);
3616 qemu_opt_set(fsdev, "path", qemu_opt_get(opts, "path"),
3617 &error_abort);
3618 qemu_opt_set(fsdev, "security_model",
3619 qemu_opt_get(opts, "security_model"),
3620 &error_abort);
3621 socket = qemu_opt_get(opts, "socket");
3622 if (socket) {
3623 qemu_opt_set(fsdev, "socket", socket, &error_abort);
3625 sock_fd = qemu_opt_get(opts, "sock_fd");
3626 if (sock_fd) {
3627 qemu_opt_set(fsdev, "sock_fd", sock_fd, &error_abort);
3630 qemu_opt_set_bool(fsdev, "readonly",
3631 qemu_opt_get_bool(opts, "readonly", 0),
3632 &error_abort);
3633 device = qemu_opts_create(qemu_find_opts("device"), NULL, 0,
3634 &error_abort);
3635 qemu_opt_set(device, "driver", "virtio-9p-pci", &error_abort);
3636 qemu_opt_set(device, "fsdev",
3637 qemu_opt_get(opts, "mount_tag"), &error_abort);
3638 qemu_opt_set(device, "mount_tag",
3639 qemu_opt_get(opts, "mount_tag"), &error_abort);
3640 break;
3642 case QEMU_OPTION_virtfs_synth: {
3643 QemuOpts *fsdev;
3644 QemuOpts *device;
3646 fsdev = qemu_opts_create(qemu_find_opts("fsdev"), "v_synth",
3647 1, NULL);
3648 if (!fsdev) {
3649 error_report("duplicate option: %s", "virtfs_synth");
3650 exit(1);
3652 qemu_opt_set(fsdev, "fsdriver", "synth", &error_abort);
3654 device = qemu_opts_create(qemu_find_opts("device"), NULL, 0,
3655 &error_abort);
3656 qemu_opt_set(device, "driver", "virtio-9p-pci", &error_abort);
3657 qemu_opt_set(device, "fsdev", "v_synth", &error_abort);
3658 qemu_opt_set(device, "mount_tag", "v_synth", &error_abort);
3659 break;
3661 case QEMU_OPTION_serial:
3662 add_device_config(DEV_SERIAL, optarg);
3663 default_serial = 0;
3664 if (strncmp(optarg, "mon:", 4) == 0) {
3665 default_monitor = 0;
3667 break;
3668 case QEMU_OPTION_watchdog:
3669 if (watchdog) {
3670 error_report("only one watchdog option may be given");
3671 return 1;
3673 watchdog = optarg;
3674 break;
3675 case QEMU_OPTION_watchdog_action:
3676 if (select_watchdog_action(optarg) == -1) {
3677 error_report("unknown -watchdog-action parameter");
3678 exit(1);
3680 break;
3681 case QEMU_OPTION_virtiocon:
3682 add_device_config(DEV_VIRTCON, optarg);
3683 default_virtcon = 0;
3684 if (strncmp(optarg, "mon:", 4) == 0) {
3685 default_monitor = 0;
3687 break;
3688 case QEMU_OPTION_parallel:
3689 add_device_config(DEV_PARALLEL, optarg);
3690 default_parallel = 0;
3691 if (strncmp(optarg, "mon:", 4) == 0) {
3692 default_monitor = 0;
3694 break;
3695 case QEMU_OPTION_debugcon:
3696 add_device_config(DEV_DEBUGCON, optarg);
3697 break;
3698 case QEMU_OPTION_loadvm:
3699 loadvm = optarg;
3700 break;
3701 case QEMU_OPTION_full_screen:
3702 full_screen = 1;
3703 break;
3704 case QEMU_OPTION_no_frame:
3705 no_frame = 1;
3706 break;
3707 case QEMU_OPTION_alt_grab:
3708 alt_grab = 1;
3709 break;
3710 case QEMU_OPTION_ctrl_grab:
3711 ctrl_grab = 1;
3712 break;
3713 case QEMU_OPTION_no_quit:
3714 no_quit = 1;
3715 break;
3716 case QEMU_OPTION_sdl:
3717 #ifdef CONFIG_SDL
3718 display_type = DT_SDL;
3719 break;
3720 #else
3721 error_report("SDL support is disabled");
3722 exit(1);
3723 #endif
3724 case QEMU_OPTION_pidfile:
3725 pid_file = optarg;
3726 break;
3727 case QEMU_OPTION_win2k_hack:
3728 win2k_install_hack = 1;
3729 break;
3730 case QEMU_OPTION_rtc_td_hack: {
3731 static GlobalProperty slew_lost_ticks = {
3732 .driver = "mc146818rtc",
3733 .property = "lost_tick_policy",
3734 .value = "slew",
3737 qdev_prop_register_global(&slew_lost_ticks);
3738 break;
3740 case QEMU_OPTION_acpitable:
3741 opts = qemu_opts_parse_noisily(qemu_find_opts("acpi"),
3742 optarg, true);
3743 if (!opts) {
3744 exit(1);
3746 do_acpitable_option(opts);
3747 break;
3748 case QEMU_OPTION_smbios:
3749 opts = qemu_opts_parse_noisily(qemu_find_opts("smbios"),
3750 optarg, false);
3751 if (!opts) {
3752 exit(1);
3754 do_smbios_option(opts);
3755 break;
3756 case QEMU_OPTION_fwcfg:
3757 opts = qemu_opts_parse_noisily(qemu_find_opts("fw_cfg"),
3758 optarg, true);
3759 if (opts == NULL) {
3760 exit(1);
3762 break;
3763 case QEMU_OPTION_enable_kvm:
3764 olist = qemu_find_opts("machine");
3765 qemu_opts_parse_noisily(olist, "accel=kvm", false);
3766 break;
3767 case QEMU_OPTION_enable_hax:
3768 olist = qemu_find_opts("machine");
3769 qemu_opts_parse_noisily(olist, "accel=hax", false);
3770 break;
3771 case QEMU_OPTION_M:
3772 case QEMU_OPTION_machine:
3773 olist = qemu_find_opts("machine");
3774 opts = qemu_opts_parse_noisily(olist, optarg, true);
3775 if (!opts) {
3776 exit(1);
3778 break;
3779 case QEMU_OPTION_no_kvm:
3780 olist = qemu_find_opts("machine");
3781 qemu_opts_parse_noisily(olist, "accel=tcg", false);
3782 break;
3783 case QEMU_OPTION_no_kvm_pit: {
3784 error_report("warning: ignoring deprecated option");
3785 break;
3787 case QEMU_OPTION_no_kvm_pit_reinjection: {
3788 static GlobalProperty kvm_pit_lost_tick_policy = {
3789 .driver = "kvm-pit",
3790 .property = "lost_tick_policy",
3791 .value = "discard",
3794 error_report("warning: deprecated, replaced by "
3795 "-global kvm-pit.lost_tick_policy=discard");
3796 qdev_prop_register_global(&kvm_pit_lost_tick_policy);
3797 break;
3799 case QEMU_OPTION_usb:
3800 olist = qemu_find_opts("machine");
3801 qemu_opts_parse_noisily(olist, "usb=on", false);
3802 break;
3803 case QEMU_OPTION_usbdevice:
3804 olist = qemu_find_opts("machine");
3805 qemu_opts_parse_noisily(olist, "usb=on", false);
3806 add_device_config(DEV_USB, optarg);
3807 break;
3808 case QEMU_OPTION_device:
3809 if (!qemu_opts_parse_noisily(qemu_find_opts("device"),
3810 optarg, true)) {
3811 exit(1);
3813 break;
3814 case QEMU_OPTION_smp:
3815 if (!qemu_opts_parse_noisily(qemu_find_opts("smp-opts"),
3816 optarg, true)) {
3817 exit(1);
3819 break;
3820 case QEMU_OPTION_vnc:
3821 vnc_parse(optarg, &error_fatal);
3822 break;
3823 case QEMU_OPTION_no_acpi:
3824 acpi_enabled = 0;
3825 break;
3826 case QEMU_OPTION_no_hpet:
3827 no_hpet = 1;
3828 break;
3829 case QEMU_OPTION_balloon:
3830 if (balloon_parse(optarg) < 0) {
3831 error_report("unknown -balloon argument %s", optarg);
3832 exit(1);
3834 break;
3835 case QEMU_OPTION_no_reboot:
3836 no_reboot = 1;
3837 break;
3838 case QEMU_OPTION_no_shutdown:
3839 no_shutdown = 1;
3840 break;
3841 case QEMU_OPTION_show_cursor:
3842 cursor_hide = 0;
3843 break;
3844 case QEMU_OPTION_uuid:
3845 if (qemu_uuid_parse(optarg, &qemu_uuid) < 0) {
3846 error_report("failed to parse UUID string: wrong format");
3847 exit(1);
3849 qemu_uuid_set = true;
3850 break;
3851 case QEMU_OPTION_option_rom:
3852 if (nb_option_roms >= MAX_OPTION_ROMS) {
3853 error_report("too many option ROMs");
3854 exit(1);
3856 opts = qemu_opts_parse_noisily(qemu_find_opts("option-rom"),
3857 optarg, true);
3858 if (!opts) {
3859 exit(1);
3861 option_rom[nb_option_roms].name = qemu_opt_get(opts, "romfile");
3862 option_rom[nb_option_roms].bootindex =
3863 qemu_opt_get_number(opts, "bootindex", -1);
3864 if (!option_rom[nb_option_roms].name) {
3865 error_report("Option ROM file is not specified");
3866 exit(1);
3868 nb_option_roms++;
3869 break;
3870 case QEMU_OPTION_semihosting:
3871 semihosting.enabled = true;
3872 semihosting.target = SEMIHOSTING_TARGET_AUTO;
3873 break;
3874 case QEMU_OPTION_semihosting_config:
3875 semihosting.enabled = true;
3876 opts = qemu_opts_parse_noisily(qemu_find_opts("semihosting-config"),
3877 optarg, false);
3878 if (opts != NULL) {
3879 semihosting.enabled = qemu_opt_get_bool(opts, "enable",
3880 true);
3881 const char *target = qemu_opt_get(opts, "target");
3882 if (target != NULL) {
3883 if (strcmp("native", target) == 0) {
3884 semihosting.target = SEMIHOSTING_TARGET_NATIVE;
3885 } else if (strcmp("gdb", target) == 0) {
3886 semihosting.target = SEMIHOSTING_TARGET_GDB;
3887 } else if (strcmp("auto", target) == 0) {
3888 semihosting.target = SEMIHOSTING_TARGET_AUTO;
3889 } else {
3890 error_report("unsupported semihosting-config %s",
3891 optarg);
3892 exit(1);
3894 } else {
3895 semihosting.target = SEMIHOSTING_TARGET_AUTO;
3897 /* Set semihosting argument count and vector */
3898 qemu_opt_foreach(opts, add_semihosting_arg,
3899 &semihosting, NULL);
3900 } else {
3901 error_report("unsupported semihosting-config %s", optarg);
3902 exit(1);
3904 break;
3905 case QEMU_OPTION_tdf:
3906 error_report("warning: ignoring deprecated option");
3907 break;
3908 case QEMU_OPTION_name:
3909 opts = qemu_opts_parse_noisily(qemu_find_opts("name"),
3910 optarg, true);
3911 if (!opts) {
3912 exit(1);
3914 break;
3915 case QEMU_OPTION_prom_env:
3916 if (nb_prom_envs >= MAX_PROM_ENVS) {
3917 error_report("too many prom variables");
3918 exit(1);
3920 prom_envs[nb_prom_envs] = optarg;
3921 nb_prom_envs++;
3922 break;
3923 case QEMU_OPTION_old_param:
3924 old_param = 1;
3925 break;
3926 case QEMU_OPTION_clock:
3927 /* Clock options no longer exist. Keep this option for
3928 * backward compatibility.
3930 break;
3931 case QEMU_OPTION_startdate:
3932 configure_rtc_date_offset(optarg, 1);
3933 break;
3934 case QEMU_OPTION_rtc:
3935 opts = qemu_opts_parse_noisily(qemu_find_opts("rtc"), optarg,
3936 false);
3937 if (!opts) {
3938 exit(1);
3940 configure_rtc(opts);
3941 break;
3942 case QEMU_OPTION_tb_size:
3943 tcg_tb_size = strtol(optarg, NULL, 0);
3944 if (tcg_tb_size < 0) {
3945 tcg_tb_size = 0;
3947 break;
3948 case QEMU_OPTION_icount:
3949 icount_opts = qemu_opts_parse_noisily(qemu_find_opts("icount"),
3950 optarg, true);
3951 if (!icount_opts) {
3952 exit(1);
3954 break;
3955 case QEMU_OPTION_incoming:
3956 if (!incoming) {
3957 runstate_set(RUN_STATE_INMIGRATE);
3959 incoming = optarg;
3960 break;
3961 case QEMU_OPTION_nodefaults:
3962 has_defaults = 0;
3963 break;
3964 case QEMU_OPTION_xen_domid:
3965 if (!(xen_available())) {
3966 error_report("Option not supported for this target");
3967 exit(1);
3969 xen_domid = atoi(optarg);
3970 break;
3971 case QEMU_OPTION_xen_create:
3972 if (!(xen_available())) {
3973 error_report("Option not supported for this target");
3974 exit(1);
3976 xen_mode = XEN_CREATE;
3977 break;
3978 case QEMU_OPTION_xen_attach:
3979 if (!(xen_available())) {
3980 error_report("Option not supported for this target");
3981 exit(1);
3983 xen_mode = XEN_ATTACH;
3984 break;
3985 case QEMU_OPTION_trace:
3986 g_free(trace_file);
3987 trace_file = trace_opt_parse(optarg);
3988 break;
3989 case QEMU_OPTION_trace_unassigned:
3990 trace_unassigned = true;
3991 break;
3992 case QEMU_OPTION_readconfig:
3994 int ret = qemu_read_config_file(optarg);
3995 if (ret < 0) {
3996 error_report("read config %s: %s", optarg,
3997 strerror(-ret));
3998 exit(1);
4000 break;
4002 case QEMU_OPTION_spice:
4003 olist = qemu_find_opts("spice");
4004 if (!olist) {
4005 error_report("spice support is disabled");
4006 exit(1);
4008 opts = qemu_opts_parse_noisily(olist, optarg, false);
4009 if (!opts) {
4010 exit(1);
4012 display_remote++;
4013 break;
4014 case QEMU_OPTION_writeconfig:
4016 FILE *fp;
4017 if (strcmp(optarg, "-") == 0) {
4018 fp = stdout;
4019 } else {
4020 fp = fopen(optarg, "w");
4021 if (fp == NULL) {
4022 error_report("open %s: %s", optarg,
4023 strerror(errno));
4024 exit(1);
4027 qemu_config_write(fp);
4028 if (fp != stdout) {
4029 fclose(fp);
4031 break;
4033 case QEMU_OPTION_qtest:
4034 qtest_chrdev = optarg;
4035 break;
4036 case QEMU_OPTION_qtest_log:
4037 qtest_log = optarg;
4038 break;
4039 case QEMU_OPTION_sandbox:
4040 opts = qemu_opts_parse_noisily(qemu_find_opts("sandbox"),
4041 optarg, true);
4042 if (!opts) {
4043 exit(1);
4045 break;
4046 case QEMU_OPTION_add_fd:
4047 #ifndef _WIN32
4048 opts = qemu_opts_parse_noisily(qemu_find_opts("add-fd"),
4049 optarg, false);
4050 if (!opts) {
4051 exit(1);
4053 #else
4054 error_report("File descriptor passing is disabled on this "
4055 "platform");
4056 exit(1);
4057 #endif
4058 break;
4059 case QEMU_OPTION_object:
4060 opts = qemu_opts_parse_noisily(qemu_find_opts("object"),
4061 optarg, true);
4062 if (!opts) {
4063 exit(1);
4065 break;
4066 case QEMU_OPTION_realtime:
4067 opts = qemu_opts_parse_noisily(qemu_find_opts("realtime"),
4068 optarg, false);
4069 if (!opts) {
4070 exit(1);
4072 enable_mlock = qemu_opt_get_bool(opts, "mlock", true);
4073 break;
4074 case QEMU_OPTION_msg:
4075 opts = qemu_opts_parse_noisily(qemu_find_opts("msg"), optarg,
4076 false);
4077 if (!opts) {
4078 exit(1);
4080 configure_msg(opts);
4081 break;
4082 case QEMU_OPTION_dump_vmstate:
4083 if (vmstate_dump_file) {
4084 error_report("only one '-dump-vmstate' "
4085 "option may be given");
4086 exit(1);
4088 vmstate_dump_file = fopen(optarg, "w");
4089 if (vmstate_dump_file == NULL) {
4090 error_report("open %s: %s", optarg, strerror(errno));
4091 exit(1);
4093 break;
4094 default:
4095 os_parse_cmd_args(popt->index, optarg);
4100 * Clear error location left behind by the loop.
4101 * Best done right after the loop. Do not insert code here!
4103 loc_set_none();
4105 replay_configure(icount_opts);
4107 machine_class = select_machine();
4109 set_memory_options(&ram_slots, &maxram_size, machine_class);
4111 os_daemonize();
4113 if (pid_file && qemu_create_pidfile(pid_file) != 0) {
4114 error_report("could not acquire pid file: %s", strerror(errno));
4115 exit(1);
4118 if (qemu_init_main_loop(&main_loop_err)) {
4119 error_report_err(main_loop_err);
4120 exit(1);
4123 if (qemu_opts_foreach(qemu_find_opts("sandbox"),
4124 parse_sandbox, NULL, NULL)) {
4125 exit(1);
4128 if (qemu_opts_foreach(qemu_find_opts("name"),
4129 parse_name, NULL, NULL)) {
4130 exit(1);
4133 #ifndef _WIN32
4134 if (qemu_opts_foreach(qemu_find_opts("add-fd"),
4135 parse_add_fd, NULL, NULL)) {
4136 exit(1);
4139 if (qemu_opts_foreach(qemu_find_opts("add-fd"),
4140 cleanup_add_fd, NULL, NULL)) {
4141 exit(1);
4143 #endif
4145 current_machine = MACHINE(object_new(object_class_get_name(
4146 OBJECT_CLASS(machine_class))));
4147 if (machine_help_func(qemu_get_machine_opts(), current_machine)) {
4148 exit(0);
4150 object_property_add_child(object_get_root(), "machine",
4151 OBJECT(current_machine), &error_abort);
4153 if (machine_class->minimum_page_bits) {
4154 if (!set_preferred_target_page_bits(machine_class->minimum_page_bits)) {
4155 /* This would be a board error: specifying a minimum smaller than
4156 * a target's compile-time fixed setting.
4158 g_assert_not_reached();
4162 cpu_exec_init_all();
4164 if (machine_class->hw_version) {
4165 qemu_set_hw_version(machine_class->hw_version);
4168 if (cpu_model && is_help_option(cpu_model)) {
4169 list_cpus(stdout, &fprintf, cpu_model);
4170 exit(0);
4173 if (!trace_init_backends()) {
4174 exit(1);
4176 trace_init_file(trace_file);
4178 /* Open the logfile at this point and set the log mask if necessary.
4180 if (log_file) {
4181 qemu_set_log_filename(log_file, &error_fatal);
4184 if (log_mask) {
4185 int mask;
4186 mask = qemu_str_to_log_mask(log_mask);
4187 if (!mask) {
4188 qemu_print_log_usage(stdout);
4189 exit(1);
4191 qemu_set_log(mask);
4192 } else {
4193 qemu_set_log(0);
4196 /* If no data_dir is specified then try to find it relative to the
4197 executable path. */
4198 if (data_dir_idx < ARRAY_SIZE(data_dir)) {
4199 data_dir[data_dir_idx] = os_find_datadir();
4200 if (data_dir[data_dir_idx] != NULL) {
4201 data_dir_idx++;
4204 /* If all else fails use the install path specified when building. */
4205 if (data_dir_idx < ARRAY_SIZE(data_dir)) {
4206 data_dir[data_dir_idx++] = CONFIG_QEMU_DATADIR;
4209 /* -L help lists the data directories and exits. */
4210 if (list_data_dirs) {
4211 for (i = 0; i < data_dir_idx; i++) {
4212 printf("%s\n", data_dir[i]);
4214 exit(0);
4217 smp_parse(qemu_opts_find(qemu_find_opts("smp-opts"), NULL));
4219 machine_class->max_cpus = machine_class->max_cpus ?: 1; /* Default to UP */
4220 if (max_cpus > machine_class->max_cpus) {
4221 error_report("Number of SMP CPUs requested (%d) exceeds max CPUs "
4222 "supported by machine '%s' (%d)", max_cpus,
4223 machine_class->name, machine_class->max_cpus);
4224 exit(1);
4228 * Get the default machine options from the machine if it is not already
4229 * specified either by the configuration file or by the command line.
4231 if (machine_class->default_machine_opts) {
4232 qemu_opts_set_defaults(qemu_find_opts("machine"),
4233 machine_class->default_machine_opts, 0);
4236 qemu_opts_foreach(qemu_find_opts("device"),
4237 default_driver_check, NULL, NULL);
4238 qemu_opts_foreach(qemu_find_opts("global"),
4239 default_driver_check, NULL, NULL);
4241 if (!vga_model && !default_vga) {
4242 vga_interface_type = VGA_DEVICE;
4244 if (!has_defaults || machine_class->no_serial) {
4245 default_serial = 0;
4247 if (!has_defaults || machine_class->no_parallel) {
4248 default_parallel = 0;
4250 if (!has_defaults || !machine_class->use_virtcon) {
4251 default_virtcon = 0;
4253 if (!has_defaults || !machine_class->use_sclp) {
4254 default_sclp = 0;
4256 if (!has_defaults || machine_class->no_floppy) {
4257 default_floppy = 0;
4259 if (!has_defaults || machine_class->no_cdrom) {
4260 default_cdrom = 0;
4262 if (!has_defaults || machine_class->no_sdcard) {
4263 default_sdcard = 0;
4265 if (!has_defaults) {
4266 default_monitor = 0;
4267 default_net = 0;
4268 default_vga = 0;
4271 if (is_daemonized()) {
4272 /* According to documentation and historically, -nographic redirects
4273 * serial port, parallel port and monitor to stdio, which does not work
4274 * with -daemonize. We can redirect these to null instead, but since
4275 * -nographic is legacy, let's just error out.
4276 * We disallow -nographic only if all other ports are not redirected
4277 * explicitly, to not break existing legacy setups which uses
4278 * -nographic _and_ redirects all ports explicitly - this is valid
4279 * usage, -nographic is just a no-op in this case.
4281 if (nographic
4282 && (default_parallel || default_serial
4283 || default_monitor || default_virtcon)) {
4284 error_report("-nographic cannot be used with -daemonize");
4285 exit(1);
4287 #ifdef CONFIG_CURSES
4288 if (display_type == DT_CURSES) {
4289 error_report("curses display cannot be used with -daemonize");
4290 exit(1);
4292 #endif
4295 if (nographic) {
4296 if (default_parallel)
4297 add_device_config(DEV_PARALLEL, "null");
4298 if (default_serial && default_monitor) {
4299 add_device_config(DEV_SERIAL, "mon:stdio");
4300 } else if (default_virtcon && default_monitor) {
4301 add_device_config(DEV_VIRTCON, "mon:stdio");
4302 } else if (default_sclp && default_monitor) {
4303 add_device_config(DEV_SCLP, "mon:stdio");
4304 } else {
4305 if (default_serial)
4306 add_device_config(DEV_SERIAL, "stdio");
4307 if (default_virtcon)
4308 add_device_config(DEV_VIRTCON, "stdio");
4309 if (default_sclp) {
4310 add_device_config(DEV_SCLP, "stdio");
4312 if (default_monitor)
4313 monitor_parse("stdio", "readline", false);
4315 } else {
4316 if (default_serial)
4317 add_device_config(DEV_SERIAL, "vc:80Cx24C");
4318 if (default_parallel)
4319 add_device_config(DEV_PARALLEL, "vc:80Cx24C");
4320 if (default_monitor)
4321 monitor_parse("vc:80Cx24C", "readline", false);
4322 if (default_virtcon)
4323 add_device_config(DEV_VIRTCON, "vc:80Cx24C");
4324 if (default_sclp) {
4325 add_device_config(DEV_SCLP, "vc:80Cx24C");
4329 #if defined(CONFIG_VNC)
4330 if (!QTAILQ_EMPTY(&(qemu_find_opts("vnc")->head))) {
4331 display_remote++;
4333 #endif
4334 if (display_type == DT_DEFAULT && !display_remote) {
4335 #if defined(CONFIG_GTK)
4336 display_type = DT_GTK;
4337 #elif defined(CONFIG_SDL)
4338 display_type = DT_SDL;
4339 #elif defined(CONFIG_COCOA)
4340 display_type = DT_COCOA;
4341 #elif defined(CONFIG_VNC)
4342 vnc_parse("localhost:0,to=99,id=default", &error_abort);
4343 #else
4344 display_type = DT_NONE;
4345 #endif
4348 if ((no_frame || alt_grab || ctrl_grab) && display_type != DT_SDL) {
4349 error_report("-no-frame, -alt-grab and -ctrl-grab are only valid "
4350 "for SDL, ignoring option");
4352 if (no_quit && (display_type != DT_GTK && display_type != DT_SDL)) {
4353 error_report("-no-quit is only valid for GTK and SDL, "
4354 "ignoring option");
4357 if (display_type == DT_GTK) {
4358 early_gtk_display_init(request_opengl);
4361 if (display_type == DT_SDL) {
4362 sdl_display_early_init(request_opengl);
4365 if (request_opengl == 1 && display_opengl == 0) {
4366 #if defined(CONFIG_OPENGL)
4367 error_report("OpenGL is not supported by the display");
4368 #else
4369 error_report("OpenGL support is disabled");
4370 #endif
4371 exit(1);
4374 page_size_init();
4375 socket_init();
4377 if (qemu_opts_foreach(qemu_find_opts("object"),
4378 user_creatable_add_opts_foreach,
4379 object_create_initial, NULL)) {
4380 exit(1);
4383 if (qemu_opts_foreach(qemu_find_opts("chardev"),
4384 chardev_init_func, NULL, NULL)) {
4385 exit(1);
4388 #ifdef CONFIG_VIRTFS
4389 if (qemu_opts_foreach(qemu_find_opts("fsdev"),
4390 fsdev_init_func, NULL, NULL)) {
4391 exit(1);
4393 #endif
4395 if (qemu_opts_foreach(qemu_find_opts("device"),
4396 device_help_func, NULL, NULL)) {
4397 exit(0);
4400 machine_opts = qemu_get_machine_opts();
4401 if (qemu_opt_foreach(machine_opts, machine_set_property, current_machine,
4402 NULL)) {
4403 object_unref(OBJECT(current_machine));
4404 exit(1);
4407 configure_accelerator(current_machine);
4409 if (qtest_chrdev) {
4410 qtest_init(qtest_chrdev, qtest_log, &error_fatal);
4413 machine_opts = qemu_get_machine_opts();
4414 kernel_filename = qemu_opt_get(machine_opts, "kernel");
4415 initrd_filename = qemu_opt_get(machine_opts, "initrd");
4416 kernel_cmdline = qemu_opt_get(machine_opts, "append");
4417 bios_name = qemu_opt_get(machine_opts, "firmware");
4419 opts = qemu_opts_find(qemu_find_opts("boot-opts"), NULL);
4420 if (opts) {
4421 boot_order = qemu_opt_get(opts, "order");
4422 if (boot_order) {
4423 validate_bootdevices(boot_order, &error_fatal);
4426 boot_once = qemu_opt_get(opts, "once");
4427 if (boot_once) {
4428 validate_bootdevices(boot_once, &error_fatal);
4431 boot_menu = qemu_opt_get_bool(opts, "menu", boot_menu);
4432 boot_strict = qemu_opt_get_bool(opts, "strict", false);
4435 if (!boot_order) {
4436 boot_order = machine_class->default_boot_order;
4439 if (!kernel_cmdline) {
4440 kernel_cmdline = "";
4441 current_machine->kernel_cmdline = (char *)kernel_cmdline;
4444 linux_boot = (kernel_filename != NULL);
4446 if (!linux_boot && *kernel_cmdline != '\0') {
4447 error_report("-append only allowed with -kernel option");
4448 exit(1);
4451 if (!linux_boot && initrd_filename != NULL) {
4452 error_report("-initrd only allowed with -kernel option");
4453 exit(1);
4456 if (semihosting_enabled() && !semihosting_get_argc() && kernel_filename) {
4457 /* fall back to the -kernel/-append */
4458 semihosting_arg_fallback(kernel_filename, kernel_cmdline);
4461 os_set_line_buffering();
4463 /* spice needs the timers to be initialized by this point */
4464 qemu_spice_init();
4466 cpu_ticks_init();
4467 if (icount_opts) {
4468 if (!tcg_enabled()) {
4469 error_report("-icount is not allowed with hardware virtualization");
4470 exit(1);
4472 configure_icount(icount_opts, &error_abort);
4473 qemu_opts_del(icount_opts);
4476 if (default_net) {
4477 QemuOptsList *net = qemu_find_opts("net");
4478 qemu_opts_set(net, NULL, "type", "nic", &error_abort);
4479 #ifdef CONFIG_SLIRP
4480 qemu_opts_set(net, NULL, "type", "user", &error_abort);
4481 #endif
4484 colo_info_init();
4486 if (net_init_clients() < 0) {
4487 exit(1);
4490 if (qemu_opts_foreach(qemu_find_opts("object"),
4491 user_creatable_add_opts_foreach,
4492 object_create_delayed, NULL)) {
4493 exit(1);
4496 #ifdef CONFIG_TPM
4497 if (tpm_init() < 0) {
4498 exit(1);
4500 #endif
4502 /* init the bluetooth world */
4503 if (foreach_device_config(DEV_BT, bt_parse))
4504 exit(1);
4506 if (!xen_enabled()) {
4507 /* On 32-bit hosts, QEMU is limited by virtual address space */
4508 if (ram_size > (2047 << 20) && HOST_LONG_BITS == 32) {
4509 error_report("at most 2047 MB RAM can be simulated");
4510 exit(1);
4514 blk_mig_init();
4515 ram_mig_init();
4517 /* If the currently selected machine wishes to override the units-per-bus
4518 * property of its default HBA interface type, do so now. */
4519 if (machine_class->units_per_default_bus) {
4520 override_max_devs(machine_class->block_default_type,
4521 machine_class->units_per_default_bus);
4524 /* open the virtual block devices */
4525 if (snapshot || replay_mode != REPLAY_MODE_NONE) {
4526 qemu_opts_foreach(qemu_find_opts("drive"), drive_enable_snapshot,
4527 NULL, NULL);
4529 if (qemu_opts_foreach(qemu_find_opts("drive"), drive_init_func,
4530 &machine_class->block_default_type, NULL)) {
4531 exit(1);
4534 default_drive(default_cdrom, snapshot, machine_class->block_default_type, 2,
4535 CDROM_OPTS);
4536 default_drive(default_floppy, snapshot, IF_FLOPPY, 0, FD_OPTS);
4537 default_drive(default_sdcard, snapshot, IF_SD, 0, SD_OPTS);
4539 parse_numa_opts(machine_class);
4541 if (qemu_opts_foreach(qemu_find_opts("mon"),
4542 mon_init_func, NULL, NULL)) {
4543 exit(1);
4546 if (foreach_device_config(DEV_SERIAL, serial_parse) < 0)
4547 exit(1);
4548 if (foreach_device_config(DEV_PARALLEL, parallel_parse) < 0)
4549 exit(1);
4550 if (foreach_device_config(DEV_VIRTCON, virtcon_parse) < 0)
4551 exit(1);
4552 if (foreach_device_config(DEV_SCLP, sclp_parse) < 0) {
4553 exit(1);
4555 if (foreach_device_config(DEV_DEBUGCON, debugcon_parse) < 0)
4556 exit(1);
4558 /* If no default VGA is requested, the default is "none". */
4559 if (default_vga) {
4560 if (machine_class->default_display) {
4561 vga_model = machine_class->default_display;
4562 } else if (vga_interface_available(VGA_CIRRUS)) {
4563 vga_model = "cirrus";
4564 } else if (vga_interface_available(VGA_STD)) {
4565 vga_model = "std";
4568 if (vga_model) {
4569 select_vgahw(vga_model);
4572 if (watchdog) {
4573 i = select_watchdog(watchdog);
4574 if (i > 0)
4575 exit (i == 1 ? 1 : 0);
4578 machine_register_compat_props(current_machine);
4580 qemu_opts_foreach(qemu_find_opts("global"),
4581 global_init_func, NULL, NULL);
4583 /* This checkpoint is required by replay to separate prior clock
4584 reading from the other reads, because timer polling functions query
4585 clock values from the log. */
4586 replay_checkpoint(CHECKPOINT_INIT);
4587 qdev_machine_init();
4589 current_machine->ram_size = ram_size;
4590 current_machine->maxram_size = maxram_size;
4591 current_machine->ram_slots = ram_slots;
4592 current_machine->boot_order = boot_order;
4593 current_machine->cpu_model = cpu_model;
4595 machine_class->init(current_machine);
4597 realtime_init();
4599 audio_init();
4601 cpu_synchronize_all_post_init();
4603 numa_post_machine_init();
4605 if (hax_enabled()) {
4606 hax_sync_vcpus();
4609 if (qemu_opts_foreach(qemu_find_opts("fw_cfg"),
4610 parse_fw_cfg, fw_cfg_find(), NULL) != 0) {
4611 exit(1);
4614 /* init USB devices */
4615 if (machine_usb(current_machine)) {
4616 if (foreach_device_config(DEV_USB, usb_parse) < 0)
4617 exit(1);
4620 /* Check if IGD GFX passthrough. */
4621 igd_gfx_passthru();
4623 /* init generic devices */
4624 rom_set_order_override(FW_CFG_ORDER_OVERRIDE_DEVICE);
4625 if (qemu_opts_foreach(qemu_find_opts("device"),
4626 device_init_func, NULL, NULL)) {
4627 exit(1);
4629 rom_reset_order_override();
4631 /* Did we create any drives that we failed to create a device for? */
4632 drive_check_orphaned();
4634 /* Don't warn about the default network setup that you get if
4635 * no command line -net or -netdev options are specified. There
4636 * are two cases that we would otherwise complain about:
4637 * (1) board doesn't support a NIC but the implicit "-net nic"
4638 * requested one
4639 * (2) CONFIG_SLIRP not set, in which case the implicit "-net nic"
4640 * sets up a nic that isn't connected to anything.
4642 if (!default_net) {
4643 net_check_clients();
4647 if (boot_once) {
4648 qemu_boot_set(boot_once, &error_fatal);
4649 qemu_register_reset(restore_boot_order, g_strdup(boot_order));
4652 ds = init_displaystate();
4654 /* init local displays */
4655 switch (display_type) {
4656 case DT_CURSES:
4657 curses_display_init(ds, full_screen);
4658 break;
4659 case DT_SDL:
4660 sdl_display_init(ds, full_screen, no_frame);
4661 break;
4662 case DT_COCOA:
4663 cocoa_display_init(ds, full_screen);
4664 break;
4665 case DT_GTK:
4666 gtk_display_init(ds, full_screen, grab_on_hover);
4667 break;
4668 default:
4669 break;
4672 /* must be after terminal init, SDL library changes signal handlers */
4673 os_setup_signal_handling();
4675 /* init remote displays */
4676 #ifdef CONFIG_VNC
4677 qemu_opts_foreach(qemu_find_opts("vnc"),
4678 vnc_init_func, NULL, NULL);
4679 #endif
4681 if (using_spice) {
4682 qemu_spice_display_init();
4685 if (foreach_device_config(DEV_GDB, gdbserver_start) < 0) {
4686 exit(1);
4689 qdev_machine_creation_done();
4691 /* TODO: once all bus devices are qdevified, this should be done
4692 * when bus is created by qdev.c */
4693 qemu_register_reset(qbus_reset_all_fn, sysbus_get_default());
4694 qemu_run_machine_init_done_notifiers();
4696 if (rom_check_and_register_reset() != 0) {
4697 error_report("rom check and register reset failed");
4698 exit(1);
4701 replay_start();
4703 /* This checkpoint is required by replay to separate prior clock
4704 reading from the other reads, because timer polling functions query
4705 clock values from the log. */
4706 replay_checkpoint(CHECKPOINT_RESET);
4707 qemu_system_reset(VMRESET_SILENT);
4708 register_global_state();
4709 if (loadvm) {
4710 if (load_vmstate(loadvm) < 0) {
4711 autostart = 0;
4715 qdev_prop_check_globals();
4716 if (vmstate_dump_file) {
4717 /* dump and exit */
4718 dump_vmstate_json_to_file(vmstate_dump_file);
4719 return 0;
4722 if (incoming) {
4723 Error *local_err = NULL;
4724 qemu_start_incoming_migration(incoming, &local_err);
4725 if (local_err) {
4726 error_reportf_err(local_err, "-incoming %s: ", incoming);
4727 exit(1);
4729 } else if (autostart) {
4730 vm_start();
4733 os_setup_post();
4735 main_loop();
4736 replay_disable_events();
4737 iothread_stop_all();
4739 bdrv_close_all();
4740 pause_all_vcpus();
4741 res_free();
4743 /* vhost-user must be cleaned up before chardevs. */
4744 net_cleanup();
4745 audio_cleanup();
4746 monitor_cleanup();
4747 qemu_chr_cleanup();
4749 return 0;