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"
147 #include "qemu-option.h"
148 #include "qemu-config.h"
149 #include "qemu-options.h"
150 #include "qmp-commands.h"
151 #include "main-loop.h"
153 #include "fsdev/qemu-fsdev.h"
158 #include "qemu_socket.h"
160 #include "slirp/libslirp.h"
163 #include "trace/control.h"
164 #include "qemu-queue.h"
166 #include "arch_init.h"
168 #include "ui/qemu-spice.h"
171 //#define DEBUG_SLIRP
173 #define DEFAULT_RAM_SIZE 128
175 #define MAX_VIRTIO_CONSOLES 1
177 static const char *data_dir;
178 const char *bios_name = NULL;
179 enum vga_retrace_method vga_retrace_method = VGA_RETRACE_DUMB;
180 DisplayType display_type = DT_DEFAULT;
181 int display_remote = 0;
182 const char* keyboard_layout = NULL;
184 const char *mem_path = NULL;
186 int mem_prealloc = 0; /* force preallocation of physical target memory */
189 NICInfo nd_table[MAX_NICS];
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;
212 const char *vnc_display;
214 int acpi_enabled = 1;
220 int graphic_rotate = 0;
221 const char *watchdog;
222 QEMUOptionRom option_rom[MAX_OPTION_ROMS];
224 int semihosting_enabled = 0;
226 const char *qemu_name;
229 unsigned int nb_prom_envs = 0;
230 const char *prom_envs[MAX_PROM_ENVS];
232 uint8_t *boot_splash_filedata;
233 int boot_splash_filedata_size;
234 uint8_t qemu_extra_params_fw[2];
236 typedef struct FWBootEntry FWBootEntry;
239 QTAILQ_ENTRY(FWBootEntry) link;
245 QTAILQ_HEAD(, FWBootEntry) fw_boot_order = QTAILQ_HEAD_INITIALIZER(fw_boot_order);
248 uint64_t node_mem[MAX_NODES];
249 uint64_t node_cpumask[MAX_NODES];
251 uint8_t qemu_uuid[16];
253 static QEMUBootSetHandler *boot_set_handler;
254 static void *boot_set_opaque;
256 static NotifierList exit_notifiers =
257 NOTIFIER_LIST_INITIALIZER(exit_notifiers);
259 static NotifierList machine_init_done_notifiers =
260 NOTIFIER_LIST_INITIALIZER(machine_init_done_notifiers);
262 static int tcg_allowed = 1;
266 enum xen_mode xen_mode = XEN_EMULATE;
267 static int tcg_tb_size;
269 static int default_serial = 1;
270 static int default_parallel = 1;
271 static int default_virtcon = 1;
272 static int default_monitor = 1;
273 static int default_vga = 1;
274 static int default_floppy = 1;
275 static int default_cdrom = 1;
276 static int default_sdcard = 1;
282 { .driver = "isa-serial", .flag = &default_serial },
283 { .driver = "isa-parallel", .flag = &default_parallel },
284 { .driver = "isa-fdc", .flag = &default_floppy },
285 { .driver = "ide-cd", .flag = &default_cdrom },
286 { .driver = "ide-hd", .flag = &default_cdrom },
287 { .driver = "ide-drive", .flag = &default_cdrom },
288 { .driver = "scsi-cd", .flag = &default_cdrom },
289 { .driver = "virtio-serial-pci", .flag = &default_virtcon },
290 { .driver = "virtio-serial-s390", .flag = &default_virtcon },
291 { .driver = "virtio-serial", .flag = &default_virtcon },
292 { .driver = "VGA", .flag = &default_vga },
293 { .driver = "cirrus-vga", .flag = &default_vga },
294 { .driver = "vmware-svga", .flag = &default_vga },
295 { .driver = "isa-vga", .flag = &default_vga },
296 { .driver = "qxl-vga", .flag = &default_vga },
299 static void res_free(void)
301 if (boot_splash_filedata != NULL) {
302 g_free(boot_splash_filedata);
303 boot_splash_filedata = NULL;
307 static int default_driver_check(QemuOpts *opts, void *opaque)
309 const char *driver = qemu_opt_get(opts, "driver");
314 for (i = 0; i < ARRAY_SIZE(default_list); i++) {
315 if (strcmp(default_list[i].driver, driver) != 0)
317 *(default_list[i].flag) = 0;
322 /***********************************************************/
325 static RunState current_run_state = RUN_STATE_PRELAUNCH;
330 } RunStateTransition;
332 static const RunStateTransition runstate_transitions_def[] = {
334 { RUN_STATE_DEBUG, RUN_STATE_RUNNING },
336 { RUN_STATE_INMIGRATE, RUN_STATE_RUNNING },
337 { RUN_STATE_INMIGRATE, RUN_STATE_PRELAUNCH },
339 { RUN_STATE_INTERNAL_ERROR, RUN_STATE_PAUSED },
340 { RUN_STATE_INTERNAL_ERROR, RUN_STATE_FINISH_MIGRATE },
342 { RUN_STATE_IO_ERROR, RUN_STATE_RUNNING },
343 { RUN_STATE_IO_ERROR, RUN_STATE_FINISH_MIGRATE },
345 { RUN_STATE_PAUSED, RUN_STATE_RUNNING },
346 { RUN_STATE_PAUSED, RUN_STATE_FINISH_MIGRATE },
348 { RUN_STATE_POSTMIGRATE, RUN_STATE_RUNNING },
349 { RUN_STATE_POSTMIGRATE, RUN_STATE_FINISH_MIGRATE },
351 { RUN_STATE_PRELAUNCH, RUN_STATE_RUNNING },
352 { RUN_STATE_PRELAUNCH, RUN_STATE_FINISH_MIGRATE },
353 { RUN_STATE_PRELAUNCH, RUN_STATE_INMIGRATE },
355 { RUN_STATE_FINISH_MIGRATE, RUN_STATE_RUNNING },
356 { RUN_STATE_FINISH_MIGRATE, RUN_STATE_POSTMIGRATE },
358 { RUN_STATE_RESTORE_VM, RUN_STATE_RUNNING },
360 { RUN_STATE_RUNNING, RUN_STATE_DEBUG },
361 { RUN_STATE_RUNNING, RUN_STATE_INTERNAL_ERROR },
362 { RUN_STATE_RUNNING, RUN_STATE_IO_ERROR },
363 { RUN_STATE_RUNNING, RUN_STATE_PAUSED },
364 { RUN_STATE_RUNNING, RUN_STATE_FINISH_MIGRATE },
365 { RUN_STATE_RUNNING, RUN_STATE_RESTORE_VM },
366 { RUN_STATE_RUNNING, RUN_STATE_SAVE_VM },
367 { RUN_STATE_RUNNING, RUN_STATE_SHUTDOWN },
368 { RUN_STATE_RUNNING, RUN_STATE_WATCHDOG },
370 { RUN_STATE_SAVE_VM, RUN_STATE_RUNNING },
372 { RUN_STATE_SHUTDOWN, RUN_STATE_PAUSED },
373 { RUN_STATE_SHUTDOWN, RUN_STATE_FINISH_MIGRATE },
375 { RUN_STATE_WATCHDOG, RUN_STATE_RUNNING },
376 { RUN_STATE_WATCHDOG, RUN_STATE_FINISH_MIGRATE },
378 { RUN_STATE_MAX, RUN_STATE_MAX },
381 static bool runstate_valid_transitions[RUN_STATE_MAX][RUN_STATE_MAX];
383 bool runstate_check(RunState state)
385 return current_run_state == state;
388 void runstate_init(void)
390 const RunStateTransition *p;
392 memset(&runstate_valid_transitions, 0, sizeof(runstate_valid_transitions));
394 for (p = &runstate_transitions_def[0]; p->from != RUN_STATE_MAX; p++) {
395 runstate_valid_transitions[p->from][p->to] = true;
399 /* This function will abort() on invalid state transitions */
400 void runstate_set(RunState new_state)
402 assert(new_state < RUN_STATE_MAX);
404 if (!runstate_valid_transitions[current_run_state][new_state]) {
405 fprintf(stderr, "ERROR: invalid runstate transition: '%s' -> '%s'\n",
406 RunState_lookup[current_run_state],
407 RunState_lookup[new_state]);
411 current_run_state = new_state;
414 int runstate_is_running(void)
416 return runstate_check(RUN_STATE_RUNNING);
419 StatusInfo *qmp_query_status(Error **errp)
421 StatusInfo *info = g_malloc0(sizeof(*info));
423 info->running = runstate_is_running();
424 info->singlestep = singlestep;
425 info->status = current_run_state;
430 /***********************************************************/
431 /* real time host monotonic timer */
433 /***********************************************************/
434 /* host time/date access */
435 void qemu_get_timedate(struct tm *tm, int offset)
442 if (rtc_date_offset == -1) {
446 ret = localtime(&ti);
448 ti -= rtc_date_offset;
452 memcpy(tm, ret, sizeof(struct tm));
455 int qemu_timedate_diff(struct tm *tm)
459 if (rtc_date_offset == -1)
461 seconds = mktimegm(tm);
464 tmp.tm_isdst = -1; /* use timezone to figure it out */
465 seconds = mktime(&tmp);
468 seconds = mktimegm(tm) + rtc_date_offset;
470 return seconds - time(NULL);
473 void rtc_change_mon_event(struct tm *tm)
477 data = qobject_from_jsonf("{ 'offset': %d }", qemu_timedate_diff(tm));
478 monitor_protocol_event(QEVENT_RTC_CHANGE, data);
479 qobject_decref(data);
482 static void configure_rtc_date_offset(const char *startdate, int legacy)
484 time_t rtc_start_date;
487 if (!strcmp(startdate, "now") && legacy) {
488 rtc_date_offset = -1;
490 if (sscanf(startdate, "%d-%d-%dT%d:%d:%d",
498 } else if (sscanf(startdate, "%d-%d-%d",
510 rtc_start_date = mktimegm(&tm);
511 if (rtc_start_date == -1) {
513 fprintf(stderr, "Invalid date format. Valid formats are:\n"
514 "'2006-06-17T16:01:21' or '2006-06-17'\n");
517 rtc_date_offset = time(NULL) - rtc_start_date;
521 static void configure_rtc(QemuOpts *opts)
525 value = qemu_opt_get(opts, "base");
527 if (!strcmp(value, "utc")) {
529 } else if (!strcmp(value, "localtime")) {
532 configure_rtc_date_offset(value, 0);
535 value = qemu_opt_get(opts, "clock");
537 if (!strcmp(value, "host")) {
538 rtc_clock = host_clock;
539 } else if (!strcmp(value, "vm")) {
540 rtc_clock = vm_clock;
542 fprintf(stderr, "qemu: invalid option value '%s'\n", value);
546 value = qemu_opt_get(opts, "driftfix");
548 if (!strcmp(value, "slew")) {
550 } else if (!strcmp(value, "none")) {
553 fprintf(stderr, "qemu: invalid option value '%s'\n", value);
559 /***********************************************************/
560 /* Bluetooth support */
563 static struct HCIInfo *hci_table[MAX_NICS];
565 static struct bt_vlan_s {
566 struct bt_scatternet_s net;
568 struct bt_vlan_s *next;
571 /* find or alloc a new bluetooth "VLAN" */
572 static struct bt_scatternet_s *qemu_find_bt_vlan(int id)
574 struct bt_vlan_s **pvlan, *vlan;
575 for (vlan = first_bt_vlan; vlan != NULL; vlan = vlan->next) {
579 vlan = g_malloc0(sizeof(struct bt_vlan_s));
581 pvlan = &first_bt_vlan;
582 while (*pvlan != NULL)
583 pvlan = &(*pvlan)->next;
588 static void null_hci_send(struct HCIInfo *hci, const uint8_t *data, int len)
592 static int null_hci_addr_set(struct HCIInfo *hci, const uint8_t *bd_addr)
597 static struct HCIInfo null_hci = {
598 .cmd_send = null_hci_send,
599 .sco_send = null_hci_send,
600 .acl_send = null_hci_send,
601 .bdaddr_set = null_hci_addr_set,
604 struct HCIInfo *qemu_next_hci(void)
606 if (cur_hci == nb_hcis)
609 return hci_table[cur_hci++];
612 static struct HCIInfo *hci_init(const char *str)
615 struct bt_scatternet_s *vlan = 0;
617 if (!strcmp(str, "null"))
620 else if (!strncmp(str, "host", 4) && (str[4] == '\0' || str[4] == ':'))
622 return bt_host_hci(str[4] ? str + 5 : "hci0");
623 else if (!strncmp(str, "hci", 3)) {
626 if (!strncmp(str + 3, ",vlan=", 6)) {
627 vlan = qemu_find_bt_vlan(strtol(str + 9, &endp, 0));
632 vlan = qemu_find_bt_vlan(0);
634 return bt_new_hci(vlan);
637 fprintf(stderr, "qemu: Unknown bluetooth HCI `%s'.\n", str);
642 static int bt_hci_parse(const char *str)
647 if (nb_hcis >= MAX_NICS) {
648 fprintf(stderr, "qemu: Too many bluetooth HCIs (max %i).\n", MAX_NICS);
661 bdaddr.b[5] = 0x56 + nb_hcis;
662 hci->bdaddr_set(hci, bdaddr.b);
664 hci_table[nb_hcis++] = hci;
669 static void bt_vhci_add(int vlan_id)
671 struct bt_scatternet_s *vlan = qemu_find_bt_vlan(vlan_id);
674 fprintf(stderr, "qemu: warning: adding a VHCI to "
675 "an empty scatternet %i\n", vlan_id);
677 bt_vhci_init(bt_new_hci(vlan));
680 static struct bt_device_s *bt_device_add(const char *opt)
682 struct bt_scatternet_s *vlan;
684 char *endp = strstr(opt, ",vlan=");
685 int len = (endp ? endp - opt : strlen(opt)) + 1;
688 pstrcpy(devname, MIN(sizeof(devname), len), opt);
691 vlan_id = strtol(endp + 6, &endp, 0);
693 fprintf(stderr, "qemu: unrecognised bluetooth vlan Id\n");
698 vlan = qemu_find_bt_vlan(vlan_id);
701 fprintf(stderr, "qemu: warning: adding a slave device to "
702 "an empty scatternet %i\n", vlan_id);
704 if (!strcmp(devname, "keyboard"))
705 return bt_keyboard_init(vlan);
707 fprintf(stderr, "qemu: unsupported bluetooth device `%s'\n", devname);
711 static int bt_parse(const char *opt)
713 const char *endp, *p;
716 if (strstart(opt, "hci", &endp)) {
717 if (!*endp || *endp == ',') {
719 if (!strstart(endp, ",vlan=", 0))
722 return bt_hci_parse(opt);
724 } else if (strstart(opt, "vhci", &endp)) {
725 if (!*endp || *endp == ',') {
727 if (strstart(endp, ",vlan=", &p)) {
728 vlan = strtol(p, (char **) &endp, 0);
730 fprintf(stderr, "qemu: bad scatternet '%s'\n", p);
734 fprintf(stderr, "qemu: bad parameter '%s'\n", endp + 1);
743 } else if (strstart(opt, "device:", &endp))
744 return !bt_device_add(endp);
746 fprintf(stderr, "qemu: bad bluetooth parameter '%s'\n", opt);
750 /***********************************************************/
751 /* QEMU Block devices */
753 #define HD_OPTS "media=disk"
754 #define CDROM_OPTS "media=cdrom"
756 #define PFLASH_OPTS ""
760 static int drive_init_func(QemuOpts *opts, void *opaque)
762 int *use_scsi = opaque;
764 return drive_init(opts, *use_scsi) == NULL;
767 static int drive_enable_snapshot(QemuOpts *opts, void *opaque)
769 if (NULL == qemu_opt_get(opts, "snapshot")) {
770 qemu_opt_set(opts, "snapshot", "on");
775 static void default_drive(int enable, int snapshot, int use_scsi,
776 BlockInterfaceType type, int index,
781 if (type == IF_DEFAULT) {
782 type = use_scsi ? IF_SCSI : IF_IDE;
785 if (!enable || drive_get_by_index(type, index)) {
789 opts = drive_add(type, index, NULL, optstr);
791 drive_enable_snapshot(opts, NULL);
793 if (!drive_init(opts, use_scsi)) {
798 void qemu_register_boot_set(QEMUBootSetHandler *func, void *opaque)
800 boot_set_handler = func;
801 boot_set_opaque = opaque;
804 int qemu_boot_set(const char *boot_devices)
806 if (!boot_set_handler) {
809 return boot_set_handler(boot_set_opaque, boot_devices);
812 static void validate_bootdevices(char *devices)
814 /* We just do some generic consistency checks */
818 for (p = devices; *p != '\0'; p++) {
819 /* Allowed boot devices are:
820 * a-b: floppy disk drives
821 * c-f: IDE disk drives
822 * g-m: machine implementation dependent drives
823 * n-p: network devices
824 * It's up to each machine implementation to check if the given boot
825 * devices match the actual hardware implementation and firmware
828 if (*p < 'a' || *p > 'p') {
829 fprintf(stderr, "Invalid boot device '%c'\n", *p);
832 if (bitmap & (1 << (*p - 'a'))) {
833 fprintf(stderr, "Boot device '%c' was given twice\n", *p);
836 bitmap |= 1 << (*p - 'a');
840 static void restore_boot_devices(void *opaque)
842 char *standard_boot_devices = opaque;
843 static int first = 1;
845 /* Restore boot order and remove ourselves after the first boot */
851 qemu_boot_set(standard_boot_devices);
853 qemu_unregister_reset(restore_boot_devices, standard_boot_devices);
854 g_free(standard_boot_devices);
857 void add_boot_device_path(int32_t bootindex, DeviceState *dev,
860 FWBootEntry *node, *i;
866 assert(dev != NULL || suffix != NULL);
868 node = g_malloc0(sizeof(FWBootEntry));
869 node->bootindex = bootindex;
870 node->suffix = suffix ? g_strdup(suffix) : NULL;
873 QTAILQ_FOREACH(i, &fw_boot_order, link) {
874 if (i->bootindex == bootindex) {
875 fprintf(stderr, "Two devices with same boot index %d\n", bootindex);
877 } else if (i->bootindex < bootindex) {
880 QTAILQ_INSERT_BEFORE(i, node, link);
883 QTAILQ_INSERT_TAIL(&fw_boot_order, node, link);
887 * This function returns null terminated string that consist of new line
888 * separated device paths.
890 * memory pointed by "size" is assigned total length of the array in bytes
893 char *get_boot_devices_list(uint32_t *size)
899 QTAILQ_FOREACH(i, &fw_boot_order, link) {
900 char *devpath = NULL, *bootpath;
904 devpath = qdev_get_fw_dev_path(i->dev);
908 if (i->suffix && devpath) {
909 size_t bootpathlen = strlen(devpath) + strlen(i->suffix) + 1;
911 bootpath = g_malloc(bootpathlen);
912 snprintf(bootpath, bootpathlen, "%s%s", devpath, i->suffix);
914 } else if (devpath) {
918 bootpath = g_strdup(i->suffix);
922 list[total-1] = '\n';
924 len = strlen(bootpath) + 1;
925 list = g_realloc(list, total + len);
926 memcpy(&list[total], bootpath, len);
936 static void numa_add(const char *optarg)
940 unsigned long long value, endvalue;
943 optarg = get_opt_name(option, 128, optarg, ',') + 1;
944 if (!strcmp(option, "node")) {
945 if (get_param_value(option, 128, "nodeid", optarg) == 0) {
946 nodenr = nb_numa_nodes;
948 nodenr = strtoull(option, NULL, 10);
951 if (get_param_value(option, 128, "mem", optarg) == 0) {
952 node_mem[nodenr] = 0;
955 sval = strtosz(option, &endptr);
956 if (sval < 0 || *endptr) {
957 fprintf(stderr, "qemu: invalid numa mem size: %s\n", optarg);
960 node_mem[nodenr] = sval;
962 if (get_param_value(option, 128, "cpus", optarg) == 0) {
963 node_cpumask[nodenr] = 0;
965 value = strtoull(option, &endptr, 10);
968 fprintf(stderr, "only 64 CPUs in NUMA mode supported.\n");
970 if (*endptr == '-') {
971 endvalue = strtoull(endptr+1, &endptr, 10);
972 if (endvalue >= 63) {
975 "only 63 CPUs in NUMA mode supported.\n");
977 value = (2ULL << endvalue) - (1ULL << value);
979 value = 1ULL << value;
982 node_cpumask[nodenr] = value;
989 static void smp_parse(const char *optarg)
991 int smp, sockets = 0, threads = 0, cores = 0;
995 smp = strtoul(optarg, &endptr, 10);
996 if (endptr != optarg) {
997 if (*endptr == ',') {
1001 if (get_param_value(option, 128, "sockets", endptr) != 0)
1002 sockets = strtoull(option, NULL, 10);
1003 if (get_param_value(option, 128, "cores", endptr) != 0)
1004 cores = strtoull(option, NULL, 10);
1005 if (get_param_value(option, 128, "threads", endptr) != 0)
1006 threads = strtoull(option, NULL, 10);
1007 if (get_param_value(option, 128, "maxcpus", endptr) != 0)
1008 max_cpus = strtoull(option, NULL, 10);
1010 /* compute missing values, prefer sockets over cores over threads */
1011 if (smp == 0 || sockets == 0) {
1012 sockets = sockets > 0 ? sockets : 1;
1013 cores = cores > 0 ? cores : 1;
1014 threads = threads > 0 ? threads : 1;
1016 smp = cores * threads * sockets;
1020 threads = threads > 0 ? threads : 1;
1021 cores = smp / (sockets * threads);
1023 threads = smp / (cores * sockets);
1027 smp_cores = cores > 0 ? cores : 1;
1028 smp_threads = threads > 0 ? threads : 1;
1030 max_cpus = smp_cpus;
1033 /***********************************************************/
1036 static int usb_device_add(const char *devname)
1039 USBDevice *dev = NULL;
1044 /* drivers with .usbdevice_name entry in USBDeviceInfo */
1045 dev = usbdevice_create(devname);
1049 /* the other ones */
1050 #ifndef CONFIG_LINUX
1051 /* only the linux version is qdev-ified, usb-bsd still needs this */
1052 if (strstart(devname, "host:", &p)) {
1053 dev = usb_host_device_open(p);
1056 if (!strcmp(devname, "bt") || strstart(devname, "bt:", &p)) {
1057 dev = usb_bt_init(devname[2] ? hci_init(p) :
1058 bt_new_hci(qemu_find_bt_vlan(0)));
1069 static int usb_device_del(const char *devname)
1074 if (strstart(devname, "host:", &p))
1075 return usb_host_device_close(p);
1080 p = strchr(devname, '.');
1083 bus_num = strtoul(devname, NULL, 0);
1084 addr = strtoul(p + 1, NULL, 0);
1086 return usb_device_delete_addr(bus_num, addr);
1089 static int usb_parse(const char *cmdline)
1092 r = usb_device_add(cmdline);
1094 fprintf(stderr, "qemu: could not add USB device '%s'\n", cmdline);
1099 void do_usb_add(Monitor *mon, const QDict *qdict)
1101 const char *devname = qdict_get_str(qdict, "devname");
1102 if (usb_device_add(devname) < 0) {
1103 error_report("could not add USB device '%s'", devname);
1107 void do_usb_del(Monitor *mon, const QDict *qdict)
1109 const char *devname = qdict_get_str(qdict, "devname");
1110 if (usb_device_del(devname) < 0) {
1111 error_report("could not delete USB device '%s'", devname);
1115 /***********************************************************/
1116 /* PCMCIA/Cardbus */
1118 static struct pcmcia_socket_entry_s {
1119 PCMCIASocket *socket;
1120 struct pcmcia_socket_entry_s *next;
1121 } *pcmcia_sockets = 0;
1123 void pcmcia_socket_register(PCMCIASocket *socket)
1125 struct pcmcia_socket_entry_s *entry;
1127 entry = g_malloc(sizeof(struct pcmcia_socket_entry_s));
1128 entry->socket = socket;
1129 entry->next = pcmcia_sockets;
1130 pcmcia_sockets = entry;
1133 void pcmcia_socket_unregister(PCMCIASocket *socket)
1135 struct pcmcia_socket_entry_s *entry, **ptr;
1137 ptr = &pcmcia_sockets;
1138 for (entry = *ptr; entry; ptr = &entry->next, entry = *ptr)
1139 if (entry->socket == socket) {
1145 void pcmcia_info(Monitor *mon)
1147 struct pcmcia_socket_entry_s *iter;
1149 if (!pcmcia_sockets)
1150 monitor_printf(mon, "No PCMCIA sockets\n");
1152 for (iter = pcmcia_sockets; iter; iter = iter->next)
1153 monitor_printf(mon, "%s: %s\n", iter->socket->slot_string,
1154 iter->socket->attached ? iter->socket->card_string :
1158 /***********************************************************/
1159 /* machine registration */
1161 static QEMUMachine *first_machine = NULL;
1162 QEMUMachine *current_machine = NULL;
1164 int qemu_register_machine(QEMUMachine *m)
1167 pm = &first_machine;
1175 static QEMUMachine *find_machine(const char *name)
1179 for(m = first_machine; m != NULL; m = m->next) {
1180 if (!strcmp(m->name, name))
1182 if (m->alias && !strcmp(m->alias, name))
1188 static QEMUMachine *find_default_machine(void)
1192 for(m = first_machine; m != NULL; m = m->next) {
1193 if (m->is_default) {
1200 /***********************************************************/
1201 /* main execution loop */
1203 static void gui_update(void *opaque)
1205 uint64_t interval = GUI_REFRESH_INTERVAL;
1206 DisplayState *ds = opaque;
1207 DisplayChangeListener *dcl = ds->listeners;
1211 while (dcl != NULL) {
1212 if (dcl->gui_timer_interval &&
1213 dcl->gui_timer_interval < interval)
1214 interval = dcl->gui_timer_interval;
1217 qemu_mod_timer(ds->gui_timer, interval + qemu_get_clock_ms(rt_clock));
1220 struct vm_change_state_entry {
1221 VMChangeStateHandler *cb;
1223 QLIST_ENTRY (vm_change_state_entry) entries;
1226 static QLIST_HEAD(vm_change_state_head, vm_change_state_entry) vm_change_state_head;
1228 VMChangeStateEntry *qemu_add_vm_change_state_handler(VMChangeStateHandler *cb,
1231 VMChangeStateEntry *e;
1233 e = g_malloc0(sizeof (*e));
1237 QLIST_INSERT_HEAD(&vm_change_state_head, e, entries);
1241 void qemu_del_vm_change_state_handler(VMChangeStateEntry *e)
1243 QLIST_REMOVE (e, entries);
1247 void vm_state_notify(int running, RunState state)
1249 VMChangeStateEntry *e;
1251 trace_vm_state_notify(running, state);
1253 for (e = vm_change_state_head.lh_first; e; e = e->entries.le_next) {
1254 e->cb(e->opaque, running, state);
1260 if (!runstate_is_running()) {
1262 runstate_set(RUN_STATE_RUNNING);
1263 vm_state_notify(1, RUN_STATE_RUNNING);
1265 monitor_protocol_event(QEVENT_RESUME, NULL);
1269 /* reset/shutdown handler */
1271 typedef struct QEMUResetEntry {
1272 QTAILQ_ENTRY(QEMUResetEntry) entry;
1273 QEMUResetHandler *func;
1277 static QTAILQ_HEAD(reset_handlers, QEMUResetEntry) reset_handlers =
1278 QTAILQ_HEAD_INITIALIZER(reset_handlers);
1279 static int reset_requested;
1280 static int shutdown_requested, shutdown_signal = -1;
1281 static pid_t shutdown_pid;
1282 static int powerdown_requested;
1283 static int debug_requested;
1284 static RunState vmstop_requested = RUN_STATE_MAX;
1286 int qemu_shutdown_requested_get(void)
1288 return shutdown_requested;
1291 int qemu_reset_requested_get(void)
1293 return reset_requested;
1296 int qemu_shutdown_requested(void)
1298 int r = shutdown_requested;
1299 shutdown_requested = 0;
1303 void qemu_kill_report(void)
1305 if (shutdown_signal != -1) {
1306 fprintf(stderr, "qemu: terminating on signal %d", shutdown_signal);
1307 if (shutdown_pid == 0) {
1308 /* This happens for eg ^C at the terminal, so it's worth
1309 * avoiding printing an odd message in that case.
1311 fputc('\n', stderr);
1313 fprintf(stderr, " from pid " FMT_pid "\n", shutdown_pid);
1315 shutdown_signal = -1;
1319 int qemu_reset_requested(void)
1321 int r = reset_requested;
1322 reset_requested = 0;
1326 int qemu_powerdown_requested(void)
1328 int r = powerdown_requested;
1329 powerdown_requested = 0;
1333 static int qemu_debug_requested(void)
1335 int r = debug_requested;
1336 debug_requested = 0;
1340 /* We use RUN_STATE_MAX but any invalid value will do */
1341 static bool qemu_vmstop_requested(RunState *r)
1343 if (vmstop_requested < RUN_STATE_MAX) {
1344 *r = vmstop_requested;
1345 vmstop_requested = RUN_STATE_MAX;
1352 void qemu_register_reset(QEMUResetHandler *func, void *opaque)
1354 QEMUResetEntry *re = g_malloc0(sizeof(QEMUResetEntry));
1357 re->opaque = opaque;
1358 QTAILQ_INSERT_TAIL(&reset_handlers, re, entry);
1361 void qemu_unregister_reset(QEMUResetHandler *func, void *opaque)
1365 QTAILQ_FOREACH(re, &reset_handlers, entry) {
1366 if (re->func == func && re->opaque == opaque) {
1367 QTAILQ_REMOVE(&reset_handlers, re, entry);
1374 void qemu_system_reset(bool report)
1376 QEMUResetEntry *re, *nre;
1378 /* reset all devices */
1379 QTAILQ_FOREACH_SAFE(re, &reset_handlers, entry, nre) {
1380 re->func(re->opaque);
1383 monitor_protocol_event(QEVENT_RESET, NULL);
1385 cpu_synchronize_all_post_reset();
1388 void qemu_system_reset_request(void)
1391 shutdown_requested = 1;
1393 reset_requested = 1;
1396 qemu_notify_event();
1399 void qemu_system_killed(int signal, pid_t pid)
1401 shutdown_signal = signal;
1404 qemu_system_shutdown_request();
1407 void qemu_system_shutdown_request(void)
1409 shutdown_requested = 1;
1410 qemu_notify_event();
1413 void qemu_system_powerdown_request(void)
1415 powerdown_requested = 1;
1416 qemu_notify_event();
1419 void qemu_system_debug_request(void)
1421 debug_requested = 1;
1422 qemu_notify_event();
1425 void qemu_system_vmstop_request(RunState state)
1427 vmstop_requested = state;
1428 qemu_notify_event();
1431 qemu_irq qemu_system_powerdown;
1433 static bool main_loop_should_exit(void)
1436 if (qemu_debug_requested()) {
1437 vm_stop(RUN_STATE_DEBUG);
1439 if (qemu_shutdown_requested()) {
1441 monitor_protocol_event(QEVENT_SHUTDOWN, NULL);
1443 vm_stop(RUN_STATE_SHUTDOWN);
1448 if (qemu_reset_requested()) {
1450 cpu_synchronize_all_states();
1451 qemu_system_reset(VMRESET_REPORT);
1453 if (runstate_check(RUN_STATE_INTERNAL_ERROR) ||
1454 runstate_check(RUN_STATE_SHUTDOWN)) {
1455 runstate_set(RUN_STATE_PAUSED);
1458 if (qemu_powerdown_requested()) {
1459 monitor_protocol_event(QEVENT_POWERDOWN, NULL);
1460 qemu_irq_raise(qemu_system_powerdown);
1462 if (qemu_vmstop_requested(&r)) {
1468 static void main_loop(void)
1472 #ifdef CONFIG_PROFILER
1476 nonblocking = !kvm_enabled() && last_io > 0;
1477 #ifdef CONFIG_PROFILER
1478 ti = profile_getclock();
1480 last_io = main_loop_wait(nonblocking);
1481 #ifdef CONFIG_PROFILER
1482 dev_time += profile_getclock() - ti;
1484 } while (!main_loop_should_exit());
1487 static void version(void)
1489 printf("QEMU emulator version " QEMU_VERSION QEMU_PKGVERSION ", Copyright (c) 2003-2008 Fabrice Bellard\n");
1492 static void help(int exitcode)
1495 printf("usage: %s [options] [disk_image]\n\n"
1496 "'disk_image' is a raw hard disk image for IDE hard disk 0\n\n",
1497 error_get_progname());
1499 #define QEMU_OPTIONS_GENERATE_HELP
1500 #include "qemu-options-wrapper.h"
1502 printf("\nDuring emulation, the following keys are useful:\n"
1503 "ctrl-alt-f toggle full screen\n"
1504 "ctrl-alt-n switch to virtual console 'n'\n"
1505 "ctrl-alt toggle mouse and keyboard grab\n"
1507 "When using -nographic, press 'ctrl-a h' to get some help.\n");
1512 #define HAS_ARG 0x0001
1514 typedef struct QEMUOption {
1521 static const QEMUOption qemu_options[] = {
1522 { "h", 0, QEMU_OPTION_h, QEMU_ARCH_ALL },
1523 #define QEMU_OPTIONS_GENERATE_OPTIONS
1524 #include "qemu-options-wrapper.h"
1527 static void select_vgahw (const char *p)
1532 vga_interface_type = VGA_NONE;
1533 if (strstart(p, "std", &opts)) {
1534 vga_interface_type = VGA_STD;
1535 } else if (strstart(p, "cirrus", &opts)) {
1536 vga_interface_type = VGA_CIRRUS;
1537 } else if (strstart(p, "vmware", &opts)) {
1538 vga_interface_type = VGA_VMWARE;
1539 } else if (strstart(p, "xenfb", &opts)) {
1540 vga_interface_type = VGA_XENFB;
1541 } else if (strstart(p, "qxl", &opts)) {
1542 vga_interface_type = VGA_QXL;
1543 } else if (!strstart(p, "none", &opts)) {
1545 fprintf(stderr, "Unknown vga type: %s\n", p);
1549 const char *nextopt;
1551 if (strstart(opts, ",retrace=", &nextopt)) {
1553 if (strstart(opts, "dumb", &nextopt))
1554 vga_retrace_method = VGA_RETRACE_DUMB;
1555 else if (strstart(opts, "precise", &nextopt))
1556 vga_retrace_method = VGA_RETRACE_PRECISE;
1557 else goto invalid_vga;
1558 } else goto invalid_vga;
1563 static DisplayType select_display(const char *p)
1566 DisplayType display = DT_DEFAULT;
1568 if (strstart(p, "sdl", &opts)) {
1572 const char *nextopt;
1574 if (strstart(opts, ",frame=", &nextopt)) {
1576 if (strstart(opts, "on", &nextopt)) {
1578 } else if (strstart(opts, "off", &nextopt)) {
1581 goto invalid_sdl_args;
1583 } else if (strstart(opts, ",alt_grab=", &nextopt)) {
1585 if (strstart(opts, "on", &nextopt)) {
1587 } else if (strstart(opts, "off", &nextopt)) {
1590 goto invalid_sdl_args;
1592 } else if (strstart(opts, ",ctrl_grab=", &nextopt)) {
1594 if (strstart(opts, "on", &nextopt)) {
1596 } else if (strstart(opts, "off", &nextopt)) {
1599 goto invalid_sdl_args;
1601 } else if (strstart(opts, ",window_close=", &nextopt)) {
1603 if (strstart(opts, "on", &nextopt)) {
1605 } else if (strstart(opts, "off", &nextopt)) {
1608 goto invalid_sdl_args;
1612 fprintf(stderr, "Invalid SDL option string: %s\n", p);
1618 fprintf(stderr, "SDL support is disabled\n");
1621 } else if (strstart(p, "vnc", &opts)) {
1626 const char *nextopt;
1628 if (strstart(opts, "=", &nextopt)) {
1629 vnc_display = nextopt;
1633 fprintf(stderr, "VNC requires a display argument vnc=<display>\n");
1637 fprintf(stderr, "VNC support is disabled\n");
1640 } else if (strstart(p, "curses", &opts)) {
1641 #ifdef CONFIG_CURSES
1642 display = DT_CURSES;
1644 fprintf(stderr, "Curses support is disabled\n");
1647 } else if (strstart(p, "none", &opts)) {
1650 fprintf(stderr, "Unknown display type: %s\n", p);
1657 static int balloon_parse(const char *arg)
1661 if (strcmp(arg, "none") == 0) {
1665 if (!strncmp(arg, "virtio", 6)) {
1666 if (arg[6] == ',') {
1667 /* have params -> parse them */
1668 opts = qemu_opts_parse(qemu_find_opts("device"), arg+7, 0);
1672 /* create empty opts */
1673 opts = qemu_opts_create(qemu_find_opts("device"), NULL, 0);
1675 qemu_opt_set(opts, "driver", "virtio-balloon");
1682 char *qemu_find_file(int type, const char *name)
1688 /* If name contains path separators then try it as a straight path. */
1689 if ((strchr(name, '/') || strchr(name, '\\'))
1690 && access(name, R_OK) == 0) {
1691 return g_strdup(name);
1694 case QEMU_FILE_TYPE_BIOS:
1697 case QEMU_FILE_TYPE_KEYMAP:
1698 subdir = "keymaps/";
1703 len = strlen(data_dir) + strlen(name) + strlen(subdir) + 2;
1704 buf = g_malloc0(len);
1705 snprintf(buf, len, "%s/%s%s", data_dir, subdir, name);
1706 if (access(buf, R_OK)) {
1713 static int device_help_func(QemuOpts *opts, void *opaque)
1715 return qdev_device_help(opts);
1718 static int device_init_func(QemuOpts *opts, void *opaque)
1722 dev = qdev_device_add(opts);
1728 static int chardev_init_func(QemuOpts *opts, void *opaque)
1730 CharDriverState *chr;
1732 chr = qemu_chr_new_from_opts(opts, NULL);
1738 #ifdef CONFIG_VIRTFS
1739 static int fsdev_init_func(QemuOpts *opts, void *opaque)
1742 ret = qemu_fsdev_add(opts);
1748 static int mon_init_func(QemuOpts *opts, void *opaque)
1750 CharDriverState *chr;
1751 const char *chardev;
1755 mode = qemu_opt_get(opts, "mode");
1759 if (strcmp(mode, "readline") == 0) {
1760 flags = MONITOR_USE_READLINE;
1761 } else if (strcmp(mode, "control") == 0) {
1762 flags = MONITOR_USE_CONTROL;
1764 fprintf(stderr, "unknown monitor mode \"%s\"\n", mode);
1768 if (qemu_opt_get_bool(opts, "pretty", 0))
1769 flags |= MONITOR_USE_PRETTY;
1771 if (qemu_opt_get_bool(opts, "default", 0))
1772 flags |= MONITOR_IS_DEFAULT;
1774 chardev = qemu_opt_get(opts, "chardev");
1775 chr = qemu_chr_find(chardev);
1777 fprintf(stderr, "chardev \"%s\" not found\n", chardev);
1781 monitor_init(chr, flags);
1785 static void monitor_parse(const char *optarg, const char *mode)
1787 static int monitor_device_index = 0;
1793 if (strstart(optarg, "chardev:", &p)) {
1794 snprintf(label, sizeof(label), "%s", p);
1796 snprintf(label, sizeof(label), "compat_monitor%d",
1797 monitor_device_index);
1798 if (monitor_device_index == 0) {
1801 opts = qemu_chr_parse_compat(label, optarg);
1803 fprintf(stderr, "parse error: %s\n", optarg);
1808 opts = qemu_opts_create(qemu_find_opts("mon"), label, 1);
1810 fprintf(stderr, "duplicate chardev: %s\n", label);
1813 qemu_opt_set(opts, "mode", mode);
1814 qemu_opt_set(opts, "chardev", label);
1816 qemu_opt_set(opts, "default", "on");
1817 monitor_device_index++;
1820 struct device_config {
1822 DEV_USB, /* -usbdevice */
1824 DEV_SERIAL, /* -serial */
1825 DEV_PARALLEL, /* -parallel */
1826 DEV_VIRTCON, /* -virtioconsole */
1827 DEV_DEBUGCON, /* -debugcon */
1829 const char *cmdline;
1830 QTAILQ_ENTRY(device_config) next;
1832 QTAILQ_HEAD(, device_config) device_configs = QTAILQ_HEAD_INITIALIZER(device_configs);
1834 static void add_device_config(int type, const char *cmdline)
1836 struct device_config *conf;
1838 conf = g_malloc0(sizeof(*conf));
1840 conf->cmdline = cmdline;
1841 QTAILQ_INSERT_TAIL(&device_configs, conf, next);
1844 static int foreach_device_config(int type, int (*func)(const char *cmdline))
1846 struct device_config *conf;
1849 QTAILQ_FOREACH(conf, &device_configs, next) {
1850 if (conf->type != type)
1852 rc = func(conf->cmdline);
1859 static int serial_parse(const char *devname)
1861 static int index = 0;
1864 if (strcmp(devname, "none") == 0)
1866 if (index == MAX_SERIAL_PORTS) {
1867 fprintf(stderr, "qemu: too many serial ports\n");
1870 snprintf(label, sizeof(label), "serial%d", index);
1871 serial_hds[index] = qemu_chr_new(label, devname, NULL);
1872 if (!serial_hds[index]) {
1873 fprintf(stderr, "qemu: could not open serial device '%s': %s\n",
1874 devname, strerror(errno));
1881 static int parallel_parse(const char *devname)
1883 static int index = 0;
1886 if (strcmp(devname, "none") == 0)
1888 if (index == MAX_PARALLEL_PORTS) {
1889 fprintf(stderr, "qemu: too many parallel ports\n");
1892 snprintf(label, sizeof(label), "parallel%d", index);
1893 parallel_hds[index] = qemu_chr_new(label, devname, NULL);
1894 if (!parallel_hds[index]) {
1895 fprintf(stderr, "qemu: could not open parallel device '%s': %s\n",
1896 devname, strerror(errno));
1903 static int virtcon_parse(const char *devname)
1905 QemuOptsList *device = qemu_find_opts("device");
1906 static int index = 0;
1908 QemuOpts *bus_opts, *dev_opts;
1910 if (strcmp(devname, "none") == 0)
1912 if (index == MAX_VIRTIO_CONSOLES) {
1913 fprintf(stderr, "qemu: too many virtio consoles\n");
1917 bus_opts = qemu_opts_create(device, NULL, 0);
1918 qemu_opt_set(bus_opts, "driver", "virtio-serial");
1920 dev_opts = qemu_opts_create(device, NULL, 0);
1921 qemu_opt_set(dev_opts, "driver", "virtconsole");
1923 snprintf(label, sizeof(label), "virtcon%d", index);
1924 virtcon_hds[index] = qemu_chr_new(label, devname, NULL);
1925 if (!virtcon_hds[index]) {
1926 fprintf(stderr, "qemu: could not open virtio console '%s': %s\n",
1927 devname, strerror(errno));
1930 qemu_opt_set(dev_opts, "chardev", label);
1936 static int debugcon_parse(const char *devname)
1940 if (!qemu_chr_new("debugcon", devname, NULL)) {
1943 opts = qemu_opts_create(qemu_find_opts("device"), "debugcon", 1);
1945 fprintf(stderr, "qemu: already have a debugcon device\n");
1948 qemu_opt_set(opts, "driver", "isa-debugcon");
1949 qemu_opt_set(opts, "chardev", "debugcon");
1953 static QEMUMachine *machine_parse(const char *name)
1955 QEMUMachine *m, *machine = NULL;
1958 machine = find_machine(name);
1963 printf("Supported machines are:\n");
1964 for (m = first_machine; m != NULL; m = m->next) {
1966 printf("%-10s %s (alias of %s)\n", m->alias, m->desc, m->name);
1968 printf("%-10s %s%s\n", m->name, m->desc,
1969 m->is_default ? " (default)" : "");
1971 exit(!name || *name != '?');
1974 static int tcg_init(void)
1976 tcg_exec_init(tcg_tb_size * 1024 * 1024);
1981 const char *opt_name;
1983 int (*available)(void);
1987 { "tcg", "tcg", tcg_available, tcg_init, &tcg_allowed },
1988 { "xen", "Xen", xen_available, xen_init, &xen_allowed },
1989 { "kvm", "KVM", kvm_available, kvm_init, &kvm_allowed },
1992 static int configure_accelerator(void)
1994 const char *p = NULL;
1997 bool accel_initalised = 0;
1998 bool init_failed = 0;
2000 QemuOptsList *list = qemu_find_opts("machine");
2001 if (!QTAILQ_EMPTY(&list->head)) {
2002 p = qemu_opt_get(QTAILQ_FIRST(&list->head), "accel");
2006 /* Use the default "accelerator", tcg */
2010 while (!accel_initalised && *p != '\0') {
2014 p = get_opt_name(buf, sizeof (buf), p, ':');
2015 for (i = 0; i < ARRAY_SIZE(accel_list); i++) {
2016 if (strcmp(accel_list[i].opt_name, buf) == 0) {
2017 *(accel_list[i].allowed) = 1;
2018 ret = accel_list[i].init();
2021 if (!accel_list[i].available()) {
2022 printf("%s not supported for this target\n",
2023 accel_list[i].name);
2025 fprintf(stderr, "failed to initialize %s: %s\n",
2029 *(accel_list[i].allowed) = 0;
2031 accel_initalised = 1;
2036 if (i == ARRAY_SIZE(accel_list)) {
2037 fprintf(stderr, "\"%s\" accelerator does not exist.\n", buf);
2041 if (!accel_initalised) {
2042 fprintf(stderr, "No accelerator found!\n");
2047 fprintf(stderr, "Back to %s accelerator.\n", accel_list[i].name);
2050 return !accel_initalised;
2053 void qemu_add_exit_notifier(Notifier *notify)
2055 notifier_list_add(&exit_notifiers, notify);
2058 void qemu_remove_exit_notifier(Notifier *notify)
2060 notifier_list_remove(&exit_notifiers, notify);
2063 static void qemu_run_exit_notifiers(void)
2065 notifier_list_notify(&exit_notifiers, NULL);
2068 void qemu_add_machine_init_done_notifier(Notifier *notify)
2070 notifier_list_add(&machine_init_done_notifiers, notify);
2073 static void qemu_run_machine_init_done_notifiers(void)
2075 notifier_list_notify(&machine_init_done_notifiers, NULL);
2078 static const QEMUOption *lookup_opt(int argc, char **argv,
2079 const char **poptarg, int *poptind)
2081 const QEMUOption *popt;
2082 int optind = *poptind;
2083 char *r = argv[optind];
2086 loc_set_cmdline(argv, optind, 1);
2088 /* Treat --foo the same as -foo. */
2091 popt = qemu_options;
2094 error_report("invalid option");
2097 if (!strcmp(popt->name, r + 1))
2101 if (popt->flags & HAS_ARG) {
2102 if (optind >= argc) {
2103 error_report("requires an argument");
2106 optarg = argv[optind++];
2107 loc_set_cmdline(argv, optind - 2, 2);
2118 static gpointer malloc_and_trace(gsize n_bytes)
2120 void *ptr = malloc(n_bytes);
2121 trace_g_malloc(n_bytes, ptr);
2125 static gpointer realloc_and_trace(gpointer mem, gsize n_bytes)
2127 void *ptr = realloc(mem, n_bytes);
2128 trace_g_realloc(mem, n_bytes, ptr);
2132 static void free_and_trace(gpointer mem)
2138 int main(int argc, char **argv, char **envp)
2140 const char *gdbstub_dev = NULL;
2142 int snapshot, linux_boot;
2143 const char *icount_option = NULL;
2144 const char *initrd_filename;
2145 const char *kernel_filename, *kernel_cmdline;
2146 char boot_devices[33] = "cad"; /* default to HD->floppy->CD-ROM */
2148 DisplayChangeListener *dcl;
2149 int cyls, heads, secs, translation;
2150 QemuOpts *hda_opts = NULL, *opts;
2151 QemuOptsList *olist;
2154 const char *loadvm = NULL;
2155 QEMUMachine *machine;
2156 const char *cpu_model;
2157 const char *pid_file = NULL;
2158 const char *incoming = NULL;
2160 int show_vnc_port = 0;
2163 const char *log_mask = NULL;
2164 const char *log_file = NULL;
2165 GMemVTable mem_trace = {
2166 .malloc = malloc_and_trace,
2167 .realloc = realloc_and_trace,
2168 .free = free_and_trace,
2170 const char *trace_events = NULL;
2171 const char *trace_file = NULL;
2173 atexit(qemu_run_exit_notifiers);
2174 error_set_progname(argv[0]);
2176 g_mem_set_vtable(&mem_trace);
2177 if (!g_thread_supported()) {
2178 #if !GLIB_CHECK_VERSION(2, 31, 0)
2179 g_thread_init(NULL);
2181 fprintf(stderr, "glib threading failed to initialize.\n");
2189 rtc_clock = host_clock;
2191 qemu_cache_utils_init(envp);
2193 QLIST_INIT (&vm_change_state_head);
2194 os_setup_early_signal_handling();
2196 module_call_init(MODULE_INIT_MACHINE);
2197 machine = find_default_machine();
2199 initrd_filename = NULL;
2202 kernel_filename = NULL;
2203 kernel_cmdline = "";
2204 cyls = heads = secs = 0;
2205 translation = BIOS_ATA_TRANSLATION_AUTO;
2207 for (i = 0; i < MAX_NODES; i++) {
2209 node_cpumask[i] = 0;
2217 /* first pass of option parsing */
2219 while (optind < argc) {
2220 if (argv[optind][0] != '-') {
2225 const QEMUOption *popt;
2227 popt = lookup_opt(argc, argv, &optarg, &optind);
2228 switch (popt->index) {
2229 case QEMU_OPTION_nodefconfig:
2239 ret = qemu_read_config_file(CONFIG_QEMU_CONFDIR "/qemu.conf");
2240 if (ret < 0 && ret != -ENOENT) {
2244 ret = qemu_read_config_file(arch_config_name);
2245 if (ret < 0 && ret != -ENOENT) {
2251 /* second pass of option parsing */
2256 if (argv[optind][0] != '-') {
2257 hda_opts = drive_add(IF_DEFAULT, 0, argv[optind++], HD_OPTS);
2259 const QEMUOption *popt;
2261 popt = lookup_opt(argc, argv, &optarg, &optind);
2262 if (!(popt->arch_mask & arch_type)) {
2263 printf("Option %s not supported for this target\n", popt->name);
2266 switch(popt->index) {
2268 machine = machine_parse(optarg);
2270 case QEMU_OPTION_cpu:
2271 /* hw initialization will check this */
2272 if (*optarg == '?') {
2273 list_cpus(stdout, &fprintf, optarg);
2279 case QEMU_OPTION_initrd:
2280 initrd_filename = optarg;
2282 case QEMU_OPTION_hda:
2286 snprintf(buf, sizeof(buf), "%s", HD_OPTS);
2288 snprintf(buf, sizeof(buf),
2289 "%s,cyls=%d,heads=%d,secs=%d%s",
2290 HD_OPTS , cyls, heads, secs,
2291 translation == BIOS_ATA_TRANSLATION_LBA ?
2293 translation == BIOS_ATA_TRANSLATION_NONE ?
2294 ",trans=none" : "");
2295 drive_add(IF_DEFAULT, 0, optarg, buf);
2298 case QEMU_OPTION_hdb:
2299 case QEMU_OPTION_hdc:
2300 case QEMU_OPTION_hdd:
2301 drive_add(IF_DEFAULT, popt->index - QEMU_OPTION_hda, optarg,
2304 case QEMU_OPTION_drive:
2305 if (drive_def(optarg) == NULL) {
2309 case QEMU_OPTION_set:
2310 if (qemu_set_option(optarg) != 0)
2313 case QEMU_OPTION_global:
2314 if (qemu_global_option(optarg) != 0)
2317 case QEMU_OPTION_mtdblock:
2318 drive_add(IF_MTD, -1, optarg, MTD_OPTS);
2320 case QEMU_OPTION_sd:
2321 drive_add(IF_SD, 0, optarg, SD_OPTS);
2323 case QEMU_OPTION_pflash:
2324 drive_add(IF_PFLASH, -1, optarg, PFLASH_OPTS);
2326 case QEMU_OPTION_snapshot:
2329 case QEMU_OPTION_hdachs:
2333 cyls = strtol(p, (char **)&p, 0);
2334 if (cyls < 1 || cyls > 16383)
2339 heads = strtol(p, (char **)&p, 0);
2340 if (heads < 1 || heads > 16)
2345 secs = strtol(p, (char **)&p, 0);
2346 if (secs < 1 || secs > 63)
2350 if (!strcmp(p, "none"))
2351 translation = BIOS_ATA_TRANSLATION_NONE;
2352 else if (!strcmp(p, "lba"))
2353 translation = BIOS_ATA_TRANSLATION_LBA;
2354 else if (!strcmp(p, "auto"))
2355 translation = BIOS_ATA_TRANSLATION_AUTO;
2358 } else if (*p != '\0') {
2360 fprintf(stderr, "qemu: invalid physical CHS format\n");
2363 if (hda_opts != NULL) {
2365 snprintf(num, sizeof(num), "%d", cyls);
2366 qemu_opt_set(hda_opts, "cyls", num);
2367 snprintf(num, sizeof(num), "%d", heads);
2368 qemu_opt_set(hda_opts, "heads", num);
2369 snprintf(num, sizeof(num), "%d", secs);
2370 qemu_opt_set(hda_opts, "secs", num);
2371 if (translation == BIOS_ATA_TRANSLATION_LBA)
2372 qemu_opt_set(hda_opts, "trans", "lba");
2373 if (translation == BIOS_ATA_TRANSLATION_NONE)
2374 qemu_opt_set(hda_opts, "trans", "none");
2378 case QEMU_OPTION_numa:
2379 if (nb_numa_nodes >= MAX_NODES) {
2380 fprintf(stderr, "qemu: too many NUMA nodes\n");
2385 case QEMU_OPTION_display:
2386 display_type = select_display(optarg);
2388 case QEMU_OPTION_nographic:
2389 display_type = DT_NOGRAPHIC;
2391 case QEMU_OPTION_curses:
2392 #ifdef CONFIG_CURSES
2393 display_type = DT_CURSES;
2395 fprintf(stderr, "Curses support is disabled\n");
2399 case QEMU_OPTION_portrait:
2400 graphic_rotate = 90;
2402 case QEMU_OPTION_rotate:
2403 graphic_rotate = strtol(optarg, (char **) &optarg, 10);
2404 if (graphic_rotate != 0 && graphic_rotate != 90 &&
2405 graphic_rotate != 180 && graphic_rotate != 270) {
2407 "qemu: only 90, 180, 270 deg rotation is available\n");
2411 case QEMU_OPTION_kernel:
2412 kernel_filename = optarg;
2414 case QEMU_OPTION_append:
2415 kernel_cmdline = optarg;
2417 case QEMU_OPTION_cdrom:
2418 drive_add(IF_DEFAULT, 2, optarg, CDROM_OPTS);
2420 case QEMU_OPTION_boot:
2422 static const char * const params[] = {
2423 "order", "once", "menu",
2424 "splash", "splash-time", NULL
2426 char buf[sizeof(boot_devices)];
2427 char *standard_boot_devices;
2430 if (!strchr(optarg, '=')) {
2432 pstrcpy(buf, sizeof(buf), optarg);
2433 } else if (check_params(buf, sizeof(buf), params, optarg) < 0) {
2435 "qemu: unknown boot parameter '%s' in '%s'\n",
2441 get_param_value(buf, sizeof(buf), "order", optarg)) {
2442 validate_bootdevices(buf);
2443 pstrcpy(boot_devices, sizeof(boot_devices), buf);
2446 if (get_param_value(buf, sizeof(buf),
2448 validate_bootdevices(buf);
2449 standard_boot_devices = g_strdup(boot_devices);
2450 pstrcpy(boot_devices, sizeof(boot_devices), buf);
2451 qemu_register_reset(restore_boot_devices,
2452 standard_boot_devices);
2454 if (get_param_value(buf, sizeof(buf),
2456 if (!strcmp(buf, "on")) {
2458 } else if (!strcmp(buf, "off")) {
2462 "qemu: invalid option value '%s'\n",
2467 qemu_opts_parse(qemu_find_opts("boot-opts"),
2472 case QEMU_OPTION_fda:
2473 case QEMU_OPTION_fdb:
2474 drive_add(IF_FLOPPY, popt->index - QEMU_OPTION_fda,
2477 case QEMU_OPTION_no_fd_bootchk:
2480 case QEMU_OPTION_netdev:
2481 if (net_client_parse(qemu_find_opts("netdev"), optarg) == -1) {
2485 case QEMU_OPTION_net:
2486 if (net_client_parse(qemu_find_opts("net"), optarg) == -1) {
2491 case QEMU_OPTION_tftp:
2492 legacy_tftp_prefix = optarg;
2494 case QEMU_OPTION_bootp:
2495 legacy_bootp_filename = optarg;
2497 case QEMU_OPTION_redir:
2498 if (net_slirp_redir(optarg) < 0)
2502 case QEMU_OPTION_bt:
2503 add_device_config(DEV_BT, optarg);
2505 case QEMU_OPTION_audio_help:
2506 if (!(audio_available())) {
2507 printf("Option %s not supported for this target\n", popt->name);
2513 case QEMU_OPTION_soundhw:
2514 if (!(audio_available())) {
2515 printf("Option %s not supported for this target\n", popt->name);
2518 select_soundhw (optarg);
2523 case QEMU_OPTION_version:
2527 case QEMU_OPTION_m: {
2531 value = strtosz(optarg, &end);
2532 if (value < 0 || *end) {
2533 fprintf(stderr, "qemu: invalid ram size: %s\n", optarg);
2537 if (value != (uint64_t)(ram_addr_t)value) {
2538 fprintf(stderr, "qemu: ram size too large\n");
2544 case QEMU_OPTION_mempath:
2548 case QEMU_OPTION_mem_prealloc:
2559 gdbstub_dev = "tcp::" DEFAULT_GDBSTUB_PORT;
2561 case QEMU_OPTION_gdb:
2562 gdbstub_dev = optarg;
2567 case QEMU_OPTION_bios:
2570 case QEMU_OPTION_singlestep:
2577 keyboard_layout = optarg;
2579 case QEMU_OPTION_localtime:
2582 case QEMU_OPTION_vga:
2583 select_vgahw (optarg);
2590 w = strtol(p, (char **)&p, 10);
2593 fprintf(stderr, "qemu: invalid resolution or depth\n");
2599 h = strtol(p, (char **)&p, 10);
2604 depth = strtol(p, (char **)&p, 10);
2605 if (depth != 8 && depth != 15 && depth != 16 &&
2606 depth != 24 && depth != 32)
2608 } else if (*p == '\0') {
2609 depth = graphic_depth;
2616 graphic_depth = depth;
2619 case QEMU_OPTION_echr:
2622 term_escape_char = strtol(optarg, &r, 0);
2624 printf("Bad argument to echr\n");
2627 case QEMU_OPTION_monitor:
2628 monitor_parse(optarg, "readline");
2629 default_monitor = 0;
2631 case QEMU_OPTION_qmp:
2632 monitor_parse(optarg, "control");
2633 default_monitor = 0;
2635 case QEMU_OPTION_mon:
2636 opts = qemu_opts_parse(qemu_find_opts("mon"), optarg, 1);
2640 default_monitor = 0;
2642 case QEMU_OPTION_chardev:
2643 opts = qemu_opts_parse(qemu_find_opts("chardev"), optarg, 1);
2648 case QEMU_OPTION_fsdev:
2649 olist = qemu_find_opts("fsdev");
2651 fprintf(stderr, "fsdev is not supported by this qemu build.\n");
2654 opts = qemu_opts_parse(olist, optarg, 1);
2656 fprintf(stderr, "parse error: %s\n", optarg);
2660 case QEMU_OPTION_virtfs: {
2663 const char *writeout, *sock_fd, *socket;
2665 olist = qemu_find_opts("virtfs");
2667 fprintf(stderr, "virtfs is not supported by this qemu build.\n");
2670 opts = qemu_opts_parse(olist, optarg, 1);
2672 fprintf(stderr, "parse error: %s\n", optarg);
2676 if (qemu_opt_get(opts, "fsdriver") == NULL ||
2677 qemu_opt_get(opts, "mount_tag") == NULL) {
2678 fprintf(stderr, "Usage: -virtfs fsdriver,mount_tag=tag.\n");
2681 fsdev = qemu_opts_create(qemu_find_opts("fsdev"),
2682 qemu_opt_get(opts, "mount_tag"), 1);
2684 fprintf(stderr, "duplicate fsdev id: %s\n",
2685 qemu_opt_get(opts, "mount_tag"));
2689 writeout = qemu_opt_get(opts, "writeout");
2691 #ifdef CONFIG_SYNC_FILE_RANGE
2692 qemu_opt_set(fsdev, "writeout", writeout);
2694 fprintf(stderr, "writeout=immediate not supported on "
2699 qemu_opt_set(fsdev, "fsdriver", qemu_opt_get(opts, "fsdriver"));
2700 qemu_opt_set(fsdev, "path", qemu_opt_get(opts, "path"));
2701 qemu_opt_set(fsdev, "security_model",
2702 qemu_opt_get(opts, "security_model"));
2703 socket = qemu_opt_get(opts, "socket");
2705 qemu_opt_set(fsdev, "socket", socket);
2707 sock_fd = qemu_opt_get(opts, "sock_fd");
2709 qemu_opt_set(fsdev, "sock_fd", sock_fd);
2712 qemu_opt_set_bool(fsdev, "readonly",
2713 qemu_opt_get_bool(opts, "readonly", 0));
2714 device = qemu_opts_create(qemu_find_opts("device"), NULL, 0);
2715 qemu_opt_set(device, "driver", "virtio-9p-pci");
2716 qemu_opt_set(device, "fsdev",
2717 qemu_opt_get(opts, "mount_tag"));
2718 qemu_opt_set(device, "mount_tag",
2719 qemu_opt_get(opts, "mount_tag"));
2722 case QEMU_OPTION_virtfs_synth: {
2726 fsdev = qemu_opts_create(qemu_find_opts("fsdev"), "v_synth", 1);
2728 fprintf(stderr, "duplicate option: %s\n", "virtfs_synth");
2731 qemu_opt_set(fsdev, "fsdriver", "synth");
2733 device = qemu_opts_create(qemu_find_opts("device"), NULL, 0);
2734 qemu_opt_set(device, "driver", "virtio-9p-pci");
2735 qemu_opt_set(device, "fsdev", "v_synth");
2736 qemu_opt_set(device, "mount_tag", "v_synth");
2739 case QEMU_OPTION_serial:
2740 add_device_config(DEV_SERIAL, optarg);
2742 if (strncmp(optarg, "mon:", 4) == 0) {
2743 default_monitor = 0;
2746 case QEMU_OPTION_watchdog:
2749 "qemu: only one watchdog option may be given\n");
2754 case QEMU_OPTION_watchdog_action:
2755 if (select_watchdog_action(optarg) == -1) {
2756 fprintf(stderr, "Unknown -watchdog-action parameter\n");
2760 case QEMU_OPTION_virtiocon:
2761 add_device_config(DEV_VIRTCON, optarg);
2762 default_virtcon = 0;
2763 if (strncmp(optarg, "mon:", 4) == 0) {
2764 default_monitor = 0;
2767 case QEMU_OPTION_parallel:
2768 add_device_config(DEV_PARALLEL, optarg);
2769 default_parallel = 0;
2770 if (strncmp(optarg, "mon:", 4) == 0) {
2771 default_monitor = 0;
2774 case QEMU_OPTION_debugcon:
2775 add_device_config(DEV_DEBUGCON, optarg);
2777 case QEMU_OPTION_loadvm:
2780 case QEMU_OPTION_full_screen:
2784 case QEMU_OPTION_no_frame:
2787 case QEMU_OPTION_alt_grab:
2790 case QEMU_OPTION_ctrl_grab:
2793 case QEMU_OPTION_no_quit:
2796 case QEMU_OPTION_sdl:
2797 display_type = DT_SDL;
2800 case QEMU_OPTION_no_frame:
2801 case QEMU_OPTION_alt_grab:
2802 case QEMU_OPTION_ctrl_grab:
2803 case QEMU_OPTION_no_quit:
2804 case QEMU_OPTION_sdl:
2805 fprintf(stderr, "SDL support is disabled\n");
2808 case QEMU_OPTION_pidfile:
2811 case QEMU_OPTION_win2k_hack:
2812 win2k_install_hack = 1;
2814 case QEMU_OPTION_rtc_td_hack:
2817 case QEMU_OPTION_acpitable:
2818 do_acpitable_option(optarg);
2820 case QEMU_OPTION_smbios:
2821 do_smbios_option(optarg);
2823 case QEMU_OPTION_enable_kvm:
2824 olist = qemu_find_opts("machine");
2825 qemu_opts_reset(olist);
2826 qemu_opts_parse(olist, "accel=kvm", 0);
2828 case QEMU_OPTION_machine:
2829 olist = qemu_find_opts("machine");
2830 qemu_opts_reset(olist);
2831 opts = qemu_opts_parse(olist, optarg, 1);
2833 fprintf(stderr, "parse error: %s\n", optarg);
2836 optarg = qemu_opt_get(opts, "type");
2838 machine = machine_parse(optarg);
2841 case QEMU_OPTION_usb:
2844 case QEMU_OPTION_usbdevice:
2846 add_device_config(DEV_USB, optarg);
2848 case QEMU_OPTION_device:
2849 if (!qemu_opts_parse(qemu_find_opts("device"), optarg, 1)) {
2853 case QEMU_OPTION_smp:
2856 fprintf(stderr, "Invalid number of CPUs\n");
2859 if (max_cpus < smp_cpus) {
2860 fprintf(stderr, "maxcpus must be equal to or greater than "
2864 if (max_cpus > 255) {
2865 fprintf(stderr, "Unsupported number of maxcpus\n");
2869 case QEMU_OPTION_vnc:
2872 vnc_display = optarg;
2874 fprintf(stderr, "VNC support is disabled\n");
2878 case QEMU_OPTION_no_acpi:
2881 case QEMU_OPTION_no_hpet:
2884 case QEMU_OPTION_balloon:
2885 if (balloon_parse(optarg) < 0) {
2886 fprintf(stderr, "Unknown -balloon argument %s\n", optarg);
2890 case QEMU_OPTION_no_reboot:
2893 case QEMU_OPTION_no_shutdown:
2896 case QEMU_OPTION_show_cursor:
2899 case QEMU_OPTION_uuid:
2900 if(qemu_uuid_parse(optarg, qemu_uuid) < 0) {
2901 fprintf(stderr, "Fail to parse UUID string."
2902 " Wrong format.\n");
2906 case QEMU_OPTION_option_rom:
2907 if (nb_option_roms >= MAX_OPTION_ROMS) {
2908 fprintf(stderr, "Too many option ROMs\n");
2911 opts = qemu_opts_parse(qemu_find_opts("option-rom"), optarg, 1);
2912 option_rom[nb_option_roms].name = qemu_opt_get(opts, "romfile");
2913 option_rom[nb_option_roms].bootindex =
2914 qemu_opt_get_number(opts, "bootindex", -1);
2915 if (!option_rom[nb_option_roms].name) {
2916 fprintf(stderr, "Option ROM file is not specified\n");
2921 case QEMU_OPTION_semihosting:
2922 semihosting_enabled = 1;
2924 case QEMU_OPTION_name:
2925 qemu_name = g_strdup(optarg);
2927 char *p = strchr(qemu_name, ',');
2930 if (strncmp(p, "process=", 8)) {
2931 fprintf(stderr, "Unknown subargument %s to -name\n", p);
2935 os_set_proc_name(p);
2939 case QEMU_OPTION_prom_env:
2940 if (nb_prom_envs >= MAX_PROM_ENVS) {
2941 fprintf(stderr, "Too many prom variables\n");
2944 prom_envs[nb_prom_envs] = optarg;
2947 case QEMU_OPTION_old_param:
2950 case QEMU_OPTION_clock:
2951 configure_alarms(optarg);
2953 case QEMU_OPTION_startdate:
2954 configure_rtc_date_offset(optarg, 1);
2956 case QEMU_OPTION_rtc:
2957 opts = qemu_opts_parse(qemu_find_opts("rtc"), optarg, 0);
2961 configure_rtc(opts);
2963 case QEMU_OPTION_tb_size:
2964 tcg_tb_size = strtol(optarg, NULL, 0);
2965 if (tcg_tb_size < 0) {
2969 case QEMU_OPTION_icount:
2970 icount_option = optarg;
2972 case QEMU_OPTION_incoming:
2975 case QEMU_OPTION_nodefaults:
2977 default_parallel = 0;
2978 default_virtcon = 0;
2979 default_monitor = 0;
2986 case QEMU_OPTION_xen_domid:
2987 if (!(xen_available())) {
2988 printf("Option %s not supported for this target\n", popt->name);
2991 xen_domid = atoi(optarg);
2993 case QEMU_OPTION_xen_create:
2994 if (!(xen_available())) {
2995 printf("Option %s not supported for this target\n", popt->name);
2998 xen_mode = XEN_CREATE;
3000 case QEMU_OPTION_xen_attach:
3001 if (!(xen_available())) {
3002 printf("Option %s not supported for this target\n", popt->name);
3005 xen_mode = XEN_ATTACH;
3007 case QEMU_OPTION_trace:
3009 opts = qemu_opts_parse(qemu_find_opts("trace"), optarg, 0);
3013 trace_events = qemu_opt_get(opts, "events");
3014 trace_file = qemu_opt_get(opts, "file");
3017 case QEMU_OPTION_readconfig:
3019 int ret = qemu_read_config_file(optarg);
3021 fprintf(stderr, "read config %s: %s\n", optarg,
3027 case QEMU_OPTION_spice:
3028 olist = qemu_find_opts("spice");
3030 fprintf(stderr, "spice is not supported by this qemu build.\n");
3033 opts = qemu_opts_parse(olist, optarg, 0);
3035 fprintf(stderr, "parse error: %s\n", optarg);
3039 case QEMU_OPTION_writeconfig:
3042 if (strcmp(optarg, "-") == 0) {
3045 fp = fopen(optarg, "w");
3047 fprintf(stderr, "open %s: %s\n", optarg, strerror(errno));
3051 qemu_config_write(fp);
3056 os_parse_cmd_args(popt->index, optarg);
3062 /* Open the logfile at this point, if necessary. We can't open the logfile
3063 * when encountering either of the logging options (-d or -D) because the
3064 * other one may be encountered later on the command line, changing the
3065 * location or level of logging.
3069 set_cpu_log_filename(log_file);
3071 set_cpu_log(log_mask);
3074 if (!trace_backend_init(trace_events, trace_file)) {
3078 /* If no data_dir is specified then try to find it relative to the
3081 data_dir = os_find_datadir(argv[0]);
3083 /* If all else fails use the install path specified when building. */
3085 data_dir = CONFIG_QEMU_DATADIR;
3088 if (machine == NULL) {
3089 fprintf(stderr, "No machine found.\n");
3094 * Default to max_cpus = smp_cpus, in case the user doesn't
3095 * specify a max_cpus value.
3098 max_cpus = smp_cpus;
3100 machine->max_cpus = machine->max_cpus ?: 1; /* Default to UP */
3101 if (smp_cpus > machine->max_cpus) {
3102 fprintf(stderr, "Number of SMP cpus requested (%d), exceeds max cpus "
3103 "supported by machine `%s' (%d)\n", smp_cpus, machine->name,
3109 * Get the default machine options from the machine if it is not already
3110 * specified either by the configuration file or by the command line.
3112 if (machine->default_machine_opts) {
3113 QemuOptsList *list = qemu_find_opts("machine");
3114 const char *p = NULL;
3116 if (!QTAILQ_EMPTY(&list->head)) {
3117 p = qemu_opt_get(QTAILQ_FIRST(&list->head), "accel");
3120 qemu_opts_reset(list);
3121 opts = qemu_opts_parse(list, machine->default_machine_opts, 0);
3123 fprintf(stderr, "parse error for machine %s: %s\n",
3124 machine->name, machine->default_machine_opts);
3130 qemu_opts_foreach(qemu_find_opts("device"), default_driver_check, NULL, 0);
3131 qemu_opts_foreach(qemu_find_opts("global"), default_driver_check, NULL, 0);
3133 if (machine->no_serial) {
3136 if (machine->no_parallel) {
3137 default_parallel = 0;
3139 if (!machine->use_virtcon) {
3140 default_virtcon = 0;
3142 if (machine->no_vga) {
3145 if (machine->no_floppy) {
3148 if (machine->no_cdrom) {
3151 if (machine->no_sdcard) {
3155 if (display_type == DT_NOGRAPHIC) {
3156 if (default_parallel)
3157 add_device_config(DEV_PARALLEL, "null");
3158 if (default_serial && default_monitor) {
3159 add_device_config(DEV_SERIAL, "mon:stdio");
3160 } else if (default_virtcon && default_monitor) {
3161 add_device_config(DEV_VIRTCON, "mon:stdio");
3164 add_device_config(DEV_SERIAL, "stdio");
3165 if (default_virtcon)
3166 add_device_config(DEV_VIRTCON, "stdio");
3167 if (default_monitor)
3168 monitor_parse("stdio", "readline");
3172 add_device_config(DEV_SERIAL, "vc:80Cx24C");
3173 if (default_parallel)
3174 add_device_config(DEV_PARALLEL, "vc:80Cx24C");
3175 if (default_monitor)
3176 monitor_parse("vc:80Cx24C", "readline");
3177 if (default_virtcon)
3178 add_device_config(DEV_VIRTCON, "vc:80Cx24C");
3181 vga_interface_type = VGA_CIRRUS;
3185 if (qemu_opts_foreach(qemu_find_opts("chardev"), chardev_init_func, NULL, 1) != 0)
3187 #ifdef CONFIG_VIRTFS
3188 if (qemu_opts_foreach(qemu_find_opts("fsdev"), fsdev_init_func, NULL, 1) != 0) {
3195 if (pid_file && qemu_create_pidfile(pid_file) != 0) {
3200 /* init the memory */
3201 if (ram_size == 0) {
3202 ram_size = DEFAULT_RAM_SIZE * 1024 * 1024;
3205 configure_accelerator();
3207 qemu_init_cpu_loop();
3208 if (qemu_init_main_loop()) {
3209 fprintf(stderr, "qemu_init_main_loop failed\n");
3212 linux_boot = (kernel_filename != NULL);
3214 if (!linux_boot && *kernel_cmdline != '\0') {
3215 fprintf(stderr, "-append only allowed with -kernel option\n");
3219 if (!linux_boot && initrd_filename != NULL) {
3220 fprintf(stderr, "-initrd only allowed with -kernel option\n");
3224 os_set_line_buffering();
3226 if (init_timer_alarm() < 0) {
3227 fprintf(stderr, "could not initialize alarm timer\n");
3231 if (icount_option && (kvm_enabled() || xen_enabled())) {
3232 fprintf(stderr, "-icount is not allowed with kvm or xen\n");
3235 configure_icount(icount_option);
3237 if (net_init_clients() < 0) {
3241 /* init the bluetooth world */
3242 if (foreach_device_config(DEV_BT, bt_parse))
3245 if (!xen_enabled()) {
3246 /* On 32-bit hosts, QEMU is limited by virtual address space */
3247 if (ram_size > (2047 << 20) && HOST_LONG_BITS == 32) {
3248 fprintf(stderr, "qemu: at most 2047 MB RAM can be simulated\n");
3253 cpu_exec_init_all();
3255 bdrv_init_with_whitelist();
3259 /* open the virtual block devices */
3261 qemu_opts_foreach(qemu_find_opts("drive"), drive_enable_snapshot, NULL, 0);
3262 if (qemu_opts_foreach(qemu_find_opts("drive"), drive_init_func, &machine->use_scsi, 1) != 0)
3265 default_drive(default_cdrom, snapshot, machine->use_scsi,
3266 IF_DEFAULT, 2, CDROM_OPTS);
3267 default_drive(default_floppy, snapshot, machine->use_scsi,
3268 IF_FLOPPY, 0, FD_OPTS);
3269 default_drive(default_sdcard, snapshot, machine->use_scsi,
3272 register_savevm_live(NULL, "ram", 0, 4, NULL, ram_save_live, NULL,
3275 if (nb_numa_nodes > 0) {
3278 if (nb_numa_nodes > MAX_NODES) {
3279 nb_numa_nodes = MAX_NODES;
3282 /* If no memory size if given for any node, assume the default case
3283 * and distribute the available memory equally across all nodes
3285 for (i = 0; i < nb_numa_nodes; i++) {
3286 if (node_mem[i] != 0)
3289 if (i == nb_numa_nodes) {
3290 uint64_t usedmem = 0;
3292 /* On Linux, the each node's border has to be 8MB aligned,
3293 * the final node gets the rest.
3295 for (i = 0; i < nb_numa_nodes - 1; i++) {
3296 node_mem[i] = (ram_size / nb_numa_nodes) & ~((1 << 23UL) - 1);
3297 usedmem += node_mem[i];
3299 node_mem[i] = ram_size - usedmem;
3302 for (i = 0; i < nb_numa_nodes; i++) {
3303 if (node_cpumask[i] != 0)
3306 /* assigning the VCPUs round-robin is easier to implement, guest OSes
3307 * must cope with this anyway, because there are BIOSes out there in
3308 * real machines which also use this scheme.
3310 if (i == nb_numa_nodes) {
3311 for (i = 0; i < max_cpus; i++) {
3312 node_cpumask[i % nb_numa_nodes] |= 1 << i;
3317 if (qemu_opts_foreach(qemu_find_opts("mon"), mon_init_func, NULL, 1) != 0) {
3321 if (foreach_device_config(DEV_SERIAL, serial_parse) < 0)
3323 if (foreach_device_config(DEV_PARALLEL, parallel_parse) < 0)
3325 if (foreach_device_config(DEV_VIRTCON, virtcon_parse) < 0)
3327 if (foreach_device_config(DEV_DEBUGCON, debugcon_parse) < 0)
3330 module_call_init(MODULE_INIT_DEVICE);
3332 if (qemu_opts_foreach(qemu_find_opts("device"), device_help_func, NULL, 0) != 0)
3336 i = select_watchdog(watchdog);
3338 exit (i == 1 ? 1 : 0);
3341 if (machine->compat_props) {
3342 qdev_prop_register_global_list(machine->compat_props);
3346 qdev_machine_init();
3348 machine->init(ram_size, boot_devices,
3349 kernel_filename, kernel_cmdline, initrd_filename, cpu_model);
3351 cpu_synchronize_all_post_init();
3355 current_machine = machine;
3357 /* init USB devices */
3359 if (foreach_device_config(DEV_USB, usb_parse) < 0)
3363 /* init generic devices */
3364 if (qemu_opts_foreach(qemu_find_opts("device"), device_init_func, NULL, 1) != 0)
3367 net_check_clients();
3369 /* just use the first displaystate for the moment */
3370 ds = get_displaystate();
3374 if (display_type == DT_DEFAULT && !display_remote) {
3375 #if defined(CONFIG_SDL) || defined(CONFIG_COCOA)
3376 display_type = DT_SDL;
3377 #elif defined(CONFIG_VNC)
3378 vnc_display = "localhost:0,to=99";
3381 display_type = DT_NONE;
3386 /* init local displays */
3387 switch (display_type) {
3390 #if defined(CONFIG_CURSES)
3392 curses_display_init(ds, full_screen);
3395 #if defined(CONFIG_SDL)
3397 sdl_display_init(ds, full_screen, no_frame);
3399 #elif defined(CONFIG_COCOA)
3401 cocoa_display_init(ds, full_screen);
3408 /* must be after terminal init, SDL library changes signal handlers */
3409 os_setup_signal_handling();
3412 /* init remote displays */
3414 vnc_display_init(ds);
3415 if (vnc_display_open(ds, vnc_display) < 0)
3418 if (show_vnc_port) {
3419 printf("VNC server running on `%s'\n", vnc_display_local_addr(ds));
3424 if (using_spice && !qxl_enabled) {
3425 qemu_spice_display_init(ds);
3431 dcl = ds->listeners;
3432 while (dcl != NULL) {
3433 if (dcl->dpy_refresh != NULL) {
3434 ds->gui_timer = qemu_new_timer_ms(rt_clock, gui_update, ds);
3435 qemu_mod_timer(ds->gui_timer, qemu_get_clock_ms(rt_clock));
3440 text_consoles_set_display(ds);
3442 if (gdbstub_dev && gdbserver_start(gdbstub_dev) < 0) {
3443 fprintf(stderr, "qemu: could not open gdbserver on device '%s'\n",
3448 qdev_machine_creation_done();
3450 if (rom_load_all() != 0) {
3451 fprintf(stderr, "rom loading failed\n");
3455 /* TODO: once all bus devices are qdevified, this should be done
3456 * when bus is created by qdev.c */
3457 qemu_register_reset(qbus_reset_all_fn, sysbus_get_default());
3458 qemu_run_machine_init_done_notifiers();
3460 qemu_system_reset(VMRESET_SILENT);
3462 if (load_vmstate(loadvm) < 0) {
3468 runstate_set(RUN_STATE_INMIGRATE);
3469 int ret = qemu_start_incoming_migration(incoming);
3471 fprintf(stderr, "Migration failed. Exit code %s(%d), exiting.\n",
3475 } else if (autostart) {