target-arm: Correct sense of the DCZID DZP bit
[qemu.git] / vl.c
blob2f81384726df445758ed2f0ba19f2216ce45876b
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 <unistd.h>
25 #include <fcntl.h>
26 #include <signal.h>
27 #include <time.h>
28 #include <errno.h>
29 #include <sys/time.h>
31 #include "config-host.h"
33 #ifdef CONFIG_SECCOMP
34 #include "sysemu/seccomp.h"
35 #endif
37 #if defined(CONFIG_VDE)
38 #include <libvdeplug.h>
39 #endif
41 #ifdef CONFIG_SDL
42 #if defined(__APPLE__) || defined(main)
43 #include <SDL.h>
44 int qemu_main(int argc, char **argv, char **envp);
45 int main(int argc, char **argv)
47 return qemu_main(argc, argv, NULL);
49 #undef main
50 #define main qemu_main
51 #endif
52 #endif /* CONFIG_SDL */
54 #ifdef CONFIG_COCOA
55 #undef main
56 #define main qemu_main
57 #endif /* CONFIG_COCOA */
59 #include <glib.h>
61 #include "qemu/sockets.h"
62 #include "hw/hw.h"
63 #include "hw/boards.h"
64 #include "sysemu/accel.h"
65 #include "hw/usb.h"
66 #include "hw/i386/pc.h"
67 #include "hw/isa/isa.h"
68 #include "hw/bt.h"
69 #include "sysemu/watchdog.h"
70 #include "hw/i386/smbios.h"
71 #include "hw/xen/xen.h"
72 #include "hw/qdev.h"
73 #include "hw/loader.h"
74 #include "monitor/qdev.h"
75 #include "sysemu/bt.h"
76 #include "net/net.h"
77 #include "net/slirp.h"
78 #include "monitor/monitor.h"
79 #include "ui/console.h"
80 #include "sysemu/sysemu.h"
81 #include "exec/gdbstub.h"
82 #include "qemu/timer.h"
83 #include "sysemu/char.h"
84 #include "qemu/bitmap.h"
85 #include "sysemu/blockdev.h"
86 #include "hw/block/block.h"
87 #include "migration/block.h"
88 #include "sysemu/tpm.h"
89 #include "sysemu/dma.h"
90 #include "audio/audio.h"
91 #include "migration/migration.h"
92 #include "sysemu/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"
114 #include "qemu/osdep.h"
116 #include "ui/qemu-spice.h"
117 #include "qapi/string-input-visitor.h"
118 #include "qapi/opts-visitor.h"
119 #include "qom/object_interfaces.h"
120 #include "qapi-event.h"
122 #define DEFAULT_RAM_SIZE 128
124 #define MAX_VIRTIO_CONSOLES 1
125 #define MAX_SCLP_CONSOLES 1
127 static const char *data_dir[16];
128 static int data_dir_idx;
129 const char *bios_name = NULL;
130 enum vga_retrace_method vga_retrace_method = VGA_RETRACE_DUMB;
131 DisplayType display_type = DT_DEFAULT;
132 static int display_remote;
133 const char* keyboard_layout = NULL;
134 ram_addr_t ram_size;
135 const char *mem_path = NULL;
136 int mem_prealloc = 0; /* force preallocation of physical target memory */
137 bool enable_mlock = false;
138 int nb_nics;
139 NICInfo nd_table[MAX_NICS];
140 int autostart;
141 static int rtc_utc = 1;
142 static int rtc_date_offset = -1; /* -1 means no change */
143 QEMUClockType rtc_clock;
144 int vga_interface_type = VGA_NONE;
145 static int full_screen = 0;
146 static int no_frame = 0;
147 int no_quit = 0;
148 #ifdef CONFIG_GTK
149 static bool grab_on_hover;
150 #endif
151 CharDriverState *serial_hds[MAX_SERIAL_PORTS];
152 CharDriverState *parallel_hds[MAX_PARALLEL_PORTS];
153 CharDriverState *virtcon_hds[MAX_VIRTIO_CONSOLES];
154 CharDriverState *sclp_hds[MAX_SCLP_CONSOLES];
155 int win2k_install_hack = 0;
156 int singlestep = 0;
157 int smp_cpus = 1;
158 int max_cpus = 0;
159 int smp_cores = 1;
160 int smp_threads = 1;
161 #ifdef CONFIG_VNC
162 const char *vnc_display;
163 #endif
164 int acpi_enabled = 1;
165 int no_hpet = 0;
166 int fd_bootchk = 1;
167 static int no_reboot;
168 int no_shutdown = 0;
169 int cursor_hide = 1;
170 int graphic_rotate = 0;
171 const char *watchdog;
172 QEMUOptionRom option_rom[MAX_OPTION_ROMS];
173 int nb_option_roms;
174 int semihosting_enabled = 0;
175 int old_param = 0;
176 const char *qemu_name;
177 int alt_grab = 0;
178 int ctrl_grab = 0;
179 unsigned int nb_prom_envs = 0;
180 const char *prom_envs[MAX_PROM_ENVS];
181 int boot_menu;
182 bool boot_strict;
183 uint8_t *boot_splash_filedata;
184 size_t boot_splash_filedata_size;
185 uint8_t qemu_extra_params_fw[2];
187 int icount_align_option;
189 int nb_numa_nodes;
190 int max_numa_nodeid;
191 NodeInfo numa_info[MAX_NODES];
193 uint8_t qemu_uuid[16];
194 bool qemu_uuid_set;
196 static QEMUBootSetHandler *boot_set_handler;
197 static void *boot_set_opaque;
199 static NotifierList exit_notifiers =
200 NOTIFIER_LIST_INITIALIZER(exit_notifiers);
202 static NotifierList machine_init_done_notifiers =
203 NOTIFIER_LIST_INITIALIZER(machine_init_done_notifiers);
205 bool xen_allowed;
206 uint32_t xen_domid;
207 enum xen_mode xen_mode = XEN_EMULATE;
209 static int has_defaults = 1;
210 static int default_serial = 1;
211 static int default_parallel = 1;
212 static int default_virtcon = 1;
213 static int default_sclp = 1;
214 static int default_monitor = 1;
215 static int default_floppy = 1;
216 static int default_cdrom = 1;
217 static int default_sdcard = 1;
218 static int default_vga = 1;
220 static struct {
221 const char *driver;
222 int *flag;
223 } default_list[] = {
224 { .driver = "isa-serial", .flag = &default_serial },
225 { .driver = "isa-parallel", .flag = &default_parallel },
226 { .driver = "isa-fdc", .flag = &default_floppy },
227 { .driver = "ide-cd", .flag = &default_cdrom },
228 { .driver = "ide-hd", .flag = &default_cdrom },
229 { .driver = "ide-drive", .flag = &default_cdrom },
230 { .driver = "scsi-cd", .flag = &default_cdrom },
231 { .driver = "virtio-serial-pci", .flag = &default_virtcon },
232 { .driver = "virtio-serial-s390", .flag = &default_virtcon },
233 { .driver = "virtio-serial", .flag = &default_virtcon },
234 { .driver = "VGA", .flag = &default_vga },
235 { .driver = "isa-vga", .flag = &default_vga },
236 { .driver = "cirrus-vga", .flag = &default_vga },
237 { .driver = "isa-cirrus-vga", .flag = &default_vga },
238 { .driver = "vmware-svga", .flag = &default_vga },
239 { .driver = "qxl-vga", .flag = &default_vga },
242 static QemuOptsList qemu_rtc_opts = {
243 .name = "rtc",
244 .head = QTAILQ_HEAD_INITIALIZER(qemu_rtc_opts.head),
245 .desc = {
247 .name = "base",
248 .type = QEMU_OPT_STRING,
250 .name = "clock",
251 .type = QEMU_OPT_STRING,
253 .name = "driftfix",
254 .type = QEMU_OPT_STRING,
256 { /* end of list */ }
260 static QemuOptsList qemu_sandbox_opts = {
261 .name = "sandbox",
262 .implied_opt_name = "enable",
263 .head = QTAILQ_HEAD_INITIALIZER(qemu_sandbox_opts.head),
264 .desc = {
266 .name = "enable",
267 .type = QEMU_OPT_BOOL,
269 { /* end of list */ }
273 static QemuOptsList qemu_trace_opts = {
274 .name = "trace",
275 .implied_opt_name = "trace",
276 .head = QTAILQ_HEAD_INITIALIZER(qemu_trace_opts.head),
277 .desc = {
279 .name = "events",
280 .type = QEMU_OPT_STRING,
282 .name = "file",
283 .type = QEMU_OPT_STRING,
285 { /* end of list */ }
289 static QemuOptsList qemu_option_rom_opts = {
290 .name = "option-rom",
291 .implied_opt_name = "romfile",
292 .head = QTAILQ_HEAD_INITIALIZER(qemu_option_rom_opts.head),
293 .desc = {
295 .name = "bootindex",
296 .type = QEMU_OPT_NUMBER,
297 }, {
298 .name = "romfile",
299 .type = QEMU_OPT_STRING,
301 { /* end of list */ }
305 static QemuOptsList qemu_machine_opts = {
306 .name = "machine",
307 .implied_opt_name = "type",
308 .merge_lists = true,
309 .head = QTAILQ_HEAD_INITIALIZER(qemu_machine_opts.head),
310 .desc = {
312 .name = "type",
313 .type = QEMU_OPT_STRING,
314 .help = "emulated machine"
315 }, {
316 .name = "accel",
317 .type = QEMU_OPT_STRING,
318 .help = "accelerator list",
319 }, {
320 .name = "kernel_irqchip",
321 .type = QEMU_OPT_BOOL,
322 .help = "use KVM in-kernel irqchip",
323 }, {
324 .name = "kvm_shadow_mem",
325 .type = QEMU_OPT_SIZE,
326 .help = "KVM shadow MMU size",
327 }, {
328 .name = "kernel",
329 .type = QEMU_OPT_STRING,
330 .help = "Linux kernel image file",
331 }, {
332 .name = "initrd",
333 .type = QEMU_OPT_STRING,
334 .help = "Linux initial ramdisk file",
335 }, {
336 .name = "append",
337 .type = QEMU_OPT_STRING,
338 .help = "Linux kernel command line",
339 }, {
340 .name = "dtb",
341 .type = QEMU_OPT_STRING,
342 .help = "Linux kernel device tree file",
343 }, {
344 .name = "dumpdtb",
345 .type = QEMU_OPT_STRING,
346 .help = "Dump current dtb to a file and quit",
347 }, {
348 .name = "phandle_start",
349 .type = QEMU_OPT_NUMBER,
350 .help = "The first phandle ID we may generate dynamically",
351 }, {
352 .name = "dt_compatible",
353 .type = QEMU_OPT_STRING,
354 .help = "Overrides the \"compatible\" property of the dt root node",
355 }, {
356 .name = "dump-guest-core",
357 .type = QEMU_OPT_BOOL,
358 .help = "Include guest memory in a core dump",
359 }, {
360 .name = "mem-merge",
361 .type = QEMU_OPT_BOOL,
362 .help = "enable/disable memory merge support",
364 .name = "usb",
365 .type = QEMU_OPT_BOOL,
366 .help = "Set on/off to enable/disable usb",
368 .name = "firmware",
369 .type = QEMU_OPT_STRING,
370 .help = "firmware image",
372 .name = "kvm-type",
373 .type = QEMU_OPT_STRING,
374 .help = "Specifies the KVM virtualization mode (HV, PR)",
376 .name = PC_MACHINE_MAX_RAM_BELOW_4G,
377 .type = QEMU_OPT_SIZE,
378 .help = "maximum ram below the 4G boundary (32bit boundary)",
380 .name = "iommu",
381 .type = QEMU_OPT_BOOL,
382 .help = "Set on/off to enable/disable Intel IOMMU (VT-d)",
384 { /* End of list */ }
388 static QemuOptsList qemu_boot_opts = {
389 .name = "boot-opts",
390 .implied_opt_name = "order",
391 .merge_lists = true,
392 .head = QTAILQ_HEAD_INITIALIZER(qemu_boot_opts.head),
393 .desc = {
395 .name = "order",
396 .type = QEMU_OPT_STRING,
397 }, {
398 .name = "once",
399 .type = QEMU_OPT_STRING,
400 }, {
401 .name = "menu",
402 .type = QEMU_OPT_BOOL,
403 }, {
404 .name = "splash",
405 .type = QEMU_OPT_STRING,
406 }, {
407 .name = "splash-time",
408 .type = QEMU_OPT_STRING,
409 }, {
410 .name = "reboot-timeout",
411 .type = QEMU_OPT_STRING,
412 }, {
413 .name = "strict",
414 .type = QEMU_OPT_BOOL,
416 { /*End of list */ }
420 static QemuOptsList qemu_add_fd_opts = {
421 .name = "add-fd",
422 .head = QTAILQ_HEAD_INITIALIZER(qemu_add_fd_opts.head),
423 .desc = {
425 .name = "fd",
426 .type = QEMU_OPT_NUMBER,
427 .help = "file descriptor of which a duplicate is added to fd set",
429 .name = "set",
430 .type = QEMU_OPT_NUMBER,
431 .help = "ID of the fd set to add fd to",
433 .name = "opaque",
434 .type = QEMU_OPT_STRING,
435 .help = "free-form string used to describe fd",
437 { /* end of list */ }
441 static QemuOptsList qemu_object_opts = {
442 .name = "object",
443 .implied_opt_name = "qom-type",
444 .head = QTAILQ_HEAD_INITIALIZER(qemu_object_opts.head),
445 .desc = {
450 static QemuOptsList qemu_tpmdev_opts = {
451 .name = "tpmdev",
452 .implied_opt_name = "type",
453 .head = QTAILQ_HEAD_INITIALIZER(qemu_tpmdev_opts.head),
454 .desc = {
455 /* options are defined in the TPM backends */
456 { /* end of list */ }
460 static QemuOptsList qemu_realtime_opts = {
461 .name = "realtime",
462 .head = QTAILQ_HEAD_INITIALIZER(qemu_realtime_opts.head),
463 .desc = {
465 .name = "mlock",
466 .type = QEMU_OPT_BOOL,
468 { /* end of list */ }
472 static QemuOptsList qemu_msg_opts = {
473 .name = "msg",
474 .head = QTAILQ_HEAD_INITIALIZER(qemu_msg_opts.head),
475 .desc = {
477 .name = "timestamp",
478 .type = QEMU_OPT_BOOL,
480 { /* end of list */ }
484 static QemuOptsList qemu_name_opts = {
485 .name = "name",
486 .implied_opt_name = "guest",
487 .merge_lists = true,
488 .head = QTAILQ_HEAD_INITIALIZER(qemu_name_opts.head),
489 .desc = {
491 .name = "guest",
492 .type = QEMU_OPT_STRING,
493 .help = "Sets the name of the guest.\n"
494 "This name will be displayed in the SDL window caption.\n"
495 "The name will also be used for the VNC server",
496 }, {
497 .name = "process",
498 .type = QEMU_OPT_STRING,
499 .help = "Sets the name of the QEMU process, as shown in top etc",
500 }, {
501 .name = "debug-threads",
502 .type = QEMU_OPT_BOOL,
503 .help = "When enabled, name the individual threads; defaults off.\n"
504 "NOTE: The thread names are for debugging and not a\n"
505 "stable API.",
507 { /* End of list */ }
511 static QemuOptsList qemu_mem_opts = {
512 .name = "memory",
513 .implied_opt_name = "size",
514 .head = QTAILQ_HEAD_INITIALIZER(qemu_mem_opts.head),
515 .merge_lists = true,
516 .desc = {
518 .name = "size",
519 .type = QEMU_OPT_SIZE,
522 .name = "slots",
523 .type = QEMU_OPT_NUMBER,
526 .name = "maxmem",
527 .type = QEMU_OPT_SIZE,
529 { /* end of list */ }
533 static QemuOptsList qemu_icount_opts = {
534 .name = "icount",
535 .implied_opt_name = "shift",
536 .merge_lists = true,
537 .head = QTAILQ_HEAD_INITIALIZER(qemu_icount_opts.head),
538 .desc = {
540 .name = "shift",
541 .type = QEMU_OPT_STRING,
542 }, {
543 .name = "align",
544 .type = QEMU_OPT_BOOL,
546 { /* end of list */ }
551 * Get machine options
553 * Returns: machine options (never null).
555 QemuOpts *qemu_get_machine_opts(void)
557 return qemu_find_opts_singleton("machine");
560 const char *qemu_get_vm_name(void)
562 return qemu_name;
565 static void res_free(void)
567 if (boot_splash_filedata != NULL) {
568 g_free(boot_splash_filedata);
569 boot_splash_filedata = NULL;
573 static int default_driver_check(QemuOpts *opts, void *opaque)
575 const char *driver = qemu_opt_get(opts, "driver");
576 int i;
578 if (!driver)
579 return 0;
580 for (i = 0; i < ARRAY_SIZE(default_list); i++) {
581 if (strcmp(default_list[i].driver, driver) != 0)
582 continue;
583 *(default_list[i].flag) = 0;
585 return 0;
588 /***********************************************************/
589 /* QEMU state */
591 static RunState current_run_state = RUN_STATE_PRELAUNCH;
593 /* We use RUN_STATE_MAX but any invalid value will do */
594 static RunState vmstop_requested = RUN_STATE_MAX;
595 static QemuMutex vmstop_lock;
597 typedef struct {
598 RunState from;
599 RunState to;
600 } RunStateTransition;
602 static const RunStateTransition runstate_transitions_def[] = {
603 /* from -> to */
604 { RUN_STATE_DEBUG, RUN_STATE_RUNNING },
605 { RUN_STATE_DEBUG, RUN_STATE_FINISH_MIGRATE },
607 { RUN_STATE_INMIGRATE, RUN_STATE_RUNNING },
608 { RUN_STATE_INMIGRATE, RUN_STATE_PAUSED },
610 { RUN_STATE_INTERNAL_ERROR, RUN_STATE_PAUSED },
611 { RUN_STATE_INTERNAL_ERROR, RUN_STATE_FINISH_MIGRATE },
613 { RUN_STATE_IO_ERROR, RUN_STATE_RUNNING },
614 { RUN_STATE_IO_ERROR, RUN_STATE_FINISH_MIGRATE },
616 { RUN_STATE_PAUSED, RUN_STATE_RUNNING },
617 { RUN_STATE_PAUSED, RUN_STATE_FINISH_MIGRATE },
619 { RUN_STATE_POSTMIGRATE, RUN_STATE_RUNNING },
620 { RUN_STATE_POSTMIGRATE, RUN_STATE_FINISH_MIGRATE },
622 { RUN_STATE_PRELAUNCH, RUN_STATE_RUNNING },
623 { RUN_STATE_PRELAUNCH, RUN_STATE_FINISH_MIGRATE },
624 { RUN_STATE_PRELAUNCH, RUN_STATE_INMIGRATE },
626 { RUN_STATE_FINISH_MIGRATE, RUN_STATE_RUNNING },
627 { RUN_STATE_FINISH_MIGRATE, RUN_STATE_POSTMIGRATE },
629 { RUN_STATE_RESTORE_VM, RUN_STATE_RUNNING },
631 { RUN_STATE_RUNNING, RUN_STATE_DEBUG },
632 { RUN_STATE_RUNNING, RUN_STATE_INTERNAL_ERROR },
633 { RUN_STATE_RUNNING, RUN_STATE_IO_ERROR },
634 { RUN_STATE_RUNNING, RUN_STATE_PAUSED },
635 { RUN_STATE_RUNNING, RUN_STATE_FINISH_MIGRATE },
636 { RUN_STATE_RUNNING, RUN_STATE_RESTORE_VM },
637 { RUN_STATE_RUNNING, RUN_STATE_SAVE_VM },
638 { RUN_STATE_RUNNING, RUN_STATE_SHUTDOWN },
639 { RUN_STATE_RUNNING, RUN_STATE_WATCHDOG },
640 { RUN_STATE_RUNNING, RUN_STATE_GUEST_PANICKED },
642 { RUN_STATE_SAVE_VM, RUN_STATE_RUNNING },
644 { RUN_STATE_SHUTDOWN, RUN_STATE_PAUSED },
645 { RUN_STATE_SHUTDOWN, RUN_STATE_FINISH_MIGRATE },
647 { RUN_STATE_DEBUG, RUN_STATE_SUSPENDED },
648 { RUN_STATE_RUNNING, RUN_STATE_SUSPENDED },
649 { RUN_STATE_SUSPENDED, RUN_STATE_RUNNING },
650 { RUN_STATE_SUSPENDED, RUN_STATE_FINISH_MIGRATE },
652 { RUN_STATE_WATCHDOG, RUN_STATE_RUNNING },
653 { RUN_STATE_WATCHDOG, RUN_STATE_FINISH_MIGRATE },
655 { RUN_STATE_GUEST_PANICKED, RUN_STATE_RUNNING },
656 { RUN_STATE_GUEST_PANICKED, RUN_STATE_FINISH_MIGRATE },
658 { RUN_STATE_MAX, RUN_STATE_MAX },
661 static bool runstate_valid_transitions[RUN_STATE_MAX][RUN_STATE_MAX];
663 bool runstate_check(RunState state)
665 return current_run_state == state;
668 static void runstate_init(void)
670 const RunStateTransition *p;
672 memset(&runstate_valid_transitions, 0, sizeof(runstate_valid_transitions));
673 for (p = &runstate_transitions_def[0]; p->from != RUN_STATE_MAX; p++) {
674 runstate_valid_transitions[p->from][p->to] = true;
677 qemu_mutex_init(&vmstop_lock);
680 /* This function will abort() on invalid state transitions */
681 void runstate_set(RunState new_state)
683 assert(new_state < RUN_STATE_MAX);
685 if (!runstate_valid_transitions[current_run_state][new_state]) {
686 fprintf(stderr, "ERROR: invalid runstate transition: '%s' -> '%s'\n",
687 RunState_lookup[current_run_state],
688 RunState_lookup[new_state]);
689 abort();
691 trace_runstate_set(new_state);
692 current_run_state = new_state;
695 int runstate_is_running(void)
697 return runstate_check(RUN_STATE_RUNNING);
700 bool runstate_needs_reset(void)
702 return runstate_check(RUN_STATE_INTERNAL_ERROR) ||
703 runstate_check(RUN_STATE_SHUTDOWN);
706 StatusInfo *qmp_query_status(Error **errp)
708 StatusInfo *info = g_malloc0(sizeof(*info));
710 info->running = runstate_is_running();
711 info->singlestep = singlestep;
712 info->status = current_run_state;
714 return info;
717 static bool qemu_vmstop_requested(RunState *r)
719 qemu_mutex_lock(&vmstop_lock);
720 *r = vmstop_requested;
721 vmstop_requested = RUN_STATE_MAX;
722 qemu_mutex_unlock(&vmstop_lock);
723 return *r < RUN_STATE_MAX;
726 void qemu_system_vmstop_request_prepare(void)
728 qemu_mutex_lock(&vmstop_lock);
731 void qemu_system_vmstop_request(RunState state)
733 vmstop_requested = state;
734 qemu_mutex_unlock(&vmstop_lock);
735 qemu_notify_event();
738 void vm_start(void)
740 RunState requested;
742 qemu_vmstop_requested(&requested);
743 if (runstate_is_running() && requested == RUN_STATE_MAX) {
744 return;
747 /* Ensure that a STOP/RESUME pair of events is emitted if a
748 * vmstop request was pending. The BLOCK_IO_ERROR event, for
749 * example, according to documentation is always followed by
750 * the STOP event.
752 if (runstate_is_running()) {
753 qapi_event_send_stop(&error_abort);
754 } else {
755 cpu_enable_ticks();
756 runstate_set(RUN_STATE_RUNNING);
757 vm_state_notify(1, RUN_STATE_RUNNING);
758 resume_all_vcpus();
761 qapi_event_send_resume(&error_abort);
765 /***********************************************************/
766 /* real time host monotonic timer */
768 /***********************************************************/
769 /* host time/date access */
770 void qemu_get_timedate(struct tm *tm, int offset)
772 time_t ti;
774 time(&ti);
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 - time(NULL);
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 fprintf(stderr, "Invalid date format. Valid formats are:\n"
837 "'2006-06-17T16:01:21' or '2006-06-17'\n");
838 exit(1);
840 rtc_date_offset = time(NULL) - rtc_start_date;
844 static void configure_rtc(QemuOpts *opts)
846 const char *value;
848 value = qemu_opt_get(opts, "base");
849 if (value) {
850 if (!strcmp(value, "utc")) {
851 rtc_utc = 1;
852 } else if (!strcmp(value, "localtime")) {
853 rtc_utc = 0;
854 } else {
855 configure_rtc_date_offset(value, 0);
858 value = qemu_opt_get(opts, "clock");
859 if (value) {
860 if (!strcmp(value, "host")) {
861 rtc_clock = QEMU_CLOCK_HOST;
862 } else if (!strcmp(value, "rt")) {
863 rtc_clock = QEMU_CLOCK_REALTIME;
864 } else if (!strcmp(value, "vm")) {
865 rtc_clock = QEMU_CLOCK_VIRTUAL;
866 } else {
867 fprintf(stderr, "qemu: invalid option value '%s'\n", value);
868 exit(1);
871 value = qemu_opt_get(opts, "driftfix");
872 if (value) {
873 if (!strcmp(value, "slew")) {
874 static GlobalProperty slew_lost_ticks[] = {
876 .driver = "mc146818rtc",
877 .property = "lost_tick_policy",
878 .value = "slew",
880 { /* end of list */ }
883 qdev_prop_register_global_list(slew_lost_ticks);
884 } else if (!strcmp(value, "none")) {
885 /* discard is default */
886 } else {
887 fprintf(stderr, "qemu: invalid option value '%s'\n", value);
888 exit(1);
893 /***********************************************************/
894 /* Bluetooth support */
895 static int nb_hcis;
896 static int cur_hci;
897 static struct HCIInfo *hci_table[MAX_NICS];
899 struct HCIInfo *qemu_next_hci(void)
901 if (cur_hci == nb_hcis)
902 return &null_hci;
904 return hci_table[cur_hci++];
907 static int bt_hci_parse(const char *str)
909 struct HCIInfo *hci;
910 bdaddr_t bdaddr;
912 if (nb_hcis >= MAX_NICS) {
913 fprintf(stderr, "qemu: Too many bluetooth HCIs (max %i).\n", MAX_NICS);
914 return -1;
917 hci = hci_init(str);
918 if (!hci)
919 return -1;
921 bdaddr.b[0] = 0x52;
922 bdaddr.b[1] = 0x54;
923 bdaddr.b[2] = 0x00;
924 bdaddr.b[3] = 0x12;
925 bdaddr.b[4] = 0x34;
926 bdaddr.b[5] = 0x56 + nb_hcis;
927 hci->bdaddr_set(hci, bdaddr.b);
929 hci_table[nb_hcis++] = hci;
931 return 0;
934 static void bt_vhci_add(int vlan_id)
936 struct bt_scatternet_s *vlan = qemu_find_bt_vlan(vlan_id);
938 if (!vlan->slave)
939 fprintf(stderr, "qemu: warning: adding a VHCI to "
940 "an empty scatternet %i\n", vlan_id);
942 bt_vhci_init(bt_new_hci(vlan));
945 static struct bt_device_s *bt_device_add(const char *opt)
947 struct bt_scatternet_s *vlan;
948 int vlan_id = 0;
949 char *endp = strstr(opt, ",vlan=");
950 int len = (endp ? endp - opt : strlen(opt)) + 1;
951 char devname[10];
953 pstrcpy(devname, MIN(sizeof(devname), len), opt);
955 if (endp) {
956 vlan_id = strtol(endp + 6, &endp, 0);
957 if (*endp) {
958 fprintf(stderr, "qemu: unrecognised bluetooth vlan Id\n");
959 return 0;
963 vlan = qemu_find_bt_vlan(vlan_id);
965 if (!vlan->slave)
966 fprintf(stderr, "qemu: warning: adding a slave device to "
967 "an empty scatternet %i\n", vlan_id);
969 if (!strcmp(devname, "keyboard"))
970 return bt_keyboard_init(vlan);
972 fprintf(stderr, "qemu: unsupported bluetooth device `%s'\n", devname);
973 return 0;
976 static int bt_parse(const char *opt)
978 const char *endp, *p;
979 int vlan;
981 if (strstart(opt, "hci", &endp)) {
982 if (!*endp || *endp == ',') {
983 if (*endp)
984 if (!strstart(endp, ",vlan=", 0))
985 opt = endp + 1;
987 return bt_hci_parse(opt);
989 } else if (strstart(opt, "vhci", &endp)) {
990 if (!*endp || *endp == ',') {
991 if (*endp) {
992 if (strstart(endp, ",vlan=", &p)) {
993 vlan = strtol(p, (char **) &endp, 0);
994 if (*endp) {
995 fprintf(stderr, "qemu: bad scatternet '%s'\n", p);
996 return 1;
998 } else {
999 fprintf(stderr, "qemu: bad parameter '%s'\n", endp + 1);
1000 return 1;
1002 } else
1003 vlan = 0;
1005 bt_vhci_add(vlan);
1006 return 0;
1008 } else if (strstart(opt, "device:", &endp))
1009 return !bt_device_add(endp);
1011 fprintf(stderr, "qemu: bad bluetooth parameter '%s'\n", opt);
1012 return 1;
1015 static int parse_sandbox(QemuOpts *opts, void *opaque)
1017 /* FIXME: change this to true for 1.3 */
1018 if (qemu_opt_get_bool(opts, "enable", false)) {
1019 #ifdef CONFIG_SECCOMP
1020 if (seccomp_start() < 0) {
1021 qerror_report(ERROR_CLASS_GENERIC_ERROR,
1022 "failed to install seccomp syscall filter in the kernel");
1023 return -1;
1025 #else
1026 qerror_report(ERROR_CLASS_GENERIC_ERROR,
1027 "sandboxing request but seccomp is not compiled into this build");
1028 return -1;
1029 #endif
1032 return 0;
1035 static int parse_name(QemuOpts *opts, void *opaque)
1037 const char *proc_name;
1039 if (qemu_opt_get(opts, "debug-threads")) {
1040 qemu_thread_naming(qemu_opt_get_bool(opts, "debug-threads", false));
1042 qemu_name = qemu_opt_get(opts, "guest");
1044 proc_name = qemu_opt_get(opts, "process");
1045 if (proc_name) {
1046 os_set_proc_name(proc_name);
1049 return 0;
1052 bool usb_enabled(bool default_usb)
1054 return qemu_opt_get_bool(qemu_get_machine_opts(), "usb",
1055 has_defaults && default_usb);
1058 #ifndef _WIN32
1059 static int parse_add_fd(QemuOpts *opts, void *opaque)
1061 int fd, dupfd, flags;
1062 int64_t fdset_id;
1063 const char *fd_opaque = NULL;
1065 fd = qemu_opt_get_number(opts, "fd", -1);
1066 fdset_id = qemu_opt_get_number(opts, "set", -1);
1067 fd_opaque = qemu_opt_get(opts, "opaque");
1069 if (fd < 0) {
1070 qerror_report(ERROR_CLASS_GENERIC_ERROR,
1071 "fd option is required and must be non-negative");
1072 return -1;
1075 if (fd <= STDERR_FILENO) {
1076 qerror_report(ERROR_CLASS_GENERIC_ERROR,
1077 "fd cannot be a standard I/O stream");
1078 return -1;
1082 * All fds inherited across exec() necessarily have FD_CLOEXEC
1083 * clear, while qemu sets FD_CLOEXEC on all other fds used internally.
1085 flags = fcntl(fd, F_GETFD);
1086 if (flags == -1 || (flags & FD_CLOEXEC)) {
1087 qerror_report(ERROR_CLASS_GENERIC_ERROR,
1088 "fd is not valid or already in use");
1089 return -1;
1092 if (fdset_id < 0) {
1093 qerror_report(ERROR_CLASS_GENERIC_ERROR,
1094 "set option is required and must be non-negative");
1095 return -1;
1098 #ifdef F_DUPFD_CLOEXEC
1099 dupfd = fcntl(fd, F_DUPFD_CLOEXEC, 0);
1100 #else
1101 dupfd = dup(fd);
1102 if (dupfd != -1) {
1103 qemu_set_cloexec(dupfd);
1105 #endif
1106 if (dupfd == -1) {
1107 qerror_report(ERROR_CLASS_GENERIC_ERROR,
1108 "Error duplicating fd: %s", strerror(errno));
1109 return -1;
1112 /* add the duplicate fd, and optionally the opaque string, to the fd set */
1113 monitor_fdset_add_fd(dupfd, true, fdset_id, fd_opaque ? true : false,
1114 fd_opaque, NULL);
1116 return 0;
1119 static int cleanup_add_fd(QemuOpts *opts, void *opaque)
1121 int fd;
1123 fd = qemu_opt_get_number(opts, "fd", -1);
1124 close(fd);
1126 return 0;
1128 #endif
1130 /***********************************************************/
1131 /* QEMU Block devices */
1133 #define HD_OPTS "media=disk"
1134 #define CDROM_OPTS "media=cdrom"
1135 #define FD_OPTS ""
1136 #define PFLASH_OPTS ""
1137 #define MTD_OPTS ""
1138 #define SD_OPTS ""
1140 static int drive_init_func(QemuOpts *opts, void *opaque)
1142 BlockInterfaceType *block_default_type = opaque;
1144 return drive_new(opts, *block_default_type) == NULL;
1147 static int drive_enable_snapshot(QemuOpts *opts, void *opaque)
1149 if (qemu_opt_get(opts, "snapshot") == NULL) {
1150 qemu_opt_set(opts, "snapshot", "on");
1152 return 0;
1155 static void default_drive(int enable, int snapshot, BlockInterfaceType type,
1156 int index, const char *optstr)
1158 QemuOpts *opts;
1159 DriveInfo *dinfo;
1161 if (!enable || drive_get_by_index(type, index)) {
1162 return;
1165 opts = drive_add(type, index, NULL, optstr);
1166 if (snapshot) {
1167 drive_enable_snapshot(opts, NULL);
1170 dinfo = drive_new(opts, type);
1171 if (!dinfo) {
1172 exit(1);
1174 dinfo->is_default = true;
1178 void qemu_register_boot_set(QEMUBootSetHandler *func, void *opaque)
1180 boot_set_handler = func;
1181 boot_set_opaque = opaque;
1184 int qemu_boot_set(const char *boot_order)
1186 if (!boot_set_handler) {
1187 return -EINVAL;
1189 return boot_set_handler(boot_set_opaque, boot_order);
1192 static void validate_bootdevices(const char *devices)
1194 /* We just do some generic consistency checks */
1195 const char *p;
1196 int bitmap = 0;
1198 for (p = devices; *p != '\0'; p++) {
1199 /* Allowed boot devices are:
1200 * a-b: floppy disk drives
1201 * c-f: IDE disk drives
1202 * g-m: machine implementation dependent drives
1203 * n-p: network devices
1204 * It's up to each machine implementation to check if the given boot
1205 * devices match the actual hardware implementation and firmware
1206 * features.
1208 if (*p < 'a' || *p > 'p') {
1209 fprintf(stderr, "Invalid boot device '%c'\n", *p);
1210 exit(1);
1212 if (bitmap & (1 << (*p - 'a'))) {
1213 fprintf(stderr, "Boot device '%c' was given twice\n", *p);
1214 exit(1);
1216 bitmap |= 1 << (*p - 'a');
1220 static void restore_boot_order(void *opaque)
1222 char *normal_boot_order = opaque;
1223 static int first = 1;
1225 /* Restore boot order and remove ourselves after the first boot */
1226 if (first) {
1227 first = 0;
1228 return;
1231 qemu_boot_set(normal_boot_order);
1233 qemu_unregister_reset(restore_boot_order, normal_boot_order);
1234 g_free(normal_boot_order);
1237 static QemuOptsList qemu_smp_opts = {
1238 .name = "smp-opts",
1239 .implied_opt_name = "cpus",
1240 .merge_lists = true,
1241 .head = QTAILQ_HEAD_INITIALIZER(qemu_smp_opts.head),
1242 .desc = {
1244 .name = "cpus",
1245 .type = QEMU_OPT_NUMBER,
1246 }, {
1247 .name = "sockets",
1248 .type = QEMU_OPT_NUMBER,
1249 }, {
1250 .name = "cores",
1251 .type = QEMU_OPT_NUMBER,
1252 }, {
1253 .name = "threads",
1254 .type = QEMU_OPT_NUMBER,
1255 }, {
1256 .name = "maxcpus",
1257 .type = QEMU_OPT_NUMBER,
1259 { /*End of list */ }
1263 static void smp_parse(QemuOpts *opts)
1265 if (opts) {
1267 unsigned cpus = qemu_opt_get_number(opts, "cpus", 0);
1268 unsigned sockets = qemu_opt_get_number(opts, "sockets", 0);
1269 unsigned cores = qemu_opt_get_number(opts, "cores", 0);
1270 unsigned threads = qemu_opt_get_number(opts, "threads", 0);
1272 /* compute missing values, prefer sockets over cores over threads */
1273 if (cpus == 0 || sockets == 0) {
1274 sockets = sockets > 0 ? sockets : 1;
1275 cores = cores > 0 ? cores : 1;
1276 threads = threads > 0 ? threads : 1;
1277 if (cpus == 0) {
1278 cpus = cores * threads * sockets;
1280 } else {
1281 if (cores == 0) {
1282 threads = threads > 0 ? threads : 1;
1283 cores = cpus / (sockets * threads);
1284 } else {
1285 threads = cpus / (cores * sockets);
1289 max_cpus = qemu_opt_get_number(opts, "maxcpus", 0);
1291 smp_cpus = cpus;
1292 smp_cores = cores > 0 ? cores : 1;
1293 smp_threads = threads > 0 ? threads : 1;
1297 if (max_cpus == 0) {
1298 max_cpus = smp_cpus;
1301 if (max_cpus > MAX_CPUMASK_BITS) {
1302 fprintf(stderr, "Unsupported number of maxcpus\n");
1303 exit(1);
1305 if (max_cpus < smp_cpus) {
1306 fprintf(stderr, "maxcpus must be equal to or greater than smp\n");
1307 exit(1);
1312 static void realtime_init(void)
1314 if (enable_mlock) {
1315 if (os_mlock() < 0) {
1316 fprintf(stderr, "qemu: locking memory failed\n");
1317 exit(1);
1323 static void configure_msg(QemuOpts *opts)
1325 enable_timestamp_msg = qemu_opt_get_bool(opts, "timestamp", true);
1328 /***********************************************************/
1329 /* USB devices */
1331 static int usb_device_add(const char *devname)
1333 USBDevice *dev = NULL;
1334 #ifndef CONFIG_LINUX
1335 const char *p;
1336 #endif
1338 if (!usb_enabled(false)) {
1339 return -1;
1342 /* drivers with .usbdevice_name entry in USBDeviceInfo */
1343 dev = usbdevice_create(devname);
1344 if (dev)
1345 goto done;
1347 /* the other ones */
1348 #ifndef CONFIG_LINUX
1349 /* only the linux version is qdev-ified, usb-bsd still needs this */
1350 if (strstart(devname, "host:", &p)) {
1351 dev = usb_host_device_open(usb_bus_find(-1), p);
1353 #endif
1354 if (!dev)
1355 return -1;
1357 done:
1358 return 0;
1361 static int usb_device_del(const char *devname)
1363 int bus_num, addr;
1364 const char *p;
1366 if (strstart(devname, "host:", &p)) {
1367 return -1;
1370 if (!usb_enabled(false)) {
1371 return -1;
1374 p = strchr(devname, '.');
1375 if (!p)
1376 return -1;
1377 bus_num = strtoul(devname, NULL, 0);
1378 addr = strtoul(p + 1, NULL, 0);
1380 return usb_device_delete_addr(bus_num, addr);
1383 static int usb_parse(const char *cmdline)
1385 int r;
1386 r = usb_device_add(cmdline);
1387 if (r < 0) {
1388 fprintf(stderr, "qemu: could not add USB device '%s'\n", cmdline);
1390 return r;
1393 void do_usb_add(Monitor *mon, const QDict *qdict)
1395 const char *devname = qdict_get_str(qdict, "devname");
1396 if (usb_device_add(devname) < 0) {
1397 error_report("could not add USB device '%s'", devname);
1401 void do_usb_del(Monitor *mon, const QDict *qdict)
1403 const char *devname = qdict_get_str(qdict, "devname");
1404 if (usb_device_del(devname) < 0) {
1405 error_report("could not delete USB device '%s'", devname);
1409 /***********************************************************/
1410 /* machine registration */
1412 MachineState *current_machine;
1414 static void machine_class_init(ObjectClass *oc, void *data)
1416 MachineClass *mc = MACHINE_CLASS(oc);
1417 QEMUMachine *qm = data;
1419 mc->name = qm->name;
1420 mc->alias = qm->alias;
1421 mc->desc = qm->desc;
1422 mc->init = qm->init;
1423 mc->reset = qm->reset;
1424 mc->hot_add_cpu = qm->hot_add_cpu;
1425 mc->kvm_type = qm->kvm_type;
1426 mc->block_default_type = qm->block_default_type;
1427 mc->units_per_default_bus = qm->units_per_default_bus;
1428 mc->max_cpus = qm->max_cpus;
1429 mc->no_serial = qm->no_serial;
1430 mc->no_parallel = qm->no_parallel;
1431 mc->use_virtcon = qm->use_virtcon;
1432 mc->use_sclp = qm->use_sclp;
1433 mc->no_floppy = qm->no_floppy;
1434 mc->no_cdrom = qm->no_cdrom;
1435 mc->no_sdcard = qm->no_sdcard;
1436 mc->is_default = qm->is_default;
1437 mc->default_machine_opts = qm->default_machine_opts;
1438 mc->default_boot_order = qm->default_boot_order;
1439 mc->compat_props = qm->compat_props;
1440 mc->hw_version = qm->hw_version;
1443 int qemu_register_machine(QEMUMachine *m)
1445 char *name = g_strconcat(m->name, TYPE_MACHINE_SUFFIX, NULL);
1446 TypeInfo ti = {
1447 .name = name,
1448 .parent = TYPE_MACHINE,
1449 .class_init = machine_class_init,
1450 .class_data = (void *)m,
1453 type_register(&ti);
1454 g_free(name);
1456 return 0;
1459 static MachineClass *find_machine(const char *name)
1461 GSList *el, *machines = object_class_get_list(TYPE_MACHINE, false);
1462 MachineClass *mc = NULL;
1464 for (el = machines; el; el = el->next) {
1465 MachineClass *temp = el->data;
1467 if (!strcmp(temp->name, name)) {
1468 mc = temp;
1469 break;
1471 if (temp->alias &&
1472 !strcmp(temp->alias, name)) {
1473 mc = temp;
1474 break;
1478 g_slist_free(machines);
1479 return mc;
1482 MachineClass *find_default_machine(void)
1484 GSList *el, *machines = object_class_get_list(TYPE_MACHINE, false);
1485 MachineClass *mc = NULL;
1487 for (el = machines; el; el = el->next) {
1488 MachineClass *temp = el->data;
1490 if (temp->is_default) {
1491 mc = temp;
1492 break;
1496 g_slist_free(machines);
1497 return mc;
1500 MachineInfoList *qmp_query_machines(Error **errp)
1502 GSList *el, *machines = object_class_get_list(TYPE_MACHINE, false);
1503 MachineInfoList *mach_list = NULL;
1505 for (el = machines; el; el = el->next) {
1506 MachineClass *mc = el->data;
1507 MachineInfoList *entry;
1508 MachineInfo *info;
1510 info = g_malloc0(sizeof(*info));
1511 if (mc->is_default) {
1512 info->has_is_default = true;
1513 info->is_default = true;
1516 if (mc->alias) {
1517 info->has_alias = true;
1518 info->alias = g_strdup(mc->alias);
1521 info->name = g_strdup(mc->name);
1522 info->cpu_max = !mc->max_cpus ? 1 : mc->max_cpus;
1524 entry = g_malloc0(sizeof(*entry));
1525 entry->value = info;
1526 entry->next = mach_list;
1527 mach_list = entry;
1530 g_slist_free(machines);
1531 return mach_list;
1534 /***********************************************************/
1535 /* main execution loop */
1537 struct vm_change_state_entry {
1538 VMChangeStateHandler *cb;
1539 void *opaque;
1540 QLIST_ENTRY (vm_change_state_entry) entries;
1543 static QLIST_HEAD(vm_change_state_head, vm_change_state_entry) vm_change_state_head;
1545 VMChangeStateEntry *qemu_add_vm_change_state_handler(VMChangeStateHandler *cb,
1546 void *opaque)
1548 VMChangeStateEntry *e;
1550 e = g_malloc0(sizeof (*e));
1552 e->cb = cb;
1553 e->opaque = opaque;
1554 QLIST_INSERT_HEAD(&vm_change_state_head, e, entries);
1555 return e;
1558 void qemu_del_vm_change_state_handler(VMChangeStateEntry *e)
1560 QLIST_REMOVE (e, entries);
1561 g_free (e);
1564 void vm_state_notify(int running, RunState state)
1566 VMChangeStateEntry *e, *next;
1568 trace_vm_state_notify(running, state);
1570 QLIST_FOREACH_SAFE(e, &vm_change_state_head, entries, next) {
1571 e->cb(e->opaque, running, state);
1575 /* reset/shutdown handler */
1577 typedef struct QEMUResetEntry {
1578 QTAILQ_ENTRY(QEMUResetEntry) entry;
1579 QEMUResetHandler *func;
1580 void *opaque;
1581 } QEMUResetEntry;
1583 static QTAILQ_HEAD(reset_handlers, QEMUResetEntry) reset_handlers =
1584 QTAILQ_HEAD_INITIALIZER(reset_handlers);
1585 static int reset_requested;
1586 static int shutdown_requested, shutdown_signal = -1;
1587 static pid_t shutdown_pid;
1588 static int powerdown_requested;
1589 static int debug_requested;
1590 static int suspend_requested;
1591 static WakeupReason wakeup_reason;
1592 static NotifierList powerdown_notifiers =
1593 NOTIFIER_LIST_INITIALIZER(powerdown_notifiers);
1594 static NotifierList suspend_notifiers =
1595 NOTIFIER_LIST_INITIALIZER(suspend_notifiers);
1596 static NotifierList wakeup_notifiers =
1597 NOTIFIER_LIST_INITIALIZER(wakeup_notifiers);
1598 static uint32_t wakeup_reason_mask = ~(1 << QEMU_WAKEUP_REASON_NONE);
1600 int qemu_shutdown_requested_get(void)
1602 return shutdown_requested;
1605 int qemu_reset_requested_get(void)
1607 return reset_requested;
1610 static int qemu_shutdown_requested(void)
1612 int r = shutdown_requested;
1613 shutdown_requested = 0;
1614 return r;
1617 static void qemu_kill_report(void)
1619 if (!qtest_driver() && shutdown_signal != -1) {
1620 fprintf(stderr, "qemu: terminating on signal %d", shutdown_signal);
1621 if (shutdown_pid == 0) {
1622 /* This happens for eg ^C at the terminal, so it's worth
1623 * avoiding printing an odd message in that case.
1625 fputc('\n', stderr);
1626 } else {
1627 fprintf(stderr, " from pid " FMT_pid "\n", shutdown_pid);
1629 shutdown_signal = -1;
1633 static int qemu_reset_requested(void)
1635 int r = reset_requested;
1636 reset_requested = 0;
1637 return r;
1640 static int qemu_suspend_requested(void)
1642 int r = suspend_requested;
1643 suspend_requested = 0;
1644 return r;
1647 static WakeupReason qemu_wakeup_requested(void)
1649 return wakeup_reason;
1652 static int qemu_powerdown_requested(void)
1654 int r = powerdown_requested;
1655 powerdown_requested = 0;
1656 return r;
1659 static int qemu_debug_requested(void)
1661 int r = debug_requested;
1662 debug_requested = 0;
1663 return r;
1666 void qemu_register_reset(QEMUResetHandler *func, void *opaque)
1668 QEMUResetEntry *re = g_malloc0(sizeof(QEMUResetEntry));
1670 re->func = func;
1671 re->opaque = opaque;
1672 QTAILQ_INSERT_TAIL(&reset_handlers, re, entry);
1675 void qemu_unregister_reset(QEMUResetHandler *func, void *opaque)
1677 QEMUResetEntry *re;
1679 QTAILQ_FOREACH(re, &reset_handlers, entry) {
1680 if (re->func == func && re->opaque == opaque) {
1681 QTAILQ_REMOVE(&reset_handlers, re, entry);
1682 g_free(re);
1683 return;
1688 void qemu_devices_reset(void)
1690 QEMUResetEntry *re, *nre;
1692 /* reset all devices */
1693 QTAILQ_FOREACH_SAFE(re, &reset_handlers, entry, nre) {
1694 re->func(re->opaque);
1698 void qemu_system_reset(bool report)
1700 MachineClass *mc;
1702 mc = current_machine ? MACHINE_GET_CLASS(current_machine) : NULL;
1704 if (mc && mc->reset) {
1705 mc->reset();
1706 } else {
1707 qemu_devices_reset();
1709 if (report) {
1710 qapi_event_send_reset(&error_abort);
1712 cpu_synchronize_all_post_reset();
1715 void qemu_system_reset_request(void)
1717 if (no_reboot) {
1718 shutdown_requested = 1;
1719 } else {
1720 reset_requested = 1;
1722 cpu_stop_current();
1723 qemu_notify_event();
1726 static void qemu_system_suspend(void)
1728 pause_all_vcpus();
1729 notifier_list_notify(&suspend_notifiers, NULL);
1730 runstate_set(RUN_STATE_SUSPENDED);
1731 qapi_event_send_suspend(&error_abort);
1734 void qemu_system_suspend_request(void)
1736 if (runstate_check(RUN_STATE_SUSPENDED)) {
1737 return;
1739 suspend_requested = 1;
1740 cpu_stop_current();
1741 qemu_notify_event();
1744 void qemu_register_suspend_notifier(Notifier *notifier)
1746 notifier_list_add(&suspend_notifiers, notifier);
1749 void qemu_system_wakeup_request(WakeupReason reason)
1751 trace_system_wakeup_request(reason);
1753 if (!runstate_check(RUN_STATE_SUSPENDED)) {
1754 return;
1756 if (!(wakeup_reason_mask & (1 << reason))) {
1757 return;
1759 runstate_set(RUN_STATE_RUNNING);
1760 wakeup_reason = reason;
1761 qemu_notify_event();
1764 void qemu_system_wakeup_enable(WakeupReason reason, bool enabled)
1766 if (enabled) {
1767 wakeup_reason_mask |= (1 << reason);
1768 } else {
1769 wakeup_reason_mask &= ~(1 << reason);
1773 void qemu_register_wakeup_notifier(Notifier *notifier)
1775 notifier_list_add(&wakeup_notifiers, notifier);
1778 void qemu_system_killed(int signal, pid_t pid)
1780 shutdown_signal = signal;
1781 shutdown_pid = pid;
1782 no_shutdown = 0;
1783 qemu_system_shutdown_request();
1786 void qemu_system_shutdown_request(void)
1788 trace_qemu_system_shutdown_request();
1789 shutdown_requested = 1;
1790 qemu_notify_event();
1793 static void qemu_system_powerdown(void)
1795 qapi_event_send_powerdown(&error_abort);
1796 notifier_list_notify(&powerdown_notifiers, NULL);
1799 void qemu_system_powerdown_request(void)
1801 trace_qemu_system_powerdown_request();
1802 powerdown_requested = 1;
1803 qemu_notify_event();
1806 void qemu_register_powerdown_notifier(Notifier *notifier)
1808 notifier_list_add(&powerdown_notifiers, notifier);
1811 void qemu_system_debug_request(void)
1813 debug_requested = 1;
1814 qemu_notify_event();
1817 static bool main_loop_should_exit(void)
1819 RunState r;
1820 if (qemu_debug_requested()) {
1821 vm_stop(RUN_STATE_DEBUG);
1823 if (qemu_suspend_requested()) {
1824 qemu_system_suspend();
1826 if (qemu_shutdown_requested()) {
1827 qemu_kill_report();
1828 qapi_event_send_shutdown(&error_abort);
1829 if (no_shutdown) {
1830 vm_stop(RUN_STATE_SHUTDOWN);
1831 } else {
1832 return true;
1835 if (qemu_reset_requested()) {
1836 pause_all_vcpus();
1837 cpu_synchronize_all_states();
1838 qemu_system_reset(VMRESET_REPORT);
1839 resume_all_vcpus();
1840 if (runstate_needs_reset()) {
1841 runstate_set(RUN_STATE_PAUSED);
1844 if (qemu_wakeup_requested()) {
1845 pause_all_vcpus();
1846 cpu_synchronize_all_states();
1847 qemu_system_reset(VMRESET_SILENT);
1848 notifier_list_notify(&wakeup_notifiers, &wakeup_reason);
1849 wakeup_reason = QEMU_WAKEUP_REASON_NONE;
1850 resume_all_vcpus();
1851 qapi_event_send_wakeup(&error_abort);
1853 if (qemu_powerdown_requested()) {
1854 qemu_system_powerdown();
1856 if (qemu_vmstop_requested(&r)) {
1857 vm_stop(r);
1859 return false;
1862 static void main_loop(void)
1864 bool nonblocking;
1865 int last_io = 0;
1866 #ifdef CONFIG_PROFILER
1867 int64_t ti;
1868 #endif
1869 do {
1870 nonblocking = !kvm_enabled() && !xen_enabled() && last_io > 0;
1871 #ifdef CONFIG_PROFILER
1872 ti = profile_getclock();
1873 #endif
1874 last_io = main_loop_wait(nonblocking);
1875 #ifdef CONFIG_PROFILER
1876 dev_time += profile_getclock() - ti;
1877 #endif
1878 } while (!main_loop_should_exit());
1881 static void version(void)
1883 printf("QEMU emulator version " QEMU_VERSION QEMU_PKGVERSION ", Copyright (c) 2003-2008 Fabrice Bellard\n");
1886 static void help(int exitcode)
1888 version();
1889 printf("usage: %s [options] [disk_image]\n\n"
1890 "'disk_image' is a raw hard disk image for IDE hard disk 0\n\n",
1891 error_get_progname());
1893 #define QEMU_OPTIONS_GENERATE_HELP
1894 #include "qemu-options-wrapper.h"
1896 printf("\nDuring emulation, the following keys are useful:\n"
1897 "ctrl-alt-f toggle full screen\n"
1898 "ctrl-alt-n switch to virtual console 'n'\n"
1899 "ctrl-alt toggle mouse and keyboard grab\n"
1900 "\n"
1901 "When using -nographic, press 'ctrl-a h' to get some help.\n");
1903 exit(exitcode);
1906 #define HAS_ARG 0x0001
1908 typedef struct QEMUOption {
1909 const char *name;
1910 int flags;
1911 int index;
1912 uint32_t arch_mask;
1913 } QEMUOption;
1915 static const QEMUOption qemu_options[] = {
1916 { "h", 0, QEMU_OPTION_h, QEMU_ARCH_ALL },
1917 #define QEMU_OPTIONS_GENERATE_OPTIONS
1918 #include "qemu-options-wrapper.h"
1919 { NULL },
1922 static bool vga_available(void)
1924 return object_class_by_name("VGA") || object_class_by_name("isa-vga");
1927 static bool cirrus_vga_available(void)
1929 return object_class_by_name("cirrus-vga")
1930 || object_class_by_name("isa-cirrus-vga");
1933 static bool vmware_vga_available(void)
1935 return object_class_by_name("vmware-svga");
1938 static bool qxl_vga_available(void)
1940 return object_class_by_name("qxl-vga");
1943 static bool tcx_vga_available(void)
1945 return object_class_by_name("SUNW,tcx");
1948 static bool cg3_vga_available(void)
1950 return object_class_by_name("cgthree");
1953 static void select_vgahw (const char *p)
1955 const char *opts;
1957 assert(vga_interface_type == VGA_NONE);
1958 if (strstart(p, "std", &opts)) {
1959 if (vga_available()) {
1960 vga_interface_type = VGA_STD;
1961 } else {
1962 fprintf(stderr, "Error: standard VGA not available\n");
1963 exit(0);
1965 } else if (strstart(p, "cirrus", &opts)) {
1966 if (cirrus_vga_available()) {
1967 vga_interface_type = VGA_CIRRUS;
1968 } else {
1969 fprintf(stderr, "Error: Cirrus VGA not available\n");
1970 exit(0);
1972 } else if (strstart(p, "vmware", &opts)) {
1973 if (vmware_vga_available()) {
1974 vga_interface_type = VGA_VMWARE;
1975 } else {
1976 fprintf(stderr, "Error: VMWare SVGA not available\n");
1977 exit(0);
1979 } else if (strstart(p, "xenfb", &opts)) {
1980 vga_interface_type = VGA_XENFB;
1981 } else if (strstart(p, "qxl", &opts)) {
1982 if (qxl_vga_available()) {
1983 vga_interface_type = VGA_QXL;
1984 } else {
1985 fprintf(stderr, "Error: QXL VGA not available\n");
1986 exit(0);
1988 } else if (strstart(p, "tcx", &opts)) {
1989 if (tcx_vga_available()) {
1990 vga_interface_type = VGA_TCX;
1991 } else {
1992 fprintf(stderr, "Error: TCX framebuffer not available\n");
1993 exit(0);
1995 } else if (strstart(p, "cg3", &opts)) {
1996 if (cg3_vga_available()) {
1997 vga_interface_type = VGA_CG3;
1998 } else {
1999 fprintf(stderr, "Error: CG3 framebuffer not available\n");
2000 exit(0);
2002 } else if (!strstart(p, "none", &opts)) {
2003 invalid_vga:
2004 fprintf(stderr, "Unknown vga type: %s\n", p);
2005 exit(1);
2007 while (*opts) {
2008 const char *nextopt;
2010 if (strstart(opts, ",retrace=", &nextopt)) {
2011 opts = nextopt;
2012 if (strstart(opts, "dumb", &nextopt))
2013 vga_retrace_method = VGA_RETRACE_DUMB;
2014 else if (strstart(opts, "precise", &nextopt))
2015 vga_retrace_method = VGA_RETRACE_PRECISE;
2016 else goto invalid_vga;
2017 } else goto invalid_vga;
2018 opts = nextopt;
2022 static DisplayType select_display(const char *p)
2024 const char *opts;
2025 DisplayType display = DT_DEFAULT;
2027 if (strstart(p, "sdl", &opts)) {
2028 #ifdef CONFIG_SDL
2029 display = DT_SDL;
2030 while (*opts) {
2031 const char *nextopt;
2033 if (strstart(opts, ",frame=", &nextopt)) {
2034 opts = nextopt;
2035 if (strstart(opts, "on", &nextopt)) {
2036 no_frame = 0;
2037 } else if (strstart(opts, "off", &nextopt)) {
2038 no_frame = 1;
2039 } else {
2040 goto invalid_sdl_args;
2042 } else if (strstart(opts, ",alt_grab=", &nextopt)) {
2043 opts = nextopt;
2044 if (strstart(opts, "on", &nextopt)) {
2045 alt_grab = 1;
2046 } else if (strstart(opts, "off", &nextopt)) {
2047 alt_grab = 0;
2048 } else {
2049 goto invalid_sdl_args;
2051 } else if (strstart(opts, ",ctrl_grab=", &nextopt)) {
2052 opts = nextopt;
2053 if (strstart(opts, "on", &nextopt)) {
2054 ctrl_grab = 1;
2055 } else if (strstart(opts, "off", &nextopt)) {
2056 ctrl_grab = 0;
2057 } else {
2058 goto invalid_sdl_args;
2060 } else if (strstart(opts, ",window_close=", &nextopt)) {
2061 opts = nextopt;
2062 if (strstart(opts, "on", &nextopt)) {
2063 no_quit = 0;
2064 } else if (strstart(opts, "off", &nextopt)) {
2065 no_quit = 1;
2066 } else {
2067 goto invalid_sdl_args;
2069 } else {
2070 invalid_sdl_args:
2071 fprintf(stderr, "Invalid SDL option string: %s\n", p);
2072 exit(1);
2074 opts = nextopt;
2076 #else
2077 fprintf(stderr, "SDL support is disabled\n");
2078 exit(1);
2079 #endif
2080 } else if (strstart(p, "vnc", &opts)) {
2081 #ifdef CONFIG_VNC
2082 display_remote++;
2084 if (*opts) {
2085 const char *nextopt;
2087 if (strstart(opts, "=", &nextopt)) {
2088 vnc_display = nextopt;
2091 if (!vnc_display) {
2092 fprintf(stderr, "VNC requires a display argument vnc=<display>\n");
2093 exit(1);
2095 #else
2096 fprintf(stderr, "VNC support is disabled\n");
2097 exit(1);
2098 #endif
2099 } else if (strstart(p, "curses", &opts)) {
2100 #ifdef CONFIG_CURSES
2101 display = DT_CURSES;
2102 #else
2103 fprintf(stderr, "Curses support is disabled\n");
2104 exit(1);
2105 #endif
2106 } else if (strstart(p, "gtk", &opts)) {
2107 #ifdef CONFIG_GTK
2108 display = DT_GTK;
2109 while (*opts) {
2110 const char *nextopt;
2112 if (strstart(opts, ",grab_on_hover=", &nextopt)) {
2113 opts = nextopt;
2114 if (strstart(opts, "on", &nextopt)) {
2115 grab_on_hover = true;
2116 } else if (strstart(opts, "off", &nextopt)) {
2117 grab_on_hover = false;
2118 } else {
2119 goto invalid_gtk_args;
2121 } else {
2122 invalid_gtk_args:
2123 fprintf(stderr, "Invalid GTK option string: %s\n", p);
2124 exit(1);
2126 opts = nextopt;
2128 #else
2129 fprintf(stderr, "GTK support is disabled\n");
2130 exit(1);
2131 #endif
2132 } else if (strstart(p, "none", &opts)) {
2133 display = DT_NONE;
2134 } else {
2135 fprintf(stderr, "Unknown display type: %s\n", p);
2136 exit(1);
2139 return display;
2142 static int balloon_parse(const char *arg)
2144 QemuOpts *opts;
2146 if (strcmp(arg, "none") == 0) {
2147 return 0;
2150 if (!strncmp(arg, "virtio", 6)) {
2151 if (arg[6] == ',') {
2152 /* have params -> parse them */
2153 opts = qemu_opts_parse(qemu_find_opts("device"), arg+7, 0);
2154 if (!opts)
2155 return -1;
2156 } else {
2157 /* create empty opts */
2158 opts = qemu_opts_create(qemu_find_opts("device"), NULL, 0,
2159 &error_abort);
2161 qemu_opt_set(opts, "driver", "virtio-balloon");
2162 return 0;
2165 return -1;
2168 char *qemu_find_file(int type, const char *name)
2170 int i;
2171 const char *subdir;
2172 char *buf;
2174 /* Try the name as a straight path first */
2175 if (access(name, R_OK) == 0) {
2176 trace_load_file(name, name);
2177 return g_strdup(name);
2180 switch (type) {
2181 case QEMU_FILE_TYPE_BIOS:
2182 subdir = "";
2183 break;
2184 case QEMU_FILE_TYPE_KEYMAP:
2185 subdir = "keymaps/";
2186 break;
2187 default:
2188 abort();
2191 for (i = 0; i < data_dir_idx; i++) {
2192 buf = g_strdup_printf("%s/%s%s", data_dir[i], subdir, name);
2193 if (access(buf, R_OK) == 0) {
2194 trace_load_file(name, buf);
2195 return buf;
2197 g_free(buf);
2199 return NULL;
2202 static int device_help_func(QemuOpts *opts, void *opaque)
2204 return qdev_device_help(opts);
2207 static int device_init_func(QemuOpts *opts, void *opaque)
2209 DeviceState *dev;
2211 dev = qdev_device_add(opts);
2212 if (!dev)
2213 return -1;
2214 object_unref(OBJECT(dev));
2215 return 0;
2218 static int chardev_init_func(QemuOpts *opts, void *opaque)
2220 Error *local_err = NULL;
2222 qemu_chr_new_from_opts(opts, NULL, &local_err);
2223 if (local_err) {
2224 error_report("%s", error_get_pretty(local_err));
2225 error_free(local_err);
2226 return -1;
2228 return 0;
2231 #ifdef CONFIG_VIRTFS
2232 static int fsdev_init_func(QemuOpts *opts, void *opaque)
2234 int ret;
2235 ret = qemu_fsdev_add(opts);
2237 return ret;
2239 #endif
2241 static int mon_init_func(QemuOpts *opts, void *opaque)
2243 CharDriverState *chr;
2244 const char *chardev;
2245 const char *mode;
2246 int flags;
2248 mode = qemu_opt_get(opts, "mode");
2249 if (mode == NULL) {
2250 mode = "readline";
2252 if (strcmp(mode, "readline") == 0) {
2253 flags = MONITOR_USE_READLINE;
2254 } else if (strcmp(mode, "control") == 0) {
2255 flags = MONITOR_USE_CONTROL;
2256 } else {
2257 fprintf(stderr, "unknown monitor mode \"%s\"\n", mode);
2258 exit(1);
2261 if (qemu_opt_get_bool(opts, "pretty", 0))
2262 flags |= MONITOR_USE_PRETTY;
2264 if (qemu_opt_get_bool(opts, "default", 0))
2265 flags |= MONITOR_IS_DEFAULT;
2267 chardev = qemu_opt_get(opts, "chardev");
2268 chr = qemu_chr_find(chardev);
2269 if (chr == NULL) {
2270 fprintf(stderr, "chardev \"%s\" not found\n", chardev);
2271 exit(1);
2274 qemu_chr_fe_claim_no_fail(chr);
2275 monitor_init(chr, flags);
2276 return 0;
2279 static void monitor_parse(const char *optarg, const char *mode)
2281 static int monitor_device_index = 0;
2282 QemuOpts *opts;
2283 const char *p;
2284 char label[32];
2285 int def = 0;
2287 if (strstart(optarg, "chardev:", &p)) {
2288 snprintf(label, sizeof(label), "%s", p);
2289 } else {
2290 snprintf(label, sizeof(label), "compat_monitor%d",
2291 monitor_device_index);
2292 if (monitor_device_index == 0) {
2293 def = 1;
2295 opts = qemu_chr_parse_compat(label, optarg);
2296 if (!opts) {
2297 fprintf(stderr, "parse error: %s\n", optarg);
2298 exit(1);
2302 opts = qemu_opts_create(qemu_find_opts("mon"), label, 1, NULL);
2303 if (!opts) {
2304 fprintf(stderr, "duplicate chardev: %s\n", label);
2305 exit(1);
2307 qemu_opt_set(opts, "mode", mode);
2308 qemu_opt_set(opts, "chardev", label);
2309 if (def)
2310 qemu_opt_set(opts, "default", "on");
2311 monitor_device_index++;
2314 struct device_config {
2315 enum {
2316 DEV_USB, /* -usbdevice */
2317 DEV_BT, /* -bt */
2318 DEV_SERIAL, /* -serial */
2319 DEV_PARALLEL, /* -parallel */
2320 DEV_VIRTCON, /* -virtioconsole */
2321 DEV_DEBUGCON, /* -debugcon */
2322 DEV_GDB, /* -gdb, -s */
2323 DEV_SCLP, /* s390 sclp */
2324 } type;
2325 const char *cmdline;
2326 Location loc;
2327 QTAILQ_ENTRY(device_config) next;
2330 static QTAILQ_HEAD(, device_config) device_configs =
2331 QTAILQ_HEAD_INITIALIZER(device_configs);
2333 static void add_device_config(int type, const char *cmdline)
2335 struct device_config *conf;
2337 conf = g_malloc0(sizeof(*conf));
2338 conf->type = type;
2339 conf->cmdline = cmdline;
2340 loc_save(&conf->loc);
2341 QTAILQ_INSERT_TAIL(&device_configs, conf, next);
2344 static int foreach_device_config(int type, int (*func)(const char *cmdline))
2346 struct device_config *conf;
2347 int rc;
2349 QTAILQ_FOREACH(conf, &device_configs, next) {
2350 if (conf->type != type)
2351 continue;
2352 loc_push_restore(&conf->loc);
2353 rc = func(conf->cmdline);
2354 loc_pop(&conf->loc);
2355 if (rc) {
2356 return rc;
2359 return 0;
2362 static int serial_parse(const char *devname)
2364 static int index = 0;
2365 char label[32];
2367 if (strcmp(devname, "none") == 0)
2368 return 0;
2369 if (index == MAX_SERIAL_PORTS) {
2370 fprintf(stderr, "qemu: too many serial ports\n");
2371 exit(1);
2373 snprintf(label, sizeof(label), "serial%d", index);
2374 serial_hds[index] = qemu_chr_new(label, devname, NULL);
2375 if (!serial_hds[index]) {
2376 fprintf(stderr, "qemu: could not connect serial device"
2377 " to character backend '%s'\n", devname);
2378 return -1;
2380 index++;
2381 return 0;
2384 static int parallel_parse(const char *devname)
2386 static int index = 0;
2387 char label[32];
2389 if (strcmp(devname, "none") == 0)
2390 return 0;
2391 if (index == MAX_PARALLEL_PORTS) {
2392 fprintf(stderr, "qemu: too many parallel ports\n");
2393 exit(1);
2395 snprintf(label, sizeof(label), "parallel%d", index);
2396 parallel_hds[index] = qemu_chr_new(label, devname, NULL);
2397 if (!parallel_hds[index]) {
2398 fprintf(stderr, "qemu: could not connect parallel device"
2399 " to character backend '%s'\n", devname);
2400 return -1;
2402 index++;
2403 return 0;
2406 static int virtcon_parse(const char *devname)
2408 QemuOptsList *device = qemu_find_opts("device");
2409 static int index = 0;
2410 char label[32];
2411 QemuOpts *bus_opts, *dev_opts;
2413 if (strcmp(devname, "none") == 0)
2414 return 0;
2415 if (index == MAX_VIRTIO_CONSOLES) {
2416 fprintf(stderr, "qemu: too many virtio consoles\n");
2417 exit(1);
2420 bus_opts = qemu_opts_create(device, NULL, 0, &error_abort);
2421 if (arch_type == QEMU_ARCH_S390X) {
2422 qemu_opt_set(bus_opts, "driver", "virtio-serial-s390");
2423 } else {
2424 qemu_opt_set(bus_opts, "driver", "virtio-serial-pci");
2427 dev_opts = qemu_opts_create(device, NULL, 0, &error_abort);
2428 qemu_opt_set(dev_opts, "driver", "virtconsole");
2430 snprintf(label, sizeof(label), "virtcon%d", index);
2431 virtcon_hds[index] = qemu_chr_new(label, devname, NULL);
2432 if (!virtcon_hds[index]) {
2433 fprintf(stderr, "qemu: could not connect virtio console"
2434 " to character backend '%s'\n", devname);
2435 return -1;
2437 qemu_opt_set(dev_opts, "chardev", label);
2439 index++;
2440 return 0;
2443 static int sclp_parse(const char *devname)
2445 QemuOptsList *device = qemu_find_opts("device");
2446 static int index = 0;
2447 char label[32];
2448 QemuOpts *dev_opts;
2450 if (strcmp(devname, "none") == 0) {
2451 return 0;
2453 if (index == MAX_SCLP_CONSOLES) {
2454 fprintf(stderr, "qemu: too many sclp consoles\n");
2455 exit(1);
2458 assert(arch_type == QEMU_ARCH_S390X);
2460 dev_opts = qemu_opts_create(device, NULL, 0, NULL);
2461 qemu_opt_set(dev_opts, "driver", "sclpconsole");
2463 snprintf(label, sizeof(label), "sclpcon%d", index);
2464 sclp_hds[index] = qemu_chr_new(label, devname, NULL);
2465 if (!sclp_hds[index]) {
2466 fprintf(stderr, "qemu: could not connect sclp console"
2467 " to character backend '%s'\n", devname);
2468 return -1;
2470 qemu_opt_set(dev_opts, "chardev", label);
2472 index++;
2473 return 0;
2476 static int debugcon_parse(const char *devname)
2478 QemuOpts *opts;
2480 if (!qemu_chr_new("debugcon", devname, NULL)) {
2481 exit(1);
2483 opts = qemu_opts_create(qemu_find_opts("device"), "debugcon", 1, NULL);
2484 if (!opts) {
2485 fprintf(stderr, "qemu: already have a debugcon device\n");
2486 exit(1);
2488 qemu_opt_set(opts, "driver", "isa-debugcon");
2489 qemu_opt_set(opts, "chardev", "debugcon");
2490 return 0;
2493 static MachineClass *machine_parse(const char *name)
2495 MachineClass *mc = NULL;
2496 GSList *el, *machines = object_class_get_list(TYPE_MACHINE, false);
2498 if (name) {
2499 mc = find_machine(name);
2501 if (mc) {
2502 return mc;
2504 if (name && !is_help_option(name)) {
2505 error_report("Unsupported machine type");
2506 error_printf("Use -machine help to list supported machines!\n");
2507 } else {
2508 printf("Supported machines are:\n");
2509 for (el = machines; el; el = el->next) {
2510 MachineClass *mc = el->data;
2511 if (mc->alias) {
2512 printf("%-20s %s (alias of %s)\n", mc->alias, mc->desc, mc->name);
2514 printf("%-20s %s%s\n", mc->name, mc->desc,
2515 mc->is_default ? " (default)" : "");
2519 g_slist_free(machines);
2520 exit(!name || !is_help_option(name));
2523 void qemu_add_exit_notifier(Notifier *notify)
2525 notifier_list_add(&exit_notifiers, notify);
2528 void qemu_remove_exit_notifier(Notifier *notify)
2530 notifier_remove(notify);
2533 static void qemu_run_exit_notifiers(void)
2535 notifier_list_notify(&exit_notifiers, NULL);
2538 void qemu_add_machine_init_done_notifier(Notifier *notify)
2540 notifier_list_add(&machine_init_done_notifiers, notify);
2543 static void qemu_run_machine_init_done_notifiers(void)
2545 notifier_list_notify(&machine_init_done_notifiers, NULL);
2548 static const QEMUOption *lookup_opt(int argc, char **argv,
2549 const char **poptarg, int *poptind)
2551 const QEMUOption *popt;
2552 int optind = *poptind;
2553 char *r = argv[optind];
2554 const char *optarg;
2556 loc_set_cmdline(argv, optind, 1);
2557 optind++;
2558 /* Treat --foo the same as -foo. */
2559 if (r[1] == '-')
2560 r++;
2561 popt = qemu_options;
2562 for(;;) {
2563 if (!popt->name) {
2564 error_report("invalid option");
2565 exit(1);
2567 if (!strcmp(popt->name, r + 1))
2568 break;
2569 popt++;
2571 if (popt->flags & HAS_ARG) {
2572 if (optind >= argc) {
2573 error_report("requires an argument");
2574 exit(1);
2576 optarg = argv[optind++];
2577 loc_set_cmdline(argv, optind - 2, 2);
2578 } else {
2579 optarg = NULL;
2582 *poptarg = optarg;
2583 *poptind = optind;
2585 return popt;
2588 static gpointer malloc_and_trace(gsize n_bytes)
2590 void *ptr = malloc(n_bytes);
2591 trace_g_malloc(n_bytes, ptr);
2592 return ptr;
2595 static gpointer realloc_and_trace(gpointer mem, gsize n_bytes)
2597 void *ptr = realloc(mem, n_bytes);
2598 trace_g_realloc(mem, n_bytes, ptr);
2599 return ptr;
2602 static void free_and_trace(gpointer mem)
2604 trace_g_free(mem);
2605 free(mem);
2608 static int machine_set_property(const char *name, const char *value,
2609 void *opaque)
2611 Object *obj = OBJECT(opaque);
2612 StringInputVisitor *siv;
2613 Error *local_err = NULL;
2614 char *c, *qom_name;
2616 if (strcmp(name, "type") == 0) {
2617 return 0;
2620 qom_name = g_strdup(name);
2621 c = qom_name;
2622 while (*c++) {
2623 if (*c == '_') {
2624 *c = '-';
2628 siv = string_input_visitor_new(value);
2629 object_property_set(obj, string_input_get_visitor(siv), qom_name, &local_err);
2630 string_input_visitor_cleanup(siv);
2631 g_free(qom_name);
2633 if (local_err) {
2634 qerror_report_err(local_err);
2635 error_free(local_err);
2636 return -1;
2639 return 0;
2642 static int object_create(QemuOpts *opts, void *opaque)
2644 Error *err = NULL;
2645 char *type = NULL;
2646 char *id = NULL;
2647 void *dummy = NULL;
2648 OptsVisitor *ov;
2649 QDict *pdict;
2651 ov = opts_visitor_new(opts);
2652 pdict = qemu_opts_to_qdict(opts, NULL);
2654 visit_start_struct(opts_get_visitor(ov), &dummy, NULL, NULL, 0, &err);
2655 if (err) {
2656 goto out;
2659 qdict_del(pdict, "qom-type");
2660 visit_type_str(opts_get_visitor(ov), &type, "qom-type", &err);
2661 if (err) {
2662 goto out;
2665 qdict_del(pdict, "id");
2666 visit_type_str(opts_get_visitor(ov), &id, "id", &err);
2667 if (err) {
2668 goto out;
2671 object_add(type, id, pdict, opts_get_visitor(ov), &err);
2672 if (err) {
2673 goto out;
2675 visit_end_struct(opts_get_visitor(ov), &err);
2676 if (err) {
2677 qmp_object_del(id, NULL);
2680 out:
2681 opts_visitor_cleanup(ov);
2683 QDECREF(pdict);
2684 g_free(id);
2685 g_free(type);
2686 g_free(dummy);
2687 if (err) {
2688 qerror_report_err(err);
2689 error_free(err);
2690 return -1;
2692 return 0;
2695 int main(int argc, char **argv, char **envp)
2697 int i;
2698 int snapshot, linux_boot;
2699 const char *initrd_filename;
2700 const char *kernel_filename, *kernel_cmdline;
2701 const char *boot_order;
2702 DisplayState *ds;
2703 int cyls, heads, secs, translation;
2704 QemuOpts *hda_opts = NULL, *opts, *machine_opts, *icount_opts = NULL;
2705 QemuOptsList *olist;
2706 int optind;
2707 const char *optarg;
2708 const char *loadvm = NULL;
2709 MachineClass *machine_class;
2710 const char *cpu_model;
2711 const char *vga_model = NULL;
2712 const char *qtest_chrdev = NULL;
2713 const char *qtest_log = NULL;
2714 const char *pid_file = NULL;
2715 const char *incoming = NULL;
2716 #ifdef CONFIG_VNC
2717 int show_vnc_port = 0;
2718 #endif
2719 bool defconfig = true;
2720 bool userconfig = true;
2721 const char *log_mask = NULL;
2722 const char *log_file = NULL;
2723 GMemVTable mem_trace = {
2724 .malloc = malloc_and_trace,
2725 .realloc = realloc_and_trace,
2726 .free = free_and_trace,
2728 const char *trace_events = NULL;
2729 const char *trace_file = NULL;
2730 const ram_addr_t default_ram_size = (ram_addr_t)DEFAULT_RAM_SIZE *
2731 1024 * 1024;
2732 ram_addr_t maxram_size = default_ram_size;
2733 uint64_t ram_slots = 0;
2734 FILE *vmstate_dump_file = NULL;
2735 Error *main_loop_err = NULL;
2737 atexit(qemu_run_exit_notifiers);
2738 error_set_progname(argv[0]);
2739 qemu_init_exec_dir(argv[0]);
2741 g_mem_set_vtable(&mem_trace);
2743 module_call_init(MODULE_INIT_QOM);
2745 qemu_add_opts(&qemu_drive_opts);
2746 qemu_add_drive_opts(&qemu_legacy_drive_opts);
2747 qemu_add_drive_opts(&qemu_common_drive_opts);
2748 qemu_add_drive_opts(&qemu_drive_opts);
2749 qemu_add_opts(&qemu_chardev_opts);
2750 qemu_add_opts(&qemu_device_opts);
2751 qemu_add_opts(&qemu_netdev_opts);
2752 qemu_add_opts(&qemu_net_opts);
2753 qemu_add_opts(&qemu_rtc_opts);
2754 qemu_add_opts(&qemu_global_opts);
2755 qemu_add_opts(&qemu_mon_opts);
2756 qemu_add_opts(&qemu_trace_opts);
2757 qemu_add_opts(&qemu_option_rom_opts);
2758 qemu_add_opts(&qemu_machine_opts);
2759 qemu_add_opts(&qemu_mem_opts);
2760 qemu_add_opts(&qemu_smp_opts);
2761 qemu_add_opts(&qemu_boot_opts);
2762 qemu_add_opts(&qemu_sandbox_opts);
2763 qemu_add_opts(&qemu_add_fd_opts);
2764 qemu_add_opts(&qemu_object_opts);
2765 qemu_add_opts(&qemu_tpmdev_opts);
2766 qemu_add_opts(&qemu_realtime_opts);
2767 qemu_add_opts(&qemu_msg_opts);
2768 qemu_add_opts(&qemu_name_opts);
2769 qemu_add_opts(&qemu_numa_opts);
2770 qemu_add_opts(&qemu_icount_opts);
2772 runstate_init();
2774 rtc_clock = QEMU_CLOCK_HOST;
2776 QLIST_INIT (&vm_change_state_head);
2777 os_setup_early_signal_handling();
2779 module_call_init(MODULE_INIT_MACHINE);
2780 machine_class = find_default_machine();
2781 cpu_model = NULL;
2782 ram_size = default_ram_size;
2783 snapshot = 0;
2784 cyls = heads = secs = 0;
2785 translation = BIOS_ATA_TRANSLATION_AUTO;
2787 for (i = 0; i < MAX_NODES; i++) {
2788 numa_info[i].node_mem = 0;
2789 numa_info[i].present = false;
2790 bitmap_zero(numa_info[i].node_cpu, MAX_CPUMASK_BITS);
2793 nb_numa_nodes = 0;
2794 max_numa_nodeid = 0;
2795 nb_nics = 0;
2797 bdrv_init_with_whitelist();
2799 autostart = 1;
2801 /* first pass of option parsing */
2802 optind = 1;
2803 while (optind < argc) {
2804 if (argv[optind][0] != '-') {
2805 /* disk image */
2806 optind++;
2807 } else {
2808 const QEMUOption *popt;
2810 popt = lookup_opt(argc, argv, &optarg, &optind);
2811 switch (popt->index) {
2812 case QEMU_OPTION_nodefconfig:
2813 defconfig = false;
2814 break;
2815 case QEMU_OPTION_nouserconfig:
2816 userconfig = false;
2817 break;
2822 if (defconfig) {
2823 int ret;
2824 ret = qemu_read_default_config_files(userconfig);
2825 if (ret < 0) {
2826 exit(1);
2830 /* second pass of option parsing */
2831 optind = 1;
2832 for(;;) {
2833 if (optind >= argc)
2834 break;
2835 if (argv[optind][0] != '-') {
2836 hda_opts = drive_add(IF_DEFAULT, 0, argv[optind++], HD_OPTS);
2837 } else {
2838 const QEMUOption *popt;
2840 popt = lookup_opt(argc, argv, &optarg, &optind);
2841 if (!(popt->arch_mask & arch_type)) {
2842 printf("Option %s not supported for this target\n", popt->name);
2843 exit(1);
2845 switch(popt->index) {
2846 case QEMU_OPTION_M:
2847 machine_class = machine_parse(optarg);
2848 break;
2849 case QEMU_OPTION_no_kvm_irqchip: {
2850 olist = qemu_find_opts("machine");
2851 qemu_opts_parse(olist, "kernel_irqchip=off", 0);
2852 break;
2854 case QEMU_OPTION_cpu:
2855 /* hw initialization will check this */
2856 cpu_model = optarg;
2857 break;
2858 case QEMU_OPTION_hda:
2860 char buf[256];
2861 if (cyls == 0)
2862 snprintf(buf, sizeof(buf), "%s", HD_OPTS);
2863 else
2864 snprintf(buf, sizeof(buf),
2865 "%s,cyls=%d,heads=%d,secs=%d%s",
2866 HD_OPTS , cyls, heads, secs,
2867 translation == BIOS_ATA_TRANSLATION_LBA ?
2868 ",trans=lba" :
2869 translation == BIOS_ATA_TRANSLATION_NONE ?
2870 ",trans=none" : "");
2871 drive_add(IF_DEFAULT, 0, optarg, buf);
2872 break;
2874 case QEMU_OPTION_hdb:
2875 case QEMU_OPTION_hdc:
2876 case QEMU_OPTION_hdd:
2877 drive_add(IF_DEFAULT, popt->index - QEMU_OPTION_hda, optarg,
2878 HD_OPTS);
2879 break;
2880 case QEMU_OPTION_drive:
2881 if (drive_def(optarg) == NULL) {
2882 exit(1);
2884 break;
2885 case QEMU_OPTION_set:
2886 if (qemu_set_option(optarg) != 0)
2887 exit(1);
2888 break;
2889 case QEMU_OPTION_global:
2890 if (qemu_global_option(optarg) != 0)
2891 exit(1);
2892 break;
2893 case QEMU_OPTION_mtdblock:
2894 drive_add(IF_MTD, -1, optarg, MTD_OPTS);
2895 break;
2896 case QEMU_OPTION_sd:
2897 drive_add(IF_SD, -1, optarg, SD_OPTS);
2898 break;
2899 case QEMU_OPTION_pflash:
2900 drive_add(IF_PFLASH, -1, optarg, PFLASH_OPTS);
2901 break;
2902 case QEMU_OPTION_snapshot:
2903 snapshot = 1;
2904 break;
2905 case QEMU_OPTION_hdachs:
2907 const char *p;
2908 p = optarg;
2909 cyls = strtol(p, (char **)&p, 0);
2910 if (cyls < 1 || cyls > 16383)
2911 goto chs_fail;
2912 if (*p != ',')
2913 goto chs_fail;
2914 p++;
2915 heads = strtol(p, (char **)&p, 0);
2916 if (heads < 1 || heads > 16)
2917 goto chs_fail;
2918 if (*p != ',')
2919 goto chs_fail;
2920 p++;
2921 secs = strtol(p, (char **)&p, 0);
2922 if (secs < 1 || secs > 63)
2923 goto chs_fail;
2924 if (*p == ',') {
2925 p++;
2926 if (!strcmp(p, "large")) {
2927 translation = BIOS_ATA_TRANSLATION_LARGE;
2928 } else if (!strcmp(p, "rechs")) {
2929 translation = BIOS_ATA_TRANSLATION_RECHS;
2930 } else if (!strcmp(p, "none")) {
2931 translation = BIOS_ATA_TRANSLATION_NONE;
2932 } else if (!strcmp(p, "lba")) {
2933 translation = BIOS_ATA_TRANSLATION_LBA;
2934 } else if (!strcmp(p, "auto")) {
2935 translation = BIOS_ATA_TRANSLATION_AUTO;
2936 } else {
2937 goto chs_fail;
2939 } else if (*p != '\0') {
2940 chs_fail:
2941 fprintf(stderr, "qemu: invalid physical CHS format\n");
2942 exit(1);
2944 if (hda_opts != NULL) {
2945 char num[16];
2946 snprintf(num, sizeof(num), "%d", cyls);
2947 qemu_opt_set(hda_opts, "cyls", num);
2948 snprintf(num, sizeof(num), "%d", heads);
2949 qemu_opt_set(hda_opts, "heads", num);
2950 snprintf(num, sizeof(num), "%d", secs);
2951 qemu_opt_set(hda_opts, "secs", num);
2952 if (translation == BIOS_ATA_TRANSLATION_LARGE) {
2953 qemu_opt_set(hda_opts, "trans", "large");
2954 } else if (translation == BIOS_ATA_TRANSLATION_RECHS) {
2955 qemu_opt_set(hda_opts, "trans", "rechs");
2956 } else if (translation == BIOS_ATA_TRANSLATION_LBA) {
2957 qemu_opt_set(hda_opts, "trans", "lba");
2958 } else if (translation == BIOS_ATA_TRANSLATION_NONE) {
2959 qemu_opt_set(hda_opts, "trans", "none");
2963 break;
2964 case QEMU_OPTION_numa:
2965 opts = qemu_opts_parse(qemu_find_opts("numa"), optarg, 1);
2966 if (!opts) {
2967 exit(1);
2969 break;
2970 case QEMU_OPTION_display:
2971 display_type = select_display(optarg);
2972 break;
2973 case QEMU_OPTION_nographic:
2974 display_type = DT_NOGRAPHIC;
2975 break;
2976 case QEMU_OPTION_curses:
2977 #ifdef CONFIG_CURSES
2978 display_type = DT_CURSES;
2979 #else
2980 fprintf(stderr, "Curses support is disabled\n");
2981 exit(1);
2982 #endif
2983 break;
2984 case QEMU_OPTION_portrait:
2985 graphic_rotate = 90;
2986 break;
2987 case QEMU_OPTION_rotate:
2988 graphic_rotate = strtol(optarg, (char **) &optarg, 10);
2989 if (graphic_rotate != 0 && graphic_rotate != 90 &&
2990 graphic_rotate != 180 && graphic_rotate != 270) {
2991 fprintf(stderr,
2992 "qemu: only 90, 180, 270 deg rotation is available\n");
2993 exit(1);
2995 break;
2996 case QEMU_OPTION_kernel:
2997 qemu_opts_set(qemu_find_opts("machine"), 0, "kernel", optarg);
2998 break;
2999 case QEMU_OPTION_initrd:
3000 qemu_opts_set(qemu_find_opts("machine"), 0, "initrd", optarg);
3001 break;
3002 case QEMU_OPTION_append:
3003 qemu_opts_set(qemu_find_opts("machine"), 0, "append", optarg);
3004 break;
3005 case QEMU_OPTION_dtb:
3006 qemu_opts_set(qemu_find_opts("machine"), 0, "dtb", optarg);
3007 break;
3008 case QEMU_OPTION_cdrom:
3009 drive_add(IF_DEFAULT, 2, optarg, CDROM_OPTS);
3010 break;
3011 case QEMU_OPTION_boot:
3012 opts = qemu_opts_parse(qemu_find_opts("boot-opts"), optarg, 1);
3013 if (!opts) {
3014 exit(1);
3016 break;
3017 case QEMU_OPTION_fda:
3018 case QEMU_OPTION_fdb:
3019 drive_add(IF_FLOPPY, popt->index - QEMU_OPTION_fda,
3020 optarg, FD_OPTS);
3021 break;
3022 case QEMU_OPTION_no_fd_bootchk:
3023 fd_bootchk = 0;
3024 break;
3025 case QEMU_OPTION_netdev:
3026 if (net_client_parse(qemu_find_opts("netdev"), optarg) == -1) {
3027 exit(1);
3029 break;
3030 case QEMU_OPTION_net:
3031 if (net_client_parse(qemu_find_opts("net"), optarg) == -1) {
3032 exit(1);
3034 break;
3035 #ifdef CONFIG_LIBISCSI
3036 case QEMU_OPTION_iscsi:
3037 opts = qemu_opts_parse(qemu_find_opts("iscsi"), optarg, 0);
3038 if (!opts) {
3039 exit(1);
3041 break;
3042 #endif
3043 #ifdef CONFIG_SLIRP
3044 case QEMU_OPTION_tftp:
3045 legacy_tftp_prefix = optarg;
3046 break;
3047 case QEMU_OPTION_bootp:
3048 legacy_bootp_filename = optarg;
3049 break;
3050 case QEMU_OPTION_redir:
3051 if (net_slirp_redir(optarg) < 0)
3052 exit(1);
3053 break;
3054 #endif
3055 case QEMU_OPTION_bt:
3056 add_device_config(DEV_BT, optarg);
3057 break;
3058 case QEMU_OPTION_audio_help:
3059 AUD_help ();
3060 exit (0);
3061 break;
3062 case QEMU_OPTION_soundhw:
3063 select_soundhw (optarg);
3064 break;
3065 case QEMU_OPTION_h:
3066 help(0);
3067 break;
3068 case QEMU_OPTION_version:
3069 version();
3070 exit(0);
3071 break;
3072 case QEMU_OPTION_m: {
3073 uint64_t sz;
3074 const char *mem_str;
3075 const char *maxmem_str, *slots_str;
3077 opts = qemu_opts_parse(qemu_find_opts("memory"),
3078 optarg, 1);
3079 if (!opts) {
3080 exit(EXIT_FAILURE);
3083 mem_str = qemu_opt_get(opts, "size");
3084 if (!mem_str) {
3085 error_report("invalid -m option, missing 'size' option");
3086 exit(EXIT_FAILURE);
3088 if (!*mem_str) {
3089 error_report("missing 'size' option value");
3090 exit(EXIT_FAILURE);
3093 sz = qemu_opt_get_size(opts, "size", ram_size);
3095 /* Fix up legacy suffix-less format */
3096 if (g_ascii_isdigit(mem_str[strlen(mem_str) - 1])) {
3097 uint64_t overflow_check = sz;
3099 sz <<= 20;
3100 if ((sz >> 20) != overflow_check) {
3101 error_report("too large 'size' option value");
3102 exit(EXIT_FAILURE);
3106 /* backward compatibility behaviour for case "-m 0" */
3107 if (sz == 0) {
3108 sz = default_ram_size;
3111 sz = QEMU_ALIGN_UP(sz, 8192);
3112 ram_size = sz;
3113 if (ram_size != sz) {
3114 error_report("ram size too large");
3115 exit(EXIT_FAILURE);
3117 maxram_size = ram_size;
3119 maxmem_str = qemu_opt_get(opts, "maxmem");
3120 slots_str = qemu_opt_get(opts, "slots");
3121 if (maxmem_str && slots_str) {
3122 uint64_t slots;
3124 sz = qemu_opt_get_size(opts, "maxmem", 0);
3125 if (sz < ram_size) {
3126 error_report("invalid -m option value: maxmem "
3127 "(0x%" PRIx64 ") <= initial memory (0x"
3128 RAM_ADDR_FMT ")", sz, ram_size);
3129 exit(EXIT_FAILURE);
3132 slots = qemu_opt_get_number(opts, "slots", 0);
3133 if ((sz > ram_size) && !slots) {
3134 error_report("invalid -m option value: maxmem "
3135 "(0x%" PRIx64 ") more than initial memory (0x"
3136 RAM_ADDR_FMT ") but no hotplug slots where "
3137 "specified", sz, ram_size);
3138 exit(EXIT_FAILURE);
3141 if ((sz <= ram_size) && slots) {
3142 error_report("invalid -m option value: %"
3143 PRIu64 " hotplug slots where specified but "
3144 "maxmem (0x%" PRIx64 ") <= initial memory (0x"
3145 RAM_ADDR_FMT ")", slots, sz, ram_size);
3146 exit(EXIT_FAILURE);
3148 maxram_size = sz;
3149 ram_slots = slots;
3150 } else if ((!maxmem_str && slots_str) ||
3151 (maxmem_str && !slots_str)) {
3152 error_report("invalid -m option value: missing "
3153 "'%s' option", slots_str ? "maxmem" : "slots");
3154 exit(EXIT_FAILURE);
3156 break;
3158 #ifdef CONFIG_TPM
3159 case QEMU_OPTION_tpmdev:
3160 if (tpm_config_parse(qemu_find_opts("tpmdev"), optarg) < 0) {
3161 exit(1);
3163 break;
3164 #endif
3165 case QEMU_OPTION_mempath:
3166 mem_path = optarg;
3167 break;
3168 case QEMU_OPTION_mem_prealloc:
3169 mem_prealloc = 1;
3170 break;
3171 case QEMU_OPTION_d:
3172 log_mask = optarg;
3173 break;
3174 case QEMU_OPTION_D:
3175 log_file = optarg;
3176 break;
3177 case QEMU_OPTION_s:
3178 add_device_config(DEV_GDB, "tcp::" DEFAULT_GDBSTUB_PORT);
3179 break;
3180 case QEMU_OPTION_gdb:
3181 add_device_config(DEV_GDB, optarg);
3182 break;
3183 case QEMU_OPTION_L:
3184 if (data_dir_idx < ARRAY_SIZE(data_dir)) {
3185 data_dir[data_dir_idx++] = optarg;
3187 break;
3188 case QEMU_OPTION_bios:
3189 qemu_opts_set(qemu_find_opts("machine"), 0, "firmware", optarg);
3190 break;
3191 case QEMU_OPTION_singlestep:
3192 singlestep = 1;
3193 break;
3194 case QEMU_OPTION_S:
3195 autostart = 0;
3196 break;
3197 case QEMU_OPTION_k:
3198 keyboard_layout = optarg;
3199 break;
3200 case QEMU_OPTION_localtime:
3201 rtc_utc = 0;
3202 break;
3203 case QEMU_OPTION_vga:
3204 vga_model = optarg;
3205 default_vga = 0;
3206 break;
3207 case QEMU_OPTION_g:
3209 const char *p;
3210 int w, h, depth;
3211 p = optarg;
3212 w = strtol(p, (char **)&p, 10);
3213 if (w <= 0) {
3214 graphic_error:
3215 fprintf(stderr, "qemu: invalid resolution or depth\n");
3216 exit(1);
3218 if (*p != 'x')
3219 goto graphic_error;
3220 p++;
3221 h = strtol(p, (char **)&p, 10);
3222 if (h <= 0)
3223 goto graphic_error;
3224 if (*p == 'x') {
3225 p++;
3226 depth = strtol(p, (char **)&p, 10);
3227 if (depth != 8 && depth != 15 && depth != 16 &&
3228 depth != 24 && depth != 32)
3229 goto graphic_error;
3230 } else if (*p == '\0') {
3231 depth = graphic_depth;
3232 } else {
3233 goto graphic_error;
3236 graphic_width = w;
3237 graphic_height = h;
3238 graphic_depth = depth;
3240 break;
3241 case QEMU_OPTION_echr:
3243 char *r;
3244 term_escape_char = strtol(optarg, &r, 0);
3245 if (r == optarg)
3246 printf("Bad argument to echr\n");
3247 break;
3249 case QEMU_OPTION_monitor:
3250 default_monitor = 0;
3251 if (strncmp(optarg, "none", 4)) {
3252 monitor_parse(optarg, "readline");
3254 break;
3255 case QEMU_OPTION_qmp:
3256 monitor_parse(optarg, "control");
3257 default_monitor = 0;
3258 break;
3259 case QEMU_OPTION_mon:
3260 opts = qemu_opts_parse(qemu_find_opts("mon"), optarg, 1);
3261 if (!opts) {
3262 exit(1);
3264 default_monitor = 0;
3265 break;
3266 case QEMU_OPTION_chardev:
3267 opts = qemu_opts_parse(qemu_find_opts("chardev"), optarg, 1);
3268 if (!opts) {
3269 exit(1);
3271 break;
3272 case QEMU_OPTION_fsdev:
3273 olist = qemu_find_opts("fsdev");
3274 if (!olist) {
3275 fprintf(stderr, "fsdev is not supported by this qemu build.\n");
3276 exit(1);
3278 opts = qemu_opts_parse(olist, optarg, 1);
3279 if (!opts) {
3280 exit(1);
3282 break;
3283 case QEMU_OPTION_virtfs: {
3284 QemuOpts *fsdev;
3285 QemuOpts *device;
3286 const char *writeout, *sock_fd, *socket;
3288 olist = qemu_find_opts("virtfs");
3289 if (!olist) {
3290 fprintf(stderr, "virtfs is not supported by this qemu build.\n");
3291 exit(1);
3293 opts = qemu_opts_parse(olist, optarg, 1);
3294 if (!opts) {
3295 exit(1);
3298 if (qemu_opt_get(opts, "fsdriver") == NULL ||
3299 qemu_opt_get(opts, "mount_tag") == NULL) {
3300 fprintf(stderr, "Usage: -virtfs fsdriver,mount_tag=tag.\n");
3301 exit(1);
3303 fsdev = qemu_opts_create(qemu_find_opts("fsdev"),
3304 qemu_opt_get(opts, "mount_tag"),
3305 1, NULL);
3306 if (!fsdev) {
3307 fprintf(stderr, "duplicate fsdev id: %s\n",
3308 qemu_opt_get(opts, "mount_tag"));
3309 exit(1);
3312 writeout = qemu_opt_get(opts, "writeout");
3313 if (writeout) {
3314 #ifdef CONFIG_SYNC_FILE_RANGE
3315 qemu_opt_set(fsdev, "writeout", writeout);
3316 #else
3317 fprintf(stderr, "writeout=immediate not supported on "
3318 "this platform\n");
3319 exit(1);
3320 #endif
3322 qemu_opt_set(fsdev, "fsdriver", qemu_opt_get(opts, "fsdriver"));
3323 qemu_opt_set(fsdev, "path", qemu_opt_get(opts, "path"));
3324 qemu_opt_set(fsdev, "security_model",
3325 qemu_opt_get(opts, "security_model"));
3326 socket = qemu_opt_get(opts, "socket");
3327 if (socket) {
3328 qemu_opt_set(fsdev, "socket", socket);
3330 sock_fd = qemu_opt_get(opts, "sock_fd");
3331 if (sock_fd) {
3332 qemu_opt_set(fsdev, "sock_fd", sock_fd);
3335 qemu_opt_set_bool(fsdev, "readonly",
3336 qemu_opt_get_bool(opts, "readonly", 0));
3337 device = qemu_opts_create(qemu_find_opts("device"), NULL, 0,
3338 &error_abort);
3339 qemu_opt_set(device, "driver", "virtio-9p-pci");
3340 qemu_opt_set(device, "fsdev",
3341 qemu_opt_get(opts, "mount_tag"));
3342 qemu_opt_set(device, "mount_tag",
3343 qemu_opt_get(opts, "mount_tag"));
3344 break;
3346 case QEMU_OPTION_virtfs_synth: {
3347 QemuOpts *fsdev;
3348 QemuOpts *device;
3350 fsdev = qemu_opts_create(qemu_find_opts("fsdev"), "v_synth",
3351 1, NULL);
3352 if (!fsdev) {
3353 fprintf(stderr, "duplicate option: %s\n", "virtfs_synth");
3354 exit(1);
3356 qemu_opt_set(fsdev, "fsdriver", "synth");
3358 device = qemu_opts_create(qemu_find_opts("device"), NULL, 0,
3359 &error_abort);
3360 qemu_opt_set(device, "driver", "virtio-9p-pci");
3361 qemu_opt_set(device, "fsdev", "v_synth");
3362 qemu_opt_set(device, "mount_tag", "v_synth");
3363 break;
3365 case QEMU_OPTION_serial:
3366 add_device_config(DEV_SERIAL, optarg);
3367 default_serial = 0;
3368 if (strncmp(optarg, "mon:", 4) == 0) {
3369 default_monitor = 0;
3371 break;
3372 case QEMU_OPTION_watchdog:
3373 if (watchdog) {
3374 fprintf(stderr,
3375 "qemu: only one watchdog option may be given\n");
3376 return 1;
3378 watchdog = optarg;
3379 break;
3380 case QEMU_OPTION_watchdog_action:
3381 if (select_watchdog_action(optarg) == -1) {
3382 fprintf(stderr, "Unknown -watchdog-action parameter\n");
3383 exit(1);
3385 break;
3386 case QEMU_OPTION_virtiocon:
3387 add_device_config(DEV_VIRTCON, optarg);
3388 default_virtcon = 0;
3389 if (strncmp(optarg, "mon:", 4) == 0) {
3390 default_monitor = 0;
3392 break;
3393 case QEMU_OPTION_parallel:
3394 add_device_config(DEV_PARALLEL, optarg);
3395 default_parallel = 0;
3396 if (strncmp(optarg, "mon:", 4) == 0) {
3397 default_monitor = 0;
3399 break;
3400 case QEMU_OPTION_debugcon:
3401 add_device_config(DEV_DEBUGCON, optarg);
3402 break;
3403 case QEMU_OPTION_loadvm:
3404 loadvm = optarg;
3405 break;
3406 case QEMU_OPTION_full_screen:
3407 full_screen = 1;
3408 break;
3409 case QEMU_OPTION_no_frame:
3410 no_frame = 1;
3411 break;
3412 case QEMU_OPTION_alt_grab:
3413 alt_grab = 1;
3414 break;
3415 case QEMU_OPTION_ctrl_grab:
3416 ctrl_grab = 1;
3417 break;
3418 case QEMU_OPTION_no_quit:
3419 no_quit = 1;
3420 break;
3421 case QEMU_OPTION_sdl:
3422 #ifdef CONFIG_SDL
3423 display_type = DT_SDL;
3424 break;
3425 #else
3426 fprintf(stderr, "SDL support is disabled\n");
3427 exit(1);
3428 #endif
3429 case QEMU_OPTION_pidfile:
3430 pid_file = optarg;
3431 break;
3432 case QEMU_OPTION_win2k_hack:
3433 win2k_install_hack = 1;
3434 break;
3435 case QEMU_OPTION_rtc_td_hack: {
3436 static GlobalProperty slew_lost_ticks[] = {
3438 .driver = "mc146818rtc",
3439 .property = "lost_tick_policy",
3440 .value = "slew",
3442 { /* end of list */ }
3445 qdev_prop_register_global_list(slew_lost_ticks);
3446 break;
3448 case QEMU_OPTION_acpitable:
3449 opts = qemu_opts_parse(qemu_find_opts("acpi"), optarg, 1);
3450 if (!opts) {
3451 exit(1);
3453 do_acpitable_option(opts);
3454 break;
3455 case QEMU_OPTION_smbios:
3456 opts = qemu_opts_parse(qemu_find_opts("smbios"), optarg, 0);
3457 if (!opts) {
3458 exit(1);
3460 do_smbios_option(opts);
3461 break;
3462 case QEMU_OPTION_enable_kvm:
3463 olist = qemu_find_opts("machine");
3464 qemu_opts_parse(olist, "accel=kvm", 0);
3465 break;
3466 case QEMU_OPTION_machine:
3467 olist = qemu_find_opts("machine");
3468 opts = qemu_opts_parse(olist, optarg, 1);
3469 if (!opts) {
3470 exit(1);
3472 optarg = qemu_opt_get(opts, "type");
3473 if (optarg) {
3474 machine_class = machine_parse(optarg);
3476 break;
3477 case QEMU_OPTION_no_kvm:
3478 olist = qemu_find_opts("machine");
3479 qemu_opts_parse(olist, "accel=tcg", 0);
3480 break;
3481 case QEMU_OPTION_no_kvm_pit: {
3482 fprintf(stderr, "Warning: KVM PIT can no longer be disabled "
3483 "separately.\n");
3484 break;
3486 case QEMU_OPTION_no_kvm_pit_reinjection: {
3487 static GlobalProperty kvm_pit_lost_tick_policy[] = {
3489 .driver = "kvm-pit",
3490 .property = "lost_tick_policy",
3491 .value = "discard",
3493 { /* end of list */ }
3496 fprintf(stderr, "Warning: option deprecated, use "
3497 "lost_tick_policy property of kvm-pit instead.\n");
3498 qdev_prop_register_global_list(kvm_pit_lost_tick_policy);
3499 break;
3501 case QEMU_OPTION_usb:
3502 olist = qemu_find_opts("machine");
3503 qemu_opts_parse(olist, "usb=on", 0);
3504 break;
3505 case QEMU_OPTION_usbdevice:
3506 olist = qemu_find_opts("machine");
3507 qemu_opts_parse(olist, "usb=on", 0);
3508 add_device_config(DEV_USB, optarg);
3509 break;
3510 case QEMU_OPTION_device:
3511 if (!qemu_opts_parse(qemu_find_opts("device"), optarg, 1)) {
3512 exit(1);
3514 break;
3515 case QEMU_OPTION_smp:
3516 if (!qemu_opts_parse(qemu_find_opts("smp-opts"), optarg, 1)) {
3517 exit(1);
3519 break;
3520 case QEMU_OPTION_vnc:
3521 #ifdef CONFIG_VNC
3522 display_remote++;
3523 vnc_display = optarg;
3524 #else
3525 fprintf(stderr, "VNC support is disabled\n");
3526 exit(1);
3527 #endif
3528 break;
3529 case QEMU_OPTION_no_acpi:
3530 acpi_enabled = 0;
3531 break;
3532 case QEMU_OPTION_no_hpet:
3533 no_hpet = 1;
3534 break;
3535 case QEMU_OPTION_balloon:
3536 if (balloon_parse(optarg) < 0) {
3537 fprintf(stderr, "Unknown -balloon argument %s\n", optarg);
3538 exit(1);
3540 break;
3541 case QEMU_OPTION_no_reboot:
3542 no_reboot = 1;
3543 break;
3544 case QEMU_OPTION_no_shutdown:
3545 no_shutdown = 1;
3546 break;
3547 case QEMU_OPTION_show_cursor:
3548 cursor_hide = 0;
3549 break;
3550 case QEMU_OPTION_uuid:
3551 if(qemu_uuid_parse(optarg, qemu_uuid) < 0) {
3552 fprintf(stderr, "Fail to parse UUID string."
3553 " Wrong format.\n");
3554 exit(1);
3556 qemu_uuid_set = true;
3557 break;
3558 case QEMU_OPTION_option_rom:
3559 if (nb_option_roms >= MAX_OPTION_ROMS) {
3560 fprintf(stderr, "Too many option ROMs\n");
3561 exit(1);
3563 opts = qemu_opts_parse(qemu_find_opts("option-rom"), optarg, 1);
3564 if (!opts) {
3565 exit(1);
3567 option_rom[nb_option_roms].name = qemu_opt_get(opts, "romfile");
3568 option_rom[nb_option_roms].bootindex =
3569 qemu_opt_get_number(opts, "bootindex", -1);
3570 if (!option_rom[nb_option_roms].name) {
3571 fprintf(stderr, "Option ROM file is not specified\n");
3572 exit(1);
3574 nb_option_roms++;
3575 break;
3576 case QEMU_OPTION_semihosting:
3577 semihosting_enabled = 1;
3578 break;
3579 case QEMU_OPTION_tdf:
3580 fprintf(stderr, "Warning: user space PIT time drift fix "
3581 "is no longer supported.\n");
3582 break;
3583 case QEMU_OPTION_name:
3584 opts = qemu_opts_parse(qemu_find_opts("name"), optarg, 1);
3585 if (!opts) {
3586 exit(1);
3588 break;
3589 case QEMU_OPTION_prom_env:
3590 if (nb_prom_envs >= MAX_PROM_ENVS) {
3591 fprintf(stderr, "Too many prom variables\n");
3592 exit(1);
3594 prom_envs[nb_prom_envs] = optarg;
3595 nb_prom_envs++;
3596 break;
3597 case QEMU_OPTION_old_param:
3598 old_param = 1;
3599 break;
3600 case QEMU_OPTION_clock:
3601 /* Clock options no longer exist. Keep this option for
3602 * backward compatibility.
3604 break;
3605 case QEMU_OPTION_startdate:
3606 configure_rtc_date_offset(optarg, 1);
3607 break;
3608 case QEMU_OPTION_rtc:
3609 opts = qemu_opts_parse(qemu_find_opts("rtc"), optarg, 0);
3610 if (!opts) {
3611 exit(1);
3613 configure_rtc(opts);
3614 break;
3615 case QEMU_OPTION_tb_size:
3616 tcg_tb_size = strtol(optarg, NULL, 0);
3617 if (tcg_tb_size < 0) {
3618 tcg_tb_size = 0;
3620 break;
3621 case QEMU_OPTION_icount:
3622 icount_opts = qemu_opts_parse(qemu_find_opts("icount"),
3623 optarg, 1);
3624 if (!icount_opts) {
3625 exit(1);
3627 break;
3628 case QEMU_OPTION_incoming:
3629 incoming = optarg;
3630 runstate_set(RUN_STATE_INMIGRATE);
3631 break;
3632 case QEMU_OPTION_nodefaults:
3633 has_defaults = 0;
3634 break;
3635 case QEMU_OPTION_xen_domid:
3636 if (!(xen_available())) {
3637 printf("Option %s not supported for this target\n", popt->name);
3638 exit(1);
3640 xen_domid = atoi(optarg);
3641 break;
3642 case QEMU_OPTION_xen_create:
3643 if (!(xen_available())) {
3644 printf("Option %s not supported for this target\n", popt->name);
3645 exit(1);
3647 xen_mode = XEN_CREATE;
3648 break;
3649 case QEMU_OPTION_xen_attach:
3650 if (!(xen_available())) {
3651 printf("Option %s not supported for this target\n", popt->name);
3652 exit(1);
3654 xen_mode = XEN_ATTACH;
3655 break;
3656 case QEMU_OPTION_trace:
3658 opts = qemu_opts_parse(qemu_find_opts("trace"), optarg, 0);
3659 if (!opts) {
3660 exit(1);
3662 trace_events = qemu_opt_get(opts, "events");
3663 trace_file = qemu_opt_get(opts, "file");
3664 break;
3666 case QEMU_OPTION_readconfig:
3668 int ret = qemu_read_config_file(optarg);
3669 if (ret < 0) {
3670 fprintf(stderr, "read config %s: %s\n", optarg,
3671 strerror(-ret));
3672 exit(1);
3674 break;
3676 case QEMU_OPTION_spice:
3677 olist = qemu_find_opts("spice");
3678 if (!olist) {
3679 fprintf(stderr, "spice is not supported by this qemu build.\n");
3680 exit(1);
3682 opts = qemu_opts_parse(olist, optarg, 0);
3683 if (!opts) {
3684 exit(1);
3686 display_remote++;
3687 break;
3688 case QEMU_OPTION_writeconfig:
3690 FILE *fp;
3691 if (strcmp(optarg, "-") == 0) {
3692 fp = stdout;
3693 } else {
3694 fp = fopen(optarg, "w");
3695 if (fp == NULL) {
3696 fprintf(stderr, "open %s: %s\n", optarg, strerror(errno));
3697 exit(1);
3700 qemu_config_write(fp);
3701 if (fp != stdout) {
3702 fclose(fp);
3704 break;
3706 case QEMU_OPTION_qtest:
3707 qtest_chrdev = optarg;
3708 break;
3709 case QEMU_OPTION_qtest_log:
3710 qtest_log = optarg;
3711 break;
3712 case QEMU_OPTION_sandbox:
3713 opts = qemu_opts_parse(qemu_find_opts("sandbox"), optarg, 1);
3714 if (!opts) {
3715 exit(1);
3717 break;
3718 case QEMU_OPTION_add_fd:
3719 #ifndef _WIN32
3720 opts = qemu_opts_parse(qemu_find_opts("add-fd"), optarg, 0);
3721 if (!opts) {
3722 exit(1);
3724 #else
3725 error_report("File descriptor passing is disabled on this "
3726 "platform");
3727 exit(1);
3728 #endif
3729 break;
3730 case QEMU_OPTION_object:
3731 opts = qemu_opts_parse(qemu_find_opts("object"), optarg, 1);
3732 if (!opts) {
3733 exit(1);
3735 break;
3736 case QEMU_OPTION_realtime:
3737 opts = qemu_opts_parse(qemu_find_opts("realtime"), optarg, 0);
3738 if (!opts) {
3739 exit(1);
3741 enable_mlock = qemu_opt_get_bool(opts, "mlock", true);
3742 break;
3743 case QEMU_OPTION_msg:
3744 opts = qemu_opts_parse(qemu_find_opts("msg"), optarg, 0);
3745 if (!opts) {
3746 exit(1);
3748 configure_msg(opts);
3749 break;
3750 case QEMU_OPTION_dump_vmstate:
3751 vmstate_dump_file = fopen(optarg, "w");
3752 if (vmstate_dump_file == NULL) {
3753 fprintf(stderr, "open %s: %s\n", optarg, strerror(errno));
3754 exit(1);
3756 break;
3757 default:
3758 os_parse_cmd_args(popt->index, optarg);
3762 loc_set_none();
3764 os_daemonize();
3766 if (qemu_init_main_loop(&main_loop_err)) {
3767 error_report("%s", error_get_pretty(main_loop_err));
3768 exit(1);
3771 if (qemu_opts_foreach(qemu_find_opts("sandbox"), parse_sandbox, NULL, 0)) {
3772 exit(1);
3775 if (qemu_opts_foreach(qemu_find_opts("name"), parse_name, NULL, 1)) {
3776 exit(1);
3779 #ifndef _WIN32
3780 if (qemu_opts_foreach(qemu_find_opts("add-fd"), parse_add_fd, NULL, 1)) {
3781 exit(1);
3784 if (qemu_opts_foreach(qemu_find_opts("add-fd"), cleanup_add_fd, NULL, 1)) {
3785 exit(1);
3787 #endif
3789 if (machine_class == NULL) {
3790 fprintf(stderr, "No machine specified, and there is no default.\n"
3791 "Use -machine help to list supported machines!\n");
3792 exit(1);
3795 current_machine = MACHINE(object_new(object_class_get_name(
3796 OBJECT_CLASS(machine_class))));
3797 object_property_add_child(object_get_root(), "machine",
3798 OBJECT(current_machine), &error_abort);
3799 cpu_exec_init_all();
3801 if (machine_class->hw_version) {
3802 qemu_set_version(machine_class->hw_version);
3805 /* Init CPU def lists, based on config
3806 * - Must be called after all the qemu_read_config_file() calls
3807 * - Must be called before list_cpus()
3808 * - Must be called before machine->init()
3810 cpudef_init();
3812 if (cpu_model && is_help_option(cpu_model)) {
3813 list_cpus(stdout, &fprintf, cpu_model);
3814 exit(0);
3817 /* Open the logfile at this point, if necessary. We can't open the logfile
3818 * when encountering either of the logging options (-d or -D) because the
3819 * other one may be encountered later on the command line, changing the
3820 * location or level of logging.
3822 if (log_mask) {
3823 int mask;
3824 if (log_file) {
3825 qemu_set_log_filename(log_file);
3828 mask = qemu_str_to_log_mask(log_mask);
3829 if (!mask) {
3830 qemu_print_log_usage(stdout);
3831 exit(1);
3833 qemu_set_log(mask);
3836 if (!is_daemonized()) {
3837 if (!trace_init_backends(trace_events, trace_file)) {
3838 exit(1);
3842 /* If no data_dir is specified then try to find it relative to the
3843 executable path. */
3844 if (data_dir_idx < ARRAY_SIZE(data_dir)) {
3845 data_dir[data_dir_idx] = os_find_datadir();
3846 if (data_dir[data_dir_idx] != NULL) {
3847 data_dir_idx++;
3850 /* If all else fails use the install path specified when building. */
3851 if (data_dir_idx < ARRAY_SIZE(data_dir)) {
3852 data_dir[data_dir_idx++] = CONFIG_QEMU_DATADIR;
3855 smp_parse(qemu_opts_find(qemu_find_opts("smp-opts"), NULL));
3857 machine_class->max_cpus = machine_class->max_cpus ?: 1; /* Default to UP */
3858 if (smp_cpus > machine_class->max_cpus) {
3859 fprintf(stderr, "Number of SMP cpus requested (%d), exceeds max cpus "
3860 "supported by machine `%s' (%d)\n", smp_cpus,
3861 machine_class->name, machine_class->max_cpus);
3862 exit(1);
3866 * Get the default machine options from the machine if it is not already
3867 * specified either by the configuration file or by the command line.
3869 if (machine_class->default_machine_opts) {
3870 qemu_opts_set_defaults(qemu_find_opts("machine"),
3871 machine_class->default_machine_opts, 0);
3874 qemu_opts_foreach(qemu_find_opts("device"), default_driver_check, NULL, 0);
3875 qemu_opts_foreach(qemu_find_opts("global"), default_driver_check, NULL, 0);
3877 if (!vga_model && !default_vga) {
3878 vga_interface_type = VGA_DEVICE;
3880 if (!has_defaults || machine_class->no_serial) {
3881 default_serial = 0;
3883 if (!has_defaults || machine_class->no_parallel) {
3884 default_parallel = 0;
3886 if (!has_defaults || !machine_class->use_virtcon) {
3887 default_virtcon = 0;
3889 if (!has_defaults || !machine_class->use_sclp) {
3890 default_sclp = 0;
3892 if (!has_defaults || machine_class->no_floppy) {
3893 default_floppy = 0;
3895 if (!has_defaults || machine_class->no_cdrom) {
3896 default_cdrom = 0;
3898 if (!has_defaults || machine_class->no_sdcard) {
3899 default_sdcard = 0;
3901 if (!has_defaults) {
3902 default_monitor = 0;
3903 default_net = 0;
3904 default_vga = 0;
3907 if (is_daemonized()) {
3908 /* According to documentation and historically, -nographic redirects
3909 * serial port, parallel port and monitor to stdio, which does not work
3910 * with -daemonize. We can redirect these to null instead, but since
3911 * -nographic is legacy, let's just error out.
3912 * We disallow -nographic only if all other ports are not redirected
3913 * explicitly, to not break existing legacy setups which uses
3914 * -nographic _and_ redirects all ports explicitly - this is valid
3915 * usage, -nographic is just a no-op in this case.
3917 if (display_type == DT_NOGRAPHIC
3918 && (default_parallel || default_serial
3919 || default_monitor || default_virtcon)) {
3920 fprintf(stderr, "-nographic can not be used with -daemonize\n");
3921 exit(1);
3923 #ifdef CONFIG_CURSES
3924 if (display_type == DT_CURSES) {
3925 fprintf(stderr, "curses display can not be used with -daemonize\n");
3926 exit(1);
3928 #endif
3931 if (display_type == DT_NOGRAPHIC) {
3932 if (default_parallel)
3933 add_device_config(DEV_PARALLEL, "null");
3934 if (default_serial && default_monitor) {
3935 add_device_config(DEV_SERIAL, "mon:stdio");
3936 } else if (default_virtcon && default_monitor) {
3937 add_device_config(DEV_VIRTCON, "mon:stdio");
3938 } else if (default_sclp && default_monitor) {
3939 add_device_config(DEV_SCLP, "mon:stdio");
3940 } else {
3941 if (default_serial)
3942 add_device_config(DEV_SERIAL, "stdio");
3943 if (default_virtcon)
3944 add_device_config(DEV_VIRTCON, "stdio");
3945 if (default_sclp) {
3946 add_device_config(DEV_SCLP, "stdio");
3948 if (default_monitor)
3949 monitor_parse("stdio", "readline");
3951 } else {
3952 if (default_serial)
3953 add_device_config(DEV_SERIAL, "vc:80Cx24C");
3954 if (default_parallel)
3955 add_device_config(DEV_PARALLEL, "vc:80Cx24C");
3956 if (default_monitor)
3957 monitor_parse("vc:80Cx24C", "readline");
3958 if (default_virtcon)
3959 add_device_config(DEV_VIRTCON, "vc:80Cx24C");
3960 if (default_sclp) {
3961 add_device_config(DEV_SCLP, "vc:80Cx24C");
3965 if (display_type == DT_DEFAULT && !display_remote) {
3966 #if defined(CONFIG_GTK)
3967 display_type = DT_GTK;
3968 #elif defined(CONFIG_SDL) || defined(CONFIG_COCOA)
3969 display_type = DT_SDL;
3970 #elif defined(CONFIG_VNC)
3971 vnc_display = "localhost:0,to=99";
3972 show_vnc_port = 1;
3973 #else
3974 display_type = DT_NONE;
3975 #endif
3978 if ((no_frame || alt_grab || ctrl_grab) && display_type != DT_SDL) {
3979 fprintf(stderr, "-no-frame, -alt-grab and -ctrl-grab are only valid "
3980 "for SDL, ignoring option\n");
3982 if (no_quit && (display_type != DT_GTK && display_type != DT_SDL)) {
3983 fprintf(stderr, "-no-quit is only valid for GTK and SDL, "
3984 "ignoring option\n");
3987 #if defined(CONFIG_GTK)
3988 if (display_type == DT_GTK) {
3989 early_gtk_display_init();
3991 #endif
3993 socket_init();
3995 if (qemu_opts_foreach(qemu_find_opts("chardev"), chardev_init_func, NULL, 1) != 0)
3996 exit(1);
3997 #ifdef CONFIG_VIRTFS
3998 if (qemu_opts_foreach(qemu_find_opts("fsdev"), fsdev_init_func, NULL, 1) != 0) {
3999 exit(1);
4001 #endif
4003 if (pid_file && qemu_create_pidfile(pid_file) != 0) {
4004 os_pidfile_error();
4005 exit(1);
4008 /* store value for the future use */
4009 qemu_opt_set_number(qemu_find_opts_singleton("memory"), "size", ram_size);
4011 if (qemu_opts_foreach(qemu_find_opts("device"), device_help_func, NULL, 0)
4012 != 0) {
4013 exit(0);
4016 if (qemu_opts_foreach(qemu_find_opts("object"),
4017 object_create, NULL, 0) != 0) {
4018 exit(1);
4021 machine_opts = qemu_get_machine_opts();
4022 if (qemu_opt_foreach(machine_opts, machine_set_property, current_machine,
4023 1) < 0) {
4024 object_unref(OBJECT(current_machine));
4025 exit(1);
4028 configure_accelerator(current_machine);
4030 if (qtest_chrdev) {
4031 Error *local_err = NULL;
4032 qtest_init(qtest_chrdev, qtest_log, &local_err);
4033 if (local_err) {
4034 error_report("%s", error_get_pretty(local_err));
4035 error_free(local_err);
4036 exit(1);
4040 machine_opts = qemu_get_machine_opts();
4041 kernel_filename = qemu_opt_get(machine_opts, "kernel");
4042 initrd_filename = qemu_opt_get(machine_opts, "initrd");
4043 kernel_cmdline = qemu_opt_get(machine_opts, "append");
4044 bios_name = qemu_opt_get(machine_opts, "firmware");
4046 boot_order = machine_class->default_boot_order;
4047 opts = qemu_opts_find(qemu_find_opts("boot-opts"), NULL);
4048 if (opts) {
4049 char *normal_boot_order;
4050 const char *order, *once;
4052 order = qemu_opt_get(opts, "order");
4053 if (order) {
4054 validate_bootdevices(order);
4055 boot_order = order;
4058 once = qemu_opt_get(opts, "once");
4059 if (once) {
4060 validate_bootdevices(once);
4061 normal_boot_order = g_strdup(boot_order);
4062 boot_order = once;
4063 qemu_register_reset(restore_boot_order, normal_boot_order);
4066 boot_menu = qemu_opt_get_bool(opts, "menu", boot_menu);
4067 boot_strict = qemu_opt_get_bool(opts, "strict", false);
4070 if (!kernel_cmdline) {
4071 kernel_cmdline = "";
4072 current_machine->kernel_cmdline = (char *)kernel_cmdline;
4075 linux_boot = (kernel_filename != NULL);
4077 if (!linux_boot && *kernel_cmdline != '\0') {
4078 fprintf(stderr, "-append only allowed with -kernel option\n");
4079 exit(1);
4082 if (!linux_boot && initrd_filename != NULL) {
4083 fprintf(stderr, "-initrd only allowed with -kernel option\n");
4084 exit(1);
4087 if (!linux_boot && qemu_opt_get(machine_opts, "dtb")) {
4088 fprintf(stderr, "-dtb only allowed with -kernel option\n");
4089 exit(1);
4092 os_set_line_buffering();
4094 qemu_init_cpu_loop();
4095 qemu_mutex_lock_iothread();
4097 #ifdef CONFIG_SPICE
4098 /* spice needs the timers to be initialized by this point */
4099 qemu_spice_init();
4100 #endif
4102 cpu_ticks_init();
4103 if (icount_opts) {
4104 if (kvm_enabled() || xen_enabled()) {
4105 fprintf(stderr, "-icount is not allowed with kvm or xen\n");
4106 exit(1);
4108 configure_icount(icount_opts, &error_abort);
4109 qemu_opts_del(icount_opts);
4112 /* clean up network at qemu process termination */
4113 atexit(&net_cleanup);
4115 if (net_init_clients() < 0) {
4116 exit(1);
4119 #ifdef CONFIG_TPM
4120 if (tpm_init() < 0) {
4121 exit(1);
4123 #endif
4125 /* init the bluetooth world */
4126 if (foreach_device_config(DEV_BT, bt_parse))
4127 exit(1);
4129 if (!xen_enabled()) {
4130 /* On 32-bit hosts, QEMU is limited by virtual address space */
4131 if (ram_size > (2047 << 20) && HOST_LONG_BITS == 32) {
4132 fprintf(stderr, "qemu: at most 2047 MB RAM can be simulated\n");
4133 exit(1);
4137 blk_mig_init();
4138 ram_mig_init();
4140 /* If the currently selected machine wishes to override the units-per-bus
4141 * property of its default HBA interface type, do so now. */
4142 if (machine_class->units_per_default_bus) {
4143 override_max_devs(machine_class->block_default_type,
4144 machine_class->units_per_default_bus);
4147 /* open the virtual block devices */
4148 if (snapshot)
4149 qemu_opts_foreach(qemu_find_opts("drive"), drive_enable_snapshot, NULL, 0);
4150 if (qemu_opts_foreach(qemu_find_opts("drive"), drive_init_func,
4151 &machine_class->block_default_type, 1) != 0) {
4152 exit(1);
4155 default_drive(default_cdrom, snapshot, machine_class->block_default_type, 2,
4156 CDROM_OPTS);
4157 default_drive(default_floppy, snapshot, IF_FLOPPY, 0, FD_OPTS);
4158 default_drive(default_sdcard, snapshot, IF_SD, 0, SD_OPTS);
4160 if (qemu_opts_foreach(qemu_find_opts("numa"), numa_init_func,
4161 NULL, 1) != 0) {
4162 exit(1);
4165 set_numa_nodes();
4167 if (qemu_opts_foreach(qemu_find_opts("mon"), mon_init_func, NULL, 1) != 0) {
4168 exit(1);
4171 if (foreach_device_config(DEV_SERIAL, serial_parse) < 0)
4172 exit(1);
4173 if (foreach_device_config(DEV_PARALLEL, parallel_parse) < 0)
4174 exit(1);
4175 if (foreach_device_config(DEV_VIRTCON, virtcon_parse) < 0)
4176 exit(1);
4177 if (foreach_device_config(DEV_SCLP, sclp_parse) < 0) {
4178 exit(1);
4180 if (foreach_device_config(DEV_DEBUGCON, debugcon_parse) < 0)
4181 exit(1);
4183 /* If no default VGA is requested, the default is "none". */
4184 if (default_vga) {
4185 if (cirrus_vga_available()) {
4186 vga_model = "cirrus";
4187 } else if (vga_available()) {
4188 vga_model = "std";
4191 if (vga_model) {
4192 select_vgahw(vga_model);
4195 if (watchdog) {
4196 i = select_watchdog(watchdog);
4197 if (i > 0)
4198 exit (i == 1 ? 1 : 0);
4201 if (machine_class->compat_props) {
4202 qdev_prop_register_global_list(machine_class->compat_props);
4204 qemu_add_globals();
4206 qdev_machine_init();
4208 current_machine->ram_size = ram_size;
4209 current_machine->maxram_size = maxram_size;
4210 current_machine->ram_slots = ram_slots;
4211 current_machine->boot_order = boot_order;
4212 current_machine->cpu_model = cpu_model;
4214 machine_class->init(current_machine);
4216 realtime_init();
4218 audio_init();
4220 cpu_synchronize_all_post_init();
4222 set_numa_modes();
4224 /* init USB devices */
4225 if (usb_enabled(false)) {
4226 if (foreach_device_config(DEV_USB, usb_parse) < 0)
4227 exit(1);
4230 /* init generic devices */
4231 if (qemu_opts_foreach(qemu_find_opts("device"), device_init_func, NULL, 1) != 0)
4232 exit(1);
4234 /* Did we create any drives that we failed to create a device for? */
4235 drive_check_orphaned();
4237 net_check_clients();
4239 ds = init_displaystate();
4241 /* init local displays */
4242 switch (display_type) {
4243 case DT_NOGRAPHIC:
4244 (void)ds; /* avoid warning if no display is configured */
4245 break;
4246 #if defined(CONFIG_CURSES)
4247 case DT_CURSES:
4248 curses_display_init(ds, full_screen);
4249 break;
4250 #endif
4251 #if defined(CONFIG_SDL)
4252 case DT_SDL:
4253 sdl_display_init(ds, full_screen, no_frame);
4254 break;
4255 #elif defined(CONFIG_COCOA)
4256 case DT_SDL:
4257 cocoa_display_init(ds, full_screen);
4258 break;
4259 #endif
4260 #if defined(CONFIG_GTK)
4261 case DT_GTK:
4262 gtk_display_init(ds, full_screen, grab_on_hover);
4263 break;
4264 #endif
4265 default:
4266 break;
4269 /* must be after terminal init, SDL library changes signal handlers */
4270 os_setup_signal_handling();
4272 #ifdef CONFIG_VNC
4273 /* init remote displays */
4274 if (vnc_display) {
4275 Error *local_err = NULL;
4276 vnc_display_init(ds);
4277 vnc_display_open(ds, vnc_display, &local_err);
4278 if (local_err != NULL) {
4279 error_report("Failed to start VNC server on `%s': %s",
4280 vnc_display, error_get_pretty(local_err));
4281 error_free(local_err);
4282 exit(1);
4285 if (show_vnc_port) {
4286 printf("VNC server running on `%s'\n", vnc_display_local_addr(ds));
4289 #endif
4290 #ifdef CONFIG_SPICE
4291 if (using_spice) {
4292 qemu_spice_display_init();
4294 #endif
4296 if (foreach_device_config(DEV_GDB, gdbserver_start) < 0) {
4297 exit(1);
4300 qdev_machine_creation_done();
4302 if (rom_load_all() != 0) {
4303 fprintf(stderr, "rom loading failed\n");
4304 exit(1);
4307 /* TODO: once all bus devices are qdevified, this should be done
4308 * when bus is created by qdev.c */
4309 qemu_register_reset(qbus_reset_all_fn, sysbus_get_default());
4310 qemu_run_machine_init_done_notifiers();
4312 /* Done notifiers can load ROMs */
4313 rom_load_done();
4315 qemu_system_reset(VMRESET_SILENT);
4316 if (loadvm) {
4317 if (load_vmstate(loadvm) < 0) {
4318 autostart = 0;
4322 qdev_prop_check_globals();
4323 if (vmstate_dump_file) {
4324 /* dump and exit */
4325 dump_vmstate_json_to_file(vmstate_dump_file);
4326 return 0;
4329 if (incoming) {
4330 Error *local_err = NULL;
4331 qemu_start_incoming_migration(incoming, &local_err);
4332 if (local_err) {
4333 error_report("-incoming %s: %s", incoming,
4334 error_get_pretty(local_err));
4335 error_free(local_err);
4336 exit(1);
4338 } else if (autostart) {
4339 vm_start();
4342 os_setup_post();
4344 if (is_daemonized()) {
4345 if (!trace_init_backends(trace_events, trace_file)) {
4346 exit(1);
4350 main_loop();
4351 bdrv_close_all();
4352 pause_all_vcpus();
4353 res_free();
4354 #ifdef CONFIG_TPM
4355 tpm_cleanup();
4356 #endif
4358 return 0;