pc & q35: Add new machine opt max-ram-below-4g
[qemu-kvm.git] / vl.c
blob194c6f79a302071399924631f68501166a009d79
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"
121 #define DEFAULT_RAM_SIZE 128
123 #define MAX_VIRTIO_CONSOLES 1
124 #define MAX_SCLP_CONSOLES 1
126 static const char *data_dir[16];
127 static int data_dir_idx;
128 const char *bios_name = NULL;
129 enum vga_retrace_method vga_retrace_method = VGA_RETRACE_DUMB;
130 DisplayType display_type = DT_DEFAULT;
131 static int display_remote;
132 const char* keyboard_layout = NULL;
133 ram_addr_t ram_size;
134 const char *mem_path = NULL;
135 int mem_prealloc = 0; /* force preallocation of physical target memory */
136 int nb_nics;
137 NICInfo nd_table[MAX_NICS];
138 int autostart;
139 static int rtc_utc = 1;
140 static int rtc_date_offset = -1; /* -1 means no change */
141 QEMUClockType rtc_clock;
142 int vga_interface_type = VGA_NONE;
143 static int full_screen = 0;
144 static int no_frame = 0;
145 int no_quit = 0;
146 #ifdef CONFIG_GTK
147 static bool grab_on_hover;
148 #endif
149 CharDriverState *serial_hds[MAX_SERIAL_PORTS];
150 CharDriverState *parallel_hds[MAX_PARALLEL_PORTS];
151 CharDriverState *virtcon_hds[MAX_VIRTIO_CONSOLES];
152 CharDriverState *sclp_hds[MAX_SCLP_CONSOLES];
153 int win2k_install_hack = 0;
154 int singlestep = 0;
155 int smp_cpus = 1;
156 int max_cpus = 0;
157 int smp_cores = 1;
158 int smp_threads = 1;
159 #ifdef CONFIG_VNC
160 const char *vnc_display;
161 #endif
162 int acpi_enabled = 1;
163 int no_hpet = 0;
164 int fd_bootchk = 1;
165 static int no_reboot;
166 int no_shutdown = 0;
167 int cursor_hide = 1;
168 int graphic_rotate = 0;
169 const char *watchdog;
170 QEMUOptionRom option_rom[MAX_OPTION_ROMS];
171 int nb_option_roms;
172 int semihosting_enabled = 0;
173 int old_param = 0;
174 const char *qemu_name;
175 int alt_grab = 0;
176 int ctrl_grab = 0;
177 unsigned int nb_prom_envs = 0;
178 const char *prom_envs[MAX_PROM_ENVS];
179 int boot_menu;
180 static bool boot_strict;
181 uint8_t *boot_splash_filedata;
182 size_t boot_splash_filedata_size;
183 uint8_t qemu_extra_params_fw[2];
185 typedef struct FWBootEntry FWBootEntry;
187 struct FWBootEntry {
188 QTAILQ_ENTRY(FWBootEntry) link;
189 int32_t bootindex;
190 DeviceState *dev;
191 char *suffix;
194 static QTAILQ_HEAD(, FWBootEntry) fw_boot_order =
195 QTAILQ_HEAD_INITIALIZER(fw_boot_order);
197 int nb_numa_nodes;
198 NodeInfo numa_info[MAX_NODES];
200 uint8_t qemu_uuid[16];
201 bool qemu_uuid_set;
203 static QEMUBootSetHandler *boot_set_handler;
204 static void *boot_set_opaque;
206 static NotifierList exit_notifiers =
207 NOTIFIER_LIST_INITIALIZER(exit_notifiers);
209 static NotifierList machine_init_done_notifiers =
210 NOTIFIER_LIST_INITIALIZER(machine_init_done_notifiers);
212 static bool tcg_allowed = true;
213 bool xen_allowed;
214 uint32_t xen_domid;
215 enum xen_mode xen_mode = XEN_EMULATE;
216 static int tcg_tb_size;
218 static int has_defaults = 1;
219 static int default_serial = 1;
220 static int default_parallel = 1;
221 static int default_virtcon = 1;
222 static int default_sclp = 1;
223 static int default_monitor = 1;
224 static int default_floppy = 1;
225 static int default_cdrom = 1;
226 static int default_sdcard = 1;
227 static int default_vga = 1;
229 static struct {
230 const char *driver;
231 int *flag;
232 } default_list[] = {
233 { .driver = "isa-serial", .flag = &default_serial },
234 { .driver = "isa-parallel", .flag = &default_parallel },
235 { .driver = "isa-fdc", .flag = &default_floppy },
236 { .driver = "ide-cd", .flag = &default_cdrom },
237 { .driver = "ide-hd", .flag = &default_cdrom },
238 { .driver = "ide-drive", .flag = &default_cdrom },
239 { .driver = "scsi-cd", .flag = &default_cdrom },
240 { .driver = "virtio-serial-pci", .flag = &default_virtcon },
241 { .driver = "virtio-serial-s390", .flag = &default_virtcon },
242 { .driver = "virtio-serial", .flag = &default_virtcon },
243 { .driver = "VGA", .flag = &default_vga },
244 { .driver = "isa-vga", .flag = &default_vga },
245 { .driver = "cirrus-vga", .flag = &default_vga },
246 { .driver = "isa-cirrus-vga", .flag = &default_vga },
247 { .driver = "vmware-svga", .flag = &default_vga },
248 { .driver = "qxl-vga", .flag = &default_vga },
251 static QemuOptsList qemu_rtc_opts = {
252 .name = "rtc",
253 .head = QTAILQ_HEAD_INITIALIZER(qemu_rtc_opts.head),
254 .desc = {
256 .name = "base",
257 .type = QEMU_OPT_STRING,
259 .name = "clock",
260 .type = QEMU_OPT_STRING,
262 .name = "driftfix",
263 .type = QEMU_OPT_STRING,
265 { /* end of list */ }
269 static QemuOptsList qemu_sandbox_opts = {
270 .name = "sandbox",
271 .implied_opt_name = "enable",
272 .head = QTAILQ_HEAD_INITIALIZER(qemu_sandbox_opts.head),
273 .desc = {
275 .name = "enable",
276 .type = QEMU_OPT_BOOL,
278 { /* end of list */ }
282 static QemuOptsList qemu_trace_opts = {
283 .name = "trace",
284 .implied_opt_name = "trace",
285 .head = QTAILQ_HEAD_INITIALIZER(qemu_trace_opts.head),
286 .desc = {
288 .name = "events",
289 .type = QEMU_OPT_STRING,
291 .name = "file",
292 .type = QEMU_OPT_STRING,
294 { /* end of list */ }
298 static QemuOptsList qemu_option_rom_opts = {
299 .name = "option-rom",
300 .implied_opt_name = "romfile",
301 .head = QTAILQ_HEAD_INITIALIZER(qemu_option_rom_opts.head),
302 .desc = {
304 .name = "bootindex",
305 .type = QEMU_OPT_NUMBER,
306 }, {
307 .name = "romfile",
308 .type = QEMU_OPT_STRING,
310 { /* end of list */ }
314 static QemuOptsList qemu_machine_opts = {
315 .name = "machine",
316 .implied_opt_name = "type",
317 .merge_lists = true,
318 .head = QTAILQ_HEAD_INITIALIZER(qemu_machine_opts.head),
319 .desc = {
321 .name = "type",
322 .type = QEMU_OPT_STRING,
323 .help = "emulated machine"
324 }, {
325 .name = "accel",
326 .type = QEMU_OPT_STRING,
327 .help = "accelerator list",
328 }, {
329 .name = "kernel_irqchip",
330 .type = QEMU_OPT_BOOL,
331 .help = "use KVM in-kernel irqchip",
332 }, {
333 .name = "kvm_shadow_mem",
334 .type = QEMU_OPT_SIZE,
335 .help = "KVM shadow MMU size",
336 }, {
337 .name = "kernel",
338 .type = QEMU_OPT_STRING,
339 .help = "Linux kernel image file",
340 }, {
341 .name = "initrd",
342 .type = QEMU_OPT_STRING,
343 .help = "Linux initial ramdisk file",
344 }, {
345 .name = "append",
346 .type = QEMU_OPT_STRING,
347 .help = "Linux kernel command line",
348 }, {
349 .name = "dtb",
350 .type = QEMU_OPT_STRING,
351 .help = "Linux kernel device tree file",
352 }, {
353 .name = "dumpdtb",
354 .type = QEMU_OPT_STRING,
355 .help = "Dump current dtb to a file and quit",
356 }, {
357 .name = "phandle_start",
358 .type = QEMU_OPT_NUMBER,
359 .help = "The first phandle ID we may generate dynamically",
360 }, {
361 .name = "dt_compatible",
362 .type = QEMU_OPT_STRING,
363 .help = "Overrides the \"compatible\" property of the dt root node",
364 }, {
365 .name = "dump-guest-core",
366 .type = QEMU_OPT_BOOL,
367 .help = "Include guest memory in a core dump",
368 }, {
369 .name = "mem-merge",
370 .type = QEMU_OPT_BOOL,
371 .help = "enable/disable memory merge support",
373 .name = "usb",
374 .type = QEMU_OPT_BOOL,
375 .help = "Set on/off to enable/disable usb",
377 .name = "firmware",
378 .type = QEMU_OPT_STRING,
379 .help = "firmware image",
381 .name = "kvm-type",
382 .type = QEMU_OPT_STRING,
383 .help = "Specifies the KVM virtualization mode (HV, PR)",
385 .name = PC_MACHINE_MAX_RAM_BELOW_4G,
386 .type = QEMU_OPT_SIZE,
387 .help = "maximum ram below the 4G boundary (32bit boundary)",
389 { /* End of list */ }
393 static QemuOptsList qemu_boot_opts = {
394 .name = "boot-opts",
395 .implied_opt_name = "order",
396 .merge_lists = true,
397 .head = QTAILQ_HEAD_INITIALIZER(qemu_boot_opts.head),
398 .desc = {
400 .name = "order",
401 .type = QEMU_OPT_STRING,
402 }, {
403 .name = "once",
404 .type = QEMU_OPT_STRING,
405 }, {
406 .name = "menu",
407 .type = QEMU_OPT_BOOL,
408 }, {
409 .name = "splash",
410 .type = QEMU_OPT_STRING,
411 }, {
412 .name = "splash-time",
413 .type = QEMU_OPT_STRING,
414 }, {
415 .name = "reboot-timeout",
416 .type = QEMU_OPT_STRING,
417 }, {
418 .name = "strict",
419 .type = QEMU_OPT_BOOL,
421 { /*End of list */ }
425 static QemuOptsList qemu_add_fd_opts = {
426 .name = "add-fd",
427 .head = QTAILQ_HEAD_INITIALIZER(qemu_add_fd_opts.head),
428 .desc = {
430 .name = "fd",
431 .type = QEMU_OPT_NUMBER,
432 .help = "file descriptor of which a duplicate is added to fd set",
434 .name = "set",
435 .type = QEMU_OPT_NUMBER,
436 .help = "ID of the fd set to add fd to",
438 .name = "opaque",
439 .type = QEMU_OPT_STRING,
440 .help = "free-form string used to describe fd",
442 { /* end of list */ }
446 static QemuOptsList qemu_object_opts = {
447 .name = "object",
448 .implied_opt_name = "qom-type",
449 .head = QTAILQ_HEAD_INITIALIZER(qemu_object_opts.head),
450 .desc = {
455 static QemuOptsList qemu_tpmdev_opts = {
456 .name = "tpmdev",
457 .implied_opt_name = "type",
458 .head = QTAILQ_HEAD_INITIALIZER(qemu_tpmdev_opts.head),
459 .desc = {
460 /* options are defined in the TPM backends */
461 { /* end of list */ }
465 static QemuOptsList qemu_realtime_opts = {
466 .name = "realtime",
467 .head = QTAILQ_HEAD_INITIALIZER(qemu_realtime_opts.head),
468 .desc = {
470 .name = "mlock",
471 .type = QEMU_OPT_BOOL,
473 { /* end of list */ }
477 static QemuOptsList qemu_msg_opts = {
478 .name = "msg",
479 .head = QTAILQ_HEAD_INITIALIZER(qemu_msg_opts.head),
480 .desc = {
482 .name = "timestamp",
483 .type = QEMU_OPT_BOOL,
485 { /* end of list */ }
489 static QemuOptsList qemu_name_opts = {
490 .name = "name",
491 .implied_opt_name = "guest",
492 .merge_lists = true,
493 .head = QTAILQ_HEAD_INITIALIZER(qemu_name_opts.head),
494 .desc = {
496 .name = "guest",
497 .type = QEMU_OPT_STRING,
498 .help = "Sets the name of the guest.\n"
499 "This name will be displayed in the SDL window caption.\n"
500 "The name will also be used for the VNC server",
501 }, {
502 .name = "process",
503 .type = QEMU_OPT_STRING,
504 .help = "Sets the name of the QEMU process, as shown in top etc",
505 }, {
506 .name = "debug-threads",
507 .type = QEMU_OPT_BOOL,
508 .help = "When enabled, name the individual threads; defaults off.\n"
509 "NOTE: The thread names are for debugging and not a\n"
510 "stable API.",
512 { /* End of list */ }
516 static QemuOptsList qemu_mem_opts = {
517 .name = "memory",
518 .implied_opt_name = "size",
519 .head = QTAILQ_HEAD_INITIALIZER(qemu_mem_opts.head),
520 .merge_lists = true,
521 .desc = {
523 .name = "size",
524 .type = QEMU_OPT_SIZE,
527 .name = "slots",
528 .type = QEMU_OPT_NUMBER,
531 .name = "maxmem",
532 .type = QEMU_OPT_SIZE,
534 { /* end of list */ }
539 * Get machine options
541 * Returns: machine options (never null).
543 QemuOpts *qemu_get_machine_opts(void)
545 return qemu_find_opts_singleton("machine");
548 const char *qemu_get_vm_name(void)
550 return qemu_name;
553 static void res_free(void)
555 if (boot_splash_filedata != NULL) {
556 g_free(boot_splash_filedata);
557 boot_splash_filedata = NULL;
561 static int default_driver_check(QemuOpts *opts, void *opaque)
563 const char *driver = qemu_opt_get(opts, "driver");
564 int i;
566 if (!driver)
567 return 0;
568 for (i = 0; i < ARRAY_SIZE(default_list); i++) {
569 if (strcmp(default_list[i].driver, driver) != 0)
570 continue;
571 *(default_list[i].flag) = 0;
573 return 0;
576 /***********************************************************/
577 /* QEMU state */
579 static RunState current_run_state = RUN_STATE_PRELAUNCH;
581 /* We use RUN_STATE_MAX but any invalid value will do */
582 static RunState vmstop_requested = RUN_STATE_MAX;
583 static QemuMutex vmstop_lock;
585 typedef struct {
586 RunState from;
587 RunState to;
588 } RunStateTransition;
590 static const RunStateTransition runstate_transitions_def[] = {
591 /* from -> to */
592 { RUN_STATE_DEBUG, RUN_STATE_RUNNING },
593 { RUN_STATE_DEBUG, RUN_STATE_FINISH_MIGRATE },
595 { RUN_STATE_INMIGRATE, RUN_STATE_RUNNING },
596 { RUN_STATE_INMIGRATE, RUN_STATE_PAUSED },
598 { RUN_STATE_INTERNAL_ERROR, RUN_STATE_PAUSED },
599 { RUN_STATE_INTERNAL_ERROR, RUN_STATE_FINISH_MIGRATE },
601 { RUN_STATE_IO_ERROR, RUN_STATE_RUNNING },
602 { RUN_STATE_IO_ERROR, RUN_STATE_FINISH_MIGRATE },
604 { RUN_STATE_PAUSED, RUN_STATE_RUNNING },
605 { RUN_STATE_PAUSED, RUN_STATE_FINISH_MIGRATE },
607 { RUN_STATE_POSTMIGRATE, RUN_STATE_RUNNING },
608 { RUN_STATE_POSTMIGRATE, RUN_STATE_FINISH_MIGRATE },
610 { RUN_STATE_PRELAUNCH, RUN_STATE_RUNNING },
611 { RUN_STATE_PRELAUNCH, RUN_STATE_FINISH_MIGRATE },
612 { RUN_STATE_PRELAUNCH, RUN_STATE_INMIGRATE },
614 { RUN_STATE_FINISH_MIGRATE, RUN_STATE_RUNNING },
615 { RUN_STATE_FINISH_MIGRATE, RUN_STATE_POSTMIGRATE },
617 { RUN_STATE_RESTORE_VM, RUN_STATE_RUNNING },
619 { RUN_STATE_RUNNING, RUN_STATE_DEBUG },
620 { RUN_STATE_RUNNING, RUN_STATE_INTERNAL_ERROR },
621 { RUN_STATE_RUNNING, RUN_STATE_IO_ERROR },
622 { RUN_STATE_RUNNING, RUN_STATE_PAUSED },
623 { RUN_STATE_RUNNING, RUN_STATE_FINISH_MIGRATE },
624 { RUN_STATE_RUNNING, RUN_STATE_RESTORE_VM },
625 { RUN_STATE_RUNNING, RUN_STATE_SAVE_VM },
626 { RUN_STATE_RUNNING, RUN_STATE_SHUTDOWN },
627 { RUN_STATE_RUNNING, RUN_STATE_WATCHDOG },
628 { RUN_STATE_RUNNING, RUN_STATE_GUEST_PANICKED },
630 { RUN_STATE_SAVE_VM, RUN_STATE_RUNNING },
632 { RUN_STATE_SHUTDOWN, RUN_STATE_PAUSED },
633 { RUN_STATE_SHUTDOWN, RUN_STATE_FINISH_MIGRATE },
635 { RUN_STATE_DEBUG, RUN_STATE_SUSPENDED },
636 { RUN_STATE_RUNNING, RUN_STATE_SUSPENDED },
637 { RUN_STATE_SUSPENDED, RUN_STATE_RUNNING },
638 { RUN_STATE_SUSPENDED, RUN_STATE_FINISH_MIGRATE },
640 { RUN_STATE_WATCHDOG, RUN_STATE_RUNNING },
641 { RUN_STATE_WATCHDOG, RUN_STATE_FINISH_MIGRATE },
643 { RUN_STATE_GUEST_PANICKED, RUN_STATE_RUNNING },
644 { RUN_STATE_GUEST_PANICKED, RUN_STATE_FINISH_MIGRATE },
646 { RUN_STATE_MAX, RUN_STATE_MAX },
649 static bool runstate_valid_transitions[RUN_STATE_MAX][RUN_STATE_MAX];
651 bool runstate_check(RunState state)
653 return current_run_state == state;
656 static void runstate_init(void)
658 const RunStateTransition *p;
660 memset(&runstate_valid_transitions, 0, sizeof(runstate_valid_transitions));
661 for (p = &runstate_transitions_def[0]; p->from != RUN_STATE_MAX; p++) {
662 runstate_valid_transitions[p->from][p->to] = true;
665 qemu_mutex_init(&vmstop_lock);
668 /* This function will abort() on invalid state transitions */
669 void runstate_set(RunState new_state)
671 assert(new_state < RUN_STATE_MAX);
673 if (!runstate_valid_transitions[current_run_state][new_state]) {
674 fprintf(stderr, "ERROR: invalid runstate transition: '%s' -> '%s'\n",
675 RunState_lookup[current_run_state],
676 RunState_lookup[new_state]);
677 abort();
679 trace_runstate_set(new_state);
680 current_run_state = new_state;
683 int runstate_is_running(void)
685 return runstate_check(RUN_STATE_RUNNING);
688 bool runstate_needs_reset(void)
690 return runstate_check(RUN_STATE_INTERNAL_ERROR) ||
691 runstate_check(RUN_STATE_SHUTDOWN);
694 StatusInfo *qmp_query_status(Error **errp)
696 StatusInfo *info = g_malloc0(sizeof(*info));
698 info->running = runstate_is_running();
699 info->singlestep = singlestep;
700 info->status = current_run_state;
702 return info;
705 static bool qemu_vmstop_requested(RunState *r)
707 qemu_mutex_lock(&vmstop_lock);
708 *r = vmstop_requested;
709 vmstop_requested = RUN_STATE_MAX;
710 qemu_mutex_unlock(&vmstop_lock);
711 return *r < RUN_STATE_MAX;
714 void qemu_system_vmstop_request_prepare(void)
716 qemu_mutex_lock(&vmstop_lock);
719 void qemu_system_vmstop_request(RunState state)
721 vmstop_requested = state;
722 qemu_mutex_unlock(&vmstop_lock);
723 qemu_notify_event();
726 void vm_start(void)
728 RunState requested;
730 qemu_vmstop_requested(&requested);
731 if (runstate_is_running() && requested == RUN_STATE_MAX) {
732 return;
735 /* Ensure that a STOP/RESUME pair of events is emitted if a
736 * vmstop request was pending. The BLOCK_IO_ERROR event, for
737 * example, according to documentation is always followed by
738 * the STOP event.
740 if (runstate_is_running()) {
741 monitor_protocol_event(QEVENT_STOP, NULL);
742 } else {
743 cpu_enable_ticks();
744 runstate_set(RUN_STATE_RUNNING);
745 vm_state_notify(1, RUN_STATE_RUNNING);
746 resume_all_vcpus();
749 monitor_protocol_event(QEVENT_RESUME, NULL);
753 /***********************************************************/
754 /* real time host monotonic timer */
756 /***********************************************************/
757 /* host time/date access */
758 void qemu_get_timedate(struct tm *tm, int offset)
760 time_t ti;
762 time(&ti);
763 ti += offset;
764 if (rtc_date_offset == -1) {
765 if (rtc_utc)
766 gmtime_r(&ti, tm);
767 else
768 localtime_r(&ti, tm);
769 } else {
770 ti -= rtc_date_offset;
771 gmtime_r(&ti, tm);
775 int qemu_timedate_diff(struct tm *tm)
777 time_t seconds;
779 if (rtc_date_offset == -1)
780 if (rtc_utc)
781 seconds = mktimegm(tm);
782 else {
783 struct tm tmp = *tm;
784 tmp.tm_isdst = -1; /* use timezone to figure it out */
785 seconds = mktime(&tmp);
787 else
788 seconds = mktimegm(tm) + rtc_date_offset;
790 return seconds - time(NULL);
793 void rtc_change_mon_event(struct tm *tm)
795 QObject *data;
797 data = qobject_from_jsonf("{ 'offset': %d }", qemu_timedate_diff(tm));
798 monitor_protocol_event(QEVENT_RTC_CHANGE, data);
799 qobject_decref(data);
802 static void configure_rtc_date_offset(const char *startdate, int legacy)
804 time_t rtc_start_date;
805 struct tm tm;
807 if (!strcmp(startdate, "now") && legacy) {
808 rtc_date_offset = -1;
809 } else {
810 if (sscanf(startdate, "%d-%d-%dT%d:%d:%d",
811 &tm.tm_year,
812 &tm.tm_mon,
813 &tm.tm_mday,
814 &tm.tm_hour,
815 &tm.tm_min,
816 &tm.tm_sec) == 6) {
817 /* OK */
818 } else if (sscanf(startdate, "%d-%d-%d",
819 &tm.tm_year,
820 &tm.tm_mon,
821 &tm.tm_mday) == 3) {
822 tm.tm_hour = 0;
823 tm.tm_min = 0;
824 tm.tm_sec = 0;
825 } else {
826 goto date_fail;
828 tm.tm_year -= 1900;
829 tm.tm_mon--;
830 rtc_start_date = mktimegm(&tm);
831 if (rtc_start_date == -1) {
832 date_fail:
833 fprintf(stderr, "Invalid date format. Valid formats are:\n"
834 "'2006-06-17T16:01:21' or '2006-06-17'\n");
835 exit(1);
837 rtc_date_offset = time(NULL) - rtc_start_date;
841 static void configure_rtc(QemuOpts *opts)
843 const char *value;
845 value = qemu_opt_get(opts, "base");
846 if (value) {
847 if (!strcmp(value, "utc")) {
848 rtc_utc = 1;
849 } else if (!strcmp(value, "localtime")) {
850 rtc_utc = 0;
851 } else {
852 configure_rtc_date_offset(value, 0);
855 value = qemu_opt_get(opts, "clock");
856 if (value) {
857 if (!strcmp(value, "host")) {
858 rtc_clock = QEMU_CLOCK_HOST;
859 } else if (!strcmp(value, "rt")) {
860 rtc_clock = QEMU_CLOCK_REALTIME;
861 } else if (!strcmp(value, "vm")) {
862 rtc_clock = QEMU_CLOCK_VIRTUAL;
863 } else {
864 fprintf(stderr, "qemu: invalid option value '%s'\n", value);
865 exit(1);
868 value = qemu_opt_get(opts, "driftfix");
869 if (value) {
870 if (!strcmp(value, "slew")) {
871 static GlobalProperty slew_lost_ticks[] = {
873 .driver = "mc146818rtc",
874 .property = "lost_tick_policy",
875 .value = "slew",
877 { /* end of list */ }
880 qdev_prop_register_global_list(slew_lost_ticks);
881 } else if (!strcmp(value, "none")) {
882 /* discard is default */
883 } else {
884 fprintf(stderr, "qemu: invalid option value '%s'\n", value);
885 exit(1);
890 /***********************************************************/
891 /* Bluetooth support */
892 static int nb_hcis;
893 static int cur_hci;
894 static struct HCIInfo *hci_table[MAX_NICS];
896 struct HCIInfo *qemu_next_hci(void)
898 if (cur_hci == nb_hcis)
899 return &null_hci;
901 return hci_table[cur_hci++];
904 static int bt_hci_parse(const char *str)
906 struct HCIInfo *hci;
907 bdaddr_t bdaddr;
909 if (nb_hcis >= MAX_NICS) {
910 fprintf(stderr, "qemu: Too many bluetooth HCIs (max %i).\n", MAX_NICS);
911 return -1;
914 hci = hci_init(str);
915 if (!hci)
916 return -1;
918 bdaddr.b[0] = 0x52;
919 bdaddr.b[1] = 0x54;
920 bdaddr.b[2] = 0x00;
921 bdaddr.b[3] = 0x12;
922 bdaddr.b[4] = 0x34;
923 bdaddr.b[5] = 0x56 + nb_hcis;
924 hci->bdaddr_set(hci, bdaddr.b);
926 hci_table[nb_hcis++] = hci;
928 return 0;
931 static void bt_vhci_add(int vlan_id)
933 struct bt_scatternet_s *vlan = qemu_find_bt_vlan(vlan_id);
935 if (!vlan->slave)
936 fprintf(stderr, "qemu: warning: adding a VHCI to "
937 "an empty scatternet %i\n", vlan_id);
939 bt_vhci_init(bt_new_hci(vlan));
942 static struct bt_device_s *bt_device_add(const char *opt)
944 struct bt_scatternet_s *vlan;
945 int vlan_id = 0;
946 char *endp = strstr(opt, ",vlan=");
947 int len = (endp ? endp - opt : strlen(opt)) + 1;
948 char devname[10];
950 pstrcpy(devname, MIN(sizeof(devname), len), opt);
952 if (endp) {
953 vlan_id = strtol(endp + 6, &endp, 0);
954 if (*endp) {
955 fprintf(stderr, "qemu: unrecognised bluetooth vlan Id\n");
956 return 0;
960 vlan = qemu_find_bt_vlan(vlan_id);
962 if (!vlan->slave)
963 fprintf(stderr, "qemu: warning: adding a slave device to "
964 "an empty scatternet %i\n", vlan_id);
966 if (!strcmp(devname, "keyboard"))
967 return bt_keyboard_init(vlan);
969 fprintf(stderr, "qemu: unsupported bluetooth device `%s'\n", devname);
970 return 0;
973 static int bt_parse(const char *opt)
975 const char *endp, *p;
976 int vlan;
978 if (strstart(opt, "hci", &endp)) {
979 if (!*endp || *endp == ',') {
980 if (*endp)
981 if (!strstart(endp, ",vlan=", 0))
982 opt = endp + 1;
984 return bt_hci_parse(opt);
986 } else if (strstart(opt, "vhci", &endp)) {
987 if (!*endp || *endp == ',') {
988 if (*endp) {
989 if (strstart(endp, ",vlan=", &p)) {
990 vlan = strtol(p, (char **) &endp, 0);
991 if (*endp) {
992 fprintf(stderr, "qemu: bad scatternet '%s'\n", p);
993 return 1;
995 } else {
996 fprintf(stderr, "qemu: bad parameter '%s'\n", endp + 1);
997 return 1;
999 } else
1000 vlan = 0;
1002 bt_vhci_add(vlan);
1003 return 0;
1005 } else if (strstart(opt, "device:", &endp))
1006 return !bt_device_add(endp);
1008 fprintf(stderr, "qemu: bad bluetooth parameter '%s'\n", opt);
1009 return 1;
1012 static int parse_sandbox(QemuOpts *opts, void *opaque)
1014 /* FIXME: change this to true for 1.3 */
1015 if (qemu_opt_get_bool(opts, "enable", false)) {
1016 #ifdef CONFIG_SECCOMP
1017 if (seccomp_start() < 0) {
1018 qerror_report(ERROR_CLASS_GENERIC_ERROR,
1019 "failed to install seccomp syscall filter in the kernel");
1020 return -1;
1022 #else
1023 qerror_report(ERROR_CLASS_GENERIC_ERROR,
1024 "sandboxing request but seccomp is not compiled into this build");
1025 return -1;
1026 #endif
1029 return 0;
1032 static int parse_name(QemuOpts *opts, void *opaque)
1034 const char *proc_name;
1036 if (qemu_opt_get(opts, "debug-threads")) {
1037 qemu_thread_naming(qemu_opt_get_bool(opts, "debug-threads", false));
1039 qemu_name = qemu_opt_get(opts, "guest");
1041 proc_name = qemu_opt_get(opts, "process");
1042 if (proc_name) {
1043 os_set_proc_name(proc_name);
1046 return 0;
1049 bool usb_enabled(bool default_usb)
1051 return qemu_opt_get_bool(qemu_get_machine_opts(), "usb",
1052 has_defaults && default_usb);
1055 #ifndef _WIN32
1056 static int parse_add_fd(QemuOpts *opts, void *opaque)
1058 int fd, dupfd, flags;
1059 int64_t fdset_id;
1060 const char *fd_opaque = NULL;
1062 fd = qemu_opt_get_number(opts, "fd", -1);
1063 fdset_id = qemu_opt_get_number(opts, "set", -1);
1064 fd_opaque = qemu_opt_get(opts, "opaque");
1066 if (fd < 0) {
1067 qerror_report(ERROR_CLASS_GENERIC_ERROR,
1068 "fd option is required and must be non-negative");
1069 return -1;
1072 if (fd <= STDERR_FILENO) {
1073 qerror_report(ERROR_CLASS_GENERIC_ERROR,
1074 "fd cannot be a standard I/O stream");
1075 return -1;
1079 * All fds inherited across exec() necessarily have FD_CLOEXEC
1080 * clear, while qemu sets FD_CLOEXEC on all other fds used internally.
1082 flags = fcntl(fd, F_GETFD);
1083 if (flags == -1 || (flags & FD_CLOEXEC)) {
1084 qerror_report(ERROR_CLASS_GENERIC_ERROR,
1085 "fd is not valid or already in use");
1086 return -1;
1089 if (fdset_id < 0) {
1090 qerror_report(ERROR_CLASS_GENERIC_ERROR,
1091 "set option is required and must be non-negative");
1092 return -1;
1095 #ifdef F_DUPFD_CLOEXEC
1096 dupfd = fcntl(fd, F_DUPFD_CLOEXEC, 0);
1097 #else
1098 dupfd = dup(fd);
1099 if (dupfd != -1) {
1100 qemu_set_cloexec(dupfd);
1102 #endif
1103 if (dupfd == -1) {
1104 qerror_report(ERROR_CLASS_GENERIC_ERROR,
1105 "Error duplicating fd: %s", strerror(errno));
1106 return -1;
1109 /* add the duplicate fd, and optionally the opaque string, to the fd set */
1110 monitor_fdset_add_fd(dupfd, true, fdset_id, fd_opaque ? true : false,
1111 fd_opaque, NULL);
1113 return 0;
1116 static int cleanup_add_fd(QemuOpts *opts, void *opaque)
1118 int fd;
1120 fd = qemu_opt_get_number(opts, "fd", -1);
1121 close(fd);
1123 return 0;
1125 #endif
1127 /***********************************************************/
1128 /* QEMU Block devices */
1130 #define HD_OPTS "media=disk"
1131 #define CDROM_OPTS "media=cdrom"
1132 #define FD_OPTS ""
1133 #define PFLASH_OPTS ""
1134 #define MTD_OPTS ""
1135 #define SD_OPTS ""
1137 static int drive_init_func(QemuOpts *opts, void *opaque)
1139 BlockInterfaceType *block_default_type = opaque;
1141 return drive_new(opts, *block_default_type) == NULL;
1144 static int drive_enable_snapshot(QemuOpts *opts, void *opaque)
1146 if (NULL == qemu_opt_get(opts, "snapshot")) {
1147 qemu_opt_set(opts, "snapshot", "on");
1149 return 0;
1152 static void default_drive(int enable, int snapshot, BlockInterfaceType type,
1153 int index, const char *optstr)
1155 QemuOpts *opts;
1157 if (!enable || drive_get_by_index(type, index)) {
1158 return;
1161 opts = drive_add(type, index, NULL, optstr);
1162 if (snapshot) {
1163 drive_enable_snapshot(opts, NULL);
1165 if (!drive_new(opts, type)) {
1166 exit(1);
1170 void qemu_register_boot_set(QEMUBootSetHandler *func, void *opaque)
1172 boot_set_handler = func;
1173 boot_set_opaque = opaque;
1176 int qemu_boot_set(const char *boot_order)
1178 if (!boot_set_handler) {
1179 return -EINVAL;
1181 return boot_set_handler(boot_set_opaque, boot_order);
1184 static void validate_bootdevices(const char *devices)
1186 /* We just do some generic consistency checks */
1187 const char *p;
1188 int bitmap = 0;
1190 for (p = devices; *p != '\0'; p++) {
1191 /* Allowed boot devices are:
1192 * a-b: floppy disk drives
1193 * c-f: IDE disk drives
1194 * g-m: machine implementation dependent drives
1195 * n-p: network devices
1196 * It's up to each machine implementation to check if the given boot
1197 * devices match the actual hardware implementation and firmware
1198 * features.
1200 if (*p < 'a' || *p > 'p') {
1201 fprintf(stderr, "Invalid boot device '%c'\n", *p);
1202 exit(1);
1204 if (bitmap & (1 << (*p - 'a'))) {
1205 fprintf(stderr, "Boot device '%c' was given twice\n", *p);
1206 exit(1);
1208 bitmap |= 1 << (*p - 'a');
1212 static void restore_boot_order(void *opaque)
1214 char *normal_boot_order = opaque;
1215 static int first = 1;
1217 /* Restore boot order and remove ourselves after the first boot */
1218 if (first) {
1219 first = 0;
1220 return;
1223 qemu_boot_set(normal_boot_order);
1225 qemu_unregister_reset(restore_boot_order, normal_boot_order);
1226 g_free(normal_boot_order);
1229 void add_boot_device_path(int32_t bootindex, DeviceState *dev,
1230 const char *suffix)
1232 FWBootEntry *node, *i;
1234 if (bootindex < 0) {
1235 return;
1238 assert(dev != NULL || suffix != NULL);
1240 node = g_malloc0(sizeof(FWBootEntry));
1241 node->bootindex = bootindex;
1242 node->suffix = g_strdup(suffix);
1243 node->dev = dev;
1245 QTAILQ_FOREACH(i, &fw_boot_order, link) {
1246 if (i->bootindex == bootindex) {
1247 fprintf(stderr, "Two devices with same boot index %d\n", bootindex);
1248 exit(1);
1249 } else if (i->bootindex < bootindex) {
1250 continue;
1252 QTAILQ_INSERT_BEFORE(i, node, link);
1253 return;
1255 QTAILQ_INSERT_TAIL(&fw_boot_order, node, link);
1258 DeviceState *get_boot_device(uint32_t position)
1260 uint32_t counter = 0;
1261 FWBootEntry *i = NULL;
1262 DeviceState *res = NULL;
1264 if (!QTAILQ_EMPTY(&fw_boot_order)) {
1265 QTAILQ_FOREACH(i, &fw_boot_order, link) {
1266 if (counter == position) {
1267 res = i->dev;
1268 break;
1270 counter++;
1273 return res;
1277 * This function returns null terminated string that consist of new line
1278 * separated device paths.
1280 * memory pointed by "size" is assigned total length of the array in bytes
1283 char *get_boot_devices_list(size_t *size, bool ignore_suffixes)
1285 FWBootEntry *i;
1286 size_t total = 0;
1287 char *list = NULL;
1289 QTAILQ_FOREACH(i, &fw_boot_order, link) {
1290 char *devpath = NULL, *bootpath;
1291 size_t len;
1293 if (i->dev) {
1294 devpath = qdev_get_fw_dev_path(i->dev);
1295 assert(devpath);
1298 if (i->suffix && !ignore_suffixes && devpath) {
1299 size_t bootpathlen = strlen(devpath) + strlen(i->suffix) + 1;
1301 bootpath = g_malloc(bootpathlen);
1302 snprintf(bootpath, bootpathlen, "%s%s", devpath, i->suffix);
1303 g_free(devpath);
1304 } else if (devpath) {
1305 bootpath = devpath;
1306 } else if (!ignore_suffixes) {
1307 assert(i->suffix);
1308 bootpath = g_strdup(i->suffix);
1309 } else {
1310 bootpath = g_strdup("");
1313 if (total) {
1314 list[total-1] = '\n';
1316 len = strlen(bootpath) + 1;
1317 list = g_realloc(list, total + len);
1318 memcpy(&list[total], bootpath, len);
1319 total += len;
1320 g_free(bootpath);
1323 *size = total;
1325 if (boot_strict && *size > 0) {
1326 list[total-1] = '\n';
1327 list = g_realloc(list, total + 5);
1328 memcpy(&list[total], "HALT", 5);
1329 *size = total + 5;
1331 return list;
1334 static QemuOptsList qemu_smp_opts = {
1335 .name = "smp-opts",
1336 .implied_opt_name = "cpus",
1337 .merge_lists = true,
1338 .head = QTAILQ_HEAD_INITIALIZER(qemu_smp_opts.head),
1339 .desc = {
1341 .name = "cpus",
1342 .type = QEMU_OPT_NUMBER,
1343 }, {
1344 .name = "sockets",
1345 .type = QEMU_OPT_NUMBER,
1346 }, {
1347 .name = "cores",
1348 .type = QEMU_OPT_NUMBER,
1349 }, {
1350 .name = "threads",
1351 .type = QEMU_OPT_NUMBER,
1352 }, {
1353 .name = "maxcpus",
1354 .type = QEMU_OPT_NUMBER,
1356 { /*End of list */ }
1360 static void smp_parse(QemuOpts *opts)
1362 if (opts) {
1364 unsigned cpus = qemu_opt_get_number(opts, "cpus", 0);
1365 unsigned sockets = qemu_opt_get_number(opts, "sockets", 0);
1366 unsigned cores = qemu_opt_get_number(opts, "cores", 0);
1367 unsigned threads = qemu_opt_get_number(opts, "threads", 0);
1369 /* compute missing values, prefer sockets over cores over threads */
1370 if (cpus == 0 || sockets == 0) {
1371 sockets = sockets > 0 ? sockets : 1;
1372 cores = cores > 0 ? cores : 1;
1373 threads = threads > 0 ? threads : 1;
1374 if (cpus == 0) {
1375 cpus = cores * threads * sockets;
1377 } else {
1378 if (cores == 0) {
1379 threads = threads > 0 ? threads : 1;
1380 cores = cpus / (sockets * threads);
1381 } else {
1382 threads = cpus / (cores * sockets);
1386 max_cpus = qemu_opt_get_number(opts, "maxcpus", 0);
1388 smp_cpus = cpus;
1389 smp_cores = cores > 0 ? cores : 1;
1390 smp_threads = threads > 0 ? threads : 1;
1394 if (max_cpus == 0) {
1395 max_cpus = smp_cpus;
1398 if (max_cpus > MAX_CPUMASK_BITS) {
1399 fprintf(stderr, "Unsupported number of maxcpus\n");
1400 exit(1);
1402 if (max_cpus < smp_cpus) {
1403 fprintf(stderr, "maxcpus must be equal to or greater than smp\n");
1404 exit(1);
1409 static void configure_realtime(QemuOpts *opts)
1411 bool enable_mlock;
1413 enable_mlock = qemu_opt_get_bool(opts, "mlock", true);
1415 if (enable_mlock) {
1416 if (os_mlock() < 0) {
1417 fprintf(stderr, "qemu: locking memory failed\n");
1418 exit(1);
1424 static void configure_msg(QemuOpts *opts)
1426 enable_timestamp_msg = qemu_opt_get_bool(opts, "timestamp", true);
1429 /***********************************************************/
1430 /* USB devices */
1432 static int usb_device_add(const char *devname)
1434 USBDevice *dev = NULL;
1435 #ifndef CONFIG_LINUX
1436 const char *p;
1437 #endif
1439 if (!usb_enabled(false)) {
1440 return -1;
1443 /* drivers with .usbdevice_name entry in USBDeviceInfo */
1444 dev = usbdevice_create(devname);
1445 if (dev)
1446 goto done;
1448 /* the other ones */
1449 #ifndef CONFIG_LINUX
1450 /* only the linux version is qdev-ified, usb-bsd still needs this */
1451 if (strstart(devname, "host:", &p)) {
1452 dev = usb_host_device_open(usb_bus_find(-1), p);
1454 #endif
1455 if (!dev)
1456 return -1;
1458 done:
1459 return 0;
1462 static int usb_device_del(const char *devname)
1464 int bus_num, addr;
1465 const char *p;
1467 if (strstart(devname, "host:", &p)) {
1468 return -1;
1471 if (!usb_enabled(false)) {
1472 return -1;
1475 p = strchr(devname, '.');
1476 if (!p)
1477 return -1;
1478 bus_num = strtoul(devname, NULL, 0);
1479 addr = strtoul(p + 1, NULL, 0);
1481 return usb_device_delete_addr(bus_num, addr);
1484 static int usb_parse(const char *cmdline)
1486 int r;
1487 r = usb_device_add(cmdline);
1488 if (r < 0) {
1489 fprintf(stderr, "qemu: could not add USB device '%s'\n", cmdline);
1491 return r;
1494 void do_usb_add(Monitor *mon, const QDict *qdict)
1496 const char *devname = qdict_get_str(qdict, "devname");
1497 if (usb_device_add(devname) < 0) {
1498 error_report("could not add USB device '%s'", devname);
1502 void do_usb_del(Monitor *mon, const QDict *qdict)
1504 const char *devname = qdict_get_str(qdict, "devname");
1505 if (usb_device_del(devname) < 0) {
1506 error_report("could not delete USB device '%s'", devname);
1510 /***********************************************************/
1511 /* PCMCIA/Cardbus */
1513 static struct pcmcia_socket_entry_s {
1514 PCMCIASocket *socket;
1515 struct pcmcia_socket_entry_s *next;
1516 } *pcmcia_sockets = 0;
1518 void pcmcia_socket_register(PCMCIASocket *socket)
1520 struct pcmcia_socket_entry_s *entry;
1522 entry = g_malloc(sizeof(struct pcmcia_socket_entry_s));
1523 entry->socket = socket;
1524 entry->next = pcmcia_sockets;
1525 pcmcia_sockets = entry;
1528 void pcmcia_socket_unregister(PCMCIASocket *socket)
1530 struct pcmcia_socket_entry_s *entry, **ptr;
1532 ptr = &pcmcia_sockets;
1533 for (entry = *ptr; entry; ptr = &entry->next, entry = *ptr)
1534 if (entry->socket == socket) {
1535 *ptr = entry->next;
1536 g_free(entry);
1540 void pcmcia_info(Monitor *mon, const QDict *qdict)
1542 struct pcmcia_socket_entry_s *iter;
1544 if (!pcmcia_sockets)
1545 monitor_printf(mon, "No PCMCIA sockets\n");
1547 for (iter = pcmcia_sockets; iter; iter = iter->next)
1548 monitor_printf(mon, "%s: %s\n", iter->socket->slot_string,
1549 iter->socket->attached ? iter->socket->card_string :
1550 "Empty");
1553 /***********************************************************/
1554 /* machine registration */
1556 MachineState *current_machine;
1558 static void machine_class_init(ObjectClass *oc, void *data)
1560 MachineClass *mc = MACHINE_CLASS(oc);
1561 QEMUMachine *qm = data;
1563 mc->name = qm->name;
1564 mc->alias = qm->alias;
1565 mc->desc = qm->desc;
1566 mc->init = qm->init;
1567 mc->reset = qm->reset;
1568 mc->hot_add_cpu = qm->hot_add_cpu;
1569 mc->kvm_type = qm->kvm_type;
1570 mc->block_default_type = qm->block_default_type;
1571 mc->max_cpus = qm->max_cpus;
1572 mc->no_serial = qm->no_serial;
1573 mc->no_parallel = qm->no_parallel;
1574 mc->use_virtcon = qm->use_virtcon;
1575 mc->use_sclp = qm->use_sclp;
1576 mc->no_floppy = qm->no_floppy;
1577 mc->no_cdrom = qm->no_cdrom;
1578 mc->no_sdcard = qm->no_sdcard;
1579 mc->is_default = qm->is_default;
1580 mc->default_machine_opts = qm->default_machine_opts;
1581 mc->default_boot_order = qm->default_boot_order;
1582 mc->compat_props = qm->compat_props;
1583 mc->hw_version = qm->hw_version;
1586 int qemu_register_machine(QEMUMachine *m)
1588 char *name = g_strconcat(m->name, TYPE_MACHINE_SUFFIX, NULL);
1589 TypeInfo ti = {
1590 .name = name,
1591 .parent = TYPE_MACHINE,
1592 .class_init = machine_class_init,
1593 .class_data = (void *)m,
1596 type_register(&ti);
1597 g_free(name);
1599 return 0;
1602 static MachineClass *find_machine(const char *name)
1604 GSList *el, *machines = object_class_get_list(TYPE_MACHINE, false);
1605 MachineClass *mc = NULL;
1607 for (el = machines; el; el = el->next) {
1608 MachineClass *temp = el->data;
1610 if (!strcmp(temp->name, name)) {
1611 mc = temp;
1612 break;
1614 if (temp->alias &&
1615 !strcmp(temp->alias, name)) {
1616 mc = temp;
1617 break;
1621 g_slist_free(machines);
1622 return mc;
1625 MachineClass *find_default_machine(void)
1627 GSList *el, *machines = object_class_get_list(TYPE_MACHINE, false);
1628 MachineClass *mc = NULL;
1630 for (el = machines; el; el = el->next) {
1631 MachineClass *temp = el->data;
1633 if (temp->is_default) {
1634 mc = temp;
1635 break;
1639 g_slist_free(machines);
1640 return mc;
1643 MachineInfoList *qmp_query_machines(Error **errp)
1645 GSList *el, *machines = object_class_get_list(TYPE_MACHINE, false);
1646 MachineInfoList *mach_list = NULL;
1648 for (el = machines; el; el = el->next) {
1649 MachineClass *mc = el->data;
1650 MachineInfoList *entry;
1651 MachineInfo *info;
1653 info = g_malloc0(sizeof(*info));
1654 if (mc->is_default) {
1655 info->has_is_default = true;
1656 info->is_default = true;
1659 if (mc->alias) {
1660 info->has_alias = true;
1661 info->alias = g_strdup(mc->alias);
1664 info->name = g_strdup(mc->name);
1665 info->cpu_max = !mc->max_cpus ? 1 : mc->max_cpus;
1667 entry = g_malloc0(sizeof(*entry));
1668 entry->value = info;
1669 entry->next = mach_list;
1670 mach_list = entry;
1673 g_slist_free(machines);
1674 return mach_list;
1677 /***********************************************************/
1678 /* main execution loop */
1680 struct vm_change_state_entry {
1681 VMChangeStateHandler *cb;
1682 void *opaque;
1683 QLIST_ENTRY (vm_change_state_entry) entries;
1686 static QLIST_HEAD(vm_change_state_head, vm_change_state_entry) vm_change_state_head;
1688 VMChangeStateEntry *qemu_add_vm_change_state_handler(VMChangeStateHandler *cb,
1689 void *opaque)
1691 VMChangeStateEntry *e;
1693 e = g_malloc0(sizeof (*e));
1695 e->cb = cb;
1696 e->opaque = opaque;
1697 QLIST_INSERT_HEAD(&vm_change_state_head, e, entries);
1698 return e;
1701 void qemu_del_vm_change_state_handler(VMChangeStateEntry *e)
1703 QLIST_REMOVE (e, entries);
1704 g_free (e);
1707 void vm_state_notify(int running, RunState state)
1709 VMChangeStateEntry *e;
1711 trace_vm_state_notify(running, state);
1713 for (e = vm_change_state_head.lh_first; e; e = e->entries.le_next) {
1714 e->cb(e->opaque, running, state);
1718 /* reset/shutdown handler */
1720 typedef struct QEMUResetEntry {
1721 QTAILQ_ENTRY(QEMUResetEntry) entry;
1722 QEMUResetHandler *func;
1723 void *opaque;
1724 } QEMUResetEntry;
1726 static QTAILQ_HEAD(reset_handlers, QEMUResetEntry) reset_handlers =
1727 QTAILQ_HEAD_INITIALIZER(reset_handlers);
1728 static int reset_requested;
1729 static int shutdown_requested, shutdown_signal = -1;
1730 static pid_t shutdown_pid;
1731 static int powerdown_requested;
1732 static int debug_requested;
1733 static int suspend_requested;
1734 static WakeupReason wakeup_reason;
1735 static NotifierList powerdown_notifiers =
1736 NOTIFIER_LIST_INITIALIZER(powerdown_notifiers);
1737 static NotifierList suspend_notifiers =
1738 NOTIFIER_LIST_INITIALIZER(suspend_notifiers);
1739 static NotifierList wakeup_notifiers =
1740 NOTIFIER_LIST_INITIALIZER(wakeup_notifiers);
1741 static uint32_t wakeup_reason_mask = ~(1 << QEMU_WAKEUP_REASON_NONE);
1743 int qemu_shutdown_requested_get(void)
1745 return shutdown_requested;
1748 int qemu_reset_requested_get(void)
1750 return reset_requested;
1753 static int qemu_shutdown_requested(void)
1755 int r = shutdown_requested;
1756 shutdown_requested = 0;
1757 return r;
1760 static void qemu_kill_report(void)
1762 if (!qtest_driver() && shutdown_signal != -1) {
1763 fprintf(stderr, "qemu: terminating on signal %d", shutdown_signal);
1764 if (shutdown_pid == 0) {
1765 /* This happens for eg ^C at the terminal, so it's worth
1766 * avoiding printing an odd message in that case.
1768 fputc('\n', stderr);
1769 } else {
1770 fprintf(stderr, " from pid " FMT_pid "\n", shutdown_pid);
1772 shutdown_signal = -1;
1776 static int qemu_reset_requested(void)
1778 int r = reset_requested;
1779 reset_requested = 0;
1780 return r;
1783 static int qemu_suspend_requested(void)
1785 int r = suspend_requested;
1786 suspend_requested = 0;
1787 return r;
1790 static WakeupReason qemu_wakeup_requested(void)
1792 return wakeup_reason;
1795 static int qemu_powerdown_requested(void)
1797 int r = powerdown_requested;
1798 powerdown_requested = 0;
1799 return r;
1802 static int qemu_debug_requested(void)
1804 int r = debug_requested;
1805 debug_requested = 0;
1806 return r;
1809 void qemu_register_reset(QEMUResetHandler *func, void *opaque)
1811 QEMUResetEntry *re = g_malloc0(sizeof(QEMUResetEntry));
1813 re->func = func;
1814 re->opaque = opaque;
1815 QTAILQ_INSERT_TAIL(&reset_handlers, re, entry);
1818 void qemu_unregister_reset(QEMUResetHandler *func, void *opaque)
1820 QEMUResetEntry *re;
1822 QTAILQ_FOREACH(re, &reset_handlers, entry) {
1823 if (re->func == func && re->opaque == opaque) {
1824 QTAILQ_REMOVE(&reset_handlers, re, entry);
1825 g_free(re);
1826 return;
1831 void qemu_devices_reset(void)
1833 QEMUResetEntry *re, *nre;
1835 /* reset all devices */
1836 QTAILQ_FOREACH_SAFE(re, &reset_handlers, entry, nre) {
1837 re->func(re->opaque);
1841 void qemu_system_reset(bool report)
1843 MachineClass *mc;
1845 mc = current_machine ? MACHINE_GET_CLASS(current_machine) : NULL;
1847 if (mc && mc->reset) {
1848 mc->reset();
1849 } else {
1850 qemu_devices_reset();
1852 if (report) {
1853 monitor_protocol_event(QEVENT_RESET, NULL);
1855 cpu_synchronize_all_post_reset();
1858 void qemu_system_reset_request(void)
1860 if (no_reboot) {
1861 shutdown_requested = 1;
1862 } else {
1863 reset_requested = 1;
1865 cpu_stop_current();
1866 qemu_notify_event();
1869 static void qemu_system_suspend(void)
1871 pause_all_vcpus();
1872 notifier_list_notify(&suspend_notifiers, NULL);
1873 runstate_set(RUN_STATE_SUSPENDED);
1874 monitor_protocol_event(QEVENT_SUSPEND, NULL);
1877 void qemu_system_suspend_request(void)
1879 if (runstate_check(RUN_STATE_SUSPENDED)) {
1880 return;
1882 suspend_requested = 1;
1883 cpu_stop_current();
1884 qemu_notify_event();
1887 void qemu_register_suspend_notifier(Notifier *notifier)
1889 notifier_list_add(&suspend_notifiers, notifier);
1892 void qemu_system_wakeup_request(WakeupReason reason)
1894 trace_system_wakeup_request(reason);
1896 if (!runstate_check(RUN_STATE_SUSPENDED)) {
1897 return;
1899 if (!(wakeup_reason_mask & (1 << reason))) {
1900 return;
1902 runstate_set(RUN_STATE_RUNNING);
1903 wakeup_reason = reason;
1904 qemu_notify_event();
1907 void qemu_system_wakeup_enable(WakeupReason reason, bool enabled)
1909 if (enabled) {
1910 wakeup_reason_mask |= (1 << reason);
1911 } else {
1912 wakeup_reason_mask &= ~(1 << reason);
1916 void qemu_register_wakeup_notifier(Notifier *notifier)
1918 notifier_list_add(&wakeup_notifiers, notifier);
1921 void qemu_system_killed(int signal, pid_t pid)
1923 shutdown_signal = signal;
1924 shutdown_pid = pid;
1925 no_shutdown = 0;
1926 qemu_system_shutdown_request();
1929 void qemu_system_shutdown_request(void)
1931 shutdown_requested = 1;
1932 qemu_notify_event();
1935 static void qemu_system_powerdown(void)
1937 monitor_protocol_event(QEVENT_POWERDOWN, NULL);
1938 notifier_list_notify(&powerdown_notifiers, NULL);
1941 void qemu_system_powerdown_request(void)
1943 powerdown_requested = 1;
1944 qemu_notify_event();
1947 void qemu_register_powerdown_notifier(Notifier *notifier)
1949 notifier_list_add(&powerdown_notifiers, notifier);
1952 void qemu_system_debug_request(void)
1954 debug_requested = 1;
1955 qemu_notify_event();
1958 static bool main_loop_should_exit(void)
1960 RunState r;
1961 if (qemu_debug_requested()) {
1962 vm_stop(RUN_STATE_DEBUG);
1964 if (qemu_suspend_requested()) {
1965 qemu_system_suspend();
1967 if (qemu_shutdown_requested()) {
1968 qemu_kill_report();
1969 monitor_protocol_event(QEVENT_SHUTDOWN, NULL);
1970 if (no_shutdown) {
1971 vm_stop(RUN_STATE_SHUTDOWN);
1972 } else {
1973 return true;
1976 if (qemu_reset_requested()) {
1977 pause_all_vcpus();
1978 cpu_synchronize_all_states();
1979 qemu_system_reset(VMRESET_REPORT);
1980 resume_all_vcpus();
1981 if (runstate_needs_reset()) {
1982 runstate_set(RUN_STATE_PAUSED);
1985 if (qemu_wakeup_requested()) {
1986 pause_all_vcpus();
1987 cpu_synchronize_all_states();
1988 qemu_system_reset(VMRESET_SILENT);
1989 notifier_list_notify(&wakeup_notifiers, &wakeup_reason);
1990 wakeup_reason = QEMU_WAKEUP_REASON_NONE;
1991 resume_all_vcpus();
1992 monitor_protocol_event(QEVENT_WAKEUP, NULL);
1994 if (qemu_powerdown_requested()) {
1995 qemu_system_powerdown();
1997 if (qemu_vmstop_requested(&r)) {
1998 vm_stop(r);
2000 return false;
2003 static void main_loop(void)
2005 bool nonblocking;
2006 int last_io = 0;
2007 #ifdef CONFIG_PROFILER
2008 int64_t ti;
2009 #endif
2010 do {
2011 nonblocking = !kvm_enabled() && !xen_enabled() && last_io > 0;
2012 #ifdef CONFIG_PROFILER
2013 ti = profile_getclock();
2014 #endif
2015 last_io = main_loop_wait(nonblocking);
2016 #ifdef CONFIG_PROFILER
2017 dev_time += profile_getclock() - ti;
2018 #endif
2019 } while (!main_loop_should_exit());
2022 static void version(void)
2024 printf("QEMU emulator version " QEMU_VERSION QEMU_PKGVERSION ", Copyright (c) 2003-2008 Fabrice Bellard\n");
2027 static void help(int exitcode)
2029 version();
2030 printf("usage: %s [options] [disk_image]\n\n"
2031 "'disk_image' is a raw hard disk image for IDE hard disk 0\n\n",
2032 error_get_progname());
2034 #define QEMU_OPTIONS_GENERATE_HELP
2035 #include "qemu-options-wrapper.h"
2037 printf("\nDuring emulation, the following keys are useful:\n"
2038 "ctrl-alt-f toggle full screen\n"
2039 "ctrl-alt-n switch to virtual console 'n'\n"
2040 "ctrl-alt toggle mouse and keyboard grab\n"
2041 "\n"
2042 "When using -nographic, press 'ctrl-a h' to get some help.\n");
2044 exit(exitcode);
2047 #define HAS_ARG 0x0001
2049 typedef struct QEMUOption {
2050 const char *name;
2051 int flags;
2052 int index;
2053 uint32_t arch_mask;
2054 } QEMUOption;
2056 static const QEMUOption qemu_options[] = {
2057 { "h", 0, QEMU_OPTION_h, QEMU_ARCH_ALL },
2058 #define QEMU_OPTIONS_GENERATE_OPTIONS
2059 #include "qemu-options-wrapper.h"
2060 { NULL },
2063 static bool vga_available(void)
2065 return object_class_by_name("VGA") || object_class_by_name("isa-vga");
2068 static bool cirrus_vga_available(void)
2070 return object_class_by_name("cirrus-vga")
2071 || object_class_by_name("isa-cirrus-vga");
2074 static bool vmware_vga_available(void)
2076 return object_class_by_name("vmware-svga");
2079 static bool qxl_vga_available(void)
2081 return object_class_by_name("qxl-vga");
2084 static bool tcx_vga_available(void)
2086 return object_class_by_name("SUNW,tcx");
2089 static bool cg3_vga_available(void)
2091 return object_class_by_name("cgthree");
2094 static void select_vgahw (const char *p)
2096 const char *opts;
2098 assert(vga_interface_type == VGA_NONE);
2099 if (strstart(p, "std", &opts)) {
2100 if (vga_available()) {
2101 vga_interface_type = VGA_STD;
2102 } else {
2103 fprintf(stderr, "Error: standard VGA not available\n");
2104 exit(0);
2106 } else if (strstart(p, "cirrus", &opts)) {
2107 if (cirrus_vga_available()) {
2108 vga_interface_type = VGA_CIRRUS;
2109 } else {
2110 fprintf(stderr, "Error: Cirrus VGA not available\n");
2111 exit(0);
2113 } else if (strstart(p, "vmware", &opts)) {
2114 if (vmware_vga_available()) {
2115 vga_interface_type = VGA_VMWARE;
2116 } else {
2117 fprintf(stderr, "Error: VMWare SVGA not available\n");
2118 exit(0);
2120 } else if (strstart(p, "xenfb", &opts)) {
2121 vga_interface_type = VGA_XENFB;
2122 } else if (strstart(p, "qxl", &opts)) {
2123 if (qxl_vga_available()) {
2124 vga_interface_type = VGA_QXL;
2125 } else {
2126 fprintf(stderr, "Error: QXL VGA not available\n");
2127 exit(0);
2129 } else if (strstart(p, "tcx", &opts)) {
2130 if (tcx_vga_available()) {
2131 vga_interface_type = VGA_TCX;
2132 } else {
2133 fprintf(stderr, "Error: TCX framebuffer not available\n");
2134 exit(0);
2136 } else if (strstart(p, "cg3", &opts)) {
2137 if (cg3_vga_available()) {
2138 vga_interface_type = VGA_CG3;
2139 } else {
2140 fprintf(stderr, "Error: CG3 framebuffer not available\n");
2141 exit(0);
2143 } else if (!strstart(p, "none", &opts)) {
2144 invalid_vga:
2145 fprintf(stderr, "Unknown vga type: %s\n", p);
2146 exit(1);
2148 while (*opts) {
2149 const char *nextopt;
2151 if (strstart(opts, ",retrace=", &nextopt)) {
2152 opts = nextopt;
2153 if (strstart(opts, "dumb", &nextopt))
2154 vga_retrace_method = VGA_RETRACE_DUMB;
2155 else if (strstart(opts, "precise", &nextopt))
2156 vga_retrace_method = VGA_RETRACE_PRECISE;
2157 else goto invalid_vga;
2158 } else goto invalid_vga;
2159 opts = nextopt;
2163 static DisplayType select_display(const char *p)
2165 const char *opts;
2166 DisplayType display = DT_DEFAULT;
2168 if (strstart(p, "sdl", &opts)) {
2169 #ifdef CONFIG_SDL
2170 display = DT_SDL;
2171 while (*opts) {
2172 const char *nextopt;
2174 if (strstart(opts, ",frame=", &nextopt)) {
2175 opts = nextopt;
2176 if (strstart(opts, "on", &nextopt)) {
2177 no_frame = 0;
2178 } else if (strstart(opts, "off", &nextopt)) {
2179 no_frame = 1;
2180 } else {
2181 goto invalid_sdl_args;
2183 } else if (strstart(opts, ",alt_grab=", &nextopt)) {
2184 opts = nextopt;
2185 if (strstart(opts, "on", &nextopt)) {
2186 alt_grab = 1;
2187 } else if (strstart(opts, "off", &nextopt)) {
2188 alt_grab = 0;
2189 } else {
2190 goto invalid_sdl_args;
2192 } else if (strstart(opts, ",ctrl_grab=", &nextopt)) {
2193 opts = nextopt;
2194 if (strstart(opts, "on", &nextopt)) {
2195 ctrl_grab = 1;
2196 } else if (strstart(opts, "off", &nextopt)) {
2197 ctrl_grab = 0;
2198 } else {
2199 goto invalid_sdl_args;
2201 } else if (strstart(opts, ",window_close=", &nextopt)) {
2202 opts = nextopt;
2203 if (strstart(opts, "on", &nextopt)) {
2204 no_quit = 0;
2205 } else if (strstart(opts, "off", &nextopt)) {
2206 no_quit = 1;
2207 } else {
2208 goto invalid_sdl_args;
2210 } else {
2211 invalid_sdl_args:
2212 fprintf(stderr, "Invalid SDL option string: %s\n", p);
2213 exit(1);
2215 opts = nextopt;
2217 #else
2218 fprintf(stderr, "SDL support is disabled\n");
2219 exit(1);
2220 #endif
2221 } else if (strstart(p, "vnc", &opts)) {
2222 #ifdef CONFIG_VNC
2223 display_remote++;
2225 if (*opts) {
2226 const char *nextopt;
2228 if (strstart(opts, "=", &nextopt)) {
2229 vnc_display = nextopt;
2232 if (!vnc_display) {
2233 fprintf(stderr, "VNC requires a display argument vnc=<display>\n");
2234 exit(1);
2236 #else
2237 fprintf(stderr, "VNC support is disabled\n");
2238 exit(1);
2239 #endif
2240 } else if (strstart(p, "curses", &opts)) {
2241 #ifdef CONFIG_CURSES
2242 display = DT_CURSES;
2243 #else
2244 fprintf(stderr, "Curses support is disabled\n");
2245 exit(1);
2246 #endif
2247 } else if (strstart(p, "gtk", &opts)) {
2248 #ifdef CONFIG_GTK
2249 display = DT_GTK;
2250 while (*opts) {
2251 const char *nextopt;
2253 if (strstart(opts, ",grab_on_hover=", &nextopt)) {
2254 opts = nextopt;
2255 if (strstart(opts, "on", &nextopt)) {
2256 grab_on_hover = true;
2257 } else if (strstart(opts, "off", &nextopt)) {
2258 grab_on_hover = false;
2259 } else {
2260 goto invalid_gtk_args;
2262 } else {
2263 invalid_gtk_args:
2264 fprintf(stderr, "Invalid GTK option string: %s\n", p);
2265 exit(1);
2267 opts = nextopt;
2269 #else
2270 fprintf(stderr, "GTK support is disabled\n");
2271 exit(1);
2272 #endif
2273 } else if (strstart(p, "none", &opts)) {
2274 display = DT_NONE;
2275 } else {
2276 fprintf(stderr, "Unknown display type: %s\n", p);
2277 exit(1);
2280 return display;
2283 static int balloon_parse(const char *arg)
2285 QemuOpts *opts;
2287 if (strcmp(arg, "none") == 0) {
2288 return 0;
2291 if (!strncmp(arg, "virtio", 6)) {
2292 if (arg[6] == ',') {
2293 /* have params -> parse them */
2294 opts = qemu_opts_parse(qemu_find_opts("device"), arg+7, 0);
2295 if (!opts)
2296 return -1;
2297 } else {
2298 /* create empty opts */
2299 opts = qemu_opts_create(qemu_find_opts("device"), NULL, 0,
2300 &error_abort);
2302 qemu_opt_set(opts, "driver", "virtio-balloon");
2303 return 0;
2306 return -1;
2309 char *qemu_find_file(int type, const char *name)
2311 int i;
2312 const char *subdir;
2313 char *buf;
2315 /* Try the name as a straight path first */
2316 if (access(name, R_OK) == 0) {
2317 trace_load_file(name, name);
2318 return g_strdup(name);
2321 switch (type) {
2322 case QEMU_FILE_TYPE_BIOS:
2323 subdir = "";
2324 break;
2325 case QEMU_FILE_TYPE_KEYMAP:
2326 subdir = "keymaps/";
2327 break;
2328 default:
2329 abort();
2332 for (i = 0; i < data_dir_idx; i++) {
2333 buf = g_strdup_printf("%s/%s%s", data_dir[i], subdir, name);
2334 if (access(buf, R_OK) == 0) {
2335 trace_load_file(name, buf);
2336 return buf;
2338 g_free(buf);
2340 return NULL;
2343 static int device_help_func(QemuOpts *opts, void *opaque)
2345 return qdev_device_help(opts);
2348 static int device_init_func(QemuOpts *opts, void *opaque)
2350 DeviceState *dev;
2352 dev = qdev_device_add(opts);
2353 if (!dev)
2354 return -1;
2355 object_unref(OBJECT(dev));
2356 return 0;
2359 static int chardev_init_func(QemuOpts *opts, void *opaque)
2361 Error *local_err = NULL;
2363 qemu_chr_new_from_opts(opts, NULL, &local_err);
2364 if (local_err) {
2365 error_report("%s", error_get_pretty(local_err));
2366 error_free(local_err);
2367 return -1;
2369 return 0;
2372 #ifdef CONFIG_VIRTFS
2373 static int fsdev_init_func(QemuOpts *opts, void *opaque)
2375 int ret;
2376 ret = qemu_fsdev_add(opts);
2378 return ret;
2380 #endif
2382 static int mon_init_func(QemuOpts *opts, void *opaque)
2384 CharDriverState *chr;
2385 const char *chardev;
2386 const char *mode;
2387 int flags;
2389 mode = qemu_opt_get(opts, "mode");
2390 if (mode == NULL) {
2391 mode = "readline";
2393 if (strcmp(mode, "readline") == 0) {
2394 flags = MONITOR_USE_READLINE;
2395 } else if (strcmp(mode, "control") == 0) {
2396 flags = MONITOR_USE_CONTROL;
2397 } else {
2398 fprintf(stderr, "unknown monitor mode \"%s\"\n", mode);
2399 exit(1);
2402 if (qemu_opt_get_bool(opts, "pretty", 0))
2403 flags |= MONITOR_USE_PRETTY;
2405 if (qemu_opt_get_bool(opts, "default", 0))
2406 flags |= MONITOR_IS_DEFAULT;
2408 chardev = qemu_opt_get(opts, "chardev");
2409 chr = qemu_chr_find(chardev);
2410 if (chr == NULL) {
2411 fprintf(stderr, "chardev \"%s\" not found\n", chardev);
2412 exit(1);
2415 qemu_chr_fe_claim_no_fail(chr);
2416 monitor_init(chr, flags);
2417 return 0;
2420 static void monitor_parse(const char *optarg, const char *mode)
2422 static int monitor_device_index = 0;
2423 QemuOpts *opts;
2424 const char *p;
2425 char label[32];
2426 int def = 0;
2428 if (strstart(optarg, "chardev:", &p)) {
2429 snprintf(label, sizeof(label), "%s", p);
2430 } else {
2431 snprintf(label, sizeof(label), "compat_monitor%d",
2432 monitor_device_index);
2433 if (monitor_device_index == 0) {
2434 def = 1;
2436 opts = qemu_chr_parse_compat(label, optarg);
2437 if (!opts) {
2438 fprintf(stderr, "parse error: %s\n", optarg);
2439 exit(1);
2443 opts = qemu_opts_create(qemu_find_opts("mon"), label, 1, NULL);
2444 if (!opts) {
2445 fprintf(stderr, "duplicate chardev: %s\n", label);
2446 exit(1);
2448 qemu_opt_set(opts, "mode", mode);
2449 qemu_opt_set(opts, "chardev", label);
2450 if (def)
2451 qemu_opt_set(opts, "default", "on");
2452 monitor_device_index++;
2455 struct device_config {
2456 enum {
2457 DEV_USB, /* -usbdevice */
2458 DEV_BT, /* -bt */
2459 DEV_SERIAL, /* -serial */
2460 DEV_PARALLEL, /* -parallel */
2461 DEV_VIRTCON, /* -virtioconsole */
2462 DEV_DEBUGCON, /* -debugcon */
2463 DEV_GDB, /* -gdb, -s */
2464 DEV_SCLP, /* s390 sclp */
2465 } type;
2466 const char *cmdline;
2467 Location loc;
2468 QTAILQ_ENTRY(device_config) next;
2471 static QTAILQ_HEAD(, device_config) device_configs =
2472 QTAILQ_HEAD_INITIALIZER(device_configs);
2474 static void add_device_config(int type, const char *cmdline)
2476 struct device_config *conf;
2478 conf = g_malloc0(sizeof(*conf));
2479 conf->type = type;
2480 conf->cmdline = cmdline;
2481 loc_save(&conf->loc);
2482 QTAILQ_INSERT_TAIL(&device_configs, conf, next);
2485 static int foreach_device_config(int type, int (*func)(const char *cmdline))
2487 struct device_config *conf;
2488 int rc;
2490 QTAILQ_FOREACH(conf, &device_configs, next) {
2491 if (conf->type != type)
2492 continue;
2493 loc_push_restore(&conf->loc);
2494 rc = func(conf->cmdline);
2495 loc_pop(&conf->loc);
2496 if (0 != rc)
2497 return rc;
2499 return 0;
2502 static int serial_parse(const char *devname)
2504 static int index = 0;
2505 char label[32];
2507 if (strcmp(devname, "none") == 0)
2508 return 0;
2509 if (index == MAX_SERIAL_PORTS) {
2510 fprintf(stderr, "qemu: too many serial ports\n");
2511 exit(1);
2513 snprintf(label, sizeof(label), "serial%d", index);
2514 serial_hds[index] = qemu_chr_new(label, devname, NULL);
2515 if (!serial_hds[index]) {
2516 fprintf(stderr, "qemu: could not connect serial device"
2517 " to character backend '%s'\n", devname);
2518 return -1;
2520 index++;
2521 return 0;
2524 static int parallel_parse(const char *devname)
2526 static int index = 0;
2527 char label[32];
2529 if (strcmp(devname, "none") == 0)
2530 return 0;
2531 if (index == MAX_PARALLEL_PORTS) {
2532 fprintf(stderr, "qemu: too many parallel ports\n");
2533 exit(1);
2535 snprintf(label, sizeof(label), "parallel%d", index);
2536 parallel_hds[index] = qemu_chr_new(label, devname, NULL);
2537 if (!parallel_hds[index]) {
2538 fprintf(stderr, "qemu: could not connect parallel device"
2539 " to character backend '%s'\n", devname);
2540 return -1;
2542 index++;
2543 return 0;
2546 static int virtcon_parse(const char *devname)
2548 QemuOptsList *device = qemu_find_opts("device");
2549 static int index = 0;
2550 char label[32];
2551 QemuOpts *bus_opts, *dev_opts;
2553 if (strcmp(devname, "none") == 0)
2554 return 0;
2555 if (index == MAX_VIRTIO_CONSOLES) {
2556 fprintf(stderr, "qemu: too many virtio consoles\n");
2557 exit(1);
2560 bus_opts = qemu_opts_create(device, NULL, 0, &error_abort);
2561 if (arch_type == QEMU_ARCH_S390X) {
2562 qemu_opt_set(bus_opts, "driver", "virtio-serial-s390");
2563 } else {
2564 qemu_opt_set(bus_opts, "driver", "virtio-serial-pci");
2567 dev_opts = qemu_opts_create(device, NULL, 0, &error_abort);
2568 qemu_opt_set(dev_opts, "driver", "virtconsole");
2570 snprintf(label, sizeof(label), "virtcon%d", index);
2571 virtcon_hds[index] = qemu_chr_new(label, devname, NULL);
2572 if (!virtcon_hds[index]) {
2573 fprintf(stderr, "qemu: could not connect virtio console"
2574 " to character backend '%s'\n", devname);
2575 return -1;
2577 qemu_opt_set(dev_opts, "chardev", label);
2579 index++;
2580 return 0;
2583 static int sclp_parse(const char *devname)
2585 QemuOptsList *device = qemu_find_opts("device");
2586 static int index = 0;
2587 char label[32];
2588 QemuOpts *dev_opts;
2590 if (strcmp(devname, "none") == 0) {
2591 return 0;
2593 if (index == MAX_SCLP_CONSOLES) {
2594 fprintf(stderr, "qemu: too many sclp consoles\n");
2595 exit(1);
2598 assert(arch_type == QEMU_ARCH_S390X);
2600 dev_opts = qemu_opts_create(device, NULL, 0, NULL);
2601 qemu_opt_set(dev_opts, "driver", "sclpconsole");
2603 snprintf(label, sizeof(label), "sclpcon%d", index);
2604 sclp_hds[index] = qemu_chr_new(label, devname, NULL);
2605 if (!sclp_hds[index]) {
2606 fprintf(stderr, "qemu: could not connect sclp console"
2607 " to character backend '%s'\n", devname);
2608 return -1;
2610 qemu_opt_set(dev_opts, "chardev", label);
2612 index++;
2613 return 0;
2616 static int debugcon_parse(const char *devname)
2618 QemuOpts *opts;
2620 if (!qemu_chr_new("debugcon", devname, NULL)) {
2621 exit(1);
2623 opts = qemu_opts_create(qemu_find_opts("device"), "debugcon", 1, NULL);
2624 if (!opts) {
2625 fprintf(stderr, "qemu: already have a debugcon device\n");
2626 exit(1);
2628 qemu_opt_set(opts, "driver", "isa-debugcon");
2629 qemu_opt_set(opts, "chardev", "debugcon");
2630 return 0;
2633 static MachineClass *machine_parse(const char *name)
2635 MachineClass *mc = NULL;
2636 GSList *el, *machines = object_class_get_list(TYPE_MACHINE, false);
2638 if (name) {
2639 mc = find_machine(name);
2641 if (mc) {
2642 return mc;
2644 if (name && !is_help_option(name)) {
2645 error_report("Unsupported machine type");
2646 error_printf("Use -machine help to list supported machines!\n");
2647 } else {
2648 printf("Supported machines are:\n");
2649 for (el = machines; el; el = el->next) {
2650 MachineClass *mc = el->data;
2651 if (mc->alias) {
2652 printf("%-20s %s (alias of %s)\n", mc->alias, mc->desc, mc->name);
2654 printf("%-20s %s%s\n", mc->name, mc->desc,
2655 mc->is_default ? " (default)" : "");
2659 g_slist_free(machines);
2660 exit(!name || !is_help_option(name));
2663 static int tcg_init(MachineClass *mc)
2665 tcg_exec_init(tcg_tb_size * 1024 * 1024);
2666 return 0;
2669 static struct {
2670 const char *opt_name;
2671 const char *name;
2672 int (*available)(void);
2673 int (*init)(MachineClass *mc);
2674 bool *allowed;
2675 } accel_list[] = {
2676 { "tcg", "tcg", tcg_available, tcg_init, &tcg_allowed },
2677 { "xen", "Xen", xen_available, xen_init, &xen_allowed },
2678 { "kvm", "KVM", kvm_available, kvm_init, &kvm_allowed },
2679 { "qtest", "QTest", qtest_available, qtest_init_accel, &qtest_allowed },
2682 static int configure_accelerator(MachineClass *mc)
2684 const char *p;
2685 char buf[10];
2686 int i, ret;
2687 bool accel_initialised = false;
2688 bool init_failed = false;
2690 p = qemu_opt_get(qemu_get_machine_opts(), "accel");
2691 if (p == NULL) {
2692 /* Use the default "accelerator", tcg */
2693 p = "tcg";
2696 while (!accel_initialised && *p != '\0') {
2697 if (*p == ':') {
2698 p++;
2700 p = get_opt_name(buf, sizeof (buf), p, ':');
2701 for (i = 0; i < ARRAY_SIZE(accel_list); i++) {
2702 if (strcmp(accel_list[i].opt_name, buf) == 0) {
2703 if (!accel_list[i].available()) {
2704 printf("%s not supported for this target\n",
2705 accel_list[i].name);
2706 break;
2708 *(accel_list[i].allowed) = true;
2709 ret = accel_list[i].init(mc);
2710 if (ret < 0) {
2711 init_failed = true;
2712 fprintf(stderr, "failed to initialize %s: %s\n",
2713 accel_list[i].name,
2714 strerror(-ret));
2715 *(accel_list[i].allowed) = false;
2716 } else {
2717 accel_initialised = true;
2719 break;
2722 if (i == ARRAY_SIZE(accel_list)) {
2723 fprintf(stderr, "\"%s\" accelerator does not exist.\n", buf);
2727 if (!accel_initialised) {
2728 if (!init_failed) {
2729 fprintf(stderr, "No accelerator found!\n");
2731 exit(1);
2734 if (init_failed) {
2735 fprintf(stderr, "Back to %s accelerator.\n", accel_list[i].name);
2738 return !accel_initialised;
2741 void qemu_add_exit_notifier(Notifier *notify)
2743 notifier_list_add(&exit_notifiers, notify);
2746 void qemu_remove_exit_notifier(Notifier *notify)
2748 notifier_remove(notify);
2751 static void qemu_run_exit_notifiers(void)
2753 notifier_list_notify(&exit_notifiers, NULL);
2756 void qemu_add_machine_init_done_notifier(Notifier *notify)
2758 notifier_list_add(&machine_init_done_notifiers, notify);
2761 static void qemu_run_machine_init_done_notifiers(void)
2763 notifier_list_notify(&machine_init_done_notifiers, NULL);
2766 static const QEMUOption *lookup_opt(int argc, char **argv,
2767 const char **poptarg, int *poptind)
2769 const QEMUOption *popt;
2770 int optind = *poptind;
2771 char *r = argv[optind];
2772 const char *optarg;
2774 loc_set_cmdline(argv, optind, 1);
2775 optind++;
2776 /* Treat --foo the same as -foo. */
2777 if (r[1] == '-')
2778 r++;
2779 popt = qemu_options;
2780 for(;;) {
2781 if (!popt->name) {
2782 error_report("invalid option");
2783 exit(1);
2785 if (!strcmp(popt->name, r + 1))
2786 break;
2787 popt++;
2789 if (popt->flags & HAS_ARG) {
2790 if (optind >= argc) {
2791 error_report("requires an argument");
2792 exit(1);
2794 optarg = argv[optind++];
2795 loc_set_cmdline(argv, optind - 2, 2);
2796 } else {
2797 optarg = NULL;
2800 *poptarg = optarg;
2801 *poptind = optind;
2803 return popt;
2806 static gpointer malloc_and_trace(gsize n_bytes)
2808 void *ptr = malloc(n_bytes);
2809 trace_g_malloc(n_bytes, ptr);
2810 return ptr;
2813 static gpointer realloc_and_trace(gpointer mem, gsize n_bytes)
2815 void *ptr = realloc(mem, n_bytes);
2816 trace_g_realloc(mem, n_bytes, ptr);
2817 return ptr;
2820 static void free_and_trace(gpointer mem)
2822 trace_g_free(mem);
2823 free(mem);
2826 static int object_set_property(const char *name, const char *value, void *opaque)
2828 Object *obj = OBJECT(opaque);
2829 StringInputVisitor *siv;
2830 Error *local_err = NULL;
2832 if (strcmp(name, "qom-type") == 0 || strcmp(name, "id") == 0 ||
2833 strcmp(name, "type") == 0) {
2834 return 0;
2837 siv = string_input_visitor_new(value);
2838 object_property_set(obj, string_input_get_visitor(siv), name, &local_err);
2839 string_input_visitor_cleanup(siv);
2841 if (local_err) {
2842 qerror_report_err(local_err);
2843 error_free(local_err);
2844 return -1;
2847 return 0;
2850 static int object_create(QemuOpts *opts, void *opaque)
2852 Error *err = NULL;
2853 char *type = NULL;
2854 char *id = NULL;
2855 void *dummy = NULL;
2856 OptsVisitor *ov;
2857 QDict *pdict;
2859 ov = opts_visitor_new(opts);
2860 pdict = qemu_opts_to_qdict(opts, NULL);
2862 visit_start_struct(opts_get_visitor(ov), &dummy, NULL, NULL, 0, &err);
2863 if (err) {
2864 goto out;
2867 qdict_del(pdict, "qom-type");
2868 visit_type_str(opts_get_visitor(ov), &type, "qom-type", &err);
2869 if (err) {
2870 goto out;
2873 qdict_del(pdict, "id");
2874 visit_type_str(opts_get_visitor(ov), &id, "id", &err);
2875 if (err) {
2876 goto out;
2879 object_add(type, id, pdict, opts_get_visitor(ov), &err);
2880 if (err) {
2881 goto out;
2883 visit_end_struct(opts_get_visitor(ov), &err);
2884 if (err) {
2885 qmp_object_del(id, NULL);
2888 out:
2889 opts_visitor_cleanup(ov);
2891 QDECREF(pdict);
2892 g_free(id);
2893 g_free(type);
2894 g_free(dummy);
2895 if (err) {
2896 qerror_report_err(err);
2897 return -1;
2899 return 0;
2902 int main(int argc, char **argv, char **envp)
2904 int i;
2905 int snapshot, linux_boot;
2906 const char *icount_option = NULL;
2907 const char *initrd_filename;
2908 const char *kernel_filename, *kernel_cmdline;
2909 const char *boot_order;
2910 DisplayState *ds;
2911 int cyls, heads, secs, translation;
2912 QemuOpts *hda_opts = NULL, *opts, *machine_opts;
2913 QemuOptsList *olist;
2914 int optind;
2915 const char *optarg;
2916 const char *loadvm = NULL;
2917 MachineClass *machine_class;
2918 const char *cpu_model;
2919 const char *vga_model = NULL;
2920 const char *qtest_chrdev = NULL;
2921 const char *qtest_log = NULL;
2922 const char *pid_file = NULL;
2923 const char *incoming = NULL;
2924 #ifdef CONFIG_VNC
2925 int show_vnc_port = 0;
2926 #endif
2927 bool defconfig = true;
2928 bool userconfig = true;
2929 const char *log_mask = NULL;
2930 const char *log_file = NULL;
2931 GMemVTable mem_trace = {
2932 .malloc = malloc_and_trace,
2933 .realloc = realloc_and_trace,
2934 .free = free_and_trace,
2936 const char *trace_events = NULL;
2937 const char *trace_file = NULL;
2938 const ram_addr_t default_ram_size = (ram_addr_t)DEFAULT_RAM_SIZE *
2939 1024 * 1024;
2940 ram_addr_t maxram_size = default_ram_size;
2941 uint64_t ram_slots = 0;
2943 atexit(qemu_run_exit_notifiers);
2944 error_set_progname(argv[0]);
2945 qemu_init_exec_dir(argv[0]);
2947 g_mem_set_vtable(&mem_trace);
2949 module_call_init(MODULE_INIT_QOM);
2951 qemu_add_opts(&qemu_drive_opts);
2952 qemu_add_drive_opts(&qemu_legacy_drive_opts);
2953 qemu_add_drive_opts(&qemu_common_drive_opts);
2954 qemu_add_drive_opts(&qemu_drive_opts);
2955 qemu_add_opts(&qemu_chardev_opts);
2956 qemu_add_opts(&qemu_device_opts);
2957 qemu_add_opts(&qemu_netdev_opts);
2958 qemu_add_opts(&qemu_net_opts);
2959 qemu_add_opts(&qemu_rtc_opts);
2960 qemu_add_opts(&qemu_global_opts);
2961 qemu_add_opts(&qemu_mon_opts);
2962 qemu_add_opts(&qemu_trace_opts);
2963 qemu_add_opts(&qemu_option_rom_opts);
2964 qemu_add_opts(&qemu_machine_opts);
2965 qemu_add_opts(&qemu_mem_opts);
2966 qemu_add_opts(&qemu_smp_opts);
2967 qemu_add_opts(&qemu_boot_opts);
2968 qemu_add_opts(&qemu_sandbox_opts);
2969 qemu_add_opts(&qemu_add_fd_opts);
2970 qemu_add_opts(&qemu_object_opts);
2971 qemu_add_opts(&qemu_tpmdev_opts);
2972 qemu_add_opts(&qemu_realtime_opts);
2973 qemu_add_opts(&qemu_msg_opts);
2974 qemu_add_opts(&qemu_name_opts);
2975 qemu_add_opts(&qemu_numa_opts);
2977 runstate_init();
2979 rtc_clock = QEMU_CLOCK_HOST;
2981 qemu_init_auxval(envp);
2982 qemu_cache_utils_init();
2984 QLIST_INIT (&vm_change_state_head);
2985 os_setup_early_signal_handling();
2987 module_call_init(MODULE_INIT_MACHINE);
2988 machine_class = find_default_machine();
2989 cpu_model = NULL;
2990 ram_size = default_ram_size;
2991 snapshot = 0;
2992 cyls = heads = secs = 0;
2993 translation = BIOS_ATA_TRANSLATION_AUTO;
2995 for (i = 0; i < MAX_NODES; i++) {
2996 numa_info[i].node_mem = 0;
2997 bitmap_zero(numa_info[i].node_cpu, MAX_CPUMASK_BITS);
3000 nb_numa_nodes = 0;
3001 nb_nics = 0;
3003 bdrv_init_with_whitelist();
3005 autostart = 1;
3007 /* first pass of option parsing */
3008 optind = 1;
3009 while (optind < argc) {
3010 if (argv[optind][0] != '-') {
3011 /* disk image */
3012 optind++;
3013 } else {
3014 const QEMUOption *popt;
3016 popt = lookup_opt(argc, argv, &optarg, &optind);
3017 switch (popt->index) {
3018 case QEMU_OPTION_nodefconfig:
3019 defconfig = false;
3020 break;
3021 case QEMU_OPTION_nouserconfig:
3022 userconfig = false;
3023 break;
3028 if (defconfig) {
3029 int ret;
3030 ret = qemu_read_default_config_files(userconfig);
3031 if (ret < 0) {
3032 exit(1);
3036 /* second pass of option parsing */
3037 optind = 1;
3038 for(;;) {
3039 if (optind >= argc)
3040 break;
3041 if (argv[optind][0] != '-') {
3042 hda_opts = drive_add(IF_DEFAULT, 0, argv[optind++], HD_OPTS);
3043 } else {
3044 const QEMUOption *popt;
3046 popt = lookup_opt(argc, argv, &optarg, &optind);
3047 if (!(popt->arch_mask & arch_type)) {
3048 printf("Option %s not supported for this target\n", popt->name);
3049 exit(1);
3051 switch(popt->index) {
3052 case QEMU_OPTION_M:
3053 machine_class = machine_parse(optarg);
3054 break;
3055 case QEMU_OPTION_no_kvm_irqchip: {
3056 olist = qemu_find_opts("machine");
3057 qemu_opts_parse(olist, "kernel_irqchip=off", 0);
3058 break;
3060 case QEMU_OPTION_cpu:
3061 /* hw initialization will check this */
3062 cpu_model = optarg;
3063 break;
3064 case QEMU_OPTION_hda:
3066 char buf[256];
3067 if (cyls == 0)
3068 snprintf(buf, sizeof(buf), "%s", HD_OPTS);
3069 else
3070 snprintf(buf, sizeof(buf),
3071 "%s,cyls=%d,heads=%d,secs=%d%s",
3072 HD_OPTS , cyls, heads, secs,
3073 translation == BIOS_ATA_TRANSLATION_LBA ?
3074 ",trans=lba" :
3075 translation == BIOS_ATA_TRANSLATION_NONE ?
3076 ",trans=none" : "");
3077 drive_add(IF_DEFAULT, 0, optarg, buf);
3078 break;
3080 case QEMU_OPTION_hdb:
3081 case QEMU_OPTION_hdc:
3082 case QEMU_OPTION_hdd:
3083 drive_add(IF_DEFAULT, popt->index - QEMU_OPTION_hda, optarg,
3084 HD_OPTS);
3085 break;
3086 case QEMU_OPTION_drive:
3087 if (drive_def(optarg) == NULL) {
3088 exit(1);
3090 break;
3091 case QEMU_OPTION_set:
3092 if (qemu_set_option(optarg) != 0)
3093 exit(1);
3094 break;
3095 case QEMU_OPTION_global:
3096 if (qemu_global_option(optarg) != 0)
3097 exit(1);
3098 break;
3099 case QEMU_OPTION_mtdblock:
3100 drive_add(IF_MTD, -1, optarg, MTD_OPTS);
3101 break;
3102 case QEMU_OPTION_sd:
3103 drive_add(IF_SD, -1, optarg, SD_OPTS);
3104 break;
3105 case QEMU_OPTION_pflash:
3106 drive_add(IF_PFLASH, -1, optarg, PFLASH_OPTS);
3107 break;
3108 case QEMU_OPTION_snapshot:
3109 snapshot = 1;
3110 break;
3111 case QEMU_OPTION_hdachs:
3113 const char *p;
3114 p = optarg;
3115 cyls = strtol(p, (char **)&p, 0);
3116 if (cyls < 1 || cyls > 16383)
3117 goto chs_fail;
3118 if (*p != ',')
3119 goto chs_fail;
3120 p++;
3121 heads = strtol(p, (char **)&p, 0);
3122 if (heads < 1 || heads > 16)
3123 goto chs_fail;
3124 if (*p != ',')
3125 goto chs_fail;
3126 p++;
3127 secs = strtol(p, (char **)&p, 0);
3128 if (secs < 1 || secs > 63)
3129 goto chs_fail;
3130 if (*p == ',') {
3131 p++;
3132 if (!strcmp(p, "large")) {
3133 translation = BIOS_ATA_TRANSLATION_LARGE;
3134 } else if (!strcmp(p, "rechs")) {
3135 translation = BIOS_ATA_TRANSLATION_RECHS;
3136 } else if (!strcmp(p, "none")) {
3137 translation = BIOS_ATA_TRANSLATION_NONE;
3138 } else if (!strcmp(p, "lba")) {
3139 translation = BIOS_ATA_TRANSLATION_LBA;
3140 } else if (!strcmp(p, "auto")) {
3141 translation = BIOS_ATA_TRANSLATION_AUTO;
3142 } else {
3143 goto chs_fail;
3145 } else if (*p != '\0') {
3146 chs_fail:
3147 fprintf(stderr, "qemu: invalid physical CHS format\n");
3148 exit(1);
3150 if (hda_opts != NULL) {
3151 char num[16];
3152 snprintf(num, sizeof(num), "%d", cyls);
3153 qemu_opt_set(hda_opts, "cyls", num);
3154 snprintf(num, sizeof(num), "%d", heads);
3155 qemu_opt_set(hda_opts, "heads", num);
3156 snprintf(num, sizeof(num), "%d", secs);
3157 qemu_opt_set(hda_opts, "secs", num);
3158 if (translation == BIOS_ATA_TRANSLATION_LARGE) {
3159 qemu_opt_set(hda_opts, "trans", "large");
3160 } else if (translation == BIOS_ATA_TRANSLATION_RECHS) {
3161 qemu_opt_set(hda_opts, "trans", "rechs");
3162 } else if (translation == BIOS_ATA_TRANSLATION_LBA) {
3163 qemu_opt_set(hda_opts, "trans", "lba");
3164 } else if (translation == BIOS_ATA_TRANSLATION_NONE) {
3165 qemu_opt_set(hda_opts, "trans", "none");
3169 break;
3170 case QEMU_OPTION_numa:
3171 opts = qemu_opts_parse(qemu_find_opts("numa"), optarg, 1);
3172 if (!opts) {
3173 exit(1);
3175 break;
3176 case QEMU_OPTION_display:
3177 display_type = select_display(optarg);
3178 break;
3179 case QEMU_OPTION_nographic:
3180 display_type = DT_NOGRAPHIC;
3181 break;
3182 case QEMU_OPTION_curses:
3183 #ifdef CONFIG_CURSES
3184 display_type = DT_CURSES;
3185 #else
3186 fprintf(stderr, "Curses support is disabled\n");
3187 exit(1);
3188 #endif
3189 break;
3190 case QEMU_OPTION_portrait:
3191 graphic_rotate = 90;
3192 break;
3193 case QEMU_OPTION_rotate:
3194 graphic_rotate = strtol(optarg, (char **) &optarg, 10);
3195 if (graphic_rotate != 0 && graphic_rotate != 90 &&
3196 graphic_rotate != 180 && graphic_rotate != 270) {
3197 fprintf(stderr,
3198 "qemu: only 90, 180, 270 deg rotation is available\n");
3199 exit(1);
3201 break;
3202 case QEMU_OPTION_kernel:
3203 qemu_opts_set(qemu_find_opts("machine"), 0, "kernel", optarg);
3204 break;
3205 case QEMU_OPTION_initrd:
3206 qemu_opts_set(qemu_find_opts("machine"), 0, "initrd", optarg);
3207 break;
3208 case QEMU_OPTION_append:
3209 qemu_opts_set(qemu_find_opts("machine"), 0, "append", optarg);
3210 break;
3211 case QEMU_OPTION_dtb:
3212 qemu_opts_set(qemu_find_opts("machine"), 0, "dtb", optarg);
3213 break;
3214 case QEMU_OPTION_cdrom:
3215 drive_add(IF_DEFAULT, 2, optarg, CDROM_OPTS);
3216 break;
3217 case QEMU_OPTION_boot:
3218 opts = qemu_opts_parse(qemu_find_opts("boot-opts"), optarg, 1);
3219 if (!opts) {
3220 exit(1);
3222 break;
3223 case QEMU_OPTION_fda:
3224 case QEMU_OPTION_fdb:
3225 drive_add(IF_FLOPPY, popt->index - QEMU_OPTION_fda,
3226 optarg, FD_OPTS);
3227 break;
3228 case QEMU_OPTION_no_fd_bootchk:
3229 fd_bootchk = 0;
3230 break;
3231 case QEMU_OPTION_netdev:
3232 if (net_client_parse(qemu_find_opts("netdev"), optarg) == -1) {
3233 exit(1);
3235 break;
3236 case QEMU_OPTION_net:
3237 if (net_client_parse(qemu_find_opts("net"), optarg) == -1) {
3238 exit(1);
3240 break;
3241 #ifdef CONFIG_LIBISCSI
3242 case QEMU_OPTION_iscsi:
3243 opts = qemu_opts_parse(qemu_find_opts("iscsi"), optarg, 0);
3244 if (!opts) {
3245 exit(1);
3247 break;
3248 #endif
3249 #ifdef CONFIG_SLIRP
3250 case QEMU_OPTION_tftp:
3251 legacy_tftp_prefix = optarg;
3252 break;
3253 case QEMU_OPTION_bootp:
3254 legacy_bootp_filename = optarg;
3255 break;
3256 case QEMU_OPTION_redir:
3257 if (net_slirp_redir(optarg) < 0)
3258 exit(1);
3259 break;
3260 #endif
3261 case QEMU_OPTION_bt:
3262 add_device_config(DEV_BT, optarg);
3263 break;
3264 case QEMU_OPTION_audio_help:
3265 AUD_help ();
3266 exit (0);
3267 break;
3268 case QEMU_OPTION_soundhw:
3269 select_soundhw (optarg);
3270 break;
3271 case QEMU_OPTION_h:
3272 help(0);
3273 break;
3274 case QEMU_OPTION_version:
3275 version();
3276 exit(0);
3277 break;
3278 case QEMU_OPTION_m: {
3279 uint64_t sz;
3280 const char *mem_str;
3281 const char *maxmem_str, *slots_str;
3283 opts = qemu_opts_parse(qemu_find_opts("memory"),
3284 optarg, 1);
3285 if (!opts) {
3286 exit(EXIT_FAILURE);
3289 mem_str = qemu_opt_get(opts, "size");
3290 if (!mem_str) {
3291 error_report("invalid -m option, missing 'size' option");
3292 exit(EXIT_FAILURE);
3294 if (!*mem_str) {
3295 error_report("missing 'size' option value");
3296 exit(EXIT_FAILURE);
3299 sz = qemu_opt_get_size(opts, "size", ram_size);
3301 /* Fix up legacy suffix-less format */
3302 if (g_ascii_isdigit(mem_str[strlen(mem_str) - 1])) {
3303 uint64_t overflow_check = sz;
3305 sz <<= 20;
3306 if ((sz >> 20) != overflow_check) {
3307 error_report("too large 'size' option value");
3308 exit(EXIT_FAILURE);
3312 /* backward compatibility behaviour for case "-m 0" */
3313 if (sz == 0) {
3314 sz = default_ram_size;
3317 sz = QEMU_ALIGN_UP(sz, 8192);
3318 ram_size = sz;
3319 if (ram_size != sz) {
3320 error_report("ram size too large");
3321 exit(EXIT_FAILURE);
3324 maxmem_str = qemu_opt_get(opts, "maxmem");
3325 slots_str = qemu_opt_get(opts, "slots");
3326 if (maxmem_str && slots_str) {
3327 uint64_t slots;
3329 sz = qemu_opt_get_size(opts, "maxmem", 0);
3330 if (sz < ram_size) {
3331 fprintf(stderr, "qemu: invalid -m option value: maxmem "
3332 "(%" PRIu64 ") <= initial memory ("
3333 RAM_ADDR_FMT ")\n", sz, ram_size);
3334 exit(EXIT_FAILURE);
3337 slots = qemu_opt_get_number(opts, "slots", 0);
3338 if ((sz > ram_size) && !slots) {
3339 fprintf(stderr, "qemu: invalid -m option value: maxmem "
3340 "(%" PRIu64 ") more than initial memory ("
3341 RAM_ADDR_FMT ") but no hotplug slots where "
3342 "specified\n", sz, ram_size);
3343 exit(EXIT_FAILURE);
3346 if ((sz <= ram_size) && slots) {
3347 fprintf(stderr, "qemu: invalid -m option value: %"
3348 PRIu64 " hotplug slots where specified but "
3349 "maxmem (%" PRIu64 ") <= initial memory ("
3350 RAM_ADDR_FMT ")\n", slots, sz, ram_size);
3351 exit(EXIT_FAILURE);
3353 maxram_size = sz;
3354 ram_slots = slots;
3355 } else if ((!maxmem_str && slots_str) ||
3356 (maxmem_str && !slots_str)) {
3357 fprintf(stderr, "qemu: invalid -m option value: missing "
3358 "'%s' option\n", slots_str ? "maxmem" : "slots");
3359 exit(EXIT_FAILURE);
3361 break;
3363 #ifdef CONFIG_TPM
3364 case QEMU_OPTION_tpmdev:
3365 if (tpm_config_parse(qemu_find_opts("tpmdev"), optarg) < 0) {
3366 exit(1);
3368 break;
3369 #endif
3370 case QEMU_OPTION_mempath:
3371 mem_path = optarg;
3372 break;
3373 case QEMU_OPTION_mem_prealloc:
3374 mem_prealloc = 1;
3375 break;
3376 case QEMU_OPTION_d:
3377 log_mask = optarg;
3378 break;
3379 case QEMU_OPTION_D:
3380 log_file = optarg;
3381 break;
3382 case QEMU_OPTION_s:
3383 add_device_config(DEV_GDB, "tcp::" DEFAULT_GDBSTUB_PORT);
3384 break;
3385 case QEMU_OPTION_gdb:
3386 add_device_config(DEV_GDB, optarg);
3387 break;
3388 case QEMU_OPTION_L:
3389 if (data_dir_idx < ARRAY_SIZE(data_dir)) {
3390 data_dir[data_dir_idx++] = optarg;
3392 break;
3393 case QEMU_OPTION_bios:
3394 qemu_opts_set(qemu_find_opts("machine"), 0, "firmware", optarg);
3395 break;
3396 case QEMU_OPTION_singlestep:
3397 singlestep = 1;
3398 break;
3399 case QEMU_OPTION_S:
3400 autostart = 0;
3401 break;
3402 case QEMU_OPTION_k:
3403 keyboard_layout = optarg;
3404 break;
3405 case QEMU_OPTION_localtime:
3406 rtc_utc = 0;
3407 break;
3408 case QEMU_OPTION_vga:
3409 vga_model = optarg;
3410 default_vga = 0;
3411 break;
3412 case QEMU_OPTION_g:
3414 const char *p;
3415 int w, h, depth;
3416 p = optarg;
3417 w = strtol(p, (char **)&p, 10);
3418 if (w <= 0) {
3419 graphic_error:
3420 fprintf(stderr, "qemu: invalid resolution or depth\n");
3421 exit(1);
3423 if (*p != 'x')
3424 goto graphic_error;
3425 p++;
3426 h = strtol(p, (char **)&p, 10);
3427 if (h <= 0)
3428 goto graphic_error;
3429 if (*p == 'x') {
3430 p++;
3431 depth = strtol(p, (char **)&p, 10);
3432 if (depth != 8 && depth != 15 && depth != 16 &&
3433 depth != 24 && depth != 32)
3434 goto graphic_error;
3435 } else if (*p == '\0') {
3436 depth = graphic_depth;
3437 } else {
3438 goto graphic_error;
3441 graphic_width = w;
3442 graphic_height = h;
3443 graphic_depth = depth;
3445 break;
3446 case QEMU_OPTION_echr:
3448 char *r;
3449 term_escape_char = strtol(optarg, &r, 0);
3450 if (r == optarg)
3451 printf("Bad argument to echr\n");
3452 break;
3454 case QEMU_OPTION_monitor:
3455 default_monitor = 0;
3456 if (strncmp(optarg, "none", 4)) {
3457 monitor_parse(optarg, "readline");
3459 break;
3460 case QEMU_OPTION_qmp:
3461 monitor_parse(optarg, "control");
3462 default_monitor = 0;
3463 break;
3464 case QEMU_OPTION_mon:
3465 opts = qemu_opts_parse(qemu_find_opts("mon"), optarg, 1);
3466 if (!opts) {
3467 exit(1);
3469 default_monitor = 0;
3470 break;
3471 case QEMU_OPTION_chardev:
3472 opts = qemu_opts_parse(qemu_find_opts("chardev"), optarg, 1);
3473 if (!opts) {
3474 exit(1);
3476 break;
3477 case QEMU_OPTION_fsdev:
3478 olist = qemu_find_opts("fsdev");
3479 if (!olist) {
3480 fprintf(stderr, "fsdev is not supported by this qemu build.\n");
3481 exit(1);
3483 opts = qemu_opts_parse(olist, optarg, 1);
3484 if (!opts) {
3485 exit(1);
3487 break;
3488 case QEMU_OPTION_virtfs: {
3489 QemuOpts *fsdev;
3490 QemuOpts *device;
3491 const char *writeout, *sock_fd, *socket;
3493 olist = qemu_find_opts("virtfs");
3494 if (!olist) {
3495 fprintf(stderr, "virtfs is not supported by this qemu build.\n");
3496 exit(1);
3498 opts = qemu_opts_parse(olist, optarg, 1);
3499 if (!opts) {
3500 exit(1);
3503 if (qemu_opt_get(opts, "fsdriver") == NULL ||
3504 qemu_opt_get(opts, "mount_tag") == NULL) {
3505 fprintf(stderr, "Usage: -virtfs fsdriver,mount_tag=tag.\n");
3506 exit(1);
3508 fsdev = qemu_opts_create(qemu_find_opts("fsdev"),
3509 qemu_opt_get(opts, "mount_tag"),
3510 1, NULL);
3511 if (!fsdev) {
3512 fprintf(stderr, "duplicate fsdev id: %s\n",
3513 qemu_opt_get(opts, "mount_tag"));
3514 exit(1);
3517 writeout = qemu_opt_get(opts, "writeout");
3518 if (writeout) {
3519 #ifdef CONFIG_SYNC_FILE_RANGE
3520 qemu_opt_set(fsdev, "writeout", writeout);
3521 #else
3522 fprintf(stderr, "writeout=immediate not supported on "
3523 "this platform\n");
3524 exit(1);
3525 #endif
3527 qemu_opt_set(fsdev, "fsdriver", qemu_opt_get(opts, "fsdriver"));
3528 qemu_opt_set(fsdev, "path", qemu_opt_get(opts, "path"));
3529 qemu_opt_set(fsdev, "security_model",
3530 qemu_opt_get(opts, "security_model"));
3531 socket = qemu_opt_get(opts, "socket");
3532 if (socket) {
3533 qemu_opt_set(fsdev, "socket", socket);
3535 sock_fd = qemu_opt_get(opts, "sock_fd");
3536 if (sock_fd) {
3537 qemu_opt_set(fsdev, "sock_fd", sock_fd);
3540 qemu_opt_set_bool(fsdev, "readonly",
3541 qemu_opt_get_bool(opts, "readonly", 0));
3542 device = qemu_opts_create(qemu_find_opts("device"), NULL, 0,
3543 &error_abort);
3544 qemu_opt_set(device, "driver", "virtio-9p-pci");
3545 qemu_opt_set(device, "fsdev",
3546 qemu_opt_get(opts, "mount_tag"));
3547 qemu_opt_set(device, "mount_tag",
3548 qemu_opt_get(opts, "mount_tag"));
3549 break;
3551 case QEMU_OPTION_virtfs_synth: {
3552 QemuOpts *fsdev;
3553 QemuOpts *device;
3555 fsdev = qemu_opts_create(qemu_find_opts("fsdev"), "v_synth",
3556 1, NULL);
3557 if (!fsdev) {
3558 fprintf(stderr, "duplicate option: %s\n", "virtfs_synth");
3559 exit(1);
3561 qemu_opt_set(fsdev, "fsdriver", "synth");
3563 device = qemu_opts_create(qemu_find_opts("device"), NULL, 0,
3564 &error_abort);
3565 qemu_opt_set(device, "driver", "virtio-9p-pci");
3566 qemu_opt_set(device, "fsdev", "v_synth");
3567 qemu_opt_set(device, "mount_tag", "v_synth");
3568 break;
3570 case QEMU_OPTION_serial:
3571 add_device_config(DEV_SERIAL, optarg);
3572 default_serial = 0;
3573 if (strncmp(optarg, "mon:", 4) == 0) {
3574 default_monitor = 0;
3576 break;
3577 case QEMU_OPTION_watchdog:
3578 if (watchdog) {
3579 fprintf(stderr,
3580 "qemu: only one watchdog option may be given\n");
3581 return 1;
3583 watchdog = optarg;
3584 break;
3585 case QEMU_OPTION_watchdog_action:
3586 if (select_watchdog_action(optarg) == -1) {
3587 fprintf(stderr, "Unknown -watchdog-action parameter\n");
3588 exit(1);
3590 break;
3591 case QEMU_OPTION_virtiocon:
3592 add_device_config(DEV_VIRTCON, optarg);
3593 default_virtcon = 0;
3594 if (strncmp(optarg, "mon:", 4) == 0) {
3595 default_monitor = 0;
3597 break;
3598 case QEMU_OPTION_parallel:
3599 add_device_config(DEV_PARALLEL, optarg);
3600 default_parallel = 0;
3601 if (strncmp(optarg, "mon:", 4) == 0) {
3602 default_monitor = 0;
3604 break;
3605 case QEMU_OPTION_debugcon:
3606 add_device_config(DEV_DEBUGCON, optarg);
3607 break;
3608 case QEMU_OPTION_loadvm:
3609 loadvm = optarg;
3610 break;
3611 case QEMU_OPTION_full_screen:
3612 full_screen = 1;
3613 break;
3614 case QEMU_OPTION_no_frame:
3615 no_frame = 1;
3616 break;
3617 case QEMU_OPTION_alt_grab:
3618 alt_grab = 1;
3619 break;
3620 case QEMU_OPTION_ctrl_grab:
3621 ctrl_grab = 1;
3622 break;
3623 case QEMU_OPTION_no_quit:
3624 no_quit = 1;
3625 break;
3626 case QEMU_OPTION_sdl:
3627 #ifdef CONFIG_SDL
3628 display_type = DT_SDL;
3629 break;
3630 #else
3631 fprintf(stderr, "SDL support is disabled\n");
3632 exit(1);
3633 #endif
3634 case QEMU_OPTION_pidfile:
3635 pid_file = optarg;
3636 break;
3637 case QEMU_OPTION_win2k_hack:
3638 win2k_install_hack = 1;
3639 break;
3640 case QEMU_OPTION_rtc_td_hack: {
3641 static GlobalProperty slew_lost_ticks[] = {
3643 .driver = "mc146818rtc",
3644 .property = "lost_tick_policy",
3645 .value = "slew",
3647 { /* end of list */ }
3650 qdev_prop_register_global_list(slew_lost_ticks);
3651 break;
3653 case QEMU_OPTION_acpitable:
3654 opts = qemu_opts_parse(qemu_find_opts("acpi"), optarg, 1);
3655 if (!opts) {
3656 exit(1);
3658 do_acpitable_option(opts);
3659 break;
3660 case QEMU_OPTION_smbios:
3661 opts = qemu_opts_parse(qemu_find_opts("smbios"), optarg, 0);
3662 if (!opts) {
3663 exit(1);
3665 do_smbios_option(opts);
3666 break;
3667 case QEMU_OPTION_enable_kvm:
3668 olist = qemu_find_opts("machine");
3669 qemu_opts_parse(olist, "accel=kvm", 0);
3670 break;
3671 case QEMU_OPTION_machine:
3672 olist = qemu_find_opts("machine");
3673 opts = qemu_opts_parse(olist, optarg, 1);
3674 if (!opts) {
3675 exit(1);
3677 optarg = qemu_opt_get(opts, "type");
3678 if (optarg) {
3679 machine_class = machine_parse(optarg);
3681 break;
3682 case QEMU_OPTION_no_kvm:
3683 olist = qemu_find_opts("machine");
3684 qemu_opts_parse(olist, "accel=tcg", 0);
3685 break;
3686 case QEMU_OPTION_no_kvm_pit: {
3687 fprintf(stderr, "Warning: KVM PIT can no longer be disabled "
3688 "separately.\n");
3689 break;
3691 case QEMU_OPTION_no_kvm_pit_reinjection: {
3692 static GlobalProperty kvm_pit_lost_tick_policy[] = {
3694 .driver = "kvm-pit",
3695 .property = "lost_tick_policy",
3696 .value = "discard",
3698 { /* end of list */ }
3701 fprintf(stderr, "Warning: option deprecated, use "
3702 "lost_tick_policy property of kvm-pit instead.\n");
3703 qdev_prop_register_global_list(kvm_pit_lost_tick_policy);
3704 break;
3706 case QEMU_OPTION_usb:
3707 olist = qemu_find_opts("machine");
3708 qemu_opts_parse(olist, "usb=on", 0);
3709 break;
3710 case QEMU_OPTION_usbdevice:
3711 olist = qemu_find_opts("machine");
3712 qemu_opts_parse(olist, "usb=on", 0);
3713 add_device_config(DEV_USB, optarg);
3714 break;
3715 case QEMU_OPTION_device:
3716 if (!qemu_opts_parse(qemu_find_opts("device"), optarg, 1)) {
3717 exit(1);
3719 break;
3720 case QEMU_OPTION_smp:
3721 if (!qemu_opts_parse(qemu_find_opts("smp-opts"), optarg, 1)) {
3722 exit(1);
3724 break;
3725 case QEMU_OPTION_vnc:
3726 #ifdef CONFIG_VNC
3727 display_remote++;
3728 vnc_display = optarg;
3729 #else
3730 fprintf(stderr, "VNC support is disabled\n");
3731 exit(1);
3732 #endif
3733 break;
3734 case QEMU_OPTION_no_acpi:
3735 acpi_enabled = 0;
3736 break;
3737 case QEMU_OPTION_no_hpet:
3738 no_hpet = 1;
3739 break;
3740 case QEMU_OPTION_balloon:
3741 if (balloon_parse(optarg) < 0) {
3742 fprintf(stderr, "Unknown -balloon argument %s\n", optarg);
3743 exit(1);
3745 break;
3746 case QEMU_OPTION_no_reboot:
3747 no_reboot = 1;
3748 break;
3749 case QEMU_OPTION_no_shutdown:
3750 no_shutdown = 1;
3751 break;
3752 case QEMU_OPTION_show_cursor:
3753 cursor_hide = 0;
3754 break;
3755 case QEMU_OPTION_uuid:
3756 if(qemu_uuid_parse(optarg, qemu_uuid) < 0) {
3757 fprintf(stderr, "Fail to parse UUID string."
3758 " Wrong format.\n");
3759 exit(1);
3761 qemu_uuid_set = true;
3762 break;
3763 case QEMU_OPTION_option_rom:
3764 if (nb_option_roms >= MAX_OPTION_ROMS) {
3765 fprintf(stderr, "Too many option ROMs\n");
3766 exit(1);
3768 opts = qemu_opts_parse(qemu_find_opts("option-rom"), optarg, 1);
3769 if (!opts) {
3770 exit(1);
3772 option_rom[nb_option_roms].name = qemu_opt_get(opts, "romfile");
3773 option_rom[nb_option_roms].bootindex =
3774 qemu_opt_get_number(opts, "bootindex", -1);
3775 if (!option_rom[nb_option_roms].name) {
3776 fprintf(stderr, "Option ROM file is not specified\n");
3777 exit(1);
3779 nb_option_roms++;
3780 break;
3781 case QEMU_OPTION_semihosting:
3782 semihosting_enabled = 1;
3783 break;
3784 case QEMU_OPTION_tdf:
3785 fprintf(stderr, "Warning: user space PIT time drift fix "
3786 "is no longer supported.\n");
3787 break;
3788 case QEMU_OPTION_name:
3789 opts = qemu_opts_parse(qemu_find_opts("name"), optarg, 1);
3790 if (!opts) {
3791 exit(1);
3793 break;
3794 case QEMU_OPTION_prom_env:
3795 if (nb_prom_envs >= MAX_PROM_ENVS) {
3796 fprintf(stderr, "Too many prom variables\n");
3797 exit(1);
3799 prom_envs[nb_prom_envs] = optarg;
3800 nb_prom_envs++;
3801 break;
3802 case QEMU_OPTION_old_param:
3803 old_param = 1;
3804 break;
3805 case QEMU_OPTION_clock:
3806 /* Clock options no longer exist. Keep this option for
3807 * backward compatibility.
3809 break;
3810 case QEMU_OPTION_startdate:
3811 configure_rtc_date_offset(optarg, 1);
3812 break;
3813 case QEMU_OPTION_rtc:
3814 opts = qemu_opts_parse(qemu_find_opts("rtc"), optarg, 0);
3815 if (!opts) {
3816 exit(1);
3818 configure_rtc(opts);
3819 break;
3820 case QEMU_OPTION_tb_size:
3821 tcg_tb_size = strtol(optarg, NULL, 0);
3822 if (tcg_tb_size < 0) {
3823 tcg_tb_size = 0;
3825 break;
3826 case QEMU_OPTION_icount:
3827 icount_option = optarg;
3828 break;
3829 case QEMU_OPTION_incoming:
3830 incoming = optarg;
3831 runstate_set(RUN_STATE_INMIGRATE);
3832 break;
3833 case QEMU_OPTION_nodefaults:
3834 has_defaults = 0;
3835 break;
3836 case QEMU_OPTION_xen_domid:
3837 if (!(xen_available())) {
3838 printf("Option %s not supported for this target\n", popt->name);
3839 exit(1);
3841 xen_domid = atoi(optarg);
3842 break;
3843 case QEMU_OPTION_xen_create:
3844 if (!(xen_available())) {
3845 printf("Option %s not supported for this target\n", popt->name);
3846 exit(1);
3848 xen_mode = XEN_CREATE;
3849 break;
3850 case QEMU_OPTION_xen_attach:
3851 if (!(xen_available())) {
3852 printf("Option %s not supported for this target\n", popt->name);
3853 exit(1);
3855 xen_mode = XEN_ATTACH;
3856 break;
3857 case QEMU_OPTION_trace:
3859 opts = qemu_opts_parse(qemu_find_opts("trace"), optarg, 0);
3860 if (!opts) {
3861 exit(1);
3863 trace_events = qemu_opt_get(opts, "events");
3864 trace_file = qemu_opt_get(opts, "file");
3865 break;
3867 case QEMU_OPTION_readconfig:
3869 int ret = qemu_read_config_file(optarg);
3870 if (ret < 0) {
3871 fprintf(stderr, "read config %s: %s\n", optarg,
3872 strerror(-ret));
3873 exit(1);
3875 break;
3877 case QEMU_OPTION_spice:
3878 olist = qemu_find_opts("spice");
3879 if (!olist) {
3880 fprintf(stderr, "spice is not supported by this qemu build.\n");
3881 exit(1);
3883 opts = qemu_opts_parse(olist, optarg, 0);
3884 if (!opts) {
3885 exit(1);
3887 display_remote++;
3888 break;
3889 case QEMU_OPTION_writeconfig:
3891 FILE *fp;
3892 if (strcmp(optarg, "-") == 0) {
3893 fp = stdout;
3894 } else {
3895 fp = fopen(optarg, "w");
3896 if (fp == NULL) {
3897 fprintf(stderr, "open %s: %s\n", optarg, strerror(errno));
3898 exit(1);
3901 qemu_config_write(fp);
3902 if (fp != stdout) {
3903 fclose(fp);
3905 break;
3907 case QEMU_OPTION_qtest:
3908 qtest_chrdev = optarg;
3909 break;
3910 case QEMU_OPTION_qtest_log:
3911 qtest_log = optarg;
3912 break;
3913 case QEMU_OPTION_sandbox:
3914 opts = qemu_opts_parse(qemu_find_opts("sandbox"), optarg, 1);
3915 if (!opts) {
3916 exit(1);
3918 break;
3919 case QEMU_OPTION_add_fd:
3920 #ifndef _WIN32
3921 opts = qemu_opts_parse(qemu_find_opts("add-fd"), optarg, 0);
3922 if (!opts) {
3923 exit(1);
3925 #else
3926 error_report("File descriptor passing is disabled on this "
3927 "platform");
3928 exit(1);
3929 #endif
3930 break;
3931 case QEMU_OPTION_object:
3932 opts = qemu_opts_parse(qemu_find_opts("object"), optarg, 1);
3933 if (!opts) {
3934 exit(1);
3936 break;
3937 case QEMU_OPTION_realtime:
3938 opts = qemu_opts_parse(qemu_find_opts("realtime"), optarg, 0);
3939 if (!opts) {
3940 exit(1);
3942 configure_realtime(opts);
3943 break;
3944 case QEMU_OPTION_msg:
3945 opts = qemu_opts_parse(qemu_find_opts("msg"), optarg, 0);
3946 if (!opts) {
3947 exit(1);
3949 configure_msg(opts);
3950 break;
3951 default:
3952 os_parse_cmd_args(popt->index, optarg);
3956 loc_set_none();
3958 os_daemonize();
3960 if (qemu_init_main_loop()) {
3961 fprintf(stderr, "qemu_init_main_loop failed\n");
3962 exit(1);
3965 if (qemu_opts_foreach(qemu_find_opts("sandbox"), parse_sandbox, NULL, 0)) {
3966 exit(1);
3969 if (qemu_opts_foreach(qemu_find_opts("name"), parse_name, NULL, 1)) {
3970 exit(1);
3973 #ifndef _WIN32
3974 if (qemu_opts_foreach(qemu_find_opts("add-fd"), parse_add_fd, NULL, 1)) {
3975 exit(1);
3978 if (qemu_opts_foreach(qemu_find_opts("add-fd"), cleanup_add_fd, NULL, 1)) {
3979 exit(1);
3981 #endif
3983 if (machine_class == NULL) {
3984 fprintf(stderr, "No machine specified, and there is no default.\n"
3985 "Use -machine help to list supported machines!\n");
3986 exit(1);
3989 cpu_exec_init_all();
3991 current_machine = MACHINE(object_new(object_class_get_name(
3992 OBJECT_CLASS(machine_class))));
3993 object_property_add_child(object_get_root(), "machine",
3994 OBJECT(current_machine), &error_abort);
3996 if (machine_class->hw_version) {
3997 qemu_set_version(machine_class->hw_version);
4000 if (qemu_opts_foreach(qemu_find_opts("object"),
4001 object_create, NULL, 0) != 0) {
4002 exit(1);
4005 /* Init CPU def lists, based on config
4006 * - Must be called after all the qemu_read_config_file() calls
4007 * - Must be called before list_cpus()
4008 * - Must be called before machine->init()
4010 cpudef_init();
4012 if (cpu_model && is_help_option(cpu_model)) {
4013 list_cpus(stdout, &fprintf, cpu_model);
4014 exit(0);
4017 /* Open the logfile at this point, if necessary. We can't open the logfile
4018 * when encountering either of the logging options (-d or -D) because the
4019 * other one may be encountered later on the command line, changing the
4020 * location or level of logging.
4022 if (log_mask) {
4023 int mask;
4024 if (log_file) {
4025 qemu_set_log_filename(log_file);
4028 mask = qemu_str_to_log_mask(log_mask);
4029 if (!mask) {
4030 qemu_print_log_usage(stdout);
4031 exit(1);
4033 qemu_set_log(mask);
4036 if (!is_daemonized()) {
4037 if (!trace_init_backends(trace_events, trace_file)) {
4038 exit(1);
4042 /* If no data_dir is specified then try to find it relative to the
4043 executable path. */
4044 if (data_dir_idx < ARRAY_SIZE(data_dir)) {
4045 data_dir[data_dir_idx] = os_find_datadir();
4046 if (data_dir[data_dir_idx] != NULL) {
4047 data_dir_idx++;
4050 /* If all else fails use the install path specified when building. */
4051 if (data_dir_idx < ARRAY_SIZE(data_dir)) {
4052 data_dir[data_dir_idx++] = CONFIG_QEMU_DATADIR;
4055 smp_parse(qemu_opts_find(qemu_find_opts("smp-opts"), NULL));
4057 machine_class->max_cpus = machine_class->max_cpus ?: 1; /* Default to UP */
4058 if (smp_cpus > machine_class->max_cpus) {
4059 fprintf(stderr, "Number of SMP cpus requested (%d), exceeds max cpus "
4060 "supported by machine `%s' (%d)\n", smp_cpus,
4061 machine_class->name, machine_class->max_cpus);
4062 exit(1);
4066 * Get the default machine options from the machine if it is not already
4067 * specified either by the configuration file or by the command line.
4069 if (machine_class->default_machine_opts) {
4070 qemu_opts_set_defaults(qemu_find_opts("machine"),
4071 machine_class->default_machine_opts, 0);
4074 qemu_opts_foreach(qemu_find_opts("device"), default_driver_check, NULL, 0);
4075 qemu_opts_foreach(qemu_find_opts("global"), default_driver_check, NULL, 0);
4077 if (!vga_model && !default_vga) {
4078 vga_interface_type = VGA_DEVICE;
4080 if (!has_defaults || machine_class->no_serial) {
4081 default_serial = 0;
4083 if (!has_defaults || machine_class->no_parallel) {
4084 default_parallel = 0;
4086 if (!has_defaults || !machine_class->use_virtcon) {
4087 default_virtcon = 0;
4089 if (!has_defaults || !machine_class->use_sclp) {
4090 default_sclp = 0;
4092 if (!has_defaults || machine_class->no_floppy) {
4093 default_floppy = 0;
4095 if (!has_defaults || machine_class->no_cdrom) {
4096 default_cdrom = 0;
4098 if (!has_defaults || machine_class->no_sdcard) {
4099 default_sdcard = 0;
4101 if (!has_defaults) {
4102 default_monitor = 0;
4103 default_net = 0;
4104 default_vga = 0;
4107 if (is_daemonized()) {
4108 /* According to documentation and historically, -nographic redirects
4109 * serial port, parallel port and monitor to stdio, which does not work
4110 * with -daemonize. We can redirect these to null instead, but since
4111 * -nographic is legacy, let's just error out.
4112 * We disallow -nographic only if all other ports are not redirected
4113 * explicitly, to not break existing legacy setups which uses
4114 * -nographic _and_ redirects all ports explicitly - this is valid
4115 * usage, -nographic is just a no-op in this case.
4117 if (display_type == DT_NOGRAPHIC
4118 && (default_parallel || default_serial
4119 || default_monitor || default_virtcon)) {
4120 fprintf(stderr, "-nographic can not be used with -daemonize\n");
4121 exit(1);
4123 #ifdef CONFIG_CURSES
4124 if (display_type == DT_CURSES) {
4125 fprintf(stderr, "curses display can not be used with -daemonize\n");
4126 exit(1);
4128 #endif
4131 if (display_type == DT_NOGRAPHIC) {
4132 if (default_parallel)
4133 add_device_config(DEV_PARALLEL, "null");
4134 if (default_serial && default_monitor) {
4135 add_device_config(DEV_SERIAL, "mon:stdio");
4136 } else if (default_virtcon && default_monitor) {
4137 add_device_config(DEV_VIRTCON, "mon:stdio");
4138 } else if (default_sclp && default_monitor) {
4139 add_device_config(DEV_SCLP, "mon:stdio");
4140 } else {
4141 if (default_serial)
4142 add_device_config(DEV_SERIAL, "stdio");
4143 if (default_virtcon)
4144 add_device_config(DEV_VIRTCON, "stdio");
4145 if (default_sclp) {
4146 add_device_config(DEV_SCLP, "stdio");
4148 if (default_monitor)
4149 monitor_parse("stdio", "readline");
4151 } else {
4152 if (default_serial)
4153 add_device_config(DEV_SERIAL, "vc:80Cx24C");
4154 if (default_parallel)
4155 add_device_config(DEV_PARALLEL, "vc:80Cx24C");
4156 if (default_monitor)
4157 monitor_parse("vc:80Cx24C", "readline");
4158 if (default_virtcon)
4159 add_device_config(DEV_VIRTCON, "vc:80Cx24C");
4160 if (default_sclp) {
4161 add_device_config(DEV_SCLP, "vc:80Cx24C");
4165 if (display_type == DT_DEFAULT && !display_remote) {
4166 #if defined(CONFIG_GTK)
4167 display_type = DT_GTK;
4168 #elif defined(CONFIG_SDL) || defined(CONFIG_COCOA)
4169 display_type = DT_SDL;
4170 #elif defined(CONFIG_VNC)
4171 vnc_display = "localhost:0,to=99";
4172 show_vnc_port = 1;
4173 #else
4174 display_type = DT_NONE;
4175 #endif
4178 if ((no_frame || alt_grab || ctrl_grab) && display_type != DT_SDL) {
4179 fprintf(stderr, "-no-frame, -alt-grab and -ctrl-grab are only valid "
4180 "for SDL, ignoring option\n");
4182 if (no_quit && (display_type != DT_GTK && display_type != DT_SDL)) {
4183 fprintf(stderr, "-no-quit is only valid for GTK and SDL, "
4184 "ignoring option\n");
4187 #if defined(CONFIG_GTK)
4188 if (display_type == DT_GTK) {
4189 early_gtk_display_init();
4191 #endif
4193 socket_init();
4195 if (qemu_opts_foreach(qemu_find_opts("chardev"), chardev_init_func, NULL, 1) != 0)
4196 exit(1);
4197 #ifdef CONFIG_VIRTFS
4198 if (qemu_opts_foreach(qemu_find_opts("fsdev"), fsdev_init_func, NULL, 1) != 0) {
4199 exit(1);
4201 #endif
4203 if (pid_file && qemu_create_pidfile(pid_file) != 0) {
4204 os_pidfile_error();
4205 exit(1);
4208 /* store value for the future use */
4209 qemu_opt_set_number(qemu_find_opts_singleton("memory"), "size", ram_size);
4211 if (qemu_opts_foreach(qemu_find_opts("device"), device_help_func, NULL, 0)
4212 != 0) {
4213 exit(0);
4216 machine_opts = qemu_get_machine_opts();
4217 if (qemu_opt_foreach(machine_opts, object_set_property, current_machine,
4218 1) < 0) {
4219 object_unref(OBJECT(current_machine));
4220 exit(1);
4223 configure_accelerator(machine_class);
4225 if (qtest_chrdev) {
4226 Error *local_err = NULL;
4227 qtest_init(qtest_chrdev, qtest_log, &local_err);
4228 if (local_err) {
4229 error_report("%s", error_get_pretty(local_err));
4230 error_free(local_err);
4231 exit(1);
4235 machine_opts = qemu_get_machine_opts();
4236 kernel_filename = qemu_opt_get(machine_opts, "kernel");
4237 initrd_filename = qemu_opt_get(machine_opts, "initrd");
4238 kernel_cmdline = qemu_opt_get(machine_opts, "append");
4239 bios_name = qemu_opt_get(machine_opts, "firmware");
4241 boot_order = machine_class->default_boot_order;
4242 opts = qemu_opts_find(qemu_find_opts("boot-opts"), NULL);
4243 if (opts) {
4244 char *normal_boot_order;
4245 const char *order, *once;
4247 order = qemu_opt_get(opts, "order");
4248 if (order) {
4249 validate_bootdevices(order);
4250 boot_order = order;
4253 once = qemu_opt_get(opts, "once");
4254 if (once) {
4255 validate_bootdevices(once);
4256 normal_boot_order = g_strdup(boot_order);
4257 boot_order = once;
4258 qemu_register_reset(restore_boot_order, normal_boot_order);
4261 boot_menu = qemu_opt_get_bool(opts, "menu", boot_menu);
4262 boot_strict = qemu_opt_get_bool(opts, "strict", false);
4265 if (!kernel_cmdline) {
4266 kernel_cmdline = "";
4267 current_machine->kernel_cmdline = (char *)kernel_cmdline;
4270 linux_boot = (kernel_filename != NULL);
4272 if (!linux_boot && *kernel_cmdline != '\0') {
4273 fprintf(stderr, "-append only allowed with -kernel option\n");
4274 exit(1);
4277 if (!linux_boot && initrd_filename != NULL) {
4278 fprintf(stderr, "-initrd only allowed with -kernel option\n");
4279 exit(1);
4282 if (!linux_boot && qemu_opt_get(machine_opts, "dtb")) {
4283 fprintf(stderr, "-dtb only allowed with -kernel option\n");
4284 exit(1);
4287 os_set_line_buffering();
4289 qemu_init_cpu_loop();
4290 qemu_mutex_lock_iothread();
4292 #ifdef CONFIG_SPICE
4293 /* spice needs the timers to be initialized by this point */
4294 qemu_spice_init();
4295 #endif
4297 if (icount_option && (kvm_enabled() || xen_enabled())) {
4298 fprintf(stderr, "-icount is not allowed with kvm or xen\n");
4299 exit(1);
4301 configure_icount(icount_option);
4303 /* clean up network at qemu process termination */
4304 atexit(&net_cleanup);
4306 if (net_init_clients() < 0) {
4307 exit(1);
4310 #ifdef CONFIG_TPM
4311 if (tpm_init() < 0) {
4312 exit(1);
4314 #endif
4316 /* init the bluetooth world */
4317 if (foreach_device_config(DEV_BT, bt_parse))
4318 exit(1);
4320 if (!xen_enabled()) {
4321 /* On 32-bit hosts, QEMU is limited by virtual address space */
4322 if (ram_size > (2047 << 20) && HOST_LONG_BITS == 32) {
4323 fprintf(stderr, "qemu: at most 2047 MB RAM can be simulated\n");
4324 exit(1);
4328 blk_mig_init();
4329 ram_mig_init();
4331 /* open the virtual block devices */
4332 if (snapshot)
4333 qemu_opts_foreach(qemu_find_opts("drive"), drive_enable_snapshot, NULL, 0);
4334 if (qemu_opts_foreach(qemu_find_opts("drive"), drive_init_func,
4335 &machine_class->block_default_type, 1) != 0) {
4336 exit(1);
4339 default_drive(default_cdrom, snapshot, machine_class->block_default_type, 2,
4340 CDROM_OPTS);
4341 default_drive(default_floppy, snapshot, IF_FLOPPY, 0, FD_OPTS);
4342 default_drive(default_sdcard, snapshot, IF_SD, 0, SD_OPTS);
4344 if (qemu_opts_foreach(qemu_find_opts("numa"), numa_init_func,
4345 NULL, 1) != 0) {
4346 exit(1);
4349 set_numa_nodes();
4351 if (qemu_opts_foreach(qemu_find_opts("mon"), mon_init_func, NULL, 1) != 0) {
4352 exit(1);
4355 if (foreach_device_config(DEV_SERIAL, serial_parse) < 0)
4356 exit(1);
4357 if (foreach_device_config(DEV_PARALLEL, parallel_parse) < 0)
4358 exit(1);
4359 if (foreach_device_config(DEV_VIRTCON, virtcon_parse) < 0)
4360 exit(1);
4361 if (foreach_device_config(DEV_SCLP, sclp_parse) < 0) {
4362 exit(1);
4364 if (foreach_device_config(DEV_DEBUGCON, debugcon_parse) < 0)
4365 exit(1);
4367 /* If no default VGA is requested, the default is "none". */
4368 if (default_vga) {
4369 if (cirrus_vga_available()) {
4370 vga_model = "cirrus";
4371 } else if (vga_available()) {
4372 vga_model = "std";
4375 if (vga_model) {
4376 select_vgahw(vga_model);
4379 if (watchdog) {
4380 i = select_watchdog(watchdog);
4381 if (i > 0)
4382 exit (i == 1 ? 1 : 0);
4385 if (machine_class->compat_props) {
4386 qdev_prop_register_global_list(machine_class->compat_props);
4388 qemu_add_globals();
4390 qdev_machine_init();
4392 current_machine->ram_size = ram_size;
4393 current_machine->maxram_size = maxram_size;
4394 current_machine->ram_slots = ram_slots;
4395 current_machine->boot_order = boot_order;
4396 current_machine->cpu_model = cpu_model;
4398 machine_class->init(current_machine);
4400 audio_init();
4402 cpu_synchronize_all_post_init();
4404 set_numa_modes();
4406 /* init USB devices */
4407 if (usb_enabled(false)) {
4408 if (foreach_device_config(DEV_USB, usb_parse) < 0)
4409 exit(1);
4412 /* init generic devices */
4413 if (qemu_opts_foreach(qemu_find_opts("device"), device_init_func, NULL, 1) != 0)
4414 exit(1);
4416 net_check_clients();
4418 ds = init_displaystate();
4420 /* init local displays */
4421 switch (display_type) {
4422 case DT_NOGRAPHIC:
4423 (void)ds; /* avoid warning if no display is configured */
4424 break;
4425 #if defined(CONFIG_CURSES)
4426 case DT_CURSES:
4427 curses_display_init(ds, full_screen);
4428 break;
4429 #endif
4430 #if defined(CONFIG_SDL)
4431 case DT_SDL:
4432 sdl_display_init(ds, full_screen, no_frame);
4433 break;
4434 #elif defined(CONFIG_COCOA)
4435 case DT_SDL:
4436 cocoa_display_init(ds, full_screen);
4437 break;
4438 #endif
4439 #if defined(CONFIG_GTK)
4440 case DT_GTK:
4441 gtk_display_init(ds, full_screen, grab_on_hover);
4442 break;
4443 #endif
4444 default:
4445 break;
4448 /* must be after terminal init, SDL library changes signal handlers */
4449 os_setup_signal_handling();
4451 #ifdef CONFIG_VNC
4452 /* init remote displays */
4453 if (vnc_display) {
4454 Error *local_err = NULL;
4455 vnc_display_init(ds);
4456 vnc_display_open(ds, vnc_display, &local_err);
4457 if (local_err != NULL) {
4458 error_report("Failed to start VNC server on `%s': %s",
4459 vnc_display, error_get_pretty(local_err));
4460 error_free(local_err);
4461 exit(1);
4464 if (show_vnc_port) {
4465 printf("VNC server running on `%s'\n", vnc_display_local_addr(ds));
4468 #endif
4469 #ifdef CONFIG_SPICE
4470 if (using_spice) {
4471 qemu_spice_display_init();
4473 #endif
4475 if (foreach_device_config(DEV_GDB, gdbserver_start) < 0) {
4476 exit(1);
4479 qdev_machine_creation_done();
4481 if (rom_load_all() != 0) {
4482 fprintf(stderr, "rom loading failed\n");
4483 exit(1);
4486 /* TODO: once all bus devices are qdevified, this should be done
4487 * when bus is created by qdev.c */
4488 qemu_register_reset(qbus_reset_all_fn, sysbus_get_default());
4489 qemu_run_machine_init_done_notifiers();
4491 /* Done notifiers can load ROMs */
4492 rom_load_done();
4494 qemu_system_reset(VMRESET_SILENT);
4495 if (loadvm) {
4496 if (load_vmstate(loadvm) < 0) {
4497 autostart = 0;
4501 qdev_prop_check_global();
4503 if (incoming) {
4504 Error *local_err = NULL;
4505 qemu_start_incoming_migration(incoming, &local_err);
4506 if (local_err) {
4507 error_report("-incoming %s: %s", incoming,
4508 error_get_pretty(local_err));
4509 error_free(local_err);
4510 exit(1);
4512 } else if (autostart) {
4513 vm_start();
4516 os_setup_post();
4518 if (is_daemonized()) {
4519 if (!trace_init_backends(trace_events, trace_file)) {
4520 exit(1);
4524 main_loop();
4525 bdrv_close_all();
4526 pause_all_vcpus();
4527 res_free();
4528 #ifdef CONFIG_TPM
4529 tpm_cleanup();
4530 #endif
4532 return 0;