Merge commit '27bb0b2d6f80f058bdb6fcc8fcdfa69b0c8a6d71' into upstream-merge
[qemu-kvm/amd-iommu.git] / vl.c
blobe19235248a31008e8ff0a5d108a06d498be7412a
1 /*
2 * QEMU System Emulator
4 * Copyright (c) 2003-2008 Fabrice Bellard
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 * THE SOFTWARE.
24 #include <unistd.h>
25 #include <fcntl.h>
26 #include <signal.h>
27 #include <time.h>
28 #include <errno.h>
29 #include <sys/time.h>
30 #include <zlib.h>
32 /* Needed early for CONFIG_BSD etc. */
33 #include "config-host.h"
35 #ifndef _WIN32
36 #include <libgen.h>
37 #include <sys/times.h>
38 #include <sys/wait.h>
39 #include <termios.h>
40 #include <sys/mman.h>
41 #include <sys/ioctl.h>
42 #include <sys/resource.h>
43 #include <sys/socket.h>
44 #include <netinet/in.h>
45 #include <net/if.h>
46 #include <arpa/inet.h>
47 #include <dirent.h>
48 #include <netdb.h>
49 #include <sys/select.h>
50 #ifdef CONFIG_BSD
51 #include <sys/stat.h>
52 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__DragonFly__)
53 #include <libutil.h>
54 #include <sys/sysctl.h>
55 #else
56 #include <util.h>
57 #endif
58 #else
59 #ifdef __linux__
60 #include <pty.h>
61 #include <malloc.h>
63 #include <linux/ppdev.h>
64 #include <linux/parport.h>
65 #endif
66 #ifdef __sun__
67 #include <sys/stat.h>
68 #include <sys/ethernet.h>
69 #include <sys/sockio.h>
70 #include <netinet/arp.h>
71 #include <netinet/in_systm.h>
72 #include <netinet/ip.h>
73 #include <netinet/ip_icmp.h> // must come after ip.h
74 #include <netinet/udp.h>
75 #include <netinet/tcp.h>
76 #include <net/if.h>
77 #include <syslog.h>
78 #include <stropts.h>
79 /* See MySQL bug #7156 (http://bugs.mysql.com/bug.php?id=7156) for
80 discussion about Solaris header problems */
81 extern int madvise(caddr_t, size_t, int);
82 #endif
83 #endif
84 #endif
86 #if defined(__OpenBSD__)
87 #include <util.h>
88 #endif
90 #if defined(CONFIG_VDE)
91 #include <libvdeplug.h>
92 #endif
94 #ifdef _WIN32
95 #include <windows.h>
96 #endif
98 #ifdef CONFIG_SDL
99 #if defined(__APPLE__) || defined(main)
100 #include <SDL.h>
101 int qemu_main(int argc, char **argv, char **envp);
102 int main(int argc, char **argv)
104 return qemu_main(argc, argv, NULL);
106 #undef main
107 #define main qemu_main
108 #endif
109 #endif /* CONFIG_SDL */
111 #ifdef CONFIG_COCOA
112 #undef main
113 #define main qemu_main
114 #endif /* CONFIG_COCOA */
116 #include "hw/hw.h"
117 #include "hw/boards.h"
118 #include "hw/usb.h"
119 #include "hw/pcmcia.h"
120 #include "hw/pc.h"
121 #include "hw/isa.h"
122 #include "hw/baum.h"
123 #include "hw/bt.h"
124 #include "hw/watchdog.h"
125 #include "hw/smbios.h"
126 #include "hw/xen.h"
127 #include "hw/qdev.h"
128 #include "hw/loader.h"
129 #include "bt-host.h"
130 #include "net.h"
131 #include "net/slirp.h"
132 #include "monitor.h"
133 #include "console.h"
134 #include "sysemu.h"
135 #include "gdbstub.h"
136 #include "qemu-timer.h"
137 #include "qemu-char.h"
138 #include "cache-utils.h"
139 #include "block.h"
140 #include "block_int.h"
141 #include "block-migration.h"
142 #include "dma.h"
143 #include "audio/audio.h"
144 #include "migration.h"
145 #include "kvm.h"
146 #include "qemu-option.h"
147 #include "qemu-config.h"
148 #include "qemu-objects.h"
149 #include "qemu-options.h"
150 #include "hw/device-assignment.h"
151 #ifdef CONFIG_LINUX
152 #include "fsdev/qemu-fsdev.h"
153 #endif
155 #include "disas.h"
157 #include "qemu_socket.h"
159 #include "slirp/libslirp.h"
161 #include "qemu-queue.h"
162 #include "cpus.h"
163 #include "arch_init.h"
165 //#define DEBUG_NET
166 //#define DEBUG_SLIRP
168 #define DEFAULT_RAM_SIZE 128
170 #define MAX_VIRTIO_CONSOLES 1
172 static const char *data_dir;
173 const char *bios_name = NULL;
174 /* Note: drives_table[MAX_DRIVES] is a dummy block driver if none available
175 to store the VM snapshots */
176 struct drivelist drives = QTAILQ_HEAD_INITIALIZER(drives);
177 struct driveoptlist driveopts = QTAILQ_HEAD_INITIALIZER(driveopts);
178 DriveInfo *extboot_drive = NULL;
179 enum vga_retrace_method vga_retrace_method = VGA_RETRACE_DUMB;
180 DisplayType display_type = DT_DEFAULT;
181 const char* keyboard_layout = NULL;
182 ram_addr_t ram_size;
183 const char *mem_path = NULL;
184 #ifdef MAP_POPULATE
185 int mem_prealloc = 0; /* force preallocation of physical target memory */
186 #endif
187 int nb_nics;
188 NICInfo nd_table[MAX_NICS];
189 int vm_running;
190 int autostart;
191 static int rtc_utc = 1;
192 static int rtc_date_offset = -1; /* -1 means no change */
193 QEMUClock *rtc_clock;
194 int vga_interface_type = VGA_NONE;
195 static int full_screen = 0;
196 #ifdef CONFIG_SDL
197 static int no_frame = 0;
198 #endif
199 int no_quit = 0;
200 CharDriverState *serial_hds[MAX_SERIAL_PORTS];
201 CharDriverState *parallel_hds[MAX_PARALLEL_PORTS];
202 CharDriverState *virtcon_hds[MAX_VIRTIO_CONSOLES];
203 int win2k_install_hack = 0;
204 int rtc_td_hack = 0;
205 int usb_enabled = 0;
206 int singlestep = 0;
207 const char *assigned_devices[MAX_DEV_ASSIGN_CMDLINE];
208 int assigned_devices_index;
209 int smp_cpus = 1;
210 int max_cpus = 0;
211 int smp_cores = 1;
212 int smp_threads = 1;
213 const char *vnc_display;
214 int acpi_enabled = 1;
215 #ifdef TARGET_I386
216 int no_hpet = 0;
217 #endif
218 int fd_bootchk = 1;
219 int no_reboot = 0;
220 int no_shutdown = 0;
221 int cursor_hide = 1;
222 int graphic_rotate = 0;
223 uint8_t irq0override = 1;
224 const char *watchdog;
225 const char *option_rom[MAX_OPTION_ROMS];
226 int nb_option_roms;
227 int semihosting_enabled = 0;
228 int time_drift_fix = 0;
229 unsigned int kvm_shadow_memory = 0;
230 int old_param = 0;
231 const char *qemu_name;
232 int alt_grab = 0;
233 int ctrl_grab = 0;
234 unsigned int nb_prom_envs = 0;
235 const char *prom_envs[MAX_PROM_ENVS];
236 const char *nvram = NULL;
237 int boot_menu;
239 int nb_numa_nodes;
240 uint64_t node_mem[MAX_NODES];
241 uint64_t node_cpumask[MAX_NODES];
243 static QEMUTimer *nographic_timer;
245 uint8_t qemu_uuid[16];
247 static QEMUBootSetHandler *boot_set_handler;
248 static void *boot_set_opaque;
250 int kvm_allowed = 1;
251 uint32_t xen_domid;
252 enum xen_mode xen_mode = XEN_EMULATE;
254 static int default_serial = 1;
255 static int default_parallel = 1;
256 static int default_virtcon = 1;
257 static int default_monitor = 1;
258 static int default_vga = 1;
259 static int default_floppy = 1;
260 static int default_cdrom = 1;
261 static int default_sdcard = 1;
263 static struct {
264 const char *driver;
265 int *flag;
266 } default_list[] = {
267 { .driver = "isa-serial", .flag = &default_serial },
268 { .driver = "isa-parallel", .flag = &default_parallel },
269 { .driver = "isa-fdc", .flag = &default_floppy },
270 { .driver = "ide-drive", .flag = &default_cdrom },
271 { .driver = "virtio-serial-pci", .flag = &default_virtcon },
272 { .driver = "virtio-serial-s390", .flag = &default_virtcon },
273 { .driver = "virtio-serial", .flag = &default_virtcon },
274 { .driver = "VGA", .flag = &default_vga },
275 { .driver = "cirrus-vga", .flag = &default_vga },
276 { .driver = "vmware-svga", .flag = &default_vga },
279 static int default_driver_check(QemuOpts *opts, void *opaque)
281 const char *driver = qemu_opt_get(opts, "driver");
282 int i;
284 if (!driver)
285 return 0;
286 for (i = 0; i < ARRAY_SIZE(default_list); i++) {
287 if (strcmp(default_list[i].driver, driver) != 0)
288 continue;
289 *(default_list[i].flag) = 0;
291 return 0;
294 /***********************************************************/
295 /* real time host monotonic timer */
297 /* compute with 96 bit intermediate result: (a*b)/c */
298 uint64_t muldiv64(uint64_t a, uint32_t b, uint32_t c)
300 union {
301 uint64_t ll;
302 struct {
303 #ifdef HOST_WORDS_BIGENDIAN
304 uint32_t high, low;
305 #else
306 uint32_t low, high;
307 #endif
308 } l;
309 } u, res;
310 uint64_t rl, rh;
312 u.ll = a;
313 rl = (uint64_t)u.l.low * (uint64_t)b;
314 rh = (uint64_t)u.l.high * (uint64_t)b;
315 rh += (rl >> 32);
316 res.l.high = rh / c;
317 res.l.low = (((rh % c) << 32) + (rl & 0xffffffff)) / c;
318 return res.ll;
321 /***********************************************************/
322 /* host time/date access */
323 void qemu_get_timedate(struct tm *tm, int offset)
325 time_t ti;
326 struct tm *ret;
328 time(&ti);
329 ti += offset;
330 if (rtc_date_offset == -1) {
331 if (rtc_utc)
332 ret = gmtime(&ti);
333 else
334 ret = localtime(&ti);
335 } else {
336 ti -= rtc_date_offset;
337 ret = gmtime(&ti);
340 memcpy(tm, ret, sizeof(struct tm));
343 int qemu_timedate_diff(struct tm *tm)
345 time_t seconds;
347 if (rtc_date_offset == -1)
348 if (rtc_utc)
349 seconds = mktimegm(tm);
350 else
351 seconds = mktime(tm);
352 else
353 seconds = mktimegm(tm) + rtc_date_offset;
355 return seconds - time(NULL);
358 void rtc_change_mon_event(struct tm *tm)
360 QObject *data;
362 data = qobject_from_jsonf("{ 'offset': %d }", qemu_timedate_diff(tm));
363 monitor_protocol_event(QEVENT_RTC_CHANGE, data);
364 qobject_decref(data);
367 static void configure_rtc_date_offset(const char *startdate, int legacy)
369 time_t rtc_start_date;
370 struct tm tm;
372 if (!strcmp(startdate, "now") && legacy) {
373 rtc_date_offset = -1;
374 } else {
375 if (sscanf(startdate, "%d-%d-%dT%d:%d:%d",
376 &tm.tm_year,
377 &tm.tm_mon,
378 &tm.tm_mday,
379 &tm.tm_hour,
380 &tm.tm_min,
381 &tm.tm_sec) == 6) {
382 /* OK */
383 } else if (sscanf(startdate, "%d-%d-%d",
384 &tm.tm_year,
385 &tm.tm_mon,
386 &tm.tm_mday) == 3) {
387 tm.tm_hour = 0;
388 tm.tm_min = 0;
389 tm.tm_sec = 0;
390 } else {
391 goto date_fail;
393 tm.tm_year -= 1900;
394 tm.tm_mon--;
395 rtc_start_date = mktimegm(&tm);
396 if (rtc_start_date == -1) {
397 date_fail:
398 fprintf(stderr, "Invalid date format. Valid formats are:\n"
399 "'2006-06-17T16:01:21' or '2006-06-17'\n");
400 exit(1);
402 rtc_date_offset = time(NULL) - rtc_start_date;
406 static void configure_rtc(QemuOpts *opts)
408 const char *value;
410 value = qemu_opt_get(opts, "base");
411 if (value) {
412 if (!strcmp(value, "utc")) {
413 rtc_utc = 1;
414 } else if (!strcmp(value, "localtime")) {
415 rtc_utc = 0;
416 } else {
417 configure_rtc_date_offset(value, 0);
420 value = qemu_opt_get(opts, "clock");
421 if (value) {
422 if (!strcmp(value, "host")) {
423 rtc_clock = host_clock;
424 } else if (!strcmp(value, "vm")) {
425 rtc_clock = vm_clock;
426 } else {
427 fprintf(stderr, "qemu: invalid option value '%s'\n", value);
428 exit(1);
431 value = qemu_opt_get(opts, "driftfix");
432 if (value) {
433 if (!strcmp(value, "slew")) {
434 rtc_td_hack = 1;
435 } else if (!strcmp(value, "none")) {
436 rtc_td_hack = 0;
437 } else {
438 fprintf(stderr, "qemu: invalid option value '%s'\n", value);
439 exit(1);
444 /***********************************************************/
445 /* Bluetooth support */
446 static int nb_hcis;
447 static int cur_hci;
448 static struct HCIInfo *hci_table[MAX_NICS];
450 static struct bt_vlan_s {
451 struct bt_scatternet_s net;
452 int id;
453 struct bt_vlan_s *next;
454 } *first_bt_vlan;
456 /* find or alloc a new bluetooth "VLAN" */
457 static struct bt_scatternet_s *qemu_find_bt_vlan(int id)
459 struct bt_vlan_s **pvlan, *vlan;
460 for (vlan = first_bt_vlan; vlan != NULL; vlan = vlan->next) {
461 if (vlan->id == id)
462 return &vlan->net;
464 vlan = qemu_mallocz(sizeof(struct bt_vlan_s));
465 vlan->id = id;
466 pvlan = &first_bt_vlan;
467 while (*pvlan != NULL)
468 pvlan = &(*pvlan)->next;
469 *pvlan = vlan;
470 return &vlan->net;
473 static void null_hci_send(struct HCIInfo *hci, const uint8_t *data, int len)
477 static int null_hci_addr_set(struct HCIInfo *hci, const uint8_t *bd_addr)
479 return -ENOTSUP;
482 static struct HCIInfo null_hci = {
483 .cmd_send = null_hci_send,
484 .sco_send = null_hci_send,
485 .acl_send = null_hci_send,
486 .bdaddr_set = null_hci_addr_set,
489 struct HCIInfo *qemu_next_hci(void)
491 if (cur_hci == nb_hcis)
492 return &null_hci;
494 return hci_table[cur_hci++];
497 static struct HCIInfo *hci_init(const char *str)
499 char *endp;
500 struct bt_scatternet_s *vlan = 0;
502 if (!strcmp(str, "null"))
503 /* null */
504 return &null_hci;
505 else if (!strncmp(str, "host", 4) && (str[4] == '\0' || str[4] == ':'))
506 /* host[:hciN] */
507 return bt_host_hci(str[4] ? str + 5 : "hci0");
508 else if (!strncmp(str, "hci", 3)) {
509 /* hci[,vlan=n] */
510 if (str[3]) {
511 if (!strncmp(str + 3, ",vlan=", 6)) {
512 vlan = qemu_find_bt_vlan(strtol(str + 9, &endp, 0));
513 if (*endp)
514 vlan = 0;
516 } else
517 vlan = qemu_find_bt_vlan(0);
518 if (vlan)
519 return bt_new_hci(vlan);
522 fprintf(stderr, "qemu: Unknown bluetooth HCI `%s'.\n", str);
524 return 0;
527 static int bt_hci_parse(const char *str)
529 struct HCIInfo *hci;
530 bdaddr_t bdaddr;
532 if (nb_hcis >= MAX_NICS) {
533 fprintf(stderr, "qemu: Too many bluetooth HCIs (max %i).\n", MAX_NICS);
534 return -1;
537 hci = hci_init(str);
538 if (!hci)
539 return -1;
541 bdaddr.b[0] = 0x52;
542 bdaddr.b[1] = 0x54;
543 bdaddr.b[2] = 0x00;
544 bdaddr.b[3] = 0x12;
545 bdaddr.b[4] = 0x34;
546 bdaddr.b[5] = 0x56 + nb_hcis;
547 hci->bdaddr_set(hci, bdaddr.b);
549 hci_table[nb_hcis++] = hci;
551 return 0;
554 static void bt_vhci_add(int vlan_id)
556 struct bt_scatternet_s *vlan = qemu_find_bt_vlan(vlan_id);
558 if (!vlan->slave)
559 fprintf(stderr, "qemu: warning: adding a VHCI to "
560 "an empty scatternet %i\n", vlan_id);
562 bt_vhci_init(bt_new_hci(vlan));
565 static struct bt_device_s *bt_device_add(const char *opt)
567 struct bt_scatternet_s *vlan;
568 int vlan_id = 0;
569 char *endp = strstr(opt, ",vlan=");
570 int len = (endp ? endp - opt : strlen(opt)) + 1;
571 char devname[10];
573 pstrcpy(devname, MIN(sizeof(devname), len), opt);
575 if (endp) {
576 vlan_id = strtol(endp + 6, &endp, 0);
577 if (*endp) {
578 fprintf(stderr, "qemu: unrecognised bluetooth vlan Id\n");
579 return 0;
583 vlan = qemu_find_bt_vlan(vlan_id);
585 if (!vlan->slave)
586 fprintf(stderr, "qemu: warning: adding a slave device to "
587 "an empty scatternet %i\n", vlan_id);
589 if (!strcmp(devname, "keyboard"))
590 return bt_keyboard_init(vlan);
592 fprintf(stderr, "qemu: unsupported bluetooth device `%s'\n", devname);
593 return 0;
596 static int bt_parse(const char *opt)
598 const char *endp, *p;
599 int vlan;
601 if (strstart(opt, "hci", &endp)) {
602 if (!*endp || *endp == ',') {
603 if (*endp)
604 if (!strstart(endp, ",vlan=", 0))
605 opt = endp + 1;
607 return bt_hci_parse(opt);
609 } else if (strstart(opt, "vhci", &endp)) {
610 if (!*endp || *endp == ',') {
611 if (*endp) {
612 if (strstart(endp, ",vlan=", &p)) {
613 vlan = strtol(p, (char **) &endp, 0);
614 if (*endp) {
615 fprintf(stderr, "qemu: bad scatternet '%s'\n", p);
616 return 1;
618 } else {
619 fprintf(stderr, "qemu: bad parameter '%s'\n", endp + 1);
620 return 1;
622 } else
623 vlan = 0;
625 bt_vhci_add(vlan);
626 return 0;
628 } else if (strstart(opt, "device:", &endp))
629 return !bt_device_add(endp);
631 fprintf(stderr, "qemu: bad bluetooth parameter '%s'\n", opt);
632 return 1;
635 /***********************************************************/
636 /* QEMU Block devices */
638 #define HD_ALIAS "index=%d,media=disk"
639 #define CDROM_ALIAS "index=2,media=cdrom"
640 #define FD_ALIAS "index=%d,if=floppy"
641 #define PFLASH_ALIAS "if=pflash"
642 #define MTD_ALIAS "if=mtd"
643 #define SD_ALIAS "index=0,if=sd"
645 QemuOpts *drive_add(const char *file, const char *fmt, ...)
647 va_list ap;
648 char optstr[1024];
649 QemuOpts *opts;
651 va_start(ap, fmt);
652 vsnprintf(optstr, sizeof(optstr), fmt, ap);
653 va_end(ap);
655 opts = qemu_opts_parse(&qemu_drive_opts, optstr, 0);
656 if (!opts) {
657 return NULL;
659 if (file)
660 qemu_opt_set(opts, "file", file);
661 return opts;
664 DriveInfo *drive_get(BlockInterfaceType type, int bus, int unit)
666 DriveInfo *dinfo;
668 /* seek interface, bus and unit */
670 QTAILQ_FOREACH(dinfo, &drives, next) {
671 if (dinfo->type == type &&
672 dinfo->bus == bus &&
673 dinfo->unit == unit)
674 return dinfo;
677 return NULL;
680 DriveInfo *drive_get_by_id(const char *id)
682 DriveInfo *dinfo;
684 QTAILQ_FOREACH(dinfo, &drives, next) {
685 if (strcmp(id, dinfo->id))
686 continue;
687 return dinfo;
689 return NULL;
692 int drive_get_max_bus(BlockInterfaceType type)
694 int max_bus;
695 DriveInfo *dinfo;
697 max_bus = -1;
698 QTAILQ_FOREACH(dinfo, &drives, next) {
699 if(dinfo->type == type &&
700 dinfo->bus > max_bus)
701 max_bus = dinfo->bus;
703 return max_bus;
706 const char *drive_get_serial(BlockDriverState *bdrv)
708 DriveInfo *dinfo;
710 QTAILQ_FOREACH(dinfo, &drives, next) {
711 if (dinfo->bdrv == bdrv)
712 return dinfo->serial;
715 return "\0";
718 BlockInterfaceErrorAction drive_get_on_error(
719 BlockDriverState *bdrv, int is_read)
721 DriveInfo *dinfo;
723 QTAILQ_FOREACH(dinfo, &drives, next) {
724 if (dinfo->bdrv == bdrv)
725 return is_read ? dinfo->on_read_error : dinfo->on_write_error;
728 return is_read ? BLOCK_ERR_REPORT : BLOCK_ERR_STOP_ENOSPC;
731 static void bdrv_format_print(void *opaque, const char *name)
733 fprintf(stderr, " %s", name);
736 void drive_uninit(DriveInfo *dinfo)
738 qemu_opts_del(dinfo->opts);
739 bdrv_delete(dinfo->bdrv);
740 QTAILQ_REMOVE(&drives, dinfo, next);
741 qemu_free(dinfo);
744 static int parse_block_error_action(const char *buf, int is_read)
746 if (!strcmp(buf, "ignore")) {
747 return BLOCK_ERR_IGNORE;
748 } else if (!is_read && !strcmp(buf, "enospc")) {
749 return BLOCK_ERR_STOP_ENOSPC;
750 } else if (!strcmp(buf, "stop")) {
751 return BLOCK_ERR_STOP_ANY;
752 } else if (!strcmp(buf, "report")) {
753 return BLOCK_ERR_REPORT;
754 } else {
755 fprintf(stderr, "qemu: '%s' invalid %s error action\n",
756 buf, is_read ? "read" : "write");
757 return -1;
761 DriveInfo *drive_init(QemuOpts *opts, void *opaque,
762 int *fatal_error)
764 const char *buf;
765 const char *file = NULL;
766 char devname[128];
767 const char *serial;
768 const char *mediastr = "";
769 BlockInterfaceType type;
770 enum { MEDIA_DISK, MEDIA_CDROM } media;
771 int bus_id, unit_id;
772 int cyls, heads, secs, translation;
773 BlockDriver *drv = NULL;
774 QEMUMachine *machine = opaque;
775 int max_devs;
776 int index;
777 int ro = 0;
778 int bdrv_flags = 0;
779 int on_read_error, on_write_error;
780 const char *devaddr;
781 DriveInfo *dinfo;
782 int is_extboot = 0;
783 int snapshot = 0;
785 *fatal_error = 1;
787 translation = BIOS_ATA_TRANSLATION_AUTO;
789 if (machine && machine->use_scsi) {
790 type = IF_SCSI;
791 max_devs = MAX_SCSI_DEVS;
792 pstrcpy(devname, sizeof(devname), "scsi");
793 } else {
794 type = IF_IDE;
795 max_devs = MAX_IDE_DEVS;
796 pstrcpy(devname, sizeof(devname), "ide");
798 media = MEDIA_DISK;
800 /* extract parameters */
801 bus_id = qemu_opt_get_number(opts, "bus", 0);
802 unit_id = qemu_opt_get_number(opts, "unit", -1);
803 index = qemu_opt_get_number(opts, "index", -1);
805 cyls = qemu_opt_get_number(opts, "cyls", 0);
806 heads = qemu_opt_get_number(opts, "heads", 0);
807 secs = qemu_opt_get_number(opts, "secs", 0);
809 snapshot = qemu_opt_get_bool(opts, "snapshot", 0);
810 ro = qemu_opt_get_bool(opts, "readonly", 0);
812 file = qemu_opt_get(opts, "file");
813 serial = qemu_opt_get(opts, "serial");
815 if ((buf = qemu_opt_get(opts, "if")) != NULL) {
816 pstrcpy(devname, sizeof(devname), buf);
817 if (!strcmp(buf, "ide")) {
818 type = IF_IDE;
819 max_devs = MAX_IDE_DEVS;
820 } else if (!strcmp(buf, "scsi")) {
821 type = IF_SCSI;
822 max_devs = MAX_SCSI_DEVS;
823 } else if (!strcmp(buf, "floppy")) {
824 type = IF_FLOPPY;
825 max_devs = 0;
826 } else if (!strcmp(buf, "pflash")) {
827 type = IF_PFLASH;
828 max_devs = 0;
829 } else if (!strcmp(buf, "mtd")) {
830 type = IF_MTD;
831 max_devs = 0;
832 } else if (!strcmp(buf, "sd")) {
833 type = IF_SD;
834 max_devs = 0;
835 } else if (!strcmp(buf, "virtio")) {
836 type = IF_VIRTIO;
837 max_devs = 0;
838 } else if (!strcmp(buf, "xen")) {
839 type = IF_XEN;
840 max_devs = 0;
841 } else if (!strcmp(buf, "none")) {
842 type = IF_NONE;
843 max_devs = 0;
844 } else {
845 fprintf(stderr, "qemu: unsupported bus type '%s'\n", buf);
846 return NULL;
850 if (cyls || heads || secs) {
851 if (cyls < 1 || (type == IF_IDE && cyls > 16383)) {
852 fprintf(stderr, "qemu: '%s' invalid physical cyls number\n", buf);
853 return NULL;
855 if (heads < 1 || (type == IF_IDE && heads > 16)) {
856 fprintf(stderr, "qemu: '%s' invalid physical heads number\n", buf);
857 return NULL;
859 if (secs < 1 || (type == IF_IDE && secs > 63)) {
860 fprintf(stderr, "qemu: '%s' invalid physical secs number\n", buf);
861 return NULL;
865 if ((buf = qemu_opt_get(opts, "trans")) != NULL) {
866 if (!cyls) {
867 fprintf(stderr,
868 "qemu: '%s' trans must be used with cyls,heads and secs\n",
869 buf);
870 return NULL;
872 if (!strcmp(buf, "none"))
873 translation = BIOS_ATA_TRANSLATION_NONE;
874 else if (!strcmp(buf, "lba"))
875 translation = BIOS_ATA_TRANSLATION_LBA;
876 else if (!strcmp(buf, "auto"))
877 translation = BIOS_ATA_TRANSLATION_AUTO;
878 else {
879 fprintf(stderr, "qemu: '%s' invalid translation type\n", buf);
880 return NULL;
884 if ((buf = qemu_opt_get(opts, "media")) != NULL) {
885 if (!strcmp(buf, "disk")) {
886 media = MEDIA_DISK;
887 } else if (!strcmp(buf, "cdrom")) {
888 if (cyls || secs || heads) {
889 fprintf(stderr,
890 "qemu: '%s' invalid physical CHS format\n", buf);
891 return NULL;
893 media = MEDIA_CDROM;
894 } else {
895 fprintf(stderr, "qemu: '%s' invalid media\n", buf);
896 return NULL;
900 if ((buf = qemu_opt_get(opts, "cache")) != NULL) {
901 if (!strcmp(buf, "off") || !strcmp(buf, "none")) {
902 bdrv_flags |= BDRV_O_NOCACHE;
903 } else if (!strcmp(buf, "writeback")) {
904 bdrv_flags |= BDRV_O_CACHE_WB;
905 } else if (!strcmp(buf, "unsafe")) {
906 bdrv_flags |= BDRV_O_CACHE_WB;
907 bdrv_flags |= BDRV_O_NO_FLUSH;
908 } else if (!strcmp(buf, "writethrough")) {
909 /* this is the default */
910 } else {
911 fprintf(stderr, "qemu: invalid cache option\n");
912 return NULL;
916 #ifdef CONFIG_LINUX_AIO
917 if ((buf = qemu_opt_get(opts, "aio")) != NULL) {
918 if (!strcmp(buf, "native")) {
919 bdrv_flags |= BDRV_O_NATIVE_AIO;
920 } else if (!strcmp(buf, "threads")) {
921 /* this is the default */
922 } else {
923 fprintf(stderr, "qemu: invalid aio option\n");
924 return NULL;
927 #endif
929 if ((buf = qemu_opt_get(opts, "format")) != NULL) {
930 if (strcmp(buf, "?") == 0) {
931 fprintf(stderr, "qemu: Supported formats:");
932 bdrv_iterate_format(bdrv_format_print, NULL);
933 fprintf(stderr, "\n");
934 return NULL;
936 drv = bdrv_find_whitelisted_format(buf);
937 if (!drv) {
938 fprintf(stderr, "qemu: '%s' invalid format\n", buf);
939 return NULL;
943 is_extboot = qemu_opt_get_bool(opts, "boot", 0);
944 if (is_extboot && extboot_drive) {
945 fprintf(stderr, "qemu: two bootable drives specified\n");
946 return NULL;
949 on_write_error = BLOCK_ERR_STOP_ENOSPC;
950 if ((buf = qemu_opt_get(opts, "werror")) != NULL) {
951 if (type != IF_IDE && type != IF_SCSI && type != IF_VIRTIO && type != IF_NONE) {
952 fprintf(stderr, "werror is no supported by this format\n");
953 return NULL;
956 on_write_error = parse_block_error_action(buf, 0);
957 if (on_write_error < 0) {
958 return NULL;
962 on_read_error = BLOCK_ERR_REPORT;
963 if ((buf = qemu_opt_get(opts, "rerror")) != NULL) {
964 if (type != IF_IDE && type != IF_VIRTIO && type != IF_NONE) {
965 fprintf(stderr, "rerror is no supported by this format\n");
966 return NULL;
969 on_read_error = parse_block_error_action(buf, 1);
970 if (on_read_error < 0) {
971 return NULL;
975 if ((devaddr = qemu_opt_get(opts, "addr")) != NULL) {
976 if (type != IF_VIRTIO) {
977 fprintf(stderr, "addr is not supported\n");
978 return NULL;
982 /* compute bus and unit according index */
984 if (index != -1) {
985 if (bus_id != 0 || unit_id != -1) {
986 fprintf(stderr,
987 "qemu: index cannot be used with bus and unit\n");
988 return NULL;
990 if (max_devs == 0)
992 unit_id = index;
993 bus_id = 0;
994 } else {
995 unit_id = index % max_devs;
996 bus_id = index / max_devs;
1000 /* if user doesn't specify a unit_id,
1001 * try to find the first free
1004 if (unit_id == -1) {
1005 unit_id = 0;
1006 while (drive_get(type, bus_id, unit_id) != NULL) {
1007 unit_id++;
1008 if (max_devs && unit_id >= max_devs) {
1009 unit_id -= max_devs;
1010 bus_id++;
1015 /* check unit id */
1017 if (max_devs && unit_id >= max_devs) {
1018 fprintf(stderr, "qemu: unit %d too big (max is %d)\n",
1019 unit_id, max_devs - 1);
1020 return NULL;
1024 * ignore multiple definitions
1027 if (drive_get(type, bus_id, unit_id) != NULL) {
1028 *fatal_error = 0;
1029 return NULL;
1032 /* init */
1034 dinfo = qemu_mallocz(sizeof(*dinfo));
1035 if ((buf = qemu_opts_id(opts)) != NULL) {
1036 dinfo->id = qemu_strdup(buf);
1037 } else {
1038 /* no id supplied -> create one */
1039 dinfo->id = qemu_mallocz(32);
1040 if (type == IF_IDE || type == IF_SCSI)
1041 mediastr = (media == MEDIA_CDROM) ? "-cd" : "-hd";
1042 if (max_devs)
1043 snprintf(dinfo->id, 32, "%s%i%s%i",
1044 devname, bus_id, mediastr, unit_id);
1045 else
1046 snprintf(dinfo->id, 32, "%s%s%i",
1047 devname, mediastr, unit_id);
1049 dinfo->bdrv = bdrv_new(dinfo->id);
1050 dinfo->devaddr = devaddr;
1051 dinfo->type = type;
1052 dinfo->bus = bus_id;
1053 dinfo->unit = unit_id;
1054 dinfo->on_read_error = on_read_error;
1055 dinfo->on_write_error = on_write_error;
1056 dinfo->opts = opts;
1057 if (serial)
1058 strncpy(dinfo->serial, serial, sizeof(serial));
1059 QTAILQ_INSERT_TAIL(&drives, dinfo, next);
1060 if (is_extboot) {
1061 extboot_drive = dinfo;
1064 switch(type) {
1065 case IF_IDE:
1066 case IF_SCSI:
1067 case IF_XEN:
1068 case IF_NONE:
1069 switch(media) {
1070 case MEDIA_DISK:
1071 if (cyls != 0) {
1072 bdrv_set_geometry_hint(dinfo->bdrv, cyls, heads, secs);
1073 bdrv_set_translation_hint(dinfo->bdrv, translation);
1075 break;
1076 case MEDIA_CDROM:
1077 bdrv_set_type_hint(dinfo->bdrv, BDRV_TYPE_CDROM);
1078 break;
1080 break;
1081 case IF_SD:
1082 /* FIXME: This isn't really a floppy, but it's a reasonable
1083 approximation. */
1084 case IF_FLOPPY:
1085 bdrv_set_type_hint(dinfo->bdrv, BDRV_TYPE_FLOPPY);
1086 break;
1087 case IF_PFLASH:
1088 case IF_MTD:
1089 break;
1090 case IF_VIRTIO:
1091 /* add virtio block device */
1092 opts = qemu_opts_create(&qemu_device_opts, NULL, 0);
1093 qemu_opt_set(opts, "driver", "virtio-blk-pci");
1094 qemu_opt_set(opts, "drive", dinfo->id);
1095 if (devaddr)
1096 qemu_opt_set(opts, "addr", devaddr);
1097 break;
1098 case IF_COUNT:
1099 abort();
1101 if (!file) {
1102 *fatal_error = 0;
1103 return NULL;
1105 if (snapshot) {
1106 /* always use cache=unsafe with snapshot */
1107 bdrv_flags &= ~BDRV_O_CACHE_MASK;
1108 bdrv_flags |= (BDRV_O_SNAPSHOT|BDRV_O_CACHE_WB|BDRV_O_NO_FLUSH);
1111 if (media == MEDIA_CDROM) {
1112 /* CDROM is fine for any interface, don't check. */
1113 ro = 1;
1114 } else if (ro == 1) {
1115 if (type != IF_SCSI && type != IF_VIRTIO && type != IF_FLOPPY && type != IF_NONE) {
1116 fprintf(stderr, "qemu: readonly flag not supported for drive with this interface\n");
1117 return NULL;
1121 bdrv_flags |= ro ? 0 : BDRV_O_RDWR;
1123 if (bdrv_open(dinfo->bdrv, file, bdrv_flags, drv) < 0) {
1124 fprintf(stderr, "qemu: could not open disk image %s: %s\n",
1125 file, strerror(errno));
1126 return NULL;
1129 if (bdrv_key_required(dinfo->bdrv))
1130 autostart = 0;
1131 *fatal_error = 0;
1132 return dinfo;
1135 static int drive_init_func(QemuOpts *opts, void *opaque)
1137 QEMUMachine *machine = opaque;
1138 int fatal_error = 0;
1140 if (drive_init(opts, machine, &fatal_error) == NULL) {
1141 if (fatal_error)
1142 return 1;
1144 return 0;
1147 static int drive_enable_snapshot(QemuOpts *opts, void *opaque)
1149 if (NULL == qemu_opt_get(opts, "snapshot")) {
1150 qemu_opt_set(opts, "snapshot", "on");
1152 return 0;
1155 void qemu_register_boot_set(QEMUBootSetHandler *func, void *opaque)
1157 boot_set_handler = func;
1158 boot_set_opaque = opaque;
1161 int qemu_boot_set(const char *boot_devices)
1163 if (!boot_set_handler) {
1164 return -EINVAL;
1166 return boot_set_handler(boot_set_opaque, boot_devices);
1169 static void validate_bootdevices(char *devices)
1171 /* We just do some generic consistency checks */
1172 const char *p;
1173 int bitmap = 0;
1175 for (p = devices; *p != '\0'; p++) {
1176 /* Allowed boot devices are:
1177 * a-b: floppy disk drives
1178 * c-f: IDE disk drives
1179 * g-m: machine implementation dependant drives
1180 * n-p: network devices
1181 * It's up to each machine implementation to check if the given boot
1182 * devices match the actual hardware implementation and firmware
1183 * features.
1185 if (*p < 'a' || *p > 'p') {
1186 fprintf(stderr, "Invalid boot device '%c'\n", *p);
1187 exit(1);
1189 if (bitmap & (1 << (*p - 'a'))) {
1190 fprintf(stderr, "Boot device '%c' was given twice\n", *p);
1191 exit(1);
1193 bitmap |= 1 << (*p - 'a');
1197 static void restore_boot_devices(void *opaque)
1199 char *standard_boot_devices = opaque;
1200 static int first = 1;
1202 /* Restore boot order and remove ourselves after the first boot */
1203 if (first) {
1204 first = 0;
1205 return;
1208 qemu_boot_set(standard_boot_devices);
1210 qemu_unregister_reset(restore_boot_devices, standard_boot_devices);
1211 qemu_free(standard_boot_devices);
1214 static void numa_add(const char *optarg)
1216 char option[128];
1217 char *endptr;
1218 unsigned long long value, endvalue;
1219 int nodenr;
1221 optarg = get_opt_name(option, 128, optarg, ',') + 1;
1222 if (!strcmp(option, "node")) {
1223 if (get_param_value(option, 128, "nodeid", optarg) == 0) {
1224 nodenr = nb_numa_nodes;
1225 } else {
1226 nodenr = strtoull(option, NULL, 10);
1229 if (get_param_value(option, 128, "mem", optarg) == 0) {
1230 node_mem[nodenr] = 0;
1231 } else {
1232 value = strtoull(option, &endptr, 0);
1233 switch (*endptr) {
1234 case 0: case 'M': case 'm':
1235 value <<= 20;
1236 break;
1237 case 'G': case 'g':
1238 value <<= 30;
1239 break;
1241 node_mem[nodenr] = value;
1243 if (get_param_value(option, 128, "cpus", optarg) == 0) {
1244 node_cpumask[nodenr] = 0;
1245 } else {
1246 value = strtoull(option, &endptr, 10);
1247 if (value >= 64) {
1248 value = 63;
1249 fprintf(stderr, "only 64 CPUs in NUMA mode supported.\n");
1250 } else {
1251 if (*endptr == '-') {
1252 endvalue = strtoull(endptr+1, &endptr, 10);
1253 if (endvalue >= 63) {
1254 endvalue = 62;
1255 fprintf(stderr,
1256 "only 63 CPUs in NUMA mode supported.\n");
1258 value = (2ULL << endvalue) - (1ULL << value);
1259 } else {
1260 value = 1ULL << value;
1263 node_cpumask[nodenr] = value;
1265 nb_numa_nodes++;
1267 return;
1270 static void smp_parse(const char *optarg)
1272 int smp, sockets = 0, threads = 0, cores = 0;
1273 char *endptr;
1274 char option[128];
1276 smp = strtoul(optarg, &endptr, 10);
1277 if (endptr != optarg) {
1278 if (*endptr == ',') {
1279 endptr++;
1282 if (get_param_value(option, 128, "sockets", endptr) != 0)
1283 sockets = strtoull(option, NULL, 10);
1284 if (get_param_value(option, 128, "cores", endptr) != 0)
1285 cores = strtoull(option, NULL, 10);
1286 if (get_param_value(option, 128, "threads", endptr) != 0)
1287 threads = strtoull(option, NULL, 10);
1288 if (get_param_value(option, 128, "maxcpus", endptr) != 0)
1289 max_cpus = strtoull(option, NULL, 10);
1291 /* compute missing values, prefer sockets over cores over threads */
1292 if (smp == 0 || sockets == 0) {
1293 sockets = sockets > 0 ? sockets : 1;
1294 cores = cores > 0 ? cores : 1;
1295 threads = threads > 0 ? threads : 1;
1296 if (smp == 0) {
1297 smp = cores * threads * sockets;
1299 } else {
1300 if (cores == 0) {
1301 threads = threads > 0 ? threads : 1;
1302 cores = smp / (sockets * threads);
1303 } else {
1304 if (sockets) {
1305 threads = smp / (cores * sockets);
1309 smp_cpus = smp;
1310 smp_cores = cores > 0 ? cores : 1;
1311 smp_threads = threads > 0 ? threads : 1;
1312 if (max_cpus == 0)
1313 max_cpus = smp_cpus;
1316 /***********************************************************/
1317 /* USB devices */
1319 static int usb_device_add(const char *devname, int is_hotplug)
1321 const char *p;
1322 USBDevice *dev = NULL;
1324 if (!usb_enabled)
1325 return -1;
1327 /* drivers with .usbdevice_name entry in USBDeviceInfo */
1328 dev = usbdevice_create(devname);
1329 if (dev)
1330 goto done;
1332 /* the other ones */
1333 if (strstart(devname, "host:", &p)) {
1334 dev = usb_host_device_open(p);
1335 } else if (!strcmp(devname, "bt") || strstart(devname, "bt:", &p)) {
1336 dev = usb_bt_init(devname[2] ? hci_init(p) :
1337 bt_new_hci(qemu_find_bt_vlan(0)));
1338 } else {
1339 return -1;
1341 if (!dev)
1342 return -1;
1344 done:
1345 return 0;
1348 static int usb_device_del(const char *devname)
1350 int bus_num, addr;
1351 const char *p;
1353 if (strstart(devname, "host:", &p))
1354 return usb_host_device_close(p);
1356 if (!usb_enabled)
1357 return -1;
1359 p = strchr(devname, '.');
1360 if (!p)
1361 return -1;
1362 bus_num = strtoul(devname, NULL, 0);
1363 addr = strtoul(p + 1, NULL, 0);
1365 return usb_device_delete_addr(bus_num, addr);
1368 static int usb_parse(const char *cmdline)
1370 int r;
1371 r = usb_device_add(cmdline, 0);
1372 if (r < 0) {
1373 fprintf(stderr, "qemu: could not add USB device '%s'\n", cmdline);
1375 return r;
1378 void do_usb_add(Monitor *mon, const QDict *qdict)
1380 const char *devname = qdict_get_str(qdict, "devname");
1381 if (usb_device_add(devname, 1) < 0) {
1382 error_report("could not add USB device '%s'", devname);
1386 void do_usb_del(Monitor *mon, const QDict *qdict)
1388 const char *devname = qdict_get_str(qdict, "devname");
1389 if (usb_device_del(devname) < 0) {
1390 error_report("could not delete USB device '%s'", devname);
1394 /***********************************************************/
1395 /* PCMCIA/Cardbus */
1397 static struct pcmcia_socket_entry_s {
1398 PCMCIASocket *socket;
1399 struct pcmcia_socket_entry_s *next;
1400 } *pcmcia_sockets = 0;
1402 void pcmcia_socket_register(PCMCIASocket *socket)
1404 struct pcmcia_socket_entry_s *entry;
1406 entry = qemu_malloc(sizeof(struct pcmcia_socket_entry_s));
1407 entry->socket = socket;
1408 entry->next = pcmcia_sockets;
1409 pcmcia_sockets = entry;
1412 void pcmcia_socket_unregister(PCMCIASocket *socket)
1414 struct pcmcia_socket_entry_s *entry, **ptr;
1416 ptr = &pcmcia_sockets;
1417 for (entry = *ptr; entry; ptr = &entry->next, entry = *ptr)
1418 if (entry->socket == socket) {
1419 *ptr = entry->next;
1420 qemu_free(entry);
1424 void pcmcia_info(Monitor *mon)
1426 struct pcmcia_socket_entry_s *iter;
1428 if (!pcmcia_sockets)
1429 monitor_printf(mon, "No PCMCIA sockets\n");
1431 for (iter = pcmcia_sockets; iter; iter = iter->next)
1432 monitor_printf(mon, "%s: %s\n", iter->socket->slot_string,
1433 iter->socket->attached ? iter->socket->card_string :
1434 "Empty");
1437 /***********************************************************/
1438 /* I/O handling */
1440 typedef struct IOHandlerRecord {
1441 int fd;
1442 IOCanReadHandler *fd_read_poll;
1443 IOHandler *fd_read;
1444 IOHandler *fd_write;
1445 int deleted;
1446 void *opaque;
1447 /* temporary data */
1448 struct pollfd *ufd;
1449 QLIST_ENTRY(IOHandlerRecord) next;
1450 } IOHandlerRecord;
1452 static QLIST_HEAD(, IOHandlerRecord) io_handlers =
1453 QLIST_HEAD_INITIALIZER(io_handlers);
1456 /* XXX: fd_read_poll should be suppressed, but an API change is
1457 necessary in the character devices to suppress fd_can_read(). */
1458 int qemu_set_fd_handler2(int fd,
1459 IOCanReadHandler *fd_read_poll,
1460 IOHandler *fd_read,
1461 IOHandler *fd_write,
1462 void *opaque)
1464 IOHandlerRecord *ioh;
1466 if (!fd_read && !fd_write) {
1467 QLIST_FOREACH(ioh, &io_handlers, next) {
1468 if (ioh->fd == fd) {
1469 ioh->deleted = 1;
1470 break;
1473 } else {
1474 QLIST_FOREACH(ioh, &io_handlers, next) {
1475 if (ioh->fd == fd)
1476 goto found;
1478 ioh = qemu_mallocz(sizeof(IOHandlerRecord));
1479 QLIST_INSERT_HEAD(&io_handlers, ioh, next);
1480 found:
1481 ioh->fd = fd;
1482 ioh->fd_read_poll = fd_read_poll;
1483 ioh->fd_read = fd_read;
1484 ioh->fd_write = fd_write;
1485 ioh->opaque = opaque;
1486 ioh->deleted = 0;
1488 qemu_notify_event();
1489 return 0;
1492 int qemu_set_fd_handler(int fd,
1493 IOHandler *fd_read,
1494 IOHandler *fd_write,
1495 void *opaque)
1497 return qemu_set_fd_handler2(fd, NULL, fd_read, fd_write, opaque);
1500 /***********************************************************/
1501 /* machine registration */
1503 static QEMUMachine *first_machine = NULL;
1504 QEMUMachine *current_machine = NULL;
1506 int qemu_register_machine(QEMUMachine *m)
1508 QEMUMachine **pm;
1509 pm = &first_machine;
1510 while (*pm != NULL)
1511 pm = &(*pm)->next;
1512 m->next = NULL;
1513 *pm = m;
1514 return 0;
1517 static QEMUMachine *find_machine(const char *name)
1519 QEMUMachine *m;
1521 for(m = first_machine; m != NULL; m = m->next) {
1522 if (!strcmp(m->name, name))
1523 return m;
1524 if (m->alias && !strcmp(m->alias, name))
1525 return m;
1527 return NULL;
1530 static QEMUMachine *find_default_machine(void)
1532 QEMUMachine *m;
1534 for(m = first_machine; m != NULL; m = m->next) {
1535 if (m->is_default) {
1536 return m;
1539 return NULL;
1542 /***********************************************************/
1543 /* main execution loop */
1545 static void gui_update(void *opaque)
1547 uint64_t interval = GUI_REFRESH_INTERVAL;
1548 DisplayState *ds = opaque;
1549 DisplayChangeListener *dcl = ds->listeners;
1551 qemu_flush_coalesced_mmio_buffer();
1552 dpy_refresh(ds);
1554 while (dcl != NULL) {
1555 if (dcl->gui_timer_interval &&
1556 dcl->gui_timer_interval < interval)
1557 interval = dcl->gui_timer_interval;
1558 dcl = dcl->next;
1560 qemu_mod_timer(ds->gui_timer, interval + qemu_get_clock(rt_clock));
1563 static void nographic_update(void *opaque)
1565 uint64_t interval = GUI_REFRESH_INTERVAL;
1567 qemu_flush_coalesced_mmio_buffer();
1568 qemu_mod_timer(nographic_timer, interval + qemu_get_clock(rt_clock));
1571 struct vm_change_state_entry {
1572 VMChangeStateHandler *cb;
1573 void *opaque;
1574 QLIST_ENTRY (vm_change_state_entry) entries;
1577 static QLIST_HEAD(vm_change_state_head, vm_change_state_entry) vm_change_state_head;
1579 VMChangeStateEntry *qemu_add_vm_change_state_handler(VMChangeStateHandler *cb,
1580 void *opaque)
1582 VMChangeStateEntry *e;
1584 e = qemu_mallocz(sizeof (*e));
1586 e->cb = cb;
1587 e->opaque = opaque;
1588 QLIST_INSERT_HEAD(&vm_change_state_head, e, entries);
1589 return e;
1592 void qemu_del_vm_change_state_handler(VMChangeStateEntry *e)
1594 QLIST_REMOVE (e, entries);
1595 qemu_free (e);
1598 void vm_state_notify(int running, int reason)
1600 VMChangeStateEntry *e;
1602 for (e = vm_change_state_head.lh_first; e; e = e->entries.le_next) {
1603 e->cb(e->opaque, running, reason);
1607 void vm_start(void)
1609 if (!vm_running) {
1610 cpu_enable_ticks();
1611 vm_running = 1;
1612 vm_state_notify(1, 0);
1613 resume_all_vcpus();
1614 monitor_protocol_event(QEVENT_RESUME, NULL);
1618 /* reset/shutdown handler */
1620 typedef struct QEMUResetEntry {
1621 QTAILQ_ENTRY(QEMUResetEntry) entry;
1622 QEMUResetHandler *func;
1623 void *opaque;
1624 } QEMUResetEntry;
1626 static QTAILQ_HEAD(reset_handlers, QEMUResetEntry) reset_handlers =
1627 QTAILQ_HEAD_INITIALIZER(reset_handlers);
1628 static int reset_requested;
1629 static int shutdown_requested;
1630 static int powerdown_requested;
1631 int debug_requested;
1632 int vmstop_requested;
1634 int qemu_no_shutdown(void)
1636 int r = no_shutdown;
1637 no_shutdown = 0;
1638 return r;
1641 int qemu_shutdown_requested(void)
1643 int r = shutdown_requested;
1644 shutdown_requested = 0;
1645 return r;
1648 int qemu_reset_requested(void)
1650 int r = reset_requested;
1651 reset_requested = 0;
1652 return r;
1655 int qemu_powerdown_requested(void)
1657 int r = powerdown_requested;
1658 powerdown_requested = 0;
1659 return r;
1662 static int qemu_debug_requested(void)
1664 int r = debug_requested;
1665 debug_requested = 0;
1666 return r;
1669 static int qemu_vmstop_requested(void)
1671 int r = vmstop_requested;
1672 vmstop_requested = 0;
1673 return r;
1676 void qemu_register_reset(QEMUResetHandler *func, void *opaque)
1678 QEMUResetEntry *re = qemu_mallocz(sizeof(QEMUResetEntry));
1680 re->func = func;
1681 re->opaque = opaque;
1682 QTAILQ_INSERT_TAIL(&reset_handlers, re, entry);
1685 void qemu_unregister_reset(QEMUResetHandler *func, void *opaque)
1687 QEMUResetEntry *re;
1689 QTAILQ_FOREACH(re, &reset_handlers, entry) {
1690 if (re->func == func && re->opaque == opaque) {
1691 QTAILQ_REMOVE(&reset_handlers, re, entry);
1692 qemu_free(re);
1693 return;
1698 void qemu_system_reset(void)
1700 QEMUResetEntry *re, *nre;
1702 /* reset all devices */
1703 QTAILQ_FOREACH_SAFE(re, &reset_handlers, entry, nre) {
1704 re->func(re->opaque);
1706 monitor_protocol_event(QEVENT_RESET, NULL);
1707 cpu_synchronize_all_post_reset();
1710 void qemu_system_reset_request(void)
1712 if (no_reboot) {
1713 shutdown_requested = 1;
1714 } else {
1715 reset_requested = 1;
1717 if (cpu_single_env) {
1718 cpu_single_env->stopped = 1;
1720 qemu_notify_event();
1723 void qemu_system_shutdown_request(void)
1725 shutdown_requested = 1;
1726 qemu_notify_event();
1729 void qemu_system_powerdown_request(void)
1731 powerdown_requested = 1;
1732 qemu_notify_event();
1735 void main_loop_wait(int nonblocking)
1737 IOHandlerRecord *ioh;
1738 fd_set rfds, wfds, xfds;
1739 int ret, nfds;
1740 struct timeval tv;
1741 int timeout;
1743 if (nonblocking)
1744 timeout = 0;
1745 else {
1746 timeout = qemu_calculate_timeout();
1747 qemu_bh_update_timeout(&timeout);
1750 os_host_main_loop_wait(&timeout);
1752 /* poll any events */
1753 /* XXX: separate device handlers from system ones */
1754 nfds = -1;
1755 FD_ZERO(&rfds);
1756 FD_ZERO(&wfds);
1757 FD_ZERO(&xfds);
1758 QLIST_FOREACH(ioh, &io_handlers, next) {
1759 if (ioh->deleted)
1760 continue;
1761 if (ioh->fd_read &&
1762 (!ioh->fd_read_poll ||
1763 ioh->fd_read_poll(ioh->opaque) != 0)) {
1764 FD_SET(ioh->fd, &rfds);
1765 if (ioh->fd > nfds)
1766 nfds = ioh->fd;
1768 if (ioh->fd_write) {
1769 FD_SET(ioh->fd, &wfds);
1770 if (ioh->fd > nfds)
1771 nfds = ioh->fd;
1775 tv.tv_sec = timeout / 1000;
1776 tv.tv_usec = (timeout % 1000) * 1000;
1778 slirp_select_fill(&nfds, &rfds, &wfds, &xfds);
1780 qemu_mutex_unlock_iothread();
1781 ret = select(nfds + 1, &rfds, &wfds, &xfds, &tv);
1782 qemu_mutex_lock_iothread();
1783 if (ret > 0) {
1784 IOHandlerRecord *pioh;
1786 QLIST_FOREACH_SAFE(ioh, &io_handlers, next, pioh) {
1787 if (ioh->deleted) {
1788 QLIST_REMOVE(ioh, next);
1789 qemu_free(ioh);
1790 continue;
1792 if (ioh->fd_read && FD_ISSET(ioh->fd, &rfds)) {
1793 ioh->fd_read(ioh->opaque);
1794 if (!(ioh->fd_read_poll && ioh->fd_read_poll(ioh->opaque)))
1795 FD_CLR(ioh->fd, &rfds);
1797 if (ioh->fd_write && FD_ISSET(ioh->fd, &wfds)) {
1798 ioh->fd_write(ioh->opaque);
1803 slirp_select_poll(&rfds, &wfds, &xfds, (ret < 0));
1805 qemu_run_all_timers();
1807 /* Check bottom-halves last in case any of the earlier events triggered
1808 them. */
1809 qemu_bh_poll();
1813 static int vm_can_run(void)
1815 if (powerdown_requested)
1816 return 0;
1817 if (reset_requested)
1818 return 0;
1819 if (shutdown_requested)
1820 return 0;
1821 if (debug_requested)
1822 return 0;
1823 return 1;
1826 qemu_irq qemu_system_powerdown;
1828 static void main_loop(void)
1830 int r;
1832 if (kvm_enabled()) {
1833 kvm_main_loop();
1834 cpu_disable_ticks();
1835 return;
1838 qemu_main_loop_start();
1840 for (;;) {
1841 do {
1842 bool nonblocking = false;
1843 #ifdef CONFIG_PROFILER
1844 int64_t ti;
1845 #endif
1846 #ifndef CONFIG_IOTHREAD
1847 nonblocking = tcg_cpu_exec();
1848 #endif
1849 #ifdef CONFIG_PROFILER
1850 ti = profile_getclock();
1851 #endif
1852 main_loop_wait(nonblocking);
1853 #ifdef CONFIG_PROFILER
1854 dev_time += profile_getclock() - ti;
1855 #endif
1856 } while (vm_can_run());
1858 if ((r = qemu_debug_requested())) {
1859 vm_stop(r);
1861 if (qemu_shutdown_requested()) {
1862 monitor_protocol_event(QEVENT_SHUTDOWN, NULL);
1863 if (no_shutdown) {
1864 vm_stop(0);
1865 no_shutdown = 0;
1866 } else
1867 break;
1869 if (qemu_reset_requested()) {
1870 pause_all_vcpus();
1871 qemu_system_reset();
1872 resume_all_vcpus();
1874 if (qemu_powerdown_requested()) {
1875 monitor_protocol_event(QEVENT_POWERDOWN, NULL);
1876 qemu_irq_raise(qemu_system_powerdown);
1878 if ((r = qemu_vmstop_requested())) {
1879 vm_stop(r);
1882 pause_all_vcpus();
1885 static void version(void)
1887 printf("QEMU emulator version " QEMU_VERSION QEMU_PKGVERSION ", Copyright (c) 2003-2008 Fabrice Bellard\n");
1890 static void help(int exitcode)
1892 const char *options_help =
1893 #define DEF(option, opt_arg, opt_enum, opt_help, arch_mask) \
1894 opt_help
1895 #define DEFHEADING(text) stringify(text) "\n"
1896 #include "qemu-options.def"
1897 #undef DEF
1898 #undef DEFHEADING
1899 #undef GEN_DOCS
1901 version();
1902 printf("usage: %s [options] [disk_image]\n"
1903 "\n"
1904 "'disk_image' is a raw hard disk image for IDE hard disk 0\n"
1905 "\n"
1906 "%s\n"
1907 "During emulation, the following keys are useful:\n"
1908 "ctrl-alt-f toggle full screen\n"
1909 "ctrl-alt-n switch to virtual console 'n'\n"
1910 "ctrl-alt toggle mouse and keyboard grab\n"
1911 "\n"
1912 "When using -nographic, press 'ctrl-a h' to get some help.\n",
1913 "qemu",
1914 options_help);
1915 exit(exitcode);
1918 #define HAS_ARG 0x0001
1920 typedef struct QEMUOption {
1921 const char *name;
1922 int flags;
1923 int index;
1924 uint32_t arch_mask;
1925 } QEMUOption;
1927 static const QEMUOption qemu_options[] = {
1928 { "h", 0, QEMU_OPTION_h, QEMU_ARCH_ALL },
1929 #define DEF(option, opt_arg, opt_enum, opt_help, arch_mask) \
1930 { option, opt_arg, opt_enum, arch_mask },
1931 #define DEFHEADING(text)
1932 #include "qemu-options.def"
1933 #undef DEF
1934 #undef DEFHEADING
1935 #undef GEN_DOCS
1936 { NULL },
1938 static void select_vgahw (const char *p)
1940 const char *opts;
1942 default_vga = 0;
1943 vga_interface_type = VGA_NONE;
1944 if (strstart(p, "std", &opts)) {
1945 vga_interface_type = VGA_STD;
1946 } else if (strstart(p, "cirrus", &opts)) {
1947 vga_interface_type = VGA_CIRRUS;
1948 } else if (strstart(p, "vmware", &opts)) {
1949 vga_interface_type = VGA_VMWARE;
1950 } else if (strstart(p, "xenfb", &opts)) {
1951 vga_interface_type = VGA_XENFB;
1952 } else if (!strstart(p, "none", &opts)) {
1953 invalid_vga:
1954 fprintf(stderr, "Unknown vga type: %s\n", p);
1955 exit(1);
1957 while (*opts) {
1958 const char *nextopt;
1960 if (strstart(opts, ",retrace=", &nextopt)) {
1961 opts = nextopt;
1962 if (strstart(opts, "dumb", &nextopt))
1963 vga_retrace_method = VGA_RETRACE_DUMB;
1964 else if (strstart(opts, "precise", &nextopt))
1965 vga_retrace_method = VGA_RETRACE_PRECISE;
1966 else goto invalid_vga;
1967 } else goto invalid_vga;
1968 opts = nextopt;
1972 static int balloon_parse(const char *arg)
1974 QemuOpts *opts;
1976 if (strcmp(arg, "none") == 0) {
1977 return 0;
1980 if (!strncmp(arg, "virtio", 6)) {
1981 if (arg[6] == ',') {
1982 /* have params -> parse them */
1983 opts = qemu_opts_parse(&qemu_device_opts, arg+7, 0);
1984 if (!opts)
1985 return -1;
1986 } else {
1987 /* create empty opts */
1988 opts = qemu_opts_create(&qemu_device_opts, NULL, 0);
1990 qemu_opt_set(opts, "driver", "virtio-balloon-pci");
1991 return 0;
1994 return -1;
1997 char *qemu_find_file(int type, const char *name)
1999 int len;
2000 const char *subdir;
2001 char *buf;
2003 /* If name contains path separators then try it as a straight path. */
2004 if ((strchr(name, '/') || strchr(name, '\\'))
2005 && access(name, R_OK) == 0) {
2006 return qemu_strdup(name);
2008 switch (type) {
2009 case QEMU_FILE_TYPE_BIOS:
2010 subdir = "";
2011 break;
2012 case QEMU_FILE_TYPE_KEYMAP:
2013 subdir = "keymaps/";
2014 break;
2015 default:
2016 abort();
2018 len = strlen(data_dir) + strlen(name) + strlen(subdir) + 2;
2019 buf = qemu_mallocz(len);
2020 snprintf(buf, len, "%s/%s%s", data_dir, subdir, name);
2021 if (access(buf, R_OK)) {
2022 qemu_free(buf);
2023 return NULL;
2025 return buf;
2028 static int device_help_func(QemuOpts *opts, void *opaque)
2030 return qdev_device_help(opts);
2033 static int device_init_func(QemuOpts *opts, void *opaque)
2035 DeviceState *dev;
2037 dev = qdev_device_add(opts);
2038 if (!dev)
2039 return -1;
2040 return 0;
2043 static int chardev_init_func(QemuOpts *opts, void *opaque)
2045 CharDriverState *chr;
2047 chr = qemu_chr_open_opts(opts, NULL);
2048 if (!chr)
2049 return -1;
2050 return 0;
2053 #ifdef CONFIG_LINUX
2054 static int fsdev_init_func(QemuOpts *opts, void *opaque)
2056 int ret;
2057 ret = qemu_fsdev_add(opts);
2059 return ret;
2061 #endif
2063 static int mon_init_func(QemuOpts *opts, void *opaque)
2065 CharDriverState *chr;
2066 const char *chardev;
2067 const char *mode;
2068 int flags;
2070 mode = qemu_opt_get(opts, "mode");
2071 if (mode == NULL) {
2072 mode = "readline";
2074 if (strcmp(mode, "readline") == 0) {
2075 flags = MONITOR_USE_READLINE;
2076 } else if (strcmp(mode, "control") == 0) {
2077 flags = MONITOR_USE_CONTROL;
2078 } else {
2079 fprintf(stderr, "unknown monitor mode \"%s\"\n", mode);
2080 exit(1);
2083 if (qemu_opt_get_bool(opts, "default", 0))
2084 flags |= MONITOR_IS_DEFAULT;
2086 chardev = qemu_opt_get(opts, "chardev");
2087 chr = qemu_chr_find(chardev);
2088 if (chr == NULL) {
2089 fprintf(stderr, "chardev \"%s\" not found\n", chardev);
2090 exit(1);
2093 monitor_init(chr, flags);
2094 return 0;
2097 static void monitor_parse(const char *optarg, const char *mode)
2099 static int monitor_device_index = 0;
2100 QemuOpts *opts;
2101 const char *p;
2102 char label[32];
2103 int def = 0;
2105 if (strstart(optarg, "chardev:", &p)) {
2106 snprintf(label, sizeof(label), "%s", p);
2107 } else {
2108 snprintf(label, sizeof(label), "compat_monitor%d",
2109 monitor_device_index);
2110 if (monitor_device_index == 0) {
2111 def = 1;
2113 opts = qemu_chr_parse_compat(label, optarg);
2114 if (!opts) {
2115 fprintf(stderr, "parse error: %s\n", optarg);
2116 exit(1);
2120 opts = qemu_opts_create(&qemu_mon_opts, label, 1);
2121 if (!opts) {
2122 fprintf(stderr, "duplicate chardev: %s\n", label);
2123 exit(1);
2125 qemu_opt_set(opts, "mode", mode);
2126 qemu_opt_set(opts, "chardev", label);
2127 if (def)
2128 qemu_opt_set(opts, "default", "on");
2129 monitor_device_index++;
2132 struct device_config {
2133 enum {
2134 DEV_USB, /* -usbdevice */
2135 DEV_BT, /* -bt */
2136 DEV_SERIAL, /* -serial */
2137 DEV_PARALLEL, /* -parallel */
2138 DEV_VIRTCON, /* -virtioconsole */
2139 DEV_DEBUGCON, /* -debugcon */
2140 } type;
2141 const char *cmdline;
2142 QTAILQ_ENTRY(device_config) next;
2144 QTAILQ_HEAD(, device_config) device_configs = QTAILQ_HEAD_INITIALIZER(device_configs);
2146 static void add_device_config(int type, const char *cmdline)
2148 struct device_config *conf;
2150 conf = qemu_mallocz(sizeof(*conf));
2151 conf->type = type;
2152 conf->cmdline = cmdline;
2153 QTAILQ_INSERT_TAIL(&device_configs, conf, next);
2156 static int foreach_device_config(int type, int (*func)(const char *cmdline))
2158 struct device_config *conf;
2159 int rc;
2161 QTAILQ_FOREACH(conf, &device_configs, next) {
2162 if (conf->type != type)
2163 continue;
2164 rc = func(conf->cmdline);
2165 if (0 != rc)
2166 return rc;
2168 return 0;
2171 static int serial_parse(const char *devname)
2173 static int index = 0;
2174 char label[32];
2176 if (strcmp(devname, "none") == 0)
2177 return 0;
2178 if (index == MAX_SERIAL_PORTS) {
2179 fprintf(stderr, "qemu: too many serial ports\n");
2180 exit(1);
2182 snprintf(label, sizeof(label), "serial%d", index);
2183 serial_hds[index] = qemu_chr_open(label, devname, NULL);
2184 if (!serial_hds[index]) {
2185 fprintf(stderr, "qemu: could not open serial device '%s': %s\n",
2186 devname, strerror(errno));
2187 return -1;
2189 index++;
2190 return 0;
2193 static int parallel_parse(const char *devname)
2195 static int index = 0;
2196 char label[32];
2198 if (strcmp(devname, "none") == 0)
2199 return 0;
2200 if (index == MAX_PARALLEL_PORTS) {
2201 fprintf(stderr, "qemu: too many parallel ports\n");
2202 exit(1);
2204 snprintf(label, sizeof(label), "parallel%d", index);
2205 parallel_hds[index] = qemu_chr_open(label, devname, NULL);
2206 if (!parallel_hds[index]) {
2207 fprintf(stderr, "qemu: could not open parallel device '%s': %s\n",
2208 devname, strerror(errno));
2209 return -1;
2211 index++;
2212 return 0;
2215 static int virtcon_parse(const char *devname)
2217 static int index = 0;
2218 char label[32];
2219 QemuOpts *bus_opts, *dev_opts;
2221 if (strcmp(devname, "none") == 0)
2222 return 0;
2223 if (index == MAX_VIRTIO_CONSOLES) {
2224 fprintf(stderr, "qemu: too many virtio consoles\n");
2225 exit(1);
2228 bus_opts = qemu_opts_create(&qemu_device_opts, NULL, 0);
2229 qemu_opt_set(bus_opts, "driver", "virtio-serial");
2231 dev_opts = qemu_opts_create(&qemu_device_opts, NULL, 0);
2232 qemu_opt_set(dev_opts, "driver", "virtconsole");
2234 snprintf(label, sizeof(label), "virtcon%d", index);
2235 virtcon_hds[index] = qemu_chr_open(label, devname, NULL);
2236 if (!virtcon_hds[index]) {
2237 fprintf(stderr, "qemu: could not open virtio console '%s': %s\n",
2238 devname, strerror(errno));
2239 return -1;
2241 qemu_opt_set(dev_opts, "chardev", label);
2243 index++;
2244 return 0;
2247 static int debugcon_parse(const char *devname)
2249 QemuOpts *opts;
2251 if (!qemu_chr_open("debugcon", devname, NULL)) {
2252 exit(1);
2254 opts = qemu_opts_create(&qemu_device_opts, "debugcon", 1);
2255 if (!opts) {
2256 fprintf(stderr, "qemu: already have a debugcon device\n");
2257 exit(1);
2259 qemu_opt_set(opts, "driver", "isa-debugcon");
2260 qemu_opt_set(opts, "chardev", "debugcon");
2261 return 0;
2264 static const QEMUOption *lookup_opt(int argc, char **argv,
2265 const char **poptarg, int *poptind)
2267 const QEMUOption *popt;
2268 int optind = *poptind;
2269 char *r = argv[optind];
2270 const char *optarg;
2272 loc_set_cmdline(argv, optind, 1);
2273 optind++;
2274 /* Treat --foo the same as -foo. */
2275 if (r[1] == '-')
2276 r++;
2277 popt = qemu_options;
2278 for(;;) {
2279 if (!popt->name) {
2280 error_report("invalid option");
2281 exit(1);
2283 if (!strcmp(popt->name, r + 1))
2284 break;
2285 popt++;
2287 if (popt->flags & HAS_ARG) {
2288 if (optind >= argc) {
2289 error_report("requires an argument");
2290 exit(1);
2292 optarg = argv[optind++];
2293 loc_set_cmdline(argv, optind - 2, 2);
2294 } else {
2295 optarg = NULL;
2298 *poptarg = optarg;
2299 *poptind = optind;
2301 return popt;
2304 int main(int argc, char **argv, char **envp)
2306 const char *gdbstub_dev = NULL;
2307 int i;
2308 int snapshot, linux_boot;
2309 const char *icount_option = NULL;
2310 const char *initrd_filename;
2311 const char *kernel_filename, *kernel_cmdline;
2312 char boot_devices[33] = "cad"; /* default to HD->floppy->CD-ROM */
2313 DisplayState *ds;
2314 DisplayChangeListener *dcl;
2315 int cyls, heads, secs, translation;
2316 QemuOpts *hda_opts = NULL, *opts;
2317 int optind;
2318 const char *optarg;
2319 const char *loadvm = NULL;
2320 QEMUMachine *machine;
2321 const char *cpu_model;
2322 int tb_size;
2323 const char *pid_file = NULL;
2324 const char *incoming = NULL;
2325 int show_vnc_port = 0;
2326 int defconfig = 1;
2328 error_set_progname(argv[0]);
2330 init_clocks();
2332 qemu_cache_utils_init(envp);
2334 QLIST_INIT (&vm_change_state_head);
2335 os_setup_early_signal_handling();
2337 module_call_init(MODULE_INIT_MACHINE);
2338 machine = find_default_machine();
2339 cpu_model = NULL;
2340 initrd_filename = NULL;
2341 ram_size = 0;
2342 snapshot = 0;
2343 kernel_filename = NULL;
2344 kernel_cmdline = "";
2345 cyls = heads = secs = 0;
2346 translation = BIOS_ATA_TRANSLATION_AUTO;
2348 for (i = 0; i < MAX_NODES; i++) {
2349 node_mem[i] = 0;
2350 node_cpumask[i] = 0;
2353 assigned_devices_index = 0;
2355 nb_numa_nodes = 0;
2356 nb_nics = 0;
2358 tb_size = 0;
2359 autostart= 1;
2361 /* first pass of option parsing */
2362 optind = 1;
2363 while (optind < argc) {
2364 if (argv[optind][0] != '-') {
2365 /* disk image */
2366 optind++;
2367 continue;
2368 } else {
2369 const QEMUOption *popt;
2371 popt = lookup_opt(argc, argv, &optarg, &optind);
2372 switch (popt->index) {
2373 case QEMU_OPTION_nodefconfig:
2374 defconfig=0;
2375 break;
2380 if (defconfig) {
2381 int ret;
2383 ret = qemu_read_config_file(CONFIG_QEMU_CONFDIR "/qemu.conf");
2384 if (ret < 0 && ret != -ENOENT) {
2385 exit(1);
2388 ret = qemu_read_config_file(arch_config_name);
2389 if (ret < 0 && ret != -ENOENT) {
2390 exit(1);
2393 cpudef_init();
2395 /* second pass of option parsing */
2396 optind = 1;
2397 for(;;) {
2398 if (optind >= argc)
2399 break;
2400 if (argv[optind][0] != '-') {
2401 hda_opts = drive_add(argv[optind++], HD_ALIAS, 0);
2402 } else {
2403 const QEMUOption *popt;
2405 popt = lookup_opt(argc, argv, &optarg, &optind);
2406 if (!(popt->arch_mask & arch_type)) {
2407 printf("Option %s not supported for this target\n", popt->name);
2408 exit(1);
2410 switch(popt->index) {
2411 case QEMU_OPTION_M:
2412 machine = find_machine(optarg);
2413 if (!machine) {
2414 QEMUMachine *m;
2415 printf("Supported machines are:\n");
2416 for(m = first_machine; m != NULL; m = m->next) {
2417 if (m->alias)
2418 printf("%-10s %s (alias of %s)\n",
2419 m->alias, m->desc, m->name);
2420 printf("%-10s %s%s\n",
2421 m->name, m->desc,
2422 m->is_default ? " (default)" : "");
2424 exit(*optarg != '?');
2426 break;
2427 case QEMU_OPTION_cpu:
2428 /* hw initialization will check this */
2429 if (*optarg == '?') {
2430 list_cpus(stdout, &fprintf, optarg);
2431 exit(0);
2432 } else {
2433 cpu_model = optarg;
2435 break;
2436 case QEMU_OPTION_initrd:
2437 initrd_filename = optarg;
2438 break;
2439 case QEMU_OPTION_hda:
2440 if (cyls == 0)
2441 hda_opts = drive_add(optarg, HD_ALIAS, 0);
2442 else
2443 hda_opts = drive_add(optarg, HD_ALIAS
2444 ",cyls=%d,heads=%d,secs=%d%s",
2445 0, cyls, heads, secs,
2446 translation == BIOS_ATA_TRANSLATION_LBA ?
2447 ",trans=lba" :
2448 translation == BIOS_ATA_TRANSLATION_NONE ?
2449 ",trans=none" : "");
2450 break;
2451 case QEMU_OPTION_hdb:
2452 case QEMU_OPTION_hdc:
2453 case QEMU_OPTION_hdd:
2454 drive_add(optarg, HD_ALIAS, popt->index - QEMU_OPTION_hda);
2455 break;
2456 case QEMU_OPTION_drive:
2457 drive_add(NULL, "%s", optarg);
2458 break;
2459 case QEMU_OPTION_set:
2460 if (qemu_set_option(optarg) != 0)
2461 exit(1);
2462 break;
2463 case QEMU_OPTION_global:
2464 if (qemu_global_option(optarg) != 0)
2465 exit(1);
2466 break;
2467 case QEMU_OPTION_mtdblock:
2468 drive_add(optarg, MTD_ALIAS);
2469 break;
2470 case QEMU_OPTION_sd:
2471 drive_add(optarg, SD_ALIAS);
2472 break;
2473 case QEMU_OPTION_pflash:
2474 drive_add(optarg, PFLASH_ALIAS);
2475 break;
2476 case QEMU_OPTION_snapshot:
2477 snapshot = 1;
2478 break;
2479 case QEMU_OPTION_hdachs:
2481 const char *p;
2482 p = optarg;
2483 cyls = strtol(p, (char **)&p, 0);
2484 if (cyls < 1 || cyls > 16383)
2485 goto chs_fail;
2486 if (*p != ',')
2487 goto chs_fail;
2488 p++;
2489 heads = strtol(p, (char **)&p, 0);
2490 if (heads < 1 || heads > 16)
2491 goto chs_fail;
2492 if (*p != ',')
2493 goto chs_fail;
2494 p++;
2495 secs = strtol(p, (char **)&p, 0);
2496 if (secs < 1 || secs > 63)
2497 goto chs_fail;
2498 if (*p == ',') {
2499 p++;
2500 if (!strcmp(p, "none"))
2501 translation = BIOS_ATA_TRANSLATION_NONE;
2502 else if (!strcmp(p, "lba"))
2503 translation = BIOS_ATA_TRANSLATION_LBA;
2504 else if (!strcmp(p, "auto"))
2505 translation = BIOS_ATA_TRANSLATION_AUTO;
2506 else
2507 goto chs_fail;
2508 } else if (*p != '\0') {
2509 chs_fail:
2510 fprintf(stderr, "qemu: invalid physical CHS format\n");
2511 exit(1);
2513 if (hda_opts != NULL) {
2514 char num[16];
2515 snprintf(num, sizeof(num), "%d", cyls);
2516 qemu_opt_set(hda_opts, "cyls", num);
2517 snprintf(num, sizeof(num), "%d", heads);
2518 qemu_opt_set(hda_opts, "heads", num);
2519 snprintf(num, sizeof(num), "%d", secs);
2520 qemu_opt_set(hda_opts, "secs", num);
2521 if (translation == BIOS_ATA_TRANSLATION_LBA)
2522 qemu_opt_set(hda_opts, "trans", "lba");
2523 if (translation == BIOS_ATA_TRANSLATION_NONE)
2524 qemu_opt_set(hda_opts, "trans", "none");
2527 break;
2528 case QEMU_OPTION_numa:
2529 if (nb_numa_nodes >= MAX_NODES) {
2530 fprintf(stderr, "qemu: too many NUMA nodes\n");
2531 exit(1);
2533 numa_add(optarg);
2534 break;
2535 case QEMU_OPTION_nographic:
2536 display_type = DT_NOGRAPHIC;
2537 break;
2538 #ifdef CONFIG_CURSES
2539 case QEMU_OPTION_curses:
2540 display_type = DT_CURSES;
2541 break;
2542 #endif
2543 case QEMU_OPTION_portrait:
2544 graphic_rotate = 1;
2545 break;
2546 case QEMU_OPTION_kernel:
2547 kernel_filename = optarg;
2548 break;
2549 case QEMU_OPTION_append:
2550 kernel_cmdline = optarg;
2551 break;
2552 case QEMU_OPTION_cdrom:
2553 drive_add(optarg, CDROM_ALIAS);
2554 break;
2555 case QEMU_OPTION_boot:
2557 static const char * const params[] = {
2558 "order", "once", "menu", NULL
2560 char buf[sizeof(boot_devices)];
2561 char *standard_boot_devices;
2562 int legacy = 0;
2564 if (!strchr(optarg, '=')) {
2565 legacy = 1;
2566 pstrcpy(buf, sizeof(buf), optarg);
2567 } else if (check_params(buf, sizeof(buf), params, optarg) < 0) {
2568 fprintf(stderr,
2569 "qemu: unknown boot parameter '%s' in '%s'\n",
2570 buf, optarg);
2571 exit(1);
2574 if (legacy ||
2575 get_param_value(buf, sizeof(buf), "order", optarg)) {
2576 validate_bootdevices(buf);
2577 pstrcpy(boot_devices, sizeof(boot_devices), buf);
2579 if (!legacy) {
2580 if (get_param_value(buf, sizeof(buf),
2581 "once", optarg)) {
2582 validate_bootdevices(buf);
2583 standard_boot_devices = qemu_strdup(boot_devices);
2584 pstrcpy(boot_devices, sizeof(boot_devices), buf);
2585 qemu_register_reset(restore_boot_devices,
2586 standard_boot_devices);
2588 if (get_param_value(buf, sizeof(buf),
2589 "menu", optarg)) {
2590 if (!strcmp(buf, "on")) {
2591 boot_menu = 1;
2592 } else if (!strcmp(buf, "off")) {
2593 boot_menu = 0;
2594 } else {
2595 fprintf(stderr,
2596 "qemu: invalid option value '%s'\n",
2597 buf);
2598 exit(1);
2603 break;
2604 case QEMU_OPTION_fda:
2605 case QEMU_OPTION_fdb:
2606 drive_add(optarg, FD_ALIAS, popt->index - QEMU_OPTION_fda);
2607 break;
2608 case QEMU_OPTION_no_fd_bootchk:
2609 fd_bootchk = 0;
2610 break;
2611 case QEMU_OPTION_netdev:
2612 if (net_client_parse(&qemu_netdev_opts, optarg) == -1) {
2613 exit(1);
2615 break;
2616 case QEMU_OPTION_net:
2617 if (net_client_parse(&qemu_net_opts, optarg) == -1) {
2618 exit(1);
2620 break;
2621 #ifdef CONFIG_SLIRP
2622 case QEMU_OPTION_tftp:
2623 legacy_tftp_prefix = optarg;
2624 break;
2625 case QEMU_OPTION_bootp:
2626 legacy_bootp_filename = optarg;
2627 break;
2628 case QEMU_OPTION_redir:
2629 if (net_slirp_redir(optarg) < 0)
2630 exit(1);
2631 break;
2632 #endif
2633 case QEMU_OPTION_bt:
2634 add_device_config(DEV_BT, optarg);
2635 break;
2636 case QEMU_OPTION_audio_help:
2637 if (!(audio_available())) {
2638 printf("Option %s not supported for this target\n", popt->name);
2639 exit(1);
2641 AUD_help ();
2642 exit (0);
2643 break;
2644 case QEMU_OPTION_soundhw:
2645 if (!(audio_available())) {
2646 printf("Option %s not supported for this target\n", popt->name);
2647 exit(1);
2649 select_soundhw (optarg);
2650 break;
2651 case QEMU_OPTION_h:
2652 help(0);
2653 break;
2654 case QEMU_OPTION_version:
2655 version();
2656 exit(0);
2657 break;
2658 case QEMU_OPTION_m: {
2659 uint64_t value;
2660 char *ptr;
2662 value = strtoul(optarg, &ptr, 10);
2663 switch (*ptr) {
2664 case 0: case 'M': case 'm':
2665 value <<= 20;
2666 break;
2667 case 'G': case 'g':
2668 value <<= 30;
2669 break;
2670 default:
2671 fprintf(stderr, "qemu: invalid ram size: %s\n", optarg);
2672 exit(1);
2675 /* On 32-bit hosts, QEMU is limited by virtual address space */
2676 if (value > (2047 << 20) && HOST_LONG_BITS == 32) {
2677 fprintf(stderr, "qemu: at most 2047 MB RAM can be simulated\n");
2678 exit(1);
2680 if (value != (uint64_t)(ram_addr_t)value) {
2681 fprintf(stderr, "qemu: ram size too large\n");
2682 exit(1);
2684 ram_size = value;
2685 break;
2687 case QEMU_OPTION_mempath:
2688 mem_path = optarg;
2689 break;
2690 #ifdef MAP_POPULATE
2691 case QEMU_OPTION_mem_prealloc:
2692 mem_prealloc = 1;
2693 break;
2694 #endif
2695 case QEMU_OPTION_d:
2696 set_cpu_log(optarg);
2697 break;
2698 case QEMU_OPTION_s:
2699 gdbstub_dev = "tcp::" DEFAULT_GDBSTUB_PORT;
2700 break;
2701 case QEMU_OPTION_gdb:
2702 gdbstub_dev = optarg;
2703 break;
2704 case QEMU_OPTION_L:
2705 data_dir = optarg;
2706 break;
2707 case QEMU_OPTION_bios:
2708 bios_name = optarg;
2709 break;
2710 case QEMU_OPTION_singlestep:
2711 singlestep = 1;
2712 break;
2713 case QEMU_OPTION_S:
2714 autostart = 0;
2715 break;
2716 case QEMU_OPTION_k:
2717 keyboard_layout = optarg;
2718 break;
2719 case QEMU_OPTION_localtime:
2720 rtc_utc = 0;
2721 break;
2722 case QEMU_OPTION_vga:
2723 select_vgahw (optarg);
2724 break;
2725 case QEMU_OPTION_g:
2727 const char *p;
2728 int w, h, depth;
2729 p = optarg;
2730 w = strtol(p, (char **)&p, 10);
2731 if (w <= 0) {
2732 graphic_error:
2733 fprintf(stderr, "qemu: invalid resolution or depth\n");
2734 exit(1);
2736 if (*p != 'x')
2737 goto graphic_error;
2738 p++;
2739 h = strtol(p, (char **)&p, 10);
2740 if (h <= 0)
2741 goto graphic_error;
2742 if (*p == 'x') {
2743 p++;
2744 depth = strtol(p, (char **)&p, 10);
2745 if (depth != 8 && depth != 15 && depth != 16 &&
2746 depth != 24 && depth != 32)
2747 goto graphic_error;
2748 } else if (*p == '\0') {
2749 depth = graphic_depth;
2750 } else {
2751 goto graphic_error;
2754 graphic_width = w;
2755 graphic_height = h;
2756 graphic_depth = depth;
2758 break;
2759 case QEMU_OPTION_echr:
2761 char *r;
2762 term_escape_char = strtol(optarg, &r, 0);
2763 if (r == optarg)
2764 printf("Bad argument to echr\n");
2765 break;
2767 case QEMU_OPTION_monitor:
2768 monitor_parse(optarg, "readline");
2769 default_monitor = 0;
2770 break;
2771 case QEMU_OPTION_qmp:
2772 monitor_parse(optarg, "control");
2773 default_monitor = 0;
2774 break;
2775 case QEMU_OPTION_mon:
2776 opts = qemu_opts_parse(&qemu_mon_opts, optarg, 1);
2777 if (!opts) {
2778 exit(1);
2780 default_monitor = 0;
2781 break;
2782 case QEMU_OPTION_chardev:
2783 opts = qemu_opts_parse(&qemu_chardev_opts, optarg, 1);
2784 if (!opts) {
2785 exit(1);
2787 break;
2788 #ifdef CONFIG_LINUX
2789 case QEMU_OPTION_fsdev:
2790 opts = qemu_opts_parse(&qemu_fsdev_opts, optarg, 1);
2791 if (!opts) {
2792 fprintf(stderr, "parse error: %s\n", optarg);
2793 exit(1);
2795 break;
2796 case QEMU_OPTION_virtfs: {
2797 char *arg_fsdev = NULL;
2798 char *arg_9p = NULL;
2799 int len = 0;
2801 opts = qemu_opts_parse(&qemu_virtfs_opts, optarg, 1);
2802 if (!opts) {
2803 fprintf(stderr, "parse error: %s\n", optarg);
2804 exit(1);
2807 len = strlen(",id=,path=");
2808 len += strlen(qemu_opt_get(opts, "fstype"));
2809 len += strlen(qemu_opt_get(opts, "mount_tag"));
2810 len += strlen(qemu_opt_get(opts, "path"));
2811 arg_fsdev = qemu_malloc((len + 1) * sizeof(*arg_fsdev));
2813 if (!arg_fsdev) {
2814 fprintf(stderr, "No memory to parse -fsdev for %s\n",
2815 optarg);
2816 exit(1);
2819 sprintf(arg_fsdev, "%s,id=%s,path=%s",
2820 qemu_opt_get(opts, "fstype"),
2821 qemu_opt_get(opts, "mount_tag"),
2822 qemu_opt_get(opts, "path"));
2824 len = strlen("virtio-9p-pci,fsdev=,mount_tag=");
2825 len += 2*strlen(qemu_opt_get(opts, "mount_tag"));
2826 arg_9p = qemu_malloc((len + 1) * sizeof(*arg_9p));
2828 if (!arg_9p) {
2829 fprintf(stderr, "No memory to parse -device for %s\n",
2830 optarg);
2831 exit(1);
2834 sprintf(arg_9p, "virtio-9p-pci,fsdev=%s,mount_tag=%s",
2835 qemu_opt_get(opts, "mount_tag"),
2836 qemu_opt_get(opts, "mount_tag"));
2838 if (!qemu_opts_parse(&qemu_fsdev_opts, arg_fsdev, 1)) {
2839 fprintf(stderr, "parse error [fsdev]: %s\n", optarg);
2840 exit(1);
2843 if (!qemu_opts_parse(&qemu_device_opts, arg_9p, 1)) {
2844 fprintf(stderr, "parse error [device]: %s\n", optarg);
2845 exit(1);
2848 qemu_free(arg_fsdev);
2849 qemu_free(arg_9p);
2850 break;
2852 #endif
2853 case QEMU_OPTION_serial:
2854 add_device_config(DEV_SERIAL, optarg);
2855 default_serial = 0;
2856 if (strncmp(optarg, "mon:", 4) == 0) {
2857 default_monitor = 0;
2859 break;
2860 case QEMU_OPTION_watchdog:
2861 if (watchdog) {
2862 fprintf(stderr,
2863 "qemu: only one watchdog option may be given\n");
2864 return 1;
2866 watchdog = optarg;
2867 break;
2868 case QEMU_OPTION_watchdog_action:
2869 if (select_watchdog_action(optarg) == -1) {
2870 fprintf(stderr, "Unknown -watchdog-action parameter\n");
2871 exit(1);
2873 break;
2874 case QEMU_OPTION_virtiocon:
2875 add_device_config(DEV_VIRTCON, optarg);
2876 default_virtcon = 0;
2877 if (strncmp(optarg, "mon:", 4) == 0) {
2878 default_monitor = 0;
2880 break;
2881 case QEMU_OPTION_parallel:
2882 add_device_config(DEV_PARALLEL, optarg);
2883 default_parallel = 0;
2884 if (strncmp(optarg, "mon:", 4) == 0) {
2885 default_monitor = 0;
2887 break;
2888 case QEMU_OPTION_debugcon:
2889 add_device_config(DEV_DEBUGCON, optarg);
2890 break;
2891 case QEMU_OPTION_loadvm:
2892 loadvm = optarg;
2893 break;
2894 case QEMU_OPTION_full_screen:
2895 full_screen = 1;
2896 break;
2897 #ifdef CONFIG_SDL
2898 case QEMU_OPTION_no_frame:
2899 no_frame = 1;
2900 break;
2901 case QEMU_OPTION_alt_grab:
2902 alt_grab = 1;
2903 break;
2904 case QEMU_OPTION_ctrl_grab:
2905 ctrl_grab = 1;
2906 break;
2907 case QEMU_OPTION_no_quit:
2908 no_quit = 1;
2909 break;
2910 case QEMU_OPTION_sdl:
2911 display_type = DT_SDL;
2912 break;
2913 #endif
2914 case QEMU_OPTION_pidfile:
2915 pid_file = optarg;
2916 break;
2917 case QEMU_OPTION_win2k_hack:
2918 win2k_install_hack = 1;
2919 break;
2920 case QEMU_OPTION_rtc_td_hack:
2921 rtc_td_hack = 1;
2922 break;
2923 case QEMU_OPTION_acpitable:
2924 do_acpitable_option(optarg);
2925 break;
2926 case QEMU_OPTION_smbios:
2927 do_smbios_option(optarg);
2928 break;
2929 #ifdef KVM_UPSTREAM
2930 case QEMU_OPTION_enable_kvm:
2931 kvm_allowed = 1;
2932 #endif
2933 break;
2934 case QEMU_OPTION_no_kvm:
2935 kvm_allowed = 0;
2936 break;
2937 case QEMU_OPTION_no_kvm_irqchip: {
2938 kvm_irqchip = 0;
2939 kvm_pit = 0;
2940 break;
2942 case QEMU_OPTION_no_kvm_pit: {
2943 kvm_pit = 0;
2944 break;
2946 case QEMU_OPTION_no_kvm_pit_reinjection: {
2947 kvm_pit_reinject = 0;
2948 break;
2950 case QEMU_OPTION_enable_nesting: {
2951 kvm_nested = 1;
2952 break;
2954 #if defined(TARGET_I386) || defined(TARGET_X86_64) || defined(TARGET_IA64) || defined(__linux__)
2955 case QEMU_OPTION_pcidevice:
2956 if (assigned_devices_index >= MAX_DEV_ASSIGN_CMDLINE) {
2957 fprintf(stderr, "Too many assigned devices\n");
2958 exit(1);
2960 assigned_devices[assigned_devices_index] = optarg;
2961 assigned_devices_index++;
2962 break;
2963 #endif
2964 case QEMU_OPTION_usb:
2965 usb_enabled = 1;
2966 break;
2967 case QEMU_OPTION_usbdevice:
2968 usb_enabled = 1;
2969 add_device_config(DEV_USB, optarg);
2970 break;
2971 case QEMU_OPTION_device:
2972 if (!qemu_opts_parse(&qemu_device_opts, optarg, 1)) {
2973 exit(1);
2975 break;
2976 case QEMU_OPTION_smp:
2977 smp_parse(optarg);
2978 if (smp_cpus < 1) {
2979 fprintf(stderr, "Invalid number of CPUs\n");
2980 exit(1);
2982 if (max_cpus < smp_cpus) {
2983 fprintf(stderr, "maxcpus must be equal to or greater than "
2984 "smp\n");
2985 exit(1);
2987 if (max_cpus > 255) {
2988 fprintf(stderr, "Unsupported number of maxcpus\n");
2989 exit(1);
2991 break;
2992 case QEMU_OPTION_vnc:
2993 display_type = DT_VNC;
2994 vnc_display = optarg;
2995 break;
2996 case QEMU_OPTION_no_acpi:
2997 acpi_enabled = 0;
2998 break;
2999 case QEMU_OPTION_no_hpet:
3000 no_hpet = 1;
3001 break;
3002 case QEMU_OPTION_balloon:
3003 if (balloon_parse(optarg) < 0) {
3004 fprintf(stderr, "Unknown -balloon argument %s\n", optarg);
3005 exit(1);
3007 break;
3008 case QEMU_OPTION_no_reboot:
3009 no_reboot = 1;
3010 break;
3011 case QEMU_OPTION_no_shutdown:
3012 no_shutdown = 1;
3013 break;
3014 case QEMU_OPTION_show_cursor:
3015 cursor_hide = 0;
3016 break;
3017 case QEMU_OPTION_uuid:
3018 if(qemu_uuid_parse(optarg, qemu_uuid) < 0) {
3019 fprintf(stderr, "Fail to parse UUID string."
3020 " Wrong format.\n");
3021 exit(1);
3023 break;
3024 case QEMU_OPTION_option_rom:
3025 if (nb_option_roms >= MAX_OPTION_ROMS) {
3026 fprintf(stderr, "Too many option ROMs\n");
3027 exit(1);
3029 option_rom[nb_option_roms] = optarg;
3030 nb_option_roms++;
3031 break;
3032 case QEMU_OPTION_semihosting:
3033 semihosting_enabled = 1;
3034 break;
3035 case QEMU_OPTION_tdf:
3036 time_drift_fix = 1;
3037 break;
3038 case QEMU_OPTION_kvm_shadow_memory:
3039 kvm_shadow_memory = (int64_t)atoi(optarg) * 1024 * 1024 / 4096;
3040 break;
3041 case QEMU_OPTION_name:
3042 qemu_name = qemu_strdup(optarg);
3044 char *p = strchr(qemu_name, ',');
3045 if (p != NULL) {
3046 *p++ = 0;
3047 if (strncmp(p, "process=", 8)) {
3048 fprintf(stderr, "Unknown subargument %s to -name", p);
3049 exit(1);
3051 p += 8;
3052 os_set_proc_name(p);
3055 break;
3056 case QEMU_OPTION_prom_env:
3057 if (nb_prom_envs >= MAX_PROM_ENVS) {
3058 fprintf(stderr, "Too many prom variables\n");
3059 exit(1);
3061 prom_envs[nb_prom_envs] = optarg;
3062 nb_prom_envs++;
3063 break;
3064 case QEMU_OPTION_old_param:
3065 old_param = 1;
3066 break;
3067 case QEMU_OPTION_clock:
3068 configure_alarms(optarg);
3069 break;
3070 case QEMU_OPTION_startdate:
3071 configure_rtc_date_offset(optarg, 1);
3072 break;
3073 case QEMU_OPTION_rtc:
3074 opts = qemu_opts_parse(&qemu_rtc_opts, optarg, 0);
3075 if (!opts) {
3076 exit(1);
3078 configure_rtc(opts);
3079 break;
3080 case QEMU_OPTION_tb_size:
3081 tb_size = strtol(optarg, NULL, 0);
3082 if (tb_size < 0)
3083 tb_size = 0;
3084 break;
3085 case QEMU_OPTION_icount:
3086 icount_option = optarg;
3087 break;
3088 case QEMU_OPTION_incoming:
3089 incoming = optarg;
3090 break;
3091 case QEMU_OPTION_nodefaults:
3092 default_serial = 0;
3093 default_parallel = 0;
3094 default_virtcon = 0;
3095 default_monitor = 0;
3096 default_vga = 0;
3097 default_net = 0;
3098 default_floppy = 0;
3099 default_cdrom = 0;
3100 default_sdcard = 0;
3101 break;
3102 #ifndef _WIN32
3103 case QEMU_OPTION_nvram:
3104 nvram = optarg;
3105 break;
3106 #endif
3107 case QEMU_OPTION_xen_domid:
3108 if (!(xen_available())) {
3109 printf("Option %s not supported for this target\n", popt->name);
3110 exit(1);
3112 xen_domid = atoi(optarg);
3113 break;
3114 case QEMU_OPTION_xen_create:
3115 if (!(xen_available())) {
3116 printf("Option %s not supported for this target\n", popt->name);
3117 exit(1);
3119 xen_mode = XEN_CREATE;
3120 break;
3121 case QEMU_OPTION_xen_attach:
3122 if (!(xen_available())) {
3123 printf("Option %s not supported for this target\n", popt->name);
3124 exit(1);
3126 xen_mode = XEN_ATTACH;
3127 break;
3128 case QEMU_OPTION_readconfig:
3130 int ret = qemu_read_config_file(optarg);
3131 if (ret < 0) {
3132 fprintf(stderr, "read config %s: %s\n", optarg,
3133 strerror(-ret));
3134 exit(1);
3136 break;
3138 case QEMU_OPTION_writeconfig:
3140 FILE *fp;
3141 if (strcmp(optarg, "-") == 0) {
3142 fp = stdout;
3143 } else {
3144 fp = fopen(optarg, "w");
3145 if (fp == NULL) {
3146 fprintf(stderr, "open %s: %s\n", optarg, strerror(errno));
3147 exit(1);
3150 qemu_config_write(fp);
3151 fclose(fp);
3152 break;
3154 default:
3155 os_parse_cmd_args(popt->index, optarg);
3159 loc_set_none();
3161 /* If no data_dir is specified then try to find it relative to the
3162 executable path. */
3163 if (!data_dir) {
3164 data_dir = os_find_datadir(argv[0]);
3166 /* If all else fails use the install patch specified when building. */
3167 if (!data_dir) {
3168 data_dir = CONFIG_QEMU_DATADIR;
3172 * Default to max_cpus = smp_cpus, in case the user doesn't
3173 * specify a max_cpus value.
3175 if (!max_cpus)
3176 max_cpus = smp_cpus;
3178 machine->max_cpus = machine->max_cpus ?: 1; /* Default to UP */
3179 if (smp_cpus > machine->max_cpus) {
3180 fprintf(stderr, "Number of SMP cpus requested (%d), exceeds max cpus "
3181 "supported by machine `%s' (%d)\n", smp_cpus, machine->name,
3182 machine->max_cpus);
3183 exit(1);
3186 qemu_opts_foreach(&qemu_device_opts, default_driver_check, NULL, 0);
3187 qemu_opts_foreach(&qemu_global_opts, default_driver_check, NULL, 0);
3189 if (machine->no_serial) {
3190 default_serial = 0;
3192 if (machine->no_parallel) {
3193 default_parallel = 0;
3195 if (!machine->use_virtcon) {
3196 default_virtcon = 0;
3198 if (machine->no_vga) {
3199 default_vga = 0;
3201 if (machine->no_floppy) {
3202 default_floppy = 0;
3204 if (machine->no_cdrom) {
3205 default_cdrom = 0;
3207 if (machine->no_sdcard) {
3208 default_sdcard = 0;
3211 if (display_type == DT_NOGRAPHIC) {
3212 if (default_parallel)
3213 add_device_config(DEV_PARALLEL, "null");
3214 if (default_serial && default_monitor) {
3215 add_device_config(DEV_SERIAL, "mon:stdio");
3216 } else if (default_virtcon && default_monitor) {
3217 add_device_config(DEV_VIRTCON, "mon:stdio");
3218 } else {
3219 if (default_serial)
3220 add_device_config(DEV_SERIAL, "stdio");
3221 if (default_virtcon)
3222 add_device_config(DEV_VIRTCON, "stdio");
3223 if (default_monitor)
3224 monitor_parse("stdio", "readline");
3226 } else {
3227 if (default_serial)
3228 add_device_config(DEV_SERIAL, "vc:80Cx24C");
3229 if (default_parallel)
3230 add_device_config(DEV_PARALLEL, "vc:80Cx24C");
3231 if (default_monitor)
3232 monitor_parse("vc:80Cx24C", "readline");
3233 if (default_virtcon)
3234 add_device_config(DEV_VIRTCON, "vc:80Cx24C");
3236 if (default_vga)
3237 vga_interface_type = VGA_CIRRUS;
3239 socket_init();
3241 if (qemu_opts_foreach(&qemu_chardev_opts, chardev_init_func, NULL, 1) != 0)
3242 exit(1);
3243 #ifdef CONFIG_LINUX
3244 if (qemu_opts_foreach(&qemu_fsdev_opts, fsdev_init_func, NULL, 1) != 0) {
3245 exit(1);
3247 #endif
3249 os_daemonize();
3251 if (pid_file && qemu_create_pidfile(pid_file) != 0) {
3252 os_pidfile_error();
3253 exit(1);
3256 if (kvm_allowed) {
3257 int ret = kvm_init(smp_cpus);
3258 if (ret < 0) {
3259 #if defined(KVM_UPSTREAM) || defined(CONFIG_NO_CPU_EMULATION)
3260 if (!kvm_available()) {
3261 printf("KVM not supported for this target\n");
3262 } else {
3263 fprintf(stderr, "failed to initialize KVM: %s\n", strerror(-ret));
3265 exit(1);
3266 #endif
3267 #ifdef CONFIG_KVM
3268 fprintf(stderr, "Could not initialize KVM, will disable KVM support\n");
3269 kvm_allowed = 0;
3270 #endif
3274 if (qemu_init_main_loop()) {
3275 fprintf(stderr, "qemu_init_main_loop failed\n");
3276 exit(1);
3278 linux_boot = (kernel_filename != NULL);
3280 if (!linux_boot && *kernel_cmdline != '\0') {
3281 fprintf(stderr, "-append only allowed with -kernel option\n");
3282 exit(1);
3285 if (!linux_boot && initrd_filename != NULL) {
3286 fprintf(stderr, "-initrd only allowed with -kernel option\n");
3287 exit(1);
3290 os_set_line_buffering();
3292 if (init_timer_alarm() < 0) {
3293 fprintf(stderr, "could not initialize alarm timer\n");
3294 exit(1);
3296 configure_icount(icount_option);
3298 if (net_init_clients() < 0) {
3299 exit(1);
3302 /* init the bluetooth world */
3303 if (foreach_device_config(DEV_BT, bt_parse))
3304 exit(1);
3306 /* init the memory */
3307 if (ram_size == 0)
3308 ram_size = DEFAULT_RAM_SIZE * 1024 * 1024;
3310 /* init the dynamic translator */
3311 cpu_exec_init_all(tb_size * 1024 * 1024);
3313 bdrv_init_with_whitelist();
3315 blk_mig_init();
3317 if (default_cdrom) {
3318 /* we always create the cdrom drive, even if no disk is there */
3319 drive_add(NULL, CDROM_ALIAS);
3322 if (default_floppy) {
3323 /* we always create at least one floppy */
3324 drive_add(NULL, FD_ALIAS, 0);
3327 if (default_sdcard) {
3328 /* we always create one sd slot, even if no card is in it */
3329 drive_add(NULL, SD_ALIAS);
3332 /* open the virtual block devices */
3333 if (snapshot)
3334 qemu_opts_foreach(&qemu_drive_opts, drive_enable_snapshot, NULL, 0);
3335 if (qemu_opts_foreach(&qemu_drive_opts, drive_init_func, machine, 1) != 0)
3336 exit(1);
3338 register_savevm_live("ram", 0, 3, NULL, ram_save_live, NULL,
3339 ram_load, NULL);
3341 if (nb_numa_nodes > 0) {
3342 int i;
3344 if (nb_numa_nodes > smp_cpus) {
3345 nb_numa_nodes = smp_cpus;
3348 /* If no memory size if given for any node, assume the default case
3349 * and distribute the available memory equally across all nodes
3351 for (i = 0; i < nb_numa_nodes; i++) {
3352 if (node_mem[i] != 0)
3353 break;
3355 if (i == nb_numa_nodes) {
3356 uint64_t usedmem = 0;
3358 /* On Linux, the each node's border has to be 8MB aligned,
3359 * the final node gets the rest.
3361 for (i = 0; i < nb_numa_nodes - 1; i++) {
3362 node_mem[i] = (ram_size / nb_numa_nodes) & ~((1 << 23UL) - 1);
3363 usedmem += node_mem[i];
3365 node_mem[i] = ram_size - usedmem;
3368 for (i = 0; i < nb_numa_nodes; i++) {
3369 if (node_cpumask[i] != 0)
3370 break;
3372 /* assigning the VCPUs round-robin is easier to implement, guest OSes
3373 * must cope with this anyway, because there are BIOSes out there in
3374 * real machines which also use this scheme.
3376 if (i == nb_numa_nodes) {
3377 for (i = 0; i < smp_cpus; i++) {
3378 node_cpumask[i % nb_numa_nodes] |= 1 << i;
3383 if (qemu_opts_foreach(&qemu_mon_opts, mon_init_func, NULL, 1) != 0) {
3384 exit(1);
3387 if (foreach_device_config(DEV_SERIAL, serial_parse) < 0)
3388 exit(1);
3389 if (foreach_device_config(DEV_PARALLEL, parallel_parse) < 0)
3390 exit(1);
3391 if (foreach_device_config(DEV_VIRTCON, virtcon_parse) < 0)
3392 exit(1);
3393 if (foreach_device_config(DEV_DEBUGCON, debugcon_parse) < 0)
3394 exit(1);
3396 module_call_init(MODULE_INIT_DEVICE);
3398 if (qemu_opts_foreach(&qemu_device_opts, device_help_func, NULL, 0) != 0)
3399 exit(0);
3401 if (watchdog) {
3402 i = select_watchdog(watchdog);
3403 if (i > 0)
3404 exit (i == 1 ? 1 : 0);
3407 if (machine->compat_props) {
3408 qdev_prop_register_global_list(machine->compat_props);
3410 qemu_add_globals();
3412 machine->init(ram_size, boot_devices,
3413 kernel_filename, kernel_cmdline, initrd_filename, cpu_model);
3415 cpu_synchronize_all_post_init();
3417 /* must be after terminal init, SDL library changes signal handlers */
3418 os_setup_signal_handling();
3420 set_numa_modes();
3422 current_machine = machine;
3424 /* init USB devices */
3425 if (usb_enabled) {
3426 if (foreach_device_config(DEV_USB, usb_parse) < 0)
3427 exit(1);
3430 /* init generic devices */
3431 if (qemu_opts_foreach(&qemu_device_opts, device_init_func, NULL, 1) != 0)
3432 exit(1);
3434 net_check_clients();
3436 /* just use the first displaystate for the moment */
3437 ds = get_displaystate();
3439 if (display_type == DT_DEFAULT) {
3440 #if defined(CONFIG_SDL) || defined(CONFIG_COCOA)
3441 display_type = DT_SDL;
3442 #else
3443 display_type = DT_VNC;
3444 vnc_display = "localhost:0,to=99";
3445 show_vnc_port = 1;
3446 #endif
3450 switch (display_type) {
3451 case DT_NOGRAPHIC:
3452 break;
3453 #if defined(CONFIG_CURSES)
3454 case DT_CURSES:
3455 curses_display_init(ds, full_screen);
3456 break;
3457 #endif
3458 #if defined(CONFIG_SDL)
3459 case DT_SDL:
3460 sdl_display_init(ds, full_screen, no_frame);
3461 break;
3462 #elif defined(CONFIG_COCOA)
3463 case DT_SDL:
3464 cocoa_display_init(ds, full_screen);
3465 break;
3466 #endif
3467 case DT_VNC:
3468 vnc_display_init(ds);
3469 if (vnc_display_open(ds, vnc_display) < 0)
3470 exit(1);
3472 if (show_vnc_port) {
3473 printf("VNC server running on `%s'\n", vnc_display_local_addr(ds));
3475 break;
3476 default:
3477 break;
3479 dpy_resize(ds);
3481 dcl = ds->listeners;
3482 while (dcl != NULL) {
3483 if (dcl->dpy_refresh != NULL) {
3484 ds->gui_timer = qemu_new_timer(rt_clock, gui_update, ds);
3485 qemu_mod_timer(ds->gui_timer, qemu_get_clock(rt_clock));
3486 break;
3488 dcl = dcl->next;
3491 if (display_type == DT_NOGRAPHIC || display_type == DT_VNC) {
3492 nographic_timer = qemu_new_timer(rt_clock, nographic_update, NULL);
3493 qemu_mod_timer(nographic_timer, qemu_get_clock(rt_clock));
3496 text_consoles_set_display(ds);
3498 if (gdbstub_dev && gdbserver_start(gdbstub_dev) < 0) {
3499 fprintf(stderr, "qemu: could not open gdbserver on device '%s'\n",
3500 gdbstub_dev);
3501 exit(1);
3504 qdev_machine_creation_done();
3506 if (rom_load_all() != 0) {
3507 fprintf(stderr, "rom loading failed\n");
3508 exit(1);
3511 qemu_system_reset();
3512 if (loadvm) {
3513 if (load_vmstate(loadvm) < 0) {
3514 autostart = 0;
3518 if (incoming) {
3519 qemu_start_incoming_migration(incoming);
3520 } else if (autostart) {
3521 vm_start();
3524 os_setup_post();
3526 main_loop();
3527 quit_timers();
3528 net_cleanup();
3530 return 0;