vnc: fix build error from VNC_DIRTY_WORDS
[qemu.git] / vl.c
blobdbb927dff680ad21af12515b63824189bfea2732
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>
51 #ifdef CONFIG_BSD
52 #include <sys/stat.h>
53 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__DragonFly__)
54 #include <libutil.h>
55 #include <sys/sysctl.h>
56 #else
57 #include <util.h>
58 #endif
59 #else
60 #ifdef __linux__
61 #include <pty.h>
62 #include <malloc.h>
64 #include <linux/ppdev.h>
65 #include <linux/parport.h>
66 #endif
67 #ifdef __sun__
68 #include <sys/stat.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>
77 #include <net/if.h>
78 #include <syslog.h>
79 #include <stropts.h>
80 #endif
81 #endif
82 #endif
84 #if defined(__OpenBSD__)
85 #include <util.h>
86 #endif
88 #if defined(CONFIG_VDE)
89 #include <libvdeplug.h>
90 #endif
92 #ifdef _WIN32
93 #include <windows.h>
94 #endif
96 #ifdef CONFIG_SDL
97 #if defined(__APPLE__) || defined(main)
98 #include <SDL.h>
99 int qemu_main(int argc, char **argv, char **envp);
100 int main(int argc, char **argv)
102 return qemu_main(argc, argv, NULL);
104 #undef main
105 #define main qemu_main
106 #endif
107 #endif /* CONFIG_SDL */
109 #ifdef CONFIG_COCOA
110 #undef main
111 #define main qemu_main
112 #endif /* CONFIG_COCOA */
114 #include "hw/hw.h"
115 #include "hw/boards.h"
116 #include "hw/usb.h"
117 #include "hw/pcmcia.h"
118 #include "hw/pc.h"
119 #include "hw/isa.h"
120 #include "hw/baum.h"
121 #include "hw/bt.h"
122 #include "hw/watchdog.h"
123 #include "hw/smbios.h"
124 #include "hw/xen.h"
125 #include "hw/qdev.h"
126 #include "hw/loader.h"
127 #include "bt-host.h"
128 #include "net.h"
129 #include "net/slirp.h"
130 #include "monitor.h"
131 #include "console.h"
132 #include "sysemu.h"
133 #include "gdbstub.h"
134 #include "qemu-timer.h"
135 #include "qemu-char.h"
136 #include "cache-utils.h"
137 #include "block.h"
138 #include "blockdev.h"
139 #include "block-migration.h"
140 #include "dma.h"
141 #include "audio/audio.h"
142 #include "migration.h"
143 #include "kvm.h"
144 #include "qemu-option.h"
145 #include "qemu-config.h"
146 #include "qemu-objects.h"
147 #include "qemu-options.h"
148 #ifdef CONFIG_VIRTFS
149 #include "fsdev/qemu-fsdev.h"
150 #endif
152 #include "disas.h"
154 #include "qemu_socket.h"
156 #include "slirp/libslirp.h"
158 #include "trace.h"
159 #include "simpletrace.h"
160 #include "qemu-queue.h"
161 #include "cpus.h"
162 #include "arch_init.h"
164 #include "ui/qemu-spice.h"
166 //#define DEBUG_NET
167 //#define DEBUG_SLIRP
169 #define DEFAULT_RAM_SIZE 128
171 #define MAX_VIRTIO_CONSOLES 1
173 static const char *data_dir;
174 const char *bios_name = NULL;
175 enum vga_retrace_method vga_retrace_method = VGA_RETRACE_DUMB;
176 DisplayType display_type = DT_DEFAULT;
177 int display_remote = 0;
178 const char* keyboard_layout = NULL;
179 ram_addr_t ram_size;
180 const char *mem_path = NULL;
181 #ifdef MAP_POPULATE
182 int mem_prealloc = 0; /* force preallocation of physical target memory */
183 #endif
184 int nb_nics;
185 NICInfo nd_table[MAX_NICS];
186 int vm_running;
187 int autostart;
188 int incoming_expected; /* Started with -incoming and waiting for incoming */
189 static int rtc_utc = 1;
190 static int rtc_date_offset = -1; /* -1 means no change */
191 QEMUClock *rtc_clock;
192 int vga_interface_type = VGA_NONE;
193 static int full_screen = 0;
194 #ifdef CONFIG_SDL
195 static int no_frame = 0;
196 #endif
197 int no_quit = 0;
198 CharDriverState *serial_hds[MAX_SERIAL_PORTS];
199 CharDriverState *parallel_hds[MAX_PARALLEL_PORTS];
200 CharDriverState *virtcon_hds[MAX_VIRTIO_CONSOLES];
201 int win2k_install_hack = 0;
202 int rtc_td_hack = 0;
203 int usb_enabled = 0;
204 int singlestep = 0;
205 int smp_cpus = 1;
206 int max_cpus = 0;
207 int smp_cores = 1;
208 int smp_threads = 1;
209 #ifdef CONFIG_VNC
210 const char *vnc_display;
211 #endif
212 int acpi_enabled = 1;
213 int no_hpet = 0;
214 int fd_bootchk = 1;
215 int no_reboot = 0;
216 int no_shutdown = 0;
217 int cursor_hide = 1;
218 int graphic_rotate = 0;
219 uint8_t irq0override = 1;
220 const char *watchdog;
221 QEMUOptionRom option_rom[MAX_OPTION_ROMS];
222 int nb_option_roms;
223 int semihosting_enabled = 0;
224 int old_param = 0;
225 const char *qemu_name;
226 int alt_grab = 0;
227 int ctrl_grab = 0;
228 unsigned int nb_prom_envs = 0;
229 const char *prom_envs[MAX_PROM_ENVS];
230 int boot_menu;
232 typedef struct FWBootEntry FWBootEntry;
234 struct FWBootEntry {
235 QTAILQ_ENTRY(FWBootEntry) link;
236 int32_t bootindex;
237 DeviceState *dev;
238 char *suffix;
241 QTAILQ_HEAD(, FWBootEntry) fw_boot_order = QTAILQ_HEAD_INITIALIZER(fw_boot_order);
243 int nb_numa_nodes;
244 uint64_t node_mem[MAX_NODES];
245 uint64_t node_cpumask[MAX_NODES];
247 static QEMUTimer *nographic_timer;
249 uint8_t qemu_uuid[16];
251 static QEMUBootSetHandler *boot_set_handler;
252 static void *boot_set_opaque;
254 static NotifierList exit_notifiers =
255 NOTIFIER_LIST_INITIALIZER(exit_notifiers);
257 static NotifierList machine_init_done_notifiers =
258 NOTIFIER_LIST_INITIALIZER(machine_init_done_notifiers);
260 int kvm_allowed = 0;
261 uint32_t xen_domid;
262 enum xen_mode xen_mode = XEN_EMULATE;
264 static int default_serial = 1;
265 static int default_parallel = 1;
266 static int default_virtcon = 1;
267 static int default_monitor = 1;
268 static int default_vga = 1;
269 static int default_floppy = 1;
270 static int default_cdrom = 1;
271 static int default_sdcard = 1;
273 static struct {
274 const char *driver;
275 int *flag;
276 } default_list[] = {
277 { .driver = "isa-serial", .flag = &default_serial },
278 { .driver = "isa-parallel", .flag = &default_parallel },
279 { .driver = "isa-fdc", .flag = &default_floppy },
280 { .driver = "ide-drive", .flag = &default_cdrom },
281 { .driver = "virtio-serial-pci", .flag = &default_virtcon },
282 { .driver = "virtio-serial-s390", .flag = &default_virtcon },
283 { .driver = "virtio-serial", .flag = &default_virtcon },
284 { .driver = "VGA", .flag = &default_vga },
285 { .driver = "cirrus-vga", .flag = &default_vga },
286 { .driver = "vmware-svga", .flag = &default_vga },
289 static int default_driver_check(QemuOpts *opts, void *opaque)
291 const char *driver = qemu_opt_get(opts, "driver");
292 int i;
294 if (!driver)
295 return 0;
296 for (i = 0; i < ARRAY_SIZE(default_list); i++) {
297 if (strcmp(default_list[i].driver, driver) != 0)
298 continue;
299 *(default_list[i].flag) = 0;
301 return 0;
304 /***********************************************************/
305 /* real time host monotonic timer */
307 /***********************************************************/
308 /* host time/date access */
309 void qemu_get_timedate(struct tm *tm, int offset)
311 time_t ti;
312 struct tm *ret;
314 time(&ti);
315 ti += offset;
316 if (rtc_date_offset == -1) {
317 if (rtc_utc)
318 ret = gmtime(&ti);
319 else
320 ret = localtime(&ti);
321 } else {
322 ti -= rtc_date_offset;
323 ret = gmtime(&ti);
326 memcpy(tm, ret, sizeof(struct tm));
329 int qemu_timedate_diff(struct tm *tm)
331 time_t seconds;
333 if (rtc_date_offset == -1)
334 if (rtc_utc)
335 seconds = mktimegm(tm);
336 else
337 seconds = mktime(tm);
338 else
339 seconds = mktimegm(tm) + rtc_date_offset;
341 return seconds - time(NULL);
344 void rtc_change_mon_event(struct tm *tm)
346 QObject *data;
348 data = qobject_from_jsonf("{ 'offset': %d }", qemu_timedate_diff(tm));
349 monitor_protocol_event(QEVENT_RTC_CHANGE, data);
350 qobject_decref(data);
353 static void configure_rtc_date_offset(const char *startdate, int legacy)
355 time_t rtc_start_date;
356 struct tm tm;
358 if (!strcmp(startdate, "now") && legacy) {
359 rtc_date_offset = -1;
360 } else {
361 if (sscanf(startdate, "%d-%d-%dT%d:%d:%d",
362 &tm.tm_year,
363 &tm.tm_mon,
364 &tm.tm_mday,
365 &tm.tm_hour,
366 &tm.tm_min,
367 &tm.tm_sec) == 6) {
368 /* OK */
369 } else if (sscanf(startdate, "%d-%d-%d",
370 &tm.tm_year,
371 &tm.tm_mon,
372 &tm.tm_mday) == 3) {
373 tm.tm_hour = 0;
374 tm.tm_min = 0;
375 tm.tm_sec = 0;
376 } else {
377 goto date_fail;
379 tm.tm_year -= 1900;
380 tm.tm_mon--;
381 rtc_start_date = mktimegm(&tm);
382 if (rtc_start_date == -1) {
383 date_fail:
384 fprintf(stderr, "Invalid date format. Valid formats are:\n"
385 "'2006-06-17T16:01:21' or '2006-06-17'\n");
386 exit(1);
388 rtc_date_offset = time(NULL) - rtc_start_date;
392 static void configure_rtc(QemuOpts *opts)
394 const char *value;
396 value = qemu_opt_get(opts, "base");
397 if (value) {
398 if (!strcmp(value, "utc")) {
399 rtc_utc = 1;
400 } else if (!strcmp(value, "localtime")) {
401 rtc_utc = 0;
402 } else {
403 configure_rtc_date_offset(value, 0);
406 value = qemu_opt_get(opts, "clock");
407 if (value) {
408 if (!strcmp(value, "host")) {
409 rtc_clock = host_clock;
410 } else if (!strcmp(value, "vm")) {
411 rtc_clock = vm_clock;
412 } else {
413 fprintf(stderr, "qemu: invalid option value '%s'\n", value);
414 exit(1);
417 value = qemu_opt_get(opts, "driftfix");
418 if (value) {
419 if (!strcmp(value, "slew")) {
420 rtc_td_hack = 1;
421 } else if (!strcmp(value, "none")) {
422 rtc_td_hack = 0;
423 } else {
424 fprintf(stderr, "qemu: invalid option value '%s'\n", value);
425 exit(1);
430 /***********************************************************/
431 /* Bluetooth support */
432 static int nb_hcis;
433 static int cur_hci;
434 static struct HCIInfo *hci_table[MAX_NICS];
436 static struct bt_vlan_s {
437 struct bt_scatternet_s net;
438 int id;
439 struct bt_vlan_s *next;
440 } *first_bt_vlan;
442 /* find or alloc a new bluetooth "VLAN" */
443 static struct bt_scatternet_s *qemu_find_bt_vlan(int id)
445 struct bt_vlan_s **pvlan, *vlan;
446 for (vlan = first_bt_vlan; vlan != NULL; vlan = vlan->next) {
447 if (vlan->id == id)
448 return &vlan->net;
450 vlan = qemu_mallocz(sizeof(struct bt_vlan_s));
451 vlan->id = id;
452 pvlan = &first_bt_vlan;
453 while (*pvlan != NULL)
454 pvlan = &(*pvlan)->next;
455 *pvlan = vlan;
456 return &vlan->net;
459 static void null_hci_send(struct HCIInfo *hci, const uint8_t *data, int len)
463 static int null_hci_addr_set(struct HCIInfo *hci, const uint8_t *bd_addr)
465 return -ENOTSUP;
468 static struct HCIInfo null_hci = {
469 .cmd_send = null_hci_send,
470 .sco_send = null_hci_send,
471 .acl_send = null_hci_send,
472 .bdaddr_set = null_hci_addr_set,
475 struct HCIInfo *qemu_next_hci(void)
477 if (cur_hci == nb_hcis)
478 return &null_hci;
480 return hci_table[cur_hci++];
483 static struct HCIInfo *hci_init(const char *str)
485 char *endp;
486 struct bt_scatternet_s *vlan = 0;
488 if (!strcmp(str, "null"))
489 /* null */
490 return &null_hci;
491 else if (!strncmp(str, "host", 4) && (str[4] == '\0' || str[4] == ':'))
492 /* host[:hciN] */
493 return bt_host_hci(str[4] ? str + 5 : "hci0");
494 else if (!strncmp(str, "hci", 3)) {
495 /* hci[,vlan=n] */
496 if (str[3]) {
497 if (!strncmp(str + 3, ",vlan=", 6)) {
498 vlan = qemu_find_bt_vlan(strtol(str + 9, &endp, 0));
499 if (*endp)
500 vlan = 0;
502 } else
503 vlan = qemu_find_bt_vlan(0);
504 if (vlan)
505 return bt_new_hci(vlan);
508 fprintf(stderr, "qemu: Unknown bluetooth HCI `%s'.\n", str);
510 return 0;
513 static int bt_hci_parse(const char *str)
515 struct HCIInfo *hci;
516 bdaddr_t bdaddr;
518 if (nb_hcis >= MAX_NICS) {
519 fprintf(stderr, "qemu: Too many bluetooth HCIs (max %i).\n", MAX_NICS);
520 return -1;
523 hci = hci_init(str);
524 if (!hci)
525 return -1;
527 bdaddr.b[0] = 0x52;
528 bdaddr.b[1] = 0x54;
529 bdaddr.b[2] = 0x00;
530 bdaddr.b[3] = 0x12;
531 bdaddr.b[4] = 0x34;
532 bdaddr.b[5] = 0x56 + nb_hcis;
533 hci->bdaddr_set(hci, bdaddr.b);
535 hci_table[nb_hcis++] = hci;
537 return 0;
540 static void bt_vhci_add(int vlan_id)
542 struct bt_scatternet_s *vlan = qemu_find_bt_vlan(vlan_id);
544 if (!vlan->slave)
545 fprintf(stderr, "qemu: warning: adding a VHCI to "
546 "an empty scatternet %i\n", vlan_id);
548 bt_vhci_init(bt_new_hci(vlan));
551 static struct bt_device_s *bt_device_add(const char *opt)
553 struct bt_scatternet_s *vlan;
554 int vlan_id = 0;
555 char *endp = strstr(opt, ",vlan=");
556 int len = (endp ? endp - opt : strlen(opt)) + 1;
557 char devname[10];
559 pstrcpy(devname, MIN(sizeof(devname), len), opt);
561 if (endp) {
562 vlan_id = strtol(endp + 6, &endp, 0);
563 if (*endp) {
564 fprintf(stderr, "qemu: unrecognised bluetooth vlan Id\n");
565 return 0;
569 vlan = qemu_find_bt_vlan(vlan_id);
571 if (!vlan->slave)
572 fprintf(stderr, "qemu: warning: adding a slave device to "
573 "an empty scatternet %i\n", vlan_id);
575 if (!strcmp(devname, "keyboard"))
576 return bt_keyboard_init(vlan);
578 fprintf(stderr, "qemu: unsupported bluetooth device `%s'\n", devname);
579 return 0;
582 static int bt_parse(const char *opt)
584 const char *endp, *p;
585 int vlan;
587 if (strstart(opt, "hci", &endp)) {
588 if (!*endp || *endp == ',') {
589 if (*endp)
590 if (!strstart(endp, ",vlan=", 0))
591 opt = endp + 1;
593 return bt_hci_parse(opt);
595 } else if (strstart(opt, "vhci", &endp)) {
596 if (!*endp || *endp == ',') {
597 if (*endp) {
598 if (strstart(endp, ",vlan=", &p)) {
599 vlan = strtol(p, (char **) &endp, 0);
600 if (*endp) {
601 fprintf(stderr, "qemu: bad scatternet '%s'\n", p);
602 return 1;
604 } else {
605 fprintf(stderr, "qemu: bad parameter '%s'\n", endp + 1);
606 return 1;
608 } else
609 vlan = 0;
611 bt_vhci_add(vlan);
612 return 0;
614 } else if (strstart(opt, "device:", &endp))
615 return !bt_device_add(endp);
617 fprintf(stderr, "qemu: bad bluetooth parameter '%s'\n", opt);
618 return 1;
621 /***********************************************************/
622 /* QEMU Block devices */
624 #define HD_OPTS "media=disk"
625 #define CDROM_OPTS "media=cdrom"
626 #define FD_OPTS ""
627 #define PFLASH_OPTS ""
628 #define MTD_OPTS ""
629 #define SD_OPTS ""
631 static int drive_init_func(QemuOpts *opts, void *opaque)
633 int *use_scsi = opaque;
635 return drive_init(opts, *use_scsi) == NULL;
638 static int drive_enable_snapshot(QemuOpts *opts, void *opaque)
640 if (NULL == qemu_opt_get(opts, "snapshot")) {
641 qemu_opt_set(opts, "snapshot", "on");
643 return 0;
646 static void default_drive(int enable, int snapshot, int use_scsi,
647 BlockInterfaceType type, int index,
648 const char *optstr)
650 QemuOpts *opts;
652 if (type == IF_DEFAULT) {
653 type = use_scsi ? IF_SCSI : IF_IDE;
656 if (!enable || drive_get_by_index(type, index)) {
657 return;
660 opts = drive_add(type, index, NULL, optstr);
661 if (snapshot) {
662 drive_enable_snapshot(opts, NULL);
664 if (!drive_init(opts, use_scsi)) {
665 exit(1);
669 void qemu_register_boot_set(QEMUBootSetHandler *func, void *opaque)
671 boot_set_handler = func;
672 boot_set_opaque = opaque;
675 int qemu_boot_set(const char *boot_devices)
677 if (!boot_set_handler) {
678 return -EINVAL;
680 return boot_set_handler(boot_set_opaque, boot_devices);
683 static void validate_bootdevices(char *devices)
685 /* We just do some generic consistency checks */
686 const char *p;
687 int bitmap = 0;
689 for (p = devices; *p != '\0'; p++) {
690 /* Allowed boot devices are:
691 * a-b: floppy disk drives
692 * c-f: IDE disk drives
693 * g-m: machine implementation dependant drives
694 * n-p: network devices
695 * It's up to each machine implementation to check if the given boot
696 * devices match the actual hardware implementation and firmware
697 * features.
699 if (*p < 'a' || *p > 'p') {
700 fprintf(stderr, "Invalid boot device '%c'\n", *p);
701 exit(1);
703 if (bitmap & (1 << (*p - 'a'))) {
704 fprintf(stderr, "Boot device '%c' was given twice\n", *p);
705 exit(1);
707 bitmap |= 1 << (*p - 'a');
711 static void restore_boot_devices(void *opaque)
713 char *standard_boot_devices = opaque;
714 static int first = 1;
716 /* Restore boot order and remove ourselves after the first boot */
717 if (first) {
718 first = 0;
719 return;
722 qemu_boot_set(standard_boot_devices);
724 qemu_unregister_reset(restore_boot_devices, standard_boot_devices);
725 qemu_free(standard_boot_devices);
728 void add_boot_device_path(int32_t bootindex, DeviceState *dev,
729 const char *suffix)
731 FWBootEntry *node, *i;
733 if (bootindex < 0) {
734 return;
737 assert(dev != NULL || suffix != NULL);
739 node = qemu_mallocz(sizeof(FWBootEntry));
740 node->bootindex = bootindex;
741 node->suffix = suffix ? qemu_strdup(suffix) : NULL;
742 node->dev = dev;
744 QTAILQ_FOREACH(i, &fw_boot_order, link) {
745 if (i->bootindex == bootindex) {
746 fprintf(stderr, "Two devices with same boot index %d\n", bootindex);
747 exit(1);
748 } else if (i->bootindex < bootindex) {
749 continue;
751 QTAILQ_INSERT_BEFORE(i, node, link);
752 return;
754 QTAILQ_INSERT_TAIL(&fw_boot_order, node, link);
758 * This function returns null terminated string that consist of new line
759 * separated device pathes.
761 * memory pointed by "size" is assigned total length of the array in bytes
764 char *get_boot_devices_list(uint32_t *size)
766 FWBootEntry *i;
767 uint32_t total = 0;
768 char *list = NULL;
770 QTAILQ_FOREACH(i, &fw_boot_order, link) {
771 char *devpath = NULL, *bootpath;
772 int len;
774 if (i->dev) {
775 devpath = qdev_get_fw_dev_path(i->dev);
776 assert(devpath);
779 if (i->suffix && devpath) {
780 size_t bootpathlen = strlen(devpath) + strlen(i->suffix) + 1;
782 bootpath = qemu_malloc(bootpathlen);
783 snprintf(bootpath, bootpathlen, "%s%s", devpath, i->suffix);
784 qemu_free(devpath);
785 } else if (devpath) {
786 bootpath = devpath;
787 } else {
788 bootpath = qemu_strdup(i->suffix);
789 assert(bootpath);
792 if (total) {
793 list[total-1] = '\n';
795 len = strlen(bootpath) + 1;
796 list = qemu_realloc(list, total + len);
797 memcpy(&list[total], bootpath, len);
798 total += len;
799 qemu_free(bootpath);
802 *size = total;
804 return list;
807 static void numa_add(const char *optarg)
809 char option[128];
810 char *endptr;
811 unsigned long long value, endvalue;
812 int nodenr;
814 optarg = get_opt_name(option, 128, optarg, ',') + 1;
815 if (!strcmp(option, "node")) {
816 if (get_param_value(option, 128, "nodeid", optarg) == 0) {
817 nodenr = nb_numa_nodes;
818 } else {
819 nodenr = strtoull(option, NULL, 10);
822 if (get_param_value(option, 128, "mem", optarg) == 0) {
823 node_mem[nodenr] = 0;
824 } else {
825 int64_t sval;
826 sval = strtosz(option, NULL);
827 if (sval < 0) {
828 fprintf(stderr, "qemu: invalid numa mem size: %s\n", optarg);
829 exit(1);
831 node_mem[nodenr] = sval;
833 if (get_param_value(option, 128, "cpus", optarg) == 0) {
834 node_cpumask[nodenr] = 0;
835 } else {
836 value = strtoull(option, &endptr, 10);
837 if (value >= 64) {
838 value = 63;
839 fprintf(stderr, "only 64 CPUs in NUMA mode supported.\n");
840 } else {
841 if (*endptr == '-') {
842 endvalue = strtoull(endptr+1, &endptr, 10);
843 if (endvalue >= 63) {
844 endvalue = 62;
845 fprintf(stderr,
846 "only 63 CPUs in NUMA mode supported.\n");
848 value = (2ULL << endvalue) - (1ULL << value);
849 } else {
850 value = 1ULL << value;
853 node_cpumask[nodenr] = value;
855 nb_numa_nodes++;
857 return;
860 static void smp_parse(const char *optarg)
862 int smp, sockets = 0, threads = 0, cores = 0;
863 char *endptr;
864 char option[128];
866 smp = strtoul(optarg, &endptr, 10);
867 if (endptr != optarg) {
868 if (*endptr == ',') {
869 endptr++;
872 if (get_param_value(option, 128, "sockets", endptr) != 0)
873 sockets = strtoull(option, NULL, 10);
874 if (get_param_value(option, 128, "cores", endptr) != 0)
875 cores = strtoull(option, NULL, 10);
876 if (get_param_value(option, 128, "threads", endptr) != 0)
877 threads = strtoull(option, NULL, 10);
878 if (get_param_value(option, 128, "maxcpus", endptr) != 0)
879 max_cpus = strtoull(option, NULL, 10);
881 /* compute missing values, prefer sockets over cores over threads */
882 if (smp == 0 || sockets == 0) {
883 sockets = sockets > 0 ? sockets : 1;
884 cores = cores > 0 ? cores : 1;
885 threads = threads > 0 ? threads : 1;
886 if (smp == 0) {
887 smp = cores * threads * sockets;
889 } else {
890 if (cores == 0) {
891 threads = threads > 0 ? threads : 1;
892 cores = smp / (sockets * threads);
893 } else {
894 threads = smp / (cores * sockets);
897 smp_cpus = smp;
898 smp_cores = cores > 0 ? cores : 1;
899 smp_threads = threads > 0 ? threads : 1;
900 if (max_cpus == 0)
901 max_cpus = smp_cpus;
904 /***********************************************************/
905 /* USB devices */
907 static int usb_device_add(const char *devname)
909 const char *p;
910 USBDevice *dev = NULL;
912 if (!usb_enabled)
913 return -1;
915 /* drivers with .usbdevice_name entry in USBDeviceInfo */
916 dev = usbdevice_create(devname);
917 if (dev)
918 goto done;
920 /* the other ones */
921 if (strstart(devname, "host:", &p)) {
922 dev = usb_host_device_open(p);
923 } else if (!strcmp(devname, "bt") || strstart(devname, "bt:", &p)) {
924 dev = usb_bt_init(devname[2] ? hci_init(p) :
925 bt_new_hci(qemu_find_bt_vlan(0)));
926 } else {
927 return -1;
929 if (!dev)
930 return -1;
932 done:
933 return 0;
936 static int usb_device_del(const char *devname)
938 int bus_num, addr;
939 const char *p;
941 if (strstart(devname, "host:", &p))
942 return usb_host_device_close(p);
944 if (!usb_enabled)
945 return -1;
947 p = strchr(devname, '.');
948 if (!p)
949 return -1;
950 bus_num = strtoul(devname, NULL, 0);
951 addr = strtoul(p + 1, NULL, 0);
953 return usb_device_delete_addr(bus_num, addr);
956 static int usb_parse(const char *cmdline)
958 int r;
959 r = usb_device_add(cmdline);
960 if (r < 0) {
961 fprintf(stderr, "qemu: could not add USB device '%s'\n", cmdline);
963 return r;
966 void do_usb_add(Monitor *mon, const QDict *qdict)
968 const char *devname = qdict_get_str(qdict, "devname");
969 if (usb_device_add(devname) < 0) {
970 error_report("could not add USB device '%s'", devname);
974 void do_usb_del(Monitor *mon, const QDict *qdict)
976 const char *devname = qdict_get_str(qdict, "devname");
977 if (usb_device_del(devname) < 0) {
978 error_report("could not delete USB device '%s'", devname);
982 /***********************************************************/
983 /* PCMCIA/Cardbus */
985 static struct pcmcia_socket_entry_s {
986 PCMCIASocket *socket;
987 struct pcmcia_socket_entry_s *next;
988 } *pcmcia_sockets = 0;
990 void pcmcia_socket_register(PCMCIASocket *socket)
992 struct pcmcia_socket_entry_s *entry;
994 entry = qemu_malloc(sizeof(struct pcmcia_socket_entry_s));
995 entry->socket = socket;
996 entry->next = pcmcia_sockets;
997 pcmcia_sockets = entry;
1000 void pcmcia_socket_unregister(PCMCIASocket *socket)
1002 struct pcmcia_socket_entry_s *entry, **ptr;
1004 ptr = &pcmcia_sockets;
1005 for (entry = *ptr; entry; ptr = &entry->next, entry = *ptr)
1006 if (entry->socket == socket) {
1007 *ptr = entry->next;
1008 qemu_free(entry);
1012 void pcmcia_info(Monitor *mon)
1014 struct pcmcia_socket_entry_s *iter;
1016 if (!pcmcia_sockets)
1017 monitor_printf(mon, "No PCMCIA sockets\n");
1019 for (iter = pcmcia_sockets; iter; iter = iter->next)
1020 monitor_printf(mon, "%s: %s\n", iter->socket->slot_string,
1021 iter->socket->attached ? iter->socket->card_string :
1022 "Empty");
1025 /***********************************************************/
1026 /* I/O handling */
1028 typedef struct IOHandlerRecord {
1029 int fd;
1030 IOCanReadHandler *fd_read_poll;
1031 IOHandler *fd_read;
1032 IOHandler *fd_write;
1033 int deleted;
1034 void *opaque;
1035 /* temporary data */
1036 struct pollfd *ufd;
1037 QLIST_ENTRY(IOHandlerRecord) next;
1038 } IOHandlerRecord;
1040 static QLIST_HEAD(, IOHandlerRecord) io_handlers =
1041 QLIST_HEAD_INITIALIZER(io_handlers);
1044 /* XXX: fd_read_poll should be suppressed, but an API change is
1045 necessary in the character devices to suppress fd_can_read(). */
1046 int qemu_set_fd_handler2(int fd,
1047 IOCanReadHandler *fd_read_poll,
1048 IOHandler *fd_read,
1049 IOHandler *fd_write,
1050 void *opaque)
1052 IOHandlerRecord *ioh;
1054 if (!fd_read && !fd_write) {
1055 QLIST_FOREACH(ioh, &io_handlers, next) {
1056 if (ioh->fd == fd) {
1057 ioh->deleted = 1;
1058 break;
1061 } else {
1062 QLIST_FOREACH(ioh, &io_handlers, next) {
1063 if (ioh->fd == fd)
1064 goto found;
1066 ioh = qemu_mallocz(sizeof(IOHandlerRecord));
1067 QLIST_INSERT_HEAD(&io_handlers, ioh, next);
1068 found:
1069 ioh->fd = fd;
1070 ioh->fd_read_poll = fd_read_poll;
1071 ioh->fd_read = fd_read;
1072 ioh->fd_write = fd_write;
1073 ioh->opaque = opaque;
1074 ioh->deleted = 0;
1076 return 0;
1079 int qemu_set_fd_handler(int fd,
1080 IOHandler *fd_read,
1081 IOHandler *fd_write,
1082 void *opaque)
1084 return qemu_set_fd_handler2(fd, NULL, fd_read, fd_write, opaque);
1087 /***********************************************************/
1088 /* machine registration */
1090 static QEMUMachine *first_machine = NULL;
1091 QEMUMachine *current_machine = NULL;
1093 int qemu_register_machine(QEMUMachine *m)
1095 QEMUMachine **pm;
1096 pm = &first_machine;
1097 while (*pm != NULL)
1098 pm = &(*pm)->next;
1099 m->next = NULL;
1100 *pm = m;
1101 return 0;
1104 static QEMUMachine *find_machine(const char *name)
1106 QEMUMachine *m;
1108 for(m = first_machine; m != NULL; m = m->next) {
1109 if (!strcmp(m->name, name))
1110 return m;
1111 if (m->alias && !strcmp(m->alias, name))
1112 return m;
1114 return NULL;
1117 static QEMUMachine *find_default_machine(void)
1119 QEMUMachine *m;
1121 for(m = first_machine; m != NULL; m = m->next) {
1122 if (m->is_default) {
1123 return m;
1126 return NULL;
1129 /***********************************************************/
1130 /* main execution loop */
1132 static void gui_update(void *opaque)
1134 uint64_t interval = GUI_REFRESH_INTERVAL;
1135 DisplayState *ds = opaque;
1136 DisplayChangeListener *dcl = ds->listeners;
1138 qemu_flush_coalesced_mmio_buffer();
1139 dpy_refresh(ds);
1141 while (dcl != NULL) {
1142 if (dcl->gui_timer_interval &&
1143 dcl->gui_timer_interval < interval)
1144 interval = dcl->gui_timer_interval;
1145 dcl = dcl->next;
1147 qemu_mod_timer(ds->gui_timer, interval + qemu_get_clock_ms(rt_clock));
1150 static void nographic_update(void *opaque)
1152 uint64_t interval = GUI_REFRESH_INTERVAL;
1154 qemu_flush_coalesced_mmio_buffer();
1155 qemu_mod_timer(nographic_timer, interval + qemu_get_clock_ms(rt_clock));
1158 struct vm_change_state_entry {
1159 VMChangeStateHandler *cb;
1160 void *opaque;
1161 QLIST_ENTRY (vm_change_state_entry) entries;
1164 static QLIST_HEAD(vm_change_state_head, vm_change_state_entry) vm_change_state_head;
1166 VMChangeStateEntry *qemu_add_vm_change_state_handler(VMChangeStateHandler *cb,
1167 void *opaque)
1169 VMChangeStateEntry *e;
1171 e = qemu_mallocz(sizeof (*e));
1173 e->cb = cb;
1174 e->opaque = opaque;
1175 QLIST_INSERT_HEAD(&vm_change_state_head, e, entries);
1176 return e;
1179 void qemu_del_vm_change_state_handler(VMChangeStateEntry *e)
1181 QLIST_REMOVE (e, entries);
1182 qemu_free (e);
1185 void vm_state_notify(int running, int reason)
1187 VMChangeStateEntry *e;
1189 trace_vm_state_notify(running, reason);
1191 for (e = vm_change_state_head.lh_first; e; e = e->entries.le_next) {
1192 e->cb(e->opaque, running, reason);
1196 void vm_start(void)
1198 if (!vm_running) {
1199 cpu_enable_ticks();
1200 vm_running = 1;
1201 vm_state_notify(1, 0);
1202 resume_all_vcpus();
1203 monitor_protocol_event(QEVENT_RESUME, NULL);
1207 /* reset/shutdown handler */
1209 typedef struct QEMUResetEntry {
1210 QTAILQ_ENTRY(QEMUResetEntry) entry;
1211 QEMUResetHandler *func;
1212 void *opaque;
1213 } QEMUResetEntry;
1215 static QTAILQ_HEAD(reset_handlers, QEMUResetEntry) reset_handlers =
1216 QTAILQ_HEAD_INITIALIZER(reset_handlers);
1217 static int reset_requested;
1218 static int shutdown_requested;
1219 static int powerdown_requested;
1220 static int debug_requested;
1221 static int vmstop_requested;
1223 int qemu_shutdown_requested(void)
1225 int r = shutdown_requested;
1226 shutdown_requested = 0;
1227 return r;
1230 int qemu_reset_requested(void)
1232 int r = reset_requested;
1233 reset_requested = 0;
1234 return r;
1237 int qemu_powerdown_requested(void)
1239 int r = powerdown_requested;
1240 powerdown_requested = 0;
1241 return r;
1244 static int qemu_debug_requested(void)
1246 int r = debug_requested;
1247 debug_requested = 0;
1248 return r;
1251 static int qemu_vmstop_requested(void)
1253 int r = vmstop_requested;
1254 vmstop_requested = 0;
1255 return r;
1258 void qemu_register_reset(QEMUResetHandler *func, void *opaque)
1260 QEMUResetEntry *re = qemu_mallocz(sizeof(QEMUResetEntry));
1262 re->func = func;
1263 re->opaque = opaque;
1264 QTAILQ_INSERT_TAIL(&reset_handlers, re, entry);
1267 void qemu_unregister_reset(QEMUResetHandler *func, void *opaque)
1269 QEMUResetEntry *re;
1271 QTAILQ_FOREACH(re, &reset_handlers, entry) {
1272 if (re->func == func && re->opaque == opaque) {
1273 QTAILQ_REMOVE(&reset_handlers, re, entry);
1274 qemu_free(re);
1275 return;
1280 void qemu_system_reset(void)
1282 QEMUResetEntry *re, *nre;
1284 /* reset all devices */
1285 QTAILQ_FOREACH_SAFE(re, &reset_handlers, entry, nre) {
1286 re->func(re->opaque);
1288 monitor_protocol_event(QEVENT_RESET, NULL);
1289 cpu_synchronize_all_post_reset();
1292 void qemu_system_reset_request(void)
1294 if (no_reboot) {
1295 shutdown_requested = 1;
1296 } else {
1297 reset_requested = 1;
1299 cpu_stop_current();
1300 qemu_notify_event();
1303 void qemu_system_shutdown_request(void)
1305 shutdown_requested = 1;
1306 qemu_notify_event();
1309 void qemu_system_powerdown_request(void)
1311 powerdown_requested = 1;
1312 qemu_notify_event();
1315 void qemu_system_debug_request(void)
1317 debug_requested = 1;
1318 qemu_notify_event();
1321 void qemu_system_vmstop_request(int reason)
1323 vmstop_requested = reason;
1324 qemu_notify_event();
1327 void main_loop_wait(int nonblocking)
1329 IOHandlerRecord *ioh;
1330 fd_set rfds, wfds, xfds;
1331 int ret, nfds;
1332 struct timeval tv;
1333 int timeout;
1335 if (nonblocking)
1336 timeout = 0;
1337 else {
1338 timeout = qemu_calculate_timeout();
1339 qemu_bh_update_timeout(&timeout);
1342 os_host_main_loop_wait(&timeout);
1344 /* poll any events */
1345 /* XXX: separate device handlers from system ones */
1346 nfds = -1;
1347 FD_ZERO(&rfds);
1348 FD_ZERO(&wfds);
1349 FD_ZERO(&xfds);
1350 QLIST_FOREACH(ioh, &io_handlers, next) {
1351 if (ioh->deleted)
1352 continue;
1353 if (ioh->fd_read &&
1354 (!ioh->fd_read_poll ||
1355 ioh->fd_read_poll(ioh->opaque) != 0)) {
1356 FD_SET(ioh->fd, &rfds);
1357 if (ioh->fd > nfds)
1358 nfds = ioh->fd;
1360 if (ioh->fd_write) {
1361 FD_SET(ioh->fd, &wfds);
1362 if (ioh->fd > nfds)
1363 nfds = ioh->fd;
1367 tv.tv_sec = timeout / 1000;
1368 tv.tv_usec = (timeout % 1000) * 1000;
1370 slirp_select_fill(&nfds, &rfds, &wfds, &xfds);
1372 qemu_mutex_unlock_iothread();
1373 ret = select(nfds + 1, &rfds, &wfds, &xfds, &tv);
1374 qemu_mutex_lock_iothread();
1375 if (ret > 0) {
1376 IOHandlerRecord *pioh;
1378 QLIST_FOREACH_SAFE(ioh, &io_handlers, next, pioh) {
1379 if (!ioh->deleted && ioh->fd_read && FD_ISSET(ioh->fd, &rfds)) {
1380 ioh->fd_read(ioh->opaque);
1382 if (!ioh->deleted && ioh->fd_write && FD_ISSET(ioh->fd, &wfds)) {
1383 ioh->fd_write(ioh->opaque);
1386 /* Do this last in case read/write handlers marked it for deletion */
1387 if (ioh->deleted) {
1388 QLIST_REMOVE(ioh, next);
1389 qemu_free(ioh);
1394 slirp_select_poll(&rfds, &wfds, &xfds, (ret < 0));
1396 qemu_run_all_timers();
1398 /* Check bottom-halves last in case any of the earlier events triggered
1399 them. */
1400 qemu_bh_poll();
1404 #ifndef CONFIG_IOTHREAD
1405 static int vm_request_pending(void)
1407 return powerdown_requested ||
1408 reset_requested ||
1409 shutdown_requested ||
1410 debug_requested ||
1411 vmstop_requested;
1413 #endif
1415 qemu_irq qemu_system_powerdown;
1417 static void main_loop(void)
1419 bool nonblocking = false;
1420 #ifdef CONFIG_PROFILER
1421 int64_t ti;
1422 #endif
1423 int r;
1425 qemu_main_loop_start();
1427 for (;;) {
1428 #ifndef CONFIG_IOTHREAD
1429 nonblocking = cpu_exec_all();
1430 if (vm_request_pending()) {
1431 nonblocking = true;
1433 #endif
1434 #ifdef CONFIG_PROFILER
1435 ti = profile_getclock();
1436 #endif
1437 main_loop_wait(nonblocking);
1438 #ifdef CONFIG_PROFILER
1439 dev_time += profile_getclock() - ti;
1440 #endif
1442 if (qemu_debug_requested()) {
1443 vm_stop(VMSTOP_DEBUG);
1445 if (qemu_shutdown_requested()) {
1446 monitor_protocol_event(QEVENT_SHUTDOWN, NULL);
1447 if (no_shutdown) {
1448 vm_stop(VMSTOP_SHUTDOWN);
1449 no_shutdown = 0;
1450 } else
1451 break;
1453 if (qemu_reset_requested()) {
1454 pause_all_vcpus();
1455 cpu_synchronize_all_states();
1456 qemu_system_reset();
1457 resume_all_vcpus();
1459 if (qemu_powerdown_requested()) {
1460 monitor_protocol_event(QEVENT_POWERDOWN, NULL);
1461 qemu_irq_raise(qemu_system_powerdown);
1463 if ((r = qemu_vmstop_requested())) {
1464 vm_stop(r);
1467 bdrv_close_all();
1468 pause_all_vcpus();
1471 static void version(void)
1473 printf("QEMU emulator version " QEMU_VERSION QEMU_PKGVERSION ", Copyright (c) 2003-2008 Fabrice Bellard\n");
1476 static void help(int exitcode)
1478 const char *options_help =
1479 #define DEF(option, opt_arg, opt_enum, opt_help, arch_mask) \
1480 opt_help
1481 #define DEFHEADING(text) stringify(text) "\n"
1482 #include "qemu-options.def"
1483 #undef DEF
1484 #undef DEFHEADING
1485 #undef GEN_DOCS
1487 version();
1488 printf("usage: %s [options] [disk_image]\n"
1489 "\n"
1490 "'disk_image' is a raw hard disk image for IDE hard disk 0\n"
1491 "\n"
1492 "%s\n"
1493 "During emulation, the following keys are useful:\n"
1494 "ctrl-alt-f toggle full screen\n"
1495 "ctrl-alt-n switch to virtual console 'n'\n"
1496 "ctrl-alt toggle mouse and keyboard grab\n"
1497 "\n"
1498 "When using -nographic, press 'ctrl-a h' to get some help.\n",
1499 "qemu",
1500 options_help);
1501 exit(exitcode);
1504 #define HAS_ARG 0x0001
1506 typedef struct QEMUOption {
1507 const char *name;
1508 int flags;
1509 int index;
1510 uint32_t arch_mask;
1511 } QEMUOption;
1513 static const QEMUOption qemu_options[] = {
1514 { "h", 0, QEMU_OPTION_h, QEMU_ARCH_ALL },
1515 #define DEF(option, opt_arg, opt_enum, opt_help, arch_mask) \
1516 { option, opt_arg, opt_enum, arch_mask },
1517 #define DEFHEADING(text)
1518 #include "qemu-options.def"
1519 #undef DEF
1520 #undef DEFHEADING
1521 #undef GEN_DOCS
1522 { NULL },
1524 static void select_vgahw (const char *p)
1526 const char *opts;
1528 default_vga = 0;
1529 vga_interface_type = VGA_NONE;
1530 if (strstart(p, "std", &opts)) {
1531 vga_interface_type = VGA_STD;
1532 } else if (strstart(p, "cirrus", &opts)) {
1533 vga_interface_type = VGA_CIRRUS;
1534 } else if (strstart(p, "vmware", &opts)) {
1535 vga_interface_type = VGA_VMWARE;
1536 } else if (strstart(p, "xenfb", &opts)) {
1537 vga_interface_type = VGA_XENFB;
1538 } else if (strstart(p, "qxl", &opts)) {
1539 vga_interface_type = VGA_QXL;
1540 } else if (!strstart(p, "none", &opts)) {
1541 invalid_vga:
1542 fprintf(stderr, "Unknown vga type: %s\n", p);
1543 exit(1);
1545 while (*opts) {
1546 const char *nextopt;
1548 if (strstart(opts, ",retrace=", &nextopt)) {
1549 opts = nextopt;
1550 if (strstart(opts, "dumb", &nextopt))
1551 vga_retrace_method = VGA_RETRACE_DUMB;
1552 else if (strstart(opts, "precise", &nextopt))
1553 vga_retrace_method = VGA_RETRACE_PRECISE;
1554 else goto invalid_vga;
1555 } else goto invalid_vga;
1556 opts = nextopt;
1560 static DisplayType select_display(const char *p)
1562 const char *opts;
1563 DisplayType display = DT_DEFAULT;
1565 if (strstart(p, "sdl", &opts)) {
1566 #ifdef CONFIG_SDL
1567 display = DT_SDL;
1568 while (*opts) {
1569 const char *nextopt;
1571 if (strstart(opts, ",frame=", &nextopt)) {
1572 opts = nextopt;
1573 if (strstart(opts, "on", &nextopt)) {
1574 no_frame = 0;
1575 } else if (strstart(opts, "off", &nextopt)) {
1576 no_frame = 1;
1577 } else {
1578 goto invalid_display;
1580 } else if (strstart(opts, ",alt_grab=", &nextopt)) {
1581 opts = nextopt;
1582 if (strstart(opts, "on", &nextopt)) {
1583 alt_grab = 1;
1584 } else if (strstart(opts, "off", &nextopt)) {
1585 alt_grab = 0;
1586 } else {
1587 goto invalid_display;
1589 } else if (strstart(opts, ",ctrl_grab=", &nextopt)) {
1590 opts = nextopt;
1591 if (strstart(opts, "on", &nextopt)) {
1592 ctrl_grab = 1;
1593 } else if (strstart(opts, "off", &nextopt)) {
1594 ctrl_grab = 0;
1595 } else {
1596 goto invalid_display;
1598 } else if (strstart(opts, ",window_close=", &nextopt)) {
1599 opts = nextopt;
1600 if (strstart(opts, "on", &nextopt)) {
1601 no_quit = 0;
1602 } else if (strstart(opts, "off", &nextopt)) {
1603 no_quit = 1;
1604 } else {
1605 goto invalid_display;
1607 } else {
1608 goto invalid_display;
1610 opts = nextopt;
1612 #else
1613 fprintf(stderr, "SDL support is disabled\n");
1614 exit(1);
1615 #endif
1616 } else if (strstart(p, "vnc", &opts)) {
1617 #ifdef CONFIG_VNC
1618 display_remote++;
1620 if (*opts) {
1621 const char *nextopt;
1623 if (strstart(opts, "=", &nextopt)) {
1624 vnc_display = nextopt;
1627 if (!vnc_display) {
1628 fprintf(stderr, "VNC requires a display argument vnc=<display>\n");
1629 exit(1);
1631 #else
1632 fprintf(stderr, "VNC support is disabled\n");
1633 exit(1);
1634 #endif
1635 } else if (strstart(p, "curses", &opts)) {
1636 #ifdef CONFIG_CURSES
1637 display = DT_CURSES;
1638 #else
1639 fprintf(stderr, "Curses support is disabled\n");
1640 exit(1);
1641 #endif
1642 } else if (strstart(p, "none", &opts)) {
1643 display = DT_NONE;
1644 } else {
1645 invalid_display:
1646 fprintf(stderr, "Unknown display type: %s\n", p);
1647 exit(1);
1650 return display;
1653 static int balloon_parse(const char *arg)
1655 QemuOpts *opts;
1657 if (strcmp(arg, "none") == 0) {
1658 return 0;
1661 if (!strncmp(arg, "virtio", 6)) {
1662 if (arg[6] == ',') {
1663 /* have params -> parse them */
1664 opts = qemu_opts_parse(qemu_find_opts("device"), arg+7, 0);
1665 if (!opts)
1666 return -1;
1667 } else {
1668 /* create empty opts */
1669 opts = qemu_opts_create(qemu_find_opts("device"), NULL, 0);
1671 qemu_opt_set(opts, "driver", "virtio-balloon-pci");
1672 return 0;
1675 return -1;
1678 char *qemu_find_file(int type, const char *name)
1680 int len;
1681 const char *subdir;
1682 char *buf;
1684 /* If name contains path separators then try it as a straight path. */
1685 if ((strchr(name, '/') || strchr(name, '\\'))
1686 && access(name, R_OK) == 0) {
1687 return qemu_strdup(name);
1689 switch (type) {
1690 case QEMU_FILE_TYPE_BIOS:
1691 subdir = "";
1692 break;
1693 case QEMU_FILE_TYPE_KEYMAP:
1694 subdir = "keymaps/";
1695 break;
1696 default:
1697 abort();
1699 len = strlen(data_dir) + strlen(name) + strlen(subdir) + 2;
1700 buf = qemu_mallocz(len);
1701 snprintf(buf, len, "%s/%s%s", data_dir, subdir, name);
1702 if (access(buf, R_OK)) {
1703 qemu_free(buf);
1704 return NULL;
1706 return buf;
1709 static int device_help_func(QemuOpts *opts, void *opaque)
1711 return qdev_device_help(opts);
1714 static int device_init_func(QemuOpts *opts, void *opaque)
1716 DeviceState *dev;
1718 dev = qdev_device_add(opts);
1719 if (!dev)
1720 return -1;
1721 return 0;
1724 static int chardev_init_func(QemuOpts *opts, void *opaque)
1726 CharDriverState *chr;
1728 chr = qemu_chr_open_opts(opts, NULL);
1729 if (!chr)
1730 return -1;
1731 return 0;
1734 #ifdef CONFIG_VIRTFS
1735 static int fsdev_init_func(QemuOpts *opts, void *opaque)
1737 int ret;
1738 ret = qemu_fsdev_add(opts);
1740 return ret;
1742 #endif
1744 static int mon_init_func(QemuOpts *opts, void *opaque)
1746 CharDriverState *chr;
1747 const char *chardev;
1748 const char *mode;
1749 int flags;
1751 mode = qemu_opt_get(opts, "mode");
1752 if (mode == NULL) {
1753 mode = "readline";
1755 if (strcmp(mode, "readline") == 0) {
1756 flags = MONITOR_USE_READLINE;
1757 } else if (strcmp(mode, "control") == 0) {
1758 flags = MONITOR_USE_CONTROL;
1759 } else {
1760 fprintf(stderr, "unknown monitor mode \"%s\"\n", mode);
1761 exit(1);
1764 if (qemu_opt_get_bool(opts, "pretty", 0))
1765 flags |= MONITOR_USE_PRETTY;
1767 if (qemu_opt_get_bool(opts, "default", 0))
1768 flags |= MONITOR_IS_DEFAULT;
1770 chardev = qemu_opt_get(opts, "chardev");
1771 chr = qemu_chr_find(chardev);
1772 if (chr == NULL) {
1773 fprintf(stderr, "chardev \"%s\" not found\n", chardev);
1774 exit(1);
1777 monitor_init(chr, flags);
1778 return 0;
1781 static void monitor_parse(const char *optarg, const char *mode)
1783 static int monitor_device_index = 0;
1784 QemuOpts *opts;
1785 const char *p;
1786 char label[32];
1787 int def = 0;
1789 if (strstart(optarg, "chardev:", &p)) {
1790 snprintf(label, sizeof(label), "%s", p);
1791 } else {
1792 snprintf(label, sizeof(label), "compat_monitor%d",
1793 monitor_device_index);
1794 if (monitor_device_index == 0) {
1795 def = 1;
1797 opts = qemu_chr_parse_compat(label, optarg);
1798 if (!opts) {
1799 fprintf(stderr, "parse error: %s\n", optarg);
1800 exit(1);
1804 opts = qemu_opts_create(qemu_find_opts("mon"), label, 1);
1805 if (!opts) {
1806 fprintf(stderr, "duplicate chardev: %s\n", label);
1807 exit(1);
1809 qemu_opt_set(opts, "mode", mode);
1810 qemu_opt_set(opts, "chardev", label);
1811 if (def)
1812 qemu_opt_set(opts, "default", "on");
1813 monitor_device_index++;
1816 struct device_config {
1817 enum {
1818 DEV_USB, /* -usbdevice */
1819 DEV_BT, /* -bt */
1820 DEV_SERIAL, /* -serial */
1821 DEV_PARALLEL, /* -parallel */
1822 DEV_VIRTCON, /* -virtioconsole */
1823 DEV_DEBUGCON, /* -debugcon */
1824 } type;
1825 const char *cmdline;
1826 QTAILQ_ENTRY(device_config) next;
1828 QTAILQ_HEAD(, device_config) device_configs = QTAILQ_HEAD_INITIALIZER(device_configs);
1830 static void add_device_config(int type, const char *cmdline)
1832 struct device_config *conf;
1834 conf = qemu_mallocz(sizeof(*conf));
1835 conf->type = type;
1836 conf->cmdline = cmdline;
1837 QTAILQ_INSERT_TAIL(&device_configs, conf, next);
1840 static int foreach_device_config(int type, int (*func)(const char *cmdline))
1842 struct device_config *conf;
1843 int rc;
1845 QTAILQ_FOREACH(conf, &device_configs, next) {
1846 if (conf->type != type)
1847 continue;
1848 rc = func(conf->cmdline);
1849 if (0 != rc)
1850 return rc;
1852 return 0;
1855 static int serial_parse(const char *devname)
1857 static int index = 0;
1858 char label[32];
1860 if (strcmp(devname, "none") == 0)
1861 return 0;
1862 if (index == MAX_SERIAL_PORTS) {
1863 fprintf(stderr, "qemu: too many serial ports\n");
1864 exit(1);
1866 snprintf(label, sizeof(label), "serial%d", index);
1867 serial_hds[index] = qemu_chr_open(label, devname, NULL);
1868 if (!serial_hds[index]) {
1869 fprintf(stderr, "qemu: could not open serial device '%s': %s\n",
1870 devname, strerror(errno));
1871 return -1;
1873 index++;
1874 return 0;
1877 static int parallel_parse(const char *devname)
1879 static int index = 0;
1880 char label[32];
1882 if (strcmp(devname, "none") == 0)
1883 return 0;
1884 if (index == MAX_PARALLEL_PORTS) {
1885 fprintf(stderr, "qemu: too many parallel ports\n");
1886 exit(1);
1888 snprintf(label, sizeof(label), "parallel%d", index);
1889 parallel_hds[index] = qemu_chr_open(label, devname, NULL);
1890 if (!parallel_hds[index]) {
1891 fprintf(stderr, "qemu: could not open parallel device '%s': %s\n",
1892 devname, strerror(errno));
1893 return -1;
1895 index++;
1896 return 0;
1899 static int virtcon_parse(const char *devname)
1901 QemuOptsList *device = qemu_find_opts("device");
1902 static int index = 0;
1903 char label[32];
1904 QemuOpts *bus_opts, *dev_opts;
1906 if (strcmp(devname, "none") == 0)
1907 return 0;
1908 if (index == MAX_VIRTIO_CONSOLES) {
1909 fprintf(stderr, "qemu: too many virtio consoles\n");
1910 exit(1);
1913 bus_opts = qemu_opts_create(device, NULL, 0);
1914 qemu_opt_set(bus_opts, "driver", "virtio-serial");
1916 dev_opts = qemu_opts_create(device, NULL, 0);
1917 qemu_opt_set(dev_opts, "driver", "virtconsole");
1919 snprintf(label, sizeof(label), "virtcon%d", index);
1920 virtcon_hds[index] = qemu_chr_open(label, devname, NULL);
1921 if (!virtcon_hds[index]) {
1922 fprintf(stderr, "qemu: could not open virtio console '%s': %s\n",
1923 devname, strerror(errno));
1924 return -1;
1926 qemu_opt_set(dev_opts, "chardev", label);
1928 index++;
1929 return 0;
1932 static int debugcon_parse(const char *devname)
1934 QemuOpts *opts;
1936 if (!qemu_chr_open("debugcon", devname, NULL)) {
1937 exit(1);
1939 opts = qemu_opts_create(qemu_find_opts("device"), "debugcon", 1);
1940 if (!opts) {
1941 fprintf(stderr, "qemu: already have a debugcon device\n");
1942 exit(1);
1944 qemu_opt_set(opts, "driver", "isa-debugcon");
1945 qemu_opt_set(opts, "chardev", "debugcon");
1946 return 0;
1949 void qemu_add_exit_notifier(Notifier *notify)
1951 notifier_list_add(&exit_notifiers, notify);
1954 void qemu_remove_exit_notifier(Notifier *notify)
1956 notifier_list_remove(&exit_notifiers, notify);
1959 static void qemu_run_exit_notifiers(void)
1961 notifier_list_notify(&exit_notifiers);
1964 void qemu_add_machine_init_done_notifier(Notifier *notify)
1966 notifier_list_add(&machine_init_done_notifiers, notify);
1969 static void qemu_run_machine_init_done_notifiers(void)
1971 notifier_list_notify(&machine_init_done_notifiers);
1974 static const QEMUOption *lookup_opt(int argc, char **argv,
1975 const char **poptarg, int *poptind)
1977 const QEMUOption *popt;
1978 int optind = *poptind;
1979 char *r = argv[optind];
1980 const char *optarg;
1982 loc_set_cmdline(argv, optind, 1);
1983 optind++;
1984 /* Treat --foo the same as -foo. */
1985 if (r[1] == '-')
1986 r++;
1987 popt = qemu_options;
1988 for(;;) {
1989 if (!popt->name) {
1990 error_report("invalid option");
1991 exit(1);
1993 if (!strcmp(popt->name, r + 1))
1994 break;
1995 popt++;
1997 if (popt->flags & HAS_ARG) {
1998 if (optind >= argc) {
1999 error_report("requires an argument");
2000 exit(1);
2002 optarg = argv[optind++];
2003 loc_set_cmdline(argv, optind - 2, 2);
2004 } else {
2005 optarg = NULL;
2008 *poptarg = optarg;
2009 *poptind = optind;
2011 return popt;
2014 int main(int argc, char **argv, char **envp)
2016 const char *gdbstub_dev = NULL;
2017 int i;
2018 int snapshot, linux_boot;
2019 const char *icount_option = NULL;
2020 const char *initrd_filename;
2021 const char *kernel_filename, *kernel_cmdline;
2022 char boot_devices[33] = "cad"; /* default to HD->floppy->CD-ROM */
2023 DisplayState *ds;
2024 DisplayChangeListener *dcl;
2025 int cyls, heads, secs, translation;
2026 QemuOpts *hda_opts = NULL, *opts;
2027 QemuOptsList *olist;
2028 int optind;
2029 const char *optarg;
2030 const char *loadvm = NULL;
2031 QEMUMachine *machine;
2032 const char *cpu_model;
2033 int tb_size;
2034 const char *pid_file = NULL;
2035 const char *incoming = NULL;
2036 #ifdef CONFIG_VNC
2037 int show_vnc_port = 0;
2038 #endif
2039 int defconfig = 1;
2040 const char *trace_file = NULL;
2042 atexit(qemu_run_exit_notifiers);
2043 error_set_progname(argv[0]);
2045 init_clocks();
2047 qemu_cache_utils_init(envp);
2049 QLIST_INIT (&vm_change_state_head);
2050 os_setup_early_signal_handling();
2052 module_call_init(MODULE_INIT_MACHINE);
2053 machine = find_default_machine();
2054 cpu_model = NULL;
2055 initrd_filename = NULL;
2056 ram_size = 0;
2057 snapshot = 0;
2058 kernel_filename = NULL;
2059 kernel_cmdline = "";
2060 cyls = heads = secs = 0;
2061 translation = BIOS_ATA_TRANSLATION_AUTO;
2063 for (i = 0; i < MAX_NODES; i++) {
2064 node_mem[i] = 0;
2065 node_cpumask[i] = 0;
2068 nb_numa_nodes = 0;
2069 nb_nics = 0;
2071 tb_size = 0;
2072 autostart= 1;
2074 /* first pass of option parsing */
2075 optind = 1;
2076 while (optind < argc) {
2077 if (argv[optind][0] != '-') {
2078 /* disk image */
2079 optind++;
2080 continue;
2081 } else {
2082 const QEMUOption *popt;
2084 popt = lookup_opt(argc, argv, &optarg, &optind);
2085 switch (popt->index) {
2086 case QEMU_OPTION_nodefconfig:
2087 defconfig=0;
2088 break;
2093 if (defconfig) {
2094 int ret;
2096 ret = qemu_read_config_file(CONFIG_QEMU_CONFDIR "/qemu.conf");
2097 if (ret < 0 && ret != -ENOENT) {
2098 exit(1);
2101 ret = qemu_read_config_file(arch_config_name);
2102 if (ret < 0 && ret != -ENOENT) {
2103 exit(1);
2106 cpudef_init();
2108 /* second pass of option parsing */
2109 optind = 1;
2110 for(;;) {
2111 if (optind >= argc)
2112 break;
2113 if (argv[optind][0] != '-') {
2114 hda_opts = drive_add(IF_DEFAULT, 0, argv[optind++], HD_OPTS);
2115 } else {
2116 const QEMUOption *popt;
2118 popt = lookup_opt(argc, argv, &optarg, &optind);
2119 if (!(popt->arch_mask & arch_type)) {
2120 printf("Option %s not supported for this target\n", popt->name);
2121 exit(1);
2123 switch(popt->index) {
2124 case QEMU_OPTION_M:
2125 machine = find_machine(optarg);
2126 if (!machine) {
2127 QEMUMachine *m;
2128 printf("Supported machines are:\n");
2129 for(m = first_machine; m != NULL; m = m->next) {
2130 if (m->alias)
2131 printf("%-10s %s (alias of %s)\n",
2132 m->alias, m->desc, m->name);
2133 printf("%-10s %s%s\n",
2134 m->name, m->desc,
2135 m->is_default ? " (default)" : "");
2137 exit(*optarg != '?');
2139 break;
2140 case QEMU_OPTION_cpu:
2141 /* hw initialization will check this */
2142 if (*optarg == '?') {
2143 list_cpus(stdout, &fprintf, optarg);
2144 exit(0);
2145 } else {
2146 cpu_model = optarg;
2148 break;
2149 case QEMU_OPTION_initrd:
2150 initrd_filename = optarg;
2151 break;
2152 case QEMU_OPTION_hda:
2154 char buf[256];
2155 if (cyls == 0)
2156 snprintf(buf, sizeof(buf), "%s", HD_OPTS);
2157 else
2158 snprintf(buf, sizeof(buf),
2159 "%s,cyls=%d,heads=%d,secs=%d%s",
2160 HD_OPTS , cyls, heads, secs,
2161 translation == BIOS_ATA_TRANSLATION_LBA ?
2162 ",trans=lba" :
2163 translation == BIOS_ATA_TRANSLATION_NONE ?
2164 ",trans=none" : "");
2165 drive_add(IF_DEFAULT, 0, optarg, buf);
2166 break;
2168 case QEMU_OPTION_hdb:
2169 case QEMU_OPTION_hdc:
2170 case QEMU_OPTION_hdd:
2171 drive_add(IF_DEFAULT, popt->index - QEMU_OPTION_hda, optarg,
2172 HD_OPTS);
2173 break;
2174 case QEMU_OPTION_drive:
2175 drive_def(optarg);
2176 break;
2177 case QEMU_OPTION_set:
2178 if (qemu_set_option(optarg) != 0)
2179 exit(1);
2180 break;
2181 case QEMU_OPTION_global:
2182 if (qemu_global_option(optarg) != 0)
2183 exit(1);
2184 break;
2185 case QEMU_OPTION_mtdblock:
2186 drive_add(IF_MTD, -1, optarg, MTD_OPTS);
2187 break;
2188 case QEMU_OPTION_sd:
2189 drive_add(IF_SD, 0, optarg, SD_OPTS);
2190 break;
2191 case QEMU_OPTION_pflash:
2192 drive_add(IF_PFLASH, -1, optarg, PFLASH_OPTS);
2193 break;
2194 case QEMU_OPTION_snapshot:
2195 snapshot = 1;
2196 break;
2197 case QEMU_OPTION_hdachs:
2199 const char *p;
2200 p = optarg;
2201 cyls = strtol(p, (char **)&p, 0);
2202 if (cyls < 1 || cyls > 16383)
2203 goto chs_fail;
2204 if (*p != ',')
2205 goto chs_fail;
2206 p++;
2207 heads = strtol(p, (char **)&p, 0);
2208 if (heads < 1 || heads > 16)
2209 goto chs_fail;
2210 if (*p != ',')
2211 goto chs_fail;
2212 p++;
2213 secs = strtol(p, (char **)&p, 0);
2214 if (secs < 1 || secs > 63)
2215 goto chs_fail;
2216 if (*p == ',') {
2217 p++;
2218 if (!strcmp(p, "none"))
2219 translation = BIOS_ATA_TRANSLATION_NONE;
2220 else if (!strcmp(p, "lba"))
2221 translation = BIOS_ATA_TRANSLATION_LBA;
2222 else if (!strcmp(p, "auto"))
2223 translation = BIOS_ATA_TRANSLATION_AUTO;
2224 else
2225 goto chs_fail;
2226 } else if (*p != '\0') {
2227 chs_fail:
2228 fprintf(stderr, "qemu: invalid physical CHS format\n");
2229 exit(1);
2231 if (hda_opts != NULL) {
2232 char num[16];
2233 snprintf(num, sizeof(num), "%d", cyls);
2234 qemu_opt_set(hda_opts, "cyls", num);
2235 snprintf(num, sizeof(num), "%d", heads);
2236 qemu_opt_set(hda_opts, "heads", num);
2237 snprintf(num, sizeof(num), "%d", secs);
2238 qemu_opt_set(hda_opts, "secs", num);
2239 if (translation == BIOS_ATA_TRANSLATION_LBA)
2240 qemu_opt_set(hda_opts, "trans", "lba");
2241 if (translation == BIOS_ATA_TRANSLATION_NONE)
2242 qemu_opt_set(hda_opts, "trans", "none");
2245 break;
2246 case QEMU_OPTION_numa:
2247 if (nb_numa_nodes >= MAX_NODES) {
2248 fprintf(stderr, "qemu: too many NUMA nodes\n");
2249 exit(1);
2251 numa_add(optarg);
2252 break;
2253 case QEMU_OPTION_display:
2254 display_type = select_display(optarg);
2255 break;
2256 case QEMU_OPTION_nographic:
2257 display_type = DT_NOGRAPHIC;
2258 break;
2259 case QEMU_OPTION_curses:
2260 #ifdef CONFIG_CURSES
2261 display_type = DT_CURSES;
2262 #else
2263 fprintf(stderr, "Curses support is disabled\n");
2264 exit(1);
2265 #endif
2266 break;
2267 case QEMU_OPTION_portrait:
2268 graphic_rotate = 1;
2269 break;
2270 case QEMU_OPTION_kernel:
2271 kernel_filename = optarg;
2272 break;
2273 case QEMU_OPTION_append:
2274 kernel_cmdline = optarg;
2275 break;
2276 case QEMU_OPTION_cdrom:
2277 drive_add(IF_DEFAULT, 2, optarg, CDROM_OPTS);
2278 break;
2279 case QEMU_OPTION_boot:
2281 static const char * const params[] = {
2282 "order", "once", "menu", NULL
2284 char buf[sizeof(boot_devices)];
2285 char *standard_boot_devices;
2286 int legacy = 0;
2288 if (!strchr(optarg, '=')) {
2289 legacy = 1;
2290 pstrcpy(buf, sizeof(buf), optarg);
2291 } else if (check_params(buf, sizeof(buf), params, optarg) < 0) {
2292 fprintf(stderr,
2293 "qemu: unknown boot parameter '%s' in '%s'\n",
2294 buf, optarg);
2295 exit(1);
2298 if (legacy ||
2299 get_param_value(buf, sizeof(buf), "order", optarg)) {
2300 validate_bootdevices(buf);
2301 pstrcpy(boot_devices, sizeof(boot_devices), buf);
2303 if (!legacy) {
2304 if (get_param_value(buf, sizeof(buf),
2305 "once", optarg)) {
2306 validate_bootdevices(buf);
2307 standard_boot_devices = qemu_strdup(boot_devices);
2308 pstrcpy(boot_devices, sizeof(boot_devices), buf);
2309 qemu_register_reset(restore_boot_devices,
2310 standard_boot_devices);
2312 if (get_param_value(buf, sizeof(buf),
2313 "menu", optarg)) {
2314 if (!strcmp(buf, "on")) {
2315 boot_menu = 1;
2316 } else if (!strcmp(buf, "off")) {
2317 boot_menu = 0;
2318 } else {
2319 fprintf(stderr,
2320 "qemu: invalid option value '%s'\n",
2321 buf);
2322 exit(1);
2327 break;
2328 case QEMU_OPTION_fda:
2329 case QEMU_OPTION_fdb:
2330 drive_add(IF_FLOPPY, popt->index - QEMU_OPTION_fda,
2331 optarg, FD_OPTS);
2332 break;
2333 case QEMU_OPTION_no_fd_bootchk:
2334 fd_bootchk = 0;
2335 break;
2336 case QEMU_OPTION_netdev:
2337 if (net_client_parse(qemu_find_opts("netdev"), optarg) == -1) {
2338 exit(1);
2340 break;
2341 case QEMU_OPTION_net:
2342 if (net_client_parse(qemu_find_opts("net"), optarg) == -1) {
2343 exit(1);
2345 break;
2346 #ifdef CONFIG_SLIRP
2347 case QEMU_OPTION_tftp:
2348 legacy_tftp_prefix = optarg;
2349 break;
2350 case QEMU_OPTION_bootp:
2351 legacy_bootp_filename = optarg;
2352 break;
2353 case QEMU_OPTION_redir:
2354 if (net_slirp_redir(optarg) < 0)
2355 exit(1);
2356 break;
2357 #endif
2358 case QEMU_OPTION_bt:
2359 add_device_config(DEV_BT, optarg);
2360 break;
2361 case QEMU_OPTION_audio_help:
2362 if (!(audio_available())) {
2363 printf("Option %s not supported for this target\n", popt->name);
2364 exit(1);
2366 AUD_help ();
2367 exit (0);
2368 break;
2369 case QEMU_OPTION_soundhw:
2370 if (!(audio_available())) {
2371 printf("Option %s not supported for this target\n", popt->name);
2372 exit(1);
2374 select_soundhw (optarg);
2375 break;
2376 case QEMU_OPTION_h:
2377 help(0);
2378 break;
2379 case QEMU_OPTION_version:
2380 version();
2381 exit(0);
2382 break;
2383 case QEMU_OPTION_m: {
2384 int64_t value;
2386 value = strtosz(optarg, NULL);
2387 if (value < 0) {
2388 fprintf(stderr, "qemu: invalid ram size: %s\n", optarg);
2389 exit(1);
2392 /* On 32-bit hosts, QEMU is limited by virtual address space */
2393 if (value > (2047 << 20) && HOST_LONG_BITS == 32) {
2394 fprintf(stderr, "qemu: at most 2047 MB RAM can be simulated\n");
2395 exit(1);
2397 if (value != (uint64_t)(ram_addr_t)value) {
2398 fprintf(stderr, "qemu: ram size too large\n");
2399 exit(1);
2401 ram_size = value;
2402 break;
2404 case QEMU_OPTION_mempath:
2405 mem_path = optarg;
2406 break;
2407 #ifdef MAP_POPULATE
2408 case QEMU_OPTION_mem_prealloc:
2409 mem_prealloc = 1;
2410 break;
2411 #endif
2412 case QEMU_OPTION_d:
2413 set_cpu_log(optarg);
2414 break;
2415 case QEMU_OPTION_s:
2416 gdbstub_dev = "tcp::" DEFAULT_GDBSTUB_PORT;
2417 break;
2418 case QEMU_OPTION_gdb:
2419 gdbstub_dev = optarg;
2420 break;
2421 case QEMU_OPTION_L:
2422 data_dir = optarg;
2423 break;
2424 case QEMU_OPTION_bios:
2425 bios_name = optarg;
2426 break;
2427 case QEMU_OPTION_singlestep:
2428 singlestep = 1;
2429 break;
2430 case QEMU_OPTION_S:
2431 autostart = 0;
2432 break;
2433 case QEMU_OPTION_k:
2434 keyboard_layout = optarg;
2435 break;
2436 case QEMU_OPTION_localtime:
2437 rtc_utc = 0;
2438 break;
2439 case QEMU_OPTION_vga:
2440 select_vgahw (optarg);
2441 break;
2442 case QEMU_OPTION_g:
2444 const char *p;
2445 int w, h, depth;
2446 p = optarg;
2447 w = strtol(p, (char **)&p, 10);
2448 if (w <= 0) {
2449 graphic_error:
2450 fprintf(stderr, "qemu: invalid resolution or depth\n");
2451 exit(1);
2453 if (*p != 'x')
2454 goto graphic_error;
2455 p++;
2456 h = strtol(p, (char **)&p, 10);
2457 if (h <= 0)
2458 goto graphic_error;
2459 if (*p == 'x') {
2460 p++;
2461 depth = strtol(p, (char **)&p, 10);
2462 if (depth != 8 && depth != 15 && depth != 16 &&
2463 depth != 24 && depth != 32)
2464 goto graphic_error;
2465 } else if (*p == '\0') {
2466 depth = graphic_depth;
2467 } else {
2468 goto graphic_error;
2471 graphic_width = w;
2472 graphic_height = h;
2473 graphic_depth = depth;
2475 break;
2476 case QEMU_OPTION_echr:
2478 char *r;
2479 term_escape_char = strtol(optarg, &r, 0);
2480 if (r == optarg)
2481 printf("Bad argument to echr\n");
2482 break;
2484 case QEMU_OPTION_monitor:
2485 monitor_parse(optarg, "readline");
2486 default_monitor = 0;
2487 break;
2488 case QEMU_OPTION_qmp:
2489 monitor_parse(optarg, "control");
2490 default_monitor = 0;
2491 break;
2492 case QEMU_OPTION_mon:
2493 opts = qemu_opts_parse(qemu_find_opts("mon"), optarg, 1);
2494 if (!opts) {
2495 exit(1);
2497 default_monitor = 0;
2498 break;
2499 case QEMU_OPTION_chardev:
2500 opts = qemu_opts_parse(qemu_find_opts("chardev"), optarg, 1);
2501 if (!opts) {
2502 exit(1);
2504 break;
2505 case QEMU_OPTION_fsdev:
2506 olist = qemu_find_opts("fsdev");
2507 if (!olist) {
2508 fprintf(stderr, "fsdev is not supported by this qemu build.\n");
2509 exit(1);
2511 opts = qemu_opts_parse(olist, optarg, 1);
2512 if (!opts) {
2513 fprintf(stderr, "parse error: %s\n", optarg);
2514 exit(1);
2516 break;
2517 case QEMU_OPTION_virtfs: {
2518 char *arg_fsdev = NULL;
2519 char *arg_9p = NULL;
2520 int len = 0;
2522 olist = qemu_find_opts("virtfs");
2523 if (!olist) {
2524 fprintf(stderr, "virtfs is not supported by this qemu build.\n");
2525 exit(1);
2527 opts = qemu_opts_parse(olist, optarg, 1);
2528 if (!opts) {
2529 fprintf(stderr, "parse error: %s\n", optarg);
2530 exit(1);
2533 if (qemu_opt_get(opts, "fstype") == NULL ||
2534 qemu_opt_get(opts, "mount_tag") == NULL ||
2535 qemu_opt_get(opts, "path") == NULL ||
2536 qemu_opt_get(opts, "security_model") == NULL) {
2537 fprintf(stderr, "Usage: -virtfs fstype,path=/share_path/,"
2538 "security_model=[mapped|passthrough|none],"
2539 "mnt_tag=tag.\n");
2540 exit(1);
2543 len = strlen(",id=,path=,security_model=");
2544 len += strlen(qemu_opt_get(opts, "fstype"));
2545 len += strlen(qemu_opt_get(opts, "mount_tag"));
2546 len += strlen(qemu_opt_get(opts, "path"));
2547 len += strlen(qemu_opt_get(opts, "security_model"));
2548 arg_fsdev = qemu_malloc((len + 1) * sizeof(*arg_fsdev));
2550 snprintf(arg_fsdev, (len + 1) * sizeof(*arg_fsdev),
2551 "%s,id=%s,path=%s,security_model=%s",
2552 qemu_opt_get(opts, "fstype"),
2553 qemu_opt_get(opts, "mount_tag"),
2554 qemu_opt_get(opts, "path"),
2555 qemu_opt_get(opts, "security_model"));
2557 len = strlen("virtio-9p-pci,fsdev=,mount_tag=");
2558 len += 2*strlen(qemu_opt_get(opts, "mount_tag"));
2559 arg_9p = qemu_malloc((len + 1) * sizeof(*arg_9p));
2561 snprintf(arg_9p, (len + 1) * sizeof(*arg_9p),
2562 "virtio-9p-pci,fsdev=%s,mount_tag=%s",
2563 qemu_opt_get(opts, "mount_tag"),
2564 qemu_opt_get(opts, "mount_tag"));
2566 if (!qemu_opts_parse(qemu_find_opts("fsdev"), arg_fsdev, 1)) {
2567 fprintf(stderr, "parse error [fsdev]: %s\n", optarg);
2568 exit(1);
2571 if (!qemu_opts_parse(qemu_find_opts("device"), arg_9p, 1)) {
2572 fprintf(stderr, "parse error [device]: %s\n", optarg);
2573 exit(1);
2576 qemu_free(arg_fsdev);
2577 qemu_free(arg_9p);
2578 break;
2580 case QEMU_OPTION_serial:
2581 add_device_config(DEV_SERIAL, optarg);
2582 default_serial = 0;
2583 if (strncmp(optarg, "mon:", 4) == 0) {
2584 default_monitor = 0;
2586 break;
2587 case QEMU_OPTION_watchdog:
2588 if (watchdog) {
2589 fprintf(stderr,
2590 "qemu: only one watchdog option may be given\n");
2591 return 1;
2593 watchdog = optarg;
2594 break;
2595 case QEMU_OPTION_watchdog_action:
2596 if (select_watchdog_action(optarg) == -1) {
2597 fprintf(stderr, "Unknown -watchdog-action parameter\n");
2598 exit(1);
2600 break;
2601 case QEMU_OPTION_virtiocon:
2602 add_device_config(DEV_VIRTCON, optarg);
2603 default_virtcon = 0;
2604 if (strncmp(optarg, "mon:", 4) == 0) {
2605 default_monitor = 0;
2607 break;
2608 case QEMU_OPTION_parallel:
2609 add_device_config(DEV_PARALLEL, optarg);
2610 default_parallel = 0;
2611 if (strncmp(optarg, "mon:", 4) == 0) {
2612 default_monitor = 0;
2614 break;
2615 case QEMU_OPTION_debugcon:
2616 add_device_config(DEV_DEBUGCON, optarg);
2617 break;
2618 case QEMU_OPTION_loadvm:
2619 loadvm = optarg;
2620 break;
2621 case QEMU_OPTION_full_screen:
2622 full_screen = 1;
2623 break;
2624 #ifdef CONFIG_SDL
2625 case QEMU_OPTION_no_frame:
2626 no_frame = 1;
2627 break;
2628 case QEMU_OPTION_alt_grab:
2629 alt_grab = 1;
2630 break;
2631 case QEMU_OPTION_ctrl_grab:
2632 ctrl_grab = 1;
2633 break;
2634 case QEMU_OPTION_no_quit:
2635 no_quit = 1;
2636 break;
2637 case QEMU_OPTION_sdl:
2638 display_type = DT_SDL;
2639 break;
2640 #else
2641 case QEMU_OPTION_no_frame:
2642 case QEMU_OPTION_alt_grab:
2643 case QEMU_OPTION_ctrl_grab:
2644 case QEMU_OPTION_no_quit:
2645 case QEMU_OPTION_sdl:
2646 fprintf(stderr, "SDL support is disabled\n");
2647 exit(1);
2648 #endif
2649 case QEMU_OPTION_pidfile:
2650 pid_file = optarg;
2651 break;
2652 case QEMU_OPTION_win2k_hack:
2653 win2k_install_hack = 1;
2654 break;
2655 case QEMU_OPTION_rtc_td_hack:
2656 rtc_td_hack = 1;
2657 break;
2658 case QEMU_OPTION_acpitable:
2659 do_acpitable_option(optarg);
2660 break;
2661 case QEMU_OPTION_smbios:
2662 do_smbios_option(optarg);
2663 break;
2664 case QEMU_OPTION_enable_kvm:
2665 kvm_allowed = 1;
2666 break;
2667 case QEMU_OPTION_usb:
2668 usb_enabled = 1;
2669 break;
2670 case QEMU_OPTION_usbdevice:
2671 usb_enabled = 1;
2672 add_device_config(DEV_USB, optarg);
2673 break;
2674 case QEMU_OPTION_device:
2675 if (!qemu_opts_parse(qemu_find_opts("device"), optarg, 1)) {
2676 exit(1);
2678 break;
2679 case QEMU_OPTION_smp:
2680 smp_parse(optarg);
2681 if (smp_cpus < 1) {
2682 fprintf(stderr, "Invalid number of CPUs\n");
2683 exit(1);
2685 if (max_cpus < smp_cpus) {
2686 fprintf(stderr, "maxcpus must be equal to or greater than "
2687 "smp\n");
2688 exit(1);
2690 if (max_cpus > 255) {
2691 fprintf(stderr, "Unsupported number of maxcpus\n");
2692 exit(1);
2694 break;
2695 case QEMU_OPTION_vnc:
2696 #ifdef CONFIG_VNC
2697 display_remote++;
2698 vnc_display = optarg;
2699 #else
2700 fprintf(stderr, "VNC support is disabled\n");
2701 exit(1);
2702 #endif
2703 break;
2704 case QEMU_OPTION_no_acpi:
2705 acpi_enabled = 0;
2706 break;
2707 case QEMU_OPTION_no_hpet:
2708 no_hpet = 1;
2709 break;
2710 case QEMU_OPTION_balloon:
2711 if (balloon_parse(optarg) < 0) {
2712 fprintf(stderr, "Unknown -balloon argument %s\n", optarg);
2713 exit(1);
2715 break;
2716 case QEMU_OPTION_no_reboot:
2717 no_reboot = 1;
2718 break;
2719 case QEMU_OPTION_no_shutdown:
2720 no_shutdown = 1;
2721 break;
2722 case QEMU_OPTION_show_cursor:
2723 cursor_hide = 0;
2724 break;
2725 case QEMU_OPTION_uuid:
2726 if(qemu_uuid_parse(optarg, qemu_uuid) < 0) {
2727 fprintf(stderr, "Fail to parse UUID string."
2728 " Wrong format.\n");
2729 exit(1);
2731 break;
2732 case QEMU_OPTION_option_rom:
2733 if (nb_option_roms >= MAX_OPTION_ROMS) {
2734 fprintf(stderr, "Too many option ROMs\n");
2735 exit(1);
2737 opts = qemu_opts_parse(qemu_find_opts("option-rom"), optarg, 1);
2738 option_rom[nb_option_roms].name = qemu_opt_get(opts, "romfile");
2739 option_rom[nb_option_roms].bootindex =
2740 qemu_opt_get_number(opts, "bootindex", -1);
2741 if (!option_rom[nb_option_roms].name) {
2742 fprintf(stderr, "Option ROM file is not specified\n");
2743 exit(1);
2745 nb_option_roms++;
2746 break;
2747 case QEMU_OPTION_semihosting:
2748 semihosting_enabled = 1;
2749 break;
2750 case QEMU_OPTION_name:
2751 qemu_name = qemu_strdup(optarg);
2753 char *p = strchr(qemu_name, ',');
2754 if (p != NULL) {
2755 *p++ = 0;
2756 if (strncmp(p, "process=", 8)) {
2757 fprintf(stderr, "Unknown subargument %s to -name\n", p);
2758 exit(1);
2760 p += 8;
2761 os_set_proc_name(p);
2764 break;
2765 case QEMU_OPTION_prom_env:
2766 if (nb_prom_envs >= MAX_PROM_ENVS) {
2767 fprintf(stderr, "Too many prom variables\n");
2768 exit(1);
2770 prom_envs[nb_prom_envs] = optarg;
2771 nb_prom_envs++;
2772 break;
2773 case QEMU_OPTION_old_param:
2774 old_param = 1;
2775 break;
2776 case QEMU_OPTION_clock:
2777 configure_alarms(optarg);
2778 break;
2779 case QEMU_OPTION_startdate:
2780 configure_rtc_date_offset(optarg, 1);
2781 break;
2782 case QEMU_OPTION_rtc:
2783 opts = qemu_opts_parse(qemu_find_opts("rtc"), optarg, 0);
2784 if (!opts) {
2785 exit(1);
2787 configure_rtc(opts);
2788 break;
2789 case QEMU_OPTION_tb_size:
2790 tb_size = strtol(optarg, NULL, 0);
2791 if (tb_size < 0)
2792 tb_size = 0;
2793 break;
2794 case QEMU_OPTION_icount:
2795 icount_option = optarg;
2796 break;
2797 case QEMU_OPTION_incoming:
2798 incoming = optarg;
2799 incoming_expected = true;
2800 break;
2801 case QEMU_OPTION_nodefaults:
2802 default_serial = 0;
2803 default_parallel = 0;
2804 default_virtcon = 0;
2805 default_monitor = 0;
2806 default_vga = 0;
2807 default_net = 0;
2808 default_floppy = 0;
2809 default_cdrom = 0;
2810 default_sdcard = 0;
2811 break;
2812 case QEMU_OPTION_xen_domid:
2813 if (!(xen_available())) {
2814 printf("Option %s not supported for this target\n", popt->name);
2815 exit(1);
2817 xen_domid = atoi(optarg);
2818 break;
2819 case QEMU_OPTION_xen_create:
2820 if (!(xen_available())) {
2821 printf("Option %s not supported for this target\n", popt->name);
2822 exit(1);
2824 xen_mode = XEN_CREATE;
2825 break;
2826 case QEMU_OPTION_xen_attach:
2827 if (!(xen_available())) {
2828 printf("Option %s not supported for this target\n", popt->name);
2829 exit(1);
2831 xen_mode = XEN_ATTACH;
2832 break;
2833 #ifdef CONFIG_SIMPLE_TRACE
2834 case QEMU_OPTION_trace:
2835 opts = qemu_opts_parse(qemu_find_opts("trace"), optarg, 0);
2836 if (opts) {
2837 trace_file = qemu_opt_get(opts, "file");
2839 break;
2840 #endif
2841 case QEMU_OPTION_readconfig:
2843 int ret = qemu_read_config_file(optarg);
2844 if (ret < 0) {
2845 fprintf(stderr, "read config %s: %s\n", optarg,
2846 strerror(-ret));
2847 exit(1);
2849 break;
2851 case QEMU_OPTION_spice:
2852 olist = qemu_find_opts("spice");
2853 if (!olist) {
2854 fprintf(stderr, "spice is not supported by this qemu build.\n");
2855 exit(1);
2857 opts = qemu_opts_parse(olist, optarg, 0);
2858 if (!opts) {
2859 fprintf(stderr, "parse error: %s\n", optarg);
2860 exit(1);
2862 break;
2863 case QEMU_OPTION_writeconfig:
2865 FILE *fp;
2866 if (strcmp(optarg, "-") == 0) {
2867 fp = stdout;
2868 } else {
2869 fp = fopen(optarg, "w");
2870 if (fp == NULL) {
2871 fprintf(stderr, "open %s: %s\n", optarg, strerror(errno));
2872 exit(1);
2875 qemu_config_write(fp);
2876 fclose(fp);
2877 break;
2879 default:
2880 os_parse_cmd_args(popt->index, optarg);
2884 loc_set_none();
2886 if (!st_init(trace_file)) {
2887 fprintf(stderr, "warning: unable to initialize simple trace backend\n");
2890 /* If no data_dir is specified then try to find it relative to the
2891 executable path. */
2892 if (!data_dir) {
2893 data_dir = os_find_datadir(argv[0]);
2895 /* If all else fails use the install patch specified when building. */
2896 if (!data_dir) {
2897 data_dir = CONFIG_QEMU_DATADIR;
2901 * Default to max_cpus = smp_cpus, in case the user doesn't
2902 * specify a max_cpus value.
2904 if (!max_cpus)
2905 max_cpus = smp_cpus;
2907 machine->max_cpus = machine->max_cpus ?: 1; /* Default to UP */
2908 if (smp_cpus > machine->max_cpus) {
2909 fprintf(stderr, "Number of SMP cpus requested (%d), exceeds max cpus "
2910 "supported by machine `%s' (%d)\n", smp_cpus, machine->name,
2911 machine->max_cpus);
2912 exit(1);
2915 qemu_opts_foreach(qemu_find_opts("device"), default_driver_check, NULL, 0);
2916 qemu_opts_foreach(qemu_find_opts("global"), default_driver_check, NULL, 0);
2918 if (machine->no_serial) {
2919 default_serial = 0;
2921 if (machine->no_parallel) {
2922 default_parallel = 0;
2924 if (!machine->use_virtcon) {
2925 default_virtcon = 0;
2927 if (machine->no_vga) {
2928 default_vga = 0;
2930 if (machine->no_floppy) {
2931 default_floppy = 0;
2933 if (machine->no_cdrom) {
2934 default_cdrom = 0;
2936 if (machine->no_sdcard) {
2937 default_sdcard = 0;
2940 if (display_type == DT_NOGRAPHIC) {
2941 if (default_parallel)
2942 add_device_config(DEV_PARALLEL, "null");
2943 if (default_serial && default_monitor) {
2944 add_device_config(DEV_SERIAL, "mon:stdio");
2945 } else if (default_virtcon && default_monitor) {
2946 add_device_config(DEV_VIRTCON, "mon:stdio");
2947 } else {
2948 if (default_serial)
2949 add_device_config(DEV_SERIAL, "stdio");
2950 if (default_virtcon)
2951 add_device_config(DEV_VIRTCON, "stdio");
2952 if (default_monitor)
2953 monitor_parse("stdio", "readline");
2955 } else {
2956 if (default_serial)
2957 add_device_config(DEV_SERIAL, "vc:80Cx24C");
2958 if (default_parallel)
2959 add_device_config(DEV_PARALLEL, "vc:80Cx24C");
2960 if (default_monitor)
2961 monitor_parse("vc:80Cx24C", "readline");
2962 if (default_virtcon)
2963 add_device_config(DEV_VIRTCON, "vc:80Cx24C");
2965 if (default_vga)
2966 vga_interface_type = VGA_CIRRUS;
2968 socket_init();
2970 if (qemu_opts_foreach(qemu_find_opts("chardev"), chardev_init_func, NULL, 1) != 0)
2971 exit(1);
2972 #ifdef CONFIG_VIRTFS
2973 if (qemu_opts_foreach(qemu_find_opts("fsdev"), fsdev_init_func, NULL, 1) != 0) {
2974 exit(1);
2976 #endif
2978 os_daemonize();
2980 if (pid_file && qemu_create_pidfile(pid_file) != 0) {
2981 os_pidfile_error();
2982 exit(1);
2985 if (kvm_allowed) {
2986 int ret = kvm_init();
2987 if (ret < 0) {
2988 if (!kvm_available()) {
2989 printf("KVM not supported for this target\n");
2990 } else {
2991 fprintf(stderr, "failed to initialize KVM: %s\n", strerror(-ret));
2993 exit(1);
2997 if (qemu_init_main_loop()) {
2998 fprintf(stderr, "qemu_init_main_loop failed\n");
2999 exit(1);
3001 linux_boot = (kernel_filename != NULL);
3003 if (!linux_boot && *kernel_cmdline != '\0') {
3004 fprintf(stderr, "-append only allowed with -kernel option\n");
3005 exit(1);
3008 if (!linux_boot && initrd_filename != NULL) {
3009 fprintf(stderr, "-initrd only allowed with -kernel option\n");
3010 exit(1);
3013 os_set_line_buffering();
3015 if (init_timer_alarm() < 0) {
3016 fprintf(stderr, "could not initialize alarm timer\n");
3017 exit(1);
3019 configure_icount(icount_option);
3021 if (net_init_clients() < 0) {
3022 exit(1);
3025 /* init the bluetooth world */
3026 if (foreach_device_config(DEV_BT, bt_parse))
3027 exit(1);
3029 /* init the memory */
3030 if (ram_size == 0)
3031 ram_size = DEFAULT_RAM_SIZE * 1024 * 1024;
3033 /* init the dynamic translator */
3034 cpu_exec_init_all(tb_size * 1024 * 1024);
3036 bdrv_init_with_whitelist();
3038 blk_mig_init();
3040 /* open the virtual block devices */
3041 if (snapshot)
3042 qemu_opts_foreach(qemu_find_opts("drive"), drive_enable_snapshot, NULL, 0);
3043 if (qemu_opts_foreach(qemu_find_opts("drive"), drive_init_func, &machine->use_scsi, 1) != 0)
3044 exit(1);
3046 default_drive(default_cdrom, snapshot, machine->use_scsi,
3047 IF_DEFAULT, 2, CDROM_OPTS);
3048 default_drive(default_floppy, snapshot, machine->use_scsi,
3049 IF_FLOPPY, 0, FD_OPTS);
3050 default_drive(default_sdcard, snapshot, machine->use_scsi,
3051 IF_SD, 0, SD_OPTS);
3053 register_savevm_live(NULL, "ram", 0, 4, NULL, ram_save_live, NULL,
3054 ram_load, NULL);
3056 if (nb_numa_nodes > 0) {
3057 int i;
3059 if (nb_numa_nodes > smp_cpus) {
3060 nb_numa_nodes = smp_cpus;
3063 /* If no memory size if given for any node, assume the default case
3064 * and distribute the available memory equally across all nodes
3066 for (i = 0; i < nb_numa_nodes; i++) {
3067 if (node_mem[i] != 0)
3068 break;
3070 if (i == nb_numa_nodes) {
3071 uint64_t usedmem = 0;
3073 /* On Linux, the each node's border has to be 8MB aligned,
3074 * the final node gets the rest.
3076 for (i = 0; i < nb_numa_nodes - 1; i++) {
3077 node_mem[i] = (ram_size / nb_numa_nodes) & ~((1 << 23UL) - 1);
3078 usedmem += node_mem[i];
3080 node_mem[i] = ram_size - usedmem;
3083 for (i = 0; i < nb_numa_nodes; i++) {
3084 if (node_cpumask[i] != 0)
3085 break;
3087 /* assigning the VCPUs round-robin is easier to implement, guest OSes
3088 * must cope with this anyway, because there are BIOSes out there in
3089 * real machines which also use this scheme.
3091 if (i == nb_numa_nodes) {
3092 for (i = 0; i < smp_cpus; i++) {
3093 node_cpumask[i % nb_numa_nodes] |= 1 << i;
3098 if (qemu_opts_foreach(qemu_find_opts("mon"), mon_init_func, NULL, 1) != 0) {
3099 exit(1);
3102 if (foreach_device_config(DEV_SERIAL, serial_parse) < 0)
3103 exit(1);
3104 if (foreach_device_config(DEV_PARALLEL, parallel_parse) < 0)
3105 exit(1);
3106 if (foreach_device_config(DEV_VIRTCON, virtcon_parse) < 0)
3107 exit(1);
3108 if (foreach_device_config(DEV_DEBUGCON, debugcon_parse) < 0)
3109 exit(1);
3111 module_call_init(MODULE_INIT_DEVICE);
3113 if (qemu_opts_foreach(qemu_find_opts("device"), device_help_func, NULL, 0) != 0)
3114 exit(0);
3116 if (watchdog) {
3117 i = select_watchdog(watchdog);
3118 if (i > 0)
3119 exit (i == 1 ? 1 : 0);
3122 if (machine->compat_props) {
3123 qdev_prop_register_global_list(machine->compat_props);
3125 qemu_add_globals();
3127 machine->init(ram_size, boot_devices,
3128 kernel_filename, kernel_cmdline, initrd_filename, cpu_model);
3130 cpu_synchronize_all_post_init();
3132 /* must be after terminal init, SDL library changes signal handlers */
3133 os_setup_signal_handling();
3135 set_numa_modes();
3137 current_machine = machine;
3139 /* init USB devices */
3140 if (usb_enabled) {
3141 if (foreach_device_config(DEV_USB, usb_parse) < 0)
3142 exit(1);
3145 /* init generic devices */
3146 if (qemu_opts_foreach(qemu_find_opts("device"), device_init_func, NULL, 1) != 0)
3147 exit(1);
3149 net_check_clients();
3151 /* just use the first displaystate for the moment */
3152 ds = get_displaystate();
3154 if (using_spice)
3155 display_remote++;
3156 if (display_type == DT_DEFAULT && !display_remote) {
3157 #if defined(CONFIG_SDL) || defined(CONFIG_COCOA)
3158 display_type = DT_SDL;
3159 #elif defined(CONFIG_VNC)
3160 vnc_display = "localhost:0,to=99";
3161 show_vnc_port = 1;
3162 #else
3163 display_type = DT_NONE;
3164 #endif
3168 /* init local displays */
3169 switch (display_type) {
3170 case DT_NOGRAPHIC:
3171 break;
3172 #if defined(CONFIG_CURSES)
3173 case DT_CURSES:
3174 curses_display_init(ds, full_screen);
3175 break;
3176 #endif
3177 #if defined(CONFIG_SDL)
3178 case DT_SDL:
3179 sdl_display_init(ds, full_screen, no_frame);
3180 break;
3181 #elif defined(CONFIG_COCOA)
3182 case DT_SDL:
3183 cocoa_display_init(ds, full_screen);
3184 break;
3185 #endif
3186 default:
3187 break;
3190 #ifdef CONFIG_VNC
3191 /* init remote displays */
3192 if (vnc_display) {
3193 vnc_display_init(ds);
3194 if (vnc_display_open(ds, vnc_display) < 0)
3195 exit(1);
3197 if (show_vnc_port) {
3198 printf("VNC server running on `%s'\n", vnc_display_local_addr(ds));
3201 #endif
3202 #ifdef CONFIG_SPICE
3203 if (using_spice && !qxl_enabled) {
3204 qemu_spice_display_init(ds);
3206 #endif
3208 /* display setup */
3209 dpy_resize(ds);
3210 dcl = ds->listeners;
3211 while (dcl != NULL) {
3212 if (dcl->dpy_refresh != NULL) {
3213 ds->gui_timer = qemu_new_timer_ms(rt_clock, gui_update, ds);
3214 qemu_mod_timer(ds->gui_timer, qemu_get_clock_ms(rt_clock));
3215 break;
3217 dcl = dcl->next;
3219 if (ds->gui_timer == NULL) {
3220 nographic_timer = qemu_new_timer_ms(rt_clock, nographic_update, NULL);
3221 qemu_mod_timer(nographic_timer, qemu_get_clock_ms(rt_clock));
3223 text_consoles_set_display(ds);
3225 if (gdbstub_dev && gdbserver_start(gdbstub_dev) < 0) {
3226 fprintf(stderr, "qemu: could not open gdbserver on device '%s'\n",
3227 gdbstub_dev);
3228 exit(1);
3231 qdev_machine_creation_done();
3233 if (rom_load_all() != 0) {
3234 fprintf(stderr, "rom loading failed\n");
3235 exit(1);
3238 /* TODO: once all bus devices are qdevified, this should be done
3239 * when bus is created by qdev.c */
3240 qemu_register_reset(qbus_reset_all_fn, sysbus_get_default());
3241 qemu_run_machine_init_done_notifiers();
3243 qemu_system_reset();
3244 if (loadvm) {
3245 if (load_vmstate(loadvm) < 0) {
3246 autostart = 0;
3250 if (incoming) {
3251 int ret = qemu_start_incoming_migration(incoming);
3252 if (ret < 0) {
3253 fprintf(stderr, "Migration failed. Exit code %s(%d), exiting.\n",
3254 incoming, ret);
3255 exit(ret);
3257 } else if (autostart) {
3258 vm_start();
3261 os_setup_post();
3263 main_loop();
3264 quit_timers();
3265 net_cleanup();
3267 return 0;