hw/m68k: Remove dummy machine
[qemu.git] / vl.c
blob27d982981ab5e667d96c69b2f2e4b06a19c7daaf
1 /*
2 * QEMU System Emulator
4 * Copyright (c) 2003-2008 Fabrice Bellard
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 * THE SOFTWARE.
24 #include "qemu/osdep.h"
25 #include "qemu-version.h"
26 #include "qemu/cutils.h"
27 #include "qemu/help_option.h"
28 #include "qemu/uuid.h"
30 #ifdef CONFIG_SECCOMP
31 #include "sysemu/seccomp.h"
32 #endif
34 #if defined(CONFIG_VDE)
35 #include <libvdeplug.h>
36 #endif
38 #ifdef CONFIG_SDL
39 #if defined(__APPLE__) || defined(main)
40 #include <SDL.h>
41 int qemu_main(int argc, char **argv, char **envp);
42 int main(int argc, char **argv)
44 return qemu_main(argc, argv, NULL);
46 #undef main
47 #define main qemu_main
48 #endif
49 #endif /* CONFIG_SDL */
51 #ifdef CONFIG_COCOA
52 #undef main
53 #define main qemu_main
54 #endif /* CONFIG_COCOA */
57 #include "qemu/error-report.h"
58 #include "qemu/sockets.h"
59 #include "hw/hw.h"
60 #include "hw/boards.h"
61 #include "sysemu/accel.h"
62 #include "hw/usb.h"
63 #include "hw/i386/pc.h"
64 #include "hw/isa/isa.h"
65 #include "hw/bt.h"
66 #include "sysemu/watchdog.h"
67 #include "hw/smbios/smbios.h"
68 #include "hw/acpi/acpi.h"
69 #include "hw/xen/xen.h"
70 #include "hw/qdev.h"
71 #include "hw/loader.h"
72 #include "monitor/qdev.h"
73 #include "sysemu/bt.h"
74 #include "net/net.h"
75 #include "net/slirp.h"
76 #include "monitor/monitor.h"
77 #include "ui/console.h"
78 #include "ui/input.h"
79 #include "sysemu/sysemu.h"
80 #include "sysemu/numa.h"
81 #include "exec/gdbstub.h"
82 #include "qemu/timer.h"
83 #include "sysemu/char.h"
84 #include "qemu/bitmap.h"
85 #include "qemu/log.h"
86 #include "sysemu/blockdev.h"
87 #include "hw/block/block.h"
88 #include "migration/block.h"
89 #include "sysemu/tpm.h"
90 #include "sysemu/dma.h"
91 #include "audio/audio.h"
92 #include "migration/migration.h"
93 #include "sysemu/cpus.h"
94 #include "migration/colo.h"
95 #include "sysemu/kvm.h"
96 #include "sysemu/hax.h"
97 #include "qapi/qmp/qjson.h"
98 #include "qemu/option.h"
99 #include "qemu/config-file.h"
100 #include "qemu-options.h"
101 #include "qmp-commands.h"
102 #include "qemu/main-loop.h"
103 #ifdef CONFIG_VIRTFS
104 #include "fsdev/qemu-fsdev.h"
105 #endif
106 #include "sysemu/qtest.h"
108 #include "disas/disas.h"
111 #include "slirp/libslirp.h"
113 #include "trace-root.h"
114 #include "trace/control.h"
115 #include "qemu/queue.h"
116 #include "sysemu/arch_init.h"
118 #include "ui/qemu-spice.h"
119 #include "qapi/string-input-visitor.h"
120 #include "qapi/opts-visitor.h"
121 #include "qom/object_interfaces.h"
122 #include "qapi-event.h"
123 #include "exec/semihost.h"
124 #include "crypto/init.h"
125 #include "sysemu/replay.h"
126 #include "qapi/qmp/qerror.h"
127 #include "sysemu/iothread.h"
129 #define MAX_VIRTIO_CONSOLES 1
130 #define MAX_SCLP_CONSOLES 1
132 static const char *data_dir[16];
133 static int data_dir_idx;
134 const char *bios_name = NULL;
135 enum vga_retrace_method vga_retrace_method = VGA_RETRACE_DUMB;
136 int request_opengl = -1;
137 int display_opengl;
138 const char* keyboard_layout = NULL;
139 ram_addr_t ram_size;
140 const char *mem_path = NULL;
141 int mem_prealloc = 0; /* force preallocation of physical target memory */
142 bool enable_mlock = false;
143 int nb_nics;
144 NICInfo nd_table[MAX_NICS];
145 int autostart;
146 static int rtc_utc = 1;
147 static int rtc_date_offset = -1; /* -1 means no change */
148 QEMUClockType rtc_clock;
149 int vga_interface_type = VGA_NONE;
150 static int full_screen = 0;
151 static int no_frame = 0;
152 int no_quit = 0;
153 static bool grab_on_hover;
154 Chardev *serial_hds[MAX_SERIAL_PORTS];
155 Chardev *parallel_hds[MAX_PARALLEL_PORTS];
156 Chardev *virtcon_hds[MAX_VIRTIO_CONSOLES];
157 Chardev *sclp_hds[MAX_SCLP_CONSOLES];
158 int win2k_install_hack = 0;
159 int singlestep = 0;
160 int smp_cpus = 1;
161 int max_cpus = 1;
162 int smp_cores = 1;
163 int smp_threads = 1;
164 int acpi_enabled = 1;
165 int no_hpet = 0;
166 int fd_bootchk = 1;
167 static int no_reboot;
168 int no_shutdown = 0;
169 int cursor_hide = 1;
170 int graphic_rotate = 0;
171 const char *watchdog;
172 QEMUOptionRom option_rom[MAX_OPTION_ROMS];
173 int nb_option_roms;
174 int old_param = 0;
175 const char *qemu_name;
176 int alt_grab = 0;
177 int ctrl_grab = 0;
178 unsigned int nb_prom_envs = 0;
179 const char *prom_envs[MAX_PROM_ENVS];
180 int boot_menu;
181 bool boot_strict;
182 uint8_t *boot_splash_filedata;
183 size_t boot_splash_filedata_size;
184 uint8_t qemu_extra_params_fw[2];
185 int only_migratable; /* turn it off unless user states otherwise */
187 int icount_align_option;
189 /* The bytes in qemu_uuid are in the order specified by RFC4122, _not_ in the
190 * little-endian "wire format" described in the SMBIOS 2.6 specification.
192 QemuUUID qemu_uuid;
193 bool qemu_uuid_set;
195 static NotifierList exit_notifiers =
196 NOTIFIER_LIST_INITIALIZER(exit_notifiers);
198 static NotifierList machine_init_done_notifiers =
199 NOTIFIER_LIST_INITIALIZER(machine_init_done_notifiers);
201 bool xen_allowed;
202 uint32_t xen_domid;
203 enum xen_mode xen_mode = XEN_EMULATE;
205 static int has_defaults = 1;
206 static int default_serial = 1;
207 static int default_parallel = 1;
208 static int default_virtcon = 1;
209 static int default_sclp = 1;
210 static int default_monitor = 1;
211 static int default_floppy = 1;
212 static int default_cdrom = 1;
213 static int default_sdcard = 1;
214 static int default_vga = 1;
215 static int default_net = 1;
217 static struct {
218 const char *driver;
219 int *flag;
220 } default_list[] = {
221 { .driver = "isa-serial", .flag = &default_serial },
222 { .driver = "isa-parallel", .flag = &default_parallel },
223 { .driver = "isa-fdc", .flag = &default_floppy },
224 { .driver = "floppy", .flag = &default_floppy },
225 { .driver = "ide-cd", .flag = &default_cdrom },
226 { .driver = "ide-hd", .flag = &default_cdrom },
227 { .driver = "ide-drive", .flag = &default_cdrom },
228 { .driver = "scsi-cd", .flag = &default_cdrom },
229 { .driver = "virtio-serial-pci", .flag = &default_virtcon },
230 { .driver = "virtio-serial", .flag = &default_virtcon },
231 { .driver = "VGA", .flag = &default_vga },
232 { .driver = "isa-vga", .flag = &default_vga },
233 { .driver = "cirrus-vga", .flag = &default_vga },
234 { .driver = "isa-cirrus-vga", .flag = &default_vga },
235 { .driver = "vmware-svga", .flag = &default_vga },
236 { .driver = "qxl-vga", .flag = &default_vga },
237 { .driver = "virtio-vga", .flag = &default_vga },
240 static QemuOptsList qemu_rtc_opts = {
241 .name = "rtc",
242 .head = QTAILQ_HEAD_INITIALIZER(qemu_rtc_opts.head),
243 .desc = {
245 .name = "base",
246 .type = QEMU_OPT_STRING,
248 .name = "clock",
249 .type = QEMU_OPT_STRING,
251 .name = "driftfix",
252 .type = QEMU_OPT_STRING,
254 { /* end of list */ }
258 static QemuOptsList qemu_sandbox_opts = {
259 .name = "sandbox",
260 .implied_opt_name = "enable",
261 .head = QTAILQ_HEAD_INITIALIZER(qemu_sandbox_opts.head),
262 .desc = {
264 .name = "enable",
265 .type = QEMU_OPT_BOOL,
267 { /* end of list */ }
271 static QemuOptsList qemu_option_rom_opts = {
272 .name = "option-rom",
273 .implied_opt_name = "romfile",
274 .head = QTAILQ_HEAD_INITIALIZER(qemu_option_rom_opts.head),
275 .desc = {
277 .name = "bootindex",
278 .type = QEMU_OPT_NUMBER,
279 }, {
280 .name = "romfile",
281 .type = QEMU_OPT_STRING,
283 { /* end of list */ }
287 static QemuOptsList qemu_machine_opts = {
288 .name = "machine",
289 .implied_opt_name = "type",
290 .merge_lists = true,
291 .head = QTAILQ_HEAD_INITIALIZER(qemu_machine_opts.head),
292 .desc = {
294 * no elements => accept any
295 * sanity checking will happen later
296 * when setting machine properties
302 static QemuOptsList qemu_boot_opts = {
303 .name = "boot-opts",
304 .implied_opt_name = "order",
305 .merge_lists = true,
306 .head = QTAILQ_HEAD_INITIALIZER(qemu_boot_opts.head),
307 .desc = {
309 .name = "order",
310 .type = QEMU_OPT_STRING,
311 }, {
312 .name = "once",
313 .type = QEMU_OPT_STRING,
314 }, {
315 .name = "menu",
316 .type = QEMU_OPT_BOOL,
317 }, {
318 .name = "splash",
319 .type = QEMU_OPT_STRING,
320 }, {
321 .name = "splash-time",
322 .type = QEMU_OPT_STRING,
323 }, {
324 .name = "reboot-timeout",
325 .type = QEMU_OPT_STRING,
326 }, {
327 .name = "strict",
328 .type = QEMU_OPT_BOOL,
330 { /*End of list */ }
334 static QemuOptsList qemu_add_fd_opts = {
335 .name = "add-fd",
336 .head = QTAILQ_HEAD_INITIALIZER(qemu_add_fd_opts.head),
337 .desc = {
339 .name = "fd",
340 .type = QEMU_OPT_NUMBER,
341 .help = "file descriptor of which a duplicate is added to fd set",
343 .name = "set",
344 .type = QEMU_OPT_NUMBER,
345 .help = "ID of the fd set to add fd to",
347 .name = "opaque",
348 .type = QEMU_OPT_STRING,
349 .help = "free-form string used to describe fd",
351 { /* end of list */ }
355 static QemuOptsList qemu_object_opts = {
356 .name = "object",
357 .implied_opt_name = "qom-type",
358 .head = QTAILQ_HEAD_INITIALIZER(qemu_object_opts.head),
359 .desc = {
364 static QemuOptsList qemu_tpmdev_opts = {
365 .name = "tpmdev",
366 .implied_opt_name = "type",
367 .head = QTAILQ_HEAD_INITIALIZER(qemu_tpmdev_opts.head),
368 .desc = {
369 /* options are defined in the TPM backends */
370 { /* end of list */ }
374 static QemuOptsList qemu_realtime_opts = {
375 .name = "realtime",
376 .head = QTAILQ_HEAD_INITIALIZER(qemu_realtime_opts.head),
377 .desc = {
379 .name = "mlock",
380 .type = QEMU_OPT_BOOL,
382 { /* end of list */ }
386 static QemuOptsList qemu_msg_opts = {
387 .name = "msg",
388 .head = QTAILQ_HEAD_INITIALIZER(qemu_msg_opts.head),
389 .desc = {
391 .name = "timestamp",
392 .type = QEMU_OPT_BOOL,
394 { /* end of list */ }
398 static QemuOptsList qemu_name_opts = {
399 .name = "name",
400 .implied_opt_name = "guest",
401 .merge_lists = true,
402 .head = QTAILQ_HEAD_INITIALIZER(qemu_name_opts.head),
403 .desc = {
405 .name = "guest",
406 .type = QEMU_OPT_STRING,
407 .help = "Sets the name of the guest.\n"
408 "This name will be displayed in the SDL window caption.\n"
409 "The name will also be used for the VNC server",
410 }, {
411 .name = "process",
412 .type = QEMU_OPT_STRING,
413 .help = "Sets the name of the QEMU process, as shown in top etc",
414 }, {
415 .name = "debug-threads",
416 .type = QEMU_OPT_BOOL,
417 .help = "When enabled, name the individual threads; defaults off.\n"
418 "NOTE: The thread names are for debugging and not a\n"
419 "stable API.",
421 { /* End of list */ }
425 static QemuOptsList qemu_mem_opts = {
426 .name = "memory",
427 .implied_opt_name = "size",
428 .head = QTAILQ_HEAD_INITIALIZER(qemu_mem_opts.head),
429 .merge_lists = true,
430 .desc = {
432 .name = "size",
433 .type = QEMU_OPT_SIZE,
436 .name = "slots",
437 .type = QEMU_OPT_NUMBER,
440 .name = "maxmem",
441 .type = QEMU_OPT_SIZE,
443 { /* end of list */ }
447 static QemuOptsList qemu_icount_opts = {
448 .name = "icount",
449 .implied_opt_name = "shift",
450 .merge_lists = true,
451 .head = QTAILQ_HEAD_INITIALIZER(qemu_icount_opts.head),
452 .desc = {
454 .name = "shift",
455 .type = QEMU_OPT_STRING,
456 }, {
457 .name = "align",
458 .type = QEMU_OPT_BOOL,
459 }, {
460 .name = "sleep",
461 .type = QEMU_OPT_BOOL,
462 }, {
463 .name = "rr",
464 .type = QEMU_OPT_STRING,
465 }, {
466 .name = "rrfile",
467 .type = QEMU_OPT_STRING,
468 }, {
469 .name = "rrsnapshot",
470 .type = QEMU_OPT_STRING,
472 { /* end of list */ }
476 static QemuOptsList qemu_semihosting_config_opts = {
477 .name = "semihosting-config",
478 .implied_opt_name = "enable",
479 .head = QTAILQ_HEAD_INITIALIZER(qemu_semihosting_config_opts.head),
480 .desc = {
482 .name = "enable",
483 .type = QEMU_OPT_BOOL,
484 }, {
485 .name = "target",
486 .type = QEMU_OPT_STRING,
487 }, {
488 .name = "arg",
489 .type = QEMU_OPT_STRING,
491 { /* end of list */ }
495 static QemuOptsList qemu_fw_cfg_opts = {
496 .name = "fw_cfg",
497 .implied_opt_name = "name",
498 .head = QTAILQ_HEAD_INITIALIZER(qemu_fw_cfg_opts.head),
499 .desc = {
501 .name = "name",
502 .type = QEMU_OPT_STRING,
503 .help = "Sets the fw_cfg name of the blob to be inserted",
504 }, {
505 .name = "file",
506 .type = QEMU_OPT_STRING,
507 .help = "Sets the name of the file from which\n"
508 "the fw_cfg blob will be loaded",
509 }, {
510 .name = "string",
511 .type = QEMU_OPT_STRING,
512 .help = "Sets content of the blob to be inserted from a string",
514 { /* end of list */ }
519 * Get machine options
521 * Returns: machine options (never null).
523 QemuOpts *qemu_get_machine_opts(void)
525 return qemu_find_opts_singleton("machine");
528 const char *qemu_get_vm_name(void)
530 return qemu_name;
533 static void res_free(void)
535 g_free(boot_splash_filedata);
536 boot_splash_filedata = NULL;
539 static int default_driver_check(void *opaque, QemuOpts *opts, Error **errp)
541 const char *driver = qemu_opt_get(opts, "driver");
542 int i;
544 if (!driver)
545 return 0;
546 for (i = 0; i < ARRAY_SIZE(default_list); i++) {
547 if (strcmp(default_list[i].driver, driver) != 0)
548 continue;
549 *(default_list[i].flag) = 0;
551 return 0;
554 /***********************************************************/
555 /* QEMU state */
557 static RunState current_run_state = RUN_STATE_PRELAUNCH;
559 /* We use RUN_STATE__MAX but any invalid value will do */
560 static RunState vmstop_requested = RUN_STATE__MAX;
561 static QemuMutex vmstop_lock;
563 typedef struct {
564 RunState from;
565 RunState to;
566 } RunStateTransition;
568 static const RunStateTransition runstate_transitions_def[] = {
569 /* from -> to */
570 { RUN_STATE_DEBUG, RUN_STATE_RUNNING },
571 { RUN_STATE_DEBUG, RUN_STATE_FINISH_MIGRATE },
572 { RUN_STATE_DEBUG, RUN_STATE_PRELAUNCH },
574 { RUN_STATE_INMIGRATE, RUN_STATE_INTERNAL_ERROR },
575 { RUN_STATE_INMIGRATE, RUN_STATE_IO_ERROR },
576 { RUN_STATE_INMIGRATE, RUN_STATE_PAUSED },
577 { RUN_STATE_INMIGRATE, RUN_STATE_RUNNING },
578 { RUN_STATE_INMIGRATE, RUN_STATE_SHUTDOWN },
579 { RUN_STATE_INMIGRATE, RUN_STATE_SUSPENDED },
580 { RUN_STATE_INMIGRATE, RUN_STATE_WATCHDOG },
581 { RUN_STATE_INMIGRATE, RUN_STATE_GUEST_PANICKED },
582 { RUN_STATE_INMIGRATE, RUN_STATE_FINISH_MIGRATE },
583 { RUN_STATE_INMIGRATE, RUN_STATE_PRELAUNCH },
584 { RUN_STATE_INMIGRATE, RUN_STATE_POSTMIGRATE },
585 { RUN_STATE_INMIGRATE, RUN_STATE_COLO },
587 { RUN_STATE_INTERNAL_ERROR, RUN_STATE_PAUSED },
588 { RUN_STATE_INTERNAL_ERROR, RUN_STATE_FINISH_MIGRATE },
589 { RUN_STATE_INTERNAL_ERROR, RUN_STATE_PRELAUNCH },
591 { RUN_STATE_IO_ERROR, RUN_STATE_RUNNING },
592 { RUN_STATE_IO_ERROR, RUN_STATE_FINISH_MIGRATE },
593 { RUN_STATE_IO_ERROR, RUN_STATE_PRELAUNCH },
595 { RUN_STATE_PAUSED, RUN_STATE_RUNNING },
596 { RUN_STATE_PAUSED, RUN_STATE_FINISH_MIGRATE },
597 { RUN_STATE_PAUSED, RUN_STATE_PRELAUNCH },
598 { RUN_STATE_PAUSED, RUN_STATE_COLO},
600 { RUN_STATE_POSTMIGRATE, RUN_STATE_RUNNING },
601 { RUN_STATE_POSTMIGRATE, RUN_STATE_FINISH_MIGRATE },
602 { RUN_STATE_POSTMIGRATE, RUN_STATE_PRELAUNCH },
604 { RUN_STATE_PRELAUNCH, RUN_STATE_RUNNING },
605 { RUN_STATE_PRELAUNCH, RUN_STATE_FINISH_MIGRATE },
606 { RUN_STATE_PRELAUNCH, RUN_STATE_INMIGRATE },
608 { RUN_STATE_FINISH_MIGRATE, RUN_STATE_RUNNING },
609 { RUN_STATE_FINISH_MIGRATE, RUN_STATE_POSTMIGRATE },
610 { RUN_STATE_FINISH_MIGRATE, RUN_STATE_PRELAUNCH },
611 { RUN_STATE_FINISH_MIGRATE, RUN_STATE_COLO},
613 { RUN_STATE_RESTORE_VM, RUN_STATE_RUNNING },
614 { RUN_STATE_RESTORE_VM, RUN_STATE_PRELAUNCH },
616 { RUN_STATE_COLO, RUN_STATE_RUNNING },
618 { RUN_STATE_RUNNING, RUN_STATE_DEBUG },
619 { RUN_STATE_RUNNING, RUN_STATE_INTERNAL_ERROR },
620 { RUN_STATE_RUNNING, RUN_STATE_IO_ERROR },
621 { RUN_STATE_RUNNING, RUN_STATE_PAUSED },
622 { RUN_STATE_RUNNING, RUN_STATE_FINISH_MIGRATE },
623 { RUN_STATE_RUNNING, RUN_STATE_RESTORE_VM },
624 { RUN_STATE_RUNNING, RUN_STATE_SAVE_VM },
625 { RUN_STATE_RUNNING, RUN_STATE_SHUTDOWN },
626 { RUN_STATE_RUNNING, RUN_STATE_WATCHDOG },
627 { RUN_STATE_RUNNING, RUN_STATE_GUEST_PANICKED },
628 { RUN_STATE_RUNNING, RUN_STATE_COLO},
630 { RUN_STATE_SAVE_VM, RUN_STATE_RUNNING },
632 { RUN_STATE_SHUTDOWN, RUN_STATE_PAUSED },
633 { RUN_STATE_SHUTDOWN, RUN_STATE_FINISH_MIGRATE },
634 { RUN_STATE_SHUTDOWN, RUN_STATE_PRELAUNCH },
636 { RUN_STATE_DEBUG, RUN_STATE_SUSPENDED },
637 { RUN_STATE_RUNNING, RUN_STATE_SUSPENDED },
638 { RUN_STATE_SUSPENDED, RUN_STATE_RUNNING },
639 { RUN_STATE_SUSPENDED, RUN_STATE_FINISH_MIGRATE },
640 { RUN_STATE_SUSPENDED, RUN_STATE_PRELAUNCH },
641 { RUN_STATE_SUSPENDED, RUN_STATE_COLO},
643 { RUN_STATE_WATCHDOG, RUN_STATE_RUNNING },
644 { RUN_STATE_WATCHDOG, RUN_STATE_FINISH_MIGRATE },
645 { RUN_STATE_WATCHDOG, RUN_STATE_PRELAUNCH },
646 { RUN_STATE_WATCHDOG, RUN_STATE_COLO},
648 { RUN_STATE_GUEST_PANICKED, RUN_STATE_RUNNING },
649 { RUN_STATE_GUEST_PANICKED, RUN_STATE_FINISH_MIGRATE },
650 { RUN_STATE_GUEST_PANICKED, RUN_STATE_PRELAUNCH },
652 { RUN_STATE__MAX, RUN_STATE__MAX },
655 static bool runstate_valid_transitions[RUN_STATE__MAX][RUN_STATE__MAX];
657 bool runstate_check(RunState state)
659 return current_run_state == state;
662 bool runstate_store(char *str, size_t size)
664 const char *state = RunState_lookup[current_run_state];
665 size_t len = strlen(state) + 1;
667 if (len > size) {
668 return false;
670 memcpy(str, state, len);
671 return true;
674 static void runstate_init(void)
676 const RunStateTransition *p;
678 memset(&runstate_valid_transitions, 0, sizeof(runstate_valid_transitions));
679 for (p = &runstate_transitions_def[0]; p->from != RUN_STATE__MAX; p++) {
680 runstate_valid_transitions[p->from][p->to] = true;
683 qemu_mutex_init(&vmstop_lock);
686 /* This function will abort() on invalid state transitions */
687 void runstate_set(RunState new_state)
689 assert(new_state < RUN_STATE__MAX);
691 if (current_run_state == new_state) {
692 return;
695 if (!runstate_valid_transitions[current_run_state][new_state]) {
696 error_report("invalid runstate transition: '%s' -> '%s'",
697 RunState_lookup[current_run_state],
698 RunState_lookup[new_state]);
699 abort();
701 trace_runstate_set(new_state);
702 current_run_state = new_state;
705 int runstate_is_running(void)
707 return runstate_check(RUN_STATE_RUNNING);
710 bool runstate_needs_reset(void)
712 return runstate_check(RUN_STATE_INTERNAL_ERROR) ||
713 runstate_check(RUN_STATE_SHUTDOWN);
716 StatusInfo *qmp_query_status(Error **errp)
718 StatusInfo *info = g_malloc0(sizeof(*info));
720 info->running = runstate_is_running();
721 info->singlestep = singlestep;
722 info->status = current_run_state;
724 return info;
727 bool qemu_vmstop_requested(RunState *r)
729 qemu_mutex_lock(&vmstop_lock);
730 *r = vmstop_requested;
731 vmstop_requested = RUN_STATE__MAX;
732 qemu_mutex_unlock(&vmstop_lock);
733 return *r < RUN_STATE__MAX;
736 void qemu_system_vmstop_request_prepare(void)
738 qemu_mutex_lock(&vmstop_lock);
741 void qemu_system_vmstop_request(RunState state)
743 vmstop_requested = state;
744 qemu_mutex_unlock(&vmstop_lock);
745 qemu_notify_event();
748 /***********************************************************/
749 /* real time host monotonic timer */
751 static time_t qemu_time(void)
753 return qemu_clock_get_ms(QEMU_CLOCK_HOST) / 1000;
756 /***********************************************************/
757 /* host time/date access */
758 void qemu_get_timedate(struct tm *tm, int offset)
760 time_t ti = qemu_time();
762 ti += offset;
763 if (rtc_date_offset == -1) {
764 if (rtc_utc)
765 gmtime_r(&ti, tm);
766 else
767 localtime_r(&ti, tm);
768 } else {
769 ti -= rtc_date_offset;
770 gmtime_r(&ti, tm);
774 int qemu_timedate_diff(struct tm *tm)
776 time_t seconds;
778 if (rtc_date_offset == -1)
779 if (rtc_utc)
780 seconds = mktimegm(tm);
781 else {
782 struct tm tmp = *tm;
783 tmp.tm_isdst = -1; /* use timezone to figure it out */
784 seconds = mktime(&tmp);
786 else
787 seconds = mktimegm(tm) + rtc_date_offset;
789 return seconds - qemu_time();
792 static void configure_rtc_date_offset(const char *startdate, int legacy)
794 time_t rtc_start_date;
795 struct tm tm;
797 if (!strcmp(startdate, "now") && legacy) {
798 rtc_date_offset = -1;
799 } else {
800 if (sscanf(startdate, "%d-%d-%dT%d:%d:%d",
801 &tm.tm_year,
802 &tm.tm_mon,
803 &tm.tm_mday,
804 &tm.tm_hour,
805 &tm.tm_min,
806 &tm.tm_sec) == 6) {
807 /* OK */
808 } else if (sscanf(startdate, "%d-%d-%d",
809 &tm.tm_year,
810 &tm.tm_mon,
811 &tm.tm_mday) == 3) {
812 tm.tm_hour = 0;
813 tm.tm_min = 0;
814 tm.tm_sec = 0;
815 } else {
816 goto date_fail;
818 tm.tm_year -= 1900;
819 tm.tm_mon--;
820 rtc_start_date = mktimegm(&tm);
821 if (rtc_start_date == -1) {
822 date_fail:
823 error_report("invalid date format");
824 error_printf("valid formats: "
825 "'2006-06-17T16:01:21' or '2006-06-17'\n");
826 exit(1);
828 rtc_date_offset = qemu_time() - rtc_start_date;
832 static void configure_rtc(QemuOpts *opts)
834 const char *value;
836 value = qemu_opt_get(opts, "base");
837 if (value) {
838 if (!strcmp(value, "utc")) {
839 rtc_utc = 1;
840 } else if (!strcmp(value, "localtime")) {
841 Error *blocker = NULL;
842 rtc_utc = 0;
843 error_setg(&blocker, QERR_REPLAY_NOT_SUPPORTED,
844 "-rtc base=localtime");
845 replay_add_blocker(blocker);
846 } else {
847 configure_rtc_date_offset(value, 0);
850 value = qemu_opt_get(opts, "clock");
851 if (value) {
852 if (!strcmp(value, "host")) {
853 rtc_clock = QEMU_CLOCK_HOST;
854 } else if (!strcmp(value, "rt")) {
855 rtc_clock = QEMU_CLOCK_REALTIME;
856 } else if (!strcmp(value, "vm")) {
857 rtc_clock = QEMU_CLOCK_VIRTUAL;
858 } else {
859 error_report("invalid option value '%s'", value);
860 exit(1);
863 value = qemu_opt_get(opts, "driftfix");
864 if (value) {
865 if (!strcmp(value, "slew")) {
866 static GlobalProperty slew_lost_ticks = {
867 .driver = "mc146818rtc",
868 .property = "lost_tick_policy",
869 .value = "slew",
872 qdev_prop_register_global(&slew_lost_ticks);
873 } else if (!strcmp(value, "none")) {
874 /* discard is default */
875 } else {
876 error_report("invalid option value '%s'", value);
877 exit(1);
882 /***********************************************************/
883 /* Bluetooth support */
884 static int nb_hcis;
885 static int cur_hci;
886 static struct HCIInfo *hci_table[MAX_NICS];
888 struct HCIInfo *qemu_next_hci(void)
890 if (cur_hci == nb_hcis)
891 return &null_hci;
893 return hci_table[cur_hci++];
896 static int bt_hci_parse(const char *str)
898 struct HCIInfo *hci;
899 bdaddr_t bdaddr;
901 if (nb_hcis >= MAX_NICS) {
902 error_report("too many bluetooth HCIs (max %i)", MAX_NICS);
903 return -1;
906 hci = hci_init(str);
907 if (!hci)
908 return -1;
910 bdaddr.b[0] = 0x52;
911 bdaddr.b[1] = 0x54;
912 bdaddr.b[2] = 0x00;
913 bdaddr.b[3] = 0x12;
914 bdaddr.b[4] = 0x34;
915 bdaddr.b[5] = 0x56 + nb_hcis;
916 hci->bdaddr_set(hci, bdaddr.b);
918 hci_table[nb_hcis++] = hci;
920 return 0;
923 static void bt_vhci_add(int vlan_id)
925 struct bt_scatternet_s *vlan = qemu_find_bt_vlan(vlan_id);
927 if (!vlan->slave)
928 error_report("warning: adding a VHCI to an empty scatternet %i",
929 vlan_id);
931 bt_vhci_init(bt_new_hci(vlan));
934 static struct bt_device_s *bt_device_add(const char *opt)
936 struct bt_scatternet_s *vlan;
937 int vlan_id = 0;
938 char *endp = strstr(opt, ",vlan=");
939 int len = (endp ? endp - opt : strlen(opt)) + 1;
940 char devname[10];
942 pstrcpy(devname, MIN(sizeof(devname), len), opt);
944 if (endp) {
945 vlan_id = strtol(endp + 6, &endp, 0);
946 if (*endp) {
947 error_report("unrecognised bluetooth vlan Id");
948 return 0;
952 vlan = qemu_find_bt_vlan(vlan_id);
954 if (!vlan->slave)
955 error_report("warning: adding a slave device to an empty scatternet %i",
956 vlan_id);
958 if (!strcmp(devname, "keyboard"))
959 return bt_keyboard_init(vlan);
961 error_report("unsupported bluetooth device '%s'", devname);
962 return 0;
965 static int bt_parse(const char *opt)
967 const char *endp, *p;
968 int vlan;
970 if (strstart(opt, "hci", &endp)) {
971 if (!*endp || *endp == ',') {
972 if (*endp)
973 if (!strstart(endp, ",vlan=", 0))
974 opt = endp + 1;
976 return bt_hci_parse(opt);
978 } else if (strstart(opt, "vhci", &endp)) {
979 if (!*endp || *endp == ',') {
980 if (*endp) {
981 if (strstart(endp, ",vlan=", &p)) {
982 vlan = strtol(p, (char **) &endp, 0);
983 if (*endp) {
984 error_report("bad scatternet '%s'", p);
985 return 1;
987 } else {
988 error_report("bad parameter '%s'", endp + 1);
989 return 1;
991 } else
992 vlan = 0;
994 bt_vhci_add(vlan);
995 return 0;
997 } else if (strstart(opt, "device:", &endp))
998 return !bt_device_add(endp);
1000 error_report("bad bluetooth parameter '%s'", opt);
1001 return 1;
1004 static int parse_sandbox(void *opaque, QemuOpts *opts, Error **errp)
1006 /* FIXME: change this to true for 1.3 */
1007 if (qemu_opt_get_bool(opts, "enable", false)) {
1008 #ifdef CONFIG_SECCOMP
1009 if (seccomp_start() < 0) {
1010 error_report("failed to install seccomp syscall filter "
1011 "in the kernel");
1012 return -1;
1014 #else
1015 error_report("seccomp support is disabled");
1016 return -1;
1017 #endif
1020 return 0;
1023 static int parse_name(void *opaque, QemuOpts *opts, Error **errp)
1025 const char *proc_name;
1027 if (qemu_opt_get(opts, "debug-threads")) {
1028 qemu_thread_naming(qemu_opt_get_bool(opts, "debug-threads", false));
1030 qemu_name = qemu_opt_get(opts, "guest");
1032 proc_name = qemu_opt_get(opts, "process");
1033 if (proc_name) {
1034 os_set_proc_name(proc_name);
1037 return 0;
1040 bool defaults_enabled(void)
1042 return has_defaults;
1045 #ifndef _WIN32
1046 static int parse_add_fd(void *opaque, QemuOpts *opts, Error **errp)
1048 int fd, dupfd, flags;
1049 int64_t fdset_id;
1050 const char *fd_opaque = NULL;
1051 AddfdInfo *fdinfo;
1053 fd = qemu_opt_get_number(opts, "fd", -1);
1054 fdset_id = qemu_opt_get_number(opts, "set", -1);
1055 fd_opaque = qemu_opt_get(opts, "opaque");
1057 if (fd < 0) {
1058 error_report("fd option is required and must be non-negative");
1059 return -1;
1062 if (fd <= STDERR_FILENO) {
1063 error_report("fd cannot be a standard I/O stream");
1064 return -1;
1068 * All fds inherited across exec() necessarily have FD_CLOEXEC
1069 * clear, while qemu sets FD_CLOEXEC on all other fds used internally.
1071 flags = fcntl(fd, F_GETFD);
1072 if (flags == -1 || (flags & FD_CLOEXEC)) {
1073 error_report("fd is not valid or already in use");
1074 return -1;
1077 if (fdset_id < 0) {
1078 error_report("set option is required and must be non-negative");
1079 return -1;
1082 #ifdef F_DUPFD_CLOEXEC
1083 dupfd = fcntl(fd, F_DUPFD_CLOEXEC, 0);
1084 #else
1085 dupfd = dup(fd);
1086 if (dupfd != -1) {
1087 qemu_set_cloexec(dupfd);
1089 #endif
1090 if (dupfd == -1) {
1091 error_report("error duplicating fd: %s", strerror(errno));
1092 return -1;
1095 /* add the duplicate fd, and optionally the opaque string, to the fd set */
1096 fdinfo = monitor_fdset_add_fd(dupfd, true, fdset_id, !!fd_opaque, fd_opaque,
1097 &error_abort);
1098 g_free(fdinfo);
1100 return 0;
1103 static int cleanup_add_fd(void *opaque, QemuOpts *opts, Error **errp)
1105 int fd;
1107 fd = qemu_opt_get_number(opts, "fd", -1);
1108 close(fd);
1110 return 0;
1112 #endif
1114 /***********************************************************/
1115 /* QEMU Block devices */
1117 #define HD_OPTS "media=disk"
1118 #define CDROM_OPTS "media=cdrom"
1119 #define FD_OPTS ""
1120 #define PFLASH_OPTS ""
1121 #define MTD_OPTS ""
1122 #define SD_OPTS ""
1124 static int drive_init_func(void *opaque, QemuOpts *opts, Error **errp)
1126 BlockInterfaceType *block_default_type = opaque;
1128 return drive_new(opts, *block_default_type) == NULL;
1131 static int drive_enable_snapshot(void *opaque, QemuOpts *opts, Error **errp)
1133 if (qemu_opt_get(opts, "snapshot") == NULL) {
1134 qemu_opt_set(opts, "snapshot", "on", &error_abort);
1136 return 0;
1139 static void default_drive(int enable, int snapshot, BlockInterfaceType type,
1140 int index, const char *optstr)
1142 QemuOpts *opts;
1143 DriveInfo *dinfo;
1145 if (!enable || drive_get_by_index(type, index)) {
1146 return;
1149 opts = drive_add(type, index, NULL, optstr);
1150 if (snapshot) {
1151 drive_enable_snapshot(NULL, opts, NULL);
1154 dinfo = drive_new(opts, type);
1155 if (!dinfo) {
1156 exit(1);
1158 dinfo->is_default = true;
1162 static QemuOptsList qemu_smp_opts = {
1163 .name = "smp-opts",
1164 .implied_opt_name = "cpus",
1165 .merge_lists = true,
1166 .head = QTAILQ_HEAD_INITIALIZER(qemu_smp_opts.head),
1167 .desc = {
1169 .name = "cpus",
1170 .type = QEMU_OPT_NUMBER,
1171 }, {
1172 .name = "sockets",
1173 .type = QEMU_OPT_NUMBER,
1174 }, {
1175 .name = "cores",
1176 .type = QEMU_OPT_NUMBER,
1177 }, {
1178 .name = "threads",
1179 .type = QEMU_OPT_NUMBER,
1180 }, {
1181 .name = "maxcpus",
1182 .type = QEMU_OPT_NUMBER,
1184 { /*End of list */ }
1188 static void smp_parse(QemuOpts *opts)
1190 if (opts) {
1191 unsigned cpus = qemu_opt_get_number(opts, "cpus", 0);
1192 unsigned sockets = qemu_opt_get_number(opts, "sockets", 0);
1193 unsigned cores = qemu_opt_get_number(opts, "cores", 0);
1194 unsigned threads = qemu_opt_get_number(opts, "threads", 0);
1196 /* compute missing values, prefer sockets over cores over threads */
1197 if (cpus == 0 || sockets == 0) {
1198 sockets = sockets > 0 ? sockets : 1;
1199 cores = cores > 0 ? cores : 1;
1200 threads = threads > 0 ? threads : 1;
1201 if (cpus == 0) {
1202 cpus = cores * threads * sockets;
1204 } else if (cores == 0) {
1205 threads = threads > 0 ? threads : 1;
1206 cores = cpus / (sockets * threads);
1207 cores = cores > 0 ? cores : 1;
1208 } else if (threads == 0) {
1209 threads = cpus / (cores * sockets);
1210 threads = threads > 0 ? threads : 1;
1211 } else if (sockets * cores * threads < cpus) {
1212 error_report("cpu topology: "
1213 "sockets (%u) * cores (%u) * threads (%u) < "
1214 "smp_cpus (%u)",
1215 sockets, cores, threads, cpus);
1216 exit(1);
1219 max_cpus = qemu_opt_get_number(opts, "maxcpus", cpus);
1221 if (max_cpus < cpus) {
1222 error_report("maxcpus must be equal to or greater than smp");
1223 exit(1);
1226 if (sockets * cores * threads > max_cpus) {
1227 error_report("cpu topology: "
1228 "sockets (%u) * cores (%u) * threads (%u) > "
1229 "maxcpus (%u)",
1230 sockets, cores, threads, max_cpus);
1231 exit(1);
1234 smp_cpus = cpus;
1235 smp_cores = cores;
1236 smp_threads = threads;
1239 if (smp_cpus > 1) {
1240 Error *blocker = NULL;
1241 error_setg(&blocker, QERR_REPLAY_NOT_SUPPORTED, "smp");
1242 replay_add_blocker(blocker);
1246 static void realtime_init(void)
1248 if (enable_mlock) {
1249 if (os_mlock() < 0) {
1250 error_report("locking memory failed");
1251 exit(1);
1257 static void configure_msg(QemuOpts *opts)
1259 enable_timestamp_msg = qemu_opt_get_bool(opts, "timestamp", true);
1262 /***********************************************************/
1263 /* Semihosting */
1265 typedef struct SemihostingConfig {
1266 bool enabled;
1267 SemihostingTarget target;
1268 const char **argv;
1269 int argc;
1270 const char *cmdline; /* concatenated argv */
1271 } SemihostingConfig;
1273 static SemihostingConfig semihosting;
1275 bool semihosting_enabled(void)
1277 return semihosting.enabled;
1280 SemihostingTarget semihosting_get_target(void)
1282 return semihosting.target;
1285 const char *semihosting_get_arg(int i)
1287 if (i >= semihosting.argc) {
1288 return NULL;
1290 return semihosting.argv[i];
1293 int semihosting_get_argc(void)
1295 return semihosting.argc;
1298 const char *semihosting_get_cmdline(void)
1300 if (semihosting.cmdline == NULL && semihosting.argc > 0) {
1301 semihosting.cmdline = g_strjoinv(" ", (gchar **)semihosting.argv);
1303 return semihosting.cmdline;
1306 static int add_semihosting_arg(void *opaque,
1307 const char *name, const char *val,
1308 Error **errp)
1310 SemihostingConfig *s = opaque;
1311 if (strcmp(name, "arg") == 0) {
1312 s->argc++;
1313 /* one extra element as g_strjoinv() expects NULL-terminated array */
1314 s->argv = g_realloc(s->argv, (s->argc + 1) * sizeof(void *));
1315 s->argv[s->argc - 1] = val;
1316 s->argv[s->argc] = NULL;
1318 return 0;
1321 /* Use strings passed via -kernel/-append to initialize semihosting.argv[] */
1322 static inline void semihosting_arg_fallback(const char *file, const char *cmd)
1324 char *cmd_token;
1326 /* argv[0] */
1327 add_semihosting_arg(&semihosting, "arg", file, NULL);
1329 /* split -append and initialize argv[1..n] */
1330 cmd_token = strtok(g_strdup(cmd), " ");
1331 while (cmd_token) {
1332 add_semihosting_arg(&semihosting, "arg", cmd_token, NULL);
1333 cmd_token = strtok(NULL, " ");
1337 /* Now we still need this for compatibility with XEN. */
1338 bool has_igd_gfx_passthru;
1339 static void igd_gfx_passthru(void)
1341 has_igd_gfx_passthru = current_machine->igd_gfx_passthru;
1344 /***********************************************************/
1345 /* USB devices */
1347 static int usb_device_add(const char *devname)
1349 USBDevice *dev = NULL;
1350 #ifndef CONFIG_LINUX
1351 const char *p;
1352 #endif
1354 if (!machine_usb(current_machine)) {
1355 return -1;
1358 /* drivers with .usbdevice_name entry in USBDeviceInfo */
1359 dev = usbdevice_create(devname);
1360 if (dev)
1361 goto done;
1363 /* the other ones */
1364 #ifndef CONFIG_LINUX
1365 /* only the linux version is qdev-ified, usb-bsd still needs this */
1366 if (strstart(devname, "host:", &p)) {
1367 dev = usb_host_device_open(usb_bus_find(-1), p);
1369 #endif
1370 if (!dev)
1371 return -1;
1373 done:
1374 return 0;
1377 static int usb_device_del(const char *devname)
1379 int bus_num, addr;
1380 const char *p;
1382 if (strstart(devname, "host:", &p)) {
1383 return -1;
1386 if (!machine_usb(current_machine)) {
1387 return -1;
1390 p = strchr(devname, '.');
1391 if (!p)
1392 return -1;
1393 bus_num = strtoul(devname, NULL, 0);
1394 addr = strtoul(p + 1, NULL, 0);
1396 return usb_device_delete_addr(bus_num, addr);
1399 static int usb_parse(const char *cmdline)
1401 int r;
1402 r = usb_device_add(cmdline);
1403 if (r < 0) {
1404 error_report("could not add USB device '%s'", cmdline);
1406 return r;
1409 void hmp_usb_add(Monitor *mon, const QDict *qdict)
1411 const char *devname = qdict_get_str(qdict, "devname");
1412 if (usb_device_add(devname) < 0) {
1413 error_report("could not add USB device '%s'", devname);
1417 void hmp_usb_del(Monitor *mon, const QDict *qdict)
1419 const char *devname = qdict_get_str(qdict, "devname");
1420 if (usb_device_del(devname) < 0) {
1421 error_report("could not delete USB device '%s'", devname);
1425 /***********************************************************/
1426 /* machine registration */
1428 MachineState *current_machine;
1430 static MachineClass *find_machine(const char *name)
1432 GSList *el, *machines = object_class_get_list(TYPE_MACHINE, false);
1433 MachineClass *mc = NULL;
1435 for (el = machines; el; el = el->next) {
1436 MachineClass *temp = el->data;
1438 if (!strcmp(temp->name, name)) {
1439 mc = temp;
1440 break;
1442 if (temp->alias &&
1443 !strcmp(temp->alias, name)) {
1444 mc = temp;
1445 break;
1449 g_slist_free(machines);
1450 return mc;
1453 MachineClass *find_default_machine(void)
1455 GSList *el, *machines = object_class_get_list(TYPE_MACHINE, false);
1456 MachineClass *mc = NULL;
1458 for (el = machines; el; el = el->next) {
1459 MachineClass *temp = el->data;
1461 if (temp->is_default) {
1462 mc = temp;
1463 break;
1467 g_slist_free(machines);
1468 return mc;
1471 MachineInfoList *qmp_query_machines(Error **errp)
1473 GSList *el, *machines = object_class_get_list(TYPE_MACHINE, false);
1474 MachineInfoList *mach_list = NULL;
1476 for (el = machines; el; el = el->next) {
1477 MachineClass *mc = el->data;
1478 MachineInfoList *entry;
1479 MachineInfo *info;
1481 info = g_malloc0(sizeof(*info));
1482 if (mc->is_default) {
1483 info->has_is_default = true;
1484 info->is_default = true;
1487 if (mc->alias) {
1488 info->has_alias = true;
1489 info->alias = g_strdup(mc->alias);
1492 info->name = g_strdup(mc->name);
1493 info->cpu_max = !mc->max_cpus ? 1 : mc->max_cpus;
1494 info->hotpluggable_cpus = !!mc->query_hotpluggable_cpus;
1496 entry = g_malloc0(sizeof(*entry));
1497 entry->value = info;
1498 entry->next = mach_list;
1499 mach_list = entry;
1502 g_slist_free(machines);
1503 return mach_list;
1506 static int machine_help_func(QemuOpts *opts, MachineState *machine)
1508 ObjectProperty *prop;
1509 ObjectPropertyIterator iter;
1511 if (!qemu_opt_has_help_opt(opts)) {
1512 return 0;
1515 object_property_iter_init(&iter, OBJECT(machine));
1516 while ((prop = object_property_iter_next(&iter))) {
1517 if (!prop->set) {
1518 continue;
1521 error_printf("%s.%s=%s", MACHINE_GET_CLASS(machine)->name,
1522 prop->name, prop->type);
1523 if (prop->description) {
1524 error_printf(" (%s)\n", prop->description);
1525 } else {
1526 error_printf("\n");
1530 return 1;
1533 /***********************************************************/
1534 /* main execution loop */
1536 struct vm_change_state_entry {
1537 VMChangeStateHandler *cb;
1538 void *opaque;
1539 QLIST_ENTRY (vm_change_state_entry) entries;
1542 static QLIST_HEAD(vm_change_state_head, vm_change_state_entry) vm_change_state_head;
1544 VMChangeStateEntry *qemu_add_vm_change_state_handler(VMChangeStateHandler *cb,
1545 void *opaque)
1547 VMChangeStateEntry *e;
1549 e = g_malloc0(sizeof (*e));
1551 e->cb = cb;
1552 e->opaque = opaque;
1553 QLIST_INSERT_HEAD(&vm_change_state_head, e, entries);
1554 return e;
1557 void qemu_del_vm_change_state_handler(VMChangeStateEntry *e)
1559 QLIST_REMOVE (e, entries);
1560 g_free (e);
1563 void vm_state_notify(int running, RunState state)
1565 VMChangeStateEntry *e, *next;
1567 trace_vm_state_notify(running, state);
1569 QLIST_FOREACH_SAFE(e, &vm_change_state_head, entries, next) {
1570 e->cb(e->opaque, running, state);
1574 static int reset_requested;
1575 static int shutdown_requested, shutdown_signal = -1;
1576 static pid_t shutdown_pid;
1577 static int powerdown_requested;
1578 static int debug_requested;
1579 static int suspend_requested;
1580 static WakeupReason wakeup_reason;
1581 static NotifierList powerdown_notifiers =
1582 NOTIFIER_LIST_INITIALIZER(powerdown_notifiers);
1583 static NotifierList suspend_notifiers =
1584 NOTIFIER_LIST_INITIALIZER(suspend_notifiers);
1585 static NotifierList wakeup_notifiers =
1586 NOTIFIER_LIST_INITIALIZER(wakeup_notifiers);
1587 static uint32_t wakeup_reason_mask = ~(1 << QEMU_WAKEUP_REASON_NONE);
1589 int qemu_shutdown_requested_get(void)
1591 return shutdown_requested;
1594 int qemu_reset_requested_get(void)
1596 return reset_requested;
1599 static int qemu_shutdown_requested(void)
1601 return atomic_xchg(&shutdown_requested, 0);
1604 static void qemu_kill_report(void)
1606 if (!qtest_driver() && shutdown_signal != -1) {
1607 if (shutdown_pid == 0) {
1608 /* This happens for eg ^C at the terminal, so it's worth
1609 * avoiding printing an odd message in that case.
1611 error_report("terminating on signal %d", shutdown_signal);
1612 } else {
1613 char *shutdown_cmd = qemu_get_pid_name(shutdown_pid);
1615 error_report("terminating on signal %d from pid " FMT_pid " (%s)",
1616 shutdown_signal, shutdown_pid,
1617 shutdown_cmd ? shutdown_cmd : "<unknown process>");
1618 g_free(shutdown_cmd);
1620 shutdown_signal = -1;
1624 static int qemu_reset_requested(void)
1626 int r = reset_requested;
1627 if (r && replay_checkpoint(CHECKPOINT_RESET_REQUESTED)) {
1628 reset_requested = 0;
1629 return r;
1631 return false;
1634 static int qemu_suspend_requested(void)
1636 int r = suspend_requested;
1637 if (r && replay_checkpoint(CHECKPOINT_SUSPEND_REQUESTED)) {
1638 suspend_requested = 0;
1639 return r;
1641 return false;
1644 static WakeupReason qemu_wakeup_requested(void)
1646 return wakeup_reason;
1649 static int qemu_powerdown_requested(void)
1651 int r = powerdown_requested;
1652 powerdown_requested = 0;
1653 return r;
1656 static int qemu_debug_requested(void)
1658 int r = debug_requested;
1659 debug_requested = 0;
1660 return r;
1663 void qemu_system_reset(bool report)
1665 MachineClass *mc;
1667 mc = current_machine ? MACHINE_GET_CLASS(current_machine) : NULL;
1669 cpu_synchronize_all_states();
1671 if (mc && mc->reset) {
1672 mc->reset();
1673 } else {
1674 qemu_devices_reset();
1676 if (report) {
1677 qapi_event_send_reset(&error_abort);
1679 cpu_synchronize_all_post_reset();
1682 void qemu_system_guest_panicked(GuestPanicInformation *info)
1684 qemu_log_mask(LOG_GUEST_ERROR, "Guest crashed\n");
1686 if (current_cpu) {
1687 current_cpu->crash_occurred = true;
1689 qapi_event_send_guest_panicked(GUEST_PANIC_ACTION_PAUSE,
1690 !!info, info, &error_abort);
1691 vm_stop(RUN_STATE_GUEST_PANICKED);
1692 if (!no_shutdown) {
1693 qapi_event_send_guest_panicked(GUEST_PANIC_ACTION_POWEROFF,
1694 !!info, info, &error_abort);
1695 qemu_system_shutdown_request();
1698 if (info) {
1699 if (info->type == GUEST_PANIC_INFORMATION_KIND_HYPER_V) {
1700 qemu_log_mask(LOG_GUEST_ERROR, "HV crash parameters: (%#"PRIx64
1701 " %#"PRIx64" %#"PRIx64" %#"PRIx64" %#"PRIx64")\n",
1702 info->u.hyper_v.data->arg1,
1703 info->u.hyper_v.data->arg2,
1704 info->u.hyper_v.data->arg3,
1705 info->u.hyper_v.data->arg4,
1706 info->u.hyper_v.data->arg5);
1708 qapi_free_GuestPanicInformation(info);
1712 void qemu_system_reset_request(void)
1714 if (no_reboot) {
1715 shutdown_requested = 1;
1716 } else {
1717 reset_requested = 1;
1719 cpu_stop_current();
1720 qemu_notify_event();
1723 static void qemu_system_suspend(void)
1725 pause_all_vcpus();
1726 notifier_list_notify(&suspend_notifiers, NULL);
1727 runstate_set(RUN_STATE_SUSPENDED);
1728 qapi_event_send_suspend(&error_abort);
1731 void qemu_system_suspend_request(void)
1733 if (runstate_check(RUN_STATE_SUSPENDED)) {
1734 return;
1736 suspend_requested = 1;
1737 cpu_stop_current();
1738 qemu_notify_event();
1741 void qemu_register_suspend_notifier(Notifier *notifier)
1743 notifier_list_add(&suspend_notifiers, notifier);
1746 void qemu_system_wakeup_request(WakeupReason reason)
1748 trace_system_wakeup_request(reason);
1750 if (!runstate_check(RUN_STATE_SUSPENDED)) {
1751 return;
1753 if (!(wakeup_reason_mask & (1 << reason))) {
1754 return;
1756 runstate_set(RUN_STATE_RUNNING);
1757 wakeup_reason = reason;
1758 qemu_notify_event();
1761 void qemu_system_wakeup_enable(WakeupReason reason, bool enabled)
1763 if (enabled) {
1764 wakeup_reason_mask |= (1 << reason);
1765 } else {
1766 wakeup_reason_mask &= ~(1 << reason);
1770 void qemu_register_wakeup_notifier(Notifier *notifier)
1772 notifier_list_add(&wakeup_notifiers, notifier);
1775 void qemu_system_killed(int signal, pid_t pid)
1777 shutdown_signal = signal;
1778 shutdown_pid = pid;
1779 no_shutdown = 0;
1781 /* Cannot call qemu_system_shutdown_request directly because
1782 * we are in a signal handler.
1784 shutdown_requested = 1;
1785 qemu_notify_event();
1788 void qemu_system_shutdown_request(void)
1790 trace_qemu_system_shutdown_request();
1791 replay_shutdown_request();
1792 shutdown_requested = 1;
1793 qemu_notify_event();
1796 static void qemu_system_powerdown(void)
1798 qapi_event_send_powerdown(&error_abort);
1799 notifier_list_notify(&powerdown_notifiers, NULL);
1802 void qemu_system_powerdown_request(void)
1804 trace_qemu_system_powerdown_request();
1805 powerdown_requested = 1;
1806 qemu_notify_event();
1809 void qemu_register_powerdown_notifier(Notifier *notifier)
1811 notifier_list_add(&powerdown_notifiers, notifier);
1814 void qemu_system_debug_request(void)
1816 debug_requested = 1;
1817 qemu_notify_event();
1820 static bool main_loop_should_exit(void)
1822 RunState r;
1823 if (qemu_debug_requested()) {
1824 vm_stop(RUN_STATE_DEBUG);
1826 if (qemu_suspend_requested()) {
1827 qemu_system_suspend();
1829 if (qemu_shutdown_requested()) {
1830 qemu_kill_report();
1831 qapi_event_send_shutdown(&error_abort);
1832 if (no_shutdown) {
1833 vm_stop(RUN_STATE_SHUTDOWN);
1834 } else {
1835 return true;
1838 if (qemu_reset_requested()) {
1839 pause_all_vcpus();
1840 qemu_system_reset(VMRESET_REPORT);
1841 resume_all_vcpus();
1842 if (!runstate_check(RUN_STATE_RUNNING) &&
1843 !runstate_check(RUN_STATE_INMIGRATE)) {
1844 runstate_set(RUN_STATE_PRELAUNCH);
1847 if (qemu_wakeup_requested()) {
1848 pause_all_vcpus();
1849 qemu_system_reset(VMRESET_SILENT);
1850 notifier_list_notify(&wakeup_notifiers, &wakeup_reason);
1851 wakeup_reason = QEMU_WAKEUP_REASON_NONE;
1852 resume_all_vcpus();
1853 qapi_event_send_wakeup(&error_abort);
1855 if (qemu_powerdown_requested()) {
1856 qemu_system_powerdown();
1858 if (qemu_vmstop_requested(&r)) {
1859 vm_stop(r);
1861 return false;
1864 static void main_loop(void)
1866 bool nonblocking;
1867 int last_io = 0;
1868 #ifdef CONFIG_PROFILER
1869 int64_t ti;
1870 #endif
1871 do {
1872 nonblocking = tcg_enabled() && last_io > 0;
1873 #ifdef CONFIG_PROFILER
1874 ti = profile_getclock();
1875 #endif
1876 last_io = main_loop_wait(nonblocking);
1877 #ifdef CONFIG_PROFILER
1878 dev_time += profile_getclock() - ti;
1879 #endif
1880 } while (!main_loop_should_exit());
1883 static void version(void)
1885 printf("QEMU emulator version " QEMU_VERSION QEMU_PKGVERSION "\n"
1886 QEMU_COPYRIGHT "\n");
1889 static void help(int exitcode)
1891 version();
1892 printf("usage: %s [options] [disk_image]\n\n"
1893 "'disk_image' is a raw hard disk image for IDE hard disk 0\n\n",
1894 error_get_progname());
1896 #define QEMU_OPTIONS_GENERATE_HELP
1897 #include "qemu-options-wrapper.h"
1899 printf("\nDuring emulation, the following keys are useful:\n"
1900 "ctrl-alt-f toggle full screen\n"
1901 "ctrl-alt-n switch to virtual console 'n'\n"
1902 "ctrl-alt toggle mouse and keyboard grab\n"
1903 "\n"
1904 "When using -nographic, press 'ctrl-a h' to get some help.\n");
1906 exit(exitcode);
1909 #define HAS_ARG 0x0001
1911 typedef struct QEMUOption {
1912 const char *name;
1913 int flags;
1914 int index;
1915 uint32_t arch_mask;
1916 } QEMUOption;
1918 static const QEMUOption qemu_options[] = {
1919 { "h", 0, QEMU_OPTION_h, QEMU_ARCH_ALL },
1920 #define QEMU_OPTIONS_GENERATE_OPTIONS
1921 #include "qemu-options-wrapper.h"
1922 { NULL },
1925 typedef struct VGAInterfaceInfo {
1926 const char *opt_name; /* option name */
1927 const char *name; /* human-readable name */
1928 /* Class names indicating that support is available.
1929 * If no class is specified, the interface is always available */
1930 const char *class_names[2];
1931 } VGAInterfaceInfo;
1933 static VGAInterfaceInfo vga_interfaces[VGA_TYPE_MAX] = {
1934 [VGA_NONE] = {
1935 .opt_name = "none",
1937 [VGA_STD] = {
1938 .opt_name = "std",
1939 .name = "standard VGA",
1940 .class_names = { "VGA", "isa-vga" },
1942 [VGA_CIRRUS] = {
1943 .opt_name = "cirrus",
1944 .name = "Cirrus VGA",
1945 .class_names = { "cirrus-vga", "isa-cirrus-vga" },
1947 [VGA_VMWARE] = {
1948 .opt_name = "vmware",
1949 .name = "VMWare SVGA",
1950 .class_names = { "vmware-svga" },
1952 [VGA_VIRTIO] = {
1953 .opt_name = "virtio",
1954 .name = "Virtio VGA",
1955 .class_names = { "virtio-vga" },
1957 [VGA_QXL] = {
1958 .opt_name = "qxl",
1959 .name = "QXL VGA",
1960 .class_names = { "qxl-vga" },
1962 [VGA_TCX] = {
1963 .opt_name = "tcx",
1964 .name = "TCX framebuffer",
1965 .class_names = { "SUNW,tcx" },
1967 [VGA_CG3] = {
1968 .opt_name = "cg3",
1969 .name = "CG3 framebuffer",
1970 .class_names = { "cgthree" },
1972 [VGA_XENFB] = {
1973 .opt_name = "xenfb",
1977 static bool vga_interface_available(VGAInterfaceType t)
1979 VGAInterfaceInfo *ti = &vga_interfaces[t];
1981 assert(t < VGA_TYPE_MAX);
1982 return !ti->class_names[0] ||
1983 object_class_by_name(ti->class_names[0]) ||
1984 object_class_by_name(ti->class_names[1]);
1987 static void select_vgahw(const char *p)
1989 const char *opts;
1990 int t;
1992 assert(vga_interface_type == VGA_NONE);
1993 for (t = 0; t < VGA_TYPE_MAX; t++) {
1994 VGAInterfaceInfo *ti = &vga_interfaces[t];
1995 if (ti->opt_name && strstart(p, ti->opt_name, &opts)) {
1996 if (!vga_interface_available(t)) {
1997 error_report("%s not available", ti->name);
1998 exit(1);
2000 vga_interface_type = t;
2001 break;
2004 if (t == VGA_TYPE_MAX) {
2005 invalid_vga:
2006 error_report("unknown vga type: %s", p);
2007 exit(1);
2009 while (*opts) {
2010 const char *nextopt;
2012 if (strstart(opts, ",retrace=", &nextopt)) {
2013 opts = nextopt;
2014 if (strstart(opts, "dumb", &nextopt))
2015 vga_retrace_method = VGA_RETRACE_DUMB;
2016 else if (strstart(opts, "precise", &nextopt))
2017 vga_retrace_method = VGA_RETRACE_PRECISE;
2018 else goto invalid_vga;
2019 } else goto invalid_vga;
2020 opts = nextopt;
2024 typedef enum DisplayType {
2025 DT_DEFAULT,
2026 DT_CURSES,
2027 DT_SDL,
2028 DT_COCOA,
2029 DT_GTK,
2030 DT_NONE,
2031 } DisplayType;
2033 static DisplayType select_display(const char *p)
2035 const char *opts;
2036 DisplayType display = DT_DEFAULT;
2038 if (strstart(p, "sdl", &opts)) {
2039 #ifdef CONFIG_SDL
2040 display = DT_SDL;
2041 while (*opts) {
2042 const char *nextopt;
2044 if (strstart(opts, ",frame=", &nextopt)) {
2045 opts = nextopt;
2046 if (strstart(opts, "on", &nextopt)) {
2047 no_frame = 0;
2048 } else if (strstart(opts, "off", &nextopt)) {
2049 no_frame = 1;
2050 } else {
2051 goto invalid_sdl_args;
2053 } else if (strstart(opts, ",alt_grab=", &nextopt)) {
2054 opts = nextopt;
2055 if (strstart(opts, "on", &nextopt)) {
2056 alt_grab = 1;
2057 } else if (strstart(opts, "off", &nextopt)) {
2058 alt_grab = 0;
2059 } else {
2060 goto invalid_sdl_args;
2062 } else if (strstart(opts, ",ctrl_grab=", &nextopt)) {
2063 opts = nextopt;
2064 if (strstart(opts, "on", &nextopt)) {
2065 ctrl_grab = 1;
2066 } else if (strstart(opts, "off", &nextopt)) {
2067 ctrl_grab = 0;
2068 } else {
2069 goto invalid_sdl_args;
2071 } else if (strstart(opts, ",window_close=", &nextopt)) {
2072 opts = nextopt;
2073 if (strstart(opts, "on", &nextopt)) {
2074 no_quit = 0;
2075 } else if (strstart(opts, "off", &nextopt)) {
2076 no_quit = 1;
2077 } else {
2078 goto invalid_sdl_args;
2080 } else if (strstart(opts, ",gl=", &nextopt)) {
2081 opts = nextopt;
2082 if (strstart(opts, "on", &nextopt)) {
2083 request_opengl = 1;
2084 } else if (strstart(opts, "off", &nextopt)) {
2085 request_opengl = 0;
2086 } else {
2087 goto invalid_sdl_args;
2089 } else {
2090 invalid_sdl_args:
2091 error_report("invalid SDL option string");
2092 exit(1);
2094 opts = nextopt;
2096 #else
2097 error_report("SDL support is disabled");
2098 exit(1);
2099 #endif
2100 } else if (strstart(p, "vnc", &opts)) {
2101 if (*opts == '=') {
2102 vnc_parse(opts + 1, &error_fatal);
2103 } else {
2104 error_report("VNC requires a display argument vnc=<display>");
2105 exit(1);
2107 } else if (strstart(p, "curses", &opts)) {
2108 #ifdef CONFIG_CURSES
2109 display = DT_CURSES;
2110 #else
2111 error_report("curses support is disabled");
2112 exit(1);
2113 #endif
2114 } else if (strstart(p, "gtk", &opts)) {
2115 #ifdef CONFIG_GTK
2116 display = DT_GTK;
2117 while (*opts) {
2118 const char *nextopt;
2120 if (strstart(opts, ",grab_on_hover=", &nextopt)) {
2121 opts = nextopt;
2122 if (strstart(opts, "on", &nextopt)) {
2123 grab_on_hover = true;
2124 } else if (strstart(opts, "off", &nextopt)) {
2125 grab_on_hover = false;
2126 } else {
2127 goto invalid_gtk_args;
2129 } else if (strstart(opts, ",gl=", &nextopt)) {
2130 opts = nextopt;
2131 if (strstart(opts, "on", &nextopt)) {
2132 request_opengl = 1;
2133 } else if (strstart(opts, "off", &nextopt)) {
2134 request_opengl = 0;
2135 } else {
2136 goto invalid_gtk_args;
2138 } else {
2139 invalid_gtk_args:
2140 error_report("invalid GTK option string");
2141 exit(1);
2143 opts = nextopt;
2145 #else
2146 error_report("GTK support is disabled");
2147 exit(1);
2148 #endif
2149 } else if (strstart(p, "none", &opts)) {
2150 display = DT_NONE;
2151 } else {
2152 error_report("unknown display type");
2153 exit(1);
2156 return display;
2159 static int balloon_parse(const char *arg)
2161 QemuOpts *opts;
2163 if (strcmp(arg, "none") == 0) {
2164 return 0;
2167 if (!strncmp(arg, "virtio", 6)) {
2168 if (arg[6] == ',') {
2169 /* have params -> parse them */
2170 opts = qemu_opts_parse_noisily(qemu_find_opts("device"), arg + 7,
2171 false);
2172 if (!opts)
2173 return -1;
2174 } else {
2175 /* create empty opts */
2176 opts = qemu_opts_create(qemu_find_opts("device"), NULL, 0,
2177 &error_abort);
2179 qemu_opt_set(opts, "driver", "virtio-balloon", &error_abort);
2180 return 0;
2183 return -1;
2186 char *qemu_find_file(int type, const char *name)
2188 int i;
2189 const char *subdir;
2190 char *buf;
2192 /* Try the name as a straight path first */
2193 if (access(name, R_OK) == 0) {
2194 trace_load_file(name, name);
2195 return g_strdup(name);
2198 switch (type) {
2199 case QEMU_FILE_TYPE_BIOS:
2200 subdir = "";
2201 break;
2202 case QEMU_FILE_TYPE_KEYMAP:
2203 subdir = "keymaps/";
2204 break;
2205 default:
2206 abort();
2209 for (i = 0; i < data_dir_idx; i++) {
2210 buf = g_strdup_printf("%s/%s%s", data_dir[i], subdir, name);
2211 if (access(buf, R_OK) == 0) {
2212 trace_load_file(name, buf);
2213 return buf;
2215 g_free(buf);
2217 return NULL;
2220 static inline bool nonempty_str(const char *str)
2222 return str && *str;
2225 static int parse_fw_cfg(void *opaque, QemuOpts *opts, Error **errp)
2227 gchar *buf;
2228 size_t size;
2229 const char *name, *file, *str;
2230 FWCfgState *fw_cfg = (FWCfgState *) opaque;
2232 if (fw_cfg == NULL) {
2233 error_report("fw_cfg device not available");
2234 return -1;
2236 name = qemu_opt_get(opts, "name");
2237 file = qemu_opt_get(opts, "file");
2238 str = qemu_opt_get(opts, "string");
2240 /* we need name and either a file or the content string */
2241 if (!(nonempty_str(name) && (nonempty_str(file) || nonempty_str(str)))) {
2242 error_report("invalid argument(s)");
2243 return -1;
2245 if (nonempty_str(file) && nonempty_str(str)) {
2246 error_report("file and string are mutually exclusive");
2247 return -1;
2249 if (strlen(name) > FW_CFG_MAX_FILE_PATH - 1) {
2250 error_report("name too long (max. %d char)", FW_CFG_MAX_FILE_PATH - 1);
2251 return -1;
2253 if (strncmp(name, "opt/", 4) != 0) {
2254 error_report("warning: externally provided fw_cfg item names "
2255 "should be prefixed with \"opt/\"");
2257 if (nonempty_str(str)) {
2258 size = strlen(str); /* NUL terminator NOT included in fw_cfg blob */
2259 buf = g_memdup(str, size);
2260 } else {
2261 if (!g_file_get_contents(file, &buf, &size, NULL)) {
2262 error_report("can't load %s", file);
2263 return -1;
2266 /* For legacy, keep user files in a specific global order. */
2267 fw_cfg_set_order_override(fw_cfg, FW_CFG_ORDER_OVERRIDE_USER);
2268 fw_cfg_add_file(fw_cfg, name, buf, size);
2269 fw_cfg_reset_order_override(fw_cfg);
2270 return 0;
2273 static int device_help_func(void *opaque, QemuOpts *opts, Error **errp)
2275 return qdev_device_help(opts);
2278 static int device_init_func(void *opaque, QemuOpts *opts, Error **errp)
2280 Error *err = NULL;
2281 DeviceState *dev;
2283 dev = qdev_device_add(opts, &err);
2284 if (!dev) {
2285 error_report_err(err);
2286 return -1;
2288 object_unref(OBJECT(dev));
2289 return 0;
2292 static int chardev_init_func(void *opaque, QemuOpts *opts, Error **errp)
2294 Error *local_err = NULL;
2296 qemu_chr_new_from_opts(opts, &local_err);
2297 if (local_err) {
2298 error_report_err(local_err);
2299 return -1;
2301 return 0;
2304 #ifdef CONFIG_VIRTFS
2305 static int fsdev_init_func(void *opaque, QemuOpts *opts, Error **errp)
2307 return qemu_fsdev_add(opts);
2309 #endif
2311 static int mon_init_func(void *opaque, QemuOpts *opts, Error **errp)
2313 Chardev *chr;
2314 const char *chardev;
2315 const char *mode;
2316 int flags;
2318 mode = qemu_opt_get(opts, "mode");
2319 if (mode == NULL) {
2320 mode = "readline";
2322 if (strcmp(mode, "readline") == 0) {
2323 flags = MONITOR_USE_READLINE;
2324 } else if (strcmp(mode, "control") == 0) {
2325 flags = MONITOR_USE_CONTROL;
2326 } else {
2327 error_report("unknown monitor mode \"%s\"", mode);
2328 exit(1);
2331 if (qemu_opt_get_bool(opts, "pretty", 0))
2332 flags |= MONITOR_USE_PRETTY;
2334 if (qemu_opt_get_bool(opts, "default", 0)) {
2335 error_report("option 'default' does nothing and is deprecated");
2338 chardev = qemu_opt_get(opts, "chardev");
2339 chr = qemu_chr_find(chardev);
2340 if (chr == NULL) {
2341 error_report("chardev \"%s\" not found", chardev);
2342 exit(1);
2345 monitor_init(chr, flags);
2346 return 0;
2349 static void monitor_parse(const char *optarg, const char *mode, bool pretty)
2351 static int monitor_device_index = 0;
2352 QemuOpts *opts;
2353 const char *p;
2354 char label[32];
2356 if (strstart(optarg, "chardev:", &p)) {
2357 snprintf(label, sizeof(label), "%s", p);
2358 } else {
2359 snprintf(label, sizeof(label), "compat_monitor%d",
2360 monitor_device_index);
2361 opts = qemu_chr_parse_compat(label, optarg);
2362 if (!opts) {
2363 error_report("parse error: %s", optarg);
2364 exit(1);
2368 opts = qemu_opts_create(qemu_find_opts("mon"), label, 1, &error_fatal);
2369 qemu_opt_set(opts, "mode", mode, &error_abort);
2370 qemu_opt_set(opts, "chardev", label, &error_abort);
2371 qemu_opt_set_bool(opts, "pretty", pretty, &error_abort);
2372 monitor_device_index++;
2375 struct device_config {
2376 enum {
2377 DEV_USB, /* -usbdevice */
2378 DEV_BT, /* -bt */
2379 DEV_SERIAL, /* -serial */
2380 DEV_PARALLEL, /* -parallel */
2381 DEV_VIRTCON, /* -virtioconsole */
2382 DEV_DEBUGCON, /* -debugcon */
2383 DEV_GDB, /* -gdb, -s */
2384 DEV_SCLP, /* s390 sclp */
2385 } type;
2386 const char *cmdline;
2387 Location loc;
2388 QTAILQ_ENTRY(device_config) next;
2391 static QTAILQ_HEAD(, device_config) device_configs =
2392 QTAILQ_HEAD_INITIALIZER(device_configs);
2394 static void add_device_config(int type, const char *cmdline)
2396 struct device_config *conf;
2398 conf = g_malloc0(sizeof(*conf));
2399 conf->type = type;
2400 conf->cmdline = cmdline;
2401 loc_save(&conf->loc);
2402 QTAILQ_INSERT_TAIL(&device_configs, conf, next);
2405 static int foreach_device_config(int type, int (*func)(const char *cmdline))
2407 struct device_config *conf;
2408 int rc;
2410 QTAILQ_FOREACH(conf, &device_configs, next) {
2411 if (conf->type != type)
2412 continue;
2413 loc_push_restore(&conf->loc);
2414 rc = func(conf->cmdline);
2415 loc_pop(&conf->loc);
2416 if (rc) {
2417 return rc;
2420 return 0;
2423 static int serial_parse(const char *devname)
2425 static int index = 0;
2426 char label[32];
2428 if (strcmp(devname, "none") == 0)
2429 return 0;
2430 if (index == MAX_SERIAL_PORTS) {
2431 error_report("too many serial ports");
2432 exit(1);
2434 snprintf(label, sizeof(label), "serial%d", index);
2435 serial_hds[index] = qemu_chr_new(label, devname);
2436 if (!serial_hds[index]) {
2437 error_report("could not connect serial device"
2438 " to character backend '%s'", devname);
2439 return -1;
2441 index++;
2442 return 0;
2445 static int parallel_parse(const char *devname)
2447 static int index = 0;
2448 char label[32];
2450 if (strcmp(devname, "none") == 0)
2451 return 0;
2452 if (index == MAX_PARALLEL_PORTS) {
2453 error_report("too many parallel ports");
2454 exit(1);
2456 snprintf(label, sizeof(label), "parallel%d", index);
2457 parallel_hds[index] = qemu_chr_new(label, devname);
2458 if (!parallel_hds[index]) {
2459 error_report("could not connect parallel device"
2460 " to character backend '%s'", devname);
2461 return -1;
2463 index++;
2464 return 0;
2467 static int virtcon_parse(const char *devname)
2469 QemuOptsList *device = qemu_find_opts("device");
2470 static int index = 0;
2471 char label[32];
2472 QemuOpts *bus_opts, *dev_opts;
2474 if (strcmp(devname, "none") == 0)
2475 return 0;
2476 if (index == MAX_VIRTIO_CONSOLES) {
2477 error_report("too many virtio consoles");
2478 exit(1);
2481 bus_opts = qemu_opts_create(device, NULL, 0, &error_abort);
2482 qemu_opt_set(bus_opts, "driver", "virtio-serial", &error_abort);
2484 dev_opts = qemu_opts_create(device, NULL, 0, &error_abort);
2485 qemu_opt_set(dev_opts, "driver", "virtconsole", &error_abort);
2487 snprintf(label, sizeof(label), "virtcon%d", index);
2488 virtcon_hds[index] = qemu_chr_new(label, devname);
2489 if (!virtcon_hds[index]) {
2490 error_report("could not connect virtio console"
2491 " to character backend '%s'", devname);
2492 return -1;
2494 qemu_opt_set(dev_opts, "chardev", label, &error_abort);
2496 index++;
2497 return 0;
2500 static int sclp_parse(const char *devname)
2502 QemuOptsList *device = qemu_find_opts("device");
2503 static int index = 0;
2504 char label[32];
2505 QemuOpts *dev_opts;
2507 if (strcmp(devname, "none") == 0) {
2508 return 0;
2510 if (index == MAX_SCLP_CONSOLES) {
2511 error_report("too many sclp consoles");
2512 exit(1);
2515 assert(arch_type == QEMU_ARCH_S390X);
2517 dev_opts = qemu_opts_create(device, NULL, 0, NULL);
2518 qemu_opt_set(dev_opts, "driver", "sclpconsole", &error_abort);
2520 snprintf(label, sizeof(label), "sclpcon%d", index);
2521 sclp_hds[index] = qemu_chr_new(label, devname);
2522 if (!sclp_hds[index]) {
2523 error_report("could not connect sclp console"
2524 " to character backend '%s'", devname);
2525 return -1;
2527 qemu_opt_set(dev_opts, "chardev", label, &error_abort);
2529 index++;
2530 return 0;
2533 static int debugcon_parse(const char *devname)
2535 QemuOpts *opts;
2537 if (!qemu_chr_new("debugcon", devname)) {
2538 exit(1);
2540 opts = qemu_opts_create(qemu_find_opts("device"), "debugcon", 1, NULL);
2541 if (!opts) {
2542 error_report("already have a debugcon device");
2543 exit(1);
2545 qemu_opt_set(opts, "driver", "isa-debugcon", &error_abort);
2546 qemu_opt_set(opts, "chardev", "debugcon", &error_abort);
2547 return 0;
2550 static gint machine_class_cmp(gconstpointer a, gconstpointer b)
2552 const MachineClass *mc1 = a, *mc2 = b;
2553 int res;
2555 if (mc1->family == NULL) {
2556 if (mc2->family == NULL) {
2557 /* Compare standalone machine types against each other; they sort
2558 * in increasing order.
2560 return strcmp(object_class_get_name(OBJECT_CLASS(mc1)),
2561 object_class_get_name(OBJECT_CLASS(mc2)));
2564 /* Standalone machine types sort after families. */
2565 return 1;
2568 if (mc2->family == NULL) {
2569 /* Families sort before standalone machine types. */
2570 return -1;
2573 /* Families sort between each other alphabetically increasingly. */
2574 res = strcmp(mc1->family, mc2->family);
2575 if (res != 0) {
2576 return res;
2579 /* Within the same family, machine types sort in decreasing order. */
2580 return strcmp(object_class_get_name(OBJECT_CLASS(mc2)),
2581 object_class_get_name(OBJECT_CLASS(mc1)));
2584 static MachineClass *machine_parse(const char *name)
2586 MachineClass *mc = NULL;
2587 GSList *el, *machines = object_class_get_list(TYPE_MACHINE, false);
2589 if (name) {
2590 mc = find_machine(name);
2592 if (mc) {
2593 g_slist_free(machines);
2594 return mc;
2596 if (name && !is_help_option(name)) {
2597 error_report("unsupported machine type");
2598 error_printf("Use -machine help to list supported machines\n");
2599 } else {
2600 printf("Supported machines are:\n");
2601 machines = g_slist_sort(machines, machine_class_cmp);
2602 for (el = machines; el; el = el->next) {
2603 MachineClass *mc = el->data;
2604 if (mc->alias) {
2605 printf("%-20s %s (alias of %s)\n", mc->alias, mc->desc, mc->name);
2607 printf("%-20s %s%s\n", mc->name, mc->desc,
2608 mc->is_default ? " (default)" : "");
2612 g_slist_free(machines);
2613 exit(!name || !is_help_option(name));
2616 void qemu_add_exit_notifier(Notifier *notify)
2618 notifier_list_add(&exit_notifiers, notify);
2621 void qemu_remove_exit_notifier(Notifier *notify)
2623 notifier_remove(notify);
2626 static void qemu_run_exit_notifiers(void)
2628 notifier_list_notify(&exit_notifiers, NULL);
2631 static bool machine_init_done;
2633 void qemu_add_machine_init_done_notifier(Notifier *notify)
2635 notifier_list_add(&machine_init_done_notifiers, notify);
2636 if (machine_init_done) {
2637 notify->notify(notify, NULL);
2641 void qemu_remove_machine_init_done_notifier(Notifier *notify)
2643 notifier_remove(notify);
2646 static void qemu_run_machine_init_done_notifiers(void)
2648 notifier_list_notify(&machine_init_done_notifiers, NULL);
2649 machine_init_done = true;
2652 static const QEMUOption *lookup_opt(int argc, char **argv,
2653 const char **poptarg, int *poptind)
2655 const QEMUOption *popt;
2656 int optind = *poptind;
2657 char *r = argv[optind];
2658 const char *optarg;
2660 loc_set_cmdline(argv, optind, 1);
2661 optind++;
2662 /* Treat --foo the same as -foo. */
2663 if (r[1] == '-')
2664 r++;
2665 popt = qemu_options;
2666 for(;;) {
2667 if (!popt->name) {
2668 error_report("invalid option");
2669 exit(1);
2671 if (!strcmp(popt->name, r + 1))
2672 break;
2673 popt++;
2675 if (popt->flags & HAS_ARG) {
2676 if (optind >= argc) {
2677 error_report("requires an argument");
2678 exit(1);
2680 optarg = argv[optind++];
2681 loc_set_cmdline(argv, optind - 2, 2);
2682 } else {
2683 optarg = NULL;
2686 *poptarg = optarg;
2687 *poptind = optind;
2689 return popt;
2692 static MachineClass *select_machine(void)
2694 MachineClass *machine_class = find_default_machine();
2695 const char *optarg;
2696 QemuOpts *opts;
2697 Location loc;
2699 loc_push_none(&loc);
2701 opts = qemu_get_machine_opts();
2702 qemu_opts_loc_restore(opts);
2704 optarg = qemu_opt_get(opts, "type");
2705 if (optarg) {
2706 machine_class = machine_parse(optarg);
2709 if (!machine_class) {
2710 error_report("No machine specified, and there is no default");
2711 error_printf("Use -machine help to list supported machines\n");
2712 exit(1);
2715 loc_pop(&loc);
2716 return machine_class;
2719 static int machine_set_property(void *opaque,
2720 const char *name, const char *value,
2721 Error **errp)
2723 Object *obj = OBJECT(opaque);
2724 Error *local_err = NULL;
2725 char *p, *qom_name;
2727 if (strcmp(name, "type") == 0) {
2728 return 0;
2731 qom_name = g_strdup(name);
2732 for (p = qom_name; *p; p++) {
2733 if (*p == '_') {
2734 *p = '-';
2738 object_property_parse(obj, value, qom_name, &local_err);
2739 g_free(qom_name);
2741 if (local_err) {
2742 error_report_err(local_err);
2743 return -1;
2746 return 0;
2751 * Initial object creation happens before all other
2752 * QEMU data types are created. The majority of objects
2753 * can be created at this point. The rng-egd object
2754 * cannot be created here, as it depends on the chardev
2755 * already existing.
2757 static bool object_create_initial(const char *type)
2759 if (g_str_equal(type, "rng-egd")) {
2760 return false;
2764 * return false for concrete netfilters since
2765 * they depend on netdevs already existing
2767 if (g_str_equal(type, "filter-buffer") ||
2768 g_str_equal(type, "filter-dump") ||
2769 g_str_equal(type, "filter-mirror") ||
2770 g_str_equal(type, "filter-redirector") ||
2771 g_str_equal(type, "colo-compare") ||
2772 g_str_equal(type, "filter-rewriter") ||
2773 g_str_equal(type, "filter-replay")) {
2774 return false;
2777 /* Memory allocation by backends needs to be done
2778 * after configure_accelerator() (due to the tcg_enabled()
2779 * checks at memory_region_init_*()).
2781 * Also, allocation of large amounts of memory may delay
2782 * chardev initialization for too long, and trigger timeouts
2783 * on software that waits for a monitor socket to be created
2784 * (e.g. libvirt).
2786 if (g_str_has_prefix(type, "memory-backend-")) {
2787 return false;
2790 return true;
2795 * The remainder of object creation happens after the
2796 * creation of chardev, fsdev, net clients and device data types.
2798 static bool object_create_delayed(const char *type)
2800 return !object_create_initial(type);
2804 static void set_memory_options(uint64_t *ram_slots, ram_addr_t *maxram_size,
2805 MachineClass *mc)
2807 uint64_t sz;
2808 const char *mem_str;
2809 const char *maxmem_str, *slots_str;
2810 const ram_addr_t default_ram_size = mc->default_ram_size;
2811 QemuOpts *opts = qemu_find_opts_singleton("memory");
2812 Location loc;
2814 loc_push_none(&loc);
2815 qemu_opts_loc_restore(opts);
2817 sz = 0;
2818 mem_str = qemu_opt_get(opts, "size");
2819 if (mem_str) {
2820 if (!*mem_str) {
2821 error_report("missing 'size' option value");
2822 exit(EXIT_FAILURE);
2825 sz = qemu_opt_get_size(opts, "size", ram_size);
2827 /* Fix up legacy suffix-less format */
2828 if (g_ascii_isdigit(mem_str[strlen(mem_str) - 1])) {
2829 uint64_t overflow_check = sz;
2831 sz <<= 20;
2832 if ((sz >> 20) != overflow_check) {
2833 error_report("too large 'size' option value");
2834 exit(EXIT_FAILURE);
2839 /* backward compatibility behaviour for case "-m 0" */
2840 if (sz == 0) {
2841 sz = default_ram_size;
2844 sz = QEMU_ALIGN_UP(sz, 8192);
2845 ram_size = sz;
2846 if (ram_size != sz) {
2847 error_report("ram size too large");
2848 exit(EXIT_FAILURE);
2851 /* store value for the future use */
2852 qemu_opt_set_number(opts, "size", ram_size, &error_abort);
2853 *maxram_size = ram_size;
2855 maxmem_str = qemu_opt_get(opts, "maxmem");
2856 slots_str = qemu_opt_get(opts, "slots");
2857 if (maxmem_str && slots_str) {
2858 uint64_t slots;
2860 sz = qemu_opt_get_size(opts, "maxmem", 0);
2861 slots = qemu_opt_get_number(opts, "slots", 0);
2862 if (sz < ram_size) {
2863 error_report("invalid value of -m option maxmem: "
2864 "maximum memory size (0x%" PRIx64 ") must be at least "
2865 "the initial memory size (0x" RAM_ADDR_FMT ")",
2866 sz, ram_size);
2867 exit(EXIT_FAILURE);
2868 } else if (sz > ram_size) {
2869 if (!slots) {
2870 error_report("invalid value of -m option: maxmem was "
2871 "specified, but no hotplug slots were specified");
2872 exit(EXIT_FAILURE);
2874 } else if (slots) {
2875 error_report("invalid value of -m option maxmem: "
2876 "memory slots were specified but maximum memory size "
2877 "(0x%" PRIx64 ") is equal to the initial memory size "
2878 "(0x" RAM_ADDR_FMT ")", sz, ram_size);
2879 exit(EXIT_FAILURE);
2882 *maxram_size = sz;
2883 *ram_slots = slots;
2884 } else if ((!maxmem_str && slots_str) ||
2885 (maxmem_str && !slots_str)) {
2886 error_report("invalid -m option value: missing "
2887 "'%s' option", slots_str ? "maxmem" : "slots");
2888 exit(EXIT_FAILURE);
2891 loc_pop(&loc);
2894 static int global_init_func(void *opaque, QemuOpts *opts, Error **errp)
2896 GlobalProperty *g;
2898 g = g_malloc0(sizeof(*g));
2899 g->driver = qemu_opt_get(opts, "driver");
2900 g->property = qemu_opt_get(opts, "property");
2901 g->value = qemu_opt_get(opts, "value");
2902 g->user_provided = true;
2903 g->errp = &error_fatal;
2904 qdev_prop_register_global(g);
2905 return 0;
2908 static int qemu_read_default_config_file(void)
2910 int ret;
2912 ret = qemu_read_config_file(CONFIG_QEMU_CONFDIR "/qemu.conf");
2913 if (ret < 0 && ret != -ENOENT) {
2914 return ret;
2917 return 0;
2920 int main(int argc, char **argv, char **envp)
2922 int i;
2923 int snapshot, linux_boot;
2924 const char *initrd_filename;
2925 const char *kernel_filename, *kernel_cmdline;
2926 const char *boot_order = NULL;
2927 const char *boot_once = NULL;
2928 DisplayState *ds;
2929 int cyls, heads, secs, translation;
2930 QemuOpts *hda_opts = NULL, *opts, *machine_opts, *icount_opts = NULL;
2931 QemuOptsList *olist;
2932 int optind;
2933 const char *optarg;
2934 const char *loadvm = NULL;
2935 MachineClass *machine_class;
2936 const char *cpu_model;
2937 const char *vga_model = NULL;
2938 const char *qtest_chrdev = NULL;
2939 const char *qtest_log = NULL;
2940 const char *pid_file = NULL;
2941 const char *incoming = NULL;
2942 bool defconfig = true;
2943 bool userconfig = true;
2944 bool nographic = false;
2945 DisplayType display_type = DT_DEFAULT;
2946 int display_remote = 0;
2947 const char *log_mask = NULL;
2948 const char *log_file = NULL;
2949 char *trace_file = NULL;
2950 ram_addr_t maxram_size;
2951 uint64_t ram_slots = 0;
2952 FILE *vmstate_dump_file = NULL;
2953 Error *main_loop_err = NULL;
2954 Error *err = NULL;
2955 bool list_data_dirs = false;
2957 module_call_init(MODULE_INIT_TRACE);
2959 qemu_init_cpu_list();
2960 qemu_init_cpu_loop();
2961 qemu_mutex_lock_iothread();
2963 atexit(qemu_run_exit_notifiers);
2964 error_set_progname(argv[0]);
2965 qemu_init_exec_dir(argv[0]);
2967 module_call_init(MODULE_INIT_QOM);
2968 module_call_init(MODULE_INIT_QAPI);
2970 qemu_add_opts(&qemu_drive_opts);
2971 qemu_add_drive_opts(&qemu_legacy_drive_opts);
2972 qemu_add_drive_opts(&qemu_common_drive_opts);
2973 qemu_add_drive_opts(&qemu_drive_opts);
2974 qemu_add_drive_opts(&bdrv_runtime_opts);
2975 qemu_add_opts(&qemu_chardev_opts);
2976 qemu_add_opts(&qemu_device_opts);
2977 qemu_add_opts(&qemu_netdev_opts);
2978 qemu_add_opts(&qemu_net_opts);
2979 qemu_add_opts(&qemu_rtc_opts);
2980 qemu_add_opts(&qemu_global_opts);
2981 qemu_add_opts(&qemu_mon_opts);
2982 qemu_add_opts(&qemu_trace_opts);
2983 qemu_add_opts(&qemu_option_rom_opts);
2984 qemu_add_opts(&qemu_machine_opts);
2985 qemu_add_opts(&qemu_mem_opts);
2986 qemu_add_opts(&qemu_smp_opts);
2987 qemu_add_opts(&qemu_boot_opts);
2988 qemu_add_opts(&qemu_sandbox_opts);
2989 qemu_add_opts(&qemu_add_fd_opts);
2990 qemu_add_opts(&qemu_object_opts);
2991 qemu_add_opts(&qemu_tpmdev_opts);
2992 qemu_add_opts(&qemu_realtime_opts);
2993 qemu_add_opts(&qemu_msg_opts);
2994 qemu_add_opts(&qemu_name_opts);
2995 qemu_add_opts(&qemu_numa_opts);
2996 qemu_add_opts(&qemu_icount_opts);
2997 qemu_add_opts(&qemu_semihosting_config_opts);
2998 qemu_add_opts(&qemu_fw_cfg_opts);
2999 module_call_init(MODULE_INIT_OPTS);
3001 runstate_init();
3003 if (qcrypto_init(&err) < 0) {
3004 error_reportf_err(err, "cannot initialize crypto: ");
3005 exit(1);
3007 rtc_clock = QEMU_CLOCK_HOST;
3009 QLIST_INIT (&vm_change_state_head);
3010 os_setup_early_signal_handling();
3012 cpu_model = NULL;
3013 snapshot = 0;
3014 cyls = heads = secs = 0;
3015 translation = BIOS_ATA_TRANSLATION_AUTO;
3017 nb_nics = 0;
3019 bdrv_init_with_whitelist();
3021 autostart = 1;
3023 /* first pass of option parsing */
3024 optind = 1;
3025 while (optind < argc) {
3026 if (argv[optind][0] != '-') {
3027 /* disk image */
3028 optind++;
3029 } else {
3030 const QEMUOption *popt;
3032 popt = lookup_opt(argc, argv, &optarg, &optind);
3033 switch (popt->index) {
3034 case QEMU_OPTION_nodefconfig:
3035 defconfig = false;
3036 break;
3037 case QEMU_OPTION_nouserconfig:
3038 userconfig = false;
3039 break;
3044 if (defconfig && userconfig) {
3045 if (qemu_read_default_config_file() < 0) {
3046 exit(1);
3050 /* second pass of option parsing */
3051 optind = 1;
3052 for(;;) {
3053 if (optind >= argc)
3054 break;
3055 if (argv[optind][0] != '-') {
3056 hda_opts = drive_add(IF_DEFAULT, 0, argv[optind++], HD_OPTS);
3057 } else {
3058 const QEMUOption *popt;
3060 popt = lookup_opt(argc, argv, &optarg, &optind);
3061 if (!(popt->arch_mask & arch_type)) {
3062 error_report("Option not supported for this target");
3063 exit(1);
3065 switch(popt->index) {
3066 case QEMU_OPTION_no_kvm_irqchip: {
3067 olist = qemu_find_opts("machine");
3068 qemu_opts_parse_noisily(olist, "kernel_irqchip=off", false);
3069 break;
3071 case QEMU_OPTION_cpu:
3072 /* hw initialization will check this */
3073 cpu_model = optarg;
3074 break;
3075 case QEMU_OPTION_hda:
3077 char buf[256];
3078 if (cyls == 0)
3079 snprintf(buf, sizeof(buf), "%s", HD_OPTS);
3080 else
3081 snprintf(buf, sizeof(buf),
3082 "%s,cyls=%d,heads=%d,secs=%d%s",
3083 HD_OPTS , cyls, heads, secs,
3084 translation == BIOS_ATA_TRANSLATION_LBA ?
3085 ",trans=lba" :
3086 translation == BIOS_ATA_TRANSLATION_NONE ?
3087 ",trans=none" : "");
3088 drive_add(IF_DEFAULT, 0, optarg, buf);
3089 break;
3091 case QEMU_OPTION_hdb:
3092 case QEMU_OPTION_hdc:
3093 case QEMU_OPTION_hdd:
3094 drive_add(IF_DEFAULT, popt->index - QEMU_OPTION_hda, optarg,
3095 HD_OPTS);
3096 break;
3097 case QEMU_OPTION_drive:
3098 if (drive_def(optarg) == NULL) {
3099 exit(1);
3101 break;
3102 case QEMU_OPTION_set:
3103 if (qemu_set_option(optarg) != 0)
3104 exit(1);
3105 break;
3106 case QEMU_OPTION_global:
3107 if (qemu_global_option(optarg) != 0)
3108 exit(1);
3109 break;
3110 case QEMU_OPTION_mtdblock:
3111 drive_add(IF_MTD, -1, optarg, MTD_OPTS);
3112 break;
3113 case QEMU_OPTION_sd:
3114 drive_add(IF_SD, -1, optarg, SD_OPTS);
3115 break;
3116 case QEMU_OPTION_pflash:
3117 drive_add(IF_PFLASH, -1, optarg, PFLASH_OPTS);
3118 break;
3119 case QEMU_OPTION_snapshot:
3120 snapshot = 1;
3121 break;
3122 case QEMU_OPTION_hdachs:
3124 const char *p;
3125 p = optarg;
3126 cyls = strtol(p, (char **)&p, 0);
3127 if (cyls < 1 || cyls > 16383)
3128 goto chs_fail;
3129 if (*p != ',')
3130 goto chs_fail;
3131 p++;
3132 heads = strtol(p, (char **)&p, 0);
3133 if (heads < 1 || heads > 16)
3134 goto chs_fail;
3135 if (*p != ',')
3136 goto chs_fail;
3137 p++;
3138 secs = strtol(p, (char **)&p, 0);
3139 if (secs < 1 || secs > 63)
3140 goto chs_fail;
3141 if (*p == ',') {
3142 p++;
3143 if (!strcmp(p, "large")) {
3144 translation = BIOS_ATA_TRANSLATION_LARGE;
3145 } else if (!strcmp(p, "rechs")) {
3146 translation = BIOS_ATA_TRANSLATION_RECHS;
3147 } else if (!strcmp(p, "none")) {
3148 translation = BIOS_ATA_TRANSLATION_NONE;
3149 } else if (!strcmp(p, "lba")) {
3150 translation = BIOS_ATA_TRANSLATION_LBA;
3151 } else if (!strcmp(p, "auto")) {
3152 translation = BIOS_ATA_TRANSLATION_AUTO;
3153 } else {
3154 goto chs_fail;
3156 } else if (*p != '\0') {
3157 chs_fail:
3158 error_report("invalid physical CHS format");
3159 exit(1);
3161 if (hda_opts != NULL) {
3162 qemu_opt_set_number(hda_opts, "cyls", cyls,
3163 &error_abort);
3164 qemu_opt_set_number(hda_opts, "heads", heads,
3165 &error_abort);
3166 qemu_opt_set_number(hda_opts, "secs", secs,
3167 &error_abort);
3168 if (translation == BIOS_ATA_TRANSLATION_LARGE) {
3169 qemu_opt_set(hda_opts, "trans", "large",
3170 &error_abort);
3171 } else if (translation == BIOS_ATA_TRANSLATION_RECHS) {
3172 qemu_opt_set(hda_opts, "trans", "rechs",
3173 &error_abort);
3174 } else if (translation == BIOS_ATA_TRANSLATION_LBA) {
3175 qemu_opt_set(hda_opts, "trans", "lba",
3176 &error_abort);
3177 } else if (translation == BIOS_ATA_TRANSLATION_NONE) {
3178 qemu_opt_set(hda_opts, "trans", "none",
3179 &error_abort);
3183 break;
3184 case QEMU_OPTION_numa:
3185 opts = qemu_opts_parse_noisily(qemu_find_opts("numa"),
3186 optarg, true);
3187 if (!opts) {
3188 exit(1);
3190 break;
3191 case QEMU_OPTION_display:
3192 display_type = select_display(optarg);
3193 break;
3194 case QEMU_OPTION_nographic:
3195 olist = qemu_find_opts("machine");
3196 qemu_opts_parse_noisily(olist, "graphics=off", false);
3197 nographic = true;
3198 display_type = DT_NONE;
3199 break;
3200 case QEMU_OPTION_curses:
3201 #ifdef CONFIG_CURSES
3202 display_type = DT_CURSES;
3203 #else
3204 error_report("curses support is disabled");
3205 exit(1);
3206 #endif
3207 break;
3208 case QEMU_OPTION_portrait:
3209 graphic_rotate = 90;
3210 break;
3211 case QEMU_OPTION_rotate:
3212 graphic_rotate = strtol(optarg, (char **) &optarg, 10);
3213 if (graphic_rotate != 0 && graphic_rotate != 90 &&
3214 graphic_rotate != 180 && graphic_rotate != 270) {
3215 error_report("only 90, 180, 270 deg rotation is available");
3216 exit(1);
3218 break;
3219 case QEMU_OPTION_kernel:
3220 qemu_opts_set(qemu_find_opts("machine"), 0, "kernel", optarg,
3221 &error_abort);
3222 break;
3223 case QEMU_OPTION_initrd:
3224 qemu_opts_set(qemu_find_opts("machine"), 0, "initrd", optarg,
3225 &error_abort);
3226 break;
3227 case QEMU_OPTION_append:
3228 qemu_opts_set(qemu_find_opts("machine"), 0, "append", optarg,
3229 &error_abort);
3230 break;
3231 case QEMU_OPTION_dtb:
3232 qemu_opts_set(qemu_find_opts("machine"), 0, "dtb", optarg,
3233 &error_abort);
3234 break;
3235 case QEMU_OPTION_cdrom:
3236 drive_add(IF_DEFAULT, 2, optarg, CDROM_OPTS);
3237 break;
3238 case QEMU_OPTION_boot:
3239 opts = qemu_opts_parse_noisily(qemu_find_opts("boot-opts"),
3240 optarg, true);
3241 if (!opts) {
3242 exit(1);
3244 break;
3245 case QEMU_OPTION_fda:
3246 case QEMU_OPTION_fdb:
3247 drive_add(IF_FLOPPY, popt->index - QEMU_OPTION_fda,
3248 optarg, FD_OPTS);
3249 break;
3250 case QEMU_OPTION_no_fd_bootchk:
3251 fd_bootchk = 0;
3252 break;
3253 case QEMU_OPTION_netdev:
3254 default_net = 0;
3255 if (net_client_parse(qemu_find_opts("netdev"), optarg) == -1) {
3256 exit(1);
3258 break;
3259 case QEMU_OPTION_net:
3260 default_net = 0;
3261 if (net_client_parse(qemu_find_opts("net"), optarg) == -1) {
3262 exit(1);
3264 break;
3265 #ifdef CONFIG_LIBISCSI
3266 case QEMU_OPTION_iscsi:
3267 opts = qemu_opts_parse_noisily(qemu_find_opts("iscsi"),
3268 optarg, false);
3269 if (!opts) {
3270 exit(1);
3272 break;
3273 #endif
3274 #ifdef CONFIG_SLIRP
3275 case QEMU_OPTION_tftp:
3276 error_report("The -tftp option is deprecated. "
3277 "Please use '-netdev user,tftp=...' instead.");
3278 legacy_tftp_prefix = optarg;
3279 break;
3280 case QEMU_OPTION_bootp:
3281 error_report("The -bootp option is deprecated. "
3282 "Please use '-netdev user,bootfile=...' instead.");
3283 legacy_bootp_filename = optarg;
3284 break;
3285 case QEMU_OPTION_redir:
3286 error_report("The -redir option is deprecated. "
3287 "Please use '-netdev user,hostfwd=...' instead.");
3288 if (net_slirp_redir(optarg) < 0)
3289 exit(1);
3290 break;
3291 #endif
3292 case QEMU_OPTION_bt:
3293 add_device_config(DEV_BT, optarg);
3294 break;
3295 case QEMU_OPTION_audio_help:
3296 AUD_help ();
3297 exit (0);
3298 break;
3299 case QEMU_OPTION_soundhw:
3300 select_soundhw (optarg);
3301 break;
3302 case QEMU_OPTION_h:
3303 help(0);
3304 break;
3305 case QEMU_OPTION_version:
3306 version();
3307 exit(0);
3308 break;
3309 case QEMU_OPTION_m:
3310 opts = qemu_opts_parse_noisily(qemu_find_opts("memory"),
3311 optarg, true);
3312 if (!opts) {
3313 exit(EXIT_FAILURE);
3315 break;
3316 #ifdef CONFIG_TPM
3317 case QEMU_OPTION_tpmdev:
3318 if (tpm_config_parse(qemu_find_opts("tpmdev"), optarg) < 0) {
3319 exit(1);
3321 break;
3322 #endif
3323 case QEMU_OPTION_mempath:
3324 mem_path = optarg;
3325 break;
3326 case QEMU_OPTION_mem_prealloc:
3327 mem_prealloc = 1;
3328 break;
3329 case QEMU_OPTION_d:
3330 log_mask = optarg;
3331 break;
3332 case QEMU_OPTION_D:
3333 log_file = optarg;
3334 break;
3335 case QEMU_OPTION_DFILTER:
3336 qemu_set_dfilter_ranges(optarg, &error_fatal);
3337 break;
3338 case QEMU_OPTION_s:
3339 add_device_config(DEV_GDB, "tcp::" DEFAULT_GDBSTUB_PORT);
3340 break;
3341 case QEMU_OPTION_gdb:
3342 add_device_config(DEV_GDB, optarg);
3343 break;
3344 case QEMU_OPTION_L:
3345 if (is_help_option(optarg)) {
3346 list_data_dirs = true;
3347 } else if (data_dir_idx < ARRAY_SIZE(data_dir)) {
3348 data_dir[data_dir_idx++] = optarg;
3350 break;
3351 case QEMU_OPTION_bios:
3352 qemu_opts_set(qemu_find_opts("machine"), 0, "firmware", optarg,
3353 &error_abort);
3354 break;
3355 case QEMU_OPTION_singlestep:
3356 singlestep = 1;
3357 break;
3358 case QEMU_OPTION_S:
3359 autostart = 0;
3360 break;
3361 case QEMU_OPTION_k:
3362 keyboard_layout = optarg;
3363 break;
3364 case QEMU_OPTION_localtime:
3365 rtc_utc = 0;
3366 break;
3367 case QEMU_OPTION_vga:
3368 vga_model = optarg;
3369 default_vga = 0;
3370 break;
3371 case QEMU_OPTION_g:
3373 const char *p;
3374 int w, h, depth;
3375 p = optarg;
3376 w = strtol(p, (char **)&p, 10);
3377 if (w <= 0) {
3378 graphic_error:
3379 error_report("invalid resolution or depth");
3380 exit(1);
3382 if (*p != 'x')
3383 goto graphic_error;
3384 p++;
3385 h = strtol(p, (char **)&p, 10);
3386 if (h <= 0)
3387 goto graphic_error;
3388 if (*p == 'x') {
3389 p++;
3390 depth = strtol(p, (char **)&p, 10);
3391 if (depth != 8 && depth != 15 && depth != 16 &&
3392 depth != 24 && depth != 32)
3393 goto graphic_error;
3394 } else if (*p == '\0') {
3395 depth = graphic_depth;
3396 } else {
3397 goto graphic_error;
3400 graphic_width = w;
3401 graphic_height = h;
3402 graphic_depth = depth;
3404 break;
3405 case QEMU_OPTION_echr:
3407 char *r;
3408 term_escape_char = strtol(optarg, &r, 0);
3409 if (r == optarg)
3410 printf("Bad argument to echr\n");
3411 break;
3413 case QEMU_OPTION_monitor:
3414 default_monitor = 0;
3415 if (strncmp(optarg, "none", 4)) {
3416 monitor_parse(optarg, "readline", false);
3418 break;
3419 case QEMU_OPTION_qmp:
3420 monitor_parse(optarg, "control", false);
3421 default_monitor = 0;
3422 break;
3423 case QEMU_OPTION_qmp_pretty:
3424 monitor_parse(optarg, "control", true);
3425 default_monitor = 0;
3426 break;
3427 case QEMU_OPTION_mon:
3428 opts = qemu_opts_parse_noisily(qemu_find_opts("mon"), optarg,
3429 true);
3430 if (!opts) {
3431 exit(1);
3433 default_monitor = 0;
3434 break;
3435 case QEMU_OPTION_chardev:
3436 opts = qemu_opts_parse_noisily(qemu_find_opts("chardev"),
3437 optarg, true);
3438 if (!opts) {
3439 exit(1);
3441 break;
3442 case QEMU_OPTION_fsdev:
3443 olist = qemu_find_opts("fsdev");
3444 if (!olist) {
3445 error_report("fsdev support is disabled");
3446 exit(1);
3448 opts = qemu_opts_parse_noisily(olist, optarg, true);
3449 if (!opts) {
3450 exit(1);
3452 break;
3453 case QEMU_OPTION_virtfs: {
3454 QemuOpts *fsdev;
3455 QemuOpts *device;
3456 const char *writeout, *sock_fd, *socket;
3458 olist = qemu_find_opts("virtfs");
3459 if (!olist) {
3460 error_report("virtfs support is disabled");
3461 exit(1);
3463 opts = qemu_opts_parse_noisily(olist, optarg, true);
3464 if (!opts) {
3465 exit(1);
3468 if (qemu_opt_get(opts, "fsdriver") == NULL ||
3469 qemu_opt_get(opts, "mount_tag") == NULL) {
3470 error_report("Usage: -virtfs fsdriver,mount_tag=tag");
3471 exit(1);
3473 fsdev = qemu_opts_create(qemu_find_opts("fsdev"),
3474 qemu_opt_get(opts, "mount_tag"),
3475 1, NULL);
3476 if (!fsdev) {
3477 error_report("duplicate fsdev id: %s",
3478 qemu_opt_get(opts, "mount_tag"));
3479 exit(1);
3482 writeout = qemu_opt_get(opts, "writeout");
3483 if (writeout) {
3484 #ifdef CONFIG_SYNC_FILE_RANGE
3485 qemu_opt_set(fsdev, "writeout", writeout, &error_abort);
3486 #else
3487 error_report("writeout=immediate not supported "
3488 "on this platform");
3489 exit(1);
3490 #endif
3492 qemu_opt_set(fsdev, "fsdriver",
3493 qemu_opt_get(opts, "fsdriver"), &error_abort);
3494 qemu_opt_set(fsdev, "path", qemu_opt_get(opts, "path"),
3495 &error_abort);
3496 qemu_opt_set(fsdev, "security_model",
3497 qemu_opt_get(opts, "security_model"),
3498 &error_abort);
3499 socket = qemu_opt_get(opts, "socket");
3500 if (socket) {
3501 qemu_opt_set(fsdev, "socket", socket, &error_abort);
3503 sock_fd = qemu_opt_get(opts, "sock_fd");
3504 if (sock_fd) {
3505 qemu_opt_set(fsdev, "sock_fd", sock_fd, &error_abort);
3508 qemu_opt_set_bool(fsdev, "readonly",
3509 qemu_opt_get_bool(opts, "readonly", 0),
3510 &error_abort);
3511 device = qemu_opts_create(qemu_find_opts("device"), NULL, 0,
3512 &error_abort);
3513 qemu_opt_set(device, "driver", "virtio-9p-pci", &error_abort);
3514 qemu_opt_set(device, "fsdev",
3515 qemu_opt_get(opts, "mount_tag"), &error_abort);
3516 qemu_opt_set(device, "mount_tag",
3517 qemu_opt_get(opts, "mount_tag"), &error_abort);
3518 break;
3520 case QEMU_OPTION_virtfs_synth: {
3521 QemuOpts *fsdev;
3522 QemuOpts *device;
3524 fsdev = qemu_opts_create(qemu_find_opts("fsdev"), "v_synth",
3525 1, NULL);
3526 if (!fsdev) {
3527 error_report("duplicate option: %s", "virtfs_synth");
3528 exit(1);
3530 qemu_opt_set(fsdev, "fsdriver", "synth", &error_abort);
3532 device = qemu_opts_create(qemu_find_opts("device"), NULL, 0,
3533 &error_abort);
3534 qemu_opt_set(device, "driver", "virtio-9p-pci", &error_abort);
3535 qemu_opt_set(device, "fsdev", "v_synth", &error_abort);
3536 qemu_opt_set(device, "mount_tag", "v_synth", &error_abort);
3537 break;
3539 case QEMU_OPTION_serial:
3540 add_device_config(DEV_SERIAL, optarg);
3541 default_serial = 0;
3542 if (strncmp(optarg, "mon:", 4) == 0) {
3543 default_monitor = 0;
3545 break;
3546 case QEMU_OPTION_watchdog:
3547 if (watchdog) {
3548 error_report("only one watchdog option may be given");
3549 return 1;
3551 watchdog = optarg;
3552 break;
3553 case QEMU_OPTION_watchdog_action:
3554 if (select_watchdog_action(optarg) == -1) {
3555 error_report("unknown -watchdog-action parameter");
3556 exit(1);
3558 break;
3559 case QEMU_OPTION_virtiocon:
3560 add_device_config(DEV_VIRTCON, optarg);
3561 default_virtcon = 0;
3562 if (strncmp(optarg, "mon:", 4) == 0) {
3563 default_monitor = 0;
3565 break;
3566 case QEMU_OPTION_parallel:
3567 add_device_config(DEV_PARALLEL, optarg);
3568 default_parallel = 0;
3569 if (strncmp(optarg, "mon:", 4) == 0) {
3570 default_monitor = 0;
3572 break;
3573 case QEMU_OPTION_debugcon:
3574 add_device_config(DEV_DEBUGCON, optarg);
3575 break;
3576 case QEMU_OPTION_loadvm:
3577 loadvm = optarg;
3578 break;
3579 case QEMU_OPTION_full_screen:
3580 full_screen = 1;
3581 break;
3582 case QEMU_OPTION_no_frame:
3583 no_frame = 1;
3584 break;
3585 case QEMU_OPTION_alt_grab:
3586 alt_grab = 1;
3587 break;
3588 case QEMU_OPTION_ctrl_grab:
3589 ctrl_grab = 1;
3590 break;
3591 case QEMU_OPTION_no_quit:
3592 no_quit = 1;
3593 break;
3594 case QEMU_OPTION_sdl:
3595 #ifdef CONFIG_SDL
3596 display_type = DT_SDL;
3597 break;
3598 #else
3599 error_report("SDL support is disabled");
3600 exit(1);
3601 #endif
3602 case QEMU_OPTION_pidfile:
3603 pid_file = optarg;
3604 break;
3605 case QEMU_OPTION_win2k_hack:
3606 win2k_install_hack = 1;
3607 break;
3608 case QEMU_OPTION_rtc_td_hack: {
3609 static GlobalProperty slew_lost_ticks = {
3610 .driver = "mc146818rtc",
3611 .property = "lost_tick_policy",
3612 .value = "slew",
3615 qdev_prop_register_global(&slew_lost_ticks);
3616 break;
3618 case QEMU_OPTION_acpitable:
3619 opts = qemu_opts_parse_noisily(qemu_find_opts("acpi"),
3620 optarg, true);
3621 if (!opts) {
3622 exit(1);
3624 acpi_table_add(opts, &error_fatal);
3625 break;
3626 case QEMU_OPTION_smbios:
3627 opts = qemu_opts_parse_noisily(qemu_find_opts("smbios"),
3628 optarg, false);
3629 if (!opts) {
3630 exit(1);
3632 smbios_entry_add(opts, &error_fatal);
3633 break;
3634 case QEMU_OPTION_fwcfg:
3635 opts = qemu_opts_parse_noisily(qemu_find_opts("fw_cfg"),
3636 optarg, true);
3637 if (opts == NULL) {
3638 exit(1);
3640 break;
3641 case QEMU_OPTION_enable_kvm:
3642 olist = qemu_find_opts("machine");
3643 qemu_opts_parse_noisily(olist, "accel=kvm", false);
3644 break;
3645 case QEMU_OPTION_enable_hax:
3646 olist = qemu_find_opts("machine");
3647 qemu_opts_parse_noisily(olist, "accel=hax", false);
3648 break;
3649 case QEMU_OPTION_M:
3650 case QEMU_OPTION_machine:
3651 olist = qemu_find_opts("machine");
3652 opts = qemu_opts_parse_noisily(olist, optarg, true);
3653 if (!opts) {
3654 exit(1);
3656 break;
3657 case QEMU_OPTION_no_kvm:
3658 olist = qemu_find_opts("machine");
3659 qemu_opts_parse_noisily(olist, "accel=tcg", false);
3660 break;
3661 case QEMU_OPTION_no_kvm_pit: {
3662 error_report("warning: ignoring deprecated option");
3663 break;
3665 case QEMU_OPTION_no_kvm_pit_reinjection: {
3666 static GlobalProperty kvm_pit_lost_tick_policy = {
3667 .driver = "kvm-pit",
3668 .property = "lost_tick_policy",
3669 .value = "discard",
3672 error_report("warning: deprecated, replaced by "
3673 "-global kvm-pit.lost_tick_policy=discard");
3674 qdev_prop_register_global(&kvm_pit_lost_tick_policy);
3675 break;
3677 case QEMU_OPTION_usb:
3678 olist = qemu_find_opts("machine");
3679 qemu_opts_parse_noisily(olist, "usb=on", false);
3680 break;
3681 case QEMU_OPTION_usbdevice:
3682 olist = qemu_find_opts("machine");
3683 qemu_opts_parse_noisily(olist, "usb=on", false);
3684 add_device_config(DEV_USB, optarg);
3685 break;
3686 case QEMU_OPTION_device:
3687 if (!qemu_opts_parse_noisily(qemu_find_opts("device"),
3688 optarg, true)) {
3689 exit(1);
3691 break;
3692 case QEMU_OPTION_smp:
3693 if (!qemu_opts_parse_noisily(qemu_find_opts("smp-opts"),
3694 optarg, true)) {
3695 exit(1);
3697 break;
3698 case QEMU_OPTION_vnc:
3699 vnc_parse(optarg, &error_fatal);
3700 break;
3701 case QEMU_OPTION_no_acpi:
3702 acpi_enabled = 0;
3703 break;
3704 case QEMU_OPTION_no_hpet:
3705 no_hpet = 1;
3706 break;
3707 case QEMU_OPTION_balloon:
3708 if (balloon_parse(optarg) < 0) {
3709 error_report("unknown -balloon argument %s", optarg);
3710 exit(1);
3712 break;
3713 case QEMU_OPTION_no_reboot:
3714 no_reboot = 1;
3715 break;
3716 case QEMU_OPTION_no_shutdown:
3717 no_shutdown = 1;
3718 break;
3719 case QEMU_OPTION_show_cursor:
3720 cursor_hide = 0;
3721 break;
3722 case QEMU_OPTION_uuid:
3723 if (qemu_uuid_parse(optarg, &qemu_uuid) < 0) {
3724 error_report("failed to parse UUID string: wrong format");
3725 exit(1);
3727 qemu_uuid_set = true;
3728 break;
3729 case QEMU_OPTION_option_rom:
3730 if (nb_option_roms >= MAX_OPTION_ROMS) {
3731 error_report("too many option ROMs");
3732 exit(1);
3734 opts = qemu_opts_parse_noisily(qemu_find_opts("option-rom"),
3735 optarg, true);
3736 if (!opts) {
3737 exit(1);
3739 option_rom[nb_option_roms].name = qemu_opt_get(opts, "romfile");
3740 option_rom[nb_option_roms].bootindex =
3741 qemu_opt_get_number(opts, "bootindex", -1);
3742 if (!option_rom[nb_option_roms].name) {
3743 error_report("Option ROM file is not specified");
3744 exit(1);
3746 nb_option_roms++;
3747 break;
3748 case QEMU_OPTION_semihosting:
3749 semihosting.enabled = true;
3750 semihosting.target = SEMIHOSTING_TARGET_AUTO;
3751 break;
3752 case QEMU_OPTION_semihosting_config:
3753 semihosting.enabled = true;
3754 opts = qemu_opts_parse_noisily(qemu_find_opts("semihosting-config"),
3755 optarg, false);
3756 if (opts != NULL) {
3757 semihosting.enabled = qemu_opt_get_bool(opts, "enable",
3758 true);
3759 const char *target = qemu_opt_get(opts, "target");
3760 if (target != NULL) {
3761 if (strcmp("native", target) == 0) {
3762 semihosting.target = SEMIHOSTING_TARGET_NATIVE;
3763 } else if (strcmp("gdb", target) == 0) {
3764 semihosting.target = SEMIHOSTING_TARGET_GDB;
3765 } else if (strcmp("auto", target) == 0) {
3766 semihosting.target = SEMIHOSTING_TARGET_AUTO;
3767 } else {
3768 error_report("unsupported semihosting-config %s",
3769 optarg);
3770 exit(1);
3772 } else {
3773 semihosting.target = SEMIHOSTING_TARGET_AUTO;
3775 /* Set semihosting argument count and vector */
3776 qemu_opt_foreach(opts, add_semihosting_arg,
3777 &semihosting, NULL);
3778 } else {
3779 error_report("unsupported semihosting-config %s", optarg);
3780 exit(1);
3782 break;
3783 case QEMU_OPTION_tdf:
3784 error_report("warning: ignoring deprecated option");
3785 break;
3786 case QEMU_OPTION_name:
3787 opts = qemu_opts_parse_noisily(qemu_find_opts("name"),
3788 optarg, true);
3789 if (!opts) {
3790 exit(1);
3792 break;
3793 case QEMU_OPTION_prom_env:
3794 if (nb_prom_envs >= MAX_PROM_ENVS) {
3795 error_report("too many prom variables");
3796 exit(1);
3798 prom_envs[nb_prom_envs] = optarg;
3799 nb_prom_envs++;
3800 break;
3801 case QEMU_OPTION_old_param:
3802 old_param = 1;
3803 break;
3804 case QEMU_OPTION_clock:
3805 /* Clock options no longer exist. Keep this option for
3806 * backward compatibility.
3808 break;
3809 case QEMU_OPTION_startdate:
3810 configure_rtc_date_offset(optarg, 1);
3811 break;
3812 case QEMU_OPTION_rtc:
3813 opts = qemu_opts_parse_noisily(qemu_find_opts("rtc"), optarg,
3814 false);
3815 if (!opts) {
3816 exit(1);
3818 configure_rtc(opts);
3819 break;
3820 case QEMU_OPTION_tb_size:
3821 tcg_tb_size = strtol(optarg, NULL, 0);
3822 if (tcg_tb_size < 0) {
3823 tcg_tb_size = 0;
3825 break;
3826 case QEMU_OPTION_icount:
3827 icount_opts = qemu_opts_parse_noisily(qemu_find_opts("icount"),
3828 optarg, true);
3829 if (!icount_opts) {
3830 exit(1);
3832 break;
3833 case QEMU_OPTION_incoming:
3834 if (!incoming) {
3835 runstate_set(RUN_STATE_INMIGRATE);
3837 incoming = optarg;
3838 break;
3839 case QEMU_OPTION_only_migratable:
3840 only_migratable = 1;
3841 break;
3842 case QEMU_OPTION_nodefaults:
3843 has_defaults = 0;
3844 break;
3845 case QEMU_OPTION_xen_domid:
3846 if (!(xen_available())) {
3847 error_report("Option not supported for this target");
3848 exit(1);
3850 xen_domid = atoi(optarg);
3851 break;
3852 case QEMU_OPTION_xen_create:
3853 if (!(xen_available())) {
3854 error_report("Option not supported for this target");
3855 exit(1);
3857 xen_mode = XEN_CREATE;
3858 break;
3859 case QEMU_OPTION_xen_attach:
3860 if (!(xen_available())) {
3861 error_report("Option not supported for this target");
3862 exit(1);
3864 xen_mode = XEN_ATTACH;
3865 break;
3866 case QEMU_OPTION_trace:
3867 g_free(trace_file);
3868 trace_file = trace_opt_parse(optarg);
3869 break;
3870 case QEMU_OPTION_readconfig:
3872 int ret = qemu_read_config_file(optarg);
3873 if (ret < 0) {
3874 error_report("read config %s: %s", optarg,
3875 strerror(-ret));
3876 exit(1);
3878 break;
3880 case QEMU_OPTION_spice:
3881 olist = qemu_find_opts("spice");
3882 if (!olist) {
3883 error_report("spice support is disabled");
3884 exit(1);
3886 opts = qemu_opts_parse_noisily(olist, optarg, false);
3887 if (!opts) {
3888 exit(1);
3890 display_remote++;
3891 break;
3892 case QEMU_OPTION_writeconfig:
3894 FILE *fp;
3895 if (strcmp(optarg, "-") == 0) {
3896 fp = stdout;
3897 } else {
3898 fp = fopen(optarg, "w");
3899 if (fp == NULL) {
3900 error_report("open %s: %s", optarg,
3901 strerror(errno));
3902 exit(1);
3905 qemu_config_write(fp);
3906 if (fp != stdout) {
3907 fclose(fp);
3909 break;
3911 case QEMU_OPTION_qtest:
3912 qtest_chrdev = optarg;
3913 break;
3914 case QEMU_OPTION_qtest_log:
3915 qtest_log = optarg;
3916 break;
3917 case QEMU_OPTION_sandbox:
3918 opts = qemu_opts_parse_noisily(qemu_find_opts("sandbox"),
3919 optarg, true);
3920 if (!opts) {
3921 exit(1);
3923 break;
3924 case QEMU_OPTION_add_fd:
3925 #ifndef _WIN32
3926 opts = qemu_opts_parse_noisily(qemu_find_opts("add-fd"),
3927 optarg, false);
3928 if (!opts) {
3929 exit(1);
3931 #else
3932 error_report("File descriptor passing is disabled on this "
3933 "platform");
3934 exit(1);
3935 #endif
3936 break;
3937 case QEMU_OPTION_object:
3938 opts = qemu_opts_parse_noisily(qemu_find_opts("object"),
3939 optarg, true);
3940 if (!opts) {
3941 exit(1);
3943 break;
3944 case QEMU_OPTION_realtime:
3945 opts = qemu_opts_parse_noisily(qemu_find_opts("realtime"),
3946 optarg, false);
3947 if (!opts) {
3948 exit(1);
3950 enable_mlock = qemu_opt_get_bool(opts, "mlock", true);
3951 break;
3952 case QEMU_OPTION_msg:
3953 opts = qemu_opts_parse_noisily(qemu_find_opts("msg"), optarg,
3954 false);
3955 if (!opts) {
3956 exit(1);
3958 configure_msg(opts);
3959 break;
3960 case QEMU_OPTION_dump_vmstate:
3961 if (vmstate_dump_file) {
3962 error_report("only one '-dump-vmstate' "
3963 "option may be given");
3964 exit(1);
3966 vmstate_dump_file = fopen(optarg, "w");
3967 if (vmstate_dump_file == NULL) {
3968 error_report("open %s: %s", optarg, strerror(errno));
3969 exit(1);
3971 break;
3972 default:
3973 os_parse_cmd_args(popt->index, optarg);
3978 * Clear error location left behind by the loop.
3979 * Best done right after the loop. Do not insert code here!
3981 loc_set_none();
3983 replay_configure(icount_opts);
3985 machine_class = select_machine();
3987 set_memory_options(&ram_slots, &maxram_size, machine_class);
3989 os_daemonize();
3991 if (pid_file && qemu_create_pidfile(pid_file) != 0) {
3992 error_report("could not acquire pid file: %s", strerror(errno));
3993 exit(1);
3996 if (qemu_init_main_loop(&main_loop_err)) {
3997 error_report_err(main_loop_err);
3998 exit(1);
4001 if (qemu_opts_foreach(qemu_find_opts("sandbox"),
4002 parse_sandbox, NULL, NULL)) {
4003 exit(1);
4006 if (qemu_opts_foreach(qemu_find_opts("name"),
4007 parse_name, NULL, NULL)) {
4008 exit(1);
4011 #ifndef _WIN32
4012 if (qemu_opts_foreach(qemu_find_opts("add-fd"),
4013 parse_add_fd, NULL, NULL)) {
4014 exit(1);
4017 if (qemu_opts_foreach(qemu_find_opts("add-fd"),
4018 cleanup_add_fd, NULL, NULL)) {
4019 exit(1);
4021 #endif
4023 current_machine = MACHINE(object_new(object_class_get_name(
4024 OBJECT_CLASS(machine_class))));
4025 if (machine_help_func(qemu_get_machine_opts(), current_machine)) {
4026 exit(0);
4028 object_property_add_child(object_get_root(), "machine",
4029 OBJECT(current_machine), &error_abort);
4031 if (machine_class->minimum_page_bits) {
4032 if (!set_preferred_target_page_bits(machine_class->minimum_page_bits)) {
4033 /* This would be a board error: specifying a minimum smaller than
4034 * a target's compile-time fixed setting.
4036 g_assert_not_reached();
4040 cpu_exec_init_all();
4042 if (machine_class->hw_version) {
4043 qemu_set_hw_version(machine_class->hw_version);
4046 if (cpu_model && is_help_option(cpu_model)) {
4047 list_cpus(stdout, &fprintf, cpu_model);
4048 exit(0);
4051 if (!trace_init_backends()) {
4052 exit(1);
4054 trace_init_file(trace_file);
4056 /* Open the logfile at this point and set the log mask if necessary.
4058 if (log_file) {
4059 qemu_set_log_filename(log_file, &error_fatal);
4062 if (log_mask) {
4063 int mask;
4064 mask = qemu_str_to_log_mask(log_mask);
4065 if (!mask) {
4066 qemu_print_log_usage(stdout);
4067 exit(1);
4069 qemu_set_log(mask);
4070 } else {
4071 qemu_set_log(0);
4074 /* If no data_dir is specified then try to find it relative to the
4075 executable path. */
4076 if (data_dir_idx < ARRAY_SIZE(data_dir)) {
4077 data_dir[data_dir_idx] = os_find_datadir();
4078 if (data_dir[data_dir_idx] != NULL) {
4079 data_dir_idx++;
4082 /* If all else fails use the install path specified when building. */
4083 if (data_dir_idx < ARRAY_SIZE(data_dir)) {
4084 data_dir[data_dir_idx++] = CONFIG_QEMU_DATADIR;
4087 /* -L help lists the data directories and exits. */
4088 if (list_data_dirs) {
4089 for (i = 0; i < data_dir_idx; i++) {
4090 printf("%s\n", data_dir[i]);
4092 exit(0);
4095 smp_parse(qemu_opts_find(qemu_find_opts("smp-opts"), NULL));
4097 machine_class->max_cpus = machine_class->max_cpus ?: 1; /* Default to UP */
4098 if (max_cpus > machine_class->max_cpus) {
4099 error_report("Number of SMP CPUs requested (%d) exceeds max CPUs "
4100 "supported by machine '%s' (%d)", max_cpus,
4101 machine_class->name, machine_class->max_cpus);
4102 exit(1);
4106 * Get the default machine options from the machine if it is not already
4107 * specified either by the configuration file or by the command line.
4109 if (machine_class->default_machine_opts) {
4110 qemu_opts_set_defaults(qemu_find_opts("machine"),
4111 machine_class->default_machine_opts, 0);
4114 qemu_opts_foreach(qemu_find_opts("device"),
4115 default_driver_check, NULL, NULL);
4116 qemu_opts_foreach(qemu_find_opts("global"),
4117 default_driver_check, NULL, NULL);
4119 if (!vga_model && !default_vga) {
4120 vga_interface_type = VGA_DEVICE;
4122 if (!has_defaults || machine_class->no_serial) {
4123 default_serial = 0;
4125 if (!has_defaults || machine_class->no_parallel) {
4126 default_parallel = 0;
4128 if (!has_defaults || !machine_class->use_virtcon) {
4129 default_virtcon = 0;
4131 if (!has_defaults || !machine_class->use_sclp) {
4132 default_sclp = 0;
4134 if (!has_defaults || machine_class->no_floppy) {
4135 default_floppy = 0;
4137 if (!has_defaults || machine_class->no_cdrom) {
4138 default_cdrom = 0;
4140 if (!has_defaults || machine_class->no_sdcard) {
4141 default_sdcard = 0;
4143 if (!has_defaults) {
4144 default_monitor = 0;
4145 default_net = 0;
4146 default_vga = 0;
4149 if (is_daemonized()) {
4150 /* According to documentation and historically, -nographic redirects
4151 * serial port, parallel port and monitor to stdio, which does not work
4152 * with -daemonize. We can redirect these to null instead, but since
4153 * -nographic is legacy, let's just error out.
4154 * We disallow -nographic only if all other ports are not redirected
4155 * explicitly, to not break existing legacy setups which uses
4156 * -nographic _and_ redirects all ports explicitly - this is valid
4157 * usage, -nographic is just a no-op in this case.
4159 if (nographic
4160 && (default_parallel || default_serial
4161 || default_monitor || default_virtcon)) {
4162 error_report("-nographic cannot be used with -daemonize");
4163 exit(1);
4165 #ifdef CONFIG_CURSES
4166 if (display_type == DT_CURSES) {
4167 error_report("curses display cannot be used with -daemonize");
4168 exit(1);
4170 #endif
4173 if (nographic) {
4174 if (default_parallel)
4175 add_device_config(DEV_PARALLEL, "null");
4176 if (default_serial && default_monitor) {
4177 add_device_config(DEV_SERIAL, "mon:stdio");
4178 } else if (default_virtcon && default_monitor) {
4179 add_device_config(DEV_VIRTCON, "mon:stdio");
4180 } else if (default_sclp && default_monitor) {
4181 add_device_config(DEV_SCLP, "mon:stdio");
4182 } else {
4183 if (default_serial)
4184 add_device_config(DEV_SERIAL, "stdio");
4185 if (default_virtcon)
4186 add_device_config(DEV_VIRTCON, "stdio");
4187 if (default_sclp) {
4188 add_device_config(DEV_SCLP, "stdio");
4190 if (default_monitor)
4191 monitor_parse("stdio", "readline", false);
4193 } else {
4194 if (default_serial)
4195 add_device_config(DEV_SERIAL, "vc:80Cx24C");
4196 if (default_parallel)
4197 add_device_config(DEV_PARALLEL, "vc:80Cx24C");
4198 if (default_monitor)
4199 monitor_parse("vc:80Cx24C", "readline", false);
4200 if (default_virtcon)
4201 add_device_config(DEV_VIRTCON, "vc:80Cx24C");
4202 if (default_sclp) {
4203 add_device_config(DEV_SCLP, "vc:80Cx24C");
4207 #if defined(CONFIG_VNC)
4208 if (!QTAILQ_EMPTY(&(qemu_find_opts("vnc")->head))) {
4209 display_remote++;
4211 #endif
4212 if (display_type == DT_DEFAULT && !display_remote) {
4213 #if defined(CONFIG_GTK)
4214 display_type = DT_GTK;
4215 #elif defined(CONFIG_SDL)
4216 display_type = DT_SDL;
4217 #elif defined(CONFIG_COCOA)
4218 display_type = DT_COCOA;
4219 #elif defined(CONFIG_VNC)
4220 vnc_parse("localhost:0,to=99,id=default", &error_abort);
4221 #else
4222 display_type = DT_NONE;
4223 #endif
4226 if ((no_frame || alt_grab || ctrl_grab) && display_type != DT_SDL) {
4227 error_report("-no-frame, -alt-grab and -ctrl-grab are only valid "
4228 "for SDL, ignoring option");
4230 if (no_quit && (display_type != DT_GTK && display_type != DT_SDL)) {
4231 error_report("-no-quit is only valid for GTK and SDL, "
4232 "ignoring option");
4235 if (display_type == DT_GTK) {
4236 early_gtk_display_init(request_opengl);
4239 if (display_type == DT_SDL) {
4240 sdl_display_early_init(request_opengl);
4243 qemu_console_early_init();
4245 if (request_opengl == 1 && display_opengl == 0) {
4246 #if defined(CONFIG_OPENGL)
4247 error_report("OpenGL is not supported by the display");
4248 #else
4249 error_report("OpenGL support is disabled");
4250 #endif
4251 exit(1);
4254 page_size_init();
4255 socket_init();
4257 if (qemu_opts_foreach(qemu_find_opts("object"),
4258 user_creatable_add_opts_foreach,
4259 object_create_initial, NULL)) {
4260 exit(1);
4263 if (qemu_opts_foreach(qemu_find_opts("chardev"),
4264 chardev_init_func, NULL, NULL)) {
4265 exit(1);
4268 #ifdef CONFIG_VIRTFS
4269 if (qemu_opts_foreach(qemu_find_opts("fsdev"),
4270 fsdev_init_func, NULL, NULL)) {
4271 exit(1);
4273 #endif
4275 if (qemu_opts_foreach(qemu_find_opts("device"),
4276 device_help_func, NULL, NULL)) {
4277 exit(0);
4280 machine_opts = qemu_get_machine_opts();
4281 if (qemu_opt_foreach(machine_opts, machine_set_property, current_machine,
4282 NULL)) {
4283 object_unref(OBJECT(current_machine));
4284 exit(1);
4287 configure_accelerator(current_machine);
4289 if (qtest_chrdev) {
4290 qtest_init(qtest_chrdev, qtest_log, &error_fatal);
4293 machine_opts = qemu_get_machine_opts();
4294 kernel_filename = qemu_opt_get(machine_opts, "kernel");
4295 initrd_filename = qemu_opt_get(machine_opts, "initrd");
4296 kernel_cmdline = qemu_opt_get(machine_opts, "append");
4297 bios_name = qemu_opt_get(machine_opts, "firmware");
4299 opts = qemu_opts_find(qemu_find_opts("boot-opts"), NULL);
4300 if (opts) {
4301 boot_order = qemu_opt_get(opts, "order");
4302 if (boot_order) {
4303 validate_bootdevices(boot_order, &error_fatal);
4306 boot_once = qemu_opt_get(opts, "once");
4307 if (boot_once) {
4308 validate_bootdevices(boot_once, &error_fatal);
4311 boot_menu = qemu_opt_get_bool(opts, "menu", boot_menu);
4312 boot_strict = qemu_opt_get_bool(opts, "strict", false);
4315 if (!boot_order) {
4316 boot_order = machine_class->default_boot_order;
4319 if (!kernel_cmdline) {
4320 kernel_cmdline = "";
4321 current_machine->kernel_cmdline = (char *)kernel_cmdline;
4324 linux_boot = (kernel_filename != NULL);
4326 if (!linux_boot && *kernel_cmdline != '\0') {
4327 error_report("-append only allowed with -kernel option");
4328 exit(1);
4331 if (!linux_boot && initrd_filename != NULL) {
4332 error_report("-initrd only allowed with -kernel option");
4333 exit(1);
4336 if (semihosting_enabled() && !semihosting_get_argc() && kernel_filename) {
4337 /* fall back to the -kernel/-append */
4338 semihosting_arg_fallback(kernel_filename, kernel_cmdline);
4341 os_set_line_buffering();
4343 /* spice needs the timers to be initialized by this point */
4344 qemu_spice_init();
4346 cpu_ticks_init();
4347 if (icount_opts) {
4348 if (!tcg_enabled()) {
4349 error_report("-icount is not allowed with hardware virtualization");
4350 exit(1);
4352 configure_icount(icount_opts, &error_abort);
4353 qemu_opts_del(icount_opts);
4356 if (default_net) {
4357 QemuOptsList *net = qemu_find_opts("net");
4358 qemu_opts_set(net, NULL, "type", "nic", &error_abort);
4359 #ifdef CONFIG_SLIRP
4360 qemu_opts_set(net, NULL, "type", "user", &error_abort);
4361 #endif
4364 colo_info_init();
4366 if (net_init_clients() < 0) {
4367 exit(1);
4370 if (qemu_opts_foreach(qemu_find_opts("object"),
4371 user_creatable_add_opts_foreach,
4372 object_create_delayed, NULL)) {
4373 exit(1);
4376 #ifdef CONFIG_TPM
4377 if (tpm_init() < 0) {
4378 exit(1);
4380 #endif
4382 /* init the bluetooth world */
4383 if (foreach_device_config(DEV_BT, bt_parse))
4384 exit(1);
4386 if (!xen_enabled()) {
4387 /* On 32-bit hosts, QEMU is limited by virtual address space */
4388 if (ram_size > (2047 << 20) && HOST_LONG_BITS == 32) {
4389 error_report("at most 2047 MB RAM can be simulated");
4390 exit(1);
4394 blk_mig_init();
4395 ram_mig_init();
4397 /* If the currently selected machine wishes to override the units-per-bus
4398 * property of its default HBA interface type, do so now. */
4399 if (machine_class->units_per_default_bus) {
4400 override_max_devs(machine_class->block_default_type,
4401 machine_class->units_per_default_bus);
4404 /* open the virtual block devices */
4405 if (snapshot || replay_mode != REPLAY_MODE_NONE) {
4406 qemu_opts_foreach(qemu_find_opts("drive"), drive_enable_snapshot,
4407 NULL, NULL);
4409 if (qemu_opts_foreach(qemu_find_opts("drive"), drive_init_func,
4410 &machine_class->block_default_type, NULL)) {
4411 exit(1);
4414 default_drive(default_cdrom, snapshot, machine_class->block_default_type, 2,
4415 CDROM_OPTS);
4416 default_drive(default_floppy, snapshot, IF_FLOPPY, 0, FD_OPTS);
4417 default_drive(default_sdcard, snapshot, IF_SD, 0, SD_OPTS);
4419 parse_numa_opts(machine_class);
4421 if (qemu_opts_foreach(qemu_find_opts("mon"),
4422 mon_init_func, NULL, NULL)) {
4423 exit(1);
4426 if (foreach_device_config(DEV_SERIAL, serial_parse) < 0)
4427 exit(1);
4428 if (foreach_device_config(DEV_PARALLEL, parallel_parse) < 0)
4429 exit(1);
4430 if (foreach_device_config(DEV_VIRTCON, virtcon_parse) < 0)
4431 exit(1);
4432 if (foreach_device_config(DEV_SCLP, sclp_parse) < 0) {
4433 exit(1);
4435 if (foreach_device_config(DEV_DEBUGCON, debugcon_parse) < 0)
4436 exit(1);
4438 /* If no default VGA is requested, the default is "none". */
4439 if (default_vga) {
4440 if (machine_class->default_display) {
4441 vga_model = machine_class->default_display;
4442 } else if (vga_interface_available(VGA_CIRRUS)) {
4443 vga_model = "cirrus";
4444 } else if (vga_interface_available(VGA_STD)) {
4445 vga_model = "std";
4448 if (vga_model) {
4449 select_vgahw(vga_model);
4452 if (watchdog) {
4453 i = select_watchdog(watchdog);
4454 if (i > 0)
4455 exit (i == 1 ? 1 : 0);
4458 machine_register_compat_props(current_machine);
4460 qemu_opts_foreach(qemu_find_opts("global"),
4461 global_init_func, NULL, NULL);
4463 /* This checkpoint is required by replay to separate prior clock
4464 reading from the other reads, because timer polling functions query
4465 clock values from the log. */
4466 replay_checkpoint(CHECKPOINT_INIT);
4467 qdev_machine_init();
4469 current_machine->ram_size = ram_size;
4470 current_machine->maxram_size = maxram_size;
4471 current_machine->ram_slots = ram_slots;
4472 current_machine->boot_order = boot_order;
4473 current_machine->cpu_model = cpu_model;
4475 machine_class->init(current_machine);
4477 realtime_init();
4479 audio_init();
4481 if (hax_enabled()) {
4482 hax_sync_vcpus();
4485 if (qemu_opts_foreach(qemu_find_opts("fw_cfg"),
4486 parse_fw_cfg, fw_cfg_find(), NULL) != 0) {
4487 exit(1);
4490 /* init USB devices */
4491 if (machine_usb(current_machine)) {
4492 if (foreach_device_config(DEV_USB, usb_parse) < 0)
4493 exit(1);
4496 /* Check if IGD GFX passthrough. */
4497 igd_gfx_passthru();
4499 /* init generic devices */
4500 rom_set_order_override(FW_CFG_ORDER_OVERRIDE_DEVICE);
4501 if (qemu_opts_foreach(qemu_find_opts("device"),
4502 device_init_func, NULL, NULL)) {
4503 exit(1);
4506 cpu_synchronize_all_post_init();
4508 numa_post_machine_init();
4510 rom_reset_order_override();
4512 /* Did we create any drives that we failed to create a device for? */
4513 drive_check_orphaned();
4515 /* Don't warn about the default network setup that you get if
4516 * no command line -net or -netdev options are specified. There
4517 * are two cases that we would otherwise complain about:
4518 * (1) board doesn't support a NIC but the implicit "-net nic"
4519 * requested one
4520 * (2) CONFIG_SLIRP not set, in which case the implicit "-net nic"
4521 * sets up a nic that isn't connected to anything.
4523 if (!default_net) {
4524 net_check_clients();
4528 if (boot_once) {
4529 qemu_boot_set(boot_once, &error_fatal);
4530 qemu_register_reset(restore_boot_order, g_strdup(boot_order));
4533 ds = init_displaystate();
4535 /* init local displays */
4536 switch (display_type) {
4537 case DT_CURSES:
4538 curses_display_init(ds, full_screen);
4539 break;
4540 case DT_SDL:
4541 sdl_display_init(ds, full_screen, no_frame);
4542 break;
4543 case DT_COCOA:
4544 cocoa_display_init(ds, full_screen);
4545 break;
4546 case DT_GTK:
4547 gtk_display_init(ds, full_screen, grab_on_hover);
4548 break;
4549 default:
4550 break;
4553 /* must be after terminal init, SDL library changes signal handlers */
4554 os_setup_signal_handling();
4556 /* init remote displays */
4557 #ifdef CONFIG_VNC
4558 qemu_opts_foreach(qemu_find_opts("vnc"),
4559 vnc_init_func, NULL, NULL);
4560 #endif
4562 if (using_spice) {
4563 qemu_spice_display_init();
4566 if (foreach_device_config(DEV_GDB, gdbserver_start) < 0) {
4567 exit(1);
4570 qdev_machine_creation_done();
4572 /* TODO: once all bus devices are qdevified, this should be done
4573 * when bus is created by qdev.c */
4574 qemu_register_reset(qbus_reset_all_fn, sysbus_get_default());
4575 qemu_run_machine_init_done_notifiers();
4577 if (rom_check_and_register_reset() != 0) {
4578 error_report("rom check and register reset failed");
4579 exit(1);
4582 replay_start();
4584 /* This checkpoint is required by replay to separate prior clock
4585 reading from the other reads, because timer polling functions query
4586 clock values from the log. */
4587 replay_checkpoint(CHECKPOINT_RESET);
4588 qemu_system_reset(VMRESET_SILENT);
4589 register_global_state();
4590 if (replay_mode != REPLAY_MODE_NONE) {
4591 replay_vmstate_init();
4592 } else if (loadvm) {
4593 if (load_vmstate(loadvm) < 0) {
4594 autostart = 0;
4598 qdev_prop_check_globals();
4599 if (vmstate_dump_file) {
4600 /* dump and exit */
4601 dump_vmstate_json_to_file(vmstate_dump_file);
4602 return 0;
4605 if (incoming) {
4606 Error *local_err = NULL;
4607 qemu_start_incoming_migration(incoming, &local_err);
4608 if (local_err) {
4609 error_reportf_err(local_err, "-incoming %s: ", incoming);
4610 exit(1);
4612 } else if (autostart) {
4613 vm_start();
4616 os_setup_post();
4618 main_loop();
4619 replay_disable_events();
4620 iothread_stop_all();
4622 bdrv_close_all();
4623 pause_all_vcpus();
4624 res_free();
4626 /* vhost-user must be cleaned up before chardevs. */
4627 net_cleanup();
4628 audio_cleanup();
4629 monitor_cleanup();
4630 qemu_chr_cleanup();
4632 return 0;