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>
52 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__DragonFly__)
54 #include <sys/sysctl.h>
62 #include <sys/prctl.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>
80 /* See MySQL bug #7156 (http://bugs.mysql.com/bug.php?id=7156) for
81 discussion about Solaris header problems */
82 extern int madvise(caddr_t, size_t, int);
87 #if defined(__OpenBSD__)
91 #if defined(CONFIG_VDE)
92 #include <libvdeplug.h>
100 #if defined(__APPLE__) || defined(main)
102 int qemu_main(int argc, char **argv, char **envp);
103 int main(int argc, char **argv)
105 return qemu_main(argc, argv, NULL);
108 #define main qemu_main
110 #endif /* CONFIG_SDL */
114 #define main qemu_main
115 #endif /* CONFIG_COCOA */
118 #include "hw/boards.h"
120 #include "hw/pcmcia.h"
125 #include "hw/watchdog.h"
126 #include "hw/smbios.h"
129 #include "hw/loader.h"
132 #include "net/slirp.h"
137 #include "qemu-timer.h"
138 #include "qemu-char.h"
139 #include "cache-utils.h"
141 #include "block_int.h"
142 #include "block-migration.h"
144 #include "audio/audio.h"
145 #include "migration.h"
147 #include "qemu-option.h"
148 #include "qemu-config.h"
149 #include "qemu-objects.h"
150 #include "qemu-options.h"
152 #include "fsdev/qemu-fsdev.h"
157 #include "qemu_socket.h"
159 #include "slirp/libslirp.h"
161 #include "qemu-queue.h"
163 #include "arch_init.h"
166 //#define DEBUG_SLIRP
168 #define DEFAULT_RAM_SIZE 128
170 #define MAX_VIRTIO_CONSOLES 1
172 static const char *data_dir;
173 const char *bios_name = NULL;
174 /* Note: drives_table[MAX_DRIVES] is a dummy block driver if none available
175 to store the VM snapshots */
176 struct drivelist drives = QTAILQ_HEAD_INITIALIZER(drives);
177 struct driveoptlist driveopts = QTAILQ_HEAD_INITIALIZER(driveopts);
178 enum vga_retrace_method vga_retrace_method = VGA_RETRACE_DUMB;
179 DisplayType display_type = DT_DEFAULT;
180 const char* keyboard_layout = NULL;
182 const char *mem_path = NULL;
184 int mem_prealloc = 0; /* force preallocation of physical target memory */
187 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;
210 const char *vnc_display;
211 int acpi_enabled = 1;
217 int graphic_rotate = 0;
218 uint8_t irq0override = 1;
219 const char *watchdog;
220 const char *option_rom[MAX_OPTION_ROMS];
222 int semihosting_enabled = 0;
224 const char *qemu_name;
227 unsigned int nb_prom_envs = 0;
228 const char *prom_envs[MAX_PROM_ENVS];
232 uint64_t node_mem[MAX_NODES];
233 uint64_t node_cpumask[MAX_NODES];
235 static QEMUTimer *nographic_timer;
237 uint8_t qemu_uuid[16];
239 static QEMUBootSetHandler *boot_set_handler;
240 static void *boot_set_opaque;
244 enum xen_mode xen_mode = XEN_EMULATE;
246 static int default_serial = 1;
247 static int default_parallel = 1;
248 static int default_virtcon = 1;
249 static int default_monitor = 1;
250 static int default_vga = 1;
251 static int default_floppy = 1;
252 static int default_cdrom = 1;
253 static int default_sdcard = 1;
259 { .driver = "isa-serial", .flag = &default_serial },
260 { .driver = "isa-parallel", .flag = &default_parallel },
261 { .driver = "isa-fdc", .flag = &default_floppy },
262 { .driver = "ide-drive", .flag = &default_cdrom },
263 { .driver = "virtio-serial-pci", .flag = &default_virtcon },
264 { .driver = "virtio-serial-s390", .flag = &default_virtcon },
265 { .driver = "virtio-serial", .flag = &default_virtcon },
266 { .driver = "VGA", .flag = &default_vga },
267 { .driver = "cirrus-vga", .flag = &default_vga },
268 { .driver = "vmware-svga", .flag = &default_vga },
271 static int default_driver_check(QemuOpts *opts, void *opaque)
273 const char *driver = qemu_opt_get(opts, "driver");
278 for (i = 0; i < ARRAY_SIZE(default_list); i++) {
279 if (strcmp(default_list[i].driver, driver) != 0)
281 *(default_list[i].flag) = 0;
286 /***********************************************************/
288 static void set_proc_name(const char *s)
290 #if defined(__linux__) && defined(PR_SET_NAME)
294 name[sizeof(name) - 1] = 0;
295 strncpy(name, s, sizeof(name));
296 /* Could rewrite argv[0] too, but that's a bit more complicated.
297 This simple way is enough for `top'. */
298 prctl(PR_SET_NAME, name);
302 /***********************************************************/
303 /* real time host monotonic timer */
305 /* compute with 96 bit intermediate result: (a*b)/c */
306 uint64_t muldiv64(uint64_t a, uint32_t b, uint32_t c)
311 #ifdef HOST_WORDS_BIGENDIAN
321 rl = (uint64_t)u.l.low * (uint64_t)b;
322 rh = (uint64_t)u.l.high * (uint64_t)b;
325 res.l.low = (((rh % c) << 32) + (rl & 0xffffffff)) / c;
329 /***********************************************************/
330 /* host time/date access */
331 void qemu_get_timedate(struct tm *tm, int offset)
338 if (rtc_date_offset == -1) {
342 ret = localtime(&ti);
344 ti -= rtc_date_offset;
348 memcpy(tm, ret, sizeof(struct tm));
351 int qemu_timedate_diff(struct tm *tm)
355 if (rtc_date_offset == -1)
357 seconds = mktimegm(tm);
359 seconds = mktime(tm);
361 seconds = mktimegm(tm) + rtc_date_offset;
363 return seconds - time(NULL);
366 void rtc_change_mon_event(struct tm *tm)
370 data = qobject_from_jsonf("{ 'offset': %d }", qemu_timedate_diff(tm));
371 monitor_protocol_event(QEVENT_RTC_CHANGE, data);
372 qobject_decref(data);
375 static void configure_rtc_date_offset(const char *startdate, int legacy)
377 time_t rtc_start_date;
380 if (!strcmp(startdate, "now") && legacy) {
381 rtc_date_offset = -1;
383 if (sscanf(startdate, "%d-%d-%dT%d:%d:%d",
391 } else if (sscanf(startdate, "%d-%d-%d",
403 rtc_start_date = mktimegm(&tm);
404 if (rtc_start_date == -1) {
406 fprintf(stderr, "Invalid date format. Valid formats are:\n"
407 "'2006-06-17T16:01:21' or '2006-06-17'\n");
410 rtc_date_offset = time(NULL) - rtc_start_date;
414 static void configure_rtc(QemuOpts *opts)
418 value = qemu_opt_get(opts, "base");
420 if (!strcmp(value, "utc")) {
422 } else if (!strcmp(value, "localtime")) {
425 configure_rtc_date_offset(value, 0);
428 value = qemu_opt_get(opts, "clock");
430 if (!strcmp(value, "host")) {
431 rtc_clock = host_clock;
432 } else if (!strcmp(value, "vm")) {
433 rtc_clock = vm_clock;
435 fprintf(stderr, "qemu: invalid option value '%s'\n", value);
439 value = qemu_opt_get(opts, "driftfix");
441 if (!strcmp(value, "slew")) {
443 } else if (!strcmp(value, "none")) {
446 fprintf(stderr, "qemu: invalid option value '%s'\n", value);
452 /***********************************************************/
453 /* Bluetooth support */
456 static struct HCIInfo *hci_table[MAX_NICS];
458 static struct bt_vlan_s {
459 struct bt_scatternet_s net;
461 struct bt_vlan_s *next;
464 /* find or alloc a new bluetooth "VLAN" */
465 static struct bt_scatternet_s *qemu_find_bt_vlan(int id)
467 struct bt_vlan_s **pvlan, *vlan;
468 for (vlan = first_bt_vlan; vlan != NULL; vlan = vlan->next) {
472 vlan = qemu_mallocz(sizeof(struct bt_vlan_s));
474 pvlan = &first_bt_vlan;
475 while (*pvlan != NULL)
476 pvlan = &(*pvlan)->next;
481 static void null_hci_send(struct HCIInfo *hci, const uint8_t *data, int len)
485 static int null_hci_addr_set(struct HCIInfo *hci, const uint8_t *bd_addr)
490 static struct HCIInfo null_hci = {
491 .cmd_send = null_hci_send,
492 .sco_send = null_hci_send,
493 .acl_send = null_hci_send,
494 .bdaddr_set = null_hci_addr_set,
497 struct HCIInfo *qemu_next_hci(void)
499 if (cur_hci == nb_hcis)
502 return hci_table[cur_hci++];
505 static struct HCIInfo *hci_init(const char *str)
508 struct bt_scatternet_s *vlan = 0;
510 if (!strcmp(str, "null"))
513 else if (!strncmp(str, "host", 4) && (str[4] == '\0' || str[4] == ':'))
515 return bt_host_hci(str[4] ? str + 5 : "hci0");
516 else if (!strncmp(str, "hci", 3)) {
519 if (!strncmp(str + 3, ",vlan=", 6)) {
520 vlan = qemu_find_bt_vlan(strtol(str + 9, &endp, 0));
525 vlan = qemu_find_bt_vlan(0);
527 return bt_new_hci(vlan);
530 fprintf(stderr, "qemu: Unknown bluetooth HCI `%s'.\n", str);
535 static int bt_hci_parse(const char *str)
540 if (nb_hcis >= MAX_NICS) {
541 fprintf(stderr, "qemu: Too many bluetooth HCIs (max %i).\n", MAX_NICS);
554 bdaddr.b[5] = 0x56 + nb_hcis;
555 hci->bdaddr_set(hci, bdaddr.b);
557 hci_table[nb_hcis++] = hci;
562 static void bt_vhci_add(int vlan_id)
564 struct bt_scatternet_s *vlan = qemu_find_bt_vlan(vlan_id);
567 fprintf(stderr, "qemu: warning: adding a VHCI to "
568 "an empty scatternet %i\n", vlan_id);
570 bt_vhci_init(bt_new_hci(vlan));
573 static struct bt_device_s *bt_device_add(const char *opt)
575 struct bt_scatternet_s *vlan;
577 char *endp = strstr(opt, ",vlan=");
578 int len = (endp ? endp - opt : strlen(opt)) + 1;
581 pstrcpy(devname, MIN(sizeof(devname), len), opt);
584 vlan_id = strtol(endp + 6, &endp, 0);
586 fprintf(stderr, "qemu: unrecognised bluetooth vlan Id\n");
591 vlan = qemu_find_bt_vlan(vlan_id);
594 fprintf(stderr, "qemu: warning: adding a slave device to "
595 "an empty scatternet %i\n", vlan_id);
597 if (!strcmp(devname, "keyboard"))
598 return bt_keyboard_init(vlan);
600 fprintf(stderr, "qemu: unsupported bluetooth device `%s'\n", devname);
604 static int bt_parse(const char *opt)
606 const char *endp, *p;
609 if (strstart(opt, "hci", &endp)) {
610 if (!*endp || *endp == ',') {
612 if (!strstart(endp, ",vlan=", 0))
615 return bt_hci_parse(opt);
617 } else if (strstart(opt, "vhci", &endp)) {
618 if (!*endp || *endp == ',') {
620 if (strstart(endp, ",vlan=", &p)) {
621 vlan = strtol(p, (char **) &endp, 0);
623 fprintf(stderr, "qemu: bad scatternet '%s'\n", p);
627 fprintf(stderr, "qemu: bad parameter '%s'\n", endp + 1);
636 } else if (strstart(opt, "device:", &endp))
637 return !bt_device_add(endp);
639 fprintf(stderr, "qemu: bad bluetooth parameter '%s'\n", opt);
643 /***********************************************************/
644 /* QEMU Block devices */
646 #define HD_ALIAS "index=%d,media=disk"
647 #define CDROM_ALIAS "index=2,media=cdrom"
648 #define FD_ALIAS "index=%d,if=floppy"
649 #define PFLASH_ALIAS "if=pflash"
650 #define MTD_ALIAS "if=mtd"
651 #define SD_ALIAS "index=0,if=sd"
653 QemuOpts *drive_add(const char *file, const char *fmt, ...)
660 vsnprintf(optstr, sizeof(optstr), fmt, ap);
663 opts = qemu_opts_parse(&qemu_drive_opts, optstr, 0);
668 qemu_opt_set(opts, "file", file);
672 DriveInfo *drive_get(BlockInterfaceType type, int bus, int unit)
676 /* seek interface, bus and unit */
678 QTAILQ_FOREACH(dinfo, &drives, next) {
679 if (dinfo->type == type &&
688 DriveInfo *drive_get_by_id(const char *id)
692 QTAILQ_FOREACH(dinfo, &drives, next) {
693 if (strcmp(id, dinfo->id))
700 int drive_get_max_bus(BlockInterfaceType type)
706 QTAILQ_FOREACH(dinfo, &drives, next) {
707 if(dinfo->type == type &&
708 dinfo->bus > max_bus)
709 max_bus = dinfo->bus;
714 const char *drive_get_serial(BlockDriverState *bdrv)
718 QTAILQ_FOREACH(dinfo, &drives, next) {
719 if (dinfo->bdrv == bdrv)
720 return dinfo->serial;
726 BlockInterfaceErrorAction drive_get_on_error(
727 BlockDriverState *bdrv, int is_read)
731 QTAILQ_FOREACH(dinfo, &drives, next) {
732 if (dinfo->bdrv == bdrv)
733 return is_read ? dinfo->on_read_error : dinfo->on_write_error;
736 return is_read ? BLOCK_ERR_REPORT : BLOCK_ERR_STOP_ENOSPC;
739 static void bdrv_format_print(void *opaque, const char *name)
741 fprintf(stderr, " %s", name);
744 void drive_uninit(DriveInfo *dinfo)
746 qemu_opts_del(dinfo->opts);
747 bdrv_delete(dinfo->bdrv);
748 QTAILQ_REMOVE(&drives, dinfo, next);
752 static int parse_block_error_action(const char *buf, int is_read)
754 if (!strcmp(buf, "ignore")) {
755 return BLOCK_ERR_IGNORE;
756 } else if (!is_read && !strcmp(buf, "enospc")) {
757 return BLOCK_ERR_STOP_ENOSPC;
758 } else if (!strcmp(buf, "stop")) {
759 return BLOCK_ERR_STOP_ANY;
760 } else if (!strcmp(buf, "report")) {
761 return BLOCK_ERR_REPORT;
763 fprintf(stderr, "qemu: '%s' invalid %s error action\n",
764 buf, is_read ? "read" : "write");
769 DriveInfo *drive_init(QemuOpts *opts, void *opaque,
773 const char *file = NULL;
776 const char *mediastr = "";
777 BlockInterfaceType type;
778 enum { MEDIA_DISK, MEDIA_CDROM } media;
780 int cyls, heads, secs, translation;
781 BlockDriver *drv = NULL;
782 QEMUMachine *machine = opaque;
787 int on_read_error, on_write_error;
794 translation = BIOS_ATA_TRANSLATION_AUTO;
796 if (machine && machine->use_scsi) {
798 max_devs = MAX_SCSI_DEVS;
799 pstrcpy(devname, sizeof(devname), "scsi");
802 max_devs = MAX_IDE_DEVS;
803 pstrcpy(devname, sizeof(devname), "ide");
807 /* extract parameters */
808 bus_id = qemu_opt_get_number(opts, "bus", 0);
809 unit_id = qemu_opt_get_number(opts, "unit", -1);
810 index = qemu_opt_get_number(opts, "index", -1);
812 cyls = qemu_opt_get_number(opts, "cyls", 0);
813 heads = qemu_opt_get_number(opts, "heads", 0);
814 secs = qemu_opt_get_number(opts, "secs", 0);
816 snapshot = qemu_opt_get_bool(opts, "snapshot", 0);
817 ro = qemu_opt_get_bool(opts, "readonly", 0);
819 file = qemu_opt_get(opts, "file");
820 serial = qemu_opt_get(opts, "serial");
822 if ((buf = qemu_opt_get(opts, "if")) != NULL) {
823 pstrcpy(devname, sizeof(devname), buf);
824 if (!strcmp(buf, "ide")) {
826 max_devs = MAX_IDE_DEVS;
827 } else if (!strcmp(buf, "scsi")) {
829 max_devs = MAX_SCSI_DEVS;
830 } else if (!strcmp(buf, "floppy")) {
833 } else if (!strcmp(buf, "pflash")) {
836 } else if (!strcmp(buf, "mtd")) {
839 } else if (!strcmp(buf, "sd")) {
842 } else if (!strcmp(buf, "virtio")) {
845 } else if (!strcmp(buf, "xen")) {
848 } else if (!strcmp(buf, "none")) {
852 fprintf(stderr, "qemu: unsupported bus type '%s'\n", buf);
857 if (cyls || heads || secs) {
858 if (cyls < 1 || (type == IF_IDE && cyls > 16383)) {
859 fprintf(stderr, "qemu: '%s' invalid physical cyls number\n", buf);
862 if (heads < 1 || (type == IF_IDE && heads > 16)) {
863 fprintf(stderr, "qemu: '%s' invalid physical heads number\n", buf);
866 if (secs < 1 || (type == IF_IDE && secs > 63)) {
867 fprintf(stderr, "qemu: '%s' invalid physical secs number\n", buf);
872 if ((buf = qemu_opt_get(opts, "trans")) != NULL) {
875 "qemu: '%s' trans must be used with cyls,heads and secs\n",
879 if (!strcmp(buf, "none"))
880 translation = BIOS_ATA_TRANSLATION_NONE;
881 else if (!strcmp(buf, "lba"))
882 translation = BIOS_ATA_TRANSLATION_LBA;
883 else if (!strcmp(buf, "auto"))
884 translation = BIOS_ATA_TRANSLATION_AUTO;
886 fprintf(stderr, "qemu: '%s' invalid translation type\n", buf);
891 if ((buf = qemu_opt_get(opts, "media")) != NULL) {
892 if (!strcmp(buf, "disk")) {
894 } else if (!strcmp(buf, "cdrom")) {
895 if (cyls || secs || heads) {
897 "qemu: '%s' invalid physical CHS format\n", buf);
902 fprintf(stderr, "qemu: '%s' invalid media\n", buf);
907 if ((buf = qemu_opt_get(opts, "cache")) != NULL) {
908 if (!strcmp(buf, "off") || !strcmp(buf, "none")) {
909 bdrv_flags |= BDRV_O_NOCACHE;
910 } else if (!strcmp(buf, "writeback")) {
911 bdrv_flags |= BDRV_O_CACHE_WB;
912 } else if (!strcmp(buf, "unsafe")) {
913 bdrv_flags |= BDRV_O_CACHE_WB;
914 bdrv_flags |= BDRV_O_NO_FLUSH;
915 } else if (!strcmp(buf, "writethrough")) {
916 /* this is the default */
918 fprintf(stderr, "qemu: invalid cache option\n");
923 #ifdef CONFIG_LINUX_AIO
924 if ((buf = qemu_opt_get(opts, "aio")) != NULL) {
925 if (!strcmp(buf, "native")) {
926 bdrv_flags |= BDRV_O_NATIVE_AIO;
927 } else if (!strcmp(buf, "threads")) {
928 /* this is the default */
930 fprintf(stderr, "qemu: invalid aio option\n");
936 if ((buf = qemu_opt_get(opts, "format")) != NULL) {
937 if (strcmp(buf, "?") == 0) {
938 fprintf(stderr, "qemu: Supported formats:");
939 bdrv_iterate_format(bdrv_format_print, NULL);
940 fprintf(stderr, "\n");
943 drv = bdrv_find_whitelisted_format(buf);
945 fprintf(stderr, "qemu: '%s' invalid format\n", buf);
950 on_write_error = BLOCK_ERR_STOP_ENOSPC;
951 if ((buf = qemu_opt_get(opts, "werror")) != NULL) {
952 if (type != IF_IDE && type != IF_SCSI && type != IF_VIRTIO && type != IF_NONE) {
953 fprintf(stderr, "werror is no supported by this format\n");
957 on_write_error = parse_block_error_action(buf, 0);
958 if (on_write_error < 0) {
963 on_read_error = BLOCK_ERR_REPORT;
964 if ((buf = qemu_opt_get(opts, "rerror")) != NULL) {
965 if (type != IF_IDE && type != IF_VIRTIO && type != IF_NONE) {
966 fprintf(stderr, "rerror is no supported by this format\n");
970 on_read_error = parse_block_error_action(buf, 1);
971 if (on_read_error < 0) {
976 if ((devaddr = qemu_opt_get(opts, "addr")) != NULL) {
977 if (type != IF_VIRTIO) {
978 fprintf(stderr, "addr is not supported\n");
983 /* compute bus and unit according index */
986 if (bus_id != 0 || unit_id != -1) {
988 "qemu: index cannot be used with bus and unit\n");
996 unit_id = index % max_devs;
997 bus_id = index / max_devs;
1001 /* if user doesn't specify a unit_id,
1002 * try to find the first free
1005 if (unit_id == -1) {
1007 while (drive_get(type, bus_id, unit_id) != NULL) {
1009 if (max_devs && unit_id >= max_devs) {
1010 unit_id -= max_devs;
1018 if (max_devs && unit_id >= max_devs) {
1019 fprintf(stderr, "qemu: unit %d too big (max is %d)\n",
1020 unit_id, max_devs - 1);
1025 * ignore multiple definitions
1028 if (drive_get(type, bus_id, unit_id) != NULL) {
1035 dinfo = qemu_mallocz(sizeof(*dinfo));
1036 if ((buf = qemu_opts_id(opts)) != NULL) {
1037 dinfo->id = qemu_strdup(buf);
1039 /* no id supplied -> create one */
1040 dinfo->id = qemu_mallocz(32);
1041 if (type == IF_IDE || type == IF_SCSI)
1042 mediastr = (media == MEDIA_CDROM) ? "-cd" : "-hd";
1044 snprintf(dinfo->id, 32, "%s%i%s%i",
1045 devname, bus_id, mediastr, unit_id);
1047 snprintf(dinfo->id, 32, "%s%s%i",
1048 devname, mediastr, unit_id);
1050 dinfo->bdrv = bdrv_new(dinfo->id);
1051 dinfo->devaddr = devaddr;
1053 dinfo->bus = bus_id;
1054 dinfo->unit = unit_id;
1055 dinfo->on_read_error = on_read_error;
1056 dinfo->on_write_error = on_write_error;
1059 strncpy(dinfo->serial, serial, sizeof(serial));
1060 QTAILQ_INSERT_TAIL(&drives, dinfo, next);
1070 bdrv_set_geometry_hint(dinfo->bdrv, cyls, heads, secs);
1071 bdrv_set_translation_hint(dinfo->bdrv, translation);
1075 bdrv_set_type_hint(dinfo->bdrv, BDRV_TYPE_CDROM);
1080 /* FIXME: This isn't really a floppy, but it's a reasonable
1083 bdrv_set_type_hint(dinfo->bdrv, BDRV_TYPE_FLOPPY);
1089 /* add virtio block device */
1090 opts = qemu_opts_create(&qemu_device_opts, NULL, 0);
1091 qemu_opt_set(opts, "driver", "virtio-blk-pci");
1092 qemu_opt_set(opts, "drive", dinfo->id);
1094 qemu_opt_set(opts, "addr", devaddr);
1104 /* always use cache=unsafe with snapshot */
1105 bdrv_flags &= ~BDRV_O_CACHE_MASK;
1106 bdrv_flags |= (BDRV_O_SNAPSHOT|BDRV_O_CACHE_WB|BDRV_O_NO_FLUSH);
1109 if (media == MEDIA_CDROM) {
1110 /* CDROM is fine for any interface, don't check. */
1112 } else if (ro == 1) {
1113 if (type != IF_SCSI && type != IF_VIRTIO && type != IF_FLOPPY && type != IF_NONE) {
1114 fprintf(stderr, "qemu: readonly flag not supported for drive with this interface\n");
1119 bdrv_flags |= ro ? 0 : BDRV_O_RDWR;
1121 if (bdrv_open(dinfo->bdrv, file, bdrv_flags, drv) < 0) {
1122 fprintf(stderr, "qemu: could not open disk image %s: %s\n",
1123 file, strerror(errno));
1127 if (bdrv_key_required(dinfo->bdrv))
1133 static int drive_init_func(QemuOpts *opts, void *opaque)
1135 QEMUMachine *machine = opaque;
1136 int fatal_error = 0;
1138 if (drive_init(opts, machine, &fatal_error) == NULL) {
1145 static int drive_enable_snapshot(QemuOpts *opts, void *opaque)
1147 if (NULL == qemu_opt_get(opts, "snapshot")) {
1148 qemu_opt_set(opts, "snapshot", "on");
1153 void qemu_register_boot_set(QEMUBootSetHandler *func, void *opaque)
1155 boot_set_handler = func;
1156 boot_set_opaque = opaque;
1159 int qemu_boot_set(const char *boot_devices)
1161 if (!boot_set_handler) {
1164 return boot_set_handler(boot_set_opaque, boot_devices);
1167 static void validate_bootdevices(char *devices)
1169 /* We just do some generic consistency checks */
1173 for (p = devices; *p != '\0'; p++) {
1174 /* Allowed boot devices are:
1175 * a-b: floppy disk drives
1176 * c-f: IDE disk drives
1177 * g-m: machine implementation dependant drives
1178 * n-p: network devices
1179 * It's up to each machine implementation to check if the given boot
1180 * devices match the actual hardware implementation and firmware
1183 if (*p < 'a' || *p > 'p') {
1184 fprintf(stderr, "Invalid boot device '%c'\n", *p);
1187 if (bitmap & (1 << (*p - 'a'))) {
1188 fprintf(stderr, "Boot device '%c' was given twice\n", *p);
1191 bitmap |= 1 << (*p - 'a');
1195 static void restore_boot_devices(void *opaque)
1197 char *standard_boot_devices = opaque;
1198 static int first = 1;
1200 /* Restore boot order and remove ourselves after the first boot */
1206 qemu_boot_set(standard_boot_devices);
1208 qemu_unregister_reset(restore_boot_devices, standard_boot_devices);
1209 qemu_free(standard_boot_devices);
1212 static void numa_add(const char *optarg)
1216 unsigned long long value, endvalue;
1219 optarg = get_opt_name(option, 128, optarg, ',') + 1;
1220 if (!strcmp(option, "node")) {
1221 if (get_param_value(option, 128, "nodeid", optarg) == 0) {
1222 nodenr = nb_numa_nodes;
1224 nodenr = strtoull(option, NULL, 10);
1227 if (get_param_value(option, 128, "mem", optarg) == 0) {
1228 node_mem[nodenr] = 0;
1230 value = strtoull(option, &endptr, 0);
1232 case 0: case 'M': case 'm':
1239 node_mem[nodenr] = value;
1241 if (get_param_value(option, 128, "cpus", optarg) == 0) {
1242 node_cpumask[nodenr] = 0;
1244 value = strtoull(option, &endptr, 10);
1247 fprintf(stderr, "only 64 CPUs in NUMA mode supported.\n");
1249 if (*endptr == '-') {
1250 endvalue = strtoull(endptr+1, &endptr, 10);
1251 if (endvalue >= 63) {
1254 "only 63 CPUs in NUMA mode supported.\n");
1256 value = (2ULL << endvalue) - (1ULL << value);
1258 value = 1ULL << value;
1261 node_cpumask[nodenr] = value;
1268 static void smp_parse(const char *optarg)
1270 int smp, sockets = 0, threads = 0, cores = 0;
1274 smp = strtoul(optarg, &endptr, 10);
1275 if (endptr != optarg) {
1276 if (*endptr == ',') {
1280 if (get_param_value(option, 128, "sockets", endptr) != 0)
1281 sockets = strtoull(option, NULL, 10);
1282 if (get_param_value(option, 128, "cores", endptr) != 0)
1283 cores = strtoull(option, NULL, 10);
1284 if (get_param_value(option, 128, "threads", endptr) != 0)
1285 threads = strtoull(option, NULL, 10);
1286 if (get_param_value(option, 128, "maxcpus", endptr) != 0)
1287 max_cpus = strtoull(option, NULL, 10);
1289 /* compute missing values, prefer sockets over cores over threads */
1290 if (smp == 0 || sockets == 0) {
1291 sockets = sockets > 0 ? sockets : 1;
1292 cores = cores > 0 ? cores : 1;
1293 threads = threads > 0 ? threads : 1;
1295 smp = cores * threads * sockets;
1299 threads = threads > 0 ? threads : 1;
1300 cores = smp / (sockets * threads);
1303 threads = smp / (cores * sockets);
1308 smp_cores = cores > 0 ? cores : 1;
1309 smp_threads = threads > 0 ? threads : 1;
1311 max_cpus = smp_cpus;
1314 /***********************************************************/
1317 static int usb_device_add(const char *devname, int is_hotplug)
1320 USBDevice *dev = NULL;
1325 /* drivers with .usbdevice_name entry in USBDeviceInfo */
1326 dev = usbdevice_create(devname);
1330 /* the other ones */
1331 if (strstart(devname, "host:", &p)) {
1332 dev = usb_host_device_open(p);
1333 } else if (!strcmp(devname, "bt") || strstart(devname, "bt:", &p)) {
1334 dev = usb_bt_init(devname[2] ? hci_init(p) :
1335 bt_new_hci(qemu_find_bt_vlan(0)));
1346 static int usb_device_del(const char *devname)
1351 if (strstart(devname, "host:", &p))
1352 return usb_host_device_close(p);
1357 p = strchr(devname, '.');
1360 bus_num = strtoul(devname, NULL, 0);
1361 addr = strtoul(p + 1, NULL, 0);
1363 return usb_device_delete_addr(bus_num, addr);
1366 static int usb_parse(const char *cmdline)
1369 r = usb_device_add(cmdline, 0);
1371 fprintf(stderr, "qemu: could not add USB device '%s'\n", cmdline);
1376 void do_usb_add(Monitor *mon, const QDict *qdict)
1378 const char *devname = qdict_get_str(qdict, "devname");
1379 if (usb_device_add(devname, 1) < 0) {
1380 error_report("could not add USB device '%s'", devname);
1384 void do_usb_del(Monitor *mon, const QDict *qdict)
1386 const char *devname = qdict_get_str(qdict, "devname");
1387 if (usb_device_del(devname) < 0) {
1388 error_report("could not delete USB device '%s'", devname);
1392 /***********************************************************/
1393 /* PCMCIA/Cardbus */
1395 static struct pcmcia_socket_entry_s {
1396 PCMCIASocket *socket;
1397 struct pcmcia_socket_entry_s *next;
1398 } *pcmcia_sockets = 0;
1400 void pcmcia_socket_register(PCMCIASocket *socket)
1402 struct pcmcia_socket_entry_s *entry;
1404 entry = qemu_malloc(sizeof(struct pcmcia_socket_entry_s));
1405 entry->socket = socket;
1406 entry->next = pcmcia_sockets;
1407 pcmcia_sockets = entry;
1410 void pcmcia_socket_unregister(PCMCIASocket *socket)
1412 struct pcmcia_socket_entry_s *entry, **ptr;
1414 ptr = &pcmcia_sockets;
1415 for (entry = *ptr; entry; ptr = &entry->next, entry = *ptr)
1416 if (entry->socket == socket) {
1422 void pcmcia_info(Monitor *mon)
1424 struct pcmcia_socket_entry_s *iter;
1426 if (!pcmcia_sockets)
1427 monitor_printf(mon, "No PCMCIA sockets\n");
1429 for (iter = pcmcia_sockets; iter; iter = iter->next)
1430 monitor_printf(mon, "%s: %s\n", iter->socket->slot_string,
1431 iter->socket->attached ? iter->socket->card_string :
1435 /***********************************************************/
1438 typedef struct IOHandlerRecord {
1440 IOCanReadHandler *fd_read_poll;
1442 IOHandler *fd_write;
1445 /* temporary data */
1447 QLIST_ENTRY(IOHandlerRecord) next;
1450 static QLIST_HEAD(, IOHandlerRecord) io_handlers =
1451 QLIST_HEAD_INITIALIZER(io_handlers);
1454 /* XXX: fd_read_poll should be suppressed, but an API change is
1455 necessary in the character devices to suppress fd_can_read(). */
1456 int qemu_set_fd_handler2(int fd,
1457 IOCanReadHandler *fd_read_poll,
1459 IOHandler *fd_write,
1462 IOHandlerRecord *ioh;
1464 if (!fd_read && !fd_write) {
1465 QLIST_FOREACH(ioh, &io_handlers, next) {
1466 if (ioh->fd == fd) {
1472 QLIST_FOREACH(ioh, &io_handlers, next) {
1476 ioh = qemu_mallocz(sizeof(IOHandlerRecord));
1477 QLIST_INSERT_HEAD(&io_handlers, ioh, next);
1480 ioh->fd_read_poll = fd_read_poll;
1481 ioh->fd_read = fd_read;
1482 ioh->fd_write = fd_write;
1483 ioh->opaque = opaque;
1489 int qemu_set_fd_handler(int fd,
1491 IOHandler *fd_write,
1494 return qemu_set_fd_handler2(fd, NULL, fd_read, fd_write, opaque);
1497 /***********************************************************/
1498 /* machine registration */
1500 static QEMUMachine *first_machine = NULL;
1501 QEMUMachine *current_machine = NULL;
1503 int qemu_register_machine(QEMUMachine *m)
1506 pm = &first_machine;
1514 static QEMUMachine *find_machine(const char *name)
1518 for(m = first_machine; m != NULL; m = m->next) {
1519 if (!strcmp(m->name, name))
1521 if (m->alias && !strcmp(m->alias, name))
1527 static QEMUMachine *find_default_machine(void)
1531 for(m = first_machine; m != NULL; m = m->next) {
1532 if (m->is_default) {
1539 /***********************************************************/
1540 /* main execution loop */
1542 static void gui_update(void *opaque)
1544 uint64_t interval = GUI_REFRESH_INTERVAL;
1545 DisplayState *ds = opaque;
1546 DisplayChangeListener *dcl = ds->listeners;
1548 qemu_flush_coalesced_mmio_buffer();
1551 while (dcl != NULL) {
1552 if (dcl->gui_timer_interval &&
1553 dcl->gui_timer_interval < interval)
1554 interval = dcl->gui_timer_interval;
1557 qemu_mod_timer(ds->gui_timer, interval + qemu_get_clock(rt_clock));
1560 static void nographic_update(void *opaque)
1562 uint64_t interval = GUI_REFRESH_INTERVAL;
1564 qemu_flush_coalesced_mmio_buffer();
1565 qemu_mod_timer(nographic_timer, interval + qemu_get_clock(rt_clock));
1568 struct vm_change_state_entry {
1569 VMChangeStateHandler *cb;
1571 QLIST_ENTRY (vm_change_state_entry) entries;
1574 static QLIST_HEAD(vm_change_state_head, vm_change_state_entry) vm_change_state_head;
1576 VMChangeStateEntry *qemu_add_vm_change_state_handler(VMChangeStateHandler *cb,
1579 VMChangeStateEntry *e;
1581 e = qemu_mallocz(sizeof (*e));
1585 QLIST_INSERT_HEAD(&vm_change_state_head, e, entries);
1589 void qemu_del_vm_change_state_handler(VMChangeStateEntry *e)
1591 QLIST_REMOVE (e, entries);
1595 void vm_state_notify(int running, int reason)
1597 VMChangeStateEntry *e;
1599 for (e = vm_change_state_head.lh_first; e; e = e->entries.le_next) {
1600 e->cb(e->opaque, running, reason);
1609 vm_state_notify(1, 0);
1611 monitor_protocol_event(QEVENT_RESUME, NULL);
1615 /* reset/shutdown handler */
1617 typedef struct QEMUResetEntry {
1618 QTAILQ_ENTRY(QEMUResetEntry) entry;
1619 QEMUResetHandler *func;
1623 static QTAILQ_HEAD(reset_handlers, QEMUResetEntry) reset_handlers =
1624 QTAILQ_HEAD_INITIALIZER(reset_handlers);
1625 static int reset_requested;
1626 static int shutdown_requested;
1627 static int powerdown_requested;
1628 int debug_requested;
1629 int vmstop_requested;
1631 int qemu_shutdown_requested(void)
1633 int r = shutdown_requested;
1634 shutdown_requested = 0;
1638 int qemu_reset_requested(void)
1640 int r = reset_requested;
1641 reset_requested = 0;
1645 int qemu_powerdown_requested(void)
1647 int r = powerdown_requested;
1648 powerdown_requested = 0;
1652 static int qemu_debug_requested(void)
1654 int r = debug_requested;
1655 debug_requested = 0;
1659 static int qemu_vmstop_requested(void)
1661 int r = vmstop_requested;
1662 vmstop_requested = 0;
1666 void qemu_register_reset(QEMUResetHandler *func, void *opaque)
1668 QEMUResetEntry *re = qemu_mallocz(sizeof(QEMUResetEntry));
1671 re->opaque = opaque;
1672 QTAILQ_INSERT_TAIL(&reset_handlers, re, entry);
1675 void qemu_unregister_reset(QEMUResetHandler *func, void *opaque)
1679 QTAILQ_FOREACH(re, &reset_handlers, entry) {
1680 if (re->func == func && re->opaque == opaque) {
1681 QTAILQ_REMOVE(&reset_handlers, re, entry);
1688 void qemu_system_reset(void)
1690 QEMUResetEntry *re, *nre;
1692 /* reset all devices */
1693 QTAILQ_FOREACH_SAFE(re, &reset_handlers, entry, nre) {
1694 re->func(re->opaque);
1696 monitor_protocol_event(QEVENT_RESET, NULL);
1697 cpu_synchronize_all_post_reset();
1700 void qemu_system_reset_request(void)
1703 shutdown_requested = 1;
1705 reset_requested = 1;
1707 qemu_notify_event();
1710 void qemu_system_shutdown_request(void)
1712 shutdown_requested = 1;
1713 qemu_notify_event();
1716 void qemu_system_powerdown_request(void)
1718 powerdown_requested = 1;
1719 qemu_notify_event();
1722 void main_loop_wait(int nonblocking)
1724 IOHandlerRecord *ioh;
1725 fd_set rfds, wfds, xfds;
1733 timeout = qemu_calculate_timeout();
1734 qemu_bh_update_timeout(&timeout);
1737 os_host_main_loop_wait(&timeout);
1739 /* poll any events */
1740 /* XXX: separate device handlers from system ones */
1745 QLIST_FOREACH(ioh, &io_handlers, next) {
1749 (!ioh->fd_read_poll ||
1750 ioh->fd_read_poll(ioh->opaque) != 0)) {
1751 FD_SET(ioh->fd, &rfds);
1755 if (ioh->fd_write) {
1756 FD_SET(ioh->fd, &wfds);
1762 tv.tv_sec = timeout / 1000;
1763 tv.tv_usec = (timeout % 1000) * 1000;
1765 slirp_select_fill(&nfds, &rfds, &wfds, &xfds);
1767 qemu_mutex_unlock_iothread();
1768 ret = select(nfds + 1, &rfds, &wfds, &xfds, &tv);
1769 qemu_mutex_lock_iothread();
1771 IOHandlerRecord *pioh;
1773 QLIST_FOREACH_SAFE(ioh, &io_handlers, next, pioh) {
1775 QLIST_REMOVE(ioh, next);
1779 if (ioh->fd_read && FD_ISSET(ioh->fd, &rfds)) {
1780 ioh->fd_read(ioh->opaque);
1782 if (ioh->fd_write && FD_ISSET(ioh->fd, &wfds)) {
1783 ioh->fd_write(ioh->opaque);
1788 slirp_select_poll(&rfds, &wfds, &xfds, (ret < 0));
1790 qemu_run_all_timers();
1792 /* Check bottom-halves last in case any of the earlier events triggered
1798 static int vm_can_run(void)
1800 if (powerdown_requested)
1802 if (reset_requested)
1804 if (shutdown_requested)
1806 if (debug_requested)
1811 qemu_irq qemu_system_powerdown;
1813 static void main_loop(void)
1817 qemu_main_loop_start();
1821 bool nonblocking = false;
1822 #ifdef CONFIG_PROFILER
1825 #ifndef CONFIG_IOTHREAD
1826 nonblocking = tcg_cpu_exec();
1828 #ifdef CONFIG_PROFILER
1829 ti = profile_getclock();
1831 main_loop_wait(nonblocking);
1832 #ifdef CONFIG_PROFILER
1833 dev_time += profile_getclock() - ti;
1835 } while (vm_can_run());
1837 if ((r = qemu_debug_requested())) {
1840 if (qemu_shutdown_requested()) {
1841 monitor_protocol_event(QEVENT_SHUTDOWN, NULL);
1848 if (qemu_reset_requested()) {
1850 qemu_system_reset();
1853 if (qemu_powerdown_requested()) {
1854 monitor_protocol_event(QEVENT_POWERDOWN, NULL);
1855 qemu_irq_raise(qemu_system_powerdown);
1857 if ((r = qemu_vmstop_requested())) {
1864 static void version(void)
1866 printf("QEMU emulator version " QEMU_VERSION QEMU_PKGVERSION ", Copyright (c) 2003-2008 Fabrice Bellard\n");
1869 static void help(int exitcode)
1871 const char *options_help =
1872 #define DEF(option, opt_arg, opt_enum, opt_help, arch_mask) \
1874 #define DEFHEADING(text) stringify(text) "\n"
1875 #include "qemu-options.def"
1881 printf("usage: %s [options] [disk_image]\n"
1883 "'disk_image' is a raw hard disk image for IDE hard disk 0\n"
1886 "During emulation, the following keys are useful:\n"
1887 "ctrl-alt-f toggle full screen\n"
1888 "ctrl-alt-n switch to virtual console 'n'\n"
1889 "ctrl-alt toggle mouse and keyboard grab\n"
1891 "When using -nographic, press 'ctrl-a h' to get some help.\n",
1897 #define HAS_ARG 0x0001
1899 typedef struct QEMUOption {
1906 static const QEMUOption qemu_options[] = {
1907 { "h", 0, QEMU_OPTION_h, QEMU_ARCH_ALL },
1908 #define DEF(option, opt_arg, opt_enum, opt_help, arch_mask) \
1909 { option, opt_arg, opt_enum, arch_mask },
1910 #define DEFHEADING(text)
1911 #include "qemu-options.def"
1917 static void select_vgahw (const char *p)
1922 vga_interface_type = VGA_NONE;
1923 if (strstart(p, "std", &opts)) {
1924 vga_interface_type = VGA_STD;
1925 } else if (strstart(p, "cirrus", &opts)) {
1926 vga_interface_type = VGA_CIRRUS;
1927 } else if (strstart(p, "vmware", &opts)) {
1928 vga_interface_type = VGA_VMWARE;
1929 } else if (strstart(p, "xenfb", &opts)) {
1930 vga_interface_type = VGA_XENFB;
1931 } else if (!strstart(p, "none", &opts)) {
1933 fprintf(stderr, "Unknown vga type: %s\n", p);
1937 const char *nextopt;
1939 if (strstart(opts, ",retrace=", &nextopt)) {
1941 if (strstart(opts, "dumb", &nextopt))
1942 vga_retrace_method = VGA_RETRACE_DUMB;
1943 else if (strstart(opts, "precise", &nextopt))
1944 vga_retrace_method = VGA_RETRACE_PRECISE;
1945 else goto invalid_vga;
1946 } else goto invalid_vga;
1951 static int balloon_parse(const char *arg)
1955 if (strcmp(arg, "none") == 0) {
1959 if (!strncmp(arg, "virtio", 6)) {
1960 if (arg[6] == ',') {
1961 /* have params -> parse them */
1962 opts = qemu_opts_parse(&qemu_device_opts, arg+7, 0);
1966 /* create empty opts */
1967 opts = qemu_opts_create(&qemu_device_opts, NULL, 0);
1969 qemu_opt_set(opts, "driver", "virtio-balloon-pci");
1976 char *qemu_find_file(int type, const char *name)
1982 /* If name contains path separators then try it as a straight path. */
1983 if ((strchr(name, '/') || strchr(name, '\\'))
1984 && access(name, R_OK) == 0) {
1985 return qemu_strdup(name);
1988 case QEMU_FILE_TYPE_BIOS:
1991 case QEMU_FILE_TYPE_KEYMAP:
1992 subdir = "keymaps/";
1997 len = strlen(data_dir) + strlen(name) + strlen(subdir) + 2;
1998 buf = qemu_mallocz(len);
1999 snprintf(buf, len, "%s/%s%s", data_dir, subdir, name);
2000 if (access(buf, R_OK)) {
2007 static int device_help_func(QemuOpts *opts, void *opaque)
2009 return qdev_device_help(opts);
2012 static int device_init_func(QemuOpts *opts, void *opaque)
2016 dev = qdev_device_add(opts);
2022 static int chardev_init_func(QemuOpts *opts, void *opaque)
2024 CharDriverState *chr;
2026 chr = qemu_chr_open_opts(opts, NULL);
2033 static int fsdev_init_func(QemuOpts *opts, void *opaque)
2036 ret = qemu_fsdev_add(opts);
2042 static int mon_init_func(QemuOpts *opts, void *opaque)
2044 CharDriverState *chr;
2045 const char *chardev;
2049 mode = qemu_opt_get(opts, "mode");
2053 if (strcmp(mode, "readline") == 0) {
2054 flags = MONITOR_USE_READLINE;
2055 } else if (strcmp(mode, "control") == 0) {
2056 flags = MONITOR_USE_CONTROL;
2058 fprintf(stderr, "unknown monitor mode \"%s\"\n", mode);
2062 if (qemu_opt_get_bool(opts, "default", 0))
2063 flags |= MONITOR_IS_DEFAULT;
2065 chardev = qemu_opt_get(opts, "chardev");
2066 chr = qemu_chr_find(chardev);
2068 fprintf(stderr, "chardev \"%s\" not found\n", chardev);
2072 monitor_init(chr, flags);
2076 static void monitor_parse(const char *optarg, const char *mode)
2078 static int monitor_device_index = 0;
2084 if (strstart(optarg, "chardev:", &p)) {
2085 snprintf(label, sizeof(label), "%s", p);
2087 snprintf(label, sizeof(label), "compat_monitor%d",
2088 monitor_device_index);
2089 if (monitor_device_index == 0) {
2092 opts = qemu_chr_parse_compat(label, optarg);
2094 fprintf(stderr, "parse error: %s\n", optarg);
2099 opts = qemu_opts_create(&qemu_mon_opts, label, 1);
2101 fprintf(stderr, "duplicate chardev: %s\n", label);
2104 qemu_opt_set(opts, "mode", mode);
2105 qemu_opt_set(opts, "chardev", label);
2107 qemu_opt_set(opts, "default", "on");
2108 monitor_device_index++;
2111 struct device_config {
2113 DEV_USB, /* -usbdevice */
2115 DEV_SERIAL, /* -serial */
2116 DEV_PARALLEL, /* -parallel */
2117 DEV_VIRTCON, /* -virtioconsole */
2118 DEV_DEBUGCON, /* -debugcon */
2120 const char *cmdline;
2121 QTAILQ_ENTRY(device_config) next;
2123 QTAILQ_HEAD(, device_config) device_configs = QTAILQ_HEAD_INITIALIZER(device_configs);
2125 static void add_device_config(int type, const char *cmdline)
2127 struct device_config *conf;
2129 conf = qemu_mallocz(sizeof(*conf));
2131 conf->cmdline = cmdline;
2132 QTAILQ_INSERT_TAIL(&device_configs, conf, next);
2135 static int foreach_device_config(int type, int (*func)(const char *cmdline))
2137 struct device_config *conf;
2140 QTAILQ_FOREACH(conf, &device_configs, next) {
2141 if (conf->type != type)
2143 rc = func(conf->cmdline);
2150 static int serial_parse(const char *devname)
2152 static int index = 0;
2155 if (strcmp(devname, "none") == 0)
2157 if (index == MAX_SERIAL_PORTS) {
2158 fprintf(stderr, "qemu: too many serial ports\n");
2161 snprintf(label, sizeof(label), "serial%d", index);
2162 serial_hds[index] = qemu_chr_open(label, devname, NULL);
2163 if (!serial_hds[index]) {
2164 fprintf(stderr, "qemu: could not open serial device '%s': %s\n",
2165 devname, strerror(errno));
2172 static int parallel_parse(const char *devname)
2174 static int index = 0;
2177 if (strcmp(devname, "none") == 0)
2179 if (index == MAX_PARALLEL_PORTS) {
2180 fprintf(stderr, "qemu: too many parallel ports\n");
2183 snprintf(label, sizeof(label), "parallel%d", index);
2184 parallel_hds[index] = qemu_chr_open(label, devname, NULL);
2185 if (!parallel_hds[index]) {
2186 fprintf(stderr, "qemu: could not open parallel device '%s': %s\n",
2187 devname, strerror(errno));
2194 static int virtcon_parse(const char *devname)
2196 static int index = 0;
2198 QemuOpts *bus_opts, *dev_opts;
2200 if (strcmp(devname, "none") == 0)
2202 if (index == MAX_VIRTIO_CONSOLES) {
2203 fprintf(stderr, "qemu: too many virtio consoles\n");
2207 bus_opts = qemu_opts_create(&qemu_device_opts, NULL, 0);
2208 qemu_opt_set(bus_opts, "driver", "virtio-serial");
2210 dev_opts = qemu_opts_create(&qemu_device_opts, NULL, 0);
2211 qemu_opt_set(dev_opts, "driver", "virtconsole");
2213 snprintf(label, sizeof(label), "virtcon%d", index);
2214 virtcon_hds[index] = qemu_chr_open(label, devname, NULL);
2215 if (!virtcon_hds[index]) {
2216 fprintf(stderr, "qemu: could not open virtio console '%s': %s\n",
2217 devname, strerror(errno));
2220 qemu_opt_set(dev_opts, "chardev", label);
2226 static int debugcon_parse(const char *devname)
2230 if (!qemu_chr_open("debugcon", devname, NULL)) {
2233 opts = qemu_opts_create(&qemu_device_opts, "debugcon", 1);
2235 fprintf(stderr, "qemu: already have a debugcon device\n");
2238 qemu_opt_set(opts, "driver", "isa-debugcon");
2239 qemu_opt_set(opts, "chardev", "debugcon");
2243 static const QEMUOption *lookup_opt(int argc, char **argv,
2244 const char **poptarg, int *poptind)
2246 const QEMUOption *popt;
2247 int optind = *poptind;
2248 char *r = argv[optind];
2251 loc_set_cmdline(argv, optind, 1);
2253 /* Treat --foo the same as -foo. */
2256 popt = qemu_options;
2259 error_report("invalid option");
2262 if (!strcmp(popt->name, r + 1))
2266 if (popt->flags & HAS_ARG) {
2267 if (optind >= argc) {
2268 error_report("requires an argument");
2271 optarg = argv[optind++];
2272 loc_set_cmdline(argv, optind - 2, 2);
2283 int main(int argc, char **argv, char **envp)
2285 const char *gdbstub_dev = NULL;
2287 int snapshot, linux_boot;
2288 const char *icount_option = NULL;
2289 const char *initrd_filename;
2290 const char *kernel_filename, *kernel_cmdline;
2291 char boot_devices[33] = "cad"; /* default to HD->floppy->CD-ROM */
2293 DisplayChangeListener *dcl;
2294 int cyls, heads, secs, translation;
2295 QemuOpts *hda_opts = NULL, *opts;
2298 const char *loadvm = NULL;
2299 QEMUMachine *machine;
2300 const char *cpu_model;
2302 const char *pid_file = NULL;
2303 const char *incoming = NULL;
2304 int show_vnc_port = 0;
2307 error_set_progname(argv[0]);
2311 qemu_cache_utils_init(envp);
2313 QLIST_INIT (&vm_change_state_head);
2314 os_setup_early_signal_handling();
2316 module_call_init(MODULE_INIT_MACHINE);
2317 machine = find_default_machine();
2319 initrd_filename = NULL;
2322 kernel_filename = NULL;
2323 kernel_cmdline = "";
2324 cyls = heads = secs = 0;
2325 translation = BIOS_ATA_TRANSLATION_AUTO;
2327 for (i = 0; i < MAX_NODES; i++) {
2329 node_cpumask[i] = 0;
2338 /* first pass of option parsing */
2340 while (optind < argc) {
2341 if (argv[optind][0] != '-') {
2346 const QEMUOption *popt;
2348 popt = lookup_opt(argc, argv, &optarg, &optind);
2349 switch (popt->index) {
2350 case QEMU_OPTION_nodefconfig:
2360 ret = qemu_read_config_file(CONFIG_QEMU_CONFDIR "/qemu.conf");
2361 if (ret < 0 && ret != -ENOENT) {
2365 ret = qemu_read_config_file(arch_config_name);
2366 if (ret < 0 && ret != -ENOENT) {
2372 /* second pass of option parsing */
2377 if (argv[optind][0] != '-') {
2378 hda_opts = drive_add(argv[optind++], HD_ALIAS, 0);
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 = find_machine(optarg);
2392 printf("Supported machines are:\n");
2393 for(m = first_machine; m != NULL; m = m->next) {
2395 printf("%-10s %s (alias of %s)\n",
2396 m->alias, m->desc, m->name);
2397 printf("%-10s %s%s\n",
2399 m->is_default ? " (default)" : "");
2401 exit(*optarg != '?');
2404 case QEMU_OPTION_cpu:
2405 /* hw initialization will check this */
2406 if (*optarg == '?') {
2407 list_cpus(stdout, &fprintf, optarg);
2413 case QEMU_OPTION_initrd:
2414 initrd_filename = optarg;
2416 case QEMU_OPTION_hda:
2418 hda_opts = drive_add(optarg, HD_ALIAS, 0);
2420 hda_opts = drive_add(optarg, HD_ALIAS
2421 ",cyls=%d,heads=%d,secs=%d%s",
2422 0, cyls, heads, secs,
2423 translation == BIOS_ATA_TRANSLATION_LBA ?
2425 translation == BIOS_ATA_TRANSLATION_NONE ?
2426 ",trans=none" : "");
2428 case QEMU_OPTION_hdb:
2429 case QEMU_OPTION_hdc:
2430 case QEMU_OPTION_hdd:
2431 drive_add(optarg, HD_ALIAS, popt->index - QEMU_OPTION_hda);
2433 case QEMU_OPTION_drive:
2434 drive_add(NULL, "%s", optarg);
2436 case QEMU_OPTION_set:
2437 if (qemu_set_option(optarg) != 0)
2440 case QEMU_OPTION_global:
2441 if (qemu_global_option(optarg) != 0)
2444 case QEMU_OPTION_mtdblock:
2445 drive_add(optarg, MTD_ALIAS);
2447 case QEMU_OPTION_sd:
2448 drive_add(optarg, SD_ALIAS);
2450 case QEMU_OPTION_pflash:
2451 drive_add(optarg, PFLASH_ALIAS);
2453 case QEMU_OPTION_snapshot:
2456 case QEMU_OPTION_hdachs:
2460 cyls = strtol(p, (char **)&p, 0);
2461 if (cyls < 1 || cyls > 16383)
2466 heads = strtol(p, (char **)&p, 0);
2467 if (heads < 1 || heads > 16)
2472 secs = strtol(p, (char **)&p, 0);
2473 if (secs < 1 || secs > 63)
2477 if (!strcmp(p, "none"))
2478 translation = BIOS_ATA_TRANSLATION_NONE;
2479 else if (!strcmp(p, "lba"))
2480 translation = BIOS_ATA_TRANSLATION_LBA;
2481 else if (!strcmp(p, "auto"))
2482 translation = BIOS_ATA_TRANSLATION_AUTO;
2485 } else if (*p != '\0') {
2487 fprintf(stderr, "qemu: invalid physical CHS format\n");
2490 if (hda_opts != NULL) {
2492 snprintf(num, sizeof(num), "%d", cyls);
2493 qemu_opt_set(hda_opts, "cyls", num);
2494 snprintf(num, sizeof(num), "%d", heads);
2495 qemu_opt_set(hda_opts, "heads", num);
2496 snprintf(num, sizeof(num), "%d", secs);
2497 qemu_opt_set(hda_opts, "secs", num);
2498 if (translation == BIOS_ATA_TRANSLATION_LBA)
2499 qemu_opt_set(hda_opts, "trans", "lba");
2500 if (translation == BIOS_ATA_TRANSLATION_NONE)
2501 qemu_opt_set(hda_opts, "trans", "none");
2505 case QEMU_OPTION_numa:
2506 if (nb_numa_nodes >= MAX_NODES) {
2507 fprintf(stderr, "qemu: too many NUMA nodes\n");
2512 case QEMU_OPTION_nographic:
2513 display_type = DT_NOGRAPHIC;
2515 #ifdef CONFIG_CURSES
2516 case QEMU_OPTION_curses:
2517 display_type = DT_CURSES;
2520 case QEMU_OPTION_portrait:
2523 case QEMU_OPTION_kernel:
2524 kernel_filename = optarg;
2526 case QEMU_OPTION_append:
2527 kernel_cmdline = optarg;
2529 case QEMU_OPTION_cdrom:
2530 drive_add(optarg, CDROM_ALIAS);
2532 case QEMU_OPTION_boot:
2534 static const char * const params[] = {
2535 "order", "once", "menu", NULL
2537 char buf[sizeof(boot_devices)];
2538 char *standard_boot_devices;
2541 if (!strchr(optarg, '=')) {
2543 pstrcpy(buf, sizeof(buf), optarg);
2544 } else if (check_params(buf, sizeof(buf), params, optarg) < 0) {
2546 "qemu: unknown boot parameter '%s' in '%s'\n",
2552 get_param_value(buf, sizeof(buf), "order", optarg)) {
2553 validate_bootdevices(buf);
2554 pstrcpy(boot_devices, sizeof(boot_devices), buf);
2557 if (get_param_value(buf, sizeof(buf),
2559 validate_bootdevices(buf);
2560 standard_boot_devices = qemu_strdup(boot_devices);
2561 pstrcpy(boot_devices, sizeof(boot_devices), buf);
2562 qemu_register_reset(restore_boot_devices,
2563 standard_boot_devices);
2565 if (get_param_value(buf, sizeof(buf),
2567 if (!strcmp(buf, "on")) {
2569 } else if (!strcmp(buf, "off")) {
2573 "qemu: invalid option value '%s'\n",
2581 case QEMU_OPTION_fda:
2582 case QEMU_OPTION_fdb:
2583 drive_add(optarg, FD_ALIAS, popt->index - QEMU_OPTION_fda);
2585 case QEMU_OPTION_no_fd_bootchk:
2588 case QEMU_OPTION_netdev:
2589 if (net_client_parse(&qemu_netdev_opts, optarg) == -1) {
2593 case QEMU_OPTION_net:
2594 if (net_client_parse(&qemu_net_opts, optarg) == -1) {
2599 case QEMU_OPTION_tftp:
2600 legacy_tftp_prefix = optarg;
2602 case QEMU_OPTION_bootp:
2603 legacy_bootp_filename = optarg;
2605 case QEMU_OPTION_redir:
2606 if (net_slirp_redir(optarg) < 0)
2610 case QEMU_OPTION_bt:
2611 add_device_config(DEV_BT, optarg);
2613 case QEMU_OPTION_audio_help:
2614 if (!(audio_available())) {
2615 printf("Option %s not supported for this target\n", popt->name);
2621 case QEMU_OPTION_soundhw:
2622 if (!(audio_available())) {
2623 printf("Option %s not supported for this target\n", popt->name);
2626 select_soundhw (optarg);
2631 case QEMU_OPTION_version:
2635 case QEMU_OPTION_m: {
2639 value = strtoul(optarg, &ptr, 10);
2641 case 0: case 'M': case 'm':
2648 fprintf(stderr, "qemu: invalid ram size: %s\n", optarg);
2652 /* On 32-bit hosts, QEMU is limited by virtual address space */
2653 if (value > (2047 << 20) && HOST_LONG_BITS == 32) {
2654 fprintf(stderr, "qemu: at most 2047 MB RAM can be simulated\n");
2657 if (value != (uint64_t)(ram_addr_t)value) {
2658 fprintf(stderr, "qemu: ram size too large\n");
2664 case QEMU_OPTION_mempath:
2668 case QEMU_OPTION_mem_prealloc:
2673 set_cpu_log(optarg);
2676 gdbstub_dev = "tcp::" DEFAULT_GDBSTUB_PORT;
2678 case QEMU_OPTION_gdb:
2679 gdbstub_dev = optarg;
2684 case QEMU_OPTION_bios:
2687 case QEMU_OPTION_singlestep:
2694 keyboard_layout = optarg;
2696 case QEMU_OPTION_localtime:
2699 case QEMU_OPTION_vga:
2700 select_vgahw (optarg);
2707 w = strtol(p, (char **)&p, 10);
2710 fprintf(stderr, "qemu: invalid resolution or depth\n");
2716 h = strtol(p, (char **)&p, 10);
2721 depth = strtol(p, (char **)&p, 10);
2722 if (depth != 8 && depth != 15 && depth != 16 &&
2723 depth != 24 && depth != 32)
2725 } else if (*p == '\0') {
2726 depth = graphic_depth;
2733 graphic_depth = depth;
2736 case QEMU_OPTION_echr:
2739 term_escape_char = strtol(optarg, &r, 0);
2741 printf("Bad argument to echr\n");
2744 case QEMU_OPTION_monitor:
2745 monitor_parse(optarg, "readline");
2746 default_monitor = 0;
2748 case QEMU_OPTION_qmp:
2749 monitor_parse(optarg, "control");
2750 default_monitor = 0;
2752 case QEMU_OPTION_mon:
2753 opts = qemu_opts_parse(&qemu_mon_opts, optarg, 1);
2757 default_monitor = 0;
2759 case QEMU_OPTION_chardev:
2760 opts = qemu_opts_parse(&qemu_chardev_opts, optarg, 1);
2766 case QEMU_OPTION_fsdev:
2767 opts = qemu_opts_parse(&qemu_fsdev_opts, optarg, 1);
2769 fprintf(stderr, "parse error: %s\n", optarg);
2773 case QEMU_OPTION_virtfs: {
2774 char *arg_fsdev = NULL;
2775 char *arg_9p = NULL;
2778 opts = qemu_opts_parse(&qemu_virtfs_opts, optarg, 1);
2780 fprintf(stderr, "parse error: %s\n", optarg);
2784 len = strlen(",id=,path=");
2785 len += strlen(qemu_opt_get(opts, "fstype"));
2786 len += strlen(qemu_opt_get(opts, "mount_tag"));
2787 len += strlen(qemu_opt_get(opts, "path"));
2788 arg_fsdev = qemu_malloc((len + 1) * sizeof(*arg_fsdev));
2791 fprintf(stderr, "No memory to parse -fsdev for %s\n",
2796 sprintf(arg_fsdev, "%s,id=%s,path=%s",
2797 qemu_opt_get(opts, "fstype"),
2798 qemu_opt_get(opts, "mount_tag"),
2799 qemu_opt_get(opts, "path"));
2801 len = strlen("virtio-9p-pci,fsdev=,mount_tag=");
2802 len += 2*strlen(qemu_opt_get(opts, "mount_tag"));
2803 arg_9p = qemu_malloc((len + 1) * sizeof(*arg_9p));
2806 fprintf(stderr, "No memory to parse -device for %s\n",
2811 sprintf(arg_9p, "virtio-9p-pci,fsdev=%s,mount_tag=%s",
2812 qemu_opt_get(opts, "mount_tag"),
2813 qemu_opt_get(opts, "mount_tag"));
2815 if (!qemu_opts_parse(&qemu_fsdev_opts, arg_fsdev, 1)) {
2816 fprintf(stderr, "parse error [fsdev]: %s\n", optarg);
2820 if (!qemu_opts_parse(&qemu_device_opts, arg_9p, 1)) {
2821 fprintf(stderr, "parse error [device]: %s\n", optarg);
2825 qemu_free(arg_fsdev);
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_pidfile:
2894 case QEMU_OPTION_win2k_hack:
2895 win2k_install_hack = 1;
2897 case QEMU_OPTION_rtc_td_hack:
2900 case QEMU_OPTION_acpitable:
2901 do_acpitable_option(optarg);
2903 case QEMU_OPTION_smbios:
2904 do_smbios_option(optarg);
2906 case QEMU_OPTION_enable_kvm:
2909 case QEMU_OPTION_usb:
2912 case QEMU_OPTION_usbdevice:
2914 add_device_config(DEV_USB, optarg);
2916 case QEMU_OPTION_device:
2917 if (!qemu_opts_parse(&qemu_device_opts, optarg, 1)) {
2921 case QEMU_OPTION_smp:
2924 fprintf(stderr, "Invalid number of CPUs\n");
2927 if (max_cpus < smp_cpus) {
2928 fprintf(stderr, "maxcpus must be equal to or greater than "
2932 if (max_cpus > 255) {
2933 fprintf(stderr, "Unsupported number of maxcpus\n");
2937 case QEMU_OPTION_vnc:
2938 display_type = DT_VNC;
2939 vnc_display = optarg;
2941 case QEMU_OPTION_no_acpi:
2944 case QEMU_OPTION_no_hpet:
2947 case QEMU_OPTION_balloon:
2948 if (balloon_parse(optarg) < 0) {
2949 fprintf(stderr, "Unknown -balloon argument %s\n", optarg);
2953 case QEMU_OPTION_no_reboot:
2956 case QEMU_OPTION_no_shutdown:
2959 case QEMU_OPTION_show_cursor:
2962 case QEMU_OPTION_uuid:
2963 if(qemu_uuid_parse(optarg, qemu_uuid) < 0) {
2964 fprintf(stderr, "Fail to parse UUID string."
2965 " Wrong format.\n");
2969 case QEMU_OPTION_option_rom:
2970 if (nb_option_roms >= MAX_OPTION_ROMS) {
2971 fprintf(stderr, "Too many option ROMs\n");
2974 option_rom[nb_option_roms] = optarg;
2977 case QEMU_OPTION_semihosting:
2978 semihosting_enabled = 1;
2980 case QEMU_OPTION_name:
2981 qemu_name = qemu_strdup(optarg);
2983 char *p = strchr(qemu_name, ',');
2986 if (strncmp(p, "process=", 8)) {
2987 fprintf(stderr, "Unknown subargument %s to -name", p);
2995 case QEMU_OPTION_prom_env:
2996 if (nb_prom_envs >= MAX_PROM_ENVS) {
2997 fprintf(stderr, "Too many prom variables\n");
3000 prom_envs[nb_prom_envs] = optarg;
3003 case QEMU_OPTION_old_param:
3006 case QEMU_OPTION_clock:
3007 configure_alarms(optarg);
3009 case QEMU_OPTION_startdate:
3010 configure_rtc_date_offset(optarg, 1);
3012 case QEMU_OPTION_rtc:
3013 opts = qemu_opts_parse(&qemu_rtc_opts, optarg, 0);
3017 configure_rtc(opts);
3019 case QEMU_OPTION_tb_size:
3020 tb_size = strtol(optarg, NULL, 0);
3024 case QEMU_OPTION_icount:
3025 icount_option = optarg;
3027 case QEMU_OPTION_incoming:
3030 case QEMU_OPTION_nodefaults:
3032 default_parallel = 0;
3033 default_virtcon = 0;
3034 default_monitor = 0;
3041 case QEMU_OPTION_xen_domid:
3042 if (!(xen_available())) {
3043 printf("Option %s not supported for this target\n", popt->name);
3046 xen_domid = atoi(optarg);
3048 case QEMU_OPTION_xen_create:
3049 if (!(xen_available())) {
3050 printf("Option %s not supported for this target\n", popt->name);
3053 xen_mode = XEN_CREATE;
3055 case QEMU_OPTION_xen_attach:
3056 if (!(xen_available())) {
3057 printf("Option %s not supported for this target\n", popt->name);
3060 xen_mode = XEN_ATTACH;
3062 case QEMU_OPTION_readconfig:
3064 int ret = qemu_read_config_file(optarg);
3066 fprintf(stderr, "read config %s: %s\n", optarg,
3072 case QEMU_OPTION_writeconfig:
3075 if (strcmp(optarg, "-") == 0) {
3078 fp = fopen(optarg, "w");
3080 fprintf(stderr, "open %s: %s\n", optarg, strerror(errno));
3084 qemu_config_write(fp);
3089 os_parse_cmd_args(popt->index, optarg);
3095 /* If no data_dir is specified then try to find it relative to the
3098 data_dir = os_find_datadir(argv[0]);
3100 /* If all else fails use the install patch specified when building. */
3102 data_dir = CONFIG_QEMU_DATADIR;
3106 * Default to max_cpus = smp_cpus, in case the user doesn't
3107 * specify a max_cpus value.
3110 max_cpus = smp_cpus;
3112 machine->max_cpus = machine->max_cpus ?: 1; /* Default to UP */
3113 if (smp_cpus > machine->max_cpus) {
3114 fprintf(stderr, "Number of SMP cpus requested (%d), exceeds max cpus "
3115 "supported by machine `%s' (%d)\n", smp_cpus, machine->name,
3120 qemu_opts_foreach(&qemu_device_opts, default_driver_check, NULL, 0);
3121 qemu_opts_foreach(&qemu_global_opts, default_driver_check, NULL, 0);
3123 if (machine->no_serial) {
3126 if (machine->no_parallel) {
3127 default_parallel = 0;
3129 if (!machine->use_virtcon) {
3130 default_virtcon = 0;
3132 if (machine->no_vga) {
3135 if (machine->no_floppy) {
3138 if (machine->no_cdrom) {
3141 if (machine->no_sdcard) {
3145 if (display_type == DT_NOGRAPHIC) {
3146 if (default_parallel)
3147 add_device_config(DEV_PARALLEL, "null");
3148 if (default_serial && default_monitor) {
3149 add_device_config(DEV_SERIAL, "mon:stdio");
3150 } else if (default_virtcon && default_monitor) {
3151 add_device_config(DEV_VIRTCON, "mon:stdio");
3154 add_device_config(DEV_SERIAL, "stdio");
3155 if (default_virtcon)
3156 add_device_config(DEV_VIRTCON, "stdio");
3157 if (default_monitor)
3158 monitor_parse("stdio", "readline");
3162 add_device_config(DEV_SERIAL, "vc:80Cx24C");
3163 if (default_parallel)
3164 add_device_config(DEV_PARALLEL, "vc:80Cx24C");
3165 if (default_monitor)
3166 monitor_parse("vc:80Cx24C", "readline");
3167 if (default_virtcon)
3168 add_device_config(DEV_VIRTCON, "vc:80Cx24C");
3171 vga_interface_type = VGA_CIRRUS;
3175 if (qemu_opts_foreach(&qemu_chardev_opts, chardev_init_func, NULL, 1) != 0)
3178 if (qemu_opts_foreach(&qemu_fsdev_opts, fsdev_init_func, NULL, 1) != 0) {
3185 if (pid_file && qemu_create_pidfile(pid_file) != 0) {
3191 int ret = kvm_init(smp_cpus);
3193 if (!kvm_available()) {
3194 printf("KVM not supported for this target\n");
3196 fprintf(stderr, "failed to initialize KVM: %s\n", strerror(-ret));
3202 if (qemu_init_main_loop()) {
3203 fprintf(stderr, "qemu_init_main_loop failed\n");
3206 linux_boot = (kernel_filename != NULL);
3208 if (!linux_boot && *kernel_cmdline != '\0') {
3209 fprintf(stderr, "-append only allowed with -kernel option\n");
3213 if (!linux_boot && initrd_filename != NULL) {
3214 fprintf(stderr, "-initrd only allowed with -kernel option\n");
3219 /* Win32 doesn't support line-buffering and requires size >= 2 */
3220 setvbuf(stdout, NULL, _IOLBF, 0);
3223 if (init_timer_alarm() < 0) {
3224 fprintf(stderr, "could not initialize alarm timer\n");
3227 configure_icount(icount_option);
3229 if (net_init_clients() < 0) {
3233 /* init the bluetooth world */
3234 if (foreach_device_config(DEV_BT, bt_parse))
3237 /* init the memory */
3239 ram_size = DEFAULT_RAM_SIZE * 1024 * 1024;
3241 /* init the dynamic translator */
3242 cpu_exec_init_all(tb_size * 1024 * 1024);
3244 bdrv_init_with_whitelist();
3248 if (default_cdrom) {
3249 /* we always create the cdrom drive, even if no disk is there */
3250 drive_add(NULL, CDROM_ALIAS);
3253 if (default_floppy) {
3254 /* we always create at least one floppy */
3255 drive_add(NULL, FD_ALIAS, 0);
3258 if (default_sdcard) {
3259 /* we always create one sd slot, even if no card is in it */
3260 drive_add(NULL, SD_ALIAS);
3263 /* open the virtual block devices */
3265 qemu_opts_foreach(&qemu_drive_opts, drive_enable_snapshot, NULL, 0);
3266 if (qemu_opts_foreach(&qemu_drive_opts, drive_init_func, machine, 1) != 0)
3269 register_savevm_live("ram", 0, 3, NULL, ram_save_live, NULL,
3272 if (nb_numa_nodes > 0) {
3275 if (nb_numa_nodes > smp_cpus) {
3276 nb_numa_nodes = smp_cpus;
3279 /* If no memory size if given for any node, assume the default case
3280 * and distribute the available memory equally across all nodes
3282 for (i = 0; i < nb_numa_nodes; i++) {
3283 if (node_mem[i] != 0)
3286 if (i == nb_numa_nodes) {
3287 uint64_t usedmem = 0;
3289 /* On Linux, the each node's border has to be 8MB aligned,
3290 * the final node gets the rest.
3292 for (i = 0; i < nb_numa_nodes - 1; i++) {
3293 node_mem[i] = (ram_size / nb_numa_nodes) & ~((1 << 23UL) - 1);
3294 usedmem += node_mem[i];
3296 node_mem[i] = ram_size - usedmem;
3299 for (i = 0; i < nb_numa_nodes; i++) {
3300 if (node_cpumask[i] != 0)
3303 /* assigning the VCPUs round-robin is easier to implement, guest OSes
3304 * must cope with this anyway, because there are BIOSes out there in
3305 * real machines which also use this scheme.
3307 if (i == nb_numa_nodes) {
3308 for (i = 0; i < smp_cpus; i++) {
3309 node_cpumask[i % nb_numa_nodes] |= 1 << i;
3314 if (qemu_opts_foreach(&qemu_mon_opts, mon_init_func, NULL, 1) != 0) {
3318 if (foreach_device_config(DEV_SERIAL, serial_parse) < 0)
3320 if (foreach_device_config(DEV_PARALLEL, parallel_parse) < 0)
3322 if (foreach_device_config(DEV_VIRTCON, virtcon_parse) < 0)
3324 if (foreach_device_config(DEV_DEBUGCON, debugcon_parse) < 0)
3327 module_call_init(MODULE_INIT_DEVICE);
3329 if (qemu_opts_foreach(&qemu_device_opts, device_help_func, NULL, 0) != 0)
3333 i = select_watchdog(watchdog);
3335 exit (i == 1 ? 1 : 0);
3338 if (machine->compat_props) {
3339 qdev_prop_register_global_list(machine->compat_props);
3343 machine->init(ram_size, boot_devices,
3344 kernel_filename, kernel_cmdline, initrd_filename, cpu_model);
3346 cpu_synchronize_all_post_init();
3348 /* must be after terminal init, SDL library changes signal handlers */
3349 os_setup_signal_handling();
3353 current_machine = machine;
3355 /* init USB devices */
3357 if (foreach_device_config(DEV_USB, usb_parse) < 0)
3361 /* init generic devices */
3362 if (qemu_opts_foreach(&qemu_device_opts, device_init_func, NULL, 1) != 0)
3365 net_check_clients();
3367 /* just use the first displaystate for the moment */
3368 ds = get_displaystate();
3370 if (display_type == DT_DEFAULT) {
3371 #if defined(CONFIG_SDL) || defined(CONFIG_COCOA)
3372 display_type = DT_SDL;
3374 display_type = DT_VNC;
3375 vnc_display = "localhost:0,to=99";
3381 switch (display_type) {
3384 #if defined(CONFIG_CURSES)
3386 curses_display_init(ds, full_screen);
3389 #if defined(CONFIG_SDL)
3391 sdl_display_init(ds, full_screen, no_frame);
3393 #elif defined(CONFIG_COCOA)
3395 cocoa_display_init(ds, full_screen);
3399 vnc_display_init(ds);
3400 if (vnc_display_open(ds, vnc_display) < 0)
3403 if (show_vnc_port) {
3404 printf("VNC server running on `%s'\n", vnc_display_local_addr(ds));
3412 dcl = ds->listeners;
3413 while (dcl != NULL) {
3414 if (dcl->dpy_refresh != NULL) {
3415 ds->gui_timer = qemu_new_timer(rt_clock, gui_update, ds);
3416 qemu_mod_timer(ds->gui_timer, qemu_get_clock(rt_clock));
3422 if (display_type == DT_NOGRAPHIC || display_type == DT_VNC) {
3423 nographic_timer = qemu_new_timer(rt_clock, nographic_update, NULL);
3424 qemu_mod_timer(nographic_timer, qemu_get_clock(rt_clock));
3427 text_consoles_set_display(ds);
3429 if (gdbstub_dev && gdbserver_start(gdbstub_dev) < 0) {
3430 fprintf(stderr, "qemu: could not open gdbserver on device '%s'\n",
3435 qdev_machine_creation_done();
3437 if (rom_load_all() != 0) {
3438 fprintf(stderr, "rom loading failed\n");
3442 qemu_system_reset();
3444 if (load_vmstate(loadvm) < 0) {
3450 qemu_start_incoming_migration(incoming);
3451 } else if (autostart) {