ac97: IOMMU support
[qemu-kvm/amd-iommu.git] / vl.c
blob80a929d5a78feacd3273e1a23b68c63ee447cd1a
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 #include "hw/device-assignment.h"
151 #ifdef CONFIG_VIRTFS
152 #include "fsdev/qemu-fsdev.h"
153 #endif
155 #include "disas.h"
157 #include "qemu_socket.h"
159 #include "slirp/libslirp.h"
161 #include "qemu-queue.h"
162 #include "cpus.h"
163 #include "arch_init.h"
165 //#define DEBUG_NET
166 //#define DEBUG_SLIRP
168 #define DEFAULT_RAM_SIZE 128
170 #define MAX_VIRTIO_CONSOLES 1
172 static const char *data_dir;
173 const char *bios_name = NULL;
174 enum vga_retrace_method vga_retrace_method = VGA_RETRACE_DUMB;
175 DisplayType display_type = DT_DEFAULT;
176 const char* keyboard_layout = NULL;
177 ram_addr_t ram_size;
178 const char *mem_path = NULL;
179 #ifdef MAP_POPULATE
180 int mem_prealloc = 0; /* force preallocation of physical target memory */
181 #endif
182 int nb_nics;
183 NICInfo nd_table[MAX_NICS];
184 int vm_running;
185 int autostart;
186 static int rtc_utc = 1;
187 static int rtc_date_offset = -1; /* -1 means no change */
188 QEMUClock *rtc_clock;
189 int vga_interface_type = VGA_NONE;
190 static int full_screen = 0;
191 #ifdef CONFIG_SDL
192 static int no_frame = 0;
193 #endif
194 int no_quit = 0;
195 CharDriverState *serial_hds[MAX_SERIAL_PORTS];
196 CharDriverState *parallel_hds[MAX_PARALLEL_PORTS];
197 CharDriverState *virtcon_hds[MAX_VIRTIO_CONSOLES];
198 int win2k_install_hack = 0;
199 int rtc_td_hack = 0;
200 int usb_enabled = 0;
201 int singlestep = 0;
202 const char *assigned_devices[MAX_DEV_ASSIGN_CMDLINE];
203 int assigned_devices_index;
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 const char *watchdog;
218 const char *option_rom[MAX_OPTION_ROMS];
219 int nb_option_roms;
220 int semihosting_enabled = 0;
221 int time_drift_fix = 0;
222 unsigned int kvm_shadow_memory = 0;
223 int old_param = 0;
224 const char *qemu_name;
225 int alt_grab = 0;
226 int ctrl_grab = 0;
227 unsigned int nb_prom_envs = 0;
228 const char *prom_envs[MAX_PROM_ENVS];
229 const char *nvram = NULL;
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 static NotifierList exit_notifiers =
244 NOTIFIER_LIST_INITIALIZER(exit_notifiers);
246 int kvm_allowed = 1;
247 uint32_t xen_domid;
248 enum xen_mode xen_mode = XEN_EMULATE;
250 static int default_serial = 1;
251 static int default_parallel = 1;
252 static int default_virtcon = 1;
253 static int default_monitor = 1;
254 static int default_vga = 1;
255 static int default_floppy = 1;
256 static int default_cdrom = 1;
257 static int default_sdcard = 1;
259 static struct {
260 const char *driver;
261 int *flag;
262 } default_list[] = {
263 { .driver = "isa-serial", .flag = &default_serial },
264 { .driver = "isa-parallel", .flag = &default_parallel },
265 { .driver = "isa-fdc", .flag = &default_floppy },
266 { .driver = "ide-drive", .flag = &default_cdrom },
267 { .driver = "virtio-serial-pci", .flag = &default_virtcon },
268 { .driver = "virtio-serial-s390", .flag = &default_virtcon },
269 { .driver = "virtio-serial", .flag = &default_virtcon },
270 { .driver = "VGA", .flag = &default_vga },
271 { .driver = "cirrus-vga", .flag = &default_vga },
272 { .driver = "vmware-svga", .flag = &default_vga },
275 static int default_driver_check(QemuOpts *opts, void *opaque)
277 const char *driver = qemu_opt_get(opts, "driver");
278 int i;
280 if (!driver)
281 return 0;
282 for (i = 0; i < ARRAY_SIZE(default_list); i++) {
283 if (strcmp(default_list[i].driver, driver) != 0)
284 continue;
285 *(default_list[i].flag) = 0;
287 return 0;
290 /***********************************************************/
291 /* real time host monotonic timer */
293 /* compute with 96 bit intermediate result: (a*b)/c */
294 uint64_t muldiv64(uint64_t a, uint32_t b, uint32_t c)
296 union {
297 uint64_t ll;
298 struct {
299 #ifdef HOST_WORDS_BIGENDIAN
300 uint32_t high, low;
301 #else
302 uint32_t low, high;
303 #endif
304 } l;
305 } u, res;
306 uint64_t rl, rh;
308 u.ll = a;
309 rl = (uint64_t)u.l.low * (uint64_t)b;
310 rh = (uint64_t)u.l.high * (uint64_t)b;
311 rh += (rl >> 32);
312 res.l.high = rh / c;
313 res.l.low = (((rh % c) << 32) + (rl & 0xffffffff)) / c;
314 return res.ll;
317 /***********************************************************/
318 /* host time/date access */
319 void qemu_get_timedate(struct tm *tm, int offset)
321 time_t ti;
322 struct tm *ret;
324 time(&ti);
325 ti += offset;
326 if (rtc_date_offset == -1) {
327 if (rtc_utc)
328 ret = gmtime(&ti);
329 else
330 ret = localtime(&ti);
331 } else {
332 ti -= rtc_date_offset;
333 ret = gmtime(&ti);
336 memcpy(tm, ret, sizeof(struct tm));
339 int qemu_timedate_diff(struct tm *tm)
341 time_t seconds;
343 if (rtc_date_offset == -1)
344 if (rtc_utc)
345 seconds = mktimegm(tm);
346 else
347 seconds = mktime(tm);
348 else
349 seconds = mktimegm(tm) + rtc_date_offset;
351 return seconds - time(NULL);
354 void rtc_change_mon_event(struct tm *tm)
356 QObject *data;
358 data = qobject_from_jsonf("{ 'offset': %d }", qemu_timedate_diff(tm));
359 monitor_protocol_event(QEVENT_RTC_CHANGE, data);
360 qobject_decref(data);
363 static void configure_rtc_date_offset(const char *startdate, int legacy)
365 time_t rtc_start_date;
366 struct tm tm;
368 if (!strcmp(startdate, "now") && legacy) {
369 rtc_date_offset = -1;
370 } else {
371 if (sscanf(startdate, "%d-%d-%dT%d:%d:%d",
372 &tm.tm_year,
373 &tm.tm_mon,
374 &tm.tm_mday,
375 &tm.tm_hour,
376 &tm.tm_min,
377 &tm.tm_sec) == 6) {
378 /* OK */
379 } else if (sscanf(startdate, "%d-%d-%d",
380 &tm.tm_year,
381 &tm.tm_mon,
382 &tm.tm_mday) == 3) {
383 tm.tm_hour = 0;
384 tm.tm_min = 0;
385 tm.tm_sec = 0;
386 } else {
387 goto date_fail;
389 tm.tm_year -= 1900;
390 tm.tm_mon--;
391 rtc_start_date = mktimegm(&tm);
392 if (rtc_start_date == -1) {
393 date_fail:
394 fprintf(stderr, "Invalid date format. Valid formats are:\n"
395 "'2006-06-17T16:01:21' or '2006-06-17'\n");
396 exit(1);
398 rtc_date_offset = time(NULL) - rtc_start_date;
402 static void configure_rtc(QemuOpts *opts)
404 const char *value;
406 value = qemu_opt_get(opts, "base");
407 if (value) {
408 if (!strcmp(value, "utc")) {
409 rtc_utc = 1;
410 } else if (!strcmp(value, "localtime")) {
411 rtc_utc = 0;
412 } else {
413 configure_rtc_date_offset(value, 0);
416 value = qemu_opt_get(opts, "clock");
417 if (value) {
418 if (!strcmp(value, "host")) {
419 rtc_clock = host_clock;
420 } else if (!strcmp(value, "vm")) {
421 rtc_clock = vm_clock;
422 } else {
423 fprintf(stderr, "qemu: invalid option value '%s'\n", value);
424 exit(1);
427 value = qemu_opt_get(opts, "driftfix");
428 if (value) {
429 if (!strcmp(value, "slew")) {
430 rtc_td_hack = 1;
431 } else if (!strcmp(value, "none")) {
432 rtc_td_hack = 0;
433 } else {
434 fprintf(stderr, "qemu: invalid option value '%s'\n", value);
435 exit(1);
440 /***********************************************************/
441 /* Bluetooth support */
442 static int nb_hcis;
443 static int cur_hci;
444 static struct HCIInfo *hci_table[MAX_NICS];
446 static struct bt_vlan_s {
447 struct bt_scatternet_s net;
448 int id;
449 struct bt_vlan_s *next;
450 } *first_bt_vlan;
452 /* find or alloc a new bluetooth "VLAN" */
453 static struct bt_scatternet_s *qemu_find_bt_vlan(int id)
455 struct bt_vlan_s **pvlan, *vlan;
456 for (vlan = first_bt_vlan; vlan != NULL; vlan = vlan->next) {
457 if (vlan->id == id)
458 return &vlan->net;
460 vlan = qemu_mallocz(sizeof(struct bt_vlan_s));
461 vlan->id = id;
462 pvlan = &first_bt_vlan;
463 while (*pvlan != NULL)
464 pvlan = &(*pvlan)->next;
465 *pvlan = vlan;
466 return &vlan->net;
469 static void null_hci_send(struct HCIInfo *hci, const uint8_t *data, int len)
473 static int null_hci_addr_set(struct HCIInfo *hci, const uint8_t *bd_addr)
475 return -ENOTSUP;
478 static struct HCIInfo null_hci = {
479 .cmd_send = null_hci_send,
480 .sco_send = null_hci_send,
481 .acl_send = null_hci_send,
482 .bdaddr_set = null_hci_addr_set,
485 struct HCIInfo *qemu_next_hci(void)
487 if (cur_hci == nb_hcis)
488 return &null_hci;
490 return hci_table[cur_hci++];
493 static struct HCIInfo *hci_init(const char *str)
495 char *endp;
496 struct bt_scatternet_s *vlan = 0;
498 if (!strcmp(str, "null"))
499 /* null */
500 return &null_hci;
501 else if (!strncmp(str, "host", 4) && (str[4] == '\0' || str[4] == ':'))
502 /* host[:hciN] */
503 return bt_host_hci(str[4] ? str + 5 : "hci0");
504 else if (!strncmp(str, "hci", 3)) {
505 /* hci[,vlan=n] */
506 if (str[3]) {
507 if (!strncmp(str + 3, ",vlan=", 6)) {
508 vlan = qemu_find_bt_vlan(strtol(str + 9, &endp, 0));
509 if (*endp)
510 vlan = 0;
512 } else
513 vlan = qemu_find_bt_vlan(0);
514 if (vlan)
515 return bt_new_hci(vlan);
518 fprintf(stderr, "qemu: Unknown bluetooth HCI `%s'.\n", str);
520 return 0;
523 static int bt_hci_parse(const char *str)
525 struct HCIInfo *hci;
526 bdaddr_t bdaddr;
528 if (nb_hcis >= MAX_NICS) {
529 fprintf(stderr, "qemu: Too many bluetooth HCIs (max %i).\n", MAX_NICS);
530 return -1;
533 hci = hci_init(str);
534 if (!hci)
535 return -1;
537 bdaddr.b[0] = 0x52;
538 bdaddr.b[1] = 0x54;
539 bdaddr.b[2] = 0x00;
540 bdaddr.b[3] = 0x12;
541 bdaddr.b[4] = 0x34;
542 bdaddr.b[5] = 0x56 + nb_hcis;
543 hci->bdaddr_set(hci, bdaddr.b);
545 hci_table[nb_hcis++] = hci;
547 return 0;
550 static void bt_vhci_add(int vlan_id)
552 struct bt_scatternet_s *vlan = qemu_find_bt_vlan(vlan_id);
554 if (!vlan->slave)
555 fprintf(stderr, "qemu: warning: adding a VHCI to "
556 "an empty scatternet %i\n", vlan_id);
558 bt_vhci_init(bt_new_hci(vlan));
561 static struct bt_device_s *bt_device_add(const char *opt)
563 struct bt_scatternet_s *vlan;
564 int vlan_id = 0;
565 char *endp = strstr(opt, ",vlan=");
566 int len = (endp ? endp - opt : strlen(opt)) + 1;
567 char devname[10];
569 pstrcpy(devname, MIN(sizeof(devname), len), opt);
571 if (endp) {
572 vlan_id = strtol(endp + 6, &endp, 0);
573 if (*endp) {
574 fprintf(stderr, "qemu: unrecognised bluetooth vlan Id\n");
575 return 0;
579 vlan = qemu_find_bt_vlan(vlan_id);
581 if (!vlan->slave)
582 fprintf(stderr, "qemu: warning: adding a slave device to "
583 "an empty scatternet %i\n", vlan_id);
585 if (!strcmp(devname, "keyboard"))
586 return bt_keyboard_init(vlan);
588 fprintf(stderr, "qemu: unsupported bluetooth device `%s'\n", devname);
589 return 0;
592 static int bt_parse(const char *opt)
594 const char *endp, *p;
595 int vlan;
597 if (strstart(opt, "hci", &endp)) {
598 if (!*endp || *endp == ',') {
599 if (*endp)
600 if (!strstart(endp, ",vlan=", 0))
601 opt = endp + 1;
603 return bt_hci_parse(opt);
605 } else if (strstart(opt, "vhci", &endp)) {
606 if (!*endp || *endp == ',') {
607 if (*endp) {
608 if (strstart(endp, ",vlan=", &p)) {
609 vlan = strtol(p, (char **) &endp, 0);
610 if (*endp) {
611 fprintf(stderr, "qemu: bad scatternet '%s'\n", p);
612 return 1;
614 } else {
615 fprintf(stderr, "qemu: bad parameter '%s'\n", endp + 1);
616 return 1;
618 } else
619 vlan = 0;
621 bt_vhci_add(vlan);
622 return 0;
624 } else if (strstart(opt, "device:", &endp))
625 return !bt_device_add(endp);
627 fprintf(stderr, "qemu: bad bluetooth parameter '%s'\n", opt);
628 return 1;
631 /***********************************************************/
632 /* QEMU Block devices */
634 #define HD_ALIAS "index=%d,media=disk"
635 #define CDROM_ALIAS "index=2,media=cdrom"
636 #define FD_ALIAS "index=%d,if=floppy"
637 #define PFLASH_ALIAS "if=pflash"
638 #define MTD_ALIAS "if=mtd"
639 #define SD_ALIAS "index=0,if=sd"
641 static int drive_init_func(QemuOpts *opts, void *opaque)
643 int *use_scsi = opaque;
644 int fatal_error = 0;
646 if (drive_init(opts, *use_scsi, &fatal_error) == NULL) {
647 if (fatal_error)
648 return 1;
650 return 0;
653 static int drive_enable_snapshot(QemuOpts *opts, void *opaque)
655 if (NULL == qemu_opt_get(opts, "snapshot")) {
656 qemu_opt_set(opts, "snapshot", "on");
658 return 0;
661 void qemu_register_boot_set(QEMUBootSetHandler *func, void *opaque)
663 boot_set_handler = func;
664 boot_set_opaque = opaque;
667 int qemu_boot_set(const char *boot_devices)
669 if (!boot_set_handler) {
670 return -EINVAL;
672 return boot_set_handler(boot_set_opaque, boot_devices);
675 static void validate_bootdevices(char *devices)
677 /* We just do some generic consistency checks */
678 const char *p;
679 int bitmap = 0;
681 for (p = devices; *p != '\0'; p++) {
682 /* Allowed boot devices are:
683 * a-b: floppy disk drives
684 * c-f: IDE disk drives
685 * g-m: machine implementation dependant drives
686 * n-p: network devices
687 * It's up to each machine implementation to check if the given boot
688 * devices match the actual hardware implementation and firmware
689 * features.
691 if (*p < 'a' || *p > 'p') {
692 fprintf(stderr, "Invalid boot device '%c'\n", *p);
693 exit(1);
695 if (bitmap & (1 << (*p - 'a'))) {
696 fprintf(stderr, "Boot device '%c' was given twice\n", *p);
697 exit(1);
699 bitmap |= 1 << (*p - 'a');
703 static void restore_boot_devices(void *opaque)
705 char *standard_boot_devices = opaque;
706 static int first = 1;
708 /* Restore boot order and remove ourselves after the first boot */
709 if (first) {
710 first = 0;
711 return;
714 qemu_boot_set(standard_boot_devices);
716 qemu_unregister_reset(restore_boot_devices, standard_boot_devices);
717 qemu_free(standard_boot_devices);
720 static void numa_add(const char *optarg)
722 char option[128];
723 char *endptr;
724 unsigned long long value, endvalue;
725 int nodenr;
727 optarg = get_opt_name(option, 128, optarg, ',') + 1;
728 if (!strcmp(option, "node")) {
729 if (get_param_value(option, 128, "nodeid", optarg) == 0) {
730 nodenr = nb_numa_nodes;
731 } else {
732 nodenr = strtoull(option, NULL, 10);
735 if (get_param_value(option, 128, "mem", optarg) == 0) {
736 node_mem[nodenr] = 0;
737 } else {
738 value = strtoull(option, &endptr, 0);
739 switch (*endptr) {
740 case 0: case 'M': case 'm':
741 value <<= 20;
742 break;
743 case 'G': case 'g':
744 value <<= 30;
745 break;
747 node_mem[nodenr] = value;
749 if (get_param_value(option, 128, "cpus", optarg) == 0) {
750 node_cpumask[nodenr] = 0;
751 } else {
752 value = strtoull(option, &endptr, 10);
753 if (value >= 64) {
754 value = 63;
755 fprintf(stderr, "only 64 CPUs in NUMA mode supported.\n");
756 } else {
757 if (*endptr == '-') {
758 endvalue = strtoull(endptr+1, &endptr, 10);
759 if (endvalue >= 63) {
760 endvalue = 62;
761 fprintf(stderr,
762 "only 63 CPUs in NUMA mode supported.\n");
764 value = (2ULL << endvalue) - (1ULL << value);
765 } else {
766 value = 1ULL << value;
769 node_cpumask[nodenr] = value;
771 nb_numa_nodes++;
773 return;
776 static void smp_parse(const char *optarg)
778 int smp, sockets = 0, threads = 0, cores = 0;
779 char *endptr;
780 char option[128];
782 smp = strtoul(optarg, &endptr, 10);
783 if (endptr != optarg) {
784 if (*endptr == ',') {
785 endptr++;
788 if (get_param_value(option, 128, "sockets", endptr) != 0)
789 sockets = strtoull(option, NULL, 10);
790 if (get_param_value(option, 128, "cores", endptr) != 0)
791 cores = strtoull(option, NULL, 10);
792 if (get_param_value(option, 128, "threads", endptr) != 0)
793 threads = strtoull(option, NULL, 10);
794 if (get_param_value(option, 128, "maxcpus", endptr) != 0)
795 max_cpus = strtoull(option, NULL, 10);
797 /* compute missing values, prefer sockets over cores over threads */
798 if (smp == 0 || sockets == 0) {
799 sockets = sockets > 0 ? sockets : 1;
800 cores = cores > 0 ? cores : 1;
801 threads = threads > 0 ? threads : 1;
802 if (smp == 0) {
803 smp = cores * threads * sockets;
805 } else {
806 if (cores == 0) {
807 threads = threads > 0 ? threads : 1;
808 cores = smp / (sockets * threads);
809 } else {
810 if (sockets) {
811 threads = smp / (cores * sockets);
815 smp_cpus = smp;
816 smp_cores = cores > 0 ? cores : 1;
817 smp_threads = threads > 0 ? threads : 1;
818 if (max_cpus == 0)
819 max_cpus = smp_cpus;
822 /***********************************************************/
823 /* USB devices */
825 static int usb_device_add(const char *devname)
827 const char *p;
828 USBDevice *dev = NULL;
830 if (!usb_enabled)
831 return -1;
833 /* drivers with .usbdevice_name entry in USBDeviceInfo */
834 dev = usbdevice_create(devname);
835 if (dev)
836 goto done;
838 /* the other ones */
839 if (strstart(devname, "host:", &p)) {
840 dev = usb_host_device_open(p);
841 } else if (!strcmp(devname, "bt") || strstart(devname, "bt:", &p)) {
842 dev = usb_bt_init(devname[2] ? hci_init(p) :
843 bt_new_hci(qemu_find_bt_vlan(0)));
844 } else {
845 return -1;
847 if (!dev)
848 return -1;
850 done:
851 return 0;
854 static int usb_device_del(const char *devname)
856 int bus_num, addr;
857 const char *p;
859 if (strstart(devname, "host:", &p))
860 return usb_host_device_close(p);
862 if (!usb_enabled)
863 return -1;
865 p = strchr(devname, '.');
866 if (!p)
867 return -1;
868 bus_num = strtoul(devname, NULL, 0);
869 addr = strtoul(p + 1, NULL, 0);
871 return usb_device_delete_addr(bus_num, addr);
874 static int usb_parse(const char *cmdline)
876 int r;
877 r = usb_device_add(cmdline);
878 if (r < 0) {
879 fprintf(stderr, "qemu: could not add USB device '%s'\n", cmdline);
881 return r;
884 void do_usb_add(Monitor *mon, const QDict *qdict)
886 const char *devname = qdict_get_str(qdict, "devname");
887 if (usb_device_add(devname) < 0) {
888 error_report("could not add USB device '%s'", devname);
892 void do_usb_del(Monitor *mon, const QDict *qdict)
894 const char *devname = qdict_get_str(qdict, "devname");
895 if (usb_device_del(devname) < 0) {
896 error_report("could not delete USB device '%s'", devname);
900 /***********************************************************/
901 /* PCMCIA/Cardbus */
903 static struct pcmcia_socket_entry_s {
904 PCMCIASocket *socket;
905 struct pcmcia_socket_entry_s *next;
906 } *pcmcia_sockets = 0;
908 void pcmcia_socket_register(PCMCIASocket *socket)
910 struct pcmcia_socket_entry_s *entry;
912 entry = qemu_malloc(sizeof(struct pcmcia_socket_entry_s));
913 entry->socket = socket;
914 entry->next = pcmcia_sockets;
915 pcmcia_sockets = entry;
918 void pcmcia_socket_unregister(PCMCIASocket *socket)
920 struct pcmcia_socket_entry_s *entry, **ptr;
922 ptr = &pcmcia_sockets;
923 for (entry = *ptr; entry; ptr = &entry->next, entry = *ptr)
924 if (entry->socket == socket) {
925 *ptr = entry->next;
926 qemu_free(entry);
930 void pcmcia_info(Monitor *mon)
932 struct pcmcia_socket_entry_s *iter;
934 if (!pcmcia_sockets)
935 monitor_printf(mon, "No PCMCIA sockets\n");
937 for (iter = pcmcia_sockets; iter; iter = iter->next)
938 monitor_printf(mon, "%s: %s\n", iter->socket->slot_string,
939 iter->socket->attached ? iter->socket->card_string :
940 "Empty");
943 /***********************************************************/
944 /* I/O handling */
946 typedef struct IOHandlerRecord {
947 int fd;
948 IOCanReadHandler *fd_read_poll;
949 IOHandler *fd_read;
950 IOHandler *fd_write;
951 int deleted;
952 void *opaque;
953 /* temporary data */
954 struct pollfd *ufd;
955 QLIST_ENTRY(IOHandlerRecord) next;
956 } IOHandlerRecord;
958 static QLIST_HEAD(, IOHandlerRecord) io_handlers =
959 QLIST_HEAD_INITIALIZER(io_handlers);
962 /* XXX: fd_read_poll should be suppressed, but an API change is
963 necessary in the character devices to suppress fd_can_read(). */
964 int qemu_set_fd_handler2(int fd,
965 IOCanReadHandler *fd_read_poll,
966 IOHandler *fd_read,
967 IOHandler *fd_write,
968 void *opaque)
970 IOHandlerRecord *ioh;
972 if (!fd_read && !fd_write) {
973 QLIST_FOREACH(ioh, &io_handlers, next) {
974 if (ioh->fd == fd) {
975 ioh->deleted = 1;
976 break;
979 } else {
980 QLIST_FOREACH(ioh, &io_handlers, next) {
981 if (ioh->fd == fd)
982 goto found;
984 ioh = qemu_mallocz(sizeof(IOHandlerRecord));
985 QLIST_INSERT_HEAD(&io_handlers, ioh, next);
986 found:
987 ioh->fd = fd;
988 ioh->fd_read_poll = fd_read_poll;
989 ioh->fd_read = fd_read;
990 ioh->fd_write = fd_write;
991 ioh->opaque = opaque;
992 ioh->deleted = 0;
994 qemu_notify_event();
995 return 0;
998 int qemu_set_fd_handler(int fd,
999 IOHandler *fd_read,
1000 IOHandler *fd_write,
1001 void *opaque)
1003 return qemu_set_fd_handler2(fd, NULL, fd_read, fd_write, opaque);
1006 /***********************************************************/
1007 /* machine registration */
1009 static QEMUMachine *first_machine = NULL;
1010 QEMUMachine *current_machine = NULL;
1012 int qemu_register_machine(QEMUMachine *m)
1014 QEMUMachine **pm;
1015 pm = &first_machine;
1016 while (*pm != NULL)
1017 pm = &(*pm)->next;
1018 m->next = NULL;
1019 *pm = m;
1020 return 0;
1023 static QEMUMachine *find_machine(const char *name)
1025 QEMUMachine *m;
1027 for(m = first_machine; m != NULL; m = m->next) {
1028 if (!strcmp(m->name, name))
1029 return m;
1030 if (m->alias && !strcmp(m->alias, name))
1031 return m;
1033 return NULL;
1036 static QEMUMachine *find_default_machine(void)
1038 QEMUMachine *m;
1040 for(m = first_machine; m != NULL; m = m->next) {
1041 if (m->is_default) {
1042 return m;
1045 return NULL;
1048 /***********************************************************/
1049 /* main execution loop */
1051 static void gui_update(void *opaque)
1053 uint64_t interval = GUI_REFRESH_INTERVAL;
1054 DisplayState *ds = opaque;
1055 DisplayChangeListener *dcl = ds->listeners;
1057 qemu_flush_coalesced_mmio_buffer();
1058 dpy_refresh(ds);
1060 while (dcl != NULL) {
1061 if (dcl->gui_timer_interval &&
1062 dcl->gui_timer_interval < interval)
1063 interval = dcl->gui_timer_interval;
1064 dcl = dcl->next;
1066 qemu_mod_timer(ds->gui_timer, interval + qemu_get_clock(rt_clock));
1069 static void nographic_update(void *opaque)
1071 uint64_t interval = GUI_REFRESH_INTERVAL;
1073 qemu_flush_coalesced_mmio_buffer();
1074 qemu_mod_timer(nographic_timer, interval + qemu_get_clock(rt_clock));
1077 struct vm_change_state_entry {
1078 VMChangeStateHandler *cb;
1079 void *opaque;
1080 QLIST_ENTRY (vm_change_state_entry) entries;
1083 static QLIST_HEAD(vm_change_state_head, vm_change_state_entry) vm_change_state_head;
1085 VMChangeStateEntry *qemu_add_vm_change_state_handler(VMChangeStateHandler *cb,
1086 void *opaque)
1088 VMChangeStateEntry *e;
1090 e = qemu_mallocz(sizeof (*e));
1092 e->cb = cb;
1093 e->opaque = opaque;
1094 QLIST_INSERT_HEAD(&vm_change_state_head, e, entries);
1095 return e;
1098 void qemu_del_vm_change_state_handler(VMChangeStateEntry *e)
1100 QLIST_REMOVE (e, entries);
1101 qemu_free (e);
1104 void vm_state_notify(int running, int reason)
1106 VMChangeStateEntry *e;
1108 for (e = vm_change_state_head.lh_first; e; e = e->entries.le_next) {
1109 e->cb(e->opaque, running, reason);
1113 void vm_start(void)
1115 if (!vm_running) {
1116 cpu_enable_ticks();
1117 vm_running = 1;
1118 vm_state_notify(1, 0);
1119 resume_all_vcpus();
1120 monitor_protocol_event(QEVENT_RESUME, NULL);
1124 /* reset/shutdown handler */
1126 typedef struct QEMUResetEntry {
1127 QTAILQ_ENTRY(QEMUResetEntry) entry;
1128 QEMUResetHandler *func;
1129 void *opaque;
1130 } QEMUResetEntry;
1132 static QTAILQ_HEAD(reset_handlers, QEMUResetEntry) reset_handlers =
1133 QTAILQ_HEAD_INITIALIZER(reset_handlers);
1134 static int reset_requested;
1135 static int shutdown_requested;
1136 static int powerdown_requested;
1137 int debug_requested;
1138 int vmstop_requested;
1140 int qemu_no_shutdown(void)
1142 int r = no_shutdown;
1143 no_shutdown = 0;
1144 return r;
1147 int qemu_shutdown_requested(void)
1149 int r = shutdown_requested;
1150 shutdown_requested = 0;
1151 return r;
1154 int qemu_reset_requested(void)
1156 int r = reset_requested;
1157 reset_requested = 0;
1158 return r;
1161 int qemu_powerdown_requested(void)
1163 int r = powerdown_requested;
1164 powerdown_requested = 0;
1165 return r;
1168 static int qemu_debug_requested(void)
1170 int r = debug_requested;
1171 debug_requested = 0;
1172 return r;
1175 static int qemu_vmstop_requested(void)
1177 int r = vmstop_requested;
1178 vmstop_requested = 0;
1179 return r;
1182 void qemu_register_reset(QEMUResetHandler *func, void *opaque)
1184 QEMUResetEntry *re = qemu_mallocz(sizeof(QEMUResetEntry));
1186 re->func = func;
1187 re->opaque = opaque;
1188 QTAILQ_INSERT_TAIL(&reset_handlers, re, entry);
1191 void qemu_unregister_reset(QEMUResetHandler *func, void *opaque)
1193 QEMUResetEntry *re;
1195 QTAILQ_FOREACH(re, &reset_handlers, entry) {
1196 if (re->func == func && re->opaque == opaque) {
1197 QTAILQ_REMOVE(&reset_handlers, re, entry);
1198 qemu_free(re);
1199 return;
1204 void qemu_system_reset(void)
1206 QEMUResetEntry *re, *nre;
1208 /* reset all devices */
1209 QTAILQ_FOREACH_SAFE(re, &reset_handlers, entry, nre) {
1210 re->func(re->opaque);
1212 monitor_protocol_event(QEVENT_RESET, NULL);
1213 cpu_synchronize_all_post_reset();
1216 void qemu_system_reset_request(void)
1218 if (no_reboot) {
1219 shutdown_requested = 1;
1220 } else {
1221 reset_requested = 1;
1223 if (cpu_single_env) {
1224 cpu_single_env->stopped = 1;
1225 cpu_exit(cpu_single_env);
1227 qemu_notify_event();
1230 void qemu_system_shutdown_request(void)
1232 shutdown_requested = 1;
1233 qemu_notify_event();
1236 void qemu_system_powerdown_request(void)
1238 powerdown_requested = 1;
1239 qemu_notify_event();
1242 void main_loop_wait(int nonblocking)
1244 IOHandlerRecord *ioh;
1245 fd_set rfds, wfds, xfds;
1246 int ret, nfds;
1247 struct timeval tv;
1248 int timeout;
1250 if (nonblocking)
1251 timeout = 0;
1252 else {
1253 timeout = qemu_calculate_timeout();
1254 qemu_bh_update_timeout(&timeout);
1257 os_host_main_loop_wait(&timeout);
1259 /* poll any events */
1260 /* XXX: separate device handlers from system ones */
1261 nfds = -1;
1262 FD_ZERO(&rfds);
1263 FD_ZERO(&wfds);
1264 FD_ZERO(&xfds);
1265 QLIST_FOREACH(ioh, &io_handlers, next) {
1266 if (ioh->deleted)
1267 continue;
1268 if (ioh->fd_read &&
1269 (!ioh->fd_read_poll ||
1270 ioh->fd_read_poll(ioh->opaque) != 0)) {
1271 FD_SET(ioh->fd, &rfds);
1272 if (ioh->fd > nfds)
1273 nfds = ioh->fd;
1275 if (ioh->fd_write) {
1276 FD_SET(ioh->fd, &wfds);
1277 if (ioh->fd > nfds)
1278 nfds = ioh->fd;
1282 tv.tv_sec = timeout / 1000;
1283 tv.tv_usec = (timeout % 1000) * 1000;
1285 slirp_select_fill(&nfds, &rfds, &wfds, &xfds);
1287 qemu_mutex_unlock_iothread();
1288 ret = select(nfds + 1, &rfds, &wfds, &xfds, &tv);
1289 qemu_mutex_lock_iothread();
1290 if (ret > 0) {
1291 IOHandlerRecord *pioh;
1293 QLIST_FOREACH_SAFE(ioh, &io_handlers, next, pioh) {
1294 if (ioh->deleted) {
1295 QLIST_REMOVE(ioh, next);
1296 qemu_free(ioh);
1297 continue;
1299 if (ioh->fd_read && FD_ISSET(ioh->fd, &rfds)) {
1300 ioh->fd_read(ioh->opaque);
1301 if (!(ioh->fd_read_poll && ioh->fd_read_poll(ioh->opaque)))
1302 FD_CLR(ioh->fd, &rfds);
1304 if (ioh->fd_write && FD_ISSET(ioh->fd, &wfds)) {
1305 ioh->fd_write(ioh->opaque);
1310 slirp_select_poll(&rfds, &wfds, &xfds, (ret < 0));
1312 qemu_run_all_timers();
1314 /* Check bottom-halves last in case any of the earlier events triggered
1315 them. */
1316 qemu_bh_poll();
1320 static int vm_can_run(void)
1322 if (powerdown_requested)
1323 return 0;
1324 if (reset_requested)
1325 return 0;
1326 if (shutdown_requested)
1327 return 0;
1328 if (debug_requested)
1329 return 0;
1330 return 1;
1333 qemu_irq qemu_system_powerdown;
1335 static void main_loop(void)
1337 int r;
1339 if (kvm_enabled()) {
1340 kvm_main_loop();
1341 cpu_disable_ticks();
1342 return;
1345 qemu_main_loop_start();
1347 for (;;) {
1348 do {
1349 bool nonblocking = false;
1350 #ifdef CONFIG_PROFILER
1351 int64_t ti;
1352 #endif
1353 #ifndef CONFIG_IOTHREAD
1354 nonblocking = tcg_cpu_exec();
1355 #endif
1356 #ifdef CONFIG_PROFILER
1357 ti = profile_getclock();
1358 #endif
1359 main_loop_wait(nonblocking);
1360 #ifdef CONFIG_PROFILER
1361 dev_time += profile_getclock() - ti;
1362 #endif
1363 } while (vm_can_run());
1365 if ((r = qemu_debug_requested())) {
1366 vm_stop(r);
1368 if (qemu_shutdown_requested()) {
1369 monitor_protocol_event(QEVENT_SHUTDOWN, NULL);
1370 if (no_shutdown) {
1371 vm_stop(0);
1372 no_shutdown = 0;
1373 } else
1374 break;
1376 if (qemu_reset_requested()) {
1377 pause_all_vcpus();
1378 qemu_system_reset();
1379 resume_all_vcpus();
1381 if (qemu_powerdown_requested()) {
1382 monitor_protocol_event(QEVENT_POWERDOWN, NULL);
1383 qemu_irq_raise(qemu_system_powerdown);
1385 if ((r = qemu_vmstop_requested())) {
1386 vm_stop(r);
1389 bdrv_close_all();
1390 pause_all_vcpus();
1393 static void version(void)
1395 printf("QEMU emulator version " QEMU_VERSION QEMU_PKGVERSION ", Copyright (c) 2003-2008 Fabrice Bellard\n");
1398 static void help(int exitcode)
1400 const char *options_help =
1401 #define DEF(option, opt_arg, opt_enum, opt_help, arch_mask) \
1402 opt_help
1403 #define DEFHEADING(text) stringify(text) "\n"
1404 #include "qemu-options.def"
1405 #undef DEF
1406 #undef DEFHEADING
1407 #undef GEN_DOCS
1409 version();
1410 printf("usage: %s [options] [disk_image]\n"
1411 "\n"
1412 "'disk_image' is a raw hard disk image for IDE hard disk 0\n"
1413 "\n"
1414 "%s\n"
1415 "During emulation, the following keys are useful:\n"
1416 "ctrl-alt-f toggle full screen\n"
1417 "ctrl-alt-n switch to virtual console 'n'\n"
1418 "ctrl-alt toggle mouse and keyboard grab\n"
1419 "\n"
1420 "When using -nographic, press 'ctrl-a h' to get some help.\n",
1421 "qemu",
1422 options_help);
1423 exit(exitcode);
1426 #define HAS_ARG 0x0001
1428 typedef struct QEMUOption {
1429 const char *name;
1430 int flags;
1431 int index;
1432 uint32_t arch_mask;
1433 } QEMUOption;
1435 static const QEMUOption qemu_options[] = {
1436 { "h", 0, QEMU_OPTION_h, QEMU_ARCH_ALL },
1437 #define DEF(option, opt_arg, opt_enum, opt_help, arch_mask) \
1438 { option, opt_arg, opt_enum, arch_mask },
1439 #define DEFHEADING(text)
1440 #include "qemu-options.def"
1441 #undef DEF
1442 #undef DEFHEADING
1443 #undef GEN_DOCS
1444 { NULL },
1446 static void select_vgahw (const char *p)
1448 const char *opts;
1450 default_vga = 0;
1451 vga_interface_type = VGA_NONE;
1452 if (strstart(p, "std", &opts)) {
1453 vga_interface_type = VGA_STD;
1454 } else if (strstart(p, "cirrus", &opts)) {
1455 vga_interface_type = VGA_CIRRUS;
1456 } else if (strstart(p, "vmware", &opts)) {
1457 vga_interface_type = VGA_VMWARE;
1458 } else if (strstart(p, "xenfb", &opts)) {
1459 vga_interface_type = VGA_XENFB;
1460 } else if (!strstart(p, "none", &opts)) {
1461 invalid_vga:
1462 fprintf(stderr, "Unknown vga type: %s\n", p);
1463 exit(1);
1465 while (*opts) {
1466 const char *nextopt;
1468 if (strstart(opts, ",retrace=", &nextopt)) {
1469 opts = nextopt;
1470 if (strstart(opts, "dumb", &nextopt))
1471 vga_retrace_method = VGA_RETRACE_DUMB;
1472 else if (strstart(opts, "precise", &nextopt))
1473 vga_retrace_method = VGA_RETRACE_PRECISE;
1474 else goto invalid_vga;
1475 } else goto invalid_vga;
1476 opts = nextopt;
1480 static int balloon_parse(const char *arg)
1482 QemuOpts *opts;
1484 if (strcmp(arg, "none") == 0) {
1485 return 0;
1488 if (!strncmp(arg, "virtio", 6)) {
1489 if (arg[6] == ',') {
1490 /* have params -> parse them */
1491 opts = qemu_opts_parse(&qemu_device_opts, arg+7, 0);
1492 if (!opts)
1493 return -1;
1494 } else {
1495 /* create empty opts */
1496 opts = qemu_opts_create(&qemu_device_opts, NULL, 0);
1498 qemu_opt_set(opts, "driver", "virtio-balloon-pci");
1499 return 0;
1502 return -1;
1505 char *qemu_find_file(int type, const char *name)
1507 int len;
1508 const char *subdir;
1509 char *buf;
1511 /* If name contains path separators then try it as a straight path. */
1512 if ((strchr(name, '/') || strchr(name, '\\'))
1513 && access(name, R_OK) == 0) {
1514 return qemu_strdup(name);
1516 switch (type) {
1517 case QEMU_FILE_TYPE_BIOS:
1518 subdir = "";
1519 break;
1520 case QEMU_FILE_TYPE_KEYMAP:
1521 subdir = "keymaps/";
1522 break;
1523 default:
1524 abort();
1526 len = strlen(data_dir) + strlen(name) + strlen(subdir) + 2;
1527 buf = qemu_mallocz(len);
1528 snprintf(buf, len, "%s/%s%s", data_dir, subdir, name);
1529 if (access(buf, R_OK)) {
1530 qemu_free(buf);
1531 return NULL;
1533 return buf;
1536 static int device_help_func(QemuOpts *opts, void *opaque)
1538 return qdev_device_help(opts);
1541 static int device_init_func(QemuOpts *opts, void *opaque)
1543 DeviceState *dev;
1545 dev = qdev_device_add(opts);
1546 if (!dev)
1547 return -1;
1548 return 0;
1551 static int chardev_init_func(QemuOpts *opts, void *opaque)
1553 CharDriverState *chr;
1555 chr = qemu_chr_open_opts(opts, NULL);
1556 if (!chr)
1557 return -1;
1558 return 0;
1561 #ifdef CONFIG_VIRTFS
1562 static int fsdev_init_func(QemuOpts *opts, void *opaque)
1564 int ret;
1565 ret = qemu_fsdev_add(opts);
1567 return ret;
1569 #endif
1571 static int mon_init_func(QemuOpts *opts, void *opaque)
1573 CharDriverState *chr;
1574 const char *chardev;
1575 const char *mode;
1576 int flags;
1578 mode = qemu_opt_get(opts, "mode");
1579 if (mode == NULL) {
1580 mode = "readline";
1582 if (strcmp(mode, "readline") == 0) {
1583 flags = MONITOR_USE_READLINE;
1584 } else if (strcmp(mode, "control") == 0) {
1585 flags = MONITOR_USE_CONTROL;
1586 } else {
1587 fprintf(stderr, "unknown monitor mode \"%s\"\n", mode);
1588 exit(1);
1591 if (qemu_opt_get_bool(opts, "default", 0))
1592 flags |= MONITOR_IS_DEFAULT;
1594 chardev = qemu_opt_get(opts, "chardev");
1595 chr = qemu_chr_find(chardev);
1596 if (chr == NULL) {
1597 fprintf(stderr, "chardev \"%s\" not found\n", chardev);
1598 exit(1);
1601 monitor_init(chr, flags);
1602 return 0;
1605 static void monitor_parse(const char *optarg, const char *mode)
1607 static int monitor_device_index = 0;
1608 QemuOpts *opts;
1609 const char *p;
1610 char label[32];
1611 int def = 0;
1613 if (strstart(optarg, "chardev:", &p)) {
1614 snprintf(label, sizeof(label), "%s", p);
1615 } else {
1616 snprintf(label, sizeof(label), "compat_monitor%d",
1617 monitor_device_index);
1618 if (monitor_device_index == 0) {
1619 def = 1;
1621 opts = qemu_chr_parse_compat(label, optarg);
1622 if (!opts) {
1623 fprintf(stderr, "parse error: %s\n", optarg);
1624 exit(1);
1628 opts = qemu_opts_create(&qemu_mon_opts, label, 1);
1629 if (!opts) {
1630 fprintf(stderr, "duplicate chardev: %s\n", label);
1631 exit(1);
1633 qemu_opt_set(opts, "mode", mode);
1634 qemu_opt_set(opts, "chardev", label);
1635 if (def)
1636 qemu_opt_set(opts, "default", "on");
1637 monitor_device_index++;
1640 struct device_config {
1641 enum {
1642 DEV_USB, /* -usbdevice */
1643 DEV_BT, /* -bt */
1644 DEV_SERIAL, /* -serial */
1645 DEV_PARALLEL, /* -parallel */
1646 DEV_VIRTCON, /* -virtioconsole */
1647 DEV_DEBUGCON, /* -debugcon */
1648 } type;
1649 const char *cmdline;
1650 QTAILQ_ENTRY(device_config) next;
1652 QTAILQ_HEAD(, device_config) device_configs = QTAILQ_HEAD_INITIALIZER(device_configs);
1654 static void add_device_config(int type, const char *cmdline)
1656 struct device_config *conf;
1658 conf = qemu_mallocz(sizeof(*conf));
1659 conf->type = type;
1660 conf->cmdline = cmdline;
1661 QTAILQ_INSERT_TAIL(&device_configs, conf, next);
1664 static int foreach_device_config(int type, int (*func)(const char *cmdline))
1666 struct device_config *conf;
1667 int rc;
1669 QTAILQ_FOREACH(conf, &device_configs, next) {
1670 if (conf->type != type)
1671 continue;
1672 rc = func(conf->cmdline);
1673 if (0 != rc)
1674 return rc;
1676 return 0;
1679 static int serial_parse(const char *devname)
1681 static int index = 0;
1682 char label[32];
1684 if (strcmp(devname, "none") == 0)
1685 return 0;
1686 if (index == MAX_SERIAL_PORTS) {
1687 fprintf(stderr, "qemu: too many serial ports\n");
1688 exit(1);
1690 snprintf(label, sizeof(label), "serial%d", index);
1691 serial_hds[index] = qemu_chr_open(label, devname, NULL);
1692 if (!serial_hds[index]) {
1693 fprintf(stderr, "qemu: could not open serial device '%s': %s\n",
1694 devname, strerror(errno));
1695 return -1;
1697 index++;
1698 return 0;
1701 static int parallel_parse(const char *devname)
1703 static int index = 0;
1704 char label[32];
1706 if (strcmp(devname, "none") == 0)
1707 return 0;
1708 if (index == MAX_PARALLEL_PORTS) {
1709 fprintf(stderr, "qemu: too many parallel ports\n");
1710 exit(1);
1712 snprintf(label, sizeof(label), "parallel%d", index);
1713 parallel_hds[index] = qemu_chr_open(label, devname, NULL);
1714 if (!parallel_hds[index]) {
1715 fprintf(stderr, "qemu: could not open parallel device '%s': %s\n",
1716 devname, strerror(errno));
1717 return -1;
1719 index++;
1720 return 0;
1723 static int virtcon_parse(const char *devname)
1725 static int index = 0;
1726 char label[32];
1727 QemuOpts *bus_opts, *dev_opts;
1729 if (strcmp(devname, "none") == 0)
1730 return 0;
1731 if (index == MAX_VIRTIO_CONSOLES) {
1732 fprintf(stderr, "qemu: too many virtio consoles\n");
1733 exit(1);
1736 bus_opts = qemu_opts_create(&qemu_device_opts, NULL, 0);
1737 qemu_opt_set(bus_opts, "driver", "virtio-serial");
1739 dev_opts = qemu_opts_create(&qemu_device_opts, NULL, 0);
1740 qemu_opt_set(dev_opts, "driver", "virtconsole");
1742 snprintf(label, sizeof(label), "virtcon%d", index);
1743 virtcon_hds[index] = qemu_chr_open(label, devname, NULL);
1744 if (!virtcon_hds[index]) {
1745 fprintf(stderr, "qemu: could not open virtio console '%s': %s\n",
1746 devname, strerror(errno));
1747 return -1;
1749 qemu_opt_set(dev_opts, "chardev", label);
1751 index++;
1752 return 0;
1755 static int debugcon_parse(const char *devname)
1757 QemuOpts *opts;
1759 if (!qemu_chr_open("debugcon", devname, NULL)) {
1760 exit(1);
1762 opts = qemu_opts_create(&qemu_device_opts, "debugcon", 1);
1763 if (!opts) {
1764 fprintf(stderr, "qemu: already have a debugcon device\n");
1765 exit(1);
1767 qemu_opt_set(opts, "driver", "isa-debugcon");
1768 qemu_opt_set(opts, "chardev", "debugcon");
1769 return 0;
1772 void qemu_add_exit_notifier(Notifier *notify)
1774 notifier_list_add(&exit_notifiers, notify);
1777 void qemu_remove_exit_notifier(Notifier *notify)
1779 notifier_list_remove(&exit_notifiers, notify);
1782 static void qemu_run_exit_notifiers(void)
1784 notifier_list_notify(&exit_notifiers);
1787 static const QEMUOption *lookup_opt(int argc, char **argv,
1788 const char **poptarg, int *poptind)
1790 const QEMUOption *popt;
1791 int optind = *poptind;
1792 char *r = argv[optind];
1793 const char *optarg;
1795 loc_set_cmdline(argv, optind, 1);
1796 optind++;
1797 /* Treat --foo the same as -foo. */
1798 if (r[1] == '-')
1799 r++;
1800 popt = qemu_options;
1801 for(;;) {
1802 if (!popt->name) {
1803 error_report("invalid option");
1804 exit(1);
1806 if (!strcmp(popt->name, r + 1))
1807 break;
1808 popt++;
1810 if (popt->flags & HAS_ARG) {
1811 if (optind >= argc) {
1812 error_report("requires an argument");
1813 exit(1);
1815 optarg = argv[optind++];
1816 loc_set_cmdline(argv, optind - 2, 2);
1817 } else {
1818 optarg = NULL;
1821 *poptarg = optarg;
1822 *poptind = optind;
1824 return popt;
1827 int main(int argc, char **argv, char **envp)
1829 const char *gdbstub_dev = NULL;
1830 int i;
1831 int snapshot, linux_boot;
1832 const char *icount_option = NULL;
1833 const char *initrd_filename;
1834 const char *kernel_filename, *kernel_cmdline;
1835 char boot_devices[33] = "cad"; /* default to HD->floppy->CD-ROM */
1836 DisplayState *ds;
1837 DisplayChangeListener *dcl;
1838 int cyls, heads, secs, translation;
1839 QemuOpts *hda_opts = NULL, *opts;
1840 int optind;
1841 const char *optarg;
1842 const char *loadvm = NULL;
1843 QEMUMachine *machine;
1844 const char *cpu_model;
1845 int tb_size;
1846 const char *pid_file = NULL;
1847 const char *incoming = NULL;
1848 int show_vnc_port = 0;
1849 int defconfig = 1;
1851 atexit(qemu_run_exit_notifiers);
1852 error_set_progname(argv[0]);
1854 init_clocks();
1856 qemu_cache_utils_init(envp);
1858 QLIST_INIT (&vm_change_state_head);
1859 os_setup_early_signal_handling();
1861 module_call_init(MODULE_INIT_MACHINE);
1862 machine = find_default_machine();
1863 cpu_model = NULL;
1864 initrd_filename = NULL;
1865 ram_size = 0;
1866 snapshot = 0;
1867 kernel_filename = NULL;
1868 kernel_cmdline = "";
1869 cyls = heads = secs = 0;
1870 translation = BIOS_ATA_TRANSLATION_AUTO;
1872 for (i = 0; i < MAX_NODES; i++) {
1873 node_mem[i] = 0;
1874 node_cpumask[i] = 0;
1877 assigned_devices_index = 0;
1879 nb_numa_nodes = 0;
1880 nb_nics = 0;
1882 tb_size = 0;
1883 autostart= 1;
1885 /* first pass of option parsing */
1886 optind = 1;
1887 while (optind < argc) {
1888 if (argv[optind][0] != '-') {
1889 /* disk image */
1890 optind++;
1891 continue;
1892 } else {
1893 const QEMUOption *popt;
1895 popt = lookup_opt(argc, argv, &optarg, &optind);
1896 switch (popt->index) {
1897 case QEMU_OPTION_nodefconfig:
1898 defconfig=0;
1899 break;
1904 if (defconfig) {
1905 int ret;
1907 ret = qemu_read_config_file(CONFIG_QEMU_CONFDIR "/qemu.conf");
1908 if (ret < 0 && ret != -ENOENT) {
1909 exit(1);
1912 ret = qemu_read_config_file(arch_config_name);
1913 if (ret < 0 && ret != -ENOENT) {
1914 exit(1);
1917 cpudef_init();
1919 /* second pass of option parsing */
1920 optind = 1;
1921 for(;;) {
1922 if (optind >= argc)
1923 break;
1924 if (argv[optind][0] != '-') {
1925 hda_opts = drive_add(argv[optind++], HD_ALIAS, 0);
1926 } else {
1927 const QEMUOption *popt;
1929 popt = lookup_opt(argc, argv, &optarg, &optind);
1930 if (!(popt->arch_mask & arch_type)) {
1931 printf("Option %s not supported for this target\n", popt->name);
1932 exit(1);
1934 switch(popt->index) {
1935 case QEMU_OPTION_M:
1936 machine = find_machine(optarg);
1937 if (!machine) {
1938 QEMUMachine *m;
1939 printf("Supported machines are:\n");
1940 for(m = first_machine; m != NULL; m = m->next) {
1941 if (m->alias)
1942 printf("%-10s %s (alias of %s)\n",
1943 m->alias, m->desc, m->name);
1944 printf("%-10s %s%s\n",
1945 m->name, m->desc,
1946 m->is_default ? " (default)" : "");
1948 exit(*optarg != '?');
1950 break;
1951 case QEMU_OPTION_cpu:
1952 /* hw initialization will check this */
1953 if (*optarg == '?') {
1954 list_cpus(stdout, &fprintf, optarg);
1955 exit(0);
1956 } else {
1957 cpu_model = optarg;
1959 break;
1960 case QEMU_OPTION_initrd:
1961 initrd_filename = optarg;
1962 break;
1963 case QEMU_OPTION_hda:
1964 if (cyls == 0)
1965 hda_opts = drive_add(optarg, HD_ALIAS, 0);
1966 else
1967 hda_opts = drive_add(optarg, HD_ALIAS
1968 ",cyls=%d,heads=%d,secs=%d%s",
1969 0, cyls, heads, secs,
1970 translation == BIOS_ATA_TRANSLATION_LBA ?
1971 ",trans=lba" :
1972 translation == BIOS_ATA_TRANSLATION_NONE ?
1973 ",trans=none" : "");
1974 break;
1975 case QEMU_OPTION_hdb:
1976 case QEMU_OPTION_hdc:
1977 case QEMU_OPTION_hdd:
1978 drive_add(optarg, HD_ALIAS, popt->index - QEMU_OPTION_hda);
1979 break;
1980 case QEMU_OPTION_drive:
1981 drive_add(NULL, "%s", optarg);
1982 break;
1983 case QEMU_OPTION_set:
1984 if (qemu_set_option(optarg) != 0)
1985 exit(1);
1986 break;
1987 case QEMU_OPTION_global:
1988 if (qemu_global_option(optarg) != 0)
1989 exit(1);
1990 break;
1991 case QEMU_OPTION_mtdblock:
1992 drive_add(optarg, MTD_ALIAS);
1993 break;
1994 case QEMU_OPTION_sd:
1995 drive_add(optarg, SD_ALIAS);
1996 break;
1997 case QEMU_OPTION_pflash:
1998 drive_add(optarg, PFLASH_ALIAS);
1999 break;
2000 case QEMU_OPTION_snapshot:
2001 snapshot = 1;
2002 break;
2003 case QEMU_OPTION_hdachs:
2005 const char *p;
2006 p = optarg;
2007 cyls = strtol(p, (char **)&p, 0);
2008 if (cyls < 1 || cyls > 16383)
2009 goto chs_fail;
2010 if (*p != ',')
2011 goto chs_fail;
2012 p++;
2013 heads = strtol(p, (char **)&p, 0);
2014 if (heads < 1 || heads > 16)
2015 goto chs_fail;
2016 if (*p != ',')
2017 goto chs_fail;
2018 p++;
2019 secs = strtol(p, (char **)&p, 0);
2020 if (secs < 1 || secs > 63)
2021 goto chs_fail;
2022 if (*p == ',') {
2023 p++;
2024 if (!strcmp(p, "none"))
2025 translation = BIOS_ATA_TRANSLATION_NONE;
2026 else if (!strcmp(p, "lba"))
2027 translation = BIOS_ATA_TRANSLATION_LBA;
2028 else if (!strcmp(p, "auto"))
2029 translation = BIOS_ATA_TRANSLATION_AUTO;
2030 else
2031 goto chs_fail;
2032 } else if (*p != '\0') {
2033 chs_fail:
2034 fprintf(stderr, "qemu: invalid physical CHS format\n");
2035 exit(1);
2037 if (hda_opts != NULL) {
2038 char num[16];
2039 snprintf(num, sizeof(num), "%d", cyls);
2040 qemu_opt_set(hda_opts, "cyls", num);
2041 snprintf(num, sizeof(num), "%d", heads);
2042 qemu_opt_set(hda_opts, "heads", num);
2043 snprintf(num, sizeof(num), "%d", secs);
2044 qemu_opt_set(hda_opts, "secs", num);
2045 if (translation == BIOS_ATA_TRANSLATION_LBA)
2046 qemu_opt_set(hda_opts, "trans", "lba");
2047 if (translation == BIOS_ATA_TRANSLATION_NONE)
2048 qemu_opt_set(hda_opts, "trans", "none");
2051 break;
2052 case QEMU_OPTION_numa:
2053 if (nb_numa_nodes >= MAX_NODES) {
2054 fprintf(stderr, "qemu: too many NUMA nodes\n");
2055 exit(1);
2057 numa_add(optarg);
2058 break;
2059 case QEMU_OPTION_nographic:
2060 display_type = DT_NOGRAPHIC;
2061 break;
2062 #ifdef CONFIG_CURSES
2063 case QEMU_OPTION_curses:
2064 display_type = DT_CURSES;
2065 break;
2066 #endif
2067 case QEMU_OPTION_portrait:
2068 graphic_rotate = 1;
2069 break;
2070 case QEMU_OPTION_kernel:
2071 kernel_filename = optarg;
2072 break;
2073 case QEMU_OPTION_append:
2074 kernel_cmdline = optarg;
2075 break;
2076 case QEMU_OPTION_cdrom:
2077 drive_add(optarg, CDROM_ALIAS);
2078 break;
2079 case QEMU_OPTION_boot:
2081 static const char * const params[] = {
2082 "order", "once", "menu", NULL
2084 char buf[sizeof(boot_devices)];
2085 char *standard_boot_devices;
2086 int legacy = 0;
2088 if (!strchr(optarg, '=')) {
2089 legacy = 1;
2090 pstrcpy(buf, sizeof(buf), optarg);
2091 } else if (check_params(buf, sizeof(buf), params, optarg) < 0) {
2092 fprintf(stderr,
2093 "qemu: unknown boot parameter '%s' in '%s'\n",
2094 buf, optarg);
2095 exit(1);
2098 if (legacy ||
2099 get_param_value(buf, sizeof(buf), "order", optarg)) {
2100 validate_bootdevices(buf);
2101 pstrcpy(boot_devices, sizeof(boot_devices), buf);
2103 if (!legacy) {
2104 if (get_param_value(buf, sizeof(buf),
2105 "once", optarg)) {
2106 validate_bootdevices(buf);
2107 standard_boot_devices = qemu_strdup(boot_devices);
2108 pstrcpy(boot_devices, sizeof(boot_devices), buf);
2109 qemu_register_reset(restore_boot_devices,
2110 standard_boot_devices);
2112 if (get_param_value(buf, sizeof(buf),
2113 "menu", optarg)) {
2114 if (!strcmp(buf, "on")) {
2115 boot_menu = 1;
2116 } else if (!strcmp(buf, "off")) {
2117 boot_menu = 0;
2118 } else {
2119 fprintf(stderr,
2120 "qemu: invalid option value '%s'\n",
2121 buf);
2122 exit(1);
2127 break;
2128 case QEMU_OPTION_fda:
2129 case QEMU_OPTION_fdb:
2130 drive_add(optarg, FD_ALIAS, popt->index - QEMU_OPTION_fda);
2131 break;
2132 case QEMU_OPTION_no_fd_bootchk:
2133 fd_bootchk = 0;
2134 break;
2135 case QEMU_OPTION_netdev:
2136 if (net_client_parse(&qemu_netdev_opts, optarg) == -1) {
2137 exit(1);
2139 break;
2140 case QEMU_OPTION_net:
2141 if (net_client_parse(&qemu_net_opts, optarg) == -1) {
2142 exit(1);
2144 break;
2145 #ifdef CONFIG_SLIRP
2146 case QEMU_OPTION_tftp:
2147 legacy_tftp_prefix = optarg;
2148 break;
2149 case QEMU_OPTION_bootp:
2150 legacy_bootp_filename = optarg;
2151 break;
2152 case QEMU_OPTION_redir:
2153 if (net_slirp_redir(optarg) < 0)
2154 exit(1);
2155 break;
2156 #endif
2157 case QEMU_OPTION_bt:
2158 add_device_config(DEV_BT, optarg);
2159 break;
2160 case QEMU_OPTION_audio_help:
2161 if (!(audio_available())) {
2162 printf("Option %s not supported for this target\n", popt->name);
2163 exit(1);
2165 AUD_help ();
2166 exit (0);
2167 break;
2168 case QEMU_OPTION_soundhw:
2169 if (!(audio_available())) {
2170 printf("Option %s not supported for this target\n", popt->name);
2171 exit(1);
2173 select_soundhw (optarg);
2174 break;
2175 case QEMU_OPTION_h:
2176 help(0);
2177 break;
2178 case QEMU_OPTION_version:
2179 version();
2180 exit(0);
2181 break;
2182 case QEMU_OPTION_m: {
2183 uint64_t value;
2184 char *ptr;
2186 value = strtoul(optarg, &ptr, 10);
2187 switch (*ptr) {
2188 case 0: case 'M': case 'm':
2189 value <<= 20;
2190 break;
2191 case 'G': case 'g':
2192 value <<= 30;
2193 break;
2194 default:
2195 fprintf(stderr, "qemu: invalid ram size: %s\n", optarg);
2196 exit(1);
2199 /* On 32-bit hosts, QEMU is limited by virtual address space */
2200 if (value > (2047 << 20) && HOST_LONG_BITS == 32) {
2201 fprintf(stderr, "qemu: at most 2047 MB RAM can be simulated\n");
2202 exit(1);
2204 if (value != (uint64_t)(ram_addr_t)value) {
2205 fprintf(stderr, "qemu: ram size too large\n");
2206 exit(1);
2208 ram_size = value;
2209 break;
2211 case QEMU_OPTION_mempath:
2212 mem_path = optarg;
2213 break;
2214 #ifdef MAP_POPULATE
2215 case QEMU_OPTION_mem_prealloc:
2216 mem_prealloc = 1;
2217 break;
2218 #endif
2219 case QEMU_OPTION_d:
2220 set_cpu_log(optarg);
2221 break;
2222 case QEMU_OPTION_s:
2223 gdbstub_dev = "tcp::" DEFAULT_GDBSTUB_PORT;
2224 break;
2225 case QEMU_OPTION_gdb:
2226 gdbstub_dev = optarg;
2227 break;
2228 case QEMU_OPTION_L:
2229 data_dir = optarg;
2230 break;
2231 case QEMU_OPTION_bios:
2232 bios_name = optarg;
2233 break;
2234 case QEMU_OPTION_singlestep:
2235 singlestep = 1;
2236 break;
2237 case QEMU_OPTION_S:
2238 autostart = 0;
2239 break;
2240 case QEMU_OPTION_k:
2241 keyboard_layout = optarg;
2242 break;
2243 case QEMU_OPTION_localtime:
2244 rtc_utc = 0;
2245 break;
2246 case QEMU_OPTION_vga:
2247 select_vgahw (optarg);
2248 break;
2249 case QEMU_OPTION_g:
2251 const char *p;
2252 int w, h, depth;
2253 p = optarg;
2254 w = strtol(p, (char **)&p, 10);
2255 if (w <= 0) {
2256 graphic_error:
2257 fprintf(stderr, "qemu: invalid resolution or depth\n");
2258 exit(1);
2260 if (*p != 'x')
2261 goto graphic_error;
2262 p++;
2263 h = strtol(p, (char **)&p, 10);
2264 if (h <= 0)
2265 goto graphic_error;
2266 if (*p == 'x') {
2267 p++;
2268 depth = strtol(p, (char **)&p, 10);
2269 if (depth != 8 && depth != 15 && depth != 16 &&
2270 depth != 24 && depth != 32)
2271 goto graphic_error;
2272 } else if (*p == '\0') {
2273 depth = graphic_depth;
2274 } else {
2275 goto graphic_error;
2278 graphic_width = w;
2279 graphic_height = h;
2280 graphic_depth = depth;
2282 break;
2283 case QEMU_OPTION_echr:
2285 char *r;
2286 term_escape_char = strtol(optarg, &r, 0);
2287 if (r == optarg)
2288 printf("Bad argument to echr\n");
2289 break;
2291 case QEMU_OPTION_monitor:
2292 monitor_parse(optarg, "readline");
2293 default_monitor = 0;
2294 break;
2295 case QEMU_OPTION_qmp:
2296 monitor_parse(optarg, "control");
2297 default_monitor = 0;
2298 break;
2299 case QEMU_OPTION_mon:
2300 opts = qemu_opts_parse(&qemu_mon_opts, optarg, 1);
2301 if (!opts) {
2302 exit(1);
2304 default_monitor = 0;
2305 break;
2306 case QEMU_OPTION_chardev:
2307 opts = qemu_opts_parse(&qemu_chardev_opts, optarg, 1);
2308 if (!opts) {
2309 exit(1);
2311 break;
2312 #ifdef CONFIG_VIRTFS
2313 case QEMU_OPTION_fsdev:
2314 opts = qemu_opts_parse(&qemu_fsdev_opts, optarg, 1);
2315 if (!opts) {
2316 fprintf(stderr, "parse error: %s\n", optarg);
2317 exit(1);
2319 break;
2320 case QEMU_OPTION_virtfs: {
2321 char *arg_fsdev = NULL;
2322 char *arg_9p = NULL;
2323 int len = 0;
2325 opts = qemu_opts_parse(&qemu_virtfs_opts, optarg, 1);
2326 if (!opts) {
2327 fprintf(stderr, "parse error: %s\n", optarg);
2328 exit(1);
2331 if (qemu_opt_get(opts, "fstype") == NULL ||
2332 qemu_opt_get(opts, "mount_tag") == NULL ||
2333 qemu_opt_get(opts, "path") == NULL ||
2334 qemu_opt_get(opts, "security_model") == NULL) {
2335 fprintf(stderr, "Usage: -virtfs fstype,path=/share_path/,"
2336 "security_model=[mapped|passthrough],"
2337 "mnt_tag=tag.\n");
2338 exit(1);
2341 len = strlen(",id=,path=,security_model=");
2342 len += strlen(qemu_opt_get(opts, "fstype"));
2343 len += strlen(qemu_opt_get(opts, "mount_tag"));
2344 len += strlen(qemu_opt_get(opts, "path"));
2345 len += strlen(qemu_opt_get(opts, "security_model"));
2346 arg_fsdev = qemu_malloc((len + 1) * sizeof(*arg_fsdev));
2348 if (!arg_fsdev) {
2349 fprintf(stderr, "No memory to parse -fsdev for %s\n",
2350 optarg);
2351 exit(1);
2354 sprintf(arg_fsdev, "%s,id=%s,path=%s,security_model=%s",
2355 qemu_opt_get(opts, "fstype"),
2356 qemu_opt_get(opts, "mount_tag"),
2357 qemu_opt_get(opts, "path"),
2358 qemu_opt_get(opts, "security_model"));
2360 len = strlen("virtio-9p-pci,fsdev=,mount_tag=");
2361 len += 2*strlen(qemu_opt_get(opts, "mount_tag"));
2362 arg_9p = qemu_malloc((len + 1) * sizeof(*arg_9p));
2364 if (!arg_9p) {
2365 fprintf(stderr, "No memory to parse -device for %s\n",
2366 optarg);
2367 exit(1);
2370 sprintf(arg_9p, "virtio-9p-pci,fsdev=%s,mount_tag=%s",
2371 qemu_opt_get(opts, "mount_tag"),
2372 qemu_opt_get(opts, "mount_tag"));
2374 if (!qemu_opts_parse(&qemu_fsdev_opts, arg_fsdev, 1)) {
2375 fprintf(stderr, "parse error [fsdev]: %s\n", optarg);
2376 exit(1);
2379 if (!qemu_opts_parse(&qemu_device_opts, arg_9p, 1)) {
2380 fprintf(stderr, "parse error [device]: %s\n", optarg);
2381 exit(1);
2384 qemu_free(arg_fsdev);
2385 qemu_free(arg_9p);
2386 break;
2388 #endif
2389 case QEMU_OPTION_serial:
2390 add_device_config(DEV_SERIAL, optarg);
2391 default_serial = 0;
2392 if (strncmp(optarg, "mon:", 4) == 0) {
2393 default_monitor = 0;
2395 break;
2396 case QEMU_OPTION_watchdog:
2397 if (watchdog) {
2398 fprintf(stderr,
2399 "qemu: only one watchdog option may be given\n");
2400 return 1;
2402 watchdog = optarg;
2403 break;
2404 case QEMU_OPTION_watchdog_action:
2405 if (select_watchdog_action(optarg) == -1) {
2406 fprintf(stderr, "Unknown -watchdog-action parameter\n");
2407 exit(1);
2409 break;
2410 case QEMU_OPTION_virtiocon:
2411 add_device_config(DEV_VIRTCON, optarg);
2412 default_virtcon = 0;
2413 if (strncmp(optarg, "mon:", 4) == 0) {
2414 default_monitor = 0;
2416 break;
2417 case QEMU_OPTION_parallel:
2418 add_device_config(DEV_PARALLEL, optarg);
2419 default_parallel = 0;
2420 if (strncmp(optarg, "mon:", 4) == 0) {
2421 default_monitor = 0;
2423 break;
2424 case QEMU_OPTION_debugcon:
2425 add_device_config(DEV_DEBUGCON, optarg);
2426 break;
2427 case QEMU_OPTION_loadvm:
2428 loadvm = optarg;
2429 break;
2430 case QEMU_OPTION_full_screen:
2431 full_screen = 1;
2432 break;
2433 #ifdef CONFIG_SDL
2434 case QEMU_OPTION_no_frame:
2435 no_frame = 1;
2436 break;
2437 case QEMU_OPTION_alt_grab:
2438 alt_grab = 1;
2439 break;
2440 case QEMU_OPTION_ctrl_grab:
2441 ctrl_grab = 1;
2442 break;
2443 case QEMU_OPTION_no_quit:
2444 no_quit = 1;
2445 break;
2446 case QEMU_OPTION_sdl:
2447 display_type = DT_SDL;
2448 break;
2449 #endif
2450 case QEMU_OPTION_pidfile:
2451 pid_file = optarg;
2452 break;
2453 case QEMU_OPTION_win2k_hack:
2454 win2k_install_hack = 1;
2455 break;
2456 case QEMU_OPTION_rtc_td_hack:
2457 rtc_td_hack = 1;
2458 break;
2459 case QEMU_OPTION_acpitable:
2460 do_acpitable_option(optarg);
2461 break;
2462 case QEMU_OPTION_smbios:
2463 do_smbios_option(optarg);
2464 break;
2465 #ifdef KVM_UPSTREAM
2466 case QEMU_OPTION_enable_kvm:
2467 kvm_allowed = 1;
2468 #endif
2469 break;
2470 case QEMU_OPTION_no_kvm:
2471 kvm_allowed = 0;
2472 break;
2473 #ifdef CONFIG_KVM
2474 case QEMU_OPTION_no_kvm_irqchip: {
2475 kvm_irqchip = 0;
2476 kvm_pit = 0;
2477 break;
2479 case QEMU_OPTION_no_kvm_pit: {
2480 kvm_pit = 0;
2481 break;
2483 case QEMU_OPTION_no_kvm_pit_reinjection: {
2484 kvm_pit_reinject = 0;
2485 break;
2487 case QEMU_OPTION_enable_nesting: {
2488 kvm_nested = 1;
2489 break;
2491 #endif
2492 #if defined(TARGET_I386) || defined(TARGET_X86_64) || defined(TARGET_IA64) || defined(__linux__)
2493 case QEMU_OPTION_pcidevice:
2494 if (assigned_devices_index >= MAX_DEV_ASSIGN_CMDLINE) {
2495 fprintf(stderr, "Too many assigned devices\n");
2496 exit(1);
2498 assigned_devices[assigned_devices_index] = optarg;
2499 assigned_devices_index++;
2500 break;
2501 #endif
2502 case QEMU_OPTION_usb:
2503 usb_enabled = 1;
2504 break;
2505 case QEMU_OPTION_usbdevice:
2506 usb_enabled = 1;
2507 add_device_config(DEV_USB, optarg);
2508 break;
2509 case QEMU_OPTION_device:
2510 if (!qemu_opts_parse(&qemu_device_opts, optarg, 1)) {
2511 exit(1);
2513 break;
2514 case QEMU_OPTION_smp:
2515 smp_parse(optarg);
2516 if (smp_cpus < 1) {
2517 fprintf(stderr, "Invalid number of CPUs\n");
2518 exit(1);
2520 if (max_cpus < smp_cpus) {
2521 fprintf(stderr, "maxcpus must be equal to or greater than "
2522 "smp\n");
2523 exit(1);
2525 if (max_cpus > 255) {
2526 fprintf(stderr, "Unsupported number of maxcpus\n");
2527 exit(1);
2529 break;
2530 case QEMU_OPTION_vnc:
2531 display_type = DT_VNC;
2532 vnc_display = optarg;
2533 break;
2534 case QEMU_OPTION_no_acpi:
2535 acpi_enabled = 0;
2536 break;
2537 case QEMU_OPTION_no_hpet:
2538 no_hpet = 1;
2539 break;
2540 case QEMU_OPTION_balloon:
2541 if (balloon_parse(optarg) < 0) {
2542 fprintf(stderr, "Unknown -balloon argument %s\n", optarg);
2543 exit(1);
2545 break;
2546 case QEMU_OPTION_no_reboot:
2547 no_reboot = 1;
2548 break;
2549 case QEMU_OPTION_no_shutdown:
2550 no_shutdown = 1;
2551 break;
2552 case QEMU_OPTION_show_cursor:
2553 cursor_hide = 0;
2554 break;
2555 case QEMU_OPTION_uuid:
2556 if(qemu_uuid_parse(optarg, qemu_uuid) < 0) {
2557 fprintf(stderr, "Fail to parse UUID string."
2558 " Wrong format.\n");
2559 exit(1);
2561 break;
2562 case QEMU_OPTION_option_rom:
2563 if (nb_option_roms >= MAX_OPTION_ROMS) {
2564 fprintf(stderr, "Too many option ROMs\n");
2565 exit(1);
2567 option_rom[nb_option_roms] = optarg;
2568 nb_option_roms++;
2569 break;
2570 case QEMU_OPTION_semihosting:
2571 semihosting_enabled = 1;
2572 break;
2573 case QEMU_OPTION_tdf:
2574 time_drift_fix = 1;
2575 break;
2576 case QEMU_OPTION_kvm_shadow_memory:
2577 kvm_shadow_memory = (int64_t)atoi(optarg) * 1024 * 1024 / 4096;
2578 break;
2579 case QEMU_OPTION_name:
2580 qemu_name = qemu_strdup(optarg);
2582 char *p = strchr(qemu_name, ',');
2583 if (p != NULL) {
2584 *p++ = 0;
2585 if (strncmp(p, "process=", 8)) {
2586 fprintf(stderr, "Unknown subargument %s to -name", p);
2587 exit(1);
2589 p += 8;
2590 os_set_proc_name(p);
2593 break;
2594 case QEMU_OPTION_prom_env:
2595 if (nb_prom_envs >= MAX_PROM_ENVS) {
2596 fprintf(stderr, "Too many prom variables\n");
2597 exit(1);
2599 prom_envs[nb_prom_envs] = optarg;
2600 nb_prom_envs++;
2601 break;
2602 case QEMU_OPTION_old_param:
2603 old_param = 1;
2604 break;
2605 case QEMU_OPTION_clock:
2606 configure_alarms(optarg);
2607 break;
2608 case QEMU_OPTION_startdate:
2609 configure_rtc_date_offset(optarg, 1);
2610 break;
2611 case QEMU_OPTION_rtc:
2612 opts = qemu_opts_parse(&qemu_rtc_opts, optarg, 0);
2613 if (!opts) {
2614 exit(1);
2616 configure_rtc(opts);
2617 break;
2618 case QEMU_OPTION_tb_size:
2619 tb_size = strtol(optarg, NULL, 0);
2620 if (tb_size < 0)
2621 tb_size = 0;
2622 break;
2623 case QEMU_OPTION_icount:
2624 icount_option = optarg;
2625 break;
2626 case QEMU_OPTION_incoming:
2627 incoming = optarg;
2628 break;
2629 case QEMU_OPTION_nodefaults:
2630 default_serial = 0;
2631 default_parallel = 0;
2632 default_virtcon = 0;
2633 default_monitor = 0;
2634 default_vga = 0;
2635 default_net = 0;
2636 default_floppy = 0;
2637 default_cdrom = 0;
2638 default_sdcard = 0;
2639 break;
2640 #ifndef _WIN32
2641 case QEMU_OPTION_nvram:
2642 nvram = optarg;
2643 break;
2644 #endif
2645 case QEMU_OPTION_xen_domid:
2646 if (!(xen_available())) {
2647 printf("Option %s not supported for this target\n", popt->name);
2648 exit(1);
2650 xen_domid = atoi(optarg);
2651 break;
2652 case QEMU_OPTION_xen_create:
2653 if (!(xen_available())) {
2654 printf("Option %s not supported for this target\n", popt->name);
2655 exit(1);
2657 xen_mode = XEN_CREATE;
2658 break;
2659 case QEMU_OPTION_xen_attach:
2660 if (!(xen_available())) {
2661 printf("Option %s not supported for this target\n", popt->name);
2662 exit(1);
2664 xen_mode = XEN_ATTACH;
2665 break;
2666 case QEMU_OPTION_readconfig:
2668 int ret = qemu_read_config_file(optarg);
2669 if (ret < 0) {
2670 fprintf(stderr, "read config %s: %s\n", optarg,
2671 strerror(-ret));
2672 exit(1);
2674 break;
2676 case QEMU_OPTION_writeconfig:
2678 FILE *fp;
2679 if (strcmp(optarg, "-") == 0) {
2680 fp = stdout;
2681 } else {
2682 fp = fopen(optarg, "w");
2683 if (fp == NULL) {
2684 fprintf(stderr, "open %s: %s\n", optarg, strerror(errno));
2685 exit(1);
2688 qemu_config_write(fp);
2689 fclose(fp);
2690 break;
2692 default:
2693 os_parse_cmd_args(popt->index, optarg);
2697 loc_set_none();
2699 /* If no data_dir is specified then try to find it relative to the
2700 executable path. */
2701 if (!data_dir) {
2702 data_dir = os_find_datadir(argv[0]);
2704 /* If all else fails use the install patch specified when building. */
2705 if (!data_dir) {
2706 data_dir = CONFIG_QEMU_DATADIR;
2710 * Default to max_cpus = smp_cpus, in case the user doesn't
2711 * specify a max_cpus value.
2713 if (!max_cpus)
2714 max_cpus = smp_cpus;
2716 machine->max_cpus = machine->max_cpus ?: 1; /* Default to UP */
2717 if (smp_cpus > machine->max_cpus) {
2718 fprintf(stderr, "Number of SMP cpus requested (%d), exceeds max cpus "
2719 "supported by machine `%s' (%d)\n", smp_cpus, machine->name,
2720 machine->max_cpus);
2721 exit(1);
2724 qemu_opts_foreach(&qemu_device_opts, default_driver_check, NULL, 0);
2725 qemu_opts_foreach(&qemu_global_opts, default_driver_check, NULL, 0);
2727 if (machine->no_serial) {
2728 default_serial = 0;
2730 if (machine->no_parallel) {
2731 default_parallel = 0;
2733 if (!machine->use_virtcon) {
2734 default_virtcon = 0;
2736 if (machine->no_vga) {
2737 default_vga = 0;
2739 if (machine->no_floppy) {
2740 default_floppy = 0;
2742 if (machine->no_cdrom) {
2743 default_cdrom = 0;
2745 if (machine->no_sdcard) {
2746 default_sdcard = 0;
2749 if (display_type == DT_NOGRAPHIC) {
2750 if (default_parallel)
2751 add_device_config(DEV_PARALLEL, "null");
2752 if (default_serial && default_monitor) {
2753 add_device_config(DEV_SERIAL, "mon:stdio");
2754 } else if (default_virtcon && default_monitor) {
2755 add_device_config(DEV_VIRTCON, "mon:stdio");
2756 } else {
2757 if (default_serial)
2758 add_device_config(DEV_SERIAL, "stdio");
2759 if (default_virtcon)
2760 add_device_config(DEV_VIRTCON, "stdio");
2761 if (default_monitor)
2762 monitor_parse("stdio", "readline");
2764 } else {
2765 if (default_serial)
2766 add_device_config(DEV_SERIAL, "vc:80Cx24C");
2767 if (default_parallel)
2768 add_device_config(DEV_PARALLEL, "vc:80Cx24C");
2769 if (default_monitor)
2770 monitor_parse("vc:80Cx24C", "readline");
2771 if (default_virtcon)
2772 add_device_config(DEV_VIRTCON, "vc:80Cx24C");
2774 if (default_vga)
2775 vga_interface_type = VGA_CIRRUS;
2777 socket_init();
2779 if (qemu_opts_foreach(&qemu_chardev_opts, chardev_init_func, NULL, 1) != 0)
2780 exit(1);
2781 #ifdef CONFIG_VIRTFS
2782 if (qemu_opts_foreach(&qemu_fsdev_opts, fsdev_init_func, NULL, 1) != 0) {
2783 exit(1);
2785 #endif
2787 os_daemonize();
2789 if (pid_file && qemu_create_pidfile(pid_file) != 0) {
2790 os_pidfile_error();
2791 exit(1);
2794 if (kvm_allowed) {
2795 int ret = kvm_init(smp_cpus);
2796 if (ret < 0) {
2797 #if defined(KVM_UPSTREAM) || defined(CONFIG_NO_CPU_EMULATION)
2798 if (!kvm_available()) {
2799 printf("KVM not supported for this target\n");
2800 } else {
2801 fprintf(stderr, "failed to initialize KVM: %s\n", strerror(-ret));
2803 exit(1);
2804 #endif
2805 #ifdef CONFIG_KVM
2806 fprintf(stderr, "Could not initialize KVM, will disable KVM support\n");
2807 kvm_allowed = 0;
2808 #endif
2812 if (qemu_init_main_loop()) {
2813 fprintf(stderr, "qemu_init_main_loop failed\n");
2814 exit(1);
2816 linux_boot = (kernel_filename != NULL);
2818 if (!linux_boot && *kernel_cmdline != '\0') {
2819 fprintf(stderr, "-append only allowed with -kernel option\n");
2820 exit(1);
2823 if (!linux_boot && initrd_filename != NULL) {
2824 fprintf(stderr, "-initrd only allowed with -kernel option\n");
2825 exit(1);
2828 os_set_line_buffering();
2830 if (init_timer_alarm() < 0) {
2831 fprintf(stderr, "could not initialize alarm timer\n");
2832 exit(1);
2834 configure_icount(icount_option);
2836 if (net_init_clients() < 0) {
2837 exit(1);
2840 /* init the bluetooth world */
2841 if (foreach_device_config(DEV_BT, bt_parse))
2842 exit(1);
2844 /* init the memory */
2845 if (ram_size == 0)
2846 ram_size = DEFAULT_RAM_SIZE * 1024 * 1024;
2848 /* init the dynamic translator */
2849 cpu_exec_init_all(tb_size * 1024 * 1024);
2851 bdrv_init_with_whitelist();
2853 blk_mig_init();
2855 if (default_cdrom) {
2856 /* we always create the cdrom drive, even if no disk is there */
2857 drive_add(NULL, CDROM_ALIAS);
2860 if (default_floppy) {
2861 /* we always create at least one floppy */
2862 drive_add(NULL, FD_ALIAS, 0);
2865 if (default_sdcard) {
2866 /* we always create one sd slot, even if no card is in it */
2867 drive_add(NULL, SD_ALIAS);
2870 /* open the virtual block devices */
2871 if (snapshot)
2872 qemu_opts_foreach(&qemu_drive_opts, drive_enable_snapshot, NULL, 0);
2873 if (qemu_opts_foreach(&qemu_drive_opts, drive_init_func, &machine->use_scsi, 1) != 0)
2874 exit(1);
2876 register_savevm_live("ram", 0, 3, NULL, ram_save_live, NULL,
2877 ram_load, NULL);
2879 if (nb_numa_nodes > 0) {
2880 int i;
2882 if (nb_numa_nodes > smp_cpus) {
2883 nb_numa_nodes = smp_cpus;
2886 /* If no memory size if given for any node, assume the default case
2887 * and distribute the available memory equally across all nodes
2889 for (i = 0; i < nb_numa_nodes; i++) {
2890 if (node_mem[i] != 0)
2891 break;
2893 if (i == nb_numa_nodes) {
2894 uint64_t usedmem = 0;
2896 /* On Linux, the each node's border has to be 8MB aligned,
2897 * the final node gets the rest.
2899 for (i = 0; i < nb_numa_nodes - 1; i++) {
2900 node_mem[i] = (ram_size / nb_numa_nodes) & ~((1 << 23UL) - 1);
2901 usedmem += node_mem[i];
2903 node_mem[i] = ram_size - usedmem;
2906 for (i = 0; i < nb_numa_nodes; i++) {
2907 if (node_cpumask[i] != 0)
2908 break;
2910 /* assigning the VCPUs round-robin is easier to implement, guest OSes
2911 * must cope with this anyway, because there are BIOSes out there in
2912 * real machines which also use this scheme.
2914 if (i == nb_numa_nodes) {
2915 for (i = 0; i < smp_cpus; i++) {
2916 node_cpumask[i % nb_numa_nodes] |= 1 << i;
2921 if (qemu_opts_foreach(&qemu_mon_opts, mon_init_func, NULL, 1) != 0) {
2922 exit(1);
2925 if (foreach_device_config(DEV_SERIAL, serial_parse) < 0)
2926 exit(1);
2927 if (foreach_device_config(DEV_PARALLEL, parallel_parse) < 0)
2928 exit(1);
2929 if (foreach_device_config(DEV_VIRTCON, virtcon_parse) < 0)
2930 exit(1);
2931 if (foreach_device_config(DEV_DEBUGCON, debugcon_parse) < 0)
2932 exit(1);
2934 module_call_init(MODULE_INIT_DEVICE);
2936 if (qemu_opts_foreach(&qemu_device_opts, device_help_func, NULL, 0) != 0)
2937 exit(0);
2939 if (watchdog) {
2940 i = select_watchdog(watchdog);
2941 if (i > 0)
2942 exit (i == 1 ? 1 : 0);
2945 if (machine->compat_props) {
2946 qdev_prop_register_global_list(machine->compat_props);
2948 qemu_add_globals();
2950 machine->init(ram_size, boot_devices,
2951 kernel_filename, kernel_cmdline, initrd_filename, cpu_model);
2953 cpu_synchronize_all_post_init();
2955 /* must be after terminal init, SDL library changes signal handlers */
2956 os_setup_signal_handling();
2958 set_numa_modes();
2960 current_machine = machine;
2962 /* init USB devices */
2963 if (usb_enabled) {
2964 if (foreach_device_config(DEV_USB, usb_parse) < 0)
2965 exit(1);
2968 /* init generic devices */
2969 if (qemu_opts_foreach(&qemu_device_opts, device_init_func, NULL, 1) != 0)
2970 exit(1);
2972 net_check_clients();
2974 /* just use the first displaystate for the moment */
2975 ds = get_displaystate();
2977 if (display_type == DT_DEFAULT) {
2978 #if defined(CONFIG_SDL) || defined(CONFIG_COCOA)
2979 display_type = DT_SDL;
2980 #else
2981 display_type = DT_VNC;
2982 vnc_display = "localhost:0,to=99";
2983 show_vnc_port = 1;
2984 #endif
2988 switch (display_type) {
2989 case DT_NOGRAPHIC:
2990 break;
2991 #if defined(CONFIG_CURSES)
2992 case DT_CURSES:
2993 curses_display_init(ds, full_screen);
2994 break;
2995 #endif
2996 #if defined(CONFIG_SDL)
2997 case DT_SDL:
2998 sdl_display_init(ds, full_screen, no_frame);
2999 break;
3000 #elif defined(CONFIG_COCOA)
3001 case DT_SDL:
3002 cocoa_display_init(ds, full_screen);
3003 break;
3004 #endif
3005 case DT_VNC:
3006 vnc_display_init(ds);
3007 if (vnc_display_open(ds, vnc_display) < 0)
3008 exit(1);
3010 if (show_vnc_port) {
3011 printf("VNC server running on `%s'\n", vnc_display_local_addr(ds));
3013 break;
3014 default:
3015 break;
3017 dpy_resize(ds);
3019 dcl = ds->listeners;
3020 while (dcl != NULL) {
3021 if (dcl->dpy_refresh != NULL) {
3022 ds->gui_timer = qemu_new_timer(rt_clock, gui_update, ds);
3023 qemu_mod_timer(ds->gui_timer, qemu_get_clock(rt_clock));
3024 break;
3026 dcl = dcl->next;
3029 if (display_type == DT_NOGRAPHIC || display_type == DT_VNC) {
3030 nographic_timer = qemu_new_timer(rt_clock, nographic_update, NULL);
3031 qemu_mod_timer(nographic_timer, qemu_get_clock(rt_clock));
3034 text_consoles_set_display(ds);
3036 if (gdbstub_dev && gdbserver_start(gdbstub_dev) < 0) {
3037 fprintf(stderr, "qemu: could not open gdbserver on device '%s'\n",
3038 gdbstub_dev);
3039 exit(1);
3042 qdev_machine_creation_done();
3044 if (rom_load_all() != 0) {
3045 fprintf(stderr, "rom loading failed\n");
3046 exit(1);
3049 qemu_system_reset();
3050 if (loadvm) {
3051 if (load_vmstate(loadvm) < 0) {
3052 autostart = 0;
3056 if (incoming) {
3057 int ret = qemu_start_incoming_migration(incoming);
3058 if (ret < 0) {
3059 fprintf(stderr, "Migration failed. Exit code %s(%d), exiting.\n",
3060 incoming, ret);
3061 exit(ret);
3063 } else if (autostart) {
3064 vm_start();
3067 os_setup_post();
3069 main_loop();
3070 quit_timers();
3071 net_cleanup();
3073 return 0;