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
32 /* Needed early for CONFIG_BSD etc. */
33 #include "config-host.h"
37 #include <sys/times.h>
41 #include <sys/ioctl.h>
42 #include <sys/resource.h>
43 #include <sys/socket.h>
44 #include <netinet/in.h>
46 #include <arpa/inet.h>
49 #include <sys/select.h>
53 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__DragonFly__)
54 #include <sys/sysctl.h>
62 #include <linux/ppdev.h>
63 #include <linux/parport.h>
67 #include <sys/ethernet.h>
68 #include <sys/sockio.h>
69 #include <netinet/arp.h>
70 #include <netinet/in_systm.h>
71 #include <netinet/ip.h>
72 #include <netinet/ip_icmp.h> // must come after ip.h
73 #include <netinet/udp.h>
74 #include <netinet/tcp.h>
82 #if defined(CONFIG_VDE)
83 #include <libvdeplug.h>
91 #if defined(__APPLE__) || defined(main)
93 int qemu_main(int argc, char **argv, char **envp);
94 int main(int argc, char **argv)
96 return qemu_main(argc, argv, NULL);
99 #define main qemu_main
101 #endif /* CONFIG_SDL */
105 #define main qemu_main
106 #endif /* CONFIG_COCOA */
111 #include "hw/boards.h"
113 #include "hw/pcmcia.h"
118 #include "hw/watchdog.h"
119 #include "hw/smbios.h"
122 #include "hw/loader.h"
125 #include "net/slirp.h"
130 #include "qemu-timer.h"
131 #include "qemu-char.h"
132 #include "cache-utils.h"
133 #include "blockdev.h"
134 #include "hw/block-common.h"
135 #include "block-migration.h"
137 #include "audio/audio.h"
138 #include "migration.h"
141 #include "qemu-option.h"
142 #include "qemu-config.h"
143 #include "qemu-options.h"
144 #include "qmp-commands.h"
145 #include "main-loop.h"
147 #include "fsdev/qemu-fsdev.h"
153 #include "qemu_socket.h"
155 #include "slirp/libslirp.h"
158 #include "trace/control.h"
159 #include "qemu-queue.h"
161 #include "arch_init.h"
164 #include "ui/qemu-spice.h"
167 //#define DEBUG_SLIRP
169 #define DEFAULT_RAM_SIZE 128
171 #define MAX_VIRTIO_CONSOLES 1
173 static const char *data_dir;
174 const char *bios_name = NULL;
175 enum vga_retrace_method vga_retrace_method = VGA_RETRACE_DUMB;
176 DisplayType display_type = DT_DEFAULT;
177 int display_remote = 0;
178 const char* keyboard_layout = NULL;
180 const char *mem_path = NULL;
182 int mem_prealloc = 0; /* force preallocation of physical target memory */
185 NICInfo nd_table[MAX_NICS];
187 static int rtc_utc = 1;
188 static int rtc_date_offset = -1; /* -1 means no change */
189 QEMUClock *rtc_clock;
190 int vga_interface_type = VGA_NONE;
191 static int full_screen = 0;
193 static int no_frame = 0;
196 CharDriverState *serial_hds[MAX_SERIAL_PORTS];
197 CharDriverState *parallel_hds[MAX_PARALLEL_PORTS];
198 CharDriverState *virtcon_hds[MAX_VIRTIO_CONSOLES];
199 int win2k_install_hack = 0;
207 const char *vnc_display;
209 int acpi_enabled = 1;
215 int graphic_rotate = 0;
216 const char *watchdog;
217 QEMUOptionRom option_rom[MAX_OPTION_ROMS];
219 int semihosting_enabled = 0;
221 const char *qemu_name;
224 unsigned int nb_prom_envs = 0;
225 const char *prom_envs[MAX_PROM_ENVS];
227 uint8_t *boot_splash_filedata;
228 int boot_splash_filedata_size;
229 uint8_t qemu_extra_params_fw[2];
231 typedef struct FWBootEntry FWBootEntry;
234 QTAILQ_ENTRY(FWBootEntry) link;
240 QTAILQ_HEAD(, FWBootEntry) fw_boot_order = QTAILQ_HEAD_INITIALIZER(fw_boot_order);
243 uint64_t node_mem[MAX_NODES];
244 uint64_t node_cpumask[MAX_NODES];
246 uint8_t qemu_uuid[16];
248 static QEMUBootSetHandler *boot_set_handler;
249 static void *boot_set_opaque;
251 static NotifierList exit_notifiers =
252 NOTIFIER_LIST_INITIALIZER(exit_notifiers);
254 static NotifierList machine_init_done_notifiers =
255 NOTIFIER_LIST_INITIALIZER(machine_init_done_notifiers);
257 static int tcg_allowed = 1;
261 enum xen_mode xen_mode = XEN_EMULATE;
262 static int tcg_tb_size;
264 static int default_serial = 1;
265 static int default_parallel = 1;
266 static int default_virtcon = 1;
267 static int default_monitor = 1;
268 static int default_floppy = 1;
269 static int default_cdrom = 1;
270 static int default_sdcard = 1;
271 static int default_vga = 1;
277 { .driver = "isa-serial", .flag = &default_serial },
278 { .driver = "isa-parallel", .flag = &default_parallel },
279 { .driver = "isa-fdc", .flag = &default_floppy },
280 { .driver = "ide-cd", .flag = &default_cdrom },
281 { .driver = "ide-hd", .flag = &default_cdrom },
282 { .driver = "ide-drive", .flag = &default_cdrom },
283 { .driver = "scsi-cd", .flag = &default_cdrom },
284 { .driver = "virtio-serial-pci", .flag = &default_virtcon },
285 { .driver = "virtio-serial-s390", .flag = &default_virtcon },
286 { .driver = "virtio-serial", .flag = &default_virtcon },
287 { .driver = "VGA", .flag = &default_vga },
288 { .driver = "isa-vga", .flag = &default_vga },
289 { .driver = "cirrus-vga", .flag = &default_vga },
290 { .driver = "isa-cirrus-vga", .flag = &default_vga },
291 { .driver = "vmware-svga", .flag = &default_vga },
292 { .driver = "qxl-vga", .flag = &default_vga },
295 static void res_free(void)
297 if (boot_splash_filedata != NULL) {
298 g_free(boot_splash_filedata);
299 boot_splash_filedata = NULL;
303 static int default_driver_check(QemuOpts *opts, void *opaque)
305 const char *driver = qemu_opt_get(opts, "driver");
310 for (i = 0; i < ARRAY_SIZE(default_list); i++) {
311 if (strcmp(default_list[i].driver, driver) != 0)
313 *(default_list[i].flag) = 0;
318 /***********************************************************/
321 static RunState current_run_state = RUN_STATE_PRELAUNCH;
326 } RunStateTransition;
328 static const RunStateTransition runstate_transitions_def[] = {
330 { RUN_STATE_DEBUG, RUN_STATE_RUNNING },
332 { RUN_STATE_INMIGRATE, RUN_STATE_RUNNING },
333 { RUN_STATE_INMIGRATE, RUN_STATE_PRELAUNCH },
335 { RUN_STATE_INTERNAL_ERROR, RUN_STATE_PAUSED },
336 { RUN_STATE_INTERNAL_ERROR, RUN_STATE_FINISH_MIGRATE },
338 { RUN_STATE_IO_ERROR, RUN_STATE_RUNNING },
339 { RUN_STATE_IO_ERROR, RUN_STATE_FINISH_MIGRATE },
341 { RUN_STATE_PAUSED, RUN_STATE_RUNNING },
342 { RUN_STATE_PAUSED, RUN_STATE_FINISH_MIGRATE },
344 { RUN_STATE_POSTMIGRATE, RUN_STATE_RUNNING },
345 { RUN_STATE_POSTMIGRATE, RUN_STATE_FINISH_MIGRATE },
347 { RUN_STATE_PRELAUNCH, RUN_STATE_RUNNING },
348 { RUN_STATE_PRELAUNCH, RUN_STATE_FINISH_MIGRATE },
349 { RUN_STATE_PRELAUNCH, RUN_STATE_INMIGRATE },
351 { RUN_STATE_FINISH_MIGRATE, RUN_STATE_RUNNING },
352 { RUN_STATE_FINISH_MIGRATE, RUN_STATE_POSTMIGRATE },
354 { RUN_STATE_RESTORE_VM, RUN_STATE_RUNNING },
356 { RUN_STATE_RUNNING, RUN_STATE_DEBUG },
357 { RUN_STATE_RUNNING, RUN_STATE_INTERNAL_ERROR },
358 { RUN_STATE_RUNNING, RUN_STATE_IO_ERROR },
359 { RUN_STATE_RUNNING, RUN_STATE_PAUSED },
360 { RUN_STATE_RUNNING, RUN_STATE_FINISH_MIGRATE },
361 { RUN_STATE_RUNNING, RUN_STATE_RESTORE_VM },
362 { RUN_STATE_RUNNING, RUN_STATE_SAVE_VM },
363 { RUN_STATE_RUNNING, RUN_STATE_SHUTDOWN },
364 { RUN_STATE_RUNNING, RUN_STATE_WATCHDOG },
366 { RUN_STATE_SAVE_VM, RUN_STATE_RUNNING },
368 { RUN_STATE_SHUTDOWN, RUN_STATE_PAUSED },
369 { RUN_STATE_SHUTDOWN, RUN_STATE_FINISH_MIGRATE },
371 { RUN_STATE_DEBUG, RUN_STATE_SUSPENDED },
372 { RUN_STATE_RUNNING, RUN_STATE_SUSPENDED },
373 { RUN_STATE_SUSPENDED, RUN_STATE_RUNNING },
374 { RUN_STATE_SUSPENDED, RUN_STATE_FINISH_MIGRATE },
376 { RUN_STATE_WATCHDOG, RUN_STATE_RUNNING },
377 { RUN_STATE_WATCHDOG, RUN_STATE_FINISH_MIGRATE },
379 { RUN_STATE_MAX, RUN_STATE_MAX },
382 static bool runstate_valid_transitions[RUN_STATE_MAX][RUN_STATE_MAX];
384 bool runstate_check(RunState state)
386 return current_run_state == state;
389 void runstate_init(void)
391 const RunStateTransition *p;
393 memset(&runstate_valid_transitions, 0, sizeof(runstate_valid_transitions));
395 for (p = &runstate_transitions_def[0]; p->from != RUN_STATE_MAX; p++) {
396 runstate_valid_transitions[p->from][p->to] = true;
400 /* This function will abort() on invalid state transitions */
401 void runstate_set(RunState new_state)
403 assert(new_state < RUN_STATE_MAX);
405 if (!runstate_valid_transitions[current_run_state][new_state]) {
406 fprintf(stderr, "ERROR: invalid runstate transition: '%s' -> '%s'\n",
407 RunState_lookup[current_run_state],
408 RunState_lookup[new_state]);
412 current_run_state = new_state;
415 int runstate_is_running(void)
417 return runstate_check(RUN_STATE_RUNNING);
420 StatusInfo *qmp_query_status(Error **errp)
422 StatusInfo *info = g_malloc0(sizeof(*info));
424 info->running = runstate_is_running();
425 info->singlestep = singlestep;
426 info->status = current_run_state;
431 /***********************************************************/
432 /* real time host monotonic timer */
434 /***********************************************************/
435 /* host time/date access */
436 void qemu_get_timedate(struct tm *tm, int offset)
443 if (rtc_date_offset == -1) {
447 ret = localtime(&ti);
449 ti -= rtc_date_offset;
453 memcpy(tm, ret, sizeof(struct tm));
456 int qemu_timedate_diff(struct tm *tm)
460 if (rtc_date_offset == -1)
462 seconds = mktimegm(tm);
465 tmp.tm_isdst = -1; /* use timezone to figure it out */
466 seconds = mktime(&tmp);
469 seconds = mktimegm(tm) + rtc_date_offset;
471 return seconds - time(NULL);
474 void rtc_change_mon_event(struct tm *tm)
478 data = qobject_from_jsonf("{ 'offset': %d }", qemu_timedate_diff(tm));
479 monitor_protocol_event(QEVENT_RTC_CHANGE, data);
480 qobject_decref(data);
483 static void configure_rtc_date_offset(const char *startdate, int legacy)
485 time_t rtc_start_date;
488 if (!strcmp(startdate, "now") && legacy) {
489 rtc_date_offset = -1;
491 if (sscanf(startdate, "%d-%d-%dT%d:%d:%d",
499 } else if (sscanf(startdate, "%d-%d-%d",
511 rtc_start_date = mktimegm(&tm);
512 if (rtc_start_date == -1) {
514 fprintf(stderr, "Invalid date format. Valid formats are:\n"
515 "'2006-06-17T16:01:21' or '2006-06-17'\n");
518 rtc_date_offset = time(NULL) - rtc_start_date;
522 static void configure_rtc(QemuOpts *opts)
526 value = qemu_opt_get(opts, "base");
528 if (!strcmp(value, "utc")) {
530 } else if (!strcmp(value, "localtime")) {
533 configure_rtc_date_offset(value, 0);
536 value = qemu_opt_get(opts, "clock");
538 if (!strcmp(value, "host")) {
539 rtc_clock = host_clock;
540 } else if (!strcmp(value, "rt")) {
541 rtc_clock = rt_clock;
542 } else if (!strcmp(value, "vm")) {
543 rtc_clock = vm_clock;
545 fprintf(stderr, "qemu: invalid option value '%s'\n", value);
549 value = qemu_opt_get(opts, "driftfix");
551 if (!strcmp(value, "slew")) {
552 static GlobalProperty slew_lost_ticks[] = {
554 .driver = "mc146818rtc",
555 .property = "lost_tick_policy",
558 { /* end of list */ }
561 qdev_prop_register_global_list(slew_lost_ticks);
562 } else if (!strcmp(value, "none")) {
563 /* discard is default */
565 fprintf(stderr, "qemu: invalid option value '%s'\n", value);
571 /***********************************************************/
572 /* Bluetooth support */
575 static struct HCIInfo *hci_table[MAX_NICS];
577 static struct bt_vlan_s {
578 struct bt_scatternet_s net;
580 struct bt_vlan_s *next;
583 /* find or alloc a new bluetooth "VLAN" */
584 static struct bt_scatternet_s *qemu_find_bt_vlan(int id)
586 struct bt_vlan_s **pvlan, *vlan;
587 for (vlan = first_bt_vlan; vlan != NULL; vlan = vlan->next) {
591 vlan = g_malloc0(sizeof(struct bt_vlan_s));
593 pvlan = &first_bt_vlan;
594 while (*pvlan != NULL)
595 pvlan = &(*pvlan)->next;
600 static void null_hci_send(struct HCIInfo *hci, const uint8_t *data, int len)
604 static int null_hci_addr_set(struct HCIInfo *hci, const uint8_t *bd_addr)
609 static struct HCIInfo null_hci = {
610 .cmd_send = null_hci_send,
611 .sco_send = null_hci_send,
612 .acl_send = null_hci_send,
613 .bdaddr_set = null_hci_addr_set,
616 struct HCIInfo *qemu_next_hci(void)
618 if (cur_hci == nb_hcis)
621 return hci_table[cur_hci++];
624 static struct HCIInfo *hci_init(const char *str)
627 struct bt_scatternet_s *vlan = 0;
629 if (!strcmp(str, "null"))
632 else if (!strncmp(str, "host", 4) && (str[4] == '\0' || str[4] == ':'))
634 return bt_host_hci(str[4] ? str + 5 : "hci0");
635 else if (!strncmp(str, "hci", 3)) {
638 if (!strncmp(str + 3, ",vlan=", 6)) {
639 vlan = qemu_find_bt_vlan(strtol(str + 9, &endp, 0));
644 vlan = qemu_find_bt_vlan(0);
646 return bt_new_hci(vlan);
649 fprintf(stderr, "qemu: Unknown bluetooth HCI `%s'.\n", str);
654 static int bt_hci_parse(const char *str)
659 if (nb_hcis >= MAX_NICS) {
660 fprintf(stderr, "qemu: Too many bluetooth HCIs (max %i).\n", MAX_NICS);
673 bdaddr.b[5] = 0x56 + nb_hcis;
674 hci->bdaddr_set(hci, bdaddr.b);
676 hci_table[nb_hcis++] = hci;
681 static void bt_vhci_add(int vlan_id)
683 struct bt_scatternet_s *vlan = qemu_find_bt_vlan(vlan_id);
686 fprintf(stderr, "qemu: warning: adding a VHCI to "
687 "an empty scatternet %i\n", vlan_id);
689 bt_vhci_init(bt_new_hci(vlan));
692 static struct bt_device_s *bt_device_add(const char *opt)
694 struct bt_scatternet_s *vlan;
696 char *endp = strstr(opt, ",vlan=");
697 int len = (endp ? endp - opt : strlen(opt)) + 1;
700 pstrcpy(devname, MIN(sizeof(devname), len), opt);
703 vlan_id = strtol(endp + 6, &endp, 0);
705 fprintf(stderr, "qemu: unrecognised bluetooth vlan Id\n");
710 vlan = qemu_find_bt_vlan(vlan_id);
713 fprintf(stderr, "qemu: warning: adding a slave device to "
714 "an empty scatternet %i\n", vlan_id);
716 if (!strcmp(devname, "keyboard"))
717 return bt_keyboard_init(vlan);
719 fprintf(stderr, "qemu: unsupported bluetooth device `%s'\n", devname);
723 static int bt_parse(const char *opt)
725 const char *endp, *p;
728 if (strstart(opt, "hci", &endp)) {
729 if (!*endp || *endp == ',') {
731 if (!strstart(endp, ",vlan=", 0))
734 return bt_hci_parse(opt);
736 } else if (strstart(opt, "vhci", &endp)) {
737 if (!*endp || *endp == ',') {
739 if (strstart(endp, ",vlan=", &p)) {
740 vlan = strtol(p, (char **) &endp, 0);
742 fprintf(stderr, "qemu: bad scatternet '%s'\n", p);
746 fprintf(stderr, "qemu: bad parameter '%s'\n", endp + 1);
755 } else if (strstart(opt, "device:", &endp))
756 return !bt_device_add(endp);
758 fprintf(stderr, "qemu: bad bluetooth parameter '%s'\n", opt);
762 /***********************************************************/
763 /* QEMU Block devices */
765 #define HD_OPTS "media=disk"
766 #define CDROM_OPTS "media=cdrom"
768 #define PFLASH_OPTS ""
772 static int drive_init_func(QemuOpts *opts, void *opaque)
774 int *use_scsi = opaque;
776 return drive_init(opts, *use_scsi) == NULL;
779 static int drive_enable_snapshot(QemuOpts *opts, void *opaque)
781 if (NULL == qemu_opt_get(opts, "snapshot")) {
782 qemu_opt_set(opts, "snapshot", "on");
787 static void default_drive(int enable, int snapshot, int use_scsi,
788 BlockInterfaceType type, int index,
793 if (type == IF_DEFAULT) {
794 type = use_scsi ? IF_SCSI : IF_IDE;
797 if (!enable || drive_get_by_index(type, index)) {
801 opts = drive_add(type, index, NULL, optstr);
803 drive_enable_snapshot(opts, NULL);
805 if (!drive_init(opts, use_scsi)) {
810 void qemu_register_boot_set(QEMUBootSetHandler *func, void *opaque)
812 boot_set_handler = func;
813 boot_set_opaque = opaque;
816 int qemu_boot_set(const char *boot_devices)
818 if (!boot_set_handler) {
821 return boot_set_handler(boot_set_opaque, boot_devices);
824 static void validate_bootdevices(char *devices)
826 /* We just do some generic consistency checks */
830 for (p = devices; *p != '\0'; p++) {
831 /* Allowed boot devices are:
832 * a-b: floppy disk drives
833 * c-f: IDE disk drives
834 * g-m: machine implementation dependent drives
835 * n-p: network devices
836 * It's up to each machine implementation to check if the given boot
837 * devices match the actual hardware implementation and firmware
840 if (*p < 'a' || *p > 'p') {
841 fprintf(stderr, "Invalid boot device '%c'\n", *p);
844 if (bitmap & (1 << (*p - 'a'))) {
845 fprintf(stderr, "Boot device '%c' was given twice\n", *p);
848 bitmap |= 1 << (*p - 'a');
852 static void restore_boot_devices(void *opaque)
854 char *standard_boot_devices = opaque;
855 static int first = 1;
857 /* Restore boot order and remove ourselves after the first boot */
863 qemu_boot_set(standard_boot_devices);
865 qemu_unregister_reset(restore_boot_devices, standard_boot_devices);
866 g_free(standard_boot_devices);
869 void add_boot_device_path(int32_t bootindex, DeviceState *dev,
872 FWBootEntry *node, *i;
878 assert(dev != NULL || suffix != NULL);
880 node = g_malloc0(sizeof(FWBootEntry));
881 node->bootindex = bootindex;
882 node->suffix = suffix ? g_strdup(suffix) : NULL;
885 QTAILQ_FOREACH(i, &fw_boot_order, link) {
886 if (i->bootindex == bootindex) {
887 fprintf(stderr, "Two devices with same boot index %d\n", bootindex);
889 } else if (i->bootindex < bootindex) {
892 QTAILQ_INSERT_BEFORE(i, node, link);
895 QTAILQ_INSERT_TAIL(&fw_boot_order, node, link);
899 * This function returns null terminated string that consist of new line
900 * separated device paths.
902 * memory pointed by "size" is assigned total length of the array in bytes
905 char *get_boot_devices_list(uint32_t *size)
911 QTAILQ_FOREACH(i, &fw_boot_order, link) {
912 char *devpath = NULL, *bootpath;
916 devpath = qdev_get_fw_dev_path(i->dev);
920 if (i->suffix && devpath) {
921 size_t bootpathlen = strlen(devpath) + strlen(i->suffix) + 1;
923 bootpath = g_malloc(bootpathlen);
924 snprintf(bootpath, bootpathlen, "%s%s", devpath, i->suffix);
926 } else if (devpath) {
930 bootpath = g_strdup(i->suffix);
934 list[total-1] = '\n';
936 len = strlen(bootpath) + 1;
937 list = g_realloc(list, total + len);
938 memcpy(&list[total], bootpath, len);
948 static void numa_add(const char *optarg)
952 unsigned long long value, endvalue;
955 optarg = get_opt_name(option, 128, optarg, ',') + 1;
956 if (!strcmp(option, "node")) {
957 if (get_param_value(option, 128, "nodeid", optarg) == 0) {
958 nodenr = nb_numa_nodes;
960 nodenr = strtoull(option, NULL, 10);
963 if (get_param_value(option, 128, "mem", optarg) == 0) {
964 node_mem[nodenr] = 0;
967 sval = strtosz(option, &endptr);
968 if (sval < 0 || *endptr) {
969 fprintf(stderr, "qemu: invalid numa mem size: %s\n", optarg);
972 node_mem[nodenr] = sval;
974 if (get_param_value(option, 128, "cpus", optarg) == 0) {
975 node_cpumask[nodenr] = 0;
977 value = strtoull(option, &endptr, 10);
980 fprintf(stderr, "only 64 CPUs in NUMA mode supported.\n");
982 if (*endptr == '-') {
983 endvalue = strtoull(endptr+1, &endptr, 10);
984 if (endvalue >= 63) {
987 "only 63 CPUs in NUMA mode supported.\n");
989 value = (2ULL << endvalue) - (1ULL << value);
991 value = 1ULL << value;
994 node_cpumask[nodenr] = value;
1001 static void smp_parse(const char *optarg)
1003 int smp, sockets = 0, threads = 0, cores = 0;
1007 smp = strtoul(optarg, &endptr, 10);
1008 if (endptr != optarg) {
1009 if (*endptr == ',') {
1013 if (get_param_value(option, 128, "sockets", endptr) != 0)
1014 sockets = strtoull(option, NULL, 10);
1015 if (get_param_value(option, 128, "cores", endptr) != 0)
1016 cores = strtoull(option, NULL, 10);
1017 if (get_param_value(option, 128, "threads", endptr) != 0)
1018 threads = strtoull(option, NULL, 10);
1019 if (get_param_value(option, 128, "maxcpus", endptr) != 0)
1020 max_cpus = strtoull(option, NULL, 10);
1022 /* compute missing values, prefer sockets over cores over threads */
1023 if (smp == 0 || sockets == 0) {
1024 sockets = sockets > 0 ? sockets : 1;
1025 cores = cores > 0 ? cores : 1;
1026 threads = threads > 0 ? threads : 1;
1028 smp = cores * threads * sockets;
1032 threads = threads > 0 ? threads : 1;
1033 cores = smp / (sockets * threads);
1035 threads = smp / (cores * sockets);
1039 smp_cores = cores > 0 ? cores : 1;
1040 smp_threads = threads > 0 ? threads : 1;
1042 max_cpus = smp_cpus;
1045 /***********************************************************/
1048 static int usb_device_add(const char *devname)
1051 USBDevice *dev = NULL;
1056 /* drivers with .usbdevice_name entry in USBDeviceInfo */
1057 dev = usbdevice_create(devname);
1061 /* the other ones */
1062 #ifndef CONFIG_LINUX
1063 /* only the linux version is qdev-ified, usb-bsd still needs this */
1064 if (strstart(devname, "host:", &p)) {
1065 dev = usb_host_device_open(usb_bus_find(-1), p);
1068 if (!strcmp(devname, "bt") || strstart(devname, "bt:", &p)) {
1069 dev = usb_bt_init(usb_bus_find(-1),
1070 devname[2] ? hci_init(p)
1071 : bt_new_hci(qemu_find_bt_vlan(0)));
1082 static int usb_device_del(const char *devname)
1087 if (strstart(devname, "host:", &p))
1088 return usb_host_device_close(p);
1093 p = strchr(devname, '.');
1096 bus_num = strtoul(devname, NULL, 0);
1097 addr = strtoul(p + 1, NULL, 0);
1099 return usb_device_delete_addr(bus_num, addr);
1102 static int usb_parse(const char *cmdline)
1105 r = usb_device_add(cmdline);
1107 fprintf(stderr, "qemu: could not add USB device '%s'\n", cmdline);
1112 void do_usb_add(Monitor *mon, const QDict *qdict)
1114 const char *devname = qdict_get_str(qdict, "devname");
1115 if (usb_device_add(devname) < 0) {
1116 error_report("could not add USB device '%s'", devname);
1120 void do_usb_del(Monitor *mon, const QDict *qdict)
1122 const char *devname = qdict_get_str(qdict, "devname");
1123 if (usb_device_del(devname) < 0) {
1124 error_report("could not delete USB device '%s'", devname);
1128 /***********************************************************/
1129 /* PCMCIA/Cardbus */
1131 static struct pcmcia_socket_entry_s {
1132 PCMCIASocket *socket;
1133 struct pcmcia_socket_entry_s *next;
1134 } *pcmcia_sockets = 0;
1136 void pcmcia_socket_register(PCMCIASocket *socket)
1138 struct pcmcia_socket_entry_s *entry;
1140 entry = g_malloc(sizeof(struct pcmcia_socket_entry_s));
1141 entry->socket = socket;
1142 entry->next = pcmcia_sockets;
1143 pcmcia_sockets = entry;
1146 void pcmcia_socket_unregister(PCMCIASocket *socket)
1148 struct pcmcia_socket_entry_s *entry, **ptr;
1150 ptr = &pcmcia_sockets;
1151 for (entry = *ptr; entry; ptr = &entry->next, entry = *ptr)
1152 if (entry->socket == socket) {
1158 void pcmcia_info(Monitor *mon)
1160 struct pcmcia_socket_entry_s *iter;
1162 if (!pcmcia_sockets)
1163 monitor_printf(mon, "No PCMCIA sockets\n");
1165 for (iter = pcmcia_sockets; iter; iter = iter->next)
1166 monitor_printf(mon, "%s: %s\n", iter->socket->slot_string,
1167 iter->socket->attached ? iter->socket->card_string :
1171 /***********************************************************/
1172 /* machine registration */
1174 static QEMUMachine *first_machine = NULL;
1175 QEMUMachine *current_machine = NULL;
1177 int qemu_register_machine(QEMUMachine *m)
1180 pm = &first_machine;
1188 static QEMUMachine *find_machine(const char *name)
1192 for(m = first_machine; m != NULL; m = m->next) {
1193 if (!strcmp(m->name, name))
1195 if (m->alias && !strcmp(m->alias, name))
1201 QEMUMachine *find_default_machine(void)
1205 for(m = first_machine; m != NULL; m = m->next) {
1206 if (m->is_default) {
1213 /***********************************************************/
1214 /* main execution loop */
1216 static void gui_update(void *opaque)
1218 uint64_t interval = GUI_REFRESH_INTERVAL;
1219 DisplayState *ds = opaque;
1220 DisplayChangeListener *dcl = ds->listeners;
1224 while (dcl != NULL) {
1225 if (dcl->gui_timer_interval &&
1226 dcl->gui_timer_interval < interval)
1227 interval = dcl->gui_timer_interval;
1230 qemu_mod_timer(ds->gui_timer, interval + qemu_get_clock_ms(rt_clock));
1233 struct vm_change_state_entry {
1234 VMChangeStateHandler *cb;
1236 QLIST_ENTRY (vm_change_state_entry) entries;
1239 static QLIST_HEAD(vm_change_state_head, vm_change_state_entry) vm_change_state_head;
1241 VMChangeStateEntry *qemu_add_vm_change_state_handler(VMChangeStateHandler *cb,
1244 VMChangeStateEntry *e;
1246 e = g_malloc0(sizeof (*e));
1250 QLIST_INSERT_HEAD(&vm_change_state_head, e, entries);
1254 void qemu_del_vm_change_state_handler(VMChangeStateEntry *e)
1256 QLIST_REMOVE (e, entries);
1260 void vm_state_notify(int running, RunState state)
1262 VMChangeStateEntry *e;
1264 trace_vm_state_notify(running, state);
1266 for (e = vm_change_state_head.lh_first; e; e = e->entries.le_next) {
1267 e->cb(e->opaque, running, state);
1273 if (!runstate_is_running()) {
1275 runstate_set(RUN_STATE_RUNNING);
1276 vm_state_notify(1, RUN_STATE_RUNNING);
1278 monitor_protocol_event(QEVENT_RESUME, NULL);
1282 /* reset/shutdown handler */
1284 typedef struct QEMUResetEntry {
1285 QTAILQ_ENTRY(QEMUResetEntry) entry;
1286 QEMUResetHandler *func;
1290 static QTAILQ_HEAD(reset_handlers, QEMUResetEntry) reset_handlers =
1291 QTAILQ_HEAD_INITIALIZER(reset_handlers);
1292 static int reset_requested;
1293 static int shutdown_requested, shutdown_signal = -1;
1294 static pid_t shutdown_pid;
1295 static int powerdown_requested;
1296 static int debug_requested;
1297 static int suspend_requested;
1298 static NotifierList suspend_notifiers =
1299 NOTIFIER_LIST_INITIALIZER(suspend_notifiers);
1300 static NotifierList wakeup_notifiers =
1301 NOTIFIER_LIST_INITIALIZER(wakeup_notifiers);
1302 static uint32_t wakeup_reason_mask = ~0;
1303 static RunState vmstop_requested = RUN_STATE_MAX;
1305 int qemu_shutdown_requested_get(void)
1307 return shutdown_requested;
1310 int qemu_reset_requested_get(void)
1312 return reset_requested;
1315 int qemu_shutdown_requested(void)
1317 int r = shutdown_requested;
1318 shutdown_requested = 0;
1322 void qemu_kill_report(void)
1324 if (!qtest_enabled() && shutdown_signal != -1) {
1325 fprintf(stderr, "qemu: terminating on signal %d", shutdown_signal);
1326 if (shutdown_pid == 0) {
1327 /* This happens for eg ^C at the terminal, so it's worth
1328 * avoiding printing an odd message in that case.
1330 fputc('\n', stderr);
1332 fprintf(stderr, " from pid " FMT_pid "\n", shutdown_pid);
1334 shutdown_signal = -1;
1338 int qemu_reset_requested(void)
1340 int r = reset_requested;
1341 reset_requested = 0;
1345 static int qemu_suspend_requested(void)
1347 int r = suspend_requested;
1348 suspend_requested = 0;
1352 int qemu_powerdown_requested(void)
1354 int r = powerdown_requested;
1355 powerdown_requested = 0;
1359 static int qemu_debug_requested(void)
1361 int r = debug_requested;
1362 debug_requested = 0;
1366 /* We use RUN_STATE_MAX but any invalid value will do */
1367 static bool qemu_vmstop_requested(RunState *r)
1369 if (vmstop_requested < RUN_STATE_MAX) {
1370 *r = vmstop_requested;
1371 vmstop_requested = RUN_STATE_MAX;
1378 void qemu_register_reset(QEMUResetHandler *func, void *opaque)
1380 QEMUResetEntry *re = g_malloc0(sizeof(QEMUResetEntry));
1383 re->opaque = opaque;
1384 QTAILQ_INSERT_TAIL(&reset_handlers, re, entry);
1387 void qemu_unregister_reset(QEMUResetHandler *func, void *opaque)
1391 QTAILQ_FOREACH(re, &reset_handlers, entry) {
1392 if (re->func == func && re->opaque == opaque) {
1393 QTAILQ_REMOVE(&reset_handlers, re, entry);
1400 void qemu_system_reset(bool report)
1402 QEMUResetEntry *re, *nre;
1404 /* reset all devices */
1405 QTAILQ_FOREACH_SAFE(re, &reset_handlers, entry, nre) {
1406 re->func(re->opaque);
1409 monitor_protocol_event(QEVENT_RESET, NULL);
1411 cpu_synchronize_all_post_reset();
1414 void qemu_system_reset_request(void)
1417 shutdown_requested = 1;
1419 reset_requested = 1;
1422 qemu_notify_event();
1425 static void qemu_system_suspend(void)
1428 notifier_list_notify(&suspend_notifiers, NULL);
1429 runstate_set(RUN_STATE_SUSPENDED);
1430 monitor_protocol_event(QEVENT_SUSPEND, NULL);
1433 void qemu_system_suspend_request(void)
1435 if (runstate_check(RUN_STATE_SUSPENDED)) {
1438 suspend_requested = 1;
1440 qemu_notify_event();
1443 void qemu_register_suspend_notifier(Notifier *notifier)
1445 notifier_list_add(&suspend_notifiers, notifier);
1448 void qemu_system_wakeup_request(WakeupReason reason)
1450 if (!runstate_check(RUN_STATE_SUSPENDED)) {
1453 if (!(wakeup_reason_mask & (1 << reason))) {
1456 runstate_set(RUN_STATE_RUNNING);
1457 monitor_protocol_event(QEVENT_WAKEUP, NULL);
1458 notifier_list_notify(&wakeup_notifiers, &reason);
1459 reset_requested = 1;
1460 qemu_notify_event();
1463 void qemu_system_wakeup_enable(WakeupReason reason, bool enabled)
1466 wakeup_reason_mask |= (1 << reason);
1468 wakeup_reason_mask &= ~(1 << reason);
1472 void qemu_register_wakeup_notifier(Notifier *notifier)
1474 notifier_list_add(&wakeup_notifiers, notifier);
1477 void qemu_system_killed(int signal, pid_t pid)
1479 shutdown_signal = signal;
1482 qemu_system_shutdown_request();
1485 void qemu_system_shutdown_request(void)
1487 shutdown_requested = 1;
1488 qemu_notify_event();
1491 void qemu_system_powerdown_request(void)
1493 powerdown_requested = 1;
1494 qemu_notify_event();
1497 void qemu_system_debug_request(void)
1499 debug_requested = 1;
1500 qemu_notify_event();
1503 void qemu_system_vmstop_request(RunState state)
1505 vmstop_requested = state;
1506 qemu_notify_event();
1509 qemu_irq qemu_system_powerdown;
1511 static bool main_loop_should_exit(void)
1514 if (qemu_debug_requested()) {
1515 vm_stop(RUN_STATE_DEBUG);
1517 if (qemu_suspend_requested()) {
1518 qemu_system_suspend();
1520 if (qemu_shutdown_requested()) {
1522 monitor_protocol_event(QEVENT_SHUTDOWN, NULL);
1524 vm_stop(RUN_STATE_SHUTDOWN);
1529 if (qemu_reset_requested()) {
1531 cpu_synchronize_all_states();
1532 qemu_system_reset(VMRESET_REPORT);
1534 if (runstate_check(RUN_STATE_INTERNAL_ERROR) ||
1535 runstate_check(RUN_STATE_SHUTDOWN)) {
1536 runstate_set(RUN_STATE_PAUSED);
1539 if (qemu_powerdown_requested()) {
1540 monitor_protocol_event(QEVENT_POWERDOWN, NULL);
1541 qemu_irq_raise(qemu_system_powerdown);
1543 if (qemu_vmstop_requested(&r)) {
1549 static void main_loop(void)
1553 #ifdef CONFIG_PROFILER
1557 nonblocking = !kvm_enabled() && last_io > 0;
1558 #ifdef CONFIG_PROFILER
1559 ti = profile_getclock();
1561 last_io = main_loop_wait(nonblocking);
1562 #ifdef CONFIG_PROFILER
1563 dev_time += profile_getclock() - ti;
1565 } while (!main_loop_should_exit());
1568 static void version(void)
1570 printf("QEMU emulator version " QEMU_VERSION QEMU_PKGVERSION ", Copyright (c) 2003-2008 Fabrice Bellard\n");
1573 static void help(int exitcode)
1576 printf("usage: %s [options] [disk_image]\n\n"
1577 "'disk_image' is a raw hard disk image for IDE hard disk 0\n\n",
1578 error_get_progname());
1580 #define QEMU_OPTIONS_GENERATE_HELP
1581 #include "qemu-options-wrapper.h"
1583 printf("\nDuring emulation, the following keys are useful:\n"
1584 "ctrl-alt-f toggle full screen\n"
1585 "ctrl-alt-n switch to virtual console 'n'\n"
1586 "ctrl-alt toggle mouse and keyboard grab\n"
1588 "When using -nographic, press 'ctrl-a h' to get some help.\n");
1593 #define HAS_ARG 0x0001
1595 typedef struct QEMUOption {
1602 static const QEMUOption qemu_options[] = {
1603 { "h", 0, QEMU_OPTION_h, QEMU_ARCH_ALL },
1604 #define QEMU_OPTIONS_GENERATE_OPTIONS
1605 #include "qemu-options-wrapper.h"
1609 static bool vga_available(void)
1611 return qdev_exists("VGA") || qdev_exists("isa-vga");
1614 static bool cirrus_vga_available(void)
1616 return qdev_exists("cirrus-vga") || qdev_exists("isa-cirrus-vga");
1619 static bool vmware_vga_available(void)
1621 return qdev_exists("vmware-svga");
1624 static void select_vgahw (const char *p)
1628 vga_interface_type = VGA_NONE;
1629 if (strstart(p, "std", &opts)) {
1630 if (vga_available()) {
1631 vga_interface_type = VGA_STD;
1633 fprintf(stderr, "Error: standard VGA not available\n");
1636 } else if (strstart(p, "cirrus", &opts)) {
1637 if (cirrus_vga_available()) {
1638 vga_interface_type = VGA_CIRRUS;
1640 fprintf(stderr, "Error: Cirrus VGA not available\n");
1643 } else if (strstart(p, "vmware", &opts)) {
1644 if (vmware_vga_available()) {
1645 vga_interface_type = VGA_VMWARE;
1647 fprintf(stderr, "Error: VMWare SVGA not available\n");
1650 } else if (strstart(p, "xenfb", &opts)) {
1651 vga_interface_type = VGA_XENFB;
1652 } else if (strstart(p, "qxl", &opts)) {
1653 vga_interface_type = VGA_QXL;
1654 } else if (!strstart(p, "none", &opts)) {
1656 fprintf(stderr, "Unknown vga type: %s\n", p);
1660 const char *nextopt;
1662 if (strstart(opts, ",retrace=", &nextopt)) {
1664 if (strstart(opts, "dumb", &nextopt))
1665 vga_retrace_method = VGA_RETRACE_DUMB;
1666 else if (strstart(opts, "precise", &nextopt))
1667 vga_retrace_method = VGA_RETRACE_PRECISE;
1668 else goto invalid_vga;
1669 } else goto invalid_vga;
1674 static DisplayType select_display(const char *p)
1677 DisplayType display = DT_DEFAULT;
1679 if (strstart(p, "sdl", &opts)) {
1683 const char *nextopt;
1685 if (strstart(opts, ",frame=", &nextopt)) {
1687 if (strstart(opts, "on", &nextopt)) {
1689 } else if (strstart(opts, "off", &nextopt)) {
1692 goto invalid_sdl_args;
1694 } else if (strstart(opts, ",alt_grab=", &nextopt)) {
1696 if (strstart(opts, "on", &nextopt)) {
1698 } else if (strstart(opts, "off", &nextopt)) {
1701 goto invalid_sdl_args;
1703 } else if (strstart(opts, ",ctrl_grab=", &nextopt)) {
1705 if (strstart(opts, "on", &nextopt)) {
1707 } else if (strstart(opts, "off", &nextopt)) {
1710 goto invalid_sdl_args;
1712 } else if (strstart(opts, ",window_close=", &nextopt)) {
1714 if (strstart(opts, "on", &nextopt)) {
1716 } else if (strstart(opts, "off", &nextopt)) {
1719 goto invalid_sdl_args;
1723 fprintf(stderr, "Invalid SDL option string: %s\n", p);
1729 fprintf(stderr, "SDL support is disabled\n");
1732 } else if (strstart(p, "vnc", &opts)) {
1737 const char *nextopt;
1739 if (strstart(opts, "=", &nextopt)) {
1740 vnc_display = nextopt;
1744 fprintf(stderr, "VNC requires a display argument vnc=<display>\n");
1748 fprintf(stderr, "VNC support is disabled\n");
1751 } else if (strstart(p, "curses", &opts)) {
1752 #ifdef CONFIG_CURSES
1753 display = DT_CURSES;
1755 fprintf(stderr, "Curses support is disabled\n");
1758 } else if (strstart(p, "none", &opts)) {
1761 fprintf(stderr, "Unknown display type: %s\n", p);
1768 static int balloon_parse(const char *arg)
1772 if (strcmp(arg, "none") == 0) {
1776 if (!strncmp(arg, "virtio", 6)) {
1777 if (arg[6] == ',') {
1778 /* have params -> parse them */
1779 opts = qemu_opts_parse(qemu_find_opts("device"), arg+7, 0);
1783 /* create empty opts */
1784 opts = qemu_opts_create(qemu_find_opts("device"), NULL, 0, NULL);
1786 qemu_opt_set(opts, "driver", "virtio-balloon");
1793 char *qemu_find_file(int type, const char *name)
1799 /* Try the name as a straight path first */
1800 if (access(name, R_OK) == 0) {
1801 return g_strdup(name);
1804 case QEMU_FILE_TYPE_BIOS:
1807 case QEMU_FILE_TYPE_KEYMAP:
1808 subdir = "keymaps/";
1813 len = strlen(data_dir) + strlen(name) + strlen(subdir) + 2;
1814 buf = g_malloc0(len);
1815 snprintf(buf, len, "%s/%s%s", data_dir, subdir, name);
1816 if (access(buf, R_OK)) {
1823 static int device_help_func(QemuOpts *opts, void *opaque)
1825 return qdev_device_help(opts);
1828 static int device_init_func(QemuOpts *opts, void *opaque)
1832 dev = qdev_device_add(opts);
1838 static int chardev_init_func(QemuOpts *opts, void *opaque)
1840 CharDriverState *chr;
1842 chr = qemu_chr_new_from_opts(opts, NULL);
1848 #ifdef CONFIG_VIRTFS
1849 static int fsdev_init_func(QemuOpts *opts, void *opaque)
1852 ret = qemu_fsdev_add(opts);
1858 static int mon_init_func(QemuOpts *opts, void *opaque)
1860 CharDriverState *chr;
1861 const char *chardev;
1865 mode = qemu_opt_get(opts, "mode");
1869 if (strcmp(mode, "readline") == 0) {
1870 flags = MONITOR_USE_READLINE;
1871 } else if (strcmp(mode, "control") == 0) {
1872 flags = MONITOR_USE_CONTROL;
1874 fprintf(stderr, "unknown monitor mode \"%s\"\n", mode);
1878 if (qemu_opt_get_bool(opts, "pretty", 0))
1879 flags |= MONITOR_USE_PRETTY;
1881 if (qemu_opt_get_bool(opts, "default", 0))
1882 flags |= MONITOR_IS_DEFAULT;
1884 chardev = qemu_opt_get(opts, "chardev");
1885 chr = qemu_chr_find(chardev);
1887 fprintf(stderr, "chardev \"%s\" not found\n", chardev);
1891 monitor_init(chr, flags);
1895 static void monitor_parse(const char *optarg, const char *mode)
1897 static int monitor_device_index = 0;
1903 if (strstart(optarg, "chardev:", &p)) {
1904 snprintf(label, sizeof(label), "%s", p);
1906 snprintf(label, sizeof(label), "compat_monitor%d",
1907 monitor_device_index);
1908 if (monitor_device_index == 0) {
1911 opts = qemu_chr_parse_compat(label, optarg);
1913 fprintf(stderr, "parse error: %s\n", optarg);
1918 opts = qemu_opts_create(qemu_find_opts("mon"), label, 1, NULL);
1920 fprintf(stderr, "duplicate chardev: %s\n", label);
1923 qemu_opt_set(opts, "mode", mode);
1924 qemu_opt_set(opts, "chardev", label);
1926 qemu_opt_set(opts, "default", "on");
1927 monitor_device_index++;
1930 struct device_config {
1932 DEV_USB, /* -usbdevice */
1934 DEV_SERIAL, /* -serial */
1935 DEV_PARALLEL, /* -parallel */
1936 DEV_VIRTCON, /* -virtioconsole */
1937 DEV_DEBUGCON, /* -debugcon */
1938 DEV_GDB, /* -gdb, -s */
1940 const char *cmdline;
1942 QTAILQ_ENTRY(device_config) next;
1944 QTAILQ_HEAD(, device_config) device_configs = QTAILQ_HEAD_INITIALIZER(device_configs);
1946 static void add_device_config(int type, const char *cmdline)
1948 struct device_config *conf;
1950 conf = g_malloc0(sizeof(*conf));
1952 conf->cmdline = cmdline;
1953 loc_save(&conf->loc);
1954 QTAILQ_INSERT_TAIL(&device_configs, conf, next);
1957 static int foreach_device_config(int type, int (*func)(const char *cmdline))
1959 struct device_config *conf;
1962 QTAILQ_FOREACH(conf, &device_configs, next) {
1963 if (conf->type != type)
1965 loc_push_restore(&conf->loc);
1966 rc = func(conf->cmdline);
1967 loc_pop(&conf->loc);
1974 static int serial_parse(const char *devname)
1976 static int index = 0;
1979 if (strcmp(devname, "none") == 0)
1981 if (index == MAX_SERIAL_PORTS) {
1982 fprintf(stderr, "qemu: too many serial ports\n");
1985 snprintf(label, sizeof(label), "serial%d", index);
1986 serial_hds[index] = qemu_chr_new(label, devname, NULL);
1987 if (!serial_hds[index]) {
1988 fprintf(stderr, "qemu: could not connect serial device"
1989 " to character backend '%s'\n", devname);
1996 static int parallel_parse(const char *devname)
1998 static int index = 0;
2001 if (strcmp(devname, "none") == 0)
2003 if (index == MAX_PARALLEL_PORTS) {
2004 fprintf(stderr, "qemu: too many parallel ports\n");
2007 snprintf(label, sizeof(label), "parallel%d", index);
2008 parallel_hds[index] = qemu_chr_new(label, devname, NULL);
2009 if (!parallel_hds[index]) {
2010 fprintf(stderr, "qemu: could not connect parallel device"
2011 " to character backend '%s'\n", devname);
2018 static int virtcon_parse(const char *devname)
2020 QemuOptsList *device = qemu_find_opts("device");
2021 static int index = 0;
2023 QemuOpts *bus_opts, *dev_opts;
2025 if (strcmp(devname, "none") == 0)
2027 if (index == MAX_VIRTIO_CONSOLES) {
2028 fprintf(stderr, "qemu: too many virtio consoles\n");
2032 bus_opts = qemu_opts_create(device, NULL, 0, NULL);
2033 if (arch_type == QEMU_ARCH_S390X) {
2034 qemu_opt_set(bus_opts, "driver", "virtio-serial-s390");
2036 qemu_opt_set(bus_opts, "driver", "virtio-serial-pci");
2039 dev_opts = qemu_opts_create(device, NULL, 0, NULL);
2040 qemu_opt_set(dev_opts, "driver", "virtconsole");
2042 snprintf(label, sizeof(label), "virtcon%d", index);
2043 virtcon_hds[index] = qemu_chr_new(label, devname, NULL);
2044 if (!virtcon_hds[index]) {
2045 fprintf(stderr, "qemu: could not connect virtio console"
2046 " to character backend '%s'\n", devname);
2049 qemu_opt_set(dev_opts, "chardev", label);
2055 static int debugcon_parse(const char *devname)
2059 if (!qemu_chr_new("debugcon", devname, NULL)) {
2062 opts = qemu_opts_create(qemu_find_opts("device"), "debugcon", 1, NULL);
2064 fprintf(stderr, "qemu: already have a debugcon device\n");
2067 qemu_opt_set(opts, "driver", "isa-debugcon");
2068 qemu_opt_set(opts, "chardev", "debugcon");
2072 static QEMUMachine *machine_parse(const char *name)
2074 QEMUMachine *m, *machine = NULL;
2077 machine = find_machine(name);
2082 printf("Supported machines are:\n");
2083 for (m = first_machine; m != NULL; m = m->next) {
2085 printf("%-20s %s (alias of %s)\n", m->alias, m->desc, m->name);
2087 printf("%-20s %s%s\n", m->name, m->desc,
2088 m->is_default ? " (default)" : "");
2090 exit(!name || !is_help_option(name));
2093 static int tcg_init(void)
2095 tcg_exec_init(tcg_tb_size * 1024 * 1024);
2100 const char *opt_name;
2102 int (*available)(void);
2106 { "tcg", "tcg", tcg_available, tcg_init, &tcg_allowed },
2107 { "xen", "Xen", xen_available, xen_init, &xen_allowed },
2108 { "kvm", "KVM", kvm_available, kvm_init, &kvm_allowed },
2109 { "qtest", "QTest", qtest_available, qtest_init, &qtest_allowed },
2112 static int configure_accelerator(void)
2114 const char *p = NULL;
2117 bool accel_initialised = 0;
2118 bool init_failed = 0;
2120 QemuOptsList *list = qemu_find_opts("machine");
2121 if (!QTAILQ_EMPTY(&list->head)) {
2122 p = qemu_opt_get(QTAILQ_FIRST(&list->head), "accel");
2126 /* Use the default "accelerator", tcg */
2130 while (!accel_initialised && *p != '\0') {
2134 p = get_opt_name(buf, sizeof (buf), p, ':');
2135 for (i = 0; i < ARRAY_SIZE(accel_list); i++) {
2136 if (strcmp(accel_list[i].opt_name, buf) == 0) {
2137 *(accel_list[i].allowed) = 1;
2138 ret = accel_list[i].init();
2141 if (!accel_list[i].available()) {
2142 printf("%s not supported for this target\n",
2143 accel_list[i].name);
2145 fprintf(stderr, "failed to initialize %s: %s\n",
2149 *(accel_list[i].allowed) = 0;
2151 accel_initialised = 1;
2156 if (i == ARRAY_SIZE(accel_list)) {
2157 fprintf(stderr, "\"%s\" accelerator does not exist.\n", buf);
2161 if (!accel_initialised) {
2162 fprintf(stderr, "No accelerator found!\n");
2167 fprintf(stderr, "Back to %s accelerator.\n", accel_list[i].name);
2170 return !accel_initialised;
2173 void qemu_add_exit_notifier(Notifier *notify)
2175 notifier_list_add(&exit_notifiers, notify);
2178 void qemu_remove_exit_notifier(Notifier *notify)
2180 notifier_remove(notify);
2183 static void qemu_run_exit_notifiers(void)
2185 notifier_list_notify(&exit_notifiers, NULL);
2188 void qemu_add_machine_init_done_notifier(Notifier *notify)
2190 notifier_list_add(&machine_init_done_notifiers, notify);
2193 static void qemu_run_machine_init_done_notifiers(void)
2195 notifier_list_notify(&machine_init_done_notifiers, NULL);
2198 static const QEMUOption *lookup_opt(int argc, char **argv,
2199 const char **poptarg, int *poptind)
2201 const QEMUOption *popt;
2202 int optind = *poptind;
2203 char *r = argv[optind];
2206 loc_set_cmdline(argv, optind, 1);
2208 /* Treat --foo the same as -foo. */
2211 popt = qemu_options;
2214 error_report("invalid option");
2217 if (!strcmp(popt->name, r + 1))
2221 if (popt->flags & HAS_ARG) {
2222 if (optind >= argc) {
2223 error_report("requires an argument");
2226 optarg = argv[optind++];
2227 loc_set_cmdline(argv, optind - 2, 2);
2238 static gpointer malloc_and_trace(gsize n_bytes)
2240 void *ptr = malloc(n_bytes);
2241 trace_g_malloc(n_bytes, ptr);
2245 static gpointer realloc_and_trace(gpointer mem, gsize n_bytes)
2247 void *ptr = realloc(mem, n_bytes);
2248 trace_g_realloc(mem, n_bytes, ptr);
2252 static void free_and_trace(gpointer mem)
2258 int qemu_init_main_loop(void)
2260 return main_loop_init();
2263 int main(int argc, char **argv, char **envp)
2266 int snapshot, linux_boot;
2267 const char *icount_option = NULL;
2268 const char *initrd_filename;
2269 const char *kernel_filename, *kernel_cmdline;
2270 char boot_devices[33] = "cad"; /* default to HD->floppy->CD-ROM */
2272 DisplayChangeListener *dcl;
2273 int cyls, heads, secs, translation;
2274 QemuOpts *hda_opts = NULL, *opts, *machine_opts;
2275 QemuOptsList *olist;
2278 const char *loadvm = NULL;
2279 QEMUMachine *machine;
2280 const char *cpu_model;
2281 const char *vga_model = "none";
2282 const char *pid_file = NULL;
2283 const char *incoming = NULL;
2285 int show_vnc_port = 0;
2287 bool defconfig = true;
2288 bool userconfig = true;
2289 const char *log_mask = NULL;
2290 const char *log_file = NULL;
2291 GMemVTable mem_trace = {
2292 .malloc = malloc_and_trace,
2293 .realloc = realloc_and_trace,
2294 .free = free_and_trace,
2296 const char *trace_events = NULL;
2297 const char *trace_file = NULL;
2299 atexit(qemu_run_exit_notifiers);
2300 error_set_progname(argv[0]);
2302 g_mem_set_vtable(&mem_trace);
2303 if (!g_thread_supported()) {
2304 #if !GLIB_CHECK_VERSION(2, 31, 0)
2305 g_thread_init(NULL);
2307 fprintf(stderr, "glib threading failed to initialize.\n");
2312 module_call_init(MODULE_INIT_QOM);
2317 rtc_clock = host_clock;
2319 qemu_cache_utils_init(envp);
2321 QLIST_INIT (&vm_change_state_head);
2322 os_setup_early_signal_handling();
2324 module_call_init(MODULE_INIT_MACHINE);
2325 machine = find_default_machine();
2329 cyls = heads = secs = 0;
2330 translation = BIOS_ATA_TRANSLATION_AUTO;
2332 for (i = 0; i < MAX_NODES; i++) {
2334 node_cpumask[i] = 0;
2342 /* first pass of option parsing */
2344 while (optind < argc) {
2345 if (argv[optind][0] != '-') {
2350 const QEMUOption *popt;
2352 popt = lookup_opt(argc, argv, &optarg, &optind);
2353 switch (popt->index) {
2354 case QEMU_OPTION_nodefconfig:
2357 case QEMU_OPTION_nouserconfig:
2366 ret = qemu_read_default_config_files(userconfig);
2372 /* second pass of option parsing */
2377 if (argv[optind][0] != '-') {
2378 hda_opts = drive_add(IF_DEFAULT, 0, argv[optind++], HD_OPTS);
2380 const QEMUOption *popt;
2382 popt = lookup_opt(argc, argv, &optarg, &optind);
2383 if (!(popt->arch_mask & arch_type)) {
2384 printf("Option %s not supported for this target\n", popt->name);
2387 switch(popt->index) {
2389 machine = machine_parse(optarg);
2391 case QEMU_OPTION_cpu:
2392 /* hw initialization will check this */
2395 case QEMU_OPTION_hda:
2399 snprintf(buf, sizeof(buf), "%s", HD_OPTS);
2401 snprintf(buf, sizeof(buf),
2402 "%s,cyls=%d,heads=%d,secs=%d%s",
2403 HD_OPTS , cyls, heads, secs,
2404 translation == BIOS_ATA_TRANSLATION_LBA ?
2406 translation == BIOS_ATA_TRANSLATION_NONE ?
2407 ",trans=none" : "");
2408 drive_add(IF_DEFAULT, 0, optarg, buf);
2411 case QEMU_OPTION_hdb:
2412 case QEMU_OPTION_hdc:
2413 case QEMU_OPTION_hdd:
2414 drive_add(IF_DEFAULT, popt->index - QEMU_OPTION_hda, optarg,
2417 case QEMU_OPTION_drive:
2418 if (drive_def(optarg) == NULL) {
2422 case QEMU_OPTION_set:
2423 if (qemu_set_option(optarg) != 0)
2426 case QEMU_OPTION_global:
2427 if (qemu_global_option(optarg) != 0)
2430 case QEMU_OPTION_mtdblock:
2431 drive_add(IF_MTD, -1, optarg, MTD_OPTS);
2433 case QEMU_OPTION_sd:
2434 drive_add(IF_SD, 0, optarg, SD_OPTS);
2436 case QEMU_OPTION_pflash:
2437 drive_add(IF_PFLASH, -1, optarg, PFLASH_OPTS);
2439 case QEMU_OPTION_snapshot:
2442 case QEMU_OPTION_hdachs:
2446 cyls = strtol(p, (char **)&p, 0);
2447 if (cyls < 1 || cyls > 16383)
2452 heads = strtol(p, (char **)&p, 0);
2453 if (heads < 1 || heads > 16)
2458 secs = strtol(p, (char **)&p, 0);
2459 if (secs < 1 || secs > 63)
2463 if (!strcmp(p, "none"))
2464 translation = BIOS_ATA_TRANSLATION_NONE;
2465 else if (!strcmp(p, "lba"))
2466 translation = BIOS_ATA_TRANSLATION_LBA;
2467 else if (!strcmp(p, "auto"))
2468 translation = BIOS_ATA_TRANSLATION_AUTO;
2471 } else if (*p != '\0') {
2473 fprintf(stderr, "qemu: invalid physical CHS format\n");
2476 if (hda_opts != NULL) {
2478 snprintf(num, sizeof(num), "%d", cyls);
2479 qemu_opt_set(hda_opts, "cyls", num);
2480 snprintf(num, sizeof(num), "%d", heads);
2481 qemu_opt_set(hda_opts, "heads", num);
2482 snprintf(num, sizeof(num), "%d", secs);
2483 qemu_opt_set(hda_opts, "secs", num);
2484 if (translation == BIOS_ATA_TRANSLATION_LBA)
2485 qemu_opt_set(hda_opts, "trans", "lba");
2486 if (translation == BIOS_ATA_TRANSLATION_NONE)
2487 qemu_opt_set(hda_opts, "trans", "none");
2491 case QEMU_OPTION_numa:
2492 if (nb_numa_nodes >= MAX_NODES) {
2493 fprintf(stderr, "qemu: too many NUMA nodes\n");
2498 case QEMU_OPTION_display:
2499 display_type = select_display(optarg);
2501 case QEMU_OPTION_nographic:
2502 display_type = DT_NOGRAPHIC;
2504 case QEMU_OPTION_curses:
2505 #ifdef CONFIG_CURSES
2506 display_type = DT_CURSES;
2508 fprintf(stderr, "Curses support is disabled\n");
2512 case QEMU_OPTION_portrait:
2513 graphic_rotate = 90;
2515 case QEMU_OPTION_rotate:
2516 graphic_rotate = strtol(optarg, (char **) &optarg, 10);
2517 if (graphic_rotate != 0 && graphic_rotate != 90 &&
2518 graphic_rotate != 180 && graphic_rotate != 270) {
2520 "qemu: only 90, 180, 270 deg rotation is available\n");
2524 case QEMU_OPTION_kernel:
2525 qemu_opts_set(qemu_find_opts("machine"), 0, "kernel", optarg);
2527 case QEMU_OPTION_initrd:
2528 qemu_opts_set(qemu_find_opts("machine"), 0, "initrd", optarg);
2530 case QEMU_OPTION_append:
2531 qemu_opts_set(qemu_find_opts("machine"), 0, "append", optarg);
2533 case QEMU_OPTION_dtb:
2534 qemu_opts_set(qemu_find_opts("machine"), 0, "dtb", optarg);
2536 case QEMU_OPTION_cdrom:
2537 drive_add(IF_DEFAULT, 2, optarg, CDROM_OPTS);
2539 case QEMU_OPTION_boot:
2541 static const char * const params[] = {
2542 "order", "once", "menu",
2543 "splash", "splash-time", NULL
2545 char buf[sizeof(boot_devices)];
2546 char *standard_boot_devices;
2549 if (!strchr(optarg, '=')) {
2551 pstrcpy(buf, sizeof(buf), optarg);
2552 } else if (check_params(buf, sizeof(buf), params, optarg) < 0) {
2554 "qemu: unknown boot parameter '%s' in '%s'\n",
2560 get_param_value(buf, sizeof(buf), "order", optarg)) {
2561 validate_bootdevices(buf);
2562 pstrcpy(boot_devices, sizeof(boot_devices), buf);
2565 if (get_param_value(buf, sizeof(buf),
2567 validate_bootdevices(buf);
2568 standard_boot_devices = g_strdup(boot_devices);
2569 pstrcpy(boot_devices, sizeof(boot_devices), buf);
2570 qemu_register_reset(restore_boot_devices,
2571 standard_boot_devices);
2573 if (get_param_value(buf, sizeof(buf),
2575 if (!strcmp(buf, "on")) {
2577 } else if (!strcmp(buf, "off")) {
2581 "qemu: invalid option value '%s'\n",
2586 qemu_opts_parse(qemu_find_opts("boot-opts"),
2591 case QEMU_OPTION_fda:
2592 case QEMU_OPTION_fdb:
2593 drive_add(IF_FLOPPY, popt->index - QEMU_OPTION_fda,
2596 case QEMU_OPTION_no_fd_bootchk:
2599 case QEMU_OPTION_netdev:
2600 if (net_client_parse(qemu_find_opts("netdev"), optarg) == -1) {
2604 case QEMU_OPTION_net:
2605 if (net_client_parse(qemu_find_opts("net"), optarg) == -1) {
2609 #ifdef CONFIG_LIBISCSI
2610 case QEMU_OPTION_iscsi:
2611 opts = qemu_opts_parse(qemu_find_opts("iscsi"), optarg, 0);
2618 case QEMU_OPTION_tftp:
2619 legacy_tftp_prefix = optarg;
2621 case QEMU_OPTION_bootp:
2622 legacy_bootp_filename = optarg;
2624 case QEMU_OPTION_redir:
2625 if (net_slirp_redir(optarg) < 0)
2629 case QEMU_OPTION_bt:
2630 add_device_config(DEV_BT, optarg);
2632 case QEMU_OPTION_audio_help:
2633 if (!(audio_available())) {
2634 printf("Option %s not supported for this target\n", popt->name);
2640 case QEMU_OPTION_soundhw:
2641 if (!(audio_available())) {
2642 printf("Option %s not supported for this target\n", popt->name);
2645 select_soundhw (optarg);
2650 case QEMU_OPTION_version:
2654 case QEMU_OPTION_m: {
2658 value = strtosz(optarg, &end);
2659 if (value < 0 || *end) {
2660 fprintf(stderr, "qemu: invalid ram size: %s\n", optarg);
2664 if (value != (uint64_t)(ram_addr_t)value) {
2665 fprintf(stderr, "qemu: ram size too large\n");
2671 case QEMU_OPTION_mempath:
2675 case QEMU_OPTION_mem_prealloc:
2686 add_device_config(DEV_GDB, "tcp::" DEFAULT_GDBSTUB_PORT);
2688 case QEMU_OPTION_gdb:
2689 add_device_config(DEV_GDB, optarg);
2694 case QEMU_OPTION_bios:
2697 case QEMU_OPTION_singlestep:
2704 keyboard_layout = optarg;
2706 case QEMU_OPTION_localtime:
2709 case QEMU_OPTION_vga:
2718 w = strtol(p, (char **)&p, 10);
2721 fprintf(stderr, "qemu: invalid resolution or depth\n");
2727 h = strtol(p, (char **)&p, 10);
2732 depth = strtol(p, (char **)&p, 10);
2733 if (depth != 8 && depth != 15 && depth != 16 &&
2734 depth != 24 && depth != 32)
2736 } else if (*p == '\0') {
2737 depth = graphic_depth;
2744 graphic_depth = depth;
2747 case QEMU_OPTION_echr:
2750 term_escape_char = strtol(optarg, &r, 0);
2752 printf("Bad argument to echr\n");
2755 case QEMU_OPTION_monitor:
2756 monitor_parse(optarg, "readline");
2757 default_monitor = 0;
2759 case QEMU_OPTION_qmp:
2760 monitor_parse(optarg, "control");
2761 default_monitor = 0;
2763 case QEMU_OPTION_mon:
2764 opts = qemu_opts_parse(qemu_find_opts("mon"), optarg, 1);
2768 default_monitor = 0;
2770 case QEMU_OPTION_chardev:
2771 opts = qemu_opts_parse(qemu_find_opts("chardev"), optarg, 1);
2776 case QEMU_OPTION_fsdev:
2777 olist = qemu_find_opts("fsdev");
2779 fprintf(stderr, "fsdev is not supported by this qemu build.\n");
2782 opts = qemu_opts_parse(olist, optarg, 1);
2784 fprintf(stderr, "parse error: %s\n", optarg);
2788 case QEMU_OPTION_virtfs: {
2791 const char *writeout, *sock_fd, *socket;
2793 olist = qemu_find_opts("virtfs");
2795 fprintf(stderr, "virtfs is not supported by this qemu build.\n");
2798 opts = qemu_opts_parse(olist, optarg, 1);
2800 fprintf(stderr, "parse error: %s\n", optarg);
2804 if (qemu_opt_get(opts, "fsdriver") == NULL ||
2805 qemu_opt_get(opts, "mount_tag") == NULL) {
2806 fprintf(stderr, "Usage: -virtfs fsdriver,mount_tag=tag.\n");
2809 fsdev = qemu_opts_create(qemu_find_opts("fsdev"),
2810 qemu_opt_get(opts, "mount_tag"),
2813 fprintf(stderr, "duplicate fsdev id: %s\n",
2814 qemu_opt_get(opts, "mount_tag"));
2818 writeout = qemu_opt_get(opts, "writeout");
2820 #ifdef CONFIG_SYNC_FILE_RANGE
2821 qemu_opt_set(fsdev, "writeout", writeout);
2823 fprintf(stderr, "writeout=immediate not supported on "
2828 qemu_opt_set(fsdev, "fsdriver", qemu_opt_get(opts, "fsdriver"));
2829 qemu_opt_set(fsdev, "path", qemu_opt_get(opts, "path"));
2830 qemu_opt_set(fsdev, "security_model",
2831 qemu_opt_get(opts, "security_model"));
2832 socket = qemu_opt_get(opts, "socket");
2834 qemu_opt_set(fsdev, "socket", socket);
2836 sock_fd = qemu_opt_get(opts, "sock_fd");
2838 qemu_opt_set(fsdev, "sock_fd", sock_fd);
2841 qemu_opt_set_bool(fsdev, "readonly",
2842 qemu_opt_get_bool(opts, "readonly", 0));
2843 device = qemu_opts_create(qemu_find_opts("device"), NULL, 0,
2845 qemu_opt_set(device, "driver", "virtio-9p-pci");
2846 qemu_opt_set(device, "fsdev",
2847 qemu_opt_get(opts, "mount_tag"));
2848 qemu_opt_set(device, "mount_tag",
2849 qemu_opt_get(opts, "mount_tag"));
2852 case QEMU_OPTION_virtfs_synth: {
2856 fsdev = qemu_opts_create(qemu_find_opts("fsdev"), "v_synth",
2859 fprintf(stderr, "duplicate option: %s\n", "virtfs_synth");
2862 qemu_opt_set(fsdev, "fsdriver", "synth");
2864 device = qemu_opts_create(qemu_find_opts("device"), NULL, 0,
2866 qemu_opt_set(device, "driver", "virtio-9p-pci");
2867 qemu_opt_set(device, "fsdev", "v_synth");
2868 qemu_opt_set(device, "mount_tag", "v_synth");
2871 case QEMU_OPTION_serial:
2872 add_device_config(DEV_SERIAL, optarg);
2874 if (strncmp(optarg, "mon:", 4) == 0) {
2875 default_monitor = 0;
2878 case QEMU_OPTION_watchdog:
2881 "qemu: only one watchdog option may be given\n");
2886 case QEMU_OPTION_watchdog_action:
2887 if (select_watchdog_action(optarg) == -1) {
2888 fprintf(stderr, "Unknown -watchdog-action parameter\n");
2892 case QEMU_OPTION_virtiocon:
2893 add_device_config(DEV_VIRTCON, optarg);
2894 default_virtcon = 0;
2895 if (strncmp(optarg, "mon:", 4) == 0) {
2896 default_monitor = 0;
2899 case QEMU_OPTION_parallel:
2900 add_device_config(DEV_PARALLEL, optarg);
2901 default_parallel = 0;
2902 if (strncmp(optarg, "mon:", 4) == 0) {
2903 default_monitor = 0;
2906 case QEMU_OPTION_debugcon:
2907 add_device_config(DEV_DEBUGCON, optarg);
2909 case QEMU_OPTION_loadvm:
2912 case QEMU_OPTION_full_screen:
2916 case QEMU_OPTION_no_frame:
2919 case QEMU_OPTION_alt_grab:
2922 case QEMU_OPTION_ctrl_grab:
2925 case QEMU_OPTION_no_quit:
2928 case QEMU_OPTION_sdl:
2929 display_type = DT_SDL;
2932 case QEMU_OPTION_no_frame:
2933 case QEMU_OPTION_alt_grab:
2934 case QEMU_OPTION_ctrl_grab:
2935 case QEMU_OPTION_no_quit:
2936 case QEMU_OPTION_sdl:
2937 fprintf(stderr, "SDL support is disabled\n");
2940 case QEMU_OPTION_pidfile:
2943 case QEMU_OPTION_win2k_hack:
2944 win2k_install_hack = 1;
2946 case QEMU_OPTION_rtc_td_hack: {
2947 static GlobalProperty slew_lost_ticks[] = {
2949 .driver = "mc146818rtc",
2950 .property = "lost_tick_policy",
2953 { /* end of list */ }
2956 qdev_prop_register_global_list(slew_lost_ticks);
2959 case QEMU_OPTION_acpitable:
2960 do_acpitable_option(optarg);
2962 case QEMU_OPTION_smbios:
2963 do_smbios_option(optarg);
2965 case QEMU_OPTION_enable_kvm:
2966 olist = qemu_find_opts("machine");
2967 qemu_opts_parse(olist, "accel=kvm", 0);
2969 case QEMU_OPTION_machine:
2970 olist = qemu_find_opts("machine");
2971 opts = qemu_opts_parse(olist, optarg, 1);
2973 fprintf(stderr, "parse error: %s\n", optarg);
2976 optarg = qemu_opt_get(opts, "type");
2978 machine = machine_parse(optarg);
2981 case QEMU_OPTION_usb:
2984 case QEMU_OPTION_usbdevice:
2986 add_device_config(DEV_USB, optarg);
2988 case QEMU_OPTION_device:
2989 if (!qemu_opts_parse(qemu_find_opts("device"), optarg, 1)) {
2993 case QEMU_OPTION_smp:
2996 fprintf(stderr, "Invalid number of CPUs\n");
2999 if (max_cpus < smp_cpus) {
3000 fprintf(stderr, "maxcpus must be equal to or greater than "
3004 if (max_cpus > 255) {
3005 fprintf(stderr, "Unsupported number of maxcpus\n");
3009 case QEMU_OPTION_vnc:
3012 vnc_display = optarg;
3014 fprintf(stderr, "VNC support is disabled\n");
3018 case QEMU_OPTION_no_acpi:
3021 case QEMU_OPTION_no_hpet:
3024 case QEMU_OPTION_balloon:
3025 if (balloon_parse(optarg) < 0) {
3026 fprintf(stderr, "Unknown -balloon argument %s\n", optarg);
3030 case QEMU_OPTION_no_reboot:
3033 case QEMU_OPTION_no_shutdown:
3036 case QEMU_OPTION_show_cursor:
3039 case QEMU_OPTION_uuid:
3040 if(qemu_uuid_parse(optarg, qemu_uuid) < 0) {
3041 fprintf(stderr, "Fail to parse UUID string."
3042 " Wrong format.\n");
3046 case QEMU_OPTION_option_rom:
3047 if (nb_option_roms >= MAX_OPTION_ROMS) {
3048 fprintf(stderr, "Too many option ROMs\n");
3051 opts = qemu_opts_parse(qemu_find_opts("option-rom"), optarg, 1);
3052 option_rom[nb_option_roms].name = qemu_opt_get(opts, "romfile");
3053 option_rom[nb_option_roms].bootindex =
3054 qemu_opt_get_number(opts, "bootindex", -1);
3055 if (!option_rom[nb_option_roms].name) {
3056 fprintf(stderr, "Option ROM file is not specified\n");
3061 case QEMU_OPTION_semihosting:
3062 semihosting_enabled = 1;
3064 case QEMU_OPTION_name:
3065 qemu_name = g_strdup(optarg);
3067 char *p = strchr(qemu_name, ',');
3070 if (strncmp(p, "process=", 8)) {
3071 fprintf(stderr, "Unknown subargument %s to -name\n", p);
3075 os_set_proc_name(p);
3079 case QEMU_OPTION_prom_env:
3080 if (nb_prom_envs >= MAX_PROM_ENVS) {
3081 fprintf(stderr, "Too many prom variables\n");
3084 prom_envs[nb_prom_envs] = optarg;
3087 case QEMU_OPTION_old_param:
3090 case QEMU_OPTION_clock:
3091 configure_alarms(optarg);
3093 case QEMU_OPTION_startdate:
3094 configure_rtc_date_offset(optarg, 1);
3096 case QEMU_OPTION_rtc:
3097 opts = qemu_opts_parse(qemu_find_opts("rtc"), optarg, 0);
3101 configure_rtc(opts);
3103 case QEMU_OPTION_tb_size:
3104 tcg_tb_size = strtol(optarg, NULL, 0);
3105 if (tcg_tb_size < 0) {
3109 case QEMU_OPTION_icount:
3110 icount_option = optarg;
3112 case QEMU_OPTION_incoming:
3114 runstate_set(RUN_STATE_INMIGRATE);
3116 case QEMU_OPTION_nodefaults:
3118 default_parallel = 0;
3119 default_virtcon = 0;
3120 default_monitor = 0;
3127 case QEMU_OPTION_xen_domid:
3128 if (!(xen_available())) {
3129 printf("Option %s not supported for this target\n", popt->name);
3132 xen_domid = atoi(optarg);
3134 case QEMU_OPTION_xen_create:
3135 if (!(xen_available())) {
3136 printf("Option %s not supported for this target\n", popt->name);
3139 xen_mode = XEN_CREATE;
3141 case QEMU_OPTION_xen_attach:
3142 if (!(xen_available())) {
3143 printf("Option %s not supported for this target\n", popt->name);
3146 xen_mode = XEN_ATTACH;
3148 case QEMU_OPTION_trace:
3150 opts = qemu_opts_parse(qemu_find_opts("trace"), optarg, 0);
3154 trace_events = qemu_opt_get(opts, "events");
3155 trace_file = qemu_opt_get(opts, "file");
3158 case QEMU_OPTION_readconfig:
3160 int ret = qemu_read_config_file(optarg);
3162 fprintf(stderr, "read config %s: %s\n", optarg,
3168 case QEMU_OPTION_spice:
3169 olist = qemu_find_opts("spice");
3171 fprintf(stderr, "spice is not supported by this qemu build.\n");
3174 opts = qemu_opts_parse(olist, optarg, 0);
3176 fprintf(stderr, "parse error: %s\n", optarg);
3180 case QEMU_OPTION_writeconfig:
3183 if (strcmp(optarg, "-") == 0) {
3186 fp = fopen(optarg, "w");
3188 fprintf(stderr, "open %s: %s\n", optarg, strerror(errno));
3192 qemu_config_write(fp);
3196 case QEMU_OPTION_qtest:
3197 qtest_chrdev = optarg;
3199 case QEMU_OPTION_qtest_log:
3203 os_parse_cmd_args(popt->index, optarg);
3209 if (machine->hw_version) {
3210 qemu_set_version(machine->hw_version);
3213 /* Init CPU def lists, based on config
3214 * - Must be called after all the qemu_read_config_file() calls
3215 * - Must be called before list_cpus()
3216 * - Must be called before machine->init()
3220 if (cpu_model && is_help_option(cpu_model)) {
3221 list_cpus(stdout, &fprintf, cpu_model);
3225 /* Open the logfile at this point, if necessary. We can't open the logfile
3226 * when encountering either of the logging options (-d or -D) because the
3227 * other one may be encountered later on the command line, changing the
3228 * location or level of logging.
3232 set_cpu_log_filename(log_file);
3234 set_cpu_log(log_mask);
3237 if (!trace_backend_init(trace_events, trace_file)) {
3241 /* If no data_dir is specified then try to find it relative to the
3244 data_dir = os_find_datadir(argv[0]);
3246 /* If all else fails use the install path specified when building. */
3248 data_dir = CONFIG_QEMU_DATADIR;
3251 if (machine == NULL) {
3252 fprintf(stderr, "No machine found.\n");
3257 * Default to max_cpus = smp_cpus, in case the user doesn't
3258 * specify a max_cpus value.
3261 max_cpus = smp_cpus;
3263 machine->max_cpus = machine->max_cpus ?: 1; /* Default to UP */
3264 if (smp_cpus > machine->max_cpus) {
3265 fprintf(stderr, "Number of SMP cpus requested (%d), exceeds max cpus "
3266 "supported by machine `%s' (%d)\n", smp_cpus, machine->name,
3272 * Get the default machine options from the machine if it is not already
3273 * specified either by the configuration file or by the command line.
3275 if (machine->default_machine_opts) {
3276 qemu_opts_set_defaults(qemu_find_opts("machine"),
3277 machine->default_machine_opts, 0);
3280 qemu_opts_foreach(qemu_find_opts("device"), default_driver_check, NULL, 0);
3281 qemu_opts_foreach(qemu_find_opts("global"), default_driver_check, NULL, 0);
3283 if (machine->no_serial) {
3286 if (machine->no_parallel) {
3287 default_parallel = 0;
3289 if (!machine->use_virtcon) {
3290 default_virtcon = 0;
3292 if (machine->no_floppy) {
3295 if (machine->no_cdrom) {
3298 if (machine->no_sdcard) {
3302 if (display_type == DT_NOGRAPHIC) {
3303 if (default_parallel)
3304 add_device_config(DEV_PARALLEL, "null");
3305 if (default_serial && default_monitor) {
3306 add_device_config(DEV_SERIAL, "mon:stdio");
3307 } else if (default_virtcon && default_monitor) {
3308 add_device_config(DEV_VIRTCON, "mon:stdio");
3311 add_device_config(DEV_SERIAL, "stdio");
3312 if (default_virtcon)
3313 add_device_config(DEV_VIRTCON, "stdio");
3314 if (default_monitor)
3315 monitor_parse("stdio", "readline");
3319 add_device_config(DEV_SERIAL, "vc:80Cx24C");
3320 if (default_parallel)
3321 add_device_config(DEV_PARALLEL, "vc:80Cx24C");
3322 if (default_monitor)
3323 monitor_parse("vc:80Cx24C", "readline");
3324 if (default_virtcon)
3325 add_device_config(DEV_VIRTCON, "vc:80Cx24C");
3330 if (qemu_opts_foreach(qemu_find_opts("chardev"), chardev_init_func, NULL, 1) != 0)
3332 #ifdef CONFIG_VIRTFS
3333 if (qemu_opts_foreach(qemu_find_opts("fsdev"), fsdev_init_func, NULL, 1) != 0) {
3340 if (pid_file && qemu_create_pidfile(pid_file) != 0) {
3345 /* init the memory */
3346 if (ram_size == 0) {
3347 ram_size = DEFAULT_RAM_SIZE * 1024 * 1024;
3350 configure_accelerator();
3352 qemu_init_cpu_loop();
3353 if (qemu_init_main_loop()) {
3354 fprintf(stderr, "qemu_init_main_loop failed\n");
3358 machine_opts = qemu_opts_find(qemu_find_opts("machine"), 0);
3360 kernel_filename = qemu_opt_get(machine_opts, "kernel");
3361 initrd_filename = qemu_opt_get(machine_opts, "initrd");
3362 kernel_cmdline = qemu_opt_get(machine_opts, "append");
3364 kernel_filename = initrd_filename = kernel_cmdline = NULL;
3367 if (!kernel_cmdline) {
3368 kernel_cmdline = "";
3371 linux_boot = (kernel_filename != NULL);
3373 if (!linux_boot && *kernel_cmdline != '\0') {
3374 fprintf(stderr, "-append only allowed with -kernel option\n");
3378 if (!linux_boot && initrd_filename != NULL) {
3379 fprintf(stderr, "-initrd only allowed with -kernel option\n");
3383 if (!linux_boot && machine_opts && qemu_opt_get(machine_opts, "dtb")) {
3384 fprintf(stderr, "-dtb only allowed with -kernel option\n");
3388 os_set_line_buffering();
3390 if (init_timer_alarm() < 0) {
3391 fprintf(stderr, "could not initialize alarm timer\n");
3396 /* spice needs the timers to be initialized by this point */
3400 if (icount_option && (kvm_enabled() || xen_enabled())) {
3401 fprintf(stderr, "-icount is not allowed with kvm or xen\n");
3404 configure_icount(icount_option);
3406 if (net_init_clients() < 0) {
3410 /* init the bluetooth world */
3411 if (foreach_device_config(DEV_BT, bt_parse))
3414 if (!xen_enabled()) {
3415 /* On 32-bit hosts, QEMU is limited by virtual address space */
3416 if (ram_size > (2047 << 20) && HOST_LONG_BITS == 32) {
3417 fprintf(stderr, "qemu: at most 2047 MB RAM can be simulated\n");
3422 cpu_exec_init_all();
3424 bdrv_init_with_whitelist();
3428 /* open the virtual block devices */
3430 qemu_opts_foreach(qemu_find_opts("drive"), drive_enable_snapshot, NULL, 0);
3431 if (qemu_opts_foreach(qemu_find_opts("drive"), drive_init_func, &machine->use_scsi, 1) != 0)
3434 default_drive(default_cdrom, snapshot, machine->use_scsi,
3435 IF_DEFAULT, 2, CDROM_OPTS);
3436 default_drive(default_floppy, snapshot, machine->use_scsi,
3437 IF_FLOPPY, 0, FD_OPTS);
3438 default_drive(default_sdcard, snapshot, machine->use_scsi,
3441 register_savevm_live(NULL, "ram", 0, 4, &savevm_ram_handlers, NULL);
3443 if (nb_numa_nodes > 0) {
3446 if (nb_numa_nodes > MAX_NODES) {
3447 nb_numa_nodes = MAX_NODES;
3450 /* If no memory size if given for any node, assume the default case
3451 * and distribute the available memory equally across all nodes
3453 for (i = 0; i < nb_numa_nodes; i++) {
3454 if (node_mem[i] != 0)
3457 if (i == nb_numa_nodes) {
3458 uint64_t usedmem = 0;
3460 /* On Linux, the each node's border has to be 8MB aligned,
3461 * the final node gets the rest.
3463 for (i = 0; i < nb_numa_nodes - 1; i++) {
3464 node_mem[i] = (ram_size / nb_numa_nodes) & ~((1 << 23UL) - 1);
3465 usedmem += node_mem[i];
3467 node_mem[i] = ram_size - usedmem;
3470 for (i = 0; i < nb_numa_nodes; i++) {
3471 if (node_cpumask[i] != 0)
3474 /* assigning the VCPUs round-robin is easier to implement, guest OSes
3475 * must cope with this anyway, because there are BIOSes out there in
3476 * real machines which also use this scheme.
3478 if (i == nb_numa_nodes) {
3479 for (i = 0; i < max_cpus; i++) {
3480 node_cpumask[i % nb_numa_nodes] |= 1 << i;
3485 if (qemu_opts_foreach(qemu_find_opts("mon"), mon_init_func, NULL, 1) != 0) {
3489 if (foreach_device_config(DEV_SERIAL, serial_parse) < 0)
3491 if (foreach_device_config(DEV_PARALLEL, parallel_parse) < 0)
3493 if (foreach_device_config(DEV_VIRTCON, virtcon_parse) < 0)
3495 if (foreach_device_config(DEV_DEBUGCON, debugcon_parse) < 0)
3498 /* If no default VGA is requested, the default is "none". */
3499 if (default_vga && cirrus_vga_available()) {
3500 vga_model = "cirrus";
3502 select_vgahw(vga_model);
3504 if (qemu_opts_foreach(qemu_find_opts("device"), device_help_func, NULL, 0) != 0)
3508 i = select_watchdog(watchdog);
3510 exit (i == 1 ? 1 : 0);
3513 if (machine->compat_props) {
3514 qdev_prop_register_global_list(machine->compat_props);
3518 qdev_machine_init();
3520 machine->init(ram_size, boot_devices,
3521 kernel_filename, kernel_cmdline, initrd_filename, cpu_model);
3523 cpu_synchronize_all_post_init();
3527 current_machine = machine;
3529 /* init USB devices */
3531 if (foreach_device_config(DEV_USB, usb_parse) < 0)
3535 /* init generic devices */
3536 if (qemu_opts_foreach(qemu_find_opts("device"), device_init_func, NULL, 1) != 0)
3539 net_check_clients();
3541 /* just use the first displaystate for the moment */
3542 ds = get_displaystate();
3546 if (display_type == DT_DEFAULT && !display_remote) {
3547 #if defined(CONFIG_SDL) || defined(CONFIG_COCOA)
3548 display_type = DT_SDL;
3549 #elif defined(CONFIG_VNC)
3550 vnc_display = "localhost:0,to=99";
3553 display_type = DT_NONE;
3558 /* init local displays */
3559 switch (display_type) {
3562 #if defined(CONFIG_CURSES)
3564 curses_display_init(ds, full_screen);
3567 #if defined(CONFIG_SDL)
3569 sdl_display_init(ds, full_screen, no_frame);
3571 #elif defined(CONFIG_COCOA)
3573 cocoa_display_init(ds, full_screen);
3580 /* must be after terminal init, SDL library changes signal handlers */
3581 os_setup_signal_handling();
3584 /* init remote displays */
3586 vnc_display_init(ds);
3587 if (vnc_display_open(ds, vnc_display) < 0) {
3588 fprintf(stderr, "Failed to start VNC server on `%s'\n",
3593 if (show_vnc_port) {
3594 printf("VNC server running on `%s'\n", vnc_display_local_addr(ds));
3599 if (using_spice && !qxl_enabled) {
3600 qemu_spice_display_init(ds);
3606 dcl = ds->listeners;
3607 while (dcl != NULL) {
3608 if (dcl->dpy_refresh != NULL) {
3609 ds->gui_timer = qemu_new_timer_ms(rt_clock, gui_update, ds);
3610 qemu_mod_timer(ds->gui_timer, qemu_get_clock_ms(rt_clock));
3615 text_consoles_set_display(ds);
3617 if (foreach_device_config(DEV_GDB, gdbserver_start) < 0) {
3621 qdev_machine_creation_done();
3623 if (rom_load_all() != 0) {
3624 fprintf(stderr, "rom loading failed\n");
3628 /* TODO: once all bus devices are qdevified, this should be done
3629 * when bus is created by qdev.c */
3630 qemu_register_reset(qbus_reset_all_fn, sysbus_get_default());
3631 qemu_run_machine_init_done_notifiers();
3633 qemu_system_reset(VMRESET_SILENT);
3635 if (load_vmstate(loadvm) < 0) {
3642 int ret = qemu_start_incoming_migration(incoming, &errp);
3644 if (error_is_set(&errp)) {
3645 fprintf(stderr, "Migrate: %s\n", error_get_pretty(errp));
3648 fprintf(stderr, "Migration failed. Exit code %s(%d), exiting.\n",
3652 } else if (autostart) {