Merge commit '92a16d7a9a904d5dd55d012b89ad798f0e4cd0bd' into upstream-merge
[qemu-kvm/amd-iommu.git] / vl.c
blob0ee963cfa013d12d15833da625d0f354ff844004
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_LINUX
152 #include "fsdev/qemu-fsdev.h"
153 #endif
155 #include "disas.h"
157 #include "qemu_socket.h"
159 #include "slirp/libslirp.h"
161 #include "qemu-queue.h"
162 #include "cpus.h"
163 #include "arch_init.h"
165 //#define DEBUG_NET
166 //#define DEBUG_SLIRP
168 #define DEFAULT_RAM_SIZE 128
170 #define MAX_VIRTIO_CONSOLES 1
172 static const char *data_dir;
173 const char *bios_name = NULL;
174 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 #ifdef TARGET_I386
211 int no_hpet = 0;
212 #endif
213 int fd_bootchk = 1;
214 int no_reboot = 0;
215 int no_shutdown = 0;
216 int cursor_hide = 1;
217 int graphic_rotate = 0;
218 uint8_t irq0override = 1;
219 const char *watchdog;
220 const char *option_rom[MAX_OPTION_ROMS];
221 int nb_option_roms;
222 int semihosting_enabled = 0;
223 int time_drift_fix = 0;
224 unsigned int kvm_shadow_memory = 0;
225 int old_param = 0;
226 const char *qemu_name;
227 int alt_grab = 0;
228 int ctrl_grab = 0;
229 unsigned int nb_prom_envs = 0;
230 const char *prom_envs[MAX_PROM_ENVS];
231 const char *nvram = NULL;
232 int boot_menu;
234 int nb_numa_nodes;
235 uint64_t node_mem[MAX_NODES];
236 uint64_t node_cpumask[MAX_NODES];
238 static QEMUTimer *nographic_timer;
240 uint8_t qemu_uuid[16];
242 static QEMUBootSetHandler *boot_set_handler;
243 static void *boot_set_opaque;
245 static NotifierList exit_notifiers =
246 NOTIFIER_LIST_INITIALIZER(exit_notifiers);
248 int kvm_allowed = 1;
249 uint32_t xen_domid;
250 enum xen_mode xen_mode = XEN_EMULATE;
252 static int default_serial = 1;
253 static int default_parallel = 1;
254 static int default_virtcon = 1;
255 static int default_monitor = 1;
256 static int default_vga = 1;
257 static int default_floppy = 1;
258 static int default_cdrom = 1;
259 static int default_sdcard = 1;
261 static struct {
262 const char *driver;
263 int *flag;
264 } default_list[] = {
265 { .driver = "isa-serial", .flag = &default_serial },
266 { .driver = "isa-parallel", .flag = &default_parallel },
267 { .driver = "isa-fdc", .flag = &default_floppy },
268 { .driver = "ide-drive", .flag = &default_cdrom },
269 { .driver = "virtio-serial-pci", .flag = &default_virtcon },
270 { .driver = "virtio-serial-s390", .flag = &default_virtcon },
271 { .driver = "virtio-serial", .flag = &default_virtcon },
272 { .driver = "VGA", .flag = &default_vga },
273 { .driver = "cirrus-vga", .flag = &default_vga },
274 { .driver = "vmware-svga", .flag = &default_vga },
277 static int default_driver_check(QemuOpts *opts, void *opaque)
279 const char *driver = qemu_opt_get(opts, "driver");
280 int i;
282 if (!driver)
283 return 0;
284 for (i = 0; i < ARRAY_SIZE(default_list); i++) {
285 if (strcmp(default_list[i].driver, driver) != 0)
286 continue;
287 *(default_list[i].flag) = 0;
289 return 0;
292 /***********************************************************/
293 /* real time host monotonic timer */
295 /* compute with 96 bit intermediate result: (a*b)/c */
296 uint64_t muldiv64(uint64_t a, uint32_t b, uint32_t c)
298 union {
299 uint64_t ll;
300 struct {
301 #ifdef HOST_WORDS_BIGENDIAN
302 uint32_t high, low;
303 #else
304 uint32_t low, high;
305 #endif
306 } l;
307 } u, res;
308 uint64_t rl, rh;
310 u.ll = a;
311 rl = (uint64_t)u.l.low * (uint64_t)b;
312 rh = (uint64_t)u.l.high * (uint64_t)b;
313 rh += (rl >> 32);
314 res.l.high = rh / c;
315 res.l.low = (((rh % c) << 32) + (rl & 0xffffffff)) / c;
316 return res.ll;
319 /***********************************************************/
320 /* host time/date access */
321 void qemu_get_timedate(struct tm *tm, int offset)
323 time_t ti;
324 struct tm *ret;
326 time(&ti);
327 ti += offset;
328 if (rtc_date_offset == -1) {
329 if (rtc_utc)
330 ret = gmtime(&ti);
331 else
332 ret = localtime(&ti);
333 } else {
334 ti -= rtc_date_offset;
335 ret = gmtime(&ti);
338 memcpy(tm, ret, sizeof(struct tm));
341 int qemu_timedate_diff(struct tm *tm)
343 time_t seconds;
345 if (rtc_date_offset == -1)
346 if (rtc_utc)
347 seconds = mktimegm(tm);
348 else
349 seconds = mktime(tm);
350 else
351 seconds = mktimegm(tm) + rtc_date_offset;
353 return seconds - time(NULL);
356 void rtc_change_mon_event(struct tm *tm)
358 QObject *data;
360 data = qobject_from_jsonf("{ 'offset': %d }", qemu_timedate_diff(tm));
361 monitor_protocol_event(QEVENT_RTC_CHANGE, data);
362 qobject_decref(data);
365 static void configure_rtc_date_offset(const char *startdate, int legacy)
367 time_t rtc_start_date;
368 struct tm tm;
370 if (!strcmp(startdate, "now") && legacy) {
371 rtc_date_offset = -1;
372 } else {
373 if (sscanf(startdate, "%d-%d-%dT%d:%d:%d",
374 &tm.tm_year,
375 &tm.tm_mon,
376 &tm.tm_mday,
377 &tm.tm_hour,
378 &tm.tm_min,
379 &tm.tm_sec) == 6) {
380 /* OK */
381 } else if (sscanf(startdate, "%d-%d-%d",
382 &tm.tm_year,
383 &tm.tm_mon,
384 &tm.tm_mday) == 3) {
385 tm.tm_hour = 0;
386 tm.tm_min = 0;
387 tm.tm_sec = 0;
388 } else {
389 goto date_fail;
391 tm.tm_year -= 1900;
392 tm.tm_mon--;
393 rtc_start_date = mktimegm(&tm);
394 if (rtc_start_date == -1) {
395 date_fail:
396 fprintf(stderr, "Invalid date format. Valid formats are:\n"
397 "'2006-06-17T16:01:21' or '2006-06-17'\n");
398 exit(1);
400 rtc_date_offset = time(NULL) - rtc_start_date;
404 static void configure_rtc(QemuOpts *opts)
406 const char *value;
408 value = qemu_opt_get(opts, "base");
409 if (value) {
410 if (!strcmp(value, "utc")) {
411 rtc_utc = 1;
412 } else if (!strcmp(value, "localtime")) {
413 rtc_utc = 0;
414 } else {
415 configure_rtc_date_offset(value, 0);
418 value = qemu_opt_get(opts, "clock");
419 if (value) {
420 if (!strcmp(value, "host")) {
421 rtc_clock = host_clock;
422 } else if (!strcmp(value, "vm")) {
423 rtc_clock = vm_clock;
424 } else {
425 fprintf(stderr, "qemu: invalid option value '%s'\n", value);
426 exit(1);
429 value = qemu_opt_get(opts, "driftfix");
430 if (value) {
431 if (!strcmp(value, "slew")) {
432 rtc_td_hack = 1;
433 } else if (!strcmp(value, "none")) {
434 rtc_td_hack = 0;
435 } else {
436 fprintf(stderr, "qemu: invalid option value '%s'\n", value);
437 exit(1);
442 /***********************************************************/
443 /* Bluetooth support */
444 static int nb_hcis;
445 static int cur_hci;
446 static struct HCIInfo *hci_table[MAX_NICS];
448 static struct bt_vlan_s {
449 struct bt_scatternet_s net;
450 int id;
451 struct bt_vlan_s *next;
452 } *first_bt_vlan;
454 /* find or alloc a new bluetooth "VLAN" */
455 static struct bt_scatternet_s *qemu_find_bt_vlan(int id)
457 struct bt_vlan_s **pvlan, *vlan;
458 for (vlan = first_bt_vlan; vlan != NULL; vlan = vlan->next) {
459 if (vlan->id == id)
460 return &vlan->net;
462 vlan = qemu_mallocz(sizeof(struct bt_vlan_s));
463 vlan->id = id;
464 pvlan = &first_bt_vlan;
465 while (*pvlan != NULL)
466 pvlan = &(*pvlan)->next;
467 *pvlan = vlan;
468 return &vlan->net;
471 static void null_hci_send(struct HCIInfo *hci, const uint8_t *data, int len)
475 static int null_hci_addr_set(struct HCIInfo *hci, const uint8_t *bd_addr)
477 return -ENOTSUP;
480 static struct HCIInfo null_hci = {
481 .cmd_send = null_hci_send,
482 .sco_send = null_hci_send,
483 .acl_send = null_hci_send,
484 .bdaddr_set = null_hci_addr_set,
487 struct HCIInfo *qemu_next_hci(void)
489 if (cur_hci == nb_hcis)
490 return &null_hci;
492 return hci_table[cur_hci++];
495 static struct HCIInfo *hci_init(const char *str)
497 char *endp;
498 struct bt_scatternet_s *vlan = 0;
500 if (!strcmp(str, "null"))
501 /* null */
502 return &null_hci;
503 else if (!strncmp(str, "host", 4) && (str[4] == '\0' || str[4] == ':'))
504 /* host[:hciN] */
505 return bt_host_hci(str[4] ? str + 5 : "hci0");
506 else if (!strncmp(str, "hci", 3)) {
507 /* hci[,vlan=n] */
508 if (str[3]) {
509 if (!strncmp(str + 3, ",vlan=", 6)) {
510 vlan = qemu_find_bt_vlan(strtol(str + 9, &endp, 0));
511 if (*endp)
512 vlan = 0;
514 } else
515 vlan = qemu_find_bt_vlan(0);
516 if (vlan)
517 return bt_new_hci(vlan);
520 fprintf(stderr, "qemu: Unknown bluetooth HCI `%s'.\n", str);
522 return 0;
525 static int bt_hci_parse(const char *str)
527 struct HCIInfo *hci;
528 bdaddr_t bdaddr;
530 if (nb_hcis >= MAX_NICS) {
531 fprintf(stderr, "qemu: Too many bluetooth HCIs (max %i).\n", MAX_NICS);
532 return -1;
535 hci = hci_init(str);
536 if (!hci)
537 return -1;
539 bdaddr.b[0] = 0x52;
540 bdaddr.b[1] = 0x54;
541 bdaddr.b[2] = 0x00;
542 bdaddr.b[3] = 0x12;
543 bdaddr.b[4] = 0x34;
544 bdaddr.b[5] = 0x56 + nb_hcis;
545 hci->bdaddr_set(hci, bdaddr.b);
547 hci_table[nb_hcis++] = hci;
549 return 0;
552 static void bt_vhci_add(int vlan_id)
554 struct bt_scatternet_s *vlan = qemu_find_bt_vlan(vlan_id);
556 if (!vlan->slave)
557 fprintf(stderr, "qemu: warning: adding a VHCI to "
558 "an empty scatternet %i\n", vlan_id);
560 bt_vhci_init(bt_new_hci(vlan));
563 static struct bt_device_s *bt_device_add(const char *opt)
565 struct bt_scatternet_s *vlan;
566 int vlan_id = 0;
567 char *endp = strstr(opt, ",vlan=");
568 int len = (endp ? endp - opt : strlen(opt)) + 1;
569 char devname[10];
571 pstrcpy(devname, MIN(sizeof(devname), len), opt);
573 if (endp) {
574 vlan_id = strtol(endp + 6, &endp, 0);
575 if (*endp) {
576 fprintf(stderr, "qemu: unrecognised bluetooth vlan Id\n");
577 return 0;
581 vlan = qemu_find_bt_vlan(vlan_id);
583 if (!vlan->slave)
584 fprintf(stderr, "qemu: warning: adding a slave device to "
585 "an empty scatternet %i\n", vlan_id);
587 if (!strcmp(devname, "keyboard"))
588 return bt_keyboard_init(vlan);
590 fprintf(stderr, "qemu: unsupported bluetooth device `%s'\n", devname);
591 return 0;
594 static int bt_parse(const char *opt)
596 const char *endp, *p;
597 int vlan;
599 if (strstart(opt, "hci", &endp)) {
600 if (!*endp || *endp == ',') {
601 if (*endp)
602 if (!strstart(endp, ",vlan=", 0))
603 opt = endp + 1;
605 return bt_hci_parse(opt);
607 } else if (strstart(opt, "vhci", &endp)) {
608 if (!*endp || *endp == ',') {
609 if (*endp) {
610 if (strstart(endp, ",vlan=", &p)) {
611 vlan = strtol(p, (char **) &endp, 0);
612 if (*endp) {
613 fprintf(stderr, "qemu: bad scatternet '%s'\n", p);
614 return 1;
616 } else {
617 fprintf(stderr, "qemu: bad parameter '%s'\n", endp + 1);
618 return 1;
620 } else
621 vlan = 0;
623 bt_vhci_add(vlan);
624 return 0;
626 } else if (strstart(opt, "device:", &endp))
627 return !bt_device_add(endp);
629 fprintf(stderr, "qemu: bad bluetooth parameter '%s'\n", opt);
630 return 1;
633 /***********************************************************/
634 /* QEMU Block devices */
636 #define HD_ALIAS "index=%d,media=disk"
637 #define CDROM_ALIAS "index=2,media=cdrom"
638 #define FD_ALIAS "index=%d,if=floppy"
639 #define PFLASH_ALIAS "if=pflash"
640 #define MTD_ALIAS "if=mtd"
641 #define SD_ALIAS "index=0,if=sd"
643 static int drive_init_func(QemuOpts *opts, void *opaque)
645 int *use_scsi = opaque;
646 int fatal_error = 0;
648 if (drive_init(opts, *use_scsi, &fatal_error) == NULL) {
649 if (fatal_error)
650 return 1;
652 return 0;
655 static int drive_enable_snapshot(QemuOpts *opts, void *opaque)
657 if (NULL == qemu_opt_get(opts, "snapshot")) {
658 qemu_opt_set(opts, "snapshot", "on");
660 return 0;
663 void qemu_register_boot_set(QEMUBootSetHandler *func, void *opaque)
665 boot_set_handler = func;
666 boot_set_opaque = opaque;
669 int qemu_boot_set(const char *boot_devices)
671 if (!boot_set_handler) {
672 return -EINVAL;
674 return boot_set_handler(boot_set_opaque, boot_devices);
677 static void validate_bootdevices(char *devices)
679 /* We just do some generic consistency checks */
680 const char *p;
681 int bitmap = 0;
683 for (p = devices; *p != '\0'; p++) {
684 /* Allowed boot devices are:
685 * a-b: floppy disk drives
686 * c-f: IDE disk drives
687 * g-m: machine implementation dependant drives
688 * n-p: network devices
689 * It's up to each machine implementation to check if the given boot
690 * devices match the actual hardware implementation and firmware
691 * features.
693 if (*p < 'a' || *p > 'p') {
694 fprintf(stderr, "Invalid boot device '%c'\n", *p);
695 exit(1);
697 if (bitmap & (1 << (*p - 'a'))) {
698 fprintf(stderr, "Boot device '%c' was given twice\n", *p);
699 exit(1);
701 bitmap |= 1 << (*p - 'a');
705 static void restore_boot_devices(void *opaque)
707 char *standard_boot_devices = opaque;
708 static int first = 1;
710 /* Restore boot order and remove ourselves after the first boot */
711 if (first) {
712 first = 0;
713 return;
716 qemu_boot_set(standard_boot_devices);
718 qemu_unregister_reset(restore_boot_devices, standard_boot_devices);
719 qemu_free(standard_boot_devices);
722 static void numa_add(const char *optarg)
724 char option[128];
725 char *endptr;
726 unsigned long long value, endvalue;
727 int nodenr;
729 optarg = get_opt_name(option, 128, optarg, ',') + 1;
730 if (!strcmp(option, "node")) {
731 if (get_param_value(option, 128, "nodeid", optarg) == 0) {
732 nodenr = nb_numa_nodes;
733 } else {
734 nodenr = strtoull(option, NULL, 10);
737 if (get_param_value(option, 128, "mem", optarg) == 0) {
738 node_mem[nodenr] = 0;
739 } else {
740 value = strtoull(option, &endptr, 0);
741 switch (*endptr) {
742 case 0: case 'M': case 'm':
743 value <<= 20;
744 break;
745 case 'G': case 'g':
746 value <<= 30;
747 break;
749 node_mem[nodenr] = value;
751 if (get_param_value(option, 128, "cpus", optarg) == 0) {
752 node_cpumask[nodenr] = 0;
753 } else {
754 value = strtoull(option, &endptr, 10);
755 if (value >= 64) {
756 value = 63;
757 fprintf(stderr, "only 64 CPUs in NUMA mode supported.\n");
758 } else {
759 if (*endptr == '-') {
760 endvalue = strtoull(endptr+1, &endptr, 10);
761 if (endvalue >= 63) {
762 endvalue = 62;
763 fprintf(stderr,
764 "only 63 CPUs in NUMA mode supported.\n");
766 value = (2ULL << endvalue) - (1ULL << value);
767 } else {
768 value = 1ULL << value;
771 node_cpumask[nodenr] = value;
773 nb_numa_nodes++;
775 return;
778 static void smp_parse(const char *optarg)
780 int smp, sockets = 0, threads = 0, cores = 0;
781 char *endptr;
782 char option[128];
784 smp = strtoul(optarg, &endptr, 10);
785 if (endptr != optarg) {
786 if (*endptr == ',') {
787 endptr++;
790 if (get_param_value(option, 128, "sockets", endptr) != 0)
791 sockets = strtoull(option, NULL, 10);
792 if (get_param_value(option, 128, "cores", endptr) != 0)
793 cores = strtoull(option, NULL, 10);
794 if (get_param_value(option, 128, "threads", endptr) != 0)
795 threads = strtoull(option, NULL, 10);
796 if (get_param_value(option, 128, "maxcpus", endptr) != 0)
797 max_cpus = strtoull(option, NULL, 10);
799 /* compute missing values, prefer sockets over cores over threads */
800 if (smp == 0 || sockets == 0) {
801 sockets = sockets > 0 ? sockets : 1;
802 cores = cores > 0 ? cores : 1;
803 threads = threads > 0 ? threads : 1;
804 if (smp == 0) {
805 smp = cores * threads * sockets;
807 } else {
808 if (cores == 0) {
809 threads = threads > 0 ? threads : 1;
810 cores = smp / (sockets * threads);
811 } else {
812 if (sockets) {
813 threads = smp / (cores * sockets);
817 smp_cpus = smp;
818 smp_cores = cores > 0 ? cores : 1;
819 smp_threads = threads > 0 ? threads : 1;
820 if (max_cpus == 0)
821 max_cpus = smp_cpus;
824 /***********************************************************/
825 /* USB devices */
827 static int usb_device_add(const char *devname)
829 const char *p;
830 USBDevice *dev = NULL;
832 if (!usb_enabled)
833 return -1;
835 /* drivers with .usbdevice_name entry in USBDeviceInfo */
836 dev = usbdevice_create(devname);
837 if (dev)
838 goto done;
840 /* the other ones */
841 if (strstart(devname, "host:", &p)) {
842 dev = usb_host_device_open(p);
843 } else if (!strcmp(devname, "bt") || strstart(devname, "bt:", &p)) {
844 dev = usb_bt_init(devname[2] ? hci_init(p) :
845 bt_new_hci(qemu_find_bt_vlan(0)));
846 } else {
847 return -1;
849 if (!dev)
850 return -1;
852 done:
853 return 0;
856 static int usb_device_del(const char *devname)
858 int bus_num, addr;
859 const char *p;
861 if (strstart(devname, "host:", &p))
862 return usb_host_device_close(p);
864 if (!usb_enabled)
865 return -1;
867 p = strchr(devname, '.');
868 if (!p)
869 return -1;
870 bus_num = strtoul(devname, NULL, 0);
871 addr = strtoul(p + 1, NULL, 0);
873 return usb_device_delete_addr(bus_num, addr);
876 static int usb_parse(const char *cmdline)
878 int r;
879 r = usb_device_add(cmdline);
880 if (r < 0) {
881 fprintf(stderr, "qemu: could not add USB device '%s'\n", cmdline);
883 return r;
886 void do_usb_add(Monitor *mon, const QDict *qdict)
888 const char *devname = qdict_get_str(qdict, "devname");
889 if (usb_device_add(devname) < 0) {
890 error_report("could not add USB device '%s'", devname);
894 void do_usb_del(Monitor *mon, const QDict *qdict)
896 const char *devname = qdict_get_str(qdict, "devname");
897 if (usb_device_del(devname) < 0) {
898 error_report("could not delete USB device '%s'", devname);
902 /***********************************************************/
903 /* PCMCIA/Cardbus */
905 static struct pcmcia_socket_entry_s {
906 PCMCIASocket *socket;
907 struct pcmcia_socket_entry_s *next;
908 } *pcmcia_sockets = 0;
910 void pcmcia_socket_register(PCMCIASocket *socket)
912 struct pcmcia_socket_entry_s *entry;
914 entry = qemu_malloc(sizeof(struct pcmcia_socket_entry_s));
915 entry->socket = socket;
916 entry->next = pcmcia_sockets;
917 pcmcia_sockets = entry;
920 void pcmcia_socket_unregister(PCMCIASocket *socket)
922 struct pcmcia_socket_entry_s *entry, **ptr;
924 ptr = &pcmcia_sockets;
925 for (entry = *ptr; entry; ptr = &entry->next, entry = *ptr)
926 if (entry->socket == socket) {
927 *ptr = entry->next;
928 qemu_free(entry);
932 void pcmcia_info(Monitor *mon)
934 struct pcmcia_socket_entry_s *iter;
936 if (!pcmcia_sockets)
937 monitor_printf(mon, "No PCMCIA sockets\n");
939 for (iter = pcmcia_sockets; iter; iter = iter->next)
940 monitor_printf(mon, "%s: %s\n", iter->socket->slot_string,
941 iter->socket->attached ? iter->socket->card_string :
942 "Empty");
945 /***********************************************************/
946 /* I/O handling */
948 typedef struct IOHandlerRecord {
949 int fd;
950 IOCanReadHandler *fd_read_poll;
951 IOHandler *fd_read;
952 IOHandler *fd_write;
953 int deleted;
954 void *opaque;
955 /* temporary data */
956 struct pollfd *ufd;
957 QLIST_ENTRY(IOHandlerRecord) next;
958 } IOHandlerRecord;
960 static QLIST_HEAD(, IOHandlerRecord) io_handlers =
961 QLIST_HEAD_INITIALIZER(io_handlers);
964 /* XXX: fd_read_poll should be suppressed, but an API change is
965 necessary in the character devices to suppress fd_can_read(). */
966 int qemu_set_fd_handler2(int fd,
967 IOCanReadHandler *fd_read_poll,
968 IOHandler *fd_read,
969 IOHandler *fd_write,
970 void *opaque)
972 IOHandlerRecord *ioh;
974 if (!fd_read && !fd_write) {
975 QLIST_FOREACH(ioh, &io_handlers, next) {
976 if (ioh->fd == fd) {
977 ioh->deleted = 1;
978 break;
981 } else {
982 QLIST_FOREACH(ioh, &io_handlers, next) {
983 if (ioh->fd == fd)
984 goto found;
986 ioh = qemu_mallocz(sizeof(IOHandlerRecord));
987 QLIST_INSERT_HEAD(&io_handlers, ioh, next);
988 found:
989 ioh->fd = fd;
990 ioh->fd_read_poll = fd_read_poll;
991 ioh->fd_read = fd_read;
992 ioh->fd_write = fd_write;
993 ioh->opaque = opaque;
994 ioh->deleted = 0;
996 qemu_notify_event();
997 return 0;
1000 int qemu_set_fd_handler(int fd,
1001 IOHandler *fd_read,
1002 IOHandler *fd_write,
1003 void *opaque)
1005 return qemu_set_fd_handler2(fd, NULL, fd_read, fd_write, opaque);
1008 /***********************************************************/
1009 /* machine registration */
1011 static QEMUMachine *first_machine = NULL;
1012 QEMUMachine *current_machine = NULL;
1014 int qemu_register_machine(QEMUMachine *m)
1016 QEMUMachine **pm;
1017 pm = &first_machine;
1018 while (*pm != NULL)
1019 pm = &(*pm)->next;
1020 m->next = NULL;
1021 *pm = m;
1022 return 0;
1025 static QEMUMachine *find_machine(const char *name)
1027 QEMUMachine *m;
1029 for(m = first_machine; m != NULL; m = m->next) {
1030 if (!strcmp(m->name, name))
1031 return m;
1032 if (m->alias && !strcmp(m->alias, name))
1033 return m;
1035 return NULL;
1038 static QEMUMachine *find_default_machine(void)
1040 QEMUMachine *m;
1042 for(m = first_machine; m != NULL; m = m->next) {
1043 if (m->is_default) {
1044 return m;
1047 return NULL;
1050 /***********************************************************/
1051 /* main execution loop */
1053 static void gui_update(void *opaque)
1055 uint64_t interval = GUI_REFRESH_INTERVAL;
1056 DisplayState *ds = opaque;
1057 DisplayChangeListener *dcl = ds->listeners;
1059 qemu_flush_coalesced_mmio_buffer();
1060 dpy_refresh(ds);
1062 while (dcl != NULL) {
1063 if (dcl->gui_timer_interval &&
1064 dcl->gui_timer_interval < interval)
1065 interval = dcl->gui_timer_interval;
1066 dcl = dcl->next;
1068 qemu_mod_timer(ds->gui_timer, interval + qemu_get_clock(rt_clock));
1071 static void nographic_update(void *opaque)
1073 uint64_t interval = GUI_REFRESH_INTERVAL;
1075 qemu_flush_coalesced_mmio_buffer();
1076 qemu_mod_timer(nographic_timer, interval + qemu_get_clock(rt_clock));
1079 struct vm_change_state_entry {
1080 VMChangeStateHandler *cb;
1081 void *opaque;
1082 QLIST_ENTRY (vm_change_state_entry) entries;
1085 static QLIST_HEAD(vm_change_state_head, vm_change_state_entry) vm_change_state_head;
1087 VMChangeStateEntry *qemu_add_vm_change_state_handler(VMChangeStateHandler *cb,
1088 void *opaque)
1090 VMChangeStateEntry *e;
1092 e = qemu_mallocz(sizeof (*e));
1094 e->cb = cb;
1095 e->opaque = opaque;
1096 QLIST_INSERT_HEAD(&vm_change_state_head, e, entries);
1097 return e;
1100 void qemu_del_vm_change_state_handler(VMChangeStateEntry *e)
1102 QLIST_REMOVE (e, entries);
1103 qemu_free (e);
1106 void vm_state_notify(int running, int reason)
1108 VMChangeStateEntry *e;
1110 for (e = vm_change_state_head.lh_first; e; e = e->entries.le_next) {
1111 e->cb(e->opaque, running, reason);
1115 void vm_start(void)
1117 if (!vm_running) {
1118 cpu_enable_ticks();
1119 vm_running = 1;
1120 vm_state_notify(1, 0);
1121 resume_all_vcpus();
1122 monitor_protocol_event(QEVENT_RESUME, NULL);
1126 /* reset/shutdown handler */
1128 typedef struct QEMUResetEntry {
1129 QTAILQ_ENTRY(QEMUResetEntry) entry;
1130 QEMUResetHandler *func;
1131 void *opaque;
1132 } QEMUResetEntry;
1134 static QTAILQ_HEAD(reset_handlers, QEMUResetEntry) reset_handlers =
1135 QTAILQ_HEAD_INITIALIZER(reset_handlers);
1136 static int reset_requested;
1137 static int shutdown_requested;
1138 static int powerdown_requested;
1139 int debug_requested;
1140 int vmstop_requested;
1142 int qemu_no_shutdown(void)
1144 int r = no_shutdown;
1145 no_shutdown = 0;
1146 return r;
1149 int qemu_shutdown_requested(void)
1151 int r = shutdown_requested;
1152 shutdown_requested = 0;
1153 return r;
1156 int qemu_reset_requested(void)
1158 int r = reset_requested;
1159 reset_requested = 0;
1160 return r;
1163 int qemu_powerdown_requested(void)
1165 int r = powerdown_requested;
1166 powerdown_requested = 0;
1167 return r;
1170 static int qemu_debug_requested(void)
1172 int r = debug_requested;
1173 debug_requested = 0;
1174 return r;
1177 static int qemu_vmstop_requested(void)
1179 int r = vmstop_requested;
1180 vmstop_requested = 0;
1181 return r;
1184 void qemu_register_reset(QEMUResetHandler *func, void *opaque)
1186 QEMUResetEntry *re = qemu_mallocz(sizeof(QEMUResetEntry));
1188 re->func = func;
1189 re->opaque = opaque;
1190 QTAILQ_INSERT_TAIL(&reset_handlers, re, entry);
1193 void qemu_unregister_reset(QEMUResetHandler *func, void *opaque)
1195 QEMUResetEntry *re;
1197 QTAILQ_FOREACH(re, &reset_handlers, entry) {
1198 if (re->func == func && re->opaque == opaque) {
1199 QTAILQ_REMOVE(&reset_handlers, re, entry);
1200 qemu_free(re);
1201 return;
1206 void qemu_system_reset(void)
1208 QEMUResetEntry *re, *nre;
1210 /* reset all devices */
1211 QTAILQ_FOREACH_SAFE(re, &reset_handlers, entry, nre) {
1212 re->func(re->opaque);
1214 monitor_protocol_event(QEVENT_RESET, NULL);
1215 cpu_synchronize_all_post_reset();
1218 void qemu_system_reset_request(void)
1220 if (no_reboot) {
1221 shutdown_requested = 1;
1222 } else {
1223 reset_requested = 1;
1225 if (cpu_single_env) {
1226 cpu_single_env->stopped = 1;
1228 qemu_notify_event();
1231 void qemu_system_shutdown_request(void)
1233 shutdown_requested = 1;
1234 qemu_notify_event();
1237 void qemu_system_powerdown_request(void)
1239 powerdown_requested = 1;
1240 qemu_notify_event();
1243 void main_loop_wait(int nonblocking)
1245 IOHandlerRecord *ioh;
1246 fd_set rfds, wfds, xfds;
1247 int ret, nfds;
1248 struct timeval tv;
1249 int timeout;
1251 if (nonblocking)
1252 timeout = 0;
1253 else {
1254 timeout = qemu_calculate_timeout();
1255 qemu_bh_update_timeout(&timeout);
1258 os_host_main_loop_wait(&timeout);
1260 /* poll any events */
1261 /* XXX: separate device handlers from system ones */
1262 nfds = -1;
1263 FD_ZERO(&rfds);
1264 FD_ZERO(&wfds);
1265 FD_ZERO(&xfds);
1266 QLIST_FOREACH(ioh, &io_handlers, next) {
1267 if (ioh->deleted)
1268 continue;
1269 if (ioh->fd_read &&
1270 (!ioh->fd_read_poll ||
1271 ioh->fd_read_poll(ioh->opaque) != 0)) {
1272 FD_SET(ioh->fd, &rfds);
1273 if (ioh->fd > nfds)
1274 nfds = ioh->fd;
1276 if (ioh->fd_write) {
1277 FD_SET(ioh->fd, &wfds);
1278 if (ioh->fd > nfds)
1279 nfds = ioh->fd;
1283 tv.tv_sec = timeout / 1000;
1284 tv.tv_usec = (timeout % 1000) * 1000;
1286 slirp_select_fill(&nfds, &rfds, &wfds, &xfds);
1288 qemu_mutex_unlock_iothread();
1289 ret = select(nfds + 1, &rfds, &wfds, &xfds, &tv);
1290 qemu_mutex_lock_iothread();
1291 if (ret > 0) {
1292 IOHandlerRecord *pioh;
1294 QLIST_FOREACH_SAFE(ioh, &io_handlers, next, pioh) {
1295 if (ioh->deleted) {
1296 QLIST_REMOVE(ioh, next);
1297 qemu_free(ioh);
1298 continue;
1300 if (ioh->fd_read && FD_ISSET(ioh->fd, &rfds)) {
1301 ioh->fd_read(ioh->opaque);
1302 if (!(ioh->fd_read_poll && ioh->fd_read_poll(ioh->opaque)))
1303 FD_CLR(ioh->fd, &rfds);
1305 if (ioh->fd_write && FD_ISSET(ioh->fd, &wfds)) {
1306 ioh->fd_write(ioh->opaque);
1311 slirp_select_poll(&rfds, &wfds, &xfds, (ret < 0));
1313 qemu_run_all_timers();
1315 /* Check bottom-halves last in case any of the earlier events triggered
1316 them. */
1317 qemu_bh_poll();
1321 static int vm_can_run(void)
1323 if (powerdown_requested)
1324 return 0;
1325 if (reset_requested)
1326 return 0;
1327 if (shutdown_requested)
1328 return 0;
1329 if (debug_requested)
1330 return 0;
1331 return 1;
1334 qemu_irq qemu_system_powerdown;
1336 static void main_loop(void)
1338 int r;
1340 if (kvm_enabled()) {
1341 kvm_main_loop();
1342 cpu_disable_ticks();
1343 return;
1346 qemu_main_loop_start();
1348 for (;;) {
1349 do {
1350 bool nonblocking = false;
1351 #ifdef CONFIG_PROFILER
1352 int64_t ti;
1353 #endif
1354 #ifndef CONFIG_IOTHREAD
1355 nonblocking = tcg_cpu_exec();
1356 #endif
1357 #ifdef CONFIG_PROFILER
1358 ti = profile_getclock();
1359 #endif
1360 main_loop_wait(nonblocking);
1361 #ifdef CONFIG_PROFILER
1362 dev_time += profile_getclock() - ti;
1363 #endif
1364 } while (vm_can_run());
1366 if ((r = qemu_debug_requested())) {
1367 vm_stop(r);
1369 if (qemu_shutdown_requested()) {
1370 monitor_protocol_event(QEVENT_SHUTDOWN, NULL);
1371 if (no_shutdown) {
1372 vm_stop(0);
1373 no_shutdown = 0;
1374 } else
1375 break;
1377 if (qemu_reset_requested()) {
1378 pause_all_vcpus();
1379 qemu_system_reset();
1380 resume_all_vcpus();
1382 if (qemu_powerdown_requested()) {
1383 monitor_protocol_event(QEVENT_POWERDOWN, NULL);
1384 qemu_irq_raise(qemu_system_powerdown);
1386 if ((r = qemu_vmstop_requested())) {
1387 vm_stop(r);
1390 bdrv_close_all();
1391 pause_all_vcpus();
1394 static void version(void)
1396 printf("QEMU emulator version " QEMU_VERSION QEMU_PKGVERSION ", Copyright (c) 2003-2008 Fabrice Bellard\n");
1399 static void help(int exitcode)
1401 const char *options_help =
1402 #define DEF(option, opt_arg, opt_enum, opt_help, arch_mask) \
1403 opt_help
1404 #define DEFHEADING(text) stringify(text) "\n"
1405 #include "qemu-options.def"
1406 #undef DEF
1407 #undef DEFHEADING
1408 #undef GEN_DOCS
1410 version();
1411 printf("usage: %s [options] [disk_image]\n"
1412 "\n"
1413 "'disk_image' is a raw hard disk image for IDE hard disk 0\n"
1414 "\n"
1415 "%s\n"
1416 "During emulation, the following keys are useful:\n"
1417 "ctrl-alt-f toggle full screen\n"
1418 "ctrl-alt-n switch to virtual console 'n'\n"
1419 "ctrl-alt toggle mouse and keyboard grab\n"
1420 "\n"
1421 "When using -nographic, press 'ctrl-a h' to get some help.\n",
1422 "qemu",
1423 options_help);
1424 exit(exitcode);
1427 #define HAS_ARG 0x0001
1429 typedef struct QEMUOption {
1430 const char *name;
1431 int flags;
1432 int index;
1433 uint32_t arch_mask;
1434 } QEMUOption;
1436 static const QEMUOption qemu_options[] = {
1437 { "h", 0, QEMU_OPTION_h, QEMU_ARCH_ALL },
1438 #define DEF(option, opt_arg, opt_enum, opt_help, arch_mask) \
1439 { option, opt_arg, opt_enum, arch_mask },
1440 #define DEFHEADING(text)
1441 #include "qemu-options.def"
1442 #undef DEF
1443 #undef DEFHEADING
1444 #undef GEN_DOCS
1445 { NULL },
1447 static void select_vgahw (const char *p)
1449 const char *opts;
1451 default_vga = 0;
1452 vga_interface_type = VGA_NONE;
1453 if (strstart(p, "std", &opts)) {
1454 vga_interface_type = VGA_STD;
1455 } else if (strstart(p, "cirrus", &opts)) {
1456 vga_interface_type = VGA_CIRRUS;
1457 } else if (strstart(p, "vmware", &opts)) {
1458 vga_interface_type = VGA_VMWARE;
1459 } else if (strstart(p, "xenfb", &opts)) {
1460 vga_interface_type = VGA_XENFB;
1461 } else if (!strstart(p, "none", &opts)) {
1462 invalid_vga:
1463 fprintf(stderr, "Unknown vga type: %s\n", p);
1464 exit(1);
1466 while (*opts) {
1467 const char *nextopt;
1469 if (strstart(opts, ",retrace=", &nextopt)) {
1470 opts = nextopt;
1471 if (strstart(opts, "dumb", &nextopt))
1472 vga_retrace_method = VGA_RETRACE_DUMB;
1473 else if (strstart(opts, "precise", &nextopt))
1474 vga_retrace_method = VGA_RETRACE_PRECISE;
1475 else goto invalid_vga;
1476 } else goto invalid_vga;
1477 opts = nextopt;
1481 static int balloon_parse(const char *arg)
1483 QemuOpts *opts;
1485 if (strcmp(arg, "none") == 0) {
1486 return 0;
1489 if (!strncmp(arg, "virtio", 6)) {
1490 if (arg[6] == ',') {
1491 /* have params -> parse them */
1492 opts = qemu_opts_parse(&qemu_device_opts, arg+7, 0);
1493 if (!opts)
1494 return -1;
1495 } else {
1496 /* create empty opts */
1497 opts = qemu_opts_create(&qemu_device_opts, NULL, 0);
1499 qemu_opt_set(opts, "driver", "virtio-balloon-pci");
1500 return 0;
1503 return -1;
1506 char *qemu_find_file(int type, const char *name)
1508 int len;
1509 const char *subdir;
1510 char *buf;
1512 /* If name contains path separators then try it as a straight path. */
1513 if ((strchr(name, '/') || strchr(name, '\\'))
1514 && access(name, R_OK) == 0) {
1515 return qemu_strdup(name);
1517 switch (type) {
1518 case QEMU_FILE_TYPE_BIOS:
1519 subdir = "";
1520 break;
1521 case QEMU_FILE_TYPE_KEYMAP:
1522 subdir = "keymaps/";
1523 break;
1524 default:
1525 abort();
1527 len = strlen(data_dir) + strlen(name) + strlen(subdir) + 2;
1528 buf = qemu_mallocz(len);
1529 snprintf(buf, len, "%s/%s%s", data_dir, subdir, name);
1530 if (access(buf, R_OK)) {
1531 qemu_free(buf);
1532 return NULL;
1534 return buf;
1537 static int device_help_func(QemuOpts *opts, void *opaque)
1539 return qdev_device_help(opts);
1542 static int device_init_func(QemuOpts *opts, void *opaque)
1544 DeviceState *dev;
1546 dev = qdev_device_add(opts);
1547 if (!dev)
1548 return -1;
1549 return 0;
1552 static int chardev_init_func(QemuOpts *opts, void *opaque)
1554 CharDriverState *chr;
1556 chr = qemu_chr_open_opts(opts, NULL);
1557 if (!chr)
1558 return -1;
1559 return 0;
1562 #ifdef CONFIG_LINUX
1563 static int fsdev_init_func(QemuOpts *opts, void *opaque)
1565 int ret;
1566 ret = qemu_fsdev_add(opts);
1568 return ret;
1570 #endif
1572 static int mon_init_func(QemuOpts *opts, void *opaque)
1574 CharDriverState *chr;
1575 const char *chardev;
1576 const char *mode;
1577 int flags;
1579 mode = qemu_opt_get(opts, "mode");
1580 if (mode == NULL) {
1581 mode = "readline";
1583 if (strcmp(mode, "readline") == 0) {
1584 flags = MONITOR_USE_READLINE;
1585 } else if (strcmp(mode, "control") == 0) {
1586 flags = MONITOR_USE_CONTROL;
1587 } else {
1588 fprintf(stderr, "unknown monitor mode \"%s\"\n", mode);
1589 exit(1);
1592 if (qemu_opt_get_bool(opts, "default", 0))
1593 flags |= MONITOR_IS_DEFAULT;
1595 chardev = qemu_opt_get(opts, "chardev");
1596 chr = qemu_chr_find(chardev);
1597 if (chr == NULL) {
1598 fprintf(stderr, "chardev \"%s\" not found\n", chardev);
1599 exit(1);
1602 monitor_init(chr, flags);
1603 return 0;
1606 static void monitor_parse(const char *optarg, const char *mode)
1608 static int monitor_device_index = 0;
1609 QemuOpts *opts;
1610 const char *p;
1611 char label[32];
1612 int def = 0;
1614 if (strstart(optarg, "chardev:", &p)) {
1615 snprintf(label, sizeof(label), "%s", p);
1616 } else {
1617 snprintf(label, sizeof(label), "compat_monitor%d",
1618 monitor_device_index);
1619 if (monitor_device_index == 0) {
1620 def = 1;
1622 opts = qemu_chr_parse_compat(label, optarg);
1623 if (!opts) {
1624 fprintf(stderr, "parse error: %s\n", optarg);
1625 exit(1);
1629 opts = qemu_opts_create(&qemu_mon_opts, label, 1);
1630 if (!opts) {
1631 fprintf(stderr, "duplicate chardev: %s\n", label);
1632 exit(1);
1634 qemu_opt_set(opts, "mode", mode);
1635 qemu_opt_set(opts, "chardev", label);
1636 if (def)
1637 qemu_opt_set(opts, "default", "on");
1638 monitor_device_index++;
1641 struct device_config {
1642 enum {
1643 DEV_USB, /* -usbdevice */
1644 DEV_BT, /* -bt */
1645 DEV_SERIAL, /* -serial */
1646 DEV_PARALLEL, /* -parallel */
1647 DEV_VIRTCON, /* -virtioconsole */
1648 DEV_DEBUGCON, /* -debugcon */
1649 } type;
1650 const char *cmdline;
1651 QTAILQ_ENTRY(device_config) next;
1653 QTAILQ_HEAD(, device_config) device_configs = QTAILQ_HEAD_INITIALIZER(device_configs);
1655 static void add_device_config(int type, const char *cmdline)
1657 struct device_config *conf;
1659 conf = qemu_mallocz(sizeof(*conf));
1660 conf->type = type;
1661 conf->cmdline = cmdline;
1662 QTAILQ_INSERT_TAIL(&device_configs, conf, next);
1665 static int foreach_device_config(int type, int (*func)(const char *cmdline))
1667 struct device_config *conf;
1668 int rc;
1670 QTAILQ_FOREACH(conf, &device_configs, next) {
1671 if (conf->type != type)
1672 continue;
1673 rc = func(conf->cmdline);
1674 if (0 != rc)
1675 return rc;
1677 return 0;
1680 static int serial_parse(const char *devname)
1682 static int index = 0;
1683 char label[32];
1685 if (strcmp(devname, "none") == 0)
1686 return 0;
1687 if (index == MAX_SERIAL_PORTS) {
1688 fprintf(stderr, "qemu: too many serial ports\n");
1689 exit(1);
1691 snprintf(label, sizeof(label), "serial%d", index);
1692 serial_hds[index] = qemu_chr_open(label, devname, NULL);
1693 if (!serial_hds[index]) {
1694 fprintf(stderr, "qemu: could not open serial device '%s': %s\n",
1695 devname, strerror(errno));
1696 return -1;
1698 index++;
1699 return 0;
1702 static int parallel_parse(const char *devname)
1704 static int index = 0;
1705 char label[32];
1707 if (strcmp(devname, "none") == 0)
1708 return 0;
1709 if (index == MAX_PARALLEL_PORTS) {
1710 fprintf(stderr, "qemu: too many parallel ports\n");
1711 exit(1);
1713 snprintf(label, sizeof(label), "parallel%d", index);
1714 parallel_hds[index] = qemu_chr_open(label, devname, NULL);
1715 if (!parallel_hds[index]) {
1716 fprintf(stderr, "qemu: could not open parallel device '%s': %s\n",
1717 devname, strerror(errno));
1718 return -1;
1720 index++;
1721 return 0;
1724 static int virtcon_parse(const char *devname)
1726 static int index = 0;
1727 char label[32];
1728 QemuOpts *bus_opts, *dev_opts;
1730 if (strcmp(devname, "none") == 0)
1731 return 0;
1732 if (index == MAX_VIRTIO_CONSOLES) {
1733 fprintf(stderr, "qemu: too many virtio consoles\n");
1734 exit(1);
1737 bus_opts = qemu_opts_create(&qemu_device_opts, NULL, 0);
1738 qemu_opt_set(bus_opts, "driver", "virtio-serial");
1740 dev_opts = qemu_opts_create(&qemu_device_opts, NULL, 0);
1741 qemu_opt_set(dev_opts, "driver", "virtconsole");
1743 snprintf(label, sizeof(label), "virtcon%d", index);
1744 virtcon_hds[index] = qemu_chr_open(label, devname, NULL);
1745 if (!virtcon_hds[index]) {
1746 fprintf(stderr, "qemu: could not open virtio console '%s': %s\n",
1747 devname, strerror(errno));
1748 return -1;
1750 qemu_opt_set(dev_opts, "chardev", label);
1752 index++;
1753 return 0;
1756 static int debugcon_parse(const char *devname)
1758 QemuOpts *opts;
1760 if (!qemu_chr_open("debugcon", devname, NULL)) {
1761 exit(1);
1763 opts = qemu_opts_create(&qemu_device_opts, "debugcon", 1);
1764 if (!opts) {
1765 fprintf(stderr, "qemu: already have a debugcon device\n");
1766 exit(1);
1768 qemu_opt_set(opts, "driver", "isa-debugcon");
1769 qemu_opt_set(opts, "chardev", "debugcon");
1770 return 0;
1773 void qemu_add_exit_notifier(Notifier *notify)
1775 notifier_list_add(&exit_notifiers, notify);
1778 void qemu_remove_exit_notifier(Notifier *notify)
1780 notifier_list_remove(&exit_notifiers, notify);
1783 static void qemu_run_exit_notifiers(void)
1785 notifier_list_notify(&exit_notifiers);
1788 static const QEMUOption *lookup_opt(int argc, char **argv,
1789 const char **poptarg, int *poptind)
1791 const QEMUOption *popt;
1792 int optind = *poptind;
1793 char *r = argv[optind];
1794 const char *optarg;
1796 loc_set_cmdline(argv, optind, 1);
1797 optind++;
1798 /* Treat --foo the same as -foo. */
1799 if (r[1] == '-')
1800 r++;
1801 popt = qemu_options;
1802 for(;;) {
1803 if (!popt->name) {
1804 error_report("invalid option");
1805 exit(1);
1807 if (!strcmp(popt->name, r + 1))
1808 break;
1809 popt++;
1811 if (popt->flags & HAS_ARG) {
1812 if (optind >= argc) {
1813 error_report("requires an argument");
1814 exit(1);
1816 optarg = argv[optind++];
1817 loc_set_cmdline(argv, optind - 2, 2);
1818 } else {
1819 optarg = NULL;
1822 *poptarg = optarg;
1823 *poptind = optind;
1825 return popt;
1828 int main(int argc, char **argv, char **envp)
1830 const char *gdbstub_dev = NULL;
1831 int i;
1832 int snapshot, linux_boot;
1833 const char *icount_option = NULL;
1834 const char *initrd_filename;
1835 const char *kernel_filename, *kernel_cmdline;
1836 char boot_devices[33] = "cad"; /* default to HD->floppy->CD-ROM */
1837 DisplayState *ds;
1838 DisplayChangeListener *dcl;
1839 int cyls, heads, secs, translation;
1840 QemuOpts *hda_opts = NULL, *opts;
1841 int optind;
1842 const char *optarg;
1843 const char *loadvm = NULL;
1844 QEMUMachine *machine;
1845 const char *cpu_model;
1846 int tb_size;
1847 const char *pid_file = NULL;
1848 const char *incoming = NULL;
1849 int show_vnc_port = 0;
1850 int defconfig = 1;
1852 atexit(qemu_run_exit_notifiers);
1853 error_set_progname(argv[0]);
1855 init_clocks();
1857 qemu_cache_utils_init(envp);
1859 QLIST_INIT (&vm_change_state_head);
1860 os_setup_early_signal_handling();
1862 module_call_init(MODULE_INIT_MACHINE);
1863 machine = find_default_machine();
1864 cpu_model = NULL;
1865 initrd_filename = NULL;
1866 ram_size = 0;
1867 snapshot = 0;
1868 kernel_filename = NULL;
1869 kernel_cmdline = "";
1870 cyls = heads = secs = 0;
1871 translation = BIOS_ATA_TRANSLATION_AUTO;
1873 for (i = 0; i < MAX_NODES; i++) {
1874 node_mem[i] = 0;
1875 node_cpumask[i] = 0;
1878 assigned_devices_index = 0;
1880 nb_numa_nodes = 0;
1881 nb_nics = 0;
1883 tb_size = 0;
1884 autostart= 1;
1886 /* first pass of option parsing */
1887 optind = 1;
1888 while (optind < argc) {
1889 if (argv[optind][0] != '-') {
1890 /* disk image */
1891 optind++;
1892 continue;
1893 } else {
1894 const QEMUOption *popt;
1896 popt = lookup_opt(argc, argv, &optarg, &optind);
1897 switch (popt->index) {
1898 case QEMU_OPTION_nodefconfig:
1899 defconfig=0;
1900 break;
1905 if (defconfig) {
1906 int ret;
1908 ret = qemu_read_config_file(CONFIG_QEMU_CONFDIR "/qemu.conf");
1909 if (ret < 0 && ret != -ENOENT) {
1910 exit(1);
1913 ret = qemu_read_config_file(arch_config_name);
1914 if (ret < 0 && ret != -ENOENT) {
1915 exit(1);
1918 cpudef_init();
1920 /* second pass of option parsing */
1921 optind = 1;
1922 for(;;) {
1923 if (optind >= argc)
1924 break;
1925 if (argv[optind][0] != '-') {
1926 hda_opts = drive_add(argv[optind++], HD_ALIAS, 0);
1927 } else {
1928 const QEMUOption *popt;
1930 popt = lookup_opt(argc, argv, &optarg, &optind);
1931 if (!(popt->arch_mask & arch_type)) {
1932 printf("Option %s not supported for this target\n", popt->name);
1933 exit(1);
1935 switch(popt->index) {
1936 case QEMU_OPTION_M:
1937 machine = find_machine(optarg);
1938 if (!machine) {
1939 QEMUMachine *m;
1940 printf("Supported machines are:\n");
1941 for(m = first_machine; m != NULL; m = m->next) {
1942 if (m->alias)
1943 printf("%-10s %s (alias of %s)\n",
1944 m->alias, m->desc, m->name);
1945 printf("%-10s %s%s\n",
1946 m->name, m->desc,
1947 m->is_default ? " (default)" : "");
1949 exit(*optarg != '?');
1951 break;
1952 case QEMU_OPTION_cpu:
1953 /* hw initialization will check this */
1954 if (*optarg == '?') {
1955 list_cpus(stdout, &fprintf, optarg);
1956 exit(0);
1957 } else {
1958 cpu_model = optarg;
1960 break;
1961 case QEMU_OPTION_initrd:
1962 initrd_filename = optarg;
1963 break;
1964 case QEMU_OPTION_hda:
1965 if (cyls == 0)
1966 hda_opts = drive_add(optarg, HD_ALIAS, 0);
1967 else
1968 hda_opts = drive_add(optarg, HD_ALIAS
1969 ",cyls=%d,heads=%d,secs=%d%s",
1970 0, cyls, heads, secs,
1971 translation == BIOS_ATA_TRANSLATION_LBA ?
1972 ",trans=lba" :
1973 translation == BIOS_ATA_TRANSLATION_NONE ?
1974 ",trans=none" : "");
1975 break;
1976 case QEMU_OPTION_hdb:
1977 case QEMU_OPTION_hdc:
1978 case QEMU_OPTION_hdd:
1979 drive_add(optarg, HD_ALIAS, popt->index - QEMU_OPTION_hda);
1980 break;
1981 case QEMU_OPTION_drive:
1982 drive_add(NULL, "%s", optarg);
1983 break;
1984 case QEMU_OPTION_set:
1985 if (qemu_set_option(optarg) != 0)
1986 exit(1);
1987 break;
1988 case QEMU_OPTION_global:
1989 if (qemu_global_option(optarg) != 0)
1990 exit(1);
1991 break;
1992 case QEMU_OPTION_mtdblock:
1993 drive_add(optarg, MTD_ALIAS);
1994 break;
1995 case QEMU_OPTION_sd:
1996 drive_add(optarg, SD_ALIAS);
1997 break;
1998 case QEMU_OPTION_pflash:
1999 drive_add(optarg, PFLASH_ALIAS);
2000 break;
2001 case QEMU_OPTION_snapshot:
2002 snapshot = 1;
2003 break;
2004 case QEMU_OPTION_hdachs:
2006 const char *p;
2007 p = optarg;
2008 cyls = strtol(p, (char **)&p, 0);
2009 if (cyls < 1 || cyls > 16383)
2010 goto chs_fail;
2011 if (*p != ',')
2012 goto chs_fail;
2013 p++;
2014 heads = strtol(p, (char **)&p, 0);
2015 if (heads < 1 || heads > 16)
2016 goto chs_fail;
2017 if (*p != ',')
2018 goto chs_fail;
2019 p++;
2020 secs = strtol(p, (char **)&p, 0);
2021 if (secs < 1 || secs > 63)
2022 goto chs_fail;
2023 if (*p == ',') {
2024 p++;
2025 if (!strcmp(p, "none"))
2026 translation = BIOS_ATA_TRANSLATION_NONE;
2027 else if (!strcmp(p, "lba"))
2028 translation = BIOS_ATA_TRANSLATION_LBA;
2029 else if (!strcmp(p, "auto"))
2030 translation = BIOS_ATA_TRANSLATION_AUTO;
2031 else
2032 goto chs_fail;
2033 } else if (*p != '\0') {
2034 chs_fail:
2035 fprintf(stderr, "qemu: invalid physical CHS format\n");
2036 exit(1);
2038 if (hda_opts != NULL) {
2039 char num[16];
2040 snprintf(num, sizeof(num), "%d", cyls);
2041 qemu_opt_set(hda_opts, "cyls", num);
2042 snprintf(num, sizeof(num), "%d", heads);
2043 qemu_opt_set(hda_opts, "heads", num);
2044 snprintf(num, sizeof(num), "%d", secs);
2045 qemu_opt_set(hda_opts, "secs", num);
2046 if (translation == BIOS_ATA_TRANSLATION_LBA)
2047 qemu_opt_set(hda_opts, "trans", "lba");
2048 if (translation == BIOS_ATA_TRANSLATION_NONE)
2049 qemu_opt_set(hda_opts, "trans", "none");
2052 break;
2053 case QEMU_OPTION_numa:
2054 if (nb_numa_nodes >= MAX_NODES) {
2055 fprintf(stderr, "qemu: too many NUMA nodes\n");
2056 exit(1);
2058 numa_add(optarg);
2059 break;
2060 case QEMU_OPTION_nographic:
2061 display_type = DT_NOGRAPHIC;
2062 break;
2063 #ifdef CONFIG_CURSES
2064 case QEMU_OPTION_curses:
2065 display_type = DT_CURSES;
2066 break;
2067 #endif
2068 case QEMU_OPTION_portrait:
2069 graphic_rotate = 1;
2070 break;
2071 case QEMU_OPTION_kernel:
2072 kernel_filename = optarg;
2073 break;
2074 case QEMU_OPTION_append:
2075 kernel_cmdline = optarg;
2076 break;
2077 case QEMU_OPTION_cdrom:
2078 drive_add(optarg, CDROM_ALIAS);
2079 break;
2080 case QEMU_OPTION_boot:
2082 static const char * const params[] = {
2083 "order", "once", "menu", NULL
2085 char buf[sizeof(boot_devices)];
2086 char *standard_boot_devices;
2087 int legacy = 0;
2089 if (!strchr(optarg, '=')) {
2090 legacy = 1;
2091 pstrcpy(buf, sizeof(buf), optarg);
2092 } else if (check_params(buf, sizeof(buf), params, optarg) < 0) {
2093 fprintf(stderr,
2094 "qemu: unknown boot parameter '%s' in '%s'\n",
2095 buf, optarg);
2096 exit(1);
2099 if (legacy ||
2100 get_param_value(buf, sizeof(buf), "order", optarg)) {
2101 validate_bootdevices(buf);
2102 pstrcpy(boot_devices, sizeof(boot_devices), buf);
2104 if (!legacy) {
2105 if (get_param_value(buf, sizeof(buf),
2106 "once", optarg)) {
2107 validate_bootdevices(buf);
2108 standard_boot_devices = qemu_strdup(boot_devices);
2109 pstrcpy(boot_devices, sizeof(boot_devices), buf);
2110 qemu_register_reset(restore_boot_devices,
2111 standard_boot_devices);
2113 if (get_param_value(buf, sizeof(buf),
2114 "menu", optarg)) {
2115 if (!strcmp(buf, "on")) {
2116 boot_menu = 1;
2117 } else if (!strcmp(buf, "off")) {
2118 boot_menu = 0;
2119 } else {
2120 fprintf(stderr,
2121 "qemu: invalid option value '%s'\n",
2122 buf);
2123 exit(1);
2128 break;
2129 case QEMU_OPTION_fda:
2130 case QEMU_OPTION_fdb:
2131 drive_add(optarg, FD_ALIAS, popt->index - QEMU_OPTION_fda);
2132 break;
2133 case QEMU_OPTION_no_fd_bootchk:
2134 fd_bootchk = 0;
2135 break;
2136 case QEMU_OPTION_netdev:
2137 if (net_client_parse(&qemu_netdev_opts, optarg) == -1) {
2138 exit(1);
2140 break;
2141 case QEMU_OPTION_net:
2142 if (net_client_parse(&qemu_net_opts, optarg) == -1) {
2143 exit(1);
2145 break;
2146 #ifdef CONFIG_SLIRP
2147 case QEMU_OPTION_tftp:
2148 legacy_tftp_prefix = optarg;
2149 break;
2150 case QEMU_OPTION_bootp:
2151 legacy_bootp_filename = optarg;
2152 break;
2153 case QEMU_OPTION_redir:
2154 if (net_slirp_redir(optarg) < 0)
2155 exit(1);
2156 break;
2157 #endif
2158 case QEMU_OPTION_bt:
2159 add_device_config(DEV_BT, optarg);
2160 break;
2161 case QEMU_OPTION_audio_help:
2162 if (!(audio_available())) {
2163 printf("Option %s not supported for this target\n", popt->name);
2164 exit(1);
2166 AUD_help ();
2167 exit (0);
2168 break;
2169 case QEMU_OPTION_soundhw:
2170 if (!(audio_available())) {
2171 printf("Option %s not supported for this target\n", popt->name);
2172 exit(1);
2174 select_soundhw (optarg);
2175 break;
2176 case QEMU_OPTION_h:
2177 help(0);
2178 break;
2179 case QEMU_OPTION_version:
2180 version();
2181 exit(0);
2182 break;
2183 case QEMU_OPTION_m: {
2184 uint64_t value;
2185 char *ptr;
2187 value = strtoul(optarg, &ptr, 10);
2188 switch (*ptr) {
2189 case 0: case 'M': case 'm':
2190 value <<= 20;
2191 break;
2192 case 'G': case 'g':
2193 value <<= 30;
2194 break;
2195 default:
2196 fprintf(stderr, "qemu: invalid ram size: %s\n", optarg);
2197 exit(1);
2200 /* On 32-bit hosts, QEMU is limited by virtual address space */
2201 if (value > (2047 << 20) && HOST_LONG_BITS == 32) {
2202 fprintf(stderr, "qemu: at most 2047 MB RAM can be simulated\n");
2203 exit(1);
2205 if (value != (uint64_t)(ram_addr_t)value) {
2206 fprintf(stderr, "qemu: ram size too large\n");
2207 exit(1);
2209 ram_size = value;
2210 break;
2212 case QEMU_OPTION_mempath:
2213 mem_path = optarg;
2214 break;
2215 #ifdef MAP_POPULATE
2216 case QEMU_OPTION_mem_prealloc:
2217 mem_prealloc = 1;
2218 break;
2219 #endif
2220 case QEMU_OPTION_d:
2221 set_cpu_log(optarg);
2222 break;
2223 case QEMU_OPTION_s:
2224 gdbstub_dev = "tcp::" DEFAULT_GDBSTUB_PORT;
2225 break;
2226 case QEMU_OPTION_gdb:
2227 gdbstub_dev = optarg;
2228 break;
2229 case QEMU_OPTION_L:
2230 data_dir = optarg;
2231 break;
2232 case QEMU_OPTION_bios:
2233 bios_name = optarg;
2234 break;
2235 case QEMU_OPTION_singlestep:
2236 singlestep = 1;
2237 break;
2238 case QEMU_OPTION_S:
2239 autostart = 0;
2240 break;
2241 case QEMU_OPTION_k:
2242 keyboard_layout = optarg;
2243 break;
2244 case QEMU_OPTION_localtime:
2245 rtc_utc = 0;
2246 break;
2247 case QEMU_OPTION_vga:
2248 select_vgahw (optarg);
2249 break;
2250 case QEMU_OPTION_g:
2252 const char *p;
2253 int w, h, depth;
2254 p = optarg;
2255 w = strtol(p, (char **)&p, 10);
2256 if (w <= 0) {
2257 graphic_error:
2258 fprintf(stderr, "qemu: invalid resolution or depth\n");
2259 exit(1);
2261 if (*p != 'x')
2262 goto graphic_error;
2263 p++;
2264 h = strtol(p, (char **)&p, 10);
2265 if (h <= 0)
2266 goto graphic_error;
2267 if (*p == 'x') {
2268 p++;
2269 depth = strtol(p, (char **)&p, 10);
2270 if (depth != 8 && depth != 15 && depth != 16 &&
2271 depth != 24 && depth != 32)
2272 goto graphic_error;
2273 } else if (*p == '\0') {
2274 depth = graphic_depth;
2275 } else {
2276 goto graphic_error;
2279 graphic_width = w;
2280 graphic_height = h;
2281 graphic_depth = depth;
2283 break;
2284 case QEMU_OPTION_echr:
2286 char *r;
2287 term_escape_char = strtol(optarg, &r, 0);
2288 if (r == optarg)
2289 printf("Bad argument to echr\n");
2290 break;
2292 case QEMU_OPTION_monitor:
2293 monitor_parse(optarg, "readline");
2294 default_monitor = 0;
2295 break;
2296 case QEMU_OPTION_qmp:
2297 monitor_parse(optarg, "control");
2298 default_monitor = 0;
2299 break;
2300 case QEMU_OPTION_mon:
2301 opts = qemu_opts_parse(&qemu_mon_opts, optarg, 1);
2302 if (!opts) {
2303 exit(1);
2305 default_monitor = 0;
2306 break;
2307 case QEMU_OPTION_chardev:
2308 opts = qemu_opts_parse(&qemu_chardev_opts, optarg, 1);
2309 if (!opts) {
2310 exit(1);
2312 break;
2313 #ifdef CONFIG_LINUX
2314 case QEMU_OPTION_fsdev:
2315 opts = qemu_opts_parse(&qemu_fsdev_opts, optarg, 1);
2316 if (!opts) {
2317 fprintf(stderr, "parse error: %s\n", optarg);
2318 exit(1);
2320 break;
2321 case QEMU_OPTION_virtfs: {
2322 char *arg_fsdev = NULL;
2323 char *arg_9p = NULL;
2324 int len = 0;
2326 opts = qemu_opts_parse(&qemu_virtfs_opts, optarg, 1);
2327 if (!opts) {
2328 fprintf(stderr, "parse error: %s\n", optarg);
2329 exit(1);
2332 len = strlen(",id=,path=");
2333 len += strlen(qemu_opt_get(opts, "fstype"));
2334 len += strlen(qemu_opt_get(opts, "mount_tag"));
2335 len += strlen(qemu_opt_get(opts, "path"));
2336 arg_fsdev = qemu_malloc((len + 1) * sizeof(*arg_fsdev));
2338 if (!arg_fsdev) {
2339 fprintf(stderr, "No memory to parse -fsdev for %s\n",
2340 optarg);
2341 exit(1);
2344 sprintf(arg_fsdev, "%s,id=%s,path=%s",
2345 qemu_opt_get(opts, "fstype"),
2346 qemu_opt_get(opts, "mount_tag"),
2347 qemu_opt_get(opts, "path"));
2349 len = strlen("virtio-9p-pci,fsdev=,mount_tag=");
2350 len += 2*strlen(qemu_opt_get(opts, "mount_tag"));
2351 arg_9p = qemu_malloc((len + 1) * sizeof(*arg_9p));
2353 if (!arg_9p) {
2354 fprintf(stderr, "No memory to parse -device for %s\n",
2355 optarg);
2356 exit(1);
2359 sprintf(arg_9p, "virtio-9p-pci,fsdev=%s,mount_tag=%s",
2360 qemu_opt_get(opts, "mount_tag"),
2361 qemu_opt_get(opts, "mount_tag"));
2363 if (!qemu_opts_parse(&qemu_fsdev_opts, arg_fsdev, 1)) {
2364 fprintf(stderr, "parse error [fsdev]: %s\n", optarg);
2365 exit(1);
2368 if (!qemu_opts_parse(&qemu_device_opts, arg_9p, 1)) {
2369 fprintf(stderr, "parse error [device]: %s\n", optarg);
2370 exit(1);
2373 qemu_free(arg_fsdev);
2374 qemu_free(arg_9p);
2375 break;
2377 #endif
2378 case QEMU_OPTION_serial:
2379 add_device_config(DEV_SERIAL, optarg);
2380 default_serial = 0;
2381 if (strncmp(optarg, "mon:", 4) == 0) {
2382 default_monitor = 0;
2384 break;
2385 case QEMU_OPTION_watchdog:
2386 if (watchdog) {
2387 fprintf(stderr,
2388 "qemu: only one watchdog option may be given\n");
2389 return 1;
2391 watchdog = optarg;
2392 break;
2393 case QEMU_OPTION_watchdog_action:
2394 if (select_watchdog_action(optarg) == -1) {
2395 fprintf(stderr, "Unknown -watchdog-action parameter\n");
2396 exit(1);
2398 break;
2399 case QEMU_OPTION_virtiocon:
2400 add_device_config(DEV_VIRTCON, optarg);
2401 default_virtcon = 0;
2402 if (strncmp(optarg, "mon:", 4) == 0) {
2403 default_monitor = 0;
2405 break;
2406 case QEMU_OPTION_parallel:
2407 add_device_config(DEV_PARALLEL, optarg);
2408 default_parallel = 0;
2409 if (strncmp(optarg, "mon:", 4) == 0) {
2410 default_monitor = 0;
2412 break;
2413 case QEMU_OPTION_debugcon:
2414 add_device_config(DEV_DEBUGCON, optarg);
2415 break;
2416 case QEMU_OPTION_loadvm:
2417 loadvm = optarg;
2418 break;
2419 case QEMU_OPTION_full_screen:
2420 full_screen = 1;
2421 break;
2422 #ifdef CONFIG_SDL
2423 case QEMU_OPTION_no_frame:
2424 no_frame = 1;
2425 break;
2426 case QEMU_OPTION_alt_grab:
2427 alt_grab = 1;
2428 break;
2429 case QEMU_OPTION_ctrl_grab:
2430 ctrl_grab = 1;
2431 break;
2432 case QEMU_OPTION_no_quit:
2433 no_quit = 1;
2434 break;
2435 case QEMU_OPTION_sdl:
2436 display_type = DT_SDL;
2437 break;
2438 #endif
2439 case QEMU_OPTION_pidfile:
2440 pid_file = optarg;
2441 break;
2442 case QEMU_OPTION_win2k_hack:
2443 win2k_install_hack = 1;
2444 break;
2445 case QEMU_OPTION_rtc_td_hack:
2446 rtc_td_hack = 1;
2447 break;
2448 case QEMU_OPTION_acpitable:
2449 do_acpitable_option(optarg);
2450 break;
2451 case QEMU_OPTION_smbios:
2452 do_smbios_option(optarg);
2453 break;
2454 #ifdef KVM_UPSTREAM
2455 case QEMU_OPTION_enable_kvm:
2456 kvm_allowed = 1;
2457 #endif
2458 break;
2459 case QEMU_OPTION_no_kvm:
2460 kvm_allowed = 0;
2461 break;
2462 case QEMU_OPTION_no_kvm_irqchip: {
2463 kvm_irqchip = 0;
2464 kvm_pit = 0;
2465 break;
2467 case QEMU_OPTION_no_kvm_pit: {
2468 kvm_pit = 0;
2469 break;
2471 case QEMU_OPTION_no_kvm_pit_reinjection: {
2472 kvm_pit_reinject = 0;
2473 break;
2475 case QEMU_OPTION_enable_nesting: {
2476 kvm_nested = 1;
2477 break;
2479 #if defined(TARGET_I386) || defined(TARGET_X86_64) || defined(TARGET_IA64) || defined(__linux__)
2480 case QEMU_OPTION_pcidevice:
2481 if (assigned_devices_index >= MAX_DEV_ASSIGN_CMDLINE) {
2482 fprintf(stderr, "Too many assigned devices\n");
2483 exit(1);
2485 assigned_devices[assigned_devices_index] = optarg;
2486 assigned_devices_index++;
2487 break;
2488 #endif
2489 case QEMU_OPTION_usb:
2490 usb_enabled = 1;
2491 break;
2492 case QEMU_OPTION_usbdevice:
2493 usb_enabled = 1;
2494 add_device_config(DEV_USB, optarg);
2495 break;
2496 case QEMU_OPTION_device:
2497 if (!qemu_opts_parse(&qemu_device_opts, optarg, 1)) {
2498 exit(1);
2500 break;
2501 case QEMU_OPTION_smp:
2502 smp_parse(optarg);
2503 if (smp_cpus < 1) {
2504 fprintf(stderr, "Invalid number of CPUs\n");
2505 exit(1);
2507 if (max_cpus < smp_cpus) {
2508 fprintf(stderr, "maxcpus must be equal to or greater than "
2509 "smp\n");
2510 exit(1);
2512 if (max_cpus > 255) {
2513 fprintf(stderr, "Unsupported number of maxcpus\n");
2514 exit(1);
2516 break;
2517 case QEMU_OPTION_vnc:
2518 display_type = DT_VNC;
2519 vnc_display = optarg;
2520 break;
2521 case QEMU_OPTION_no_acpi:
2522 acpi_enabled = 0;
2523 break;
2524 case QEMU_OPTION_no_hpet:
2525 no_hpet = 1;
2526 break;
2527 case QEMU_OPTION_balloon:
2528 if (balloon_parse(optarg) < 0) {
2529 fprintf(stderr, "Unknown -balloon argument %s\n", optarg);
2530 exit(1);
2532 break;
2533 case QEMU_OPTION_no_reboot:
2534 no_reboot = 1;
2535 break;
2536 case QEMU_OPTION_no_shutdown:
2537 no_shutdown = 1;
2538 break;
2539 case QEMU_OPTION_show_cursor:
2540 cursor_hide = 0;
2541 break;
2542 case QEMU_OPTION_uuid:
2543 if(qemu_uuid_parse(optarg, qemu_uuid) < 0) {
2544 fprintf(stderr, "Fail to parse UUID string."
2545 " Wrong format.\n");
2546 exit(1);
2548 break;
2549 case QEMU_OPTION_option_rom:
2550 if (nb_option_roms >= MAX_OPTION_ROMS) {
2551 fprintf(stderr, "Too many option ROMs\n");
2552 exit(1);
2554 option_rom[nb_option_roms] = optarg;
2555 nb_option_roms++;
2556 break;
2557 case QEMU_OPTION_semihosting:
2558 semihosting_enabled = 1;
2559 break;
2560 case QEMU_OPTION_tdf:
2561 time_drift_fix = 1;
2562 break;
2563 case QEMU_OPTION_kvm_shadow_memory:
2564 kvm_shadow_memory = (int64_t)atoi(optarg) * 1024 * 1024 / 4096;
2565 break;
2566 case QEMU_OPTION_name:
2567 qemu_name = qemu_strdup(optarg);
2569 char *p = strchr(qemu_name, ',');
2570 if (p != NULL) {
2571 *p++ = 0;
2572 if (strncmp(p, "process=", 8)) {
2573 fprintf(stderr, "Unknown subargument %s to -name", p);
2574 exit(1);
2576 p += 8;
2577 os_set_proc_name(p);
2580 break;
2581 case QEMU_OPTION_prom_env:
2582 if (nb_prom_envs >= MAX_PROM_ENVS) {
2583 fprintf(stderr, "Too many prom variables\n");
2584 exit(1);
2586 prom_envs[nb_prom_envs] = optarg;
2587 nb_prom_envs++;
2588 break;
2589 case QEMU_OPTION_old_param:
2590 old_param = 1;
2591 break;
2592 case QEMU_OPTION_clock:
2593 configure_alarms(optarg);
2594 break;
2595 case QEMU_OPTION_startdate:
2596 configure_rtc_date_offset(optarg, 1);
2597 break;
2598 case QEMU_OPTION_rtc:
2599 opts = qemu_opts_parse(&qemu_rtc_opts, optarg, 0);
2600 if (!opts) {
2601 exit(1);
2603 configure_rtc(opts);
2604 break;
2605 case QEMU_OPTION_tb_size:
2606 tb_size = strtol(optarg, NULL, 0);
2607 if (tb_size < 0)
2608 tb_size = 0;
2609 break;
2610 case QEMU_OPTION_icount:
2611 icount_option = optarg;
2612 break;
2613 case QEMU_OPTION_incoming:
2614 incoming = optarg;
2615 break;
2616 case QEMU_OPTION_nodefaults:
2617 default_serial = 0;
2618 default_parallel = 0;
2619 default_virtcon = 0;
2620 default_monitor = 0;
2621 default_vga = 0;
2622 default_net = 0;
2623 default_floppy = 0;
2624 default_cdrom = 0;
2625 default_sdcard = 0;
2626 break;
2627 #ifndef _WIN32
2628 case QEMU_OPTION_nvram:
2629 nvram = optarg;
2630 break;
2631 #endif
2632 case QEMU_OPTION_xen_domid:
2633 if (!(xen_available())) {
2634 printf("Option %s not supported for this target\n", popt->name);
2635 exit(1);
2637 xen_domid = atoi(optarg);
2638 break;
2639 case QEMU_OPTION_xen_create:
2640 if (!(xen_available())) {
2641 printf("Option %s not supported for this target\n", popt->name);
2642 exit(1);
2644 xen_mode = XEN_CREATE;
2645 break;
2646 case QEMU_OPTION_xen_attach:
2647 if (!(xen_available())) {
2648 printf("Option %s not supported for this target\n", popt->name);
2649 exit(1);
2651 xen_mode = XEN_ATTACH;
2652 break;
2653 case QEMU_OPTION_readconfig:
2655 int ret = qemu_read_config_file(optarg);
2656 if (ret < 0) {
2657 fprintf(stderr, "read config %s: %s\n", optarg,
2658 strerror(-ret));
2659 exit(1);
2661 break;
2663 case QEMU_OPTION_writeconfig:
2665 FILE *fp;
2666 if (strcmp(optarg, "-") == 0) {
2667 fp = stdout;
2668 } else {
2669 fp = fopen(optarg, "w");
2670 if (fp == NULL) {
2671 fprintf(stderr, "open %s: %s\n", optarg, strerror(errno));
2672 exit(1);
2675 qemu_config_write(fp);
2676 fclose(fp);
2677 break;
2679 default:
2680 os_parse_cmd_args(popt->index, optarg);
2684 loc_set_none();
2686 /* If no data_dir is specified then try to find it relative to the
2687 executable path. */
2688 if (!data_dir) {
2689 data_dir = os_find_datadir(argv[0]);
2691 /* If all else fails use the install patch specified when building. */
2692 if (!data_dir) {
2693 data_dir = CONFIG_QEMU_DATADIR;
2697 * Default to max_cpus = smp_cpus, in case the user doesn't
2698 * specify a max_cpus value.
2700 if (!max_cpus)
2701 max_cpus = smp_cpus;
2703 machine->max_cpus = machine->max_cpus ?: 1; /* Default to UP */
2704 if (smp_cpus > machine->max_cpus) {
2705 fprintf(stderr, "Number of SMP cpus requested (%d), exceeds max cpus "
2706 "supported by machine `%s' (%d)\n", smp_cpus, machine->name,
2707 machine->max_cpus);
2708 exit(1);
2711 qemu_opts_foreach(&qemu_device_opts, default_driver_check, NULL, 0);
2712 qemu_opts_foreach(&qemu_global_opts, default_driver_check, NULL, 0);
2714 if (machine->no_serial) {
2715 default_serial = 0;
2717 if (machine->no_parallel) {
2718 default_parallel = 0;
2720 if (!machine->use_virtcon) {
2721 default_virtcon = 0;
2723 if (machine->no_vga) {
2724 default_vga = 0;
2726 if (machine->no_floppy) {
2727 default_floppy = 0;
2729 if (machine->no_cdrom) {
2730 default_cdrom = 0;
2732 if (machine->no_sdcard) {
2733 default_sdcard = 0;
2736 if (display_type == DT_NOGRAPHIC) {
2737 if (default_parallel)
2738 add_device_config(DEV_PARALLEL, "null");
2739 if (default_serial && default_monitor) {
2740 add_device_config(DEV_SERIAL, "mon:stdio");
2741 } else if (default_virtcon && default_monitor) {
2742 add_device_config(DEV_VIRTCON, "mon:stdio");
2743 } else {
2744 if (default_serial)
2745 add_device_config(DEV_SERIAL, "stdio");
2746 if (default_virtcon)
2747 add_device_config(DEV_VIRTCON, "stdio");
2748 if (default_monitor)
2749 monitor_parse("stdio", "readline");
2751 } else {
2752 if (default_serial)
2753 add_device_config(DEV_SERIAL, "vc:80Cx24C");
2754 if (default_parallel)
2755 add_device_config(DEV_PARALLEL, "vc:80Cx24C");
2756 if (default_monitor)
2757 monitor_parse("vc:80Cx24C", "readline");
2758 if (default_virtcon)
2759 add_device_config(DEV_VIRTCON, "vc:80Cx24C");
2761 if (default_vga)
2762 vga_interface_type = VGA_CIRRUS;
2764 socket_init();
2766 if (qemu_opts_foreach(&qemu_chardev_opts, chardev_init_func, NULL, 1) != 0)
2767 exit(1);
2768 #ifdef CONFIG_LINUX
2769 if (qemu_opts_foreach(&qemu_fsdev_opts, fsdev_init_func, NULL, 1) != 0) {
2770 exit(1);
2772 #endif
2774 os_daemonize();
2776 if (pid_file && qemu_create_pidfile(pid_file) != 0) {
2777 os_pidfile_error();
2778 exit(1);
2781 if (kvm_allowed) {
2782 int ret = kvm_init(smp_cpus);
2783 if (ret < 0) {
2784 #if defined(KVM_UPSTREAM) || defined(CONFIG_NO_CPU_EMULATION)
2785 if (!kvm_available()) {
2786 printf("KVM not supported for this target\n");
2787 } else {
2788 fprintf(stderr, "failed to initialize KVM: %s\n", strerror(-ret));
2790 exit(1);
2791 #endif
2792 #ifdef CONFIG_KVM
2793 fprintf(stderr, "Could not initialize KVM, will disable KVM support\n");
2794 kvm_allowed = 0;
2795 #endif
2799 if (qemu_init_main_loop()) {
2800 fprintf(stderr, "qemu_init_main_loop failed\n");
2801 exit(1);
2803 linux_boot = (kernel_filename != NULL);
2805 if (!linux_boot && *kernel_cmdline != '\0') {
2806 fprintf(stderr, "-append only allowed with -kernel option\n");
2807 exit(1);
2810 if (!linux_boot && initrd_filename != NULL) {
2811 fprintf(stderr, "-initrd only allowed with -kernel option\n");
2812 exit(1);
2815 os_set_line_buffering();
2817 if (init_timer_alarm() < 0) {
2818 fprintf(stderr, "could not initialize alarm timer\n");
2819 exit(1);
2821 configure_icount(icount_option);
2823 if (net_init_clients() < 0) {
2824 exit(1);
2827 /* init the bluetooth world */
2828 if (foreach_device_config(DEV_BT, bt_parse))
2829 exit(1);
2831 /* init the memory */
2832 if (ram_size == 0)
2833 ram_size = DEFAULT_RAM_SIZE * 1024 * 1024;
2835 /* init the dynamic translator */
2836 cpu_exec_init_all(tb_size * 1024 * 1024);
2838 bdrv_init_with_whitelist();
2840 blk_mig_init();
2842 if (default_cdrom) {
2843 /* we always create the cdrom drive, even if no disk is there */
2844 drive_add(NULL, CDROM_ALIAS);
2847 if (default_floppy) {
2848 /* we always create at least one floppy */
2849 drive_add(NULL, FD_ALIAS, 0);
2852 if (default_sdcard) {
2853 /* we always create one sd slot, even if no card is in it */
2854 drive_add(NULL, SD_ALIAS);
2857 /* open the virtual block devices */
2858 if (snapshot)
2859 qemu_opts_foreach(&qemu_drive_opts, drive_enable_snapshot, NULL, 0);
2860 if (qemu_opts_foreach(&qemu_drive_opts, drive_init_func, &machine->use_scsi, 1) != 0)
2861 exit(1);
2863 register_savevm_live("ram", 0, 3, NULL, ram_save_live, NULL,
2864 ram_load, NULL);
2866 if (nb_numa_nodes > 0) {
2867 int i;
2869 if (nb_numa_nodes > smp_cpus) {
2870 nb_numa_nodes = smp_cpus;
2873 /* If no memory size if given for any node, assume the default case
2874 * and distribute the available memory equally across all nodes
2876 for (i = 0; i < nb_numa_nodes; i++) {
2877 if (node_mem[i] != 0)
2878 break;
2880 if (i == nb_numa_nodes) {
2881 uint64_t usedmem = 0;
2883 /* On Linux, the each node's border has to be 8MB aligned,
2884 * the final node gets the rest.
2886 for (i = 0; i < nb_numa_nodes - 1; i++) {
2887 node_mem[i] = (ram_size / nb_numa_nodes) & ~((1 << 23UL) - 1);
2888 usedmem += node_mem[i];
2890 node_mem[i] = ram_size - usedmem;
2893 for (i = 0; i < nb_numa_nodes; i++) {
2894 if (node_cpumask[i] != 0)
2895 break;
2897 /* assigning the VCPUs round-robin is easier to implement, guest OSes
2898 * must cope with this anyway, because there are BIOSes out there in
2899 * real machines which also use this scheme.
2901 if (i == nb_numa_nodes) {
2902 for (i = 0; i < smp_cpus; i++) {
2903 node_cpumask[i % nb_numa_nodes] |= 1 << i;
2908 if (qemu_opts_foreach(&qemu_mon_opts, mon_init_func, NULL, 1) != 0) {
2909 exit(1);
2912 if (foreach_device_config(DEV_SERIAL, serial_parse) < 0)
2913 exit(1);
2914 if (foreach_device_config(DEV_PARALLEL, parallel_parse) < 0)
2915 exit(1);
2916 if (foreach_device_config(DEV_VIRTCON, virtcon_parse) < 0)
2917 exit(1);
2918 if (foreach_device_config(DEV_DEBUGCON, debugcon_parse) < 0)
2919 exit(1);
2921 module_call_init(MODULE_INIT_DEVICE);
2923 if (qemu_opts_foreach(&qemu_device_opts, device_help_func, NULL, 0) != 0)
2924 exit(0);
2926 if (watchdog) {
2927 i = select_watchdog(watchdog);
2928 if (i > 0)
2929 exit (i == 1 ? 1 : 0);
2932 if (machine->compat_props) {
2933 qdev_prop_register_global_list(machine->compat_props);
2935 qemu_add_globals();
2937 machine->init(ram_size, boot_devices,
2938 kernel_filename, kernel_cmdline, initrd_filename, cpu_model);
2940 cpu_synchronize_all_post_init();
2942 /* must be after terminal init, SDL library changes signal handlers */
2943 os_setup_signal_handling();
2945 set_numa_modes();
2947 current_machine = machine;
2949 /* init USB devices */
2950 if (usb_enabled) {
2951 if (foreach_device_config(DEV_USB, usb_parse) < 0)
2952 exit(1);
2955 /* init generic devices */
2956 if (qemu_opts_foreach(&qemu_device_opts, device_init_func, NULL, 1) != 0)
2957 exit(1);
2959 net_check_clients();
2961 /* just use the first displaystate for the moment */
2962 ds = get_displaystate();
2964 if (display_type == DT_DEFAULT) {
2965 #if defined(CONFIG_SDL) || defined(CONFIG_COCOA)
2966 display_type = DT_SDL;
2967 #else
2968 display_type = DT_VNC;
2969 vnc_display = "localhost:0,to=99";
2970 show_vnc_port = 1;
2971 #endif
2975 switch (display_type) {
2976 case DT_NOGRAPHIC:
2977 break;
2978 #if defined(CONFIG_CURSES)
2979 case DT_CURSES:
2980 curses_display_init(ds, full_screen);
2981 break;
2982 #endif
2983 #if defined(CONFIG_SDL)
2984 case DT_SDL:
2985 sdl_display_init(ds, full_screen, no_frame);
2986 break;
2987 #elif defined(CONFIG_COCOA)
2988 case DT_SDL:
2989 cocoa_display_init(ds, full_screen);
2990 break;
2991 #endif
2992 case DT_VNC:
2993 vnc_display_init(ds);
2994 if (vnc_display_open(ds, vnc_display) < 0)
2995 exit(1);
2997 if (show_vnc_port) {
2998 printf("VNC server running on `%s'\n", vnc_display_local_addr(ds));
3000 break;
3001 default:
3002 break;
3004 dpy_resize(ds);
3006 dcl = ds->listeners;
3007 while (dcl != NULL) {
3008 if (dcl->dpy_refresh != NULL) {
3009 ds->gui_timer = qemu_new_timer(rt_clock, gui_update, ds);
3010 qemu_mod_timer(ds->gui_timer, qemu_get_clock(rt_clock));
3011 break;
3013 dcl = dcl->next;
3016 if (display_type == DT_NOGRAPHIC || display_type == DT_VNC) {
3017 nographic_timer = qemu_new_timer(rt_clock, nographic_update, NULL);
3018 qemu_mod_timer(nographic_timer, qemu_get_clock(rt_clock));
3021 text_consoles_set_display(ds);
3023 if (gdbstub_dev && gdbserver_start(gdbstub_dev) < 0) {
3024 fprintf(stderr, "qemu: could not open gdbserver on device '%s'\n",
3025 gdbstub_dev);
3026 exit(1);
3029 qdev_machine_creation_done();
3031 if (rom_load_all() != 0) {
3032 fprintf(stderr, "rom loading failed\n");
3033 exit(1);
3036 qemu_system_reset();
3037 if (loadvm) {
3038 if (load_vmstate(loadvm) < 0) {
3039 autostart = 0;
3043 if (incoming) {
3044 qemu_start_incoming_migration(incoming);
3045 } else if (autostart) {
3046 vm_start();
3049 os_setup_post();
3051 main_loop();
3052 quit_timers();
3053 net_cleanup();
3055 return 0;