kvm_stat: move groups and events into well defined objects
[qemu/qemu-dev-zwu.git] / vl.c
blobac1bdd34686091394f708f380fe15d6a19e9d9cd
1 /*
2 * QEMU System Emulator
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
22 * THE SOFTWARE.
24 #include <unistd.h>
25 #include <fcntl.h>
26 #include <signal.h>
27 #include <time.h>
28 #include <errno.h>
29 #include <sys/time.h>
30 #include <zlib.h>
32 /* Needed early for CONFIG_BSD etc. */
33 #include "config-host.h"
35 #ifndef _WIN32
36 #include <libgen.h>
37 #include <sys/times.h>
38 #include <sys/wait.h>
39 #include <termios.h>
40 #include <sys/mman.h>
41 #include <sys/ioctl.h>
42 #include <sys/resource.h>
43 #include <sys/socket.h>
44 #include <netinet/in.h>
45 #include <net/if.h>
46 #include <arpa/inet.h>
47 #include <dirent.h>
48 #include <netdb.h>
49 #include <sys/select.h>
50 #ifdef CONFIG_SIMPLE_TRACE
51 #include "trace.h"
52 #endif
54 #ifdef CONFIG_BSD
55 #include <sys/stat.h>
56 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__DragonFly__)
57 #include <libutil.h>
58 #include <sys/sysctl.h>
59 #else
60 #include <util.h>
61 #endif
62 #else
63 #ifdef __linux__
64 #include <pty.h>
65 #include <malloc.h>
67 #include <linux/ppdev.h>
68 #include <linux/parport.h>
69 #endif
70 #ifdef __sun__
71 #include <sys/stat.h>
72 #include <sys/ethernet.h>
73 #include <sys/sockio.h>
74 #include <netinet/arp.h>
75 #include <netinet/in_systm.h>
76 #include <netinet/ip.h>
77 #include <netinet/ip_icmp.h> // must come after ip.h
78 #include <netinet/udp.h>
79 #include <netinet/tcp.h>
80 #include <net/if.h>
81 #include <syslog.h>
82 #include <stropts.h>
83 #endif
84 #endif
85 #endif
87 #if defined(__OpenBSD__)
88 #include <util.h>
89 #endif
91 #if defined(CONFIG_VDE)
92 #include <libvdeplug.h>
93 #endif
95 #ifdef _WIN32
96 #include <windows.h>
97 #endif
99 #ifdef CONFIG_SDL
100 #if defined(__APPLE__) || defined(main)
101 #include <SDL.h>
102 int qemu_main(int argc, char **argv, char **envp);
103 int main(int argc, char **argv)
105 return qemu_main(argc, argv, NULL);
107 #undef main
108 #define main qemu_main
109 #endif
110 #endif /* CONFIG_SDL */
112 #ifdef CONFIG_COCOA
113 #undef main
114 #define main qemu_main
115 #endif /* CONFIG_COCOA */
117 #include "hw/hw.h"
118 #include "hw/boards.h"
119 #include "hw/usb.h"
120 #include "hw/pcmcia.h"
121 #include "hw/pc.h"
122 #include "hw/isa.h"
123 #include "hw/baum.h"
124 #include "hw/bt.h"
125 #include "hw/watchdog.h"
126 #include "hw/smbios.h"
127 #include "hw/xen.h"
128 #include "hw/qdev.h"
129 #include "hw/loader.h"
130 #include "bt-host.h"
131 #include "net.h"
132 #include "net/slirp.h"
133 #include "monitor.h"
134 #include "console.h"
135 #include "sysemu.h"
136 #include "gdbstub.h"
137 #include "qemu-timer.h"
138 #include "qemu-char.h"
139 #include "cache-utils.h"
140 #include "block.h"
141 #include "blockdev.h"
142 #include "block-migration.h"
143 #include "dma.h"
144 #include "audio/audio.h"
145 #include "migration.h"
146 #include "kvm.h"
147 #include "qemu-option.h"
148 #include "qemu-config.h"
149 #include "qemu-objects.h"
150 #include "qemu-options.h"
151 #include "hw/device-assignment.h"
152 #ifdef CONFIG_VIRTFS
153 #include "fsdev/qemu-fsdev.h"
154 #endif
156 #include "disas.h"
158 #include "qemu_socket.h"
160 #include "slirp/libslirp.h"
162 #include "trace.h"
163 #include "qemu-queue.h"
164 #include "cpus.h"
165 #include "arch_init.h"
167 #include "ui/qemu-spice.h"
169 //#define DEBUG_NET
170 //#define DEBUG_SLIRP
172 #define DEFAULT_RAM_SIZE 128
174 #define MAX_VIRTIO_CONSOLES 1
176 static const char *data_dir;
177 const char *bios_name = NULL;
178 enum vga_retrace_method vga_retrace_method = VGA_RETRACE_DUMB;
179 DisplayType display_type = DT_DEFAULT;
180 int display_remote = 0;
181 const char* keyboard_layout = NULL;
182 ram_addr_t ram_size;
183 const char *mem_path = NULL;
184 #ifdef MAP_POPULATE
185 int mem_prealloc = 0; /* force preallocation of physical target memory */
186 #endif
187 int nb_nics;
188 NICInfo nd_table[MAX_NICS];
189 int vm_running;
190 int autostart;
191 int incoming_expected; /* Started with -incoming and waiting for incoming */
192 static int rtc_utc = 1;
193 static int rtc_date_offset = -1; /* -1 means no change */
194 QEMUClock *rtc_clock;
195 int vga_interface_type = VGA_NONE;
196 static int full_screen = 0;
197 #ifdef CONFIG_SDL
198 static int no_frame = 0;
199 #endif
200 int no_quit = 0;
201 CharDriverState *serial_hds[MAX_SERIAL_PORTS];
202 CharDriverState *parallel_hds[MAX_PARALLEL_PORTS];
203 CharDriverState *virtcon_hds[MAX_VIRTIO_CONSOLES];
204 int win2k_install_hack = 0;
205 int rtc_td_hack = 0;
206 int usb_enabled = 0;
207 int singlestep = 0;
208 int smp_cpus = 1;
209 int max_cpus = 0;
210 int smp_cores = 1;
211 int smp_threads = 1;
212 const char *vnc_display;
213 int acpi_enabled = 1;
214 int no_hpet = 0;
215 int fd_bootchk = 1;
216 int no_reboot = 0;
217 int no_shutdown = 0;
218 int cursor_hide = 1;
219 int graphic_rotate = 0;
220 uint8_t irq0override = 1;
221 const char *watchdog;
222 QEMUOptionRom option_rom[MAX_OPTION_ROMS];
223 int nb_option_roms;
224 int semihosting_enabled = 0;
225 int time_drift_fix = 0;
226 unsigned int kvm_shadow_memory = 0;
227 int old_param = 0;
228 const char *qemu_name;
229 int alt_grab = 0;
230 int ctrl_grab = 0;
231 unsigned int nb_prom_envs = 0;
232 const char *prom_envs[MAX_PROM_ENVS];
233 const char *nvram = NULL;
234 int boot_menu;
236 typedef struct FWBootEntry FWBootEntry;
238 struct FWBootEntry {
239 QTAILQ_ENTRY(FWBootEntry) link;
240 int32_t bootindex;
241 DeviceState *dev;
242 char *suffix;
245 QTAILQ_HEAD(, FWBootEntry) fw_boot_order = QTAILQ_HEAD_INITIALIZER(fw_boot_order);
247 int nb_numa_nodes;
248 uint64_t node_mem[MAX_NODES];
249 uint64_t node_cpumask[MAX_NODES];
251 static QEMUTimer *nographic_timer;
253 uint8_t qemu_uuid[16];
255 static QEMUBootSetHandler *boot_set_handler;
256 static void *boot_set_opaque;
258 static NotifierList exit_notifiers =
259 NOTIFIER_LIST_INITIALIZER(exit_notifiers);
261 static NotifierList machine_init_done_notifiers =
262 NOTIFIER_LIST_INITIALIZER(machine_init_done_notifiers);
264 int kvm_allowed = -1;
265 uint32_t xen_domid;
266 enum xen_mode xen_mode = XEN_EMULATE;
268 static int default_serial = 1;
269 static int default_parallel = 1;
270 static int default_virtcon = 1;
271 static int default_monitor = 1;
272 static int default_vga = 1;
273 static int default_floppy = 1;
274 static int default_cdrom = 1;
275 static int default_sdcard = 1;
277 static struct {
278 const char *driver;
279 int *flag;
280 } default_list[] = {
281 { .driver = "isa-serial", .flag = &default_serial },
282 { .driver = "isa-parallel", .flag = &default_parallel },
283 { .driver = "isa-fdc", .flag = &default_floppy },
284 { .driver = "ide-drive", .flag = &default_cdrom },
285 { .driver = "virtio-serial-pci", .flag = &default_virtcon },
286 { .driver = "virtio-serial-s390", .flag = &default_virtcon },
287 { .driver = "virtio-serial", .flag = &default_virtcon },
288 { .driver = "VGA", .flag = &default_vga },
289 { .driver = "cirrus-vga", .flag = &default_vga },
290 { .driver = "vmware-svga", .flag = &default_vga },
293 static int default_driver_check(QemuOpts *opts, void *opaque)
295 const char *driver = qemu_opt_get(opts, "driver");
296 int i;
298 if (!driver)
299 return 0;
300 for (i = 0; i < ARRAY_SIZE(default_list); i++) {
301 if (strcmp(default_list[i].driver, driver) != 0)
302 continue;
303 *(default_list[i].flag) = 0;
305 return 0;
308 /***********************************************************/
309 /* real time host monotonic timer */
311 /***********************************************************/
312 /* host time/date access */
313 void qemu_get_timedate(struct tm *tm, int offset)
315 time_t ti;
316 struct tm *ret;
318 time(&ti);
319 ti += offset;
320 if (rtc_date_offset == -1) {
321 if (rtc_utc)
322 ret = gmtime(&ti);
323 else
324 ret = localtime(&ti);
325 } else {
326 ti -= rtc_date_offset;
327 ret = gmtime(&ti);
330 memcpy(tm, ret, sizeof(struct tm));
333 int qemu_timedate_diff(struct tm *tm)
335 time_t seconds;
337 if (rtc_date_offset == -1)
338 if (rtc_utc)
339 seconds = mktimegm(tm);
340 else
341 seconds = mktime(tm);
342 else
343 seconds = mktimegm(tm) + rtc_date_offset;
345 return seconds - time(NULL);
348 void rtc_change_mon_event(struct tm *tm)
350 QObject *data;
352 data = qobject_from_jsonf("{ 'offset': %d }", qemu_timedate_diff(tm));
353 monitor_protocol_event(QEVENT_RTC_CHANGE, data);
354 qobject_decref(data);
357 static void configure_rtc_date_offset(const char *startdate, int legacy)
359 time_t rtc_start_date;
360 struct tm tm;
362 if (!strcmp(startdate, "now") && legacy) {
363 rtc_date_offset = -1;
364 } else {
365 if (sscanf(startdate, "%d-%d-%dT%d:%d:%d",
366 &tm.tm_year,
367 &tm.tm_mon,
368 &tm.tm_mday,
369 &tm.tm_hour,
370 &tm.tm_min,
371 &tm.tm_sec) == 6) {
372 /* OK */
373 } else if (sscanf(startdate, "%d-%d-%d",
374 &tm.tm_year,
375 &tm.tm_mon,
376 &tm.tm_mday) == 3) {
377 tm.tm_hour = 0;
378 tm.tm_min = 0;
379 tm.tm_sec = 0;
380 } else {
381 goto date_fail;
383 tm.tm_year -= 1900;
384 tm.tm_mon--;
385 rtc_start_date = mktimegm(&tm);
386 if (rtc_start_date == -1) {
387 date_fail:
388 fprintf(stderr, "Invalid date format. Valid formats are:\n"
389 "'2006-06-17T16:01:21' or '2006-06-17'\n");
390 exit(1);
392 rtc_date_offset = time(NULL) - rtc_start_date;
396 static void configure_rtc(QemuOpts *opts)
398 const char *value;
400 value = qemu_opt_get(opts, "base");
401 if (value) {
402 if (!strcmp(value, "utc")) {
403 rtc_utc = 1;
404 } else if (!strcmp(value, "localtime")) {
405 rtc_utc = 0;
406 } else {
407 configure_rtc_date_offset(value, 0);
410 value = qemu_opt_get(opts, "clock");
411 if (value) {
412 if (!strcmp(value, "host")) {
413 rtc_clock = host_clock;
414 } else if (!strcmp(value, "vm")) {
415 rtc_clock = vm_clock;
416 } else {
417 fprintf(stderr, "qemu: invalid option value '%s'\n", value);
418 exit(1);
421 value = qemu_opt_get(opts, "driftfix");
422 if (value) {
423 if (!strcmp(value, "slew")) {
424 rtc_td_hack = 1;
425 } else if (!strcmp(value, "none")) {
426 rtc_td_hack = 0;
427 } else {
428 fprintf(stderr, "qemu: invalid option value '%s'\n", value);
429 exit(1);
434 /***********************************************************/
435 /* Bluetooth support */
436 static int nb_hcis;
437 static int cur_hci;
438 static struct HCIInfo *hci_table[MAX_NICS];
440 static struct bt_vlan_s {
441 struct bt_scatternet_s net;
442 int id;
443 struct bt_vlan_s *next;
444 } *first_bt_vlan;
446 /* find or alloc a new bluetooth "VLAN" */
447 static struct bt_scatternet_s *qemu_find_bt_vlan(int id)
449 struct bt_vlan_s **pvlan, *vlan;
450 for (vlan = first_bt_vlan; vlan != NULL; vlan = vlan->next) {
451 if (vlan->id == id)
452 return &vlan->net;
454 vlan = qemu_mallocz(sizeof(struct bt_vlan_s));
455 vlan->id = id;
456 pvlan = &first_bt_vlan;
457 while (*pvlan != NULL)
458 pvlan = &(*pvlan)->next;
459 *pvlan = vlan;
460 return &vlan->net;
463 static void null_hci_send(struct HCIInfo *hci, const uint8_t *data, int len)
467 static int null_hci_addr_set(struct HCIInfo *hci, const uint8_t *bd_addr)
469 return -ENOTSUP;
472 static struct HCIInfo null_hci = {
473 .cmd_send = null_hci_send,
474 .sco_send = null_hci_send,
475 .acl_send = null_hci_send,
476 .bdaddr_set = null_hci_addr_set,
479 struct HCIInfo *qemu_next_hci(void)
481 if (cur_hci == nb_hcis)
482 return &null_hci;
484 return hci_table[cur_hci++];
487 static struct HCIInfo *hci_init(const char *str)
489 char *endp;
490 struct bt_scatternet_s *vlan = 0;
492 if (!strcmp(str, "null"))
493 /* null */
494 return &null_hci;
495 else if (!strncmp(str, "host", 4) && (str[4] == '\0' || str[4] == ':'))
496 /* host[:hciN] */
497 return bt_host_hci(str[4] ? str + 5 : "hci0");
498 else if (!strncmp(str, "hci", 3)) {
499 /* hci[,vlan=n] */
500 if (str[3]) {
501 if (!strncmp(str + 3, ",vlan=", 6)) {
502 vlan = qemu_find_bt_vlan(strtol(str + 9, &endp, 0));
503 if (*endp)
504 vlan = 0;
506 } else
507 vlan = qemu_find_bt_vlan(0);
508 if (vlan)
509 return bt_new_hci(vlan);
512 fprintf(stderr, "qemu: Unknown bluetooth HCI `%s'.\n", str);
514 return 0;
517 static int bt_hci_parse(const char *str)
519 struct HCIInfo *hci;
520 bdaddr_t bdaddr;
522 if (nb_hcis >= MAX_NICS) {
523 fprintf(stderr, "qemu: Too many bluetooth HCIs (max %i).\n", MAX_NICS);
524 return -1;
527 hci = hci_init(str);
528 if (!hci)
529 return -1;
531 bdaddr.b[0] = 0x52;
532 bdaddr.b[1] = 0x54;
533 bdaddr.b[2] = 0x00;
534 bdaddr.b[3] = 0x12;
535 bdaddr.b[4] = 0x34;
536 bdaddr.b[5] = 0x56 + nb_hcis;
537 hci->bdaddr_set(hci, bdaddr.b);
539 hci_table[nb_hcis++] = hci;
541 return 0;
544 static void bt_vhci_add(int vlan_id)
546 struct bt_scatternet_s *vlan = qemu_find_bt_vlan(vlan_id);
548 if (!vlan->slave)
549 fprintf(stderr, "qemu: warning: adding a VHCI to "
550 "an empty scatternet %i\n", vlan_id);
552 bt_vhci_init(bt_new_hci(vlan));
555 static struct bt_device_s *bt_device_add(const char *opt)
557 struct bt_scatternet_s *vlan;
558 int vlan_id = 0;
559 char *endp = strstr(opt, ",vlan=");
560 int len = (endp ? endp - opt : strlen(opt)) + 1;
561 char devname[10];
563 pstrcpy(devname, MIN(sizeof(devname), len), opt);
565 if (endp) {
566 vlan_id = strtol(endp + 6, &endp, 0);
567 if (*endp) {
568 fprintf(stderr, "qemu: unrecognised bluetooth vlan Id\n");
569 return 0;
573 vlan = qemu_find_bt_vlan(vlan_id);
575 if (!vlan->slave)
576 fprintf(stderr, "qemu: warning: adding a slave device to "
577 "an empty scatternet %i\n", vlan_id);
579 if (!strcmp(devname, "keyboard"))
580 return bt_keyboard_init(vlan);
582 fprintf(stderr, "qemu: unsupported bluetooth device `%s'\n", devname);
583 return 0;
586 static int bt_parse(const char *opt)
588 const char *endp, *p;
589 int vlan;
591 if (strstart(opt, "hci", &endp)) {
592 if (!*endp || *endp == ',') {
593 if (*endp)
594 if (!strstart(endp, ",vlan=", 0))
595 opt = endp + 1;
597 return bt_hci_parse(opt);
599 } else if (strstart(opt, "vhci", &endp)) {
600 if (!*endp || *endp == ',') {
601 if (*endp) {
602 if (strstart(endp, ",vlan=", &p)) {
603 vlan = strtol(p, (char **) &endp, 0);
604 if (*endp) {
605 fprintf(stderr, "qemu: bad scatternet '%s'\n", p);
606 return 1;
608 } else {
609 fprintf(stderr, "qemu: bad parameter '%s'\n", endp + 1);
610 return 1;
612 } else
613 vlan = 0;
615 bt_vhci_add(vlan);
616 return 0;
618 } else if (strstart(opt, "device:", &endp))
619 return !bt_device_add(endp);
621 fprintf(stderr, "qemu: bad bluetooth parameter '%s'\n", opt);
622 return 1;
625 /***********************************************************/
626 /* QEMU Block devices */
628 #define HD_OPTS "media=disk"
629 #define CDROM_OPTS "media=cdrom"
630 #define FD_OPTS ""
631 #define PFLASH_OPTS ""
632 #define MTD_OPTS ""
633 #define SD_OPTS ""
635 static int drive_init_func(QemuOpts *opts, void *opaque)
637 int *use_scsi = opaque;
639 return drive_init(opts, *use_scsi) == NULL;
642 static int drive_enable_snapshot(QemuOpts *opts, void *opaque)
644 if (NULL == qemu_opt_get(opts, "snapshot")) {
645 qemu_opt_set(opts, "snapshot", "on");
647 return 0;
650 static void default_drive(int enable, int snapshot, int use_scsi,
651 BlockInterfaceType type, int index,
652 const char *optstr)
654 QemuOpts *opts;
656 if (type == IF_DEFAULT) {
657 type = use_scsi ? IF_SCSI : IF_IDE;
660 if (!enable || drive_get_by_index(type, index)) {
661 return;
664 opts = drive_add(type, index, NULL, optstr);
665 if (snapshot) {
666 drive_enable_snapshot(opts, NULL);
668 if (!drive_init(opts, use_scsi)) {
669 exit(1);
673 void qemu_register_boot_set(QEMUBootSetHandler *func, void *opaque)
675 boot_set_handler = func;
676 boot_set_opaque = opaque;
679 int qemu_boot_set(const char *boot_devices)
681 if (!boot_set_handler) {
682 return -EINVAL;
684 return boot_set_handler(boot_set_opaque, boot_devices);
687 static void validate_bootdevices(char *devices)
689 /* We just do some generic consistency checks */
690 const char *p;
691 int bitmap = 0;
693 for (p = devices; *p != '\0'; p++) {
694 /* Allowed boot devices are:
695 * a-b: floppy disk drives
696 * c-f: IDE disk drives
697 * g-m: machine implementation dependant drives
698 * n-p: network devices
699 * It's up to each machine implementation to check if the given boot
700 * devices match the actual hardware implementation and firmware
701 * features.
703 if (*p < 'a' || *p > 'p') {
704 fprintf(stderr, "Invalid boot device '%c'\n", *p);
705 exit(1);
707 if (bitmap & (1 << (*p - 'a'))) {
708 fprintf(stderr, "Boot device '%c' was given twice\n", *p);
709 exit(1);
711 bitmap |= 1 << (*p - 'a');
715 static void restore_boot_devices(void *opaque)
717 char *standard_boot_devices = opaque;
718 static int first = 1;
720 /* Restore boot order and remove ourselves after the first boot */
721 if (first) {
722 first = 0;
723 return;
726 qemu_boot_set(standard_boot_devices);
728 qemu_unregister_reset(restore_boot_devices, standard_boot_devices);
729 qemu_free(standard_boot_devices);
732 void add_boot_device_path(int32_t bootindex, DeviceState *dev,
733 const char *suffix)
735 FWBootEntry *node, *i;
737 if (bootindex < 0) {
738 return;
741 assert(dev != NULL || suffix != NULL);
743 node = qemu_mallocz(sizeof(FWBootEntry));
744 node->bootindex = bootindex;
745 node->suffix = suffix ? qemu_strdup(suffix) : NULL;
746 node->dev = dev;
748 QTAILQ_FOREACH(i, &fw_boot_order, link) {
749 if (i->bootindex == bootindex) {
750 fprintf(stderr, "Two devices with same boot index %d\n", bootindex);
751 exit(1);
752 } else if (i->bootindex < bootindex) {
753 continue;
755 QTAILQ_INSERT_BEFORE(i, node, link);
756 return;
758 QTAILQ_INSERT_TAIL(&fw_boot_order, node, link);
762 * This function returns null terminated string that consist of new line
763 * separated device pathes.
765 * memory pointed by "size" is assigned total length of the array in bytes
768 char *get_boot_devices_list(uint32_t *size)
770 FWBootEntry *i;
771 uint32_t total = 0;
772 char *list = NULL;
774 QTAILQ_FOREACH(i, &fw_boot_order, link) {
775 char *devpath = NULL, *bootpath;
776 int len;
778 if (i->dev) {
779 devpath = qdev_get_fw_dev_path(i->dev);
780 assert(devpath);
783 if (i->suffix && devpath) {
784 size_t bootpathlen = strlen(devpath) + strlen(i->suffix) + 1;
786 bootpath = qemu_malloc(bootpathlen);
787 snprintf(bootpath, bootpathlen, "%s%s", devpath, i->suffix);
788 qemu_free(devpath);
789 } else if (devpath) {
790 bootpath = devpath;
791 } else {
792 bootpath = qemu_strdup(i->suffix);
793 assert(bootpath);
796 if (total) {
797 list[total-1] = '\n';
799 len = strlen(bootpath) + 1;
800 list = qemu_realloc(list, total + len);
801 memcpy(&list[total], bootpath, len);
802 total += len;
803 qemu_free(bootpath);
806 *size = total;
808 return list;
811 static void numa_add(const char *optarg)
813 char option[128];
814 char *endptr;
815 unsigned long long value, endvalue;
816 int nodenr;
818 optarg = get_opt_name(option, 128, optarg, ',') + 1;
819 if (!strcmp(option, "node")) {
820 if (get_param_value(option, 128, "nodeid", optarg) == 0) {
821 nodenr = nb_numa_nodes;
822 } else {
823 nodenr = strtoull(option, NULL, 10);
826 if (get_param_value(option, 128, "mem", optarg) == 0) {
827 node_mem[nodenr] = 0;
828 } else {
829 int64_t sval;
830 sval = strtosz(option, NULL);
831 if (sval < 0) {
832 fprintf(stderr, "qemu: invalid numa mem size: %s\n", optarg);
833 exit(1);
835 node_mem[nodenr] = sval;
837 if (get_param_value(option, 128, "cpus", optarg) == 0) {
838 node_cpumask[nodenr] = 0;
839 } else {
840 value = strtoull(option, &endptr, 10);
841 if (value >= 64) {
842 value = 63;
843 fprintf(stderr, "only 64 CPUs in NUMA mode supported.\n");
844 } else {
845 if (*endptr == '-') {
846 endvalue = strtoull(endptr+1, &endptr, 10);
847 if (endvalue >= 63) {
848 endvalue = 62;
849 fprintf(stderr,
850 "only 63 CPUs in NUMA mode supported.\n");
852 value = (2ULL << endvalue) - (1ULL << value);
853 } else {
854 value = 1ULL << value;
857 node_cpumask[nodenr] = value;
859 nb_numa_nodes++;
861 return;
864 static void smp_parse(const char *optarg)
866 int smp, sockets = 0, threads = 0, cores = 0;
867 char *endptr;
868 char option[128];
870 smp = strtoul(optarg, &endptr, 10);
871 if (endptr != optarg) {
872 if (*endptr == ',') {
873 endptr++;
876 if (get_param_value(option, 128, "sockets", endptr) != 0)
877 sockets = strtoull(option, NULL, 10);
878 if (get_param_value(option, 128, "cores", endptr) != 0)
879 cores = strtoull(option, NULL, 10);
880 if (get_param_value(option, 128, "threads", endptr) != 0)
881 threads = strtoull(option, NULL, 10);
882 if (get_param_value(option, 128, "maxcpus", endptr) != 0)
883 max_cpus = strtoull(option, NULL, 10);
885 /* compute missing values, prefer sockets over cores over threads */
886 if (smp == 0 || sockets == 0) {
887 sockets = sockets > 0 ? sockets : 1;
888 cores = cores > 0 ? cores : 1;
889 threads = threads > 0 ? threads : 1;
890 if (smp == 0) {
891 smp = cores * threads * sockets;
893 } else {
894 if (cores == 0) {
895 threads = threads > 0 ? threads : 1;
896 cores = smp / (sockets * threads);
897 } else {
898 threads = smp / (cores * sockets);
901 smp_cpus = smp;
902 smp_cores = cores > 0 ? cores : 1;
903 smp_threads = threads > 0 ? threads : 1;
904 if (max_cpus == 0)
905 max_cpus = smp_cpus;
908 /***********************************************************/
909 /* USB devices */
911 static int usb_device_add(const char *devname)
913 const char *p;
914 USBDevice *dev = NULL;
916 if (!usb_enabled)
917 return -1;
919 /* drivers with .usbdevice_name entry in USBDeviceInfo */
920 dev = usbdevice_create(devname);
921 if (dev)
922 goto done;
924 /* the other ones */
925 if (strstart(devname, "host:", &p)) {
926 dev = usb_host_device_open(p);
927 } else if (!strcmp(devname, "bt") || strstart(devname, "bt:", &p)) {
928 dev = usb_bt_init(devname[2] ? hci_init(p) :
929 bt_new_hci(qemu_find_bt_vlan(0)));
930 } else {
931 return -1;
933 if (!dev)
934 return -1;
936 done:
937 return 0;
940 static int usb_device_del(const char *devname)
942 int bus_num, addr;
943 const char *p;
945 if (strstart(devname, "host:", &p))
946 return usb_host_device_close(p);
948 if (!usb_enabled)
949 return -1;
951 p = strchr(devname, '.');
952 if (!p)
953 return -1;
954 bus_num = strtoul(devname, NULL, 0);
955 addr = strtoul(p + 1, NULL, 0);
957 return usb_device_delete_addr(bus_num, addr);
960 static int usb_parse(const char *cmdline)
962 int r;
963 r = usb_device_add(cmdline);
964 if (r < 0) {
965 fprintf(stderr, "qemu: could not add USB device '%s'\n", cmdline);
967 return r;
970 void do_usb_add(Monitor *mon, const QDict *qdict)
972 const char *devname = qdict_get_str(qdict, "devname");
973 if (usb_device_add(devname) < 0) {
974 error_report("could not add USB device '%s'", devname);
978 void do_usb_del(Monitor *mon, const QDict *qdict)
980 const char *devname = qdict_get_str(qdict, "devname");
981 if (usb_device_del(devname) < 0) {
982 error_report("could not delete USB device '%s'", devname);
986 /***********************************************************/
987 /* PCMCIA/Cardbus */
989 static struct pcmcia_socket_entry_s {
990 PCMCIASocket *socket;
991 struct pcmcia_socket_entry_s *next;
992 } *pcmcia_sockets = 0;
994 void pcmcia_socket_register(PCMCIASocket *socket)
996 struct pcmcia_socket_entry_s *entry;
998 entry = qemu_malloc(sizeof(struct pcmcia_socket_entry_s));
999 entry->socket = socket;
1000 entry->next = pcmcia_sockets;
1001 pcmcia_sockets = entry;
1004 void pcmcia_socket_unregister(PCMCIASocket *socket)
1006 struct pcmcia_socket_entry_s *entry, **ptr;
1008 ptr = &pcmcia_sockets;
1009 for (entry = *ptr; entry; ptr = &entry->next, entry = *ptr)
1010 if (entry->socket == socket) {
1011 *ptr = entry->next;
1012 qemu_free(entry);
1016 void pcmcia_info(Monitor *mon)
1018 struct pcmcia_socket_entry_s *iter;
1020 if (!pcmcia_sockets)
1021 monitor_printf(mon, "No PCMCIA sockets\n");
1023 for (iter = pcmcia_sockets; iter; iter = iter->next)
1024 monitor_printf(mon, "%s: %s\n", iter->socket->slot_string,
1025 iter->socket->attached ? iter->socket->card_string :
1026 "Empty");
1029 /***********************************************************/
1030 /* I/O handling */
1032 typedef struct IOHandlerRecord {
1033 int fd;
1034 IOCanReadHandler *fd_read_poll;
1035 IOHandler *fd_read;
1036 IOHandler *fd_write;
1037 int deleted;
1038 void *opaque;
1039 /* temporary data */
1040 struct pollfd *ufd;
1041 QLIST_ENTRY(IOHandlerRecord) next;
1042 } IOHandlerRecord;
1044 static QLIST_HEAD(, IOHandlerRecord) io_handlers =
1045 QLIST_HEAD_INITIALIZER(io_handlers);
1048 /* XXX: fd_read_poll should be suppressed, but an API change is
1049 necessary in the character devices to suppress fd_can_read(). */
1050 int qemu_set_fd_handler2(int fd,
1051 IOCanReadHandler *fd_read_poll,
1052 IOHandler *fd_read,
1053 IOHandler *fd_write,
1054 void *opaque)
1056 IOHandlerRecord *ioh;
1058 if (!fd_read && !fd_write) {
1059 QLIST_FOREACH(ioh, &io_handlers, next) {
1060 if (ioh->fd == fd) {
1061 ioh->deleted = 1;
1062 break;
1065 } else {
1066 QLIST_FOREACH(ioh, &io_handlers, next) {
1067 if (ioh->fd == fd)
1068 goto found;
1070 ioh = qemu_mallocz(sizeof(IOHandlerRecord));
1071 QLIST_INSERT_HEAD(&io_handlers, ioh, next);
1072 found:
1073 ioh->fd = fd;
1074 ioh->fd_read_poll = fd_read_poll;
1075 ioh->fd_read = fd_read;
1076 ioh->fd_write = fd_write;
1077 ioh->opaque = opaque;
1078 ioh->deleted = 0;
1080 qemu_notify_event();
1081 return 0;
1084 int qemu_set_fd_handler(int fd,
1085 IOHandler *fd_read,
1086 IOHandler *fd_write,
1087 void *opaque)
1089 return qemu_set_fd_handler2(fd, NULL, fd_read, fd_write, opaque);
1092 /***********************************************************/
1093 /* machine registration */
1095 static QEMUMachine *first_machine = NULL;
1096 QEMUMachine *current_machine = NULL;
1098 int qemu_register_machine(QEMUMachine *m)
1100 QEMUMachine **pm;
1101 pm = &first_machine;
1102 while (*pm != NULL)
1103 pm = &(*pm)->next;
1104 m->next = NULL;
1105 *pm = m;
1106 return 0;
1109 static QEMUMachine *find_machine(const char *name)
1111 QEMUMachine *m;
1113 for(m = first_machine; m != NULL; m = m->next) {
1114 if (!strcmp(m->name, name))
1115 return m;
1116 if (m->alias && !strcmp(m->alias, name))
1117 return m;
1119 return NULL;
1122 static QEMUMachine *find_default_machine(void)
1124 QEMUMachine *m;
1126 for(m = first_machine; m != NULL; m = m->next) {
1127 if (m->is_default) {
1128 return m;
1131 return NULL;
1134 /***********************************************************/
1135 /* main execution loop */
1137 static void gui_update(void *opaque)
1139 uint64_t interval = GUI_REFRESH_INTERVAL;
1140 DisplayState *ds = opaque;
1141 DisplayChangeListener *dcl = ds->listeners;
1143 qemu_flush_coalesced_mmio_buffer();
1144 dpy_refresh(ds);
1146 while (dcl != NULL) {
1147 if (dcl->gui_timer_interval &&
1148 dcl->gui_timer_interval < interval)
1149 interval = dcl->gui_timer_interval;
1150 dcl = dcl->next;
1152 qemu_mod_timer(ds->gui_timer, interval + qemu_get_clock(rt_clock));
1155 static void nographic_update(void *opaque)
1157 uint64_t interval = GUI_REFRESH_INTERVAL;
1159 qemu_flush_coalesced_mmio_buffer();
1160 qemu_mod_timer(nographic_timer, interval + qemu_get_clock(rt_clock));
1163 struct vm_change_state_entry {
1164 VMChangeStateHandler *cb;
1165 void *opaque;
1166 QLIST_ENTRY (vm_change_state_entry) entries;
1169 static QLIST_HEAD(vm_change_state_head, vm_change_state_entry) vm_change_state_head;
1171 VMChangeStateEntry *qemu_add_vm_change_state_handler(VMChangeStateHandler *cb,
1172 void *opaque)
1174 VMChangeStateEntry *e;
1176 e = qemu_mallocz(sizeof (*e));
1178 e->cb = cb;
1179 e->opaque = opaque;
1180 QLIST_INSERT_HEAD(&vm_change_state_head, e, entries);
1181 return e;
1184 void qemu_del_vm_change_state_handler(VMChangeStateEntry *e)
1186 QLIST_REMOVE (e, entries);
1187 qemu_free (e);
1190 void vm_state_notify(int running, int reason)
1192 VMChangeStateEntry *e;
1194 trace_vm_state_notify(running, reason);
1196 for (e = vm_change_state_head.lh_first; e; e = e->entries.le_next) {
1197 e->cb(e->opaque, running, reason);
1201 void vm_start(void)
1203 if (!vm_running) {
1204 cpu_enable_ticks();
1205 vm_running = 1;
1206 vm_state_notify(1, 0);
1207 resume_all_vcpus();
1208 monitor_protocol_event(QEVENT_RESUME, NULL);
1212 /* reset/shutdown handler */
1214 typedef struct QEMUResetEntry {
1215 QTAILQ_ENTRY(QEMUResetEntry) entry;
1216 QEMUResetHandler *func;
1217 void *opaque;
1218 } QEMUResetEntry;
1220 static QTAILQ_HEAD(reset_handlers, QEMUResetEntry) reset_handlers =
1221 QTAILQ_HEAD_INITIALIZER(reset_handlers);
1222 static int reset_requested;
1223 static int shutdown_requested;
1224 static int powerdown_requested;
1225 static int debug_requested;
1226 static int vmstop_requested;
1228 int qemu_no_shutdown(void)
1230 int r = no_shutdown;
1231 no_shutdown = 0;
1232 return r;
1235 int qemu_shutdown_requested(void)
1237 int r = shutdown_requested;
1238 shutdown_requested = 0;
1239 return r;
1242 int qemu_reset_requested(void)
1244 int r = reset_requested;
1245 reset_requested = 0;
1246 return r;
1249 int qemu_powerdown_requested(void)
1251 int r = powerdown_requested;
1252 powerdown_requested = 0;
1253 return r;
1256 static int qemu_debug_requested(void)
1258 int r = debug_requested;
1259 debug_requested = 0;
1260 return r;
1263 static int qemu_vmstop_requested(void)
1265 int r = vmstop_requested;
1266 vmstop_requested = 0;
1267 return r;
1270 void qemu_register_reset(QEMUResetHandler *func, void *opaque)
1272 QEMUResetEntry *re = qemu_mallocz(sizeof(QEMUResetEntry));
1274 re->func = func;
1275 re->opaque = opaque;
1276 QTAILQ_INSERT_TAIL(&reset_handlers, re, entry);
1279 void qemu_unregister_reset(QEMUResetHandler *func, void *opaque)
1281 QEMUResetEntry *re;
1283 QTAILQ_FOREACH(re, &reset_handlers, entry) {
1284 if (re->func == func && re->opaque == opaque) {
1285 QTAILQ_REMOVE(&reset_handlers, re, entry);
1286 qemu_free(re);
1287 return;
1292 void qemu_system_reset(void)
1294 QEMUResetEntry *re, *nre;
1296 /* reset all devices */
1297 QTAILQ_FOREACH_SAFE(re, &reset_handlers, entry, nre) {
1298 re->func(re->opaque);
1300 monitor_protocol_event(QEVENT_RESET, NULL);
1301 cpu_synchronize_all_post_reset();
1304 void qemu_system_reset_request(void)
1306 if (no_reboot) {
1307 shutdown_requested = 1;
1308 } else {
1309 reset_requested = 1;
1311 if (cpu_single_env) {
1312 cpu_single_env->stopped = 1;
1313 cpu_exit(cpu_single_env);
1315 cpu_stop_current();
1316 qemu_notify_event();
1319 void qemu_system_shutdown_request(void)
1321 shutdown_requested = 1;
1322 qemu_notify_event();
1325 void qemu_system_powerdown_request(void)
1327 powerdown_requested = 1;
1328 qemu_notify_event();
1331 void qemu_system_debug_request(void)
1333 debug_requested = 1;
1334 qemu_notify_event();
1337 void qemu_system_vmstop_request(int reason)
1339 vmstop_requested = reason;
1340 qemu_notify_event();
1343 void main_loop_wait(int nonblocking)
1345 IOHandlerRecord *ioh;
1346 fd_set rfds, wfds, xfds;
1347 int ret, nfds;
1348 struct timeval tv;
1349 int timeout;
1351 if (nonblocking)
1352 timeout = 0;
1353 else {
1354 timeout = qemu_calculate_timeout();
1355 qemu_bh_update_timeout(&timeout);
1358 os_host_main_loop_wait(&timeout);
1360 /* poll any events */
1361 /* XXX: separate device handlers from system ones */
1362 nfds = -1;
1363 FD_ZERO(&rfds);
1364 FD_ZERO(&wfds);
1365 FD_ZERO(&xfds);
1366 QLIST_FOREACH(ioh, &io_handlers, next) {
1367 if (ioh->deleted)
1368 continue;
1369 if (ioh->fd_read &&
1370 (!ioh->fd_read_poll ||
1371 ioh->fd_read_poll(ioh->opaque) != 0)) {
1372 FD_SET(ioh->fd, &rfds);
1373 if (ioh->fd > nfds)
1374 nfds = ioh->fd;
1376 if (ioh->fd_write) {
1377 FD_SET(ioh->fd, &wfds);
1378 if (ioh->fd > nfds)
1379 nfds = ioh->fd;
1383 tv.tv_sec = timeout / 1000;
1384 tv.tv_usec = (timeout % 1000) * 1000;
1386 slirp_select_fill(&nfds, &rfds, &wfds, &xfds);
1388 qemu_mutex_unlock_iothread();
1389 ret = select(nfds + 1, &rfds, &wfds, &xfds, &tv);
1390 qemu_mutex_lock_iothread();
1391 if (ret > 0) {
1392 IOHandlerRecord *pioh;
1394 QLIST_FOREACH_SAFE(ioh, &io_handlers, next, pioh) {
1395 if (!ioh->deleted && ioh->fd_read && FD_ISSET(ioh->fd, &rfds)) {
1396 ioh->fd_read(ioh->opaque);
1397 if (!(ioh->fd_read_poll && ioh->fd_read_poll(ioh->opaque)))
1398 FD_CLR(ioh->fd, &rfds);
1400 if (!ioh->deleted && ioh->fd_write && FD_ISSET(ioh->fd, &wfds)) {
1401 ioh->fd_write(ioh->opaque);
1404 /* Do this last in case read/write handlers marked it for deletion */
1405 if (ioh->deleted) {
1406 QLIST_REMOVE(ioh, next);
1407 qemu_free(ioh);
1412 slirp_select_poll(&rfds, &wfds, &xfds, (ret < 0));
1414 qemu_run_all_timers();
1416 /* Check bottom-halves last in case any of the earlier events triggered
1417 them. */
1418 qemu_bh_poll();
1422 #ifndef CONFIG_IOTHREAD
1423 static int vm_request_pending(void)
1425 return powerdown_requested ||
1426 reset_requested ||
1427 shutdown_requested ||
1428 debug_requested ||
1429 vmstop_requested;
1431 #endif
1433 qemu_irq qemu_system_powerdown;
1435 static void main_loop(void)
1437 bool nonblocking = false;
1438 #ifdef CONFIG_PROFILER
1439 int64_t ti;
1440 #endif
1441 int r;
1443 if (kvm_enabled()) {
1444 kvm_main_loop();
1445 cpu_disable_ticks();
1446 return;
1449 qemu_main_loop_start();
1451 for (;;) {
1452 #ifndef CONFIG_IOTHREAD
1453 nonblocking = cpu_exec_all();
1454 if (vm_request_pending()) {
1455 nonblocking = true;
1457 #endif
1458 #ifdef CONFIG_PROFILER
1459 ti = profile_getclock();
1460 #endif
1461 main_loop_wait(nonblocking);
1462 #ifdef CONFIG_PROFILER
1463 dev_time += profile_getclock() - ti;
1464 #endif
1466 if (qemu_debug_requested()) {
1467 vm_stop(VMSTOP_DEBUG);
1469 if (qemu_shutdown_requested()) {
1470 monitor_protocol_event(QEVENT_SHUTDOWN, NULL);
1471 if (no_shutdown) {
1472 vm_stop(VMSTOP_SHUTDOWN);
1473 no_shutdown = 0;
1474 } else
1475 break;
1477 if (qemu_reset_requested()) {
1478 pause_all_vcpus();
1479 qemu_system_reset();
1480 resume_all_vcpus();
1482 if (qemu_powerdown_requested()) {
1483 monitor_protocol_event(QEVENT_POWERDOWN, NULL);
1484 qemu_irq_raise(qemu_system_powerdown);
1486 if ((r = qemu_vmstop_requested())) {
1487 vm_stop(r);
1490 bdrv_close_all();
1491 pause_all_vcpus();
1494 static void version(void)
1496 printf("QEMU emulator version " QEMU_VERSION QEMU_PKGVERSION ", Copyright (c) 2003-2008 Fabrice Bellard\n");
1499 static void help(int exitcode)
1501 const char *options_help =
1502 #define DEF(option, opt_arg, opt_enum, opt_help, arch_mask) \
1503 opt_help
1504 #define DEFHEADING(text) stringify(text) "\n"
1505 #include "qemu-options.def"
1506 #undef DEF
1507 #undef DEFHEADING
1508 #undef GEN_DOCS
1510 version();
1511 printf("usage: %s [options] [disk_image]\n"
1512 "\n"
1513 "'disk_image' is a raw hard disk image for IDE hard disk 0\n"
1514 "\n"
1515 "%s\n"
1516 "During emulation, the following keys are useful:\n"
1517 "ctrl-alt-f toggle full screen\n"
1518 "ctrl-alt-n switch to virtual console 'n'\n"
1519 "ctrl-alt toggle mouse and keyboard grab\n"
1520 "\n"
1521 "When using -nographic, press 'ctrl-a h' to get some help.\n",
1522 "qemu",
1523 options_help);
1524 exit(exitcode);
1527 #define HAS_ARG 0x0001
1529 typedef struct QEMUOption {
1530 const char *name;
1531 int flags;
1532 int index;
1533 uint32_t arch_mask;
1534 } QEMUOption;
1536 static const QEMUOption qemu_options[] = {
1537 { "h", 0, QEMU_OPTION_h, QEMU_ARCH_ALL },
1538 #define DEF(option, opt_arg, opt_enum, opt_help, arch_mask) \
1539 { option, opt_arg, opt_enum, arch_mask },
1540 #define DEFHEADING(text)
1541 #include "qemu-options.def"
1542 #undef DEF
1543 #undef DEFHEADING
1544 #undef GEN_DOCS
1545 { NULL },
1547 static void select_vgahw (const char *p)
1549 const char *opts;
1551 default_vga = 0;
1552 vga_interface_type = VGA_NONE;
1553 if (strstart(p, "std", &opts)) {
1554 vga_interface_type = VGA_STD;
1555 } else if (strstart(p, "cirrus", &opts)) {
1556 vga_interface_type = VGA_CIRRUS;
1557 } else if (strstart(p, "vmware", &opts)) {
1558 vga_interface_type = VGA_VMWARE;
1559 } else if (strstart(p, "xenfb", &opts)) {
1560 vga_interface_type = VGA_XENFB;
1561 } else if (strstart(p, "qxl", &opts)) {
1562 vga_interface_type = VGA_QXL;
1563 } else if (!strstart(p, "none", &opts)) {
1564 invalid_vga:
1565 fprintf(stderr, "Unknown vga type: %s\n", p);
1566 exit(1);
1568 while (*opts) {
1569 const char *nextopt;
1571 if (strstart(opts, ",retrace=", &nextopt)) {
1572 opts = nextopt;
1573 if (strstart(opts, "dumb", &nextopt))
1574 vga_retrace_method = VGA_RETRACE_DUMB;
1575 else if (strstart(opts, "precise", &nextopt))
1576 vga_retrace_method = VGA_RETRACE_PRECISE;
1577 else goto invalid_vga;
1578 } else goto invalid_vga;
1579 opts = nextopt;
1583 static int balloon_parse(const char *arg)
1585 QemuOpts *opts;
1587 if (strcmp(arg, "none") == 0) {
1588 return 0;
1591 if (!strncmp(arg, "virtio", 6)) {
1592 if (arg[6] == ',') {
1593 /* have params -> parse them */
1594 opts = qemu_opts_parse(qemu_find_opts("device"), arg+7, 0);
1595 if (!opts)
1596 return -1;
1597 } else {
1598 /* create empty opts */
1599 opts = qemu_opts_create(qemu_find_opts("device"), NULL, 0);
1601 qemu_opt_set(opts, "driver", "virtio-balloon-pci");
1602 return 0;
1605 return -1;
1608 char *qemu_find_file(int type, const char *name)
1610 int len;
1611 const char *subdir;
1612 char *buf;
1614 /* If name contains path separators then try it as a straight path. */
1615 if ((strchr(name, '/') || strchr(name, '\\'))
1616 && access(name, R_OK) == 0) {
1617 return qemu_strdup(name);
1619 switch (type) {
1620 case QEMU_FILE_TYPE_BIOS:
1621 subdir = "";
1622 break;
1623 case QEMU_FILE_TYPE_KEYMAP:
1624 subdir = "keymaps/";
1625 break;
1626 default:
1627 abort();
1629 len = strlen(data_dir) + strlen(name) + strlen(subdir) + 2;
1630 buf = qemu_mallocz(len);
1631 snprintf(buf, len, "%s/%s%s", data_dir, subdir, name);
1632 if (access(buf, R_OK)) {
1633 qemu_free(buf);
1634 return NULL;
1636 return buf;
1639 static int device_help_func(QemuOpts *opts, void *opaque)
1641 return qdev_device_help(opts);
1644 static int device_init_func(QemuOpts *opts, void *opaque)
1646 DeviceState *dev;
1648 dev = qdev_device_add(opts);
1649 if (!dev)
1650 return -1;
1651 return 0;
1654 static int chardev_init_func(QemuOpts *opts, void *opaque)
1656 CharDriverState *chr;
1658 chr = qemu_chr_open_opts(opts, NULL);
1659 if (!chr)
1660 return -1;
1661 return 0;
1664 #ifdef CONFIG_VIRTFS
1665 static int fsdev_init_func(QemuOpts *opts, void *opaque)
1667 int ret;
1668 ret = qemu_fsdev_add(opts);
1670 return ret;
1672 #endif
1674 static int mon_init_func(QemuOpts *opts, void *opaque)
1676 CharDriverState *chr;
1677 const char *chardev;
1678 const char *mode;
1679 int flags;
1681 mode = qemu_opt_get(opts, "mode");
1682 if (mode == NULL) {
1683 mode = "readline";
1685 if (strcmp(mode, "readline") == 0) {
1686 flags = MONITOR_USE_READLINE;
1687 } else if (strcmp(mode, "control") == 0) {
1688 flags = MONITOR_USE_CONTROL;
1689 } else {
1690 fprintf(stderr, "unknown monitor mode \"%s\"\n", mode);
1691 exit(1);
1694 if (qemu_opt_get_bool(opts, "pretty", 0))
1695 flags |= MONITOR_USE_PRETTY;
1697 if (qemu_opt_get_bool(opts, "default", 0))
1698 flags |= MONITOR_IS_DEFAULT;
1700 chardev = qemu_opt_get(opts, "chardev");
1701 chr = qemu_chr_find(chardev);
1702 if (chr == NULL) {
1703 fprintf(stderr, "chardev \"%s\" not found\n", chardev);
1704 exit(1);
1707 monitor_init(chr, flags);
1708 return 0;
1711 static void monitor_parse(const char *optarg, const char *mode)
1713 static int monitor_device_index = 0;
1714 QemuOpts *opts;
1715 const char *p;
1716 char label[32];
1717 int def = 0;
1719 if (strstart(optarg, "chardev:", &p)) {
1720 snprintf(label, sizeof(label), "%s", p);
1721 } else {
1722 snprintf(label, sizeof(label), "compat_monitor%d",
1723 monitor_device_index);
1724 if (monitor_device_index == 0) {
1725 def = 1;
1727 opts = qemu_chr_parse_compat(label, optarg);
1728 if (!opts) {
1729 fprintf(stderr, "parse error: %s\n", optarg);
1730 exit(1);
1734 opts = qemu_opts_create(qemu_find_opts("mon"), label, 1);
1735 if (!opts) {
1736 fprintf(stderr, "duplicate chardev: %s\n", label);
1737 exit(1);
1739 qemu_opt_set(opts, "mode", mode);
1740 qemu_opt_set(opts, "chardev", label);
1741 if (def)
1742 qemu_opt_set(opts, "default", "on");
1743 monitor_device_index++;
1746 struct device_config {
1747 enum {
1748 DEV_USB, /* -usbdevice */
1749 DEV_BT, /* -bt */
1750 DEV_SERIAL, /* -serial */
1751 DEV_PARALLEL, /* -parallel */
1752 DEV_VIRTCON, /* -virtioconsole */
1753 DEV_DEBUGCON, /* -debugcon */
1754 } type;
1755 const char *cmdline;
1756 QTAILQ_ENTRY(device_config) next;
1758 QTAILQ_HEAD(, device_config) device_configs = QTAILQ_HEAD_INITIALIZER(device_configs);
1760 static void add_device_config(int type, const char *cmdline)
1762 struct device_config *conf;
1764 conf = qemu_mallocz(sizeof(*conf));
1765 conf->type = type;
1766 conf->cmdline = cmdline;
1767 QTAILQ_INSERT_TAIL(&device_configs, conf, next);
1770 static int foreach_device_config(int type, int (*func)(const char *cmdline))
1772 struct device_config *conf;
1773 int rc;
1775 QTAILQ_FOREACH(conf, &device_configs, next) {
1776 if (conf->type != type)
1777 continue;
1778 rc = func(conf->cmdline);
1779 if (0 != rc)
1780 return rc;
1782 return 0;
1785 static int serial_parse(const char *devname)
1787 static int index = 0;
1788 char label[32];
1790 if (strcmp(devname, "none") == 0)
1791 return 0;
1792 if (index == MAX_SERIAL_PORTS) {
1793 fprintf(stderr, "qemu: too many serial ports\n");
1794 exit(1);
1796 snprintf(label, sizeof(label), "serial%d", index);
1797 serial_hds[index] = qemu_chr_open(label, devname, NULL);
1798 if (!serial_hds[index]) {
1799 fprintf(stderr, "qemu: could not open serial device '%s': %s\n",
1800 devname, strerror(errno));
1801 return -1;
1803 index++;
1804 return 0;
1807 static int parallel_parse(const char *devname)
1809 static int index = 0;
1810 char label[32];
1812 if (strcmp(devname, "none") == 0)
1813 return 0;
1814 if (index == MAX_PARALLEL_PORTS) {
1815 fprintf(stderr, "qemu: too many parallel ports\n");
1816 exit(1);
1818 snprintf(label, sizeof(label), "parallel%d", index);
1819 parallel_hds[index] = qemu_chr_open(label, devname, NULL);
1820 if (!parallel_hds[index]) {
1821 fprintf(stderr, "qemu: could not open parallel device '%s': %s\n",
1822 devname, strerror(errno));
1823 return -1;
1825 index++;
1826 return 0;
1829 static int virtcon_parse(const char *devname)
1831 QemuOptsList *device = qemu_find_opts("device");
1832 static int index = 0;
1833 char label[32];
1834 QemuOpts *bus_opts, *dev_opts;
1836 if (strcmp(devname, "none") == 0)
1837 return 0;
1838 if (index == MAX_VIRTIO_CONSOLES) {
1839 fprintf(stderr, "qemu: too many virtio consoles\n");
1840 exit(1);
1843 bus_opts = qemu_opts_create(device, NULL, 0);
1844 qemu_opt_set(bus_opts, "driver", "virtio-serial");
1846 dev_opts = qemu_opts_create(device, NULL, 0);
1847 qemu_opt_set(dev_opts, "driver", "virtconsole");
1849 snprintf(label, sizeof(label), "virtcon%d", index);
1850 virtcon_hds[index] = qemu_chr_open(label, devname, NULL);
1851 if (!virtcon_hds[index]) {
1852 fprintf(stderr, "qemu: could not open virtio console '%s': %s\n",
1853 devname, strerror(errno));
1854 return -1;
1856 qemu_opt_set(dev_opts, "chardev", label);
1858 index++;
1859 return 0;
1862 static int debugcon_parse(const char *devname)
1864 QemuOpts *opts;
1866 if (!qemu_chr_open("debugcon", devname, NULL)) {
1867 exit(1);
1869 opts = qemu_opts_create(qemu_find_opts("device"), "debugcon", 1);
1870 if (!opts) {
1871 fprintf(stderr, "qemu: already have a debugcon device\n");
1872 exit(1);
1874 qemu_opt_set(opts, "driver", "isa-debugcon");
1875 qemu_opt_set(opts, "chardev", "debugcon");
1876 return 0;
1879 void qemu_add_exit_notifier(Notifier *notify)
1881 notifier_list_add(&exit_notifiers, notify);
1884 void qemu_remove_exit_notifier(Notifier *notify)
1886 notifier_list_remove(&exit_notifiers, notify);
1889 static void qemu_run_exit_notifiers(void)
1891 notifier_list_notify(&exit_notifiers);
1894 void qemu_add_machine_init_done_notifier(Notifier *notify)
1896 notifier_list_add(&machine_init_done_notifiers, notify);
1899 static void qemu_run_machine_init_done_notifiers(void)
1901 notifier_list_notify(&machine_init_done_notifiers);
1904 static const QEMUOption *lookup_opt(int argc, char **argv,
1905 const char **poptarg, int *poptind)
1907 const QEMUOption *popt;
1908 int optind = *poptind;
1909 char *r = argv[optind];
1910 const char *optarg;
1912 loc_set_cmdline(argv, optind, 1);
1913 optind++;
1914 /* Treat --foo the same as -foo. */
1915 if (r[1] == '-')
1916 r++;
1917 popt = qemu_options;
1918 for(;;) {
1919 if (!popt->name) {
1920 error_report("invalid option");
1921 exit(1);
1923 if (!strcmp(popt->name, r + 1))
1924 break;
1925 popt++;
1927 if (popt->flags & HAS_ARG) {
1928 if (optind >= argc) {
1929 error_report("requires an argument");
1930 exit(1);
1932 optarg = argv[optind++];
1933 loc_set_cmdline(argv, optind - 2, 2);
1934 } else {
1935 optarg = NULL;
1938 *poptarg = optarg;
1939 *poptind = optind;
1941 return popt;
1944 int main(int argc, char **argv, char **envp)
1946 const char *gdbstub_dev = NULL;
1947 int i;
1948 int snapshot, linux_boot;
1949 const char *icount_option = NULL;
1950 const char *initrd_filename;
1951 const char *kernel_filename, *kernel_cmdline;
1952 char boot_devices[33] = "cad"; /* default to HD->floppy->CD-ROM */
1953 DisplayState *ds;
1954 DisplayChangeListener *dcl;
1955 int cyls, heads, secs, translation;
1956 QemuOpts *hda_opts = NULL, *opts;
1957 QemuOptsList *olist;
1958 int optind;
1959 const char *optarg;
1960 const char *loadvm = NULL;
1961 QEMUMachine *machine;
1962 const char *cpu_model;
1963 int tb_size;
1964 const char *pid_file = NULL;
1965 const char *incoming = NULL;
1966 int show_vnc_port = 0;
1967 int defconfig = 1;
1969 #ifdef CONFIG_SIMPLE_TRACE
1970 const char *trace_file = NULL;
1971 #endif
1972 atexit(qemu_run_exit_notifiers);
1973 error_set_progname(argv[0]);
1975 init_clocks();
1977 qemu_cache_utils_init(envp);
1979 QLIST_INIT (&vm_change_state_head);
1980 os_setup_early_signal_handling();
1982 module_call_init(MODULE_INIT_MACHINE);
1983 machine = find_default_machine();
1984 cpu_model = NULL;
1985 initrd_filename = NULL;
1986 ram_size = 0;
1987 snapshot = 0;
1988 kernel_filename = NULL;
1989 kernel_cmdline = "";
1990 cyls = heads = secs = 0;
1991 translation = BIOS_ATA_TRANSLATION_AUTO;
1993 for (i = 0; i < MAX_NODES; i++) {
1994 node_mem[i] = 0;
1995 node_cpumask[i] = 0;
1998 nb_numa_nodes = 0;
1999 nb_nics = 0;
2001 tb_size = 0;
2002 autostart= 1;
2004 /* first pass of option parsing */
2005 optind = 1;
2006 while (optind < argc) {
2007 if (argv[optind][0] != '-') {
2008 /* disk image */
2009 optind++;
2010 continue;
2011 } else {
2012 const QEMUOption *popt;
2014 popt = lookup_opt(argc, argv, &optarg, &optind);
2015 switch (popt->index) {
2016 case QEMU_OPTION_nodefconfig:
2017 defconfig=0;
2018 break;
2023 if (defconfig) {
2024 int ret;
2026 ret = qemu_read_config_file(CONFIG_QEMU_CONFDIR "/qemu.conf");
2027 if (ret < 0 && ret != -ENOENT) {
2028 exit(1);
2031 ret = qemu_read_config_file(arch_config_name);
2032 if (ret < 0 && ret != -ENOENT) {
2033 exit(1);
2036 cpudef_init();
2038 /* second pass of option parsing */
2039 optind = 1;
2040 for(;;) {
2041 if (optind >= argc)
2042 break;
2043 if (argv[optind][0] != '-') {
2044 hda_opts = drive_add(IF_DEFAULT, 0, argv[optind++], HD_OPTS);
2045 } else {
2046 const QEMUOption *popt;
2048 popt = lookup_opt(argc, argv, &optarg, &optind);
2049 if (!(popt->arch_mask & arch_type)) {
2050 printf("Option %s not supported for this target\n", popt->name);
2051 exit(1);
2053 switch(popt->index) {
2054 case QEMU_OPTION_M:
2055 machine = find_machine(optarg);
2056 if (!machine) {
2057 QEMUMachine *m;
2058 printf("Supported machines are:\n");
2059 for(m = first_machine; m != NULL; m = m->next) {
2060 if (m->alias)
2061 printf("%-10s %s (alias of %s)\n",
2062 m->alias, m->desc, m->name);
2063 printf("%-10s %s%s\n",
2064 m->name, m->desc,
2065 m->is_default ? " (default)" : "");
2067 exit(*optarg != '?');
2069 break;
2070 case QEMU_OPTION_cpu:
2071 /* hw initialization will check this */
2072 if (*optarg == '?') {
2073 list_cpus(stdout, &fprintf, optarg);
2074 exit(0);
2075 } else {
2076 cpu_model = optarg;
2078 break;
2079 case QEMU_OPTION_initrd:
2080 initrd_filename = optarg;
2081 break;
2082 case QEMU_OPTION_hda:
2084 char buf[256];
2085 if (cyls == 0)
2086 snprintf(buf, sizeof(buf), "%s", HD_OPTS);
2087 else
2088 snprintf(buf, sizeof(buf),
2089 "%s,cyls=%d,heads=%d,secs=%d%s",
2090 HD_OPTS , cyls, heads, secs,
2091 translation == BIOS_ATA_TRANSLATION_LBA ?
2092 ",trans=lba" :
2093 translation == BIOS_ATA_TRANSLATION_NONE ?
2094 ",trans=none" : "");
2095 drive_add(IF_DEFAULT, 0, optarg, buf);
2096 break;
2098 case QEMU_OPTION_hdb:
2099 case QEMU_OPTION_hdc:
2100 case QEMU_OPTION_hdd:
2101 drive_add(IF_DEFAULT, popt->index - QEMU_OPTION_hda, optarg,
2102 HD_OPTS);
2103 break;
2104 case QEMU_OPTION_drive:
2105 drive_def(optarg);
2106 break;
2107 case QEMU_OPTION_set:
2108 if (qemu_set_option(optarg) != 0)
2109 exit(1);
2110 break;
2111 case QEMU_OPTION_global:
2112 if (qemu_global_option(optarg) != 0)
2113 exit(1);
2114 break;
2115 case QEMU_OPTION_mtdblock:
2116 drive_add(IF_MTD, -1, optarg, MTD_OPTS);
2117 break;
2118 case QEMU_OPTION_sd:
2119 drive_add(IF_SD, 0, optarg, SD_OPTS);
2120 break;
2121 case QEMU_OPTION_pflash:
2122 drive_add(IF_PFLASH, -1, optarg, PFLASH_OPTS);
2123 break;
2124 case QEMU_OPTION_snapshot:
2125 snapshot = 1;
2126 break;
2127 case QEMU_OPTION_hdachs:
2129 const char *p;
2130 p = optarg;
2131 cyls = strtol(p, (char **)&p, 0);
2132 if (cyls < 1 || cyls > 16383)
2133 goto chs_fail;
2134 if (*p != ',')
2135 goto chs_fail;
2136 p++;
2137 heads = strtol(p, (char **)&p, 0);
2138 if (heads < 1 || heads > 16)
2139 goto chs_fail;
2140 if (*p != ',')
2141 goto chs_fail;
2142 p++;
2143 secs = strtol(p, (char **)&p, 0);
2144 if (secs < 1 || secs > 63)
2145 goto chs_fail;
2146 if (*p == ',') {
2147 p++;
2148 if (!strcmp(p, "none"))
2149 translation = BIOS_ATA_TRANSLATION_NONE;
2150 else if (!strcmp(p, "lba"))
2151 translation = BIOS_ATA_TRANSLATION_LBA;
2152 else if (!strcmp(p, "auto"))
2153 translation = BIOS_ATA_TRANSLATION_AUTO;
2154 else
2155 goto chs_fail;
2156 } else if (*p != '\0') {
2157 chs_fail:
2158 fprintf(stderr, "qemu: invalid physical CHS format\n");
2159 exit(1);
2161 if (hda_opts != NULL) {
2162 char num[16];
2163 snprintf(num, sizeof(num), "%d", cyls);
2164 qemu_opt_set(hda_opts, "cyls", num);
2165 snprintf(num, sizeof(num), "%d", heads);
2166 qemu_opt_set(hda_opts, "heads", num);
2167 snprintf(num, sizeof(num), "%d", secs);
2168 qemu_opt_set(hda_opts, "secs", num);
2169 if (translation == BIOS_ATA_TRANSLATION_LBA)
2170 qemu_opt_set(hda_opts, "trans", "lba");
2171 if (translation == BIOS_ATA_TRANSLATION_NONE)
2172 qemu_opt_set(hda_opts, "trans", "none");
2175 break;
2176 case QEMU_OPTION_numa:
2177 if (nb_numa_nodes >= MAX_NODES) {
2178 fprintf(stderr, "qemu: too many NUMA nodes\n");
2179 exit(1);
2181 numa_add(optarg);
2182 break;
2183 case QEMU_OPTION_nographic:
2184 display_type = DT_NOGRAPHIC;
2185 break;
2186 #ifdef CONFIG_CURSES
2187 case QEMU_OPTION_curses:
2188 display_type = DT_CURSES;
2189 break;
2190 #endif
2191 case QEMU_OPTION_portrait:
2192 graphic_rotate = 1;
2193 break;
2194 case QEMU_OPTION_kernel:
2195 kernel_filename = optarg;
2196 break;
2197 case QEMU_OPTION_append:
2198 kernel_cmdline = optarg;
2199 break;
2200 case QEMU_OPTION_cdrom:
2201 drive_add(IF_DEFAULT, 2, optarg, CDROM_OPTS);
2202 break;
2203 case QEMU_OPTION_boot:
2205 static const char * const params[] = {
2206 "order", "once", "menu", NULL
2208 char buf[sizeof(boot_devices)];
2209 char *standard_boot_devices;
2210 int legacy = 0;
2212 if (!strchr(optarg, '=')) {
2213 legacy = 1;
2214 pstrcpy(buf, sizeof(buf), optarg);
2215 } else if (check_params(buf, sizeof(buf), params, optarg) < 0) {
2216 fprintf(stderr,
2217 "qemu: unknown boot parameter '%s' in '%s'\n",
2218 buf, optarg);
2219 exit(1);
2222 if (legacy ||
2223 get_param_value(buf, sizeof(buf), "order", optarg)) {
2224 validate_bootdevices(buf);
2225 pstrcpy(boot_devices, sizeof(boot_devices), buf);
2227 if (!legacy) {
2228 if (get_param_value(buf, sizeof(buf),
2229 "once", optarg)) {
2230 validate_bootdevices(buf);
2231 standard_boot_devices = qemu_strdup(boot_devices);
2232 pstrcpy(boot_devices, sizeof(boot_devices), buf);
2233 qemu_register_reset(restore_boot_devices,
2234 standard_boot_devices);
2236 if (get_param_value(buf, sizeof(buf),
2237 "menu", optarg)) {
2238 if (!strcmp(buf, "on")) {
2239 boot_menu = 1;
2240 } else if (!strcmp(buf, "off")) {
2241 boot_menu = 0;
2242 } else {
2243 fprintf(stderr,
2244 "qemu: invalid option value '%s'\n",
2245 buf);
2246 exit(1);
2251 break;
2252 case QEMU_OPTION_fda:
2253 case QEMU_OPTION_fdb:
2254 drive_add(IF_FLOPPY, popt->index - QEMU_OPTION_fda,
2255 optarg, FD_OPTS);
2256 break;
2257 case QEMU_OPTION_no_fd_bootchk:
2258 fd_bootchk = 0;
2259 break;
2260 case QEMU_OPTION_netdev:
2261 if (net_client_parse(qemu_find_opts("netdev"), optarg) == -1) {
2262 exit(1);
2264 break;
2265 case QEMU_OPTION_net:
2266 if (net_client_parse(qemu_find_opts("net"), optarg) == -1) {
2267 exit(1);
2269 break;
2270 #ifdef CONFIG_SLIRP
2271 case QEMU_OPTION_tftp:
2272 legacy_tftp_prefix = optarg;
2273 break;
2274 case QEMU_OPTION_bootp:
2275 legacy_bootp_filename = optarg;
2276 break;
2277 case QEMU_OPTION_redir:
2278 if (net_slirp_redir(optarg) < 0)
2279 exit(1);
2280 break;
2281 #endif
2282 case QEMU_OPTION_bt:
2283 add_device_config(DEV_BT, optarg);
2284 break;
2285 case QEMU_OPTION_audio_help:
2286 if (!(audio_available())) {
2287 printf("Option %s not supported for this target\n", popt->name);
2288 exit(1);
2290 AUD_help ();
2291 exit (0);
2292 break;
2293 case QEMU_OPTION_soundhw:
2294 if (!(audio_available())) {
2295 printf("Option %s not supported for this target\n", popt->name);
2296 exit(1);
2298 select_soundhw (optarg);
2299 break;
2300 case QEMU_OPTION_h:
2301 help(0);
2302 break;
2303 case QEMU_OPTION_version:
2304 version();
2305 exit(0);
2306 break;
2307 case QEMU_OPTION_m: {
2308 int64_t value;
2310 value = strtosz(optarg, NULL);
2311 if (value < 0) {
2312 fprintf(stderr, "qemu: invalid ram size: %s\n", optarg);
2313 exit(1);
2316 /* On 32-bit hosts, QEMU is limited by virtual address space */
2317 if (value > (2047 << 20) && HOST_LONG_BITS == 32) {
2318 fprintf(stderr, "qemu: at most 2047 MB RAM can be simulated\n");
2319 exit(1);
2321 if (value != (uint64_t)(ram_addr_t)value) {
2322 fprintf(stderr, "qemu: ram size too large\n");
2323 exit(1);
2325 ram_size = value;
2326 break;
2328 case QEMU_OPTION_mempath:
2329 mem_path = optarg;
2330 break;
2331 #ifdef MAP_POPULATE
2332 case QEMU_OPTION_mem_prealloc:
2333 mem_prealloc = 1;
2334 break;
2335 #endif
2336 case QEMU_OPTION_d:
2337 set_cpu_log(optarg);
2338 break;
2339 case QEMU_OPTION_s:
2340 gdbstub_dev = "tcp::" DEFAULT_GDBSTUB_PORT;
2341 break;
2342 case QEMU_OPTION_gdb:
2343 gdbstub_dev = optarg;
2344 break;
2345 case QEMU_OPTION_L:
2346 data_dir = optarg;
2347 break;
2348 case QEMU_OPTION_bios:
2349 bios_name = optarg;
2350 break;
2351 case QEMU_OPTION_singlestep:
2352 singlestep = 1;
2353 break;
2354 case QEMU_OPTION_S:
2355 autostart = 0;
2356 break;
2357 case QEMU_OPTION_k:
2358 keyboard_layout = optarg;
2359 break;
2360 case QEMU_OPTION_localtime:
2361 rtc_utc = 0;
2362 break;
2363 case QEMU_OPTION_vga:
2364 select_vgahw (optarg);
2365 break;
2366 case QEMU_OPTION_g:
2368 const char *p;
2369 int w, h, depth;
2370 p = optarg;
2371 w = strtol(p, (char **)&p, 10);
2372 if (w <= 0) {
2373 graphic_error:
2374 fprintf(stderr, "qemu: invalid resolution or depth\n");
2375 exit(1);
2377 if (*p != 'x')
2378 goto graphic_error;
2379 p++;
2380 h = strtol(p, (char **)&p, 10);
2381 if (h <= 0)
2382 goto graphic_error;
2383 if (*p == 'x') {
2384 p++;
2385 depth = strtol(p, (char **)&p, 10);
2386 if (depth != 8 && depth != 15 && depth != 16 &&
2387 depth != 24 && depth != 32)
2388 goto graphic_error;
2389 } else if (*p == '\0') {
2390 depth = graphic_depth;
2391 } else {
2392 goto graphic_error;
2395 graphic_width = w;
2396 graphic_height = h;
2397 graphic_depth = depth;
2399 break;
2400 case QEMU_OPTION_echr:
2402 char *r;
2403 term_escape_char = strtol(optarg, &r, 0);
2404 if (r == optarg)
2405 printf("Bad argument to echr\n");
2406 break;
2408 case QEMU_OPTION_monitor:
2409 monitor_parse(optarg, "readline");
2410 default_monitor = 0;
2411 break;
2412 case QEMU_OPTION_qmp:
2413 monitor_parse(optarg, "control");
2414 default_monitor = 0;
2415 break;
2416 case QEMU_OPTION_mon:
2417 opts = qemu_opts_parse(qemu_find_opts("mon"), optarg, 1);
2418 if (!opts) {
2419 exit(1);
2421 default_monitor = 0;
2422 break;
2423 case QEMU_OPTION_chardev:
2424 opts = qemu_opts_parse(qemu_find_opts("chardev"), optarg, 1);
2425 if (!opts) {
2426 exit(1);
2428 break;
2429 case QEMU_OPTION_fsdev:
2430 olist = qemu_find_opts("fsdev");
2431 if (!olist) {
2432 fprintf(stderr, "fsdev is not supported by this qemu build.\n");
2433 exit(1);
2435 opts = qemu_opts_parse(olist, optarg, 1);
2436 if (!opts) {
2437 fprintf(stderr, "parse error: %s\n", optarg);
2438 exit(1);
2440 break;
2441 case QEMU_OPTION_virtfs: {
2442 char *arg_fsdev = NULL;
2443 char *arg_9p = NULL;
2444 int len = 0;
2446 olist = qemu_find_opts("virtfs");
2447 if (!olist) {
2448 fprintf(stderr, "virtfs is not supported by this qemu build.\n");
2449 exit(1);
2451 opts = qemu_opts_parse(olist, optarg, 1);
2452 if (!opts) {
2453 fprintf(stderr, "parse error: %s\n", optarg);
2454 exit(1);
2457 if (qemu_opt_get(opts, "fstype") == NULL ||
2458 qemu_opt_get(opts, "mount_tag") == NULL ||
2459 qemu_opt_get(opts, "path") == NULL ||
2460 qemu_opt_get(opts, "security_model") == NULL) {
2461 fprintf(stderr, "Usage: -virtfs fstype,path=/share_path/,"
2462 "security_model=[mapped|passthrough|none],"
2463 "mnt_tag=tag.\n");
2464 exit(1);
2467 len = strlen(",id=,path=,security_model=");
2468 len += strlen(qemu_opt_get(opts, "fstype"));
2469 len += strlen(qemu_opt_get(opts, "mount_tag"));
2470 len += strlen(qemu_opt_get(opts, "path"));
2471 len += strlen(qemu_opt_get(opts, "security_model"));
2472 arg_fsdev = qemu_malloc((len + 1) * sizeof(*arg_fsdev));
2474 snprintf(arg_fsdev, (len + 1) * sizeof(*arg_fsdev),
2475 "%s,id=%s,path=%s,security_model=%s",
2476 qemu_opt_get(opts, "fstype"),
2477 qemu_opt_get(opts, "mount_tag"),
2478 qemu_opt_get(opts, "path"),
2479 qemu_opt_get(opts, "security_model"));
2481 len = strlen("virtio-9p-pci,fsdev=,mount_tag=");
2482 len += 2*strlen(qemu_opt_get(opts, "mount_tag"));
2483 arg_9p = qemu_malloc((len + 1) * sizeof(*arg_9p));
2485 snprintf(arg_9p, (len + 1) * sizeof(*arg_9p),
2486 "virtio-9p-pci,fsdev=%s,mount_tag=%s",
2487 qemu_opt_get(opts, "mount_tag"),
2488 qemu_opt_get(opts, "mount_tag"));
2490 if (!qemu_opts_parse(qemu_find_opts("fsdev"), arg_fsdev, 1)) {
2491 fprintf(stderr, "parse error [fsdev]: %s\n", optarg);
2492 exit(1);
2495 if (!qemu_opts_parse(qemu_find_opts("device"), arg_9p, 1)) {
2496 fprintf(stderr, "parse error [device]: %s\n", optarg);
2497 exit(1);
2500 qemu_free(arg_fsdev);
2501 qemu_free(arg_9p);
2502 break;
2504 case QEMU_OPTION_serial:
2505 add_device_config(DEV_SERIAL, optarg);
2506 default_serial = 0;
2507 if (strncmp(optarg, "mon:", 4) == 0) {
2508 default_monitor = 0;
2510 break;
2511 case QEMU_OPTION_watchdog:
2512 if (watchdog) {
2513 fprintf(stderr,
2514 "qemu: only one watchdog option may be given\n");
2515 return 1;
2517 watchdog = optarg;
2518 break;
2519 case QEMU_OPTION_watchdog_action:
2520 if (select_watchdog_action(optarg) == -1) {
2521 fprintf(stderr, "Unknown -watchdog-action parameter\n");
2522 exit(1);
2524 break;
2525 case QEMU_OPTION_virtiocon:
2526 add_device_config(DEV_VIRTCON, optarg);
2527 default_virtcon = 0;
2528 if (strncmp(optarg, "mon:", 4) == 0) {
2529 default_monitor = 0;
2531 break;
2532 case QEMU_OPTION_parallel:
2533 add_device_config(DEV_PARALLEL, optarg);
2534 default_parallel = 0;
2535 if (strncmp(optarg, "mon:", 4) == 0) {
2536 default_monitor = 0;
2538 break;
2539 case QEMU_OPTION_debugcon:
2540 add_device_config(DEV_DEBUGCON, optarg);
2541 break;
2542 case QEMU_OPTION_loadvm:
2543 loadvm = optarg;
2544 break;
2545 case QEMU_OPTION_full_screen:
2546 full_screen = 1;
2547 break;
2548 #ifdef CONFIG_SDL
2549 case QEMU_OPTION_no_frame:
2550 no_frame = 1;
2551 break;
2552 case QEMU_OPTION_alt_grab:
2553 alt_grab = 1;
2554 break;
2555 case QEMU_OPTION_ctrl_grab:
2556 ctrl_grab = 1;
2557 break;
2558 case QEMU_OPTION_no_quit:
2559 no_quit = 1;
2560 break;
2561 case QEMU_OPTION_sdl:
2562 display_type = DT_SDL;
2563 break;
2564 #endif
2565 case QEMU_OPTION_pidfile:
2566 pid_file = optarg;
2567 break;
2568 case QEMU_OPTION_win2k_hack:
2569 win2k_install_hack = 1;
2570 break;
2571 case QEMU_OPTION_rtc_td_hack:
2572 rtc_td_hack = 1;
2573 break;
2574 case QEMU_OPTION_acpitable:
2575 do_acpitable_option(optarg);
2576 break;
2577 case QEMU_OPTION_smbios:
2578 do_smbios_option(optarg);
2579 break;
2580 case QEMU_OPTION_enable_kvm:
2581 kvm_allowed = 1;
2582 break;
2583 case QEMU_OPTION_no_kvm:
2584 kvm_allowed = 0;
2585 #ifdef CONFIG_NO_CPU_EMULATION
2586 fprintf(stderr, "cpu emulation not configured\n");
2587 exit(1);
2588 #endif
2589 break;
2590 #ifdef CONFIG_KVM
2591 case QEMU_OPTION_no_kvm_irqchip: {
2592 kvm_irqchip = 0;
2593 kvm_pit = 0;
2594 break;
2596 case QEMU_OPTION_no_kvm_pit: {
2597 kvm_pit = 0;
2598 break;
2600 case QEMU_OPTION_no_kvm_pit_reinjection: {
2601 kvm_pit_reinject = 0;
2602 break;
2604 case QEMU_OPTION_enable_nesting: {
2605 kvm_nested = 1;
2606 break;
2608 #endif
2609 case QEMU_OPTION_usb:
2610 usb_enabled = 1;
2611 break;
2612 case QEMU_OPTION_usbdevice:
2613 usb_enabled = 1;
2614 add_device_config(DEV_USB, optarg);
2615 break;
2616 case QEMU_OPTION_device:
2617 if (!qemu_opts_parse(qemu_find_opts("device"), optarg, 1)) {
2618 exit(1);
2620 break;
2621 case QEMU_OPTION_smp:
2622 smp_parse(optarg);
2623 if (smp_cpus < 1) {
2624 fprintf(stderr, "Invalid number of CPUs\n");
2625 exit(1);
2627 if (max_cpus < smp_cpus) {
2628 fprintf(stderr, "maxcpus must be equal to or greater than "
2629 "smp\n");
2630 exit(1);
2632 if (max_cpus > 255) {
2633 fprintf(stderr, "Unsupported number of maxcpus\n");
2634 exit(1);
2636 break;
2637 case QEMU_OPTION_vnc:
2638 display_remote++;
2639 vnc_display = optarg;
2640 break;
2641 case QEMU_OPTION_no_acpi:
2642 acpi_enabled = 0;
2643 break;
2644 case QEMU_OPTION_no_hpet:
2645 no_hpet = 1;
2646 break;
2647 case QEMU_OPTION_balloon:
2648 if (balloon_parse(optarg) < 0) {
2649 fprintf(stderr, "Unknown -balloon argument %s\n", optarg);
2650 exit(1);
2652 break;
2653 case QEMU_OPTION_no_reboot:
2654 no_reboot = 1;
2655 break;
2656 case QEMU_OPTION_no_shutdown:
2657 no_shutdown = 1;
2658 break;
2659 case QEMU_OPTION_show_cursor:
2660 cursor_hide = 0;
2661 break;
2662 case QEMU_OPTION_uuid:
2663 if(qemu_uuid_parse(optarg, qemu_uuid) < 0) {
2664 fprintf(stderr, "Fail to parse UUID string."
2665 " Wrong format.\n");
2666 exit(1);
2668 break;
2669 case QEMU_OPTION_option_rom:
2670 if (nb_option_roms >= MAX_OPTION_ROMS) {
2671 fprintf(stderr, "Too many option ROMs\n");
2672 exit(1);
2674 opts = qemu_opts_parse(qemu_find_opts("option-rom"), optarg, 1);
2675 option_rom[nb_option_roms].name = qemu_opt_get(opts, "romfile");
2676 option_rom[nb_option_roms].bootindex =
2677 qemu_opt_get_number(opts, "bootindex", -1);
2678 if (!option_rom[nb_option_roms].name) {
2679 fprintf(stderr, "Option ROM file is not specified\n");
2680 exit(1);
2682 nb_option_roms++;
2683 break;
2684 case QEMU_OPTION_semihosting:
2685 semihosting_enabled = 1;
2686 break;
2687 case QEMU_OPTION_tdf:
2688 time_drift_fix = 1;
2689 break;
2690 case QEMU_OPTION_kvm_shadow_memory:
2691 kvm_shadow_memory = (int64_t)atoi(optarg) * 1024 * 1024 / 4096;
2692 break;
2693 case QEMU_OPTION_name:
2694 qemu_name = qemu_strdup(optarg);
2696 char *p = strchr(qemu_name, ',');
2697 if (p != NULL) {
2698 *p++ = 0;
2699 if (strncmp(p, "process=", 8)) {
2700 fprintf(stderr, "Unknown subargument %s to -name\n", p);
2701 exit(1);
2703 p += 8;
2704 os_set_proc_name(p);
2707 break;
2708 case QEMU_OPTION_prom_env:
2709 if (nb_prom_envs >= MAX_PROM_ENVS) {
2710 fprintf(stderr, "Too many prom variables\n");
2711 exit(1);
2713 prom_envs[nb_prom_envs] = optarg;
2714 nb_prom_envs++;
2715 break;
2716 case QEMU_OPTION_old_param:
2717 old_param = 1;
2718 break;
2719 case QEMU_OPTION_clock:
2720 configure_alarms(optarg);
2721 break;
2722 case QEMU_OPTION_startdate:
2723 configure_rtc_date_offset(optarg, 1);
2724 break;
2725 case QEMU_OPTION_rtc:
2726 opts = qemu_opts_parse(qemu_find_opts("rtc"), optarg, 0);
2727 if (!opts) {
2728 exit(1);
2730 configure_rtc(opts);
2731 break;
2732 case QEMU_OPTION_tb_size:
2733 tb_size = strtol(optarg, NULL, 0);
2734 if (tb_size < 0)
2735 tb_size = 0;
2736 break;
2737 case QEMU_OPTION_icount:
2738 icount_option = optarg;
2739 break;
2740 case QEMU_OPTION_incoming:
2741 incoming = optarg;
2742 incoming_expected = true;
2743 break;
2744 case QEMU_OPTION_nodefaults:
2745 default_serial = 0;
2746 default_parallel = 0;
2747 default_virtcon = 0;
2748 default_monitor = 0;
2749 default_vga = 0;
2750 default_net = 0;
2751 default_floppy = 0;
2752 default_cdrom = 0;
2753 default_sdcard = 0;
2754 break;
2755 #ifndef _WIN32
2756 case QEMU_OPTION_nvram:
2757 nvram = optarg;
2758 break;
2759 #endif
2760 case QEMU_OPTION_xen_domid:
2761 if (!(xen_available())) {
2762 printf("Option %s not supported for this target\n", popt->name);
2763 exit(1);
2765 xen_domid = atoi(optarg);
2766 break;
2767 case QEMU_OPTION_xen_create:
2768 if (!(xen_available())) {
2769 printf("Option %s not supported for this target\n", popt->name);
2770 exit(1);
2772 xen_mode = XEN_CREATE;
2773 break;
2774 case QEMU_OPTION_xen_attach:
2775 if (!(xen_available())) {
2776 printf("Option %s not supported for this target\n", popt->name);
2777 exit(1);
2779 xen_mode = XEN_ATTACH;
2780 break;
2781 #ifdef CONFIG_SIMPLE_TRACE
2782 case QEMU_OPTION_trace:
2783 opts = qemu_opts_parse(qemu_find_opts("trace"), optarg, 0);
2784 if (opts) {
2785 trace_file = qemu_opt_get(opts, "file");
2787 break;
2788 #endif
2789 case QEMU_OPTION_readconfig:
2791 int ret = qemu_read_config_file(optarg);
2792 if (ret < 0) {
2793 fprintf(stderr, "read config %s: %s\n", optarg,
2794 strerror(-ret));
2795 exit(1);
2797 break;
2799 case QEMU_OPTION_spice:
2800 olist = qemu_find_opts("spice");
2801 if (!olist) {
2802 fprintf(stderr, "spice is not supported by this qemu build.\n");
2803 exit(1);
2805 opts = qemu_opts_parse(olist, optarg, 0);
2806 if (!opts) {
2807 fprintf(stderr, "parse error: %s\n", optarg);
2808 exit(1);
2810 break;
2811 case QEMU_OPTION_writeconfig:
2813 FILE *fp;
2814 if (strcmp(optarg, "-") == 0) {
2815 fp = stdout;
2816 } else {
2817 fp = fopen(optarg, "w");
2818 if (fp == NULL) {
2819 fprintf(stderr, "open %s: %s\n", optarg, strerror(errno));
2820 exit(1);
2823 qemu_config_write(fp);
2824 fclose(fp);
2825 break;
2827 default:
2828 os_parse_cmd_args(popt->index, optarg);
2832 loc_set_none();
2834 /* If no data_dir is specified then try to find it relative to the
2835 executable path. */
2836 if (!data_dir) {
2837 data_dir = os_find_datadir(argv[0]);
2839 /* If all else fails use the install patch specified when building. */
2840 if (!data_dir) {
2841 data_dir = CONFIG_QEMU_DATADIR;
2844 #ifdef CONFIG_SIMPLE_TRACE
2846 * Set the trace file name, if specified.
2848 st_set_trace_file(trace_file);
2849 #endif
2851 * Default to max_cpus = smp_cpus, in case the user doesn't
2852 * specify a max_cpus value.
2854 if (!max_cpus)
2855 max_cpus = smp_cpus;
2857 machine->max_cpus = machine->max_cpus ?: 1; /* Default to UP */
2858 if (smp_cpus > machine->max_cpus) {
2859 fprintf(stderr, "Number of SMP cpus requested (%d), exceeds max cpus "
2860 "supported by machine `%s' (%d)\n", smp_cpus, machine->name,
2861 machine->max_cpus);
2862 exit(1);
2865 qemu_opts_foreach(qemu_find_opts("device"), default_driver_check, NULL, 0);
2866 qemu_opts_foreach(qemu_find_opts("global"), default_driver_check, NULL, 0);
2868 if (machine->no_serial) {
2869 default_serial = 0;
2871 if (machine->no_parallel) {
2872 default_parallel = 0;
2874 if (!machine->use_virtcon) {
2875 default_virtcon = 0;
2877 if (machine->no_vga) {
2878 default_vga = 0;
2880 if (machine->no_floppy) {
2881 default_floppy = 0;
2883 if (machine->no_cdrom) {
2884 default_cdrom = 0;
2886 if (machine->no_sdcard) {
2887 default_sdcard = 0;
2890 if (display_type == DT_NOGRAPHIC) {
2891 if (default_parallel)
2892 add_device_config(DEV_PARALLEL, "null");
2893 if (default_serial && default_monitor) {
2894 add_device_config(DEV_SERIAL, "mon:stdio");
2895 } else if (default_virtcon && default_monitor) {
2896 add_device_config(DEV_VIRTCON, "mon:stdio");
2897 } else {
2898 if (default_serial)
2899 add_device_config(DEV_SERIAL, "stdio");
2900 if (default_virtcon)
2901 add_device_config(DEV_VIRTCON, "stdio");
2902 if (default_monitor)
2903 monitor_parse("stdio", "readline");
2905 } else {
2906 if (default_serial)
2907 add_device_config(DEV_SERIAL, "vc:80Cx24C");
2908 if (default_parallel)
2909 add_device_config(DEV_PARALLEL, "vc:80Cx24C");
2910 if (default_monitor)
2911 monitor_parse("vc:80Cx24C", "readline");
2912 if (default_virtcon)
2913 add_device_config(DEV_VIRTCON, "vc:80Cx24C");
2915 if (default_vga)
2916 vga_interface_type = VGA_CIRRUS;
2918 socket_init();
2920 if (qemu_opts_foreach(qemu_find_opts("chardev"), chardev_init_func, NULL, 1) != 0)
2921 exit(1);
2922 #ifdef CONFIG_VIRTFS
2923 if (qemu_opts_foreach(qemu_find_opts("fsdev"), fsdev_init_func, NULL, 1) != 0) {
2924 exit(1);
2926 #endif
2928 os_daemonize();
2930 if (pid_file && qemu_create_pidfile(pid_file) != 0) {
2931 os_pidfile_error();
2932 exit(1);
2935 if (kvm_allowed) {
2936 int ret = kvm_init();
2937 if (ret < 0) {
2938 if (kvm_allowed > 0) {
2939 if (!kvm_available()) {
2940 printf("KVM not supported for this target\n");
2941 } else {
2942 fprintf(stderr, "failed to initialize KVM: %s\n", strerror(-ret));
2944 exit(1);
2946 fprintf(stderr, "Could not initialize KVM, will disable KVM support\n");
2948 kvm_allowed = ret >= 0;
2951 if (qemu_init_main_loop()) {
2952 fprintf(stderr, "qemu_init_main_loop failed\n");
2953 exit(1);
2955 linux_boot = (kernel_filename != NULL);
2957 if (!linux_boot && *kernel_cmdline != '\0') {
2958 fprintf(stderr, "-append only allowed with -kernel option\n");
2959 exit(1);
2962 if (!linux_boot && initrd_filename != NULL) {
2963 fprintf(stderr, "-initrd only allowed with -kernel option\n");
2964 exit(1);
2967 os_set_line_buffering();
2969 if (init_timer_alarm() < 0) {
2970 fprintf(stderr, "could not initialize alarm timer\n");
2971 exit(1);
2973 configure_icount(icount_option);
2975 if (net_init_clients() < 0) {
2976 exit(1);
2979 /* init the bluetooth world */
2980 if (foreach_device_config(DEV_BT, bt_parse))
2981 exit(1);
2983 /* init the memory */
2984 if (ram_size == 0)
2985 ram_size = DEFAULT_RAM_SIZE * 1024 * 1024;
2987 /* init the dynamic translator */
2988 cpu_exec_init_all(tb_size * 1024 * 1024);
2990 bdrv_init_with_whitelist();
2992 blk_mig_init();
2994 /* open the virtual block devices */
2995 if (snapshot)
2996 qemu_opts_foreach(qemu_find_opts("drive"), drive_enable_snapshot, NULL, 0);
2997 if (qemu_opts_foreach(qemu_find_opts("drive"), drive_init_func, &machine->use_scsi, 1) != 0)
2998 exit(1);
3000 default_drive(default_cdrom, snapshot, machine->use_scsi,
3001 IF_DEFAULT, 2, CDROM_OPTS);
3002 default_drive(default_floppy, snapshot, machine->use_scsi,
3003 IF_FLOPPY, 0, FD_OPTS);
3004 default_drive(default_sdcard, snapshot, machine->use_scsi,
3005 IF_SD, 0, SD_OPTS);
3007 register_savevm_live(NULL, "ram", 0, 4, NULL, ram_save_live, NULL,
3008 ram_load, NULL);
3010 if (nb_numa_nodes > 0) {
3011 int i;
3013 if (nb_numa_nodes > smp_cpus) {
3014 nb_numa_nodes = smp_cpus;
3017 /* If no memory size if given for any node, assume the default case
3018 * and distribute the available memory equally across all nodes
3020 for (i = 0; i < nb_numa_nodes; i++) {
3021 if (node_mem[i] != 0)
3022 break;
3024 if (i == nb_numa_nodes) {
3025 uint64_t usedmem = 0;
3027 /* On Linux, the each node's border has to be 8MB aligned,
3028 * the final node gets the rest.
3030 for (i = 0; i < nb_numa_nodes - 1; i++) {
3031 node_mem[i] = (ram_size / nb_numa_nodes) & ~((1 << 23UL) - 1);
3032 usedmem += node_mem[i];
3034 node_mem[i] = ram_size - usedmem;
3037 for (i = 0; i < nb_numa_nodes; i++) {
3038 if (node_cpumask[i] != 0)
3039 break;
3041 /* assigning the VCPUs round-robin is easier to implement, guest OSes
3042 * must cope with this anyway, because there are BIOSes out there in
3043 * real machines which also use this scheme.
3045 if (i == nb_numa_nodes) {
3046 for (i = 0; i < smp_cpus; i++) {
3047 node_cpumask[i % nb_numa_nodes] |= 1 << i;
3052 if (qemu_opts_foreach(qemu_find_opts("mon"), mon_init_func, NULL, 1) != 0) {
3053 exit(1);
3056 if (foreach_device_config(DEV_SERIAL, serial_parse) < 0)
3057 exit(1);
3058 if (foreach_device_config(DEV_PARALLEL, parallel_parse) < 0)
3059 exit(1);
3060 if (foreach_device_config(DEV_VIRTCON, virtcon_parse) < 0)
3061 exit(1);
3062 if (foreach_device_config(DEV_DEBUGCON, debugcon_parse) < 0)
3063 exit(1);
3065 module_call_init(MODULE_INIT_DEVICE);
3067 if (qemu_opts_foreach(qemu_find_opts("device"), device_help_func, NULL, 0) != 0)
3068 exit(0);
3070 if (watchdog) {
3071 i = select_watchdog(watchdog);
3072 if (i > 0)
3073 exit (i == 1 ? 1 : 0);
3076 if (machine->compat_props) {
3077 qdev_prop_register_global_list(machine->compat_props);
3079 qemu_add_globals();
3081 machine->init(ram_size, boot_devices,
3082 kernel_filename, kernel_cmdline, initrd_filename, cpu_model);
3084 cpu_synchronize_all_post_init();
3086 /* must be after terminal init, SDL library changes signal handlers */
3087 os_setup_signal_handling();
3089 set_numa_modes();
3091 current_machine = machine;
3093 /* init USB devices */
3094 if (usb_enabled) {
3095 if (foreach_device_config(DEV_USB, usb_parse) < 0)
3096 exit(1);
3099 /* init generic devices */
3100 if (qemu_opts_foreach(qemu_find_opts("device"), device_init_func, NULL, 1) != 0)
3101 exit(1);
3103 net_check_clients();
3105 /* just use the first displaystate for the moment */
3106 ds = get_displaystate();
3108 if (using_spice)
3109 display_remote++;
3110 if (display_type == DT_DEFAULT && !display_remote) {
3111 #if defined(CONFIG_SDL) || defined(CONFIG_COCOA)
3112 display_type = DT_SDL;
3113 #else
3114 vnc_display = "localhost:0,to=99";
3115 show_vnc_port = 1;
3116 #endif
3120 /* init local displays */
3121 switch (display_type) {
3122 case DT_NOGRAPHIC:
3123 break;
3124 #if defined(CONFIG_CURSES)
3125 case DT_CURSES:
3126 curses_display_init(ds, full_screen);
3127 break;
3128 #endif
3129 #if defined(CONFIG_SDL)
3130 case DT_SDL:
3131 sdl_display_init(ds, full_screen, no_frame);
3132 break;
3133 #elif defined(CONFIG_COCOA)
3134 case DT_SDL:
3135 cocoa_display_init(ds, full_screen);
3136 break;
3137 #endif
3138 default:
3139 break;
3142 /* init remote displays */
3143 if (vnc_display) {
3144 vnc_display_init(ds);
3145 if (vnc_display_open(ds, vnc_display) < 0)
3146 exit(1);
3148 if (show_vnc_port) {
3149 printf("VNC server running on `%s'\n", vnc_display_local_addr(ds));
3152 #ifdef CONFIG_SPICE
3153 if (using_spice && !qxl_enabled) {
3154 qemu_spice_display_init(ds);
3156 #endif
3158 /* display setup */
3159 dpy_resize(ds);
3160 dcl = ds->listeners;
3161 while (dcl != NULL) {
3162 if (dcl->dpy_refresh != NULL) {
3163 ds->gui_timer = qemu_new_timer(rt_clock, gui_update, ds);
3164 qemu_mod_timer(ds->gui_timer, qemu_get_clock(rt_clock));
3165 break;
3167 dcl = dcl->next;
3169 if (ds->gui_timer == NULL) {
3170 nographic_timer = qemu_new_timer(rt_clock, nographic_update, NULL);
3171 qemu_mod_timer(nographic_timer, qemu_get_clock(rt_clock));
3173 text_consoles_set_display(ds);
3175 if (gdbstub_dev && gdbserver_start(gdbstub_dev) < 0) {
3176 fprintf(stderr, "qemu: could not open gdbserver on device '%s'\n",
3177 gdbstub_dev);
3178 exit(1);
3181 qdev_machine_creation_done();
3183 if (rom_load_all() != 0) {
3184 fprintf(stderr, "rom loading failed\n");
3185 exit(1);
3188 /* TODO: once all bus devices are qdevified, this should be done
3189 * when bus is created by qdev.c */
3190 qemu_register_reset(qbus_reset_all_fn, sysbus_get_default());
3191 qemu_run_machine_init_done_notifiers();
3193 qemu_system_reset();
3194 if (loadvm) {
3195 if (load_vmstate(loadvm) < 0) {
3196 autostart = 0;
3200 if (incoming) {
3201 int ret = qemu_start_incoming_migration(incoming);
3202 if (ret < 0) {
3203 fprintf(stderr, "Migration failed. Exit code %s(%d), exiting.\n",
3204 incoming, ret);
3205 exit(ret);
3207 } else if (autostart) {
3208 vm_start();
3211 os_setup_post();
3213 main_loop();
3214 quit_timers();
3215 net_cleanup();
3217 return 0;