wxx: Fix compiler warning for host-libusb.c
[qemu/ar7.git] / vl.c
blob7932cbf37bb52ede5700360ef2b41aafeb2f84ca
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);
41 int main(int argc, char **argv)
43 return qemu_main(argc, argv);
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 */
56 #include "qemu/error-report.h"
57 #include "qemu/sockets.h"
58 #include "hw/hw.h"
59 #include "hw/boards.h"
60 #include "sysemu/accel.h"
61 #include "hw/usb.h"
62 #include "hw/i386/pc.h"
63 #include "hw/isa/isa.h"
64 #include "hw/bt.h"
65 #include "sysemu/watchdog.h"
66 #include "hw/smbios/smbios.h"
67 #include "hw/xen/xen.h"
68 #include "hw/qdev.h"
69 #include "hw/loader.h"
70 #include "monitor/qdev.h"
71 #include "sysemu/bt.h"
72 #include "net/net.h"
73 #include "net/slirp.h"
74 #include "monitor/monitor.h"
75 #include "ui/console.h"
76 #include "ui/input.h"
77 #include "sysemu/sysemu.h"
78 #include "sysemu/numa.h"
79 #include "exec/gdbstub.h"
80 #include "qemu/timer.h"
81 #include "sysemu/char.h"
82 #include "qemu/bitmap.h"
83 #include "qemu/log.h"
84 #include "sysemu/blockdev.h"
85 #include "hw/block/block.h"
86 #include "migration/block.h"
87 #include "sysemu/tpm.h"
88 #include "sysemu/dma.h"
89 #include "audio/audio.h"
90 #include "migration/migration.h"
91 #include "sysemu/cpus.h"
92 #include "sysemu/kvm.h"
93 #include "qapi/qmp/qjson.h"
94 #include "qemu/option.h"
95 #include "qemu/config-file.h"
96 #include "qemu-options.h"
97 #include "qmp-commands.h"
98 #include "qemu/main-loop.h"
99 #ifdef CONFIG_VIRTFS
100 #include "fsdev/qemu-fsdev.h"
101 #endif
102 #include "sysemu/qtest.h"
104 #include "disas/disas.h"
107 #include "slirp/libslirp.h"
109 #include "trace.h"
110 #include "trace/control.h"
111 #include "qemu/queue.h"
112 #include "sysemu/cpus.h"
113 #include "sysemu/arch_init.h"
115 #include "ui/qemu-spice.h"
116 #include "qapi/string-input-visitor.h"
117 #include "qapi/opts-visitor.h"
118 #include "qom/object_interfaces.h"
119 #include "qapi-event.h"
120 #include "exec/semihost.h"
121 #include "crypto/init.h"
122 #include "sysemu/replay.h"
123 #include "qapi/qmp/qerror.h"
125 #define MAX_VIRTIO_CONSOLES 1
126 #define MAX_SCLP_CONSOLES 1
128 static const char *data_dir[16];
129 static int data_dir_idx;
130 const char *bios_name = NULL;
131 enum vga_retrace_method vga_retrace_method = VGA_RETRACE_DUMB;
132 int request_opengl = -1;
133 int display_opengl;
134 const char* keyboard_layout = NULL;
135 ram_addr_t ram_size;
136 const char *mem_path = NULL;
137 int mem_prealloc = 0; /* force preallocation of physical target memory */
138 bool enable_mlock = false;
139 int nb_nics;
140 NICInfo nd_table[MAX_NICS];
141 int autostart;
142 static int rtc_utc = 1;
143 static int rtc_date_offset = -1; /* -1 means no change */
144 QEMUClockType rtc_clock;
145 int vga_interface_type = VGA_NONE;
146 static int full_screen = 0;
147 static int no_frame = 0;
148 int no_quit = 0;
149 static bool grab_on_hover;
150 CharDriverState *serial_hds[MAX_SERIAL_PORTS];
151 CharDriverState *parallel_hds[MAX_PARALLEL_PORTS];
152 CharDriverState *virtcon_hds[MAX_VIRTIO_CONSOLES];
153 CharDriverState *sclp_hds[MAX_SCLP_CONSOLES];
154 int win2k_install_hack = 0;
155 int singlestep = 0;
156 int smp_cpus = 1;
157 int max_cpus = 1;
158 int smp_cores = 1;
159 int smp_threads = 1;
160 int acpi_enabled = 1;
161 int no_hpet = 0;
162 int fd_bootchk = 1;
163 static int no_reboot;
164 int no_shutdown = 0;
165 int cursor_hide = 1;
166 int graphic_rotate = 0;
167 const char *watchdog;
168 QEMUOptionRom option_rom[MAX_OPTION_ROMS];
169 int nb_option_roms;
170 int old_param = 0;
171 const char *qemu_name;
172 int alt_grab = 0;
173 int ctrl_grab = 0;
174 unsigned int nb_prom_envs = 0;
175 const char *prom_envs[MAX_PROM_ENVS];
176 int boot_menu;
177 bool boot_strict;
178 uint8_t *boot_splash_filedata;
179 size_t boot_splash_filedata_size;
180 uint8_t qemu_extra_params_fw[2];
182 int icount_align_option;
184 /* The bytes in qemu_uuid[] are in the order specified by RFC4122, _not_ in the
185 * little-endian "wire format" described in the SMBIOS 2.6 specification.
187 uint8_t qemu_uuid[16];
188 bool qemu_uuid_set;
190 /* Trace unassigned memory or i/o accesses. */
191 bool trace_unassigned;
193 static NotifierList exit_notifiers =
194 NOTIFIER_LIST_INITIALIZER(exit_notifiers);
196 static NotifierList machine_init_done_notifiers =
197 NOTIFIER_LIST_INITIALIZER(machine_init_done_notifiers);
199 bool xen_allowed;
200 uint32_t xen_domid;
201 enum xen_mode xen_mode = XEN_EMULATE;
203 static int has_defaults = 1;
204 static int default_serial = 1;
205 static int default_parallel = 1;
206 static int default_virtcon = 1;
207 static int default_sclp = 1;
208 static int default_monitor = 1;
209 static int default_floppy = 1;
210 static int default_cdrom = 1;
211 static int default_sdcard = 1;
212 static int default_vga = 1;
213 static int default_net = 1;
215 static struct {
216 const char *driver;
217 int *flag;
218 } default_list[] = {
219 { .driver = "isa-serial", .flag = &default_serial },
220 { .driver = "isa-parallel", .flag = &default_parallel },
221 { .driver = "isa-fdc", .flag = &default_floppy },
222 { .driver = "ide-cd", .flag = &default_cdrom },
223 { .driver = "ide-hd", .flag = &default_cdrom },
224 { .driver = "ide-drive", .flag = &default_cdrom },
225 { .driver = "scsi-cd", .flag = &default_cdrom },
226 { .driver = "virtio-serial-pci", .flag = &default_virtcon },
227 { .driver = "virtio-serial", .flag = &default_virtcon },
228 { .driver = "VGA", .flag = &default_vga },
229 { .driver = "isa-vga", .flag = &default_vga },
230 { .driver = "cirrus-vga", .flag = &default_vga },
231 { .driver = "isa-cirrus-vga", .flag = &default_vga },
232 { .driver = "vmware-svga", .flag = &default_vga },
233 { .driver = "qxl-vga", .flag = &default_vga },
234 { .driver = "virtio-vga", .flag = &default_vga },
237 static QemuOptsList qemu_rtc_opts = {
238 .name = "rtc",
239 .head = QTAILQ_HEAD_INITIALIZER(qemu_rtc_opts.head),
240 .desc = {
242 .name = "base",
243 .type = QEMU_OPT_STRING,
245 .name = "clock",
246 .type = QEMU_OPT_STRING,
248 .name = "driftfix",
249 .type = QEMU_OPT_STRING,
251 { /* end of list */ }
255 static QemuOptsList qemu_sandbox_opts = {
256 .name = "sandbox",
257 .implied_opt_name = "enable",
258 .head = QTAILQ_HEAD_INITIALIZER(qemu_sandbox_opts.head),
259 .desc = {
261 .name = "enable",
262 .type = QEMU_OPT_BOOL,
264 { /* end of list */ }
268 static QemuOptsList qemu_option_rom_opts = {
269 .name = "option-rom",
270 .implied_opt_name = "romfile",
271 .head = QTAILQ_HEAD_INITIALIZER(qemu_option_rom_opts.head),
272 .desc = {
274 .name = "bootindex",
275 .type = QEMU_OPT_NUMBER,
276 }, {
277 .name = "romfile",
278 .type = QEMU_OPT_STRING,
280 { /* end of list */ }
284 static QemuOptsList qemu_machine_opts = {
285 .name = "machine",
286 .implied_opt_name = "type",
287 .merge_lists = true,
288 .head = QTAILQ_HEAD_INITIALIZER(qemu_machine_opts.head),
289 .desc = {
291 * no elements => accept any
292 * sanity checking will happen later
293 * when setting machine properties
299 static QemuOptsList qemu_boot_opts = {
300 .name = "boot-opts",
301 .implied_opt_name = "order",
302 .merge_lists = true,
303 .head = QTAILQ_HEAD_INITIALIZER(qemu_boot_opts.head),
304 .desc = {
306 .name = "order",
307 .type = QEMU_OPT_STRING,
308 }, {
309 .name = "once",
310 .type = QEMU_OPT_STRING,
311 }, {
312 .name = "menu",
313 .type = QEMU_OPT_BOOL,
314 }, {
315 .name = "splash",
316 .type = QEMU_OPT_STRING,
317 }, {
318 .name = "splash-time",
319 .type = QEMU_OPT_STRING,
320 }, {
321 .name = "reboot-timeout",
322 .type = QEMU_OPT_STRING,
323 }, {
324 .name = "strict",
325 .type = QEMU_OPT_BOOL,
327 { /*End of list */ }
331 static QemuOptsList qemu_add_fd_opts = {
332 .name = "add-fd",
333 .head = QTAILQ_HEAD_INITIALIZER(qemu_add_fd_opts.head),
334 .desc = {
336 .name = "fd",
337 .type = QEMU_OPT_NUMBER,
338 .help = "file descriptor of which a duplicate is added to fd set",
340 .name = "set",
341 .type = QEMU_OPT_NUMBER,
342 .help = "ID of the fd set to add fd to",
344 .name = "opaque",
345 .type = QEMU_OPT_STRING,
346 .help = "free-form string used to describe fd",
348 { /* end of list */ }
352 static QemuOptsList qemu_object_opts = {
353 .name = "object",
354 .implied_opt_name = "qom-type",
355 .head = QTAILQ_HEAD_INITIALIZER(qemu_object_opts.head),
356 .desc = {
361 static QemuOptsList qemu_tpmdev_opts = {
362 .name = "tpmdev",
363 .implied_opt_name = "type",
364 .head = QTAILQ_HEAD_INITIALIZER(qemu_tpmdev_opts.head),
365 .desc = {
366 /* options are defined in the TPM backends */
367 { /* end of list */ }
371 static QemuOptsList qemu_realtime_opts = {
372 .name = "realtime",
373 .head = QTAILQ_HEAD_INITIALIZER(qemu_realtime_opts.head),
374 .desc = {
376 .name = "mlock",
377 .type = QEMU_OPT_BOOL,
379 { /* end of list */ }
383 static QemuOptsList qemu_msg_opts = {
384 .name = "msg",
385 .head = QTAILQ_HEAD_INITIALIZER(qemu_msg_opts.head),
386 .desc = {
388 .name = "timestamp",
389 .type = QEMU_OPT_BOOL,
391 { /* end of list */ }
395 static QemuOptsList qemu_name_opts = {
396 .name = "name",
397 .implied_opt_name = "guest",
398 .merge_lists = true,
399 .head = QTAILQ_HEAD_INITIALIZER(qemu_name_opts.head),
400 .desc = {
402 .name = "guest",
403 .type = QEMU_OPT_STRING,
404 .help = "Sets the name of the guest.\n"
405 "This name will be displayed in the SDL window caption.\n"
406 "The name will also be used for the VNC server",
407 }, {
408 .name = "process",
409 .type = QEMU_OPT_STRING,
410 .help = "Sets the name of the QEMU process, as shown in top etc",
411 }, {
412 .name = "debug-threads",
413 .type = QEMU_OPT_BOOL,
414 .help = "When enabled, name the individual threads; defaults off.\n"
415 "NOTE: The thread names are for debugging and not a\n"
416 "stable API.",
418 { /* End of list */ }
422 static QemuOptsList qemu_mem_opts = {
423 .name = "memory",
424 .implied_opt_name = "size",
425 .head = QTAILQ_HEAD_INITIALIZER(qemu_mem_opts.head),
426 .merge_lists = true,
427 .desc = {
429 .name = "size",
430 .type = QEMU_OPT_SIZE,
433 .name = "slots",
434 .type = QEMU_OPT_NUMBER,
437 .name = "maxmem",
438 .type = QEMU_OPT_SIZE,
440 { /* end of list */ }
444 static QemuOptsList qemu_icount_opts = {
445 .name = "icount",
446 .implied_opt_name = "shift",
447 .merge_lists = true,
448 .head = QTAILQ_HEAD_INITIALIZER(qemu_icount_opts.head),
449 .desc = {
451 .name = "shift",
452 .type = QEMU_OPT_STRING,
453 }, {
454 .name = "align",
455 .type = QEMU_OPT_BOOL,
456 }, {
457 .name = "sleep",
458 .type = QEMU_OPT_BOOL,
459 }, {
460 .name = "rr",
461 .type = QEMU_OPT_STRING,
462 }, {
463 .name = "rrfile",
464 .type = QEMU_OPT_STRING,
466 { /* end of list */ }
470 static QemuOptsList qemu_semihosting_config_opts = {
471 .name = "semihosting-config",
472 .implied_opt_name = "enable",
473 .head = QTAILQ_HEAD_INITIALIZER(qemu_semihosting_config_opts.head),
474 .desc = {
476 .name = "enable",
477 .type = QEMU_OPT_BOOL,
478 }, {
479 .name = "target",
480 .type = QEMU_OPT_STRING,
481 }, {
482 .name = "arg",
483 .type = QEMU_OPT_STRING,
485 { /* end of list */ }
489 static QemuOptsList qemu_fw_cfg_opts = {
490 .name = "fw_cfg",
491 .implied_opt_name = "name",
492 .head = QTAILQ_HEAD_INITIALIZER(qemu_fw_cfg_opts.head),
493 .desc = {
495 .name = "name",
496 .type = QEMU_OPT_STRING,
497 .help = "Sets the fw_cfg name of the blob to be inserted",
498 }, {
499 .name = "file",
500 .type = QEMU_OPT_STRING,
501 .help = "Sets the name of the file from which\n"
502 "the fw_cfg blob will be loaded",
503 }, {
504 .name = "string",
505 .type = QEMU_OPT_STRING,
506 .help = "Sets content of the blob to be inserted from a string",
508 { /* end of list */ }
513 * Get machine options
515 * Returns: machine options (never null).
517 QemuOpts *qemu_get_machine_opts(void)
519 return qemu_find_opts_singleton("machine");
522 const char *qemu_get_vm_name(void)
524 return qemu_name;
527 static void res_free(void)
529 g_free(boot_splash_filedata);
530 boot_splash_filedata = NULL;
533 static int default_driver_check(void *opaque, QemuOpts *opts, Error **errp)
535 const char *driver = qemu_opt_get(opts, "driver");
536 int i;
538 if (!driver)
539 return 0;
540 for (i = 0; i < ARRAY_SIZE(default_list); i++) {
541 if (strcmp(default_list[i].driver, driver) != 0)
542 continue;
543 *(default_list[i].flag) = 0;
545 return 0;
548 /***********************************************************/
549 /* QEMU state */
551 static RunState current_run_state = RUN_STATE_PRELAUNCH;
553 /* We use RUN_STATE__MAX but any invalid value will do */
554 static RunState vmstop_requested = RUN_STATE__MAX;
555 static QemuMutex vmstop_lock;
557 typedef struct {
558 RunState from;
559 RunState to;
560 } RunStateTransition;
562 static const RunStateTransition runstate_transitions_def[] = {
563 /* from -> to */
564 { RUN_STATE_DEBUG, RUN_STATE_RUNNING },
565 { RUN_STATE_DEBUG, RUN_STATE_FINISH_MIGRATE },
566 { RUN_STATE_DEBUG, RUN_STATE_PRELAUNCH },
568 { RUN_STATE_INMIGRATE, RUN_STATE_INTERNAL_ERROR },
569 { RUN_STATE_INMIGRATE, RUN_STATE_IO_ERROR },
570 { RUN_STATE_INMIGRATE, RUN_STATE_PAUSED },
571 { RUN_STATE_INMIGRATE, RUN_STATE_RUNNING },
572 { RUN_STATE_INMIGRATE, RUN_STATE_SHUTDOWN },
573 { RUN_STATE_INMIGRATE, RUN_STATE_SUSPENDED },
574 { RUN_STATE_INMIGRATE, RUN_STATE_WATCHDOG },
575 { RUN_STATE_INMIGRATE, RUN_STATE_GUEST_PANICKED },
576 { RUN_STATE_INMIGRATE, RUN_STATE_FINISH_MIGRATE },
577 { RUN_STATE_INMIGRATE, RUN_STATE_PRELAUNCH },
578 { RUN_STATE_INMIGRATE, RUN_STATE_POSTMIGRATE },
580 { RUN_STATE_INTERNAL_ERROR, RUN_STATE_PAUSED },
581 { RUN_STATE_INTERNAL_ERROR, RUN_STATE_FINISH_MIGRATE },
582 { RUN_STATE_INTERNAL_ERROR, RUN_STATE_PRELAUNCH },
584 { RUN_STATE_IO_ERROR, RUN_STATE_RUNNING },
585 { RUN_STATE_IO_ERROR, RUN_STATE_FINISH_MIGRATE },
586 { RUN_STATE_IO_ERROR, RUN_STATE_PRELAUNCH },
588 { RUN_STATE_PAUSED, RUN_STATE_RUNNING },
589 { RUN_STATE_PAUSED, RUN_STATE_FINISH_MIGRATE },
590 { RUN_STATE_PAUSED, RUN_STATE_PRELAUNCH },
592 { RUN_STATE_POSTMIGRATE, RUN_STATE_RUNNING },
593 { RUN_STATE_POSTMIGRATE, RUN_STATE_FINISH_MIGRATE },
594 { RUN_STATE_POSTMIGRATE, RUN_STATE_PRELAUNCH },
596 { RUN_STATE_PRELAUNCH, RUN_STATE_RUNNING },
597 { RUN_STATE_PRELAUNCH, RUN_STATE_FINISH_MIGRATE },
598 { RUN_STATE_PRELAUNCH, RUN_STATE_INMIGRATE },
600 { RUN_STATE_FINISH_MIGRATE, RUN_STATE_RUNNING },
601 { RUN_STATE_FINISH_MIGRATE, RUN_STATE_POSTMIGRATE },
602 { RUN_STATE_FINISH_MIGRATE, RUN_STATE_PRELAUNCH },
604 { RUN_STATE_RESTORE_VM, RUN_STATE_RUNNING },
605 { RUN_STATE_RESTORE_VM, RUN_STATE_PRELAUNCH },
607 { RUN_STATE_RUNNING, RUN_STATE_DEBUG },
608 { RUN_STATE_RUNNING, RUN_STATE_INTERNAL_ERROR },
609 { RUN_STATE_RUNNING, RUN_STATE_IO_ERROR },
610 { RUN_STATE_RUNNING, RUN_STATE_PAUSED },
611 { RUN_STATE_RUNNING, RUN_STATE_FINISH_MIGRATE },
612 { RUN_STATE_RUNNING, RUN_STATE_RESTORE_VM },
613 { RUN_STATE_RUNNING, RUN_STATE_SAVE_VM },
614 { RUN_STATE_RUNNING, RUN_STATE_SHUTDOWN },
615 { RUN_STATE_RUNNING, RUN_STATE_WATCHDOG },
616 { RUN_STATE_RUNNING, RUN_STATE_GUEST_PANICKED },
618 { RUN_STATE_SAVE_VM, RUN_STATE_RUNNING },
620 { RUN_STATE_SHUTDOWN, RUN_STATE_PAUSED },
621 { RUN_STATE_SHUTDOWN, RUN_STATE_FINISH_MIGRATE },
622 { RUN_STATE_SHUTDOWN, RUN_STATE_PRELAUNCH },
624 { RUN_STATE_DEBUG, RUN_STATE_SUSPENDED },
625 { RUN_STATE_RUNNING, RUN_STATE_SUSPENDED },
626 { RUN_STATE_SUSPENDED, RUN_STATE_RUNNING },
627 { RUN_STATE_SUSPENDED, RUN_STATE_FINISH_MIGRATE },
628 { RUN_STATE_SUSPENDED, RUN_STATE_PRELAUNCH },
630 { RUN_STATE_WATCHDOG, RUN_STATE_RUNNING },
631 { RUN_STATE_WATCHDOG, RUN_STATE_FINISH_MIGRATE },
632 { RUN_STATE_WATCHDOG, RUN_STATE_PRELAUNCH },
634 { RUN_STATE_GUEST_PANICKED, RUN_STATE_RUNNING },
635 { RUN_STATE_GUEST_PANICKED, RUN_STATE_FINISH_MIGRATE },
636 { RUN_STATE_GUEST_PANICKED, RUN_STATE_PRELAUNCH },
638 { RUN_STATE__MAX, RUN_STATE__MAX },
641 static bool runstate_valid_transitions[RUN_STATE__MAX][RUN_STATE__MAX];
643 bool runstate_check(RunState state)
645 return current_run_state == state;
648 bool runstate_store(char *str, size_t size)
650 const char *state = RunState_lookup[current_run_state];
651 size_t len = strlen(state) + 1;
653 if (len > size) {
654 return false;
656 memcpy(str, state, len);
657 return true;
660 static void runstate_init(void)
662 const RunStateTransition *p;
664 memset(&runstate_valid_transitions, 0, sizeof(runstate_valid_transitions));
665 for (p = &runstate_transitions_def[0]; p->from != RUN_STATE__MAX; p++) {
666 runstate_valid_transitions[p->from][p->to] = true;
669 qemu_mutex_init(&vmstop_lock);
672 /* This function will abort() on invalid state transitions */
673 void runstate_set(RunState new_state)
675 assert(new_state < RUN_STATE__MAX);
677 if (current_run_state == new_state) {
678 return;
681 if (!runstate_valid_transitions[current_run_state][new_state]) {
682 error_report("invalid runstate transition: '%s' -> '%s'",
683 RunState_lookup[current_run_state],
684 RunState_lookup[new_state]);
685 abort();
687 trace_runstate_set(new_state);
688 current_run_state = new_state;
691 int runstate_is_running(void)
693 return runstate_check(RUN_STATE_RUNNING);
696 bool runstate_needs_reset(void)
698 return runstate_check(RUN_STATE_INTERNAL_ERROR) ||
699 runstate_check(RUN_STATE_SHUTDOWN);
702 StatusInfo *qmp_query_status(Error **errp)
704 StatusInfo *info = g_malloc0(sizeof(*info));
706 info->running = runstate_is_running();
707 info->singlestep = singlestep;
708 info->status = current_run_state;
710 return info;
713 static bool qemu_vmstop_requested(RunState *r)
715 qemu_mutex_lock(&vmstop_lock);
716 *r = vmstop_requested;
717 vmstop_requested = RUN_STATE__MAX;
718 qemu_mutex_unlock(&vmstop_lock);
719 return *r < RUN_STATE__MAX;
722 void qemu_system_vmstop_request_prepare(void)
724 qemu_mutex_lock(&vmstop_lock);
727 void qemu_system_vmstop_request(RunState state)
729 vmstop_requested = state;
730 qemu_mutex_unlock(&vmstop_lock);
731 qemu_notify_event();
734 void vm_start(void)
736 RunState requested;
738 qemu_vmstop_requested(&requested);
739 if (runstate_is_running() && requested == RUN_STATE__MAX) {
740 return;
743 /* Ensure that a STOP/RESUME pair of events is emitted if a
744 * vmstop request was pending. The BLOCK_IO_ERROR event, for
745 * example, according to documentation is always followed by
746 * the STOP event.
748 if (runstate_is_running()) {
749 qapi_event_send_stop(&error_abort);
750 } else {
751 cpu_enable_ticks();
752 runstate_set(RUN_STATE_RUNNING);
753 vm_state_notify(1, RUN_STATE_RUNNING);
754 resume_all_vcpus();
757 qapi_event_send_resume(&error_abort);
761 /***********************************************************/
762 /* real time host monotonic timer */
764 static time_t qemu_time(void)
766 return qemu_clock_get_ms(QEMU_CLOCK_HOST) / 1000;
769 /***********************************************************/
770 /* host time/date access */
771 void qemu_get_timedate(struct tm *tm, int offset)
773 time_t ti = qemu_time();
775 ti += offset;
776 if (rtc_date_offset == -1) {
777 if (rtc_utc)
778 gmtime_r(&ti, tm);
779 else
780 localtime_r(&ti, tm);
781 } else {
782 ti -= rtc_date_offset;
783 gmtime_r(&ti, tm);
787 int qemu_timedate_diff(struct tm *tm)
789 time_t seconds;
791 if (rtc_date_offset == -1)
792 if (rtc_utc)
793 seconds = mktimegm(tm);
794 else {
795 struct tm tmp = *tm;
796 tmp.tm_isdst = -1; /* use timezone to figure it out */
797 seconds = mktime(&tmp);
799 else
800 seconds = mktimegm(tm) + rtc_date_offset;
802 return seconds - qemu_time();
805 static void configure_rtc_date_offset(const char *startdate, int legacy)
807 time_t rtc_start_date;
808 struct tm tm;
810 if (!strcmp(startdate, "now") && legacy) {
811 rtc_date_offset = -1;
812 } else {
813 if (sscanf(startdate, "%d-%d-%dT%d:%d:%d",
814 &tm.tm_year,
815 &tm.tm_mon,
816 &tm.tm_mday,
817 &tm.tm_hour,
818 &tm.tm_min,
819 &tm.tm_sec) == 6) {
820 /* OK */
821 } else if (sscanf(startdate, "%d-%d-%d",
822 &tm.tm_year,
823 &tm.tm_mon,
824 &tm.tm_mday) == 3) {
825 tm.tm_hour = 0;
826 tm.tm_min = 0;
827 tm.tm_sec = 0;
828 } else {
829 goto date_fail;
831 tm.tm_year -= 1900;
832 tm.tm_mon--;
833 rtc_start_date = mktimegm(&tm);
834 if (rtc_start_date == -1) {
835 date_fail:
836 error_report("invalid date format");
837 error_printf("valid formats: "
838 "'2006-06-17T16:01:21' or '2006-06-17'\n");
839 exit(1);
841 rtc_date_offset = qemu_time() - rtc_start_date;
845 static void configure_rtc(QemuOpts *opts)
847 const char *value;
849 value = qemu_opt_get(opts, "base");
850 if (value) {
851 if (!strcmp(value, "utc")) {
852 rtc_utc = 1;
853 } else if (!strcmp(value, "localtime")) {
854 Error *blocker = NULL;
855 rtc_utc = 0;
856 error_setg(&blocker, QERR_REPLAY_NOT_SUPPORTED,
857 "-rtc base=localtime");
858 replay_add_blocker(blocker);
859 } else {
860 configure_rtc_date_offset(value, 0);
863 value = qemu_opt_get(opts, "clock");
864 if (value) {
865 if (!strcmp(value, "host")) {
866 rtc_clock = QEMU_CLOCK_HOST;
867 } else if (!strcmp(value, "rt")) {
868 rtc_clock = QEMU_CLOCK_REALTIME;
869 } else if (!strcmp(value, "vm")) {
870 rtc_clock = QEMU_CLOCK_VIRTUAL;
871 } else {
872 error_report("invalid option value '%s'", value);
873 exit(1);
876 value = qemu_opt_get(opts, "driftfix");
877 if (value) {
878 if (!strcmp(value, "slew")) {
879 static GlobalProperty slew_lost_ticks = {
880 .driver = "mc146818rtc",
881 .property = "lost_tick_policy",
882 .value = "slew",
885 qdev_prop_register_global(&slew_lost_ticks);
886 } else if (!strcmp(value, "none")) {
887 /* discard is default */
888 } else {
889 error_report("invalid option value '%s'", value);
890 exit(1);
895 /***********************************************************/
896 /* Bluetooth support */
897 static int nb_hcis;
898 static int cur_hci;
899 static struct HCIInfo *hci_table[MAX_NICS];
901 struct HCIInfo *qemu_next_hci(void)
903 if (cur_hci == nb_hcis)
904 return &null_hci;
906 return hci_table[cur_hci++];
909 static int bt_hci_parse(const char *str)
911 struct HCIInfo *hci;
912 bdaddr_t bdaddr;
914 if (nb_hcis >= MAX_NICS) {
915 error_report("too many bluetooth HCIs (max %i)", MAX_NICS);
916 return -1;
919 hci = hci_init(str);
920 if (!hci)
921 return -1;
923 bdaddr.b[0] = 0x52;
924 bdaddr.b[1] = 0x54;
925 bdaddr.b[2] = 0x00;
926 bdaddr.b[3] = 0x12;
927 bdaddr.b[4] = 0x34;
928 bdaddr.b[5] = 0x56 + nb_hcis;
929 hci->bdaddr_set(hci, bdaddr.b);
931 hci_table[nb_hcis++] = hci;
933 return 0;
936 static void bt_vhci_add(int vlan_id)
938 struct bt_scatternet_s *vlan = qemu_find_bt_vlan(vlan_id);
940 if (!vlan->slave)
941 error_report("warning: adding a VHCI to an empty scatternet %i",
942 vlan_id);
944 bt_vhci_init(bt_new_hci(vlan));
947 static struct bt_device_s *bt_device_add(const char *opt)
949 struct bt_scatternet_s *vlan;
950 int vlan_id = 0;
951 char *endp = strstr(opt, ",vlan=");
952 int len = (endp ? endp - opt : strlen(opt)) + 1;
953 char devname[10];
955 pstrcpy(devname, MIN(sizeof(devname), len), opt);
957 if (endp) {
958 vlan_id = strtol(endp + 6, &endp, 0);
959 if (*endp) {
960 error_report("unrecognised bluetooth vlan Id");
961 return 0;
965 vlan = qemu_find_bt_vlan(vlan_id);
967 if (!vlan->slave)
968 error_report("warning: adding a slave device to an empty scatternet %i",
969 vlan_id);
971 if (!strcmp(devname, "keyboard"))
972 return bt_keyboard_init(vlan);
974 error_report("unsupported bluetooth device '%s'", devname);
975 return 0;
978 static int bt_parse(const char *opt)
980 const char *endp, *p;
981 int vlan;
983 if (strstart(opt, "hci", &endp)) {
984 if (!*endp || *endp == ',') {
985 if (*endp)
986 if (!strstart(endp, ",vlan=", 0))
987 opt = endp + 1;
989 return bt_hci_parse(opt);
991 } else if (strstart(opt, "vhci", &endp)) {
992 if (!*endp || *endp == ',') {
993 if (*endp) {
994 if (strstart(endp, ",vlan=", &p)) {
995 vlan = strtol(p, (char **) &endp, 0);
996 if (*endp) {
997 error_report("bad scatternet '%s'", p);
998 return 1;
1000 } else {
1001 error_report("bad parameter '%s'", endp + 1);
1002 return 1;
1004 } else
1005 vlan = 0;
1007 bt_vhci_add(vlan);
1008 return 0;
1010 } else if (strstart(opt, "device:", &endp))
1011 return !bt_device_add(endp);
1013 error_report("bad bluetooth parameter '%s'", opt);
1014 return 1;
1017 static int parse_sandbox(void *opaque, QemuOpts *opts, Error **errp)
1019 /* FIXME: change this to true for 1.3 */
1020 if (qemu_opt_get_bool(opts, "enable", false)) {
1021 #ifdef CONFIG_SECCOMP
1022 if (seccomp_start() < 0) {
1023 error_report("failed to install seccomp syscall filter "
1024 "in the kernel");
1025 return -1;
1027 #else
1028 error_report("seccomp support is disabled");
1029 return -1;
1030 #endif
1033 return 0;
1036 static int parse_name(void *opaque, QemuOpts *opts, Error **errp)
1038 const char *proc_name;
1040 if (qemu_opt_get(opts, "debug-threads")) {
1041 qemu_thread_naming(qemu_opt_get_bool(opts, "debug-threads", false));
1043 qemu_name = qemu_opt_get(opts, "guest");
1045 proc_name = qemu_opt_get(opts, "process");
1046 if (proc_name) {
1047 os_set_proc_name(proc_name);
1050 return 0;
1053 bool defaults_enabled(void)
1055 return has_defaults;
1058 #ifndef _WIN32
1059 static int parse_add_fd(void *opaque, QemuOpts *opts, Error **errp)
1061 int fd, dupfd, flags;
1062 int64_t fdset_id;
1063 const char *fd_opaque = NULL;
1064 AddfdInfo *fdinfo;
1066 fd = qemu_opt_get_number(opts, "fd", -1);
1067 fdset_id = qemu_opt_get_number(opts, "set", -1);
1068 fd_opaque = qemu_opt_get(opts, "opaque");
1070 if (fd < 0) {
1071 error_report("fd option is required and must be non-negative");
1072 return -1;
1075 if (fd <= STDERR_FILENO) {
1076 error_report("fd cannot be a standard I/O stream");
1077 return -1;
1081 * All fds inherited across exec() necessarily have FD_CLOEXEC
1082 * clear, while qemu sets FD_CLOEXEC on all other fds used internally.
1084 flags = fcntl(fd, F_GETFD);
1085 if (flags == -1 || (flags & FD_CLOEXEC)) {
1086 error_report("fd is not valid or already in use");
1087 return -1;
1090 if (fdset_id < 0) {
1091 error_report("set option is required and must be non-negative");
1092 return -1;
1095 #ifdef F_DUPFD_CLOEXEC
1096 dupfd = fcntl(fd, F_DUPFD_CLOEXEC, 0);
1097 #else
1098 dupfd = dup(fd);
1099 if (dupfd != -1) {
1100 qemu_set_cloexec(dupfd);
1102 #endif
1103 if (dupfd == -1) {
1104 error_report("error duplicating fd: %s", strerror(errno));
1105 return -1;
1108 /* add the duplicate fd, and optionally the opaque string, to the fd set */
1109 fdinfo = monitor_fdset_add_fd(dupfd, true, fdset_id, !!fd_opaque, fd_opaque,
1110 &error_abort);
1111 g_free(fdinfo);
1113 return 0;
1116 static int cleanup_add_fd(void *opaque, QemuOpts *opts, Error **errp)
1118 int fd;
1120 fd = qemu_opt_get_number(opts, "fd", -1);
1121 close(fd);
1123 return 0;
1125 #endif
1127 /***********************************************************/
1128 /* QEMU Block devices */
1130 #define HD_OPTS "media=disk"
1131 #define CDROM_OPTS "media=cdrom"
1132 #define FD_OPTS ""
1133 #define PFLASH_OPTS ""
1134 #define MTD_OPTS ""
1135 #define SD_OPTS ""
1137 static int drive_init_func(void *opaque, QemuOpts *opts, Error **errp)
1139 BlockInterfaceType *block_default_type = opaque;
1141 return drive_new(opts, *block_default_type) == NULL;
1144 static int drive_enable_snapshot(void *opaque, QemuOpts *opts, Error **errp)
1146 if (qemu_opt_get(opts, "snapshot") == NULL) {
1147 qemu_opt_set(opts, "snapshot", "on", &error_abort);
1149 return 0;
1152 static void default_drive(int enable, int snapshot, BlockInterfaceType type,
1153 int index, const char *optstr)
1155 QemuOpts *opts;
1156 DriveInfo *dinfo;
1158 if (!enable || drive_get_by_index(type, index)) {
1159 return;
1162 opts = drive_add(type, index, NULL, optstr);
1163 if (snapshot) {
1164 drive_enable_snapshot(NULL, opts, NULL);
1167 dinfo = drive_new(opts, type);
1168 if (!dinfo) {
1169 exit(1);
1171 dinfo->is_default = true;
1175 static QemuOptsList qemu_smp_opts = {
1176 .name = "smp-opts",
1177 .implied_opt_name = "cpus",
1178 .merge_lists = true,
1179 .head = QTAILQ_HEAD_INITIALIZER(qemu_smp_opts.head),
1180 .desc = {
1182 .name = "cpus",
1183 .type = QEMU_OPT_NUMBER,
1184 }, {
1185 .name = "sockets",
1186 .type = QEMU_OPT_NUMBER,
1187 }, {
1188 .name = "cores",
1189 .type = QEMU_OPT_NUMBER,
1190 }, {
1191 .name = "threads",
1192 .type = QEMU_OPT_NUMBER,
1193 }, {
1194 .name = "maxcpus",
1195 .type = QEMU_OPT_NUMBER,
1197 { /*End of list */ }
1201 static void smp_parse(QemuOpts *opts)
1203 if (opts) {
1204 unsigned cpus = qemu_opt_get_number(opts, "cpus", 0);
1205 unsigned sockets = qemu_opt_get_number(opts, "sockets", 0);
1206 unsigned cores = qemu_opt_get_number(opts, "cores", 0);
1207 unsigned threads = qemu_opt_get_number(opts, "threads", 0);
1209 /* compute missing values, prefer sockets over cores over threads */
1210 if (cpus == 0 || sockets == 0) {
1211 sockets = sockets > 0 ? sockets : 1;
1212 cores = cores > 0 ? cores : 1;
1213 threads = threads > 0 ? threads : 1;
1214 if (cpus == 0) {
1215 cpus = cores * threads * sockets;
1217 } else if (cores == 0) {
1218 threads = threads > 0 ? threads : 1;
1219 cores = cpus / (sockets * threads);
1220 cores = cores > 0 ? cores : 1;
1221 } else if (threads == 0) {
1222 threads = cpus / (cores * sockets);
1223 threads = threads > 0 ? threads : 1;
1224 } else if (sockets * cores * threads < cpus) {
1225 error_report("cpu topology: "
1226 "sockets (%u) * cores (%u) * threads (%u) < "
1227 "smp_cpus (%u)",
1228 sockets, cores, threads, cpus);
1229 exit(1);
1232 max_cpus = qemu_opt_get_number(opts, "maxcpus", cpus);
1234 if (max_cpus > MAX_CPUMASK_BITS) {
1235 error_report("unsupported number of maxcpus");
1236 exit(1);
1239 if (max_cpus < cpus) {
1240 error_report("maxcpus must be equal to or greater than smp");
1241 exit(1);
1244 if (sockets * cores * threads > max_cpus) {
1245 error_report("cpu topology: "
1246 "sockets (%u) * cores (%u) * threads (%u) > "
1247 "maxcpus (%u)",
1248 sockets, cores, threads, max_cpus);
1249 exit(1);
1252 smp_cpus = cpus;
1253 smp_cores = cores;
1254 smp_threads = threads;
1257 if (smp_cpus > 1) {
1258 Error *blocker = NULL;
1259 error_setg(&blocker, QERR_REPLAY_NOT_SUPPORTED, "smp");
1260 replay_add_blocker(blocker);
1264 static void realtime_init(void)
1266 if (enable_mlock) {
1267 if (os_mlock() < 0) {
1268 error_report("locking memory failed");
1269 exit(1);
1275 static void configure_msg(QemuOpts *opts)
1277 enable_timestamp_msg = qemu_opt_get_bool(opts, "timestamp", true);
1280 /***********************************************************/
1281 /* Semihosting */
1283 typedef struct SemihostingConfig {
1284 bool enabled;
1285 SemihostingTarget target;
1286 const char **argv;
1287 int argc;
1288 const char *cmdline; /* concatenated argv */
1289 } SemihostingConfig;
1291 static SemihostingConfig semihosting;
1293 bool semihosting_enabled(void)
1295 return semihosting.enabled;
1298 SemihostingTarget semihosting_get_target(void)
1300 return semihosting.target;
1303 const char *semihosting_get_arg(int i)
1305 if (i >= semihosting.argc) {
1306 return NULL;
1308 return semihosting.argv[i];
1311 int semihosting_get_argc(void)
1313 return semihosting.argc;
1316 const char *semihosting_get_cmdline(void)
1318 if (semihosting.cmdline == NULL && semihosting.argc > 0) {
1319 semihosting.cmdline = g_strjoinv(" ", (gchar **)semihosting.argv);
1321 return semihosting.cmdline;
1324 static int add_semihosting_arg(void *opaque,
1325 const char *name, const char *val,
1326 Error **errp)
1328 SemihostingConfig *s = opaque;
1329 if (strcmp(name, "arg") == 0) {
1330 s->argc++;
1331 /* one extra element as g_strjoinv() expects NULL-terminated array */
1332 s->argv = g_realloc(s->argv, (s->argc + 1) * sizeof(void *));
1333 s->argv[s->argc - 1] = val;
1334 s->argv[s->argc] = NULL;
1336 return 0;
1339 /* Use strings passed via -kernel/-append to initialize semihosting.argv[] */
1340 static inline void semihosting_arg_fallback(const char *file, const char *cmd)
1342 char *cmd_token;
1344 /* argv[0] */
1345 add_semihosting_arg(&semihosting, "arg", file, NULL);
1347 /* split -append and initialize argv[1..n] */
1348 cmd_token = strtok(g_strdup(cmd), " ");
1349 while (cmd_token) {
1350 add_semihosting_arg(&semihosting, "arg", cmd_token, NULL);
1351 cmd_token = strtok(NULL, " ");
1355 /* Now we still need this for compatibility with XEN. */
1356 bool has_igd_gfx_passthru;
1357 static void igd_gfx_passthru(void)
1359 has_igd_gfx_passthru = current_machine->igd_gfx_passthru;
1362 /***********************************************************/
1363 /* USB devices */
1365 static int usb_device_add(const char *devname)
1367 USBDevice *dev = NULL;
1368 #ifndef CONFIG_LINUX
1369 const char *p;
1370 #endif
1372 if (!machine_usb(current_machine)) {
1373 return -1;
1376 /* drivers with .usbdevice_name entry in USBDeviceInfo */
1377 dev = usbdevice_create(devname);
1378 if (dev)
1379 goto done;
1381 /* the other ones */
1382 #ifndef CONFIG_LINUX
1383 /* only the linux version is qdev-ified, usb-bsd still needs this */
1384 if (strstart(devname, "host:", &p)) {
1385 dev = usb_host_device_open(usb_bus_find(-1), p);
1387 #endif
1388 if (!dev)
1389 return -1;
1391 done:
1392 return 0;
1395 static int usb_device_del(const char *devname)
1397 int bus_num, addr;
1398 const char *p;
1400 if (strstart(devname, "host:", &p)) {
1401 return -1;
1404 if (!machine_usb(current_machine)) {
1405 return -1;
1408 p = strchr(devname, '.');
1409 if (!p)
1410 return -1;
1411 bus_num = strtoul(devname, NULL, 0);
1412 addr = strtoul(p + 1, NULL, 0);
1414 return usb_device_delete_addr(bus_num, addr);
1417 static int usb_parse(const char *cmdline)
1419 int r;
1420 r = usb_device_add(cmdline);
1421 if (r < 0) {
1422 error_report("could not add USB device '%s'", cmdline);
1424 return r;
1427 void hmp_usb_add(Monitor *mon, const QDict *qdict)
1429 const char *devname = qdict_get_str(qdict, "devname");
1430 if (usb_device_add(devname) < 0) {
1431 error_report("could not add USB device '%s'", devname);
1435 void hmp_usb_del(Monitor *mon, const QDict *qdict)
1437 const char *devname = qdict_get_str(qdict, "devname");
1438 if (usb_device_del(devname) < 0) {
1439 error_report("could not delete USB device '%s'", devname);
1443 /***********************************************************/
1444 /* machine registration */
1446 MachineState *current_machine;
1448 static MachineClass *find_machine(const char *name)
1450 GSList *el, *machines = object_class_get_list(TYPE_MACHINE, false);
1451 MachineClass *mc = NULL;
1453 for (el = machines; el; el = el->next) {
1454 MachineClass *temp = el->data;
1456 if (!strcmp(temp->name, name)) {
1457 mc = temp;
1458 break;
1460 if (temp->alias &&
1461 !strcmp(temp->alias, name)) {
1462 mc = temp;
1463 break;
1467 g_slist_free(machines);
1468 return mc;
1471 MachineClass *find_default_machine(void)
1473 GSList *el, *machines = object_class_get_list(TYPE_MACHINE, false);
1474 MachineClass *mc = NULL;
1476 for (el = machines; el; el = el->next) {
1477 MachineClass *temp = el->data;
1479 if (temp->is_default) {
1480 mc = temp;
1481 break;
1485 g_slist_free(machines);
1486 return mc;
1489 MachineInfoList *qmp_query_machines(Error **errp)
1491 GSList *el, *machines = object_class_get_list(TYPE_MACHINE, false);
1492 MachineInfoList *mach_list = NULL;
1494 for (el = machines; el; el = el->next) {
1495 MachineClass *mc = el->data;
1496 MachineInfoList *entry;
1497 MachineInfo *info;
1499 info = g_malloc0(sizeof(*info));
1500 if (mc->is_default) {
1501 info->has_is_default = true;
1502 info->is_default = true;
1505 if (mc->alias) {
1506 info->has_alias = true;
1507 info->alias = g_strdup(mc->alias);
1510 info->name = g_strdup(mc->name);
1511 info->cpu_max = !mc->max_cpus ? 1 : mc->max_cpus;
1512 info->hotpluggable_cpus = !!mc->query_hotpluggable_cpus;
1514 entry = g_malloc0(sizeof(*entry));
1515 entry->value = info;
1516 entry->next = mach_list;
1517 mach_list = entry;
1520 g_slist_free(machines);
1521 return mach_list;
1524 static int machine_help_func(QemuOpts *opts, MachineState *machine)
1526 ObjectProperty *prop;
1527 ObjectPropertyIterator iter;
1529 if (!qemu_opt_has_help_opt(opts)) {
1530 return 0;
1533 object_property_iter_init(&iter, OBJECT(machine));
1534 while ((prop = object_property_iter_next(&iter))) {
1535 if (!prop->set) {
1536 continue;
1539 error_printf("%s.%s=%s", MACHINE_GET_CLASS(machine)->name,
1540 prop->name, prop->type);
1541 if (prop->description) {
1542 error_printf(" (%s)\n", prop->description);
1543 } else {
1544 error_printf("\n");
1548 return 1;
1551 /***********************************************************/
1552 /* main execution loop */
1554 struct vm_change_state_entry {
1555 VMChangeStateHandler *cb;
1556 void *opaque;
1557 QLIST_ENTRY (vm_change_state_entry) entries;
1560 static QLIST_HEAD(vm_change_state_head, vm_change_state_entry) vm_change_state_head;
1562 VMChangeStateEntry *qemu_add_vm_change_state_handler(VMChangeStateHandler *cb,
1563 void *opaque)
1565 VMChangeStateEntry *e;
1567 e = g_malloc0(sizeof (*e));
1569 e->cb = cb;
1570 e->opaque = opaque;
1571 QLIST_INSERT_HEAD(&vm_change_state_head, e, entries);
1572 return e;
1575 void qemu_del_vm_change_state_handler(VMChangeStateEntry *e)
1577 QLIST_REMOVE (e, entries);
1578 g_free (e);
1581 void vm_state_notify(int running, RunState state)
1583 VMChangeStateEntry *e, *next;
1585 trace_vm_state_notify(running, state);
1587 QLIST_FOREACH_SAFE(e, &vm_change_state_head, entries, next) {
1588 e->cb(e->opaque, running, state);
1592 /* reset/shutdown handler */
1594 typedef struct QEMUResetEntry {
1595 QTAILQ_ENTRY(QEMUResetEntry) entry;
1596 QEMUResetHandler *func;
1597 void *opaque;
1598 } QEMUResetEntry;
1600 static QTAILQ_HEAD(reset_handlers, QEMUResetEntry) reset_handlers =
1601 QTAILQ_HEAD_INITIALIZER(reset_handlers);
1602 static int reset_requested;
1603 static int shutdown_requested, shutdown_signal = -1;
1604 static pid_t shutdown_pid;
1605 static int powerdown_requested;
1606 static int debug_requested;
1607 static int suspend_requested;
1608 static WakeupReason wakeup_reason;
1609 static NotifierList powerdown_notifiers =
1610 NOTIFIER_LIST_INITIALIZER(powerdown_notifiers);
1611 static NotifierList suspend_notifiers =
1612 NOTIFIER_LIST_INITIALIZER(suspend_notifiers);
1613 static NotifierList wakeup_notifiers =
1614 NOTIFIER_LIST_INITIALIZER(wakeup_notifiers);
1615 static uint32_t wakeup_reason_mask = ~(1 << QEMU_WAKEUP_REASON_NONE);
1617 int qemu_shutdown_requested_get(void)
1619 return shutdown_requested;
1622 int qemu_reset_requested_get(void)
1624 return reset_requested;
1627 static int qemu_shutdown_requested(void)
1629 return atomic_xchg(&shutdown_requested, 0);
1632 static void qemu_kill_report(void)
1634 if (!qtest_driver() && shutdown_signal != -1) {
1635 if (shutdown_pid == 0) {
1636 /* This happens for eg ^C at the terminal, so it's worth
1637 * avoiding printing an odd message in that case.
1639 error_report("terminating on signal %d", shutdown_signal);
1640 } else {
1641 error_report("terminating on signal %d from pid " FMT_pid,
1642 shutdown_signal, shutdown_pid);
1644 shutdown_signal = -1;
1648 static int qemu_reset_requested(void)
1650 int r = reset_requested;
1651 if (r && replay_checkpoint(CHECKPOINT_RESET_REQUESTED)) {
1652 reset_requested = 0;
1653 return r;
1655 return false;
1658 static int qemu_suspend_requested(void)
1660 int r = suspend_requested;
1661 if (r && replay_checkpoint(CHECKPOINT_SUSPEND_REQUESTED)) {
1662 suspend_requested = 0;
1663 return r;
1665 return false;
1668 static WakeupReason qemu_wakeup_requested(void)
1670 return wakeup_reason;
1673 static int qemu_powerdown_requested(void)
1675 int r = powerdown_requested;
1676 powerdown_requested = 0;
1677 return r;
1680 static int qemu_debug_requested(void)
1682 int r = debug_requested;
1683 debug_requested = 0;
1684 return r;
1687 void qemu_register_reset(QEMUResetHandler *func, void *opaque)
1689 QEMUResetEntry *re = g_malloc0(sizeof(QEMUResetEntry));
1691 re->func = func;
1692 re->opaque = opaque;
1693 QTAILQ_INSERT_TAIL(&reset_handlers, re, entry);
1696 void qemu_unregister_reset(QEMUResetHandler *func, void *opaque)
1698 QEMUResetEntry *re;
1700 QTAILQ_FOREACH(re, &reset_handlers, entry) {
1701 if (re->func == func && re->opaque == opaque) {
1702 QTAILQ_REMOVE(&reset_handlers, re, entry);
1703 g_free(re);
1704 return;
1709 void qemu_devices_reset(void)
1711 QEMUResetEntry *re, *nre;
1713 /* reset all devices */
1714 QTAILQ_FOREACH_SAFE(re, &reset_handlers, entry, nre) {
1715 re->func(re->opaque);
1719 void qemu_system_reset(bool report)
1721 MachineClass *mc;
1723 mc = current_machine ? MACHINE_GET_CLASS(current_machine) : NULL;
1725 cpu_synchronize_all_states();
1727 if (mc && mc->reset) {
1728 mc->reset();
1729 } else {
1730 qemu_devices_reset();
1732 if (report) {
1733 qapi_event_send_reset(&error_abort);
1735 cpu_synchronize_all_post_reset();
1738 void qemu_system_guest_panicked(void)
1740 if (current_cpu) {
1741 current_cpu->crash_occurred = true;
1743 qapi_event_send_guest_panicked(GUEST_PANIC_ACTION_PAUSE, &error_abort);
1744 vm_stop(RUN_STATE_GUEST_PANICKED);
1747 void qemu_system_reset_request(void)
1749 if (no_reboot) {
1750 shutdown_requested = 1;
1751 } else {
1752 reset_requested = 1;
1754 cpu_stop_current();
1755 qemu_notify_event();
1758 static void qemu_system_suspend(void)
1760 pause_all_vcpus();
1761 notifier_list_notify(&suspend_notifiers, NULL);
1762 runstate_set(RUN_STATE_SUSPENDED);
1763 qapi_event_send_suspend(&error_abort);
1766 void qemu_system_suspend_request(void)
1768 if (runstate_check(RUN_STATE_SUSPENDED)) {
1769 return;
1771 suspend_requested = 1;
1772 cpu_stop_current();
1773 qemu_notify_event();
1776 void qemu_register_suspend_notifier(Notifier *notifier)
1778 notifier_list_add(&suspend_notifiers, notifier);
1781 void qemu_system_wakeup_request(WakeupReason reason)
1783 trace_system_wakeup_request(reason);
1785 if (!runstate_check(RUN_STATE_SUSPENDED)) {
1786 return;
1788 if (!(wakeup_reason_mask & (1 << reason))) {
1789 return;
1791 runstate_set(RUN_STATE_RUNNING);
1792 wakeup_reason = reason;
1793 qemu_notify_event();
1796 void qemu_system_wakeup_enable(WakeupReason reason, bool enabled)
1798 if (enabled) {
1799 wakeup_reason_mask |= (1 << reason);
1800 } else {
1801 wakeup_reason_mask &= ~(1 << reason);
1805 void qemu_register_wakeup_notifier(Notifier *notifier)
1807 notifier_list_add(&wakeup_notifiers, notifier);
1810 void qemu_system_killed(int signal, pid_t pid)
1812 shutdown_signal = signal;
1813 shutdown_pid = pid;
1814 no_shutdown = 0;
1816 /* Cannot call qemu_system_shutdown_request directly because
1817 * we are in a signal handler.
1819 shutdown_requested = 1;
1820 qemu_notify_event();
1823 void qemu_system_shutdown_request(void)
1825 trace_qemu_system_shutdown_request();
1826 replay_shutdown_request();
1827 shutdown_requested = 1;
1828 qemu_notify_event();
1831 static void qemu_system_powerdown(void)
1833 qapi_event_send_powerdown(&error_abort);
1834 notifier_list_notify(&powerdown_notifiers, NULL);
1837 void qemu_system_powerdown_request(void)
1839 trace_qemu_system_powerdown_request();
1840 powerdown_requested = 1;
1841 qemu_notify_event();
1844 void qemu_register_powerdown_notifier(Notifier *notifier)
1846 notifier_list_add(&powerdown_notifiers, notifier);
1849 void qemu_system_debug_request(void)
1851 debug_requested = 1;
1852 qemu_notify_event();
1855 static bool main_loop_should_exit(void)
1857 RunState r;
1858 if (qemu_debug_requested()) {
1859 vm_stop(RUN_STATE_DEBUG);
1861 if (qemu_suspend_requested()) {
1862 qemu_system_suspend();
1864 if (qemu_shutdown_requested()) {
1865 qemu_kill_report();
1866 qapi_event_send_shutdown(&error_abort);
1867 if (no_shutdown) {
1868 vm_stop(RUN_STATE_SHUTDOWN);
1869 } else {
1870 return true;
1873 if (qemu_reset_requested()) {
1874 pause_all_vcpus();
1875 qemu_system_reset(VMRESET_REPORT);
1876 resume_all_vcpus();
1877 if (!runstate_check(RUN_STATE_RUNNING) &&
1878 !runstate_check(RUN_STATE_INMIGRATE)) {
1879 runstate_set(RUN_STATE_PRELAUNCH);
1882 if (qemu_wakeup_requested()) {
1883 pause_all_vcpus();
1884 qemu_system_reset(VMRESET_SILENT);
1885 notifier_list_notify(&wakeup_notifiers, &wakeup_reason);
1886 wakeup_reason = QEMU_WAKEUP_REASON_NONE;
1887 resume_all_vcpus();
1888 qapi_event_send_wakeup(&error_abort);
1890 if (qemu_powerdown_requested()) {
1891 qemu_system_powerdown();
1893 if (qemu_vmstop_requested(&r)) {
1894 vm_stop(r);
1896 return false;
1899 static void main_loop(void)
1901 bool nonblocking;
1902 int last_io = 0;
1903 #ifdef CONFIG_PROFILER
1904 int64_t ti;
1905 #endif
1906 do {
1907 nonblocking = !kvm_enabled() && !xen_enabled() && last_io > 0;
1908 #ifdef CONFIG_PROFILER
1909 ti = profile_getclock();
1910 #endif
1911 last_io = main_loop_wait(nonblocking);
1912 #ifdef CONFIG_PROFILER
1913 dev_time += profile_getclock() - ti;
1914 #endif
1915 } while (!main_loop_should_exit());
1918 static void version(void)
1920 printf("QEMU emulator version " QEMU_VERSION QEMU_PKGVERSION ", Copyright (c) 2003-2008 Fabrice Bellard\n");
1923 static void QEMU_NORETURN help(int exitcode)
1925 version();
1926 printf("usage: %s [options] [disk_image]\n\n"
1927 "'disk_image' is a raw hard disk image for IDE hard disk 0\n\n",
1928 error_get_progname());
1930 #define QEMU_OPTIONS_GENERATE_HELP
1931 #include "qemu-options-wrapper.h"
1933 printf("\nDuring emulation, the following keys are useful:\n"
1934 "ctrl-alt-f toggle full screen\n"
1935 "ctrl-alt-n switch to virtual console 'n'\n"
1936 "ctrl-alt toggle mouse and keyboard grab\n"
1937 "\n"
1938 "When using -nographic, press 'ctrl-a h' to get some help.\n");
1940 exit(exitcode);
1943 #define HAS_ARG 0x0001
1945 typedef struct QEMUOption {
1946 const char *name;
1947 int flags;
1948 int index;
1949 uint32_t arch_mask;
1950 } QEMUOption;
1952 static const QEMUOption qemu_options[] = {
1953 { "h", 0, QEMU_OPTION_h, QEMU_ARCH_ALL },
1954 #define QEMU_OPTIONS_GENERATE_OPTIONS
1955 #include "qemu-options-wrapper.h"
1956 { NULL },
1959 typedef struct VGAInterfaceInfo {
1960 const char *opt_name; /* option name */
1961 const char *name; /* human-readable name */
1962 /* Class names indicating that support is available.
1963 * If no class is specified, the interface is always available */
1964 const char *class_names[2];
1965 } VGAInterfaceInfo;
1967 static VGAInterfaceInfo vga_interfaces[VGA_TYPE_MAX] = {
1968 [VGA_NONE] = {
1969 .opt_name = "none",
1971 [VGA_STD] = {
1972 .opt_name = "std",
1973 .name = "standard VGA",
1974 .class_names = { "VGA", "isa-vga" },
1976 [VGA_CIRRUS] = {
1977 .opt_name = "cirrus",
1978 .name = "Cirrus VGA",
1979 .class_names = { "cirrus-vga", "isa-cirrus-vga" },
1981 [VGA_VMWARE] = {
1982 .opt_name = "vmware",
1983 .name = "VMWare SVGA",
1984 .class_names = { "vmware-svga" },
1986 [VGA_VIRTIO] = {
1987 .opt_name = "virtio",
1988 .name = "Virtio VGA",
1989 .class_names = { "virtio-vga" },
1991 [VGA_QXL] = {
1992 .opt_name = "qxl",
1993 .name = "QXL VGA",
1994 .class_names = { "qxl-vga" },
1996 [VGA_TCX] = {
1997 .opt_name = "tcx",
1998 .name = "TCX framebuffer",
1999 .class_names = { "SUNW,tcx" },
2001 [VGA_CG3] = {
2002 .opt_name = "cg3",
2003 .name = "CG3 framebuffer",
2004 .class_names = { "cgthree" },
2006 [VGA_XENFB] = {
2007 .opt_name = "xenfb",
2011 static bool vga_interface_available(VGAInterfaceType t)
2013 VGAInterfaceInfo *ti = &vga_interfaces[t];
2015 assert(t < VGA_TYPE_MAX);
2016 return !ti->class_names[0] ||
2017 object_class_by_name(ti->class_names[0]) ||
2018 object_class_by_name(ti->class_names[1]);
2021 static void select_vgahw(const char *p)
2023 const char *opts;
2024 int t;
2026 assert(vga_interface_type == VGA_NONE);
2027 for (t = 0; t < VGA_TYPE_MAX; t++) {
2028 VGAInterfaceInfo *ti = &vga_interfaces[t];
2029 if (ti->opt_name && strstart(p, ti->opt_name, &opts)) {
2030 if (!vga_interface_available(t)) {
2031 error_report("%s not available", ti->name);
2032 exit(1);
2034 vga_interface_type = t;
2035 break;
2038 if (t == VGA_TYPE_MAX) {
2039 invalid_vga:
2040 error_report("unknown vga type: %s", p);
2041 exit(1);
2043 while (*opts) {
2044 const char *nextopt;
2046 if (strstart(opts, ",retrace=", &nextopt)) {
2047 opts = nextopt;
2048 if (strstart(opts, "dumb", &nextopt))
2049 vga_retrace_method = VGA_RETRACE_DUMB;
2050 else if (strstart(opts, "precise", &nextopt))
2051 vga_retrace_method = VGA_RETRACE_PRECISE;
2052 else goto invalid_vga;
2053 } else goto invalid_vga;
2054 opts = nextopt;
2058 typedef enum DisplayType {
2059 DT_DEFAULT,
2060 DT_CURSES,
2061 DT_SDL,
2062 DT_COCOA,
2063 DT_GTK,
2064 DT_NONE,
2065 } DisplayType;
2067 static DisplayType select_display(const char *p)
2069 const char *opts;
2070 DisplayType display = DT_DEFAULT;
2072 if (strstart(p, "sdl", &opts)) {
2073 #ifdef CONFIG_SDL
2074 display = DT_SDL;
2075 while (*opts) {
2076 const char *nextopt;
2078 if (strstart(opts, ",frame=", &nextopt)) {
2079 opts = nextopt;
2080 if (strstart(opts, "on", &nextopt)) {
2081 no_frame = 0;
2082 } else if (strstart(opts, "off", &nextopt)) {
2083 no_frame = 1;
2084 } else {
2085 goto invalid_sdl_args;
2087 } else if (strstart(opts, ",alt_grab=", &nextopt)) {
2088 opts = nextopt;
2089 if (strstart(opts, "on", &nextopt)) {
2090 alt_grab = 1;
2091 } else if (strstart(opts, "off", &nextopt)) {
2092 alt_grab = 0;
2093 } else {
2094 goto invalid_sdl_args;
2096 } else if (strstart(opts, ",ctrl_grab=", &nextopt)) {
2097 opts = nextopt;
2098 if (strstart(opts, "on", &nextopt)) {
2099 ctrl_grab = 1;
2100 } else if (strstart(opts, "off", &nextopt)) {
2101 ctrl_grab = 0;
2102 } else {
2103 goto invalid_sdl_args;
2105 } else if (strstart(opts, ",window_close=", &nextopt)) {
2106 opts = nextopt;
2107 if (strstart(opts, "on", &nextopt)) {
2108 no_quit = 0;
2109 } else if (strstart(opts, "off", &nextopt)) {
2110 no_quit = 1;
2111 } else {
2112 goto invalid_sdl_args;
2114 } else if (strstart(opts, ",gl=", &nextopt)) {
2115 opts = nextopt;
2116 if (strstart(opts, "on", &nextopt)) {
2117 request_opengl = 1;
2118 } else if (strstart(opts, "off", &nextopt)) {
2119 request_opengl = 0;
2120 } else {
2121 goto invalid_sdl_args;
2123 } else {
2124 invalid_sdl_args:
2125 error_report("invalid SDL option string");
2126 exit(1);
2128 opts = nextopt;
2130 #else
2131 error_report("SDL support is disabled");
2132 exit(1);
2133 #endif
2134 } else if (strstart(p, "vnc", &opts)) {
2135 if (*opts == '=') {
2136 vnc_parse(opts + 1, &error_fatal);
2137 } else {
2138 error_report("VNC requires a display argument vnc=<display>");
2139 exit(1);
2141 } else if (strstart(p, "curses", &opts)) {
2142 #ifdef CONFIG_CURSES
2143 display = DT_CURSES;
2144 #else
2145 error_report("curses support is disabled");
2146 exit(1);
2147 #endif
2148 } else if (strstart(p, "gtk", &opts)) {
2149 #ifdef CONFIG_GTK
2150 display = DT_GTK;
2151 while (*opts) {
2152 const char *nextopt;
2154 if (strstart(opts, ",grab_on_hover=", &nextopt)) {
2155 opts = nextopt;
2156 if (strstart(opts, "on", &nextopt)) {
2157 grab_on_hover = true;
2158 } else if (strstart(opts, "off", &nextopt)) {
2159 grab_on_hover = false;
2160 } else {
2161 goto invalid_gtk_args;
2163 } else if (strstart(opts, ",gl=", &nextopt)) {
2164 opts = nextopt;
2165 if (strstart(opts, "on", &nextopt)) {
2166 request_opengl = 1;
2167 } else if (strstart(opts, "off", &nextopt)) {
2168 request_opengl = 0;
2169 } else {
2170 goto invalid_gtk_args;
2172 } else {
2173 invalid_gtk_args:
2174 error_report("invalid GTK option string");
2175 exit(1);
2177 opts = nextopt;
2179 #else
2180 error_report("GTK support is disabled");
2181 exit(1);
2182 #endif
2183 } else if (strstart(p, "none", &opts)) {
2184 display = DT_NONE;
2185 } else {
2186 error_report("unknown display type");
2187 exit(1);
2190 return display;
2193 static int balloon_parse(const char *arg)
2195 QemuOpts *opts;
2197 if (strcmp(arg, "none") == 0) {
2198 return 0;
2201 if (!strncmp(arg, "virtio", 6)) {
2202 if (arg[6] == ',') {
2203 /* have params -> parse them */
2204 opts = qemu_opts_parse_noisily(qemu_find_opts("device"), arg + 7,
2205 false);
2206 if (!opts)
2207 return -1;
2208 } else {
2209 /* create empty opts */
2210 opts = qemu_opts_create(qemu_find_opts("device"), NULL, 0,
2211 &error_abort);
2213 qemu_opt_set(opts, "driver", "virtio-balloon", &error_abort);
2214 return 0;
2217 return -1;
2220 #if defined(CONFIG_RUBY)
2221 /* Ruby interface for QEMU. See README.EXT for programming example. */
2222 #warning mit ruby
2223 #include <ruby.h>
2225 static VALUE qemu_open(int argc, VALUE *argv, VALUE klass)
2227 int i;
2228 for (i = 0; i < argc; i++) {
2229 argv[i] = StringValue(argv[i]);
2230 fprintf(stderr, "argv[%d] = %s\n", i, StringValuePtr(argv[i]));
2232 //~ if (rb_scan_args(argc, argv, "11", &file, &vmode) == 1) {
2233 //~ mode = 0666; /* default value */
2234 //~ }
2235 return INT2FIX(argc);
2238 void Init_qemu(void)
2240 printf("%s\n", __func__);
2241 VALUE cQEMU = rb_define_class("QEMU", rb_cObject);
2242 rb_define_singleton_method(cQEMU, "run", qemu_open, -1);
2243 #if 0
2244 rb_define_method();
2245 #endif
2247 #endif /* CONFIG_RUBY */
2249 #if defined(CONFIG_DLL)
2250 BOOL APIENTRY DllMain(HINSTANCE hModule, DWORD ul_reason_for_call, LPVOID data)
2252 return FALSE;
2254 #endif /* CONFIG_DLL */
2256 char *qemu_find_file(int type, const char *name)
2258 int i;
2259 const char *subdir;
2260 char *buf;
2262 /* Try the name as a straight path first */
2263 if (access(name, R_OK) == 0) {
2264 trace_load_file(name, name);
2265 return g_strdup(name);
2268 switch (type) {
2269 case QEMU_FILE_TYPE_BIOS:
2270 subdir = "";
2271 break;
2272 case QEMU_FILE_TYPE_KEYMAP:
2273 subdir = "keymaps/";
2274 break;
2275 default:
2276 abort();
2279 for (i = 0; i < data_dir_idx; i++) {
2280 buf = g_strdup_printf("%s/%s%s", data_dir[i], subdir, name);
2281 if (access(buf, R_OK) == 0) {
2282 trace_load_file(name, buf);
2283 return buf;
2285 g_free(buf);
2287 return NULL;
2290 static inline bool nonempty_str(const char *str)
2292 return str && *str;
2295 static int parse_fw_cfg(void *opaque, QemuOpts *opts, Error **errp)
2297 gchar *buf;
2298 size_t size;
2299 const char *name, *file, *str;
2300 FWCfgState *fw_cfg = (FWCfgState *) opaque;
2302 if (fw_cfg == NULL) {
2303 error_report("fw_cfg device not available");
2304 return -1;
2306 name = qemu_opt_get(opts, "name");
2307 file = qemu_opt_get(opts, "file");
2308 str = qemu_opt_get(opts, "string");
2310 /* we need name and either a file or the content string */
2311 if (!(nonempty_str(name) && (nonempty_str(file) || nonempty_str(str)))) {
2312 error_report("invalid argument(s)");
2313 return -1;
2315 if (nonempty_str(file) && nonempty_str(str)) {
2316 error_report("file and string are mutually exclusive");
2317 return -1;
2319 if (strlen(name) > FW_CFG_MAX_FILE_PATH - 1) {
2320 error_report("name too long (max. %d char)", FW_CFG_MAX_FILE_PATH - 1);
2321 return -1;
2323 if (strncmp(name, "opt/", 4) != 0) {
2324 error_report("warning: externally provided fw_cfg item names "
2325 "should be prefixed with \"opt/\"");
2327 if (nonempty_str(str)) {
2328 size = strlen(str); /* NUL terminator NOT included in fw_cfg blob */
2329 buf = g_memdup(str, size);
2330 } else {
2331 if (!g_file_get_contents(file, &buf, &size, NULL)) {
2332 error_report("can't load %s", file);
2333 return -1;
2336 /* For legacy, keep user files in a specific global order. */
2337 fw_cfg_set_order_override(fw_cfg, FW_CFG_ORDER_OVERRIDE_USER);
2338 fw_cfg_add_file(fw_cfg, name, buf, size);
2339 fw_cfg_reset_order_override(fw_cfg);
2340 return 0;
2343 static int device_help_func(void *opaque, QemuOpts *opts, Error **errp)
2345 return qdev_device_help(opts);
2348 static int device_init_func(void *opaque, QemuOpts *opts, Error **errp)
2350 Error *err = NULL;
2351 DeviceState *dev;
2353 dev = qdev_device_add(opts, &err);
2354 if (!dev) {
2355 error_report_err(err);
2356 return -1;
2358 object_unref(OBJECT(dev));
2359 return 0;
2362 static int chardev_init_func(void *opaque, QemuOpts *opts, Error **errp)
2364 Error *local_err = NULL;
2366 qemu_chr_new_from_opts(opts, NULL, &local_err);
2367 if (local_err) {
2368 error_report_err(local_err);
2369 return -1;
2371 return 0;
2374 #ifdef CONFIG_VIRTFS
2375 static int fsdev_init_func(void *opaque, QemuOpts *opts, Error **errp)
2377 return qemu_fsdev_add(opts);
2379 #endif
2381 static int mon_init_func(void *opaque, QemuOpts *opts, Error **errp)
2383 CharDriverState *chr;
2384 const char *chardev;
2385 const char *mode;
2386 int flags;
2388 mode = qemu_opt_get(opts, "mode");
2389 if (mode == NULL) {
2390 mode = "readline";
2392 if (strcmp(mode, "readline") == 0) {
2393 flags = MONITOR_USE_READLINE;
2394 } else if (strcmp(mode, "control") == 0) {
2395 flags = MONITOR_USE_CONTROL;
2396 } else {
2397 error_report("unknown monitor mode \"%s\"", mode);
2398 exit(1);
2401 if (qemu_opt_get_bool(opts, "pretty", 0))
2402 flags |= MONITOR_USE_PRETTY;
2404 if (qemu_opt_get_bool(opts, "default", 0))
2405 flags |= MONITOR_IS_DEFAULT;
2407 chardev = qemu_opt_get(opts, "chardev");
2408 chr = qemu_chr_find(chardev);
2409 if (chr == NULL) {
2410 error_report("chardev \"%s\" not found", chardev);
2411 exit(1);
2414 qemu_chr_fe_claim_no_fail(chr);
2415 monitor_init(chr, flags);
2416 return 0;
2419 static void monitor_parse(const char *optarg, const char *mode, bool pretty)
2421 static int monitor_device_index = 0;
2422 QemuOpts *opts;
2423 const char *p;
2424 char label[32];
2425 int def = 0;
2427 if (strstart(optarg, "chardev:", &p)) {
2428 snprintf(label, sizeof(label), "%s", p);
2429 } else {
2430 snprintf(label, sizeof(label), "compat_monitor%d",
2431 monitor_device_index);
2432 if (monitor_device_index == 0) {
2433 def = 1;
2435 opts = qemu_chr_parse_compat(label, optarg);
2436 if (!opts) {
2437 error_report("parse error: %s", optarg);
2438 exit(1);
2442 opts = qemu_opts_create(qemu_find_opts("mon"), label, 1, &error_fatal);
2443 qemu_opt_set(opts, "mode", mode, &error_abort);
2444 qemu_opt_set(opts, "chardev", label, &error_abort);
2445 qemu_opt_set_bool(opts, "pretty", pretty, &error_abort);
2446 if (def)
2447 qemu_opt_set(opts, "default", "on", &error_abort);
2448 monitor_device_index++;
2451 struct device_config {
2452 enum {
2453 DEV_USB, /* -usbdevice */
2454 DEV_BT, /* -bt */
2455 DEV_SERIAL, /* -serial */
2456 DEV_PARALLEL, /* -parallel */
2457 DEV_VIRTCON, /* -virtioconsole */
2458 DEV_DEBUGCON, /* -debugcon */
2459 DEV_GDB, /* -gdb, -s */
2460 DEV_SCLP, /* s390 sclp */
2461 } type;
2462 const char *cmdline;
2463 Location loc;
2464 QTAILQ_ENTRY(device_config) next;
2467 static QTAILQ_HEAD(, device_config) device_configs =
2468 QTAILQ_HEAD_INITIALIZER(device_configs);
2470 static void add_device_config(int type, const char *cmdline)
2472 struct device_config *conf;
2474 conf = g_malloc0(sizeof(*conf));
2475 conf->type = type;
2476 conf->cmdline = cmdline;
2477 loc_save(&conf->loc);
2478 QTAILQ_INSERT_TAIL(&device_configs, conf, next);
2481 static int foreach_device_config(int type, int (*func)(const char *cmdline))
2483 struct device_config *conf;
2484 int rc;
2486 QTAILQ_FOREACH(conf, &device_configs, next) {
2487 if (conf->type != type)
2488 continue;
2489 loc_push_restore(&conf->loc);
2490 rc = func(conf->cmdline);
2491 loc_pop(&conf->loc);
2492 if (rc) {
2493 return rc;
2496 return 0;
2499 static int serial_parse(const char *devname)
2501 static int index = 0;
2502 char label[32];
2504 if (strcmp(devname, "none") == 0)
2505 return 0;
2506 if (index == MAX_SERIAL_PORTS) {
2507 error_report("too many serial ports");
2508 exit(1);
2510 snprintf(label, sizeof(label), "serial%d", index);
2511 serial_hds[index] = qemu_chr_new(label, devname, NULL);
2512 if (!serial_hds[index]) {
2513 error_report("could not connect serial device"
2514 " to character backend '%s'", devname);
2515 return -1;
2517 index++;
2518 return 0;
2521 static int parallel_parse(const char *devname)
2523 static int index = 0;
2524 char label[32];
2526 if (strcmp(devname, "none") == 0)
2527 return 0;
2528 if (index == MAX_PARALLEL_PORTS) {
2529 error_report("too many parallel ports");
2530 exit(1);
2532 snprintf(label, sizeof(label), "parallel%d", index);
2533 parallel_hds[index] = qemu_chr_new(label, devname, NULL);
2534 if (!parallel_hds[index]) {
2535 error_report("could not connect parallel device"
2536 " to character backend '%s'", devname);
2537 return -1;
2539 index++;
2540 return 0;
2543 static int virtcon_parse(const char *devname)
2545 QemuOptsList *device = qemu_find_opts("device");
2546 static int index = 0;
2547 char label[32];
2548 QemuOpts *bus_opts, *dev_opts;
2550 if (strcmp(devname, "none") == 0)
2551 return 0;
2552 if (index == MAX_VIRTIO_CONSOLES) {
2553 error_report("too many virtio consoles");
2554 exit(1);
2557 bus_opts = qemu_opts_create(device, NULL, 0, &error_abort);
2558 qemu_opt_set(bus_opts, "driver", "virtio-serial", &error_abort);
2560 dev_opts = qemu_opts_create(device, NULL, 0, &error_abort);
2561 qemu_opt_set(dev_opts, "driver", "virtconsole", &error_abort);
2563 snprintf(label, sizeof(label), "virtcon%d", index);
2564 virtcon_hds[index] = qemu_chr_new(label, devname, NULL);
2565 if (!virtcon_hds[index]) {
2566 error_report("could not connect virtio console"
2567 " to character backend '%s'", devname);
2568 return -1;
2570 qemu_opt_set(dev_opts, "chardev", label, &error_abort);
2572 index++;
2573 return 0;
2576 static int sclp_parse(const char *devname)
2578 QemuOptsList *device = qemu_find_opts("device");
2579 static int index = 0;
2580 char label[32];
2581 QemuOpts *dev_opts;
2583 if (strcmp(devname, "none") == 0) {
2584 return 0;
2586 if (index == MAX_SCLP_CONSOLES) {
2587 error_report("too many sclp consoles");
2588 exit(1);
2591 assert(arch_type == QEMU_ARCH_S390X);
2593 dev_opts = qemu_opts_create(device, NULL, 0, NULL);
2594 qemu_opt_set(dev_opts, "driver", "sclpconsole", &error_abort);
2596 snprintf(label, sizeof(label), "sclpcon%d", index);
2597 sclp_hds[index] = qemu_chr_new(label, devname, NULL);
2598 if (!sclp_hds[index]) {
2599 error_report("could not connect sclp console"
2600 " to character backend '%s'", devname);
2601 return -1;
2603 qemu_opt_set(dev_opts, "chardev", label, &error_abort);
2605 index++;
2606 return 0;
2609 static int debugcon_parse(const char *devname)
2611 QemuOpts *opts;
2613 if (!qemu_chr_new("debugcon", devname, NULL)) {
2614 exit(1);
2616 opts = qemu_opts_create(qemu_find_opts("device"), "debugcon", 1, NULL);
2617 if (!opts) {
2618 error_report("already have a debugcon device");
2619 exit(1);
2621 qemu_opt_set(opts, "driver", "isa-debugcon", &error_abort);
2622 qemu_opt_set(opts, "chardev", "debugcon", &error_abort);
2623 return 0;
2626 static gint machine_class_cmp(gconstpointer a, gconstpointer b)
2628 const MachineClass *mc1 = a, *mc2 = b;
2629 int res;
2631 if (mc1->family == NULL) {
2632 if (mc2->family == NULL) {
2633 /* Compare standalone machine types against each other; they sort
2634 * in increasing order.
2636 return strcmp(object_class_get_name(OBJECT_CLASS(mc1)),
2637 object_class_get_name(OBJECT_CLASS(mc2)));
2640 /* Standalone machine types sort after families. */
2641 return 1;
2644 if (mc2->family == NULL) {
2645 /* Families sort before standalone machine types. */
2646 return -1;
2649 /* Families sort between each other alphabetically increasingly. */
2650 res = strcmp(mc1->family, mc2->family);
2651 if (res != 0) {
2652 return res;
2655 /* Within the same family, machine types sort in decreasing order. */
2656 return strcmp(object_class_get_name(OBJECT_CLASS(mc2)),
2657 object_class_get_name(OBJECT_CLASS(mc1)));
2660 static MachineClass *machine_parse(const char *name)
2662 MachineClass *mc = NULL;
2663 GSList *el, *machines = object_class_get_list(TYPE_MACHINE, false);
2665 if (name) {
2666 mc = find_machine(name);
2668 if (mc) {
2669 g_slist_free(machines);
2670 return mc;
2672 if (name && !is_help_option(name)) {
2673 error_report("unsupported machine type");
2674 error_printf("Use -machine help to list supported machines\n");
2675 } else {
2676 printf("Supported machines are:\n");
2677 machines = g_slist_sort(machines, machine_class_cmp);
2678 for (el = machines; el; el = el->next) {
2679 MachineClass *mc = el->data;
2680 if (mc->alias) {
2681 printf("%-20s %s (alias of %s)\n", mc->alias, mc->desc, mc->name);
2683 printf("%-20s %s%s\n", mc->name, mc->desc,
2684 mc->is_default ? " (default)" : "");
2688 g_slist_free(machines);
2689 exit(!name || !is_help_option(name));
2692 void qemu_add_exit_notifier(Notifier *notify)
2694 notifier_list_add(&exit_notifiers, notify);
2697 void qemu_remove_exit_notifier(Notifier *notify)
2699 notifier_remove(notify);
2702 static void qemu_run_exit_notifiers(void)
2704 notifier_list_notify(&exit_notifiers, NULL);
2707 static bool machine_init_done;
2709 void qemu_add_machine_init_done_notifier(Notifier *notify)
2711 notifier_list_add(&machine_init_done_notifiers, notify);
2712 if (machine_init_done) {
2713 notify->notify(notify, NULL);
2717 void qemu_remove_machine_init_done_notifier(Notifier *notify)
2719 notifier_remove(notify);
2722 static void qemu_run_machine_init_done_notifiers(void)
2724 notifier_list_notify(&machine_init_done_notifiers, NULL);
2725 machine_init_done = true;
2728 static const QEMUOption *lookup_opt(int argc, char **argv,
2729 const char **poptarg, int *poptind)
2731 const QEMUOption *popt;
2732 int optind = *poptind;
2733 char *r = argv[optind];
2734 const char *optarg;
2736 loc_set_cmdline(argv, optind, 1);
2737 optind++;
2738 /* Treat --foo the same as -foo. */
2739 if (r[1] == '-')
2740 r++;
2741 popt = qemu_options;
2742 for(;;) {
2743 if (!popt->name) {
2744 error_report("invalid option");
2745 exit(1);
2747 if (!strcmp(popt->name, r + 1))
2748 break;
2749 popt++;
2751 if (popt->flags & HAS_ARG) {
2752 if (optind >= argc) {
2753 error_report("requires an argument");
2754 exit(1);
2756 optarg = argv[optind++];
2757 loc_set_cmdline(argv, optind - 2, 2);
2758 } else {
2759 optarg = NULL;
2762 *poptarg = optarg;
2763 *poptind = optind;
2765 return popt;
2768 static MachineClass *select_machine(void)
2770 MachineClass *machine_class = find_default_machine();
2771 const char *optarg;
2772 QemuOpts *opts;
2773 Location loc;
2775 loc_push_none(&loc);
2777 opts = qemu_get_machine_opts();
2778 qemu_opts_loc_restore(opts);
2780 optarg = qemu_opt_get(opts, "type");
2781 if (optarg) {
2782 machine_class = machine_parse(optarg);
2785 if (!machine_class) {
2786 error_report("No machine specified, and there is no default");
2787 error_printf("Use -machine help to list supported machines\n");
2788 exit(1);
2791 loc_pop(&loc);
2792 return machine_class;
2795 static int machine_set_property(void *opaque,
2796 const char *name, const char *value,
2797 Error **errp)
2799 Object *obj = OBJECT(opaque);
2800 Error *local_err = NULL;
2801 char *c, *qom_name;
2803 if (strcmp(name, "type") == 0) {
2804 return 0;
2807 qom_name = g_strdup(name);
2808 c = qom_name;
2809 while (*c++) {
2810 if (*c == '_') {
2811 *c = '-';
2815 object_property_parse(obj, value, qom_name, &local_err);
2816 g_free(qom_name);
2818 if (local_err) {
2819 error_report_err(local_err);
2820 return -1;
2823 return 0;
2828 * Initial object creation happens before all other
2829 * QEMU data types are created. The majority of objects
2830 * can be created at this point. The rng-egd object
2831 * cannot be created here, as it depends on the chardev
2832 * already existing.
2834 static bool object_create_initial(const char *type)
2836 if (g_str_equal(type, "rng-egd")) {
2837 return false;
2841 * return false for concrete netfilters since
2842 * they depend on netdevs already existing
2844 if (g_str_equal(type, "filter-buffer") ||
2845 g_str_equal(type, "filter-dump") ||
2846 g_str_equal(type, "filter-mirror") ||
2847 g_str_equal(type, "filter-redirector")) {
2848 return false;
2851 return true;
2856 * The remainder of object creation happens after the
2857 * creation of chardev, fsdev, net clients and device data types.
2859 static bool object_create_delayed(const char *type)
2861 return !object_create_initial(type);
2865 static void set_memory_options(uint64_t *ram_slots, ram_addr_t *maxram_size,
2866 MachineClass *mc)
2868 uint64_t sz;
2869 const char *mem_str;
2870 const char *maxmem_str, *slots_str;
2871 const ram_addr_t default_ram_size = mc->default_ram_size;
2872 QemuOpts *opts = qemu_find_opts_singleton("memory");
2873 Location loc;
2875 loc_push_none(&loc);
2876 qemu_opts_loc_restore(opts);
2878 sz = 0;
2879 mem_str = qemu_opt_get(opts, "size");
2880 if (mem_str) {
2881 if (!*mem_str) {
2882 error_report("missing 'size' option value");
2883 exit(EXIT_FAILURE);
2886 sz = qemu_opt_get_size(opts, "size", ram_size);
2888 /* Fix up legacy suffix-less format */
2889 if (g_ascii_isdigit(mem_str[strlen(mem_str) - 1])) {
2890 uint64_t overflow_check = sz;
2892 sz <<= 20;
2893 if ((sz >> 20) != overflow_check) {
2894 error_report("too large 'size' option value");
2895 exit(EXIT_FAILURE);
2900 /* backward compatibility behaviour for case "-m 0" */
2901 if (sz == 0) {
2902 sz = default_ram_size;
2905 sz = QEMU_ALIGN_UP(sz, 8192);
2906 ram_size = sz;
2907 if (ram_size != sz) {
2908 error_report("ram size too large");
2909 exit(EXIT_FAILURE);
2912 /* store value for the future use */
2913 qemu_opt_set_number(opts, "size", ram_size, &error_abort);
2914 *maxram_size = ram_size;
2916 maxmem_str = qemu_opt_get(opts, "maxmem");
2917 slots_str = qemu_opt_get(opts, "slots");
2918 if (maxmem_str && slots_str) {
2919 uint64_t slots;
2921 sz = qemu_opt_get_size(opts, "maxmem", 0);
2922 slots = qemu_opt_get_number(opts, "slots", 0);
2923 if (sz < ram_size) {
2924 error_report("invalid value of -m option maxmem: "
2925 "maximum memory size (0x%" PRIx64 ") must be at least "
2926 "the initial memory size (0x" RAM_ADDR_FMT ")",
2927 sz, ram_size);
2928 exit(EXIT_FAILURE);
2929 } else if (sz > ram_size) {
2930 if (!slots) {
2931 error_report("invalid value of -m option: maxmem was "
2932 "specified, but no hotplug slots were specified");
2933 exit(EXIT_FAILURE);
2935 } else if (slots) {
2936 error_report("invalid value of -m option maxmem: "
2937 "memory slots were specified but maximum memory size "
2938 "(0x%" PRIx64 ") is equal to the initial memory size "
2939 "(0x" RAM_ADDR_FMT ")", sz, ram_size);
2940 exit(EXIT_FAILURE);
2943 *maxram_size = sz;
2944 *ram_slots = slots;
2945 } else if ((!maxmem_str && slots_str) ||
2946 (maxmem_str && !slots_str)) {
2947 error_report("invalid -m option value: missing "
2948 "'%s' option", slots_str ? "maxmem" : "slots");
2949 exit(EXIT_FAILURE);
2952 loc_pop(&loc);
2955 static int global_init_func(void *opaque, QemuOpts *opts, Error **errp)
2957 GlobalProperty *g;
2959 g = g_malloc0(sizeof(*g));
2960 g->driver = qemu_opt_get(opts, "driver");
2961 g->property = qemu_opt_get(opts, "property");
2962 g->value = qemu_opt_get(opts, "value");
2963 g->user_provided = true;
2964 g->errp = &error_fatal;
2965 qdev_prop_register_global(g);
2966 return 0;
2969 int main(int argc, char **argv)
2971 int i;
2972 int snapshot, linux_boot;
2973 const char *initrd_filename;
2974 const char *kernel_filename, *kernel_cmdline;
2975 const char *boot_order = NULL;
2976 const char *boot_once = NULL;
2977 DisplayState *ds;
2978 int cyls, heads, secs, translation;
2979 QemuOpts *hda_opts = NULL, *opts, *machine_opts, *icount_opts = NULL;
2980 QemuOptsList *olist;
2981 int optind;
2982 const char *optarg;
2983 const char *loadvm = NULL;
2984 MachineClass *machine_class;
2985 const char *cpu_model;
2986 const char *vga_model = NULL;
2987 const char *qtest_chrdev = NULL;
2988 const char *qtest_log = NULL;
2989 const char *pid_file = NULL;
2990 const char *incoming = NULL;
2991 bool defconfig = true;
2992 bool userconfig = true;
2993 bool nographic = false;
2994 DisplayType display_type = DT_DEFAULT;
2995 int display_remote = 0;
2996 const char *log_mask = NULL;
2997 const char *log_file = NULL;
2998 char *trace_file = NULL;
2999 ram_addr_t maxram_size;
3000 uint64_t ram_slots = 0;
3001 FILE *vmstate_dump_file = NULL;
3002 Error *main_loop_err = NULL;
3003 Error *err = NULL;
3004 bool list_data_dirs = false;
3006 qemu_init_cpu_loop();
3007 qemu_mutex_lock_iothread();
3009 atexit(qemu_run_exit_notifiers);
3010 error_set_progname(argv[0]);
3011 qemu_init_exec_dir(argv[0]);
3013 module_call_init(MODULE_INIT_QOM);
3015 qemu_add_opts(&qemu_drive_opts);
3016 qemu_add_drive_opts(&qemu_legacy_drive_opts);
3017 qemu_add_drive_opts(&qemu_common_drive_opts);
3018 qemu_add_drive_opts(&qemu_drive_opts);
3019 qemu_add_opts(&qemu_chardev_opts);
3020 qemu_add_opts(&qemu_device_opts);
3021 qemu_add_opts(&qemu_netdev_opts);
3022 qemu_add_opts(&qemu_net_opts);
3023 qemu_add_opts(&qemu_rtc_opts);
3024 qemu_add_opts(&qemu_global_opts);
3025 qemu_add_opts(&qemu_mon_opts);
3026 qemu_add_opts(&qemu_trace_opts);
3027 qemu_add_opts(&qemu_option_rom_opts);
3028 qemu_add_opts(&qemu_machine_opts);
3029 qemu_add_opts(&qemu_mem_opts);
3030 qemu_add_opts(&qemu_smp_opts);
3031 qemu_add_opts(&qemu_boot_opts);
3032 qemu_add_opts(&qemu_sandbox_opts);
3033 qemu_add_opts(&qemu_add_fd_opts);
3034 qemu_add_opts(&qemu_object_opts);
3035 qemu_add_opts(&qemu_tpmdev_opts);
3036 qemu_add_opts(&qemu_realtime_opts);
3037 qemu_add_opts(&qemu_msg_opts);
3038 qemu_add_opts(&qemu_name_opts);
3039 qemu_add_opts(&qemu_numa_opts);
3040 qemu_add_opts(&qemu_icount_opts);
3041 qemu_add_opts(&qemu_semihosting_config_opts);
3042 qemu_add_opts(&qemu_fw_cfg_opts);
3043 module_call_init(MODULE_INIT_OPTS);
3045 runstate_init();
3047 if (qcrypto_init(&err) < 0) {
3048 error_reportf_err(err, "cannot initialize crypto: ");
3049 exit(1);
3051 rtc_clock = QEMU_CLOCK_HOST;
3053 QLIST_INIT (&vm_change_state_head);
3054 os_setup_early_signal_handling();
3056 cpu_model = NULL;
3057 snapshot = 0;
3058 cyls = heads = secs = 0;
3059 translation = BIOS_ATA_TRANSLATION_AUTO;
3061 nb_nics = 0;
3063 bdrv_init_with_whitelist();
3065 autostart = 1;
3067 /* first pass of option parsing */
3068 optind = 1;
3069 while (optind < argc) {
3070 if (argv[optind][0] != '-') {
3071 /* disk image */
3072 optind++;
3073 } else {
3074 const QEMUOption *popt;
3076 popt = lookup_opt(argc, argv, &optarg, &optind);
3077 switch (popt->index) {
3078 case QEMU_OPTION_nodefconfig:
3079 defconfig = false;
3080 break;
3081 case QEMU_OPTION_nouserconfig:
3082 userconfig = false;
3083 break;
3088 if (defconfig) {
3089 int ret;
3090 ret = qemu_read_default_config_files(userconfig);
3091 if (ret < 0) {
3092 exit(1);
3096 /* second pass of option parsing */
3097 optind = 1;
3098 for(;;) {
3099 if (optind >= argc)
3100 break;
3101 if (argv[optind][0] != '-') {
3102 hda_opts = drive_add(IF_DEFAULT, 0, argv[optind++], HD_OPTS);
3103 } else {
3104 const QEMUOption *popt;
3106 popt = lookup_opt(argc, argv, &optarg, &optind);
3107 if (!(popt->arch_mask & arch_type)) {
3108 error_report("Option not supported for this target");
3109 exit(1);
3111 switch(popt->index) {
3112 case QEMU_OPTION_no_kvm_irqchip: {
3113 olist = qemu_find_opts("machine");
3114 qemu_opts_parse_noisily(olist, "kernel_irqchip=off", false);
3115 break;
3117 case QEMU_OPTION_cpu:
3118 /* hw initialization will check this */
3119 cpu_model = optarg;
3120 break;
3121 case QEMU_OPTION_hda:
3123 char buf[256];
3124 if (cyls == 0)
3125 snprintf(buf, sizeof(buf), "%s", HD_OPTS);
3126 else
3127 snprintf(buf, sizeof(buf),
3128 "%s,cyls=%d,heads=%d,secs=%d%s",
3129 HD_OPTS , cyls, heads, secs,
3130 translation == BIOS_ATA_TRANSLATION_LBA ?
3131 ",trans=lba" :
3132 translation == BIOS_ATA_TRANSLATION_NONE ?
3133 ",trans=none" : "");
3134 drive_add(IF_DEFAULT, 0, optarg, buf);
3135 break;
3137 case QEMU_OPTION_hdb:
3138 case QEMU_OPTION_hdc:
3139 case QEMU_OPTION_hdd:
3140 drive_add(IF_DEFAULT, popt->index - QEMU_OPTION_hda, optarg,
3141 HD_OPTS);
3142 break;
3143 case QEMU_OPTION_drive:
3144 if (drive_def(optarg) == NULL) {
3145 exit(1);
3147 break;
3148 case QEMU_OPTION_set:
3149 if (qemu_set_option(optarg) != 0)
3150 exit(1);
3151 break;
3152 case QEMU_OPTION_global:
3153 if (qemu_global_option(optarg) != 0)
3154 exit(1);
3155 break;
3156 case QEMU_OPTION_mtdblock:
3157 drive_add(IF_MTD, -1, optarg, MTD_OPTS);
3158 break;
3159 case QEMU_OPTION_sd:
3160 drive_add(IF_SD, -1, optarg, SD_OPTS);
3161 break;
3162 case QEMU_OPTION_pflash:
3163 drive_add(IF_PFLASH, -1, optarg, PFLASH_OPTS);
3164 break;
3165 case QEMU_OPTION_snapshot:
3166 snapshot = 1;
3167 break;
3168 case QEMU_OPTION_hdachs:
3170 const char *p;
3171 p = optarg;
3172 cyls = strtol(p, (char **)&p, 0);
3173 if (cyls < 1 || cyls > 16383)
3174 goto chs_fail;
3175 if (*p != ',')
3176 goto chs_fail;
3177 p++;
3178 heads = strtol(p, (char **)&p, 0);
3179 if (heads < 1 || heads > 16)
3180 goto chs_fail;
3181 if (*p != ',')
3182 goto chs_fail;
3183 p++;
3184 secs = strtol(p, (char **)&p, 0);
3185 if (secs < 1 || secs > 63)
3186 goto chs_fail;
3187 if (*p == ',') {
3188 p++;
3189 if (!strcmp(p, "large")) {
3190 translation = BIOS_ATA_TRANSLATION_LARGE;
3191 } else if (!strcmp(p, "rechs")) {
3192 translation = BIOS_ATA_TRANSLATION_RECHS;
3193 } else if (!strcmp(p, "none")) {
3194 translation = BIOS_ATA_TRANSLATION_NONE;
3195 } else if (!strcmp(p, "lba")) {
3196 translation = BIOS_ATA_TRANSLATION_LBA;
3197 } else if (!strcmp(p, "auto")) {
3198 translation = BIOS_ATA_TRANSLATION_AUTO;
3199 } else {
3200 goto chs_fail;
3202 } else if (*p != '\0') {
3203 chs_fail:
3204 error_report("invalid physical CHS format");
3205 exit(1);
3207 if (hda_opts != NULL) {
3208 qemu_opt_set_number(hda_opts, "cyls", cyls,
3209 &error_abort);
3210 qemu_opt_set_number(hda_opts, "heads", heads,
3211 &error_abort);
3212 qemu_opt_set_number(hda_opts, "secs", secs,
3213 &error_abort);
3214 if (translation == BIOS_ATA_TRANSLATION_LARGE) {
3215 qemu_opt_set(hda_opts, "trans", "large",
3216 &error_abort);
3217 } else if (translation == BIOS_ATA_TRANSLATION_RECHS) {
3218 qemu_opt_set(hda_opts, "trans", "rechs",
3219 &error_abort);
3220 } else if (translation == BIOS_ATA_TRANSLATION_LBA) {
3221 qemu_opt_set(hda_opts, "trans", "lba",
3222 &error_abort);
3223 } else if (translation == BIOS_ATA_TRANSLATION_NONE) {
3224 qemu_opt_set(hda_opts, "trans", "none",
3225 &error_abort);
3229 break;
3230 case QEMU_OPTION_numa:
3231 opts = qemu_opts_parse_noisily(qemu_find_opts("numa"),
3232 optarg, true);
3233 if (!opts) {
3234 exit(1);
3236 break;
3237 case QEMU_OPTION_display:
3238 display_type = select_display(optarg);
3239 break;
3240 case QEMU_OPTION_nographic:
3241 olist = qemu_find_opts("machine");
3242 qemu_opts_parse_noisily(olist, "graphics=off", false);
3243 nographic = true;
3244 display_type = DT_NONE;
3245 break;
3246 case QEMU_OPTION_curses:
3247 #ifdef CONFIG_CURSES
3248 display_type = DT_CURSES;
3249 #else
3250 error_report("curses support is disabled");
3251 exit(1);
3252 #endif
3253 break;
3254 case QEMU_OPTION_portrait:
3255 graphic_rotate = 90;
3256 break;
3257 case QEMU_OPTION_rotate:
3258 graphic_rotate = strtol(optarg, (char **) &optarg, 10);
3259 if (graphic_rotate != 0 && graphic_rotate != 90 &&
3260 graphic_rotate != 180 && graphic_rotate != 270) {
3261 error_report("only 90, 180, 270 deg rotation is available");
3262 exit(1);
3264 break;
3265 case QEMU_OPTION_kernel:
3266 qemu_opts_set(qemu_find_opts("machine"), 0, "kernel", optarg,
3267 &error_abort);
3268 break;
3269 case QEMU_OPTION_initrd:
3270 qemu_opts_set(qemu_find_opts("machine"), 0, "initrd", optarg,
3271 &error_abort);
3272 break;
3273 case QEMU_OPTION_append:
3274 qemu_opts_set(qemu_find_opts("machine"), 0, "append", optarg,
3275 &error_abort);
3276 break;
3277 case QEMU_OPTION_dtb:
3278 qemu_opts_set(qemu_find_opts("machine"), 0, "dtb", optarg,
3279 &error_abort);
3280 break;
3281 case QEMU_OPTION_cdrom:
3282 drive_add(IF_DEFAULT, 2, optarg, CDROM_OPTS);
3283 break;
3284 case QEMU_OPTION_boot:
3285 opts = qemu_opts_parse_noisily(qemu_find_opts("boot-opts"),
3286 optarg, true);
3287 if (!opts) {
3288 exit(1);
3290 break;
3291 case QEMU_OPTION_fda:
3292 case QEMU_OPTION_fdb:
3293 drive_add(IF_FLOPPY, popt->index - QEMU_OPTION_fda,
3294 optarg, FD_OPTS);
3295 break;
3296 case QEMU_OPTION_no_fd_bootchk:
3297 fd_bootchk = 0;
3298 break;
3299 case QEMU_OPTION_netdev:
3300 default_net = 0;
3301 if (net_client_parse(qemu_find_opts("netdev"), optarg) == -1) {
3302 exit(1);
3304 break;
3305 case QEMU_OPTION_net:
3306 default_net = 0;
3307 if (net_client_parse(qemu_find_opts("net"), optarg) == -1) {
3308 exit(1);
3310 break;
3311 #ifdef CONFIG_LIBISCSI
3312 case QEMU_OPTION_iscsi:
3313 opts = qemu_opts_parse_noisily(qemu_find_opts("iscsi"),
3314 optarg, false);
3315 if (!opts) {
3316 exit(1);
3318 break;
3319 #endif
3320 #ifdef CONFIG_SLIRP
3321 case QEMU_OPTION_tftp:
3322 error_report("The -tftp option is deprecated. "
3323 "Please use '-netdev user,tftp=...' instead.");
3324 legacy_tftp_prefix = optarg;
3325 break;
3326 case QEMU_OPTION_bootp:
3327 error_report("The -bootp option is deprecated. "
3328 "Please use '-netdev user,bootfile=...' instead.");
3329 legacy_bootp_filename = optarg;
3330 break;
3331 case QEMU_OPTION_redir:
3332 error_report("The -redir option is deprecated. "
3333 "Please use '-netdev user,hostfwd=...' instead.");
3334 if (net_slirp_redir(optarg) < 0)
3335 exit(1);
3336 break;
3337 #endif
3338 case QEMU_OPTION_bt:
3339 add_device_config(DEV_BT, optarg);
3340 break;
3341 case QEMU_OPTION_audio_help:
3342 AUD_help ();
3343 exit (0);
3344 break;
3345 case QEMU_OPTION_soundhw:
3346 select_soundhw (optarg);
3347 break;
3348 case QEMU_OPTION_h:
3349 help(0);
3350 break;
3351 case QEMU_OPTION_version:
3352 version();
3353 exit(0);
3354 break;
3355 case QEMU_OPTION_m:
3356 opts = qemu_opts_parse_noisily(qemu_find_opts("memory"),
3357 optarg, true);
3358 if (!opts) {
3359 exit(EXIT_FAILURE);
3361 break;
3362 #ifdef CONFIG_TPM
3363 case QEMU_OPTION_tpmdev:
3364 if (tpm_config_parse(qemu_find_opts("tpmdev"), optarg) < 0) {
3365 exit(1);
3367 break;
3368 #endif
3369 case QEMU_OPTION_mempath:
3370 mem_path = optarg;
3371 break;
3372 case QEMU_OPTION_mem_prealloc:
3373 mem_prealloc = 1;
3374 break;
3375 case QEMU_OPTION_d:
3376 log_mask = optarg;
3377 break;
3378 case QEMU_OPTION_D:
3379 log_file = optarg;
3380 break;
3381 case QEMU_OPTION_DFILTER:
3382 qemu_set_dfilter_ranges(optarg, &error_fatal);
3383 break;
3384 case QEMU_OPTION_s:
3385 add_device_config(DEV_GDB, "tcp::" DEFAULT_GDBSTUB_PORT);
3386 break;
3387 case QEMU_OPTION_gdb:
3388 add_device_config(DEV_GDB, optarg);
3389 break;
3390 case QEMU_OPTION_L:
3391 if (is_help_option(optarg)) {
3392 list_data_dirs = true;
3393 } else if (data_dir_idx < ARRAY_SIZE(data_dir)) {
3394 data_dir[data_dir_idx++] = optarg;
3396 break;
3397 case QEMU_OPTION_bios:
3398 qemu_opts_set(qemu_find_opts("machine"), 0, "firmware", optarg,
3399 &error_abort);
3400 break;
3401 case QEMU_OPTION_singlestep:
3402 singlestep = 1;
3403 break;
3404 case QEMU_OPTION_S:
3405 autostart = 0;
3406 break;
3407 case QEMU_OPTION_k:
3408 keyboard_layout = optarg;
3409 break;
3410 case QEMU_OPTION_localtime:
3411 rtc_utc = 0;
3412 break;
3413 case QEMU_OPTION_vga:
3414 vga_model = optarg;
3415 default_vga = 0;
3416 break;
3417 case QEMU_OPTION_g:
3419 const char *p;
3420 int w, h, depth;
3421 p = optarg;
3422 w = strtol(p, (char **)&p, 10);
3423 if (w <= 0) {
3424 graphic_error:
3425 error_report("invalid resolution or depth");
3426 exit(1);
3428 if (*p != 'x')
3429 goto graphic_error;
3430 p++;
3431 h = strtol(p, (char **)&p, 10);
3432 if (h <= 0)
3433 goto graphic_error;
3434 if (*p == 'x') {
3435 p++;
3436 depth = strtol(p, (char **)&p, 10);
3437 if (depth != 8 && depth != 15 && depth != 16 &&
3438 depth != 24 && depth != 32)
3439 goto graphic_error;
3440 } else if (*p == '\0') {
3441 depth = graphic_depth;
3442 } else {
3443 goto graphic_error;
3446 graphic_width = w;
3447 graphic_height = h;
3448 graphic_depth = depth;
3450 break;
3451 case QEMU_OPTION_echr:
3453 char *r;
3454 term_escape_char = strtol(optarg, &r, 0);
3455 if (r == optarg)
3456 printf("Bad argument to echr\n");
3457 break;
3459 case QEMU_OPTION_monitor:
3460 default_monitor = 0;
3461 if (strncmp(optarg, "none", 4)) {
3462 monitor_parse(optarg, "readline", false);
3464 break;
3465 case QEMU_OPTION_qmp:
3466 monitor_parse(optarg, "control", false);
3467 default_monitor = 0;
3468 break;
3469 case QEMU_OPTION_qmp_pretty:
3470 monitor_parse(optarg, "control", true);
3471 default_monitor = 0;
3472 break;
3473 case QEMU_OPTION_mon:
3474 opts = qemu_opts_parse_noisily(qemu_find_opts("mon"), optarg,
3475 true);
3476 if (!opts) {
3477 exit(1);
3479 default_monitor = 0;
3480 break;
3481 case QEMU_OPTION_chardev:
3482 opts = qemu_opts_parse_noisily(qemu_find_opts("chardev"),
3483 optarg, true);
3484 if (!opts) {
3485 exit(1);
3487 break;
3488 case QEMU_OPTION_fsdev:
3489 olist = qemu_find_opts("fsdev");
3490 if (!olist) {
3491 error_report("fsdev support is disabled");
3492 exit(1);
3494 opts = qemu_opts_parse_noisily(olist, optarg, true);
3495 if (!opts) {
3496 exit(1);
3498 break;
3499 case QEMU_OPTION_virtfs: {
3500 QemuOpts *fsdev;
3501 QemuOpts *device;
3502 const char *writeout, *sock_fd, *socket;
3504 olist = qemu_find_opts("virtfs");
3505 if (!olist) {
3506 error_report("virtfs support is disabled");
3507 exit(1);
3509 opts = qemu_opts_parse_noisily(olist, optarg, true);
3510 if (!opts) {
3511 exit(1);
3514 if (qemu_opt_get(opts, "fsdriver") == NULL ||
3515 qemu_opt_get(opts, "mount_tag") == NULL) {
3516 error_report("Usage: -virtfs fsdriver,mount_tag=tag");
3517 exit(1);
3519 fsdev = qemu_opts_create(qemu_find_opts("fsdev"),
3520 qemu_opt_get(opts, "mount_tag"),
3521 1, NULL);
3522 if (!fsdev) {
3523 error_report("duplicate fsdev id: %s",
3524 qemu_opt_get(opts, "mount_tag"));
3525 exit(1);
3528 writeout = qemu_opt_get(opts, "writeout");
3529 if (writeout) {
3530 #ifdef CONFIG_SYNC_FILE_RANGE
3531 qemu_opt_set(fsdev, "writeout", writeout, &error_abort);
3532 #else
3533 error_report("writeout=immediate not supported "
3534 "on this platform");
3535 exit(1);
3536 #endif
3538 qemu_opt_set(fsdev, "fsdriver",
3539 qemu_opt_get(opts, "fsdriver"), &error_abort);
3540 qemu_opt_set(fsdev, "path", qemu_opt_get(opts, "path"),
3541 &error_abort);
3542 qemu_opt_set(fsdev, "security_model",
3543 qemu_opt_get(opts, "security_model"),
3544 &error_abort);
3545 socket = qemu_opt_get(opts, "socket");
3546 if (socket) {
3547 qemu_opt_set(fsdev, "socket", socket, &error_abort);
3549 sock_fd = qemu_opt_get(opts, "sock_fd");
3550 if (sock_fd) {
3551 qemu_opt_set(fsdev, "sock_fd", sock_fd, &error_abort);
3554 qemu_opt_set_bool(fsdev, "readonly",
3555 qemu_opt_get_bool(opts, "readonly", 0),
3556 &error_abort);
3557 device = qemu_opts_create(qemu_find_opts("device"), NULL, 0,
3558 &error_abort);
3559 qemu_opt_set(device, "driver", "virtio-9p-pci", &error_abort);
3560 qemu_opt_set(device, "fsdev",
3561 qemu_opt_get(opts, "mount_tag"), &error_abort);
3562 qemu_opt_set(device, "mount_tag",
3563 qemu_opt_get(opts, "mount_tag"), &error_abort);
3564 break;
3566 case QEMU_OPTION_virtfs_synth: {
3567 QemuOpts *fsdev;
3568 QemuOpts *device;
3570 fsdev = qemu_opts_create(qemu_find_opts("fsdev"), "v_synth",
3571 1, NULL);
3572 if (!fsdev) {
3573 error_report("duplicate option: %s", "virtfs_synth");
3574 exit(1);
3576 qemu_opt_set(fsdev, "fsdriver", "synth", &error_abort);
3578 device = qemu_opts_create(qemu_find_opts("device"), NULL, 0,
3579 &error_abort);
3580 qemu_opt_set(device, "driver", "virtio-9p-pci", &error_abort);
3581 qemu_opt_set(device, "fsdev", "v_synth", &error_abort);
3582 qemu_opt_set(device, "mount_tag", "v_synth", &error_abort);
3583 break;
3585 case QEMU_OPTION_serial:
3586 add_device_config(DEV_SERIAL, optarg);
3587 default_serial = 0;
3588 if (strncmp(optarg, "mon:", 4) == 0) {
3589 default_monitor = 0;
3591 break;
3592 case QEMU_OPTION_watchdog:
3593 if (watchdog) {
3594 error_report("only one watchdog option may be given");
3595 return 1;
3597 watchdog = optarg;
3598 break;
3599 case QEMU_OPTION_watchdog_action:
3600 if (select_watchdog_action(optarg) == -1) {
3601 error_report("unknown -watchdog-action parameter");
3602 exit(1);
3604 break;
3605 case QEMU_OPTION_virtiocon:
3606 add_device_config(DEV_VIRTCON, optarg);
3607 default_virtcon = 0;
3608 if (strncmp(optarg, "mon:", 4) == 0) {
3609 default_monitor = 0;
3611 break;
3612 case QEMU_OPTION_parallel:
3613 add_device_config(DEV_PARALLEL, optarg);
3614 default_parallel = 0;
3615 if (strncmp(optarg, "mon:", 4) == 0) {
3616 default_monitor = 0;
3618 break;
3619 case QEMU_OPTION_debugcon:
3620 add_device_config(DEV_DEBUGCON, optarg);
3621 break;
3622 case QEMU_OPTION_loadvm:
3623 loadvm = optarg;
3624 break;
3625 case QEMU_OPTION_full_screen:
3626 full_screen = 1;
3627 break;
3628 case QEMU_OPTION_no_frame:
3629 no_frame = 1;
3630 break;
3631 case QEMU_OPTION_alt_grab:
3632 alt_grab = 1;
3633 break;
3634 case QEMU_OPTION_ctrl_grab:
3635 ctrl_grab = 1;
3636 break;
3637 case QEMU_OPTION_no_quit:
3638 no_quit = 1;
3639 break;
3640 case QEMU_OPTION_sdl:
3641 #ifdef CONFIG_SDL
3642 display_type = DT_SDL;
3643 break;
3644 #else
3645 error_report("SDL support is disabled");
3646 exit(1);
3647 #endif
3648 case QEMU_OPTION_pidfile:
3649 pid_file = optarg;
3650 break;
3651 case QEMU_OPTION_win2k_hack:
3652 win2k_install_hack = 1;
3653 break;
3654 case QEMU_OPTION_rtc_td_hack: {
3655 static GlobalProperty slew_lost_ticks = {
3656 .driver = "mc146818rtc",
3657 .property = "lost_tick_policy",
3658 .value = "slew",
3661 qdev_prop_register_global(&slew_lost_ticks);
3662 break;
3664 case QEMU_OPTION_acpitable:
3665 opts = qemu_opts_parse_noisily(qemu_find_opts("acpi"),
3666 optarg, true);
3667 if (!opts) {
3668 exit(1);
3670 do_acpitable_option(opts);
3671 break;
3672 case QEMU_OPTION_smbios:
3673 opts = qemu_opts_parse_noisily(qemu_find_opts("smbios"),
3674 optarg, false);
3675 if (!opts) {
3676 exit(1);
3678 do_smbios_option(opts);
3679 break;
3680 case QEMU_OPTION_fwcfg:
3681 opts = qemu_opts_parse_noisily(qemu_find_opts("fw_cfg"),
3682 optarg, true);
3683 if (opts == NULL) {
3684 exit(1);
3686 break;
3687 case QEMU_OPTION_enable_kvm:
3688 olist = qemu_find_opts("machine");
3689 qemu_opts_parse_noisily(olist, "accel=kvm", false);
3690 break;
3691 case QEMU_OPTION_M:
3692 case QEMU_OPTION_machine:
3693 olist = qemu_find_opts("machine");
3694 opts = qemu_opts_parse_noisily(olist, optarg, true);
3695 if (!opts) {
3696 exit(1);
3698 break;
3699 case QEMU_OPTION_no_kvm:
3700 olist = qemu_find_opts("machine");
3701 qemu_opts_parse_noisily(olist, "accel=tcg", false);
3702 break;
3703 case QEMU_OPTION_no_kvm_pit: {
3704 error_report("warning: ignoring deprecated option");
3705 break;
3707 case QEMU_OPTION_no_kvm_pit_reinjection: {
3708 static GlobalProperty kvm_pit_lost_tick_policy = {
3709 .driver = "kvm-pit",
3710 .property = "lost_tick_policy",
3711 .value = "discard",
3714 error_report("warning: deprecated, replaced by "
3715 "-global kvm-pit.lost_tick_policy=discard");
3716 qdev_prop_register_global(&kvm_pit_lost_tick_policy);
3717 break;
3719 case QEMU_OPTION_usb:
3720 olist = qemu_find_opts("machine");
3721 qemu_opts_parse_noisily(olist, "usb=on", false);
3722 break;
3723 case QEMU_OPTION_usbdevice:
3724 olist = qemu_find_opts("machine");
3725 qemu_opts_parse_noisily(olist, "usb=on", false);
3726 add_device_config(DEV_USB, optarg);
3727 break;
3728 case QEMU_OPTION_device:
3729 if (!qemu_opts_parse_noisily(qemu_find_opts("device"),
3730 optarg, true)) {
3731 exit(1);
3733 break;
3734 case QEMU_OPTION_smp:
3735 if (!qemu_opts_parse_noisily(qemu_find_opts("smp-opts"),
3736 optarg, true)) {
3737 exit(1);
3739 break;
3740 case QEMU_OPTION_vnc:
3741 vnc_parse(optarg, &error_fatal);
3742 break;
3743 case QEMU_OPTION_no_acpi:
3744 acpi_enabled = 0;
3745 break;
3746 case QEMU_OPTION_no_hpet:
3747 no_hpet = 1;
3748 break;
3749 case QEMU_OPTION_balloon:
3750 if (balloon_parse(optarg) < 0) {
3751 error_report("unknown -balloon argument %s", optarg);
3752 exit(1);
3754 break;
3755 case QEMU_OPTION_no_reboot:
3756 no_reboot = 1;
3757 break;
3758 case QEMU_OPTION_no_shutdown:
3759 no_shutdown = 1;
3760 break;
3761 case QEMU_OPTION_show_cursor:
3762 cursor_hide = 0;
3763 break;
3764 case QEMU_OPTION_uuid:
3765 if(qemu_uuid_parse(optarg, qemu_uuid) < 0) {
3766 error_report("failed to parse UUID string: wrong format");
3767 exit(1);
3769 qemu_uuid_set = true;
3770 break;
3771 case QEMU_OPTION_option_rom:
3772 if (nb_option_roms >= MAX_OPTION_ROMS) {
3773 error_report("too many option ROMs");
3774 exit(1);
3776 opts = qemu_opts_parse_noisily(qemu_find_opts("option-rom"),
3777 optarg, true);
3778 if (!opts) {
3779 exit(1);
3781 option_rom[nb_option_roms].name = qemu_opt_get(opts, "romfile");
3782 option_rom[nb_option_roms].bootindex =
3783 qemu_opt_get_number(opts, "bootindex", -1);
3784 if (!option_rom[nb_option_roms].name) {
3785 error_report("Option ROM file is not specified");
3786 exit(1);
3788 nb_option_roms++;
3789 break;
3790 case QEMU_OPTION_semihosting:
3791 semihosting.enabled = true;
3792 semihosting.target = SEMIHOSTING_TARGET_AUTO;
3793 break;
3794 case QEMU_OPTION_semihosting_config:
3795 semihosting.enabled = true;
3796 opts = qemu_opts_parse_noisily(qemu_find_opts("semihosting-config"),
3797 optarg, false);
3798 if (opts != NULL) {
3799 semihosting.enabled = qemu_opt_get_bool(opts, "enable",
3800 true);
3801 const char *target = qemu_opt_get(opts, "target");
3802 if (target != NULL) {
3803 if (strcmp("native", target) == 0) {
3804 semihosting.target = SEMIHOSTING_TARGET_NATIVE;
3805 } else if (strcmp("gdb", target) == 0) {
3806 semihosting.target = SEMIHOSTING_TARGET_GDB;
3807 } else if (strcmp("auto", target) == 0) {
3808 semihosting.target = SEMIHOSTING_TARGET_AUTO;
3809 } else {
3810 error_report("unsupported semihosting-config %s",
3811 optarg);
3812 exit(1);
3814 } else {
3815 semihosting.target = SEMIHOSTING_TARGET_AUTO;
3817 /* Set semihosting argument count and vector */
3818 qemu_opt_foreach(opts, add_semihosting_arg,
3819 &semihosting, NULL);
3820 } else {
3821 error_report("unsupported semihosting-config %s", optarg);
3822 exit(1);
3824 break;
3825 case QEMU_OPTION_tdf:
3826 error_report("warning: ignoring deprecated option");
3827 break;
3828 case QEMU_OPTION_name:
3829 opts = qemu_opts_parse_noisily(qemu_find_opts("name"),
3830 optarg, true);
3831 if (!opts) {
3832 exit(1);
3834 break;
3835 case QEMU_OPTION_prom_env:
3836 if (nb_prom_envs >= MAX_PROM_ENVS) {
3837 error_report("too many prom variables");
3838 exit(1);
3840 prom_envs[nb_prom_envs] = optarg;
3841 nb_prom_envs++;
3842 break;
3843 case QEMU_OPTION_old_param:
3844 old_param = 1;
3845 break;
3846 case QEMU_OPTION_clock:
3847 /* Clock options no longer exist. Keep this option for
3848 * backward compatibility.
3850 break;
3851 case QEMU_OPTION_startdate:
3852 configure_rtc_date_offset(optarg, 1);
3853 break;
3854 case QEMU_OPTION_rtc:
3855 opts = qemu_opts_parse_noisily(qemu_find_opts("rtc"), optarg,
3856 false);
3857 if (!opts) {
3858 exit(1);
3860 configure_rtc(opts);
3861 break;
3862 case QEMU_OPTION_tb_size:
3863 tcg_tb_size = strtol(optarg, NULL, 0);
3864 if (tcg_tb_size < 0) {
3865 tcg_tb_size = 0;
3867 break;
3868 case QEMU_OPTION_icount:
3869 icount_opts = qemu_opts_parse_noisily(qemu_find_opts("icount"),
3870 optarg, true);
3871 if (!icount_opts) {
3872 exit(1);
3874 break;
3875 case QEMU_OPTION_incoming:
3876 if (!incoming) {
3877 runstate_set(RUN_STATE_INMIGRATE);
3879 incoming = optarg;
3880 break;
3881 case QEMU_OPTION_nodefaults:
3882 has_defaults = 0;
3883 break;
3884 case QEMU_OPTION_xen_domid:
3885 if (!(xen_available())) {
3886 error_report("Option not supported for this target");
3887 exit(1);
3889 xen_domid = atoi(optarg);
3890 break;
3891 case QEMU_OPTION_xen_create:
3892 if (!(xen_available())) {
3893 error_report("Option not supported for this target");
3894 exit(1);
3896 xen_mode = XEN_CREATE;
3897 break;
3898 case QEMU_OPTION_xen_attach:
3899 if (!(xen_available())) {
3900 error_report("Option not supported for this target");
3901 exit(1);
3903 xen_mode = XEN_ATTACH;
3904 break;
3905 case QEMU_OPTION_trace:
3906 g_free(trace_file);
3907 trace_file = trace_opt_parse(optarg);
3908 break;
3909 case QEMU_OPTION_trace_unassigned:
3910 trace_unassigned = true;
3911 break;
3912 case QEMU_OPTION_readconfig:
3914 int ret = qemu_read_config_file(optarg);
3915 if (ret < 0) {
3916 error_report("read config %s: %s", optarg,
3917 strerror(-ret));
3918 exit(1);
3920 break;
3922 case QEMU_OPTION_spice:
3923 olist = qemu_find_opts("spice");
3924 if (!olist) {
3925 error_report("spice support is disabled");
3926 exit(1);
3928 opts = qemu_opts_parse_noisily(olist, optarg, false);
3929 if (!opts) {
3930 exit(1);
3932 display_remote++;
3933 break;
3934 case QEMU_OPTION_writeconfig:
3936 FILE *fp;
3937 if (strcmp(optarg, "-") == 0) {
3938 fp = stdout;
3939 } else {
3940 fp = fopen(optarg, "w");
3941 if (fp == NULL) {
3942 error_report("open %s: %s", optarg,
3943 strerror(errno));
3944 exit(1);
3947 qemu_config_write(fp);
3948 if (fp != stdout) {
3949 fclose(fp);
3951 break;
3953 case QEMU_OPTION_qtest:
3954 qtest_chrdev = optarg;
3955 break;
3956 case QEMU_OPTION_qtest_log:
3957 qtest_log = optarg;
3958 break;
3959 case QEMU_OPTION_sandbox:
3960 opts = qemu_opts_parse_noisily(qemu_find_opts("sandbox"),
3961 optarg, true);
3962 if (!opts) {
3963 exit(1);
3965 break;
3966 case QEMU_OPTION_add_fd:
3967 #ifndef _WIN32
3968 opts = qemu_opts_parse_noisily(qemu_find_opts("add-fd"),
3969 optarg, false);
3970 if (!opts) {
3971 exit(1);
3973 #else
3974 error_report("File descriptor passing is disabled on this "
3975 "platform");
3976 exit(1);
3977 #endif
3978 break;
3979 case QEMU_OPTION_object:
3980 opts = qemu_opts_parse_noisily(qemu_find_opts("object"),
3981 optarg, true);
3982 if (!opts) {
3983 exit(1);
3985 break;
3986 case QEMU_OPTION_realtime:
3987 opts = qemu_opts_parse_noisily(qemu_find_opts("realtime"),
3988 optarg, false);
3989 if (!opts) {
3990 exit(1);
3992 enable_mlock = qemu_opt_get_bool(opts, "mlock", true);
3993 break;
3994 case QEMU_OPTION_msg:
3995 opts = qemu_opts_parse_noisily(qemu_find_opts("msg"), optarg,
3996 false);
3997 if (!opts) {
3998 exit(1);
4000 configure_msg(opts);
4001 break;
4002 case QEMU_OPTION_dump_vmstate:
4003 if (vmstate_dump_file) {
4004 error_report("only one '-dump-vmstate' "
4005 "option may be given");
4006 exit(1);
4008 vmstate_dump_file = fopen(optarg, "w");
4009 if (vmstate_dump_file == NULL) {
4010 error_report("open %s: %s", optarg, strerror(errno));
4011 exit(1);
4013 break;
4014 default:
4015 os_parse_cmd_args(popt->index, optarg);
4020 * Clear error location left behind by the loop.
4021 * Best done right after the loop. Do not insert code here!
4023 loc_set_none();
4025 replay_configure(icount_opts);
4027 machine_class = select_machine();
4029 set_memory_options(&ram_slots, &maxram_size, machine_class);
4031 os_daemonize();
4033 if (qemu_init_main_loop(&main_loop_err)) {
4034 error_report_err(main_loop_err);
4035 exit(1);
4038 if (qemu_opts_foreach(qemu_find_opts("sandbox"),
4039 parse_sandbox, NULL, NULL)) {
4040 exit(1);
4043 if (qemu_opts_foreach(qemu_find_opts("name"),
4044 parse_name, NULL, NULL)) {
4045 exit(1);
4048 #ifndef _WIN32
4049 if (qemu_opts_foreach(qemu_find_opts("add-fd"),
4050 parse_add_fd, NULL, NULL)) {
4051 exit(1);
4054 if (qemu_opts_foreach(qemu_find_opts("add-fd"),
4055 cleanup_add_fd, NULL, NULL)) {
4056 exit(1);
4058 #endif
4060 current_machine = MACHINE(object_new(object_class_get_name(
4061 OBJECT_CLASS(machine_class))));
4062 if (machine_help_func(qemu_get_machine_opts(), current_machine)) {
4063 exit(0);
4065 object_property_add_child(object_get_root(), "machine",
4066 OBJECT(current_machine), &error_abort);
4067 cpu_exec_init_all();
4069 if (machine_class->hw_version) {
4070 qemu_set_hw_version(machine_class->hw_version);
4073 if (cpu_model && is_help_option(cpu_model)) {
4074 list_cpus(stdout, &fprintf, cpu_model);
4075 exit(0);
4078 if (!trace_init_backends()) {
4079 exit(1);
4081 trace_init_file(trace_file);
4083 /* Open the logfile at this point and set the log mask if necessary.
4085 if (log_file) {
4086 qemu_set_log_filename(log_file, &error_fatal);
4089 if (log_mask) {
4090 int mask;
4091 mask = qemu_str_to_log_mask(log_mask);
4092 if (!mask) {
4093 qemu_print_log_usage(stdout);
4094 exit(1);
4096 qemu_set_log(mask);
4097 } else {
4098 qemu_set_log(0);
4101 /* If no data_dir is specified then try to find it relative to the
4102 executable path. */
4103 if (data_dir_idx < ARRAY_SIZE(data_dir)) {
4104 data_dir[data_dir_idx] = os_find_datadir();
4105 if (data_dir[data_dir_idx] != NULL) {
4106 data_dir_idx++;
4109 /* If all else fails use the install path specified when building. */
4110 if (data_dir_idx < ARRAY_SIZE(data_dir)) {
4111 data_dir[data_dir_idx++] = CONFIG_QEMU_DATADIR;
4114 /* -L help lists the data directories and exits. */
4115 if (list_data_dirs) {
4116 for (i = 0; i < data_dir_idx; i++) {
4117 printf("%s\n", data_dir[i]);
4119 exit(0);
4122 smp_parse(qemu_opts_find(qemu_find_opts("smp-opts"), NULL));
4124 machine_class->max_cpus = machine_class->max_cpus ?: 1; /* Default to UP */
4125 if (max_cpus > machine_class->max_cpus) {
4126 error_report("Number of SMP CPUs requested (%d) exceeds max CPUs "
4127 "supported by machine '%s' (%d)", max_cpus,
4128 machine_class->name, machine_class->max_cpus);
4129 exit(1);
4133 * Get the default machine options from the machine if it is not already
4134 * specified either by the configuration file or by the command line.
4136 if (machine_class->default_machine_opts) {
4137 qemu_opts_set_defaults(qemu_find_opts("machine"),
4138 machine_class->default_machine_opts, 0);
4141 qemu_opts_foreach(qemu_find_opts("device"),
4142 default_driver_check, NULL, NULL);
4143 qemu_opts_foreach(qemu_find_opts("global"),
4144 default_driver_check, NULL, NULL);
4146 if (!vga_model && !default_vga) {
4147 vga_interface_type = VGA_DEVICE;
4149 if (!has_defaults || machine_class->no_serial) {
4150 default_serial = 0;
4152 if (!has_defaults || machine_class->no_parallel) {
4153 default_parallel = 0;
4155 if (!has_defaults || !machine_class->use_virtcon) {
4156 default_virtcon = 0;
4158 if (!has_defaults || !machine_class->use_sclp) {
4159 default_sclp = 0;
4161 if (!has_defaults || machine_class->no_floppy) {
4162 default_floppy = 0;
4164 if (!has_defaults || machine_class->no_cdrom) {
4165 default_cdrom = 0;
4167 if (!has_defaults || machine_class->no_sdcard) {
4168 default_sdcard = 0;
4170 if (!has_defaults) {
4171 default_monitor = 0;
4172 default_net = 0;
4173 default_vga = 0;
4176 if (is_daemonized()) {
4177 /* According to documentation and historically, -nographic redirects
4178 * serial port, parallel port and monitor to stdio, which does not work
4179 * with -daemonize. We can redirect these to null instead, but since
4180 * -nographic is legacy, let's just error out.
4181 * We disallow -nographic only if all other ports are not redirected
4182 * explicitly, to not break existing legacy setups which uses
4183 * -nographic _and_ redirects all ports explicitly - this is valid
4184 * usage, -nographic is just a no-op in this case.
4186 if (nographic
4187 && (default_parallel || default_serial
4188 || default_monitor || default_virtcon)) {
4189 error_report("-nographic cannot be used with -daemonize");
4190 exit(1);
4192 #ifdef CONFIG_CURSES
4193 if (display_type == DT_CURSES) {
4194 error_report("curses display cannot be used with -daemonize");
4195 exit(1);
4197 #endif
4200 if (nographic) {
4201 if (default_parallel)
4202 add_device_config(DEV_PARALLEL, "null");
4203 if (default_serial && default_monitor) {
4204 add_device_config(DEV_SERIAL, "mon:stdio");
4205 } else if (default_virtcon && default_monitor) {
4206 add_device_config(DEV_VIRTCON, "mon:stdio");
4207 } else if (default_sclp && default_monitor) {
4208 add_device_config(DEV_SCLP, "mon:stdio");
4209 } else {
4210 if (default_serial)
4211 add_device_config(DEV_SERIAL, "stdio");
4212 if (default_virtcon)
4213 add_device_config(DEV_VIRTCON, "stdio");
4214 if (default_sclp) {
4215 add_device_config(DEV_SCLP, "stdio");
4217 if (default_monitor)
4218 monitor_parse("stdio", "readline", false);
4220 } else {
4221 if (default_serial)
4222 add_device_config(DEV_SERIAL, "vc:80Cx24C");
4223 if (default_parallel)
4224 add_device_config(DEV_PARALLEL, "vc:80Cx24C");
4225 if (default_monitor)
4226 monitor_parse("vc:80Cx24C", "readline", false);
4227 if (default_virtcon)
4228 add_device_config(DEV_VIRTCON, "vc:80Cx24C");
4229 if (default_sclp) {
4230 add_device_config(DEV_SCLP, "vc:80Cx24C");
4234 #if defined(CONFIG_VNC)
4235 if (!QTAILQ_EMPTY(&(qemu_find_opts("vnc")->head))) {
4236 display_remote++;
4238 #endif
4239 if (display_type == DT_DEFAULT && !display_remote) {
4240 #if defined(CONFIG_GTK)
4241 display_type = DT_GTK;
4242 #elif defined(CONFIG_SDL)
4243 display_type = DT_SDL;
4244 #elif defined(CONFIG_COCOA)
4245 display_type = DT_COCOA;
4246 #elif defined(CONFIG_VNC)
4247 vnc_parse("localhost:0,to=99,id=default", &error_abort);
4248 #else
4249 display_type = DT_NONE;
4250 #endif
4253 if ((no_frame || alt_grab || ctrl_grab) && display_type != DT_SDL) {
4254 error_report("-no-frame, -alt-grab and -ctrl-grab are only valid "
4255 "for SDL, ignoring option");
4257 if (no_quit && (display_type != DT_GTK && display_type != DT_SDL)) {
4258 error_report("-no-quit is only valid for GTK and SDL, "
4259 "ignoring option");
4262 if (display_type == DT_GTK) {
4263 early_gtk_display_init(request_opengl);
4266 if (display_type == DT_SDL) {
4267 sdl_display_early_init(request_opengl);
4270 if (request_opengl == 1 && display_opengl == 0) {
4271 #if defined(CONFIG_OPENGL)
4272 error_report("OpenGL is not supported by the display");
4273 #else
4274 error_report("OpenGL support is disabled");
4275 #endif
4276 exit(1);
4279 page_size_init();
4280 socket_init();
4282 if (qemu_opts_foreach(qemu_find_opts("object"),
4283 user_creatable_add_opts_foreach,
4284 object_create_initial, NULL)) {
4285 exit(1);
4288 if (qemu_opts_foreach(qemu_find_opts("chardev"),
4289 chardev_init_func, NULL, NULL)) {
4290 exit(1);
4293 #ifdef CONFIG_VIRTFS
4294 if (qemu_opts_foreach(qemu_find_opts("fsdev"),
4295 fsdev_init_func, NULL, NULL)) {
4296 exit(1);
4298 #endif
4300 if (pid_file && qemu_create_pidfile(pid_file) != 0) {
4301 error_report("could not acquire pid file: %s", strerror(errno));
4302 exit(1);
4305 if (qemu_opts_foreach(qemu_find_opts("device"),
4306 device_help_func, NULL, NULL)) {
4307 exit(0);
4310 machine_opts = qemu_get_machine_opts();
4311 if (qemu_opt_foreach(machine_opts, machine_set_property, current_machine,
4312 NULL)) {
4313 object_unref(OBJECT(current_machine));
4314 exit(1);
4317 configure_accelerator(current_machine);
4319 if (qtest_chrdev) {
4320 qtest_init(qtest_chrdev, qtest_log, &error_fatal);
4323 machine_opts = qemu_get_machine_opts();
4324 kernel_filename = qemu_opt_get(machine_opts, "kernel");
4325 initrd_filename = qemu_opt_get(machine_opts, "initrd");
4326 kernel_cmdline = qemu_opt_get(machine_opts, "append");
4327 bios_name = qemu_opt_get(machine_opts, "firmware");
4329 opts = qemu_opts_find(qemu_find_opts("boot-opts"), NULL);
4330 if (opts) {
4331 boot_order = qemu_opt_get(opts, "order");
4332 if (boot_order) {
4333 validate_bootdevices(boot_order, &error_fatal);
4336 boot_once = qemu_opt_get(opts, "once");
4337 if (boot_once) {
4338 validate_bootdevices(boot_once, &error_fatal);
4341 boot_menu = qemu_opt_get_bool(opts, "menu", boot_menu);
4342 boot_strict = qemu_opt_get_bool(opts, "strict", false);
4345 if (!boot_order) {
4346 boot_order = machine_class->default_boot_order;
4349 if (!kernel_cmdline) {
4350 kernel_cmdline = "";
4351 current_machine->kernel_cmdline = (char *)kernel_cmdline;
4354 linux_boot = (kernel_filename != NULL);
4356 if (!linux_boot && *kernel_cmdline != '\0') {
4357 error_report("-append only allowed with -kernel option");
4358 exit(1);
4361 if (!linux_boot && initrd_filename != NULL) {
4362 error_report("-initrd only allowed with -kernel option");
4363 exit(1);
4366 if (!linux_boot && qemu_opt_get(machine_opts, "dtb")) {
4367 error_report("-dtb only allowed with -kernel option");
4368 exit(1);
4371 if (semihosting_enabled() && !semihosting_get_argc() && kernel_filename) {
4372 /* fall back to the -kernel/-append */
4373 semihosting_arg_fallback(kernel_filename, kernel_cmdline);
4376 os_set_line_buffering();
4378 /* spice needs the timers to be initialized by this point */
4379 qemu_spice_init();
4381 cpu_ticks_init();
4382 if (icount_opts) {
4383 if (kvm_enabled() || xen_enabled()) {
4384 error_report("-icount is not allowed with kvm or xen");
4385 exit(1);
4387 configure_icount(icount_opts, &error_abort);
4388 qemu_opts_del(icount_opts);
4391 if (default_net) {
4392 QemuOptsList *net = qemu_find_opts("net");
4393 qemu_opts_set(net, NULL, "type", "nic", &error_abort);
4394 #ifdef CONFIG_SLIRP
4395 qemu_opts_set(net, NULL, "type", "user", &error_abort);
4396 #endif
4399 if (net_init_clients() < 0) {
4400 exit(1);
4403 if (qemu_opts_foreach(qemu_find_opts("object"),
4404 user_creatable_add_opts_foreach,
4405 object_create_delayed, NULL)) {
4406 exit(1);
4409 #ifdef CONFIG_TPM
4410 if (tpm_init() < 0) {
4411 exit(1);
4413 #endif
4415 /* init the bluetooth world */
4416 if (foreach_device_config(DEV_BT, bt_parse))
4417 exit(1);
4419 if (!xen_enabled()) {
4420 /* On 32-bit hosts, QEMU is limited by virtual address space */
4421 if (ram_size > (2047 << 20) && HOST_LONG_BITS == 32) {
4422 error_report("at most 2047 MB RAM can be simulated");
4423 exit(1);
4427 blk_mig_init();
4428 ram_mig_init();
4430 /* If the currently selected machine wishes to override the units-per-bus
4431 * property of its default HBA interface type, do so now. */
4432 if (machine_class->units_per_default_bus) {
4433 override_max_devs(machine_class->block_default_type,
4434 machine_class->units_per_default_bus);
4437 /* open the virtual block devices */
4438 if (snapshot || replay_mode != REPLAY_MODE_NONE) {
4439 qemu_opts_foreach(qemu_find_opts("drive"), drive_enable_snapshot,
4440 NULL, NULL);
4442 if (qemu_opts_foreach(qemu_find_opts("drive"), drive_init_func,
4443 &machine_class->block_default_type, NULL)) {
4444 exit(1);
4447 default_drive(default_cdrom, snapshot, machine_class->block_default_type, 2,
4448 CDROM_OPTS);
4449 default_drive(default_floppy, snapshot, IF_FLOPPY, 0, FD_OPTS);
4450 default_drive(default_sdcard, snapshot, IF_SD, 0, SD_OPTS);
4452 parse_numa_opts(machine_class);
4454 if (qemu_opts_foreach(qemu_find_opts("mon"),
4455 mon_init_func, NULL, NULL)) {
4456 exit(1);
4459 if (foreach_device_config(DEV_SERIAL, serial_parse) < 0)
4460 exit(1);
4461 if (foreach_device_config(DEV_PARALLEL, parallel_parse) < 0)
4462 exit(1);
4463 if (foreach_device_config(DEV_VIRTCON, virtcon_parse) < 0)
4464 exit(1);
4465 if (foreach_device_config(DEV_SCLP, sclp_parse) < 0) {
4466 exit(1);
4468 if (foreach_device_config(DEV_DEBUGCON, debugcon_parse) < 0)
4469 exit(1);
4471 /* If no default VGA is requested, the default is "none". */
4472 if (default_vga) {
4473 if (machine_class->default_display) {
4474 vga_model = machine_class->default_display;
4475 } else if (vga_interface_available(VGA_CIRRUS)) {
4476 vga_model = "cirrus";
4477 } else if (vga_interface_available(VGA_STD)) {
4478 vga_model = "std";
4481 if (vga_model) {
4482 select_vgahw(vga_model);
4485 if (watchdog) {
4486 i = select_watchdog(watchdog);
4487 if (i > 0)
4488 exit (i == 1 ? 1 : 0);
4491 machine_register_compat_props(current_machine);
4493 qemu_opts_foreach(qemu_find_opts("global"),
4494 global_init_func, NULL, NULL);
4496 /* This checkpoint is required by replay to separate prior clock
4497 reading from the other reads, because timer polling functions query
4498 clock values from the log. */
4499 replay_checkpoint(CHECKPOINT_INIT);
4500 qdev_machine_init();
4502 current_machine->ram_size = ram_size;
4503 current_machine->maxram_size = maxram_size;
4504 current_machine->ram_slots = ram_slots;
4505 current_machine->boot_order = boot_order;
4506 current_machine->cpu_model = cpu_model;
4508 machine_class->init(current_machine);
4510 realtime_init();
4512 audio_init();
4514 cpu_synchronize_all_post_init();
4516 numa_post_machine_init();
4518 if (qemu_opts_foreach(qemu_find_opts("fw_cfg"),
4519 parse_fw_cfg, fw_cfg_find(), NULL) != 0) {
4520 exit(1);
4523 /* init USB devices */
4524 if (machine_usb(current_machine)) {
4525 if (foreach_device_config(DEV_USB, usb_parse) < 0)
4526 exit(1);
4529 /* Check if IGD GFX passthrough. */
4530 igd_gfx_passthru();
4532 /* init generic devices */
4533 rom_set_order_override(FW_CFG_ORDER_OVERRIDE_DEVICE);
4534 if (qemu_opts_foreach(qemu_find_opts("device"),
4535 device_init_func, NULL, NULL)) {
4536 exit(1);
4538 rom_reset_order_override();
4540 /* Did we create any drives that we failed to create a device for? */
4541 drive_check_orphaned();
4543 /* Don't warn about the default network setup that you get if
4544 * no command line -net or -netdev options are specified. There
4545 * are two cases that we would otherwise complain about:
4546 * (1) board doesn't support a NIC but the implicit "-net nic"
4547 * requested one
4548 * (2) CONFIG_SLIRP not set, in which case the implicit "-net nic"
4549 * sets up a nic that isn't connected to anything.
4551 if (!default_net) {
4552 net_check_clients();
4556 if (boot_once) {
4557 qemu_boot_set(boot_once, &error_fatal);
4558 qemu_register_reset(restore_boot_order, g_strdup(boot_order));
4561 ds = init_displaystate();
4563 /* init local displays */
4564 switch (display_type) {
4565 case DT_CURSES:
4566 curses_display_init(ds, full_screen);
4567 break;
4568 case DT_SDL:
4569 sdl_display_init(ds, full_screen, no_frame);
4570 break;
4571 case DT_COCOA:
4572 cocoa_display_init(ds, full_screen);
4573 break;
4574 case DT_GTK:
4575 gtk_display_init(ds, full_screen, grab_on_hover);
4576 break;
4577 default:
4578 break;
4581 /* must be after terminal init, SDL library changes signal handlers */
4582 os_setup_signal_handling();
4584 /* init remote displays */
4585 #ifdef CONFIG_VNC
4586 qemu_opts_foreach(qemu_find_opts("vnc"),
4587 vnc_init_func, NULL, NULL);
4588 #endif
4590 if (using_spice) {
4591 qemu_spice_display_init();
4594 if (foreach_device_config(DEV_GDB, gdbserver_start) < 0) {
4595 exit(1);
4598 qdev_machine_creation_done();
4600 /* TODO: once all bus devices are qdevified, this should be done
4601 * when bus is created by qdev.c */
4602 qemu_register_reset(qbus_reset_all_fn, sysbus_get_default());
4603 qemu_run_machine_init_done_notifiers();
4605 if (rom_check_and_register_reset() != 0) {
4606 error_report("rom check and register reset failed");
4607 exit(1);
4610 replay_start();
4612 /* This checkpoint is required by replay to separate prior clock
4613 reading from the other reads, because timer polling functions query
4614 clock values from the log. */
4615 replay_checkpoint(CHECKPOINT_RESET);
4616 qemu_system_reset(VMRESET_SILENT);
4617 register_global_state();
4618 if (loadvm) {
4619 if (load_vmstate(loadvm) < 0) {
4620 autostart = 0;
4624 qdev_prop_check_globals();
4625 if (vmstate_dump_file) {
4626 /* dump and exit */
4627 dump_vmstate_json_to_file(vmstate_dump_file);
4628 return 0;
4631 if (incoming) {
4632 Error *local_err = NULL;
4633 qemu_start_incoming_migration(incoming, &local_err);
4634 if (local_err) {
4635 error_reportf_err(local_err, "-incoming %s: ", incoming);
4636 exit(1);
4638 } else if (autostart) {
4639 vm_start();
4642 os_setup_post();
4644 trace_init_vcpu_events();
4645 main_loop();
4646 replay_disable_events();
4648 bdrv_close_all();
4649 pause_all_vcpus();
4650 res_free();
4651 #ifdef CONFIG_TPM
4652 tpm_cleanup();
4653 #endif
4655 /* vhost-user must be cleaned up before chardevs. */
4656 net_cleanup();
4657 qemu_chr_cleanup();
4659 return 0;