Add missing GCC_FMT_ATTR
[qemu/ar7.git] / vl.c
blob36957b6cb96acc5ba8346c1e4c1b8bc7e09dd9c9
1 /*
2 * QEMU System Emulator
4 * Copyright (c) 2003-2008 Fabrice Bellard
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 * THE SOFTWARE.
24 #include "qemu/osdep.h"
25 #include "qemu-version.h"
26 #include "qemu/cutils.h"
27 #include "qemu/help_option.h"
28 #include "qemu/uuid.h"
30 #ifdef CONFIG_SECCOMP
31 #include "sysemu/seccomp.h"
32 #endif
34 #if defined(CONFIG_VDE)
35 #include <libvdeplug.h>
36 #endif
38 #ifdef CONFIG_SDL
39 #if defined(__APPLE__) || defined(main)
40 #define SDL_MAIN_HANDLED
41 #include <SDL.h>
42 #if SDL_MAJOR_VERSION == 1
43 int qemu_main(int argc, char **argv);
44 int main(int argc, char **argv)
46 return qemu_main(argc, argv);
48 #undef main
49 #define main qemu_main
50 #elif SDL_MAJOR_VERSION == 2
51 #undef main
52 #else
53 #error undefined SDL version
54 #endif
55 #endif
56 #endif /* CONFIG_SDL */
58 #ifdef CONFIG_COCOA
59 #undef main
60 #define main qemu_main
61 #endif /* CONFIG_COCOA */
64 #include "qemu/error-report.h"
65 #include "qemu/sockets.h"
66 #include "hw/hw.h"
67 #include "hw/boards.h"
68 #include "sysemu/accel.h"
69 #include "hw/usb.h"
70 #include "hw/i386/pc.h"
71 #include "hw/isa/isa.h"
72 #include "hw/scsi/scsi.h"
73 #include "hw/bt.h"
74 #include "sysemu/watchdog.h"
75 #include "hw/smbios/smbios.h"
76 #include "hw/acpi/acpi.h"
77 #include "hw/xen/xen.h"
78 #include "hw/qdev.h"
79 #include "hw/loader.h"
80 #include "monitor/qdev.h"
81 #include "sysemu/bt.h"
82 #include "net/net.h"
83 #include "net/slirp.h"
84 #include "monitor/monitor.h"
85 #include "ui/console.h"
86 #include "ui/input.h"
87 #include "sysemu/sysemu.h"
88 #include "sysemu/numa.h"
89 #include "exec/gdbstub.h"
90 #include "qemu/timer.h"
91 #include "sysemu/char.h"
92 #include "qemu/bitmap.h"
93 #include "qemu/log.h"
94 #include "sysemu/blockdev.h"
95 #include "hw/block/block.h"
96 #include "migration/block.h"
97 #include "sysemu/tpm.h"
98 #include "sysemu/dma.h"
99 #include "audio/audio.h"
100 #include "migration/migration.h"
101 #include "sysemu/cpus.h"
102 #include "migration/colo.h"
103 #include "sysemu/kvm.h"
104 #include "sysemu/hax.h"
105 #include "qapi/qobject-input-visitor.h"
106 #include "qapi/qobject-input-visitor.h"
107 #include "qapi-visit.h"
108 #include "qapi/qmp/qjson.h"
109 #include "qemu/option.h"
110 #include "qemu/config-file.h"
111 #include "qemu-options.h"
112 #include "qmp-commands.h"
113 #include "qemu/main-loop.h"
114 #ifdef CONFIG_VIRTFS
115 #include "fsdev/qemu-fsdev.h"
116 #endif
117 #include "sysemu/qtest.h"
119 #include "disas/disas.h"
122 #include "slirp/libslirp.h"
124 #include "trace-root.h"
125 #include "trace/control.h"
126 #include "qemu/queue.h"
127 #include "sysemu/arch_init.h"
129 #include "ui/qemu-spice.h"
130 #include "qapi/string-input-visitor.h"
131 #include "qapi/opts-visitor.h"
132 #include "qom/object_interfaces.h"
133 #include "qapi-event.h"
134 #include "exec/semihost.h"
135 #include "crypto/init.h"
136 #include "sysemu/replay.h"
137 #include "qapi/qmp/qerror.h"
138 #include "sysemu/iothread.h"
140 #define MAX_VIRTIO_CONSOLES 1
141 #define MAX_SCLP_CONSOLES 1
143 static const char *data_dir[16];
144 static int data_dir_idx;
145 const char *bios_name = NULL;
146 enum vga_retrace_method vga_retrace_method = VGA_RETRACE_DUMB;
147 int request_opengl = -1;
148 int display_opengl;
149 const char* keyboard_layout = NULL;
150 ram_addr_t ram_size;
151 const char *mem_path = NULL;
152 int mem_prealloc = 0; /* force preallocation of physical target memory */
153 bool enable_mlock = false;
154 int nb_nics;
155 NICInfo nd_table[MAX_NICS];
156 int autostart;
157 static int rtc_utc = 1;
158 static int rtc_date_offset = -1; /* -1 means no change */
159 QEMUClockType rtc_clock;
160 int vga_interface_type = VGA_NONE;
161 static int full_screen = 0;
162 static int no_frame = 0;
163 int no_quit = 0;
164 static bool grab_on_hover;
165 Chardev *serial_hds[MAX_SERIAL_PORTS];
166 Chardev *parallel_hds[MAX_PARALLEL_PORTS];
167 Chardev *virtcon_hds[MAX_VIRTIO_CONSOLES];
168 Chardev *sclp_hds[MAX_SCLP_CONSOLES];
169 int win2k_install_hack = 0;
170 int singlestep = 0;
171 int smp_cpus = 1;
172 int max_cpus = 1;
173 int smp_cores = 1;
174 int smp_threads = 1;
175 int acpi_enabled = 1;
176 int no_hpet = 0;
177 int fd_bootchk = 1;
178 static int no_reboot;
179 int no_shutdown = 0;
180 int cursor_hide = 1;
181 int graphic_rotate = 0;
182 const char *watchdog;
183 QEMUOptionRom option_rom[MAX_OPTION_ROMS];
184 int nb_option_roms;
185 int old_param = 0;
186 const char *qemu_name;
187 int alt_grab = 0;
188 int ctrl_grab = 0;
189 unsigned int nb_prom_envs = 0;
190 const char *prom_envs[MAX_PROM_ENVS];
191 int boot_menu;
192 bool boot_strict;
193 uint8_t *boot_splash_filedata;
194 size_t boot_splash_filedata_size;
195 uint8_t qemu_extra_params_fw[2];
196 int only_migratable; /* turn it off unless user states otherwise */
198 int icount_align_option;
200 /* The bytes in qemu_uuid are in the order specified by RFC4122, _not_ in the
201 * little-endian "wire format" described in the SMBIOS 2.6 specification.
203 QemuUUID qemu_uuid;
204 bool qemu_uuid_set;
206 /* Trace unassigned memory or i/o accesses. */
207 bool trace_unassigned;
209 static NotifierList exit_notifiers =
210 NOTIFIER_LIST_INITIALIZER(exit_notifiers);
212 static NotifierList machine_init_done_notifiers =
213 NOTIFIER_LIST_INITIALIZER(machine_init_done_notifiers);
215 bool xen_allowed;
216 uint32_t xen_domid;
217 enum xen_mode xen_mode = XEN_EMULATE;
219 static int has_defaults = 1;
220 static int default_serial = 1;
221 static int default_parallel = 1;
222 static int default_virtcon = 1;
223 static int default_sclp = 1;
224 static int default_monitor = 1;
225 static int default_floppy = 1;
226 static int default_cdrom = 1;
227 static int default_sdcard = 1;
228 static int default_vga = 1;
229 static int default_net = 1;
231 static struct {
232 const char *driver;
233 int *flag;
234 } default_list[] = {
235 { .driver = "isa-serial", .flag = &default_serial },
236 { .driver = "isa-parallel", .flag = &default_parallel },
237 { .driver = "isa-fdc", .flag = &default_floppy },
238 { .driver = "floppy", .flag = &default_floppy },
239 { .driver = "ide-cd", .flag = &default_cdrom },
240 { .driver = "ide-hd", .flag = &default_cdrom },
241 { .driver = "ide-drive", .flag = &default_cdrom },
242 { .driver = "scsi-cd", .flag = &default_cdrom },
243 { .driver = "scsi-hd", .flag = &default_cdrom },
244 { .driver = "virtio-serial-pci", .flag = &default_virtcon },
245 { .driver = "virtio-serial", .flag = &default_virtcon },
246 { .driver = "VGA", .flag = &default_vga },
247 { .driver = "isa-vga", .flag = &default_vga },
248 { .driver = "cirrus-vga", .flag = &default_vga },
249 { .driver = "isa-cirrus-vga", .flag = &default_vga },
250 { .driver = "vmware-svga", .flag = &default_vga },
251 { .driver = "qxl-vga", .flag = &default_vga },
252 { .driver = "virtio-vga", .flag = &default_vga },
255 static QemuOptsList qemu_rtc_opts = {
256 .name = "rtc",
257 .head = QTAILQ_HEAD_INITIALIZER(qemu_rtc_opts.head),
258 .desc = {
260 .name = "base",
261 .type = QEMU_OPT_STRING,
263 .name = "clock",
264 .type = QEMU_OPT_STRING,
266 .name = "driftfix",
267 .type = QEMU_OPT_STRING,
269 { /* end of list */ }
273 static QemuOptsList qemu_sandbox_opts = {
274 .name = "sandbox",
275 .implied_opt_name = "enable",
276 .head = QTAILQ_HEAD_INITIALIZER(qemu_sandbox_opts.head),
277 .desc = {
279 .name = "enable",
280 .type = QEMU_OPT_BOOL,
282 { /* end of list */ }
286 static QemuOptsList qemu_option_rom_opts = {
287 .name = "option-rom",
288 .implied_opt_name = "romfile",
289 .head = QTAILQ_HEAD_INITIALIZER(qemu_option_rom_opts.head),
290 .desc = {
292 .name = "bootindex",
293 .type = QEMU_OPT_NUMBER,
294 }, {
295 .name = "romfile",
296 .type = QEMU_OPT_STRING,
298 { /* end of list */ }
302 static QemuOptsList qemu_machine_opts = {
303 .name = "machine",
304 .implied_opt_name = "type",
305 .merge_lists = true,
306 .head = QTAILQ_HEAD_INITIALIZER(qemu_machine_opts.head),
307 .desc = {
309 * no elements => accept any
310 * sanity checking will happen later
311 * when setting machine properties
317 static QemuOptsList qemu_accel_opts = {
318 .name = "accel",
319 .implied_opt_name = "accel",
320 .head = QTAILQ_HEAD_INITIALIZER(qemu_accel_opts.head),
321 .merge_lists = true,
322 .desc = {
324 .name = "accel",
325 .type = QEMU_OPT_STRING,
326 .help = "Select the type of accelerator",
329 .name = "thread",
330 .type = QEMU_OPT_STRING,
331 .help = "Enable/disable multi-threaded TCG",
333 { /* end of list */ }
337 static QemuOptsList qemu_boot_opts = {
338 .name = "boot-opts",
339 .implied_opt_name = "order",
340 .merge_lists = true,
341 .head = QTAILQ_HEAD_INITIALIZER(qemu_boot_opts.head),
342 .desc = {
344 .name = "order",
345 .type = QEMU_OPT_STRING,
346 }, {
347 .name = "once",
348 .type = QEMU_OPT_STRING,
349 }, {
350 .name = "menu",
351 .type = QEMU_OPT_BOOL,
352 }, {
353 .name = "splash",
354 .type = QEMU_OPT_STRING,
355 }, {
356 .name = "splash-time",
357 .type = QEMU_OPT_STRING,
358 }, {
359 .name = "reboot-timeout",
360 .type = QEMU_OPT_STRING,
361 }, {
362 .name = "strict",
363 .type = QEMU_OPT_BOOL,
365 { /*End of list */ }
369 static QemuOptsList qemu_add_fd_opts = {
370 .name = "add-fd",
371 .head = QTAILQ_HEAD_INITIALIZER(qemu_add_fd_opts.head),
372 .desc = {
374 .name = "fd",
375 .type = QEMU_OPT_NUMBER,
376 .help = "file descriptor of which a duplicate is added to fd set",
378 .name = "set",
379 .type = QEMU_OPT_NUMBER,
380 .help = "ID of the fd set to add fd to",
382 .name = "opaque",
383 .type = QEMU_OPT_STRING,
384 .help = "free-form string used to describe fd",
386 { /* end of list */ }
390 static QemuOptsList qemu_object_opts = {
391 .name = "object",
392 .implied_opt_name = "qom-type",
393 .head = QTAILQ_HEAD_INITIALIZER(qemu_object_opts.head),
394 .desc = {
399 static QemuOptsList qemu_tpmdev_opts = {
400 .name = "tpmdev",
401 .implied_opt_name = "type",
402 .head = QTAILQ_HEAD_INITIALIZER(qemu_tpmdev_opts.head),
403 .desc = {
404 /* options are defined in the TPM backends */
405 { /* end of list */ }
409 static QemuOptsList qemu_realtime_opts = {
410 .name = "realtime",
411 .head = QTAILQ_HEAD_INITIALIZER(qemu_realtime_opts.head),
412 .desc = {
414 .name = "mlock",
415 .type = QEMU_OPT_BOOL,
417 { /* end of list */ }
421 static QemuOptsList qemu_msg_opts = {
422 .name = "msg",
423 .head = QTAILQ_HEAD_INITIALIZER(qemu_msg_opts.head),
424 .desc = {
426 .name = "timestamp",
427 .type = QEMU_OPT_BOOL,
429 { /* end of list */ }
433 static QemuOptsList qemu_name_opts = {
434 .name = "name",
435 .implied_opt_name = "guest",
436 .merge_lists = true,
437 .head = QTAILQ_HEAD_INITIALIZER(qemu_name_opts.head),
438 .desc = {
440 .name = "guest",
441 .type = QEMU_OPT_STRING,
442 .help = "Sets the name of the guest.\n"
443 "This name will be displayed in the SDL window caption.\n"
444 "The name will also be used for the VNC server",
445 }, {
446 .name = "process",
447 .type = QEMU_OPT_STRING,
448 .help = "Sets the name of the QEMU process, as shown in top etc",
449 }, {
450 .name = "debug-threads",
451 .type = QEMU_OPT_BOOL,
452 .help = "When enabled, name the individual threads; defaults off.\n"
453 "NOTE: The thread names are for debugging and not a\n"
454 "stable API.",
456 { /* End of list */ }
460 static QemuOptsList qemu_mem_opts = {
461 .name = "memory",
462 .implied_opt_name = "size",
463 .head = QTAILQ_HEAD_INITIALIZER(qemu_mem_opts.head),
464 .merge_lists = true,
465 .desc = {
467 .name = "size",
468 .type = QEMU_OPT_SIZE,
471 .name = "slots",
472 .type = QEMU_OPT_NUMBER,
475 .name = "maxmem",
476 .type = QEMU_OPT_SIZE,
478 { /* end of list */ }
482 static QemuOptsList qemu_icount_opts = {
483 .name = "icount",
484 .implied_opt_name = "shift",
485 .merge_lists = true,
486 .head = QTAILQ_HEAD_INITIALIZER(qemu_icount_opts.head),
487 .desc = {
489 .name = "shift",
490 .type = QEMU_OPT_STRING,
491 }, {
492 .name = "align",
493 .type = QEMU_OPT_BOOL,
494 }, {
495 .name = "sleep",
496 .type = QEMU_OPT_BOOL,
497 }, {
498 .name = "rr",
499 .type = QEMU_OPT_STRING,
500 }, {
501 .name = "rrfile",
502 .type = QEMU_OPT_STRING,
503 }, {
504 .name = "rrsnapshot",
505 .type = QEMU_OPT_STRING,
507 { /* end of list */ }
511 static QemuOptsList qemu_semihosting_config_opts = {
512 .name = "semihosting-config",
513 .implied_opt_name = "enable",
514 .head = QTAILQ_HEAD_INITIALIZER(qemu_semihosting_config_opts.head),
515 .desc = {
517 .name = "enable",
518 .type = QEMU_OPT_BOOL,
519 }, {
520 .name = "target",
521 .type = QEMU_OPT_STRING,
522 }, {
523 .name = "arg",
524 .type = QEMU_OPT_STRING,
526 { /* end of list */ }
530 static QemuOptsList qemu_fw_cfg_opts = {
531 .name = "fw_cfg",
532 .implied_opt_name = "name",
533 .head = QTAILQ_HEAD_INITIALIZER(qemu_fw_cfg_opts.head),
534 .desc = {
536 .name = "name",
537 .type = QEMU_OPT_STRING,
538 .help = "Sets the fw_cfg name of the blob to be inserted",
539 }, {
540 .name = "file",
541 .type = QEMU_OPT_STRING,
542 .help = "Sets the name of the file from which\n"
543 "the fw_cfg blob will be loaded",
544 }, {
545 .name = "string",
546 .type = QEMU_OPT_STRING,
547 .help = "Sets content of the blob to be inserted from a string",
549 { /* end of list */ }
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 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 /***********************************************************/
784 /* real time host monotonic timer */
786 static time_t qemu_time(void)
788 return qemu_clock_get_ms(QEMU_CLOCK_HOST) / 1000;
791 /***********************************************************/
792 /* host time/date access */
793 void qemu_get_timedate(struct tm *tm, int offset)
795 time_t ti = qemu_time();
797 ti += offset;
798 if (rtc_date_offset == -1) {
799 if (rtc_utc)
800 gmtime_r(&ti, tm);
801 else
802 localtime_r(&ti, tm);
803 } else {
804 ti -= rtc_date_offset;
805 gmtime_r(&ti, tm);
809 int qemu_timedate_diff(struct tm *tm)
811 time_t seconds;
813 if (rtc_date_offset == -1)
814 if (rtc_utc)
815 seconds = mktimegm(tm);
816 else {
817 struct tm tmp = *tm;
818 tmp.tm_isdst = -1; /* use timezone to figure it out */
819 seconds = mktime(&tmp);
821 else
822 seconds = mktimegm(tm) + rtc_date_offset;
824 return seconds - qemu_time();
827 static void configure_rtc_date_offset(const char *startdate, int legacy)
829 time_t rtc_start_date;
830 struct tm tm;
832 if (!strcmp(startdate, "now") && legacy) {
833 rtc_date_offset = -1;
834 } else {
835 if (sscanf(startdate, "%d-%d-%dT%d:%d:%d",
836 &tm.tm_year,
837 &tm.tm_mon,
838 &tm.tm_mday,
839 &tm.tm_hour,
840 &tm.tm_min,
841 &tm.tm_sec) == 6) {
842 /* OK */
843 } else if (sscanf(startdate, "%d-%d-%d",
844 &tm.tm_year,
845 &tm.tm_mon,
846 &tm.tm_mday) == 3) {
847 tm.tm_hour = 0;
848 tm.tm_min = 0;
849 tm.tm_sec = 0;
850 } else {
851 goto date_fail;
853 tm.tm_year -= 1900;
854 tm.tm_mon--;
855 rtc_start_date = mktimegm(&tm);
856 if (rtc_start_date == -1) {
857 date_fail:
858 error_report("invalid date format");
859 error_printf("valid formats: "
860 "'2006-06-17T16:01:21' or '2006-06-17'\n");
861 exit(1);
863 rtc_date_offset = qemu_time() - rtc_start_date;
867 static void configure_rtc(QemuOpts *opts)
869 const char *value;
871 value = qemu_opt_get(opts, "base");
872 if (value) {
873 if (!strcmp(value, "utc")) {
874 rtc_utc = 1;
875 } else if (!strcmp(value, "localtime")) {
876 Error *blocker = NULL;
877 rtc_utc = 0;
878 error_setg(&blocker, QERR_REPLAY_NOT_SUPPORTED,
879 "-rtc base=localtime");
880 replay_add_blocker(blocker);
881 } else {
882 configure_rtc_date_offset(value, 0);
885 value = qemu_opt_get(opts, "clock");
886 if (value) {
887 if (!strcmp(value, "host")) {
888 rtc_clock = QEMU_CLOCK_HOST;
889 } else if (!strcmp(value, "rt")) {
890 rtc_clock = QEMU_CLOCK_REALTIME;
891 } else if (!strcmp(value, "vm")) {
892 rtc_clock = QEMU_CLOCK_VIRTUAL;
893 } else {
894 error_report("invalid option value '%s'", value);
895 exit(1);
898 value = qemu_opt_get(opts, "driftfix");
899 if (value) {
900 if (!strcmp(value, "slew")) {
901 static GlobalProperty slew_lost_ticks = {
902 .driver = "mc146818rtc",
903 .property = "lost_tick_policy",
904 .value = "slew",
907 qdev_prop_register_global(&slew_lost_ticks);
908 } else if (!strcmp(value, "none")) {
909 /* discard is default */
910 } else {
911 error_report("invalid option value '%s'", value);
912 exit(1);
917 /***********************************************************/
918 /* Bluetooth support */
919 static int nb_hcis;
920 static int cur_hci;
921 static struct HCIInfo *hci_table[MAX_NICS];
923 struct HCIInfo *qemu_next_hci(void)
925 if (cur_hci == nb_hcis)
926 return &null_hci;
928 return hci_table[cur_hci++];
931 static int bt_hci_parse(const char *str)
933 struct HCIInfo *hci;
934 bdaddr_t bdaddr;
936 if (nb_hcis >= MAX_NICS) {
937 error_report("too many bluetooth HCIs (max %i)", MAX_NICS);
938 return -1;
941 hci = hci_init(str);
942 if (!hci)
943 return -1;
945 bdaddr.b[0] = 0x52;
946 bdaddr.b[1] = 0x54;
947 bdaddr.b[2] = 0x00;
948 bdaddr.b[3] = 0x12;
949 bdaddr.b[4] = 0x34;
950 bdaddr.b[5] = 0x56 + nb_hcis;
951 hci->bdaddr_set(hci, bdaddr.b);
953 hci_table[nb_hcis++] = hci;
955 return 0;
958 static void bt_vhci_add(int vlan_id)
960 struct bt_scatternet_s *vlan = qemu_find_bt_vlan(vlan_id);
962 if (!vlan->slave)
963 error_report("warning: adding a VHCI to an empty scatternet %i",
964 vlan_id);
966 bt_vhci_init(bt_new_hci(vlan));
969 static struct bt_device_s *bt_device_add(const char *opt)
971 struct bt_scatternet_s *vlan;
972 int vlan_id = 0;
973 char *endp = strstr(opt, ",vlan=");
974 int len = (endp ? endp - opt : strlen(opt)) + 1;
975 char devname[10];
977 pstrcpy(devname, MIN(sizeof(devname), len), opt);
979 if (endp) {
980 vlan_id = strtol(endp + 6, &endp, 0);
981 if (*endp) {
982 error_report("unrecognised bluetooth vlan Id");
983 return 0;
987 vlan = qemu_find_bt_vlan(vlan_id);
989 if (!vlan->slave)
990 error_report("warning: adding a slave device to an empty scatternet %i",
991 vlan_id);
993 if (!strcmp(devname, "keyboard"))
994 return bt_keyboard_init(vlan);
996 error_report("unsupported bluetooth device '%s'", devname);
997 return 0;
1000 static int bt_parse(const char *opt)
1002 const char *endp, *p;
1003 int vlan;
1005 if (strstart(opt, "hci", &endp)) {
1006 if (!*endp || *endp == ',') {
1007 if (*endp)
1008 if (!strstart(endp, ",vlan=", 0))
1009 opt = endp + 1;
1011 return bt_hci_parse(opt);
1013 } else if (strstart(opt, "vhci", &endp)) {
1014 if (!*endp || *endp == ',') {
1015 if (*endp) {
1016 if (strstart(endp, ",vlan=", &p)) {
1017 vlan = strtol(p, (char **) &endp, 0);
1018 if (*endp) {
1019 error_report("bad scatternet '%s'", p);
1020 return 1;
1022 } else {
1023 error_report("bad parameter '%s'", endp + 1);
1024 return 1;
1026 } else
1027 vlan = 0;
1029 bt_vhci_add(vlan);
1030 return 0;
1032 } else if (strstart(opt, "device:", &endp))
1033 return !bt_device_add(endp);
1035 error_report("bad bluetooth parameter '%s'", opt);
1036 return 1;
1039 static int parse_sandbox(void *opaque, QemuOpts *opts, Error **errp)
1041 /* FIXME: change this to true for 1.3 */
1042 if (qemu_opt_get_bool(opts, "enable", false)) {
1043 #ifdef CONFIG_SECCOMP
1044 if (seccomp_start() < 0) {
1045 error_report("failed to install seccomp syscall filter "
1046 "in the kernel");
1047 return -1;
1049 #else
1050 error_report("seccomp support is disabled");
1051 return -1;
1052 #endif
1055 return 0;
1058 static int parse_name(void *opaque, QemuOpts *opts, Error **errp)
1060 const char *proc_name;
1062 if (qemu_opt_get(opts, "debug-threads")) {
1063 qemu_thread_naming(qemu_opt_get_bool(opts, "debug-threads", false));
1065 qemu_name = qemu_opt_get(opts, "guest");
1067 proc_name = qemu_opt_get(opts, "process");
1068 if (proc_name) {
1069 os_set_proc_name(proc_name);
1072 return 0;
1075 bool defaults_enabled(void)
1077 return has_defaults;
1080 #ifndef _WIN32
1081 static int parse_add_fd(void *opaque, QemuOpts *opts, Error **errp)
1083 int fd, dupfd, flags;
1084 int64_t fdset_id;
1085 const char *fd_opaque = NULL;
1086 AddfdInfo *fdinfo;
1088 fd = qemu_opt_get_number(opts, "fd", -1);
1089 fdset_id = qemu_opt_get_number(opts, "set", -1);
1090 fd_opaque = qemu_opt_get(opts, "opaque");
1092 if (fd < 0) {
1093 error_report("fd option is required and must be non-negative");
1094 return -1;
1097 if (fd <= STDERR_FILENO) {
1098 error_report("fd cannot be a standard I/O stream");
1099 return -1;
1103 * All fds inherited across exec() necessarily have FD_CLOEXEC
1104 * clear, while qemu sets FD_CLOEXEC on all other fds used internally.
1106 flags = fcntl(fd, F_GETFD);
1107 if (flags == -1 || (flags & FD_CLOEXEC)) {
1108 error_report("fd is not valid or already in use");
1109 return -1;
1112 if (fdset_id < 0) {
1113 error_report("set option is required and must be non-negative");
1114 return -1;
1117 #ifdef F_DUPFD_CLOEXEC
1118 dupfd = fcntl(fd, F_DUPFD_CLOEXEC, 0);
1119 #else
1120 dupfd = dup(fd);
1121 if (dupfd != -1) {
1122 qemu_set_cloexec(dupfd);
1124 #endif
1125 if (dupfd == -1) {
1126 error_report("error duplicating fd: %s", strerror(errno));
1127 return -1;
1130 /* add the duplicate fd, and optionally the opaque string, to the fd set */
1131 fdinfo = monitor_fdset_add_fd(dupfd, true, fdset_id, !!fd_opaque, fd_opaque,
1132 &error_abort);
1133 g_free(fdinfo);
1135 return 0;
1138 static int cleanup_add_fd(void *opaque, QemuOpts *opts, Error **errp)
1140 int fd;
1142 fd = qemu_opt_get_number(opts, "fd", -1);
1143 close(fd);
1145 return 0;
1147 #endif
1149 /***********************************************************/
1150 /* QEMU Block devices */
1152 #define HD_OPTS "media=disk"
1153 #define CDROM_OPTS "media=cdrom"
1154 #define FD_OPTS ""
1155 #define PFLASH_OPTS ""
1156 #define MTD_OPTS ""
1157 #define SD_OPTS ""
1159 static int drive_init_func(void *opaque, QemuOpts *opts, Error **errp)
1161 BlockInterfaceType *block_default_type = opaque;
1163 return drive_new(opts, *block_default_type) == NULL;
1166 static int drive_enable_snapshot(void *opaque, QemuOpts *opts, Error **errp)
1168 if (qemu_opt_get(opts, "snapshot") == NULL) {
1169 qemu_opt_set(opts, "snapshot", "on", &error_abort);
1171 return 0;
1174 static void default_drive(int enable, int snapshot, BlockInterfaceType type,
1175 int index, const char *optstr)
1177 QemuOpts *opts;
1178 DriveInfo *dinfo;
1180 if (!enable || drive_get_by_index(type, index)) {
1181 return;
1184 opts = drive_add(type, index, NULL, optstr);
1185 if (snapshot) {
1186 drive_enable_snapshot(NULL, opts, NULL);
1189 dinfo = drive_new(opts, type);
1190 if (!dinfo) {
1191 exit(1);
1193 dinfo->is_default = true;
1197 static QemuOptsList qemu_smp_opts = {
1198 .name = "smp-opts",
1199 .implied_opt_name = "cpus",
1200 .merge_lists = true,
1201 .head = QTAILQ_HEAD_INITIALIZER(qemu_smp_opts.head),
1202 .desc = {
1204 .name = "cpus",
1205 .type = QEMU_OPT_NUMBER,
1206 }, {
1207 .name = "sockets",
1208 .type = QEMU_OPT_NUMBER,
1209 }, {
1210 .name = "cores",
1211 .type = QEMU_OPT_NUMBER,
1212 }, {
1213 .name = "threads",
1214 .type = QEMU_OPT_NUMBER,
1215 }, {
1216 .name = "maxcpus",
1217 .type = QEMU_OPT_NUMBER,
1219 { /*End of list */ }
1223 static void smp_parse(QemuOpts *opts)
1225 if (opts) {
1226 unsigned cpus = qemu_opt_get_number(opts, "cpus", 0);
1227 unsigned sockets = qemu_opt_get_number(opts, "sockets", 0);
1228 unsigned cores = qemu_opt_get_number(opts, "cores", 0);
1229 unsigned threads = qemu_opt_get_number(opts, "threads", 0);
1231 /* compute missing values, prefer sockets over cores over threads */
1232 if (cpus == 0 || sockets == 0) {
1233 sockets = sockets > 0 ? sockets : 1;
1234 cores = cores > 0 ? cores : 1;
1235 threads = threads > 0 ? threads : 1;
1236 if (cpus == 0) {
1237 cpus = cores * threads * sockets;
1239 } else if (cores == 0) {
1240 threads = threads > 0 ? threads : 1;
1241 cores = cpus / (sockets * threads);
1242 cores = cores > 0 ? cores : 1;
1243 } else if (threads == 0) {
1244 threads = cpus / (cores * sockets);
1245 threads = threads > 0 ? threads : 1;
1246 } else if (sockets * cores * threads < cpus) {
1247 error_report("cpu topology: "
1248 "sockets (%u) * cores (%u) * threads (%u) < "
1249 "smp_cpus (%u)",
1250 sockets, cores, threads, cpus);
1251 exit(1);
1254 max_cpus = qemu_opt_get_number(opts, "maxcpus", cpus);
1256 if (max_cpus < cpus) {
1257 error_report("maxcpus must be equal to or greater than smp");
1258 exit(1);
1261 if (sockets * cores * threads > max_cpus) {
1262 error_report("cpu topology: "
1263 "sockets (%u) * cores (%u) * threads (%u) > "
1264 "maxcpus (%u)",
1265 sockets, cores, threads, max_cpus);
1266 exit(1);
1269 smp_cpus = cpus;
1270 smp_cores = cores;
1271 smp_threads = threads;
1274 if (smp_cpus > 1) {
1275 Error *blocker = NULL;
1276 error_setg(&blocker, QERR_REPLAY_NOT_SUPPORTED, "smp");
1277 replay_add_blocker(blocker);
1281 static void realtime_init(void)
1283 if (enable_mlock) {
1284 if (os_mlock() < 0) {
1285 error_report("locking memory failed");
1286 exit(1);
1292 static void configure_msg(QemuOpts *opts)
1294 enable_timestamp_msg = qemu_opt_get_bool(opts, "timestamp", true);
1297 /***********************************************************/
1298 /* Semihosting */
1300 typedef struct SemihostingConfig {
1301 bool enabled;
1302 SemihostingTarget target;
1303 const char **argv;
1304 int argc;
1305 const char *cmdline; /* concatenated argv */
1306 } SemihostingConfig;
1308 static SemihostingConfig semihosting;
1310 bool semihosting_enabled(void)
1312 return semihosting.enabled;
1315 SemihostingTarget semihosting_get_target(void)
1317 return semihosting.target;
1320 const char *semihosting_get_arg(int i)
1322 if (i >= semihosting.argc) {
1323 return NULL;
1325 return semihosting.argv[i];
1328 int semihosting_get_argc(void)
1330 return semihosting.argc;
1333 const char *semihosting_get_cmdline(void)
1335 if (semihosting.cmdline == NULL && semihosting.argc > 0) {
1336 semihosting.cmdline = g_strjoinv(" ", (gchar **)semihosting.argv);
1338 return semihosting.cmdline;
1341 static int add_semihosting_arg(void *opaque,
1342 const char *name, const char *val,
1343 Error **errp)
1345 SemihostingConfig *s = opaque;
1346 if (strcmp(name, "arg") == 0) {
1347 s->argc++;
1348 /* one extra element as g_strjoinv() expects NULL-terminated array */
1349 s->argv = g_realloc(s->argv, (s->argc + 1) * sizeof(void *));
1350 s->argv[s->argc - 1] = val;
1351 s->argv[s->argc] = NULL;
1353 return 0;
1356 /* Use strings passed via -kernel/-append to initialize semihosting.argv[] */
1357 static inline void semihosting_arg_fallback(const char *file, const char *cmd)
1359 char *cmd_token;
1361 /* argv[0] */
1362 add_semihosting_arg(&semihosting, "arg", file, NULL);
1364 /* split -append and initialize argv[1..n] */
1365 cmd_token = strtok(g_strdup(cmd), " ");
1366 while (cmd_token) {
1367 add_semihosting_arg(&semihosting, "arg", cmd_token, NULL);
1368 cmd_token = strtok(NULL, " ");
1372 /* Now we still need this for compatibility with XEN. */
1373 bool has_igd_gfx_passthru;
1374 static void igd_gfx_passthru(void)
1376 has_igd_gfx_passthru = current_machine->igd_gfx_passthru;
1379 /***********************************************************/
1380 /* USB devices */
1382 static int usb_device_add(const char *devname)
1384 USBDevice *dev = NULL;
1385 #ifndef CONFIG_LINUX
1386 const char *p;
1387 #endif
1389 if (!machine_usb(current_machine)) {
1390 return -1;
1393 /* drivers with .usbdevice_name entry in USBDeviceInfo */
1394 dev = usbdevice_create(devname);
1395 if (dev)
1396 goto done;
1398 /* the other ones */
1399 #ifndef CONFIG_LINUX
1400 /* only the linux version is qdev-ified, usb-bsd still needs this */
1401 if (strstart(devname, "host:", &p)) {
1402 dev = usb_host_device_open(usb_bus_find(-1), p);
1404 #endif
1405 if (!dev)
1406 return -1;
1408 done:
1409 return 0;
1412 static int usb_device_del(const char *devname)
1414 int bus_num, addr;
1415 const char *p;
1417 if (strstart(devname, "host:", &p)) {
1418 return -1;
1421 if (!machine_usb(current_machine)) {
1422 return -1;
1425 p = strchr(devname, '.');
1426 if (!p)
1427 return -1;
1428 bus_num = strtoul(devname, NULL, 0);
1429 addr = strtoul(p + 1, NULL, 0);
1431 return usb_device_delete_addr(bus_num, addr);
1434 static int usb_parse(const char *cmdline)
1436 int r;
1437 r = usb_device_add(cmdline);
1438 if (r < 0) {
1439 error_report("could not add USB device '%s'", cmdline);
1441 return r;
1444 void hmp_usb_add(Monitor *mon, const QDict *qdict)
1446 const char *devname = qdict_get_str(qdict, "devname");
1447 if (usb_device_add(devname) < 0) {
1448 error_report("could not add USB device '%s'", devname);
1452 void hmp_usb_del(Monitor *mon, const QDict *qdict)
1454 const char *devname = qdict_get_str(qdict, "devname");
1455 if (usb_device_del(devname) < 0) {
1456 error_report("could not delete USB device '%s'", devname);
1460 /***********************************************************/
1461 /* machine registration */
1463 MachineState *current_machine;
1465 static MachineClass *find_machine(const char *name)
1467 GSList *el, *machines = object_class_get_list(TYPE_MACHINE, false);
1468 MachineClass *mc = NULL;
1470 for (el = machines; el; el = el->next) {
1471 MachineClass *temp = el->data;
1473 if (!strcmp(temp->name, name)) {
1474 mc = temp;
1475 break;
1477 if (temp->alias &&
1478 !strcmp(temp->alias, name)) {
1479 mc = temp;
1480 break;
1484 g_slist_free(machines);
1485 return mc;
1488 MachineClass *find_default_machine(void)
1490 GSList *el, *machines = object_class_get_list(TYPE_MACHINE, false);
1491 MachineClass *mc = NULL;
1493 for (el = machines; el; el = el->next) {
1494 MachineClass *temp = el->data;
1496 if (temp->is_default) {
1497 mc = temp;
1498 break;
1502 g_slist_free(machines);
1503 return mc;
1506 MachineInfoList *qmp_query_machines(Error **errp)
1508 GSList *el, *machines = object_class_get_list(TYPE_MACHINE, false);
1509 MachineInfoList *mach_list = NULL;
1511 for (el = machines; el; el = el->next) {
1512 MachineClass *mc = el->data;
1513 MachineInfoList *entry;
1514 MachineInfo *info;
1516 info = g_malloc0(sizeof(*info));
1517 if (mc->is_default) {
1518 info->has_is_default = true;
1519 info->is_default = true;
1522 if (mc->alias) {
1523 info->has_alias = true;
1524 info->alias = g_strdup(mc->alias);
1527 info->name = g_strdup(mc->name);
1528 info->cpu_max = !mc->max_cpus ? 1 : mc->max_cpus;
1529 info->hotpluggable_cpus = mc->has_hotpluggable_cpus;
1531 entry = g_malloc0(sizeof(*entry));
1532 entry->value = info;
1533 entry->next = mach_list;
1534 mach_list = entry;
1537 g_slist_free(machines);
1538 return mach_list;
1541 static int machine_help_func(QemuOpts *opts, MachineState *machine)
1543 ObjectProperty *prop;
1544 ObjectPropertyIterator iter;
1546 if (!qemu_opt_has_help_opt(opts)) {
1547 return 0;
1550 object_property_iter_init(&iter, OBJECT(machine));
1551 while ((prop = object_property_iter_next(&iter))) {
1552 if (!prop->set) {
1553 continue;
1556 error_printf("%s.%s=%s", MACHINE_GET_CLASS(machine)->name,
1557 prop->name, prop->type);
1558 if (prop->description) {
1559 error_printf(" (%s)\n", prop->description);
1560 } else {
1561 error_printf("\n");
1565 return 1;
1568 /***********************************************************/
1569 /* main execution loop */
1571 struct vm_change_state_entry {
1572 VMChangeStateHandler *cb;
1573 void *opaque;
1574 QLIST_ENTRY (vm_change_state_entry) entries;
1577 static QLIST_HEAD(vm_change_state_head, vm_change_state_entry) vm_change_state_head;
1579 VMChangeStateEntry *qemu_add_vm_change_state_handler(VMChangeStateHandler *cb,
1580 void *opaque)
1582 VMChangeStateEntry *e;
1584 e = g_malloc0(sizeof (*e));
1586 e->cb = cb;
1587 e->opaque = opaque;
1588 QLIST_INSERT_HEAD(&vm_change_state_head, e, entries);
1589 return e;
1592 void qemu_del_vm_change_state_handler(VMChangeStateEntry *e)
1594 QLIST_REMOVE (e, entries);
1595 g_free (e);
1598 void vm_state_notify(int running, RunState state)
1600 VMChangeStateEntry *e, *next;
1602 trace_vm_state_notify(running, state);
1604 QLIST_FOREACH_SAFE(e, &vm_change_state_head, entries, next) {
1605 e->cb(e->opaque, running, state);
1609 static int reset_requested;
1610 static int shutdown_requested, shutdown_signal = -1;
1611 static pid_t shutdown_pid;
1612 static int powerdown_requested;
1613 static int debug_requested;
1614 static int suspend_requested;
1615 static WakeupReason wakeup_reason;
1616 static NotifierList powerdown_notifiers =
1617 NOTIFIER_LIST_INITIALIZER(powerdown_notifiers);
1618 static NotifierList suspend_notifiers =
1619 NOTIFIER_LIST_INITIALIZER(suspend_notifiers);
1620 static NotifierList wakeup_notifiers =
1621 NOTIFIER_LIST_INITIALIZER(wakeup_notifiers);
1622 static uint32_t wakeup_reason_mask = ~(1 << QEMU_WAKEUP_REASON_NONE);
1624 int qemu_shutdown_requested_get(void)
1626 return shutdown_requested;
1629 int qemu_reset_requested_get(void)
1631 return reset_requested;
1634 static int qemu_shutdown_requested(void)
1636 return atomic_xchg(&shutdown_requested, 0);
1639 static void qemu_kill_report(void)
1641 if (!qtest_driver() && shutdown_signal != -1) {
1642 if (shutdown_pid == 0) {
1643 /* This happens for eg ^C at the terminal, so it's worth
1644 * avoiding printing an odd message in that case.
1646 error_report("terminating on signal %d", shutdown_signal);
1647 } else {
1648 char *shutdown_cmd = qemu_get_pid_name(shutdown_pid);
1650 error_report("terminating on signal %d from pid " FMT_pid " (%s)",
1651 shutdown_signal, shutdown_pid,
1652 shutdown_cmd ? shutdown_cmd : "<unknown process>");
1653 g_free(shutdown_cmd);
1655 shutdown_signal = -1;
1659 static int qemu_reset_requested(void)
1661 int r = reset_requested;
1662 if (r && replay_checkpoint(CHECKPOINT_RESET_REQUESTED)) {
1663 reset_requested = 0;
1664 return r;
1666 return false;
1669 static int qemu_suspend_requested(void)
1671 int r = suspend_requested;
1672 if (r && replay_checkpoint(CHECKPOINT_SUSPEND_REQUESTED)) {
1673 suspend_requested = 0;
1674 return r;
1676 return false;
1679 static WakeupReason qemu_wakeup_requested(void)
1681 return wakeup_reason;
1684 static int qemu_powerdown_requested(void)
1686 int r = powerdown_requested;
1687 powerdown_requested = 0;
1688 return r;
1691 static int qemu_debug_requested(void)
1693 int r = debug_requested;
1694 debug_requested = 0;
1695 return r;
1698 void qemu_system_reset(bool report)
1700 MachineClass *mc;
1702 mc = current_machine ? MACHINE_GET_CLASS(current_machine) : NULL;
1704 cpu_synchronize_all_states();
1706 if (mc && mc->reset) {
1707 mc->reset();
1708 } else {
1709 qemu_devices_reset();
1711 if (report) {
1712 qapi_event_send_reset(&error_abort);
1714 cpu_synchronize_all_post_reset();
1717 void qemu_system_guest_panicked(GuestPanicInformation *info)
1719 qemu_log_mask(LOG_GUEST_ERROR, "Guest crashed\n");
1721 if (current_cpu) {
1722 current_cpu->crash_occurred = true;
1724 qapi_event_send_guest_panicked(GUEST_PANIC_ACTION_PAUSE,
1725 !!info, info, &error_abort);
1726 vm_stop(RUN_STATE_GUEST_PANICKED);
1727 if (!no_shutdown) {
1728 qapi_event_send_guest_panicked(GUEST_PANIC_ACTION_POWEROFF,
1729 !!info, info, &error_abort);
1730 qemu_system_shutdown_request();
1733 if (info) {
1734 if (info->type == GUEST_PANIC_INFORMATION_TYPE_HYPER_V) {
1735 qemu_log_mask(LOG_GUEST_ERROR, "HV crash parameters: (%#"PRIx64
1736 " %#"PRIx64" %#"PRIx64" %#"PRIx64" %#"PRIx64")\n",
1737 info->u.hyper_v.arg1,
1738 info->u.hyper_v.arg2,
1739 info->u.hyper_v.arg3,
1740 info->u.hyper_v.arg4,
1741 info->u.hyper_v.arg5);
1743 qapi_free_GuestPanicInformation(info);
1747 void qemu_system_reset_request(void)
1749 if (no_reboot) {
1750 shutdown_requested = 1;
1751 } else {
1752 reset_requested = 1;
1754 cpu_stop_current();
1755 qemu_notify_event();
1758 static void qemu_system_suspend(void)
1760 pause_all_vcpus();
1761 notifier_list_notify(&suspend_notifiers, NULL);
1762 runstate_set(RUN_STATE_SUSPENDED);
1763 qapi_event_send_suspend(&error_abort);
1766 void qemu_system_suspend_request(void)
1768 if (runstate_check(RUN_STATE_SUSPENDED)) {
1769 return;
1771 suspend_requested = 1;
1772 cpu_stop_current();
1773 qemu_notify_event();
1776 void qemu_register_suspend_notifier(Notifier *notifier)
1778 notifier_list_add(&suspend_notifiers, notifier);
1781 void qemu_system_wakeup_request(WakeupReason reason)
1783 trace_system_wakeup_request(reason);
1785 if (!runstate_check(RUN_STATE_SUSPENDED)) {
1786 return;
1788 if (!(wakeup_reason_mask & (1 << reason))) {
1789 return;
1791 runstate_set(RUN_STATE_RUNNING);
1792 wakeup_reason = reason;
1793 qemu_notify_event();
1796 void qemu_system_wakeup_enable(WakeupReason reason, bool enabled)
1798 if (enabled) {
1799 wakeup_reason_mask |= (1 << reason);
1800 } else {
1801 wakeup_reason_mask &= ~(1 << reason);
1805 void qemu_register_wakeup_notifier(Notifier *notifier)
1807 notifier_list_add(&wakeup_notifiers, notifier);
1810 void qemu_system_killed(int signal, pid_t pid)
1812 shutdown_signal = signal;
1813 shutdown_pid = pid;
1814 no_shutdown = 0;
1816 /* Cannot call qemu_system_shutdown_request directly because
1817 * we are in a signal handler.
1819 shutdown_requested = 1;
1820 qemu_notify_event();
1823 void qemu_system_shutdown_request(void)
1825 trace_qemu_system_shutdown_request();
1826 replay_shutdown_request();
1827 shutdown_requested = 1;
1828 qemu_notify_event();
1831 static void qemu_system_powerdown(void)
1833 qapi_event_send_powerdown(&error_abort);
1834 notifier_list_notify(&powerdown_notifiers, NULL);
1837 void qemu_system_powerdown_request(void)
1839 trace_qemu_system_powerdown_request();
1840 powerdown_requested = 1;
1841 qemu_notify_event();
1844 void qemu_register_powerdown_notifier(Notifier *notifier)
1846 notifier_list_add(&powerdown_notifiers, notifier);
1849 void qemu_system_debug_request(void)
1851 debug_requested = 1;
1852 qemu_notify_event();
1855 static bool main_loop_should_exit(void)
1857 RunState r;
1858 if (qemu_debug_requested()) {
1859 vm_stop(RUN_STATE_DEBUG);
1861 if (qemu_suspend_requested()) {
1862 qemu_system_suspend();
1864 if (qemu_shutdown_requested()) {
1865 qemu_kill_report();
1866 qapi_event_send_shutdown(&error_abort);
1867 if (no_shutdown) {
1868 vm_stop(RUN_STATE_SHUTDOWN);
1869 } else {
1870 return true;
1873 if (qemu_reset_requested()) {
1874 pause_all_vcpus();
1875 qemu_system_reset(VMRESET_REPORT);
1876 resume_all_vcpus();
1877 if (!runstate_check(RUN_STATE_RUNNING) &&
1878 !runstate_check(RUN_STATE_INMIGRATE)) {
1879 runstate_set(RUN_STATE_PRELAUNCH);
1882 if (qemu_wakeup_requested()) {
1883 pause_all_vcpus();
1884 qemu_system_reset(VMRESET_SILENT);
1885 notifier_list_notify(&wakeup_notifiers, &wakeup_reason);
1886 wakeup_reason = QEMU_WAKEUP_REASON_NONE;
1887 resume_all_vcpus();
1888 qapi_event_send_wakeup(&error_abort);
1890 if (qemu_powerdown_requested()) {
1891 qemu_system_powerdown();
1893 if (qemu_vmstop_requested(&r)) {
1894 vm_stop(r);
1896 return false;
1899 static void main_loop(void)
1901 bool nonblocking;
1902 int last_io = 0;
1903 #ifdef CONFIG_PROFILER
1904 int64_t ti;
1905 #endif
1906 do {
1907 nonblocking = tcg_enabled() && last_io > 0;
1908 #ifdef CONFIG_PROFILER
1909 ti = profile_getclock();
1910 #endif
1911 last_io = main_loop_wait(nonblocking);
1912 #ifdef CONFIG_PROFILER
1913 dev_time += profile_getclock() - ti;
1914 #endif
1915 } while (!main_loop_should_exit());
1918 static void version(void)
1920 printf("QEMU emulator version " QEMU_VERSION QEMU_PKGVERSION "\n"
1921 QEMU_COPYRIGHT "\n");
1924 static void QEMU_NORETURN help(int exitcode)
1926 version();
1927 printf("usage: %s [options] [disk_image]\n\n"
1928 "'disk_image' is a raw hard disk image for IDE hard disk 0\n\n",
1929 error_get_progname());
1931 #define QEMU_OPTIONS_GENERATE_HELP
1932 #include "qemu-options-wrapper.h"
1934 printf("\nDuring emulation, the following keys are useful:\n"
1935 "ctrl-alt-f toggle full screen\n"
1936 "ctrl-alt-n switch to virtual console 'n'\n"
1937 "ctrl-alt toggle mouse and keyboard grab\n"
1938 "\n"
1939 "When using -nographic, press 'ctrl-a h' to get some help.\n");
1941 exit(exitcode);
1944 #define HAS_ARG 0x0001
1946 typedef struct QEMUOption {
1947 const char *name;
1948 int flags;
1949 int index;
1950 uint32_t arch_mask;
1951 } QEMUOption;
1953 static const QEMUOption qemu_options[] = {
1954 { "h", 0, QEMU_OPTION_h, QEMU_ARCH_ALL },
1955 #define QEMU_OPTIONS_GENERATE_OPTIONS
1956 #include "qemu-options-wrapper.h"
1957 { NULL },
1960 typedef struct VGAInterfaceInfo {
1961 const char *opt_name; /* option name */
1962 const char *name; /* human-readable name */
1963 /* Class names indicating that support is available.
1964 * If no class is specified, the interface is always available */
1965 const char *class_names[2];
1966 } VGAInterfaceInfo;
1968 static VGAInterfaceInfo vga_interfaces[VGA_TYPE_MAX] = {
1969 [VGA_NONE] = {
1970 .opt_name = "none",
1972 [VGA_STD] = {
1973 .opt_name = "std",
1974 .name = "standard VGA",
1975 .class_names = { "VGA", "isa-vga" },
1977 [VGA_CIRRUS] = {
1978 .opt_name = "cirrus",
1979 .name = "Cirrus VGA",
1980 .class_names = { "cirrus-vga", "isa-cirrus-vga" },
1982 [VGA_VMWARE] = {
1983 .opt_name = "vmware",
1984 .name = "VMWare SVGA",
1985 .class_names = { "vmware-svga" },
1987 [VGA_VIRTIO] = {
1988 .opt_name = "virtio",
1989 .name = "Virtio VGA",
1990 .class_names = { "virtio-vga" },
1992 [VGA_QXL] = {
1993 .opt_name = "qxl",
1994 .name = "QXL VGA",
1995 .class_names = { "qxl-vga" },
1997 [VGA_TCX] = {
1998 .opt_name = "tcx",
1999 .name = "TCX framebuffer",
2000 .class_names = { "SUNW,tcx" },
2002 [VGA_CG3] = {
2003 .opt_name = "cg3",
2004 .name = "CG3 framebuffer",
2005 .class_names = { "cgthree" },
2007 [VGA_XENFB] = {
2008 .opt_name = "xenfb",
2012 static bool vga_interface_available(VGAInterfaceType t)
2014 VGAInterfaceInfo *ti = &vga_interfaces[t];
2016 assert(t < VGA_TYPE_MAX);
2017 return !ti->class_names[0] ||
2018 object_class_by_name(ti->class_names[0]) ||
2019 object_class_by_name(ti->class_names[1]);
2022 static void select_vgahw(const char *p)
2024 const char *opts;
2025 int t;
2027 assert(vga_interface_type == VGA_NONE);
2028 for (t = 0; t < VGA_TYPE_MAX; t++) {
2029 VGAInterfaceInfo *ti = &vga_interfaces[t];
2030 if (ti->opt_name && strstart(p, ti->opt_name, &opts)) {
2031 if (!vga_interface_available(t)) {
2032 error_report("%s not available", ti->name);
2033 exit(1);
2035 vga_interface_type = t;
2036 break;
2039 if (t == VGA_TYPE_MAX) {
2040 invalid_vga:
2041 error_report("unknown vga type: %s", p);
2042 exit(1);
2044 while (*opts) {
2045 const char *nextopt;
2047 if (strstart(opts, ",retrace=", &nextopt)) {
2048 opts = nextopt;
2049 if (strstart(opts, "dumb", &nextopt))
2050 vga_retrace_method = VGA_RETRACE_DUMB;
2051 else if (strstart(opts, "precise", &nextopt))
2052 vga_retrace_method = VGA_RETRACE_PRECISE;
2053 else goto invalid_vga;
2054 } else goto invalid_vga;
2055 opts = nextopt;
2059 typedef enum DisplayType {
2060 DT_DEFAULT,
2061 DT_CURSES,
2062 DT_SDL,
2063 DT_COCOA,
2064 DT_GTK,
2065 DT_NONE,
2066 } DisplayType;
2068 static DisplayType select_display(const char *p)
2070 const char *opts;
2071 DisplayType display = DT_DEFAULT;
2073 if (strstart(p, "sdl", &opts)) {
2074 #ifdef CONFIG_SDL
2075 display = DT_SDL;
2076 while (*opts) {
2077 const char *nextopt;
2079 if (strstart(opts, ",frame=", &nextopt)) {
2080 opts = nextopt;
2081 if (strstart(opts, "on", &nextopt)) {
2082 no_frame = 0;
2083 } else if (strstart(opts, "off", &nextopt)) {
2084 no_frame = 1;
2085 } else {
2086 goto invalid_sdl_args;
2088 } else if (strstart(opts, ",alt_grab=", &nextopt)) {
2089 opts = nextopt;
2090 if (strstart(opts, "on", &nextopt)) {
2091 alt_grab = 1;
2092 } else if (strstart(opts, "off", &nextopt)) {
2093 alt_grab = 0;
2094 } else {
2095 goto invalid_sdl_args;
2097 } else if (strstart(opts, ",ctrl_grab=", &nextopt)) {
2098 opts = nextopt;
2099 if (strstart(opts, "on", &nextopt)) {
2100 ctrl_grab = 1;
2101 } else if (strstart(opts, "off", &nextopt)) {
2102 ctrl_grab = 0;
2103 } else {
2104 goto invalid_sdl_args;
2106 } else if (strstart(opts, ",window_close=", &nextopt)) {
2107 opts = nextopt;
2108 if (strstart(opts, "on", &nextopt)) {
2109 no_quit = 0;
2110 } else if (strstart(opts, "off", &nextopt)) {
2111 no_quit = 1;
2112 } else {
2113 goto invalid_sdl_args;
2115 } else if (strstart(opts, ",gl=", &nextopt)) {
2116 opts = nextopt;
2117 if (strstart(opts, "on", &nextopt)) {
2118 request_opengl = 1;
2119 } else if (strstart(opts, "off", &nextopt)) {
2120 request_opengl = 0;
2121 } else {
2122 goto invalid_sdl_args;
2124 } else {
2125 invalid_sdl_args:
2126 error_report("invalid SDL option string");
2127 exit(1);
2129 opts = nextopt;
2131 #else
2132 error_report("SDL support is disabled");
2133 exit(1);
2134 #endif
2135 } else if (strstart(p, "vnc", &opts)) {
2136 if (*opts == '=') {
2137 vnc_parse(opts + 1, &error_fatal);
2138 } else {
2139 error_report("VNC requires a display argument vnc=<display>");
2140 exit(1);
2142 } else if (strstart(p, "curses", &opts)) {
2143 #ifdef CONFIG_CURSES
2144 display = DT_CURSES;
2145 #else
2146 error_report("curses support is disabled");
2147 exit(1);
2148 #endif
2149 } else if (strstart(p, "gtk", &opts)) {
2150 #ifdef CONFIG_GTK
2151 display = DT_GTK;
2152 while (*opts) {
2153 const char *nextopt;
2155 if (strstart(opts, ",grab_on_hover=", &nextopt)) {
2156 opts = nextopt;
2157 if (strstart(opts, "on", &nextopt)) {
2158 grab_on_hover = true;
2159 } else if (strstart(opts, "off", &nextopt)) {
2160 grab_on_hover = false;
2161 } else {
2162 goto invalid_gtk_args;
2164 } else if (strstart(opts, ",gl=", &nextopt)) {
2165 opts = nextopt;
2166 if (strstart(opts, "on", &nextopt)) {
2167 request_opengl = 1;
2168 } else if (strstart(opts, "off", &nextopt)) {
2169 request_opengl = 0;
2170 } else {
2171 goto invalid_gtk_args;
2173 } else {
2174 invalid_gtk_args:
2175 error_report("invalid GTK option string");
2176 exit(1);
2178 opts = nextopt;
2180 #else
2181 error_report("GTK support is disabled");
2182 exit(1);
2183 #endif
2184 } else if (strstart(p, "none", &opts)) {
2185 display = DT_NONE;
2186 } else {
2187 error_report("unknown display type");
2188 exit(1);
2191 return display;
2194 static int balloon_parse(const char *arg)
2196 QemuOpts *opts;
2198 if (strcmp(arg, "none") == 0) {
2199 return 0;
2202 if (!strncmp(arg, "virtio", 6)) {
2203 if (arg[6] == ',') {
2204 /* have params -> parse them */
2205 opts = qemu_opts_parse_noisily(qemu_find_opts("device"), arg + 7,
2206 false);
2207 if (!opts)
2208 return -1;
2209 } else {
2210 /* create empty opts */
2211 opts = qemu_opts_create(qemu_find_opts("device"), NULL, 0,
2212 &error_abort);
2214 qemu_opt_set(opts, "driver", "virtio-balloon", &error_abort);
2215 return 0;
2218 return -1;
2221 #if defined(CONFIG_RUBY)
2222 /* Ruby interface for QEMU. See README.EXT for programming example. */
2223 #warning mit ruby
2224 #include <ruby.h>
2226 static VALUE qemu_open(int argc, VALUE *argv, VALUE klass)
2228 int i;
2229 for (i = 0; i < argc; i++) {
2230 argv[i] = StringValue(argv[i]);
2231 fprintf(stderr, "argv[%d] = %s\n", i, StringValuePtr(argv[i]));
2233 //~ if (rb_scan_args(argc, argv, "11", &file, &vmode) == 1) {
2234 //~ mode = 0666; /* default value */
2235 //~ }
2236 return INT2FIX(argc);
2239 void Init_qemu(void)
2241 printf("%s\n", __func__);
2242 VALUE cQEMU = rb_define_class("QEMU", rb_cObject);
2243 rb_define_singleton_method(cQEMU, "run", qemu_open, -1);
2244 #if 0
2245 rb_define_method();
2246 #endif
2248 #endif /* CONFIG_RUBY */
2250 #if defined(CONFIG_DLL)
2251 BOOL APIENTRY DllMain(HINSTANCE hModule, DWORD ul_reason_for_call, LPVOID data)
2253 return FALSE;
2255 #endif /* CONFIG_DLL */
2257 char *qemu_find_file(int type, const char *name)
2259 int i;
2260 const char *subdir;
2261 char *buf;
2263 /* Try the name as a straight path first */
2264 if (access(name, R_OK) == 0) {
2265 trace_load_file(name, name);
2266 return g_strdup(name);
2269 switch (type) {
2270 case QEMU_FILE_TYPE_BIOS:
2271 subdir = "";
2272 break;
2273 case QEMU_FILE_TYPE_KEYMAP:
2274 subdir = "keymaps/";
2275 break;
2276 default:
2277 abort();
2280 for (i = 0; i < data_dir_idx; i++) {
2281 buf = g_strdup_printf("%s/%s%s", data_dir[i], subdir, name);
2282 if (access(buf, R_OK) == 0) {
2283 trace_load_file(name, buf);
2284 return buf;
2286 g_free(buf);
2288 return NULL;
2291 static inline bool nonempty_str(const char *str)
2293 return str && *str;
2296 static int parse_fw_cfg(void *opaque, QemuOpts *opts, Error **errp)
2298 gchar *buf;
2299 size_t size;
2300 const char *name, *file, *str;
2301 FWCfgState *fw_cfg = (FWCfgState *) opaque;
2303 if (fw_cfg == NULL) {
2304 error_report("fw_cfg device not available");
2305 return -1;
2307 name = qemu_opt_get(opts, "name");
2308 file = qemu_opt_get(opts, "file");
2309 str = qemu_opt_get(opts, "string");
2311 /* we need name and either a file or the content string */
2312 if (!(nonempty_str(name) && (nonempty_str(file) || nonempty_str(str)))) {
2313 error_report("invalid argument(s)");
2314 return -1;
2316 if (nonempty_str(file) && nonempty_str(str)) {
2317 error_report("file and string are mutually exclusive");
2318 return -1;
2320 if (strlen(name) > FW_CFG_MAX_FILE_PATH - 1) {
2321 error_report("name too long (max. %d char)", FW_CFG_MAX_FILE_PATH - 1);
2322 return -1;
2324 if (strncmp(name, "opt/", 4) != 0) {
2325 error_report("warning: externally provided fw_cfg item names "
2326 "should be prefixed with \"opt/\"");
2328 if (nonempty_str(str)) {
2329 size = strlen(str); /* NUL terminator NOT included in fw_cfg blob */
2330 buf = g_memdup(str, size);
2331 } else {
2332 if (!g_file_get_contents(file, &buf, &size, NULL)) {
2333 error_report("can't load %s", file);
2334 return -1;
2337 /* For legacy, keep user files in a specific global order. */
2338 fw_cfg_set_order_override(fw_cfg, FW_CFG_ORDER_OVERRIDE_USER);
2339 fw_cfg_add_file(fw_cfg, name, buf, size);
2340 fw_cfg_reset_order_override(fw_cfg);
2341 return 0;
2344 static int device_help_func(void *opaque, QemuOpts *opts, Error **errp)
2346 return qdev_device_help(opts);
2349 static int device_init_func(void *opaque, QemuOpts *opts, Error **errp)
2351 Error *err = NULL;
2352 DeviceState *dev;
2354 dev = qdev_device_add(opts, &err);
2355 if (!dev) {
2356 error_report_err(err);
2357 return -1;
2359 object_unref(OBJECT(dev));
2360 return 0;
2363 static int chardev_init_func(void *opaque, QemuOpts *opts, Error **errp)
2365 Error *local_err = NULL;
2367 qemu_chr_new_from_opts(opts, &local_err);
2368 if (local_err) {
2369 error_report_err(local_err);
2370 return -1;
2372 return 0;
2375 #ifdef CONFIG_VIRTFS
2376 static int fsdev_init_func(void *opaque, QemuOpts *opts, Error **errp)
2378 return qemu_fsdev_add(opts);
2380 #endif
2382 static int mon_init_func(void *opaque, QemuOpts *opts, Error **errp)
2384 Chardev *chr;
2385 const char *chardev;
2386 const char *mode;
2387 int flags;
2389 mode = qemu_opt_get(opts, "mode");
2390 if (mode == NULL) {
2391 mode = "readline";
2393 if (strcmp(mode, "readline") == 0) {
2394 flags = MONITOR_USE_READLINE;
2395 } else if (strcmp(mode, "control") == 0) {
2396 flags = MONITOR_USE_CONTROL;
2397 } else {
2398 error_report("unknown monitor mode \"%s\"", mode);
2399 exit(1);
2402 if (qemu_opt_get_bool(opts, "pretty", 0))
2403 flags |= MONITOR_USE_PRETTY;
2405 if (qemu_opt_get_bool(opts, "default", 0)) {
2406 error_report("option 'default' does nothing and is deprecated");
2409 chardev = qemu_opt_get(opts, "chardev");
2410 chr = qemu_chr_find(chardev);
2411 if (chr == NULL) {
2412 error_report("chardev \"%s\" not found", chardev);
2413 exit(1);
2416 monitor_init(chr, flags);
2417 return 0;
2420 static void monitor_parse(const char *optarg, const char *mode, bool pretty)
2422 static int monitor_device_index = 0;
2423 QemuOpts *opts;
2424 const char *p;
2425 char label[32];
2427 if (strstart(optarg, "chardev:", &p)) {
2428 snprintf(label, sizeof(label), "%s", p);
2429 } else {
2430 snprintf(label, sizeof(label), "compat_monitor%d",
2431 monitor_device_index);
2432 opts = qemu_chr_parse_compat(label, optarg);
2433 if (!opts) {
2434 error_report("parse error: %s", optarg);
2435 exit(1);
2439 opts = qemu_opts_create(qemu_find_opts("mon"), label, 1, &error_fatal);
2440 qemu_opt_set(opts, "mode", mode, &error_abort);
2441 qemu_opt_set(opts, "chardev", label, &error_abort);
2442 qemu_opt_set_bool(opts, "pretty", pretty, &error_abort);
2443 monitor_device_index++;
2446 struct device_config {
2447 enum {
2448 DEV_USB, /* -usbdevice */
2449 DEV_BT, /* -bt */
2450 DEV_SERIAL, /* -serial */
2451 DEV_PARALLEL, /* -parallel */
2452 DEV_VIRTCON, /* -virtioconsole */
2453 DEV_DEBUGCON, /* -debugcon */
2454 DEV_GDB, /* -gdb, -s */
2455 DEV_SCLP, /* s390 sclp */
2456 } type;
2457 const char *cmdline;
2458 Location loc;
2459 QTAILQ_ENTRY(device_config) next;
2462 static QTAILQ_HEAD(, device_config) device_configs =
2463 QTAILQ_HEAD_INITIALIZER(device_configs);
2465 static void add_device_config(int type, const char *cmdline)
2467 struct device_config *conf;
2469 conf = g_malloc0(sizeof(*conf));
2470 conf->type = type;
2471 conf->cmdline = cmdline;
2472 loc_save(&conf->loc);
2473 QTAILQ_INSERT_TAIL(&device_configs, conf, next);
2476 static int foreach_device_config(int type, int (*func)(const char *cmdline))
2478 struct device_config *conf;
2479 int rc;
2481 QTAILQ_FOREACH(conf, &device_configs, next) {
2482 if (conf->type != type)
2483 continue;
2484 loc_push_restore(&conf->loc);
2485 rc = func(conf->cmdline);
2486 loc_pop(&conf->loc);
2487 if (rc) {
2488 return rc;
2491 return 0;
2494 static int serial_parse(const char *devname)
2496 static int index = 0;
2497 char label[32];
2499 if (strcmp(devname, "none") == 0)
2500 return 0;
2501 if (index == MAX_SERIAL_PORTS) {
2502 error_report("too many serial ports");
2503 exit(1);
2505 snprintf(label, sizeof(label), "serial%d", index);
2506 serial_hds[index] = qemu_chr_new(label, devname);
2507 if (!serial_hds[index]) {
2508 error_report("could not connect serial device"
2509 " to character backend '%s'", devname);
2510 return -1;
2512 index++;
2513 return 0;
2516 static int parallel_parse(const char *devname)
2518 static int index = 0;
2519 char label[32];
2521 if (strcmp(devname, "none") == 0)
2522 return 0;
2523 if (index == MAX_PARALLEL_PORTS) {
2524 error_report("too many parallel ports");
2525 exit(1);
2527 snprintf(label, sizeof(label), "parallel%d", index);
2528 parallel_hds[index] = qemu_chr_new(label, devname);
2529 if (!parallel_hds[index]) {
2530 error_report("could not connect parallel device"
2531 " to character backend '%s'", devname);
2532 return -1;
2534 index++;
2535 return 0;
2538 static int virtcon_parse(const char *devname)
2540 QemuOptsList *device = qemu_find_opts("device");
2541 static int index = 0;
2542 char label[32];
2543 QemuOpts *bus_opts, *dev_opts;
2545 if (strcmp(devname, "none") == 0)
2546 return 0;
2547 if (index == MAX_VIRTIO_CONSOLES) {
2548 error_report("too many virtio consoles");
2549 exit(1);
2552 bus_opts = qemu_opts_create(device, NULL, 0, &error_abort);
2553 qemu_opt_set(bus_opts, "driver", "virtio-serial", &error_abort);
2555 dev_opts = qemu_opts_create(device, NULL, 0, &error_abort);
2556 qemu_opt_set(dev_opts, "driver", "virtconsole", &error_abort);
2558 snprintf(label, sizeof(label), "virtcon%d", index);
2559 virtcon_hds[index] = qemu_chr_new(label, devname);
2560 if (!virtcon_hds[index]) {
2561 error_report("could not connect virtio console"
2562 " to character backend '%s'", devname);
2563 return -1;
2565 qemu_opt_set(dev_opts, "chardev", label, &error_abort);
2567 index++;
2568 return 0;
2571 static int sclp_parse(const char *devname)
2573 QemuOptsList *device = qemu_find_opts("device");
2574 static int index = 0;
2575 char label[32];
2576 QemuOpts *dev_opts;
2578 if (strcmp(devname, "none") == 0) {
2579 return 0;
2581 if (index == MAX_SCLP_CONSOLES) {
2582 error_report("too many sclp consoles");
2583 exit(1);
2586 assert(arch_type == QEMU_ARCH_S390X);
2588 dev_opts = qemu_opts_create(device, NULL, 0, NULL);
2589 qemu_opt_set(dev_opts, "driver", "sclpconsole", &error_abort);
2591 snprintf(label, sizeof(label), "sclpcon%d", index);
2592 sclp_hds[index] = qemu_chr_new(label, devname);
2593 if (!sclp_hds[index]) {
2594 error_report("could not connect sclp console"
2595 " to character backend '%s'", devname);
2596 return -1;
2598 qemu_opt_set(dev_opts, "chardev", label, &error_abort);
2600 index++;
2601 return 0;
2604 static int debugcon_parse(const char *devname)
2606 QemuOpts *opts;
2608 if (!qemu_chr_new("debugcon", devname)) {
2609 exit(1);
2611 opts = qemu_opts_create(qemu_find_opts("device"), "debugcon", 1, NULL);
2612 if (!opts) {
2613 error_report("already have a debugcon device");
2614 exit(1);
2616 qemu_opt_set(opts, "driver", "isa-debugcon", &error_abort);
2617 qemu_opt_set(opts, "chardev", "debugcon", &error_abort);
2618 return 0;
2621 static gint machine_class_cmp(gconstpointer a, gconstpointer b)
2623 const MachineClass *mc1 = a, *mc2 = b;
2624 int res;
2626 if (mc1->family == NULL) {
2627 if (mc2->family == NULL) {
2628 /* Compare standalone machine types against each other; they sort
2629 * in increasing order.
2631 return strcmp(object_class_get_name(OBJECT_CLASS(mc1)),
2632 object_class_get_name(OBJECT_CLASS(mc2)));
2635 /* Standalone machine types sort after families. */
2636 return 1;
2639 if (mc2->family == NULL) {
2640 /* Families sort before standalone machine types. */
2641 return -1;
2644 /* Families sort between each other alphabetically increasingly. */
2645 res = strcmp(mc1->family, mc2->family);
2646 if (res != 0) {
2647 return res;
2650 /* Within the same family, machine types sort in decreasing order. */
2651 return strcmp(object_class_get_name(OBJECT_CLASS(mc2)),
2652 object_class_get_name(OBJECT_CLASS(mc1)));
2655 static MachineClass *machine_parse(const char *name)
2657 MachineClass *mc = NULL;
2658 GSList *el, *machines = object_class_get_list(TYPE_MACHINE, false);
2660 if (name) {
2661 mc = find_machine(name);
2663 if (mc) {
2664 g_slist_free(machines);
2665 return mc;
2667 if (name && !is_help_option(name)) {
2668 error_report("unsupported machine type");
2669 error_printf("Use -machine help to list supported machines\n");
2670 } else {
2671 printf("Supported machines are:\n");
2672 machines = g_slist_sort(machines, machine_class_cmp);
2673 for (el = machines; el; el = el->next) {
2674 MachineClass *mc = el->data;
2675 if (mc->alias) {
2676 printf("%-20s %s (alias of %s)\n", mc->alias, mc->desc, mc->name);
2678 printf("%-20s %s%s\n", mc->name, mc->desc,
2679 mc->is_default ? " (default)" : "");
2683 g_slist_free(machines);
2684 exit(!name || !is_help_option(name));
2687 void qemu_add_exit_notifier(Notifier *notify)
2689 notifier_list_add(&exit_notifiers, notify);
2692 void qemu_remove_exit_notifier(Notifier *notify)
2694 notifier_remove(notify);
2697 static void qemu_run_exit_notifiers(void)
2699 notifier_list_notify(&exit_notifiers, NULL);
2702 static bool machine_init_done;
2704 void qemu_add_machine_init_done_notifier(Notifier *notify)
2706 notifier_list_add(&machine_init_done_notifiers, notify);
2707 if (machine_init_done) {
2708 notify->notify(notify, NULL);
2712 void qemu_remove_machine_init_done_notifier(Notifier *notify)
2714 notifier_remove(notify);
2717 static void qemu_run_machine_init_done_notifiers(void)
2719 notifier_list_notify(&machine_init_done_notifiers, NULL);
2720 machine_init_done = true;
2723 static const QEMUOption *lookup_opt(int argc, char **argv,
2724 const char **poptarg, int *poptind)
2726 const QEMUOption *popt;
2727 int optind = *poptind;
2728 char *r = argv[optind];
2729 const char *optarg;
2731 loc_set_cmdline(argv, optind, 1);
2732 optind++;
2733 /* Treat --foo the same as -foo. */
2734 if (r[1] == '-')
2735 r++;
2736 popt = qemu_options;
2737 for(;;) {
2738 if (!popt->name) {
2739 error_report("invalid option");
2740 exit(1);
2742 if (!strcmp(popt->name, r + 1))
2743 break;
2744 popt++;
2746 if (popt->flags & HAS_ARG) {
2747 if (optind >= argc) {
2748 error_report("requires an argument");
2749 exit(1);
2751 optarg = argv[optind++];
2752 loc_set_cmdline(argv, optind - 2, 2);
2753 } else {
2754 optarg = NULL;
2757 *poptarg = optarg;
2758 *poptind = optind;
2760 return popt;
2763 static MachineClass *select_machine(void)
2765 MachineClass *machine_class = find_default_machine();
2766 const char *optarg;
2767 QemuOpts *opts;
2768 Location loc;
2770 loc_push_none(&loc);
2772 opts = qemu_get_machine_opts();
2773 qemu_opts_loc_restore(opts);
2775 optarg = qemu_opt_get(opts, "type");
2776 if (optarg) {
2777 machine_class = machine_parse(optarg);
2780 if (!machine_class) {
2781 error_report("No machine specified, and there is no default");
2782 error_printf("Use -machine help to list supported machines\n");
2783 exit(1);
2786 loc_pop(&loc);
2787 return machine_class;
2790 static int machine_set_property(void *opaque,
2791 const char *name, const char *value,
2792 Error **errp)
2794 Object *obj = OBJECT(opaque);
2795 Error *local_err = NULL;
2796 char *p, *qom_name;
2798 if (strcmp(name, "type") == 0) {
2799 return 0;
2802 qom_name = g_strdup(name);
2803 for (p = qom_name; *p; p++) {
2804 if (*p == '_') {
2805 *p = '-';
2809 object_property_parse(obj, value, qom_name, &local_err);
2810 g_free(qom_name);
2812 if (local_err) {
2813 error_report_err(local_err);
2814 return -1;
2817 return 0;
2822 * Initial object creation happens before all other
2823 * QEMU data types are created. The majority of objects
2824 * can be created at this point. The rng-egd object
2825 * cannot be created here, as it depends on the chardev
2826 * already existing.
2828 static bool object_create_initial(const char *type)
2830 if (g_str_equal(type, "rng-egd")) {
2831 return false;
2835 * return false for concrete netfilters since
2836 * they depend on netdevs already existing
2838 if (g_str_equal(type, "filter-buffer") ||
2839 g_str_equal(type, "filter-dump") ||
2840 g_str_equal(type, "filter-mirror") ||
2841 g_str_equal(type, "filter-redirector") ||
2842 g_str_equal(type, "colo-compare") ||
2843 g_str_equal(type, "filter-rewriter") ||
2844 g_str_equal(type, "filter-replay")) {
2845 return false;
2848 /* Memory allocation by backends needs to be done
2849 * after configure_accelerator() (due to the tcg_enabled()
2850 * checks at memory_region_init_*()).
2852 * Also, allocation of large amounts of memory may delay
2853 * chardev initialization for too long, and trigger timeouts
2854 * on software that waits for a monitor socket to be created
2855 * (e.g. libvirt).
2857 if (g_str_has_prefix(type, "memory-backend-")) {
2858 return false;
2861 return true;
2866 * The remainder of object creation happens after the
2867 * creation of chardev, fsdev, net clients and device data types.
2869 static bool object_create_delayed(const char *type)
2871 return !object_create_initial(type);
2875 static void set_memory_options(uint64_t *ram_slots, ram_addr_t *maxram_size,
2876 MachineClass *mc)
2878 uint64_t sz;
2879 const char *mem_str;
2880 const char *maxmem_str, *slots_str;
2881 const ram_addr_t default_ram_size = mc->default_ram_size;
2882 QemuOpts *opts = qemu_find_opts_singleton("memory");
2883 Location loc;
2885 loc_push_none(&loc);
2886 qemu_opts_loc_restore(opts);
2888 sz = 0;
2889 mem_str = qemu_opt_get(opts, "size");
2890 if (mem_str) {
2891 if (!*mem_str) {
2892 error_report("missing 'size' option value");
2893 exit(EXIT_FAILURE);
2896 sz = qemu_opt_get_size(opts, "size", ram_size);
2898 /* Fix up legacy suffix-less format */
2899 if (g_ascii_isdigit(mem_str[strlen(mem_str) - 1])) {
2900 uint64_t overflow_check = sz;
2902 sz <<= 20;
2903 if ((sz >> 20) != overflow_check) {
2904 error_report("too large 'size' option value");
2905 exit(EXIT_FAILURE);
2910 /* backward compatibility behaviour for case "-m 0" */
2911 if (sz == 0) {
2912 sz = default_ram_size;
2915 sz = QEMU_ALIGN_UP(sz, 8192);
2916 ram_size = sz;
2917 if (ram_size != sz) {
2918 error_report("ram size too large");
2919 exit(EXIT_FAILURE);
2922 /* store value for the future use */
2923 qemu_opt_set_number(opts, "size", ram_size, &error_abort);
2924 *maxram_size = ram_size;
2926 maxmem_str = qemu_opt_get(opts, "maxmem");
2927 slots_str = qemu_opt_get(opts, "slots");
2928 if (maxmem_str && slots_str) {
2929 uint64_t slots;
2931 sz = qemu_opt_get_size(opts, "maxmem", 0);
2932 slots = qemu_opt_get_number(opts, "slots", 0);
2933 if (sz < ram_size) {
2934 error_report("invalid value of -m option maxmem: "
2935 "maximum memory size (0x%" PRIx64 ") must be at least "
2936 "the initial memory size (0x" RAM_ADDR_FMT ")",
2937 sz, ram_size);
2938 exit(EXIT_FAILURE);
2939 } else if (sz > ram_size) {
2940 if (!slots) {
2941 error_report("invalid value of -m option: maxmem was "
2942 "specified, but no hotplug slots were specified");
2943 exit(EXIT_FAILURE);
2945 } else if (slots) {
2946 error_report("invalid value of -m option maxmem: "
2947 "memory slots were specified but maximum memory size "
2948 "(0x%" PRIx64 ") is equal to the initial memory size "
2949 "(0x" RAM_ADDR_FMT ")", sz, ram_size);
2950 exit(EXIT_FAILURE);
2953 *maxram_size = sz;
2954 *ram_slots = slots;
2955 } else if ((!maxmem_str && slots_str) ||
2956 (maxmem_str && !slots_str)) {
2957 error_report("invalid -m option value: missing "
2958 "'%s' option", slots_str ? "maxmem" : "slots");
2959 exit(EXIT_FAILURE);
2962 loc_pop(&loc);
2965 static int global_init_func(void *opaque, QemuOpts *opts, Error **errp)
2967 GlobalProperty *g;
2969 g = g_malloc0(sizeof(*g));
2970 g->driver = qemu_opt_get(opts, "driver");
2971 g->property = qemu_opt_get(opts, "property");
2972 g->value = qemu_opt_get(opts, "value");
2973 g->user_provided = true;
2974 g->errp = &error_fatal;
2975 qdev_prop_register_global(g);
2976 return 0;
2979 static int qemu_read_default_config_file(void)
2981 int ret;
2983 ret = qemu_read_config_file(CONFIG_QEMU_CONFDIR "/qemu.conf");
2984 if (ret < 0 && ret != -ENOENT) {
2985 return ret;
2988 return 0;
2991 int main(int argc, char **argv)
2993 int i;
2994 int snapshot, linux_boot;
2995 const char *initrd_filename;
2996 const char *kernel_filename, *kernel_cmdline;
2997 const char *boot_order = NULL;
2998 const char *boot_once = NULL;
2999 DisplayState *ds;
3000 int cyls, heads, secs, translation;
3001 QemuOpts *opts, *machine_opts;
3002 QemuOpts *hda_opts = NULL, *icount_opts = NULL, *accel_opts = NULL;
3003 QemuOptsList *olist;
3004 int optind;
3005 const char *optarg;
3006 const char *loadvm = NULL;
3007 MachineClass *machine_class;
3008 const char *cpu_model;
3009 const char *vga_model = NULL;
3010 const char *qtest_chrdev = NULL;
3011 const char *qtest_log = NULL;
3012 const char *pid_file = NULL;
3013 const char *incoming = NULL;
3014 bool defconfig = true;
3015 bool userconfig = true;
3016 bool nographic = false;
3017 DisplayType display_type = DT_DEFAULT;
3018 int display_remote = 0;
3019 const char *log_mask = NULL;
3020 const char *log_file = NULL;
3021 char *trace_file = NULL;
3022 ram_addr_t maxram_size;
3023 uint64_t ram_slots = 0;
3024 FILE *vmstate_dump_file = NULL;
3025 Error *main_loop_err = NULL;
3026 Error *err = NULL;
3027 bool list_data_dirs = false;
3028 typedef struct BlockdevOptions_queue {
3029 BlockdevOptions *bdo;
3030 Location loc;
3031 QSIMPLEQ_ENTRY(BlockdevOptions_queue) entry;
3032 } BlockdevOptions_queue;
3033 QSIMPLEQ_HEAD(, BlockdevOptions_queue) bdo_queue
3034 = QSIMPLEQ_HEAD_INITIALIZER(bdo_queue);
3036 module_call_init(MODULE_INIT_TRACE);
3038 qemu_init_cpu_list();
3039 qemu_init_cpu_loop();
3040 qemu_mutex_lock_iothread();
3042 atexit(qemu_run_exit_notifiers);
3043 error_set_progname(argv[0]);
3044 qemu_init_exec_dir(argv[0]);
3046 module_call_init(MODULE_INIT_QOM);
3047 monitor_init_qmp_commands();
3049 qemu_add_opts(&qemu_drive_opts);
3050 qemu_add_drive_opts(&qemu_legacy_drive_opts);
3051 qemu_add_drive_opts(&qemu_common_drive_opts);
3052 qemu_add_drive_opts(&qemu_drive_opts);
3053 qemu_add_drive_opts(&bdrv_runtime_opts);
3054 qemu_add_opts(&qemu_chardev_opts);
3055 qemu_add_opts(&qemu_device_opts);
3056 qemu_add_opts(&qemu_netdev_opts);
3057 qemu_add_opts(&qemu_net_opts);
3058 qemu_add_opts(&qemu_rtc_opts);
3059 qemu_add_opts(&qemu_global_opts);
3060 qemu_add_opts(&qemu_mon_opts);
3061 qemu_add_opts(&qemu_trace_opts);
3062 qemu_add_opts(&qemu_option_rom_opts);
3063 qemu_add_opts(&qemu_machine_opts);
3064 qemu_add_opts(&qemu_accel_opts);
3065 qemu_add_opts(&qemu_mem_opts);
3066 qemu_add_opts(&qemu_smp_opts);
3067 qemu_add_opts(&qemu_boot_opts);
3068 qemu_add_opts(&qemu_sandbox_opts);
3069 qemu_add_opts(&qemu_add_fd_opts);
3070 qemu_add_opts(&qemu_object_opts);
3071 qemu_add_opts(&qemu_tpmdev_opts);
3072 qemu_add_opts(&qemu_realtime_opts);
3073 qemu_add_opts(&qemu_msg_opts);
3074 qemu_add_opts(&qemu_name_opts);
3075 qemu_add_opts(&qemu_numa_opts);
3076 qemu_add_opts(&qemu_icount_opts);
3077 qemu_add_opts(&qemu_semihosting_config_opts);
3078 qemu_add_opts(&qemu_fw_cfg_opts);
3079 module_call_init(MODULE_INIT_OPTS);
3081 runstate_init();
3083 if (qcrypto_init(&err) < 0) {
3084 error_reportf_err(err, "cannot initialize crypto: ");
3085 exit(1);
3087 rtc_clock = QEMU_CLOCK_HOST;
3089 QLIST_INIT (&vm_change_state_head);
3090 os_setup_early_signal_handling();
3092 cpu_model = NULL;
3093 snapshot = 0;
3094 cyls = heads = secs = 0;
3095 translation = BIOS_ATA_TRANSLATION_AUTO;
3097 nb_nics = 0;
3099 bdrv_init_with_whitelist();
3101 autostart = 1;
3103 /* first pass of option parsing */
3104 optind = 1;
3105 while (optind < argc) {
3106 if (argv[optind][0] != '-') {
3107 /* disk image */
3108 optind++;
3109 } else {
3110 const QEMUOption *popt;
3112 popt = lookup_opt(argc, argv, &optarg, &optind);
3113 switch (popt->index) {
3114 case QEMU_OPTION_nodefconfig:
3115 defconfig = false;
3116 break;
3117 case QEMU_OPTION_nouserconfig:
3118 userconfig = false;
3119 break;
3124 if (defconfig && userconfig) {
3125 if (qemu_read_default_config_file() < 0) {
3126 exit(1);
3130 /* second pass of option parsing */
3131 optind = 1;
3132 for(;;) {
3133 if (optind >= argc)
3134 break;
3135 if (argv[optind][0] != '-') {
3136 hda_opts = drive_add(IF_DEFAULT, 0, argv[optind++], HD_OPTS);
3137 } else {
3138 const QEMUOption *popt;
3140 popt = lookup_opt(argc, argv, &optarg, &optind);
3141 if (!(popt->arch_mask & arch_type)) {
3142 error_report("Option not supported for this target");
3143 exit(1);
3145 switch(popt->index) {
3146 case QEMU_OPTION_no_kvm_irqchip: {
3147 olist = qemu_find_opts("machine");
3148 qemu_opts_parse_noisily(olist, "kernel_irqchip=off", false);
3149 break;
3151 case QEMU_OPTION_cpu:
3152 /* hw initialization will check this */
3153 cpu_model = optarg;
3154 break;
3155 case QEMU_OPTION_hda:
3157 char buf[256];
3158 if (cyls == 0)
3159 snprintf(buf, sizeof(buf), "%s", HD_OPTS);
3160 else
3161 snprintf(buf, sizeof(buf),
3162 "%s,cyls=%d,heads=%d,secs=%d%s",
3163 HD_OPTS , cyls, heads, secs,
3164 translation == BIOS_ATA_TRANSLATION_LBA ?
3165 ",trans=lba" :
3166 translation == BIOS_ATA_TRANSLATION_NONE ?
3167 ",trans=none" : "");
3168 drive_add(IF_DEFAULT, 0, optarg, buf);
3169 break;
3171 case QEMU_OPTION_hdb:
3172 case QEMU_OPTION_hdc:
3173 case QEMU_OPTION_hdd:
3174 drive_add(IF_DEFAULT, popt->index - QEMU_OPTION_hda, optarg,
3175 HD_OPTS);
3176 break;
3177 case QEMU_OPTION_blockdev:
3179 Visitor *v;
3180 BlockdevOptions_queue *bdo;
3182 v = qobject_input_visitor_new_str(optarg, "driver", &err);
3183 if (!v) {
3184 error_report_err(err);
3185 exit(1);
3188 bdo = g_new(BlockdevOptions_queue, 1);
3189 visit_type_BlockdevOptions(v, NULL, &bdo->bdo,
3190 &error_fatal);
3191 visit_free(v);
3192 loc_save(&bdo->loc);
3193 QSIMPLEQ_INSERT_TAIL(&bdo_queue, bdo, entry);
3194 break;
3196 case QEMU_OPTION_drive:
3197 if (drive_def(optarg) == NULL) {
3198 exit(1);
3200 break;
3201 case QEMU_OPTION_set:
3202 if (qemu_set_option(optarg) != 0)
3203 exit(1);
3204 break;
3205 case QEMU_OPTION_global:
3206 if (qemu_global_option(optarg) != 0)
3207 exit(1);
3208 break;
3209 case QEMU_OPTION_mtdblock:
3210 drive_add(IF_MTD, -1, optarg, MTD_OPTS);
3211 break;
3212 case QEMU_OPTION_sd:
3213 drive_add(IF_SD, -1, optarg, SD_OPTS);
3214 break;
3215 case QEMU_OPTION_pflash:
3216 drive_add(IF_PFLASH, -1, optarg, PFLASH_OPTS);
3217 break;
3218 case QEMU_OPTION_snapshot:
3219 snapshot = 1;
3220 break;
3221 case QEMU_OPTION_hdachs:
3223 const char *p;
3224 p = optarg;
3225 cyls = strtol(p, (char **)&p, 0);
3226 if (cyls < 1 || cyls > 16383)
3227 goto chs_fail;
3228 if (*p != ',')
3229 goto chs_fail;
3230 p++;
3231 heads = strtol(p, (char **)&p, 0);
3232 if (heads < 1 || heads > 16)
3233 goto chs_fail;
3234 if (*p != ',')
3235 goto chs_fail;
3236 p++;
3237 secs = strtol(p, (char **)&p, 0);
3238 if (secs < 1 || secs > 63)
3239 goto chs_fail;
3240 if (*p == ',') {
3241 p++;
3242 if (!strcmp(p, "large")) {
3243 translation = BIOS_ATA_TRANSLATION_LARGE;
3244 } else if (!strcmp(p, "rechs")) {
3245 translation = BIOS_ATA_TRANSLATION_RECHS;
3246 } else if (!strcmp(p, "none")) {
3247 translation = BIOS_ATA_TRANSLATION_NONE;
3248 } else if (!strcmp(p, "lba")) {
3249 translation = BIOS_ATA_TRANSLATION_LBA;
3250 } else if (!strcmp(p, "auto")) {
3251 translation = BIOS_ATA_TRANSLATION_AUTO;
3252 } else {
3253 goto chs_fail;
3255 } else if (*p != '\0') {
3256 chs_fail:
3257 error_report("invalid physical CHS format");
3258 exit(1);
3260 if (hda_opts != NULL) {
3261 qemu_opt_set_number(hda_opts, "cyls", cyls,
3262 &error_abort);
3263 qemu_opt_set_number(hda_opts, "heads", heads,
3264 &error_abort);
3265 qemu_opt_set_number(hda_opts, "secs", secs,
3266 &error_abort);
3267 if (translation == BIOS_ATA_TRANSLATION_LARGE) {
3268 qemu_opt_set(hda_opts, "trans", "large",
3269 &error_abort);
3270 } else if (translation == BIOS_ATA_TRANSLATION_RECHS) {
3271 qemu_opt_set(hda_opts, "trans", "rechs",
3272 &error_abort);
3273 } else if (translation == BIOS_ATA_TRANSLATION_LBA) {
3274 qemu_opt_set(hda_opts, "trans", "lba",
3275 &error_abort);
3276 } else if (translation == BIOS_ATA_TRANSLATION_NONE) {
3277 qemu_opt_set(hda_opts, "trans", "none",
3278 &error_abort);
3282 break;
3283 case QEMU_OPTION_numa:
3284 opts = qemu_opts_parse_noisily(qemu_find_opts("numa"),
3285 optarg, true);
3286 if (!opts) {
3287 exit(1);
3289 break;
3290 case QEMU_OPTION_display:
3291 display_type = select_display(optarg);
3292 break;
3293 case QEMU_OPTION_nographic:
3294 olist = qemu_find_opts("machine");
3295 qemu_opts_parse_noisily(olist, "graphics=off", false);
3296 nographic = true;
3297 display_type = DT_NONE;
3298 break;
3299 case QEMU_OPTION_curses:
3300 #ifdef CONFIG_CURSES
3301 display_type = DT_CURSES;
3302 #else
3303 error_report("curses support is disabled");
3304 exit(1);
3305 #endif
3306 break;
3307 case QEMU_OPTION_portrait:
3308 graphic_rotate = 90;
3309 break;
3310 case QEMU_OPTION_rotate:
3311 graphic_rotate = strtol(optarg, (char **) &optarg, 10);
3312 if (graphic_rotate != 0 && graphic_rotate != 90 &&
3313 graphic_rotate != 180 && graphic_rotate != 270) {
3314 error_report("only 90, 180, 270 deg rotation is available");
3315 exit(1);
3317 break;
3318 case QEMU_OPTION_kernel:
3319 qemu_opts_set(qemu_find_opts("machine"), 0, "kernel", optarg,
3320 &error_abort);
3321 break;
3322 case QEMU_OPTION_initrd:
3323 qemu_opts_set(qemu_find_opts("machine"), 0, "initrd", optarg,
3324 &error_abort);
3325 break;
3326 case QEMU_OPTION_append:
3327 qemu_opts_set(qemu_find_opts("machine"), 0, "append", optarg,
3328 &error_abort);
3329 break;
3330 case QEMU_OPTION_dtb:
3331 qemu_opts_set(qemu_find_opts("machine"), 0, "dtb", optarg,
3332 &error_abort);
3333 break;
3334 case QEMU_OPTION_cdrom:
3335 drive_add(IF_DEFAULT, 2, optarg, CDROM_OPTS);
3336 break;
3337 case QEMU_OPTION_boot:
3338 opts = qemu_opts_parse_noisily(qemu_find_opts("boot-opts"),
3339 optarg, true);
3340 if (!opts) {
3341 exit(1);
3343 break;
3344 case QEMU_OPTION_fda:
3345 case QEMU_OPTION_fdb:
3346 drive_add(IF_FLOPPY, popt->index - QEMU_OPTION_fda,
3347 optarg, FD_OPTS);
3348 break;
3349 case QEMU_OPTION_no_fd_bootchk:
3350 fd_bootchk = 0;
3351 break;
3352 case QEMU_OPTION_netdev:
3353 default_net = 0;
3354 if (net_client_parse(qemu_find_opts("netdev"), optarg) == -1) {
3355 exit(1);
3357 break;
3358 case QEMU_OPTION_net:
3359 default_net = 0;
3360 if (net_client_parse(qemu_find_opts("net"), optarg) == -1) {
3361 exit(1);
3363 break;
3364 #ifdef CONFIG_LIBISCSI
3365 case QEMU_OPTION_iscsi:
3366 opts = qemu_opts_parse_noisily(qemu_find_opts("iscsi"),
3367 optarg, false);
3368 if (!opts) {
3369 exit(1);
3371 break;
3372 #endif
3373 #ifdef CONFIG_SLIRP
3374 case QEMU_OPTION_tftp:
3375 error_report("The -tftp option is deprecated. "
3376 "Please use '-netdev user,tftp=...' instead.");
3377 legacy_tftp_prefix = optarg;
3378 break;
3379 case QEMU_OPTION_bootp:
3380 error_report("The -bootp option is deprecated. "
3381 "Please use '-netdev user,bootfile=...' instead.");
3382 legacy_bootp_filename = optarg;
3383 break;
3384 case QEMU_OPTION_redir:
3385 error_report("The -redir option is deprecated. "
3386 "Please use '-netdev user,hostfwd=...' instead.");
3387 if (net_slirp_redir(optarg) < 0)
3388 exit(1);
3389 break;
3390 #endif
3391 case QEMU_OPTION_bt:
3392 add_device_config(DEV_BT, optarg);
3393 break;
3394 case QEMU_OPTION_audio_help:
3395 AUD_help ();
3396 exit (0);
3397 break;
3398 case QEMU_OPTION_soundhw:
3399 select_soundhw (optarg);
3400 break;
3401 case QEMU_OPTION_h:
3402 help(0);
3403 break;
3404 case QEMU_OPTION_version:
3405 version();
3406 exit(0);
3407 break;
3408 case QEMU_OPTION_m:
3409 opts = qemu_opts_parse_noisily(qemu_find_opts("memory"),
3410 optarg, true);
3411 if (!opts) {
3412 exit(EXIT_FAILURE);
3414 break;
3415 #ifdef CONFIG_TPM
3416 case QEMU_OPTION_tpmdev:
3417 if (tpm_config_parse(qemu_find_opts("tpmdev"), optarg) < 0) {
3418 exit(1);
3420 break;
3421 #endif
3422 case QEMU_OPTION_mempath:
3423 mem_path = optarg;
3424 break;
3425 case QEMU_OPTION_mem_prealloc:
3426 mem_prealloc = 1;
3427 break;
3428 case QEMU_OPTION_d:
3429 log_mask = optarg;
3430 break;
3431 case QEMU_OPTION_D:
3432 log_file = optarg;
3433 break;
3434 case QEMU_OPTION_DFILTER:
3435 qemu_set_dfilter_ranges(optarg, &error_fatal);
3436 break;
3437 case QEMU_OPTION_s:
3438 add_device_config(DEV_GDB, "tcp::" DEFAULT_GDBSTUB_PORT);
3439 break;
3440 case QEMU_OPTION_gdb:
3441 add_device_config(DEV_GDB, optarg);
3442 break;
3443 case QEMU_OPTION_L:
3444 if (is_help_option(optarg)) {
3445 list_data_dirs = true;
3446 } else if (data_dir_idx < ARRAY_SIZE(data_dir)) {
3447 data_dir[data_dir_idx++] = optarg;
3449 break;
3450 case QEMU_OPTION_bios:
3451 qemu_opts_set(qemu_find_opts("machine"), 0, "firmware", optarg,
3452 &error_abort);
3453 break;
3454 case QEMU_OPTION_singlestep:
3455 singlestep = 1;
3456 break;
3457 case QEMU_OPTION_S:
3458 autostart = 0;
3459 break;
3460 case QEMU_OPTION_k:
3461 keyboard_layout = optarg;
3462 break;
3463 case QEMU_OPTION_localtime:
3464 rtc_utc = 0;
3465 break;
3466 case QEMU_OPTION_vga:
3467 vga_model = optarg;
3468 default_vga = 0;
3469 break;
3470 case QEMU_OPTION_g:
3472 const char *p;
3473 int w, h, depth;
3474 p = optarg;
3475 w = strtol(p, (char **)&p, 10);
3476 if (w <= 0) {
3477 graphic_error:
3478 error_report("invalid resolution or depth");
3479 exit(1);
3481 if (*p != 'x')
3482 goto graphic_error;
3483 p++;
3484 h = strtol(p, (char **)&p, 10);
3485 if (h <= 0)
3486 goto graphic_error;
3487 if (*p == 'x') {
3488 p++;
3489 depth = strtol(p, (char **)&p, 10);
3490 if (depth != 8 && depth != 15 && depth != 16 &&
3491 depth != 24 && depth != 32)
3492 goto graphic_error;
3493 } else if (*p == '\0') {
3494 depth = graphic_depth;
3495 } else {
3496 goto graphic_error;
3499 graphic_width = w;
3500 graphic_height = h;
3501 graphic_depth = depth;
3503 break;
3504 case QEMU_OPTION_echr:
3506 char *r;
3507 term_escape_char = strtol(optarg, &r, 0);
3508 if (r == optarg)
3509 printf("Bad argument to echr\n");
3510 break;
3512 case QEMU_OPTION_monitor:
3513 default_monitor = 0;
3514 if (strncmp(optarg, "none", 4)) {
3515 monitor_parse(optarg, "readline", false);
3517 break;
3518 case QEMU_OPTION_qmp:
3519 monitor_parse(optarg, "control", false);
3520 default_monitor = 0;
3521 break;
3522 case QEMU_OPTION_qmp_pretty:
3523 monitor_parse(optarg, "control", true);
3524 default_monitor = 0;
3525 break;
3526 case QEMU_OPTION_mon:
3527 opts = qemu_opts_parse_noisily(qemu_find_opts("mon"), optarg,
3528 true);
3529 if (!opts) {
3530 exit(1);
3532 default_monitor = 0;
3533 break;
3534 case QEMU_OPTION_chardev:
3535 opts = qemu_opts_parse_noisily(qemu_find_opts("chardev"),
3536 optarg, true);
3537 if (!opts) {
3538 exit(1);
3540 break;
3541 case QEMU_OPTION_fsdev:
3542 olist = qemu_find_opts("fsdev");
3543 if (!olist) {
3544 error_report("fsdev support is disabled");
3545 exit(1);
3547 opts = qemu_opts_parse_noisily(olist, optarg, true);
3548 if (!opts) {
3549 exit(1);
3551 break;
3552 case QEMU_OPTION_virtfs: {
3553 QemuOpts *fsdev;
3554 QemuOpts *device;
3555 const char *writeout, *sock_fd, *socket;
3557 olist = qemu_find_opts("virtfs");
3558 if (!olist) {
3559 error_report("virtfs support is disabled");
3560 exit(1);
3562 opts = qemu_opts_parse_noisily(olist, optarg, true);
3563 if (!opts) {
3564 exit(1);
3567 if (qemu_opt_get(opts, "fsdriver") == NULL ||
3568 qemu_opt_get(opts, "mount_tag") == NULL) {
3569 error_report("Usage: -virtfs fsdriver,mount_tag=tag");
3570 exit(1);
3572 fsdev = qemu_opts_create(qemu_find_opts("fsdev"),
3573 qemu_opt_get(opts, "mount_tag"),
3574 1, NULL);
3575 if (!fsdev) {
3576 error_report("duplicate fsdev id: %s",
3577 qemu_opt_get(opts, "mount_tag"));
3578 exit(1);
3581 writeout = qemu_opt_get(opts, "writeout");
3582 if (writeout) {
3583 #ifdef CONFIG_SYNC_FILE_RANGE
3584 qemu_opt_set(fsdev, "writeout", writeout, &error_abort);
3585 #else
3586 error_report("writeout=immediate not supported "
3587 "on this platform");
3588 exit(1);
3589 #endif
3591 qemu_opt_set(fsdev, "fsdriver",
3592 qemu_opt_get(opts, "fsdriver"), &error_abort);
3593 qemu_opt_set(fsdev, "path", qemu_opt_get(opts, "path"),
3594 &error_abort);
3595 qemu_opt_set(fsdev, "security_model",
3596 qemu_opt_get(opts, "security_model"),
3597 &error_abort);
3598 socket = qemu_opt_get(opts, "socket");
3599 if (socket) {
3600 qemu_opt_set(fsdev, "socket", socket, &error_abort);
3602 sock_fd = qemu_opt_get(opts, "sock_fd");
3603 if (sock_fd) {
3604 qemu_opt_set(fsdev, "sock_fd", sock_fd, &error_abort);
3607 qemu_opt_set_bool(fsdev, "readonly",
3608 qemu_opt_get_bool(opts, "readonly", 0),
3609 &error_abort);
3610 device = qemu_opts_create(qemu_find_opts("device"), NULL, 0,
3611 &error_abort);
3612 qemu_opt_set(device, "driver", "virtio-9p-pci", &error_abort);
3613 qemu_opt_set(device, "fsdev",
3614 qemu_opt_get(opts, "mount_tag"), &error_abort);
3615 qemu_opt_set(device, "mount_tag",
3616 qemu_opt_get(opts, "mount_tag"), &error_abort);
3617 break;
3619 case QEMU_OPTION_virtfs_synth: {
3620 QemuOpts *fsdev;
3621 QemuOpts *device;
3623 fsdev = qemu_opts_create(qemu_find_opts("fsdev"), "v_synth",
3624 1, NULL);
3625 if (!fsdev) {
3626 error_report("duplicate option: %s", "virtfs_synth");
3627 exit(1);
3629 qemu_opt_set(fsdev, "fsdriver", "synth", &error_abort);
3631 device = qemu_opts_create(qemu_find_opts("device"), NULL, 0,
3632 &error_abort);
3633 qemu_opt_set(device, "driver", "virtio-9p-pci", &error_abort);
3634 qemu_opt_set(device, "fsdev", "v_synth", &error_abort);
3635 qemu_opt_set(device, "mount_tag", "v_synth", &error_abort);
3636 break;
3638 case QEMU_OPTION_serial:
3639 add_device_config(DEV_SERIAL, optarg);
3640 default_serial = 0;
3641 if (strncmp(optarg, "mon:", 4) == 0) {
3642 default_monitor = 0;
3644 break;
3645 case QEMU_OPTION_watchdog:
3646 if (watchdog) {
3647 error_report("only one watchdog option may be given");
3648 return 1;
3650 watchdog = optarg;
3651 break;
3652 case QEMU_OPTION_watchdog_action:
3653 if (select_watchdog_action(optarg) == -1) {
3654 error_report("unknown -watchdog-action parameter");
3655 exit(1);
3657 break;
3658 case QEMU_OPTION_virtiocon:
3659 add_device_config(DEV_VIRTCON, optarg);
3660 default_virtcon = 0;
3661 if (strncmp(optarg, "mon:", 4) == 0) {
3662 default_monitor = 0;
3664 break;
3665 case QEMU_OPTION_parallel:
3666 add_device_config(DEV_PARALLEL, optarg);
3667 default_parallel = 0;
3668 if (strncmp(optarg, "mon:", 4) == 0) {
3669 default_monitor = 0;
3671 break;
3672 case QEMU_OPTION_debugcon:
3673 add_device_config(DEV_DEBUGCON, optarg);
3674 break;
3675 case QEMU_OPTION_loadvm:
3676 loadvm = optarg;
3677 break;
3678 case QEMU_OPTION_full_screen:
3679 full_screen = 1;
3680 break;
3681 case QEMU_OPTION_no_frame:
3682 no_frame = 1;
3683 break;
3684 case QEMU_OPTION_alt_grab:
3685 alt_grab = 1;
3686 break;
3687 case QEMU_OPTION_ctrl_grab:
3688 ctrl_grab = 1;
3689 break;
3690 case QEMU_OPTION_no_quit:
3691 no_quit = 1;
3692 break;
3693 case QEMU_OPTION_sdl:
3694 #ifdef CONFIG_SDL
3695 display_type = DT_SDL;
3696 break;
3697 #else
3698 error_report("SDL support is disabled");
3699 exit(1);
3700 #endif
3701 case QEMU_OPTION_pidfile:
3702 pid_file = optarg;
3703 break;
3704 case QEMU_OPTION_win2k_hack:
3705 win2k_install_hack = 1;
3706 break;
3707 case QEMU_OPTION_rtc_td_hack: {
3708 static GlobalProperty slew_lost_ticks = {
3709 .driver = "mc146818rtc",
3710 .property = "lost_tick_policy",
3711 .value = "slew",
3714 qdev_prop_register_global(&slew_lost_ticks);
3715 break;
3717 case QEMU_OPTION_acpitable:
3718 opts = qemu_opts_parse_noisily(qemu_find_opts("acpi"),
3719 optarg, true);
3720 if (!opts) {
3721 exit(1);
3723 acpi_table_add(opts, &error_fatal);
3724 break;
3725 case QEMU_OPTION_smbios:
3726 opts = qemu_opts_parse_noisily(qemu_find_opts("smbios"),
3727 optarg, false);
3728 if (!opts) {
3729 exit(1);
3731 smbios_entry_add(opts, &error_fatal);
3732 break;
3733 case QEMU_OPTION_fwcfg:
3734 opts = qemu_opts_parse_noisily(qemu_find_opts("fw_cfg"),
3735 optarg, true);
3736 if (opts == NULL) {
3737 exit(1);
3739 break;
3740 case QEMU_OPTION_enable_kvm:
3741 olist = qemu_find_opts("machine");
3742 qemu_opts_parse_noisily(olist, "accel=kvm", false);
3743 break;
3744 case QEMU_OPTION_enable_hax:
3745 olist = qemu_find_opts("machine");
3746 qemu_opts_parse_noisily(olist, "accel=hax", false);
3747 break;
3748 case QEMU_OPTION_M:
3749 case QEMU_OPTION_machine:
3750 olist = qemu_find_opts("machine");
3751 opts = qemu_opts_parse_noisily(olist, optarg, true);
3752 if (!opts) {
3753 exit(1);
3755 break;
3756 case QEMU_OPTION_no_kvm:
3757 olist = qemu_find_opts("machine");
3758 qemu_opts_parse_noisily(olist, "accel=tcg", false);
3759 break;
3760 case QEMU_OPTION_no_kvm_pit: {
3761 error_report("warning: ignoring deprecated option");
3762 break;
3764 case QEMU_OPTION_no_kvm_pit_reinjection: {
3765 static GlobalProperty kvm_pit_lost_tick_policy = {
3766 .driver = "kvm-pit",
3767 .property = "lost_tick_policy",
3768 .value = "discard",
3771 error_report("warning: deprecated, replaced by "
3772 "-global kvm-pit.lost_tick_policy=discard");
3773 qdev_prop_register_global(&kvm_pit_lost_tick_policy);
3774 break;
3776 case QEMU_OPTION_accel:
3777 accel_opts = qemu_opts_parse_noisily(qemu_find_opts("accel"),
3778 optarg, true);
3779 optarg = qemu_opt_get(accel_opts, "accel");
3781 olist = qemu_find_opts("machine");
3782 if (strcmp("kvm", optarg) == 0) {
3783 qemu_opts_parse_noisily(olist, "accel=kvm", false);
3784 } else if (strcmp("xen", optarg) == 0) {
3785 qemu_opts_parse_noisily(olist, "accel=xen", false);
3786 } else if (strcmp("tcg", optarg) == 0) {
3787 qemu_opts_parse_noisily(olist, "accel=tcg", false);
3788 } else {
3789 if (!is_help_option(optarg)) {
3790 error_printf("Unknown accelerator: %s", optarg);
3792 error_printf("Supported accelerators: kvm, xen, tcg\n");
3793 exit(1);
3795 break;
3796 case QEMU_OPTION_usb:
3797 olist = qemu_find_opts("machine");
3798 qemu_opts_parse_noisily(olist, "usb=on", false);
3799 break;
3800 case QEMU_OPTION_usbdevice:
3801 olist = qemu_find_opts("machine");
3802 qemu_opts_parse_noisily(olist, "usb=on", false);
3803 add_device_config(DEV_USB, optarg);
3804 break;
3805 case QEMU_OPTION_device:
3806 if (!qemu_opts_parse_noisily(qemu_find_opts("device"),
3807 optarg, true)) {
3808 exit(1);
3810 break;
3811 case QEMU_OPTION_smp:
3812 if (!qemu_opts_parse_noisily(qemu_find_opts("smp-opts"),
3813 optarg, true)) {
3814 exit(1);
3816 break;
3817 case QEMU_OPTION_vnc:
3818 vnc_parse(optarg, &error_fatal);
3819 break;
3820 case QEMU_OPTION_no_acpi:
3821 acpi_enabled = 0;
3822 break;
3823 case QEMU_OPTION_no_hpet:
3824 no_hpet = 1;
3825 break;
3826 case QEMU_OPTION_balloon:
3827 if (balloon_parse(optarg) < 0) {
3828 error_report("unknown -balloon argument %s", optarg);
3829 exit(1);
3831 break;
3832 case QEMU_OPTION_no_reboot:
3833 no_reboot = 1;
3834 break;
3835 case QEMU_OPTION_no_shutdown:
3836 no_shutdown = 1;
3837 break;
3838 case QEMU_OPTION_show_cursor:
3839 cursor_hide = 0;
3840 break;
3841 case QEMU_OPTION_uuid:
3842 if (qemu_uuid_parse(optarg, &qemu_uuid) < 0) {
3843 error_report("failed to parse UUID string: wrong format");
3844 exit(1);
3846 qemu_uuid_set = true;
3847 break;
3848 case QEMU_OPTION_option_rom:
3849 if (nb_option_roms >= MAX_OPTION_ROMS) {
3850 error_report("too many option ROMs");
3851 exit(1);
3853 opts = qemu_opts_parse_noisily(qemu_find_opts("option-rom"),
3854 optarg, true);
3855 if (!opts) {
3856 exit(1);
3858 option_rom[nb_option_roms].name = qemu_opt_get(opts, "romfile");
3859 option_rom[nb_option_roms].bootindex =
3860 qemu_opt_get_number(opts, "bootindex", -1);
3861 if (!option_rom[nb_option_roms].name) {
3862 error_report("Option ROM file is not specified");
3863 exit(1);
3865 nb_option_roms++;
3866 break;
3867 case QEMU_OPTION_semihosting:
3868 semihosting.enabled = true;
3869 semihosting.target = SEMIHOSTING_TARGET_AUTO;
3870 break;
3871 case QEMU_OPTION_semihosting_config:
3872 semihosting.enabled = true;
3873 opts = qemu_opts_parse_noisily(qemu_find_opts("semihosting-config"),
3874 optarg, false);
3875 if (opts != NULL) {
3876 semihosting.enabled = qemu_opt_get_bool(opts, "enable",
3877 true);
3878 const char *target = qemu_opt_get(opts, "target");
3879 if (target != NULL) {
3880 if (strcmp("native", target) == 0) {
3881 semihosting.target = SEMIHOSTING_TARGET_NATIVE;
3882 } else if (strcmp("gdb", target) == 0) {
3883 semihosting.target = SEMIHOSTING_TARGET_GDB;
3884 } else if (strcmp("auto", target) == 0) {
3885 semihosting.target = SEMIHOSTING_TARGET_AUTO;
3886 } else {
3887 error_report("unsupported semihosting-config %s",
3888 optarg);
3889 exit(1);
3891 } else {
3892 semihosting.target = SEMIHOSTING_TARGET_AUTO;
3894 /* Set semihosting argument count and vector */
3895 qemu_opt_foreach(opts, add_semihosting_arg,
3896 &semihosting, NULL);
3897 } else {
3898 error_report("unsupported semihosting-config %s", optarg);
3899 exit(1);
3901 break;
3902 case QEMU_OPTION_tdf:
3903 error_report("warning: ignoring deprecated option");
3904 break;
3905 case QEMU_OPTION_name:
3906 opts = qemu_opts_parse_noisily(qemu_find_opts("name"),
3907 optarg, true);
3908 if (!opts) {
3909 exit(1);
3911 break;
3912 case QEMU_OPTION_prom_env:
3913 if (nb_prom_envs >= MAX_PROM_ENVS) {
3914 error_report("too many prom variables");
3915 exit(1);
3917 prom_envs[nb_prom_envs] = optarg;
3918 nb_prom_envs++;
3919 break;
3920 case QEMU_OPTION_old_param:
3921 old_param = 1;
3922 break;
3923 case QEMU_OPTION_clock:
3924 /* Clock options no longer exist. Keep this option for
3925 * backward compatibility.
3927 break;
3928 case QEMU_OPTION_startdate:
3929 configure_rtc_date_offset(optarg, 1);
3930 break;
3931 case QEMU_OPTION_rtc:
3932 opts = qemu_opts_parse_noisily(qemu_find_opts("rtc"), optarg,
3933 false);
3934 if (!opts) {
3935 exit(1);
3937 configure_rtc(opts);
3938 break;
3939 case QEMU_OPTION_tb_size:
3940 tcg_tb_size = strtol(optarg, NULL, 0);
3941 if (tcg_tb_size < 0) {
3942 tcg_tb_size = 0;
3944 break;
3945 case QEMU_OPTION_icount:
3946 icount_opts = qemu_opts_parse_noisily(qemu_find_opts("icount"),
3947 optarg, true);
3948 if (!icount_opts) {
3949 exit(1);
3951 break;
3952 case QEMU_OPTION_incoming:
3953 if (!incoming) {
3954 runstate_set(RUN_STATE_INMIGRATE);
3956 incoming = optarg;
3957 break;
3958 case QEMU_OPTION_only_migratable:
3959 only_migratable = 1;
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 qemu_tcg_configure(accel_opts, &error_fatal);
4109 machine_class = select_machine();
4111 set_memory_options(&ram_slots, &maxram_size, machine_class);
4113 os_daemonize();
4115 if (pid_file && qemu_create_pidfile(pid_file) != 0) {
4116 error_report("could not acquire pid file: %s", strerror(errno));
4117 exit(1);
4120 if (qemu_init_main_loop(&main_loop_err)) {
4121 error_report_err(main_loop_err);
4122 exit(1);
4125 if (qemu_opts_foreach(qemu_find_opts("sandbox"),
4126 parse_sandbox, NULL, NULL)) {
4127 exit(1);
4130 if (qemu_opts_foreach(qemu_find_opts("name"),
4131 parse_name, NULL, NULL)) {
4132 exit(1);
4135 #ifndef _WIN32
4136 if (qemu_opts_foreach(qemu_find_opts("add-fd"),
4137 parse_add_fd, NULL, NULL)) {
4138 exit(1);
4141 if (qemu_opts_foreach(qemu_find_opts("add-fd"),
4142 cleanup_add_fd, NULL, NULL)) {
4143 exit(1);
4145 #endif
4147 current_machine = MACHINE(object_new(object_class_get_name(
4148 OBJECT_CLASS(machine_class))));
4149 if (machine_help_func(qemu_get_machine_opts(), current_machine)) {
4150 exit(0);
4152 object_property_add_child(object_get_root(), "machine",
4153 OBJECT(current_machine), &error_abort);
4155 if (machine_class->minimum_page_bits) {
4156 if (!set_preferred_target_page_bits(machine_class->minimum_page_bits)) {
4157 /* This would be a board error: specifying a minimum smaller than
4158 * a target's compile-time fixed setting.
4160 g_assert_not_reached();
4164 cpu_exec_init_all();
4166 if (machine_class->hw_version) {
4167 qemu_set_hw_version(machine_class->hw_version);
4170 if (cpu_model && is_help_option(cpu_model)) {
4171 list_cpus(stdout, &fprintf, cpu_model);
4172 exit(0);
4175 if (!trace_init_backends()) {
4176 exit(1);
4178 trace_init_file(trace_file);
4180 /* Open the logfile at this point and set the log mask if necessary.
4182 if (log_file) {
4183 qemu_set_log_filename(log_file, &error_fatal);
4186 if (log_mask) {
4187 int mask;
4188 mask = qemu_str_to_log_mask(log_mask);
4189 if (!mask) {
4190 qemu_print_log_usage(stdout);
4191 exit(1);
4193 qemu_set_log(mask);
4194 } else {
4195 qemu_set_log(0);
4198 /* If no data_dir is specified then try to find it relative to the
4199 executable path. */
4200 if (data_dir_idx < ARRAY_SIZE(data_dir)) {
4201 data_dir[data_dir_idx] = os_find_datadir();
4202 if (data_dir[data_dir_idx] != NULL) {
4203 data_dir_idx++;
4206 /* If all else fails use the install path specified when building. */
4207 if (data_dir_idx < ARRAY_SIZE(data_dir)) {
4208 data_dir[data_dir_idx++] = CONFIG_QEMU_DATADIR;
4211 /* -L help lists the data directories and exits. */
4212 if (list_data_dirs) {
4213 for (i = 0; i < data_dir_idx; i++) {
4214 printf("%s\n", data_dir[i]);
4216 exit(0);
4219 smp_parse(qemu_opts_find(qemu_find_opts("smp-opts"), NULL));
4221 machine_class->max_cpus = machine_class->max_cpus ?: 1; /* Default to UP */
4222 if (max_cpus > machine_class->max_cpus) {
4223 error_report("Number of SMP CPUs requested (%d) exceeds max CPUs "
4224 "supported by machine '%s' (%d)", max_cpus,
4225 machine_class->name, machine_class->max_cpus);
4226 exit(1);
4230 * Get the default machine options from the machine if it is not already
4231 * specified either by the configuration file or by the command line.
4233 if (machine_class->default_machine_opts) {
4234 qemu_opts_set_defaults(qemu_find_opts("machine"),
4235 machine_class->default_machine_opts, 0);
4238 qemu_opts_foreach(qemu_find_opts("device"),
4239 default_driver_check, NULL, NULL);
4240 qemu_opts_foreach(qemu_find_opts("global"),
4241 default_driver_check, NULL, NULL);
4243 if (!vga_model && !default_vga) {
4244 vga_interface_type = VGA_DEVICE;
4246 if (!has_defaults || machine_class->no_serial) {
4247 default_serial = 0;
4249 if (!has_defaults || machine_class->no_parallel) {
4250 default_parallel = 0;
4252 if (!has_defaults || !machine_class->use_virtcon) {
4253 default_virtcon = 0;
4255 if (!has_defaults || !machine_class->use_sclp) {
4256 default_sclp = 0;
4258 if (!has_defaults || machine_class->no_floppy) {
4259 default_floppy = 0;
4261 if (!has_defaults || machine_class->no_cdrom) {
4262 default_cdrom = 0;
4264 if (!has_defaults || machine_class->no_sdcard) {
4265 default_sdcard = 0;
4267 if (!has_defaults) {
4268 default_monitor = 0;
4269 default_net = 0;
4270 default_vga = 0;
4273 if (is_daemonized()) {
4274 /* According to documentation and historically, -nographic redirects
4275 * serial port, parallel port and monitor to stdio, which does not work
4276 * with -daemonize. We can redirect these to null instead, but since
4277 * -nographic is legacy, let's just error out.
4278 * We disallow -nographic only if all other ports are not redirected
4279 * explicitly, to not break existing legacy setups which uses
4280 * -nographic _and_ redirects all ports explicitly - this is valid
4281 * usage, -nographic is just a no-op in this case.
4283 if (nographic
4284 && (default_parallel || default_serial
4285 || default_monitor || default_virtcon)) {
4286 error_report("-nographic cannot be used with -daemonize");
4287 exit(1);
4289 #ifdef CONFIG_CURSES
4290 if (display_type == DT_CURSES) {
4291 error_report("curses display cannot be used with -daemonize");
4292 exit(1);
4294 #endif
4297 if (nographic) {
4298 if (default_parallel)
4299 add_device_config(DEV_PARALLEL, "null");
4300 if (default_serial && default_monitor) {
4301 add_device_config(DEV_SERIAL, "mon:stdio");
4302 } else if (default_virtcon && default_monitor) {
4303 add_device_config(DEV_VIRTCON, "mon:stdio");
4304 } else if (default_sclp && default_monitor) {
4305 add_device_config(DEV_SCLP, "mon:stdio");
4306 } else {
4307 if (default_serial)
4308 add_device_config(DEV_SERIAL, "stdio");
4309 if (default_virtcon)
4310 add_device_config(DEV_VIRTCON, "stdio");
4311 if (default_sclp) {
4312 add_device_config(DEV_SCLP, "stdio");
4314 if (default_monitor)
4315 monitor_parse("stdio", "readline", false);
4317 } else {
4318 if (default_serial)
4319 add_device_config(DEV_SERIAL, "vc:80Cx24C");
4320 if (default_parallel)
4321 add_device_config(DEV_PARALLEL, "vc:80Cx24C");
4322 if (default_monitor)
4323 monitor_parse("vc:80Cx24C", "readline", false);
4324 if (default_virtcon)
4325 add_device_config(DEV_VIRTCON, "vc:80Cx24C");
4326 if (default_sclp) {
4327 add_device_config(DEV_SCLP, "vc:80Cx24C");
4331 #if defined(CONFIG_VNC)
4332 if (!QTAILQ_EMPTY(&(qemu_find_opts("vnc")->head))) {
4333 display_remote++;
4335 #endif
4336 if (display_type == DT_DEFAULT && !display_remote) {
4337 #if defined(CONFIG_GTK)
4338 display_type = DT_GTK;
4339 #elif defined(CONFIG_SDL)
4340 display_type = DT_SDL;
4341 #elif defined(CONFIG_COCOA)
4342 display_type = DT_COCOA;
4343 #elif defined(CONFIG_VNC)
4344 vnc_parse("localhost:0,to=99,id=default", &error_abort);
4345 #else
4346 display_type = DT_NONE;
4347 #endif
4350 if ((no_frame || alt_grab || ctrl_grab) && display_type != DT_SDL) {
4351 error_report("-no-frame, -alt-grab and -ctrl-grab are only valid "
4352 "for SDL, ignoring option");
4354 if (no_quit && (display_type != DT_GTK && display_type != DT_SDL)) {
4355 error_report("-no-quit is only valid for GTK and SDL, "
4356 "ignoring option");
4359 if (display_type == DT_GTK) {
4360 early_gtk_display_init(request_opengl);
4363 if (display_type == DT_SDL) {
4364 sdl_display_early_init(request_opengl);
4367 qemu_console_early_init();
4369 if (request_opengl == 1 && display_opengl == 0) {
4370 #if defined(CONFIG_OPENGL)
4371 error_report("OpenGL is not supported by the display");
4372 #else
4373 error_report("OpenGL support is disabled");
4374 #endif
4375 exit(1);
4378 page_size_init();
4379 socket_init();
4381 if (qemu_opts_foreach(qemu_find_opts("object"),
4382 user_creatable_add_opts_foreach,
4383 object_create_initial, NULL)) {
4384 exit(1);
4387 if (qemu_opts_foreach(qemu_find_opts("chardev"),
4388 chardev_init_func, NULL, NULL)) {
4389 exit(1);
4392 #ifdef CONFIG_VIRTFS
4393 if (qemu_opts_foreach(qemu_find_opts("fsdev"),
4394 fsdev_init_func, NULL, NULL)) {
4395 exit(1);
4397 #endif
4399 if (qemu_opts_foreach(qemu_find_opts("device"),
4400 device_help_func, NULL, NULL)) {
4401 exit(0);
4404 machine_opts = qemu_get_machine_opts();
4405 if (qemu_opt_foreach(machine_opts, machine_set_property, current_machine,
4406 NULL)) {
4407 object_unref(OBJECT(current_machine));
4408 exit(1);
4411 configure_accelerator(current_machine);
4413 if (qtest_chrdev) {
4414 qtest_init(qtest_chrdev, qtest_log, &error_fatal);
4417 machine_opts = qemu_get_machine_opts();
4418 kernel_filename = qemu_opt_get(machine_opts, "kernel");
4419 initrd_filename = qemu_opt_get(machine_opts, "initrd");
4420 kernel_cmdline = qemu_opt_get(machine_opts, "append");
4421 bios_name = qemu_opt_get(machine_opts, "firmware");
4423 opts = qemu_opts_find(qemu_find_opts("boot-opts"), NULL);
4424 if (opts) {
4425 boot_order = qemu_opt_get(opts, "order");
4426 if (boot_order) {
4427 validate_bootdevices(boot_order, &error_fatal);
4430 boot_once = qemu_opt_get(opts, "once");
4431 if (boot_once) {
4432 validate_bootdevices(boot_once, &error_fatal);
4435 boot_menu = qemu_opt_get_bool(opts, "menu", boot_menu);
4436 boot_strict = qemu_opt_get_bool(opts, "strict", false);
4439 if (!boot_order) {
4440 boot_order = machine_class->default_boot_order;
4443 if (!kernel_cmdline) {
4444 kernel_cmdline = "";
4445 current_machine->kernel_cmdline = (char *)kernel_cmdline;
4448 linux_boot = (kernel_filename != NULL);
4450 if (!linux_boot && *kernel_cmdline != '\0') {
4451 error_report("-append only allowed with -kernel option");
4452 exit(1);
4455 if (!linux_boot && initrd_filename != NULL) {
4456 error_report("-initrd only allowed with -kernel option");
4457 exit(1);
4460 if (semihosting_enabled() && !semihosting_get_argc() && kernel_filename) {
4461 /* fall back to the -kernel/-append */
4462 semihosting_arg_fallback(kernel_filename, kernel_cmdline);
4465 os_set_line_buffering();
4467 /* spice needs the timers to be initialized by this point */
4468 qemu_spice_init();
4470 cpu_ticks_init();
4471 if (icount_opts) {
4472 if (!tcg_enabled()) {
4473 error_report("-icount is not allowed with hardware virtualization");
4474 exit(1);
4475 } else if (qemu_tcg_mttcg_enabled()) {
4476 error_report("-icount does not currently work with MTTCG");
4477 exit(1);
4479 configure_icount(icount_opts, &error_abort);
4480 qemu_opts_del(icount_opts);
4483 if (default_net) {
4484 QemuOptsList *net = qemu_find_opts("net");
4485 qemu_opts_set(net, NULL, "type", "nic", &error_abort);
4486 #ifdef CONFIG_SLIRP
4487 qemu_opts_set(net, NULL, "type", "user", &error_abort);
4488 #endif
4491 colo_info_init();
4493 if (net_init_clients() < 0) {
4494 exit(1);
4497 if (qemu_opts_foreach(qemu_find_opts("object"),
4498 user_creatable_add_opts_foreach,
4499 object_create_delayed, NULL)) {
4500 exit(1);
4503 #ifdef CONFIG_TPM
4504 if (tpm_init() < 0) {
4505 exit(1);
4507 #endif
4509 /* init the bluetooth world */
4510 if (foreach_device_config(DEV_BT, bt_parse))
4511 exit(1);
4513 if (!xen_enabled()) {
4514 /* On 32-bit hosts, QEMU is limited by virtual address space */
4515 if (ram_size > (2047 << 20) && HOST_LONG_BITS == 32) {
4516 error_report("at most 2047 MB RAM can be simulated");
4517 exit(1);
4521 blk_mig_init();
4522 ram_mig_init();
4524 /* If the currently selected machine wishes to override the units-per-bus
4525 * property of its default HBA interface type, do so now. */
4526 if (machine_class->units_per_default_bus) {
4527 override_max_devs(machine_class->block_default_type,
4528 machine_class->units_per_default_bus);
4531 /* open the virtual block devices */
4532 while (!QSIMPLEQ_EMPTY(&bdo_queue)) {
4533 BlockdevOptions_queue *bdo = QSIMPLEQ_FIRST(&bdo_queue);
4535 QSIMPLEQ_REMOVE_HEAD(&bdo_queue, entry);
4536 loc_push_restore(&bdo->loc);
4537 qmp_blockdev_add(bdo->bdo, &error_fatal);
4538 loc_pop(&bdo->loc);
4539 qapi_free_BlockdevOptions(bdo->bdo);
4540 g_free(bdo);
4542 if (snapshot || replay_mode != REPLAY_MODE_NONE) {
4543 qemu_opts_foreach(qemu_find_opts("drive"), drive_enable_snapshot,
4544 NULL, NULL);
4546 if (qemu_opts_foreach(qemu_find_opts("drive"), drive_init_func,
4547 &machine_class->block_default_type, NULL)) {
4548 exit(1);
4551 default_drive(default_cdrom, snapshot, machine_class->block_default_type, 2,
4552 CDROM_OPTS);
4553 default_drive(default_floppy, snapshot, IF_FLOPPY, 0, FD_OPTS);
4554 default_drive(default_sdcard, snapshot, IF_SD, 0, SD_OPTS);
4556 parse_numa_opts(machine_class);
4558 if (qemu_opts_foreach(qemu_find_opts("mon"),
4559 mon_init_func, NULL, NULL)) {
4560 exit(1);
4563 if (foreach_device_config(DEV_SERIAL, serial_parse) < 0)
4564 exit(1);
4565 if (foreach_device_config(DEV_PARALLEL, parallel_parse) < 0)
4566 exit(1);
4567 if (foreach_device_config(DEV_VIRTCON, virtcon_parse) < 0)
4568 exit(1);
4569 if (foreach_device_config(DEV_SCLP, sclp_parse) < 0) {
4570 exit(1);
4572 if (foreach_device_config(DEV_DEBUGCON, debugcon_parse) < 0)
4573 exit(1);
4575 /* If no default VGA is requested, the default is "none". */
4576 if (default_vga) {
4577 if (machine_class->default_display) {
4578 vga_model = machine_class->default_display;
4579 } else if (vga_interface_available(VGA_CIRRUS)) {
4580 vga_model = "cirrus";
4581 } else if (vga_interface_available(VGA_STD)) {
4582 vga_model = "std";
4585 if (vga_model) {
4586 select_vgahw(vga_model);
4589 if (watchdog) {
4590 i = select_watchdog(watchdog);
4591 if (i > 0)
4592 exit (i == 1 ? 1 : 0);
4595 machine_register_compat_props(current_machine);
4597 qemu_opts_foreach(qemu_find_opts("global"),
4598 global_init_func, NULL, NULL);
4600 /* This checkpoint is required by replay to separate prior clock
4601 reading from the other reads, because timer polling functions query
4602 clock values from the log. */
4603 replay_checkpoint(CHECKPOINT_INIT);
4604 qdev_machine_init();
4606 current_machine->ram_size = ram_size;
4607 current_machine->maxram_size = maxram_size;
4608 current_machine->ram_slots = ram_slots;
4609 current_machine->boot_order = boot_order;
4610 current_machine->cpu_model = cpu_model;
4612 machine_class->init(current_machine);
4614 realtime_init();
4616 audio_init();
4618 if (hax_enabled()) {
4619 hax_sync_vcpus();
4622 if (qemu_opts_foreach(qemu_find_opts("fw_cfg"),
4623 parse_fw_cfg, fw_cfg_find(), NULL) != 0) {
4624 exit(1);
4627 /* init USB devices */
4628 if (machine_usb(current_machine)) {
4629 if (foreach_device_config(DEV_USB, usb_parse) < 0)
4630 exit(1);
4633 /* Check if IGD GFX passthrough. */
4634 igd_gfx_passthru();
4636 /* init generic devices */
4637 rom_set_order_override(FW_CFG_ORDER_OVERRIDE_DEVICE);
4638 if (qemu_opts_foreach(qemu_find_opts("device"),
4639 device_init_func, NULL, NULL)) {
4640 exit(1);
4643 cpu_synchronize_all_post_init();
4645 numa_post_machine_init();
4647 rom_reset_order_override();
4650 * Create frontends for -drive if=scsi leftovers.
4651 * Normally, frontends for -drive get created by machine
4652 * initialization for onboard SCSI HBAs. However, we create a few
4653 * more ever since SCSI qdevification, but this is pretty much an
4654 * implementation accident, and deprecated.
4656 scsi_legacy_handle_cmdline();
4658 /* Did we create any drives that we failed to create a device for? */
4659 drive_check_orphaned();
4661 /* Don't warn about the default network setup that you get if
4662 * no command line -net or -netdev options are specified. There
4663 * are two cases that we would otherwise complain about:
4664 * (1) board doesn't support a NIC but the implicit "-net nic"
4665 * requested one
4666 * (2) CONFIG_SLIRP not set, in which case the implicit "-net nic"
4667 * sets up a nic that isn't connected to anything.
4669 if (!default_net) {
4670 net_check_clients();
4674 if (boot_once) {
4675 qemu_boot_set(boot_once, &error_fatal);
4676 qemu_register_reset(restore_boot_order, g_strdup(boot_order));
4679 ds = init_displaystate();
4681 /* init local displays */
4682 switch (display_type) {
4683 case DT_CURSES:
4684 curses_display_init(ds, full_screen);
4685 break;
4686 case DT_SDL:
4687 sdl_display_init(ds, full_screen, no_frame);
4688 break;
4689 case DT_COCOA:
4690 cocoa_display_init(ds, full_screen);
4691 break;
4692 case DT_GTK:
4693 gtk_display_init(ds, full_screen, grab_on_hover);
4694 break;
4695 default:
4696 break;
4699 /* must be after terminal init, SDL library changes signal handlers */
4700 os_setup_signal_handling();
4702 /* init remote displays */
4703 #ifdef CONFIG_VNC
4704 qemu_opts_foreach(qemu_find_opts("vnc"),
4705 vnc_init_func, NULL, NULL);
4706 #endif
4708 if (using_spice) {
4709 qemu_spice_display_init();
4712 if (foreach_device_config(DEV_GDB, gdbserver_start) < 0) {
4713 exit(1);
4716 qdev_machine_creation_done();
4718 /* TODO: once all bus devices are qdevified, this should be done
4719 * when bus is created by qdev.c */
4720 qemu_register_reset(qbus_reset_all_fn, sysbus_get_default());
4721 qemu_run_machine_init_done_notifiers();
4723 if (rom_check_and_register_reset() != 0) {
4724 error_report("rom check and register reset failed");
4725 exit(1);
4728 replay_start();
4730 /* This checkpoint is required by replay to separate prior clock
4731 reading from the other reads, because timer polling functions query
4732 clock values from the log. */
4733 replay_checkpoint(CHECKPOINT_RESET);
4734 qemu_system_reset(VMRESET_SILENT);
4735 register_global_state();
4736 if (replay_mode != REPLAY_MODE_NONE) {
4737 replay_vmstate_init();
4738 } else if (loadvm) {
4739 if (load_vmstate(loadvm) < 0) {
4740 autostart = 0;
4744 qdev_prop_check_globals();
4745 if (vmstate_dump_file) {
4746 /* dump and exit */
4747 dump_vmstate_json_to_file(vmstate_dump_file);
4748 return 0;
4751 if (incoming) {
4752 Error *local_err = NULL;
4753 qemu_start_incoming_migration(incoming, &local_err);
4754 if (local_err) {
4755 error_reportf_err(local_err, "-incoming %s: ", incoming);
4756 exit(1);
4758 } else if (autostart) {
4759 vm_start();
4762 os_setup_post();
4764 main_loop();
4765 replay_disable_events();
4766 iothread_stop_all();
4768 bdrv_close_all();
4769 pause_all_vcpus();
4770 res_free();
4772 /* vhost-user must be cleaned up before chardevs. */
4773 net_cleanup();
4774 audio_cleanup();
4775 monitor_cleanup();
4776 qemu_chr_cleanup();
4778 return 0;