Merge commit 'b4a3d965dee06d52281496bb5fd0a5cb5534b545' into upstream-merge
[qemu-kvm.git] / vl.c
blob879336a2cff27fe23a7fe05e14ab0fab25b8e2a6
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 int debug_requested;
1226 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 main_loop_wait(int nonblocking)
1333 IOHandlerRecord *ioh;
1334 fd_set rfds, wfds, xfds;
1335 int ret, nfds;
1336 struct timeval tv;
1337 int timeout;
1339 if (nonblocking)
1340 timeout = 0;
1341 else {
1342 timeout = qemu_calculate_timeout();
1343 qemu_bh_update_timeout(&timeout);
1346 os_host_main_loop_wait(&timeout);
1348 /* poll any events */
1349 /* XXX: separate device handlers from system ones */
1350 nfds = -1;
1351 FD_ZERO(&rfds);
1352 FD_ZERO(&wfds);
1353 FD_ZERO(&xfds);
1354 QLIST_FOREACH(ioh, &io_handlers, next) {
1355 if (ioh->deleted)
1356 continue;
1357 if (ioh->fd_read &&
1358 (!ioh->fd_read_poll ||
1359 ioh->fd_read_poll(ioh->opaque) != 0)) {
1360 FD_SET(ioh->fd, &rfds);
1361 if (ioh->fd > nfds)
1362 nfds = ioh->fd;
1364 if (ioh->fd_write) {
1365 FD_SET(ioh->fd, &wfds);
1366 if (ioh->fd > nfds)
1367 nfds = ioh->fd;
1371 tv.tv_sec = timeout / 1000;
1372 tv.tv_usec = (timeout % 1000) * 1000;
1374 slirp_select_fill(&nfds, &rfds, &wfds, &xfds);
1376 qemu_mutex_unlock_iothread();
1377 ret = select(nfds + 1, &rfds, &wfds, &xfds, &tv);
1378 qemu_mutex_lock_iothread();
1379 if (ret > 0) {
1380 IOHandlerRecord *pioh;
1382 QLIST_FOREACH_SAFE(ioh, &io_handlers, next, pioh) {
1383 if (!ioh->deleted && ioh->fd_read && FD_ISSET(ioh->fd, &rfds)) {
1384 ioh->fd_read(ioh->opaque);
1385 if (!(ioh->fd_read_poll && ioh->fd_read_poll(ioh->opaque)))
1386 FD_CLR(ioh->fd, &rfds);
1388 if (!ioh->deleted && ioh->fd_write && FD_ISSET(ioh->fd, &wfds)) {
1389 ioh->fd_write(ioh->opaque);
1392 /* Do this last in case read/write handlers marked it for deletion */
1393 if (ioh->deleted) {
1394 QLIST_REMOVE(ioh, next);
1395 qemu_free(ioh);
1400 slirp_select_poll(&rfds, &wfds, &xfds, (ret < 0));
1402 qemu_run_all_timers();
1404 /* Check bottom-halves last in case any of the earlier events triggered
1405 them. */
1406 qemu_bh_poll();
1410 static int vm_can_run(void)
1412 if (powerdown_requested)
1413 return 0;
1414 if (reset_requested)
1415 return 0;
1416 if (shutdown_requested)
1417 return 0;
1418 if (debug_requested)
1419 return 0;
1420 return 1;
1423 qemu_irq qemu_system_powerdown;
1425 static void main_loop(void)
1427 int r;
1429 if (kvm_enabled()) {
1430 kvm_main_loop();
1431 cpu_disable_ticks();
1432 return;
1435 qemu_main_loop_start();
1437 for (;;) {
1438 do {
1439 bool nonblocking = false;
1440 #ifdef CONFIG_PROFILER
1441 int64_t ti;
1442 #endif
1443 #ifndef CONFIG_IOTHREAD
1444 nonblocking = cpu_exec_all();
1445 #endif
1446 #ifdef CONFIG_PROFILER
1447 ti = profile_getclock();
1448 #endif
1449 main_loop_wait(nonblocking);
1450 #ifdef CONFIG_PROFILER
1451 dev_time += profile_getclock() - ti;
1452 #endif
1453 } while (vm_can_run());
1455 if ((r = qemu_debug_requested())) {
1456 vm_stop(r);
1458 if (qemu_shutdown_requested()) {
1459 monitor_protocol_event(QEVENT_SHUTDOWN, NULL);
1460 if (no_shutdown) {
1461 vm_stop(0);
1462 no_shutdown = 0;
1463 } else
1464 break;
1466 if (qemu_reset_requested()) {
1467 pause_all_vcpus();
1468 qemu_system_reset();
1469 resume_all_vcpus();
1471 if (qemu_powerdown_requested()) {
1472 monitor_protocol_event(QEVENT_POWERDOWN, NULL);
1473 qemu_irq_raise(qemu_system_powerdown);
1475 if ((r = qemu_vmstop_requested())) {
1476 vm_stop(r);
1479 bdrv_close_all();
1480 pause_all_vcpus();
1483 static void version(void)
1485 printf("QEMU emulator version " QEMU_VERSION QEMU_PKGVERSION ", Copyright (c) 2003-2008 Fabrice Bellard\n");
1488 static void help(int exitcode)
1490 const char *options_help =
1491 #define DEF(option, opt_arg, opt_enum, opt_help, arch_mask) \
1492 opt_help
1493 #define DEFHEADING(text) stringify(text) "\n"
1494 #include "qemu-options.def"
1495 #undef DEF
1496 #undef DEFHEADING
1497 #undef GEN_DOCS
1499 version();
1500 printf("usage: %s [options] [disk_image]\n"
1501 "\n"
1502 "'disk_image' is a raw hard disk image for IDE hard disk 0\n"
1503 "\n"
1504 "%s\n"
1505 "During emulation, the following keys are useful:\n"
1506 "ctrl-alt-f toggle full screen\n"
1507 "ctrl-alt-n switch to virtual console 'n'\n"
1508 "ctrl-alt toggle mouse and keyboard grab\n"
1509 "\n"
1510 "When using -nographic, press 'ctrl-a h' to get some help.\n",
1511 "qemu",
1512 options_help);
1513 exit(exitcode);
1516 #define HAS_ARG 0x0001
1518 typedef struct QEMUOption {
1519 const char *name;
1520 int flags;
1521 int index;
1522 uint32_t arch_mask;
1523 } QEMUOption;
1525 static const QEMUOption qemu_options[] = {
1526 { "h", 0, QEMU_OPTION_h, QEMU_ARCH_ALL },
1527 #define DEF(option, opt_arg, opt_enum, opt_help, arch_mask) \
1528 { option, opt_arg, opt_enum, arch_mask },
1529 #define DEFHEADING(text)
1530 #include "qemu-options.def"
1531 #undef DEF
1532 #undef DEFHEADING
1533 #undef GEN_DOCS
1534 { NULL },
1536 static void select_vgahw (const char *p)
1538 const char *opts;
1540 default_vga = 0;
1541 vga_interface_type = VGA_NONE;
1542 if (strstart(p, "std", &opts)) {
1543 vga_interface_type = VGA_STD;
1544 } else if (strstart(p, "cirrus", &opts)) {
1545 vga_interface_type = VGA_CIRRUS;
1546 } else if (strstart(p, "vmware", &opts)) {
1547 vga_interface_type = VGA_VMWARE;
1548 } else if (strstart(p, "xenfb", &opts)) {
1549 vga_interface_type = VGA_XENFB;
1550 } else if (strstart(p, "qxl", &opts)) {
1551 vga_interface_type = VGA_QXL;
1552 } else if (!strstart(p, "none", &opts)) {
1553 invalid_vga:
1554 fprintf(stderr, "Unknown vga type: %s\n", p);
1555 exit(1);
1557 while (*opts) {
1558 const char *nextopt;
1560 if (strstart(opts, ",retrace=", &nextopt)) {
1561 opts = nextopt;
1562 if (strstart(opts, "dumb", &nextopt))
1563 vga_retrace_method = VGA_RETRACE_DUMB;
1564 else if (strstart(opts, "precise", &nextopt))
1565 vga_retrace_method = VGA_RETRACE_PRECISE;
1566 else goto invalid_vga;
1567 } else goto invalid_vga;
1568 opts = nextopt;
1572 static int balloon_parse(const char *arg)
1574 QemuOpts *opts;
1576 if (strcmp(arg, "none") == 0) {
1577 return 0;
1580 if (!strncmp(arg, "virtio", 6)) {
1581 if (arg[6] == ',') {
1582 /* have params -> parse them */
1583 opts = qemu_opts_parse(qemu_find_opts("device"), arg+7, 0);
1584 if (!opts)
1585 return -1;
1586 } else {
1587 /* create empty opts */
1588 opts = qemu_opts_create(qemu_find_opts("device"), NULL, 0);
1590 qemu_opt_set(opts, "driver", "virtio-balloon-pci");
1591 return 0;
1594 return -1;
1597 char *qemu_find_file(int type, const char *name)
1599 int len;
1600 const char *subdir;
1601 char *buf;
1603 /* If name contains path separators then try it as a straight path. */
1604 if ((strchr(name, '/') || strchr(name, '\\'))
1605 && access(name, R_OK) == 0) {
1606 return qemu_strdup(name);
1608 switch (type) {
1609 case QEMU_FILE_TYPE_BIOS:
1610 subdir = "";
1611 break;
1612 case QEMU_FILE_TYPE_KEYMAP:
1613 subdir = "keymaps/";
1614 break;
1615 default:
1616 abort();
1618 len = strlen(data_dir) + strlen(name) + strlen(subdir) + 2;
1619 buf = qemu_mallocz(len);
1620 snprintf(buf, len, "%s/%s%s", data_dir, subdir, name);
1621 if (access(buf, R_OK)) {
1622 qemu_free(buf);
1623 return NULL;
1625 return buf;
1628 static int device_help_func(QemuOpts *opts, void *opaque)
1630 return qdev_device_help(opts);
1633 static int device_init_func(QemuOpts *opts, void *opaque)
1635 DeviceState *dev;
1637 dev = qdev_device_add(opts);
1638 if (!dev)
1639 return -1;
1640 return 0;
1643 static int chardev_init_func(QemuOpts *opts, void *opaque)
1645 CharDriverState *chr;
1647 chr = qemu_chr_open_opts(opts, NULL);
1648 if (!chr)
1649 return -1;
1650 return 0;
1653 #ifdef CONFIG_VIRTFS
1654 static int fsdev_init_func(QemuOpts *opts, void *opaque)
1656 int ret;
1657 ret = qemu_fsdev_add(opts);
1659 return ret;
1661 #endif
1663 static int mon_init_func(QemuOpts *opts, void *opaque)
1665 CharDriverState *chr;
1666 const char *chardev;
1667 const char *mode;
1668 int flags;
1670 mode = qemu_opt_get(opts, "mode");
1671 if (mode == NULL) {
1672 mode = "readline";
1674 if (strcmp(mode, "readline") == 0) {
1675 flags = MONITOR_USE_READLINE;
1676 } else if (strcmp(mode, "control") == 0) {
1677 flags = MONITOR_USE_CONTROL;
1678 } else {
1679 fprintf(stderr, "unknown monitor mode \"%s\"\n", mode);
1680 exit(1);
1683 if (qemu_opt_get_bool(opts, "pretty", 0))
1684 flags |= MONITOR_USE_PRETTY;
1686 if (qemu_opt_get_bool(opts, "default", 0))
1687 flags |= MONITOR_IS_DEFAULT;
1689 chardev = qemu_opt_get(opts, "chardev");
1690 chr = qemu_chr_find(chardev);
1691 if (chr == NULL) {
1692 fprintf(stderr, "chardev \"%s\" not found\n", chardev);
1693 exit(1);
1696 monitor_init(chr, flags);
1697 return 0;
1700 static void monitor_parse(const char *optarg, const char *mode)
1702 static int monitor_device_index = 0;
1703 QemuOpts *opts;
1704 const char *p;
1705 char label[32];
1706 int def = 0;
1708 if (strstart(optarg, "chardev:", &p)) {
1709 snprintf(label, sizeof(label), "%s", p);
1710 } else {
1711 snprintf(label, sizeof(label), "compat_monitor%d",
1712 monitor_device_index);
1713 if (monitor_device_index == 0) {
1714 def = 1;
1716 opts = qemu_chr_parse_compat(label, optarg);
1717 if (!opts) {
1718 fprintf(stderr, "parse error: %s\n", optarg);
1719 exit(1);
1723 opts = qemu_opts_create(qemu_find_opts("mon"), label, 1);
1724 if (!opts) {
1725 fprintf(stderr, "duplicate chardev: %s\n", label);
1726 exit(1);
1728 qemu_opt_set(opts, "mode", mode);
1729 qemu_opt_set(opts, "chardev", label);
1730 if (def)
1731 qemu_opt_set(opts, "default", "on");
1732 monitor_device_index++;
1735 struct device_config {
1736 enum {
1737 DEV_USB, /* -usbdevice */
1738 DEV_BT, /* -bt */
1739 DEV_SERIAL, /* -serial */
1740 DEV_PARALLEL, /* -parallel */
1741 DEV_VIRTCON, /* -virtioconsole */
1742 DEV_DEBUGCON, /* -debugcon */
1743 } type;
1744 const char *cmdline;
1745 QTAILQ_ENTRY(device_config) next;
1747 QTAILQ_HEAD(, device_config) device_configs = QTAILQ_HEAD_INITIALIZER(device_configs);
1749 static void add_device_config(int type, const char *cmdline)
1751 struct device_config *conf;
1753 conf = qemu_mallocz(sizeof(*conf));
1754 conf->type = type;
1755 conf->cmdline = cmdline;
1756 QTAILQ_INSERT_TAIL(&device_configs, conf, next);
1759 static int foreach_device_config(int type, int (*func)(const char *cmdline))
1761 struct device_config *conf;
1762 int rc;
1764 QTAILQ_FOREACH(conf, &device_configs, next) {
1765 if (conf->type != type)
1766 continue;
1767 rc = func(conf->cmdline);
1768 if (0 != rc)
1769 return rc;
1771 return 0;
1774 static int serial_parse(const char *devname)
1776 static int index = 0;
1777 char label[32];
1779 if (strcmp(devname, "none") == 0)
1780 return 0;
1781 if (index == MAX_SERIAL_PORTS) {
1782 fprintf(stderr, "qemu: too many serial ports\n");
1783 exit(1);
1785 snprintf(label, sizeof(label), "serial%d", index);
1786 serial_hds[index] = qemu_chr_open(label, devname, NULL);
1787 if (!serial_hds[index]) {
1788 fprintf(stderr, "qemu: could not open serial device '%s': %s\n",
1789 devname, strerror(errno));
1790 return -1;
1792 index++;
1793 return 0;
1796 static int parallel_parse(const char *devname)
1798 static int index = 0;
1799 char label[32];
1801 if (strcmp(devname, "none") == 0)
1802 return 0;
1803 if (index == MAX_PARALLEL_PORTS) {
1804 fprintf(stderr, "qemu: too many parallel ports\n");
1805 exit(1);
1807 snprintf(label, sizeof(label), "parallel%d", index);
1808 parallel_hds[index] = qemu_chr_open(label, devname, NULL);
1809 if (!parallel_hds[index]) {
1810 fprintf(stderr, "qemu: could not open parallel device '%s': %s\n",
1811 devname, strerror(errno));
1812 return -1;
1814 index++;
1815 return 0;
1818 static int virtcon_parse(const char *devname)
1820 QemuOptsList *device = qemu_find_opts("device");
1821 static int index = 0;
1822 char label[32];
1823 QemuOpts *bus_opts, *dev_opts;
1825 if (strcmp(devname, "none") == 0)
1826 return 0;
1827 if (index == MAX_VIRTIO_CONSOLES) {
1828 fprintf(stderr, "qemu: too many virtio consoles\n");
1829 exit(1);
1832 bus_opts = qemu_opts_create(device, NULL, 0);
1833 qemu_opt_set(bus_opts, "driver", "virtio-serial");
1835 dev_opts = qemu_opts_create(device, NULL, 0);
1836 qemu_opt_set(dev_opts, "driver", "virtconsole");
1838 snprintf(label, sizeof(label), "virtcon%d", index);
1839 virtcon_hds[index] = qemu_chr_open(label, devname, NULL);
1840 if (!virtcon_hds[index]) {
1841 fprintf(stderr, "qemu: could not open virtio console '%s': %s\n",
1842 devname, strerror(errno));
1843 return -1;
1845 qemu_opt_set(dev_opts, "chardev", label);
1847 index++;
1848 return 0;
1851 static int debugcon_parse(const char *devname)
1853 QemuOpts *opts;
1855 if (!qemu_chr_open("debugcon", devname, NULL)) {
1856 exit(1);
1858 opts = qemu_opts_create(qemu_find_opts("device"), "debugcon", 1);
1859 if (!opts) {
1860 fprintf(stderr, "qemu: already have a debugcon device\n");
1861 exit(1);
1863 qemu_opt_set(opts, "driver", "isa-debugcon");
1864 qemu_opt_set(opts, "chardev", "debugcon");
1865 return 0;
1868 void qemu_add_exit_notifier(Notifier *notify)
1870 notifier_list_add(&exit_notifiers, notify);
1873 void qemu_remove_exit_notifier(Notifier *notify)
1875 notifier_list_remove(&exit_notifiers, notify);
1878 static void qemu_run_exit_notifiers(void)
1880 notifier_list_notify(&exit_notifiers);
1883 void qemu_add_machine_init_done_notifier(Notifier *notify)
1885 notifier_list_add(&machine_init_done_notifiers, notify);
1888 static void qemu_run_machine_init_done_notifiers(void)
1890 notifier_list_notify(&machine_init_done_notifiers);
1893 static const QEMUOption *lookup_opt(int argc, char **argv,
1894 const char **poptarg, int *poptind)
1896 const QEMUOption *popt;
1897 int optind = *poptind;
1898 char *r = argv[optind];
1899 const char *optarg;
1901 loc_set_cmdline(argv, optind, 1);
1902 optind++;
1903 /* Treat --foo the same as -foo. */
1904 if (r[1] == '-')
1905 r++;
1906 popt = qemu_options;
1907 for(;;) {
1908 if (!popt->name) {
1909 error_report("invalid option");
1910 exit(1);
1912 if (!strcmp(popt->name, r + 1))
1913 break;
1914 popt++;
1916 if (popt->flags & HAS_ARG) {
1917 if (optind >= argc) {
1918 error_report("requires an argument");
1919 exit(1);
1921 optarg = argv[optind++];
1922 loc_set_cmdline(argv, optind - 2, 2);
1923 } else {
1924 optarg = NULL;
1927 *poptarg = optarg;
1928 *poptind = optind;
1930 return popt;
1933 int main(int argc, char **argv, char **envp)
1935 const char *gdbstub_dev = NULL;
1936 int i;
1937 int snapshot, linux_boot;
1938 const char *icount_option = NULL;
1939 const char *initrd_filename;
1940 const char *kernel_filename, *kernel_cmdline;
1941 char boot_devices[33] = "cad"; /* default to HD->floppy->CD-ROM */
1942 DisplayState *ds;
1943 DisplayChangeListener *dcl;
1944 int cyls, heads, secs, translation;
1945 QemuOpts *hda_opts = NULL, *opts;
1946 QemuOptsList *olist;
1947 int optind;
1948 const char *optarg;
1949 const char *loadvm = NULL;
1950 QEMUMachine *machine;
1951 const char *cpu_model;
1952 int tb_size;
1953 const char *pid_file = NULL;
1954 const char *incoming = NULL;
1955 int show_vnc_port = 0;
1956 int defconfig = 1;
1958 #ifdef CONFIG_SIMPLE_TRACE
1959 const char *trace_file = NULL;
1960 #endif
1961 atexit(qemu_run_exit_notifiers);
1962 error_set_progname(argv[0]);
1964 init_clocks();
1966 qemu_cache_utils_init(envp);
1968 QLIST_INIT (&vm_change_state_head);
1969 os_setup_early_signal_handling();
1971 module_call_init(MODULE_INIT_MACHINE);
1972 machine = find_default_machine();
1973 cpu_model = NULL;
1974 initrd_filename = NULL;
1975 ram_size = 0;
1976 snapshot = 0;
1977 kernel_filename = NULL;
1978 kernel_cmdline = "";
1979 cyls = heads = secs = 0;
1980 translation = BIOS_ATA_TRANSLATION_AUTO;
1982 for (i = 0; i < MAX_NODES; i++) {
1983 node_mem[i] = 0;
1984 node_cpumask[i] = 0;
1987 nb_numa_nodes = 0;
1988 nb_nics = 0;
1990 tb_size = 0;
1991 autostart= 1;
1993 /* first pass of option parsing */
1994 optind = 1;
1995 while (optind < argc) {
1996 if (argv[optind][0] != '-') {
1997 /* disk image */
1998 optind++;
1999 continue;
2000 } else {
2001 const QEMUOption *popt;
2003 popt = lookup_opt(argc, argv, &optarg, &optind);
2004 switch (popt->index) {
2005 case QEMU_OPTION_nodefconfig:
2006 defconfig=0;
2007 break;
2012 if (defconfig) {
2013 int ret;
2015 ret = qemu_read_config_file(CONFIG_QEMU_CONFDIR "/qemu.conf");
2016 if (ret < 0 && ret != -ENOENT) {
2017 exit(1);
2020 ret = qemu_read_config_file(arch_config_name);
2021 if (ret < 0 && ret != -ENOENT) {
2022 exit(1);
2025 cpudef_init();
2027 /* second pass of option parsing */
2028 optind = 1;
2029 for(;;) {
2030 if (optind >= argc)
2031 break;
2032 if (argv[optind][0] != '-') {
2033 hda_opts = drive_add(IF_DEFAULT, 0, argv[optind++], HD_OPTS);
2034 } else {
2035 const QEMUOption *popt;
2037 popt = lookup_opt(argc, argv, &optarg, &optind);
2038 if (!(popt->arch_mask & arch_type)) {
2039 printf("Option %s not supported for this target\n", popt->name);
2040 exit(1);
2042 switch(popt->index) {
2043 case QEMU_OPTION_M:
2044 machine = find_machine(optarg);
2045 if (!machine) {
2046 QEMUMachine *m;
2047 printf("Supported machines are:\n");
2048 for(m = first_machine; m != NULL; m = m->next) {
2049 if (m->alias)
2050 printf("%-10s %s (alias of %s)\n",
2051 m->alias, m->desc, m->name);
2052 printf("%-10s %s%s\n",
2053 m->name, m->desc,
2054 m->is_default ? " (default)" : "");
2056 exit(*optarg != '?');
2058 break;
2059 case QEMU_OPTION_cpu:
2060 /* hw initialization will check this */
2061 if (*optarg == '?') {
2062 list_cpus(stdout, &fprintf, optarg);
2063 exit(0);
2064 } else {
2065 cpu_model = optarg;
2067 break;
2068 case QEMU_OPTION_initrd:
2069 initrd_filename = optarg;
2070 break;
2071 case QEMU_OPTION_hda:
2073 char buf[256];
2074 if (cyls == 0)
2075 snprintf(buf, sizeof(buf), "%s", HD_OPTS);
2076 else
2077 snprintf(buf, sizeof(buf),
2078 "%s,cyls=%d,heads=%d,secs=%d%s",
2079 HD_OPTS , cyls, heads, secs,
2080 translation == BIOS_ATA_TRANSLATION_LBA ?
2081 ",trans=lba" :
2082 translation == BIOS_ATA_TRANSLATION_NONE ?
2083 ",trans=none" : "");
2084 drive_add(IF_DEFAULT, 0, optarg, buf);
2085 break;
2087 case QEMU_OPTION_hdb:
2088 case QEMU_OPTION_hdc:
2089 case QEMU_OPTION_hdd:
2090 drive_add(IF_DEFAULT, popt->index - QEMU_OPTION_hda, optarg,
2091 HD_OPTS);
2092 break;
2093 case QEMU_OPTION_drive:
2094 drive_def(optarg);
2095 break;
2096 case QEMU_OPTION_set:
2097 if (qemu_set_option(optarg) != 0)
2098 exit(1);
2099 break;
2100 case QEMU_OPTION_global:
2101 if (qemu_global_option(optarg) != 0)
2102 exit(1);
2103 break;
2104 case QEMU_OPTION_mtdblock:
2105 drive_add(IF_MTD, -1, optarg, MTD_OPTS);
2106 break;
2107 case QEMU_OPTION_sd:
2108 drive_add(IF_SD, 0, optarg, SD_OPTS);
2109 break;
2110 case QEMU_OPTION_pflash:
2111 drive_add(IF_PFLASH, -1, optarg, PFLASH_OPTS);
2112 break;
2113 case QEMU_OPTION_snapshot:
2114 snapshot = 1;
2115 break;
2116 case QEMU_OPTION_hdachs:
2118 const char *p;
2119 p = optarg;
2120 cyls = strtol(p, (char **)&p, 0);
2121 if (cyls < 1 || cyls > 16383)
2122 goto chs_fail;
2123 if (*p != ',')
2124 goto chs_fail;
2125 p++;
2126 heads = strtol(p, (char **)&p, 0);
2127 if (heads < 1 || heads > 16)
2128 goto chs_fail;
2129 if (*p != ',')
2130 goto chs_fail;
2131 p++;
2132 secs = strtol(p, (char **)&p, 0);
2133 if (secs < 1 || secs > 63)
2134 goto chs_fail;
2135 if (*p == ',') {
2136 p++;
2137 if (!strcmp(p, "none"))
2138 translation = BIOS_ATA_TRANSLATION_NONE;
2139 else if (!strcmp(p, "lba"))
2140 translation = BIOS_ATA_TRANSLATION_LBA;
2141 else if (!strcmp(p, "auto"))
2142 translation = BIOS_ATA_TRANSLATION_AUTO;
2143 else
2144 goto chs_fail;
2145 } else if (*p != '\0') {
2146 chs_fail:
2147 fprintf(stderr, "qemu: invalid physical CHS format\n");
2148 exit(1);
2150 if (hda_opts != NULL) {
2151 char num[16];
2152 snprintf(num, sizeof(num), "%d", cyls);
2153 qemu_opt_set(hda_opts, "cyls", num);
2154 snprintf(num, sizeof(num), "%d", heads);
2155 qemu_opt_set(hda_opts, "heads", num);
2156 snprintf(num, sizeof(num), "%d", secs);
2157 qemu_opt_set(hda_opts, "secs", num);
2158 if (translation == BIOS_ATA_TRANSLATION_LBA)
2159 qemu_opt_set(hda_opts, "trans", "lba");
2160 if (translation == BIOS_ATA_TRANSLATION_NONE)
2161 qemu_opt_set(hda_opts, "trans", "none");
2164 break;
2165 case QEMU_OPTION_numa:
2166 if (nb_numa_nodes >= MAX_NODES) {
2167 fprintf(stderr, "qemu: too many NUMA nodes\n");
2168 exit(1);
2170 numa_add(optarg);
2171 break;
2172 case QEMU_OPTION_nographic:
2173 display_type = DT_NOGRAPHIC;
2174 break;
2175 #ifdef CONFIG_CURSES
2176 case QEMU_OPTION_curses:
2177 display_type = DT_CURSES;
2178 break;
2179 #endif
2180 case QEMU_OPTION_portrait:
2181 graphic_rotate = 1;
2182 break;
2183 case QEMU_OPTION_kernel:
2184 kernel_filename = optarg;
2185 break;
2186 case QEMU_OPTION_append:
2187 kernel_cmdline = optarg;
2188 break;
2189 case QEMU_OPTION_cdrom:
2190 drive_add(IF_DEFAULT, 2, optarg, CDROM_OPTS);
2191 break;
2192 case QEMU_OPTION_boot:
2194 static const char * const params[] = {
2195 "order", "once", "menu", NULL
2197 char buf[sizeof(boot_devices)];
2198 char *standard_boot_devices;
2199 int legacy = 0;
2201 if (!strchr(optarg, '=')) {
2202 legacy = 1;
2203 pstrcpy(buf, sizeof(buf), optarg);
2204 } else if (check_params(buf, sizeof(buf), params, optarg) < 0) {
2205 fprintf(stderr,
2206 "qemu: unknown boot parameter '%s' in '%s'\n",
2207 buf, optarg);
2208 exit(1);
2211 if (legacy ||
2212 get_param_value(buf, sizeof(buf), "order", optarg)) {
2213 validate_bootdevices(buf);
2214 pstrcpy(boot_devices, sizeof(boot_devices), buf);
2216 if (!legacy) {
2217 if (get_param_value(buf, sizeof(buf),
2218 "once", optarg)) {
2219 validate_bootdevices(buf);
2220 standard_boot_devices = qemu_strdup(boot_devices);
2221 pstrcpy(boot_devices, sizeof(boot_devices), buf);
2222 qemu_register_reset(restore_boot_devices,
2223 standard_boot_devices);
2225 if (get_param_value(buf, sizeof(buf),
2226 "menu", optarg)) {
2227 if (!strcmp(buf, "on")) {
2228 boot_menu = 1;
2229 } else if (!strcmp(buf, "off")) {
2230 boot_menu = 0;
2231 } else {
2232 fprintf(stderr,
2233 "qemu: invalid option value '%s'\n",
2234 buf);
2235 exit(1);
2240 break;
2241 case QEMU_OPTION_fda:
2242 case QEMU_OPTION_fdb:
2243 drive_add(IF_FLOPPY, popt->index - QEMU_OPTION_fda,
2244 optarg, FD_OPTS);
2245 break;
2246 case QEMU_OPTION_no_fd_bootchk:
2247 fd_bootchk = 0;
2248 break;
2249 case QEMU_OPTION_netdev:
2250 if (net_client_parse(qemu_find_opts("netdev"), optarg) == -1) {
2251 exit(1);
2253 break;
2254 case QEMU_OPTION_net:
2255 if (net_client_parse(qemu_find_opts("net"), optarg) == -1) {
2256 exit(1);
2258 break;
2259 #ifdef CONFIG_SLIRP
2260 case QEMU_OPTION_tftp:
2261 legacy_tftp_prefix = optarg;
2262 break;
2263 case QEMU_OPTION_bootp:
2264 legacy_bootp_filename = optarg;
2265 break;
2266 case QEMU_OPTION_redir:
2267 if (net_slirp_redir(optarg) < 0)
2268 exit(1);
2269 break;
2270 #endif
2271 case QEMU_OPTION_bt:
2272 add_device_config(DEV_BT, optarg);
2273 break;
2274 case QEMU_OPTION_audio_help:
2275 if (!(audio_available())) {
2276 printf("Option %s not supported for this target\n", popt->name);
2277 exit(1);
2279 AUD_help ();
2280 exit (0);
2281 break;
2282 case QEMU_OPTION_soundhw:
2283 if (!(audio_available())) {
2284 printf("Option %s not supported for this target\n", popt->name);
2285 exit(1);
2287 select_soundhw (optarg);
2288 break;
2289 case QEMU_OPTION_h:
2290 help(0);
2291 break;
2292 case QEMU_OPTION_version:
2293 version();
2294 exit(0);
2295 break;
2296 case QEMU_OPTION_m: {
2297 int64_t value;
2299 value = strtosz(optarg, NULL);
2300 if (value < 0) {
2301 fprintf(stderr, "qemu: invalid ram size: %s\n", optarg);
2302 exit(1);
2305 /* On 32-bit hosts, QEMU is limited by virtual address space */
2306 if (value > (2047 << 20) && HOST_LONG_BITS == 32) {
2307 fprintf(stderr, "qemu: at most 2047 MB RAM can be simulated\n");
2308 exit(1);
2310 if (value != (uint64_t)(ram_addr_t)value) {
2311 fprintf(stderr, "qemu: ram size too large\n");
2312 exit(1);
2314 ram_size = value;
2315 break;
2317 case QEMU_OPTION_mempath:
2318 mem_path = optarg;
2319 break;
2320 #ifdef MAP_POPULATE
2321 case QEMU_OPTION_mem_prealloc:
2322 mem_prealloc = 1;
2323 break;
2324 #endif
2325 case QEMU_OPTION_d:
2326 set_cpu_log(optarg);
2327 break;
2328 case QEMU_OPTION_s:
2329 gdbstub_dev = "tcp::" DEFAULT_GDBSTUB_PORT;
2330 break;
2331 case QEMU_OPTION_gdb:
2332 gdbstub_dev = optarg;
2333 break;
2334 case QEMU_OPTION_L:
2335 data_dir = optarg;
2336 break;
2337 case QEMU_OPTION_bios:
2338 bios_name = optarg;
2339 break;
2340 case QEMU_OPTION_singlestep:
2341 singlestep = 1;
2342 break;
2343 case QEMU_OPTION_S:
2344 autostart = 0;
2345 break;
2346 case QEMU_OPTION_k:
2347 keyboard_layout = optarg;
2348 break;
2349 case QEMU_OPTION_localtime:
2350 rtc_utc = 0;
2351 break;
2352 case QEMU_OPTION_vga:
2353 select_vgahw (optarg);
2354 break;
2355 case QEMU_OPTION_g:
2357 const char *p;
2358 int w, h, depth;
2359 p = optarg;
2360 w = strtol(p, (char **)&p, 10);
2361 if (w <= 0) {
2362 graphic_error:
2363 fprintf(stderr, "qemu: invalid resolution or depth\n");
2364 exit(1);
2366 if (*p != 'x')
2367 goto graphic_error;
2368 p++;
2369 h = strtol(p, (char **)&p, 10);
2370 if (h <= 0)
2371 goto graphic_error;
2372 if (*p == 'x') {
2373 p++;
2374 depth = strtol(p, (char **)&p, 10);
2375 if (depth != 8 && depth != 15 && depth != 16 &&
2376 depth != 24 && depth != 32)
2377 goto graphic_error;
2378 } else if (*p == '\0') {
2379 depth = graphic_depth;
2380 } else {
2381 goto graphic_error;
2384 graphic_width = w;
2385 graphic_height = h;
2386 graphic_depth = depth;
2388 break;
2389 case QEMU_OPTION_echr:
2391 char *r;
2392 term_escape_char = strtol(optarg, &r, 0);
2393 if (r == optarg)
2394 printf("Bad argument to echr\n");
2395 break;
2397 case QEMU_OPTION_monitor:
2398 monitor_parse(optarg, "readline");
2399 default_monitor = 0;
2400 break;
2401 case QEMU_OPTION_qmp:
2402 monitor_parse(optarg, "control");
2403 default_monitor = 0;
2404 break;
2405 case QEMU_OPTION_mon:
2406 opts = qemu_opts_parse(qemu_find_opts("mon"), optarg, 1);
2407 if (!opts) {
2408 exit(1);
2410 default_monitor = 0;
2411 break;
2412 case QEMU_OPTION_chardev:
2413 opts = qemu_opts_parse(qemu_find_opts("chardev"), optarg, 1);
2414 if (!opts) {
2415 exit(1);
2417 break;
2418 case QEMU_OPTION_fsdev:
2419 olist = qemu_find_opts("fsdev");
2420 if (!olist) {
2421 fprintf(stderr, "fsdev is not supported by this qemu build.\n");
2422 exit(1);
2424 opts = qemu_opts_parse(olist, optarg, 1);
2425 if (!opts) {
2426 fprintf(stderr, "parse error: %s\n", optarg);
2427 exit(1);
2429 break;
2430 case QEMU_OPTION_virtfs: {
2431 char *arg_fsdev = NULL;
2432 char *arg_9p = NULL;
2433 int len = 0;
2435 olist = qemu_find_opts("virtfs");
2436 if (!olist) {
2437 fprintf(stderr, "virtfs is not supported by this qemu build.\n");
2438 exit(1);
2440 opts = qemu_opts_parse(olist, optarg, 1);
2441 if (!opts) {
2442 fprintf(stderr, "parse error: %s\n", optarg);
2443 exit(1);
2446 if (qemu_opt_get(opts, "fstype") == NULL ||
2447 qemu_opt_get(opts, "mount_tag") == NULL ||
2448 qemu_opt_get(opts, "path") == NULL ||
2449 qemu_opt_get(opts, "security_model") == NULL) {
2450 fprintf(stderr, "Usage: -virtfs fstype,path=/share_path/,"
2451 "security_model=[mapped|passthrough|none],"
2452 "mnt_tag=tag.\n");
2453 exit(1);
2456 len = strlen(",id=,path=,security_model=");
2457 len += strlen(qemu_opt_get(opts, "fstype"));
2458 len += strlen(qemu_opt_get(opts, "mount_tag"));
2459 len += strlen(qemu_opt_get(opts, "path"));
2460 len += strlen(qemu_opt_get(opts, "security_model"));
2461 arg_fsdev = qemu_malloc((len + 1) * sizeof(*arg_fsdev));
2463 snprintf(arg_fsdev, (len + 1) * sizeof(*arg_fsdev),
2464 "%s,id=%s,path=%s,security_model=%s",
2465 qemu_opt_get(opts, "fstype"),
2466 qemu_opt_get(opts, "mount_tag"),
2467 qemu_opt_get(opts, "path"),
2468 qemu_opt_get(opts, "security_model"));
2470 len = strlen("virtio-9p-pci,fsdev=,mount_tag=");
2471 len += 2*strlen(qemu_opt_get(opts, "mount_tag"));
2472 arg_9p = qemu_malloc((len + 1) * sizeof(*arg_9p));
2474 snprintf(arg_9p, (len + 1) * sizeof(*arg_9p),
2475 "virtio-9p-pci,fsdev=%s,mount_tag=%s",
2476 qemu_opt_get(opts, "mount_tag"),
2477 qemu_opt_get(opts, "mount_tag"));
2479 if (!qemu_opts_parse(qemu_find_opts("fsdev"), arg_fsdev, 1)) {
2480 fprintf(stderr, "parse error [fsdev]: %s\n", optarg);
2481 exit(1);
2484 if (!qemu_opts_parse(qemu_find_opts("device"), arg_9p, 1)) {
2485 fprintf(stderr, "parse error [device]: %s\n", optarg);
2486 exit(1);
2489 qemu_free(arg_fsdev);
2490 qemu_free(arg_9p);
2491 break;
2493 case QEMU_OPTION_serial:
2494 add_device_config(DEV_SERIAL, optarg);
2495 default_serial = 0;
2496 if (strncmp(optarg, "mon:", 4) == 0) {
2497 default_monitor = 0;
2499 break;
2500 case QEMU_OPTION_watchdog:
2501 if (watchdog) {
2502 fprintf(stderr,
2503 "qemu: only one watchdog option may be given\n");
2504 return 1;
2506 watchdog = optarg;
2507 break;
2508 case QEMU_OPTION_watchdog_action:
2509 if (select_watchdog_action(optarg) == -1) {
2510 fprintf(stderr, "Unknown -watchdog-action parameter\n");
2511 exit(1);
2513 break;
2514 case QEMU_OPTION_virtiocon:
2515 add_device_config(DEV_VIRTCON, optarg);
2516 default_virtcon = 0;
2517 if (strncmp(optarg, "mon:", 4) == 0) {
2518 default_monitor = 0;
2520 break;
2521 case QEMU_OPTION_parallel:
2522 add_device_config(DEV_PARALLEL, optarg);
2523 default_parallel = 0;
2524 if (strncmp(optarg, "mon:", 4) == 0) {
2525 default_monitor = 0;
2527 break;
2528 case QEMU_OPTION_debugcon:
2529 add_device_config(DEV_DEBUGCON, optarg);
2530 break;
2531 case QEMU_OPTION_loadvm:
2532 loadvm = optarg;
2533 break;
2534 case QEMU_OPTION_full_screen:
2535 full_screen = 1;
2536 break;
2537 #ifdef CONFIG_SDL
2538 case QEMU_OPTION_no_frame:
2539 no_frame = 1;
2540 break;
2541 case QEMU_OPTION_alt_grab:
2542 alt_grab = 1;
2543 break;
2544 case QEMU_OPTION_ctrl_grab:
2545 ctrl_grab = 1;
2546 break;
2547 case QEMU_OPTION_no_quit:
2548 no_quit = 1;
2549 break;
2550 case QEMU_OPTION_sdl:
2551 display_type = DT_SDL;
2552 break;
2553 #endif
2554 case QEMU_OPTION_pidfile:
2555 pid_file = optarg;
2556 break;
2557 case QEMU_OPTION_win2k_hack:
2558 win2k_install_hack = 1;
2559 break;
2560 case QEMU_OPTION_rtc_td_hack:
2561 rtc_td_hack = 1;
2562 break;
2563 case QEMU_OPTION_acpitable:
2564 do_acpitable_option(optarg);
2565 break;
2566 case QEMU_OPTION_smbios:
2567 do_smbios_option(optarg);
2568 break;
2569 case QEMU_OPTION_enable_kvm:
2570 kvm_allowed = 1;
2571 break;
2572 case QEMU_OPTION_no_kvm:
2573 kvm_allowed = 0;
2574 #ifdef CONFIG_NO_CPU_EMULATION
2575 fprintf(stderr, "cpu emulation not configured\n");
2576 exit(1);
2577 #endif
2578 break;
2579 #ifdef CONFIG_KVM
2580 case QEMU_OPTION_no_kvm_irqchip: {
2581 kvm_irqchip = 0;
2582 kvm_pit = 0;
2583 break;
2585 case QEMU_OPTION_no_kvm_pit: {
2586 kvm_pit = 0;
2587 break;
2589 case QEMU_OPTION_no_kvm_pit_reinjection: {
2590 kvm_pit_reinject = 0;
2591 break;
2593 case QEMU_OPTION_enable_nesting: {
2594 kvm_nested = 1;
2595 break;
2597 #endif
2598 case QEMU_OPTION_usb:
2599 usb_enabled = 1;
2600 break;
2601 case QEMU_OPTION_usbdevice:
2602 usb_enabled = 1;
2603 add_device_config(DEV_USB, optarg);
2604 break;
2605 case QEMU_OPTION_device:
2606 if (!qemu_opts_parse(qemu_find_opts("device"), optarg, 1)) {
2607 exit(1);
2609 break;
2610 case QEMU_OPTION_smp:
2611 smp_parse(optarg);
2612 if (smp_cpus < 1) {
2613 fprintf(stderr, "Invalid number of CPUs\n");
2614 exit(1);
2616 if (max_cpus < smp_cpus) {
2617 fprintf(stderr, "maxcpus must be equal to or greater than "
2618 "smp\n");
2619 exit(1);
2621 if (max_cpus > 255) {
2622 fprintf(stderr, "Unsupported number of maxcpus\n");
2623 exit(1);
2625 break;
2626 case QEMU_OPTION_vnc:
2627 display_remote++;
2628 vnc_display = optarg;
2629 break;
2630 case QEMU_OPTION_no_acpi:
2631 acpi_enabled = 0;
2632 break;
2633 case QEMU_OPTION_no_hpet:
2634 no_hpet = 1;
2635 break;
2636 case QEMU_OPTION_balloon:
2637 if (balloon_parse(optarg) < 0) {
2638 fprintf(stderr, "Unknown -balloon argument %s\n", optarg);
2639 exit(1);
2641 break;
2642 case QEMU_OPTION_no_reboot:
2643 no_reboot = 1;
2644 break;
2645 case QEMU_OPTION_no_shutdown:
2646 no_shutdown = 1;
2647 break;
2648 case QEMU_OPTION_show_cursor:
2649 cursor_hide = 0;
2650 break;
2651 case QEMU_OPTION_uuid:
2652 if(qemu_uuid_parse(optarg, qemu_uuid) < 0) {
2653 fprintf(stderr, "Fail to parse UUID string."
2654 " Wrong format.\n");
2655 exit(1);
2657 break;
2658 case QEMU_OPTION_option_rom:
2659 if (nb_option_roms >= MAX_OPTION_ROMS) {
2660 fprintf(stderr, "Too many option ROMs\n");
2661 exit(1);
2663 opts = qemu_opts_parse(qemu_find_opts("option-rom"), optarg, 1);
2664 option_rom[nb_option_roms].name = qemu_opt_get(opts, "romfile");
2665 option_rom[nb_option_roms].bootindex =
2666 qemu_opt_get_number(opts, "bootindex", -1);
2667 if (!option_rom[nb_option_roms].name) {
2668 fprintf(stderr, "Option ROM file is not specified\n");
2669 exit(1);
2671 nb_option_roms++;
2672 break;
2673 case QEMU_OPTION_semihosting:
2674 semihosting_enabled = 1;
2675 break;
2676 case QEMU_OPTION_tdf:
2677 time_drift_fix = 1;
2678 break;
2679 case QEMU_OPTION_kvm_shadow_memory:
2680 kvm_shadow_memory = (int64_t)atoi(optarg) * 1024 * 1024 / 4096;
2681 break;
2682 case QEMU_OPTION_name:
2683 qemu_name = qemu_strdup(optarg);
2685 char *p = strchr(qemu_name, ',');
2686 if (p != NULL) {
2687 *p++ = 0;
2688 if (strncmp(p, "process=", 8)) {
2689 fprintf(stderr, "Unknown subargument %s to -name\n", p);
2690 exit(1);
2692 p += 8;
2693 os_set_proc_name(p);
2696 break;
2697 case QEMU_OPTION_prom_env:
2698 if (nb_prom_envs >= MAX_PROM_ENVS) {
2699 fprintf(stderr, "Too many prom variables\n");
2700 exit(1);
2702 prom_envs[nb_prom_envs] = optarg;
2703 nb_prom_envs++;
2704 break;
2705 case QEMU_OPTION_old_param:
2706 old_param = 1;
2707 break;
2708 case QEMU_OPTION_clock:
2709 configure_alarms(optarg);
2710 break;
2711 case QEMU_OPTION_startdate:
2712 configure_rtc_date_offset(optarg, 1);
2713 break;
2714 case QEMU_OPTION_rtc:
2715 opts = qemu_opts_parse(qemu_find_opts("rtc"), optarg, 0);
2716 if (!opts) {
2717 exit(1);
2719 configure_rtc(opts);
2720 break;
2721 case QEMU_OPTION_tb_size:
2722 tb_size = strtol(optarg, NULL, 0);
2723 if (tb_size < 0)
2724 tb_size = 0;
2725 break;
2726 case QEMU_OPTION_icount:
2727 icount_option = optarg;
2728 break;
2729 case QEMU_OPTION_incoming:
2730 incoming = optarg;
2731 incoming_expected = true;
2732 break;
2733 case QEMU_OPTION_nodefaults:
2734 default_serial = 0;
2735 default_parallel = 0;
2736 default_virtcon = 0;
2737 default_monitor = 0;
2738 default_vga = 0;
2739 default_net = 0;
2740 default_floppy = 0;
2741 default_cdrom = 0;
2742 default_sdcard = 0;
2743 break;
2744 #ifndef _WIN32
2745 case QEMU_OPTION_nvram:
2746 nvram = optarg;
2747 break;
2748 #endif
2749 case QEMU_OPTION_xen_domid:
2750 if (!(xen_available())) {
2751 printf("Option %s not supported for this target\n", popt->name);
2752 exit(1);
2754 xen_domid = atoi(optarg);
2755 break;
2756 case QEMU_OPTION_xen_create:
2757 if (!(xen_available())) {
2758 printf("Option %s not supported for this target\n", popt->name);
2759 exit(1);
2761 xen_mode = XEN_CREATE;
2762 break;
2763 case QEMU_OPTION_xen_attach:
2764 if (!(xen_available())) {
2765 printf("Option %s not supported for this target\n", popt->name);
2766 exit(1);
2768 xen_mode = XEN_ATTACH;
2769 break;
2770 #ifdef CONFIG_SIMPLE_TRACE
2771 case QEMU_OPTION_trace:
2772 opts = qemu_opts_parse(qemu_find_opts("trace"), optarg, 0);
2773 if (opts) {
2774 trace_file = qemu_opt_get(opts, "file");
2776 break;
2777 #endif
2778 case QEMU_OPTION_readconfig:
2780 int ret = qemu_read_config_file(optarg);
2781 if (ret < 0) {
2782 fprintf(stderr, "read config %s: %s\n", optarg,
2783 strerror(-ret));
2784 exit(1);
2786 break;
2788 case QEMU_OPTION_spice:
2789 olist = qemu_find_opts("spice");
2790 if (!olist) {
2791 fprintf(stderr, "spice is not supported by this qemu build.\n");
2792 exit(1);
2794 opts = qemu_opts_parse(olist, optarg, 0);
2795 if (!opts) {
2796 fprintf(stderr, "parse error: %s\n", optarg);
2797 exit(1);
2799 break;
2800 case QEMU_OPTION_writeconfig:
2802 FILE *fp;
2803 if (strcmp(optarg, "-") == 0) {
2804 fp = stdout;
2805 } else {
2806 fp = fopen(optarg, "w");
2807 if (fp == NULL) {
2808 fprintf(stderr, "open %s: %s\n", optarg, strerror(errno));
2809 exit(1);
2812 qemu_config_write(fp);
2813 fclose(fp);
2814 break;
2816 default:
2817 os_parse_cmd_args(popt->index, optarg);
2821 loc_set_none();
2823 /* If no data_dir is specified then try to find it relative to the
2824 executable path. */
2825 if (!data_dir) {
2826 data_dir = os_find_datadir(argv[0]);
2828 /* If all else fails use the install patch specified when building. */
2829 if (!data_dir) {
2830 data_dir = CONFIG_QEMU_DATADIR;
2833 #ifdef CONFIG_SIMPLE_TRACE
2835 * Set the trace file name, if specified.
2837 st_set_trace_file(trace_file);
2838 #endif
2840 * Default to max_cpus = smp_cpus, in case the user doesn't
2841 * specify a max_cpus value.
2843 if (!max_cpus)
2844 max_cpus = smp_cpus;
2846 machine->max_cpus = machine->max_cpus ?: 1; /* Default to UP */
2847 if (smp_cpus > machine->max_cpus) {
2848 fprintf(stderr, "Number of SMP cpus requested (%d), exceeds max cpus "
2849 "supported by machine `%s' (%d)\n", smp_cpus, machine->name,
2850 machine->max_cpus);
2851 exit(1);
2854 qemu_opts_foreach(qemu_find_opts("device"), default_driver_check, NULL, 0);
2855 qemu_opts_foreach(qemu_find_opts("global"), default_driver_check, NULL, 0);
2857 if (machine->no_serial) {
2858 default_serial = 0;
2860 if (machine->no_parallel) {
2861 default_parallel = 0;
2863 if (!machine->use_virtcon) {
2864 default_virtcon = 0;
2866 if (machine->no_vga) {
2867 default_vga = 0;
2869 if (machine->no_floppy) {
2870 default_floppy = 0;
2872 if (machine->no_cdrom) {
2873 default_cdrom = 0;
2875 if (machine->no_sdcard) {
2876 default_sdcard = 0;
2879 if (display_type == DT_NOGRAPHIC) {
2880 if (default_parallel)
2881 add_device_config(DEV_PARALLEL, "null");
2882 if (default_serial && default_monitor) {
2883 add_device_config(DEV_SERIAL, "mon:stdio");
2884 } else if (default_virtcon && default_monitor) {
2885 add_device_config(DEV_VIRTCON, "mon:stdio");
2886 } else {
2887 if (default_serial)
2888 add_device_config(DEV_SERIAL, "stdio");
2889 if (default_virtcon)
2890 add_device_config(DEV_VIRTCON, "stdio");
2891 if (default_monitor)
2892 monitor_parse("stdio", "readline");
2894 } else {
2895 if (default_serial)
2896 add_device_config(DEV_SERIAL, "vc:80Cx24C");
2897 if (default_parallel)
2898 add_device_config(DEV_PARALLEL, "vc:80Cx24C");
2899 if (default_monitor)
2900 monitor_parse("vc:80Cx24C", "readline");
2901 if (default_virtcon)
2902 add_device_config(DEV_VIRTCON, "vc:80Cx24C");
2904 if (default_vga)
2905 vga_interface_type = VGA_CIRRUS;
2907 socket_init();
2909 if (qemu_opts_foreach(qemu_find_opts("chardev"), chardev_init_func, NULL, 1) != 0)
2910 exit(1);
2911 #ifdef CONFIG_VIRTFS
2912 if (qemu_opts_foreach(qemu_find_opts("fsdev"), fsdev_init_func, NULL, 1) != 0) {
2913 exit(1);
2915 #endif
2917 os_daemonize();
2919 if (pid_file && qemu_create_pidfile(pid_file) != 0) {
2920 os_pidfile_error();
2921 exit(1);
2924 if (kvm_allowed) {
2925 int ret = kvm_init();
2926 if (ret < 0) {
2927 if (kvm_allowed > 0) {
2928 if (!kvm_available()) {
2929 printf("KVM not supported for this target\n");
2930 } else {
2931 fprintf(stderr, "failed to initialize KVM: %s\n", strerror(-ret));
2933 exit(1);
2935 fprintf(stderr, "Could not initialize KVM, will disable KVM support\n");
2937 kvm_allowed = ret >= 0;
2940 if (qemu_init_main_loop()) {
2941 fprintf(stderr, "qemu_init_main_loop failed\n");
2942 exit(1);
2944 linux_boot = (kernel_filename != NULL);
2946 if (!linux_boot && *kernel_cmdline != '\0') {
2947 fprintf(stderr, "-append only allowed with -kernel option\n");
2948 exit(1);
2951 if (!linux_boot && initrd_filename != NULL) {
2952 fprintf(stderr, "-initrd only allowed with -kernel option\n");
2953 exit(1);
2956 os_set_line_buffering();
2958 if (init_timer_alarm() < 0) {
2959 fprintf(stderr, "could not initialize alarm timer\n");
2960 exit(1);
2962 configure_icount(icount_option);
2964 if (net_init_clients() < 0) {
2965 exit(1);
2968 /* init the bluetooth world */
2969 if (foreach_device_config(DEV_BT, bt_parse))
2970 exit(1);
2972 /* init the memory */
2973 if (ram_size == 0)
2974 ram_size = DEFAULT_RAM_SIZE * 1024 * 1024;
2976 /* init the dynamic translator */
2977 cpu_exec_init_all(tb_size * 1024 * 1024);
2979 bdrv_init_with_whitelist();
2981 blk_mig_init();
2983 /* open the virtual block devices */
2984 if (snapshot)
2985 qemu_opts_foreach(qemu_find_opts("drive"), drive_enable_snapshot, NULL, 0);
2986 if (qemu_opts_foreach(qemu_find_opts("drive"), drive_init_func, &machine->use_scsi, 1) != 0)
2987 exit(1);
2989 default_drive(default_cdrom, snapshot, machine->use_scsi,
2990 IF_DEFAULT, 2, CDROM_OPTS);
2991 default_drive(default_floppy, snapshot, machine->use_scsi,
2992 IF_FLOPPY, 0, FD_OPTS);
2993 default_drive(default_sdcard, snapshot, machine->use_scsi,
2994 IF_SD, 0, SD_OPTS);
2996 register_savevm_live(NULL, "ram", 0, 4, NULL, ram_save_live, NULL,
2997 ram_load, NULL);
2999 if (nb_numa_nodes > 0) {
3000 int i;
3002 if (nb_numa_nodes > smp_cpus) {
3003 nb_numa_nodes = smp_cpus;
3006 /* If no memory size if given for any node, assume the default case
3007 * and distribute the available memory equally across all nodes
3009 for (i = 0; i < nb_numa_nodes; i++) {
3010 if (node_mem[i] != 0)
3011 break;
3013 if (i == nb_numa_nodes) {
3014 uint64_t usedmem = 0;
3016 /* On Linux, the each node's border has to be 8MB aligned,
3017 * the final node gets the rest.
3019 for (i = 0; i < nb_numa_nodes - 1; i++) {
3020 node_mem[i] = (ram_size / nb_numa_nodes) & ~((1 << 23UL) - 1);
3021 usedmem += node_mem[i];
3023 node_mem[i] = ram_size - usedmem;
3026 for (i = 0; i < nb_numa_nodes; i++) {
3027 if (node_cpumask[i] != 0)
3028 break;
3030 /* assigning the VCPUs round-robin is easier to implement, guest OSes
3031 * must cope with this anyway, because there are BIOSes out there in
3032 * real machines which also use this scheme.
3034 if (i == nb_numa_nodes) {
3035 for (i = 0; i < smp_cpus; i++) {
3036 node_cpumask[i % nb_numa_nodes] |= 1 << i;
3041 if (qemu_opts_foreach(qemu_find_opts("mon"), mon_init_func, NULL, 1) != 0) {
3042 exit(1);
3045 if (foreach_device_config(DEV_SERIAL, serial_parse) < 0)
3046 exit(1);
3047 if (foreach_device_config(DEV_PARALLEL, parallel_parse) < 0)
3048 exit(1);
3049 if (foreach_device_config(DEV_VIRTCON, virtcon_parse) < 0)
3050 exit(1);
3051 if (foreach_device_config(DEV_DEBUGCON, debugcon_parse) < 0)
3052 exit(1);
3054 module_call_init(MODULE_INIT_DEVICE);
3056 if (qemu_opts_foreach(qemu_find_opts("device"), device_help_func, NULL, 0) != 0)
3057 exit(0);
3059 if (watchdog) {
3060 i = select_watchdog(watchdog);
3061 if (i > 0)
3062 exit (i == 1 ? 1 : 0);
3065 if (machine->compat_props) {
3066 qdev_prop_register_global_list(machine->compat_props);
3068 qemu_add_globals();
3070 machine->init(ram_size, boot_devices,
3071 kernel_filename, kernel_cmdline, initrd_filename, cpu_model);
3073 cpu_synchronize_all_post_init();
3075 /* must be after terminal init, SDL library changes signal handlers */
3076 os_setup_signal_handling();
3078 set_numa_modes();
3080 current_machine = machine;
3082 /* init USB devices */
3083 if (usb_enabled) {
3084 if (foreach_device_config(DEV_USB, usb_parse) < 0)
3085 exit(1);
3088 /* init generic devices */
3089 if (qemu_opts_foreach(qemu_find_opts("device"), device_init_func, NULL, 1) != 0)
3090 exit(1);
3092 net_check_clients();
3094 /* just use the first displaystate for the moment */
3095 ds = get_displaystate();
3097 if (using_spice)
3098 display_remote++;
3099 if (display_type == DT_DEFAULT && !display_remote) {
3100 #if defined(CONFIG_SDL) || defined(CONFIG_COCOA)
3101 display_type = DT_SDL;
3102 #else
3103 vnc_display = "localhost:0,to=99";
3104 show_vnc_port = 1;
3105 #endif
3109 /* init local displays */
3110 switch (display_type) {
3111 case DT_NOGRAPHIC:
3112 break;
3113 #if defined(CONFIG_CURSES)
3114 case DT_CURSES:
3115 curses_display_init(ds, full_screen);
3116 break;
3117 #endif
3118 #if defined(CONFIG_SDL)
3119 case DT_SDL:
3120 sdl_display_init(ds, full_screen, no_frame);
3121 break;
3122 #elif defined(CONFIG_COCOA)
3123 case DT_SDL:
3124 cocoa_display_init(ds, full_screen);
3125 break;
3126 #endif
3127 default:
3128 break;
3131 /* init remote displays */
3132 if (vnc_display) {
3133 vnc_display_init(ds);
3134 if (vnc_display_open(ds, vnc_display) < 0)
3135 exit(1);
3137 if (show_vnc_port) {
3138 printf("VNC server running on `%s'\n", vnc_display_local_addr(ds));
3141 #ifdef CONFIG_SPICE
3142 if (using_spice && !qxl_enabled) {
3143 qemu_spice_display_init(ds);
3145 #endif
3147 /* display setup */
3148 dpy_resize(ds);
3149 dcl = ds->listeners;
3150 while (dcl != NULL) {
3151 if (dcl->dpy_refresh != NULL) {
3152 ds->gui_timer = qemu_new_timer(rt_clock, gui_update, ds);
3153 qemu_mod_timer(ds->gui_timer, qemu_get_clock(rt_clock));
3154 break;
3156 dcl = dcl->next;
3158 if (ds->gui_timer == NULL) {
3159 nographic_timer = qemu_new_timer(rt_clock, nographic_update, NULL);
3160 qemu_mod_timer(nographic_timer, qemu_get_clock(rt_clock));
3162 text_consoles_set_display(ds);
3164 if (gdbstub_dev && gdbserver_start(gdbstub_dev) < 0) {
3165 fprintf(stderr, "qemu: could not open gdbserver on device '%s'\n",
3166 gdbstub_dev);
3167 exit(1);
3170 qdev_machine_creation_done();
3172 if (rom_load_all() != 0) {
3173 fprintf(stderr, "rom loading failed\n");
3174 exit(1);
3177 /* TODO: once all bus devices are qdevified, this should be done
3178 * when bus is created by qdev.c */
3179 qemu_register_reset(qbus_reset_all_fn, sysbus_get_default());
3180 qemu_run_machine_init_done_notifiers();
3182 qemu_system_reset();
3183 if (loadvm) {
3184 if (load_vmstate(loadvm) < 0) {
3185 autostart = 0;
3189 if (incoming) {
3190 int ret = qemu_start_incoming_migration(incoming);
3191 if (ret < 0) {
3192 fprintf(stderr, "Migration failed. Exit code %s(%d), exiting.\n",
3193 incoming, ret);
3194 exit(ret);
3196 } else if (autostart) {
3197 vm_start();
3200 os_setup_post();
3202 main_loop();
3203 quit_timers();
3204 net_cleanup();
3206 return 0;