pci-assign: Catch missing KVM support
[qemu/qemu-dev-zwu.git] / vl.c
blob4bad6753fe5b08bd6eee3c49e679ffd4f29fd37e
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 value = strtoull(option, &endptr, 0);
718 switch (*endptr) {
719 case 0: case 'M': case 'm':
720 value <<= 20;
721 break;
722 case 'G': case 'g':
723 value <<= 30;
724 break;
726 node_mem[nodenr] = value;
728 if (get_param_value(option, 128, "cpus", optarg) == 0) {
729 node_cpumask[nodenr] = 0;
730 } else {
731 value = strtoull(option, &endptr, 10);
732 if (value >= 64) {
733 value = 63;
734 fprintf(stderr, "only 64 CPUs in NUMA mode supported.\n");
735 } else {
736 if (*endptr == '-') {
737 endvalue = strtoull(endptr+1, &endptr, 10);
738 if (endvalue >= 63) {
739 endvalue = 62;
740 fprintf(stderr,
741 "only 63 CPUs in NUMA mode supported.\n");
743 value = (2ULL << endvalue) - (1ULL << value);
744 } else {
745 value = 1ULL << value;
748 node_cpumask[nodenr] = value;
750 nb_numa_nodes++;
752 return;
755 static void smp_parse(const char *optarg)
757 int smp, sockets = 0, threads = 0, cores = 0;
758 char *endptr;
759 char option[128];
761 smp = strtoul(optarg, &endptr, 10);
762 if (endptr != optarg) {
763 if (*endptr == ',') {
764 endptr++;
767 if (get_param_value(option, 128, "sockets", endptr) != 0)
768 sockets = strtoull(option, NULL, 10);
769 if (get_param_value(option, 128, "cores", endptr) != 0)
770 cores = strtoull(option, NULL, 10);
771 if (get_param_value(option, 128, "threads", endptr) != 0)
772 threads = strtoull(option, NULL, 10);
773 if (get_param_value(option, 128, "maxcpus", endptr) != 0)
774 max_cpus = strtoull(option, NULL, 10);
776 /* compute missing values, prefer sockets over cores over threads */
777 if (smp == 0 || sockets == 0) {
778 sockets = sockets > 0 ? sockets : 1;
779 cores = cores > 0 ? cores : 1;
780 threads = threads > 0 ? threads : 1;
781 if (smp == 0) {
782 smp = cores * threads * sockets;
784 } else {
785 if (cores == 0) {
786 threads = threads > 0 ? threads : 1;
787 cores = smp / (sockets * threads);
788 } else {
789 threads = smp / (cores * sockets);
792 smp_cpus = smp;
793 smp_cores = cores > 0 ? cores : 1;
794 smp_threads = threads > 0 ? threads : 1;
795 if (max_cpus == 0)
796 max_cpus = smp_cpus;
799 /***********************************************************/
800 /* USB devices */
802 static int usb_device_add(const char *devname)
804 const char *p;
805 USBDevice *dev = NULL;
807 if (!usb_enabled)
808 return -1;
810 /* drivers with .usbdevice_name entry in USBDeviceInfo */
811 dev = usbdevice_create(devname);
812 if (dev)
813 goto done;
815 /* the other ones */
816 if (strstart(devname, "host:", &p)) {
817 dev = usb_host_device_open(p);
818 } else if (!strcmp(devname, "bt") || strstart(devname, "bt:", &p)) {
819 dev = usb_bt_init(devname[2] ? hci_init(p) :
820 bt_new_hci(qemu_find_bt_vlan(0)));
821 } else {
822 return -1;
824 if (!dev)
825 return -1;
827 done:
828 return 0;
831 static int usb_device_del(const char *devname)
833 int bus_num, addr;
834 const char *p;
836 if (strstart(devname, "host:", &p))
837 return usb_host_device_close(p);
839 if (!usb_enabled)
840 return -1;
842 p = strchr(devname, '.');
843 if (!p)
844 return -1;
845 bus_num = strtoul(devname, NULL, 0);
846 addr = strtoul(p + 1, NULL, 0);
848 return usb_device_delete_addr(bus_num, addr);
851 static int usb_parse(const char *cmdline)
853 int r;
854 r = usb_device_add(cmdline);
855 if (r < 0) {
856 fprintf(stderr, "qemu: could not add USB device '%s'\n", cmdline);
858 return r;
861 void do_usb_add(Monitor *mon, const QDict *qdict)
863 const char *devname = qdict_get_str(qdict, "devname");
864 if (usb_device_add(devname) < 0) {
865 error_report("could not add USB device '%s'", devname);
869 void do_usb_del(Monitor *mon, const QDict *qdict)
871 const char *devname = qdict_get_str(qdict, "devname");
872 if (usb_device_del(devname) < 0) {
873 error_report("could not delete USB device '%s'", devname);
877 /***********************************************************/
878 /* PCMCIA/Cardbus */
880 static struct pcmcia_socket_entry_s {
881 PCMCIASocket *socket;
882 struct pcmcia_socket_entry_s *next;
883 } *pcmcia_sockets = 0;
885 void pcmcia_socket_register(PCMCIASocket *socket)
887 struct pcmcia_socket_entry_s *entry;
889 entry = qemu_malloc(sizeof(struct pcmcia_socket_entry_s));
890 entry->socket = socket;
891 entry->next = pcmcia_sockets;
892 pcmcia_sockets = entry;
895 void pcmcia_socket_unregister(PCMCIASocket *socket)
897 struct pcmcia_socket_entry_s *entry, **ptr;
899 ptr = &pcmcia_sockets;
900 for (entry = *ptr; entry; ptr = &entry->next, entry = *ptr)
901 if (entry->socket == socket) {
902 *ptr = entry->next;
903 qemu_free(entry);
907 void pcmcia_info(Monitor *mon)
909 struct pcmcia_socket_entry_s *iter;
911 if (!pcmcia_sockets)
912 monitor_printf(mon, "No PCMCIA sockets\n");
914 for (iter = pcmcia_sockets; iter; iter = iter->next)
915 monitor_printf(mon, "%s: %s\n", iter->socket->slot_string,
916 iter->socket->attached ? iter->socket->card_string :
917 "Empty");
920 /***********************************************************/
921 /* I/O handling */
923 typedef struct IOHandlerRecord {
924 int fd;
925 IOCanReadHandler *fd_read_poll;
926 IOHandler *fd_read;
927 IOHandler *fd_write;
928 int deleted;
929 void *opaque;
930 /* temporary data */
931 struct pollfd *ufd;
932 QLIST_ENTRY(IOHandlerRecord) next;
933 } IOHandlerRecord;
935 static QLIST_HEAD(, IOHandlerRecord) io_handlers =
936 QLIST_HEAD_INITIALIZER(io_handlers);
939 /* XXX: fd_read_poll should be suppressed, but an API change is
940 necessary in the character devices to suppress fd_can_read(). */
941 int qemu_set_fd_handler2(int fd,
942 IOCanReadHandler *fd_read_poll,
943 IOHandler *fd_read,
944 IOHandler *fd_write,
945 void *opaque)
947 IOHandlerRecord *ioh;
949 if (!fd_read && !fd_write) {
950 QLIST_FOREACH(ioh, &io_handlers, next) {
951 if (ioh->fd == fd) {
952 ioh->deleted = 1;
953 break;
956 } else {
957 QLIST_FOREACH(ioh, &io_handlers, next) {
958 if (ioh->fd == fd)
959 goto found;
961 ioh = qemu_mallocz(sizeof(IOHandlerRecord));
962 QLIST_INSERT_HEAD(&io_handlers, ioh, next);
963 found:
964 ioh->fd = fd;
965 ioh->fd_read_poll = fd_read_poll;
966 ioh->fd_read = fd_read;
967 ioh->fd_write = fd_write;
968 ioh->opaque = opaque;
969 ioh->deleted = 0;
971 qemu_notify_event();
972 return 0;
975 int qemu_set_fd_handler(int fd,
976 IOHandler *fd_read,
977 IOHandler *fd_write,
978 void *opaque)
980 return qemu_set_fd_handler2(fd, NULL, fd_read, fd_write, opaque);
983 /***********************************************************/
984 /* machine registration */
986 static QEMUMachine *first_machine = NULL;
987 QEMUMachine *current_machine = NULL;
989 int qemu_register_machine(QEMUMachine *m)
991 QEMUMachine **pm;
992 pm = &first_machine;
993 while (*pm != NULL)
994 pm = &(*pm)->next;
995 m->next = NULL;
996 *pm = m;
997 return 0;
1000 static QEMUMachine *find_machine(const char *name)
1002 QEMUMachine *m;
1004 for(m = first_machine; m != NULL; m = m->next) {
1005 if (!strcmp(m->name, name))
1006 return m;
1007 if (m->alias && !strcmp(m->alias, name))
1008 return m;
1010 return NULL;
1013 static QEMUMachine *find_default_machine(void)
1015 QEMUMachine *m;
1017 for(m = first_machine; m != NULL; m = m->next) {
1018 if (m->is_default) {
1019 return m;
1022 return NULL;
1025 /***********************************************************/
1026 /* main execution loop */
1028 static void gui_update(void *opaque)
1030 uint64_t interval = GUI_REFRESH_INTERVAL;
1031 DisplayState *ds = opaque;
1032 DisplayChangeListener *dcl = ds->listeners;
1034 qemu_flush_coalesced_mmio_buffer();
1035 dpy_refresh(ds);
1037 while (dcl != NULL) {
1038 if (dcl->gui_timer_interval &&
1039 dcl->gui_timer_interval < interval)
1040 interval = dcl->gui_timer_interval;
1041 dcl = dcl->next;
1043 qemu_mod_timer(ds->gui_timer, interval + qemu_get_clock(rt_clock));
1046 static void nographic_update(void *opaque)
1048 uint64_t interval = GUI_REFRESH_INTERVAL;
1050 qemu_flush_coalesced_mmio_buffer();
1051 qemu_mod_timer(nographic_timer, interval + qemu_get_clock(rt_clock));
1054 struct vm_change_state_entry {
1055 VMChangeStateHandler *cb;
1056 void *opaque;
1057 QLIST_ENTRY (vm_change_state_entry) entries;
1060 static QLIST_HEAD(vm_change_state_head, vm_change_state_entry) vm_change_state_head;
1062 VMChangeStateEntry *qemu_add_vm_change_state_handler(VMChangeStateHandler *cb,
1063 void *opaque)
1065 VMChangeStateEntry *e;
1067 e = qemu_mallocz(sizeof (*e));
1069 e->cb = cb;
1070 e->opaque = opaque;
1071 QLIST_INSERT_HEAD(&vm_change_state_head, e, entries);
1072 return e;
1075 void qemu_del_vm_change_state_handler(VMChangeStateEntry *e)
1077 QLIST_REMOVE (e, entries);
1078 qemu_free (e);
1081 void vm_state_notify(int running, int reason)
1083 VMChangeStateEntry *e;
1085 for (e = vm_change_state_head.lh_first; e; e = e->entries.le_next) {
1086 e->cb(e->opaque, running, reason);
1090 void vm_start(void)
1092 if (!vm_running) {
1093 cpu_enable_ticks();
1094 vm_running = 1;
1095 vm_state_notify(1, 0);
1096 resume_all_vcpus();
1097 monitor_protocol_event(QEVENT_RESUME, NULL);
1101 /* reset/shutdown handler */
1103 typedef struct QEMUResetEntry {
1104 QTAILQ_ENTRY(QEMUResetEntry) entry;
1105 QEMUResetHandler *func;
1106 void *opaque;
1107 } QEMUResetEntry;
1109 static QTAILQ_HEAD(reset_handlers, QEMUResetEntry) reset_handlers =
1110 QTAILQ_HEAD_INITIALIZER(reset_handlers);
1111 static int reset_requested;
1112 static int shutdown_requested;
1113 static int powerdown_requested;
1114 int debug_requested;
1115 int vmstop_requested;
1117 int qemu_no_shutdown(void)
1119 int r = no_shutdown;
1120 no_shutdown = 0;
1121 return r;
1124 int qemu_shutdown_requested(void)
1126 int r = shutdown_requested;
1127 shutdown_requested = 0;
1128 return r;
1131 int qemu_reset_requested(void)
1133 int r = reset_requested;
1134 reset_requested = 0;
1135 return r;
1138 int qemu_powerdown_requested(void)
1140 int r = powerdown_requested;
1141 powerdown_requested = 0;
1142 return r;
1145 static int qemu_debug_requested(void)
1147 int r = debug_requested;
1148 debug_requested = 0;
1149 return r;
1152 static int qemu_vmstop_requested(void)
1154 int r = vmstop_requested;
1155 vmstop_requested = 0;
1156 return r;
1159 void qemu_register_reset(QEMUResetHandler *func, void *opaque)
1161 QEMUResetEntry *re = qemu_mallocz(sizeof(QEMUResetEntry));
1163 re->func = func;
1164 re->opaque = opaque;
1165 QTAILQ_INSERT_TAIL(&reset_handlers, re, entry);
1168 void qemu_unregister_reset(QEMUResetHandler *func, void *opaque)
1170 QEMUResetEntry *re;
1172 QTAILQ_FOREACH(re, &reset_handlers, entry) {
1173 if (re->func == func && re->opaque == opaque) {
1174 QTAILQ_REMOVE(&reset_handlers, re, entry);
1175 qemu_free(re);
1176 return;
1181 void qemu_system_reset(void)
1183 QEMUResetEntry *re, *nre;
1185 /* reset all devices */
1186 QTAILQ_FOREACH_SAFE(re, &reset_handlers, entry, nre) {
1187 re->func(re->opaque);
1189 monitor_protocol_event(QEVENT_RESET, NULL);
1190 cpu_synchronize_all_post_reset();
1193 void qemu_system_reset_request(void)
1195 if (no_reboot) {
1196 shutdown_requested = 1;
1197 } else {
1198 reset_requested = 1;
1200 if (cpu_single_env) {
1201 cpu_single_env->stopped = 1;
1202 cpu_exit(cpu_single_env);
1204 qemu_notify_event();
1207 void qemu_system_shutdown_request(void)
1209 shutdown_requested = 1;
1210 qemu_notify_event();
1213 void qemu_system_powerdown_request(void)
1215 powerdown_requested = 1;
1216 qemu_notify_event();
1219 void main_loop_wait(int nonblocking)
1221 IOHandlerRecord *ioh;
1222 fd_set rfds, wfds, xfds;
1223 int ret, nfds;
1224 struct timeval tv;
1225 int timeout;
1227 if (nonblocking)
1228 timeout = 0;
1229 else {
1230 timeout = qemu_calculate_timeout();
1231 qemu_bh_update_timeout(&timeout);
1234 os_host_main_loop_wait(&timeout);
1236 /* poll any events */
1237 /* XXX: separate device handlers from system ones */
1238 nfds = -1;
1239 FD_ZERO(&rfds);
1240 FD_ZERO(&wfds);
1241 FD_ZERO(&xfds);
1242 QLIST_FOREACH(ioh, &io_handlers, next) {
1243 if (ioh->deleted)
1244 continue;
1245 if (ioh->fd_read &&
1246 (!ioh->fd_read_poll ||
1247 ioh->fd_read_poll(ioh->opaque) != 0)) {
1248 FD_SET(ioh->fd, &rfds);
1249 if (ioh->fd > nfds)
1250 nfds = ioh->fd;
1252 if (ioh->fd_write) {
1253 FD_SET(ioh->fd, &wfds);
1254 if (ioh->fd > nfds)
1255 nfds = ioh->fd;
1259 tv.tv_sec = timeout / 1000;
1260 tv.tv_usec = (timeout % 1000) * 1000;
1262 slirp_select_fill(&nfds, &rfds, &wfds, &xfds);
1264 qemu_mutex_unlock_iothread();
1265 ret = select(nfds + 1, &rfds, &wfds, &xfds, &tv);
1266 qemu_mutex_lock_iothread();
1267 if (ret > 0) {
1268 IOHandlerRecord *pioh;
1270 QLIST_FOREACH_SAFE(ioh, &io_handlers, next, pioh) {
1271 if (ioh->deleted) {
1272 QLIST_REMOVE(ioh, next);
1273 qemu_free(ioh);
1274 continue;
1276 if (ioh->fd_read && FD_ISSET(ioh->fd, &rfds)) {
1277 ioh->fd_read(ioh->opaque);
1278 if (!(ioh->fd_read_poll && ioh->fd_read_poll(ioh->opaque)))
1279 FD_CLR(ioh->fd, &rfds);
1281 if (ioh->fd_write && FD_ISSET(ioh->fd, &wfds)) {
1282 ioh->fd_write(ioh->opaque);
1287 slirp_select_poll(&rfds, &wfds, &xfds, (ret < 0));
1289 qemu_run_all_timers();
1291 /* Check bottom-halves last in case any of the earlier events triggered
1292 them. */
1293 qemu_bh_poll();
1297 static int vm_can_run(void)
1299 if (powerdown_requested)
1300 return 0;
1301 if (reset_requested)
1302 return 0;
1303 if (shutdown_requested)
1304 return 0;
1305 if (debug_requested)
1306 return 0;
1307 return 1;
1310 qemu_irq qemu_system_powerdown;
1312 static void main_loop(void)
1314 int r;
1316 if (kvm_enabled()) {
1317 kvm_main_loop();
1318 cpu_disable_ticks();
1319 return;
1322 qemu_main_loop_start();
1324 for (;;) {
1325 do {
1326 bool nonblocking = false;
1327 #ifdef CONFIG_PROFILER
1328 int64_t ti;
1329 #endif
1330 #ifndef CONFIG_IOTHREAD
1331 nonblocking = cpu_exec_all();
1332 #endif
1333 #ifdef CONFIG_PROFILER
1334 ti = profile_getclock();
1335 #endif
1336 main_loop_wait(nonblocking);
1337 #ifdef CONFIG_PROFILER
1338 dev_time += profile_getclock() - ti;
1339 #endif
1340 } while (vm_can_run());
1342 if ((r = qemu_debug_requested())) {
1343 vm_stop(r);
1345 if (qemu_shutdown_requested()) {
1346 monitor_protocol_event(QEVENT_SHUTDOWN, NULL);
1347 if (no_shutdown) {
1348 vm_stop(0);
1349 no_shutdown = 0;
1350 } else
1351 break;
1353 if (qemu_reset_requested()) {
1354 pause_all_vcpus();
1355 qemu_system_reset();
1356 resume_all_vcpus();
1358 if (qemu_powerdown_requested()) {
1359 monitor_protocol_event(QEVENT_POWERDOWN, NULL);
1360 qemu_irq_raise(qemu_system_powerdown);
1362 if ((r = qemu_vmstop_requested())) {
1363 vm_stop(r);
1366 bdrv_close_all();
1367 pause_all_vcpus();
1370 static void version(void)
1372 printf("QEMU emulator version " QEMU_VERSION QEMU_PKGVERSION ", Copyright (c) 2003-2008 Fabrice Bellard\n");
1375 static void help(int exitcode)
1377 const char *options_help =
1378 #define DEF(option, opt_arg, opt_enum, opt_help, arch_mask) \
1379 opt_help
1380 #define DEFHEADING(text) stringify(text) "\n"
1381 #include "qemu-options.def"
1382 #undef DEF
1383 #undef DEFHEADING
1384 #undef GEN_DOCS
1386 version();
1387 printf("usage: %s [options] [disk_image]\n"
1388 "\n"
1389 "'disk_image' is a raw hard disk image for IDE hard disk 0\n"
1390 "\n"
1391 "%s\n"
1392 "During emulation, the following keys are useful:\n"
1393 "ctrl-alt-f toggle full screen\n"
1394 "ctrl-alt-n switch to virtual console 'n'\n"
1395 "ctrl-alt toggle mouse and keyboard grab\n"
1396 "\n"
1397 "When using -nographic, press 'ctrl-a h' to get some help.\n",
1398 "qemu",
1399 options_help);
1400 exit(exitcode);
1403 #define HAS_ARG 0x0001
1405 typedef struct QEMUOption {
1406 const char *name;
1407 int flags;
1408 int index;
1409 uint32_t arch_mask;
1410 } QEMUOption;
1412 static const QEMUOption qemu_options[] = {
1413 { "h", 0, QEMU_OPTION_h, QEMU_ARCH_ALL },
1414 #define DEF(option, opt_arg, opt_enum, opt_help, arch_mask) \
1415 { option, opt_arg, opt_enum, arch_mask },
1416 #define DEFHEADING(text)
1417 #include "qemu-options.def"
1418 #undef DEF
1419 #undef DEFHEADING
1420 #undef GEN_DOCS
1421 { NULL },
1423 static void select_vgahw (const char *p)
1425 const char *opts;
1427 default_vga = 0;
1428 vga_interface_type = VGA_NONE;
1429 if (strstart(p, "std", &opts)) {
1430 vga_interface_type = VGA_STD;
1431 } else if (strstart(p, "cirrus", &opts)) {
1432 vga_interface_type = VGA_CIRRUS;
1433 } else if (strstart(p, "vmware", &opts)) {
1434 vga_interface_type = VGA_VMWARE;
1435 } else if (strstart(p, "xenfb", &opts)) {
1436 vga_interface_type = VGA_XENFB;
1437 } else if (!strstart(p, "none", &opts)) {
1438 invalid_vga:
1439 fprintf(stderr, "Unknown vga type: %s\n", p);
1440 exit(1);
1442 while (*opts) {
1443 const char *nextopt;
1445 if (strstart(opts, ",retrace=", &nextopt)) {
1446 opts = nextopt;
1447 if (strstart(opts, "dumb", &nextopt))
1448 vga_retrace_method = VGA_RETRACE_DUMB;
1449 else if (strstart(opts, "precise", &nextopt))
1450 vga_retrace_method = VGA_RETRACE_PRECISE;
1451 else goto invalid_vga;
1452 } else goto invalid_vga;
1453 opts = nextopt;
1457 static int balloon_parse(const char *arg)
1459 QemuOpts *opts;
1461 if (strcmp(arg, "none") == 0) {
1462 return 0;
1465 if (!strncmp(arg, "virtio", 6)) {
1466 if (arg[6] == ',') {
1467 /* have params -> parse them */
1468 opts = qemu_opts_parse(qemu_find_opts("device"), arg+7, 0);
1469 if (!opts)
1470 return -1;
1471 } else {
1472 /* create empty opts */
1473 opts = qemu_opts_create(qemu_find_opts("device"), NULL, 0);
1475 qemu_opt_set(opts, "driver", "virtio-balloon-pci");
1476 return 0;
1479 return -1;
1482 char *qemu_find_file(int type, const char *name)
1484 int len;
1485 const char *subdir;
1486 char *buf;
1488 /* If name contains path separators then try it as a straight path. */
1489 if ((strchr(name, '/') || strchr(name, '\\'))
1490 && access(name, R_OK) == 0) {
1491 return qemu_strdup(name);
1493 switch (type) {
1494 case QEMU_FILE_TYPE_BIOS:
1495 subdir = "";
1496 break;
1497 case QEMU_FILE_TYPE_KEYMAP:
1498 subdir = "keymaps/";
1499 break;
1500 default:
1501 abort();
1503 len = strlen(data_dir) + strlen(name) + strlen(subdir) + 2;
1504 buf = qemu_mallocz(len);
1505 snprintf(buf, len, "%s/%s%s", data_dir, subdir, name);
1506 if (access(buf, R_OK)) {
1507 qemu_free(buf);
1508 return NULL;
1510 return buf;
1513 static int device_help_func(QemuOpts *opts, void *opaque)
1515 return qdev_device_help(opts);
1518 static int device_init_func(QemuOpts *opts, void *opaque)
1520 DeviceState *dev;
1522 dev = qdev_device_add(opts);
1523 if (!dev)
1524 return -1;
1525 return 0;
1528 static int chardev_init_func(QemuOpts *opts, void *opaque)
1530 CharDriverState *chr;
1532 chr = qemu_chr_open_opts(opts, NULL);
1533 if (!chr)
1534 return -1;
1535 return 0;
1538 #ifdef CONFIG_VIRTFS
1539 static int fsdev_init_func(QemuOpts *opts, void *opaque)
1541 int ret;
1542 ret = qemu_fsdev_add(opts);
1544 return ret;
1546 #endif
1548 static int mon_init_func(QemuOpts *opts, void *opaque)
1550 CharDriverState *chr;
1551 const char *chardev;
1552 const char *mode;
1553 int flags;
1555 mode = qemu_opt_get(opts, "mode");
1556 if (mode == NULL) {
1557 mode = "readline";
1559 if (strcmp(mode, "readline") == 0) {
1560 flags = MONITOR_USE_READLINE;
1561 } else if (strcmp(mode, "control") == 0) {
1562 flags = MONITOR_USE_CONTROL;
1563 } else {
1564 fprintf(stderr, "unknown monitor mode \"%s\"\n", mode);
1565 exit(1);
1568 if (qemu_opt_get_bool(opts, "pretty", 0))
1569 flags |= MONITOR_USE_PRETTY;
1571 if (qemu_opt_get_bool(opts, "default", 0))
1572 flags |= MONITOR_IS_DEFAULT;
1574 chardev = qemu_opt_get(opts, "chardev");
1575 chr = qemu_chr_find(chardev);
1576 if (chr == NULL) {
1577 fprintf(stderr, "chardev \"%s\" not found\n", chardev);
1578 exit(1);
1581 monitor_init(chr, flags);
1582 return 0;
1585 static void monitor_parse(const char *optarg, const char *mode)
1587 static int monitor_device_index = 0;
1588 QemuOpts *opts;
1589 const char *p;
1590 char label[32];
1591 int def = 0;
1593 if (strstart(optarg, "chardev:", &p)) {
1594 snprintf(label, sizeof(label), "%s", p);
1595 } else {
1596 snprintf(label, sizeof(label), "compat_monitor%d",
1597 monitor_device_index);
1598 if (monitor_device_index == 0) {
1599 def = 1;
1601 opts = qemu_chr_parse_compat(label, optarg);
1602 if (!opts) {
1603 fprintf(stderr, "parse error: %s\n", optarg);
1604 exit(1);
1608 opts = qemu_opts_create(qemu_find_opts("mon"), label, 1);
1609 if (!opts) {
1610 fprintf(stderr, "duplicate chardev: %s\n", label);
1611 exit(1);
1613 qemu_opt_set(opts, "mode", mode);
1614 qemu_opt_set(opts, "chardev", label);
1615 if (def)
1616 qemu_opt_set(opts, "default", "on");
1617 monitor_device_index++;
1620 struct device_config {
1621 enum {
1622 DEV_USB, /* -usbdevice */
1623 DEV_BT, /* -bt */
1624 DEV_SERIAL, /* -serial */
1625 DEV_PARALLEL, /* -parallel */
1626 DEV_VIRTCON, /* -virtioconsole */
1627 DEV_DEBUGCON, /* -debugcon */
1628 } type;
1629 const char *cmdline;
1630 QTAILQ_ENTRY(device_config) next;
1632 QTAILQ_HEAD(, device_config) device_configs = QTAILQ_HEAD_INITIALIZER(device_configs);
1634 static void add_device_config(int type, const char *cmdline)
1636 struct device_config *conf;
1638 conf = qemu_mallocz(sizeof(*conf));
1639 conf->type = type;
1640 conf->cmdline = cmdline;
1641 QTAILQ_INSERT_TAIL(&device_configs, conf, next);
1644 static int foreach_device_config(int type, int (*func)(const char *cmdline))
1646 struct device_config *conf;
1647 int rc;
1649 QTAILQ_FOREACH(conf, &device_configs, next) {
1650 if (conf->type != type)
1651 continue;
1652 rc = func(conf->cmdline);
1653 if (0 != rc)
1654 return rc;
1656 return 0;
1659 static int serial_parse(const char *devname)
1661 static int index = 0;
1662 char label[32];
1664 if (strcmp(devname, "none") == 0)
1665 return 0;
1666 if (index == MAX_SERIAL_PORTS) {
1667 fprintf(stderr, "qemu: too many serial ports\n");
1668 exit(1);
1670 snprintf(label, sizeof(label), "serial%d", index);
1671 serial_hds[index] = qemu_chr_open(label, devname, NULL);
1672 if (!serial_hds[index]) {
1673 fprintf(stderr, "qemu: could not open serial device '%s': %s\n",
1674 devname, strerror(errno));
1675 return -1;
1677 index++;
1678 return 0;
1681 static int parallel_parse(const char *devname)
1683 static int index = 0;
1684 char label[32];
1686 if (strcmp(devname, "none") == 0)
1687 return 0;
1688 if (index == MAX_PARALLEL_PORTS) {
1689 fprintf(stderr, "qemu: too many parallel ports\n");
1690 exit(1);
1692 snprintf(label, sizeof(label), "parallel%d", index);
1693 parallel_hds[index] = qemu_chr_open(label, devname, NULL);
1694 if (!parallel_hds[index]) {
1695 fprintf(stderr, "qemu: could not open parallel device '%s': %s\n",
1696 devname, strerror(errno));
1697 return -1;
1699 index++;
1700 return 0;
1703 static int virtcon_parse(const char *devname)
1705 QemuOptsList *device = qemu_find_opts("device");
1706 static int index = 0;
1707 char label[32];
1708 QemuOpts *bus_opts, *dev_opts;
1710 if (strcmp(devname, "none") == 0)
1711 return 0;
1712 if (index == MAX_VIRTIO_CONSOLES) {
1713 fprintf(stderr, "qemu: too many virtio consoles\n");
1714 exit(1);
1717 bus_opts = qemu_opts_create(device, NULL, 0);
1718 qemu_opt_set(bus_opts, "driver", "virtio-serial");
1720 dev_opts = qemu_opts_create(device, NULL, 0);
1721 qemu_opt_set(dev_opts, "driver", "virtconsole");
1723 snprintf(label, sizeof(label), "virtcon%d", index);
1724 virtcon_hds[index] = qemu_chr_open(label, devname, NULL);
1725 if (!virtcon_hds[index]) {
1726 fprintf(stderr, "qemu: could not open virtio console '%s': %s\n",
1727 devname, strerror(errno));
1728 return -1;
1730 qemu_opt_set(dev_opts, "chardev", label);
1732 index++;
1733 return 0;
1736 static int debugcon_parse(const char *devname)
1738 QemuOpts *opts;
1740 if (!qemu_chr_open("debugcon", devname, NULL)) {
1741 exit(1);
1743 opts = qemu_opts_create(qemu_find_opts("device"), "debugcon", 1);
1744 if (!opts) {
1745 fprintf(stderr, "qemu: already have a debugcon device\n");
1746 exit(1);
1748 qemu_opt_set(opts, "driver", "isa-debugcon");
1749 qemu_opt_set(opts, "chardev", "debugcon");
1750 return 0;
1753 void qemu_add_exit_notifier(Notifier *notify)
1755 notifier_list_add(&exit_notifiers, notify);
1758 void qemu_remove_exit_notifier(Notifier *notify)
1760 notifier_list_remove(&exit_notifiers, notify);
1763 static void qemu_run_exit_notifiers(void)
1765 notifier_list_notify(&exit_notifiers);
1768 static const QEMUOption *lookup_opt(int argc, char **argv,
1769 const char **poptarg, int *poptind)
1771 const QEMUOption *popt;
1772 int optind = *poptind;
1773 char *r = argv[optind];
1774 const char *optarg;
1776 loc_set_cmdline(argv, optind, 1);
1777 optind++;
1778 /* Treat --foo the same as -foo. */
1779 if (r[1] == '-')
1780 r++;
1781 popt = qemu_options;
1782 for(;;) {
1783 if (!popt->name) {
1784 error_report("invalid option");
1785 exit(1);
1787 if (!strcmp(popt->name, r + 1))
1788 break;
1789 popt++;
1791 if (popt->flags & HAS_ARG) {
1792 if (optind >= argc) {
1793 error_report("requires an argument");
1794 exit(1);
1796 optarg = argv[optind++];
1797 loc_set_cmdline(argv, optind - 2, 2);
1798 } else {
1799 optarg = NULL;
1802 *poptarg = optarg;
1803 *poptind = optind;
1805 return popt;
1808 int main(int argc, char **argv, char **envp)
1810 const char *gdbstub_dev = NULL;
1811 int i;
1812 int snapshot, linux_boot;
1813 const char *icount_option = NULL;
1814 const char *initrd_filename;
1815 const char *kernel_filename, *kernel_cmdline;
1816 char boot_devices[33] = "cad"; /* default to HD->floppy->CD-ROM */
1817 DisplayState *ds;
1818 DisplayChangeListener *dcl;
1819 int cyls, heads, secs, translation;
1820 QemuOpts *hda_opts = NULL, *opts;
1821 QemuOptsList *olist;
1822 int optind;
1823 const char *optarg;
1824 const char *loadvm = NULL;
1825 QEMUMachine *machine;
1826 const char *cpu_model;
1827 int tb_size;
1828 const char *pid_file = NULL;
1829 const char *incoming = NULL;
1830 int show_vnc_port = 0;
1831 int defconfig = 1;
1833 #ifdef CONFIG_SIMPLE_TRACE
1834 const char *trace_file = NULL;
1835 #endif
1836 atexit(qemu_run_exit_notifiers);
1837 error_set_progname(argv[0]);
1839 init_clocks();
1841 qemu_cache_utils_init(envp);
1843 QLIST_INIT (&vm_change_state_head);
1844 os_setup_early_signal_handling();
1846 module_call_init(MODULE_INIT_MACHINE);
1847 machine = find_default_machine();
1848 cpu_model = NULL;
1849 initrd_filename = NULL;
1850 ram_size = 0;
1851 snapshot = 0;
1852 kernel_filename = NULL;
1853 kernel_cmdline = "";
1854 cyls = heads = secs = 0;
1855 translation = BIOS_ATA_TRANSLATION_AUTO;
1857 for (i = 0; i < MAX_NODES; i++) {
1858 node_mem[i] = 0;
1859 node_cpumask[i] = 0;
1862 nb_numa_nodes = 0;
1863 nb_nics = 0;
1865 tb_size = 0;
1866 autostart= 1;
1868 /* first pass of option parsing */
1869 optind = 1;
1870 while (optind < argc) {
1871 if (argv[optind][0] != '-') {
1872 /* disk image */
1873 optind++;
1874 continue;
1875 } else {
1876 const QEMUOption *popt;
1878 popt = lookup_opt(argc, argv, &optarg, &optind);
1879 switch (popt->index) {
1880 case QEMU_OPTION_nodefconfig:
1881 defconfig=0;
1882 break;
1887 if (defconfig) {
1888 int ret;
1890 ret = qemu_read_config_file(CONFIG_QEMU_CONFDIR "/qemu.conf");
1891 if (ret < 0 && ret != -ENOENT) {
1892 exit(1);
1895 ret = qemu_read_config_file(arch_config_name);
1896 if (ret < 0 && ret != -ENOENT) {
1897 exit(1);
1900 cpudef_init();
1902 /* second pass of option parsing */
1903 optind = 1;
1904 for(;;) {
1905 if (optind >= argc)
1906 break;
1907 if (argv[optind][0] != '-') {
1908 hda_opts = drive_add(argv[optind++], HD_ALIAS, 0);
1909 } else {
1910 const QEMUOption *popt;
1912 popt = lookup_opt(argc, argv, &optarg, &optind);
1913 if (!(popt->arch_mask & arch_type)) {
1914 printf("Option %s not supported for this target\n", popt->name);
1915 exit(1);
1917 switch(popt->index) {
1918 case QEMU_OPTION_M:
1919 machine = find_machine(optarg);
1920 if (!machine) {
1921 QEMUMachine *m;
1922 printf("Supported machines are:\n");
1923 for(m = first_machine; m != NULL; m = m->next) {
1924 if (m->alias)
1925 printf("%-10s %s (alias of %s)\n",
1926 m->alias, m->desc, m->name);
1927 printf("%-10s %s%s\n",
1928 m->name, m->desc,
1929 m->is_default ? " (default)" : "");
1931 exit(*optarg != '?');
1933 break;
1934 case QEMU_OPTION_cpu:
1935 /* hw initialization will check this */
1936 if (*optarg == '?') {
1937 list_cpus(stdout, &fprintf, optarg);
1938 exit(0);
1939 } else {
1940 cpu_model = optarg;
1942 break;
1943 case QEMU_OPTION_initrd:
1944 initrd_filename = optarg;
1945 break;
1946 case QEMU_OPTION_hda:
1947 if (cyls == 0)
1948 hda_opts = drive_add(optarg, HD_ALIAS, 0);
1949 else
1950 hda_opts = drive_add(optarg, HD_ALIAS
1951 ",cyls=%d,heads=%d,secs=%d%s",
1952 0, cyls, heads, secs,
1953 translation == BIOS_ATA_TRANSLATION_LBA ?
1954 ",trans=lba" :
1955 translation == BIOS_ATA_TRANSLATION_NONE ?
1956 ",trans=none" : "");
1957 break;
1958 case QEMU_OPTION_hdb:
1959 case QEMU_OPTION_hdc:
1960 case QEMU_OPTION_hdd:
1961 drive_add(optarg, HD_ALIAS, popt->index - QEMU_OPTION_hda);
1962 break;
1963 case QEMU_OPTION_drive:
1964 drive_add(NULL, "%s", optarg);
1965 break;
1966 case QEMU_OPTION_set:
1967 if (qemu_set_option(optarg) != 0)
1968 exit(1);
1969 break;
1970 case QEMU_OPTION_global:
1971 if (qemu_global_option(optarg) != 0)
1972 exit(1);
1973 break;
1974 case QEMU_OPTION_mtdblock:
1975 drive_add(optarg, MTD_ALIAS);
1976 break;
1977 case QEMU_OPTION_sd:
1978 drive_add(optarg, SD_ALIAS);
1979 break;
1980 case QEMU_OPTION_pflash:
1981 drive_add(optarg, PFLASH_ALIAS);
1982 break;
1983 case QEMU_OPTION_snapshot:
1984 snapshot = 1;
1985 break;
1986 case QEMU_OPTION_hdachs:
1988 const char *p;
1989 p = optarg;
1990 cyls = strtol(p, (char **)&p, 0);
1991 if (cyls < 1 || cyls > 16383)
1992 goto chs_fail;
1993 if (*p != ',')
1994 goto chs_fail;
1995 p++;
1996 heads = strtol(p, (char **)&p, 0);
1997 if (heads < 1 || heads > 16)
1998 goto chs_fail;
1999 if (*p != ',')
2000 goto chs_fail;
2001 p++;
2002 secs = strtol(p, (char **)&p, 0);
2003 if (secs < 1 || secs > 63)
2004 goto chs_fail;
2005 if (*p == ',') {
2006 p++;
2007 if (!strcmp(p, "none"))
2008 translation = BIOS_ATA_TRANSLATION_NONE;
2009 else if (!strcmp(p, "lba"))
2010 translation = BIOS_ATA_TRANSLATION_LBA;
2011 else if (!strcmp(p, "auto"))
2012 translation = BIOS_ATA_TRANSLATION_AUTO;
2013 else
2014 goto chs_fail;
2015 } else if (*p != '\0') {
2016 chs_fail:
2017 fprintf(stderr, "qemu: invalid physical CHS format\n");
2018 exit(1);
2020 if (hda_opts != NULL) {
2021 char num[16];
2022 snprintf(num, sizeof(num), "%d", cyls);
2023 qemu_opt_set(hda_opts, "cyls", num);
2024 snprintf(num, sizeof(num), "%d", heads);
2025 qemu_opt_set(hda_opts, "heads", num);
2026 snprintf(num, sizeof(num), "%d", secs);
2027 qemu_opt_set(hda_opts, "secs", num);
2028 if (translation == BIOS_ATA_TRANSLATION_LBA)
2029 qemu_opt_set(hda_opts, "trans", "lba");
2030 if (translation == BIOS_ATA_TRANSLATION_NONE)
2031 qemu_opt_set(hda_opts, "trans", "none");
2034 break;
2035 case QEMU_OPTION_numa:
2036 if (nb_numa_nodes >= MAX_NODES) {
2037 fprintf(stderr, "qemu: too many NUMA nodes\n");
2038 exit(1);
2040 numa_add(optarg);
2041 break;
2042 case QEMU_OPTION_nographic:
2043 display_type = DT_NOGRAPHIC;
2044 break;
2045 #ifdef CONFIG_CURSES
2046 case QEMU_OPTION_curses:
2047 display_type = DT_CURSES;
2048 break;
2049 #endif
2050 case QEMU_OPTION_portrait:
2051 graphic_rotate = 1;
2052 break;
2053 case QEMU_OPTION_kernel:
2054 kernel_filename = optarg;
2055 break;
2056 case QEMU_OPTION_append:
2057 kernel_cmdline = optarg;
2058 break;
2059 case QEMU_OPTION_cdrom:
2060 drive_add(optarg, CDROM_ALIAS);
2061 break;
2062 case QEMU_OPTION_boot:
2064 static const char * const params[] = {
2065 "order", "once", "menu", NULL
2067 char buf[sizeof(boot_devices)];
2068 char *standard_boot_devices;
2069 int legacy = 0;
2071 if (!strchr(optarg, '=')) {
2072 legacy = 1;
2073 pstrcpy(buf, sizeof(buf), optarg);
2074 } else if (check_params(buf, sizeof(buf), params, optarg) < 0) {
2075 fprintf(stderr,
2076 "qemu: unknown boot parameter '%s' in '%s'\n",
2077 buf, optarg);
2078 exit(1);
2081 if (legacy ||
2082 get_param_value(buf, sizeof(buf), "order", optarg)) {
2083 validate_bootdevices(buf);
2084 pstrcpy(boot_devices, sizeof(boot_devices), buf);
2086 if (!legacy) {
2087 if (get_param_value(buf, sizeof(buf),
2088 "once", optarg)) {
2089 validate_bootdevices(buf);
2090 standard_boot_devices = qemu_strdup(boot_devices);
2091 pstrcpy(boot_devices, sizeof(boot_devices), buf);
2092 qemu_register_reset(restore_boot_devices,
2093 standard_boot_devices);
2095 if (get_param_value(buf, sizeof(buf),
2096 "menu", optarg)) {
2097 if (!strcmp(buf, "on")) {
2098 boot_menu = 1;
2099 } else if (!strcmp(buf, "off")) {
2100 boot_menu = 0;
2101 } else {
2102 fprintf(stderr,
2103 "qemu: invalid option value '%s'\n",
2104 buf);
2105 exit(1);
2110 break;
2111 case QEMU_OPTION_fda:
2112 case QEMU_OPTION_fdb:
2113 drive_add(optarg, FD_ALIAS, popt->index - QEMU_OPTION_fda);
2114 break;
2115 case QEMU_OPTION_no_fd_bootchk:
2116 fd_bootchk = 0;
2117 break;
2118 case QEMU_OPTION_netdev:
2119 if (net_client_parse(qemu_find_opts("netdev"), optarg) == -1) {
2120 exit(1);
2122 break;
2123 case QEMU_OPTION_net:
2124 if (net_client_parse(qemu_find_opts("net"), optarg) == -1) {
2125 exit(1);
2127 break;
2128 #ifdef CONFIG_SLIRP
2129 case QEMU_OPTION_tftp:
2130 legacy_tftp_prefix = optarg;
2131 break;
2132 case QEMU_OPTION_bootp:
2133 legacy_bootp_filename = optarg;
2134 break;
2135 case QEMU_OPTION_redir:
2136 if (net_slirp_redir(optarg) < 0)
2137 exit(1);
2138 break;
2139 #endif
2140 case QEMU_OPTION_bt:
2141 add_device_config(DEV_BT, optarg);
2142 break;
2143 case QEMU_OPTION_audio_help:
2144 if (!(audio_available())) {
2145 printf("Option %s not supported for this target\n", popt->name);
2146 exit(1);
2148 AUD_help ();
2149 exit (0);
2150 break;
2151 case QEMU_OPTION_soundhw:
2152 if (!(audio_available())) {
2153 printf("Option %s not supported for this target\n", popt->name);
2154 exit(1);
2156 select_soundhw (optarg);
2157 break;
2158 case QEMU_OPTION_h:
2159 help(0);
2160 break;
2161 case QEMU_OPTION_version:
2162 version();
2163 exit(0);
2164 break;
2165 case QEMU_OPTION_m: {
2166 uint64_t value;
2167 char *ptr;
2169 value = strtoul(optarg, &ptr, 10);
2170 switch (*ptr) {
2171 case 0: case 'M': case 'm':
2172 value <<= 20;
2173 break;
2174 case 'G': case 'g':
2175 value <<= 30;
2176 break;
2177 default:
2178 fprintf(stderr, "qemu: invalid ram size: %s\n", optarg);
2179 exit(1);
2182 /* On 32-bit hosts, QEMU is limited by virtual address space */
2183 if (value > (2047 << 20) && HOST_LONG_BITS == 32) {
2184 fprintf(stderr, "qemu: at most 2047 MB RAM can be simulated\n");
2185 exit(1);
2187 if (value != (uint64_t)(ram_addr_t)value) {
2188 fprintf(stderr, "qemu: ram size too large\n");
2189 exit(1);
2191 ram_size = value;
2192 break;
2194 case QEMU_OPTION_mempath:
2195 mem_path = optarg;
2196 break;
2197 #ifdef MAP_POPULATE
2198 case QEMU_OPTION_mem_prealloc:
2199 mem_prealloc = 1;
2200 break;
2201 #endif
2202 case QEMU_OPTION_d:
2203 set_cpu_log(optarg);
2204 break;
2205 case QEMU_OPTION_s:
2206 gdbstub_dev = "tcp::" DEFAULT_GDBSTUB_PORT;
2207 break;
2208 case QEMU_OPTION_gdb:
2209 gdbstub_dev = optarg;
2210 break;
2211 case QEMU_OPTION_L:
2212 data_dir = optarg;
2213 break;
2214 case QEMU_OPTION_bios:
2215 bios_name = optarg;
2216 break;
2217 case QEMU_OPTION_singlestep:
2218 singlestep = 1;
2219 break;
2220 case QEMU_OPTION_S:
2221 autostart = 0;
2222 break;
2223 case QEMU_OPTION_k:
2224 keyboard_layout = optarg;
2225 break;
2226 case QEMU_OPTION_localtime:
2227 rtc_utc = 0;
2228 break;
2229 case QEMU_OPTION_vga:
2230 select_vgahw (optarg);
2231 break;
2232 case QEMU_OPTION_g:
2234 const char *p;
2235 int w, h, depth;
2236 p = optarg;
2237 w = strtol(p, (char **)&p, 10);
2238 if (w <= 0) {
2239 graphic_error:
2240 fprintf(stderr, "qemu: invalid resolution or depth\n");
2241 exit(1);
2243 if (*p != 'x')
2244 goto graphic_error;
2245 p++;
2246 h = strtol(p, (char **)&p, 10);
2247 if (h <= 0)
2248 goto graphic_error;
2249 if (*p == 'x') {
2250 p++;
2251 depth = strtol(p, (char **)&p, 10);
2252 if (depth != 8 && depth != 15 && depth != 16 &&
2253 depth != 24 && depth != 32)
2254 goto graphic_error;
2255 } else if (*p == '\0') {
2256 depth = graphic_depth;
2257 } else {
2258 goto graphic_error;
2261 graphic_width = w;
2262 graphic_height = h;
2263 graphic_depth = depth;
2265 break;
2266 case QEMU_OPTION_echr:
2268 char *r;
2269 term_escape_char = strtol(optarg, &r, 0);
2270 if (r == optarg)
2271 printf("Bad argument to echr\n");
2272 break;
2274 case QEMU_OPTION_monitor:
2275 monitor_parse(optarg, "readline");
2276 default_monitor = 0;
2277 break;
2278 case QEMU_OPTION_qmp:
2279 monitor_parse(optarg, "control");
2280 default_monitor = 0;
2281 break;
2282 case QEMU_OPTION_mon:
2283 opts = qemu_opts_parse(qemu_find_opts("mon"), optarg, 1);
2284 if (!opts) {
2285 exit(1);
2287 default_monitor = 0;
2288 break;
2289 case QEMU_OPTION_chardev:
2290 opts = qemu_opts_parse(qemu_find_opts("chardev"), optarg, 1);
2291 if (!opts) {
2292 exit(1);
2294 break;
2295 case QEMU_OPTION_fsdev:
2296 olist = qemu_find_opts("fsdev");
2297 if (!olist) {
2298 fprintf(stderr, "fsdev is not supported by this qemu build.\n");
2299 exit(1);
2301 opts = qemu_opts_parse(olist, optarg, 1);
2302 if (!opts) {
2303 fprintf(stderr, "parse error: %s\n", optarg);
2304 exit(1);
2306 break;
2307 case QEMU_OPTION_virtfs: {
2308 char *arg_fsdev = NULL;
2309 char *arg_9p = NULL;
2310 int len = 0;
2312 olist = qemu_find_opts("virtfs");
2313 if (!olist) {
2314 fprintf(stderr, "virtfs is not supported by this qemu build.\n");
2315 exit(1);
2317 opts = qemu_opts_parse(olist, optarg, 1);
2318 if (!opts) {
2319 fprintf(stderr, "parse error: %s\n", optarg);
2320 exit(1);
2323 if (qemu_opt_get(opts, "fstype") == NULL ||
2324 qemu_opt_get(opts, "mount_tag") == NULL ||
2325 qemu_opt_get(opts, "path") == NULL ||
2326 qemu_opt_get(opts, "security_model") == NULL) {
2327 fprintf(stderr, "Usage: -virtfs fstype,path=/share_path/,"
2328 "security_model=[mapped|passthrough|none],"
2329 "mnt_tag=tag.\n");
2330 exit(1);
2333 len = strlen(",id=,path=,security_model=");
2334 len += strlen(qemu_opt_get(opts, "fstype"));
2335 len += strlen(qemu_opt_get(opts, "mount_tag"));
2336 len += strlen(qemu_opt_get(opts, "path"));
2337 len += strlen(qemu_opt_get(opts, "security_model"));
2338 arg_fsdev = qemu_malloc((len + 1) * sizeof(*arg_fsdev));
2340 snprintf(arg_fsdev, (len + 1) * sizeof(*arg_fsdev),
2341 "%s,id=%s,path=%s,security_model=%s",
2342 qemu_opt_get(opts, "fstype"),
2343 qemu_opt_get(opts, "mount_tag"),
2344 qemu_opt_get(opts, "path"),
2345 qemu_opt_get(opts, "security_model"));
2347 len = strlen("virtio-9p-pci,fsdev=,mount_tag=");
2348 len += 2*strlen(qemu_opt_get(opts, "mount_tag"));
2349 arg_9p = qemu_malloc((len + 1) * sizeof(*arg_9p));
2351 snprintf(arg_9p, (len + 1) * sizeof(*arg_9p),
2352 "virtio-9p-pci,fsdev=%s,mount_tag=%s",
2353 qemu_opt_get(opts, "mount_tag"),
2354 qemu_opt_get(opts, "mount_tag"));
2356 if (!qemu_opts_parse(qemu_find_opts("fsdev"), arg_fsdev, 1)) {
2357 fprintf(stderr, "parse error [fsdev]: %s\n", optarg);
2358 exit(1);
2361 if (!qemu_opts_parse(qemu_find_opts("device"), arg_9p, 1)) {
2362 fprintf(stderr, "parse error [device]: %s\n", optarg);
2363 exit(1);
2366 qemu_free(arg_fsdev);
2367 qemu_free(arg_9p);
2368 break;
2370 case QEMU_OPTION_serial:
2371 add_device_config(DEV_SERIAL, optarg);
2372 default_serial = 0;
2373 if (strncmp(optarg, "mon:", 4) == 0) {
2374 default_monitor = 0;
2376 break;
2377 case QEMU_OPTION_watchdog:
2378 if (watchdog) {
2379 fprintf(stderr,
2380 "qemu: only one watchdog option may be given\n");
2381 return 1;
2383 watchdog = optarg;
2384 break;
2385 case QEMU_OPTION_watchdog_action:
2386 if (select_watchdog_action(optarg) == -1) {
2387 fprintf(stderr, "Unknown -watchdog-action parameter\n");
2388 exit(1);
2390 break;
2391 case QEMU_OPTION_virtiocon:
2392 add_device_config(DEV_VIRTCON, optarg);
2393 default_virtcon = 0;
2394 if (strncmp(optarg, "mon:", 4) == 0) {
2395 default_monitor = 0;
2397 break;
2398 case QEMU_OPTION_parallel:
2399 add_device_config(DEV_PARALLEL, optarg);
2400 default_parallel = 0;
2401 if (strncmp(optarg, "mon:", 4) == 0) {
2402 default_monitor = 0;
2404 break;
2405 case QEMU_OPTION_debugcon:
2406 add_device_config(DEV_DEBUGCON, optarg);
2407 break;
2408 case QEMU_OPTION_loadvm:
2409 loadvm = optarg;
2410 break;
2411 case QEMU_OPTION_full_screen:
2412 full_screen = 1;
2413 break;
2414 #ifdef CONFIG_SDL
2415 case QEMU_OPTION_no_frame:
2416 no_frame = 1;
2417 break;
2418 case QEMU_OPTION_alt_grab:
2419 alt_grab = 1;
2420 break;
2421 case QEMU_OPTION_ctrl_grab:
2422 ctrl_grab = 1;
2423 break;
2424 case QEMU_OPTION_no_quit:
2425 no_quit = 1;
2426 break;
2427 case QEMU_OPTION_sdl:
2428 display_type = DT_SDL;
2429 break;
2430 #endif
2431 case QEMU_OPTION_pidfile:
2432 pid_file = optarg;
2433 break;
2434 case QEMU_OPTION_win2k_hack:
2435 win2k_install_hack = 1;
2436 break;
2437 case QEMU_OPTION_rtc_td_hack:
2438 rtc_td_hack = 1;
2439 break;
2440 case QEMU_OPTION_acpitable:
2441 do_acpitable_option(optarg);
2442 break;
2443 case QEMU_OPTION_smbios:
2444 do_smbios_option(optarg);
2445 break;
2446 #ifdef OBSOLETE_KVM_IMPL
2447 case QEMU_OPTION_enable_kvm:
2448 kvm_allowed = 1;
2449 #endif
2450 break;
2451 case QEMU_OPTION_no_kvm:
2452 kvm_allowed = 0;
2453 #ifdef CONFIG_NO_CPU_EMULATION
2454 fprintf(stderr, "cpu emulation not configured\n");
2455 exit(1);
2456 #endif
2457 break;
2458 #ifdef CONFIG_KVM
2459 case QEMU_OPTION_no_kvm_irqchip: {
2460 kvm_irqchip = 0;
2461 kvm_pit = 0;
2462 break;
2464 case QEMU_OPTION_no_kvm_pit: {
2465 kvm_pit = 0;
2466 break;
2468 case QEMU_OPTION_no_kvm_pit_reinjection: {
2469 kvm_pit_reinject = 0;
2470 break;
2472 case QEMU_OPTION_enable_nesting: {
2473 kvm_nested = 1;
2474 break;
2476 #endif
2477 case QEMU_OPTION_usb:
2478 usb_enabled = 1;
2479 break;
2480 case QEMU_OPTION_usbdevice:
2481 usb_enabled = 1;
2482 add_device_config(DEV_USB, optarg);
2483 break;
2484 case QEMU_OPTION_device:
2485 if (!qemu_opts_parse(qemu_find_opts("device"), optarg, 1)) {
2486 exit(1);
2488 break;
2489 case QEMU_OPTION_smp:
2490 smp_parse(optarg);
2491 if (smp_cpus < 1) {
2492 fprintf(stderr, "Invalid number of CPUs\n");
2493 exit(1);
2495 if (max_cpus < smp_cpus) {
2496 fprintf(stderr, "maxcpus must be equal to or greater than "
2497 "smp\n");
2498 exit(1);
2500 if (max_cpus > 255) {
2501 fprintf(stderr, "Unsupported number of maxcpus\n");
2502 exit(1);
2504 break;
2505 case QEMU_OPTION_vnc:
2506 display_remote++;
2507 vnc_display = optarg;
2508 break;
2509 case QEMU_OPTION_no_acpi:
2510 acpi_enabled = 0;
2511 break;
2512 case QEMU_OPTION_no_hpet:
2513 no_hpet = 1;
2514 break;
2515 case QEMU_OPTION_balloon:
2516 if (balloon_parse(optarg) < 0) {
2517 fprintf(stderr, "Unknown -balloon argument %s\n", optarg);
2518 exit(1);
2520 break;
2521 case QEMU_OPTION_no_reboot:
2522 no_reboot = 1;
2523 break;
2524 case QEMU_OPTION_no_shutdown:
2525 no_shutdown = 1;
2526 break;
2527 case QEMU_OPTION_show_cursor:
2528 cursor_hide = 0;
2529 break;
2530 case QEMU_OPTION_uuid:
2531 if(qemu_uuid_parse(optarg, qemu_uuid) < 0) {
2532 fprintf(stderr, "Fail to parse UUID string."
2533 " Wrong format.\n");
2534 exit(1);
2536 break;
2537 case QEMU_OPTION_option_rom:
2538 if (nb_option_roms >= MAX_OPTION_ROMS) {
2539 fprintf(stderr, "Too many option ROMs\n");
2540 exit(1);
2542 option_rom[nb_option_roms] = optarg;
2543 nb_option_roms++;
2544 break;
2545 case QEMU_OPTION_semihosting:
2546 semihosting_enabled = 1;
2547 break;
2548 case QEMU_OPTION_tdf:
2549 time_drift_fix = 1;
2550 break;
2551 case QEMU_OPTION_kvm_shadow_memory:
2552 kvm_shadow_memory = (int64_t)atoi(optarg) * 1024 * 1024 / 4096;
2553 break;
2554 case QEMU_OPTION_name:
2555 qemu_name = qemu_strdup(optarg);
2557 char *p = strchr(qemu_name, ',');
2558 if (p != NULL) {
2559 *p++ = 0;
2560 if (strncmp(p, "process=", 8)) {
2561 fprintf(stderr, "Unknown subargument %s to -name", p);
2562 exit(1);
2564 p += 8;
2565 os_set_proc_name(p);
2568 break;
2569 case QEMU_OPTION_prom_env:
2570 if (nb_prom_envs >= MAX_PROM_ENVS) {
2571 fprintf(stderr, "Too many prom variables\n");
2572 exit(1);
2574 prom_envs[nb_prom_envs] = optarg;
2575 nb_prom_envs++;
2576 break;
2577 case QEMU_OPTION_old_param:
2578 old_param = 1;
2579 break;
2580 case QEMU_OPTION_clock:
2581 configure_alarms(optarg);
2582 break;
2583 case QEMU_OPTION_startdate:
2584 configure_rtc_date_offset(optarg, 1);
2585 break;
2586 case QEMU_OPTION_rtc:
2587 opts = qemu_opts_parse(qemu_find_opts("rtc"), optarg, 0);
2588 if (!opts) {
2589 exit(1);
2591 configure_rtc(opts);
2592 break;
2593 case QEMU_OPTION_tb_size:
2594 tb_size = strtol(optarg, NULL, 0);
2595 if (tb_size < 0)
2596 tb_size = 0;
2597 break;
2598 case QEMU_OPTION_icount:
2599 icount_option = optarg;
2600 break;
2601 case QEMU_OPTION_incoming:
2602 incoming = optarg;
2603 incoming_expected = true;
2604 break;
2605 case QEMU_OPTION_nodefaults:
2606 default_serial = 0;
2607 default_parallel = 0;
2608 default_virtcon = 0;
2609 default_monitor = 0;
2610 default_vga = 0;
2611 default_net = 0;
2612 default_floppy = 0;
2613 default_cdrom = 0;
2614 default_sdcard = 0;
2615 break;
2616 #ifndef _WIN32
2617 case QEMU_OPTION_nvram:
2618 nvram = optarg;
2619 break;
2620 #endif
2621 case QEMU_OPTION_xen_domid:
2622 if (!(xen_available())) {
2623 printf("Option %s not supported for this target\n", popt->name);
2624 exit(1);
2626 xen_domid = atoi(optarg);
2627 break;
2628 case QEMU_OPTION_xen_create:
2629 if (!(xen_available())) {
2630 printf("Option %s not supported for this target\n", popt->name);
2631 exit(1);
2633 xen_mode = XEN_CREATE;
2634 break;
2635 case QEMU_OPTION_xen_attach:
2636 if (!(xen_available())) {
2637 printf("Option %s not supported for this target\n", popt->name);
2638 exit(1);
2640 xen_mode = XEN_ATTACH;
2641 break;
2642 #ifdef CONFIG_SIMPLE_TRACE
2643 case QEMU_OPTION_trace:
2644 opts = qemu_opts_parse(qemu_find_opts("trace"), optarg, 0);
2645 if (opts) {
2646 trace_file = qemu_opt_get(opts, "file");
2648 break;
2649 #endif
2650 case QEMU_OPTION_readconfig:
2652 int ret = qemu_read_config_file(optarg);
2653 if (ret < 0) {
2654 fprintf(stderr, "read config %s: %s\n", optarg,
2655 strerror(-ret));
2656 exit(1);
2658 break;
2660 case QEMU_OPTION_spice:
2661 olist = qemu_find_opts("spice");
2662 if (!olist) {
2663 fprintf(stderr, "spice is not supported by this qemu build.\n");
2664 exit(1);
2666 opts = qemu_opts_parse(olist, optarg, 0);
2667 if (!opts) {
2668 fprintf(stderr, "parse error: %s\n", optarg);
2669 exit(1);
2671 break;
2672 case QEMU_OPTION_writeconfig:
2674 FILE *fp;
2675 if (strcmp(optarg, "-") == 0) {
2676 fp = stdout;
2677 } else {
2678 fp = fopen(optarg, "w");
2679 if (fp == NULL) {
2680 fprintf(stderr, "open %s: %s\n", optarg, strerror(errno));
2681 exit(1);
2684 qemu_config_write(fp);
2685 fclose(fp);
2686 break;
2688 default:
2689 os_parse_cmd_args(popt->index, optarg);
2693 loc_set_none();
2695 /* If no data_dir is specified then try to find it relative to the
2696 executable path. */
2697 if (!data_dir) {
2698 data_dir = os_find_datadir(argv[0]);
2700 /* If all else fails use the install patch specified when building. */
2701 if (!data_dir) {
2702 data_dir = CONFIG_QEMU_DATADIR;
2705 #ifdef CONFIG_SIMPLE_TRACE
2707 * Set the trace file name, if specified.
2709 st_set_trace_file(trace_file);
2710 #endif
2712 * Default to max_cpus = smp_cpus, in case the user doesn't
2713 * specify a max_cpus value.
2715 if (!max_cpus)
2716 max_cpus = smp_cpus;
2718 machine->max_cpus = machine->max_cpus ?: 1; /* Default to UP */
2719 if (smp_cpus > machine->max_cpus) {
2720 fprintf(stderr, "Number of SMP cpus requested (%d), exceeds max cpus "
2721 "supported by machine `%s' (%d)\n", smp_cpus, machine->name,
2722 machine->max_cpus);
2723 exit(1);
2726 qemu_opts_foreach(qemu_find_opts("device"), default_driver_check, NULL, 0);
2727 qemu_opts_foreach(qemu_find_opts("global"), default_driver_check, NULL, 0);
2729 if (machine->no_serial) {
2730 default_serial = 0;
2732 if (machine->no_parallel) {
2733 default_parallel = 0;
2735 if (!machine->use_virtcon) {
2736 default_virtcon = 0;
2738 if (machine->no_vga) {
2739 default_vga = 0;
2741 if (machine->no_floppy) {
2742 default_floppy = 0;
2744 if (machine->no_cdrom) {
2745 default_cdrom = 0;
2747 if (machine->no_sdcard) {
2748 default_sdcard = 0;
2751 if (display_type == DT_NOGRAPHIC) {
2752 if (default_parallel)
2753 add_device_config(DEV_PARALLEL, "null");
2754 if (default_serial && default_monitor) {
2755 add_device_config(DEV_SERIAL, "mon:stdio");
2756 } else if (default_virtcon && default_monitor) {
2757 add_device_config(DEV_VIRTCON, "mon:stdio");
2758 } else {
2759 if (default_serial)
2760 add_device_config(DEV_SERIAL, "stdio");
2761 if (default_virtcon)
2762 add_device_config(DEV_VIRTCON, "stdio");
2763 if (default_monitor)
2764 monitor_parse("stdio", "readline");
2766 } else {
2767 if (default_serial)
2768 add_device_config(DEV_SERIAL, "vc:80Cx24C");
2769 if (default_parallel)
2770 add_device_config(DEV_PARALLEL, "vc:80Cx24C");
2771 if (default_monitor)
2772 monitor_parse("vc:80Cx24C", "readline");
2773 if (default_virtcon)
2774 add_device_config(DEV_VIRTCON, "vc:80Cx24C");
2776 if (default_vga)
2777 vga_interface_type = VGA_CIRRUS;
2779 socket_init();
2781 if (qemu_opts_foreach(qemu_find_opts("chardev"), chardev_init_func, NULL, 1) != 0)
2782 exit(1);
2783 #ifdef CONFIG_VIRTFS
2784 if (qemu_opts_foreach(qemu_find_opts("fsdev"), fsdev_init_func, NULL, 1) != 0) {
2785 exit(1);
2787 #endif
2789 os_daemonize();
2791 if (pid_file && qemu_create_pidfile(pid_file) != 0) {
2792 os_pidfile_error();
2793 exit(1);
2796 if (kvm_allowed) {
2797 int ret = kvm_init(smp_cpus);
2798 if (ret < 0) {
2799 #if defined(OBSOLETE_KVM_IMPL) || defined(CONFIG_NO_CPU_EMULATION)
2800 if (!kvm_available()) {
2801 printf("KVM not supported for this target\n");
2802 } else {
2803 fprintf(stderr, "failed to initialize KVM: %s\n", strerror(-ret));
2805 exit(1);
2806 #endif
2807 #ifdef CONFIG_KVM
2808 fprintf(stderr, "Could not initialize KVM, will disable KVM support\n");
2809 kvm_allowed = 0;
2810 #endif
2814 if (qemu_init_main_loop()) {
2815 fprintf(stderr, "qemu_init_main_loop failed\n");
2816 exit(1);
2818 linux_boot = (kernel_filename != NULL);
2820 if (!linux_boot && *kernel_cmdline != '\0') {
2821 fprintf(stderr, "-append only allowed with -kernel option\n");
2822 exit(1);
2825 if (!linux_boot && initrd_filename != NULL) {
2826 fprintf(stderr, "-initrd only allowed with -kernel option\n");
2827 exit(1);
2830 os_set_line_buffering();
2832 if (init_timer_alarm() < 0) {
2833 fprintf(stderr, "could not initialize alarm timer\n");
2834 exit(1);
2836 configure_icount(icount_option);
2838 if (net_init_clients() < 0) {
2839 exit(1);
2842 /* init the bluetooth world */
2843 if (foreach_device_config(DEV_BT, bt_parse))
2844 exit(1);
2846 /* init the memory */
2847 if (ram_size == 0)
2848 ram_size = DEFAULT_RAM_SIZE * 1024 * 1024;
2850 /* init the dynamic translator */
2851 cpu_exec_init_all(tb_size * 1024 * 1024);
2853 bdrv_init_with_whitelist();
2855 blk_mig_init();
2857 if (default_cdrom) {
2858 /* we always create the cdrom drive, even if no disk is there */
2859 drive_add(NULL, CDROM_ALIAS);
2862 if (default_floppy) {
2863 /* we always create at least one floppy */
2864 drive_add(NULL, FD_ALIAS, 0);
2867 if (default_sdcard) {
2868 /* we always create one sd slot, even if no card is in it */
2869 drive_add(NULL, SD_ALIAS);
2872 /* open the virtual block devices */
2873 if (snapshot)
2874 qemu_opts_foreach(qemu_find_opts("drive"), drive_enable_snapshot, NULL, 0);
2875 if (qemu_opts_foreach(qemu_find_opts("drive"), drive_init_func, &machine->use_scsi, 1) != 0)
2876 exit(1);
2878 register_savevm_live(NULL, "ram", 0, 4, NULL, ram_save_live, NULL,
2879 ram_load, NULL);
2881 if (nb_numa_nodes > 0) {
2882 int i;
2884 if (nb_numa_nodes > smp_cpus) {
2885 nb_numa_nodes = smp_cpus;
2888 /* If no memory size if given for any node, assume the default case
2889 * and distribute the available memory equally across all nodes
2891 for (i = 0; i < nb_numa_nodes; i++) {
2892 if (node_mem[i] != 0)
2893 break;
2895 if (i == nb_numa_nodes) {
2896 uint64_t usedmem = 0;
2898 /* On Linux, the each node's border has to be 8MB aligned,
2899 * the final node gets the rest.
2901 for (i = 0; i < nb_numa_nodes - 1; i++) {
2902 node_mem[i] = (ram_size / nb_numa_nodes) & ~((1 << 23UL) - 1);
2903 usedmem += node_mem[i];
2905 node_mem[i] = ram_size - usedmem;
2908 for (i = 0; i < nb_numa_nodes; i++) {
2909 if (node_cpumask[i] != 0)
2910 break;
2912 /* assigning the VCPUs round-robin is easier to implement, guest OSes
2913 * must cope with this anyway, because there are BIOSes out there in
2914 * real machines which also use this scheme.
2916 if (i == nb_numa_nodes) {
2917 for (i = 0; i < smp_cpus; i++) {
2918 node_cpumask[i % nb_numa_nodes] |= 1 << i;
2923 if (qemu_opts_foreach(qemu_find_opts("mon"), mon_init_func, NULL, 1) != 0) {
2924 exit(1);
2927 if (foreach_device_config(DEV_SERIAL, serial_parse) < 0)
2928 exit(1);
2929 if (foreach_device_config(DEV_PARALLEL, parallel_parse) < 0)
2930 exit(1);
2931 if (foreach_device_config(DEV_VIRTCON, virtcon_parse) < 0)
2932 exit(1);
2933 if (foreach_device_config(DEV_DEBUGCON, debugcon_parse) < 0)
2934 exit(1);
2936 module_call_init(MODULE_INIT_DEVICE);
2938 if (qemu_opts_foreach(qemu_find_opts("device"), device_help_func, NULL, 0) != 0)
2939 exit(0);
2941 if (watchdog) {
2942 i = select_watchdog(watchdog);
2943 if (i > 0)
2944 exit (i == 1 ? 1 : 0);
2947 if (machine->compat_props) {
2948 qdev_prop_register_global_list(machine->compat_props);
2950 qemu_add_globals();
2952 machine->init(ram_size, boot_devices,
2953 kernel_filename, kernel_cmdline, initrd_filename, cpu_model);
2955 cpu_synchronize_all_post_init();
2957 /* must be after terminal init, SDL library changes signal handlers */
2958 os_setup_signal_handling();
2960 set_numa_modes();
2962 current_machine = machine;
2964 /* init USB devices */
2965 if (usb_enabled) {
2966 if (foreach_device_config(DEV_USB, usb_parse) < 0)
2967 exit(1);
2970 /* init generic devices */
2971 if (qemu_opts_foreach(qemu_find_opts("device"), device_init_func, NULL, 1) != 0)
2972 exit(1);
2974 net_check_clients();
2976 /* just use the first displaystate for the moment */
2977 ds = get_displaystate();
2979 if (using_spice)
2980 display_remote++;
2981 if (display_type == DT_DEFAULT && !display_remote) {
2982 #if defined(CONFIG_SDL) || defined(CONFIG_COCOA)
2983 display_type = DT_SDL;
2984 #else
2985 vnc_display = "localhost:0,to=99";
2986 show_vnc_port = 1;
2987 #endif
2991 /* init local displays */
2992 switch (display_type) {
2993 case DT_NOGRAPHIC:
2994 break;
2995 #if defined(CONFIG_CURSES)
2996 case DT_CURSES:
2997 curses_display_init(ds, full_screen);
2998 break;
2999 #endif
3000 #if defined(CONFIG_SDL)
3001 case DT_SDL:
3002 sdl_display_init(ds, full_screen, no_frame);
3003 break;
3004 #elif defined(CONFIG_COCOA)
3005 case DT_SDL:
3006 cocoa_display_init(ds, full_screen);
3007 break;
3008 #endif
3009 default:
3010 break;
3013 /* init remote displays */
3014 if (vnc_display) {
3015 vnc_display_init(ds);
3016 if (vnc_display_open(ds, vnc_display) < 0)
3017 exit(1);
3019 if (show_vnc_port) {
3020 printf("VNC server running on `%s'\n", vnc_display_local_addr(ds));
3023 #ifdef CONFIG_SPICE
3024 if (using_spice) {
3025 qemu_spice_display_init(ds);
3027 #endif
3029 /* display setup */
3030 dpy_resize(ds);
3031 dcl = ds->listeners;
3032 while (dcl != NULL) {
3033 if (dcl->dpy_refresh != NULL) {
3034 ds->gui_timer = qemu_new_timer(rt_clock, gui_update, ds);
3035 qemu_mod_timer(ds->gui_timer, qemu_get_clock(rt_clock));
3036 break;
3038 dcl = dcl->next;
3040 if (ds->gui_timer == NULL) {
3041 nographic_timer = qemu_new_timer(rt_clock, nographic_update, NULL);
3042 qemu_mod_timer(nographic_timer, qemu_get_clock(rt_clock));
3044 text_consoles_set_display(ds);
3046 if (gdbstub_dev && gdbserver_start(gdbstub_dev) < 0) {
3047 fprintf(stderr, "qemu: could not open gdbserver on device '%s'\n",
3048 gdbstub_dev);
3049 exit(1);
3052 qdev_machine_creation_done();
3054 if (rom_load_all() != 0) {
3055 fprintf(stderr, "rom loading failed\n");
3056 exit(1);
3059 qemu_system_reset();
3060 if (loadvm) {
3061 if (load_vmstate(loadvm) < 0) {
3062 autostart = 0;
3066 if (incoming) {
3067 int ret = qemu_start_incoming_migration(incoming);
3068 if (ret < 0) {
3069 fprintf(stderr, "Migration failed. Exit code %s(%d), exiting.\n",
3070 incoming, ret);
3071 exit(ret);
3073 } else if (autostart) {
3074 vm_start();
3077 os_setup_post();
3079 main_loop();
3080 quit_timers();
3081 net_cleanup();
3083 return 0;