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>
50 #ifdef CONFIG_SIMPLE_TRACE
56 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__DragonFly__)
58 #include <sys/sysctl.h>
67 #include <linux/ppdev.h>
68 #include <linux/parport.h>
72 #include <sys/ethernet.h>
73 #include <sys/sockio.h>
74 #include <netinet/arp.h>
75 #include <netinet/in_systm.h>
76 #include <netinet/ip.h>
77 #include <netinet/ip_icmp.h> // must come after ip.h
78 #include <netinet/udp.h>
79 #include <netinet/tcp.h>
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 "blockdev.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"
162 #include "qemu-queue.h"
164 #include "arch_init.h"
166 #include "ui/qemu-spice.h"
169 //#define DEBUG_SLIRP
171 #define DEFAULT_RAM_SIZE 128
173 #define MAX_VIRTIO_CONSOLES 1
175 static const char *data_dir;
176 const char *bios_name = NULL;
177 enum vga_retrace_method vga_retrace_method = VGA_RETRACE_DUMB;
178 DisplayType display_type = DT_DEFAULT;
179 int display_remote = 0;
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 int incoming_expected; /* Started with -incoming and waiting for incoming */
191 static int rtc_utc = 1;
192 static int rtc_date_offset = -1; /* -1 means no change */
193 QEMUClock *rtc_clock;
194 int vga_interface_type = VGA_NONE;
195 static int full_screen = 0;
197 static int no_frame = 0;
200 CharDriverState *serial_hds[MAX_SERIAL_PORTS];
201 CharDriverState *parallel_hds[MAX_PARALLEL_PORTS];
202 CharDriverState *virtcon_hds[MAX_VIRTIO_CONSOLES];
203 int win2k_install_hack = 0;
211 const char *vnc_display;
212 int acpi_enabled = 1;
218 int graphic_rotate = 0;
219 uint8_t irq0override = 1;
220 const char *watchdog;
221 QEMUOptionRom option_rom[MAX_OPTION_ROMS];
223 int semihosting_enabled = 0;
225 const char *qemu_name;
228 unsigned int nb_prom_envs = 0;
229 const char *prom_envs[MAX_PROM_ENVS];
232 typedef struct FWBootEntry FWBootEntry;
235 QTAILQ_ENTRY(FWBootEntry) link;
241 QTAILQ_HEAD(, FWBootEntry) fw_boot_order = QTAILQ_HEAD_INITIALIZER(fw_boot_order);
244 uint64_t node_mem[MAX_NODES];
245 uint64_t node_cpumask[MAX_NODES];
247 static QEMUTimer *nographic_timer;
249 uint8_t qemu_uuid[16];
251 static QEMUBootSetHandler *boot_set_handler;
252 static void *boot_set_opaque;
254 static NotifierList exit_notifiers =
255 NOTIFIER_LIST_INITIALIZER(exit_notifiers);
259 enum xen_mode xen_mode = XEN_EMULATE;
261 static int default_serial = 1;
262 static int default_parallel = 1;
263 static int default_virtcon = 1;
264 static int default_monitor = 1;
265 static int default_vga = 1;
266 static int default_floppy = 1;
267 static int default_cdrom = 1;
268 static int default_sdcard = 1;
274 { .driver = "isa-serial", .flag = &default_serial },
275 { .driver = "isa-parallel", .flag = &default_parallel },
276 { .driver = "isa-fdc", .flag = &default_floppy },
277 { .driver = "ide-drive", .flag = &default_cdrom },
278 { .driver = "virtio-serial-pci", .flag = &default_virtcon },
279 { .driver = "virtio-serial-s390", .flag = &default_virtcon },
280 { .driver = "virtio-serial", .flag = &default_virtcon },
281 { .driver = "VGA", .flag = &default_vga },
282 { .driver = "cirrus-vga", .flag = &default_vga },
283 { .driver = "vmware-svga", .flag = &default_vga },
286 static int default_driver_check(QemuOpts *opts, void *opaque)
288 const char *driver = qemu_opt_get(opts, "driver");
293 for (i = 0; i < ARRAY_SIZE(default_list); i++) {
294 if (strcmp(default_list[i].driver, driver) != 0)
296 *(default_list[i].flag) = 0;
301 /***********************************************************/
302 /* real time host monotonic timer */
304 /***********************************************************/
305 /* host time/date access */
306 void qemu_get_timedate(struct tm *tm, int offset)
313 if (rtc_date_offset == -1) {
317 ret = localtime(&ti);
319 ti -= rtc_date_offset;
323 memcpy(tm, ret, sizeof(struct tm));
326 int qemu_timedate_diff(struct tm *tm)
330 if (rtc_date_offset == -1)
332 seconds = mktimegm(tm);
334 seconds = mktime(tm);
336 seconds = mktimegm(tm) + rtc_date_offset;
338 return seconds - time(NULL);
341 void rtc_change_mon_event(struct tm *tm)
345 data = qobject_from_jsonf("{ 'offset': %d }", qemu_timedate_diff(tm));
346 monitor_protocol_event(QEVENT_RTC_CHANGE, data);
347 qobject_decref(data);
350 static void configure_rtc_date_offset(const char *startdate, int legacy)
352 time_t rtc_start_date;
355 if (!strcmp(startdate, "now") && legacy) {
356 rtc_date_offset = -1;
358 if (sscanf(startdate, "%d-%d-%dT%d:%d:%d",
366 } else if (sscanf(startdate, "%d-%d-%d",
378 rtc_start_date = mktimegm(&tm);
379 if (rtc_start_date == -1) {
381 fprintf(stderr, "Invalid date format. Valid formats are:\n"
382 "'2006-06-17T16:01:21' or '2006-06-17'\n");
385 rtc_date_offset = time(NULL) - rtc_start_date;
389 static void configure_rtc(QemuOpts *opts)
393 value = qemu_opt_get(opts, "base");
395 if (!strcmp(value, "utc")) {
397 } else if (!strcmp(value, "localtime")) {
400 configure_rtc_date_offset(value, 0);
403 value = qemu_opt_get(opts, "clock");
405 if (!strcmp(value, "host")) {
406 rtc_clock = host_clock;
407 } else if (!strcmp(value, "vm")) {
408 rtc_clock = vm_clock;
410 fprintf(stderr, "qemu: invalid option value '%s'\n", value);
414 value = qemu_opt_get(opts, "driftfix");
416 if (!strcmp(value, "slew")) {
418 } else if (!strcmp(value, "none")) {
421 fprintf(stderr, "qemu: invalid option value '%s'\n", value);
427 /***********************************************************/
428 /* Bluetooth support */
431 static struct HCIInfo *hci_table[MAX_NICS];
433 static struct bt_vlan_s {
434 struct bt_scatternet_s net;
436 struct bt_vlan_s *next;
439 /* find or alloc a new bluetooth "VLAN" */
440 static struct bt_scatternet_s *qemu_find_bt_vlan(int id)
442 struct bt_vlan_s **pvlan, *vlan;
443 for (vlan = first_bt_vlan; vlan != NULL; vlan = vlan->next) {
447 vlan = qemu_mallocz(sizeof(struct bt_vlan_s));
449 pvlan = &first_bt_vlan;
450 while (*pvlan != NULL)
451 pvlan = &(*pvlan)->next;
456 static void null_hci_send(struct HCIInfo *hci, const uint8_t *data, int len)
460 static int null_hci_addr_set(struct HCIInfo *hci, const uint8_t *bd_addr)
465 static struct HCIInfo null_hci = {
466 .cmd_send = null_hci_send,
467 .sco_send = null_hci_send,
468 .acl_send = null_hci_send,
469 .bdaddr_set = null_hci_addr_set,
472 struct HCIInfo *qemu_next_hci(void)
474 if (cur_hci == nb_hcis)
477 return hci_table[cur_hci++];
480 static struct HCIInfo *hci_init(const char *str)
483 struct bt_scatternet_s *vlan = 0;
485 if (!strcmp(str, "null"))
488 else if (!strncmp(str, "host", 4) && (str[4] == '\0' || str[4] == ':'))
490 return bt_host_hci(str[4] ? str + 5 : "hci0");
491 else if (!strncmp(str, "hci", 3)) {
494 if (!strncmp(str + 3, ",vlan=", 6)) {
495 vlan = qemu_find_bt_vlan(strtol(str + 9, &endp, 0));
500 vlan = qemu_find_bt_vlan(0);
502 return bt_new_hci(vlan);
505 fprintf(stderr, "qemu: Unknown bluetooth HCI `%s'.\n", str);
510 static int bt_hci_parse(const char *str)
515 if (nb_hcis >= MAX_NICS) {
516 fprintf(stderr, "qemu: Too many bluetooth HCIs (max %i).\n", MAX_NICS);
529 bdaddr.b[5] = 0x56 + nb_hcis;
530 hci->bdaddr_set(hci, bdaddr.b);
532 hci_table[nb_hcis++] = hci;
537 static void bt_vhci_add(int vlan_id)
539 struct bt_scatternet_s *vlan = qemu_find_bt_vlan(vlan_id);
542 fprintf(stderr, "qemu: warning: adding a VHCI to "
543 "an empty scatternet %i\n", vlan_id);
545 bt_vhci_init(bt_new_hci(vlan));
548 static struct bt_device_s *bt_device_add(const char *opt)
550 struct bt_scatternet_s *vlan;
552 char *endp = strstr(opt, ",vlan=");
553 int len = (endp ? endp - opt : strlen(opt)) + 1;
556 pstrcpy(devname, MIN(sizeof(devname), len), opt);
559 vlan_id = strtol(endp + 6, &endp, 0);
561 fprintf(stderr, "qemu: unrecognised bluetooth vlan Id\n");
566 vlan = qemu_find_bt_vlan(vlan_id);
569 fprintf(stderr, "qemu: warning: adding a slave device to "
570 "an empty scatternet %i\n", vlan_id);
572 if (!strcmp(devname, "keyboard"))
573 return bt_keyboard_init(vlan);
575 fprintf(stderr, "qemu: unsupported bluetooth device `%s'\n", devname);
579 static int bt_parse(const char *opt)
581 const char *endp, *p;
584 if (strstart(opt, "hci", &endp)) {
585 if (!*endp || *endp == ',') {
587 if (!strstart(endp, ",vlan=", 0))
590 return bt_hci_parse(opt);
592 } else if (strstart(opt, "vhci", &endp)) {
593 if (!*endp || *endp == ',') {
595 if (strstart(endp, ",vlan=", &p)) {
596 vlan = strtol(p, (char **) &endp, 0);
598 fprintf(stderr, "qemu: bad scatternet '%s'\n", p);
602 fprintf(stderr, "qemu: bad parameter '%s'\n", endp + 1);
611 } else if (strstart(opt, "device:", &endp))
612 return !bt_device_add(endp);
614 fprintf(stderr, "qemu: bad bluetooth parameter '%s'\n", opt);
618 /***********************************************************/
619 /* QEMU Block devices */
621 #define HD_ALIAS "index=%d,media=disk"
622 #define CDROM_ALIAS "index=2,media=cdrom"
623 #define FD_ALIAS "index=%d,if=floppy"
624 #define PFLASH_ALIAS "if=pflash"
625 #define MTD_ALIAS "if=mtd"
626 #define SD_ALIAS "index=0,if=sd"
628 static int drive_init_func(QemuOpts *opts, void *opaque)
630 int *use_scsi = opaque;
633 if (drive_init(opts, *use_scsi, &fatal_error) == NULL) {
640 static int drive_enable_snapshot(QemuOpts *opts, void *opaque)
642 if (NULL == qemu_opt_get(opts, "snapshot")) {
643 qemu_opt_set(opts, "snapshot", "on");
648 void qemu_register_boot_set(QEMUBootSetHandler *func, void *opaque)
650 boot_set_handler = func;
651 boot_set_opaque = opaque;
654 int qemu_boot_set(const char *boot_devices)
656 if (!boot_set_handler) {
659 return boot_set_handler(boot_set_opaque, boot_devices);
662 static void validate_bootdevices(char *devices)
664 /* We just do some generic consistency checks */
668 for (p = devices; *p != '\0'; p++) {
669 /* Allowed boot devices are:
670 * a-b: floppy disk drives
671 * c-f: IDE disk drives
672 * g-m: machine implementation dependant drives
673 * n-p: network devices
674 * It's up to each machine implementation to check if the given boot
675 * devices match the actual hardware implementation and firmware
678 if (*p < 'a' || *p > 'p') {
679 fprintf(stderr, "Invalid boot device '%c'\n", *p);
682 if (bitmap & (1 << (*p - 'a'))) {
683 fprintf(stderr, "Boot device '%c' was given twice\n", *p);
686 bitmap |= 1 << (*p - 'a');
690 static void restore_boot_devices(void *opaque)
692 char *standard_boot_devices = opaque;
693 static int first = 1;
695 /* Restore boot order and remove ourselves after the first boot */
701 qemu_boot_set(standard_boot_devices);
703 qemu_unregister_reset(restore_boot_devices, standard_boot_devices);
704 qemu_free(standard_boot_devices);
707 void add_boot_device_path(int32_t bootindex, DeviceState *dev,
710 FWBootEntry *node, *i;
716 assert(dev != NULL || suffix != NULL);
718 node = qemu_mallocz(sizeof(FWBootEntry));
719 node->bootindex = bootindex;
720 node->suffix = strdup(suffix);
723 QTAILQ_FOREACH(i, &fw_boot_order, link) {
724 if (i->bootindex == bootindex) {
725 fprintf(stderr, "Two devices with same boot index %d\n", bootindex);
727 } else if (i->bootindex < bootindex) {
730 QTAILQ_INSERT_BEFORE(i, node, link);
733 QTAILQ_INSERT_TAIL(&fw_boot_order, node, link);
736 static void numa_add(const char *optarg)
740 unsigned long long value, endvalue;
743 optarg = get_opt_name(option, 128, optarg, ',') + 1;
744 if (!strcmp(option, "node")) {
745 if (get_param_value(option, 128, "nodeid", optarg) == 0) {
746 nodenr = nb_numa_nodes;
748 nodenr = strtoull(option, NULL, 10);
751 if (get_param_value(option, 128, "mem", optarg) == 0) {
752 node_mem[nodenr] = 0;
755 sval = strtosz(option, NULL);
757 fprintf(stderr, "qemu: invalid numa mem size: %s\n", optarg);
760 node_mem[nodenr] = sval;
762 if (get_param_value(option, 128, "cpus", optarg) == 0) {
763 node_cpumask[nodenr] = 0;
765 value = strtoull(option, &endptr, 10);
768 fprintf(stderr, "only 64 CPUs in NUMA mode supported.\n");
770 if (*endptr == '-') {
771 endvalue = strtoull(endptr+1, &endptr, 10);
772 if (endvalue >= 63) {
775 "only 63 CPUs in NUMA mode supported.\n");
777 value = (2ULL << endvalue) - (1ULL << value);
779 value = 1ULL << value;
782 node_cpumask[nodenr] = value;
789 static void smp_parse(const char *optarg)
791 int smp, sockets = 0, threads = 0, cores = 0;
795 smp = strtoul(optarg, &endptr, 10);
796 if (endptr != optarg) {
797 if (*endptr == ',') {
801 if (get_param_value(option, 128, "sockets", endptr) != 0)
802 sockets = strtoull(option, NULL, 10);
803 if (get_param_value(option, 128, "cores", endptr) != 0)
804 cores = strtoull(option, NULL, 10);
805 if (get_param_value(option, 128, "threads", endptr) != 0)
806 threads = strtoull(option, NULL, 10);
807 if (get_param_value(option, 128, "maxcpus", endptr) != 0)
808 max_cpus = strtoull(option, NULL, 10);
810 /* compute missing values, prefer sockets over cores over threads */
811 if (smp == 0 || sockets == 0) {
812 sockets = sockets > 0 ? sockets : 1;
813 cores = cores > 0 ? cores : 1;
814 threads = threads > 0 ? threads : 1;
816 smp = cores * threads * sockets;
820 threads = threads > 0 ? threads : 1;
821 cores = smp / (sockets * threads);
823 threads = smp / (cores * sockets);
827 smp_cores = cores > 0 ? cores : 1;
828 smp_threads = threads > 0 ? threads : 1;
833 /***********************************************************/
836 static int usb_device_add(const char *devname)
839 USBDevice *dev = NULL;
844 /* drivers with .usbdevice_name entry in USBDeviceInfo */
845 dev = usbdevice_create(devname);
850 if (strstart(devname, "host:", &p)) {
851 dev = usb_host_device_open(p);
852 } else if (!strcmp(devname, "bt") || strstart(devname, "bt:", &p)) {
853 dev = usb_bt_init(devname[2] ? hci_init(p) :
854 bt_new_hci(qemu_find_bt_vlan(0)));
865 static int usb_device_del(const char *devname)
870 if (strstart(devname, "host:", &p))
871 return usb_host_device_close(p);
876 p = strchr(devname, '.');
879 bus_num = strtoul(devname, NULL, 0);
880 addr = strtoul(p + 1, NULL, 0);
882 return usb_device_delete_addr(bus_num, addr);
885 static int usb_parse(const char *cmdline)
888 r = usb_device_add(cmdline);
890 fprintf(stderr, "qemu: could not add USB device '%s'\n", cmdline);
895 void do_usb_add(Monitor *mon, const QDict *qdict)
897 const char *devname = qdict_get_str(qdict, "devname");
898 if (usb_device_add(devname) < 0) {
899 error_report("could not add USB device '%s'", devname);
903 void do_usb_del(Monitor *mon, const QDict *qdict)
905 const char *devname = qdict_get_str(qdict, "devname");
906 if (usb_device_del(devname) < 0) {
907 error_report("could not delete USB device '%s'", devname);
911 /***********************************************************/
914 static struct pcmcia_socket_entry_s {
915 PCMCIASocket *socket;
916 struct pcmcia_socket_entry_s *next;
917 } *pcmcia_sockets = 0;
919 void pcmcia_socket_register(PCMCIASocket *socket)
921 struct pcmcia_socket_entry_s *entry;
923 entry = qemu_malloc(sizeof(struct pcmcia_socket_entry_s));
924 entry->socket = socket;
925 entry->next = pcmcia_sockets;
926 pcmcia_sockets = entry;
929 void pcmcia_socket_unregister(PCMCIASocket *socket)
931 struct pcmcia_socket_entry_s *entry, **ptr;
933 ptr = &pcmcia_sockets;
934 for (entry = *ptr; entry; ptr = &entry->next, entry = *ptr)
935 if (entry->socket == socket) {
941 void pcmcia_info(Monitor *mon)
943 struct pcmcia_socket_entry_s *iter;
946 monitor_printf(mon, "No PCMCIA sockets\n");
948 for (iter = pcmcia_sockets; iter; iter = iter->next)
949 monitor_printf(mon, "%s: %s\n", iter->socket->slot_string,
950 iter->socket->attached ? iter->socket->card_string :
954 /***********************************************************/
957 typedef struct IOHandlerRecord {
959 IOCanReadHandler *fd_read_poll;
966 QLIST_ENTRY(IOHandlerRecord) next;
969 static QLIST_HEAD(, IOHandlerRecord) io_handlers =
970 QLIST_HEAD_INITIALIZER(io_handlers);
973 /* XXX: fd_read_poll should be suppressed, but an API change is
974 necessary in the character devices to suppress fd_can_read(). */
975 int qemu_set_fd_handler2(int fd,
976 IOCanReadHandler *fd_read_poll,
981 IOHandlerRecord *ioh;
983 if (!fd_read && !fd_write) {
984 QLIST_FOREACH(ioh, &io_handlers, next) {
991 QLIST_FOREACH(ioh, &io_handlers, next) {
995 ioh = qemu_mallocz(sizeof(IOHandlerRecord));
996 QLIST_INSERT_HEAD(&io_handlers, ioh, next);
999 ioh->fd_read_poll = fd_read_poll;
1000 ioh->fd_read = fd_read;
1001 ioh->fd_write = fd_write;
1002 ioh->opaque = opaque;
1008 int qemu_set_fd_handler(int fd,
1010 IOHandler *fd_write,
1013 return qemu_set_fd_handler2(fd, NULL, fd_read, fd_write, opaque);
1016 /***********************************************************/
1017 /* machine registration */
1019 static QEMUMachine *first_machine = NULL;
1020 QEMUMachine *current_machine = NULL;
1022 int qemu_register_machine(QEMUMachine *m)
1025 pm = &first_machine;
1033 static QEMUMachine *find_machine(const char *name)
1037 for(m = first_machine; m != NULL; m = m->next) {
1038 if (!strcmp(m->name, name))
1040 if (m->alias && !strcmp(m->alias, name))
1046 static QEMUMachine *find_default_machine(void)
1050 for(m = first_machine; m != NULL; m = m->next) {
1051 if (m->is_default) {
1058 /***********************************************************/
1059 /* main execution loop */
1061 static void gui_update(void *opaque)
1063 uint64_t interval = GUI_REFRESH_INTERVAL;
1064 DisplayState *ds = opaque;
1065 DisplayChangeListener *dcl = ds->listeners;
1067 qemu_flush_coalesced_mmio_buffer();
1070 while (dcl != NULL) {
1071 if (dcl->gui_timer_interval &&
1072 dcl->gui_timer_interval < interval)
1073 interval = dcl->gui_timer_interval;
1076 qemu_mod_timer(ds->gui_timer, interval + qemu_get_clock(rt_clock));
1079 static void nographic_update(void *opaque)
1081 uint64_t interval = GUI_REFRESH_INTERVAL;
1083 qemu_flush_coalesced_mmio_buffer();
1084 qemu_mod_timer(nographic_timer, interval + qemu_get_clock(rt_clock));
1087 struct vm_change_state_entry {
1088 VMChangeStateHandler *cb;
1090 QLIST_ENTRY (vm_change_state_entry) entries;
1093 static QLIST_HEAD(vm_change_state_head, vm_change_state_entry) vm_change_state_head;
1095 VMChangeStateEntry *qemu_add_vm_change_state_handler(VMChangeStateHandler *cb,
1098 VMChangeStateEntry *e;
1100 e = qemu_mallocz(sizeof (*e));
1104 QLIST_INSERT_HEAD(&vm_change_state_head, e, entries);
1108 void qemu_del_vm_change_state_handler(VMChangeStateEntry *e)
1110 QLIST_REMOVE (e, entries);
1114 void vm_state_notify(int running, int reason)
1116 VMChangeStateEntry *e;
1118 trace_vm_state_notify(running, reason);
1120 for (e = vm_change_state_head.lh_first; e; e = e->entries.le_next) {
1121 e->cb(e->opaque, running, reason);
1130 vm_state_notify(1, 0);
1132 monitor_protocol_event(QEVENT_RESUME, NULL);
1136 /* reset/shutdown handler */
1138 typedef struct QEMUResetEntry {
1139 QTAILQ_ENTRY(QEMUResetEntry) entry;
1140 QEMUResetHandler *func;
1144 static QTAILQ_HEAD(reset_handlers, QEMUResetEntry) reset_handlers =
1145 QTAILQ_HEAD_INITIALIZER(reset_handlers);
1146 static int reset_requested;
1147 static int shutdown_requested;
1148 static int powerdown_requested;
1149 int debug_requested;
1150 int vmstop_requested;
1152 int qemu_shutdown_requested(void)
1154 int r = shutdown_requested;
1155 shutdown_requested = 0;
1159 int qemu_reset_requested(void)
1161 int r = reset_requested;
1162 reset_requested = 0;
1166 int qemu_powerdown_requested(void)
1168 int r = powerdown_requested;
1169 powerdown_requested = 0;
1173 static int qemu_debug_requested(void)
1175 int r = debug_requested;
1176 debug_requested = 0;
1180 static int qemu_vmstop_requested(void)
1182 int r = vmstop_requested;
1183 vmstop_requested = 0;
1187 void qemu_register_reset(QEMUResetHandler *func, void *opaque)
1189 QEMUResetEntry *re = qemu_mallocz(sizeof(QEMUResetEntry));
1192 re->opaque = opaque;
1193 QTAILQ_INSERT_TAIL(&reset_handlers, re, entry);
1196 void qemu_unregister_reset(QEMUResetHandler *func, void *opaque)
1200 QTAILQ_FOREACH(re, &reset_handlers, entry) {
1201 if (re->func == func && re->opaque == opaque) {
1202 QTAILQ_REMOVE(&reset_handlers, re, entry);
1209 void qemu_system_reset(void)
1211 QEMUResetEntry *re, *nre;
1213 /* reset all devices */
1214 QTAILQ_FOREACH_SAFE(re, &reset_handlers, entry, nre) {
1215 re->func(re->opaque);
1217 monitor_protocol_event(QEVENT_RESET, NULL);
1218 cpu_synchronize_all_post_reset();
1221 void qemu_system_reset_request(void)
1224 shutdown_requested = 1;
1226 reset_requested = 1;
1228 qemu_notify_event();
1231 void qemu_system_shutdown_request(void)
1233 shutdown_requested = 1;
1234 qemu_notify_event();
1237 void qemu_system_powerdown_request(void)
1239 powerdown_requested = 1;
1240 qemu_notify_event();
1243 void main_loop_wait(int nonblocking)
1245 IOHandlerRecord *ioh;
1246 fd_set rfds, wfds, xfds;
1254 timeout = qemu_calculate_timeout();
1255 qemu_bh_update_timeout(&timeout);
1258 os_host_main_loop_wait(&timeout);
1260 /* poll any events */
1261 /* XXX: separate device handlers from system ones */
1266 QLIST_FOREACH(ioh, &io_handlers, next) {
1270 (!ioh->fd_read_poll ||
1271 ioh->fd_read_poll(ioh->opaque) != 0)) {
1272 FD_SET(ioh->fd, &rfds);
1276 if (ioh->fd_write) {
1277 FD_SET(ioh->fd, &wfds);
1283 tv.tv_sec = timeout / 1000;
1284 tv.tv_usec = (timeout % 1000) * 1000;
1286 slirp_select_fill(&nfds, &rfds, &wfds, &xfds);
1288 qemu_mutex_unlock_iothread();
1289 ret = select(nfds + 1, &rfds, &wfds, &xfds, &tv);
1290 qemu_mutex_lock_iothread();
1292 IOHandlerRecord *pioh;
1294 QLIST_FOREACH_SAFE(ioh, &io_handlers, next, pioh) {
1295 if (!ioh->deleted && ioh->fd_read && FD_ISSET(ioh->fd, &rfds)) {
1296 ioh->fd_read(ioh->opaque);
1298 if (!ioh->deleted && ioh->fd_write && FD_ISSET(ioh->fd, &wfds)) {
1299 ioh->fd_write(ioh->opaque);
1302 /* Do this last in case read/write handlers marked it for deletion */
1304 QLIST_REMOVE(ioh, next);
1310 slirp_select_poll(&rfds, &wfds, &xfds, (ret < 0));
1312 qemu_run_all_timers();
1314 /* Check bottom-halves last in case any of the earlier events triggered
1320 static int vm_can_run(void)
1322 if (powerdown_requested)
1324 if (reset_requested)
1326 if (shutdown_requested)
1328 if (debug_requested)
1333 qemu_irq qemu_system_powerdown;
1335 static void main_loop(void)
1339 qemu_main_loop_start();
1343 bool nonblocking = false;
1344 #ifdef CONFIG_PROFILER
1347 #ifndef CONFIG_IOTHREAD
1348 nonblocking = cpu_exec_all();
1350 #ifdef CONFIG_PROFILER
1351 ti = profile_getclock();
1353 main_loop_wait(nonblocking);
1354 #ifdef CONFIG_PROFILER
1355 dev_time += profile_getclock() - ti;
1357 } while (vm_can_run());
1359 if ((r = qemu_debug_requested())) {
1362 if (qemu_shutdown_requested()) {
1363 monitor_protocol_event(QEVENT_SHUTDOWN, NULL);
1370 if (qemu_reset_requested()) {
1372 qemu_system_reset();
1375 if (qemu_powerdown_requested()) {
1376 monitor_protocol_event(QEVENT_POWERDOWN, NULL);
1377 qemu_irq_raise(qemu_system_powerdown);
1379 if ((r = qemu_vmstop_requested())) {
1387 static void version(void)
1389 printf("QEMU emulator version " QEMU_VERSION QEMU_PKGVERSION ", Copyright (c) 2003-2008 Fabrice Bellard\n");
1392 static void help(int exitcode)
1394 const char *options_help =
1395 #define DEF(option, opt_arg, opt_enum, opt_help, arch_mask) \
1397 #define DEFHEADING(text) stringify(text) "\n"
1398 #include "qemu-options.def"
1404 printf("usage: %s [options] [disk_image]\n"
1406 "'disk_image' is a raw hard disk image for IDE hard disk 0\n"
1409 "During emulation, the following keys are useful:\n"
1410 "ctrl-alt-f toggle full screen\n"
1411 "ctrl-alt-n switch to virtual console 'n'\n"
1412 "ctrl-alt toggle mouse and keyboard grab\n"
1414 "When using -nographic, press 'ctrl-a h' to get some help.\n",
1420 #define HAS_ARG 0x0001
1422 typedef struct QEMUOption {
1429 static const QEMUOption qemu_options[] = {
1430 { "h", 0, QEMU_OPTION_h, QEMU_ARCH_ALL },
1431 #define DEF(option, opt_arg, opt_enum, opt_help, arch_mask) \
1432 { option, opt_arg, opt_enum, arch_mask },
1433 #define DEFHEADING(text)
1434 #include "qemu-options.def"
1440 static void select_vgahw (const char *p)
1445 vga_interface_type = VGA_NONE;
1446 if (strstart(p, "std", &opts)) {
1447 vga_interface_type = VGA_STD;
1448 } else if (strstart(p, "cirrus", &opts)) {
1449 vga_interface_type = VGA_CIRRUS;
1450 } else if (strstart(p, "vmware", &opts)) {
1451 vga_interface_type = VGA_VMWARE;
1452 } else if (strstart(p, "xenfb", &opts)) {
1453 vga_interface_type = VGA_XENFB;
1454 } else if (!strstart(p, "none", &opts)) {
1456 fprintf(stderr, "Unknown vga type: %s\n", p);
1460 const char *nextopt;
1462 if (strstart(opts, ",retrace=", &nextopt)) {
1464 if (strstart(opts, "dumb", &nextopt))
1465 vga_retrace_method = VGA_RETRACE_DUMB;
1466 else if (strstart(opts, "precise", &nextopt))
1467 vga_retrace_method = VGA_RETRACE_PRECISE;
1468 else goto invalid_vga;
1469 } else goto invalid_vga;
1474 static int balloon_parse(const char *arg)
1478 if (strcmp(arg, "none") == 0) {
1482 if (!strncmp(arg, "virtio", 6)) {
1483 if (arg[6] == ',') {
1484 /* have params -> parse them */
1485 opts = qemu_opts_parse(qemu_find_opts("device"), arg+7, 0);
1489 /* create empty opts */
1490 opts = qemu_opts_create(qemu_find_opts("device"), NULL, 0);
1492 qemu_opt_set(opts, "driver", "virtio-balloon-pci");
1499 char *qemu_find_file(int type, const char *name)
1505 /* If name contains path separators then try it as a straight path. */
1506 if ((strchr(name, '/') || strchr(name, '\\'))
1507 && access(name, R_OK) == 0) {
1508 return qemu_strdup(name);
1511 case QEMU_FILE_TYPE_BIOS:
1514 case QEMU_FILE_TYPE_KEYMAP:
1515 subdir = "keymaps/";
1520 len = strlen(data_dir) + strlen(name) + strlen(subdir) + 2;
1521 buf = qemu_mallocz(len);
1522 snprintf(buf, len, "%s/%s%s", data_dir, subdir, name);
1523 if (access(buf, R_OK)) {
1530 static int device_help_func(QemuOpts *opts, void *opaque)
1532 return qdev_device_help(opts);
1535 static int device_init_func(QemuOpts *opts, void *opaque)
1539 dev = qdev_device_add(opts);
1545 static int chardev_init_func(QemuOpts *opts, void *opaque)
1547 CharDriverState *chr;
1549 chr = qemu_chr_open_opts(opts, NULL);
1555 #ifdef CONFIG_VIRTFS
1556 static int fsdev_init_func(QemuOpts *opts, void *opaque)
1559 ret = qemu_fsdev_add(opts);
1565 static int mon_init_func(QemuOpts *opts, void *opaque)
1567 CharDriverState *chr;
1568 const char *chardev;
1572 mode = qemu_opt_get(opts, "mode");
1576 if (strcmp(mode, "readline") == 0) {
1577 flags = MONITOR_USE_READLINE;
1578 } else if (strcmp(mode, "control") == 0) {
1579 flags = MONITOR_USE_CONTROL;
1581 fprintf(stderr, "unknown monitor mode \"%s\"\n", mode);
1585 if (qemu_opt_get_bool(opts, "pretty", 0))
1586 flags |= MONITOR_USE_PRETTY;
1588 if (qemu_opt_get_bool(opts, "default", 0))
1589 flags |= MONITOR_IS_DEFAULT;
1591 chardev = qemu_opt_get(opts, "chardev");
1592 chr = qemu_chr_find(chardev);
1594 fprintf(stderr, "chardev \"%s\" not found\n", chardev);
1598 monitor_init(chr, flags);
1602 static void monitor_parse(const char *optarg, const char *mode)
1604 static int monitor_device_index = 0;
1610 if (strstart(optarg, "chardev:", &p)) {
1611 snprintf(label, sizeof(label), "%s", p);
1613 snprintf(label, sizeof(label), "compat_monitor%d",
1614 monitor_device_index);
1615 if (monitor_device_index == 0) {
1618 opts = qemu_chr_parse_compat(label, optarg);
1620 fprintf(stderr, "parse error: %s\n", optarg);
1625 opts = qemu_opts_create(qemu_find_opts("mon"), label, 1);
1627 fprintf(stderr, "duplicate chardev: %s\n", label);
1630 qemu_opt_set(opts, "mode", mode);
1631 qemu_opt_set(opts, "chardev", label);
1633 qemu_opt_set(opts, "default", "on");
1634 monitor_device_index++;
1637 struct device_config {
1639 DEV_USB, /* -usbdevice */
1641 DEV_SERIAL, /* -serial */
1642 DEV_PARALLEL, /* -parallel */
1643 DEV_VIRTCON, /* -virtioconsole */
1644 DEV_DEBUGCON, /* -debugcon */
1646 const char *cmdline;
1647 QTAILQ_ENTRY(device_config) next;
1649 QTAILQ_HEAD(, device_config) device_configs = QTAILQ_HEAD_INITIALIZER(device_configs);
1651 static void add_device_config(int type, const char *cmdline)
1653 struct device_config *conf;
1655 conf = qemu_mallocz(sizeof(*conf));
1657 conf->cmdline = cmdline;
1658 QTAILQ_INSERT_TAIL(&device_configs, conf, next);
1661 static int foreach_device_config(int type, int (*func)(const char *cmdline))
1663 struct device_config *conf;
1666 QTAILQ_FOREACH(conf, &device_configs, next) {
1667 if (conf->type != type)
1669 rc = func(conf->cmdline);
1676 static int serial_parse(const char *devname)
1678 static int index = 0;
1681 if (strcmp(devname, "none") == 0)
1683 if (index == MAX_SERIAL_PORTS) {
1684 fprintf(stderr, "qemu: too many serial ports\n");
1687 snprintf(label, sizeof(label), "serial%d", index);
1688 serial_hds[index] = qemu_chr_open(label, devname, NULL);
1689 if (!serial_hds[index]) {
1690 fprintf(stderr, "qemu: could not open serial device '%s': %s\n",
1691 devname, strerror(errno));
1698 static int parallel_parse(const char *devname)
1700 static int index = 0;
1703 if (strcmp(devname, "none") == 0)
1705 if (index == MAX_PARALLEL_PORTS) {
1706 fprintf(stderr, "qemu: too many parallel ports\n");
1709 snprintf(label, sizeof(label), "parallel%d", index);
1710 parallel_hds[index] = qemu_chr_open(label, devname, NULL);
1711 if (!parallel_hds[index]) {
1712 fprintf(stderr, "qemu: could not open parallel device '%s': %s\n",
1713 devname, strerror(errno));
1720 static int virtcon_parse(const char *devname)
1722 QemuOptsList *device = qemu_find_opts("device");
1723 static int index = 0;
1725 QemuOpts *bus_opts, *dev_opts;
1727 if (strcmp(devname, "none") == 0)
1729 if (index == MAX_VIRTIO_CONSOLES) {
1730 fprintf(stderr, "qemu: too many virtio consoles\n");
1734 bus_opts = qemu_opts_create(device, NULL, 0);
1735 qemu_opt_set(bus_opts, "driver", "virtio-serial");
1737 dev_opts = qemu_opts_create(device, NULL, 0);
1738 qemu_opt_set(dev_opts, "driver", "virtconsole");
1740 snprintf(label, sizeof(label), "virtcon%d", index);
1741 virtcon_hds[index] = qemu_chr_open(label, devname, NULL);
1742 if (!virtcon_hds[index]) {
1743 fprintf(stderr, "qemu: could not open virtio console '%s': %s\n",
1744 devname, strerror(errno));
1747 qemu_opt_set(dev_opts, "chardev", label);
1753 static int debugcon_parse(const char *devname)
1757 if (!qemu_chr_open("debugcon", devname, NULL)) {
1760 opts = qemu_opts_create(qemu_find_opts("device"), "debugcon", 1);
1762 fprintf(stderr, "qemu: already have a debugcon device\n");
1765 qemu_opt_set(opts, "driver", "isa-debugcon");
1766 qemu_opt_set(opts, "chardev", "debugcon");
1770 void qemu_add_exit_notifier(Notifier *notify)
1772 notifier_list_add(&exit_notifiers, notify);
1775 void qemu_remove_exit_notifier(Notifier *notify)
1777 notifier_list_remove(&exit_notifiers, notify);
1780 static void qemu_run_exit_notifiers(void)
1782 notifier_list_notify(&exit_notifiers);
1785 static const QEMUOption *lookup_opt(int argc, char **argv,
1786 const char **poptarg, int *poptind)
1788 const QEMUOption *popt;
1789 int optind = *poptind;
1790 char *r = argv[optind];
1793 loc_set_cmdline(argv, optind, 1);
1795 /* Treat --foo the same as -foo. */
1798 popt = qemu_options;
1801 error_report("invalid option");
1804 if (!strcmp(popt->name, r + 1))
1808 if (popt->flags & HAS_ARG) {
1809 if (optind >= argc) {
1810 error_report("requires an argument");
1813 optarg = argv[optind++];
1814 loc_set_cmdline(argv, optind - 2, 2);
1825 int main(int argc, char **argv, char **envp)
1827 const char *gdbstub_dev = NULL;
1829 int snapshot, linux_boot;
1830 const char *icount_option = NULL;
1831 const char *initrd_filename;
1832 const char *kernel_filename, *kernel_cmdline;
1833 char boot_devices[33] = "cad"; /* default to HD->floppy->CD-ROM */
1835 DisplayChangeListener *dcl;
1836 int cyls, heads, secs, translation;
1837 QemuOpts *hda_opts = NULL, *opts;
1838 QemuOptsList *olist;
1841 const char *loadvm = NULL;
1842 QEMUMachine *machine;
1843 const char *cpu_model;
1845 const char *pid_file = NULL;
1846 const char *incoming = NULL;
1847 int show_vnc_port = 0;
1850 #ifdef CONFIG_SIMPLE_TRACE
1851 const char *trace_file = NULL;
1853 atexit(qemu_run_exit_notifiers);
1854 error_set_progname(argv[0]);
1858 qemu_cache_utils_init(envp);
1860 QLIST_INIT (&vm_change_state_head);
1861 os_setup_early_signal_handling();
1863 module_call_init(MODULE_INIT_MACHINE);
1864 machine = find_default_machine();
1866 initrd_filename = NULL;
1869 kernel_filename = NULL;
1870 kernel_cmdline = "";
1871 cyls = heads = secs = 0;
1872 translation = BIOS_ATA_TRANSLATION_AUTO;
1874 for (i = 0; i < MAX_NODES; i++) {
1876 node_cpumask[i] = 0;
1885 /* first pass of option parsing */
1887 while (optind < argc) {
1888 if (argv[optind][0] != '-') {
1893 const QEMUOption *popt;
1895 popt = lookup_opt(argc, argv, &optarg, &optind);
1896 switch (popt->index) {
1897 case QEMU_OPTION_nodefconfig:
1907 ret = qemu_read_config_file(CONFIG_QEMU_CONFDIR "/qemu.conf");
1908 if (ret < 0 && ret != -ENOENT) {
1912 ret = qemu_read_config_file(arch_config_name);
1913 if (ret < 0 && ret != -ENOENT) {
1919 /* second pass of option parsing */
1924 if (argv[optind][0] != '-') {
1925 hda_opts = drive_add(argv[optind++], HD_ALIAS, 0);
1927 const QEMUOption *popt;
1929 popt = lookup_opt(argc, argv, &optarg, &optind);
1930 if (!(popt->arch_mask & arch_type)) {
1931 printf("Option %s not supported for this target\n", popt->name);
1934 switch(popt->index) {
1936 machine = find_machine(optarg);
1939 printf("Supported machines are:\n");
1940 for(m = first_machine; m != NULL; m = m->next) {
1942 printf("%-10s %s (alias of %s)\n",
1943 m->alias, m->desc, m->name);
1944 printf("%-10s %s%s\n",
1946 m->is_default ? " (default)" : "");
1948 exit(*optarg != '?');
1951 case QEMU_OPTION_cpu:
1952 /* hw initialization will check this */
1953 if (*optarg == '?') {
1954 list_cpus(stdout, &fprintf, optarg);
1960 case QEMU_OPTION_initrd:
1961 initrd_filename = optarg;
1963 case QEMU_OPTION_hda:
1965 hda_opts = drive_add(optarg, HD_ALIAS, 0);
1967 hda_opts = drive_add(optarg, HD_ALIAS
1968 ",cyls=%d,heads=%d,secs=%d%s",
1969 0, cyls, heads, secs,
1970 translation == BIOS_ATA_TRANSLATION_LBA ?
1972 translation == BIOS_ATA_TRANSLATION_NONE ?
1973 ",trans=none" : "");
1975 case QEMU_OPTION_hdb:
1976 case QEMU_OPTION_hdc:
1977 case QEMU_OPTION_hdd:
1978 drive_add(optarg, HD_ALIAS, popt->index - QEMU_OPTION_hda);
1980 case QEMU_OPTION_drive:
1981 drive_add(NULL, "%s", optarg);
1983 case QEMU_OPTION_set:
1984 if (qemu_set_option(optarg) != 0)
1987 case QEMU_OPTION_global:
1988 if (qemu_global_option(optarg) != 0)
1991 case QEMU_OPTION_mtdblock:
1992 drive_add(optarg, MTD_ALIAS);
1994 case QEMU_OPTION_sd:
1995 drive_add(optarg, SD_ALIAS);
1997 case QEMU_OPTION_pflash:
1998 drive_add(optarg, PFLASH_ALIAS);
2000 case QEMU_OPTION_snapshot:
2003 case QEMU_OPTION_hdachs:
2007 cyls = strtol(p, (char **)&p, 0);
2008 if (cyls < 1 || cyls > 16383)
2013 heads = strtol(p, (char **)&p, 0);
2014 if (heads < 1 || heads > 16)
2019 secs = strtol(p, (char **)&p, 0);
2020 if (secs < 1 || secs > 63)
2024 if (!strcmp(p, "none"))
2025 translation = BIOS_ATA_TRANSLATION_NONE;
2026 else if (!strcmp(p, "lba"))
2027 translation = BIOS_ATA_TRANSLATION_LBA;
2028 else if (!strcmp(p, "auto"))
2029 translation = BIOS_ATA_TRANSLATION_AUTO;
2032 } else if (*p != '\0') {
2034 fprintf(stderr, "qemu: invalid physical CHS format\n");
2037 if (hda_opts != NULL) {
2039 snprintf(num, sizeof(num), "%d", cyls);
2040 qemu_opt_set(hda_opts, "cyls", num);
2041 snprintf(num, sizeof(num), "%d", heads);
2042 qemu_opt_set(hda_opts, "heads", num);
2043 snprintf(num, sizeof(num), "%d", secs);
2044 qemu_opt_set(hda_opts, "secs", num);
2045 if (translation == BIOS_ATA_TRANSLATION_LBA)
2046 qemu_opt_set(hda_opts, "trans", "lba");
2047 if (translation == BIOS_ATA_TRANSLATION_NONE)
2048 qemu_opt_set(hda_opts, "trans", "none");
2052 case QEMU_OPTION_numa:
2053 if (nb_numa_nodes >= MAX_NODES) {
2054 fprintf(stderr, "qemu: too many NUMA nodes\n");
2059 case QEMU_OPTION_nographic:
2060 display_type = DT_NOGRAPHIC;
2062 #ifdef CONFIG_CURSES
2063 case QEMU_OPTION_curses:
2064 display_type = DT_CURSES;
2067 case QEMU_OPTION_portrait:
2070 case QEMU_OPTION_kernel:
2071 kernel_filename = optarg;
2073 case QEMU_OPTION_append:
2074 kernel_cmdline = optarg;
2076 case QEMU_OPTION_cdrom:
2077 drive_add(optarg, CDROM_ALIAS);
2079 case QEMU_OPTION_boot:
2081 static const char * const params[] = {
2082 "order", "once", "menu", NULL
2084 char buf[sizeof(boot_devices)];
2085 char *standard_boot_devices;
2088 if (!strchr(optarg, '=')) {
2090 pstrcpy(buf, sizeof(buf), optarg);
2091 } else if (check_params(buf, sizeof(buf), params, optarg) < 0) {
2093 "qemu: unknown boot parameter '%s' in '%s'\n",
2099 get_param_value(buf, sizeof(buf), "order", optarg)) {
2100 validate_bootdevices(buf);
2101 pstrcpy(boot_devices, sizeof(boot_devices), buf);
2104 if (get_param_value(buf, sizeof(buf),
2106 validate_bootdevices(buf);
2107 standard_boot_devices = qemu_strdup(boot_devices);
2108 pstrcpy(boot_devices, sizeof(boot_devices), buf);
2109 qemu_register_reset(restore_boot_devices,
2110 standard_boot_devices);
2112 if (get_param_value(buf, sizeof(buf),
2114 if (!strcmp(buf, "on")) {
2116 } else if (!strcmp(buf, "off")) {
2120 "qemu: invalid option value '%s'\n",
2128 case QEMU_OPTION_fda:
2129 case QEMU_OPTION_fdb:
2130 drive_add(optarg, FD_ALIAS, popt->index - QEMU_OPTION_fda);
2132 case QEMU_OPTION_no_fd_bootchk:
2135 case QEMU_OPTION_netdev:
2136 if (net_client_parse(qemu_find_opts("netdev"), optarg) == -1) {
2140 case QEMU_OPTION_net:
2141 if (net_client_parse(qemu_find_opts("net"), optarg) == -1) {
2146 case QEMU_OPTION_tftp:
2147 legacy_tftp_prefix = optarg;
2149 case QEMU_OPTION_bootp:
2150 legacy_bootp_filename = optarg;
2152 case QEMU_OPTION_redir:
2153 if (net_slirp_redir(optarg) < 0)
2157 case QEMU_OPTION_bt:
2158 add_device_config(DEV_BT, optarg);
2160 case QEMU_OPTION_audio_help:
2161 if (!(audio_available())) {
2162 printf("Option %s not supported for this target\n", popt->name);
2168 case QEMU_OPTION_soundhw:
2169 if (!(audio_available())) {
2170 printf("Option %s not supported for this target\n", popt->name);
2173 select_soundhw (optarg);
2178 case QEMU_OPTION_version:
2182 case QEMU_OPTION_m: {
2185 value = strtosz(optarg, NULL);
2187 fprintf(stderr, "qemu: invalid ram size: %s\n", optarg);
2191 /* On 32-bit hosts, QEMU is limited by virtual address space */
2192 if (value > (2047 << 20) && HOST_LONG_BITS == 32) {
2193 fprintf(stderr, "qemu: at most 2047 MB RAM can be simulated\n");
2196 if (value != (uint64_t)(ram_addr_t)value) {
2197 fprintf(stderr, "qemu: ram size too large\n");
2203 case QEMU_OPTION_mempath:
2207 case QEMU_OPTION_mem_prealloc:
2212 set_cpu_log(optarg);
2215 gdbstub_dev = "tcp::" DEFAULT_GDBSTUB_PORT;
2217 case QEMU_OPTION_gdb:
2218 gdbstub_dev = optarg;
2223 case QEMU_OPTION_bios:
2226 case QEMU_OPTION_singlestep:
2233 keyboard_layout = optarg;
2235 case QEMU_OPTION_localtime:
2238 case QEMU_OPTION_vga:
2239 select_vgahw (optarg);
2246 w = strtol(p, (char **)&p, 10);
2249 fprintf(stderr, "qemu: invalid resolution or depth\n");
2255 h = strtol(p, (char **)&p, 10);
2260 depth = strtol(p, (char **)&p, 10);
2261 if (depth != 8 && depth != 15 && depth != 16 &&
2262 depth != 24 && depth != 32)
2264 } else if (*p == '\0') {
2265 depth = graphic_depth;
2272 graphic_depth = depth;
2275 case QEMU_OPTION_echr:
2278 term_escape_char = strtol(optarg, &r, 0);
2280 printf("Bad argument to echr\n");
2283 case QEMU_OPTION_monitor:
2284 monitor_parse(optarg, "readline");
2285 default_monitor = 0;
2287 case QEMU_OPTION_qmp:
2288 monitor_parse(optarg, "control");
2289 default_monitor = 0;
2291 case QEMU_OPTION_mon:
2292 opts = qemu_opts_parse(qemu_find_opts("mon"), optarg, 1);
2296 default_monitor = 0;
2298 case QEMU_OPTION_chardev:
2299 opts = qemu_opts_parse(qemu_find_opts("chardev"), optarg, 1);
2304 case QEMU_OPTION_fsdev:
2305 olist = qemu_find_opts("fsdev");
2307 fprintf(stderr, "fsdev is not supported by this qemu build.\n");
2310 opts = qemu_opts_parse(olist, optarg, 1);
2312 fprintf(stderr, "parse error: %s\n", optarg);
2316 case QEMU_OPTION_virtfs: {
2317 char *arg_fsdev = NULL;
2318 char *arg_9p = NULL;
2321 olist = qemu_find_opts("virtfs");
2323 fprintf(stderr, "virtfs is not supported by this qemu build.\n");
2326 opts = qemu_opts_parse(olist, optarg, 1);
2328 fprintf(stderr, "parse error: %s\n", optarg);
2332 if (qemu_opt_get(opts, "fstype") == NULL ||
2333 qemu_opt_get(opts, "mount_tag") == NULL ||
2334 qemu_opt_get(opts, "path") == NULL ||
2335 qemu_opt_get(opts, "security_model") == NULL) {
2336 fprintf(stderr, "Usage: -virtfs fstype,path=/share_path/,"
2337 "security_model=[mapped|passthrough|none],"
2342 len = strlen(",id=,path=,security_model=");
2343 len += strlen(qemu_opt_get(opts, "fstype"));
2344 len += strlen(qemu_opt_get(opts, "mount_tag"));
2345 len += strlen(qemu_opt_get(opts, "path"));
2346 len += strlen(qemu_opt_get(opts, "security_model"));
2347 arg_fsdev = qemu_malloc((len + 1) * sizeof(*arg_fsdev));
2349 snprintf(arg_fsdev, (len + 1) * sizeof(*arg_fsdev),
2350 "%s,id=%s,path=%s,security_model=%s",
2351 qemu_opt_get(opts, "fstype"),
2352 qemu_opt_get(opts, "mount_tag"),
2353 qemu_opt_get(opts, "path"),
2354 qemu_opt_get(opts, "security_model"));
2356 len = strlen("virtio-9p-pci,fsdev=,mount_tag=");
2357 len += 2*strlen(qemu_opt_get(opts, "mount_tag"));
2358 arg_9p = qemu_malloc((len + 1) * sizeof(*arg_9p));
2360 snprintf(arg_9p, (len + 1) * sizeof(*arg_9p),
2361 "virtio-9p-pci,fsdev=%s,mount_tag=%s",
2362 qemu_opt_get(opts, "mount_tag"),
2363 qemu_opt_get(opts, "mount_tag"));
2365 if (!qemu_opts_parse(qemu_find_opts("fsdev"), arg_fsdev, 1)) {
2366 fprintf(stderr, "parse error [fsdev]: %s\n", optarg);
2370 if (!qemu_opts_parse(qemu_find_opts("device"), arg_9p, 1)) {
2371 fprintf(stderr, "parse error [device]: %s\n", optarg);
2375 qemu_free(arg_fsdev);
2379 case QEMU_OPTION_serial:
2380 add_device_config(DEV_SERIAL, optarg);
2382 if (strncmp(optarg, "mon:", 4) == 0) {
2383 default_monitor = 0;
2386 case QEMU_OPTION_watchdog:
2389 "qemu: only one watchdog option may be given\n");
2394 case QEMU_OPTION_watchdog_action:
2395 if (select_watchdog_action(optarg) == -1) {
2396 fprintf(stderr, "Unknown -watchdog-action parameter\n");
2400 case QEMU_OPTION_virtiocon:
2401 add_device_config(DEV_VIRTCON, optarg);
2402 default_virtcon = 0;
2403 if (strncmp(optarg, "mon:", 4) == 0) {
2404 default_monitor = 0;
2407 case QEMU_OPTION_parallel:
2408 add_device_config(DEV_PARALLEL, optarg);
2409 default_parallel = 0;
2410 if (strncmp(optarg, "mon:", 4) == 0) {
2411 default_monitor = 0;
2414 case QEMU_OPTION_debugcon:
2415 add_device_config(DEV_DEBUGCON, optarg);
2417 case QEMU_OPTION_loadvm:
2420 case QEMU_OPTION_full_screen:
2424 case QEMU_OPTION_no_frame:
2427 case QEMU_OPTION_alt_grab:
2430 case QEMU_OPTION_ctrl_grab:
2433 case QEMU_OPTION_no_quit:
2436 case QEMU_OPTION_sdl:
2437 display_type = DT_SDL;
2440 case QEMU_OPTION_pidfile:
2443 case QEMU_OPTION_win2k_hack:
2444 win2k_install_hack = 1;
2446 case QEMU_OPTION_rtc_td_hack:
2449 case QEMU_OPTION_acpitable:
2450 do_acpitable_option(optarg);
2452 case QEMU_OPTION_smbios:
2453 do_smbios_option(optarg);
2455 case QEMU_OPTION_enable_kvm:
2458 case QEMU_OPTION_usb:
2461 case QEMU_OPTION_usbdevice:
2463 add_device_config(DEV_USB, optarg);
2465 case QEMU_OPTION_device:
2466 if (!qemu_opts_parse(qemu_find_opts("device"), optarg, 1)) {
2470 case QEMU_OPTION_smp:
2473 fprintf(stderr, "Invalid number of CPUs\n");
2476 if (max_cpus < smp_cpus) {
2477 fprintf(stderr, "maxcpus must be equal to or greater than "
2481 if (max_cpus > 255) {
2482 fprintf(stderr, "Unsupported number of maxcpus\n");
2486 case QEMU_OPTION_vnc:
2488 vnc_display = optarg;
2490 case QEMU_OPTION_no_acpi:
2493 case QEMU_OPTION_no_hpet:
2496 case QEMU_OPTION_balloon:
2497 if (balloon_parse(optarg) < 0) {
2498 fprintf(stderr, "Unknown -balloon argument %s\n", optarg);
2502 case QEMU_OPTION_no_reboot:
2505 case QEMU_OPTION_no_shutdown:
2508 case QEMU_OPTION_show_cursor:
2511 case QEMU_OPTION_uuid:
2512 if(qemu_uuid_parse(optarg, qemu_uuid) < 0) {
2513 fprintf(stderr, "Fail to parse UUID string."
2514 " Wrong format.\n");
2518 case QEMU_OPTION_option_rom:
2519 if (nb_option_roms >= MAX_OPTION_ROMS) {
2520 fprintf(stderr, "Too many option ROMs\n");
2523 opts = qemu_opts_parse(qemu_find_opts("option-rom"), optarg, 1);
2524 option_rom[nb_option_roms].name = qemu_opt_get(opts, "romfile");
2525 option_rom[nb_option_roms].bootindex =
2526 qemu_opt_get_number(opts, "bootindex", -1);
2527 if (!option_rom[nb_option_roms].name) {
2528 fprintf(stderr, "Option ROM file is not specified\n");
2533 case QEMU_OPTION_semihosting:
2534 semihosting_enabled = 1;
2536 case QEMU_OPTION_name:
2537 qemu_name = qemu_strdup(optarg);
2539 char *p = strchr(qemu_name, ',');
2542 if (strncmp(p, "process=", 8)) {
2543 fprintf(stderr, "Unknown subargument %s to -name", p);
2547 os_set_proc_name(p);
2551 case QEMU_OPTION_prom_env:
2552 if (nb_prom_envs >= MAX_PROM_ENVS) {
2553 fprintf(stderr, "Too many prom variables\n");
2556 prom_envs[nb_prom_envs] = optarg;
2559 case QEMU_OPTION_old_param:
2562 case QEMU_OPTION_clock:
2563 configure_alarms(optarg);
2565 case QEMU_OPTION_startdate:
2566 configure_rtc_date_offset(optarg, 1);
2568 case QEMU_OPTION_rtc:
2569 opts = qemu_opts_parse(qemu_find_opts("rtc"), optarg, 0);
2573 configure_rtc(opts);
2575 case QEMU_OPTION_tb_size:
2576 tb_size = strtol(optarg, NULL, 0);
2580 case QEMU_OPTION_icount:
2581 icount_option = optarg;
2583 case QEMU_OPTION_incoming:
2585 incoming_expected = true;
2587 case QEMU_OPTION_nodefaults:
2589 default_parallel = 0;
2590 default_virtcon = 0;
2591 default_monitor = 0;
2598 case QEMU_OPTION_xen_domid:
2599 if (!(xen_available())) {
2600 printf("Option %s not supported for this target\n", popt->name);
2603 xen_domid = atoi(optarg);
2605 case QEMU_OPTION_xen_create:
2606 if (!(xen_available())) {
2607 printf("Option %s not supported for this target\n", popt->name);
2610 xen_mode = XEN_CREATE;
2612 case QEMU_OPTION_xen_attach:
2613 if (!(xen_available())) {
2614 printf("Option %s not supported for this target\n", popt->name);
2617 xen_mode = XEN_ATTACH;
2619 #ifdef CONFIG_SIMPLE_TRACE
2620 case QEMU_OPTION_trace:
2621 opts = qemu_opts_parse(qemu_find_opts("trace"), optarg, 0);
2623 trace_file = qemu_opt_get(opts, "file");
2627 case QEMU_OPTION_readconfig:
2629 int ret = qemu_read_config_file(optarg);
2631 fprintf(stderr, "read config %s: %s\n", optarg,
2637 case QEMU_OPTION_spice:
2638 olist = qemu_find_opts("spice");
2640 fprintf(stderr, "spice is not supported by this qemu build.\n");
2643 opts = qemu_opts_parse(olist, optarg, 0);
2645 fprintf(stderr, "parse error: %s\n", optarg);
2649 case QEMU_OPTION_writeconfig:
2652 if (strcmp(optarg, "-") == 0) {
2655 fp = fopen(optarg, "w");
2657 fprintf(stderr, "open %s: %s\n", optarg, strerror(errno));
2661 qemu_config_write(fp);
2666 os_parse_cmd_args(popt->index, optarg);
2672 /* If no data_dir is specified then try to find it relative to the
2675 data_dir = os_find_datadir(argv[0]);
2677 /* If all else fails use the install patch specified when building. */
2679 data_dir = CONFIG_QEMU_DATADIR;
2682 #ifdef CONFIG_SIMPLE_TRACE
2684 * Set the trace file name, if specified.
2686 st_set_trace_file(trace_file);
2689 * Default to max_cpus = smp_cpus, in case the user doesn't
2690 * specify a max_cpus value.
2693 max_cpus = smp_cpus;
2695 machine->max_cpus = machine->max_cpus ?: 1; /* Default to UP */
2696 if (smp_cpus > machine->max_cpus) {
2697 fprintf(stderr, "Number of SMP cpus requested (%d), exceeds max cpus "
2698 "supported by machine `%s' (%d)\n", smp_cpus, machine->name,
2703 qemu_opts_foreach(qemu_find_opts("device"), default_driver_check, NULL, 0);
2704 qemu_opts_foreach(qemu_find_opts("global"), default_driver_check, NULL, 0);
2706 if (machine->no_serial) {
2709 if (machine->no_parallel) {
2710 default_parallel = 0;
2712 if (!machine->use_virtcon) {
2713 default_virtcon = 0;
2715 if (machine->no_vga) {
2718 if (machine->no_floppy) {
2721 if (machine->no_cdrom) {
2724 if (machine->no_sdcard) {
2728 if (display_type == DT_NOGRAPHIC) {
2729 if (default_parallel)
2730 add_device_config(DEV_PARALLEL, "null");
2731 if (default_serial && default_monitor) {
2732 add_device_config(DEV_SERIAL, "mon:stdio");
2733 } else if (default_virtcon && default_monitor) {
2734 add_device_config(DEV_VIRTCON, "mon:stdio");
2737 add_device_config(DEV_SERIAL, "stdio");
2738 if (default_virtcon)
2739 add_device_config(DEV_VIRTCON, "stdio");
2740 if (default_monitor)
2741 monitor_parse("stdio", "readline");
2745 add_device_config(DEV_SERIAL, "vc:80Cx24C");
2746 if (default_parallel)
2747 add_device_config(DEV_PARALLEL, "vc:80Cx24C");
2748 if (default_monitor)
2749 monitor_parse("vc:80Cx24C", "readline");
2750 if (default_virtcon)
2751 add_device_config(DEV_VIRTCON, "vc:80Cx24C");
2754 vga_interface_type = VGA_CIRRUS;
2758 if (qemu_opts_foreach(qemu_find_opts("chardev"), chardev_init_func, NULL, 1) != 0)
2760 #ifdef CONFIG_VIRTFS
2761 if (qemu_opts_foreach(qemu_find_opts("fsdev"), fsdev_init_func, NULL, 1) != 0) {
2768 if (pid_file && qemu_create_pidfile(pid_file) != 0) {
2774 int ret = kvm_init(smp_cpus);
2776 if (!kvm_available()) {
2777 printf("KVM not supported for this target\n");
2779 fprintf(stderr, "failed to initialize KVM: %s\n", strerror(-ret));
2785 if (qemu_init_main_loop()) {
2786 fprintf(stderr, "qemu_init_main_loop failed\n");
2789 linux_boot = (kernel_filename != NULL);
2791 if (!linux_boot && *kernel_cmdline != '\0') {
2792 fprintf(stderr, "-append only allowed with -kernel option\n");
2796 if (!linux_boot && initrd_filename != NULL) {
2797 fprintf(stderr, "-initrd only allowed with -kernel option\n");
2801 os_set_line_buffering();
2803 if (init_timer_alarm() < 0) {
2804 fprintf(stderr, "could not initialize alarm timer\n");
2807 configure_icount(icount_option);
2809 if (net_init_clients() < 0) {
2813 /* init the bluetooth world */
2814 if (foreach_device_config(DEV_BT, bt_parse))
2817 /* init the memory */
2819 ram_size = DEFAULT_RAM_SIZE * 1024 * 1024;
2821 /* init the dynamic translator */
2822 cpu_exec_init_all(tb_size * 1024 * 1024);
2824 bdrv_init_with_whitelist();
2828 if (default_cdrom) {
2829 /* we always create the cdrom drive, even if no disk is there */
2830 drive_add(NULL, CDROM_ALIAS);
2833 if (default_floppy) {
2834 /* we always create at least one floppy */
2835 drive_add(NULL, FD_ALIAS, 0);
2838 if (default_sdcard) {
2839 /* we always create one sd slot, even if no card is in it */
2840 drive_add(NULL, SD_ALIAS);
2843 /* open the virtual block devices */
2845 qemu_opts_foreach(qemu_find_opts("drive"), drive_enable_snapshot, NULL, 0);
2846 if (qemu_opts_foreach(qemu_find_opts("drive"), drive_init_func, &machine->use_scsi, 1) != 0)
2849 register_savevm_live(NULL, "ram", 0, 4, NULL, ram_save_live, NULL,
2852 if (nb_numa_nodes > 0) {
2855 if (nb_numa_nodes > smp_cpus) {
2856 nb_numa_nodes = smp_cpus;
2859 /* If no memory size if given for any node, assume the default case
2860 * and distribute the available memory equally across all nodes
2862 for (i = 0; i < nb_numa_nodes; i++) {
2863 if (node_mem[i] != 0)
2866 if (i == nb_numa_nodes) {
2867 uint64_t usedmem = 0;
2869 /* On Linux, the each node's border has to be 8MB aligned,
2870 * the final node gets the rest.
2872 for (i = 0; i < nb_numa_nodes - 1; i++) {
2873 node_mem[i] = (ram_size / nb_numa_nodes) & ~((1 << 23UL) - 1);
2874 usedmem += node_mem[i];
2876 node_mem[i] = ram_size - usedmem;
2879 for (i = 0; i < nb_numa_nodes; i++) {
2880 if (node_cpumask[i] != 0)
2883 /* assigning the VCPUs round-robin is easier to implement, guest OSes
2884 * must cope with this anyway, because there are BIOSes out there in
2885 * real machines which also use this scheme.
2887 if (i == nb_numa_nodes) {
2888 for (i = 0; i < smp_cpus; i++) {
2889 node_cpumask[i % nb_numa_nodes] |= 1 << i;
2894 if (qemu_opts_foreach(qemu_find_opts("mon"), mon_init_func, NULL, 1) != 0) {
2898 if (foreach_device_config(DEV_SERIAL, serial_parse) < 0)
2900 if (foreach_device_config(DEV_PARALLEL, parallel_parse) < 0)
2902 if (foreach_device_config(DEV_VIRTCON, virtcon_parse) < 0)
2904 if (foreach_device_config(DEV_DEBUGCON, debugcon_parse) < 0)
2907 module_call_init(MODULE_INIT_DEVICE);
2909 if (qemu_opts_foreach(qemu_find_opts("device"), device_help_func, NULL, 0) != 0)
2913 i = select_watchdog(watchdog);
2915 exit (i == 1 ? 1 : 0);
2918 if (machine->compat_props) {
2919 qdev_prop_register_global_list(machine->compat_props);
2923 machine->init(ram_size, boot_devices,
2924 kernel_filename, kernel_cmdline, initrd_filename, cpu_model);
2926 cpu_synchronize_all_post_init();
2928 /* must be after terminal init, SDL library changes signal handlers */
2929 os_setup_signal_handling();
2933 current_machine = machine;
2935 /* init USB devices */
2937 if (foreach_device_config(DEV_USB, usb_parse) < 0)
2941 /* init generic devices */
2942 if (qemu_opts_foreach(qemu_find_opts("device"), device_init_func, NULL, 1) != 0)
2945 net_check_clients();
2947 /* just use the first displaystate for the moment */
2948 ds = get_displaystate();
2952 if (display_type == DT_DEFAULT && !display_remote) {
2953 #if defined(CONFIG_SDL) || defined(CONFIG_COCOA)
2954 display_type = DT_SDL;
2956 vnc_display = "localhost:0,to=99";
2962 /* init local displays */
2963 switch (display_type) {
2966 #if defined(CONFIG_CURSES)
2968 curses_display_init(ds, full_screen);
2971 #if defined(CONFIG_SDL)
2973 sdl_display_init(ds, full_screen, no_frame);
2975 #elif defined(CONFIG_COCOA)
2977 cocoa_display_init(ds, full_screen);
2984 /* init remote displays */
2986 vnc_display_init(ds);
2987 if (vnc_display_open(ds, vnc_display) < 0)
2990 if (show_vnc_port) {
2991 printf("VNC server running on `%s'\n", vnc_display_local_addr(ds));
2996 qemu_spice_display_init(ds);
3002 dcl = ds->listeners;
3003 while (dcl != NULL) {
3004 if (dcl->dpy_refresh != NULL) {
3005 ds->gui_timer = qemu_new_timer(rt_clock, gui_update, ds);
3006 qemu_mod_timer(ds->gui_timer, qemu_get_clock(rt_clock));
3011 if (ds->gui_timer == NULL) {
3012 nographic_timer = qemu_new_timer(rt_clock, nographic_update, NULL);
3013 qemu_mod_timer(nographic_timer, qemu_get_clock(rt_clock));
3015 text_consoles_set_display(ds);
3017 if (gdbstub_dev && gdbserver_start(gdbstub_dev) < 0) {
3018 fprintf(stderr, "qemu: could not open gdbserver on device '%s'\n",
3023 qdev_machine_creation_done();
3025 if (rom_load_all() != 0) {
3026 fprintf(stderr, "rom loading failed\n");
3030 qemu_register_reset((void *)qbus_reset_all, sysbus_get_default());
3031 qemu_system_reset();
3033 if (load_vmstate(loadvm) < 0) {
3039 int ret = qemu_start_incoming_migration(incoming);
3041 fprintf(stderr, "Migration failed. Exit code %s(%d), exiting.\n",
3045 } else if (autostart) {