target-lm32: make cpu-qom.h not target specific
[qemu/ar7.git] / vl.c
blobbf61983a577e320f4eb8760297ebcd9d57e1df11
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/cutils.h"
26 #include "qemu/help_option.h"
28 #ifdef CONFIG_SECCOMP
29 #include "sysemu/seccomp.h"
30 #endif
32 #if defined(CONFIG_VDE)
33 #include <libvdeplug.h>
34 #endif
36 #ifdef CONFIG_SDL
37 #if defined(__APPLE__) || defined(main)
38 #include <SDL.h>
39 int qemu_main(int argc, char **argv, char **envp);
40 int main(int argc, char **argv)
42 return qemu_main(argc, argv, NULL);
44 #undef main
45 #define main qemu_main
46 #endif
47 #endif /* CONFIG_SDL */
49 #ifdef CONFIG_COCOA
50 #undef main
51 #define main qemu_main
52 #endif /* CONFIG_COCOA */
54 #include <glib.h>
56 #include "qemu/error-report.h"
57 #include "qemu/sockets.h"
58 #include "hw/hw.h"
59 #include "hw/boards.h"
60 #include "sysemu/accel.h"
61 #include "hw/usb.h"
62 #include "hw/i386/pc.h"
63 #include "hw/isa/isa.h"
64 #include "hw/bt.h"
65 #include "sysemu/watchdog.h"
66 #include "hw/smbios/smbios.h"
67 #include "hw/xen/xen.h"
68 #include "hw/qdev.h"
69 #include "hw/loader.h"
70 #include "monitor/qdev.h"
71 #include "sysemu/bt.h"
72 #include "net/net.h"
73 #include "net/slirp.h"
74 #include "monitor/monitor.h"
75 #include "ui/console.h"
76 #include "ui/input.h"
77 #include "sysemu/sysemu.h"
78 #include "sysemu/numa.h"
79 #include "exec/gdbstub.h"
80 #include "qemu/timer.h"
81 #include "sysemu/char.h"
82 #include "qemu/bitmap.h"
83 #include "sysemu/blockdev.h"
84 #include "hw/block/block.h"
85 #include "migration/block.h"
86 #include "sysemu/tpm.h"
87 #include "sysemu/dma.h"
88 #include "audio/audio.h"
89 #include "migration/migration.h"
90 #include "sysemu/cpus.h"
91 #include "sysemu/kvm.h"
92 #include "qapi/qmp/qjson.h"
93 #include "qemu/option.h"
94 #include "qemu/config-file.h"
95 #include "qemu-options.h"
96 #include "qmp-commands.h"
97 #include "qemu/main-loop.h"
98 #ifdef CONFIG_VIRTFS
99 #include "fsdev/qemu-fsdev.h"
100 #endif
101 #include "sysemu/qtest.h"
103 #include "disas/disas.h"
106 #include "slirp/libslirp.h"
108 #include "trace.h"
109 #include "trace/control.h"
110 #include "qemu/queue.h"
111 #include "sysemu/cpus.h"
112 #include "sysemu/arch_init.h"
114 #include "ui/qemu-spice.h"
115 #include "qapi/string-input-visitor.h"
116 #include "qapi/opts-visitor.h"
117 #include "qom/object_interfaces.h"
118 #include "qapi-event.h"
119 #include "exec/semihost.h"
120 #include "crypto/init.h"
121 #include "sysemu/replay.h"
122 #include "qapi/qmp/qerror.h"
124 #define MAX_VIRTIO_CONSOLES 1
125 #define MAX_SCLP_CONSOLES 1
127 static const char *data_dir[16];
128 static int data_dir_idx;
129 const char *bios_name = NULL;
130 enum vga_retrace_method vga_retrace_method = VGA_RETRACE_DUMB;
131 DisplayType display_type = DT_DEFAULT;
132 int request_opengl = -1;
133 int display_opengl;
134 static int display_remote;
135 const char* keyboard_layout = NULL;
136 ram_addr_t ram_size;
137 const char *mem_path = NULL;
138 int mem_prealloc = 0; /* force preallocation of physical target memory */
139 bool enable_mlock = false;
140 int nb_nics;
141 NICInfo nd_table[MAX_NICS];
142 int autostart;
143 static int rtc_utc = 1;
144 static int rtc_date_offset = -1; /* -1 means no change */
145 QEMUClockType rtc_clock;
146 int vga_interface_type = VGA_NONE;
147 static int full_screen = 0;
148 static int no_frame = 0;
149 int no_quit = 0;
150 #ifdef CONFIG_GTK
151 static bool grab_on_hover;
152 #endif
153 CharDriverState *serial_hds[MAX_SERIAL_PORTS];
154 CharDriverState *parallel_hds[MAX_PARALLEL_PORTS];
155 CharDriverState *virtcon_hds[MAX_VIRTIO_CONSOLES];
156 CharDriverState *sclp_hds[MAX_SCLP_CONSOLES];
157 int win2k_install_hack = 0;
158 int singlestep = 0;
159 int smp_cpus = 1;
160 int max_cpus = 0;
161 int smp_cores = 1;
162 int smp_threads = 1;
163 int acpi_enabled = 1;
164 int no_hpet = 0;
165 int fd_bootchk = 1;
166 static int no_reboot;
167 int no_shutdown = 0;
168 int cursor_hide = 1;
169 int graphic_rotate = 0;
170 const char *watchdog;
171 QEMUOptionRom option_rom[MAX_OPTION_ROMS];
172 int nb_option_roms;
173 int old_param = 0;
174 const char *qemu_name;
175 int alt_grab = 0;
176 int ctrl_grab = 0;
177 unsigned int nb_prom_envs = 0;
178 const char *prom_envs[MAX_PROM_ENVS];
179 int boot_menu;
180 bool boot_strict;
181 uint8_t *boot_splash_filedata;
182 size_t boot_splash_filedata_size;
183 uint8_t qemu_extra_params_fw[2];
185 int icount_align_option;
187 /* The bytes in qemu_uuid[] are in the order specified by RFC4122, _not_ in the
188 * little-endian "wire format" described in the SMBIOS 2.6 specification.
190 uint8_t qemu_uuid[16];
191 bool qemu_uuid_set;
193 static NotifierList exit_notifiers =
194 NOTIFIER_LIST_INITIALIZER(exit_notifiers);
196 static NotifierList machine_init_done_notifiers =
197 NOTIFIER_LIST_INITIALIZER(machine_init_done_notifiers);
199 bool xen_allowed;
200 uint32_t xen_domid;
201 enum xen_mode xen_mode = XEN_EMULATE;
203 static int has_defaults = 1;
204 static int default_serial = 1;
205 static int default_parallel = 1;
206 static int default_virtcon = 1;
207 static int default_sclp = 1;
208 static int default_monitor = 1;
209 static int default_floppy = 1;
210 static int default_cdrom = 1;
211 static int default_sdcard = 1;
212 static int default_vga = 1;
214 static struct {
215 const char *driver;
216 int *flag;
217 } default_list[] = {
218 { .driver = "isa-serial", .flag = &default_serial },
219 { .driver = "isa-parallel", .flag = &default_parallel },
220 { .driver = "isa-fdc", .flag = &default_floppy },
221 { .driver = "ide-cd", .flag = &default_cdrom },
222 { .driver = "ide-hd", .flag = &default_cdrom },
223 { .driver = "ide-drive", .flag = &default_cdrom },
224 { .driver = "scsi-cd", .flag = &default_cdrom },
225 { .driver = "virtio-serial-pci", .flag = &default_virtcon },
226 { .driver = "virtio-serial", .flag = &default_virtcon },
227 { .driver = "VGA", .flag = &default_vga },
228 { .driver = "isa-vga", .flag = &default_vga },
229 { .driver = "cirrus-vga", .flag = &default_vga },
230 { .driver = "isa-cirrus-vga", .flag = &default_vga },
231 { .driver = "vmware-svga", .flag = &default_vga },
232 { .driver = "qxl-vga", .flag = &default_vga },
233 { .driver = "virtio-vga", .flag = &default_vga },
236 static QemuOptsList qemu_rtc_opts = {
237 .name = "rtc",
238 .head = QTAILQ_HEAD_INITIALIZER(qemu_rtc_opts.head),
239 .desc = {
241 .name = "base",
242 .type = QEMU_OPT_STRING,
244 .name = "clock",
245 .type = QEMU_OPT_STRING,
247 .name = "driftfix",
248 .type = QEMU_OPT_STRING,
250 { /* end of list */ }
254 static QemuOptsList qemu_sandbox_opts = {
255 .name = "sandbox",
256 .implied_opt_name = "enable",
257 .head = QTAILQ_HEAD_INITIALIZER(qemu_sandbox_opts.head),
258 .desc = {
260 .name = "enable",
261 .type = QEMU_OPT_BOOL,
263 { /* end of list */ }
267 static QemuOptsList qemu_trace_opts = {
268 .name = "trace",
269 .implied_opt_name = "enable",
270 .head = QTAILQ_HEAD_INITIALIZER(qemu_trace_opts.head),
271 .desc = {
273 .name = "enable",
274 .type = QEMU_OPT_STRING,
277 .name = "events",
278 .type = QEMU_OPT_STRING,
280 .name = "file",
281 .type = QEMU_OPT_STRING,
283 { /* end of list */ }
287 static QemuOptsList qemu_option_rom_opts = {
288 .name = "option-rom",
289 .implied_opt_name = "romfile",
290 .head = QTAILQ_HEAD_INITIALIZER(qemu_option_rom_opts.head),
291 .desc = {
293 .name = "bootindex",
294 .type = QEMU_OPT_NUMBER,
295 }, {
296 .name = "romfile",
297 .type = QEMU_OPT_STRING,
299 { /* end of list */ }
303 static QemuOptsList qemu_machine_opts = {
304 .name = "machine",
305 .implied_opt_name = "type",
306 .merge_lists = true,
307 .head = QTAILQ_HEAD_INITIALIZER(qemu_machine_opts.head),
308 .desc = {
310 * no elements => accept any
311 * sanity checking will happen later
312 * when setting machine properties
318 static QemuOptsList qemu_boot_opts = {
319 .name = "boot-opts",
320 .implied_opt_name = "order",
321 .merge_lists = true,
322 .head = QTAILQ_HEAD_INITIALIZER(qemu_boot_opts.head),
323 .desc = {
325 .name = "order",
326 .type = QEMU_OPT_STRING,
327 }, {
328 .name = "once",
329 .type = QEMU_OPT_STRING,
330 }, {
331 .name = "menu",
332 .type = QEMU_OPT_BOOL,
333 }, {
334 .name = "splash",
335 .type = QEMU_OPT_STRING,
336 }, {
337 .name = "splash-time",
338 .type = QEMU_OPT_STRING,
339 }, {
340 .name = "reboot-timeout",
341 .type = QEMU_OPT_STRING,
342 }, {
343 .name = "strict",
344 .type = QEMU_OPT_BOOL,
346 { /*End of list */ }
350 static QemuOptsList qemu_add_fd_opts = {
351 .name = "add-fd",
352 .head = QTAILQ_HEAD_INITIALIZER(qemu_add_fd_opts.head),
353 .desc = {
355 .name = "fd",
356 .type = QEMU_OPT_NUMBER,
357 .help = "file descriptor of which a duplicate is added to fd set",
359 .name = "set",
360 .type = QEMU_OPT_NUMBER,
361 .help = "ID of the fd set to add fd to",
363 .name = "opaque",
364 .type = QEMU_OPT_STRING,
365 .help = "free-form string used to describe fd",
367 { /* end of list */ }
371 static QemuOptsList qemu_object_opts = {
372 .name = "object",
373 .implied_opt_name = "qom-type",
374 .head = QTAILQ_HEAD_INITIALIZER(qemu_object_opts.head),
375 .desc = {
380 static QemuOptsList qemu_tpmdev_opts = {
381 .name = "tpmdev",
382 .implied_opt_name = "type",
383 .head = QTAILQ_HEAD_INITIALIZER(qemu_tpmdev_opts.head),
384 .desc = {
385 /* options are defined in the TPM backends */
386 { /* end of list */ }
390 static QemuOptsList qemu_realtime_opts = {
391 .name = "realtime",
392 .head = QTAILQ_HEAD_INITIALIZER(qemu_realtime_opts.head),
393 .desc = {
395 .name = "mlock",
396 .type = QEMU_OPT_BOOL,
398 { /* end of list */ }
402 static QemuOptsList qemu_msg_opts = {
403 .name = "msg",
404 .head = QTAILQ_HEAD_INITIALIZER(qemu_msg_opts.head),
405 .desc = {
407 .name = "timestamp",
408 .type = QEMU_OPT_BOOL,
410 { /* end of list */ }
414 static QemuOptsList qemu_name_opts = {
415 .name = "name",
416 .implied_opt_name = "guest",
417 .merge_lists = true,
418 .head = QTAILQ_HEAD_INITIALIZER(qemu_name_opts.head),
419 .desc = {
421 .name = "guest",
422 .type = QEMU_OPT_STRING,
423 .help = "Sets the name of the guest.\n"
424 "This name will be displayed in the SDL window caption.\n"
425 "The name will also be used for the VNC server",
426 }, {
427 .name = "process",
428 .type = QEMU_OPT_STRING,
429 .help = "Sets the name of the QEMU process, as shown in top etc",
430 }, {
431 .name = "debug-threads",
432 .type = QEMU_OPT_BOOL,
433 .help = "When enabled, name the individual threads; defaults off.\n"
434 "NOTE: The thread names are for debugging and not a\n"
435 "stable API.",
437 { /* End of list */ }
441 static QemuOptsList qemu_mem_opts = {
442 .name = "memory",
443 .implied_opt_name = "size",
444 .head = QTAILQ_HEAD_INITIALIZER(qemu_mem_opts.head),
445 .merge_lists = true,
446 .desc = {
448 .name = "size",
449 .type = QEMU_OPT_SIZE,
452 .name = "slots",
453 .type = QEMU_OPT_NUMBER,
456 .name = "maxmem",
457 .type = QEMU_OPT_SIZE,
459 { /* end of list */ }
463 static QemuOptsList qemu_icount_opts = {
464 .name = "icount",
465 .implied_opt_name = "shift",
466 .merge_lists = true,
467 .head = QTAILQ_HEAD_INITIALIZER(qemu_icount_opts.head),
468 .desc = {
470 .name = "shift",
471 .type = QEMU_OPT_STRING,
472 }, {
473 .name = "align",
474 .type = QEMU_OPT_BOOL,
475 }, {
476 .name = "sleep",
477 .type = QEMU_OPT_BOOL,
478 }, {
479 .name = "rr",
480 .type = QEMU_OPT_STRING,
481 }, {
482 .name = "rrfile",
483 .type = QEMU_OPT_STRING,
485 { /* end of list */ }
489 static QemuOptsList qemu_semihosting_config_opts = {
490 .name = "semihosting-config",
491 .implied_opt_name = "enable",
492 .head = QTAILQ_HEAD_INITIALIZER(qemu_semihosting_config_opts.head),
493 .desc = {
495 .name = "enable",
496 .type = QEMU_OPT_BOOL,
497 }, {
498 .name = "target",
499 .type = QEMU_OPT_STRING,
500 }, {
501 .name = "arg",
502 .type = QEMU_OPT_STRING,
504 { /* end of list */ }
508 static QemuOptsList qemu_fw_cfg_opts = {
509 .name = "fw_cfg",
510 .implied_opt_name = "name",
511 .head = QTAILQ_HEAD_INITIALIZER(qemu_fw_cfg_opts.head),
512 .desc = {
514 .name = "name",
515 .type = QEMU_OPT_STRING,
516 .help = "Sets the fw_cfg name of the blob to be inserted",
517 }, {
518 .name = "file",
519 .type = QEMU_OPT_STRING,
520 .help = "Sets the name of the file from which\n"
521 "the fw_cfg blob will be loaded",
522 }, {
523 .name = "string",
524 .type = QEMU_OPT_STRING,
525 .help = "Sets content of the blob to be inserted from a string",
527 { /* end of list */ }
532 * Get machine options
534 * Returns: machine options (never null).
536 QemuOpts *qemu_get_machine_opts(void)
538 return qemu_find_opts_singleton("machine");
541 const char *qemu_get_vm_name(void)
543 return qemu_name;
546 static void res_free(void)
548 g_free(boot_splash_filedata);
549 boot_splash_filedata = NULL;
552 static int default_driver_check(void *opaque, QemuOpts *opts, Error **errp)
554 const char *driver = qemu_opt_get(opts, "driver");
555 int i;
557 if (!driver)
558 return 0;
559 for (i = 0; i < ARRAY_SIZE(default_list); i++) {
560 if (strcmp(default_list[i].driver, driver) != 0)
561 continue;
562 *(default_list[i].flag) = 0;
564 return 0;
567 /***********************************************************/
568 /* QEMU state */
570 static RunState current_run_state = RUN_STATE_PRELAUNCH;
572 /* We use RUN_STATE__MAX but any invalid value will do */
573 static RunState vmstop_requested = RUN_STATE__MAX;
574 static QemuMutex vmstop_lock;
576 typedef struct {
577 RunState from;
578 RunState to;
579 } RunStateTransition;
581 static const RunStateTransition runstate_transitions_def[] = {
582 /* from -> to */
583 { RUN_STATE_DEBUG, RUN_STATE_RUNNING },
584 { RUN_STATE_DEBUG, RUN_STATE_FINISH_MIGRATE },
585 { RUN_STATE_DEBUG, RUN_STATE_PRELAUNCH },
587 { RUN_STATE_INMIGRATE, RUN_STATE_INTERNAL_ERROR },
588 { RUN_STATE_INMIGRATE, RUN_STATE_IO_ERROR },
589 { RUN_STATE_INMIGRATE, RUN_STATE_PAUSED },
590 { RUN_STATE_INMIGRATE, RUN_STATE_RUNNING },
591 { RUN_STATE_INMIGRATE, RUN_STATE_SHUTDOWN },
592 { RUN_STATE_INMIGRATE, RUN_STATE_SUSPENDED },
593 { RUN_STATE_INMIGRATE, RUN_STATE_WATCHDOG },
594 { RUN_STATE_INMIGRATE, RUN_STATE_GUEST_PANICKED },
595 { RUN_STATE_INMIGRATE, RUN_STATE_FINISH_MIGRATE },
596 { RUN_STATE_INMIGRATE, RUN_STATE_PRELAUNCH },
597 { RUN_STATE_INMIGRATE, RUN_STATE_POSTMIGRATE },
599 { RUN_STATE_INTERNAL_ERROR, RUN_STATE_PAUSED },
600 { RUN_STATE_INTERNAL_ERROR, RUN_STATE_FINISH_MIGRATE },
601 { RUN_STATE_INTERNAL_ERROR, RUN_STATE_PRELAUNCH },
603 { RUN_STATE_IO_ERROR, RUN_STATE_RUNNING },
604 { RUN_STATE_IO_ERROR, RUN_STATE_FINISH_MIGRATE },
605 { RUN_STATE_IO_ERROR, RUN_STATE_PRELAUNCH },
607 { RUN_STATE_PAUSED, RUN_STATE_RUNNING },
608 { RUN_STATE_PAUSED, RUN_STATE_FINISH_MIGRATE },
609 { RUN_STATE_PAUSED, RUN_STATE_PRELAUNCH },
611 { RUN_STATE_POSTMIGRATE, RUN_STATE_RUNNING },
612 { RUN_STATE_POSTMIGRATE, RUN_STATE_FINISH_MIGRATE },
613 { RUN_STATE_POSTMIGRATE, RUN_STATE_PRELAUNCH },
615 { RUN_STATE_PRELAUNCH, RUN_STATE_RUNNING },
616 { RUN_STATE_PRELAUNCH, RUN_STATE_FINISH_MIGRATE },
617 { RUN_STATE_PRELAUNCH, RUN_STATE_INMIGRATE },
619 { RUN_STATE_FINISH_MIGRATE, RUN_STATE_RUNNING },
620 { RUN_STATE_FINISH_MIGRATE, RUN_STATE_POSTMIGRATE },
621 { RUN_STATE_FINISH_MIGRATE, RUN_STATE_PRELAUNCH },
623 { RUN_STATE_RESTORE_VM, RUN_STATE_RUNNING },
624 { RUN_STATE_RESTORE_VM, RUN_STATE_PRELAUNCH },
626 { RUN_STATE_RUNNING, RUN_STATE_DEBUG },
627 { RUN_STATE_RUNNING, RUN_STATE_INTERNAL_ERROR },
628 { RUN_STATE_RUNNING, RUN_STATE_IO_ERROR },
629 { RUN_STATE_RUNNING, RUN_STATE_PAUSED },
630 { RUN_STATE_RUNNING, RUN_STATE_FINISH_MIGRATE },
631 { RUN_STATE_RUNNING, RUN_STATE_RESTORE_VM },
632 { RUN_STATE_RUNNING, RUN_STATE_SAVE_VM },
633 { RUN_STATE_RUNNING, RUN_STATE_SHUTDOWN },
634 { RUN_STATE_RUNNING, RUN_STATE_WATCHDOG },
635 { RUN_STATE_RUNNING, RUN_STATE_GUEST_PANICKED },
637 { RUN_STATE_SAVE_VM, RUN_STATE_RUNNING },
639 { RUN_STATE_SHUTDOWN, RUN_STATE_PAUSED },
640 { RUN_STATE_SHUTDOWN, RUN_STATE_FINISH_MIGRATE },
641 { RUN_STATE_SHUTDOWN, RUN_STATE_PRELAUNCH },
643 { RUN_STATE_DEBUG, RUN_STATE_SUSPENDED },
644 { RUN_STATE_RUNNING, RUN_STATE_SUSPENDED },
645 { RUN_STATE_SUSPENDED, RUN_STATE_RUNNING },
646 { RUN_STATE_SUSPENDED, RUN_STATE_FINISH_MIGRATE },
647 { RUN_STATE_SUSPENDED, RUN_STATE_PRELAUNCH },
649 { RUN_STATE_WATCHDOG, RUN_STATE_RUNNING },
650 { RUN_STATE_WATCHDOG, RUN_STATE_FINISH_MIGRATE },
651 { RUN_STATE_WATCHDOG, RUN_STATE_PRELAUNCH },
653 { RUN_STATE_GUEST_PANICKED, RUN_STATE_RUNNING },
654 { RUN_STATE_GUEST_PANICKED, RUN_STATE_FINISH_MIGRATE },
655 { RUN_STATE_GUEST_PANICKED, RUN_STATE_PRELAUNCH },
657 { RUN_STATE__MAX, RUN_STATE__MAX },
660 static bool runstate_valid_transitions[RUN_STATE__MAX][RUN_STATE__MAX];
662 bool runstate_check(RunState state)
664 return current_run_state == state;
667 bool runstate_store(char *str, size_t size)
669 const char *state = RunState_lookup[current_run_state];
670 size_t len = strlen(state) + 1;
672 if (len > size) {
673 return false;
675 memcpy(str, state, len);
676 return true;
679 static void runstate_init(void)
681 const RunStateTransition *p;
683 memset(&runstate_valid_transitions, 0, sizeof(runstate_valid_transitions));
684 for (p = &runstate_transitions_def[0]; p->from != RUN_STATE__MAX; p++) {
685 runstate_valid_transitions[p->from][p->to] = true;
688 qemu_mutex_init(&vmstop_lock);
691 /* This function will abort() on invalid state transitions */
692 void runstate_set(RunState new_state)
694 assert(new_state < RUN_STATE__MAX);
696 if (!runstate_valid_transitions[current_run_state][new_state]) {
697 error_report("invalid runstate transition: '%s' -> '%s'",
698 RunState_lookup[current_run_state],
699 RunState_lookup[new_state]);
700 abort();
702 trace_runstate_set(new_state);
703 current_run_state = new_state;
706 int runstate_is_running(void)
708 return runstate_check(RUN_STATE_RUNNING);
711 bool runstate_needs_reset(void)
713 return runstate_check(RUN_STATE_INTERNAL_ERROR) ||
714 runstate_check(RUN_STATE_SHUTDOWN);
717 StatusInfo *qmp_query_status(Error **errp)
719 StatusInfo *info = g_malloc0(sizeof(*info));
721 info->running = runstate_is_running();
722 info->singlestep = singlestep;
723 info->status = current_run_state;
725 return info;
728 static bool qemu_vmstop_requested(RunState *r)
730 qemu_mutex_lock(&vmstop_lock);
731 *r = vmstop_requested;
732 vmstop_requested = RUN_STATE__MAX;
733 qemu_mutex_unlock(&vmstop_lock);
734 return *r < RUN_STATE__MAX;
737 void qemu_system_vmstop_request_prepare(void)
739 qemu_mutex_lock(&vmstop_lock);
742 void qemu_system_vmstop_request(RunState state)
744 vmstop_requested = state;
745 qemu_mutex_unlock(&vmstop_lock);
746 qemu_notify_event();
749 void vm_start(void)
751 RunState requested;
753 qemu_vmstop_requested(&requested);
754 if (runstate_is_running() && requested == RUN_STATE__MAX) {
755 return;
758 /* Ensure that a STOP/RESUME pair of events is emitted if a
759 * vmstop request was pending. The BLOCK_IO_ERROR event, for
760 * example, according to documentation is always followed by
761 * the STOP event.
763 if (runstate_is_running()) {
764 qapi_event_send_stop(&error_abort);
765 } else {
766 cpu_enable_ticks();
767 runstate_set(RUN_STATE_RUNNING);
768 vm_state_notify(1, RUN_STATE_RUNNING);
769 resume_all_vcpus();
772 qapi_event_send_resume(&error_abort);
776 /***********************************************************/
777 /* real time host monotonic timer */
779 static time_t qemu_time(void)
781 return qemu_clock_get_ms(QEMU_CLOCK_HOST) / 1000;
784 /***********************************************************/
785 /* host time/date access */
786 void qemu_get_timedate(struct tm *tm, int offset)
788 time_t ti = qemu_time();
790 ti += offset;
791 if (rtc_date_offset == -1) {
792 if (rtc_utc)
793 gmtime_r(&ti, tm);
794 else
795 localtime_r(&ti, tm);
796 } else {
797 ti -= rtc_date_offset;
798 gmtime_r(&ti, tm);
802 int qemu_timedate_diff(struct tm *tm)
804 time_t seconds;
806 if (rtc_date_offset == -1)
807 if (rtc_utc)
808 seconds = mktimegm(tm);
809 else {
810 struct tm tmp = *tm;
811 tmp.tm_isdst = -1; /* use timezone to figure it out */
812 seconds = mktime(&tmp);
814 else
815 seconds = mktimegm(tm) + rtc_date_offset;
817 return seconds - qemu_time();
820 static void configure_rtc_date_offset(const char *startdate, int legacy)
822 time_t rtc_start_date;
823 struct tm tm;
825 if (!strcmp(startdate, "now") && legacy) {
826 rtc_date_offset = -1;
827 } else {
828 if (sscanf(startdate, "%d-%d-%dT%d:%d:%d",
829 &tm.tm_year,
830 &tm.tm_mon,
831 &tm.tm_mday,
832 &tm.tm_hour,
833 &tm.tm_min,
834 &tm.tm_sec) == 6) {
835 /* OK */
836 } else if (sscanf(startdate, "%d-%d-%d",
837 &tm.tm_year,
838 &tm.tm_mon,
839 &tm.tm_mday) == 3) {
840 tm.tm_hour = 0;
841 tm.tm_min = 0;
842 tm.tm_sec = 0;
843 } else {
844 goto date_fail;
846 tm.tm_year -= 1900;
847 tm.tm_mon--;
848 rtc_start_date = mktimegm(&tm);
849 if (rtc_start_date == -1) {
850 date_fail:
851 error_report("invalid date format");
852 error_printf("valid formats: "
853 "'2006-06-17T16:01:21' or '2006-06-17'\n");
854 exit(1);
856 rtc_date_offset = qemu_time() - rtc_start_date;
860 static void configure_rtc(QemuOpts *opts)
862 const char *value;
864 value = qemu_opt_get(opts, "base");
865 if (value) {
866 if (!strcmp(value, "utc")) {
867 rtc_utc = 1;
868 } else if (!strcmp(value, "localtime")) {
869 Error *blocker = NULL;
870 rtc_utc = 0;
871 error_setg(&blocker, QERR_REPLAY_NOT_SUPPORTED,
872 "-rtc base=localtime");
873 replay_add_blocker(blocker);
874 } else {
875 configure_rtc_date_offset(value, 0);
878 value = qemu_opt_get(opts, "clock");
879 if (value) {
880 if (!strcmp(value, "host")) {
881 rtc_clock = QEMU_CLOCK_HOST;
882 } else if (!strcmp(value, "rt")) {
883 rtc_clock = QEMU_CLOCK_REALTIME;
884 } else if (!strcmp(value, "vm")) {
885 rtc_clock = QEMU_CLOCK_VIRTUAL;
886 } else {
887 error_report("invalid option value '%s'", value);
888 exit(1);
891 value = qemu_opt_get(opts, "driftfix");
892 if (value) {
893 if (!strcmp(value, "slew")) {
894 static GlobalProperty slew_lost_ticks[] = {
896 .driver = "mc146818rtc",
897 .property = "lost_tick_policy",
898 .value = "slew",
900 { /* end of list */ }
903 qdev_prop_register_global_list(slew_lost_ticks);
904 } else if (!strcmp(value, "none")) {
905 /* discard is default */
906 } else {
907 error_report("invalid option value '%s'", value);
908 exit(1);
913 /***********************************************************/
914 /* Bluetooth support */
915 static int nb_hcis;
916 static int cur_hci;
917 static struct HCIInfo *hci_table[MAX_NICS];
919 struct HCIInfo *qemu_next_hci(void)
921 if (cur_hci == nb_hcis)
922 return &null_hci;
924 return hci_table[cur_hci++];
927 static int bt_hci_parse(const char *str)
929 struct HCIInfo *hci;
930 bdaddr_t bdaddr;
932 if (nb_hcis >= MAX_NICS) {
933 error_report("too many bluetooth HCIs (max %i)", MAX_NICS);
934 return -1;
937 hci = hci_init(str);
938 if (!hci)
939 return -1;
941 bdaddr.b[0] = 0x52;
942 bdaddr.b[1] = 0x54;
943 bdaddr.b[2] = 0x00;
944 bdaddr.b[3] = 0x12;
945 bdaddr.b[4] = 0x34;
946 bdaddr.b[5] = 0x56 + nb_hcis;
947 hci->bdaddr_set(hci, bdaddr.b);
949 hci_table[nb_hcis++] = hci;
951 return 0;
954 static void bt_vhci_add(int vlan_id)
956 struct bt_scatternet_s *vlan = qemu_find_bt_vlan(vlan_id);
958 if (!vlan->slave)
959 error_report("warning: adding a VHCI to an empty scatternet %i",
960 vlan_id);
962 bt_vhci_init(bt_new_hci(vlan));
965 static struct bt_device_s *bt_device_add(const char *opt)
967 struct bt_scatternet_s *vlan;
968 int vlan_id = 0;
969 char *endp = strstr(opt, ",vlan=");
970 int len = (endp ? endp - opt : strlen(opt)) + 1;
971 char devname[10];
973 pstrcpy(devname, MIN(sizeof(devname), len), opt);
975 if (endp) {
976 vlan_id = strtol(endp + 6, &endp, 0);
977 if (*endp) {
978 error_report("unrecognised bluetooth vlan Id");
979 return 0;
983 vlan = qemu_find_bt_vlan(vlan_id);
985 if (!vlan->slave)
986 error_report("warning: adding a slave device to an empty scatternet %i",
987 vlan_id);
989 if (!strcmp(devname, "keyboard"))
990 return bt_keyboard_init(vlan);
992 error_report("unsupported bluetooth device '%s'", devname);
993 return 0;
996 static int bt_parse(const char *opt)
998 const char *endp, *p;
999 int vlan;
1001 if (strstart(opt, "hci", &endp)) {
1002 if (!*endp || *endp == ',') {
1003 if (*endp)
1004 if (!strstart(endp, ",vlan=", 0))
1005 opt = endp + 1;
1007 return bt_hci_parse(opt);
1009 } else if (strstart(opt, "vhci", &endp)) {
1010 if (!*endp || *endp == ',') {
1011 if (*endp) {
1012 if (strstart(endp, ",vlan=", &p)) {
1013 vlan = strtol(p, (char **) &endp, 0);
1014 if (*endp) {
1015 error_report("bad scatternet '%s'", p);
1016 return 1;
1018 } else {
1019 error_report("bad parameter '%s'", endp + 1);
1020 return 1;
1022 } else
1023 vlan = 0;
1025 bt_vhci_add(vlan);
1026 return 0;
1028 } else if (strstart(opt, "device:", &endp))
1029 return !bt_device_add(endp);
1031 error_report("bad bluetooth parameter '%s'", opt);
1032 return 1;
1035 static int parse_sandbox(void *opaque, QemuOpts *opts, Error **errp)
1037 /* FIXME: change this to true for 1.3 */
1038 if (qemu_opt_get_bool(opts, "enable", false)) {
1039 #ifdef CONFIG_SECCOMP
1040 if (seccomp_start() < 0) {
1041 error_report("failed to install seccomp syscall filter "
1042 "in the kernel");
1043 return -1;
1045 #else
1046 error_report("seccomp support is disabled");
1047 return -1;
1048 #endif
1051 return 0;
1054 static int parse_name(void *opaque, QemuOpts *opts, Error **errp)
1056 const char *proc_name;
1058 if (qemu_opt_get(opts, "debug-threads")) {
1059 qemu_thread_naming(qemu_opt_get_bool(opts, "debug-threads", false));
1061 qemu_name = qemu_opt_get(opts, "guest");
1063 proc_name = qemu_opt_get(opts, "process");
1064 if (proc_name) {
1065 os_set_proc_name(proc_name);
1068 return 0;
1071 bool defaults_enabled(void)
1073 return has_defaults;
1076 bool usb_enabled(void)
1078 return machine_usb(current_machine);
1081 #ifndef _WIN32
1082 static int parse_add_fd(void *opaque, QemuOpts *opts, Error **errp)
1084 int fd, dupfd, flags;
1085 int64_t fdset_id;
1086 const char *fd_opaque = NULL;
1087 AddfdInfo *fdinfo;
1089 fd = qemu_opt_get_number(opts, "fd", -1);
1090 fdset_id = qemu_opt_get_number(opts, "set", -1);
1091 fd_opaque = qemu_opt_get(opts, "opaque");
1093 if (fd < 0) {
1094 error_report("fd option is required and must be non-negative");
1095 return -1;
1098 if (fd <= STDERR_FILENO) {
1099 error_report("fd cannot be a standard I/O stream");
1100 return -1;
1104 * All fds inherited across exec() necessarily have FD_CLOEXEC
1105 * clear, while qemu sets FD_CLOEXEC on all other fds used internally.
1107 flags = fcntl(fd, F_GETFD);
1108 if (flags == -1 || (flags & FD_CLOEXEC)) {
1109 error_report("fd is not valid or already in use");
1110 return -1;
1113 if (fdset_id < 0) {
1114 error_report("set option is required and must be non-negative");
1115 return -1;
1118 #ifdef F_DUPFD_CLOEXEC
1119 dupfd = fcntl(fd, F_DUPFD_CLOEXEC, 0);
1120 #else
1121 dupfd = dup(fd);
1122 if (dupfd != -1) {
1123 qemu_set_cloexec(dupfd);
1125 #endif
1126 if (dupfd == -1) {
1127 error_report("error duplicating fd: %s", strerror(errno));
1128 return -1;
1131 /* add the duplicate fd, and optionally the opaque string, to the fd set */
1132 fdinfo = monitor_fdset_add_fd(dupfd, true, fdset_id, !!fd_opaque, fd_opaque,
1133 &error_abort);
1134 g_free(fdinfo);
1136 return 0;
1139 static int cleanup_add_fd(void *opaque, QemuOpts *opts, Error **errp)
1141 int fd;
1143 fd = qemu_opt_get_number(opts, "fd", -1);
1144 close(fd);
1146 return 0;
1148 #endif
1150 /***********************************************************/
1151 /* QEMU Block devices */
1153 #define HD_OPTS "media=disk"
1154 #define CDROM_OPTS "media=cdrom"
1155 #define FD_OPTS ""
1156 #define PFLASH_OPTS ""
1157 #define MTD_OPTS ""
1158 #define SD_OPTS ""
1160 static int drive_init_func(void *opaque, QemuOpts *opts, Error **errp)
1162 BlockInterfaceType *block_default_type = opaque;
1164 return drive_new(opts, *block_default_type) == NULL;
1167 static int drive_enable_snapshot(void *opaque, QemuOpts *opts, Error **errp)
1169 if (qemu_opt_get(opts, "snapshot") == NULL) {
1170 qemu_opt_set(opts, "snapshot", "on", &error_abort);
1172 return 0;
1175 static void default_drive(int enable, int snapshot, BlockInterfaceType type,
1176 int index, const char *optstr)
1178 QemuOpts *opts;
1179 DriveInfo *dinfo;
1181 if (!enable || drive_get_by_index(type, index)) {
1182 return;
1185 opts = drive_add(type, index, NULL, optstr);
1186 if (snapshot) {
1187 drive_enable_snapshot(NULL, opts, NULL);
1190 dinfo = drive_new(opts, type);
1191 if (!dinfo) {
1192 exit(1);
1194 dinfo->is_default = true;
1198 static QemuOptsList qemu_smp_opts = {
1199 .name = "smp-opts",
1200 .implied_opt_name = "cpus",
1201 .merge_lists = true,
1202 .head = QTAILQ_HEAD_INITIALIZER(qemu_smp_opts.head),
1203 .desc = {
1205 .name = "cpus",
1206 .type = QEMU_OPT_NUMBER,
1207 }, {
1208 .name = "sockets",
1209 .type = QEMU_OPT_NUMBER,
1210 }, {
1211 .name = "cores",
1212 .type = QEMU_OPT_NUMBER,
1213 }, {
1214 .name = "threads",
1215 .type = QEMU_OPT_NUMBER,
1216 }, {
1217 .name = "maxcpus",
1218 .type = QEMU_OPT_NUMBER,
1220 { /*End of list */ }
1224 static void smp_parse(QemuOpts *opts)
1226 if (opts) {
1228 unsigned cpus = qemu_opt_get_number(opts, "cpus", 0);
1229 unsigned sockets = qemu_opt_get_number(opts, "sockets", 0);
1230 unsigned cores = qemu_opt_get_number(opts, "cores", 0);
1231 unsigned threads = qemu_opt_get_number(opts, "threads", 0);
1233 /* compute missing values, prefer sockets over cores over threads */
1234 if (cpus == 0 || sockets == 0) {
1235 sockets = sockets > 0 ? sockets : 1;
1236 cores = cores > 0 ? cores : 1;
1237 threads = threads > 0 ? threads : 1;
1238 if (cpus == 0) {
1239 cpus = cores * threads * sockets;
1241 } else if (cores == 0) {
1242 threads = threads > 0 ? threads : 1;
1243 cores = cpus / (sockets * threads);
1244 } else if (threads == 0) {
1245 threads = cpus / (cores * sockets);
1246 } else if (sockets * cores * threads < cpus) {
1247 error_report("cpu topology: "
1248 "sockets (%u) * cores (%u) * threads (%u) < "
1249 "smp_cpus (%u)",
1250 sockets, cores, threads, cpus);
1251 exit(1);
1254 max_cpus = qemu_opt_get_number(opts, "maxcpus", cpus);
1255 if (sockets * cores * threads > max_cpus) {
1256 error_report("cpu topology: "
1257 "sockets (%u) * cores (%u) * threads (%u) > "
1258 "maxcpus (%u)",
1259 sockets, cores, threads, max_cpus);
1260 exit(1);
1263 smp_cpus = cpus;
1264 smp_cores = cores > 0 ? cores : 1;
1265 smp_threads = threads > 0 ? threads : 1;
1269 if (max_cpus == 0) {
1270 max_cpus = smp_cpus;
1273 if (max_cpus > MAX_CPUMASK_BITS) {
1274 error_report("unsupported number of maxcpus");
1275 exit(1);
1277 if (max_cpus < smp_cpus) {
1278 error_report("maxcpus must be equal to or greater than smp");
1279 exit(1);
1282 if (smp_cpus > 1 || smp_cores > 1 || smp_threads > 1) {
1283 Error *blocker = NULL;
1284 error_setg(&blocker, QERR_REPLAY_NOT_SUPPORTED, "smp");
1285 replay_add_blocker(blocker);
1289 static void realtime_init(void)
1291 if (enable_mlock) {
1292 if (os_mlock() < 0) {
1293 error_report("locking memory failed");
1294 exit(1);
1300 static void configure_msg(QemuOpts *opts)
1302 enable_timestamp_msg = qemu_opt_get_bool(opts, "timestamp", true);
1305 /***********************************************************/
1306 /* Semihosting */
1308 typedef struct SemihostingConfig {
1309 bool enabled;
1310 SemihostingTarget target;
1311 const char **argv;
1312 int argc;
1313 const char *cmdline; /* concatenated argv */
1314 } SemihostingConfig;
1316 static SemihostingConfig semihosting;
1318 bool semihosting_enabled(void)
1320 return semihosting.enabled;
1323 SemihostingTarget semihosting_get_target(void)
1325 return semihosting.target;
1328 const char *semihosting_get_arg(int i)
1330 if (i >= semihosting.argc) {
1331 return NULL;
1333 return semihosting.argv[i];
1336 int semihosting_get_argc(void)
1338 return semihosting.argc;
1341 const char *semihosting_get_cmdline(void)
1343 if (semihosting.cmdline == NULL && semihosting.argc > 0) {
1344 semihosting.cmdline = g_strjoinv(" ", (gchar **)semihosting.argv);
1346 return semihosting.cmdline;
1349 static int add_semihosting_arg(void *opaque,
1350 const char *name, const char *val,
1351 Error **errp)
1353 SemihostingConfig *s = opaque;
1354 if (strcmp(name, "arg") == 0) {
1355 s->argc++;
1356 /* one extra element as g_strjoinv() expects NULL-terminated array */
1357 s->argv = g_realloc(s->argv, (s->argc + 1) * sizeof(void *));
1358 s->argv[s->argc - 1] = val;
1359 s->argv[s->argc] = NULL;
1361 return 0;
1364 /* Use strings passed via -kernel/-append to initialize semihosting.argv[] */
1365 static inline void semihosting_arg_fallback(const char *file, const char *cmd)
1367 char *cmd_token;
1369 /* argv[0] */
1370 add_semihosting_arg(&semihosting, "arg", file, NULL);
1372 /* split -append and initialize argv[1..n] */
1373 cmd_token = strtok(g_strdup(cmd), " ");
1374 while (cmd_token) {
1375 add_semihosting_arg(&semihosting, "arg", cmd_token, NULL);
1376 cmd_token = strtok(NULL, " ");
1380 /* Now we still need this for compatibility with XEN. */
1381 bool has_igd_gfx_passthru;
1382 static void igd_gfx_passthru(void)
1384 has_igd_gfx_passthru = current_machine->igd_gfx_passthru;
1387 /***********************************************************/
1388 /* USB devices */
1390 static int usb_device_add(const char *devname)
1392 USBDevice *dev = NULL;
1393 #ifndef CONFIG_LINUX
1394 const char *p;
1395 #endif
1397 if (!usb_enabled()) {
1398 return -1;
1401 /* drivers with .usbdevice_name entry in USBDeviceInfo */
1402 dev = usbdevice_create(devname);
1403 if (dev)
1404 goto done;
1406 /* the other ones */
1407 #ifndef CONFIG_LINUX
1408 /* only the linux version is qdev-ified, usb-bsd still needs this */
1409 if (strstart(devname, "host:", &p)) {
1410 dev = usb_host_device_open(usb_bus_find(-1), p);
1412 #endif
1413 if (!dev)
1414 return -1;
1416 done:
1417 return 0;
1420 static int usb_device_del(const char *devname)
1422 int bus_num, addr;
1423 const char *p;
1425 if (strstart(devname, "host:", &p)) {
1426 return -1;
1429 if (!usb_enabled()) {
1430 return -1;
1433 p = strchr(devname, '.');
1434 if (!p)
1435 return -1;
1436 bus_num = strtoul(devname, NULL, 0);
1437 addr = strtoul(p + 1, NULL, 0);
1439 return usb_device_delete_addr(bus_num, addr);
1442 static int usb_parse(const char *cmdline)
1444 int r;
1445 r = usb_device_add(cmdline);
1446 if (r < 0) {
1447 error_report("could not add USB device '%s'", cmdline);
1449 return r;
1452 void hmp_usb_add(Monitor *mon, const QDict *qdict)
1454 const char *devname = qdict_get_str(qdict, "devname");
1455 if (usb_device_add(devname) < 0) {
1456 error_report("could not add USB device '%s'", devname);
1460 void hmp_usb_del(Monitor *mon, const QDict *qdict)
1462 const char *devname = qdict_get_str(qdict, "devname");
1463 if (usb_device_del(devname) < 0) {
1464 error_report("could not delete USB device '%s'", devname);
1468 /***********************************************************/
1469 /* machine registration */
1471 MachineState *current_machine;
1473 static MachineClass *find_machine(const char *name)
1475 GSList *el, *machines = object_class_get_list(TYPE_MACHINE, false);
1476 MachineClass *mc = NULL;
1478 for (el = machines; el; el = el->next) {
1479 MachineClass *temp = el->data;
1481 if (!strcmp(temp->name, name)) {
1482 mc = temp;
1483 break;
1485 if (temp->alias &&
1486 !strcmp(temp->alias, name)) {
1487 mc = temp;
1488 break;
1492 g_slist_free(machines);
1493 return mc;
1496 MachineClass *find_default_machine(void)
1498 GSList *el, *machines = object_class_get_list(TYPE_MACHINE, false);
1499 MachineClass *mc = NULL;
1501 for (el = machines; el; el = el->next) {
1502 MachineClass *temp = el->data;
1504 if (temp->is_default) {
1505 mc = temp;
1506 break;
1510 g_slist_free(machines);
1511 return mc;
1514 MachineInfoList *qmp_query_machines(Error **errp)
1516 GSList *el, *machines = object_class_get_list(TYPE_MACHINE, false);
1517 MachineInfoList *mach_list = NULL;
1519 for (el = machines; el; el = el->next) {
1520 MachineClass *mc = el->data;
1521 MachineInfoList *entry;
1522 MachineInfo *info;
1524 info = g_malloc0(sizeof(*info));
1525 if (mc->is_default) {
1526 info->has_is_default = true;
1527 info->is_default = true;
1530 if (mc->alias) {
1531 info->has_alias = true;
1532 info->alias = g_strdup(mc->alias);
1535 info->name = g_strdup(mc->name);
1536 info->cpu_max = !mc->max_cpus ? 1 : mc->max_cpus;
1538 entry = g_malloc0(sizeof(*entry));
1539 entry->value = info;
1540 entry->next = mach_list;
1541 mach_list = entry;
1544 g_slist_free(machines);
1545 return mach_list;
1548 static int machine_help_func(QemuOpts *opts, MachineState *machine)
1550 ObjectProperty *prop;
1551 ObjectPropertyIterator iter;
1553 if (!qemu_opt_has_help_opt(opts)) {
1554 return 0;
1557 object_property_iter_init(&iter, OBJECT(machine));
1558 while ((prop = object_property_iter_next(&iter))) {
1559 if (!prop->set) {
1560 continue;
1563 error_printf("%s.%s=%s", MACHINE_GET_CLASS(machine)->name,
1564 prop->name, prop->type);
1565 if (prop->description) {
1566 error_printf(" (%s)\n", prop->description);
1567 } else {
1568 error_printf("\n");
1572 return 1;
1575 /***********************************************************/
1576 /* main execution loop */
1578 struct vm_change_state_entry {
1579 VMChangeStateHandler *cb;
1580 void *opaque;
1581 QLIST_ENTRY (vm_change_state_entry) entries;
1584 static QLIST_HEAD(vm_change_state_head, vm_change_state_entry) vm_change_state_head;
1586 VMChangeStateEntry *qemu_add_vm_change_state_handler(VMChangeStateHandler *cb,
1587 void *opaque)
1589 VMChangeStateEntry *e;
1591 e = g_malloc0(sizeof (*e));
1593 e->cb = cb;
1594 e->opaque = opaque;
1595 QLIST_INSERT_HEAD(&vm_change_state_head, e, entries);
1596 return e;
1599 void qemu_del_vm_change_state_handler(VMChangeStateEntry *e)
1601 QLIST_REMOVE (e, entries);
1602 g_free (e);
1605 void vm_state_notify(int running, RunState state)
1607 VMChangeStateEntry *e, *next;
1609 trace_vm_state_notify(running, state);
1611 QLIST_FOREACH_SAFE(e, &vm_change_state_head, entries, next) {
1612 e->cb(e->opaque, running, state);
1616 /* reset/shutdown handler */
1618 typedef struct QEMUResetEntry {
1619 QTAILQ_ENTRY(QEMUResetEntry) entry;
1620 QEMUResetHandler *func;
1621 void *opaque;
1622 } QEMUResetEntry;
1624 static QTAILQ_HEAD(reset_handlers, QEMUResetEntry) reset_handlers =
1625 QTAILQ_HEAD_INITIALIZER(reset_handlers);
1626 static int reset_requested;
1627 static int shutdown_requested, shutdown_signal = -1;
1628 static pid_t shutdown_pid;
1629 static int powerdown_requested;
1630 static int debug_requested;
1631 static int suspend_requested;
1632 static WakeupReason wakeup_reason;
1633 static NotifierList powerdown_notifiers =
1634 NOTIFIER_LIST_INITIALIZER(powerdown_notifiers);
1635 static NotifierList suspend_notifiers =
1636 NOTIFIER_LIST_INITIALIZER(suspend_notifiers);
1637 static NotifierList wakeup_notifiers =
1638 NOTIFIER_LIST_INITIALIZER(wakeup_notifiers);
1639 static uint32_t wakeup_reason_mask = ~(1 << QEMU_WAKEUP_REASON_NONE);
1641 int qemu_shutdown_requested_get(void)
1643 return shutdown_requested;
1646 int qemu_reset_requested_get(void)
1648 return reset_requested;
1651 static int qemu_shutdown_requested(void)
1653 return atomic_xchg(&shutdown_requested, 0);
1656 static void qemu_kill_report(void)
1658 if (!qtest_driver() && shutdown_signal != -1) {
1659 if (shutdown_pid == 0) {
1660 /* This happens for eg ^C at the terminal, so it's worth
1661 * avoiding printing an odd message in that case.
1663 error_report("terminating on signal %d", shutdown_signal);
1664 } else {
1665 error_report("terminating on signal %d from pid " FMT_pid,
1666 shutdown_signal, shutdown_pid);
1668 shutdown_signal = -1;
1672 static int qemu_reset_requested(void)
1674 int r = reset_requested;
1675 if (r && replay_checkpoint(CHECKPOINT_RESET_REQUESTED)) {
1676 reset_requested = 0;
1677 return r;
1679 return false;
1682 static int qemu_suspend_requested(void)
1684 int r = suspend_requested;
1685 if (r && replay_checkpoint(CHECKPOINT_SUSPEND_REQUESTED)) {
1686 suspend_requested = 0;
1687 return r;
1689 return false;
1692 static WakeupReason qemu_wakeup_requested(void)
1694 return wakeup_reason;
1697 static int qemu_powerdown_requested(void)
1699 int r = powerdown_requested;
1700 powerdown_requested = 0;
1701 return r;
1704 static int qemu_debug_requested(void)
1706 int r = debug_requested;
1707 debug_requested = 0;
1708 return r;
1711 void qemu_register_reset(QEMUResetHandler *func, void *opaque)
1713 QEMUResetEntry *re = g_malloc0(sizeof(QEMUResetEntry));
1715 re->func = func;
1716 re->opaque = opaque;
1717 QTAILQ_INSERT_TAIL(&reset_handlers, re, entry);
1720 void qemu_unregister_reset(QEMUResetHandler *func, void *opaque)
1722 QEMUResetEntry *re;
1724 QTAILQ_FOREACH(re, &reset_handlers, entry) {
1725 if (re->func == func && re->opaque == opaque) {
1726 QTAILQ_REMOVE(&reset_handlers, re, entry);
1727 g_free(re);
1728 return;
1733 void qemu_devices_reset(void)
1735 QEMUResetEntry *re, *nre;
1737 /* reset all devices */
1738 QTAILQ_FOREACH_SAFE(re, &reset_handlers, entry, nre) {
1739 re->func(re->opaque);
1743 void qemu_system_reset(bool report)
1745 MachineClass *mc;
1747 mc = current_machine ? MACHINE_GET_CLASS(current_machine) : NULL;
1749 cpu_synchronize_all_states();
1751 if (mc && mc->reset) {
1752 mc->reset();
1753 } else {
1754 qemu_devices_reset();
1756 if (report) {
1757 qapi_event_send_reset(&error_abort);
1759 cpu_synchronize_all_post_reset();
1762 void qemu_system_guest_panicked(void)
1764 if (current_cpu) {
1765 current_cpu->crash_occurred = true;
1767 qapi_event_send_guest_panicked(GUEST_PANIC_ACTION_PAUSE, &error_abort);
1768 vm_stop(RUN_STATE_GUEST_PANICKED);
1771 void qemu_system_reset_request(void)
1773 if (no_reboot) {
1774 shutdown_requested = 1;
1775 } else {
1776 reset_requested = 1;
1778 cpu_stop_current();
1779 qemu_notify_event();
1782 static void qemu_system_suspend(void)
1784 pause_all_vcpus();
1785 notifier_list_notify(&suspend_notifiers, NULL);
1786 runstate_set(RUN_STATE_SUSPENDED);
1787 qapi_event_send_suspend(&error_abort);
1790 void qemu_system_suspend_request(void)
1792 if (runstate_check(RUN_STATE_SUSPENDED)) {
1793 return;
1795 suspend_requested = 1;
1796 cpu_stop_current();
1797 qemu_notify_event();
1800 void qemu_register_suspend_notifier(Notifier *notifier)
1802 notifier_list_add(&suspend_notifiers, notifier);
1805 void qemu_system_wakeup_request(WakeupReason reason)
1807 trace_system_wakeup_request(reason);
1809 if (!runstate_check(RUN_STATE_SUSPENDED)) {
1810 return;
1812 if (!(wakeup_reason_mask & (1 << reason))) {
1813 return;
1815 runstate_set(RUN_STATE_RUNNING);
1816 wakeup_reason = reason;
1817 qemu_notify_event();
1820 void qemu_system_wakeup_enable(WakeupReason reason, bool enabled)
1822 if (enabled) {
1823 wakeup_reason_mask |= (1 << reason);
1824 } else {
1825 wakeup_reason_mask &= ~(1 << reason);
1829 void qemu_register_wakeup_notifier(Notifier *notifier)
1831 notifier_list_add(&wakeup_notifiers, notifier);
1834 void qemu_system_killed(int signal, pid_t pid)
1836 shutdown_signal = signal;
1837 shutdown_pid = pid;
1838 no_shutdown = 0;
1840 /* Cannot call qemu_system_shutdown_request directly because
1841 * we are in a signal handler.
1843 shutdown_requested = 1;
1844 qemu_notify_event();
1847 void qemu_system_shutdown_request(void)
1849 trace_qemu_system_shutdown_request();
1850 replay_shutdown_request();
1851 shutdown_requested = 1;
1852 qemu_notify_event();
1855 static void qemu_system_powerdown(void)
1857 qapi_event_send_powerdown(&error_abort);
1858 notifier_list_notify(&powerdown_notifiers, NULL);
1861 void qemu_system_powerdown_request(void)
1863 trace_qemu_system_powerdown_request();
1864 powerdown_requested = 1;
1865 qemu_notify_event();
1868 void qemu_register_powerdown_notifier(Notifier *notifier)
1870 notifier_list_add(&powerdown_notifiers, notifier);
1873 void qemu_system_debug_request(void)
1875 debug_requested = 1;
1876 qemu_notify_event();
1879 static bool main_loop_should_exit(void)
1881 RunState r;
1882 if (qemu_debug_requested()) {
1883 vm_stop(RUN_STATE_DEBUG);
1885 if (qemu_suspend_requested()) {
1886 qemu_system_suspend();
1888 if (qemu_shutdown_requested()) {
1889 qemu_kill_report();
1890 qapi_event_send_shutdown(&error_abort);
1891 if (no_shutdown) {
1892 vm_stop(RUN_STATE_SHUTDOWN);
1893 } else {
1894 return true;
1897 if (qemu_reset_requested()) {
1898 pause_all_vcpus();
1899 qemu_system_reset(VMRESET_REPORT);
1900 resume_all_vcpus();
1901 if (!runstate_check(RUN_STATE_RUNNING) &&
1902 !runstate_check(RUN_STATE_INMIGRATE)) {
1903 runstate_set(RUN_STATE_PRELAUNCH);
1906 if (qemu_wakeup_requested()) {
1907 pause_all_vcpus();
1908 qemu_system_reset(VMRESET_SILENT);
1909 notifier_list_notify(&wakeup_notifiers, &wakeup_reason);
1910 wakeup_reason = QEMU_WAKEUP_REASON_NONE;
1911 resume_all_vcpus();
1912 qapi_event_send_wakeup(&error_abort);
1914 if (qemu_powerdown_requested()) {
1915 qemu_system_powerdown();
1917 if (qemu_vmstop_requested(&r)) {
1918 vm_stop(r);
1920 return false;
1923 static void main_loop(void)
1925 bool nonblocking;
1926 int last_io = 0;
1927 #ifdef CONFIG_PROFILER
1928 int64_t ti;
1929 #endif
1930 do {
1931 nonblocking = !kvm_enabled() && !xen_enabled() && last_io > 0;
1932 #ifdef CONFIG_PROFILER
1933 ti = profile_getclock();
1934 #endif
1935 last_io = main_loop_wait(nonblocking);
1936 #ifdef CONFIG_PROFILER
1937 dev_time += profile_getclock() - ti;
1938 #endif
1939 } while (!main_loop_should_exit());
1942 static void version(void)
1944 printf("QEMU emulator version " QEMU_VERSION QEMU_PKGVERSION ", Copyright (c) 2003-2008 Fabrice Bellard\n");
1947 static void help(int exitcode)
1949 version();
1950 printf("usage: %s [options] [disk_image]\n\n"
1951 "'disk_image' is a raw hard disk image for IDE hard disk 0\n\n",
1952 error_get_progname());
1954 #define QEMU_OPTIONS_GENERATE_HELP
1955 #include "qemu-options-wrapper.h"
1957 printf("\nDuring emulation, the following keys are useful:\n"
1958 "ctrl-alt-f toggle full screen\n"
1959 "ctrl-alt-n switch to virtual console 'n'\n"
1960 "ctrl-alt toggle mouse and keyboard grab\n"
1961 "\n"
1962 "When using -nographic, press 'ctrl-a h' to get some help.\n");
1964 exit(exitcode);
1967 #define HAS_ARG 0x0001
1969 typedef struct QEMUOption {
1970 const char *name;
1971 int flags;
1972 int index;
1973 uint32_t arch_mask;
1974 } QEMUOption;
1976 static const QEMUOption qemu_options[] = {
1977 { "h", 0, QEMU_OPTION_h, QEMU_ARCH_ALL },
1978 #define QEMU_OPTIONS_GENERATE_OPTIONS
1979 #include "qemu-options-wrapper.h"
1980 { NULL },
1983 static bool vga_available(void)
1985 return object_class_by_name("VGA") || object_class_by_name("isa-vga");
1988 static bool cirrus_vga_available(void)
1990 return object_class_by_name("cirrus-vga")
1991 || object_class_by_name("isa-cirrus-vga");
1994 static bool vmware_vga_available(void)
1996 return object_class_by_name("vmware-svga");
1999 static bool qxl_vga_available(void)
2001 return object_class_by_name("qxl-vga");
2004 static bool tcx_vga_available(void)
2006 return object_class_by_name("SUNW,tcx");
2009 static bool cg3_vga_available(void)
2011 return object_class_by_name("cgthree");
2014 static bool virtio_vga_available(void)
2016 return object_class_by_name("virtio-vga");
2019 static void select_vgahw (const char *p)
2021 const char *opts;
2023 assert(vga_interface_type == VGA_NONE);
2024 if (strstart(p, "std", &opts)) {
2025 if (vga_available()) {
2026 vga_interface_type = VGA_STD;
2027 } else {
2028 error_report("standard VGA not available");
2029 exit(0);
2031 } else if (strstart(p, "cirrus", &opts)) {
2032 if (cirrus_vga_available()) {
2033 vga_interface_type = VGA_CIRRUS;
2034 } else {
2035 error_report("Cirrus VGA not available");
2036 exit(0);
2038 } else if (strstart(p, "vmware", &opts)) {
2039 if (vmware_vga_available()) {
2040 vga_interface_type = VGA_VMWARE;
2041 } else {
2042 error_report("VMWare SVGA not available");
2043 exit(0);
2045 } else if (strstart(p, "virtio", &opts)) {
2046 if (virtio_vga_available()) {
2047 vga_interface_type = VGA_VIRTIO;
2048 } else {
2049 error_report("Virtio VGA not available");
2050 exit(0);
2052 } else if (strstart(p, "xenfb", &opts)) {
2053 vga_interface_type = VGA_XENFB;
2054 } else if (strstart(p, "qxl", &opts)) {
2055 if (qxl_vga_available()) {
2056 vga_interface_type = VGA_QXL;
2057 } else {
2058 error_report("QXL VGA not available");
2059 exit(0);
2061 } else if (strstart(p, "tcx", &opts)) {
2062 if (tcx_vga_available()) {
2063 vga_interface_type = VGA_TCX;
2064 } else {
2065 error_report("TCX framebuffer not available");
2066 exit(0);
2068 } else if (strstart(p, "cg3", &opts)) {
2069 if (cg3_vga_available()) {
2070 vga_interface_type = VGA_CG3;
2071 } else {
2072 error_report("CG3 framebuffer not available");
2073 exit(0);
2075 } else if (!strstart(p, "none", &opts)) {
2076 invalid_vga:
2077 error_report("unknown vga type: %s", p);
2078 exit(1);
2080 while (*opts) {
2081 const char *nextopt;
2083 if (strstart(opts, ",retrace=", &nextopt)) {
2084 opts = nextopt;
2085 if (strstart(opts, "dumb", &nextopt))
2086 vga_retrace_method = VGA_RETRACE_DUMB;
2087 else if (strstart(opts, "precise", &nextopt))
2088 vga_retrace_method = VGA_RETRACE_PRECISE;
2089 else goto invalid_vga;
2090 } else goto invalid_vga;
2091 opts = nextopt;
2095 static DisplayType select_display(const char *p)
2097 const char *opts;
2098 DisplayType display = DT_DEFAULT;
2100 if (strstart(p, "sdl", &opts)) {
2101 #ifdef CONFIG_SDL
2102 display = DT_SDL;
2103 while (*opts) {
2104 const char *nextopt;
2106 if (strstart(opts, ",frame=", &nextopt)) {
2107 opts = nextopt;
2108 if (strstart(opts, "on", &nextopt)) {
2109 no_frame = 0;
2110 } else if (strstart(opts, "off", &nextopt)) {
2111 no_frame = 1;
2112 } else {
2113 goto invalid_sdl_args;
2115 } else if (strstart(opts, ",alt_grab=", &nextopt)) {
2116 opts = nextopt;
2117 if (strstart(opts, "on", &nextopt)) {
2118 alt_grab = 1;
2119 } else if (strstart(opts, "off", &nextopt)) {
2120 alt_grab = 0;
2121 } else {
2122 goto invalid_sdl_args;
2124 } else if (strstart(opts, ",ctrl_grab=", &nextopt)) {
2125 opts = nextopt;
2126 if (strstart(opts, "on", &nextopt)) {
2127 ctrl_grab = 1;
2128 } else if (strstart(opts, "off", &nextopt)) {
2129 ctrl_grab = 0;
2130 } else {
2131 goto invalid_sdl_args;
2133 } else if (strstart(opts, ",window_close=", &nextopt)) {
2134 opts = nextopt;
2135 if (strstart(opts, "on", &nextopt)) {
2136 no_quit = 0;
2137 } else if (strstart(opts, "off", &nextopt)) {
2138 no_quit = 1;
2139 } else {
2140 goto invalid_sdl_args;
2142 } else if (strstart(opts, ",gl=", &nextopt)) {
2143 opts = nextopt;
2144 if (strstart(opts, "on", &nextopt)) {
2145 request_opengl = 1;
2146 } else if (strstart(opts, "off", &nextopt)) {
2147 request_opengl = 0;
2148 } else {
2149 goto invalid_sdl_args;
2151 } else {
2152 invalid_sdl_args:
2153 error_report("invalid SDL option string");
2154 exit(1);
2156 opts = nextopt;
2158 #else
2159 error_report("SDL support is disabled");
2160 exit(1);
2161 #endif
2162 } else if (strstart(p, "vnc", &opts)) {
2163 #ifdef CONFIG_VNC
2164 if (*opts == '=') {
2165 Error *err = NULL;
2166 if (vnc_parse(opts + 1, &err) == NULL) {
2167 error_report_err(err);
2168 exit(1);
2170 } else {
2171 error_report("VNC requires a display argument vnc=<display>");
2172 exit(1);
2174 #else
2175 error_report("VNC support is disabled");
2176 exit(1);
2177 #endif
2178 } else if (strstart(p, "curses", &opts)) {
2179 #ifdef CONFIG_CURSES
2180 display = DT_CURSES;
2181 #else
2182 error_report("curses support is disabled");
2183 exit(1);
2184 #endif
2185 } else if (strstart(p, "gtk", &opts)) {
2186 #ifdef CONFIG_GTK
2187 display = DT_GTK;
2188 while (*opts) {
2189 const char *nextopt;
2191 if (strstart(opts, ",grab_on_hover=", &nextopt)) {
2192 opts = nextopt;
2193 if (strstart(opts, "on", &nextopt)) {
2194 grab_on_hover = true;
2195 } else if (strstart(opts, "off", &nextopt)) {
2196 grab_on_hover = false;
2197 } else {
2198 goto invalid_gtk_args;
2200 } else if (strstart(opts, ",gl=", &nextopt)) {
2201 opts = nextopt;
2202 if (strstart(opts, "on", &nextopt)) {
2203 request_opengl = 1;
2204 } else if (strstart(opts, "off", &nextopt)) {
2205 request_opengl = 0;
2206 } else {
2207 goto invalid_gtk_args;
2209 } else {
2210 invalid_gtk_args:
2211 error_report("invalid GTK option string");
2212 exit(1);
2214 opts = nextopt;
2216 #else
2217 error_report("GTK support is disabled");
2218 exit(1);
2219 #endif
2220 } else if (strstart(p, "none", &opts)) {
2221 display = DT_NONE;
2222 } else {
2223 error_report("unknown display type");
2224 exit(1);
2227 return display;
2230 static int balloon_parse(const char *arg)
2232 QemuOpts *opts;
2234 if (strcmp(arg, "none") == 0) {
2235 return 0;
2238 if (!strncmp(arg, "virtio", 6)) {
2239 if (arg[6] == ',') {
2240 /* have params -> parse them */
2241 opts = qemu_opts_parse_noisily(qemu_find_opts("device"), arg + 7,
2242 false);
2243 if (!opts)
2244 return -1;
2245 } else {
2246 /* create empty opts */
2247 opts = qemu_opts_create(qemu_find_opts("device"), NULL, 0,
2248 &error_abort);
2250 qemu_opt_set(opts, "driver", "virtio-balloon", &error_abort);
2251 return 0;
2254 return -1;
2257 char *qemu_find_file(int type, const char *name)
2259 int i;
2260 const char *subdir;
2261 char *buf;
2263 /* Try the name as a straight path first */
2264 if (access(name, R_OK) == 0) {
2265 trace_load_file(name, name);
2266 return g_strdup(name);
2269 switch (type) {
2270 case QEMU_FILE_TYPE_BIOS:
2271 subdir = "";
2272 break;
2273 case QEMU_FILE_TYPE_KEYMAP:
2274 subdir = "keymaps/";
2275 break;
2276 default:
2277 abort();
2280 for (i = 0; i < data_dir_idx; i++) {
2281 buf = g_strdup_printf("%s/%s%s", data_dir[i], subdir, name);
2282 if (access(buf, R_OK) == 0) {
2283 trace_load_file(name, buf);
2284 return buf;
2286 g_free(buf);
2288 return NULL;
2291 static inline bool nonempty_str(const char *str)
2293 return str && *str;
2296 static int parse_fw_cfg(void *opaque, QemuOpts *opts, Error **errp)
2298 gchar *buf;
2299 size_t size;
2300 const char *name, *file, *str;
2301 FWCfgState *fw_cfg = (FWCfgState *) opaque;
2303 if (fw_cfg == NULL) {
2304 error_report("fw_cfg device not available");
2305 return -1;
2307 name = qemu_opt_get(opts, "name");
2308 file = qemu_opt_get(opts, "file");
2309 str = qemu_opt_get(opts, "string");
2311 /* we need name and either a file or the content string */
2312 if (!(nonempty_str(name) && (nonempty_str(file) || nonempty_str(str)))) {
2313 error_report("invalid argument(s)");
2314 return -1;
2316 if (nonempty_str(file) && nonempty_str(str)) {
2317 error_report("file and string are mutually exclusive");
2318 return -1;
2320 if (strlen(name) > FW_CFG_MAX_FILE_PATH - 1) {
2321 error_report("name too long (max. %d char)", FW_CFG_MAX_FILE_PATH - 1);
2322 return -1;
2324 if (strncmp(name, "opt/", 4) != 0) {
2325 error_report("warning: externally provided fw_cfg item names "
2326 "should be prefixed with \"opt/\"");
2328 if (nonempty_str(str)) {
2329 size = strlen(str); /* NUL terminator NOT included in fw_cfg blob */
2330 buf = g_memdup(str, size);
2331 } else {
2332 if (!g_file_get_contents(file, &buf, &size, NULL)) {
2333 error_report("can't load %s", file);
2334 return -1;
2337 /* For legacy, keep user files in a specific global order. */
2338 fw_cfg_set_order_override(fw_cfg, FW_CFG_ORDER_OVERRIDE_USER);
2339 fw_cfg_add_file(fw_cfg, name, buf, size);
2340 fw_cfg_reset_order_override(fw_cfg);
2341 return 0;
2344 static int device_help_func(void *opaque, QemuOpts *opts, Error **errp)
2346 return qdev_device_help(opts);
2349 static int device_init_func(void *opaque, QemuOpts *opts, Error **errp)
2351 Error *err = NULL;
2352 DeviceState *dev;
2354 dev = qdev_device_add(opts, &err);
2355 if (!dev) {
2356 error_report_err(err);
2357 return -1;
2359 object_unref(OBJECT(dev));
2360 return 0;
2363 static int chardev_init_func(void *opaque, QemuOpts *opts, Error **errp)
2365 Error *local_err = NULL;
2367 qemu_chr_new_from_opts(opts, NULL, &local_err);
2368 if (local_err) {
2369 error_report_err(local_err);
2370 return -1;
2372 return 0;
2375 #ifdef CONFIG_VIRTFS
2376 static int fsdev_init_func(void *opaque, QemuOpts *opts, Error **errp)
2378 int ret;
2379 ret = qemu_fsdev_add(opts);
2381 return ret;
2383 #endif
2385 static int mon_init_func(void *opaque, QemuOpts *opts, Error **errp)
2387 CharDriverState *chr;
2388 const char *chardev;
2389 const char *mode;
2390 int flags;
2392 mode = qemu_opt_get(opts, "mode");
2393 if (mode == NULL) {
2394 mode = "readline";
2396 if (strcmp(mode, "readline") == 0) {
2397 flags = MONITOR_USE_READLINE;
2398 } else if (strcmp(mode, "control") == 0) {
2399 flags = MONITOR_USE_CONTROL;
2400 } else {
2401 error_report("unknown monitor mode \"%s\"", mode);
2402 exit(1);
2405 if (qemu_opt_get_bool(opts, "pretty", 0))
2406 flags |= MONITOR_USE_PRETTY;
2408 if (qemu_opt_get_bool(opts, "default", 0))
2409 flags |= MONITOR_IS_DEFAULT;
2411 chardev = qemu_opt_get(opts, "chardev");
2412 chr = qemu_chr_find(chardev);
2413 if (chr == NULL) {
2414 error_report("chardev \"%s\" not found", chardev);
2415 exit(1);
2418 qemu_chr_fe_claim_no_fail(chr);
2419 monitor_init(chr, flags);
2420 return 0;
2423 static void monitor_parse(const char *optarg, const char *mode, bool pretty)
2425 static int monitor_device_index = 0;
2426 Error *local_err = NULL;
2427 QemuOpts *opts;
2428 const char *p;
2429 char label[32];
2430 int def = 0;
2432 if (strstart(optarg, "chardev:", &p)) {
2433 snprintf(label, sizeof(label), "%s", p);
2434 } else {
2435 snprintf(label, sizeof(label), "compat_monitor%d",
2436 monitor_device_index);
2437 if (monitor_device_index == 0) {
2438 def = 1;
2440 opts = qemu_chr_parse_compat(label, optarg);
2441 if (!opts) {
2442 error_report("parse error: %s", optarg);
2443 exit(1);
2447 opts = qemu_opts_create(qemu_find_opts("mon"), label, 1, &local_err);
2448 if (!opts) {
2449 error_report_err(local_err);
2450 exit(1);
2452 qemu_opt_set(opts, "mode", mode, &error_abort);
2453 qemu_opt_set(opts, "chardev", label, &error_abort);
2454 qemu_opt_set_bool(opts, "pretty", pretty, &error_abort);
2455 if (def)
2456 qemu_opt_set(opts, "default", "on", &error_abort);
2457 monitor_device_index++;
2460 struct device_config {
2461 enum {
2462 DEV_USB, /* -usbdevice */
2463 DEV_BT, /* -bt */
2464 DEV_SERIAL, /* -serial */
2465 DEV_PARALLEL, /* -parallel */
2466 DEV_VIRTCON, /* -virtioconsole */
2467 DEV_DEBUGCON, /* -debugcon */
2468 DEV_GDB, /* -gdb, -s */
2469 DEV_SCLP, /* s390 sclp */
2470 } type;
2471 const char *cmdline;
2472 Location loc;
2473 QTAILQ_ENTRY(device_config) next;
2476 static QTAILQ_HEAD(, device_config) device_configs =
2477 QTAILQ_HEAD_INITIALIZER(device_configs);
2479 static void add_device_config(int type, const char *cmdline)
2481 struct device_config *conf;
2483 conf = g_malloc0(sizeof(*conf));
2484 conf->type = type;
2485 conf->cmdline = cmdline;
2486 loc_save(&conf->loc);
2487 QTAILQ_INSERT_TAIL(&device_configs, conf, next);
2490 static int foreach_device_config(int type, int (*func)(const char *cmdline))
2492 struct device_config *conf;
2493 int rc;
2495 QTAILQ_FOREACH(conf, &device_configs, next) {
2496 if (conf->type != type)
2497 continue;
2498 loc_push_restore(&conf->loc);
2499 rc = func(conf->cmdline);
2500 loc_pop(&conf->loc);
2501 if (rc) {
2502 return rc;
2505 return 0;
2508 static int serial_parse(const char *devname)
2510 static int index = 0;
2511 char label[32];
2513 if (strcmp(devname, "none") == 0)
2514 return 0;
2515 if (index == MAX_SERIAL_PORTS) {
2516 error_report("too many serial ports");
2517 exit(1);
2519 snprintf(label, sizeof(label), "serial%d", index);
2520 serial_hds[index] = qemu_chr_new(label, devname, NULL);
2521 if (!serial_hds[index]) {
2522 error_report("could not connect serial device"
2523 " to character backend '%s'", devname);
2524 return -1;
2526 index++;
2527 return 0;
2530 static int parallel_parse(const char *devname)
2532 static int index = 0;
2533 char label[32];
2535 if (strcmp(devname, "none") == 0)
2536 return 0;
2537 if (index == MAX_PARALLEL_PORTS) {
2538 error_report("too many parallel ports");
2539 exit(1);
2541 snprintf(label, sizeof(label), "parallel%d", index);
2542 parallel_hds[index] = qemu_chr_new(label, devname, NULL);
2543 if (!parallel_hds[index]) {
2544 error_report("could not connect parallel device"
2545 " to character backend '%s'", devname);
2546 return -1;
2548 index++;
2549 return 0;
2552 static int virtcon_parse(const char *devname)
2554 QemuOptsList *device = qemu_find_opts("device");
2555 static int index = 0;
2556 char label[32];
2557 QemuOpts *bus_opts, *dev_opts;
2559 if (strcmp(devname, "none") == 0)
2560 return 0;
2561 if (index == MAX_VIRTIO_CONSOLES) {
2562 error_report("too many virtio consoles");
2563 exit(1);
2566 bus_opts = qemu_opts_create(device, NULL, 0, &error_abort);
2567 qemu_opt_set(bus_opts, "driver", "virtio-serial", &error_abort);
2569 dev_opts = qemu_opts_create(device, NULL, 0, &error_abort);
2570 qemu_opt_set(dev_opts, "driver", "virtconsole", &error_abort);
2572 snprintf(label, sizeof(label), "virtcon%d", index);
2573 virtcon_hds[index] = qemu_chr_new(label, devname, NULL);
2574 if (!virtcon_hds[index]) {
2575 error_report("could not connect virtio console"
2576 " to character backend '%s'", devname);
2577 return -1;
2579 qemu_opt_set(dev_opts, "chardev", label, &error_abort);
2581 index++;
2582 return 0;
2585 static int sclp_parse(const char *devname)
2587 QemuOptsList *device = qemu_find_opts("device");
2588 static int index = 0;
2589 char label[32];
2590 QemuOpts *dev_opts;
2592 if (strcmp(devname, "none") == 0) {
2593 return 0;
2595 if (index == MAX_SCLP_CONSOLES) {
2596 error_report("too many sclp consoles");
2597 exit(1);
2600 assert(arch_type == QEMU_ARCH_S390X);
2602 dev_opts = qemu_opts_create(device, NULL, 0, NULL);
2603 qemu_opt_set(dev_opts, "driver", "sclpconsole", &error_abort);
2605 snprintf(label, sizeof(label), "sclpcon%d", index);
2606 sclp_hds[index] = qemu_chr_new(label, devname, NULL);
2607 if (!sclp_hds[index]) {
2608 error_report("could not connect sclp console"
2609 " to character backend '%s'", devname);
2610 return -1;
2612 qemu_opt_set(dev_opts, "chardev", label, &error_abort);
2614 index++;
2615 return 0;
2618 static int debugcon_parse(const char *devname)
2620 QemuOpts *opts;
2622 if (!qemu_chr_new("debugcon", devname, NULL)) {
2623 exit(1);
2625 opts = qemu_opts_create(qemu_find_opts("device"), "debugcon", 1, NULL);
2626 if (!opts) {
2627 error_report("already have a debugcon device");
2628 exit(1);
2630 qemu_opt_set(opts, "driver", "isa-debugcon", &error_abort);
2631 qemu_opt_set(opts, "chardev", "debugcon", &error_abort);
2632 return 0;
2635 static gint machine_class_cmp(gconstpointer a, gconstpointer b)
2637 const MachineClass *mc1 = a, *mc2 = b;
2638 int res;
2640 if (mc1->family == NULL) {
2641 if (mc2->family == NULL) {
2642 /* Compare standalone machine types against each other; they sort
2643 * in increasing order.
2645 return strcmp(object_class_get_name(OBJECT_CLASS(mc1)),
2646 object_class_get_name(OBJECT_CLASS(mc2)));
2649 /* Standalone machine types sort after families. */
2650 return 1;
2653 if (mc2->family == NULL) {
2654 /* Families sort before standalone machine types. */
2655 return -1;
2658 /* Families sort between each other alphabetically increasingly. */
2659 res = strcmp(mc1->family, mc2->family);
2660 if (res != 0) {
2661 return res;
2664 /* Within the same family, machine types sort in decreasing order. */
2665 return strcmp(object_class_get_name(OBJECT_CLASS(mc2)),
2666 object_class_get_name(OBJECT_CLASS(mc1)));
2669 static MachineClass *machine_parse(const char *name)
2671 MachineClass *mc = NULL;
2672 GSList *el, *machines = object_class_get_list(TYPE_MACHINE, false);
2674 if (name) {
2675 mc = find_machine(name);
2677 if (mc) {
2678 g_slist_free(machines);
2679 return mc;
2681 if (name && !is_help_option(name)) {
2682 error_report("unsupported machine type");
2683 error_printf("Use -machine help to list supported machines\n");
2684 } else {
2685 printf("Supported machines are:\n");
2686 machines = g_slist_sort(machines, machine_class_cmp);
2687 for (el = machines; el; el = el->next) {
2688 MachineClass *mc = el->data;
2689 if (mc->alias) {
2690 printf("%-20s %s (alias of %s)\n", mc->alias, mc->desc, mc->name);
2692 printf("%-20s %s%s\n", mc->name, mc->desc,
2693 mc->is_default ? " (default)" : "");
2697 g_slist_free(machines);
2698 exit(!name || !is_help_option(name));
2701 void qemu_add_exit_notifier(Notifier *notify)
2703 notifier_list_add(&exit_notifiers, notify);
2706 void qemu_remove_exit_notifier(Notifier *notify)
2708 notifier_remove(notify);
2711 static void qemu_run_exit_notifiers(void)
2713 notifier_list_notify(&exit_notifiers, NULL);
2716 static bool machine_init_done;
2718 void qemu_add_machine_init_done_notifier(Notifier *notify)
2720 notifier_list_add(&machine_init_done_notifiers, notify);
2721 if (machine_init_done) {
2722 notify->notify(notify, NULL);
2726 static void qemu_run_machine_init_done_notifiers(void)
2728 notifier_list_notify(&machine_init_done_notifiers, NULL);
2729 machine_init_done = true;
2732 static const QEMUOption *lookup_opt(int argc, char **argv,
2733 const char **poptarg, int *poptind)
2735 const QEMUOption *popt;
2736 int optind = *poptind;
2737 char *r = argv[optind];
2738 const char *optarg;
2740 loc_set_cmdline(argv, optind, 1);
2741 optind++;
2742 /* Treat --foo the same as -foo. */
2743 if (r[1] == '-')
2744 r++;
2745 popt = qemu_options;
2746 for(;;) {
2747 if (!popt->name) {
2748 error_report("invalid option");
2749 exit(1);
2751 if (!strcmp(popt->name, r + 1))
2752 break;
2753 popt++;
2755 if (popt->flags & HAS_ARG) {
2756 if (optind >= argc) {
2757 error_report("requires an argument");
2758 exit(1);
2760 optarg = argv[optind++];
2761 loc_set_cmdline(argv, optind - 2, 2);
2762 } else {
2763 optarg = NULL;
2766 *poptarg = optarg;
2767 *poptind = optind;
2769 return popt;
2772 static MachineClass *select_machine(void)
2774 MachineClass *machine_class = find_default_machine();
2775 const char *optarg;
2776 QemuOpts *opts;
2777 Location loc;
2779 loc_push_none(&loc);
2781 opts = qemu_get_machine_opts();
2782 qemu_opts_loc_restore(opts);
2784 optarg = qemu_opt_get(opts, "type");
2785 if (optarg) {
2786 machine_class = machine_parse(optarg);
2789 if (!machine_class) {
2790 error_report("No machine specified, and there is no default");
2791 error_printf("Use -machine help to list supported machines\n");
2792 exit(1);
2795 loc_pop(&loc);
2796 return machine_class;
2799 static int machine_set_property(void *opaque,
2800 const char *name, const char *value,
2801 Error **errp)
2803 Object *obj = OBJECT(opaque);
2804 Error *local_err = NULL;
2805 char *c, *qom_name;
2807 if (strcmp(name, "type") == 0) {
2808 return 0;
2811 qom_name = g_strdup(name);
2812 c = qom_name;
2813 while (*c++) {
2814 if (*c == '_') {
2815 *c = '-';
2819 object_property_parse(obj, value, qom_name, &local_err);
2820 g_free(qom_name);
2822 if (local_err) {
2823 error_report_err(local_err);
2824 return -1;
2827 return 0;
2832 * Initial object creation happens before all other
2833 * QEMU data types are created. The majority of objects
2834 * can be created at this point. The rng-egd object
2835 * cannot be created here, as it depends on the chardev
2836 * already existing.
2838 static bool object_create_initial(const char *type)
2840 if (g_str_equal(type, "rng-egd")) {
2841 return false;
2845 * return false for concrete netfilters since
2846 * they depend on netdevs already existing
2848 if (g_str_equal(type, "filter-buffer") ||
2849 g_str_equal(type, "filter-dump") ||
2850 g_str_equal(type, "filter-mirror") ||
2851 g_str_equal(type, "filter-redirector")) {
2852 return false;
2855 return true;
2860 * The remainder of object creation happens after the
2861 * creation of chardev, fsdev, net clients and device data types.
2863 static bool object_create_delayed(const char *type)
2865 return !object_create_initial(type);
2869 static void set_memory_options(uint64_t *ram_slots, ram_addr_t *maxram_size,
2870 MachineClass *mc)
2872 uint64_t sz;
2873 const char *mem_str;
2874 const char *maxmem_str, *slots_str;
2875 const ram_addr_t default_ram_size = mc->default_ram_size;
2876 QemuOpts *opts = qemu_find_opts_singleton("memory");
2877 Location loc;
2879 loc_push_none(&loc);
2880 qemu_opts_loc_restore(opts);
2882 sz = 0;
2883 mem_str = qemu_opt_get(opts, "size");
2884 if (mem_str) {
2885 if (!*mem_str) {
2886 error_report("missing 'size' option value");
2887 exit(EXIT_FAILURE);
2890 sz = qemu_opt_get_size(opts, "size", ram_size);
2892 /* Fix up legacy suffix-less format */
2893 if (g_ascii_isdigit(mem_str[strlen(mem_str) - 1])) {
2894 uint64_t overflow_check = sz;
2896 sz <<= 20;
2897 if ((sz >> 20) != overflow_check) {
2898 error_report("too large 'size' option value");
2899 exit(EXIT_FAILURE);
2904 /* backward compatibility behaviour for case "-m 0" */
2905 if (sz == 0) {
2906 sz = default_ram_size;
2909 sz = QEMU_ALIGN_UP(sz, 8192);
2910 ram_size = sz;
2911 if (ram_size != sz) {
2912 error_report("ram size too large");
2913 exit(EXIT_FAILURE);
2916 /* store value for the future use */
2917 qemu_opt_set_number(opts, "size", ram_size, &error_abort);
2918 *maxram_size = ram_size;
2920 maxmem_str = qemu_opt_get(opts, "maxmem");
2921 slots_str = qemu_opt_get(opts, "slots");
2922 if (maxmem_str && slots_str) {
2923 uint64_t slots;
2925 sz = qemu_opt_get_size(opts, "maxmem", 0);
2926 slots = qemu_opt_get_number(opts, "slots", 0);
2927 if (sz < ram_size) {
2928 error_report("invalid value of -m option maxmem: "
2929 "maximum memory size (0x%" PRIx64 ") must be at least "
2930 "the initial memory size (0x" RAM_ADDR_FMT ")",
2931 sz, ram_size);
2932 exit(EXIT_FAILURE);
2933 } else if (sz > ram_size) {
2934 if (!slots) {
2935 error_report("invalid value of -m option: maxmem was "
2936 "specified, but no hotplug slots were specified");
2937 exit(EXIT_FAILURE);
2939 } else if (slots) {
2940 error_report("invalid value of -m option maxmem: "
2941 "memory slots were specified but maximum memory size "
2942 "(0x%" PRIx64 ") is equal to the initial memory size "
2943 "(0x" RAM_ADDR_FMT ")", sz, ram_size);
2944 exit(EXIT_FAILURE);
2947 *maxram_size = sz;
2948 *ram_slots = slots;
2949 } else if ((!maxmem_str && slots_str) ||
2950 (maxmem_str && !slots_str)) {
2951 error_report("invalid -m option value: missing "
2952 "'%s' option", slots_str ? "maxmem" : "slots");
2953 exit(EXIT_FAILURE);
2956 loc_pop(&loc);
2959 int main(int argc, char **argv, char **envp)
2961 int i;
2962 int snapshot, linux_boot;
2963 const char *initrd_filename;
2964 const char *kernel_filename, *kernel_cmdline;
2965 const char *boot_order = NULL;
2966 const char *boot_once = NULL;
2967 DisplayState *ds;
2968 int cyls, heads, secs, translation;
2969 QemuOpts *hda_opts = NULL, *opts, *machine_opts, *icount_opts = NULL;
2970 QemuOptsList *olist;
2971 int optind;
2972 const char *optarg;
2973 const char *loadvm = NULL;
2974 MachineClass *machine_class;
2975 const char *cpu_model;
2976 const char *vga_model = NULL;
2977 const char *qtest_chrdev = NULL;
2978 const char *qtest_log = NULL;
2979 const char *pid_file = NULL;
2980 const char *incoming = NULL;
2981 #ifdef CONFIG_VNC
2982 int show_vnc_port = 0;
2983 #endif
2984 bool defconfig = true;
2985 bool userconfig = true;
2986 const char *log_mask = NULL;
2987 const char *log_file = NULL;
2988 char *trace_file = NULL;
2989 ram_addr_t maxram_size;
2990 uint64_t ram_slots = 0;
2991 FILE *vmstate_dump_file = NULL;
2992 Error *main_loop_err = NULL;
2993 Error *err = NULL;
2995 qemu_init_cpu_loop();
2996 qemu_mutex_lock_iothread();
2998 atexit(qemu_run_exit_notifiers);
2999 error_set_progname(argv[0]);
3000 qemu_init_exec_dir(argv[0]);
3002 module_call_init(MODULE_INIT_QOM);
3004 qemu_add_opts(&qemu_drive_opts);
3005 qemu_add_drive_opts(&qemu_legacy_drive_opts);
3006 qemu_add_drive_opts(&qemu_common_drive_opts);
3007 qemu_add_drive_opts(&qemu_drive_opts);
3008 qemu_add_opts(&qemu_chardev_opts);
3009 qemu_add_opts(&qemu_device_opts);
3010 qemu_add_opts(&qemu_netdev_opts);
3011 qemu_add_opts(&qemu_net_opts);
3012 qemu_add_opts(&qemu_rtc_opts);
3013 qemu_add_opts(&qemu_global_opts);
3014 qemu_add_opts(&qemu_mon_opts);
3015 qemu_add_opts(&qemu_trace_opts);
3016 qemu_add_opts(&qemu_option_rom_opts);
3017 qemu_add_opts(&qemu_machine_opts);
3018 qemu_add_opts(&qemu_mem_opts);
3019 qemu_add_opts(&qemu_smp_opts);
3020 qemu_add_opts(&qemu_boot_opts);
3021 qemu_add_opts(&qemu_sandbox_opts);
3022 qemu_add_opts(&qemu_add_fd_opts);
3023 qemu_add_opts(&qemu_object_opts);
3024 qemu_add_opts(&qemu_tpmdev_opts);
3025 qemu_add_opts(&qemu_realtime_opts);
3026 qemu_add_opts(&qemu_msg_opts);
3027 qemu_add_opts(&qemu_name_opts);
3028 qemu_add_opts(&qemu_numa_opts);
3029 qemu_add_opts(&qemu_icount_opts);
3030 qemu_add_opts(&qemu_semihosting_config_opts);
3031 qemu_add_opts(&qemu_fw_cfg_opts);
3032 module_call_init(MODULE_INIT_OPTS);
3034 runstate_init();
3036 if (qcrypto_init(&err) < 0) {
3037 error_reportf_err(err, "cannot initialize crypto: ");
3038 exit(1);
3040 rtc_clock = QEMU_CLOCK_HOST;
3042 QLIST_INIT (&vm_change_state_head);
3043 os_setup_early_signal_handling();
3045 cpu_model = NULL;
3046 snapshot = 0;
3047 cyls = heads = secs = 0;
3048 translation = BIOS_ATA_TRANSLATION_AUTO;
3050 nb_nics = 0;
3052 bdrv_init_with_whitelist();
3054 autostart = 1;
3056 /* first pass of option parsing */
3057 optind = 1;
3058 while (optind < argc) {
3059 if (argv[optind][0] != '-') {
3060 /* disk image */
3061 optind++;
3062 } else {
3063 const QEMUOption *popt;
3065 popt = lookup_opt(argc, argv, &optarg, &optind);
3066 switch (popt->index) {
3067 case QEMU_OPTION_nodefconfig:
3068 defconfig = false;
3069 break;
3070 case QEMU_OPTION_nouserconfig:
3071 userconfig = false;
3072 break;
3077 if (defconfig) {
3078 int ret;
3079 ret = qemu_read_default_config_files(userconfig);
3080 if (ret < 0) {
3081 exit(1);
3085 /* second pass of option parsing */
3086 optind = 1;
3087 for(;;) {
3088 if (optind >= argc)
3089 break;
3090 if (argv[optind][0] != '-') {
3091 hda_opts = drive_add(IF_DEFAULT, 0, argv[optind++], HD_OPTS);
3092 } else {
3093 const QEMUOption *popt;
3095 popt = lookup_opt(argc, argv, &optarg, &optind);
3096 if (!(popt->arch_mask & arch_type)) {
3097 printf("Option %s not supported for this target\n", popt->name);
3098 exit(1);
3100 switch(popt->index) {
3101 case QEMU_OPTION_no_kvm_irqchip: {
3102 olist = qemu_find_opts("machine");
3103 qemu_opts_parse_noisily(olist, "kernel_irqchip=off", false);
3104 break;
3106 case QEMU_OPTION_cpu:
3107 /* hw initialization will check this */
3108 cpu_model = optarg;
3109 break;
3110 case QEMU_OPTION_hda:
3112 char buf[256];
3113 if (cyls == 0)
3114 snprintf(buf, sizeof(buf), "%s", HD_OPTS);
3115 else
3116 snprintf(buf, sizeof(buf),
3117 "%s,cyls=%d,heads=%d,secs=%d%s",
3118 HD_OPTS , cyls, heads, secs,
3119 translation == BIOS_ATA_TRANSLATION_LBA ?
3120 ",trans=lba" :
3121 translation == BIOS_ATA_TRANSLATION_NONE ?
3122 ",trans=none" : "");
3123 drive_add(IF_DEFAULT, 0, optarg, buf);
3124 break;
3126 case QEMU_OPTION_hdb:
3127 case QEMU_OPTION_hdc:
3128 case QEMU_OPTION_hdd:
3129 drive_add(IF_DEFAULT, popt->index - QEMU_OPTION_hda, optarg,
3130 HD_OPTS);
3131 break;
3132 case QEMU_OPTION_drive:
3133 if (drive_def(optarg) == NULL) {
3134 exit(1);
3136 break;
3137 case QEMU_OPTION_set:
3138 if (qemu_set_option(optarg) != 0)
3139 exit(1);
3140 break;
3141 case QEMU_OPTION_global:
3142 if (qemu_global_option(optarg) != 0)
3143 exit(1);
3144 break;
3145 case QEMU_OPTION_mtdblock:
3146 drive_add(IF_MTD, -1, optarg, MTD_OPTS);
3147 break;
3148 case QEMU_OPTION_sd:
3149 drive_add(IF_SD, -1, optarg, SD_OPTS);
3150 break;
3151 case QEMU_OPTION_pflash:
3152 drive_add(IF_PFLASH, -1, optarg, PFLASH_OPTS);
3153 break;
3154 case QEMU_OPTION_snapshot:
3155 snapshot = 1;
3156 break;
3157 case QEMU_OPTION_hdachs:
3159 const char *p;
3160 p = optarg;
3161 cyls = strtol(p, (char **)&p, 0);
3162 if (cyls < 1 || cyls > 16383)
3163 goto chs_fail;
3164 if (*p != ',')
3165 goto chs_fail;
3166 p++;
3167 heads = strtol(p, (char **)&p, 0);
3168 if (heads < 1 || heads > 16)
3169 goto chs_fail;
3170 if (*p != ',')
3171 goto chs_fail;
3172 p++;
3173 secs = strtol(p, (char **)&p, 0);
3174 if (secs < 1 || secs > 63)
3175 goto chs_fail;
3176 if (*p == ',') {
3177 p++;
3178 if (!strcmp(p, "large")) {
3179 translation = BIOS_ATA_TRANSLATION_LARGE;
3180 } else if (!strcmp(p, "rechs")) {
3181 translation = BIOS_ATA_TRANSLATION_RECHS;
3182 } else if (!strcmp(p, "none")) {
3183 translation = BIOS_ATA_TRANSLATION_NONE;
3184 } else if (!strcmp(p, "lba")) {
3185 translation = BIOS_ATA_TRANSLATION_LBA;
3186 } else if (!strcmp(p, "auto")) {
3187 translation = BIOS_ATA_TRANSLATION_AUTO;
3188 } else {
3189 goto chs_fail;
3191 } else if (*p != '\0') {
3192 chs_fail:
3193 error_report("invalid physical CHS format");
3194 exit(1);
3196 if (hda_opts != NULL) {
3197 qemu_opt_set_number(hda_opts, "cyls", cyls,
3198 &error_abort);
3199 qemu_opt_set_number(hda_opts, "heads", heads,
3200 &error_abort);
3201 qemu_opt_set_number(hda_opts, "secs", secs,
3202 &error_abort);
3203 if (translation == BIOS_ATA_TRANSLATION_LARGE) {
3204 qemu_opt_set(hda_opts, "trans", "large",
3205 &error_abort);
3206 } else if (translation == BIOS_ATA_TRANSLATION_RECHS) {
3207 qemu_opt_set(hda_opts, "trans", "rechs",
3208 &error_abort);
3209 } else if (translation == BIOS_ATA_TRANSLATION_LBA) {
3210 qemu_opt_set(hda_opts, "trans", "lba",
3211 &error_abort);
3212 } else if (translation == BIOS_ATA_TRANSLATION_NONE) {
3213 qemu_opt_set(hda_opts, "trans", "none",
3214 &error_abort);
3218 break;
3219 case QEMU_OPTION_numa:
3220 opts = qemu_opts_parse_noisily(qemu_find_opts("numa"),
3221 optarg, true);
3222 if (!opts) {
3223 exit(1);
3225 break;
3226 case QEMU_OPTION_display:
3227 display_type = select_display(optarg);
3228 break;
3229 case QEMU_OPTION_nographic:
3230 display_type = DT_NOGRAPHIC;
3231 break;
3232 case QEMU_OPTION_curses:
3233 #ifdef CONFIG_CURSES
3234 display_type = DT_CURSES;
3235 #else
3236 error_report("curses support is disabled");
3237 exit(1);
3238 #endif
3239 break;
3240 case QEMU_OPTION_portrait:
3241 graphic_rotate = 90;
3242 break;
3243 case QEMU_OPTION_rotate:
3244 graphic_rotate = strtol(optarg, (char **) &optarg, 10);
3245 if (graphic_rotate != 0 && graphic_rotate != 90 &&
3246 graphic_rotate != 180 && graphic_rotate != 270) {
3247 error_report("only 90, 180, 270 deg rotation is available");
3248 exit(1);
3250 break;
3251 case QEMU_OPTION_kernel:
3252 qemu_opts_set(qemu_find_opts("machine"), 0, "kernel", optarg,
3253 &error_abort);
3254 break;
3255 case QEMU_OPTION_initrd:
3256 qemu_opts_set(qemu_find_opts("machine"), 0, "initrd", optarg,
3257 &error_abort);
3258 break;
3259 case QEMU_OPTION_append:
3260 qemu_opts_set(qemu_find_opts("machine"), 0, "append", optarg,
3261 &error_abort);
3262 break;
3263 case QEMU_OPTION_dtb:
3264 qemu_opts_set(qemu_find_opts("machine"), 0, "dtb", optarg,
3265 &error_abort);
3266 break;
3267 case QEMU_OPTION_cdrom:
3268 drive_add(IF_DEFAULT, 2, optarg, CDROM_OPTS);
3269 break;
3270 case QEMU_OPTION_boot:
3271 opts = qemu_opts_parse_noisily(qemu_find_opts("boot-opts"),
3272 optarg, true);
3273 if (!opts) {
3274 exit(1);
3276 break;
3277 case QEMU_OPTION_fda:
3278 case QEMU_OPTION_fdb:
3279 drive_add(IF_FLOPPY, popt->index - QEMU_OPTION_fda,
3280 optarg, FD_OPTS);
3281 break;
3282 case QEMU_OPTION_no_fd_bootchk:
3283 fd_bootchk = 0;
3284 break;
3285 case QEMU_OPTION_netdev:
3286 if (net_client_parse(qemu_find_opts("netdev"), optarg) == -1) {
3287 exit(1);
3289 break;
3290 case QEMU_OPTION_net:
3291 if (net_client_parse(qemu_find_opts("net"), optarg) == -1) {
3292 exit(1);
3294 break;
3295 #ifdef CONFIG_LIBISCSI
3296 case QEMU_OPTION_iscsi:
3297 opts = qemu_opts_parse_noisily(qemu_find_opts("iscsi"),
3298 optarg, false);
3299 if (!opts) {
3300 exit(1);
3302 break;
3303 #endif
3304 #ifdef CONFIG_SLIRP
3305 case QEMU_OPTION_tftp:
3306 error_report("The -tftp option is deprecated. "
3307 "Please use '-netdev user,tftp=...' instead.");
3308 legacy_tftp_prefix = optarg;
3309 break;
3310 case QEMU_OPTION_bootp:
3311 error_report("The -bootp option is deprecated. "
3312 "Please use '-netdev user,bootfile=...' instead.");
3313 legacy_bootp_filename = optarg;
3314 break;
3315 case QEMU_OPTION_redir:
3316 error_report("The -redir option is deprecated. "
3317 "Please use '-netdev user,hostfwd=...' instead.");
3318 if (net_slirp_redir(optarg) < 0)
3319 exit(1);
3320 break;
3321 #endif
3322 case QEMU_OPTION_bt:
3323 add_device_config(DEV_BT, optarg);
3324 break;
3325 case QEMU_OPTION_audio_help:
3326 AUD_help ();
3327 exit (0);
3328 break;
3329 case QEMU_OPTION_soundhw:
3330 select_soundhw (optarg);
3331 break;
3332 case QEMU_OPTION_h:
3333 help(0);
3334 break;
3335 case QEMU_OPTION_version:
3336 version();
3337 exit(0);
3338 break;
3339 case QEMU_OPTION_m:
3340 opts = qemu_opts_parse_noisily(qemu_find_opts("memory"),
3341 optarg, true);
3342 if (!opts) {
3343 exit(EXIT_FAILURE);
3345 break;
3346 #ifdef CONFIG_TPM
3347 case QEMU_OPTION_tpmdev:
3348 if (tpm_config_parse(qemu_find_opts("tpmdev"), optarg) < 0) {
3349 exit(1);
3351 break;
3352 #endif
3353 case QEMU_OPTION_mempath:
3354 mem_path = optarg;
3355 break;
3356 case QEMU_OPTION_mem_prealloc:
3357 mem_prealloc = 1;
3358 break;
3359 case QEMU_OPTION_d:
3360 log_mask = optarg;
3361 break;
3362 case QEMU_OPTION_D:
3363 log_file = optarg;
3364 break;
3365 case QEMU_OPTION_DFILTER:
3366 qemu_set_dfilter_ranges(optarg);
3367 break;
3368 case QEMU_OPTION_s:
3369 add_device_config(DEV_GDB, "tcp::" DEFAULT_GDBSTUB_PORT);
3370 break;
3371 case QEMU_OPTION_gdb:
3372 add_device_config(DEV_GDB, optarg);
3373 break;
3374 case QEMU_OPTION_L:
3375 if (data_dir_idx < ARRAY_SIZE(data_dir)) {
3376 data_dir[data_dir_idx++] = optarg;
3378 break;
3379 case QEMU_OPTION_bios:
3380 qemu_opts_set(qemu_find_opts("machine"), 0, "firmware", optarg,
3381 &error_abort);
3382 break;
3383 case QEMU_OPTION_singlestep:
3384 singlestep = 1;
3385 break;
3386 case QEMU_OPTION_S:
3387 autostart = 0;
3388 break;
3389 case QEMU_OPTION_k:
3390 keyboard_layout = optarg;
3391 break;
3392 case QEMU_OPTION_localtime:
3393 rtc_utc = 0;
3394 break;
3395 case QEMU_OPTION_vga:
3396 vga_model = optarg;
3397 default_vga = 0;
3398 break;
3399 case QEMU_OPTION_g:
3401 const char *p;
3402 int w, h, depth;
3403 p = optarg;
3404 w = strtol(p, (char **)&p, 10);
3405 if (w <= 0) {
3406 graphic_error:
3407 error_report("invalid resolution or depth");
3408 exit(1);
3410 if (*p != 'x')
3411 goto graphic_error;
3412 p++;
3413 h = strtol(p, (char **)&p, 10);
3414 if (h <= 0)
3415 goto graphic_error;
3416 if (*p == 'x') {
3417 p++;
3418 depth = strtol(p, (char **)&p, 10);
3419 if (depth != 8 && depth != 15 && depth != 16 &&
3420 depth != 24 && depth != 32)
3421 goto graphic_error;
3422 } else if (*p == '\0') {
3423 depth = graphic_depth;
3424 } else {
3425 goto graphic_error;
3428 graphic_width = w;
3429 graphic_height = h;
3430 graphic_depth = depth;
3432 break;
3433 case QEMU_OPTION_echr:
3435 char *r;
3436 term_escape_char = strtol(optarg, &r, 0);
3437 if (r == optarg)
3438 printf("Bad argument to echr\n");
3439 break;
3441 case QEMU_OPTION_monitor:
3442 default_monitor = 0;
3443 if (strncmp(optarg, "none", 4)) {
3444 monitor_parse(optarg, "readline", false);
3446 break;
3447 case QEMU_OPTION_qmp:
3448 monitor_parse(optarg, "control", false);
3449 default_monitor = 0;
3450 break;
3451 case QEMU_OPTION_qmp_pretty:
3452 monitor_parse(optarg, "control", true);
3453 default_monitor = 0;
3454 break;
3455 case QEMU_OPTION_mon:
3456 opts = qemu_opts_parse_noisily(qemu_find_opts("mon"), optarg,
3457 true);
3458 if (!opts) {
3459 exit(1);
3461 default_monitor = 0;
3462 break;
3463 case QEMU_OPTION_chardev:
3464 opts = qemu_opts_parse_noisily(qemu_find_opts("chardev"),
3465 optarg, true);
3466 if (!opts) {
3467 exit(1);
3469 break;
3470 case QEMU_OPTION_fsdev:
3471 olist = qemu_find_opts("fsdev");
3472 if (!olist) {
3473 error_report("fsdev support is disabled");
3474 exit(1);
3476 opts = qemu_opts_parse_noisily(olist, optarg, true);
3477 if (!opts) {
3478 exit(1);
3480 break;
3481 case QEMU_OPTION_virtfs: {
3482 QemuOpts *fsdev;
3483 QemuOpts *device;
3484 const char *writeout, *sock_fd, *socket;
3486 olist = qemu_find_opts("virtfs");
3487 if (!olist) {
3488 error_report("virtfs support is disabled");
3489 exit(1);
3491 opts = qemu_opts_parse_noisily(olist, optarg, true);
3492 if (!opts) {
3493 exit(1);
3496 if (qemu_opt_get(opts, "fsdriver") == NULL ||
3497 qemu_opt_get(opts, "mount_tag") == NULL) {
3498 error_report("Usage: -virtfs fsdriver,mount_tag=tag");
3499 exit(1);
3501 fsdev = qemu_opts_create(qemu_find_opts("fsdev"),
3502 qemu_opt_get(opts, "mount_tag"),
3503 1, NULL);
3504 if (!fsdev) {
3505 error_report("duplicate fsdev id: %s",
3506 qemu_opt_get(opts, "mount_tag"));
3507 exit(1);
3510 writeout = qemu_opt_get(opts, "writeout");
3511 if (writeout) {
3512 #ifdef CONFIG_SYNC_FILE_RANGE
3513 qemu_opt_set(fsdev, "writeout", writeout, &error_abort);
3514 #else
3515 error_report("writeout=immediate not supported "
3516 "on this platform");
3517 exit(1);
3518 #endif
3520 qemu_opt_set(fsdev, "fsdriver",
3521 qemu_opt_get(opts, "fsdriver"), &error_abort);
3522 qemu_opt_set(fsdev, "path", qemu_opt_get(opts, "path"),
3523 &error_abort);
3524 qemu_opt_set(fsdev, "security_model",
3525 qemu_opt_get(opts, "security_model"),
3526 &error_abort);
3527 socket = qemu_opt_get(opts, "socket");
3528 if (socket) {
3529 qemu_opt_set(fsdev, "socket", socket, &error_abort);
3531 sock_fd = qemu_opt_get(opts, "sock_fd");
3532 if (sock_fd) {
3533 qemu_opt_set(fsdev, "sock_fd", sock_fd, &error_abort);
3536 qemu_opt_set_bool(fsdev, "readonly",
3537 qemu_opt_get_bool(opts, "readonly", 0),
3538 &error_abort);
3539 device = qemu_opts_create(qemu_find_opts("device"), NULL, 0,
3540 &error_abort);
3541 qemu_opt_set(device, "driver", "virtio-9p-pci", &error_abort);
3542 qemu_opt_set(device, "fsdev",
3543 qemu_opt_get(opts, "mount_tag"), &error_abort);
3544 qemu_opt_set(device, "mount_tag",
3545 qemu_opt_get(opts, "mount_tag"), &error_abort);
3546 break;
3548 case QEMU_OPTION_virtfs_synth: {
3549 QemuOpts *fsdev;
3550 QemuOpts *device;
3552 fsdev = qemu_opts_create(qemu_find_opts("fsdev"), "v_synth",
3553 1, NULL);
3554 if (!fsdev) {
3555 error_report("duplicate option: %s", "virtfs_synth");
3556 exit(1);
3558 qemu_opt_set(fsdev, "fsdriver", "synth", &error_abort);
3560 device = qemu_opts_create(qemu_find_opts("device"), NULL, 0,
3561 &error_abort);
3562 qemu_opt_set(device, "driver", "virtio-9p-pci", &error_abort);
3563 qemu_opt_set(device, "fsdev", "v_synth", &error_abort);
3564 qemu_opt_set(device, "mount_tag", "v_synth", &error_abort);
3565 break;
3567 case QEMU_OPTION_serial:
3568 add_device_config(DEV_SERIAL, optarg);
3569 default_serial = 0;
3570 if (strncmp(optarg, "mon:", 4) == 0) {
3571 default_monitor = 0;
3573 break;
3574 case QEMU_OPTION_watchdog:
3575 if (watchdog) {
3576 error_report("only one watchdog option may be given");
3577 return 1;
3579 watchdog = optarg;
3580 break;
3581 case QEMU_OPTION_watchdog_action:
3582 if (select_watchdog_action(optarg) == -1) {
3583 error_report("unknown -watchdog-action parameter");
3584 exit(1);
3586 break;
3587 case QEMU_OPTION_virtiocon:
3588 add_device_config(DEV_VIRTCON, optarg);
3589 default_virtcon = 0;
3590 if (strncmp(optarg, "mon:", 4) == 0) {
3591 default_monitor = 0;
3593 break;
3594 case QEMU_OPTION_parallel:
3595 add_device_config(DEV_PARALLEL, optarg);
3596 default_parallel = 0;
3597 if (strncmp(optarg, "mon:", 4) == 0) {
3598 default_monitor = 0;
3600 break;
3601 case QEMU_OPTION_debugcon:
3602 add_device_config(DEV_DEBUGCON, optarg);
3603 break;
3604 case QEMU_OPTION_loadvm:
3605 loadvm = optarg;
3606 break;
3607 case QEMU_OPTION_full_screen:
3608 full_screen = 1;
3609 break;
3610 case QEMU_OPTION_no_frame:
3611 no_frame = 1;
3612 break;
3613 case QEMU_OPTION_alt_grab:
3614 alt_grab = 1;
3615 break;
3616 case QEMU_OPTION_ctrl_grab:
3617 ctrl_grab = 1;
3618 break;
3619 case QEMU_OPTION_no_quit:
3620 no_quit = 1;
3621 break;
3622 case QEMU_OPTION_sdl:
3623 #ifdef CONFIG_SDL
3624 display_type = DT_SDL;
3625 break;
3626 #else
3627 error_report("SDL support is disabled");
3628 exit(1);
3629 #endif
3630 case QEMU_OPTION_pidfile:
3631 pid_file = optarg;
3632 break;
3633 case QEMU_OPTION_win2k_hack:
3634 win2k_install_hack = 1;
3635 break;
3636 case QEMU_OPTION_rtc_td_hack: {
3637 static GlobalProperty slew_lost_ticks[] = {
3639 .driver = "mc146818rtc",
3640 .property = "lost_tick_policy",
3641 .value = "slew",
3643 { /* end of list */ }
3646 qdev_prop_register_global_list(slew_lost_ticks);
3647 break;
3649 case QEMU_OPTION_acpitable:
3650 opts = qemu_opts_parse_noisily(qemu_find_opts("acpi"),
3651 optarg, true);
3652 if (!opts) {
3653 exit(1);
3655 do_acpitable_option(opts);
3656 break;
3657 case QEMU_OPTION_smbios:
3658 opts = qemu_opts_parse_noisily(qemu_find_opts("smbios"),
3659 optarg, false);
3660 if (!opts) {
3661 exit(1);
3663 do_smbios_option(opts);
3664 break;
3665 case QEMU_OPTION_fwcfg:
3666 opts = qemu_opts_parse_noisily(qemu_find_opts("fw_cfg"),
3667 optarg, true);
3668 if (opts == NULL) {
3669 exit(1);
3671 break;
3672 case QEMU_OPTION_enable_kvm:
3673 olist = qemu_find_opts("machine");
3674 qemu_opts_parse_noisily(olist, "accel=kvm", false);
3675 break;
3676 case QEMU_OPTION_M:
3677 case QEMU_OPTION_machine:
3678 olist = qemu_find_opts("machine");
3679 opts = qemu_opts_parse_noisily(olist, optarg, true);
3680 if (!opts) {
3681 exit(1);
3683 break;
3684 case QEMU_OPTION_no_kvm:
3685 olist = qemu_find_opts("machine");
3686 qemu_opts_parse_noisily(olist, "accel=tcg", false);
3687 break;
3688 case QEMU_OPTION_no_kvm_pit: {
3689 error_report("warning: ignoring deprecated option");
3690 break;
3692 case QEMU_OPTION_no_kvm_pit_reinjection: {
3693 static GlobalProperty kvm_pit_lost_tick_policy[] = {
3695 .driver = "kvm-pit",
3696 .property = "lost_tick_policy",
3697 .value = "discard",
3699 { /* end of list */ }
3702 error_report("warning: deprecated, replaced by "
3703 "-global kvm-pit.lost_tick_policy=discard");
3704 qdev_prop_register_global_list(kvm_pit_lost_tick_policy);
3705 break;
3707 case QEMU_OPTION_usb:
3708 olist = qemu_find_opts("machine");
3709 qemu_opts_parse_noisily(olist, "usb=on", false);
3710 break;
3711 case QEMU_OPTION_usbdevice:
3712 olist = qemu_find_opts("machine");
3713 qemu_opts_parse_noisily(olist, "usb=on", false);
3714 add_device_config(DEV_USB, optarg);
3715 break;
3716 case QEMU_OPTION_device:
3717 if (!qemu_opts_parse_noisily(qemu_find_opts("device"),
3718 optarg, true)) {
3719 exit(1);
3721 break;
3722 case QEMU_OPTION_smp:
3723 if (!qemu_opts_parse_noisily(qemu_find_opts("smp-opts"),
3724 optarg, true)) {
3725 exit(1);
3727 break;
3728 case QEMU_OPTION_vnc:
3730 #ifdef CONFIG_VNC
3731 Error *local_err = NULL;
3733 if (vnc_parse(optarg, &local_err) == NULL) {
3734 error_report_err(local_err);
3735 exit(1);
3737 #else
3738 error_report("VNC support is disabled");
3739 exit(1);
3740 #endif
3741 break;
3743 case QEMU_OPTION_no_acpi:
3744 acpi_enabled = 0;
3745 break;
3746 case QEMU_OPTION_no_hpet:
3747 no_hpet = 1;
3748 break;
3749 case QEMU_OPTION_balloon:
3750 if (balloon_parse(optarg) < 0) {
3751 error_report("unknown -balloon argument %s", optarg);
3752 exit(1);
3754 break;
3755 case QEMU_OPTION_no_reboot:
3756 no_reboot = 1;
3757 break;
3758 case QEMU_OPTION_no_shutdown:
3759 no_shutdown = 1;
3760 break;
3761 case QEMU_OPTION_show_cursor:
3762 cursor_hide = 0;
3763 break;
3764 case QEMU_OPTION_uuid:
3765 if(qemu_uuid_parse(optarg, qemu_uuid) < 0) {
3766 error_report("failed to parse UUID string: wrong format");
3767 exit(1);
3769 qemu_uuid_set = true;
3770 break;
3771 case QEMU_OPTION_option_rom:
3772 if (nb_option_roms >= MAX_OPTION_ROMS) {
3773 error_report("too many option ROMs");
3774 exit(1);
3776 opts = qemu_opts_parse_noisily(qemu_find_opts("option-rom"),
3777 optarg, true);
3778 if (!opts) {
3779 exit(1);
3781 option_rom[nb_option_roms].name = qemu_opt_get(opts, "romfile");
3782 option_rom[nb_option_roms].bootindex =
3783 qemu_opt_get_number(opts, "bootindex", -1);
3784 if (!option_rom[nb_option_roms].name) {
3785 error_report("Option ROM file is not specified");
3786 exit(1);
3788 nb_option_roms++;
3789 break;
3790 case QEMU_OPTION_semihosting:
3791 semihosting.enabled = true;
3792 semihosting.target = SEMIHOSTING_TARGET_AUTO;
3793 break;
3794 case QEMU_OPTION_semihosting_config:
3795 semihosting.enabled = true;
3796 opts = qemu_opts_parse_noisily(qemu_find_opts("semihosting-config"),
3797 optarg, false);
3798 if (opts != NULL) {
3799 semihosting.enabled = qemu_opt_get_bool(opts, "enable",
3800 true);
3801 const char *target = qemu_opt_get(opts, "target");
3802 if (target != NULL) {
3803 if (strcmp("native", target) == 0) {
3804 semihosting.target = SEMIHOSTING_TARGET_NATIVE;
3805 } else if (strcmp("gdb", target) == 0) {
3806 semihosting.target = SEMIHOSTING_TARGET_GDB;
3807 } else if (strcmp("auto", target) == 0) {
3808 semihosting.target = SEMIHOSTING_TARGET_AUTO;
3809 } else {
3810 error_report("unsupported semihosting-config %s",
3811 optarg);
3812 exit(1);
3814 } else {
3815 semihosting.target = SEMIHOSTING_TARGET_AUTO;
3817 /* Set semihosting argument count and vector */
3818 qemu_opt_foreach(opts, add_semihosting_arg,
3819 &semihosting, NULL);
3820 } else {
3821 error_report("unsupported semihosting-config %s", optarg);
3822 exit(1);
3824 break;
3825 case QEMU_OPTION_tdf:
3826 error_report("warning: ignoring deprecated option");
3827 break;
3828 case QEMU_OPTION_name:
3829 opts = qemu_opts_parse_noisily(qemu_find_opts("name"),
3830 optarg, true);
3831 if (!opts) {
3832 exit(1);
3834 break;
3835 case QEMU_OPTION_prom_env:
3836 if (nb_prom_envs >= MAX_PROM_ENVS) {
3837 error_report("too many prom variables");
3838 exit(1);
3840 prom_envs[nb_prom_envs] = optarg;
3841 nb_prom_envs++;
3842 break;
3843 case QEMU_OPTION_old_param:
3844 old_param = 1;
3845 break;
3846 case QEMU_OPTION_clock:
3847 /* Clock options no longer exist. Keep this option for
3848 * backward compatibility.
3850 break;
3851 case QEMU_OPTION_startdate:
3852 configure_rtc_date_offset(optarg, 1);
3853 break;
3854 case QEMU_OPTION_rtc:
3855 opts = qemu_opts_parse_noisily(qemu_find_opts("rtc"), optarg,
3856 false);
3857 if (!opts) {
3858 exit(1);
3860 configure_rtc(opts);
3861 break;
3862 case QEMU_OPTION_tb_size:
3863 tcg_tb_size = strtol(optarg, NULL, 0);
3864 if (tcg_tb_size < 0) {
3865 tcg_tb_size = 0;
3867 break;
3868 case QEMU_OPTION_icount:
3869 icount_opts = qemu_opts_parse_noisily(qemu_find_opts("icount"),
3870 optarg, true);
3871 if (!icount_opts) {
3872 exit(1);
3874 break;
3875 case QEMU_OPTION_incoming:
3876 if (!incoming) {
3877 runstate_set(RUN_STATE_INMIGRATE);
3879 incoming = optarg;
3880 break;
3881 case QEMU_OPTION_nodefaults:
3882 has_defaults = 0;
3883 break;
3884 case QEMU_OPTION_xen_domid:
3885 if (!(xen_available())) {
3886 printf("Option %s not supported for this target\n", popt->name);
3887 exit(1);
3889 xen_domid = atoi(optarg);
3890 break;
3891 case QEMU_OPTION_xen_create:
3892 if (!(xen_available())) {
3893 printf("Option %s not supported for this target\n", popt->name);
3894 exit(1);
3896 xen_mode = XEN_CREATE;
3897 break;
3898 case QEMU_OPTION_xen_attach:
3899 if (!(xen_available())) {
3900 printf("Option %s not supported for this target\n", popt->name);
3901 exit(1);
3903 xen_mode = XEN_ATTACH;
3904 break;
3905 case QEMU_OPTION_trace:
3907 opts = qemu_opts_parse_noisily(qemu_find_opts("trace"),
3908 optarg, true);
3909 if (!opts) {
3910 exit(1);
3912 if (qemu_opt_get(opts, "enable")) {
3913 trace_enable_events(qemu_opt_get(opts, "enable"));
3915 trace_init_events(qemu_opt_get(opts, "events"));
3916 if (trace_file) {
3917 g_free(trace_file);
3919 trace_file = g_strdup(qemu_opt_get(opts, "file"));
3920 qemu_opts_del(opts);
3921 break;
3923 case QEMU_OPTION_readconfig:
3925 int ret = qemu_read_config_file(optarg);
3926 if (ret < 0) {
3927 error_report("read config %s: %s", optarg,
3928 strerror(-ret));
3929 exit(1);
3931 break;
3933 case QEMU_OPTION_spice:
3934 olist = qemu_find_opts("spice");
3935 if (!olist) {
3936 error_report("spice support is disabled");
3937 exit(1);
3939 opts = qemu_opts_parse_noisily(olist, optarg, false);
3940 if (!opts) {
3941 exit(1);
3943 display_remote++;
3944 break;
3945 case QEMU_OPTION_writeconfig:
3947 FILE *fp;
3948 if (strcmp(optarg, "-") == 0) {
3949 fp = stdout;
3950 } else {
3951 fp = fopen(optarg, "w");
3952 if (fp == NULL) {
3953 error_report("open %s: %s", optarg,
3954 strerror(errno));
3955 exit(1);
3958 qemu_config_write(fp);
3959 if (fp != stdout) {
3960 fclose(fp);
3962 break;
3964 case QEMU_OPTION_qtest:
3965 qtest_chrdev = optarg;
3966 break;
3967 case QEMU_OPTION_qtest_log:
3968 qtest_log = optarg;
3969 break;
3970 case QEMU_OPTION_sandbox:
3971 opts = qemu_opts_parse_noisily(qemu_find_opts("sandbox"),
3972 optarg, true);
3973 if (!opts) {
3974 exit(1);
3976 break;
3977 case QEMU_OPTION_add_fd:
3978 #ifndef _WIN32
3979 opts = qemu_opts_parse_noisily(qemu_find_opts("add-fd"),
3980 optarg, false);
3981 if (!opts) {
3982 exit(1);
3984 #else
3985 error_report("File descriptor passing is disabled on this "
3986 "platform");
3987 exit(1);
3988 #endif
3989 break;
3990 case QEMU_OPTION_object:
3991 opts = qemu_opts_parse_noisily(qemu_find_opts("object"),
3992 optarg, true);
3993 if (!opts) {
3994 exit(1);
3996 break;
3997 case QEMU_OPTION_realtime:
3998 opts = qemu_opts_parse_noisily(qemu_find_opts("realtime"),
3999 optarg, false);
4000 if (!opts) {
4001 exit(1);
4003 enable_mlock = qemu_opt_get_bool(opts, "mlock", true);
4004 break;
4005 case QEMU_OPTION_msg:
4006 opts = qemu_opts_parse_noisily(qemu_find_opts("msg"), optarg,
4007 false);
4008 if (!opts) {
4009 exit(1);
4011 configure_msg(opts);
4012 break;
4013 case QEMU_OPTION_dump_vmstate:
4014 if (vmstate_dump_file) {
4015 error_report("only one '-dump-vmstate' "
4016 "option may be given");
4017 exit(1);
4019 vmstate_dump_file = fopen(optarg, "w");
4020 if (vmstate_dump_file == NULL) {
4021 error_report("open %s: %s", optarg, strerror(errno));
4022 exit(1);
4024 break;
4025 default:
4026 os_parse_cmd_args(popt->index, optarg);
4031 * Clear error location left behind by the loop.
4032 * Best done right after the loop. Do not insert code here!
4034 loc_set_none();
4036 replay_configure(icount_opts);
4038 machine_class = select_machine();
4040 set_memory_options(&ram_slots, &maxram_size, machine_class);
4042 os_daemonize();
4044 if (qemu_init_main_loop(&main_loop_err)) {
4045 error_report_err(main_loop_err);
4046 exit(1);
4049 if (qemu_opts_foreach(qemu_find_opts("sandbox"),
4050 parse_sandbox, NULL, NULL)) {
4051 exit(1);
4054 if (qemu_opts_foreach(qemu_find_opts("name"),
4055 parse_name, NULL, NULL)) {
4056 exit(1);
4059 #ifndef _WIN32
4060 if (qemu_opts_foreach(qemu_find_opts("add-fd"),
4061 parse_add_fd, NULL, NULL)) {
4062 exit(1);
4065 if (qemu_opts_foreach(qemu_find_opts("add-fd"),
4066 cleanup_add_fd, NULL, NULL)) {
4067 exit(1);
4069 #endif
4071 current_machine = MACHINE(object_new(object_class_get_name(
4072 OBJECT_CLASS(machine_class))));
4073 if (machine_help_func(qemu_get_machine_opts(), current_machine)) {
4074 exit(0);
4076 object_property_add_child(object_get_root(), "machine",
4077 OBJECT(current_machine), &error_abort);
4078 cpu_exec_init_all();
4080 if (machine_class->hw_version) {
4081 qemu_set_hw_version(machine_class->hw_version);
4084 /* Init CPU def lists, based on config
4085 * - Must be called after all the qemu_read_config_file() calls
4086 * - Must be called before list_cpus()
4087 * - Must be called before machine_class->init()
4089 cpudef_init();
4091 if (cpu_model && is_help_option(cpu_model)) {
4092 list_cpus(stdout, &fprintf, cpu_model);
4093 exit(0);
4096 if (!trace_init_backends()) {
4097 exit(1);
4099 trace_init_file(trace_file);
4101 /* Open the logfile at this point and set the log mask if necessary.
4103 if (log_file) {
4104 qemu_set_log_filename(log_file);
4107 if (log_mask) {
4108 int mask;
4109 mask = qemu_str_to_log_mask(log_mask);
4110 if (!mask) {
4111 qemu_print_log_usage(stdout);
4112 exit(1);
4114 qemu_set_log(mask);
4115 } else {
4116 qemu_set_log(0);
4119 /* If no data_dir is specified then try to find it relative to the
4120 executable path. */
4121 if (data_dir_idx < ARRAY_SIZE(data_dir)) {
4122 data_dir[data_dir_idx] = os_find_datadir();
4123 if (data_dir[data_dir_idx] != NULL) {
4124 data_dir_idx++;
4127 /* If all else fails use the install path specified when building. */
4128 if (data_dir_idx < ARRAY_SIZE(data_dir)) {
4129 data_dir[data_dir_idx++] = CONFIG_QEMU_DATADIR;
4132 smp_parse(qemu_opts_find(qemu_find_opts("smp-opts"), NULL));
4134 machine_class->max_cpus = machine_class->max_cpus ?: 1; /* Default to UP */
4135 if (max_cpus > machine_class->max_cpus) {
4136 error_report("Number of SMP CPUs requested (%d) exceeds max CPUs "
4137 "supported by machine '%s' (%d)", max_cpus,
4138 machine_class->name, machine_class->max_cpus);
4139 exit(1);
4143 * Get the default machine options from the machine if it is not already
4144 * specified either by the configuration file or by the command line.
4146 if (machine_class->default_machine_opts) {
4147 qemu_opts_set_defaults(qemu_find_opts("machine"),
4148 machine_class->default_machine_opts, 0);
4151 qemu_opts_foreach(qemu_find_opts("device"),
4152 default_driver_check, NULL, NULL);
4153 qemu_opts_foreach(qemu_find_opts("global"),
4154 default_driver_check, NULL, NULL);
4156 if (!vga_model && !default_vga) {
4157 vga_interface_type = VGA_DEVICE;
4159 if (!has_defaults || machine_class->no_serial) {
4160 default_serial = 0;
4162 if (!has_defaults || machine_class->no_parallel) {
4163 default_parallel = 0;
4165 if (!has_defaults || !machine_class->use_virtcon) {
4166 default_virtcon = 0;
4168 if (!has_defaults || !machine_class->use_sclp) {
4169 default_sclp = 0;
4171 if (!has_defaults || machine_class->no_floppy) {
4172 default_floppy = 0;
4174 if (!has_defaults || machine_class->no_cdrom) {
4175 default_cdrom = 0;
4177 if (!has_defaults || machine_class->no_sdcard) {
4178 default_sdcard = 0;
4180 if (!has_defaults) {
4181 default_monitor = 0;
4182 default_net = 0;
4183 default_vga = 0;
4186 if (is_daemonized()) {
4187 /* According to documentation and historically, -nographic redirects
4188 * serial port, parallel port and monitor to stdio, which does not work
4189 * with -daemonize. We can redirect these to null instead, but since
4190 * -nographic is legacy, let's just error out.
4191 * We disallow -nographic only if all other ports are not redirected
4192 * explicitly, to not break existing legacy setups which uses
4193 * -nographic _and_ redirects all ports explicitly - this is valid
4194 * usage, -nographic is just a no-op in this case.
4196 if (display_type == DT_NOGRAPHIC
4197 && (default_parallel || default_serial
4198 || default_monitor || default_virtcon)) {
4199 error_report("-nographic cannot be used with -daemonize");
4200 exit(1);
4202 #ifdef CONFIG_CURSES
4203 if (display_type == DT_CURSES) {
4204 error_report("curses display cannot be used with -daemonize");
4205 exit(1);
4207 #endif
4210 if (display_type == DT_NOGRAPHIC) {
4211 if (default_parallel)
4212 add_device_config(DEV_PARALLEL, "null");
4213 if (default_serial && default_monitor) {
4214 add_device_config(DEV_SERIAL, "mon:stdio");
4215 } else if (default_virtcon && default_monitor) {
4216 add_device_config(DEV_VIRTCON, "mon:stdio");
4217 } else if (default_sclp && default_monitor) {
4218 add_device_config(DEV_SCLP, "mon:stdio");
4219 } else {
4220 if (default_serial)
4221 add_device_config(DEV_SERIAL, "stdio");
4222 if (default_virtcon)
4223 add_device_config(DEV_VIRTCON, "stdio");
4224 if (default_sclp) {
4225 add_device_config(DEV_SCLP, "stdio");
4227 if (default_monitor)
4228 monitor_parse("stdio", "readline", false);
4230 } else {
4231 if (default_serial)
4232 add_device_config(DEV_SERIAL, "vc:80Cx24C");
4233 if (default_parallel)
4234 add_device_config(DEV_PARALLEL, "vc:80Cx24C");
4235 if (default_monitor)
4236 monitor_parse("vc:80Cx24C", "readline", false);
4237 if (default_virtcon)
4238 add_device_config(DEV_VIRTCON, "vc:80Cx24C");
4239 if (default_sclp) {
4240 add_device_config(DEV_SCLP, "vc:80Cx24C");
4244 #if defined(CONFIG_VNC)
4245 if (!QTAILQ_EMPTY(&(qemu_find_opts("vnc")->head))) {
4246 display_remote++;
4248 #endif
4249 if (display_type == DT_DEFAULT && !display_remote) {
4250 #if defined(CONFIG_GTK)
4251 display_type = DT_GTK;
4252 #elif defined(CONFIG_SDL) || defined(CONFIG_COCOA)
4253 display_type = DT_SDL;
4254 #elif defined(CONFIG_VNC)
4255 vnc_parse("localhost:0,to=99,id=default", &error_abort);
4256 show_vnc_port = 1;
4257 #else
4258 display_type = DT_NONE;
4259 #endif
4262 if ((no_frame || alt_grab || ctrl_grab) && display_type != DT_SDL) {
4263 error_report("-no-frame, -alt-grab and -ctrl-grab are only valid "
4264 "for SDL, ignoring option");
4266 if (no_quit && (display_type != DT_GTK && display_type != DT_SDL)) {
4267 error_report("-no-quit is only valid for GTK and SDL, "
4268 "ignoring option");
4271 #if defined(CONFIG_GTK)
4272 if (display_type == DT_GTK) {
4273 early_gtk_display_init(request_opengl);
4275 #endif
4276 #if defined(CONFIG_SDL)
4277 if (display_type == DT_SDL) {
4278 sdl_display_early_init(request_opengl);
4280 #endif
4281 if (request_opengl == 1 && display_opengl == 0) {
4282 #if defined(CONFIG_OPENGL)
4283 error_report("OpenGL is not supported by the display");
4284 #else
4285 error_report("OpenGL support is disabled");
4286 #endif
4287 exit(1);
4290 page_size_init();
4291 socket_init();
4293 if (qemu_opts_foreach(qemu_find_opts("object"),
4294 user_creatable_add_opts_foreach,
4295 object_create_initial, NULL)) {
4296 exit(1);
4299 if (qemu_opts_foreach(qemu_find_opts("chardev"),
4300 chardev_init_func, NULL, NULL)) {
4301 exit(1);
4304 #ifdef CONFIG_VIRTFS
4305 if (qemu_opts_foreach(qemu_find_opts("fsdev"),
4306 fsdev_init_func, NULL, NULL)) {
4307 exit(1);
4309 #endif
4311 if (pid_file && qemu_create_pidfile(pid_file) != 0) {
4312 error_report("could not acquire pid file: %s", strerror(errno));
4313 exit(1);
4316 if (qemu_opts_foreach(qemu_find_opts("device"),
4317 device_help_func, NULL, NULL)) {
4318 exit(0);
4321 machine_opts = qemu_get_machine_opts();
4322 if (qemu_opt_foreach(machine_opts, machine_set_property, current_machine,
4323 NULL)) {
4324 object_unref(OBJECT(current_machine));
4325 exit(1);
4328 configure_accelerator(current_machine);
4330 if (qtest_chrdev) {
4331 qtest_init(qtest_chrdev, qtest_log, &error_fatal);
4334 machine_opts = qemu_get_machine_opts();
4335 kernel_filename = qemu_opt_get(machine_opts, "kernel");
4336 initrd_filename = qemu_opt_get(machine_opts, "initrd");
4337 kernel_cmdline = qemu_opt_get(machine_opts, "append");
4338 bios_name = qemu_opt_get(machine_opts, "firmware");
4340 opts = qemu_opts_find(qemu_find_opts("boot-opts"), NULL);
4341 if (opts) {
4342 boot_order = qemu_opt_get(opts, "order");
4343 if (boot_order) {
4344 validate_bootdevices(boot_order, &error_fatal);
4347 boot_once = qemu_opt_get(opts, "once");
4348 if (boot_once) {
4349 validate_bootdevices(boot_once, &error_fatal);
4352 boot_menu = qemu_opt_get_bool(opts, "menu", boot_menu);
4353 boot_strict = qemu_opt_get_bool(opts, "strict", false);
4356 if (!boot_order) {
4357 boot_order = machine_class->default_boot_order;
4360 if (!kernel_cmdline) {
4361 kernel_cmdline = "";
4362 current_machine->kernel_cmdline = (char *)kernel_cmdline;
4365 linux_boot = (kernel_filename != NULL);
4367 if (!linux_boot && *kernel_cmdline != '\0') {
4368 error_report("-append only allowed with -kernel option");
4369 exit(1);
4372 if (!linux_boot && initrd_filename != NULL) {
4373 error_report("-initrd only allowed with -kernel option");
4374 exit(1);
4377 if (!linux_boot && qemu_opt_get(machine_opts, "dtb")) {
4378 error_report("-dtb only allowed with -kernel option");
4379 exit(1);
4382 if (semihosting_enabled() && !semihosting_get_argc() && kernel_filename) {
4383 /* fall back to the -kernel/-append */
4384 semihosting_arg_fallback(kernel_filename, kernel_cmdline);
4387 os_set_line_buffering();
4389 #ifdef CONFIG_SPICE
4390 /* spice needs the timers to be initialized by this point */
4391 qemu_spice_init();
4392 #endif
4394 cpu_ticks_init();
4395 if (icount_opts) {
4396 if (kvm_enabled() || xen_enabled()) {
4397 error_report("-icount is not allowed with kvm or xen");
4398 exit(1);
4400 configure_icount(icount_opts, &error_abort);
4401 qemu_opts_del(icount_opts);
4404 /* clean up network at qemu process termination */
4405 atexit(&net_cleanup);
4407 if (net_init_clients() < 0) {
4408 exit(1);
4411 if (qemu_opts_foreach(qemu_find_opts("object"),
4412 user_creatable_add_opts_foreach,
4413 object_create_delayed, NULL)) {
4414 exit(1);
4417 #ifdef CONFIG_TPM
4418 if (tpm_init() < 0) {
4419 exit(1);
4421 #endif
4423 /* init the bluetooth world */
4424 if (foreach_device_config(DEV_BT, bt_parse))
4425 exit(1);
4427 if (!xen_enabled()) {
4428 /* On 32-bit hosts, QEMU is limited by virtual address space */
4429 if (ram_size > (2047 << 20) && HOST_LONG_BITS == 32) {
4430 error_report("at most 2047 MB RAM can be simulated");
4431 exit(1);
4435 blk_mig_init();
4436 ram_mig_init();
4438 /* If the currently selected machine wishes to override the units-per-bus
4439 * property of its default HBA interface type, do so now. */
4440 if (machine_class->units_per_default_bus) {
4441 override_max_devs(machine_class->block_default_type,
4442 machine_class->units_per_default_bus);
4445 /* open the virtual block devices */
4446 if (snapshot || replay_mode != REPLAY_MODE_NONE) {
4447 qemu_opts_foreach(qemu_find_opts("drive"), drive_enable_snapshot,
4448 NULL, NULL);
4450 if (qemu_opts_foreach(qemu_find_opts("drive"), drive_init_func,
4451 &machine_class->block_default_type, NULL)) {
4452 exit(1);
4455 default_drive(default_cdrom, snapshot, machine_class->block_default_type, 2,
4456 CDROM_OPTS);
4457 default_drive(default_floppy, snapshot, IF_FLOPPY, 0, FD_OPTS);
4458 default_drive(default_sdcard, snapshot, IF_SD, 0, SD_OPTS);
4460 parse_numa_opts(machine_class);
4462 if (qemu_opts_foreach(qemu_find_opts("mon"),
4463 mon_init_func, NULL, NULL)) {
4464 exit(1);
4467 if (foreach_device_config(DEV_SERIAL, serial_parse) < 0)
4468 exit(1);
4469 if (foreach_device_config(DEV_PARALLEL, parallel_parse) < 0)
4470 exit(1);
4471 if (foreach_device_config(DEV_VIRTCON, virtcon_parse) < 0)
4472 exit(1);
4473 if (foreach_device_config(DEV_SCLP, sclp_parse) < 0) {
4474 exit(1);
4476 if (foreach_device_config(DEV_DEBUGCON, debugcon_parse) < 0)
4477 exit(1);
4479 /* If no default VGA is requested, the default is "none". */
4480 if (default_vga) {
4481 if (machine_class->default_display) {
4482 vga_model = machine_class->default_display;
4483 } else if (cirrus_vga_available()) {
4484 vga_model = "cirrus";
4485 } else if (vga_available()) {
4486 vga_model = "std";
4489 if (vga_model) {
4490 select_vgahw(vga_model);
4493 if (watchdog) {
4494 i = select_watchdog(watchdog);
4495 if (i > 0)
4496 exit (i == 1 ? 1 : 0);
4499 if (machine_class->compat_props) {
4500 qdev_prop_register_global_list(machine_class->compat_props);
4502 qemu_add_globals();
4504 /* This checkpoint is required by replay to separate prior clock
4505 reading from the other reads, because timer polling functions query
4506 clock values from the log. */
4507 replay_checkpoint(CHECKPOINT_INIT);
4508 qdev_machine_init();
4510 current_machine->ram_size = ram_size;
4511 current_machine->maxram_size = maxram_size;
4512 current_machine->ram_slots = ram_slots;
4513 current_machine->boot_order = boot_order;
4514 current_machine->cpu_model = cpu_model;
4516 machine_class->init(current_machine);
4518 realtime_init();
4520 audio_init();
4522 cpu_synchronize_all_post_init();
4524 numa_post_machine_init();
4526 if (qemu_opts_foreach(qemu_find_opts("fw_cfg"),
4527 parse_fw_cfg, fw_cfg_find(), NULL) != 0) {
4528 exit(1);
4531 /* init USB devices */
4532 if (usb_enabled()) {
4533 if (foreach_device_config(DEV_USB, usb_parse) < 0)
4534 exit(1);
4537 /* Check if IGD GFX passthrough. */
4538 igd_gfx_passthru();
4540 /* init generic devices */
4541 rom_set_order_override(FW_CFG_ORDER_OVERRIDE_DEVICE);
4542 if (qemu_opts_foreach(qemu_find_opts("device"),
4543 device_init_func, NULL, NULL)) {
4544 exit(1);
4546 rom_reset_order_override();
4548 /* Did we create any drives that we failed to create a device for? */
4549 drive_check_orphaned();
4551 net_check_clients();
4553 if (boot_once) {
4554 qemu_boot_set(boot_once, &error_fatal);
4555 qemu_register_reset(restore_boot_order, g_strdup(boot_order));
4558 ds = init_displaystate();
4560 /* init local displays */
4561 switch (display_type) {
4562 case DT_NOGRAPHIC:
4563 (void)ds; /* avoid warning if no display is configured */
4564 break;
4565 #if defined(CONFIG_CURSES)
4566 case DT_CURSES:
4567 curses_display_init(ds, full_screen);
4568 break;
4569 #endif
4570 #if defined(CONFIG_SDL)
4571 case DT_SDL:
4572 sdl_display_init(ds, full_screen, no_frame);
4573 break;
4574 #elif defined(CONFIG_COCOA)
4575 case DT_SDL:
4576 cocoa_display_init(ds, full_screen);
4577 break;
4578 #endif
4579 #if defined(CONFIG_GTK)
4580 case DT_GTK:
4581 gtk_display_init(ds, full_screen, grab_on_hover);
4582 break;
4583 #endif
4584 default:
4585 break;
4588 /* must be after terminal init, SDL library changes signal handlers */
4589 os_setup_signal_handling();
4591 #ifdef CONFIG_VNC
4592 /* init remote displays */
4593 qemu_opts_foreach(qemu_find_opts("vnc"),
4594 vnc_init_func, NULL, NULL);
4595 if (show_vnc_port) {
4596 char *ret = vnc_display_local_addr("default");
4597 printf("VNC server running on '%s'\n", ret);
4598 g_free(ret);
4600 #endif
4601 #ifdef CONFIG_SPICE
4602 if (using_spice) {
4603 qemu_spice_display_init();
4605 #endif
4607 if (foreach_device_config(DEV_GDB, gdbserver_start) < 0) {
4608 exit(1);
4611 qdev_machine_creation_done();
4613 /* TODO: once all bus devices are qdevified, this should be done
4614 * when bus is created by qdev.c */
4615 qemu_register_reset(qbus_reset_all_fn, sysbus_get_default());
4616 qemu_run_machine_init_done_notifiers();
4618 if (rom_check_and_register_reset() != 0) {
4619 error_report("rom check and register reset failed");
4620 exit(1);
4623 replay_start();
4625 /* This checkpoint is required by replay to separate prior clock
4626 reading from the other reads, because timer polling functions query
4627 clock values from the log. */
4628 replay_checkpoint(CHECKPOINT_RESET);
4629 qemu_system_reset(VMRESET_SILENT);
4630 register_global_state();
4631 if (loadvm) {
4632 if (load_vmstate(loadvm) < 0) {
4633 autostart = 0;
4637 qdev_prop_check_globals();
4638 if (vmstate_dump_file) {
4639 /* dump and exit */
4640 dump_vmstate_json_to_file(vmstate_dump_file);
4641 return 0;
4644 if (incoming) {
4645 Error *local_err = NULL;
4646 qemu_start_incoming_migration(incoming, &local_err);
4647 if (local_err) {
4648 error_reportf_err(local_err, "-incoming %s: ", incoming);
4649 exit(1);
4651 } else if (autostart) {
4652 vm_start();
4655 os_setup_post();
4657 main_loop();
4658 replay_disable_events();
4660 bdrv_close_all();
4661 pause_all_vcpus();
4662 res_free();
4663 #ifdef CONFIG_TPM
4664 tpm_cleanup();
4665 #endif
4667 return 0;