pci: Remove pci_enable_capability_support()
[qemu-kvm/stefanha.git] / vl.c
blob37ed560ee8cd78404b4e5e2a24065798f38f6216
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_SIMPLE_TRACE
51 #include "trace.h"
52 #endif
54 #ifdef CONFIG_BSD
55 #include <sys/stat.h>
56 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__DragonFly__)
57 #include <libutil.h>
58 #include <sys/sysctl.h>
59 #else
60 #include <util.h>
61 #endif
62 #else
63 #ifdef __linux__
64 #include <pty.h>
65 #include <malloc.h>
67 #include <linux/ppdev.h>
68 #include <linux/parport.h>
69 #endif
70 #ifdef __sun__
71 #include <sys/stat.h>
72 #include <sys/ethernet.h>
73 #include <sys/sockio.h>
74 #include <netinet/arp.h>
75 #include <netinet/in_systm.h>
76 #include <netinet/ip.h>
77 #include <netinet/ip_icmp.h> // must come after ip.h
78 #include <netinet/udp.h>
79 #include <netinet/tcp.h>
80 #include <net/if.h>
81 #include <syslog.h>
82 #include <stropts.h>
83 #endif
84 #endif
85 #endif
87 #if defined(__OpenBSD__)
88 #include <util.h>
89 #endif
91 #if defined(CONFIG_VDE)
92 #include <libvdeplug.h>
93 #endif
95 #ifdef _WIN32
96 #include <windows.h>
97 #endif
99 #ifdef CONFIG_SDL
100 #if defined(__APPLE__) || defined(main)
101 #include <SDL.h>
102 int qemu_main(int argc, char **argv, char **envp);
103 int main(int argc, char **argv)
105 return qemu_main(argc, argv, NULL);
107 #undef main
108 #define main qemu_main
109 #endif
110 #endif /* CONFIG_SDL */
112 #ifdef CONFIG_COCOA
113 #undef main
114 #define main qemu_main
115 #endif /* CONFIG_COCOA */
117 #include "hw/hw.h"
118 #include "hw/boards.h"
119 #include "hw/usb.h"
120 #include "hw/pcmcia.h"
121 #include "hw/pc.h"
122 #include "hw/isa.h"
123 #include "hw/baum.h"
124 #include "hw/bt.h"
125 #include "hw/watchdog.h"
126 #include "hw/smbios.h"
127 #include "hw/xen.h"
128 #include "hw/qdev.h"
129 #include "hw/loader.h"
130 #include "bt-host.h"
131 #include "net.h"
132 #include "net/slirp.h"
133 #include "monitor.h"
134 #include "console.h"
135 #include "sysemu.h"
136 #include "gdbstub.h"
137 #include "qemu-timer.h"
138 #include "qemu-char.h"
139 #include "cache-utils.h"
140 #include "block.h"
141 #include "blockdev.h"
142 #include "block-migration.h"
143 #include "dma.h"
144 #include "audio/audio.h"
145 #include "migration.h"
146 #include "kvm.h"
147 #include "qemu-option.h"
148 #include "qemu-config.h"
149 #include "qemu-objects.h"
150 #include "qemu-options.h"
151 #include "hw/device-assignment.h"
152 #ifdef CONFIG_VIRTFS
153 #include "fsdev/qemu-fsdev.h"
154 #endif
156 #include "disas.h"
158 #include "qemu_socket.h"
160 #include "slirp/libslirp.h"
162 #include "qemu-queue.h"
163 #include "cpus.h"
164 #include "arch_init.h"
166 #include "ui/qemu-spice.h"
168 //#define DEBUG_NET
169 //#define DEBUG_SLIRP
171 #define DEFAULT_RAM_SIZE 128
173 #define MAX_VIRTIO_CONSOLES 1
175 static const char *data_dir;
176 const char *bios_name = NULL;
177 enum vga_retrace_method vga_retrace_method = VGA_RETRACE_DUMB;
178 DisplayType display_type = DT_DEFAULT;
179 int display_remote = 0;
180 const char* keyboard_layout = NULL;
181 ram_addr_t ram_size;
182 const char *mem_path = NULL;
183 #ifdef MAP_POPULATE
184 int mem_prealloc = 0; /* force preallocation of physical target memory */
185 #endif
186 int nb_nics;
187 NICInfo nd_table[MAX_NICS];
188 int vm_running;
189 int autostart;
190 int incoming_expected; /* Started with -incoming and waiting for incoming */
191 static int rtc_utc = 1;
192 static int rtc_date_offset = -1; /* -1 means no change */
193 QEMUClock *rtc_clock;
194 int vga_interface_type = VGA_NONE;
195 static int full_screen = 0;
196 #ifdef CONFIG_SDL
197 static int no_frame = 0;
198 #endif
199 int no_quit = 0;
200 CharDriverState *serial_hds[MAX_SERIAL_PORTS];
201 CharDriverState *parallel_hds[MAX_PARALLEL_PORTS];
202 CharDriverState *virtcon_hds[MAX_VIRTIO_CONSOLES];
203 int win2k_install_hack = 0;
204 int rtc_td_hack = 0;
205 int usb_enabled = 0;
206 int singlestep = 0;
207 int smp_cpus = 1;
208 int max_cpus = 0;
209 int smp_cores = 1;
210 int smp_threads = 1;
211 const char *vnc_display;
212 int acpi_enabled = 1;
213 int no_hpet = 0;
214 int fd_bootchk = 1;
215 int no_reboot = 0;
216 int no_shutdown = 0;
217 int cursor_hide = 1;
218 int graphic_rotate = 0;
219 uint8_t irq0override = 1;
220 const char *watchdog;
221 const char *option_rom[MAX_OPTION_ROMS];
222 int nb_option_roms;
223 int semihosting_enabled = 0;
224 int time_drift_fix = 0;
225 unsigned int kvm_shadow_memory = 0;
226 int old_param = 0;
227 const char *qemu_name;
228 int alt_grab = 0;
229 int ctrl_grab = 0;
230 unsigned int nb_prom_envs = 0;
231 const char *prom_envs[MAX_PROM_ENVS];
232 const char *nvram = NULL;
233 int boot_menu;
235 int nb_numa_nodes;
236 uint64_t node_mem[MAX_NODES];
237 uint64_t node_cpumask[MAX_NODES];
239 static QEMUTimer *nographic_timer;
241 uint8_t qemu_uuid[16];
243 static QEMUBootSetHandler *boot_set_handler;
244 static void *boot_set_opaque;
246 static NotifierList exit_notifiers =
247 NOTIFIER_LIST_INITIALIZER(exit_notifiers);
249 int kvm_allowed = 1;
250 uint32_t xen_domid;
251 enum xen_mode xen_mode = XEN_EMULATE;
253 static int default_serial = 1;
254 static int default_parallel = 1;
255 static int default_virtcon = 1;
256 static int default_monitor = 1;
257 static int default_vga = 1;
258 static int default_floppy = 1;
259 static int default_cdrom = 1;
260 static int default_sdcard = 1;
262 static struct {
263 const char *driver;
264 int *flag;
265 } default_list[] = {
266 { .driver = "isa-serial", .flag = &default_serial },
267 { .driver = "isa-parallel", .flag = &default_parallel },
268 { .driver = "isa-fdc", .flag = &default_floppy },
269 { .driver = "ide-drive", .flag = &default_cdrom },
270 { .driver = "virtio-serial-pci", .flag = &default_virtcon },
271 { .driver = "virtio-serial-s390", .flag = &default_virtcon },
272 { .driver = "virtio-serial", .flag = &default_virtcon },
273 { .driver = "VGA", .flag = &default_vga },
274 { .driver = "cirrus-vga", .flag = &default_vga },
275 { .driver = "vmware-svga", .flag = &default_vga },
278 static int default_driver_check(QemuOpts *opts, void *opaque)
280 const char *driver = qemu_opt_get(opts, "driver");
281 int i;
283 if (!driver)
284 return 0;
285 for (i = 0; i < ARRAY_SIZE(default_list); i++) {
286 if (strcmp(default_list[i].driver, driver) != 0)
287 continue;
288 *(default_list[i].flag) = 0;
290 return 0;
293 /***********************************************************/
294 /* real time host monotonic timer */
296 /***********************************************************/
297 /* host time/date access */
298 void qemu_get_timedate(struct tm *tm, int offset)
300 time_t ti;
301 struct tm *ret;
303 time(&ti);
304 ti += offset;
305 if (rtc_date_offset == -1) {
306 if (rtc_utc)
307 ret = gmtime(&ti);
308 else
309 ret = localtime(&ti);
310 } else {
311 ti -= rtc_date_offset;
312 ret = gmtime(&ti);
315 memcpy(tm, ret, sizeof(struct tm));
318 int qemu_timedate_diff(struct tm *tm)
320 time_t seconds;
322 if (rtc_date_offset == -1)
323 if (rtc_utc)
324 seconds = mktimegm(tm);
325 else
326 seconds = mktime(tm);
327 else
328 seconds = mktimegm(tm) + rtc_date_offset;
330 return seconds - time(NULL);
333 void rtc_change_mon_event(struct tm *tm)
335 QObject *data;
337 data = qobject_from_jsonf("{ 'offset': %d }", qemu_timedate_diff(tm));
338 monitor_protocol_event(QEVENT_RTC_CHANGE, data);
339 qobject_decref(data);
342 static void configure_rtc_date_offset(const char *startdate, int legacy)
344 time_t rtc_start_date;
345 struct tm tm;
347 if (!strcmp(startdate, "now") && legacy) {
348 rtc_date_offset = -1;
349 } else {
350 if (sscanf(startdate, "%d-%d-%dT%d:%d:%d",
351 &tm.tm_year,
352 &tm.tm_mon,
353 &tm.tm_mday,
354 &tm.tm_hour,
355 &tm.tm_min,
356 &tm.tm_sec) == 6) {
357 /* OK */
358 } else if (sscanf(startdate, "%d-%d-%d",
359 &tm.tm_year,
360 &tm.tm_mon,
361 &tm.tm_mday) == 3) {
362 tm.tm_hour = 0;
363 tm.tm_min = 0;
364 tm.tm_sec = 0;
365 } else {
366 goto date_fail;
368 tm.tm_year -= 1900;
369 tm.tm_mon--;
370 rtc_start_date = mktimegm(&tm);
371 if (rtc_start_date == -1) {
372 date_fail:
373 fprintf(stderr, "Invalid date format. Valid formats are:\n"
374 "'2006-06-17T16:01:21' or '2006-06-17'\n");
375 exit(1);
377 rtc_date_offset = time(NULL) - rtc_start_date;
381 static void configure_rtc(QemuOpts *opts)
383 const char *value;
385 value = qemu_opt_get(opts, "base");
386 if (value) {
387 if (!strcmp(value, "utc")) {
388 rtc_utc = 1;
389 } else if (!strcmp(value, "localtime")) {
390 rtc_utc = 0;
391 } else {
392 configure_rtc_date_offset(value, 0);
395 value = qemu_opt_get(opts, "clock");
396 if (value) {
397 if (!strcmp(value, "host")) {
398 rtc_clock = host_clock;
399 } else if (!strcmp(value, "vm")) {
400 rtc_clock = vm_clock;
401 } else {
402 fprintf(stderr, "qemu: invalid option value '%s'\n", value);
403 exit(1);
406 value = qemu_opt_get(opts, "driftfix");
407 if (value) {
408 if (!strcmp(value, "slew")) {
409 rtc_td_hack = 1;
410 } else if (!strcmp(value, "none")) {
411 rtc_td_hack = 0;
412 } else {
413 fprintf(stderr, "qemu: invalid option value '%s'\n", value);
414 exit(1);
419 /***********************************************************/
420 /* Bluetooth support */
421 static int nb_hcis;
422 static int cur_hci;
423 static struct HCIInfo *hci_table[MAX_NICS];
425 static struct bt_vlan_s {
426 struct bt_scatternet_s net;
427 int id;
428 struct bt_vlan_s *next;
429 } *first_bt_vlan;
431 /* find or alloc a new bluetooth "VLAN" */
432 static struct bt_scatternet_s *qemu_find_bt_vlan(int id)
434 struct bt_vlan_s **pvlan, *vlan;
435 for (vlan = first_bt_vlan; vlan != NULL; vlan = vlan->next) {
436 if (vlan->id == id)
437 return &vlan->net;
439 vlan = qemu_mallocz(sizeof(struct bt_vlan_s));
440 vlan->id = id;
441 pvlan = &first_bt_vlan;
442 while (*pvlan != NULL)
443 pvlan = &(*pvlan)->next;
444 *pvlan = vlan;
445 return &vlan->net;
448 static void null_hci_send(struct HCIInfo *hci, const uint8_t *data, int len)
452 static int null_hci_addr_set(struct HCIInfo *hci, const uint8_t *bd_addr)
454 return -ENOTSUP;
457 static struct HCIInfo null_hci = {
458 .cmd_send = null_hci_send,
459 .sco_send = null_hci_send,
460 .acl_send = null_hci_send,
461 .bdaddr_set = null_hci_addr_set,
464 struct HCIInfo *qemu_next_hci(void)
466 if (cur_hci == nb_hcis)
467 return &null_hci;
469 return hci_table[cur_hci++];
472 static struct HCIInfo *hci_init(const char *str)
474 char *endp;
475 struct bt_scatternet_s *vlan = 0;
477 if (!strcmp(str, "null"))
478 /* null */
479 return &null_hci;
480 else if (!strncmp(str, "host", 4) && (str[4] == '\0' || str[4] == ':'))
481 /* host[:hciN] */
482 return bt_host_hci(str[4] ? str + 5 : "hci0");
483 else if (!strncmp(str, "hci", 3)) {
484 /* hci[,vlan=n] */
485 if (str[3]) {
486 if (!strncmp(str + 3, ",vlan=", 6)) {
487 vlan = qemu_find_bt_vlan(strtol(str + 9, &endp, 0));
488 if (*endp)
489 vlan = 0;
491 } else
492 vlan = qemu_find_bt_vlan(0);
493 if (vlan)
494 return bt_new_hci(vlan);
497 fprintf(stderr, "qemu: Unknown bluetooth HCI `%s'.\n", str);
499 return 0;
502 static int bt_hci_parse(const char *str)
504 struct HCIInfo *hci;
505 bdaddr_t bdaddr;
507 if (nb_hcis >= MAX_NICS) {
508 fprintf(stderr, "qemu: Too many bluetooth HCIs (max %i).\n", MAX_NICS);
509 return -1;
512 hci = hci_init(str);
513 if (!hci)
514 return -1;
516 bdaddr.b[0] = 0x52;
517 bdaddr.b[1] = 0x54;
518 bdaddr.b[2] = 0x00;
519 bdaddr.b[3] = 0x12;
520 bdaddr.b[4] = 0x34;
521 bdaddr.b[5] = 0x56 + nb_hcis;
522 hci->bdaddr_set(hci, bdaddr.b);
524 hci_table[nb_hcis++] = hci;
526 return 0;
529 static void bt_vhci_add(int vlan_id)
531 struct bt_scatternet_s *vlan = qemu_find_bt_vlan(vlan_id);
533 if (!vlan->slave)
534 fprintf(stderr, "qemu: warning: adding a VHCI to "
535 "an empty scatternet %i\n", vlan_id);
537 bt_vhci_init(bt_new_hci(vlan));
540 static struct bt_device_s *bt_device_add(const char *opt)
542 struct bt_scatternet_s *vlan;
543 int vlan_id = 0;
544 char *endp = strstr(opt, ",vlan=");
545 int len = (endp ? endp - opt : strlen(opt)) + 1;
546 char devname[10];
548 pstrcpy(devname, MIN(sizeof(devname), len), opt);
550 if (endp) {
551 vlan_id = strtol(endp + 6, &endp, 0);
552 if (*endp) {
553 fprintf(stderr, "qemu: unrecognised bluetooth vlan Id\n");
554 return 0;
558 vlan = qemu_find_bt_vlan(vlan_id);
560 if (!vlan->slave)
561 fprintf(stderr, "qemu: warning: adding a slave device to "
562 "an empty scatternet %i\n", vlan_id);
564 if (!strcmp(devname, "keyboard"))
565 return bt_keyboard_init(vlan);
567 fprintf(stderr, "qemu: unsupported bluetooth device `%s'\n", devname);
568 return 0;
571 static int bt_parse(const char *opt)
573 const char *endp, *p;
574 int vlan;
576 if (strstart(opt, "hci", &endp)) {
577 if (!*endp || *endp == ',') {
578 if (*endp)
579 if (!strstart(endp, ",vlan=", 0))
580 opt = endp + 1;
582 return bt_hci_parse(opt);
584 } else if (strstart(opt, "vhci", &endp)) {
585 if (!*endp || *endp == ',') {
586 if (*endp) {
587 if (strstart(endp, ",vlan=", &p)) {
588 vlan = strtol(p, (char **) &endp, 0);
589 if (*endp) {
590 fprintf(stderr, "qemu: bad scatternet '%s'\n", p);
591 return 1;
593 } else {
594 fprintf(stderr, "qemu: bad parameter '%s'\n", endp + 1);
595 return 1;
597 } else
598 vlan = 0;
600 bt_vhci_add(vlan);
601 return 0;
603 } else if (strstart(opt, "device:", &endp))
604 return !bt_device_add(endp);
606 fprintf(stderr, "qemu: bad bluetooth parameter '%s'\n", opt);
607 return 1;
610 /***********************************************************/
611 /* QEMU Block devices */
613 #define HD_ALIAS "index=%d,media=disk"
614 #define CDROM_ALIAS "index=2,media=cdrom"
615 #define FD_ALIAS "index=%d,if=floppy"
616 #define PFLASH_ALIAS "if=pflash"
617 #define MTD_ALIAS "if=mtd"
618 #define SD_ALIAS "index=0,if=sd"
620 static int drive_init_func(QemuOpts *opts, void *opaque)
622 int *use_scsi = opaque;
623 int fatal_error = 0;
625 if (drive_init(opts, *use_scsi, &fatal_error) == NULL) {
626 if (fatal_error)
627 return 1;
629 return 0;
632 static int drive_enable_snapshot(QemuOpts *opts, void *opaque)
634 if (NULL == qemu_opt_get(opts, "snapshot")) {
635 qemu_opt_set(opts, "snapshot", "on");
637 return 0;
640 void qemu_register_boot_set(QEMUBootSetHandler *func, void *opaque)
642 boot_set_handler = func;
643 boot_set_opaque = opaque;
646 int qemu_boot_set(const char *boot_devices)
648 if (!boot_set_handler) {
649 return -EINVAL;
651 return boot_set_handler(boot_set_opaque, boot_devices);
654 static void validate_bootdevices(char *devices)
656 /* We just do some generic consistency checks */
657 const char *p;
658 int bitmap = 0;
660 for (p = devices; *p != '\0'; p++) {
661 /* Allowed boot devices are:
662 * a-b: floppy disk drives
663 * c-f: IDE disk drives
664 * g-m: machine implementation dependant drives
665 * n-p: network devices
666 * It's up to each machine implementation to check if the given boot
667 * devices match the actual hardware implementation and firmware
668 * features.
670 if (*p < 'a' || *p > 'p') {
671 fprintf(stderr, "Invalid boot device '%c'\n", *p);
672 exit(1);
674 if (bitmap & (1 << (*p - 'a'))) {
675 fprintf(stderr, "Boot device '%c' was given twice\n", *p);
676 exit(1);
678 bitmap |= 1 << (*p - 'a');
682 static void restore_boot_devices(void *opaque)
684 char *standard_boot_devices = opaque;
685 static int first = 1;
687 /* Restore boot order and remove ourselves after the first boot */
688 if (first) {
689 first = 0;
690 return;
693 qemu_boot_set(standard_boot_devices);
695 qemu_unregister_reset(restore_boot_devices, standard_boot_devices);
696 qemu_free(standard_boot_devices);
699 static void numa_add(const char *optarg)
701 char option[128];
702 char *endptr;
703 unsigned long long value, endvalue;
704 int nodenr;
706 optarg = get_opt_name(option, 128, optarg, ',') + 1;
707 if (!strcmp(option, "node")) {
708 if (get_param_value(option, 128, "nodeid", optarg) == 0) {
709 nodenr = nb_numa_nodes;
710 } else {
711 nodenr = strtoull(option, NULL, 10);
714 if (get_param_value(option, 128, "mem", optarg) == 0) {
715 node_mem[nodenr] = 0;
716 } else {
717 ssize_t sval;
718 sval = strtosz(option, NULL);
719 if (sval < 0) {
720 fprintf(stderr, "qemu: invalid numa mem size: %s\n", optarg);
721 exit(1);
723 node_mem[nodenr] = sval;
725 if (get_param_value(option, 128, "cpus", optarg) == 0) {
726 node_cpumask[nodenr] = 0;
727 } else {
728 value = strtoull(option, &endptr, 10);
729 if (value >= 64) {
730 value = 63;
731 fprintf(stderr, "only 64 CPUs in NUMA mode supported.\n");
732 } else {
733 if (*endptr == '-') {
734 endvalue = strtoull(endptr+1, &endptr, 10);
735 if (endvalue >= 63) {
736 endvalue = 62;
737 fprintf(stderr,
738 "only 63 CPUs in NUMA mode supported.\n");
740 value = (2ULL << endvalue) - (1ULL << value);
741 } else {
742 value = 1ULL << value;
745 node_cpumask[nodenr] = value;
747 nb_numa_nodes++;
749 return;
752 static void smp_parse(const char *optarg)
754 int smp, sockets = 0, threads = 0, cores = 0;
755 char *endptr;
756 char option[128];
758 smp = strtoul(optarg, &endptr, 10);
759 if (endptr != optarg) {
760 if (*endptr == ',') {
761 endptr++;
764 if (get_param_value(option, 128, "sockets", endptr) != 0)
765 sockets = strtoull(option, NULL, 10);
766 if (get_param_value(option, 128, "cores", endptr) != 0)
767 cores = strtoull(option, NULL, 10);
768 if (get_param_value(option, 128, "threads", endptr) != 0)
769 threads = strtoull(option, NULL, 10);
770 if (get_param_value(option, 128, "maxcpus", endptr) != 0)
771 max_cpus = strtoull(option, NULL, 10);
773 /* compute missing values, prefer sockets over cores over threads */
774 if (smp == 0 || sockets == 0) {
775 sockets = sockets > 0 ? sockets : 1;
776 cores = cores > 0 ? cores : 1;
777 threads = threads > 0 ? threads : 1;
778 if (smp == 0) {
779 smp = cores * threads * sockets;
781 } else {
782 if (cores == 0) {
783 threads = threads > 0 ? threads : 1;
784 cores = smp / (sockets * threads);
785 } else {
786 threads = smp / (cores * sockets);
789 smp_cpus = smp;
790 smp_cores = cores > 0 ? cores : 1;
791 smp_threads = threads > 0 ? threads : 1;
792 if (max_cpus == 0)
793 max_cpus = smp_cpus;
796 /***********************************************************/
797 /* USB devices */
799 static int usb_device_add(const char *devname)
801 const char *p;
802 USBDevice *dev = NULL;
804 if (!usb_enabled)
805 return -1;
807 /* drivers with .usbdevice_name entry in USBDeviceInfo */
808 dev = usbdevice_create(devname);
809 if (dev)
810 goto done;
812 /* the other ones */
813 if (strstart(devname, "host:", &p)) {
814 dev = usb_host_device_open(p);
815 } else if (!strcmp(devname, "bt") || strstart(devname, "bt:", &p)) {
816 dev = usb_bt_init(devname[2] ? hci_init(p) :
817 bt_new_hci(qemu_find_bt_vlan(0)));
818 } else {
819 return -1;
821 if (!dev)
822 return -1;
824 done:
825 return 0;
828 static int usb_device_del(const char *devname)
830 int bus_num, addr;
831 const char *p;
833 if (strstart(devname, "host:", &p))
834 return usb_host_device_close(p);
836 if (!usb_enabled)
837 return -1;
839 p = strchr(devname, '.');
840 if (!p)
841 return -1;
842 bus_num = strtoul(devname, NULL, 0);
843 addr = strtoul(p + 1, NULL, 0);
845 return usb_device_delete_addr(bus_num, addr);
848 static int usb_parse(const char *cmdline)
850 int r;
851 r = usb_device_add(cmdline);
852 if (r < 0) {
853 fprintf(stderr, "qemu: could not add USB device '%s'\n", cmdline);
855 return r;
858 void do_usb_add(Monitor *mon, const QDict *qdict)
860 const char *devname = qdict_get_str(qdict, "devname");
861 if (usb_device_add(devname) < 0) {
862 error_report("could not add USB device '%s'", devname);
866 void do_usb_del(Monitor *mon, const QDict *qdict)
868 const char *devname = qdict_get_str(qdict, "devname");
869 if (usb_device_del(devname) < 0) {
870 error_report("could not delete USB device '%s'", devname);
874 /***********************************************************/
875 /* PCMCIA/Cardbus */
877 static struct pcmcia_socket_entry_s {
878 PCMCIASocket *socket;
879 struct pcmcia_socket_entry_s *next;
880 } *pcmcia_sockets = 0;
882 void pcmcia_socket_register(PCMCIASocket *socket)
884 struct pcmcia_socket_entry_s *entry;
886 entry = qemu_malloc(sizeof(struct pcmcia_socket_entry_s));
887 entry->socket = socket;
888 entry->next = pcmcia_sockets;
889 pcmcia_sockets = entry;
892 void pcmcia_socket_unregister(PCMCIASocket *socket)
894 struct pcmcia_socket_entry_s *entry, **ptr;
896 ptr = &pcmcia_sockets;
897 for (entry = *ptr; entry; ptr = &entry->next, entry = *ptr)
898 if (entry->socket == socket) {
899 *ptr = entry->next;
900 qemu_free(entry);
904 void pcmcia_info(Monitor *mon)
906 struct pcmcia_socket_entry_s *iter;
908 if (!pcmcia_sockets)
909 monitor_printf(mon, "No PCMCIA sockets\n");
911 for (iter = pcmcia_sockets; iter; iter = iter->next)
912 monitor_printf(mon, "%s: %s\n", iter->socket->slot_string,
913 iter->socket->attached ? iter->socket->card_string :
914 "Empty");
917 /***********************************************************/
918 /* I/O handling */
920 typedef struct IOHandlerRecord {
921 int fd;
922 IOCanReadHandler *fd_read_poll;
923 IOHandler *fd_read;
924 IOHandler *fd_write;
925 int deleted;
926 void *opaque;
927 /* temporary data */
928 struct pollfd *ufd;
929 QLIST_ENTRY(IOHandlerRecord) next;
930 } IOHandlerRecord;
932 static QLIST_HEAD(, IOHandlerRecord) io_handlers =
933 QLIST_HEAD_INITIALIZER(io_handlers);
936 /* XXX: fd_read_poll should be suppressed, but an API change is
937 necessary in the character devices to suppress fd_can_read(). */
938 int qemu_set_fd_handler2(int fd,
939 IOCanReadHandler *fd_read_poll,
940 IOHandler *fd_read,
941 IOHandler *fd_write,
942 void *opaque)
944 IOHandlerRecord *ioh;
946 if (!fd_read && !fd_write) {
947 QLIST_FOREACH(ioh, &io_handlers, next) {
948 if (ioh->fd == fd) {
949 ioh->deleted = 1;
950 break;
953 } else {
954 QLIST_FOREACH(ioh, &io_handlers, next) {
955 if (ioh->fd == fd)
956 goto found;
958 ioh = qemu_mallocz(sizeof(IOHandlerRecord));
959 QLIST_INSERT_HEAD(&io_handlers, ioh, next);
960 found:
961 ioh->fd = fd;
962 ioh->fd_read_poll = fd_read_poll;
963 ioh->fd_read = fd_read;
964 ioh->fd_write = fd_write;
965 ioh->opaque = opaque;
966 ioh->deleted = 0;
968 qemu_notify_event();
969 return 0;
972 int qemu_set_fd_handler(int fd,
973 IOHandler *fd_read,
974 IOHandler *fd_write,
975 void *opaque)
977 return qemu_set_fd_handler2(fd, NULL, fd_read, fd_write, opaque);
980 /***********************************************************/
981 /* machine registration */
983 static QEMUMachine *first_machine = NULL;
984 QEMUMachine *current_machine = NULL;
986 int qemu_register_machine(QEMUMachine *m)
988 QEMUMachine **pm;
989 pm = &first_machine;
990 while (*pm != NULL)
991 pm = &(*pm)->next;
992 m->next = NULL;
993 *pm = m;
994 return 0;
997 static QEMUMachine *find_machine(const char *name)
999 QEMUMachine *m;
1001 for(m = first_machine; m != NULL; m = m->next) {
1002 if (!strcmp(m->name, name))
1003 return m;
1004 if (m->alias && !strcmp(m->alias, name))
1005 return m;
1007 return NULL;
1010 static QEMUMachine *find_default_machine(void)
1012 QEMUMachine *m;
1014 for(m = first_machine; m != NULL; m = m->next) {
1015 if (m->is_default) {
1016 return m;
1019 return NULL;
1022 /***********************************************************/
1023 /* main execution loop */
1025 static void gui_update(void *opaque)
1027 uint64_t interval = GUI_REFRESH_INTERVAL;
1028 DisplayState *ds = opaque;
1029 DisplayChangeListener *dcl = ds->listeners;
1031 qemu_flush_coalesced_mmio_buffer();
1032 dpy_refresh(ds);
1034 while (dcl != NULL) {
1035 if (dcl->gui_timer_interval &&
1036 dcl->gui_timer_interval < interval)
1037 interval = dcl->gui_timer_interval;
1038 dcl = dcl->next;
1040 qemu_mod_timer(ds->gui_timer, interval + qemu_get_clock(rt_clock));
1043 static void nographic_update(void *opaque)
1045 uint64_t interval = GUI_REFRESH_INTERVAL;
1047 qemu_flush_coalesced_mmio_buffer();
1048 qemu_mod_timer(nographic_timer, interval + qemu_get_clock(rt_clock));
1051 struct vm_change_state_entry {
1052 VMChangeStateHandler *cb;
1053 void *opaque;
1054 QLIST_ENTRY (vm_change_state_entry) entries;
1057 static QLIST_HEAD(vm_change_state_head, vm_change_state_entry) vm_change_state_head;
1059 VMChangeStateEntry *qemu_add_vm_change_state_handler(VMChangeStateHandler *cb,
1060 void *opaque)
1062 VMChangeStateEntry *e;
1064 e = qemu_mallocz(sizeof (*e));
1066 e->cb = cb;
1067 e->opaque = opaque;
1068 QLIST_INSERT_HEAD(&vm_change_state_head, e, entries);
1069 return e;
1072 void qemu_del_vm_change_state_handler(VMChangeStateEntry *e)
1074 QLIST_REMOVE (e, entries);
1075 qemu_free (e);
1078 void vm_state_notify(int running, int reason)
1080 VMChangeStateEntry *e;
1082 for (e = vm_change_state_head.lh_first; e; e = e->entries.le_next) {
1083 e->cb(e->opaque, running, reason);
1087 void vm_start(void)
1089 if (!vm_running) {
1090 cpu_enable_ticks();
1091 vm_running = 1;
1092 vm_state_notify(1, 0);
1093 resume_all_vcpus();
1094 monitor_protocol_event(QEVENT_RESUME, NULL);
1098 /* reset/shutdown handler */
1100 typedef struct QEMUResetEntry {
1101 QTAILQ_ENTRY(QEMUResetEntry) entry;
1102 QEMUResetHandler *func;
1103 void *opaque;
1104 } QEMUResetEntry;
1106 static QTAILQ_HEAD(reset_handlers, QEMUResetEntry) reset_handlers =
1107 QTAILQ_HEAD_INITIALIZER(reset_handlers);
1108 static int reset_requested;
1109 static int shutdown_requested;
1110 static int powerdown_requested;
1111 int debug_requested;
1112 int vmstop_requested;
1114 int qemu_no_shutdown(void)
1116 int r = no_shutdown;
1117 no_shutdown = 0;
1118 return r;
1121 int qemu_shutdown_requested(void)
1123 int r = shutdown_requested;
1124 shutdown_requested = 0;
1125 return r;
1128 int qemu_reset_requested(void)
1130 int r = reset_requested;
1131 reset_requested = 0;
1132 return r;
1135 int qemu_powerdown_requested(void)
1137 int r = powerdown_requested;
1138 powerdown_requested = 0;
1139 return r;
1142 static int qemu_debug_requested(void)
1144 int r = debug_requested;
1145 debug_requested = 0;
1146 return r;
1149 static int qemu_vmstop_requested(void)
1151 int r = vmstop_requested;
1152 vmstop_requested = 0;
1153 return r;
1156 void qemu_register_reset(QEMUResetHandler *func, void *opaque)
1158 QEMUResetEntry *re = qemu_mallocz(sizeof(QEMUResetEntry));
1160 re->func = func;
1161 re->opaque = opaque;
1162 QTAILQ_INSERT_TAIL(&reset_handlers, re, entry);
1165 void qemu_unregister_reset(QEMUResetHandler *func, void *opaque)
1167 QEMUResetEntry *re;
1169 QTAILQ_FOREACH(re, &reset_handlers, entry) {
1170 if (re->func == func && re->opaque == opaque) {
1171 QTAILQ_REMOVE(&reset_handlers, re, entry);
1172 qemu_free(re);
1173 return;
1178 void qemu_system_reset(void)
1180 QEMUResetEntry *re, *nre;
1182 /* reset all devices */
1183 QTAILQ_FOREACH_SAFE(re, &reset_handlers, entry, nre) {
1184 re->func(re->opaque);
1186 monitor_protocol_event(QEVENT_RESET, NULL);
1187 cpu_synchronize_all_post_reset();
1190 void qemu_system_reset_request(void)
1192 if (no_reboot) {
1193 shutdown_requested = 1;
1194 } else {
1195 reset_requested = 1;
1197 if (cpu_single_env) {
1198 cpu_single_env->stopped = 1;
1199 cpu_exit(cpu_single_env);
1201 qemu_notify_event();
1204 void qemu_system_shutdown_request(void)
1206 shutdown_requested = 1;
1207 qemu_notify_event();
1210 void qemu_system_powerdown_request(void)
1212 powerdown_requested = 1;
1213 qemu_notify_event();
1216 void main_loop_wait(int nonblocking)
1218 IOHandlerRecord *ioh;
1219 fd_set rfds, wfds, xfds;
1220 int ret, nfds;
1221 struct timeval tv;
1222 int timeout;
1224 if (nonblocking)
1225 timeout = 0;
1226 else {
1227 timeout = qemu_calculate_timeout();
1228 qemu_bh_update_timeout(&timeout);
1231 os_host_main_loop_wait(&timeout);
1233 /* poll any events */
1234 /* XXX: separate device handlers from system ones */
1235 nfds = -1;
1236 FD_ZERO(&rfds);
1237 FD_ZERO(&wfds);
1238 FD_ZERO(&xfds);
1239 QLIST_FOREACH(ioh, &io_handlers, next) {
1240 if (ioh->deleted)
1241 continue;
1242 if (ioh->fd_read &&
1243 (!ioh->fd_read_poll ||
1244 ioh->fd_read_poll(ioh->opaque) != 0)) {
1245 FD_SET(ioh->fd, &rfds);
1246 if (ioh->fd > nfds)
1247 nfds = ioh->fd;
1249 if (ioh->fd_write) {
1250 FD_SET(ioh->fd, &wfds);
1251 if (ioh->fd > nfds)
1252 nfds = ioh->fd;
1256 tv.tv_sec = timeout / 1000;
1257 tv.tv_usec = (timeout % 1000) * 1000;
1259 slirp_select_fill(&nfds, &rfds, &wfds, &xfds);
1261 qemu_mutex_unlock_iothread();
1262 ret = select(nfds + 1, &rfds, &wfds, &xfds, &tv);
1263 qemu_mutex_lock_iothread();
1264 if (ret > 0) {
1265 IOHandlerRecord *pioh;
1267 QLIST_FOREACH_SAFE(ioh, &io_handlers, next, pioh) {
1268 if (ioh->deleted) {
1269 QLIST_REMOVE(ioh, next);
1270 qemu_free(ioh);
1271 continue;
1273 if (ioh->fd_read && FD_ISSET(ioh->fd, &rfds)) {
1274 ioh->fd_read(ioh->opaque);
1275 if (!(ioh->fd_read_poll && ioh->fd_read_poll(ioh->opaque)))
1276 FD_CLR(ioh->fd, &rfds);
1278 if (ioh->fd_write && FD_ISSET(ioh->fd, &wfds)) {
1279 ioh->fd_write(ioh->opaque);
1284 slirp_select_poll(&rfds, &wfds, &xfds, (ret < 0));
1286 qemu_run_all_timers();
1288 /* Check bottom-halves last in case any of the earlier events triggered
1289 them. */
1290 qemu_bh_poll();
1294 static int vm_can_run(void)
1296 if (powerdown_requested)
1297 return 0;
1298 if (reset_requested)
1299 return 0;
1300 if (shutdown_requested)
1301 return 0;
1302 if (debug_requested)
1303 return 0;
1304 return 1;
1307 qemu_irq qemu_system_powerdown;
1309 static void main_loop(void)
1311 int r;
1313 if (kvm_enabled()) {
1314 kvm_main_loop();
1315 cpu_disable_ticks();
1316 return;
1319 qemu_main_loop_start();
1321 for (;;) {
1322 do {
1323 bool nonblocking = false;
1324 #ifdef CONFIG_PROFILER
1325 int64_t ti;
1326 #endif
1327 #ifndef CONFIG_IOTHREAD
1328 nonblocking = cpu_exec_all();
1329 #endif
1330 #ifdef CONFIG_PROFILER
1331 ti = profile_getclock();
1332 #endif
1333 main_loop_wait(nonblocking);
1334 #ifdef CONFIG_PROFILER
1335 dev_time += profile_getclock() - ti;
1336 #endif
1337 } while (vm_can_run());
1339 if ((r = qemu_debug_requested())) {
1340 vm_stop(r);
1342 if (qemu_shutdown_requested()) {
1343 monitor_protocol_event(QEVENT_SHUTDOWN, NULL);
1344 if (no_shutdown) {
1345 vm_stop(0);
1346 no_shutdown = 0;
1347 } else
1348 break;
1350 if (qemu_reset_requested()) {
1351 pause_all_vcpus();
1352 qemu_system_reset();
1353 resume_all_vcpus();
1355 if (qemu_powerdown_requested()) {
1356 monitor_protocol_event(QEVENT_POWERDOWN, NULL);
1357 qemu_irq_raise(qemu_system_powerdown);
1359 if ((r = qemu_vmstop_requested())) {
1360 vm_stop(r);
1363 bdrv_close_all();
1364 pause_all_vcpus();
1367 static void version(void)
1369 printf("QEMU emulator version " QEMU_VERSION QEMU_PKGVERSION ", Copyright (c) 2003-2008 Fabrice Bellard\n");
1372 static void help(int exitcode)
1374 const char *options_help =
1375 #define DEF(option, opt_arg, opt_enum, opt_help, arch_mask) \
1376 opt_help
1377 #define DEFHEADING(text) stringify(text) "\n"
1378 #include "qemu-options.def"
1379 #undef DEF
1380 #undef DEFHEADING
1381 #undef GEN_DOCS
1383 version();
1384 printf("usage: %s [options] [disk_image]\n"
1385 "\n"
1386 "'disk_image' is a raw hard disk image for IDE hard disk 0\n"
1387 "\n"
1388 "%s\n"
1389 "During emulation, the following keys are useful:\n"
1390 "ctrl-alt-f toggle full screen\n"
1391 "ctrl-alt-n switch to virtual console 'n'\n"
1392 "ctrl-alt toggle mouse and keyboard grab\n"
1393 "\n"
1394 "When using -nographic, press 'ctrl-a h' to get some help.\n",
1395 "qemu",
1396 options_help);
1397 exit(exitcode);
1400 #define HAS_ARG 0x0001
1402 typedef struct QEMUOption {
1403 const char *name;
1404 int flags;
1405 int index;
1406 uint32_t arch_mask;
1407 } QEMUOption;
1409 static const QEMUOption qemu_options[] = {
1410 { "h", 0, QEMU_OPTION_h, QEMU_ARCH_ALL },
1411 #define DEF(option, opt_arg, opt_enum, opt_help, arch_mask) \
1412 { option, opt_arg, opt_enum, arch_mask },
1413 #define DEFHEADING(text)
1414 #include "qemu-options.def"
1415 #undef DEF
1416 #undef DEFHEADING
1417 #undef GEN_DOCS
1418 { NULL },
1420 static void select_vgahw (const char *p)
1422 const char *opts;
1424 default_vga = 0;
1425 vga_interface_type = VGA_NONE;
1426 if (strstart(p, "std", &opts)) {
1427 vga_interface_type = VGA_STD;
1428 } else if (strstart(p, "cirrus", &opts)) {
1429 vga_interface_type = VGA_CIRRUS;
1430 } else if (strstart(p, "vmware", &opts)) {
1431 vga_interface_type = VGA_VMWARE;
1432 } else if (strstart(p, "xenfb", &opts)) {
1433 vga_interface_type = VGA_XENFB;
1434 } else if (!strstart(p, "none", &opts)) {
1435 invalid_vga:
1436 fprintf(stderr, "Unknown vga type: %s\n", p);
1437 exit(1);
1439 while (*opts) {
1440 const char *nextopt;
1442 if (strstart(opts, ",retrace=", &nextopt)) {
1443 opts = nextopt;
1444 if (strstart(opts, "dumb", &nextopt))
1445 vga_retrace_method = VGA_RETRACE_DUMB;
1446 else if (strstart(opts, "precise", &nextopt))
1447 vga_retrace_method = VGA_RETRACE_PRECISE;
1448 else goto invalid_vga;
1449 } else goto invalid_vga;
1450 opts = nextopt;
1454 static int balloon_parse(const char *arg)
1456 QemuOpts *opts;
1458 if (strcmp(arg, "none") == 0) {
1459 return 0;
1462 if (!strncmp(arg, "virtio", 6)) {
1463 if (arg[6] == ',') {
1464 /* have params -> parse them */
1465 opts = qemu_opts_parse(qemu_find_opts("device"), arg+7, 0);
1466 if (!opts)
1467 return -1;
1468 } else {
1469 /* create empty opts */
1470 opts = qemu_opts_create(qemu_find_opts("device"), NULL, 0);
1472 qemu_opt_set(opts, "driver", "virtio-balloon-pci");
1473 return 0;
1476 return -1;
1479 char *qemu_find_file(int type, const char *name)
1481 int len;
1482 const char *subdir;
1483 char *buf;
1485 /* If name contains path separators then try it as a straight path. */
1486 if ((strchr(name, '/') || strchr(name, '\\'))
1487 && access(name, R_OK) == 0) {
1488 return qemu_strdup(name);
1490 switch (type) {
1491 case QEMU_FILE_TYPE_BIOS:
1492 subdir = "";
1493 break;
1494 case QEMU_FILE_TYPE_KEYMAP:
1495 subdir = "keymaps/";
1496 break;
1497 default:
1498 abort();
1500 len = strlen(data_dir) + strlen(name) + strlen(subdir) + 2;
1501 buf = qemu_mallocz(len);
1502 snprintf(buf, len, "%s/%s%s", data_dir, subdir, name);
1503 if (access(buf, R_OK)) {
1504 qemu_free(buf);
1505 return NULL;
1507 return buf;
1510 static int device_help_func(QemuOpts *opts, void *opaque)
1512 return qdev_device_help(opts);
1515 static int device_init_func(QemuOpts *opts, void *opaque)
1517 DeviceState *dev;
1519 dev = qdev_device_add(opts);
1520 if (!dev)
1521 return -1;
1522 return 0;
1525 static int chardev_init_func(QemuOpts *opts, void *opaque)
1527 CharDriverState *chr;
1529 chr = qemu_chr_open_opts(opts, NULL);
1530 if (!chr)
1531 return -1;
1532 return 0;
1535 #ifdef CONFIG_VIRTFS
1536 static int fsdev_init_func(QemuOpts *opts, void *opaque)
1538 int ret;
1539 ret = qemu_fsdev_add(opts);
1541 return ret;
1543 #endif
1545 static int mon_init_func(QemuOpts *opts, void *opaque)
1547 CharDriverState *chr;
1548 const char *chardev;
1549 const char *mode;
1550 int flags;
1552 mode = qemu_opt_get(opts, "mode");
1553 if (mode == NULL) {
1554 mode = "readline";
1556 if (strcmp(mode, "readline") == 0) {
1557 flags = MONITOR_USE_READLINE;
1558 } else if (strcmp(mode, "control") == 0) {
1559 flags = MONITOR_USE_CONTROL;
1560 } else {
1561 fprintf(stderr, "unknown monitor mode \"%s\"\n", mode);
1562 exit(1);
1565 if (qemu_opt_get_bool(opts, "pretty", 0))
1566 flags |= MONITOR_USE_PRETTY;
1568 if (qemu_opt_get_bool(opts, "default", 0))
1569 flags |= MONITOR_IS_DEFAULT;
1571 chardev = qemu_opt_get(opts, "chardev");
1572 chr = qemu_chr_find(chardev);
1573 if (chr == NULL) {
1574 fprintf(stderr, "chardev \"%s\" not found\n", chardev);
1575 exit(1);
1578 monitor_init(chr, flags);
1579 return 0;
1582 static void monitor_parse(const char *optarg, const char *mode)
1584 static int monitor_device_index = 0;
1585 QemuOpts *opts;
1586 const char *p;
1587 char label[32];
1588 int def = 0;
1590 if (strstart(optarg, "chardev:", &p)) {
1591 snprintf(label, sizeof(label), "%s", p);
1592 } else {
1593 snprintf(label, sizeof(label), "compat_monitor%d",
1594 monitor_device_index);
1595 if (monitor_device_index == 0) {
1596 def = 1;
1598 opts = qemu_chr_parse_compat(label, optarg);
1599 if (!opts) {
1600 fprintf(stderr, "parse error: %s\n", optarg);
1601 exit(1);
1605 opts = qemu_opts_create(qemu_find_opts("mon"), label, 1);
1606 if (!opts) {
1607 fprintf(stderr, "duplicate chardev: %s\n", label);
1608 exit(1);
1610 qemu_opt_set(opts, "mode", mode);
1611 qemu_opt_set(opts, "chardev", label);
1612 if (def)
1613 qemu_opt_set(opts, "default", "on");
1614 monitor_device_index++;
1617 struct device_config {
1618 enum {
1619 DEV_USB, /* -usbdevice */
1620 DEV_BT, /* -bt */
1621 DEV_SERIAL, /* -serial */
1622 DEV_PARALLEL, /* -parallel */
1623 DEV_VIRTCON, /* -virtioconsole */
1624 DEV_DEBUGCON, /* -debugcon */
1625 } type;
1626 const char *cmdline;
1627 QTAILQ_ENTRY(device_config) next;
1629 QTAILQ_HEAD(, device_config) device_configs = QTAILQ_HEAD_INITIALIZER(device_configs);
1631 static void add_device_config(int type, const char *cmdline)
1633 struct device_config *conf;
1635 conf = qemu_mallocz(sizeof(*conf));
1636 conf->type = type;
1637 conf->cmdline = cmdline;
1638 QTAILQ_INSERT_TAIL(&device_configs, conf, next);
1641 static int foreach_device_config(int type, int (*func)(const char *cmdline))
1643 struct device_config *conf;
1644 int rc;
1646 QTAILQ_FOREACH(conf, &device_configs, next) {
1647 if (conf->type != type)
1648 continue;
1649 rc = func(conf->cmdline);
1650 if (0 != rc)
1651 return rc;
1653 return 0;
1656 static int serial_parse(const char *devname)
1658 static int index = 0;
1659 char label[32];
1661 if (strcmp(devname, "none") == 0)
1662 return 0;
1663 if (index == MAX_SERIAL_PORTS) {
1664 fprintf(stderr, "qemu: too many serial ports\n");
1665 exit(1);
1667 snprintf(label, sizeof(label), "serial%d", index);
1668 serial_hds[index] = qemu_chr_open(label, devname, NULL);
1669 if (!serial_hds[index]) {
1670 fprintf(stderr, "qemu: could not open serial device '%s': %s\n",
1671 devname, strerror(errno));
1672 return -1;
1674 index++;
1675 return 0;
1678 static int parallel_parse(const char *devname)
1680 static int index = 0;
1681 char label[32];
1683 if (strcmp(devname, "none") == 0)
1684 return 0;
1685 if (index == MAX_PARALLEL_PORTS) {
1686 fprintf(stderr, "qemu: too many parallel ports\n");
1687 exit(1);
1689 snprintf(label, sizeof(label), "parallel%d", index);
1690 parallel_hds[index] = qemu_chr_open(label, devname, NULL);
1691 if (!parallel_hds[index]) {
1692 fprintf(stderr, "qemu: could not open parallel device '%s': %s\n",
1693 devname, strerror(errno));
1694 return -1;
1696 index++;
1697 return 0;
1700 static int virtcon_parse(const char *devname)
1702 QemuOptsList *device = qemu_find_opts("device");
1703 static int index = 0;
1704 char label[32];
1705 QemuOpts *bus_opts, *dev_opts;
1707 if (strcmp(devname, "none") == 0)
1708 return 0;
1709 if (index == MAX_VIRTIO_CONSOLES) {
1710 fprintf(stderr, "qemu: too many virtio consoles\n");
1711 exit(1);
1714 bus_opts = qemu_opts_create(device, NULL, 0);
1715 qemu_opt_set(bus_opts, "driver", "virtio-serial");
1717 dev_opts = qemu_opts_create(device, NULL, 0);
1718 qemu_opt_set(dev_opts, "driver", "virtconsole");
1720 snprintf(label, sizeof(label), "virtcon%d", index);
1721 virtcon_hds[index] = qemu_chr_open(label, devname, NULL);
1722 if (!virtcon_hds[index]) {
1723 fprintf(stderr, "qemu: could not open virtio console '%s': %s\n",
1724 devname, strerror(errno));
1725 return -1;
1727 qemu_opt_set(dev_opts, "chardev", label);
1729 index++;
1730 return 0;
1733 static int debugcon_parse(const char *devname)
1735 QemuOpts *opts;
1737 if (!qemu_chr_open("debugcon", devname, NULL)) {
1738 exit(1);
1740 opts = qemu_opts_create(qemu_find_opts("device"), "debugcon", 1);
1741 if (!opts) {
1742 fprintf(stderr, "qemu: already have a debugcon device\n");
1743 exit(1);
1745 qemu_opt_set(opts, "driver", "isa-debugcon");
1746 qemu_opt_set(opts, "chardev", "debugcon");
1747 return 0;
1750 void qemu_add_exit_notifier(Notifier *notify)
1752 notifier_list_add(&exit_notifiers, notify);
1755 void qemu_remove_exit_notifier(Notifier *notify)
1757 notifier_list_remove(&exit_notifiers, notify);
1760 static void qemu_run_exit_notifiers(void)
1762 notifier_list_notify(&exit_notifiers);
1765 static const QEMUOption *lookup_opt(int argc, char **argv,
1766 const char **poptarg, int *poptind)
1768 const QEMUOption *popt;
1769 int optind = *poptind;
1770 char *r = argv[optind];
1771 const char *optarg;
1773 loc_set_cmdline(argv, optind, 1);
1774 optind++;
1775 /* Treat --foo the same as -foo. */
1776 if (r[1] == '-')
1777 r++;
1778 popt = qemu_options;
1779 for(;;) {
1780 if (!popt->name) {
1781 error_report("invalid option");
1782 exit(1);
1784 if (!strcmp(popt->name, r + 1))
1785 break;
1786 popt++;
1788 if (popt->flags & HAS_ARG) {
1789 if (optind >= argc) {
1790 error_report("requires an argument");
1791 exit(1);
1793 optarg = argv[optind++];
1794 loc_set_cmdline(argv, optind - 2, 2);
1795 } else {
1796 optarg = NULL;
1799 *poptarg = optarg;
1800 *poptind = optind;
1802 return popt;
1805 int main(int argc, char **argv, char **envp)
1807 const char *gdbstub_dev = NULL;
1808 int i;
1809 int snapshot, linux_boot;
1810 const char *icount_option = NULL;
1811 const char *initrd_filename;
1812 const char *kernel_filename, *kernel_cmdline;
1813 char boot_devices[33] = "cad"; /* default to HD->floppy->CD-ROM */
1814 DisplayState *ds;
1815 DisplayChangeListener *dcl;
1816 int cyls, heads, secs, translation;
1817 QemuOpts *hda_opts = NULL, *opts;
1818 QemuOptsList *olist;
1819 int optind;
1820 const char *optarg;
1821 const char *loadvm = NULL;
1822 QEMUMachine *machine;
1823 const char *cpu_model;
1824 int tb_size;
1825 const char *pid_file = NULL;
1826 const char *incoming = NULL;
1827 int show_vnc_port = 0;
1828 int defconfig = 1;
1830 #ifdef CONFIG_SIMPLE_TRACE
1831 const char *trace_file = NULL;
1832 #endif
1833 atexit(qemu_run_exit_notifiers);
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 nb_numa_nodes = 0;
1860 nb_nics = 0;
1862 tb_size = 0;
1863 autostart= 1;
1865 /* first pass of option parsing */
1866 optind = 1;
1867 while (optind < argc) {
1868 if (argv[optind][0] != '-') {
1869 /* disk image */
1870 optind++;
1871 continue;
1872 } else {
1873 const QEMUOption *popt;
1875 popt = lookup_opt(argc, argv, &optarg, &optind);
1876 switch (popt->index) {
1877 case QEMU_OPTION_nodefconfig:
1878 defconfig=0;
1879 break;
1884 if (defconfig) {
1885 int ret;
1887 ret = qemu_read_config_file(CONFIG_QEMU_CONFDIR "/qemu.conf");
1888 if (ret < 0 && ret != -ENOENT) {
1889 exit(1);
1892 ret = qemu_read_config_file(arch_config_name);
1893 if (ret < 0 && ret != -ENOENT) {
1894 exit(1);
1897 cpudef_init();
1899 /* second pass of option parsing */
1900 optind = 1;
1901 for(;;) {
1902 if (optind >= argc)
1903 break;
1904 if (argv[optind][0] != '-') {
1905 hda_opts = drive_add(argv[optind++], HD_ALIAS, 0);
1906 } else {
1907 const QEMUOption *popt;
1909 popt = lookup_opt(argc, argv, &optarg, &optind);
1910 if (!(popt->arch_mask & arch_type)) {
1911 printf("Option %s not supported for this target\n", popt->name);
1912 exit(1);
1914 switch(popt->index) {
1915 case QEMU_OPTION_M:
1916 machine = find_machine(optarg);
1917 if (!machine) {
1918 QEMUMachine *m;
1919 printf("Supported machines are:\n");
1920 for(m = first_machine; m != NULL; m = m->next) {
1921 if (m->alias)
1922 printf("%-10s %s (alias of %s)\n",
1923 m->alias, m->desc, m->name);
1924 printf("%-10s %s%s\n",
1925 m->name, m->desc,
1926 m->is_default ? " (default)" : "");
1928 exit(*optarg != '?');
1930 break;
1931 case QEMU_OPTION_cpu:
1932 /* hw initialization will check this */
1933 if (*optarg == '?') {
1934 list_cpus(stdout, &fprintf, optarg);
1935 exit(0);
1936 } else {
1937 cpu_model = optarg;
1939 break;
1940 case QEMU_OPTION_initrd:
1941 initrd_filename = optarg;
1942 break;
1943 case QEMU_OPTION_hda:
1944 if (cyls == 0)
1945 hda_opts = drive_add(optarg, HD_ALIAS, 0);
1946 else
1947 hda_opts = drive_add(optarg, HD_ALIAS
1948 ",cyls=%d,heads=%d,secs=%d%s",
1949 0, cyls, heads, secs,
1950 translation == BIOS_ATA_TRANSLATION_LBA ?
1951 ",trans=lba" :
1952 translation == BIOS_ATA_TRANSLATION_NONE ?
1953 ",trans=none" : "");
1954 break;
1955 case QEMU_OPTION_hdb:
1956 case QEMU_OPTION_hdc:
1957 case QEMU_OPTION_hdd:
1958 drive_add(optarg, HD_ALIAS, popt->index - QEMU_OPTION_hda);
1959 break;
1960 case QEMU_OPTION_drive:
1961 drive_add(NULL, "%s", optarg);
1962 break;
1963 case QEMU_OPTION_set:
1964 if (qemu_set_option(optarg) != 0)
1965 exit(1);
1966 break;
1967 case QEMU_OPTION_global:
1968 if (qemu_global_option(optarg) != 0)
1969 exit(1);
1970 break;
1971 case QEMU_OPTION_mtdblock:
1972 drive_add(optarg, MTD_ALIAS);
1973 break;
1974 case QEMU_OPTION_sd:
1975 drive_add(optarg, SD_ALIAS);
1976 break;
1977 case QEMU_OPTION_pflash:
1978 drive_add(optarg, PFLASH_ALIAS);
1979 break;
1980 case QEMU_OPTION_snapshot:
1981 snapshot = 1;
1982 break;
1983 case QEMU_OPTION_hdachs:
1985 const char *p;
1986 p = optarg;
1987 cyls = strtol(p, (char **)&p, 0);
1988 if (cyls < 1 || cyls > 16383)
1989 goto chs_fail;
1990 if (*p != ',')
1991 goto chs_fail;
1992 p++;
1993 heads = strtol(p, (char **)&p, 0);
1994 if (heads < 1 || heads > 16)
1995 goto chs_fail;
1996 if (*p != ',')
1997 goto chs_fail;
1998 p++;
1999 secs = strtol(p, (char **)&p, 0);
2000 if (secs < 1 || secs > 63)
2001 goto chs_fail;
2002 if (*p == ',') {
2003 p++;
2004 if (!strcmp(p, "none"))
2005 translation = BIOS_ATA_TRANSLATION_NONE;
2006 else if (!strcmp(p, "lba"))
2007 translation = BIOS_ATA_TRANSLATION_LBA;
2008 else if (!strcmp(p, "auto"))
2009 translation = BIOS_ATA_TRANSLATION_AUTO;
2010 else
2011 goto chs_fail;
2012 } else if (*p != '\0') {
2013 chs_fail:
2014 fprintf(stderr, "qemu: invalid physical CHS format\n");
2015 exit(1);
2017 if (hda_opts != NULL) {
2018 char num[16];
2019 snprintf(num, sizeof(num), "%d", cyls);
2020 qemu_opt_set(hda_opts, "cyls", num);
2021 snprintf(num, sizeof(num), "%d", heads);
2022 qemu_opt_set(hda_opts, "heads", num);
2023 snprintf(num, sizeof(num), "%d", secs);
2024 qemu_opt_set(hda_opts, "secs", num);
2025 if (translation == BIOS_ATA_TRANSLATION_LBA)
2026 qemu_opt_set(hda_opts, "trans", "lba");
2027 if (translation == BIOS_ATA_TRANSLATION_NONE)
2028 qemu_opt_set(hda_opts, "trans", "none");
2031 break;
2032 case QEMU_OPTION_numa:
2033 if (nb_numa_nodes >= MAX_NODES) {
2034 fprintf(stderr, "qemu: too many NUMA nodes\n");
2035 exit(1);
2037 numa_add(optarg);
2038 break;
2039 case QEMU_OPTION_nographic:
2040 display_type = DT_NOGRAPHIC;
2041 break;
2042 #ifdef CONFIG_CURSES
2043 case QEMU_OPTION_curses:
2044 display_type = DT_CURSES;
2045 break;
2046 #endif
2047 case QEMU_OPTION_portrait:
2048 graphic_rotate = 1;
2049 break;
2050 case QEMU_OPTION_kernel:
2051 kernel_filename = optarg;
2052 break;
2053 case QEMU_OPTION_append:
2054 kernel_cmdline = optarg;
2055 break;
2056 case QEMU_OPTION_cdrom:
2057 drive_add(optarg, CDROM_ALIAS);
2058 break;
2059 case QEMU_OPTION_boot:
2061 static const char * const params[] = {
2062 "order", "once", "menu", NULL
2064 char buf[sizeof(boot_devices)];
2065 char *standard_boot_devices;
2066 int legacy = 0;
2068 if (!strchr(optarg, '=')) {
2069 legacy = 1;
2070 pstrcpy(buf, sizeof(buf), optarg);
2071 } else if (check_params(buf, sizeof(buf), params, optarg) < 0) {
2072 fprintf(stderr,
2073 "qemu: unknown boot parameter '%s' in '%s'\n",
2074 buf, optarg);
2075 exit(1);
2078 if (legacy ||
2079 get_param_value(buf, sizeof(buf), "order", optarg)) {
2080 validate_bootdevices(buf);
2081 pstrcpy(boot_devices, sizeof(boot_devices), buf);
2083 if (!legacy) {
2084 if (get_param_value(buf, sizeof(buf),
2085 "once", optarg)) {
2086 validate_bootdevices(buf);
2087 standard_boot_devices = qemu_strdup(boot_devices);
2088 pstrcpy(boot_devices, sizeof(boot_devices), buf);
2089 qemu_register_reset(restore_boot_devices,
2090 standard_boot_devices);
2092 if (get_param_value(buf, sizeof(buf),
2093 "menu", optarg)) {
2094 if (!strcmp(buf, "on")) {
2095 boot_menu = 1;
2096 } else if (!strcmp(buf, "off")) {
2097 boot_menu = 0;
2098 } else {
2099 fprintf(stderr,
2100 "qemu: invalid option value '%s'\n",
2101 buf);
2102 exit(1);
2107 break;
2108 case QEMU_OPTION_fda:
2109 case QEMU_OPTION_fdb:
2110 drive_add(optarg, FD_ALIAS, popt->index - QEMU_OPTION_fda);
2111 break;
2112 case QEMU_OPTION_no_fd_bootchk:
2113 fd_bootchk = 0;
2114 break;
2115 case QEMU_OPTION_netdev:
2116 if (net_client_parse(qemu_find_opts("netdev"), optarg) == -1) {
2117 exit(1);
2119 break;
2120 case QEMU_OPTION_net:
2121 if (net_client_parse(qemu_find_opts("net"), optarg) == -1) {
2122 exit(1);
2124 break;
2125 #ifdef CONFIG_SLIRP
2126 case QEMU_OPTION_tftp:
2127 legacy_tftp_prefix = optarg;
2128 break;
2129 case QEMU_OPTION_bootp:
2130 legacy_bootp_filename = optarg;
2131 break;
2132 case QEMU_OPTION_redir:
2133 if (net_slirp_redir(optarg) < 0)
2134 exit(1);
2135 break;
2136 #endif
2137 case QEMU_OPTION_bt:
2138 add_device_config(DEV_BT, optarg);
2139 break;
2140 case QEMU_OPTION_audio_help:
2141 if (!(audio_available())) {
2142 printf("Option %s not supported for this target\n", popt->name);
2143 exit(1);
2145 AUD_help ();
2146 exit (0);
2147 break;
2148 case QEMU_OPTION_soundhw:
2149 if (!(audio_available())) {
2150 printf("Option %s not supported for this target\n", popt->name);
2151 exit(1);
2153 select_soundhw (optarg);
2154 break;
2155 case QEMU_OPTION_h:
2156 help(0);
2157 break;
2158 case QEMU_OPTION_version:
2159 version();
2160 exit(0);
2161 break;
2162 case QEMU_OPTION_m: {
2163 ssize_t value;
2165 value = strtosz(optarg, NULL);
2166 if (value < 0) {
2167 fprintf(stderr, "qemu: invalid ram size: %s\n", optarg);
2168 exit(1);
2171 /* On 32-bit hosts, QEMU is limited by virtual address space */
2172 if (value > (2047 << 20) && HOST_LONG_BITS == 32) {
2173 fprintf(stderr, "qemu: at most 2047 MB RAM can be simulated\n");
2174 exit(1);
2176 if (value != (uint64_t)(ram_addr_t)value) {
2177 fprintf(stderr, "qemu: ram size too large\n");
2178 exit(1);
2180 ram_size = value;
2181 break;
2183 case QEMU_OPTION_mempath:
2184 mem_path = optarg;
2185 break;
2186 #ifdef MAP_POPULATE
2187 case QEMU_OPTION_mem_prealloc:
2188 mem_prealloc = 1;
2189 break;
2190 #endif
2191 case QEMU_OPTION_d:
2192 set_cpu_log(optarg);
2193 break;
2194 case QEMU_OPTION_s:
2195 gdbstub_dev = "tcp::" DEFAULT_GDBSTUB_PORT;
2196 break;
2197 case QEMU_OPTION_gdb:
2198 gdbstub_dev = optarg;
2199 break;
2200 case QEMU_OPTION_L:
2201 data_dir = optarg;
2202 break;
2203 case QEMU_OPTION_bios:
2204 bios_name = optarg;
2205 break;
2206 case QEMU_OPTION_singlestep:
2207 singlestep = 1;
2208 break;
2209 case QEMU_OPTION_S:
2210 autostart = 0;
2211 break;
2212 case QEMU_OPTION_k:
2213 keyboard_layout = optarg;
2214 break;
2215 case QEMU_OPTION_localtime:
2216 rtc_utc = 0;
2217 break;
2218 case QEMU_OPTION_vga:
2219 select_vgahw (optarg);
2220 break;
2221 case QEMU_OPTION_g:
2223 const char *p;
2224 int w, h, depth;
2225 p = optarg;
2226 w = strtol(p, (char **)&p, 10);
2227 if (w <= 0) {
2228 graphic_error:
2229 fprintf(stderr, "qemu: invalid resolution or depth\n");
2230 exit(1);
2232 if (*p != 'x')
2233 goto graphic_error;
2234 p++;
2235 h = strtol(p, (char **)&p, 10);
2236 if (h <= 0)
2237 goto graphic_error;
2238 if (*p == 'x') {
2239 p++;
2240 depth = strtol(p, (char **)&p, 10);
2241 if (depth != 8 && depth != 15 && depth != 16 &&
2242 depth != 24 && depth != 32)
2243 goto graphic_error;
2244 } else if (*p == '\0') {
2245 depth = graphic_depth;
2246 } else {
2247 goto graphic_error;
2250 graphic_width = w;
2251 graphic_height = h;
2252 graphic_depth = depth;
2254 break;
2255 case QEMU_OPTION_echr:
2257 char *r;
2258 term_escape_char = strtol(optarg, &r, 0);
2259 if (r == optarg)
2260 printf("Bad argument to echr\n");
2261 break;
2263 case QEMU_OPTION_monitor:
2264 monitor_parse(optarg, "readline");
2265 default_monitor = 0;
2266 break;
2267 case QEMU_OPTION_qmp:
2268 monitor_parse(optarg, "control");
2269 default_monitor = 0;
2270 break;
2271 case QEMU_OPTION_mon:
2272 opts = qemu_opts_parse(qemu_find_opts("mon"), optarg, 1);
2273 if (!opts) {
2274 exit(1);
2276 default_monitor = 0;
2277 break;
2278 case QEMU_OPTION_chardev:
2279 opts = qemu_opts_parse(qemu_find_opts("chardev"), optarg, 1);
2280 if (!opts) {
2281 exit(1);
2283 break;
2284 case QEMU_OPTION_fsdev:
2285 olist = qemu_find_opts("fsdev");
2286 if (!olist) {
2287 fprintf(stderr, "fsdev is not supported by this qemu build.\n");
2288 exit(1);
2290 opts = qemu_opts_parse(olist, optarg, 1);
2291 if (!opts) {
2292 fprintf(stderr, "parse error: %s\n", optarg);
2293 exit(1);
2295 break;
2296 case QEMU_OPTION_virtfs: {
2297 char *arg_fsdev = NULL;
2298 char *arg_9p = NULL;
2299 int len = 0;
2301 olist = qemu_find_opts("virtfs");
2302 if (!olist) {
2303 fprintf(stderr, "virtfs is not supported by this qemu build.\n");
2304 exit(1);
2306 opts = qemu_opts_parse(olist, optarg, 1);
2307 if (!opts) {
2308 fprintf(stderr, "parse error: %s\n", optarg);
2309 exit(1);
2312 if (qemu_opt_get(opts, "fstype") == NULL ||
2313 qemu_opt_get(opts, "mount_tag") == NULL ||
2314 qemu_opt_get(opts, "path") == NULL ||
2315 qemu_opt_get(opts, "security_model") == NULL) {
2316 fprintf(stderr, "Usage: -virtfs fstype,path=/share_path/,"
2317 "security_model=[mapped|passthrough|none],"
2318 "mnt_tag=tag.\n");
2319 exit(1);
2322 len = strlen(",id=,path=,security_model=");
2323 len += strlen(qemu_opt_get(opts, "fstype"));
2324 len += strlen(qemu_opt_get(opts, "mount_tag"));
2325 len += strlen(qemu_opt_get(opts, "path"));
2326 len += strlen(qemu_opt_get(opts, "security_model"));
2327 arg_fsdev = qemu_malloc((len + 1) * sizeof(*arg_fsdev));
2329 snprintf(arg_fsdev, (len + 1) * sizeof(*arg_fsdev),
2330 "%s,id=%s,path=%s,security_model=%s",
2331 qemu_opt_get(opts, "fstype"),
2332 qemu_opt_get(opts, "mount_tag"),
2333 qemu_opt_get(opts, "path"),
2334 qemu_opt_get(opts, "security_model"));
2336 len = strlen("virtio-9p-pci,fsdev=,mount_tag=");
2337 len += 2*strlen(qemu_opt_get(opts, "mount_tag"));
2338 arg_9p = qemu_malloc((len + 1) * sizeof(*arg_9p));
2340 snprintf(arg_9p, (len + 1) * sizeof(*arg_9p),
2341 "virtio-9p-pci,fsdev=%s,mount_tag=%s",
2342 qemu_opt_get(opts, "mount_tag"),
2343 qemu_opt_get(opts, "mount_tag"));
2345 if (!qemu_opts_parse(qemu_find_opts("fsdev"), arg_fsdev, 1)) {
2346 fprintf(stderr, "parse error [fsdev]: %s\n", optarg);
2347 exit(1);
2350 if (!qemu_opts_parse(qemu_find_opts("device"), arg_9p, 1)) {
2351 fprintf(stderr, "parse error [device]: %s\n", optarg);
2352 exit(1);
2355 qemu_free(arg_fsdev);
2356 qemu_free(arg_9p);
2357 break;
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 OBSOLETE_KVM_IMPL
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 #ifdef CONFIG_NO_CPU_EMULATION
2443 fprintf(stderr, "cpu emulation not configured\n");
2444 exit(1);
2445 #endif
2446 break;
2447 #ifdef CONFIG_KVM
2448 case QEMU_OPTION_no_kvm_irqchip: {
2449 kvm_irqchip = 0;
2450 kvm_pit = 0;
2451 break;
2453 case QEMU_OPTION_no_kvm_pit: {
2454 kvm_pit = 0;
2455 break;
2457 case QEMU_OPTION_no_kvm_pit_reinjection: {
2458 kvm_pit_reinject = 0;
2459 break;
2461 case QEMU_OPTION_enable_nesting: {
2462 kvm_nested = 1;
2463 break;
2465 #endif
2466 case QEMU_OPTION_usb:
2467 usb_enabled = 1;
2468 break;
2469 case QEMU_OPTION_usbdevice:
2470 usb_enabled = 1;
2471 add_device_config(DEV_USB, optarg);
2472 break;
2473 case QEMU_OPTION_device:
2474 if (!qemu_opts_parse(qemu_find_opts("device"), optarg, 1)) {
2475 exit(1);
2477 break;
2478 case QEMU_OPTION_smp:
2479 smp_parse(optarg);
2480 if (smp_cpus < 1) {
2481 fprintf(stderr, "Invalid number of CPUs\n");
2482 exit(1);
2484 if (max_cpus < smp_cpus) {
2485 fprintf(stderr, "maxcpus must be equal to or greater than "
2486 "smp\n");
2487 exit(1);
2489 if (max_cpus > 255) {
2490 fprintf(stderr, "Unsupported number of maxcpus\n");
2491 exit(1);
2493 break;
2494 case QEMU_OPTION_vnc:
2495 display_remote++;
2496 vnc_display = optarg;
2497 break;
2498 case QEMU_OPTION_no_acpi:
2499 acpi_enabled = 0;
2500 break;
2501 case QEMU_OPTION_no_hpet:
2502 no_hpet = 1;
2503 break;
2504 case QEMU_OPTION_balloon:
2505 if (balloon_parse(optarg) < 0) {
2506 fprintf(stderr, "Unknown -balloon argument %s\n", optarg);
2507 exit(1);
2509 break;
2510 case QEMU_OPTION_no_reboot:
2511 no_reboot = 1;
2512 break;
2513 case QEMU_OPTION_no_shutdown:
2514 no_shutdown = 1;
2515 break;
2516 case QEMU_OPTION_show_cursor:
2517 cursor_hide = 0;
2518 break;
2519 case QEMU_OPTION_uuid:
2520 if(qemu_uuid_parse(optarg, qemu_uuid) < 0) {
2521 fprintf(stderr, "Fail to parse UUID string."
2522 " Wrong format.\n");
2523 exit(1);
2525 break;
2526 case QEMU_OPTION_option_rom:
2527 if (nb_option_roms >= MAX_OPTION_ROMS) {
2528 fprintf(stderr, "Too many option ROMs\n");
2529 exit(1);
2531 option_rom[nb_option_roms] = optarg;
2532 nb_option_roms++;
2533 break;
2534 case QEMU_OPTION_semihosting:
2535 semihosting_enabled = 1;
2536 break;
2537 case QEMU_OPTION_tdf:
2538 time_drift_fix = 1;
2539 break;
2540 case QEMU_OPTION_kvm_shadow_memory:
2541 kvm_shadow_memory = (int64_t)atoi(optarg) * 1024 * 1024 / 4096;
2542 break;
2543 case QEMU_OPTION_name:
2544 qemu_name = qemu_strdup(optarg);
2546 char *p = strchr(qemu_name, ',');
2547 if (p != NULL) {
2548 *p++ = 0;
2549 if (strncmp(p, "process=", 8)) {
2550 fprintf(stderr, "Unknown subargument %s to -name", p);
2551 exit(1);
2553 p += 8;
2554 os_set_proc_name(p);
2557 break;
2558 case QEMU_OPTION_prom_env:
2559 if (nb_prom_envs >= MAX_PROM_ENVS) {
2560 fprintf(stderr, "Too many prom variables\n");
2561 exit(1);
2563 prom_envs[nb_prom_envs] = optarg;
2564 nb_prom_envs++;
2565 break;
2566 case QEMU_OPTION_old_param:
2567 old_param = 1;
2568 break;
2569 case QEMU_OPTION_clock:
2570 configure_alarms(optarg);
2571 break;
2572 case QEMU_OPTION_startdate:
2573 configure_rtc_date_offset(optarg, 1);
2574 break;
2575 case QEMU_OPTION_rtc:
2576 opts = qemu_opts_parse(qemu_find_opts("rtc"), optarg, 0);
2577 if (!opts) {
2578 exit(1);
2580 configure_rtc(opts);
2581 break;
2582 case QEMU_OPTION_tb_size:
2583 tb_size = strtol(optarg, NULL, 0);
2584 if (tb_size < 0)
2585 tb_size = 0;
2586 break;
2587 case QEMU_OPTION_icount:
2588 icount_option = optarg;
2589 break;
2590 case QEMU_OPTION_incoming:
2591 incoming = optarg;
2592 incoming_expected = true;
2593 break;
2594 case QEMU_OPTION_nodefaults:
2595 default_serial = 0;
2596 default_parallel = 0;
2597 default_virtcon = 0;
2598 default_monitor = 0;
2599 default_vga = 0;
2600 default_net = 0;
2601 default_floppy = 0;
2602 default_cdrom = 0;
2603 default_sdcard = 0;
2604 break;
2605 #ifndef _WIN32
2606 case QEMU_OPTION_nvram:
2607 nvram = optarg;
2608 break;
2609 #endif
2610 case QEMU_OPTION_xen_domid:
2611 if (!(xen_available())) {
2612 printf("Option %s not supported for this target\n", popt->name);
2613 exit(1);
2615 xen_domid = atoi(optarg);
2616 break;
2617 case QEMU_OPTION_xen_create:
2618 if (!(xen_available())) {
2619 printf("Option %s not supported for this target\n", popt->name);
2620 exit(1);
2622 xen_mode = XEN_CREATE;
2623 break;
2624 case QEMU_OPTION_xen_attach:
2625 if (!(xen_available())) {
2626 printf("Option %s not supported for this target\n", popt->name);
2627 exit(1);
2629 xen_mode = XEN_ATTACH;
2630 break;
2631 #ifdef CONFIG_SIMPLE_TRACE
2632 case QEMU_OPTION_trace:
2633 opts = qemu_opts_parse(qemu_find_opts("trace"), optarg, 0);
2634 if (opts) {
2635 trace_file = qemu_opt_get(opts, "file");
2637 break;
2638 #endif
2639 case QEMU_OPTION_readconfig:
2641 int ret = qemu_read_config_file(optarg);
2642 if (ret < 0) {
2643 fprintf(stderr, "read config %s: %s\n", optarg,
2644 strerror(-ret));
2645 exit(1);
2647 break;
2649 case QEMU_OPTION_spice:
2650 olist = qemu_find_opts("spice");
2651 if (!olist) {
2652 fprintf(stderr, "spice is not supported by this qemu build.\n");
2653 exit(1);
2655 opts = qemu_opts_parse(olist, optarg, 0);
2656 if (!opts) {
2657 fprintf(stderr, "parse error: %s\n", optarg);
2658 exit(1);
2660 break;
2661 case QEMU_OPTION_writeconfig:
2663 FILE *fp;
2664 if (strcmp(optarg, "-") == 0) {
2665 fp = stdout;
2666 } else {
2667 fp = fopen(optarg, "w");
2668 if (fp == NULL) {
2669 fprintf(stderr, "open %s: %s\n", optarg, strerror(errno));
2670 exit(1);
2673 qemu_config_write(fp);
2674 fclose(fp);
2675 break;
2677 default:
2678 os_parse_cmd_args(popt->index, optarg);
2682 loc_set_none();
2684 /* If no data_dir is specified then try to find it relative to the
2685 executable path. */
2686 if (!data_dir) {
2687 data_dir = os_find_datadir(argv[0]);
2689 /* If all else fails use the install patch specified when building. */
2690 if (!data_dir) {
2691 data_dir = CONFIG_QEMU_DATADIR;
2694 #ifdef CONFIG_SIMPLE_TRACE
2696 * Set the trace file name, if specified.
2698 st_set_trace_file(trace_file);
2699 #endif
2701 * Default to max_cpus = smp_cpus, in case the user doesn't
2702 * specify a max_cpus value.
2704 if (!max_cpus)
2705 max_cpus = smp_cpus;
2707 machine->max_cpus = machine->max_cpus ?: 1; /* Default to UP */
2708 if (smp_cpus > machine->max_cpus) {
2709 fprintf(stderr, "Number of SMP cpus requested (%d), exceeds max cpus "
2710 "supported by machine `%s' (%d)\n", smp_cpus, machine->name,
2711 machine->max_cpus);
2712 exit(1);
2715 qemu_opts_foreach(qemu_find_opts("device"), default_driver_check, NULL, 0);
2716 qemu_opts_foreach(qemu_find_opts("global"), default_driver_check, NULL, 0);
2718 if (machine->no_serial) {
2719 default_serial = 0;
2721 if (machine->no_parallel) {
2722 default_parallel = 0;
2724 if (!machine->use_virtcon) {
2725 default_virtcon = 0;
2727 if (machine->no_vga) {
2728 default_vga = 0;
2730 if (machine->no_floppy) {
2731 default_floppy = 0;
2733 if (machine->no_cdrom) {
2734 default_cdrom = 0;
2736 if (machine->no_sdcard) {
2737 default_sdcard = 0;
2740 if (display_type == DT_NOGRAPHIC) {
2741 if (default_parallel)
2742 add_device_config(DEV_PARALLEL, "null");
2743 if (default_serial && default_monitor) {
2744 add_device_config(DEV_SERIAL, "mon:stdio");
2745 } else if (default_virtcon && default_monitor) {
2746 add_device_config(DEV_VIRTCON, "mon:stdio");
2747 } else {
2748 if (default_serial)
2749 add_device_config(DEV_SERIAL, "stdio");
2750 if (default_virtcon)
2751 add_device_config(DEV_VIRTCON, "stdio");
2752 if (default_monitor)
2753 monitor_parse("stdio", "readline");
2755 } else {
2756 if (default_serial)
2757 add_device_config(DEV_SERIAL, "vc:80Cx24C");
2758 if (default_parallel)
2759 add_device_config(DEV_PARALLEL, "vc:80Cx24C");
2760 if (default_monitor)
2761 monitor_parse("vc:80Cx24C", "readline");
2762 if (default_virtcon)
2763 add_device_config(DEV_VIRTCON, "vc:80Cx24C");
2765 if (default_vga)
2766 vga_interface_type = VGA_CIRRUS;
2768 socket_init();
2770 if (qemu_opts_foreach(qemu_find_opts("chardev"), chardev_init_func, NULL, 1) != 0)
2771 exit(1);
2772 #ifdef CONFIG_VIRTFS
2773 if (qemu_opts_foreach(qemu_find_opts("fsdev"), fsdev_init_func, NULL, 1) != 0) {
2774 exit(1);
2776 #endif
2778 os_daemonize();
2780 if (pid_file && qemu_create_pidfile(pid_file) != 0) {
2781 os_pidfile_error();
2782 exit(1);
2785 if (kvm_allowed) {
2786 int ret = kvm_init(smp_cpus);
2787 if (ret < 0) {
2788 #if defined(OBSOLETE_KVM_IMPL) || defined(CONFIG_NO_CPU_EMULATION)
2789 if (!kvm_available()) {
2790 printf("KVM not supported for this target\n");
2791 } else {
2792 fprintf(stderr, "failed to initialize KVM: %s\n", strerror(-ret));
2794 exit(1);
2795 #endif
2796 #ifdef CONFIG_KVM
2797 fprintf(stderr, "Could not initialize KVM, will disable KVM support\n");
2798 kvm_allowed = 0;
2799 #endif
2803 if (qemu_init_main_loop()) {
2804 fprintf(stderr, "qemu_init_main_loop failed\n");
2805 exit(1);
2807 linux_boot = (kernel_filename != NULL);
2809 if (!linux_boot && *kernel_cmdline != '\0') {
2810 fprintf(stderr, "-append only allowed with -kernel option\n");
2811 exit(1);
2814 if (!linux_boot && initrd_filename != NULL) {
2815 fprintf(stderr, "-initrd only allowed with -kernel option\n");
2816 exit(1);
2819 os_set_line_buffering();
2821 if (init_timer_alarm() < 0) {
2822 fprintf(stderr, "could not initialize alarm timer\n");
2823 exit(1);
2825 configure_icount(icount_option);
2827 if (net_init_clients() < 0) {
2828 exit(1);
2831 /* init the bluetooth world */
2832 if (foreach_device_config(DEV_BT, bt_parse))
2833 exit(1);
2835 /* init the memory */
2836 if (ram_size == 0)
2837 ram_size = DEFAULT_RAM_SIZE * 1024 * 1024;
2839 /* init the dynamic translator */
2840 cpu_exec_init_all(tb_size * 1024 * 1024);
2842 bdrv_init_with_whitelist();
2844 blk_mig_init();
2846 if (default_cdrom) {
2847 /* we always create the cdrom drive, even if no disk is there */
2848 drive_add(NULL, CDROM_ALIAS);
2851 if (default_floppy) {
2852 /* we always create at least one floppy */
2853 drive_add(NULL, FD_ALIAS, 0);
2856 if (default_sdcard) {
2857 /* we always create one sd slot, even if no card is in it */
2858 drive_add(NULL, SD_ALIAS);
2861 /* open the virtual block devices */
2862 if (snapshot)
2863 qemu_opts_foreach(qemu_find_opts("drive"), drive_enable_snapshot, NULL, 0);
2864 if (qemu_opts_foreach(qemu_find_opts("drive"), drive_init_func, &machine->use_scsi, 1) != 0)
2865 exit(1);
2867 register_savevm_live(NULL, "ram", 0, 4, NULL, ram_save_live, NULL,
2868 ram_load, NULL);
2870 if (nb_numa_nodes > 0) {
2871 int i;
2873 if (nb_numa_nodes > smp_cpus) {
2874 nb_numa_nodes = smp_cpus;
2877 /* If no memory size if given for any node, assume the default case
2878 * and distribute the available memory equally across all nodes
2880 for (i = 0; i < nb_numa_nodes; i++) {
2881 if (node_mem[i] != 0)
2882 break;
2884 if (i == nb_numa_nodes) {
2885 uint64_t usedmem = 0;
2887 /* On Linux, the each node's border has to be 8MB aligned,
2888 * the final node gets the rest.
2890 for (i = 0; i < nb_numa_nodes - 1; i++) {
2891 node_mem[i] = (ram_size / nb_numa_nodes) & ~((1 << 23UL) - 1);
2892 usedmem += node_mem[i];
2894 node_mem[i] = ram_size - usedmem;
2897 for (i = 0; i < nb_numa_nodes; i++) {
2898 if (node_cpumask[i] != 0)
2899 break;
2901 /* assigning the VCPUs round-robin is easier to implement, guest OSes
2902 * must cope with this anyway, because there are BIOSes out there in
2903 * real machines which also use this scheme.
2905 if (i == nb_numa_nodes) {
2906 for (i = 0; i < smp_cpus; i++) {
2907 node_cpumask[i % nb_numa_nodes] |= 1 << i;
2912 if (qemu_opts_foreach(qemu_find_opts("mon"), mon_init_func, NULL, 1) != 0) {
2913 exit(1);
2916 if (foreach_device_config(DEV_SERIAL, serial_parse) < 0)
2917 exit(1);
2918 if (foreach_device_config(DEV_PARALLEL, parallel_parse) < 0)
2919 exit(1);
2920 if (foreach_device_config(DEV_VIRTCON, virtcon_parse) < 0)
2921 exit(1);
2922 if (foreach_device_config(DEV_DEBUGCON, debugcon_parse) < 0)
2923 exit(1);
2925 module_call_init(MODULE_INIT_DEVICE);
2927 if (qemu_opts_foreach(qemu_find_opts("device"), device_help_func, NULL, 0) != 0)
2928 exit(0);
2930 if (watchdog) {
2931 i = select_watchdog(watchdog);
2932 if (i > 0)
2933 exit (i == 1 ? 1 : 0);
2936 if (machine->compat_props) {
2937 qdev_prop_register_global_list(machine->compat_props);
2939 qemu_add_globals();
2941 machine->init(ram_size, boot_devices,
2942 kernel_filename, kernel_cmdline, initrd_filename, cpu_model);
2944 cpu_synchronize_all_post_init();
2946 /* must be after terminal init, SDL library changes signal handlers */
2947 os_setup_signal_handling();
2949 set_numa_modes();
2951 current_machine = machine;
2953 /* init USB devices */
2954 if (usb_enabled) {
2955 if (foreach_device_config(DEV_USB, usb_parse) < 0)
2956 exit(1);
2959 /* init generic devices */
2960 if (qemu_opts_foreach(qemu_find_opts("device"), device_init_func, NULL, 1) != 0)
2961 exit(1);
2963 net_check_clients();
2965 /* just use the first displaystate for the moment */
2966 ds = get_displaystate();
2968 if (using_spice)
2969 display_remote++;
2970 if (display_type == DT_DEFAULT && !display_remote) {
2971 #if defined(CONFIG_SDL) || defined(CONFIG_COCOA)
2972 display_type = DT_SDL;
2973 #else
2974 vnc_display = "localhost:0,to=99";
2975 show_vnc_port = 1;
2976 #endif
2980 /* init local displays */
2981 switch (display_type) {
2982 case DT_NOGRAPHIC:
2983 break;
2984 #if defined(CONFIG_CURSES)
2985 case DT_CURSES:
2986 curses_display_init(ds, full_screen);
2987 break;
2988 #endif
2989 #if defined(CONFIG_SDL)
2990 case DT_SDL:
2991 sdl_display_init(ds, full_screen, no_frame);
2992 break;
2993 #elif defined(CONFIG_COCOA)
2994 case DT_SDL:
2995 cocoa_display_init(ds, full_screen);
2996 break;
2997 #endif
2998 default:
2999 break;
3002 /* init remote displays */
3003 if (vnc_display) {
3004 vnc_display_init(ds);
3005 if (vnc_display_open(ds, vnc_display) < 0)
3006 exit(1);
3008 if (show_vnc_port) {
3009 printf("VNC server running on `%s'\n", vnc_display_local_addr(ds));
3012 #ifdef CONFIG_SPICE
3013 if (using_spice) {
3014 qemu_spice_display_init(ds);
3016 #endif
3018 /* display setup */
3019 dpy_resize(ds);
3020 dcl = ds->listeners;
3021 while (dcl != NULL) {
3022 if (dcl->dpy_refresh != NULL) {
3023 ds->gui_timer = qemu_new_timer(rt_clock, gui_update, ds);
3024 qemu_mod_timer(ds->gui_timer, qemu_get_clock(rt_clock));
3025 break;
3027 dcl = dcl->next;
3029 if (ds->gui_timer == NULL) {
3030 nographic_timer = qemu_new_timer(rt_clock, nographic_update, NULL);
3031 qemu_mod_timer(nographic_timer, qemu_get_clock(rt_clock));
3033 text_consoles_set_display(ds);
3035 if (gdbstub_dev && gdbserver_start(gdbstub_dev) < 0) {
3036 fprintf(stderr, "qemu: could not open gdbserver on device '%s'\n",
3037 gdbstub_dev);
3038 exit(1);
3041 qdev_machine_creation_done();
3043 if (rom_load_all() != 0) {
3044 fprintf(stderr, "rom loading failed\n");
3045 exit(1);
3048 qemu_system_reset();
3049 if (loadvm) {
3050 if (load_vmstate(loadvm) < 0) {
3051 autostart = 0;
3055 if (incoming) {
3056 int ret = qemu_start_incoming_migration(incoming);
3057 if (ret < 0) {
3058 fprintf(stderr, "Migration failed. Exit code %s(%d), exiting.\n",
3059 incoming, ret);
3060 exit(ret);
3062 } else if (autostart) {
3063 vm_start();
3066 os_setup_post();
3068 main_loop();
3069 quit_timers();
3070 net_cleanup();
3072 return 0;