Merge remote-tracking branch 'qemu/master'
[qemu/ar7.git] / vl.c
blob3f1f7826aac9a3091c5c1c5d3a3a2569996291b7
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 qdev_prop_register_global(g);
2965 return 0;
2968 int main(int argc, char **argv)
2970 int i;
2971 int snapshot, linux_boot;
2972 const char *initrd_filename;
2973 const char *kernel_filename, *kernel_cmdline;
2974 const char *boot_order = NULL;
2975 const char *boot_once = NULL;
2976 DisplayState *ds;
2977 int cyls, heads, secs, translation;
2978 QemuOpts *hda_opts = NULL, *opts, *machine_opts, *icount_opts = NULL;
2979 QemuOptsList *olist;
2980 int optind;
2981 const char *optarg;
2982 const char *loadvm = NULL;
2983 MachineClass *machine_class;
2984 const char *cpu_model;
2985 const char *vga_model = NULL;
2986 const char *qtest_chrdev = NULL;
2987 const char *qtest_log = NULL;
2988 const char *pid_file = NULL;
2989 const char *incoming = NULL;
2990 bool defconfig = true;
2991 bool userconfig = true;
2992 bool nographic = false;
2993 DisplayType display_type = DT_DEFAULT;
2994 int display_remote = 0;
2995 const char *log_mask = NULL;
2996 const char *log_file = NULL;
2997 char *trace_file = NULL;
2998 ram_addr_t maxram_size;
2999 uint64_t ram_slots = 0;
3000 FILE *vmstate_dump_file = NULL;
3001 Error *main_loop_err = NULL;
3002 Error *err = NULL;
3003 bool list_data_dirs = false;
3005 qemu_init_cpu_loop();
3006 qemu_mutex_lock_iothread();
3008 atexit(qemu_run_exit_notifiers);
3009 error_set_progname(argv[0]);
3010 qemu_init_exec_dir(argv[0]);
3012 module_call_init(MODULE_INIT_QOM);
3014 qemu_add_opts(&qemu_drive_opts);
3015 qemu_add_drive_opts(&qemu_legacy_drive_opts);
3016 qemu_add_drive_opts(&qemu_common_drive_opts);
3017 qemu_add_drive_opts(&qemu_drive_opts);
3018 qemu_add_opts(&qemu_chardev_opts);
3019 qemu_add_opts(&qemu_device_opts);
3020 qemu_add_opts(&qemu_netdev_opts);
3021 qemu_add_opts(&qemu_net_opts);
3022 qemu_add_opts(&qemu_rtc_opts);
3023 qemu_add_opts(&qemu_global_opts);
3024 qemu_add_opts(&qemu_mon_opts);
3025 qemu_add_opts(&qemu_trace_opts);
3026 qemu_add_opts(&qemu_option_rom_opts);
3027 qemu_add_opts(&qemu_machine_opts);
3028 qemu_add_opts(&qemu_mem_opts);
3029 qemu_add_opts(&qemu_smp_opts);
3030 qemu_add_opts(&qemu_boot_opts);
3031 qemu_add_opts(&qemu_sandbox_opts);
3032 qemu_add_opts(&qemu_add_fd_opts);
3033 qemu_add_opts(&qemu_object_opts);
3034 qemu_add_opts(&qemu_tpmdev_opts);
3035 qemu_add_opts(&qemu_realtime_opts);
3036 qemu_add_opts(&qemu_msg_opts);
3037 qemu_add_opts(&qemu_name_opts);
3038 qemu_add_opts(&qemu_numa_opts);
3039 qemu_add_opts(&qemu_icount_opts);
3040 qemu_add_opts(&qemu_semihosting_config_opts);
3041 qemu_add_opts(&qemu_fw_cfg_opts);
3042 module_call_init(MODULE_INIT_OPTS);
3044 runstate_init();
3046 if (qcrypto_init(&err) < 0) {
3047 error_reportf_err(err, "cannot initialize crypto: ");
3048 exit(1);
3050 rtc_clock = QEMU_CLOCK_HOST;
3052 QLIST_INIT (&vm_change_state_head);
3053 os_setup_early_signal_handling();
3055 cpu_model = NULL;
3056 snapshot = 0;
3057 cyls = heads = secs = 0;
3058 translation = BIOS_ATA_TRANSLATION_AUTO;
3060 nb_nics = 0;
3062 bdrv_init_with_whitelist();
3064 autostart = 1;
3066 /* first pass of option parsing */
3067 optind = 1;
3068 while (optind < argc) {
3069 if (argv[optind][0] != '-') {
3070 /* disk image */
3071 optind++;
3072 } else {
3073 const QEMUOption *popt;
3075 popt = lookup_opt(argc, argv, &optarg, &optind);
3076 switch (popt->index) {
3077 case QEMU_OPTION_nodefconfig:
3078 defconfig = false;
3079 break;
3080 case QEMU_OPTION_nouserconfig:
3081 userconfig = false;
3082 break;
3087 if (defconfig) {
3088 int ret;
3089 ret = qemu_read_default_config_files(userconfig);
3090 if (ret < 0) {
3091 exit(1);
3095 /* second pass of option parsing */
3096 optind = 1;
3097 for(;;) {
3098 if (optind >= argc)
3099 break;
3100 if (argv[optind][0] != '-') {
3101 hda_opts = drive_add(IF_DEFAULT, 0, argv[optind++], HD_OPTS);
3102 } else {
3103 const QEMUOption *popt;
3105 popt = lookup_opt(argc, argv, &optarg, &optind);
3106 if (!(popt->arch_mask & arch_type)) {
3107 error_report("Option not supported for this target");
3108 exit(1);
3110 switch(popt->index) {
3111 case QEMU_OPTION_no_kvm_irqchip: {
3112 olist = qemu_find_opts("machine");
3113 qemu_opts_parse_noisily(olist, "kernel_irqchip=off", false);
3114 break;
3116 case QEMU_OPTION_cpu:
3117 /* hw initialization will check this */
3118 cpu_model = optarg;
3119 break;
3120 case QEMU_OPTION_hda:
3122 char buf[256];
3123 if (cyls == 0)
3124 snprintf(buf, sizeof(buf), "%s", HD_OPTS);
3125 else
3126 snprintf(buf, sizeof(buf),
3127 "%s,cyls=%d,heads=%d,secs=%d%s",
3128 HD_OPTS , cyls, heads, secs,
3129 translation == BIOS_ATA_TRANSLATION_LBA ?
3130 ",trans=lba" :
3131 translation == BIOS_ATA_TRANSLATION_NONE ?
3132 ",trans=none" : "");
3133 drive_add(IF_DEFAULT, 0, optarg, buf);
3134 break;
3136 case QEMU_OPTION_hdb:
3137 case QEMU_OPTION_hdc:
3138 case QEMU_OPTION_hdd:
3139 drive_add(IF_DEFAULT, popt->index - QEMU_OPTION_hda, optarg,
3140 HD_OPTS);
3141 break;
3142 case QEMU_OPTION_drive:
3143 if (drive_def(optarg) == NULL) {
3144 exit(1);
3146 break;
3147 case QEMU_OPTION_set:
3148 if (qemu_set_option(optarg) != 0)
3149 exit(1);
3150 break;
3151 case QEMU_OPTION_global:
3152 if (qemu_global_option(optarg) != 0)
3153 exit(1);
3154 break;
3155 case QEMU_OPTION_mtdblock:
3156 drive_add(IF_MTD, -1, optarg, MTD_OPTS);
3157 break;
3158 case QEMU_OPTION_sd:
3159 drive_add(IF_SD, -1, optarg, SD_OPTS);
3160 break;
3161 case QEMU_OPTION_pflash:
3162 drive_add(IF_PFLASH, -1, optarg, PFLASH_OPTS);
3163 break;
3164 case QEMU_OPTION_snapshot:
3165 snapshot = 1;
3166 break;
3167 case QEMU_OPTION_hdachs:
3169 const char *p;
3170 p = optarg;
3171 cyls = strtol(p, (char **)&p, 0);
3172 if (cyls < 1 || cyls > 16383)
3173 goto chs_fail;
3174 if (*p != ',')
3175 goto chs_fail;
3176 p++;
3177 heads = strtol(p, (char **)&p, 0);
3178 if (heads < 1 || heads > 16)
3179 goto chs_fail;
3180 if (*p != ',')
3181 goto chs_fail;
3182 p++;
3183 secs = strtol(p, (char **)&p, 0);
3184 if (secs < 1 || secs > 63)
3185 goto chs_fail;
3186 if (*p == ',') {
3187 p++;
3188 if (!strcmp(p, "large")) {
3189 translation = BIOS_ATA_TRANSLATION_LARGE;
3190 } else if (!strcmp(p, "rechs")) {
3191 translation = BIOS_ATA_TRANSLATION_RECHS;
3192 } else if (!strcmp(p, "none")) {
3193 translation = BIOS_ATA_TRANSLATION_NONE;
3194 } else if (!strcmp(p, "lba")) {
3195 translation = BIOS_ATA_TRANSLATION_LBA;
3196 } else if (!strcmp(p, "auto")) {
3197 translation = BIOS_ATA_TRANSLATION_AUTO;
3198 } else {
3199 goto chs_fail;
3201 } else if (*p != '\0') {
3202 chs_fail:
3203 error_report("invalid physical CHS format");
3204 exit(1);
3206 if (hda_opts != NULL) {
3207 qemu_opt_set_number(hda_opts, "cyls", cyls,
3208 &error_abort);
3209 qemu_opt_set_number(hda_opts, "heads", heads,
3210 &error_abort);
3211 qemu_opt_set_number(hda_opts, "secs", secs,
3212 &error_abort);
3213 if (translation == BIOS_ATA_TRANSLATION_LARGE) {
3214 qemu_opt_set(hda_opts, "trans", "large",
3215 &error_abort);
3216 } else if (translation == BIOS_ATA_TRANSLATION_RECHS) {
3217 qemu_opt_set(hda_opts, "trans", "rechs",
3218 &error_abort);
3219 } else if (translation == BIOS_ATA_TRANSLATION_LBA) {
3220 qemu_opt_set(hda_opts, "trans", "lba",
3221 &error_abort);
3222 } else if (translation == BIOS_ATA_TRANSLATION_NONE) {
3223 qemu_opt_set(hda_opts, "trans", "none",
3224 &error_abort);
3228 break;
3229 case QEMU_OPTION_numa:
3230 opts = qemu_opts_parse_noisily(qemu_find_opts("numa"),
3231 optarg, true);
3232 if (!opts) {
3233 exit(1);
3235 break;
3236 case QEMU_OPTION_display:
3237 display_type = select_display(optarg);
3238 break;
3239 case QEMU_OPTION_nographic:
3240 olist = qemu_find_opts("machine");
3241 qemu_opts_parse_noisily(olist, "graphics=off", false);
3242 nographic = true;
3243 display_type = DT_NONE;
3244 break;
3245 case QEMU_OPTION_curses:
3246 #ifdef CONFIG_CURSES
3247 display_type = DT_CURSES;
3248 #else
3249 error_report("curses support is disabled");
3250 exit(1);
3251 #endif
3252 break;
3253 case QEMU_OPTION_portrait:
3254 graphic_rotate = 90;
3255 break;
3256 case QEMU_OPTION_rotate:
3257 graphic_rotate = strtol(optarg, (char **) &optarg, 10);
3258 if (graphic_rotate != 0 && graphic_rotate != 90 &&
3259 graphic_rotate != 180 && graphic_rotate != 270) {
3260 error_report("only 90, 180, 270 deg rotation is available");
3261 exit(1);
3263 break;
3264 case QEMU_OPTION_kernel:
3265 qemu_opts_set(qemu_find_opts("machine"), 0, "kernel", optarg,
3266 &error_abort);
3267 break;
3268 case QEMU_OPTION_initrd:
3269 qemu_opts_set(qemu_find_opts("machine"), 0, "initrd", optarg,
3270 &error_abort);
3271 break;
3272 case QEMU_OPTION_append:
3273 qemu_opts_set(qemu_find_opts("machine"), 0, "append", optarg,
3274 &error_abort);
3275 break;
3276 case QEMU_OPTION_dtb:
3277 qemu_opts_set(qemu_find_opts("machine"), 0, "dtb", optarg,
3278 &error_abort);
3279 break;
3280 case QEMU_OPTION_cdrom:
3281 drive_add(IF_DEFAULT, 2, optarg, CDROM_OPTS);
3282 break;
3283 case QEMU_OPTION_boot:
3284 opts = qemu_opts_parse_noisily(qemu_find_opts("boot-opts"),
3285 optarg, true);
3286 if (!opts) {
3287 exit(1);
3289 break;
3290 case QEMU_OPTION_fda:
3291 case QEMU_OPTION_fdb:
3292 drive_add(IF_FLOPPY, popt->index - QEMU_OPTION_fda,
3293 optarg, FD_OPTS);
3294 break;
3295 case QEMU_OPTION_no_fd_bootchk:
3296 fd_bootchk = 0;
3297 break;
3298 case QEMU_OPTION_netdev:
3299 default_net = 0;
3300 if (net_client_parse(qemu_find_opts("netdev"), optarg) == -1) {
3301 exit(1);
3303 break;
3304 case QEMU_OPTION_net:
3305 default_net = 0;
3306 if (net_client_parse(qemu_find_opts("net"), optarg) == -1) {
3307 exit(1);
3309 break;
3310 #ifdef CONFIG_LIBISCSI
3311 case QEMU_OPTION_iscsi:
3312 opts = qemu_opts_parse_noisily(qemu_find_opts("iscsi"),
3313 optarg, false);
3314 if (!opts) {
3315 exit(1);
3317 break;
3318 #endif
3319 #ifdef CONFIG_SLIRP
3320 case QEMU_OPTION_tftp:
3321 error_report("The -tftp option is deprecated. "
3322 "Please use '-netdev user,tftp=...' instead.");
3323 legacy_tftp_prefix = optarg;
3324 break;
3325 case QEMU_OPTION_bootp:
3326 error_report("The -bootp option is deprecated. "
3327 "Please use '-netdev user,bootfile=...' instead.");
3328 legacy_bootp_filename = optarg;
3329 break;
3330 case QEMU_OPTION_redir:
3331 error_report("The -redir option is deprecated. "
3332 "Please use '-netdev user,hostfwd=...' instead.");
3333 if (net_slirp_redir(optarg) < 0)
3334 exit(1);
3335 break;
3336 #endif
3337 case QEMU_OPTION_bt:
3338 add_device_config(DEV_BT, optarg);
3339 break;
3340 case QEMU_OPTION_audio_help:
3341 AUD_help ();
3342 exit (0);
3343 break;
3344 case QEMU_OPTION_soundhw:
3345 select_soundhw (optarg);
3346 break;
3347 case QEMU_OPTION_h:
3348 help(0);
3349 break;
3350 case QEMU_OPTION_version:
3351 version();
3352 exit(0);
3353 break;
3354 case QEMU_OPTION_m:
3355 opts = qemu_opts_parse_noisily(qemu_find_opts("memory"),
3356 optarg, true);
3357 if (!opts) {
3358 exit(EXIT_FAILURE);
3360 break;
3361 #ifdef CONFIG_TPM
3362 case QEMU_OPTION_tpmdev:
3363 if (tpm_config_parse(qemu_find_opts("tpmdev"), optarg) < 0) {
3364 exit(1);
3366 break;
3367 #endif
3368 case QEMU_OPTION_mempath:
3369 mem_path = optarg;
3370 break;
3371 case QEMU_OPTION_mem_prealloc:
3372 mem_prealloc = 1;
3373 break;
3374 case QEMU_OPTION_d:
3375 log_mask = optarg;
3376 break;
3377 case QEMU_OPTION_D:
3378 log_file = optarg;
3379 break;
3380 case QEMU_OPTION_DFILTER:
3381 qemu_set_dfilter_ranges(optarg, &error_fatal);
3382 break;
3383 case QEMU_OPTION_s:
3384 add_device_config(DEV_GDB, "tcp::" DEFAULT_GDBSTUB_PORT);
3385 break;
3386 case QEMU_OPTION_gdb:
3387 add_device_config(DEV_GDB, optarg);
3388 break;
3389 case QEMU_OPTION_L:
3390 if (is_help_option(optarg)) {
3391 list_data_dirs = true;
3392 } else if (data_dir_idx < ARRAY_SIZE(data_dir)) {
3393 data_dir[data_dir_idx++] = optarg;
3395 break;
3396 case QEMU_OPTION_bios:
3397 qemu_opts_set(qemu_find_opts("machine"), 0, "firmware", optarg,
3398 &error_abort);
3399 break;
3400 case QEMU_OPTION_singlestep:
3401 singlestep = 1;
3402 break;
3403 case QEMU_OPTION_S:
3404 autostart = 0;
3405 break;
3406 case QEMU_OPTION_k:
3407 keyboard_layout = optarg;
3408 break;
3409 case QEMU_OPTION_localtime:
3410 rtc_utc = 0;
3411 break;
3412 case QEMU_OPTION_vga:
3413 vga_model = optarg;
3414 default_vga = 0;
3415 break;
3416 case QEMU_OPTION_g:
3418 const char *p;
3419 int w, h, depth;
3420 p = optarg;
3421 w = strtol(p, (char **)&p, 10);
3422 if (w <= 0) {
3423 graphic_error:
3424 error_report("invalid resolution or depth");
3425 exit(1);
3427 if (*p != 'x')
3428 goto graphic_error;
3429 p++;
3430 h = strtol(p, (char **)&p, 10);
3431 if (h <= 0)
3432 goto graphic_error;
3433 if (*p == 'x') {
3434 p++;
3435 depth = strtol(p, (char **)&p, 10);
3436 if (depth != 8 && depth != 15 && depth != 16 &&
3437 depth != 24 && depth != 32)
3438 goto graphic_error;
3439 } else if (*p == '\0') {
3440 depth = graphic_depth;
3441 } else {
3442 goto graphic_error;
3445 graphic_width = w;
3446 graphic_height = h;
3447 graphic_depth = depth;
3449 break;
3450 case QEMU_OPTION_echr:
3452 char *r;
3453 term_escape_char = strtol(optarg, &r, 0);
3454 if (r == optarg)
3455 printf("Bad argument to echr\n");
3456 break;
3458 case QEMU_OPTION_monitor:
3459 default_monitor = 0;
3460 if (strncmp(optarg, "none", 4)) {
3461 monitor_parse(optarg, "readline", false);
3463 break;
3464 case QEMU_OPTION_qmp:
3465 monitor_parse(optarg, "control", false);
3466 default_monitor = 0;
3467 break;
3468 case QEMU_OPTION_qmp_pretty:
3469 monitor_parse(optarg, "control", true);
3470 default_monitor = 0;
3471 break;
3472 case QEMU_OPTION_mon:
3473 opts = qemu_opts_parse_noisily(qemu_find_opts("mon"), optarg,
3474 true);
3475 if (!opts) {
3476 exit(1);
3478 default_monitor = 0;
3479 break;
3480 case QEMU_OPTION_chardev:
3481 opts = qemu_opts_parse_noisily(qemu_find_opts("chardev"),
3482 optarg, true);
3483 if (!opts) {
3484 exit(1);
3486 break;
3487 case QEMU_OPTION_fsdev:
3488 olist = qemu_find_opts("fsdev");
3489 if (!olist) {
3490 error_report("fsdev support is disabled");
3491 exit(1);
3493 opts = qemu_opts_parse_noisily(olist, optarg, true);
3494 if (!opts) {
3495 exit(1);
3497 break;
3498 case QEMU_OPTION_virtfs: {
3499 QemuOpts *fsdev;
3500 QemuOpts *device;
3501 const char *writeout, *sock_fd, *socket;
3503 olist = qemu_find_opts("virtfs");
3504 if (!olist) {
3505 error_report("virtfs support is disabled");
3506 exit(1);
3508 opts = qemu_opts_parse_noisily(olist, optarg, true);
3509 if (!opts) {
3510 exit(1);
3513 if (qemu_opt_get(opts, "fsdriver") == NULL ||
3514 qemu_opt_get(opts, "mount_tag") == NULL) {
3515 error_report("Usage: -virtfs fsdriver,mount_tag=tag");
3516 exit(1);
3518 fsdev = qemu_opts_create(qemu_find_opts("fsdev"),
3519 qemu_opt_get(opts, "mount_tag"),
3520 1, NULL);
3521 if (!fsdev) {
3522 error_report("duplicate fsdev id: %s",
3523 qemu_opt_get(opts, "mount_tag"));
3524 exit(1);
3527 writeout = qemu_opt_get(opts, "writeout");
3528 if (writeout) {
3529 #ifdef CONFIG_SYNC_FILE_RANGE
3530 qemu_opt_set(fsdev, "writeout", writeout, &error_abort);
3531 #else
3532 error_report("writeout=immediate not supported "
3533 "on this platform");
3534 exit(1);
3535 #endif
3537 qemu_opt_set(fsdev, "fsdriver",
3538 qemu_opt_get(opts, "fsdriver"), &error_abort);
3539 qemu_opt_set(fsdev, "path", qemu_opt_get(opts, "path"),
3540 &error_abort);
3541 qemu_opt_set(fsdev, "security_model",
3542 qemu_opt_get(opts, "security_model"),
3543 &error_abort);
3544 socket = qemu_opt_get(opts, "socket");
3545 if (socket) {
3546 qemu_opt_set(fsdev, "socket", socket, &error_abort);
3548 sock_fd = qemu_opt_get(opts, "sock_fd");
3549 if (sock_fd) {
3550 qemu_opt_set(fsdev, "sock_fd", sock_fd, &error_abort);
3553 qemu_opt_set_bool(fsdev, "readonly",
3554 qemu_opt_get_bool(opts, "readonly", 0),
3555 &error_abort);
3556 device = qemu_opts_create(qemu_find_opts("device"), NULL, 0,
3557 &error_abort);
3558 qemu_opt_set(device, "driver", "virtio-9p-pci", &error_abort);
3559 qemu_opt_set(device, "fsdev",
3560 qemu_opt_get(opts, "mount_tag"), &error_abort);
3561 qemu_opt_set(device, "mount_tag",
3562 qemu_opt_get(opts, "mount_tag"), &error_abort);
3563 break;
3565 case QEMU_OPTION_virtfs_synth: {
3566 QemuOpts *fsdev;
3567 QemuOpts *device;
3569 fsdev = qemu_opts_create(qemu_find_opts("fsdev"), "v_synth",
3570 1, NULL);
3571 if (!fsdev) {
3572 error_report("duplicate option: %s", "virtfs_synth");
3573 exit(1);
3575 qemu_opt_set(fsdev, "fsdriver", "synth", &error_abort);
3577 device = qemu_opts_create(qemu_find_opts("device"), NULL, 0,
3578 &error_abort);
3579 qemu_opt_set(device, "driver", "virtio-9p-pci", &error_abort);
3580 qemu_opt_set(device, "fsdev", "v_synth", &error_abort);
3581 qemu_opt_set(device, "mount_tag", "v_synth", &error_abort);
3582 break;
3584 case QEMU_OPTION_serial:
3585 add_device_config(DEV_SERIAL, optarg);
3586 default_serial = 0;
3587 if (strncmp(optarg, "mon:", 4) == 0) {
3588 default_monitor = 0;
3590 break;
3591 case QEMU_OPTION_watchdog:
3592 if (watchdog) {
3593 error_report("only one watchdog option may be given");
3594 return 1;
3596 watchdog = optarg;
3597 break;
3598 case QEMU_OPTION_watchdog_action:
3599 if (select_watchdog_action(optarg) == -1) {
3600 error_report("unknown -watchdog-action parameter");
3601 exit(1);
3603 break;
3604 case QEMU_OPTION_virtiocon:
3605 add_device_config(DEV_VIRTCON, optarg);
3606 default_virtcon = 0;
3607 if (strncmp(optarg, "mon:", 4) == 0) {
3608 default_monitor = 0;
3610 break;
3611 case QEMU_OPTION_parallel:
3612 add_device_config(DEV_PARALLEL, optarg);
3613 default_parallel = 0;
3614 if (strncmp(optarg, "mon:", 4) == 0) {
3615 default_monitor = 0;
3617 break;
3618 case QEMU_OPTION_debugcon:
3619 add_device_config(DEV_DEBUGCON, optarg);
3620 break;
3621 case QEMU_OPTION_loadvm:
3622 loadvm = optarg;
3623 break;
3624 case QEMU_OPTION_full_screen:
3625 full_screen = 1;
3626 break;
3627 case QEMU_OPTION_no_frame:
3628 no_frame = 1;
3629 break;
3630 case QEMU_OPTION_alt_grab:
3631 alt_grab = 1;
3632 break;
3633 case QEMU_OPTION_ctrl_grab:
3634 ctrl_grab = 1;
3635 break;
3636 case QEMU_OPTION_no_quit:
3637 no_quit = 1;
3638 break;
3639 case QEMU_OPTION_sdl:
3640 #ifdef CONFIG_SDL
3641 display_type = DT_SDL;
3642 break;
3643 #else
3644 error_report("SDL support is disabled");
3645 exit(1);
3646 #endif
3647 case QEMU_OPTION_pidfile:
3648 pid_file = optarg;
3649 break;
3650 case QEMU_OPTION_win2k_hack:
3651 win2k_install_hack = 1;
3652 break;
3653 case QEMU_OPTION_rtc_td_hack: {
3654 static GlobalProperty slew_lost_ticks = {
3655 .driver = "mc146818rtc",
3656 .property = "lost_tick_policy",
3657 .value = "slew",
3660 qdev_prop_register_global(&slew_lost_ticks);
3661 break;
3663 case QEMU_OPTION_acpitable:
3664 opts = qemu_opts_parse_noisily(qemu_find_opts("acpi"),
3665 optarg, true);
3666 if (!opts) {
3667 exit(1);
3669 do_acpitable_option(opts);
3670 break;
3671 case QEMU_OPTION_smbios:
3672 opts = qemu_opts_parse_noisily(qemu_find_opts("smbios"),
3673 optarg, false);
3674 if (!opts) {
3675 exit(1);
3677 do_smbios_option(opts);
3678 break;
3679 case QEMU_OPTION_fwcfg:
3680 opts = qemu_opts_parse_noisily(qemu_find_opts("fw_cfg"),
3681 optarg, true);
3682 if (opts == NULL) {
3683 exit(1);
3685 break;
3686 case QEMU_OPTION_enable_kvm:
3687 olist = qemu_find_opts("machine");
3688 qemu_opts_parse_noisily(olist, "accel=kvm", false);
3689 break;
3690 case QEMU_OPTION_M:
3691 case QEMU_OPTION_machine:
3692 olist = qemu_find_opts("machine");
3693 opts = qemu_opts_parse_noisily(olist, optarg, true);
3694 if (!opts) {
3695 exit(1);
3697 break;
3698 case QEMU_OPTION_no_kvm:
3699 olist = qemu_find_opts("machine");
3700 qemu_opts_parse_noisily(olist, "accel=tcg", false);
3701 break;
3702 case QEMU_OPTION_no_kvm_pit: {
3703 error_report("warning: ignoring deprecated option");
3704 break;
3706 case QEMU_OPTION_no_kvm_pit_reinjection: {
3707 static GlobalProperty kvm_pit_lost_tick_policy = {
3708 .driver = "kvm-pit",
3709 .property = "lost_tick_policy",
3710 .value = "discard",
3713 error_report("warning: deprecated, replaced by "
3714 "-global kvm-pit.lost_tick_policy=discard");
3715 qdev_prop_register_global(&kvm_pit_lost_tick_policy);
3716 break;
3718 case QEMU_OPTION_usb:
3719 olist = qemu_find_opts("machine");
3720 qemu_opts_parse_noisily(olist, "usb=on", false);
3721 break;
3722 case QEMU_OPTION_usbdevice:
3723 olist = qemu_find_opts("machine");
3724 qemu_opts_parse_noisily(olist, "usb=on", false);
3725 add_device_config(DEV_USB, optarg);
3726 break;
3727 case QEMU_OPTION_device:
3728 if (!qemu_opts_parse_noisily(qemu_find_opts("device"),
3729 optarg, true)) {
3730 exit(1);
3732 break;
3733 case QEMU_OPTION_smp:
3734 if (!qemu_opts_parse_noisily(qemu_find_opts("smp-opts"),
3735 optarg, true)) {
3736 exit(1);
3738 break;
3739 case QEMU_OPTION_vnc:
3740 vnc_parse(optarg, &error_fatal);
3741 break;
3742 case QEMU_OPTION_no_acpi:
3743 acpi_enabled = 0;
3744 break;
3745 case QEMU_OPTION_no_hpet:
3746 no_hpet = 1;
3747 break;
3748 case QEMU_OPTION_balloon:
3749 if (balloon_parse(optarg) < 0) {
3750 error_report("unknown -balloon argument %s", optarg);
3751 exit(1);
3753 break;
3754 case QEMU_OPTION_no_reboot:
3755 no_reboot = 1;
3756 break;
3757 case QEMU_OPTION_no_shutdown:
3758 no_shutdown = 1;
3759 break;
3760 case QEMU_OPTION_show_cursor:
3761 cursor_hide = 0;
3762 break;
3763 case QEMU_OPTION_uuid:
3764 if(qemu_uuid_parse(optarg, qemu_uuid) < 0) {
3765 error_report("failed to parse UUID string: wrong format");
3766 exit(1);
3768 qemu_uuid_set = true;
3769 break;
3770 case QEMU_OPTION_option_rom:
3771 if (nb_option_roms >= MAX_OPTION_ROMS) {
3772 error_report("too many option ROMs");
3773 exit(1);
3775 opts = qemu_opts_parse_noisily(qemu_find_opts("option-rom"),
3776 optarg, true);
3777 if (!opts) {
3778 exit(1);
3780 option_rom[nb_option_roms].name = qemu_opt_get(opts, "romfile");
3781 option_rom[nb_option_roms].bootindex =
3782 qemu_opt_get_number(opts, "bootindex", -1);
3783 if (!option_rom[nb_option_roms].name) {
3784 error_report("Option ROM file is not specified");
3785 exit(1);
3787 nb_option_roms++;
3788 break;
3789 case QEMU_OPTION_semihosting:
3790 semihosting.enabled = true;
3791 semihosting.target = SEMIHOSTING_TARGET_AUTO;
3792 break;
3793 case QEMU_OPTION_semihosting_config:
3794 semihosting.enabled = true;
3795 opts = qemu_opts_parse_noisily(qemu_find_opts("semihosting-config"),
3796 optarg, false);
3797 if (opts != NULL) {
3798 semihosting.enabled = qemu_opt_get_bool(opts, "enable",
3799 true);
3800 const char *target = qemu_opt_get(opts, "target");
3801 if (target != NULL) {
3802 if (strcmp("native", target) == 0) {
3803 semihosting.target = SEMIHOSTING_TARGET_NATIVE;
3804 } else if (strcmp("gdb", target) == 0) {
3805 semihosting.target = SEMIHOSTING_TARGET_GDB;
3806 } else if (strcmp("auto", target) == 0) {
3807 semihosting.target = SEMIHOSTING_TARGET_AUTO;
3808 } else {
3809 error_report("unsupported semihosting-config %s",
3810 optarg);
3811 exit(1);
3813 } else {
3814 semihosting.target = SEMIHOSTING_TARGET_AUTO;
3816 /* Set semihosting argument count and vector */
3817 qemu_opt_foreach(opts, add_semihosting_arg,
3818 &semihosting, NULL);
3819 } else {
3820 error_report("unsupported semihosting-config %s", optarg);
3821 exit(1);
3823 break;
3824 case QEMU_OPTION_tdf:
3825 error_report("warning: ignoring deprecated option");
3826 break;
3827 case QEMU_OPTION_name:
3828 opts = qemu_opts_parse_noisily(qemu_find_opts("name"),
3829 optarg, true);
3830 if (!opts) {
3831 exit(1);
3833 break;
3834 case QEMU_OPTION_prom_env:
3835 if (nb_prom_envs >= MAX_PROM_ENVS) {
3836 error_report("too many prom variables");
3837 exit(1);
3839 prom_envs[nb_prom_envs] = optarg;
3840 nb_prom_envs++;
3841 break;
3842 case QEMU_OPTION_old_param:
3843 old_param = 1;
3844 break;
3845 case QEMU_OPTION_clock:
3846 /* Clock options no longer exist. Keep this option for
3847 * backward compatibility.
3849 break;
3850 case QEMU_OPTION_startdate:
3851 configure_rtc_date_offset(optarg, 1);
3852 break;
3853 case QEMU_OPTION_rtc:
3854 opts = qemu_opts_parse_noisily(qemu_find_opts("rtc"), optarg,
3855 false);
3856 if (!opts) {
3857 exit(1);
3859 configure_rtc(opts);
3860 break;
3861 case QEMU_OPTION_tb_size:
3862 tcg_tb_size = strtol(optarg, NULL, 0);
3863 if (tcg_tb_size < 0) {
3864 tcg_tb_size = 0;
3866 break;
3867 case QEMU_OPTION_icount:
3868 icount_opts = qemu_opts_parse_noisily(qemu_find_opts("icount"),
3869 optarg, true);
3870 if (!icount_opts) {
3871 exit(1);
3873 break;
3874 case QEMU_OPTION_incoming:
3875 if (!incoming) {
3876 runstate_set(RUN_STATE_INMIGRATE);
3878 incoming = optarg;
3879 break;
3880 case QEMU_OPTION_nodefaults:
3881 has_defaults = 0;
3882 break;
3883 case QEMU_OPTION_xen_domid:
3884 if (!(xen_available())) {
3885 error_report("Option not supported for this target");
3886 exit(1);
3888 xen_domid = atoi(optarg);
3889 break;
3890 case QEMU_OPTION_xen_create:
3891 if (!(xen_available())) {
3892 error_report("Option not supported for this target");
3893 exit(1);
3895 xen_mode = XEN_CREATE;
3896 break;
3897 case QEMU_OPTION_xen_attach:
3898 if (!(xen_available())) {
3899 error_report("Option not supported for this target");
3900 exit(1);
3902 xen_mode = XEN_ATTACH;
3903 break;
3904 case QEMU_OPTION_trace:
3905 g_free(trace_file);
3906 trace_file = trace_opt_parse(optarg);
3907 break;
3908 case QEMU_OPTION_trace_unassigned:
3909 trace_unassigned = true;
3910 break;
3911 case QEMU_OPTION_readconfig:
3913 int ret = qemu_read_config_file(optarg);
3914 if (ret < 0) {
3915 error_report("read config %s: %s", optarg,
3916 strerror(-ret));
3917 exit(1);
3919 break;
3921 case QEMU_OPTION_spice:
3922 olist = qemu_find_opts("spice");
3923 if (!olist) {
3924 error_report("spice support is disabled");
3925 exit(1);
3927 opts = qemu_opts_parse_noisily(olist, optarg, false);
3928 if (!opts) {
3929 exit(1);
3931 display_remote++;
3932 break;
3933 case QEMU_OPTION_writeconfig:
3935 FILE *fp;
3936 if (strcmp(optarg, "-") == 0) {
3937 fp = stdout;
3938 } else {
3939 fp = fopen(optarg, "w");
3940 if (fp == NULL) {
3941 error_report("open %s: %s", optarg,
3942 strerror(errno));
3943 exit(1);
3946 qemu_config_write(fp);
3947 if (fp != stdout) {
3948 fclose(fp);
3950 break;
3952 case QEMU_OPTION_qtest:
3953 qtest_chrdev = optarg;
3954 break;
3955 case QEMU_OPTION_qtest_log:
3956 qtest_log = optarg;
3957 break;
3958 case QEMU_OPTION_sandbox:
3959 opts = qemu_opts_parse_noisily(qemu_find_opts("sandbox"),
3960 optarg, true);
3961 if (!opts) {
3962 exit(1);
3964 break;
3965 case QEMU_OPTION_add_fd:
3966 #ifndef _WIN32
3967 opts = qemu_opts_parse_noisily(qemu_find_opts("add-fd"),
3968 optarg, false);
3969 if (!opts) {
3970 exit(1);
3972 #else
3973 error_report("File descriptor passing is disabled on this "
3974 "platform");
3975 exit(1);
3976 #endif
3977 break;
3978 case QEMU_OPTION_object:
3979 opts = qemu_opts_parse_noisily(qemu_find_opts("object"),
3980 optarg, true);
3981 if (!opts) {
3982 exit(1);
3984 break;
3985 case QEMU_OPTION_realtime:
3986 opts = qemu_opts_parse_noisily(qemu_find_opts("realtime"),
3987 optarg, false);
3988 if (!opts) {
3989 exit(1);
3991 enable_mlock = qemu_opt_get_bool(opts, "mlock", true);
3992 break;
3993 case QEMU_OPTION_msg:
3994 opts = qemu_opts_parse_noisily(qemu_find_opts("msg"), optarg,
3995 false);
3996 if (!opts) {
3997 exit(1);
3999 configure_msg(opts);
4000 break;
4001 case QEMU_OPTION_dump_vmstate:
4002 if (vmstate_dump_file) {
4003 error_report("only one '-dump-vmstate' "
4004 "option may be given");
4005 exit(1);
4007 vmstate_dump_file = fopen(optarg, "w");
4008 if (vmstate_dump_file == NULL) {
4009 error_report("open %s: %s", optarg, strerror(errno));
4010 exit(1);
4012 break;
4013 default:
4014 os_parse_cmd_args(popt->index, optarg);
4019 * Clear error location left behind by the loop.
4020 * Best done right after the loop. Do not insert code here!
4022 loc_set_none();
4024 replay_configure(icount_opts);
4026 machine_class = select_machine();
4028 set_memory_options(&ram_slots, &maxram_size, machine_class);
4030 os_daemonize();
4032 if (qemu_init_main_loop(&main_loop_err)) {
4033 error_report_err(main_loop_err);
4034 exit(1);
4037 if (qemu_opts_foreach(qemu_find_opts("sandbox"),
4038 parse_sandbox, NULL, NULL)) {
4039 exit(1);
4042 if (qemu_opts_foreach(qemu_find_opts("name"),
4043 parse_name, NULL, NULL)) {
4044 exit(1);
4047 #ifndef _WIN32
4048 if (qemu_opts_foreach(qemu_find_opts("add-fd"),
4049 parse_add_fd, NULL, NULL)) {
4050 exit(1);
4053 if (qemu_opts_foreach(qemu_find_opts("add-fd"),
4054 cleanup_add_fd, NULL, NULL)) {
4055 exit(1);
4057 #endif
4059 current_machine = MACHINE(object_new(object_class_get_name(
4060 OBJECT_CLASS(machine_class))));
4061 if (machine_help_func(qemu_get_machine_opts(), current_machine)) {
4062 exit(0);
4064 object_property_add_child(object_get_root(), "machine",
4065 OBJECT(current_machine), &error_abort);
4066 cpu_exec_init_all();
4068 if (machine_class->hw_version) {
4069 qemu_set_hw_version(machine_class->hw_version);
4072 if (cpu_model && is_help_option(cpu_model)) {
4073 list_cpus(stdout, &fprintf, cpu_model);
4074 exit(0);
4077 if (!trace_init_backends()) {
4078 exit(1);
4080 trace_init_file(trace_file);
4082 /* Open the logfile at this point and set the log mask if necessary.
4084 if (log_file) {
4085 qemu_set_log_filename(log_file, &error_fatal);
4088 if (log_mask) {
4089 int mask;
4090 mask = qemu_str_to_log_mask(log_mask);
4091 if (!mask) {
4092 qemu_print_log_usage(stdout);
4093 exit(1);
4095 qemu_set_log(mask);
4096 } else {
4097 qemu_set_log(0);
4100 /* If no data_dir is specified then try to find it relative to the
4101 executable path. */
4102 if (data_dir_idx < ARRAY_SIZE(data_dir)) {
4103 data_dir[data_dir_idx] = os_find_datadir();
4104 if (data_dir[data_dir_idx] != NULL) {
4105 data_dir_idx++;
4108 /* If all else fails use the install path specified when building. */
4109 if (data_dir_idx < ARRAY_SIZE(data_dir)) {
4110 data_dir[data_dir_idx++] = CONFIG_QEMU_DATADIR;
4113 /* -L help lists the data directories and exits. */
4114 if (list_data_dirs) {
4115 for (i = 0; i < data_dir_idx; i++) {
4116 printf("%s\n", data_dir[i]);
4118 exit(0);
4121 smp_parse(qemu_opts_find(qemu_find_opts("smp-opts"), NULL));
4123 machine_class->max_cpus = machine_class->max_cpus ?: 1; /* Default to UP */
4124 if (max_cpus > machine_class->max_cpus) {
4125 error_report("Number of SMP CPUs requested (%d) exceeds max CPUs "
4126 "supported by machine '%s' (%d)", max_cpus,
4127 machine_class->name, machine_class->max_cpus);
4128 exit(1);
4132 * Get the default machine options from the machine if it is not already
4133 * specified either by the configuration file or by the command line.
4135 if (machine_class->default_machine_opts) {
4136 qemu_opts_set_defaults(qemu_find_opts("machine"),
4137 machine_class->default_machine_opts, 0);
4140 qemu_opts_foreach(qemu_find_opts("device"),
4141 default_driver_check, NULL, NULL);
4142 qemu_opts_foreach(qemu_find_opts("global"),
4143 default_driver_check, NULL, NULL);
4145 if (!vga_model && !default_vga) {
4146 vga_interface_type = VGA_DEVICE;
4148 if (!has_defaults || machine_class->no_serial) {
4149 default_serial = 0;
4151 if (!has_defaults || machine_class->no_parallel) {
4152 default_parallel = 0;
4154 if (!has_defaults || !machine_class->use_virtcon) {
4155 default_virtcon = 0;
4157 if (!has_defaults || !machine_class->use_sclp) {
4158 default_sclp = 0;
4160 if (!has_defaults || machine_class->no_floppy) {
4161 default_floppy = 0;
4163 if (!has_defaults || machine_class->no_cdrom) {
4164 default_cdrom = 0;
4166 if (!has_defaults || machine_class->no_sdcard) {
4167 default_sdcard = 0;
4169 if (!has_defaults) {
4170 default_monitor = 0;
4171 default_net = 0;
4172 default_vga = 0;
4175 if (is_daemonized()) {
4176 /* According to documentation and historically, -nographic redirects
4177 * serial port, parallel port and monitor to stdio, which does not work
4178 * with -daemonize. We can redirect these to null instead, but since
4179 * -nographic is legacy, let's just error out.
4180 * We disallow -nographic only if all other ports are not redirected
4181 * explicitly, to not break existing legacy setups which uses
4182 * -nographic _and_ redirects all ports explicitly - this is valid
4183 * usage, -nographic is just a no-op in this case.
4185 if (nographic
4186 && (default_parallel || default_serial
4187 || default_monitor || default_virtcon)) {
4188 error_report("-nographic cannot be used with -daemonize");
4189 exit(1);
4191 #ifdef CONFIG_CURSES
4192 if (display_type == DT_CURSES) {
4193 error_report("curses display cannot be used with -daemonize");
4194 exit(1);
4196 #endif
4199 if (nographic) {
4200 if (default_parallel)
4201 add_device_config(DEV_PARALLEL, "null");
4202 if (default_serial && default_monitor) {
4203 add_device_config(DEV_SERIAL, "mon:stdio");
4204 } else if (default_virtcon && default_monitor) {
4205 add_device_config(DEV_VIRTCON, "mon:stdio");
4206 } else if (default_sclp && default_monitor) {
4207 add_device_config(DEV_SCLP, "mon:stdio");
4208 } else {
4209 if (default_serial)
4210 add_device_config(DEV_SERIAL, "stdio");
4211 if (default_virtcon)
4212 add_device_config(DEV_VIRTCON, "stdio");
4213 if (default_sclp) {
4214 add_device_config(DEV_SCLP, "stdio");
4216 if (default_monitor)
4217 monitor_parse("stdio", "readline", false);
4219 } else {
4220 if (default_serial)
4221 add_device_config(DEV_SERIAL, "vc:80Cx24C");
4222 if (default_parallel)
4223 add_device_config(DEV_PARALLEL, "vc:80Cx24C");
4224 if (default_monitor)
4225 monitor_parse("vc:80Cx24C", "readline", false);
4226 if (default_virtcon)
4227 add_device_config(DEV_VIRTCON, "vc:80Cx24C");
4228 if (default_sclp) {
4229 add_device_config(DEV_SCLP, "vc:80Cx24C");
4233 #if defined(CONFIG_VNC)
4234 if (!QTAILQ_EMPTY(&(qemu_find_opts("vnc")->head))) {
4235 display_remote++;
4237 #endif
4238 if (display_type == DT_DEFAULT && !display_remote) {
4239 #if defined(CONFIG_GTK)
4240 display_type = DT_GTK;
4241 #elif defined(CONFIG_SDL)
4242 display_type = DT_SDL;
4243 #elif defined(CONFIG_COCOA)
4244 display_type = DT_COCOA;
4245 #elif defined(CONFIG_VNC)
4246 vnc_parse("localhost:0,to=99,id=default", &error_abort);
4247 #else
4248 display_type = DT_NONE;
4249 #endif
4252 if ((no_frame || alt_grab || ctrl_grab) && display_type != DT_SDL) {
4253 error_report("-no-frame, -alt-grab and -ctrl-grab are only valid "
4254 "for SDL, ignoring option");
4256 if (no_quit && (display_type != DT_GTK && display_type != DT_SDL)) {
4257 error_report("-no-quit is only valid for GTK and SDL, "
4258 "ignoring option");
4261 if (display_type == DT_GTK) {
4262 early_gtk_display_init(request_opengl);
4265 if (display_type == DT_SDL) {
4266 sdl_display_early_init(request_opengl);
4269 if (request_opengl == 1 && display_opengl == 0) {
4270 #if defined(CONFIG_OPENGL)
4271 error_report("OpenGL is not supported by the display");
4272 #else
4273 error_report("OpenGL support is disabled");
4274 #endif
4275 exit(1);
4278 page_size_init();
4279 socket_init();
4281 if (qemu_opts_foreach(qemu_find_opts("object"),
4282 user_creatable_add_opts_foreach,
4283 object_create_initial, NULL)) {
4284 exit(1);
4287 if (qemu_opts_foreach(qemu_find_opts("chardev"),
4288 chardev_init_func, NULL, NULL)) {
4289 exit(1);
4292 #ifdef CONFIG_VIRTFS
4293 if (qemu_opts_foreach(qemu_find_opts("fsdev"),
4294 fsdev_init_func, NULL, NULL)) {
4295 exit(1);
4297 #endif
4299 if (pid_file && qemu_create_pidfile(pid_file) != 0) {
4300 error_report("could not acquire pid file: %s", strerror(errno));
4301 exit(1);
4304 if (qemu_opts_foreach(qemu_find_opts("device"),
4305 device_help_func, NULL, NULL)) {
4306 exit(0);
4309 machine_opts = qemu_get_machine_opts();
4310 if (qemu_opt_foreach(machine_opts, machine_set_property, current_machine,
4311 NULL)) {
4312 object_unref(OBJECT(current_machine));
4313 exit(1);
4316 configure_accelerator(current_machine);
4318 if (qtest_chrdev) {
4319 qtest_init(qtest_chrdev, qtest_log, &error_fatal);
4322 machine_opts = qemu_get_machine_opts();
4323 kernel_filename = qemu_opt_get(machine_opts, "kernel");
4324 initrd_filename = qemu_opt_get(machine_opts, "initrd");
4325 kernel_cmdline = qemu_opt_get(machine_opts, "append");
4326 bios_name = qemu_opt_get(machine_opts, "firmware");
4328 opts = qemu_opts_find(qemu_find_opts("boot-opts"), NULL);
4329 if (opts) {
4330 boot_order = qemu_opt_get(opts, "order");
4331 if (boot_order) {
4332 validate_bootdevices(boot_order, &error_fatal);
4335 boot_once = qemu_opt_get(opts, "once");
4336 if (boot_once) {
4337 validate_bootdevices(boot_once, &error_fatal);
4340 boot_menu = qemu_opt_get_bool(opts, "menu", boot_menu);
4341 boot_strict = qemu_opt_get_bool(opts, "strict", false);
4344 if (!boot_order) {
4345 boot_order = machine_class->default_boot_order;
4348 if (!kernel_cmdline) {
4349 kernel_cmdline = "";
4350 current_machine->kernel_cmdline = (char *)kernel_cmdline;
4353 linux_boot = (kernel_filename != NULL);
4355 if (!linux_boot && *kernel_cmdline != '\0') {
4356 error_report("-append only allowed with -kernel option");
4357 exit(1);
4360 if (!linux_boot && initrd_filename != NULL) {
4361 error_report("-initrd only allowed with -kernel option");
4362 exit(1);
4365 if (!linux_boot && qemu_opt_get(machine_opts, "dtb")) {
4366 error_report("-dtb only allowed with -kernel option");
4367 exit(1);
4370 if (semihosting_enabled() && !semihosting_get_argc() && kernel_filename) {
4371 /* fall back to the -kernel/-append */
4372 semihosting_arg_fallback(kernel_filename, kernel_cmdline);
4375 os_set_line_buffering();
4377 /* spice needs the timers to be initialized by this point */
4378 qemu_spice_init();
4380 cpu_ticks_init();
4381 if (icount_opts) {
4382 if (kvm_enabled() || xen_enabled()) {
4383 error_report("-icount is not allowed with kvm or xen");
4384 exit(1);
4386 configure_icount(icount_opts, &error_abort);
4387 qemu_opts_del(icount_opts);
4390 if (default_net) {
4391 QemuOptsList *net = qemu_find_opts("net");
4392 qemu_opts_set(net, NULL, "type", "nic", &error_abort);
4393 #ifdef CONFIG_SLIRP
4394 qemu_opts_set(net, NULL, "type", "user", &error_abort);
4395 #endif
4398 if (net_init_clients() < 0) {
4399 exit(1);
4402 if (qemu_opts_foreach(qemu_find_opts("object"),
4403 user_creatable_add_opts_foreach,
4404 object_create_delayed, NULL)) {
4405 exit(1);
4408 #ifdef CONFIG_TPM
4409 if (tpm_init() < 0) {
4410 exit(1);
4412 #endif
4414 /* init the bluetooth world */
4415 if (foreach_device_config(DEV_BT, bt_parse))
4416 exit(1);
4418 if (!xen_enabled()) {
4419 /* On 32-bit hosts, QEMU is limited by virtual address space */
4420 if (ram_size > (2047 << 20) && HOST_LONG_BITS == 32) {
4421 error_report("at most 2047 MB RAM can be simulated");
4422 exit(1);
4426 blk_mig_init();
4427 ram_mig_init();
4429 /* If the currently selected machine wishes to override the units-per-bus
4430 * property of its default HBA interface type, do so now. */
4431 if (machine_class->units_per_default_bus) {
4432 override_max_devs(machine_class->block_default_type,
4433 machine_class->units_per_default_bus);
4436 /* open the virtual block devices */
4437 if (snapshot || replay_mode != REPLAY_MODE_NONE) {
4438 qemu_opts_foreach(qemu_find_opts("drive"), drive_enable_snapshot,
4439 NULL, NULL);
4441 if (qemu_opts_foreach(qemu_find_opts("drive"), drive_init_func,
4442 &machine_class->block_default_type, NULL)) {
4443 exit(1);
4446 default_drive(default_cdrom, snapshot, machine_class->block_default_type, 2,
4447 CDROM_OPTS);
4448 default_drive(default_floppy, snapshot, IF_FLOPPY, 0, FD_OPTS);
4449 default_drive(default_sdcard, snapshot, IF_SD, 0, SD_OPTS);
4451 parse_numa_opts(machine_class);
4453 if (qemu_opts_foreach(qemu_find_opts("mon"),
4454 mon_init_func, NULL, NULL)) {
4455 exit(1);
4458 if (foreach_device_config(DEV_SERIAL, serial_parse) < 0)
4459 exit(1);
4460 if (foreach_device_config(DEV_PARALLEL, parallel_parse) < 0)
4461 exit(1);
4462 if (foreach_device_config(DEV_VIRTCON, virtcon_parse) < 0)
4463 exit(1);
4464 if (foreach_device_config(DEV_SCLP, sclp_parse) < 0) {
4465 exit(1);
4467 if (foreach_device_config(DEV_DEBUGCON, debugcon_parse) < 0)
4468 exit(1);
4470 /* If no default VGA is requested, the default is "none". */
4471 if (default_vga) {
4472 if (machine_class->default_display) {
4473 vga_model = machine_class->default_display;
4474 } else if (vga_interface_available(VGA_CIRRUS)) {
4475 vga_model = "cirrus";
4476 } else if (vga_interface_available(VGA_STD)) {
4477 vga_model = "std";
4480 if (vga_model) {
4481 select_vgahw(vga_model);
4484 if (watchdog) {
4485 i = select_watchdog(watchdog);
4486 if (i > 0)
4487 exit (i == 1 ? 1 : 0);
4490 machine_register_compat_props(current_machine);
4492 qemu_opts_foreach(qemu_find_opts("global"),
4493 global_init_func, NULL, NULL);
4495 /* This checkpoint is required by replay to separate prior clock
4496 reading from the other reads, because timer polling functions query
4497 clock values from the log. */
4498 replay_checkpoint(CHECKPOINT_INIT);
4499 qdev_machine_init();
4501 current_machine->ram_size = ram_size;
4502 current_machine->maxram_size = maxram_size;
4503 current_machine->ram_slots = ram_slots;
4504 current_machine->boot_order = boot_order;
4505 current_machine->cpu_model = cpu_model;
4507 machine_class->init(current_machine);
4509 realtime_init();
4511 audio_init();
4513 cpu_synchronize_all_post_init();
4515 numa_post_machine_init();
4517 if (qemu_opts_foreach(qemu_find_opts("fw_cfg"),
4518 parse_fw_cfg, fw_cfg_find(), NULL) != 0) {
4519 exit(1);
4522 /* init USB devices */
4523 if (machine_usb(current_machine)) {
4524 if (foreach_device_config(DEV_USB, usb_parse) < 0)
4525 exit(1);
4528 /* Check if IGD GFX passthrough. */
4529 igd_gfx_passthru();
4531 /* init generic devices */
4532 rom_set_order_override(FW_CFG_ORDER_OVERRIDE_DEVICE);
4533 if (qemu_opts_foreach(qemu_find_opts("device"),
4534 device_init_func, NULL, NULL)) {
4535 exit(1);
4537 rom_reset_order_override();
4539 /* Did we create any drives that we failed to create a device for? */
4540 drive_check_orphaned();
4542 /* Don't warn about the default network setup that you get if
4543 * no command line -net or -netdev options are specified. There
4544 * are two cases that we would otherwise complain about:
4545 * (1) board doesn't support a NIC but the implicit "-net nic"
4546 * requested one
4547 * (2) CONFIG_SLIRP not set, in which case the implicit "-net nic"
4548 * sets up a nic that isn't connected to anything.
4550 if (!default_net) {
4551 net_check_clients();
4555 if (boot_once) {
4556 qemu_boot_set(boot_once, &error_fatal);
4557 qemu_register_reset(restore_boot_order, g_strdup(boot_order));
4560 ds = init_displaystate();
4562 /* init local displays */
4563 switch (display_type) {
4564 case DT_CURSES:
4565 curses_display_init(ds, full_screen);
4566 break;
4567 case DT_SDL:
4568 sdl_display_init(ds, full_screen, no_frame);
4569 break;
4570 case DT_COCOA:
4571 cocoa_display_init(ds, full_screen);
4572 break;
4573 case DT_GTK:
4574 gtk_display_init(ds, full_screen, grab_on_hover);
4575 break;
4576 default:
4577 break;
4580 /* must be after terminal init, SDL library changes signal handlers */
4581 os_setup_signal_handling();
4583 /* init remote displays */
4584 #ifdef CONFIG_VNC
4585 qemu_opts_foreach(qemu_find_opts("vnc"),
4586 vnc_init_func, NULL, NULL);
4587 #endif
4589 if (using_spice) {
4590 qemu_spice_display_init();
4593 if (foreach_device_config(DEV_GDB, gdbserver_start) < 0) {
4594 exit(1);
4597 qdev_machine_creation_done();
4599 /* TODO: once all bus devices are qdevified, this should be done
4600 * when bus is created by qdev.c */
4601 qemu_register_reset(qbus_reset_all_fn, sysbus_get_default());
4602 qemu_run_machine_init_done_notifiers();
4604 if (rom_check_and_register_reset() != 0) {
4605 error_report("rom check and register reset failed");
4606 exit(1);
4609 replay_start();
4611 /* This checkpoint is required by replay to separate prior clock
4612 reading from the other reads, because timer polling functions query
4613 clock values from the log. */
4614 replay_checkpoint(CHECKPOINT_RESET);
4615 qemu_system_reset(VMRESET_SILENT);
4616 register_global_state();
4617 if (loadvm) {
4618 if (load_vmstate(loadvm) < 0) {
4619 autostart = 0;
4623 qdev_prop_check_globals();
4624 if (vmstate_dump_file) {
4625 /* dump and exit */
4626 dump_vmstate_json_to_file(vmstate_dump_file);
4627 return 0;
4630 if (incoming) {
4631 Error *local_err = NULL;
4632 qemu_start_incoming_migration(incoming, &local_err);
4633 if (local_err) {
4634 error_reportf_err(local_err, "-incoming %s: ", incoming);
4635 exit(1);
4637 } else if (autostart) {
4638 vm_start();
4641 os_setup_post();
4643 main_loop();
4644 replay_disable_events();
4646 bdrv_close_all();
4647 pause_all_vcpus();
4648 res_free();
4649 #ifdef CONFIG_TPM
4650 tpm_cleanup();
4651 #endif
4653 /* vhost-user must be cleaned up before chardevs. */
4654 net_cleanup();
4655 qemu_chr_cleanup();
4657 return 0;