qmp: convert ACPI_DEVICE_OST event
[qemu/ar7.git] / vl.c
blob68380d7ff12c1d8f3c8b0a0e16a22980d2ddfc62
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 "hw/usb.h"
65 #include "hw/pcmcia.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 "qemu/cache-utils.h"
86 #include "sysemu/blockdev.h"
87 #include "hw/block/block.h"
88 #include "migration/block.h"
89 #include "sysemu/tpm.h"
90 #include "sysemu/dma.h"
91 #include "audio/audio.h"
92 #include "migration/migration.h"
93 #include "sysemu/kvm.h"
94 #include "qapi/qmp/qjson.h"
95 #include "qemu/option.h"
96 #include "qemu/config-file.h"
97 #include "qemu-options.h"
98 #include "qmp-commands.h"
99 #include "qemu/main-loop.h"
100 #ifdef CONFIG_VIRTFS
101 #include "fsdev/qemu-fsdev.h"
102 #endif
103 #include "sysemu/qtest.h"
105 #include "disas/disas.h"
108 #include "slirp/libslirp.h"
110 #include "trace.h"
111 #include "trace/control.h"
112 #include "qemu/queue.h"
113 #include "sysemu/cpus.h"
114 #include "sysemu/arch_init.h"
115 #include "qemu/osdep.h"
117 #include "ui/qemu-spice.h"
118 #include "qapi/string-input-visitor.h"
119 #include "qapi/opts-visitor.h"
120 #include "qom/object_interfaces.h"
121 #include "qapi-event.h"
123 #define DEFAULT_RAM_SIZE 128
125 #define MAX_VIRTIO_CONSOLES 1
126 #define MAX_SCLP_CONSOLES 1
128 static const char *data_dir[16];
129 static int data_dir_idx;
130 const char *bios_name = NULL;
131 enum vga_retrace_method vga_retrace_method = VGA_RETRACE_DUMB;
132 DisplayType display_type = DT_DEFAULT;
133 static int display_remote;
134 const char* keyboard_layout = NULL;
135 ram_addr_t ram_size;
136 const char *mem_path = NULL;
137 int mem_prealloc = 0; /* force preallocation of physical target memory */
138 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 static 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 typedef struct FWBootEntry FWBootEntry;
189 struct FWBootEntry {
190 QTAILQ_ENTRY(FWBootEntry) link;
191 int32_t bootindex;
192 DeviceState *dev;
193 char *suffix;
196 static QTAILQ_HEAD(, FWBootEntry) fw_boot_order =
197 QTAILQ_HEAD_INITIALIZER(fw_boot_order);
199 int nb_numa_nodes;
200 NodeInfo numa_info[MAX_NODES];
202 uint8_t qemu_uuid[16];
203 bool qemu_uuid_set;
205 static QEMUBootSetHandler *boot_set_handler;
206 static void *boot_set_opaque;
208 static NotifierList exit_notifiers =
209 NOTIFIER_LIST_INITIALIZER(exit_notifiers);
211 static NotifierList machine_init_done_notifiers =
212 NOTIFIER_LIST_INITIALIZER(machine_init_done_notifiers);
214 static bool tcg_allowed = true;
215 bool xen_allowed;
216 uint32_t xen_domid;
217 enum xen_mode xen_mode = XEN_EMULATE;
218 static int tcg_tb_size;
220 static int has_defaults = 1;
221 static int default_serial = 1;
222 static int default_parallel = 1;
223 static int default_virtcon = 1;
224 static int default_sclp = 1;
225 static int default_monitor = 1;
226 static int default_floppy = 1;
227 static int default_cdrom = 1;
228 static int default_sdcard = 1;
229 static int default_vga = 1;
231 static struct {
232 const char *driver;
233 int *flag;
234 } default_list[] = {
235 { .driver = "isa-serial", .flag = &default_serial },
236 { .driver = "isa-parallel", .flag = &default_parallel },
237 { .driver = "isa-fdc", .flag = &default_floppy },
238 { .driver = "ide-cd", .flag = &default_cdrom },
239 { .driver = "ide-hd", .flag = &default_cdrom },
240 { .driver = "ide-drive", .flag = &default_cdrom },
241 { .driver = "scsi-cd", .flag = &default_cdrom },
242 { .driver = "virtio-serial-pci", .flag = &default_virtcon },
243 { .driver = "virtio-serial-s390", .flag = &default_virtcon },
244 { .driver = "virtio-serial", .flag = &default_virtcon },
245 { .driver = "VGA", .flag = &default_vga },
246 { .driver = "isa-vga", .flag = &default_vga },
247 { .driver = "cirrus-vga", .flag = &default_vga },
248 { .driver = "isa-cirrus-vga", .flag = &default_vga },
249 { .driver = "vmware-svga", .flag = &default_vga },
250 { .driver = "qxl-vga", .flag = &default_vga },
253 static QemuOptsList qemu_rtc_opts = {
254 .name = "rtc",
255 .head = QTAILQ_HEAD_INITIALIZER(qemu_rtc_opts.head),
256 .desc = {
258 .name = "base",
259 .type = QEMU_OPT_STRING,
261 .name = "clock",
262 .type = QEMU_OPT_STRING,
264 .name = "driftfix",
265 .type = QEMU_OPT_STRING,
267 { /* end of list */ }
271 static QemuOptsList qemu_sandbox_opts = {
272 .name = "sandbox",
273 .implied_opt_name = "enable",
274 .head = QTAILQ_HEAD_INITIALIZER(qemu_sandbox_opts.head),
275 .desc = {
277 .name = "enable",
278 .type = QEMU_OPT_BOOL,
280 { /* end of list */ }
284 static QemuOptsList qemu_trace_opts = {
285 .name = "trace",
286 .implied_opt_name = "trace",
287 .head = QTAILQ_HEAD_INITIALIZER(qemu_trace_opts.head),
288 .desc = {
290 .name = "events",
291 .type = QEMU_OPT_STRING,
293 .name = "file",
294 .type = QEMU_OPT_STRING,
296 { /* end of list */ }
300 static QemuOptsList qemu_option_rom_opts = {
301 .name = "option-rom",
302 .implied_opt_name = "romfile",
303 .head = QTAILQ_HEAD_INITIALIZER(qemu_option_rom_opts.head),
304 .desc = {
306 .name = "bootindex",
307 .type = QEMU_OPT_NUMBER,
308 }, {
309 .name = "romfile",
310 .type = QEMU_OPT_STRING,
312 { /* end of list */ }
316 static QemuOptsList qemu_machine_opts = {
317 .name = "machine",
318 .implied_opt_name = "type",
319 .merge_lists = true,
320 .head = QTAILQ_HEAD_INITIALIZER(qemu_machine_opts.head),
321 .desc = {
323 .name = "type",
324 .type = QEMU_OPT_STRING,
325 .help = "emulated machine"
326 }, {
327 .name = "accel",
328 .type = QEMU_OPT_STRING,
329 .help = "accelerator list",
330 }, {
331 .name = "kernel_irqchip",
332 .type = QEMU_OPT_BOOL,
333 .help = "use KVM in-kernel irqchip",
334 }, {
335 .name = "kvm_shadow_mem",
336 .type = QEMU_OPT_SIZE,
337 .help = "KVM shadow MMU size",
338 }, {
339 .name = "kernel",
340 .type = QEMU_OPT_STRING,
341 .help = "Linux kernel image file",
342 }, {
343 .name = "initrd",
344 .type = QEMU_OPT_STRING,
345 .help = "Linux initial ramdisk file",
346 }, {
347 .name = "append",
348 .type = QEMU_OPT_STRING,
349 .help = "Linux kernel command line",
350 }, {
351 .name = "dtb",
352 .type = QEMU_OPT_STRING,
353 .help = "Linux kernel device tree file",
354 }, {
355 .name = "dumpdtb",
356 .type = QEMU_OPT_STRING,
357 .help = "Dump current dtb to a file and quit",
358 }, {
359 .name = "phandle_start",
360 .type = QEMU_OPT_NUMBER,
361 .help = "The first phandle ID we may generate dynamically",
362 }, {
363 .name = "dt_compatible",
364 .type = QEMU_OPT_STRING,
365 .help = "Overrides the \"compatible\" property of the dt root node",
366 }, {
367 .name = "dump-guest-core",
368 .type = QEMU_OPT_BOOL,
369 .help = "Include guest memory in a core dump",
370 }, {
371 .name = "mem-merge",
372 .type = QEMU_OPT_BOOL,
373 .help = "enable/disable memory merge support",
375 .name = "usb",
376 .type = QEMU_OPT_BOOL,
377 .help = "Set on/off to enable/disable usb",
379 .name = "firmware",
380 .type = QEMU_OPT_STRING,
381 .help = "firmware image",
383 .name = "kvm-type",
384 .type = QEMU_OPT_STRING,
385 .help = "Specifies the KVM virtualization mode (HV, PR)",
387 { /* End of list */ }
391 static QemuOptsList qemu_boot_opts = {
392 .name = "boot-opts",
393 .implied_opt_name = "order",
394 .merge_lists = true,
395 .head = QTAILQ_HEAD_INITIALIZER(qemu_boot_opts.head),
396 .desc = {
398 .name = "order",
399 .type = QEMU_OPT_STRING,
400 }, {
401 .name = "once",
402 .type = QEMU_OPT_STRING,
403 }, {
404 .name = "menu",
405 .type = QEMU_OPT_BOOL,
406 }, {
407 .name = "splash",
408 .type = QEMU_OPT_STRING,
409 }, {
410 .name = "splash-time",
411 .type = QEMU_OPT_STRING,
412 }, {
413 .name = "reboot-timeout",
414 .type = QEMU_OPT_STRING,
415 }, {
416 .name = "strict",
417 .type = QEMU_OPT_BOOL,
419 { /*End of list */ }
423 static QemuOptsList qemu_add_fd_opts = {
424 .name = "add-fd",
425 .head = QTAILQ_HEAD_INITIALIZER(qemu_add_fd_opts.head),
426 .desc = {
428 .name = "fd",
429 .type = QEMU_OPT_NUMBER,
430 .help = "file descriptor of which a duplicate is added to fd set",
432 .name = "set",
433 .type = QEMU_OPT_NUMBER,
434 .help = "ID of the fd set to add fd to",
436 .name = "opaque",
437 .type = QEMU_OPT_STRING,
438 .help = "free-form string used to describe fd",
440 { /* end of list */ }
444 static QemuOptsList qemu_object_opts = {
445 .name = "object",
446 .implied_opt_name = "qom-type",
447 .head = QTAILQ_HEAD_INITIALIZER(qemu_object_opts.head),
448 .desc = {
453 static QemuOptsList qemu_tpmdev_opts = {
454 .name = "tpmdev",
455 .implied_opt_name = "type",
456 .head = QTAILQ_HEAD_INITIALIZER(qemu_tpmdev_opts.head),
457 .desc = {
458 /* options are defined in the TPM backends */
459 { /* end of list */ }
463 static QemuOptsList qemu_realtime_opts = {
464 .name = "realtime",
465 .head = QTAILQ_HEAD_INITIALIZER(qemu_realtime_opts.head),
466 .desc = {
468 .name = "mlock",
469 .type = QEMU_OPT_BOOL,
471 { /* end of list */ }
475 static QemuOptsList qemu_msg_opts = {
476 .name = "msg",
477 .head = QTAILQ_HEAD_INITIALIZER(qemu_msg_opts.head),
478 .desc = {
480 .name = "timestamp",
481 .type = QEMU_OPT_BOOL,
483 { /* end of list */ }
487 static QemuOptsList qemu_name_opts = {
488 .name = "name",
489 .implied_opt_name = "guest",
490 .merge_lists = true,
491 .head = QTAILQ_HEAD_INITIALIZER(qemu_name_opts.head),
492 .desc = {
494 .name = "guest",
495 .type = QEMU_OPT_STRING,
496 .help = "Sets the name of the guest.\n"
497 "This name will be displayed in the SDL window caption.\n"
498 "The name will also be used for the VNC server",
499 }, {
500 .name = "process",
501 .type = QEMU_OPT_STRING,
502 .help = "Sets the name of the QEMU process, as shown in top etc",
503 }, {
504 .name = "debug-threads",
505 .type = QEMU_OPT_BOOL,
506 .help = "When enabled, name the individual threads; defaults off.\n"
507 "NOTE: The thread names are for debugging and not a\n"
508 "stable API.",
510 { /* End of list */ }
514 static QemuOptsList qemu_mem_opts = {
515 .name = "memory",
516 .implied_opt_name = "size",
517 .head = QTAILQ_HEAD_INITIALIZER(qemu_mem_opts.head),
518 .merge_lists = true,
519 .desc = {
521 .name = "size",
522 .type = QEMU_OPT_SIZE,
525 .name = "slots",
526 .type = QEMU_OPT_NUMBER,
529 .name = "maxmem",
530 .type = QEMU_OPT_SIZE,
532 { /* end of list */ }
537 * Get machine options
539 * Returns: machine options (never null).
541 QemuOpts *qemu_get_machine_opts(void)
543 return qemu_find_opts_singleton("machine");
546 const char *qemu_get_vm_name(void)
548 return qemu_name;
551 static void res_free(void)
553 if (boot_splash_filedata != NULL) {
554 g_free(boot_splash_filedata);
555 boot_splash_filedata = NULL;
559 static int default_driver_check(QemuOpts *opts, void *opaque)
561 const char *driver = qemu_opt_get(opts, "driver");
562 int i;
564 if (!driver)
565 return 0;
566 for (i = 0; i < ARRAY_SIZE(default_list); i++) {
567 if (strcmp(default_list[i].driver, driver) != 0)
568 continue;
569 *(default_list[i].flag) = 0;
571 return 0;
574 /***********************************************************/
575 /* QEMU state */
577 static RunState current_run_state = RUN_STATE_PRELAUNCH;
579 /* We use RUN_STATE_MAX but any invalid value will do */
580 static RunState vmstop_requested = RUN_STATE_MAX;
581 static QemuMutex vmstop_lock;
583 typedef struct {
584 RunState from;
585 RunState to;
586 } RunStateTransition;
588 static const RunStateTransition runstate_transitions_def[] = {
589 /* from -> to */
590 { RUN_STATE_DEBUG, RUN_STATE_RUNNING },
591 { RUN_STATE_DEBUG, RUN_STATE_FINISH_MIGRATE },
593 { RUN_STATE_INMIGRATE, RUN_STATE_RUNNING },
594 { RUN_STATE_INMIGRATE, RUN_STATE_PAUSED },
596 { RUN_STATE_INTERNAL_ERROR, RUN_STATE_PAUSED },
597 { RUN_STATE_INTERNAL_ERROR, RUN_STATE_FINISH_MIGRATE },
599 { RUN_STATE_IO_ERROR, RUN_STATE_RUNNING },
600 { RUN_STATE_IO_ERROR, RUN_STATE_FINISH_MIGRATE },
602 { RUN_STATE_PAUSED, RUN_STATE_RUNNING },
603 { RUN_STATE_PAUSED, RUN_STATE_FINISH_MIGRATE },
605 { RUN_STATE_POSTMIGRATE, RUN_STATE_RUNNING },
606 { RUN_STATE_POSTMIGRATE, RUN_STATE_FINISH_MIGRATE },
608 { RUN_STATE_PRELAUNCH, RUN_STATE_RUNNING },
609 { RUN_STATE_PRELAUNCH, RUN_STATE_FINISH_MIGRATE },
610 { RUN_STATE_PRELAUNCH, RUN_STATE_INMIGRATE },
612 { RUN_STATE_FINISH_MIGRATE, RUN_STATE_RUNNING },
613 { RUN_STATE_FINISH_MIGRATE, RUN_STATE_POSTMIGRATE },
615 { RUN_STATE_RESTORE_VM, RUN_STATE_RUNNING },
617 { RUN_STATE_RUNNING, RUN_STATE_DEBUG },
618 { RUN_STATE_RUNNING, RUN_STATE_INTERNAL_ERROR },
619 { RUN_STATE_RUNNING, RUN_STATE_IO_ERROR },
620 { RUN_STATE_RUNNING, RUN_STATE_PAUSED },
621 { RUN_STATE_RUNNING, RUN_STATE_FINISH_MIGRATE },
622 { RUN_STATE_RUNNING, RUN_STATE_RESTORE_VM },
623 { RUN_STATE_RUNNING, RUN_STATE_SAVE_VM },
624 { RUN_STATE_RUNNING, RUN_STATE_SHUTDOWN },
625 { RUN_STATE_RUNNING, RUN_STATE_WATCHDOG },
626 { RUN_STATE_RUNNING, RUN_STATE_GUEST_PANICKED },
628 { RUN_STATE_SAVE_VM, RUN_STATE_RUNNING },
630 { RUN_STATE_SHUTDOWN, RUN_STATE_PAUSED },
631 { RUN_STATE_SHUTDOWN, RUN_STATE_FINISH_MIGRATE },
633 { RUN_STATE_DEBUG, RUN_STATE_SUSPENDED },
634 { RUN_STATE_RUNNING, RUN_STATE_SUSPENDED },
635 { RUN_STATE_SUSPENDED, RUN_STATE_RUNNING },
636 { RUN_STATE_SUSPENDED, RUN_STATE_FINISH_MIGRATE },
638 { RUN_STATE_WATCHDOG, RUN_STATE_RUNNING },
639 { RUN_STATE_WATCHDOG, RUN_STATE_FINISH_MIGRATE },
641 { RUN_STATE_GUEST_PANICKED, RUN_STATE_RUNNING },
642 { RUN_STATE_GUEST_PANICKED, RUN_STATE_FINISH_MIGRATE },
644 { RUN_STATE_MAX, RUN_STATE_MAX },
647 static bool runstate_valid_transitions[RUN_STATE_MAX][RUN_STATE_MAX];
649 bool runstate_check(RunState state)
651 return current_run_state == state;
654 static void runstate_init(void)
656 const RunStateTransition *p;
658 memset(&runstate_valid_transitions, 0, sizeof(runstate_valid_transitions));
659 for (p = &runstate_transitions_def[0]; p->from != RUN_STATE_MAX; p++) {
660 runstate_valid_transitions[p->from][p->to] = true;
663 qemu_mutex_init(&vmstop_lock);
666 /* This function will abort() on invalid state transitions */
667 void runstate_set(RunState new_state)
669 assert(new_state < RUN_STATE_MAX);
671 if (!runstate_valid_transitions[current_run_state][new_state]) {
672 fprintf(stderr, "ERROR: invalid runstate transition: '%s' -> '%s'\n",
673 RunState_lookup[current_run_state],
674 RunState_lookup[new_state]);
675 abort();
677 trace_runstate_set(new_state);
678 current_run_state = new_state;
681 int runstate_is_running(void)
683 return runstate_check(RUN_STATE_RUNNING);
686 bool runstate_needs_reset(void)
688 return runstate_check(RUN_STATE_INTERNAL_ERROR) ||
689 runstate_check(RUN_STATE_SHUTDOWN);
692 StatusInfo *qmp_query_status(Error **errp)
694 StatusInfo *info = g_malloc0(sizeof(*info));
696 info->running = runstate_is_running();
697 info->singlestep = singlestep;
698 info->status = current_run_state;
700 return info;
703 static bool qemu_vmstop_requested(RunState *r)
705 qemu_mutex_lock(&vmstop_lock);
706 *r = vmstop_requested;
707 vmstop_requested = RUN_STATE_MAX;
708 qemu_mutex_unlock(&vmstop_lock);
709 return *r < RUN_STATE_MAX;
712 void qemu_system_vmstop_request_prepare(void)
714 qemu_mutex_lock(&vmstop_lock);
717 void qemu_system_vmstop_request(RunState state)
719 vmstop_requested = state;
720 qemu_mutex_unlock(&vmstop_lock);
721 qemu_notify_event();
724 void vm_start(void)
726 RunState requested;
728 qemu_vmstop_requested(&requested);
729 if (runstate_is_running() && requested == RUN_STATE_MAX) {
730 return;
733 /* Ensure that a STOP/RESUME pair of events is emitted if a
734 * vmstop request was pending. The BLOCK_IO_ERROR event, for
735 * example, according to documentation is always followed by
736 * the STOP event.
738 if (runstate_is_running()) {
739 qapi_event_send_stop(&error_abort);
740 } else {
741 cpu_enable_ticks();
742 runstate_set(RUN_STATE_RUNNING);
743 vm_state_notify(1, RUN_STATE_RUNNING);
744 resume_all_vcpus();
747 qapi_event_send_resume(&error_abort);
751 /***********************************************************/
752 /* real time host monotonic timer */
754 /***********************************************************/
755 /* host time/date access */
756 void qemu_get_timedate(struct tm *tm, int offset)
758 time_t ti;
760 time(&ti);
761 ti += offset;
762 if (rtc_date_offset == -1) {
763 if (rtc_utc)
764 gmtime_r(&ti, tm);
765 else
766 localtime_r(&ti, tm);
767 } else {
768 ti -= rtc_date_offset;
769 gmtime_r(&ti, tm);
773 int qemu_timedate_diff(struct tm *tm)
775 time_t seconds;
777 if (rtc_date_offset == -1)
778 if (rtc_utc)
779 seconds = mktimegm(tm);
780 else {
781 struct tm tmp = *tm;
782 tmp.tm_isdst = -1; /* use timezone to figure it out */
783 seconds = mktime(&tmp);
785 else
786 seconds = mktimegm(tm) + rtc_date_offset;
788 return seconds - time(NULL);
791 static void configure_rtc_date_offset(const char *startdate, int legacy)
793 time_t rtc_start_date;
794 struct tm tm;
796 if (!strcmp(startdate, "now") && legacy) {
797 rtc_date_offset = -1;
798 } else {
799 if (sscanf(startdate, "%d-%d-%dT%d:%d:%d",
800 &tm.tm_year,
801 &tm.tm_mon,
802 &tm.tm_mday,
803 &tm.tm_hour,
804 &tm.tm_min,
805 &tm.tm_sec) == 6) {
806 /* OK */
807 } else if (sscanf(startdate, "%d-%d-%d",
808 &tm.tm_year,
809 &tm.tm_mon,
810 &tm.tm_mday) == 3) {
811 tm.tm_hour = 0;
812 tm.tm_min = 0;
813 tm.tm_sec = 0;
814 } else {
815 goto date_fail;
817 tm.tm_year -= 1900;
818 tm.tm_mon--;
819 rtc_start_date = mktimegm(&tm);
820 if (rtc_start_date == -1) {
821 date_fail:
822 fprintf(stderr, "Invalid date format. Valid formats are:\n"
823 "'2006-06-17T16:01:21' or '2006-06-17'\n");
824 exit(1);
826 rtc_date_offset = time(NULL) - rtc_start_date;
830 static void configure_rtc(QemuOpts *opts)
832 const char *value;
834 value = qemu_opt_get(opts, "base");
835 if (value) {
836 if (!strcmp(value, "utc")) {
837 rtc_utc = 1;
838 } else if (!strcmp(value, "localtime")) {
839 rtc_utc = 0;
840 } else {
841 configure_rtc_date_offset(value, 0);
844 value = qemu_opt_get(opts, "clock");
845 if (value) {
846 if (!strcmp(value, "host")) {
847 rtc_clock = QEMU_CLOCK_HOST;
848 } else if (!strcmp(value, "rt")) {
849 rtc_clock = QEMU_CLOCK_REALTIME;
850 } else if (!strcmp(value, "vm")) {
851 rtc_clock = QEMU_CLOCK_VIRTUAL;
852 } else {
853 fprintf(stderr, "qemu: invalid option value '%s'\n", value);
854 exit(1);
857 value = qemu_opt_get(opts, "driftfix");
858 if (value) {
859 if (!strcmp(value, "slew")) {
860 static GlobalProperty slew_lost_ticks[] = {
862 .driver = "mc146818rtc",
863 .property = "lost_tick_policy",
864 .value = "slew",
866 { /* end of list */ }
869 qdev_prop_register_global_list(slew_lost_ticks);
870 } else if (!strcmp(value, "none")) {
871 /* discard is default */
872 } else {
873 fprintf(stderr, "qemu: invalid option value '%s'\n", value);
874 exit(1);
879 /***********************************************************/
880 /* Bluetooth support */
881 static int nb_hcis;
882 static int cur_hci;
883 static struct HCIInfo *hci_table[MAX_NICS];
885 struct HCIInfo *qemu_next_hci(void)
887 if (cur_hci == nb_hcis)
888 return &null_hci;
890 return hci_table[cur_hci++];
893 static int bt_hci_parse(const char *str)
895 struct HCIInfo *hci;
896 bdaddr_t bdaddr;
898 if (nb_hcis >= MAX_NICS) {
899 fprintf(stderr, "qemu: Too many bluetooth HCIs (max %i).\n", MAX_NICS);
900 return -1;
903 hci = hci_init(str);
904 if (!hci)
905 return -1;
907 bdaddr.b[0] = 0x52;
908 bdaddr.b[1] = 0x54;
909 bdaddr.b[2] = 0x00;
910 bdaddr.b[3] = 0x12;
911 bdaddr.b[4] = 0x34;
912 bdaddr.b[5] = 0x56 + nb_hcis;
913 hci->bdaddr_set(hci, bdaddr.b);
915 hci_table[nb_hcis++] = hci;
917 return 0;
920 static void bt_vhci_add(int vlan_id)
922 struct bt_scatternet_s *vlan = qemu_find_bt_vlan(vlan_id);
924 if (!vlan->slave)
925 fprintf(stderr, "qemu: warning: adding a VHCI to "
926 "an empty scatternet %i\n", vlan_id);
928 bt_vhci_init(bt_new_hci(vlan));
931 static struct bt_device_s *bt_device_add(const char *opt)
933 struct bt_scatternet_s *vlan;
934 int vlan_id = 0;
935 char *endp = strstr(opt, ",vlan=");
936 int len = (endp ? endp - opt : strlen(opt)) + 1;
937 char devname[10];
939 pstrcpy(devname, MIN(sizeof(devname), len), opt);
941 if (endp) {
942 vlan_id = strtol(endp + 6, &endp, 0);
943 if (*endp) {
944 fprintf(stderr, "qemu: unrecognised bluetooth vlan Id\n");
945 return 0;
949 vlan = qemu_find_bt_vlan(vlan_id);
951 if (!vlan->slave)
952 fprintf(stderr, "qemu: warning: adding a slave device to "
953 "an empty scatternet %i\n", vlan_id);
955 if (!strcmp(devname, "keyboard"))
956 return bt_keyboard_init(vlan);
958 fprintf(stderr, "qemu: unsupported bluetooth device `%s'\n", devname);
959 return 0;
962 static int bt_parse(const char *opt)
964 const char *endp, *p;
965 int vlan;
967 if (strstart(opt, "hci", &endp)) {
968 if (!*endp || *endp == ',') {
969 if (*endp)
970 if (!strstart(endp, ",vlan=", 0))
971 opt = endp + 1;
973 return bt_hci_parse(opt);
975 } else if (strstart(opt, "vhci", &endp)) {
976 if (!*endp || *endp == ',') {
977 if (*endp) {
978 if (strstart(endp, ",vlan=", &p)) {
979 vlan = strtol(p, (char **) &endp, 0);
980 if (*endp) {
981 fprintf(stderr, "qemu: bad scatternet '%s'\n", p);
982 return 1;
984 } else {
985 fprintf(stderr, "qemu: bad parameter '%s'\n", endp + 1);
986 return 1;
988 } else
989 vlan = 0;
991 bt_vhci_add(vlan);
992 return 0;
994 } else if (strstart(opt, "device:", &endp))
995 return !bt_device_add(endp);
997 fprintf(stderr, "qemu: bad bluetooth parameter '%s'\n", opt);
998 return 1;
1001 static int parse_sandbox(QemuOpts *opts, void *opaque)
1003 /* FIXME: change this to true for 1.3 */
1004 if (qemu_opt_get_bool(opts, "enable", false)) {
1005 #ifdef CONFIG_SECCOMP
1006 if (seccomp_start() < 0) {
1007 qerror_report(ERROR_CLASS_GENERIC_ERROR,
1008 "failed to install seccomp syscall filter in the kernel");
1009 return -1;
1011 #else
1012 qerror_report(ERROR_CLASS_GENERIC_ERROR,
1013 "sandboxing request but seccomp is not compiled into this build");
1014 return -1;
1015 #endif
1018 return 0;
1021 static int parse_name(QemuOpts *opts, void *opaque)
1023 const char *proc_name;
1025 if (qemu_opt_get(opts, "debug-threads")) {
1026 qemu_thread_naming(qemu_opt_get_bool(opts, "debug-threads", false));
1028 qemu_name = qemu_opt_get(opts, "guest");
1030 proc_name = qemu_opt_get(opts, "process");
1031 if (proc_name) {
1032 os_set_proc_name(proc_name);
1035 return 0;
1038 bool usb_enabled(bool default_usb)
1040 return qemu_opt_get_bool(qemu_get_machine_opts(), "usb",
1041 has_defaults && default_usb);
1044 #ifndef _WIN32
1045 static int parse_add_fd(QemuOpts *opts, void *opaque)
1047 int fd, dupfd, flags;
1048 int64_t fdset_id;
1049 const char *fd_opaque = NULL;
1051 fd = qemu_opt_get_number(opts, "fd", -1);
1052 fdset_id = qemu_opt_get_number(opts, "set", -1);
1053 fd_opaque = qemu_opt_get(opts, "opaque");
1055 if (fd < 0) {
1056 qerror_report(ERROR_CLASS_GENERIC_ERROR,
1057 "fd option is required and must be non-negative");
1058 return -1;
1061 if (fd <= STDERR_FILENO) {
1062 qerror_report(ERROR_CLASS_GENERIC_ERROR,
1063 "fd cannot be a standard I/O stream");
1064 return -1;
1068 * All fds inherited across exec() necessarily have FD_CLOEXEC
1069 * clear, while qemu sets FD_CLOEXEC on all other fds used internally.
1071 flags = fcntl(fd, F_GETFD);
1072 if (flags == -1 || (flags & FD_CLOEXEC)) {
1073 qerror_report(ERROR_CLASS_GENERIC_ERROR,
1074 "fd is not valid or already in use");
1075 return -1;
1078 if (fdset_id < 0) {
1079 qerror_report(ERROR_CLASS_GENERIC_ERROR,
1080 "set option is required and must be non-negative");
1081 return -1;
1084 #ifdef F_DUPFD_CLOEXEC
1085 dupfd = fcntl(fd, F_DUPFD_CLOEXEC, 0);
1086 #else
1087 dupfd = dup(fd);
1088 if (dupfd != -1) {
1089 qemu_set_cloexec(dupfd);
1091 #endif
1092 if (dupfd == -1) {
1093 qerror_report(ERROR_CLASS_GENERIC_ERROR,
1094 "Error duplicating fd: %s", strerror(errno));
1095 return -1;
1098 /* add the duplicate fd, and optionally the opaque string, to the fd set */
1099 monitor_fdset_add_fd(dupfd, true, fdset_id, fd_opaque ? true : false,
1100 fd_opaque, NULL);
1102 return 0;
1105 static int cleanup_add_fd(QemuOpts *opts, void *opaque)
1107 int fd;
1109 fd = qemu_opt_get_number(opts, "fd", -1);
1110 close(fd);
1112 return 0;
1114 #endif
1116 /***********************************************************/
1117 /* QEMU Block devices */
1119 #define HD_OPTS "media=disk"
1120 #define CDROM_OPTS "media=cdrom"
1121 #define FD_OPTS ""
1122 #define PFLASH_OPTS ""
1123 #define MTD_OPTS ""
1124 #define SD_OPTS ""
1126 static int drive_init_func(QemuOpts *opts, void *opaque)
1128 BlockInterfaceType *block_default_type = opaque;
1130 return drive_new(opts, *block_default_type) == NULL;
1133 static int drive_enable_snapshot(QemuOpts *opts, void *opaque)
1135 if (NULL == qemu_opt_get(opts, "snapshot")) {
1136 qemu_opt_set(opts, "snapshot", "on");
1138 return 0;
1141 static void default_drive(int enable, int snapshot, BlockInterfaceType type,
1142 int index, const char *optstr)
1144 QemuOpts *opts;
1146 if (!enable || drive_get_by_index(type, index)) {
1147 return;
1150 opts = drive_add(type, index, NULL, optstr);
1151 if (snapshot) {
1152 drive_enable_snapshot(opts, NULL);
1154 if (!drive_new(opts, type)) {
1155 exit(1);
1159 void qemu_register_boot_set(QEMUBootSetHandler *func, void *opaque)
1161 boot_set_handler = func;
1162 boot_set_opaque = opaque;
1165 int qemu_boot_set(const char *boot_order)
1167 if (!boot_set_handler) {
1168 return -EINVAL;
1170 return boot_set_handler(boot_set_opaque, boot_order);
1173 static void validate_bootdevices(const char *devices)
1175 /* We just do some generic consistency checks */
1176 const char *p;
1177 int bitmap = 0;
1179 for (p = devices; *p != '\0'; p++) {
1180 /* Allowed boot devices are:
1181 * a-b: floppy disk drives
1182 * c-f: IDE disk drives
1183 * g-m: machine implementation dependent drives
1184 * n-p: network devices
1185 * It's up to each machine implementation to check if the given boot
1186 * devices match the actual hardware implementation and firmware
1187 * features.
1189 if (*p < 'a' || *p > 'p') {
1190 fprintf(stderr, "Invalid boot device '%c'\n", *p);
1191 exit(1);
1193 if (bitmap & (1 << (*p - 'a'))) {
1194 fprintf(stderr, "Boot device '%c' was given twice\n", *p);
1195 exit(1);
1197 bitmap |= 1 << (*p - 'a');
1201 static void restore_boot_order(void *opaque)
1203 char *normal_boot_order = opaque;
1204 static int first = 1;
1206 /* Restore boot order and remove ourselves after the first boot */
1207 if (first) {
1208 first = 0;
1209 return;
1212 qemu_boot_set(normal_boot_order);
1214 qemu_unregister_reset(restore_boot_order, normal_boot_order);
1215 g_free(normal_boot_order);
1218 void add_boot_device_path(int32_t bootindex, DeviceState *dev,
1219 const char *suffix)
1221 FWBootEntry *node, *i;
1223 if (bootindex < 0) {
1224 return;
1227 assert(dev != NULL || suffix != NULL);
1229 node = g_malloc0(sizeof(FWBootEntry));
1230 node->bootindex = bootindex;
1231 node->suffix = g_strdup(suffix);
1232 node->dev = dev;
1234 QTAILQ_FOREACH(i, &fw_boot_order, link) {
1235 if (i->bootindex == bootindex) {
1236 fprintf(stderr, "Two devices with same boot index %d\n", bootindex);
1237 exit(1);
1238 } else if (i->bootindex < bootindex) {
1239 continue;
1241 QTAILQ_INSERT_BEFORE(i, node, link);
1242 return;
1244 QTAILQ_INSERT_TAIL(&fw_boot_order, node, link);
1247 DeviceState *get_boot_device(uint32_t position)
1249 uint32_t counter = 0;
1250 FWBootEntry *i = NULL;
1251 DeviceState *res = NULL;
1253 if (!QTAILQ_EMPTY(&fw_boot_order)) {
1254 QTAILQ_FOREACH(i, &fw_boot_order, link) {
1255 if (counter == position) {
1256 res = i->dev;
1257 break;
1259 counter++;
1262 return res;
1266 * This function returns null terminated string that consist of new line
1267 * separated device paths.
1269 * memory pointed by "size" is assigned total length of the array in bytes
1272 char *get_boot_devices_list(size_t *size, bool ignore_suffixes)
1274 FWBootEntry *i;
1275 size_t total = 0;
1276 char *list = NULL;
1278 QTAILQ_FOREACH(i, &fw_boot_order, link) {
1279 char *devpath = NULL, *bootpath;
1280 size_t len;
1282 if (i->dev) {
1283 devpath = qdev_get_fw_dev_path(i->dev);
1284 assert(devpath);
1287 if (i->suffix && !ignore_suffixes && devpath) {
1288 size_t bootpathlen = strlen(devpath) + strlen(i->suffix) + 1;
1290 bootpath = g_malloc(bootpathlen);
1291 snprintf(bootpath, bootpathlen, "%s%s", devpath, i->suffix);
1292 g_free(devpath);
1293 } else if (devpath) {
1294 bootpath = devpath;
1295 } else if (!ignore_suffixes) {
1296 assert(i->suffix);
1297 bootpath = g_strdup(i->suffix);
1298 } else {
1299 bootpath = g_strdup("");
1302 if (total) {
1303 list[total-1] = '\n';
1305 len = strlen(bootpath) + 1;
1306 list = g_realloc(list, total + len);
1307 memcpy(&list[total], bootpath, len);
1308 total += len;
1309 g_free(bootpath);
1312 *size = total;
1314 if (boot_strict && *size > 0) {
1315 list[total-1] = '\n';
1316 list = g_realloc(list, total + 5);
1317 memcpy(&list[total], "HALT", 5);
1318 *size = total + 5;
1320 return list;
1323 static QemuOptsList qemu_smp_opts = {
1324 .name = "smp-opts",
1325 .implied_opt_name = "cpus",
1326 .merge_lists = true,
1327 .head = QTAILQ_HEAD_INITIALIZER(qemu_smp_opts.head),
1328 .desc = {
1330 .name = "cpus",
1331 .type = QEMU_OPT_NUMBER,
1332 }, {
1333 .name = "sockets",
1334 .type = QEMU_OPT_NUMBER,
1335 }, {
1336 .name = "cores",
1337 .type = QEMU_OPT_NUMBER,
1338 }, {
1339 .name = "threads",
1340 .type = QEMU_OPT_NUMBER,
1341 }, {
1342 .name = "maxcpus",
1343 .type = QEMU_OPT_NUMBER,
1345 { /*End of list */ }
1349 static void smp_parse(QemuOpts *opts)
1351 if (opts) {
1353 unsigned cpus = qemu_opt_get_number(opts, "cpus", 0);
1354 unsigned sockets = qemu_opt_get_number(opts, "sockets", 0);
1355 unsigned cores = qemu_opt_get_number(opts, "cores", 0);
1356 unsigned threads = qemu_opt_get_number(opts, "threads", 0);
1358 /* compute missing values, prefer sockets over cores over threads */
1359 if (cpus == 0 || sockets == 0) {
1360 sockets = sockets > 0 ? sockets : 1;
1361 cores = cores > 0 ? cores : 1;
1362 threads = threads > 0 ? threads : 1;
1363 if (cpus == 0) {
1364 cpus = cores * threads * sockets;
1366 } else {
1367 if (cores == 0) {
1368 threads = threads > 0 ? threads : 1;
1369 cores = cpus / (sockets * threads);
1370 } else {
1371 threads = cpus / (cores * sockets);
1375 max_cpus = qemu_opt_get_number(opts, "maxcpus", 0);
1377 smp_cpus = cpus;
1378 smp_cores = cores > 0 ? cores : 1;
1379 smp_threads = threads > 0 ? threads : 1;
1383 if (max_cpus == 0) {
1384 max_cpus = smp_cpus;
1387 if (max_cpus > MAX_CPUMASK_BITS) {
1388 fprintf(stderr, "Unsupported number of maxcpus\n");
1389 exit(1);
1391 if (max_cpus < smp_cpus) {
1392 fprintf(stderr, "maxcpus must be equal to or greater than smp\n");
1393 exit(1);
1398 static void configure_realtime(QemuOpts *opts)
1400 bool enable_mlock;
1402 enable_mlock = qemu_opt_get_bool(opts, "mlock", true);
1404 if (enable_mlock) {
1405 if (os_mlock() < 0) {
1406 fprintf(stderr, "qemu: locking memory failed\n");
1407 exit(1);
1413 static void configure_msg(QemuOpts *opts)
1415 enable_timestamp_msg = qemu_opt_get_bool(opts, "timestamp", true);
1418 /***********************************************************/
1419 /* USB devices */
1421 static int usb_device_add(const char *devname)
1423 USBDevice *dev = NULL;
1424 #ifndef CONFIG_LINUX
1425 const char *p;
1426 #endif
1428 if (!usb_enabled(false)) {
1429 return -1;
1432 /* drivers with .usbdevice_name entry in USBDeviceInfo */
1433 dev = usbdevice_create(devname);
1434 if (dev)
1435 goto done;
1437 /* the other ones */
1438 #ifndef CONFIG_LINUX
1439 /* only the linux version is qdev-ified, usb-bsd still needs this */
1440 if (strstart(devname, "host:", &p)) {
1441 dev = usb_host_device_open(usb_bus_find(-1), p);
1443 #endif
1444 if (!dev)
1445 return -1;
1447 done:
1448 return 0;
1451 static int usb_device_del(const char *devname)
1453 int bus_num, addr;
1454 const char *p;
1456 if (strstart(devname, "host:", &p)) {
1457 return -1;
1460 if (!usb_enabled(false)) {
1461 return -1;
1464 p = strchr(devname, '.');
1465 if (!p)
1466 return -1;
1467 bus_num = strtoul(devname, NULL, 0);
1468 addr = strtoul(p + 1, NULL, 0);
1470 return usb_device_delete_addr(bus_num, addr);
1473 static int usb_parse(const char *cmdline)
1475 int r;
1476 r = usb_device_add(cmdline);
1477 if (r < 0) {
1478 fprintf(stderr, "qemu: could not add USB device '%s'\n", cmdline);
1480 return r;
1483 void do_usb_add(Monitor *mon, const QDict *qdict)
1485 const char *devname = qdict_get_str(qdict, "devname");
1486 if (usb_device_add(devname) < 0) {
1487 error_report("could not add USB device '%s'", devname);
1491 void do_usb_del(Monitor *mon, const QDict *qdict)
1493 const char *devname = qdict_get_str(qdict, "devname");
1494 if (usb_device_del(devname) < 0) {
1495 error_report("could not delete USB device '%s'", devname);
1499 /***********************************************************/
1500 /* PCMCIA/Cardbus */
1502 static struct pcmcia_socket_entry_s {
1503 PCMCIASocket *socket;
1504 struct pcmcia_socket_entry_s *next;
1505 } *pcmcia_sockets = 0;
1507 void pcmcia_socket_register(PCMCIASocket *socket)
1509 struct pcmcia_socket_entry_s *entry;
1511 entry = g_malloc(sizeof(struct pcmcia_socket_entry_s));
1512 entry->socket = socket;
1513 entry->next = pcmcia_sockets;
1514 pcmcia_sockets = entry;
1517 void pcmcia_socket_unregister(PCMCIASocket *socket)
1519 struct pcmcia_socket_entry_s *entry, **ptr;
1521 ptr = &pcmcia_sockets;
1522 for (entry = *ptr; entry; ptr = &entry->next, entry = *ptr)
1523 if (entry->socket == socket) {
1524 *ptr = entry->next;
1525 g_free(entry);
1529 void pcmcia_info(Monitor *mon, const QDict *qdict)
1531 struct pcmcia_socket_entry_s *iter;
1533 if (!pcmcia_sockets)
1534 monitor_printf(mon, "No PCMCIA sockets\n");
1536 for (iter = pcmcia_sockets; iter; iter = iter->next)
1537 monitor_printf(mon, "%s: %s\n", iter->socket->slot_string,
1538 iter->socket->attached ? iter->socket->card_string :
1539 "Empty");
1542 /***********************************************************/
1543 /* machine registration */
1545 MachineState *current_machine;
1547 static void machine_class_init(ObjectClass *oc, void *data)
1549 MachineClass *mc = MACHINE_CLASS(oc);
1550 QEMUMachine *qm = data;
1552 mc->name = qm->name;
1553 mc->alias = qm->alias;
1554 mc->desc = qm->desc;
1555 mc->init = qm->init;
1556 mc->reset = qm->reset;
1557 mc->hot_add_cpu = qm->hot_add_cpu;
1558 mc->kvm_type = qm->kvm_type;
1559 mc->block_default_type = qm->block_default_type;
1560 mc->max_cpus = qm->max_cpus;
1561 mc->no_serial = qm->no_serial;
1562 mc->no_parallel = qm->no_parallel;
1563 mc->use_virtcon = qm->use_virtcon;
1564 mc->use_sclp = qm->use_sclp;
1565 mc->no_floppy = qm->no_floppy;
1566 mc->no_cdrom = qm->no_cdrom;
1567 mc->no_sdcard = qm->no_sdcard;
1568 mc->is_default = qm->is_default;
1569 mc->default_machine_opts = qm->default_machine_opts;
1570 mc->default_boot_order = qm->default_boot_order;
1571 mc->compat_props = qm->compat_props;
1572 mc->hw_version = qm->hw_version;
1575 int qemu_register_machine(QEMUMachine *m)
1577 char *name = g_strconcat(m->name, TYPE_MACHINE_SUFFIX, NULL);
1578 TypeInfo ti = {
1579 .name = name,
1580 .parent = TYPE_MACHINE,
1581 .class_init = machine_class_init,
1582 .class_data = (void *)m,
1585 type_register(&ti);
1586 g_free(name);
1588 return 0;
1591 static MachineClass *find_machine(const char *name)
1593 GSList *el, *machines = object_class_get_list(TYPE_MACHINE, false);
1594 MachineClass *mc = NULL;
1596 for (el = machines; el; el = el->next) {
1597 MachineClass *temp = el->data;
1599 if (!strcmp(temp->name, name)) {
1600 mc = temp;
1601 break;
1603 if (temp->alias &&
1604 !strcmp(temp->alias, name)) {
1605 mc = temp;
1606 break;
1610 g_slist_free(machines);
1611 return mc;
1614 MachineClass *find_default_machine(void)
1616 GSList *el, *machines = object_class_get_list(TYPE_MACHINE, false);
1617 MachineClass *mc = NULL;
1619 for (el = machines; el; el = el->next) {
1620 MachineClass *temp = el->data;
1622 if (temp->is_default) {
1623 mc = temp;
1624 break;
1628 g_slist_free(machines);
1629 return mc;
1632 MachineInfoList *qmp_query_machines(Error **errp)
1634 GSList *el, *machines = object_class_get_list(TYPE_MACHINE, false);
1635 MachineInfoList *mach_list = NULL;
1637 for (el = machines; el; el = el->next) {
1638 MachineClass *mc = el->data;
1639 MachineInfoList *entry;
1640 MachineInfo *info;
1642 info = g_malloc0(sizeof(*info));
1643 if (mc->is_default) {
1644 info->has_is_default = true;
1645 info->is_default = true;
1648 if (mc->alias) {
1649 info->has_alias = true;
1650 info->alias = g_strdup(mc->alias);
1653 info->name = g_strdup(mc->name);
1654 info->cpu_max = !mc->max_cpus ? 1 : mc->max_cpus;
1656 entry = g_malloc0(sizeof(*entry));
1657 entry->value = info;
1658 entry->next = mach_list;
1659 mach_list = entry;
1662 g_slist_free(machines);
1663 return mach_list;
1666 /***********************************************************/
1667 /* main execution loop */
1669 struct vm_change_state_entry {
1670 VMChangeStateHandler *cb;
1671 void *opaque;
1672 QLIST_ENTRY (vm_change_state_entry) entries;
1675 static QLIST_HEAD(vm_change_state_head, vm_change_state_entry) vm_change_state_head;
1677 VMChangeStateEntry *qemu_add_vm_change_state_handler(VMChangeStateHandler *cb,
1678 void *opaque)
1680 VMChangeStateEntry *e;
1682 e = g_malloc0(sizeof (*e));
1684 e->cb = cb;
1685 e->opaque = opaque;
1686 QLIST_INSERT_HEAD(&vm_change_state_head, e, entries);
1687 return e;
1690 void qemu_del_vm_change_state_handler(VMChangeStateEntry *e)
1692 QLIST_REMOVE (e, entries);
1693 g_free (e);
1696 void vm_state_notify(int running, RunState state)
1698 VMChangeStateEntry *e;
1700 trace_vm_state_notify(running, state);
1702 for (e = vm_change_state_head.lh_first; e; e = e->entries.le_next) {
1703 e->cb(e->opaque, running, state);
1707 /* reset/shutdown handler */
1709 typedef struct QEMUResetEntry {
1710 QTAILQ_ENTRY(QEMUResetEntry) entry;
1711 QEMUResetHandler *func;
1712 void *opaque;
1713 } QEMUResetEntry;
1715 static QTAILQ_HEAD(reset_handlers, QEMUResetEntry) reset_handlers =
1716 QTAILQ_HEAD_INITIALIZER(reset_handlers);
1717 static int reset_requested;
1718 static int shutdown_requested, shutdown_signal = -1;
1719 static pid_t shutdown_pid;
1720 static int powerdown_requested;
1721 static int debug_requested;
1722 static int suspend_requested;
1723 static WakeupReason wakeup_reason;
1724 static NotifierList powerdown_notifiers =
1725 NOTIFIER_LIST_INITIALIZER(powerdown_notifiers);
1726 static NotifierList suspend_notifiers =
1727 NOTIFIER_LIST_INITIALIZER(suspend_notifiers);
1728 static NotifierList wakeup_notifiers =
1729 NOTIFIER_LIST_INITIALIZER(wakeup_notifiers);
1730 static uint32_t wakeup_reason_mask = ~(1 << QEMU_WAKEUP_REASON_NONE);
1732 int qemu_shutdown_requested_get(void)
1734 return shutdown_requested;
1737 int qemu_reset_requested_get(void)
1739 return reset_requested;
1742 static int qemu_shutdown_requested(void)
1744 int r = shutdown_requested;
1745 shutdown_requested = 0;
1746 return r;
1749 static void qemu_kill_report(void)
1751 if (!qtest_driver() && shutdown_signal != -1) {
1752 fprintf(stderr, "qemu: terminating on signal %d", shutdown_signal);
1753 if (shutdown_pid == 0) {
1754 /* This happens for eg ^C at the terminal, so it's worth
1755 * avoiding printing an odd message in that case.
1757 fputc('\n', stderr);
1758 } else {
1759 fprintf(stderr, " from pid " FMT_pid "\n", shutdown_pid);
1761 shutdown_signal = -1;
1765 static int qemu_reset_requested(void)
1767 int r = reset_requested;
1768 reset_requested = 0;
1769 return r;
1772 static int qemu_suspend_requested(void)
1774 int r = suspend_requested;
1775 suspend_requested = 0;
1776 return r;
1779 static WakeupReason qemu_wakeup_requested(void)
1781 return wakeup_reason;
1784 static int qemu_powerdown_requested(void)
1786 int r = powerdown_requested;
1787 powerdown_requested = 0;
1788 return r;
1791 static int qemu_debug_requested(void)
1793 int r = debug_requested;
1794 debug_requested = 0;
1795 return r;
1798 void qemu_register_reset(QEMUResetHandler *func, void *opaque)
1800 QEMUResetEntry *re = g_malloc0(sizeof(QEMUResetEntry));
1802 re->func = func;
1803 re->opaque = opaque;
1804 QTAILQ_INSERT_TAIL(&reset_handlers, re, entry);
1807 void qemu_unregister_reset(QEMUResetHandler *func, void *opaque)
1809 QEMUResetEntry *re;
1811 QTAILQ_FOREACH(re, &reset_handlers, entry) {
1812 if (re->func == func && re->opaque == opaque) {
1813 QTAILQ_REMOVE(&reset_handlers, re, entry);
1814 g_free(re);
1815 return;
1820 void qemu_devices_reset(void)
1822 QEMUResetEntry *re, *nre;
1824 /* reset all devices */
1825 QTAILQ_FOREACH_SAFE(re, &reset_handlers, entry, nre) {
1826 re->func(re->opaque);
1830 void qemu_system_reset(bool report)
1832 MachineClass *mc;
1834 mc = current_machine ? MACHINE_GET_CLASS(current_machine) : NULL;
1836 if (mc && mc->reset) {
1837 mc->reset();
1838 } else {
1839 qemu_devices_reset();
1841 if (report) {
1842 qapi_event_send_reset(&error_abort);
1844 cpu_synchronize_all_post_reset();
1847 void qemu_system_reset_request(void)
1849 if (no_reboot) {
1850 shutdown_requested = 1;
1851 } else {
1852 reset_requested = 1;
1854 cpu_stop_current();
1855 qemu_notify_event();
1858 static void qemu_system_suspend(void)
1860 pause_all_vcpus();
1861 notifier_list_notify(&suspend_notifiers, NULL);
1862 runstate_set(RUN_STATE_SUSPENDED);
1863 qapi_event_send_suspend(&error_abort);
1866 void qemu_system_suspend_request(void)
1868 if (runstate_check(RUN_STATE_SUSPENDED)) {
1869 return;
1871 suspend_requested = 1;
1872 cpu_stop_current();
1873 qemu_notify_event();
1876 void qemu_register_suspend_notifier(Notifier *notifier)
1878 notifier_list_add(&suspend_notifiers, notifier);
1881 void qemu_system_wakeup_request(WakeupReason reason)
1883 trace_system_wakeup_request(reason);
1885 if (!runstate_check(RUN_STATE_SUSPENDED)) {
1886 return;
1888 if (!(wakeup_reason_mask & (1 << reason))) {
1889 return;
1891 runstate_set(RUN_STATE_RUNNING);
1892 wakeup_reason = reason;
1893 qemu_notify_event();
1896 void qemu_system_wakeup_enable(WakeupReason reason, bool enabled)
1898 if (enabled) {
1899 wakeup_reason_mask |= (1 << reason);
1900 } else {
1901 wakeup_reason_mask &= ~(1 << reason);
1905 void qemu_register_wakeup_notifier(Notifier *notifier)
1907 notifier_list_add(&wakeup_notifiers, notifier);
1910 void qemu_system_killed(int signal, pid_t pid)
1912 shutdown_signal = signal;
1913 shutdown_pid = pid;
1914 no_shutdown = 0;
1915 qemu_system_shutdown_request();
1918 void qemu_system_shutdown_request(void)
1920 shutdown_requested = 1;
1921 qemu_notify_event();
1924 static void qemu_system_powerdown(void)
1926 qapi_event_send_powerdown(&error_abort);
1927 notifier_list_notify(&powerdown_notifiers, NULL);
1930 void qemu_system_powerdown_request(void)
1932 powerdown_requested = 1;
1933 qemu_notify_event();
1936 void qemu_register_powerdown_notifier(Notifier *notifier)
1938 notifier_list_add(&powerdown_notifiers, notifier);
1941 void qemu_system_debug_request(void)
1943 debug_requested = 1;
1944 qemu_notify_event();
1947 static bool main_loop_should_exit(void)
1949 RunState r;
1950 if (qemu_debug_requested()) {
1951 vm_stop(RUN_STATE_DEBUG);
1953 if (qemu_suspend_requested()) {
1954 qemu_system_suspend();
1956 if (qemu_shutdown_requested()) {
1957 qemu_kill_report();
1958 qapi_event_send_shutdown(&error_abort);
1959 if (no_shutdown) {
1960 vm_stop(RUN_STATE_SHUTDOWN);
1961 } else {
1962 return true;
1965 if (qemu_reset_requested()) {
1966 pause_all_vcpus();
1967 cpu_synchronize_all_states();
1968 qemu_system_reset(VMRESET_REPORT);
1969 resume_all_vcpus();
1970 if (runstate_needs_reset()) {
1971 runstate_set(RUN_STATE_PAUSED);
1974 if (qemu_wakeup_requested()) {
1975 pause_all_vcpus();
1976 cpu_synchronize_all_states();
1977 qemu_system_reset(VMRESET_SILENT);
1978 notifier_list_notify(&wakeup_notifiers, &wakeup_reason);
1979 wakeup_reason = QEMU_WAKEUP_REASON_NONE;
1980 resume_all_vcpus();
1981 qapi_event_send_wakeup(&error_abort);
1983 if (qemu_powerdown_requested()) {
1984 qemu_system_powerdown();
1986 if (qemu_vmstop_requested(&r)) {
1987 vm_stop(r);
1989 return false;
1992 static void main_loop(void)
1994 bool nonblocking;
1995 int last_io = 0;
1996 #ifdef CONFIG_PROFILER
1997 int64_t ti;
1998 #endif
1999 do {
2000 nonblocking = !kvm_enabled() && !xen_enabled() && last_io > 0;
2001 #ifdef CONFIG_PROFILER
2002 ti = profile_getclock();
2003 #endif
2004 last_io = main_loop_wait(nonblocking);
2005 #ifdef CONFIG_PROFILER
2006 dev_time += profile_getclock() - ti;
2007 #endif
2008 } while (!main_loop_should_exit());
2011 static void version(void)
2013 printf("QEMU emulator version " QEMU_VERSION QEMU_PKGVERSION ", Copyright (c) 2003-2008 Fabrice Bellard\n");
2016 static void help(int exitcode)
2018 version();
2019 printf("usage: %s [options] [disk_image]\n\n"
2020 "'disk_image' is a raw hard disk image for IDE hard disk 0\n\n",
2021 error_get_progname());
2023 #define QEMU_OPTIONS_GENERATE_HELP
2024 #include "qemu-options-wrapper.h"
2026 printf("\nDuring emulation, the following keys are useful:\n"
2027 "ctrl-alt-f toggle full screen\n"
2028 "ctrl-alt-n switch to virtual console 'n'\n"
2029 "ctrl-alt toggle mouse and keyboard grab\n"
2030 "\n"
2031 "When using -nographic, press 'ctrl-a h' to get some help.\n");
2033 exit(exitcode);
2036 #define HAS_ARG 0x0001
2038 typedef struct QEMUOption {
2039 const char *name;
2040 int flags;
2041 int index;
2042 uint32_t arch_mask;
2043 } QEMUOption;
2045 static const QEMUOption qemu_options[] = {
2046 { "h", 0, QEMU_OPTION_h, QEMU_ARCH_ALL },
2047 #define QEMU_OPTIONS_GENERATE_OPTIONS
2048 #include "qemu-options-wrapper.h"
2049 { NULL },
2052 static bool vga_available(void)
2054 return object_class_by_name("VGA") || object_class_by_name("isa-vga");
2057 static bool cirrus_vga_available(void)
2059 return object_class_by_name("cirrus-vga")
2060 || object_class_by_name("isa-cirrus-vga");
2063 static bool vmware_vga_available(void)
2065 return object_class_by_name("vmware-svga");
2068 static bool qxl_vga_available(void)
2070 return object_class_by_name("qxl-vga");
2073 static bool tcx_vga_available(void)
2075 return object_class_by_name("SUNW,tcx");
2078 static bool cg3_vga_available(void)
2080 return object_class_by_name("cgthree");
2083 static void select_vgahw (const char *p)
2085 const char *opts;
2087 assert(vga_interface_type == VGA_NONE);
2088 if (strstart(p, "std", &opts)) {
2089 if (vga_available()) {
2090 vga_interface_type = VGA_STD;
2091 } else {
2092 fprintf(stderr, "Error: standard VGA not available\n");
2093 exit(0);
2095 } else if (strstart(p, "cirrus", &opts)) {
2096 if (cirrus_vga_available()) {
2097 vga_interface_type = VGA_CIRRUS;
2098 } else {
2099 fprintf(stderr, "Error: Cirrus VGA not available\n");
2100 exit(0);
2102 } else if (strstart(p, "vmware", &opts)) {
2103 if (vmware_vga_available()) {
2104 vga_interface_type = VGA_VMWARE;
2105 } else {
2106 fprintf(stderr, "Error: VMWare SVGA not available\n");
2107 exit(0);
2109 } else if (strstart(p, "xenfb", &opts)) {
2110 vga_interface_type = VGA_XENFB;
2111 } else if (strstart(p, "qxl", &opts)) {
2112 if (qxl_vga_available()) {
2113 vga_interface_type = VGA_QXL;
2114 } else {
2115 fprintf(stderr, "Error: QXL VGA not available\n");
2116 exit(0);
2118 } else if (strstart(p, "tcx", &opts)) {
2119 if (tcx_vga_available()) {
2120 vga_interface_type = VGA_TCX;
2121 } else {
2122 fprintf(stderr, "Error: TCX framebuffer not available\n");
2123 exit(0);
2125 } else if (strstart(p, "cg3", &opts)) {
2126 if (cg3_vga_available()) {
2127 vga_interface_type = VGA_CG3;
2128 } else {
2129 fprintf(stderr, "Error: CG3 framebuffer not available\n");
2130 exit(0);
2132 } else if (!strstart(p, "none", &opts)) {
2133 invalid_vga:
2134 fprintf(stderr, "Unknown vga type: %s\n", p);
2135 exit(1);
2137 while (*opts) {
2138 const char *nextopt;
2140 if (strstart(opts, ",retrace=", &nextopt)) {
2141 opts = nextopt;
2142 if (strstart(opts, "dumb", &nextopt))
2143 vga_retrace_method = VGA_RETRACE_DUMB;
2144 else if (strstart(opts, "precise", &nextopt))
2145 vga_retrace_method = VGA_RETRACE_PRECISE;
2146 else goto invalid_vga;
2147 } else goto invalid_vga;
2148 opts = nextopt;
2152 static DisplayType select_display(const char *p)
2154 const char *opts;
2155 DisplayType display = DT_DEFAULT;
2157 if (strstart(p, "sdl", &opts)) {
2158 #ifdef CONFIG_SDL
2159 display = DT_SDL;
2160 while (*opts) {
2161 const char *nextopt;
2163 if (strstart(opts, ",frame=", &nextopt)) {
2164 opts = nextopt;
2165 if (strstart(opts, "on", &nextopt)) {
2166 no_frame = 0;
2167 } else if (strstart(opts, "off", &nextopt)) {
2168 no_frame = 1;
2169 } else {
2170 goto invalid_sdl_args;
2172 } else if (strstart(opts, ",alt_grab=", &nextopt)) {
2173 opts = nextopt;
2174 if (strstart(opts, "on", &nextopt)) {
2175 alt_grab = 1;
2176 } else if (strstart(opts, "off", &nextopt)) {
2177 alt_grab = 0;
2178 } else {
2179 goto invalid_sdl_args;
2181 } else if (strstart(opts, ",ctrl_grab=", &nextopt)) {
2182 opts = nextopt;
2183 if (strstart(opts, "on", &nextopt)) {
2184 ctrl_grab = 1;
2185 } else if (strstart(opts, "off", &nextopt)) {
2186 ctrl_grab = 0;
2187 } else {
2188 goto invalid_sdl_args;
2190 } else if (strstart(opts, ",window_close=", &nextopt)) {
2191 opts = nextopt;
2192 if (strstart(opts, "on", &nextopt)) {
2193 no_quit = 0;
2194 } else if (strstart(opts, "off", &nextopt)) {
2195 no_quit = 1;
2196 } else {
2197 goto invalid_sdl_args;
2199 } else {
2200 invalid_sdl_args:
2201 fprintf(stderr, "Invalid SDL option string: %s\n", p);
2202 exit(1);
2204 opts = nextopt;
2206 #else
2207 fprintf(stderr, "SDL support is disabled\n");
2208 exit(1);
2209 #endif
2210 } else if (strstart(p, "vnc", &opts)) {
2211 #ifdef CONFIG_VNC
2212 display_remote++;
2214 if (*opts) {
2215 const char *nextopt;
2217 if (strstart(opts, "=", &nextopt)) {
2218 vnc_display = nextopt;
2221 if (!vnc_display) {
2222 fprintf(stderr, "VNC requires a display argument vnc=<display>\n");
2223 exit(1);
2225 #else
2226 fprintf(stderr, "VNC support is disabled\n");
2227 exit(1);
2228 #endif
2229 } else if (strstart(p, "curses", &opts)) {
2230 #ifdef CONFIG_CURSES
2231 display = DT_CURSES;
2232 #else
2233 fprintf(stderr, "Curses support is disabled\n");
2234 exit(1);
2235 #endif
2236 } else if (strstart(p, "gtk", &opts)) {
2237 #ifdef CONFIG_GTK
2238 display = DT_GTK;
2239 while (*opts) {
2240 const char *nextopt;
2242 if (strstart(opts, ",grab_on_hover=", &nextopt)) {
2243 opts = nextopt;
2244 if (strstart(opts, "on", &nextopt)) {
2245 grab_on_hover = true;
2246 } else if (strstart(opts, "off", &nextopt)) {
2247 grab_on_hover = false;
2248 } else {
2249 goto invalid_gtk_args;
2251 } else {
2252 invalid_gtk_args:
2253 fprintf(stderr, "Invalid GTK option string: %s\n", p);
2254 exit(1);
2256 opts = nextopt;
2258 #else
2259 fprintf(stderr, "GTK support is disabled\n");
2260 exit(1);
2261 #endif
2262 } else if (strstart(p, "none", &opts)) {
2263 display = DT_NONE;
2264 } else {
2265 fprintf(stderr, "Unknown display type: %s\n", p);
2266 exit(1);
2269 return display;
2272 static int balloon_parse(const char *arg)
2274 QemuOpts *opts;
2276 if (strcmp(arg, "none") == 0) {
2277 return 0;
2280 if (!strncmp(arg, "virtio", 6)) {
2281 if (arg[6] == ',') {
2282 /* have params -> parse them */
2283 opts = qemu_opts_parse(qemu_find_opts("device"), arg+7, 0);
2284 if (!opts)
2285 return -1;
2286 } else {
2287 /* create empty opts */
2288 opts = qemu_opts_create(qemu_find_opts("device"), NULL, 0,
2289 &error_abort);
2291 qemu_opt_set(opts, "driver", "virtio-balloon");
2292 return 0;
2295 return -1;
2298 char *qemu_find_file(int type, const char *name)
2300 int i;
2301 const char *subdir;
2302 char *buf;
2304 /* Try the name as a straight path first */
2305 if (access(name, R_OK) == 0) {
2306 trace_load_file(name, name);
2307 return g_strdup(name);
2310 switch (type) {
2311 case QEMU_FILE_TYPE_BIOS:
2312 subdir = "";
2313 break;
2314 case QEMU_FILE_TYPE_KEYMAP:
2315 subdir = "keymaps/";
2316 break;
2317 default:
2318 abort();
2321 for (i = 0; i < data_dir_idx; i++) {
2322 buf = g_strdup_printf("%s/%s%s", data_dir[i], subdir, name);
2323 if (access(buf, R_OK) == 0) {
2324 trace_load_file(name, buf);
2325 return buf;
2327 g_free(buf);
2329 return NULL;
2332 static int device_help_func(QemuOpts *opts, void *opaque)
2334 return qdev_device_help(opts);
2337 static int device_init_func(QemuOpts *opts, void *opaque)
2339 DeviceState *dev;
2341 dev = qdev_device_add(opts);
2342 if (!dev)
2343 return -1;
2344 object_unref(OBJECT(dev));
2345 return 0;
2348 static int chardev_init_func(QemuOpts *opts, void *opaque)
2350 Error *local_err = NULL;
2352 qemu_chr_new_from_opts(opts, NULL, &local_err);
2353 if (local_err) {
2354 error_report("%s", error_get_pretty(local_err));
2355 error_free(local_err);
2356 return -1;
2358 return 0;
2361 #ifdef CONFIG_VIRTFS
2362 static int fsdev_init_func(QemuOpts *opts, void *opaque)
2364 int ret;
2365 ret = qemu_fsdev_add(opts);
2367 return ret;
2369 #endif
2371 static int mon_init_func(QemuOpts *opts, void *opaque)
2373 CharDriverState *chr;
2374 const char *chardev;
2375 const char *mode;
2376 int flags;
2378 mode = qemu_opt_get(opts, "mode");
2379 if (mode == NULL) {
2380 mode = "readline";
2382 if (strcmp(mode, "readline") == 0) {
2383 flags = MONITOR_USE_READLINE;
2384 } else if (strcmp(mode, "control") == 0) {
2385 flags = MONITOR_USE_CONTROL;
2386 } else {
2387 fprintf(stderr, "unknown monitor mode \"%s\"\n", mode);
2388 exit(1);
2391 if (qemu_opt_get_bool(opts, "pretty", 0))
2392 flags |= MONITOR_USE_PRETTY;
2394 if (qemu_opt_get_bool(opts, "default", 0))
2395 flags |= MONITOR_IS_DEFAULT;
2397 chardev = qemu_opt_get(opts, "chardev");
2398 chr = qemu_chr_find(chardev);
2399 if (chr == NULL) {
2400 fprintf(stderr, "chardev \"%s\" not found\n", chardev);
2401 exit(1);
2404 qemu_chr_fe_claim_no_fail(chr);
2405 monitor_init(chr, flags);
2406 return 0;
2409 static void monitor_parse(const char *optarg, const char *mode)
2411 static int monitor_device_index = 0;
2412 QemuOpts *opts;
2413 const char *p;
2414 char label[32];
2415 int def = 0;
2417 if (strstart(optarg, "chardev:", &p)) {
2418 snprintf(label, sizeof(label), "%s", p);
2419 } else {
2420 snprintf(label, sizeof(label), "compat_monitor%d",
2421 monitor_device_index);
2422 if (monitor_device_index == 0) {
2423 def = 1;
2425 opts = qemu_chr_parse_compat(label, optarg);
2426 if (!opts) {
2427 fprintf(stderr, "parse error: %s\n", optarg);
2428 exit(1);
2432 opts = qemu_opts_create(qemu_find_opts("mon"), label, 1, NULL);
2433 if (!opts) {
2434 fprintf(stderr, "duplicate chardev: %s\n", label);
2435 exit(1);
2437 qemu_opt_set(opts, "mode", mode);
2438 qemu_opt_set(opts, "chardev", label);
2439 if (def)
2440 qemu_opt_set(opts, "default", "on");
2441 monitor_device_index++;
2444 struct device_config {
2445 enum {
2446 DEV_USB, /* -usbdevice */
2447 DEV_BT, /* -bt */
2448 DEV_SERIAL, /* -serial */
2449 DEV_PARALLEL, /* -parallel */
2450 DEV_VIRTCON, /* -virtioconsole */
2451 DEV_DEBUGCON, /* -debugcon */
2452 DEV_GDB, /* -gdb, -s */
2453 DEV_SCLP, /* s390 sclp */
2454 } type;
2455 const char *cmdline;
2456 Location loc;
2457 QTAILQ_ENTRY(device_config) next;
2460 static QTAILQ_HEAD(, device_config) device_configs =
2461 QTAILQ_HEAD_INITIALIZER(device_configs);
2463 static void add_device_config(int type, const char *cmdline)
2465 struct device_config *conf;
2467 conf = g_malloc0(sizeof(*conf));
2468 conf->type = type;
2469 conf->cmdline = cmdline;
2470 loc_save(&conf->loc);
2471 QTAILQ_INSERT_TAIL(&device_configs, conf, next);
2474 static int foreach_device_config(int type, int (*func)(const char *cmdline))
2476 struct device_config *conf;
2477 int rc;
2479 QTAILQ_FOREACH(conf, &device_configs, next) {
2480 if (conf->type != type)
2481 continue;
2482 loc_push_restore(&conf->loc);
2483 rc = func(conf->cmdline);
2484 loc_pop(&conf->loc);
2485 if (0 != rc)
2486 return rc;
2488 return 0;
2491 static int serial_parse(const char *devname)
2493 static int index = 0;
2494 char label[32];
2496 if (strcmp(devname, "none") == 0)
2497 return 0;
2498 if (index == MAX_SERIAL_PORTS) {
2499 fprintf(stderr, "qemu: too many serial ports\n");
2500 exit(1);
2502 snprintf(label, sizeof(label), "serial%d", index);
2503 serial_hds[index] = qemu_chr_new(label, devname, NULL);
2504 if (!serial_hds[index]) {
2505 fprintf(stderr, "qemu: could not connect serial device"
2506 " to character backend '%s'\n", devname);
2507 return -1;
2509 index++;
2510 return 0;
2513 static int parallel_parse(const char *devname)
2515 static int index = 0;
2516 char label[32];
2518 if (strcmp(devname, "none") == 0)
2519 return 0;
2520 if (index == MAX_PARALLEL_PORTS) {
2521 fprintf(stderr, "qemu: too many parallel ports\n");
2522 exit(1);
2524 snprintf(label, sizeof(label), "parallel%d", index);
2525 parallel_hds[index] = qemu_chr_new(label, devname, NULL);
2526 if (!parallel_hds[index]) {
2527 fprintf(stderr, "qemu: could not connect parallel device"
2528 " to character backend '%s'\n", devname);
2529 return -1;
2531 index++;
2532 return 0;
2535 static int virtcon_parse(const char *devname)
2537 QemuOptsList *device = qemu_find_opts("device");
2538 static int index = 0;
2539 char label[32];
2540 QemuOpts *bus_opts, *dev_opts;
2542 if (strcmp(devname, "none") == 0)
2543 return 0;
2544 if (index == MAX_VIRTIO_CONSOLES) {
2545 fprintf(stderr, "qemu: too many virtio consoles\n");
2546 exit(1);
2549 bus_opts = qemu_opts_create(device, NULL, 0, &error_abort);
2550 if (arch_type == QEMU_ARCH_S390X) {
2551 qemu_opt_set(bus_opts, "driver", "virtio-serial-s390");
2552 } else {
2553 qemu_opt_set(bus_opts, "driver", "virtio-serial-pci");
2556 dev_opts = qemu_opts_create(device, NULL, 0, &error_abort);
2557 qemu_opt_set(dev_opts, "driver", "virtconsole");
2559 snprintf(label, sizeof(label), "virtcon%d", index);
2560 virtcon_hds[index] = qemu_chr_new(label, devname, NULL);
2561 if (!virtcon_hds[index]) {
2562 fprintf(stderr, "qemu: could not connect virtio console"
2563 " to character backend '%s'\n", devname);
2564 return -1;
2566 qemu_opt_set(dev_opts, "chardev", label);
2568 index++;
2569 return 0;
2572 static int sclp_parse(const char *devname)
2574 QemuOptsList *device = qemu_find_opts("device");
2575 static int index = 0;
2576 char label[32];
2577 QemuOpts *dev_opts;
2579 if (strcmp(devname, "none") == 0) {
2580 return 0;
2582 if (index == MAX_SCLP_CONSOLES) {
2583 fprintf(stderr, "qemu: too many sclp consoles\n");
2584 exit(1);
2587 assert(arch_type == QEMU_ARCH_S390X);
2589 dev_opts = qemu_opts_create(device, NULL, 0, NULL);
2590 qemu_opt_set(dev_opts, "driver", "sclpconsole");
2592 snprintf(label, sizeof(label), "sclpcon%d", index);
2593 sclp_hds[index] = qemu_chr_new(label, devname, NULL);
2594 if (!sclp_hds[index]) {
2595 fprintf(stderr, "qemu: could not connect sclp console"
2596 " to character backend '%s'\n", devname);
2597 return -1;
2599 qemu_opt_set(dev_opts, "chardev", label);
2601 index++;
2602 return 0;
2605 static int debugcon_parse(const char *devname)
2607 QemuOpts *opts;
2609 if (!qemu_chr_new("debugcon", devname, NULL)) {
2610 exit(1);
2612 opts = qemu_opts_create(qemu_find_opts("device"), "debugcon", 1, NULL);
2613 if (!opts) {
2614 fprintf(stderr, "qemu: already have a debugcon device\n");
2615 exit(1);
2617 qemu_opt_set(opts, "driver", "isa-debugcon");
2618 qemu_opt_set(opts, "chardev", "debugcon");
2619 return 0;
2622 static MachineClass *machine_parse(const char *name)
2624 MachineClass *mc = NULL;
2625 GSList *el, *machines = object_class_get_list(TYPE_MACHINE, false);
2627 if (name) {
2628 mc = find_machine(name);
2630 if (mc) {
2631 return mc;
2633 if (name && !is_help_option(name)) {
2634 error_report("Unsupported machine type");
2635 error_printf("Use -machine help to list supported machines!\n");
2636 } else {
2637 printf("Supported machines are:\n");
2638 for (el = machines; el; el = el->next) {
2639 MachineClass *mc = el->data;
2640 if (mc->alias) {
2641 printf("%-20s %s (alias of %s)\n", mc->alias, mc->desc, mc->name);
2643 printf("%-20s %s%s\n", mc->name, mc->desc,
2644 mc->is_default ? " (default)" : "");
2648 g_slist_free(machines);
2649 exit(!name || !is_help_option(name));
2652 static int tcg_init(MachineClass *mc)
2654 tcg_exec_init(tcg_tb_size * 1024 * 1024);
2655 return 0;
2658 static struct {
2659 const char *opt_name;
2660 const char *name;
2661 int (*available)(void);
2662 int (*init)(MachineClass *mc);
2663 bool *allowed;
2664 } accel_list[] = {
2665 { "tcg", "tcg", tcg_available, tcg_init, &tcg_allowed },
2666 { "xen", "Xen", xen_available, xen_init, &xen_allowed },
2667 { "kvm", "KVM", kvm_available, kvm_init, &kvm_allowed },
2668 { "qtest", "QTest", qtest_available, qtest_init_accel, &qtest_allowed },
2671 static int configure_accelerator(MachineClass *mc)
2673 const char *p;
2674 char buf[10];
2675 int i, ret;
2676 bool accel_initialised = false;
2677 bool init_failed = false;
2679 p = qemu_opt_get(qemu_get_machine_opts(), "accel");
2680 if (p == NULL) {
2681 /* Use the default "accelerator", tcg */
2682 p = "tcg";
2685 while (!accel_initialised && *p != '\0') {
2686 if (*p == ':') {
2687 p++;
2689 p = get_opt_name(buf, sizeof (buf), p, ':');
2690 for (i = 0; i < ARRAY_SIZE(accel_list); i++) {
2691 if (strcmp(accel_list[i].opt_name, buf) == 0) {
2692 if (!accel_list[i].available()) {
2693 printf("%s not supported for this target\n",
2694 accel_list[i].name);
2695 break;
2697 *(accel_list[i].allowed) = true;
2698 ret = accel_list[i].init(mc);
2699 if (ret < 0) {
2700 init_failed = true;
2701 fprintf(stderr, "failed to initialize %s: %s\n",
2702 accel_list[i].name,
2703 strerror(-ret));
2704 *(accel_list[i].allowed) = false;
2705 } else {
2706 accel_initialised = true;
2708 break;
2711 if (i == ARRAY_SIZE(accel_list)) {
2712 fprintf(stderr, "\"%s\" accelerator does not exist.\n", buf);
2716 if (!accel_initialised) {
2717 if (!init_failed) {
2718 fprintf(stderr, "No accelerator found!\n");
2720 exit(1);
2723 if (init_failed) {
2724 fprintf(stderr, "Back to %s accelerator.\n", accel_list[i].name);
2727 return !accel_initialised;
2730 void qemu_add_exit_notifier(Notifier *notify)
2732 notifier_list_add(&exit_notifiers, notify);
2735 void qemu_remove_exit_notifier(Notifier *notify)
2737 notifier_remove(notify);
2740 static void qemu_run_exit_notifiers(void)
2742 notifier_list_notify(&exit_notifiers, NULL);
2745 void qemu_add_machine_init_done_notifier(Notifier *notify)
2747 notifier_list_add(&machine_init_done_notifiers, notify);
2750 static void qemu_run_machine_init_done_notifiers(void)
2752 notifier_list_notify(&machine_init_done_notifiers, NULL);
2755 static const QEMUOption *lookup_opt(int argc, char **argv,
2756 const char **poptarg, int *poptind)
2758 const QEMUOption *popt;
2759 int optind = *poptind;
2760 char *r = argv[optind];
2761 const char *optarg;
2763 loc_set_cmdline(argv, optind, 1);
2764 optind++;
2765 /* Treat --foo the same as -foo. */
2766 if (r[1] == '-')
2767 r++;
2768 popt = qemu_options;
2769 for(;;) {
2770 if (!popt->name) {
2771 error_report("invalid option");
2772 exit(1);
2774 if (!strcmp(popt->name, r + 1))
2775 break;
2776 popt++;
2778 if (popt->flags & HAS_ARG) {
2779 if (optind >= argc) {
2780 error_report("requires an argument");
2781 exit(1);
2783 optarg = argv[optind++];
2784 loc_set_cmdline(argv, optind - 2, 2);
2785 } else {
2786 optarg = NULL;
2789 *poptarg = optarg;
2790 *poptind = optind;
2792 return popt;
2795 static gpointer malloc_and_trace(gsize n_bytes)
2797 void *ptr = malloc(n_bytes);
2798 trace_g_malloc(n_bytes, ptr);
2799 return ptr;
2802 static gpointer realloc_and_trace(gpointer mem, gsize n_bytes)
2804 void *ptr = realloc(mem, n_bytes);
2805 trace_g_realloc(mem, n_bytes, ptr);
2806 return ptr;
2809 static void free_and_trace(gpointer mem)
2811 trace_g_free(mem);
2812 free(mem);
2815 static int object_set_property(const char *name, const char *value, void *opaque)
2817 Object *obj = OBJECT(opaque);
2818 StringInputVisitor *siv;
2819 Error *local_err = NULL;
2821 if (strcmp(name, "qom-type") == 0 || strcmp(name, "id") == 0 ||
2822 strcmp(name, "type") == 0) {
2823 return 0;
2826 siv = string_input_visitor_new(value);
2827 object_property_set(obj, string_input_get_visitor(siv), name, &local_err);
2828 string_input_visitor_cleanup(siv);
2830 if (local_err) {
2831 qerror_report_err(local_err);
2832 error_free(local_err);
2833 return -1;
2836 return 0;
2839 static int object_create(QemuOpts *opts, void *opaque)
2841 Error *err = NULL;
2842 char *type = NULL;
2843 char *id = NULL;
2844 void *dummy = NULL;
2845 OptsVisitor *ov;
2846 QDict *pdict;
2848 ov = opts_visitor_new(opts);
2849 pdict = qemu_opts_to_qdict(opts, NULL);
2851 visit_start_struct(opts_get_visitor(ov), &dummy, NULL, NULL, 0, &err);
2852 if (err) {
2853 goto out;
2856 qdict_del(pdict, "qom-type");
2857 visit_type_str(opts_get_visitor(ov), &type, "qom-type", &err);
2858 if (err) {
2859 goto out;
2862 qdict_del(pdict, "id");
2863 visit_type_str(opts_get_visitor(ov), &id, "id", &err);
2864 if (err) {
2865 goto out;
2868 object_add(type, id, pdict, opts_get_visitor(ov), &err);
2869 if (err) {
2870 goto out;
2872 visit_end_struct(opts_get_visitor(ov), &err);
2873 if (err) {
2874 qmp_object_del(id, NULL);
2877 out:
2878 opts_visitor_cleanup(ov);
2880 QDECREF(pdict);
2881 g_free(id);
2882 g_free(type);
2883 g_free(dummy);
2884 if (err) {
2885 qerror_report_err(err);
2886 return -1;
2888 return 0;
2891 int main(int argc, char **argv, char **envp)
2893 int i;
2894 int snapshot, linux_boot;
2895 const char *icount_option = NULL;
2896 const char *initrd_filename;
2897 const char *kernel_filename, *kernel_cmdline;
2898 const char *boot_order;
2899 DisplayState *ds;
2900 int cyls, heads, secs, translation;
2901 QemuOpts *hda_opts = NULL, *opts, *machine_opts;
2902 QemuOptsList *olist;
2903 int optind;
2904 const char *optarg;
2905 const char *loadvm = NULL;
2906 MachineClass *machine_class;
2907 const char *cpu_model;
2908 const char *vga_model = NULL;
2909 const char *qtest_chrdev = NULL;
2910 const char *qtest_log = NULL;
2911 const char *pid_file = NULL;
2912 const char *incoming = NULL;
2913 #ifdef CONFIG_VNC
2914 int show_vnc_port = 0;
2915 #endif
2916 bool defconfig = true;
2917 bool userconfig = true;
2918 const char *log_mask = NULL;
2919 const char *log_file = NULL;
2920 GMemVTable mem_trace = {
2921 .malloc = malloc_and_trace,
2922 .realloc = realloc_and_trace,
2923 .free = free_and_trace,
2925 const char *trace_events = NULL;
2926 const char *trace_file = NULL;
2927 const ram_addr_t default_ram_size = (ram_addr_t)DEFAULT_RAM_SIZE *
2928 1024 * 1024;
2929 ram_addr_t maxram_size = default_ram_size;
2930 uint64_t ram_slots = 0;
2932 atexit(qemu_run_exit_notifiers);
2933 error_set_progname(argv[0]);
2934 qemu_init_exec_dir(argv[0]);
2936 g_mem_set_vtable(&mem_trace);
2938 module_call_init(MODULE_INIT_QOM);
2940 qemu_add_opts(&qemu_drive_opts);
2941 qemu_add_drive_opts(&qemu_legacy_drive_opts);
2942 qemu_add_drive_opts(&qemu_common_drive_opts);
2943 qemu_add_drive_opts(&qemu_drive_opts);
2944 qemu_add_opts(&qemu_chardev_opts);
2945 qemu_add_opts(&qemu_device_opts);
2946 qemu_add_opts(&qemu_netdev_opts);
2947 qemu_add_opts(&qemu_net_opts);
2948 qemu_add_opts(&qemu_rtc_opts);
2949 qemu_add_opts(&qemu_global_opts);
2950 qemu_add_opts(&qemu_mon_opts);
2951 qemu_add_opts(&qemu_trace_opts);
2952 qemu_add_opts(&qemu_option_rom_opts);
2953 qemu_add_opts(&qemu_machine_opts);
2954 qemu_add_opts(&qemu_mem_opts);
2955 qemu_add_opts(&qemu_smp_opts);
2956 qemu_add_opts(&qemu_boot_opts);
2957 qemu_add_opts(&qemu_sandbox_opts);
2958 qemu_add_opts(&qemu_add_fd_opts);
2959 qemu_add_opts(&qemu_object_opts);
2960 qemu_add_opts(&qemu_tpmdev_opts);
2961 qemu_add_opts(&qemu_realtime_opts);
2962 qemu_add_opts(&qemu_msg_opts);
2963 qemu_add_opts(&qemu_name_opts);
2964 qemu_add_opts(&qemu_numa_opts);
2966 runstate_init();
2968 rtc_clock = QEMU_CLOCK_HOST;
2970 qemu_init_auxval(envp);
2971 qemu_cache_utils_init();
2973 QLIST_INIT (&vm_change_state_head);
2974 os_setup_early_signal_handling();
2976 module_call_init(MODULE_INIT_MACHINE);
2977 machine_class = find_default_machine();
2978 cpu_model = NULL;
2979 ram_size = default_ram_size;
2980 snapshot = 0;
2981 cyls = heads = secs = 0;
2982 translation = BIOS_ATA_TRANSLATION_AUTO;
2984 for (i = 0; i < MAX_NODES; i++) {
2985 numa_info[i].node_mem = 0;
2986 bitmap_zero(numa_info[i].node_cpu, MAX_CPUMASK_BITS);
2989 nb_numa_nodes = 0;
2990 nb_nics = 0;
2992 bdrv_init_with_whitelist();
2994 autostart = 1;
2996 /* first pass of option parsing */
2997 optind = 1;
2998 while (optind < argc) {
2999 if (argv[optind][0] != '-') {
3000 /* disk image */
3001 optind++;
3002 } else {
3003 const QEMUOption *popt;
3005 popt = lookup_opt(argc, argv, &optarg, &optind);
3006 switch (popt->index) {
3007 case QEMU_OPTION_nodefconfig:
3008 defconfig = false;
3009 break;
3010 case QEMU_OPTION_nouserconfig:
3011 userconfig = false;
3012 break;
3017 if (defconfig) {
3018 int ret;
3019 ret = qemu_read_default_config_files(userconfig);
3020 if (ret < 0) {
3021 exit(1);
3025 /* second pass of option parsing */
3026 optind = 1;
3027 for(;;) {
3028 if (optind >= argc)
3029 break;
3030 if (argv[optind][0] != '-') {
3031 hda_opts = drive_add(IF_DEFAULT, 0, argv[optind++], HD_OPTS);
3032 } else {
3033 const QEMUOption *popt;
3035 popt = lookup_opt(argc, argv, &optarg, &optind);
3036 if (!(popt->arch_mask & arch_type)) {
3037 printf("Option %s not supported for this target\n", popt->name);
3038 exit(1);
3040 switch(popt->index) {
3041 case QEMU_OPTION_M:
3042 machine_class = machine_parse(optarg);
3043 break;
3044 case QEMU_OPTION_no_kvm_irqchip: {
3045 olist = qemu_find_opts("machine");
3046 qemu_opts_parse(olist, "kernel_irqchip=off", 0);
3047 break;
3049 case QEMU_OPTION_cpu:
3050 /* hw initialization will check this */
3051 cpu_model = optarg;
3052 break;
3053 case QEMU_OPTION_hda:
3055 char buf[256];
3056 if (cyls == 0)
3057 snprintf(buf, sizeof(buf), "%s", HD_OPTS);
3058 else
3059 snprintf(buf, sizeof(buf),
3060 "%s,cyls=%d,heads=%d,secs=%d%s",
3061 HD_OPTS , cyls, heads, secs,
3062 translation == BIOS_ATA_TRANSLATION_LBA ?
3063 ",trans=lba" :
3064 translation == BIOS_ATA_TRANSLATION_NONE ?
3065 ",trans=none" : "");
3066 drive_add(IF_DEFAULT, 0, optarg, buf);
3067 break;
3069 case QEMU_OPTION_hdb:
3070 case QEMU_OPTION_hdc:
3071 case QEMU_OPTION_hdd:
3072 drive_add(IF_DEFAULT, popt->index - QEMU_OPTION_hda, optarg,
3073 HD_OPTS);
3074 break;
3075 case QEMU_OPTION_drive:
3076 if (drive_def(optarg) == NULL) {
3077 exit(1);
3079 break;
3080 case QEMU_OPTION_set:
3081 if (qemu_set_option(optarg) != 0)
3082 exit(1);
3083 break;
3084 case QEMU_OPTION_global:
3085 if (qemu_global_option(optarg) != 0)
3086 exit(1);
3087 break;
3088 case QEMU_OPTION_mtdblock:
3089 drive_add(IF_MTD, -1, optarg, MTD_OPTS);
3090 break;
3091 case QEMU_OPTION_sd:
3092 drive_add(IF_SD, -1, optarg, SD_OPTS);
3093 break;
3094 case QEMU_OPTION_pflash:
3095 drive_add(IF_PFLASH, -1, optarg, PFLASH_OPTS);
3096 break;
3097 case QEMU_OPTION_snapshot:
3098 snapshot = 1;
3099 break;
3100 case QEMU_OPTION_hdachs:
3102 const char *p;
3103 p = optarg;
3104 cyls = strtol(p, (char **)&p, 0);
3105 if (cyls < 1 || cyls > 16383)
3106 goto chs_fail;
3107 if (*p != ',')
3108 goto chs_fail;
3109 p++;
3110 heads = strtol(p, (char **)&p, 0);
3111 if (heads < 1 || heads > 16)
3112 goto chs_fail;
3113 if (*p != ',')
3114 goto chs_fail;
3115 p++;
3116 secs = strtol(p, (char **)&p, 0);
3117 if (secs < 1 || secs > 63)
3118 goto chs_fail;
3119 if (*p == ',') {
3120 p++;
3121 if (!strcmp(p, "large")) {
3122 translation = BIOS_ATA_TRANSLATION_LARGE;
3123 } else if (!strcmp(p, "rechs")) {
3124 translation = BIOS_ATA_TRANSLATION_RECHS;
3125 } else if (!strcmp(p, "none")) {
3126 translation = BIOS_ATA_TRANSLATION_NONE;
3127 } else if (!strcmp(p, "lba")) {
3128 translation = BIOS_ATA_TRANSLATION_LBA;
3129 } else if (!strcmp(p, "auto")) {
3130 translation = BIOS_ATA_TRANSLATION_AUTO;
3131 } else {
3132 goto chs_fail;
3134 } else if (*p != '\0') {
3135 chs_fail:
3136 fprintf(stderr, "qemu: invalid physical CHS format\n");
3137 exit(1);
3139 if (hda_opts != NULL) {
3140 char num[16];
3141 snprintf(num, sizeof(num), "%d", cyls);
3142 qemu_opt_set(hda_opts, "cyls", num);
3143 snprintf(num, sizeof(num), "%d", heads);
3144 qemu_opt_set(hda_opts, "heads", num);
3145 snprintf(num, sizeof(num), "%d", secs);
3146 qemu_opt_set(hda_opts, "secs", num);
3147 if (translation == BIOS_ATA_TRANSLATION_LARGE) {
3148 qemu_opt_set(hda_opts, "trans", "large");
3149 } else if (translation == BIOS_ATA_TRANSLATION_RECHS) {
3150 qemu_opt_set(hda_opts, "trans", "rechs");
3151 } else if (translation == BIOS_ATA_TRANSLATION_LBA) {
3152 qemu_opt_set(hda_opts, "trans", "lba");
3153 } else if (translation == BIOS_ATA_TRANSLATION_NONE) {
3154 qemu_opt_set(hda_opts, "trans", "none");
3158 break;
3159 case QEMU_OPTION_numa:
3160 opts = qemu_opts_parse(qemu_find_opts("numa"), optarg, 1);
3161 if (!opts) {
3162 exit(1);
3164 break;
3165 case QEMU_OPTION_display:
3166 display_type = select_display(optarg);
3167 break;
3168 case QEMU_OPTION_nographic:
3169 display_type = DT_NOGRAPHIC;
3170 break;
3171 case QEMU_OPTION_curses:
3172 #ifdef CONFIG_CURSES
3173 display_type = DT_CURSES;
3174 #else
3175 fprintf(stderr, "Curses support is disabled\n");
3176 exit(1);
3177 #endif
3178 break;
3179 case QEMU_OPTION_portrait:
3180 graphic_rotate = 90;
3181 break;
3182 case QEMU_OPTION_rotate:
3183 graphic_rotate = strtol(optarg, (char **) &optarg, 10);
3184 if (graphic_rotate != 0 && graphic_rotate != 90 &&
3185 graphic_rotate != 180 && graphic_rotate != 270) {
3186 fprintf(stderr,
3187 "qemu: only 90, 180, 270 deg rotation is available\n");
3188 exit(1);
3190 break;
3191 case QEMU_OPTION_kernel:
3192 qemu_opts_set(qemu_find_opts("machine"), 0, "kernel", optarg);
3193 break;
3194 case QEMU_OPTION_initrd:
3195 qemu_opts_set(qemu_find_opts("machine"), 0, "initrd", optarg);
3196 break;
3197 case QEMU_OPTION_append:
3198 qemu_opts_set(qemu_find_opts("machine"), 0, "append", optarg);
3199 break;
3200 case QEMU_OPTION_dtb:
3201 qemu_opts_set(qemu_find_opts("machine"), 0, "dtb", optarg);
3202 break;
3203 case QEMU_OPTION_cdrom:
3204 drive_add(IF_DEFAULT, 2, optarg, CDROM_OPTS);
3205 break;
3206 case QEMU_OPTION_boot:
3207 opts = qemu_opts_parse(qemu_find_opts("boot-opts"), optarg, 1);
3208 if (!opts) {
3209 exit(1);
3211 break;
3212 case QEMU_OPTION_fda:
3213 case QEMU_OPTION_fdb:
3214 drive_add(IF_FLOPPY, popt->index - QEMU_OPTION_fda,
3215 optarg, FD_OPTS);
3216 break;
3217 case QEMU_OPTION_no_fd_bootchk:
3218 fd_bootchk = 0;
3219 break;
3220 case QEMU_OPTION_netdev:
3221 if (net_client_parse(qemu_find_opts("netdev"), optarg) == -1) {
3222 exit(1);
3224 break;
3225 case QEMU_OPTION_net:
3226 if (net_client_parse(qemu_find_opts("net"), optarg) == -1) {
3227 exit(1);
3229 break;
3230 #ifdef CONFIG_LIBISCSI
3231 case QEMU_OPTION_iscsi:
3232 opts = qemu_opts_parse(qemu_find_opts("iscsi"), optarg, 0);
3233 if (!opts) {
3234 exit(1);
3236 break;
3237 #endif
3238 #ifdef CONFIG_SLIRP
3239 case QEMU_OPTION_tftp:
3240 legacy_tftp_prefix = optarg;
3241 break;
3242 case QEMU_OPTION_bootp:
3243 legacy_bootp_filename = optarg;
3244 break;
3245 case QEMU_OPTION_redir:
3246 if (net_slirp_redir(optarg) < 0)
3247 exit(1);
3248 break;
3249 #endif
3250 case QEMU_OPTION_bt:
3251 add_device_config(DEV_BT, optarg);
3252 break;
3253 case QEMU_OPTION_audio_help:
3254 AUD_help ();
3255 exit (0);
3256 break;
3257 case QEMU_OPTION_soundhw:
3258 select_soundhw (optarg);
3259 break;
3260 case QEMU_OPTION_h:
3261 help(0);
3262 break;
3263 case QEMU_OPTION_version:
3264 version();
3265 exit(0);
3266 break;
3267 case QEMU_OPTION_m: {
3268 uint64_t sz;
3269 const char *mem_str;
3270 const char *maxmem_str, *slots_str;
3272 opts = qemu_opts_parse(qemu_find_opts("memory"),
3273 optarg, 1);
3274 if (!opts) {
3275 exit(EXIT_FAILURE);
3278 mem_str = qemu_opt_get(opts, "size");
3279 if (!mem_str) {
3280 error_report("invalid -m option, missing 'size' option");
3281 exit(EXIT_FAILURE);
3283 if (!*mem_str) {
3284 error_report("missing 'size' option value");
3285 exit(EXIT_FAILURE);
3288 sz = qemu_opt_get_size(opts, "size", ram_size);
3290 /* Fix up legacy suffix-less format */
3291 if (g_ascii_isdigit(mem_str[strlen(mem_str) - 1])) {
3292 uint64_t overflow_check = sz;
3294 sz <<= 20;
3295 if ((sz >> 20) != overflow_check) {
3296 error_report("too large 'size' option value");
3297 exit(EXIT_FAILURE);
3301 /* backward compatibility behaviour for case "-m 0" */
3302 if (sz == 0) {
3303 sz = default_ram_size;
3306 sz = QEMU_ALIGN_UP(sz, 8192);
3307 ram_size = sz;
3308 if (ram_size != sz) {
3309 error_report("ram size too large");
3310 exit(EXIT_FAILURE);
3313 maxmem_str = qemu_opt_get(opts, "maxmem");
3314 slots_str = qemu_opt_get(opts, "slots");
3315 if (maxmem_str && slots_str) {
3316 uint64_t slots;
3318 sz = qemu_opt_get_size(opts, "maxmem", 0);
3319 if (sz < ram_size) {
3320 fprintf(stderr, "qemu: invalid -m option value: maxmem "
3321 "(%" PRIu64 ") <= initial memory ("
3322 RAM_ADDR_FMT ")\n", sz, ram_size);
3323 exit(EXIT_FAILURE);
3326 slots = qemu_opt_get_number(opts, "slots", 0);
3327 if ((sz > ram_size) && !slots) {
3328 fprintf(stderr, "qemu: invalid -m option value: maxmem "
3329 "(%" PRIu64 ") more than initial memory ("
3330 RAM_ADDR_FMT ") but no hotplug slots where "
3331 "specified\n", sz, ram_size);
3332 exit(EXIT_FAILURE);
3335 if ((sz <= ram_size) && slots) {
3336 fprintf(stderr, "qemu: invalid -m option value: %"
3337 PRIu64 " hotplug slots where specified but "
3338 "maxmem (%" PRIu64 ") <= initial memory ("
3339 RAM_ADDR_FMT ")\n", slots, sz, ram_size);
3340 exit(EXIT_FAILURE);
3342 maxram_size = sz;
3343 ram_slots = slots;
3344 } else if ((!maxmem_str && slots_str) ||
3345 (maxmem_str && !slots_str)) {
3346 fprintf(stderr, "qemu: invalid -m option value: missing "
3347 "'%s' option\n", slots_str ? "maxmem" : "slots");
3348 exit(EXIT_FAILURE);
3350 break;
3352 #ifdef CONFIG_TPM
3353 case QEMU_OPTION_tpmdev:
3354 if (tpm_config_parse(qemu_find_opts("tpmdev"), optarg) < 0) {
3355 exit(1);
3357 break;
3358 #endif
3359 case QEMU_OPTION_mempath:
3360 mem_path = optarg;
3361 break;
3362 case QEMU_OPTION_mem_prealloc:
3363 mem_prealloc = 1;
3364 break;
3365 case QEMU_OPTION_d:
3366 log_mask = optarg;
3367 break;
3368 case QEMU_OPTION_D:
3369 log_file = optarg;
3370 break;
3371 case QEMU_OPTION_s:
3372 add_device_config(DEV_GDB, "tcp::" DEFAULT_GDBSTUB_PORT);
3373 break;
3374 case QEMU_OPTION_gdb:
3375 add_device_config(DEV_GDB, optarg);
3376 break;
3377 case QEMU_OPTION_L:
3378 if (data_dir_idx < ARRAY_SIZE(data_dir)) {
3379 data_dir[data_dir_idx++] = optarg;
3381 break;
3382 case QEMU_OPTION_bios:
3383 qemu_opts_set(qemu_find_opts("machine"), 0, "firmware", optarg);
3384 break;
3385 case QEMU_OPTION_singlestep:
3386 singlestep = 1;
3387 break;
3388 case QEMU_OPTION_S:
3389 autostart = 0;
3390 break;
3391 case QEMU_OPTION_k:
3392 keyboard_layout = optarg;
3393 break;
3394 case QEMU_OPTION_localtime:
3395 rtc_utc = 0;
3396 break;
3397 case QEMU_OPTION_vga:
3398 vga_model = optarg;
3399 default_vga = 0;
3400 break;
3401 case QEMU_OPTION_g:
3403 const char *p;
3404 int w, h, depth;
3405 p = optarg;
3406 w = strtol(p, (char **)&p, 10);
3407 if (w <= 0) {
3408 graphic_error:
3409 fprintf(stderr, "qemu: invalid resolution or depth\n");
3410 exit(1);
3412 if (*p != 'x')
3413 goto graphic_error;
3414 p++;
3415 h = strtol(p, (char **)&p, 10);
3416 if (h <= 0)
3417 goto graphic_error;
3418 if (*p == 'x') {
3419 p++;
3420 depth = strtol(p, (char **)&p, 10);
3421 if (depth != 8 && depth != 15 && depth != 16 &&
3422 depth != 24 && depth != 32)
3423 goto graphic_error;
3424 } else if (*p == '\0') {
3425 depth = graphic_depth;
3426 } else {
3427 goto graphic_error;
3430 graphic_width = w;
3431 graphic_height = h;
3432 graphic_depth = depth;
3434 break;
3435 case QEMU_OPTION_echr:
3437 char *r;
3438 term_escape_char = strtol(optarg, &r, 0);
3439 if (r == optarg)
3440 printf("Bad argument to echr\n");
3441 break;
3443 case QEMU_OPTION_monitor:
3444 default_monitor = 0;
3445 if (strncmp(optarg, "none", 4)) {
3446 monitor_parse(optarg, "readline");
3448 break;
3449 case QEMU_OPTION_qmp:
3450 monitor_parse(optarg, "control");
3451 default_monitor = 0;
3452 break;
3453 case QEMU_OPTION_mon:
3454 opts = qemu_opts_parse(qemu_find_opts("mon"), optarg, 1);
3455 if (!opts) {
3456 exit(1);
3458 default_monitor = 0;
3459 break;
3460 case QEMU_OPTION_chardev:
3461 opts = qemu_opts_parse(qemu_find_opts("chardev"), optarg, 1);
3462 if (!opts) {
3463 exit(1);
3465 break;
3466 case QEMU_OPTION_fsdev:
3467 olist = qemu_find_opts("fsdev");
3468 if (!olist) {
3469 fprintf(stderr, "fsdev is not supported by this qemu build.\n");
3470 exit(1);
3472 opts = qemu_opts_parse(olist, optarg, 1);
3473 if (!opts) {
3474 exit(1);
3476 break;
3477 case QEMU_OPTION_virtfs: {
3478 QemuOpts *fsdev;
3479 QemuOpts *device;
3480 const char *writeout, *sock_fd, *socket;
3482 olist = qemu_find_opts("virtfs");
3483 if (!olist) {
3484 fprintf(stderr, "virtfs is not supported by this qemu build.\n");
3485 exit(1);
3487 opts = qemu_opts_parse(olist, optarg, 1);
3488 if (!opts) {
3489 exit(1);
3492 if (qemu_opt_get(opts, "fsdriver") == NULL ||
3493 qemu_opt_get(opts, "mount_tag") == NULL) {
3494 fprintf(stderr, "Usage: -virtfs fsdriver,mount_tag=tag.\n");
3495 exit(1);
3497 fsdev = qemu_opts_create(qemu_find_opts("fsdev"),
3498 qemu_opt_get(opts, "mount_tag"),
3499 1, NULL);
3500 if (!fsdev) {
3501 fprintf(stderr, "duplicate fsdev id: %s\n",
3502 qemu_opt_get(opts, "mount_tag"));
3503 exit(1);
3506 writeout = qemu_opt_get(opts, "writeout");
3507 if (writeout) {
3508 #ifdef CONFIG_SYNC_FILE_RANGE
3509 qemu_opt_set(fsdev, "writeout", writeout);
3510 #else
3511 fprintf(stderr, "writeout=immediate not supported on "
3512 "this platform\n");
3513 exit(1);
3514 #endif
3516 qemu_opt_set(fsdev, "fsdriver", qemu_opt_get(opts, "fsdriver"));
3517 qemu_opt_set(fsdev, "path", qemu_opt_get(opts, "path"));
3518 qemu_opt_set(fsdev, "security_model",
3519 qemu_opt_get(opts, "security_model"));
3520 socket = qemu_opt_get(opts, "socket");
3521 if (socket) {
3522 qemu_opt_set(fsdev, "socket", socket);
3524 sock_fd = qemu_opt_get(opts, "sock_fd");
3525 if (sock_fd) {
3526 qemu_opt_set(fsdev, "sock_fd", sock_fd);
3529 qemu_opt_set_bool(fsdev, "readonly",
3530 qemu_opt_get_bool(opts, "readonly", 0));
3531 device = qemu_opts_create(qemu_find_opts("device"), NULL, 0,
3532 &error_abort);
3533 qemu_opt_set(device, "driver", "virtio-9p-pci");
3534 qemu_opt_set(device, "fsdev",
3535 qemu_opt_get(opts, "mount_tag"));
3536 qemu_opt_set(device, "mount_tag",
3537 qemu_opt_get(opts, "mount_tag"));
3538 break;
3540 case QEMU_OPTION_virtfs_synth: {
3541 QemuOpts *fsdev;
3542 QemuOpts *device;
3544 fsdev = qemu_opts_create(qemu_find_opts("fsdev"), "v_synth",
3545 1, NULL);
3546 if (!fsdev) {
3547 fprintf(stderr, "duplicate option: %s\n", "virtfs_synth");
3548 exit(1);
3550 qemu_opt_set(fsdev, "fsdriver", "synth");
3552 device = qemu_opts_create(qemu_find_opts("device"), NULL, 0,
3553 &error_abort);
3554 qemu_opt_set(device, "driver", "virtio-9p-pci");
3555 qemu_opt_set(device, "fsdev", "v_synth");
3556 qemu_opt_set(device, "mount_tag", "v_synth");
3557 break;
3559 case QEMU_OPTION_serial:
3560 add_device_config(DEV_SERIAL, optarg);
3561 default_serial = 0;
3562 if (strncmp(optarg, "mon:", 4) == 0) {
3563 default_monitor = 0;
3565 break;
3566 case QEMU_OPTION_watchdog:
3567 if (watchdog) {
3568 fprintf(stderr,
3569 "qemu: only one watchdog option may be given\n");
3570 return 1;
3572 watchdog = optarg;
3573 break;
3574 case QEMU_OPTION_watchdog_action:
3575 if (select_watchdog_action(optarg) == -1) {
3576 fprintf(stderr, "Unknown -watchdog-action parameter\n");
3577 exit(1);
3579 break;
3580 case QEMU_OPTION_virtiocon:
3581 add_device_config(DEV_VIRTCON, optarg);
3582 default_virtcon = 0;
3583 if (strncmp(optarg, "mon:", 4) == 0) {
3584 default_monitor = 0;
3586 break;
3587 case QEMU_OPTION_parallel:
3588 add_device_config(DEV_PARALLEL, optarg);
3589 default_parallel = 0;
3590 if (strncmp(optarg, "mon:", 4) == 0) {
3591 default_monitor = 0;
3593 break;
3594 case QEMU_OPTION_debugcon:
3595 add_device_config(DEV_DEBUGCON, optarg);
3596 break;
3597 case QEMU_OPTION_loadvm:
3598 loadvm = optarg;
3599 break;
3600 case QEMU_OPTION_full_screen:
3601 full_screen = 1;
3602 break;
3603 case QEMU_OPTION_no_frame:
3604 no_frame = 1;
3605 break;
3606 case QEMU_OPTION_alt_grab:
3607 alt_grab = 1;
3608 break;
3609 case QEMU_OPTION_ctrl_grab:
3610 ctrl_grab = 1;
3611 break;
3612 case QEMU_OPTION_no_quit:
3613 no_quit = 1;
3614 break;
3615 case QEMU_OPTION_sdl:
3616 #ifdef CONFIG_SDL
3617 display_type = DT_SDL;
3618 break;
3619 #else
3620 fprintf(stderr, "SDL support is disabled\n");
3621 exit(1);
3622 #endif
3623 case QEMU_OPTION_pidfile:
3624 pid_file = optarg;
3625 break;
3626 case QEMU_OPTION_win2k_hack:
3627 win2k_install_hack = 1;
3628 break;
3629 case QEMU_OPTION_rtc_td_hack: {
3630 static GlobalProperty slew_lost_ticks[] = {
3632 .driver = "mc146818rtc",
3633 .property = "lost_tick_policy",
3634 .value = "slew",
3636 { /* end of list */ }
3639 qdev_prop_register_global_list(slew_lost_ticks);
3640 break;
3642 case QEMU_OPTION_acpitable:
3643 opts = qemu_opts_parse(qemu_find_opts("acpi"), optarg, 1);
3644 if (!opts) {
3645 exit(1);
3647 do_acpitable_option(opts);
3648 break;
3649 case QEMU_OPTION_smbios:
3650 opts = qemu_opts_parse(qemu_find_opts("smbios"), optarg, 0);
3651 if (!opts) {
3652 exit(1);
3654 do_smbios_option(opts);
3655 break;
3656 case QEMU_OPTION_enable_kvm:
3657 olist = qemu_find_opts("machine");
3658 qemu_opts_parse(olist, "accel=kvm", 0);
3659 break;
3660 case QEMU_OPTION_machine:
3661 olist = qemu_find_opts("machine");
3662 opts = qemu_opts_parse(olist, optarg, 1);
3663 if (!opts) {
3664 exit(1);
3666 optarg = qemu_opt_get(opts, "type");
3667 if (optarg) {
3668 machine_class = machine_parse(optarg);
3670 break;
3671 case QEMU_OPTION_no_kvm:
3672 olist = qemu_find_opts("machine");
3673 qemu_opts_parse(olist, "accel=tcg", 0);
3674 break;
3675 case QEMU_OPTION_no_kvm_pit: {
3676 fprintf(stderr, "Warning: KVM PIT can no longer be disabled "
3677 "separately.\n");
3678 break;
3680 case QEMU_OPTION_no_kvm_pit_reinjection: {
3681 static GlobalProperty kvm_pit_lost_tick_policy[] = {
3683 .driver = "kvm-pit",
3684 .property = "lost_tick_policy",
3685 .value = "discard",
3687 { /* end of list */ }
3690 fprintf(stderr, "Warning: option deprecated, use "
3691 "lost_tick_policy property of kvm-pit instead.\n");
3692 qdev_prop_register_global_list(kvm_pit_lost_tick_policy);
3693 break;
3695 case QEMU_OPTION_usb:
3696 olist = qemu_find_opts("machine");
3697 qemu_opts_parse(olist, "usb=on", 0);
3698 break;
3699 case QEMU_OPTION_usbdevice:
3700 olist = qemu_find_opts("machine");
3701 qemu_opts_parse(olist, "usb=on", 0);
3702 add_device_config(DEV_USB, optarg);
3703 break;
3704 case QEMU_OPTION_device:
3705 if (!qemu_opts_parse(qemu_find_opts("device"), optarg, 1)) {
3706 exit(1);
3708 break;
3709 case QEMU_OPTION_smp:
3710 if (!qemu_opts_parse(qemu_find_opts("smp-opts"), optarg, 1)) {
3711 exit(1);
3713 break;
3714 case QEMU_OPTION_vnc:
3715 #ifdef CONFIG_VNC
3716 display_remote++;
3717 vnc_display = optarg;
3718 #else
3719 fprintf(stderr, "VNC support is disabled\n");
3720 exit(1);
3721 #endif
3722 break;
3723 case QEMU_OPTION_no_acpi:
3724 acpi_enabled = 0;
3725 break;
3726 case QEMU_OPTION_no_hpet:
3727 no_hpet = 1;
3728 break;
3729 case QEMU_OPTION_balloon:
3730 if (balloon_parse(optarg) < 0) {
3731 fprintf(stderr, "Unknown -balloon argument %s\n", optarg);
3732 exit(1);
3734 break;
3735 case QEMU_OPTION_no_reboot:
3736 no_reboot = 1;
3737 break;
3738 case QEMU_OPTION_no_shutdown:
3739 no_shutdown = 1;
3740 break;
3741 case QEMU_OPTION_show_cursor:
3742 cursor_hide = 0;
3743 break;
3744 case QEMU_OPTION_uuid:
3745 if(qemu_uuid_parse(optarg, qemu_uuid) < 0) {
3746 fprintf(stderr, "Fail to parse UUID string."
3747 " Wrong format.\n");
3748 exit(1);
3750 qemu_uuid_set = true;
3751 break;
3752 case QEMU_OPTION_option_rom:
3753 if (nb_option_roms >= MAX_OPTION_ROMS) {
3754 fprintf(stderr, "Too many option ROMs\n");
3755 exit(1);
3757 opts = qemu_opts_parse(qemu_find_opts("option-rom"), optarg, 1);
3758 if (!opts) {
3759 exit(1);
3761 option_rom[nb_option_roms].name = qemu_opt_get(opts, "romfile");
3762 option_rom[nb_option_roms].bootindex =
3763 qemu_opt_get_number(opts, "bootindex", -1);
3764 if (!option_rom[nb_option_roms].name) {
3765 fprintf(stderr, "Option ROM file is not specified\n");
3766 exit(1);
3768 nb_option_roms++;
3769 break;
3770 case QEMU_OPTION_semihosting:
3771 semihosting_enabled = 1;
3772 break;
3773 case QEMU_OPTION_tdf:
3774 fprintf(stderr, "Warning: user space PIT time drift fix "
3775 "is no longer supported.\n");
3776 break;
3777 case QEMU_OPTION_name:
3778 opts = qemu_opts_parse(qemu_find_opts("name"), optarg, 1);
3779 if (!opts) {
3780 exit(1);
3782 break;
3783 case QEMU_OPTION_prom_env:
3784 if (nb_prom_envs >= MAX_PROM_ENVS) {
3785 fprintf(stderr, "Too many prom variables\n");
3786 exit(1);
3788 prom_envs[nb_prom_envs] = optarg;
3789 nb_prom_envs++;
3790 break;
3791 case QEMU_OPTION_old_param:
3792 old_param = 1;
3793 break;
3794 case QEMU_OPTION_clock:
3795 /* Clock options no longer exist. Keep this option for
3796 * backward compatibility.
3798 break;
3799 case QEMU_OPTION_startdate:
3800 configure_rtc_date_offset(optarg, 1);
3801 break;
3802 case QEMU_OPTION_rtc:
3803 opts = qemu_opts_parse(qemu_find_opts("rtc"), optarg, 0);
3804 if (!opts) {
3805 exit(1);
3807 configure_rtc(opts);
3808 break;
3809 case QEMU_OPTION_tb_size:
3810 tcg_tb_size = strtol(optarg, NULL, 0);
3811 if (tcg_tb_size < 0) {
3812 tcg_tb_size = 0;
3814 break;
3815 case QEMU_OPTION_icount:
3816 icount_option = optarg;
3817 break;
3818 case QEMU_OPTION_incoming:
3819 incoming = optarg;
3820 runstate_set(RUN_STATE_INMIGRATE);
3821 break;
3822 case QEMU_OPTION_nodefaults:
3823 has_defaults = 0;
3824 break;
3825 case QEMU_OPTION_xen_domid:
3826 if (!(xen_available())) {
3827 printf("Option %s not supported for this target\n", popt->name);
3828 exit(1);
3830 xen_domid = atoi(optarg);
3831 break;
3832 case QEMU_OPTION_xen_create:
3833 if (!(xen_available())) {
3834 printf("Option %s not supported for this target\n", popt->name);
3835 exit(1);
3837 xen_mode = XEN_CREATE;
3838 break;
3839 case QEMU_OPTION_xen_attach:
3840 if (!(xen_available())) {
3841 printf("Option %s not supported for this target\n", popt->name);
3842 exit(1);
3844 xen_mode = XEN_ATTACH;
3845 break;
3846 case QEMU_OPTION_trace:
3848 opts = qemu_opts_parse(qemu_find_opts("trace"), optarg, 0);
3849 if (!opts) {
3850 exit(1);
3852 trace_events = qemu_opt_get(opts, "events");
3853 trace_file = qemu_opt_get(opts, "file");
3854 break;
3856 case QEMU_OPTION_readconfig:
3858 int ret = qemu_read_config_file(optarg);
3859 if (ret < 0) {
3860 fprintf(stderr, "read config %s: %s\n", optarg,
3861 strerror(-ret));
3862 exit(1);
3864 break;
3866 case QEMU_OPTION_spice:
3867 olist = qemu_find_opts("spice");
3868 if (!olist) {
3869 fprintf(stderr, "spice is not supported by this qemu build.\n");
3870 exit(1);
3872 opts = qemu_opts_parse(olist, optarg, 0);
3873 if (!opts) {
3874 exit(1);
3876 display_remote++;
3877 break;
3878 case QEMU_OPTION_writeconfig:
3880 FILE *fp;
3881 if (strcmp(optarg, "-") == 0) {
3882 fp = stdout;
3883 } else {
3884 fp = fopen(optarg, "w");
3885 if (fp == NULL) {
3886 fprintf(stderr, "open %s: %s\n", optarg, strerror(errno));
3887 exit(1);
3890 qemu_config_write(fp);
3891 if (fp != stdout) {
3892 fclose(fp);
3894 break;
3896 case QEMU_OPTION_qtest:
3897 qtest_chrdev = optarg;
3898 break;
3899 case QEMU_OPTION_qtest_log:
3900 qtest_log = optarg;
3901 break;
3902 case QEMU_OPTION_sandbox:
3903 opts = qemu_opts_parse(qemu_find_opts("sandbox"), optarg, 1);
3904 if (!opts) {
3905 exit(1);
3907 break;
3908 case QEMU_OPTION_add_fd:
3909 #ifndef _WIN32
3910 opts = qemu_opts_parse(qemu_find_opts("add-fd"), optarg, 0);
3911 if (!opts) {
3912 exit(1);
3914 #else
3915 error_report("File descriptor passing is disabled on this "
3916 "platform");
3917 exit(1);
3918 #endif
3919 break;
3920 case QEMU_OPTION_object:
3921 opts = qemu_opts_parse(qemu_find_opts("object"), optarg, 1);
3922 if (!opts) {
3923 exit(1);
3925 break;
3926 case QEMU_OPTION_realtime:
3927 opts = qemu_opts_parse(qemu_find_opts("realtime"), optarg, 0);
3928 if (!opts) {
3929 exit(1);
3931 configure_realtime(opts);
3932 break;
3933 case QEMU_OPTION_msg:
3934 opts = qemu_opts_parse(qemu_find_opts("msg"), optarg, 0);
3935 if (!opts) {
3936 exit(1);
3938 configure_msg(opts);
3939 break;
3940 default:
3941 os_parse_cmd_args(popt->index, optarg);
3945 loc_set_none();
3947 os_daemonize();
3949 if (qemu_init_main_loop()) {
3950 fprintf(stderr, "qemu_init_main_loop failed\n");
3951 exit(1);
3954 if (qemu_opts_foreach(qemu_find_opts("sandbox"), parse_sandbox, NULL, 0)) {
3955 exit(1);
3958 if (qemu_opts_foreach(qemu_find_opts("name"), parse_name, NULL, 1)) {
3959 exit(1);
3962 #ifndef _WIN32
3963 if (qemu_opts_foreach(qemu_find_opts("add-fd"), parse_add_fd, NULL, 1)) {
3964 exit(1);
3967 if (qemu_opts_foreach(qemu_find_opts("add-fd"), cleanup_add_fd, NULL, 1)) {
3968 exit(1);
3970 #endif
3972 if (machine_class == NULL) {
3973 fprintf(stderr, "No machine specified, and there is no default.\n"
3974 "Use -machine help to list supported machines!\n");
3975 exit(1);
3978 cpu_exec_init_all();
3980 current_machine = MACHINE(object_new(object_class_get_name(
3981 OBJECT_CLASS(machine_class))));
3982 object_property_add_child(object_get_root(), "machine",
3983 OBJECT(current_machine), &error_abort);
3985 if (machine_class->hw_version) {
3986 qemu_set_version(machine_class->hw_version);
3989 if (qemu_opts_foreach(qemu_find_opts("object"),
3990 object_create, NULL, 0) != 0) {
3991 exit(1);
3994 /* Init CPU def lists, based on config
3995 * - Must be called after all the qemu_read_config_file() calls
3996 * - Must be called before list_cpus()
3997 * - Must be called before machine->init()
3999 cpudef_init();
4001 if (cpu_model && is_help_option(cpu_model)) {
4002 list_cpus(stdout, &fprintf, cpu_model);
4003 exit(0);
4006 /* Open the logfile at this point, if necessary. We can't open the logfile
4007 * when encountering either of the logging options (-d or -D) because the
4008 * other one may be encountered later on the command line, changing the
4009 * location or level of logging.
4011 if (log_mask) {
4012 int mask;
4013 if (log_file) {
4014 qemu_set_log_filename(log_file);
4017 mask = qemu_str_to_log_mask(log_mask);
4018 if (!mask) {
4019 qemu_print_log_usage(stdout);
4020 exit(1);
4022 qemu_set_log(mask);
4025 if (!is_daemonized()) {
4026 if (!trace_init_backends(trace_events, trace_file)) {
4027 exit(1);
4031 /* If no data_dir is specified then try to find it relative to the
4032 executable path. */
4033 if (data_dir_idx < ARRAY_SIZE(data_dir)) {
4034 data_dir[data_dir_idx] = os_find_datadir();
4035 if (data_dir[data_dir_idx] != NULL) {
4036 data_dir_idx++;
4039 /* If all else fails use the install path specified when building. */
4040 if (data_dir_idx < ARRAY_SIZE(data_dir)) {
4041 data_dir[data_dir_idx++] = CONFIG_QEMU_DATADIR;
4044 smp_parse(qemu_opts_find(qemu_find_opts("smp-opts"), NULL));
4046 machine_class->max_cpus = machine_class->max_cpus ?: 1; /* Default to UP */
4047 if (smp_cpus > machine_class->max_cpus) {
4048 fprintf(stderr, "Number of SMP cpus requested (%d), exceeds max cpus "
4049 "supported by machine `%s' (%d)\n", smp_cpus,
4050 machine_class->name, machine_class->max_cpus);
4051 exit(1);
4055 * Get the default machine options from the machine if it is not already
4056 * specified either by the configuration file or by the command line.
4058 if (machine_class->default_machine_opts) {
4059 qemu_opts_set_defaults(qemu_find_opts("machine"),
4060 machine_class->default_machine_opts, 0);
4063 qemu_opts_foreach(qemu_find_opts("device"), default_driver_check, NULL, 0);
4064 qemu_opts_foreach(qemu_find_opts("global"), default_driver_check, NULL, 0);
4066 if (!vga_model && !default_vga) {
4067 vga_interface_type = VGA_DEVICE;
4069 if (!has_defaults || machine_class->no_serial) {
4070 default_serial = 0;
4072 if (!has_defaults || machine_class->no_parallel) {
4073 default_parallel = 0;
4075 if (!has_defaults || !machine_class->use_virtcon) {
4076 default_virtcon = 0;
4078 if (!has_defaults || !machine_class->use_sclp) {
4079 default_sclp = 0;
4081 if (!has_defaults || machine_class->no_floppy) {
4082 default_floppy = 0;
4084 if (!has_defaults || machine_class->no_cdrom) {
4085 default_cdrom = 0;
4087 if (!has_defaults || machine_class->no_sdcard) {
4088 default_sdcard = 0;
4090 if (!has_defaults) {
4091 default_monitor = 0;
4092 default_net = 0;
4093 default_vga = 0;
4096 if (is_daemonized()) {
4097 /* According to documentation and historically, -nographic redirects
4098 * serial port, parallel port and monitor to stdio, which does not work
4099 * with -daemonize. We can redirect these to null instead, but since
4100 * -nographic is legacy, let's just error out.
4101 * We disallow -nographic only if all other ports are not redirected
4102 * explicitly, to not break existing legacy setups which uses
4103 * -nographic _and_ redirects all ports explicitly - this is valid
4104 * usage, -nographic is just a no-op in this case.
4106 if (display_type == DT_NOGRAPHIC
4107 && (default_parallel || default_serial
4108 || default_monitor || default_virtcon)) {
4109 fprintf(stderr, "-nographic can not be used with -daemonize\n");
4110 exit(1);
4112 #ifdef CONFIG_CURSES
4113 if (display_type == DT_CURSES) {
4114 fprintf(stderr, "curses display can not be used with -daemonize\n");
4115 exit(1);
4117 #endif
4120 if (display_type == DT_NOGRAPHIC) {
4121 if (default_parallel)
4122 add_device_config(DEV_PARALLEL, "null");
4123 if (default_serial && default_monitor) {
4124 add_device_config(DEV_SERIAL, "mon:stdio");
4125 } else if (default_virtcon && default_monitor) {
4126 add_device_config(DEV_VIRTCON, "mon:stdio");
4127 } else if (default_sclp && default_monitor) {
4128 add_device_config(DEV_SCLP, "mon:stdio");
4129 } else {
4130 if (default_serial)
4131 add_device_config(DEV_SERIAL, "stdio");
4132 if (default_virtcon)
4133 add_device_config(DEV_VIRTCON, "stdio");
4134 if (default_sclp) {
4135 add_device_config(DEV_SCLP, "stdio");
4137 if (default_monitor)
4138 monitor_parse("stdio", "readline");
4140 } else {
4141 if (default_serial)
4142 add_device_config(DEV_SERIAL, "vc:80Cx24C");
4143 if (default_parallel)
4144 add_device_config(DEV_PARALLEL, "vc:80Cx24C");
4145 if (default_monitor)
4146 monitor_parse("vc:80Cx24C", "readline");
4147 if (default_virtcon)
4148 add_device_config(DEV_VIRTCON, "vc:80Cx24C");
4149 if (default_sclp) {
4150 add_device_config(DEV_SCLP, "vc:80Cx24C");
4154 if (display_type == DT_DEFAULT && !display_remote) {
4155 #if defined(CONFIG_GTK)
4156 display_type = DT_GTK;
4157 #elif defined(CONFIG_SDL) || defined(CONFIG_COCOA)
4158 display_type = DT_SDL;
4159 #elif defined(CONFIG_VNC)
4160 vnc_display = "localhost:0,to=99";
4161 show_vnc_port = 1;
4162 #else
4163 display_type = DT_NONE;
4164 #endif
4167 if ((no_frame || alt_grab || ctrl_grab) && display_type != DT_SDL) {
4168 fprintf(stderr, "-no-frame, -alt-grab and -ctrl-grab are only valid "
4169 "for SDL, ignoring option\n");
4171 if (no_quit && (display_type != DT_GTK && display_type != DT_SDL)) {
4172 fprintf(stderr, "-no-quit is only valid for GTK and SDL, "
4173 "ignoring option\n");
4176 #if defined(CONFIG_GTK)
4177 if (display_type == DT_GTK) {
4178 early_gtk_display_init();
4180 #endif
4182 socket_init();
4184 if (qemu_opts_foreach(qemu_find_opts("chardev"), chardev_init_func, NULL, 1) != 0)
4185 exit(1);
4186 #ifdef CONFIG_VIRTFS
4187 if (qemu_opts_foreach(qemu_find_opts("fsdev"), fsdev_init_func, NULL, 1) != 0) {
4188 exit(1);
4190 #endif
4192 if (pid_file && qemu_create_pidfile(pid_file) != 0) {
4193 os_pidfile_error();
4194 exit(1);
4197 /* store value for the future use */
4198 qemu_opt_set_number(qemu_find_opts_singleton("memory"), "size", ram_size);
4200 if (qemu_opts_foreach(qemu_find_opts("device"), device_help_func, NULL, 0)
4201 != 0) {
4202 exit(0);
4205 machine_opts = qemu_get_machine_opts();
4206 if (qemu_opt_foreach(machine_opts, object_set_property, current_machine,
4207 1) < 0) {
4208 object_unref(OBJECT(current_machine));
4209 exit(1);
4212 configure_accelerator(machine_class);
4214 if (qtest_chrdev) {
4215 Error *local_err = NULL;
4216 qtest_init(qtest_chrdev, qtest_log, &local_err);
4217 if (local_err) {
4218 error_report("%s", error_get_pretty(local_err));
4219 error_free(local_err);
4220 exit(1);
4224 machine_opts = qemu_get_machine_opts();
4225 kernel_filename = qemu_opt_get(machine_opts, "kernel");
4226 initrd_filename = qemu_opt_get(machine_opts, "initrd");
4227 kernel_cmdline = qemu_opt_get(machine_opts, "append");
4228 bios_name = qemu_opt_get(machine_opts, "firmware");
4230 boot_order = machine_class->default_boot_order;
4231 opts = qemu_opts_find(qemu_find_opts("boot-opts"), NULL);
4232 if (opts) {
4233 char *normal_boot_order;
4234 const char *order, *once;
4236 order = qemu_opt_get(opts, "order");
4237 if (order) {
4238 validate_bootdevices(order);
4239 boot_order = order;
4242 once = qemu_opt_get(opts, "once");
4243 if (once) {
4244 validate_bootdevices(once);
4245 normal_boot_order = g_strdup(boot_order);
4246 boot_order = once;
4247 qemu_register_reset(restore_boot_order, normal_boot_order);
4250 boot_menu = qemu_opt_get_bool(opts, "menu", boot_menu);
4251 boot_strict = qemu_opt_get_bool(opts, "strict", false);
4254 if (!kernel_cmdline) {
4255 kernel_cmdline = "";
4256 current_machine->kernel_cmdline = (char *)kernel_cmdline;
4259 linux_boot = (kernel_filename != NULL);
4261 if (!linux_boot && *kernel_cmdline != '\0') {
4262 fprintf(stderr, "-append only allowed with -kernel option\n");
4263 exit(1);
4266 if (!linux_boot && initrd_filename != NULL) {
4267 fprintf(stderr, "-initrd only allowed with -kernel option\n");
4268 exit(1);
4271 if (!linux_boot && qemu_opt_get(machine_opts, "dtb")) {
4272 fprintf(stderr, "-dtb only allowed with -kernel option\n");
4273 exit(1);
4276 os_set_line_buffering();
4278 qemu_init_cpu_loop();
4279 qemu_mutex_lock_iothread();
4281 #ifdef CONFIG_SPICE
4282 /* spice needs the timers to be initialized by this point */
4283 qemu_spice_init();
4284 #endif
4286 if (icount_option && (kvm_enabled() || xen_enabled())) {
4287 fprintf(stderr, "-icount is not allowed with kvm or xen\n");
4288 exit(1);
4290 configure_icount(icount_option);
4292 /* clean up network at qemu process termination */
4293 atexit(&net_cleanup);
4295 if (net_init_clients() < 0) {
4296 exit(1);
4299 #ifdef CONFIG_TPM
4300 if (tpm_init() < 0) {
4301 exit(1);
4303 #endif
4305 /* init the bluetooth world */
4306 if (foreach_device_config(DEV_BT, bt_parse))
4307 exit(1);
4309 if (!xen_enabled()) {
4310 /* On 32-bit hosts, QEMU is limited by virtual address space */
4311 if (ram_size > (2047 << 20) && HOST_LONG_BITS == 32) {
4312 fprintf(stderr, "qemu: at most 2047 MB RAM can be simulated\n");
4313 exit(1);
4317 blk_mig_init();
4318 ram_mig_init();
4320 /* open the virtual block devices */
4321 if (snapshot)
4322 qemu_opts_foreach(qemu_find_opts("drive"), drive_enable_snapshot, NULL, 0);
4323 if (qemu_opts_foreach(qemu_find_opts("drive"), drive_init_func,
4324 &machine_class->block_default_type, 1) != 0) {
4325 exit(1);
4328 default_drive(default_cdrom, snapshot, machine_class->block_default_type, 2,
4329 CDROM_OPTS);
4330 default_drive(default_floppy, snapshot, IF_FLOPPY, 0, FD_OPTS);
4331 default_drive(default_sdcard, snapshot, IF_SD, 0, SD_OPTS);
4333 if (qemu_opts_foreach(qemu_find_opts("numa"), numa_init_func,
4334 NULL, 1) != 0) {
4335 exit(1);
4338 set_numa_nodes();
4340 if (qemu_opts_foreach(qemu_find_opts("mon"), mon_init_func, NULL, 1) != 0) {
4341 exit(1);
4344 if (foreach_device_config(DEV_SERIAL, serial_parse) < 0)
4345 exit(1);
4346 if (foreach_device_config(DEV_PARALLEL, parallel_parse) < 0)
4347 exit(1);
4348 if (foreach_device_config(DEV_VIRTCON, virtcon_parse) < 0)
4349 exit(1);
4350 if (foreach_device_config(DEV_SCLP, sclp_parse) < 0) {
4351 exit(1);
4353 if (foreach_device_config(DEV_DEBUGCON, debugcon_parse) < 0)
4354 exit(1);
4356 /* If no default VGA is requested, the default is "none". */
4357 if (default_vga) {
4358 if (cirrus_vga_available()) {
4359 vga_model = "cirrus";
4360 } else if (vga_available()) {
4361 vga_model = "std";
4364 if (vga_model) {
4365 select_vgahw(vga_model);
4368 if (watchdog) {
4369 i = select_watchdog(watchdog);
4370 if (i > 0)
4371 exit (i == 1 ? 1 : 0);
4374 if (machine_class->compat_props) {
4375 qdev_prop_register_global_list(machine_class->compat_props);
4377 qemu_add_globals();
4379 qdev_machine_init();
4381 current_machine->ram_size = ram_size;
4382 current_machine->maxram_size = maxram_size;
4383 current_machine->ram_slots = ram_slots;
4384 current_machine->boot_order = boot_order;
4385 current_machine->cpu_model = cpu_model;
4387 machine_class->init(current_machine);
4389 audio_init();
4391 cpu_synchronize_all_post_init();
4393 set_numa_modes();
4395 /* init USB devices */
4396 if (usb_enabled(false)) {
4397 if (foreach_device_config(DEV_USB, usb_parse) < 0)
4398 exit(1);
4401 /* init generic devices */
4402 if (qemu_opts_foreach(qemu_find_opts("device"), device_init_func, NULL, 1) != 0)
4403 exit(1);
4405 net_check_clients();
4407 ds = init_displaystate();
4409 /* init local displays */
4410 switch (display_type) {
4411 case DT_NOGRAPHIC:
4412 (void)ds; /* avoid warning if no display is configured */
4413 break;
4414 #if defined(CONFIG_CURSES)
4415 case DT_CURSES:
4416 curses_display_init(ds, full_screen);
4417 break;
4418 #endif
4419 #if defined(CONFIG_SDL)
4420 case DT_SDL:
4421 sdl_display_init(ds, full_screen, no_frame);
4422 break;
4423 #elif defined(CONFIG_COCOA)
4424 case DT_SDL:
4425 cocoa_display_init(ds, full_screen);
4426 break;
4427 #endif
4428 #if defined(CONFIG_GTK)
4429 case DT_GTK:
4430 gtk_display_init(ds, full_screen, grab_on_hover);
4431 break;
4432 #endif
4433 default:
4434 break;
4437 /* must be after terminal init, SDL library changes signal handlers */
4438 os_setup_signal_handling();
4440 #ifdef CONFIG_VNC
4441 /* init remote displays */
4442 if (vnc_display) {
4443 Error *local_err = NULL;
4444 vnc_display_init(ds);
4445 vnc_display_open(ds, vnc_display, &local_err);
4446 if (local_err != NULL) {
4447 error_report("Failed to start VNC server on `%s': %s",
4448 vnc_display, error_get_pretty(local_err));
4449 error_free(local_err);
4450 exit(1);
4453 if (show_vnc_port) {
4454 printf("VNC server running on `%s'\n", vnc_display_local_addr(ds));
4457 #endif
4458 #ifdef CONFIG_SPICE
4459 if (using_spice) {
4460 qemu_spice_display_init();
4462 #endif
4464 if (foreach_device_config(DEV_GDB, gdbserver_start) < 0) {
4465 exit(1);
4468 qdev_machine_creation_done();
4470 if (rom_load_all() != 0) {
4471 fprintf(stderr, "rom loading failed\n");
4472 exit(1);
4475 /* TODO: once all bus devices are qdevified, this should be done
4476 * when bus is created by qdev.c */
4477 qemu_register_reset(qbus_reset_all_fn, sysbus_get_default());
4478 qemu_run_machine_init_done_notifiers();
4480 /* Done notifiers can load ROMs */
4481 rom_load_done();
4483 qemu_system_reset(VMRESET_SILENT);
4484 if (loadvm) {
4485 if (load_vmstate(loadvm) < 0) {
4486 autostart = 0;
4490 qdev_prop_check_global();
4492 if (incoming) {
4493 Error *local_err = NULL;
4494 qemu_start_incoming_migration(incoming, &local_err);
4495 if (local_err) {
4496 error_report("-incoming %s: %s", incoming,
4497 error_get_pretty(local_err));
4498 error_free(local_err);
4499 exit(1);
4501 } else if (autostart) {
4502 vm_start();
4505 os_setup_post();
4507 if (is_daemonized()) {
4508 if (!trace_init_backends(trace_events, trace_file)) {
4509 exit(1);
4513 main_loop();
4514 bdrv_close_all();
4515 pause_all_vcpus();
4516 res_free();
4517 #ifdef CONFIG_TPM
4518 tpm_cleanup();
4519 #endif
4521 return 0;