Merge commit '666daa68234b5b1758652633cab07d5ca6046a5b' into upstream-merge
[qemu-kvm/amd-iommu.git] / vl.c
blob3a1457627515e17b88cd2d88d4bf0f78d19b958d
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 int kvm_allowed = 1;
246 uint32_t xen_domid;
247 enum xen_mode xen_mode = XEN_EMULATE;
249 static int default_serial = 1;
250 static int default_parallel = 1;
251 static int default_virtcon = 1;
252 static int default_monitor = 1;
253 static int default_vga = 1;
254 static int default_floppy = 1;
255 static int default_cdrom = 1;
256 static int default_sdcard = 1;
258 static struct {
259 const char *driver;
260 int *flag;
261 } default_list[] = {
262 { .driver = "isa-serial", .flag = &default_serial },
263 { .driver = "isa-parallel", .flag = &default_parallel },
264 { .driver = "isa-fdc", .flag = &default_floppy },
265 { .driver = "ide-drive", .flag = &default_cdrom },
266 { .driver = "virtio-serial-pci", .flag = &default_virtcon },
267 { .driver = "virtio-serial-s390", .flag = &default_virtcon },
268 { .driver = "virtio-serial", .flag = &default_virtcon },
269 { .driver = "VGA", .flag = &default_vga },
270 { .driver = "cirrus-vga", .flag = &default_vga },
271 { .driver = "vmware-svga", .flag = &default_vga },
274 static int default_driver_check(QemuOpts *opts, void *opaque)
276 const char *driver = qemu_opt_get(opts, "driver");
277 int i;
279 if (!driver)
280 return 0;
281 for (i = 0; i < ARRAY_SIZE(default_list); i++) {
282 if (strcmp(default_list[i].driver, driver) != 0)
283 continue;
284 *(default_list[i].flag) = 0;
286 return 0;
289 /***********************************************************/
290 /* real time host monotonic timer */
292 /* compute with 96 bit intermediate result: (a*b)/c */
293 uint64_t muldiv64(uint64_t a, uint32_t b, uint32_t c)
295 union {
296 uint64_t ll;
297 struct {
298 #ifdef HOST_WORDS_BIGENDIAN
299 uint32_t high, low;
300 #else
301 uint32_t low, high;
302 #endif
303 } l;
304 } u, res;
305 uint64_t rl, rh;
307 u.ll = a;
308 rl = (uint64_t)u.l.low * (uint64_t)b;
309 rh = (uint64_t)u.l.high * (uint64_t)b;
310 rh += (rl >> 32);
311 res.l.high = rh / c;
312 res.l.low = (((rh % c) << 32) + (rl & 0xffffffff)) / c;
313 return res.ll;
316 /***********************************************************/
317 /* host time/date access */
318 void qemu_get_timedate(struct tm *tm, int offset)
320 time_t ti;
321 struct tm *ret;
323 time(&ti);
324 ti += offset;
325 if (rtc_date_offset == -1) {
326 if (rtc_utc)
327 ret = gmtime(&ti);
328 else
329 ret = localtime(&ti);
330 } else {
331 ti -= rtc_date_offset;
332 ret = gmtime(&ti);
335 memcpy(tm, ret, sizeof(struct tm));
338 int qemu_timedate_diff(struct tm *tm)
340 time_t seconds;
342 if (rtc_date_offset == -1)
343 if (rtc_utc)
344 seconds = mktimegm(tm);
345 else
346 seconds = mktime(tm);
347 else
348 seconds = mktimegm(tm) + rtc_date_offset;
350 return seconds - time(NULL);
353 void rtc_change_mon_event(struct tm *tm)
355 QObject *data;
357 data = qobject_from_jsonf("{ 'offset': %d }", qemu_timedate_diff(tm));
358 monitor_protocol_event(QEVENT_RTC_CHANGE, data);
359 qobject_decref(data);
362 static void configure_rtc_date_offset(const char *startdate, int legacy)
364 time_t rtc_start_date;
365 struct tm tm;
367 if (!strcmp(startdate, "now") && legacy) {
368 rtc_date_offset = -1;
369 } else {
370 if (sscanf(startdate, "%d-%d-%dT%d:%d:%d",
371 &tm.tm_year,
372 &tm.tm_mon,
373 &tm.tm_mday,
374 &tm.tm_hour,
375 &tm.tm_min,
376 &tm.tm_sec) == 6) {
377 /* OK */
378 } else if (sscanf(startdate, "%d-%d-%d",
379 &tm.tm_year,
380 &tm.tm_mon,
381 &tm.tm_mday) == 3) {
382 tm.tm_hour = 0;
383 tm.tm_min = 0;
384 tm.tm_sec = 0;
385 } else {
386 goto date_fail;
388 tm.tm_year -= 1900;
389 tm.tm_mon--;
390 rtc_start_date = mktimegm(&tm);
391 if (rtc_start_date == -1) {
392 date_fail:
393 fprintf(stderr, "Invalid date format. Valid formats are:\n"
394 "'2006-06-17T16:01:21' or '2006-06-17'\n");
395 exit(1);
397 rtc_date_offset = time(NULL) - rtc_start_date;
401 static void configure_rtc(QemuOpts *opts)
403 const char *value;
405 value = qemu_opt_get(opts, "base");
406 if (value) {
407 if (!strcmp(value, "utc")) {
408 rtc_utc = 1;
409 } else if (!strcmp(value, "localtime")) {
410 rtc_utc = 0;
411 } else {
412 configure_rtc_date_offset(value, 0);
415 value = qemu_opt_get(opts, "clock");
416 if (value) {
417 if (!strcmp(value, "host")) {
418 rtc_clock = host_clock;
419 } else if (!strcmp(value, "vm")) {
420 rtc_clock = vm_clock;
421 } else {
422 fprintf(stderr, "qemu: invalid option value '%s'\n", value);
423 exit(1);
426 value = qemu_opt_get(opts, "driftfix");
427 if (value) {
428 if (!strcmp(value, "slew")) {
429 rtc_td_hack = 1;
430 } else if (!strcmp(value, "none")) {
431 rtc_td_hack = 0;
432 } else {
433 fprintf(stderr, "qemu: invalid option value '%s'\n", value);
434 exit(1);
439 /***********************************************************/
440 /* Bluetooth support */
441 static int nb_hcis;
442 static int cur_hci;
443 static struct HCIInfo *hci_table[MAX_NICS];
445 static struct bt_vlan_s {
446 struct bt_scatternet_s net;
447 int id;
448 struct bt_vlan_s *next;
449 } *first_bt_vlan;
451 /* find or alloc a new bluetooth "VLAN" */
452 static struct bt_scatternet_s *qemu_find_bt_vlan(int id)
454 struct bt_vlan_s **pvlan, *vlan;
455 for (vlan = first_bt_vlan; vlan != NULL; vlan = vlan->next) {
456 if (vlan->id == id)
457 return &vlan->net;
459 vlan = qemu_mallocz(sizeof(struct bt_vlan_s));
460 vlan->id = id;
461 pvlan = &first_bt_vlan;
462 while (*pvlan != NULL)
463 pvlan = &(*pvlan)->next;
464 *pvlan = vlan;
465 return &vlan->net;
468 static void null_hci_send(struct HCIInfo *hci, const uint8_t *data, int len)
472 static int null_hci_addr_set(struct HCIInfo *hci, const uint8_t *bd_addr)
474 return -ENOTSUP;
477 static struct HCIInfo null_hci = {
478 .cmd_send = null_hci_send,
479 .sco_send = null_hci_send,
480 .acl_send = null_hci_send,
481 .bdaddr_set = null_hci_addr_set,
484 struct HCIInfo *qemu_next_hci(void)
486 if (cur_hci == nb_hcis)
487 return &null_hci;
489 return hci_table[cur_hci++];
492 static struct HCIInfo *hci_init(const char *str)
494 char *endp;
495 struct bt_scatternet_s *vlan = 0;
497 if (!strcmp(str, "null"))
498 /* null */
499 return &null_hci;
500 else if (!strncmp(str, "host", 4) && (str[4] == '\0' || str[4] == ':'))
501 /* host[:hciN] */
502 return bt_host_hci(str[4] ? str + 5 : "hci0");
503 else if (!strncmp(str, "hci", 3)) {
504 /* hci[,vlan=n] */
505 if (str[3]) {
506 if (!strncmp(str + 3, ",vlan=", 6)) {
507 vlan = qemu_find_bt_vlan(strtol(str + 9, &endp, 0));
508 if (*endp)
509 vlan = 0;
511 } else
512 vlan = qemu_find_bt_vlan(0);
513 if (vlan)
514 return bt_new_hci(vlan);
517 fprintf(stderr, "qemu: Unknown bluetooth HCI `%s'.\n", str);
519 return 0;
522 static int bt_hci_parse(const char *str)
524 struct HCIInfo *hci;
525 bdaddr_t bdaddr;
527 if (nb_hcis >= MAX_NICS) {
528 fprintf(stderr, "qemu: Too many bluetooth HCIs (max %i).\n", MAX_NICS);
529 return -1;
532 hci = hci_init(str);
533 if (!hci)
534 return -1;
536 bdaddr.b[0] = 0x52;
537 bdaddr.b[1] = 0x54;
538 bdaddr.b[2] = 0x00;
539 bdaddr.b[3] = 0x12;
540 bdaddr.b[4] = 0x34;
541 bdaddr.b[5] = 0x56 + nb_hcis;
542 hci->bdaddr_set(hci, bdaddr.b);
544 hci_table[nb_hcis++] = hci;
546 return 0;
549 static void bt_vhci_add(int vlan_id)
551 struct bt_scatternet_s *vlan = qemu_find_bt_vlan(vlan_id);
553 if (!vlan->slave)
554 fprintf(stderr, "qemu: warning: adding a VHCI to "
555 "an empty scatternet %i\n", vlan_id);
557 bt_vhci_init(bt_new_hci(vlan));
560 static struct bt_device_s *bt_device_add(const char *opt)
562 struct bt_scatternet_s *vlan;
563 int vlan_id = 0;
564 char *endp = strstr(opt, ",vlan=");
565 int len = (endp ? endp - opt : strlen(opt)) + 1;
566 char devname[10];
568 pstrcpy(devname, MIN(sizeof(devname), len), opt);
570 if (endp) {
571 vlan_id = strtol(endp + 6, &endp, 0);
572 if (*endp) {
573 fprintf(stderr, "qemu: unrecognised bluetooth vlan Id\n");
574 return 0;
578 vlan = qemu_find_bt_vlan(vlan_id);
580 if (!vlan->slave)
581 fprintf(stderr, "qemu: warning: adding a slave device to "
582 "an empty scatternet %i\n", vlan_id);
584 if (!strcmp(devname, "keyboard"))
585 return bt_keyboard_init(vlan);
587 fprintf(stderr, "qemu: unsupported bluetooth device `%s'\n", devname);
588 return 0;
591 static int bt_parse(const char *opt)
593 const char *endp, *p;
594 int vlan;
596 if (strstart(opt, "hci", &endp)) {
597 if (!*endp || *endp == ',') {
598 if (*endp)
599 if (!strstart(endp, ",vlan=", 0))
600 opt = endp + 1;
602 return bt_hci_parse(opt);
604 } else if (strstart(opt, "vhci", &endp)) {
605 if (!*endp || *endp == ',') {
606 if (*endp) {
607 if (strstart(endp, ",vlan=", &p)) {
608 vlan = strtol(p, (char **) &endp, 0);
609 if (*endp) {
610 fprintf(stderr, "qemu: bad scatternet '%s'\n", p);
611 return 1;
613 } else {
614 fprintf(stderr, "qemu: bad parameter '%s'\n", endp + 1);
615 return 1;
617 } else
618 vlan = 0;
620 bt_vhci_add(vlan);
621 return 0;
623 } else if (strstart(opt, "device:", &endp))
624 return !bt_device_add(endp);
626 fprintf(stderr, "qemu: bad bluetooth parameter '%s'\n", opt);
627 return 1;
630 /***********************************************************/
631 /* QEMU Block devices */
633 #define HD_ALIAS "index=%d,media=disk"
634 #define CDROM_ALIAS "index=2,media=cdrom"
635 #define FD_ALIAS "index=%d,if=floppy"
636 #define PFLASH_ALIAS "if=pflash"
637 #define MTD_ALIAS "if=mtd"
638 #define SD_ALIAS "index=0,if=sd"
640 static int drive_init_func(QemuOpts *opts, void *opaque)
642 int *use_scsi = opaque;
643 int fatal_error = 0;
645 if (drive_init(opts, *use_scsi, &fatal_error) == NULL) {
646 if (fatal_error)
647 return 1;
649 return 0;
652 static int drive_enable_snapshot(QemuOpts *opts, void *opaque)
654 if (NULL == qemu_opt_get(opts, "snapshot")) {
655 qemu_opt_set(opts, "snapshot", "on");
657 return 0;
660 void qemu_register_boot_set(QEMUBootSetHandler *func, void *opaque)
662 boot_set_handler = func;
663 boot_set_opaque = opaque;
666 int qemu_boot_set(const char *boot_devices)
668 if (!boot_set_handler) {
669 return -EINVAL;
671 return boot_set_handler(boot_set_opaque, boot_devices);
674 static void validate_bootdevices(char *devices)
676 /* We just do some generic consistency checks */
677 const char *p;
678 int bitmap = 0;
680 for (p = devices; *p != '\0'; p++) {
681 /* Allowed boot devices are:
682 * a-b: floppy disk drives
683 * c-f: IDE disk drives
684 * g-m: machine implementation dependant drives
685 * n-p: network devices
686 * It's up to each machine implementation to check if the given boot
687 * devices match the actual hardware implementation and firmware
688 * features.
690 if (*p < 'a' || *p > 'p') {
691 fprintf(stderr, "Invalid boot device '%c'\n", *p);
692 exit(1);
694 if (bitmap & (1 << (*p - 'a'))) {
695 fprintf(stderr, "Boot device '%c' was given twice\n", *p);
696 exit(1);
698 bitmap |= 1 << (*p - 'a');
702 static void restore_boot_devices(void *opaque)
704 char *standard_boot_devices = opaque;
705 static int first = 1;
707 /* Restore boot order and remove ourselves after the first boot */
708 if (first) {
709 first = 0;
710 return;
713 qemu_boot_set(standard_boot_devices);
715 qemu_unregister_reset(restore_boot_devices, standard_boot_devices);
716 qemu_free(standard_boot_devices);
719 static void numa_add(const char *optarg)
721 char option[128];
722 char *endptr;
723 unsigned long long value, endvalue;
724 int nodenr;
726 optarg = get_opt_name(option, 128, optarg, ',') + 1;
727 if (!strcmp(option, "node")) {
728 if (get_param_value(option, 128, "nodeid", optarg) == 0) {
729 nodenr = nb_numa_nodes;
730 } else {
731 nodenr = strtoull(option, NULL, 10);
734 if (get_param_value(option, 128, "mem", optarg) == 0) {
735 node_mem[nodenr] = 0;
736 } else {
737 value = strtoull(option, &endptr, 0);
738 switch (*endptr) {
739 case 0: case 'M': case 'm':
740 value <<= 20;
741 break;
742 case 'G': case 'g':
743 value <<= 30;
744 break;
746 node_mem[nodenr] = value;
748 if (get_param_value(option, 128, "cpus", optarg) == 0) {
749 node_cpumask[nodenr] = 0;
750 } else {
751 value = strtoull(option, &endptr, 10);
752 if (value >= 64) {
753 value = 63;
754 fprintf(stderr, "only 64 CPUs in NUMA mode supported.\n");
755 } else {
756 if (*endptr == '-') {
757 endvalue = strtoull(endptr+1, &endptr, 10);
758 if (endvalue >= 63) {
759 endvalue = 62;
760 fprintf(stderr,
761 "only 63 CPUs in NUMA mode supported.\n");
763 value = (2ULL << endvalue) - (1ULL << value);
764 } else {
765 value = 1ULL << value;
768 node_cpumask[nodenr] = value;
770 nb_numa_nodes++;
772 return;
775 static void smp_parse(const char *optarg)
777 int smp, sockets = 0, threads = 0, cores = 0;
778 char *endptr;
779 char option[128];
781 smp = strtoul(optarg, &endptr, 10);
782 if (endptr != optarg) {
783 if (*endptr == ',') {
784 endptr++;
787 if (get_param_value(option, 128, "sockets", endptr) != 0)
788 sockets = strtoull(option, NULL, 10);
789 if (get_param_value(option, 128, "cores", endptr) != 0)
790 cores = strtoull(option, NULL, 10);
791 if (get_param_value(option, 128, "threads", endptr) != 0)
792 threads = strtoull(option, NULL, 10);
793 if (get_param_value(option, 128, "maxcpus", endptr) != 0)
794 max_cpus = strtoull(option, NULL, 10);
796 /* compute missing values, prefer sockets over cores over threads */
797 if (smp == 0 || sockets == 0) {
798 sockets = sockets > 0 ? sockets : 1;
799 cores = cores > 0 ? cores : 1;
800 threads = threads > 0 ? threads : 1;
801 if (smp == 0) {
802 smp = cores * threads * sockets;
804 } else {
805 if (cores == 0) {
806 threads = threads > 0 ? threads : 1;
807 cores = smp / (sockets * threads);
808 } else {
809 if (sockets) {
810 threads = smp / (cores * sockets);
814 smp_cpus = smp;
815 smp_cores = cores > 0 ? cores : 1;
816 smp_threads = threads > 0 ? threads : 1;
817 if (max_cpus == 0)
818 max_cpus = smp_cpus;
821 /***********************************************************/
822 /* USB devices */
824 static int usb_device_add(const char *devname)
826 const char *p;
827 USBDevice *dev = NULL;
829 if (!usb_enabled)
830 return -1;
832 /* drivers with .usbdevice_name entry in USBDeviceInfo */
833 dev = usbdevice_create(devname);
834 if (dev)
835 goto done;
837 /* the other ones */
838 if (strstart(devname, "host:", &p)) {
839 dev = usb_host_device_open(p);
840 } else if (!strcmp(devname, "bt") || strstart(devname, "bt:", &p)) {
841 dev = usb_bt_init(devname[2] ? hci_init(p) :
842 bt_new_hci(qemu_find_bt_vlan(0)));
843 } else {
844 return -1;
846 if (!dev)
847 return -1;
849 done:
850 return 0;
853 static int usb_device_del(const char *devname)
855 int bus_num, addr;
856 const char *p;
858 if (strstart(devname, "host:", &p))
859 return usb_host_device_close(p);
861 if (!usb_enabled)
862 return -1;
864 p = strchr(devname, '.');
865 if (!p)
866 return -1;
867 bus_num = strtoul(devname, NULL, 0);
868 addr = strtoul(p + 1, NULL, 0);
870 return usb_device_delete_addr(bus_num, addr);
873 static int usb_parse(const char *cmdline)
875 int r;
876 r = usb_device_add(cmdline);
877 if (r < 0) {
878 fprintf(stderr, "qemu: could not add USB device '%s'\n", cmdline);
880 return r;
883 void do_usb_add(Monitor *mon, const QDict *qdict)
885 const char *devname = qdict_get_str(qdict, "devname");
886 if (usb_device_add(devname) < 0) {
887 error_report("could not add USB device '%s'", devname);
891 void do_usb_del(Monitor *mon, const QDict *qdict)
893 const char *devname = qdict_get_str(qdict, "devname");
894 if (usb_device_del(devname) < 0) {
895 error_report("could not delete USB device '%s'", devname);
899 /***********************************************************/
900 /* PCMCIA/Cardbus */
902 static struct pcmcia_socket_entry_s {
903 PCMCIASocket *socket;
904 struct pcmcia_socket_entry_s *next;
905 } *pcmcia_sockets = 0;
907 void pcmcia_socket_register(PCMCIASocket *socket)
909 struct pcmcia_socket_entry_s *entry;
911 entry = qemu_malloc(sizeof(struct pcmcia_socket_entry_s));
912 entry->socket = socket;
913 entry->next = pcmcia_sockets;
914 pcmcia_sockets = entry;
917 void pcmcia_socket_unregister(PCMCIASocket *socket)
919 struct pcmcia_socket_entry_s *entry, **ptr;
921 ptr = &pcmcia_sockets;
922 for (entry = *ptr; entry; ptr = &entry->next, entry = *ptr)
923 if (entry->socket == socket) {
924 *ptr = entry->next;
925 qemu_free(entry);
929 void pcmcia_info(Monitor *mon)
931 struct pcmcia_socket_entry_s *iter;
933 if (!pcmcia_sockets)
934 monitor_printf(mon, "No PCMCIA sockets\n");
936 for (iter = pcmcia_sockets; iter; iter = iter->next)
937 monitor_printf(mon, "%s: %s\n", iter->socket->slot_string,
938 iter->socket->attached ? iter->socket->card_string :
939 "Empty");
942 /***********************************************************/
943 /* I/O handling */
945 typedef struct IOHandlerRecord {
946 int fd;
947 IOCanReadHandler *fd_read_poll;
948 IOHandler *fd_read;
949 IOHandler *fd_write;
950 int deleted;
951 void *opaque;
952 /* temporary data */
953 struct pollfd *ufd;
954 QLIST_ENTRY(IOHandlerRecord) next;
955 } IOHandlerRecord;
957 static QLIST_HEAD(, IOHandlerRecord) io_handlers =
958 QLIST_HEAD_INITIALIZER(io_handlers);
961 /* XXX: fd_read_poll should be suppressed, but an API change is
962 necessary in the character devices to suppress fd_can_read(). */
963 int qemu_set_fd_handler2(int fd,
964 IOCanReadHandler *fd_read_poll,
965 IOHandler *fd_read,
966 IOHandler *fd_write,
967 void *opaque)
969 IOHandlerRecord *ioh;
971 if (!fd_read && !fd_write) {
972 QLIST_FOREACH(ioh, &io_handlers, next) {
973 if (ioh->fd == fd) {
974 ioh->deleted = 1;
975 break;
978 } else {
979 QLIST_FOREACH(ioh, &io_handlers, next) {
980 if (ioh->fd == fd)
981 goto found;
983 ioh = qemu_mallocz(sizeof(IOHandlerRecord));
984 QLIST_INSERT_HEAD(&io_handlers, ioh, next);
985 found:
986 ioh->fd = fd;
987 ioh->fd_read_poll = fd_read_poll;
988 ioh->fd_read = fd_read;
989 ioh->fd_write = fd_write;
990 ioh->opaque = opaque;
991 ioh->deleted = 0;
993 qemu_notify_event();
994 return 0;
997 int qemu_set_fd_handler(int fd,
998 IOHandler *fd_read,
999 IOHandler *fd_write,
1000 void *opaque)
1002 return qemu_set_fd_handler2(fd, NULL, fd_read, fd_write, opaque);
1005 /***********************************************************/
1006 /* machine registration */
1008 static QEMUMachine *first_machine = NULL;
1009 QEMUMachine *current_machine = NULL;
1011 int qemu_register_machine(QEMUMachine *m)
1013 QEMUMachine **pm;
1014 pm = &first_machine;
1015 while (*pm != NULL)
1016 pm = &(*pm)->next;
1017 m->next = NULL;
1018 *pm = m;
1019 return 0;
1022 static QEMUMachine *find_machine(const char *name)
1024 QEMUMachine *m;
1026 for(m = first_machine; m != NULL; m = m->next) {
1027 if (!strcmp(m->name, name))
1028 return m;
1029 if (m->alias && !strcmp(m->alias, name))
1030 return m;
1032 return NULL;
1035 static QEMUMachine *find_default_machine(void)
1037 QEMUMachine *m;
1039 for(m = first_machine; m != NULL; m = m->next) {
1040 if (m->is_default) {
1041 return m;
1044 return NULL;
1047 /***********************************************************/
1048 /* main execution loop */
1050 static void gui_update(void *opaque)
1052 uint64_t interval = GUI_REFRESH_INTERVAL;
1053 DisplayState *ds = opaque;
1054 DisplayChangeListener *dcl = ds->listeners;
1056 qemu_flush_coalesced_mmio_buffer();
1057 dpy_refresh(ds);
1059 while (dcl != NULL) {
1060 if (dcl->gui_timer_interval &&
1061 dcl->gui_timer_interval < interval)
1062 interval = dcl->gui_timer_interval;
1063 dcl = dcl->next;
1065 qemu_mod_timer(ds->gui_timer, interval + qemu_get_clock(rt_clock));
1068 static void nographic_update(void *opaque)
1070 uint64_t interval = GUI_REFRESH_INTERVAL;
1072 qemu_flush_coalesced_mmio_buffer();
1073 qemu_mod_timer(nographic_timer, interval + qemu_get_clock(rt_clock));
1076 struct vm_change_state_entry {
1077 VMChangeStateHandler *cb;
1078 void *opaque;
1079 QLIST_ENTRY (vm_change_state_entry) entries;
1082 static QLIST_HEAD(vm_change_state_head, vm_change_state_entry) vm_change_state_head;
1084 VMChangeStateEntry *qemu_add_vm_change_state_handler(VMChangeStateHandler *cb,
1085 void *opaque)
1087 VMChangeStateEntry *e;
1089 e = qemu_mallocz(sizeof (*e));
1091 e->cb = cb;
1092 e->opaque = opaque;
1093 QLIST_INSERT_HEAD(&vm_change_state_head, e, entries);
1094 return e;
1097 void qemu_del_vm_change_state_handler(VMChangeStateEntry *e)
1099 QLIST_REMOVE (e, entries);
1100 qemu_free (e);
1103 void vm_state_notify(int running, int reason)
1105 VMChangeStateEntry *e;
1107 for (e = vm_change_state_head.lh_first; e; e = e->entries.le_next) {
1108 e->cb(e->opaque, running, reason);
1112 void vm_start(void)
1114 if (!vm_running) {
1115 cpu_enable_ticks();
1116 vm_running = 1;
1117 vm_state_notify(1, 0);
1118 resume_all_vcpus();
1119 monitor_protocol_event(QEVENT_RESUME, NULL);
1123 /* reset/shutdown handler */
1125 typedef struct QEMUResetEntry {
1126 QTAILQ_ENTRY(QEMUResetEntry) entry;
1127 QEMUResetHandler *func;
1128 void *opaque;
1129 } QEMUResetEntry;
1131 static QTAILQ_HEAD(reset_handlers, QEMUResetEntry) reset_handlers =
1132 QTAILQ_HEAD_INITIALIZER(reset_handlers);
1133 static int reset_requested;
1134 static int shutdown_requested;
1135 static int powerdown_requested;
1136 int debug_requested;
1137 int vmstop_requested;
1139 int qemu_no_shutdown(void)
1141 int r = no_shutdown;
1142 no_shutdown = 0;
1143 return r;
1146 int qemu_shutdown_requested(void)
1148 int r = shutdown_requested;
1149 shutdown_requested = 0;
1150 return r;
1153 int qemu_reset_requested(void)
1155 int r = reset_requested;
1156 reset_requested = 0;
1157 return r;
1160 int qemu_powerdown_requested(void)
1162 int r = powerdown_requested;
1163 powerdown_requested = 0;
1164 return r;
1167 static int qemu_debug_requested(void)
1169 int r = debug_requested;
1170 debug_requested = 0;
1171 return r;
1174 static int qemu_vmstop_requested(void)
1176 int r = vmstop_requested;
1177 vmstop_requested = 0;
1178 return r;
1181 void qemu_register_reset(QEMUResetHandler *func, void *opaque)
1183 QEMUResetEntry *re = qemu_mallocz(sizeof(QEMUResetEntry));
1185 re->func = func;
1186 re->opaque = opaque;
1187 QTAILQ_INSERT_TAIL(&reset_handlers, re, entry);
1190 void qemu_unregister_reset(QEMUResetHandler *func, void *opaque)
1192 QEMUResetEntry *re;
1194 QTAILQ_FOREACH(re, &reset_handlers, entry) {
1195 if (re->func == func && re->opaque == opaque) {
1196 QTAILQ_REMOVE(&reset_handlers, re, entry);
1197 qemu_free(re);
1198 return;
1203 void qemu_system_reset(void)
1205 QEMUResetEntry *re, *nre;
1207 /* reset all devices */
1208 QTAILQ_FOREACH_SAFE(re, &reset_handlers, entry, nre) {
1209 re->func(re->opaque);
1211 monitor_protocol_event(QEVENT_RESET, NULL);
1212 cpu_synchronize_all_post_reset();
1215 void qemu_system_reset_request(void)
1217 if (no_reboot) {
1218 shutdown_requested = 1;
1219 } else {
1220 reset_requested = 1;
1222 if (cpu_single_env) {
1223 cpu_single_env->stopped = 1;
1225 qemu_notify_event();
1228 void qemu_system_shutdown_request(void)
1230 shutdown_requested = 1;
1231 qemu_notify_event();
1234 void qemu_system_powerdown_request(void)
1236 powerdown_requested = 1;
1237 qemu_notify_event();
1240 void main_loop_wait(int nonblocking)
1242 IOHandlerRecord *ioh;
1243 fd_set rfds, wfds, xfds;
1244 int ret, nfds;
1245 struct timeval tv;
1246 int timeout;
1248 if (nonblocking)
1249 timeout = 0;
1250 else {
1251 timeout = qemu_calculate_timeout();
1252 qemu_bh_update_timeout(&timeout);
1255 os_host_main_loop_wait(&timeout);
1257 /* poll any events */
1258 /* XXX: separate device handlers from system ones */
1259 nfds = -1;
1260 FD_ZERO(&rfds);
1261 FD_ZERO(&wfds);
1262 FD_ZERO(&xfds);
1263 QLIST_FOREACH(ioh, &io_handlers, next) {
1264 if (ioh->deleted)
1265 continue;
1266 if (ioh->fd_read &&
1267 (!ioh->fd_read_poll ||
1268 ioh->fd_read_poll(ioh->opaque) != 0)) {
1269 FD_SET(ioh->fd, &rfds);
1270 if (ioh->fd > nfds)
1271 nfds = ioh->fd;
1273 if (ioh->fd_write) {
1274 FD_SET(ioh->fd, &wfds);
1275 if (ioh->fd > nfds)
1276 nfds = ioh->fd;
1280 tv.tv_sec = timeout / 1000;
1281 tv.tv_usec = (timeout % 1000) * 1000;
1283 slirp_select_fill(&nfds, &rfds, &wfds, &xfds);
1285 qemu_mutex_unlock_iothread();
1286 ret = select(nfds + 1, &rfds, &wfds, &xfds, &tv);
1287 qemu_mutex_lock_iothread();
1288 if (ret > 0) {
1289 IOHandlerRecord *pioh;
1291 QLIST_FOREACH_SAFE(ioh, &io_handlers, next, pioh) {
1292 if (ioh->deleted) {
1293 QLIST_REMOVE(ioh, next);
1294 qemu_free(ioh);
1295 continue;
1297 if (ioh->fd_read && FD_ISSET(ioh->fd, &rfds)) {
1298 ioh->fd_read(ioh->opaque);
1299 if (!(ioh->fd_read_poll && ioh->fd_read_poll(ioh->opaque)))
1300 FD_CLR(ioh->fd, &rfds);
1302 if (ioh->fd_write && FD_ISSET(ioh->fd, &wfds)) {
1303 ioh->fd_write(ioh->opaque);
1308 slirp_select_poll(&rfds, &wfds, &xfds, (ret < 0));
1310 qemu_run_all_timers();
1312 /* Check bottom-halves last in case any of the earlier events triggered
1313 them. */
1314 qemu_bh_poll();
1318 static int vm_can_run(void)
1320 if (powerdown_requested)
1321 return 0;
1322 if (reset_requested)
1323 return 0;
1324 if (shutdown_requested)
1325 return 0;
1326 if (debug_requested)
1327 return 0;
1328 return 1;
1331 qemu_irq qemu_system_powerdown;
1333 static void main_loop(void)
1335 int r;
1337 if (kvm_enabled()) {
1338 kvm_main_loop();
1339 cpu_disable_ticks();
1340 return;
1343 qemu_main_loop_start();
1345 for (;;) {
1346 do {
1347 bool nonblocking = false;
1348 #ifdef CONFIG_PROFILER
1349 int64_t ti;
1350 #endif
1351 #ifndef CONFIG_IOTHREAD
1352 nonblocking = tcg_cpu_exec();
1353 #endif
1354 #ifdef CONFIG_PROFILER
1355 ti = profile_getclock();
1356 #endif
1357 main_loop_wait(nonblocking);
1358 #ifdef CONFIG_PROFILER
1359 dev_time += profile_getclock() - ti;
1360 #endif
1361 } while (vm_can_run());
1363 if ((r = qemu_debug_requested())) {
1364 vm_stop(r);
1366 if (qemu_shutdown_requested()) {
1367 monitor_protocol_event(QEVENT_SHUTDOWN, NULL);
1368 if (no_shutdown) {
1369 vm_stop(0);
1370 no_shutdown = 0;
1371 } else
1372 break;
1374 if (qemu_reset_requested()) {
1375 pause_all_vcpus();
1376 qemu_system_reset();
1377 resume_all_vcpus();
1379 if (qemu_powerdown_requested()) {
1380 monitor_protocol_event(QEVENT_POWERDOWN, NULL);
1381 qemu_irq_raise(qemu_system_powerdown);
1383 if ((r = qemu_vmstop_requested())) {
1384 vm_stop(r);
1387 bdrv_close_all();
1388 pause_all_vcpus();
1391 static void version(void)
1393 printf("QEMU emulator version " QEMU_VERSION QEMU_PKGVERSION ", Copyright (c) 2003-2008 Fabrice Bellard\n");
1396 static void help(int exitcode)
1398 const char *options_help =
1399 #define DEF(option, opt_arg, opt_enum, opt_help, arch_mask) \
1400 opt_help
1401 #define DEFHEADING(text) stringify(text) "\n"
1402 #include "qemu-options.def"
1403 #undef DEF
1404 #undef DEFHEADING
1405 #undef GEN_DOCS
1407 version();
1408 printf("usage: %s [options] [disk_image]\n"
1409 "\n"
1410 "'disk_image' is a raw hard disk image for IDE hard disk 0\n"
1411 "\n"
1412 "%s\n"
1413 "During emulation, the following keys are useful:\n"
1414 "ctrl-alt-f toggle full screen\n"
1415 "ctrl-alt-n switch to virtual console 'n'\n"
1416 "ctrl-alt toggle mouse and keyboard grab\n"
1417 "\n"
1418 "When using -nographic, press 'ctrl-a h' to get some help.\n",
1419 "qemu",
1420 options_help);
1421 exit(exitcode);
1424 #define HAS_ARG 0x0001
1426 typedef struct QEMUOption {
1427 const char *name;
1428 int flags;
1429 int index;
1430 uint32_t arch_mask;
1431 } QEMUOption;
1433 static const QEMUOption qemu_options[] = {
1434 { "h", 0, QEMU_OPTION_h, QEMU_ARCH_ALL },
1435 #define DEF(option, opt_arg, opt_enum, opt_help, arch_mask) \
1436 { option, opt_arg, opt_enum, arch_mask },
1437 #define DEFHEADING(text)
1438 #include "qemu-options.def"
1439 #undef DEF
1440 #undef DEFHEADING
1441 #undef GEN_DOCS
1442 { NULL },
1444 static void select_vgahw (const char *p)
1446 const char *opts;
1448 default_vga = 0;
1449 vga_interface_type = VGA_NONE;
1450 if (strstart(p, "std", &opts)) {
1451 vga_interface_type = VGA_STD;
1452 } else if (strstart(p, "cirrus", &opts)) {
1453 vga_interface_type = VGA_CIRRUS;
1454 } else if (strstart(p, "vmware", &opts)) {
1455 vga_interface_type = VGA_VMWARE;
1456 } else if (strstart(p, "xenfb", &opts)) {
1457 vga_interface_type = VGA_XENFB;
1458 } else if (!strstart(p, "none", &opts)) {
1459 invalid_vga:
1460 fprintf(stderr, "Unknown vga type: %s\n", p);
1461 exit(1);
1463 while (*opts) {
1464 const char *nextopt;
1466 if (strstart(opts, ",retrace=", &nextopt)) {
1467 opts = nextopt;
1468 if (strstart(opts, "dumb", &nextopt))
1469 vga_retrace_method = VGA_RETRACE_DUMB;
1470 else if (strstart(opts, "precise", &nextopt))
1471 vga_retrace_method = VGA_RETRACE_PRECISE;
1472 else goto invalid_vga;
1473 } else goto invalid_vga;
1474 opts = nextopt;
1478 static int balloon_parse(const char *arg)
1480 QemuOpts *opts;
1482 if (strcmp(arg, "none") == 0) {
1483 return 0;
1486 if (!strncmp(arg, "virtio", 6)) {
1487 if (arg[6] == ',') {
1488 /* have params -> parse them */
1489 opts = qemu_opts_parse(&qemu_device_opts, arg+7, 0);
1490 if (!opts)
1491 return -1;
1492 } else {
1493 /* create empty opts */
1494 opts = qemu_opts_create(&qemu_device_opts, NULL, 0);
1496 qemu_opt_set(opts, "driver", "virtio-balloon-pci");
1497 return 0;
1500 return -1;
1503 char *qemu_find_file(int type, const char *name)
1505 int len;
1506 const char *subdir;
1507 char *buf;
1509 /* If name contains path separators then try it as a straight path. */
1510 if ((strchr(name, '/') || strchr(name, '\\'))
1511 && access(name, R_OK) == 0) {
1512 return qemu_strdup(name);
1514 switch (type) {
1515 case QEMU_FILE_TYPE_BIOS:
1516 subdir = "";
1517 break;
1518 case QEMU_FILE_TYPE_KEYMAP:
1519 subdir = "keymaps/";
1520 break;
1521 default:
1522 abort();
1524 len = strlen(data_dir) + strlen(name) + strlen(subdir) + 2;
1525 buf = qemu_mallocz(len);
1526 snprintf(buf, len, "%s/%s%s", data_dir, subdir, name);
1527 if (access(buf, R_OK)) {
1528 qemu_free(buf);
1529 return NULL;
1531 return buf;
1534 static int device_help_func(QemuOpts *opts, void *opaque)
1536 return qdev_device_help(opts);
1539 static int device_init_func(QemuOpts *opts, void *opaque)
1541 DeviceState *dev;
1543 dev = qdev_device_add(opts);
1544 if (!dev)
1545 return -1;
1546 return 0;
1549 static int chardev_init_func(QemuOpts *opts, void *opaque)
1551 CharDriverState *chr;
1553 chr = qemu_chr_open_opts(opts, NULL);
1554 if (!chr)
1555 return -1;
1556 return 0;
1559 #ifdef CONFIG_LINUX
1560 static int fsdev_init_func(QemuOpts *opts, void *opaque)
1562 int ret;
1563 ret = qemu_fsdev_add(opts);
1565 return ret;
1567 #endif
1569 static int mon_init_func(QemuOpts *opts, void *opaque)
1571 CharDriverState *chr;
1572 const char *chardev;
1573 const char *mode;
1574 int flags;
1576 mode = qemu_opt_get(opts, "mode");
1577 if (mode == NULL) {
1578 mode = "readline";
1580 if (strcmp(mode, "readline") == 0) {
1581 flags = MONITOR_USE_READLINE;
1582 } else if (strcmp(mode, "control") == 0) {
1583 flags = MONITOR_USE_CONTROL;
1584 } else {
1585 fprintf(stderr, "unknown monitor mode \"%s\"\n", mode);
1586 exit(1);
1589 if (qemu_opt_get_bool(opts, "default", 0))
1590 flags |= MONITOR_IS_DEFAULT;
1592 chardev = qemu_opt_get(opts, "chardev");
1593 chr = qemu_chr_find(chardev);
1594 if (chr == NULL) {
1595 fprintf(stderr, "chardev \"%s\" not found\n", chardev);
1596 exit(1);
1599 monitor_init(chr, flags);
1600 return 0;
1603 static void monitor_parse(const char *optarg, const char *mode)
1605 static int monitor_device_index = 0;
1606 QemuOpts *opts;
1607 const char *p;
1608 char label[32];
1609 int def = 0;
1611 if (strstart(optarg, "chardev:", &p)) {
1612 snprintf(label, sizeof(label), "%s", p);
1613 } else {
1614 snprintf(label, sizeof(label), "compat_monitor%d",
1615 monitor_device_index);
1616 if (monitor_device_index == 0) {
1617 def = 1;
1619 opts = qemu_chr_parse_compat(label, optarg);
1620 if (!opts) {
1621 fprintf(stderr, "parse error: %s\n", optarg);
1622 exit(1);
1626 opts = qemu_opts_create(&qemu_mon_opts, label, 1);
1627 if (!opts) {
1628 fprintf(stderr, "duplicate chardev: %s\n", label);
1629 exit(1);
1631 qemu_opt_set(opts, "mode", mode);
1632 qemu_opt_set(opts, "chardev", label);
1633 if (def)
1634 qemu_opt_set(opts, "default", "on");
1635 monitor_device_index++;
1638 struct device_config {
1639 enum {
1640 DEV_USB, /* -usbdevice */
1641 DEV_BT, /* -bt */
1642 DEV_SERIAL, /* -serial */
1643 DEV_PARALLEL, /* -parallel */
1644 DEV_VIRTCON, /* -virtioconsole */
1645 DEV_DEBUGCON, /* -debugcon */
1646 } type;
1647 const char *cmdline;
1648 QTAILQ_ENTRY(device_config) next;
1650 QTAILQ_HEAD(, device_config) device_configs = QTAILQ_HEAD_INITIALIZER(device_configs);
1652 static void add_device_config(int type, const char *cmdline)
1654 struct device_config *conf;
1656 conf = qemu_mallocz(sizeof(*conf));
1657 conf->type = type;
1658 conf->cmdline = cmdline;
1659 QTAILQ_INSERT_TAIL(&device_configs, conf, next);
1662 static int foreach_device_config(int type, int (*func)(const char *cmdline))
1664 struct device_config *conf;
1665 int rc;
1667 QTAILQ_FOREACH(conf, &device_configs, next) {
1668 if (conf->type != type)
1669 continue;
1670 rc = func(conf->cmdline);
1671 if (0 != rc)
1672 return rc;
1674 return 0;
1677 static int serial_parse(const char *devname)
1679 static int index = 0;
1680 char label[32];
1682 if (strcmp(devname, "none") == 0)
1683 return 0;
1684 if (index == MAX_SERIAL_PORTS) {
1685 fprintf(stderr, "qemu: too many serial ports\n");
1686 exit(1);
1688 snprintf(label, sizeof(label), "serial%d", index);
1689 serial_hds[index] = qemu_chr_open(label, devname, NULL);
1690 if (!serial_hds[index]) {
1691 fprintf(stderr, "qemu: could not open serial device '%s': %s\n",
1692 devname, strerror(errno));
1693 return -1;
1695 index++;
1696 return 0;
1699 static int parallel_parse(const char *devname)
1701 static int index = 0;
1702 char label[32];
1704 if (strcmp(devname, "none") == 0)
1705 return 0;
1706 if (index == MAX_PARALLEL_PORTS) {
1707 fprintf(stderr, "qemu: too many parallel ports\n");
1708 exit(1);
1710 snprintf(label, sizeof(label), "parallel%d", index);
1711 parallel_hds[index] = qemu_chr_open(label, devname, NULL);
1712 if (!parallel_hds[index]) {
1713 fprintf(stderr, "qemu: could not open parallel device '%s': %s\n",
1714 devname, strerror(errno));
1715 return -1;
1717 index++;
1718 return 0;
1721 static int virtcon_parse(const char *devname)
1723 static int index = 0;
1724 char label[32];
1725 QemuOpts *bus_opts, *dev_opts;
1727 if (strcmp(devname, "none") == 0)
1728 return 0;
1729 if (index == MAX_VIRTIO_CONSOLES) {
1730 fprintf(stderr, "qemu: too many virtio consoles\n");
1731 exit(1);
1734 bus_opts = qemu_opts_create(&qemu_device_opts, NULL, 0);
1735 qemu_opt_set(bus_opts, "driver", "virtio-serial");
1737 dev_opts = qemu_opts_create(&qemu_device_opts, NULL, 0);
1738 qemu_opt_set(dev_opts, "driver", "virtconsole");
1740 snprintf(label, sizeof(label), "virtcon%d", index);
1741 virtcon_hds[index] = qemu_chr_open(label, devname, NULL);
1742 if (!virtcon_hds[index]) {
1743 fprintf(stderr, "qemu: could not open virtio console '%s': %s\n",
1744 devname, strerror(errno));
1745 return -1;
1747 qemu_opt_set(dev_opts, "chardev", label);
1749 index++;
1750 return 0;
1753 static int debugcon_parse(const char *devname)
1755 QemuOpts *opts;
1757 if (!qemu_chr_open("debugcon", devname, NULL)) {
1758 exit(1);
1760 opts = qemu_opts_create(&qemu_device_opts, "debugcon", 1);
1761 if (!opts) {
1762 fprintf(stderr, "qemu: already have a debugcon device\n");
1763 exit(1);
1765 qemu_opt_set(opts, "driver", "isa-debugcon");
1766 qemu_opt_set(opts, "chardev", "debugcon");
1767 return 0;
1770 static const QEMUOption *lookup_opt(int argc, char **argv,
1771 const char **poptarg, int *poptind)
1773 const QEMUOption *popt;
1774 int optind = *poptind;
1775 char *r = argv[optind];
1776 const char *optarg;
1778 loc_set_cmdline(argv, optind, 1);
1779 optind++;
1780 /* Treat --foo the same as -foo. */
1781 if (r[1] == '-')
1782 r++;
1783 popt = qemu_options;
1784 for(;;) {
1785 if (!popt->name) {
1786 error_report("invalid option");
1787 exit(1);
1789 if (!strcmp(popt->name, r + 1))
1790 break;
1791 popt++;
1793 if (popt->flags & HAS_ARG) {
1794 if (optind >= argc) {
1795 error_report("requires an argument");
1796 exit(1);
1798 optarg = argv[optind++];
1799 loc_set_cmdline(argv, optind - 2, 2);
1800 } else {
1801 optarg = NULL;
1804 *poptarg = optarg;
1805 *poptind = optind;
1807 return popt;
1810 int main(int argc, char **argv, char **envp)
1812 const char *gdbstub_dev = NULL;
1813 int i;
1814 int snapshot, linux_boot;
1815 const char *icount_option = NULL;
1816 const char *initrd_filename;
1817 const char *kernel_filename, *kernel_cmdline;
1818 char boot_devices[33] = "cad"; /* default to HD->floppy->CD-ROM */
1819 DisplayState *ds;
1820 DisplayChangeListener *dcl;
1821 int cyls, heads, secs, translation;
1822 QemuOpts *hda_opts = NULL, *opts;
1823 int optind;
1824 const char *optarg;
1825 const char *loadvm = NULL;
1826 QEMUMachine *machine;
1827 const char *cpu_model;
1828 int tb_size;
1829 const char *pid_file = NULL;
1830 const char *incoming = NULL;
1831 int show_vnc_port = 0;
1832 int defconfig = 1;
1834 error_set_progname(argv[0]);
1836 init_clocks();
1838 qemu_cache_utils_init(envp);
1840 QLIST_INIT (&vm_change_state_head);
1841 os_setup_early_signal_handling();
1843 module_call_init(MODULE_INIT_MACHINE);
1844 machine = find_default_machine();
1845 cpu_model = NULL;
1846 initrd_filename = NULL;
1847 ram_size = 0;
1848 snapshot = 0;
1849 kernel_filename = NULL;
1850 kernel_cmdline = "";
1851 cyls = heads = secs = 0;
1852 translation = BIOS_ATA_TRANSLATION_AUTO;
1854 for (i = 0; i < MAX_NODES; i++) {
1855 node_mem[i] = 0;
1856 node_cpumask[i] = 0;
1859 assigned_devices_index = 0;
1861 nb_numa_nodes = 0;
1862 nb_nics = 0;
1864 tb_size = 0;
1865 autostart= 1;
1867 /* first pass of option parsing */
1868 optind = 1;
1869 while (optind < argc) {
1870 if (argv[optind][0] != '-') {
1871 /* disk image */
1872 optind++;
1873 continue;
1874 } else {
1875 const QEMUOption *popt;
1877 popt = lookup_opt(argc, argv, &optarg, &optind);
1878 switch (popt->index) {
1879 case QEMU_OPTION_nodefconfig:
1880 defconfig=0;
1881 break;
1886 if (defconfig) {
1887 int ret;
1889 ret = qemu_read_config_file(CONFIG_QEMU_CONFDIR "/qemu.conf");
1890 if (ret < 0 && ret != -ENOENT) {
1891 exit(1);
1894 ret = qemu_read_config_file(arch_config_name);
1895 if (ret < 0 && ret != -ENOENT) {
1896 exit(1);
1899 cpudef_init();
1901 /* second pass of option parsing */
1902 optind = 1;
1903 for(;;) {
1904 if (optind >= argc)
1905 break;
1906 if (argv[optind][0] != '-') {
1907 hda_opts = drive_add(argv[optind++], HD_ALIAS, 0);
1908 } else {
1909 const QEMUOption *popt;
1911 popt = lookup_opt(argc, argv, &optarg, &optind);
1912 if (!(popt->arch_mask & arch_type)) {
1913 printf("Option %s not supported for this target\n", popt->name);
1914 exit(1);
1916 switch(popt->index) {
1917 case QEMU_OPTION_M:
1918 machine = find_machine(optarg);
1919 if (!machine) {
1920 QEMUMachine *m;
1921 printf("Supported machines are:\n");
1922 for(m = first_machine; m != NULL; m = m->next) {
1923 if (m->alias)
1924 printf("%-10s %s (alias of %s)\n",
1925 m->alias, m->desc, m->name);
1926 printf("%-10s %s%s\n",
1927 m->name, m->desc,
1928 m->is_default ? " (default)" : "");
1930 exit(*optarg != '?');
1932 break;
1933 case QEMU_OPTION_cpu:
1934 /* hw initialization will check this */
1935 if (*optarg == '?') {
1936 list_cpus(stdout, &fprintf, optarg);
1937 exit(0);
1938 } else {
1939 cpu_model = optarg;
1941 break;
1942 case QEMU_OPTION_initrd:
1943 initrd_filename = optarg;
1944 break;
1945 case QEMU_OPTION_hda:
1946 if (cyls == 0)
1947 hda_opts = drive_add(optarg, HD_ALIAS, 0);
1948 else
1949 hda_opts = drive_add(optarg, HD_ALIAS
1950 ",cyls=%d,heads=%d,secs=%d%s",
1951 0, cyls, heads, secs,
1952 translation == BIOS_ATA_TRANSLATION_LBA ?
1953 ",trans=lba" :
1954 translation == BIOS_ATA_TRANSLATION_NONE ?
1955 ",trans=none" : "");
1956 break;
1957 case QEMU_OPTION_hdb:
1958 case QEMU_OPTION_hdc:
1959 case QEMU_OPTION_hdd:
1960 drive_add(optarg, HD_ALIAS, popt->index - QEMU_OPTION_hda);
1961 break;
1962 case QEMU_OPTION_drive:
1963 drive_add(NULL, "%s", optarg);
1964 break;
1965 case QEMU_OPTION_set:
1966 if (qemu_set_option(optarg) != 0)
1967 exit(1);
1968 break;
1969 case QEMU_OPTION_global:
1970 if (qemu_global_option(optarg) != 0)
1971 exit(1);
1972 break;
1973 case QEMU_OPTION_mtdblock:
1974 drive_add(optarg, MTD_ALIAS);
1975 break;
1976 case QEMU_OPTION_sd:
1977 drive_add(optarg, SD_ALIAS);
1978 break;
1979 case QEMU_OPTION_pflash:
1980 drive_add(optarg, PFLASH_ALIAS);
1981 break;
1982 case QEMU_OPTION_snapshot:
1983 snapshot = 1;
1984 break;
1985 case QEMU_OPTION_hdachs:
1987 const char *p;
1988 p = optarg;
1989 cyls = strtol(p, (char **)&p, 0);
1990 if (cyls < 1 || cyls > 16383)
1991 goto chs_fail;
1992 if (*p != ',')
1993 goto chs_fail;
1994 p++;
1995 heads = strtol(p, (char **)&p, 0);
1996 if (heads < 1 || heads > 16)
1997 goto chs_fail;
1998 if (*p != ',')
1999 goto chs_fail;
2000 p++;
2001 secs = strtol(p, (char **)&p, 0);
2002 if (secs < 1 || secs > 63)
2003 goto chs_fail;
2004 if (*p == ',') {
2005 p++;
2006 if (!strcmp(p, "none"))
2007 translation = BIOS_ATA_TRANSLATION_NONE;
2008 else if (!strcmp(p, "lba"))
2009 translation = BIOS_ATA_TRANSLATION_LBA;
2010 else if (!strcmp(p, "auto"))
2011 translation = BIOS_ATA_TRANSLATION_AUTO;
2012 else
2013 goto chs_fail;
2014 } else if (*p != '\0') {
2015 chs_fail:
2016 fprintf(stderr, "qemu: invalid physical CHS format\n");
2017 exit(1);
2019 if (hda_opts != NULL) {
2020 char num[16];
2021 snprintf(num, sizeof(num), "%d", cyls);
2022 qemu_opt_set(hda_opts, "cyls", num);
2023 snprintf(num, sizeof(num), "%d", heads);
2024 qemu_opt_set(hda_opts, "heads", num);
2025 snprintf(num, sizeof(num), "%d", secs);
2026 qemu_opt_set(hda_opts, "secs", num);
2027 if (translation == BIOS_ATA_TRANSLATION_LBA)
2028 qemu_opt_set(hda_opts, "trans", "lba");
2029 if (translation == BIOS_ATA_TRANSLATION_NONE)
2030 qemu_opt_set(hda_opts, "trans", "none");
2033 break;
2034 case QEMU_OPTION_numa:
2035 if (nb_numa_nodes >= MAX_NODES) {
2036 fprintf(stderr, "qemu: too many NUMA nodes\n");
2037 exit(1);
2039 numa_add(optarg);
2040 break;
2041 case QEMU_OPTION_nographic:
2042 display_type = DT_NOGRAPHIC;
2043 break;
2044 #ifdef CONFIG_CURSES
2045 case QEMU_OPTION_curses:
2046 display_type = DT_CURSES;
2047 break;
2048 #endif
2049 case QEMU_OPTION_portrait:
2050 graphic_rotate = 1;
2051 break;
2052 case QEMU_OPTION_kernel:
2053 kernel_filename = optarg;
2054 break;
2055 case QEMU_OPTION_append:
2056 kernel_cmdline = optarg;
2057 break;
2058 case QEMU_OPTION_cdrom:
2059 drive_add(optarg, CDROM_ALIAS);
2060 break;
2061 case QEMU_OPTION_boot:
2063 static const char * const params[] = {
2064 "order", "once", "menu", NULL
2066 char buf[sizeof(boot_devices)];
2067 char *standard_boot_devices;
2068 int legacy = 0;
2070 if (!strchr(optarg, '=')) {
2071 legacy = 1;
2072 pstrcpy(buf, sizeof(buf), optarg);
2073 } else if (check_params(buf, sizeof(buf), params, optarg) < 0) {
2074 fprintf(stderr,
2075 "qemu: unknown boot parameter '%s' in '%s'\n",
2076 buf, optarg);
2077 exit(1);
2080 if (legacy ||
2081 get_param_value(buf, sizeof(buf), "order", optarg)) {
2082 validate_bootdevices(buf);
2083 pstrcpy(boot_devices, sizeof(boot_devices), buf);
2085 if (!legacy) {
2086 if (get_param_value(buf, sizeof(buf),
2087 "once", optarg)) {
2088 validate_bootdevices(buf);
2089 standard_boot_devices = qemu_strdup(boot_devices);
2090 pstrcpy(boot_devices, sizeof(boot_devices), buf);
2091 qemu_register_reset(restore_boot_devices,
2092 standard_boot_devices);
2094 if (get_param_value(buf, sizeof(buf),
2095 "menu", optarg)) {
2096 if (!strcmp(buf, "on")) {
2097 boot_menu = 1;
2098 } else if (!strcmp(buf, "off")) {
2099 boot_menu = 0;
2100 } else {
2101 fprintf(stderr,
2102 "qemu: invalid option value '%s'\n",
2103 buf);
2104 exit(1);
2109 break;
2110 case QEMU_OPTION_fda:
2111 case QEMU_OPTION_fdb:
2112 drive_add(optarg, FD_ALIAS, popt->index - QEMU_OPTION_fda);
2113 break;
2114 case QEMU_OPTION_no_fd_bootchk:
2115 fd_bootchk = 0;
2116 break;
2117 case QEMU_OPTION_netdev:
2118 if (net_client_parse(&qemu_netdev_opts, optarg) == -1) {
2119 exit(1);
2121 break;
2122 case QEMU_OPTION_net:
2123 if (net_client_parse(&qemu_net_opts, optarg) == -1) {
2124 exit(1);
2126 break;
2127 #ifdef CONFIG_SLIRP
2128 case QEMU_OPTION_tftp:
2129 legacy_tftp_prefix = optarg;
2130 break;
2131 case QEMU_OPTION_bootp:
2132 legacy_bootp_filename = optarg;
2133 break;
2134 case QEMU_OPTION_redir:
2135 if (net_slirp_redir(optarg) < 0)
2136 exit(1);
2137 break;
2138 #endif
2139 case QEMU_OPTION_bt:
2140 add_device_config(DEV_BT, optarg);
2141 break;
2142 case QEMU_OPTION_audio_help:
2143 if (!(audio_available())) {
2144 printf("Option %s not supported for this target\n", popt->name);
2145 exit(1);
2147 AUD_help ();
2148 exit (0);
2149 break;
2150 case QEMU_OPTION_soundhw:
2151 if (!(audio_available())) {
2152 printf("Option %s not supported for this target\n", popt->name);
2153 exit(1);
2155 select_soundhw (optarg);
2156 break;
2157 case QEMU_OPTION_h:
2158 help(0);
2159 break;
2160 case QEMU_OPTION_version:
2161 version();
2162 exit(0);
2163 break;
2164 case QEMU_OPTION_m: {
2165 uint64_t value;
2166 char *ptr;
2168 value = strtoul(optarg, &ptr, 10);
2169 switch (*ptr) {
2170 case 0: case 'M': case 'm':
2171 value <<= 20;
2172 break;
2173 case 'G': case 'g':
2174 value <<= 30;
2175 break;
2176 default:
2177 fprintf(stderr, "qemu: invalid ram size: %s\n", optarg);
2178 exit(1);
2181 /* On 32-bit hosts, QEMU is limited by virtual address space */
2182 if (value > (2047 << 20) && HOST_LONG_BITS == 32) {
2183 fprintf(stderr, "qemu: at most 2047 MB RAM can be simulated\n");
2184 exit(1);
2186 if (value != (uint64_t)(ram_addr_t)value) {
2187 fprintf(stderr, "qemu: ram size too large\n");
2188 exit(1);
2190 ram_size = value;
2191 break;
2193 case QEMU_OPTION_mempath:
2194 mem_path = optarg;
2195 break;
2196 #ifdef MAP_POPULATE
2197 case QEMU_OPTION_mem_prealloc:
2198 mem_prealloc = 1;
2199 break;
2200 #endif
2201 case QEMU_OPTION_d:
2202 set_cpu_log(optarg);
2203 break;
2204 case QEMU_OPTION_s:
2205 gdbstub_dev = "tcp::" DEFAULT_GDBSTUB_PORT;
2206 break;
2207 case QEMU_OPTION_gdb:
2208 gdbstub_dev = optarg;
2209 break;
2210 case QEMU_OPTION_L:
2211 data_dir = optarg;
2212 break;
2213 case QEMU_OPTION_bios:
2214 bios_name = optarg;
2215 break;
2216 case QEMU_OPTION_singlestep:
2217 singlestep = 1;
2218 break;
2219 case QEMU_OPTION_S:
2220 autostart = 0;
2221 break;
2222 case QEMU_OPTION_k:
2223 keyboard_layout = optarg;
2224 break;
2225 case QEMU_OPTION_localtime:
2226 rtc_utc = 0;
2227 break;
2228 case QEMU_OPTION_vga:
2229 select_vgahw (optarg);
2230 break;
2231 case QEMU_OPTION_g:
2233 const char *p;
2234 int w, h, depth;
2235 p = optarg;
2236 w = strtol(p, (char **)&p, 10);
2237 if (w <= 0) {
2238 graphic_error:
2239 fprintf(stderr, "qemu: invalid resolution or depth\n");
2240 exit(1);
2242 if (*p != 'x')
2243 goto graphic_error;
2244 p++;
2245 h = strtol(p, (char **)&p, 10);
2246 if (h <= 0)
2247 goto graphic_error;
2248 if (*p == 'x') {
2249 p++;
2250 depth = strtol(p, (char **)&p, 10);
2251 if (depth != 8 && depth != 15 && depth != 16 &&
2252 depth != 24 && depth != 32)
2253 goto graphic_error;
2254 } else if (*p == '\0') {
2255 depth = graphic_depth;
2256 } else {
2257 goto graphic_error;
2260 graphic_width = w;
2261 graphic_height = h;
2262 graphic_depth = depth;
2264 break;
2265 case QEMU_OPTION_echr:
2267 char *r;
2268 term_escape_char = strtol(optarg, &r, 0);
2269 if (r == optarg)
2270 printf("Bad argument to echr\n");
2271 break;
2273 case QEMU_OPTION_monitor:
2274 monitor_parse(optarg, "readline");
2275 default_monitor = 0;
2276 break;
2277 case QEMU_OPTION_qmp:
2278 monitor_parse(optarg, "control");
2279 default_monitor = 0;
2280 break;
2281 case QEMU_OPTION_mon:
2282 opts = qemu_opts_parse(&qemu_mon_opts, optarg, 1);
2283 if (!opts) {
2284 exit(1);
2286 default_monitor = 0;
2287 break;
2288 case QEMU_OPTION_chardev:
2289 opts = qemu_opts_parse(&qemu_chardev_opts, optarg, 1);
2290 if (!opts) {
2291 exit(1);
2293 break;
2294 #ifdef CONFIG_LINUX
2295 case QEMU_OPTION_fsdev:
2296 opts = qemu_opts_parse(&qemu_fsdev_opts, optarg, 1);
2297 if (!opts) {
2298 fprintf(stderr, "parse error: %s\n", optarg);
2299 exit(1);
2301 break;
2302 case QEMU_OPTION_virtfs: {
2303 char *arg_fsdev = NULL;
2304 char *arg_9p = NULL;
2305 int len = 0;
2307 opts = qemu_opts_parse(&qemu_virtfs_opts, optarg, 1);
2308 if (!opts) {
2309 fprintf(stderr, "parse error: %s\n", optarg);
2310 exit(1);
2313 len = strlen(",id=,path=");
2314 len += strlen(qemu_opt_get(opts, "fstype"));
2315 len += strlen(qemu_opt_get(opts, "mount_tag"));
2316 len += strlen(qemu_opt_get(opts, "path"));
2317 arg_fsdev = qemu_malloc((len + 1) * sizeof(*arg_fsdev));
2319 if (!arg_fsdev) {
2320 fprintf(stderr, "No memory to parse -fsdev for %s\n",
2321 optarg);
2322 exit(1);
2325 sprintf(arg_fsdev, "%s,id=%s,path=%s",
2326 qemu_opt_get(opts, "fstype"),
2327 qemu_opt_get(opts, "mount_tag"),
2328 qemu_opt_get(opts, "path"));
2330 len = strlen("virtio-9p-pci,fsdev=,mount_tag=");
2331 len += 2*strlen(qemu_opt_get(opts, "mount_tag"));
2332 arg_9p = qemu_malloc((len + 1) * sizeof(*arg_9p));
2334 if (!arg_9p) {
2335 fprintf(stderr, "No memory to parse -device for %s\n",
2336 optarg);
2337 exit(1);
2340 sprintf(arg_9p, "virtio-9p-pci,fsdev=%s,mount_tag=%s",
2341 qemu_opt_get(opts, "mount_tag"),
2342 qemu_opt_get(opts, "mount_tag"));
2344 if (!qemu_opts_parse(&qemu_fsdev_opts, arg_fsdev, 1)) {
2345 fprintf(stderr, "parse error [fsdev]: %s\n", optarg);
2346 exit(1);
2349 if (!qemu_opts_parse(&qemu_device_opts, arg_9p, 1)) {
2350 fprintf(stderr, "parse error [device]: %s\n", optarg);
2351 exit(1);
2354 qemu_free(arg_fsdev);
2355 qemu_free(arg_9p);
2356 break;
2358 #endif
2359 case QEMU_OPTION_serial:
2360 add_device_config(DEV_SERIAL, optarg);
2361 default_serial = 0;
2362 if (strncmp(optarg, "mon:", 4) == 0) {
2363 default_monitor = 0;
2365 break;
2366 case QEMU_OPTION_watchdog:
2367 if (watchdog) {
2368 fprintf(stderr,
2369 "qemu: only one watchdog option may be given\n");
2370 return 1;
2372 watchdog = optarg;
2373 break;
2374 case QEMU_OPTION_watchdog_action:
2375 if (select_watchdog_action(optarg) == -1) {
2376 fprintf(stderr, "Unknown -watchdog-action parameter\n");
2377 exit(1);
2379 break;
2380 case QEMU_OPTION_virtiocon:
2381 add_device_config(DEV_VIRTCON, optarg);
2382 default_virtcon = 0;
2383 if (strncmp(optarg, "mon:", 4) == 0) {
2384 default_monitor = 0;
2386 break;
2387 case QEMU_OPTION_parallel:
2388 add_device_config(DEV_PARALLEL, optarg);
2389 default_parallel = 0;
2390 if (strncmp(optarg, "mon:", 4) == 0) {
2391 default_monitor = 0;
2393 break;
2394 case QEMU_OPTION_debugcon:
2395 add_device_config(DEV_DEBUGCON, optarg);
2396 break;
2397 case QEMU_OPTION_loadvm:
2398 loadvm = optarg;
2399 break;
2400 case QEMU_OPTION_full_screen:
2401 full_screen = 1;
2402 break;
2403 #ifdef CONFIG_SDL
2404 case QEMU_OPTION_no_frame:
2405 no_frame = 1;
2406 break;
2407 case QEMU_OPTION_alt_grab:
2408 alt_grab = 1;
2409 break;
2410 case QEMU_OPTION_ctrl_grab:
2411 ctrl_grab = 1;
2412 break;
2413 case QEMU_OPTION_no_quit:
2414 no_quit = 1;
2415 break;
2416 case QEMU_OPTION_sdl:
2417 display_type = DT_SDL;
2418 break;
2419 #endif
2420 case QEMU_OPTION_pidfile:
2421 pid_file = optarg;
2422 break;
2423 case QEMU_OPTION_win2k_hack:
2424 win2k_install_hack = 1;
2425 break;
2426 case QEMU_OPTION_rtc_td_hack:
2427 rtc_td_hack = 1;
2428 break;
2429 case QEMU_OPTION_acpitable:
2430 do_acpitable_option(optarg);
2431 break;
2432 case QEMU_OPTION_smbios:
2433 do_smbios_option(optarg);
2434 break;
2435 #ifdef KVM_UPSTREAM
2436 case QEMU_OPTION_enable_kvm:
2437 kvm_allowed = 1;
2438 #endif
2439 break;
2440 case QEMU_OPTION_no_kvm:
2441 kvm_allowed = 0;
2442 break;
2443 case QEMU_OPTION_no_kvm_irqchip: {
2444 kvm_irqchip = 0;
2445 kvm_pit = 0;
2446 break;
2448 case QEMU_OPTION_no_kvm_pit: {
2449 kvm_pit = 0;
2450 break;
2452 case QEMU_OPTION_no_kvm_pit_reinjection: {
2453 kvm_pit_reinject = 0;
2454 break;
2456 case QEMU_OPTION_enable_nesting: {
2457 kvm_nested = 1;
2458 break;
2460 #if defined(TARGET_I386) || defined(TARGET_X86_64) || defined(TARGET_IA64) || defined(__linux__)
2461 case QEMU_OPTION_pcidevice:
2462 if (assigned_devices_index >= MAX_DEV_ASSIGN_CMDLINE) {
2463 fprintf(stderr, "Too many assigned devices\n");
2464 exit(1);
2466 assigned_devices[assigned_devices_index] = optarg;
2467 assigned_devices_index++;
2468 break;
2469 #endif
2470 case QEMU_OPTION_usb:
2471 usb_enabled = 1;
2472 break;
2473 case QEMU_OPTION_usbdevice:
2474 usb_enabled = 1;
2475 add_device_config(DEV_USB, optarg);
2476 break;
2477 case QEMU_OPTION_device:
2478 if (!qemu_opts_parse(&qemu_device_opts, optarg, 1)) {
2479 exit(1);
2481 break;
2482 case QEMU_OPTION_smp:
2483 smp_parse(optarg);
2484 if (smp_cpus < 1) {
2485 fprintf(stderr, "Invalid number of CPUs\n");
2486 exit(1);
2488 if (max_cpus < smp_cpus) {
2489 fprintf(stderr, "maxcpus must be equal to or greater than "
2490 "smp\n");
2491 exit(1);
2493 if (max_cpus > 255) {
2494 fprintf(stderr, "Unsupported number of maxcpus\n");
2495 exit(1);
2497 break;
2498 case QEMU_OPTION_vnc:
2499 display_type = DT_VNC;
2500 vnc_display = optarg;
2501 break;
2502 case QEMU_OPTION_no_acpi:
2503 acpi_enabled = 0;
2504 break;
2505 case QEMU_OPTION_no_hpet:
2506 no_hpet = 1;
2507 break;
2508 case QEMU_OPTION_balloon:
2509 if (balloon_parse(optarg) < 0) {
2510 fprintf(stderr, "Unknown -balloon argument %s\n", optarg);
2511 exit(1);
2513 break;
2514 case QEMU_OPTION_no_reboot:
2515 no_reboot = 1;
2516 break;
2517 case QEMU_OPTION_no_shutdown:
2518 no_shutdown = 1;
2519 break;
2520 case QEMU_OPTION_show_cursor:
2521 cursor_hide = 0;
2522 break;
2523 case QEMU_OPTION_uuid:
2524 if(qemu_uuid_parse(optarg, qemu_uuid) < 0) {
2525 fprintf(stderr, "Fail to parse UUID string."
2526 " Wrong format.\n");
2527 exit(1);
2529 break;
2530 case QEMU_OPTION_option_rom:
2531 if (nb_option_roms >= MAX_OPTION_ROMS) {
2532 fprintf(stderr, "Too many option ROMs\n");
2533 exit(1);
2535 option_rom[nb_option_roms] = optarg;
2536 nb_option_roms++;
2537 break;
2538 case QEMU_OPTION_semihosting:
2539 semihosting_enabled = 1;
2540 break;
2541 case QEMU_OPTION_tdf:
2542 time_drift_fix = 1;
2543 break;
2544 case QEMU_OPTION_kvm_shadow_memory:
2545 kvm_shadow_memory = (int64_t)atoi(optarg) * 1024 * 1024 / 4096;
2546 break;
2547 case QEMU_OPTION_name:
2548 qemu_name = qemu_strdup(optarg);
2550 char *p = strchr(qemu_name, ',');
2551 if (p != NULL) {
2552 *p++ = 0;
2553 if (strncmp(p, "process=", 8)) {
2554 fprintf(stderr, "Unknown subargument %s to -name", p);
2555 exit(1);
2557 p += 8;
2558 os_set_proc_name(p);
2561 break;
2562 case QEMU_OPTION_prom_env:
2563 if (nb_prom_envs >= MAX_PROM_ENVS) {
2564 fprintf(stderr, "Too many prom variables\n");
2565 exit(1);
2567 prom_envs[nb_prom_envs] = optarg;
2568 nb_prom_envs++;
2569 break;
2570 case QEMU_OPTION_old_param:
2571 old_param = 1;
2572 break;
2573 case QEMU_OPTION_clock:
2574 configure_alarms(optarg);
2575 break;
2576 case QEMU_OPTION_startdate:
2577 configure_rtc_date_offset(optarg, 1);
2578 break;
2579 case QEMU_OPTION_rtc:
2580 opts = qemu_opts_parse(&qemu_rtc_opts, optarg, 0);
2581 if (!opts) {
2582 exit(1);
2584 configure_rtc(opts);
2585 break;
2586 case QEMU_OPTION_tb_size:
2587 tb_size = strtol(optarg, NULL, 0);
2588 if (tb_size < 0)
2589 tb_size = 0;
2590 break;
2591 case QEMU_OPTION_icount:
2592 icount_option = optarg;
2593 break;
2594 case QEMU_OPTION_incoming:
2595 incoming = optarg;
2596 break;
2597 case QEMU_OPTION_nodefaults:
2598 default_serial = 0;
2599 default_parallel = 0;
2600 default_virtcon = 0;
2601 default_monitor = 0;
2602 default_vga = 0;
2603 default_net = 0;
2604 default_floppy = 0;
2605 default_cdrom = 0;
2606 default_sdcard = 0;
2607 break;
2608 #ifndef _WIN32
2609 case QEMU_OPTION_nvram:
2610 nvram = optarg;
2611 break;
2612 #endif
2613 case QEMU_OPTION_xen_domid:
2614 if (!(xen_available())) {
2615 printf("Option %s not supported for this target\n", popt->name);
2616 exit(1);
2618 xen_domid = atoi(optarg);
2619 break;
2620 case QEMU_OPTION_xen_create:
2621 if (!(xen_available())) {
2622 printf("Option %s not supported for this target\n", popt->name);
2623 exit(1);
2625 xen_mode = XEN_CREATE;
2626 break;
2627 case QEMU_OPTION_xen_attach:
2628 if (!(xen_available())) {
2629 printf("Option %s not supported for this target\n", popt->name);
2630 exit(1);
2632 xen_mode = XEN_ATTACH;
2633 break;
2634 case QEMU_OPTION_readconfig:
2636 int ret = qemu_read_config_file(optarg);
2637 if (ret < 0) {
2638 fprintf(stderr, "read config %s: %s\n", optarg,
2639 strerror(-ret));
2640 exit(1);
2642 break;
2644 case QEMU_OPTION_writeconfig:
2646 FILE *fp;
2647 if (strcmp(optarg, "-") == 0) {
2648 fp = stdout;
2649 } else {
2650 fp = fopen(optarg, "w");
2651 if (fp == NULL) {
2652 fprintf(stderr, "open %s: %s\n", optarg, strerror(errno));
2653 exit(1);
2656 qemu_config_write(fp);
2657 fclose(fp);
2658 break;
2660 default:
2661 os_parse_cmd_args(popt->index, optarg);
2665 loc_set_none();
2667 /* If no data_dir is specified then try to find it relative to the
2668 executable path. */
2669 if (!data_dir) {
2670 data_dir = os_find_datadir(argv[0]);
2672 /* If all else fails use the install patch specified when building. */
2673 if (!data_dir) {
2674 data_dir = CONFIG_QEMU_DATADIR;
2678 * Default to max_cpus = smp_cpus, in case the user doesn't
2679 * specify a max_cpus value.
2681 if (!max_cpus)
2682 max_cpus = smp_cpus;
2684 machine->max_cpus = machine->max_cpus ?: 1; /* Default to UP */
2685 if (smp_cpus > machine->max_cpus) {
2686 fprintf(stderr, "Number of SMP cpus requested (%d), exceeds max cpus "
2687 "supported by machine `%s' (%d)\n", smp_cpus, machine->name,
2688 machine->max_cpus);
2689 exit(1);
2692 qemu_opts_foreach(&qemu_device_opts, default_driver_check, NULL, 0);
2693 qemu_opts_foreach(&qemu_global_opts, default_driver_check, NULL, 0);
2695 if (machine->no_serial) {
2696 default_serial = 0;
2698 if (machine->no_parallel) {
2699 default_parallel = 0;
2701 if (!machine->use_virtcon) {
2702 default_virtcon = 0;
2704 if (machine->no_vga) {
2705 default_vga = 0;
2707 if (machine->no_floppy) {
2708 default_floppy = 0;
2710 if (machine->no_cdrom) {
2711 default_cdrom = 0;
2713 if (machine->no_sdcard) {
2714 default_sdcard = 0;
2717 if (display_type == DT_NOGRAPHIC) {
2718 if (default_parallel)
2719 add_device_config(DEV_PARALLEL, "null");
2720 if (default_serial && default_monitor) {
2721 add_device_config(DEV_SERIAL, "mon:stdio");
2722 } else if (default_virtcon && default_monitor) {
2723 add_device_config(DEV_VIRTCON, "mon:stdio");
2724 } else {
2725 if (default_serial)
2726 add_device_config(DEV_SERIAL, "stdio");
2727 if (default_virtcon)
2728 add_device_config(DEV_VIRTCON, "stdio");
2729 if (default_monitor)
2730 monitor_parse("stdio", "readline");
2732 } else {
2733 if (default_serial)
2734 add_device_config(DEV_SERIAL, "vc:80Cx24C");
2735 if (default_parallel)
2736 add_device_config(DEV_PARALLEL, "vc:80Cx24C");
2737 if (default_monitor)
2738 monitor_parse("vc:80Cx24C", "readline");
2739 if (default_virtcon)
2740 add_device_config(DEV_VIRTCON, "vc:80Cx24C");
2742 if (default_vga)
2743 vga_interface_type = VGA_CIRRUS;
2745 socket_init();
2747 if (qemu_opts_foreach(&qemu_chardev_opts, chardev_init_func, NULL, 1) != 0)
2748 exit(1);
2749 #ifdef CONFIG_LINUX
2750 if (qemu_opts_foreach(&qemu_fsdev_opts, fsdev_init_func, NULL, 1) != 0) {
2751 exit(1);
2753 #endif
2755 os_daemonize();
2757 if (pid_file && qemu_create_pidfile(pid_file) != 0) {
2758 os_pidfile_error();
2759 exit(1);
2762 if (kvm_allowed) {
2763 int ret = kvm_init(smp_cpus);
2764 if (ret < 0) {
2765 #if defined(KVM_UPSTREAM) || defined(CONFIG_NO_CPU_EMULATION)
2766 if (!kvm_available()) {
2767 printf("KVM not supported for this target\n");
2768 } else {
2769 fprintf(stderr, "failed to initialize KVM: %s\n", strerror(-ret));
2771 exit(1);
2772 #endif
2773 #ifdef CONFIG_KVM
2774 fprintf(stderr, "Could not initialize KVM, will disable KVM support\n");
2775 kvm_allowed = 0;
2776 #endif
2780 if (qemu_init_main_loop()) {
2781 fprintf(stderr, "qemu_init_main_loop failed\n");
2782 exit(1);
2784 linux_boot = (kernel_filename != NULL);
2786 if (!linux_boot && *kernel_cmdline != '\0') {
2787 fprintf(stderr, "-append only allowed with -kernel option\n");
2788 exit(1);
2791 if (!linux_boot && initrd_filename != NULL) {
2792 fprintf(stderr, "-initrd only allowed with -kernel option\n");
2793 exit(1);
2796 os_set_line_buffering();
2798 if (init_timer_alarm() < 0) {
2799 fprintf(stderr, "could not initialize alarm timer\n");
2800 exit(1);
2802 configure_icount(icount_option);
2804 if (net_init_clients() < 0) {
2805 exit(1);
2808 /* init the bluetooth world */
2809 if (foreach_device_config(DEV_BT, bt_parse))
2810 exit(1);
2812 /* init the memory */
2813 if (ram_size == 0)
2814 ram_size = DEFAULT_RAM_SIZE * 1024 * 1024;
2816 /* init the dynamic translator */
2817 cpu_exec_init_all(tb_size * 1024 * 1024);
2819 bdrv_init_with_whitelist();
2821 blk_mig_init();
2823 if (default_cdrom) {
2824 /* we always create the cdrom drive, even if no disk is there */
2825 drive_add(NULL, CDROM_ALIAS);
2828 if (default_floppy) {
2829 /* we always create at least one floppy */
2830 drive_add(NULL, FD_ALIAS, 0);
2833 if (default_sdcard) {
2834 /* we always create one sd slot, even if no card is in it */
2835 drive_add(NULL, SD_ALIAS);
2838 /* open the virtual block devices */
2839 if (snapshot)
2840 qemu_opts_foreach(&qemu_drive_opts, drive_enable_snapshot, NULL, 0);
2841 if (qemu_opts_foreach(&qemu_drive_opts, drive_init_func, &machine->use_scsi, 1) != 0)
2842 exit(1);
2844 register_savevm_live("ram", 0, 3, NULL, ram_save_live, NULL,
2845 ram_load, NULL);
2847 if (nb_numa_nodes > 0) {
2848 int i;
2850 if (nb_numa_nodes > smp_cpus) {
2851 nb_numa_nodes = smp_cpus;
2854 /* If no memory size if given for any node, assume the default case
2855 * and distribute the available memory equally across all nodes
2857 for (i = 0; i < nb_numa_nodes; i++) {
2858 if (node_mem[i] != 0)
2859 break;
2861 if (i == nb_numa_nodes) {
2862 uint64_t usedmem = 0;
2864 /* On Linux, the each node's border has to be 8MB aligned,
2865 * the final node gets the rest.
2867 for (i = 0; i < nb_numa_nodes - 1; i++) {
2868 node_mem[i] = (ram_size / nb_numa_nodes) & ~((1 << 23UL) - 1);
2869 usedmem += node_mem[i];
2871 node_mem[i] = ram_size - usedmem;
2874 for (i = 0; i < nb_numa_nodes; i++) {
2875 if (node_cpumask[i] != 0)
2876 break;
2878 /* assigning the VCPUs round-robin is easier to implement, guest OSes
2879 * must cope with this anyway, because there are BIOSes out there in
2880 * real machines which also use this scheme.
2882 if (i == nb_numa_nodes) {
2883 for (i = 0; i < smp_cpus; i++) {
2884 node_cpumask[i % nb_numa_nodes] |= 1 << i;
2889 if (qemu_opts_foreach(&qemu_mon_opts, mon_init_func, NULL, 1) != 0) {
2890 exit(1);
2893 if (foreach_device_config(DEV_SERIAL, serial_parse) < 0)
2894 exit(1);
2895 if (foreach_device_config(DEV_PARALLEL, parallel_parse) < 0)
2896 exit(1);
2897 if (foreach_device_config(DEV_VIRTCON, virtcon_parse) < 0)
2898 exit(1);
2899 if (foreach_device_config(DEV_DEBUGCON, debugcon_parse) < 0)
2900 exit(1);
2902 module_call_init(MODULE_INIT_DEVICE);
2904 if (qemu_opts_foreach(&qemu_device_opts, device_help_func, NULL, 0) != 0)
2905 exit(0);
2907 if (watchdog) {
2908 i = select_watchdog(watchdog);
2909 if (i > 0)
2910 exit (i == 1 ? 1 : 0);
2913 if (machine->compat_props) {
2914 qdev_prop_register_global_list(machine->compat_props);
2916 qemu_add_globals();
2918 machine->init(ram_size, boot_devices,
2919 kernel_filename, kernel_cmdline, initrd_filename, cpu_model);
2921 cpu_synchronize_all_post_init();
2923 /* must be after terminal init, SDL library changes signal handlers */
2924 os_setup_signal_handling();
2926 set_numa_modes();
2928 current_machine = machine;
2930 /* init USB devices */
2931 if (usb_enabled) {
2932 if (foreach_device_config(DEV_USB, usb_parse) < 0)
2933 exit(1);
2936 /* init generic devices */
2937 if (qemu_opts_foreach(&qemu_device_opts, device_init_func, NULL, 1) != 0)
2938 exit(1);
2940 net_check_clients();
2942 /* just use the first displaystate for the moment */
2943 ds = get_displaystate();
2945 if (display_type == DT_DEFAULT) {
2946 #if defined(CONFIG_SDL) || defined(CONFIG_COCOA)
2947 display_type = DT_SDL;
2948 #else
2949 display_type = DT_VNC;
2950 vnc_display = "localhost:0,to=99";
2951 show_vnc_port = 1;
2952 #endif
2956 switch (display_type) {
2957 case DT_NOGRAPHIC:
2958 break;
2959 #if defined(CONFIG_CURSES)
2960 case DT_CURSES:
2961 curses_display_init(ds, full_screen);
2962 break;
2963 #endif
2964 #if defined(CONFIG_SDL)
2965 case DT_SDL:
2966 sdl_display_init(ds, full_screen, no_frame);
2967 break;
2968 #elif defined(CONFIG_COCOA)
2969 case DT_SDL:
2970 cocoa_display_init(ds, full_screen);
2971 break;
2972 #endif
2973 case DT_VNC:
2974 vnc_display_init(ds);
2975 if (vnc_display_open(ds, vnc_display) < 0)
2976 exit(1);
2978 if (show_vnc_port) {
2979 printf("VNC server running on `%s'\n", vnc_display_local_addr(ds));
2981 break;
2982 default:
2983 break;
2985 dpy_resize(ds);
2987 dcl = ds->listeners;
2988 while (dcl != NULL) {
2989 if (dcl->dpy_refresh != NULL) {
2990 ds->gui_timer = qemu_new_timer(rt_clock, gui_update, ds);
2991 qemu_mod_timer(ds->gui_timer, qemu_get_clock(rt_clock));
2992 break;
2994 dcl = dcl->next;
2997 if (display_type == DT_NOGRAPHIC || display_type == DT_VNC) {
2998 nographic_timer = qemu_new_timer(rt_clock, nographic_update, NULL);
2999 qemu_mod_timer(nographic_timer, qemu_get_clock(rt_clock));
3002 text_consoles_set_display(ds);
3004 if (gdbstub_dev && gdbserver_start(gdbstub_dev) < 0) {
3005 fprintf(stderr, "qemu: could not open gdbserver on device '%s'\n",
3006 gdbstub_dev);
3007 exit(1);
3010 qdev_machine_creation_done();
3012 if (rom_load_all() != 0) {
3013 fprintf(stderr, "rom loading failed\n");
3014 exit(1);
3017 qemu_system_reset();
3018 if (loadvm) {
3019 if (load_vmstate(loadvm) < 0) {
3020 autostart = 0;
3024 if (incoming) {
3025 qemu_start_incoming_migration(incoming);
3026 } else if (autostart) {
3027 vm_start();
3030 os_setup_post();
3032 main_loop();
3033 quit_timers();
3034 net_cleanup();
3036 return 0;