virtio-blk: stop tracking old_bs
[qemu/kraxel.git] / vl.c
blobdf18198a1ea656e58d376f424c2a646ad408ecf2
1 /*
2 * QEMU System Emulator
4 * Copyright (c) 2003-2008 Fabrice Bellard
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 * THE SOFTWARE.
24 #include <unistd.h>
25 #include <fcntl.h>
26 #include <signal.h>
27 #include <time.h>
28 #include <errno.h>
29 #include <sys/time.h>
30 #include <zlib.h>
32 /* Needed early for CONFIG_BSD etc. */
33 #include "config-host.h"
35 #ifndef _WIN32
36 #include <libgen.h>
37 #include <sys/times.h>
38 #include <sys/wait.h>
39 #include <termios.h>
40 #include <sys/mman.h>
41 #include <sys/ioctl.h>
42 #include <sys/resource.h>
43 #include <sys/socket.h>
44 #include <netinet/in.h>
45 #include <net/if.h>
46 #include <arpa/inet.h>
47 #include <dirent.h>
48 #include <netdb.h>
49 #include <sys/select.h>
50 #ifdef CONFIG_BSD
51 #include <sys/stat.h>
52 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__DragonFly__)
53 #include <libutil.h>
54 #include <sys/sysctl.h>
55 #else
56 #include <util.h>
57 #endif
58 #else
59 #ifdef __linux__
60 #include <pty.h>
61 #include <malloc.h>
63 #include <linux/ppdev.h>
64 #include <linux/parport.h>
65 #endif
66 #ifdef __sun__
67 #include <sys/stat.h>
68 #include <sys/ethernet.h>
69 #include <sys/sockio.h>
70 #include <netinet/arp.h>
71 #include <netinet/in_systm.h>
72 #include <netinet/ip.h>
73 #include <netinet/ip_icmp.h> // must come after ip.h
74 #include <netinet/udp.h>
75 #include <netinet/tcp.h>
76 #include <net/if.h>
77 #include <syslog.h>
78 #include <stropts.h>
79 /* See MySQL bug #7156 (http://bugs.mysql.com/bug.php?id=7156) for
80 discussion about Solaris header problems */
81 extern int madvise(caddr_t, size_t, int);
82 #endif
83 #endif
84 #endif
86 #if defined(__OpenBSD__)
87 #include <util.h>
88 #endif
90 #if defined(CONFIG_VDE)
91 #include <libvdeplug.h>
92 #endif
94 #ifdef _WIN32
95 #include <windows.h>
96 #endif
98 #ifdef CONFIG_SDL
99 #if defined(__APPLE__) || defined(main)
100 #include <SDL.h>
101 int qemu_main(int argc, char **argv, char **envp);
102 int main(int argc, char **argv)
104 return qemu_main(argc, argv, NULL);
106 #undef main
107 #define main qemu_main
108 #endif
109 #endif /* CONFIG_SDL */
111 #ifdef CONFIG_COCOA
112 #undef main
113 #define main qemu_main
114 #endif /* CONFIG_COCOA */
116 #include "hw/hw.h"
117 #include "hw/boards.h"
118 #include "hw/usb.h"
119 #include "hw/pcmcia.h"
120 #include "hw/pc.h"
121 #include "hw/isa.h"
122 #include "hw/baum.h"
123 #include "hw/bt.h"
124 #include "hw/watchdog.h"
125 #include "hw/smbios.h"
126 #include "hw/xen.h"
127 #include "hw/qdev.h"
128 #include "hw/loader.h"
129 #include "bt-host.h"
130 #include "net.h"
131 #include "net/slirp.h"
132 #include "monitor.h"
133 #include "console.h"
134 #include "sysemu.h"
135 #include "gdbstub.h"
136 #include "qemu-timer.h"
137 #include "qemu-char.h"
138 #include "cache-utils.h"
139 #include "block.h"
140 #include "blockdev.h"
141 #include "block-migration.h"
142 #include "dma.h"
143 #include "audio/audio.h"
144 #include "migration.h"
145 #include "kvm.h"
146 #include "qemu-option.h"
147 #include "qemu-config.h"
148 #include "qemu-objects.h"
149 #include "qemu-options.h"
150 #ifdef CONFIG_LINUX
151 #include "fsdev/qemu-fsdev.h"
152 #endif
154 #include "disas.h"
156 #include "qemu_socket.h"
158 #include "slirp/libslirp.h"
160 #include "qemu-queue.h"
161 #include "cpus.h"
162 #include "arch_init.h"
164 //#define DEBUG_NET
165 //#define DEBUG_SLIRP
167 #define DEFAULT_RAM_SIZE 128
169 #define MAX_VIRTIO_CONSOLES 1
171 static const char *data_dir;
172 const char *bios_name = NULL;
173 enum vga_retrace_method vga_retrace_method = VGA_RETRACE_DUMB;
174 DisplayType display_type = DT_DEFAULT;
175 const char* keyboard_layout = NULL;
176 ram_addr_t ram_size;
177 const char *mem_path = NULL;
178 #ifdef MAP_POPULATE
179 int mem_prealloc = 0; /* force preallocation of physical target memory */
180 #endif
181 int nb_nics;
182 NICInfo nd_table[MAX_NICS];
183 int vm_running;
184 int autostart;
185 static int rtc_utc = 1;
186 static int rtc_date_offset = -1; /* -1 means no change */
187 QEMUClock *rtc_clock;
188 int vga_interface_type = VGA_NONE;
189 static int full_screen = 0;
190 #ifdef CONFIG_SDL
191 static int no_frame = 0;
192 #endif
193 int no_quit = 0;
194 CharDriverState *serial_hds[MAX_SERIAL_PORTS];
195 CharDriverState *parallel_hds[MAX_PARALLEL_PORTS];
196 CharDriverState *virtcon_hds[MAX_VIRTIO_CONSOLES];
197 int win2k_install_hack = 0;
198 int rtc_td_hack = 0;
199 int usb_enabled = 0;
200 int singlestep = 0;
201 int smp_cpus = 1;
202 int max_cpus = 0;
203 int smp_cores = 1;
204 int smp_threads = 1;
205 const char *vnc_display;
206 int acpi_enabled = 1;
207 int no_hpet = 0;
208 int fd_bootchk = 1;
209 int no_reboot = 0;
210 int no_shutdown = 0;
211 int cursor_hide = 1;
212 int graphic_rotate = 0;
213 uint8_t irq0override = 1;
214 const char *watchdog;
215 const char *option_rom[MAX_OPTION_ROMS];
216 int nb_option_roms;
217 int semihosting_enabled = 0;
218 int old_param = 0;
219 const char *qemu_name;
220 int alt_grab = 0;
221 int ctrl_grab = 0;
222 unsigned int nb_prom_envs = 0;
223 const char *prom_envs[MAX_PROM_ENVS];
224 int boot_menu;
226 int nb_numa_nodes;
227 uint64_t node_mem[MAX_NODES];
228 uint64_t node_cpumask[MAX_NODES];
230 static QEMUTimer *nographic_timer;
232 uint8_t qemu_uuid[16];
234 static QEMUBootSetHandler *boot_set_handler;
235 static void *boot_set_opaque;
237 int kvm_allowed = 0;
238 uint32_t xen_domid;
239 enum xen_mode xen_mode = XEN_EMULATE;
241 static int default_serial = 1;
242 static int default_parallel = 1;
243 static int default_virtcon = 1;
244 static int default_monitor = 1;
245 static int default_vga = 1;
246 static int default_floppy = 1;
247 static int default_cdrom = 1;
248 static int default_sdcard = 1;
250 static struct {
251 const char *driver;
252 int *flag;
253 } default_list[] = {
254 { .driver = "isa-serial", .flag = &default_serial },
255 { .driver = "isa-parallel", .flag = &default_parallel },
256 { .driver = "isa-fdc", .flag = &default_floppy },
257 { .driver = "ide-drive", .flag = &default_cdrom },
258 { .driver = "virtio-serial-pci", .flag = &default_virtcon },
259 { .driver = "virtio-serial-s390", .flag = &default_virtcon },
260 { .driver = "virtio-serial", .flag = &default_virtcon },
261 { .driver = "VGA", .flag = &default_vga },
262 { .driver = "cirrus-vga", .flag = &default_vga },
263 { .driver = "vmware-svga", .flag = &default_vga },
266 static int default_driver_check(QemuOpts *opts, void *opaque)
268 const char *driver = qemu_opt_get(opts, "driver");
269 int i;
271 if (!driver)
272 return 0;
273 for (i = 0; i < ARRAY_SIZE(default_list); i++) {
274 if (strcmp(default_list[i].driver, driver) != 0)
275 continue;
276 *(default_list[i].flag) = 0;
278 return 0;
281 /***********************************************************/
282 /* real time host monotonic timer */
284 /* compute with 96 bit intermediate result: (a*b)/c */
285 uint64_t muldiv64(uint64_t a, uint32_t b, uint32_t c)
287 union {
288 uint64_t ll;
289 struct {
290 #ifdef HOST_WORDS_BIGENDIAN
291 uint32_t high, low;
292 #else
293 uint32_t low, high;
294 #endif
295 } l;
296 } u, res;
297 uint64_t rl, rh;
299 u.ll = a;
300 rl = (uint64_t)u.l.low * (uint64_t)b;
301 rh = (uint64_t)u.l.high * (uint64_t)b;
302 rh += (rl >> 32);
303 res.l.high = rh / c;
304 res.l.low = (((rh % c) << 32) + (rl & 0xffffffff)) / c;
305 return res.ll;
308 /***********************************************************/
309 /* host time/date access */
310 void qemu_get_timedate(struct tm *tm, int offset)
312 time_t ti;
313 struct tm *ret;
315 time(&ti);
316 ti += offset;
317 if (rtc_date_offset == -1) {
318 if (rtc_utc)
319 ret = gmtime(&ti);
320 else
321 ret = localtime(&ti);
322 } else {
323 ti -= rtc_date_offset;
324 ret = gmtime(&ti);
327 memcpy(tm, ret, sizeof(struct tm));
330 int qemu_timedate_diff(struct tm *tm)
332 time_t seconds;
334 if (rtc_date_offset == -1)
335 if (rtc_utc)
336 seconds = mktimegm(tm);
337 else
338 seconds = mktime(tm);
339 else
340 seconds = mktimegm(tm) + rtc_date_offset;
342 return seconds - time(NULL);
345 void rtc_change_mon_event(struct tm *tm)
347 QObject *data;
349 data = qobject_from_jsonf("{ 'offset': %d }", qemu_timedate_diff(tm));
350 monitor_protocol_event(QEVENT_RTC_CHANGE, data);
351 qobject_decref(data);
354 static void configure_rtc_date_offset(const char *startdate, int legacy)
356 time_t rtc_start_date;
357 struct tm tm;
359 if (!strcmp(startdate, "now") && legacy) {
360 rtc_date_offset = -1;
361 } else {
362 if (sscanf(startdate, "%d-%d-%dT%d:%d:%d",
363 &tm.tm_year,
364 &tm.tm_mon,
365 &tm.tm_mday,
366 &tm.tm_hour,
367 &tm.tm_min,
368 &tm.tm_sec) == 6) {
369 /* OK */
370 } else if (sscanf(startdate, "%d-%d-%d",
371 &tm.tm_year,
372 &tm.tm_mon,
373 &tm.tm_mday) == 3) {
374 tm.tm_hour = 0;
375 tm.tm_min = 0;
376 tm.tm_sec = 0;
377 } else {
378 goto date_fail;
380 tm.tm_year -= 1900;
381 tm.tm_mon--;
382 rtc_start_date = mktimegm(&tm);
383 if (rtc_start_date == -1) {
384 date_fail:
385 fprintf(stderr, "Invalid date format. Valid formats are:\n"
386 "'2006-06-17T16:01:21' or '2006-06-17'\n");
387 exit(1);
389 rtc_date_offset = time(NULL) - rtc_start_date;
393 static void configure_rtc(QemuOpts *opts)
395 const char *value;
397 value = qemu_opt_get(opts, "base");
398 if (value) {
399 if (!strcmp(value, "utc")) {
400 rtc_utc = 1;
401 } else if (!strcmp(value, "localtime")) {
402 rtc_utc = 0;
403 } else {
404 configure_rtc_date_offset(value, 0);
407 value = qemu_opt_get(opts, "clock");
408 if (value) {
409 if (!strcmp(value, "host")) {
410 rtc_clock = host_clock;
411 } else if (!strcmp(value, "vm")) {
412 rtc_clock = vm_clock;
413 } else {
414 fprintf(stderr, "qemu: invalid option value '%s'\n", value);
415 exit(1);
418 value = qemu_opt_get(opts, "driftfix");
419 if (value) {
420 if (!strcmp(value, "slew")) {
421 rtc_td_hack = 1;
422 } else if (!strcmp(value, "none")) {
423 rtc_td_hack = 0;
424 } else {
425 fprintf(stderr, "qemu: invalid option value '%s'\n", value);
426 exit(1);
431 /***********************************************************/
432 /* Bluetooth support */
433 static int nb_hcis;
434 static int cur_hci;
435 static struct HCIInfo *hci_table[MAX_NICS];
437 static struct bt_vlan_s {
438 struct bt_scatternet_s net;
439 int id;
440 struct bt_vlan_s *next;
441 } *first_bt_vlan;
443 /* find or alloc a new bluetooth "VLAN" */
444 static struct bt_scatternet_s *qemu_find_bt_vlan(int id)
446 struct bt_vlan_s **pvlan, *vlan;
447 for (vlan = first_bt_vlan; vlan != NULL; vlan = vlan->next) {
448 if (vlan->id == id)
449 return &vlan->net;
451 vlan = qemu_mallocz(sizeof(struct bt_vlan_s));
452 vlan->id = id;
453 pvlan = &first_bt_vlan;
454 while (*pvlan != NULL)
455 pvlan = &(*pvlan)->next;
456 *pvlan = vlan;
457 return &vlan->net;
460 static void null_hci_send(struct HCIInfo *hci, const uint8_t *data, int len)
464 static int null_hci_addr_set(struct HCIInfo *hci, const uint8_t *bd_addr)
466 return -ENOTSUP;
469 static struct HCIInfo null_hci = {
470 .cmd_send = null_hci_send,
471 .sco_send = null_hci_send,
472 .acl_send = null_hci_send,
473 .bdaddr_set = null_hci_addr_set,
476 struct HCIInfo *qemu_next_hci(void)
478 if (cur_hci == nb_hcis)
479 return &null_hci;
481 return hci_table[cur_hci++];
484 static struct HCIInfo *hci_init(const char *str)
486 char *endp;
487 struct bt_scatternet_s *vlan = 0;
489 if (!strcmp(str, "null"))
490 /* null */
491 return &null_hci;
492 else if (!strncmp(str, "host", 4) && (str[4] == '\0' || str[4] == ':'))
493 /* host[:hciN] */
494 return bt_host_hci(str[4] ? str + 5 : "hci0");
495 else if (!strncmp(str, "hci", 3)) {
496 /* hci[,vlan=n] */
497 if (str[3]) {
498 if (!strncmp(str + 3, ",vlan=", 6)) {
499 vlan = qemu_find_bt_vlan(strtol(str + 9, &endp, 0));
500 if (*endp)
501 vlan = 0;
503 } else
504 vlan = qemu_find_bt_vlan(0);
505 if (vlan)
506 return bt_new_hci(vlan);
509 fprintf(stderr, "qemu: Unknown bluetooth HCI `%s'.\n", str);
511 return 0;
514 static int bt_hci_parse(const char *str)
516 struct HCIInfo *hci;
517 bdaddr_t bdaddr;
519 if (nb_hcis >= MAX_NICS) {
520 fprintf(stderr, "qemu: Too many bluetooth HCIs (max %i).\n", MAX_NICS);
521 return -1;
524 hci = hci_init(str);
525 if (!hci)
526 return -1;
528 bdaddr.b[0] = 0x52;
529 bdaddr.b[1] = 0x54;
530 bdaddr.b[2] = 0x00;
531 bdaddr.b[3] = 0x12;
532 bdaddr.b[4] = 0x34;
533 bdaddr.b[5] = 0x56 + nb_hcis;
534 hci->bdaddr_set(hci, bdaddr.b);
536 hci_table[nb_hcis++] = hci;
538 return 0;
541 static void bt_vhci_add(int vlan_id)
543 struct bt_scatternet_s *vlan = qemu_find_bt_vlan(vlan_id);
545 if (!vlan->slave)
546 fprintf(stderr, "qemu: warning: adding a VHCI to "
547 "an empty scatternet %i\n", vlan_id);
549 bt_vhci_init(bt_new_hci(vlan));
552 static struct bt_device_s *bt_device_add(const char *opt)
554 struct bt_scatternet_s *vlan;
555 int vlan_id = 0;
556 char *endp = strstr(opt, ",vlan=");
557 int len = (endp ? endp - opt : strlen(opt)) + 1;
558 char devname[10];
560 pstrcpy(devname, MIN(sizeof(devname), len), opt);
562 if (endp) {
563 vlan_id = strtol(endp + 6, &endp, 0);
564 if (*endp) {
565 fprintf(stderr, "qemu: unrecognised bluetooth vlan Id\n");
566 return 0;
570 vlan = qemu_find_bt_vlan(vlan_id);
572 if (!vlan->slave)
573 fprintf(stderr, "qemu: warning: adding a slave device to "
574 "an empty scatternet %i\n", vlan_id);
576 if (!strcmp(devname, "keyboard"))
577 return bt_keyboard_init(vlan);
579 fprintf(stderr, "qemu: unsupported bluetooth device `%s'\n", devname);
580 return 0;
583 static int bt_parse(const char *opt)
585 const char *endp, *p;
586 int vlan;
588 if (strstart(opt, "hci", &endp)) {
589 if (!*endp || *endp == ',') {
590 if (*endp)
591 if (!strstart(endp, ",vlan=", 0))
592 opt = endp + 1;
594 return bt_hci_parse(opt);
596 } else if (strstart(opt, "vhci", &endp)) {
597 if (!*endp || *endp == ',') {
598 if (*endp) {
599 if (strstart(endp, ",vlan=", &p)) {
600 vlan = strtol(p, (char **) &endp, 0);
601 if (*endp) {
602 fprintf(stderr, "qemu: bad scatternet '%s'\n", p);
603 return 1;
605 } else {
606 fprintf(stderr, "qemu: bad parameter '%s'\n", endp + 1);
607 return 1;
609 } else
610 vlan = 0;
612 bt_vhci_add(vlan);
613 return 0;
615 } else if (strstart(opt, "device:", &endp))
616 return !bt_device_add(endp);
618 fprintf(stderr, "qemu: bad bluetooth parameter '%s'\n", opt);
619 return 1;
622 /***********************************************************/
623 /* QEMU Block devices */
625 #define HD_ALIAS "index=%d,media=disk"
626 #define CDROM_ALIAS "index=2,media=cdrom"
627 #define FD_ALIAS "index=%d,if=floppy"
628 #define PFLASH_ALIAS "if=pflash"
629 #define MTD_ALIAS "if=mtd"
630 #define SD_ALIAS "index=0,if=sd"
632 static int drive_init_func(QemuOpts *opts, void *opaque)
634 int *use_scsi = opaque;
635 int fatal_error = 0;
637 if (drive_init(opts, *use_scsi, &fatal_error) == NULL) {
638 if (fatal_error)
639 return 1;
641 return 0;
644 static int drive_enable_snapshot(QemuOpts *opts, void *opaque)
646 if (NULL == qemu_opt_get(opts, "snapshot")) {
647 qemu_opt_set(opts, "snapshot", "on");
649 return 0;
652 void qemu_register_boot_set(QEMUBootSetHandler *func, void *opaque)
654 boot_set_handler = func;
655 boot_set_opaque = opaque;
658 int qemu_boot_set(const char *boot_devices)
660 if (!boot_set_handler) {
661 return -EINVAL;
663 return boot_set_handler(boot_set_opaque, boot_devices);
666 static void validate_bootdevices(char *devices)
668 /* We just do some generic consistency checks */
669 const char *p;
670 int bitmap = 0;
672 for (p = devices; *p != '\0'; p++) {
673 /* Allowed boot devices are:
674 * a-b: floppy disk drives
675 * c-f: IDE disk drives
676 * g-m: machine implementation dependant drives
677 * n-p: network devices
678 * It's up to each machine implementation to check if the given boot
679 * devices match the actual hardware implementation and firmware
680 * features.
682 if (*p < 'a' || *p > 'p') {
683 fprintf(stderr, "Invalid boot device '%c'\n", *p);
684 exit(1);
686 if (bitmap & (1 << (*p - 'a'))) {
687 fprintf(stderr, "Boot device '%c' was given twice\n", *p);
688 exit(1);
690 bitmap |= 1 << (*p - 'a');
694 static void restore_boot_devices(void *opaque)
696 char *standard_boot_devices = opaque;
697 static int first = 1;
699 /* Restore boot order and remove ourselves after the first boot */
700 if (first) {
701 first = 0;
702 return;
705 qemu_boot_set(standard_boot_devices);
707 qemu_unregister_reset(restore_boot_devices, standard_boot_devices);
708 qemu_free(standard_boot_devices);
711 static void numa_add(const char *optarg)
713 char option[128];
714 char *endptr;
715 unsigned long long value, endvalue;
716 int nodenr;
718 optarg = get_opt_name(option, 128, optarg, ',') + 1;
719 if (!strcmp(option, "node")) {
720 if (get_param_value(option, 128, "nodeid", optarg) == 0) {
721 nodenr = nb_numa_nodes;
722 } else {
723 nodenr = strtoull(option, NULL, 10);
726 if (get_param_value(option, 128, "mem", optarg) == 0) {
727 node_mem[nodenr] = 0;
728 } else {
729 value = strtoull(option, &endptr, 0);
730 switch (*endptr) {
731 case 0: case 'M': case 'm':
732 value <<= 20;
733 break;
734 case 'G': case 'g':
735 value <<= 30;
736 break;
738 node_mem[nodenr] = value;
740 if (get_param_value(option, 128, "cpus", optarg) == 0) {
741 node_cpumask[nodenr] = 0;
742 } else {
743 value = strtoull(option, &endptr, 10);
744 if (value >= 64) {
745 value = 63;
746 fprintf(stderr, "only 64 CPUs in NUMA mode supported.\n");
747 } else {
748 if (*endptr == '-') {
749 endvalue = strtoull(endptr+1, &endptr, 10);
750 if (endvalue >= 63) {
751 endvalue = 62;
752 fprintf(stderr,
753 "only 63 CPUs in NUMA mode supported.\n");
755 value = (2ULL << endvalue) - (1ULL << value);
756 } else {
757 value = 1ULL << value;
760 node_cpumask[nodenr] = value;
762 nb_numa_nodes++;
764 return;
767 static void smp_parse(const char *optarg)
769 int smp, sockets = 0, threads = 0, cores = 0;
770 char *endptr;
771 char option[128];
773 smp = strtoul(optarg, &endptr, 10);
774 if (endptr != optarg) {
775 if (*endptr == ',') {
776 endptr++;
779 if (get_param_value(option, 128, "sockets", endptr) != 0)
780 sockets = strtoull(option, NULL, 10);
781 if (get_param_value(option, 128, "cores", endptr) != 0)
782 cores = strtoull(option, NULL, 10);
783 if (get_param_value(option, 128, "threads", endptr) != 0)
784 threads = strtoull(option, NULL, 10);
785 if (get_param_value(option, 128, "maxcpus", endptr) != 0)
786 max_cpus = strtoull(option, NULL, 10);
788 /* compute missing values, prefer sockets over cores over threads */
789 if (smp == 0 || sockets == 0) {
790 sockets = sockets > 0 ? sockets : 1;
791 cores = cores > 0 ? cores : 1;
792 threads = threads > 0 ? threads : 1;
793 if (smp == 0) {
794 smp = cores * threads * sockets;
796 } else {
797 if (cores == 0) {
798 threads = threads > 0 ? threads : 1;
799 cores = smp / (sockets * threads);
800 } else {
801 if (sockets) {
802 threads = smp / (cores * sockets);
806 smp_cpus = smp;
807 smp_cores = cores > 0 ? cores : 1;
808 smp_threads = threads > 0 ? threads : 1;
809 if (max_cpus == 0)
810 max_cpus = smp_cpus;
813 /***********************************************************/
814 /* USB devices */
816 static int usb_device_add(const char *devname)
818 const char *p;
819 USBDevice *dev = NULL;
821 if (!usb_enabled)
822 return -1;
824 /* drivers with .usbdevice_name entry in USBDeviceInfo */
825 dev = usbdevice_create(devname);
826 if (dev)
827 goto done;
829 /* the other ones */
830 if (strstart(devname, "host:", &p)) {
831 dev = usb_host_device_open(p);
832 } else if (!strcmp(devname, "bt") || strstart(devname, "bt:", &p)) {
833 dev = usb_bt_init(devname[2] ? hci_init(p) :
834 bt_new_hci(qemu_find_bt_vlan(0)));
835 } else {
836 return -1;
838 if (!dev)
839 return -1;
841 done:
842 return 0;
845 static int usb_device_del(const char *devname)
847 int bus_num, addr;
848 const char *p;
850 if (strstart(devname, "host:", &p))
851 return usb_host_device_close(p);
853 if (!usb_enabled)
854 return -1;
856 p = strchr(devname, '.');
857 if (!p)
858 return -1;
859 bus_num = strtoul(devname, NULL, 0);
860 addr = strtoul(p + 1, NULL, 0);
862 return usb_device_delete_addr(bus_num, addr);
865 static int usb_parse(const char *cmdline)
867 int r;
868 r = usb_device_add(cmdline);
869 if (r < 0) {
870 fprintf(stderr, "qemu: could not add USB device '%s'\n", cmdline);
872 return r;
875 void do_usb_add(Monitor *mon, const QDict *qdict)
877 const char *devname = qdict_get_str(qdict, "devname");
878 if (usb_device_add(devname) < 0) {
879 error_report("could not add USB device '%s'", devname);
883 void do_usb_del(Monitor *mon, const QDict *qdict)
885 const char *devname = qdict_get_str(qdict, "devname");
886 if (usb_device_del(devname) < 0) {
887 error_report("could not delete USB device '%s'", devname);
891 /***********************************************************/
892 /* PCMCIA/Cardbus */
894 static struct pcmcia_socket_entry_s {
895 PCMCIASocket *socket;
896 struct pcmcia_socket_entry_s *next;
897 } *pcmcia_sockets = 0;
899 void pcmcia_socket_register(PCMCIASocket *socket)
901 struct pcmcia_socket_entry_s *entry;
903 entry = qemu_malloc(sizeof(struct pcmcia_socket_entry_s));
904 entry->socket = socket;
905 entry->next = pcmcia_sockets;
906 pcmcia_sockets = entry;
909 void pcmcia_socket_unregister(PCMCIASocket *socket)
911 struct pcmcia_socket_entry_s *entry, **ptr;
913 ptr = &pcmcia_sockets;
914 for (entry = *ptr; entry; ptr = &entry->next, entry = *ptr)
915 if (entry->socket == socket) {
916 *ptr = entry->next;
917 qemu_free(entry);
921 void pcmcia_info(Monitor *mon)
923 struct pcmcia_socket_entry_s *iter;
925 if (!pcmcia_sockets)
926 monitor_printf(mon, "No PCMCIA sockets\n");
928 for (iter = pcmcia_sockets; iter; iter = iter->next)
929 monitor_printf(mon, "%s: %s\n", iter->socket->slot_string,
930 iter->socket->attached ? iter->socket->card_string :
931 "Empty");
934 /***********************************************************/
935 /* I/O handling */
937 typedef struct IOHandlerRecord {
938 int fd;
939 IOCanReadHandler *fd_read_poll;
940 IOHandler *fd_read;
941 IOHandler *fd_write;
942 int deleted;
943 void *opaque;
944 /* temporary data */
945 struct pollfd *ufd;
946 QLIST_ENTRY(IOHandlerRecord) next;
947 } IOHandlerRecord;
949 static QLIST_HEAD(, IOHandlerRecord) io_handlers =
950 QLIST_HEAD_INITIALIZER(io_handlers);
953 /* XXX: fd_read_poll should be suppressed, but an API change is
954 necessary in the character devices to suppress fd_can_read(). */
955 int qemu_set_fd_handler2(int fd,
956 IOCanReadHandler *fd_read_poll,
957 IOHandler *fd_read,
958 IOHandler *fd_write,
959 void *opaque)
961 IOHandlerRecord *ioh;
963 if (!fd_read && !fd_write) {
964 QLIST_FOREACH(ioh, &io_handlers, next) {
965 if (ioh->fd == fd) {
966 ioh->deleted = 1;
967 break;
970 } else {
971 QLIST_FOREACH(ioh, &io_handlers, next) {
972 if (ioh->fd == fd)
973 goto found;
975 ioh = qemu_mallocz(sizeof(IOHandlerRecord));
976 QLIST_INSERT_HEAD(&io_handlers, ioh, next);
977 found:
978 ioh->fd = fd;
979 ioh->fd_read_poll = fd_read_poll;
980 ioh->fd_read = fd_read;
981 ioh->fd_write = fd_write;
982 ioh->opaque = opaque;
983 ioh->deleted = 0;
985 return 0;
988 int qemu_set_fd_handler(int fd,
989 IOHandler *fd_read,
990 IOHandler *fd_write,
991 void *opaque)
993 return qemu_set_fd_handler2(fd, NULL, fd_read, fd_write, opaque);
996 /***********************************************************/
997 /* machine registration */
999 static QEMUMachine *first_machine = NULL;
1000 QEMUMachine *current_machine = NULL;
1002 int qemu_register_machine(QEMUMachine *m)
1004 QEMUMachine **pm;
1005 pm = &first_machine;
1006 while (*pm != NULL)
1007 pm = &(*pm)->next;
1008 m->next = NULL;
1009 *pm = m;
1010 return 0;
1013 static QEMUMachine *find_machine(const char *name)
1015 QEMUMachine *m;
1017 for(m = first_machine; m != NULL; m = m->next) {
1018 if (!strcmp(m->name, name))
1019 return m;
1020 if (m->alias && !strcmp(m->alias, name))
1021 return m;
1023 return NULL;
1026 static QEMUMachine *find_default_machine(void)
1028 QEMUMachine *m;
1030 for(m = first_machine; m != NULL; m = m->next) {
1031 if (m->is_default) {
1032 return m;
1035 return NULL;
1038 /***********************************************************/
1039 /* main execution loop */
1041 static void gui_update(void *opaque)
1043 uint64_t interval = GUI_REFRESH_INTERVAL;
1044 DisplayState *ds = opaque;
1045 DisplayChangeListener *dcl = ds->listeners;
1047 qemu_flush_coalesced_mmio_buffer();
1048 dpy_refresh(ds);
1050 while (dcl != NULL) {
1051 if (dcl->gui_timer_interval &&
1052 dcl->gui_timer_interval < interval)
1053 interval = dcl->gui_timer_interval;
1054 dcl = dcl->next;
1056 qemu_mod_timer(ds->gui_timer, interval + qemu_get_clock(rt_clock));
1059 static void nographic_update(void *opaque)
1061 uint64_t interval = GUI_REFRESH_INTERVAL;
1063 qemu_flush_coalesced_mmio_buffer();
1064 qemu_mod_timer(nographic_timer, interval + qemu_get_clock(rt_clock));
1067 struct vm_change_state_entry {
1068 VMChangeStateHandler *cb;
1069 void *opaque;
1070 QLIST_ENTRY (vm_change_state_entry) entries;
1073 static QLIST_HEAD(vm_change_state_head, vm_change_state_entry) vm_change_state_head;
1075 VMChangeStateEntry *qemu_add_vm_change_state_handler(VMChangeStateHandler *cb,
1076 void *opaque)
1078 VMChangeStateEntry *e;
1080 e = qemu_mallocz(sizeof (*e));
1082 e->cb = cb;
1083 e->opaque = opaque;
1084 QLIST_INSERT_HEAD(&vm_change_state_head, e, entries);
1085 return e;
1088 void qemu_del_vm_change_state_handler(VMChangeStateEntry *e)
1090 QLIST_REMOVE (e, entries);
1091 qemu_free (e);
1094 void vm_state_notify(int running, int reason)
1096 VMChangeStateEntry *e;
1098 for (e = vm_change_state_head.lh_first; e; e = e->entries.le_next) {
1099 e->cb(e->opaque, running, reason);
1103 void vm_start(void)
1105 if (!vm_running) {
1106 cpu_enable_ticks();
1107 vm_running = 1;
1108 vm_state_notify(1, 0);
1109 resume_all_vcpus();
1110 monitor_protocol_event(QEVENT_RESUME, NULL);
1114 /* reset/shutdown handler */
1116 typedef struct QEMUResetEntry {
1117 QTAILQ_ENTRY(QEMUResetEntry) entry;
1118 QEMUResetHandler *func;
1119 void *opaque;
1120 } QEMUResetEntry;
1122 static QTAILQ_HEAD(reset_handlers, QEMUResetEntry) reset_handlers =
1123 QTAILQ_HEAD_INITIALIZER(reset_handlers);
1124 static int reset_requested;
1125 static int shutdown_requested;
1126 static int powerdown_requested;
1127 int debug_requested;
1128 int vmstop_requested;
1130 int qemu_shutdown_requested(void)
1132 int r = shutdown_requested;
1133 shutdown_requested = 0;
1134 return r;
1137 int qemu_reset_requested(void)
1139 int r = reset_requested;
1140 reset_requested = 0;
1141 return r;
1144 int qemu_powerdown_requested(void)
1146 int r = powerdown_requested;
1147 powerdown_requested = 0;
1148 return r;
1151 static int qemu_debug_requested(void)
1153 int r = debug_requested;
1154 debug_requested = 0;
1155 return r;
1158 static int qemu_vmstop_requested(void)
1160 int r = vmstop_requested;
1161 vmstop_requested = 0;
1162 return r;
1165 void qemu_register_reset(QEMUResetHandler *func, void *opaque)
1167 QEMUResetEntry *re = qemu_mallocz(sizeof(QEMUResetEntry));
1169 re->func = func;
1170 re->opaque = opaque;
1171 QTAILQ_INSERT_TAIL(&reset_handlers, re, entry);
1174 void qemu_unregister_reset(QEMUResetHandler *func, void *opaque)
1176 QEMUResetEntry *re;
1178 QTAILQ_FOREACH(re, &reset_handlers, entry) {
1179 if (re->func == func && re->opaque == opaque) {
1180 QTAILQ_REMOVE(&reset_handlers, re, entry);
1181 qemu_free(re);
1182 return;
1187 void qemu_system_reset(void)
1189 QEMUResetEntry *re, *nre;
1191 /* reset all devices */
1192 QTAILQ_FOREACH_SAFE(re, &reset_handlers, entry, nre) {
1193 re->func(re->opaque);
1195 monitor_protocol_event(QEVENT_RESET, NULL);
1196 cpu_synchronize_all_post_reset();
1199 void qemu_system_reset_request(void)
1201 if (no_reboot) {
1202 shutdown_requested = 1;
1203 } else {
1204 reset_requested = 1;
1206 qemu_notify_event();
1209 void qemu_system_shutdown_request(void)
1211 shutdown_requested = 1;
1212 qemu_notify_event();
1215 void qemu_system_powerdown_request(void)
1217 powerdown_requested = 1;
1218 qemu_notify_event();
1221 void main_loop_wait(int nonblocking)
1223 IOHandlerRecord *ioh;
1224 fd_set rfds, wfds, xfds;
1225 int ret, nfds;
1226 struct timeval tv;
1227 int timeout;
1229 if (nonblocking)
1230 timeout = 0;
1231 else {
1232 timeout = qemu_calculate_timeout();
1233 qemu_bh_update_timeout(&timeout);
1236 os_host_main_loop_wait(&timeout);
1238 /* poll any events */
1239 /* XXX: separate device handlers from system ones */
1240 nfds = -1;
1241 FD_ZERO(&rfds);
1242 FD_ZERO(&wfds);
1243 FD_ZERO(&xfds);
1244 QLIST_FOREACH(ioh, &io_handlers, next) {
1245 if (ioh->deleted)
1246 continue;
1247 if (ioh->fd_read &&
1248 (!ioh->fd_read_poll ||
1249 ioh->fd_read_poll(ioh->opaque) != 0)) {
1250 FD_SET(ioh->fd, &rfds);
1251 if (ioh->fd > nfds)
1252 nfds = ioh->fd;
1254 if (ioh->fd_write) {
1255 FD_SET(ioh->fd, &wfds);
1256 if (ioh->fd > nfds)
1257 nfds = ioh->fd;
1261 tv.tv_sec = timeout / 1000;
1262 tv.tv_usec = (timeout % 1000) * 1000;
1264 slirp_select_fill(&nfds, &rfds, &wfds, &xfds);
1266 qemu_mutex_unlock_iothread();
1267 ret = select(nfds + 1, &rfds, &wfds, &xfds, &tv);
1268 qemu_mutex_lock_iothread();
1269 if (ret > 0) {
1270 IOHandlerRecord *pioh;
1272 QLIST_FOREACH_SAFE(ioh, &io_handlers, next, pioh) {
1273 if (ioh->deleted) {
1274 QLIST_REMOVE(ioh, next);
1275 qemu_free(ioh);
1276 continue;
1278 if (ioh->fd_read && FD_ISSET(ioh->fd, &rfds)) {
1279 ioh->fd_read(ioh->opaque);
1281 if (ioh->fd_write && FD_ISSET(ioh->fd, &wfds)) {
1282 ioh->fd_write(ioh->opaque);
1287 slirp_select_poll(&rfds, &wfds, &xfds, (ret < 0));
1289 qemu_run_all_timers();
1291 /* Check bottom-halves last in case any of the earlier events triggered
1292 them. */
1293 qemu_bh_poll();
1297 static int vm_can_run(void)
1299 if (powerdown_requested)
1300 return 0;
1301 if (reset_requested)
1302 return 0;
1303 if (shutdown_requested)
1304 return 0;
1305 if (debug_requested)
1306 return 0;
1307 return 1;
1310 qemu_irq qemu_system_powerdown;
1312 static void main_loop(void)
1314 int r;
1316 qemu_main_loop_start();
1318 for (;;) {
1319 do {
1320 bool nonblocking = false;
1321 #ifdef CONFIG_PROFILER
1322 int64_t ti;
1323 #endif
1324 #ifndef CONFIG_IOTHREAD
1325 nonblocking = tcg_cpu_exec();
1326 #endif
1327 #ifdef CONFIG_PROFILER
1328 ti = profile_getclock();
1329 #endif
1330 main_loop_wait(nonblocking);
1331 #ifdef CONFIG_PROFILER
1332 dev_time += profile_getclock() - ti;
1333 #endif
1334 } while (vm_can_run());
1336 if ((r = qemu_debug_requested())) {
1337 vm_stop(r);
1339 if (qemu_shutdown_requested()) {
1340 monitor_protocol_event(QEVENT_SHUTDOWN, NULL);
1341 if (no_shutdown) {
1342 vm_stop(0);
1343 no_shutdown = 0;
1344 } else
1345 break;
1347 if (qemu_reset_requested()) {
1348 pause_all_vcpus();
1349 qemu_system_reset();
1350 resume_all_vcpus();
1352 if (qemu_powerdown_requested()) {
1353 monitor_protocol_event(QEVENT_POWERDOWN, NULL);
1354 qemu_irq_raise(qemu_system_powerdown);
1356 if ((r = qemu_vmstop_requested())) {
1357 vm_stop(r);
1360 bdrv_close_all();
1361 pause_all_vcpus();
1364 static void version(void)
1366 printf("QEMU emulator version " QEMU_VERSION QEMU_PKGVERSION ", Copyright (c) 2003-2008 Fabrice Bellard\n");
1369 static void help(int exitcode)
1371 const char *options_help =
1372 #define DEF(option, opt_arg, opt_enum, opt_help, arch_mask) \
1373 opt_help
1374 #define DEFHEADING(text) stringify(text) "\n"
1375 #include "qemu-options.def"
1376 #undef DEF
1377 #undef DEFHEADING
1378 #undef GEN_DOCS
1380 version();
1381 printf("usage: %s [options] [disk_image]\n"
1382 "\n"
1383 "'disk_image' is a raw hard disk image for IDE hard disk 0\n"
1384 "\n"
1385 "%s\n"
1386 "During emulation, the following keys are useful:\n"
1387 "ctrl-alt-f toggle full screen\n"
1388 "ctrl-alt-n switch to virtual console 'n'\n"
1389 "ctrl-alt toggle mouse and keyboard grab\n"
1390 "\n"
1391 "When using -nographic, press 'ctrl-a h' to get some help.\n",
1392 "qemu",
1393 options_help);
1394 exit(exitcode);
1397 #define HAS_ARG 0x0001
1399 typedef struct QEMUOption {
1400 const char *name;
1401 int flags;
1402 int index;
1403 uint32_t arch_mask;
1404 } QEMUOption;
1406 static const QEMUOption qemu_options[] = {
1407 { "h", 0, QEMU_OPTION_h, QEMU_ARCH_ALL },
1408 #define DEF(option, opt_arg, opt_enum, opt_help, arch_mask) \
1409 { option, opt_arg, opt_enum, arch_mask },
1410 #define DEFHEADING(text)
1411 #include "qemu-options.def"
1412 #undef DEF
1413 #undef DEFHEADING
1414 #undef GEN_DOCS
1415 { NULL },
1417 static void select_vgahw (const char *p)
1419 const char *opts;
1421 default_vga = 0;
1422 vga_interface_type = VGA_NONE;
1423 if (strstart(p, "std", &opts)) {
1424 vga_interface_type = VGA_STD;
1425 } else if (strstart(p, "cirrus", &opts)) {
1426 vga_interface_type = VGA_CIRRUS;
1427 } else if (strstart(p, "vmware", &opts)) {
1428 vga_interface_type = VGA_VMWARE;
1429 } else if (strstart(p, "xenfb", &opts)) {
1430 vga_interface_type = VGA_XENFB;
1431 } else if (!strstart(p, "none", &opts)) {
1432 invalid_vga:
1433 fprintf(stderr, "Unknown vga type: %s\n", p);
1434 exit(1);
1436 while (*opts) {
1437 const char *nextopt;
1439 if (strstart(opts, ",retrace=", &nextopt)) {
1440 opts = nextopt;
1441 if (strstart(opts, "dumb", &nextopt))
1442 vga_retrace_method = VGA_RETRACE_DUMB;
1443 else if (strstart(opts, "precise", &nextopt))
1444 vga_retrace_method = VGA_RETRACE_PRECISE;
1445 else goto invalid_vga;
1446 } else goto invalid_vga;
1447 opts = nextopt;
1451 static int balloon_parse(const char *arg)
1453 QemuOpts *opts;
1455 if (strcmp(arg, "none") == 0) {
1456 return 0;
1459 if (!strncmp(arg, "virtio", 6)) {
1460 if (arg[6] == ',') {
1461 /* have params -> parse them */
1462 opts = qemu_opts_parse(&qemu_device_opts, arg+7, 0);
1463 if (!opts)
1464 return -1;
1465 } else {
1466 /* create empty opts */
1467 opts = qemu_opts_create(&qemu_device_opts, NULL, 0);
1469 qemu_opt_set(opts, "driver", "virtio-balloon-pci");
1470 return 0;
1473 return -1;
1476 char *qemu_find_file(int type, const char *name)
1478 int len;
1479 const char *subdir;
1480 char *buf;
1482 /* If name contains path separators then try it as a straight path. */
1483 if ((strchr(name, '/') || strchr(name, '\\'))
1484 && access(name, R_OK) == 0) {
1485 return qemu_strdup(name);
1487 switch (type) {
1488 case QEMU_FILE_TYPE_BIOS:
1489 subdir = "";
1490 break;
1491 case QEMU_FILE_TYPE_KEYMAP:
1492 subdir = "keymaps/";
1493 break;
1494 default:
1495 abort();
1497 len = strlen(data_dir) + strlen(name) + strlen(subdir) + 2;
1498 buf = qemu_mallocz(len);
1499 snprintf(buf, len, "%s/%s%s", data_dir, subdir, name);
1500 if (access(buf, R_OK)) {
1501 qemu_free(buf);
1502 return NULL;
1504 return buf;
1507 static int device_help_func(QemuOpts *opts, void *opaque)
1509 return qdev_device_help(opts);
1512 static int device_init_func(QemuOpts *opts, void *opaque)
1514 DeviceState *dev;
1516 dev = qdev_device_add(opts);
1517 if (!dev)
1518 return -1;
1519 return 0;
1522 static int chardev_init_func(QemuOpts *opts, void *opaque)
1524 CharDriverState *chr;
1526 chr = qemu_chr_open_opts(opts, NULL);
1527 if (!chr)
1528 return -1;
1529 return 0;
1532 #ifdef CONFIG_LINUX
1533 static int fsdev_init_func(QemuOpts *opts, void *opaque)
1535 int ret;
1536 ret = qemu_fsdev_add(opts);
1538 return ret;
1540 #endif
1542 static int mon_init_func(QemuOpts *opts, void *opaque)
1544 CharDriverState *chr;
1545 const char *chardev;
1546 const char *mode;
1547 int flags;
1549 mode = qemu_opt_get(opts, "mode");
1550 if (mode == NULL) {
1551 mode = "readline";
1553 if (strcmp(mode, "readline") == 0) {
1554 flags = MONITOR_USE_READLINE;
1555 } else if (strcmp(mode, "control") == 0) {
1556 flags = MONITOR_USE_CONTROL;
1557 } else {
1558 fprintf(stderr, "unknown monitor mode \"%s\"\n", mode);
1559 exit(1);
1562 if (qemu_opt_get_bool(opts, "default", 0))
1563 flags |= MONITOR_IS_DEFAULT;
1565 chardev = qemu_opt_get(opts, "chardev");
1566 chr = qemu_chr_find(chardev);
1567 if (chr == NULL) {
1568 fprintf(stderr, "chardev \"%s\" not found\n", chardev);
1569 exit(1);
1572 monitor_init(chr, flags);
1573 return 0;
1576 static void monitor_parse(const char *optarg, const char *mode)
1578 static int monitor_device_index = 0;
1579 QemuOpts *opts;
1580 const char *p;
1581 char label[32];
1582 int def = 0;
1584 if (strstart(optarg, "chardev:", &p)) {
1585 snprintf(label, sizeof(label), "%s", p);
1586 } else {
1587 snprintf(label, sizeof(label), "compat_monitor%d",
1588 monitor_device_index);
1589 if (monitor_device_index == 0) {
1590 def = 1;
1592 opts = qemu_chr_parse_compat(label, optarg);
1593 if (!opts) {
1594 fprintf(stderr, "parse error: %s\n", optarg);
1595 exit(1);
1599 opts = qemu_opts_create(&qemu_mon_opts, label, 1);
1600 if (!opts) {
1601 fprintf(stderr, "duplicate chardev: %s\n", label);
1602 exit(1);
1604 qemu_opt_set(opts, "mode", mode);
1605 qemu_opt_set(opts, "chardev", label);
1606 if (def)
1607 qemu_opt_set(opts, "default", "on");
1608 monitor_device_index++;
1611 struct device_config {
1612 enum {
1613 DEV_USB, /* -usbdevice */
1614 DEV_BT, /* -bt */
1615 DEV_SERIAL, /* -serial */
1616 DEV_PARALLEL, /* -parallel */
1617 DEV_VIRTCON, /* -virtioconsole */
1618 DEV_DEBUGCON, /* -debugcon */
1619 } type;
1620 const char *cmdline;
1621 QTAILQ_ENTRY(device_config) next;
1623 QTAILQ_HEAD(, device_config) device_configs = QTAILQ_HEAD_INITIALIZER(device_configs);
1625 static void add_device_config(int type, const char *cmdline)
1627 struct device_config *conf;
1629 conf = qemu_mallocz(sizeof(*conf));
1630 conf->type = type;
1631 conf->cmdline = cmdline;
1632 QTAILQ_INSERT_TAIL(&device_configs, conf, next);
1635 static int foreach_device_config(int type, int (*func)(const char *cmdline))
1637 struct device_config *conf;
1638 int rc;
1640 QTAILQ_FOREACH(conf, &device_configs, next) {
1641 if (conf->type != type)
1642 continue;
1643 rc = func(conf->cmdline);
1644 if (0 != rc)
1645 return rc;
1647 return 0;
1650 static int serial_parse(const char *devname)
1652 static int index = 0;
1653 char label[32];
1655 if (strcmp(devname, "none") == 0)
1656 return 0;
1657 if (index == MAX_SERIAL_PORTS) {
1658 fprintf(stderr, "qemu: too many serial ports\n");
1659 exit(1);
1661 snprintf(label, sizeof(label), "serial%d", index);
1662 serial_hds[index] = qemu_chr_open(label, devname, NULL);
1663 if (!serial_hds[index]) {
1664 fprintf(stderr, "qemu: could not open serial device '%s': %s\n",
1665 devname, strerror(errno));
1666 return -1;
1668 index++;
1669 return 0;
1672 static int parallel_parse(const char *devname)
1674 static int index = 0;
1675 char label[32];
1677 if (strcmp(devname, "none") == 0)
1678 return 0;
1679 if (index == MAX_PARALLEL_PORTS) {
1680 fprintf(stderr, "qemu: too many parallel ports\n");
1681 exit(1);
1683 snprintf(label, sizeof(label), "parallel%d", index);
1684 parallel_hds[index] = qemu_chr_open(label, devname, NULL);
1685 if (!parallel_hds[index]) {
1686 fprintf(stderr, "qemu: could not open parallel device '%s': %s\n",
1687 devname, strerror(errno));
1688 return -1;
1690 index++;
1691 return 0;
1694 static int virtcon_parse(const char *devname)
1696 static int index = 0;
1697 char label[32];
1698 QemuOpts *bus_opts, *dev_opts;
1700 if (strcmp(devname, "none") == 0)
1701 return 0;
1702 if (index == MAX_VIRTIO_CONSOLES) {
1703 fprintf(stderr, "qemu: too many virtio consoles\n");
1704 exit(1);
1707 bus_opts = qemu_opts_create(&qemu_device_opts, NULL, 0);
1708 qemu_opt_set(bus_opts, "driver", "virtio-serial");
1710 dev_opts = qemu_opts_create(&qemu_device_opts, NULL, 0);
1711 qemu_opt_set(dev_opts, "driver", "virtconsole");
1713 snprintf(label, sizeof(label), "virtcon%d", index);
1714 virtcon_hds[index] = qemu_chr_open(label, devname, NULL);
1715 if (!virtcon_hds[index]) {
1716 fprintf(stderr, "qemu: could not open virtio console '%s': %s\n",
1717 devname, strerror(errno));
1718 return -1;
1720 qemu_opt_set(dev_opts, "chardev", label);
1722 index++;
1723 return 0;
1726 static int debugcon_parse(const char *devname)
1728 QemuOpts *opts;
1730 if (!qemu_chr_open("debugcon", devname, NULL)) {
1731 exit(1);
1733 opts = qemu_opts_create(&qemu_device_opts, "debugcon", 1);
1734 if (!opts) {
1735 fprintf(stderr, "qemu: already have a debugcon device\n");
1736 exit(1);
1738 qemu_opt_set(opts, "driver", "isa-debugcon");
1739 qemu_opt_set(opts, "chardev", "debugcon");
1740 return 0;
1743 static const QEMUOption *lookup_opt(int argc, char **argv,
1744 const char **poptarg, int *poptind)
1746 const QEMUOption *popt;
1747 int optind = *poptind;
1748 char *r = argv[optind];
1749 const char *optarg;
1751 loc_set_cmdline(argv, optind, 1);
1752 optind++;
1753 /* Treat --foo the same as -foo. */
1754 if (r[1] == '-')
1755 r++;
1756 popt = qemu_options;
1757 for(;;) {
1758 if (!popt->name) {
1759 error_report("invalid option");
1760 exit(1);
1762 if (!strcmp(popt->name, r + 1))
1763 break;
1764 popt++;
1766 if (popt->flags & HAS_ARG) {
1767 if (optind >= argc) {
1768 error_report("requires an argument");
1769 exit(1);
1771 optarg = argv[optind++];
1772 loc_set_cmdline(argv, optind - 2, 2);
1773 } else {
1774 optarg = NULL;
1777 *poptarg = optarg;
1778 *poptind = optind;
1780 return popt;
1783 int main(int argc, char **argv, char **envp)
1785 const char *gdbstub_dev = NULL;
1786 int i;
1787 int snapshot, linux_boot;
1788 const char *icount_option = NULL;
1789 const char *initrd_filename;
1790 const char *kernel_filename, *kernel_cmdline;
1791 char boot_devices[33] = "cad"; /* default to HD->floppy->CD-ROM */
1792 DisplayState *ds;
1793 DisplayChangeListener *dcl;
1794 int cyls, heads, secs, translation;
1795 QemuOpts *hda_opts = NULL, *opts;
1796 int optind;
1797 const char *optarg;
1798 const char *loadvm = NULL;
1799 QEMUMachine *machine;
1800 const char *cpu_model;
1801 int tb_size;
1802 const char *pid_file = NULL;
1803 const char *incoming = NULL;
1804 int show_vnc_port = 0;
1805 int defconfig = 1;
1807 error_set_progname(argv[0]);
1809 init_clocks();
1811 qemu_cache_utils_init(envp);
1813 QLIST_INIT (&vm_change_state_head);
1814 os_setup_early_signal_handling();
1816 module_call_init(MODULE_INIT_MACHINE);
1817 machine = find_default_machine();
1818 cpu_model = NULL;
1819 initrd_filename = NULL;
1820 ram_size = 0;
1821 snapshot = 0;
1822 kernel_filename = NULL;
1823 kernel_cmdline = "";
1824 cyls = heads = secs = 0;
1825 translation = BIOS_ATA_TRANSLATION_AUTO;
1827 for (i = 0; i < MAX_NODES; i++) {
1828 node_mem[i] = 0;
1829 node_cpumask[i] = 0;
1832 nb_numa_nodes = 0;
1833 nb_nics = 0;
1835 tb_size = 0;
1836 autostart= 1;
1838 /* first pass of option parsing */
1839 optind = 1;
1840 while (optind < argc) {
1841 if (argv[optind][0] != '-') {
1842 /* disk image */
1843 optind++;
1844 continue;
1845 } else {
1846 const QEMUOption *popt;
1848 popt = lookup_opt(argc, argv, &optarg, &optind);
1849 switch (popt->index) {
1850 case QEMU_OPTION_nodefconfig:
1851 defconfig=0;
1852 break;
1857 if (defconfig) {
1858 int ret;
1860 ret = qemu_read_config_file(CONFIG_QEMU_CONFDIR "/qemu.conf");
1861 if (ret < 0 && ret != -ENOENT) {
1862 exit(1);
1865 ret = qemu_read_config_file(arch_config_name);
1866 if (ret < 0 && ret != -ENOENT) {
1867 exit(1);
1870 cpudef_init();
1872 /* second pass of option parsing */
1873 optind = 1;
1874 for(;;) {
1875 if (optind >= argc)
1876 break;
1877 if (argv[optind][0] != '-') {
1878 hda_opts = drive_add(argv[optind++], HD_ALIAS, 0);
1879 } else {
1880 const QEMUOption *popt;
1882 popt = lookup_opt(argc, argv, &optarg, &optind);
1883 if (!(popt->arch_mask & arch_type)) {
1884 printf("Option %s not supported for this target\n", popt->name);
1885 exit(1);
1887 switch(popt->index) {
1888 case QEMU_OPTION_M:
1889 machine = find_machine(optarg);
1890 if (!machine) {
1891 QEMUMachine *m;
1892 printf("Supported machines are:\n");
1893 for(m = first_machine; m != NULL; m = m->next) {
1894 if (m->alias)
1895 printf("%-10s %s (alias of %s)\n",
1896 m->alias, m->desc, m->name);
1897 printf("%-10s %s%s\n",
1898 m->name, m->desc,
1899 m->is_default ? " (default)" : "");
1901 exit(*optarg != '?');
1903 break;
1904 case QEMU_OPTION_cpu:
1905 /* hw initialization will check this */
1906 if (*optarg == '?') {
1907 list_cpus(stdout, &fprintf, optarg);
1908 exit(0);
1909 } else {
1910 cpu_model = optarg;
1912 break;
1913 case QEMU_OPTION_initrd:
1914 initrd_filename = optarg;
1915 break;
1916 case QEMU_OPTION_hda:
1917 if (cyls == 0)
1918 hda_opts = drive_add(optarg, HD_ALIAS, 0);
1919 else
1920 hda_opts = drive_add(optarg, HD_ALIAS
1921 ",cyls=%d,heads=%d,secs=%d%s",
1922 0, cyls, heads, secs,
1923 translation == BIOS_ATA_TRANSLATION_LBA ?
1924 ",trans=lba" :
1925 translation == BIOS_ATA_TRANSLATION_NONE ?
1926 ",trans=none" : "");
1927 break;
1928 case QEMU_OPTION_hdb:
1929 case QEMU_OPTION_hdc:
1930 case QEMU_OPTION_hdd:
1931 drive_add(optarg, HD_ALIAS, popt->index - QEMU_OPTION_hda);
1932 break;
1933 case QEMU_OPTION_drive:
1934 drive_add(NULL, "%s", optarg);
1935 break;
1936 case QEMU_OPTION_set:
1937 if (qemu_set_option(optarg) != 0)
1938 exit(1);
1939 break;
1940 case QEMU_OPTION_global:
1941 if (qemu_global_option(optarg) != 0)
1942 exit(1);
1943 break;
1944 case QEMU_OPTION_mtdblock:
1945 drive_add(optarg, MTD_ALIAS);
1946 break;
1947 case QEMU_OPTION_sd:
1948 drive_add(optarg, SD_ALIAS);
1949 break;
1950 case QEMU_OPTION_pflash:
1951 drive_add(optarg, PFLASH_ALIAS);
1952 break;
1953 case QEMU_OPTION_snapshot:
1954 snapshot = 1;
1955 break;
1956 case QEMU_OPTION_hdachs:
1958 const char *p;
1959 p = optarg;
1960 cyls = strtol(p, (char **)&p, 0);
1961 if (cyls < 1 || cyls > 16383)
1962 goto chs_fail;
1963 if (*p != ',')
1964 goto chs_fail;
1965 p++;
1966 heads = strtol(p, (char **)&p, 0);
1967 if (heads < 1 || heads > 16)
1968 goto chs_fail;
1969 if (*p != ',')
1970 goto chs_fail;
1971 p++;
1972 secs = strtol(p, (char **)&p, 0);
1973 if (secs < 1 || secs > 63)
1974 goto chs_fail;
1975 if (*p == ',') {
1976 p++;
1977 if (!strcmp(p, "none"))
1978 translation = BIOS_ATA_TRANSLATION_NONE;
1979 else if (!strcmp(p, "lba"))
1980 translation = BIOS_ATA_TRANSLATION_LBA;
1981 else if (!strcmp(p, "auto"))
1982 translation = BIOS_ATA_TRANSLATION_AUTO;
1983 else
1984 goto chs_fail;
1985 } else if (*p != '\0') {
1986 chs_fail:
1987 fprintf(stderr, "qemu: invalid physical CHS format\n");
1988 exit(1);
1990 if (hda_opts != NULL) {
1991 char num[16];
1992 snprintf(num, sizeof(num), "%d", cyls);
1993 qemu_opt_set(hda_opts, "cyls", num);
1994 snprintf(num, sizeof(num), "%d", heads);
1995 qemu_opt_set(hda_opts, "heads", num);
1996 snprintf(num, sizeof(num), "%d", secs);
1997 qemu_opt_set(hda_opts, "secs", num);
1998 if (translation == BIOS_ATA_TRANSLATION_LBA)
1999 qemu_opt_set(hda_opts, "trans", "lba");
2000 if (translation == BIOS_ATA_TRANSLATION_NONE)
2001 qemu_opt_set(hda_opts, "trans", "none");
2004 break;
2005 case QEMU_OPTION_numa:
2006 if (nb_numa_nodes >= MAX_NODES) {
2007 fprintf(stderr, "qemu: too many NUMA nodes\n");
2008 exit(1);
2010 numa_add(optarg);
2011 break;
2012 case QEMU_OPTION_nographic:
2013 display_type = DT_NOGRAPHIC;
2014 break;
2015 #ifdef CONFIG_CURSES
2016 case QEMU_OPTION_curses:
2017 display_type = DT_CURSES;
2018 break;
2019 #endif
2020 case QEMU_OPTION_portrait:
2021 graphic_rotate = 1;
2022 break;
2023 case QEMU_OPTION_kernel:
2024 kernel_filename = optarg;
2025 break;
2026 case QEMU_OPTION_append:
2027 kernel_cmdline = optarg;
2028 break;
2029 case QEMU_OPTION_cdrom:
2030 drive_add(optarg, CDROM_ALIAS);
2031 break;
2032 case QEMU_OPTION_boot:
2034 static const char * const params[] = {
2035 "order", "once", "menu", NULL
2037 char buf[sizeof(boot_devices)];
2038 char *standard_boot_devices;
2039 int legacy = 0;
2041 if (!strchr(optarg, '=')) {
2042 legacy = 1;
2043 pstrcpy(buf, sizeof(buf), optarg);
2044 } else if (check_params(buf, sizeof(buf), params, optarg) < 0) {
2045 fprintf(stderr,
2046 "qemu: unknown boot parameter '%s' in '%s'\n",
2047 buf, optarg);
2048 exit(1);
2051 if (legacy ||
2052 get_param_value(buf, sizeof(buf), "order", optarg)) {
2053 validate_bootdevices(buf);
2054 pstrcpy(boot_devices, sizeof(boot_devices), buf);
2056 if (!legacy) {
2057 if (get_param_value(buf, sizeof(buf),
2058 "once", optarg)) {
2059 validate_bootdevices(buf);
2060 standard_boot_devices = qemu_strdup(boot_devices);
2061 pstrcpy(boot_devices, sizeof(boot_devices), buf);
2062 qemu_register_reset(restore_boot_devices,
2063 standard_boot_devices);
2065 if (get_param_value(buf, sizeof(buf),
2066 "menu", optarg)) {
2067 if (!strcmp(buf, "on")) {
2068 boot_menu = 1;
2069 } else if (!strcmp(buf, "off")) {
2070 boot_menu = 0;
2071 } else {
2072 fprintf(stderr,
2073 "qemu: invalid option value '%s'\n",
2074 buf);
2075 exit(1);
2080 break;
2081 case QEMU_OPTION_fda:
2082 case QEMU_OPTION_fdb:
2083 drive_add(optarg, FD_ALIAS, popt->index - QEMU_OPTION_fda);
2084 break;
2085 case QEMU_OPTION_no_fd_bootchk:
2086 fd_bootchk = 0;
2087 break;
2088 case QEMU_OPTION_netdev:
2089 if (net_client_parse(&qemu_netdev_opts, optarg) == -1) {
2090 exit(1);
2092 break;
2093 case QEMU_OPTION_net:
2094 if (net_client_parse(&qemu_net_opts, optarg) == -1) {
2095 exit(1);
2097 break;
2098 #ifdef CONFIG_SLIRP
2099 case QEMU_OPTION_tftp:
2100 legacy_tftp_prefix = optarg;
2101 break;
2102 case QEMU_OPTION_bootp:
2103 legacy_bootp_filename = optarg;
2104 break;
2105 case QEMU_OPTION_redir:
2106 if (net_slirp_redir(optarg) < 0)
2107 exit(1);
2108 break;
2109 #endif
2110 case QEMU_OPTION_bt:
2111 add_device_config(DEV_BT, optarg);
2112 break;
2113 case QEMU_OPTION_audio_help:
2114 if (!(audio_available())) {
2115 printf("Option %s not supported for this target\n", popt->name);
2116 exit(1);
2118 AUD_help ();
2119 exit (0);
2120 break;
2121 case QEMU_OPTION_soundhw:
2122 if (!(audio_available())) {
2123 printf("Option %s not supported for this target\n", popt->name);
2124 exit(1);
2126 select_soundhw (optarg);
2127 break;
2128 case QEMU_OPTION_h:
2129 help(0);
2130 break;
2131 case QEMU_OPTION_version:
2132 version();
2133 exit(0);
2134 break;
2135 case QEMU_OPTION_m: {
2136 uint64_t value;
2137 char *ptr;
2139 value = strtoul(optarg, &ptr, 10);
2140 switch (*ptr) {
2141 case 0: case 'M': case 'm':
2142 value <<= 20;
2143 break;
2144 case 'G': case 'g':
2145 value <<= 30;
2146 break;
2147 default:
2148 fprintf(stderr, "qemu: invalid ram size: %s\n", optarg);
2149 exit(1);
2152 /* On 32-bit hosts, QEMU is limited by virtual address space */
2153 if (value > (2047 << 20) && HOST_LONG_BITS == 32) {
2154 fprintf(stderr, "qemu: at most 2047 MB RAM can be simulated\n");
2155 exit(1);
2157 if (value != (uint64_t)(ram_addr_t)value) {
2158 fprintf(stderr, "qemu: ram size too large\n");
2159 exit(1);
2161 ram_size = value;
2162 break;
2164 case QEMU_OPTION_mempath:
2165 mem_path = optarg;
2166 break;
2167 #ifdef MAP_POPULATE
2168 case QEMU_OPTION_mem_prealloc:
2169 mem_prealloc = 1;
2170 break;
2171 #endif
2172 case QEMU_OPTION_d:
2173 set_cpu_log(optarg);
2174 break;
2175 case QEMU_OPTION_s:
2176 gdbstub_dev = "tcp::" DEFAULT_GDBSTUB_PORT;
2177 break;
2178 case QEMU_OPTION_gdb:
2179 gdbstub_dev = optarg;
2180 break;
2181 case QEMU_OPTION_L:
2182 data_dir = optarg;
2183 break;
2184 case QEMU_OPTION_bios:
2185 bios_name = optarg;
2186 break;
2187 case QEMU_OPTION_singlestep:
2188 singlestep = 1;
2189 break;
2190 case QEMU_OPTION_S:
2191 autostart = 0;
2192 break;
2193 case QEMU_OPTION_k:
2194 keyboard_layout = optarg;
2195 break;
2196 case QEMU_OPTION_localtime:
2197 rtc_utc = 0;
2198 break;
2199 case QEMU_OPTION_vga:
2200 select_vgahw (optarg);
2201 break;
2202 case QEMU_OPTION_g:
2204 const char *p;
2205 int w, h, depth;
2206 p = optarg;
2207 w = strtol(p, (char **)&p, 10);
2208 if (w <= 0) {
2209 graphic_error:
2210 fprintf(stderr, "qemu: invalid resolution or depth\n");
2211 exit(1);
2213 if (*p != 'x')
2214 goto graphic_error;
2215 p++;
2216 h = strtol(p, (char **)&p, 10);
2217 if (h <= 0)
2218 goto graphic_error;
2219 if (*p == 'x') {
2220 p++;
2221 depth = strtol(p, (char **)&p, 10);
2222 if (depth != 8 && depth != 15 && depth != 16 &&
2223 depth != 24 && depth != 32)
2224 goto graphic_error;
2225 } else if (*p == '\0') {
2226 depth = graphic_depth;
2227 } else {
2228 goto graphic_error;
2231 graphic_width = w;
2232 graphic_height = h;
2233 graphic_depth = depth;
2235 break;
2236 case QEMU_OPTION_echr:
2238 char *r;
2239 term_escape_char = strtol(optarg, &r, 0);
2240 if (r == optarg)
2241 printf("Bad argument to echr\n");
2242 break;
2244 case QEMU_OPTION_monitor:
2245 monitor_parse(optarg, "readline");
2246 default_monitor = 0;
2247 break;
2248 case QEMU_OPTION_qmp:
2249 monitor_parse(optarg, "control");
2250 default_monitor = 0;
2251 break;
2252 case QEMU_OPTION_mon:
2253 opts = qemu_opts_parse(&qemu_mon_opts, optarg, 1);
2254 if (!opts) {
2255 exit(1);
2257 default_monitor = 0;
2258 break;
2259 case QEMU_OPTION_chardev:
2260 opts = qemu_opts_parse(&qemu_chardev_opts, optarg, 1);
2261 if (!opts) {
2262 exit(1);
2264 break;
2265 #ifdef CONFIG_LINUX
2266 case QEMU_OPTION_fsdev:
2267 opts = qemu_opts_parse(&qemu_fsdev_opts, optarg, 1);
2268 if (!opts) {
2269 fprintf(stderr, "parse error: %s\n", optarg);
2270 exit(1);
2272 break;
2273 case QEMU_OPTION_virtfs: {
2274 char *arg_fsdev = NULL;
2275 char *arg_9p = NULL;
2276 int len = 0;
2278 opts = qemu_opts_parse(&qemu_virtfs_opts, optarg, 1);
2279 if (!opts) {
2280 fprintf(stderr, "parse error: %s\n", optarg);
2281 exit(1);
2284 len = strlen(",id=,path=");
2285 len += strlen(qemu_opt_get(opts, "fstype"));
2286 len += strlen(qemu_opt_get(opts, "mount_tag"));
2287 len += strlen(qemu_opt_get(opts, "path"));
2288 arg_fsdev = qemu_malloc((len + 1) * sizeof(*arg_fsdev));
2290 if (!arg_fsdev) {
2291 fprintf(stderr, "No memory to parse -fsdev for %s\n",
2292 optarg);
2293 exit(1);
2296 sprintf(arg_fsdev, "%s,id=%s,path=%s",
2297 qemu_opt_get(opts, "fstype"),
2298 qemu_opt_get(opts, "mount_tag"),
2299 qemu_opt_get(opts, "path"));
2301 len = strlen("virtio-9p-pci,fsdev=,mount_tag=");
2302 len += 2*strlen(qemu_opt_get(opts, "mount_tag"));
2303 arg_9p = qemu_malloc((len + 1) * sizeof(*arg_9p));
2305 if (!arg_9p) {
2306 fprintf(stderr, "No memory to parse -device for %s\n",
2307 optarg);
2308 exit(1);
2311 sprintf(arg_9p, "virtio-9p-pci,fsdev=%s,mount_tag=%s",
2312 qemu_opt_get(opts, "mount_tag"),
2313 qemu_opt_get(opts, "mount_tag"));
2315 if (!qemu_opts_parse(&qemu_fsdev_opts, arg_fsdev, 1)) {
2316 fprintf(stderr, "parse error [fsdev]: %s\n", optarg);
2317 exit(1);
2320 if (!qemu_opts_parse(&qemu_device_opts, arg_9p, 1)) {
2321 fprintf(stderr, "parse error [device]: %s\n", optarg);
2322 exit(1);
2325 qemu_free(arg_fsdev);
2326 qemu_free(arg_9p);
2327 break;
2329 #endif
2330 case QEMU_OPTION_serial:
2331 add_device_config(DEV_SERIAL, optarg);
2332 default_serial = 0;
2333 if (strncmp(optarg, "mon:", 4) == 0) {
2334 default_monitor = 0;
2336 break;
2337 case QEMU_OPTION_watchdog:
2338 if (watchdog) {
2339 fprintf(stderr,
2340 "qemu: only one watchdog option may be given\n");
2341 return 1;
2343 watchdog = optarg;
2344 break;
2345 case QEMU_OPTION_watchdog_action:
2346 if (select_watchdog_action(optarg) == -1) {
2347 fprintf(stderr, "Unknown -watchdog-action parameter\n");
2348 exit(1);
2350 break;
2351 case QEMU_OPTION_virtiocon:
2352 add_device_config(DEV_VIRTCON, optarg);
2353 default_virtcon = 0;
2354 if (strncmp(optarg, "mon:", 4) == 0) {
2355 default_monitor = 0;
2357 break;
2358 case QEMU_OPTION_parallel:
2359 add_device_config(DEV_PARALLEL, optarg);
2360 default_parallel = 0;
2361 if (strncmp(optarg, "mon:", 4) == 0) {
2362 default_monitor = 0;
2364 break;
2365 case QEMU_OPTION_debugcon:
2366 add_device_config(DEV_DEBUGCON, optarg);
2367 break;
2368 case QEMU_OPTION_loadvm:
2369 loadvm = optarg;
2370 break;
2371 case QEMU_OPTION_full_screen:
2372 full_screen = 1;
2373 break;
2374 #ifdef CONFIG_SDL
2375 case QEMU_OPTION_no_frame:
2376 no_frame = 1;
2377 break;
2378 case QEMU_OPTION_alt_grab:
2379 alt_grab = 1;
2380 break;
2381 case QEMU_OPTION_ctrl_grab:
2382 ctrl_grab = 1;
2383 break;
2384 case QEMU_OPTION_no_quit:
2385 no_quit = 1;
2386 break;
2387 case QEMU_OPTION_sdl:
2388 display_type = DT_SDL;
2389 break;
2390 #endif
2391 case QEMU_OPTION_pidfile:
2392 pid_file = optarg;
2393 break;
2394 case QEMU_OPTION_win2k_hack:
2395 win2k_install_hack = 1;
2396 break;
2397 case QEMU_OPTION_rtc_td_hack:
2398 rtc_td_hack = 1;
2399 break;
2400 case QEMU_OPTION_acpitable:
2401 do_acpitable_option(optarg);
2402 break;
2403 case QEMU_OPTION_smbios:
2404 do_smbios_option(optarg);
2405 break;
2406 case QEMU_OPTION_enable_kvm:
2407 kvm_allowed = 1;
2408 break;
2409 case QEMU_OPTION_usb:
2410 usb_enabled = 1;
2411 break;
2412 case QEMU_OPTION_usbdevice:
2413 usb_enabled = 1;
2414 add_device_config(DEV_USB, optarg);
2415 break;
2416 case QEMU_OPTION_device:
2417 if (!qemu_opts_parse(&qemu_device_opts, optarg, 1)) {
2418 exit(1);
2420 break;
2421 case QEMU_OPTION_smp:
2422 smp_parse(optarg);
2423 if (smp_cpus < 1) {
2424 fprintf(stderr, "Invalid number of CPUs\n");
2425 exit(1);
2427 if (max_cpus < smp_cpus) {
2428 fprintf(stderr, "maxcpus must be equal to or greater than "
2429 "smp\n");
2430 exit(1);
2432 if (max_cpus > 255) {
2433 fprintf(stderr, "Unsupported number of maxcpus\n");
2434 exit(1);
2436 break;
2437 case QEMU_OPTION_vnc:
2438 display_type = DT_VNC;
2439 vnc_display = optarg;
2440 break;
2441 case QEMU_OPTION_no_acpi:
2442 acpi_enabled = 0;
2443 break;
2444 case QEMU_OPTION_no_hpet:
2445 no_hpet = 1;
2446 break;
2447 case QEMU_OPTION_balloon:
2448 if (balloon_parse(optarg) < 0) {
2449 fprintf(stderr, "Unknown -balloon argument %s\n", optarg);
2450 exit(1);
2452 break;
2453 case QEMU_OPTION_no_reboot:
2454 no_reboot = 1;
2455 break;
2456 case QEMU_OPTION_no_shutdown:
2457 no_shutdown = 1;
2458 break;
2459 case QEMU_OPTION_show_cursor:
2460 cursor_hide = 0;
2461 break;
2462 case QEMU_OPTION_uuid:
2463 if(qemu_uuid_parse(optarg, qemu_uuid) < 0) {
2464 fprintf(stderr, "Fail to parse UUID string."
2465 " Wrong format.\n");
2466 exit(1);
2468 break;
2469 case QEMU_OPTION_option_rom:
2470 if (nb_option_roms >= MAX_OPTION_ROMS) {
2471 fprintf(stderr, "Too many option ROMs\n");
2472 exit(1);
2474 option_rom[nb_option_roms] = optarg;
2475 nb_option_roms++;
2476 break;
2477 case QEMU_OPTION_semihosting:
2478 semihosting_enabled = 1;
2479 break;
2480 case QEMU_OPTION_name:
2481 qemu_name = qemu_strdup(optarg);
2483 char *p = strchr(qemu_name, ',');
2484 if (p != NULL) {
2485 *p++ = 0;
2486 if (strncmp(p, "process=", 8)) {
2487 fprintf(stderr, "Unknown subargument %s to -name", p);
2488 exit(1);
2490 p += 8;
2491 os_set_proc_name(p);
2494 break;
2495 case QEMU_OPTION_prom_env:
2496 if (nb_prom_envs >= MAX_PROM_ENVS) {
2497 fprintf(stderr, "Too many prom variables\n");
2498 exit(1);
2500 prom_envs[nb_prom_envs] = optarg;
2501 nb_prom_envs++;
2502 break;
2503 case QEMU_OPTION_old_param:
2504 old_param = 1;
2505 break;
2506 case QEMU_OPTION_clock:
2507 configure_alarms(optarg);
2508 break;
2509 case QEMU_OPTION_startdate:
2510 configure_rtc_date_offset(optarg, 1);
2511 break;
2512 case QEMU_OPTION_rtc:
2513 opts = qemu_opts_parse(&qemu_rtc_opts, optarg, 0);
2514 if (!opts) {
2515 exit(1);
2517 configure_rtc(opts);
2518 break;
2519 case QEMU_OPTION_tb_size:
2520 tb_size = strtol(optarg, NULL, 0);
2521 if (tb_size < 0)
2522 tb_size = 0;
2523 break;
2524 case QEMU_OPTION_icount:
2525 icount_option = optarg;
2526 break;
2527 case QEMU_OPTION_incoming:
2528 incoming = optarg;
2529 break;
2530 case QEMU_OPTION_nodefaults:
2531 default_serial = 0;
2532 default_parallel = 0;
2533 default_virtcon = 0;
2534 default_monitor = 0;
2535 default_vga = 0;
2536 default_net = 0;
2537 default_floppy = 0;
2538 default_cdrom = 0;
2539 default_sdcard = 0;
2540 break;
2541 case QEMU_OPTION_xen_domid:
2542 if (!(xen_available())) {
2543 printf("Option %s not supported for this target\n", popt->name);
2544 exit(1);
2546 xen_domid = atoi(optarg);
2547 break;
2548 case QEMU_OPTION_xen_create:
2549 if (!(xen_available())) {
2550 printf("Option %s not supported for this target\n", popt->name);
2551 exit(1);
2553 xen_mode = XEN_CREATE;
2554 break;
2555 case QEMU_OPTION_xen_attach:
2556 if (!(xen_available())) {
2557 printf("Option %s not supported for this target\n", popt->name);
2558 exit(1);
2560 xen_mode = XEN_ATTACH;
2561 break;
2562 case QEMU_OPTION_readconfig:
2564 int ret = qemu_read_config_file(optarg);
2565 if (ret < 0) {
2566 fprintf(stderr, "read config %s: %s\n", optarg,
2567 strerror(-ret));
2568 exit(1);
2570 break;
2572 case QEMU_OPTION_writeconfig:
2574 FILE *fp;
2575 if (strcmp(optarg, "-") == 0) {
2576 fp = stdout;
2577 } else {
2578 fp = fopen(optarg, "w");
2579 if (fp == NULL) {
2580 fprintf(stderr, "open %s: %s\n", optarg, strerror(errno));
2581 exit(1);
2584 qemu_config_write(fp);
2585 fclose(fp);
2586 break;
2588 default:
2589 os_parse_cmd_args(popt->index, optarg);
2593 loc_set_none();
2595 /* If no data_dir is specified then try to find it relative to the
2596 executable path. */
2597 if (!data_dir) {
2598 data_dir = os_find_datadir(argv[0]);
2600 /* If all else fails use the install patch specified when building. */
2601 if (!data_dir) {
2602 data_dir = CONFIG_QEMU_DATADIR;
2606 * Default to max_cpus = smp_cpus, in case the user doesn't
2607 * specify a max_cpus value.
2609 if (!max_cpus)
2610 max_cpus = smp_cpus;
2612 machine->max_cpus = machine->max_cpus ?: 1; /* Default to UP */
2613 if (smp_cpus > machine->max_cpus) {
2614 fprintf(stderr, "Number of SMP cpus requested (%d), exceeds max cpus "
2615 "supported by machine `%s' (%d)\n", smp_cpus, machine->name,
2616 machine->max_cpus);
2617 exit(1);
2620 qemu_opts_foreach(&qemu_device_opts, default_driver_check, NULL, 0);
2621 qemu_opts_foreach(&qemu_global_opts, default_driver_check, NULL, 0);
2623 if (machine->no_serial) {
2624 default_serial = 0;
2626 if (machine->no_parallel) {
2627 default_parallel = 0;
2629 if (!machine->use_virtcon) {
2630 default_virtcon = 0;
2632 if (machine->no_vga) {
2633 default_vga = 0;
2635 if (machine->no_floppy) {
2636 default_floppy = 0;
2638 if (machine->no_cdrom) {
2639 default_cdrom = 0;
2641 if (machine->no_sdcard) {
2642 default_sdcard = 0;
2645 if (display_type == DT_NOGRAPHIC) {
2646 if (default_parallel)
2647 add_device_config(DEV_PARALLEL, "null");
2648 if (default_serial && default_monitor) {
2649 add_device_config(DEV_SERIAL, "mon:stdio");
2650 } else if (default_virtcon && default_monitor) {
2651 add_device_config(DEV_VIRTCON, "mon:stdio");
2652 } else {
2653 if (default_serial)
2654 add_device_config(DEV_SERIAL, "stdio");
2655 if (default_virtcon)
2656 add_device_config(DEV_VIRTCON, "stdio");
2657 if (default_monitor)
2658 monitor_parse("stdio", "readline");
2660 } else {
2661 if (default_serial)
2662 add_device_config(DEV_SERIAL, "vc:80Cx24C");
2663 if (default_parallel)
2664 add_device_config(DEV_PARALLEL, "vc:80Cx24C");
2665 if (default_monitor)
2666 monitor_parse("vc:80Cx24C", "readline");
2667 if (default_virtcon)
2668 add_device_config(DEV_VIRTCON, "vc:80Cx24C");
2670 if (default_vga)
2671 vga_interface_type = VGA_CIRRUS;
2673 socket_init();
2675 if (qemu_opts_foreach(&qemu_chardev_opts, chardev_init_func, NULL, 1) != 0)
2676 exit(1);
2677 #ifdef CONFIG_LINUX
2678 if (qemu_opts_foreach(&qemu_fsdev_opts, fsdev_init_func, NULL, 1) != 0) {
2679 exit(1);
2681 #endif
2683 os_daemonize();
2685 if (pid_file && qemu_create_pidfile(pid_file) != 0) {
2686 os_pidfile_error();
2687 exit(1);
2690 if (kvm_allowed) {
2691 int ret = kvm_init(smp_cpus);
2692 if (ret < 0) {
2693 if (!kvm_available()) {
2694 printf("KVM not supported for this target\n");
2695 } else {
2696 fprintf(stderr, "failed to initialize KVM: %s\n", strerror(-ret));
2698 exit(1);
2702 if (qemu_init_main_loop()) {
2703 fprintf(stderr, "qemu_init_main_loop failed\n");
2704 exit(1);
2706 linux_boot = (kernel_filename != NULL);
2708 if (!linux_boot && *kernel_cmdline != '\0') {
2709 fprintf(stderr, "-append only allowed with -kernel option\n");
2710 exit(1);
2713 if (!linux_boot && initrd_filename != NULL) {
2714 fprintf(stderr, "-initrd only allowed with -kernel option\n");
2715 exit(1);
2718 os_set_line_buffering();
2720 if (init_timer_alarm() < 0) {
2721 fprintf(stderr, "could not initialize alarm timer\n");
2722 exit(1);
2724 configure_icount(icount_option);
2726 if (net_init_clients() < 0) {
2727 exit(1);
2730 /* init the bluetooth world */
2731 if (foreach_device_config(DEV_BT, bt_parse))
2732 exit(1);
2734 /* init the memory */
2735 if (ram_size == 0)
2736 ram_size = DEFAULT_RAM_SIZE * 1024 * 1024;
2738 /* init the dynamic translator */
2739 cpu_exec_init_all(tb_size * 1024 * 1024);
2741 bdrv_init_with_whitelist();
2743 blk_mig_init();
2745 if (default_cdrom) {
2746 /* we always create the cdrom drive, even if no disk is there */
2747 drive_add(NULL, CDROM_ALIAS);
2750 if (default_floppy) {
2751 /* we always create at least one floppy */
2752 drive_add(NULL, FD_ALIAS, 0);
2755 if (default_sdcard) {
2756 /* we always create one sd slot, even if no card is in it */
2757 drive_add(NULL, SD_ALIAS);
2760 /* open the virtual block devices */
2761 if (snapshot)
2762 qemu_opts_foreach(&qemu_drive_opts, drive_enable_snapshot, NULL, 0);
2763 if (qemu_opts_foreach(&qemu_drive_opts, drive_init_func, &machine->use_scsi, 1) != 0)
2764 exit(1);
2766 register_savevm_live("ram", 0, 3, NULL, ram_save_live, NULL,
2767 ram_load, NULL);
2769 if (nb_numa_nodes > 0) {
2770 int i;
2772 if (nb_numa_nodes > smp_cpus) {
2773 nb_numa_nodes = smp_cpus;
2776 /* If no memory size if given for any node, assume the default case
2777 * and distribute the available memory equally across all nodes
2779 for (i = 0; i < nb_numa_nodes; i++) {
2780 if (node_mem[i] != 0)
2781 break;
2783 if (i == nb_numa_nodes) {
2784 uint64_t usedmem = 0;
2786 /* On Linux, the each node's border has to be 8MB aligned,
2787 * the final node gets the rest.
2789 for (i = 0; i < nb_numa_nodes - 1; i++) {
2790 node_mem[i] = (ram_size / nb_numa_nodes) & ~((1 << 23UL) - 1);
2791 usedmem += node_mem[i];
2793 node_mem[i] = ram_size - usedmem;
2796 for (i = 0; i < nb_numa_nodes; i++) {
2797 if (node_cpumask[i] != 0)
2798 break;
2800 /* assigning the VCPUs round-robin is easier to implement, guest OSes
2801 * must cope with this anyway, because there are BIOSes out there in
2802 * real machines which also use this scheme.
2804 if (i == nb_numa_nodes) {
2805 for (i = 0; i < smp_cpus; i++) {
2806 node_cpumask[i % nb_numa_nodes] |= 1 << i;
2811 if (qemu_opts_foreach(&qemu_mon_opts, mon_init_func, NULL, 1) != 0) {
2812 exit(1);
2815 if (foreach_device_config(DEV_SERIAL, serial_parse) < 0)
2816 exit(1);
2817 if (foreach_device_config(DEV_PARALLEL, parallel_parse) < 0)
2818 exit(1);
2819 if (foreach_device_config(DEV_VIRTCON, virtcon_parse) < 0)
2820 exit(1);
2821 if (foreach_device_config(DEV_DEBUGCON, debugcon_parse) < 0)
2822 exit(1);
2824 module_call_init(MODULE_INIT_DEVICE);
2826 if (qemu_opts_foreach(&qemu_device_opts, device_help_func, NULL, 0) != 0)
2827 exit(0);
2829 if (watchdog) {
2830 i = select_watchdog(watchdog);
2831 if (i > 0)
2832 exit (i == 1 ? 1 : 0);
2835 if (machine->compat_props) {
2836 qdev_prop_register_global_list(machine->compat_props);
2838 qemu_add_globals();
2840 machine->init(ram_size, boot_devices,
2841 kernel_filename, kernel_cmdline, initrd_filename, cpu_model);
2843 cpu_synchronize_all_post_init();
2845 /* must be after terminal init, SDL library changes signal handlers */
2846 os_setup_signal_handling();
2848 set_numa_modes();
2850 current_machine = machine;
2852 /* init USB devices */
2853 if (usb_enabled) {
2854 if (foreach_device_config(DEV_USB, usb_parse) < 0)
2855 exit(1);
2858 /* init generic devices */
2859 if (qemu_opts_foreach(&qemu_device_opts, device_init_func, NULL, 1) != 0)
2860 exit(1);
2862 net_check_clients();
2864 /* just use the first displaystate for the moment */
2865 ds = get_displaystate();
2867 if (display_type == DT_DEFAULT) {
2868 #if defined(CONFIG_SDL) || defined(CONFIG_COCOA)
2869 display_type = DT_SDL;
2870 #else
2871 display_type = DT_VNC;
2872 vnc_display = "localhost:0,to=99";
2873 show_vnc_port = 1;
2874 #endif
2878 switch (display_type) {
2879 case DT_NOGRAPHIC:
2880 break;
2881 #if defined(CONFIG_CURSES)
2882 case DT_CURSES:
2883 curses_display_init(ds, full_screen);
2884 break;
2885 #endif
2886 #if defined(CONFIG_SDL)
2887 case DT_SDL:
2888 sdl_display_init(ds, full_screen, no_frame);
2889 break;
2890 #elif defined(CONFIG_COCOA)
2891 case DT_SDL:
2892 cocoa_display_init(ds, full_screen);
2893 break;
2894 #endif
2895 case DT_VNC:
2896 vnc_display_init(ds);
2897 if (vnc_display_open(ds, vnc_display) < 0)
2898 exit(1);
2900 if (show_vnc_port) {
2901 printf("VNC server running on `%s'\n", vnc_display_local_addr(ds));
2903 break;
2904 default:
2905 break;
2907 dpy_resize(ds);
2909 dcl = ds->listeners;
2910 while (dcl != NULL) {
2911 if (dcl->dpy_refresh != NULL) {
2912 ds->gui_timer = qemu_new_timer(rt_clock, gui_update, ds);
2913 qemu_mod_timer(ds->gui_timer, qemu_get_clock(rt_clock));
2914 break;
2916 dcl = dcl->next;
2919 if (display_type == DT_NOGRAPHIC || display_type == DT_VNC) {
2920 nographic_timer = qemu_new_timer(rt_clock, nographic_update, NULL);
2921 qemu_mod_timer(nographic_timer, qemu_get_clock(rt_clock));
2924 text_consoles_set_display(ds);
2926 if (gdbstub_dev && gdbserver_start(gdbstub_dev) < 0) {
2927 fprintf(stderr, "qemu: could not open gdbserver on device '%s'\n",
2928 gdbstub_dev);
2929 exit(1);
2932 qdev_machine_creation_done();
2934 if (rom_load_all() != 0) {
2935 fprintf(stderr, "rom loading failed\n");
2936 exit(1);
2939 qemu_system_reset();
2940 if (loadvm) {
2941 if (load_vmstate(loadvm) < 0) {
2942 autostart = 0;
2946 if (incoming) {
2947 qemu_start_incoming_migration(incoming);
2948 } else if (autostart) {
2949 vm_start();
2952 os_setup_post();
2954 main_loop();
2955 quit_timers();
2956 net_cleanup();
2958 return 0;