Merge tag 'v2.10.0-rc1'
[qemu/ar7.git] / vl.c
blob89a567fe754b63437e01673d6eae2910d5f9115d
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 "chardev/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/misc.h"
97 #include "migration/snapshot.h"
98 #include "migration/global_state.h"
99 #include "sysemu/tpm.h"
100 #include "sysemu/dma.h"
101 #include "hw/audio/soundhw.h"
102 #include "audio/audio.h"
103 #include "sysemu/cpus.h"
104 #include "migration/colo.h"
105 #include "sysemu/kvm.h"
106 #include "sysemu/hax.h"
107 #include "qapi/qobject-input-visitor.h"
108 #include "qapi/qobject-input-visitor.h"
109 #include "qapi-visit.h"
110 #include "qapi/qmp/qjson.h"
111 #include "qemu/option.h"
112 #include "qemu/config-file.h"
113 #include "qemu-options.h"
114 #include "qmp-commands.h"
115 #include "qemu/main-loop.h"
116 #ifdef CONFIG_VIRTFS
117 #include "fsdev/qemu-fsdev.h"
118 #endif
119 #include "sysemu/qtest.h"
121 #include "disas/disas.h"
124 #include "slirp/libslirp.h"
126 #include "trace-root.h"
127 #include "trace/control.h"
128 #include "qemu/queue.h"
129 #include "sysemu/arch_init.h"
131 #include "ui/qemu-spice.h"
132 #include "qapi/string-input-visitor.h"
133 #include "qapi/opts-visitor.h"
134 #include "qom/object_interfaces.h"
135 #include "qapi-event.h"
136 #include "exec/semihost.h"
137 #include "crypto/init.h"
138 #include "sysemu/replay.h"
139 #include "qapi/qmp/qerror.h"
140 #include "sysemu/iothread.h"
142 #define MAX_VIRTIO_CONSOLES 1
143 #define MAX_SCLP_CONSOLES 1
145 static const char *data_dir[16];
146 static int data_dir_idx;
147 const char *bios_name = NULL;
148 enum vga_retrace_method vga_retrace_method = VGA_RETRACE_DUMB;
149 int request_opengl = -1;
150 int display_opengl;
151 const char* keyboard_layout = NULL;
152 ram_addr_t ram_size;
153 const char *mem_path = NULL;
154 int mem_prealloc = 0; /* force preallocation of physical target memory */
155 bool enable_mlock = false;
156 int nb_nics;
157 NICInfo nd_table[MAX_NICS];
158 int autostart;
159 static int rtc_utc = 1;
160 static int rtc_date_offset = -1; /* -1 means no change */
161 QEMUClockType rtc_clock;
162 int vga_interface_type = VGA_NONE;
163 static int full_screen = 0;
164 static int no_frame = 0;
165 int no_quit = 0;
166 static bool grab_on_hover;
167 Chardev *serial_hds[MAX_SERIAL_PORTS];
168 Chardev *parallel_hds[MAX_PARALLEL_PORTS];
169 Chardev *virtcon_hds[MAX_VIRTIO_CONSOLES];
170 Chardev *sclp_hds[MAX_SCLP_CONSOLES];
171 int win2k_install_hack = 0;
172 int singlestep = 0;
173 int smp_cpus = 1;
174 int max_cpus = 1;
175 int smp_cores = 1;
176 int smp_threads = 1;
177 int acpi_enabled = 1;
178 int no_hpet = 0;
179 int fd_bootchk = 1;
180 static int no_reboot;
181 int no_shutdown = 0;
182 int cursor_hide = 1;
183 int graphic_rotate = 0;
184 const char *watchdog;
185 QEMUOptionRom option_rom[MAX_OPTION_ROMS];
186 int nb_option_roms;
187 int old_param = 0;
188 const char *qemu_name;
189 int alt_grab = 0;
190 int ctrl_grab = 0;
191 unsigned int nb_prom_envs = 0;
192 const char *prom_envs[MAX_PROM_ENVS];
193 int boot_menu;
194 bool boot_strict;
195 uint8_t *boot_splash_filedata;
196 size_t boot_splash_filedata_size;
197 uint8_t qemu_extra_params_fw[2];
199 int icount_align_option;
201 /* The bytes in qemu_uuid are in the order specified by RFC4122, _not_ in the
202 * little-endian "wire format" described in the SMBIOS 2.6 specification.
204 QemuUUID qemu_uuid;
205 bool qemu_uuid_set;
207 /* Trace unassigned memory or i/o accesses. */
208 bool trace_unassigned;
210 static NotifierList exit_notifiers =
211 NOTIFIER_LIST_INITIALIZER(exit_notifiers);
213 static NotifierList machine_init_done_notifiers =
214 NOTIFIER_LIST_INITIALIZER(machine_init_done_notifiers);
216 bool xen_allowed;
217 uint32_t xen_domid;
218 enum xen_mode xen_mode = XEN_EMULATE;
219 bool xen_domid_restrict;
221 static int has_defaults = 1;
222 static int default_serial = 1;
223 static int default_parallel = 1;
224 static int default_virtcon = 1;
225 static int default_sclp = 1;
226 static int default_monitor = 1;
227 static int default_floppy = 1;
228 static int default_cdrom = 1;
229 static int default_sdcard = 1;
230 static int default_vga = 1;
231 static int default_net = 1;
233 static struct {
234 const char *driver;
235 int *flag;
236 } default_list[] = {
237 { .driver = "isa-serial", .flag = &default_serial },
238 { .driver = "isa-parallel", .flag = &default_parallel },
239 { .driver = "isa-fdc", .flag = &default_floppy },
240 { .driver = "floppy", .flag = &default_floppy },
241 { .driver = "ide-cd", .flag = &default_cdrom },
242 { .driver = "ide-hd", .flag = &default_cdrom },
243 { .driver = "ide-drive", .flag = &default_cdrom },
244 { .driver = "scsi-cd", .flag = &default_cdrom },
245 { .driver = "scsi-hd", .flag = &default_cdrom },
246 { .driver = "virtio-serial-pci", .flag = &default_virtcon },
247 { .driver = "virtio-serial", .flag = &default_virtcon },
248 { .driver = "VGA", .flag = &default_vga },
249 { .driver = "isa-vga", .flag = &default_vga },
250 { .driver = "cirrus-vga", .flag = &default_vga },
251 { .driver = "isa-cirrus-vga", .flag = &default_vga },
252 { .driver = "vmware-svga", .flag = &default_vga },
253 { .driver = "qxl-vga", .flag = &default_vga },
254 { .driver = "virtio-vga", .flag = &default_vga },
257 static QemuOptsList qemu_rtc_opts = {
258 .name = "rtc",
259 .head = QTAILQ_HEAD_INITIALIZER(qemu_rtc_opts.head),
260 .desc = {
262 .name = "base",
263 .type = QEMU_OPT_STRING,
265 .name = "clock",
266 .type = QEMU_OPT_STRING,
268 .name = "driftfix",
269 .type = QEMU_OPT_STRING,
271 { /* end of list */ }
275 static QemuOptsList qemu_sandbox_opts = {
276 .name = "sandbox",
277 .implied_opt_name = "enable",
278 .head = QTAILQ_HEAD_INITIALIZER(qemu_sandbox_opts.head),
279 .desc = {
281 .name = "enable",
282 .type = QEMU_OPT_BOOL,
284 { /* end of list */ }
288 static QemuOptsList qemu_option_rom_opts = {
289 .name = "option-rom",
290 .implied_opt_name = "romfile",
291 .head = QTAILQ_HEAD_INITIALIZER(qemu_option_rom_opts.head),
292 .desc = {
294 .name = "bootindex",
295 .type = QEMU_OPT_NUMBER,
296 }, {
297 .name = "romfile",
298 .type = QEMU_OPT_STRING,
300 { /* end of list */ }
304 static QemuOptsList qemu_machine_opts = {
305 .name = "machine",
306 .implied_opt_name = "type",
307 .merge_lists = true,
308 .head = QTAILQ_HEAD_INITIALIZER(qemu_machine_opts.head),
309 .desc = {
311 * no elements => accept any
312 * sanity checking will happen later
313 * when setting machine properties
319 static QemuOptsList qemu_accel_opts = {
320 .name = "accel",
321 .implied_opt_name = "accel",
322 .head = QTAILQ_HEAD_INITIALIZER(qemu_accel_opts.head),
323 .merge_lists = true,
324 .desc = {
326 .name = "accel",
327 .type = QEMU_OPT_STRING,
328 .help = "Select the type of accelerator",
331 .name = "thread",
332 .type = QEMU_OPT_STRING,
333 .help = "Enable/disable multi-threaded TCG",
335 { /* end of list */ }
339 static QemuOptsList qemu_boot_opts = {
340 .name = "boot-opts",
341 .implied_opt_name = "order",
342 .merge_lists = true,
343 .head = QTAILQ_HEAD_INITIALIZER(qemu_boot_opts.head),
344 .desc = {
346 .name = "order",
347 .type = QEMU_OPT_STRING,
348 }, {
349 .name = "once",
350 .type = QEMU_OPT_STRING,
351 }, {
352 .name = "menu",
353 .type = QEMU_OPT_BOOL,
354 }, {
355 .name = "splash",
356 .type = QEMU_OPT_STRING,
357 }, {
358 .name = "splash-time",
359 .type = QEMU_OPT_STRING,
360 }, {
361 .name = "reboot-timeout",
362 .type = QEMU_OPT_STRING,
363 }, {
364 .name = "strict",
365 .type = QEMU_OPT_BOOL,
367 { /*End of list */ }
371 static QemuOptsList qemu_add_fd_opts = {
372 .name = "add-fd",
373 .head = QTAILQ_HEAD_INITIALIZER(qemu_add_fd_opts.head),
374 .desc = {
376 .name = "fd",
377 .type = QEMU_OPT_NUMBER,
378 .help = "file descriptor of which a duplicate is added to fd set",
380 .name = "set",
381 .type = QEMU_OPT_NUMBER,
382 .help = "ID of the fd set to add fd to",
384 .name = "opaque",
385 .type = QEMU_OPT_STRING,
386 .help = "free-form string used to describe fd",
388 { /* end of list */ }
392 static QemuOptsList qemu_object_opts = {
393 .name = "object",
394 .implied_opt_name = "qom-type",
395 .head = QTAILQ_HEAD_INITIALIZER(qemu_object_opts.head),
396 .desc = {
401 static QemuOptsList qemu_tpmdev_opts = {
402 .name = "tpmdev",
403 .implied_opt_name = "type",
404 .head = QTAILQ_HEAD_INITIALIZER(qemu_tpmdev_opts.head),
405 .desc = {
406 /* options are defined in the TPM backends */
407 { /* end of list */ }
411 static QemuOptsList qemu_realtime_opts = {
412 .name = "realtime",
413 .head = QTAILQ_HEAD_INITIALIZER(qemu_realtime_opts.head),
414 .desc = {
416 .name = "mlock",
417 .type = QEMU_OPT_BOOL,
419 { /* end of list */ }
423 static QemuOptsList qemu_msg_opts = {
424 .name = "msg",
425 .head = QTAILQ_HEAD_INITIALIZER(qemu_msg_opts.head),
426 .desc = {
428 .name = "timestamp",
429 .type = QEMU_OPT_BOOL,
431 { /* end of list */ }
435 static QemuOptsList qemu_name_opts = {
436 .name = "name",
437 .implied_opt_name = "guest",
438 .merge_lists = true,
439 .head = QTAILQ_HEAD_INITIALIZER(qemu_name_opts.head),
440 .desc = {
442 .name = "guest",
443 .type = QEMU_OPT_STRING,
444 .help = "Sets the name of the guest.\n"
445 "This name will be displayed in the SDL window caption.\n"
446 "The name will also be used for the VNC server",
447 }, {
448 .name = "process",
449 .type = QEMU_OPT_STRING,
450 .help = "Sets the name of the QEMU process, as shown in top etc",
451 }, {
452 .name = "debug-threads",
453 .type = QEMU_OPT_BOOL,
454 .help = "When enabled, name the individual threads; defaults off.\n"
455 "NOTE: The thread names are for debugging and not a\n"
456 "stable API.",
458 { /* End of list */ }
462 static QemuOptsList qemu_mem_opts = {
463 .name = "memory",
464 .implied_opt_name = "size",
465 .head = QTAILQ_HEAD_INITIALIZER(qemu_mem_opts.head),
466 .merge_lists = true,
467 .desc = {
469 .name = "size",
470 .type = QEMU_OPT_SIZE,
473 .name = "slots",
474 .type = QEMU_OPT_NUMBER,
477 .name = "maxmem",
478 .type = QEMU_OPT_SIZE,
480 { /* end of list */ }
484 static QemuOptsList qemu_icount_opts = {
485 .name = "icount",
486 .implied_opt_name = "shift",
487 .merge_lists = true,
488 .head = QTAILQ_HEAD_INITIALIZER(qemu_icount_opts.head),
489 .desc = {
491 .name = "shift",
492 .type = QEMU_OPT_STRING,
493 }, {
494 .name = "align",
495 .type = QEMU_OPT_BOOL,
496 }, {
497 .name = "sleep",
498 .type = QEMU_OPT_BOOL,
499 }, {
500 .name = "rr",
501 .type = QEMU_OPT_STRING,
502 }, {
503 .name = "rrfile",
504 .type = QEMU_OPT_STRING,
505 }, {
506 .name = "rrsnapshot",
507 .type = QEMU_OPT_STRING,
509 { /* end of list */ }
513 static QemuOptsList qemu_semihosting_config_opts = {
514 .name = "semihosting-config",
515 .implied_opt_name = "enable",
516 .head = QTAILQ_HEAD_INITIALIZER(qemu_semihosting_config_opts.head),
517 .desc = {
519 .name = "enable",
520 .type = QEMU_OPT_BOOL,
521 }, {
522 .name = "target",
523 .type = QEMU_OPT_STRING,
524 }, {
525 .name = "arg",
526 .type = QEMU_OPT_STRING,
528 { /* end of list */ }
532 static QemuOptsList qemu_fw_cfg_opts = {
533 .name = "fw_cfg",
534 .implied_opt_name = "name",
535 .head = QTAILQ_HEAD_INITIALIZER(qemu_fw_cfg_opts.head),
536 .desc = {
538 .name = "name",
539 .type = QEMU_OPT_STRING,
540 .help = "Sets the fw_cfg name of the blob to be inserted",
541 }, {
542 .name = "file",
543 .type = QEMU_OPT_STRING,
544 .help = "Sets the name of the file from which\n"
545 "the fw_cfg blob will be loaded",
546 }, {
547 .name = "string",
548 .type = QEMU_OPT_STRING,
549 .help = "Sets content of the blob to be inserted from a string",
551 { /* end of list */ }
556 * Get machine options
558 * Returns: machine options (never null).
560 QemuOpts *qemu_get_machine_opts(void)
562 return qemu_find_opts_singleton("machine");
565 const char *qemu_get_vm_name(void)
567 return qemu_name;
570 static void res_free(void)
572 g_free(boot_splash_filedata);
573 boot_splash_filedata = NULL;
576 static int default_driver_check(void *opaque, QemuOpts *opts, Error **errp)
578 const char *driver = qemu_opt_get(opts, "driver");
579 int i;
581 if (!driver)
582 return 0;
583 for (i = 0; i < ARRAY_SIZE(default_list); i++) {
584 if (strcmp(default_list[i].driver, driver) != 0)
585 continue;
586 *(default_list[i].flag) = 0;
588 return 0;
591 /***********************************************************/
592 /* QEMU state */
594 static RunState current_run_state = RUN_STATE_PRELAUNCH;
596 /* We use RUN_STATE__MAX but any invalid value will do */
597 static RunState vmstop_requested = RUN_STATE__MAX;
598 static QemuMutex vmstop_lock;
600 typedef struct {
601 RunState from;
602 RunState to;
603 } RunStateTransition;
605 static const RunStateTransition runstate_transitions_def[] = {
606 /* from -> to */
607 { RUN_STATE_DEBUG, RUN_STATE_RUNNING },
608 { RUN_STATE_DEBUG, RUN_STATE_FINISH_MIGRATE },
609 { RUN_STATE_DEBUG, RUN_STATE_PRELAUNCH },
611 { RUN_STATE_INMIGRATE, RUN_STATE_INTERNAL_ERROR },
612 { RUN_STATE_INMIGRATE, RUN_STATE_IO_ERROR },
613 { RUN_STATE_INMIGRATE, RUN_STATE_PAUSED },
614 { RUN_STATE_INMIGRATE, RUN_STATE_RUNNING },
615 { RUN_STATE_INMIGRATE, RUN_STATE_SHUTDOWN },
616 { RUN_STATE_INMIGRATE, RUN_STATE_SUSPENDED },
617 { RUN_STATE_INMIGRATE, RUN_STATE_WATCHDOG },
618 { RUN_STATE_INMIGRATE, RUN_STATE_GUEST_PANICKED },
619 { RUN_STATE_INMIGRATE, RUN_STATE_FINISH_MIGRATE },
620 { RUN_STATE_INMIGRATE, RUN_STATE_PRELAUNCH },
621 { RUN_STATE_INMIGRATE, RUN_STATE_POSTMIGRATE },
622 { RUN_STATE_INMIGRATE, RUN_STATE_COLO },
624 { RUN_STATE_INTERNAL_ERROR, RUN_STATE_PAUSED },
625 { RUN_STATE_INTERNAL_ERROR, RUN_STATE_FINISH_MIGRATE },
626 { RUN_STATE_INTERNAL_ERROR, RUN_STATE_PRELAUNCH },
628 { RUN_STATE_IO_ERROR, RUN_STATE_RUNNING },
629 { RUN_STATE_IO_ERROR, RUN_STATE_FINISH_MIGRATE },
630 { RUN_STATE_IO_ERROR, RUN_STATE_PRELAUNCH },
632 { RUN_STATE_PAUSED, RUN_STATE_RUNNING },
633 { RUN_STATE_PAUSED, RUN_STATE_FINISH_MIGRATE },
634 { RUN_STATE_PAUSED, RUN_STATE_PRELAUNCH },
635 { RUN_STATE_PAUSED, RUN_STATE_COLO},
637 { RUN_STATE_POSTMIGRATE, RUN_STATE_RUNNING },
638 { RUN_STATE_POSTMIGRATE, RUN_STATE_FINISH_MIGRATE },
639 { RUN_STATE_POSTMIGRATE, RUN_STATE_PRELAUNCH },
641 { RUN_STATE_PRELAUNCH, RUN_STATE_RUNNING },
642 { RUN_STATE_PRELAUNCH, RUN_STATE_FINISH_MIGRATE },
643 { RUN_STATE_PRELAUNCH, RUN_STATE_INMIGRATE },
645 { RUN_STATE_FINISH_MIGRATE, RUN_STATE_RUNNING },
646 { RUN_STATE_FINISH_MIGRATE, RUN_STATE_POSTMIGRATE },
647 { RUN_STATE_FINISH_MIGRATE, RUN_STATE_PRELAUNCH },
648 { RUN_STATE_FINISH_MIGRATE, RUN_STATE_COLO},
650 { RUN_STATE_RESTORE_VM, RUN_STATE_RUNNING },
651 { RUN_STATE_RESTORE_VM, RUN_STATE_PRELAUNCH },
653 { RUN_STATE_COLO, RUN_STATE_RUNNING },
655 { RUN_STATE_RUNNING, RUN_STATE_DEBUG },
656 { RUN_STATE_RUNNING, RUN_STATE_INTERNAL_ERROR },
657 { RUN_STATE_RUNNING, RUN_STATE_IO_ERROR },
658 { RUN_STATE_RUNNING, RUN_STATE_PAUSED },
659 { RUN_STATE_RUNNING, RUN_STATE_FINISH_MIGRATE },
660 { RUN_STATE_RUNNING, RUN_STATE_RESTORE_VM },
661 { RUN_STATE_RUNNING, RUN_STATE_SAVE_VM },
662 { RUN_STATE_RUNNING, RUN_STATE_SHUTDOWN },
663 { RUN_STATE_RUNNING, RUN_STATE_WATCHDOG },
664 { RUN_STATE_RUNNING, RUN_STATE_GUEST_PANICKED },
665 { RUN_STATE_RUNNING, RUN_STATE_COLO},
667 { RUN_STATE_SAVE_VM, RUN_STATE_RUNNING },
669 { RUN_STATE_SHUTDOWN, RUN_STATE_PAUSED },
670 { RUN_STATE_SHUTDOWN, RUN_STATE_FINISH_MIGRATE },
671 { RUN_STATE_SHUTDOWN, RUN_STATE_PRELAUNCH },
673 { RUN_STATE_DEBUG, RUN_STATE_SUSPENDED },
674 { RUN_STATE_RUNNING, RUN_STATE_SUSPENDED },
675 { RUN_STATE_SUSPENDED, RUN_STATE_RUNNING },
676 { RUN_STATE_SUSPENDED, RUN_STATE_FINISH_MIGRATE },
677 { RUN_STATE_SUSPENDED, RUN_STATE_PRELAUNCH },
678 { RUN_STATE_SUSPENDED, RUN_STATE_COLO},
680 { RUN_STATE_WATCHDOG, RUN_STATE_RUNNING },
681 { RUN_STATE_WATCHDOG, RUN_STATE_FINISH_MIGRATE },
682 { RUN_STATE_WATCHDOG, RUN_STATE_PRELAUNCH },
683 { RUN_STATE_WATCHDOG, RUN_STATE_COLO},
685 { RUN_STATE_GUEST_PANICKED, RUN_STATE_RUNNING },
686 { RUN_STATE_GUEST_PANICKED, RUN_STATE_FINISH_MIGRATE },
687 { RUN_STATE_GUEST_PANICKED, RUN_STATE_PRELAUNCH },
689 { RUN_STATE__MAX, RUN_STATE__MAX },
692 static bool runstate_valid_transitions[RUN_STATE__MAX][RUN_STATE__MAX];
694 bool runstate_check(RunState state)
696 return current_run_state == state;
699 bool runstate_store(char *str, size_t size)
701 const char *state = RunState_lookup[current_run_state];
702 size_t len = strlen(state) + 1;
704 if (len > size) {
705 return false;
707 memcpy(str, state, len);
708 return true;
711 static void runstate_init(void)
713 const RunStateTransition *p;
715 memset(&runstate_valid_transitions, 0, sizeof(runstate_valid_transitions));
716 for (p = &runstate_transitions_def[0]; p->from != RUN_STATE__MAX; p++) {
717 runstate_valid_transitions[p->from][p->to] = true;
720 qemu_mutex_init(&vmstop_lock);
723 /* This function will abort() on invalid state transitions */
724 void runstate_set(RunState new_state)
726 assert(new_state < RUN_STATE__MAX);
728 if (current_run_state == new_state) {
729 return;
732 if (!runstate_valid_transitions[current_run_state][new_state]) {
733 error_report("invalid runstate transition: '%s' -> '%s'",
734 RunState_lookup[current_run_state],
735 RunState_lookup[new_state]);
736 abort();
738 trace_runstate_set(new_state);
739 current_run_state = new_state;
742 int runstate_is_running(void)
744 return runstate_check(RUN_STATE_RUNNING);
747 bool runstate_needs_reset(void)
749 return runstate_check(RUN_STATE_INTERNAL_ERROR) ||
750 runstate_check(RUN_STATE_SHUTDOWN);
753 StatusInfo *qmp_query_status(Error **errp)
755 StatusInfo *info = g_malloc0(sizeof(*info));
757 info->running = runstate_is_running();
758 info->singlestep = singlestep;
759 info->status = current_run_state;
761 return info;
764 bool qemu_vmstop_requested(RunState *r)
766 qemu_mutex_lock(&vmstop_lock);
767 *r = vmstop_requested;
768 vmstop_requested = RUN_STATE__MAX;
769 qemu_mutex_unlock(&vmstop_lock);
770 return *r < RUN_STATE__MAX;
773 void qemu_system_vmstop_request_prepare(void)
775 qemu_mutex_lock(&vmstop_lock);
778 void qemu_system_vmstop_request(RunState state)
780 vmstop_requested = state;
781 qemu_mutex_unlock(&vmstop_lock);
782 qemu_notify_event();
785 /***********************************************************/
786 /* real time host monotonic timer */
788 static time_t qemu_time(void)
790 return qemu_clock_get_ms(QEMU_CLOCK_HOST) / 1000;
793 /***********************************************************/
794 /* host time/date access */
795 void qemu_get_timedate(struct tm *tm, int offset)
797 time_t ti = qemu_time();
799 ti += offset;
800 if (rtc_date_offset == -1) {
801 if (rtc_utc)
802 gmtime_r(&ti, tm);
803 else
804 localtime_r(&ti, tm);
805 } else {
806 ti -= rtc_date_offset;
807 gmtime_r(&ti, tm);
811 int qemu_timedate_diff(struct tm *tm)
813 time_t seconds;
815 if (rtc_date_offset == -1)
816 if (rtc_utc)
817 seconds = mktimegm(tm);
818 else {
819 struct tm tmp = *tm;
820 tmp.tm_isdst = -1; /* use timezone to figure it out */
821 seconds = mktime(&tmp);
823 else
824 seconds = mktimegm(tm) + rtc_date_offset;
826 return seconds - qemu_time();
829 static void configure_rtc_date_offset(const char *startdate, int legacy)
831 time_t rtc_start_date;
832 struct tm tm;
834 if (!strcmp(startdate, "now") && legacy) {
835 rtc_date_offset = -1;
836 } else {
837 if (sscanf(startdate, "%d-%d-%dT%d:%d:%d",
838 &tm.tm_year,
839 &tm.tm_mon,
840 &tm.tm_mday,
841 &tm.tm_hour,
842 &tm.tm_min,
843 &tm.tm_sec) == 6) {
844 /* OK */
845 } else if (sscanf(startdate, "%d-%d-%d",
846 &tm.tm_year,
847 &tm.tm_mon,
848 &tm.tm_mday) == 3) {
849 tm.tm_hour = 0;
850 tm.tm_min = 0;
851 tm.tm_sec = 0;
852 } else {
853 goto date_fail;
855 tm.tm_year -= 1900;
856 tm.tm_mon--;
857 rtc_start_date = mktimegm(&tm);
858 if (rtc_start_date == -1) {
859 date_fail:
860 error_report("invalid date format");
861 error_printf("valid formats: "
862 "'2006-06-17T16:01:21' or '2006-06-17'\n");
863 exit(1);
865 rtc_date_offset = qemu_time() - rtc_start_date;
869 static void configure_rtc(QemuOpts *opts)
871 const char *value;
873 value = qemu_opt_get(opts, "base");
874 if (value) {
875 if (!strcmp(value, "utc")) {
876 rtc_utc = 1;
877 } else if (!strcmp(value, "localtime")) {
878 Error *blocker = NULL;
879 rtc_utc = 0;
880 error_setg(&blocker, QERR_REPLAY_NOT_SUPPORTED,
881 "-rtc base=localtime");
882 replay_add_blocker(blocker);
883 } else {
884 configure_rtc_date_offset(value, 0);
887 value = qemu_opt_get(opts, "clock");
888 if (value) {
889 if (!strcmp(value, "host")) {
890 rtc_clock = QEMU_CLOCK_HOST;
891 } else if (!strcmp(value, "rt")) {
892 rtc_clock = QEMU_CLOCK_REALTIME;
893 } else if (!strcmp(value, "vm")) {
894 rtc_clock = QEMU_CLOCK_VIRTUAL;
895 } else {
896 error_report("invalid option value '%s'", value);
897 exit(1);
900 value = qemu_opt_get(opts, "driftfix");
901 if (value) {
902 if (!strcmp(value, "slew")) {
903 static GlobalProperty slew_lost_ticks = {
904 .driver = "mc146818rtc",
905 .property = "lost_tick_policy",
906 .value = "slew",
909 qdev_prop_register_global(&slew_lost_ticks);
910 } else if (!strcmp(value, "none")) {
911 /* discard is default */
912 } else {
913 error_report("invalid option value '%s'", value);
914 exit(1);
919 /***********************************************************/
920 /* Bluetooth support */
921 static int nb_hcis;
922 static int cur_hci;
923 static struct HCIInfo *hci_table[MAX_NICS];
925 struct HCIInfo *qemu_next_hci(void)
927 if (cur_hci == nb_hcis)
928 return &null_hci;
930 return hci_table[cur_hci++];
933 static int bt_hci_parse(const char *str)
935 struct HCIInfo *hci;
936 bdaddr_t bdaddr;
938 if (nb_hcis >= MAX_NICS) {
939 error_report("too many bluetooth HCIs (max %i)", MAX_NICS);
940 return -1;
943 hci = hci_init(str);
944 if (!hci)
945 return -1;
947 bdaddr.b[0] = 0x52;
948 bdaddr.b[1] = 0x54;
949 bdaddr.b[2] = 0x00;
950 bdaddr.b[3] = 0x12;
951 bdaddr.b[4] = 0x34;
952 bdaddr.b[5] = 0x56 + nb_hcis;
953 hci->bdaddr_set(hci, bdaddr.b);
955 hci_table[nb_hcis++] = hci;
957 return 0;
960 static void bt_vhci_add(int vlan_id)
962 struct bt_scatternet_s *vlan = qemu_find_bt_vlan(vlan_id);
964 if (!vlan->slave)
965 warn_report("adding a VHCI to an empty scatternet %i",
966 vlan_id);
968 bt_vhci_init(bt_new_hci(vlan));
971 static struct bt_device_s *bt_device_add(const char *opt)
973 struct bt_scatternet_s *vlan;
974 int vlan_id = 0;
975 char *endp = strstr(opt, ",vlan=");
976 int len = (endp ? endp - opt : strlen(opt)) + 1;
977 char devname[10];
979 pstrcpy(devname, MIN(sizeof(devname), len), opt);
981 if (endp) {
982 vlan_id = strtol(endp + 6, &endp, 0);
983 if (*endp) {
984 error_report("unrecognised bluetooth vlan Id");
985 return 0;
989 vlan = qemu_find_bt_vlan(vlan_id);
991 if (!vlan->slave)
992 warn_report("adding a slave device to an empty scatternet %i",
993 vlan_id);
995 if (!strcmp(devname, "keyboard"))
996 return bt_keyboard_init(vlan);
998 error_report("unsupported bluetooth device '%s'", devname);
999 return 0;
1002 static int bt_parse(const char *opt)
1004 const char *endp, *p;
1005 int vlan;
1007 if (strstart(opt, "hci", &endp)) {
1008 if (!*endp || *endp == ',') {
1009 if (*endp)
1010 if (!strstart(endp, ",vlan=", 0))
1011 opt = endp + 1;
1013 return bt_hci_parse(opt);
1015 } else if (strstart(opt, "vhci", &endp)) {
1016 if (!*endp || *endp == ',') {
1017 if (*endp) {
1018 if (strstart(endp, ",vlan=", &p)) {
1019 vlan = strtol(p, (char **) &endp, 0);
1020 if (*endp) {
1021 error_report("bad scatternet '%s'", p);
1022 return 1;
1024 } else {
1025 error_report("bad parameter '%s'", endp + 1);
1026 return 1;
1028 } else
1029 vlan = 0;
1031 bt_vhci_add(vlan);
1032 return 0;
1034 } else if (strstart(opt, "device:", &endp))
1035 return !bt_device_add(endp);
1037 error_report("bad bluetooth parameter '%s'", opt);
1038 return 1;
1041 static int parse_sandbox(void *opaque, QemuOpts *opts, Error **errp)
1043 /* FIXME: change this to true for 1.3 */
1044 if (qemu_opt_get_bool(opts, "enable", false)) {
1045 #ifdef CONFIG_SECCOMP
1046 if (seccomp_start() < 0) {
1047 error_report("failed to install seccomp syscall filter "
1048 "in the kernel");
1049 return -1;
1051 #else
1052 error_report("seccomp support is disabled");
1053 return -1;
1054 #endif
1057 return 0;
1060 static int parse_name(void *opaque, QemuOpts *opts, Error **errp)
1062 const char *proc_name;
1064 if (qemu_opt_get(opts, "debug-threads")) {
1065 qemu_thread_naming(qemu_opt_get_bool(opts, "debug-threads", false));
1067 qemu_name = qemu_opt_get(opts, "guest");
1069 proc_name = qemu_opt_get(opts, "process");
1070 if (proc_name) {
1071 os_set_proc_name(proc_name);
1074 return 0;
1077 bool defaults_enabled(void)
1079 return has_defaults;
1082 #ifndef _WIN32
1083 static int parse_add_fd(void *opaque, QemuOpts *opts, Error **errp)
1085 int fd, dupfd, flags;
1086 int64_t fdset_id;
1087 const char *fd_opaque = NULL;
1088 AddfdInfo *fdinfo;
1090 fd = qemu_opt_get_number(opts, "fd", -1);
1091 fdset_id = qemu_opt_get_number(opts, "set", -1);
1092 fd_opaque = qemu_opt_get(opts, "opaque");
1094 if (fd < 0) {
1095 error_report("fd option is required and must be non-negative");
1096 return -1;
1099 if (fd <= STDERR_FILENO) {
1100 error_report("fd cannot be a standard I/O stream");
1101 return -1;
1105 * All fds inherited across exec() necessarily have FD_CLOEXEC
1106 * clear, while qemu sets FD_CLOEXEC on all other fds used internally.
1108 flags = fcntl(fd, F_GETFD);
1109 if (flags == -1 || (flags & FD_CLOEXEC)) {
1110 error_report("fd is not valid or already in use");
1111 return -1;
1114 if (fdset_id < 0) {
1115 error_report("set option is required and must be non-negative");
1116 return -1;
1119 #ifdef F_DUPFD_CLOEXEC
1120 dupfd = fcntl(fd, F_DUPFD_CLOEXEC, 0);
1121 #else
1122 dupfd = dup(fd);
1123 if (dupfd != -1) {
1124 qemu_set_cloexec(dupfd);
1126 #endif
1127 if (dupfd == -1) {
1128 error_report("error duplicating fd: %s", strerror(errno));
1129 return -1;
1132 /* add the duplicate fd, and optionally the opaque string, to the fd set */
1133 fdinfo = monitor_fdset_add_fd(dupfd, true, fdset_id, !!fd_opaque, fd_opaque,
1134 &error_abort);
1135 g_free(fdinfo);
1137 return 0;
1140 static int cleanup_add_fd(void *opaque, QemuOpts *opts, Error **errp)
1142 int fd;
1144 fd = qemu_opt_get_number(opts, "fd", -1);
1145 close(fd);
1147 return 0;
1149 #endif
1151 /***********************************************************/
1152 /* QEMU Block devices */
1154 #define HD_OPTS "media=disk"
1155 #define CDROM_OPTS "media=cdrom"
1156 #define FD_OPTS ""
1157 #define PFLASH_OPTS ""
1158 #define MTD_OPTS ""
1159 #define SD_OPTS ""
1161 static int drive_init_func(void *opaque, QemuOpts *opts, Error **errp)
1163 BlockInterfaceType *block_default_type = opaque;
1165 return drive_new(opts, *block_default_type) == NULL;
1168 static int drive_enable_snapshot(void *opaque, QemuOpts *opts, Error **errp)
1170 if (qemu_opt_get(opts, "snapshot") == NULL) {
1171 qemu_opt_set(opts, "snapshot", "on", &error_abort);
1173 return 0;
1176 static void default_drive(int enable, int snapshot, BlockInterfaceType type,
1177 int index, const char *optstr)
1179 QemuOpts *opts;
1180 DriveInfo *dinfo;
1182 if (!enable || drive_get_by_index(type, index)) {
1183 return;
1186 opts = drive_add(type, index, NULL, optstr);
1187 if (snapshot) {
1188 drive_enable_snapshot(NULL, opts, NULL);
1191 dinfo = drive_new(opts, type);
1192 if (!dinfo) {
1193 exit(1);
1195 dinfo->is_default = true;
1199 static QemuOptsList qemu_smp_opts = {
1200 .name = "smp-opts",
1201 .implied_opt_name = "cpus",
1202 .merge_lists = true,
1203 .head = QTAILQ_HEAD_INITIALIZER(qemu_smp_opts.head),
1204 .desc = {
1206 .name = "cpus",
1207 .type = QEMU_OPT_NUMBER,
1208 }, {
1209 .name = "sockets",
1210 .type = QEMU_OPT_NUMBER,
1211 }, {
1212 .name = "cores",
1213 .type = QEMU_OPT_NUMBER,
1214 }, {
1215 .name = "threads",
1216 .type = QEMU_OPT_NUMBER,
1217 }, {
1218 .name = "maxcpus",
1219 .type = QEMU_OPT_NUMBER,
1221 { /*End of list */ }
1225 static void smp_parse(QemuOpts *opts)
1227 if (opts) {
1228 unsigned cpus = qemu_opt_get_number(opts, "cpus", 0);
1229 unsigned sockets = qemu_opt_get_number(opts, "sockets", 0);
1230 unsigned cores = qemu_opt_get_number(opts, "cores", 0);
1231 unsigned threads = qemu_opt_get_number(opts, "threads", 0);
1233 /* compute missing values, prefer sockets over cores over threads */
1234 if (cpus == 0 || sockets == 0) {
1235 sockets = sockets > 0 ? sockets : 1;
1236 cores = cores > 0 ? cores : 1;
1237 threads = threads > 0 ? threads : 1;
1238 if (cpus == 0) {
1239 cpus = cores * threads * sockets;
1241 } else if (cores == 0) {
1242 threads = threads > 0 ? threads : 1;
1243 cores = cpus / (sockets * threads);
1244 cores = cores > 0 ? cores : 1;
1245 } else if (threads == 0) {
1246 threads = cpus / (cores * sockets);
1247 threads = threads > 0 ? threads : 1;
1248 } else if (sockets * cores * threads < cpus) {
1249 error_report("cpu topology: "
1250 "sockets (%u) * cores (%u) * threads (%u) < "
1251 "smp_cpus (%u)",
1252 sockets, cores, threads, cpus);
1253 exit(1);
1256 max_cpus = qemu_opt_get_number(opts, "maxcpus", cpus);
1258 if (max_cpus < cpus) {
1259 error_report("maxcpus must be equal to or greater than smp");
1260 exit(1);
1263 if (sockets * cores * threads > max_cpus) {
1264 error_report("cpu topology: "
1265 "sockets (%u) * cores (%u) * threads (%u) > "
1266 "maxcpus (%u)",
1267 sockets, cores, threads, max_cpus);
1268 exit(1);
1271 smp_cpus = cpus;
1272 smp_cores = cores;
1273 smp_threads = threads;
1276 if (smp_cpus > 1) {
1277 Error *blocker = NULL;
1278 error_setg(&blocker, QERR_REPLAY_NOT_SUPPORTED, "smp");
1279 replay_add_blocker(blocker);
1283 static void realtime_init(void)
1285 if (enable_mlock) {
1286 if (os_mlock() < 0) {
1287 error_report("locking memory failed");
1288 exit(1);
1294 static void configure_msg(QemuOpts *opts)
1296 enable_timestamp_msg = qemu_opt_get_bool(opts, "timestamp", true);
1299 /***********************************************************/
1300 /* Semihosting */
1302 typedef struct SemihostingConfig {
1303 bool enabled;
1304 SemihostingTarget target;
1305 const char **argv;
1306 int argc;
1307 const char *cmdline; /* concatenated argv */
1308 } SemihostingConfig;
1310 static SemihostingConfig semihosting;
1312 bool semihosting_enabled(void)
1314 return semihosting.enabled;
1317 SemihostingTarget semihosting_get_target(void)
1319 return semihosting.target;
1322 const char *semihosting_get_arg(int i)
1324 if (i >= semihosting.argc) {
1325 return NULL;
1327 return semihosting.argv[i];
1330 int semihosting_get_argc(void)
1332 return semihosting.argc;
1335 const char *semihosting_get_cmdline(void)
1337 if (semihosting.cmdline == NULL && semihosting.argc > 0) {
1338 semihosting.cmdline = g_strjoinv(" ", (gchar **)semihosting.argv);
1340 return semihosting.cmdline;
1343 static int add_semihosting_arg(void *opaque,
1344 const char *name, const char *val,
1345 Error **errp)
1347 SemihostingConfig *s = opaque;
1348 if (strcmp(name, "arg") == 0) {
1349 s->argc++;
1350 /* one extra element as g_strjoinv() expects NULL-terminated array */
1351 s->argv = g_realloc(s->argv, (s->argc + 1) * sizeof(void *));
1352 s->argv[s->argc - 1] = val;
1353 s->argv[s->argc] = NULL;
1355 return 0;
1358 /* Use strings passed via -kernel/-append to initialize semihosting.argv[] */
1359 static inline void semihosting_arg_fallback(const char *file, const char *cmd)
1361 char *cmd_token;
1363 /* argv[0] */
1364 add_semihosting_arg(&semihosting, "arg", file, NULL);
1366 /* split -append and initialize argv[1..n] */
1367 cmd_token = strtok(g_strdup(cmd), " ");
1368 while (cmd_token) {
1369 add_semihosting_arg(&semihosting, "arg", cmd_token, NULL);
1370 cmd_token = strtok(NULL, " ");
1374 /* Now we still need this for compatibility with XEN. */
1375 bool has_igd_gfx_passthru;
1376 static void igd_gfx_passthru(void)
1378 has_igd_gfx_passthru = current_machine->igd_gfx_passthru;
1381 /***********************************************************/
1382 /* USB devices */
1384 static int usb_device_add(const char *devname)
1386 USBDevice *dev = NULL;
1387 #ifndef CONFIG_LINUX
1388 const char *p;
1389 #endif
1391 if (!machine_usb(current_machine)) {
1392 return -1;
1395 /* drivers with .usbdevice_name entry in USBDeviceInfo */
1396 dev = usbdevice_create(devname);
1397 if (dev)
1398 goto done;
1400 /* the other ones */
1401 #ifndef CONFIG_LINUX
1402 /* only the linux version is qdev-ified, usb-bsd still needs this */
1403 if (strstart(devname, "host:", &p)) {
1404 dev = usb_host_device_open(usb_bus_find(-1), p);
1406 #endif
1407 if (!dev)
1408 return -1;
1410 done:
1411 return 0;
1414 static int usb_device_del(const char *devname)
1416 int bus_num, addr;
1417 const char *p;
1419 if (strstart(devname, "host:", &p)) {
1420 return -1;
1423 if (!machine_usb(current_machine)) {
1424 return -1;
1427 p = strchr(devname, '.');
1428 if (!p)
1429 return -1;
1430 bus_num = strtoul(devname, NULL, 0);
1431 addr = strtoul(p + 1, NULL, 0);
1433 return usb_device_delete_addr(bus_num, addr);
1436 static int usb_parse(const char *cmdline)
1438 int r;
1439 r = usb_device_add(cmdline);
1440 if (r < 0) {
1441 error_report("could not add USB device '%s'", cmdline);
1443 return r;
1446 void hmp_usb_add(Monitor *mon, const QDict *qdict)
1448 const char *devname = qdict_get_str(qdict, "devname");
1450 error_report("usb_add is deprecated, please use device_add instead");
1452 if (usb_device_add(devname) < 0) {
1453 error_report("could not add USB device '%s'", devname);
1457 void hmp_usb_del(Monitor *mon, const QDict *qdict)
1459 const char *devname = qdict_get_str(qdict, "devname");
1461 error_report("usb_del is deprecated, please use device_del instead");
1463 if (usb_device_del(devname) < 0) {
1464 error_report("could not delete USB device '%s'", devname);
1468 /***********************************************************/
1469 /* machine registration */
1471 MachineState *current_machine;
1473 static MachineClass *find_machine(const char *name)
1475 GSList *el, *machines = object_class_get_list(TYPE_MACHINE, false);
1476 MachineClass *mc = NULL;
1478 for (el = machines; el; el = el->next) {
1479 MachineClass *temp = el->data;
1481 if (!strcmp(temp->name, name)) {
1482 mc = temp;
1483 break;
1485 if (temp->alias &&
1486 !strcmp(temp->alias, name)) {
1487 mc = temp;
1488 break;
1492 g_slist_free(machines);
1493 return mc;
1496 MachineClass *find_default_machine(void)
1498 GSList *el, *machines = object_class_get_list(TYPE_MACHINE, false);
1499 MachineClass *mc = NULL;
1501 for (el = machines; el; el = el->next) {
1502 MachineClass *temp = el->data;
1504 if (temp->is_default) {
1505 mc = temp;
1506 break;
1510 g_slist_free(machines);
1511 return mc;
1514 MachineInfoList *qmp_query_machines(Error **errp)
1516 GSList *el, *machines = object_class_get_list(TYPE_MACHINE, false);
1517 MachineInfoList *mach_list = NULL;
1519 for (el = machines; el; el = el->next) {
1520 MachineClass *mc = el->data;
1521 MachineInfoList *entry;
1522 MachineInfo *info;
1524 info = g_malloc0(sizeof(*info));
1525 if (mc->is_default) {
1526 info->has_is_default = true;
1527 info->is_default = true;
1530 if (mc->alias) {
1531 info->has_alias = true;
1532 info->alias = g_strdup(mc->alias);
1535 info->name = g_strdup(mc->name);
1536 info->cpu_max = !mc->max_cpus ? 1 : mc->max_cpus;
1537 info->hotpluggable_cpus = mc->has_hotpluggable_cpus;
1539 entry = g_malloc0(sizeof(*entry));
1540 entry->value = info;
1541 entry->next = mach_list;
1542 mach_list = entry;
1545 g_slist_free(machines);
1546 return mach_list;
1549 static int machine_help_func(QemuOpts *opts, MachineState *machine)
1551 ObjectProperty *prop;
1552 ObjectPropertyIterator iter;
1554 if (!qemu_opt_has_help_opt(opts)) {
1555 return 0;
1558 object_property_iter_init(&iter, OBJECT(machine));
1559 while ((prop = object_property_iter_next(&iter))) {
1560 if (!prop->set) {
1561 continue;
1564 error_printf("%s.%s=%s", MACHINE_GET_CLASS(machine)->name,
1565 prop->name, prop->type);
1566 if (prop->description) {
1567 error_printf(" (%s)\n", prop->description);
1568 } else {
1569 error_printf("\n");
1573 return 1;
1576 /***********************************************************/
1577 /* main execution loop */
1579 struct vm_change_state_entry {
1580 VMChangeStateHandler *cb;
1581 void *opaque;
1582 QLIST_ENTRY (vm_change_state_entry) entries;
1585 static QLIST_HEAD(vm_change_state_head, vm_change_state_entry) vm_change_state_head;
1587 VMChangeStateEntry *qemu_add_vm_change_state_handler(VMChangeStateHandler *cb,
1588 void *opaque)
1590 VMChangeStateEntry *e;
1592 e = g_malloc0(sizeof (*e));
1594 e->cb = cb;
1595 e->opaque = opaque;
1596 QLIST_INSERT_HEAD(&vm_change_state_head, e, entries);
1597 return e;
1600 void qemu_del_vm_change_state_handler(VMChangeStateEntry *e)
1602 QLIST_REMOVE (e, entries);
1603 g_free (e);
1606 void vm_state_notify(int running, RunState state)
1608 VMChangeStateEntry *e, *next;
1610 trace_vm_state_notify(running, state);
1612 QLIST_FOREACH_SAFE(e, &vm_change_state_head, entries, next) {
1613 e->cb(e->opaque, running, state);
1617 static ShutdownCause reset_requested;
1618 static ShutdownCause shutdown_requested;
1619 static int shutdown_signal;
1620 static pid_t shutdown_pid;
1621 static int powerdown_requested;
1622 static int debug_requested;
1623 static int suspend_requested;
1624 static WakeupReason wakeup_reason;
1625 static NotifierList powerdown_notifiers =
1626 NOTIFIER_LIST_INITIALIZER(powerdown_notifiers);
1627 static NotifierList suspend_notifiers =
1628 NOTIFIER_LIST_INITIALIZER(suspend_notifiers);
1629 static NotifierList wakeup_notifiers =
1630 NOTIFIER_LIST_INITIALIZER(wakeup_notifiers);
1631 static uint32_t wakeup_reason_mask = ~(1 << QEMU_WAKEUP_REASON_NONE);
1633 ShutdownCause qemu_shutdown_requested_get(void)
1635 return shutdown_requested;
1638 ShutdownCause qemu_reset_requested_get(void)
1640 return reset_requested;
1643 static int qemu_shutdown_requested(void)
1645 return atomic_xchg(&shutdown_requested, SHUTDOWN_CAUSE_NONE);
1648 static void qemu_kill_report(void)
1650 if (!qtest_driver() && shutdown_signal) {
1651 if (shutdown_pid == 0) {
1652 /* This happens for eg ^C at the terminal, so it's worth
1653 * avoiding printing an odd message in that case.
1655 error_report("terminating on signal %d", shutdown_signal);
1656 } else {
1657 char *shutdown_cmd = qemu_get_pid_name(shutdown_pid);
1659 error_report("terminating on signal %d from pid " FMT_pid " (%s)",
1660 shutdown_signal, shutdown_pid,
1661 shutdown_cmd ? shutdown_cmd : "<unknown process>");
1662 g_free(shutdown_cmd);
1664 shutdown_signal = 0;
1668 static ShutdownCause qemu_reset_requested(void)
1670 ShutdownCause r = reset_requested;
1672 if (r && replay_checkpoint(CHECKPOINT_RESET_REQUESTED)) {
1673 reset_requested = SHUTDOWN_CAUSE_NONE;
1674 return r;
1676 return SHUTDOWN_CAUSE_NONE;
1679 static int qemu_suspend_requested(void)
1681 int r = suspend_requested;
1682 if (r && replay_checkpoint(CHECKPOINT_SUSPEND_REQUESTED)) {
1683 suspend_requested = 0;
1684 return r;
1686 return false;
1689 static WakeupReason qemu_wakeup_requested(void)
1691 return wakeup_reason;
1694 static int qemu_powerdown_requested(void)
1696 int r = powerdown_requested;
1697 powerdown_requested = 0;
1698 return r;
1701 static int qemu_debug_requested(void)
1703 int r = debug_requested;
1704 debug_requested = 0;
1705 return r;
1709 * Reset the VM. Issue an event unless @reason is SHUTDOWN_CAUSE_NONE.
1711 void qemu_system_reset(ShutdownCause reason)
1713 MachineClass *mc;
1715 mc = current_machine ? MACHINE_GET_CLASS(current_machine) : NULL;
1717 cpu_synchronize_all_states();
1719 if (mc && mc->reset) {
1720 mc->reset();
1721 } else {
1722 qemu_devices_reset();
1724 if (reason) {
1725 qapi_event_send_reset(shutdown_caused_by_guest(reason),
1726 &error_abort);
1728 cpu_synchronize_all_post_reset();
1731 void qemu_system_guest_panicked(GuestPanicInformation *info)
1733 qemu_log_mask(LOG_GUEST_ERROR, "Guest crashed\n");
1735 if (current_cpu) {
1736 current_cpu->crash_occurred = true;
1738 qapi_event_send_guest_panicked(GUEST_PANIC_ACTION_PAUSE,
1739 !!info, info, &error_abort);
1740 vm_stop(RUN_STATE_GUEST_PANICKED);
1741 if (!no_shutdown) {
1742 qapi_event_send_guest_panicked(GUEST_PANIC_ACTION_POWEROFF,
1743 !!info, info, &error_abort);
1744 qemu_system_shutdown_request(SHUTDOWN_CAUSE_GUEST_PANIC);
1747 if (info) {
1748 if (info->type == GUEST_PANIC_INFORMATION_TYPE_HYPER_V) {
1749 qemu_log_mask(LOG_GUEST_ERROR, "HV crash parameters: (%#"PRIx64
1750 " %#"PRIx64" %#"PRIx64" %#"PRIx64" %#"PRIx64")\n",
1751 info->u.hyper_v.arg1,
1752 info->u.hyper_v.arg2,
1753 info->u.hyper_v.arg3,
1754 info->u.hyper_v.arg4,
1755 info->u.hyper_v.arg5);
1757 qapi_free_GuestPanicInformation(info);
1761 void qemu_system_reset_request(ShutdownCause reason)
1763 if (no_reboot) {
1764 shutdown_requested = reason;
1765 } else {
1766 reset_requested = reason;
1768 cpu_stop_current();
1769 qemu_notify_event();
1772 static void qemu_system_suspend(void)
1774 pause_all_vcpus();
1775 notifier_list_notify(&suspend_notifiers, NULL);
1776 runstate_set(RUN_STATE_SUSPENDED);
1777 qapi_event_send_suspend(&error_abort);
1780 void qemu_system_suspend_request(void)
1782 if (runstate_check(RUN_STATE_SUSPENDED)) {
1783 return;
1785 suspend_requested = 1;
1786 cpu_stop_current();
1787 qemu_notify_event();
1790 void qemu_register_suspend_notifier(Notifier *notifier)
1792 notifier_list_add(&suspend_notifiers, notifier);
1795 void qemu_system_wakeup_request(WakeupReason reason)
1797 trace_system_wakeup_request(reason);
1799 if (!runstate_check(RUN_STATE_SUSPENDED)) {
1800 return;
1802 if (!(wakeup_reason_mask & (1 << reason))) {
1803 return;
1805 runstate_set(RUN_STATE_RUNNING);
1806 wakeup_reason = reason;
1807 qemu_notify_event();
1810 void qemu_system_wakeup_enable(WakeupReason reason, bool enabled)
1812 if (enabled) {
1813 wakeup_reason_mask |= (1 << reason);
1814 } else {
1815 wakeup_reason_mask &= ~(1 << reason);
1819 void qemu_register_wakeup_notifier(Notifier *notifier)
1821 notifier_list_add(&wakeup_notifiers, notifier);
1824 void qemu_system_killed(int signal, pid_t pid)
1826 shutdown_signal = signal;
1827 shutdown_pid = pid;
1828 no_shutdown = 0;
1830 /* Cannot call qemu_system_shutdown_request directly because
1831 * we are in a signal handler.
1833 shutdown_requested = SHUTDOWN_CAUSE_HOST_SIGNAL;
1834 qemu_notify_event();
1837 void qemu_system_shutdown_request(ShutdownCause reason)
1839 trace_qemu_system_shutdown_request(reason);
1840 replay_shutdown_request(reason);
1841 shutdown_requested = reason;
1842 qemu_notify_event();
1845 static void qemu_system_powerdown(void)
1847 qapi_event_send_powerdown(&error_abort);
1848 notifier_list_notify(&powerdown_notifiers, NULL);
1851 void qemu_system_powerdown_request(void)
1853 trace_qemu_system_powerdown_request();
1854 powerdown_requested = 1;
1855 qemu_notify_event();
1858 void qemu_register_powerdown_notifier(Notifier *notifier)
1860 notifier_list_add(&powerdown_notifiers, notifier);
1863 void qemu_system_debug_request(void)
1865 debug_requested = 1;
1866 qemu_notify_event();
1869 static bool main_loop_should_exit(void)
1871 RunState r;
1872 ShutdownCause request;
1874 if (qemu_debug_requested()) {
1875 vm_stop(RUN_STATE_DEBUG);
1877 if (qemu_suspend_requested()) {
1878 qemu_system_suspend();
1880 request = qemu_shutdown_requested();
1881 if (request) {
1882 qemu_kill_report();
1883 qapi_event_send_shutdown(shutdown_caused_by_guest(request),
1884 &error_abort);
1885 if (no_shutdown) {
1886 vm_stop(RUN_STATE_SHUTDOWN);
1887 } else {
1888 return true;
1891 request = qemu_reset_requested();
1892 if (request) {
1893 pause_all_vcpus();
1894 qemu_system_reset(request);
1895 resume_all_vcpus();
1896 if (!runstate_check(RUN_STATE_RUNNING) &&
1897 !runstate_check(RUN_STATE_INMIGRATE)) {
1898 runstate_set(RUN_STATE_PRELAUNCH);
1901 if (qemu_wakeup_requested()) {
1902 pause_all_vcpus();
1903 qemu_system_reset(SHUTDOWN_CAUSE_NONE);
1904 notifier_list_notify(&wakeup_notifiers, &wakeup_reason);
1905 wakeup_reason = QEMU_WAKEUP_REASON_NONE;
1906 resume_all_vcpus();
1907 qapi_event_send_wakeup(&error_abort);
1909 if (qemu_powerdown_requested()) {
1910 qemu_system_powerdown();
1912 if (qemu_vmstop_requested(&r)) {
1913 vm_stop(r);
1915 return false;
1918 static void main_loop(void)
1920 #ifdef CONFIG_PROFILER
1921 int64_t ti;
1922 #endif
1923 do {
1924 #ifdef CONFIG_PROFILER
1925 ti = profile_getclock();
1926 #endif
1927 main_loop_wait(false);
1928 #ifdef CONFIG_PROFILER
1929 dev_time += profile_getclock() - ti;
1930 #endif
1931 } while (!main_loop_should_exit());
1934 static void version(void)
1936 printf("QEMU emulator version " QEMU_VERSION QEMU_PKGVERSION "\n"
1937 QEMU_COPYRIGHT "\n");
1940 static void QEMU_NORETURN help(int exitcode)
1942 version();
1943 printf("usage: %s [options] [disk_image]\n\n"
1944 "'disk_image' is a raw hard disk image for IDE hard disk 0\n\n",
1945 error_get_progname());
1947 #define QEMU_OPTIONS_GENERATE_HELP
1948 #include "qemu-options-wrapper.h"
1950 printf("\nDuring emulation, the following keys are useful:\n"
1951 "ctrl-alt-f toggle full screen\n"
1952 "ctrl-alt-n switch to virtual console 'n'\n"
1953 "ctrl-alt toggle mouse and keyboard grab\n"
1954 "\n"
1955 "When using -nographic, press 'ctrl-a h' to get some help.\n");
1957 exit(exitcode);
1960 #define HAS_ARG 0x0001
1962 typedef struct QEMUOption {
1963 const char *name;
1964 int flags;
1965 int index;
1966 uint32_t arch_mask;
1967 } QEMUOption;
1969 static const QEMUOption qemu_options[] = {
1970 { "h", 0, QEMU_OPTION_h, QEMU_ARCH_ALL },
1971 #define QEMU_OPTIONS_GENERATE_OPTIONS
1972 #include "qemu-options-wrapper.h"
1973 { NULL },
1976 typedef struct VGAInterfaceInfo {
1977 const char *opt_name; /* option name */
1978 const char *name; /* human-readable name */
1979 /* Class names indicating that support is available.
1980 * If no class is specified, the interface is always available */
1981 const char *class_names[2];
1982 } VGAInterfaceInfo;
1984 static VGAInterfaceInfo vga_interfaces[VGA_TYPE_MAX] = {
1985 [VGA_NONE] = {
1986 .opt_name = "none",
1988 [VGA_STD] = {
1989 .opt_name = "std",
1990 .name = "standard VGA",
1991 .class_names = { "VGA", "isa-vga" },
1993 [VGA_CIRRUS] = {
1994 .opt_name = "cirrus",
1995 .name = "Cirrus VGA",
1996 .class_names = { "cirrus-vga", "isa-cirrus-vga" },
1998 [VGA_VMWARE] = {
1999 .opt_name = "vmware",
2000 .name = "VMWare SVGA",
2001 .class_names = { "vmware-svga" },
2003 [VGA_VIRTIO] = {
2004 .opt_name = "virtio",
2005 .name = "Virtio VGA",
2006 .class_names = { "virtio-vga" },
2008 [VGA_QXL] = {
2009 .opt_name = "qxl",
2010 .name = "QXL VGA",
2011 .class_names = { "qxl-vga" },
2013 [VGA_TCX] = {
2014 .opt_name = "tcx",
2015 .name = "TCX framebuffer",
2016 .class_names = { "SUNW,tcx" },
2018 [VGA_CG3] = {
2019 .opt_name = "cg3",
2020 .name = "CG3 framebuffer",
2021 .class_names = { "cgthree" },
2023 [VGA_XENFB] = {
2024 .opt_name = "xenfb",
2028 static bool vga_interface_available(VGAInterfaceType t)
2030 VGAInterfaceInfo *ti = &vga_interfaces[t];
2032 assert(t < VGA_TYPE_MAX);
2033 return !ti->class_names[0] ||
2034 object_class_by_name(ti->class_names[0]) ||
2035 object_class_by_name(ti->class_names[1]);
2038 static void select_vgahw(const char *p)
2040 const char *opts;
2041 int t;
2043 assert(vga_interface_type == VGA_NONE);
2044 for (t = 0; t < VGA_TYPE_MAX; t++) {
2045 VGAInterfaceInfo *ti = &vga_interfaces[t];
2046 if (ti->opt_name && strstart(p, ti->opt_name, &opts)) {
2047 if (!vga_interface_available(t)) {
2048 error_report("%s not available", ti->name);
2049 exit(1);
2051 vga_interface_type = t;
2052 break;
2055 if (t == VGA_TYPE_MAX) {
2056 invalid_vga:
2057 error_report("unknown vga type: %s", p);
2058 exit(1);
2060 while (*opts) {
2061 const char *nextopt;
2063 if (strstart(opts, ",retrace=", &nextopt)) {
2064 opts = nextopt;
2065 if (strstart(opts, "dumb", &nextopt))
2066 vga_retrace_method = VGA_RETRACE_DUMB;
2067 else if (strstart(opts, "precise", &nextopt))
2068 vga_retrace_method = VGA_RETRACE_PRECISE;
2069 else goto invalid_vga;
2070 } else goto invalid_vga;
2071 opts = nextopt;
2075 typedef enum DisplayType {
2076 DT_DEFAULT,
2077 DT_CURSES,
2078 DT_SDL,
2079 DT_COCOA,
2080 DT_GTK,
2081 DT_EGL,
2082 DT_NONE,
2083 } DisplayType;
2085 static DisplayType select_display(const char *p)
2087 const char *opts;
2088 DisplayType display = DT_DEFAULT;
2090 if (strstart(p, "sdl", &opts)) {
2091 #ifdef CONFIG_SDL
2092 display = DT_SDL;
2093 while (*opts) {
2094 const char *nextopt;
2096 if (strstart(opts, ",frame=", &nextopt)) {
2097 opts = nextopt;
2098 if (strstart(opts, "on", &nextopt)) {
2099 no_frame = 0;
2100 } else if (strstart(opts, "off", &nextopt)) {
2101 no_frame = 1;
2102 } else {
2103 goto invalid_sdl_args;
2105 } else if (strstart(opts, ",alt_grab=", &nextopt)) {
2106 opts = nextopt;
2107 if (strstart(opts, "on", &nextopt)) {
2108 alt_grab = 1;
2109 } else if (strstart(opts, "off", &nextopt)) {
2110 alt_grab = 0;
2111 } else {
2112 goto invalid_sdl_args;
2114 } else if (strstart(opts, ",ctrl_grab=", &nextopt)) {
2115 opts = nextopt;
2116 if (strstart(opts, "on", &nextopt)) {
2117 ctrl_grab = 1;
2118 } else if (strstart(opts, "off", &nextopt)) {
2119 ctrl_grab = 0;
2120 } else {
2121 goto invalid_sdl_args;
2123 } else if (strstart(opts, ",window_close=", &nextopt)) {
2124 opts = nextopt;
2125 if (strstart(opts, "on", &nextopt)) {
2126 no_quit = 0;
2127 } else if (strstart(opts, "off", &nextopt)) {
2128 no_quit = 1;
2129 } else {
2130 goto invalid_sdl_args;
2132 } else if (strstart(opts, ",gl=", &nextopt)) {
2133 opts = nextopt;
2134 if (strstart(opts, "on", &nextopt)) {
2135 request_opengl = 1;
2136 } else if (strstart(opts, "off", &nextopt)) {
2137 request_opengl = 0;
2138 } else {
2139 goto invalid_sdl_args;
2141 } else {
2142 invalid_sdl_args:
2143 error_report("invalid SDL option string");
2144 exit(1);
2146 opts = nextopt;
2148 #else
2149 error_report("SDL support is disabled");
2150 exit(1);
2151 #endif
2152 } else if (strstart(p, "vnc", &opts)) {
2153 if (*opts == '=') {
2154 vnc_parse(opts + 1, &error_fatal);
2155 } else {
2156 error_report("VNC requires a display argument vnc=<display>");
2157 exit(1);
2159 } else if (strstart(p, "egl-headless", &opts)) {
2160 #ifdef CONFIG_OPENGL_DMABUF
2161 request_opengl = 1;
2162 display_opengl = 1;
2163 display = DT_EGL;
2164 #else
2165 fprintf(stderr, "egl support is disabled\n");
2166 exit(1);
2167 #endif
2168 } else if (strstart(p, "curses", &opts)) {
2169 #ifdef CONFIG_CURSES
2170 display = DT_CURSES;
2171 #else
2172 error_report("curses support is disabled");
2173 exit(1);
2174 #endif
2175 } else if (strstart(p, "gtk", &opts)) {
2176 #ifdef CONFIG_GTK
2177 display = DT_GTK;
2178 while (*opts) {
2179 const char *nextopt;
2181 if (strstart(opts, ",grab_on_hover=", &nextopt)) {
2182 opts = nextopt;
2183 if (strstart(opts, "on", &nextopt)) {
2184 grab_on_hover = true;
2185 } else if (strstart(opts, "off", &nextopt)) {
2186 grab_on_hover = false;
2187 } else {
2188 goto invalid_gtk_args;
2190 } else if (strstart(opts, ",gl=", &nextopt)) {
2191 opts = nextopt;
2192 if (strstart(opts, "on", &nextopt)) {
2193 request_opengl = 1;
2194 } else if (strstart(opts, "off", &nextopt)) {
2195 request_opengl = 0;
2196 } else {
2197 goto invalid_gtk_args;
2199 } else {
2200 invalid_gtk_args:
2201 error_report("invalid GTK option string");
2202 exit(1);
2204 opts = nextopt;
2206 #else
2207 error_report("GTK support is disabled");
2208 exit(1);
2209 #endif
2210 } else if (strstart(p, "none", &opts)) {
2211 display = DT_NONE;
2212 } else {
2213 error_report("unknown display type");
2214 exit(1);
2217 return display;
2220 static int balloon_parse(const char *arg)
2222 QemuOpts *opts;
2224 if (strcmp(arg, "none") == 0) {
2225 return 0;
2228 if (!strncmp(arg, "virtio", 6)) {
2229 if (arg[6] == ',') {
2230 /* have params -> parse them */
2231 opts = qemu_opts_parse_noisily(qemu_find_opts("device"), arg + 7,
2232 false);
2233 if (!opts)
2234 return -1;
2235 } else {
2236 /* create empty opts */
2237 opts = qemu_opts_create(qemu_find_opts("device"), NULL, 0,
2238 &error_abort);
2240 qemu_opt_set(opts, "driver", "virtio-balloon", &error_abort);
2241 return 0;
2244 return -1;
2247 #if defined(CONFIG_RUBY)
2248 /* Ruby interface for QEMU. See README.EXT for programming example. */
2249 #warning mit ruby
2250 #include <ruby.h>
2252 static VALUE qemu_open(int argc, VALUE *argv, VALUE klass)
2254 int i;
2255 for (i = 0; i < argc; i++) {
2256 argv[i] = StringValue(argv[i]);
2257 fprintf(stderr, "argv[%d] = %s\n", i, StringValuePtr(argv[i]));
2259 //~ if (rb_scan_args(argc, argv, "11", &file, &vmode) == 1) {
2260 //~ mode = 0666; /* default value */
2261 //~ }
2262 return INT2FIX(argc);
2265 void Init_qemu(void)
2267 printf("%s\n", __func__);
2268 VALUE cQEMU = rb_define_class("QEMU", rb_cObject);
2269 rb_define_singleton_method(cQEMU, "run", qemu_open, -1);
2270 #if 0
2271 rb_define_method();
2272 #endif
2274 #endif /* CONFIG_RUBY */
2276 #if defined(CONFIG_DLL)
2277 BOOL APIENTRY DllMain(HINSTANCE hModule, DWORD ul_reason_for_call, LPVOID data)
2279 return FALSE;
2281 #endif /* CONFIG_DLL */
2283 char *qemu_find_file(int type, const char *name)
2285 int i;
2286 const char *subdir;
2287 char *buf;
2289 /* Try the name as a straight path first */
2290 if (access(name, R_OK) == 0) {
2291 trace_load_file(name, name);
2292 return g_strdup(name);
2295 switch (type) {
2296 case QEMU_FILE_TYPE_BIOS:
2297 subdir = "";
2298 break;
2299 case QEMU_FILE_TYPE_KEYMAP:
2300 subdir = "keymaps/";
2301 break;
2302 default:
2303 abort();
2306 for (i = 0; i < data_dir_idx; i++) {
2307 buf = g_strdup_printf("%s/%s%s", data_dir[i], subdir, name);
2308 if (access(buf, R_OK) == 0) {
2309 trace_load_file(name, buf);
2310 return buf;
2312 g_free(buf);
2314 return NULL;
2317 static inline bool nonempty_str(const char *str)
2319 return str && *str;
2322 static int parse_fw_cfg(void *opaque, QemuOpts *opts, Error **errp)
2324 gchar *buf;
2325 size_t size;
2326 const char *name, *file, *str;
2327 FWCfgState *fw_cfg = (FWCfgState *) opaque;
2329 if (fw_cfg == NULL) {
2330 error_report("fw_cfg device not available");
2331 return -1;
2333 name = qemu_opt_get(opts, "name");
2334 file = qemu_opt_get(opts, "file");
2335 str = qemu_opt_get(opts, "string");
2337 /* we need name and either a file or the content string */
2338 if (!(nonempty_str(name) && (nonempty_str(file) || nonempty_str(str)))) {
2339 error_report("invalid argument(s)");
2340 return -1;
2342 if (nonempty_str(file) && nonempty_str(str)) {
2343 error_report("file and string are mutually exclusive");
2344 return -1;
2346 if (strlen(name) > FW_CFG_MAX_FILE_PATH - 1) {
2347 error_report("name too long (max. %d char)", FW_CFG_MAX_FILE_PATH - 1);
2348 return -1;
2350 if (strncmp(name, "opt/", 4) != 0) {
2351 warn_report("externally provided fw_cfg item names "
2352 "should be prefixed with \"opt/\"");
2354 if (nonempty_str(str)) {
2355 size = strlen(str); /* NUL terminator NOT included in fw_cfg blob */
2356 buf = g_memdup(str, size);
2357 } else {
2358 if (!g_file_get_contents(file, &buf, &size, NULL)) {
2359 error_report("can't load %s", file);
2360 return -1;
2363 /* For legacy, keep user files in a specific global order. */
2364 fw_cfg_set_order_override(fw_cfg, FW_CFG_ORDER_OVERRIDE_USER);
2365 fw_cfg_add_file(fw_cfg, name, buf, size);
2366 fw_cfg_reset_order_override(fw_cfg);
2367 return 0;
2370 static int device_help_func(void *opaque, QemuOpts *opts, Error **errp)
2372 return qdev_device_help(opts);
2375 static int device_init_func(void *opaque, QemuOpts *opts, Error **errp)
2377 Error *err = NULL;
2378 DeviceState *dev;
2380 dev = qdev_device_add(opts, &err);
2381 if (!dev) {
2382 error_report_err(err);
2383 return -1;
2385 object_unref(OBJECT(dev));
2386 return 0;
2389 static int chardev_init_func(void *opaque, QemuOpts *opts, Error **errp)
2391 Error *local_err = NULL;
2393 if (!qemu_chr_new_from_opts(opts, &local_err)) {
2394 if (local_err) {
2395 error_report_err(local_err);
2396 return -1;
2398 exit(0);
2400 return 0;
2403 #ifdef CONFIG_VIRTFS
2404 static int fsdev_init_func(void *opaque, QemuOpts *opts, Error **errp)
2406 return qemu_fsdev_add(opts);
2408 #endif
2410 static int mon_init_func(void *opaque, QemuOpts *opts, Error **errp)
2412 Chardev *chr;
2413 const char *chardev;
2414 const char *mode;
2415 int flags;
2417 mode = qemu_opt_get(opts, "mode");
2418 if (mode == NULL) {
2419 mode = "readline";
2421 if (strcmp(mode, "readline") == 0) {
2422 flags = MONITOR_USE_READLINE;
2423 } else if (strcmp(mode, "control") == 0) {
2424 flags = MONITOR_USE_CONTROL;
2425 } else {
2426 error_report("unknown monitor mode \"%s\"", mode);
2427 exit(1);
2430 if (qemu_opt_get_bool(opts, "pretty", 0))
2431 flags |= MONITOR_USE_PRETTY;
2433 if (qemu_opt_get_bool(opts, "default", 0)) {
2434 error_report("option 'default' does nothing and is deprecated");
2437 chardev = qemu_opt_get(opts, "chardev");
2438 chr = qemu_chr_find(chardev);
2439 if (chr == NULL) {
2440 error_report("chardev \"%s\" not found", chardev);
2441 exit(1);
2444 monitor_init(chr, flags);
2445 return 0;
2448 static void monitor_parse(const char *optarg, const char *mode, bool pretty)
2450 static int monitor_device_index = 0;
2451 QemuOpts *opts;
2452 const char *p;
2453 char label[32];
2455 if (strstart(optarg, "chardev:", &p)) {
2456 snprintf(label, sizeof(label), "%s", p);
2457 } else {
2458 snprintf(label, sizeof(label), "compat_monitor%d",
2459 monitor_device_index);
2460 opts = qemu_chr_parse_compat(label, optarg);
2461 if (!opts) {
2462 error_report("parse error: %s", optarg);
2463 exit(1);
2467 opts = qemu_opts_create(qemu_find_opts("mon"), label, 1, &error_fatal);
2468 qemu_opt_set(opts, "mode", mode, &error_abort);
2469 qemu_opt_set(opts, "chardev", label, &error_abort);
2470 qemu_opt_set_bool(opts, "pretty", pretty, &error_abort);
2471 monitor_device_index++;
2474 struct device_config {
2475 enum {
2476 DEV_USB, /* -usbdevice */
2477 DEV_BT, /* -bt */
2478 DEV_SERIAL, /* -serial */
2479 DEV_PARALLEL, /* -parallel */
2480 DEV_VIRTCON, /* -virtioconsole */
2481 DEV_DEBUGCON, /* -debugcon */
2482 DEV_GDB, /* -gdb, -s */
2483 DEV_SCLP, /* s390 sclp */
2484 } type;
2485 const char *cmdline;
2486 Location loc;
2487 QTAILQ_ENTRY(device_config) next;
2490 static QTAILQ_HEAD(, device_config) device_configs =
2491 QTAILQ_HEAD_INITIALIZER(device_configs);
2493 static void add_device_config(int type, const char *cmdline)
2495 struct device_config *conf;
2497 conf = g_malloc0(sizeof(*conf));
2498 conf->type = type;
2499 conf->cmdline = cmdline;
2500 loc_save(&conf->loc);
2501 QTAILQ_INSERT_TAIL(&device_configs, conf, next);
2504 static int foreach_device_config(int type, int (*func)(const char *cmdline))
2506 struct device_config *conf;
2507 int rc;
2509 QTAILQ_FOREACH(conf, &device_configs, next) {
2510 if (conf->type != type)
2511 continue;
2512 loc_push_restore(&conf->loc);
2513 rc = func(conf->cmdline);
2514 loc_pop(&conf->loc);
2515 if (rc) {
2516 return rc;
2519 return 0;
2522 static int serial_parse(const char *devname)
2524 static int index = 0;
2525 char label[32];
2527 if (strcmp(devname, "none") == 0)
2528 return 0;
2529 if (index == MAX_SERIAL_PORTS) {
2530 error_report("too many serial ports");
2531 exit(1);
2533 snprintf(label, sizeof(label), "serial%d", index);
2534 serial_hds[index] = qemu_chr_new(label, devname);
2535 if (!serial_hds[index]) {
2536 error_report("could not connect serial device"
2537 " to character backend '%s'", devname);
2538 return -1;
2540 index++;
2541 return 0;
2544 static int parallel_parse(const char *devname)
2546 static int index = 0;
2547 char label[32];
2549 if (strcmp(devname, "none") == 0)
2550 return 0;
2551 if (index == MAX_PARALLEL_PORTS) {
2552 error_report("too many parallel ports");
2553 exit(1);
2555 snprintf(label, sizeof(label), "parallel%d", index);
2556 parallel_hds[index] = qemu_chr_new(label, devname);
2557 if (!parallel_hds[index]) {
2558 error_report("could not connect parallel device"
2559 " to character backend '%s'", devname);
2560 return -1;
2562 index++;
2563 return 0;
2566 static int virtcon_parse(const char *devname)
2568 QemuOptsList *device = qemu_find_opts("device");
2569 static int index = 0;
2570 char label[32];
2571 QemuOpts *bus_opts, *dev_opts;
2573 if (strcmp(devname, "none") == 0)
2574 return 0;
2575 if (index == MAX_VIRTIO_CONSOLES) {
2576 error_report("too many virtio consoles");
2577 exit(1);
2580 bus_opts = qemu_opts_create(device, NULL, 0, &error_abort);
2581 qemu_opt_set(bus_opts, "driver", "virtio-serial", &error_abort);
2583 dev_opts = qemu_opts_create(device, NULL, 0, &error_abort);
2584 qemu_opt_set(dev_opts, "driver", "virtconsole", &error_abort);
2586 snprintf(label, sizeof(label), "virtcon%d", index);
2587 virtcon_hds[index] = qemu_chr_new(label, devname);
2588 if (!virtcon_hds[index]) {
2589 error_report("could not connect virtio console"
2590 " to character backend '%s'", devname);
2591 return -1;
2593 qemu_opt_set(dev_opts, "chardev", label, &error_abort);
2595 index++;
2596 return 0;
2599 static int sclp_parse(const char *devname)
2601 QemuOptsList *device = qemu_find_opts("device");
2602 static int index = 0;
2603 char label[32];
2604 QemuOpts *dev_opts;
2606 if (strcmp(devname, "none") == 0) {
2607 return 0;
2609 if (index == MAX_SCLP_CONSOLES) {
2610 error_report("too many sclp consoles");
2611 exit(1);
2614 assert(arch_type == QEMU_ARCH_S390X);
2616 dev_opts = qemu_opts_create(device, NULL, 0, NULL);
2617 qemu_opt_set(dev_opts, "driver", "sclpconsole", &error_abort);
2619 snprintf(label, sizeof(label), "sclpcon%d", index);
2620 sclp_hds[index] = qemu_chr_new(label, devname);
2621 if (!sclp_hds[index]) {
2622 error_report("could not connect sclp console"
2623 " to character backend '%s'", devname);
2624 return -1;
2626 qemu_opt_set(dev_opts, "chardev", label, &error_abort);
2628 index++;
2629 return 0;
2632 static int debugcon_parse(const char *devname)
2634 QemuOpts *opts;
2636 if (!qemu_chr_new("debugcon", devname)) {
2637 exit(1);
2639 opts = qemu_opts_create(qemu_find_opts("device"), "debugcon", 1, NULL);
2640 if (!opts) {
2641 error_report("already have a debugcon device");
2642 exit(1);
2644 qemu_opt_set(opts, "driver", "isa-debugcon", &error_abort);
2645 qemu_opt_set(opts, "chardev", "debugcon", &error_abort);
2646 return 0;
2649 static gint machine_class_cmp(gconstpointer a, gconstpointer b)
2651 const MachineClass *mc1 = a, *mc2 = b;
2652 int res;
2654 if (mc1->family == NULL) {
2655 if (mc2->family == NULL) {
2656 /* Compare standalone machine types against each other; they sort
2657 * in increasing order.
2659 return strcmp(object_class_get_name(OBJECT_CLASS(mc1)),
2660 object_class_get_name(OBJECT_CLASS(mc2)));
2663 /* Standalone machine types sort after families. */
2664 return 1;
2667 if (mc2->family == NULL) {
2668 /* Families sort before standalone machine types. */
2669 return -1;
2672 /* Families sort between each other alphabetically increasingly. */
2673 res = strcmp(mc1->family, mc2->family);
2674 if (res != 0) {
2675 return res;
2678 /* Within the same family, machine types sort in decreasing order. */
2679 return strcmp(object_class_get_name(OBJECT_CLASS(mc2)),
2680 object_class_get_name(OBJECT_CLASS(mc1)));
2683 static MachineClass *machine_parse(const char *name)
2685 MachineClass *mc = NULL;
2686 GSList *el, *machines = object_class_get_list(TYPE_MACHINE, false);
2688 if (name) {
2689 mc = find_machine(name);
2691 if (mc) {
2692 g_slist_free(machines);
2693 return mc;
2695 if (name && !is_help_option(name)) {
2696 error_report("unsupported machine type");
2697 error_printf("Use -machine help to list supported machines\n");
2698 } else {
2699 printf("Supported machines are:\n");
2700 machines = g_slist_sort(machines, machine_class_cmp);
2701 for (el = machines; el; el = el->next) {
2702 MachineClass *mc = el->data;
2703 if (mc->alias) {
2704 printf("%-20s %s (alias of %s)\n", mc->alias, mc->desc, mc->name);
2706 printf("%-20s %s%s\n", mc->name, mc->desc,
2707 mc->is_default ? " (default)" : "");
2711 g_slist_free(machines);
2712 exit(!name || !is_help_option(name));
2715 void qemu_add_exit_notifier(Notifier *notify)
2717 notifier_list_add(&exit_notifiers, notify);
2720 void qemu_remove_exit_notifier(Notifier *notify)
2722 notifier_remove(notify);
2725 static void qemu_run_exit_notifiers(void)
2727 notifier_list_notify(&exit_notifiers, NULL);
2730 static bool machine_init_done;
2732 void qemu_add_machine_init_done_notifier(Notifier *notify)
2734 notifier_list_add(&machine_init_done_notifiers, notify);
2735 if (machine_init_done) {
2736 notify->notify(notify, NULL);
2740 void qemu_remove_machine_init_done_notifier(Notifier *notify)
2742 notifier_remove(notify);
2745 static void qemu_run_machine_init_done_notifiers(void)
2747 notifier_list_notify(&machine_init_done_notifiers, NULL);
2748 machine_init_done = true;
2751 static const QEMUOption *lookup_opt(int argc, char **argv,
2752 const char **poptarg, int *poptind)
2754 const QEMUOption *popt;
2755 int optind = *poptind;
2756 char *r = argv[optind];
2757 const char *optarg;
2759 loc_set_cmdline(argv, optind, 1);
2760 optind++;
2761 /* Treat --foo the same as -foo. */
2762 if (r[1] == '-')
2763 r++;
2764 popt = qemu_options;
2765 for(;;) {
2766 if (!popt->name) {
2767 error_report("invalid option");
2768 exit(1);
2770 if (!strcmp(popt->name, r + 1))
2771 break;
2772 popt++;
2774 if (popt->flags & HAS_ARG) {
2775 if (optind >= argc) {
2776 error_report("requires an argument");
2777 exit(1);
2779 optarg = argv[optind++];
2780 loc_set_cmdline(argv, optind - 2, 2);
2781 } else {
2782 optarg = NULL;
2785 *poptarg = optarg;
2786 *poptind = optind;
2788 return popt;
2791 static MachineClass *select_machine(void)
2793 MachineClass *machine_class = find_default_machine();
2794 const char *optarg;
2795 QemuOpts *opts;
2796 Location loc;
2798 loc_push_none(&loc);
2800 opts = qemu_get_machine_opts();
2801 qemu_opts_loc_restore(opts);
2803 optarg = qemu_opt_get(opts, "type");
2804 if (optarg) {
2805 machine_class = machine_parse(optarg);
2808 if (!machine_class) {
2809 error_report("No machine specified, and there is no default");
2810 error_printf("Use -machine help to list supported machines\n");
2811 exit(1);
2814 loc_pop(&loc);
2815 return machine_class;
2818 static int machine_set_property(void *opaque,
2819 const char *name, const char *value,
2820 Error **errp)
2822 Object *obj = OBJECT(opaque);
2823 Error *local_err = NULL;
2824 char *p, *qom_name;
2826 if (strcmp(name, "type") == 0) {
2827 return 0;
2830 qom_name = g_strdup(name);
2831 for (p = qom_name; *p; p++) {
2832 if (*p == '_') {
2833 *p = '-';
2837 object_property_parse(obj, value, qom_name, &local_err);
2838 g_free(qom_name);
2840 if (local_err) {
2841 error_report_err(local_err);
2842 return -1;
2845 return 0;
2850 * Initial object creation happens before all other
2851 * QEMU data types are created. The majority of objects
2852 * can be created at this point. The rng-egd object
2853 * cannot be created here, as it depends on the chardev
2854 * already existing.
2856 static bool object_create_initial(const char *type)
2858 if (g_str_equal(type, "rng-egd")) {
2859 return false;
2863 * return false for concrete netfilters since
2864 * they depend on netdevs already existing
2866 if (g_str_equal(type, "filter-buffer") ||
2867 g_str_equal(type, "filter-dump") ||
2868 g_str_equal(type, "filter-mirror") ||
2869 g_str_equal(type, "filter-redirector") ||
2870 g_str_equal(type, "colo-compare") ||
2871 g_str_equal(type, "filter-rewriter") ||
2872 g_str_equal(type, "filter-replay")) {
2873 return false;
2876 /* Memory allocation by backends needs to be done
2877 * after configure_accelerator() (due to the tcg_enabled()
2878 * checks at memory_region_init_*()).
2880 * Also, allocation of large amounts of memory may delay
2881 * chardev initialization for too long, and trigger timeouts
2882 * on software that waits for a monitor socket to be created
2883 * (e.g. libvirt).
2885 if (g_str_has_prefix(type, "memory-backend-")) {
2886 return false;
2889 return true;
2894 * The remainder of object creation happens after the
2895 * creation of chardev, fsdev, net clients and device data types.
2897 static bool object_create_delayed(const char *type)
2899 return !object_create_initial(type);
2903 static void set_memory_options(uint64_t *ram_slots, ram_addr_t *maxram_size,
2904 MachineClass *mc)
2906 uint64_t sz;
2907 const char *mem_str;
2908 const char *maxmem_str, *slots_str;
2909 const ram_addr_t default_ram_size = mc->default_ram_size;
2910 QemuOpts *opts = qemu_find_opts_singleton("memory");
2911 Location loc;
2913 loc_push_none(&loc);
2914 qemu_opts_loc_restore(opts);
2916 sz = 0;
2917 mem_str = qemu_opt_get(opts, "size");
2918 if (mem_str) {
2919 if (!*mem_str) {
2920 error_report("missing 'size' option value");
2921 exit(EXIT_FAILURE);
2924 sz = qemu_opt_get_size(opts, "size", ram_size);
2926 /* Fix up legacy suffix-less format */
2927 if (g_ascii_isdigit(mem_str[strlen(mem_str) - 1])) {
2928 uint64_t overflow_check = sz;
2930 sz <<= 20;
2931 if ((sz >> 20) != overflow_check) {
2932 error_report("too large 'size' option value");
2933 exit(EXIT_FAILURE);
2938 /* backward compatibility behaviour for case "-m 0" */
2939 if (sz == 0) {
2940 sz = default_ram_size;
2943 sz = QEMU_ALIGN_UP(sz, 8192);
2944 ram_size = sz;
2945 if (ram_size != sz) {
2946 error_report("ram size too large");
2947 exit(EXIT_FAILURE);
2950 /* store value for the future use */
2951 qemu_opt_set_number(opts, "size", ram_size, &error_abort);
2952 *maxram_size = ram_size;
2954 maxmem_str = qemu_opt_get(opts, "maxmem");
2955 slots_str = qemu_opt_get(opts, "slots");
2956 if (maxmem_str && slots_str) {
2957 uint64_t slots;
2959 sz = qemu_opt_get_size(opts, "maxmem", 0);
2960 slots = qemu_opt_get_number(opts, "slots", 0);
2961 if (sz < ram_size) {
2962 error_report("invalid value of -m option maxmem: "
2963 "maximum memory size (0x%" PRIx64 ") must be at least "
2964 "the initial memory size (0x" RAM_ADDR_FMT ")",
2965 sz, ram_size);
2966 exit(EXIT_FAILURE);
2967 } else if (sz > ram_size) {
2968 if (!slots) {
2969 error_report("invalid value of -m option: maxmem was "
2970 "specified, but no hotplug slots were specified");
2971 exit(EXIT_FAILURE);
2973 } else if (slots) {
2974 error_report("invalid value of -m option maxmem: "
2975 "memory slots were specified but maximum memory size "
2976 "(0x%" PRIx64 ") is equal to the initial memory size "
2977 "(0x" RAM_ADDR_FMT ")", sz, ram_size);
2978 exit(EXIT_FAILURE);
2981 *maxram_size = sz;
2982 *ram_slots = slots;
2983 } else if ((!maxmem_str && slots_str) ||
2984 (maxmem_str && !slots_str)) {
2985 error_report("invalid -m option value: missing "
2986 "'%s' option", slots_str ? "maxmem" : "slots");
2987 exit(EXIT_FAILURE);
2990 loc_pop(&loc);
2993 static int global_init_func(void *opaque, QemuOpts *opts, Error **errp)
2995 GlobalProperty *g;
2997 g = g_malloc0(sizeof(*g));
2998 g->driver = qemu_opt_get(opts, "driver");
2999 g->property = qemu_opt_get(opts, "property");
3000 g->value = qemu_opt_get(opts, "value");
3001 g->user_provided = true;
3002 g->errp = &error_fatal;
3003 qdev_prop_register_global(g);
3004 return 0;
3007 static int qemu_read_default_config_file(void)
3009 int ret;
3011 ret = qemu_read_config_file(CONFIG_QEMU_CONFDIR "/qemu.conf");
3012 if (ret < 0 && ret != -ENOENT) {
3013 return ret;
3016 return 0;
3019 static void user_register_global_props(void)
3021 qemu_opts_foreach(qemu_find_opts("global"),
3022 global_init_func, NULL, NULL);
3026 * Note: we should see that these properties are actually having a
3027 * priority: accel < machine < user. This means e.g. when user
3028 * specifies something in "-global", it'll always be used with highest
3029 * priority than either machine/accelerator compat properties.
3031 static void register_global_properties(MachineState *ms)
3033 accel_register_compat_props(ms->accelerator);
3034 machine_register_compat_props(ms);
3035 user_register_global_props();
3038 int main(int argc, char **argv)
3040 int i;
3041 int snapshot, linux_boot;
3042 const char *initrd_filename;
3043 const char *kernel_filename, *kernel_cmdline;
3044 const char *boot_order = NULL;
3045 const char *boot_once = NULL;
3046 DisplayState *ds;
3047 int cyls, heads, secs, translation;
3048 QemuOpts *opts, *machine_opts;
3049 QemuOpts *hda_opts = NULL, *icount_opts = NULL, *accel_opts = NULL;
3050 QemuOptsList *olist;
3051 int optind;
3052 const char *optarg;
3053 const char *loadvm = NULL;
3054 MachineClass *machine_class;
3055 const char *cpu_model;
3056 const char *vga_model = NULL;
3057 const char *qtest_chrdev = NULL;
3058 const char *qtest_log = NULL;
3059 const char *pid_file = NULL;
3060 const char *incoming = NULL;
3061 bool defconfig = true;
3062 bool userconfig = true;
3063 bool nographic = false;
3064 DisplayType display_type = DT_DEFAULT;
3065 int display_remote = 0;
3066 const char *log_mask = NULL;
3067 const char *log_file = NULL;
3068 char *trace_file = NULL;
3069 ram_addr_t maxram_size;
3070 uint64_t ram_slots = 0;
3071 FILE *vmstate_dump_file = NULL;
3072 Error *main_loop_err = NULL;
3073 Error *err = NULL;
3074 bool list_data_dirs = false;
3075 typedef struct BlockdevOptions_queue {
3076 BlockdevOptions *bdo;
3077 Location loc;
3078 QSIMPLEQ_ENTRY(BlockdevOptions_queue) entry;
3079 } BlockdevOptions_queue;
3080 QSIMPLEQ_HEAD(, BlockdevOptions_queue) bdo_queue
3081 = QSIMPLEQ_HEAD_INITIALIZER(bdo_queue);
3083 module_call_init(MODULE_INIT_TRACE);
3085 qemu_init_cpu_list();
3086 qemu_init_cpu_loop();
3087 qemu_mutex_lock_iothread();
3089 atexit(qemu_run_exit_notifiers);
3090 error_set_progname(argv[0]);
3091 qemu_init_exec_dir(argv[0]);
3093 module_call_init(MODULE_INIT_QOM);
3094 monitor_init_qmp_commands();
3096 qemu_add_opts(&qemu_drive_opts);
3097 qemu_add_drive_opts(&qemu_legacy_drive_opts);
3098 qemu_add_drive_opts(&qemu_common_drive_opts);
3099 qemu_add_drive_opts(&qemu_drive_opts);
3100 qemu_add_drive_opts(&bdrv_runtime_opts);
3101 qemu_add_opts(&qemu_chardev_opts);
3102 qemu_add_opts(&qemu_device_opts);
3103 qemu_add_opts(&qemu_netdev_opts);
3104 qemu_add_opts(&qemu_net_opts);
3105 qemu_add_opts(&qemu_rtc_opts);
3106 qemu_add_opts(&qemu_global_opts);
3107 qemu_add_opts(&qemu_mon_opts);
3108 qemu_add_opts(&qemu_trace_opts);
3109 qemu_add_opts(&qemu_option_rom_opts);
3110 qemu_add_opts(&qemu_machine_opts);
3111 qemu_add_opts(&qemu_accel_opts);
3112 qemu_add_opts(&qemu_mem_opts);
3113 qemu_add_opts(&qemu_smp_opts);
3114 qemu_add_opts(&qemu_boot_opts);
3115 qemu_add_opts(&qemu_sandbox_opts);
3116 qemu_add_opts(&qemu_add_fd_opts);
3117 qemu_add_opts(&qemu_object_opts);
3118 qemu_add_opts(&qemu_tpmdev_opts);
3119 qemu_add_opts(&qemu_realtime_opts);
3120 qemu_add_opts(&qemu_msg_opts);
3121 qemu_add_opts(&qemu_name_opts);
3122 qemu_add_opts(&qemu_numa_opts);
3123 qemu_add_opts(&qemu_icount_opts);
3124 qemu_add_opts(&qemu_semihosting_config_opts);
3125 qemu_add_opts(&qemu_fw_cfg_opts);
3126 module_call_init(MODULE_INIT_OPTS);
3128 runstate_init();
3130 if (qcrypto_init(&err) < 0) {
3131 error_reportf_err(err, "cannot initialize crypto: ");
3132 exit(1);
3134 rtc_clock = QEMU_CLOCK_HOST;
3136 QLIST_INIT (&vm_change_state_head);
3137 os_setup_early_signal_handling();
3139 cpu_model = NULL;
3140 snapshot = 0;
3141 cyls = heads = secs = 0;
3142 translation = BIOS_ATA_TRANSLATION_AUTO;
3144 nb_nics = 0;
3146 bdrv_init_with_whitelist();
3148 autostart = 1;
3150 /* first pass of option parsing */
3151 optind = 1;
3152 while (optind < argc) {
3153 if (argv[optind][0] != '-') {
3154 /* disk image */
3155 optind++;
3156 } else {
3157 const QEMUOption *popt;
3159 popt = lookup_opt(argc, argv, &optarg, &optind);
3160 switch (popt->index) {
3161 case QEMU_OPTION_nodefconfig:
3162 defconfig = false;
3163 break;
3164 case QEMU_OPTION_nouserconfig:
3165 userconfig = false;
3166 break;
3171 if (defconfig && userconfig) {
3172 if (qemu_read_default_config_file() < 0) {
3173 exit(1);
3177 /* second pass of option parsing */
3178 optind = 1;
3179 for(;;) {
3180 if (optind >= argc)
3181 break;
3182 if (argv[optind][0] != '-') {
3183 hda_opts = drive_add(IF_DEFAULT, 0, argv[optind++], HD_OPTS);
3184 } else {
3185 const QEMUOption *popt;
3187 popt = lookup_opt(argc, argv, &optarg, &optind);
3188 if (!(popt->arch_mask & arch_type)) {
3189 error_report("Option not supported for this target");
3190 exit(1);
3192 switch(popt->index) {
3193 case QEMU_OPTION_no_kvm_irqchip: {
3194 olist = qemu_find_opts("machine");
3195 qemu_opts_parse_noisily(olist, "kernel_irqchip=off", false);
3196 break;
3198 case QEMU_OPTION_cpu:
3199 /* hw initialization will check this */
3200 cpu_model = optarg;
3201 break;
3202 case QEMU_OPTION_hda:
3204 char buf[256];
3205 if (cyls == 0)
3206 snprintf(buf, sizeof(buf), "%s", HD_OPTS);
3207 else
3208 snprintf(buf, sizeof(buf),
3209 "%s,cyls=%d,heads=%d,secs=%d%s",
3210 HD_OPTS , cyls, heads, secs,
3211 translation == BIOS_ATA_TRANSLATION_LBA ?
3212 ",trans=lba" :
3213 translation == BIOS_ATA_TRANSLATION_NONE ?
3214 ",trans=none" : "");
3215 drive_add(IF_DEFAULT, 0, optarg, buf);
3216 break;
3218 case QEMU_OPTION_hdb:
3219 case QEMU_OPTION_hdc:
3220 case QEMU_OPTION_hdd:
3221 drive_add(IF_DEFAULT, popt->index - QEMU_OPTION_hda, optarg,
3222 HD_OPTS);
3223 break;
3224 case QEMU_OPTION_blockdev:
3226 Visitor *v;
3227 BlockdevOptions_queue *bdo;
3229 v = qobject_input_visitor_new_str(optarg, "driver", &err);
3230 if (!v) {
3231 error_report_err(err);
3232 exit(1);
3235 bdo = g_new(BlockdevOptions_queue, 1);
3236 visit_type_BlockdevOptions(v, NULL, &bdo->bdo,
3237 &error_fatal);
3238 visit_free(v);
3239 loc_save(&bdo->loc);
3240 QSIMPLEQ_INSERT_TAIL(&bdo_queue, bdo, entry);
3241 break;
3243 case QEMU_OPTION_drive:
3244 if (drive_def(optarg) == NULL) {
3245 exit(1);
3247 break;
3248 case QEMU_OPTION_set:
3249 if (qemu_set_option(optarg) != 0)
3250 exit(1);
3251 break;
3252 case QEMU_OPTION_global:
3253 if (qemu_global_option(optarg) != 0)
3254 exit(1);
3255 break;
3256 case QEMU_OPTION_mtdblock:
3257 drive_add(IF_MTD, -1, optarg, MTD_OPTS);
3258 break;
3259 case QEMU_OPTION_sd:
3260 drive_add(IF_SD, -1, optarg, SD_OPTS);
3261 break;
3262 case QEMU_OPTION_pflash:
3263 drive_add(IF_PFLASH, -1, optarg, PFLASH_OPTS);
3264 break;
3265 case QEMU_OPTION_snapshot:
3266 snapshot = 1;
3267 break;
3268 case QEMU_OPTION_hdachs:
3270 const char *p;
3271 p = optarg;
3272 cyls = strtol(p, (char **)&p, 0);
3273 if (cyls < 1 || cyls > 16383)
3274 goto chs_fail;
3275 if (*p != ',')
3276 goto chs_fail;
3277 p++;
3278 heads = strtol(p, (char **)&p, 0);
3279 if (heads < 1 || heads > 16)
3280 goto chs_fail;
3281 if (*p != ',')
3282 goto chs_fail;
3283 p++;
3284 secs = strtol(p, (char **)&p, 0);
3285 if (secs < 1 || secs > 63)
3286 goto chs_fail;
3287 if (*p == ',') {
3288 p++;
3289 if (!strcmp(p, "large")) {
3290 translation = BIOS_ATA_TRANSLATION_LARGE;
3291 } else if (!strcmp(p, "rechs")) {
3292 translation = BIOS_ATA_TRANSLATION_RECHS;
3293 } else if (!strcmp(p, "none")) {
3294 translation = BIOS_ATA_TRANSLATION_NONE;
3295 } else if (!strcmp(p, "lba")) {
3296 translation = BIOS_ATA_TRANSLATION_LBA;
3297 } else if (!strcmp(p, "auto")) {
3298 translation = BIOS_ATA_TRANSLATION_AUTO;
3299 } else {
3300 goto chs_fail;
3302 } else if (*p != '\0') {
3303 chs_fail:
3304 error_report("invalid physical CHS format");
3305 exit(1);
3307 if (hda_opts != NULL) {
3308 qemu_opt_set_number(hda_opts, "cyls", cyls,
3309 &error_abort);
3310 qemu_opt_set_number(hda_opts, "heads", heads,
3311 &error_abort);
3312 qemu_opt_set_number(hda_opts, "secs", secs,
3313 &error_abort);
3314 if (translation == BIOS_ATA_TRANSLATION_LARGE) {
3315 qemu_opt_set(hda_opts, "trans", "large",
3316 &error_abort);
3317 } else if (translation == BIOS_ATA_TRANSLATION_RECHS) {
3318 qemu_opt_set(hda_opts, "trans", "rechs",
3319 &error_abort);
3320 } else if (translation == BIOS_ATA_TRANSLATION_LBA) {
3321 qemu_opt_set(hda_opts, "trans", "lba",
3322 &error_abort);
3323 } else if (translation == BIOS_ATA_TRANSLATION_NONE) {
3324 qemu_opt_set(hda_opts, "trans", "none",
3325 &error_abort);
3329 error_report("'-hdachs' is deprecated, please use '-device"
3330 " ide-hd,cyls=c,heads=h,secs=s,...' instead");
3331 break;
3332 case QEMU_OPTION_numa:
3333 opts = qemu_opts_parse_noisily(qemu_find_opts("numa"),
3334 optarg, true);
3335 if (!opts) {
3336 exit(1);
3338 break;
3339 case QEMU_OPTION_display:
3340 display_type = select_display(optarg);
3341 break;
3342 case QEMU_OPTION_nographic:
3343 olist = qemu_find_opts("machine");
3344 qemu_opts_parse_noisily(olist, "graphics=off", false);
3345 nographic = true;
3346 display_type = DT_NONE;
3347 break;
3348 case QEMU_OPTION_curses:
3349 #ifdef CONFIG_CURSES
3350 display_type = DT_CURSES;
3351 #else
3352 error_report("curses support is disabled");
3353 exit(1);
3354 #endif
3355 break;
3356 case QEMU_OPTION_portrait:
3357 graphic_rotate = 90;
3358 break;
3359 case QEMU_OPTION_rotate:
3360 graphic_rotate = strtol(optarg, (char **) &optarg, 10);
3361 if (graphic_rotate != 0 && graphic_rotate != 90 &&
3362 graphic_rotate != 180 && graphic_rotate != 270) {
3363 error_report("only 90, 180, 270 deg rotation is available");
3364 exit(1);
3366 break;
3367 case QEMU_OPTION_kernel:
3368 qemu_opts_set(qemu_find_opts("machine"), 0, "kernel", optarg,
3369 &error_abort);
3370 break;
3371 case QEMU_OPTION_initrd:
3372 qemu_opts_set(qemu_find_opts("machine"), 0, "initrd", optarg,
3373 &error_abort);
3374 break;
3375 case QEMU_OPTION_append:
3376 qemu_opts_set(qemu_find_opts("machine"), 0, "append", optarg,
3377 &error_abort);
3378 break;
3379 case QEMU_OPTION_dtb:
3380 qemu_opts_set(qemu_find_opts("machine"), 0, "dtb", optarg,
3381 &error_abort);
3382 break;
3383 case QEMU_OPTION_cdrom:
3384 drive_add(IF_DEFAULT, 2, optarg, CDROM_OPTS);
3385 break;
3386 case QEMU_OPTION_boot:
3387 opts = qemu_opts_parse_noisily(qemu_find_opts("boot-opts"),
3388 optarg, true);
3389 if (!opts) {
3390 exit(1);
3392 break;
3393 case QEMU_OPTION_fda:
3394 case QEMU_OPTION_fdb:
3395 drive_add(IF_FLOPPY, popt->index - QEMU_OPTION_fda,
3396 optarg, FD_OPTS);
3397 break;
3398 case QEMU_OPTION_no_fd_bootchk:
3399 fd_bootchk = 0;
3400 break;
3401 case QEMU_OPTION_netdev:
3402 default_net = 0;
3403 if (net_client_parse(qemu_find_opts("netdev"), optarg) == -1) {
3404 exit(1);
3406 break;
3407 case QEMU_OPTION_net:
3408 default_net = 0;
3409 if (net_client_parse(qemu_find_opts("net"), optarg) == -1) {
3410 exit(1);
3412 break;
3413 #ifdef CONFIG_LIBISCSI
3414 case QEMU_OPTION_iscsi:
3415 opts = qemu_opts_parse_noisily(qemu_find_opts("iscsi"),
3416 optarg, false);
3417 if (!opts) {
3418 exit(1);
3420 break;
3421 #endif
3422 #ifdef CONFIG_SLIRP
3423 case QEMU_OPTION_tftp:
3424 error_report("The -tftp option is deprecated. "
3425 "Please use '-netdev user,tftp=...' instead.");
3426 legacy_tftp_prefix = optarg;
3427 break;
3428 case QEMU_OPTION_bootp:
3429 error_report("The -bootp option is deprecated. "
3430 "Please use '-netdev user,bootfile=...' instead.");
3431 legacy_bootp_filename = optarg;
3432 break;
3433 case QEMU_OPTION_redir:
3434 error_report("The -redir option is deprecated. "
3435 "Please use '-netdev user,hostfwd=...' instead.");
3436 if (net_slirp_redir(optarg) < 0)
3437 exit(1);
3438 break;
3439 #endif
3440 case QEMU_OPTION_bt:
3441 add_device_config(DEV_BT, optarg);
3442 break;
3443 case QEMU_OPTION_audio_help:
3444 AUD_help ();
3445 exit (0);
3446 break;
3447 case QEMU_OPTION_soundhw:
3448 select_soundhw (optarg);
3449 break;
3450 case QEMU_OPTION_h:
3451 help(0);
3452 break;
3453 case QEMU_OPTION_version:
3454 version();
3455 exit(0);
3456 break;
3457 case QEMU_OPTION_m:
3458 opts = qemu_opts_parse_noisily(qemu_find_opts("memory"),
3459 optarg, true);
3460 if (!opts) {
3461 exit(EXIT_FAILURE);
3463 break;
3464 #ifdef CONFIG_TPM
3465 case QEMU_OPTION_tpmdev:
3466 if (tpm_config_parse(qemu_find_opts("tpmdev"), optarg) < 0) {
3467 exit(1);
3469 break;
3470 #endif
3471 case QEMU_OPTION_mempath:
3472 mem_path = optarg;
3473 break;
3474 case QEMU_OPTION_mem_prealloc:
3475 mem_prealloc = 1;
3476 break;
3477 case QEMU_OPTION_d:
3478 log_mask = optarg;
3479 break;
3480 case QEMU_OPTION_D:
3481 log_file = optarg;
3482 break;
3483 case QEMU_OPTION_DFILTER:
3484 qemu_set_dfilter_ranges(optarg, &error_fatal);
3485 break;
3486 case QEMU_OPTION_s:
3487 add_device_config(DEV_GDB, "tcp::" DEFAULT_GDBSTUB_PORT);
3488 break;
3489 case QEMU_OPTION_gdb:
3490 add_device_config(DEV_GDB, optarg);
3491 break;
3492 case QEMU_OPTION_L:
3493 if (is_help_option(optarg)) {
3494 list_data_dirs = true;
3495 } else if (data_dir_idx < ARRAY_SIZE(data_dir)) {
3496 data_dir[data_dir_idx++] = optarg;
3498 break;
3499 case QEMU_OPTION_bios:
3500 qemu_opts_set(qemu_find_opts("machine"), 0, "firmware", optarg,
3501 &error_abort);
3502 break;
3503 case QEMU_OPTION_singlestep:
3504 singlestep = 1;
3505 break;
3506 case QEMU_OPTION_S:
3507 autostart = 0;
3508 break;
3509 case QEMU_OPTION_k:
3510 keyboard_layout = optarg;
3511 break;
3512 case QEMU_OPTION_localtime:
3513 rtc_utc = 0;
3514 break;
3515 case QEMU_OPTION_vga:
3516 vga_model = optarg;
3517 default_vga = 0;
3518 break;
3519 case QEMU_OPTION_g:
3521 const char *p;
3522 int w, h, depth;
3523 p = optarg;
3524 w = strtol(p, (char **)&p, 10);
3525 if (w <= 0) {
3526 graphic_error:
3527 error_report("invalid resolution or depth");
3528 exit(1);
3530 if (*p != 'x')
3531 goto graphic_error;
3532 p++;
3533 h = strtol(p, (char **)&p, 10);
3534 if (h <= 0)
3535 goto graphic_error;
3536 if (*p == 'x') {
3537 p++;
3538 depth = strtol(p, (char **)&p, 10);
3539 if (depth != 8 && depth != 15 && depth != 16 &&
3540 depth != 24 && depth != 32)
3541 goto graphic_error;
3542 } else if (*p == '\0') {
3543 depth = graphic_depth;
3544 } else {
3545 goto graphic_error;
3548 graphic_width = w;
3549 graphic_height = h;
3550 graphic_depth = depth;
3552 break;
3553 case QEMU_OPTION_echr:
3555 char *r;
3556 term_escape_char = strtol(optarg, &r, 0);
3557 if (r == optarg)
3558 printf("Bad argument to echr\n");
3559 break;
3561 case QEMU_OPTION_monitor:
3562 default_monitor = 0;
3563 if (strncmp(optarg, "none", 4)) {
3564 monitor_parse(optarg, "readline", false);
3566 break;
3567 case QEMU_OPTION_qmp:
3568 monitor_parse(optarg, "control", false);
3569 default_monitor = 0;
3570 break;
3571 case QEMU_OPTION_qmp_pretty:
3572 monitor_parse(optarg, "control", true);
3573 default_monitor = 0;
3574 break;
3575 case QEMU_OPTION_mon:
3576 opts = qemu_opts_parse_noisily(qemu_find_opts("mon"), optarg,
3577 true);
3578 if (!opts) {
3579 exit(1);
3581 default_monitor = 0;
3582 break;
3583 case QEMU_OPTION_chardev:
3584 opts = qemu_opts_parse_noisily(qemu_find_opts("chardev"),
3585 optarg, true);
3586 if (!opts) {
3587 exit(1);
3589 break;
3590 case QEMU_OPTION_fsdev:
3591 olist = qemu_find_opts("fsdev");
3592 if (!olist) {
3593 error_report("fsdev support is disabled");
3594 exit(1);
3596 opts = qemu_opts_parse_noisily(olist, optarg, true);
3597 if (!opts) {
3598 exit(1);
3600 break;
3601 case QEMU_OPTION_virtfs: {
3602 QemuOpts *fsdev;
3603 QemuOpts *device;
3604 const char *writeout, *sock_fd, *socket;
3606 olist = qemu_find_opts("virtfs");
3607 if (!olist) {
3608 error_report("virtfs support is disabled");
3609 exit(1);
3611 opts = qemu_opts_parse_noisily(olist, optarg, true);
3612 if (!opts) {
3613 exit(1);
3616 if (qemu_opt_get(opts, "fsdriver") == NULL ||
3617 qemu_opt_get(opts, "mount_tag") == NULL) {
3618 error_report("Usage: -virtfs fsdriver,mount_tag=tag");
3619 exit(1);
3621 fsdev = qemu_opts_create(qemu_find_opts("fsdev"),
3622 qemu_opts_id(opts) ?:
3623 qemu_opt_get(opts, "mount_tag"),
3624 1, NULL);
3625 if (!fsdev) {
3626 error_report("duplicate or invalid fsdev id: %s",
3627 qemu_opt_get(opts, "mount_tag"));
3628 exit(1);
3631 writeout = qemu_opt_get(opts, "writeout");
3632 if (writeout) {
3633 #ifdef CONFIG_SYNC_FILE_RANGE
3634 qemu_opt_set(fsdev, "writeout", writeout, &error_abort);
3635 #else
3636 error_report("writeout=immediate not supported "
3637 "on this platform");
3638 exit(1);
3639 #endif
3641 qemu_opt_set(fsdev, "fsdriver",
3642 qemu_opt_get(opts, "fsdriver"), &error_abort);
3643 qemu_opt_set(fsdev, "path", qemu_opt_get(opts, "path"),
3644 &error_abort);
3645 qemu_opt_set(fsdev, "security_model",
3646 qemu_opt_get(opts, "security_model"),
3647 &error_abort);
3648 socket = qemu_opt_get(opts, "socket");
3649 if (socket) {
3650 qemu_opt_set(fsdev, "socket", socket, &error_abort);
3652 sock_fd = qemu_opt_get(opts, "sock_fd");
3653 if (sock_fd) {
3654 qemu_opt_set(fsdev, "sock_fd", sock_fd, &error_abort);
3657 qemu_opt_set_bool(fsdev, "readonly",
3658 qemu_opt_get_bool(opts, "readonly", 0),
3659 &error_abort);
3660 device = qemu_opts_create(qemu_find_opts("device"), NULL, 0,
3661 &error_abort);
3662 qemu_opt_set(device, "driver", "virtio-9p-pci", &error_abort);
3663 qemu_opt_set(device, "fsdev",
3664 qemu_opts_id(fsdev), &error_abort);
3665 qemu_opt_set(device, "mount_tag",
3666 qemu_opt_get(opts, "mount_tag"), &error_abort);
3667 break;
3669 case QEMU_OPTION_virtfs_synth: {
3670 QemuOpts *fsdev;
3671 QemuOpts *device;
3673 fsdev = qemu_opts_create(qemu_find_opts("fsdev"), "v_synth",
3674 1, NULL);
3675 if (!fsdev) {
3676 error_report("duplicate option: %s", "virtfs_synth");
3677 exit(1);
3679 qemu_opt_set(fsdev, "fsdriver", "synth", &error_abort);
3681 device = qemu_opts_create(qemu_find_opts("device"), NULL, 0,
3682 &error_abort);
3683 qemu_opt_set(device, "driver", "virtio-9p-pci", &error_abort);
3684 qemu_opt_set(device, "fsdev", "v_synth", &error_abort);
3685 qemu_opt_set(device, "mount_tag", "v_synth", &error_abort);
3686 break;
3688 case QEMU_OPTION_serial:
3689 add_device_config(DEV_SERIAL, optarg);
3690 default_serial = 0;
3691 if (strncmp(optarg, "mon:", 4) == 0) {
3692 default_monitor = 0;
3694 break;
3695 case QEMU_OPTION_watchdog:
3696 if (watchdog) {
3697 error_report("only one watchdog option may be given");
3698 return 1;
3700 watchdog = optarg;
3701 break;
3702 case QEMU_OPTION_watchdog_action:
3703 if (select_watchdog_action(optarg) == -1) {
3704 error_report("unknown -watchdog-action parameter");
3705 exit(1);
3707 break;
3708 case QEMU_OPTION_virtiocon:
3709 add_device_config(DEV_VIRTCON, optarg);
3710 default_virtcon = 0;
3711 if (strncmp(optarg, "mon:", 4) == 0) {
3712 default_monitor = 0;
3714 break;
3715 case QEMU_OPTION_parallel:
3716 add_device_config(DEV_PARALLEL, optarg);
3717 default_parallel = 0;
3718 if (strncmp(optarg, "mon:", 4) == 0) {
3719 default_monitor = 0;
3721 break;
3722 case QEMU_OPTION_debugcon:
3723 add_device_config(DEV_DEBUGCON, optarg);
3724 break;
3725 case QEMU_OPTION_loadvm:
3726 loadvm = optarg;
3727 break;
3728 case QEMU_OPTION_full_screen:
3729 full_screen = 1;
3730 break;
3731 case QEMU_OPTION_no_frame:
3732 no_frame = 1;
3733 break;
3734 case QEMU_OPTION_alt_grab:
3735 alt_grab = 1;
3736 break;
3737 case QEMU_OPTION_ctrl_grab:
3738 ctrl_grab = 1;
3739 break;
3740 case QEMU_OPTION_no_quit:
3741 no_quit = 1;
3742 break;
3743 case QEMU_OPTION_sdl:
3744 #ifdef CONFIG_SDL
3745 display_type = DT_SDL;
3746 break;
3747 #else
3748 error_report("SDL support is disabled");
3749 exit(1);
3750 #endif
3751 case QEMU_OPTION_pidfile:
3752 pid_file = optarg;
3753 break;
3754 case QEMU_OPTION_win2k_hack:
3755 win2k_install_hack = 1;
3756 break;
3757 case QEMU_OPTION_rtc_td_hack: {
3758 static GlobalProperty slew_lost_ticks = {
3759 .driver = "mc146818rtc",
3760 .property = "lost_tick_policy",
3761 .value = "slew",
3764 qdev_prop_register_global(&slew_lost_ticks);
3765 break;
3767 case QEMU_OPTION_acpitable:
3768 opts = qemu_opts_parse_noisily(qemu_find_opts("acpi"),
3769 optarg, true);
3770 if (!opts) {
3771 exit(1);
3773 acpi_table_add(opts, &error_fatal);
3774 break;
3775 case QEMU_OPTION_smbios:
3776 opts = qemu_opts_parse_noisily(qemu_find_opts("smbios"),
3777 optarg, false);
3778 if (!opts) {
3779 exit(1);
3781 smbios_entry_add(opts, &error_fatal);
3782 break;
3783 case QEMU_OPTION_fwcfg:
3784 opts = qemu_opts_parse_noisily(qemu_find_opts("fw_cfg"),
3785 optarg, true);
3786 if (opts == NULL) {
3787 exit(1);
3789 break;
3790 case QEMU_OPTION_enable_kvm:
3791 olist = qemu_find_opts("machine");
3792 qemu_opts_parse_noisily(olist, "accel=kvm", false);
3793 break;
3794 case QEMU_OPTION_enable_hax:
3795 olist = qemu_find_opts("machine");
3796 qemu_opts_parse_noisily(olist, "accel=hax", false);
3797 break;
3798 case QEMU_OPTION_M:
3799 case QEMU_OPTION_machine:
3800 olist = qemu_find_opts("machine");
3801 opts = qemu_opts_parse_noisily(olist, optarg, true);
3802 if (!opts) {
3803 exit(1);
3805 break;
3806 case QEMU_OPTION_no_kvm:
3807 olist = qemu_find_opts("machine");
3808 qemu_opts_parse_noisily(olist, "accel=tcg", false);
3809 break;
3810 case QEMU_OPTION_no_kvm_pit: {
3811 warn_report("ignoring deprecated option");
3812 break;
3814 case QEMU_OPTION_no_kvm_pit_reinjection: {
3815 static GlobalProperty kvm_pit_lost_tick_policy = {
3816 .driver = "kvm-pit",
3817 .property = "lost_tick_policy",
3818 .value = "discard",
3821 warn_report("deprecated, replaced by "
3822 "-global kvm-pit.lost_tick_policy=discard");
3823 qdev_prop_register_global(&kvm_pit_lost_tick_policy);
3824 break;
3826 case QEMU_OPTION_accel:
3827 accel_opts = qemu_opts_parse_noisily(qemu_find_opts("accel"),
3828 optarg, true);
3829 optarg = qemu_opt_get(accel_opts, "accel");
3830 if (!optarg || is_help_option(optarg)) {
3831 error_printf("Possible accelerators: kvm, xen, hax, tcg\n");
3832 exit(0);
3834 opts = qemu_opts_create(qemu_find_opts("machine"), NULL,
3835 false, &error_abort);
3836 qemu_opt_set(opts, "accel", optarg, &error_abort);
3837 break;
3838 case QEMU_OPTION_usb:
3839 olist = qemu_find_opts("machine");
3840 qemu_opts_parse_noisily(olist, "usb=on", false);
3841 break;
3842 case QEMU_OPTION_usbdevice:
3843 error_report("'-usbdevice' is deprecated, please use "
3844 "'-device usb-...' instead");
3845 olist = qemu_find_opts("machine");
3846 qemu_opts_parse_noisily(olist, "usb=on", false);
3847 add_device_config(DEV_USB, optarg);
3848 break;
3849 case QEMU_OPTION_device:
3850 if (!qemu_opts_parse_noisily(qemu_find_opts("device"),
3851 optarg, true)) {
3852 exit(1);
3854 break;
3855 case QEMU_OPTION_smp:
3856 if (!qemu_opts_parse_noisily(qemu_find_opts("smp-opts"),
3857 optarg, true)) {
3858 exit(1);
3860 break;
3861 case QEMU_OPTION_vnc:
3862 vnc_parse(optarg, &error_fatal);
3863 break;
3864 case QEMU_OPTION_no_acpi:
3865 acpi_enabled = 0;
3866 break;
3867 case QEMU_OPTION_no_hpet:
3868 no_hpet = 1;
3869 break;
3870 case QEMU_OPTION_balloon:
3871 if (balloon_parse(optarg) < 0) {
3872 error_report("unknown -balloon argument %s", optarg);
3873 exit(1);
3875 break;
3876 case QEMU_OPTION_no_reboot:
3877 no_reboot = 1;
3878 break;
3879 case QEMU_OPTION_no_shutdown:
3880 no_shutdown = 1;
3881 break;
3882 case QEMU_OPTION_show_cursor:
3883 cursor_hide = 0;
3884 break;
3885 case QEMU_OPTION_uuid:
3886 if (qemu_uuid_parse(optarg, &qemu_uuid) < 0) {
3887 error_report("failed to parse UUID string: wrong format");
3888 exit(1);
3890 qemu_uuid_set = true;
3891 break;
3892 case QEMU_OPTION_option_rom:
3893 if (nb_option_roms >= MAX_OPTION_ROMS) {
3894 error_report("too many option ROMs");
3895 exit(1);
3897 opts = qemu_opts_parse_noisily(qemu_find_opts("option-rom"),
3898 optarg, true);
3899 if (!opts) {
3900 exit(1);
3902 option_rom[nb_option_roms].name = qemu_opt_get(opts, "romfile");
3903 option_rom[nb_option_roms].bootindex =
3904 qemu_opt_get_number(opts, "bootindex", -1);
3905 if (!option_rom[nb_option_roms].name) {
3906 error_report("Option ROM file is not specified");
3907 exit(1);
3909 nb_option_roms++;
3910 break;
3911 case QEMU_OPTION_semihosting:
3912 semihosting.enabled = true;
3913 semihosting.target = SEMIHOSTING_TARGET_AUTO;
3914 break;
3915 case QEMU_OPTION_semihosting_config:
3916 semihosting.enabled = true;
3917 opts = qemu_opts_parse_noisily(qemu_find_opts("semihosting-config"),
3918 optarg, false);
3919 if (opts != NULL) {
3920 semihosting.enabled = qemu_opt_get_bool(opts, "enable",
3921 true);
3922 const char *target = qemu_opt_get(opts, "target");
3923 if (target != NULL) {
3924 if (strcmp("native", target) == 0) {
3925 semihosting.target = SEMIHOSTING_TARGET_NATIVE;
3926 } else if (strcmp("gdb", target) == 0) {
3927 semihosting.target = SEMIHOSTING_TARGET_GDB;
3928 } else if (strcmp("auto", target) == 0) {
3929 semihosting.target = SEMIHOSTING_TARGET_AUTO;
3930 } else {
3931 error_report("unsupported semihosting-config %s",
3932 optarg);
3933 exit(1);
3935 } else {
3936 semihosting.target = SEMIHOSTING_TARGET_AUTO;
3938 /* Set semihosting argument count and vector */
3939 qemu_opt_foreach(opts, add_semihosting_arg,
3940 &semihosting, NULL);
3941 } else {
3942 error_report("unsupported semihosting-config %s", optarg);
3943 exit(1);
3945 break;
3946 case QEMU_OPTION_tdf:
3947 warn_report("ignoring deprecated option");
3948 break;
3949 case QEMU_OPTION_name:
3950 opts = qemu_opts_parse_noisily(qemu_find_opts("name"),
3951 optarg, true);
3952 if (!opts) {
3953 exit(1);
3955 break;
3956 case QEMU_OPTION_prom_env:
3957 if (nb_prom_envs >= MAX_PROM_ENVS) {
3958 error_report("too many prom variables");
3959 exit(1);
3961 prom_envs[nb_prom_envs] = optarg;
3962 nb_prom_envs++;
3963 break;
3964 case QEMU_OPTION_old_param:
3965 old_param = 1;
3966 break;
3967 case QEMU_OPTION_clock:
3968 /* Clock options no longer exist. Keep this option for
3969 * backward compatibility.
3971 break;
3972 case QEMU_OPTION_startdate:
3973 configure_rtc_date_offset(optarg, 1);
3974 break;
3975 case QEMU_OPTION_rtc:
3976 opts = qemu_opts_parse_noisily(qemu_find_opts("rtc"), optarg,
3977 false);
3978 if (!opts) {
3979 exit(1);
3981 configure_rtc(opts);
3982 break;
3983 case QEMU_OPTION_tb_size:
3984 #ifndef CONFIG_TCG
3985 error_report("TCG is disabled");
3986 exit(1);
3987 #endif
3988 if (qemu_strtoul(optarg, NULL, 0, &tcg_tb_size) < 0) {
3989 error_report("Invalid argument to -tb-size");
3990 exit(1);
3992 break;
3993 case QEMU_OPTION_icount:
3994 icount_opts = qemu_opts_parse_noisily(qemu_find_opts("icount"),
3995 optarg, true);
3996 if (!icount_opts) {
3997 exit(1);
3999 break;
4000 case QEMU_OPTION_incoming:
4001 if (!incoming) {
4002 runstate_set(RUN_STATE_INMIGRATE);
4004 incoming = optarg;
4005 break;
4006 case QEMU_OPTION_only_migratable:
4008 * TODO: we can remove this option one day, and we
4009 * should all use:
4011 * "-global migration.only-migratable=true"
4013 qemu_global_option("migration.only-migratable=true");
4014 break;
4015 case QEMU_OPTION_nodefaults:
4016 has_defaults = 0;
4017 break;
4018 case QEMU_OPTION_xen_domid:
4019 if (!(xen_available())) {
4020 error_report("Option not supported for this target");
4021 exit(1);
4023 xen_domid = atoi(optarg);
4024 break;
4025 case QEMU_OPTION_xen_create:
4026 if (!(xen_available())) {
4027 error_report("Option not supported for this target");
4028 exit(1);
4030 xen_mode = XEN_CREATE;
4031 break;
4032 case QEMU_OPTION_xen_attach:
4033 if (!(xen_available())) {
4034 error_report("Option not supported for this target");
4035 exit(1);
4037 xen_mode = XEN_ATTACH;
4038 break;
4039 case QEMU_OPTION_xen_domid_restrict:
4040 if (!(xen_available())) {
4041 error_report("Option not supported for this target");
4042 exit(1);
4044 xen_domid_restrict = true;
4045 break;
4046 case QEMU_OPTION_trace:
4047 g_free(trace_file);
4048 trace_file = trace_opt_parse(optarg);
4049 break;
4050 case QEMU_OPTION_trace_unassigned:
4051 trace_unassigned = true;
4052 break;
4053 case QEMU_OPTION_readconfig:
4055 int ret = qemu_read_config_file(optarg);
4056 if (ret < 0) {
4057 error_report("read config %s: %s", optarg,
4058 strerror(-ret));
4059 exit(1);
4061 break;
4063 case QEMU_OPTION_spice:
4064 olist = qemu_find_opts("spice");
4065 if (!olist) {
4066 error_report("spice support is disabled");
4067 exit(1);
4069 opts = qemu_opts_parse_noisily(olist, optarg, false);
4070 if (!opts) {
4071 exit(1);
4073 display_remote++;
4074 break;
4075 case QEMU_OPTION_writeconfig:
4077 FILE *fp;
4078 if (strcmp(optarg, "-") == 0) {
4079 fp = stdout;
4080 } else {
4081 fp = fopen(optarg, "w");
4082 if (fp == NULL) {
4083 error_report("open %s: %s", optarg,
4084 strerror(errno));
4085 exit(1);
4088 qemu_config_write(fp);
4089 if (fp != stdout) {
4090 fclose(fp);
4092 break;
4094 case QEMU_OPTION_qtest:
4095 qtest_chrdev = optarg;
4096 break;
4097 case QEMU_OPTION_qtest_log:
4098 qtest_log = optarg;
4099 break;
4100 case QEMU_OPTION_sandbox:
4101 opts = qemu_opts_parse_noisily(qemu_find_opts("sandbox"),
4102 optarg, true);
4103 if (!opts) {
4104 exit(1);
4106 break;
4107 case QEMU_OPTION_add_fd:
4108 #ifndef _WIN32
4109 opts = qemu_opts_parse_noisily(qemu_find_opts("add-fd"),
4110 optarg, false);
4111 if (!opts) {
4112 exit(1);
4114 #else
4115 error_report("File descriptor passing is disabled on this "
4116 "platform");
4117 exit(1);
4118 #endif
4119 break;
4120 case QEMU_OPTION_object:
4121 opts = qemu_opts_parse_noisily(qemu_find_opts("object"),
4122 optarg, true);
4123 if (!opts) {
4124 exit(1);
4126 break;
4127 case QEMU_OPTION_realtime:
4128 opts = qemu_opts_parse_noisily(qemu_find_opts("realtime"),
4129 optarg, false);
4130 if (!opts) {
4131 exit(1);
4133 enable_mlock = qemu_opt_get_bool(opts, "mlock", true);
4134 break;
4135 case QEMU_OPTION_msg:
4136 opts = qemu_opts_parse_noisily(qemu_find_opts("msg"), optarg,
4137 false);
4138 if (!opts) {
4139 exit(1);
4141 configure_msg(opts);
4142 break;
4143 case QEMU_OPTION_dump_vmstate:
4144 if (vmstate_dump_file) {
4145 error_report("only one '-dump-vmstate' "
4146 "option may be given");
4147 exit(1);
4149 vmstate_dump_file = fopen(optarg, "w");
4150 if (vmstate_dump_file == NULL) {
4151 error_report("open %s: %s", optarg, strerror(errno));
4152 exit(1);
4154 break;
4155 default:
4156 os_parse_cmd_args(popt->index, optarg);
4161 * Clear error location left behind by the loop.
4162 * Best done right after the loop. Do not insert code here!
4164 loc_set_none();
4166 replay_configure(icount_opts);
4168 machine_class = select_machine();
4170 set_memory_options(&ram_slots, &maxram_size, machine_class);
4172 os_daemonize();
4174 if (pid_file && qemu_create_pidfile(pid_file) != 0) {
4175 error_report("could not acquire pid file: %s", strerror(errno));
4176 exit(1);
4179 if (qemu_init_main_loop(&main_loop_err)) {
4180 error_report_err(main_loop_err);
4181 exit(1);
4184 if (qemu_opts_foreach(qemu_find_opts("sandbox"),
4185 parse_sandbox, NULL, NULL)) {
4186 exit(1);
4189 if (qemu_opts_foreach(qemu_find_opts("name"),
4190 parse_name, NULL, NULL)) {
4191 exit(1);
4194 #ifndef _WIN32
4195 if (qemu_opts_foreach(qemu_find_opts("add-fd"),
4196 parse_add_fd, NULL, NULL)) {
4197 exit(1);
4200 if (qemu_opts_foreach(qemu_find_opts("add-fd"),
4201 cleanup_add_fd, NULL, NULL)) {
4202 exit(1);
4204 #endif
4206 current_machine = MACHINE(object_new(object_class_get_name(
4207 OBJECT_CLASS(machine_class))));
4208 if (machine_help_func(qemu_get_machine_opts(), current_machine)) {
4209 exit(0);
4211 object_property_add_child(object_get_root(), "machine",
4212 OBJECT(current_machine), &error_abort);
4214 if (machine_class->minimum_page_bits) {
4215 if (!set_preferred_target_page_bits(machine_class->minimum_page_bits)) {
4216 /* This would be a board error: specifying a minimum smaller than
4217 * a target's compile-time fixed setting.
4219 g_assert_not_reached();
4223 cpu_exec_init_all();
4225 if (machine_class->hw_version) {
4226 qemu_set_hw_version(machine_class->hw_version);
4229 if (cpu_model && is_help_option(cpu_model)) {
4230 list_cpus(stdout, &fprintf, cpu_model);
4231 exit(0);
4234 if (!trace_init_backends()) {
4235 exit(1);
4237 trace_init_file(trace_file);
4239 /* Open the logfile at this point and set the log mask if necessary.
4241 if (log_file) {
4242 qemu_set_log_filename(log_file, &error_fatal);
4245 if (log_mask) {
4246 int mask;
4247 mask = qemu_str_to_log_mask(log_mask);
4248 if (!mask) {
4249 qemu_print_log_usage(stdout);
4250 exit(1);
4252 qemu_set_log(mask);
4253 } else {
4254 qemu_set_log(0);
4257 /* If no data_dir is specified then try to find it relative to the
4258 executable path. */
4259 if (data_dir_idx < ARRAY_SIZE(data_dir)) {
4260 data_dir[data_dir_idx] = os_find_datadir();
4261 if (data_dir[data_dir_idx] != NULL) {
4262 data_dir_idx++;
4265 /* If all else fails use the install path specified when building. */
4266 if (data_dir_idx < ARRAY_SIZE(data_dir)) {
4267 data_dir[data_dir_idx++] = CONFIG_QEMU_DATADIR;
4270 /* -L help lists the data directories and exits. */
4271 if (list_data_dirs) {
4272 for (i = 0; i < data_dir_idx; i++) {
4273 printf("%s\n", data_dir[i]);
4275 exit(0);
4278 smp_parse(qemu_opts_find(qemu_find_opts("smp-opts"), NULL));
4280 machine_class->max_cpus = machine_class->max_cpus ?: 1; /* Default to UP */
4281 if (max_cpus > machine_class->max_cpus) {
4282 error_report("Number of SMP CPUs requested (%d) exceeds max CPUs "
4283 "supported by machine '%s' (%d)", max_cpus,
4284 machine_class->name, machine_class->max_cpus);
4285 exit(1);
4289 * Get the default machine options from the machine if it is not already
4290 * specified either by the configuration file or by the command line.
4292 if (machine_class->default_machine_opts) {
4293 qemu_opts_set_defaults(qemu_find_opts("machine"),
4294 machine_class->default_machine_opts, 0);
4297 qemu_opts_foreach(qemu_find_opts("device"),
4298 default_driver_check, NULL, NULL);
4299 qemu_opts_foreach(qemu_find_opts("global"),
4300 default_driver_check, NULL, NULL);
4302 if (!vga_model && !default_vga) {
4303 vga_interface_type = VGA_DEVICE;
4305 if (!has_defaults || machine_class->no_serial) {
4306 default_serial = 0;
4308 if (!has_defaults || machine_class->no_parallel) {
4309 default_parallel = 0;
4311 if (!has_defaults || !machine_class->use_virtcon) {
4312 default_virtcon = 0;
4314 if (!has_defaults || !machine_class->use_sclp) {
4315 default_sclp = 0;
4317 if (!has_defaults || machine_class->no_floppy) {
4318 default_floppy = 0;
4320 if (!has_defaults || machine_class->no_cdrom) {
4321 default_cdrom = 0;
4323 if (!has_defaults || machine_class->no_sdcard) {
4324 default_sdcard = 0;
4326 if (!has_defaults) {
4327 default_monitor = 0;
4328 default_net = 0;
4329 default_vga = 0;
4332 if (is_daemonized()) {
4333 /* According to documentation and historically, -nographic redirects
4334 * serial port, parallel port and monitor to stdio, which does not work
4335 * with -daemonize. We can redirect these to null instead, but since
4336 * -nographic is legacy, let's just error out.
4337 * We disallow -nographic only if all other ports are not redirected
4338 * explicitly, to not break existing legacy setups which uses
4339 * -nographic _and_ redirects all ports explicitly - this is valid
4340 * usage, -nographic is just a no-op in this case.
4342 if (nographic
4343 && (default_parallel || default_serial
4344 || default_monitor || default_virtcon)) {
4345 error_report("-nographic cannot be used with -daemonize");
4346 exit(1);
4348 #ifdef CONFIG_CURSES
4349 if (display_type == DT_CURSES) {
4350 error_report("curses display cannot be used with -daemonize");
4351 exit(1);
4353 #endif
4356 if (nographic) {
4357 if (default_parallel)
4358 add_device_config(DEV_PARALLEL, "null");
4359 if (default_serial && default_monitor) {
4360 add_device_config(DEV_SERIAL, "mon:stdio");
4361 } else if (default_virtcon && default_monitor) {
4362 add_device_config(DEV_VIRTCON, "mon:stdio");
4363 } else if (default_sclp && default_monitor) {
4364 add_device_config(DEV_SCLP, "mon:stdio");
4365 } else {
4366 if (default_serial)
4367 add_device_config(DEV_SERIAL, "stdio");
4368 if (default_virtcon)
4369 add_device_config(DEV_VIRTCON, "stdio");
4370 if (default_sclp) {
4371 add_device_config(DEV_SCLP, "stdio");
4373 if (default_monitor)
4374 monitor_parse("stdio", "readline", false);
4376 } else {
4377 if (default_serial)
4378 add_device_config(DEV_SERIAL, "vc:80Cx24C");
4379 if (default_parallel)
4380 add_device_config(DEV_PARALLEL, "vc:80Cx24C");
4381 if (default_monitor)
4382 monitor_parse("vc:80Cx24C", "readline", false);
4383 if (default_virtcon)
4384 add_device_config(DEV_VIRTCON, "vc:80Cx24C");
4385 if (default_sclp) {
4386 add_device_config(DEV_SCLP, "vc:80Cx24C");
4390 #if defined(CONFIG_VNC)
4391 if (!QTAILQ_EMPTY(&(qemu_find_opts("vnc")->head))) {
4392 display_remote++;
4394 #endif
4395 if (display_type == DT_DEFAULT && !display_remote) {
4396 #if defined(CONFIG_GTK)
4397 display_type = DT_GTK;
4398 #elif defined(CONFIG_SDL)
4399 display_type = DT_SDL;
4400 #elif defined(CONFIG_COCOA)
4401 display_type = DT_COCOA;
4402 #elif defined(CONFIG_VNC)
4403 vnc_parse("localhost:0,to=99,id=default", &error_abort);
4404 #else
4405 display_type = DT_NONE;
4406 #endif
4409 if ((no_frame || alt_grab || ctrl_grab) && display_type != DT_SDL) {
4410 error_report("-no-frame, -alt-grab and -ctrl-grab are only valid "
4411 "for SDL, ignoring option");
4413 if (no_quit && (display_type != DT_GTK && display_type != DT_SDL)) {
4414 error_report("-no-quit is only valid for GTK and SDL, "
4415 "ignoring option");
4418 if (display_type == DT_GTK) {
4419 early_gtk_display_init(request_opengl);
4422 if (display_type == DT_SDL) {
4423 sdl_display_early_init(request_opengl);
4426 qemu_console_early_init();
4428 if (request_opengl == 1 && display_opengl == 0) {
4429 #if defined(CONFIG_OPENGL)
4430 error_report("OpenGL is not supported by the display");
4431 #else
4432 error_report("OpenGL support is disabled");
4433 #endif
4434 exit(1);
4437 page_size_init();
4438 socket_init();
4440 if (qemu_opts_foreach(qemu_find_opts("object"),
4441 user_creatable_add_opts_foreach,
4442 object_create_initial, NULL)) {
4443 exit(1);
4446 if (qemu_opts_foreach(qemu_find_opts("chardev"),
4447 chardev_init_func, NULL, NULL)) {
4448 exit(1);
4451 #ifdef CONFIG_VIRTFS
4452 if (qemu_opts_foreach(qemu_find_opts("fsdev"),
4453 fsdev_init_func, NULL, NULL)) {
4454 exit(1);
4456 #endif
4458 if (qemu_opts_foreach(qemu_find_opts("device"),
4459 device_help_func, NULL, NULL)) {
4460 exit(0);
4463 machine_opts = qemu_get_machine_opts();
4464 if (qemu_opt_foreach(machine_opts, machine_set_property, current_machine,
4465 NULL)) {
4466 object_unref(OBJECT(current_machine));
4467 exit(1);
4470 configure_accelerator(current_machine);
4473 * Register all the global properties, including accel properties,
4474 * machine properties, and user-specified ones.
4476 register_global_properties(current_machine);
4479 * Migration object can only be created after global properties
4480 * are applied correctly.
4482 migration_object_init();
4484 if (qtest_chrdev) {
4485 qtest_init(qtest_chrdev, qtest_log, &error_fatal);
4488 machine_opts = qemu_get_machine_opts();
4489 kernel_filename = qemu_opt_get(machine_opts, "kernel");
4490 initrd_filename = qemu_opt_get(machine_opts, "initrd");
4491 kernel_cmdline = qemu_opt_get(machine_opts, "append");
4492 bios_name = qemu_opt_get(machine_opts, "firmware");
4494 opts = qemu_opts_find(qemu_find_opts("boot-opts"), NULL);
4495 if (opts) {
4496 boot_order = qemu_opt_get(opts, "order");
4497 if (boot_order) {
4498 validate_bootdevices(boot_order, &error_fatal);
4501 boot_once = qemu_opt_get(opts, "once");
4502 if (boot_once) {
4503 validate_bootdevices(boot_once, &error_fatal);
4506 boot_menu = qemu_opt_get_bool(opts, "menu", boot_menu);
4507 boot_strict = qemu_opt_get_bool(opts, "strict", false);
4510 if (!boot_order) {
4511 boot_order = machine_class->default_boot_order;
4514 if (!kernel_cmdline) {
4515 kernel_cmdline = "";
4516 current_machine->kernel_cmdline = (char *)kernel_cmdline;
4519 linux_boot = (kernel_filename != NULL);
4521 if (!linux_boot && *kernel_cmdline != '\0') {
4522 error_report("-append only allowed with -kernel option");
4523 exit(1);
4526 if (!linux_boot && initrd_filename != NULL) {
4527 error_report("-initrd only allowed with -kernel option");
4528 exit(1);
4531 if (semihosting_enabled() && !semihosting_get_argc() && kernel_filename) {
4532 /* fall back to the -kernel/-append */
4533 semihosting_arg_fallback(kernel_filename, kernel_cmdline);
4536 os_set_line_buffering();
4538 /* spice needs the timers to be initialized by this point */
4539 qemu_spice_init();
4541 cpu_ticks_init();
4542 if (icount_opts) {
4543 if (!tcg_enabled()) {
4544 error_report("-icount is not allowed with hardware virtualization");
4545 exit(1);
4547 configure_icount(icount_opts, &error_abort);
4548 qemu_opts_del(icount_opts);
4551 if (tcg_enabled()) {
4552 qemu_tcg_configure(accel_opts, &error_fatal);
4555 if (default_net) {
4556 QemuOptsList *net = qemu_find_opts("net");
4557 qemu_opts_set(net, NULL, "type", "nic", &error_abort);
4558 #ifdef CONFIG_SLIRP
4559 qemu_opts_set(net, NULL, "type", "user", &error_abort);
4560 #endif
4563 colo_info_init();
4565 if (net_init_clients() < 0) {
4566 exit(1);
4569 if (qemu_opts_foreach(qemu_find_opts("object"),
4570 user_creatable_add_opts_foreach,
4571 object_create_delayed, NULL)) {
4572 exit(1);
4575 #ifdef CONFIG_TPM
4576 if (tpm_init() < 0) {
4577 exit(1);
4579 #endif
4581 /* init the bluetooth world */
4582 if (foreach_device_config(DEV_BT, bt_parse))
4583 exit(1);
4585 if (!xen_enabled()) {
4586 /* On 32-bit hosts, QEMU is limited by virtual address space */
4587 if (ram_size > (2047 << 20) && HOST_LONG_BITS == 32) {
4588 error_report("at most 2047 MB RAM can be simulated");
4589 exit(1);
4593 blk_mig_init();
4594 ram_mig_init();
4596 /* If the currently selected machine wishes to override the units-per-bus
4597 * property of its default HBA interface type, do so now. */
4598 if (machine_class->units_per_default_bus) {
4599 override_max_devs(machine_class->block_default_type,
4600 machine_class->units_per_default_bus);
4603 /* open the virtual block devices */
4604 while (!QSIMPLEQ_EMPTY(&bdo_queue)) {
4605 BlockdevOptions_queue *bdo = QSIMPLEQ_FIRST(&bdo_queue);
4607 QSIMPLEQ_REMOVE_HEAD(&bdo_queue, entry);
4608 loc_push_restore(&bdo->loc);
4609 qmp_blockdev_add(bdo->bdo, &error_fatal);
4610 loc_pop(&bdo->loc);
4611 qapi_free_BlockdevOptions(bdo->bdo);
4612 g_free(bdo);
4614 if (snapshot || replay_mode != REPLAY_MODE_NONE) {
4615 qemu_opts_foreach(qemu_find_opts("drive"), drive_enable_snapshot,
4616 NULL, NULL);
4618 if (qemu_opts_foreach(qemu_find_opts("drive"), drive_init_func,
4619 &machine_class->block_default_type, NULL)) {
4620 exit(1);
4623 default_drive(default_cdrom, snapshot, machine_class->block_default_type, 2,
4624 CDROM_OPTS);
4625 default_drive(default_floppy, snapshot, IF_FLOPPY, 0, FD_OPTS);
4626 default_drive(default_sdcard, snapshot, IF_SD, 0, SD_OPTS);
4628 parse_numa_opts(current_machine);
4630 if (qemu_opts_foreach(qemu_find_opts("mon"),
4631 mon_init_func, NULL, NULL)) {
4632 exit(1);
4635 if (foreach_device_config(DEV_SERIAL, serial_parse) < 0)
4636 exit(1);
4637 if (foreach_device_config(DEV_PARALLEL, parallel_parse) < 0)
4638 exit(1);
4639 if (foreach_device_config(DEV_VIRTCON, virtcon_parse) < 0)
4640 exit(1);
4641 if (foreach_device_config(DEV_SCLP, sclp_parse) < 0) {
4642 exit(1);
4644 if (foreach_device_config(DEV_DEBUGCON, debugcon_parse) < 0)
4645 exit(1);
4647 /* If no default VGA is requested, the default is "none". */
4648 if (default_vga) {
4649 if (machine_class->default_display) {
4650 vga_model = machine_class->default_display;
4651 } else if (vga_interface_available(VGA_CIRRUS)) {
4652 vga_model = "cirrus";
4653 } else if (vga_interface_available(VGA_STD)) {
4654 vga_model = "std";
4657 if (vga_model) {
4658 select_vgahw(vga_model);
4661 if (watchdog) {
4662 i = select_watchdog(watchdog);
4663 if (i > 0)
4664 exit (i == 1 ? 1 : 0);
4667 /* This checkpoint is required by replay to separate prior clock
4668 reading from the other reads, because timer polling functions query
4669 clock values from the log. */
4670 replay_checkpoint(CHECKPOINT_INIT);
4671 qdev_machine_init();
4673 current_machine->ram_size = ram_size;
4674 current_machine->maxram_size = maxram_size;
4675 current_machine->ram_slots = ram_slots;
4676 current_machine->boot_order = boot_order;
4677 current_machine->cpu_model = cpu_model;
4679 machine_run_board_init(current_machine);
4681 realtime_init();
4683 soundhw_init();
4685 if (hax_enabled()) {
4686 hax_sync_vcpus();
4689 if (qemu_opts_foreach(qemu_find_opts("fw_cfg"),
4690 parse_fw_cfg, fw_cfg_find(), NULL) != 0) {
4691 exit(1);
4694 /* init USB devices */
4695 if (machine_usb(current_machine)) {
4696 if (foreach_device_config(DEV_USB, usb_parse) < 0)
4697 exit(1);
4700 /* Check if IGD GFX passthrough. */
4701 igd_gfx_passthru();
4703 /* init generic devices */
4704 rom_set_order_override(FW_CFG_ORDER_OVERRIDE_DEVICE);
4705 if (qemu_opts_foreach(qemu_find_opts("device"),
4706 device_init_func, NULL, NULL)) {
4707 exit(1);
4710 cpu_synchronize_all_post_init();
4712 rom_reset_order_override();
4715 * Create frontends for -drive if=scsi leftovers.
4716 * Normally, frontends for -drive get created by machine
4717 * initialization for onboard SCSI HBAs. However, we create a few
4718 * more ever since SCSI qdevification, but this is pretty much an
4719 * implementation accident, and deprecated.
4721 scsi_legacy_handle_cmdline();
4723 /* Did we create any drives that we failed to create a device for? */
4724 drive_check_orphaned();
4726 /* Don't warn about the default network setup that you get if
4727 * no command line -net or -netdev options are specified. There
4728 * are two cases that we would otherwise complain about:
4729 * (1) board doesn't support a NIC but the implicit "-net nic"
4730 * requested one
4731 * (2) CONFIG_SLIRP not set, in which case the implicit "-net nic"
4732 * sets up a nic that isn't connected to anything.
4734 if (!default_net) {
4735 net_check_clients();
4739 if (boot_once) {
4740 qemu_boot_set(boot_once, &error_fatal);
4741 qemu_register_reset(restore_boot_order, g_strdup(boot_order));
4744 ds = init_displaystate();
4746 /* init local displays */
4747 switch (display_type) {
4748 case DT_CURSES:
4749 curses_display_init(ds, full_screen);
4750 break;
4751 case DT_SDL:
4752 sdl_display_init(ds, full_screen, no_frame);
4753 break;
4754 case DT_COCOA:
4755 cocoa_display_init(ds, full_screen);
4756 break;
4757 case DT_GTK:
4758 gtk_display_init(ds, full_screen, grab_on_hover);
4759 break;
4760 default:
4761 break;
4764 /* must be after terminal init, SDL library changes signal handlers */
4765 os_setup_signal_handling();
4767 /* init remote displays */
4768 #ifdef CONFIG_VNC
4769 qemu_opts_foreach(qemu_find_opts("vnc"),
4770 vnc_init_func, NULL, NULL);
4771 #endif
4773 if (using_spice) {
4774 qemu_spice_display_init();
4777 #ifdef CONFIG_OPENGL_DMABUF
4778 if (display_type == DT_EGL) {
4779 egl_headless_init();
4781 #endif
4783 if (foreach_device_config(DEV_GDB, gdbserver_start) < 0) {
4784 exit(1);
4787 qdev_machine_creation_done();
4789 /* TODO: once all bus devices are qdevified, this should be done
4790 * when bus is created by qdev.c */
4791 qemu_register_reset(qbus_reset_all_fn, sysbus_get_default());
4792 qemu_run_machine_init_done_notifiers();
4794 if (rom_check_and_register_reset() != 0) {
4795 error_report("rom check and register reset failed");
4796 exit(1);
4799 replay_start();
4801 /* This checkpoint is required by replay to separate prior clock
4802 reading from the other reads, because timer polling functions query
4803 clock values from the log. */
4804 replay_checkpoint(CHECKPOINT_RESET);
4805 qemu_system_reset(SHUTDOWN_CAUSE_NONE);
4806 register_global_state();
4807 if (replay_mode != REPLAY_MODE_NONE) {
4808 replay_vmstate_init();
4809 } else if (loadvm) {
4810 Error *local_err = NULL;
4811 if (load_snapshot(loadvm, &local_err) < 0) {
4812 error_report_err(local_err);
4813 autostart = 0;
4817 qdev_prop_check_globals();
4818 if (vmstate_dump_file) {
4819 /* dump and exit */
4820 dump_vmstate_json_to_file(vmstate_dump_file);
4821 return 0;
4824 if (incoming) {
4825 Error *local_err = NULL;
4826 qemu_start_incoming_migration(incoming, &local_err);
4827 if (local_err) {
4828 error_reportf_err(local_err, "-incoming %s: ", incoming);
4829 exit(1);
4831 } else if (autostart) {
4832 vm_start();
4835 os_setup_post();
4837 main_loop();
4838 replay_disable_events();
4839 iothread_stop_all();
4841 pause_all_vcpus();
4842 bdrv_close_all();
4843 res_free();
4845 /* vhost-user must be cleaned up before chardevs. */
4846 net_cleanup();
4847 audio_cleanup();
4848 monitor_cleanup();
4849 qemu_chr_cleanup();
4850 /* TODO: unref root container, check all devices are ok */
4852 return 0;