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__)
55 #include <sys/sysctl.h>
64 #include <linux/ppdev.h>
65 #include <linux/parport.h>
69 #include <sys/ethernet.h>
70 #include <sys/sockio.h>
71 #include <netinet/arp.h>
72 #include <netinet/in_systm.h>
73 #include <netinet/ip.h>
74 #include <netinet/ip_icmp.h> // must come after ip.h
75 #include <netinet/udp.h>
76 #include <netinet/tcp.h>
84 #if defined(__OpenBSD__)
88 #if defined(CONFIG_VDE)
89 #include <libvdeplug.h>
97 #if defined(__APPLE__) || defined(main)
99 int qemu_main(int argc, char **argv, char **envp);
100 int main(int argc, char **argv)
102 return qemu_main(argc, argv, NULL);
105 #define main qemu_main
107 #endif /* CONFIG_SDL */
111 #define main qemu_main
112 #endif /* CONFIG_COCOA */
117 #include "hw/boards.h"
119 #include "hw/pcmcia.h"
124 #include "hw/watchdog.h"
125 #include "hw/smbios.h"
128 #include "hw/loader.h"
131 #include "net/slirp.h"
136 #include "qemu-timer.h"
137 #include "qemu-char.h"
138 #include "cache-utils.h"
140 #include "blockdev.h"
141 #include "block-migration.h"
143 #include "audio/audio.h"
144 #include "migration.h"
146 #include "qemu-option.h"
147 #include "qemu-config.h"
148 #include "qemu-objects.h"
149 #include "qemu-options.h"
150 #include "qmp-commands.h"
152 #include "fsdev/qemu-fsdev.h"
157 #include "qemu_socket.h"
159 #include "slirp/libslirp.h"
162 #include "trace/control.h"
163 #include "qemu-queue.h"
165 #include "arch_init.h"
167 #include "ui/qemu-spice.h"
170 //#define DEBUG_SLIRP
172 #define DEFAULT_RAM_SIZE 128
174 #define MAX_VIRTIO_CONSOLES 1
176 static const char *data_dir;
177 const char *bios_name = NULL;
178 enum vga_retrace_method vga_retrace_method = VGA_RETRACE_DUMB;
179 DisplayType display_type = DT_DEFAULT;
180 int display_remote = 0;
181 const char* keyboard_layout = NULL;
183 const char *mem_path = NULL;
185 int mem_prealloc = 0; /* force preallocation of physical target memory */
188 NICInfo nd_table[MAX_NICS];
190 static int rtc_utc = 1;
191 static int rtc_date_offset = -1; /* -1 means no change */
192 QEMUClock *rtc_clock;
193 int vga_interface_type = VGA_NONE;
194 static int full_screen = 0;
196 static int no_frame = 0;
199 CharDriverState *serial_hds[MAX_SERIAL_PORTS];
200 CharDriverState *parallel_hds[MAX_PARALLEL_PORTS];
201 CharDriverState *virtcon_hds[MAX_VIRTIO_CONSOLES];
202 int win2k_install_hack = 0;
211 const char *vnc_display;
213 int acpi_enabled = 1;
219 int graphic_rotate = 0;
220 uint8_t irq0override = 1;
221 const char *watchdog;
222 QEMUOptionRom option_rom[MAX_OPTION_ROMS];
224 int semihosting_enabled = 0;
226 const char *qemu_name;
229 unsigned int nb_prom_envs = 0;
230 const char *prom_envs[MAX_PROM_ENVS];
232 uint8_t *boot_splash_filedata;
233 int boot_splash_filedata_size;
234 uint8_t qemu_extra_params_fw[2];
236 typedef struct FWBootEntry FWBootEntry;
239 QTAILQ_ENTRY(FWBootEntry) link;
245 QTAILQ_HEAD(, FWBootEntry) fw_boot_order = QTAILQ_HEAD_INITIALIZER(fw_boot_order);
248 uint64_t node_mem[MAX_NODES];
249 uint64_t node_cpumask[MAX_NODES];
251 static QEMUTimer *nographic_timer;
253 uint8_t qemu_uuid[16];
255 static QEMUBootSetHandler *boot_set_handler;
256 static void *boot_set_opaque;
258 static NotifierList exit_notifiers =
259 NOTIFIER_LIST_INITIALIZER(exit_notifiers);
261 static NotifierList machine_init_done_notifiers =
262 NOTIFIER_LIST_INITIALIZER(machine_init_done_notifiers);
264 static int tcg_allowed = 1;
268 enum xen_mode xen_mode = XEN_EMULATE;
269 static int tcg_tb_size;
271 static int default_serial = 1;
272 static int default_parallel = 1;
273 static int default_virtcon = 1;
274 static int default_monitor = 1;
275 static int default_vga = 1;
276 static int default_floppy = 1;
277 static int default_cdrom = 1;
278 static int default_sdcard = 1;
284 { .driver = "isa-serial", .flag = &default_serial },
285 { .driver = "isa-parallel", .flag = &default_parallel },
286 { .driver = "isa-fdc", .flag = &default_floppy },
287 { .driver = "ide-cd", .flag = &default_cdrom },
288 { .driver = "ide-hd", .flag = &default_cdrom },
289 { .driver = "ide-drive", .flag = &default_cdrom },
290 { .driver = "scsi-cd", .flag = &default_cdrom },
291 { .driver = "virtio-serial-pci", .flag = &default_virtcon },
292 { .driver = "virtio-serial-s390", .flag = &default_virtcon },
293 { .driver = "virtio-serial", .flag = &default_virtcon },
294 { .driver = "VGA", .flag = &default_vga },
295 { .driver = "cirrus-vga", .flag = &default_vga },
296 { .driver = "vmware-svga", .flag = &default_vga },
297 { .driver = "isa-vga", .flag = &default_vga },
298 { .driver = "qxl-vga", .flag = &default_vga },
301 static void res_free(void)
303 if (boot_splash_filedata != NULL) {
304 g_free(boot_splash_filedata);
305 boot_splash_filedata = NULL;
309 static int default_driver_check(QemuOpts *opts, void *opaque)
311 const char *driver = qemu_opt_get(opts, "driver");
316 for (i = 0; i < ARRAY_SIZE(default_list); i++) {
317 if (strcmp(default_list[i].driver, driver) != 0)
319 *(default_list[i].flag) = 0;
324 /***********************************************************/
327 static RunState current_run_state = RUN_STATE_PRELAUNCH;
332 } RunStateTransition;
334 static const RunStateTransition runstate_transitions_def[] = {
336 { RUN_STATE_DEBUG, RUN_STATE_RUNNING },
338 { RUN_STATE_INMIGRATE, RUN_STATE_RUNNING },
339 { RUN_STATE_INMIGRATE, RUN_STATE_PRELAUNCH },
341 { RUN_STATE_INTERNAL_ERROR, RUN_STATE_PAUSED },
343 { RUN_STATE_IO_ERROR, RUN_STATE_RUNNING },
345 { RUN_STATE_PAUSED, RUN_STATE_RUNNING },
347 { RUN_STATE_POSTMIGRATE, RUN_STATE_RUNNING },
349 { RUN_STATE_PRELAUNCH, RUN_STATE_RUNNING },
350 { RUN_STATE_PRELAUNCH, RUN_STATE_INMIGRATE },
351 { RUN_STATE_PRELAUNCH, RUN_STATE_POSTMIGRATE },
353 { RUN_STATE_FINISH_MIGRATE, RUN_STATE_RUNNING },
354 { RUN_STATE_FINISH_MIGRATE, RUN_STATE_POSTMIGRATE },
356 { RUN_STATE_RESTORE_VM, RUN_STATE_RUNNING },
358 { RUN_STATE_RUNNING, RUN_STATE_DEBUG },
359 { RUN_STATE_RUNNING, RUN_STATE_INTERNAL_ERROR },
360 { RUN_STATE_RUNNING, RUN_STATE_IO_ERROR },
361 { RUN_STATE_RUNNING, RUN_STATE_PAUSED },
362 { RUN_STATE_RUNNING, RUN_STATE_FINISH_MIGRATE },
363 { RUN_STATE_RUNNING, RUN_STATE_RESTORE_VM },
364 { RUN_STATE_RUNNING, RUN_STATE_SAVE_VM },
365 { RUN_STATE_RUNNING, RUN_STATE_SHUTDOWN },
366 { RUN_STATE_RUNNING, RUN_STATE_WATCHDOG },
368 { RUN_STATE_SAVE_VM, RUN_STATE_RUNNING },
370 { RUN_STATE_SHUTDOWN, RUN_STATE_PAUSED },
372 { RUN_STATE_WATCHDOG, RUN_STATE_RUNNING },
374 { RUN_STATE_MAX, RUN_STATE_MAX },
377 static bool runstate_valid_transitions[RUN_STATE_MAX][RUN_STATE_MAX];
379 bool runstate_check(RunState state)
381 return current_run_state == state;
384 void runstate_init(void)
386 const RunStateTransition *p;
388 memset(&runstate_valid_transitions, 0, sizeof(runstate_valid_transitions));
390 for (p = &runstate_transitions_def[0]; p->from != RUN_STATE_MAX; p++) {
391 runstate_valid_transitions[p->from][p->to] = true;
395 /* This function will abort() on invalid state transitions */
396 void runstate_set(RunState new_state)
398 if (new_state >= RUN_STATE_MAX ||
399 !runstate_valid_transitions[current_run_state][new_state]) {
400 fprintf(stderr, "invalid runstate transition\n");
404 current_run_state = new_state;
407 int runstate_is_running(void)
409 return runstate_check(RUN_STATE_RUNNING);
412 StatusInfo *qmp_query_status(Error **errp)
414 StatusInfo *info = g_malloc0(sizeof(*info));
416 info->running = runstate_is_running();
417 info->singlestep = singlestep;
418 info->status = current_run_state;
423 /***********************************************************/
424 /* real time host monotonic timer */
426 /***********************************************************/
427 /* host time/date access */
428 void qemu_get_timedate(struct tm *tm, int offset)
435 if (rtc_date_offset == -1) {
439 ret = localtime(&ti);
441 ti -= rtc_date_offset;
445 memcpy(tm, ret, sizeof(struct tm));
448 int qemu_timedate_diff(struct tm *tm)
452 if (rtc_date_offset == -1)
454 seconds = mktimegm(tm);
456 seconds = mktime(tm);
458 seconds = mktimegm(tm) + rtc_date_offset;
460 return seconds - time(NULL);
463 void rtc_change_mon_event(struct tm *tm)
467 data = qobject_from_jsonf("{ 'offset': %d }", qemu_timedate_diff(tm));
468 monitor_protocol_event(QEVENT_RTC_CHANGE, data);
469 qobject_decref(data);
472 static void configure_rtc_date_offset(const char *startdate, int legacy)
474 time_t rtc_start_date;
477 if (!strcmp(startdate, "now") && legacy) {
478 rtc_date_offset = -1;
480 if (sscanf(startdate, "%d-%d-%dT%d:%d:%d",
488 } else if (sscanf(startdate, "%d-%d-%d",
500 rtc_start_date = mktimegm(&tm);
501 if (rtc_start_date == -1) {
503 fprintf(stderr, "Invalid date format. Valid formats are:\n"
504 "'2006-06-17T16:01:21' or '2006-06-17'\n");
507 rtc_date_offset = time(NULL) - rtc_start_date;
511 static void configure_rtc(QemuOpts *opts)
515 value = qemu_opt_get(opts, "base");
517 if (!strcmp(value, "utc")) {
519 } else if (!strcmp(value, "localtime")) {
522 configure_rtc_date_offset(value, 0);
525 value = qemu_opt_get(opts, "clock");
527 if (!strcmp(value, "host")) {
528 rtc_clock = host_clock;
529 } else if (!strcmp(value, "vm")) {
530 rtc_clock = vm_clock;
532 fprintf(stderr, "qemu: invalid option value '%s'\n", value);
536 value = qemu_opt_get(opts, "driftfix");
538 if (!strcmp(value, "slew")) {
540 } else if (!strcmp(value, "none")) {
543 fprintf(stderr, "qemu: invalid option value '%s'\n", value);
549 /***********************************************************/
550 /* Bluetooth support */
553 static struct HCIInfo *hci_table[MAX_NICS];
555 static struct bt_vlan_s {
556 struct bt_scatternet_s net;
558 struct bt_vlan_s *next;
561 /* find or alloc a new bluetooth "VLAN" */
562 static struct bt_scatternet_s *qemu_find_bt_vlan(int id)
564 struct bt_vlan_s **pvlan, *vlan;
565 for (vlan = first_bt_vlan; vlan != NULL; vlan = vlan->next) {
569 vlan = g_malloc0(sizeof(struct bt_vlan_s));
571 pvlan = &first_bt_vlan;
572 while (*pvlan != NULL)
573 pvlan = &(*pvlan)->next;
578 static void null_hci_send(struct HCIInfo *hci, const uint8_t *data, int len)
582 static int null_hci_addr_set(struct HCIInfo *hci, const uint8_t *bd_addr)
587 static struct HCIInfo null_hci = {
588 .cmd_send = null_hci_send,
589 .sco_send = null_hci_send,
590 .acl_send = null_hci_send,
591 .bdaddr_set = null_hci_addr_set,
594 struct HCIInfo *qemu_next_hci(void)
596 if (cur_hci == nb_hcis)
599 return hci_table[cur_hci++];
602 static struct HCIInfo *hci_init(const char *str)
605 struct bt_scatternet_s *vlan = 0;
607 if (!strcmp(str, "null"))
610 else if (!strncmp(str, "host", 4) && (str[4] == '\0' || str[4] == ':'))
612 return bt_host_hci(str[4] ? str + 5 : "hci0");
613 else if (!strncmp(str, "hci", 3)) {
616 if (!strncmp(str + 3, ",vlan=", 6)) {
617 vlan = qemu_find_bt_vlan(strtol(str + 9, &endp, 0));
622 vlan = qemu_find_bt_vlan(0);
624 return bt_new_hci(vlan);
627 fprintf(stderr, "qemu: Unknown bluetooth HCI `%s'.\n", str);
632 static int bt_hci_parse(const char *str)
637 if (nb_hcis >= MAX_NICS) {
638 fprintf(stderr, "qemu: Too many bluetooth HCIs (max %i).\n", MAX_NICS);
651 bdaddr.b[5] = 0x56 + nb_hcis;
652 hci->bdaddr_set(hci, bdaddr.b);
654 hci_table[nb_hcis++] = hci;
659 static void bt_vhci_add(int vlan_id)
661 struct bt_scatternet_s *vlan = qemu_find_bt_vlan(vlan_id);
664 fprintf(stderr, "qemu: warning: adding a VHCI to "
665 "an empty scatternet %i\n", vlan_id);
667 bt_vhci_init(bt_new_hci(vlan));
670 static struct bt_device_s *bt_device_add(const char *opt)
672 struct bt_scatternet_s *vlan;
674 char *endp = strstr(opt, ",vlan=");
675 int len = (endp ? endp - opt : strlen(opt)) + 1;
678 pstrcpy(devname, MIN(sizeof(devname), len), opt);
681 vlan_id = strtol(endp + 6, &endp, 0);
683 fprintf(stderr, "qemu: unrecognised bluetooth vlan Id\n");
688 vlan = qemu_find_bt_vlan(vlan_id);
691 fprintf(stderr, "qemu: warning: adding a slave device to "
692 "an empty scatternet %i\n", vlan_id);
694 if (!strcmp(devname, "keyboard"))
695 return bt_keyboard_init(vlan);
697 fprintf(stderr, "qemu: unsupported bluetooth device `%s'\n", devname);
701 static int bt_parse(const char *opt)
703 const char *endp, *p;
706 if (strstart(opt, "hci", &endp)) {
707 if (!*endp || *endp == ',') {
709 if (!strstart(endp, ",vlan=", 0))
712 return bt_hci_parse(opt);
714 } else if (strstart(opt, "vhci", &endp)) {
715 if (!*endp || *endp == ',') {
717 if (strstart(endp, ",vlan=", &p)) {
718 vlan = strtol(p, (char **) &endp, 0);
720 fprintf(stderr, "qemu: bad scatternet '%s'\n", p);
724 fprintf(stderr, "qemu: bad parameter '%s'\n", endp + 1);
733 } else if (strstart(opt, "device:", &endp))
734 return !bt_device_add(endp);
736 fprintf(stderr, "qemu: bad bluetooth parameter '%s'\n", opt);
740 /***********************************************************/
741 /* QEMU Block devices */
743 #define HD_OPTS "media=disk"
744 #define CDROM_OPTS "media=cdrom"
746 #define PFLASH_OPTS ""
750 static int drive_init_func(QemuOpts *opts, void *opaque)
752 int *use_scsi = opaque;
754 return drive_init(opts, *use_scsi) == NULL;
757 static int drive_enable_snapshot(QemuOpts *opts, void *opaque)
759 if (NULL == qemu_opt_get(opts, "snapshot")) {
760 qemu_opt_set(opts, "snapshot", "on");
765 static void default_drive(int enable, int snapshot, int use_scsi,
766 BlockInterfaceType type, int index,
771 if (type == IF_DEFAULT) {
772 type = use_scsi ? IF_SCSI : IF_IDE;
775 if (!enable || drive_get_by_index(type, index)) {
779 opts = drive_add(type, index, NULL, optstr);
781 drive_enable_snapshot(opts, NULL);
783 if (!drive_init(opts, use_scsi)) {
788 void qemu_register_boot_set(QEMUBootSetHandler *func, void *opaque)
790 boot_set_handler = func;
791 boot_set_opaque = opaque;
794 int qemu_boot_set(const char *boot_devices)
796 if (!boot_set_handler) {
799 return boot_set_handler(boot_set_opaque, boot_devices);
802 static void validate_bootdevices(char *devices)
804 /* We just do some generic consistency checks */
808 for (p = devices; *p != '\0'; p++) {
809 /* Allowed boot devices are:
810 * a-b: floppy disk drives
811 * c-f: IDE disk drives
812 * g-m: machine implementation dependant drives
813 * n-p: network devices
814 * It's up to each machine implementation to check if the given boot
815 * devices match the actual hardware implementation and firmware
818 if (*p < 'a' || *p > 'p') {
819 fprintf(stderr, "Invalid boot device '%c'\n", *p);
822 if (bitmap & (1 << (*p - 'a'))) {
823 fprintf(stderr, "Boot device '%c' was given twice\n", *p);
826 bitmap |= 1 << (*p - 'a');
830 static void restore_boot_devices(void *opaque)
832 char *standard_boot_devices = opaque;
833 static int first = 1;
835 /* Restore boot order and remove ourselves after the first boot */
841 qemu_boot_set(standard_boot_devices);
843 qemu_unregister_reset(restore_boot_devices, standard_boot_devices);
844 g_free(standard_boot_devices);
847 void add_boot_device_path(int32_t bootindex, DeviceState *dev,
850 FWBootEntry *node, *i;
856 assert(dev != NULL || suffix != NULL);
858 node = g_malloc0(sizeof(FWBootEntry));
859 node->bootindex = bootindex;
860 node->suffix = suffix ? g_strdup(suffix) : NULL;
863 QTAILQ_FOREACH(i, &fw_boot_order, link) {
864 if (i->bootindex == bootindex) {
865 fprintf(stderr, "Two devices with same boot index %d\n", bootindex);
867 } else if (i->bootindex < bootindex) {
870 QTAILQ_INSERT_BEFORE(i, node, link);
873 QTAILQ_INSERT_TAIL(&fw_boot_order, node, link);
877 * This function returns null terminated string that consist of new line
878 * separated device paths.
880 * memory pointed by "size" is assigned total length of the array in bytes
883 char *get_boot_devices_list(uint32_t *size)
889 QTAILQ_FOREACH(i, &fw_boot_order, link) {
890 char *devpath = NULL, *bootpath;
894 devpath = qdev_get_fw_dev_path(i->dev);
898 if (i->suffix && devpath) {
899 size_t bootpathlen = strlen(devpath) + strlen(i->suffix) + 1;
901 bootpath = g_malloc(bootpathlen);
902 snprintf(bootpath, bootpathlen, "%s%s", devpath, i->suffix);
904 } else if (devpath) {
907 bootpath = g_strdup(i->suffix);
912 list[total-1] = '\n';
914 len = strlen(bootpath) + 1;
915 list = g_realloc(list, total + len);
916 memcpy(&list[total], bootpath, len);
926 static void numa_add(const char *optarg)
930 unsigned long long value, endvalue;
933 optarg = get_opt_name(option, 128, optarg, ',') + 1;
934 if (!strcmp(option, "node")) {
935 if (get_param_value(option, 128, "nodeid", optarg) == 0) {
936 nodenr = nb_numa_nodes;
938 nodenr = strtoull(option, NULL, 10);
941 if (get_param_value(option, 128, "mem", optarg) == 0) {
942 node_mem[nodenr] = 0;
945 sval = strtosz(option, NULL);
947 fprintf(stderr, "qemu: invalid numa mem size: %s\n", optarg);
950 node_mem[nodenr] = sval;
952 if (get_param_value(option, 128, "cpus", optarg) == 0) {
953 node_cpumask[nodenr] = 0;
955 value = strtoull(option, &endptr, 10);
958 fprintf(stderr, "only 64 CPUs in NUMA mode supported.\n");
960 if (*endptr == '-') {
961 endvalue = strtoull(endptr+1, &endptr, 10);
962 if (endvalue >= 63) {
965 "only 63 CPUs in NUMA mode supported.\n");
967 value = (2ULL << endvalue) - (1ULL << value);
969 value = 1ULL << value;
972 node_cpumask[nodenr] = value;
979 static void smp_parse(const char *optarg)
981 int smp, sockets = 0, threads = 0, cores = 0;
985 smp = strtoul(optarg, &endptr, 10);
986 if (endptr != optarg) {
987 if (*endptr == ',') {
991 if (get_param_value(option, 128, "sockets", endptr) != 0)
992 sockets = strtoull(option, NULL, 10);
993 if (get_param_value(option, 128, "cores", endptr) != 0)
994 cores = strtoull(option, NULL, 10);
995 if (get_param_value(option, 128, "threads", endptr) != 0)
996 threads = strtoull(option, NULL, 10);
997 if (get_param_value(option, 128, "maxcpus", endptr) != 0)
998 max_cpus = strtoull(option, NULL, 10);
1000 /* compute missing values, prefer sockets over cores over threads */
1001 if (smp == 0 || sockets == 0) {
1002 sockets = sockets > 0 ? sockets : 1;
1003 cores = cores > 0 ? cores : 1;
1004 threads = threads > 0 ? threads : 1;
1006 smp = cores * threads * sockets;
1010 threads = threads > 0 ? threads : 1;
1011 cores = smp / (sockets * threads);
1013 threads = smp / (cores * sockets);
1017 smp_cores = cores > 0 ? cores : 1;
1018 smp_threads = threads > 0 ? threads : 1;
1020 max_cpus = smp_cpus;
1023 /***********************************************************/
1026 static int usb_device_add(const char *devname)
1029 USBDevice *dev = NULL;
1034 /* drivers with .usbdevice_name entry in USBDeviceInfo */
1035 dev = usbdevice_create(devname);
1039 /* the other ones */
1040 #ifndef CONFIG_LINUX
1041 /* only the linux version is qdev-ified, usb-bsd still needs this */
1042 if (strstart(devname, "host:", &p)) {
1043 dev = usb_host_device_open(p);
1046 if (!strcmp(devname, "bt") || strstart(devname, "bt:", &p)) {
1047 dev = usb_bt_init(devname[2] ? hci_init(p) :
1048 bt_new_hci(qemu_find_bt_vlan(0)));
1059 static int usb_device_del(const char *devname)
1064 if (strstart(devname, "host:", &p))
1065 return usb_host_device_close(p);
1070 p = strchr(devname, '.');
1073 bus_num = strtoul(devname, NULL, 0);
1074 addr = strtoul(p + 1, NULL, 0);
1076 return usb_device_delete_addr(bus_num, addr);
1079 static int usb_parse(const char *cmdline)
1082 r = usb_device_add(cmdline);
1084 fprintf(stderr, "qemu: could not add USB device '%s'\n", cmdline);
1089 void do_usb_add(Monitor *mon, const QDict *qdict)
1091 const char *devname = qdict_get_str(qdict, "devname");
1092 if (usb_device_add(devname) < 0) {
1093 error_report("could not add USB device '%s'", devname);
1097 void do_usb_del(Monitor *mon, const QDict *qdict)
1099 const char *devname = qdict_get_str(qdict, "devname");
1100 if (usb_device_del(devname) < 0) {
1101 error_report("could not delete USB device '%s'", devname);
1105 /***********************************************************/
1106 /* PCMCIA/Cardbus */
1108 static struct pcmcia_socket_entry_s {
1109 PCMCIASocket *socket;
1110 struct pcmcia_socket_entry_s *next;
1111 } *pcmcia_sockets = 0;
1113 void pcmcia_socket_register(PCMCIASocket *socket)
1115 struct pcmcia_socket_entry_s *entry;
1117 entry = g_malloc(sizeof(struct pcmcia_socket_entry_s));
1118 entry->socket = socket;
1119 entry->next = pcmcia_sockets;
1120 pcmcia_sockets = entry;
1123 void pcmcia_socket_unregister(PCMCIASocket *socket)
1125 struct pcmcia_socket_entry_s *entry, **ptr;
1127 ptr = &pcmcia_sockets;
1128 for (entry = *ptr; entry; ptr = &entry->next, entry = *ptr)
1129 if (entry->socket == socket) {
1135 void pcmcia_info(Monitor *mon)
1137 struct pcmcia_socket_entry_s *iter;
1139 if (!pcmcia_sockets)
1140 monitor_printf(mon, "No PCMCIA sockets\n");
1142 for (iter = pcmcia_sockets; iter; iter = iter->next)
1143 monitor_printf(mon, "%s: %s\n", iter->socket->slot_string,
1144 iter->socket->attached ? iter->socket->card_string :
1148 /***********************************************************/
1149 /* machine registration */
1151 static QEMUMachine *first_machine = NULL;
1152 QEMUMachine *current_machine = NULL;
1154 int qemu_register_machine(QEMUMachine *m)
1157 pm = &first_machine;
1165 static QEMUMachine *find_machine(const char *name)
1169 for(m = first_machine; m != NULL; m = m->next) {
1170 if (!strcmp(m->name, name))
1172 if (m->alias && !strcmp(m->alias, name))
1178 static QEMUMachine *find_default_machine(void)
1182 for(m = first_machine; m != NULL; m = m->next) {
1183 if (m->is_default) {
1190 /***********************************************************/
1191 /* main execution loop */
1193 static void gui_update(void *opaque)
1195 uint64_t interval = GUI_REFRESH_INTERVAL;
1196 DisplayState *ds = opaque;
1197 DisplayChangeListener *dcl = ds->listeners;
1199 qemu_flush_coalesced_mmio_buffer();
1202 while (dcl != NULL) {
1203 if (dcl->gui_timer_interval &&
1204 dcl->gui_timer_interval < interval)
1205 interval = dcl->gui_timer_interval;
1208 qemu_mod_timer(ds->gui_timer, interval + qemu_get_clock_ms(rt_clock));
1211 static void nographic_update(void *opaque)
1213 uint64_t interval = GUI_REFRESH_INTERVAL;
1215 qemu_flush_coalesced_mmio_buffer();
1216 qemu_mod_timer(nographic_timer, interval + qemu_get_clock_ms(rt_clock));
1219 struct vm_change_state_entry {
1220 VMChangeStateHandler *cb;
1222 QLIST_ENTRY (vm_change_state_entry) entries;
1225 static QLIST_HEAD(vm_change_state_head, vm_change_state_entry) vm_change_state_head;
1227 VMChangeStateEntry *qemu_add_vm_change_state_handler(VMChangeStateHandler *cb,
1230 VMChangeStateEntry *e;
1232 e = g_malloc0(sizeof (*e));
1236 QLIST_INSERT_HEAD(&vm_change_state_head, e, entries);
1240 void qemu_del_vm_change_state_handler(VMChangeStateEntry *e)
1242 QLIST_REMOVE (e, entries);
1246 void vm_state_notify(int running, RunState state)
1248 VMChangeStateEntry *e;
1250 trace_vm_state_notify(running, state);
1252 for (e = vm_change_state_head.lh_first; e; e = e->entries.le_next) {
1253 e->cb(e->opaque, running, state);
1259 if (!runstate_is_running()) {
1261 runstate_set(RUN_STATE_RUNNING);
1262 vm_state_notify(1, RUN_STATE_RUNNING);
1264 monitor_protocol_event(QEVENT_RESUME, NULL);
1268 /* reset/shutdown handler */
1270 typedef struct QEMUResetEntry {
1271 QTAILQ_ENTRY(QEMUResetEntry) entry;
1272 QEMUResetHandler *func;
1276 static QTAILQ_HEAD(reset_handlers, QEMUResetEntry) reset_handlers =
1277 QTAILQ_HEAD_INITIALIZER(reset_handlers);
1278 static int reset_requested;
1279 static int shutdown_requested, shutdown_signal = -1;
1280 static pid_t shutdown_pid;
1281 static int powerdown_requested;
1282 static int debug_requested;
1283 static RunState vmstop_requested = RUN_STATE_MAX;
1285 int qemu_shutdown_requested_get(void)
1287 return shutdown_requested;
1290 int qemu_reset_requested_get(void)
1292 return reset_requested;
1295 int qemu_shutdown_requested(void)
1297 int r = shutdown_requested;
1298 shutdown_requested = 0;
1302 void qemu_kill_report(void)
1304 if (shutdown_signal != -1) {
1305 fprintf(stderr, "qemu: terminating on signal %d", shutdown_signal);
1306 if (shutdown_pid == 0) {
1307 /* This happens for eg ^C at the terminal, so it's worth
1308 * avoiding printing an odd message in that case.
1310 fputc('\n', stderr);
1312 fprintf(stderr, " from pid " FMT_pid "\n", shutdown_pid);
1314 shutdown_signal = -1;
1318 int qemu_reset_requested(void)
1320 int r = reset_requested;
1321 reset_requested = 0;
1325 int qemu_powerdown_requested(void)
1327 int r = powerdown_requested;
1328 powerdown_requested = 0;
1332 static int qemu_debug_requested(void)
1334 int r = debug_requested;
1335 debug_requested = 0;
1339 /* We use RUN_STATE_MAX but any invalid value will do */
1340 static bool qemu_vmstop_requested(RunState *r)
1342 if (vmstop_requested < RUN_STATE_MAX) {
1343 *r = vmstop_requested;
1344 vmstop_requested = RUN_STATE_MAX;
1351 void qemu_register_reset(QEMUResetHandler *func, void *opaque)
1353 QEMUResetEntry *re = g_malloc0(sizeof(QEMUResetEntry));
1356 re->opaque = opaque;
1357 QTAILQ_INSERT_TAIL(&reset_handlers, re, entry);
1360 void qemu_unregister_reset(QEMUResetHandler *func, void *opaque)
1364 QTAILQ_FOREACH(re, &reset_handlers, entry) {
1365 if (re->func == func && re->opaque == opaque) {
1366 QTAILQ_REMOVE(&reset_handlers, re, entry);
1373 void qemu_system_reset(bool report)
1375 QEMUResetEntry *re, *nre;
1377 /* reset all devices */
1378 QTAILQ_FOREACH_SAFE(re, &reset_handlers, entry, nre) {
1379 re->func(re->opaque);
1382 monitor_protocol_event(QEVENT_RESET, NULL);
1384 cpu_synchronize_all_post_reset();
1387 void qemu_system_reset_request(void)
1390 shutdown_requested = 1;
1392 reset_requested = 1;
1395 qemu_notify_event();
1398 void qemu_system_killed(int signal, pid_t pid)
1400 shutdown_signal = signal;
1403 qemu_system_shutdown_request();
1406 void qemu_system_shutdown_request(void)
1408 shutdown_requested = 1;
1409 qemu_notify_event();
1412 void qemu_system_powerdown_request(void)
1414 powerdown_requested = 1;
1415 qemu_notify_event();
1418 void qemu_system_debug_request(void)
1420 debug_requested = 1;
1421 qemu_notify_event();
1424 void qemu_system_vmstop_request(RunState state)
1426 vmstop_requested = state;
1427 qemu_notify_event();
1430 static GPollFD poll_fds[1024 * 2]; /* this is probably overkill */
1431 static int n_poll_fds;
1432 static int max_priority;
1434 static void glib_select_fill(int *max_fd, fd_set *rfds, fd_set *wfds,
1435 fd_set *xfds, struct timeval *tv)
1437 GMainContext *context = g_main_context_default();
1439 int timeout = 0, cur_timeout;
1441 g_main_context_prepare(context, &max_priority);
1443 n_poll_fds = g_main_context_query(context, max_priority, &timeout,
1444 poll_fds, ARRAY_SIZE(poll_fds));
1445 g_assert(n_poll_fds <= ARRAY_SIZE(poll_fds));
1447 for (i = 0; i < n_poll_fds; i++) {
1448 GPollFD *p = &poll_fds[i];
1450 if ((p->events & G_IO_IN)) {
1451 FD_SET(p->fd, rfds);
1452 *max_fd = MAX(*max_fd, p->fd);
1454 if ((p->events & G_IO_OUT)) {
1455 FD_SET(p->fd, wfds);
1456 *max_fd = MAX(*max_fd, p->fd);
1458 if ((p->events & G_IO_ERR)) {
1459 FD_SET(p->fd, xfds);
1460 *max_fd = MAX(*max_fd, p->fd);
1464 cur_timeout = (tv->tv_sec * 1000) + ((tv->tv_usec + 500) / 1000);
1465 if (timeout >= 0 && timeout < cur_timeout) {
1466 tv->tv_sec = timeout / 1000;
1467 tv->tv_usec = (timeout % 1000) * 1000;
1471 static void glib_select_poll(fd_set *rfds, fd_set *wfds, fd_set *xfds,
1474 GMainContext *context = g_main_context_default();
1479 for (i = 0; i < n_poll_fds; i++) {
1480 GPollFD *p = &poll_fds[i];
1482 if ((p->events & G_IO_IN) && FD_ISSET(p->fd, rfds)) {
1483 p->revents |= G_IO_IN;
1485 if ((p->events & G_IO_OUT) && FD_ISSET(p->fd, wfds)) {
1486 p->revents |= G_IO_OUT;
1488 if ((p->events & G_IO_ERR) && FD_ISSET(p->fd, xfds)) {
1489 p->revents |= G_IO_ERR;
1494 if (g_main_context_check(context, max_priority, poll_fds, n_poll_fds)) {
1495 g_main_context_dispatch(context);
1499 int main_loop_wait(int nonblocking)
1501 fd_set rfds, wfds, xfds;
1509 timeout = qemu_calculate_timeout();
1510 qemu_bh_update_timeout(&timeout);
1513 os_host_main_loop_wait(&timeout);
1515 tv.tv_sec = timeout / 1000;
1516 tv.tv_usec = (timeout % 1000) * 1000;
1518 /* poll any events */
1519 /* XXX: separate device handlers from system ones */
1525 qemu_iohandler_fill(&nfds, &rfds, &wfds, &xfds);
1526 slirp_select_fill(&nfds, &rfds, &wfds, &xfds);
1527 glib_select_fill(&nfds, &rfds, &wfds, &xfds, &tv);
1530 qemu_mutex_unlock_iothread();
1533 ret = select(nfds + 1, &rfds, &wfds, &xfds, &tv);
1536 qemu_mutex_lock_iothread();
1539 qemu_iohandler_poll(&rfds, &wfds, &xfds, ret);
1540 slirp_select_poll(&rfds, &wfds, &xfds, (ret < 0));
1541 glib_select_poll(&rfds, &wfds, &xfds, (ret < 0));
1543 qemu_run_all_timers();
1545 /* Check bottom-halves last in case any of the earlier events triggered
1552 qemu_irq qemu_system_powerdown;
1554 static void main_loop(void)
1557 int last_io __attribute__ ((unused)) = 0;
1558 #ifdef CONFIG_PROFILER
1563 qemu_main_loop_start();
1566 nonblocking = !kvm_enabled() && last_io > 0;
1567 #ifdef CONFIG_PROFILER
1568 ti = profile_getclock();
1570 last_io = main_loop_wait(nonblocking);
1571 #ifdef CONFIG_PROFILER
1572 dev_time += profile_getclock() - ti;
1575 if (qemu_debug_requested()) {
1576 vm_stop(RUN_STATE_DEBUG);
1578 if (qemu_shutdown_requested()) {
1580 monitor_protocol_event(QEVENT_SHUTDOWN, NULL);
1582 vm_stop(RUN_STATE_SHUTDOWN);
1586 if (qemu_reset_requested()) {
1588 cpu_synchronize_all_states();
1589 qemu_system_reset(VMRESET_REPORT);
1591 if (runstate_check(RUN_STATE_INTERNAL_ERROR) ||
1592 runstate_check(RUN_STATE_SHUTDOWN)) {
1593 runstate_set(RUN_STATE_PAUSED);
1596 if (qemu_powerdown_requested()) {
1597 monitor_protocol_event(QEVENT_POWERDOWN, NULL);
1598 qemu_irq_raise(qemu_system_powerdown);
1600 if (qemu_vmstop_requested(&r)) {
1608 static void version(void)
1610 printf("QEMU emulator version " QEMU_VERSION QEMU_PKGVERSION ", Copyright (c) 2003-2008 Fabrice Bellard\n");
1613 static void help(int exitcode)
1615 const char *options_help =
1616 #define DEF(option, opt_arg, opt_enum, opt_help, arch_mask) \
1618 #define DEFHEADING(text) stringify(text) "\n"
1619 #include "qemu-options.def"
1625 printf("usage: %s [options] [disk_image]\n"
1627 "'disk_image' is a raw hard disk image for IDE hard disk 0\n"
1630 "During emulation, the following keys are useful:\n"
1631 "ctrl-alt-f toggle full screen\n"
1632 "ctrl-alt-n switch to virtual console 'n'\n"
1633 "ctrl-alt toggle mouse and keyboard grab\n"
1635 "When using -nographic, press 'ctrl-a h' to get some help.\n",
1641 #define HAS_ARG 0x0001
1643 typedef struct QEMUOption {
1650 static const QEMUOption qemu_options[] = {
1651 { "h", 0, QEMU_OPTION_h, QEMU_ARCH_ALL },
1652 #define DEF(option, opt_arg, opt_enum, opt_help, arch_mask) \
1653 { option, opt_arg, opt_enum, arch_mask },
1654 #define DEFHEADING(text)
1655 #include "qemu-options.def"
1661 static void select_vgahw (const char *p)
1666 vga_interface_type = VGA_NONE;
1667 if (strstart(p, "std", &opts)) {
1668 vga_interface_type = VGA_STD;
1669 } else if (strstart(p, "cirrus", &opts)) {
1670 vga_interface_type = VGA_CIRRUS;
1671 } else if (strstart(p, "vmware", &opts)) {
1672 vga_interface_type = VGA_VMWARE;
1673 } else if (strstart(p, "xenfb", &opts)) {
1674 vga_interface_type = VGA_XENFB;
1675 } else if (strstart(p, "qxl", &opts)) {
1676 vga_interface_type = VGA_QXL;
1677 } else if (!strstart(p, "none", &opts)) {
1679 fprintf(stderr, "Unknown vga type: %s\n", p);
1683 const char *nextopt;
1685 if (strstart(opts, ",retrace=", &nextopt)) {
1687 if (strstart(opts, "dumb", &nextopt))
1688 vga_retrace_method = VGA_RETRACE_DUMB;
1689 else if (strstart(opts, "precise", &nextopt))
1690 vga_retrace_method = VGA_RETRACE_PRECISE;
1691 else goto invalid_vga;
1692 } else goto invalid_vga;
1697 static DisplayType select_display(const char *p)
1700 DisplayType display = DT_DEFAULT;
1702 if (strstart(p, "sdl", &opts)) {
1706 const char *nextopt;
1708 if (strstart(opts, ",frame=", &nextopt)) {
1710 if (strstart(opts, "on", &nextopt)) {
1712 } else if (strstart(opts, "off", &nextopt)) {
1715 goto invalid_sdl_args;
1717 } else if (strstart(opts, ",alt_grab=", &nextopt)) {
1719 if (strstart(opts, "on", &nextopt)) {
1721 } else if (strstart(opts, "off", &nextopt)) {
1724 goto invalid_sdl_args;
1726 } else if (strstart(opts, ",ctrl_grab=", &nextopt)) {
1728 if (strstart(opts, "on", &nextopt)) {
1730 } else if (strstart(opts, "off", &nextopt)) {
1733 goto invalid_sdl_args;
1735 } else if (strstart(opts, ",window_close=", &nextopt)) {
1737 if (strstart(opts, "on", &nextopt)) {
1739 } else if (strstart(opts, "off", &nextopt)) {
1742 goto invalid_sdl_args;
1746 fprintf(stderr, "Invalid SDL option string: %s\n", p);
1752 fprintf(stderr, "SDL support is disabled\n");
1755 } else if (strstart(p, "vnc", &opts)) {
1760 const char *nextopt;
1762 if (strstart(opts, "=", &nextopt)) {
1763 vnc_display = nextopt;
1767 fprintf(stderr, "VNC requires a display argument vnc=<display>\n");
1771 fprintf(stderr, "VNC support is disabled\n");
1774 } else if (strstart(p, "curses", &opts)) {
1775 #ifdef CONFIG_CURSES
1776 display = DT_CURSES;
1778 fprintf(stderr, "Curses support is disabled\n");
1781 } else if (strstart(p, "none", &opts)) {
1784 fprintf(stderr, "Unknown display type: %s\n", p);
1791 static int balloon_parse(const char *arg)
1795 if (strcmp(arg, "none") == 0) {
1799 if (!strncmp(arg, "virtio", 6)) {
1800 if (arg[6] == ',') {
1801 /* have params -> parse them */
1802 opts = qemu_opts_parse(qemu_find_opts("device"), arg+7, 0);
1806 /* create empty opts */
1807 opts = qemu_opts_create(qemu_find_opts("device"), NULL, 0);
1809 qemu_opt_set(opts, "driver", "virtio-balloon");
1816 char *qemu_find_file(int type, const char *name)
1822 /* If name contains path separators then try it as a straight path. */
1823 if ((strchr(name, '/') || strchr(name, '\\'))
1824 && access(name, R_OK) == 0) {
1825 return g_strdup(name);
1828 case QEMU_FILE_TYPE_BIOS:
1831 case QEMU_FILE_TYPE_KEYMAP:
1832 subdir = "keymaps/";
1837 len = strlen(data_dir) + strlen(name) + strlen(subdir) + 2;
1838 buf = g_malloc0(len);
1839 snprintf(buf, len, "%s/%s%s", data_dir, subdir, name);
1840 if (access(buf, R_OK)) {
1847 static int device_help_func(QemuOpts *opts, void *opaque)
1849 return qdev_device_help(opts);
1852 static int device_init_func(QemuOpts *opts, void *opaque)
1856 dev = qdev_device_add(opts);
1862 static int chardev_init_func(QemuOpts *opts, void *opaque)
1864 CharDriverState *chr;
1866 chr = qemu_chr_new_from_opts(opts, NULL);
1872 #ifdef CONFIG_VIRTFS
1873 static int fsdev_init_func(QemuOpts *opts, void *opaque)
1876 ret = qemu_fsdev_add(opts);
1882 static int mon_init_func(QemuOpts *opts, void *opaque)
1884 CharDriverState *chr;
1885 const char *chardev;
1889 mode = qemu_opt_get(opts, "mode");
1893 if (strcmp(mode, "readline") == 0) {
1894 flags = MONITOR_USE_READLINE;
1895 } else if (strcmp(mode, "control") == 0) {
1896 flags = MONITOR_USE_CONTROL;
1898 fprintf(stderr, "unknown monitor mode \"%s\"\n", mode);
1902 if (qemu_opt_get_bool(opts, "pretty", 0))
1903 flags |= MONITOR_USE_PRETTY;
1905 if (qemu_opt_get_bool(opts, "default", 0))
1906 flags |= MONITOR_IS_DEFAULT;
1908 chardev = qemu_opt_get(opts, "chardev");
1909 chr = qemu_chr_find(chardev);
1911 fprintf(stderr, "chardev \"%s\" not found\n", chardev);
1915 monitor_init(chr, flags);
1919 static void monitor_parse(const char *optarg, const char *mode)
1921 static int monitor_device_index = 0;
1927 if (strstart(optarg, "chardev:", &p)) {
1928 snprintf(label, sizeof(label), "%s", p);
1930 snprintf(label, sizeof(label), "compat_monitor%d",
1931 monitor_device_index);
1932 if (monitor_device_index == 0) {
1935 opts = qemu_chr_parse_compat(label, optarg);
1937 fprintf(stderr, "parse error: %s\n", optarg);
1942 opts = qemu_opts_create(qemu_find_opts("mon"), label, 1);
1944 fprintf(stderr, "duplicate chardev: %s\n", label);
1947 qemu_opt_set(opts, "mode", mode);
1948 qemu_opt_set(opts, "chardev", label);
1950 qemu_opt_set(opts, "default", "on");
1951 monitor_device_index++;
1954 struct device_config {
1956 DEV_USB, /* -usbdevice */
1958 DEV_SERIAL, /* -serial */
1959 DEV_PARALLEL, /* -parallel */
1960 DEV_VIRTCON, /* -virtioconsole */
1961 DEV_DEBUGCON, /* -debugcon */
1963 const char *cmdline;
1964 QTAILQ_ENTRY(device_config) next;
1966 QTAILQ_HEAD(, device_config) device_configs = QTAILQ_HEAD_INITIALIZER(device_configs);
1968 static void add_device_config(int type, const char *cmdline)
1970 struct device_config *conf;
1972 conf = g_malloc0(sizeof(*conf));
1974 conf->cmdline = cmdline;
1975 QTAILQ_INSERT_TAIL(&device_configs, conf, next);
1978 static int foreach_device_config(int type, int (*func)(const char *cmdline))
1980 struct device_config *conf;
1983 QTAILQ_FOREACH(conf, &device_configs, next) {
1984 if (conf->type != type)
1986 rc = func(conf->cmdline);
1993 static int serial_parse(const char *devname)
1995 static int index = 0;
1998 if (strcmp(devname, "none") == 0)
2000 if (index == MAX_SERIAL_PORTS) {
2001 fprintf(stderr, "qemu: too many serial ports\n");
2004 snprintf(label, sizeof(label), "serial%d", index);
2005 serial_hds[index] = qemu_chr_new(label, devname, NULL);
2006 if (!serial_hds[index]) {
2007 fprintf(stderr, "qemu: could not open serial device '%s': %s\n",
2008 devname, strerror(errno));
2015 static int parallel_parse(const char *devname)
2017 static int index = 0;
2020 if (strcmp(devname, "none") == 0)
2022 if (index == MAX_PARALLEL_PORTS) {
2023 fprintf(stderr, "qemu: too many parallel ports\n");
2026 snprintf(label, sizeof(label), "parallel%d", index);
2027 parallel_hds[index] = qemu_chr_new(label, devname, NULL);
2028 if (!parallel_hds[index]) {
2029 fprintf(stderr, "qemu: could not open parallel device '%s': %s\n",
2030 devname, strerror(errno));
2037 static int virtcon_parse(const char *devname)
2039 QemuOptsList *device = qemu_find_opts("device");
2040 static int index = 0;
2042 QemuOpts *bus_opts, *dev_opts;
2044 if (strcmp(devname, "none") == 0)
2046 if (index == MAX_VIRTIO_CONSOLES) {
2047 fprintf(stderr, "qemu: too many virtio consoles\n");
2051 bus_opts = qemu_opts_create(device, NULL, 0);
2052 qemu_opt_set(bus_opts, "driver", "virtio-serial");
2054 dev_opts = qemu_opts_create(device, NULL, 0);
2055 qemu_opt_set(dev_opts, "driver", "virtconsole");
2057 snprintf(label, sizeof(label), "virtcon%d", index);
2058 virtcon_hds[index] = qemu_chr_new(label, devname, NULL);
2059 if (!virtcon_hds[index]) {
2060 fprintf(stderr, "qemu: could not open virtio console '%s': %s\n",
2061 devname, strerror(errno));
2064 qemu_opt_set(dev_opts, "chardev", label);
2070 static int debugcon_parse(const char *devname)
2074 if (!qemu_chr_new("debugcon", devname, NULL)) {
2077 opts = qemu_opts_create(qemu_find_opts("device"), "debugcon", 1);
2079 fprintf(stderr, "qemu: already have a debugcon device\n");
2082 qemu_opt_set(opts, "driver", "isa-debugcon");
2083 qemu_opt_set(opts, "chardev", "debugcon");
2087 static QEMUMachine *machine_parse(const char *name)
2089 QEMUMachine *m, *machine = NULL;
2092 machine = find_machine(name);
2097 printf("Supported machines are:\n");
2098 for (m = first_machine; m != NULL; m = m->next) {
2100 printf("%-10s %s (alias of %s)\n", m->alias, m->desc, m->name);
2102 printf("%-10s %s%s\n", m->name, m->desc,
2103 m->is_default ? " (default)" : "");
2105 exit(!name || *name != '?');
2108 static int tcg_init(void)
2110 tcg_exec_init(tcg_tb_size * 1024 * 1024);
2115 const char *opt_name;
2117 int (*available)(void);
2121 { "tcg", "tcg", tcg_available, tcg_init, &tcg_allowed },
2122 { "xen", "Xen", xen_available, xen_init, &xen_allowed },
2123 { "kvm", "KVM", kvm_available, kvm_init, &kvm_allowed },
2126 static int configure_accelerator(void)
2128 const char *p = NULL;
2131 bool accel_initalised = 0;
2132 bool init_failed = 0;
2134 QemuOptsList *list = qemu_find_opts("machine");
2135 if (!QTAILQ_EMPTY(&list->head)) {
2136 p = qemu_opt_get(QTAILQ_FIRST(&list->head), "accel");
2140 /* Use the default "accelerator", tcg */
2144 while (!accel_initalised && *p != '\0') {
2148 p = get_opt_name(buf, sizeof (buf), p, ':');
2149 for (i = 0; i < ARRAY_SIZE(accel_list); i++) {
2150 if (strcmp(accel_list[i].opt_name, buf) == 0) {
2151 *(accel_list[i].allowed) = 1;
2152 ret = accel_list[i].init();
2155 if (!accel_list[i].available()) {
2156 printf("%s not supported for this target\n",
2157 accel_list[i].name);
2159 fprintf(stderr, "failed to initialize %s: %s\n",
2163 *(accel_list[i].allowed) = 0;
2165 accel_initalised = 1;
2170 if (i == ARRAY_SIZE(accel_list)) {
2171 fprintf(stderr, "\"%s\" accelerator does not exist.\n", buf);
2175 if (!accel_initalised) {
2176 fprintf(stderr, "No accelerator found!\n");
2181 fprintf(stderr, "Back to %s accelerator.\n", accel_list[i].name);
2184 return !accel_initalised;
2187 void qemu_add_exit_notifier(Notifier *notify)
2189 notifier_list_add(&exit_notifiers, notify);
2192 void qemu_remove_exit_notifier(Notifier *notify)
2194 notifier_list_remove(&exit_notifiers, notify);
2197 static void qemu_run_exit_notifiers(void)
2199 notifier_list_notify(&exit_notifiers, NULL);
2202 void qemu_add_machine_init_done_notifier(Notifier *notify)
2204 notifier_list_add(&machine_init_done_notifiers, notify);
2207 static void qemu_run_machine_init_done_notifiers(void)
2209 notifier_list_notify(&machine_init_done_notifiers, NULL);
2212 static const QEMUOption *lookup_opt(int argc, char **argv,
2213 const char **poptarg, int *poptind)
2215 const QEMUOption *popt;
2216 int optind = *poptind;
2217 char *r = argv[optind];
2220 loc_set_cmdline(argv, optind, 1);
2222 /* Treat --foo the same as -foo. */
2225 popt = qemu_options;
2228 error_report("invalid option");
2231 if (!strcmp(popt->name, r + 1))
2235 if (popt->flags & HAS_ARG) {
2236 if (optind >= argc) {
2237 error_report("requires an argument");
2240 optarg = argv[optind++];
2241 loc_set_cmdline(argv, optind - 2, 2);
2252 static gpointer malloc_and_trace(gsize n_bytes)
2254 void *ptr = malloc(n_bytes);
2255 trace_g_malloc(n_bytes, ptr);
2259 static gpointer realloc_and_trace(gpointer mem, gsize n_bytes)
2261 void *ptr = realloc(mem, n_bytes);
2262 trace_g_realloc(mem, n_bytes, ptr);
2266 static void free_and_trace(gpointer mem)
2272 int main(int argc, char **argv, char **envp)
2274 const char *gdbstub_dev = NULL;
2276 int snapshot, linux_boot;
2277 const char *icount_option = NULL;
2278 const char *initrd_filename;
2279 const char *kernel_filename, *kernel_cmdline;
2280 char boot_devices[33] = "cad"; /* default to HD->floppy->CD-ROM */
2282 DisplayChangeListener *dcl;
2283 int cyls, heads, secs, translation;
2284 QemuOpts *hda_opts = NULL, *opts;
2285 QemuOptsList *olist;
2288 const char *loadvm = NULL;
2289 QEMUMachine *machine;
2290 const char *cpu_model;
2291 const char *pid_file = NULL;
2292 const char *incoming = NULL;
2294 int show_vnc_port = 0;
2297 const char *log_mask = NULL;
2298 const char *log_file = NULL;
2299 GMemVTable mem_trace = {
2300 .malloc = malloc_and_trace,
2301 .realloc = realloc_and_trace,
2302 .free = free_and_trace,
2304 const char *trace_events = NULL;
2305 const char *trace_file = NULL;
2307 atexit(qemu_run_exit_notifiers);
2308 error_set_progname(argv[0]);
2310 g_mem_set_vtable(&mem_trace);
2311 g_thread_init(NULL);
2317 qemu_cache_utils_init(envp);
2319 QLIST_INIT (&vm_change_state_head);
2320 os_setup_early_signal_handling();
2322 module_call_init(MODULE_INIT_MACHINE);
2323 machine = find_default_machine();
2325 initrd_filename = NULL;
2328 kernel_filename = NULL;
2329 kernel_cmdline = "";
2330 cyls = heads = secs = 0;
2331 translation = BIOS_ATA_TRANSLATION_AUTO;
2333 for (i = 0; i < MAX_NODES; i++) {
2335 node_cpumask[i] = 0;
2343 /* first pass of option parsing */
2345 while (optind < argc) {
2346 if (argv[optind][0] != '-') {
2351 const QEMUOption *popt;
2353 popt = lookup_opt(argc, argv, &optarg, &optind);
2354 switch (popt->index) {
2355 case QEMU_OPTION_nodefconfig:
2365 ret = qemu_read_config_file(CONFIG_QEMU_CONFDIR "/qemu.conf");
2366 if (ret < 0 && ret != -ENOENT) {
2370 ret = qemu_read_config_file(arch_config_name);
2371 if (ret < 0 && ret != -ENOENT) {
2377 /* second pass of option parsing */
2382 if (argv[optind][0] != '-') {
2383 hda_opts = drive_add(IF_DEFAULT, 0, argv[optind++], HD_OPTS);
2385 const QEMUOption *popt;
2387 popt = lookup_opt(argc, argv, &optarg, &optind);
2388 if (!(popt->arch_mask & arch_type)) {
2389 printf("Option %s not supported for this target\n", popt->name);
2392 switch(popt->index) {
2394 machine = machine_parse(optarg);
2396 case QEMU_OPTION_cpu:
2397 /* hw initialization will check this */
2398 if (*optarg == '?') {
2399 list_cpus(stdout, &fprintf, optarg);
2405 case QEMU_OPTION_initrd:
2406 initrd_filename = optarg;
2408 case QEMU_OPTION_hda:
2412 snprintf(buf, sizeof(buf), "%s", HD_OPTS);
2414 snprintf(buf, sizeof(buf),
2415 "%s,cyls=%d,heads=%d,secs=%d%s",
2416 HD_OPTS , cyls, heads, secs,
2417 translation == BIOS_ATA_TRANSLATION_LBA ?
2419 translation == BIOS_ATA_TRANSLATION_NONE ?
2420 ",trans=none" : "");
2421 drive_add(IF_DEFAULT, 0, optarg, buf);
2424 case QEMU_OPTION_hdb:
2425 case QEMU_OPTION_hdc:
2426 case QEMU_OPTION_hdd:
2427 drive_add(IF_DEFAULT, popt->index - QEMU_OPTION_hda, optarg,
2430 case QEMU_OPTION_drive:
2431 if (drive_def(optarg) == NULL) {
2435 case QEMU_OPTION_set:
2436 if (qemu_set_option(optarg) != 0)
2439 case QEMU_OPTION_global:
2440 if (qemu_global_option(optarg) != 0)
2443 case QEMU_OPTION_mtdblock:
2444 drive_add(IF_MTD, -1, optarg, MTD_OPTS);
2446 case QEMU_OPTION_sd:
2447 drive_add(IF_SD, 0, optarg, SD_OPTS);
2449 case QEMU_OPTION_pflash:
2450 drive_add(IF_PFLASH, -1, optarg, PFLASH_OPTS);
2452 case QEMU_OPTION_snapshot:
2455 case QEMU_OPTION_hdachs:
2459 cyls = strtol(p, (char **)&p, 0);
2460 if (cyls < 1 || cyls > 16383)
2465 heads = strtol(p, (char **)&p, 0);
2466 if (heads < 1 || heads > 16)
2471 secs = strtol(p, (char **)&p, 0);
2472 if (secs < 1 || secs > 63)
2476 if (!strcmp(p, "none"))
2477 translation = BIOS_ATA_TRANSLATION_NONE;
2478 else if (!strcmp(p, "lba"))
2479 translation = BIOS_ATA_TRANSLATION_LBA;
2480 else if (!strcmp(p, "auto"))
2481 translation = BIOS_ATA_TRANSLATION_AUTO;
2484 } else if (*p != '\0') {
2486 fprintf(stderr, "qemu: invalid physical CHS format\n");
2489 if (hda_opts != NULL) {
2491 snprintf(num, sizeof(num), "%d", cyls);
2492 qemu_opt_set(hda_opts, "cyls", num);
2493 snprintf(num, sizeof(num), "%d", heads);
2494 qemu_opt_set(hda_opts, "heads", num);
2495 snprintf(num, sizeof(num), "%d", secs);
2496 qemu_opt_set(hda_opts, "secs", num);
2497 if (translation == BIOS_ATA_TRANSLATION_LBA)
2498 qemu_opt_set(hda_opts, "trans", "lba");
2499 if (translation == BIOS_ATA_TRANSLATION_NONE)
2500 qemu_opt_set(hda_opts, "trans", "none");
2504 case QEMU_OPTION_numa:
2505 if (nb_numa_nodes >= MAX_NODES) {
2506 fprintf(stderr, "qemu: too many NUMA nodes\n");
2511 case QEMU_OPTION_display:
2512 display_type = select_display(optarg);
2514 case QEMU_OPTION_nographic:
2515 display_type = DT_NOGRAPHIC;
2517 case QEMU_OPTION_curses:
2518 #ifdef CONFIG_CURSES
2519 display_type = DT_CURSES;
2521 fprintf(stderr, "Curses support is disabled\n");
2525 case QEMU_OPTION_portrait:
2526 graphic_rotate = 90;
2528 case QEMU_OPTION_rotate:
2529 graphic_rotate = strtol(optarg, (char **) &optarg, 10);
2530 if (graphic_rotate != 0 && graphic_rotate != 90 &&
2531 graphic_rotate != 180 && graphic_rotate != 270) {
2533 "qemu: only 90, 180, 270 deg rotation is available\n");
2537 case QEMU_OPTION_kernel:
2538 kernel_filename = optarg;
2540 case QEMU_OPTION_append:
2541 kernel_cmdline = optarg;
2543 case QEMU_OPTION_cdrom:
2544 drive_add(IF_DEFAULT, 2, optarg, CDROM_OPTS);
2546 case QEMU_OPTION_boot:
2548 static const char * const params[] = {
2549 "order", "once", "menu",
2550 "splash", "splash-time", NULL
2552 char buf[sizeof(boot_devices)];
2553 char *standard_boot_devices;
2556 if (!strchr(optarg, '=')) {
2558 pstrcpy(buf, sizeof(buf), optarg);
2559 } else if (check_params(buf, sizeof(buf), params, optarg) < 0) {
2561 "qemu: unknown boot parameter '%s' in '%s'\n",
2567 get_param_value(buf, sizeof(buf), "order", optarg)) {
2568 validate_bootdevices(buf);
2569 pstrcpy(boot_devices, sizeof(boot_devices), buf);
2572 if (get_param_value(buf, sizeof(buf),
2574 validate_bootdevices(buf);
2575 standard_boot_devices = g_strdup(boot_devices);
2576 pstrcpy(boot_devices, sizeof(boot_devices), buf);
2577 qemu_register_reset(restore_boot_devices,
2578 standard_boot_devices);
2580 if (get_param_value(buf, sizeof(buf),
2582 if (!strcmp(buf, "on")) {
2584 } else if (!strcmp(buf, "off")) {
2588 "qemu: invalid option value '%s'\n",
2593 qemu_opts_parse(qemu_find_opts("boot-opts"),
2598 case QEMU_OPTION_fda:
2599 case QEMU_OPTION_fdb:
2600 drive_add(IF_FLOPPY, popt->index - QEMU_OPTION_fda,
2603 case QEMU_OPTION_no_fd_bootchk:
2606 case QEMU_OPTION_netdev:
2607 if (net_client_parse(qemu_find_opts("netdev"), optarg) == -1) {
2611 case QEMU_OPTION_net:
2612 if (net_client_parse(qemu_find_opts("net"), optarg) == -1) {
2617 case QEMU_OPTION_tftp:
2618 legacy_tftp_prefix = optarg;
2620 case QEMU_OPTION_bootp:
2621 legacy_bootp_filename = optarg;
2623 case QEMU_OPTION_redir:
2624 if (net_slirp_redir(optarg) < 0)
2628 case QEMU_OPTION_bt:
2629 add_device_config(DEV_BT, optarg);
2631 case QEMU_OPTION_audio_help:
2632 if (!(audio_available())) {
2633 printf("Option %s not supported for this target\n", popt->name);
2639 case QEMU_OPTION_soundhw:
2640 if (!(audio_available())) {
2641 printf("Option %s not supported for this target\n", popt->name);
2644 select_soundhw (optarg);
2649 case QEMU_OPTION_version:
2653 case QEMU_OPTION_m: {
2656 value = strtosz(optarg, NULL);
2658 fprintf(stderr, "qemu: invalid ram size: %s\n", optarg);
2662 if (value != (uint64_t)(ram_addr_t)value) {
2663 fprintf(stderr, "qemu: ram size too large\n");
2669 case QEMU_OPTION_mempath:
2673 case QEMU_OPTION_mem_prealloc:
2684 gdbstub_dev = "tcp::" DEFAULT_GDBSTUB_PORT;
2686 case QEMU_OPTION_gdb:
2687 gdbstub_dev = optarg;
2692 case QEMU_OPTION_bios:
2695 case QEMU_OPTION_singlestep:
2702 keyboard_layout = optarg;
2704 case QEMU_OPTION_localtime:
2707 case QEMU_OPTION_vga:
2708 select_vgahw (optarg);
2715 w = strtol(p, (char **)&p, 10);
2718 fprintf(stderr, "qemu: invalid resolution or depth\n");
2724 h = strtol(p, (char **)&p, 10);
2729 depth = strtol(p, (char **)&p, 10);
2730 if (depth != 8 && depth != 15 && depth != 16 &&
2731 depth != 24 && depth != 32)
2733 } else if (*p == '\0') {
2734 depth = graphic_depth;
2741 graphic_depth = depth;
2744 case QEMU_OPTION_echr:
2747 term_escape_char = strtol(optarg, &r, 0);
2749 printf("Bad argument to echr\n");
2752 case QEMU_OPTION_monitor:
2753 monitor_parse(optarg, "readline");
2754 default_monitor = 0;
2756 case QEMU_OPTION_qmp:
2757 monitor_parse(optarg, "control");
2758 default_monitor = 0;
2760 case QEMU_OPTION_mon:
2761 opts = qemu_opts_parse(qemu_find_opts("mon"), optarg, 1);
2765 default_monitor = 0;
2767 case QEMU_OPTION_chardev:
2768 opts = qemu_opts_parse(qemu_find_opts("chardev"), optarg, 1);
2773 case QEMU_OPTION_fsdev:
2774 olist = qemu_find_opts("fsdev");
2776 fprintf(stderr, "fsdev is not supported by this qemu build.\n");
2779 opts = qemu_opts_parse(olist, optarg, 1);
2781 fprintf(stderr, "parse error: %s\n", optarg);
2785 case QEMU_OPTION_virtfs: {
2789 olist = qemu_find_opts("virtfs");
2791 fprintf(stderr, "virtfs is not supported by this qemu build.\n");
2794 opts = qemu_opts_parse(olist, optarg, 1);
2796 fprintf(stderr, "parse error: %s\n", optarg);
2800 if (qemu_opt_get(opts, "fstype") == NULL ||
2801 qemu_opt_get(opts, "mount_tag") == NULL ||
2802 qemu_opt_get(opts, "path") == NULL ||
2803 qemu_opt_get(opts, "security_model") == NULL) {
2804 fprintf(stderr, "Usage: -virtfs fstype,path=/share_path/,"
2805 "security_model=[mapped|passthrough|none],"
2806 "mount_tag=tag.\n");
2810 fsdev = qemu_opts_create(qemu_find_opts("fsdev"),
2811 qemu_opt_get(opts, "mount_tag"), 1);
2813 fprintf(stderr, "duplicate fsdev id: %s\n",
2814 qemu_opt_get(opts, "mount_tag"));
2817 qemu_opt_set(fsdev, "fstype", qemu_opt_get(opts, "fstype"));
2818 qemu_opt_set(fsdev, "path", qemu_opt_get(opts, "path"));
2819 qemu_opt_set(fsdev, "security_model",
2820 qemu_opt_get(opts, "security_model"));
2822 device = qemu_opts_create(qemu_find_opts("device"), NULL, 0);
2823 qemu_opt_set(device, "driver", "virtio-9p-pci");
2824 qemu_opt_set(device, "fsdev",
2825 qemu_opt_get(opts, "mount_tag"));
2826 qemu_opt_set(device, "mount_tag",
2827 qemu_opt_get(opts, "mount_tag"));
2830 case QEMU_OPTION_serial:
2831 add_device_config(DEV_SERIAL, optarg);
2833 if (strncmp(optarg, "mon:", 4) == 0) {
2834 default_monitor = 0;
2837 case QEMU_OPTION_watchdog:
2840 "qemu: only one watchdog option may be given\n");
2845 case QEMU_OPTION_watchdog_action:
2846 if (select_watchdog_action(optarg) == -1) {
2847 fprintf(stderr, "Unknown -watchdog-action parameter\n");
2851 case QEMU_OPTION_virtiocon:
2852 add_device_config(DEV_VIRTCON, optarg);
2853 default_virtcon = 0;
2854 if (strncmp(optarg, "mon:", 4) == 0) {
2855 default_monitor = 0;
2858 case QEMU_OPTION_parallel:
2859 add_device_config(DEV_PARALLEL, optarg);
2860 default_parallel = 0;
2861 if (strncmp(optarg, "mon:", 4) == 0) {
2862 default_monitor = 0;
2865 case QEMU_OPTION_debugcon:
2866 add_device_config(DEV_DEBUGCON, optarg);
2868 case QEMU_OPTION_loadvm:
2871 case QEMU_OPTION_full_screen:
2875 case QEMU_OPTION_no_frame:
2878 case QEMU_OPTION_alt_grab:
2881 case QEMU_OPTION_ctrl_grab:
2884 case QEMU_OPTION_no_quit:
2887 case QEMU_OPTION_sdl:
2888 display_type = DT_SDL;
2891 case QEMU_OPTION_no_frame:
2892 case QEMU_OPTION_alt_grab:
2893 case QEMU_OPTION_ctrl_grab:
2894 case QEMU_OPTION_no_quit:
2895 case QEMU_OPTION_sdl:
2896 fprintf(stderr, "SDL support is disabled\n");
2899 case QEMU_OPTION_pidfile:
2902 case QEMU_OPTION_win2k_hack:
2903 win2k_install_hack = 1;
2905 case QEMU_OPTION_rtc_td_hack:
2908 case QEMU_OPTION_acpitable:
2909 do_acpitable_option(optarg);
2911 case QEMU_OPTION_smbios:
2912 do_smbios_option(optarg);
2914 case QEMU_OPTION_enable_kvm:
2915 olist = qemu_find_opts("machine");
2916 qemu_opts_reset(olist);
2917 qemu_opts_parse(olist, "accel=kvm", 0);
2919 case QEMU_OPTION_machine:
2920 olist = qemu_find_opts("machine");
2921 qemu_opts_reset(olist);
2922 opts = qemu_opts_parse(olist, optarg, 1);
2924 fprintf(stderr, "parse error: %s\n", optarg);
2927 optarg = qemu_opt_get(opts, "type");
2929 machine = machine_parse(optarg);
2932 case QEMU_OPTION_usb:
2935 case QEMU_OPTION_usbdevice:
2937 add_device_config(DEV_USB, optarg);
2939 case QEMU_OPTION_device:
2940 if (!qemu_opts_parse(qemu_find_opts("device"), optarg, 1)) {
2944 case QEMU_OPTION_smp:
2947 fprintf(stderr, "Invalid number of CPUs\n");
2950 if (max_cpus < smp_cpus) {
2951 fprintf(stderr, "maxcpus must be equal to or greater than "
2955 if (max_cpus > 255) {
2956 fprintf(stderr, "Unsupported number of maxcpus\n");
2960 case QEMU_OPTION_vnc:
2963 vnc_display = optarg;
2965 fprintf(stderr, "VNC support is disabled\n");
2969 case QEMU_OPTION_no_acpi:
2972 case QEMU_OPTION_no_hpet:
2975 case QEMU_OPTION_balloon:
2976 if (balloon_parse(optarg) < 0) {
2977 fprintf(stderr, "Unknown -balloon argument %s\n", optarg);
2981 case QEMU_OPTION_no_reboot:
2984 case QEMU_OPTION_no_shutdown:
2987 case QEMU_OPTION_show_cursor:
2990 case QEMU_OPTION_uuid:
2991 if(qemu_uuid_parse(optarg, qemu_uuid) < 0) {
2992 fprintf(stderr, "Fail to parse UUID string."
2993 " Wrong format.\n");
2997 case QEMU_OPTION_option_rom:
2998 if (nb_option_roms >= MAX_OPTION_ROMS) {
2999 fprintf(stderr, "Too many option ROMs\n");
3002 opts = qemu_opts_parse(qemu_find_opts("option-rom"), optarg, 1);
3003 option_rom[nb_option_roms].name = qemu_opt_get(opts, "romfile");
3004 option_rom[nb_option_roms].bootindex =
3005 qemu_opt_get_number(opts, "bootindex", -1);
3006 if (!option_rom[nb_option_roms].name) {
3007 fprintf(stderr, "Option ROM file is not specified\n");
3012 case QEMU_OPTION_semihosting:
3013 semihosting_enabled = 1;
3015 case QEMU_OPTION_name:
3016 qemu_name = g_strdup(optarg);
3018 char *p = strchr(qemu_name, ',');
3021 if (strncmp(p, "process=", 8)) {
3022 fprintf(stderr, "Unknown subargument %s to -name\n", p);
3026 os_set_proc_name(p);
3030 case QEMU_OPTION_prom_env:
3031 if (nb_prom_envs >= MAX_PROM_ENVS) {
3032 fprintf(stderr, "Too many prom variables\n");
3035 prom_envs[nb_prom_envs] = optarg;
3038 case QEMU_OPTION_old_param:
3041 case QEMU_OPTION_clock:
3042 configure_alarms(optarg);
3044 case QEMU_OPTION_startdate:
3045 configure_rtc_date_offset(optarg, 1);
3047 case QEMU_OPTION_rtc:
3048 opts = qemu_opts_parse(qemu_find_opts("rtc"), optarg, 0);
3052 configure_rtc(opts);
3054 case QEMU_OPTION_tb_size:
3055 tcg_tb_size = strtol(optarg, NULL, 0);
3056 if (tcg_tb_size < 0) {
3060 case QEMU_OPTION_icount:
3061 icount_option = optarg;
3063 case QEMU_OPTION_incoming:
3066 case QEMU_OPTION_nodefaults:
3068 default_parallel = 0;
3069 default_virtcon = 0;
3070 default_monitor = 0;
3077 case QEMU_OPTION_xen_domid:
3078 if (!(xen_available())) {
3079 printf("Option %s not supported for this target\n", popt->name);
3082 xen_domid = atoi(optarg);
3084 case QEMU_OPTION_xen_create:
3085 if (!(xen_available())) {
3086 printf("Option %s not supported for this target\n", popt->name);
3089 xen_mode = XEN_CREATE;
3091 case QEMU_OPTION_xen_attach:
3092 if (!(xen_available())) {
3093 printf("Option %s not supported for this target\n", popt->name);
3096 xen_mode = XEN_ATTACH;
3098 case QEMU_OPTION_trace:
3100 opts = qemu_opts_parse(qemu_find_opts("trace"), optarg, 0);
3104 trace_events = qemu_opt_get(opts, "events");
3105 trace_file = qemu_opt_get(opts, "file");
3108 case QEMU_OPTION_readconfig:
3110 int ret = qemu_read_config_file(optarg);
3112 fprintf(stderr, "read config %s: %s\n", optarg,
3118 case QEMU_OPTION_spice:
3119 olist = qemu_find_opts("spice");
3121 fprintf(stderr, "spice is not supported by this qemu build.\n");
3124 opts = qemu_opts_parse(olist, optarg, 0);
3126 fprintf(stderr, "parse error: %s\n", optarg);
3130 case QEMU_OPTION_writeconfig:
3133 if (strcmp(optarg, "-") == 0) {
3136 fp = fopen(optarg, "w");
3138 fprintf(stderr, "open %s: %s\n", optarg, strerror(errno));
3142 qemu_config_write(fp);
3147 os_parse_cmd_args(popt->index, optarg);
3153 /* Open the logfile at this point, if necessary. We can't open the logfile
3154 * when encountering either of the logging options (-d or -D) because the
3155 * other one may be encountered later on the command line, changing the
3156 * location or level of logging.
3160 set_cpu_log_filename(log_file);
3162 set_cpu_log(log_mask);
3165 if (!trace_backend_init(trace_events, trace_file)) {
3169 /* If no data_dir is specified then try to find it relative to the
3172 data_dir = os_find_datadir(argv[0]);
3174 /* If all else fails use the install path specified when building. */
3176 data_dir = CONFIG_QEMU_DATADIR;
3180 * Default to max_cpus = smp_cpus, in case the user doesn't
3181 * specify a max_cpus value.
3184 max_cpus = smp_cpus;
3186 machine->max_cpus = machine->max_cpus ?: 1; /* Default to UP */
3187 if (smp_cpus > machine->max_cpus) {
3188 fprintf(stderr, "Number of SMP cpus requested (%d), exceeds max cpus "
3189 "supported by machine `%s' (%d)\n", smp_cpus, machine->name,
3195 * Get the default machine options from the machine if it is not already
3196 * specified either by the configuration file or by the command line.
3198 if (machine->default_machine_opts) {
3199 QemuOptsList *list = qemu_find_opts("machine");
3200 const char *p = NULL;
3202 if (!QTAILQ_EMPTY(&list->head)) {
3203 p = qemu_opt_get(QTAILQ_FIRST(&list->head), "accel");
3206 qemu_opts_reset(list);
3207 opts = qemu_opts_parse(list, machine->default_machine_opts, 0);
3209 fprintf(stderr, "parse error for machine %s: %s\n",
3210 machine->name, machine->default_machine_opts);
3216 qemu_opts_foreach(qemu_find_opts("device"), default_driver_check, NULL, 0);
3217 qemu_opts_foreach(qemu_find_opts("global"), default_driver_check, NULL, 0);
3219 if (machine->no_serial) {
3222 if (machine->no_parallel) {
3223 default_parallel = 0;
3225 if (!machine->use_virtcon) {
3226 default_virtcon = 0;
3228 if (machine->no_vga) {
3231 if (machine->no_floppy) {
3234 if (machine->no_cdrom) {
3237 if (machine->no_sdcard) {
3241 if (display_type == DT_NOGRAPHIC) {
3242 if (default_parallel)
3243 add_device_config(DEV_PARALLEL, "null");
3244 if (default_serial && default_monitor) {
3245 add_device_config(DEV_SERIAL, "mon:stdio");
3246 } else if (default_virtcon && default_monitor) {
3247 add_device_config(DEV_VIRTCON, "mon:stdio");
3250 add_device_config(DEV_SERIAL, "stdio");
3251 if (default_virtcon)
3252 add_device_config(DEV_VIRTCON, "stdio");
3253 if (default_monitor)
3254 monitor_parse("stdio", "readline");
3258 add_device_config(DEV_SERIAL, "vc:80Cx24C");
3259 if (default_parallel)
3260 add_device_config(DEV_PARALLEL, "vc:80Cx24C");
3261 if (default_monitor)
3262 monitor_parse("vc:80Cx24C", "readline");
3263 if (default_virtcon)
3264 add_device_config(DEV_VIRTCON, "vc:80Cx24C");
3267 vga_interface_type = VGA_CIRRUS;
3271 if (qemu_opts_foreach(qemu_find_opts("chardev"), chardev_init_func, NULL, 1) != 0)
3273 #ifdef CONFIG_VIRTFS
3274 if (qemu_opts_foreach(qemu_find_opts("fsdev"), fsdev_init_func, NULL, 1) != 0) {
3281 if (pid_file && qemu_create_pidfile(pid_file) != 0) {
3286 /* init the memory */
3287 if (ram_size == 0) {
3288 ram_size = DEFAULT_RAM_SIZE * 1024 * 1024;
3291 configure_accelerator();
3293 if (qemu_init_main_loop()) {
3294 fprintf(stderr, "qemu_init_main_loop failed\n");
3297 linux_boot = (kernel_filename != NULL);
3299 if (!linux_boot && *kernel_cmdline != '\0') {
3300 fprintf(stderr, "-append only allowed with -kernel option\n");
3304 if (!linux_boot && initrd_filename != NULL) {
3305 fprintf(stderr, "-initrd only allowed with -kernel option\n");
3309 os_set_line_buffering();
3311 if (init_timer_alarm() < 0) {
3312 fprintf(stderr, "could not initialize alarm timer\n");
3315 configure_icount(icount_option);
3317 if (net_init_clients() < 0) {
3321 /* init the bluetooth world */
3322 if (foreach_device_config(DEV_BT, bt_parse))
3325 if (!xen_enabled()) {
3326 /* On 32-bit hosts, QEMU is limited by virtual address space */
3327 if (ram_size > (2047 << 20) && HOST_LONG_BITS == 32) {
3328 fprintf(stderr, "qemu: at most 2047 MB RAM can be simulated\n");
3333 cpu_exec_init_all();
3335 bdrv_init_with_whitelist();
3339 /* open the virtual block devices */
3341 qemu_opts_foreach(qemu_find_opts("drive"), drive_enable_snapshot, NULL, 0);
3342 if (qemu_opts_foreach(qemu_find_opts("drive"), drive_init_func, &machine->use_scsi, 1) != 0)
3345 default_drive(default_cdrom, snapshot, machine->use_scsi,
3346 IF_DEFAULT, 2, CDROM_OPTS);
3347 default_drive(default_floppy, snapshot, machine->use_scsi,
3348 IF_FLOPPY, 0, FD_OPTS);
3349 default_drive(default_sdcard, snapshot, machine->use_scsi,
3352 register_savevm_live(NULL, "ram", 0, 4, NULL, ram_save_live, NULL,
3355 if (nb_numa_nodes > 0) {
3358 if (nb_numa_nodes > MAX_NODES) {
3359 nb_numa_nodes = MAX_NODES;
3362 /* If no memory size if given for any node, assume the default case
3363 * and distribute the available memory equally across all nodes
3365 for (i = 0; i < nb_numa_nodes; i++) {
3366 if (node_mem[i] != 0)
3369 if (i == nb_numa_nodes) {
3370 uint64_t usedmem = 0;
3372 /* On Linux, the each node's border has to be 8MB aligned,
3373 * the final node gets the rest.
3375 for (i = 0; i < nb_numa_nodes - 1; i++) {
3376 node_mem[i] = (ram_size / nb_numa_nodes) & ~((1 << 23UL) - 1);
3377 usedmem += node_mem[i];
3379 node_mem[i] = ram_size - usedmem;
3382 for (i = 0; i < nb_numa_nodes; i++) {
3383 if (node_cpumask[i] != 0)
3386 /* assigning the VCPUs round-robin is easier to implement, guest OSes
3387 * must cope with this anyway, because there are BIOSes out there in
3388 * real machines which also use this scheme.
3390 if (i == nb_numa_nodes) {
3391 for (i = 0; i < smp_cpus; i++) {
3392 node_cpumask[i % nb_numa_nodes] |= 1 << i;
3397 if (qemu_opts_foreach(qemu_find_opts("mon"), mon_init_func, NULL, 1) != 0) {
3401 if (foreach_device_config(DEV_SERIAL, serial_parse) < 0)
3403 if (foreach_device_config(DEV_PARALLEL, parallel_parse) < 0)
3405 if (foreach_device_config(DEV_VIRTCON, virtcon_parse) < 0)
3407 if (foreach_device_config(DEV_DEBUGCON, debugcon_parse) < 0)
3410 module_call_init(MODULE_INIT_DEVICE);
3412 if (qemu_opts_foreach(qemu_find_opts("device"), device_help_func, NULL, 0) != 0)
3416 i = select_watchdog(watchdog);
3418 exit (i == 1 ? 1 : 0);
3421 if (machine->compat_props) {
3422 qdev_prop_register_global_list(machine->compat_props);
3426 machine->init(ram_size, boot_devices,
3427 kernel_filename, kernel_cmdline, initrd_filename, cpu_model);
3429 cpu_synchronize_all_post_init();
3433 current_machine = machine;
3435 /* init USB devices */
3437 if (foreach_device_config(DEV_USB, usb_parse) < 0)
3441 /* init generic devices */
3442 if (qemu_opts_foreach(qemu_find_opts("device"), device_init_func, NULL, 1) != 0)
3445 net_check_clients();
3447 /* just use the first displaystate for the moment */
3448 ds = get_displaystate();
3452 if (display_type == DT_DEFAULT && !display_remote) {
3453 #if defined(CONFIG_SDL) || defined(CONFIG_COCOA)
3454 display_type = DT_SDL;
3455 #elif defined(CONFIG_VNC)
3456 vnc_display = "localhost:0,to=99";
3459 display_type = DT_NONE;
3464 /* init local displays */
3465 switch (display_type) {
3468 #if defined(CONFIG_CURSES)
3470 curses_display_init(ds, full_screen);
3473 #if defined(CONFIG_SDL)
3475 sdl_display_init(ds, full_screen, no_frame);
3477 #elif defined(CONFIG_COCOA)
3479 cocoa_display_init(ds, full_screen);
3486 /* must be after terminal init, SDL library changes signal handlers */
3487 os_setup_signal_handling();
3490 /* init remote displays */
3492 vnc_display_init(ds);
3493 if (vnc_display_open(ds, vnc_display) < 0)
3496 if (show_vnc_port) {
3497 printf("VNC server running on `%s'\n", vnc_display_local_addr(ds));
3502 if (using_spice && !qxl_enabled) {
3503 qemu_spice_display_init(ds);
3509 dcl = ds->listeners;
3510 while (dcl != NULL) {
3511 if (dcl->dpy_refresh != NULL) {
3512 ds->gui_timer = qemu_new_timer_ms(rt_clock, gui_update, ds);
3513 qemu_mod_timer(ds->gui_timer, qemu_get_clock_ms(rt_clock));
3518 if (ds->gui_timer == NULL) {
3519 nographic_timer = qemu_new_timer_ms(rt_clock, nographic_update, NULL);
3520 qemu_mod_timer(nographic_timer, qemu_get_clock_ms(rt_clock));
3522 text_consoles_set_display(ds);
3524 if (gdbstub_dev && gdbserver_start(gdbstub_dev) < 0) {
3525 fprintf(stderr, "qemu: could not open gdbserver on device '%s'\n",
3530 qdev_machine_creation_done();
3532 if (rom_load_all() != 0) {
3533 fprintf(stderr, "rom loading failed\n");
3537 /* TODO: once all bus devices are qdevified, this should be done
3538 * when bus is created by qdev.c */
3539 qemu_register_reset(qbus_reset_all_fn, sysbus_get_default());
3540 qemu_run_machine_init_done_notifiers();
3542 qemu_system_reset(VMRESET_SILENT);
3544 if (load_vmstate(loadvm) < 0) {
3550 runstate_set(RUN_STATE_INMIGRATE);
3551 int ret = qemu_start_incoming_migration(incoming);
3553 fprintf(stderr, "Migration failed. Exit code %s(%d), exiting.\n",
3557 } else if (autostart) {