kvm: Add CPUID support for VIA CPU
[qemu/mdroth.git] / vl.c
blobd7f905df3a658dc417358aae396491f91a5c8700
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 },
293 { .driver = "qxl-vga", .flag = &default_vga },
296 static int default_driver_check(QemuOpts *opts, void *opaque)
298 const char *driver = qemu_opt_get(opts, "driver");
299 int i;
301 if (!driver)
302 return 0;
303 for (i = 0; i < ARRAY_SIZE(default_list); i++) {
304 if (strcmp(default_list[i].driver, driver) != 0)
305 continue;
306 *(default_list[i].flag) = 0;
308 return 0;
311 /***********************************************************/
312 /* real time host monotonic timer */
314 /***********************************************************/
315 /* host time/date access */
316 void qemu_get_timedate(struct tm *tm, int offset)
318 time_t ti;
319 struct tm *ret;
321 time(&ti);
322 ti += offset;
323 if (rtc_date_offset == -1) {
324 if (rtc_utc)
325 ret = gmtime(&ti);
326 else
327 ret = localtime(&ti);
328 } else {
329 ti -= rtc_date_offset;
330 ret = gmtime(&ti);
333 memcpy(tm, ret, sizeof(struct tm));
336 int qemu_timedate_diff(struct tm *tm)
338 time_t seconds;
340 if (rtc_date_offset == -1)
341 if (rtc_utc)
342 seconds = mktimegm(tm);
343 else
344 seconds = mktime(tm);
345 else
346 seconds = mktimegm(tm) + rtc_date_offset;
348 return seconds - time(NULL);
351 void rtc_change_mon_event(struct tm *tm)
353 QObject *data;
355 data = qobject_from_jsonf("{ 'offset': %d }", qemu_timedate_diff(tm));
356 monitor_protocol_event(QEVENT_RTC_CHANGE, data);
357 qobject_decref(data);
360 static void configure_rtc_date_offset(const char *startdate, int legacy)
362 time_t rtc_start_date;
363 struct tm tm;
365 if (!strcmp(startdate, "now") && legacy) {
366 rtc_date_offset = -1;
367 } else {
368 if (sscanf(startdate, "%d-%d-%dT%d:%d:%d",
369 &tm.tm_year,
370 &tm.tm_mon,
371 &tm.tm_mday,
372 &tm.tm_hour,
373 &tm.tm_min,
374 &tm.tm_sec) == 6) {
375 /* OK */
376 } else if (sscanf(startdate, "%d-%d-%d",
377 &tm.tm_year,
378 &tm.tm_mon,
379 &tm.tm_mday) == 3) {
380 tm.tm_hour = 0;
381 tm.tm_min = 0;
382 tm.tm_sec = 0;
383 } else {
384 goto date_fail;
386 tm.tm_year -= 1900;
387 tm.tm_mon--;
388 rtc_start_date = mktimegm(&tm);
389 if (rtc_start_date == -1) {
390 date_fail:
391 fprintf(stderr, "Invalid date format. Valid formats are:\n"
392 "'2006-06-17T16:01:21' or '2006-06-17'\n");
393 exit(1);
395 rtc_date_offset = time(NULL) - rtc_start_date;
399 static void configure_rtc(QemuOpts *opts)
401 const char *value;
403 value = qemu_opt_get(opts, "base");
404 if (value) {
405 if (!strcmp(value, "utc")) {
406 rtc_utc = 1;
407 } else if (!strcmp(value, "localtime")) {
408 rtc_utc = 0;
409 } else {
410 configure_rtc_date_offset(value, 0);
413 value = qemu_opt_get(opts, "clock");
414 if (value) {
415 if (!strcmp(value, "host")) {
416 rtc_clock = host_clock;
417 } else if (!strcmp(value, "vm")) {
418 rtc_clock = vm_clock;
419 } else {
420 fprintf(stderr, "qemu: invalid option value '%s'\n", value);
421 exit(1);
424 value = qemu_opt_get(opts, "driftfix");
425 if (value) {
426 if (!strcmp(value, "slew")) {
427 rtc_td_hack = 1;
428 } else if (!strcmp(value, "none")) {
429 rtc_td_hack = 0;
430 } else {
431 fprintf(stderr, "qemu: invalid option value '%s'\n", value);
432 exit(1);
437 /***********************************************************/
438 /* Bluetooth support */
439 static int nb_hcis;
440 static int cur_hci;
441 static struct HCIInfo *hci_table[MAX_NICS];
443 static struct bt_vlan_s {
444 struct bt_scatternet_s net;
445 int id;
446 struct bt_vlan_s *next;
447 } *first_bt_vlan;
449 /* find or alloc a new bluetooth "VLAN" */
450 static struct bt_scatternet_s *qemu_find_bt_vlan(int id)
452 struct bt_vlan_s **pvlan, *vlan;
453 for (vlan = first_bt_vlan; vlan != NULL; vlan = vlan->next) {
454 if (vlan->id == id)
455 return &vlan->net;
457 vlan = qemu_mallocz(sizeof(struct bt_vlan_s));
458 vlan->id = id;
459 pvlan = &first_bt_vlan;
460 while (*pvlan != NULL)
461 pvlan = &(*pvlan)->next;
462 *pvlan = vlan;
463 return &vlan->net;
466 static void null_hci_send(struct HCIInfo *hci, const uint8_t *data, int len)
470 static int null_hci_addr_set(struct HCIInfo *hci, const uint8_t *bd_addr)
472 return -ENOTSUP;
475 static struct HCIInfo null_hci = {
476 .cmd_send = null_hci_send,
477 .sco_send = null_hci_send,
478 .acl_send = null_hci_send,
479 .bdaddr_set = null_hci_addr_set,
482 struct HCIInfo *qemu_next_hci(void)
484 if (cur_hci == nb_hcis)
485 return &null_hci;
487 return hci_table[cur_hci++];
490 static struct HCIInfo *hci_init(const char *str)
492 char *endp;
493 struct bt_scatternet_s *vlan = 0;
495 if (!strcmp(str, "null"))
496 /* null */
497 return &null_hci;
498 else if (!strncmp(str, "host", 4) && (str[4] == '\0' || str[4] == ':'))
499 /* host[:hciN] */
500 return bt_host_hci(str[4] ? str + 5 : "hci0");
501 else if (!strncmp(str, "hci", 3)) {
502 /* hci[,vlan=n] */
503 if (str[3]) {
504 if (!strncmp(str + 3, ",vlan=", 6)) {
505 vlan = qemu_find_bt_vlan(strtol(str + 9, &endp, 0));
506 if (*endp)
507 vlan = 0;
509 } else
510 vlan = qemu_find_bt_vlan(0);
511 if (vlan)
512 return bt_new_hci(vlan);
515 fprintf(stderr, "qemu: Unknown bluetooth HCI `%s'.\n", str);
517 return 0;
520 static int bt_hci_parse(const char *str)
522 struct HCIInfo *hci;
523 bdaddr_t bdaddr;
525 if (nb_hcis >= MAX_NICS) {
526 fprintf(stderr, "qemu: Too many bluetooth HCIs (max %i).\n", MAX_NICS);
527 return -1;
530 hci = hci_init(str);
531 if (!hci)
532 return -1;
534 bdaddr.b[0] = 0x52;
535 bdaddr.b[1] = 0x54;
536 bdaddr.b[2] = 0x00;
537 bdaddr.b[3] = 0x12;
538 bdaddr.b[4] = 0x34;
539 bdaddr.b[5] = 0x56 + nb_hcis;
540 hci->bdaddr_set(hci, bdaddr.b);
542 hci_table[nb_hcis++] = hci;
544 return 0;
547 static void bt_vhci_add(int vlan_id)
549 struct bt_scatternet_s *vlan = qemu_find_bt_vlan(vlan_id);
551 if (!vlan->slave)
552 fprintf(stderr, "qemu: warning: adding a VHCI to "
553 "an empty scatternet %i\n", vlan_id);
555 bt_vhci_init(bt_new_hci(vlan));
558 static struct bt_device_s *bt_device_add(const char *opt)
560 struct bt_scatternet_s *vlan;
561 int vlan_id = 0;
562 char *endp = strstr(opt, ",vlan=");
563 int len = (endp ? endp - opt : strlen(opt)) + 1;
564 char devname[10];
566 pstrcpy(devname, MIN(sizeof(devname), len), opt);
568 if (endp) {
569 vlan_id = strtol(endp + 6, &endp, 0);
570 if (*endp) {
571 fprintf(stderr, "qemu: unrecognised bluetooth vlan Id\n");
572 return 0;
576 vlan = qemu_find_bt_vlan(vlan_id);
578 if (!vlan->slave)
579 fprintf(stderr, "qemu: warning: adding a slave device to "
580 "an empty scatternet %i\n", vlan_id);
582 if (!strcmp(devname, "keyboard"))
583 return bt_keyboard_init(vlan);
585 fprintf(stderr, "qemu: unsupported bluetooth device `%s'\n", devname);
586 return 0;
589 static int bt_parse(const char *opt)
591 const char *endp, *p;
592 int vlan;
594 if (strstart(opt, "hci", &endp)) {
595 if (!*endp || *endp == ',') {
596 if (*endp)
597 if (!strstart(endp, ",vlan=", 0))
598 opt = endp + 1;
600 return bt_hci_parse(opt);
602 } else if (strstart(opt, "vhci", &endp)) {
603 if (!*endp || *endp == ',') {
604 if (*endp) {
605 if (strstart(endp, ",vlan=", &p)) {
606 vlan = strtol(p, (char **) &endp, 0);
607 if (*endp) {
608 fprintf(stderr, "qemu: bad scatternet '%s'\n", p);
609 return 1;
611 } else {
612 fprintf(stderr, "qemu: bad parameter '%s'\n", endp + 1);
613 return 1;
615 } else
616 vlan = 0;
618 bt_vhci_add(vlan);
619 return 0;
621 } else if (strstart(opt, "device:", &endp))
622 return !bt_device_add(endp);
624 fprintf(stderr, "qemu: bad bluetooth parameter '%s'\n", opt);
625 return 1;
628 /***********************************************************/
629 /* QEMU Block devices */
631 #define HD_OPTS "media=disk"
632 #define CDROM_OPTS "media=cdrom"
633 #define FD_OPTS ""
634 #define PFLASH_OPTS ""
635 #define MTD_OPTS ""
636 #define SD_OPTS ""
638 static int drive_init_func(QemuOpts *opts, void *opaque)
640 int *use_scsi = opaque;
642 return drive_init(opts, *use_scsi) == NULL;
645 static int drive_enable_snapshot(QemuOpts *opts, void *opaque)
647 if (NULL == qemu_opt_get(opts, "snapshot")) {
648 qemu_opt_set(opts, "snapshot", "on");
650 return 0;
653 static void default_drive(int enable, int snapshot, int use_scsi,
654 BlockInterfaceType type, int index,
655 const char *optstr)
657 QemuOpts *opts;
659 if (type == IF_DEFAULT) {
660 type = use_scsi ? IF_SCSI : IF_IDE;
663 if (!enable || drive_get_by_index(type, index)) {
664 return;
667 opts = drive_add(type, index, NULL, optstr);
668 if (snapshot) {
669 drive_enable_snapshot(opts, NULL);
671 if (!drive_init(opts, use_scsi)) {
672 exit(1);
676 void qemu_register_boot_set(QEMUBootSetHandler *func, void *opaque)
678 boot_set_handler = func;
679 boot_set_opaque = opaque;
682 int qemu_boot_set(const char *boot_devices)
684 if (!boot_set_handler) {
685 return -EINVAL;
687 return boot_set_handler(boot_set_opaque, boot_devices);
690 static void validate_bootdevices(char *devices)
692 /* We just do some generic consistency checks */
693 const char *p;
694 int bitmap = 0;
696 for (p = devices; *p != '\0'; p++) {
697 /* Allowed boot devices are:
698 * a-b: floppy disk drives
699 * c-f: IDE disk drives
700 * g-m: machine implementation dependant drives
701 * n-p: network devices
702 * It's up to each machine implementation to check if the given boot
703 * devices match the actual hardware implementation and firmware
704 * features.
706 if (*p < 'a' || *p > 'p') {
707 fprintf(stderr, "Invalid boot device '%c'\n", *p);
708 exit(1);
710 if (bitmap & (1 << (*p - 'a'))) {
711 fprintf(stderr, "Boot device '%c' was given twice\n", *p);
712 exit(1);
714 bitmap |= 1 << (*p - 'a');
718 static void restore_boot_devices(void *opaque)
720 char *standard_boot_devices = opaque;
721 static int first = 1;
723 /* Restore boot order and remove ourselves after the first boot */
724 if (first) {
725 first = 0;
726 return;
729 qemu_boot_set(standard_boot_devices);
731 qemu_unregister_reset(restore_boot_devices, standard_boot_devices);
732 qemu_free(standard_boot_devices);
735 void add_boot_device_path(int32_t bootindex, DeviceState *dev,
736 const char *suffix)
738 FWBootEntry *node, *i;
740 if (bootindex < 0) {
741 return;
744 assert(dev != NULL || suffix != NULL);
746 node = qemu_mallocz(sizeof(FWBootEntry));
747 node->bootindex = bootindex;
748 node->suffix = suffix ? qemu_strdup(suffix) : NULL;
749 node->dev = dev;
751 QTAILQ_FOREACH(i, &fw_boot_order, link) {
752 if (i->bootindex == bootindex) {
753 fprintf(stderr, "Two devices with same boot index %d\n", bootindex);
754 exit(1);
755 } else if (i->bootindex < bootindex) {
756 continue;
758 QTAILQ_INSERT_BEFORE(i, node, link);
759 return;
761 QTAILQ_INSERT_TAIL(&fw_boot_order, node, link);
765 * This function returns null terminated string that consist of new line
766 * separated device paths.
768 * memory pointed by "size" is assigned total length of the array in bytes
771 char *get_boot_devices_list(uint32_t *size)
773 FWBootEntry *i;
774 uint32_t total = 0;
775 char *list = NULL;
777 QTAILQ_FOREACH(i, &fw_boot_order, link) {
778 char *devpath = NULL, *bootpath;
779 int len;
781 if (i->dev) {
782 devpath = qdev_get_fw_dev_path(i->dev);
783 assert(devpath);
786 if (i->suffix && devpath) {
787 size_t bootpathlen = strlen(devpath) + strlen(i->suffix) + 1;
789 bootpath = qemu_malloc(bootpathlen);
790 snprintf(bootpath, bootpathlen, "%s%s", devpath, i->suffix);
791 qemu_free(devpath);
792 } else if (devpath) {
793 bootpath = devpath;
794 } else {
795 bootpath = qemu_strdup(i->suffix);
796 assert(bootpath);
799 if (total) {
800 list[total-1] = '\n';
802 len = strlen(bootpath) + 1;
803 list = qemu_realloc(list, total + len);
804 memcpy(&list[total], bootpath, len);
805 total += len;
806 qemu_free(bootpath);
809 *size = total;
811 return list;
814 static void numa_add(const char *optarg)
816 char option[128];
817 char *endptr;
818 unsigned long long value, endvalue;
819 int nodenr;
821 optarg = get_opt_name(option, 128, optarg, ',') + 1;
822 if (!strcmp(option, "node")) {
823 if (get_param_value(option, 128, "nodeid", optarg) == 0) {
824 nodenr = nb_numa_nodes;
825 } else {
826 nodenr = strtoull(option, NULL, 10);
829 if (get_param_value(option, 128, "mem", optarg) == 0) {
830 node_mem[nodenr] = 0;
831 } else {
832 int64_t sval;
833 sval = strtosz(option, NULL);
834 if (sval < 0) {
835 fprintf(stderr, "qemu: invalid numa mem size: %s\n", optarg);
836 exit(1);
838 node_mem[nodenr] = sval;
840 if (get_param_value(option, 128, "cpus", optarg) == 0) {
841 node_cpumask[nodenr] = 0;
842 } else {
843 value = strtoull(option, &endptr, 10);
844 if (value >= 64) {
845 value = 63;
846 fprintf(stderr, "only 64 CPUs in NUMA mode supported.\n");
847 } else {
848 if (*endptr == '-') {
849 endvalue = strtoull(endptr+1, &endptr, 10);
850 if (endvalue >= 63) {
851 endvalue = 62;
852 fprintf(stderr,
853 "only 63 CPUs in NUMA mode supported.\n");
855 value = (2ULL << endvalue) - (1ULL << value);
856 } else {
857 value = 1ULL << value;
860 node_cpumask[nodenr] = value;
862 nb_numa_nodes++;
864 return;
867 static void smp_parse(const char *optarg)
869 int smp, sockets = 0, threads = 0, cores = 0;
870 char *endptr;
871 char option[128];
873 smp = strtoul(optarg, &endptr, 10);
874 if (endptr != optarg) {
875 if (*endptr == ',') {
876 endptr++;
879 if (get_param_value(option, 128, "sockets", endptr) != 0)
880 sockets = strtoull(option, NULL, 10);
881 if (get_param_value(option, 128, "cores", endptr) != 0)
882 cores = strtoull(option, NULL, 10);
883 if (get_param_value(option, 128, "threads", endptr) != 0)
884 threads = strtoull(option, NULL, 10);
885 if (get_param_value(option, 128, "maxcpus", endptr) != 0)
886 max_cpus = strtoull(option, NULL, 10);
888 /* compute missing values, prefer sockets over cores over threads */
889 if (smp == 0 || sockets == 0) {
890 sockets = sockets > 0 ? sockets : 1;
891 cores = cores > 0 ? cores : 1;
892 threads = threads > 0 ? threads : 1;
893 if (smp == 0) {
894 smp = cores * threads * sockets;
896 } else {
897 if (cores == 0) {
898 threads = threads > 0 ? threads : 1;
899 cores = smp / (sockets * threads);
900 } else {
901 threads = smp / (cores * sockets);
904 smp_cpus = smp;
905 smp_cores = cores > 0 ? cores : 1;
906 smp_threads = threads > 0 ? threads : 1;
907 if (max_cpus == 0)
908 max_cpus = smp_cpus;
911 /***********************************************************/
912 /* USB devices */
914 static int usb_device_add(const char *devname)
916 const char *p;
917 USBDevice *dev = NULL;
919 if (!usb_enabled)
920 return -1;
922 /* drivers with .usbdevice_name entry in USBDeviceInfo */
923 dev = usbdevice_create(devname);
924 if (dev)
925 goto done;
927 /* the other ones */
928 if (strstart(devname, "host:", &p)) {
929 dev = usb_host_device_open(p);
930 } else if (!strcmp(devname, "bt") || strstart(devname, "bt:", &p)) {
931 dev = usb_bt_init(devname[2] ? hci_init(p) :
932 bt_new_hci(qemu_find_bt_vlan(0)));
933 } else {
934 return -1;
936 if (!dev)
937 return -1;
939 done:
940 return 0;
943 static int usb_device_del(const char *devname)
945 int bus_num, addr;
946 const char *p;
948 if (strstart(devname, "host:", &p))
949 return usb_host_device_close(p);
951 if (!usb_enabled)
952 return -1;
954 p = strchr(devname, '.');
955 if (!p)
956 return -1;
957 bus_num = strtoul(devname, NULL, 0);
958 addr = strtoul(p + 1, NULL, 0);
960 return usb_device_delete_addr(bus_num, addr);
963 static int usb_parse(const char *cmdline)
965 int r;
966 r = usb_device_add(cmdline);
967 if (r < 0) {
968 fprintf(stderr, "qemu: could not add USB device '%s'\n", cmdline);
970 return r;
973 void do_usb_add(Monitor *mon, const QDict *qdict)
975 const char *devname = qdict_get_str(qdict, "devname");
976 if (usb_device_add(devname) < 0) {
977 error_report("could not add USB device '%s'", devname);
981 void do_usb_del(Monitor *mon, const QDict *qdict)
983 const char *devname = qdict_get_str(qdict, "devname");
984 if (usb_device_del(devname) < 0) {
985 error_report("could not delete USB device '%s'", devname);
989 /***********************************************************/
990 /* PCMCIA/Cardbus */
992 static struct pcmcia_socket_entry_s {
993 PCMCIASocket *socket;
994 struct pcmcia_socket_entry_s *next;
995 } *pcmcia_sockets = 0;
997 void pcmcia_socket_register(PCMCIASocket *socket)
999 struct pcmcia_socket_entry_s *entry;
1001 entry = qemu_malloc(sizeof(struct pcmcia_socket_entry_s));
1002 entry->socket = socket;
1003 entry->next = pcmcia_sockets;
1004 pcmcia_sockets = entry;
1007 void pcmcia_socket_unregister(PCMCIASocket *socket)
1009 struct pcmcia_socket_entry_s *entry, **ptr;
1011 ptr = &pcmcia_sockets;
1012 for (entry = *ptr; entry; ptr = &entry->next, entry = *ptr)
1013 if (entry->socket == socket) {
1014 *ptr = entry->next;
1015 qemu_free(entry);
1019 void pcmcia_info(Monitor *mon)
1021 struct pcmcia_socket_entry_s *iter;
1023 if (!pcmcia_sockets)
1024 monitor_printf(mon, "No PCMCIA sockets\n");
1026 for (iter = pcmcia_sockets; iter; iter = iter->next)
1027 monitor_printf(mon, "%s: %s\n", iter->socket->slot_string,
1028 iter->socket->attached ? iter->socket->card_string :
1029 "Empty");
1032 /***********************************************************/
1033 /* machine registration */
1035 static QEMUMachine *first_machine = NULL;
1036 QEMUMachine *current_machine = NULL;
1038 int qemu_register_machine(QEMUMachine *m)
1040 QEMUMachine **pm;
1041 pm = &first_machine;
1042 while (*pm != NULL)
1043 pm = &(*pm)->next;
1044 m->next = NULL;
1045 *pm = m;
1046 return 0;
1049 static QEMUMachine *find_machine(const char *name)
1051 QEMUMachine *m;
1053 for(m = first_machine; m != NULL; m = m->next) {
1054 if (!strcmp(m->name, name))
1055 return m;
1056 if (m->alias && !strcmp(m->alias, name))
1057 return m;
1059 return NULL;
1062 static QEMUMachine *find_default_machine(void)
1064 QEMUMachine *m;
1066 for(m = first_machine; m != NULL; m = m->next) {
1067 if (m->is_default) {
1068 return m;
1071 return NULL;
1074 /***********************************************************/
1075 /* main execution loop */
1077 static void gui_update(void *opaque)
1079 uint64_t interval = GUI_REFRESH_INTERVAL;
1080 DisplayState *ds = opaque;
1081 DisplayChangeListener *dcl = ds->listeners;
1083 qemu_flush_coalesced_mmio_buffer();
1084 dpy_refresh(ds);
1086 while (dcl != NULL) {
1087 if (dcl->gui_timer_interval &&
1088 dcl->gui_timer_interval < interval)
1089 interval = dcl->gui_timer_interval;
1090 dcl = dcl->next;
1092 qemu_mod_timer(ds->gui_timer, interval + qemu_get_clock_ms(rt_clock));
1095 static void nographic_update(void *opaque)
1097 uint64_t interval = GUI_REFRESH_INTERVAL;
1099 qemu_flush_coalesced_mmio_buffer();
1100 qemu_mod_timer(nographic_timer, interval + qemu_get_clock_ms(rt_clock));
1103 struct vm_change_state_entry {
1104 VMChangeStateHandler *cb;
1105 void *opaque;
1106 QLIST_ENTRY (vm_change_state_entry) entries;
1109 static QLIST_HEAD(vm_change_state_head, vm_change_state_entry) vm_change_state_head;
1111 VMChangeStateEntry *qemu_add_vm_change_state_handler(VMChangeStateHandler *cb,
1112 void *opaque)
1114 VMChangeStateEntry *e;
1116 e = qemu_mallocz(sizeof (*e));
1118 e->cb = cb;
1119 e->opaque = opaque;
1120 QLIST_INSERT_HEAD(&vm_change_state_head, e, entries);
1121 return e;
1124 void qemu_del_vm_change_state_handler(VMChangeStateEntry *e)
1126 QLIST_REMOVE (e, entries);
1127 qemu_free (e);
1130 void vm_state_notify(int running, int reason)
1132 VMChangeStateEntry *e;
1134 trace_vm_state_notify(running, reason);
1136 for (e = vm_change_state_head.lh_first; e; e = e->entries.le_next) {
1137 e->cb(e->opaque, running, reason);
1141 void vm_start(void)
1143 if (!vm_running) {
1144 cpu_enable_ticks();
1145 vm_running = 1;
1146 vm_state_notify(1, 0);
1147 resume_all_vcpus();
1148 monitor_protocol_event(QEVENT_RESUME, NULL);
1152 /* reset/shutdown handler */
1154 typedef struct QEMUResetEntry {
1155 QTAILQ_ENTRY(QEMUResetEntry) entry;
1156 QEMUResetHandler *func;
1157 void *opaque;
1158 } QEMUResetEntry;
1160 static QTAILQ_HEAD(reset_handlers, QEMUResetEntry) reset_handlers =
1161 QTAILQ_HEAD_INITIALIZER(reset_handlers);
1162 static int reset_requested;
1163 static int shutdown_requested, shutdown_signal = -1;
1164 static pid_t shutdown_pid;
1165 static int powerdown_requested;
1166 static int debug_requested;
1167 static int vmstop_requested;
1169 int qemu_shutdown_requested_get(void)
1171 return shutdown_requested;
1174 int qemu_reset_requested_get(void)
1176 return reset_requested;
1179 int qemu_shutdown_requested(void)
1181 int r = shutdown_requested;
1182 shutdown_requested = 0;
1183 return r;
1186 void qemu_kill_report(void)
1188 if (shutdown_signal != -1) {
1189 fprintf(stderr, "qemu: terminating on signal %d", shutdown_signal);
1190 if (shutdown_pid == 0) {
1191 /* This happens for eg ^C at the terminal, so it's worth
1192 * avoiding printing an odd message in that case.
1194 fputc('\n', stderr);
1195 } else {
1196 fprintf(stderr, " from pid %d\n", shutdown_pid);
1198 shutdown_signal = -1;
1202 int qemu_reset_requested(void)
1204 int r = reset_requested;
1205 reset_requested = 0;
1206 return r;
1209 int qemu_powerdown_requested(void)
1211 int r = powerdown_requested;
1212 powerdown_requested = 0;
1213 return r;
1216 static int qemu_debug_requested(void)
1218 int r = debug_requested;
1219 debug_requested = 0;
1220 return r;
1223 static int qemu_vmstop_requested(void)
1225 int r = vmstop_requested;
1226 vmstop_requested = 0;
1227 return r;
1230 void qemu_register_reset(QEMUResetHandler *func, void *opaque)
1232 QEMUResetEntry *re = qemu_mallocz(sizeof(QEMUResetEntry));
1234 re->func = func;
1235 re->opaque = opaque;
1236 QTAILQ_INSERT_TAIL(&reset_handlers, re, entry);
1239 void qemu_unregister_reset(QEMUResetHandler *func, void *opaque)
1241 QEMUResetEntry *re;
1243 QTAILQ_FOREACH(re, &reset_handlers, entry) {
1244 if (re->func == func && re->opaque == opaque) {
1245 QTAILQ_REMOVE(&reset_handlers, re, entry);
1246 qemu_free(re);
1247 return;
1252 void qemu_system_reset(void)
1254 QEMUResetEntry *re, *nre;
1256 /* reset all devices */
1257 QTAILQ_FOREACH_SAFE(re, &reset_handlers, entry, nre) {
1258 re->func(re->opaque);
1260 monitor_protocol_event(QEVENT_RESET, NULL);
1261 cpu_synchronize_all_post_reset();
1264 void qemu_system_reset_request(void)
1266 if (no_reboot) {
1267 shutdown_requested = 1;
1268 } else {
1269 reset_requested = 1;
1271 cpu_stop_current();
1272 qemu_notify_event();
1275 void qemu_system_killed(int signal, pid_t pid)
1277 shutdown_signal = signal;
1278 shutdown_pid = pid;
1279 qemu_system_shutdown_request();
1282 void qemu_system_shutdown_request(void)
1284 shutdown_requested = 1;
1285 qemu_notify_event();
1288 void qemu_system_powerdown_request(void)
1290 powerdown_requested = 1;
1291 qemu_notify_event();
1294 void qemu_system_debug_request(void)
1296 debug_requested = 1;
1297 qemu_notify_event();
1300 void qemu_system_vmstop_request(int reason)
1302 vmstop_requested = reason;
1303 qemu_notify_event();
1306 void main_loop_wait(int nonblocking)
1308 fd_set rfds, wfds, xfds;
1309 int ret, nfds;
1310 struct timeval tv;
1311 int timeout;
1313 if (nonblocking)
1314 timeout = 0;
1315 else {
1316 timeout = qemu_calculate_timeout();
1317 qemu_bh_update_timeout(&timeout);
1320 os_host_main_loop_wait(&timeout);
1322 tv.tv_sec = timeout / 1000;
1323 tv.tv_usec = (timeout % 1000) * 1000;
1325 /* poll any events */
1326 /* XXX: separate device handlers from system ones */
1327 nfds = -1;
1328 FD_ZERO(&rfds);
1329 FD_ZERO(&wfds);
1330 FD_ZERO(&xfds);
1331 qemu_iohandler_fill(&nfds, &rfds, &wfds, &xfds);
1332 slirp_select_fill(&nfds, &rfds, &wfds, &xfds);
1334 qemu_mutex_unlock_iothread();
1335 ret = select(nfds + 1, &rfds, &wfds, &xfds, &tv);
1336 qemu_mutex_lock_iothread();
1338 qemu_iohandler_poll(&rfds, &wfds, &xfds, ret);
1339 slirp_select_poll(&rfds, &wfds, &xfds, (ret < 0));
1341 qemu_run_all_timers();
1343 /* Check bottom-halves last in case any of the earlier events triggered
1344 them. */
1345 qemu_bh_poll();
1349 #ifndef CONFIG_IOTHREAD
1350 static int vm_request_pending(void)
1352 return powerdown_requested ||
1353 reset_requested ||
1354 shutdown_requested ||
1355 debug_requested ||
1356 vmstop_requested;
1358 #endif
1360 qemu_irq qemu_system_powerdown;
1362 static void main_loop(void)
1364 bool nonblocking = false;
1365 #ifdef CONFIG_PROFILER
1366 int64_t ti;
1367 #endif
1368 int r;
1370 qemu_main_loop_start();
1372 for (;;) {
1373 #ifndef CONFIG_IOTHREAD
1374 nonblocking = cpu_exec_all();
1375 if (vm_request_pending()) {
1376 nonblocking = true;
1378 #endif
1379 #ifdef CONFIG_PROFILER
1380 ti = profile_getclock();
1381 #endif
1382 main_loop_wait(nonblocking);
1383 #ifdef CONFIG_PROFILER
1384 dev_time += profile_getclock() - ti;
1385 #endif
1387 if (qemu_debug_requested()) {
1388 vm_stop(VMSTOP_DEBUG);
1390 if (qemu_shutdown_requested()) {
1391 qemu_kill_report();
1392 monitor_protocol_event(QEVENT_SHUTDOWN, NULL);
1393 if (no_shutdown) {
1394 vm_stop(VMSTOP_SHUTDOWN);
1395 no_shutdown = 0;
1396 } else
1397 break;
1399 if (qemu_reset_requested()) {
1400 pause_all_vcpus();
1401 cpu_synchronize_all_states();
1402 qemu_system_reset();
1403 resume_all_vcpus();
1405 if (qemu_powerdown_requested()) {
1406 monitor_protocol_event(QEVENT_POWERDOWN, NULL);
1407 qemu_irq_raise(qemu_system_powerdown);
1409 if ((r = qemu_vmstop_requested())) {
1410 vm_stop(r);
1413 bdrv_close_all();
1414 pause_all_vcpus();
1417 static void version(void)
1419 printf("QEMU emulator version " QEMU_VERSION QEMU_PKGVERSION ", Copyright (c) 2003-2008 Fabrice Bellard\n");
1422 static void help(int exitcode)
1424 const char *options_help =
1425 #define DEF(option, opt_arg, opt_enum, opt_help, arch_mask) \
1426 opt_help
1427 #define DEFHEADING(text) stringify(text) "\n"
1428 #include "qemu-options.def"
1429 #undef DEF
1430 #undef DEFHEADING
1431 #undef GEN_DOCS
1433 version();
1434 printf("usage: %s [options] [disk_image]\n"
1435 "\n"
1436 "'disk_image' is a raw hard disk image for IDE hard disk 0\n"
1437 "\n"
1438 "%s\n"
1439 "During emulation, the following keys are useful:\n"
1440 "ctrl-alt-f toggle full screen\n"
1441 "ctrl-alt-n switch to virtual console 'n'\n"
1442 "ctrl-alt toggle mouse and keyboard grab\n"
1443 "\n"
1444 "When using -nographic, press 'ctrl-a h' to get some help.\n",
1445 "qemu",
1446 options_help);
1447 exit(exitcode);
1450 #define HAS_ARG 0x0001
1452 typedef struct QEMUOption {
1453 const char *name;
1454 int flags;
1455 int index;
1456 uint32_t arch_mask;
1457 } QEMUOption;
1459 static const QEMUOption qemu_options[] = {
1460 { "h", 0, QEMU_OPTION_h, QEMU_ARCH_ALL },
1461 #define DEF(option, opt_arg, opt_enum, opt_help, arch_mask) \
1462 { option, opt_arg, opt_enum, arch_mask },
1463 #define DEFHEADING(text)
1464 #include "qemu-options.def"
1465 #undef DEF
1466 #undef DEFHEADING
1467 #undef GEN_DOCS
1468 { NULL },
1470 static void select_vgahw (const char *p)
1472 const char *opts;
1474 default_vga = 0;
1475 vga_interface_type = VGA_NONE;
1476 if (strstart(p, "std", &opts)) {
1477 vga_interface_type = VGA_STD;
1478 } else if (strstart(p, "cirrus", &opts)) {
1479 vga_interface_type = VGA_CIRRUS;
1480 } else if (strstart(p, "vmware", &opts)) {
1481 vga_interface_type = VGA_VMWARE;
1482 } else if (strstart(p, "xenfb", &opts)) {
1483 vga_interface_type = VGA_XENFB;
1484 } else if (strstart(p, "qxl", &opts)) {
1485 vga_interface_type = VGA_QXL;
1486 } else if (!strstart(p, "none", &opts)) {
1487 invalid_vga:
1488 fprintf(stderr, "Unknown vga type: %s\n", p);
1489 exit(1);
1491 while (*opts) {
1492 const char *nextopt;
1494 if (strstart(opts, ",retrace=", &nextopt)) {
1495 opts = nextopt;
1496 if (strstart(opts, "dumb", &nextopt))
1497 vga_retrace_method = VGA_RETRACE_DUMB;
1498 else if (strstart(opts, "precise", &nextopt))
1499 vga_retrace_method = VGA_RETRACE_PRECISE;
1500 else goto invalid_vga;
1501 } else goto invalid_vga;
1502 opts = nextopt;
1506 static DisplayType select_display(const char *p)
1508 const char *opts;
1509 DisplayType display = DT_DEFAULT;
1511 if (strstart(p, "sdl", &opts)) {
1512 #ifdef CONFIG_SDL
1513 display = DT_SDL;
1514 while (*opts) {
1515 const char *nextopt;
1517 if (strstart(opts, ",frame=", &nextopt)) {
1518 opts = nextopt;
1519 if (strstart(opts, "on", &nextopt)) {
1520 no_frame = 0;
1521 } else if (strstart(opts, "off", &nextopt)) {
1522 no_frame = 1;
1523 } else {
1524 goto invalid_sdl_args;
1526 } else if (strstart(opts, ",alt_grab=", &nextopt)) {
1527 opts = nextopt;
1528 if (strstart(opts, "on", &nextopt)) {
1529 alt_grab = 1;
1530 } else if (strstart(opts, "off", &nextopt)) {
1531 alt_grab = 0;
1532 } else {
1533 goto invalid_sdl_args;
1535 } else if (strstart(opts, ",ctrl_grab=", &nextopt)) {
1536 opts = nextopt;
1537 if (strstart(opts, "on", &nextopt)) {
1538 ctrl_grab = 1;
1539 } else if (strstart(opts, "off", &nextopt)) {
1540 ctrl_grab = 0;
1541 } else {
1542 goto invalid_sdl_args;
1544 } else if (strstart(opts, ",window_close=", &nextopt)) {
1545 opts = nextopt;
1546 if (strstart(opts, "on", &nextopt)) {
1547 no_quit = 0;
1548 } else if (strstart(opts, "off", &nextopt)) {
1549 no_quit = 1;
1550 } else {
1551 goto invalid_sdl_args;
1553 } else {
1554 invalid_sdl_args:
1555 fprintf(stderr, "Invalid SDL option string: %s\n", p);
1556 exit(1);
1558 opts = nextopt;
1560 #else
1561 fprintf(stderr, "SDL support is disabled\n");
1562 exit(1);
1563 #endif
1564 } else if (strstart(p, "vnc", &opts)) {
1565 #ifdef CONFIG_VNC
1566 display_remote++;
1568 if (*opts) {
1569 const char *nextopt;
1571 if (strstart(opts, "=", &nextopt)) {
1572 vnc_display = nextopt;
1575 if (!vnc_display) {
1576 fprintf(stderr, "VNC requires a display argument vnc=<display>\n");
1577 exit(1);
1579 #else
1580 fprintf(stderr, "VNC support is disabled\n");
1581 exit(1);
1582 #endif
1583 } else if (strstart(p, "curses", &opts)) {
1584 #ifdef CONFIG_CURSES
1585 display = DT_CURSES;
1586 #else
1587 fprintf(stderr, "Curses support is disabled\n");
1588 exit(1);
1589 #endif
1590 } else if (strstart(p, "none", &opts)) {
1591 display = DT_NONE;
1592 } else {
1593 fprintf(stderr, "Unknown display type: %s\n", p);
1594 exit(1);
1597 return display;
1600 static int balloon_parse(const char *arg)
1602 QemuOpts *opts;
1604 if (strcmp(arg, "none") == 0) {
1605 return 0;
1608 if (!strncmp(arg, "virtio", 6)) {
1609 if (arg[6] == ',') {
1610 /* have params -> parse them */
1611 opts = qemu_opts_parse(qemu_find_opts("device"), arg+7, 0);
1612 if (!opts)
1613 return -1;
1614 } else {
1615 /* create empty opts */
1616 opts = qemu_opts_create(qemu_find_opts("device"), NULL, 0);
1618 qemu_opt_set(opts, "driver", "virtio-balloon");
1619 return 0;
1622 return -1;
1625 char *qemu_find_file(int type, const char *name)
1627 int len;
1628 const char *subdir;
1629 char *buf;
1631 /* If name contains path separators then try it as a straight path. */
1632 if ((strchr(name, '/') || strchr(name, '\\'))
1633 && access(name, R_OK) == 0) {
1634 return qemu_strdup(name);
1636 switch (type) {
1637 case QEMU_FILE_TYPE_BIOS:
1638 subdir = "";
1639 break;
1640 case QEMU_FILE_TYPE_KEYMAP:
1641 subdir = "keymaps/";
1642 break;
1643 default:
1644 abort();
1646 len = strlen(data_dir) + strlen(name) + strlen(subdir) + 2;
1647 buf = qemu_mallocz(len);
1648 snprintf(buf, len, "%s/%s%s", data_dir, subdir, name);
1649 if (access(buf, R_OK)) {
1650 qemu_free(buf);
1651 return NULL;
1653 return buf;
1656 static int device_help_func(QemuOpts *opts, void *opaque)
1658 return qdev_device_help(opts);
1661 static int device_init_func(QemuOpts *opts, void *opaque)
1663 DeviceState *dev;
1665 dev = qdev_device_add(opts);
1666 if (!dev)
1667 return -1;
1668 return 0;
1671 static int chardev_init_func(QemuOpts *opts, void *opaque)
1673 CharDriverState *chr;
1675 chr = qemu_chr_open_opts(opts, NULL);
1676 if (!chr)
1677 return -1;
1678 return 0;
1681 #ifdef CONFIG_VIRTFS
1682 static int fsdev_init_func(QemuOpts *opts, void *opaque)
1684 int ret;
1685 ret = qemu_fsdev_add(opts);
1687 return ret;
1689 #endif
1691 static int mon_init_func(QemuOpts *opts, void *opaque)
1693 CharDriverState *chr;
1694 const char *chardev;
1695 const char *mode;
1696 int flags;
1698 mode = qemu_opt_get(opts, "mode");
1699 if (mode == NULL) {
1700 mode = "readline";
1702 if (strcmp(mode, "readline") == 0) {
1703 flags = MONITOR_USE_READLINE;
1704 } else if (strcmp(mode, "control") == 0) {
1705 flags = MONITOR_USE_CONTROL;
1706 } else {
1707 fprintf(stderr, "unknown monitor mode \"%s\"\n", mode);
1708 exit(1);
1711 if (qemu_opt_get_bool(opts, "pretty", 0))
1712 flags |= MONITOR_USE_PRETTY;
1714 if (qemu_opt_get_bool(opts, "default", 0))
1715 flags |= MONITOR_IS_DEFAULT;
1717 chardev = qemu_opt_get(opts, "chardev");
1718 chr = qemu_chr_find(chardev);
1719 if (chr == NULL) {
1720 fprintf(stderr, "chardev \"%s\" not found\n", chardev);
1721 exit(1);
1724 monitor_init(chr, flags);
1725 return 0;
1728 static void monitor_parse(const char *optarg, const char *mode)
1730 static int monitor_device_index = 0;
1731 QemuOpts *opts;
1732 const char *p;
1733 char label[32];
1734 int def = 0;
1736 if (strstart(optarg, "chardev:", &p)) {
1737 snprintf(label, sizeof(label), "%s", p);
1738 } else {
1739 snprintf(label, sizeof(label), "compat_monitor%d",
1740 monitor_device_index);
1741 if (monitor_device_index == 0) {
1742 def = 1;
1744 opts = qemu_chr_parse_compat(label, optarg);
1745 if (!opts) {
1746 fprintf(stderr, "parse error: %s\n", optarg);
1747 exit(1);
1751 opts = qemu_opts_create(qemu_find_opts("mon"), label, 1);
1752 if (!opts) {
1753 fprintf(stderr, "duplicate chardev: %s\n", label);
1754 exit(1);
1756 qemu_opt_set(opts, "mode", mode);
1757 qemu_opt_set(opts, "chardev", label);
1758 if (def)
1759 qemu_opt_set(opts, "default", "on");
1760 monitor_device_index++;
1763 struct device_config {
1764 enum {
1765 DEV_USB, /* -usbdevice */
1766 DEV_BT, /* -bt */
1767 DEV_SERIAL, /* -serial */
1768 DEV_PARALLEL, /* -parallel */
1769 DEV_VIRTCON, /* -virtioconsole */
1770 DEV_DEBUGCON, /* -debugcon */
1771 } type;
1772 const char *cmdline;
1773 QTAILQ_ENTRY(device_config) next;
1775 QTAILQ_HEAD(, device_config) device_configs = QTAILQ_HEAD_INITIALIZER(device_configs);
1777 static void add_device_config(int type, const char *cmdline)
1779 struct device_config *conf;
1781 conf = qemu_mallocz(sizeof(*conf));
1782 conf->type = type;
1783 conf->cmdline = cmdline;
1784 QTAILQ_INSERT_TAIL(&device_configs, conf, next);
1787 static int foreach_device_config(int type, int (*func)(const char *cmdline))
1789 struct device_config *conf;
1790 int rc;
1792 QTAILQ_FOREACH(conf, &device_configs, next) {
1793 if (conf->type != type)
1794 continue;
1795 rc = func(conf->cmdline);
1796 if (0 != rc)
1797 return rc;
1799 return 0;
1802 static int serial_parse(const char *devname)
1804 static int index = 0;
1805 char label[32];
1807 if (strcmp(devname, "none") == 0)
1808 return 0;
1809 if (index == MAX_SERIAL_PORTS) {
1810 fprintf(stderr, "qemu: too many serial ports\n");
1811 exit(1);
1813 snprintf(label, sizeof(label), "serial%d", index);
1814 serial_hds[index] = qemu_chr_open(label, devname, NULL);
1815 if (!serial_hds[index]) {
1816 fprintf(stderr, "qemu: could not open serial device '%s': %s\n",
1817 devname, strerror(errno));
1818 return -1;
1820 index++;
1821 return 0;
1824 static int parallel_parse(const char *devname)
1826 static int index = 0;
1827 char label[32];
1829 if (strcmp(devname, "none") == 0)
1830 return 0;
1831 if (index == MAX_PARALLEL_PORTS) {
1832 fprintf(stderr, "qemu: too many parallel ports\n");
1833 exit(1);
1835 snprintf(label, sizeof(label), "parallel%d", index);
1836 parallel_hds[index] = qemu_chr_open(label, devname, NULL);
1837 if (!parallel_hds[index]) {
1838 fprintf(stderr, "qemu: could not open parallel device '%s': %s\n",
1839 devname, strerror(errno));
1840 return -1;
1842 index++;
1843 return 0;
1846 static int virtcon_parse(const char *devname)
1848 QemuOptsList *device = qemu_find_opts("device");
1849 static int index = 0;
1850 char label[32];
1851 QemuOpts *bus_opts, *dev_opts;
1853 if (strcmp(devname, "none") == 0)
1854 return 0;
1855 if (index == MAX_VIRTIO_CONSOLES) {
1856 fprintf(stderr, "qemu: too many virtio consoles\n");
1857 exit(1);
1860 bus_opts = qemu_opts_create(device, NULL, 0);
1861 qemu_opt_set(bus_opts, "driver", "virtio-serial");
1863 dev_opts = qemu_opts_create(device, NULL, 0);
1864 qemu_opt_set(dev_opts, "driver", "virtconsole");
1866 snprintf(label, sizeof(label), "virtcon%d", index);
1867 virtcon_hds[index] = qemu_chr_open(label, devname, NULL);
1868 if (!virtcon_hds[index]) {
1869 fprintf(stderr, "qemu: could not open virtio console '%s': %s\n",
1870 devname, strerror(errno));
1871 return -1;
1873 qemu_opt_set(dev_opts, "chardev", label);
1875 index++;
1876 return 0;
1879 static int debugcon_parse(const char *devname)
1881 QemuOpts *opts;
1883 if (!qemu_chr_open("debugcon", devname, NULL)) {
1884 exit(1);
1886 opts = qemu_opts_create(qemu_find_opts("device"), "debugcon", 1);
1887 if (!opts) {
1888 fprintf(stderr, "qemu: already have a debugcon device\n");
1889 exit(1);
1891 qemu_opt_set(opts, "driver", "isa-debugcon");
1892 qemu_opt_set(opts, "chardev", "debugcon");
1893 return 0;
1896 static int tcg_init(void)
1898 return 0;
1901 static struct {
1902 const char *opt_name;
1903 const char *name;
1904 int (*available)(void);
1905 int (*init)(void);
1906 int *allowed;
1907 } accel_list[] = {
1908 { "tcg", "tcg", tcg_available, tcg_init, &tcg_allowed },
1909 { "xen", "Xen", xen_available, xen_init, &xen_allowed },
1910 { "kvm", "KVM", kvm_available, kvm_init, &kvm_allowed },
1913 static int configure_accelerator(void)
1915 const char *p = NULL;
1916 char buf[10];
1917 int i, ret;
1918 bool accel_initalised = 0;
1919 bool init_failed = 0;
1921 QemuOptsList *list = qemu_find_opts("machine");
1922 if (!QTAILQ_EMPTY(&list->head)) {
1923 p = qemu_opt_get(QTAILQ_FIRST(&list->head), "accel");
1926 if (p == NULL) {
1927 /* Use the default "accelerator", tcg */
1928 p = "tcg";
1931 while (!accel_initalised && *p != '\0') {
1932 if (*p == ':') {
1933 p++;
1935 p = get_opt_name(buf, sizeof (buf), p, ':');
1936 for (i = 0; i < ARRAY_SIZE(accel_list); i++) {
1937 if (strcmp(accel_list[i].opt_name, buf) == 0) {
1938 *(accel_list[i].allowed) = 1;
1939 ret = accel_list[i].init();
1940 if (ret < 0) {
1941 init_failed = 1;
1942 if (!accel_list[i].available()) {
1943 printf("%s not supported for this target\n",
1944 accel_list[i].name);
1945 } else {
1946 fprintf(stderr, "failed to initialize %s: %s\n",
1947 accel_list[i].name,
1948 strerror(-ret));
1950 *(accel_list[i].allowed) = 0;
1951 } else {
1952 accel_initalised = 1;
1954 break;
1957 if (i == ARRAY_SIZE(accel_list)) {
1958 fprintf(stderr, "\"%s\" accelerator does not exist.\n", buf);
1962 if (!accel_initalised) {
1963 fprintf(stderr, "No accelerator found!\n");
1964 exit(1);
1967 if (init_failed) {
1968 fprintf(stderr, "Back to %s accelerator.\n", accel_list[i].name);
1971 return !accel_initalised;
1974 void qemu_add_exit_notifier(Notifier *notify)
1976 notifier_list_add(&exit_notifiers, notify);
1979 void qemu_remove_exit_notifier(Notifier *notify)
1981 notifier_list_remove(&exit_notifiers, notify);
1984 static void qemu_run_exit_notifiers(void)
1986 notifier_list_notify(&exit_notifiers);
1989 void qemu_add_machine_init_done_notifier(Notifier *notify)
1991 notifier_list_add(&machine_init_done_notifiers, notify);
1994 static void qemu_run_machine_init_done_notifiers(void)
1996 notifier_list_notify(&machine_init_done_notifiers);
1999 static const QEMUOption *lookup_opt(int argc, char **argv,
2000 const char **poptarg, int *poptind)
2002 const QEMUOption *popt;
2003 int optind = *poptind;
2004 char *r = argv[optind];
2005 const char *optarg;
2007 loc_set_cmdline(argv, optind, 1);
2008 optind++;
2009 /* Treat --foo the same as -foo. */
2010 if (r[1] == '-')
2011 r++;
2012 popt = qemu_options;
2013 for(;;) {
2014 if (!popt->name) {
2015 error_report("invalid option");
2016 exit(1);
2018 if (!strcmp(popt->name, r + 1))
2019 break;
2020 popt++;
2022 if (popt->flags & HAS_ARG) {
2023 if (optind >= argc) {
2024 error_report("requires an argument");
2025 exit(1);
2027 optarg = argv[optind++];
2028 loc_set_cmdline(argv, optind - 2, 2);
2029 } else {
2030 optarg = NULL;
2033 *poptarg = optarg;
2034 *poptind = optind;
2036 return popt;
2039 int main(int argc, char **argv, char **envp)
2041 const char *gdbstub_dev = NULL;
2042 int i;
2043 int snapshot, linux_boot;
2044 const char *icount_option = NULL;
2045 const char *initrd_filename;
2046 const char *kernel_filename, *kernel_cmdline;
2047 char boot_devices[33] = "cad"; /* default to HD->floppy->CD-ROM */
2048 DisplayState *ds;
2049 DisplayChangeListener *dcl;
2050 int cyls, heads, secs, translation;
2051 QemuOpts *hda_opts = NULL, *opts;
2052 QemuOptsList *olist;
2053 int optind;
2054 const char *optarg;
2055 const char *loadvm = NULL;
2056 QEMUMachine *machine;
2057 const char *cpu_model;
2058 int tb_size;
2059 const char *pid_file = NULL;
2060 const char *incoming = NULL;
2061 #ifdef CONFIG_VNC
2062 int show_vnc_port = 0;
2063 #endif
2064 int defconfig = 1;
2065 const char *trace_file = NULL;
2067 atexit(qemu_run_exit_notifiers);
2068 error_set_progname(argv[0]);
2070 init_clocks();
2072 qemu_cache_utils_init(envp);
2074 QLIST_INIT (&vm_change_state_head);
2075 os_setup_early_signal_handling();
2077 module_call_init(MODULE_INIT_MACHINE);
2078 machine = find_default_machine();
2079 cpu_model = NULL;
2080 initrd_filename = NULL;
2081 ram_size = 0;
2082 snapshot = 0;
2083 kernel_filename = NULL;
2084 kernel_cmdline = "";
2085 cyls = heads = secs = 0;
2086 translation = BIOS_ATA_TRANSLATION_AUTO;
2088 for (i = 0; i < MAX_NODES; i++) {
2089 node_mem[i] = 0;
2090 node_cpumask[i] = 0;
2093 nb_numa_nodes = 0;
2094 nb_nics = 0;
2096 tb_size = 0;
2097 autostart= 1;
2099 /* first pass of option parsing */
2100 optind = 1;
2101 while (optind < argc) {
2102 if (argv[optind][0] != '-') {
2103 /* disk image */
2104 optind++;
2105 continue;
2106 } else {
2107 const QEMUOption *popt;
2109 popt = lookup_opt(argc, argv, &optarg, &optind);
2110 switch (popt->index) {
2111 case QEMU_OPTION_nodefconfig:
2112 defconfig=0;
2113 break;
2118 if (defconfig) {
2119 int ret;
2121 ret = qemu_read_config_file(CONFIG_QEMU_CONFDIR "/qemu.conf");
2122 if (ret < 0 && ret != -ENOENT) {
2123 exit(1);
2126 ret = qemu_read_config_file(arch_config_name);
2127 if (ret < 0 && ret != -ENOENT) {
2128 exit(1);
2131 cpudef_init();
2133 /* second pass of option parsing */
2134 optind = 1;
2135 for(;;) {
2136 if (optind >= argc)
2137 break;
2138 if (argv[optind][0] != '-') {
2139 hda_opts = drive_add(IF_DEFAULT, 0, argv[optind++], HD_OPTS);
2140 } else {
2141 const QEMUOption *popt;
2143 popt = lookup_opt(argc, argv, &optarg, &optind);
2144 if (!(popt->arch_mask & arch_type)) {
2145 printf("Option %s not supported for this target\n", popt->name);
2146 exit(1);
2148 switch(popt->index) {
2149 case QEMU_OPTION_M:
2150 machine = find_machine(optarg);
2151 if (!machine) {
2152 QEMUMachine *m;
2153 printf("Supported machines are:\n");
2154 for(m = first_machine; m != NULL; m = m->next) {
2155 if (m->alias)
2156 printf("%-10s %s (alias of %s)\n",
2157 m->alias, m->desc, m->name);
2158 printf("%-10s %s%s\n",
2159 m->name, m->desc,
2160 m->is_default ? " (default)" : "");
2162 exit(*optarg != '?');
2164 break;
2165 case QEMU_OPTION_cpu:
2166 /* hw initialization will check this */
2167 if (*optarg == '?') {
2168 list_cpus(stdout, &fprintf, optarg);
2169 exit(0);
2170 } else {
2171 cpu_model = optarg;
2173 break;
2174 case QEMU_OPTION_initrd:
2175 initrd_filename = optarg;
2176 break;
2177 case QEMU_OPTION_hda:
2179 char buf[256];
2180 if (cyls == 0)
2181 snprintf(buf, sizeof(buf), "%s", HD_OPTS);
2182 else
2183 snprintf(buf, sizeof(buf),
2184 "%s,cyls=%d,heads=%d,secs=%d%s",
2185 HD_OPTS , cyls, heads, secs,
2186 translation == BIOS_ATA_TRANSLATION_LBA ?
2187 ",trans=lba" :
2188 translation == BIOS_ATA_TRANSLATION_NONE ?
2189 ",trans=none" : "");
2190 drive_add(IF_DEFAULT, 0, optarg, buf);
2191 break;
2193 case QEMU_OPTION_hdb:
2194 case QEMU_OPTION_hdc:
2195 case QEMU_OPTION_hdd:
2196 drive_add(IF_DEFAULT, popt->index - QEMU_OPTION_hda, optarg,
2197 HD_OPTS);
2198 break;
2199 case QEMU_OPTION_drive:
2200 if (drive_def(optarg) == NULL) {
2201 exit(1);
2203 break;
2204 case QEMU_OPTION_set:
2205 if (qemu_set_option(optarg) != 0)
2206 exit(1);
2207 break;
2208 case QEMU_OPTION_global:
2209 if (qemu_global_option(optarg) != 0)
2210 exit(1);
2211 break;
2212 case QEMU_OPTION_mtdblock:
2213 drive_add(IF_MTD, -1, optarg, MTD_OPTS);
2214 break;
2215 case QEMU_OPTION_sd:
2216 drive_add(IF_SD, 0, optarg, SD_OPTS);
2217 break;
2218 case QEMU_OPTION_pflash:
2219 drive_add(IF_PFLASH, -1, optarg, PFLASH_OPTS);
2220 break;
2221 case QEMU_OPTION_snapshot:
2222 snapshot = 1;
2223 break;
2224 case QEMU_OPTION_hdachs:
2226 const char *p;
2227 p = optarg;
2228 cyls = strtol(p, (char **)&p, 0);
2229 if (cyls < 1 || cyls > 16383)
2230 goto chs_fail;
2231 if (*p != ',')
2232 goto chs_fail;
2233 p++;
2234 heads = strtol(p, (char **)&p, 0);
2235 if (heads < 1 || heads > 16)
2236 goto chs_fail;
2237 if (*p != ',')
2238 goto chs_fail;
2239 p++;
2240 secs = strtol(p, (char **)&p, 0);
2241 if (secs < 1 || secs > 63)
2242 goto chs_fail;
2243 if (*p == ',') {
2244 p++;
2245 if (!strcmp(p, "none"))
2246 translation = BIOS_ATA_TRANSLATION_NONE;
2247 else if (!strcmp(p, "lba"))
2248 translation = BIOS_ATA_TRANSLATION_LBA;
2249 else if (!strcmp(p, "auto"))
2250 translation = BIOS_ATA_TRANSLATION_AUTO;
2251 else
2252 goto chs_fail;
2253 } else if (*p != '\0') {
2254 chs_fail:
2255 fprintf(stderr, "qemu: invalid physical CHS format\n");
2256 exit(1);
2258 if (hda_opts != NULL) {
2259 char num[16];
2260 snprintf(num, sizeof(num), "%d", cyls);
2261 qemu_opt_set(hda_opts, "cyls", num);
2262 snprintf(num, sizeof(num), "%d", heads);
2263 qemu_opt_set(hda_opts, "heads", num);
2264 snprintf(num, sizeof(num), "%d", secs);
2265 qemu_opt_set(hda_opts, "secs", num);
2266 if (translation == BIOS_ATA_TRANSLATION_LBA)
2267 qemu_opt_set(hda_opts, "trans", "lba");
2268 if (translation == BIOS_ATA_TRANSLATION_NONE)
2269 qemu_opt_set(hda_opts, "trans", "none");
2272 break;
2273 case QEMU_OPTION_numa:
2274 if (nb_numa_nodes >= MAX_NODES) {
2275 fprintf(stderr, "qemu: too many NUMA nodes\n");
2276 exit(1);
2278 numa_add(optarg);
2279 break;
2280 case QEMU_OPTION_display:
2281 display_type = select_display(optarg);
2282 break;
2283 case QEMU_OPTION_nographic:
2284 display_type = DT_NOGRAPHIC;
2285 break;
2286 case QEMU_OPTION_curses:
2287 #ifdef CONFIG_CURSES
2288 display_type = DT_CURSES;
2289 #else
2290 fprintf(stderr, "Curses support is disabled\n");
2291 exit(1);
2292 #endif
2293 break;
2294 case QEMU_OPTION_portrait:
2295 graphic_rotate = 1;
2296 break;
2297 case QEMU_OPTION_kernel:
2298 kernel_filename = optarg;
2299 break;
2300 case QEMU_OPTION_append:
2301 kernel_cmdline = optarg;
2302 break;
2303 case QEMU_OPTION_cdrom:
2304 drive_add(IF_DEFAULT, 2, optarg, CDROM_OPTS);
2305 break;
2306 case QEMU_OPTION_boot:
2308 static const char * const params[] = {
2309 "order", "once", "menu", NULL
2311 char buf[sizeof(boot_devices)];
2312 char *standard_boot_devices;
2313 int legacy = 0;
2315 if (!strchr(optarg, '=')) {
2316 legacy = 1;
2317 pstrcpy(buf, sizeof(buf), optarg);
2318 } else if (check_params(buf, sizeof(buf), params, optarg) < 0) {
2319 fprintf(stderr,
2320 "qemu: unknown boot parameter '%s' in '%s'\n",
2321 buf, optarg);
2322 exit(1);
2325 if (legacy ||
2326 get_param_value(buf, sizeof(buf), "order", optarg)) {
2327 validate_bootdevices(buf);
2328 pstrcpy(boot_devices, sizeof(boot_devices), buf);
2330 if (!legacy) {
2331 if (get_param_value(buf, sizeof(buf),
2332 "once", optarg)) {
2333 validate_bootdevices(buf);
2334 standard_boot_devices = qemu_strdup(boot_devices);
2335 pstrcpy(boot_devices, sizeof(boot_devices), buf);
2336 qemu_register_reset(restore_boot_devices,
2337 standard_boot_devices);
2339 if (get_param_value(buf, sizeof(buf),
2340 "menu", optarg)) {
2341 if (!strcmp(buf, "on")) {
2342 boot_menu = 1;
2343 } else if (!strcmp(buf, "off")) {
2344 boot_menu = 0;
2345 } else {
2346 fprintf(stderr,
2347 "qemu: invalid option value '%s'\n",
2348 buf);
2349 exit(1);
2354 break;
2355 case QEMU_OPTION_fda:
2356 case QEMU_OPTION_fdb:
2357 drive_add(IF_FLOPPY, popt->index - QEMU_OPTION_fda,
2358 optarg, FD_OPTS);
2359 break;
2360 case QEMU_OPTION_no_fd_bootchk:
2361 fd_bootchk = 0;
2362 break;
2363 case QEMU_OPTION_netdev:
2364 if (net_client_parse(qemu_find_opts("netdev"), optarg) == -1) {
2365 exit(1);
2367 break;
2368 case QEMU_OPTION_net:
2369 if (net_client_parse(qemu_find_opts("net"), optarg) == -1) {
2370 exit(1);
2372 break;
2373 #ifdef CONFIG_SLIRP
2374 case QEMU_OPTION_tftp:
2375 legacy_tftp_prefix = optarg;
2376 break;
2377 case QEMU_OPTION_bootp:
2378 legacy_bootp_filename = optarg;
2379 break;
2380 case QEMU_OPTION_redir:
2381 if (net_slirp_redir(optarg) < 0)
2382 exit(1);
2383 break;
2384 #endif
2385 case QEMU_OPTION_bt:
2386 add_device_config(DEV_BT, optarg);
2387 break;
2388 case QEMU_OPTION_audio_help:
2389 if (!(audio_available())) {
2390 printf("Option %s not supported for this target\n", popt->name);
2391 exit(1);
2393 AUD_help ();
2394 exit (0);
2395 break;
2396 case QEMU_OPTION_soundhw:
2397 if (!(audio_available())) {
2398 printf("Option %s not supported for this target\n", popt->name);
2399 exit(1);
2401 select_soundhw (optarg);
2402 break;
2403 case QEMU_OPTION_h:
2404 help(0);
2405 break;
2406 case QEMU_OPTION_version:
2407 version();
2408 exit(0);
2409 break;
2410 case QEMU_OPTION_m: {
2411 int64_t value;
2413 value = strtosz(optarg, NULL);
2414 if (value < 0) {
2415 fprintf(stderr, "qemu: invalid ram size: %s\n", optarg);
2416 exit(1);
2419 /* On 32-bit hosts, QEMU is limited by virtual address space */
2420 if (value > (2047 << 20) && HOST_LONG_BITS == 32) {
2421 fprintf(stderr, "qemu: at most 2047 MB RAM can be simulated\n");
2422 exit(1);
2424 if (value != (uint64_t)(ram_addr_t)value) {
2425 fprintf(stderr, "qemu: ram size too large\n");
2426 exit(1);
2428 ram_size = value;
2429 break;
2431 case QEMU_OPTION_mempath:
2432 mem_path = optarg;
2433 break;
2434 #ifdef MAP_POPULATE
2435 case QEMU_OPTION_mem_prealloc:
2436 mem_prealloc = 1;
2437 break;
2438 #endif
2439 case QEMU_OPTION_d:
2440 set_cpu_log(optarg);
2441 break;
2442 case QEMU_OPTION_s:
2443 gdbstub_dev = "tcp::" DEFAULT_GDBSTUB_PORT;
2444 break;
2445 case QEMU_OPTION_gdb:
2446 gdbstub_dev = optarg;
2447 break;
2448 case QEMU_OPTION_L:
2449 data_dir = optarg;
2450 break;
2451 case QEMU_OPTION_bios:
2452 bios_name = optarg;
2453 break;
2454 case QEMU_OPTION_singlestep:
2455 singlestep = 1;
2456 break;
2457 case QEMU_OPTION_S:
2458 autostart = 0;
2459 break;
2460 case QEMU_OPTION_k:
2461 keyboard_layout = optarg;
2462 break;
2463 case QEMU_OPTION_localtime:
2464 rtc_utc = 0;
2465 break;
2466 case QEMU_OPTION_vga:
2467 select_vgahw (optarg);
2468 break;
2469 case QEMU_OPTION_g:
2471 const char *p;
2472 int w, h, depth;
2473 p = optarg;
2474 w = strtol(p, (char **)&p, 10);
2475 if (w <= 0) {
2476 graphic_error:
2477 fprintf(stderr, "qemu: invalid resolution or depth\n");
2478 exit(1);
2480 if (*p != 'x')
2481 goto graphic_error;
2482 p++;
2483 h = strtol(p, (char **)&p, 10);
2484 if (h <= 0)
2485 goto graphic_error;
2486 if (*p == 'x') {
2487 p++;
2488 depth = strtol(p, (char **)&p, 10);
2489 if (depth != 8 && depth != 15 && depth != 16 &&
2490 depth != 24 && depth != 32)
2491 goto graphic_error;
2492 } else if (*p == '\0') {
2493 depth = graphic_depth;
2494 } else {
2495 goto graphic_error;
2498 graphic_width = w;
2499 graphic_height = h;
2500 graphic_depth = depth;
2502 break;
2503 case QEMU_OPTION_echr:
2505 char *r;
2506 term_escape_char = strtol(optarg, &r, 0);
2507 if (r == optarg)
2508 printf("Bad argument to echr\n");
2509 break;
2511 case QEMU_OPTION_monitor:
2512 monitor_parse(optarg, "readline");
2513 default_monitor = 0;
2514 break;
2515 case QEMU_OPTION_qmp:
2516 monitor_parse(optarg, "control");
2517 default_monitor = 0;
2518 break;
2519 case QEMU_OPTION_mon:
2520 opts = qemu_opts_parse(qemu_find_opts("mon"), optarg, 1);
2521 if (!opts) {
2522 exit(1);
2524 default_monitor = 0;
2525 break;
2526 case QEMU_OPTION_chardev:
2527 opts = qemu_opts_parse(qemu_find_opts("chardev"), optarg, 1);
2528 if (!opts) {
2529 exit(1);
2531 break;
2532 case QEMU_OPTION_fsdev:
2533 olist = qemu_find_opts("fsdev");
2534 if (!olist) {
2535 fprintf(stderr, "fsdev is not supported by this qemu build.\n");
2536 exit(1);
2538 opts = qemu_opts_parse(olist, optarg, 1);
2539 if (!opts) {
2540 fprintf(stderr, "parse error: %s\n", optarg);
2541 exit(1);
2543 break;
2544 case QEMU_OPTION_virtfs: {
2545 QemuOpts *fsdev;
2546 QemuOpts *device;
2548 olist = qemu_find_opts("virtfs");
2549 if (!olist) {
2550 fprintf(stderr, "virtfs is not supported by this qemu build.\n");
2551 exit(1);
2553 opts = qemu_opts_parse(olist, optarg, 1);
2554 if (!opts) {
2555 fprintf(stderr, "parse error: %s\n", optarg);
2556 exit(1);
2559 if (qemu_opt_get(opts, "fstype") == NULL ||
2560 qemu_opt_get(opts, "mount_tag") == NULL ||
2561 qemu_opt_get(opts, "path") == NULL ||
2562 qemu_opt_get(opts, "security_model") == NULL) {
2563 fprintf(stderr, "Usage: -virtfs fstype,path=/share_path/,"
2564 "security_model=[mapped|passthrough|none],"
2565 "mount_tag=tag.\n");
2566 exit(1);
2569 fsdev = qemu_opts_create(qemu_find_opts("fsdev"),
2570 qemu_opt_get(opts, "mount_tag"), 1);
2571 if (!fsdev) {
2572 fprintf(stderr, "duplicate fsdev id: %s\n",
2573 qemu_opt_get(opts, "mount_tag"));
2574 exit(1);
2576 qemu_opt_set(fsdev, "fstype", qemu_opt_get(opts, "fstype"));
2577 qemu_opt_set(fsdev, "path", qemu_opt_get(opts, "path"));
2578 qemu_opt_set(fsdev, "security_model",
2579 qemu_opt_get(opts, "security_model"));
2581 device = qemu_opts_create(qemu_find_opts("device"), NULL, 0);
2582 qemu_opt_set(device, "driver", "virtio-9p-pci");
2583 qemu_opt_set(device, "fsdev",
2584 qemu_opt_get(opts, "mount_tag"));
2585 qemu_opt_set(device, "mount_tag",
2586 qemu_opt_get(opts, "mount_tag"));
2587 break;
2589 case QEMU_OPTION_serial:
2590 add_device_config(DEV_SERIAL, optarg);
2591 default_serial = 0;
2592 if (strncmp(optarg, "mon:", 4) == 0) {
2593 default_monitor = 0;
2595 break;
2596 case QEMU_OPTION_watchdog:
2597 if (watchdog) {
2598 fprintf(stderr,
2599 "qemu: only one watchdog option may be given\n");
2600 return 1;
2602 watchdog = optarg;
2603 break;
2604 case QEMU_OPTION_watchdog_action:
2605 if (select_watchdog_action(optarg) == -1) {
2606 fprintf(stderr, "Unknown -watchdog-action parameter\n");
2607 exit(1);
2609 break;
2610 case QEMU_OPTION_virtiocon:
2611 add_device_config(DEV_VIRTCON, optarg);
2612 default_virtcon = 0;
2613 if (strncmp(optarg, "mon:", 4) == 0) {
2614 default_monitor = 0;
2616 break;
2617 case QEMU_OPTION_parallel:
2618 add_device_config(DEV_PARALLEL, optarg);
2619 default_parallel = 0;
2620 if (strncmp(optarg, "mon:", 4) == 0) {
2621 default_monitor = 0;
2623 break;
2624 case QEMU_OPTION_debugcon:
2625 add_device_config(DEV_DEBUGCON, optarg);
2626 break;
2627 case QEMU_OPTION_loadvm:
2628 loadvm = optarg;
2629 break;
2630 case QEMU_OPTION_full_screen:
2631 full_screen = 1;
2632 break;
2633 #ifdef CONFIG_SDL
2634 case QEMU_OPTION_no_frame:
2635 no_frame = 1;
2636 break;
2637 case QEMU_OPTION_alt_grab:
2638 alt_grab = 1;
2639 break;
2640 case QEMU_OPTION_ctrl_grab:
2641 ctrl_grab = 1;
2642 break;
2643 case QEMU_OPTION_no_quit:
2644 no_quit = 1;
2645 break;
2646 case QEMU_OPTION_sdl:
2647 display_type = DT_SDL;
2648 break;
2649 #else
2650 case QEMU_OPTION_no_frame:
2651 case QEMU_OPTION_alt_grab:
2652 case QEMU_OPTION_ctrl_grab:
2653 case QEMU_OPTION_no_quit:
2654 case QEMU_OPTION_sdl:
2655 fprintf(stderr, "SDL support is disabled\n");
2656 exit(1);
2657 #endif
2658 case QEMU_OPTION_pidfile:
2659 pid_file = optarg;
2660 break;
2661 case QEMU_OPTION_win2k_hack:
2662 win2k_install_hack = 1;
2663 break;
2664 case QEMU_OPTION_rtc_td_hack:
2665 rtc_td_hack = 1;
2666 break;
2667 case QEMU_OPTION_acpitable:
2668 do_acpitable_option(optarg);
2669 break;
2670 case QEMU_OPTION_smbios:
2671 do_smbios_option(optarg);
2672 break;
2673 case QEMU_OPTION_enable_kvm:
2674 olist = qemu_find_opts("machine");
2675 qemu_opts_reset(olist);
2676 qemu_opts_parse(olist, "accel=kvm", 0);
2677 break;
2678 case QEMU_OPTION_machine:
2679 olist = qemu_find_opts("machine");
2680 qemu_opts_reset(olist);
2681 opts = qemu_opts_parse(olist, optarg, 0);
2682 if (!opts) {
2683 fprintf(stderr, "parse error: %s\n", optarg);
2684 exit(1);
2686 break;
2687 case QEMU_OPTION_usb:
2688 usb_enabled = 1;
2689 break;
2690 case QEMU_OPTION_usbdevice:
2691 usb_enabled = 1;
2692 add_device_config(DEV_USB, optarg);
2693 break;
2694 case QEMU_OPTION_device:
2695 if (!qemu_opts_parse(qemu_find_opts("device"), optarg, 1)) {
2696 exit(1);
2698 break;
2699 case QEMU_OPTION_smp:
2700 smp_parse(optarg);
2701 if (smp_cpus < 1) {
2702 fprintf(stderr, "Invalid number of CPUs\n");
2703 exit(1);
2705 if (max_cpus < smp_cpus) {
2706 fprintf(stderr, "maxcpus must be equal to or greater than "
2707 "smp\n");
2708 exit(1);
2710 if (max_cpus > 255) {
2711 fprintf(stderr, "Unsupported number of maxcpus\n");
2712 exit(1);
2714 break;
2715 case QEMU_OPTION_vnc:
2716 #ifdef CONFIG_VNC
2717 display_remote++;
2718 vnc_display = optarg;
2719 #else
2720 fprintf(stderr, "VNC support is disabled\n");
2721 exit(1);
2722 #endif
2723 break;
2724 case QEMU_OPTION_no_acpi:
2725 acpi_enabled = 0;
2726 break;
2727 case QEMU_OPTION_no_hpet:
2728 no_hpet = 1;
2729 break;
2730 case QEMU_OPTION_balloon:
2731 if (balloon_parse(optarg) < 0) {
2732 fprintf(stderr, "Unknown -balloon argument %s\n", optarg);
2733 exit(1);
2735 break;
2736 case QEMU_OPTION_no_reboot:
2737 no_reboot = 1;
2738 break;
2739 case QEMU_OPTION_no_shutdown:
2740 no_shutdown = 1;
2741 break;
2742 case QEMU_OPTION_show_cursor:
2743 cursor_hide = 0;
2744 break;
2745 case QEMU_OPTION_uuid:
2746 if(qemu_uuid_parse(optarg, qemu_uuid) < 0) {
2747 fprintf(stderr, "Fail to parse UUID string."
2748 " Wrong format.\n");
2749 exit(1);
2751 break;
2752 case QEMU_OPTION_option_rom:
2753 if (nb_option_roms >= MAX_OPTION_ROMS) {
2754 fprintf(stderr, "Too many option ROMs\n");
2755 exit(1);
2757 opts = qemu_opts_parse(qemu_find_opts("option-rom"), optarg, 1);
2758 option_rom[nb_option_roms].name = qemu_opt_get(opts, "romfile");
2759 option_rom[nb_option_roms].bootindex =
2760 qemu_opt_get_number(opts, "bootindex", -1);
2761 if (!option_rom[nb_option_roms].name) {
2762 fprintf(stderr, "Option ROM file is not specified\n");
2763 exit(1);
2765 nb_option_roms++;
2766 break;
2767 case QEMU_OPTION_semihosting:
2768 semihosting_enabled = 1;
2769 break;
2770 case QEMU_OPTION_name:
2771 qemu_name = qemu_strdup(optarg);
2773 char *p = strchr(qemu_name, ',');
2774 if (p != NULL) {
2775 *p++ = 0;
2776 if (strncmp(p, "process=", 8)) {
2777 fprintf(stderr, "Unknown subargument %s to -name\n", p);
2778 exit(1);
2780 p += 8;
2781 os_set_proc_name(p);
2784 break;
2785 case QEMU_OPTION_prom_env:
2786 if (nb_prom_envs >= MAX_PROM_ENVS) {
2787 fprintf(stderr, "Too many prom variables\n");
2788 exit(1);
2790 prom_envs[nb_prom_envs] = optarg;
2791 nb_prom_envs++;
2792 break;
2793 case QEMU_OPTION_old_param:
2794 old_param = 1;
2795 break;
2796 case QEMU_OPTION_clock:
2797 configure_alarms(optarg);
2798 break;
2799 case QEMU_OPTION_startdate:
2800 configure_rtc_date_offset(optarg, 1);
2801 break;
2802 case QEMU_OPTION_rtc:
2803 opts = qemu_opts_parse(qemu_find_opts("rtc"), optarg, 0);
2804 if (!opts) {
2805 exit(1);
2807 configure_rtc(opts);
2808 break;
2809 case QEMU_OPTION_tb_size:
2810 tb_size = strtol(optarg, NULL, 0);
2811 if (tb_size < 0)
2812 tb_size = 0;
2813 break;
2814 case QEMU_OPTION_icount:
2815 icount_option = optarg;
2816 break;
2817 case QEMU_OPTION_incoming:
2818 incoming = optarg;
2819 incoming_expected = true;
2820 break;
2821 case QEMU_OPTION_nodefaults:
2822 default_serial = 0;
2823 default_parallel = 0;
2824 default_virtcon = 0;
2825 default_monitor = 0;
2826 default_vga = 0;
2827 default_net = 0;
2828 default_floppy = 0;
2829 default_cdrom = 0;
2830 default_sdcard = 0;
2831 break;
2832 case QEMU_OPTION_xen_domid:
2833 if (!(xen_available())) {
2834 printf("Option %s not supported for this target\n", popt->name);
2835 exit(1);
2837 xen_domid = atoi(optarg);
2838 break;
2839 case QEMU_OPTION_xen_create:
2840 if (!(xen_available())) {
2841 printf("Option %s not supported for this target\n", popt->name);
2842 exit(1);
2844 xen_mode = XEN_CREATE;
2845 break;
2846 case QEMU_OPTION_xen_attach:
2847 if (!(xen_available())) {
2848 printf("Option %s not supported for this target\n", popt->name);
2849 exit(1);
2851 xen_mode = XEN_ATTACH;
2852 break;
2853 #ifdef CONFIG_SIMPLE_TRACE
2854 case QEMU_OPTION_trace:
2855 opts = qemu_opts_parse(qemu_find_opts("trace"), optarg, 0);
2856 if (opts) {
2857 trace_file = qemu_opt_get(opts, "file");
2859 break;
2860 #endif
2861 case QEMU_OPTION_readconfig:
2863 int ret = qemu_read_config_file(optarg);
2864 if (ret < 0) {
2865 fprintf(stderr, "read config %s: %s\n", optarg,
2866 strerror(-ret));
2867 exit(1);
2869 break;
2871 case QEMU_OPTION_spice:
2872 olist = qemu_find_opts("spice");
2873 if (!olist) {
2874 fprintf(stderr, "spice is not supported by this qemu build.\n");
2875 exit(1);
2877 opts = qemu_opts_parse(olist, optarg, 0);
2878 if (!opts) {
2879 fprintf(stderr, "parse error: %s\n", optarg);
2880 exit(1);
2882 break;
2883 case QEMU_OPTION_writeconfig:
2885 FILE *fp;
2886 if (strcmp(optarg, "-") == 0) {
2887 fp = stdout;
2888 } else {
2889 fp = fopen(optarg, "w");
2890 if (fp == NULL) {
2891 fprintf(stderr, "open %s: %s\n", optarg, strerror(errno));
2892 exit(1);
2895 qemu_config_write(fp);
2896 fclose(fp);
2897 break;
2899 default:
2900 os_parse_cmd_args(popt->index, optarg);
2904 loc_set_none();
2906 if (!st_init(trace_file)) {
2907 fprintf(stderr, "warning: unable to initialize simple trace backend\n");
2910 /* If no data_dir is specified then try to find it relative to the
2911 executable path. */
2912 if (!data_dir) {
2913 data_dir = os_find_datadir(argv[0]);
2915 /* If all else fails use the install patch specified when building. */
2916 if (!data_dir) {
2917 data_dir = CONFIG_QEMU_DATADIR;
2921 * Default to max_cpus = smp_cpus, in case the user doesn't
2922 * specify a max_cpus value.
2924 if (!max_cpus)
2925 max_cpus = smp_cpus;
2927 machine->max_cpus = machine->max_cpus ?: 1; /* Default to UP */
2928 if (smp_cpus > machine->max_cpus) {
2929 fprintf(stderr, "Number of SMP cpus requested (%d), exceeds max cpus "
2930 "supported by machine `%s' (%d)\n", smp_cpus, machine->name,
2931 machine->max_cpus);
2932 exit(1);
2936 * Get the default machine options from the machine if it is not already
2937 * specified either by the configuration file or by the command line.
2939 if (machine->default_machine_opts) {
2940 QemuOptsList *list = qemu_find_opts("machine");
2941 const char *p = NULL;
2943 if (!QTAILQ_EMPTY(&list->head)) {
2944 p = qemu_opt_get(QTAILQ_FIRST(&list->head), "accel");
2946 if (p == NULL) {
2947 opts = qemu_opts_parse(qemu_find_opts("machine"),
2948 machine->default_machine_opts, 0);
2949 if (!opts) {
2950 fprintf(stderr, "parse error for machine %s: %s\n",
2951 machine->name, machine->default_machine_opts);
2952 exit(1);
2957 qemu_opts_foreach(qemu_find_opts("device"), default_driver_check, NULL, 0);
2958 qemu_opts_foreach(qemu_find_opts("global"), default_driver_check, NULL, 0);
2960 if (machine->no_serial) {
2961 default_serial = 0;
2963 if (machine->no_parallel) {
2964 default_parallel = 0;
2966 if (!machine->use_virtcon) {
2967 default_virtcon = 0;
2969 if (machine->no_vga) {
2970 default_vga = 0;
2972 if (machine->no_floppy) {
2973 default_floppy = 0;
2975 if (machine->no_cdrom) {
2976 default_cdrom = 0;
2978 if (machine->no_sdcard) {
2979 default_sdcard = 0;
2982 if (display_type == DT_NOGRAPHIC) {
2983 if (default_parallel)
2984 add_device_config(DEV_PARALLEL, "null");
2985 if (default_serial && default_monitor) {
2986 add_device_config(DEV_SERIAL, "mon:stdio");
2987 } else if (default_virtcon && default_monitor) {
2988 add_device_config(DEV_VIRTCON, "mon:stdio");
2989 } else {
2990 if (default_serial)
2991 add_device_config(DEV_SERIAL, "stdio");
2992 if (default_virtcon)
2993 add_device_config(DEV_VIRTCON, "stdio");
2994 if (default_monitor)
2995 monitor_parse("stdio", "readline");
2997 } else {
2998 if (default_serial)
2999 add_device_config(DEV_SERIAL, "vc:80Cx24C");
3000 if (default_parallel)
3001 add_device_config(DEV_PARALLEL, "vc:80Cx24C");
3002 if (default_monitor)
3003 monitor_parse("vc:80Cx24C", "readline");
3004 if (default_virtcon)
3005 add_device_config(DEV_VIRTCON, "vc:80Cx24C");
3007 if (default_vga)
3008 vga_interface_type = VGA_CIRRUS;
3010 socket_init();
3012 if (qemu_opts_foreach(qemu_find_opts("chardev"), chardev_init_func, NULL, 1) != 0)
3013 exit(1);
3014 #ifdef CONFIG_VIRTFS
3015 if (qemu_opts_foreach(qemu_find_opts("fsdev"), fsdev_init_func, NULL, 1) != 0) {
3016 exit(1);
3018 #endif
3020 os_daemonize();
3022 if (pid_file && qemu_create_pidfile(pid_file) != 0) {
3023 os_pidfile_error();
3024 exit(1);
3027 configure_accelerator();
3029 if (qemu_init_main_loop()) {
3030 fprintf(stderr, "qemu_init_main_loop failed\n");
3031 exit(1);
3033 linux_boot = (kernel_filename != NULL);
3035 if (!linux_boot && *kernel_cmdline != '\0') {
3036 fprintf(stderr, "-append only allowed with -kernel option\n");
3037 exit(1);
3040 if (!linux_boot && initrd_filename != NULL) {
3041 fprintf(stderr, "-initrd only allowed with -kernel option\n");
3042 exit(1);
3045 os_set_line_buffering();
3047 if (init_timer_alarm() < 0) {
3048 fprintf(stderr, "could not initialize alarm timer\n");
3049 exit(1);
3051 configure_icount(icount_option);
3053 if (net_init_clients() < 0) {
3054 exit(1);
3057 /* init the bluetooth world */
3058 if (foreach_device_config(DEV_BT, bt_parse))
3059 exit(1);
3061 /* init the memory */
3062 if (ram_size == 0)
3063 ram_size = DEFAULT_RAM_SIZE * 1024 * 1024;
3065 /* init the dynamic translator */
3066 cpu_exec_init_all(tb_size * 1024 * 1024);
3068 bdrv_init_with_whitelist();
3070 blk_mig_init();
3072 /* open the virtual block devices */
3073 if (snapshot)
3074 qemu_opts_foreach(qemu_find_opts("drive"), drive_enable_snapshot, NULL, 0);
3075 if (qemu_opts_foreach(qemu_find_opts("drive"), drive_init_func, &machine->use_scsi, 1) != 0)
3076 exit(1);
3078 default_drive(default_cdrom, snapshot, machine->use_scsi,
3079 IF_DEFAULT, 2, CDROM_OPTS);
3080 default_drive(default_floppy, snapshot, machine->use_scsi,
3081 IF_FLOPPY, 0, FD_OPTS);
3082 default_drive(default_sdcard, snapshot, machine->use_scsi,
3083 IF_SD, 0, SD_OPTS);
3085 register_savevm_live(NULL, "ram", 0, 4, NULL, ram_save_live, NULL,
3086 ram_load, NULL);
3088 if (nb_numa_nodes > 0) {
3089 int i;
3091 if (nb_numa_nodes > smp_cpus) {
3092 nb_numa_nodes = smp_cpus;
3095 /* If no memory size if given for any node, assume the default case
3096 * and distribute the available memory equally across all nodes
3098 for (i = 0; i < nb_numa_nodes; i++) {
3099 if (node_mem[i] != 0)
3100 break;
3102 if (i == nb_numa_nodes) {
3103 uint64_t usedmem = 0;
3105 /* On Linux, the each node's border has to be 8MB aligned,
3106 * the final node gets the rest.
3108 for (i = 0; i < nb_numa_nodes - 1; i++) {
3109 node_mem[i] = (ram_size / nb_numa_nodes) & ~((1 << 23UL) - 1);
3110 usedmem += node_mem[i];
3112 node_mem[i] = ram_size - usedmem;
3115 for (i = 0; i < nb_numa_nodes; i++) {
3116 if (node_cpumask[i] != 0)
3117 break;
3119 /* assigning the VCPUs round-robin is easier to implement, guest OSes
3120 * must cope with this anyway, because there are BIOSes out there in
3121 * real machines which also use this scheme.
3123 if (i == nb_numa_nodes) {
3124 for (i = 0; i < smp_cpus; i++) {
3125 node_cpumask[i % nb_numa_nodes] |= 1 << i;
3130 if (qemu_opts_foreach(qemu_find_opts("mon"), mon_init_func, NULL, 1) != 0) {
3131 exit(1);
3134 if (foreach_device_config(DEV_SERIAL, serial_parse) < 0)
3135 exit(1);
3136 if (foreach_device_config(DEV_PARALLEL, parallel_parse) < 0)
3137 exit(1);
3138 if (foreach_device_config(DEV_VIRTCON, virtcon_parse) < 0)
3139 exit(1);
3140 if (foreach_device_config(DEV_DEBUGCON, debugcon_parse) < 0)
3141 exit(1);
3143 module_call_init(MODULE_INIT_DEVICE);
3145 if (qemu_opts_foreach(qemu_find_opts("device"), device_help_func, NULL, 0) != 0)
3146 exit(0);
3148 if (watchdog) {
3149 i = select_watchdog(watchdog);
3150 if (i > 0)
3151 exit (i == 1 ? 1 : 0);
3154 if (machine->compat_props) {
3155 qdev_prop_register_global_list(machine->compat_props);
3157 qemu_add_globals();
3159 machine->init(ram_size, boot_devices,
3160 kernel_filename, kernel_cmdline, initrd_filename, cpu_model);
3162 cpu_synchronize_all_post_init();
3164 set_numa_modes();
3166 current_machine = machine;
3168 /* init USB devices */
3169 if (usb_enabled) {
3170 if (foreach_device_config(DEV_USB, usb_parse) < 0)
3171 exit(1);
3174 /* init generic devices */
3175 if (qemu_opts_foreach(qemu_find_opts("device"), device_init_func, NULL, 1) != 0)
3176 exit(1);
3178 net_check_clients();
3180 /* just use the first displaystate for the moment */
3181 ds = get_displaystate();
3183 if (using_spice)
3184 display_remote++;
3185 if (display_type == DT_DEFAULT && !display_remote) {
3186 #if defined(CONFIG_SDL) || defined(CONFIG_COCOA)
3187 display_type = DT_SDL;
3188 #elif defined(CONFIG_VNC)
3189 vnc_display = "localhost:0,to=99";
3190 show_vnc_port = 1;
3191 #else
3192 display_type = DT_NONE;
3193 #endif
3197 /* init local displays */
3198 switch (display_type) {
3199 case DT_NOGRAPHIC:
3200 break;
3201 #if defined(CONFIG_CURSES)
3202 case DT_CURSES:
3203 curses_display_init(ds, full_screen);
3204 break;
3205 #endif
3206 #if defined(CONFIG_SDL)
3207 case DT_SDL:
3208 sdl_display_init(ds, full_screen, no_frame);
3209 break;
3210 #elif defined(CONFIG_COCOA)
3211 case DT_SDL:
3212 cocoa_display_init(ds, full_screen);
3213 break;
3214 #endif
3215 default:
3216 break;
3219 /* must be after terminal init, SDL library changes signal handlers */
3220 os_setup_signal_handling();
3222 #ifdef CONFIG_VNC
3223 /* init remote displays */
3224 if (vnc_display) {
3225 vnc_display_init(ds);
3226 if (vnc_display_open(ds, vnc_display) < 0)
3227 exit(1);
3229 if (show_vnc_port) {
3230 printf("VNC server running on `%s'\n", vnc_display_local_addr(ds));
3233 #endif
3234 #ifdef CONFIG_SPICE
3235 if (using_spice && !qxl_enabled) {
3236 qemu_spice_display_init(ds);
3238 #endif
3240 /* display setup */
3241 dpy_resize(ds);
3242 dcl = ds->listeners;
3243 while (dcl != NULL) {
3244 if (dcl->dpy_refresh != NULL) {
3245 ds->gui_timer = qemu_new_timer_ms(rt_clock, gui_update, ds);
3246 qemu_mod_timer(ds->gui_timer, qemu_get_clock_ms(rt_clock));
3247 break;
3249 dcl = dcl->next;
3251 if (ds->gui_timer == NULL) {
3252 nographic_timer = qemu_new_timer_ms(rt_clock, nographic_update, NULL);
3253 qemu_mod_timer(nographic_timer, qemu_get_clock_ms(rt_clock));
3255 text_consoles_set_display(ds);
3257 if (gdbstub_dev && gdbserver_start(gdbstub_dev) < 0) {
3258 fprintf(stderr, "qemu: could not open gdbserver on device '%s'\n",
3259 gdbstub_dev);
3260 exit(1);
3263 qdev_machine_creation_done();
3265 if (rom_load_all() != 0) {
3266 fprintf(stderr, "rom loading failed\n");
3267 exit(1);
3270 /* TODO: once all bus devices are qdevified, this should be done
3271 * when bus is created by qdev.c */
3272 qemu_register_reset(qbus_reset_all_fn, sysbus_get_default());
3273 qemu_run_machine_init_done_notifiers();
3275 qemu_system_reset();
3276 if (loadvm) {
3277 if (load_vmstate(loadvm) < 0) {
3278 autostart = 0;
3282 if (incoming) {
3283 int ret = qemu_start_incoming_migration(incoming);
3284 if (ret < 0) {
3285 fprintf(stderr, "Migration failed. Exit code %s(%d), exiting.\n",
3286 incoming, ret);
3287 exit(ret);
3289 } else if (autostart) {
3290 vm_start();
3293 os_setup_post();
3295 main_loop();
3296 quit_timers();
3297 net_cleanup();
3299 return 0;