boot: remove unused boot_devices_bitmap variable
[qemu.git] / vl.c
blobea79ac4437a7c26953fa9ec2750556cce33a4d36
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 <pwd.h>
38 #include <sys/times.h>
39 #include <sys/wait.h>
40 #include <termios.h>
41 #include <sys/mman.h>
42 #include <sys/ioctl.h>
43 #include <sys/resource.h>
44 #include <sys/socket.h>
45 #include <netinet/in.h>
46 #include <net/if.h>
47 #include <arpa/inet.h>
48 #include <dirent.h>
49 #include <netdb.h>
50 #include <sys/select.h>
51 #ifdef CONFIG_BSD
52 #include <sys/stat.h>
53 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__DragonFly__)
54 #include <libutil.h>
55 #include <sys/sysctl.h>
56 #else
57 #include <util.h>
58 #endif
59 #else
60 #ifdef __linux__
61 #include <pty.h>
62 #include <malloc.h>
63 #include <sys/prctl.h>
65 #include <linux/ppdev.h>
66 #include <linux/parport.h>
67 #endif
68 #ifdef __sun__
69 #include <sys/stat.h>
70 #include <sys/ethernet.h>
71 #include <sys/sockio.h>
72 #include <netinet/arp.h>
73 #include <netinet/in.h>
74 #include <netinet/in_systm.h>
75 #include <netinet/ip.h>
76 #include <netinet/ip_icmp.h> // must come after ip.h
77 #include <netinet/udp.h>
78 #include <netinet/tcp.h>
79 #include <net/if.h>
80 #include <syslog.h>
81 #include <stropts.h>
82 /* See MySQL bug #7156 (http://bugs.mysql.com/bug.php?id=7156) for
83 discussion about Solaris header problems */
84 extern int madvise(caddr_t, size_t, int);
85 #endif
86 #endif
87 #endif
89 #if defined(__OpenBSD__)
90 #include <util.h>
91 #endif
93 #if defined(CONFIG_VDE)
94 #include <libvdeplug.h>
95 #endif
97 #ifdef _WIN32
98 #include <windows.h>
99 #endif
101 #ifdef CONFIG_SDL
102 #if defined(__APPLE__) || defined(main)
103 #include <SDL.h>
104 int qemu_main(int argc, char **argv, char **envp);
105 int main(int argc, char **argv)
107 return qemu_main(argc, argv, NULL);
109 #undef main
110 #define main qemu_main
111 #endif
112 #endif /* CONFIG_SDL */
114 #ifdef CONFIG_COCOA
115 #undef main
116 #define main qemu_main
117 #endif /* CONFIG_COCOA */
119 #include "hw/hw.h"
120 #include "hw/boards.h"
121 #include "hw/usb.h"
122 #include "hw/pcmcia.h"
123 #include "hw/pc.h"
124 #include "hw/isa.h"
125 #include "hw/baum.h"
126 #include "hw/bt.h"
127 #include "hw/watchdog.h"
128 #include "hw/smbios.h"
129 #include "hw/xen.h"
130 #include "hw/qdev.h"
131 #include "hw/loader.h"
132 #include "bt-host.h"
133 #include "net.h"
134 #include "net/slirp.h"
135 #include "monitor.h"
136 #include "console.h"
137 #include "sysemu.h"
138 #include "gdbstub.h"
139 #include "qemu-timer.h"
140 #include "qemu-char.h"
141 #include "cache-utils.h"
142 #include "block.h"
143 #include "block_int.h"
144 #include "block-migration.h"
145 #include "dma.h"
146 #include "audio/audio.h"
147 #include "migration.h"
148 #include "kvm.h"
149 #include "qemu-option.h"
150 #include "qemu-config.h"
151 #include "qemu-objects.h"
153 #include "disas.h"
155 #include "qemu_socket.h"
157 #include "slirp/libslirp.h"
159 #include "qemu-queue.h"
160 #include "cpus.h"
161 #include "arch_init.h"
163 //#define DEBUG_NET
164 //#define DEBUG_SLIRP
166 #define DEFAULT_RAM_SIZE 128
168 #define MAX_VIRTIO_CONSOLES 1
170 static const char *data_dir;
171 const char *bios_name = NULL;
172 /* Note: drives_table[MAX_DRIVES] is a dummy block driver if none available
173 to store the VM snapshots */
174 struct drivelist drives = QTAILQ_HEAD_INITIALIZER(drives);
175 struct driveoptlist driveopts = QTAILQ_HEAD_INITIALIZER(driveopts);
176 enum vga_retrace_method vga_retrace_method = VGA_RETRACE_DUMB;
177 DisplayType display_type = DT_DEFAULT;
178 const char* keyboard_layout = NULL;
179 ram_addr_t ram_size;
180 const char *mem_path = NULL;
181 #ifdef MAP_POPULATE
182 int mem_prealloc = 0; /* force preallocation of physical target memory */
183 #endif
184 int nb_nics;
185 NICInfo nd_table[MAX_NICS];
186 int vm_running;
187 int autostart;
188 static int rtc_utc = 1;
189 static int rtc_date_offset = -1; /* -1 means no change */
190 QEMUClock *rtc_clock;
191 int vga_interface_type = VGA_NONE;
192 static int full_screen = 0;
193 #ifdef CONFIG_SDL
194 static int no_frame = 0;
195 #endif
196 int no_quit = 0;
197 CharDriverState *serial_hds[MAX_SERIAL_PORTS];
198 CharDriverState *parallel_hds[MAX_PARALLEL_PORTS];
199 CharDriverState *virtcon_hds[MAX_VIRTIO_CONSOLES];
200 int win2k_install_hack = 0;
201 int rtc_td_hack = 0;
202 int usb_enabled = 0;
203 int singlestep = 0;
204 int smp_cpus = 1;
205 int max_cpus = 0;
206 int smp_cores = 1;
207 int smp_threads = 1;
208 const char *vnc_display;
209 int acpi_enabled = 1;
210 int no_hpet = 0;
211 int fd_bootchk = 1;
212 int no_reboot = 0;
213 int no_shutdown = 0;
214 int cursor_hide = 1;
215 int graphic_rotate = 0;
216 uint8_t irq0override = 1;
217 #ifndef _WIN32
218 int daemonize = 0;
219 #endif
220 const char *watchdog;
221 const char *option_rom[MAX_OPTION_ROMS];
222 int nb_option_roms;
223 int semihosting_enabled = 0;
224 int old_param = 0;
225 const char *qemu_name;
226 int alt_grab = 0;
227 int ctrl_grab = 0;
228 unsigned int nb_prom_envs = 0;
229 const char *prom_envs[MAX_PROM_ENVS];
230 int boot_menu;
232 int nb_numa_nodes;
233 uint64_t node_mem[MAX_NODES];
234 uint64_t node_cpumask[MAX_NODES];
236 static QEMUTimer *nographic_timer;
238 uint8_t qemu_uuid[16];
240 static QEMUBootSetHandler *boot_set_handler;
241 static void *boot_set_opaque;
243 int kvm_allowed = 0;
244 uint32_t xen_domid;
245 enum xen_mode xen_mode = XEN_EMULATE;
247 #ifdef SIGRTMIN
248 #define SIG_IPI (SIGRTMIN+4)
249 #else
250 #define SIG_IPI SIGUSR1
251 #endif
253 static int default_serial = 1;
254 static int default_parallel = 1;
255 static int default_virtcon = 1;
256 static int default_monitor = 1;
257 static int default_vga = 1;
258 static int default_floppy = 1;
259 static int default_cdrom = 1;
260 static int default_sdcard = 1;
262 static struct {
263 const char *driver;
264 int *flag;
265 } default_list[] = {
266 { .driver = "isa-serial", .flag = &default_serial },
267 { .driver = "isa-parallel", .flag = &default_parallel },
268 { .driver = "isa-fdc", .flag = &default_floppy },
269 { .driver = "ide-drive", .flag = &default_cdrom },
270 { .driver = "virtio-serial-pci", .flag = &default_virtcon },
271 { .driver = "virtio-serial-s390", .flag = &default_virtcon },
272 { .driver = "virtio-serial", .flag = &default_virtcon },
273 { .driver = "VGA", .flag = &default_vga },
274 { .driver = "cirrus-vga", .flag = &default_vga },
275 { .driver = "vmware-svga", .flag = &default_vga },
278 static int default_driver_check(QemuOpts *opts, void *opaque)
280 const char *driver = qemu_opt_get(opts, "driver");
281 int i;
283 if (!driver)
284 return 0;
285 for (i = 0; i < ARRAY_SIZE(default_list); i++) {
286 if (strcmp(default_list[i].driver, driver) != 0)
287 continue;
288 *(default_list[i].flag) = 0;
290 return 0;
293 /***********************************************************/
295 static void set_proc_name(const char *s)
297 #if defined(__linux__) && defined(PR_SET_NAME)
298 char name[16];
299 if (!s)
300 return;
301 name[sizeof(name) - 1] = 0;
302 strncpy(name, s, sizeof(name));
303 /* Could rewrite argv[0] too, but that's a bit more complicated.
304 This simple way is enough for `top'. */
305 prctl(PR_SET_NAME, name);
306 #endif
309 /***********************************************************/
310 /* real time host monotonic timer */
312 /* compute with 96 bit intermediate result: (a*b)/c */
313 uint64_t muldiv64(uint64_t a, uint32_t b, uint32_t c)
315 union {
316 uint64_t ll;
317 struct {
318 #ifdef HOST_WORDS_BIGENDIAN
319 uint32_t high, low;
320 #else
321 uint32_t low, high;
322 #endif
323 } l;
324 } u, res;
325 uint64_t rl, rh;
327 u.ll = a;
328 rl = (uint64_t)u.l.low * (uint64_t)b;
329 rh = (uint64_t)u.l.high * (uint64_t)b;
330 rh += (rl >> 32);
331 res.l.high = rh / c;
332 res.l.low = (((rh % c) << 32) + (rl & 0xffffffff)) / c;
333 return res.ll;
336 /***********************************************************/
337 /* host time/date access */
338 void qemu_get_timedate(struct tm *tm, int offset)
340 time_t ti;
341 struct tm *ret;
343 time(&ti);
344 ti += offset;
345 if (rtc_date_offset == -1) {
346 if (rtc_utc)
347 ret = gmtime(&ti);
348 else
349 ret = localtime(&ti);
350 } else {
351 ti -= rtc_date_offset;
352 ret = gmtime(&ti);
355 memcpy(tm, ret, sizeof(struct tm));
358 int qemu_timedate_diff(struct tm *tm)
360 time_t seconds;
362 if (rtc_date_offset == -1)
363 if (rtc_utc)
364 seconds = mktimegm(tm);
365 else
366 seconds = mktime(tm);
367 else
368 seconds = mktimegm(tm) + rtc_date_offset;
370 return seconds - time(NULL);
373 void rtc_change_mon_event(struct tm *tm)
375 QObject *data;
377 data = qobject_from_jsonf("{ 'offset': %d }", qemu_timedate_diff(tm));
378 monitor_protocol_event(QEVENT_RTC_CHANGE, data);
379 qobject_decref(data);
382 static void configure_rtc_date_offset(const char *startdate, int legacy)
384 time_t rtc_start_date;
385 struct tm tm;
387 if (!strcmp(startdate, "now") && legacy) {
388 rtc_date_offset = -1;
389 } else {
390 if (sscanf(startdate, "%d-%d-%dT%d:%d:%d",
391 &tm.tm_year,
392 &tm.tm_mon,
393 &tm.tm_mday,
394 &tm.tm_hour,
395 &tm.tm_min,
396 &tm.tm_sec) == 6) {
397 /* OK */
398 } else if (sscanf(startdate, "%d-%d-%d",
399 &tm.tm_year,
400 &tm.tm_mon,
401 &tm.tm_mday) == 3) {
402 tm.tm_hour = 0;
403 tm.tm_min = 0;
404 tm.tm_sec = 0;
405 } else {
406 goto date_fail;
408 tm.tm_year -= 1900;
409 tm.tm_mon--;
410 rtc_start_date = mktimegm(&tm);
411 if (rtc_start_date == -1) {
412 date_fail:
413 fprintf(stderr, "Invalid date format. Valid formats are:\n"
414 "'2006-06-17T16:01:21' or '2006-06-17'\n");
415 exit(1);
417 rtc_date_offset = time(NULL) - rtc_start_date;
421 static void configure_rtc(QemuOpts *opts)
423 const char *value;
425 value = qemu_opt_get(opts, "base");
426 if (value) {
427 if (!strcmp(value, "utc")) {
428 rtc_utc = 1;
429 } else if (!strcmp(value, "localtime")) {
430 rtc_utc = 0;
431 } else {
432 configure_rtc_date_offset(value, 0);
435 value = qemu_opt_get(opts, "clock");
436 if (value) {
437 if (!strcmp(value, "host")) {
438 rtc_clock = host_clock;
439 } else if (!strcmp(value, "vm")) {
440 rtc_clock = vm_clock;
441 } else {
442 fprintf(stderr, "qemu: invalid option value '%s'\n", value);
443 exit(1);
446 value = qemu_opt_get(opts, "driftfix");
447 if (value) {
448 if (!strcmp(value, "slew")) {
449 rtc_td_hack = 1;
450 } else if (!strcmp(value, "none")) {
451 rtc_td_hack = 0;
452 } else {
453 fprintf(stderr, "qemu: invalid option value '%s'\n", value);
454 exit(1);
459 /***********************************************************/
460 /* Bluetooth support */
461 static int nb_hcis;
462 static int cur_hci;
463 static struct HCIInfo *hci_table[MAX_NICS];
465 static struct bt_vlan_s {
466 struct bt_scatternet_s net;
467 int id;
468 struct bt_vlan_s *next;
469 } *first_bt_vlan;
471 /* find or alloc a new bluetooth "VLAN" */
472 static struct bt_scatternet_s *qemu_find_bt_vlan(int id)
474 struct bt_vlan_s **pvlan, *vlan;
475 for (vlan = first_bt_vlan; vlan != NULL; vlan = vlan->next) {
476 if (vlan->id == id)
477 return &vlan->net;
479 vlan = qemu_mallocz(sizeof(struct bt_vlan_s));
480 vlan->id = id;
481 pvlan = &first_bt_vlan;
482 while (*pvlan != NULL)
483 pvlan = &(*pvlan)->next;
484 *pvlan = vlan;
485 return &vlan->net;
488 static void null_hci_send(struct HCIInfo *hci, const uint8_t *data, int len)
492 static int null_hci_addr_set(struct HCIInfo *hci, const uint8_t *bd_addr)
494 return -ENOTSUP;
497 static struct HCIInfo null_hci = {
498 .cmd_send = null_hci_send,
499 .sco_send = null_hci_send,
500 .acl_send = null_hci_send,
501 .bdaddr_set = null_hci_addr_set,
504 struct HCIInfo *qemu_next_hci(void)
506 if (cur_hci == nb_hcis)
507 return &null_hci;
509 return hci_table[cur_hci++];
512 static struct HCIInfo *hci_init(const char *str)
514 char *endp;
515 struct bt_scatternet_s *vlan = 0;
517 if (!strcmp(str, "null"))
518 /* null */
519 return &null_hci;
520 else if (!strncmp(str, "host", 4) && (str[4] == '\0' || str[4] == ':'))
521 /* host[:hciN] */
522 return bt_host_hci(str[4] ? str + 5 : "hci0");
523 else if (!strncmp(str, "hci", 3)) {
524 /* hci[,vlan=n] */
525 if (str[3]) {
526 if (!strncmp(str + 3, ",vlan=", 6)) {
527 vlan = qemu_find_bt_vlan(strtol(str + 9, &endp, 0));
528 if (*endp)
529 vlan = 0;
531 } else
532 vlan = qemu_find_bt_vlan(0);
533 if (vlan)
534 return bt_new_hci(vlan);
537 fprintf(stderr, "qemu: Unknown bluetooth HCI `%s'.\n", str);
539 return 0;
542 static int bt_hci_parse(const char *str)
544 struct HCIInfo *hci;
545 bdaddr_t bdaddr;
547 if (nb_hcis >= MAX_NICS) {
548 fprintf(stderr, "qemu: Too many bluetooth HCIs (max %i).\n", MAX_NICS);
549 return -1;
552 hci = hci_init(str);
553 if (!hci)
554 return -1;
556 bdaddr.b[0] = 0x52;
557 bdaddr.b[1] = 0x54;
558 bdaddr.b[2] = 0x00;
559 bdaddr.b[3] = 0x12;
560 bdaddr.b[4] = 0x34;
561 bdaddr.b[5] = 0x56 + nb_hcis;
562 hci->bdaddr_set(hci, bdaddr.b);
564 hci_table[nb_hcis++] = hci;
566 return 0;
569 static void bt_vhci_add(int vlan_id)
571 struct bt_scatternet_s *vlan = qemu_find_bt_vlan(vlan_id);
573 if (!vlan->slave)
574 fprintf(stderr, "qemu: warning: adding a VHCI to "
575 "an empty scatternet %i\n", vlan_id);
577 bt_vhci_init(bt_new_hci(vlan));
580 static struct bt_device_s *bt_device_add(const char *opt)
582 struct bt_scatternet_s *vlan;
583 int vlan_id = 0;
584 char *endp = strstr(opt, ",vlan=");
585 int len = (endp ? endp - opt : strlen(opt)) + 1;
586 char devname[10];
588 pstrcpy(devname, MIN(sizeof(devname), len), opt);
590 if (endp) {
591 vlan_id = strtol(endp + 6, &endp, 0);
592 if (*endp) {
593 fprintf(stderr, "qemu: unrecognised bluetooth vlan Id\n");
594 return 0;
598 vlan = qemu_find_bt_vlan(vlan_id);
600 if (!vlan->slave)
601 fprintf(stderr, "qemu: warning: adding a slave device to "
602 "an empty scatternet %i\n", vlan_id);
604 if (!strcmp(devname, "keyboard"))
605 return bt_keyboard_init(vlan);
607 fprintf(stderr, "qemu: unsupported bluetooth device `%s'\n", devname);
608 return 0;
611 static int bt_parse(const char *opt)
613 const char *endp, *p;
614 int vlan;
616 if (strstart(opt, "hci", &endp)) {
617 if (!*endp || *endp == ',') {
618 if (*endp)
619 if (!strstart(endp, ",vlan=", 0))
620 opt = endp + 1;
622 return bt_hci_parse(opt);
624 } else if (strstart(opt, "vhci", &endp)) {
625 if (!*endp || *endp == ',') {
626 if (*endp) {
627 if (strstart(endp, ",vlan=", &p)) {
628 vlan = strtol(p, (char **) &endp, 0);
629 if (*endp) {
630 fprintf(stderr, "qemu: bad scatternet '%s'\n", p);
631 return 1;
633 } else {
634 fprintf(stderr, "qemu: bad parameter '%s'\n", endp + 1);
635 return 1;
637 } else
638 vlan = 0;
640 bt_vhci_add(vlan);
641 return 0;
643 } else if (strstart(opt, "device:", &endp))
644 return !bt_device_add(endp);
646 fprintf(stderr, "qemu: bad bluetooth parameter '%s'\n", opt);
647 return 1;
650 /***********************************************************/
651 /* QEMU Block devices */
653 #define HD_ALIAS "index=%d,media=disk"
654 #define CDROM_ALIAS "index=2,media=cdrom"
655 #define FD_ALIAS "index=%d,if=floppy"
656 #define PFLASH_ALIAS "if=pflash"
657 #define MTD_ALIAS "if=mtd"
658 #define SD_ALIAS "index=0,if=sd"
660 QemuOpts *drive_add(const char *file, const char *fmt, ...)
662 va_list ap;
663 char optstr[1024];
664 QemuOpts *opts;
666 va_start(ap, fmt);
667 vsnprintf(optstr, sizeof(optstr), fmt, ap);
668 va_end(ap);
670 opts = qemu_opts_parse(&qemu_drive_opts, optstr, 0);
671 if (!opts) {
672 fprintf(stderr, "%s: huh? duplicate? (%s)\n",
673 __FUNCTION__, optstr);
674 return NULL;
676 if (file)
677 qemu_opt_set(opts, "file", file);
678 return opts;
681 DriveInfo *drive_get(BlockInterfaceType type, int bus, int unit)
683 DriveInfo *dinfo;
685 /* seek interface, bus and unit */
687 QTAILQ_FOREACH(dinfo, &drives, next) {
688 if (dinfo->type == type &&
689 dinfo->bus == bus &&
690 dinfo->unit == unit)
691 return dinfo;
694 return NULL;
697 DriveInfo *drive_get_by_id(const char *id)
699 DriveInfo *dinfo;
701 QTAILQ_FOREACH(dinfo, &drives, next) {
702 if (strcmp(id, dinfo->id))
703 continue;
704 return dinfo;
706 return NULL;
709 int drive_get_max_bus(BlockInterfaceType type)
711 int max_bus;
712 DriveInfo *dinfo;
714 max_bus = -1;
715 QTAILQ_FOREACH(dinfo, &drives, next) {
716 if(dinfo->type == type &&
717 dinfo->bus > max_bus)
718 max_bus = dinfo->bus;
720 return max_bus;
723 const char *drive_get_serial(BlockDriverState *bdrv)
725 DriveInfo *dinfo;
727 QTAILQ_FOREACH(dinfo, &drives, next) {
728 if (dinfo->bdrv == bdrv)
729 return dinfo->serial;
732 return "\0";
735 BlockInterfaceErrorAction drive_get_on_error(
736 BlockDriverState *bdrv, int is_read)
738 DriveInfo *dinfo;
740 QTAILQ_FOREACH(dinfo, &drives, next) {
741 if (dinfo->bdrv == bdrv)
742 return is_read ? dinfo->on_read_error : dinfo->on_write_error;
745 return is_read ? BLOCK_ERR_REPORT : BLOCK_ERR_STOP_ENOSPC;
748 static void bdrv_format_print(void *opaque, const char *name)
750 fprintf(stderr, " %s", name);
753 void drive_uninit(DriveInfo *dinfo)
755 qemu_opts_del(dinfo->opts);
756 bdrv_delete(dinfo->bdrv);
757 QTAILQ_REMOVE(&drives, dinfo, next);
758 qemu_free(dinfo);
761 static int parse_block_error_action(const char *buf, int is_read)
763 if (!strcmp(buf, "ignore")) {
764 return BLOCK_ERR_IGNORE;
765 } else if (!is_read && !strcmp(buf, "enospc")) {
766 return BLOCK_ERR_STOP_ENOSPC;
767 } else if (!strcmp(buf, "stop")) {
768 return BLOCK_ERR_STOP_ANY;
769 } else if (!strcmp(buf, "report")) {
770 return BLOCK_ERR_REPORT;
771 } else {
772 fprintf(stderr, "qemu: '%s' invalid %s error action\n",
773 buf, is_read ? "read" : "write");
774 return -1;
778 DriveInfo *drive_init(QemuOpts *opts, void *opaque,
779 int *fatal_error)
781 const char *buf;
782 const char *file = NULL;
783 char devname[128];
784 const char *serial;
785 const char *mediastr = "";
786 BlockInterfaceType type;
787 enum { MEDIA_DISK, MEDIA_CDROM } media;
788 int bus_id, unit_id;
789 int cyls, heads, secs, translation;
790 BlockDriver *drv = NULL;
791 QEMUMachine *machine = opaque;
792 int max_devs;
793 int index;
794 int cache;
795 int aio = 0;
796 int ro = 0;
797 int bdrv_flags;
798 int on_read_error, on_write_error;
799 const char *devaddr;
800 DriveInfo *dinfo;
801 int snapshot = 0;
803 *fatal_error = 1;
805 translation = BIOS_ATA_TRANSLATION_AUTO;
806 cache = 1;
808 if (machine && machine->use_scsi) {
809 type = IF_SCSI;
810 max_devs = MAX_SCSI_DEVS;
811 pstrcpy(devname, sizeof(devname), "scsi");
812 } else {
813 type = IF_IDE;
814 max_devs = MAX_IDE_DEVS;
815 pstrcpy(devname, sizeof(devname), "ide");
817 media = MEDIA_DISK;
819 /* extract parameters */
820 bus_id = qemu_opt_get_number(opts, "bus", 0);
821 unit_id = qemu_opt_get_number(opts, "unit", -1);
822 index = qemu_opt_get_number(opts, "index", -1);
824 cyls = qemu_opt_get_number(opts, "cyls", 0);
825 heads = qemu_opt_get_number(opts, "heads", 0);
826 secs = qemu_opt_get_number(opts, "secs", 0);
828 snapshot = qemu_opt_get_bool(opts, "snapshot", 0);
829 ro = qemu_opt_get_bool(opts, "readonly", 0);
831 file = qemu_opt_get(opts, "file");
832 serial = qemu_opt_get(opts, "serial");
834 if ((buf = qemu_opt_get(opts, "if")) != NULL) {
835 pstrcpy(devname, sizeof(devname), buf);
836 if (!strcmp(buf, "ide")) {
837 type = IF_IDE;
838 max_devs = MAX_IDE_DEVS;
839 } else if (!strcmp(buf, "scsi")) {
840 type = IF_SCSI;
841 max_devs = MAX_SCSI_DEVS;
842 } else if (!strcmp(buf, "floppy")) {
843 type = IF_FLOPPY;
844 max_devs = 0;
845 } else if (!strcmp(buf, "pflash")) {
846 type = IF_PFLASH;
847 max_devs = 0;
848 } else if (!strcmp(buf, "mtd")) {
849 type = IF_MTD;
850 max_devs = 0;
851 } else if (!strcmp(buf, "sd")) {
852 type = IF_SD;
853 max_devs = 0;
854 } else if (!strcmp(buf, "virtio")) {
855 type = IF_VIRTIO;
856 max_devs = 0;
857 } else if (!strcmp(buf, "xen")) {
858 type = IF_XEN;
859 max_devs = 0;
860 } else if (!strcmp(buf, "none")) {
861 type = IF_NONE;
862 max_devs = 0;
863 } else {
864 fprintf(stderr, "qemu: unsupported bus type '%s'\n", buf);
865 return NULL;
869 if (cyls || heads || secs) {
870 if (cyls < 1 || (type == IF_IDE && cyls > 16383)) {
871 fprintf(stderr, "qemu: '%s' invalid physical cyls number\n", buf);
872 return NULL;
874 if (heads < 1 || (type == IF_IDE && heads > 16)) {
875 fprintf(stderr, "qemu: '%s' invalid physical heads number\n", buf);
876 return NULL;
878 if (secs < 1 || (type == IF_IDE && secs > 63)) {
879 fprintf(stderr, "qemu: '%s' invalid physical secs number\n", buf);
880 return NULL;
884 if ((buf = qemu_opt_get(opts, "trans")) != NULL) {
885 if (!cyls) {
886 fprintf(stderr,
887 "qemu: '%s' trans must be used with cyls,heads and secs\n",
888 buf);
889 return NULL;
891 if (!strcmp(buf, "none"))
892 translation = BIOS_ATA_TRANSLATION_NONE;
893 else if (!strcmp(buf, "lba"))
894 translation = BIOS_ATA_TRANSLATION_LBA;
895 else if (!strcmp(buf, "auto"))
896 translation = BIOS_ATA_TRANSLATION_AUTO;
897 else {
898 fprintf(stderr, "qemu: '%s' invalid translation type\n", buf);
899 return NULL;
903 if ((buf = qemu_opt_get(opts, "media")) != NULL) {
904 if (!strcmp(buf, "disk")) {
905 media = MEDIA_DISK;
906 } else if (!strcmp(buf, "cdrom")) {
907 if (cyls || secs || heads) {
908 fprintf(stderr,
909 "qemu: '%s' invalid physical CHS format\n", buf);
910 return NULL;
912 media = MEDIA_CDROM;
913 } else {
914 fprintf(stderr, "qemu: '%s' invalid media\n", buf);
915 return NULL;
919 if ((buf = qemu_opt_get(opts, "cache")) != NULL) {
920 if (!strcmp(buf, "off") || !strcmp(buf, "none"))
921 cache = 0;
922 else if (!strcmp(buf, "writethrough"))
923 cache = 1;
924 else if (!strcmp(buf, "writeback"))
925 cache = 2;
926 else {
927 fprintf(stderr, "qemu: invalid cache option\n");
928 return NULL;
932 #ifdef CONFIG_LINUX_AIO
933 if ((buf = qemu_opt_get(opts, "aio")) != NULL) {
934 if (!strcmp(buf, "threads"))
935 aio = 0;
936 else if (!strcmp(buf, "native"))
937 aio = 1;
938 else {
939 fprintf(stderr, "qemu: invalid aio option\n");
940 return NULL;
943 #endif
945 if ((buf = qemu_opt_get(opts, "format")) != NULL) {
946 if (strcmp(buf, "?") == 0) {
947 fprintf(stderr, "qemu: Supported formats:");
948 bdrv_iterate_format(bdrv_format_print, NULL);
949 fprintf(stderr, "\n");
950 return NULL;
952 drv = bdrv_find_whitelisted_format(buf);
953 if (!drv) {
954 fprintf(stderr, "qemu: '%s' invalid format\n", buf);
955 return NULL;
959 on_write_error = BLOCK_ERR_STOP_ENOSPC;
960 if ((buf = qemu_opt_get(opts, "werror")) != NULL) {
961 if (type != IF_IDE && type != IF_SCSI && type != IF_VIRTIO) {
962 fprintf(stderr, "werror is no supported by this format\n");
963 return NULL;
966 on_write_error = parse_block_error_action(buf, 0);
967 if (on_write_error < 0) {
968 return NULL;
972 on_read_error = BLOCK_ERR_REPORT;
973 if ((buf = qemu_opt_get(opts, "rerror")) != NULL) {
974 if (type != IF_IDE && type != IF_VIRTIO) {
975 fprintf(stderr, "rerror is no supported by this format\n");
976 return NULL;
979 on_read_error = parse_block_error_action(buf, 1);
980 if (on_read_error < 0) {
981 return NULL;
985 if ((devaddr = qemu_opt_get(opts, "addr")) != NULL) {
986 if (type != IF_VIRTIO) {
987 fprintf(stderr, "addr is not supported\n");
988 return NULL;
992 /* compute bus and unit according index */
994 if (index != -1) {
995 if (bus_id != 0 || unit_id != -1) {
996 fprintf(stderr,
997 "qemu: index cannot be used with bus and unit\n");
998 return NULL;
1000 if (max_devs == 0)
1002 unit_id = index;
1003 bus_id = 0;
1004 } else {
1005 unit_id = index % max_devs;
1006 bus_id = index / max_devs;
1010 /* if user doesn't specify a unit_id,
1011 * try to find the first free
1014 if (unit_id == -1) {
1015 unit_id = 0;
1016 while (drive_get(type, bus_id, unit_id) != NULL) {
1017 unit_id++;
1018 if (max_devs && unit_id >= max_devs) {
1019 unit_id -= max_devs;
1020 bus_id++;
1025 /* check unit id */
1027 if (max_devs && unit_id >= max_devs) {
1028 fprintf(stderr, "qemu: unit %d too big (max is %d)\n",
1029 unit_id, max_devs - 1);
1030 return NULL;
1034 * ignore multiple definitions
1037 if (drive_get(type, bus_id, unit_id) != NULL) {
1038 *fatal_error = 0;
1039 return NULL;
1042 /* init */
1044 dinfo = qemu_mallocz(sizeof(*dinfo));
1045 if ((buf = qemu_opts_id(opts)) != NULL) {
1046 dinfo->id = qemu_strdup(buf);
1047 } else {
1048 /* no id supplied -> create one */
1049 dinfo->id = qemu_mallocz(32);
1050 if (type == IF_IDE || type == IF_SCSI)
1051 mediastr = (media == MEDIA_CDROM) ? "-cd" : "-hd";
1052 if (max_devs)
1053 snprintf(dinfo->id, 32, "%s%i%s%i",
1054 devname, bus_id, mediastr, unit_id);
1055 else
1056 snprintf(dinfo->id, 32, "%s%s%i",
1057 devname, mediastr, unit_id);
1059 dinfo->bdrv = bdrv_new(dinfo->id);
1060 dinfo->devaddr = devaddr;
1061 dinfo->type = type;
1062 dinfo->bus = bus_id;
1063 dinfo->unit = unit_id;
1064 dinfo->on_read_error = on_read_error;
1065 dinfo->on_write_error = on_write_error;
1066 dinfo->opts = opts;
1067 if (serial)
1068 strncpy(dinfo->serial, serial, sizeof(serial));
1069 QTAILQ_INSERT_TAIL(&drives, dinfo, next);
1071 switch(type) {
1072 case IF_IDE:
1073 case IF_SCSI:
1074 case IF_XEN:
1075 case IF_NONE:
1076 switch(media) {
1077 case MEDIA_DISK:
1078 if (cyls != 0) {
1079 bdrv_set_geometry_hint(dinfo->bdrv, cyls, heads, secs);
1080 bdrv_set_translation_hint(dinfo->bdrv, translation);
1082 break;
1083 case MEDIA_CDROM:
1084 bdrv_set_type_hint(dinfo->bdrv, BDRV_TYPE_CDROM);
1085 break;
1087 break;
1088 case IF_SD:
1089 /* FIXME: This isn't really a floppy, but it's a reasonable
1090 approximation. */
1091 case IF_FLOPPY:
1092 bdrv_set_type_hint(dinfo->bdrv, BDRV_TYPE_FLOPPY);
1093 break;
1094 case IF_PFLASH:
1095 case IF_MTD:
1096 break;
1097 case IF_VIRTIO:
1098 /* add virtio block device */
1099 opts = qemu_opts_create(&qemu_device_opts, NULL, 0);
1100 qemu_opt_set(opts, "driver", "virtio-blk-pci");
1101 qemu_opt_set(opts, "drive", dinfo->id);
1102 if (devaddr)
1103 qemu_opt_set(opts, "addr", devaddr);
1104 break;
1105 case IF_COUNT:
1106 abort();
1108 if (!file) {
1109 *fatal_error = 0;
1110 return NULL;
1112 bdrv_flags = 0;
1113 if (snapshot) {
1114 bdrv_flags |= BDRV_O_SNAPSHOT;
1115 cache = 2; /* always use write-back with snapshot */
1117 if (cache == 0) /* no caching */
1118 bdrv_flags |= BDRV_O_NOCACHE;
1119 else if (cache == 2) /* write-back */
1120 bdrv_flags |= BDRV_O_CACHE_WB;
1122 if (aio == 1) {
1123 bdrv_flags |= BDRV_O_NATIVE_AIO;
1124 } else {
1125 bdrv_flags &= ~BDRV_O_NATIVE_AIO;
1128 if (media == MEDIA_CDROM) {
1129 /* CDROM is fine for any interface, don't check. */
1130 ro = 1;
1131 } else if (ro == 1) {
1132 if (type != IF_SCSI && type != IF_VIRTIO && type != IF_FLOPPY) {
1133 fprintf(stderr, "qemu: readonly flag not supported for drive with this interface\n");
1134 return NULL;
1138 bdrv_flags |= ro ? 0 : BDRV_O_RDWR;
1140 if (bdrv_open2(dinfo->bdrv, file, bdrv_flags, drv) < 0) {
1141 fprintf(stderr, "qemu: could not open disk image %s: %s\n",
1142 file, strerror(errno));
1143 return NULL;
1146 if (bdrv_key_required(dinfo->bdrv))
1147 autostart = 0;
1148 *fatal_error = 0;
1149 return dinfo;
1152 static int drive_init_func(QemuOpts *opts, void *opaque)
1154 QEMUMachine *machine = opaque;
1155 int fatal_error = 0;
1157 if (drive_init(opts, machine, &fatal_error) == NULL) {
1158 if (fatal_error)
1159 return 1;
1161 return 0;
1164 static int drive_enable_snapshot(QemuOpts *opts, void *opaque)
1166 if (NULL == qemu_opt_get(opts, "snapshot")) {
1167 qemu_opt_set(opts, "snapshot", "on");
1169 return 0;
1172 void qemu_register_boot_set(QEMUBootSetHandler *func, void *opaque)
1174 boot_set_handler = func;
1175 boot_set_opaque = opaque;
1178 int qemu_boot_set(const char *boot_devices)
1180 if (!boot_set_handler) {
1181 return -EINVAL;
1183 return boot_set_handler(boot_set_opaque, boot_devices);
1186 static void validate_bootdevices(char *devices)
1188 /* We just do some generic consistency checks */
1189 const char *p;
1190 int bitmap = 0;
1192 for (p = devices; *p != '\0'; p++) {
1193 /* Allowed boot devices are:
1194 * a-b: floppy disk drives
1195 * c-f: IDE disk drives
1196 * g-m: machine implementation dependant drives
1197 * n-p: network devices
1198 * It's up to each machine implementation to check if the given boot
1199 * devices match the actual hardware implementation and firmware
1200 * features.
1202 if (*p < 'a' || *p > 'p') {
1203 fprintf(stderr, "Invalid boot device '%c'\n", *p);
1204 exit(1);
1206 if (bitmap & (1 << (*p - 'a'))) {
1207 fprintf(stderr, "Boot device '%c' was given twice\n", *p);
1208 exit(1);
1210 bitmap |= 1 << (*p - 'a');
1214 static void restore_boot_devices(void *opaque)
1216 char *standard_boot_devices = opaque;
1218 qemu_boot_set(standard_boot_devices);
1220 qemu_unregister_reset(restore_boot_devices, standard_boot_devices);
1221 qemu_free(standard_boot_devices);
1224 static void numa_add(const char *optarg)
1226 char option[128];
1227 char *endptr;
1228 unsigned long long value, endvalue;
1229 int nodenr;
1231 optarg = get_opt_name(option, 128, optarg, ',') + 1;
1232 if (!strcmp(option, "node")) {
1233 if (get_param_value(option, 128, "nodeid", optarg) == 0) {
1234 nodenr = nb_numa_nodes;
1235 } else {
1236 nodenr = strtoull(option, NULL, 10);
1239 if (get_param_value(option, 128, "mem", optarg) == 0) {
1240 node_mem[nodenr] = 0;
1241 } else {
1242 value = strtoull(option, &endptr, 0);
1243 switch (*endptr) {
1244 case 0: case 'M': case 'm':
1245 value <<= 20;
1246 break;
1247 case 'G': case 'g':
1248 value <<= 30;
1249 break;
1251 node_mem[nodenr] = value;
1253 if (get_param_value(option, 128, "cpus", optarg) == 0) {
1254 node_cpumask[nodenr] = 0;
1255 } else {
1256 value = strtoull(option, &endptr, 10);
1257 if (value >= 64) {
1258 value = 63;
1259 fprintf(stderr, "only 64 CPUs in NUMA mode supported.\n");
1260 } else {
1261 if (*endptr == '-') {
1262 endvalue = strtoull(endptr+1, &endptr, 10);
1263 if (endvalue >= 63) {
1264 endvalue = 62;
1265 fprintf(stderr,
1266 "only 63 CPUs in NUMA mode supported.\n");
1268 value = (2ULL << endvalue) - (1ULL << value);
1269 } else {
1270 value = 1ULL << value;
1273 node_cpumask[nodenr] = value;
1275 nb_numa_nodes++;
1277 return;
1280 static void smp_parse(const char *optarg)
1282 int smp, sockets = 0, threads = 0, cores = 0;
1283 char *endptr;
1284 char option[128];
1286 smp = strtoul(optarg, &endptr, 10);
1287 if (endptr != optarg) {
1288 if (*endptr == ',') {
1289 endptr++;
1292 if (get_param_value(option, 128, "sockets", endptr) != 0)
1293 sockets = strtoull(option, NULL, 10);
1294 if (get_param_value(option, 128, "cores", endptr) != 0)
1295 cores = strtoull(option, NULL, 10);
1296 if (get_param_value(option, 128, "threads", endptr) != 0)
1297 threads = strtoull(option, NULL, 10);
1298 if (get_param_value(option, 128, "maxcpus", endptr) != 0)
1299 max_cpus = strtoull(option, NULL, 10);
1301 /* compute missing values, prefer sockets over cores over threads */
1302 if (smp == 0 || sockets == 0) {
1303 sockets = sockets > 0 ? sockets : 1;
1304 cores = cores > 0 ? cores : 1;
1305 threads = threads > 0 ? threads : 1;
1306 if (smp == 0) {
1307 smp = cores * threads * sockets;
1309 } else {
1310 if (cores == 0) {
1311 threads = threads > 0 ? threads : 1;
1312 cores = smp / (sockets * threads);
1313 } else {
1314 if (sockets) {
1315 threads = smp / (cores * sockets);
1319 smp_cpus = smp;
1320 smp_cores = cores > 0 ? cores : 1;
1321 smp_threads = threads > 0 ? threads : 1;
1322 if (max_cpus == 0)
1323 max_cpus = smp_cpus;
1326 /***********************************************************/
1327 /* USB devices */
1329 static int usb_device_add(const char *devname, int is_hotplug)
1331 const char *p;
1332 USBDevice *dev = NULL;
1334 if (!usb_enabled)
1335 return -1;
1337 /* drivers with .usbdevice_name entry in USBDeviceInfo */
1338 dev = usbdevice_create(devname);
1339 if (dev)
1340 goto done;
1342 /* the other ones */
1343 if (strstart(devname, "host:", &p)) {
1344 dev = usb_host_device_open(p);
1345 } else if (!strcmp(devname, "bt") || strstart(devname, "bt:", &p)) {
1346 dev = usb_bt_init(devname[2] ? hci_init(p) :
1347 bt_new_hci(qemu_find_bt_vlan(0)));
1348 } else {
1349 return -1;
1351 if (!dev)
1352 return -1;
1354 done:
1355 return 0;
1358 static int usb_device_del(const char *devname)
1360 int bus_num, addr;
1361 const char *p;
1363 if (strstart(devname, "host:", &p))
1364 return usb_host_device_close(p);
1366 if (!usb_enabled)
1367 return -1;
1369 p = strchr(devname, '.');
1370 if (!p)
1371 return -1;
1372 bus_num = strtoul(devname, NULL, 0);
1373 addr = strtoul(p + 1, NULL, 0);
1375 return usb_device_delete_addr(bus_num, addr);
1378 static int usb_parse(const char *cmdline)
1380 int r;
1381 r = usb_device_add(cmdline, 0);
1382 if (r < 0) {
1383 fprintf(stderr, "qemu: could not add USB device '%s'\n", cmdline);
1385 return r;
1388 void do_usb_add(Monitor *mon, const QDict *qdict)
1390 const char *devname = qdict_get_str(qdict, "devname");
1391 if (usb_device_add(devname, 1) < 0) {
1392 error_report("could not add USB device '%s'", devname);
1396 void do_usb_del(Monitor *mon, const QDict *qdict)
1398 const char *devname = qdict_get_str(qdict, "devname");
1399 if (usb_device_del(devname) < 0) {
1400 error_report("could not delete USB device '%s'", devname);
1404 /***********************************************************/
1405 /* PCMCIA/Cardbus */
1407 static struct pcmcia_socket_entry_s {
1408 PCMCIASocket *socket;
1409 struct pcmcia_socket_entry_s *next;
1410 } *pcmcia_sockets = 0;
1412 void pcmcia_socket_register(PCMCIASocket *socket)
1414 struct pcmcia_socket_entry_s *entry;
1416 entry = qemu_malloc(sizeof(struct pcmcia_socket_entry_s));
1417 entry->socket = socket;
1418 entry->next = pcmcia_sockets;
1419 pcmcia_sockets = entry;
1422 void pcmcia_socket_unregister(PCMCIASocket *socket)
1424 struct pcmcia_socket_entry_s *entry, **ptr;
1426 ptr = &pcmcia_sockets;
1427 for (entry = *ptr; entry; ptr = &entry->next, entry = *ptr)
1428 if (entry->socket == socket) {
1429 *ptr = entry->next;
1430 qemu_free(entry);
1434 void pcmcia_info(Monitor *mon)
1436 struct pcmcia_socket_entry_s *iter;
1438 if (!pcmcia_sockets)
1439 monitor_printf(mon, "No PCMCIA sockets\n");
1441 for (iter = pcmcia_sockets; iter; iter = iter->next)
1442 monitor_printf(mon, "%s: %s\n", iter->socket->slot_string,
1443 iter->socket->attached ? iter->socket->card_string :
1444 "Empty");
1447 /***********************************************************/
1448 /* I/O handling */
1450 typedef struct IOHandlerRecord {
1451 int fd;
1452 IOCanReadHandler *fd_read_poll;
1453 IOHandler *fd_read;
1454 IOHandler *fd_write;
1455 int deleted;
1456 void *opaque;
1457 /* temporary data */
1458 struct pollfd *ufd;
1459 QLIST_ENTRY(IOHandlerRecord) next;
1460 } IOHandlerRecord;
1462 static QLIST_HEAD(, IOHandlerRecord) io_handlers =
1463 QLIST_HEAD_INITIALIZER(io_handlers);
1466 /* XXX: fd_read_poll should be suppressed, but an API change is
1467 necessary in the character devices to suppress fd_can_read(). */
1468 int qemu_set_fd_handler2(int fd,
1469 IOCanReadHandler *fd_read_poll,
1470 IOHandler *fd_read,
1471 IOHandler *fd_write,
1472 void *opaque)
1474 IOHandlerRecord *ioh;
1476 if (!fd_read && !fd_write) {
1477 QLIST_FOREACH(ioh, &io_handlers, next) {
1478 if (ioh->fd == fd) {
1479 ioh->deleted = 1;
1480 break;
1483 } else {
1484 QLIST_FOREACH(ioh, &io_handlers, next) {
1485 if (ioh->fd == fd)
1486 goto found;
1488 ioh = qemu_mallocz(sizeof(IOHandlerRecord));
1489 QLIST_INSERT_HEAD(&io_handlers, ioh, next);
1490 found:
1491 ioh->fd = fd;
1492 ioh->fd_read_poll = fd_read_poll;
1493 ioh->fd_read = fd_read;
1494 ioh->fd_write = fd_write;
1495 ioh->opaque = opaque;
1496 ioh->deleted = 0;
1498 return 0;
1501 int qemu_set_fd_handler(int fd,
1502 IOHandler *fd_read,
1503 IOHandler *fd_write,
1504 void *opaque)
1506 return qemu_set_fd_handler2(fd, NULL, fd_read, fd_write, opaque);
1509 #ifdef _WIN32
1510 /***********************************************************/
1511 /* Polling handling */
1513 typedef struct PollingEntry {
1514 PollingFunc *func;
1515 void *opaque;
1516 struct PollingEntry *next;
1517 } PollingEntry;
1519 static PollingEntry *first_polling_entry;
1521 int qemu_add_polling_cb(PollingFunc *func, void *opaque)
1523 PollingEntry **ppe, *pe;
1524 pe = qemu_mallocz(sizeof(PollingEntry));
1525 pe->func = func;
1526 pe->opaque = opaque;
1527 for(ppe = &first_polling_entry; *ppe != NULL; ppe = &(*ppe)->next);
1528 *ppe = pe;
1529 return 0;
1532 void qemu_del_polling_cb(PollingFunc *func, void *opaque)
1534 PollingEntry **ppe, *pe;
1535 for(ppe = &first_polling_entry; *ppe != NULL; ppe = &(*ppe)->next) {
1536 pe = *ppe;
1537 if (pe->func == func && pe->opaque == opaque) {
1538 *ppe = pe->next;
1539 qemu_free(pe);
1540 break;
1545 /***********************************************************/
1546 /* Wait objects support */
1547 typedef struct WaitObjects {
1548 int num;
1549 HANDLE events[MAXIMUM_WAIT_OBJECTS + 1];
1550 WaitObjectFunc *func[MAXIMUM_WAIT_OBJECTS + 1];
1551 void *opaque[MAXIMUM_WAIT_OBJECTS + 1];
1552 } WaitObjects;
1554 static WaitObjects wait_objects = {0};
1556 int qemu_add_wait_object(HANDLE handle, WaitObjectFunc *func, void *opaque)
1558 WaitObjects *w = &wait_objects;
1560 if (w->num >= MAXIMUM_WAIT_OBJECTS)
1561 return -1;
1562 w->events[w->num] = handle;
1563 w->func[w->num] = func;
1564 w->opaque[w->num] = opaque;
1565 w->num++;
1566 return 0;
1569 void qemu_del_wait_object(HANDLE handle, WaitObjectFunc *func, void *opaque)
1571 int i, found;
1572 WaitObjects *w = &wait_objects;
1574 found = 0;
1575 for (i = 0; i < w->num; i++) {
1576 if (w->events[i] == handle)
1577 found = 1;
1578 if (found) {
1579 w->events[i] = w->events[i + 1];
1580 w->func[i] = w->func[i + 1];
1581 w->opaque[i] = w->opaque[i + 1];
1584 if (found)
1585 w->num--;
1587 #endif
1589 /***********************************************************/
1590 /* machine registration */
1592 static QEMUMachine *first_machine = NULL;
1593 QEMUMachine *current_machine = NULL;
1595 int qemu_register_machine(QEMUMachine *m)
1597 QEMUMachine **pm;
1598 pm = &first_machine;
1599 while (*pm != NULL)
1600 pm = &(*pm)->next;
1601 m->next = NULL;
1602 *pm = m;
1603 return 0;
1606 static QEMUMachine *find_machine(const char *name)
1608 QEMUMachine *m;
1610 for(m = first_machine; m != NULL; m = m->next) {
1611 if (!strcmp(m->name, name))
1612 return m;
1613 if (m->alias && !strcmp(m->alias, name))
1614 return m;
1616 return NULL;
1619 static QEMUMachine *find_default_machine(void)
1621 QEMUMachine *m;
1623 for(m = first_machine; m != NULL; m = m->next) {
1624 if (m->is_default) {
1625 return m;
1628 return NULL;
1631 /***********************************************************/
1632 /* main execution loop */
1634 static void gui_update(void *opaque)
1636 uint64_t interval = GUI_REFRESH_INTERVAL;
1637 DisplayState *ds = opaque;
1638 DisplayChangeListener *dcl = ds->listeners;
1640 qemu_flush_coalesced_mmio_buffer();
1641 dpy_refresh(ds);
1643 while (dcl != NULL) {
1644 if (dcl->gui_timer_interval &&
1645 dcl->gui_timer_interval < interval)
1646 interval = dcl->gui_timer_interval;
1647 dcl = dcl->next;
1649 qemu_mod_timer(ds->gui_timer, interval + qemu_get_clock(rt_clock));
1652 static void nographic_update(void *opaque)
1654 uint64_t interval = GUI_REFRESH_INTERVAL;
1656 qemu_flush_coalesced_mmio_buffer();
1657 qemu_mod_timer(nographic_timer, interval + qemu_get_clock(rt_clock));
1660 struct vm_change_state_entry {
1661 VMChangeStateHandler *cb;
1662 void *opaque;
1663 QLIST_ENTRY (vm_change_state_entry) entries;
1666 static QLIST_HEAD(vm_change_state_head, vm_change_state_entry) vm_change_state_head;
1668 VMChangeStateEntry *qemu_add_vm_change_state_handler(VMChangeStateHandler *cb,
1669 void *opaque)
1671 VMChangeStateEntry *e;
1673 e = qemu_mallocz(sizeof (*e));
1675 e->cb = cb;
1676 e->opaque = opaque;
1677 QLIST_INSERT_HEAD(&vm_change_state_head, e, entries);
1678 return e;
1681 void qemu_del_vm_change_state_handler(VMChangeStateEntry *e)
1683 QLIST_REMOVE (e, entries);
1684 qemu_free (e);
1687 void vm_state_notify(int running, int reason)
1689 VMChangeStateEntry *e;
1691 for (e = vm_change_state_head.lh_first; e; e = e->entries.le_next) {
1692 e->cb(e->opaque, running, reason);
1696 void vm_start(void)
1698 if (!vm_running) {
1699 cpu_enable_ticks();
1700 vm_running = 1;
1701 vm_state_notify(1, 0);
1702 resume_all_vcpus();
1706 /* reset/shutdown handler */
1708 typedef struct QEMUResetEntry {
1709 QTAILQ_ENTRY(QEMUResetEntry) entry;
1710 QEMUResetHandler *func;
1711 void *opaque;
1712 } QEMUResetEntry;
1714 static QTAILQ_HEAD(reset_handlers, QEMUResetEntry) reset_handlers =
1715 QTAILQ_HEAD_INITIALIZER(reset_handlers);
1716 static int reset_requested;
1717 static int shutdown_requested;
1718 static int powerdown_requested;
1719 int debug_requested;
1720 static int vmstop_requested;
1722 int qemu_shutdown_requested(void)
1724 int r = shutdown_requested;
1725 shutdown_requested = 0;
1726 return r;
1729 int qemu_reset_requested(void)
1731 int r = reset_requested;
1732 reset_requested = 0;
1733 return r;
1736 int qemu_powerdown_requested(void)
1738 int r = powerdown_requested;
1739 powerdown_requested = 0;
1740 return r;
1743 static int qemu_debug_requested(void)
1745 int r = debug_requested;
1746 debug_requested = 0;
1747 return r;
1750 static int qemu_vmstop_requested(void)
1752 int r = vmstop_requested;
1753 vmstop_requested = 0;
1754 return r;
1757 void qemu_register_reset(QEMUResetHandler *func, void *opaque)
1759 QEMUResetEntry *re = qemu_mallocz(sizeof(QEMUResetEntry));
1761 re->func = func;
1762 re->opaque = opaque;
1763 QTAILQ_INSERT_TAIL(&reset_handlers, re, entry);
1766 void qemu_unregister_reset(QEMUResetHandler *func, void *opaque)
1768 QEMUResetEntry *re;
1770 QTAILQ_FOREACH(re, &reset_handlers, entry) {
1771 if (re->func == func && re->opaque == opaque) {
1772 QTAILQ_REMOVE(&reset_handlers, re, entry);
1773 qemu_free(re);
1774 return;
1779 void qemu_system_reset(void)
1781 QEMUResetEntry *re, *nre;
1783 /* reset all devices */
1784 QTAILQ_FOREACH_SAFE(re, &reset_handlers, entry, nre) {
1785 re->func(re->opaque);
1787 monitor_protocol_event(QEVENT_RESET, NULL);
1788 cpu_synchronize_all_post_reset();
1791 void qemu_system_reset_request(void)
1793 if (no_reboot) {
1794 shutdown_requested = 1;
1795 } else {
1796 reset_requested = 1;
1798 qemu_notify_event();
1801 void qemu_system_shutdown_request(void)
1803 shutdown_requested = 1;
1804 qemu_notify_event();
1807 void qemu_system_powerdown_request(void)
1809 powerdown_requested = 1;
1810 qemu_notify_event();
1813 #ifdef _WIN32
1814 static void host_main_loop_wait(int *timeout)
1816 int ret, ret2, i;
1817 PollingEntry *pe;
1820 /* XXX: need to suppress polling by better using win32 events */
1821 ret = 0;
1822 for(pe = first_polling_entry; pe != NULL; pe = pe->next) {
1823 ret |= pe->func(pe->opaque);
1825 if (ret == 0) {
1826 int err;
1827 WaitObjects *w = &wait_objects;
1829 ret = WaitForMultipleObjects(w->num, w->events, FALSE, *timeout);
1830 if (WAIT_OBJECT_0 + 0 <= ret && ret <= WAIT_OBJECT_0 + w->num - 1) {
1831 if (w->func[ret - WAIT_OBJECT_0])
1832 w->func[ret - WAIT_OBJECT_0](w->opaque[ret - WAIT_OBJECT_0]);
1834 /* Check for additional signaled events */
1835 for(i = (ret - WAIT_OBJECT_0 + 1); i < w->num; i++) {
1837 /* Check if event is signaled */
1838 ret2 = WaitForSingleObject(w->events[i], 0);
1839 if(ret2 == WAIT_OBJECT_0) {
1840 if (w->func[i])
1841 w->func[i](w->opaque[i]);
1842 } else if (ret2 == WAIT_TIMEOUT) {
1843 } else {
1844 err = GetLastError();
1845 fprintf(stderr, "WaitForSingleObject error %d %d\n", i, err);
1848 } else if (ret == WAIT_TIMEOUT) {
1849 } else {
1850 err = GetLastError();
1851 fprintf(stderr, "WaitForMultipleObjects error %d %d\n", ret, err);
1855 *timeout = 0;
1857 #else
1858 static void host_main_loop_wait(int *timeout)
1861 #endif
1863 void main_loop_wait(int nonblocking)
1865 IOHandlerRecord *ioh;
1866 fd_set rfds, wfds, xfds;
1867 int ret, nfds;
1868 struct timeval tv;
1869 int timeout;
1871 if (nonblocking)
1872 timeout = 0;
1873 else {
1874 timeout = qemu_calculate_timeout();
1875 qemu_bh_update_timeout(&timeout);
1878 host_main_loop_wait(&timeout);
1880 /* poll any events */
1881 /* XXX: separate device handlers from system ones */
1882 nfds = -1;
1883 FD_ZERO(&rfds);
1884 FD_ZERO(&wfds);
1885 FD_ZERO(&xfds);
1886 QLIST_FOREACH(ioh, &io_handlers, next) {
1887 if (ioh->deleted)
1888 continue;
1889 if (ioh->fd_read &&
1890 (!ioh->fd_read_poll ||
1891 ioh->fd_read_poll(ioh->opaque) != 0)) {
1892 FD_SET(ioh->fd, &rfds);
1893 if (ioh->fd > nfds)
1894 nfds = ioh->fd;
1896 if (ioh->fd_write) {
1897 FD_SET(ioh->fd, &wfds);
1898 if (ioh->fd > nfds)
1899 nfds = ioh->fd;
1903 tv.tv_sec = timeout / 1000;
1904 tv.tv_usec = (timeout % 1000) * 1000;
1906 slirp_select_fill(&nfds, &rfds, &wfds, &xfds);
1908 qemu_mutex_unlock_iothread();
1909 ret = select(nfds + 1, &rfds, &wfds, &xfds, &tv);
1910 qemu_mutex_lock_iothread();
1911 if (ret > 0) {
1912 IOHandlerRecord *pioh;
1914 QLIST_FOREACH_SAFE(ioh, &io_handlers, next, pioh) {
1915 if (ioh->deleted) {
1916 QLIST_REMOVE(ioh, next);
1917 qemu_free(ioh);
1918 continue;
1920 if (ioh->fd_read && FD_ISSET(ioh->fd, &rfds)) {
1921 ioh->fd_read(ioh->opaque);
1923 if (ioh->fd_write && FD_ISSET(ioh->fd, &wfds)) {
1924 ioh->fd_write(ioh->opaque);
1929 slirp_select_poll(&rfds, &wfds, &xfds, (ret < 0));
1931 qemu_run_all_timers();
1933 /* Check bottom-halves last in case any of the earlier events triggered
1934 them. */
1935 qemu_bh_poll();
1939 static int vm_can_run(void)
1941 if (powerdown_requested)
1942 return 0;
1943 if (reset_requested)
1944 return 0;
1945 if (shutdown_requested)
1946 return 0;
1947 if (debug_requested)
1948 return 0;
1949 return 1;
1952 qemu_irq qemu_system_powerdown;
1954 static void main_loop(void)
1956 int r;
1958 #ifdef CONFIG_IOTHREAD
1959 qemu_system_ready = 1;
1960 qemu_cond_broadcast(&qemu_system_cond);
1961 #endif
1963 for (;;) {
1964 do {
1965 bool nonblocking = false;
1966 #ifdef CONFIG_PROFILER
1967 int64_t ti;
1968 #endif
1969 #ifndef CONFIG_IOTHREAD
1970 nonblocking = tcg_cpu_exec();
1971 #endif
1972 #ifdef CONFIG_PROFILER
1973 ti = profile_getclock();
1974 #endif
1975 main_loop_wait(nonblocking);
1976 #ifdef CONFIG_PROFILER
1977 dev_time += profile_getclock() - ti;
1978 #endif
1979 } while (vm_can_run());
1981 if ((r = qemu_debug_requested())) {
1982 vm_stop(r);
1984 if (qemu_shutdown_requested()) {
1985 monitor_protocol_event(QEVENT_SHUTDOWN, NULL);
1986 if (no_shutdown) {
1987 vm_stop(0);
1988 no_shutdown = 0;
1989 } else
1990 break;
1992 if (qemu_reset_requested()) {
1993 pause_all_vcpus();
1994 qemu_system_reset();
1995 resume_all_vcpus();
1997 if (qemu_powerdown_requested()) {
1998 monitor_protocol_event(QEVENT_POWERDOWN, NULL);
1999 qemu_irq_raise(qemu_system_powerdown);
2001 if ((r = qemu_vmstop_requested())) {
2002 vm_stop(r);
2005 pause_all_vcpus();
2008 static void version(void)
2010 printf("QEMU PC emulator version " QEMU_VERSION QEMU_PKGVERSION ", Copyright (c) 2003-2008 Fabrice Bellard\n");
2013 static void help(int exitcode)
2015 const char *options_help =
2016 #define DEF(option, opt_arg, opt_enum, opt_help, arch_mask) \
2017 opt_help
2018 #define DEFHEADING(text) stringify(text) "\n"
2019 #include "qemu-options.h"
2020 #undef DEF
2021 #undef DEFHEADING
2022 #undef GEN_DOCS
2024 version();
2025 printf("usage: %s [options] [disk_image]\n"
2026 "\n"
2027 "'disk_image' is a raw hard image image for IDE hard disk 0\n"
2028 "\n"
2029 "%s\n"
2030 "During emulation, the following keys are useful:\n"
2031 "ctrl-alt-f toggle full screen\n"
2032 "ctrl-alt-n switch to virtual console 'n'\n"
2033 "ctrl-alt toggle mouse and keyboard grab\n"
2034 "\n"
2035 "When using -nographic, press 'ctrl-a h' to get some help.\n",
2036 "qemu",
2037 options_help);
2038 exit(exitcode);
2041 #define HAS_ARG 0x0001
2043 enum {
2044 #define DEF(option, opt_arg, opt_enum, opt_help, arch_mask) \
2045 opt_enum,
2046 #define DEFHEADING(text)
2047 #include "qemu-options.h"
2048 #undef DEF
2049 #undef DEFHEADING
2050 #undef GEN_DOCS
2053 typedef struct QEMUOption {
2054 const char *name;
2055 int flags;
2056 int index;
2057 uint32_t arch_mask;
2058 } QEMUOption;
2060 static const QEMUOption qemu_options[] = {
2061 { "h", 0, QEMU_OPTION_h, QEMU_ARCH_ALL },
2062 #define DEF(option, opt_arg, opt_enum, opt_help, arch_mask) \
2063 { option, opt_arg, opt_enum, arch_mask },
2064 #define DEFHEADING(text)
2065 #include "qemu-options.h"
2066 #undef DEF
2067 #undef DEFHEADING
2068 #undef GEN_DOCS
2069 { NULL },
2071 static void select_vgahw (const char *p)
2073 const char *opts;
2075 default_vga = 0;
2076 vga_interface_type = VGA_NONE;
2077 if (strstart(p, "std", &opts)) {
2078 vga_interface_type = VGA_STD;
2079 } else if (strstart(p, "cirrus", &opts)) {
2080 vga_interface_type = VGA_CIRRUS;
2081 } else if (strstart(p, "vmware", &opts)) {
2082 vga_interface_type = VGA_VMWARE;
2083 } else if (strstart(p, "xenfb", &opts)) {
2084 vga_interface_type = VGA_XENFB;
2085 } else if (!strstart(p, "none", &opts)) {
2086 invalid_vga:
2087 fprintf(stderr, "Unknown vga type: %s\n", p);
2088 exit(1);
2090 while (*opts) {
2091 const char *nextopt;
2093 if (strstart(opts, ",retrace=", &nextopt)) {
2094 opts = nextopt;
2095 if (strstart(opts, "dumb", &nextopt))
2096 vga_retrace_method = VGA_RETRACE_DUMB;
2097 else if (strstart(opts, "precise", &nextopt))
2098 vga_retrace_method = VGA_RETRACE_PRECISE;
2099 else goto invalid_vga;
2100 } else goto invalid_vga;
2101 opts = nextopt;
2105 static int balloon_parse(const char *arg)
2107 QemuOpts *opts;
2109 if (strcmp(arg, "none") == 0) {
2110 return 0;
2113 if (!strncmp(arg, "virtio", 6)) {
2114 if (arg[6] == ',') {
2115 /* have params -> parse them */
2116 opts = qemu_opts_parse(&qemu_device_opts, arg+7, 0);
2117 if (!opts)
2118 return -1;
2119 } else {
2120 /* create empty opts */
2121 opts = qemu_opts_create(&qemu_device_opts, NULL, 0);
2123 qemu_opt_set(opts, "driver", "virtio-balloon-pci");
2124 return 0;
2127 return -1;
2130 #ifdef _WIN32
2131 static BOOL WINAPI qemu_ctrl_handler(DWORD type)
2133 exit(STATUS_CONTROL_C_EXIT);
2134 return TRUE;
2136 #endif
2138 #ifndef _WIN32
2140 static void termsig_handler(int signal)
2142 qemu_system_shutdown_request();
2145 static void sigchld_handler(int signal)
2147 waitpid(-1, NULL, WNOHANG);
2150 static void sighandler_setup(void)
2152 struct sigaction act;
2154 memset(&act, 0, sizeof(act));
2155 act.sa_handler = termsig_handler;
2156 sigaction(SIGINT, &act, NULL);
2157 sigaction(SIGHUP, &act, NULL);
2158 sigaction(SIGTERM, &act, NULL);
2160 act.sa_handler = sigchld_handler;
2161 act.sa_flags = SA_NOCLDSTOP;
2162 sigaction(SIGCHLD, &act, NULL);
2165 #endif
2167 #ifdef _WIN32
2168 /* Look for support files in the same directory as the executable. */
2169 static char *find_datadir(const char *argv0)
2171 char *p;
2172 char buf[MAX_PATH];
2173 DWORD len;
2175 len = GetModuleFileName(NULL, buf, sizeof(buf) - 1);
2176 if (len == 0) {
2177 return NULL;
2180 buf[len] = 0;
2181 p = buf + len - 1;
2182 while (p != buf && *p != '\\')
2183 p--;
2184 *p = 0;
2185 if (access(buf, R_OK) == 0) {
2186 return qemu_strdup(buf);
2188 return NULL;
2190 #else /* !_WIN32 */
2192 /* Find a likely location for support files using the location of the binary.
2193 For installed binaries this will be "$bindir/../share/qemu". When
2194 running from the build tree this will be "$bindir/../pc-bios". */
2195 #define SHARE_SUFFIX "/share/qemu"
2196 #define BUILD_SUFFIX "/pc-bios"
2197 static char *find_datadir(const char *argv0)
2199 char *dir;
2200 char *p = NULL;
2201 char *res;
2202 char buf[PATH_MAX];
2203 size_t max_len;
2205 #if defined(__linux__)
2207 int len;
2208 len = readlink("/proc/self/exe", buf, sizeof(buf) - 1);
2209 if (len > 0) {
2210 buf[len] = 0;
2211 p = buf;
2214 #elif defined(__FreeBSD__)
2216 static int mib[4] = {CTL_KERN, KERN_PROC, KERN_PROC_PATHNAME, -1};
2217 size_t len = sizeof(buf) - 1;
2219 *buf = '\0';
2220 if (!sysctl(mib, sizeof(mib)/sizeof(*mib), buf, &len, NULL, 0) &&
2221 *buf) {
2222 buf[sizeof(buf) - 1] = '\0';
2223 p = buf;
2226 #endif
2227 /* If we don't have any way of figuring out the actual executable
2228 location then try argv[0]. */
2229 if (!p) {
2230 p = realpath(argv0, buf);
2231 if (!p) {
2232 return NULL;
2235 dir = dirname(p);
2236 dir = dirname(dir);
2238 max_len = strlen(dir) +
2239 MAX(strlen(SHARE_SUFFIX), strlen(BUILD_SUFFIX)) + 1;
2240 res = qemu_mallocz(max_len);
2241 snprintf(res, max_len, "%s%s", dir, SHARE_SUFFIX);
2242 if (access(res, R_OK)) {
2243 snprintf(res, max_len, "%s%s", dir, BUILD_SUFFIX);
2244 if (access(res, R_OK)) {
2245 qemu_free(res);
2246 res = NULL;
2250 return res;
2252 #undef SHARE_SUFFIX
2253 #undef BUILD_SUFFIX
2254 #endif
2256 char *qemu_find_file(int type, const char *name)
2258 int len;
2259 const char *subdir;
2260 char *buf;
2262 /* If name contains path separators then try it as a straight path. */
2263 if ((strchr(name, '/') || strchr(name, '\\'))
2264 && access(name, R_OK) == 0) {
2265 return qemu_strdup(name);
2267 switch (type) {
2268 case QEMU_FILE_TYPE_BIOS:
2269 subdir = "";
2270 break;
2271 case QEMU_FILE_TYPE_KEYMAP:
2272 subdir = "keymaps/";
2273 break;
2274 default:
2275 abort();
2277 len = strlen(data_dir) + strlen(name) + strlen(subdir) + 2;
2278 buf = qemu_mallocz(len);
2279 snprintf(buf, len, "%s/%s%s", data_dir, subdir, name);
2280 if (access(buf, R_OK)) {
2281 qemu_free(buf);
2282 return NULL;
2284 return buf;
2287 static int device_help_func(QemuOpts *opts, void *opaque)
2289 return qdev_device_help(opts);
2292 static int device_init_func(QemuOpts *opts, void *opaque)
2294 DeviceState *dev;
2296 dev = qdev_device_add(opts);
2297 if (!dev)
2298 return -1;
2299 return 0;
2302 static int chardev_init_func(QemuOpts *opts, void *opaque)
2304 CharDriverState *chr;
2306 chr = qemu_chr_open_opts(opts, NULL);
2307 if (!chr)
2308 return -1;
2309 return 0;
2312 static int mon_init_func(QemuOpts *opts, void *opaque)
2314 CharDriverState *chr;
2315 const char *chardev;
2316 const char *mode;
2317 int flags;
2319 mode = qemu_opt_get(opts, "mode");
2320 if (mode == NULL) {
2321 mode = "readline";
2323 if (strcmp(mode, "readline") == 0) {
2324 flags = MONITOR_USE_READLINE;
2325 } else if (strcmp(mode, "control") == 0) {
2326 flags = MONITOR_USE_CONTROL;
2327 } else {
2328 fprintf(stderr, "unknown monitor mode \"%s\"\n", mode);
2329 exit(1);
2332 if (qemu_opt_get_bool(opts, "default", 0))
2333 flags |= MONITOR_IS_DEFAULT;
2335 chardev = qemu_opt_get(opts, "chardev");
2336 chr = qemu_chr_find(chardev);
2337 if (chr == NULL) {
2338 fprintf(stderr, "chardev \"%s\" not found\n", chardev);
2339 exit(1);
2342 monitor_init(chr, flags);
2343 return 0;
2346 static void monitor_parse(const char *optarg, const char *mode)
2348 static int monitor_device_index = 0;
2349 QemuOpts *opts;
2350 const char *p;
2351 char label[32];
2352 int def = 0;
2354 if (strstart(optarg, "chardev:", &p)) {
2355 snprintf(label, sizeof(label), "%s", p);
2356 } else {
2357 if (monitor_device_index) {
2358 snprintf(label, sizeof(label), "monitor%d",
2359 monitor_device_index);
2360 } else {
2361 snprintf(label, sizeof(label), "monitor");
2362 def = 1;
2364 opts = qemu_chr_parse_compat(label, optarg);
2365 if (!opts) {
2366 fprintf(stderr, "parse error: %s\n", optarg);
2367 exit(1);
2371 opts = qemu_opts_create(&qemu_mon_opts, label, 1);
2372 if (!opts) {
2373 fprintf(stderr, "duplicate chardev: %s\n", label);
2374 exit(1);
2376 qemu_opt_set(opts, "mode", mode);
2377 qemu_opt_set(opts, "chardev", label);
2378 if (def)
2379 qemu_opt_set(opts, "default", "on");
2380 monitor_device_index++;
2383 struct device_config {
2384 enum {
2385 DEV_USB, /* -usbdevice */
2386 DEV_BT, /* -bt */
2387 DEV_SERIAL, /* -serial */
2388 DEV_PARALLEL, /* -parallel */
2389 DEV_VIRTCON, /* -virtioconsole */
2390 DEV_DEBUGCON, /* -debugcon */
2391 } type;
2392 const char *cmdline;
2393 QTAILQ_ENTRY(device_config) next;
2395 QTAILQ_HEAD(, device_config) device_configs = QTAILQ_HEAD_INITIALIZER(device_configs);
2397 static void add_device_config(int type, const char *cmdline)
2399 struct device_config *conf;
2401 conf = qemu_mallocz(sizeof(*conf));
2402 conf->type = type;
2403 conf->cmdline = cmdline;
2404 QTAILQ_INSERT_TAIL(&device_configs, conf, next);
2407 static int foreach_device_config(int type, int (*func)(const char *cmdline))
2409 struct device_config *conf;
2410 int rc;
2412 QTAILQ_FOREACH(conf, &device_configs, next) {
2413 if (conf->type != type)
2414 continue;
2415 rc = func(conf->cmdline);
2416 if (0 != rc)
2417 return rc;
2419 return 0;
2422 static int serial_parse(const char *devname)
2424 static int index = 0;
2425 char label[32];
2427 if (strcmp(devname, "none") == 0)
2428 return 0;
2429 if (index == MAX_SERIAL_PORTS) {
2430 fprintf(stderr, "qemu: too many serial ports\n");
2431 exit(1);
2433 snprintf(label, sizeof(label), "serial%d", index);
2434 serial_hds[index] = qemu_chr_open(label, devname, NULL);
2435 if (!serial_hds[index]) {
2436 fprintf(stderr, "qemu: could not open serial device '%s': %s\n",
2437 devname, strerror(errno));
2438 return -1;
2440 index++;
2441 return 0;
2444 static int parallel_parse(const char *devname)
2446 static int index = 0;
2447 char label[32];
2449 if (strcmp(devname, "none") == 0)
2450 return 0;
2451 if (index == MAX_PARALLEL_PORTS) {
2452 fprintf(stderr, "qemu: too many parallel ports\n");
2453 exit(1);
2455 snprintf(label, sizeof(label), "parallel%d", index);
2456 parallel_hds[index] = qemu_chr_open(label, devname, NULL);
2457 if (!parallel_hds[index]) {
2458 fprintf(stderr, "qemu: could not open parallel device '%s': %s\n",
2459 devname, strerror(errno));
2460 return -1;
2462 index++;
2463 return 0;
2466 static int virtcon_parse(const char *devname)
2468 static int index = 0;
2469 char label[32];
2470 QemuOpts *bus_opts, *dev_opts;
2472 if (strcmp(devname, "none") == 0)
2473 return 0;
2474 if (index == MAX_VIRTIO_CONSOLES) {
2475 fprintf(stderr, "qemu: too many virtio consoles\n");
2476 exit(1);
2479 bus_opts = qemu_opts_create(&qemu_device_opts, NULL, 0);
2480 qemu_opt_set(bus_opts, "driver", "virtio-serial");
2482 dev_opts = qemu_opts_create(&qemu_device_opts, NULL, 0);
2483 qemu_opt_set(dev_opts, "driver", "virtconsole");
2485 snprintf(label, sizeof(label), "virtcon%d", index);
2486 virtcon_hds[index] = qemu_chr_open(label, devname, NULL);
2487 if (!virtcon_hds[index]) {
2488 fprintf(stderr, "qemu: could not open virtio console '%s': %s\n",
2489 devname, strerror(errno));
2490 return -1;
2492 qemu_opt_set(dev_opts, "chardev", label);
2494 index++;
2495 return 0;
2498 static int debugcon_parse(const char *devname)
2500 QemuOpts *opts;
2502 if (!qemu_chr_open("debugcon", devname, NULL)) {
2503 exit(1);
2505 opts = qemu_opts_create(&qemu_device_opts, "debugcon", 1);
2506 if (!opts) {
2507 fprintf(stderr, "qemu: already have a debugcon device\n");
2508 exit(1);
2510 qemu_opt_set(opts, "driver", "isa-debugcon");
2511 qemu_opt_set(opts, "chardev", "debugcon");
2512 return 0;
2515 static const QEMUOption *lookup_opt(int argc, char **argv,
2516 const char **poptarg, int *poptind)
2518 const QEMUOption *popt;
2519 int optind = *poptind;
2520 char *r = argv[optind];
2521 const char *optarg;
2523 loc_set_cmdline(argv, optind, 1);
2524 optind++;
2525 /* Treat --foo the same as -foo. */
2526 if (r[1] == '-')
2527 r++;
2528 popt = qemu_options;
2529 for(;;) {
2530 if (!popt->name) {
2531 error_report("invalid option");
2532 exit(1);
2534 if (!strcmp(popt->name, r + 1))
2535 break;
2536 popt++;
2538 if (popt->flags & HAS_ARG) {
2539 if (optind >= argc) {
2540 error_report("requires an argument");
2541 exit(1);
2543 optarg = argv[optind++];
2544 loc_set_cmdline(argv, optind - 2, 2);
2545 } else {
2546 optarg = NULL;
2549 *poptarg = optarg;
2550 *poptind = optind;
2552 return popt;
2555 int main(int argc, char **argv, char **envp)
2557 const char *gdbstub_dev = NULL;
2558 int i;
2559 int snapshot, linux_boot;
2560 const char *icount_option = NULL;
2561 const char *initrd_filename;
2562 const char *kernel_filename, *kernel_cmdline;
2563 char boot_devices[33] = "cad"; /* default to HD->floppy->CD-ROM */
2564 DisplayState *ds;
2565 DisplayChangeListener *dcl;
2566 int cyls, heads, secs, translation;
2567 QemuOpts *hda_opts = NULL, *opts;
2568 int optind;
2569 const char *optarg;
2570 const char *loadvm = NULL;
2571 QEMUMachine *machine;
2572 const char *cpu_model;
2573 #ifndef _WIN32
2574 int fds[2];
2575 #endif
2576 int tb_size;
2577 const char *pid_file = NULL;
2578 const char *incoming = NULL;
2579 #ifndef _WIN32
2580 int fd = 0;
2581 struct passwd *pwd = NULL;
2582 const char *chroot_dir = NULL;
2583 const char *run_as = NULL;
2584 #endif
2585 int show_vnc_port = 0;
2586 int defconfig = 1;
2588 error_set_progname(argv[0]);
2590 init_clocks();
2592 qemu_cache_utils_init(envp);
2594 QLIST_INIT (&vm_change_state_head);
2595 #ifndef _WIN32
2597 struct sigaction act;
2598 sigfillset(&act.sa_mask);
2599 act.sa_flags = 0;
2600 act.sa_handler = SIG_IGN;
2601 sigaction(SIGPIPE, &act, NULL);
2603 #else
2604 SetConsoleCtrlHandler(qemu_ctrl_handler, TRUE);
2605 /* Note: cpu_interrupt() is currently not SMP safe, so we force
2606 QEMU to run on a single CPU */
2608 HANDLE h;
2609 DWORD mask, smask;
2610 int i;
2611 h = GetCurrentProcess();
2612 if (GetProcessAffinityMask(h, &mask, &smask)) {
2613 for(i = 0; i < 32; i++) {
2614 if (mask & (1 << i))
2615 break;
2617 if (i != 32) {
2618 mask = 1 << i;
2619 SetProcessAffinityMask(h, mask);
2623 #endif
2625 module_call_init(MODULE_INIT_MACHINE);
2626 machine = find_default_machine();
2627 cpu_model = NULL;
2628 initrd_filename = NULL;
2629 ram_size = 0;
2630 snapshot = 0;
2631 kernel_filename = NULL;
2632 kernel_cmdline = "";
2633 cyls = heads = secs = 0;
2634 translation = BIOS_ATA_TRANSLATION_AUTO;
2636 for (i = 0; i < MAX_NODES; i++) {
2637 node_mem[i] = 0;
2638 node_cpumask[i] = 0;
2641 nb_numa_nodes = 0;
2642 nb_nics = 0;
2644 tb_size = 0;
2645 autostart= 1;
2647 /* first pass of option parsing */
2648 optind = 1;
2649 while (optind < argc) {
2650 if (argv[optind][0] != '-') {
2651 /* disk image */
2652 optind++;
2653 continue;
2654 } else {
2655 const QEMUOption *popt;
2657 popt = lookup_opt(argc, argv, &optarg, &optind);
2658 switch (popt->index) {
2659 case QEMU_OPTION_nodefconfig:
2660 defconfig=0;
2661 break;
2666 if (defconfig) {
2667 const char *fname;
2668 FILE *fp;
2670 fname = CONFIG_QEMU_CONFDIR "/qemu.conf";
2671 fp = fopen(fname, "r");
2672 if (fp) {
2673 if (qemu_config_parse(fp, fname) != 0) {
2674 exit(1);
2676 fclose(fp);
2679 fname = arch_config_name;
2680 fp = fopen(fname, "r");
2681 if (fp) {
2682 if (qemu_config_parse(fp, fname) != 0) {
2683 exit(1);
2685 fclose(fp);
2688 cpudef_init();
2690 /* second pass of option parsing */
2691 optind = 1;
2692 for(;;) {
2693 if (optind >= argc)
2694 break;
2695 if (argv[optind][0] != '-') {
2696 hda_opts = drive_add(argv[optind++], HD_ALIAS, 0);
2697 } else {
2698 const QEMUOption *popt;
2700 popt = lookup_opt(argc, argv, &optarg, &optind);
2701 if (!(popt->arch_mask & arch_type)) {
2702 printf("Option %s not supported for this target\n", popt->name);
2703 exit(1);
2705 switch(popt->index) {
2706 case QEMU_OPTION_M:
2707 machine = find_machine(optarg);
2708 if (!machine) {
2709 QEMUMachine *m;
2710 printf("Supported machines are:\n");
2711 for(m = first_machine; m != NULL; m = m->next) {
2712 if (m->alias)
2713 printf("%-10s %s (alias of %s)\n",
2714 m->alias, m->desc, m->name);
2715 printf("%-10s %s%s\n",
2716 m->name, m->desc,
2717 m->is_default ? " (default)" : "");
2719 exit(*optarg != '?');
2721 break;
2722 case QEMU_OPTION_cpu:
2723 /* hw initialization will check this */
2724 if (*optarg == '?') {
2725 /* XXX: implement xxx_cpu_list for targets that still miss it */
2726 #if defined(cpu_list_id)
2727 cpu_list_id(stdout, &fprintf, optarg);
2728 #elif defined(cpu_list)
2729 cpu_list(stdout, &fprintf); /* deprecated */
2730 #endif
2731 exit(0);
2732 } else {
2733 cpu_model = optarg;
2735 break;
2736 case QEMU_OPTION_initrd:
2737 initrd_filename = optarg;
2738 break;
2739 case QEMU_OPTION_hda:
2740 if (cyls == 0)
2741 hda_opts = drive_add(optarg, HD_ALIAS, 0);
2742 else
2743 hda_opts = drive_add(optarg, HD_ALIAS
2744 ",cyls=%d,heads=%d,secs=%d%s",
2745 0, cyls, heads, secs,
2746 translation == BIOS_ATA_TRANSLATION_LBA ?
2747 ",trans=lba" :
2748 translation == BIOS_ATA_TRANSLATION_NONE ?
2749 ",trans=none" : "");
2750 break;
2751 case QEMU_OPTION_hdb:
2752 case QEMU_OPTION_hdc:
2753 case QEMU_OPTION_hdd:
2754 drive_add(optarg, HD_ALIAS, popt->index - QEMU_OPTION_hda);
2755 break;
2756 case QEMU_OPTION_drive:
2757 drive_add(NULL, "%s", optarg);
2758 break;
2759 case QEMU_OPTION_set:
2760 if (qemu_set_option(optarg) != 0)
2761 exit(1);
2762 break;
2763 case QEMU_OPTION_global:
2764 if (qemu_global_option(optarg) != 0)
2765 exit(1);
2766 break;
2767 case QEMU_OPTION_mtdblock:
2768 drive_add(optarg, MTD_ALIAS);
2769 break;
2770 case QEMU_OPTION_sd:
2771 drive_add(optarg, SD_ALIAS);
2772 break;
2773 case QEMU_OPTION_pflash:
2774 drive_add(optarg, PFLASH_ALIAS);
2775 break;
2776 case QEMU_OPTION_snapshot:
2777 snapshot = 1;
2778 break;
2779 case QEMU_OPTION_hdachs:
2781 const char *p;
2782 p = optarg;
2783 cyls = strtol(p, (char **)&p, 0);
2784 if (cyls < 1 || cyls > 16383)
2785 goto chs_fail;
2786 if (*p != ',')
2787 goto chs_fail;
2788 p++;
2789 heads = strtol(p, (char **)&p, 0);
2790 if (heads < 1 || heads > 16)
2791 goto chs_fail;
2792 if (*p != ',')
2793 goto chs_fail;
2794 p++;
2795 secs = strtol(p, (char **)&p, 0);
2796 if (secs < 1 || secs > 63)
2797 goto chs_fail;
2798 if (*p == ',') {
2799 p++;
2800 if (!strcmp(p, "none"))
2801 translation = BIOS_ATA_TRANSLATION_NONE;
2802 else if (!strcmp(p, "lba"))
2803 translation = BIOS_ATA_TRANSLATION_LBA;
2804 else if (!strcmp(p, "auto"))
2805 translation = BIOS_ATA_TRANSLATION_AUTO;
2806 else
2807 goto chs_fail;
2808 } else if (*p != '\0') {
2809 chs_fail:
2810 fprintf(stderr, "qemu: invalid physical CHS format\n");
2811 exit(1);
2813 if (hda_opts != NULL) {
2814 char num[16];
2815 snprintf(num, sizeof(num), "%d", cyls);
2816 qemu_opt_set(hda_opts, "cyls", num);
2817 snprintf(num, sizeof(num), "%d", heads);
2818 qemu_opt_set(hda_opts, "heads", num);
2819 snprintf(num, sizeof(num), "%d", secs);
2820 qemu_opt_set(hda_opts, "secs", num);
2821 if (translation == BIOS_ATA_TRANSLATION_LBA)
2822 qemu_opt_set(hda_opts, "trans", "lba");
2823 if (translation == BIOS_ATA_TRANSLATION_NONE)
2824 qemu_opt_set(hda_opts, "trans", "none");
2827 break;
2828 case QEMU_OPTION_numa:
2829 if (nb_numa_nodes >= MAX_NODES) {
2830 fprintf(stderr, "qemu: too many NUMA nodes\n");
2831 exit(1);
2833 numa_add(optarg);
2834 break;
2835 case QEMU_OPTION_nographic:
2836 display_type = DT_NOGRAPHIC;
2837 break;
2838 #ifdef CONFIG_CURSES
2839 case QEMU_OPTION_curses:
2840 display_type = DT_CURSES;
2841 break;
2842 #endif
2843 case QEMU_OPTION_portrait:
2844 graphic_rotate = 1;
2845 break;
2846 case QEMU_OPTION_kernel:
2847 kernel_filename = optarg;
2848 break;
2849 case QEMU_OPTION_append:
2850 kernel_cmdline = optarg;
2851 break;
2852 case QEMU_OPTION_cdrom:
2853 drive_add(optarg, CDROM_ALIAS);
2854 break;
2855 case QEMU_OPTION_boot:
2857 static const char * const params[] = {
2858 "order", "once", "menu", NULL
2860 char buf[sizeof(boot_devices)];
2861 char *standard_boot_devices;
2862 int legacy = 0;
2864 if (!strchr(optarg, '=')) {
2865 legacy = 1;
2866 pstrcpy(buf, sizeof(buf), optarg);
2867 } else if (check_params(buf, sizeof(buf), params, optarg) < 0) {
2868 fprintf(stderr,
2869 "qemu: unknown boot parameter '%s' in '%s'\n",
2870 buf, optarg);
2871 exit(1);
2874 if (legacy ||
2875 get_param_value(buf, sizeof(buf), "order", optarg)) {
2876 validate_bootdevices(buf);
2877 pstrcpy(boot_devices, sizeof(boot_devices), buf);
2879 if (!legacy) {
2880 if (get_param_value(buf, sizeof(buf),
2881 "once", optarg)) {
2882 validate_bootdevices(buf);
2883 standard_boot_devices = qemu_strdup(boot_devices);
2884 pstrcpy(boot_devices, sizeof(boot_devices), buf);
2885 qemu_register_reset(restore_boot_devices,
2886 standard_boot_devices);
2888 if (get_param_value(buf, sizeof(buf),
2889 "menu", optarg)) {
2890 if (!strcmp(buf, "on")) {
2891 boot_menu = 1;
2892 } else if (!strcmp(buf, "off")) {
2893 boot_menu = 0;
2894 } else {
2895 fprintf(stderr,
2896 "qemu: invalid option value '%s'\n",
2897 buf);
2898 exit(1);
2903 break;
2904 case QEMU_OPTION_fda:
2905 case QEMU_OPTION_fdb:
2906 drive_add(optarg, FD_ALIAS, popt->index - QEMU_OPTION_fda);
2907 break;
2908 case QEMU_OPTION_no_fd_bootchk:
2909 fd_bootchk = 0;
2910 break;
2911 case QEMU_OPTION_netdev:
2912 if (net_client_parse(&qemu_netdev_opts, optarg) == -1) {
2913 exit(1);
2915 break;
2916 case QEMU_OPTION_net:
2917 if (net_client_parse(&qemu_net_opts, optarg) == -1) {
2918 exit(1);
2920 break;
2921 #ifdef CONFIG_SLIRP
2922 case QEMU_OPTION_tftp:
2923 legacy_tftp_prefix = optarg;
2924 break;
2925 case QEMU_OPTION_bootp:
2926 legacy_bootp_filename = optarg;
2927 break;
2928 #ifndef _WIN32
2929 case QEMU_OPTION_smb:
2930 if (net_slirp_smb(optarg) < 0)
2931 exit(1);
2932 break;
2933 #endif
2934 case QEMU_OPTION_redir:
2935 if (net_slirp_redir(optarg) < 0)
2936 exit(1);
2937 break;
2938 #endif
2939 case QEMU_OPTION_bt:
2940 add_device_config(DEV_BT, optarg);
2941 break;
2942 case QEMU_OPTION_audio_help:
2943 if (!(audio_available())) {
2944 printf("Option %s not supported for this target\n", popt->name);
2945 exit(1);
2947 AUD_help ();
2948 exit (0);
2949 break;
2950 case QEMU_OPTION_soundhw:
2951 if (!(audio_available())) {
2952 printf("Option %s not supported for this target\n", popt->name);
2953 exit(1);
2955 select_soundhw (optarg);
2956 break;
2957 case QEMU_OPTION_h:
2958 help(0);
2959 break;
2960 case QEMU_OPTION_version:
2961 version();
2962 exit(0);
2963 break;
2964 case QEMU_OPTION_m: {
2965 uint64_t value;
2966 char *ptr;
2968 value = strtoul(optarg, &ptr, 10);
2969 switch (*ptr) {
2970 case 0: case 'M': case 'm':
2971 value <<= 20;
2972 break;
2973 case 'G': case 'g':
2974 value <<= 30;
2975 break;
2976 default:
2977 fprintf(stderr, "qemu: invalid ram size: %s\n", optarg);
2978 exit(1);
2981 /* On 32-bit hosts, QEMU is limited by virtual address space */
2982 if (value > (2047 << 20) && HOST_LONG_BITS == 32) {
2983 fprintf(stderr, "qemu: at most 2047 MB RAM can be simulated\n");
2984 exit(1);
2986 if (value != (uint64_t)(ram_addr_t)value) {
2987 fprintf(stderr, "qemu: ram size too large\n");
2988 exit(1);
2990 ram_size = value;
2991 break;
2993 case QEMU_OPTION_mempath:
2994 mem_path = optarg;
2995 break;
2996 #ifdef MAP_POPULATE
2997 case QEMU_OPTION_mem_prealloc:
2998 mem_prealloc = 1;
2999 break;
3000 #endif
3001 case QEMU_OPTION_d:
3002 set_cpu_log(optarg);
3003 break;
3004 case QEMU_OPTION_s:
3005 gdbstub_dev = "tcp::" DEFAULT_GDBSTUB_PORT;
3006 break;
3007 case QEMU_OPTION_gdb:
3008 gdbstub_dev = optarg;
3009 break;
3010 case QEMU_OPTION_L:
3011 data_dir = optarg;
3012 break;
3013 case QEMU_OPTION_bios:
3014 bios_name = optarg;
3015 break;
3016 case QEMU_OPTION_singlestep:
3017 singlestep = 1;
3018 break;
3019 case QEMU_OPTION_S:
3020 autostart = 0;
3021 break;
3022 case QEMU_OPTION_k:
3023 keyboard_layout = optarg;
3024 break;
3025 case QEMU_OPTION_localtime:
3026 rtc_utc = 0;
3027 break;
3028 case QEMU_OPTION_vga:
3029 select_vgahw (optarg);
3030 break;
3031 case QEMU_OPTION_g:
3033 const char *p;
3034 int w, h, depth;
3035 p = optarg;
3036 w = strtol(p, (char **)&p, 10);
3037 if (w <= 0) {
3038 graphic_error:
3039 fprintf(stderr, "qemu: invalid resolution or depth\n");
3040 exit(1);
3042 if (*p != 'x')
3043 goto graphic_error;
3044 p++;
3045 h = strtol(p, (char **)&p, 10);
3046 if (h <= 0)
3047 goto graphic_error;
3048 if (*p == 'x') {
3049 p++;
3050 depth = strtol(p, (char **)&p, 10);
3051 if (depth != 8 && depth != 15 && depth != 16 &&
3052 depth != 24 && depth != 32)
3053 goto graphic_error;
3054 } else if (*p == '\0') {
3055 depth = graphic_depth;
3056 } else {
3057 goto graphic_error;
3060 graphic_width = w;
3061 graphic_height = h;
3062 graphic_depth = depth;
3064 break;
3065 case QEMU_OPTION_echr:
3067 char *r;
3068 term_escape_char = strtol(optarg, &r, 0);
3069 if (r == optarg)
3070 printf("Bad argument to echr\n");
3071 break;
3073 case QEMU_OPTION_monitor:
3074 monitor_parse(optarg, "readline");
3075 default_monitor = 0;
3076 break;
3077 case QEMU_OPTION_qmp:
3078 monitor_parse(optarg, "control");
3079 default_monitor = 0;
3080 break;
3081 case QEMU_OPTION_mon:
3082 opts = qemu_opts_parse(&qemu_mon_opts, optarg, 1);
3083 if (!opts) {
3084 fprintf(stderr, "parse error: %s\n", optarg);
3085 exit(1);
3087 default_monitor = 0;
3088 break;
3089 case QEMU_OPTION_chardev:
3090 opts = qemu_opts_parse(&qemu_chardev_opts, optarg, 1);
3091 if (!opts) {
3092 fprintf(stderr, "parse error: %s\n", optarg);
3093 exit(1);
3095 break;
3096 case QEMU_OPTION_serial:
3097 add_device_config(DEV_SERIAL, optarg);
3098 default_serial = 0;
3099 if (strncmp(optarg, "mon:", 4) == 0) {
3100 default_monitor = 0;
3102 break;
3103 case QEMU_OPTION_watchdog:
3104 if (watchdog) {
3105 fprintf(stderr,
3106 "qemu: only one watchdog option may be given\n");
3107 return 1;
3109 watchdog = optarg;
3110 break;
3111 case QEMU_OPTION_watchdog_action:
3112 if (select_watchdog_action(optarg) == -1) {
3113 fprintf(stderr, "Unknown -watchdog-action parameter\n");
3114 exit(1);
3116 break;
3117 case QEMU_OPTION_virtiocon:
3118 add_device_config(DEV_VIRTCON, optarg);
3119 default_virtcon = 0;
3120 if (strncmp(optarg, "mon:", 4) == 0) {
3121 default_monitor = 0;
3123 break;
3124 case QEMU_OPTION_parallel:
3125 add_device_config(DEV_PARALLEL, optarg);
3126 default_parallel = 0;
3127 if (strncmp(optarg, "mon:", 4) == 0) {
3128 default_monitor = 0;
3130 break;
3131 case QEMU_OPTION_debugcon:
3132 add_device_config(DEV_DEBUGCON, optarg);
3133 break;
3134 case QEMU_OPTION_loadvm:
3135 loadvm = optarg;
3136 break;
3137 case QEMU_OPTION_full_screen:
3138 full_screen = 1;
3139 break;
3140 #ifdef CONFIG_SDL
3141 case QEMU_OPTION_no_frame:
3142 no_frame = 1;
3143 break;
3144 case QEMU_OPTION_alt_grab:
3145 alt_grab = 1;
3146 break;
3147 case QEMU_OPTION_ctrl_grab:
3148 ctrl_grab = 1;
3149 break;
3150 case QEMU_OPTION_no_quit:
3151 no_quit = 1;
3152 break;
3153 case QEMU_OPTION_sdl:
3154 display_type = DT_SDL;
3155 break;
3156 #endif
3157 case QEMU_OPTION_pidfile:
3158 pid_file = optarg;
3159 break;
3160 case QEMU_OPTION_win2k_hack:
3161 win2k_install_hack = 1;
3162 break;
3163 case QEMU_OPTION_rtc_td_hack:
3164 rtc_td_hack = 1;
3165 break;
3166 case QEMU_OPTION_acpitable:
3167 do_acpitable_option(optarg);
3168 break;
3169 case QEMU_OPTION_smbios:
3170 do_smbios_option(optarg);
3171 break;
3172 case QEMU_OPTION_enable_kvm:
3173 if (!(kvm_available())) {
3174 printf("Option %s not supported for this target\n", popt->name);
3175 exit(1);
3177 kvm_allowed = 1;
3178 break;
3179 case QEMU_OPTION_usb:
3180 usb_enabled = 1;
3181 break;
3182 case QEMU_OPTION_usbdevice:
3183 usb_enabled = 1;
3184 add_device_config(DEV_USB, optarg);
3185 break;
3186 case QEMU_OPTION_device:
3187 if (!qemu_opts_parse(&qemu_device_opts, optarg, 1)) {
3188 exit(1);
3190 break;
3191 case QEMU_OPTION_smp:
3192 smp_parse(optarg);
3193 if (smp_cpus < 1) {
3194 fprintf(stderr, "Invalid number of CPUs\n");
3195 exit(1);
3197 if (max_cpus < smp_cpus) {
3198 fprintf(stderr, "maxcpus must be equal to or greater than "
3199 "smp\n");
3200 exit(1);
3202 if (max_cpus > 255) {
3203 fprintf(stderr, "Unsupported number of maxcpus\n");
3204 exit(1);
3206 break;
3207 case QEMU_OPTION_vnc:
3208 display_type = DT_VNC;
3209 vnc_display = optarg;
3210 break;
3211 case QEMU_OPTION_no_acpi:
3212 acpi_enabled = 0;
3213 break;
3214 case QEMU_OPTION_no_hpet:
3215 no_hpet = 1;
3216 break;
3217 case QEMU_OPTION_balloon:
3218 if (balloon_parse(optarg) < 0) {
3219 fprintf(stderr, "Unknown -balloon argument %s\n", optarg);
3220 exit(1);
3222 break;
3223 case QEMU_OPTION_no_reboot:
3224 no_reboot = 1;
3225 break;
3226 case QEMU_OPTION_no_shutdown:
3227 no_shutdown = 1;
3228 break;
3229 case QEMU_OPTION_show_cursor:
3230 cursor_hide = 0;
3231 break;
3232 case QEMU_OPTION_uuid:
3233 if(qemu_uuid_parse(optarg, qemu_uuid) < 0) {
3234 fprintf(stderr, "Fail to parse UUID string."
3235 " Wrong format.\n");
3236 exit(1);
3238 break;
3239 #ifndef _WIN32
3240 case QEMU_OPTION_daemonize:
3241 daemonize = 1;
3242 break;
3243 #endif
3244 case QEMU_OPTION_option_rom:
3245 if (nb_option_roms >= MAX_OPTION_ROMS) {
3246 fprintf(stderr, "Too many option ROMs\n");
3247 exit(1);
3249 option_rom[nb_option_roms] = optarg;
3250 nb_option_roms++;
3251 break;
3252 case QEMU_OPTION_semihosting:
3253 semihosting_enabled = 1;
3254 break;
3255 case QEMU_OPTION_name:
3256 qemu_name = qemu_strdup(optarg);
3258 char *p = strchr(qemu_name, ',');
3259 if (p != NULL) {
3260 *p++ = 0;
3261 if (strncmp(p, "process=", 8)) {
3262 fprintf(stderr, "Unknown subargument %s to -name", p);
3263 exit(1);
3265 p += 8;
3266 set_proc_name(p);
3269 break;
3270 case QEMU_OPTION_prom_env:
3271 if (nb_prom_envs >= MAX_PROM_ENVS) {
3272 fprintf(stderr, "Too many prom variables\n");
3273 exit(1);
3275 prom_envs[nb_prom_envs] = optarg;
3276 nb_prom_envs++;
3277 break;
3278 case QEMU_OPTION_old_param:
3279 old_param = 1;
3280 break;
3281 case QEMU_OPTION_clock:
3282 configure_alarms(optarg);
3283 break;
3284 case QEMU_OPTION_startdate:
3285 configure_rtc_date_offset(optarg, 1);
3286 break;
3287 case QEMU_OPTION_rtc:
3288 opts = qemu_opts_parse(&qemu_rtc_opts, optarg, 0);
3289 if (!opts) {
3290 fprintf(stderr, "parse error: %s\n", optarg);
3291 exit(1);
3293 configure_rtc(opts);
3294 break;
3295 case QEMU_OPTION_tb_size:
3296 tb_size = strtol(optarg, NULL, 0);
3297 if (tb_size < 0)
3298 tb_size = 0;
3299 break;
3300 case QEMU_OPTION_icount:
3301 icount_option = optarg;
3302 break;
3303 case QEMU_OPTION_incoming:
3304 incoming = optarg;
3305 break;
3306 case QEMU_OPTION_nodefaults:
3307 default_serial = 0;
3308 default_parallel = 0;
3309 default_virtcon = 0;
3310 default_monitor = 0;
3311 default_vga = 0;
3312 default_net = 0;
3313 default_floppy = 0;
3314 default_cdrom = 0;
3315 default_sdcard = 0;
3316 break;
3317 #ifndef _WIN32
3318 case QEMU_OPTION_chroot:
3319 chroot_dir = optarg;
3320 break;
3321 case QEMU_OPTION_runas:
3322 run_as = optarg;
3323 break;
3324 #endif
3325 case QEMU_OPTION_xen_domid:
3326 if (!(xen_available())) {
3327 printf("Option %s not supported for this target\n", popt->name);
3328 exit(1);
3330 xen_domid = atoi(optarg);
3331 break;
3332 case QEMU_OPTION_xen_create:
3333 if (!(xen_available())) {
3334 printf("Option %s not supported for this target\n", popt->name);
3335 exit(1);
3337 xen_mode = XEN_CREATE;
3338 break;
3339 case QEMU_OPTION_xen_attach:
3340 if (!(xen_available())) {
3341 printf("Option %s not supported for this target\n", popt->name);
3342 exit(1);
3344 xen_mode = XEN_ATTACH;
3345 break;
3346 case QEMU_OPTION_readconfig:
3348 FILE *fp;
3349 fp = fopen(optarg, "r");
3350 if (fp == NULL) {
3351 fprintf(stderr, "open %s: %s\n", optarg, strerror(errno));
3352 exit(1);
3354 if (qemu_config_parse(fp, optarg) != 0) {
3355 exit(1);
3357 fclose(fp);
3358 break;
3360 case QEMU_OPTION_writeconfig:
3362 FILE *fp;
3363 if (strcmp(optarg, "-") == 0) {
3364 fp = stdout;
3365 } else {
3366 fp = fopen(optarg, "w");
3367 if (fp == NULL) {
3368 fprintf(stderr, "open %s: %s\n", optarg, strerror(errno));
3369 exit(1);
3372 qemu_config_write(fp);
3373 fclose(fp);
3374 break;
3379 loc_set_none();
3381 /* If no data_dir is specified then try to find it relative to the
3382 executable path. */
3383 if (!data_dir) {
3384 data_dir = find_datadir(argv[0]);
3386 /* If all else fails use the install patch specified when building. */
3387 if (!data_dir) {
3388 data_dir = CONFIG_QEMU_SHAREDIR;
3392 * Default to max_cpus = smp_cpus, in case the user doesn't
3393 * specify a max_cpus value.
3395 if (!max_cpus)
3396 max_cpus = smp_cpus;
3398 machine->max_cpus = machine->max_cpus ?: 1; /* Default to UP */
3399 if (smp_cpus > machine->max_cpus) {
3400 fprintf(stderr, "Number of SMP cpus requested (%d), exceeds max cpus "
3401 "supported by machine `%s' (%d)\n", smp_cpus, machine->name,
3402 machine->max_cpus);
3403 exit(1);
3406 qemu_opts_foreach(&qemu_device_opts, default_driver_check, NULL, 0);
3407 qemu_opts_foreach(&qemu_global_opts, default_driver_check, NULL, 0);
3409 if (machine->no_serial) {
3410 default_serial = 0;
3412 if (machine->no_parallel) {
3413 default_parallel = 0;
3415 if (!machine->use_virtcon) {
3416 default_virtcon = 0;
3418 if (machine->no_vga) {
3419 default_vga = 0;
3421 if (machine->no_floppy) {
3422 default_floppy = 0;
3424 if (machine->no_cdrom) {
3425 default_cdrom = 0;
3427 if (machine->no_sdcard) {
3428 default_sdcard = 0;
3431 if (display_type == DT_NOGRAPHIC) {
3432 if (default_parallel)
3433 add_device_config(DEV_PARALLEL, "null");
3434 if (default_serial && default_monitor) {
3435 add_device_config(DEV_SERIAL, "mon:stdio");
3436 } else if (default_virtcon && default_monitor) {
3437 add_device_config(DEV_VIRTCON, "mon:stdio");
3438 } else {
3439 if (default_serial)
3440 add_device_config(DEV_SERIAL, "stdio");
3441 if (default_virtcon)
3442 add_device_config(DEV_VIRTCON, "stdio");
3443 if (default_monitor)
3444 monitor_parse("stdio", "readline");
3446 } else {
3447 if (default_serial)
3448 add_device_config(DEV_SERIAL, "vc:80Cx24C");
3449 if (default_parallel)
3450 add_device_config(DEV_PARALLEL, "vc:80Cx24C");
3451 if (default_monitor)
3452 monitor_parse("vc:80Cx24C", "readline");
3453 if (default_virtcon)
3454 add_device_config(DEV_VIRTCON, "vc:80Cx24C");
3456 if (default_vga)
3457 vga_interface_type = VGA_CIRRUS;
3459 if (qemu_opts_foreach(&qemu_chardev_opts, chardev_init_func, NULL, 1) != 0)
3460 exit(1);
3462 #ifndef _WIN32
3463 if (daemonize) {
3464 pid_t pid;
3466 if (pipe(fds) == -1)
3467 exit(1);
3469 pid = fork();
3470 if (pid > 0) {
3471 uint8_t status;
3472 ssize_t len;
3474 close(fds[1]);
3476 again:
3477 len = read(fds[0], &status, 1);
3478 if (len == -1 && (errno == EINTR))
3479 goto again;
3481 if (len != 1)
3482 exit(1);
3483 else if (status == 1) {
3484 fprintf(stderr, "Could not acquire pidfile: %s\n", strerror(errno));
3485 exit(1);
3486 } else
3487 exit(0);
3488 } else if (pid < 0)
3489 exit(1);
3491 close(fds[0]);
3492 qemu_set_cloexec(fds[1]);
3494 setsid();
3496 pid = fork();
3497 if (pid > 0)
3498 exit(0);
3499 else if (pid < 0)
3500 exit(1);
3502 umask(027);
3504 signal(SIGTSTP, SIG_IGN);
3505 signal(SIGTTOU, SIG_IGN);
3506 signal(SIGTTIN, SIG_IGN);
3508 #endif
3510 if (pid_file && qemu_create_pidfile(pid_file) != 0) {
3511 #ifndef _WIN32
3512 if (daemonize) {
3513 uint8_t status = 1;
3514 if (write(fds[1], &status, 1) != 1) {
3515 perror("daemonize. Writing to pipe\n");
3517 } else
3518 #endif
3519 fprintf(stderr, "Could not acquire pid file: %s\n", strerror(errno));
3520 exit(1);
3523 if (kvm_enabled()) {
3524 int ret;
3526 ret = kvm_init(smp_cpus);
3527 if (ret < 0) {
3528 fprintf(stderr, "failed to initialize KVM\n");
3529 exit(1);
3533 if (qemu_init_main_loop()) {
3534 fprintf(stderr, "qemu_init_main_loop failed\n");
3535 exit(1);
3537 linux_boot = (kernel_filename != NULL);
3539 if (!linux_boot && *kernel_cmdline != '\0') {
3540 fprintf(stderr, "-append only allowed with -kernel option\n");
3541 exit(1);
3544 if (!linux_boot && initrd_filename != NULL) {
3545 fprintf(stderr, "-initrd only allowed with -kernel option\n");
3546 exit(1);
3549 #ifndef _WIN32
3550 /* Win32 doesn't support line-buffering and requires size >= 2 */
3551 setvbuf(stdout, NULL, _IOLBF, 0);
3552 #endif
3554 if (init_timer_alarm() < 0) {
3555 fprintf(stderr, "could not initialize alarm timer\n");
3556 exit(1);
3558 configure_icount(icount_option);
3560 socket_init();
3562 if (net_init_clients() < 0) {
3563 exit(1);
3566 /* init the bluetooth world */
3567 if (foreach_device_config(DEV_BT, bt_parse))
3568 exit(1);
3570 /* init the memory */
3571 if (ram_size == 0)
3572 ram_size = DEFAULT_RAM_SIZE * 1024 * 1024;
3574 /* init the dynamic translator */
3575 cpu_exec_init_all(tb_size * 1024 * 1024);
3577 bdrv_init_with_whitelist();
3579 blk_mig_init();
3581 if (default_cdrom) {
3582 /* we always create the cdrom drive, even if no disk is there */
3583 drive_add(NULL, CDROM_ALIAS);
3586 if (default_floppy) {
3587 /* we always create at least one floppy */
3588 drive_add(NULL, FD_ALIAS, 0);
3591 if (default_sdcard) {
3592 /* we always create one sd slot, even if no card is in it */
3593 drive_add(NULL, SD_ALIAS);
3596 /* open the virtual block devices */
3597 if (snapshot)
3598 qemu_opts_foreach(&qemu_drive_opts, drive_enable_snapshot, NULL, 0);
3599 if (qemu_opts_foreach(&qemu_drive_opts, drive_init_func, machine, 1) != 0)
3600 exit(1);
3602 register_savevm_live("ram", 0, 3, NULL, ram_save_live, NULL,
3603 ram_load, NULL);
3605 if (nb_numa_nodes > 0) {
3606 int i;
3608 if (nb_numa_nodes > smp_cpus) {
3609 nb_numa_nodes = smp_cpus;
3612 /* If no memory size if given for any node, assume the default case
3613 * and distribute the available memory equally across all nodes
3615 for (i = 0; i < nb_numa_nodes; i++) {
3616 if (node_mem[i] != 0)
3617 break;
3619 if (i == nb_numa_nodes) {
3620 uint64_t usedmem = 0;
3622 /* On Linux, the each node's border has to be 8MB aligned,
3623 * the final node gets the rest.
3625 for (i = 0; i < nb_numa_nodes - 1; i++) {
3626 node_mem[i] = (ram_size / nb_numa_nodes) & ~((1 << 23UL) - 1);
3627 usedmem += node_mem[i];
3629 node_mem[i] = ram_size - usedmem;
3632 for (i = 0; i < nb_numa_nodes; i++) {
3633 if (node_cpumask[i] != 0)
3634 break;
3636 /* assigning the VCPUs round-robin is easier to implement, guest OSes
3637 * must cope with this anyway, because there are BIOSes out there in
3638 * real machines which also use this scheme.
3640 if (i == nb_numa_nodes) {
3641 for (i = 0; i < smp_cpus; i++) {
3642 node_cpumask[i % nb_numa_nodes] |= 1 << i;
3647 if (foreach_device_config(DEV_SERIAL, serial_parse) < 0)
3648 exit(1);
3649 if (foreach_device_config(DEV_PARALLEL, parallel_parse) < 0)
3650 exit(1);
3651 if (foreach_device_config(DEV_VIRTCON, virtcon_parse) < 0)
3652 exit(1);
3653 if (foreach_device_config(DEV_DEBUGCON, debugcon_parse) < 0)
3654 exit(1);
3656 module_call_init(MODULE_INIT_DEVICE);
3658 if (qemu_opts_foreach(&qemu_device_opts, device_help_func, NULL, 0) != 0)
3659 exit(0);
3661 if (watchdog) {
3662 i = select_watchdog(watchdog);
3663 if (i > 0)
3664 exit (i == 1 ? 1 : 0);
3667 if (machine->compat_props) {
3668 qdev_prop_register_global_list(machine->compat_props);
3670 qemu_add_globals();
3672 machine->init(ram_size, boot_devices,
3673 kernel_filename, kernel_cmdline, initrd_filename, cpu_model);
3675 cpu_synchronize_all_post_init();
3677 #ifndef _WIN32
3678 /* must be after terminal init, SDL library changes signal handlers */
3679 sighandler_setup();
3680 #endif
3682 set_numa_modes();
3684 current_machine = machine;
3686 /* init USB devices */
3687 if (usb_enabled) {
3688 if (foreach_device_config(DEV_USB, usb_parse) < 0)
3689 exit(1);
3692 /* init generic devices */
3693 if (qemu_opts_foreach(&qemu_device_opts, device_init_func, NULL, 1) != 0)
3694 exit(1);
3696 net_check_clients();
3698 /* just use the first displaystate for the moment */
3699 ds = get_displaystate();
3701 if (display_type == DT_DEFAULT) {
3702 #if defined(CONFIG_SDL) || defined(CONFIG_COCOA)
3703 display_type = DT_SDL;
3704 #else
3705 display_type = DT_VNC;
3706 vnc_display = "localhost:0,to=99";
3707 show_vnc_port = 1;
3708 #endif
3712 switch (display_type) {
3713 case DT_NOGRAPHIC:
3714 break;
3715 #if defined(CONFIG_CURSES)
3716 case DT_CURSES:
3717 curses_display_init(ds, full_screen);
3718 break;
3719 #endif
3720 #if defined(CONFIG_SDL)
3721 case DT_SDL:
3722 sdl_display_init(ds, full_screen, no_frame);
3723 break;
3724 #elif defined(CONFIG_COCOA)
3725 case DT_SDL:
3726 cocoa_display_init(ds, full_screen);
3727 break;
3728 #endif
3729 case DT_VNC:
3730 vnc_display_init(ds);
3731 if (vnc_display_open(ds, vnc_display) < 0)
3732 exit(1);
3734 if (show_vnc_port) {
3735 printf("VNC server running on `%s'\n", vnc_display_local_addr(ds));
3737 break;
3738 default:
3739 break;
3741 dpy_resize(ds);
3743 dcl = ds->listeners;
3744 while (dcl != NULL) {
3745 if (dcl->dpy_refresh != NULL) {
3746 ds->gui_timer = qemu_new_timer(rt_clock, gui_update, ds);
3747 qemu_mod_timer(ds->gui_timer, qemu_get_clock(rt_clock));
3749 dcl = dcl->next;
3752 if (display_type == DT_NOGRAPHIC || display_type == DT_VNC) {
3753 nographic_timer = qemu_new_timer(rt_clock, nographic_update, NULL);
3754 qemu_mod_timer(nographic_timer, qemu_get_clock(rt_clock));
3757 text_consoles_set_display(ds);
3759 if (qemu_opts_foreach(&qemu_mon_opts, mon_init_func, NULL, 1) != 0)
3760 exit(1);
3762 if (gdbstub_dev && gdbserver_start(gdbstub_dev) < 0) {
3763 fprintf(stderr, "qemu: could not open gdbserver on device '%s'\n",
3764 gdbstub_dev);
3765 exit(1);
3768 qdev_machine_creation_done();
3770 if (rom_load_all() != 0) {
3771 fprintf(stderr, "rom loading failed\n");
3772 exit(1);
3775 qemu_system_reset();
3776 if (loadvm) {
3777 if (load_vmstate(loadvm) < 0) {
3778 autostart = 0;
3782 if (incoming) {
3783 qemu_start_incoming_migration(incoming);
3784 } else if (autostart) {
3785 vm_start();
3788 #ifndef _WIN32
3789 if (daemonize) {
3790 uint8_t status = 0;
3791 ssize_t len;
3793 again1:
3794 len = write(fds[1], &status, 1);
3795 if (len == -1 && (errno == EINTR))
3796 goto again1;
3798 if (len != 1)
3799 exit(1);
3801 if (chdir("/")) {
3802 perror("not able to chdir to /");
3803 exit(1);
3805 TFR(fd = qemu_open("/dev/null", O_RDWR));
3806 if (fd == -1)
3807 exit(1);
3810 if (run_as) {
3811 pwd = getpwnam(run_as);
3812 if (!pwd) {
3813 fprintf(stderr, "User \"%s\" doesn't exist\n", run_as);
3814 exit(1);
3818 if (chroot_dir) {
3819 if (chroot(chroot_dir) < 0) {
3820 fprintf(stderr, "chroot failed\n");
3821 exit(1);
3823 if (chdir("/")) {
3824 perror("not able to chdir to /");
3825 exit(1);
3829 if (run_as) {
3830 if (setgid(pwd->pw_gid) < 0) {
3831 fprintf(stderr, "Failed to setgid(%d)\n", pwd->pw_gid);
3832 exit(1);
3834 if (setuid(pwd->pw_uid) < 0) {
3835 fprintf(stderr, "Failed to setuid(%d)\n", pwd->pw_uid);
3836 exit(1);
3838 if (setuid(0) != -1) {
3839 fprintf(stderr, "Dropping privileges failed\n");
3840 exit(1);
3844 if (daemonize) {
3845 dup2(fd, 0);
3846 dup2(fd, 1);
3847 dup2(fd, 2);
3849 close(fd);
3851 #endif
3853 main_loop();
3854 quit_timers();
3855 net_cleanup();
3857 return 0;