4 * Copyright (c) 2003-2008 Fabrice Bellard
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
32 /* Needed early for CONFIG_BSD etc. */
33 #include "config-host.h"
37 #include <sys/times.h>
41 #include <sys/ioctl.h>
42 #include <sys/resource.h>
43 #include <sys/socket.h>
44 #include <netinet/in.h>
46 #include <arpa/inet.h>
49 #include <sys/select.h>
53 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__DragonFly__)
55 #include <sys/sysctl.h>
64 #include <linux/ppdev.h>
65 #include <linux/parport.h>
69 #include <sys/ethernet.h>
70 #include <sys/sockio.h>
71 #include <netinet/arp.h>
72 #include <netinet/in_systm.h>
73 #include <netinet/ip.h>
74 #include <netinet/ip_icmp.h> // must come after ip.h
75 #include <netinet/udp.h>
76 #include <netinet/tcp.h>
84 #if defined(__OpenBSD__)
88 #if defined(CONFIG_VDE)
89 #include <libvdeplug.h>
97 #if defined(__APPLE__) || defined(main)
99 int qemu_main(int argc, char **argv, char **envp);
100 int main(int argc, char **argv)
102 return qemu_main(argc, argv, NULL);
105 #define main qemu_main
107 #endif /* CONFIG_SDL */
111 #define main qemu_main
112 #endif /* CONFIG_COCOA */
117 #include "hw/boards.h"
119 #include "hw/pcmcia.h"
124 #include "hw/watchdog.h"
125 #include "hw/smbios.h"
128 #include "hw/loader.h"
131 #include "net/slirp.h"
136 #include "qemu-timer.h"
137 #include "qemu-char.h"
138 #include "cache-utils.h"
140 #include "blockdev.h"
141 #include "block-migration.h"
143 #include "audio/audio.h"
144 #include "migration.h"
146 #include "qemu-option.h"
147 #include "qemu-config.h"
148 #include "qemu-objects.h"
149 #include "qemu-options.h"
151 #include "fsdev/qemu-fsdev.h"
156 #include "qemu_socket.h"
158 #include "slirp/libslirp.h"
161 #include "trace/control.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];
189 static int rtc_utc = 1;
190 static int rtc_date_offset = -1; /* -1 means no change */
191 QEMUClock *rtc_clock;
192 int vga_interface_type = VGA_NONE;
193 static int full_screen = 0;
195 static int no_frame = 0;
198 CharDriverState *serial_hds[MAX_SERIAL_PORTS];
199 CharDriverState *parallel_hds[MAX_PARALLEL_PORTS];
200 CharDriverState *virtcon_hds[MAX_VIRTIO_CONSOLES];
201 int win2k_install_hack = 0;
210 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];
231 uint8_t *boot_splash_filedata;
232 int boot_splash_filedata_size;
233 uint8_t qemu_extra_params_fw[2];
235 typedef struct FWBootEntry FWBootEntry;
238 QTAILQ_ENTRY(FWBootEntry) link;
244 QTAILQ_HEAD(, FWBootEntry) fw_boot_order = QTAILQ_HEAD_INITIALIZER(fw_boot_order);
247 uint64_t node_mem[MAX_NODES];
248 uint64_t node_cpumask[MAX_NODES];
250 static QEMUTimer *nographic_timer;
252 uint8_t qemu_uuid[16];
254 static QEMUBootSetHandler *boot_set_handler;
255 static void *boot_set_opaque;
257 static NotifierList exit_notifiers =
258 NOTIFIER_LIST_INITIALIZER(exit_notifiers);
260 static NotifierList machine_init_done_notifiers =
261 NOTIFIER_LIST_INITIALIZER(machine_init_done_notifiers);
263 static int tcg_allowed = 1;
267 enum xen_mode xen_mode = XEN_EMULATE;
268 static int tcg_tb_size;
270 static int default_serial = 1;
271 static int default_parallel = 1;
272 static int default_virtcon = 1;
273 static int default_monitor = 1;
274 static int default_vga = 1;
275 static int default_floppy = 1;
276 static int default_cdrom = 1;
277 static int default_sdcard = 1;
283 { .driver = "isa-serial", .flag = &default_serial },
284 { .driver = "isa-parallel", .flag = &default_parallel },
285 { .driver = "isa-fdc", .flag = &default_floppy },
286 { .driver = "ide-cd", .flag = &default_cdrom },
287 { .driver = "ide-hd", .flag = &default_cdrom },
288 { .driver = "ide-drive", .flag = &default_cdrom },
289 { .driver = "scsi-cd", .flag = &default_cdrom },
290 { .driver = "virtio-serial-pci", .flag = &default_virtcon },
291 { .driver = "virtio-serial-s390", .flag = &default_virtcon },
292 { .driver = "virtio-serial", .flag = &default_virtcon },
293 { .driver = "VGA", .flag = &default_vga },
294 { .driver = "cirrus-vga", .flag = &default_vga },
295 { .driver = "vmware-svga", .flag = &default_vga },
296 { .driver = "isa-vga", .flag = &default_vga },
297 { .driver = "qxl-vga", .flag = &default_vga },
300 static void res_free(void)
302 if (boot_splash_filedata != NULL) {
303 g_free(boot_splash_filedata);
304 boot_splash_filedata = NULL;
308 static int default_driver_check(QemuOpts *opts, void *opaque)
310 const char *driver = qemu_opt_get(opts, "driver");
315 for (i = 0; i < ARRAY_SIZE(default_list); i++) {
316 if (strcmp(default_list[i].driver, driver) != 0)
318 *(default_list[i].flag) = 0;
323 /***********************************************************/
326 static RunState current_run_state = RSTATE_NO_STATE;
331 } RunStateTransition;
333 static const RunStateTransition runstate_transitions_def[] = {
335 { RSTATE_NO_STATE, RSTATE_RUNNING },
336 { RSTATE_NO_STATE, RSTATE_IN_MIGRATE },
337 { RSTATE_NO_STATE, RSTATE_PRE_LAUNCH },
339 { RSTATE_DEBUG, RSTATE_RUNNING },
341 { RSTATE_IN_MIGRATE, RSTATE_RUNNING },
342 { RSTATE_IN_MIGRATE, RSTATE_PRE_LAUNCH },
344 { RSTATE_PANICKED, RSTATE_PAUSED },
346 { RSTATE_IO_ERROR, RSTATE_RUNNING },
348 { RSTATE_PAUSED, RSTATE_RUNNING },
350 { RSTATE_POST_MIGRATE, RSTATE_RUNNING },
352 { RSTATE_PRE_LAUNCH, RSTATE_RUNNING },
353 { RSTATE_PRE_LAUNCH, RSTATE_POST_MIGRATE },
355 { RSTATE_PRE_MIGRATE, RSTATE_RUNNING },
356 { RSTATE_PRE_MIGRATE, RSTATE_POST_MIGRATE },
358 { RSTATE_RESTORE, RSTATE_RUNNING },
360 { RSTATE_RUNNING, RSTATE_DEBUG },
361 { RSTATE_RUNNING, RSTATE_PANICKED },
362 { RSTATE_RUNNING, RSTATE_IO_ERROR },
363 { RSTATE_RUNNING, RSTATE_PAUSED },
364 { RSTATE_RUNNING, RSTATE_PRE_MIGRATE },
365 { RSTATE_RUNNING, RSTATE_RESTORE },
366 { RSTATE_RUNNING, RSTATE_SAVEVM },
367 { RSTATE_RUNNING, RSTATE_SHUTDOWN },
368 { RSTATE_RUNNING, RSTATE_WATCHDOG },
370 { RSTATE_SAVEVM, RSTATE_RUNNING },
372 { RSTATE_SHUTDOWN, RSTATE_PAUSED },
374 { RSTATE_WATCHDOG, RSTATE_RUNNING },
376 { RSTATE_MAX, RSTATE_MAX },
379 static bool runstate_valid_transitions[RSTATE_MAX][RSTATE_MAX];
381 static const char *const runstate_name_tbl[RSTATE_MAX] = {
382 [RSTATE_DEBUG] = "debug",
383 [RSTATE_IN_MIGRATE] = "incoming-migration",
384 [RSTATE_PANICKED] = "internal-error",
385 [RSTATE_IO_ERROR] = "io-error",
386 [RSTATE_PAUSED] = "paused",
387 [RSTATE_POST_MIGRATE] = "post-migrate",
388 [RSTATE_PRE_LAUNCH] = "prelaunch",
389 [RSTATE_PRE_MIGRATE] = "finish-migrate",
390 [RSTATE_RESTORE] = "restore-vm",
391 [RSTATE_RUNNING] = "running",
392 [RSTATE_SAVEVM] = "save-vm",
393 [RSTATE_SHUTDOWN] = "shutdown",
394 [RSTATE_WATCHDOG] = "watchdog",
397 bool runstate_check(RunState state)
399 return current_run_state == state;
402 void runstate_init(void)
404 const RunStateTransition *p;
406 memset(&runstate_valid_transitions, 0, sizeof(runstate_valid_transitions));
408 for (p = &runstate_transitions_def[0]; p->from != RSTATE_MAX; p++) {
409 runstate_valid_transitions[p->from][p->to] = true;
413 /* This function will abort() on invalid state transitions */
414 void runstate_set(RunState new_state)
416 if (new_state >= RSTATE_MAX ||
417 !runstate_valid_transitions[current_run_state][new_state]) {
418 fprintf(stderr, "invalid runstate transition\n");
422 current_run_state = new_state;
425 const char *runstate_as_string(void)
427 assert(current_run_state > RSTATE_NO_STATE &&
428 current_run_state < RSTATE_MAX);
429 return runstate_name_tbl[current_run_state];
432 int runstate_is_running(void)
434 return runstate_check(RSTATE_RUNNING);
437 /***********************************************************/
438 /* real time host monotonic timer */
440 /***********************************************************/
441 /* host time/date access */
442 void qemu_get_timedate(struct tm *tm, int offset)
449 if (rtc_date_offset == -1) {
453 ret = localtime(&ti);
455 ti -= rtc_date_offset;
459 memcpy(tm, ret, sizeof(struct tm));
462 int qemu_timedate_diff(struct tm *tm)
466 if (rtc_date_offset == -1)
468 seconds = mktimegm(tm);
470 seconds = mktime(tm);
472 seconds = mktimegm(tm) + rtc_date_offset;
474 return seconds - time(NULL);
477 void rtc_change_mon_event(struct tm *tm)
481 data = qobject_from_jsonf("{ 'offset': %d }", qemu_timedate_diff(tm));
482 monitor_protocol_event(QEVENT_RTC_CHANGE, data);
483 qobject_decref(data);
486 static void configure_rtc_date_offset(const char *startdate, int legacy)
488 time_t rtc_start_date;
491 if (!strcmp(startdate, "now") && legacy) {
492 rtc_date_offset = -1;
494 if (sscanf(startdate, "%d-%d-%dT%d:%d:%d",
502 } else if (sscanf(startdate, "%d-%d-%d",
514 rtc_start_date = mktimegm(&tm);
515 if (rtc_start_date == -1) {
517 fprintf(stderr, "Invalid date format. Valid formats are:\n"
518 "'2006-06-17T16:01:21' or '2006-06-17'\n");
521 rtc_date_offset = time(NULL) - rtc_start_date;
525 static void configure_rtc(QemuOpts *opts)
529 value = qemu_opt_get(opts, "base");
531 if (!strcmp(value, "utc")) {
533 } else if (!strcmp(value, "localtime")) {
536 configure_rtc_date_offset(value, 0);
539 value = qemu_opt_get(opts, "clock");
541 if (!strcmp(value, "host")) {
542 rtc_clock = host_clock;
543 } else if (!strcmp(value, "vm")) {
544 rtc_clock = vm_clock;
546 fprintf(stderr, "qemu: invalid option value '%s'\n", value);
550 value = qemu_opt_get(opts, "driftfix");
552 if (!strcmp(value, "slew")) {
554 } else if (!strcmp(value, "none")) {
557 fprintf(stderr, "qemu: invalid option value '%s'\n", value);
563 /***********************************************************/
564 /* Bluetooth support */
567 static struct HCIInfo *hci_table[MAX_NICS];
569 static struct bt_vlan_s {
570 struct bt_scatternet_s net;
572 struct bt_vlan_s *next;
575 /* find or alloc a new bluetooth "VLAN" */
576 static struct bt_scatternet_s *qemu_find_bt_vlan(int id)
578 struct bt_vlan_s **pvlan, *vlan;
579 for (vlan = first_bt_vlan; vlan != NULL; vlan = vlan->next) {
583 vlan = g_malloc0(sizeof(struct bt_vlan_s));
585 pvlan = &first_bt_vlan;
586 while (*pvlan != NULL)
587 pvlan = &(*pvlan)->next;
592 static void null_hci_send(struct HCIInfo *hci, const uint8_t *data, int len)
596 static int null_hci_addr_set(struct HCIInfo *hci, const uint8_t *bd_addr)
601 static struct HCIInfo null_hci = {
602 .cmd_send = null_hci_send,
603 .sco_send = null_hci_send,
604 .acl_send = null_hci_send,
605 .bdaddr_set = null_hci_addr_set,
608 struct HCIInfo *qemu_next_hci(void)
610 if (cur_hci == nb_hcis)
613 return hci_table[cur_hci++];
616 static struct HCIInfo *hci_init(const char *str)
619 struct bt_scatternet_s *vlan = 0;
621 if (!strcmp(str, "null"))
624 else if (!strncmp(str, "host", 4) && (str[4] == '\0' || str[4] == ':'))
626 return bt_host_hci(str[4] ? str + 5 : "hci0");
627 else if (!strncmp(str, "hci", 3)) {
630 if (!strncmp(str + 3, ",vlan=", 6)) {
631 vlan = qemu_find_bt_vlan(strtol(str + 9, &endp, 0));
636 vlan = qemu_find_bt_vlan(0);
638 return bt_new_hci(vlan);
641 fprintf(stderr, "qemu: Unknown bluetooth HCI `%s'.\n", str);
646 static int bt_hci_parse(const char *str)
651 if (nb_hcis >= MAX_NICS) {
652 fprintf(stderr, "qemu: Too many bluetooth HCIs (max %i).\n", MAX_NICS);
665 bdaddr.b[5] = 0x56 + nb_hcis;
666 hci->bdaddr_set(hci, bdaddr.b);
668 hci_table[nb_hcis++] = hci;
673 static void bt_vhci_add(int vlan_id)
675 struct bt_scatternet_s *vlan = qemu_find_bt_vlan(vlan_id);
678 fprintf(stderr, "qemu: warning: adding a VHCI to "
679 "an empty scatternet %i\n", vlan_id);
681 bt_vhci_init(bt_new_hci(vlan));
684 static struct bt_device_s *bt_device_add(const char *opt)
686 struct bt_scatternet_s *vlan;
688 char *endp = strstr(opt, ",vlan=");
689 int len = (endp ? endp - opt : strlen(opt)) + 1;
692 pstrcpy(devname, MIN(sizeof(devname), len), opt);
695 vlan_id = strtol(endp + 6, &endp, 0);
697 fprintf(stderr, "qemu: unrecognised bluetooth vlan Id\n");
702 vlan = qemu_find_bt_vlan(vlan_id);
705 fprintf(stderr, "qemu: warning: adding a slave device to "
706 "an empty scatternet %i\n", vlan_id);
708 if (!strcmp(devname, "keyboard"))
709 return bt_keyboard_init(vlan);
711 fprintf(stderr, "qemu: unsupported bluetooth device `%s'\n", devname);
715 static int bt_parse(const char *opt)
717 const char *endp, *p;
720 if (strstart(opt, "hci", &endp)) {
721 if (!*endp || *endp == ',') {
723 if (!strstart(endp, ",vlan=", 0))
726 return bt_hci_parse(opt);
728 } else if (strstart(opt, "vhci", &endp)) {
729 if (!*endp || *endp == ',') {
731 if (strstart(endp, ",vlan=", &p)) {
732 vlan = strtol(p, (char **) &endp, 0);
734 fprintf(stderr, "qemu: bad scatternet '%s'\n", p);
738 fprintf(stderr, "qemu: bad parameter '%s'\n", endp + 1);
747 } else if (strstart(opt, "device:", &endp))
748 return !bt_device_add(endp);
750 fprintf(stderr, "qemu: bad bluetooth parameter '%s'\n", opt);
754 /***********************************************************/
755 /* QEMU Block devices */
757 #define HD_OPTS "media=disk"
758 #define CDROM_OPTS "media=cdrom"
760 #define PFLASH_OPTS ""
764 static int drive_init_func(QemuOpts *opts, void *opaque)
766 int *use_scsi = opaque;
768 return drive_init(opts, *use_scsi) == NULL;
771 static int drive_enable_snapshot(QemuOpts *opts, void *opaque)
773 if (NULL == qemu_opt_get(opts, "snapshot")) {
774 qemu_opt_set(opts, "snapshot", "on");
779 static void default_drive(int enable, int snapshot, int use_scsi,
780 BlockInterfaceType type, int index,
785 if (type == IF_DEFAULT) {
786 type = use_scsi ? IF_SCSI : IF_IDE;
789 if (!enable || drive_get_by_index(type, index)) {
793 opts = drive_add(type, index, NULL, optstr);
795 drive_enable_snapshot(opts, NULL);
797 if (!drive_init(opts, use_scsi)) {
802 void qemu_register_boot_set(QEMUBootSetHandler *func, void *opaque)
804 boot_set_handler = func;
805 boot_set_opaque = opaque;
808 int qemu_boot_set(const char *boot_devices)
810 if (!boot_set_handler) {
813 return boot_set_handler(boot_set_opaque, boot_devices);
816 static void validate_bootdevices(char *devices)
818 /* We just do some generic consistency checks */
822 for (p = devices; *p != '\0'; p++) {
823 /* Allowed boot devices are:
824 * a-b: floppy disk drives
825 * c-f: IDE disk drives
826 * g-m: machine implementation dependant drives
827 * n-p: network devices
828 * It's up to each machine implementation to check if the given boot
829 * devices match the actual hardware implementation and firmware
832 if (*p < 'a' || *p > 'p') {
833 fprintf(stderr, "Invalid boot device '%c'\n", *p);
836 if (bitmap & (1 << (*p - 'a'))) {
837 fprintf(stderr, "Boot device '%c' was given twice\n", *p);
840 bitmap |= 1 << (*p - 'a');
844 static void restore_boot_devices(void *opaque)
846 char *standard_boot_devices = opaque;
847 static int first = 1;
849 /* Restore boot order and remove ourselves after the first boot */
855 qemu_boot_set(standard_boot_devices);
857 qemu_unregister_reset(restore_boot_devices, standard_boot_devices);
858 g_free(standard_boot_devices);
861 void add_boot_device_path(int32_t bootindex, DeviceState *dev,
864 FWBootEntry *node, *i;
870 assert(dev != NULL || suffix != NULL);
872 node = g_malloc0(sizeof(FWBootEntry));
873 node->bootindex = bootindex;
874 node->suffix = suffix ? g_strdup(suffix) : NULL;
877 QTAILQ_FOREACH(i, &fw_boot_order, link) {
878 if (i->bootindex == bootindex) {
879 fprintf(stderr, "Two devices with same boot index %d\n", bootindex);
881 } else if (i->bootindex < bootindex) {
884 QTAILQ_INSERT_BEFORE(i, node, link);
887 QTAILQ_INSERT_TAIL(&fw_boot_order, node, link);
891 * This function returns null terminated string that consist of new line
892 * separated device paths.
894 * memory pointed by "size" is assigned total length of the array in bytes
897 char *get_boot_devices_list(uint32_t *size)
903 QTAILQ_FOREACH(i, &fw_boot_order, link) {
904 char *devpath = NULL, *bootpath;
908 devpath = qdev_get_fw_dev_path(i->dev);
912 if (i->suffix && devpath) {
913 size_t bootpathlen = strlen(devpath) + strlen(i->suffix) + 1;
915 bootpath = g_malloc(bootpathlen);
916 snprintf(bootpath, bootpathlen, "%s%s", devpath, i->suffix);
918 } else if (devpath) {
921 bootpath = g_strdup(i->suffix);
926 list[total-1] = '\n';
928 len = strlen(bootpath) + 1;
929 list = g_realloc(list, total + len);
930 memcpy(&list[total], bootpath, len);
940 static void numa_add(const char *optarg)
944 unsigned long long value, endvalue;
947 optarg = get_opt_name(option, 128, optarg, ',') + 1;
948 if (!strcmp(option, "node")) {
949 if (get_param_value(option, 128, "nodeid", optarg) == 0) {
950 nodenr = nb_numa_nodes;
952 nodenr = strtoull(option, NULL, 10);
955 if (get_param_value(option, 128, "mem", optarg) == 0) {
956 node_mem[nodenr] = 0;
959 sval = strtosz(option, NULL);
961 fprintf(stderr, "qemu: invalid numa mem size: %s\n", optarg);
964 node_mem[nodenr] = sval;
966 if (get_param_value(option, 128, "cpus", optarg) == 0) {
967 node_cpumask[nodenr] = 0;
969 value = strtoull(option, &endptr, 10);
972 fprintf(stderr, "only 64 CPUs in NUMA mode supported.\n");
974 if (*endptr == '-') {
975 endvalue = strtoull(endptr+1, &endptr, 10);
976 if (endvalue >= 63) {
979 "only 63 CPUs in NUMA mode supported.\n");
981 value = (2ULL << endvalue) - (1ULL << value);
983 value = 1ULL << value;
986 node_cpumask[nodenr] = value;
993 static void smp_parse(const char *optarg)
995 int smp, sockets = 0, threads = 0, cores = 0;
999 smp = strtoul(optarg, &endptr, 10);
1000 if (endptr != optarg) {
1001 if (*endptr == ',') {
1005 if (get_param_value(option, 128, "sockets", endptr) != 0)
1006 sockets = strtoull(option, NULL, 10);
1007 if (get_param_value(option, 128, "cores", endptr) != 0)
1008 cores = strtoull(option, NULL, 10);
1009 if (get_param_value(option, 128, "threads", endptr) != 0)
1010 threads = strtoull(option, NULL, 10);
1011 if (get_param_value(option, 128, "maxcpus", endptr) != 0)
1012 max_cpus = strtoull(option, NULL, 10);
1014 /* compute missing values, prefer sockets over cores over threads */
1015 if (smp == 0 || sockets == 0) {
1016 sockets = sockets > 0 ? sockets : 1;
1017 cores = cores > 0 ? cores : 1;
1018 threads = threads > 0 ? threads : 1;
1020 smp = cores * threads * sockets;
1024 threads = threads > 0 ? threads : 1;
1025 cores = smp / (sockets * threads);
1027 threads = smp / (cores * sockets);
1031 smp_cores = cores > 0 ? cores : 1;
1032 smp_threads = threads > 0 ? threads : 1;
1034 max_cpus = smp_cpus;
1037 /***********************************************************/
1040 static int usb_device_add(const char *devname)
1043 USBDevice *dev = NULL;
1048 /* drivers with .usbdevice_name entry in USBDeviceInfo */
1049 dev = usbdevice_create(devname);
1053 /* the other ones */
1054 #ifndef CONFIG_LINUX
1055 /* only the linux version is qdev-ified, usb-bsd still needs this */
1056 if (strstart(devname, "host:", &p)) {
1057 dev = usb_host_device_open(p);
1060 if (!strcmp(devname, "bt") || strstart(devname, "bt:", &p)) {
1061 dev = usb_bt_init(devname[2] ? hci_init(p) :
1062 bt_new_hci(qemu_find_bt_vlan(0)));
1073 static int usb_device_del(const char *devname)
1078 if (strstart(devname, "host:", &p))
1079 return usb_host_device_close(p);
1084 p = strchr(devname, '.');
1087 bus_num = strtoul(devname, NULL, 0);
1088 addr = strtoul(p + 1, NULL, 0);
1090 return usb_device_delete_addr(bus_num, addr);
1093 static int usb_parse(const char *cmdline)
1096 r = usb_device_add(cmdline);
1098 fprintf(stderr, "qemu: could not add USB device '%s'\n", cmdline);
1103 void do_usb_add(Monitor *mon, const QDict *qdict)
1105 const char *devname = qdict_get_str(qdict, "devname");
1106 if (usb_device_add(devname) < 0) {
1107 error_report("could not add USB device '%s'", devname);
1111 void do_usb_del(Monitor *mon, const QDict *qdict)
1113 const char *devname = qdict_get_str(qdict, "devname");
1114 if (usb_device_del(devname) < 0) {
1115 error_report("could not delete USB device '%s'", devname);
1119 /***********************************************************/
1120 /* PCMCIA/Cardbus */
1122 static struct pcmcia_socket_entry_s {
1123 PCMCIASocket *socket;
1124 struct pcmcia_socket_entry_s *next;
1125 } *pcmcia_sockets = 0;
1127 void pcmcia_socket_register(PCMCIASocket *socket)
1129 struct pcmcia_socket_entry_s *entry;
1131 entry = g_malloc(sizeof(struct pcmcia_socket_entry_s));
1132 entry->socket = socket;
1133 entry->next = pcmcia_sockets;
1134 pcmcia_sockets = entry;
1137 void pcmcia_socket_unregister(PCMCIASocket *socket)
1139 struct pcmcia_socket_entry_s *entry, **ptr;
1141 ptr = &pcmcia_sockets;
1142 for (entry = *ptr; entry; ptr = &entry->next, entry = *ptr)
1143 if (entry->socket == socket) {
1149 void pcmcia_info(Monitor *mon)
1151 struct pcmcia_socket_entry_s *iter;
1153 if (!pcmcia_sockets)
1154 monitor_printf(mon, "No PCMCIA sockets\n");
1156 for (iter = pcmcia_sockets; iter; iter = iter->next)
1157 monitor_printf(mon, "%s: %s\n", iter->socket->slot_string,
1158 iter->socket->attached ? iter->socket->card_string :
1162 /***********************************************************/
1163 /* machine registration */
1165 static QEMUMachine *first_machine = NULL;
1166 QEMUMachine *current_machine = NULL;
1168 int qemu_register_machine(QEMUMachine *m)
1171 pm = &first_machine;
1179 static QEMUMachine *find_machine(const char *name)
1183 for(m = first_machine; m != NULL; m = m->next) {
1184 if (!strcmp(m->name, name))
1186 if (m->alias && !strcmp(m->alias, name))
1192 static QEMUMachine *find_default_machine(void)
1196 for(m = first_machine; m != NULL; m = m->next) {
1197 if (m->is_default) {
1204 /***********************************************************/
1205 /* main execution loop */
1207 static void gui_update(void *opaque)
1209 uint64_t interval = GUI_REFRESH_INTERVAL;
1210 DisplayState *ds = opaque;
1211 DisplayChangeListener *dcl = ds->listeners;
1213 qemu_flush_coalesced_mmio_buffer();
1216 while (dcl != NULL) {
1217 if (dcl->gui_timer_interval &&
1218 dcl->gui_timer_interval < interval)
1219 interval = dcl->gui_timer_interval;
1222 qemu_mod_timer(ds->gui_timer, interval + qemu_get_clock_ms(rt_clock));
1225 static void nographic_update(void *opaque)
1227 uint64_t interval = GUI_REFRESH_INTERVAL;
1229 qemu_flush_coalesced_mmio_buffer();
1230 qemu_mod_timer(nographic_timer, interval + qemu_get_clock_ms(rt_clock));
1233 struct vm_change_state_entry {
1234 VMChangeStateHandler *cb;
1236 QLIST_ENTRY (vm_change_state_entry) entries;
1239 static QLIST_HEAD(vm_change_state_head, vm_change_state_entry) vm_change_state_head;
1241 VMChangeStateEntry *qemu_add_vm_change_state_handler(VMChangeStateHandler *cb,
1244 VMChangeStateEntry *e;
1246 e = g_malloc0(sizeof (*e));
1250 QLIST_INSERT_HEAD(&vm_change_state_head, e, entries);
1254 void qemu_del_vm_change_state_handler(VMChangeStateEntry *e)
1256 QLIST_REMOVE (e, entries);
1260 void vm_state_notify(int running, RunState state)
1262 VMChangeStateEntry *e;
1264 trace_vm_state_notify(running, state);
1266 for (e = vm_change_state_head.lh_first; e; e = e->entries.le_next) {
1267 e->cb(e->opaque, running, state);
1273 if (!runstate_is_running()) {
1275 runstate_set(RSTATE_RUNNING);
1276 vm_state_notify(1, RSTATE_RUNNING);
1278 monitor_protocol_event(QEVENT_RESUME, NULL);
1282 /* reset/shutdown handler */
1284 typedef struct QEMUResetEntry {
1285 QTAILQ_ENTRY(QEMUResetEntry) entry;
1286 QEMUResetHandler *func;
1290 static QTAILQ_HEAD(reset_handlers, QEMUResetEntry) reset_handlers =
1291 QTAILQ_HEAD_INITIALIZER(reset_handlers);
1292 static int reset_requested;
1293 static int shutdown_requested, shutdown_signal = -1;
1294 static pid_t shutdown_pid;
1295 static int powerdown_requested;
1296 static int debug_requested;
1297 static RunState vmstop_requested = RSTATE_NO_STATE;
1299 int qemu_shutdown_requested_get(void)
1301 return shutdown_requested;
1304 int qemu_reset_requested_get(void)
1306 return reset_requested;
1309 int qemu_shutdown_requested(void)
1311 int r = shutdown_requested;
1312 shutdown_requested = 0;
1316 void qemu_kill_report(void)
1318 if (shutdown_signal != -1) {
1319 fprintf(stderr, "qemu: terminating on signal %d", shutdown_signal);
1320 if (shutdown_pid == 0) {
1321 /* This happens for eg ^C at the terminal, so it's worth
1322 * avoiding printing an odd message in that case.
1324 fputc('\n', stderr);
1326 fprintf(stderr, " from pid " FMT_pid "\n", shutdown_pid);
1328 shutdown_signal = -1;
1332 int qemu_reset_requested(void)
1334 int r = reset_requested;
1335 reset_requested = 0;
1339 int qemu_powerdown_requested(void)
1341 int r = powerdown_requested;
1342 powerdown_requested = 0;
1346 static int qemu_debug_requested(void)
1348 int r = debug_requested;
1349 debug_requested = 0;
1353 static RunState qemu_vmstop_requested(void)
1355 RunState s = vmstop_requested;
1356 vmstop_requested = RSTATE_NO_STATE;
1360 void qemu_register_reset(QEMUResetHandler *func, void *opaque)
1362 QEMUResetEntry *re = g_malloc0(sizeof(QEMUResetEntry));
1365 re->opaque = opaque;
1366 QTAILQ_INSERT_TAIL(&reset_handlers, re, entry);
1369 void qemu_unregister_reset(QEMUResetHandler *func, void *opaque)
1373 QTAILQ_FOREACH(re, &reset_handlers, entry) {
1374 if (re->func == func && re->opaque == opaque) {
1375 QTAILQ_REMOVE(&reset_handlers, re, entry);
1382 void qemu_system_reset(bool report)
1384 QEMUResetEntry *re, *nre;
1386 /* reset all devices */
1387 QTAILQ_FOREACH_SAFE(re, &reset_handlers, entry, nre) {
1388 re->func(re->opaque);
1391 monitor_protocol_event(QEVENT_RESET, NULL);
1393 cpu_synchronize_all_post_reset();
1396 void qemu_system_reset_request(void)
1399 shutdown_requested = 1;
1401 reset_requested = 1;
1404 qemu_notify_event();
1407 void qemu_system_killed(int signal, pid_t pid)
1409 shutdown_signal = signal;
1412 qemu_system_shutdown_request();
1415 void qemu_system_shutdown_request(void)
1417 shutdown_requested = 1;
1418 qemu_notify_event();
1421 void qemu_system_powerdown_request(void)
1423 powerdown_requested = 1;
1424 qemu_notify_event();
1427 void qemu_system_debug_request(void)
1429 debug_requested = 1;
1430 qemu_notify_event();
1433 void qemu_system_vmstop_request(RunState state)
1435 vmstop_requested = state;
1436 qemu_notify_event();
1439 static GPollFD poll_fds[1024 * 2]; /* this is probably overkill */
1440 static int n_poll_fds;
1441 static int max_priority;
1443 static void glib_select_fill(int *max_fd, fd_set *rfds, fd_set *wfds,
1444 fd_set *xfds, struct timeval *tv)
1446 GMainContext *context = g_main_context_default();
1448 int timeout = 0, cur_timeout;
1450 g_main_context_prepare(context, &max_priority);
1452 n_poll_fds = g_main_context_query(context, max_priority, &timeout,
1453 poll_fds, ARRAY_SIZE(poll_fds));
1454 g_assert(n_poll_fds <= ARRAY_SIZE(poll_fds));
1456 for (i = 0; i < n_poll_fds; i++) {
1457 GPollFD *p = &poll_fds[i];
1459 if ((p->events & G_IO_IN)) {
1460 FD_SET(p->fd, rfds);
1461 *max_fd = MAX(*max_fd, p->fd);
1463 if ((p->events & G_IO_OUT)) {
1464 FD_SET(p->fd, wfds);
1465 *max_fd = MAX(*max_fd, p->fd);
1467 if ((p->events & G_IO_ERR)) {
1468 FD_SET(p->fd, xfds);
1469 *max_fd = MAX(*max_fd, p->fd);
1473 cur_timeout = (tv->tv_sec * 1000) + ((tv->tv_usec + 500) / 1000);
1474 if (timeout >= 0 && timeout < cur_timeout) {
1475 tv->tv_sec = timeout / 1000;
1476 tv->tv_usec = (timeout % 1000) * 1000;
1480 static void glib_select_poll(fd_set *rfds, fd_set *wfds, fd_set *xfds,
1483 GMainContext *context = g_main_context_default();
1488 for (i = 0; i < n_poll_fds; i++) {
1489 GPollFD *p = &poll_fds[i];
1491 if ((p->events & G_IO_IN) && FD_ISSET(p->fd, rfds)) {
1492 p->revents |= G_IO_IN;
1494 if ((p->events & G_IO_OUT) && FD_ISSET(p->fd, wfds)) {
1495 p->revents |= G_IO_OUT;
1497 if ((p->events & G_IO_ERR) && FD_ISSET(p->fd, xfds)) {
1498 p->revents |= G_IO_ERR;
1503 if (g_main_context_check(context, max_priority, poll_fds, n_poll_fds)) {
1504 g_main_context_dispatch(context);
1508 int main_loop_wait(int nonblocking)
1510 fd_set rfds, wfds, xfds;
1518 timeout = qemu_calculate_timeout();
1519 qemu_bh_update_timeout(&timeout);
1522 os_host_main_loop_wait(&timeout);
1524 tv.tv_sec = timeout / 1000;
1525 tv.tv_usec = (timeout % 1000) * 1000;
1527 /* poll any events */
1528 /* XXX: separate device handlers from system ones */
1534 qemu_iohandler_fill(&nfds, &rfds, &wfds, &xfds);
1535 slirp_select_fill(&nfds, &rfds, &wfds, &xfds);
1536 glib_select_fill(&nfds, &rfds, &wfds, &xfds, &tv);
1539 qemu_mutex_unlock_iothread();
1542 ret = select(nfds + 1, &rfds, &wfds, &xfds, &tv);
1545 qemu_mutex_lock_iothread();
1548 qemu_iohandler_poll(&rfds, &wfds, &xfds, ret);
1549 slirp_select_poll(&rfds, &wfds, &xfds, (ret < 0));
1550 glib_select_poll(&rfds, &wfds, &xfds, (ret < 0));
1552 qemu_run_all_timers();
1554 /* Check bottom-halves last in case any of the earlier events triggered
1561 qemu_irq qemu_system_powerdown;
1563 static void main_loop(void)
1566 int last_io __attribute__ ((unused)) = 0;
1567 #ifdef CONFIG_PROFILER
1572 qemu_main_loop_start();
1575 nonblocking = !kvm_enabled() && last_io > 0;
1576 #ifdef CONFIG_PROFILER
1577 ti = profile_getclock();
1579 last_io = main_loop_wait(nonblocking);
1580 #ifdef CONFIG_PROFILER
1581 dev_time += profile_getclock() - ti;
1584 if (qemu_debug_requested()) {
1585 vm_stop(RSTATE_DEBUG);
1587 if (qemu_shutdown_requested()) {
1589 monitor_protocol_event(QEVENT_SHUTDOWN, NULL);
1591 vm_stop(RSTATE_SHUTDOWN);
1595 if (qemu_reset_requested()) {
1597 cpu_synchronize_all_states();
1598 qemu_system_reset(VMRESET_REPORT);
1600 if (runstate_check(RSTATE_PANICKED) ||
1601 runstate_check(RSTATE_SHUTDOWN)) {
1602 runstate_set(RSTATE_PAUSED);
1605 if (qemu_powerdown_requested()) {
1606 monitor_protocol_event(QEVENT_POWERDOWN, NULL);
1607 qemu_irq_raise(qemu_system_powerdown);
1609 if ((r = qemu_vmstop_requested())) {
1617 static void version(void)
1619 printf("QEMU emulator version " QEMU_VERSION QEMU_PKGVERSION ", Copyright (c) 2003-2008 Fabrice Bellard\n");
1622 static void help(int exitcode)
1624 const char *options_help =
1625 #define DEF(option, opt_arg, opt_enum, opt_help, arch_mask) \
1627 #define DEFHEADING(text) stringify(text) "\n"
1628 #include "qemu-options.def"
1634 printf("usage: %s [options] [disk_image]\n"
1636 "'disk_image' is a raw hard disk image for IDE hard disk 0\n"
1639 "During emulation, the following keys are useful:\n"
1640 "ctrl-alt-f toggle full screen\n"
1641 "ctrl-alt-n switch to virtual console 'n'\n"
1642 "ctrl-alt toggle mouse and keyboard grab\n"
1644 "When using -nographic, press 'ctrl-a h' to get some help.\n",
1650 #define HAS_ARG 0x0001
1652 typedef struct QEMUOption {
1659 static const QEMUOption qemu_options[] = {
1660 { "h", 0, QEMU_OPTION_h, QEMU_ARCH_ALL },
1661 #define DEF(option, opt_arg, opt_enum, opt_help, arch_mask) \
1662 { option, opt_arg, opt_enum, arch_mask },
1663 #define DEFHEADING(text)
1664 #include "qemu-options.def"
1670 static void select_vgahw (const char *p)
1675 vga_interface_type = VGA_NONE;
1676 if (strstart(p, "std", &opts)) {
1677 vga_interface_type = VGA_STD;
1678 } else if (strstart(p, "cirrus", &opts)) {
1679 vga_interface_type = VGA_CIRRUS;
1680 } else if (strstart(p, "vmware", &opts)) {
1681 vga_interface_type = VGA_VMWARE;
1682 } else if (strstart(p, "xenfb", &opts)) {
1683 vga_interface_type = VGA_XENFB;
1684 } else if (strstart(p, "qxl", &opts)) {
1685 vga_interface_type = VGA_QXL;
1686 } else if (!strstart(p, "none", &opts)) {
1688 fprintf(stderr, "Unknown vga type: %s\n", p);
1692 const char *nextopt;
1694 if (strstart(opts, ",retrace=", &nextopt)) {
1696 if (strstart(opts, "dumb", &nextopt))
1697 vga_retrace_method = VGA_RETRACE_DUMB;
1698 else if (strstart(opts, "precise", &nextopt))
1699 vga_retrace_method = VGA_RETRACE_PRECISE;
1700 else goto invalid_vga;
1701 } else goto invalid_vga;
1706 static DisplayType select_display(const char *p)
1709 DisplayType display = DT_DEFAULT;
1711 if (strstart(p, "sdl", &opts)) {
1715 const char *nextopt;
1717 if (strstart(opts, ",frame=", &nextopt)) {
1719 if (strstart(opts, "on", &nextopt)) {
1721 } else if (strstart(opts, "off", &nextopt)) {
1724 goto invalid_sdl_args;
1726 } else if (strstart(opts, ",alt_grab=", &nextopt)) {
1728 if (strstart(opts, "on", &nextopt)) {
1730 } else if (strstart(opts, "off", &nextopt)) {
1733 goto invalid_sdl_args;
1735 } else if (strstart(opts, ",ctrl_grab=", &nextopt)) {
1737 if (strstart(opts, "on", &nextopt)) {
1739 } else if (strstart(opts, "off", &nextopt)) {
1742 goto invalid_sdl_args;
1744 } else if (strstart(opts, ",window_close=", &nextopt)) {
1746 if (strstart(opts, "on", &nextopt)) {
1748 } else if (strstart(opts, "off", &nextopt)) {
1751 goto invalid_sdl_args;
1755 fprintf(stderr, "Invalid SDL option string: %s\n", p);
1761 fprintf(stderr, "SDL support is disabled\n");
1764 } else if (strstart(p, "vnc", &opts)) {
1769 const char *nextopt;
1771 if (strstart(opts, "=", &nextopt)) {
1772 vnc_display = nextopt;
1776 fprintf(stderr, "VNC requires a display argument vnc=<display>\n");
1780 fprintf(stderr, "VNC support is disabled\n");
1783 } else if (strstart(p, "curses", &opts)) {
1784 #ifdef CONFIG_CURSES
1785 display = DT_CURSES;
1787 fprintf(stderr, "Curses support is disabled\n");
1790 } else if (strstart(p, "none", &opts)) {
1793 fprintf(stderr, "Unknown display type: %s\n", p);
1800 static int balloon_parse(const char *arg)
1804 if (strcmp(arg, "none") == 0) {
1808 if (!strncmp(arg, "virtio", 6)) {
1809 if (arg[6] == ',') {
1810 /* have params -> parse them */
1811 opts = qemu_opts_parse(qemu_find_opts("device"), arg+7, 0);
1815 /* create empty opts */
1816 opts = qemu_opts_create(qemu_find_opts("device"), NULL, 0);
1818 qemu_opt_set(opts, "driver", "virtio-balloon");
1825 char *qemu_find_file(int type, const char *name)
1831 /* If name contains path separators then try it as a straight path. */
1832 if ((strchr(name, '/') || strchr(name, '\\'))
1833 && access(name, R_OK) == 0) {
1834 return g_strdup(name);
1837 case QEMU_FILE_TYPE_BIOS:
1840 case QEMU_FILE_TYPE_KEYMAP:
1841 subdir = "keymaps/";
1846 len = strlen(data_dir) + strlen(name) + strlen(subdir) + 2;
1847 buf = g_malloc0(len);
1848 snprintf(buf, len, "%s/%s%s", data_dir, subdir, name);
1849 if (access(buf, R_OK)) {
1856 static int device_help_func(QemuOpts *opts, void *opaque)
1858 return qdev_device_help(opts);
1861 static int device_init_func(QemuOpts *opts, void *opaque)
1865 dev = qdev_device_add(opts);
1871 static int chardev_init_func(QemuOpts *opts, void *opaque)
1873 CharDriverState *chr;
1875 chr = qemu_chr_new_from_opts(opts, NULL);
1881 #ifdef CONFIG_VIRTFS
1882 static int fsdev_init_func(QemuOpts *opts, void *opaque)
1885 ret = qemu_fsdev_add(opts);
1891 static int mon_init_func(QemuOpts *opts, void *opaque)
1893 CharDriverState *chr;
1894 const char *chardev;
1898 mode = qemu_opt_get(opts, "mode");
1902 if (strcmp(mode, "readline") == 0) {
1903 flags = MONITOR_USE_READLINE;
1904 } else if (strcmp(mode, "control") == 0) {
1905 flags = MONITOR_USE_CONTROL;
1907 fprintf(stderr, "unknown monitor mode \"%s\"\n", mode);
1911 if (qemu_opt_get_bool(opts, "pretty", 0))
1912 flags |= MONITOR_USE_PRETTY;
1914 if (qemu_opt_get_bool(opts, "default", 0))
1915 flags |= MONITOR_IS_DEFAULT;
1917 chardev = qemu_opt_get(opts, "chardev");
1918 chr = qemu_chr_find(chardev);
1920 fprintf(stderr, "chardev \"%s\" not found\n", chardev);
1924 monitor_init(chr, flags);
1928 static void monitor_parse(const char *optarg, const char *mode)
1930 static int monitor_device_index = 0;
1936 if (strstart(optarg, "chardev:", &p)) {
1937 snprintf(label, sizeof(label), "%s", p);
1939 snprintf(label, sizeof(label), "compat_monitor%d",
1940 monitor_device_index);
1941 if (monitor_device_index == 0) {
1944 opts = qemu_chr_parse_compat(label, optarg);
1946 fprintf(stderr, "parse error: %s\n", optarg);
1951 opts = qemu_opts_create(qemu_find_opts("mon"), label, 1);
1953 fprintf(stderr, "duplicate chardev: %s\n", label);
1956 qemu_opt_set(opts, "mode", mode);
1957 qemu_opt_set(opts, "chardev", label);
1959 qemu_opt_set(opts, "default", "on");
1960 monitor_device_index++;
1963 struct device_config {
1965 DEV_USB, /* -usbdevice */
1967 DEV_SERIAL, /* -serial */
1968 DEV_PARALLEL, /* -parallel */
1969 DEV_VIRTCON, /* -virtioconsole */
1970 DEV_DEBUGCON, /* -debugcon */
1972 const char *cmdline;
1973 QTAILQ_ENTRY(device_config) next;
1975 QTAILQ_HEAD(, device_config) device_configs = QTAILQ_HEAD_INITIALIZER(device_configs);
1977 static void add_device_config(int type, const char *cmdline)
1979 struct device_config *conf;
1981 conf = g_malloc0(sizeof(*conf));
1983 conf->cmdline = cmdline;
1984 QTAILQ_INSERT_TAIL(&device_configs, conf, next);
1987 static int foreach_device_config(int type, int (*func)(const char *cmdline))
1989 struct device_config *conf;
1992 QTAILQ_FOREACH(conf, &device_configs, next) {
1993 if (conf->type != type)
1995 rc = func(conf->cmdline);
2002 static int serial_parse(const char *devname)
2004 static int index = 0;
2007 if (strcmp(devname, "none") == 0)
2009 if (index == MAX_SERIAL_PORTS) {
2010 fprintf(stderr, "qemu: too many serial ports\n");
2013 snprintf(label, sizeof(label), "serial%d", index);
2014 serial_hds[index] = qemu_chr_new(label, devname, NULL);
2015 if (!serial_hds[index]) {
2016 fprintf(stderr, "qemu: could not open serial device '%s': %s\n",
2017 devname, strerror(errno));
2024 static int parallel_parse(const char *devname)
2026 static int index = 0;
2029 if (strcmp(devname, "none") == 0)
2031 if (index == MAX_PARALLEL_PORTS) {
2032 fprintf(stderr, "qemu: too many parallel ports\n");
2035 snprintf(label, sizeof(label), "parallel%d", index);
2036 parallel_hds[index] = qemu_chr_new(label, devname, NULL);
2037 if (!parallel_hds[index]) {
2038 fprintf(stderr, "qemu: could not open parallel device '%s': %s\n",
2039 devname, strerror(errno));
2046 static int virtcon_parse(const char *devname)
2048 QemuOptsList *device = qemu_find_opts("device");
2049 static int index = 0;
2051 QemuOpts *bus_opts, *dev_opts;
2053 if (strcmp(devname, "none") == 0)
2055 if (index == MAX_VIRTIO_CONSOLES) {
2056 fprintf(stderr, "qemu: too many virtio consoles\n");
2060 bus_opts = qemu_opts_create(device, NULL, 0);
2061 qemu_opt_set(bus_opts, "driver", "virtio-serial");
2063 dev_opts = qemu_opts_create(device, NULL, 0);
2064 qemu_opt_set(dev_opts, "driver", "virtconsole");
2066 snprintf(label, sizeof(label), "virtcon%d", index);
2067 virtcon_hds[index] = qemu_chr_new(label, devname, NULL);
2068 if (!virtcon_hds[index]) {
2069 fprintf(stderr, "qemu: could not open virtio console '%s': %s\n",
2070 devname, strerror(errno));
2073 qemu_opt_set(dev_opts, "chardev", label);
2079 static int debugcon_parse(const char *devname)
2083 if (!qemu_chr_new("debugcon", devname, NULL)) {
2086 opts = qemu_opts_create(qemu_find_opts("device"), "debugcon", 1);
2088 fprintf(stderr, "qemu: already have a debugcon device\n");
2091 qemu_opt_set(opts, "driver", "isa-debugcon");
2092 qemu_opt_set(opts, "chardev", "debugcon");
2096 static QEMUMachine *machine_parse(const char *name)
2098 QEMUMachine *m, *machine = NULL;
2101 machine = find_machine(name);
2106 printf("Supported machines are:\n");
2107 for (m = first_machine; m != NULL; m = m->next) {
2109 printf("%-10s %s (alias of %s)\n", m->alias, m->desc, m->name);
2111 printf("%-10s %s%s\n", m->name, m->desc,
2112 m->is_default ? " (default)" : "");
2114 exit(!name || *name != '?');
2117 static int tcg_init(void)
2119 tcg_exec_init(tcg_tb_size * 1024 * 1024);
2124 const char *opt_name;
2126 int (*available)(void);
2130 { "tcg", "tcg", tcg_available, tcg_init, &tcg_allowed },
2131 { "xen", "Xen", xen_available, xen_init, &xen_allowed },
2132 { "kvm", "KVM", kvm_available, kvm_init, &kvm_allowed },
2135 static int configure_accelerator(void)
2137 const char *p = NULL;
2140 bool accel_initalised = 0;
2141 bool init_failed = 0;
2143 QemuOptsList *list = qemu_find_opts("machine");
2144 if (!QTAILQ_EMPTY(&list->head)) {
2145 p = qemu_opt_get(QTAILQ_FIRST(&list->head), "accel");
2149 /* Use the default "accelerator", tcg */
2153 while (!accel_initalised && *p != '\0') {
2157 p = get_opt_name(buf, sizeof (buf), p, ':');
2158 for (i = 0; i < ARRAY_SIZE(accel_list); i++) {
2159 if (strcmp(accel_list[i].opt_name, buf) == 0) {
2160 *(accel_list[i].allowed) = 1;
2161 ret = accel_list[i].init();
2164 if (!accel_list[i].available()) {
2165 printf("%s not supported for this target\n",
2166 accel_list[i].name);
2168 fprintf(stderr, "failed to initialize %s: %s\n",
2172 *(accel_list[i].allowed) = 0;
2174 accel_initalised = 1;
2179 if (i == ARRAY_SIZE(accel_list)) {
2180 fprintf(stderr, "\"%s\" accelerator does not exist.\n", buf);
2184 if (!accel_initalised) {
2185 fprintf(stderr, "No accelerator found!\n");
2190 fprintf(stderr, "Back to %s accelerator.\n", accel_list[i].name);
2193 return !accel_initalised;
2196 void qemu_add_exit_notifier(Notifier *notify)
2198 notifier_list_add(&exit_notifiers, notify);
2201 void qemu_remove_exit_notifier(Notifier *notify)
2203 notifier_list_remove(&exit_notifiers, notify);
2206 static void qemu_run_exit_notifiers(void)
2208 notifier_list_notify(&exit_notifiers, NULL);
2211 void qemu_add_machine_init_done_notifier(Notifier *notify)
2213 notifier_list_add(&machine_init_done_notifiers, notify);
2216 static void qemu_run_machine_init_done_notifiers(void)
2218 notifier_list_notify(&machine_init_done_notifiers, NULL);
2221 static const QEMUOption *lookup_opt(int argc, char **argv,
2222 const char **poptarg, int *poptind)
2224 const QEMUOption *popt;
2225 int optind = *poptind;
2226 char *r = argv[optind];
2229 loc_set_cmdline(argv, optind, 1);
2231 /* Treat --foo the same as -foo. */
2234 popt = qemu_options;
2237 error_report("invalid option");
2240 if (!strcmp(popt->name, r + 1))
2244 if (popt->flags & HAS_ARG) {
2245 if (optind >= argc) {
2246 error_report("requires an argument");
2249 optarg = argv[optind++];
2250 loc_set_cmdline(argv, optind - 2, 2);
2261 static gpointer malloc_and_trace(gsize n_bytes)
2263 void *ptr = malloc(n_bytes);
2264 trace_g_malloc(n_bytes, ptr);
2268 static gpointer realloc_and_trace(gpointer mem, gsize n_bytes)
2270 void *ptr = realloc(mem, n_bytes);
2271 trace_g_realloc(mem, n_bytes, ptr);
2275 static void free_and_trace(gpointer mem)
2281 int main(int argc, char **argv, char **envp)
2283 const char *gdbstub_dev = NULL;
2285 int snapshot, linux_boot;
2286 const char *icount_option = NULL;
2287 const char *initrd_filename;
2288 const char *kernel_filename, *kernel_cmdline;
2289 char boot_devices[33] = "cad"; /* default to HD->floppy->CD-ROM */
2291 DisplayChangeListener *dcl;
2292 int cyls, heads, secs, translation;
2293 QemuOpts *hda_opts = NULL, *opts;
2294 QemuOptsList *olist;
2297 const char *loadvm = NULL;
2298 QEMUMachine *machine;
2299 const char *cpu_model;
2300 const char *pid_file = NULL;
2301 const char *incoming = NULL;
2303 int show_vnc_port = 0;
2306 const char *log_mask = NULL;
2307 const char *log_file = NULL;
2308 GMemVTable mem_trace = {
2309 .malloc = malloc_and_trace,
2310 .realloc = realloc_and_trace,
2311 .free = free_and_trace,
2313 const char *trace_events = NULL;
2314 const char *trace_file = NULL;
2316 atexit(qemu_run_exit_notifiers);
2317 error_set_progname(argv[0]);
2319 g_mem_set_vtable(&mem_trace);
2320 g_thread_init(NULL);
2326 qemu_cache_utils_init(envp);
2328 QLIST_INIT (&vm_change_state_head);
2329 os_setup_early_signal_handling();
2331 module_call_init(MODULE_INIT_MACHINE);
2332 machine = find_default_machine();
2334 initrd_filename = NULL;
2337 kernel_filename = NULL;
2338 kernel_cmdline = "";
2339 cyls = heads = secs = 0;
2340 translation = BIOS_ATA_TRANSLATION_AUTO;
2342 for (i = 0; i < MAX_NODES; i++) {
2344 node_cpumask[i] = 0;
2352 /* first pass of option parsing */
2354 while (optind < argc) {
2355 if (argv[optind][0] != '-') {
2360 const QEMUOption *popt;
2362 popt = lookup_opt(argc, argv, &optarg, &optind);
2363 switch (popt->index) {
2364 case QEMU_OPTION_nodefconfig:
2374 ret = qemu_read_config_file(CONFIG_QEMU_CONFDIR "/qemu.conf");
2375 if (ret < 0 && ret != -ENOENT) {
2379 ret = qemu_read_config_file(arch_config_name);
2380 if (ret < 0 && ret != -ENOENT) {
2386 /* second pass of option parsing */
2391 if (argv[optind][0] != '-') {
2392 hda_opts = drive_add(IF_DEFAULT, 0, argv[optind++], HD_OPTS);
2394 const QEMUOption *popt;
2396 popt = lookup_opt(argc, argv, &optarg, &optind);
2397 if (!(popt->arch_mask & arch_type)) {
2398 printf("Option %s not supported for this target\n", popt->name);
2401 switch(popt->index) {
2403 machine = machine_parse(optarg);
2405 case QEMU_OPTION_cpu:
2406 /* hw initialization will check this */
2407 if (*optarg == '?') {
2408 list_cpus(stdout, &fprintf, optarg);
2414 case QEMU_OPTION_initrd:
2415 initrd_filename = optarg;
2417 case QEMU_OPTION_hda:
2421 snprintf(buf, sizeof(buf), "%s", HD_OPTS);
2423 snprintf(buf, sizeof(buf),
2424 "%s,cyls=%d,heads=%d,secs=%d%s",
2425 HD_OPTS , cyls, heads, secs,
2426 translation == BIOS_ATA_TRANSLATION_LBA ?
2428 translation == BIOS_ATA_TRANSLATION_NONE ?
2429 ",trans=none" : "");
2430 drive_add(IF_DEFAULT, 0, optarg, buf);
2433 case QEMU_OPTION_hdb:
2434 case QEMU_OPTION_hdc:
2435 case QEMU_OPTION_hdd:
2436 drive_add(IF_DEFAULT, popt->index - QEMU_OPTION_hda, optarg,
2439 case QEMU_OPTION_drive:
2440 if (drive_def(optarg) == NULL) {
2444 case QEMU_OPTION_set:
2445 if (qemu_set_option(optarg) != 0)
2448 case QEMU_OPTION_global:
2449 if (qemu_global_option(optarg) != 0)
2452 case QEMU_OPTION_mtdblock:
2453 drive_add(IF_MTD, -1, optarg, MTD_OPTS);
2455 case QEMU_OPTION_sd:
2456 drive_add(IF_SD, 0, optarg, SD_OPTS);
2458 case QEMU_OPTION_pflash:
2459 drive_add(IF_PFLASH, -1, optarg, PFLASH_OPTS);
2461 case QEMU_OPTION_snapshot:
2464 case QEMU_OPTION_hdachs:
2468 cyls = strtol(p, (char **)&p, 0);
2469 if (cyls < 1 || cyls > 16383)
2474 heads = strtol(p, (char **)&p, 0);
2475 if (heads < 1 || heads > 16)
2480 secs = strtol(p, (char **)&p, 0);
2481 if (secs < 1 || secs > 63)
2485 if (!strcmp(p, "none"))
2486 translation = BIOS_ATA_TRANSLATION_NONE;
2487 else if (!strcmp(p, "lba"))
2488 translation = BIOS_ATA_TRANSLATION_LBA;
2489 else if (!strcmp(p, "auto"))
2490 translation = BIOS_ATA_TRANSLATION_AUTO;
2493 } else if (*p != '\0') {
2495 fprintf(stderr, "qemu: invalid physical CHS format\n");
2498 if (hda_opts != NULL) {
2500 snprintf(num, sizeof(num), "%d", cyls);
2501 qemu_opt_set(hda_opts, "cyls", num);
2502 snprintf(num, sizeof(num), "%d", heads);
2503 qemu_opt_set(hda_opts, "heads", num);
2504 snprintf(num, sizeof(num), "%d", secs);
2505 qemu_opt_set(hda_opts, "secs", num);
2506 if (translation == BIOS_ATA_TRANSLATION_LBA)
2507 qemu_opt_set(hda_opts, "trans", "lba");
2508 if (translation == BIOS_ATA_TRANSLATION_NONE)
2509 qemu_opt_set(hda_opts, "trans", "none");
2513 case QEMU_OPTION_numa:
2514 if (nb_numa_nodes >= MAX_NODES) {
2515 fprintf(stderr, "qemu: too many NUMA nodes\n");
2520 case QEMU_OPTION_display:
2521 display_type = select_display(optarg);
2523 case QEMU_OPTION_nographic:
2524 display_type = DT_NOGRAPHIC;
2526 case QEMU_OPTION_curses:
2527 #ifdef CONFIG_CURSES
2528 display_type = DT_CURSES;
2530 fprintf(stderr, "Curses support is disabled\n");
2534 case QEMU_OPTION_portrait:
2535 graphic_rotate = 90;
2537 case QEMU_OPTION_rotate:
2538 graphic_rotate = strtol(optarg, (char **) &optarg, 10);
2539 if (graphic_rotate != 0 && graphic_rotate != 90 &&
2540 graphic_rotate != 180 && graphic_rotate != 270) {
2542 "qemu: only 90, 180, 270 deg rotation is available\n");
2546 case QEMU_OPTION_kernel:
2547 kernel_filename = optarg;
2549 case QEMU_OPTION_append:
2550 kernel_cmdline = optarg;
2552 case QEMU_OPTION_cdrom:
2553 drive_add(IF_DEFAULT, 2, optarg, CDROM_OPTS);
2555 case QEMU_OPTION_boot:
2557 static const char * const params[] = {
2558 "order", "once", "menu",
2559 "splash", "splash-time", NULL
2561 char buf[sizeof(boot_devices)];
2562 char *standard_boot_devices;
2565 if (!strchr(optarg, '=')) {
2567 pstrcpy(buf, sizeof(buf), optarg);
2568 } else if (check_params(buf, sizeof(buf), params, optarg) < 0) {
2570 "qemu: unknown boot parameter '%s' in '%s'\n",
2576 get_param_value(buf, sizeof(buf), "order", optarg)) {
2577 validate_bootdevices(buf);
2578 pstrcpy(boot_devices, sizeof(boot_devices), buf);
2581 if (get_param_value(buf, sizeof(buf),
2583 validate_bootdevices(buf);
2584 standard_boot_devices = g_strdup(boot_devices);
2585 pstrcpy(boot_devices, sizeof(boot_devices), buf);
2586 qemu_register_reset(restore_boot_devices,
2587 standard_boot_devices);
2589 if (get_param_value(buf, sizeof(buf),
2591 if (!strcmp(buf, "on")) {
2593 } else if (!strcmp(buf, "off")) {
2597 "qemu: invalid option value '%s'\n",
2602 qemu_opts_parse(qemu_find_opts("boot-opts"),
2607 case QEMU_OPTION_fda:
2608 case QEMU_OPTION_fdb:
2609 drive_add(IF_FLOPPY, popt->index - QEMU_OPTION_fda,
2612 case QEMU_OPTION_no_fd_bootchk:
2615 case QEMU_OPTION_netdev:
2616 if (net_client_parse(qemu_find_opts("netdev"), optarg) == -1) {
2620 case QEMU_OPTION_net:
2621 if (net_client_parse(qemu_find_opts("net"), optarg) == -1) {
2626 case QEMU_OPTION_tftp:
2627 legacy_tftp_prefix = optarg;
2629 case QEMU_OPTION_bootp:
2630 legacy_bootp_filename = optarg;
2632 case QEMU_OPTION_redir:
2633 if (net_slirp_redir(optarg) < 0)
2637 case QEMU_OPTION_bt:
2638 add_device_config(DEV_BT, optarg);
2640 case QEMU_OPTION_audio_help:
2641 if (!(audio_available())) {
2642 printf("Option %s not supported for this target\n", popt->name);
2648 case QEMU_OPTION_soundhw:
2649 if (!(audio_available())) {
2650 printf("Option %s not supported for this target\n", popt->name);
2653 select_soundhw (optarg);
2658 case QEMU_OPTION_version:
2662 case QEMU_OPTION_m: {
2665 value = strtosz(optarg, NULL);
2667 fprintf(stderr, "qemu: invalid ram size: %s\n", optarg);
2671 if (value != (uint64_t)(ram_addr_t)value) {
2672 fprintf(stderr, "qemu: ram size too large\n");
2678 case QEMU_OPTION_mempath:
2682 case QEMU_OPTION_mem_prealloc:
2693 gdbstub_dev = "tcp::" DEFAULT_GDBSTUB_PORT;
2695 case QEMU_OPTION_gdb:
2696 gdbstub_dev = optarg;
2701 case QEMU_OPTION_bios:
2704 case QEMU_OPTION_singlestep:
2711 keyboard_layout = optarg;
2713 case QEMU_OPTION_localtime:
2716 case QEMU_OPTION_vga:
2717 select_vgahw (optarg);
2724 w = strtol(p, (char **)&p, 10);
2727 fprintf(stderr, "qemu: invalid resolution or depth\n");
2733 h = strtol(p, (char **)&p, 10);
2738 depth = strtol(p, (char **)&p, 10);
2739 if (depth != 8 && depth != 15 && depth != 16 &&
2740 depth != 24 && depth != 32)
2742 } else if (*p == '\0') {
2743 depth = graphic_depth;
2750 graphic_depth = depth;
2753 case QEMU_OPTION_echr:
2756 term_escape_char = strtol(optarg, &r, 0);
2758 printf("Bad argument to echr\n");
2761 case QEMU_OPTION_monitor:
2762 monitor_parse(optarg, "readline");
2763 default_monitor = 0;
2765 case QEMU_OPTION_qmp:
2766 monitor_parse(optarg, "control");
2767 default_monitor = 0;
2769 case QEMU_OPTION_mon:
2770 opts = qemu_opts_parse(qemu_find_opts("mon"), optarg, 1);
2774 default_monitor = 0;
2776 case QEMU_OPTION_chardev:
2777 opts = qemu_opts_parse(qemu_find_opts("chardev"), optarg, 1);
2782 case QEMU_OPTION_fsdev:
2783 olist = qemu_find_opts("fsdev");
2785 fprintf(stderr, "fsdev is not supported by this qemu build.\n");
2788 opts = qemu_opts_parse(olist, optarg, 1);
2790 fprintf(stderr, "parse error: %s\n", optarg);
2794 case QEMU_OPTION_virtfs: {
2798 olist = qemu_find_opts("virtfs");
2800 fprintf(stderr, "virtfs is not supported by this qemu build.\n");
2803 opts = qemu_opts_parse(olist, optarg, 1);
2805 fprintf(stderr, "parse error: %s\n", optarg);
2809 if (qemu_opt_get(opts, "fstype") == NULL ||
2810 qemu_opt_get(opts, "mount_tag") == NULL ||
2811 qemu_opt_get(opts, "path") == NULL ||
2812 qemu_opt_get(opts, "security_model") == NULL) {
2813 fprintf(stderr, "Usage: -virtfs fstype,path=/share_path/,"
2814 "security_model=[mapped|passthrough|none],"
2815 "mount_tag=tag.\n");
2819 fsdev = qemu_opts_create(qemu_find_opts("fsdev"),
2820 qemu_opt_get(opts, "mount_tag"), 1);
2822 fprintf(stderr, "duplicate fsdev id: %s\n",
2823 qemu_opt_get(opts, "mount_tag"));
2826 qemu_opt_set(fsdev, "fstype", qemu_opt_get(opts, "fstype"));
2827 qemu_opt_set(fsdev, "path", qemu_opt_get(opts, "path"));
2828 qemu_opt_set(fsdev, "security_model",
2829 qemu_opt_get(opts, "security_model"));
2831 device = qemu_opts_create(qemu_find_opts("device"), NULL, 0);
2832 qemu_opt_set(device, "driver", "virtio-9p-pci");
2833 qemu_opt_set(device, "fsdev",
2834 qemu_opt_get(opts, "mount_tag"));
2835 qemu_opt_set(device, "mount_tag",
2836 qemu_opt_get(opts, "mount_tag"));
2839 case QEMU_OPTION_serial:
2840 add_device_config(DEV_SERIAL, optarg);
2842 if (strncmp(optarg, "mon:", 4) == 0) {
2843 default_monitor = 0;
2846 case QEMU_OPTION_watchdog:
2849 "qemu: only one watchdog option may be given\n");
2854 case QEMU_OPTION_watchdog_action:
2855 if (select_watchdog_action(optarg) == -1) {
2856 fprintf(stderr, "Unknown -watchdog-action parameter\n");
2860 case QEMU_OPTION_virtiocon:
2861 add_device_config(DEV_VIRTCON, optarg);
2862 default_virtcon = 0;
2863 if (strncmp(optarg, "mon:", 4) == 0) {
2864 default_monitor = 0;
2867 case QEMU_OPTION_parallel:
2868 add_device_config(DEV_PARALLEL, optarg);
2869 default_parallel = 0;
2870 if (strncmp(optarg, "mon:", 4) == 0) {
2871 default_monitor = 0;
2874 case QEMU_OPTION_debugcon:
2875 add_device_config(DEV_DEBUGCON, optarg);
2877 case QEMU_OPTION_loadvm:
2880 case QEMU_OPTION_full_screen:
2884 case QEMU_OPTION_no_frame:
2887 case QEMU_OPTION_alt_grab:
2890 case QEMU_OPTION_ctrl_grab:
2893 case QEMU_OPTION_no_quit:
2896 case QEMU_OPTION_sdl:
2897 display_type = DT_SDL;
2900 case QEMU_OPTION_no_frame:
2901 case QEMU_OPTION_alt_grab:
2902 case QEMU_OPTION_ctrl_grab:
2903 case QEMU_OPTION_no_quit:
2904 case QEMU_OPTION_sdl:
2905 fprintf(stderr, "SDL support is disabled\n");
2908 case QEMU_OPTION_pidfile:
2911 case QEMU_OPTION_win2k_hack:
2912 win2k_install_hack = 1;
2914 case QEMU_OPTION_rtc_td_hack:
2917 case QEMU_OPTION_acpitable:
2918 do_acpitable_option(optarg);
2920 case QEMU_OPTION_smbios:
2921 do_smbios_option(optarg);
2923 case QEMU_OPTION_enable_kvm:
2924 olist = qemu_find_opts("machine");
2925 qemu_opts_reset(olist);
2926 qemu_opts_parse(olist, "accel=kvm", 0);
2928 case QEMU_OPTION_machine:
2929 olist = qemu_find_opts("machine");
2930 qemu_opts_reset(olist);
2931 opts = qemu_opts_parse(olist, optarg, 1);
2933 fprintf(stderr, "parse error: %s\n", optarg);
2936 optarg = qemu_opt_get(opts, "type");
2938 machine = machine_parse(optarg);
2941 case QEMU_OPTION_usb:
2944 case QEMU_OPTION_usbdevice:
2946 add_device_config(DEV_USB, optarg);
2948 case QEMU_OPTION_device:
2949 if (!qemu_opts_parse(qemu_find_opts("device"), optarg, 1)) {
2953 case QEMU_OPTION_smp:
2956 fprintf(stderr, "Invalid number of CPUs\n");
2959 if (max_cpus < smp_cpus) {
2960 fprintf(stderr, "maxcpus must be equal to or greater than "
2964 if (max_cpus > 255) {
2965 fprintf(stderr, "Unsupported number of maxcpus\n");
2969 case QEMU_OPTION_vnc:
2972 vnc_display = optarg;
2974 fprintf(stderr, "VNC support is disabled\n");
2978 case QEMU_OPTION_no_acpi:
2981 case QEMU_OPTION_no_hpet:
2984 case QEMU_OPTION_balloon:
2985 if (balloon_parse(optarg) < 0) {
2986 fprintf(stderr, "Unknown -balloon argument %s\n", optarg);
2990 case QEMU_OPTION_no_reboot:
2993 case QEMU_OPTION_no_shutdown:
2996 case QEMU_OPTION_show_cursor:
2999 case QEMU_OPTION_uuid:
3000 if(qemu_uuid_parse(optarg, qemu_uuid) < 0) {
3001 fprintf(stderr, "Fail to parse UUID string."
3002 " Wrong format.\n");
3006 case QEMU_OPTION_option_rom:
3007 if (nb_option_roms >= MAX_OPTION_ROMS) {
3008 fprintf(stderr, "Too many option ROMs\n");
3011 opts = qemu_opts_parse(qemu_find_opts("option-rom"), optarg, 1);
3012 option_rom[nb_option_roms].name = qemu_opt_get(opts, "romfile");
3013 option_rom[nb_option_roms].bootindex =
3014 qemu_opt_get_number(opts, "bootindex", -1);
3015 if (!option_rom[nb_option_roms].name) {
3016 fprintf(stderr, "Option ROM file is not specified\n");
3021 case QEMU_OPTION_semihosting:
3022 semihosting_enabled = 1;
3024 case QEMU_OPTION_name:
3025 qemu_name = g_strdup(optarg);
3027 char *p = strchr(qemu_name, ',');
3030 if (strncmp(p, "process=", 8)) {
3031 fprintf(stderr, "Unknown subargument %s to -name\n", p);
3035 os_set_proc_name(p);
3039 case QEMU_OPTION_prom_env:
3040 if (nb_prom_envs >= MAX_PROM_ENVS) {
3041 fprintf(stderr, "Too many prom variables\n");
3044 prom_envs[nb_prom_envs] = optarg;
3047 case QEMU_OPTION_old_param:
3050 case QEMU_OPTION_clock:
3051 configure_alarms(optarg);
3053 case QEMU_OPTION_startdate:
3054 configure_rtc_date_offset(optarg, 1);
3056 case QEMU_OPTION_rtc:
3057 opts = qemu_opts_parse(qemu_find_opts("rtc"), optarg, 0);
3061 configure_rtc(opts);
3063 case QEMU_OPTION_tb_size:
3064 tcg_tb_size = strtol(optarg, NULL, 0);
3065 if (tcg_tb_size < 0) {
3069 case QEMU_OPTION_icount:
3070 icount_option = optarg;
3072 case QEMU_OPTION_incoming:
3075 case QEMU_OPTION_nodefaults:
3077 default_parallel = 0;
3078 default_virtcon = 0;
3079 default_monitor = 0;
3086 case QEMU_OPTION_xen_domid:
3087 if (!(xen_available())) {
3088 printf("Option %s not supported for this target\n", popt->name);
3091 xen_domid = atoi(optarg);
3093 case QEMU_OPTION_xen_create:
3094 if (!(xen_available())) {
3095 printf("Option %s not supported for this target\n", popt->name);
3098 xen_mode = XEN_CREATE;
3100 case QEMU_OPTION_xen_attach:
3101 if (!(xen_available())) {
3102 printf("Option %s not supported for this target\n", popt->name);
3105 xen_mode = XEN_ATTACH;
3107 case QEMU_OPTION_trace:
3109 opts = qemu_opts_parse(qemu_find_opts("trace"), optarg, 0);
3113 trace_events = qemu_opt_get(opts, "events");
3114 trace_file = qemu_opt_get(opts, "file");
3117 case QEMU_OPTION_readconfig:
3119 int ret = qemu_read_config_file(optarg);
3121 fprintf(stderr, "read config %s: %s\n", optarg,
3127 case QEMU_OPTION_spice:
3128 olist = qemu_find_opts("spice");
3130 fprintf(stderr, "spice is not supported by this qemu build.\n");
3133 opts = qemu_opts_parse(olist, optarg, 0);
3135 fprintf(stderr, "parse error: %s\n", optarg);
3139 case QEMU_OPTION_writeconfig:
3142 if (strcmp(optarg, "-") == 0) {
3145 fp = fopen(optarg, "w");
3147 fprintf(stderr, "open %s: %s\n", optarg, strerror(errno));
3151 qemu_config_write(fp);
3156 os_parse_cmd_args(popt->index, optarg);
3162 /* Open the logfile at this point, if necessary. We can't open the logfile
3163 * when encountering either of the logging options (-d or -D) because the
3164 * other one may be encountered later on the command line, changing the
3165 * location or level of logging.
3169 set_cpu_log_filename(log_file);
3171 set_cpu_log(log_mask);
3174 if (!trace_backend_init(trace_events, trace_file)) {
3178 /* If no data_dir is specified then try to find it relative to the
3181 data_dir = os_find_datadir(argv[0]);
3183 /* If all else fails use the install path specified when building. */
3185 data_dir = CONFIG_QEMU_DATADIR;
3189 * Default to max_cpus = smp_cpus, in case the user doesn't
3190 * specify a max_cpus value.
3193 max_cpus = smp_cpus;
3195 machine->max_cpus = machine->max_cpus ?: 1; /* Default to UP */
3196 if (smp_cpus > machine->max_cpus) {
3197 fprintf(stderr, "Number of SMP cpus requested (%d), exceeds max cpus "
3198 "supported by machine `%s' (%d)\n", smp_cpus, machine->name,
3204 * Get the default machine options from the machine if it is not already
3205 * specified either by the configuration file or by the command line.
3207 if (machine->default_machine_opts) {
3208 QemuOptsList *list = qemu_find_opts("machine");
3209 const char *p = NULL;
3211 if (!QTAILQ_EMPTY(&list->head)) {
3212 p = qemu_opt_get(QTAILQ_FIRST(&list->head), "accel");
3215 qemu_opts_reset(list);
3216 opts = qemu_opts_parse(list, machine->default_machine_opts, 0);
3218 fprintf(stderr, "parse error for machine %s: %s\n",
3219 machine->name, machine->default_machine_opts);
3225 qemu_opts_foreach(qemu_find_opts("device"), default_driver_check, NULL, 0);
3226 qemu_opts_foreach(qemu_find_opts("global"), default_driver_check, NULL, 0);
3228 if (machine->no_serial) {
3231 if (machine->no_parallel) {
3232 default_parallel = 0;
3234 if (!machine->use_virtcon) {
3235 default_virtcon = 0;
3237 if (machine->no_vga) {
3240 if (machine->no_floppy) {
3243 if (machine->no_cdrom) {
3246 if (machine->no_sdcard) {
3250 if (display_type == DT_NOGRAPHIC) {
3251 if (default_parallel)
3252 add_device_config(DEV_PARALLEL, "null");
3253 if (default_serial && default_monitor) {
3254 add_device_config(DEV_SERIAL, "mon:stdio");
3255 } else if (default_virtcon && default_monitor) {
3256 add_device_config(DEV_VIRTCON, "mon:stdio");
3259 add_device_config(DEV_SERIAL, "stdio");
3260 if (default_virtcon)
3261 add_device_config(DEV_VIRTCON, "stdio");
3262 if (default_monitor)
3263 monitor_parse("stdio", "readline");
3267 add_device_config(DEV_SERIAL, "vc:80Cx24C");
3268 if (default_parallel)
3269 add_device_config(DEV_PARALLEL, "vc:80Cx24C");
3270 if (default_monitor)
3271 monitor_parse("vc:80Cx24C", "readline");
3272 if (default_virtcon)
3273 add_device_config(DEV_VIRTCON, "vc:80Cx24C");
3276 vga_interface_type = VGA_CIRRUS;
3280 if (qemu_opts_foreach(qemu_find_opts("chardev"), chardev_init_func, NULL, 1) != 0)
3282 #ifdef CONFIG_VIRTFS
3283 if (qemu_opts_foreach(qemu_find_opts("fsdev"), fsdev_init_func, NULL, 1) != 0) {
3290 if (pid_file && qemu_create_pidfile(pid_file) != 0) {
3295 /* init the memory */
3296 if (ram_size == 0) {
3297 ram_size = DEFAULT_RAM_SIZE * 1024 * 1024;
3300 configure_accelerator();
3302 if (qemu_init_main_loop()) {
3303 fprintf(stderr, "qemu_init_main_loop failed\n");
3306 linux_boot = (kernel_filename != NULL);
3308 if (!linux_boot && *kernel_cmdline != '\0') {
3309 fprintf(stderr, "-append only allowed with -kernel option\n");
3313 if (!linux_boot && initrd_filename != NULL) {
3314 fprintf(stderr, "-initrd only allowed with -kernel option\n");
3318 os_set_line_buffering();
3320 if (init_timer_alarm() < 0) {
3321 fprintf(stderr, "could not initialize alarm timer\n");
3324 configure_icount(icount_option);
3326 if (net_init_clients() < 0) {
3330 /* init the bluetooth world */
3331 if (foreach_device_config(DEV_BT, bt_parse))
3334 if (!xen_enabled()) {
3335 /* On 32-bit hosts, QEMU is limited by virtual address space */
3336 if (ram_size > (2047 << 20) && HOST_LONG_BITS == 32) {
3337 fprintf(stderr, "qemu: at most 2047 MB RAM can be simulated\n");
3342 cpu_exec_init_all();
3344 bdrv_init_with_whitelist();
3348 /* open the virtual block devices */
3350 qemu_opts_foreach(qemu_find_opts("drive"), drive_enable_snapshot, NULL, 0);
3351 if (qemu_opts_foreach(qemu_find_opts("drive"), drive_init_func, &machine->use_scsi, 1) != 0)
3354 default_drive(default_cdrom, snapshot, machine->use_scsi,
3355 IF_DEFAULT, 2, CDROM_OPTS);
3356 default_drive(default_floppy, snapshot, machine->use_scsi,
3357 IF_FLOPPY, 0, FD_OPTS);
3358 default_drive(default_sdcard, snapshot, machine->use_scsi,
3361 register_savevm_live(NULL, "ram", 0, 4, NULL, ram_save_live, NULL,
3364 if (nb_numa_nodes > 0) {
3367 if (nb_numa_nodes > MAX_NODES) {
3368 nb_numa_nodes = MAX_NODES;
3371 /* If no memory size if given for any node, assume the default case
3372 * and distribute the available memory equally across all nodes
3374 for (i = 0; i < nb_numa_nodes; i++) {
3375 if (node_mem[i] != 0)
3378 if (i == nb_numa_nodes) {
3379 uint64_t usedmem = 0;
3381 /* On Linux, the each node's border has to be 8MB aligned,
3382 * the final node gets the rest.
3384 for (i = 0; i < nb_numa_nodes - 1; i++) {
3385 node_mem[i] = (ram_size / nb_numa_nodes) & ~((1 << 23UL) - 1);
3386 usedmem += node_mem[i];
3388 node_mem[i] = ram_size - usedmem;
3391 for (i = 0; i < nb_numa_nodes; i++) {
3392 if (node_cpumask[i] != 0)
3395 /* assigning the VCPUs round-robin is easier to implement, guest OSes
3396 * must cope with this anyway, because there are BIOSes out there in
3397 * real machines which also use this scheme.
3399 if (i == nb_numa_nodes) {
3400 for (i = 0; i < smp_cpus; i++) {
3401 node_cpumask[i % nb_numa_nodes] |= 1 << i;
3406 if (qemu_opts_foreach(qemu_find_opts("mon"), mon_init_func, NULL, 1) != 0) {
3410 if (foreach_device_config(DEV_SERIAL, serial_parse) < 0)
3412 if (foreach_device_config(DEV_PARALLEL, parallel_parse) < 0)
3414 if (foreach_device_config(DEV_VIRTCON, virtcon_parse) < 0)
3416 if (foreach_device_config(DEV_DEBUGCON, debugcon_parse) < 0)
3419 module_call_init(MODULE_INIT_DEVICE);
3421 if (qemu_opts_foreach(qemu_find_opts("device"), device_help_func, NULL, 0) != 0)
3425 i = select_watchdog(watchdog);
3427 exit (i == 1 ? 1 : 0);
3430 if (machine->compat_props) {
3431 qdev_prop_register_global_list(machine->compat_props);
3435 machine->init(ram_size, boot_devices,
3436 kernel_filename, kernel_cmdline, initrd_filename, cpu_model);
3438 cpu_synchronize_all_post_init();
3442 current_machine = machine;
3444 /* init USB devices */
3446 if (foreach_device_config(DEV_USB, usb_parse) < 0)
3450 /* init generic devices */
3451 if (qemu_opts_foreach(qemu_find_opts("device"), device_init_func, NULL, 1) != 0)
3454 net_check_clients();
3456 /* just use the first displaystate for the moment */
3457 ds = get_displaystate();
3461 if (display_type == DT_DEFAULT && !display_remote) {
3462 #if defined(CONFIG_SDL) || defined(CONFIG_COCOA)
3463 display_type = DT_SDL;
3464 #elif defined(CONFIG_VNC)
3465 vnc_display = "localhost:0,to=99";
3468 display_type = DT_NONE;
3473 /* init local displays */
3474 switch (display_type) {
3477 #if defined(CONFIG_CURSES)
3479 curses_display_init(ds, full_screen);
3482 #if defined(CONFIG_SDL)
3484 sdl_display_init(ds, full_screen, no_frame);
3486 #elif defined(CONFIG_COCOA)
3488 cocoa_display_init(ds, full_screen);
3495 /* must be after terminal init, SDL library changes signal handlers */
3496 os_setup_signal_handling();
3499 /* init remote displays */
3501 vnc_display_init(ds);
3502 if (vnc_display_open(ds, vnc_display) < 0)
3505 if (show_vnc_port) {
3506 printf("VNC server running on `%s'\n", vnc_display_local_addr(ds));
3511 if (using_spice && !qxl_enabled) {
3512 qemu_spice_display_init(ds);
3518 dcl = ds->listeners;
3519 while (dcl != NULL) {
3520 if (dcl->dpy_refresh != NULL) {
3521 ds->gui_timer = qemu_new_timer_ms(rt_clock, gui_update, ds);
3522 qemu_mod_timer(ds->gui_timer, qemu_get_clock_ms(rt_clock));
3527 if (ds->gui_timer == NULL) {
3528 nographic_timer = qemu_new_timer_ms(rt_clock, nographic_update, NULL);
3529 qemu_mod_timer(nographic_timer, qemu_get_clock_ms(rt_clock));
3531 text_consoles_set_display(ds);
3533 if (gdbstub_dev && gdbserver_start(gdbstub_dev) < 0) {
3534 fprintf(stderr, "qemu: could not open gdbserver on device '%s'\n",
3539 qdev_machine_creation_done();
3541 if (rom_load_all() != 0) {
3542 fprintf(stderr, "rom loading failed\n");
3546 /* TODO: once all bus devices are qdevified, this should be done
3547 * when bus is created by qdev.c */
3548 qemu_register_reset(qbus_reset_all_fn, sysbus_get_default());
3549 qemu_run_machine_init_done_notifiers();
3551 qemu_system_reset(VMRESET_SILENT);
3553 if (load_vmstate(loadvm) < 0) {
3559 runstate_set(RSTATE_IN_MIGRATE);
3560 int ret = qemu_start_incoming_migration(incoming);
3562 fprintf(stderr, "Migration failed. Exit code %s(%d), exiting.\n",
3566 } else if (autostart) {
3569 runstate_set(RSTATE_PRE_LAUNCH);