Merge commit '0766379d4c58c5c1edc6adc414bfb25fc979b083' into upstream-merge
[qemu-kvm/amd-iommu.git] / vl.c
blob4a9572c05be3797061f19adf9abdb058af6db96b
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>
62 #include <sys/prctl.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 /* See MySQL bug #7156 (http://bugs.mysql.com/bug.php?id=7156) for
81 discussion about Solaris header problems */
82 extern int madvise(caddr_t, size_t, int);
83 #endif
84 #endif
85 #endif
87 #if defined(__OpenBSD__)
88 #include <util.h>
89 #endif
91 #if defined(CONFIG_VDE)
92 #include <libvdeplug.h>
93 #endif
95 #ifdef _WIN32
96 #include <windows.h>
97 #endif
99 #ifdef CONFIG_SDL
100 #if defined(__APPLE__) || defined(main)
101 #include <SDL.h>
102 int qemu_main(int argc, char **argv, char **envp);
103 int main(int argc, char **argv)
105 return qemu_main(argc, argv, NULL);
107 #undef main
108 #define main qemu_main
109 #endif
110 #endif /* CONFIG_SDL */
112 #ifdef CONFIG_COCOA
113 #undef main
114 #define main qemu_main
115 #endif /* CONFIG_COCOA */
117 #include "hw/hw.h"
118 #include "hw/boards.h"
119 #include "hw/usb.h"
120 #include "hw/pcmcia.h"
121 #include "hw/pc.h"
122 #include "hw/isa.h"
123 #include "hw/baum.h"
124 #include "hw/bt.h"
125 #include "hw/watchdog.h"
126 #include "hw/smbios.h"
127 #include "hw/xen.h"
128 #include "hw/qdev.h"
129 #include "hw/loader.h"
130 #include "bt-host.h"
131 #include "net.h"
132 #include "net/slirp.h"
133 #include "monitor.h"
134 #include "console.h"
135 #include "sysemu.h"
136 #include "gdbstub.h"
137 #include "qemu-timer.h"
138 #include "qemu-char.h"
139 #include "cache-utils.h"
140 #include "block.h"
141 #include "block_int.h"
142 #include "block-migration.h"
143 #include "dma.h"
144 #include "audio/audio.h"
145 #include "migration.h"
146 #include "kvm.h"
147 #include "qemu-option.h"
148 #include "qemu-config.h"
149 #include "qemu-objects.h"
150 #include "qemu-options.h"
151 #include "hw/device-assignment.h"
152 #ifdef CONFIG_LINUX
153 #include "fsdev/qemu-fsdev.h"
154 #endif
156 #include "disas.h"
158 #include "qemu_socket.h"
160 #include "slirp/libslirp.h"
162 #include "qemu-queue.h"
163 #include "cpus.h"
164 #include "arch_init.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 /* Note: drives_table[MAX_DRIVES] is a dummy block driver if none available
176 to store the VM snapshots */
177 struct drivelist drives = QTAILQ_HEAD_INITIALIZER(drives);
178 struct driveoptlist driveopts = QTAILQ_HEAD_INITIALIZER(driveopts);
179 DriveInfo *extboot_drive = NULL;
180 enum vga_retrace_method vga_retrace_method = VGA_RETRACE_DUMB;
181 DisplayType display_type = DT_DEFAULT;
182 const char* keyboard_layout = NULL;
183 ram_addr_t ram_size;
184 const char *mem_path = NULL;
185 #ifdef MAP_POPULATE
186 int mem_prealloc = 0; /* force preallocation of physical target memory */
187 #endif
188 int nb_nics;
189 NICInfo nd_table[MAX_NICS];
190 int vm_running;
191 int autostart;
192 static int rtc_utc = 1;
193 static int rtc_date_offset = -1; /* -1 means no change */
194 QEMUClock *rtc_clock;
195 int vga_interface_type = VGA_NONE;
196 static int full_screen = 0;
197 #ifdef CONFIG_SDL
198 static int no_frame = 0;
199 #endif
200 int no_quit = 0;
201 CharDriverState *serial_hds[MAX_SERIAL_PORTS];
202 CharDriverState *parallel_hds[MAX_PARALLEL_PORTS];
203 CharDriverState *virtcon_hds[MAX_VIRTIO_CONSOLES];
204 int win2k_install_hack = 0;
205 int rtc_td_hack = 0;
206 int usb_enabled = 0;
207 int singlestep = 0;
208 const char *assigned_devices[MAX_DEV_ASSIGN_CMDLINE];
209 int assigned_devices_index;
210 int smp_cpus = 1;
211 int max_cpus = 0;
212 int smp_cores = 1;
213 int smp_threads = 1;
214 const char *vnc_display;
215 int acpi_enabled = 1;
216 #ifdef TARGET_I386
217 int no_hpet = 0;
218 #endif
219 int fd_bootchk = 1;
220 int no_reboot = 0;
221 int no_shutdown = 0;
222 int cursor_hide = 1;
223 int graphic_rotate = 0;
224 uint8_t irq0override = 1;
225 #ifndef _WIN32
226 int daemonize = 0;
227 #endif
228 const char *watchdog;
229 const char *option_rom[MAX_OPTION_ROMS];
230 int nb_option_roms;
231 int semihosting_enabled = 0;
232 int time_drift_fix = 0;
233 unsigned int kvm_shadow_memory = 0;
234 int old_param = 0;
235 const char *qemu_name;
236 int alt_grab = 0;
237 int ctrl_grab = 0;
238 unsigned int nb_prom_envs = 0;
239 const char *prom_envs[MAX_PROM_ENVS];
240 const char *nvram = NULL;
241 int boot_menu;
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 int kvm_allowed = 1;
255 uint32_t xen_domid;
256 enum xen_mode xen_mode = XEN_EMULATE;
258 static int default_serial = 1;
259 static int default_parallel = 1;
260 static int default_virtcon = 1;
261 static int default_monitor = 1;
262 static int default_vga = 1;
263 static int default_floppy = 1;
264 static int default_cdrom = 1;
265 static int default_sdcard = 1;
267 static struct {
268 const char *driver;
269 int *flag;
270 } default_list[] = {
271 { .driver = "isa-serial", .flag = &default_serial },
272 { .driver = "isa-parallel", .flag = &default_parallel },
273 { .driver = "isa-fdc", .flag = &default_floppy },
274 { .driver = "ide-drive", .flag = &default_cdrom },
275 { .driver = "virtio-serial-pci", .flag = &default_virtcon },
276 { .driver = "virtio-serial-s390", .flag = &default_virtcon },
277 { .driver = "virtio-serial", .flag = &default_virtcon },
278 { .driver = "VGA", .flag = &default_vga },
279 { .driver = "cirrus-vga", .flag = &default_vga },
280 { .driver = "vmware-svga", .flag = &default_vga },
283 static int default_driver_check(QemuOpts *opts, void *opaque)
285 const char *driver = qemu_opt_get(opts, "driver");
286 int i;
288 if (!driver)
289 return 0;
290 for (i = 0; i < ARRAY_SIZE(default_list); i++) {
291 if (strcmp(default_list[i].driver, driver) != 0)
292 continue;
293 *(default_list[i].flag) = 0;
295 return 0;
298 /***********************************************************/
300 static void set_proc_name(const char *s)
302 #if defined(__linux__) && defined(PR_SET_NAME)
303 char name[16];
304 if (!s)
305 return;
306 name[sizeof(name) - 1] = 0;
307 strncpy(name, s, sizeof(name));
308 /* Could rewrite argv[0] too, but that's a bit more complicated.
309 This simple way is enough for `top'. */
310 prctl(PR_SET_NAME, name);
311 #endif
314 /***********************************************************/
315 /* real time host monotonic timer */
317 /* compute with 96 bit intermediate result: (a*b)/c */
318 uint64_t muldiv64(uint64_t a, uint32_t b, uint32_t c)
320 union {
321 uint64_t ll;
322 struct {
323 #ifdef HOST_WORDS_BIGENDIAN
324 uint32_t high, low;
325 #else
326 uint32_t low, high;
327 #endif
328 } l;
329 } u, res;
330 uint64_t rl, rh;
332 u.ll = a;
333 rl = (uint64_t)u.l.low * (uint64_t)b;
334 rh = (uint64_t)u.l.high * (uint64_t)b;
335 rh += (rl >> 32);
336 res.l.high = rh / c;
337 res.l.low = (((rh % c) << 32) + (rl & 0xffffffff)) / c;
338 return res.ll;
341 /***********************************************************/
342 /* host time/date access */
343 void qemu_get_timedate(struct tm *tm, int offset)
345 time_t ti;
346 struct tm *ret;
348 time(&ti);
349 ti += offset;
350 if (rtc_date_offset == -1) {
351 if (rtc_utc)
352 ret = gmtime(&ti);
353 else
354 ret = localtime(&ti);
355 } else {
356 ti -= rtc_date_offset;
357 ret = gmtime(&ti);
360 memcpy(tm, ret, sizeof(struct tm));
363 int qemu_timedate_diff(struct tm *tm)
365 time_t seconds;
367 if (rtc_date_offset == -1)
368 if (rtc_utc)
369 seconds = mktimegm(tm);
370 else
371 seconds = mktime(tm);
372 else
373 seconds = mktimegm(tm) + rtc_date_offset;
375 return seconds - time(NULL);
378 void rtc_change_mon_event(struct tm *tm)
380 QObject *data;
382 data = qobject_from_jsonf("{ 'offset': %d }", qemu_timedate_diff(tm));
383 monitor_protocol_event(QEVENT_RTC_CHANGE, data);
384 qobject_decref(data);
387 static void configure_rtc_date_offset(const char *startdate, int legacy)
389 time_t rtc_start_date;
390 struct tm tm;
392 if (!strcmp(startdate, "now") && legacy) {
393 rtc_date_offset = -1;
394 } else {
395 if (sscanf(startdate, "%d-%d-%dT%d:%d:%d",
396 &tm.tm_year,
397 &tm.tm_mon,
398 &tm.tm_mday,
399 &tm.tm_hour,
400 &tm.tm_min,
401 &tm.tm_sec) == 6) {
402 /* OK */
403 } else if (sscanf(startdate, "%d-%d-%d",
404 &tm.tm_year,
405 &tm.tm_mon,
406 &tm.tm_mday) == 3) {
407 tm.tm_hour = 0;
408 tm.tm_min = 0;
409 tm.tm_sec = 0;
410 } else {
411 goto date_fail;
413 tm.tm_year -= 1900;
414 tm.tm_mon--;
415 rtc_start_date = mktimegm(&tm);
416 if (rtc_start_date == -1) {
417 date_fail:
418 fprintf(stderr, "Invalid date format. Valid formats are:\n"
419 "'2006-06-17T16:01:21' or '2006-06-17'\n");
420 exit(1);
422 rtc_date_offset = time(NULL) - rtc_start_date;
426 static void configure_rtc(QemuOpts *opts)
428 const char *value;
430 value = qemu_opt_get(opts, "base");
431 if (value) {
432 if (!strcmp(value, "utc")) {
433 rtc_utc = 1;
434 } else if (!strcmp(value, "localtime")) {
435 rtc_utc = 0;
436 } else {
437 configure_rtc_date_offset(value, 0);
440 value = qemu_opt_get(opts, "clock");
441 if (value) {
442 if (!strcmp(value, "host")) {
443 rtc_clock = host_clock;
444 } else if (!strcmp(value, "vm")) {
445 rtc_clock = vm_clock;
446 } else {
447 fprintf(stderr, "qemu: invalid option value '%s'\n", value);
448 exit(1);
451 value = qemu_opt_get(opts, "driftfix");
452 if (value) {
453 if (!strcmp(value, "slew")) {
454 rtc_td_hack = 1;
455 } else if (!strcmp(value, "none")) {
456 rtc_td_hack = 0;
457 } else {
458 fprintf(stderr, "qemu: invalid option value '%s'\n", value);
459 exit(1);
464 /***********************************************************/
465 /* Bluetooth support */
466 static int nb_hcis;
467 static int cur_hci;
468 static struct HCIInfo *hci_table[MAX_NICS];
470 static struct bt_vlan_s {
471 struct bt_scatternet_s net;
472 int id;
473 struct bt_vlan_s *next;
474 } *first_bt_vlan;
476 /* find or alloc a new bluetooth "VLAN" */
477 static struct bt_scatternet_s *qemu_find_bt_vlan(int id)
479 struct bt_vlan_s **pvlan, *vlan;
480 for (vlan = first_bt_vlan; vlan != NULL; vlan = vlan->next) {
481 if (vlan->id == id)
482 return &vlan->net;
484 vlan = qemu_mallocz(sizeof(struct bt_vlan_s));
485 vlan->id = id;
486 pvlan = &first_bt_vlan;
487 while (*pvlan != NULL)
488 pvlan = &(*pvlan)->next;
489 *pvlan = vlan;
490 return &vlan->net;
493 static void null_hci_send(struct HCIInfo *hci, const uint8_t *data, int len)
497 static int null_hci_addr_set(struct HCIInfo *hci, const uint8_t *bd_addr)
499 return -ENOTSUP;
502 static struct HCIInfo null_hci = {
503 .cmd_send = null_hci_send,
504 .sco_send = null_hci_send,
505 .acl_send = null_hci_send,
506 .bdaddr_set = null_hci_addr_set,
509 struct HCIInfo *qemu_next_hci(void)
511 if (cur_hci == nb_hcis)
512 return &null_hci;
514 return hci_table[cur_hci++];
517 static struct HCIInfo *hci_init(const char *str)
519 char *endp;
520 struct bt_scatternet_s *vlan = 0;
522 if (!strcmp(str, "null"))
523 /* null */
524 return &null_hci;
525 else if (!strncmp(str, "host", 4) && (str[4] == '\0' || str[4] == ':'))
526 /* host[:hciN] */
527 return bt_host_hci(str[4] ? str + 5 : "hci0");
528 else if (!strncmp(str, "hci", 3)) {
529 /* hci[,vlan=n] */
530 if (str[3]) {
531 if (!strncmp(str + 3, ",vlan=", 6)) {
532 vlan = qemu_find_bt_vlan(strtol(str + 9, &endp, 0));
533 if (*endp)
534 vlan = 0;
536 } else
537 vlan = qemu_find_bt_vlan(0);
538 if (vlan)
539 return bt_new_hci(vlan);
542 fprintf(stderr, "qemu: Unknown bluetooth HCI `%s'.\n", str);
544 return 0;
547 static int bt_hci_parse(const char *str)
549 struct HCIInfo *hci;
550 bdaddr_t bdaddr;
552 if (nb_hcis >= MAX_NICS) {
553 fprintf(stderr, "qemu: Too many bluetooth HCIs (max %i).\n", MAX_NICS);
554 return -1;
557 hci = hci_init(str);
558 if (!hci)
559 return -1;
561 bdaddr.b[0] = 0x52;
562 bdaddr.b[1] = 0x54;
563 bdaddr.b[2] = 0x00;
564 bdaddr.b[3] = 0x12;
565 bdaddr.b[4] = 0x34;
566 bdaddr.b[5] = 0x56 + nb_hcis;
567 hci->bdaddr_set(hci, bdaddr.b);
569 hci_table[nb_hcis++] = hci;
571 return 0;
574 static void bt_vhci_add(int vlan_id)
576 struct bt_scatternet_s *vlan = qemu_find_bt_vlan(vlan_id);
578 if (!vlan->slave)
579 fprintf(stderr, "qemu: warning: adding a VHCI to "
580 "an empty scatternet %i\n", vlan_id);
582 bt_vhci_init(bt_new_hci(vlan));
585 static struct bt_device_s *bt_device_add(const char *opt)
587 struct bt_scatternet_s *vlan;
588 int vlan_id = 0;
589 char *endp = strstr(opt, ",vlan=");
590 int len = (endp ? endp - opt : strlen(opt)) + 1;
591 char devname[10];
593 pstrcpy(devname, MIN(sizeof(devname), len), opt);
595 if (endp) {
596 vlan_id = strtol(endp + 6, &endp, 0);
597 if (*endp) {
598 fprintf(stderr, "qemu: unrecognised bluetooth vlan Id\n");
599 return 0;
603 vlan = qemu_find_bt_vlan(vlan_id);
605 if (!vlan->slave)
606 fprintf(stderr, "qemu: warning: adding a slave device to "
607 "an empty scatternet %i\n", vlan_id);
609 if (!strcmp(devname, "keyboard"))
610 return bt_keyboard_init(vlan);
612 fprintf(stderr, "qemu: unsupported bluetooth device `%s'\n", devname);
613 return 0;
616 static int bt_parse(const char *opt)
618 const char *endp, *p;
619 int vlan;
621 if (strstart(opt, "hci", &endp)) {
622 if (!*endp || *endp == ',') {
623 if (*endp)
624 if (!strstart(endp, ",vlan=", 0))
625 opt = endp + 1;
627 return bt_hci_parse(opt);
629 } else if (strstart(opt, "vhci", &endp)) {
630 if (!*endp || *endp == ',') {
631 if (*endp) {
632 if (strstart(endp, ",vlan=", &p)) {
633 vlan = strtol(p, (char **) &endp, 0);
634 if (*endp) {
635 fprintf(stderr, "qemu: bad scatternet '%s'\n", p);
636 return 1;
638 } else {
639 fprintf(stderr, "qemu: bad parameter '%s'\n", endp + 1);
640 return 1;
642 } else
643 vlan = 0;
645 bt_vhci_add(vlan);
646 return 0;
648 } else if (strstart(opt, "device:", &endp))
649 return !bt_device_add(endp);
651 fprintf(stderr, "qemu: bad bluetooth parameter '%s'\n", opt);
652 return 1;
655 /***********************************************************/
656 /* QEMU Block devices */
658 #define HD_ALIAS "index=%d,media=disk"
659 #define CDROM_ALIAS "index=2,media=cdrom"
660 #define FD_ALIAS "index=%d,if=floppy"
661 #define PFLASH_ALIAS "if=pflash"
662 #define MTD_ALIAS "if=mtd"
663 #define SD_ALIAS "index=0,if=sd"
665 QemuOpts *drive_add(const char *file, const char *fmt, ...)
667 va_list ap;
668 char optstr[1024];
669 QemuOpts *opts;
671 va_start(ap, fmt);
672 vsnprintf(optstr, sizeof(optstr), fmt, ap);
673 va_end(ap);
675 opts = qemu_opts_parse(&qemu_drive_opts, optstr, 0);
676 if (!opts) {
677 return NULL;
679 if (file)
680 qemu_opt_set(opts, "file", file);
681 return opts;
684 DriveInfo *drive_get(BlockInterfaceType type, int bus, int unit)
686 DriveInfo *dinfo;
688 /* seek interface, bus and unit */
690 QTAILQ_FOREACH(dinfo, &drives, next) {
691 if (dinfo->type == type &&
692 dinfo->bus == bus &&
693 dinfo->unit == unit)
694 return dinfo;
697 return NULL;
700 DriveInfo *drive_get_by_id(const char *id)
702 DriveInfo *dinfo;
704 QTAILQ_FOREACH(dinfo, &drives, next) {
705 if (strcmp(id, dinfo->id))
706 continue;
707 return dinfo;
709 return NULL;
712 int drive_get_max_bus(BlockInterfaceType type)
714 int max_bus;
715 DriveInfo *dinfo;
717 max_bus = -1;
718 QTAILQ_FOREACH(dinfo, &drives, next) {
719 if(dinfo->type == type &&
720 dinfo->bus > max_bus)
721 max_bus = dinfo->bus;
723 return max_bus;
726 const char *drive_get_serial(BlockDriverState *bdrv)
728 DriveInfo *dinfo;
730 QTAILQ_FOREACH(dinfo, &drives, next) {
731 if (dinfo->bdrv == bdrv)
732 return dinfo->serial;
735 return "\0";
738 BlockInterfaceErrorAction drive_get_on_error(
739 BlockDriverState *bdrv, int is_read)
741 DriveInfo *dinfo;
743 QTAILQ_FOREACH(dinfo, &drives, next) {
744 if (dinfo->bdrv == bdrv)
745 return is_read ? dinfo->on_read_error : dinfo->on_write_error;
748 return is_read ? BLOCK_ERR_REPORT : BLOCK_ERR_STOP_ENOSPC;
751 static void bdrv_format_print(void *opaque, const char *name)
753 fprintf(stderr, " %s", name);
756 void drive_uninit(DriveInfo *dinfo)
758 qemu_opts_del(dinfo->opts);
759 bdrv_delete(dinfo->bdrv);
760 QTAILQ_REMOVE(&drives, dinfo, next);
761 qemu_free(dinfo);
764 static int parse_block_error_action(const char *buf, int is_read)
766 if (!strcmp(buf, "ignore")) {
767 return BLOCK_ERR_IGNORE;
768 } else if (!is_read && !strcmp(buf, "enospc")) {
769 return BLOCK_ERR_STOP_ENOSPC;
770 } else if (!strcmp(buf, "stop")) {
771 return BLOCK_ERR_STOP_ANY;
772 } else if (!strcmp(buf, "report")) {
773 return BLOCK_ERR_REPORT;
774 } else {
775 fprintf(stderr, "qemu: '%s' invalid %s error action\n",
776 buf, is_read ? "read" : "write");
777 return -1;
781 DriveInfo *drive_init(QemuOpts *opts, void *opaque,
782 int *fatal_error)
784 const char *buf;
785 const char *file = NULL;
786 char devname[128];
787 const char *serial;
788 const char *mediastr = "";
789 BlockInterfaceType type;
790 enum { MEDIA_DISK, MEDIA_CDROM } media;
791 int bus_id, unit_id;
792 int cyls, heads, secs, translation;
793 BlockDriver *drv = NULL;
794 QEMUMachine *machine = opaque;
795 int max_devs;
796 int index;
797 int ro = 0;
798 int bdrv_flags = 0;
799 int on_read_error, on_write_error;
800 const char *devaddr;
801 DriveInfo *dinfo;
802 int is_extboot = 0;
803 int snapshot = 0;
805 *fatal_error = 1;
807 translation = BIOS_ATA_TRANSLATION_AUTO;
809 if (machine && machine->use_scsi) {
810 type = IF_SCSI;
811 max_devs = MAX_SCSI_DEVS;
812 pstrcpy(devname, sizeof(devname), "scsi");
813 } else {
814 type = IF_IDE;
815 max_devs = MAX_IDE_DEVS;
816 pstrcpy(devname, sizeof(devname), "ide");
818 media = MEDIA_DISK;
820 /* extract parameters */
821 bus_id = qemu_opt_get_number(opts, "bus", 0);
822 unit_id = qemu_opt_get_number(opts, "unit", -1);
823 index = qemu_opt_get_number(opts, "index", -1);
825 cyls = qemu_opt_get_number(opts, "cyls", 0);
826 heads = qemu_opt_get_number(opts, "heads", 0);
827 secs = qemu_opt_get_number(opts, "secs", 0);
829 snapshot = qemu_opt_get_bool(opts, "snapshot", 0);
830 ro = qemu_opt_get_bool(opts, "readonly", 0);
832 file = qemu_opt_get(opts, "file");
833 serial = qemu_opt_get(opts, "serial");
835 if ((buf = qemu_opt_get(opts, "if")) != NULL) {
836 pstrcpy(devname, sizeof(devname), buf);
837 if (!strcmp(buf, "ide")) {
838 type = IF_IDE;
839 max_devs = MAX_IDE_DEVS;
840 } else if (!strcmp(buf, "scsi")) {
841 type = IF_SCSI;
842 max_devs = MAX_SCSI_DEVS;
843 } else if (!strcmp(buf, "floppy")) {
844 type = IF_FLOPPY;
845 max_devs = 0;
846 } else if (!strcmp(buf, "pflash")) {
847 type = IF_PFLASH;
848 max_devs = 0;
849 } else if (!strcmp(buf, "mtd")) {
850 type = IF_MTD;
851 max_devs = 0;
852 } else if (!strcmp(buf, "sd")) {
853 type = IF_SD;
854 max_devs = 0;
855 } else if (!strcmp(buf, "virtio")) {
856 type = IF_VIRTIO;
857 max_devs = 0;
858 } else if (!strcmp(buf, "xen")) {
859 type = IF_XEN;
860 max_devs = 0;
861 } else if (!strcmp(buf, "none")) {
862 type = IF_NONE;
863 max_devs = 0;
864 } else {
865 fprintf(stderr, "qemu: unsupported bus type '%s'\n", buf);
866 return NULL;
870 if (cyls || heads || secs) {
871 if (cyls < 1 || (type == IF_IDE && cyls > 16383)) {
872 fprintf(stderr, "qemu: '%s' invalid physical cyls number\n", buf);
873 return NULL;
875 if (heads < 1 || (type == IF_IDE && heads > 16)) {
876 fprintf(stderr, "qemu: '%s' invalid physical heads number\n", buf);
877 return NULL;
879 if (secs < 1 || (type == IF_IDE && secs > 63)) {
880 fprintf(stderr, "qemu: '%s' invalid physical secs number\n", buf);
881 return NULL;
885 if ((buf = qemu_opt_get(opts, "trans")) != NULL) {
886 if (!cyls) {
887 fprintf(stderr,
888 "qemu: '%s' trans must be used with cyls,heads and secs\n",
889 buf);
890 return NULL;
892 if (!strcmp(buf, "none"))
893 translation = BIOS_ATA_TRANSLATION_NONE;
894 else if (!strcmp(buf, "lba"))
895 translation = BIOS_ATA_TRANSLATION_LBA;
896 else if (!strcmp(buf, "auto"))
897 translation = BIOS_ATA_TRANSLATION_AUTO;
898 else {
899 fprintf(stderr, "qemu: '%s' invalid translation type\n", buf);
900 return NULL;
904 if ((buf = qemu_opt_get(opts, "media")) != NULL) {
905 if (!strcmp(buf, "disk")) {
906 media = MEDIA_DISK;
907 } else if (!strcmp(buf, "cdrom")) {
908 if (cyls || secs || heads) {
909 fprintf(stderr,
910 "qemu: '%s' invalid physical CHS format\n", buf);
911 return NULL;
913 media = MEDIA_CDROM;
914 } else {
915 fprintf(stderr, "qemu: '%s' invalid media\n", buf);
916 return NULL;
920 if ((buf = qemu_opt_get(opts, "cache")) != NULL) {
921 if (!strcmp(buf, "off") || !strcmp(buf, "none")) {
922 bdrv_flags |= BDRV_O_NOCACHE;
923 } else if (!strcmp(buf, "writeback")) {
924 bdrv_flags |= BDRV_O_CACHE_WB;
925 } else if (!strcmp(buf, "unsafe")) {
926 bdrv_flags |= BDRV_O_CACHE_WB;
927 bdrv_flags |= BDRV_O_NO_FLUSH;
928 } else if (!strcmp(buf, "writethrough")) {
929 /* this is the default */
930 } else {
931 fprintf(stderr, "qemu: invalid cache option\n");
932 return NULL;
936 #ifdef CONFIG_LINUX_AIO
937 if ((buf = qemu_opt_get(opts, "aio")) != NULL) {
938 if (!strcmp(buf, "native")) {
939 bdrv_flags |= BDRV_O_NATIVE_AIO;
940 } else if (!strcmp(buf, "threads")) {
941 /* this is the default */
942 } else {
943 fprintf(stderr, "qemu: invalid aio option\n");
944 return NULL;
947 #endif
949 if ((buf = qemu_opt_get(opts, "format")) != NULL) {
950 if (strcmp(buf, "?") == 0) {
951 fprintf(stderr, "qemu: Supported formats:");
952 bdrv_iterate_format(bdrv_format_print, NULL);
953 fprintf(stderr, "\n");
954 return NULL;
956 drv = bdrv_find_whitelisted_format(buf);
957 if (!drv) {
958 fprintf(stderr, "qemu: '%s' invalid format\n", buf);
959 return NULL;
963 is_extboot = qemu_opt_get_bool(opts, "boot", 0);
964 if (is_extboot && extboot_drive) {
965 fprintf(stderr, "qemu: two bootable drives specified\n");
966 return NULL;
969 on_write_error = BLOCK_ERR_STOP_ENOSPC;
970 if ((buf = qemu_opt_get(opts, "werror")) != NULL) {
971 if (type != IF_IDE && type != IF_SCSI && type != IF_VIRTIO && type != IF_NONE) {
972 fprintf(stderr, "werror is no supported by this format\n");
973 return NULL;
976 on_write_error = parse_block_error_action(buf, 0);
977 if (on_write_error < 0) {
978 return NULL;
982 on_read_error = BLOCK_ERR_REPORT;
983 if ((buf = qemu_opt_get(opts, "rerror")) != NULL) {
984 if (type != IF_IDE && type != IF_VIRTIO && type != IF_NONE) {
985 fprintf(stderr, "rerror is no supported by this format\n");
986 return NULL;
989 on_read_error = parse_block_error_action(buf, 1);
990 if (on_read_error < 0) {
991 return NULL;
995 if ((devaddr = qemu_opt_get(opts, "addr")) != NULL) {
996 if (type != IF_VIRTIO) {
997 fprintf(stderr, "addr is not supported\n");
998 return NULL;
1002 /* compute bus and unit according index */
1004 if (index != -1) {
1005 if (bus_id != 0 || unit_id != -1) {
1006 fprintf(stderr,
1007 "qemu: index cannot be used with bus and unit\n");
1008 return NULL;
1010 if (max_devs == 0)
1012 unit_id = index;
1013 bus_id = 0;
1014 } else {
1015 unit_id = index % max_devs;
1016 bus_id = index / max_devs;
1020 /* if user doesn't specify a unit_id,
1021 * try to find the first free
1024 if (unit_id == -1) {
1025 unit_id = 0;
1026 while (drive_get(type, bus_id, unit_id) != NULL) {
1027 unit_id++;
1028 if (max_devs && unit_id >= max_devs) {
1029 unit_id -= max_devs;
1030 bus_id++;
1035 /* check unit id */
1037 if (max_devs && unit_id >= max_devs) {
1038 fprintf(stderr, "qemu: unit %d too big (max is %d)\n",
1039 unit_id, max_devs - 1);
1040 return NULL;
1044 * ignore multiple definitions
1047 if (drive_get(type, bus_id, unit_id) != NULL) {
1048 *fatal_error = 0;
1049 return NULL;
1052 /* init */
1054 dinfo = qemu_mallocz(sizeof(*dinfo));
1055 if ((buf = qemu_opts_id(opts)) != NULL) {
1056 dinfo->id = qemu_strdup(buf);
1057 } else {
1058 /* no id supplied -> create one */
1059 dinfo->id = qemu_mallocz(32);
1060 if (type == IF_IDE || type == IF_SCSI)
1061 mediastr = (media == MEDIA_CDROM) ? "-cd" : "-hd";
1062 if (max_devs)
1063 snprintf(dinfo->id, 32, "%s%i%s%i",
1064 devname, bus_id, mediastr, unit_id);
1065 else
1066 snprintf(dinfo->id, 32, "%s%s%i",
1067 devname, mediastr, unit_id);
1069 dinfo->bdrv = bdrv_new(dinfo->id);
1070 dinfo->devaddr = devaddr;
1071 dinfo->type = type;
1072 dinfo->bus = bus_id;
1073 dinfo->unit = unit_id;
1074 dinfo->on_read_error = on_read_error;
1075 dinfo->on_write_error = on_write_error;
1076 dinfo->opts = opts;
1077 if (serial)
1078 strncpy(dinfo->serial, serial, sizeof(serial));
1079 QTAILQ_INSERT_TAIL(&drives, dinfo, next);
1080 if (is_extboot) {
1081 extboot_drive = dinfo;
1084 switch(type) {
1085 case IF_IDE:
1086 case IF_SCSI:
1087 case IF_XEN:
1088 case IF_NONE:
1089 switch(media) {
1090 case MEDIA_DISK:
1091 if (cyls != 0) {
1092 bdrv_set_geometry_hint(dinfo->bdrv, cyls, heads, secs);
1093 bdrv_set_translation_hint(dinfo->bdrv, translation);
1095 break;
1096 case MEDIA_CDROM:
1097 bdrv_set_type_hint(dinfo->bdrv, BDRV_TYPE_CDROM);
1098 break;
1100 break;
1101 case IF_SD:
1102 /* FIXME: This isn't really a floppy, but it's a reasonable
1103 approximation. */
1104 case IF_FLOPPY:
1105 bdrv_set_type_hint(dinfo->bdrv, BDRV_TYPE_FLOPPY);
1106 break;
1107 case IF_PFLASH:
1108 case IF_MTD:
1109 break;
1110 case IF_VIRTIO:
1111 /* add virtio block device */
1112 opts = qemu_opts_create(&qemu_device_opts, NULL, 0);
1113 qemu_opt_set(opts, "driver", "virtio-blk-pci");
1114 qemu_opt_set(opts, "drive", dinfo->id);
1115 if (devaddr)
1116 qemu_opt_set(opts, "addr", devaddr);
1117 break;
1118 case IF_COUNT:
1119 abort();
1121 if (!file) {
1122 *fatal_error = 0;
1123 return NULL;
1125 if (snapshot) {
1126 /* always use cache=unsafe with snapshot */
1127 bdrv_flags &= ~BDRV_O_CACHE_MASK;
1128 bdrv_flags |= (BDRV_O_SNAPSHOT|BDRV_O_CACHE_WB|BDRV_O_NO_FLUSH);
1131 if (media == MEDIA_CDROM) {
1132 /* CDROM is fine for any interface, don't check. */
1133 ro = 1;
1134 } else if (ro == 1) {
1135 if (type != IF_SCSI && type != IF_VIRTIO && type != IF_FLOPPY && type != IF_NONE) {
1136 fprintf(stderr, "qemu: readonly flag not supported for drive with this interface\n");
1137 return NULL;
1141 bdrv_flags |= ro ? 0 : BDRV_O_RDWR;
1143 if (bdrv_open(dinfo->bdrv, file, bdrv_flags, drv) < 0) {
1144 fprintf(stderr, "qemu: could not open disk image %s: %s\n",
1145 file, strerror(errno));
1146 return NULL;
1149 if (bdrv_key_required(dinfo->bdrv))
1150 autostart = 0;
1151 *fatal_error = 0;
1152 return dinfo;
1155 static int drive_init_func(QemuOpts *opts, void *opaque)
1157 QEMUMachine *machine = opaque;
1158 int fatal_error = 0;
1160 if (drive_init(opts, machine, &fatal_error) == NULL) {
1161 if (fatal_error)
1162 return 1;
1164 return 0;
1167 static int drive_enable_snapshot(QemuOpts *opts, void *opaque)
1169 if (NULL == qemu_opt_get(opts, "snapshot")) {
1170 qemu_opt_set(opts, "snapshot", "on");
1172 return 0;
1175 void qemu_register_boot_set(QEMUBootSetHandler *func, void *opaque)
1177 boot_set_handler = func;
1178 boot_set_opaque = opaque;
1181 int qemu_boot_set(const char *boot_devices)
1183 if (!boot_set_handler) {
1184 return -EINVAL;
1186 return boot_set_handler(boot_set_opaque, boot_devices);
1189 static void validate_bootdevices(char *devices)
1191 /* We just do some generic consistency checks */
1192 const char *p;
1193 int bitmap = 0;
1195 for (p = devices; *p != '\0'; p++) {
1196 /* Allowed boot devices are:
1197 * a-b: floppy disk drives
1198 * c-f: IDE disk drives
1199 * g-m: machine implementation dependant drives
1200 * n-p: network devices
1201 * It's up to each machine implementation to check if the given boot
1202 * devices match the actual hardware implementation and firmware
1203 * features.
1205 if (*p < 'a' || *p > 'p') {
1206 fprintf(stderr, "Invalid boot device '%c'\n", *p);
1207 exit(1);
1209 if (bitmap & (1 << (*p - 'a'))) {
1210 fprintf(stderr, "Boot device '%c' was given twice\n", *p);
1211 exit(1);
1213 bitmap |= 1 << (*p - 'a');
1217 static void restore_boot_devices(void *opaque)
1219 char *standard_boot_devices = opaque;
1220 static int first = 1;
1222 /* Restore boot order and remove ourselves after the first boot */
1223 if (first) {
1224 first = 0;
1225 return;
1228 qemu_boot_set(standard_boot_devices);
1230 qemu_unregister_reset(restore_boot_devices, standard_boot_devices);
1231 qemu_free(standard_boot_devices);
1234 static void numa_add(const char *optarg)
1236 char option[128];
1237 char *endptr;
1238 unsigned long long value, endvalue;
1239 int nodenr;
1241 optarg = get_opt_name(option, 128, optarg, ',') + 1;
1242 if (!strcmp(option, "node")) {
1243 if (get_param_value(option, 128, "nodeid", optarg) == 0) {
1244 nodenr = nb_numa_nodes;
1245 } else {
1246 nodenr = strtoull(option, NULL, 10);
1249 if (get_param_value(option, 128, "mem", optarg) == 0) {
1250 node_mem[nodenr] = 0;
1251 } else {
1252 value = strtoull(option, &endptr, 0);
1253 switch (*endptr) {
1254 case 0: case 'M': case 'm':
1255 value <<= 20;
1256 break;
1257 case 'G': case 'g':
1258 value <<= 30;
1259 break;
1261 node_mem[nodenr] = value;
1263 if (get_param_value(option, 128, "cpus", optarg) == 0) {
1264 node_cpumask[nodenr] = 0;
1265 } else {
1266 value = strtoull(option, &endptr, 10);
1267 if (value >= 64) {
1268 value = 63;
1269 fprintf(stderr, "only 64 CPUs in NUMA mode supported.\n");
1270 } else {
1271 if (*endptr == '-') {
1272 endvalue = strtoull(endptr+1, &endptr, 10);
1273 if (endvalue >= 63) {
1274 endvalue = 62;
1275 fprintf(stderr,
1276 "only 63 CPUs in NUMA mode supported.\n");
1278 value = (2ULL << endvalue) - (1ULL << value);
1279 } else {
1280 value = 1ULL << value;
1283 node_cpumask[nodenr] = value;
1285 nb_numa_nodes++;
1287 return;
1290 static void smp_parse(const char *optarg)
1292 int smp, sockets = 0, threads = 0, cores = 0;
1293 char *endptr;
1294 char option[128];
1296 smp = strtoul(optarg, &endptr, 10);
1297 if (endptr != optarg) {
1298 if (*endptr == ',') {
1299 endptr++;
1302 if (get_param_value(option, 128, "sockets", endptr) != 0)
1303 sockets = strtoull(option, NULL, 10);
1304 if (get_param_value(option, 128, "cores", endptr) != 0)
1305 cores = strtoull(option, NULL, 10);
1306 if (get_param_value(option, 128, "threads", endptr) != 0)
1307 threads = strtoull(option, NULL, 10);
1308 if (get_param_value(option, 128, "maxcpus", endptr) != 0)
1309 max_cpus = strtoull(option, NULL, 10);
1311 /* compute missing values, prefer sockets over cores over threads */
1312 if (smp == 0 || sockets == 0) {
1313 sockets = sockets > 0 ? sockets : 1;
1314 cores = cores > 0 ? cores : 1;
1315 threads = threads > 0 ? threads : 1;
1316 if (smp == 0) {
1317 smp = cores * threads * sockets;
1319 } else {
1320 if (cores == 0) {
1321 threads = threads > 0 ? threads : 1;
1322 cores = smp / (sockets * threads);
1323 } else {
1324 if (sockets) {
1325 threads = smp / (cores * sockets);
1329 smp_cpus = smp;
1330 smp_cores = cores > 0 ? cores : 1;
1331 smp_threads = threads > 0 ? threads : 1;
1332 if (max_cpus == 0)
1333 max_cpus = smp_cpus;
1336 /***********************************************************/
1337 /* USB devices */
1339 static int usb_device_add(const char *devname, int is_hotplug)
1341 const char *p;
1342 USBDevice *dev = NULL;
1344 if (!usb_enabled)
1345 return -1;
1347 /* drivers with .usbdevice_name entry in USBDeviceInfo */
1348 dev = usbdevice_create(devname);
1349 if (dev)
1350 goto done;
1352 /* the other ones */
1353 if (strstart(devname, "host:", &p)) {
1354 dev = usb_host_device_open(p);
1355 } else if (!strcmp(devname, "bt") || strstart(devname, "bt:", &p)) {
1356 dev = usb_bt_init(devname[2] ? hci_init(p) :
1357 bt_new_hci(qemu_find_bt_vlan(0)));
1358 } else {
1359 return -1;
1361 if (!dev)
1362 return -1;
1364 done:
1365 return 0;
1368 static int usb_device_del(const char *devname)
1370 int bus_num, addr;
1371 const char *p;
1373 if (strstart(devname, "host:", &p))
1374 return usb_host_device_close(p);
1376 if (!usb_enabled)
1377 return -1;
1379 p = strchr(devname, '.');
1380 if (!p)
1381 return -1;
1382 bus_num = strtoul(devname, NULL, 0);
1383 addr = strtoul(p + 1, NULL, 0);
1385 return usb_device_delete_addr(bus_num, addr);
1388 static int usb_parse(const char *cmdline)
1390 int r;
1391 r = usb_device_add(cmdline, 0);
1392 if (r < 0) {
1393 fprintf(stderr, "qemu: could not add USB device '%s'\n", cmdline);
1395 return r;
1398 void do_usb_add(Monitor *mon, const QDict *qdict)
1400 const char *devname = qdict_get_str(qdict, "devname");
1401 if (usb_device_add(devname, 1) < 0) {
1402 error_report("could not add USB device '%s'", devname);
1406 void do_usb_del(Monitor *mon, const QDict *qdict)
1408 const char *devname = qdict_get_str(qdict, "devname");
1409 if (usb_device_del(devname) < 0) {
1410 error_report("could not delete USB device '%s'", devname);
1414 /***********************************************************/
1415 /* PCMCIA/Cardbus */
1417 static struct pcmcia_socket_entry_s {
1418 PCMCIASocket *socket;
1419 struct pcmcia_socket_entry_s *next;
1420 } *pcmcia_sockets = 0;
1422 void pcmcia_socket_register(PCMCIASocket *socket)
1424 struct pcmcia_socket_entry_s *entry;
1426 entry = qemu_malloc(sizeof(struct pcmcia_socket_entry_s));
1427 entry->socket = socket;
1428 entry->next = pcmcia_sockets;
1429 pcmcia_sockets = entry;
1432 void pcmcia_socket_unregister(PCMCIASocket *socket)
1434 struct pcmcia_socket_entry_s *entry, **ptr;
1436 ptr = &pcmcia_sockets;
1437 for (entry = *ptr; entry; ptr = &entry->next, entry = *ptr)
1438 if (entry->socket == socket) {
1439 *ptr = entry->next;
1440 qemu_free(entry);
1444 void pcmcia_info(Monitor *mon)
1446 struct pcmcia_socket_entry_s *iter;
1448 if (!pcmcia_sockets)
1449 monitor_printf(mon, "No PCMCIA sockets\n");
1451 for (iter = pcmcia_sockets; iter; iter = iter->next)
1452 monitor_printf(mon, "%s: %s\n", iter->socket->slot_string,
1453 iter->socket->attached ? iter->socket->card_string :
1454 "Empty");
1457 /***********************************************************/
1458 /* I/O handling */
1460 typedef struct IOHandlerRecord {
1461 int fd;
1462 IOCanReadHandler *fd_read_poll;
1463 IOHandler *fd_read;
1464 IOHandler *fd_write;
1465 int deleted;
1466 void *opaque;
1467 /* temporary data */
1468 struct pollfd *ufd;
1469 QLIST_ENTRY(IOHandlerRecord) next;
1470 } IOHandlerRecord;
1472 static QLIST_HEAD(, IOHandlerRecord) io_handlers =
1473 QLIST_HEAD_INITIALIZER(io_handlers);
1476 /* XXX: fd_read_poll should be suppressed, but an API change is
1477 necessary in the character devices to suppress fd_can_read(). */
1478 int qemu_set_fd_handler2(int fd,
1479 IOCanReadHandler *fd_read_poll,
1480 IOHandler *fd_read,
1481 IOHandler *fd_write,
1482 void *opaque)
1484 IOHandlerRecord *ioh;
1486 if (!fd_read && !fd_write) {
1487 QLIST_FOREACH(ioh, &io_handlers, next) {
1488 if (ioh->fd == fd) {
1489 ioh->deleted = 1;
1490 break;
1493 } else {
1494 QLIST_FOREACH(ioh, &io_handlers, next) {
1495 if (ioh->fd == fd)
1496 goto found;
1498 ioh = qemu_mallocz(sizeof(IOHandlerRecord));
1499 QLIST_INSERT_HEAD(&io_handlers, ioh, next);
1500 found:
1501 ioh->fd = fd;
1502 ioh->fd_read_poll = fd_read_poll;
1503 ioh->fd_read = fd_read;
1504 ioh->fd_write = fd_write;
1505 ioh->opaque = opaque;
1506 ioh->deleted = 0;
1508 qemu_notify_event();
1509 return 0;
1512 int qemu_set_fd_handler(int fd,
1513 IOHandler *fd_read,
1514 IOHandler *fd_write,
1515 void *opaque)
1517 return qemu_set_fd_handler2(fd, NULL, fd_read, fd_write, opaque);
1520 /***********************************************************/
1521 /* machine registration */
1523 static QEMUMachine *first_machine = NULL;
1524 QEMUMachine *current_machine = NULL;
1526 int qemu_register_machine(QEMUMachine *m)
1528 QEMUMachine **pm;
1529 pm = &first_machine;
1530 while (*pm != NULL)
1531 pm = &(*pm)->next;
1532 m->next = NULL;
1533 *pm = m;
1534 return 0;
1537 static QEMUMachine *find_machine(const char *name)
1539 QEMUMachine *m;
1541 for(m = first_machine; m != NULL; m = m->next) {
1542 if (!strcmp(m->name, name))
1543 return m;
1544 if (m->alias && !strcmp(m->alias, name))
1545 return m;
1547 return NULL;
1550 static QEMUMachine *find_default_machine(void)
1552 QEMUMachine *m;
1554 for(m = first_machine; m != NULL; m = m->next) {
1555 if (m->is_default) {
1556 return m;
1559 return NULL;
1562 /***********************************************************/
1563 /* main execution loop */
1565 static void gui_update(void *opaque)
1567 uint64_t interval = GUI_REFRESH_INTERVAL;
1568 DisplayState *ds = opaque;
1569 DisplayChangeListener *dcl = ds->listeners;
1571 qemu_flush_coalesced_mmio_buffer();
1572 dpy_refresh(ds);
1574 while (dcl != NULL) {
1575 if (dcl->gui_timer_interval &&
1576 dcl->gui_timer_interval < interval)
1577 interval = dcl->gui_timer_interval;
1578 dcl = dcl->next;
1580 qemu_mod_timer(ds->gui_timer, interval + qemu_get_clock(rt_clock));
1583 static void nographic_update(void *opaque)
1585 uint64_t interval = GUI_REFRESH_INTERVAL;
1587 qemu_flush_coalesced_mmio_buffer();
1588 qemu_mod_timer(nographic_timer, interval + qemu_get_clock(rt_clock));
1591 struct vm_change_state_entry {
1592 VMChangeStateHandler *cb;
1593 void *opaque;
1594 QLIST_ENTRY (vm_change_state_entry) entries;
1597 static QLIST_HEAD(vm_change_state_head, vm_change_state_entry) vm_change_state_head;
1599 VMChangeStateEntry *qemu_add_vm_change_state_handler(VMChangeStateHandler *cb,
1600 void *opaque)
1602 VMChangeStateEntry *e;
1604 e = qemu_mallocz(sizeof (*e));
1606 e->cb = cb;
1607 e->opaque = opaque;
1608 QLIST_INSERT_HEAD(&vm_change_state_head, e, entries);
1609 return e;
1612 void qemu_del_vm_change_state_handler(VMChangeStateEntry *e)
1614 QLIST_REMOVE (e, entries);
1615 qemu_free (e);
1618 void vm_state_notify(int running, int reason)
1620 VMChangeStateEntry *e;
1622 for (e = vm_change_state_head.lh_first; e; e = e->entries.le_next) {
1623 e->cb(e->opaque, running, reason);
1627 void vm_start(void)
1629 if (!vm_running) {
1630 cpu_enable_ticks();
1631 vm_running = 1;
1632 vm_state_notify(1, 0);
1633 resume_all_vcpus();
1634 monitor_protocol_event(QEVENT_RESUME, NULL);
1638 /* reset/shutdown handler */
1640 typedef struct QEMUResetEntry {
1641 QTAILQ_ENTRY(QEMUResetEntry) entry;
1642 QEMUResetHandler *func;
1643 void *opaque;
1644 } QEMUResetEntry;
1646 static QTAILQ_HEAD(reset_handlers, QEMUResetEntry) reset_handlers =
1647 QTAILQ_HEAD_INITIALIZER(reset_handlers);
1648 static int reset_requested;
1649 static int shutdown_requested;
1650 static int powerdown_requested;
1651 int debug_requested;
1652 int vmstop_requested;
1654 int qemu_no_shutdown(void)
1656 int r = no_shutdown;
1657 no_shutdown = 0;
1658 return r;
1661 int qemu_shutdown_requested(void)
1663 int r = shutdown_requested;
1664 shutdown_requested = 0;
1665 return r;
1668 int qemu_reset_requested(void)
1670 int r = reset_requested;
1671 reset_requested = 0;
1672 return r;
1675 int qemu_powerdown_requested(void)
1677 int r = powerdown_requested;
1678 powerdown_requested = 0;
1679 return r;
1682 static int qemu_debug_requested(void)
1684 int r = debug_requested;
1685 debug_requested = 0;
1686 return r;
1689 static int qemu_vmstop_requested(void)
1691 int r = vmstop_requested;
1692 vmstop_requested = 0;
1693 return r;
1696 void qemu_register_reset(QEMUResetHandler *func, void *opaque)
1698 QEMUResetEntry *re = qemu_mallocz(sizeof(QEMUResetEntry));
1700 re->func = func;
1701 re->opaque = opaque;
1702 QTAILQ_INSERT_TAIL(&reset_handlers, re, entry);
1705 void qemu_unregister_reset(QEMUResetHandler *func, void *opaque)
1707 QEMUResetEntry *re;
1709 QTAILQ_FOREACH(re, &reset_handlers, entry) {
1710 if (re->func == func && re->opaque == opaque) {
1711 QTAILQ_REMOVE(&reset_handlers, re, entry);
1712 qemu_free(re);
1713 return;
1718 void qemu_system_reset(void)
1720 QEMUResetEntry *re, *nre;
1722 /* reset all devices */
1723 QTAILQ_FOREACH_SAFE(re, &reset_handlers, entry, nre) {
1724 re->func(re->opaque);
1726 monitor_protocol_event(QEVENT_RESET, NULL);
1727 cpu_synchronize_all_post_reset();
1730 void qemu_system_reset_request(void)
1732 if (no_reboot) {
1733 shutdown_requested = 1;
1734 } else {
1735 reset_requested = 1;
1737 if (cpu_single_env) {
1738 cpu_single_env->stopped = 1;
1740 qemu_notify_event();
1743 void qemu_system_shutdown_request(void)
1745 shutdown_requested = 1;
1746 qemu_notify_event();
1749 void qemu_system_powerdown_request(void)
1751 powerdown_requested = 1;
1752 qemu_notify_event();
1755 void main_loop_wait(int nonblocking)
1757 IOHandlerRecord *ioh;
1758 fd_set rfds, wfds, xfds;
1759 int ret, nfds;
1760 struct timeval tv;
1761 int timeout;
1763 if (nonblocking)
1764 timeout = 0;
1765 else {
1766 timeout = qemu_calculate_timeout();
1767 qemu_bh_update_timeout(&timeout);
1770 os_host_main_loop_wait(&timeout);
1772 /* poll any events */
1773 /* XXX: separate device handlers from system ones */
1774 nfds = -1;
1775 FD_ZERO(&rfds);
1776 FD_ZERO(&wfds);
1777 FD_ZERO(&xfds);
1778 QLIST_FOREACH(ioh, &io_handlers, next) {
1779 if (ioh->deleted)
1780 continue;
1781 if (ioh->fd_read &&
1782 (!ioh->fd_read_poll ||
1783 ioh->fd_read_poll(ioh->opaque) != 0)) {
1784 FD_SET(ioh->fd, &rfds);
1785 if (ioh->fd > nfds)
1786 nfds = ioh->fd;
1788 if (ioh->fd_write) {
1789 FD_SET(ioh->fd, &wfds);
1790 if (ioh->fd > nfds)
1791 nfds = ioh->fd;
1795 tv.tv_sec = timeout / 1000;
1796 tv.tv_usec = (timeout % 1000) * 1000;
1798 slirp_select_fill(&nfds, &rfds, &wfds, &xfds);
1800 qemu_mutex_unlock_iothread();
1801 ret = select(nfds + 1, &rfds, &wfds, &xfds, &tv);
1802 qemu_mutex_lock_iothread();
1803 if (ret > 0) {
1804 IOHandlerRecord *pioh;
1806 QLIST_FOREACH_SAFE(ioh, &io_handlers, next, pioh) {
1807 if (ioh->deleted) {
1808 QLIST_REMOVE(ioh, next);
1809 qemu_free(ioh);
1810 continue;
1812 if (ioh->fd_read && FD_ISSET(ioh->fd, &rfds)) {
1813 ioh->fd_read(ioh->opaque);
1814 if (!(ioh->fd_read_poll && ioh->fd_read_poll(ioh->opaque)))
1815 FD_CLR(ioh->fd, &rfds);
1817 if (ioh->fd_write && FD_ISSET(ioh->fd, &wfds)) {
1818 ioh->fd_write(ioh->opaque);
1823 slirp_select_poll(&rfds, &wfds, &xfds, (ret < 0));
1825 qemu_run_all_timers();
1827 /* Check bottom-halves last in case any of the earlier events triggered
1828 them. */
1829 qemu_bh_poll();
1833 static int vm_can_run(void)
1835 if (powerdown_requested)
1836 return 0;
1837 if (reset_requested)
1838 return 0;
1839 if (shutdown_requested)
1840 return 0;
1841 if (debug_requested)
1842 return 0;
1843 return 1;
1846 qemu_irq qemu_system_powerdown;
1848 static void main_loop(void)
1850 int r;
1852 if (kvm_enabled()) {
1853 kvm_main_loop();
1854 cpu_disable_ticks();
1855 return;
1858 qemu_main_loop_start();
1860 for (;;) {
1861 do {
1862 bool nonblocking = false;
1863 #ifdef CONFIG_PROFILER
1864 int64_t ti;
1865 #endif
1866 #ifndef CONFIG_IOTHREAD
1867 nonblocking = tcg_cpu_exec();
1868 #endif
1869 #ifdef CONFIG_PROFILER
1870 ti = profile_getclock();
1871 #endif
1872 main_loop_wait(nonblocking);
1873 #ifdef CONFIG_PROFILER
1874 dev_time += profile_getclock() - ti;
1875 #endif
1876 } while (vm_can_run());
1878 if ((r = qemu_debug_requested())) {
1879 vm_stop(r);
1881 if (qemu_shutdown_requested()) {
1882 monitor_protocol_event(QEVENT_SHUTDOWN, NULL);
1883 if (no_shutdown) {
1884 vm_stop(0);
1885 no_shutdown = 0;
1886 } else
1887 break;
1889 if (qemu_reset_requested()) {
1890 pause_all_vcpus();
1891 qemu_system_reset();
1892 resume_all_vcpus();
1894 if (qemu_powerdown_requested()) {
1895 monitor_protocol_event(QEVENT_POWERDOWN, NULL);
1896 qemu_irq_raise(qemu_system_powerdown);
1898 if ((r = qemu_vmstop_requested())) {
1899 vm_stop(r);
1902 pause_all_vcpus();
1905 static void version(void)
1907 printf("QEMU emulator version " QEMU_VERSION QEMU_PKGVERSION ", Copyright (c) 2003-2008 Fabrice Bellard\n");
1910 static void help(int exitcode)
1912 const char *options_help =
1913 #define DEF(option, opt_arg, opt_enum, opt_help, arch_mask) \
1914 opt_help
1915 #define DEFHEADING(text) stringify(text) "\n"
1916 #include "qemu-options.def"
1917 #undef DEF
1918 #undef DEFHEADING
1919 #undef GEN_DOCS
1921 version();
1922 printf("usage: %s [options] [disk_image]\n"
1923 "\n"
1924 "'disk_image' is a raw hard disk image for IDE hard disk 0\n"
1925 "\n"
1926 "%s\n"
1927 "During emulation, the following keys are useful:\n"
1928 "ctrl-alt-f toggle full screen\n"
1929 "ctrl-alt-n switch to virtual console 'n'\n"
1930 "ctrl-alt toggle mouse and keyboard grab\n"
1931 "\n"
1932 "When using -nographic, press 'ctrl-a h' to get some help.\n",
1933 "qemu",
1934 options_help);
1935 exit(exitcode);
1938 #define HAS_ARG 0x0001
1940 typedef struct QEMUOption {
1941 const char *name;
1942 int flags;
1943 int index;
1944 uint32_t arch_mask;
1945 } QEMUOption;
1947 static const QEMUOption qemu_options[] = {
1948 { "h", 0, QEMU_OPTION_h, QEMU_ARCH_ALL },
1949 #define DEF(option, opt_arg, opt_enum, opt_help, arch_mask) \
1950 { option, opt_arg, opt_enum, arch_mask },
1951 #define DEFHEADING(text)
1952 #include "qemu-options.def"
1953 #undef DEF
1954 #undef DEFHEADING
1955 #undef GEN_DOCS
1956 { NULL },
1958 static void select_vgahw (const char *p)
1960 const char *opts;
1962 default_vga = 0;
1963 vga_interface_type = VGA_NONE;
1964 if (strstart(p, "std", &opts)) {
1965 vga_interface_type = VGA_STD;
1966 } else if (strstart(p, "cirrus", &opts)) {
1967 vga_interface_type = VGA_CIRRUS;
1968 } else if (strstart(p, "vmware", &opts)) {
1969 vga_interface_type = VGA_VMWARE;
1970 } else if (strstart(p, "xenfb", &opts)) {
1971 vga_interface_type = VGA_XENFB;
1972 } else if (!strstart(p, "none", &opts)) {
1973 invalid_vga:
1974 fprintf(stderr, "Unknown vga type: %s\n", p);
1975 exit(1);
1977 while (*opts) {
1978 const char *nextopt;
1980 if (strstart(opts, ",retrace=", &nextopt)) {
1981 opts = nextopt;
1982 if (strstart(opts, "dumb", &nextopt))
1983 vga_retrace_method = VGA_RETRACE_DUMB;
1984 else if (strstart(opts, "precise", &nextopt))
1985 vga_retrace_method = VGA_RETRACE_PRECISE;
1986 else goto invalid_vga;
1987 } else goto invalid_vga;
1988 opts = nextopt;
1992 static int balloon_parse(const char *arg)
1994 QemuOpts *opts;
1996 if (strcmp(arg, "none") == 0) {
1997 return 0;
2000 if (!strncmp(arg, "virtio", 6)) {
2001 if (arg[6] == ',') {
2002 /* have params -> parse them */
2003 opts = qemu_opts_parse(&qemu_device_opts, arg+7, 0);
2004 if (!opts)
2005 return -1;
2006 } else {
2007 /* create empty opts */
2008 opts = qemu_opts_create(&qemu_device_opts, NULL, 0);
2010 qemu_opt_set(opts, "driver", "virtio-balloon-pci");
2011 return 0;
2014 return -1;
2017 char *qemu_find_file(int type, const char *name)
2019 int len;
2020 const char *subdir;
2021 char *buf;
2023 /* If name contains path separators then try it as a straight path. */
2024 if ((strchr(name, '/') || strchr(name, '\\'))
2025 && access(name, R_OK) == 0) {
2026 return qemu_strdup(name);
2028 switch (type) {
2029 case QEMU_FILE_TYPE_BIOS:
2030 subdir = "";
2031 break;
2032 case QEMU_FILE_TYPE_KEYMAP:
2033 subdir = "keymaps/";
2034 break;
2035 default:
2036 abort();
2038 len = strlen(data_dir) + strlen(name) + strlen(subdir) + 2;
2039 buf = qemu_mallocz(len);
2040 snprintf(buf, len, "%s/%s%s", data_dir, subdir, name);
2041 if (access(buf, R_OK)) {
2042 qemu_free(buf);
2043 return NULL;
2045 return buf;
2048 static int device_help_func(QemuOpts *opts, void *opaque)
2050 return qdev_device_help(opts);
2053 static int device_init_func(QemuOpts *opts, void *opaque)
2055 DeviceState *dev;
2057 dev = qdev_device_add(opts);
2058 if (!dev)
2059 return -1;
2060 return 0;
2063 static int chardev_init_func(QemuOpts *opts, void *opaque)
2065 CharDriverState *chr;
2067 chr = qemu_chr_open_opts(opts, NULL);
2068 if (!chr)
2069 return -1;
2070 return 0;
2073 #ifdef CONFIG_LINUX
2074 static int fsdev_init_func(QemuOpts *opts, void *opaque)
2076 int ret;
2077 ret = qemu_fsdev_add(opts);
2079 return ret;
2081 #endif
2083 static int mon_init_func(QemuOpts *opts, void *opaque)
2085 CharDriverState *chr;
2086 const char *chardev;
2087 const char *mode;
2088 int flags;
2090 mode = qemu_opt_get(opts, "mode");
2091 if (mode == NULL) {
2092 mode = "readline";
2094 if (strcmp(mode, "readline") == 0) {
2095 flags = MONITOR_USE_READLINE;
2096 } else if (strcmp(mode, "control") == 0) {
2097 flags = MONITOR_USE_CONTROL;
2098 } else {
2099 fprintf(stderr, "unknown monitor mode \"%s\"\n", mode);
2100 exit(1);
2103 if (qemu_opt_get_bool(opts, "default", 0))
2104 flags |= MONITOR_IS_DEFAULT;
2106 chardev = qemu_opt_get(opts, "chardev");
2107 chr = qemu_chr_find(chardev);
2108 if (chr == NULL) {
2109 fprintf(stderr, "chardev \"%s\" not found\n", chardev);
2110 exit(1);
2113 monitor_init(chr, flags);
2114 return 0;
2117 static void monitor_parse(const char *optarg, const char *mode)
2119 static int monitor_device_index = 0;
2120 QemuOpts *opts;
2121 const char *p;
2122 char label[32];
2123 int def = 0;
2125 if (strstart(optarg, "chardev:", &p)) {
2126 snprintf(label, sizeof(label), "%s", p);
2127 } else {
2128 snprintf(label, sizeof(label), "compat_monitor%d",
2129 monitor_device_index);
2130 if (monitor_device_index == 0) {
2131 def = 1;
2133 opts = qemu_chr_parse_compat(label, optarg);
2134 if (!opts) {
2135 fprintf(stderr, "parse error: %s\n", optarg);
2136 exit(1);
2140 opts = qemu_opts_create(&qemu_mon_opts, label, 1);
2141 if (!opts) {
2142 fprintf(stderr, "duplicate chardev: %s\n", label);
2143 exit(1);
2145 qemu_opt_set(opts, "mode", mode);
2146 qemu_opt_set(opts, "chardev", label);
2147 if (def)
2148 qemu_opt_set(opts, "default", "on");
2149 monitor_device_index++;
2152 struct device_config {
2153 enum {
2154 DEV_USB, /* -usbdevice */
2155 DEV_BT, /* -bt */
2156 DEV_SERIAL, /* -serial */
2157 DEV_PARALLEL, /* -parallel */
2158 DEV_VIRTCON, /* -virtioconsole */
2159 DEV_DEBUGCON, /* -debugcon */
2160 } type;
2161 const char *cmdline;
2162 QTAILQ_ENTRY(device_config) next;
2164 QTAILQ_HEAD(, device_config) device_configs = QTAILQ_HEAD_INITIALIZER(device_configs);
2166 static void add_device_config(int type, const char *cmdline)
2168 struct device_config *conf;
2170 conf = qemu_mallocz(sizeof(*conf));
2171 conf->type = type;
2172 conf->cmdline = cmdline;
2173 QTAILQ_INSERT_TAIL(&device_configs, conf, next);
2176 static int foreach_device_config(int type, int (*func)(const char *cmdline))
2178 struct device_config *conf;
2179 int rc;
2181 QTAILQ_FOREACH(conf, &device_configs, next) {
2182 if (conf->type != type)
2183 continue;
2184 rc = func(conf->cmdline);
2185 if (0 != rc)
2186 return rc;
2188 return 0;
2191 static int serial_parse(const char *devname)
2193 static int index = 0;
2194 char label[32];
2196 if (strcmp(devname, "none") == 0)
2197 return 0;
2198 if (index == MAX_SERIAL_PORTS) {
2199 fprintf(stderr, "qemu: too many serial ports\n");
2200 exit(1);
2202 snprintf(label, sizeof(label), "serial%d", index);
2203 serial_hds[index] = qemu_chr_open(label, devname, NULL);
2204 if (!serial_hds[index]) {
2205 fprintf(stderr, "qemu: could not open serial device '%s': %s\n",
2206 devname, strerror(errno));
2207 return -1;
2209 index++;
2210 return 0;
2213 static int parallel_parse(const char *devname)
2215 static int index = 0;
2216 char label[32];
2218 if (strcmp(devname, "none") == 0)
2219 return 0;
2220 if (index == MAX_PARALLEL_PORTS) {
2221 fprintf(stderr, "qemu: too many parallel ports\n");
2222 exit(1);
2224 snprintf(label, sizeof(label), "parallel%d", index);
2225 parallel_hds[index] = qemu_chr_open(label, devname, NULL);
2226 if (!parallel_hds[index]) {
2227 fprintf(stderr, "qemu: could not open parallel device '%s': %s\n",
2228 devname, strerror(errno));
2229 return -1;
2231 index++;
2232 return 0;
2235 static int virtcon_parse(const char *devname)
2237 static int index = 0;
2238 char label[32];
2239 QemuOpts *bus_opts, *dev_opts;
2241 if (strcmp(devname, "none") == 0)
2242 return 0;
2243 if (index == MAX_VIRTIO_CONSOLES) {
2244 fprintf(stderr, "qemu: too many virtio consoles\n");
2245 exit(1);
2248 bus_opts = qemu_opts_create(&qemu_device_opts, NULL, 0);
2249 qemu_opt_set(bus_opts, "driver", "virtio-serial");
2251 dev_opts = qemu_opts_create(&qemu_device_opts, NULL, 0);
2252 qemu_opt_set(dev_opts, "driver", "virtconsole");
2254 snprintf(label, sizeof(label), "virtcon%d", index);
2255 virtcon_hds[index] = qemu_chr_open(label, devname, NULL);
2256 if (!virtcon_hds[index]) {
2257 fprintf(stderr, "qemu: could not open virtio console '%s': %s\n",
2258 devname, strerror(errno));
2259 return -1;
2261 qemu_opt_set(dev_opts, "chardev", label);
2263 index++;
2264 return 0;
2267 static int debugcon_parse(const char *devname)
2269 QemuOpts *opts;
2271 if (!qemu_chr_open("debugcon", devname, NULL)) {
2272 exit(1);
2274 opts = qemu_opts_create(&qemu_device_opts, "debugcon", 1);
2275 if (!opts) {
2276 fprintf(stderr, "qemu: already have a debugcon device\n");
2277 exit(1);
2279 qemu_opt_set(opts, "driver", "isa-debugcon");
2280 qemu_opt_set(opts, "chardev", "debugcon");
2281 return 0;
2284 static const QEMUOption *lookup_opt(int argc, char **argv,
2285 const char **poptarg, int *poptind)
2287 const QEMUOption *popt;
2288 int optind = *poptind;
2289 char *r = argv[optind];
2290 const char *optarg;
2292 loc_set_cmdline(argv, optind, 1);
2293 optind++;
2294 /* Treat --foo the same as -foo. */
2295 if (r[1] == '-')
2296 r++;
2297 popt = qemu_options;
2298 for(;;) {
2299 if (!popt->name) {
2300 error_report("invalid option");
2301 exit(1);
2303 if (!strcmp(popt->name, r + 1))
2304 break;
2305 popt++;
2307 if (popt->flags & HAS_ARG) {
2308 if (optind >= argc) {
2309 error_report("requires an argument");
2310 exit(1);
2312 optarg = argv[optind++];
2313 loc_set_cmdline(argv, optind - 2, 2);
2314 } else {
2315 optarg = NULL;
2318 *poptarg = optarg;
2319 *poptind = optind;
2321 return popt;
2324 int main(int argc, char **argv, char **envp)
2326 const char *gdbstub_dev = NULL;
2327 int i;
2328 int snapshot, linux_boot;
2329 const char *icount_option = NULL;
2330 const char *initrd_filename;
2331 const char *kernel_filename, *kernel_cmdline;
2332 char boot_devices[33] = "cad"; /* default to HD->floppy->CD-ROM */
2333 DisplayState *ds;
2334 DisplayChangeListener *dcl;
2335 int cyls, heads, secs, translation;
2336 QemuOpts *hda_opts = NULL, *opts;
2337 int optind;
2338 const char *optarg;
2339 const char *loadvm = NULL;
2340 QEMUMachine *machine;
2341 const char *cpu_model;
2342 #ifndef _WIN32
2343 int fds[2];
2344 #endif
2345 int tb_size;
2346 const char *pid_file = NULL;
2347 const char *incoming = NULL;
2348 #ifndef _WIN32
2349 int fd = 0;
2350 #endif
2351 int show_vnc_port = 0;
2352 int defconfig = 1;
2354 error_set_progname(argv[0]);
2356 init_clocks();
2358 qemu_cache_utils_init(envp);
2360 QLIST_INIT (&vm_change_state_head);
2361 os_setup_early_signal_handling();
2363 module_call_init(MODULE_INIT_MACHINE);
2364 machine = find_default_machine();
2365 cpu_model = NULL;
2366 initrd_filename = NULL;
2367 ram_size = 0;
2368 snapshot = 0;
2369 kernel_filename = NULL;
2370 kernel_cmdline = "";
2371 cyls = heads = secs = 0;
2372 translation = BIOS_ATA_TRANSLATION_AUTO;
2374 for (i = 0; i < MAX_NODES; i++) {
2375 node_mem[i] = 0;
2376 node_cpumask[i] = 0;
2379 assigned_devices_index = 0;
2381 nb_numa_nodes = 0;
2382 nb_nics = 0;
2384 tb_size = 0;
2385 autostart= 1;
2387 /* first pass of option parsing */
2388 optind = 1;
2389 while (optind < argc) {
2390 if (argv[optind][0] != '-') {
2391 /* disk image */
2392 optind++;
2393 continue;
2394 } else {
2395 const QEMUOption *popt;
2397 popt = lookup_opt(argc, argv, &optarg, &optind);
2398 switch (popt->index) {
2399 case QEMU_OPTION_nodefconfig:
2400 defconfig=0;
2401 break;
2406 if (defconfig) {
2407 int ret;
2409 ret = qemu_read_config_file(CONFIG_QEMU_CONFDIR "/qemu.conf");
2410 if (ret < 0 && ret != -ENOENT) {
2411 exit(1);
2414 ret = qemu_read_config_file(arch_config_name);
2415 if (ret < 0 && ret != -ENOENT) {
2416 exit(1);
2419 cpudef_init();
2421 /* second pass of option parsing */
2422 optind = 1;
2423 for(;;) {
2424 if (optind >= argc)
2425 break;
2426 if (argv[optind][0] != '-') {
2427 hda_opts = drive_add(argv[optind++], HD_ALIAS, 0);
2428 } else {
2429 const QEMUOption *popt;
2431 popt = lookup_opt(argc, argv, &optarg, &optind);
2432 if (!(popt->arch_mask & arch_type)) {
2433 printf("Option %s not supported for this target\n", popt->name);
2434 exit(1);
2436 switch(popt->index) {
2437 case QEMU_OPTION_M:
2438 machine = find_machine(optarg);
2439 if (!machine) {
2440 QEMUMachine *m;
2441 printf("Supported machines are:\n");
2442 for(m = first_machine; m != NULL; m = m->next) {
2443 if (m->alias)
2444 printf("%-10s %s (alias of %s)\n",
2445 m->alias, m->desc, m->name);
2446 printf("%-10s %s%s\n",
2447 m->name, m->desc,
2448 m->is_default ? " (default)" : "");
2450 exit(*optarg != '?');
2452 break;
2453 case QEMU_OPTION_cpu:
2454 /* hw initialization will check this */
2455 if (*optarg == '?') {
2456 list_cpus(stdout, &fprintf, optarg);
2457 exit(0);
2458 } else {
2459 cpu_model = optarg;
2461 break;
2462 case QEMU_OPTION_initrd:
2463 initrd_filename = optarg;
2464 break;
2465 case QEMU_OPTION_hda:
2466 if (cyls == 0)
2467 hda_opts = drive_add(optarg, HD_ALIAS, 0);
2468 else
2469 hda_opts = drive_add(optarg, HD_ALIAS
2470 ",cyls=%d,heads=%d,secs=%d%s",
2471 0, cyls, heads, secs,
2472 translation == BIOS_ATA_TRANSLATION_LBA ?
2473 ",trans=lba" :
2474 translation == BIOS_ATA_TRANSLATION_NONE ?
2475 ",trans=none" : "");
2476 break;
2477 case QEMU_OPTION_hdb:
2478 case QEMU_OPTION_hdc:
2479 case QEMU_OPTION_hdd:
2480 drive_add(optarg, HD_ALIAS, popt->index - QEMU_OPTION_hda);
2481 break;
2482 case QEMU_OPTION_drive:
2483 drive_add(NULL, "%s", optarg);
2484 break;
2485 case QEMU_OPTION_set:
2486 if (qemu_set_option(optarg) != 0)
2487 exit(1);
2488 break;
2489 case QEMU_OPTION_global:
2490 if (qemu_global_option(optarg) != 0)
2491 exit(1);
2492 break;
2493 case QEMU_OPTION_mtdblock:
2494 drive_add(optarg, MTD_ALIAS);
2495 break;
2496 case QEMU_OPTION_sd:
2497 drive_add(optarg, SD_ALIAS);
2498 break;
2499 case QEMU_OPTION_pflash:
2500 drive_add(optarg, PFLASH_ALIAS);
2501 break;
2502 case QEMU_OPTION_snapshot:
2503 snapshot = 1;
2504 break;
2505 case QEMU_OPTION_hdachs:
2507 const char *p;
2508 p = optarg;
2509 cyls = strtol(p, (char **)&p, 0);
2510 if (cyls < 1 || cyls > 16383)
2511 goto chs_fail;
2512 if (*p != ',')
2513 goto chs_fail;
2514 p++;
2515 heads = strtol(p, (char **)&p, 0);
2516 if (heads < 1 || heads > 16)
2517 goto chs_fail;
2518 if (*p != ',')
2519 goto chs_fail;
2520 p++;
2521 secs = strtol(p, (char **)&p, 0);
2522 if (secs < 1 || secs > 63)
2523 goto chs_fail;
2524 if (*p == ',') {
2525 p++;
2526 if (!strcmp(p, "none"))
2527 translation = BIOS_ATA_TRANSLATION_NONE;
2528 else if (!strcmp(p, "lba"))
2529 translation = BIOS_ATA_TRANSLATION_LBA;
2530 else if (!strcmp(p, "auto"))
2531 translation = BIOS_ATA_TRANSLATION_AUTO;
2532 else
2533 goto chs_fail;
2534 } else if (*p != '\0') {
2535 chs_fail:
2536 fprintf(stderr, "qemu: invalid physical CHS format\n");
2537 exit(1);
2539 if (hda_opts != NULL) {
2540 char num[16];
2541 snprintf(num, sizeof(num), "%d", cyls);
2542 qemu_opt_set(hda_opts, "cyls", num);
2543 snprintf(num, sizeof(num), "%d", heads);
2544 qemu_opt_set(hda_opts, "heads", num);
2545 snprintf(num, sizeof(num), "%d", secs);
2546 qemu_opt_set(hda_opts, "secs", num);
2547 if (translation == BIOS_ATA_TRANSLATION_LBA)
2548 qemu_opt_set(hda_opts, "trans", "lba");
2549 if (translation == BIOS_ATA_TRANSLATION_NONE)
2550 qemu_opt_set(hda_opts, "trans", "none");
2553 break;
2554 case QEMU_OPTION_numa:
2555 if (nb_numa_nodes >= MAX_NODES) {
2556 fprintf(stderr, "qemu: too many NUMA nodes\n");
2557 exit(1);
2559 numa_add(optarg);
2560 break;
2561 case QEMU_OPTION_nographic:
2562 display_type = DT_NOGRAPHIC;
2563 break;
2564 #ifdef CONFIG_CURSES
2565 case QEMU_OPTION_curses:
2566 display_type = DT_CURSES;
2567 break;
2568 #endif
2569 case QEMU_OPTION_portrait:
2570 graphic_rotate = 1;
2571 break;
2572 case QEMU_OPTION_kernel:
2573 kernel_filename = optarg;
2574 break;
2575 case QEMU_OPTION_append:
2576 kernel_cmdline = optarg;
2577 break;
2578 case QEMU_OPTION_cdrom:
2579 drive_add(optarg, CDROM_ALIAS);
2580 break;
2581 case QEMU_OPTION_boot:
2583 static const char * const params[] = {
2584 "order", "once", "menu", NULL
2586 char buf[sizeof(boot_devices)];
2587 char *standard_boot_devices;
2588 int legacy = 0;
2590 if (!strchr(optarg, '=')) {
2591 legacy = 1;
2592 pstrcpy(buf, sizeof(buf), optarg);
2593 } else if (check_params(buf, sizeof(buf), params, optarg) < 0) {
2594 fprintf(stderr,
2595 "qemu: unknown boot parameter '%s' in '%s'\n",
2596 buf, optarg);
2597 exit(1);
2600 if (legacy ||
2601 get_param_value(buf, sizeof(buf), "order", optarg)) {
2602 validate_bootdevices(buf);
2603 pstrcpy(boot_devices, sizeof(boot_devices), buf);
2605 if (!legacy) {
2606 if (get_param_value(buf, sizeof(buf),
2607 "once", optarg)) {
2608 validate_bootdevices(buf);
2609 standard_boot_devices = qemu_strdup(boot_devices);
2610 pstrcpy(boot_devices, sizeof(boot_devices), buf);
2611 qemu_register_reset(restore_boot_devices,
2612 standard_boot_devices);
2614 if (get_param_value(buf, sizeof(buf),
2615 "menu", optarg)) {
2616 if (!strcmp(buf, "on")) {
2617 boot_menu = 1;
2618 } else if (!strcmp(buf, "off")) {
2619 boot_menu = 0;
2620 } else {
2621 fprintf(stderr,
2622 "qemu: invalid option value '%s'\n",
2623 buf);
2624 exit(1);
2629 break;
2630 case QEMU_OPTION_fda:
2631 case QEMU_OPTION_fdb:
2632 drive_add(optarg, FD_ALIAS, popt->index - QEMU_OPTION_fda);
2633 break;
2634 case QEMU_OPTION_no_fd_bootchk:
2635 fd_bootchk = 0;
2636 break;
2637 case QEMU_OPTION_netdev:
2638 if (net_client_parse(&qemu_netdev_opts, optarg) == -1) {
2639 exit(1);
2641 break;
2642 case QEMU_OPTION_net:
2643 if (net_client_parse(&qemu_net_opts, optarg) == -1) {
2644 exit(1);
2646 break;
2647 #ifdef CONFIG_SLIRP
2648 case QEMU_OPTION_tftp:
2649 legacy_tftp_prefix = optarg;
2650 break;
2651 case QEMU_OPTION_bootp:
2652 legacy_bootp_filename = optarg;
2653 break;
2654 case QEMU_OPTION_redir:
2655 if (net_slirp_redir(optarg) < 0)
2656 exit(1);
2657 break;
2658 #endif
2659 case QEMU_OPTION_bt:
2660 add_device_config(DEV_BT, optarg);
2661 break;
2662 case QEMU_OPTION_audio_help:
2663 if (!(audio_available())) {
2664 printf("Option %s not supported for this target\n", popt->name);
2665 exit(1);
2667 AUD_help ();
2668 exit (0);
2669 break;
2670 case QEMU_OPTION_soundhw:
2671 if (!(audio_available())) {
2672 printf("Option %s not supported for this target\n", popt->name);
2673 exit(1);
2675 select_soundhw (optarg);
2676 break;
2677 case QEMU_OPTION_h:
2678 help(0);
2679 break;
2680 case QEMU_OPTION_version:
2681 version();
2682 exit(0);
2683 break;
2684 case QEMU_OPTION_m: {
2685 uint64_t value;
2686 char *ptr;
2688 value = strtoul(optarg, &ptr, 10);
2689 switch (*ptr) {
2690 case 0: case 'M': case 'm':
2691 value <<= 20;
2692 break;
2693 case 'G': case 'g':
2694 value <<= 30;
2695 break;
2696 default:
2697 fprintf(stderr, "qemu: invalid ram size: %s\n", optarg);
2698 exit(1);
2701 /* On 32-bit hosts, QEMU is limited by virtual address space */
2702 if (value > (2047 << 20) && HOST_LONG_BITS == 32) {
2703 fprintf(stderr, "qemu: at most 2047 MB RAM can be simulated\n");
2704 exit(1);
2706 if (value != (uint64_t)(ram_addr_t)value) {
2707 fprintf(stderr, "qemu: ram size too large\n");
2708 exit(1);
2710 ram_size = value;
2711 break;
2713 case QEMU_OPTION_mempath:
2714 mem_path = optarg;
2715 break;
2716 #ifdef MAP_POPULATE
2717 case QEMU_OPTION_mem_prealloc:
2718 mem_prealloc = 1;
2719 break;
2720 #endif
2721 case QEMU_OPTION_d:
2722 set_cpu_log(optarg);
2723 break;
2724 case QEMU_OPTION_s:
2725 gdbstub_dev = "tcp::" DEFAULT_GDBSTUB_PORT;
2726 break;
2727 case QEMU_OPTION_gdb:
2728 gdbstub_dev = optarg;
2729 break;
2730 case QEMU_OPTION_L:
2731 data_dir = optarg;
2732 break;
2733 case QEMU_OPTION_bios:
2734 bios_name = optarg;
2735 break;
2736 case QEMU_OPTION_singlestep:
2737 singlestep = 1;
2738 break;
2739 case QEMU_OPTION_S:
2740 autostart = 0;
2741 break;
2742 case QEMU_OPTION_k:
2743 keyboard_layout = optarg;
2744 break;
2745 case QEMU_OPTION_localtime:
2746 rtc_utc = 0;
2747 break;
2748 case QEMU_OPTION_vga:
2749 select_vgahw (optarg);
2750 break;
2751 case QEMU_OPTION_g:
2753 const char *p;
2754 int w, h, depth;
2755 p = optarg;
2756 w = strtol(p, (char **)&p, 10);
2757 if (w <= 0) {
2758 graphic_error:
2759 fprintf(stderr, "qemu: invalid resolution or depth\n");
2760 exit(1);
2762 if (*p != 'x')
2763 goto graphic_error;
2764 p++;
2765 h = strtol(p, (char **)&p, 10);
2766 if (h <= 0)
2767 goto graphic_error;
2768 if (*p == 'x') {
2769 p++;
2770 depth = strtol(p, (char **)&p, 10);
2771 if (depth != 8 && depth != 15 && depth != 16 &&
2772 depth != 24 && depth != 32)
2773 goto graphic_error;
2774 } else if (*p == '\0') {
2775 depth = graphic_depth;
2776 } else {
2777 goto graphic_error;
2780 graphic_width = w;
2781 graphic_height = h;
2782 graphic_depth = depth;
2784 break;
2785 case QEMU_OPTION_echr:
2787 char *r;
2788 term_escape_char = strtol(optarg, &r, 0);
2789 if (r == optarg)
2790 printf("Bad argument to echr\n");
2791 break;
2793 case QEMU_OPTION_monitor:
2794 monitor_parse(optarg, "readline");
2795 default_monitor = 0;
2796 break;
2797 case QEMU_OPTION_qmp:
2798 monitor_parse(optarg, "control");
2799 default_monitor = 0;
2800 break;
2801 case QEMU_OPTION_mon:
2802 opts = qemu_opts_parse(&qemu_mon_opts, optarg, 1);
2803 if (!opts) {
2804 exit(1);
2806 default_monitor = 0;
2807 break;
2808 case QEMU_OPTION_chardev:
2809 opts = qemu_opts_parse(&qemu_chardev_opts, optarg, 1);
2810 if (!opts) {
2811 exit(1);
2813 break;
2814 #ifdef CONFIG_LINUX
2815 case QEMU_OPTION_fsdev:
2816 opts = qemu_opts_parse(&qemu_fsdev_opts, optarg, 1);
2817 if (!opts) {
2818 fprintf(stderr, "parse error: %s\n", optarg);
2819 exit(1);
2821 break;
2822 case QEMU_OPTION_virtfs: {
2823 char *arg_fsdev = NULL;
2824 char *arg_9p = NULL;
2825 int len = 0;
2827 opts = qemu_opts_parse(&qemu_virtfs_opts, optarg, 1);
2828 if (!opts) {
2829 fprintf(stderr, "parse error: %s\n", optarg);
2830 exit(1);
2833 len = strlen(",id=,path=");
2834 len += strlen(qemu_opt_get(opts, "fstype"));
2835 len += strlen(qemu_opt_get(opts, "mount_tag"));
2836 len += strlen(qemu_opt_get(opts, "path"));
2837 arg_fsdev = qemu_malloc((len + 1) * sizeof(*arg_fsdev));
2839 if (!arg_fsdev) {
2840 fprintf(stderr, "No memory to parse -fsdev for %s\n",
2841 optarg);
2842 exit(1);
2845 sprintf(arg_fsdev, "%s,id=%s,path=%s",
2846 qemu_opt_get(opts, "fstype"),
2847 qemu_opt_get(opts, "mount_tag"),
2848 qemu_opt_get(opts, "path"));
2850 len = strlen("virtio-9p-pci,fsdev=,mount_tag=");
2851 len += 2*strlen(qemu_opt_get(opts, "mount_tag"));
2852 arg_9p = qemu_malloc((len + 1) * sizeof(*arg_9p));
2854 if (!arg_9p) {
2855 fprintf(stderr, "No memory to parse -device for %s\n",
2856 optarg);
2857 exit(1);
2860 sprintf(arg_9p, "virtio-9p-pci,fsdev=%s,mount_tag=%s",
2861 qemu_opt_get(opts, "mount_tag"),
2862 qemu_opt_get(opts, "mount_tag"));
2864 if (!qemu_opts_parse(&qemu_fsdev_opts, arg_fsdev, 1)) {
2865 fprintf(stderr, "parse error [fsdev]: %s\n", optarg);
2866 exit(1);
2869 if (!qemu_opts_parse(&qemu_device_opts, arg_9p, 1)) {
2870 fprintf(stderr, "parse error [device]: %s\n", optarg);
2871 exit(1);
2874 qemu_free(arg_fsdev);
2875 qemu_free(arg_9p);
2876 break;
2878 #endif
2879 case QEMU_OPTION_serial:
2880 add_device_config(DEV_SERIAL, optarg);
2881 default_serial = 0;
2882 if (strncmp(optarg, "mon:", 4) == 0) {
2883 default_monitor = 0;
2885 break;
2886 case QEMU_OPTION_watchdog:
2887 if (watchdog) {
2888 fprintf(stderr,
2889 "qemu: only one watchdog option may be given\n");
2890 return 1;
2892 watchdog = optarg;
2893 break;
2894 case QEMU_OPTION_watchdog_action:
2895 if (select_watchdog_action(optarg) == -1) {
2896 fprintf(stderr, "Unknown -watchdog-action parameter\n");
2897 exit(1);
2899 break;
2900 case QEMU_OPTION_virtiocon:
2901 add_device_config(DEV_VIRTCON, optarg);
2902 default_virtcon = 0;
2903 if (strncmp(optarg, "mon:", 4) == 0) {
2904 default_monitor = 0;
2906 break;
2907 case QEMU_OPTION_parallel:
2908 add_device_config(DEV_PARALLEL, optarg);
2909 default_parallel = 0;
2910 if (strncmp(optarg, "mon:", 4) == 0) {
2911 default_monitor = 0;
2913 break;
2914 case QEMU_OPTION_debugcon:
2915 add_device_config(DEV_DEBUGCON, optarg);
2916 break;
2917 case QEMU_OPTION_loadvm:
2918 loadvm = optarg;
2919 break;
2920 case QEMU_OPTION_full_screen:
2921 full_screen = 1;
2922 break;
2923 #ifdef CONFIG_SDL
2924 case QEMU_OPTION_no_frame:
2925 no_frame = 1;
2926 break;
2927 case QEMU_OPTION_alt_grab:
2928 alt_grab = 1;
2929 break;
2930 case QEMU_OPTION_ctrl_grab:
2931 ctrl_grab = 1;
2932 break;
2933 case QEMU_OPTION_no_quit:
2934 no_quit = 1;
2935 break;
2936 case QEMU_OPTION_sdl:
2937 display_type = DT_SDL;
2938 break;
2939 #endif
2940 case QEMU_OPTION_pidfile:
2941 pid_file = optarg;
2942 break;
2943 case QEMU_OPTION_win2k_hack:
2944 win2k_install_hack = 1;
2945 break;
2946 case QEMU_OPTION_rtc_td_hack:
2947 rtc_td_hack = 1;
2948 break;
2949 case QEMU_OPTION_acpitable:
2950 do_acpitable_option(optarg);
2951 break;
2952 case QEMU_OPTION_smbios:
2953 do_smbios_option(optarg);
2954 break;
2955 #ifdef KVM_UPSTREAM
2956 case QEMU_OPTION_enable_kvm:
2957 kvm_allowed = 1;
2958 #endif
2959 break;
2960 case QEMU_OPTION_no_kvm:
2961 kvm_allowed = 0;
2962 break;
2963 case QEMU_OPTION_no_kvm_irqchip: {
2964 kvm_irqchip = 0;
2965 kvm_pit = 0;
2966 break;
2968 case QEMU_OPTION_no_kvm_pit: {
2969 kvm_pit = 0;
2970 break;
2972 case QEMU_OPTION_no_kvm_pit_reinjection: {
2973 kvm_pit_reinject = 0;
2974 break;
2976 case QEMU_OPTION_enable_nesting: {
2977 kvm_nested = 1;
2978 break;
2980 #if defined(TARGET_I386) || defined(TARGET_X86_64) || defined(TARGET_IA64) || defined(__linux__)
2981 case QEMU_OPTION_pcidevice:
2982 if (assigned_devices_index >= MAX_DEV_ASSIGN_CMDLINE) {
2983 fprintf(stderr, "Too many assigned devices\n");
2984 exit(1);
2986 assigned_devices[assigned_devices_index] = optarg;
2987 assigned_devices_index++;
2988 break;
2989 #endif
2990 case QEMU_OPTION_usb:
2991 usb_enabled = 1;
2992 break;
2993 case QEMU_OPTION_usbdevice:
2994 usb_enabled = 1;
2995 add_device_config(DEV_USB, optarg);
2996 break;
2997 case QEMU_OPTION_device:
2998 if (!qemu_opts_parse(&qemu_device_opts, optarg, 1)) {
2999 exit(1);
3001 break;
3002 case QEMU_OPTION_smp:
3003 smp_parse(optarg);
3004 if (smp_cpus < 1) {
3005 fprintf(stderr, "Invalid number of CPUs\n");
3006 exit(1);
3008 if (max_cpus < smp_cpus) {
3009 fprintf(stderr, "maxcpus must be equal to or greater than "
3010 "smp\n");
3011 exit(1);
3013 if (max_cpus > 255) {
3014 fprintf(stderr, "Unsupported number of maxcpus\n");
3015 exit(1);
3017 break;
3018 case QEMU_OPTION_vnc:
3019 display_type = DT_VNC;
3020 vnc_display = optarg;
3021 break;
3022 case QEMU_OPTION_no_acpi:
3023 acpi_enabled = 0;
3024 break;
3025 case QEMU_OPTION_no_hpet:
3026 no_hpet = 1;
3027 break;
3028 case QEMU_OPTION_balloon:
3029 if (balloon_parse(optarg) < 0) {
3030 fprintf(stderr, "Unknown -balloon argument %s\n", optarg);
3031 exit(1);
3033 break;
3034 case QEMU_OPTION_no_reboot:
3035 no_reboot = 1;
3036 break;
3037 case QEMU_OPTION_no_shutdown:
3038 no_shutdown = 1;
3039 break;
3040 case QEMU_OPTION_show_cursor:
3041 cursor_hide = 0;
3042 break;
3043 case QEMU_OPTION_uuid:
3044 if(qemu_uuid_parse(optarg, qemu_uuid) < 0) {
3045 fprintf(stderr, "Fail to parse UUID string."
3046 " Wrong format.\n");
3047 exit(1);
3049 break;
3050 #ifndef _WIN32
3051 case QEMU_OPTION_daemonize:
3052 daemonize = 1;
3053 break;
3054 #endif
3055 case QEMU_OPTION_option_rom:
3056 if (nb_option_roms >= MAX_OPTION_ROMS) {
3057 fprintf(stderr, "Too many option ROMs\n");
3058 exit(1);
3060 option_rom[nb_option_roms] = optarg;
3061 nb_option_roms++;
3062 break;
3063 case QEMU_OPTION_semihosting:
3064 semihosting_enabled = 1;
3065 break;
3066 case QEMU_OPTION_tdf:
3067 time_drift_fix = 1;
3068 break;
3069 case QEMU_OPTION_kvm_shadow_memory:
3070 kvm_shadow_memory = (int64_t)atoi(optarg) * 1024 * 1024 / 4096;
3071 break;
3072 case QEMU_OPTION_name:
3073 qemu_name = qemu_strdup(optarg);
3075 char *p = strchr(qemu_name, ',');
3076 if (p != NULL) {
3077 *p++ = 0;
3078 if (strncmp(p, "process=", 8)) {
3079 fprintf(stderr, "Unknown subargument %s to -name", p);
3080 exit(1);
3082 p += 8;
3083 set_proc_name(p);
3086 break;
3087 case QEMU_OPTION_prom_env:
3088 if (nb_prom_envs >= MAX_PROM_ENVS) {
3089 fprintf(stderr, "Too many prom variables\n");
3090 exit(1);
3092 prom_envs[nb_prom_envs] = optarg;
3093 nb_prom_envs++;
3094 break;
3095 case QEMU_OPTION_old_param:
3096 old_param = 1;
3097 break;
3098 case QEMU_OPTION_clock:
3099 configure_alarms(optarg);
3100 break;
3101 case QEMU_OPTION_startdate:
3102 configure_rtc_date_offset(optarg, 1);
3103 break;
3104 case QEMU_OPTION_rtc:
3105 opts = qemu_opts_parse(&qemu_rtc_opts, optarg, 0);
3106 if (!opts) {
3107 exit(1);
3109 configure_rtc(opts);
3110 break;
3111 case QEMU_OPTION_tb_size:
3112 tb_size = strtol(optarg, NULL, 0);
3113 if (tb_size < 0)
3114 tb_size = 0;
3115 break;
3116 case QEMU_OPTION_icount:
3117 icount_option = optarg;
3118 break;
3119 case QEMU_OPTION_incoming:
3120 incoming = optarg;
3121 break;
3122 case QEMU_OPTION_nodefaults:
3123 default_serial = 0;
3124 default_parallel = 0;
3125 default_virtcon = 0;
3126 default_monitor = 0;
3127 default_vga = 0;
3128 default_net = 0;
3129 default_floppy = 0;
3130 default_cdrom = 0;
3131 default_sdcard = 0;
3132 break;
3133 #ifndef _WIN32
3134 case QEMU_OPTION_nvram:
3135 nvram = optarg;
3136 break;
3137 #endif
3138 case QEMU_OPTION_xen_domid:
3139 if (!(xen_available())) {
3140 printf("Option %s not supported for this target\n", popt->name);
3141 exit(1);
3143 xen_domid = atoi(optarg);
3144 break;
3145 case QEMU_OPTION_xen_create:
3146 if (!(xen_available())) {
3147 printf("Option %s not supported for this target\n", popt->name);
3148 exit(1);
3150 xen_mode = XEN_CREATE;
3151 break;
3152 case QEMU_OPTION_xen_attach:
3153 if (!(xen_available())) {
3154 printf("Option %s not supported for this target\n", popt->name);
3155 exit(1);
3157 xen_mode = XEN_ATTACH;
3158 break;
3159 case QEMU_OPTION_readconfig:
3161 int ret = qemu_read_config_file(optarg);
3162 if (ret < 0) {
3163 fprintf(stderr, "read config %s: %s\n", optarg,
3164 strerror(-ret));
3165 exit(1);
3167 break;
3169 case QEMU_OPTION_writeconfig:
3171 FILE *fp;
3172 if (strcmp(optarg, "-") == 0) {
3173 fp = stdout;
3174 } else {
3175 fp = fopen(optarg, "w");
3176 if (fp == NULL) {
3177 fprintf(stderr, "open %s: %s\n", optarg, strerror(errno));
3178 exit(1);
3181 qemu_config_write(fp);
3182 fclose(fp);
3183 break;
3185 default:
3186 os_parse_cmd_args(popt->index, optarg);
3190 loc_set_none();
3192 /* If no data_dir is specified then try to find it relative to the
3193 executable path. */
3194 if (!data_dir) {
3195 data_dir = os_find_datadir(argv[0]);
3197 /* If all else fails use the install patch specified when building. */
3198 if (!data_dir) {
3199 data_dir = CONFIG_QEMU_DATADIR;
3203 * Default to max_cpus = smp_cpus, in case the user doesn't
3204 * specify a max_cpus value.
3206 if (!max_cpus)
3207 max_cpus = smp_cpus;
3209 machine->max_cpus = machine->max_cpus ?: 1; /* Default to UP */
3210 if (smp_cpus > machine->max_cpus) {
3211 fprintf(stderr, "Number of SMP cpus requested (%d), exceeds max cpus "
3212 "supported by machine `%s' (%d)\n", smp_cpus, machine->name,
3213 machine->max_cpus);
3214 exit(1);
3217 qemu_opts_foreach(&qemu_device_opts, default_driver_check, NULL, 0);
3218 qemu_opts_foreach(&qemu_global_opts, default_driver_check, NULL, 0);
3220 if (machine->no_serial) {
3221 default_serial = 0;
3223 if (machine->no_parallel) {
3224 default_parallel = 0;
3226 if (!machine->use_virtcon) {
3227 default_virtcon = 0;
3229 if (machine->no_vga) {
3230 default_vga = 0;
3232 if (machine->no_floppy) {
3233 default_floppy = 0;
3235 if (machine->no_cdrom) {
3236 default_cdrom = 0;
3238 if (machine->no_sdcard) {
3239 default_sdcard = 0;
3242 if (display_type == DT_NOGRAPHIC) {
3243 if (default_parallel)
3244 add_device_config(DEV_PARALLEL, "null");
3245 if (default_serial && default_monitor) {
3246 add_device_config(DEV_SERIAL, "mon:stdio");
3247 } else if (default_virtcon && default_monitor) {
3248 add_device_config(DEV_VIRTCON, "mon:stdio");
3249 } else {
3250 if (default_serial)
3251 add_device_config(DEV_SERIAL, "stdio");
3252 if (default_virtcon)
3253 add_device_config(DEV_VIRTCON, "stdio");
3254 if (default_monitor)
3255 monitor_parse("stdio", "readline");
3257 } else {
3258 if (default_serial)
3259 add_device_config(DEV_SERIAL, "vc:80Cx24C");
3260 if (default_parallel)
3261 add_device_config(DEV_PARALLEL, "vc:80Cx24C");
3262 if (default_monitor)
3263 monitor_parse("vc:80Cx24C", "readline");
3264 if (default_virtcon)
3265 add_device_config(DEV_VIRTCON, "vc:80Cx24C");
3267 if (default_vga)
3268 vga_interface_type = VGA_CIRRUS;
3270 socket_init();
3272 if (qemu_opts_foreach(&qemu_chardev_opts, chardev_init_func, NULL, 1) != 0)
3273 exit(1);
3274 #ifdef CONFIG_LINUX
3275 if (qemu_opts_foreach(&qemu_fsdev_opts, fsdev_init_func, NULL, 1) != 0) {
3276 exit(1);
3278 #endif
3280 #ifndef _WIN32
3281 if (daemonize) {
3282 pid_t pid;
3284 if (pipe(fds) == -1)
3285 exit(1);
3287 pid = fork();
3288 if (pid > 0) {
3289 uint8_t status;
3290 ssize_t len;
3292 close(fds[1]);
3294 again:
3295 len = read(fds[0], &status, 1);
3296 if (len == -1 && (errno == EINTR))
3297 goto again;
3299 if (len != 1)
3300 exit(1);
3301 else if (status == 1) {
3302 fprintf(stderr, "Could not acquire pidfile: %s\n", strerror(errno));
3303 exit(1);
3304 } else
3305 exit(0);
3306 } else if (pid < 0)
3307 exit(1);
3309 close(fds[0]);
3310 qemu_set_cloexec(fds[1]);
3312 setsid();
3314 pid = fork();
3315 if (pid > 0)
3316 exit(0);
3317 else if (pid < 0)
3318 exit(1);
3320 umask(027);
3322 signal(SIGTSTP, SIG_IGN);
3323 signal(SIGTTOU, SIG_IGN);
3324 signal(SIGTTIN, SIG_IGN);
3326 #endif
3328 if (pid_file && qemu_create_pidfile(pid_file) != 0) {
3329 #ifndef _WIN32
3330 if (daemonize) {
3331 uint8_t status = 1;
3332 if (write(fds[1], &status, 1) != 1) {
3333 perror("daemonize. Writing to pipe\n");
3335 } else
3336 #endif
3337 fprintf(stderr, "Could not acquire pid file: %s\n", strerror(errno));
3338 exit(1);
3341 if (kvm_allowed) {
3342 int ret = kvm_init(smp_cpus);
3343 if (ret < 0) {
3344 #if defined(KVM_UPSTREAM) || defined(CONFIG_NO_CPU_EMULATION)
3345 if (!kvm_available()) {
3346 printf("KVM not supported for this target\n");
3347 } else {
3348 fprintf(stderr, "failed to initialize KVM: %s\n", strerror(-ret));
3350 exit(1);
3351 #endif
3352 #ifdef CONFIG_KVM
3353 fprintf(stderr, "Could not initialize KVM, will disable KVM support\n");
3354 kvm_allowed = 0;
3355 #endif
3359 if (qemu_init_main_loop()) {
3360 fprintf(stderr, "qemu_init_main_loop failed\n");
3361 exit(1);
3363 linux_boot = (kernel_filename != NULL);
3365 if (!linux_boot && *kernel_cmdline != '\0') {
3366 fprintf(stderr, "-append only allowed with -kernel option\n");
3367 exit(1);
3370 if (!linux_boot && initrd_filename != NULL) {
3371 fprintf(stderr, "-initrd only allowed with -kernel option\n");
3372 exit(1);
3375 #ifndef _WIN32
3376 /* Win32 doesn't support line-buffering and requires size >= 2 */
3377 setvbuf(stdout, NULL, _IOLBF, 0);
3378 #endif
3380 if (init_timer_alarm() < 0) {
3381 fprintf(stderr, "could not initialize alarm timer\n");
3382 exit(1);
3384 configure_icount(icount_option);
3386 if (net_init_clients() < 0) {
3387 exit(1);
3390 /* init the bluetooth world */
3391 if (foreach_device_config(DEV_BT, bt_parse))
3392 exit(1);
3394 /* init the memory */
3395 if (ram_size == 0)
3396 ram_size = DEFAULT_RAM_SIZE * 1024 * 1024;
3398 /* init the dynamic translator */
3399 cpu_exec_init_all(tb_size * 1024 * 1024);
3401 bdrv_init_with_whitelist();
3403 blk_mig_init();
3405 if (default_cdrom) {
3406 /* we always create the cdrom drive, even if no disk is there */
3407 drive_add(NULL, CDROM_ALIAS);
3410 if (default_floppy) {
3411 /* we always create at least one floppy */
3412 drive_add(NULL, FD_ALIAS, 0);
3415 if (default_sdcard) {
3416 /* we always create one sd slot, even if no card is in it */
3417 drive_add(NULL, SD_ALIAS);
3420 /* open the virtual block devices */
3421 if (snapshot)
3422 qemu_opts_foreach(&qemu_drive_opts, drive_enable_snapshot, NULL, 0);
3423 if (qemu_opts_foreach(&qemu_drive_opts, drive_init_func, machine, 1) != 0)
3424 exit(1);
3426 register_savevm_live("ram", 0, 3, NULL, ram_save_live, NULL,
3427 ram_load, NULL);
3429 if (nb_numa_nodes > 0) {
3430 int i;
3432 if (nb_numa_nodes > smp_cpus) {
3433 nb_numa_nodes = smp_cpus;
3436 /* If no memory size if given for any node, assume the default case
3437 * and distribute the available memory equally across all nodes
3439 for (i = 0; i < nb_numa_nodes; i++) {
3440 if (node_mem[i] != 0)
3441 break;
3443 if (i == nb_numa_nodes) {
3444 uint64_t usedmem = 0;
3446 /* On Linux, the each node's border has to be 8MB aligned,
3447 * the final node gets the rest.
3449 for (i = 0; i < nb_numa_nodes - 1; i++) {
3450 node_mem[i] = (ram_size / nb_numa_nodes) & ~((1 << 23UL) - 1);
3451 usedmem += node_mem[i];
3453 node_mem[i] = ram_size - usedmem;
3456 for (i = 0; i < nb_numa_nodes; i++) {
3457 if (node_cpumask[i] != 0)
3458 break;
3460 /* assigning the VCPUs round-robin is easier to implement, guest OSes
3461 * must cope with this anyway, because there are BIOSes out there in
3462 * real machines which also use this scheme.
3464 if (i == nb_numa_nodes) {
3465 for (i = 0; i < smp_cpus; i++) {
3466 node_cpumask[i % nb_numa_nodes] |= 1 << i;
3471 if (qemu_opts_foreach(&qemu_mon_opts, mon_init_func, NULL, 1) != 0) {
3472 exit(1);
3475 if (foreach_device_config(DEV_SERIAL, serial_parse) < 0)
3476 exit(1);
3477 if (foreach_device_config(DEV_PARALLEL, parallel_parse) < 0)
3478 exit(1);
3479 if (foreach_device_config(DEV_VIRTCON, virtcon_parse) < 0)
3480 exit(1);
3481 if (foreach_device_config(DEV_DEBUGCON, debugcon_parse) < 0)
3482 exit(1);
3484 module_call_init(MODULE_INIT_DEVICE);
3486 if (qemu_opts_foreach(&qemu_device_opts, device_help_func, NULL, 0) != 0)
3487 exit(0);
3489 if (watchdog) {
3490 i = select_watchdog(watchdog);
3491 if (i > 0)
3492 exit (i == 1 ? 1 : 0);
3495 if (machine->compat_props) {
3496 qdev_prop_register_global_list(machine->compat_props);
3498 qemu_add_globals();
3500 machine->init(ram_size, boot_devices,
3501 kernel_filename, kernel_cmdline, initrd_filename, cpu_model);
3503 cpu_synchronize_all_post_init();
3505 /* must be after terminal init, SDL library changes signal handlers */
3506 os_setup_signal_handling();
3508 set_numa_modes();
3510 current_machine = machine;
3512 /* init USB devices */
3513 if (usb_enabled) {
3514 if (foreach_device_config(DEV_USB, usb_parse) < 0)
3515 exit(1);
3518 /* init generic devices */
3519 if (qemu_opts_foreach(&qemu_device_opts, device_init_func, NULL, 1) != 0)
3520 exit(1);
3522 net_check_clients();
3524 /* just use the first displaystate for the moment */
3525 ds = get_displaystate();
3527 if (display_type == DT_DEFAULT) {
3528 #if defined(CONFIG_SDL) || defined(CONFIG_COCOA)
3529 display_type = DT_SDL;
3530 #else
3531 display_type = DT_VNC;
3532 vnc_display = "localhost:0,to=99";
3533 show_vnc_port = 1;
3534 #endif
3538 switch (display_type) {
3539 case DT_NOGRAPHIC:
3540 break;
3541 #if defined(CONFIG_CURSES)
3542 case DT_CURSES:
3543 curses_display_init(ds, full_screen);
3544 break;
3545 #endif
3546 #if defined(CONFIG_SDL)
3547 case DT_SDL:
3548 sdl_display_init(ds, full_screen, no_frame);
3549 break;
3550 #elif defined(CONFIG_COCOA)
3551 case DT_SDL:
3552 cocoa_display_init(ds, full_screen);
3553 break;
3554 #endif
3555 case DT_VNC:
3556 vnc_display_init(ds);
3557 if (vnc_display_open(ds, vnc_display) < 0)
3558 exit(1);
3560 if (show_vnc_port) {
3561 printf("VNC server running on `%s'\n", vnc_display_local_addr(ds));
3563 break;
3564 default:
3565 break;
3567 dpy_resize(ds);
3569 dcl = ds->listeners;
3570 while (dcl != NULL) {
3571 if (dcl->dpy_refresh != NULL) {
3572 ds->gui_timer = qemu_new_timer(rt_clock, gui_update, ds);
3573 qemu_mod_timer(ds->gui_timer, qemu_get_clock(rt_clock));
3574 break;
3576 dcl = dcl->next;
3579 if (display_type == DT_NOGRAPHIC || display_type == DT_VNC) {
3580 nographic_timer = qemu_new_timer(rt_clock, nographic_update, NULL);
3581 qemu_mod_timer(nographic_timer, qemu_get_clock(rt_clock));
3584 text_consoles_set_display(ds);
3586 if (gdbstub_dev && gdbserver_start(gdbstub_dev) < 0) {
3587 fprintf(stderr, "qemu: could not open gdbserver on device '%s'\n",
3588 gdbstub_dev);
3589 exit(1);
3592 qdev_machine_creation_done();
3594 if (rom_load_all() != 0) {
3595 fprintf(stderr, "rom loading failed\n");
3596 exit(1);
3599 qemu_system_reset();
3600 if (loadvm) {
3601 if (load_vmstate(loadvm) < 0) {
3602 autostart = 0;
3606 if (incoming) {
3607 qemu_start_incoming_migration(incoming);
3608 } else if (autostart) {
3609 vm_start();
3612 #ifndef _WIN32
3613 if (daemonize) {
3614 uint8_t status = 0;
3615 ssize_t len;
3617 again1:
3618 len = write(fds[1], &status, 1);
3619 if (len == -1 && (errno == EINTR))
3620 goto again1;
3622 if (len != 1)
3623 exit(1);
3625 if (chdir("/")) {
3626 perror("not able to chdir to /");
3627 exit(1);
3629 TFR(fd = qemu_open("/dev/null", O_RDWR));
3630 if (fd == -1)
3631 exit(1);
3634 os_change_root();
3635 os_change_process_uid();
3637 if (daemonize) {
3638 dup2(fd, 0);
3639 dup2(fd, 1);
3640 dup2(fd, 2);
3642 close(fd);
3644 #endif
3646 main_loop();
3647 quit_timers();
3648 net_cleanup();
3650 return 0;