Add vhost-user protocol documentation
[qemu/ar7.git] / vl.c
blob4c6d6df26c443433edaf0dc7b835ccbd3ac4e2e6
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 "qom/object_interfaces.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 uint64_t node_mem[MAX_NODES];
199 unsigned long *node_cpumask[MAX_NODES];
201 uint8_t qemu_uuid[16];
202 bool qemu_uuid_set;
204 static QEMUBootSetHandler *boot_set_handler;
205 static void *boot_set_opaque;
207 static NotifierList exit_notifiers =
208 NOTIFIER_LIST_INITIALIZER(exit_notifiers);
210 static NotifierList machine_init_done_notifiers =
211 NOTIFIER_LIST_INITIALIZER(machine_init_done_notifiers);
213 static bool tcg_allowed = true;
214 bool xen_allowed;
215 uint32_t xen_domid;
216 enum xen_mode xen_mode = XEN_EMULATE;
217 static int tcg_tb_size;
219 static int has_defaults = 1;
220 static int default_serial = 1;
221 static int default_parallel = 1;
222 static int default_virtcon = 1;
223 static int default_sclp = 1;
224 static int default_monitor = 1;
225 static int default_floppy = 1;
226 static int default_cdrom = 1;
227 static int default_sdcard = 1;
228 static int default_vga = 1;
230 static struct {
231 const char *driver;
232 int *flag;
233 } default_list[] = {
234 { .driver = "isa-serial", .flag = &default_serial },
235 { .driver = "isa-parallel", .flag = &default_parallel },
236 { .driver = "isa-fdc", .flag = &default_floppy },
237 { .driver = "ide-cd", .flag = &default_cdrom },
238 { .driver = "ide-hd", .flag = &default_cdrom },
239 { .driver = "ide-drive", .flag = &default_cdrom },
240 { .driver = "scsi-cd", .flag = &default_cdrom },
241 { .driver = "virtio-serial-pci", .flag = &default_virtcon },
242 { .driver = "virtio-serial-s390", .flag = &default_virtcon },
243 { .driver = "virtio-serial", .flag = &default_virtcon },
244 { .driver = "VGA", .flag = &default_vga },
245 { .driver = "isa-vga", .flag = &default_vga },
246 { .driver = "cirrus-vga", .flag = &default_vga },
247 { .driver = "isa-cirrus-vga", .flag = &default_vga },
248 { .driver = "vmware-svga", .flag = &default_vga },
249 { .driver = "qxl-vga", .flag = &default_vga },
252 static QemuOptsList qemu_rtc_opts = {
253 .name = "rtc",
254 .head = QTAILQ_HEAD_INITIALIZER(qemu_rtc_opts.head),
255 .desc = {
257 .name = "base",
258 .type = QEMU_OPT_STRING,
260 .name = "clock",
261 .type = QEMU_OPT_STRING,
263 .name = "driftfix",
264 .type = QEMU_OPT_STRING,
266 { /* end of list */ }
270 static QemuOptsList qemu_sandbox_opts = {
271 .name = "sandbox",
272 .implied_opt_name = "enable",
273 .head = QTAILQ_HEAD_INITIALIZER(qemu_sandbox_opts.head),
274 .desc = {
276 .name = "enable",
277 .type = QEMU_OPT_BOOL,
279 { /* end of list */ }
283 static QemuOptsList qemu_trace_opts = {
284 .name = "trace",
285 .implied_opt_name = "trace",
286 .head = QTAILQ_HEAD_INITIALIZER(qemu_trace_opts.head),
287 .desc = {
289 .name = "events",
290 .type = QEMU_OPT_STRING,
292 .name = "file",
293 .type = QEMU_OPT_STRING,
295 { /* end of list */ }
299 static QemuOptsList qemu_option_rom_opts = {
300 .name = "option-rom",
301 .implied_opt_name = "romfile",
302 .head = QTAILQ_HEAD_INITIALIZER(qemu_option_rom_opts.head),
303 .desc = {
305 .name = "bootindex",
306 .type = QEMU_OPT_NUMBER,
307 }, {
308 .name = "romfile",
309 .type = QEMU_OPT_STRING,
311 { /* end of list */ }
315 static QemuOptsList qemu_machine_opts = {
316 .name = "machine",
317 .implied_opt_name = "type",
318 .merge_lists = true,
319 .head = QTAILQ_HEAD_INITIALIZER(qemu_machine_opts.head),
320 .desc = {
322 .name = "type",
323 .type = QEMU_OPT_STRING,
324 .help = "emulated machine"
325 }, {
326 .name = "accel",
327 .type = QEMU_OPT_STRING,
328 .help = "accelerator list",
329 }, {
330 .name = "kernel_irqchip",
331 .type = QEMU_OPT_BOOL,
332 .help = "use KVM in-kernel irqchip",
333 }, {
334 .name = "kvm_shadow_mem",
335 .type = QEMU_OPT_SIZE,
336 .help = "KVM shadow MMU size",
337 }, {
338 .name = "kernel",
339 .type = QEMU_OPT_STRING,
340 .help = "Linux kernel image file",
341 }, {
342 .name = "initrd",
343 .type = QEMU_OPT_STRING,
344 .help = "Linux initial ramdisk file",
345 }, {
346 .name = "append",
347 .type = QEMU_OPT_STRING,
348 .help = "Linux kernel command line",
349 }, {
350 .name = "dtb",
351 .type = QEMU_OPT_STRING,
352 .help = "Linux kernel device tree file",
353 }, {
354 .name = "dumpdtb",
355 .type = QEMU_OPT_STRING,
356 .help = "Dump current dtb to a file and quit",
357 }, {
358 .name = "phandle_start",
359 .type = QEMU_OPT_NUMBER,
360 .help = "The first phandle ID we may generate dynamically",
361 }, {
362 .name = "dt_compatible",
363 .type = QEMU_OPT_STRING,
364 .help = "Overrides the \"compatible\" property of the dt root node",
365 }, {
366 .name = "dump-guest-core",
367 .type = QEMU_OPT_BOOL,
368 .help = "Include guest memory in a core dump",
369 }, {
370 .name = "mem-merge",
371 .type = QEMU_OPT_BOOL,
372 .help = "enable/disable memory merge support",
374 .name = "usb",
375 .type = QEMU_OPT_BOOL,
376 .help = "Set on/off to enable/disable usb",
378 .name = "firmware",
379 .type = QEMU_OPT_STRING,
380 .help = "firmware image",
382 .name = "kvm-type",
383 .type = QEMU_OPT_STRING,
384 .help = "Specifies the KVM virtualization mode (HV, PR)",
386 { /* End of list */ }
390 static QemuOptsList qemu_boot_opts = {
391 .name = "boot-opts",
392 .implied_opt_name = "order",
393 .merge_lists = true,
394 .head = QTAILQ_HEAD_INITIALIZER(qemu_boot_opts.head),
395 .desc = {
397 .name = "order",
398 .type = QEMU_OPT_STRING,
399 }, {
400 .name = "once",
401 .type = QEMU_OPT_STRING,
402 }, {
403 .name = "menu",
404 .type = QEMU_OPT_BOOL,
405 }, {
406 .name = "splash",
407 .type = QEMU_OPT_STRING,
408 }, {
409 .name = "splash-time",
410 .type = QEMU_OPT_STRING,
411 }, {
412 .name = "reboot-timeout",
413 .type = QEMU_OPT_STRING,
414 }, {
415 .name = "strict",
416 .type = QEMU_OPT_BOOL,
418 { /*End of list */ }
422 static QemuOptsList qemu_add_fd_opts = {
423 .name = "add-fd",
424 .head = QTAILQ_HEAD_INITIALIZER(qemu_add_fd_opts.head),
425 .desc = {
427 .name = "fd",
428 .type = QEMU_OPT_NUMBER,
429 .help = "file descriptor of which a duplicate is added to fd set",
431 .name = "set",
432 .type = QEMU_OPT_NUMBER,
433 .help = "ID of the fd set to add fd to",
435 .name = "opaque",
436 .type = QEMU_OPT_STRING,
437 .help = "free-form string used to describe fd",
439 { /* end of list */ }
443 static QemuOptsList qemu_object_opts = {
444 .name = "object",
445 .implied_opt_name = "qom-type",
446 .head = QTAILQ_HEAD_INITIALIZER(qemu_object_opts.head),
447 .desc = {
452 static QemuOptsList qemu_tpmdev_opts = {
453 .name = "tpmdev",
454 .implied_opt_name = "type",
455 .head = QTAILQ_HEAD_INITIALIZER(qemu_tpmdev_opts.head),
456 .desc = {
457 /* options are defined in the TPM backends */
458 { /* end of list */ }
462 static QemuOptsList qemu_realtime_opts = {
463 .name = "realtime",
464 .head = QTAILQ_HEAD_INITIALIZER(qemu_realtime_opts.head),
465 .desc = {
467 .name = "mlock",
468 .type = QEMU_OPT_BOOL,
470 { /* end of list */ }
474 static QemuOptsList qemu_msg_opts = {
475 .name = "msg",
476 .head = QTAILQ_HEAD_INITIALIZER(qemu_msg_opts.head),
477 .desc = {
479 .name = "timestamp",
480 .type = QEMU_OPT_BOOL,
482 { /* end of list */ }
486 static QemuOptsList qemu_name_opts = {
487 .name = "name",
488 .implied_opt_name = "guest",
489 .merge_lists = true,
490 .head = QTAILQ_HEAD_INITIALIZER(qemu_name_opts.head),
491 .desc = {
493 .name = "guest",
494 .type = QEMU_OPT_STRING,
495 .help = "Sets the name of the guest.\n"
496 "This name will be displayed in the SDL window caption.\n"
497 "The name will also be used for the VNC server",
498 }, {
499 .name = "process",
500 .type = QEMU_OPT_STRING,
501 .help = "Sets the name of the QEMU process, as shown in top etc",
502 }, {
503 .name = "debug-threads",
504 .type = QEMU_OPT_BOOL,
505 .help = "When enabled, name the individual threads; defaults off.\n"
506 "NOTE: The thread names are for debugging and not a\n"
507 "stable API.",
509 { /* End of list */ }
513 static QemuOptsList qemu_mem_opts = {
514 .name = "memory",
515 .implied_opt_name = "size",
516 .head = QTAILQ_HEAD_INITIALIZER(qemu_mem_opts.head),
517 .merge_lists = true,
518 .desc = {
520 .name = "size",
521 .type = QEMU_OPT_SIZE,
524 .name = "slots",
525 .type = QEMU_OPT_NUMBER,
528 .name = "maxmem",
529 .type = QEMU_OPT_SIZE,
531 { /* end of list */ }
536 * Get machine options
538 * Returns: machine options (never null).
540 QemuOpts *qemu_get_machine_opts(void)
542 return qemu_find_opts_singleton("machine");
545 const char *qemu_get_vm_name(void)
547 return qemu_name;
550 static void res_free(void)
552 if (boot_splash_filedata != NULL) {
553 g_free(boot_splash_filedata);
554 boot_splash_filedata = NULL;
558 static int default_driver_check(QemuOpts *opts, void *opaque)
560 const char *driver = qemu_opt_get(opts, "driver");
561 int i;
563 if (!driver)
564 return 0;
565 for (i = 0; i < ARRAY_SIZE(default_list); i++) {
566 if (strcmp(default_list[i].driver, driver) != 0)
567 continue;
568 *(default_list[i].flag) = 0;
570 return 0;
573 /***********************************************************/
574 /* QEMU state */
576 static RunState current_run_state = RUN_STATE_PRELAUNCH;
578 typedef struct {
579 RunState from;
580 RunState to;
581 } RunStateTransition;
583 static const RunStateTransition runstate_transitions_def[] = {
584 /* from -> to */
585 { RUN_STATE_DEBUG, RUN_STATE_RUNNING },
586 { RUN_STATE_DEBUG, RUN_STATE_FINISH_MIGRATE },
588 { RUN_STATE_INMIGRATE, RUN_STATE_RUNNING },
589 { RUN_STATE_INMIGRATE, RUN_STATE_PAUSED },
591 { RUN_STATE_INTERNAL_ERROR, RUN_STATE_PAUSED },
592 { RUN_STATE_INTERNAL_ERROR, RUN_STATE_FINISH_MIGRATE },
594 { RUN_STATE_IO_ERROR, RUN_STATE_RUNNING },
595 { RUN_STATE_IO_ERROR, RUN_STATE_FINISH_MIGRATE },
597 { RUN_STATE_PAUSED, RUN_STATE_RUNNING },
598 { RUN_STATE_PAUSED, RUN_STATE_FINISH_MIGRATE },
600 { RUN_STATE_POSTMIGRATE, RUN_STATE_RUNNING },
601 { RUN_STATE_POSTMIGRATE, RUN_STATE_FINISH_MIGRATE },
603 { RUN_STATE_PRELAUNCH, RUN_STATE_RUNNING },
604 { RUN_STATE_PRELAUNCH, RUN_STATE_FINISH_MIGRATE },
605 { RUN_STATE_PRELAUNCH, RUN_STATE_INMIGRATE },
607 { RUN_STATE_FINISH_MIGRATE, RUN_STATE_RUNNING },
608 { RUN_STATE_FINISH_MIGRATE, RUN_STATE_POSTMIGRATE },
610 { RUN_STATE_RESTORE_VM, RUN_STATE_RUNNING },
612 { RUN_STATE_RUNNING, RUN_STATE_DEBUG },
613 { RUN_STATE_RUNNING, RUN_STATE_INTERNAL_ERROR },
614 { RUN_STATE_RUNNING, RUN_STATE_IO_ERROR },
615 { RUN_STATE_RUNNING, RUN_STATE_PAUSED },
616 { RUN_STATE_RUNNING, RUN_STATE_FINISH_MIGRATE },
617 { RUN_STATE_RUNNING, RUN_STATE_RESTORE_VM },
618 { RUN_STATE_RUNNING, RUN_STATE_SAVE_VM },
619 { RUN_STATE_RUNNING, RUN_STATE_SHUTDOWN },
620 { RUN_STATE_RUNNING, RUN_STATE_WATCHDOG },
621 { RUN_STATE_RUNNING, RUN_STATE_GUEST_PANICKED },
623 { RUN_STATE_SAVE_VM, RUN_STATE_RUNNING },
625 { RUN_STATE_SHUTDOWN, RUN_STATE_PAUSED },
626 { RUN_STATE_SHUTDOWN, RUN_STATE_FINISH_MIGRATE },
628 { RUN_STATE_DEBUG, RUN_STATE_SUSPENDED },
629 { RUN_STATE_RUNNING, RUN_STATE_SUSPENDED },
630 { RUN_STATE_SUSPENDED, RUN_STATE_RUNNING },
631 { RUN_STATE_SUSPENDED, RUN_STATE_FINISH_MIGRATE },
633 { RUN_STATE_WATCHDOG, RUN_STATE_RUNNING },
634 { RUN_STATE_WATCHDOG, RUN_STATE_FINISH_MIGRATE },
636 { RUN_STATE_GUEST_PANICKED, RUN_STATE_RUNNING },
637 { RUN_STATE_GUEST_PANICKED, RUN_STATE_FINISH_MIGRATE },
639 { RUN_STATE_MAX, RUN_STATE_MAX },
642 static bool runstate_valid_transitions[RUN_STATE_MAX][RUN_STATE_MAX];
644 bool runstate_check(RunState state)
646 return current_run_state == state;
649 static void runstate_init(void)
651 const RunStateTransition *p;
653 memset(&runstate_valid_transitions, 0, sizeof(runstate_valid_transitions));
655 for (p = &runstate_transitions_def[0]; p->from != RUN_STATE_MAX; p++) {
656 runstate_valid_transitions[p->from][p->to] = true;
660 /* This function will abort() on invalid state transitions */
661 void runstate_set(RunState new_state)
663 assert(new_state < RUN_STATE_MAX);
665 if (!runstate_valid_transitions[current_run_state][new_state]) {
666 fprintf(stderr, "ERROR: invalid runstate transition: '%s' -> '%s'\n",
667 RunState_lookup[current_run_state],
668 RunState_lookup[new_state]);
669 abort();
671 trace_runstate_set(new_state);
672 current_run_state = new_state;
675 int runstate_is_running(void)
677 return runstate_check(RUN_STATE_RUNNING);
680 bool runstate_needs_reset(void)
682 return runstate_check(RUN_STATE_INTERNAL_ERROR) ||
683 runstate_check(RUN_STATE_SHUTDOWN);
686 StatusInfo *qmp_query_status(Error **errp)
688 StatusInfo *info = g_malloc0(sizeof(*info));
690 info->running = runstate_is_running();
691 info->singlestep = singlestep;
692 info->status = current_run_state;
694 return info;
697 /***********************************************************/
698 /* real time host monotonic timer */
700 /***********************************************************/
701 /* host time/date access */
702 void qemu_get_timedate(struct tm *tm, int offset)
704 time_t ti;
706 time(&ti);
707 ti += offset;
708 if (rtc_date_offset == -1) {
709 if (rtc_utc)
710 gmtime_r(&ti, tm);
711 else
712 localtime_r(&ti, tm);
713 } else {
714 ti -= rtc_date_offset;
715 gmtime_r(&ti, tm);
719 int qemu_timedate_diff(struct tm *tm)
721 time_t seconds;
723 if (rtc_date_offset == -1)
724 if (rtc_utc)
725 seconds = mktimegm(tm);
726 else {
727 struct tm tmp = *tm;
728 tmp.tm_isdst = -1; /* use timezone to figure it out */
729 seconds = mktime(&tmp);
731 else
732 seconds = mktimegm(tm) + rtc_date_offset;
734 return seconds - time(NULL);
737 void rtc_change_mon_event(struct tm *tm)
739 QObject *data;
741 data = qobject_from_jsonf("{ 'offset': %d }", qemu_timedate_diff(tm));
742 monitor_protocol_event(QEVENT_RTC_CHANGE, data);
743 qobject_decref(data);
746 static void configure_rtc_date_offset(const char *startdate, int legacy)
748 time_t rtc_start_date;
749 struct tm tm;
751 if (!strcmp(startdate, "now") && legacy) {
752 rtc_date_offset = -1;
753 } else {
754 if (sscanf(startdate, "%d-%d-%dT%d:%d:%d",
755 &tm.tm_year,
756 &tm.tm_mon,
757 &tm.tm_mday,
758 &tm.tm_hour,
759 &tm.tm_min,
760 &tm.tm_sec) == 6) {
761 /* OK */
762 } else if (sscanf(startdate, "%d-%d-%d",
763 &tm.tm_year,
764 &tm.tm_mon,
765 &tm.tm_mday) == 3) {
766 tm.tm_hour = 0;
767 tm.tm_min = 0;
768 tm.tm_sec = 0;
769 } else {
770 goto date_fail;
772 tm.tm_year -= 1900;
773 tm.tm_mon--;
774 rtc_start_date = mktimegm(&tm);
775 if (rtc_start_date == -1) {
776 date_fail:
777 fprintf(stderr, "Invalid date format. Valid formats are:\n"
778 "'2006-06-17T16:01:21' or '2006-06-17'\n");
779 exit(1);
781 rtc_date_offset = time(NULL) - rtc_start_date;
785 static void configure_rtc(QemuOpts *opts)
787 const char *value;
789 value = qemu_opt_get(opts, "base");
790 if (value) {
791 if (!strcmp(value, "utc")) {
792 rtc_utc = 1;
793 } else if (!strcmp(value, "localtime")) {
794 rtc_utc = 0;
795 } else {
796 configure_rtc_date_offset(value, 0);
799 value = qemu_opt_get(opts, "clock");
800 if (value) {
801 if (!strcmp(value, "host")) {
802 rtc_clock = QEMU_CLOCK_HOST;
803 } else if (!strcmp(value, "rt")) {
804 rtc_clock = QEMU_CLOCK_REALTIME;
805 } else if (!strcmp(value, "vm")) {
806 rtc_clock = QEMU_CLOCK_VIRTUAL;
807 } else {
808 fprintf(stderr, "qemu: invalid option value '%s'\n", value);
809 exit(1);
812 value = qemu_opt_get(opts, "driftfix");
813 if (value) {
814 if (!strcmp(value, "slew")) {
815 static GlobalProperty slew_lost_ticks[] = {
817 .driver = "mc146818rtc",
818 .property = "lost_tick_policy",
819 .value = "slew",
821 { /* end of list */ }
824 qdev_prop_register_global_list(slew_lost_ticks);
825 } else if (!strcmp(value, "none")) {
826 /* discard is default */
827 } else {
828 fprintf(stderr, "qemu: invalid option value '%s'\n", value);
829 exit(1);
834 /***********************************************************/
835 /* Bluetooth support */
836 static int nb_hcis;
837 static int cur_hci;
838 static struct HCIInfo *hci_table[MAX_NICS];
840 struct HCIInfo *qemu_next_hci(void)
842 if (cur_hci == nb_hcis)
843 return &null_hci;
845 return hci_table[cur_hci++];
848 static int bt_hci_parse(const char *str)
850 struct HCIInfo *hci;
851 bdaddr_t bdaddr;
853 if (nb_hcis >= MAX_NICS) {
854 fprintf(stderr, "qemu: Too many bluetooth HCIs (max %i).\n", MAX_NICS);
855 return -1;
858 hci = hci_init(str);
859 if (!hci)
860 return -1;
862 bdaddr.b[0] = 0x52;
863 bdaddr.b[1] = 0x54;
864 bdaddr.b[2] = 0x00;
865 bdaddr.b[3] = 0x12;
866 bdaddr.b[4] = 0x34;
867 bdaddr.b[5] = 0x56 + nb_hcis;
868 hci->bdaddr_set(hci, bdaddr.b);
870 hci_table[nb_hcis++] = hci;
872 return 0;
875 static void bt_vhci_add(int vlan_id)
877 struct bt_scatternet_s *vlan = qemu_find_bt_vlan(vlan_id);
879 if (!vlan->slave)
880 fprintf(stderr, "qemu: warning: adding a VHCI to "
881 "an empty scatternet %i\n", vlan_id);
883 bt_vhci_init(bt_new_hci(vlan));
886 static struct bt_device_s *bt_device_add(const char *opt)
888 struct bt_scatternet_s *vlan;
889 int vlan_id = 0;
890 char *endp = strstr(opt, ",vlan=");
891 int len = (endp ? endp - opt : strlen(opt)) + 1;
892 char devname[10];
894 pstrcpy(devname, MIN(sizeof(devname), len), opt);
896 if (endp) {
897 vlan_id = strtol(endp + 6, &endp, 0);
898 if (*endp) {
899 fprintf(stderr, "qemu: unrecognised bluetooth vlan Id\n");
900 return 0;
904 vlan = qemu_find_bt_vlan(vlan_id);
906 if (!vlan->slave)
907 fprintf(stderr, "qemu: warning: adding a slave device to "
908 "an empty scatternet %i\n", vlan_id);
910 if (!strcmp(devname, "keyboard"))
911 return bt_keyboard_init(vlan);
913 fprintf(stderr, "qemu: unsupported bluetooth device `%s'\n", devname);
914 return 0;
917 static int bt_parse(const char *opt)
919 const char *endp, *p;
920 int vlan;
922 if (strstart(opt, "hci", &endp)) {
923 if (!*endp || *endp == ',') {
924 if (*endp)
925 if (!strstart(endp, ",vlan=", 0))
926 opt = endp + 1;
928 return bt_hci_parse(opt);
930 } else if (strstart(opt, "vhci", &endp)) {
931 if (!*endp || *endp == ',') {
932 if (*endp) {
933 if (strstart(endp, ",vlan=", &p)) {
934 vlan = strtol(p, (char **) &endp, 0);
935 if (*endp) {
936 fprintf(stderr, "qemu: bad scatternet '%s'\n", p);
937 return 1;
939 } else {
940 fprintf(stderr, "qemu: bad parameter '%s'\n", endp + 1);
941 return 1;
943 } else
944 vlan = 0;
946 bt_vhci_add(vlan);
947 return 0;
949 } else if (strstart(opt, "device:", &endp))
950 return !bt_device_add(endp);
952 fprintf(stderr, "qemu: bad bluetooth parameter '%s'\n", opt);
953 return 1;
956 static int parse_sandbox(QemuOpts *opts, void *opaque)
958 /* FIXME: change this to true for 1.3 */
959 if (qemu_opt_get_bool(opts, "enable", false)) {
960 #ifdef CONFIG_SECCOMP
961 if (seccomp_start() < 0) {
962 qerror_report(ERROR_CLASS_GENERIC_ERROR,
963 "failed to install seccomp syscall filter in the kernel");
964 return -1;
966 #else
967 qerror_report(ERROR_CLASS_GENERIC_ERROR,
968 "sandboxing request but seccomp is not compiled into this build");
969 return -1;
970 #endif
973 return 0;
976 static int parse_name(QemuOpts *opts, void *opaque)
978 const char *proc_name;
980 if (qemu_opt_get(opts, "debug-threads")) {
981 qemu_thread_naming(qemu_opt_get_bool(opts, "debug-threads", false));
983 qemu_name = qemu_opt_get(opts, "guest");
985 proc_name = qemu_opt_get(opts, "process");
986 if (proc_name) {
987 os_set_proc_name(proc_name);
990 return 0;
993 bool usb_enabled(bool default_usb)
995 return qemu_opt_get_bool(qemu_get_machine_opts(), "usb",
996 has_defaults && default_usb);
999 #ifndef _WIN32
1000 static int parse_add_fd(QemuOpts *opts, void *opaque)
1002 int fd, dupfd, flags;
1003 int64_t fdset_id;
1004 const char *fd_opaque = NULL;
1006 fd = qemu_opt_get_number(opts, "fd", -1);
1007 fdset_id = qemu_opt_get_number(opts, "set", -1);
1008 fd_opaque = qemu_opt_get(opts, "opaque");
1010 if (fd < 0) {
1011 qerror_report(ERROR_CLASS_GENERIC_ERROR,
1012 "fd option is required and must be non-negative");
1013 return -1;
1016 if (fd <= STDERR_FILENO) {
1017 qerror_report(ERROR_CLASS_GENERIC_ERROR,
1018 "fd cannot be a standard I/O stream");
1019 return -1;
1023 * All fds inherited across exec() necessarily have FD_CLOEXEC
1024 * clear, while qemu sets FD_CLOEXEC on all other fds used internally.
1026 flags = fcntl(fd, F_GETFD);
1027 if (flags == -1 || (flags & FD_CLOEXEC)) {
1028 qerror_report(ERROR_CLASS_GENERIC_ERROR,
1029 "fd is not valid or already in use");
1030 return -1;
1033 if (fdset_id < 0) {
1034 qerror_report(ERROR_CLASS_GENERIC_ERROR,
1035 "set option is required and must be non-negative");
1036 return -1;
1039 #ifdef F_DUPFD_CLOEXEC
1040 dupfd = fcntl(fd, F_DUPFD_CLOEXEC, 0);
1041 #else
1042 dupfd = dup(fd);
1043 if (dupfd != -1) {
1044 qemu_set_cloexec(dupfd);
1046 #endif
1047 if (dupfd == -1) {
1048 qerror_report(ERROR_CLASS_GENERIC_ERROR,
1049 "Error duplicating fd: %s", strerror(errno));
1050 return -1;
1053 /* add the duplicate fd, and optionally the opaque string, to the fd set */
1054 monitor_fdset_add_fd(dupfd, true, fdset_id, fd_opaque ? true : false,
1055 fd_opaque, NULL);
1057 return 0;
1060 static int cleanup_add_fd(QemuOpts *opts, void *opaque)
1062 int fd;
1064 fd = qemu_opt_get_number(opts, "fd", -1);
1065 close(fd);
1067 return 0;
1069 #endif
1071 /***********************************************************/
1072 /* QEMU Block devices */
1074 #define HD_OPTS "media=disk"
1075 #define CDROM_OPTS "media=cdrom"
1076 #define FD_OPTS ""
1077 #define PFLASH_OPTS ""
1078 #define MTD_OPTS ""
1079 #define SD_OPTS ""
1081 static int drive_init_func(QemuOpts *opts, void *opaque)
1083 BlockInterfaceType *block_default_type = opaque;
1085 return drive_new(opts, *block_default_type) == NULL;
1088 static int drive_enable_snapshot(QemuOpts *opts, void *opaque)
1090 if (NULL == qemu_opt_get(opts, "snapshot")) {
1091 qemu_opt_set(opts, "snapshot", "on");
1093 return 0;
1096 static void default_drive(int enable, int snapshot, BlockInterfaceType type,
1097 int index, const char *optstr)
1099 QemuOpts *opts;
1101 if (!enable || drive_get_by_index(type, index)) {
1102 return;
1105 opts = drive_add(type, index, NULL, optstr);
1106 if (snapshot) {
1107 drive_enable_snapshot(opts, NULL);
1109 if (!drive_new(opts, type)) {
1110 exit(1);
1114 void qemu_register_boot_set(QEMUBootSetHandler *func, void *opaque)
1116 boot_set_handler = func;
1117 boot_set_opaque = opaque;
1120 int qemu_boot_set(const char *boot_order)
1122 if (!boot_set_handler) {
1123 return -EINVAL;
1125 return boot_set_handler(boot_set_opaque, boot_order);
1128 static void validate_bootdevices(const char *devices)
1130 /* We just do some generic consistency checks */
1131 const char *p;
1132 int bitmap = 0;
1134 for (p = devices; *p != '\0'; p++) {
1135 /* Allowed boot devices are:
1136 * a-b: floppy disk drives
1137 * c-f: IDE disk drives
1138 * g-m: machine implementation dependent drives
1139 * n-p: network devices
1140 * It's up to each machine implementation to check if the given boot
1141 * devices match the actual hardware implementation and firmware
1142 * features.
1144 if (*p < 'a' || *p > 'p') {
1145 fprintf(stderr, "Invalid boot device '%c'\n", *p);
1146 exit(1);
1148 if (bitmap & (1 << (*p - 'a'))) {
1149 fprintf(stderr, "Boot device '%c' was given twice\n", *p);
1150 exit(1);
1152 bitmap |= 1 << (*p - 'a');
1156 static void restore_boot_order(void *opaque)
1158 char *normal_boot_order = opaque;
1159 static int first = 1;
1161 /* Restore boot order and remove ourselves after the first boot */
1162 if (first) {
1163 first = 0;
1164 return;
1167 qemu_boot_set(normal_boot_order);
1169 qemu_unregister_reset(restore_boot_order, normal_boot_order);
1170 g_free(normal_boot_order);
1173 void add_boot_device_path(int32_t bootindex, DeviceState *dev,
1174 const char *suffix)
1176 FWBootEntry *node, *i;
1178 if (bootindex < 0) {
1179 return;
1182 assert(dev != NULL || suffix != NULL);
1184 node = g_malloc0(sizeof(FWBootEntry));
1185 node->bootindex = bootindex;
1186 node->suffix = g_strdup(suffix);
1187 node->dev = dev;
1189 QTAILQ_FOREACH(i, &fw_boot_order, link) {
1190 if (i->bootindex == bootindex) {
1191 fprintf(stderr, "Two devices with same boot index %d\n", bootindex);
1192 exit(1);
1193 } else if (i->bootindex < bootindex) {
1194 continue;
1196 QTAILQ_INSERT_BEFORE(i, node, link);
1197 return;
1199 QTAILQ_INSERT_TAIL(&fw_boot_order, node, link);
1202 DeviceState *get_boot_device(uint32_t position)
1204 uint32_t counter = 0;
1205 FWBootEntry *i = NULL;
1206 DeviceState *res = NULL;
1208 if (!QTAILQ_EMPTY(&fw_boot_order)) {
1209 QTAILQ_FOREACH(i, &fw_boot_order, link) {
1210 if (counter == position) {
1211 res = i->dev;
1212 break;
1214 counter++;
1217 return res;
1221 * This function returns null terminated string that consist of new line
1222 * separated device paths.
1224 * memory pointed by "size" is assigned total length of the array in bytes
1227 char *get_boot_devices_list(size_t *size, bool ignore_suffixes)
1229 FWBootEntry *i;
1230 size_t total = 0;
1231 char *list = NULL;
1233 QTAILQ_FOREACH(i, &fw_boot_order, link) {
1234 char *devpath = NULL, *bootpath;
1235 size_t len;
1237 if (i->dev) {
1238 devpath = qdev_get_fw_dev_path(i->dev);
1239 assert(devpath);
1242 if (i->suffix && !ignore_suffixes && devpath) {
1243 size_t bootpathlen = strlen(devpath) + strlen(i->suffix) + 1;
1245 bootpath = g_malloc(bootpathlen);
1246 snprintf(bootpath, bootpathlen, "%s%s", devpath, i->suffix);
1247 g_free(devpath);
1248 } else if (devpath) {
1249 bootpath = devpath;
1250 } else if (!ignore_suffixes) {
1251 assert(i->suffix);
1252 bootpath = g_strdup(i->suffix);
1253 } else {
1254 bootpath = g_strdup("");
1257 if (total) {
1258 list[total-1] = '\n';
1260 len = strlen(bootpath) + 1;
1261 list = g_realloc(list, total + len);
1262 memcpy(&list[total], bootpath, len);
1263 total += len;
1264 g_free(bootpath);
1267 *size = total;
1269 if (boot_strict && *size > 0) {
1270 list[total-1] = '\n';
1271 list = g_realloc(list, total + 5);
1272 memcpy(&list[total], "HALT", 5);
1273 *size = total + 5;
1275 return list;
1278 static void numa_node_parse_cpus(int nodenr, const char *cpus)
1280 char *endptr;
1281 unsigned long long value, endvalue;
1283 /* Empty CPU range strings will be considered valid, they will simply
1284 * not set any bit in the CPU bitmap.
1286 if (!*cpus) {
1287 return;
1290 if (parse_uint(cpus, &value, &endptr, 10) < 0) {
1291 goto error;
1293 if (*endptr == '-') {
1294 if (parse_uint_full(endptr + 1, &endvalue, 10) < 0) {
1295 goto error;
1297 } else if (*endptr == '\0') {
1298 endvalue = value;
1299 } else {
1300 goto error;
1303 if (endvalue >= MAX_CPUMASK_BITS) {
1304 endvalue = MAX_CPUMASK_BITS - 1;
1305 fprintf(stderr,
1306 "qemu: NUMA: A max of %d VCPUs are supported\n",
1307 MAX_CPUMASK_BITS);
1310 if (endvalue < value) {
1311 goto error;
1314 bitmap_set(node_cpumask[nodenr], value, endvalue-value+1);
1315 return;
1317 error:
1318 fprintf(stderr, "qemu: Invalid NUMA CPU range: %s\n", cpus);
1319 exit(1);
1322 static void numa_add(const char *optarg)
1324 char option[128];
1325 char *endptr;
1326 unsigned long long nodenr;
1328 optarg = get_opt_name(option, 128, optarg, ',');
1329 if (*optarg == ',') {
1330 optarg++;
1332 if (!strcmp(option, "node")) {
1334 if (nb_numa_nodes >= MAX_NODES) {
1335 fprintf(stderr, "qemu: too many NUMA nodes\n");
1336 exit(1);
1339 if (get_param_value(option, 128, "nodeid", optarg) == 0) {
1340 nodenr = nb_numa_nodes;
1341 } else {
1342 if (parse_uint_full(option, &nodenr, 10) < 0) {
1343 fprintf(stderr, "qemu: Invalid NUMA nodeid: %s\n", option);
1344 exit(1);
1348 if (nodenr >= MAX_NODES) {
1349 fprintf(stderr, "qemu: invalid NUMA nodeid: %llu\n", nodenr);
1350 exit(1);
1353 if (get_param_value(option, 128, "mem", optarg) == 0) {
1354 node_mem[nodenr] = 0;
1355 } else {
1356 int64_t sval;
1357 sval = strtosz(option, &endptr);
1358 if (sval < 0 || *endptr) {
1359 fprintf(stderr, "qemu: invalid numa mem size: %s\n", optarg);
1360 exit(1);
1362 node_mem[nodenr] = sval;
1364 if (get_param_value(option, 128, "cpus", optarg) != 0) {
1365 numa_node_parse_cpus(nodenr, option);
1367 nb_numa_nodes++;
1368 } else {
1369 fprintf(stderr, "Invalid -numa option: %s\n", option);
1370 exit(1);
1374 static QemuOptsList qemu_smp_opts = {
1375 .name = "smp-opts",
1376 .implied_opt_name = "cpus",
1377 .merge_lists = true,
1378 .head = QTAILQ_HEAD_INITIALIZER(qemu_smp_opts.head),
1379 .desc = {
1381 .name = "cpus",
1382 .type = QEMU_OPT_NUMBER,
1383 }, {
1384 .name = "sockets",
1385 .type = QEMU_OPT_NUMBER,
1386 }, {
1387 .name = "cores",
1388 .type = QEMU_OPT_NUMBER,
1389 }, {
1390 .name = "threads",
1391 .type = QEMU_OPT_NUMBER,
1392 }, {
1393 .name = "maxcpus",
1394 .type = QEMU_OPT_NUMBER,
1396 { /*End of list */ }
1400 static void smp_parse(QemuOpts *opts)
1402 if (opts) {
1404 unsigned cpus = qemu_opt_get_number(opts, "cpus", 0);
1405 unsigned sockets = qemu_opt_get_number(opts, "sockets", 0);
1406 unsigned cores = qemu_opt_get_number(opts, "cores", 0);
1407 unsigned threads = qemu_opt_get_number(opts, "threads", 0);
1409 /* compute missing values, prefer sockets over cores over threads */
1410 if (cpus == 0 || sockets == 0) {
1411 sockets = sockets > 0 ? sockets : 1;
1412 cores = cores > 0 ? cores : 1;
1413 threads = threads > 0 ? threads : 1;
1414 if (cpus == 0) {
1415 cpus = cores * threads * sockets;
1417 } else {
1418 if (cores == 0) {
1419 threads = threads > 0 ? threads : 1;
1420 cores = cpus / (sockets * threads);
1421 } else {
1422 threads = cpus / (cores * sockets);
1426 max_cpus = qemu_opt_get_number(opts, "maxcpus", 0);
1428 smp_cpus = cpus;
1429 smp_cores = cores > 0 ? cores : 1;
1430 smp_threads = threads > 0 ? threads : 1;
1434 if (max_cpus == 0) {
1435 max_cpus = smp_cpus;
1438 if (max_cpus > MAX_CPUMASK_BITS) {
1439 fprintf(stderr, "Unsupported number of maxcpus\n");
1440 exit(1);
1442 if (max_cpus < smp_cpus) {
1443 fprintf(stderr, "maxcpus must be equal to or greater than smp\n");
1444 exit(1);
1449 static void configure_realtime(QemuOpts *opts)
1451 bool enable_mlock;
1453 enable_mlock = qemu_opt_get_bool(opts, "mlock", true);
1455 if (enable_mlock) {
1456 if (os_mlock() < 0) {
1457 fprintf(stderr, "qemu: locking memory failed\n");
1458 exit(1);
1464 static void configure_msg(QemuOpts *opts)
1466 enable_timestamp_msg = qemu_opt_get_bool(opts, "timestamp", true);
1469 /***********************************************************/
1470 /* USB devices */
1472 static int usb_device_add(const char *devname)
1474 USBDevice *dev = NULL;
1475 #ifndef CONFIG_LINUX
1476 const char *p;
1477 #endif
1479 if (!usb_enabled(false)) {
1480 return -1;
1483 /* drivers with .usbdevice_name entry in USBDeviceInfo */
1484 dev = usbdevice_create(devname);
1485 if (dev)
1486 goto done;
1488 /* the other ones */
1489 #ifndef CONFIG_LINUX
1490 /* only the linux version is qdev-ified, usb-bsd still needs this */
1491 if (strstart(devname, "host:", &p)) {
1492 dev = usb_host_device_open(usb_bus_find(-1), p);
1494 #endif
1495 if (!dev)
1496 return -1;
1498 done:
1499 return 0;
1502 static int usb_device_del(const char *devname)
1504 int bus_num, addr;
1505 const char *p;
1507 if (strstart(devname, "host:", &p)) {
1508 return -1;
1511 if (!usb_enabled(false)) {
1512 return -1;
1515 p = strchr(devname, '.');
1516 if (!p)
1517 return -1;
1518 bus_num = strtoul(devname, NULL, 0);
1519 addr = strtoul(p + 1, NULL, 0);
1521 return usb_device_delete_addr(bus_num, addr);
1524 static int usb_parse(const char *cmdline)
1526 int r;
1527 r = usb_device_add(cmdline);
1528 if (r < 0) {
1529 fprintf(stderr, "qemu: could not add USB device '%s'\n", cmdline);
1531 return r;
1534 void do_usb_add(Monitor *mon, const QDict *qdict)
1536 const char *devname = qdict_get_str(qdict, "devname");
1537 if (usb_device_add(devname) < 0) {
1538 error_report("could not add USB device '%s'", devname);
1542 void do_usb_del(Monitor *mon, const QDict *qdict)
1544 const char *devname = qdict_get_str(qdict, "devname");
1545 if (usb_device_del(devname) < 0) {
1546 error_report("could not delete USB device '%s'", devname);
1550 /***********************************************************/
1551 /* PCMCIA/Cardbus */
1553 static struct pcmcia_socket_entry_s {
1554 PCMCIASocket *socket;
1555 struct pcmcia_socket_entry_s *next;
1556 } *pcmcia_sockets = 0;
1558 void pcmcia_socket_register(PCMCIASocket *socket)
1560 struct pcmcia_socket_entry_s *entry;
1562 entry = g_malloc(sizeof(struct pcmcia_socket_entry_s));
1563 entry->socket = socket;
1564 entry->next = pcmcia_sockets;
1565 pcmcia_sockets = entry;
1568 void pcmcia_socket_unregister(PCMCIASocket *socket)
1570 struct pcmcia_socket_entry_s *entry, **ptr;
1572 ptr = &pcmcia_sockets;
1573 for (entry = *ptr; entry; ptr = &entry->next, entry = *ptr)
1574 if (entry->socket == socket) {
1575 *ptr = entry->next;
1576 g_free(entry);
1580 void pcmcia_info(Monitor *mon, const QDict *qdict)
1582 struct pcmcia_socket_entry_s *iter;
1584 if (!pcmcia_sockets)
1585 monitor_printf(mon, "No PCMCIA sockets\n");
1587 for (iter = pcmcia_sockets; iter; iter = iter->next)
1588 monitor_printf(mon, "%s: %s\n", iter->socket->slot_string,
1589 iter->socket->attached ? iter->socket->card_string :
1590 "Empty");
1593 /***********************************************************/
1594 /* machine registration */
1596 MachineState *current_machine;
1598 static void machine_class_init(ObjectClass *oc, void *data)
1600 MachineClass *mc = MACHINE_CLASS(oc);
1601 QEMUMachine *qm = data;
1603 mc->name = qm->name;
1604 mc->alias = qm->alias;
1605 mc->desc = qm->desc;
1606 mc->init = qm->init;
1607 mc->reset = qm->reset;
1608 mc->hot_add_cpu = qm->hot_add_cpu;
1609 mc->kvm_type = qm->kvm_type;
1610 mc->block_default_type = qm->block_default_type;
1611 mc->max_cpus = qm->max_cpus;
1612 mc->no_serial = qm->no_serial;
1613 mc->no_parallel = qm->no_parallel;
1614 mc->use_virtcon = qm->use_virtcon;
1615 mc->use_sclp = qm->use_sclp;
1616 mc->no_floppy = qm->no_floppy;
1617 mc->no_cdrom = qm->no_cdrom;
1618 mc->no_sdcard = qm->no_sdcard;
1619 mc->is_default = qm->is_default;
1620 mc->default_machine_opts = qm->default_machine_opts;
1621 mc->default_boot_order = qm->default_boot_order;
1622 mc->compat_props = qm->compat_props;
1623 mc->hw_version = qm->hw_version;
1626 int qemu_register_machine(QEMUMachine *m)
1628 char *name = g_strconcat(m->name, TYPE_MACHINE_SUFFIX, NULL);
1629 TypeInfo ti = {
1630 .name = name,
1631 .parent = TYPE_MACHINE,
1632 .class_init = machine_class_init,
1633 .class_data = (void *)m,
1636 type_register(&ti);
1637 g_free(name);
1639 return 0;
1642 static MachineClass *find_machine(const char *name)
1644 GSList *el, *machines = object_class_get_list(TYPE_MACHINE, false);
1645 MachineClass *mc = NULL;
1647 for (el = machines; el; el = el->next) {
1648 MachineClass *temp = el->data;
1650 if (!strcmp(temp->name, name)) {
1651 mc = temp;
1652 break;
1654 if (temp->alias &&
1655 !strcmp(temp->alias, name)) {
1656 mc = temp;
1657 break;
1661 g_slist_free(machines);
1662 return mc;
1665 MachineClass *find_default_machine(void)
1667 GSList *el, *machines = object_class_get_list(TYPE_MACHINE, false);
1668 MachineClass *mc = NULL;
1670 for (el = machines; el; el = el->next) {
1671 MachineClass *temp = el->data;
1673 if (temp->is_default) {
1674 mc = temp;
1675 break;
1679 g_slist_free(machines);
1680 return mc;
1683 MachineInfoList *qmp_query_machines(Error **errp)
1685 GSList *el, *machines = object_class_get_list(TYPE_MACHINE, false);
1686 MachineInfoList *mach_list = NULL;
1688 for (el = machines; el; el = el->next) {
1689 MachineClass *mc = el->data;
1690 MachineInfoList *entry;
1691 MachineInfo *info;
1693 info = g_malloc0(sizeof(*info));
1694 if (mc->is_default) {
1695 info->has_is_default = true;
1696 info->is_default = true;
1699 if (mc->alias) {
1700 info->has_alias = true;
1701 info->alias = g_strdup(mc->alias);
1704 info->name = g_strdup(mc->name);
1705 info->cpu_max = !mc->max_cpus ? 1 : mc->max_cpus;
1707 entry = g_malloc0(sizeof(*entry));
1708 entry->value = info;
1709 entry->next = mach_list;
1710 mach_list = entry;
1713 g_slist_free(machines);
1714 return mach_list;
1717 /***********************************************************/
1718 /* main execution loop */
1720 struct vm_change_state_entry {
1721 VMChangeStateHandler *cb;
1722 void *opaque;
1723 QLIST_ENTRY (vm_change_state_entry) entries;
1726 static QLIST_HEAD(vm_change_state_head, vm_change_state_entry) vm_change_state_head;
1728 VMChangeStateEntry *qemu_add_vm_change_state_handler(VMChangeStateHandler *cb,
1729 void *opaque)
1731 VMChangeStateEntry *e;
1733 e = g_malloc0(sizeof (*e));
1735 e->cb = cb;
1736 e->opaque = opaque;
1737 QLIST_INSERT_HEAD(&vm_change_state_head, e, entries);
1738 return e;
1741 void qemu_del_vm_change_state_handler(VMChangeStateEntry *e)
1743 QLIST_REMOVE (e, entries);
1744 g_free (e);
1747 void vm_state_notify(int running, RunState state)
1749 VMChangeStateEntry *e;
1751 trace_vm_state_notify(running, state);
1753 for (e = vm_change_state_head.lh_first; e; e = e->entries.le_next) {
1754 e->cb(e->opaque, running, state);
1758 void vm_start(void)
1760 if (!runstate_is_running()) {
1761 cpu_enable_ticks();
1762 runstate_set(RUN_STATE_RUNNING);
1763 vm_state_notify(1, RUN_STATE_RUNNING);
1764 resume_all_vcpus();
1765 monitor_protocol_event(QEVENT_RESUME, NULL);
1769 /* reset/shutdown handler */
1771 typedef struct QEMUResetEntry {
1772 QTAILQ_ENTRY(QEMUResetEntry) entry;
1773 QEMUResetHandler *func;
1774 void *opaque;
1775 } QEMUResetEntry;
1777 static QTAILQ_HEAD(reset_handlers, QEMUResetEntry) reset_handlers =
1778 QTAILQ_HEAD_INITIALIZER(reset_handlers);
1779 static int reset_requested;
1780 static int shutdown_requested, shutdown_signal = -1;
1781 static pid_t shutdown_pid;
1782 static int powerdown_requested;
1783 static int debug_requested;
1784 static int suspend_requested;
1785 static WakeupReason wakeup_reason;
1786 static NotifierList powerdown_notifiers =
1787 NOTIFIER_LIST_INITIALIZER(powerdown_notifiers);
1788 static NotifierList suspend_notifiers =
1789 NOTIFIER_LIST_INITIALIZER(suspend_notifiers);
1790 static NotifierList wakeup_notifiers =
1791 NOTIFIER_LIST_INITIALIZER(wakeup_notifiers);
1792 static uint32_t wakeup_reason_mask = ~(1 << QEMU_WAKEUP_REASON_NONE);
1793 static RunState vmstop_requested = RUN_STATE_MAX;
1795 int qemu_shutdown_requested_get(void)
1797 return shutdown_requested;
1800 int qemu_reset_requested_get(void)
1802 return reset_requested;
1805 static int qemu_shutdown_requested(void)
1807 int r = shutdown_requested;
1808 shutdown_requested = 0;
1809 return r;
1812 static void qemu_kill_report(void)
1814 if (!qtest_driver() && shutdown_signal != -1) {
1815 fprintf(stderr, "qemu: terminating on signal %d", shutdown_signal);
1816 if (shutdown_pid == 0) {
1817 /* This happens for eg ^C at the terminal, so it's worth
1818 * avoiding printing an odd message in that case.
1820 fputc('\n', stderr);
1821 } else {
1822 fprintf(stderr, " from pid " FMT_pid "\n", shutdown_pid);
1824 shutdown_signal = -1;
1828 static int qemu_reset_requested(void)
1830 int r = reset_requested;
1831 reset_requested = 0;
1832 return r;
1835 static int qemu_suspend_requested(void)
1837 int r = suspend_requested;
1838 suspend_requested = 0;
1839 return r;
1842 static WakeupReason qemu_wakeup_requested(void)
1844 return wakeup_reason;
1847 static int qemu_powerdown_requested(void)
1849 int r = powerdown_requested;
1850 powerdown_requested = 0;
1851 return r;
1854 static int qemu_debug_requested(void)
1856 int r = debug_requested;
1857 debug_requested = 0;
1858 return r;
1861 /* We use RUN_STATE_MAX but any invalid value will do */
1862 static bool qemu_vmstop_requested(RunState *r)
1864 if (vmstop_requested < RUN_STATE_MAX) {
1865 *r = vmstop_requested;
1866 vmstop_requested = RUN_STATE_MAX;
1867 return true;
1870 return false;
1873 void qemu_register_reset(QEMUResetHandler *func, void *opaque)
1875 QEMUResetEntry *re = g_malloc0(sizeof(QEMUResetEntry));
1877 re->func = func;
1878 re->opaque = opaque;
1879 QTAILQ_INSERT_TAIL(&reset_handlers, re, entry);
1882 void qemu_unregister_reset(QEMUResetHandler *func, void *opaque)
1884 QEMUResetEntry *re;
1886 QTAILQ_FOREACH(re, &reset_handlers, entry) {
1887 if (re->func == func && re->opaque == opaque) {
1888 QTAILQ_REMOVE(&reset_handlers, re, entry);
1889 g_free(re);
1890 return;
1895 void qemu_devices_reset(void)
1897 QEMUResetEntry *re, *nre;
1899 /* reset all devices */
1900 QTAILQ_FOREACH_SAFE(re, &reset_handlers, entry, nre) {
1901 re->func(re->opaque);
1905 void qemu_system_reset(bool report)
1907 MachineClass *mc;
1909 mc = current_machine ? MACHINE_GET_CLASS(current_machine) : NULL;
1911 if (mc && mc->reset) {
1912 mc->reset();
1913 } else {
1914 qemu_devices_reset();
1916 if (report) {
1917 monitor_protocol_event(QEVENT_RESET, NULL);
1919 cpu_synchronize_all_post_reset();
1922 void qemu_system_reset_request(void)
1924 if (no_reboot) {
1925 shutdown_requested = 1;
1926 } else {
1927 reset_requested = 1;
1929 cpu_stop_current();
1930 qemu_notify_event();
1933 static void qemu_system_suspend(void)
1935 pause_all_vcpus();
1936 notifier_list_notify(&suspend_notifiers, NULL);
1937 runstate_set(RUN_STATE_SUSPENDED);
1938 monitor_protocol_event(QEVENT_SUSPEND, NULL);
1941 void qemu_system_suspend_request(void)
1943 if (runstate_check(RUN_STATE_SUSPENDED)) {
1944 return;
1946 suspend_requested = 1;
1947 cpu_stop_current();
1948 qemu_notify_event();
1951 void qemu_register_suspend_notifier(Notifier *notifier)
1953 notifier_list_add(&suspend_notifiers, notifier);
1956 void qemu_system_wakeup_request(WakeupReason reason)
1958 trace_system_wakeup_request(reason);
1960 if (!runstate_check(RUN_STATE_SUSPENDED)) {
1961 return;
1963 if (!(wakeup_reason_mask & (1 << reason))) {
1964 return;
1966 runstate_set(RUN_STATE_RUNNING);
1967 wakeup_reason = reason;
1968 qemu_notify_event();
1971 void qemu_system_wakeup_enable(WakeupReason reason, bool enabled)
1973 if (enabled) {
1974 wakeup_reason_mask |= (1 << reason);
1975 } else {
1976 wakeup_reason_mask &= ~(1 << reason);
1980 void qemu_register_wakeup_notifier(Notifier *notifier)
1982 notifier_list_add(&wakeup_notifiers, notifier);
1985 void qemu_system_killed(int signal, pid_t pid)
1987 shutdown_signal = signal;
1988 shutdown_pid = pid;
1989 no_shutdown = 0;
1990 qemu_system_shutdown_request();
1993 void qemu_system_shutdown_request(void)
1995 shutdown_requested = 1;
1996 qemu_notify_event();
1999 static void qemu_system_powerdown(void)
2001 monitor_protocol_event(QEVENT_POWERDOWN, NULL);
2002 notifier_list_notify(&powerdown_notifiers, NULL);
2005 void qemu_system_powerdown_request(void)
2007 powerdown_requested = 1;
2008 qemu_notify_event();
2011 void qemu_register_powerdown_notifier(Notifier *notifier)
2013 notifier_list_add(&powerdown_notifiers, notifier);
2016 void qemu_system_debug_request(void)
2018 debug_requested = 1;
2019 qemu_notify_event();
2022 void qemu_system_vmstop_request(RunState state)
2024 vmstop_requested = state;
2025 qemu_notify_event();
2028 static bool main_loop_should_exit(void)
2030 RunState r;
2031 if (qemu_debug_requested()) {
2032 vm_stop(RUN_STATE_DEBUG);
2034 if (qemu_suspend_requested()) {
2035 qemu_system_suspend();
2037 if (qemu_shutdown_requested()) {
2038 qemu_kill_report();
2039 monitor_protocol_event(QEVENT_SHUTDOWN, NULL);
2040 if (no_shutdown) {
2041 vm_stop(RUN_STATE_SHUTDOWN);
2042 } else {
2043 return true;
2046 if (qemu_reset_requested()) {
2047 pause_all_vcpus();
2048 cpu_synchronize_all_states();
2049 qemu_system_reset(VMRESET_REPORT);
2050 resume_all_vcpus();
2051 if (runstate_needs_reset()) {
2052 runstate_set(RUN_STATE_PAUSED);
2055 if (qemu_wakeup_requested()) {
2056 pause_all_vcpus();
2057 cpu_synchronize_all_states();
2058 qemu_system_reset(VMRESET_SILENT);
2059 notifier_list_notify(&wakeup_notifiers, &wakeup_reason);
2060 wakeup_reason = QEMU_WAKEUP_REASON_NONE;
2061 resume_all_vcpus();
2062 monitor_protocol_event(QEVENT_WAKEUP, NULL);
2064 if (qemu_powerdown_requested()) {
2065 qemu_system_powerdown();
2067 if (qemu_vmstop_requested(&r)) {
2068 vm_stop(r);
2070 return false;
2073 static void main_loop(void)
2075 bool nonblocking;
2076 int last_io = 0;
2077 #ifdef CONFIG_PROFILER
2078 int64_t ti;
2079 #endif
2080 do {
2081 nonblocking = !kvm_enabled() && !xen_enabled() && last_io > 0;
2082 #ifdef CONFIG_PROFILER
2083 ti = profile_getclock();
2084 #endif
2085 last_io = main_loop_wait(nonblocking);
2086 #ifdef CONFIG_PROFILER
2087 dev_time += profile_getclock() - ti;
2088 #endif
2089 } while (!main_loop_should_exit());
2092 static void version(void)
2094 printf("QEMU emulator version " QEMU_VERSION QEMU_PKGVERSION ", Copyright (c) 2003-2008 Fabrice Bellard\n");
2097 static void help(int exitcode)
2099 version();
2100 printf("usage: %s [options] [disk_image]\n\n"
2101 "'disk_image' is a raw hard disk image for IDE hard disk 0\n\n",
2102 error_get_progname());
2104 #define QEMU_OPTIONS_GENERATE_HELP
2105 #include "qemu-options-wrapper.h"
2107 printf("\nDuring emulation, the following keys are useful:\n"
2108 "ctrl-alt-f toggle full screen\n"
2109 "ctrl-alt-n switch to virtual console 'n'\n"
2110 "ctrl-alt toggle mouse and keyboard grab\n"
2111 "\n"
2112 "When using -nographic, press 'ctrl-a h' to get some help.\n");
2114 exit(exitcode);
2117 #define HAS_ARG 0x0001
2119 typedef struct QEMUOption {
2120 const char *name;
2121 int flags;
2122 int index;
2123 uint32_t arch_mask;
2124 } QEMUOption;
2126 static const QEMUOption qemu_options[] = {
2127 { "h", 0, QEMU_OPTION_h, QEMU_ARCH_ALL },
2128 #define QEMU_OPTIONS_GENERATE_OPTIONS
2129 #include "qemu-options-wrapper.h"
2130 { NULL },
2133 static bool vga_available(void)
2135 return object_class_by_name("VGA") || object_class_by_name("isa-vga");
2138 static bool cirrus_vga_available(void)
2140 return object_class_by_name("cirrus-vga")
2141 || object_class_by_name("isa-cirrus-vga");
2144 static bool vmware_vga_available(void)
2146 return object_class_by_name("vmware-svga");
2149 static bool qxl_vga_available(void)
2151 return object_class_by_name("qxl-vga");
2154 static bool tcx_vga_available(void)
2156 return object_class_by_name("SUNW,tcx");
2159 static bool cg3_vga_available(void)
2161 return object_class_by_name("cgthree");
2164 static void select_vgahw (const char *p)
2166 const char *opts;
2168 assert(vga_interface_type == VGA_NONE);
2169 if (strstart(p, "std", &opts)) {
2170 if (vga_available()) {
2171 vga_interface_type = VGA_STD;
2172 } else {
2173 fprintf(stderr, "Error: standard VGA not available\n");
2174 exit(0);
2176 } else if (strstart(p, "cirrus", &opts)) {
2177 if (cirrus_vga_available()) {
2178 vga_interface_type = VGA_CIRRUS;
2179 } else {
2180 fprintf(stderr, "Error: Cirrus VGA not available\n");
2181 exit(0);
2183 } else if (strstart(p, "vmware", &opts)) {
2184 if (vmware_vga_available()) {
2185 vga_interface_type = VGA_VMWARE;
2186 } else {
2187 fprintf(stderr, "Error: VMWare SVGA not available\n");
2188 exit(0);
2190 } else if (strstart(p, "xenfb", &opts)) {
2191 vga_interface_type = VGA_XENFB;
2192 } else if (strstart(p, "qxl", &opts)) {
2193 if (qxl_vga_available()) {
2194 vga_interface_type = VGA_QXL;
2195 } else {
2196 fprintf(stderr, "Error: QXL VGA not available\n");
2197 exit(0);
2199 } else if (strstart(p, "tcx", &opts)) {
2200 if (tcx_vga_available()) {
2201 vga_interface_type = VGA_TCX;
2202 } else {
2203 fprintf(stderr, "Error: TCX framebuffer not available\n");
2204 exit(0);
2206 } else if (strstart(p, "cg3", &opts)) {
2207 if (cg3_vga_available()) {
2208 vga_interface_type = VGA_CG3;
2209 } else {
2210 fprintf(stderr, "Error: CG3 framebuffer not available\n");
2211 exit(0);
2213 } else if (!strstart(p, "none", &opts)) {
2214 invalid_vga:
2215 fprintf(stderr, "Unknown vga type: %s\n", p);
2216 exit(1);
2218 while (*opts) {
2219 const char *nextopt;
2221 if (strstart(opts, ",retrace=", &nextopt)) {
2222 opts = nextopt;
2223 if (strstart(opts, "dumb", &nextopt))
2224 vga_retrace_method = VGA_RETRACE_DUMB;
2225 else if (strstart(opts, "precise", &nextopt))
2226 vga_retrace_method = VGA_RETRACE_PRECISE;
2227 else goto invalid_vga;
2228 } else goto invalid_vga;
2229 opts = nextopt;
2233 static DisplayType select_display(const char *p)
2235 const char *opts;
2236 DisplayType display = DT_DEFAULT;
2238 if (strstart(p, "sdl", &opts)) {
2239 #ifdef CONFIG_SDL
2240 display = DT_SDL;
2241 while (*opts) {
2242 const char *nextopt;
2244 if (strstart(opts, ",frame=", &nextopt)) {
2245 opts = nextopt;
2246 if (strstart(opts, "on", &nextopt)) {
2247 no_frame = 0;
2248 } else if (strstart(opts, "off", &nextopt)) {
2249 no_frame = 1;
2250 } else {
2251 goto invalid_sdl_args;
2253 } else if (strstart(opts, ",alt_grab=", &nextopt)) {
2254 opts = nextopt;
2255 if (strstart(opts, "on", &nextopt)) {
2256 alt_grab = 1;
2257 } else if (strstart(opts, "off", &nextopt)) {
2258 alt_grab = 0;
2259 } else {
2260 goto invalid_sdl_args;
2262 } else if (strstart(opts, ",ctrl_grab=", &nextopt)) {
2263 opts = nextopt;
2264 if (strstart(opts, "on", &nextopt)) {
2265 ctrl_grab = 1;
2266 } else if (strstart(opts, "off", &nextopt)) {
2267 ctrl_grab = 0;
2268 } else {
2269 goto invalid_sdl_args;
2271 } else if (strstart(opts, ",window_close=", &nextopt)) {
2272 opts = nextopt;
2273 if (strstart(opts, "on", &nextopt)) {
2274 no_quit = 0;
2275 } else if (strstart(opts, "off", &nextopt)) {
2276 no_quit = 1;
2277 } else {
2278 goto invalid_sdl_args;
2280 } else {
2281 invalid_sdl_args:
2282 fprintf(stderr, "Invalid SDL option string: %s\n", p);
2283 exit(1);
2285 opts = nextopt;
2287 #else
2288 fprintf(stderr, "SDL support is disabled\n");
2289 exit(1);
2290 #endif
2291 } else if (strstart(p, "vnc", &opts)) {
2292 #ifdef CONFIG_VNC
2293 display_remote++;
2295 if (*opts) {
2296 const char *nextopt;
2298 if (strstart(opts, "=", &nextopt)) {
2299 vnc_display = nextopt;
2302 if (!vnc_display) {
2303 fprintf(stderr, "VNC requires a display argument vnc=<display>\n");
2304 exit(1);
2306 #else
2307 fprintf(stderr, "VNC support is disabled\n");
2308 exit(1);
2309 #endif
2310 } else if (strstart(p, "curses", &opts)) {
2311 #ifdef CONFIG_CURSES
2312 display = DT_CURSES;
2313 #else
2314 fprintf(stderr, "Curses support is disabled\n");
2315 exit(1);
2316 #endif
2317 } else if (strstart(p, "gtk", &opts)) {
2318 #ifdef CONFIG_GTK
2319 display = DT_GTK;
2320 while (*opts) {
2321 const char *nextopt;
2323 if (strstart(opts, ",grab_on_hover=", &nextopt)) {
2324 opts = nextopt;
2325 if (strstart(opts, "on", &nextopt)) {
2326 grab_on_hover = true;
2327 } else if (strstart(opts, "off", &nextopt)) {
2328 grab_on_hover = false;
2329 } else {
2330 goto invalid_gtk_args;
2332 } else {
2333 invalid_gtk_args:
2334 fprintf(stderr, "Invalid GTK option string: %s\n", p);
2335 exit(1);
2337 opts = nextopt;
2339 #else
2340 fprintf(stderr, "GTK support is disabled\n");
2341 exit(1);
2342 #endif
2343 } else if (strstart(p, "none", &opts)) {
2344 display = DT_NONE;
2345 } else {
2346 fprintf(stderr, "Unknown display type: %s\n", p);
2347 exit(1);
2350 return display;
2353 static int balloon_parse(const char *arg)
2355 QemuOpts *opts;
2357 if (strcmp(arg, "none") == 0) {
2358 return 0;
2361 if (!strncmp(arg, "virtio", 6)) {
2362 if (arg[6] == ',') {
2363 /* have params -> parse them */
2364 opts = qemu_opts_parse(qemu_find_opts("device"), arg+7, 0);
2365 if (!opts)
2366 return -1;
2367 } else {
2368 /* create empty opts */
2369 opts = qemu_opts_create(qemu_find_opts("device"), NULL, 0,
2370 &error_abort);
2372 qemu_opt_set(opts, "driver", "virtio-balloon");
2373 return 0;
2376 return -1;
2379 char *qemu_find_file(int type, const char *name)
2381 int i;
2382 const char *subdir;
2383 char *buf;
2385 /* Try the name as a straight path first */
2386 if (access(name, R_OK) == 0) {
2387 trace_load_file(name, name);
2388 return g_strdup(name);
2391 switch (type) {
2392 case QEMU_FILE_TYPE_BIOS:
2393 subdir = "";
2394 break;
2395 case QEMU_FILE_TYPE_KEYMAP:
2396 subdir = "keymaps/";
2397 break;
2398 default:
2399 abort();
2402 for (i = 0; i < data_dir_idx; i++) {
2403 buf = g_strdup_printf("%s/%s%s", data_dir[i], subdir, name);
2404 if (access(buf, R_OK) == 0) {
2405 trace_load_file(name, buf);
2406 return buf;
2408 g_free(buf);
2410 return NULL;
2413 static int device_help_func(QemuOpts *opts, void *opaque)
2415 return qdev_device_help(opts);
2418 static int device_init_func(QemuOpts *opts, void *opaque)
2420 DeviceState *dev;
2422 dev = qdev_device_add(opts);
2423 if (!dev)
2424 return -1;
2425 object_unref(OBJECT(dev));
2426 return 0;
2429 static int chardev_init_func(QemuOpts *opts, void *opaque)
2431 Error *local_err = NULL;
2433 qemu_chr_new_from_opts(opts, NULL, &local_err);
2434 if (local_err) {
2435 error_report("%s", error_get_pretty(local_err));
2436 error_free(local_err);
2437 return -1;
2439 return 0;
2442 #ifdef CONFIG_VIRTFS
2443 static int fsdev_init_func(QemuOpts *opts, void *opaque)
2445 int ret;
2446 ret = qemu_fsdev_add(opts);
2448 return ret;
2450 #endif
2452 static int mon_init_func(QemuOpts *opts, void *opaque)
2454 CharDriverState *chr;
2455 const char *chardev;
2456 const char *mode;
2457 int flags;
2459 mode = qemu_opt_get(opts, "mode");
2460 if (mode == NULL) {
2461 mode = "readline";
2463 if (strcmp(mode, "readline") == 0) {
2464 flags = MONITOR_USE_READLINE;
2465 } else if (strcmp(mode, "control") == 0) {
2466 flags = MONITOR_USE_CONTROL;
2467 } else {
2468 fprintf(stderr, "unknown monitor mode \"%s\"\n", mode);
2469 exit(1);
2472 if (qemu_opt_get_bool(opts, "pretty", 0))
2473 flags |= MONITOR_USE_PRETTY;
2475 if (qemu_opt_get_bool(opts, "default", 0))
2476 flags |= MONITOR_IS_DEFAULT;
2478 chardev = qemu_opt_get(opts, "chardev");
2479 chr = qemu_chr_find(chardev);
2480 if (chr == NULL) {
2481 fprintf(stderr, "chardev \"%s\" not found\n", chardev);
2482 exit(1);
2485 qemu_chr_fe_claim_no_fail(chr);
2486 monitor_init(chr, flags);
2487 return 0;
2490 static void monitor_parse(const char *optarg, const char *mode)
2492 static int monitor_device_index = 0;
2493 QemuOpts *opts;
2494 const char *p;
2495 char label[32];
2496 int def = 0;
2498 if (strstart(optarg, "chardev:", &p)) {
2499 snprintf(label, sizeof(label), "%s", p);
2500 } else {
2501 snprintf(label, sizeof(label), "compat_monitor%d",
2502 monitor_device_index);
2503 if (monitor_device_index == 0) {
2504 def = 1;
2506 opts = qemu_chr_parse_compat(label, optarg);
2507 if (!opts) {
2508 fprintf(stderr, "parse error: %s\n", optarg);
2509 exit(1);
2513 opts = qemu_opts_create(qemu_find_opts("mon"), label, 1, NULL);
2514 if (!opts) {
2515 fprintf(stderr, "duplicate chardev: %s\n", label);
2516 exit(1);
2518 qemu_opt_set(opts, "mode", mode);
2519 qemu_opt_set(opts, "chardev", label);
2520 if (def)
2521 qemu_opt_set(opts, "default", "on");
2522 monitor_device_index++;
2525 struct device_config {
2526 enum {
2527 DEV_USB, /* -usbdevice */
2528 DEV_BT, /* -bt */
2529 DEV_SERIAL, /* -serial */
2530 DEV_PARALLEL, /* -parallel */
2531 DEV_VIRTCON, /* -virtioconsole */
2532 DEV_DEBUGCON, /* -debugcon */
2533 DEV_GDB, /* -gdb, -s */
2534 DEV_SCLP, /* s390 sclp */
2535 } type;
2536 const char *cmdline;
2537 Location loc;
2538 QTAILQ_ENTRY(device_config) next;
2541 static QTAILQ_HEAD(, device_config) device_configs =
2542 QTAILQ_HEAD_INITIALIZER(device_configs);
2544 static void add_device_config(int type, const char *cmdline)
2546 struct device_config *conf;
2548 conf = g_malloc0(sizeof(*conf));
2549 conf->type = type;
2550 conf->cmdline = cmdline;
2551 loc_save(&conf->loc);
2552 QTAILQ_INSERT_TAIL(&device_configs, conf, next);
2555 static int foreach_device_config(int type, int (*func)(const char *cmdline))
2557 struct device_config *conf;
2558 int rc;
2560 QTAILQ_FOREACH(conf, &device_configs, next) {
2561 if (conf->type != type)
2562 continue;
2563 loc_push_restore(&conf->loc);
2564 rc = func(conf->cmdline);
2565 loc_pop(&conf->loc);
2566 if (0 != rc)
2567 return rc;
2569 return 0;
2572 static int serial_parse(const char *devname)
2574 static int index = 0;
2575 char label[32];
2577 if (strcmp(devname, "none") == 0)
2578 return 0;
2579 if (index == MAX_SERIAL_PORTS) {
2580 fprintf(stderr, "qemu: too many serial ports\n");
2581 exit(1);
2583 snprintf(label, sizeof(label), "serial%d", index);
2584 serial_hds[index] = qemu_chr_new(label, devname, NULL);
2585 if (!serial_hds[index]) {
2586 fprintf(stderr, "qemu: could not connect serial device"
2587 " to character backend '%s'\n", devname);
2588 return -1;
2590 index++;
2591 return 0;
2594 static int parallel_parse(const char *devname)
2596 static int index = 0;
2597 char label[32];
2599 if (strcmp(devname, "none") == 0)
2600 return 0;
2601 if (index == MAX_PARALLEL_PORTS) {
2602 fprintf(stderr, "qemu: too many parallel ports\n");
2603 exit(1);
2605 snprintf(label, sizeof(label), "parallel%d", index);
2606 parallel_hds[index] = qemu_chr_new(label, devname, NULL);
2607 if (!parallel_hds[index]) {
2608 fprintf(stderr, "qemu: could not connect parallel device"
2609 " to character backend '%s'\n", devname);
2610 return -1;
2612 index++;
2613 return 0;
2616 static int virtcon_parse(const char *devname)
2618 QemuOptsList *device = qemu_find_opts("device");
2619 static int index = 0;
2620 char label[32];
2621 QemuOpts *bus_opts, *dev_opts;
2623 if (strcmp(devname, "none") == 0)
2624 return 0;
2625 if (index == MAX_VIRTIO_CONSOLES) {
2626 fprintf(stderr, "qemu: too many virtio consoles\n");
2627 exit(1);
2630 bus_opts = qemu_opts_create(device, NULL, 0, &error_abort);
2631 if (arch_type == QEMU_ARCH_S390X) {
2632 qemu_opt_set(bus_opts, "driver", "virtio-serial-s390");
2633 } else {
2634 qemu_opt_set(bus_opts, "driver", "virtio-serial-pci");
2637 dev_opts = qemu_opts_create(device, NULL, 0, &error_abort);
2638 qemu_opt_set(dev_opts, "driver", "virtconsole");
2640 snprintf(label, sizeof(label), "virtcon%d", index);
2641 virtcon_hds[index] = qemu_chr_new(label, devname, NULL);
2642 if (!virtcon_hds[index]) {
2643 fprintf(stderr, "qemu: could not connect virtio console"
2644 " to character backend '%s'\n", devname);
2645 return -1;
2647 qemu_opt_set(dev_opts, "chardev", label);
2649 index++;
2650 return 0;
2653 static int sclp_parse(const char *devname)
2655 QemuOptsList *device = qemu_find_opts("device");
2656 static int index = 0;
2657 char label[32];
2658 QemuOpts *dev_opts;
2660 if (strcmp(devname, "none") == 0) {
2661 return 0;
2663 if (index == MAX_SCLP_CONSOLES) {
2664 fprintf(stderr, "qemu: too many sclp consoles\n");
2665 exit(1);
2668 assert(arch_type == QEMU_ARCH_S390X);
2670 dev_opts = qemu_opts_create(device, NULL, 0, NULL);
2671 qemu_opt_set(dev_opts, "driver", "sclpconsole");
2673 snprintf(label, sizeof(label), "sclpcon%d", index);
2674 sclp_hds[index] = qemu_chr_new(label, devname, NULL);
2675 if (!sclp_hds[index]) {
2676 fprintf(stderr, "qemu: could not connect sclp console"
2677 " to character backend '%s'\n", devname);
2678 return -1;
2680 qemu_opt_set(dev_opts, "chardev", label);
2682 index++;
2683 return 0;
2686 static int debugcon_parse(const char *devname)
2688 QemuOpts *opts;
2690 if (!qemu_chr_new("debugcon", devname, NULL)) {
2691 exit(1);
2693 opts = qemu_opts_create(qemu_find_opts("device"), "debugcon", 1, NULL);
2694 if (!opts) {
2695 fprintf(stderr, "qemu: already have a debugcon device\n");
2696 exit(1);
2698 qemu_opt_set(opts, "driver", "isa-debugcon");
2699 qemu_opt_set(opts, "chardev", "debugcon");
2700 return 0;
2703 static MachineClass *machine_parse(const char *name)
2705 MachineClass *mc = NULL;
2706 GSList *el, *machines = object_class_get_list(TYPE_MACHINE, false);
2708 if (name) {
2709 mc = find_machine(name);
2711 if (mc) {
2712 return mc;
2714 if (name && !is_help_option(name)) {
2715 error_report("Unsupported machine type");
2716 error_printf("Use -machine help to list supported machines!\n");
2717 } else {
2718 printf("Supported machines are:\n");
2719 for (el = machines; el; el = el->next) {
2720 MachineClass *mc = el->data;
2721 if (mc->alias) {
2722 printf("%-20s %s (alias of %s)\n", mc->alias, mc->desc, mc->name);
2724 printf("%-20s %s%s\n", mc->name, mc->desc,
2725 mc->is_default ? " (default)" : "");
2729 g_slist_free(machines);
2730 exit(!name || !is_help_option(name));
2733 static int tcg_init(MachineClass *mc)
2735 tcg_exec_init(tcg_tb_size * 1024 * 1024);
2736 return 0;
2739 static struct {
2740 const char *opt_name;
2741 const char *name;
2742 int (*available)(void);
2743 int (*init)(MachineClass *mc);
2744 bool *allowed;
2745 } accel_list[] = {
2746 { "tcg", "tcg", tcg_available, tcg_init, &tcg_allowed },
2747 { "xen", "Xen", xen_available, xen_init, &xen_allowed },
2748 { "kvm", "KVM", kvm_available, kvm_init, &kvm_allowed },
2749 { "qtest", "QTest", qtest_available, qtest_init_accel, &qtest_allowed },
2752 static int configure_accelerator(MachineClass *mc)
2754 const char *p;
2755 char buf[10];
2756 int i, ret;
2757 bool accel_initialised = false;
2758 bool init_failed = false;
2760 p = qemu_opt_get(qemu_get_machine_opts(), "accel");
2761 if (p == NULL) {
2762 /* Use the default "accelerator", tcg */
2763 p = "tcg";
2766 while (!accel_initialised && *p != '\0') {
2767 if (*p == ':') {
2768 p++;
2770 p = get_opt_name(buf, sizeof (buf), p, ':');
2771 for (i = 0; i < ARRAY_SIZE(accel_list); i++) {
2772 if (strcmp(accel_list[i].opt_name, buf) == 0) {
2773 if (!accel_list[i].available()) {
2774 printf("%s not supported for this target\n",
2775 accel_list[i].name);
2776 break;
2778 *(accel_list[i].allowed) = true;
2779 ret = accel_list[i].init(mc);
2780 if (ret < 0) {
2781 init_failed = true;
2782 fprintf(stderr, "failed to initialize %s: %s\n",
2783 accel_list[i].name,
2784 strerror(-ret));
2785 *(accel_list[i].allowed) = false;
2786 } else {
2787 accel_initialised = true;
2789 break;
2792 if (i == ARRAY_SIZE(accel_list)) {
2793 fprintf(stderr, "\"%s\" accelerator does not exist.\n", buf);
2797 if (!accel_initialised) {
2798 if (!init_failed) {
2799 fprintf(stderr, "No accelerator found!\n");
2801 exit(1);
2804 if (init_failed) {
2805 fprintf(stderr, "Back to %s accelerator.\n", accel_list[i].name);
2808 return !accel_initialised;
2811 void qemu_add_exit_notifier(Notifier *notify)
2813 notifier_list_add(&exit_notifiers, notify);
2816 void qemu_remove_exit_notifier(Notifier *notify)
2818 notifier_remove(notify);
2821 static void qemu_run_exit_notifiers(void)
2823 notifier_list_notify(&exit_notifiers, NULL);
2826 void qemu_add_machine_init_done_notifier(Notifier *notify)
2828 notifier_list_add(&machine_init_done_notifiers, notify);
2831 static void qemu_run_machine_init_done_notifiers(void)
2833 notifier_list_notify(&machine_init_done_notifiers, NULL);
2836 static const QEMUOption *lookup_opt(int argc, char **argv,
2837 const char **poptarg, int *poptind)
2839 const QEMUOption *popt;
2840 int optind = *poptind;
2841 char *r = argv[optind];
2842 const char *optarg;
2844 loc_set_cmdline(argv, optind, 1);
2845 optind++;
2846 /* Treat --foo the same as -foo. */
2847 if (r[1] == '-')
2848 r++;
2849 popt = qemu_options;
2850 for(;;) {
2851 if (!popt->name) {
2852 error_report("invalid option");
2853 exit(1);
2855 if (!strcmp(popt->name, r + 1))
2856 break;
2857 popt++;
2859 if (popt->flags & HAS_ARG) {
2860 if (optind >= argc) {
2861 error_report("requires an argument");
2862 exit(1);
2864 optarg = argv[optind++];
2865 loc_set_cmdline(argv, optind - 2, 2);
2866 } else {
2867 optarg = NULL;
2870 *poptarg = optarg;
2871 *poptind = optind;
2873 return popt;
2876 static gpointer malloc_and_trace(gsize n_bytes)
2878 void *ptr = malloc(n_bytes);
2879 trace_g_malloc(n_bytes, ptr);
2880 return ptr;
2883 static gpointer realloc_and_trace(gpointer mem, gsize n_bytes)
2885 void *ptr = realloc(mem, n_bytes);
2886 trace_g_realloc(mem, n_bytes, ptr);
2887 return ptr;
2890 static void free_and_trace(gpointer mem)
2892 trace_g_free(mem);
2893 free(mem);
2896 static int object_set_property(const char *name, const char *value, void *opaque)
2898 Object *obj = OBJECT(opaque);
2899 StringInputVisitor *siv;
2900 Error *local_err = NULL;
2902 if (strcmp(name, "qom-type") == 0 || strcmp(name, "id") == 0 ||
2903 strcmp(name, "type") == 0) {
2904 return 0;
2907 siv = string_input_visitor_new(value);
2908 object_property_set(obj, string_input_get_visitor(siv), name, &local_err);
2909 string_input_visitor_cleanup(siv);
2911 if (local_err) {
2912 qerror_report_err(local_err);
2913 error_free(local_err);
2914 return -1;
2917 return 0;
2920 static int object_create(QemuOpts *opts, void *opaque)
2922 const char *type = qemu_opt_get(opts, "qom-type");
2923 const char *id = qemu_opts_id(opts);
2924 Error *local_err = NULL;
2925 Object *obj;
2927 g_assert(type != NULL);
2929 if (id == NULL) {
2930 qerror_report(QERR_MISSING_PARAMETER, "id");
2931 return -1;
2934 obj = object_new(type);
2935 if (qemu_opt_foreach(opts, object_set_property, obj, 1) < 0) {
2936 object_unref(obj);
2937 return -1;
2940 if (!object_dynamic_cast(obj, TYPE_USER_CREATABLE)) {
2941 error_setg(&local_err, "object '%s' isn't supported by -object",
2942 id);
2943 goto out;
2946 object_property_add_child(container_get(object_get_root(), "/objects"),
2947 id, obj, &local_err);
2949 user_creatable_complete(obj, &local_err);
2950 if (local_err) {
2951 object_property_del(container_get(object_get_root(), "/objects"),
2952 id, &error_abort);
2953 goto out;
2955 out:
2956 object_unref(obj);
2957 if (local_err) {
2958 qerror_report_err(local_err);
2959 error_free(local_err);
2960 return -1;
2962 return 0;
2965 int main(int argc, char **argv, char **envp)
2967 int i;
2968 int snapshot, linux_boot;
2969 const char *icount_option = NULL;
2970 const char *initrd_filename;
2971 const char *kernel_filename, *kernel_cmdline;
2972 const char *boot_order;
2973 DisplayState *ds;
2974 int cyls, heads, secs, translation;
2975 QemuOpts *hda_opts = NULL, *opts, *machine_opts;
2976 QemuOptsList *olist;
2977 int optind;
2978 const char *optarg;
2979 const char *loadvm = NULL;
2980 MachineClass *machine_class;
2981 const char *cpu_model;
2982 const char *vga_model = NULL;
2983 const char *qtest_chrdev = NULL;
2984 const char *qtest_log = NULL;
2985 const char *pid_file = NULL;
2986 const char *incoming = NULL;
2987 #ifdef CONFIG_VNC
2988 int show_vnc_port = 0;
2989 #endif
2990 bool defconfig = true;
2991 bool userconfig = true;
2992 const char *log_mask = NULL;
2993 const char *log_file = NULL;
2994 GMemVTable mem_trace = {
2995 .malloc = malloc_and_trace,
2996 .realloc = realloc_and_trace,
2997 .free = free_and_trace,
2999 const char *trace_events = NULL;
3000 const char *trace_file = NULL;
3001 const ram_addr_t default_ram_size = (ram_addr_t)DEFAULT_RAM_SIZE *
3002 1024 * 1024;
3003 ram_addr_t maxram_size = default_ram_size;
3004 uint64_t ram_slots = 0;
3006 atexit(qemu_run_exit_notifiers);
3007 error_set_progname(argv[0]);
3008 qemu_init_exec_dir(argv[0]);
3010 g_mem_set_vtable(&mem_trace);
3012 module_call_init(MODULE_INIT_QOM);
3014 qemu_add_opts(&qemu_drive_opts);
3015 qemu_add_drive_opts(&qemu_legacy_drive_opts);
3016 qemu_add_drive_opts(&qemu_common_drive_opts);
3017 qemu_add_drive_opts(&qemu_drive_opts);
3018 qemu_add_opts(&qemu_chardev_opts);
3019 qemu_add_opts(&qemu_device_opts);
3020 qemu_add_opts(&qemu_netdev_opts);
3021 qemu_add_opts(&qemu_net_opts);
3022 qemu_add_opts(&qemu_rtc_opts);
3023 qemu_add_opts(&qemu_global_opts);
3024 qemu_add_opts(&qemu_mon_opts);
3025 qemu_add_opts(&qemu_trace_opts);
3026 qemu_add_opts(&qemu_option_rom_opts);
3027 qemu_add_opts(&qemu_machine_opts);
3028 qemu_add_opts(&qemu_mem_opts);
3029 qemu_add_opts(&qemu_smp_opts);
3030 qemu_add_opts(&qemu_boot_opts);
3031 qemu_add_opts(&qemu_sandbox_opts);
3032 qemu_add_opts(&qemu_add_fd_opts);
3033 qemu_add_opts(&qemu_object_opts);
3034 qemu_add_opts(&qemu_tpmdev_opts);
3035 qemu_add_opts(&qemu_realtime_opts);
3036 qemu_add_opts(&qemu_msg_opts);
3037 qemu_add_opts(&qemu_name_opts);
3039 runstate_init();
3041 rtc_clock = QEMU_CLOCK_HOST;
3043 qemu_init_auxval(envp);
3044 qemu_cache_utils_init();
3046 QLIST_INIT (&vm_change_state_head);
3047 os_setup_early_signal_handling();
3049 module_call_init(MODULE_INIT_MACHINE);
3050 machine_class = find_default_machine();
3051 cpu_model = NULL;
3052 ram_size = default_ram_size;
3053 snapshot = 0;
3054 cyls = heads = secs = 0;
3055 translation = BIOS_ATA_TRANSLATION_AUTO;
3057 for (i = 0; i < MAX_NODES; i++) {
3058 node_mem[i] = 0;
3059 node_cpumask[i] = bitmap_new(MAX_CPUMASK_BITS);
3062 nb_numa_nodes = 0;
3063 nb_nics = 0;
3065 bdrv_init_with_whitelist();
3067 autostart = 1;
3069 /* first pass of option parsing */
3070 optind = 1;
3071 while (optind < argc) {
3072 if (argv[optind][0] != '-') {
3073 /* disk image */
3074 optind++;
3075 } else {
3076 const QEMUOption *popt;
3078 popt = lookup_opt(argc, argv, &optarg, &optind);
3079 switch (popt->index) {
3080 case QEMU_OPTION_nodefconfig:
3081 defconfig = false;
3082 break;
3083 case QEMU_OPTION_nouserconfig:
3084 userconfig = false;
3085 break;
3090 if (defconfig) {
3091 int ret;
3092 ret = qemu_read_default_config_files(userconfig);
3093 if (ret < 0) {
3094 exit(1);
3098 /* second pass of option parsing */
3099 optind = 1;
3100 for(;;) {
3101 if (optind >= argc)
3102 break;
3103 if (argv[optind][0] != '-') {
3104 hda_opts = drive_add(IF_DEFAULT, 0, argv[optind++], HD_OPTS);
3105 } else {
3106 const QEMUOption *popt;
3108 popt = lookup_opt(argc, argv, &optarg, &optind);
3109 if (!(popt->arch_mask & arch_type)) {
3110 printf("Option %s not supported for this target\n", popt->name);
3111 exit(1);
3113 switch(popt->index) {
3114 case QEMU_OPTION_M:
3115 machine_class = machine_parse(optarg);
3116 break;
3117 case QEMU_OPTION_no_kvm_irqchip: {
3118 olist = qemu_find_opts("machine");
3119 qemu_opts_parse(olist, "kernel_irqchip=off", 0);
3120 break;
3122 case QEMU_OPTION_cpu:
3123 /* hw initialization will check this */
3124 cpu_model = optarg;
3125 break;
3126 case QEMU_OPTION_hda:
3128 char buf[256];
3129 if (cyls == 0)
3130 snprintf(buf, sizeof(buf), "%s", HD_OPTS);
3131 else
3132 snprintf(buf, sizeof(buf),
3133 "%s,cyls=%d,heads=%d,secs=%d%s",
3134 HD_OPTS , cyls, heads, secs,
3135 translation == BIOS_ATA_TRANSLATION_LBA ?
3136 ",trans=lba" :
3137 translation == BIOS_ATA_TRANSLATION_NONE ?
3138 ",trans=none" : "");
3139 drive_add(IF_DEFAULT, 0, optarg, buf);
3140 break;
3142 case QEMU_OPTION_hdb:
3143 case QEMU_OPTION_hdc:
3144 case QEMU_OPTION_hdd:
3145 drive_add(IF_DEFAULT, popt->index - QEMU_OPTION_hda, optarg,
3146 HD_OPTS);
3147 break;
3148 case QEMU_OPTION_drive:
3149 if (drive_def(optarg) == NULL) {
3150 exit(1);
3152 break;
3153 case QEMU_OPTION_set:
3154 if (qemu_set_option(optarg) != 0)
3155 exit(1);
3156 break;
3157 case QEMU_OPTION_global:
3158 if (qemu_global_option(optarg) != 0)
3159 exit(1);
3160 break;
3161 case QEMU_OPTION_mtdblock:
3162 drive_add(IF_MTD, -1, optarg, MTD_OPTS);
3163 break;
3164 case QEMU_OPTION_sd:
3165 drive_add(IF_SD, -1, optarg, SD_OPTS);
3166 break;
3167 case QEMU_OPTION_pflash:
3168 drive_add(IF_PFLASH, -1, optarg, PFLASH_OPTS);
3169 break;
3170 case QEMU_OPTION_snapshot:
3171 snapshot = 1;
3172 break;
3173 case QEMU_OPTION_hdachs:
3175 const char *p;
3176 p = optarg;
3177 cyls = strtol(p, (char **)&p, 0);
3178 if (cyls < 1 || cyls > 16383)
3179 goto chs_fail;
3180 if (*p != ',')
3181 goto chs_fail;
3182 p++;
3183 heads = strtol(p, (char **)&p, 0);
3184 if (heads < 1 || heads > 16)
3185 goto chs_fail;
3186 if (*p != ',')
3187 goto chs_fail;
3188 p++;
3189 secs = strtol(p, (char **)&p, 0);
3190 if (secs < 1 || secs > 63)
3191 goto chs_fail;
3192 if (*p == ',') {
3193 p++;
3194 if (!strcmp(p, "large")) {
3195 translation = BIOS_ATA_TRANSLATION_LARGE;
3196 } else if (!strcmp(p, "rechs")) {
3197 translation = BIOS_ATA_TRANSLATION_RECHS;
3198 } else if (!strcmp(p, "none")) {
3199 translation = BIOS_ATA_TRANSLATION_NONE;
3200 } else if (!strcmp(p, "lba")) {
3201 translation = BIOS_ATA_TRANSLATION_LBA;
3202 } else if (!strcmp(p, "auto")) {
3203 translation = BIOS_ATA_TRANSLATION_AUTO;
3204 } else {
3205 goto chs_fail;
3207 } else if (*p != '\0') {
3208 chs_fail:
3209 fprintf(stderr, "qemu: invalid physical CHS format\n");
3210 exit(1);
3212 if (hda_opts != NULL) {
3213 char num[16];
3214 snprintf(num, sizeof(num), "%d", cyls);
3215 qemu_opt_set(hda_opts, "cyls", num);
3216 snprintf(num, sizeof(num), "%d", heads);
3217 qemu_opt_set(hda_opts, "heads", num);
3218 snprintf(num, sizeof(num), "%d", secs);
3219 qemu_opt_set(hda_opts, "secs", num);
3220 if (translation == BIOS_ATA_TRANSLATION_LARGE) {
3221 qemu_opt_set(hda_opts, "trans", "large");
3222 } else if (translation == BIOS_ATA_TRANSLATION_RECHS) {
3223 qemu_opt_set(hda_opts, "trans", "rechs");
3224 } else if (translation == BIOS_ATA_TRANSLATION_LBA) {
3225 qemu_opt_set(hda_opts, "trans", "lba");
3226 } else if (translation == BIOS_ATA_TRANSLATION_NONE) {
3227 qemu_opt_set(hda_opts, "trans", "none");
3231 break;
3232 case QEMU_OPTION_numa:
3233 numa_add(optarg);
3234 break;
3235 case QEMU_OPTION_display:
3236 display_type = select_display(optarg);
3237 break;
3238 case QEMU_OPTION_nographic:
3239 display_type = DT_NOGRAPHIC;
3240 break;
3241 case QEMU_OPTION_curses:
3242 #ifdef CONFIG_CURSES
3243 display_type = DT_CURSES;
3244 #else
3245 fprintf(stderr, "Curses support is disabled\n");
3246 exit(1);
3247 #endif
3248 break;
3249 case QEMU_OPTION_portrait:
3250 graphic_rotate = 90;
3251 break;
3252 case QEMU_OPTION_rotate:
3253 graphic_rotate = strtol(optarg, (char **) &optarg, 10);
3254 if (graphic_rotate != 0 && graphic_rotate != 90 &&
3255 graphic_rotate != 180 && graphic_rotate != 270) {
3256 fprintf(stderr,
3257 "qemu: only 90, 180, 270 deg rotation is available\n");
3258 exit(1);
3260 break;
3261 case QEMU_OPTION_kernel:
3262 qemu_opts_set(qemu_find_opts("machine"), 0, "kernel", optarg);
3263 break;
3264 case QEMU_OPTION_initrd:
3265 qemu_opts_set(qemu_find_opts("machine"), 0, "initrd", optarg);
3266 break;
3267 case QEMU_OPTION_append:
3268 qemu_opts_set(qemu_find_opts("machine"), 0, "append", optarg);
3269 break;
3270 case QEMU_OPTION_dtb:
3271 qemu_opts_set(qemu_find_opts("machine"), 0, "dtb", optarg);
3272 break;
3273 case QEMU_OPTION_cdrom:
3274 drive_add(IF_DEFAULT, 2, optarg, CDROM_OPTS);
3275 break;
3276 case QEMU_OPTION_boot:
3277 opts = qemu_opts_parse(qemu_find_opts("boot-opts"), optarg, 1);
3278 if (!opts) {
3279 exit(1);
3281 break;
3282 case QEMU_OPTION_fda:
3283 case QEMU_OPTION_fdb:
3284 drive_add(IF_FLOPPY, popt->index - QEMU_OPTION_fda,
3285 optarg, FD_OPTS);
3286 break;
3287 case QEMU_OPTION_no_fd_bootchk:
3288 fd_bootchk = 0;
3289 break;
3290 case QEMU_OPTION_netdev:
3291 if (net_client_parse(qemu_find_opts("netdev"), optarg) == -1) {
3292 exit(1);
3294 break;
3295 case QEMU_OPTION_net:
3296 if (net_client_parse(qemu_find_opts("net"), optarg) == -1) {
3297 exit(1);
3299 break;
3300 #ifdef CONFIG_LIBISCSI
3301 case QEMU_OPTION_iscsi:
3302 opts = qemu_opts_parse(qemu_find_opts("iscsi"), optarg, 0);
3303 if (!opts) {
3304 exit(1);
3306 break;
3307 #endif
3308 #ifdef CONFIG_SLIRP
3309 case QEMU_OPTION_tftp:
3310 legacy_tftp_prefix = optarg;
3311 break;
3312 case QEMU_OPTION_bootp:
3313 legacy_bootp_filename = optarg;
3314 break;
3315 case QEMU_OPTION_redir:
3316 if (net_slirp_redir(optarg) < 0)
3317 exit(1);
3318 break;
3319 #endif
3320 case QEMU_OPTION_bt:
3321 add_device_config(DEV_BT, optarg);
3322 break;
3323 case QEMU_OPTION_audio_help:
3324 AUD_help ();
3325 exit (0);
3326 break;
3327 case QEMU_OPTION_soundhw:
3328 select_soundhw (optarg);
3329 break;
3330 case QEMU_OPTION_h:
3331 help(0);
3332 break;
3333 case QEMU_OPTION_version:
3334 version();
3335 exit(0);
3336 break;
3337 case QEMU_OPTION_m: {
3338 uint64_t sz;
3339 const char *mem_str;
3340 const char *maxmem_str, *slots_str;
3342 opts = qemu_opts_parse(qemu_find_opts("memory"),
3343 optarg, 1);
3344 if (!opts) {
3345 exit(EXIT_FAILURE);
3348 mem_str = qemu_opt_get(opts, "size");
3349 if (!mem_str) {
3350 error_report("invalid -m option, missing 'size' option");
3351 exit(EXIT_FAILURE);
3353 if (!*mem_str) {
3354 error_report("missing 'size' option value");
3355 exit(EXIT_FAILURE);
3358 sz = qemu_opt_get_size(opts, "size", ram_size);
3360 /* Fix up legacy suffix-less format */
3361 if (g_ascii_isdigit(mem_str[strlen(mem_str) - 1])) {
3362 uint64_t overflow_check = sz;
3364 sz <<= 20;
3365 if ((sz >> 20) != overflow_check) {
3366 error_report("too large 'size' option value");
3367 exit(EXIT_FAILURE);
3371 /* backward compatibility behaviour for case "-m 0" */
3372 if (sz == 0) {
3373 sz = default_ram_size;
3376 sz = QEMU_ALIGN_UP(sz, 8192);
3377 ram_size = sz;
3378 if (ram_size != sz) {
3379 error_report("ram size too large");
3380 exit(EXIT_FAILURE);
3383 maxmem_str = qemu_opt_get(opts, "maxmem");
3384 slots_str = qemu_opt_get(opts, "slots");
3385 if (maxmem_str && slots_str) {
3386 uint64_t slots;
3388 sz = qemu_opt_get_size(opts, "maxmem", 0);
3389 if (sz < ram_size) {
3390 fprintf(stderr, "qemu: invalid -m option value: maxmem "
3391 "(%" PRIu64 ") <= initial memory (%"
3392 PRIu64 ")\n", sz, ram_size);
3393 exit(EXIT_FAILURE);
3396 slots = qemu_opt_get_number(opts, "slots", 0);
3397 if ((sz > ram_size) && !slots) {
3398 fprintf(stderr, "qemu: invalid -m option value: maxmem "
3399 "(%" PRIu64 ") more than initial memory (%"
3400 PRIu64 ") but no hotplug slots where "
3401 "specified\n", sz, ram_size);
3402 exit(EXIT_FAILURE);
3405 if ((sz <= ram_size) && slots) {
3406 fprintf(stderr, "qemu: invalid -m option value: %"
3407 PRIu64 " hotplug slots where specified but "
3408 "maxmem (%" PRIu64 ") <= initial memory (%"
3409 PRIu64 ")\n", slots, sz, ram_size);
3410 exit(EXIT_FAILURE);
3412 maxram_size = sz;
3413 ram_slots = slots;
3414 } else if ((!maxmem_str && slots_str) ||
3415 (maxmem_str && !slots_str)) {
3416 fprintf(stderr, "qemu: invalid -m option value: missing "
3417 "'%s' option\n", slots_str ? "maxmem" : "slots");
3418 exit(EXIT_FAILURE);
3420 break;
3422 #ifdef CONFIG_TPM
3423 case QEMU_OPTION_tpmdev:
3424 if (tpm_config_parse(qemu_find_opts("tpmdev"), optarg) < 0) {
3425 exit(1);
3427 break;
3428 #endif
3429 case QEMU_OPTION_mempath:
3430 mem_path = optarg;
3431 break;
3432 case QEMU_OPTION_mem_prealloc:
3433 mem_prealloc = 1;
3434 break;
3435 case QEMU_OPTION_d:
3436 log_mask = optarg;
3437 break;
3438 case QEMU_OPTION_D:
3439 log_file = optarg;
3440 break;
3441 case QEMU_OPTION_s:
3442 add_device_config(DEV_GDB, "tcp::" DEFAULT_GDBSTUB_PORT);
3443 break;
3444 case QEMU_OPTION_gdb:
3445 add_device_config(DEV_GDB, optarg);
3446 break;
3447 case QEMU_OPTION_L:
3448 if (data_dir_idx < ARRAY_SIZE(data_dir)) {
3449 data_dir[data_dir_idx++] = optarg;
3451 break;
3452 case QEMU_OPTION_bios:
3453 qemu_opts_set(qemu_find_opts("machine"), 0, "firmware", optarg);
3454 break;
3455 case QEMU_OPTION_singlestep:
3456 singlestep = 1;
3457 break;
3458 case QEMU_OPTION_S:
3459 autostart = 0;
3460 break;
3461 case QEMU_OPTION_k:
3462 keyboard_layout = optarg;
3463 break;
3464 case QEMU_OPTION_localtime:
3465 rtc_utc = 0;
3466 break;
3467 case QEMU_OPTION_vga:
3468 vga_model = optarg;
3469 default_vga = 0;
3470 break;
3471 case QEMU_OPTION_g:
3473 const char *p;
3474 int w, h, depth;
3475 p = optarg;
3476 w = strtol(p, (char **)&p, 10);
3477 if (w <= 0) {
3478 graphic_error:
3479 fprintf(stderr, "qemu: invalid resolution or depth\n");
3480 exit(1);
3482 if (*p != 'x')
3483 goto graphic_error;
3484 p++;
3485 h = strtol(p, (char **)&p, 10);
3486 if (h <= 0)
3487 goto graphic_error;
3488 if (*p == 'x') {
3489 p++;
3490 depth = strtol(p, (char **)&p, 10);
3491 if (depth != 8 && depth != 15 && depth != 16 &&
3492 depth != 24 && depth != 32)
3493 goto graphic_error;
3494 } else if (*p == '\0') {
3495 depth = graphic_depth;
3496 } else {
3497 goto graphic_error;
3500 graphic_width = w;
3501 graphic_height = h;
3502 graphic_depth = depth;
3504 break;
3505 case QEMU_OPTION_echr:
3507 char *r;
3508 term_escape_char = strtol(optarg, &r, 0);
3509 if (r == optarg)
3510 printf("Bad argument to echr\n");
3511 break;
3513 case QEMU_OPTION_monitor:
3514 default_monitor = 0;
3515 if (strncmp(optarg, "none", 4)) {
3516 monitor_parse(optarg, "readline");
3518 break;
3519 case QEMU_OPTION_qmp:
3520 monitor_parse(optarg, "control");
3521 default_monitor = 0;
3522 break;
3523 case QEMU_OPTION_mon:
3524 opts = qemu_opts_parse(qemu_find_opts("mon"), optarg, 1);
3525 if (!opts) {
3526 exit(1);
3528 default_monitor = 0;
3529 break;
3530 case QEMU_OPTION_chardev:
3531 opts = qemu_opts_parse(qemu_find_opts("chardev"), optarg, 1);
3532 if (!opts) {
3533 exit(1);
3535 break;
3536 case QEMU_OPTION_fsdev:
3537 olist = qemu_find_opts("fsdev");
3538 if (!olist) {
3539 fprintf(stderr, "fsdev is not supported by this qemu build.\n");
3540 exit(1);
3542 opts = qemu_opts_parse(olist, optarg, 1);
3543 if (!opts) {
3544 exit(1);
3546 break;
3547 case QEMU_OPTION_virtfs: {
3548 QemuOpts *fsdev;
3549 QemuOpts *device;
3550 const char *writeout, *sock_fd, *socket;
3552 olist = qemu_find_opts("virtfs");
3553 if (!olist) {
3554 fprintf(stderr, "virtfs is not supported by this qemu build.\n");
3555 exit(1);
3557 opts = qemu_opts_parse(olist, optarg, 1);
3558 if (!opts) {
3559 exit(1);
3562 if (qemu_opt_get(opts, "fsdriver") == NULL ||
3563 qemu_opt_get(opts, "mount_tag") == NULL) {
3564 fprintf(stderr, "Usage: -virtfs fsdriver,mount_tag=tag.\n");
3565 exit(1);
3567 fsdev = qemu_opts_create(qemu_find_opts("fsdev"),
3568 qemu_opt_get(opts, "mount_tag"),
3569 1, NULL);
3570 if (!fsdev) {
3571 fprintf(stderr, "duplicate fsdev id: %s\n",
3572 qemu_opt_get(opts, "mount_tag"));
3573 exit(1);
3576 writeout = qemu_opt_get(opts, "writeout");
3577 if (writeout) {
3578 #ifdef CONFIG_SYNC_FILE_RANGE
3579 qemu_opt_set(fsdev, "writeout", writeout);
3580 #else
3581 fprintf(stderr, "writeout=immediate not supported on "
3582 "this platform\n");
3583 exit(1);
3584 #endif
3586 qemu_opt_set(fsdev, "fsdriver", qemu_opt_get(opts, "fsdriver"));
3587 qemu_opt_set(fsdev, "path", qemu_opt_get(opts, "path"));
3588 qemu_opt_set(fsdev, "security_model",
3589 qemu_opt_get(opts, "security_model"));
3590 socket = qemu_opt_get(opts, "socket");
3591 if (socket) {
3592 qemu_opt_set(fsdev, "socket", socket);
3594 sock_fd = qemu_opt_get(opts, "sock_fd");
3595 if (sock_fd) {
3596 qemu_opt_set(fsdev, "sock_fd", sock_fd);
3599 qemu_opt_set_bool(fsdev, "readonly",
3600 qemu_opt_get_bool(opts, "readonly", 0));
3601 device = qemu_opts_create(qemu_find_opts("device"), NULL, 0,
3602 &error_abort);
3603 qemu_opt_set(device, "driver", "virtio-9p-pci");
3604 qemu_opt_set(device, "fsdev",
3605 qemu_opt_get(opts, "mount_tag"));
3606 qemu_opt_set(device, "mount_tag",
3607 qemu_opt_get(opts, "mount_tag"));
3608 break;
3610 case QEMU_OPTION_virtfs_synth: {
3611 QemuOpts *fsdev;
3612 QemuOpts *device;
3614 fsdev = qemu_opts_create(qemu_find_opts("fsdev"), "v_synth",
3615 1, NULL);
3616 if (!fsdev) {
3617 fprintf(stderr, "duplicate option: %s\n", "virtfs_synth");
3618 exit(1);
3620 qemu_opt_set(fsdev, "fsdriver", "synth");
3622 device = qemu_opts_create(qemu_find_opts("device"), NULL, 0,
3623 &error_abort);
3624 qemu_opt_set(device, "driver", "virtio-9p-pci");
3625 qemu_opt_set(device, "fsdev", "v_synth");
3626 qemu_opt_set(device, "mount_tag", "v_synth");
3627 break;
3629 case QEMU_OPTION_serial:
3630 add_device_config(DEV_SERIAL, optarg);
3631 default_serial = 0;
3632 if (strncmp(optarg, "mon:", 4) == 0) {
3633 default_monitor = 0;
3635 break;
3636 case QEMU_OPTION_watchdog:
3637 if (watchdog) {
3638 fprintf(stderr,
3639 "qemu: only one watchdog option may be given\n");
3640 return 1;
3642 watchdog = optarg;
3643 break;
3644 case QEMU_OPTION_watchdog_action:
3645 if (select_watchdog_action(optarg) == -1) {
3646 fprintf(stderr, "Unknown -watchdog-action parameter\n");
3647 exit(1);
3649 break;
3650 case QEMU_OPTION_virtiocon:
3651 add_device_config(DEV_VIRTCON, optarg);
3652 default_virtcon = 0;
3653 if (strncmp(optarg, "mon:", 4) == 0) {
3654 default_monitor = 0;
3656 break;
3657 case QEMU_OPTION_parallel:
3658 add_device_config(DEV_PARALLEL, optarg);
3659 default_parallel = 0;
3660 if (strncmp(optarg, "mon:", 4) == 0) {
3661 default_monitor = 0;
3663 break;
3664 case QEMU_OPTION_debugcon:
3665 add_device_config(DEV_DEBUGCON, optarg);
3666 break;
3667 case QEMU_OPTION_loadvm:
3668 loadvm = optarg;
3669 break;
3670 case QEMU_OPTION_full_screen:
3671 full_screen = 1;
3672 break;
3673 case QEMU_OPTION_no_frame:
3674 no_frame = 1;
3675 break;
3676 case QEMU_OPTION_alt_grab:
3677 alt_grab = 1;
3678 break;
3679 case QEMU_OPTION_ctrl_grab:
3680 ctrl_grab = 1;
3681 break;
3682 case QEMU_OPTION_no_quit:
3683 no_quit = 1;
3684 break;
3685 case QEMU_OPTION_sdl:
3686 #ifdef CONFIG_SDL
3687 display_type = DT_SDL;
3688 break;
3689 #else
3690 fprintf(stderr, "SDL support is disabled\n");
3691 exit(1);
3692 #endif
3693 case QEMU_OPTION_pidfile:
3694 pid_file = optarg;
3695 break;
3696 case QEMU_OPTION_win2k_hack:
3697 win2k_install_hack = 1;
3698 break;
3699 case QEMU_OPTION_rtc_td_hack: {
3700 static GlobalProperty slew_lost_ticks[] = {
3702 .driver = "mc146818rtc",
3703 .property = "lost_tick_policy",
3704 .value = "slew",
3706 { /* end of list */ }
3709 qdev_prop_register_global_list(slew_lost_ticks);
3710 break;
3712 case QEMU_OPTION_acpitable:
3713 opts = qemu_opts_parse(qemu_find_opts("acpi"), optarg, 1);
3714 if (!opts) {
3715 exit(1);
3717 do_acpitable_option(opts);
3718 break;
3719 case QEMU_OPTION_smbios:
3720 opts = qemu_opts_parse(qemu_find_opts("smbios"), optarg, 0);
3721 if (!opts) {
3722 exit(1);
3724 do_smbios_option(opts);
3725 break;
3726 case QEMU_OPTION_enable_kvm:
3727 olist = qemu_find_opts("machine");
3728 qemu_opts_parse(olist, "accel=kvm", 0);
3729 break;
3730 case QEMU_OPTION_machine:
3731 olist = qemu_find_opts("machine");
3732 opts = qemu_opts_parse(olist, optarg, 1);
3733 if (!opts) {
3734 exit(1);
3736 optarg = qemu_opt_get(opts, "type");
3737 if (optarg) {
3738 machine_class = machine_parse(optarg);
3740 break;
3741 case QEMU_OPTION_no_kvm:
3742 olist = qemu_find_opts("machine");
3743 qemu_opts_parse(olist, "accel=tcg", 0);
3744 break;
3745 case QEMU_OPTION_no_kvm_pit: {
3746 fprintf(stderr, "Warning: KVM PIT can no longer be disabled "
3747 "separately.\n");
3748 break;
3750 case QEMU_OPTION_no_kvm_pit_reinjection: {
3751 static GlobalProperty kvm_pit_lost_tick_policy[] = {
3753 .driver = "kvm-pit",
3754 .property = "lost_tick_policy",
3755 .value = "discard",
3757 { /* end of list */ }
3760 fprintf(stderr, "Warning: option deprecated, use "
3761 "lost_tick_policy property of kvm-pit instead.\n");
3762 qdev_prop_register_global_list(kvm_pit_lost_tick_policy);
3763 break;
3765 case QEMU_OPTION_usb:
3766 olist = qemu_find_opts("machine");
3767 qemu_opts_parse(olist, "usb=on", 0);
3768 break;
3769 case QEMU_OPTION_usbdevice:
3770 olist = qemu_find_opts("machine");
3771 qemu_opts_parse(olist, "usb=on", 0);
3772 add_device_config(DEV_USB, optarg);
3773 break;
3774 case QEMU_OPTION_device:
3775 if (!qemu_opts_parse(qemu_find_opts("device"), optarg, 1)) {
3776 exit(1);
3778 break;
3779 case QEMU_OPTION_smp:
3780 if (!qemu_opts_parse(qemu_find_opts("smp-opts"), optarg, 1)) {
3781 exit(1);
3783 break;
3784 case QEMU_OPTION_vnc:
3785 #ifdef CONFIG_VNC
3786 display_remote++;
3787 vnc_display = optarg;
3788 #else
3789 fprintf(stderr, "VNC support is disabled\n");
3790 exit(1);
3791 #endif
3792 break;
3793 case QEMU_OPTION_no_acpi:
3794 acpi_enabled = 0;
3795 break;
3796 case QEMU_OPTION_no_hpet:
3797 no_hpet = 1;
3798 break;
3799 case QEMU_OPTION_balloon:
3800 if (balloon_parse(optarg) < 0) {
3801 fprintf(stderr, "Unknown -balloon argument %s\n", optarg);
3802 exit(1);
3804 break;
3805 case QEMU_OPTION_no_reboot:
3806 no_reboot = 1;
3807 break;
3808 case QEMU_OPTION_no_shutdown:
3809 no_shutdown = 1;
3810 break;
3811 case QEMU_OPTION_show_cursor:
3812 cursor_hide = 0;
3813 break;
3814 case QEMU_OPTION_uuid:
3815 if(qemu_uuid_parse(optarg, qemu_uuid) < 0) {
3816 fprintf(stderr, "Fail to parse UUID string."
3817 " Wrong format.\n");
3818 exit(1);
3820 qemu_uuid_set = true;
3821 break;
3822 case QEMU_OPTION_option_rom:
3823 if (nb_option_roms >= MAX_OPTION_ROMS) {
3824 fprintf(stderr, "Too many option ROMs\n");
3825 exit(1);
3827 opts = qemu_opts_parse(qemu_find_opts("option-rom"), optarg, 1);
3828 if (!opts) {
3829 exit(1);
3831 option_rom[nb_option_roms].name = qemu_opt_get(opts, "romfile");
3832 option_rom[nb_option_roms].bootindex =
3833 qemu_opt_get_number(opts, "bootindex", -1);
3834 if (!option_rom[nb_option_roms].name) {
3835 fprintf(stderr, "Option ROM file is not specified\n");
3836 exit(1);
3838 nb_option_roms++;
3839 break;
3840 case QEMU_OPTION_semihosting:
3841 semihosting_enabled = 1;
3842 break;
3843 case QEMU_OPTION_tdf:
3844 fprintf(stderr, "Warning: user space PIT time drift fix "
3845 "is no longer supported.\n");
3846 break;
3847 case QEMU_OPTION_name:
3848 opts = qemu_opts_parse(qemu_find_opts("name"), optarg, 1);
3849 if (!opts) {
3850 exit(1);
3852 break;
3853 case QEMU_OPTION_prom_env:
3854 if (nb_prom_envs >= MAX_PROM_ENVS) {
3855 fprintf(stderr, "Too many prom variables\n");
3856 exit(1);
3858 prom_envs[nb_prom_envs] = optarg;
3859 nb_prom_envs++;
3860 break;
3861 case QEMU_OPTION_old_param:
3862 old_param = 1;
3863 break;
3864 case QEMU_OPTION_clock:
3865 /* Clock options no longer exist. Keep this option for
3866 * backward compatibility.
3868 break;
3869 case QEMU_OPTION_startdate:
3870 configure_rtc_date_offset(optarg, 1);
3871 break;
3872 case QEMU_OPTION_rtc:
3873 opts = qemu_opts_parse(qemu_find_opts("rtc"), optarg, 0);
3874 if (!opts) {
3875 exit(1);
3877 configure_rtc(opts);
3878 break;
3879 case QEMU_OPTION_tb_size:
3880 tcg_tb_size = strtol(optarg, NULL, 0);
3881 if (tcg_tb_size < 0) {
3882 tcg_tb_size = 0;
3884 break;
3885 case QEMU_OPTION_icount:
3886 icount_option = optarg;
3887 break;
3888 case QEMU_OPTION_incoming:
3889 incoming = optarg;
3890 runstate_set(RUN_STATE_INMIGRATE);
3891 break;
3892 case QEMU_OPTION_nodefaults:
3893 has_defaults = 0;
3894 break;
3895 case QEMU_OPTION_xen_domid:
3896 if (!(xen_available())) {
3897 printf("Option %s not supported for this target\n", popt->name);
3898 exit(1);
3900 xen_domid = atoi(optarg);
3901 break;
3902 case QEMU_OPTION_xen_create:
3903 if (!(xen_available())) {
3904 printf("Option %s not supported for this target\n", popt->name);
3905 exit(1);
3907 xen_mode = XEN_CREATE;
3908 break;
3909 case QEMU_OPTION_xen_attach:
3910 if (!(xen_available())) {
3911 printf("Option %s not supported for this target\n", popt->name);
3912 exit(1);
3914 xen_mode = XEN_ATTACH;
3915 break;
3916 case QEMU_OPTION_trace:
3918 opts = qemu_opts_parse(qemu_find_opts("trace"), optarg, 0);
3919 if (!opts) {
3920 exit(1);
3922 trace_events = qemu_opt_get(opts, "events");
3923 trace_file = qemu_opt_get(opts, "file");
3924 break;
3926 case QEMU_OPTION_readconfig:
3928 int ret = qemu_read_config_file(optarg);
3929 if (ret < 0) {
3930 fprintf(stderr, "read config %s: %s\n", optarg,
3931 strerror(-ret));
3932 exit(1);
3934 break;
3936 case QEMU_OPTION_spice:
3937 olist = qemu_find_opts("spice");
3938 if (!olist) {
3939 fprintf(stderr, "spice is not supported by this qemu build.\n");
3940 exit(1);
3942 opts = qemu_opts_parse(olist, optarg, 0);
3943 if (!opts) {
3944 exit(1);
3946 display_remote++;
3947 break;
3948 case QEMU_OPTION_writeconfig:
3950 FILE *fp;
3951 if (strcmp(optarg, "-") == 0) {
3952 fp = stdout;
3953 } else {
3954 fp = fopen(optarg, "w");
3955 if (fp == NULL) {
3956 fprintf(stderr, "open %s: %s\n", optarg, strerror(errno));
3957 exit(1);
3960 qemu_config_write(fp);
3961 if (fp != stdout) {
3962 fclose(fp);
3964 break;
3966 case QEMU_OPTION_qtest:
3967 qtest_chrdev = optarg;
3968 break;
3969 case QEMU_OPTION_qtest_log:
3970 qtest_log = optarg;
3971 break;
3972 case QEMU_OPTION_sandbox:
3973 opts = qemu_opts_parse(qemu_find_opts("sandbox"), optarg, 1);
3974 if (!opts) {
3975 exit(1);
3977 break;
3978 case QEMU_OPTION_add_fd:
3979 #ifndef _WIN32
3980 opts = qemu_opts_parse(qemu_find_opts("add-fd"), optarg, 0);
3981 if (!opts) {
3982 exit(1);
3984 #else
3985 error_report("File descriptor passing is disabled on this "
3986 "platform");
3987 exit(1);
3988 #endif
3989 break;
3990 case QEMU_OPTION_object:
3991 opts = qemu_opts_parse(qemu_find_opts("object"), optarg, 1);
3992 if (!opts) {
3993 exit(1);
3995 break;
3996 case QEMU_OPTION_realtime:
3997 opts = qemu_opts_parse(qemu_find_opts("realtime"), optarg, 0);
3998 if (!opts) {
3999 exit(1);
4001 configure_realtime(opts);
4002 break;
4003 case QEMU_OPTION_msg:
4004 opts = qemu_opts_parse(qemu_find_opts("msg"), optarg, 0);
4005 if (!opts) {
4006 exit(1);
4008 configure_msg(opts);
4009 break;
4010 default:
4011 os_parse_cmd_args(popt->index, optarg);
4015 loc_set_none();
4017 os_daemonize();
4019 if (qemu_init_main_loop()) {
4020 fprintf(stderr, "qemu_init_main_loop failed\n");
4021 exit(1);
4024 if (qemu_opts_foreach(qemu_find_opts("sandbox"), parse_sandbox, NULL, 0)) {
4025 exit(1);
4028 if (qemu_opts_foreach(qemu_find_opts("name"), parse_name, NULL, 1)) {
4029 exit(1);
4032 #ifndef _WIN32
4033 if (qemu_opts_foreach(qemu_find_opts("add-fd"), parse_add_fd, NULL, 1)) {
4034 exit(1);
4037 if (qemu_opts_foreach(qemu_find_opts("add-fd"), cleanup_add_fd, NULL, 1)) {
4038 exit(1);
4040 #endif
4042 if (machine_class == NULL) {
4043 fprintf(stderr, "No machine specified, and there is no default.\n"
4044 "Use -machine help to list supported machines!\n");
4045 exit(1);
4048 current_machine = MACHINE(object_new(object_class_get_name(
4049 OBJECT_CLASS(machine_class))));
4050 object_property_add_child(object_get_root(), "machine",
4051 OBJECT(current_machine), &error_abort);
4053 if (machine_class->hw_version) {
4054 qemu_set_version(machine_class->hw_version);
4057 if (qemu_opts_foreach(qemu_find_opts("object"),
4058 object_create, NULL, 0) != 0) {
4059 exit(1);
4062 /* Init CPU def lists, based on config
4063 * - Must be called after all the qemu_read_config_file() calls
4064 * - Must be called before list_cpus()
4065 * - Must be called before machine->init()
4067 cpudef_init();
4069 if (cpu_model && is_help_option(cpu_model)) {
4070 list_cpus(stdout, &fprintf, cpu_model);
4071 exit(0);
4074 /* Open the logfile at this point, if necessary. We can't open the logfile
4075 * when encountering either of the logging options (-d or -D) because the
4076 * other one may be encountered later on the command line, changing the
4077 * location or level of logging.
4079 if (log_mask) {
4080 int mask;
4081 if (log_file) {
4082 qemu_set_log_filename(log_file);
4085 mask = qemu_str_to_log_mask(log_mask);
4086 if (!mask) {
4087 qemu_print_log_usage(stdout);
4088 exit(1);
4090 qemu_set_log(mask);
4093 if (!is_daemonized()) {
4094 if (!trace_init_backends(trace_events, trace_file)) {
4095 exit(1);
4099 /* If no data_dir is specified then try to find it relative to the
4100 executable path. */
4101 if (data_dir_idx < ARRAY_SIZE(data_dir)) {
4102 data_dir[data_dir_idx] = os_find_datadir();
4103 if (data_dir[data_dir_idx] != NULL) {
4104 data_dir_idx++;
4107 /* If all else fails use the install path specified when building. */
4108 if (data_dir_idx < ARRAY_SIZE(data_dir)) {
4109 data_dir[data_dir_idx++] = CONFIG_QEMU_DATADIR;
4112 smp_parse(qemu_opts_find(qemu_find_opts("smp-opts"), NULL));
4114 machine_class->max_cpus = machine_class->max_cpus ?: 1; /* Default to UP */
4115 if (smp_cpus > machine_class->max_cpus) {
4116 fprintf(stderr, "Number of SMP cpus requested (%d), exceeds max cpus "
4117 "supported by machine `%s' (%d)\n", smp_cpus,
4118 machine_class->name, machine_class->max_cpus);
4119 exit(1);
4123 * Get the default machine options from the machine if it is not already
4124 * specified either by the configuration file or by the command line.
4126 if (machine_class->default_machine_opts) {
4127 qemu_opts_set_defaults(qemu_find_opts("machine"),
4128 machine_class->default_machine_opts, 0);
4131 qemu_opts_foreach(qemu_find_opts("device"), default_driver_check, NULL, 0);
4132 qemu_opts_foreach(qemu_find_opts("global"), default_driver_check, NULL, 0);
4134 if (!vga_model && !default_vga) {
4135 vga_interface_type = VGA_DEVICE;
4137 if (!has_defaults || machine_class->no_serial) {
4138 default_serial = 0;
4140 if (!has_defaults || machine_class->no_parallel) {
4141 default_parallel = 0;
4143 if (!has_defaults || !machine_class->use_virtcon) {
4144 default_virtcon = 0;
4146 if (!has_defaults || !machine_class->use_sclp) {
4147 default_sclp = 0;
4149 if (!has_defaults || machine_class->no_floppy) {
4150 default_floppy = 0;
4152 if (!has_defaults || machine_class->no_cdrom) {
4153 default_cdrom = 0;
4155 if (!has_defaults || machine_class->no_sdcard) {
4156 default_sdcard = 0;
4158 if (!has_defaults) {
4159 default_monitor = 0;
4160 default_net = 0;
4161 default_vga = 0;
4164 if (is_daemonized()) {
4165 /* According to documentation and historically, -nographic redirects
4166 * serial port, parallel port and monitor to stdio, which does not work
4167 * with -daemonize. We can redirect these to null instead, but since
4168 * -nographic is legacy, let's just error out.
4169 * We disallow -nographic only if all other ports are not redirected
4170 * explicitly, to not break existing legacy setups which uses
4171 * -nographic _and_ redirects all ports explicitly - this is valid
4172 * usage, -nographic is just a no-op in this case.
4174 if (display_type == DT_NOGRAPHIC
4175 && (default_parallel || default_serial
4176 || default_monitor || default_virtcon)) {
4177 fprintf(stderr, "-nographic can not be used with -daemonize\n");
4178 exit(1);
4180 #ifdef CONFIG_CURSES
4181 if (display_type == DT_CURSES) {
4182 fprintf(stderr, "curses display can not be used with -daemonize\n");
4183 exit(1);
4185 #endif
4188 if (display_type == DT_NOGRAPHIC) {
4189 if (default_parallel)
4190 add_device_config(DEV_PARALLEL, "null");
4191 if (default_serial && default_monitor) {
4192 add_device_config(DEV_SERIAL, "mon:stdio");
4193 } else if (default_virtcon && default_monitor) {
4194 add_device_config(DEV_VIRTCON, "mon:stdio");
4195 } else if (default_sclp && default_monitor) {
4196 add_device_config(DEV_SCLP, "mon:stdio");
4197 } else {
4198 if (default_serial)
4199 add_device_config(DEV_SERIAL, "stdio");
4200 if (default_virtcon)
4201 add_device_config(DEV_VIRTCON, "stdio");
4202 if (default_sclp) {
4203 add_device_config(DEV_SCLP, "stdio");
4205 if (default_monitor)
4206 monitor_parse("stdio", "readline");
4208 } else {
4209 if (default_serial)
4210 add_device_config(DEV_SERIAL, "vc:80Cx24C");
4211 if (default_parallel)
4212 add_device_config(DEV_PARALLEL, "vc:80Cx24C");
4213 if (default_monitor)
4214 monitor_parse("vc:80Cx24C", "readline");
4215 if (default_virtcon)
4216 add_device_config(DEV_VIRTCON, "vc:80Cx24C");
4217 if (default_sclp) {
4218 add_device_config(DEV_SCLP, "vc:80Cx24C");
4222 if (display_type == DT_DEFAULT && !display_remote) {
4223 #if defined(CONFIG_GTK)
4224 display_type = DT_GTK;
4225 #elif defined(CONFIG_SDL) || defined(CONFIG_COCOA)
4226 display_type = DT_SDL;
4227 #elif defined(CONFIG_VNC)
4228 vnc_display = "localhost:0,to=99";
4229 show_vnc_port = 1;
4230 #else
4231 display_type = DT_NONE;
4232 #endif
4235 if ((no_frame || alt_grab || ctrl_grab) && display_type != DT_SDL) {
4236 fprintf(stderr, "-no-frame, -alt-grab and -ctrl-grab are only valid "
4237 "for SDL, ignoring option\n");
4239 if (no_quit && (display_type != DT_GTK && display_type != DT_SDL)) {
4240 fprintf(stderr, "-no-quit is only valid for GTK and SDL, "
4241 "ignoring option\n");
4244 #if defined(CONFIG_GTK)
4245 if (display_type == DT_GTK) {
4246 early_gtk_display_init();
4248 #endif
4250 socket_init();
4252 if (qemu_opts_foreach(qemu_find_opts("chardev"), chardev_init_func, NULL, 1) != 0)
4253 exit(1);
4254 #ifdef CONFIG_VIRTFS
4255 if (qemu_opts_foreach(qemu_find_opts("fsdev"), fsdev_init_func, NULL, 1) != 0) {
4256 exit(1);
4258 #endif
4260 if (pid_file && qemu_create_pidfile(pid_file) != 0) {
4261 os_pidfile_error();
4262 exit(1);
4265 /* store value for the future use */
4266 qemu_opt_set_number(qemu_find_opts_singleton("memory"), "size", ram_size);
4268 if (qemu_opts_foreach(qemu_find_opts("device"), device_help_func, NULL, 0)
4269 != 0) {
4270 exit(0);
4273 machine_opts = qemu_get_machine_opts();
4274 if (qemu_opt_foreach(machine_opts, object_set_property, current_machine,
4275 1) < 0) {
4276 object_unref(OBJECT(current_machine));
4277 exit(1);
4280 configure_accelerator(machine_class);
4282 if (qtest_chrdev) {
4283 Error *local_err = NULL;
4284 qtest_init(qtest_chrdev, qtest_log, &local_err);
4285 if (local_err) {
4286 error_report("%s", error_get_pretty(local_err));
4287 error_free(local_err);
4288 exit(1);
4292 machine_opts = qemu_get_machine_opts();
4293 kernel_filename = qemu_opt_get(machine_opts, "kernel");
4294 initrd_filename = qemu_opt_get(machine_opts, "initrd");
4295 kernel_cmdline = qemu_opt_get(machine_opts, "append");
4296 bios_name = qemu_opt_get(machine_opts, "firmware");
4298 boot_order = machine_class->default_boot_order;
4299 opts = qemu_opts_find(qemu_find_opts("boot-opts"), NULL);
4300 if (opts) {
4301 char *normal_boot_order;
4302 const char *order, *once;
4304 order = qemu_opt_get(opts, "order");
4305 if (order) {
4306 validate_bootdevices(order);
4307 boot_order = order;
4310 once = qemu_opt_get(opts, "once");
4311 if (once) {
4312 validate_bootdevices(once);
4313 normal_boot_order = g_strdup(boot_order);
4314 boot_order = once;
4315 qemu_register_reset(restore_boot_order, normal_boot_order);
4318 boot_menu = qemu_opt_get_bool(opts, "menu", boot_menu);
4319 boot_strict = qemu_opt_get_bool(opts, "strict", false);
4322 if (!kernel_cmdline) {
4323 kernel_cmdline = "";
4324 current_machine->kernel_cmdline = (char *)kernel_cmdline;
4327 linux_boot = (kernel_filename != NULL);
4329 if (!linux_boot && *kernel_cmdline != '\0') {
4330 fprintf(stderr, "-append only allowed with -kernel option\n");
4331 exit(1);
4334 if (!linux_boot && initrd_filename != NULL) {
4335 fprintf(stderr, "-initrd only allowed with -kernel option\n");
4336 exit(1);
4339 if (!linux_boot && qemu_opt_get(machine_opts, "dtb")) {
4340 fprintf(stderr, "-dtb only allowed with -kernel option\n");
4341 exit(1);
4344 os_set_line_buffering();
4346 qemu_init_cpu_loop();
4347 qemu_mutex_lock_iothread();
4349 #ifdef CONFIG_SPICE
4350 /* spice needs the timers to be initialized by this point */
4351 qemu_spice_init();
4352 #endif
4354 if (icount_option && (kvm_enabled() || xen_enabled())) {
4355 fprintf(stderr, "-icount is not allowed with kvm or xen\n");
4356 exit(1);
4358 configure_icount(icount_option);
4360 /* clean up network at qemu process termination */
4361 atexit(&net_cleanup);
4363 if (net_init_clients() < 0) {
4364 exit(1);
4367 #ifdef CONFIG_TPM
4368 if (tpm_init() < 0) {
4369 exit(1);
4371 #endif
4373 /* init the bluetooth world */
4374 if (foreach_device_config(DEV_BT, bt_parse))
4375 exit(1);
4377 if (!xen_enabled()) {
4378 /* On 32-bit hosts, QEMU is limited by virtual address space */
4379 if (ram_size > (2047 << 20) && HOST_LONG_BITS == 32) {
4380 fprintf(stderr, "qemu: at most 2047 MB RAM can be simulated\n");
4381 exit(1);
4385 cpu_exec_init_all();
4387 blk_mig_init();
4388 ram_mig_init();
4390 /* open the virtual block devices */
4391 if (snapshot)
4392 qemu_opts_foreach(qemu_find_opts("drive"), drive_enable_snapshot, NULL, 0);
4393 if (qemu_opts_foreach(qemu_find_opts("drive"), drive_init_func,
4394 &machine_class->block_default_type, 1) != 0) {
4395 exit(1);
4398 default_drive(default_cdrom, snapshot, machine_class->block_default_type, 2,
4399 CDROM_OPTS);
4400 default_drive(default_floppy, snapshot, IF_FLOPPY, 0, FD_OPTS);
4401 default_drive(default_sdcard, snapshot, IF_SD, 0, SD_OPTS);
4403 if (nb_numa_nodes > 0) {
4404 int i;
4406 if (nb_numa_nodes > MAX_NODES) {
4407 nb_numa_nodes = MAX_NODES;
4410 /* If no memory size if given for any node, assume the default case
4411 * and distribute the available memory equally across all nodes
4413 for (i = 0; i < nb_numa_nodes; i++) {
4414 if (node_mem[i] != 0)
4415 break;
4417 if (i == nb_numa_nodes) {
4418 uint64_t usedmem = 0;
4420 /* On Linux, the each node's border has to be 8MB aligned,
4421 * the final node gets the rest.
4423 for (i = 0; i < nb_numa_nodes - 1; i++) {
4424 node_mem[i] = (ram_size / nb_numa_nodes) & ~((1 << 23UL) - 1);
4425 usedmem += node_mem[i];
4427 node_mem[i] = ram_size - usedmem;
4430 for (i = 0; i < nb_numa_nodes; i++) {
4431 if (!bitmap_empty(node_cpumask[i], MAX_CPUMASK_BITS)) {
4432 break;
4435 /* assigning the VCPUs round-robin is easier to implement, guest OSes
4436 * must cope with this anyway, because there are BIOSes out there in
4437 * real machines which also use this scheme.
4439 if (i == nb_numa_nodes) {
4440 for (i = 0; i < max_cpus; i++) {
4441 set_bit(i, node_cpumask[i % nb_numa_nodes]);
4446 if (qemu_opts_foreach(qemu_find_opts("mon"), mon_init_func, NULL, 1) != 0) {
4447 exit(1);
4450 if (foreach_device_config(DEV_SERIAL, serial_parse) < 0)
4451 exit(1);
4452 if (foreach_device_config(DEV_PARALLEL, parallel_parse) < 0)
4453 exit(1);
4454 if (foreach_device_config(DEV_VIRTCON, virtcon_parse) < 0)
4455 exit(1);
4456 if (foreach_device_config(DEV_SCLP, sclp_parse) < 0) {
4457 exit(1);
4459 if (foreach_device_config(DEV_DEBUGCON, debugcon_parse) < 0)
4460 exit(1);
4462 /* If no default VGA is requested, the default is "none". */
4463 if (default_vga) {
4464 if (cirrus_vga_available()) {
4465 vga_model = "cirrus";
4466 } else if (vga_available()) {
4467 vga_model = "std";
4470 if (vga_model) {
4471 select_vgahw(vga_model);
4474 if (watchdog) {
4475 i = select_watchdog(watchdog);
4476 if (i > 0)
4477 exit (i == 1 ? 1 : 0);
4480 if (machine_class->compat_props) {
4481 qdev_prop_register_global_list(machine_class->compat_props);
4483 qemu_add_globals();
4485 qdev_machine_init();
4487 current_machine->ram_size = ram_size;
4488 current_machine->maxram_size = maxram_size;
4489 current_machine->ram_slots = ram_slots;
4490 current_machine->boot_order = boot_order;
4491 current_machine->cpu_model = cpu_model;
4493 machine_class->init(current_machine);
4495 audio_init();
4497 cpu_synchronize_all_post_init();
4499 set_numa_modes();
4501 /* init USB devices */
4502 if (usb_enabled(false)) {
4503 if (foreach_device_config(DEV_USB, usb_parse) < 0)
4504 exit(1);
4507 /* init generic devices */
4508 if (qemu_opts_foreach(qemu_find_opts("device"), device_init_func, NULL, 1) != 0)
4509 exit(1);
4511 net_check_clients();
4513 ds = init_displaystate();
4515 /* init local displays */
4516 switch (display_type) {
4517 case DT_NOGRAPHIC:
4518 (void)ds; /* avoid warning if no display is configured */
4519 break;
4520 #if defined(CONFIG_CURSES)
4521 case DT_CURSES:
4522 curses_display_init(ds, full_screen);
4523 break;
4524 #endif
4525 #if defined(CONFIG_SDL)
4526 case DT_SDL:
4527 sdl_display_init(ds, full_screen, no_frame);
4528 break;
4529 #elif defined(CONFIG_COCOA)
4530 case DT_SDL:
4531 cocoa_display_init(ds, full_screen);
4532 break;
4533 #endif
4534 #if defined(CONFIG_GTK)
4535 case DT_GTK:
4536 gtk_display_init(ds, full_screen, grab_on_hover);
4537 break;
4538 #endif
4539 default:
4540 break;
4543 /* must be after terminal init, SDL library changes signal handlers */
4544 os_setup_signal_handling();
4546 #ifdef CONFIG_VNC
4547 /* init remote displays */
4548 if (vnc_display) {
4549 Error *local_err = NULL;
4550 vnc_display_init(ds);
4551 vnc_display_open(ds, vnc_display, &local_err);
4552 if (local_err != NULL) {
4553 error_report("Failed to start VNC server on `%s': %s",
4554 vnc_display, error_get_pretty(local_err));
4555 error_free(local_err);
4556 exit(1);
4559 if (show_vnc_port) {
4560 printf("VNC server running on `%s'\n", vnc_display_local_addr(ds));
4563 #endif
4564 #ifdef CONFIG_SPICE
4565 if (using_spice) {
4566 qemu_spice_display_init();
4568 #endif
4570 if (foreach_device_config(DEV_GDB, gdbserver_start) < 0) {
4571 exit(1);
4574 qdev_machine_creation_done();
4576 if (rom_load_all() != 0) {
4577 fprintf(stderr, "rom loading failed\n");
4578 exit(1);
4581 /* TODO: once all bus devices are qdevified, this should be done
4582 * when bus is created by qdev.c */
4583 qemu_register_reset(qbus_reset_all_fn, sysbus_get_default());
4584 qemu_run_machine_init_done_notifiers();
4586 /* Done notifiers can load ROMs */
4587 rom_load_done();
4589 qemu_system_reset(VMRESET_SILENT);
4590 if (loadvm) {
4591 if (load_vmstate(loadvm) < 0) {
4592 autostart = 0;
4596 qdev_prop_check_global();
4598 if (incoming) {
4599 Error *local_err = NULL;
4600 qemu_start_incoming_migration(incoming, &local_err);
4601 if (local_err) {
4602 error_report("-incoming %s: %s", incoming,
4603 error_get_pretty(local_err));
4604 error_free(local_err);
4605 exit(1);
4607 } else if (autostart) {
4608 vm_start();
4611 os_setup_post();
4613 if (is_daemonized()) {
4614 if (!trace_init_backends(trace_events, trace_file)) {
4615 exit(1);
4619 main_loop();
4620 bdrv_close_all();
4621 pause_all_vcpus();
4622 res_free();
4623 #ifdef CONFIG_TPM
4624 tpm_cleanup();
4625 #endif
4627 return 0;