Makefile: Derive "PKGVERSION" from "git describe" by default
[qemu/ar7.git] / vl.c
blob9539f926943be1db3284c005bcf38a112d74b6ad
1 /*
2 * QEMU System Emulator
4 * Copyright (c) 2003-2008 Fabrice Bellard
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 * THE SOFTWARE.
24 #include "qemu/osdep.h"
25 #include "qemu-version.h"
26 #include "qemu/cutils.h"
27 #include "qemu/help_option.h"
29 #ifdef CONFIG_SECCOMP
30 #include "sysemu/seccomp.h"
31 #endif
33 #if defined(CONFIG_VDE)
34 #include <libvdeplug.h>
35 #endif
37 #ifdef CONFIG_SDL
38 #if defined(__APPLE__) || defined(main)
39 #include <SDL.h>
40 int qemu_main(int argc, char **argv, char **envp);
41 int main(int argc, char **argv)
43 return qemu_main(argc, argv, NULL);
45 #undef main
46 #define main qemu_main
47 #endif
48 #endif /* CONFIG_SDL */
50 #ifdef CONFIG_COCOA
51 #undef main
52 #define main qemu_main
53 #endif /* CONFIG_COCOA */
55 #include <glib.h>
57 #include "qemu/error-report.h"
58 #include "qemu/sockets.h"
59 #include "hw/hw.h"
60 #include "hw/boards.h"
61 #include "sysemu/accel.h"
62 #include "hw/usb.h"
63 #include "hw/i386/pc.h"
64 #include "hw/isa/isa.h"
65 #include "hw/bt.h"
66 #include "sysemu/watchdog.h"
67 #include "hw/smbios/smbios.h"
68 #include "hw/xen/xen.h"
69 #include "hw/qdev.h"
70 #include "hw/loader.h"
71 #include "monitor/qdev.h"
72 #include "sysemu/bt.h"
73 #include "net/net.h"
74 #include "net/slirp.h"
75 #include "monitor/monitor.h"
76 #include "ui/console.h"
77 #include "ui/input.h"
78 #include "sysemu/sysemu.h"
79 #include "sysemu/numa.h"
80 #include "exec/gdbstub.h"
81 #include "qemu/timer.h"
82 #include "sysemu/char.h"
83 #include "qemu/bitmap.h"
84 #include "qemu/log.h"
85 #include "sysemu/blockdev.h"
86 #include "hw/block/block.h"
87 #include "migration/block.h"
88 #include "sysemu/tpm.h"
89 #include "sysemu/dma.h"
90 #include "audio/audio.h"
91 #include "migration/migration.h"
92 #include "sysemu/cpus.h"
93 #include "sysemu/kvm.h"
94 #include "qapi/qmp/qjson.h"
95 #include "qemu/option.h"
96 #include "qemu/config-file.h"
97 #include "qemu-options.h"
98 #include "qmp-commands.h"
99 #include "qemu/main-loop.h"
100 #ifdef CONFIG_VIRTFS
101 #include "fsdev/qemu-fsdev.h"
102 #endif
103 #include "sysemu/qtest.h"
105 #include "disas/disas.h"
108 #include "slirp/libslirp.h"
110 #include "trace.h"
111 #include "trace/control.h"
112 #include "qemu/queue.h"
113 #include "sysemu/cpus.h"
114 #include "sysemu/arch_init.h"
116 #include "ui/qemu-spice.h"
117 #include "qapi/string-input-visitor.h"
118 #include "qapi/opts-visitor.h"
119 #include "qom/object_interfaces.h"
120 #include "qapi-event.h"
121 #include "exec/semihost.h"
122 #include "crypto/init.h"
123 #include "sysemu/replay.h"
124 #include "qapi/qmp/qerror.h"
126 #define MAX_VIRTIO_CONSOLES 1
127 #define MAX_SCLP_CONSOLES 1
129 static const char *data_dir[16];
130 static int data_dir_idx;
131 const char *bios_name = NULL;
132 enum vga_retrace_method vga_retrace_method = VGA_RETRACE_DUMB;
133 int request_opengl = -1;
134 int display_opengl;
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 static bool grab_on_hover;
151 CharDriverState *serial_hds[MAX_SERIAL_PORTS];
152 CharDriverState *parallel_hds[MAX_PARALLEL_PORTS];
153 CharDriverState *virtcon_hds[MAX_VIRTIO_CONSOLES];
154 CharDriverState *sclp_hds[MAX_SCLP_CONSOLES];
155 int win2k_install_hack = 0;
156 int singlestep = 0;
157 int smp_cpus = 1;
158 int max_cpus = 0;
159 int smp_cores = 1;
160 int smp_threads = 1;
161 int acpi_enabled = 1;
162 int no_hpet = 0;
163 int fd_bootchk = 1;
164 static int no_reboot;
165 int no_shutdown = 0;
166 int cursor_hide = 1;
167 int graphic_rotate = 0;
168 const char *watchdog;
169 QEMUOptionRom option_rom[MAX_OPTION_ROMS];
170 int nb_option_roms;
171 int old_param = 0;
172 const char *qemu_name;
173 int alt_grab = 0;
174 int ctrl_grab = 0;
175 unsigned int nb_prom_envs = 0;
176 const char *prom_envs[MAX_PROM_ENVS];
177 int boot_menu;
178 bool boot_strict;
179 uint8_t *boot_splash_filedata;
180 size_t boot_splash_filedata_size;
181 uint8_t qemu_extra_params_fw[2];
183 int icount_align_option;
185 /* The bytes in qemu_uuid[] are in the order specified by RFC4122, _not_ in the
186 * little-endian "wire format" described in the SMBIOS 2.6 specification.
188 uint8_t qemu_uuid[16];
189 bool qemu_uuid_set;
191 static NotifierList exit_notifiers =
192 NOTIFIER_LIST_INITIALIZER(exit_notifiers);
194 static NotifierList machine_init_done_notifiers =
195 NOTIFIER_LIST_INITIALIZER(machine_init_done_notifiers);
197 bool xen_allowed;
198 uint32_t xen_domid;
199 enum xen_mode xen_mode = XEN_EMULATE;
201 static int has_defaults = 1;
202 static int default_serial = 1;
203 static int default_parallel = 1;
204 static int default_virtcon = 1;
205 static int default_sclp = 1;
206 static int default_monitor = 1;
207 static int default_floppy = 1;
208 static int default_cdrom = 1;
209 static int default_sdcard = 1;
210 static int default_vga = 1;
211 static int default_net = 1;
213 static struct {
214 const char *driver;
215 int *flag;
216 } default_list[] = {
217 { .driver = "isa-serial", .flag = &default_serial },
218 { .driver = "isa-parallel", .flag = &default_parallel },
219 { .driver = "isa-fdc", .flag = &default_floppy },
220 { .driver = "ide-cd", .flag = &default_cdrom },
221 { .driver = "ide-hd", .flag = &default_cdrom },
222 { .driver = "ide-drive", .flag = &default_cdrom },
223 { .driver = "scsi-cd", .flag = &default_cdrom },
224 { .driver = "virtio-serial-pci", .flag = &default_virtcon },
225 { .driver = "virtio-serial", .flag = &default_virtcon },
226 { .driver = "VGA", .flag = &default_vga },
227 { .driver = "isa-vga", .flag = &default_vga },
228 { .driver = "cirrus-vga", .flag = &default_vga },
229 { .driver = "isa-cirrus-vga", .flag = &default_vga },
230 { .driver = "vmware-svga", .flag = &default_vga },
231 { .driver = "qxl-vga", .flag = &default_vga },
232 { .driver = "virtio-vga", .flag = &default_vga },
235 static QemuOptsList qemu_rtc_opts = {
236 .name = "rtc",
237 .head = QTAILQ_HEAD_INITIALIZER(qemu_rtc_opts.head),
238 .desc = {
240 .name = "base",
241 .type = QEMU_OPT_STRING,
243 .name = "clock",
244 .type = QEMU_OPT_STRING,
246 .name = "driftfix",
247 .type = QEMU_OPT_STRING,
249 { /* end of list */ }
253 static QemuOptsList qemu_sandbox_opts = {
254 .name = "sandbox",
255 .implied_opt_name = "enable",
256 .head = QTAILQ_HEAD_INITIALIZER(qemu_sandbox_opts.head),
257 .desc = {
259 .name = "enable",
260 .type = QEMU_OPT_BOOL,
262 { /* end of list */ }
266 static QemuOptsList qemu_trace_opts = {
267 .name = "trace",
268 .implied_opt_name = "enable",
269 .head = QTAILQ_HEAD_INITIALIZER(qemu_trace_opts.head),
270 .desc = {
272 .name = "enable",
273 .type = QEMU_OPT_STRING,
276 .name = "events",
277 .type = QEMU_OPT_STRING,
279 .name = "file",
280 .type = QEMU_OPT_STRING,
282 { /* end of list */ }
286 static QemuOptsList qemu_option_rom_opts = {
287 .name = "option-rom",
288 .implied_opt_name = "romfile",
289 .head = QTAILQ_HEAD_INITIALIZER(qemu_option_rom_opts.head),
290 .desc = {
292 .name = "bootindex",
293 .type = QEMU_OPT_NUMBER,
294 }, {
295 .name = "romfile",
296 .type = QEMU_OPT_STRING,
298 { /* end of list */ }
302 static QemuOptsList qemu_machine_opts = {
303 .name = "machine",
304 .implied_opt_name = "type",
305 .merge_lists = true,
306 .head = QTAILQ_HEAD_INITIALIZER(qemu_machine_opts.head),
307 .desc = {
309 * no elements => accept any
310 * sanity checking will happen later
311 * when setting machine properties
317 static QemuOptsList qemu_boot_opts = {
318 .name = "boot-opts",
319 .implied_opt_name = "order",
320 .merge_lists = true,
321 .head = QTAILQ_HEAD_INITIALIZER(qemu_boot_opts.head),
322 .desc = {
324 .name = "order",
325 .type = QEMU_OPT_STRING,
326 }, {
327 .name = "once",
328 .type = QEMU_OPT_STRING,
329 }, {
330 .name = "menu",
331 .type = QEMU_OPT_BOOL,
332 }, {
333 .name = "splash",
334 .type = QEMU_OPT_STRING,
335 }, {
336 .name = "splash-time",
337 .type = QEMU_OPT_STRING,
338 }, {
339 .name = "reboot-timeout",
340 .type = QEMU_OPT_STRING,
341 }, {
342 .name = "strict",
343 .type = QEMU_OPT_BOOL,
345 { /*End of list */ }
349 static QemuOptsList qemu_add_fd_opts = {
350 .name = "add-fd",
351 .head = QTAILQ_HEAD_INITIALIZER(qemu_add_fd_opts.head),
352 .desc = {
354 .name = "fd",
355 .type = QEMU_OPT_NUMBER,
356 .help = "file descriptor of which a duplicate is added to fd set",
358 .name = "set",
359 .type = QEMU_OPT_NUMBER,
360 .help = "ID of the fd set to add fd to",
362 .name = "opaque",
363 .type = QEMU_OPT_STRING,
364 .help = "free-form string used to describe fd",
366 { /* end of list */ }
370 static QemuOptsList qemu_object_opts = {
371 .name = "object",
372 .implied_opt_name = "qom-type",
373 .head = QTAILQ_HEAD_INITIALIZER(qemu_object_opts.head),
374 .desc = {
379 static QemuOptsList qemu_tpmdev_opts = {
380 .name = "tpmdev",
381 .implied_opt_name = "type",
382 .head = QTAILQ_HEAD_INITIALIZER(qemu_tpmdev_opts.head),
383 .desc = {
384 /* options are defined in the TPM backends */
385 { /* end of list */ }
389 static QemuOptsList qemu_realtime_opts = {
390 .name = "realtime",
391 .head = QTAILQ_HEAD_INITIALIZER(qemu_realtime_opts.head),
392 .desc = {
394 .name = "mlock",
395 .type = QEMU_OPT_BOOL,
397 { /* end of list */ }
401 static QemuOptsList qemu_msg_opts = {
402 .name = "msg",
403 .head = QTAILQ_HEAD_INITIALIZER(qemu_msg_opts.head),
404 .desc = {
406 .name = "timestamp",
407 .type = QEMU_OPT_BOOL,
409 { /* end of list */ }
413 static QemuOptsList qemu_name_opts = {
414 .name = "name",
415 .implied_opt_name = "guest",
416 .merge_lists = true,
417 .head = QTAILQ_HEAD_INITIALIZER(qemu_name_opts.head),
418 .desc = {
420 .name = "guest",
421 .type = QEMU_OPT_STRING,
422 .help = "Sets the name of the guest.\n"
423 "This name will be displayed in the SDL window caption.\n"
424 "The name will also be used for the VNC server",
425 }, {
426 .name = "process",
427 .type = QEMU_OPT_STRING,
428 .help = "Sets the name of the QEMU process, as shown in top etc",
429 }, {
430 .name = "debug-threads",
431 .type = QEMU_OPT_BOOL,
432 .help = "When enabled, name the individual threads; defaults off.\n"
433 "NOTE: The thread names are for debugging and not a\n"
434 "stable API.",
436 { /* End of list */ }
440 static QemuOptsList qemu_mem_opts = {
441 .name = "memory",
442 .implied_opt_name = "size",
443 .head = QTAILQ_HEAD_INITIALIZER(qemu_mem_opts.head),
444 .merge_lists = true,
445 .desc = {
447 .name = "size",
448 .type = QEMU_OPT_SIZE,
451 .name = "slots",
452 .type = QEMU_OPT_NUMBER,
455 .name = "maxmem",
456 .type = QEMU_OPT_SIZE,
458 { /* end of list */ }
462 static QemuOptsList qemu_icount_opts = {
463 .name = "icount",
464 .implied_opt_name = "shift",
465 .merge_lists = true,
466 .head = QTAILQ_HEAD_INITIALIZER(qemu_icount_opts.head),
467 .desc = {
469 .name = "shift",
470 .type = QEMU_OPT_STRING,
471 }, {
472 .name = "align",
473 .type = QEMU_OPT_BOOL,
474 }, {
475 .name = "sleep",
476 .type = QEMU_OPT_BOOL,
477 }, {
478 .name = "rr",
479 .type = QEMU_OPT_STRING,
480 }, {
481 .name = "rrfile",
482 .type = QEMU_OPT_STRING,
484 { /* end of list */ }
488 static QemuOptsList qemu_semihosting_config_opts = {
489 .name = "semihosting-config",
490 .implied_opt_name = "enable",
491 .head = QTAILQ_HEAD_INITIALIZER(qemu_semihosting_config_opts.head),
492 .desc = {
494 .name = "enable",
495 .type = QEMU_OPT_BOOL,
496 }, {
497 .name = "target",
498 .type = QEMU_OPT_STRING,
499 }, {
500 .name = "arg",
501 .type = QEMU_OPT_STRING,
503 { /* end of list */ }
507 static QemuOptsList qemu_fw_cfg_opts = {
508 .name = "fw_cfg",
509 .implied_opt_name = "name",
510 .head = QTAILQ_HEAD_INITIALIZER(qemu_fw_cfg_opts.head),
511 .desc = {
513 .name = "name",
514 .type = QEMU_OPT_STRING,
515 .help = "Sets the fw_cfg name of the blob to be inserted",
516 }, {
517 .name = "file",
518 .type = QEMU_OPT_STRING,
519 .help = "Sets the name of the file from which\n"
520 "the fw_cfg blob will be loaded",
521 }, {
522 .name = "string",
523 .type = QEMU_OPT_STRING,
524 .help = "Sets content of the blob to be inserted from a string",
526 { /* end of list */ }
531 * Get machine options
533 * Returns: machine options (never null).
535 QemuOpts *qemu_get_machine_opts(void)
537 return qemu_find_opts_singleton("machine");
540 const char *qemu_get_vm_name(void)
542 return qemu_name;
545 static void res_free(void)
547 g_free(boot_splash_filedata);
548 boot_splash_filedata = NULL;
551 static int default_driver_check(void *opaque, QemuOpts *opts, Error **errp)
553 const char *driver = qemu_opt_get(opts, "driver");
554 int i;
556 if (!driver)
557 return 0;
558 for (i = 0; i < ARRAY_SIZE(default_list); i++) {
559 if (strcmp(default_list[i].driver, driver) != 0)
560 continue;
561 *(default_list[i].flag) = 0;
563 return 0;
566 /***********************************************************/
567 /* QEMU state */
569 static RunState current_run_state = RUN_STATE_PRELAUNCH;
571 /* We use RUN_STATE__MAX but any invalid value will do */
572 static RunState vmstop_requested = RUN_STATE__MAX;
573 static QemuMutex vmstop_lock;
575 typedef struct {
576 RunState from;
577 RunState to;
578 } RunStateTransition;
580 static const RunStateTransition runstate_transitions_def[] = {
581 /* from -> to */
582 { RUN_STATE_DEBUG, RUN_STATE_RUNNING },
583 { RUN_STATE_DEBUG, RUN_STATE_FINISH_MIGRATE },
584 { RUN_STATE_DEBUG, RUN_STATE_PRELAUNCH },
586 { RUN_STATE_INMIGRATE, RUN_STATE_INTERNAL_ERROR },
587 { RUN_STATE_INMIGRATE, RUN_STATE_IO_ERROR },
588 { RUN_STATE_INMIGRATE, RUN_STATE_PAUSED },
589 { RUN_STATE_INMIGRATE, RUN_STATE_RUNNING },
590 { RUN_STATE_INMIGRATE, RUN_STATE_SHUTDOWN },
591 { RUN_STATE_INMIGRATE, RUN_STATE_SUSPENDED },
592 { RUN_STATE_INMIGRATE, RUN_STATE_WATCHDOG },
593 { RUN_STATE_INMIGRATE, RUN_STATE_GUEST_PANICKED },
594 { RUN_STATE_INMIGRATE, RUN_STATE_FINISH_MIGRATE },
595 { RUN_STATE_INMIGRATE, RUN_STATE_PRELAUNCH },
596 { RUN_STATE_INMIGRATE, RUN_STATE_POSTMIGRATE },
598 { RUN_STATE_INTERNAL_ERROR, RUN_STATE_PAUSED },
599 { RUN_STATE_INTERNAL_ERROR, RUN_STATE_FINISH_MIGRATE },
600 { RUN_STATE_INTERNAL_ERROR, RUN_STATE_PRELAUNCH },
602 { RUN_STATE_IO_ERROR, RUN_STATE_RUNNING },
603 { RUN_STATE_IO_ERROR, RUN_STATE_FINISH_MIGRATE },
604 { RUN_STATE_IO_ERROR, RUN_STATE_PRELAUNCH },
606 { RUN_STATE_PAUSED, RUN_STATE_RUNNING },
607 { RUN_STATE_PAUSED, RUN_STATE_FINISH_MIGRATE },
608 { RUN_STATE_PAUSED, RUN_STATE_PRELAUNCH },
610 { RUN_STATE_POSTMIGRATE, RUN_STATE_RUNNING },
611 { RUN_STATE_POSTMIGRATE, RUN_STATE_FINISH_MIGRATE },
612 { RUN_STATE_POSTMIGRATE, RUN_STATE_PRELAUNCH },
614 { RUN_STATE_PRELAUNCH, RUN_STATE_RUNNING },
615 { RUN_STATE_PRELAUNCH, RUN_STATE_FINISH_MIGRATE },
616 { RUN_STATE_PRELAUNCH, RUN_STATE_INMIGRATE },
618 { RUN_STATE_FINISH_MIGRATE, RUN_STATE_RUNNING },
619 { RUN_STATE_FINISH_MIGRATE, RUN_STATE_POSTMIGRATE },
620 { RUN_STATE_FINISH_MIGRATE, RUN_STATE_PRELAUNCH },
622 { RUN_STATE_RESTORE_VM, RUN_STATE_RUNNING },
623 { RUN_STATE_RESTORE_VM, RUN_STATE_PRELAUNCH },
625 { RUN_STATE_RUNNING, RUN_STATE_DEBUG },
626 { RUN_STATE_RUNNING, RUN_STATE_INTERNAL_ERROR },
627 { RUN_STATE_RUNNING, RUN_STATE_IO_ERROR },
628 { RUN_STATE_RUNNING, RUN_STATE_PAUSED },
629 { RUN_STATE_RUNNING, RUN_STATE_FINISH_MIGRATE },
630 { RUN_STATE_RUNNING, RUN_STATE_RESTORE_VM },
631 { RUN_STATE_RUNNING, RUN_STATE_SAVE_VM },
632 { RUN_STATE_RUNNING, RUN_STATE_SHUTDOWN },
633 { RUN_STATE_RUNNING, RUN_STATE_WATCHDOG },
634 { RUN_STATE_RUNNING, RUN_STATE_GUEST_PANICKED },
636 { RUN_STATE_SAVE_VM, RUN_STATE_RUNNING },
638 { RUN_STATE_SHUTDOWN, RUN_STATE_PAUSED },
639 { RUN_STATE_SHUTDOWN, RUN_STATE_FINISH_MIGRATE },
640 { RUN_STATE_SHUTDOWN, RUN_STATE_PRELAUNCH },
642 { RUN_STATE_DEBUG, RUN_STATE_SUSPENDED },
643 { RUN_STATE_RUNNING, RUN_STATE_SUSPENDED },
644 { RUN_STATE_SUSPENDED, RUN_STATE_RUNNING },
645 { RUN_STATE_SUSPENDED, RUN_STATE_FINISH_MIGRATE },
646 { RUN_STATE_SUSPENDED, RUN_STATE_PRELAUNCH },
648 { RUN_STATE_WATCHDOG, RUN_STATE_RUNNING },
649 { RUN_STATE_WATCHDOG, RUN_STATE_FINISH_MIGRATE },
650 { RUN_STATE_WATCHDOG, RUN_STATE_PRELAUNCH },
652 { RUN_STATE_GUEST_PANICKED, RUN_STATE_RUNNING },
653 { RUN_STATE_GUEST_PANICKED, RUN_STATE_FINISH_MIGRATE },
654 { RUN_STATE_GUEST_PANICKED, RUN_STATE_PRELAUNCH },
656 { RUN_STATE__MAX, RUN_STATE__MAX },
659 static bool runstate_valid_transitions[RUN_STATE__MAX][RUN_STATE__MAX];
661 bool runstate_check(RunState state)
663 return current_run_state == state;
666 bool runstate_store(char *str, size_t size)
668 const char *state = RunState_lookup[current_run_state];
669 size_t len = strlen(state) + 1;
671 if (len > size) {
672 return false;
674 memcpy(str, state, len);
675 return true;
678 static void runstate_init(void)
680 const RunStateTransition *p;
682 memset(&runstate_valid_transitions, 0, sizeof(runstate_valid_transitions));
683 for (p = &runstate_transitions_def[0]; p->from != RUN_STATE__MAX; p++) {
684 runstate_valid_transitions[p->from][p->to] = true;
687 qemu_mutex_init(&vmstop_lock);
690 /* This function will abort() on invalid state transitions */
691 void runstate_set(RunState new_state)
693 assert(new_state < RUN_STATE__MAX);
695 if (current_run_state == new_state) {
696 return;
699 if (!runstate_valid_transitions[current_run_state][new_state]) {
700 error_report("invalid runstate transition: '%s' -> '%s'",
701 RunState_lookup[current_run_state],
702 RunState_lookup[new_state]);
703 abort();
705 trace_runstate_set(new_state);
706 current_run_state = new_state;
709 int runstate_is_running(void)
711 return runstate_check(RUN_STATE_RUNNING);
714 bool runstate_needs_reset(void)
716 return runstate_check(RUN_STATE_INTERNAL_ERROR) ||
717 runstate_check(RUN_STATE_SHUTDOWN);
720 StatusInfo *qmp_query_status(Error **errp)
722 StatusInfo *info = g_malloc0(sizeof(*info));
724 info->running = runstate_is_running();
725 info->singlestep = singlestep;
726 info->status = current_run_state;
728 return info;
731 static bool qemu_vmstop_requested(RunState *r)
733 qemu_mutex_lock(&vmstop_lock);
734 *r = vmstop_requested;
735 vmstop_requested = RUN_STATE__MAX;
736 qemu_mutex_unlock(&vmstop_lock);
737 return *r < RUN_STATE__MAX;
740 void qemu_system_vmstop_request_prepare(void)
742 qemu_mutex_lock(&vmstop_lock);
745 void qemu_system_vmstop_request(RunState state)
747 vmstop_requested = state;
748 qemu_mutex_unlock(&vmstop_lock);
749 qemu_notify_event();
752 void vm_start(void)
754 RunState requested;
756 qemu_vmstop_requested(&requested);
757 if (runstate_is_running() && requested == RUN_STATE__MAX) {
758 return;
761 /* Ensure that a STOP/RESUME pair of events is emitted if a
762 * vmstop request was pending. The BLOCK_IO_ERROR event, for
763 * example, according to documentation is always followed by
764 * the STOP event.
766 if (runstate_is_running()) {
767 qapi_event_send_stop(&error_abort);
768 } else {
769 cpu_enable_ticks();
770 runstate_set(RUN_STATE_RUNNING);
771 vm_state_notify(1, RUN_STATE_RUNNING);
772 resume_all_vcpus();
775 qapi_event_send_resume(&error_abort);
779 /***********************************************************/
780 /* real time host monotonic timer */
782 static time_t qemu_time(void)
784 return qemu_clock_get_ms(QEMU_CLOCK_HOST) / 1000;
787 /***********************************************************/
788 /* host time/date access */
789 void qemu_get_timedate(struct tm *tm, int offset)
791 time_t ti = qemu_time();
793 ti += offset;
794 if (rtc_date_offset == -1) {
795 if (rtc_utc)
796 gmtime_r(&ti, tm);
797 else
798 localtime_r(&ti, tm);
799 } else {
800 ti -= rtc_date_offset;
801 gmtime_r(&ti, tm);
805 int qemu_timedate_diff(struct tm *tm)
807 time_t seconds;
809 if (rtc_date_offset == -1)
810 if (rtc_utc)
811 seconds = mktimegm(tm);
812 else {
813 struct tm tmp = *tm;
814 tmp.tm_isdst = -1; /* use timezone to figure it out */
815 seconds = mktime(&tmp);
817 else
818 seconds = mktimegm(tm) + rtc_date_offset;
820 return seconds - qemu_time();
823 static void configure_rtc_date_offset(const char *startdate, int legacy)
825 time_t rtc_start_date;
826 struct tm tm;
828 if (!strcmp(startdate, "now") && legacy) {
829 rtc_date_offset = -1;
830 } else {
831 if (sscanf(startdate, "%d-%d-%dT%d:%d:%d",
832 &tm.tm_year,
833 &tm.tm_mon,
834 &tm.tm_mday,
835 &tm.tm_hour,
836 &tm.tm_min,
837 &tm.tm_sec) == 6) {
838 /* OK */
839 } else if (sscanf(startdate, "%d-%d-%d",
840 &tm.tm_year,
841 &tm.tm_mon,
842 &tm.tm_mday) == 3) {
843 tm.tm_hour = 0;
844 tm.tm_min = 0;
845 tm.tm_sec = 0;
846 } else {
847 goto date_fail;
849 tm.tm_year -= 1900;
850 tm.tm_mon--;
851 rtc_start_date = mktimegm(&tm);
852 if (rtc_start_date == -1) {
853 date_fail:
854 error_report("invalid date format");
855 error_printf("valid formats: "
856 "'2006-06-17T16:01:21' or '2006-06-17'\n");
857 exit(1);
859 rtc_date_offset = qemu_time() - rtc_start_date;
863 static void configure_rtc(QemuOpts *opts)
865 const char *value;
867 value = qemu_opt_get(opts, "base");
868 if (value) {
869 if (!strcmp(value, "utc")) {
870 rtc_utc = 1;
871 } else if (!strcmp(value, "localtime")) {
872 Error *blocker = NULL;
873 rtc_utc = 0;
874 error_setg(&blocker, QERR_REPLAY_NOT_SUPPORTED,
875 "-rtc base=localtime");
876 replay_add_blocker(blocker);
877 } else {
878 configure_rtc_date_offset(value, 0);
881 value = qemu_opt_get(opts, "clock");
882 if (value) {
883 if (!strcmp(value, "host")) {
884 rtc_clock = QEMU_CLOCK_HOST;
885 } else if (!strcmp(value, "rt")) {
886 rtc_clock = QEMU_CLOCK_REALTIME;
887 } else if (!strcmp(value, "vm")) {
888 rtc_clock = QEMU_CLOCK_VIRTUAL;
889 } else {
890 error_report("invalid option value '%s'", value);
891 exit(1);
894 value = qemu_opt_get(opts, "driftfix");
895 if (value) {
896 if (!strcmp(value, "slew")) {
897 static GlobalProperty slew_lost_ticks = {
898 .driver = "mc146818rtc",
899 .property = "lost_tick_policy",
900 .value = "slew",
903 qdev_prop_register_global(&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 typedef struct VGAInterfaceInfo {
1984 const char *opt_name; /* option name */
1985 const char *name; /* human-readable name */
1986 /* Class names indicating that support is available.
1987 * If no class is specified, the interface is always available */
1988 const char *class_names[2];
1989 } VGAInterfaceInfo;
1991 static VGAInterfaceInfo vga_interfaces[VGA_TYPE_MAX] = {
1992 [VGA_NONE] = {
1993 .opt_name = "none",
1995 [VGA_STD] = {
1996 .opt_name = "std",
1997 .name = "standard VGA",
1998 .class_names = { "VGA", "isa-vga" },
2000 [VGA_CIRRUS] = {
2001 .opt_name = "cirrus",
2002 .name = "Cirrus VGA",
2003 .class_names = { "cirrus-vga", "isa-cirrus-vga" },
2005 [VGA_VMWARE] = {
2006 .opt_name = "vmware",
2007 .name = "VMWare SVGA",
2008 .class_names = { "vmware-svga" },
2010 [VGA_VIRTIO] = {
2011 .opt_name = "virtio",
2012 .name = "Virtio VGA",
2013 .class_names = { "virtio-vga" },
2015 [VGA_QXL] = {
2016 .opt_name = "qxl",
2017 .name = "QXL VGA",
2018 .class_names = { "qxl-vga" },
2020 [VGA_TCX] = {
2021 .opt_name = "tcx",
2022 .name = "TCX framebuffer",
2023 .class_names = { "SUNW,tcx" },
2025 [VGA_CG3] = {
2026 .opt_name = "cg3",
2027 .name = "CG3 framebuffer",
2028 .class_names = { "cgthree" },
2030 [VGA_XENFB] = {
2031 .opt_name = "xenfb",
2035 static bool vga_interface_available(VGAInterfaceType t)
2037 VGAInterfaceInfo *ti = &vga_interfaces[t];
2039 assert(t < VGA_TYPE_MAX);
2040 return !ti->class_names[0] ||
2041 object_class_by_name(ti->class_names[0]) ||
2042 object_class_by_name(ti->class_names[1]);
2045 static void select_vgahw(const char *p)
2047 const char *opts;
2048 int t;
2050 assert(vga_interface_type == VGA_NONE);
2051 for (t = 0; t < VGA_TYPE_MAX; t++) {
2052 VGAInterfaceInfo *ti = &vga_interfaces[t];
2053 if (ti->opt_name && strstart(p, ti->opt_name, &opts)) {
2054 if (!vga_interface_available(t)) {
2055 error_report("%s not available", ti->name);
2056 exit(1);
2058 vga_interface_type = t;
2059 break;
2062 if (t == VGA_TYPE_MAX) {
2063 invalid_vga:
2064 error_report("unknown vga type: %s", p);
2065 exit(1);
2067 while (*opts) {
2068 const char *nextopt;
2070 if (strstart(opts, ",retrace=", &nextopt)) {
2071 opts = nextopt;
2072 if (strstart(opts, "dumb", &nextopt))
2073 vga_retrace_method = VGA_RETRACE_DUMB;
2074 else if (strstart(opts, "precise", &nextopt))
2075 vga_retrace_method = VGA_RETRACE_PRECISE;
2076 else goto invalid_vga;
2077 } else goto invalid_vga;
2078 opts = nextopt;
2082 typedef enum DisplayType {
2083 DT_DEFAULT,
2084 DT_CURSES,
2085 DT_SDL,
2086 DT_COCOA,
2087 DT_GTK,
2088 DT_NONE,
2089 } DisplayType;
2091 static DisplayType select_display(const char *p)
2093 const char *opts;
2094 DisplayType display = DT_DEFAULT;
2096 if (strstart(p, "sdl", &opts)) {
2097 #ifdef CONFIG_SDL
2098 display = DT_SDL;
2099 while (*opts) {
2100 const char *nextopt;
2102 if (strstart(opts, ",frame=", &nextopt)) {
2103 opts = nextopt;
2104 if (strstart(opts, "on", &nextopt)) {
2105 no_frame = 0;
2106 } else if (strstart(opts, "off", &nextopt)) {
2107 no_frame = 1;
2108 } else {
2109 goto invalid_sdl_args;
2111 } else if (strstart(opts, ",alt_grab=", &nextopt)) {
2112 opts = nextopt;
2113 if (strstart(opts, "on", &nextopt)) {
2114 alt_grab = 1;
2115 } else if (strstart(opts, "off", &nextopt)) {
2116 alt_grab = 0;
2117 } else {
2118 goto invalid_sdl_args;
2120 } else if (strstart(opts, ",ctrl_grab=", &nextopt)) {
2121 opts = nextopt;
2122 if (strstart(opts, "on", &nextopt)) {
2123 ctrl_grab = 1;
2124 } else if (strstart(opts, "off", &nextopt)) {
2125 ctrl_grab = 0;
2126 } else {
2127 goto invalid_sdl_args;
2129 } else if (strstart(opts, ",window_close=", &nextopt)) {
2130 opts = nextopt;
2131 if (strstart(opts, "on", &nextopt)) {
2132 no_quit = 0;
2133 } else if (strstart(opts, "off", &nextopt)) {
2134 no_quit = 1;
2135 } else {
2136 goto invalid_sdl_args;
2138 } else if (strstart(opts, ",gl=", &nextopt)) {
2139 opts = nextopt;
2140 if (strstart(opts, "on", &nextopt)) {
2141 request_opengl = 1;
2142 } else if (strstart(opts, "off", &nextopt)) {
2143 request_opengl = 0;
2144 } else {
2145 goto invalid_sdl_args;
2147 } else {
2148 invalid_sdl_args:
2149 error_report("invalid SDL option string");
2150 exit(1);
2152 opts = nextopt;
2154 #else
2155 error_report("SDL support is disabled");
2156 exit(1);
2157 #endif
2158 } else if (strstart(p, "vnc", &opts)) {
2159 if (*opts == '=') {
2160 vnc_parse(opts + 1, &error_fatal);
2161 } else {
2162 error_report("VNC requires a display argument vnc=<display>");
2163 exit(1);
2165 } else if (strstart(p, "curses", &opts)) {
2166 #ifdef CONFIG_CURSES
2167 display = DT_CURSES;
2168 #else
2169 error_report("curses support is disabled");
2170 exit(1);
2171 #endif
2172 } else if (strstart(p, "gtk", &opts)) {
2173 #ifdef CONFIG_GTK
2174 display = DT_GTK;
2175 while (*opts) {
2176 const char *nextopt;
2178 if (strstart(opts, ",grab_on_hover=", &nextopt)) {
2179 opts = nextopt;
2180 if (strstart(opts, "on", &nextopt)) {
2181 grab_on_hover = true;
2182 } else if (strstart(opts, "off", &nextopt)) {
2183 grab_on_hover = false;
2184 } else {
2185 goto invalid_gtk_args;
2187 } else if (strstart(opts, ",gl=", &nextopt)) {
2188 opts = nextopt;
2189 if (strstart(opts, "on", &nextopt)) {
2190 request_opengl = 1;
2191 } else if (strstart(opts, "off", &nextopt)) {
2192 request_opengl = 0;
2193 } else {
2194 goto invalid_gtk_args;
2196 } else {
2197 invalid_gtk_args:
2198 error_report("invalid GTK option string");
2199 exit(1);
2201 opts = nextopt;
2203 #else
2204 error_report("GTK support is disabled");
2205 exit(1);
2206 #endif
2207 } else if (strstart(p, "none", &opts)) {
2208 display = DT_NONE;
2209 } else {
2210 error_report("unknown display type");
2211 exit(1);
2214 return display;
2217 static int balloon_parse(const char *arg)
2219 QemuOpts *opts;
2221 if (strcmp(arg, "none") == 0) {
2222 return 0;
2225 if (!strncmp(arg, "virtio", 6)) {
2226 if (arg[6] == ',') {
2227 /* have params -> parse them */
2228 opts = qemu_opts_parse_noisily(qemu_find_opts("device"), arg + 7,
2229 false);
2230 if (!opts)
2231 return -1;
2232 } else {
2233 /* create empty opts */
2234 opts = qemu_opts_create(qemu_find_opts("device"), NULL, 0,
2235 &error_abort);
2237 qemu_opt_set(opts, "driver", "virtio-balloon", &error_abort);
2238 return 0;
2241 return -1;
2244 char *qemu_find_file(int type, const char *name)
2246 int i;
2247 const char *subdir;
2248 char *buf;
2250 /* Try the name as a straight path first */
2251 if (access(name, R_OK) == 0) {
2252 trace_load_file(name, name);
2253 return g_strdup(name);
2256 switch (type) {
2257 case QEMU_FILE_TYPE_BIOS:
2258 subdir = "";
2259 break;
2260 case QEMU_FILE_TYPE_KEYMAP:
2261 subdir = "keymaps/";
2262 break;
2263 default:
2264 abort();
2267 for (i = 0; i < data_dir_idx; i++) {
2268 buf = g_strdup_printf("%s/%s%s", data_dir[i], subdir, name);
2269 if (access(buf, R_OK) == 0) {
2270 trace_load_file(name, buf);
2271 return buf;
2273 g_free(buf);
2275 return NULL;
2278 static inline bool nonempty_str(const char *str)
2280 return str && *str;
2283 static int parse_fw_cfg(void *opaque, QemuOpts *opts, Error **errp)
2285 gchar *buf;
2286 size_t size;
2287 const char *name, *file, *str;
2288 FWCfgState *fw_cfg = (FWCfgState *) opaque;
2290 if (fw_cfg == NULL) {
2291 error_report("fw_cfg device not available");
2292 return -1;
2294 name = qemu_opt_get(opts, "name");
2295 file = qemu_opt_get(opts, "file");
2296 str = qemu_opt_get(opts, "string");
2298 /* we need name and either a file or the content string */
2299 if (!(nonempty_str(name) && (nonempty_str(file) || nonempty_str(str)))) {
2300 error_report("invalid argument(s)");
2301 return -1;
2303 if (nonempty_str(file) && nonempty_str(str)) {
2304 error_report("file and string are mutually exclusive");
2305 return -1;
2307 if (strlen(name) > FW_CFG_MAX_FILE_PATH - 1) {
2308 error_report("name too long (max. %d char)", FW_CFG_MAX_FILE_PATH - 1);
2309 return -1;
2311 if (strncmp(name, "opt/", 4) != 0) {
2312 error_report("warning: externally provided fw_cfg item names "
2313 "should be prefixed with \"opt/\"");
2315 if (nonempty_str(str)) {
2316 size = strlen(str); /* NUL terminator NOT included in fw_cfg blob */
2317 buf = g_memdup(str, size);
2318 } else {
2319 if (!g_file_get_contents(file, &buf, &size, NULL)) {
2320 error_report("can't load %s", file);
2321 return -1;
2324 /* For legacy, keep user files in a specific global order. */
2325 fw_cfg_set_order_override(fw_cfg, FW_CFG_ORDER_OVERRIDE_USER);
2326 fw_cfg_add_file(fw_cfg, name, buf, size);
2327 fw_cfg_reset_order_override(fw_cfg);
2328 return 0;
2331 static int device_help_func(void *opaque, QemuOpts *opts, Error **errp)
2333 return qdev_device_help(opts);
2336 static int device_init_func(void *opaque, QemuOpts *opts, Error **errp)
2338 Error *err = NULL;
2339 DeviceState *dev;
2341 dev = qdev_device_add(opts, &err);
2342 if (!dev) {
2343 error_report_err(err);
2344 return -1;
2346 object_unref(OBJECT(dev));
2347 return 0;
2350 static int chardev_init_func(void *opaque, QemuOpts *opts, Error **errp)
2352 Error *local_err = NULL;
2354 qemu_chr_new_from_opts(opts, NULL, &local_err);
2355 if (local_err) {
2356 error_report_err(local_err);
2357 return -1;
2359 return 0;
2362 #ifdef CONFIG_VIRTFS
2363 static int fsdev_init_func(void *opaque, QemuOpts *opts, Error **errp)
2365 int ret;
2366 ret = qemu_fsdev_add(opts);
2368 return ret;
2370 #endif
2372 static int mon_init_func(void *opaque, QemuOpts *opts, Error **errp)
2374 CharDriverState *chr;
2375 const char *chardev;
2376 const char *mode;
2377 int flags;
2379 mode = qemu_opt_get(opts, "mode");
2380 if (mode == NULL) {
2381 mode = "readline";
2383 if (strcmp(mode, "readline") == 0) {
2384 flags = MONITOR_USE_READLINE;
2385 } else if (strcmp(mode, "control") == 0) {
2386 flags = MONITOR_USE_CONTROL;
2387 } else {
2388 error_report("unknown monitor mode \"%s\"", mode);
2389 exit(1);
2392 if (qemu_opt_get_bool(opts, "pretty", 0))
2393 flags |= MONITOR_USE_PRETTY;
2395 if (qemu_opt_get_bool(opts, "default", 0))
2396 flags |= MONITOR_IS_DEFAULT;
2398 chardev = qemu_opt_get(opts, "chardev");
2399 chr = qemu_chr_find(chardev);
2400 if (chr == NULL) {
2401 error_report("chardev \"%s\" not found", chardev);
2402 exit(1);
2405 qemu_chr_fe_claim_no_fail(chr);
2406 monitor_init(chr, flags);
2407 return 0;
2410 static void monitor_parse(const char *optarg, const char *mode, bool pretty)
2412 static int monitor_device_index = 0;
2413 QemuOpts *opts;
2414 const char *p;
2415 char label[32];
2416 int def = 0;
2418 if (strstart(optarg, "chardev:", &p)) {
2419 snprintf(label, sizeof(label), "%s", p);
2420 } else {
2421 snprintf(label, sizeof(label), "compat_monitor%d",
2422 monitor_device_index);
2423 if (monitor_device_index == 0) {
2424 def = 1;
2426 opts = qemu_chr_parse_compat(label, optarg);
2427 if (!opts) {
2428 error_report("parse error: %s", optarg);
2429 exit(1);
2433 opts = qemu_opts_create(qemu_find_opts("mon"), label, 1, &error_fatal);
2434 qemu_opt_set(opts, "mode", mode, &error_abort);
2435 qemu_opt_set(opts, "chardev", label, &error_abort);
2436 qemu_opt_set_bool(opts, "pretty", pretty, &error_abort);
2437 if (def)
2438 qemu_opt_set(opts, "default", "on", &error_abort);
2439 monitor_device_index++;
2442 struct device_config {
2443 enum {
2444 DEV_USB, /* -usbdevice */
2445 DEV_BT, /* -bt */
2446 DEV_SERIAL, /* -serial */
2447 DEV_PARALLEL, /* -parallel */
2448 DEV_VIRTCON, /* -virtioconsole */
2449 DEV_DEBUGCON, /* -debugcon */
2450 DEV_GDB, /* -gdb, -s */
2451 DEV_SCLP, /* s390 sclp */
2452 } type;
2453 const char *cmdline;
2454 Location loc;
2455 QTAILQ_ENTRY(device_config) next;
2458 static QTAILQ_HEAD(, device_config) device_configs =
2459 QTAILQ_HEAD_INITIALIZER(device_configs);
2461 static void add_device_config(int type, const char *cmdline)
2463 struct device_config *conf;
2465 conf = g_malloc0(sizeof(*conf));
2466 conf->type = type;
2467 conf->cmdline = cmdline;
2468 loc_save(&conf->loc);
2469 QTAILQ_INSERT_TAIL(&device_configs, conf, next);
2472 static int foreach_device_config(int type, int (*func)(const char *cmdline))
2474 struct device_config *conf;
2475 int rc;
2477 QTAILQ_FOREACH(conf, &device_configs, next) {
2478 if (conf->type != type)
2479 continue;
2480 loc_push_restore(&conf->loc);
2481 rc = func(conf->cmdline);
2482 loc_pop(&conf->loc);
2483 if (rc) {
2484 return rc;
2487 return 0;
2490 static int serial_parse(const char *devname)
2492 static int index = 0;
2493 char label[32];
2495 if (strcmp(devname, "none") == 0)
2496 return 0;
2497 if (index == MAX_SERIAL_PORTS) {
2498 error_report("too many serial ports");
2499 exit(1);
2501 snprintf(label, sizeof(label), "serial%d", index);
2502 serial_hds[index] = qemu_chr_new(label, devname, NULL);
2503 if (!serial_hds[index]) {
2504 error_report("could not connect serial device"
2505 " to character backend '%s'", devname);
2506 return -1;
2508 index++;
2509 return 0;
2512 static int parallel_parse(const char *devname)
2514 static int index = 0;
2515 char label[32];
2517 if (strcmp(devname, "none") == 0)
2518 return 0;
2519 if (index == MAX_PARALLEL_PORTS) {
2520 error_report("too many parallel ports");
2521 exit(1);
2523 snprintf(label, sizeof(label), "parallel%d", index);
2524 parallel_hds[index] = qemu_chr_new(label, devname, NULL);
2525 if (!parallel_hds[index]) {
2526 error_report("could not connect parallel device"
2527 " to character backend '%s'", devname);
2528 return -1;
2530 index++;
2531 return 0;
2534 static int virtcon_parse(const char *devname)
2536 QemuOptsList *device = qemu_find_opts("device");
2537 static int index = 0;
2538 char label[32];
2539 QemuOpts *bus_opts, *dev_opts;
2541 if (strcmp(devname, "none") == 0)
2542 return 0;
2543 if (index == MAX_VIRTIO_CONSOLES) {
2544 error_report("too many virtio consoles");
2545 exit(1);
2548 bus_opts = qemu_opts_create(device, NULL, 0, &error_abort);
2549 qemu_opt_set(bus_opts, "driver", "virtio-serial", &error_abort);
2551 dev_opts = qemu_opts_create(device, NULL, 0, &error_abort);
2552 qemu_opt_set(dev_opts, "driver", "virtconsole", &error_abort);
2554 snprintf(label, sizeof(label), "virtcon%d", index);
2555 virtcon_hds[index] = qemu_chr_new(label, devname, NULL);
2556 if (!virtcon_hds[index]) {
2557 error_report("could not connect virtio console"
2558 " to character backend '%s'", devname);
2559 return -1;
2561 qemu_opt_set(dev_opts, "chardev", label, &error_abort);
2563 index++;
2564 return 0;
2567 static int sclp_parse(const char *devname)
2569 QemuOptsList *device = qemu_find_opts("device");
2570 static int index = 0;
2571 char label[32];
2572 QemuOpts *dev_opts;
2574 if (strcmp(devname, "none") == 0) {
2575 return 0;
2577 if (index == MAX_SCLP_CONSOLES) {
2578 error_report("too many sclp consoles");
2579 exit(1);
2582 assert(arch_type == QEMU_ARCH_S390X);
2584 dev_opts = qemu_opts_create(device, NULL, 0, NULL);
2585 qemu_opt_set(dev_opts, "driver", "sclpconsole", &error_abort);
2587 snprintf(label, sizeof(label), "sclpcon%d", index);
2588 sclp_hds[index] = qemu_chr_new(label, devname, NULL);
2589 if (!sclp_hds[index]) {
2590 error_report("could not connect sclp console"
2591 " to character backend '%s'", devname);
2592 return -1;
2594 qemu_opt_set(dev_opts, "chardev", label, &error_abort);
2596 index++;
2597 return 0;
2600 static int debugcon_parse(const char *devname)
2602 QemuOpts *opts;
2604 if (!qemu_chr_new("debugcon", devname, NULL)) {
2605 exit(1);
2607 opts = qemu_opts_create(qemu_find_opts("device"), "debugcon", 1, NULL);
2608 if (!opts) {
2609 error_report("already have a debugcon device");
2610 exit(1);
2612 qemu_opt_set(opts, "driver", "isa-debugcon", &error_abort);
2613 qemu_opt_set(opts, "chardev", "debugcon", &error_abort);
2614 return 0;
2617 static gint machine_class_cmp(gconstpointer a, gconstpointer b)
2619 const MachineClass *mc1 = a, *mc2 = b;
2620 int res;
2622 if (mc1->family == NULL) {
2623 if (mc2->family == NULL) {
2624 /* Compare standalone machine types against each other; they sort
2625 * in increasing order.
2627 return strcmp(object_class_get_name(OBJECT_CLASS(mc1)),
2628 object_class_get_name(OBJECT_CLASS(mc2)));
2631 /* Standalone machine types sort after families. */
2632 return 1;
2635 if (mc2->family == NULL) {
2636 /* Families sort before standalone machine types. */
2637 return -1;
2640 /* Families sort between each other alphabetically increasingly. */
2641 res = strcmp(mc1->family, mc2->family);
2642 if (res != 0) {
2643 return res;
2646 /* Within the same family, machine types sort in decreasing order. */
2647 return strcmp(object_class_get_name(OBJECT_CLASS(mc2)),
2648 object_class_get_name(OBJECT_CLASS(mc1)));
2651 static MachineClass *machine_parse(const char *name)
2653 MachineClass *mc = NULL;
2654 GSList *el, *machines = object_class_get_list(TYPE_MACHINE, false);
2656 if (name) {
2657 mc = find_machine(name);
2659 if (mc) {
2660 g_slist_free(machines);
2661 return mc;
2663 if (name && !is_help_option(name)) {
2664 error_report("unsupported machine type");
2665 error_printf("Use -machine help to list supported machines\n");
2666 } else {
2667 printf("Supported machines are:\n");
2668 machines = g_slist_sort(machines, machine_class_cmp);
2669 for (el = machines; el; el = el->next) {
2670 MachineClass *mc = el->data;
2671 if (mc->alias) {
2672 printf("%-20s %s (alias of %s)\n", mc->alias, mc->desc, mc->name);
2674 printf("%-20s %s%s\n", mc->name, mc->desc,
2675 mc->is_default ? " (default)" : "");
2679 g_slist_free(machines);
2680 exit(!name || !is_help_option(name));
2683 void qemu_add_exit_notifier(Notifier *notify)
2685 notifier_list_add(&exit_notifiers, notify);
2688 void qemu_remove_exit_notifier(Notifier *notify)
2690 notifier_remove(notify);
2693 static void qemu_run_exit_notifiers(void)
2695 notifier_list_notify(&exit_notifiers, NULL);
2698 static bool machine_init_done;
2700 void qemu_add_machine_init_done_notifier(Notifier *notify)
2702 notifier_list_add(&machine_init_done_notifiers, notify);
2703 if (machine_init_done) {
2704 notify->notify(notify, NULL);
2708 static void qemu_run_machine_init_done_notifiers(void)
2710 notifier_list_notify(&machine_init_done_notifiers, NULL);
2711 machine_init_done = true;
2714 static const QEMUOption *lookup_opt(int argc, char **argv,
2715 const char **poptarg, int *poptind)
2717 const QEMUOption *popt;
2718 int optind = *poptind;
2719 char *r = argv[optind];
2720 const char *optarg;
2722 loc_set_cmdline(argv, optind, 1);
2723 optind++;
2724 /* Treat --foo the same as -foo. */
2725 if (r[1] == '-')
2726 r++;
2727 popt = qemu_options;
2728 for(;;) {
2729 if (!popt->name) {
2730 error_report("invalid option");
2731 exit(1);
2733 if (!strcmp(popt->name, r + 1))
2734 break;
2735 popt++;
2737 if (popt->flags & HAS_ARG) {
2738 if (optind >= argc) {
2739 error_report("requires an argument");
2740 exit(1);
2742 optarg = argv[optind++];
2743 loc_set_cmdline(argv, optind - 2, 2);
2744 } else {
2745 optarg = NULL;
2748 *poptarg = optarg;
2749 *poptind = optind;
2751 return popt;
2754 static MachineClass *select_machine(void)
2756 MachineClass *machine_class = find_default_machine();
2757 const char *optarg;
2758 QemuOpts *opts;
2759 Location loc;
2761 loc_push_none(&loc);
2763 opts = qemu_get_machine_opts();
2764 qemu_opts_loc_restore(opts);
2766 optarg = qemu_opt_get(opts, "type");
2767 if (optarg) {
2768 machine_class = machine_parse(optarg);
2771 if (!machine_class) {
2772 error_report("No machine specified, and there is no default");
2773 error_printf("Use -machine help to list supported machines\n");
2774 exit(1);
2777 loc_pop(&loc);
2778 return machine_class;
2781 static int machine_set_property(void *opaque,
2782 const char *name, const char *value,
2783 Error **errp)
2785 Object *obj = OBJECT(opaque);
2786 Error *local_err = NULL;
2787 char *c, *qom_name;
2789 if (strcmp(name, "type") == 0) {
2790 return 0;
2793 qom_name = g_strdup(name);
2794 c = qom_name;
2795 while (*c++) {
2796 if (*c == '_') {
2797 *c = '-';
2801 object_property_parse(obj, value, qom_name, &local_err);
2802 g_free(qom_name);
2804 if (local_err) {
2805 error_report_err(local_err);
2806 return -1;
2809 return 0;
2814 * Initial object creation happens before all other
2815 * QEMU data types are created. The majority of objects
2816 * can be created at this point. The rng-egd object
2817 * cannot be created here, as it depends on the chardev
2818 * already existing.
2820 static bool object_create_initial(const char *type)
2822 if (g_str_equal(type, "rng-egd")) {
2823 return false;
2827 * return false for concrete netfilters since
2828 * they depend on netdevs already existing
2830 if (g_str_equal(type, "filter-buffer") ||
2831 g_str_equal(type, "filter-dump") ||
2832 g_str_equal(type, "filter-mirror") ||
2833 g_str_equal(type, "filter-redirector")) {
2834 return false;
2837 return true;
2842 * The remainder of object creation happens after the
2843 * creation of chardev, fsdev, net clients and device data types.
2845 static bool object_create_delayed(const char *type)
2847 return !object_create_initial(type);
2851 static void set_memory_options(uint64_t *ram_slots, ram_addr_t *maxram_size,
2852 MachineClass *mc)
2854 uint64_t sz;
2855 const char *mem_str;
2856 const char *maxmem_str, *slots_str;
2857 const ram_addr_t default_ram_size = mc->default_ram_size;
2858 QemuOpts *opts = qemu_find_opts_singleton("memory");
2859 Location loc;
2861 loc_push_none(&loc);
2862 qemu_opts_loc_restore(opts);
2864 sz = 0;
2865 mem_str = qemu_opt_get(opts, "size");
2866 if (mem_str) {
2867 if (!*mem_str) {
2868 error_report("missing 'size' option value");
2869 exit(EXIT_FAILURE);
2872 sz = qemu_opt_get_size(opts, "size", ram_size);
2874 /* Fix up legacy suffix-less format */
2875 if (g_ascii_isdigit(mem_str[strlen(mem_str) - 1])) {
2876 uint64_t overflow_check = sz;
2878 sz <<= 20;
2879 if ((sz >> 20) != overflow_check) {
2880 error_report("too large 'size' option value");
2881 exit(EXIT_FAILURE);
2886 /* backward compatibility behaviour for case "-m 0" */
2887 if (sz == 0) {
2888 sz = default_ram_size;
2891 sz = QEMU_ALIGN_UP(sz, 8192);
2892 ram_size = sz;
2893 if (ram_size != sz) {
2894 error_report("ram size too large");
2895 exit(EXIT_FAILURE);
2898 /* store value for the future use */
2899 qemu_opt_set_number(opts, "size", ram_size, &error_abort);
2900 *maxram_size = ram_size;
2902 maxmem_str = qemu_opt_get(opts, "maxmem");
2903 slots_str = qemu_opt_get(opts, "slots");
2904 if (maxmem_str && slots_str) {
2905 uint64_t slots;
2907 sz = qemu_opt_get_size(opts, "maxmem", 0);
2908 slots = qemu_opt_get_number(opts, "slots", 0);
2909 if (sz < ram_size) {
2910 error_report("invalid value of -m option maxmem: "
2911 "maximum memory size (0x%" PRIx64 ") must be at least "
2912 "the initial memory size (0x" RAM_ADDR_FMT ")",
2913 sz, ram_size);
2914 exit(EXIT_FAILURE);
2915 } else if (sz > ram_size) {
2916 if (!slots) {
2917 error_report("invalid value of -m option: maxmem was "
2918 "specified, but no hotplug slots were specified");
2919 exit(EXIT_FAILURE);
2921 } else if (slots) {
2922 error_report("invalid value of -m option maxmem: "
2923 "memory slots were specified but maximum memory size "
2924 "(0x%" PRIx64 ") is equal to the initial memory size "
2925 "(0x" RAM_ADDR_FMT ")", sz, ram_size);
2926 exit(EXIT_FAILURE);
2929 *maxram_size = sz;
2930 *ram_slots = slots;
2931 } else if ((!maxmem_str && slots_str) ||
2932 (maxmem_str && !slots_str)) {
2933 error_report("invalid -m option value: missing "
2934 "'%s' option", slots_str ? "maxmem" : "slots");
2935 exit(EXIT_FAILURE);
2938 loc_pop(&loc);
2941 int main(int argc, char **argv, char **envp)
2943 int i;
2944 int snapshot, linux_boot;
2945 const char *initrd_filename;
2946 const char *kernel_filename, *kernel_cmdline;
2947 const char *boot_order = NULL;
2948 const char *boot_once = NULL;
2949 DisplayState *ds;
2950 int cyls, heads, secs, translation;
2951 QemuOpts *hda_opts = NULL, *opts, *machine_opts, *icount_opts = NULL;
2952 QemuOptsList *olist;
2953 int optind;
2954 const char *optarg;
2955 const char *loadvm = NULL;
2956 MachineClass *machine_class;
2957 const char *cpu_model;
2958 const char *vga_model = NULL;
2959 const char *qtest_chrdev = NULL;
2960 const char *qtest_log = NULL;
2961 const char *pid_file = NULL;
2962 const char *incoming = NULL;
2963 int show_vnc_port = 0;
2964 bool defconfig = true;
2965 bool userconfig = true;
2966 bool nographic = false;
2967 DisplayType display_type = DT_DEFAULT;
2968 int display_remote = 0;
2969 const char *log_mask = NULL;
2970 const char *log_file = NULL;
2971 char *trace_file = NULL;
2972 ram_addr_t maxram_size;
2973 uint64_t ram_slots = 0;
2974 FILE *vmstate_dump_file = NULL;
2975 Error *main_loop_err = NULL;
2976 Error *err = NULL;
2978 qemu_init_cpu_loop();
2979 qemu_mutex_lock_iothread();
2981 atexit(qemu_run_exit_notifiers);
2982 error_set_progname(argv[0]);
2983 qemu_init_exec_dir(argv[0]);
2985 module_call_init(MODULE_INIT_QOM);
2987 qemu_add_opts(&qemu_drive_opts);
2988 qemu_add_drive_opts(&qemu_legacy_drive_opts);
2989 qemu_add_drive_opts(&qemu_common_drive_opts);
2990 qemu_add_drive_opts(&qemu_drive_opts);
2991 qemu_add_opts(&qemu_chardev_opts);
2992 qemu_add_opts(&qemu_device_opts);
2993 qemu_add_opts(&qemu_netdev_opts);
2994 qemu_add_opts(&qemu_net_opts);
2995 qemu_add_opts(&qemu_rtc_opts);
2996 qemu_add_opts(&qemu_global_opts);
2997 qemu_add_opts(&qemu_mon_opts);
2998 qemu_add_opts(&qemu_trace_opts);
2999 qemu_add_opts(&qemu_option_rom_opts);
3000 qemu_add_opts(&qemu_machine_opts);
3001 qemu_add_opts(&qemu_mem_opts);
3002 qemu_add_opts(&qemu_smp_opts);
3003 qemu_add_opts(&qemu_boot_opts);
3004 qemu_add_opts(&qemu_sandbox_opts);
3005 qemu_add_opts(&qemu_add_fd_opts);
3006 qemu_add_opts(&qemu_object_opts);
3007 qemu_add_opts(&qemu_tpmdev_opts);
3008 qemu_add_opts(&qemu_realtime_opts);
3009 qemu_add_opts(&qemu_msg_opts);
3010 qemu_add_opts(&qemu_name_opts);
3011 qemu_add_opts(&qemu_numa_opts);
3012 qemu_add_opts(&qemu_icount_opts);
3013 qemu_add_opts(&qemu_semihosting_config_opts);
3014 qemu_add_opts(&qemu_fw_cfg_opts);
3015 module_call_init(MODULE_INIT_OPTS);
3017 runstate_init();
3019 if (qcrypto_init(&err) < 0) {
3020 error_reportf_err(err, "cannot initialize crypto: ");
3021 exit(1);
3023 rtc_clock = QEMU_CLOCK_HOST;
3025 QLIST_INIT (&vm_change_state_head);
3026 os_setup_early_signal_handling();
3028 cpu_model = NULL;
3029 snapshot = 0;
3030 cyls = heads = secs = 0;
3031 translation = BIOS_ATA_TRANSLATION_AUTO;
3033 nb_nics = 0;
3035 bdrv_init_with_whitelist();
3037 autostart = 1;
3039 /* first pass of option parsing */
3040 optind = 1;
3041 while (optind < argc) {
3042 if (argv[optind][0] != '-') {
3043 /* disk image */
3044 optind++;
3045 } else {
3046 const QEMUOption *popt;
3048 popt = lookup_opt(argc, argv, &optarg, &optind);
3049 switch (popt->index) {
3050 case QEMU_OPTION_nodefconfig:
3051 defconfig = false;
3052 break;
3053 case QEMU_OPTION_nouserconfig:
3054 userconfig = false;
3055 break;
3060 if (defconfig) {
3061 int ret;
3062 ret = qemu_read_default_config_files(userconfig);
3063 if (ret < 0) {
3064 exit(1);
3068 /* second pass of option parsing */
3069 optind = 1;
3070 for(;;) {
3071 if (optind >= argc)
3072 break;
3073 if (argv[optind][0] != '-') {
3074 hda_opts = drive_add(IF_DEFAULT, 0, argv[optind++], HD_OPTS);
3075 } else {
3076 const QEMUOption *popt;
3078 popt = lookup_opt(argc, argv, &optarg, &optind);
3079 if (!(popt->arch_mask & arch_type)) {
3080 printf("Option %s not supported for this target\n", popt->name);
3081 exit(1);
3083 switch(popt->index) {
3084 case QEMU_OPTION_no_kvm_irqchip: {
3085 olist = qemu_find_opts("machine");
3086 qemu_opts_parse_noisily(olist, "kernel_irqchip=off", false);
3087 break;
3089 case QEMU_OPTION_cpu:
3090 /* hw initialization will check this */
3091 cpu_model = optarg;
3092 break;
3093 case QEMU_OPTION_hda:
3095 char buf[256];
3096 if (cyls == 0)
3097 snprintf(buf, sizeof(buf), "%s", HD_OPTS);
3098 else
3099 snprintf(buf, sizeof(buf),
3100 "%s,cyls=%d,heads=%d,secs=%d%s",
3101 HD_OPTS , cyls, heads, secs,
3102 translation == BIOS_ATA_TRANSLATION_LBA ?
3103 ",trans=lba" :
3104 translation == BIOS_ATA_TRANSLATION_NONE ?
3105 ",trans=none" : "");
3106 drive_add(IF_DEFAULT, 0, optarg, buf);
3107 break;
3109 case QEMU_OPTION_hdb:
3110 case QEMU_OPTION_hdc:
3111 case QEMU_OPTION_hdd:
3112 drive_add(IF_DEFAULT, popt->index - QEMU_OPTION_hda, optarg,
3113 HD_OPTS);
3114 break;
3115 case QEMU_OPTION_drive:
3116 if (drive_def(optarg) == NULL) {
3117 exit(1);
3119 break;
3120 case QEMU_OPTION_set:
3121 if (qemu_set_option(optarg) != 0)
3122 exit(1);
3123 break;
3124 case QEMU_OPTION_global:
3125 if (qemu_global_option(optarg) != 0)
3126 exit(1);
3127 break;
3128 case QEMU_OPTION_mtdblock:
3129 drive_add(IF_MTD, -1, optarg, MTD_OPTS);
3130 break;
3131 case QEMU_OPTION_sd:
3132 drive_add(IF_SD, -1, optarg, SD_OPTS);
3133 break;
3134 case QEMU_OPTION_pflash:
3135 drive_add(IF_PFLASH, -1, optarg, PFLASH_OPTS);
3136 break;
3137 case QEMU_OPTION_snapshot:
3138 snapshot = 1;
3139 break;
3140 case QEMU_OPTION_hdachs:
3142 const char *p;
3143 p = optarg;
3144 cyls = strtol(p, (char **)&p, 0);
3145 if (cyls < 1 || cyls > 16383)
3146 goto chs_fail;
3147 if (*p != ',')
3148 goto chs_fail;
3149 p++;
3150 heads = strtol(p, (char **)&p, 0);
3151 if (heads < 1 || heads > 16)
3152 goto chs_fail;
3153 if (*p != ',')
3154 goto chs_fail;
3155 p++;
3156 secs = strtol(p, (char **)&p, 0);
3157 if (secs < 1 || secs > 63)
3158 goto chs_fail;
3159 if (*p == ',') {
3160 p++;
3161 if (!strcmp(p, "large")) {
3162 translation = BIOS_ATA_TRANSLATION_LARGE;
3163 } else if (!strcmp(p, "rechs")) {
3164 translation = BIOS_ATA_TRANSLATION_RECHS;
3165 } else if (!strcmp(p, "none")) {
3166 translation = BIOS_ATA_TRANSLATION_NONE;
3167 } else if (!strcmp(p, "lba")) {
3168 translation = BIOS_ATA_TRANSLATION_LBA;
3169 } else if (!strcmp(p, "auto")) {
3170 translation = BIOS_ATA_TRANSLATION_AUTO;
3171 } else {
3172 goto chs_fail;
3174 } else if (*p != '\0') {
3175 chs_fail:
3176 error_report("invalid physical CHS format");
3177 exit(1);
3179 if (hda_opts != NULL) {
3180 qemu_opt_set_number(hda_opts, "cyls", cyls,
3181 &error_abort);
3182 qemu_opt_set_number(hda_opts, "heads", heads,
3183 &error_abort);
3184 qemu_opt_set_number(hda_opts, "secs", secs,
3185 &error_abort);
3186 if (translation == BIOS_ATA_TRANSLATION_LARGE) {
3187 qemu_opt_set(hda_opts, "trans", "large",
3188 &error_abort);
3189 } else if (translation == BIOS_ATA_TRANSLATION_RECHS) {
3190 qemu_opt_set(hda_opts, "trans", "rechs",
3191 &error_abort);
3192 } else if (translation == BIOS_ATA_TRANSLATION_LBA) {
3193 qemu_opt_set(hda_opts, "trans", "lba",
3194 &error_abort);
3195 } else if (translation == BIOS_ATA_TRANSLATION_NONE) {
3196 qemu_opt_set(hda_opts, "trans", "none",
3197 &error_abort);
3201 break;
3202 case QEMU_OPTION_numa:
3203 opts = qemu_opts_parse_noisily(qemu_find_opts("numa"),
3204 optarg, true);
3205 if (!opts) {
3206 exit(1);
3208 break;
3209 case QEMU_OPTION_display:
3210 display_type = select_display(optarg);
3211 break;
3212 case QEMU_OPTION_nographic:
3213 olist = qemu_find_opts("machine");
3214 qemu_opts_parse_noisily(olist, "graphics=off", false);
3215 nographic = true;
3216 display_type = DT_NONE;
3217 break;
3218 case QEMU_OPTION_curses:
3219 #ifdef CONFIG_CURSES
3220 display_type = DT_CURSES;
3221 #else
3222 error_report("curses support is disabled");
3223 exit(1);
3224 #endif
3225 break;
3226 case QEMU_OPTION_portrait:
3227 graphic_rotate = 90;
3228 break;
3229 case QEMU_OPTION_rotate:
3230 graphic_rotate = strtol(optarg, (char **) &optarg, 10);
3231 if (graphic_rotate != 0 && graphic_rotate != 90 &&
3232 graphic_rotate != 180 && graphic_rotate != 270) {
3233 error_report("only 90, 180, 270 deg rotation is available");
3234 exit(1);
3236 break;
3237 case QEMU_OPTION_kernel:
3238 qemu_opts_set(qemu_find_opts("machine"), 0, "kernel", optarg,
3239 &error_abort);
3240 break;
3241 case QEMU_OPTION_initrd:
3242 qemu_opts_set(qemu_find_opts("machine"), 0, "initrd", optarg,
3243 &error_abort);
3244 break;
3245 case QEMU_OPTION_append:
3246 qemu_opts_set(qemu_find_opts("machine"), 0, "append", optarg,
3247 &error_abort);
3248 break;
3249 case QEMU_OPTION_dtb:
3250 qemu_opts_set(qemu_find_opts("machine"), 0, "dtb", optarg,
3251 &error_abort);
3252 break;
3253 case QEMU_OPTION_cdrom:
3254 drive_add(IF_DEFAULT, 2, optarg, CDROM_OPTS);
3255 break;
3256 case QEMU_OPTION_boot:
3257 opts = qemu_opts_parse_noisily(qemu_find_opts("boot-opts"),
3258 optarg, true);
3259 if (!opts) {
3260 exit(1);
3262 break;
3263 case QEMU_OPTION_fda:
3264 case QEMU_OPTION_fdb:
3265 drive_add(IF_FLOPPY, popt->index - QEMU_OPTION_fda,
3266 optarg, FD_OPTS);
3267 break;
3268 case QEMU_OPTION_no_fd_bootchk:
3269 fd_bootchk = 0;
3270 break;
3271 case QEMU_OPTION_netdev:
3272 default_net = 0;
3273 if (net_client_parse(qemu_find_opts("netdev"), optarg) == -1) {
3274 exit(1);
3276 break;
3277 case QEMU_OPTION_net:
3278 default_net = 0;
3279 if (net_client_parse(qemu_find_opts("net"), optarg) == -1) {
3280 exit(1);
3282 break;
3283 #ifdef CONFIG_LIBISCSI
3284 case QEMU_OPTION_iscsi:
3285 opts = qemu_opts_parse_noisily(qemu_find_opts("iscsi"),
3286 optarg, false);
3287 if (!opts) {
3288 exit(1);
3290 break;
3291 #endif
3292 #ifdef CONFIG_SLIRP
3293 case QEMU_OPTION_tftp:
3294 error_report("The -tftp option is deprecated. "
3295 "Please use '-netdev user,tftp=...' instead.");
3296 legacy_tftp_prefix = optarg;
3297 break;
3298 case QEMU_OPTION_bootp:
3299 error_report("The -bootp option is deprecated. "
3300 "Please use '-netdev user,bootfile=...' instead.");
3301 legacy_bootp_filename = optarg;
3302 break;
3303 case QEMU_OPTION_redir:
3304 error_report("The -redir option is deprecated. "
3305 "Please use '-netdev user,hostfwd=...' instead.");
3306 if (net_slirp_redir(optarg) < 0)
3307 exit(1);
3308 break;
3309 #endif
3310 case QEMU_OPTION_bt:
3311 add_device_config(DEV_BT, optarg);
3312 break;
3313 case QEMU_OPTION_audio_help:
3314 AUD_help ();
3315 exit (0);
3316 break;
3317 case QEMU_OPTION_soundhw:
3318 select_soundhw (optarg);
3319 break;
3320 case QEMU_OPTION_h:
3321 help(0);
3322 break;
3323 case QEMU_OPTION_version:
3324 version();
3325 exit(0);
3326 break;
3327 case QEMU_OPTION_m:
3328 opts = qemu_opts_parse_noisily(qemu_find_opts("memory"),
3329 optarg, true);
3330 if (!opts) {
3331 exit(EXIT_FAILURE);
3333 break;
3334 #ifdef CONFIG_TPM
3335 case QEMU_OPTION_tpmdev:
3336 if (tpm_config_parse(qemu_find_opts("tpmdev"), optarg) < 0) {
3337 exit(1);
3339 break;
3340 #endif
3341 case QEMU_OPTION_mempath:
3342 mem_path = optarg;
3343 break;
3344 case QEMU_OPTION_mem_prealloc:
3345 mem_prealloc = 1;
3346 break;
3347 case QEMU_OPTION_d:
3348 log_mask = optarg;
3349 break;
3350 case QEMU_OPTION_D:
3351 log_file = optarg;
3352 break;
3353 case QEMU_OPTION_DFILTER:
3354 qemu_set_dfilter_ranges(optarg);
3355 break;
3356 case QEMU_OPTION_s:
3357 add_device_config(DEV_GDB, "tcp::" DEFAULT_GDBSTUB_PORT);
3358 break;
3359 case QEMU_OPTION_gdb:
3360 add_device_config(DEV_GDB, optarg);
3361 break;
3362 case QEMU_OPTION_L:
3363 if (data_dir_idx < ARRAY_SIZE(data_dir)) {
3364 data_dir[data_dir_idx++] = optarg;
3366 break;
3367 case QEMU_OPTION_bios:
3368 qemu_opts_set(qemu_find_opts("machine"), 0, "firmware", optarg,
3369 &error_abort);
3370 break;
3371 case QEMU_OPTION_singlestep:
3372 singlestep = 1;
3373 break;
3374 case QEMU_OPTION_S:
3375 autostart = 0;
3376 break;
3377 case QEMU_OPTION_k:
3378 keyboard_layout = optarg;
3379 break;
3380 case QEMU_OPTION_localtime:
3381 rtc_utc = 0;
3382 break;
3383 case QEMU_OPTION_vga:
3384 vga_model = optarg;
3385 default_vga = 0;
3386 break;
3387 case QEMU_OPTION_g:
3389 const char *p;
3390 int w, h, depth;
3391 p = optarg;
3392 w = strtol(p, (char **)&p, 10);
3393 if (w <= 0) {
3394 graphic_error:
3395 error_report("invalid resolution or depth");
3396 exit(1);
3398 if (*p != 'x')
3399 goto graphic_error;
3400 p++;
3401 h = strtol(p, (char **)&p, 10);
3402 if (h <= 0)
3403 goto graphic_error;
3404 if (*p == 'x') {
3405 p++;
3406 depth = strtol(p, (char **)&p, 10);
3407 if (depth != 8 && depth != 15 && depth != 16 &&
3408 depth != 24 && depth != 32)
3409 goto graphic_error;
3410 } else if (*p == '\0') {
3411 depth = graphic_depth;
3412 } else {
3413 goto graphic_error;
3416 graphic_width = w;
3417 graphic_height = h;
3418 graphic_depth = depth;
3420 break;
3421 case QEMU_OPTION_echr:
3423 char *r;
3424 term_escape_char = strtol(optarg, &r, 0);
3425 if (r == optarg)
3426 printf("Bad argument to echr\n");
3427 break;
3429 case QEMU_OPTION_monitor:
3430 default_monitor = 0;
3431 if (strncmp(optarg, "none", 4)) {
3432 monitor_parse(optarg, "readline", false);
3434 break;
3435 case QEMU_OPTION_qmp:
3436 monitor_parse(optarg, "control", false);
3437 default_monitor = 0;
3438 break;
3439 case QEMU_OPTION_qmp_pretty:
3440 monitor_parse(optarg, "control", true);
3441 default_monitor = 0;
3442 break;
3443 case QEMU_OPTION_mon:
3444 opts = qemu_opts_parse_noisily(qemu_find_opts("mon"), optarg,
3445 true);
3446 if (!opts) {
3447 exit(1);
3449 default_monitor = 0;
3450 break;
3451 case QEMU_OPTION_chardev:
3452 opts = qemu_opts_parse_noisily(qemu_find_opts("chardev"),
3453 optarg, true);
3454 if (!opts) {
3455 exit(1);
3457 break;
3458 case QEMU_OPTION_fsdev:
3459 olist = qemu_find_opts("fsdev");
3460 if (!olist) {
3461 error_report("fsdev support is disabled");
3462 exit(1);
3464 opts = qemu_opts_parse_noisily(olist, optarg, true);
3465 if (!opts) {
3466 exit(1);
3468 break;
3469 case QEMU_OPTION_virtfs: {
3470 QemuOpts *fsdev;
3471 QemuOpts *device;
3472 const char *writeout, *sock_fd, *socket;
3474 olist = qemu_find_opts("virtfs");
3475 if (!olist) {
3476 error_report("virtfs support is disabled");
3477 exit(1);
3479 opts = qemu_opts_parse_noisily(olist, optarg, true);
3480 if (!opts) {
3481 exit(1);
3484 if (qemu_opt_get(opts, "fsdriver") == NULL ||
3485 qemu_opt_get(opts, "mount_tag") == NULL) {
3486 error_report("Usage: -virtfs fsdriver,mount_tag=tag");
3487 exit(1);
3489 fsdev = qemu_opts_create(qemu_find_opts("fsdev"),
3490 qemu_opt_get(opts, "mount_tag"),
3491 1, NULL);
3492 if (!fsdev) {
3493 error_report("duplicate fsdev id: %s",
3494 qemu_opt_get(opts, "mount_tag"));
3495 exit(1);
3498 writeout = qemu_opt_get(opts, "writeout");
3499 if (writeout) {
3500 #ifdef CONFIG_SYNC_FILE_RANGE
3501 qemu_opt_set(fsdev, "writeout", writeout, &error_abort);
3502 #else
3503 error_report("writeout=immediate not supported "
3504 "on this platform");
3505 exit(1);
3506 #endif
3508 qemu_opt_set(fsdev, "fsdriver",
3509 qemu_opt_get(opts, "fsdriver"), &error_abort);
3510 qemu_opt_set(fsdev, "path", qemu_opt_get(opts, "path"),
3511 &error_abort);
3512 qemu_opt_set(fsdev, "security_model",
3513 qemu_opt_get(opts, "security_model"),
3514 &error_abort);
3515 socket = qemu_opt_get(opts, "socket");
3516 if (socket) {
3517 qemu_opt_set(fsdev, "socket", socket, &error_abort);
3519 sock_fd = qemu_opt_get(opts, "sock_fd");
3520 if (sock_fd) {
3521 qemu_opt_set(fsdev, "sock_fd", sock_fd, &error_abort);
3524 qemu_opt_set_bool(fsdev, "readonly",
3525 qemu_opt_get_bool(opts, "readonly", 0),
3526 &error_abort);
3527 device = qemu_opts_create(qemu_find_opts("device"), NULL, 0,
3528 &error_abort);
3529 qemu_opt_set(device, "driver", "virtio-9p-pci", &error_abort);
3530 qemu_opt_set(device, "fsdev",
3531 qemu_opt_get(opts, "mount_tag"), &error_abort);
3532 qemu_opt_set(device, "mount_tag",
3533 qemu_opt_get(opts, "mount_tag"), &error_abort);
3534 break;
3536 case QEMU_OPTION_virtfs_synth: {
3537 QemuOpts *fsdev;
3538 QemuOpts *device;
3540 fsdev = qemu_opts_create(qemu_find_opts("fsdev"), "v_synth",
3541 1, NULL);
3542 if (!fsdev) {
3543 error_report("duplicate option: %s", "virtfs_synth");
3544 exit(1);
3546 qemu_opt_set(fsdev, "fsdriver", "synth", &error_abort);
3548 device = qemu_opts_create(qemu_find_opts("device"), NULL, 0,
3549 &error_abort);
3550 qemu_opt_set(device, "driver", "virtio-9p-pci", &error_abort);
3551 qemu_opt_set(device, "fsdev", "v_synth", &error_abort);
3552 qemu_opt_set(device, "mount_tag", "v_synth", &error_abort);
3553 break;
3555 case QEMU_OPTION_serial:
3556 add_device_config(DEV_SERIAL, optarg);
3557 default_serial = 0;
3558 if (strncmp(optarg, "mon:", 4) == 0) {
3559 default_monitor = 0;
3561 break;
3562 case QEMU_OPTION_watchdog:
3563 if (watchdog) {
3564 error_report("only one watchdog option may be given");
3565 return 1;
3567 watchdog = optarg;
3568 break;
3569 case QEMU_OPTION_watchdog_action:
3570 if (select_watchdog_action(optarg) == -1) {
3571 error_report("unknown -watchdog-action parameter");
3572 exit(1);
3574 break;
3575 case QEMU_OPTION_virtiocon:
3576 add_device_config(DEV_VIRTCON, optarg);
3577 default_virtcon = 0;
3578 if (strncmp(optarg, "mon:", 4) == 0) {
3579 default_monitor = 0;
3581 break;
3582 case QEMU_OPTION_parallel:
3583 add_device_config(DEV_PARALLEL, optarg);
3584 default_parallel = 0;
3585 if (strncmp(optarg, "mon:", 4) == 0) {
3586 default_monitor = 0;
3588 break;
3589 case QEMU_OPTION_debugcon:
3590 add_device_config(DEV_DEBUGCON, optarg);
3591 break;
3592 case QEMU_OPTION_loadvm:
3593 loadvm = optarg;
3594 break;
3595 case QEMU_OPTION_full_screen:
3596 full_screen = 1;
3597 break;
3598 case QEMU_OPTION_no_frame:
3599 no_frame = 1;
3600 break;
3601 case QEMU_OPTION_alt_grab:
3602 alt_grab = 1;
3603 break;
3604 case QEMU_OPTION_ctrl_grab:
3605 ctrl_grab = 1;
3606 break;
3607 case QEMU_OPTION_no_quit:
3608 no_quit = 1;
3609 break;
3610 case QEMU_OPTION_sdl:
3611 #ifdef CONFIG_SDL
3612 display_type = DT_SDL;
3613 break;
3614 #else
3615 error_report("SDL support is disabled");
3616 exit(1);
3617 #endif
3618 case QEMU_OPTION_pidfile:
3619 pid_file = optarg;
3620 break;
3621 case QEMU_OPTION_win2k_hack:
3622 win2k_install_hack = 1;
3623 break;
3624 case QEMU_OPTION_rtc_td_hack: {
3625 static GlobalProperty slew_lost_ticks = {
3626 .driver = "mc146818rtc",
3627 .property = "lost_tick_policy",
3628 .value = "slew",
3631 qdev_prop_register_global(&slew_lost_ticks);
3632 break;
3634 case QEMU_OPTION_acpitable:
3635 opts = qemu_opts_parse_noisily(qemu_find_opts("acpi"),
3636 optarg, true);
3637 if (!opts) {
3638 exit(1);
3640 do_acpitable_option(opts);
3641 break;
3642 case QEMU_OPTION_smbios:
3643 opts = qemu_opts_parse_noisily(qemu_find_opts("smbios"),
3644 optarg, false);
3645 if (!opts) {
3646 exit(1);
3648 do_smbios_option(opts);
3649 break;
3650 case QEMU_OPTION_fwcfg:
3651 opts = qemu_opts_parse_noisily(qemu_find_opts("fw_cfg"),
3652 optarg, true);
3653 if (opts == NULL) {
3654 exit(1);
3656 break;
3657 case QEMU_OPTION_enable_kvm:
3658 olist = qemu_find_opts("machine");
3659 qemu_opts_parse_noisily(olist, "accel=kvm", false);
3660 break;
3661 case QEMU_OPTION_M:
3662 case QEMU_OPTION_machine:
3663 olist = qemu_find_opts("machine");
3664 opts = qemu_opts_parse_noisily(olist, optarg, true);
3665 if (!opts) {
3666 exit(1);
3668 break;
3669 case QEMU_OPTION_no_kvm:
3670 olist = qemu_find_opts("machine");
3671 qemu_opts_parse_noisily(olist, "accel=tcg", false);
3672 break;
3673 case QEMU_OPTION_no_kvm_pit: {
3674 error_report("warning: ignoring deprecated option");
3675 break;
3677 case QEMU_OPTION_no_kvm_pit_reinjection: {
3678 static GlobalProperty kvm_pit_lost_tick_policy = {
3679 .driver = "kvm-pit",
3680 .property = "lost_tick_policy",
3681 .value = "discard",
3684 error_report("warning: deprecated, replaced by "
3685 "-global kvm-pit.lost_tick_policy=discard");
3686 qdev_prop_register_global(&kvm_pit_lost_tick_policy);
3687 break;
3689 case QEMU_OPTION_usb:
3690 olist = qemu_find_opts("machine");
3691 qemu_opts_parse_noisily(olist, "usb=on", false);
3692 break;
3693 case QEMU_OPTION_usbdevice:
3694 olist = qemu_find_opts("machine");
3695 qemu_opts_parse_noisily(olist, "usb=on", false);
3696 add_device_config(DEV_USB, optarg);
3697 break;
3698 case QEMU_OPTION_device:
3699 if (!qemu_opts_parse_noisily(qemu_find_opts("device"),
3700 optarg, true)) {
3701 exit(1);
3703 break;
3704 case QEMU_OPTION_smp:
3705 if (!qemu_opts_parse_noisily(qemu_find_opts("smp-opts"),
3706 optarg, true)) {
3707 exit(1);
3709 break;
3710 case QEMU_OPTION_vnc:
3711 vnc_parse(optarg, &error_fatal);
3712 break;
3713 case QEMU_OPTION_no_acpi:
3714 acpi_enabled = 0;
3715 break;
3716 case QEMU_OPTION_no_hpet:
3717 no_hpet = 1;
3718 break;
3719 case QEMU_OPTION_balloon:
3720 if (balloon_parse(optarg) < 0) {
3721 error_report("unknown -balloon argument %s", optarg);
3722 exit(1);
3724 break;
3725 case QEMU_OPTION_no_reboot:
3726 no_reboot = 1;
3727 break;
3728 case QEMU_OPTION_no_shutdown:
3729 no_shutdown = 1;
3730 break;
3731 case QEMU_OPTION_show_cursor:
3732 cursor_hide = 0;
3733 break;
3734 case QEMU_OPTION_uuid:
3735 if(qemu_uuid_parse(optarg, qemu_uuid) < 0) {
3736 error_report("failed to parse UUID string: wrong format");
3737 exit(1);
3739 qemu_uuid_set = true;
3740 break;
3741 case QEMU_OPTION_option_rom:
3742 if (nb_option_roms >= MAX_OPTION_ROMS) {
3743 error_report("too many option ROMs");
3744 exit(1);
3746 opts = qemu_opts_parse_noisily(qemu_find_opts("option-rom"),
3747 optarg, true);
3748 if (!opts) {
3749 exit(1);
3751 option_rom[nb_option_roms].name = qemu_opt_get(opts, "romfile");
3752 option_rom[nb_option_roms].bootindex =
3753 qemu_opt_get_number(opts, "bootindex", -1);
3754 if (!option_rom[nb_option_roms].name) {
3755 error_report("Option ROM file is not specified");
3756 exit(1);
3758 nb_option_roms++;
3759 break;
3760 case QEMU_OPTION_semihosting:
3761 semihosting.enabled = true;
3762 semihosting.target = SEMIHOSTING_TARGET_AUTO;
3763 break;
3764 case QEMU_OPTION_semihosting_config:
3765 semihosting.enabled = true;
3766 opts = qemu_opts_parse_noisily(qemu_find_opts("semihosting-config"),
3767 optarg, false);
3768 if (opts != NULL) {
3769 semihosting.enabled = qemu_opt_get_bool(opts, "enable",
3770 true);
3771 const char *target = qemu_opt_get(opts, "target");
3772 if (target != NULL) {
3773 if (strcmp("native", target) == 0) {
3774 semihosting.target = SEMIHOSTING_TARGET_NATIVE;
3775 } else if (strcmp("gdb", target) == 0) {
3776 semihosting.target = SEMIHOSTING_TARGET_GDB;
3777 } else if (strcmp("auto", target) == 0) {
3778 semihosting.target = SEMIHOSTING_TARGET_AUTO;
3779 } else {
3780 error_report("unsupported semihosting-config %s",
3781 optarg);
3782 exit(1);
3784 } else {
3785 semihosting.target = SEMIHOSTING_TARGET_AUTO;
3787 /* Set semihosting argument count and vector */
3788 qemu_opt_foreach(opts, add_semihosting_arg,
3789 &semihosting, NULL);
3790 } else {
3791 error_report("unsupported semihosting-config %s", optarg);
3792 exit(1);
3794 break;
3795 case QEMU_OPTION_tdf:
3796 error_report("warning: ignoring deprecated option");
3797 break;
3798 case QEMU_OPTION_name:
3799 opts = qemu_opts_parse_noisily(qemu_find_opts("name"),
3800 optarg, true);
3801 if (!opts) {
3802 exit(1);
3804 break;
3805 case QEMU_OPTION_prom_env:
3806 if (nb_prom_envs >= MAX_PROM_ENVS) {
3807 error_report("too many prom variables");
3808 exit(1);
3810 prom_envs[nb_prom_envs] = optarg;
3811 nb_prom_envs++;
3812 break;
3813 case QEMU_OPTION_old_param:
3814 old_param = 1;
3815 break;
3816 case QEMU_OPTION_clock:
3817 /* Clock options no longer exist. Keep this option for
3818 * backward compatibility.
3820 break;
3821 case QEMU_OPTION_startdate:
3822 configure_rtc_date_offset(optarg, 1);
3823 break;
3824 case QEMU_OPTION_rtc:
3825 opts = qemu_opts_parse_noisily(qemu_find_opts("rtc"), optarg,
3826 false);
3827 if (!opts) {
3828 exit(1);
3830 configure_rtc(opts);
3831 break;
3832 case QEMU_OPTION_tb_size:
3833 tcg_tb_size = strtol(optarg, NULL, 0);
3834 if (tcg_tb_size < 0) {
3835 tcg_tb_size = 0;
3837 break;
3838 case QEMU_OPTION_icount:
3839 icount_opts = qemu_opts_parse_noisily(qemu_find_opts("icount"),
3840 optarg, true);
3841 if (!icount_opts) {
3842 exit(1);
3844 break;
3845 case QEMU_OPTION_incoming:
3846 if (!incoming) {
3847 runstate_set(RUN_STATE_INMIGRATE);
3849 incoming = optarg;
3850 break;
3851 case QEMU_OPTION_nodefaults:
3852 has_defaults = 0;
3853 break;
3854 case QEMU_OPTION_xen_domid:
3855 if (!(xen_available())) {
3856 printf("Option %s not supported for this target\n", popt->name);
3857 exit(1);
3859 xen_domid = atoi(optarg);
3860 break;
3861 case QEMU_OPTION_xen_create:
3862 if (!(xen_available())) {
3863 printf("Option %s not supported for this target\n", popt->name);
3864 exit(1);
3866 xen_mode = XEN_CREATE;
3867 break;
3868 case QEMU_OPTION_xen_attach:
3869 if (!(xen_available())) {
3870 printf("Option %s not supported for this target\n", popt->name);
3871 exit(1);
3873 xen_mode = XEN_ATTACH;
3874 break;
3875 case QEMU_OPTION_trace:
3877 opts = qemu_opts_parse_noisily(qemu_find_opts("trace"),
3878 optarg, true);
3879 if (!opts) {
3880 exit(1);
3882 if (qemu_opt_get(opts, "enable")) {
3883 trace_enable_events(qemu_opt_get(opts, "enable"));
3885 trace_init_events(qemu_opt_get(opts, "events"));
3886 if (trace_file) {
3887 g_free(trace_file);
3889 trace_file = g_strdup(qemu_opt_get(opts, "file"));
3890 qemu_opts_del(opts);
3891 break;
3893 case QEMU_OPTION_readconfig:
3895 int ret = qemu_read_config_file(optarg);
3896 if (ret < 0) {
3897 error_report("read config %s: %s", optarg,
3898 strerror(-ret));
3899 exit(1);
3901 break;
3903 case QEMU_OPTION_spice:
3904 olist = qemu_find_opts("spice");
3905 if (!olist) {
3906 error_report("spice support is disabled");
3907 exit(1);
3909 opts = qemu_opts_parse_noisily(olist, optarg, false);
3910 if (!opts) {
3911 exit(1);
3913 display_remote++;
3914 break;
3915 case QEMU_OPTION_writeconfig:
3917 FILE *fp;
3918 if (strcmp(optarg, "-") == 0) {
3919 fp = stdout;
3920 } else {
3921 fp = fopen(optarg, "w");
3922 if (fp == NULL) {
3923 error_report("open %s: %s", optarg,
3924 strerror(errno));
3925 exit(1);
3928 qemu_config_write(fp);
3929 if (fp != stdout) {
3930 fclose(fp);
3932 break;
3934 case QEMU_OPTION_qtest:
3935 qtest_chrdev = optarg;
3936 break;
3937 case QEMU_OPTION_qtest_log:
3938 qtest_log = optarg;
3939 break;
3940 case QEMU_OPTION_sandbox:
3941 opts = qemu_opts_parse_noisily(qemu_find_opts("sandbox"),
3942 optarg, true);
3943 if (!opts) {
3944 exit(1);
3946 break;
3947 case QEMU_OPTION_add_fd:
3948 #ifndef _WIN32
3949 opts = qemu_opts_parse_noisily(qemu_find_opts("add-fd"),
3950 optarg, false);
3951 if (!opts) {
3952 exit(1);
3954 #else
3955 error_report("File descriptor passing is disabled on this "
3956 "platform");
3957 exit(1);
3958 #endif
3959 break;
3960 case QEMU_OPTION_object:
3961 opts = qemu_opts_parse_noisily(qemu_find_opts("object"),
3962 optarg, true);
3963 if (!opts) {
3964 exit(1);
3966 break;
3967 case QEMU_OPTION_realtime:
3968 opts = qemu_opts_parse_noisily(qemu_find_opts("realtime"),
3969 optarg, false);
3970 if (!opts) {
3971 exit(1);
3973 enable_mlock = qemu_opt_get_bool(opts, "mlock", true);
3974 break;
3975 case QEMU_OPTION_msg:
3976 opts = qemu_opts_parse_noisily(qemu_find_opts("msg"), optarg,
3977 false);
3978 if (!opts) {
3979 exit(1);
3981 configure_msg(opts);
3982 break;
3983 case QEMU_OPTION_dump_vmstate:
3984 if (vmstate_dump_file) {
3985 error_report("only one '-dump-vmstate' "
3986 "option may be given");
3987 exit(1);
3989 vmstate_dump_file = fopen(optarg, "w");
3990 if (vmstate_dump_file == NULL) {
3991 error_report("open %s: %s", optarg, strerror(errno));
3992 exit(1);
3994 break;
3995 default:
3996 os_parse_cmd_args(popt->index, optarg);
4001 * Clear error location left behind by the loop.
4002 * Best done right after the loop. Do not insert code here!
4004 loc_set_none();
4006 replay_configure(icount_opts);
4008 machine_class = select_machine();
4010 set_memory_options(&ram_slots, &maxram_size, machine_class);
4012 os_daemonize();
4014 if (qemu_init_main_loop(&main_loop_err)) {
4015 error_report_err(main_loop_err);
4016 exit(1);
4019 if (qemu_opts_foreach(qemu_find_opts("sandbox"),
4020 parse_sandbox, NULL, NULL)) {
4021 exit(1);
4024 if (qemu_opts_foreach(qemu_find_opts("name"),
4025 parse_name, NULL, NULL)) {
4026 exit(1);
4029 #ifndef _WIN32
4030 if (qemu_opts_foreach(qemu_find_opts("add-fd"),
4031 parse_add_fd, NULL, NULL)) {
4032 exit(1);
4035 if (qemu_opts_foreach(qemu_find_opts("add-fd"),
4036 cleanup_add_fd, NULL, NULL)) {
4037 exit(1);
4039 #endif
4041 current_machine = MACHINE(object_new(object_class_get_name(
4042 OBJECT_CLASS(machine_class))));
4043 if (machine_help_func(qemu_get_machine_opts(), current_machine)) {
4044 exit(0);
4046 object_property_add_child(object_get_root(), "machine",
4047 OBJECT(current_machine), &error_abort);
4048 cpu_exec_init_all();
4050 if (machine_class->hw_version) {
4051 qemu_set_hw_version(machine_class->hw_version);
4054 if (cpu_model && is_help_option(cpu_model)) {
4055 list_cpus(stdout, &fprintf, cpu_model);
4056 exit(0);
4059 if (!trace_init_backends()) {
4060 exit(1);
4062 trace_init_file(trace_file);
4064 /* Open the logfile at this point and set the log mask if necessary.
4066 if (log_file) {
4067 qemu_set_log_filename(log_file);
4070 if (log_mask) {
4071 int mask;
4072 mask = qemu_str_to_log_mask(log_mask);
4073 if (!mask) {
4074 qemu_print_log_usage(stdout);
4075 exit(1);
4077 qemu_set_log(mask);
4078 } else {
4079 qemu_set_log(0);
4082 /* If no data_dir is specified then try to find it relative to the
4083 executable path. */
4084 if (data_dir_idx < ARRAY_SIZE(data_dir)) {
4085 data_dir[data_dir_idx] = os_find_datadir();
4086 if (data_dir[data_dir_idx] != NULL) {
4087 data_dir_idx++;
4090 /* If all else fails use the install path specified when building. */
4091 if (data_dir_idx < ARRAY_SIZE(data_dir)) {
4092 data_dir[data_dir_idx++] = CONFIG_QEMU_DATADIR;
4095 smp_parse(qemu_opts_find(qemu_find_opts("smp-opts"), NULL));
4097 machine_class->max_cpus = machine_class->max_cpus ?: 1; /* Default to UP */
4098 if (max_cpus > machine_class->max_cpus) {
4099 error_report("Number of SMP CPUs requested (%d) exceeds max CPUs "
4100 "supported by machine '%s' (%d)", max_cpus,
4101 machine_class->name, machine_class->max_cpus);
4102 exit(1);
4106 * Get the default machine options from the machine if it is not already
4107 * specified either by the configuration file or by the command line.
4109 if (machine_class->default_machine_opts) {
4110 qemu_opts_set_defaults(qemu_find_opts("machine"),
4111 machine_class->default_machine_opts, 0);
4114 qemu_opts_foreach(qemu_find_opts("device"),
4115 default_driver_check, NULL, NULL);
4116 qemu_opts_foreach(qemu_find_opts("global"),
4117 default_driver_check, NULL, NULL);
4119 if (!vga_model && !default_vga) {
4120 vga_interface_type = VGA_DEVICE;
4122 if (!has_defaults || machine_class->no_serial) {
4123 default_serial = 0;
4125 if (!has_defaults || machine_class->no_parallel) {
4126 default_parallel = 0;
4128 if (!has_defaults || !machine_class->use_virtcon) {
4129 default_virtcon = 0;
4131 if (!has_defaults || !machine_class->use_sclp) {
4132 default_sclp = 0;
4134 if (!has_defaults || machine_class->no_floppy) {
4135 default_floppy = 0;
4137 if (!has_defaults || machine_class->no_cdrom) {
4138 default_cdrom = 0;
4140 if (!has_defaults || machine_class->no_sdcard) {
4141 default_sdcard = 0;
4143 if (!has_defaults) {
4144 default_monitor = 0;
4145 default_net = 0;
4146 default_vga = 0;
4149 if (is_daemonized()) {
4150 /* According to documentation and historically, -nographic redirects
4151 * serial port, parallel port and monitor to stdio, which does not work
4152 * with -daemonize. We can redirect these to null instead, but since
4153 * -nographic is legacy, let's just error out.
4154 * We disallow -nographic only if all other ports are not redirected
4155 * explicitly, to not break existing legacy setups which uses
4156 * -nographic _and_ redirects all ports explicitly - this is valid
4157 * usage, -nographic is just a no-op in this case.
4159 if (nographic
4160 && (default_parallel || default_serial
4161 || default_monitor || default_virtcon)) {
4162 error_report("-nographic cannot be used with -daemonize");
4163 exit(1);
4165 #ifdef CONFIG_CURSES
4166 if (display_type == DT_CURSES) {
4167 error_report("curses display cannot be used with -daemonize");
4168 exit(1);
4170 #endif
4173 if (nographic) {
4174 if (default_parallel)
4175 add_device_config(DEV_PARALLEL, "null");
4176 if (default_serial && default_monitor) {
4177 add_device_config(DEV_SERIAL, "mon:stdio");
4178 } else if (default_virtcon && default_monitor) {
4179 add_device_config(DEV_VIRTCON, "mon:stdio");
4180 } else if (default_sclp && default_monitor) {
4181 add_device_config(DEV_SCLP, "mon:stdio");
4182 } else {
4183 if (default_serial)
4184 add_device_config(DEV_SERIAL, "stdio");
4185 if (default_virtcon)
4186 add_device_config(DEV_VIRTCON, "stdio");
4187 if (default_sclp) {
4188 add_device_config(DEV_SCLP, "stdio");
4190 if (default_monitor)
4191 monitor_parse("stdio", "readline", false);
4193 } else {
4194 if (default_serial)
4195 add_device_config(DEV_SERIAL, "vc:80Cx24C");
4196 if (default_parallel)
4197 add_device_config(DEV_PARALLEL, "vc:80Cx24C");
4198 if (default_monitor)
4199 monitor_parse("vc:80Cx24C", "readline", false);
4200 if (default_virtcon)
4201 add_device_config(DEV_VIRTCON, "vc:80Cx24C");
4202 if (default_sclp) {
4203 add_device_config(DEV_SCLP, "vc:80Cx24C");
4207 #if defined(CONFIG_VNC)
4208 if (!QTAILQ_EMPTY(&(qemu_find_opts("vnc")->head))) {
4209 display_remote++;
4211 #endif
4212 if (display_type == DT_DEFAULT && !display_remote) {
4213 #if defined(CONFIG_GTK)
4214 display_type = DT_GTK;
4215 #elif defined(CONFIG_SDL)
4216 display_type = DT_SDL;
4217 #elif defined(CONFIG_COCOA)
4218 display_type = DT_COCOA;
4219 #elif defined(CONFIG_VNC)
4220 vnc_parse("localhost:0,to=99,id=default", &error_abort);
4221 show_vnc_port = 1;
4222 #else
4223 display_type = DT_NONE;
4224 #endif
4227 if ((no_frame || alt_grab || ctrl_grab) && display_type != DT_SDL) {
4228 error_report("-no-frame, -alt-grab and -ctrl-grab are only valid "
4229 "for SDL, ignoring option");
4231 if (no_quit && (display_type != DT_GTK && display_type != DT_SDL)) {
4232 error_report("-no-quit is only valid for GTK and SDL, "
4233 "ignoring option");
4236 if (display_type == DT_GTK) {
4237 early_gtk_display_init(request_opengl);
4240 if (display_type == DT_SDL) {
4241 sdl_display_early_init(request_opengl);
4244 if (request_opengl == 1 && display_opengl == 0) {
4245 #if defined(CONFIG_OPENGL)
4246 error_report("OpenGL is not supported by the display");
4247 #else
4248 error_report("OpenGL support is disabled");
4249 #endif
4250 exit(1);
4253 page_size_init();
4254 socket_init();
4256 if (qemu_opts_foreach(qemu_find_opts("object"),
4257 user_creatable_add_opts_foreach,
4258 object_create_initial, NULL)) {
4259 exit(1);
4262 if (qemu_opts_foreach(qemu_find_opts("chardev"),
4263 chardev_init_func, NULL, NULL)) {
4264 exit(1);
4267 #ifdef CONFIG_VIRTFS
4268 if (qemu_opts_foreach(qemu_find_opts("fsdev"),
4269 fsdev_init_func, NULL, NULL)) {
4270 exit(1);
4272 #endif
4274 if (pid_file && qemu_create_pidfile(pid_file) != 0) {
4275 error_report("could not acquire pid file: %s", strerror(errno));
4276 exit(1);
4279 if (qemu_opts_foreach(qemu_find_opts("device"),
4280 device_help_func, NULL, NULL)) {
4281 exit(0);
4284 machine_opts = qemu_get_machine_opts();
4285 if (qemu_opt_foreach(machine_opts, machine_set_property, current_machine,
4286 NULL)) {
4287 object_unref(OBJECT(current_machine));
4288 exit(1);
4291 configure_accelerator(current_machine);
4293 if (qtest_chrdev) {
4294 qtest_init(qtest_chrdev, qtest_log, &error_fatal);
4297 machine_opts = qemu_get_machine_opts();
4298 kernel_filename = qemu_opt_get(machine_opts, "kernel");
4299 initrd_filename = qemu_opt_get(machine_opts, "initrd");
4300 kernel_cmdline = qemu_opt_get(machine_opts, "append");
4301 bios_name = qemu_opt_get(machine_opts, "firmware");
4303 opts = qemu_opts_find(qemu_find_opts("boot-opts"), NULL);
4304 if (opts) {
4305 boot_order = qemu_opt_get(opts, "order");
4306 if (boot_order) {
4307 validate_bootdevices(boot_order, &error_fatal);
4310 boot_once = qemu_opt_get(opts, "once");
4311 if (boot_once) {
4312 validate_bootdevices(boot_once, &error_fatal);
4315 boot_menu = qemu_opt_get_bool(opts, "menu", boot_menu);
4316 boot_strict = qemu_opt_get_bool(opts, "strict", false);
4319 if (!boot_order) {
4320 boot_order = machine_class->default_boot_order;
4323 if (!kernel_cmdline) {
4324 kernel_cmdline = "";
4325 current_machine->kernel_cmdline = (char *)kernel_cmdline;
4328 linux_boot = (kernel_filename != NULL);
4330 if (!linux_boot && *kernel_cmdline != '\0') {
4331 error_report("-append only allowed with -kernel option");
4332 exit(1);
4335 if (!linux_boot && initrd_filename != NULL) {
4336 error_report("-initrd only allowed with -kernel option");
4337 exit(1);
4340 if (!linux_boot && qemu_opt_get(machine_opts, "dtb")) {
4341 error_report("-dtb only allowed with -kernel option");
4342 exit(1);
4345 if (semihosting_enabled() && !semihosting_get_argc() && kernel_filename) {
4346 /* fall back to the -kernel/-append */
4347 semihosting_arg_fallback(kernel_filename, kernel_cmdline);
4350 os_set_line_buffering();
4352 /* spice needs the timers to be initialized by this point */
4353 qemu_spice_init();
4355 cpu_ticks_init();
4356 if (icount_opts) {
4357 if (kvm_enabled() || xen_enabled()) {
4358 error_report("-icount is not allowed with kvm or xen");
4359 exit(1);
4361 configure_icount(icount_opts, &error_abort);
4362 qemu_opts_del(icount_opts);
4365 /* clean up network at qemu process termination */
4366 atexit(&net_cleanup);
4368 if (default_net) {
4369 QemuOptsList *net = qemu_find_opts("net");
4370 qemu_opts_set(net, NULL, "type", "nic", &error_abort);
4371 #ifdef CONFIG_SLIRP
4372 qemu_opts_set(net, NULL, "type", "user", &error_abort);
4373 #endif
4376 if (net_init_clients() < 0) {
4377 exit(1);
4380 if (qemu_opts_foreach(qemu_find_opts("object"),
4381 user_creatable_add_opts_foreach,
4382 object_create_delayed, NULL)) {
4383 exit(1);
4386 #ifdef CONFIG_TPM
4387 if (tpm_init() < 0) {
4388 exit(1);
4390 #endif
4392 /* init the bluetooth world */
4393 if (foreach_device_config(DEV_BT, bt_parse))
4394 exit(1);
4396 if (!xen_enabled()) {
4397 /* On 32-bit hosts, QEMU is limited by virtual address space */
4398 if (ram_size > (2047 << 20) && HOST_LONG_BITS == 32) {
4399 error_report("at most 2047 MB RAM can be simulated");
4400 exit(1);
4404 blk_mig_init();
4405 ram_mig_init();
4407 /* If the currently selected machine wishes to override the units-per-bus
4408 * property of its default HBA interface type, do so now. */
4409 if (machine_class->units_per_default_bus) {
4410 override_max_devs(machine_class->block_default_type,
4411 machine_class->units_per_default_bus);
4414 /* open the virtual block devices */
4415 if (snapshot || replay_mode != REPLAY_MODE_NONE) {
4416 qemu_opts_foreach(qemu_find_opts("drive"), drive_enable_snapshot,
4417 NULL, NULL);
4419 if (qemu_opts_foreach(qemu_find_opts("drive"), drive_init_func,
4420 &machine_class->block_default_type, NULL)) {
4421 exit(1);
4424 default_drive(default_cdrom, snapshot, machine_class->block_default_type, 2,
4425 CDROM_OPTS);
4426 default_drive(default_floppy, snapshot, IF_FLOPPY, 0, FD_OPTS);
4427 default_drive(default_sdcard, snapshot, IF_SD, 0, SD_OPTS);
4429 parse_numa_opts(machine_class);
4431 if (qemu_opts_foreach(qemu_find_opts("mon"),
4432 mon_init_func, NULL, NULL)) {
4433 exit(1);
4436 if (foreach_device_config(DEV_SERIAL, serial_parse) < 0)
4437 exit(1);
4438 if (foreach_device_config(DEV_PARALLEL, parallel_parse) < 0)
4439 exit(1);
4440 if (foreach_device_config(DEV_VIRTCON, virtcon_parse) < 0)
4441 exit(1);
4442 if (foreach_device_config(DEV_SCLP, sclp_parse) < 0) {
4443 exit(1);
4445 if (foreach_device_config(DEV_DEBUGCON, debugcon_parse) < 0)
4446 exit(1);
4448 /* If no default VGA is requested, the default is "none". */
4449 if (default_vga) {
4450 if (machine_class->default_display) {
4451 vga_model = machine_class->default_display;
4452 } else if (vga_interface_available(VGA_CIRRUS)) {
4453 vga_model = "cirrus";
4454 } else if (vga_interface_available(VGA_STD)) {
4455 vga_model = "std";
4458 if (vga_model) {
4459 select_vgahw(vga_model);
4462 if (watchdog) {
4463 i = select_watchdog(watchdog);
4464 if (i > 0)
4465 exit (i == 1 ? 1 : 0);
4468 if (machine_class->compat_props) {
4469 GlobalProperty *p;
4470 for (i = 0; i < machine_class->compat_props->len; i++) {
4471 p = g_array_index(machine_class->compat_props, GlobalProperty *, i);
4472 qdev_prop_register_global(p);
4475 qemu_add_globals();
4477 /* This checkpoint is required by replay to separate prior clock
4478 reading from the other reads, because timer polling functions query
4479 clock values from the log. */
4480 replay_checkpoint(CHECKPOINT_INIT);
4481 qdev_machine_init();
4483 current_machine->ram_size = ram_size;
4484 current_machine->maxram_size = maxram_size;
4485 current_machine->ram_slots = ram_slots;
4486 current_machine->boot_order = boot_order;
4487 current_machine->cpu_model = cpu_model;
4489 machine_class->init(current_machine);
4491 realtime_init();
4493 audio_init();
4495 cpu_synchronize_all_post_init();
4497 numa_post_machine_init();
4499 if (qemu_opts_foreach(qemu_find_opts("fw_cfg"),
4500 parse_fw_cfg, fw_cfg_find(), NULL) != 0) {
4501 exit(1);
4504 /* init USB devices */
4505 if (usb_enabled()) {
4506 if (foreach_device_config(DEV_USB, usb_parse) < 0)
4507 exit(1);
4510 /* Check if IGD GFX passthrough. */
4511 igd_gfx_passthru();
4513 /* init generic devices */
4514 rom_set_order_override(FW_CFG_ORDER_OVERRIDE_DEVICE);
4515 if (qemu_opts_foreach(qemu_find_opts("device"),
4516 device_init_func, NULL, NULL)) {
4517 exit(1);
4519 rom_reset_order_override();
4521 /* Did we create any drives that we failed to create a device for? */
4522 drive_check_orphaned();
4524 /* Don't warn about the default network setup that you get if
4525 * no command line -net or -netdev options are specified. There
4526 * are two cases that we would otherwise complain about:
4527 * (1) board doesn't support a NIC but the implicit "-net nic"
4528 * requested one
4529 * (2) CONFIG_SLIRP not set, in which case the implicit "-net nic"
4530 * sets up a nic that isn't connected to anything.
4532 if (!default_net) {
4533 net_check_clients();
4537 if (boot_once) {
4538 qemu_boot_set(boot_once, &error_fatal);
4539 qemu_register_reset(restore_boot_order, g_strdup(boot_order));
4542 ds = init_displaystate();
4544 /* init local displays */
4545 switch (display_type) {
4546 case DT_CURSES:
4547 curses_display_init(ds, full_screen);
4548 break;
4549 case DT_SDL:
4550 sdl_display_init(ds, full_screen, no_frame);
4551 break;
4552 case DT_COCOA:
4553 cocoa_display_init(ds, full_screen);
4554 break;
4555 case DT_GTK:
4556 gtk_display_init(ds, full_screen, grab_on_hover);
4557 break;
4558 default:
4559 break;
4562 /* must be after terminal init, SDL library changes signal handlers */
4563 os_setup_signal_handling();
4565 /* init remote displays */
4566 qemu_opts_foreach(qemu_find_opts("vnc"),
4567 vnc_init_func, NULL, NULL);
4568 if (show_vnc_port) {
4569 char *ret = vnc_display_local_addr("default");
4570 printf("VNC server running on '%s'\n", ret);
4571 g_free(ret);
4574 if (using_spice) {
4575 qemu_spice_display_init();
4578 if (foreach_device_config(DEV_GDB, gdbserver_start) < 0) {
4579 exit(1);
4582 qdev_machine_creation_done();
4584 /* TODO: once all bus devices are qdevified, this should be done
4585 * when bus is created by qdev.c */
4586 qemu_register_reset(qbus_reset_all_fn, sysbus_get_default());
4587 qemu_run_machine_init_done_notifiers();
4589 if (rom_check_and_register_reset() != 0) {
4590 error_report("rom check and register reset failed");
4591 exit(1);
4594 replay_start();
4596 /* This checkpoint is required by replay to separate prior clock
4597 reading from the other reads, because timer polling functions query
4598 clock values from the log. */
4599 replay_checkpoint(CHECKPOINT_RESET);
4600 qemu_system_reset(VMRESET_SILENT);
4601 register_global_state();
4602 if (loadvm) {
4603 if (load_vmstate(loadvm) < 0) {
4604 autostart = 0;
4608 qdev_prop_check_globals();
4609 if (vmstate_dump_file) {
4610 /* dump and exit */
4611 dump_vmstate_json_to_file(vmstate_dump_file);
4612 return 0;
4615 if (incoming) {
4616 Error *local_err = NULL;
4617 qemu_start_incoming_migration(incoming, &local_err);
4618 if (local_err) {
4619 error_reportf_err(local_err, "-incoming %s: ", incoming);
4620 exit(1);
4622 } else if (autostart) {
4623 vm_start();
4626 os_setup_post();
4628 main_loop();
4629 replay_disable_events();
4631 bdrv_close_all();
4632 pause_all_vcpus();
4633 res_free();
4634 #ifdef CONFIG_TPM
4635 tpm_cleanup();
4636 #endif
4638 return 0;