json-lexer: make lexer error-recovery more deterministic
[qemu/mdroth.git] / vl.c
blob04b4a20ecc25328be222149b03090f9bce73aa98
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 static int tcg_allowed = 1;
261 int kvm_allowed = 0;
262 int xen_allowed = 0;
263 uint32_t xen_domid;
264 enum xen_mode xen_mode = XEN_EMULATE;
266 static int default_serial = 1;
267 static int default_parallel = 1;
268 static int default_virtcon = 1;
269 static int default_monitor = 1;
270 static int default_vga = 1;
271 static int default_floppy = 1;
272 static int default_cdrom = 1;
273 static int default_sdcard = 1;
275 static struct {
276 const char *driver;
277 int *flag;
278 } default_list[] = {
279 { .driver = "isa-serial", .flag = &default_serial },
280 { .driver = "isa-parallel", .flag = &default_parallel },
281 { .driver = "isa-fdc", .flag = &default_floppy },
282 { .driver = "ide-cd", .flag = &default_cdrom },
283 { .driver = "ide-hd", .flag = &default_cdrom },
284 { .driver = "ide-drive", .flag = &default_cdrom },
285 { .driver = "scsi-cd", .flag = &default_cdrom },
286 { .driver = "virtio-serial-pci", .flag = &default_virtcon },
287 { .driver = "virtio-serial-s390", .flag = &default_virtcon },
288 { .driver = "virtio-serial", .flag = &default_virtcon },
289 { .driver = "VGA", .flag = &default_vga },
290 { .driver = "cirrus-vga", .flag = &default_vga },
291 { .driver = "vmware-svga", .flag = &default_vga },
292 { .driver = "isa-vga", .flag = &default_vga },
295 static int default_driver_check(QemuOpts *opts, void *opaque)
297 const char *driver = qemu_opt_get(opts, "driver");
298 int i;
300 if (!driver)
301 return 0;
302 for (i = 0; i < ARRAY_SIZE(default_list); i++) {
303 if (strcmp(default_list[i].driver, driver) != 0)
304 continue;
305 *(default_list[i].flag) = 0;
307 return 0;
310 /***********************************************************/
311 /* real time host monotonic timer */
313 /***********************************************************/
314 /* host time/date access */
315 void qemu_get_timedate(struct tm *tm, int offset)
317 time_t ti;
318 struct tm *ret;
320 time(&ti);
321 ti += offset;
322 if (rtc_date_offset == -1) {
323 if (rtc_utc)
324 ret = gmtime(&ti);
325 else
326 ret = localtime(&ti);
327 } else {
328 ti -= rtc_date_offset;
329 ret = gmtime(&ti);
332 memcpy(tm, ret, sizeof(struct tm));
335 int qemu_timedate_diff(struct tm *tm)
337 time_t seconds;
339 if (rtc_date_offset == -1)
340 if (rtc_utc)
341 seconds = mktimegm(tm);
342 else
343 seconds = mktime(tm);
344 else
345 seconds = mktimegm(tm) + rtc_date_offset;
347 return seconds - time(NULL);
350 void rtc_change_mon_event(struct tm *tm)
352 QObject *data;
354 data = qobject_from_jsonf("{ 'offset': %d }", qemu_timedate_diff(tm));
355 monitor_protocol_event(QEVENT_RTC_CHANGE, data);
356 qobject_decref(data);
359 static void configure_rtc_date_offset(const char *startdate, int legacy)
361 time_t rtc_start_date;
362 struct tm tm;
364 if (!strcmp(startdate, "now") && legacy) {
365 rtc_date_offset = -1;
366 } else {
367 if (sscanf(startdate, "%d-%d-%dT%d:%d:%d",
368 &tm.tm_year,
369 &tm.tm_mon,
370 &tm.tm_mday,
371 &tm.tm_hour,
372 &tm.tm_min,
373 &tm.tm_sec) == 6) {
374 /* OK */
375 } else if (sscanf(startdate, "%d-%d-%d",
376 &tm.tm_year,
377 &tm.tm_mon,
378 &tm.tm_mday) == 3) {
379 tm.tm_hour = 0;
380 tm.tm_min = 0;
381 tm.tm_sec = 0;
382 } else {
383 goto date_fail;
385 tm.tm_year -= 1900;
386 tm.tm_mon--;
387 rtc_start_date = mktimegm(&tm);
388 if (rtc_start_date == -1) {
389 date_fail:
390 fprintf(stderr, "Invalid date format. Valid formats are:\n"
391 "'2006-06-17T16:01:21' or '2006-06-17'\n");
392 exit(1);
394 rtc_date_offset = time(NULL) - rtc_start_date;
398 static void configure_rtc(QemuOpts *opts)
400 const char *value;
402 value = qemu_opt_get(opts, "base");
403 if (value) {
404 if (!strcmp(value, "utc")) {
405 rtc_utc = 1;
406 } else if (!strcmp(value, "localtime")) {
407 rtc_utc = 0;
408 } else {
409 configure_rtc_date_offset(value, 0);
412 value = qemu_opt_get(opts, "clock");
413 if (value) {
414 if (!strcmp(value, "host")) {
415 rtc_clock = host_clock;
416 } else if (!strcmp(value, "vm")) {
417 rtc_clock = vm_clock;
418 } else {
419 fprintf(stderr, "qemu: invalid option value '%s'\n", value);
420 exit(1);
423 value = qemu_opt_get(opts, "driftfix");
424 if (value) {
425 if (!strcmp(value, "slew")) {
426 rtc_td_hack = 1;
427 } else if (!strcmp(value, "none")) {
428 rtc_td_hack = 0;
429 } else {
430 fprintf(stderr, "qemu: invalid option value '%s'\n", value);
431 exit(1);
436 /***********************************************************/
437 /* Bluetooth support */
438 static int nb_hcis;
439 static int cur_hci;
440 static struct HCIInfo *hci_table[MAX_NICS];
442 static struct bt_vlan_s {
443 struct bt_scatternet_s net;
444 int id;
445 struct bt_vlan_s *next;
446 } *first_bt_vlan;
448 /* find or alloc a new bluetooth "VLAN" */
449 static struct bt_scatternet_s *qemu_find_bt_vlan(int id)
451 struct bt_vlan_s **pvlan, *vlan;
452 for (vlan = first_bt_vlan; vlan != NULL; vlan = vlan->next) {
453 if (vlan->id == id)
454 return &vlan->net;
456 vlan = qemu_mallocz(sizeof(struct bt_vlan_s));
457 vlan->id = id;
458 pvlan = &first_bt_vlan;
459 while (*pvlan != NULL)
460 pvlan = &(*pvlan)->next;
461 *pvlan = vlan;
462 return &vlan->net;
465 static void null_hci_send(struct HCIInfo *hci, const uint8_t *data, int len)
469 static int null_hci_addr_set(struct HCIInfo *hci, const uint8_t *bd_addr)
471 return -ENOTSUP;
474 static struct HCIInfo null_hci = {
475 .cmd_send = null_hci_send,
476 .sco_send = null_hci_send,
477 .acl_send = null_hci_send,
478 .bdaddr_set = null_hci_addr_set,
481 struct HCIInfo *qemu_next_hci(void)
483 if (cur_hci == nb_hcis)
484 return &null_hci;
486 return hci_table[cur_hci++];
489 static struct HCIInfo *hci_init(const char *str)
491 char *endp;
492 struct bt_scatternet_s *vlan = 0;
494 if (!strcmp(str, "null"))
495 /* null */
496 return &null_hci;
497 else if (!strncmp(str, "host", 4) && (str[4] == '\0' || str[4] == ':'))
498 /* host[:hciN] */
499 return bt_host_hci(str[4] ? str + 5 : "hci0");
500 else if (!strncmp(str, "hci", 3)) {
501 /* hci[,vlan=n] */
502 if (str[3]) {
503 if (!strncmp(str + 3, ",vlan=", 6)) {
504 vlan = qemu_find_bt_vlan(strtol(str + 9, &endp, 0));
505 if (*endp)
506 vlan = 0;
508 } else
509 vlan = qemu_find_bt_vlan(0);
510 if (vlan)
511 return bt_new_hci(vlan);
514 fprintf(stderr, "qemu: Unknown bluetooth HCI `%s'.\n", str);
516 return 0;
519 static int bt_hci_parse(const char *str)
521 struct HCIInfo *hci;
522 bdaddr_t bdaddr;
524 if (nb_hcis >= MAX_NICS) {
525 fprintf(stderr, "qemu: Too many bluetooth HCIs (max %i).\n", MAX_NICS);
526 return -1;
529 hci = hci_init(str);
530 if (!hci)
531 return -1;
533 bdaddr.b[0] = 0x52;
534 bdaddr.b[1] = 0x54;
535 bdaddr.b[2] = 0x00;
536 bdaddr.b[3] = 0x12;
537 bdaddr.b[4] = 0x34;
538 bdaddr.b[5] = 0x56 + nb_hcis;
539 hci->bdaddr_set(hci, bdaddr.b);
541 hci_table[nb_hcis++] = hci;
543 return 0;
546 static void bt_vhci_add(int vlan_id)
548 struct bt_scatternet_s *vlan = qemu_find_bt_vlan(vlan_id);
550 if (!vlan->slave)
551 fprintf(stderr, "qemu: warning: adding a VHCI to "
552 "an empty scatternet %i\n", vlan_id);
554 bt_vhci_init(bt_new_hci(vlan));
557 static struct bt_device_s *bt_device_add(const char *opt)
559 struct bt_scatternet_s *vlan;
560 int vlan_id = 0;
561 char *endp = strstr(opt, ",vlan=");
562 int len = (endp ? endp - opt : strlen(opt)) + 1;
563 char devname[10];
565 pstrcpy(devname, MIN(sizeof(devname), len), opt);
567 if (endp) {
568 vlan_id = strtol(endp + 6, &endp, 0);
569 if (*endp) {
570 fprintf(stderr, "qemu: unrecognised bluetooth vlan Id\n");
571 return 0;
575 vlan = qemu_find_bt_vlan(vlan_id);
577 if (!vlan->slave)
578 fprintf(stderr, "qemu: warning: adding a slave device to "
579 "an empty scatternet %i\n", vlan_id);
581 if (!strcmp(devname, "keyboard"))
582 return bt_keyboard_init(vlan);
584 fprintf(stderr, "qemu: unsupported bluetooth device `%s'\n", devname);
585 return 0;
588 static int bt_parse(const char *opt)
590 const char *endp, *p;
591 int vlan;
593 if (strstart(opt, "hci", &endp)) {
594 if (!*endp || *endp == ',') {
595 if (*endp)
596 if (!strstart(endp, ",vlan=", 0))
597 opt = endp + 1;
599 return bt_hci_parse(opt);
601 } else if (strstart(opt, "vhci", &endp)) {
602 if (!*endp || *endp == ',') {
603 if (*endp) {
604 if (strstart(endp, ",vlan=", &p)) {
605 vlan = strtol(p, (char **) &endp, 0);
606 if (*endp) {
607 fprintf(stderr, "qemu: bad scatternet '%s'\n", p);
608 return 1;
610 } else {
611 fprintf(stderr, "qemu: bad parameter '%s'\n", endp + 1);
612 return 1;
614 } else
615 vlan = 0;
617 bt_vhci_add(vlan);
618 return 0;
620 } else if (strstart(opt, "device:", &endp))
621 return !bt_device_add(endp);
623 fprintf(stderr, "qemu: bad bluetooth parameter '%s'\n", opt);
624 return 1;
627 /***********************************************************/
628 /* QEMU Block devices */
630 #define HD_OPTS "media=disk"
631 #define CDROM_OPTS "media=cdrom"
632 #define FD_OPTS ""
633 #define PFLASH_OPTS ""
634 #define MTD_OPTS ""
635 #define SD_OPTS ""
637 static int drive_init_func(QemuOpts *opts, void *opaque)
639 int *use_scsi = opaque;
641 return drive_init(opts, *use_scsi) == NULL;
644 static int drive_enable_snapshot(QemuOpts *opts, void *opaque)
646 if (NULL == qemu_opt_get(opts, "snapshot")) {
647 qemu_opt_set(opts, "snapshot", "on");
649 return 0;
652 static void default_drive(int enable, int snapshot, int use_scsi,
653 BlockInterfaceType type, int index,
654 const char *optstr)
656 QemuOpts *opts;
658 if (type == IF_DEFAULT) {
659 type = use_scsi ? IF_SCSI : IF_IDE;
662 if (!enable || drive_get_by_index(type, index)) {
663 return;
666 opts = drive_add(type, index, NULL, optstr);
667 if (snapshot) {
668 drive_enable_snapshot(opts, NULL);
670 if (!drive_init(opts, use_scsi)) {
671 exit(1);
675 void qemu_register_boot_set(QEMUBootSetHandler *func, void *opaque)
677 boot_set_handler = func;
678 boot_set_opaque = opaque;
681 int qemu_boot_set(const char *boot_devices)
683 if (!boot_set_handler) {
684 return -EINVAL;
686 return boot_set_handler(boot_set_opaque, boot_devices);
689 static void validate_bootdevices(char *devices)
691 /* We just do some generic consistency checks */
692 const char *p;
693 int bitmap = 0;
695 for (p = devices; *p != '\0'; p++) {
696 /* Allowed boot devices are:
697 * a-b: floppy disk drives
698 * c-f: IDE disk drives
699 * g-m: machine implementation dependant drives
700 * n-p: network devices
701 * It's up to each machine implementation to check if the given boot
702 * devices match the actual hardware implementation and firmware
703 * features.
705 if (*p < 'a' || *p > 'p') {
706 fprintf(stderr, "Invalid boot device '%c'\n", *p);
707 exit(1);
709 if (bitmap & (1 << (*p - 'a'))) {
710 fprintf(stderr, "Boot device '%c' was given twice\n", *p);
711 exit(1);
713 bitmap |= 1 << (*p - 'a');
717 static void restore_boot_devices(void *opaque)
719 char *standard_boot_devices = opaque;
720 static int first = 1;
722 /* Restore boot order and remove ourselves after the first boot */
723 if (first) {
724 first = 0;
725 return;
728 qemu_boot_set(standard_boot_devices);
730 qemu_unregister_reset(restore_boot_devices, standard_boot_devices);
731 qemu_free(standard_boot_devices);
734 void add_boot_device_path(int32_t bootindex, DeviceState *dev,
735 const char *suffix)
737 FWBootEntry *node, *i;
739 if (bootindex < 0) {
740 return;
743 assert(dev != NULL || suffix != NULL);
745 node = qemu_mallocz(sizeof(FWBootEntry));
746 node->bootindex = bootindex;
747 node->suffix = suffix ? qemu_strdup(suffix) : NULL;
748 node->dev = dev;
750 QTAILQ_FOREACH(i, &fw_boot_order, link) {
751 if (i->bootindex == bootindex) {
752 fprintf(stderr, "Two devices with same boot index %d\n", bootindex);
753 exit(1);
754 } else if (i->bootindex < bootindex) {
755 continue;
757 QTAILQ_INSERT_BEFORE(i, node, link);
758 return;
760 QTAILQ_INSERT_TAIL(&fw_boot_order, node, link);
764 * This function returns null terminated string that consist of new line
765 * separated device paths.
767 * memory pointed by "size" is assigned total length of the array in bytes
770 char *get_boot_devices_list(uint32_t *size)
772 FWBootEntry *i;
773 uint32_t total = 0;
774 char *list = NULL;
776 QTAILQ_FOREACH(i, &fw_boot_order, link) {
777 char *devpath = NULL, *bootpath;
778 int len;
780 if (i->dev) {
781 devpath = qdev_get_fw_dev_path(i->dev);
782 assert(devpath);
785 if (i->suffix && devpath) {
786 size_t bootpathlen = strlen(devpath) + strlen(i->suffix) + 1;
788 bootpath = qemu_malloc(bootpathlen);
789 snprintf(bootpath, bootpathlen, "%s%s", devpath, i->suffix);
790 qemu_free(devpath);
791 } else if (devpath) {
792 bootpath = devpath;
793 } else {
794 bootpath = qemu_strdup(i->suffix);
795 assert(bootpath);
798 if (total) {
799 list[total-1] = '\n';
801 len = strlen(bootpath) + 1;
802 list = qemu_realloc(list, total + len);
803 memcpy(&list[total], bootpath, len);
804 total += len;
805 qemu_free(bootpath);
808 *size = total;
810 return list;
813 static void numa_add(const char *optarg)
815 char option[128];
816 char *endptr;
817 unsigned long long value, endvalue;
818 int nodenr;
820 optarg = get_opt_name(option, 128, optarg, ',') + 1;
821 if (!strcmp(option, "node")) {
822 if (get_param_value(option, 128, "nodeid", optarg) == 0) {
823 nodenr = nb_numa_nodes;
824 } else {
825 nodenr = strtoull(option, NULL, 10);
828 if (get_param_value(option, 128, "mem", optarg) == 0) {
829 node_mem[nodenr] = 0;
830 } else {
831 int64_t sval;
832 sval = strtosz(option, NULL);
833 if (sval < 0) {
834 fprintf(stderr, "qemu: invalid numa mem size: %s\n", optarg);
835 exit(1);
837 node_mem[nodenr] = sval;
839 if (get_param_value(option, 128, "cpus", optarg) == 0) {
840 node_cpumask[nodenr] = 0;
841 } else {
842 value = strtoull(option, &endptr, 10);
843 if (value >= 64) {
844 value = 63;
845 fprintf(stderr, "only 64 CPUs in NUMA mode supported.\n");
846 } else {
847 if (*endptr == '-') {
848 endvalue = strtoull(endptr+1, &endptr, 10);
849 if (endvalue >= 63) {
850 endvalue = 62;
851 fprintf(stderr,
852 "only 63 CPUs in NUMA mode supported.\n");
854 value = (2ULL << endvalue) - (1ULL << value);
855 } else {
856 value = 1ULL << value;
859 node_cpumask[nodenr] = value;
861 nb_numa_nodes++;
863 return;
866 static void smp_parse(const char *optarg)
868 int smp, sockets = 0, threads = 0, cores = 0;
869 char *endptr;
870 char option[128];
872 smp = strtoul(optarg, &endptr, 10);
873 if (endptr != optarg) {
874 if (*endptr == ',') {
875 endptr++;
878 if (get_param_value(option, 128, "sockets", endptr) != 0)
879 sockets = strtoull(option, NULL, 10);
880 if (get_param_value(option, 128, "cores", endptr) != 0)
881 cores = strtoull(option, NULL, 10);
882 if (get_param_value(option, 128, "threads", endptr) != 0)
883 threads = strtoull(option, NULL, 10);
884 if (get_param_value(option, 128, "maxcpus", endptr) != 0)
885 max_cpus = strtoull(option, NULL, 10);
887 /* compute missing values, prefer sockets over cores over threads */
888 if (smp == 0 || sockets == 0) {
889 sockets = sockets > 0 ? sockets : 1;
890 cores = cores > 0 ? cores : 1;
891 threads = threads > 0 ? threads : 1;
892 if (smp == 0) {
893 smp = cores * threads * sockets;
895 } else {
896 if (cores == 0) {
897 threads = threads > 0 ? threads : 1;
898 cores = smp / (sockets * threads);
899 } else {
900 threads = smp / (cores * sockets);
903 smp_cpus = smp;
904 smp_cores = cores > 0 ? cores : 1;
905 smp_threads = threads > 0 ? threads : 1;
906 if (max_cpus == 0)
907 max_cpus = smp_cpus;
910 /***********************************************************/
911 /* USB devices */
913 static int usb_device_add(const char *devname)
915 const char *p;
916 USBDevice *dev = NULL;
918 if (!usb_enabled)
919 return -1;
921 /* drivers with .usbdevice_name entry in USBDeviceInfo */
922 dev = usbdevice_create(devname);
923 if (dev)
924 goto done;
926 /* the other ones */
927 if (strstart(devname, "host:", &p)) {
928 dev = usb_host_device_open(p);
929 } else if (!strcmp(devname, "bt") || strstart(devname, "bt:", &p)) {
930 dev = usb_bt_init(devname[2] ? hci_init(p) :
931 bt_new_hci(qemu_find_bt_vlan(0)));
932 } else {
933 return -1;
935 if (!dev)
936 return -1;
938 done:
939 return 0;
942 static int usb_device_del(const char *devname)
944 int bus_num, addr;
945 const char *p;
947 if (strstart(devname, "host:", &p))
948 return usb_host_device_close(p);
950 if (!usb_enabled)
951 return -1;
953 p = strchr(devname, '.');
954 if (!p)
955 return -1;
956 bus_num = strtoul(devname, NULL, 0);
957 addr = strtoul(p + 1, NULL, 0);
959 return usb_device_delete_addr(bus_num, addr);
962 static int usb_parse(const char *cmdline)
964 int r;
965 r = usb_device_add(cmdline);
966 if (r < 0) {
967 fprintf(stderr, "qemu: could not add USB device '%s'\n", cmdline);
969 return r;
972 void do_usb_add(Monitor *mon, const QDict *qdict)
974 const char *devname = qdict_get_str(qdict, "devname");
975 if (usb_device_add(devname) < 0) {
976 error_report("could not add USB device '%s'", devname);
980 void do_usb_del(Monitor *mon, const QDict *qdict)
982 const char *devname = qdict_get_str(qdict, "devname");
983 if (usb_device_del(devname) < 0) {
984 error_report("could not delete USB device '%s'", devname);
988 /***********************************************************/
989 /* PCMCIA/Cardbus */
991 static struct pcmcia_socket_entry_s {
992 PCMCIASocket *socket;
993 struct pcmcia_socket_entry_s *next;
994 } *pcmcia_sockets = 0;
996 void pcmcia_socket_register(PCMCIASocket *socket)
998 struct pcmcia_socket_entry_s *entry;
1000 entry = qemu_malloc(sizeof(struct pcmcia_socket_entry_s));
1001 entry->socket = socket;
1002 entry->next = pcmcia_sockets;
1003 pcmcia_sockets = entry;
1006 void pcmcia_socket_unregister(PCMCIASocket *socket)
1008 struct pcmcia_socket_entry_s *entry, **ptr;
1010 ptr = &pcmcia_sockets;
1011 for (entry = *ptr; entry; ptr = &entry->next, entry = *ptr)
1012 if (entry->socket == socket) {
1013 *ptr = entry->next;
1014 qemu_free(entry);
1018 void pcmcia_info(Monitor *mon)
1020 struct pcmcia_socket_entry_s *iter;
1022 if (!pcmcia_sockets)
1023 monitor_printf(mon, "No PCMCIA sockets\n");
1025 for (iter = pcmcia_sockets; iter; iter = iter->next)
1026 monitor_printf(mon, "%s: %s\n", iter->socket->slot_string,
1027 iter->socket->attached ? iter->socket->card_string :
1028 "Empty");
1031 /***********************************************************/
1032 /* machine registration */
1034 static QEMUMachine *first_machine = NULL;
1035 QEMUMachine *current_machine = NULL;
1037 int qemu_register_machine(QEMUMachine *m)
1039 QEMUMachine **pm;
1040 pm = &first_machine;
1041 while (*pm != NULL)
1042 pm = &(*pm)->next;
1043 m->next = NULL;
1044 *pm = m;
1045 return 0;
1048 static QEMUMachine *find_machine(const char *name)
1050 QEMUMachine *m;
1052 for(m = first_machine; m != NULL; m = m->next) {
1053 if (!strcmp(m->name, name))
1054 return m;
1055 if (m->alias && !strcmp(m->alias, name))
1056 return m;
1058 return NULL;
1061 static QEMUMachine *find_default_machine(void)
1063 QEMUMachine *m;
1065 for(m = first_machine; m != NULL; m = m->next) {
1066 if (m->is_default) {
1067 return m;
1070 return NULL;
1073 /***********************************************************/
1074 /* main execution loop */
1076 static void gui_update(void *opaque)
1078 uint64_t interval = GUI_REFRESH_INTERVAL;
1079 DisplayState *ds = opaque;
1080 DisplayChangeListener *dcl = ds->listeners;
1082 qemu_flush_coalesced_mmio_buffer();
1083 dpy_refresh(ds);
1085 while (dcl != NULL) {
1086 if (dcl->gui_timer_interval &&
1087 dcl->gui_timer_interval < interval)
1088 interval = dcl->gui_timer_interval;
1089 dcl = dcl->next;
1091 qemu_mod_timer(ds->gui_timer, interval + qemu_get_clock_ms(rt_clock));
1094 static void nographic_update(void *opaque)
1096 uint64_t interval = GUI_REFRESH_INTERVAL;
1098 qemu_flush_coalesced_mmio_buffer();
1099 qemu_mod_timer(nographic_timer, interval + qemu_get_clock_ms(rt_clock));
1102 struct vm_change_state_entry {
1103 VMChangeStateHandler *cb;
1104 void *opaque;
1105 QLIST_ENTRY (vm_change_state_entry) entries;
1108 static QLIST_HEAD(vm_change_state_head, vm_change_state_entry) vm_change_state_head;
1110 VMChangeStateEntry *qemu_add_vm_change_state_handler(VMChangeStateHandler *cb,
1111 void *opaque)
1113 VMChangeStateEntry *e;
1115 e = qemu_mallocz(sizeof (*e));
1117 e->cb = cb;
1118 e->opaque = opaque;
1119 QLIST_INSERT_HEAD(&vm_change_state_head, e, entries);
1120 return e;
1123 void qemu_del_vm_change_state_handler(VMChangeStateEntry *e)
1125 QLIST_REMOVE (e, entries);
1126 qemu_free (e);
1129 void vm_state_notify(int running, int reason)
1131 VMChangeStateEntry *e;
1133 trace_vm_state_notify(running, reason);
1135 for (e = vm_change_state_head.lh_first; e; e = e->entries.le_next) {
1136 e->cb(e->opaque, running, reason);
1140 void vm_start(void)
1142 if (!vm_running) {
1143 cpu_enable_ticks();
1144 vm_running = 1;
1145 vm_state_notify(1, 0);
1146 resume_all_vcpus();
1147 monitor_protocol_event(QEVENT_RESUME, NULL);
1151 /* reset/shutdown handler */
1153 typedef struct QEMUResetEntry {
1154 QTAILQ_ENTRY(QEMUResetEntry) entry;
1155 QEMUResetHandler *func;
1156 void *opaque;
1157 } QEMUResetEntry;
1159 static QTAILQ_HEAD(reset_handlers, QEMUResetEntry) reset_handlers =
1160 QTAILQ_HEAD_INITIALIZER(reset_handlers);
1161 static int reset_requested;
1162 static int shutdown_requested, shutdown_signal = -1;
1163 static pid_t shutdown_pid;
1164 static int powerdown_requested;
1165 static int debug_requested;
1166 static int vmstop_requested;
1168 int qemu_shutdown_requested_get(void)
1170 return shutdown_requested;
1173 int qemu_reset_requested_get(void)
1175 return reset_requested;
1178 int qemu_shutdown_requested(void)
1180 int r = shutdown_requested;
1181 shutdown_requested = 0;
1182 return r;
1185 void qemu_kill_report(void)
1187 if (shutdown_signal != -1) {
1188 fprintf(stderr, "qemu: terminating on signal %d", shutdown_signal);
1189 if (shutdown_pid == 0) {
1190 /* This happens for eg ^C at the terminal, so it's worth
1191 * avoiding printing an odd message in that case.
1193 fputc('\n', stderr);
1194 } else {
1195 fprintf(stderr, " from pid %d\n", shutdown_pid);
1197 shutdown_signal = -1;
1201 int qemu_reset_requested(void)
1203 int r = reset_requested;
1204 reset_requested = 0;
1205 return r;
1208 int qemu_powerdown_requested(void)
1210 int r = powerdown_requested;
1211 powerdown_requested = 0;
1212 return r;
1215 static int qemu_debug_requested(void)
1217 int r = debug_requested;
1218 debug_requested = 0;
1219 return r;
1222 static int qemu_vmstop_requested(void)
1224 int r = vmstop_requested;
1225 vmstop_requested = 0;
1226 return r;
1229 void qemu_register_reset(QEMUResetHandler *func, void *opaque)
1231 QEMUResetEntry *re = qemu_mallocz(sizeof(QEMUResetEntry));
1233 re->func = func;
1234 re->opaque = opaque;
1235 QTAILQ_INSERT_TAIL(&reset_handlers, re, entry);
1238 void qemu_unregister_reset(QEMUResetHandler *func, void *opaque)
1240 QEMUResetEntry *re;
1242 QTAILQ_FOREACH(re, &reset_handlers, entry) {
1243 if (re->func == func && re->opaque == opaque) {
1244 QTAILQ_REMOVE(&reset_handlers, re, entry);
1245 qemu_free(re);
1246 return;
1251 void qemu_system_reset(void)
1253 QEMUResetEntry *re, *nre;
1255 /* reset all devices */
1256 QTAILQ_FOREACH_SAFE(re, &reset_handlers, entry, nre) {
1257 re->func(re->opaque);
1259 monitor_protocol_event(QEVENT_RESET, NULL);
1260 cpu_synchronize_all_post_reset();
1263 void qemu_system_reset_request(void)
1265 if (no_reboot) {
1266 shutdown_requested = 1;
1267 } else {
1268 reset_requested = 1;
1270 cpu_stop_current();
1271 qemu_notify_event();
1274 void qemu_system_killed(int signal, pid_t pid)
1276 shutdown_signal = signal;
1277 shutdown_pid = pid;
1278 qemu_system_shutdown_request();
1281 void qemu_system_shutdown_request(void)
1283 shutdown_requested = 1;
1284 qemu_notify_event();
1287 void qemu_system_powerdown_request(void)
1289 powerdown_requested = 1;
1290 qemu_notify_event();
1293 void qemu_system_debug_request(void)
1295 debug_requested = 1;
1296 qemu_notify_event();
1299 void qemu_system_vmstop_request(int reason)
1301 vmstop_requested = reason;
1302 qemu_notify_event();
1305 void main_loop_wait(int nonblocking)
1307 fd_set rfds, wfds, xfds;
1308 int ret, nfds;
1309 struct timeval tv;
1310 int timeout;
1312 if (nonblocking)
1313 timeout = 0;
1314 else {
1315 timeout = qemu_calculate_timeout();
1316 qemu_bh_update_timeout(&timeout);
1319 os_host_main_loop_wait(&timeout);
1321 tv.tv_sec = timeout / 1000;
1322 tv.tv_usec = (timeout % 1000) * 1000;
1324 /* poll any events */
1325 /* XXX: separate device handlers from system ones */
1326 nfds = -1;
1327 FD_ZERO(&rfds);
1328 FD_ZERO(&wfds);
1329 FD_ZERO(&xfds);
1330 qemu_iohandler_fill(&nfds, &rfds, &wfds, &xfds);
1331 slirp_select_fill(&nfds, &rfds, &wfds, &xfds);
1333 qemu_mutex_unlock_iothread();
1334 ret = select(nfds + 1, &rfds, &wfds, &xfds, &tv);
1335 qemu_mutex_lock_iothread();
1337 qemu_iohandler_poll(&rfds, &wfds, &xfds, ret);
1338 slirp_select_poll(&rfds, &wfds, &xfds, (ret < 0));
1340 qemu_run_all_timers();
1342 /* Check bottom-halves last in case any of the earlier events triggered
1343 them. */
1344 qemu_bh_poll();
1348 #ifndef CONFIG_IOTHREAD
1349 static int vm_request_pending(void)
1351 return powerdown_requested ||
1352 reset_requested ||
1353 shutdown_requested ||
1354 debug_requested ||
1355 vmstop_requested;
1357 #endif
1359 qemu_irq qemu_system_powerdown;
1361 static void main_loop(void)
1363 bool nonblocking = false;
1364 #ifdef CONFIG_PROFILER
1365 int64_t ti;
1366 #endif
1367 int r;
1369 qemu_main_loop_start();
1371 for (;;) {
1372 #ifndef CONFIG_IOTHREAD
1373 nonblocking = cpu_exec_all();
1374 if (vm_request_pending()) {
1375 nonblocking = true;
1377 #endif
1378 #ifdef CONFIG_PROFILER
1379 ti = profile_getclock();
1380 #endif
1381 main_loop_wait(nonblocking);
1382 #ifdef CONFIG_PROFILER
1383 dev_time += profile_getclock() - ti;
1384 #endif
1386 if (qemu_debug_requested()) {
1387 vm_stop(VMSTOP_DEBUG);
1389 if (qemu_shutdown_requested()) {
1390 qemu_kill_report();
1391 monitor_protocol_event(QEVENT_SHUTDOWN, NULL);
1392 if (no_shutdown) {
1393 vm_stop(VMSTOP_SHUTDOWN);
1394 no_shutdown = 0;
1395 } else
1396 break;
1398 if (qemu_reset_requested()) {
1399 pause_all_vcpus();
1400 cpu_synchronize_all_states();
1401 qemu_system_reset();
1402 resume_all_vcpus();
1404 if (qemu_powerdown_requested()) {
1405 monitor_protocol_event(QEVENT_POWERDOWN, NULL);
1406 qemu_irq_raise(qemu_system_powerdown);
1408 if ((r = qemu_vmstop_requested())) {
1409 vm_stop(r);
1412 bdrv_close_all();
1413 pause_all_vcpus();
1416 static void version(void)
1418 printf("QEMU emulator version " QEMU_VERSION QEMU_PKGVERSION ", Copyright (c) 2003-2008 Fabrice Bellard\n");
1421 static void help(int exitcode)
1423 const char *options_help =
1424 #define DEF(option, opt_arg, opt_enum, opt_help, arch_mask) \
1425 opt_help
1426 #define DEFHEADING(text) stringify(text) "\n"
1427 #include "qemu-options.def"
1428 #undef DEF
1429 #undef DEFHEADING
1430 #undef GEN_DOCS
1432 version();
1433 printf("usage: %s [options] [disk_image]\n"
1434 "\n"
1435 "'disk_image' is a raw hard disk image for IDE hard disk 0\n"
1436 "\n"
1437 "%s\n"
1438 "During emulation, the following keys are useful:\n"
1439 "ctrl-alt-f toggle full screen\n"
1440 "ctrl-alt-n switch to virtual console 'n'\n"
1441 "ctrl-alt toggle mouse and keyboard grab\n"
1442 "\n"
1443 "When using -nographic, press 'ctrl-a h' to get some help.\n",
1444 "qemu",
1445 options_help);
1446 exit(exitcode);
1449 #define HAS_ARG 0x0001
1451 typedef struct QEMUOption {
1452 const char *name;
1453 int flags;
1454 int index;
1455 uint32_t arch_mask;
1456 } QEMUOption;
1458 static const QEMUOption qemu_options[] = {
1459 { "h", 0, QEMU_OPTION_h, QEMU_ARCH_ALL },
1460 #define DEF(option, opt_arg, opt_enum, opt_help, arch_mask) \
1461 { option, opt_arg, opt_enum, arch_mask },
1462 #define DEFHEADING(text)
1463 #include "qemu-options.def"
1464 #undef DEF
1465 #undef DEFHEADING
1466 #undef GEN_DOCS
1467 { NULL },
1469 static void select_vgahw (const char *p)
1471 const char *opts;
1473 default_vga = 0;
1474 vga_interface_type = VGA_NONE;
1475 if (strstart(p, "std", &opts)) {
1476 vga_interface_type = VGA_STD;
1477 } else if (strstart(p, "cirrus", &opts)) {
1478 vga_interface_type = VGA_CIRRUS;
1479 } else if (strstart(p, "vmware", &opts)) {
1480 vga_interface_type = VGA_VMWARE;
1481 } else if (strstart(p, "xenfb", &opts)) {
1482 vga_interface_type = VGA_XENFB;
1483 } else if (strstart(p, "qxl", &opts)) {
1484 vga_interface_type = VGA_QXL;
1485 } else if (!strstart(p, "none", &opts)) {
1486 invalid_vga:
1487 fprintf(stderr, "Unknown vga type: %s\n", p);
1488 exit(1);
1490 while (*opts) {
1491 const char *nextopt;
1493 if (strstart(opts, ",retrace=", &nextopt)) {
1494 opts = nextopt;
1495 if (strstart(opts, "dumb", &nextopt))
1496 vga_retrace_method = VGA_RETRACE_DUMB;
1497 else if (strstart(opts, "precise", &nextopt))
1498 vga_retrace_method = VGA_RETRACE_PRECISE;
1499 else goto invalid_vga;
1500 } else goto invalid_vga;
1501 opts = nextopt;
1505 static DisplayType select_display(const char *p)
1507 const char *opts;
1508 DisplayType display = DT_DEFAULT;
1510 if (strstart(p, "sdl", &opts)) {
1511 #ifdef CONFIG_SDL
1512 display = DT_SDL;
1513 while (*opts) {
1514 const char *nextopt;
1516 if (strstart(opts, ",frame=", &nextopt)) {
1517 opts = nextopt;
1518 if (strstart(opts, "on", &nextopt)) {
1519 no_frame = 0;
1520 } else if (strstart(opts, "off", &nextopt)) {
1521 no_frame = 1;
1522 } else {
1523 goto invalid_sdl_args;
1525 } else if (strstart(opts, ",alt_grab=", &nextopt)) {
1526 opts = nextopt;
1527 if (strstart(opts, "on", &nextopt)) {
1528 alt_grab = 1;
1529 } else if (strstart(opts, "off", &nextopt)) {
1530 alt_grab = 0;
1531 } else {
1532 goto invalid_sdl_args;
1534 } else if (strstart(opts, ",ctrl_grab=", &nextopt)) {
1535 opts = nextopt;
1536 if (strstart(opts, "on", &nextopt)) {
1537 ctrl_grab = 1;
1538 } else if (strstart(opts, "off", &nextopt)) {
1539 ctrl_grab = 0;
1540 } else {
1541 goto invalid_sdl_args;
1543 } else if (strstart(opts, ",window_close=", &nextopt)) {
1544 opts = nextopt;
1545 if (strstart(opts, "on", &nextopt)) {
1546 no_quit = 0;
1547 } else if (strstart(opts, "off", &nextopt)) {
1548 no_quit = 1;
1549 } else {
1550 goto invalid_sdl_args;
1552 } else {
1553 invalid_sdl_args:
1554 fprintf(stderr, "Invalid SDL option string: %s\n", p);
1555 exit(1);
1557 opts = nextopt;
1559 #else
1560 fprintf(stderr, "SDL support is disabled\n");
1561 exit(1);
1562 #endif
1563 } else if (strstart(p, "vnc", &opts)) {
1564 #ifdef CONFIG_VNC
1565 display_remote++;
1567 if (*opts) {
1568 const char *nextopt;
1570 if (strstart(opts, "=", &nextopt)) {
1571 vnc_display = nextopt;
1574 if (!vnc_display) {
1575 fprintf(stderr, "VNC requires a display argument vnc=<display>\n");
1576 exit(1);
1578 #else
1579 fprintf(stderr, "VNC support is disabled\n");
1580 exit(1);
1581 #endif
1582 } else if (strstart(p, "curses", &opts)) {
1583 #ifdef CONFIG_CURSES
1584 display = DT_CURSES;
1585 #else
1586 fprintf(stderr, "Curses support is disabled\n");
1587 exit(1);
1588 #endif
1589 } else if (strstart(p, "none", &opts)) {
1590 display = DT_NONE;
1591 } else {
1592 fprintf(stderr, "Unknown display type: %s\n", p);
1593 exit(1);
1596 return display;
1599 static int balloon_parse(const char *arg)
1601 QemuOpts *opts;
1603 if (strcmp(arg, "none") == 0) {
1604 return 0;
1607 if (!strncmp(arg, "virtio", 6)) {
1608 if (arg[6] == ',') {
1609 /* have params -> parse them */
1610 opts = qemu_opts_parse(qemu_find_opts("device"), arg+7, 0);
1611 if (!opts)
1612 return -1;
1613 } else {
1614 /* create empty opts */
1615 opts = qemu_opts_create(qemu_find_opts("device"), NULL, 0);
1617 qemu_opt_set(opts, "driver", "virtio-balloon");
1618 return 0;
1621 return -1;
1624 char *qemu_find_file(int type, const char *name)
1626 int len;
1627 const char *subdir;
1628 char *buf;
1630 /* If name contains path separators then try it as a straight path. */
1631 if ((strchr(name, '/') || strchr(name, '\\'))
1632 && access(name, R_OK) == 0) {
1633 return qemu_strdup(name);
1635 switch (type) {
1636 case QEMU_FILE_TYPE_BIOS:
1637 subdir = "";
1638 break;
1639 case QEMU_FILE_TYPE_KEYMAP:
1640 subdir = "keymaps/";
1641 break;
1642 default:
1643 abort();
1645 len = strlen(data_dir) + strlen(name) + strlen(subdir) + 2;
1646 buf = qemu_mallocz(len);
1647 snprintf(buf, len, "%s/%s%s", data_dir, subdir, name);
1648 if (access(buf, R_OK)) {
1649 qemu_free(buf);
1650 return NULL;
1652 return buf;
1655 static int device_help_func(QemuOpts *opts, void *opaque)
1657 return qdev_device_help(opts);
1660 static int device_init_func(QemuOpts *opts, void *opaque)
1662 DeviceState *dev;
1664 dev = qdev_device_add(opts);
1665 if (!dev)
1666 return -1;
1667 return 0;
1670 static int chardev_init_func(QemuOpts *opts, void *opaque)
1672 CharDriverState *chr;
1674 chr = qemu_chr_open_opts(opts, NULL);
1675 if (!chr)
1676 return -1;
1677 return 0;
1680 #ifdef CONFIG_VIRTFS
1681 static int fsdev_init_func(QemuOpts *opts, void *opaque)
1683 int ret;
1684 ret = qemu_fsdev_add(opts);
1686 return ret;
1688 #endif
1690 static int mon_init_func(QemuOpts *opts, void *opaque)
1692 CharDriverState *chr;
1693 const char *chardev;
1694 const char *mode;
1695 int flags;
1697 mode = qemu_opt_get(opts, "mode");
1698 if (mode == NULL) {
1699 mode = "readline";
1701 if (strcmp(mode, "readline") == 0) {
1702 flags = MONITOR_USE_READLINE;
1703 } else if (strcmp(mode, "control") == 0) {
1704 flags = MONITOR_USE_CONTROL;
1705 } else {
1706 fprintf(stderr, "unknown monitor mode \"%s\"\n", mode);
1707 exit(1);
1710 if (qemu_opt_get_bool(opts, "pretty", 0))
1711 flags |= MONITOR_USE_PRETTY;
1713 if (qemu_opt_get_bool(opts, "default", 0))
1714 flags |= MONITOR_IS_DEFAULT;
1716 chardev = qemu_opt_get(opts, "chardev");
1717 chr = qemu_chr_find(chardev);
1718 if (chr == NULL) {
1719 fprintf(stderr, "chardev \"%s\" not found\n", chardev);
1720 exit(1);
1723 monitor_init(chr, flags);
1724 return 0;
1727 static void monitor_parse(const char *optarg, const char *mode)
1729 static int monitor_device_index = 0;
1730 QemuOpts *opts;
1731 const char *p;
1732 char label[32];
1733 int def = 0;
1735 if (strstart(optarg, "chardev:", &p)) {
1736 snprintf(label, sizeof(label), "%s", p);
1737 } else {
1738 snprintf(label, sizeof(label), "compat_monitor%d",
1739 monitor_device_index);
1740 if (monitor_device_index == 0) {
1741 def = 1;
1743 opts = qemu_chr_parse_compat(label, optarg);
1744 if (!opts) {
1745 fprintf(stderr, "parse error: %s\n", optarg);
1746 exit(1);
1750 opts = qemu_opts_create(qemu_find_opts("mon"), label, 1);
1751 if (!opts) {
1752 fprintf(stderr, "duplicate chardev: %s\n", label);
1753 exit(1);
1755 qemu_opt_set(opts, "mode", mode);
1756 qemu_opt_set(opts, "chardev", label);
1757 if (def)
1758 qemu_opt_set(opts, "default", "on");
1759 monitor_device_index++;
1762 struct device_config {
1763 enum {
1764 DEV_USB, /* -usbdevice */
1765 DEV_BT, /* -bt */
1766 DEV_SERIAL, /* -serial */
1767 DEV_PARALLEL, /* -parallel */
1768 DEV_VIRTCON, /* -virtioconsole */
1769 DEV_DEBUGCON, /* -debugcon */
1770 } type;
1771 const char *cmdline;
1772 QTAILQ_ENTRY(device_config) next;
1774 QTAILQ_HEAD(, device_config) device_configs = QTAILQ_HEAD_INITIALIZER(device_configs);
1776 static void add_device_config(int type, const char *cmdline)
1778 struct device_config *conf;
1780 conf = qemu_mallocz(sizeof(*conf));
1781 conf->type = type;
1782 conf->cmdline = cmdline;
1783 QTAILQ_INSERT_TAIL(&device_configs, conf, next);
1786 static int foreach_device_config(int type, int (*func)(const char *cmdline))
1788 struct device_config *conf;
1789 int rc;
1791 QTAILQ_FOREACH(conf, &device_configs, next) {
1792 if (conf->type != type)
1793 continue;
1794 rc = func(conf->cmdline);
1795 if (0 != rc)
1796 return rc;
1798 return 0;
1801 static int serial_parse(const char *devname)
1803 static int index = 0;
1804 char label[32];
1806 if (strcmp(devname, "none") == 0)
1807 return 0;
1808 if (index == MAX_SERIAL_PORTS) {
1809 fprintf(stderr, "qemu: too many serial ports\n");
1810 exit(1);
1812 snprintf(label, sizeof(label), "serial%d", index);
1813 serial_hds[index] = qemu_chr_open(label, devname, NULL);
1814 if (!serial_hds[index]) {
1815 fprintf(stderr, "qemu: could not open serial device '%s': %s\n",
1816 devname, strerror(errno));
1817 return -1;
1819 index++;
1820 return 0;
1823 static int parallel_parse(const char *devname)
1825 static int index = 0;
1826 char label[32];
1828 if (strcmp(devname, "none") == 0)
1829 return 0;
1830 if (index == MAX_PARALLEL_PORTS) {
1831 fprintf(stderr, "qemu: too many parallel ports\n");
1832 exit(1);
1834 snprintf(label, sizeof(label), "parallel%d", index);
1835 parallel_hds[index] = qemu_chr_open(label, devname, NULL);
1836 if (!parallel_hds[index]) {
1837 fprintf(stderr, "qemu: could not open parallel device '%s': %s\n",
1838 devname, strerror(errno));
1839 return -1;
1841 index++;
1842 return 0;
1845 static int virtcon_parse(const char *devname)
1847 QemuOptsList *device = qemu_find_opts("device");
1848 static int index = 0;
1849 char label[32];
1850 QemuOpts *bus_opts, *dev_opts;
1852 if (strcmp(devname, "none") == 0)
1853 return 0;
1854 if (index == MAX_VIRTIO_CONSOLES) {
1855 fprintf(stderr, "qemu: too many virtio consoles\n");
1856 exit(1);
1859 bus_opts = qemu_opts_create(device, NULL, 0);
1860 qemu_opt_set(bus_opts, "driver", "virtio-serial");
1862 dev_opts = qemu_opts_create(device, NULL, 0);
1863 qemu_opt_set(dev_opts, "driver", "virtconsole");
1865 snprintf(label, sizeof(label), "virtcon%d", index);
1866 virtcon_hds[index] = qemu_chr_open(label, devname, NULL);
1867 if (!virtcon_hds[index]) {
1868 fprintf(stderr, "qemu: could not open virtio console '%s': %s\n",
1869 devname, strerror(errno));
1870 return -1;
1872 qemu_opt_set(dev_opts, "chardev", label);
1874 index++;
1875 return 0;
1878 static int debugcon_parse(const char *devname)
1880 QemuOpts *opts;
1882 if (!qemu_chr_open("debugcon", devname, NULL)) {
1883 exit(1);
1885 opts = qemu_opts_create(qemu_find_opts("device"), "debugcon", 1);
1886 if (!opts) {
1887 fprintf(stderr, "qemu: already have a debugcon device\n");
1888 exit(1);
1890 qemu_opt_set(opts, "driver", "isa-debugcon");
1891 qemu_opt_set(opts, "chardev", "debugcon");
1892 return 0;
1895 static int tcg_init(void)
1897 return 0;
1900 static struct {
1901 const char *opt_name;
1902 const char *name;
1903 int (*available)(void);
1904 int (*init)(void);
1905 int *allowed;
1906 } accel_list[] = {
1907 { "tcg", "tcg", tcg_available, tcg_init, &tcg_allowed },
1908 { "xen", "Xen", xen_available, xen_init, &xen_allowed },
1909 { "kvm", "KVM", kvm_available, kvm_init, &kvm_allowed },
1912 static int configure_accelerator(void)
1914 const char *p = NULL;
1915 char buf[10];
1916 int i, ret;
1917 bool accel_initalised = 0;
1918 bool init_failed = 0;
1920 QemuOptsList *list = qemu_find_opts("machine");
1921 if (!QTAILQ_EMPTY(&list->head)) {
1922 p = qemu_opt_get(QTAILQ_FIRST(&list->head), "accel");
1925 if (p == NULL) {
1926 /* Use the default "accelerator", tcg */
1927 p = "tcg";
1930 while (!accel_initalised && *p != '\0') {
1931 if (*p == ':') {
1932 p++;
1934 p = get_opt_name(buf, sizeof (buf), p, ':');
1935 for (i = 0; i < ARRAY_SIZE(accel_list); i++) {
1936 if (strcmp(accel_list[i].opt_name, buf) == 0) {
1937 *(accel_list[i].allowed) = 1;
1938 ret = accel_list[i].init();
1939 if (ret < 0) {
1940 init_failed = 1;
1941 if (!accel_list[i].available()) {
1942 printf("%s not supported for this target\n",
1943 accel_list[i].name);
1944 } else {
1945 fprintf(stderr, "failed to initialize %s: %s\n",
1946 accel_list[i].name,
1947 strerror(-ret));
1949 *(accel_list[i].allowed) = 0;
1950 } else {
1951 accel_initalised = 1;
1953 break;
1956 if (i == ARRAY_SIZE(accel_list)) {
1957 fprintf(stderr, "\"%s\" accelerator does not exist.\n", buf);
1961 if (!accel_initalised) {
1962 fprintf(stderr, "No accelerator found!\n");
1963 exit(1);
1966 if (init_failed) {
1967 fprintf(stderr, "Back to %s accelerator.\n", accel_list[i].name);
1970 return !accel_initalised;
1973 void qemu_add_exit_notifier(Notifier *notify)
1975 notifier_list_add(&exit_notifiers, notify);
1978 void qemu_remove_exit_notifier(Notifier *notify)
1980 notifier_list_remove(&exit_notifiers, notify);
1983 static void qemu_run_exit_notifiers(void)
1985 notifier_list_notify(&exit_notifiers);
1988 void qemu_add_machine_init_done_notifier(Notifier *notify)
1990 notifier_list_add(&machine_init_done_notifiers, notify);
1993 static void qemu_run_machine_init_done_notifiers(void)
1995 notifier_list_notify(&machine_init_done_notifiers);
1998 static const QEMUOption *lookup_opt(int argc, char **argv,
1999 const char **poptarg, int *poptind)
2001 const QEMUOption *popt;
2002 int optind = *poptind;
2003 char *r = argv[optind];
2004 const char *optarg;
2006 loc_set_cmdline(argv, optind, 1);
2007 optind++;
2008 /* Treat --foo the same as -foo. */
2009 if (r[1] == '-')
2010 r++;
2011 popt = qemu_options;
2012 for(;;) {
2013 if (!popt->name) {
2014 error_report("invalid option");
2015 exit(1);
2017 if (!strcmp(popt->name, r + 1))
2018 break;
2019 popt++;
2021 if (popt->flags & HAS_ARG) {
2022 if (optind >= argc) {
2023 error_report("requires an argument");
2024 exit(1);
2026 optarg = argv[optind++];
2027 loc_set_cmdline(argv, optind - 2, 2);
2028 } else {
2029 optarg = NULL;
2032 *poptarg = optarg;
2033 *poptind = optind;
2035 return popt;
2038 int main(int argc, char **argv, char **envp)
2040 const char *gdbstub_dev = NULL;
2041 int i;
2042 int snapshot, linux_boot;
2043 const char *icount_option = NULL;
2044 const char *initrd_filename;
2045 const char *kernel_filename, *kernel_cmdline;
2046 char boot_devices[33] = "cad"; /* default to HD->floppy->CD-ROM */
2047 DisplayState *ds;
2048 DisplayChangeListener *dcl;
2049 int cyls, heads, secs, translation;
2050 QemuOpts *hda_opts = NULL, *opts;
2051 QemuOptsList *olist;
2052 int optind;
2053 const char *optarg;
2054 const char *loadvm = NULL;
2055 QEMUMachine *machine;
2056 const char *cpu_model;
2057 int tb_size;
2058 const char *pid_file = NULL;
2059 const char *incoming = NULL;
2060 #ifdef CONFIG_VNC
2061 int show_vnc_port = 0;
2062 #endif
2063 int defconfig = 1;
2064 const char *trace_file = NULL;
2066 atexit(qemu_run_exit_notifiers);
2067 error_set_progname(argv[0]);
2069 init_clocks();
2071 qemu_cache_utils_init(envp);
2073 QLIST_INIT (&vm_change_state_head);
2074 os_setup_early_signal_handling();
2076 module_call_init(MODULE_INIT_MACHINE);
2077 machine = find_default_machine();
2078 cpu_model = NULL;
2079 initrd_filename = NULL;
2080 ram_size = 0;
2081 snapshot = 0;
2082 kernel_filename = NULL;
2083 kernel_cmdline = "";
2084 cyls = heads = secs = 0;
2085 translation = BIOS_ATA_TRANSLATION_AUTO;
2087 for (i = 0; i < MAX_NODES; i++) {
2088 node_mem[i] = 0;
2089 node_cpumask[i] = 0;
2092 nb_numa_nodes = 0;
2093 nb_nics = 0;
2095 tb_size = 0;
2096 autostart= 1;
2098 /* first pass of option parsing */
2099 optind = 1;
2100 while (optind < argc) {
2101 if (argv[optind][0] != '-') {
2102 /* disk image */
2103 optind++;
2104 continue;
2105 } else {
2106 const QEMUOption *popt;
2108 popt = lookup_opt(argc, argv, &optarg, &optind);
2109 switch (popt->index) {
2110 case QEMU_OPTION_nodefconfig:
2111 defconfig=0;
2112 break;
2117 if (defconfig) {
2118 int ret;
2120 ret = qemu_read_config_file(CONFIG_QEMU_CONFDIR "/qemu.conf");
2121 if (ret < 0 && ret != -ENOENT) {
2122 exit(1);
2125 ret = qemu_read_config_file(arch_config_name);
2126 if (ret < 0 && ret != -ENOENT) {
2127 exit(1);
2130 cpudef_init();
2132 /* second pass of option parsing */
2133 optind = 1;
2134 for(;;) {
2135 if (optind >= argc)
2136 break;
2137 if (argv[optind][0] != '-') {
2138 hda_opts = drive_add(IF_DEFAULT, 0, argv[optind++], HD_OPTS);
2139 } else {
2140 const QEMUOption *popt;
2142 popt = lookup_opt(argc, argv, &optarg, &optind);
2143 if (!(popt->arch_mask & arch_type)) {
2144 printf("Option %s not supported for this target\n", popt->name);
2145 exit(1);
2147 switch(popt->index) {
2148 case QEMU_OPTION_M:
2149 machine = find_machine(optarg);
2150 if (!machine) {
2151 QEMUMachine *m;
2152 printf("Supported machines are:\n");
2153 for(m = first_machine; m != NULL; m = m->next) {
2154 if (m->alias)
2155 printf("%-10s %s (alias of %s)\n",
2156 m->alias, m->desc, m->name);
2157 printf("%-10s %s%s\n",
2158 m->name, m->desc,
2159 m->is_default ? " (default)" : "");
2161 exit(*optarg != '?');
2163 break;
2164 case QEMU_OPTION_cpu:
2165 /* hw initialization will check this */
2166 if (*optarg == '?') {
2167 list_cpus(stdout, &fprintf, optarg);
2168 exit(0);
2169 } else {
2170 cpu_model = optarg;
2172 break;
2173 case QEMU_OPTION_initrd:
2174 initrd_filename = optarg;
2175 break;
2176 case QEMU_OPTION_hda:
2178 char buf[256];
2179 if (cyls == 0)
2180 snprintf(buf, sizeof(buf), "%s", HD_OPTS);
2181 else
2182 snprintf(buf, sizeof(buf),
2183 "%s,cyls=%d,heads=%d,secs=%d%s",
2184 HD_OPTS , cyls, heads, secs,
2185 translation == BIOS_ATA_TRANSLATION_LBA ?
2186 ",trans=lba" :
2187 translation == BIOS_ATA_TRANSLATION_NONE ?
2188 ",trans=none" : "");
2189 drive_add(IF_DEFAULT, 0, optarg, buf);
2190 break;
2192 case QEMU_OPTION_hdb:
2193 case QEMU_OPTION_hdc:
2194 case QEMU_OPTION_hdd:
2195 drive_add(IF_DEFAULT, popt->index - QEMU_OPTION_hda, optarg,
2196 HD_OPTS);
2197 break;
2198 case QEMU_OPTION_drive:
2199 if (drive_def(optarg) == NULL) {
2200 exit(1);
2202 break;
2203 case QEMU_OPTION_set:
2204 if (qemu_set_option(optarg) != 0)
2205 exit(1);
2206 break;
2207 case QEMU_OPTION_global:
2208 if (qemu_global_option(optarg) != 0)
2209 exit(1);
2210 break;
2211 case QEMU_OPTION_mtdblock:
2212 drive_add(IF_MTD, -1, optarg, MTD_OPTS);
2213 break;
2214 case QEMU_OPTION_sd:
2215 drive_add(IF_SD, 0, optarg, SD_OPTS);
2216 break;
2217 case QEMU_OPTION_pflash:
2218 drive_add(IF_PFLASH, -1, optarg, PFLASH_OPTS);
2219 break;
2220 case QEMU_OPTION_snapshot:
2221 snapshot = 1;
2222 break;
2223 case QEMU_OPTION_hdachs:
2225 const char *p;
2226 p = optarg;
2227 cyls = strtol(p, (char **)&p, 0);
2228 if (cyls < 1 || cyls > 16383)
2229 goto chs_fail;
2230 if (*p != ',')
2231 goto chs_fail;
2232 p++;
2233 heads = strtol(p, (char **)&p, 0);
2234 if (heads < 1 || heads > 16)
2235 goto chs_fail;
2236 if (*p != ',')
2237 goto chs_fail;
2238 p++;
2239 secs = strtol(p, (char **)&p, 0);
2240 if (secs < 1 || secs > 63)
2241 goto chs_fail;
2242 if (*p == ',') {
2243 p++;
2244 if (!strcmp(p, "none"))
2245 translation = BIOS_ATA_TRANSLATION_NONE;
2246 else if (!strcmp(p, "lba"))
2247 translation = BIOS_ATA_TRANSLATION_LBA;
2248 else if (!strcmp(p, "auto"))
2249 translation = BIOS_ATA_TRANSLATION_AUTO;
2250 else
2251 goto chs_fail;
2252 } else if (*p != '\0') {
2253 chs_fail:
2254 fprintf(stderr, "qemu: invalid physical CHS format\n");
2255 exit(1);
2257 if (hda_opts != NULL) {
2258 char num[16];
2259 snprintf(num, sizeof(num), "%d", cyls);
2260 qemu_opt_set(hda_opts, "cyls", num);
2261 snprintf(num, sizeof(num), "%d", heads);
2262 qemu_opt_set(hda_opts, "heads", num);
2263 snprintf(num, sizeof(num), "%d", secs);
2264 qemu_opt_set(hda_opts, "secs", num);
2265 if (translation == BIOS_ATA_TRANSLATION_LBA)
2266 qemu_opt_set(hda_opts, "trans", "lba");
2267 if (translation == BIOS_ATA_TRANSLATION_NONE)
2268 qemu_opt_set(hda_opts, "trans", "none");
2271 break;
2272 case QEMU_OPTION_numa:
2273 if (nb_numa_nodes >= MAX_NODES) {
2274 fprintf(stderr, "qemu: too many NUMA nodes\n");
2275 exit(1);
2277 numa_add(optarg);
2278 break;
2279 case QEMU_OPTION_display:
2280 display_type = select_display(optarg);
2281 break;
2282 case QEMU_OPTION_nographic:
2283 display_type = DT_NOGRAPHIC;
2284 break;
2285 case QEMU_OPTION_curses:
2286 #ifdef CONFIG_CURSES
2287 display_type = DT_CURSES;
2288 #else
2289 fprintf(stderr, "Curses support is disabled\n");
2290 exit(1);
2291 #endif
2292 break;
2293 case QEMU_OPTION_portrait:
2294 graphic_rotate = 1;
2295 break;
2296 case QEMU_OPTION_kernel:
2297 kernel_filename = optarg;
2298 break;
2299 case QEMU_OPTION_append:
2300 kernel_cmdline = optarg;
2301 break;
2302 case QEMU_OPTION_cdrom:
2303 drive_add(IF_DEFAULT, 2, optarg, CDROM_OPTS);
2304 break;
2305 case QEMU_OPTION_boot:
2307 static const char * const params[] = {
2308 "order", "once", "menu", NULL
2310 char buf[sizeof(boot_devices)];
2311 char *standard_boot_devices;
2312 int legacy = 0;
2314 if (!strchr(optarg, '=')) {
2315 legacy = 1;
2316 pstrcpy(buf, sizeof(buf), optarg);
2317 } else if (check_params(buf, sizeof(buf), params, optarg) < 0) {
2318 fprintf(stderr,
2319 "qemu: unknown boot parameter '%s' in '%s'\n",
2320 buf, optarg);
2321 exit(1);
2324 if (legacy ||
2325 get_param_value(buf, sizeof(buf), "order", optarg)) {
2326 validate_bootdevices(buf);
2327 pstrcpy(boot_devices, sizeof(boot_devices), buf);
2329 if (!legacy) {
2330 if (get_param_value(buf, sizeof(buf),
2331 "once", optarg)) {
2332 validate_bootdevices(buf);
2333 standard_boot_devices = qemu_strdup(boot_devices);
2334 pstrcpy(boot_devices, sizeof(boot_devices), buf);
2335 qemu_register_reset(restore_boot_devices,
2336 standard_boot_devices);
2338 if (get_param_value(buf, sizeof(buf),
2339 "menu", optarg)) {
2340 if (!strcmp(buf, "on")) {
2341 boot_menu = 1;
2342 } else if (!strcmp(buf, "off")) {
2343 boot_menu = 0;
2344 } else {
2345 fprintf(stderr,
2346 "qemu: invalid option value '%s'\n",
2347 buf);
2348 exit(1);
2353 break;
2354 case QEMU_OPTION_fda:
2355 case QEMU_OPTION_fdb:
2356 drive_add(IF_FLOPPY, popt->index - QEMU_OPTION_fda,
2357 optarg, FD_OPTS);
2358 break;
2359 case QEMU_OPTION_no_fd_bootchk:
2360 fd_bootchk = 0;
2361 break;
2362 case QEMU_OPTION_netdev:
2363 if (net_client_parse(qemu_find_opts("netdev"), optarg) == -1) {
2364 exit(1);
2366 break;
2367 case QEMU_OPTION_net:
2368 if (net_client_parse(qemu_find_opts("net"), optarg) == -1) {
2369 exit(1);
2371 break;
2372 #ifdef CONFIG_SLIRP
2373 case QEMU_OPTION_tftp:
2374 legacy_tftp_prefix = optarg;
2375 break;
2376 case QEMU_OPTION_bootp:
2377 legacy_bootp_filename = optarg;
2378 break;
2379 case QEMU_OPTION_redir:
2380 if (net_slirp_redir(optarg) < 0)
2381 exit(1);
2382 break;
2383 #endif
2384 case QEMU_OPTION_bt:
2385 add_device_config(DEV_BT, optarg);
2386 break;
2387 case QEMU_OPTION_audio_help:
2388 if (!(audio_available())) {
2389 printf("Option %s not supported for this target\n", popt->name);
2390 exit(1);
2392 AUD_help ();
2393 exit (0);
2394 break;
2395 case QEMU_OPTION_soundhw:
2396 if (!(audio_available())) {
2397 printf("Option %s not supported for this target\n", popt->name);
2398 exit(1);
2400 select_soundhw (optarg);
2401 break;
2402 case QEMU_OPTION_h:
2403 help(0);
2404 break;
2405 case QEMU_OPTION_version:
2406 version();
2407 exit(0);
2408 break;
2409 case QEMU_OPTION_m: {
2410 int64_t value;
2412 value = strtosz(optarg, NULL);
2413 if (value < 0) {
2414 fprintf(stderr, "qemu: invalid ram size: %s\n", optarg);
2415 exit(1);
2418 /* On 32-bit hosts, QEMU is limited by virtual address space */
2419 if (value > (2047 << 20) && HOST_LONG_BITS == 32) {
2420 fprintf(stderr, "qemu: at most 2047 MB RAM can be simulated\n");
2421 exit(1);
2423 if (value != (uint64_t)(ram_addr_t)value) {
2424 fprintf(stderr, "qemu: ram size too large\n");
2425 exit(1);
2427 ram_size = value;
2428 break;
2430 case QEMU_OPTION_mempath:
2431 mem_path = optarg;
2432 break;
2433 #ifdef MAP_POPULATE
2434 case QEMU_OPTION_mem_prealloc:
2435 mem_prealloc = 1;
2436 break;
2437 #endif
2438 case QEMU_OPTION_d:
2439 set_cpu_log(optarg);
2440 break;
2441 case QEMU_OPTION_s:
2442 gdbstub_dev = "tcp::" DEFAULT_GDBSTUB_PORT;
2443 break;
2444 case QEMU_OPTION_gdb:
2445 gdbstub_dev = optarg;
2446 break;
2447 case QEMU_OPTION_L:
2448 data_dir = optarg;
2449 break;
2450 case QEMU_OPTION_bios:
2451 bios_name = optarg;
2452 break;
2453 case QEMU_OPTION_singlestep:
2454 singlestep = 1;
2455 break;
2456 case QEMU_OPTION_S:
2457 autostart = 0;
2458 break;
2459 case QEMU_OPTION_k:
2460 keyboard_layout = optarg;
2461 break;
2462 case QEMU_OPTION_localtime:
2463 rtc_utc = 0;
2464 break;
2465 case QEMU_OPTION_vga:
2466 select_vgahw (optarg);
2467 break;
2468 case QEMU_OPTION_g:
2470 const char *p;
2471 int w, h, depth;
2472 p = optarg;
2473 w = strtol(p, (char **)&p, 10);
2474 if (w <= 0) {
2475 graphic_error:
2476 fprintf(stderr, "qemu: invalid resolution or depth\n");
2477 exit(1);
2479 if (*p != 'x')
2480 goto graphic_error;
2481 p++;
2482 h = strtol(p, (char **)&p, 10);
2483 if (h <= 0)
2484 goto graphic_error;
2485 if (*p == 'x') {
2486 p++;
2487 depth = strtol(p, (char **)&p, 10);
2488 if (depth != 8 && depth != 15 && depth != 16 &&
2489 depth != 24 && depth != 32)
2490 goto graphic_error;
2491 } else if (*p == '\0') {
2492 depth = graphic_depth;
2493 } else {
2494 goto graphic_error;
2497 graphic_width = w;
2498 graphic_height = h;
2499 graphic_depth = depth;
2501 break;
2502 case QEMU_OPTION_echr:
2504 char *r;
2505 term_escape_char = strtol(optarg, &r, 0);
2506 if (r == optarg)
2507 printf("Bad argument to echr\n");
2508 break;
2510 case QEMU_OPTION_monitor:
2511 monitor_parse(optarg, "readline");
2512 default_monitor = 0;
2513 break;
2514 case QEMU_OPTION_qmp:
2515 monitor_parse(optarg, "control");
2516 default_monitor = 0;
2517 break;
2518 case QEMU_OPTION_mon:
2519 opts = qemu_opts_parse(qemu_find_opts("mon"), optarg, 1);
2520 if (!opts) {
2521 exit(1);
2523 default_monitor = 0;
2524 break;
2525 case QEMU_OPTION_chardev:
2526 opts = qemu_opts_parse(qemu_find_opts("chardev"), optarg, 1);
2527 if (!opts) {
2528 exit(1);
2530 break;
2531 case QEMU_OPTION_fsdev:
2532 olist = qemu_find_opts("fsdev");
2533 if (!olist) {
2534 fprintf(stderr, "fsdev is not supported by this qemu build.\n");
2535 exit(1);
2537 opts = qemu_opts_parse(olist, optarg, 1);
2538 if (!opts) {
2539 fprintf(stderr, "parse error: %s\n", optarg);
2540 exit(1);
2542 break;
2543 case QEMU_OPTION_virtfs: {
2544 QemuOpts *fsdev;
2545 QemuOpts *device;
2547 olist = qemu_find_opts("virtfs");
2548 if (!olist) {
2549 fprintf(stderr, "virtfs is not supported by this qemu build.\n");
2550 exit(1);
2552 opts = qemu_opts_parse(olist, optarg, 1);
2553 if (!opts) {
2554 fprintf(stderr, "parse error: %s\n", optarg);
2555 exit(1);
2558 if (qemu_opt_get(opts, "fstype") == NULL ||
2559 qemu_opt_get(opts, "mount_tag") == NULL ||
2560 qemu_opt_get(opts, "path") == NULL ||
2561 qemu_opt_get(opts, "security_model") == NULL) {
2562 fprintf(stderr, "Usage: -virtfs fstype,path=/share_path/,"
2563 "security_model=[mapped|passthrough|none],"
2564 "mount_tag=tag.\n");
2565 exit(1);
2568 fsdev = qemu_opts_create(qemu_find_opts("fsdev"),
2569 qemu_opt_get(opts, "mount_tag"), 1);
2570 if (!fsdev) {
2571 fprintf(stderr, "duplicate fsdev id: %s\n",
2572 qemu_opt_get(opts, "mount_tag"));
2573 exit(1);
2575 qemu_opt_set(fsdev, "fstype", qemu_opt_get(opts, "fstype"));
2576 qemu_opt_set(fsdev, "path", qemu_opt_get(opts, "path"));
2577 qemu_opt_set(fsdev, "security_model",
2578 qemu_opt_get(opts, "security_model"));
2580 device = qemu_opts_create(qemu_find_opts("device"), NULL, 0);
2581 qemu_opt_set(device, "driver", "virtio-9p-pci");
2582 qemu_opt_set(device, "fsdev",
2583 qemu_opt_get(opts, "mount_tag"));
2584 qemu_opt_set(device, "mount_tag",
2585 qemu_opt_get(opts, "mount_tag"));
2586 break;
2588 case QEMU_OPTION_serial:
2589 add_device_config(DEV_SERIAL, optarg);
2590 default_serial = 0;
2591 if (strncmp(optarg, "mon:", 4) == 0) {
2592 default_monitor = 0;
2594 break;
2595 case QEMU_OPTION_watchdog:
2596 if (watchdog) {
2597 fprintf(stderr,
2598 "qemu: only one watchdog option may be given\n");
2599 return 1;
2601 watchdog = optarg;
2602 break;
2603 case QEMU_OPTION_watchdog_action:
2604 if (select_watchdog_action(optarg) == -1) {
2605 fprintf(stderr, "Unknown -watchdog-action parameter\n");
2606 exit(1);
2608 break;
2609 case QEMU_OPTION_virtiocon:
2610 add_device_config(DEV_VIRTCON, optarg);
2611 default_virtcon = 0;
2612 if (strncmp(optarg, "mon:", 4) == 0) {
2613 default_monitor = 0;
2615 break;
2616 case QEMU_OPTION_parallel:
2617 add_device_config(DEV_PARALLEL, optarg);
2618 default_parallel = 0;
2619 if (strncmp(optarg, "mon:", 4) == 0) {
2620 default_monitor = 0;
2622 break;
2623 case QEMU_OPTION_debugcon:
2624 add_device_config(DEV_DEBUGCON, optarg);
2625 break;
2626 case QEMU_OPTION_loadvm:
2627 loadvm = optarg;
2628 break;
2629 case QEMU_OPTION_full_screen:
2630 full_screen = 1;
2631 break;
2632 #ifdef CONFIG_SDL
2633 case QEMU_OPTION_no_frame:
2634 no_frame = 1;
2635 break;
2636 case QEMU_OPTION_alt_grab:
2637 alt_grab = 1;
2638 break;
2639 case QEMU_OPTION_ctrl_grab:
2640 ctrl_grab = 1;
2641 break;
2642 case QEMU_OPTION_no_quit:
2643 no_quit = 1;
2644 break;
2645 case QEMU_OPTION_sdl:
2646 display_type = DT_SDL;
2647 break;
2648 #else
2649 case QEMU_OPTION_no_frame:
2650 case QEMU_OPTION_alt_grab:
2651 case QEMU_OPTION_ctrl_grab:
2652 case QEMU_OPTION_no_quit:
2653 case QEMU_OPTION_sdl:
2654 fprintf(stderr, "SDL support is disabled\n");
2655 exit(1);
2656 #endif
2657 case QEMU_OPTION_pidfile:
2658 pid_file = optarg;
2659 break;
2660 case QEMU_OPTION_win2k_hack:
2661 win2k_install_hack = 1;
2662 break;
2663 case QEMU_OPTION_rtc_td_hack:
2664 rtc_td_hack = 1;
2665 break;
2666 case QEMU_OPTION_acpitable:
2667 do_acpitable_option(optarg);
2668 break;
2669 case QEMU_OPTION_smbios:
2670 do_smbios_option(optarg);
2671 break;
2672 case QEMU_OPTION_enable_kvm:
2673 olist = qemu_find_opts("machine");
2674 qemu_opts_reset(olist);
2675 qemu_opts_parse(olist, "accel=kvm", 0);
2676 break;
2677 case QEMU_OPTION_machine:
2678 olist = qemu_find_opts("machine");
2679 qemu_opts_reset(olist);
2680 opts = qemu_opts_parse(olist, optarg, 0);
2681 if (!opts) {
2682 fprintf(stderr, "parse error: %s\n", optarg);
2683 exit(1);
2685 break;
2686 case QEMU_OPTION_usb:
2687 usb_enabled = 1;
2688 break;
2689 case QEMU_OPTION_usbdevice:
2690 usb_enabled = 1;
2691 add_device_config(DEV_USB, optarg);
2692 break;
2693 case QEMU_OPTION_device:
2694 if (!qemu_opts_parse(qemu_find_opts("device"), optarg, 1)) {
2695 exit(1);
2697 break;
2698 case QEMU_OPTION_smp:
2699 smp_parse(optarg);
2700 if (smp_cpus < 1) {
2701 fprintf(stderr, "Invalid number of CPUs\n");
2702 exit(1);
2704 if (max_cpus < smp_cpus) {
2705 fprintf(stderr, "maxcpus must be equal to or greater than "
2706 "smp\n");
2707 exit(1);
2709 if (max_cpus > 255) {
2710 fprintf(stderr, "Unsupported number of maxcpus\n");
2711 exit(1);
2713 break;
2714 case QEMU_OPTION_vnc:
2715 #ifdef CONFIG_VNC
2716 display_remote++;
2717 vnc_display = optarg;
2718 #else
2719 fprintf(stderr, "VNC support is disabled\n");
2720 exit(1);
2721 #endif
2722 break;
2723 case QEMU_OPTION_no_acpi:
2724 acpi_enabled = 0;
2725 break;
2726 case QEMU_OPTION_no_hpet:
2727 no_hpet = 1;
2728 break;
2729 case QEMU_OPTION_balloon:
2730 if (balloon_parse(optarg) < 0) {
2731 fprintf(stderr, "Unknown -balloon argument %s\n", optarg);
2732 exit(1);
2734 break;
2735 case QEMU_OPTION_no_reboot:
2736 no_reboot = 1;
2737 break;
2738 case QEMU_OPTION_no_shutdown:
2739 no_shutdown = 1;
2740 break;
2741 case QEMU_OPTION_show_cursor:
2742 cursor_hide = 0;
2743 break;
2744 case QEMU_OPTION_uuid:
2745 if(qemu_uuid_parse(optarg, qemu_uuid) < 0) {
2746 fprintf(stderr, "Fail to parse UUID string."
2747 " Wrong format.\n");
2748 exit(1);
2750 break;
2751 case QEMU_OPTION_option_rom:
2752 if (nb_option_roms >= MAX_OPTION_ROMS) {
2753 fprintf(stderr, "Too many option ROMs\n");
2754 exit(1);
2756 opts = qemu_opts_parse(qemu_find_opts("option-rom"), optarg, 1);
2757 option_rom[nb_option_roms].name = qemu_opt_get(opts, "romfile");
2758 option_rom[nb_option_roms].bootindex =
2759 qemu_opt_get_number(opts, "bootindex", -1);
2760 if (!option_rom[nb_option_roms].name) {
2761 fprintf(stderr, "Option ROM file is not specified\n");
2762 exit(1);
2764 nb_option_roms++;
2765 break;
2766 case QEMU_OPTION_semihosting:
2767 semihosting_enabled = 1;
2768 break;
2769 case QEMU_OPTION_name:
2770 qemu_name = qemu_strdup(optarg);
2772 char *p = strchr(qemu_name, ',');
2773 if (p != NULL) {
2774 *p++ = 0;
2775 if (strncmp(p, "process=", 8)) {
2776 fprintf(stderr, "Unknown subargument %s to -name\n", p);
2777 exit(1);
2779 p += 8;
2780 os_set_proc_name(p);
2783 break;
2784 case QEMU_OPTION_prom_env:
2785 if (nb_prom_envs >= MAX_PROM_ENVS) {
2786 fprintf(stderr, "Too many prom variables\n");
2787 exit(1);
2789 prom_envs[nb_prom_envs] = optarg;
2790 nb_prom_envs++;
2791 break;
2792 case QEMU_OPTION_old_param:
2793 old_param = 1;
2794 break;
2795 case QEMU_OPTION_clock:
2796 configure_alarms(optarg);
2797 break;
2798 case QEMU_OPTION_startdate:
2799 configure_rtc_date_offset(optarg, 1);
2800 break;
2801 case QEMU_OPTION_rtc:
2802 opts = qemu_opts_parse(qemu_find_opts("rtc"), optarg, 0);
2803 if (!opts) {
2804 exit(1);
2806 configure_rtc(opts);
2807 break;
2808 case QEMU_OPTION_tb_size:
2809 tb_size = strtol(optarg, NULL, 0);
2810 if (tb_size < 0)
2811 tb_size = 0;
2812 break;
2813 case QEMU_OPTION_icount:
2814 icount_option = optarg;
2815 break;
2816 case QEMU_OPTION_incoming:
2817 incoming = optarg;
2818 incoming_expected = true;
2819 break;
2820 case QEMU_OPTION_nodefaults:
2821 default_serial = 0;
2822 default_parallel = 0;
2823 default_virtcon = 0;
2824 default_monitor = 0;
2825 default_vga = 0;
2826 default_net = 0;
2827 default_floppy = 0;
2828 default_cdrom = 0;
2829 default_sdcard = 0;
2830 break;
2831 case QEMU_OPTION_xen_domid:
2832 if (!(xen_available())) {
2833 printf("Option %s not supported for this target\n", popt->name);
2834 exit(1);
2836 xen_domid = atoi(optarg);
2837 break;
2838 case QEMU_OPTION_xen_create:
2839 if (!(xen_available())) {
2840 printf("Option %s not supported for this target\n", popt->name);
2841 exit(1);
2843 xen_mode = XEN_CREATE;
2844 break;
2845 case QEMU_OPTION_xen_attach:
2846 if (!(xen_available())) {
2847 printf("Option %s not supported for this target\n", popt->name);
2848 exit(1);
2850 xen_mode = XEN_ATTACH;
2851 break;
2852 #ifdef CONFIG_SIMPLE_TRACE
2853 case QEMU_OPTION_trace:
2854 opts = qemu_opts_parse(qemu_find_opts("trace"), optarg, 0);
2855 if (opts) {
2856 trace_file = qemu_opt_get(opts, "file");
2858 break;
2859 #endif
2860 case QEMU_OPTION_readconfig:
2862 int ret = qemu_read_config_file(optarg);
2863 if (ret < 0) {
2864 fprintf(stderr, "read config %s: %s\n", optarg,
2865 strerror(-ret));
2866 exit(1);
2868 break;
2870 case QEMU_OPTION_spice:
2871 olist = qemu_find_opts("spice");
2872 if (!olist) {
2873 fprintf(stderr, "spice is not supported by this qemu build.\n");
2874 exit(1);
2876 opts = qemu_opts_parse(olist, optarg, 0);
2877 if (!opts) {
2878 fprintf(stderr, "parse error: %s\n", optarg);
2879 exit(1);
2881 break;
2882 case QEMU_OPTION_writeconfig:
2884 FILE *fp;
2885 if (strcmp(optarg, "-") == 0) {
2886 fp = stdout;
2887 } else {
2888 fp = fopen(optarg, "w");
2889 if (fp == NULL) {
2890 fprintf(stderr, "open %s: %s\n", optarg, strerror(errno));
2891 exit(1);
2894 qemu_config_write(fp);
2895 fclose(fp);
2896 break;
2898 default:
2899 os_parse_cmd_args(popt->index, optarg);
2903 loc_set_none();
2905 if (!st_init(trace_file)) {
2906 fprintf(stderr, "warning: unable to initialize simple trace backend\n");
2909 /* If no data_dir is specified then try to find it relative to the
2910 executable path. */
2911 if (!data_dir) {
2912 data_dir = os_find_datadir(argv[0]);
2914 /* If all else fails use the install patch specified when building. */
2915 if (!data_dir) {
2916 data_dir = CONFIG_QEMU_DATADIR;
2920 * Default to max_cpus = smp_cpus, in case the user doesn't
2921 * specify a max_cpus value.
2923 if (!max_cpus)
2924 max_cpus = smp_cpus;
2926 machine->max_cpus = machine->max_cpus ?: 1; /* Default to UP */
2927 if (smp_cpus > machine->max_cpus) {
2928 fprintf(stderr, "Number of SMP cpus requested (%d), exceeds max cpus "
2929 "supported by machine `%s' (%d)\n", smp_cpus, machine->name,
2930 machine->max_cpus);
2931 exit(1);
2935 * Get the default machine options from the machine if it is not already
2936 * specified either by the configuration file or by the command line.
2938 if (machine->default_machine_opts) {
2939 QemuOptsList *list = qemu_find_opts("machine");
2940 const char *p = NULL;
2942 if (!QTAILQ_EMPTY(&list->head)) {
2943 p = qemu_opt_get(QTAILQ_FIRST(&list->head), "accel");
2945 if (p == NULL) {
2946 opts = qemu_opts_parse(qemu_find_opts("machine"),
2947 machine->default_machine_opts, 0);
2948 if (!opts) {
2949 fprintf(stderr, "parse error for machine %s: %s\n",
2950 machine->name, machine->default_machine_opts);
2951 exit(1);
2956 qemu_opts_foreach(qemu_find_opts("device"), default_driver_check, NULL, 0);
2957 qemu_opts_foreach(qemu_find_opts("global"), default_driver_check, NULL, 0);
2959 if (machine->no_serial) {
2960 default_serial = 0;
2962 if (machine->no_parallel) {
2963 default_parallel = 0;
2965 if (!machine->use_virtcon) {
2966 default_virtcon = 0;
2968 if (machine->no_vga) {
2969 default_vga = 0;
2971 if (machine->no_floppy) {
2972 default_floppy = 0;
2974 if (machine->no_cdrom) {
2975 default_cdrom = 0;
2977 if (machine->no_sdcard) {
2978 default_sdcard = 0;
2981 if (display_type == DT_NOGRAPHIC) {
2982 if (default_parallel)
2983 add_device_config(DEV_PARALLEL, "null");
2984 if (default_serial && default_monitor) {
2985 add_device_config(DEV_SERIAL, "mon:stdio");
2986 } else if (default_virtcon && default_monitor) {
2987 add_device_config(DEV_VIRTCON, "mon:stdio");
2988 } else {
2989 if (default_serial)
2990 add_device_config(DEV_SERIAL, "stdio");
2991 if (default_virtcon)
2992 add_device_config(DEV_VIRTCON, "stdio");
2993 if (default_monitor)
2994 monitor_parse("stdio", "readline");
2996 } else {
2997 if (default_serial)
2998 add_device_config(DEV_SERIAL, "vc:80Cx24C");
2999 if (default_parallel)
3000 add_device_config(DEV_PARALLEL, "vc:80Cx24C");
3001 if (default_monitor)
3002 monitor_parse("vc:80Cx24C", "readline");
3003 if (default_virtcon)
3004 add_device_config(DEV_VIRTCON, "vc:80Cx24C");
3006 if (default_vga)
3007 vga_interface_type = VGA_CIRRUS;
3009 socket_init();
3011 if (qemu_opts_foreach(qemu_find_opts("chardev"), chardev_init_func, NULL, 1) != 0)
3012 exit(1);
3013 #ifdef CONFIG_VIRTFS
3014 if (qemu_opts_foreach(qemu_find_opts("fsdev"), fsdev_init_func, NULL, 1) != 0) {
3015 exit(1);
3017 #endif
3019 os_daemonize();
3021 if (pid_file && qemu_create_pidfile(pid_file) != 0) {
3022 os_pidfile_error();
3023 exit(1);
3026 configure_accelerator();
3028 if (qemu_init_main_loop()) {
3029 fprintf(stderr, "qemu_init_main_loop failed\n");
3030 exit(1);
3032 linux_boot = (kernel_filename != NULL);
3034 if (!linux_boot && *kernel_cmdline != '\0') {
3035 fprintf(stderr, "-append only allowed with -kernel option\n");
3036 exit(1);
3039 if (!linux_boot && initrd_filename != NULL) {
3040 fprintf(stderr, "-initrd only allowed with -kernel option\n");
3041 exit(1);
3044 os_set_line_buffering();
3046 if (init_timer_alarm() < 0) {
3047 fprintf(stderr, "could not initialize alarm timer\n");
3048 exit(1);
3050 configure_icount(icount_option);
3052 if (net_init_clients() < 0) {
3053 exit(1);
3056 /* init the bluetooth world */
3057 if (foreach_device_config(DEV_BT, bt_parse))
3058 exit(1);
3060 /* init the memory */
3061 if (ram_size == 0)
3062 ram_size = DEFAULT_RAM_SIZE * 1024 * 1024;
3064 /* init the dynamic translator */
3065 cpu_exec_init_all(tb_size * 1024 * 1024);
3067 bdrv_init_with_whitelist();
3069 blk_mig_init();
3071 /* open the virtual block devices */
3072 if (snapshot)
3073 qemu_opts_foreach(qemu_find_opts("drive"), drive_enable_snapshot, NULL, 0);
3074 if (qemu_opts_foreach(qemu_find_opts("drive"), drive_init_func, &machine->use_scsi, 1) != 0)
3075 exit(1);
3077 default_drive(default_cdrom, snapshot, machine->use_scsi,
3078 IF_DEFAULT, 2, CDROM_OPTS);
3079 default_drive(default_floppy, snapshot, machine->use_scsi,
3080 IF_FLOPPY, 0, FD_OPTS);
3081 default_drive(default_sdcard, snapshot, machine->use_scsi,
3082 IF_SD, 0, SD_OPTS);
3084 register_savevm_live(NULL, "ram", 0, 4, NULL, ram_save_live, NULL,
3085 ram_load, NULL);
3087 if (nb_numa_nodes > 0) {
3088 int i;
3090 if (nb_numa_nodes > smp_cpus) {
3091 nb_numa_nodes = smp_cpus;
3094 /* If no memory size if given for any node, assume the default case
3095 * and distribute the available memory equally across all nodes
3097 for (i = 0; i < nb_numa_nodes; i++) {
3098 if (node_mem[i] != 0)
3099 break;
3101 if (i == nb_numa_nodes) {
3102 uint64_t usedmem = 0;
3104 /* On Linux, the each node's border has to be 8MB aligned,
3105 * the final node gets the rest.
3107 for (i = 0; i < nb_numa_nodes - 1; i++) {
3108 node_mem[i] = (ram_size / nb_numa_nodes) & ~((1 << 23UL) - 1);
3109 usedmem += node_mem[i];
3111 node_mem[i] = ram_size - usedmem;
3114 for (i = 0; i < nb_numa_nodes; i++) {
3115 if (node_cpumask[i] != 0)
3116 break;
3118 /* assigning the VCPUs round-robin is easier to implement, guest OSes
3119 * must cope with this anyway, because there are BIOSes out there in
3120 * real machines which also use this scheme.
3122 if (i == nb_numa_nodes) {
3123 for (i = 0; i < smp_cpus; i++) {
3124 node_cpumask[i % nb_numa_nodes] |= 1 << i;
3129 if (qemu_opts_foreach(qemu_find_opts("mon"), mon_init_func, NULL, 1) != 0) {
3130 exit(1);
3133 if (foreach_device_config(DEV_SERIAL, serial_parse) < 0)
3134 exit(1);
3135 if (foreach_device_config(DEV_PARALLEL, parallel_parse) < 0)
3136 exit(1);
3137 if (foreach_device_config(DEV_VIRTCON, virtcon_parse) < 0)
3138 exit(1);
3139 if (foreach_device_config(DEV_DEBUGCON, debugcon_parse) < 0)
3140 exit(1);
3142 module_call_init(MODULE_INIT_DEVICE);
3144 if (qemu_opts_foreach(qemu_find_opts("device"), device_help_func, NULL, 0) != 0)
3145 exit(0);
3147 if (watchdog) {
3148 i = select_watchdog(watchdog);
3149 if (i > 0)
3150 exit (i == 1 ? 1 : 0);
3153 if (machine->compat_props) {
3154 qdev_prop_register_global_list(machine->compat_props);
3156 qemu_add_globals();
3158 machine->init(ram_size, boot_devices,
3159 kernel_filename, kernel_cmdline, initrd_filename, cpu_model);
3161 cpu_synchronize_all_post_init();
3163 set_numa_modes();
3165 current_machine = machine;
3167 /* init USB devices */
3168 if (usb_enabled) {
3169 if (foreach_device_config(DEV_USB, usb_parse) < 0)
3170 exit(1);
3173 /* init generic devices */
3174 if (qemu_opts_foreach(qemu_find_opts("device"), device_init_func, NULL, 1) != 0)
3175 exit(1);
3177 net_check_clients();
3179 /* just use the first displaystate for the moment */
3180 ds = get_displaystate();
3182 if (using_spice)
3183 display_remote++;
3184 if (display_type == DT_DEFAULT && !display_remote) {
3185 #if defined(CONFIG_SDL) || defined(CONFIG_COCOA)
3186 display_type = DT_SDL;
3187 #elif defined(CONFIG_VNC)
3188 vnc_display = "localhost:0,to=99";
3189 show_vnc_port = 1;
3190 #else
3191 display_type = DT_NONE;
3192 #endif
3196 /* init local displays */
3197 switch (display_type) {
3198 case DT_NOGRAPHIC:
3199 break;
3200 #if defined(CONFIG_CURSES)
3201 case DT_CURSES:
3202 curses_display_init(ds, full_screen);
3203 break;
3204 #endif
3205 #if defined(CONFIG_SDL)
3206 case DT_SDL:
3207 sdl_display_init(ds, full_screen, no_frame);
3208 break;
3209 #elif defined(CONFIG_COCOA)
3210 case DT_SDL:
3211 cocoa_display_init(ds, full_screen);
3212 break;
3213 #endif
3214 default:
3215 break;
3218 /* must be after terminal init, SDL library changes signal handlers */
3219 os_setup_signal_handling();
3221 #ifdef CONFIG_VNC
3222 /* init remote displays */
3223 if (vnc_display) {
3224 vnc_display_init(ds);
3225 if (vnc_display_open(ds, vnc_display) < 0)
3226 exit(1);
3228 if (show_vnc_port) {
3229 printf("VNC server running on `%s'\n", vnc_display_local_addr(ds));
3232 #endif
3233 #ifdef CONFIG_SPICE
3234 if (using_spice && !qxl_enabled) {
3235 qemu_spice_display_init(ds);
3237 #endif
3239 /* display setup */
3240 dpy_resize(ds);
3241 dcl = ds->listeners;
3242 while (dcl != NULL) {
3243 if (dcl->dpy_refresh != NULL) {
3244 ds->gui_timer = qemu_new_timer_ms(rt_clock, gui_update, ds);
3245 qemu_mod_timer(ds->gui_timer, qemu_get_clock_ms(rt_clock));
3246 break;
3248 dcl = dcl->next;
3250 if (ds->gui_timer == NULL) {
3251 nographic_timer = qemu_new_timer_ms(rt_clock, nographic_update, NULL);
3252 qemu_mod_timer(nographic_timer, qemu_get_clock_ms(rt_clock));
3254 text_consoles_set_display(ds);
3256 if (gdbstub_dev && gdbserver_start(gdbstub_dev) < 0) {
3257 fprintf(stderr, "qemu: could not open gdbserver on device '%s'\n",
3258 gdbstub_dev);
3259 exit(1);
3262 qdev_machine_creation_done();
3264 if (rom_load_all() != 0) {
3265 fprintf(stderr, "rom loading failed\n");
3266 exit(1);
3269 /* TODO: once all bus devices are qdevified, this should be done
3270 * when bus is created by qdev.c */
3271 qemu_register_reset(qbus_reset_all_fn, sysbus_get_default());
3272 qemu_run_machine_init_done_notifiers();
3274 qemu_system_reset();
3275 if (loadvm) {
3276 if (load_vmstate(loadvm) < 0) {
3277 autostart = 0;
3281 if (incoming) {
3282 int ret = qemu_start_incoming_migration(incoming);
3283 if (ret < 0) {
3284 fprintf(stderr, "Migration failed. Exit code %s(%d), exiting.\n",
3285 incoming, ret);
3286 exit(ret);
3288 } else if (autostart) {
3289 vm_start();
3292 os_setup_post();
3294 main_loop();
3295 quit_timers();
3296 net_cleanup();
3298 return 0;