Add get_fw_dev_path callback for usb bus.
[qemu/mdroth.git] / vl.c
blob2cd263eda4cb2f48fcb54f99022f6d6b0ec635da
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 #ifdef CONFIG_VIRTFS
152 #include "fsdev/qemu-fsdev.h"
153 #endif
155 #include "disas.h"
157 #include "qemu_socket.h"
159 #include "slirp/libslirp.h"
161 #include "trace.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 old_param = 0;
225 const char *qemu_name;
226 int alt_grab = 0;
227 int ctrl_grab = 0;
228 unsigned int nb_prom_envs = 0;
229 const char *prom_envs[MAX_PROM_ENVS];
230 int boot_menu;
232 int nb_numa_nodes;
233 uint64_t node_mem[MAX_NODES];
234 uint64_t node_cpumask[MAX_NODES];
236 static QEMUTimer *nographic_timer;
238 uint8_t qemu_uuid[16];
240 static QEMUBootSetHandler *boot_set_handler;
241 static void *boot_set_opaque;
243 static NotifierList exit_notifiers =
244 NOTIFIER_LIST_INITIALIZER(exit_notifiers);
246 int kvm_allowed = 0;
247 uint32_t xen_domid;
248 enum xen_mode xen_mode = XEN_EMULATE;
250 static int default_serial = 1;
251 static int default_parallel = 1;
252 static int default_virtcon = 1;
253 static int default_monitor = 1;
254 static int default_vga = 1;
255 static int default_floppy = 1;
256 static int default_cdrom = 1;
257 static int default_sdcard = 1;
259 static struct {
260 const char *driver;
261 int *flag;
262 } default_list[] = {
263 { .driver = "isa-serial", .flag = &default_serial },
264 { .driver = "isa-parallel", .flag = &default_parallel },
265 { .driver = "isa-fdc", .flag = &default_floppy },
266 { .driver = "ide-drive", .flag = &default_cdrom },
267 { .driver = "virtio-serial-pci", .flag = &default_virtcon },
268 { .driver = "virtio-serial-s390", .flag = &default_virtcon },
269 { .driver = "virtio-serial", .flag = &default_virtcon },
270 { .driver = "VGA", .flag = &default_vga },
271 { .driver = "cirrus-vga", .flag = &default_vga },
272 { .driver = "vmware-svga", .flag = &default_vga },
275 static int default_driver_check(QemuOpts *opts, void *opaque)
277 const char *driver = qemu_opt_get(opts, "driver");
278 int i;
280 if (!driver)
281 return 0;
282 for (i = 0; i < ARRAY_SIZE(default_list); i++) {
283 if (strcmp(default_list[i].driver, driver) != 0)
284 continue;
285 *(default_list[i].flag) = 0;
287 return 0;
290 /***********************************************************/
291 /* real time host monotonic timer */
293 /***********************************************************/
294 /* host time/date access */
295 void qemu_get_timedate(struct tm *tm, int offset)
297 time_t ti;
298 struct tm *ret;
300 time(&ti);
301 ti += offset;
302 if (rtc_date_offset == -1) {
303 if (rtc_utc)
304 ret = gmtime(&ti);
305 else
306 ret = localtime(&ti);
307 } else {
308 ti -= rtc_date_offset;
309 ret = gmtime(&ti);
312 memcpy(tm, ret, sizeof(struct tm));
315 int qemu_timedate_diff(struct tm *tm)
317 time_t seconds;
319 if (rtc_date_offset == -1)
320 if (rtc_utc)
321 seconds = mktimegm(tm);
322 else
323 seconds = mktime(tm);
324 else
325 seconds = mktimegm(tm) + rtc_date_offset;
327 return seconds - time(NULL);
330 void rtc_change_mon_event(struct tm *tm)
332 QObject *data;
334 data = qobject_from_jsonf("{ 'offset': %d }", qemu_timedate_diff(tm));
335 monitor_protocol_event(QEVENT_RTC_CHANGE, data);
336 qobject_decref(data);
339 static void configure_rtc_date_offset(const char *startdate, int legacy)
341 time_t rtc_start_date;
342 struct tm tm;
344 if (!strcmp(startdate, "now") && legacy) {
345 rtc_date_offset = -1;
346 } else {
347 if (sscanf(startdate, "%d-%d-%dT%d:%d:%d",
348 &tm.tm_year,
349 &tm.tm_mon,
350 &tm.tm_mday,
351 &tm.tm_hour,
352 &tm.tm_min,
353 &tm.tm_sec) == 6) {
354 /* OK */
355 } else if (sscanf(startdate, "%d-%d-%d",
356 &tm.tm_year,
357 &tm.tm_mon,
358 &tm.tm_mday) == 3) {
359 tm.tm_hour = 0;
360 tm.tm_min = 0;
361 tm.tm_sec = 0;
362 } else {
363 goto date_fail;
365 tm.tm_year -= 1900;
366 tm.tm_mon--;
367 rtc_start_date = mktimegm(&tm);
368 if (rtc_start_date == -1) {
369 date_fail:
370 fprintf(stderr, "Invalid date format. Valid formats are:\n"
371 "'2006-06-17T16:01:21' or '2006-06-17'\n");
372 exit(1);
374 rtc_date_offset = time(NULL) - rtc_start_date;
378 static void configure_rtc(QemuOpts *opts)
380 const char *value;
382 value = qemu_opt_get(opts, "base");
383 if (value) {
384 if (!strcmp(value, "utc")) {
385 rtc_utc = 1;
386 } else if (!strcmp(value, "localtime")) {
387 rtc_utc = 0;
388 } else {
389 configure_rtc_date_offset(value, 0);
392 value = qemu_opt_get(opts, "clock");
393 if (value) {
394 if (!strcmp(value, "host")) {
395 rtc_clock = host_clock;
396 } else if (!strcmp(value, "vm")) {
397 rtc_clock = vm_clock;
398 } else {
399 fprintf(stderr, "qemu: invalid option value '%s'\n", value);
400 exit(1);
403 value = qemu_opt_get(opts, "driftfix");
404 if (value) {
405 if (!strcmp(value, "slew")) {
406 rtc_td_hack = 1;
407 } else if (!strcmp(value, "none")) {
408 rtc_td_hack = 0;
409 } else {
410 fprintf(stderr, "qemu: invalid option value '%s'\n", value);
411 exit(1);
416 /***********************************************************/
417 /* Bluetooth support */
418 static int nb_hcis;
419 static int cur_hci;
420 static struct HCIInfo *hci_table[MAX_NICS];
422 static struct bt_vlan_s {
423 struct bt_scatternet_s net;
424 int id;
425 struct bt_vlan_s *next;
426 } *first_bt_vlan;
428 /* find or alloc a new bluetooth "VLAN" */
429 static struct bt_scatternet_s *qemu_find_bt_vlan(int id)
431 struct bt_vlan_s **pvlan, *vlan;
432 for (vlan = first_bt_vlan; vlan != NULL; vlan = vlan->next) {
433 if (vlan->id == id)
434 return &vlan->net;
436 vlan = qemu_mallocz(sizeof(struct bt_vlan_s));
437 vlan->id = id;
438 pvlan = &first_bt_vlan;
439 while (*pvlan != NULL)
440 pvlan = &(*pvlan)->next;
441 *pvlan = vlan;
442 return &vlan->net;
445 static void null_hci_send(struct HCIInfo *hci, const uint8_t *data, int len)
449 static int null_hci_addr_set(struct HCIInfo *hci, const uint8_t *bd_addr)
451 return -ENOTSUP;
454 static struct HCIInfo null_hci = {
455 .cmd_send = null_hci_send,
456 .sco_send = null_hci_send,
457 .acl_send = null_hci_send,
458 .bdaddr_set = null_hci_addr_set,
461 struct HCIInfo *qemu_next_hci(void)
463 if (cur_hci == nb_hcis)
464 return &null_hci;
466 return hci_table[cur_hci++];
469 static struct HCIInfo *hci_init(const char *str)
471 char *endp;
472 struct bt_scatternet_s *vlan = 0;
474 if (!strcmp(str, "null"))
475 /* null */
476 return &null_hci;
477 else if (!strncmp(str, "host", 4) && (str[4] == '\0' || str[4] == ':'))
478 /* host[:hciN] */
479 return bt_host_hci(str[4] ? str + 5 : "hci0");
480 else if (!strncmp(str, "hci", 3)) {
481 /* hci[,vlan=n] */
482 if (str[3]) {
483 if (!strncmp(str + 3, ",vlan=", 6)) {
484 vlan = qemu_find_bt_vlan(strtol(str + 9, &endp, 0));
485 if (*endp)
486 vlan = 0;
488 } else
489 vlan = qemu_find_bt_vlan(0);
490 if (vlan)
491 return bt_new_hci(vlan);
494 fprintf(stderr, "qemu: Unknown bluetooth HCI `%s'.\n", str);
496 return 0;
499 static int bt_hci_parse(const char *str)
501 struct HCIInfo *hci;
502 bdaddr_t bdaddr;
504 if (nb_hcis >= MAX_NICS) {
505 fprintf(stderr, "qemu: Too many bluetooth HCIs (max %i).\n", MAX_NICS);
506 return -1;
509 hci = hci_init(str);
510 if (!hci)
511 return -1;
513 bdaddr.b[0] = 0x52;
514 bdaddr.b[1] = 0x54;
515 bdaddr.b[2] = 0x00;
516 bdaddr.b[3] = 0x12;
517 bdaddr.b[4] = 0x34;
518 bdaddr.b[5] = 0x56 + nb_hcis;
519 hci->bdaddr_set(hci, bdaddr.b);
521 hci_table[nb_hcis++] = hci;
523 return 0;
526 static void bt_vhci_add(int vlan_id)
528 struct bt_scatternet_s *vlan = qemu_find_bt_vlan(vlan_id);
530 if (!vlan->slave)
531 fprintf(stderr, "qemu: warning: adding a VHCI to "
532 "an empty scatternet %i\n", vlan_id);
534 bt_vhci_init(bt_new_hci(vlan));
537 static struct bt_device_s *bt_device_add(const char *opt)
539 struct bt_scatternet_s *vlan;
540 int vlan_id = 0;
541 char *endp = strstr(opt, ",vlan=");
542 int len = (endp ? endp - opt : strlen(opt)) + 1;
543 char devname[10];
545 pstrcpy(devname, MIN(sizeof(devname), len), opt);
547 if (endp) {
548 vlan_id = strtol(endp + 6, &endp, 0);
549 if (*endp) {
550 fprintf(stderr, "qemu: unrecognised bluetooth vlan Id\n");
551 return 0;
555 vlan = qemu_find_bt_vlan(vlan_id);
557 if (!vlan->slave)
558 fprintf(stderr, "qemu: warning: adding a slave device to "
559 "an empty scatternet %i\n", vlan_id);
561 if (!strcmp(devname, "keyboard"))
562 return bt_keyboard_init(vlan);
564 fprintf(stderr, "qemu: unsupported bluetooth device `%s'\n", devname);
565 return 0;
568 static int bt_parse(const char *opt)
570 const char *endp, *p;
571 int vlan;
573 if (strstart(opt, "hci", &endp)) {
574 if (!*endp || *endp == ',') {
575 if (*endp)
576 if (!strstart(endp, ",vlan=", 0))
577 opt = endp + 1;
579 return bt_hci_parse(opt);
581 } else if (strstart(opt, "vhci", &endp)) {
582 if (!*endp || *endp == ',') {
583 if (*endp) {
584 if (strstart(endp, ",vlan=", &p)) {
585 vlan = strtol(p, (char **) &endp, 0);
586 if (*endp) {
587 fprintf(stderr, "qemu: bad scatternet '%s'\n", p);
588 return 1;
590 } else {
591 fprintf(stderr, "qemu: bad parameter '%s'\n", endp + 1);
592 return 1;
594 } else
595 vlan = 0;
597 bt_vhci_add(vlan);
598 return 0;
600 } else if (strstart(opt, "device:", &endp))
601 return !bt_device_add(endp);
603 fprintf(stderr, "qemu: bad bluetooth parameter '%s'\n", opt);
604 return 1;
607 /***********************************************************/
608 /* QEMU Block devices */
610 #define HD_ALIAS "index=%d,media=disk"
611 #define CDROM_ALIAS "index=2,media=cdrom"
612 #define FD_ALIAS "index=%d,if=floppy"
613 #define PFLASH_ALIAS "if=pflash"
614 #define MTD_ALIAS "if=mtd"
615 #define SD_ALIAS "index=0,if=sd"
617 static int drive_init_func(QemuOpts *opts, void *opaque)
619 int *use_scsi = opaque;
620 int fatal_error = 0;
622 if (drive_init(opts, *use_scsi, &fatal_error) == NULL) {
623 if (fatal_error)
624 return 1;
626 return 0;
629 static int drive_enable_snapshot(QemuOpts *opts, void *opaque)
631 if (NULL == qemu_opt_get(opts, "snapshot")) {
632 qemu_opt_set(opts, "snapshot", "on");
634 return 0;
637 void qemu_register_boot_set(QEMUBootSetHandler *func, void *opaque)
639 boot_set_handler = func;
640 boot_set_opaque = opaque;
643 int qemu_boot_set(const char *boot_devices)
645 if (!boot_set_handler) {
646 return -EINVAL;
648 return boot_set_handler(boot_set_opaque, boot_devices);
651 static void validate_bootdevices(char *devices)
653 /* We just do some generic consistency checks */
654 const char *p;
655 int bitmap = 0;
657 for (p = devices; *p != '\0'; p++) {
658 /* Allowed boot devices are:
659 * a-b: floppy disk drives
660 * c-f: IDE disk drives
661 * g-m: machine implementation dependant drives
662 * n-p: network devices
663 * It's up to each machine implementation to check if the given boot
664 * devices match the actual hardware implementation and firmware
665 * features.
667 if (*p < 'a' || *p > 'p') {
668 fprintf(stderr, "Invalid boot device '%c'\n", *p);
669 exit(1);
671 if (bitmap & (1 << (*p - 'a'))) {
672 fprintf(stderr, "Boot device '%c' was given twice\n", *p);
673 exit(1);
675 bitmap |= 1 << (*p - 'a');
679 static void restore_boot_devices(void *opaque)
681 char *standard_boot_devices = opaque;
682 static int first = 1;
684 /* Restore boot order and remove ourselves after the first boot */
685 if (first) {
686 first = 0;
687 return;
690 qemu_boot_set(standard_boot_devices);
692 qemu_unregister_reset(restore_boot_devices, standard_boot_devices);
693 qemu_free(standard_boot_devices);
696 static void numa_add(const char *optarg)
698 char option[128];
699 char *endptr;
700 unsigned long long value, endvalue;
701 int nodenr;
703 optarg = get_opt_name(option, 128, optarg, ',') + 1;
704 if (!strcmp(option, "node")) {
705 if (get_param_value(option, 128, "nodeid", optarg) == 0) {
706 nodenr = nb_numa_nodes;
707 } else {
708 nodenr = strtoull(option, NULL, 10);
711 if (get_param_value(option, 128, "mem", optarg) == 0) {
712 node_mem[nodenr] = 0;
713 } else {
714 ssize_t sval;
715 sval = strtosz(option, NULL);
716 if (sval < 0) {
717 fprintf(stderr, "qemu: invalid numa mem size: %s\n", optarg);
718 exit(1);
720 node_mem[nodenr] = sval;
722 if (get_param_value(option, 128, "cpus", optarg) == 0) {
723 node_cpumask[nodenr] = 0;
724 } else {
725 value = strtoull(option, &endptr, 10);
726 if (value >= 64) {
727 value = 63;
728 fprintf(stderr, "only 64 CPUs in NUMA mode supported.\n");
729 } else {
730 if (*endptr == '-') {
731 endvalue = strtoull(endptr+1, &endptr, 10);
732 if (endvalue >= 63) {
733 endvalue = 62;
734 fprintf(stderr,
735 "only 63 CPUs in NUMA mode supported.\n");
737 value = (2ULL << endvalue) - (1ULL << value);
738 } else {
739 value = 1ULL << value;
742 node_cpumask[nodenr] = value;
744 nb_numa_nodes++;
746 return;
749 static void smp_parse(const char *optarg)
751 int smp, sockets = 0, threads = 0, cores = 0;
752 char *endptr;
753 char option[128];
755 smp = strtoul(optarg, &endptr, 10);
756 if (endptr != optarg) {
757 if (*endptr == ',') {
758 endptr++;
761 if (get_param_value(option, 128, "sockets", endptr) != 0)
762 sockets = strtoull(option, NULL, 10);
763 if (get_param_value(option, 128, "cores", endptr) != 0)
764 cores = strtoull(option, NULL, 10);
765 if (get_param_value(option, 128, "threads", endptr) != 0)
766 threads = strtoull(option, NULL, 10);
767 if (get_param_value(option, 128, "maxcpus", endptr) != 0)
768 max_cpus = strtoull(option, NULL, 10);
770 /* compute missing values, prefer sockets over cores over threads */
771 if (smp == 0 || sockets == 0) {
772 sockets = sockets > 0 ? sockets : 1;
773 cores = cores > 0 ? cores : 1;
774 threads = threads > 0 ? threads : 1;
775 if (smp == 0) {
776 smp = cores * threads * sockets;
778 } else {
779 if (cores == 0) {
780 threads = threads > 0 ? threads : 1;
781 cores = smp / (sockets * threads);
782 } else {
783 threads = smp / (cores * sockets);
786 smp_cpus = smp;
787 smp_cores = cores > 0 ? cores : 1;
788 smp_threads = threads > 0 ? threads : 1;
789 if (max_cpus == 0)
790 max_cpus = smp_cpus;
793 /***********************************************************/
794 /* USB devices */
796 static int usb_device_add(const char *devname)
798 const char *p;
799 USBDevice *dev = NULL;
801 if (!usb_enabled)
802 return -1;
804 /* drivers with .usbdevice_name entry in USBDeviceInfo */
805 dev = usbdevice_create(devname);
806 if (dev)
807 goto done;
809 /* the other ones */
810 if (strstart(devname, "host:", &p)) {
811 dev = usb_host_device_open(p);
812 } else if (!strcmp(devname, "bt") || strstart(devname, "bt:", &p)) {
813 dev = usb_bt_init(devname[2] ? hci_init(p) :
814 bt_new_hci(qemu_find_bt_vlan(0)));
815 } else {
816 return -1;
818 if (!dev)
819 return -1;
821 done:
822 return 0;
825 static int usb_device_del(const char *devname)
827 int bus_num, addr;
828 const char *p;
830 if (strstart(devname, "host:", &p))
831 return usb_host_device_close(p);
833 if (!usb_enabled)
834 return -1;
836 p = strchr(devname, '.');
837 if (!p)
838 return -1;
839 bus_num = strtoul(devname, NULL, 0);
840 addr = strtoul(p + 1, NULL, 0);
842 return usb_device_delete_addr(bus_num, addr);
845 static int usb_parse(const char *cmdline)
847 int r;
848 r = usb_device_add(cmdline);
849 if (r < 0) {
850 fprintf(stderr, "qemu: could not add USB device '%s'\n", cmdline);
852 return r;
855 void do_usb_add(Monitor *mon, const QDict *qdict)
857 const char *devname = qdict_get_str(qdict, "devname");
858 if (usb_device_add(devname) < 0) {
859 error_report("could not add USB device '%s'", devname);
863 void do_usb_del(Monitor *mon, const QDict *qdict)
865 const char *devname = qdict_get_str(qdict, "devname");
866 if (usb_device_del(devname) < 0) {
867 error_report("could not delete USB device '%s'", devname);
871 /***********************************************************/
872 /* PCMCIA/Cardbus */
874 static struct pcmcia_socket_entry_s {
875 PCMCIASocket *socket;
876 struct pcmcia_socket_entry_s *next;
877 } *pcmcia_sockets = 0;
879 void pcmcia_socket_register(PCMCIASocket *socket)
881 struct pcmcia_socket_entry_s *entry;
883 entry = qemu_malloc(sizeof(struct pcmcia_socket_entry_s));
884 entry->socket = socket;
885 entry->next = pcmcia_sockets;
886 pcmcia_sockets = entry;
889 void pcmcia_socket_unregister(PCMCIASocket *socket)
891 struct pcmcia_socket_entry_s *entry, **ptr;
893 ptr = &pcmcia_sockets;
894 for (entry = *ptr; entry; ptr = &entry->next, entry = *ptr)
895 if (entry->socket == socket) {
896 *ptr = entry->next;
897 qemu_free(entry);
901 void pcmcia_info(Monitor *mon)
903 struct pcmcia_socket_entry_s *iter;
905 if (!pcmcia_sockets)
906 monitor_printf(mon, "No PCMCIA sockets\n");
908 for (iter = pcmcia_sockets; iter; iter = iter->next)
909 monitor_printf(mon, "%s: %s\n", iter->socket->slot_string,
910 iter->socket->attached ? iter->socket->card_string :
911 "Empty");
914 /***********************************************************/
915 /* I/O handling */
917 typedef struct IOHandlerRecord {
918 int fd;
919 IOCanReadHandler *fd_read_poll;
920 IOHandler *fd_read;
921 IOHandler *fd_write;
922 int deleted;
923 void *opaque;
924 /* temporary data */
925 struct pollfd *ufd;
926 QLIST_ENTRY(IOHandlerRecord) next;
927 } IOHandlerRecord;
929 static QLIST_HEAD(, IOHandlerRecord) io_handlers =
930 QLIST_HEAD_INITIALIZER(io_handlers);
933 /* XXX: fd_read_poll should be suppressed, but an API change is
934 necessary in the character devices to suppress fd_can_read(). */
935 int qemu_set_fd_handler2(int fd,
936 IOCanReadHandler *fd_read_poll,
937 IOHandler *fd_read,
938 IOHandler *fd_write,
939 void *opaque)
941 IOHandlerRecord *ioh;
943 if (!fd_read && !fd_write) {
944 QLIST_FOREACH(ioh, &io_handlers, next) {
945 if (ioh->fd == fd) {
946 ioh->deleted = 1;
947 break;
950 } else {
951 QLIST_FOREACH(ioh, &io_handlers, next) {
952 if (ioh->fd == fd)
953 goto found;
955 ioh = qemu_mallocz(sizeof(IOHandlerRecord));
956 QLIST_INSERT_HEAD(&io_handlers, ioh, next);
957 found:
958 ioh->fd = fd;
959 ioh->fd_read_poll = fd_read_poll;
960 ioh->fd_read = fd_read;
961 ioh->fd_write = fd_write;
962 ioh->opaque = opaque;
963 ioh->deleted = 0;
965 return 0;
968 int qemu_set_fd_handler(int fd,
969 IOHandler *fd_read,
970 IOHandler *fd_write,
971 void *opaque)
973 return qemu_set_fd_handler2(fd, NULL, fd_read, fd_write, opaque);
976 /***********************************************************/
977 /* machine registration */
979 static QEMUMachine *first_machine = NULL;
980 QEMUMachine *current_machine = NULL;
982 int qemu_register_machine(QEMUMachine *m)
984 QEMUMachine **pm;
985 pm = &first_machine;
986 while (*pm != NULL)
987 pm = &(*pm)->next;
988 m->next = NULL;
989 *pm = m;
990 return 0;
993 static QEMUMachine *find_machine(const char *name)
995 QEMUMachine *m;
997 for(m = first_machine; m != NULL; m = m->next) {
998 if (!strcmp(m->name, name))
999 return m;
1000 if (m->alias && !strcmp(m->alias, name))
1001 return m;
1003 return NULL;
1006 static QEMUMachine *find_default_machine(void)
1008 QEMUMachine *m;
1010 for(m = first_machine; m != NULL; m = m->next) {
1011 if (m->is_default) {
1012 return m;
1015 return NULL;
1018 /***********************************************************/
1019 /* main execution loop */
1021 static void gui_update(void *opaque)
1023 uint64_t interval = GUI_REFRESH_INTERVAL;
1024 DisplayState *ds = opaque;
1025 DisplayChangeListener *dcl = ds->listeners;
1027 qemu_flush_coalesced_mmio_buffer();
1028 dpy_refresh(ds);
1030 while (dcl != NULL) {
1031 if (dcl->gui_timer_interval &&
1032 dcl->gui_timer_interval < interval)
1033 interval = dcl->gui_timer_interval;
1034 dcl = dcl->next;
1036 qemu_mod_timer(ds->gui_timer, interval + qemu_get_clock(rt_clock));
1039 static void nographic_update(void *opaque)
1041 uint64_t interval = GUI_REFRESH_INTERVAL;
1043 qemu_flush_coalesced_mmio_buffer();
1044 qemu_mod_timer(nographic_timer, interval + qemu_get_clock(rt_clock));
1047 struct vm_change_state_entry {
1048 VMChangeStateHandler *cb;
1049 void *opaque;
1050 QLIST_ENTRY (vm_change_state_entry) entries;
1053 static QLIST_HEAD(vm_change_state_head, vm_change_state_entry) vm_change_state_head;
1055 VMChangeStateEntry *qemu_add_vm_change_state_handler(VMChangeStateHandler *cb,
1056 void *opaque)
1058 VMChangeStateEntry *e;
1060 e = qemu_mallocz(sizeof (*e));
1062 e->cb = cb;
1063 e->opaque = opaque;
1064 QLIST_INSERT_HEAD(&vm_change_state_head, e, entries);
1065 return e;
1068 void qemu_del_vm_change_state_handler(VMChangeStateEntry *e)
1070 QLIST_REMOVE (e, entries);
1071 qemu_free (e);
1074 void vm_state_notify(int running, int reason)
1076 VMChangeStateEntry *e;
1078 trace_vm_state_notify(running, reason);
1080 for (e = vm_change_state_head.lh_first; e; e = e->entries.le_next) {
1081 e->cb(e->opaque, running, reason);
1085 void vm_start(void)
1087 if (!vm_running) {
1088 cpu_enable_ticks();
1089 vm_running = 1;
1090 vm_state_notify(1, 0);
1091 resume_all_vcpus();
1092 monitor_protocol_event(QEVENT_RESUME, NULL);
1096 /* reset/shutdown handler */
1098 typedef struct QEMUResetEntry {
1099 QTAILQ_ENTRY(QEMUResetEntry) entry;
1100 QEMUResetHandler *func;
1101 void *opaque;
1102 } QEMUResetEntry;
1104 static QTAILQ_HEAD(reset_handlers, QEMUResetEntry) reset_handlers =
1105 QTAILQ_HEAD_INITIALIZER(reset_handlers);
1106 static int reset_requested;
1107 static int shutdown_requested;
1108 static int powerdown_requested;
1109 int debug_requested;
1110 int vmstop_requested;
1112 int qemu_shutdown_requested(void)
1114 int r = shutdown_requested;
1115 shutdown_requested = 0;
1116 return r;
1119 int qemu_reset_requested(void)
1121 int r = reset_requested;
1122 reset_requested = 0;
1123 return r;
1126 int qemu_powerdown_requested(void)
1128 int r = powerdown_requested;
1129 powerdown_requested = 0;
1130 return r;
1133 static int qemu_debug_requested(void)
1135 int r = debug_requested;
1136 debug_requested = 0;
1137 return r;
1140 static int qemu_vmstop_requested(void)
1142 int r = vmstop_requested;
1143 vmstop_requested = 0;
1144 return r;
1147 void qemu_register_reset(QEMUResetHandler *func, void *opaque)
1149 QEMUResetEntry *re = qemu_mallocz(sizeof(QEMUResetEntry));
1151 re->func = func;
1152 re->opaque = opaque;
1153 QTAILQ_INSERT_TAIL(&reset_handlers, re, entry);
1156 void qemu_unregister_reset(QEMUResetHandler *func, void *opaque)
1158 QEMUResetEntry *re;
1160 QTAILQ_FOREACH(re, &reset_handlers, entry) {
1161 if (re->func == func && re->opaque == opaque) {
1162 QTAILQ_REMOVE(&reset_handlers, re, entry);
1163 qemu_free(re);
1164 return;
1169 void qemu_system_reset(void)
1171 QEMUResetEntry *re, *nre;
1173 /* reset all devices */
1174 QTAILQ_FOREACH_SAFE(re, &reset_handlers, entry, nre) {
1175 re->func(re->opaque);
1177 monitor_protocol_event(QEVENT_RESET, NULL);
1178 cpu_synchronize_all_post_reset();
1181 void qemu_system_reset_request(void)
1183 if (no_reboot) {
1184 shutdown_requested = 1;
1185 } else {
1186 reset_requested = 1;
1188 qemu_notify_event();
1191 void qemu_system_shutdown_request(void)
1193 shutdown_requested = 1;
1194 qemu_notify_event();
1197 void qemu_system_powerdown_request(void)
1199 powerdown_requested = 1;
1200 qemu_notify_event();
1203 void main_loop_wait(int nonblocking)
1205 IOHandlerRecord *ioh;
1206 fd_set rfds, wfds, xfds;
1207 int ret, nfds;
1208 struct timeval tv;
1209 int timeout;
1211 if (nonblocking)
1212 timeout = 0;
1213 else {
1214 timeout = qemu_calculate_timeout();
1215 qemu_bh_update_timeout(&timeout);
1218 os_host_main_loop_wait(&timeout);
1220 /* poll any events */
1221 /* XXX: separate device handlers from system ones */
1222 nfds = -1;
1223 FD_ZERO(&rfds);
1224 FD_ZERO(&wfds);
1225 FD_ZERO(&xfds);
1226 QLIST_FOREACH(ioh, &io_handlers, next) {
1227 if (ioh->deleted)
1228 continue;
1229 if (ioh->fd_read &&
1230 (!ioh->fd_read_poll ||
1231 ioh->fd_read_poll(ioh->opaque) != 0)) {
1232 FD_SET(ioh->fd, &rfds);
1233 if (ioh->fd > nfds)
1234 nfds = ioh->fd;
1236 if (ioh->fd_write) {
1237 FD_SET(ioh->fd, &wfds);
1238 if (ioh->fd > nfds)
1239 nfds = ioh->fd;
1243 tv.tv_sec = timeout / 1000;
1244 tv.tv_usec = (timeout % 1000) * 1000;
1246 slirp_select_fill(&nfds, &rfds, &wfds, &xfds);
1248 qemu_mutex_unlock_iothread();
1249 ret = select(nfds + 1, &rfds, &wfds, &xfds, &tv);
1250 qemu_mutex_lock_iothread();
1251 if (ret > 0) {
1252 IOHandlerRecord *pioh;
1254 QLIST_FOREACH_SAFE(ioh, &io_handlers, next, pioh) {
1255 if (!ioh->deleted && ioh->fd_read && FD_ISSET(ioh->fd, &rfds)) {
1256 ioh->fd_read(ioh->opaque);
1258 if (!ioh->deleted && ioh->fd_write && FD_ISSET(ioh->fd, &wfds)) {
1259 ioh->fd_write(ioh->opaque);
1262 /* Do this last in case read/write handlers marked it for deletion */
1263 if (ioh->deleted) {
1264 QLIST_REMOVE(ioh, next);
1265 qemu_free(ioh);
1270 slirp_select_poll(&rfds, &wfds, &xfds, (ret < 0));
1272 qemu_run_all_timers();
1274 /* Check bottom-halves last in case any of the earlier events triggered
1275 them. */
1276 qemu_bh_poll();
1280 static int vm_can_run(void)
1282 if (powerdown_requested)
1283 return 0;
1284 if (reset_requested)
1285 return 0;
1286 if (shutdown_requested)
1287 return 0;
1288 if (debug_requested)
1289 return 0;
1290 return 1;
1293 qemu_irq qemu_system_powerdown;
1295 static void main_loop(void)
1297 int r;
1299 qemu_main_loop_start();
1301 for (;;) {
1302 do {
1303 bool nonblocking = false;
1304 #ifdef CONFIG_PROFILER
1305 int64_t ti;
1306 #endif
1307 #ifndef CONFIG_IOTHREAD
1308 nonblocking = cpu_exec_all();
1309 #endif
1310 #ifdef CONFIG_PROFILER
1311 ti = profile_getclock();
1312 #endif
1313 main_loop_wait(nonblocking);
1314 #ifdef CONFIG_PROFILER
1315 dev_time += profile_getclock() - ti;
1316 #endif
1317 } while (vm_can_run());
1319 if ((r = qemu_debug_requested())) {
1320 vm_stop(r);
1322 if (qemu_shutdown_requested()) {
1323 monitor_protocol_event(QEVENT_SHUTDOWN, NULL);
1324 if (no_shutdown) {
1325 vm_stop(0);
1326 no_shutdown = 0;
1327 } else
1328 break;
1330 if (qemu_reset_requested()) {
1331 pause_all_vcpus();
1332 qemu_system_reset();
1333 resume_all_vcpus();
1335 if (qemu_powerdown_requested()) {
1336 monitor_protocol_event(QEVENT_POWERDOWN, NULL);
1337 qemu_irq_raise(qemu_system_powerdown);
1339 if ((r = qemu_vmstop_requested())) {
1340 vm_stop(r);
1343 bdrv_close_all();
1344 pause_all_vcpus();
1347 static void version(void)
1349 printf("QEMU emulator version " QEMU_VERSION QEMU_PKGVERSION ", Copyright (c) 2003-2008 Fabrice Bellard\n");
1352 static void help(int exitcode)
1354 const char *options_help =
1355 #define DEF(option, opt_arg, opt_enum, opt_help, arch_mask) \
1356 opt_help
1357 #define DEFHEADING(text) stringify(text) "\n"
1358 #include "qemu-options.def"
1359 #undef DEF
1360 #undef DEFHEADING
1361 #undef GEN_DOCS
1363 version();
1364 printf("usage: %s [options] [disk_image]\n"
1365 "\n"
1366 "'disk_image' is a raw hard disk image for IDE hard disk 0\n"
1367 "\n"
1368 "%s\n"
1369 "During emulation, the following keys are useful:\n"
1370 "ctrl-alt-f toggle full screen\n"
1371 "ctrl-alt-n switch to virtual console 'n'\n"
1372 "ctrl-alt toggle mouse and keyboard grab\n"
1373 "\n"
1374 "When using -nographic, press 'ctrl-a h' to get some help.\n",
1375 "qemu",
1376 options_help);
1377 exit(exitcode);
1380 #define HAS_ARG 0x0001
1382 typedef struct QEMUOption {
1383 const char *name;
1384 int flags;
1385 int index;
1386 uint32_t arch_mask;
1387 } QEMUOption;
1389 static const QEMUOption qemu_options[] = {
1390 { "h", 0, QEMU_OPTION_h, QEMU_ARCH_ALL },
1391 #define DEF(option, opt_arg, opt_enum, opt_help, arch_mask) \
1392 { option, opt_arg, opt_enum, arch_mask },
1393 #define DEFHEADING(text)
1394 #include "qemu-options.def"
1395 #undef DEF
1396 #undef DEFHEADING
1397 #undef GEN_DOCS
1398 { NULL },
1400 static void select_vgahw (const char *p)
1402 const char *opts;
1404 default_vga = 0;
1405 vga_interface_type = VGA_NONE;
1406 if (strstart(p, "std", &opts)) {
1407 vga_interface_type = VGA_STD;
1408 } else if (strstart(p, "cirrus", &opts)) {
1409 vga_interface_type = VGA_CIRRUS;
1410 } else if (strstart(p, "vmware", &opts)) {
1411 vga_interface_type = VGA_VMWARE;
1412 } else if (strstart(p, "xenfb", &opts)) {
1413 vga_interface_type = VGA_XENFB;
1414 } else if (!strstart(p, "none", &opts)) {
1415 invalid_vga:
1416 fprintf(stderr, "Unknown vga type: %s\n", p);
1417 exit(1);
1419 while (*opts) {
1420 const char *nextopt;
1422 if (strstart(opts, ",retrace=", &nextopt)) {
1423 opts = nextopt;
1424 if (strstart(opts, "dumb", &nextopt))
1425 vga_retrace_method = VGA_RETRACE_DUMB;
1426 else if (strstart(opts, "precise", &nextopt))
1427 vga_retrace_method = VGA_RETRACE_PRECISE;
1428 else goto invalid_vga;
1429 } else goto invalid_vga;
1430 opts = nextopt;
1434 static int balloon_parse(const char *arg)
1436 QemuOpts *opts;
1438 if (strcmp(arg, "none") == 0) {
1439 return 0;
1442 if (!strncmp(arg, "virtio", 6)) {
1443 if (arg[6] == ',') {
1444 /* have params -> parse them */
1445 opts = qemu_opts_parse(qemu_find_opts("device"), arg+7, 0);
1446 if (!opts)
1447 return -1;
1448 } else {
1449 /* create empty opts */
1450 opts = qemu_opts_create(qemu_find_opts("device"), NULL, 0);
1452 qemu_opt_set(opts, "driver", "virtio-balloon-pci");
1453 return 0;
1456 return -1;
1459 char *qemu_find_file(int type, const char *name)
1461 int len;
1462 const char *subdir;
1463 char *buf;
1465 /* If name contains path separators then try it as a straight path. */
1466 if ((strchr(name, '/') || strchr(name, '\\'))
1467 && access(name, R_OK) == 0) {
1468 return qemu_strdup(name);
1470 switch (type) {
1471 case QEMU_FILE_TYPE_BIOS:
1472 subdir = "";
1473 break;
1474 case QEMU_FILE_TYPE_KEYMAP:
1475 subdir = "keymaps/";
1476 break;
1477 default:
1478 abort();
1480 len = strlen(data_dir) + strlen(name) + strlen(subdir) + 2;
1481 buf = qemu_mallocz(len);
1482 snprintf(buf, len, "%s/%s%s", data_dir, subdir, name);
1483 if (access(buf, R_OK)) {
1484 qemu_free(buf);
1485 return NULL;
1487 return buf;
1490 static int device_help_func(QemuOpts *opts, void *opaque)
1492 return qdev_device_help(opts);
1495 static int device_init_func(QemuOpts *opts, void *opaque)
1497 DeviceState *dev;
1499 dev = qdev_device_add(opts);
1500 if (!dev)
1501 return -1;
1502 return 0;
1505 static int chardev_init_func(QemuOpts *opts, void *opaque)
1507 CharDriverState *chr;
1509 chr = qemu_chr_open_opts(opts, NULL);
1510 if (!chr)
1511 return -1;
1512 return 0;
1515 #ifdef CONFIG_VIRTFS
1516 static int fsdev_init_func(QemuOpts *opts, void *opaque)
1518 int ret;
1519 ret = qemu_fsdev_add(opts);
1521 return ret;
1523 #endif
1525 static int mon_init_func(QemuOpts *opts, void *opaque)
1527 CharDriverState *chr;
1528 const char *chardev;
1529 const char *mode;
1530 int flags;
1532 mode = qemu_opt_get(opts, "mode");
1533 if (mode == NULL) {
1534 mode = "readline";
1536 if (strcmp(mode, "readline") == 0) {
1537 flags = MONITOR_USE_READLINE;
1538 } else if (strcmp(mode, "control") == 0) {
1539 flags = MONITOR_USE_CONTROL;
1540 } else {
1541 fprintf(stderr, "unknown monitor mode \"%s\"\n", mode);
1542 exit(1);
1545 if (qemu_opt_get_bool(opts, "pretty", 0))
1546 flags |= MONITOR_USE_PRETTY;
1548 if (qemu_opt_get_bool(opts, "default", 0))
1549 flags |= MONITOR_IS_DEFAULT;
1551 chardev = qemu_opt_get(opts, "chardev");
1552 chr = qemu_chr_find(chardev);
1553 if (chr == NULL) {
1554 fprintf(stderr, "chardev \"%s\" not found\n", chardev);
1555 exit(1);
1558 monitor_init(chr, flags);
1559 return 0;
1562 static void monitor_parse(const char *optarg, const char *mode)
1564 static int monitor_device_index = 0;
1565 QemuOpts *opts;
1566 const char *p;
1567 char label[32];
1568 int def = 0;
1570 if (strstart(optarg, "chardev:", &p)) {
1571 snprintf(label, sizeof(label), "%s", p);
1572 } else {
1573 snprintf(label, sizeof(label), "compat_monitor%d",
1574 monitor_device_index);
1575 if (monitor_device_index == 0) {
1576 def = 1;
1578 opts = qemu_chr_parse_compat(label, optarg);
1579 if (!opts) {
1580 fprintf(stderr, "parse error: %s\n", optarg);
1581 exit(1);
1585 opts = qemu_opts_create(qemu_find_opts("mon"), label, 1);
1586 if (!opts) {
1587 fprintf(stderr, "duplicate chardev: %s\n", label);
1588 exit(1);
1590 qemu_opt_set(opts, "mode", mode);
1591 qemu_opt_set(opts, "chardev", label);
1592 if (def)
1593 qemu_opt_set(opts, "default", "on");
1594 monitor_device_index++;
1597 struct device_config {
1598 enum {
1599 DEV_USB, /* -usbdevice */
1600 DEV_BT, /* -bt */
1601 DEV_SERIAL, /* -serial */
1602 DEV_PARALLEL, /* -parallel */
1603 DEV_VIRTCON, /* -virtioconsole */
1604 DEV_DEBUGCON, /* -debugcon */
1605 } type;
1606 const char *cmdline;
1607 QTAILQ_ENTRY(device_config) next;
1609 QTAILQ_HEAD(, device_config) device_configs = QTAILQ_HEAD_INITIALIZER(device_configs);
1611 static void add_device_config(int type, const char *cmdline)
1613 struct device_config *conf;
1615 conf = qemu_mallocz(sizeof(*conf));
1616 conf->type = type;
1617 conf->cmdline = cmdline;
1618 QTAILQ_INSERT_TAIL(&device_configs, conf, next);
1621 static int foreach_device_config(int type, int (*func)(const char *cmdline))
1623 struct device_config *conf;
1624 int rc;
1626 QTAILQ_FOREACH(conf, &device_configs, next) {
1627 if (conf->type != type)
1628 continue;
1629 rc = func(conf->cmdline);
1630 if (0 != rc)
1631 return rc;
1633 return 0;
1636 static int serial_parse(const char *devname)
1638 static int index = 0;
1639 char label[32];
1641 if (strcmp(devname, "none") == 0)
1642 return 0;
1643 if (index == MAX_SERIAL_PORTS) {
1644 fprintf(stderr, "qemu: too many serial ports\n");
1645 exit(1);
1647 snprintf(label, sizeof(label), "serial%d", index);
1648 serial_hds[index] = qemu_chr_open(label, devname, NULL);
1649 if (!serial_hds[index]) {
1650 fprintf(stderr, "qemu: could not open serial device '%s': %s\n",
1651 devname, strerror(errno));
1652 return -1;
1654 index++;
1655 return 0;
1658 static int parallel_parse(const char *devname)
1660 static int index = 0;
1661 char label[32];
1663 if (strcmp(devname, "none") == 0)
1664 return 0;
1665 if (index == MAX_PARALLEL_PORTS) {
1666 fprintf(stderr, "qemu: too many parallel ports\n");
1667 exit(1);
1669 snprintf(label, sizeof(label), "parallel%d", index);
1670 parallel_hds[index] = qemu_chr_open(label, devname, NULL);
1671 if (!parallel_hds[index]) {
1672 fprintf(stderr, "qemu: could not open parallel device '%s': %s\n",
1673 devname, strerror(errno));
1674 return -1;
1676 index++;
1677 return 0;
1680 static int virtcon_parse(const char *devname)
1682 QemuOptsList *device = qemu_find_opts("device");
1683 static int index = 0;
1684 char label[32];
1685 QemuOpts *bus_opts, *dev_opts;
1687 if (strcmp(devname, "none") == 0)
1688 return 0;
1689 if (index == MAX_VIRTIO_CONSOLES) {
1690 fprintf(stderr, "qemu: too many virtio consoles\n");
1691 exit(1);
1694 bus_opts = qemu_opts_create(device, NULL, 0);
1695 qemu_opt_set(bus_opts, "driver", "virtio-serial");
1697 dev_opts = qemu_opts_create(device, NULL, 0);
1698 qemu_opt_set(dev_opts, "driver", "virtconsole");
1700 snprintf(label, sizeof(label), "virtcon%d", index);
1701 virtcon_hds[index] = qemu_chr_open(label, devname, NULL);
1702 if (!virtcon_hds[index]) {
1703 fprintf(stderr, "qemu: could not open virtio console '%s': %s\n",
1704 devname, strerror(errno));
1705 return -1;
1707 qemu_opt_set(dev_opts, "chardev", label);
1709 index++;
1710 return 0;
1713 static int debugcon_parse(const char *devname)
1715 QemuOpts *opts;
1717 if (!qemu_chr_open("debugcon", devname, NULL)) {
1718 exit(1);
1720 opts = qemu_opts_create(qemu_find_opts("device"), "debugcon", 1);
1721 if (!opts) {
1722 fprintf(stderr, "qemu: already have a debugcon device\n");
1723 exit(1);
1725 qemu_opt_set(opts, "driver", "isa-debugcon");
1726 qemu_opt_set(opts, "chardev", "debugcon");
1727 return 0;
1730 void qemu_add_exit_notifier(Notifier *notify)
1732 notifier_list_add(&exit_notifiers, notify);
1735 void qemu_remove_exit_notifier(Notifier *notify)
1737 notifier_list_remove(&exit_notifiers, notify);
1740 static void qemu_run_exit_notifiers(void)
1742 notifier_list_notify(&exit_notifiers);
1745 static const QEMUOption *lookup_opt(int argc, char **argv,
1746 const char **poptarg, int *poptind)
1748 const QEMUOption *popt;
1749 int optind = *poptind;
1750 char *r = argv[optind];
1751 const char *optarg;
1753 loc_set_cmdline(argv, optind, 1);
1754 optind++;
1755 /* Treat --foo the same as -foo. */
1756 if (r[1] == '-')
1757 r++;
1758 popt = qemu_options;
1759 for(;;) {
1760 if (!popt->name) {
1761 error_report("invalid option");
1762 exit(1);
1764 if (!strcmp(popt->name, r + 1))
1765 break;
1766 popt++;
1768 if (popt->flags & HAS_ARG) {
1769 if (optind >= argc) {
1770 error_report("requires an argument");
1771 exit(1);
1773 optarg = argv[optind++];
1774 loc_set_cmdline(argv, optind - 2, 2);
1775 } else {
1776 optarg = NULL;
1779 *poptarg = optarg;
1780 *poptind = optind;
1782 return popt;
1785 int main(int argc, char **argv, char **envp)
1787 const char *gdbstub_dev = NULL;
1788 int i;
1789 int snapshot, linux_boot;
1790 const char *icount_option = NULL;
1791 const char *initrd_filename;
1792 const char *kernel_filename, *kernel_cmdline;
1793 char boot_devices[33] = "cad"; /* default to HD->floppy->CD-ROM */
1794 DisplayState *ds;
1795 DisplayChangeListener *dcl;
1796 int cyls, heads, secs, translation;
1797 QemuOpts *hda_opts = NULL, *opts;
1798 QemuOptsList *olist;
1799 int optind;
1800 const char *optarg;
1801 const char *loadvm = NULL;
1802 QEMUMachine *machine;
1803 const char *cpu_model;
1804 int tb_size;
1805 const char *pid_file = NULL;
1806 const char *incoming = NULL;
1807 int show_vnc_port = 0;
1808 int defconfig = 1;
1810 #ifdef CONFIG_SIMPLE_TRACE
1811 const char *trace_file = NULL;
1812 #endif
1813 atexit(qemu_run_exit_notifiers);
1814 error_set_progname(argv[0]);
1816 init_clocks();
1818 qemu_cache_utils_init(envp);
1820 QLIST_INIT (&vm_change_state_head);
1821 os_setup_early_signal_handling();
1823 module_call_init(MODULE_INIT_MACHINE);
1824 machine = find_default_machine();
1825 cpu_model = NULL;
1826 initrd_filename = NULL;
1827 ram_size = 0;
1828 snapshot = 0;
1829 kernel_filename = NULL;
1830 kernel_cmdline = "";
1831 cyls = heads = secs = 0;
1832 translation = BIOS_ATA_TRANSLATION_AUTO;
1834 for (i = 0; i < MAX_NODES; i++) {
1835 node_mem[i] = 0;
1836 node_cpumask[i] = 0;
1839 nb_numa_nodes = 0;
1840 nb_nics = 0;
1842 tb_size = 0;
1843 autostart= 1;
1845 /* first pass of option parsing */
1846 optind = 1;
1847 while (optind < argc) {
1848 if (argv[optind][0] != '-') {
1849 /* disk image */
1850 optind++;
1851 continue;
1852 } else {
1853 const QEMUOption *popt;
1855 popt = lookup_opt(argc, argv, &optarg, &optind);
1856 switch (popt->index) {
1857 case QEMU_OPTION_nodefconfig:
1858 defconfig=0;
1859 break;
1864 if (defconfig) {
1865 int ret;
1867 ret = qemu_read_config_file(CONFIG_QEMU_CONFDIR "/qemu.conf");
1868 if (ret < 0 && ret != -ENOENT) {
1869 exit(1);
1872 ret = qemu_read_config_file(arch_config_name);
1873 if (ret < 0 && ret != -ENOENT) {
1874 exit(1);
1877 cpudef_init();
1879 /* second pass of option parsing */
1880 optind = 1;
1881 for(;;) {
1882 if (optind >= argc)
1883 break;
1884 if (argv[optind][0] != '-') {
1885 hda_opts = drive_add(argv[optind++], HD_ALIAS, 0);
1886 } else {
1887 const QEMUOption *popt;
1889 popt = lookup_opt(argc, argv, &optarg, &optind);
1890 if (!(popt->arch_mask & arch_type)) {
1891 printf("Option %s not supported for this target\n", popt->name);
1892 exit(1);
1894 switch(popt->index) {
1895 case QEMU_OPTION_M:
1896 machine = find_machine(optarg);
1897 if (!machine) {
1898 QEMUMachine *m;
1899 printf("Supported machines are:\n");
1900 for(m = first_machine; m != NULL; m = m->next) {
1901 if (m->alias)
1902 printf("%-10s %s (alias of %s)\n",
1903 m->alias, m->desc, m->name);
1904 printf("%-10s %s%s\n",
1905 m->name, m->desc,
1906 m->is_default ? " (default)" : "");
1908 exit(*optarg != '?');
1910 break;
1911 case QEMU_OPTION_cpu:
1912 /* hw initialization will check this */
1913 if (*optarg == '?') {
1914 list_cpus(stdout, &fprintf, optarg);
1915 exit(0);
1916 } else {
1917 cpu_model = optarg;
1919 break;
1920 case QEMU_OPTION_initrd:
1921 initrd_filename = optarg;
1922 break;
1923 case QEMU_OPTION_hda:
1924 if (cyls == 0)
1925 hda_opts = drive_add(optarg, HD_ALIAS, 0);
1926 else
1927 hda_opts = drive_add(optarg, HD_ALIAS
1928 ",cyls=%d,heads=%d,secs=%d%s",
1929 0, cyls, heads, secs,
1930 translation == BIOS_ATA_TRANSLATION_LBA ?
1931 ",trans=lba" :
1932 translation == BIOS_ATA_TRANSLATION_NONE ?
1933 ",trans=none" : "");
1934 break;
1935 case QEMU_OPTION_hdb:
1936 case QEMU_OPTION_hdc:
1937 case QEMU_OPTION_hdd:
1938 drive_add(optarg, HD_ALIAS, popt->index - QEMU_OPTION_hda);
1939 break;
1940 case QEMU_OPTION_drive:
1941 drive_add(NULL, "%s", optarg);
1942 break;
1943 case QEMU_OPTION_set:
1944 if (qemu_set_option(optarg) != 0)
1945 exit(1);
1946 break;
1947 case QEMU_OPTION_global:
1948 if (qemu_global_option(optarg) != 0)
1949 exit(1);
1950 break;
1951 case QEMU_OPTION_mtdblock:
1952 drive_add(optarg, MTD_ALIAS);
1953 break;
1954 case QEMU_OPTION_sd:
1955 drive_add(optarg, SD_ALIAS);
1956 break;
1957 case QEMU_OPTION_pflash:
1958 drive_add(optarg, PFLASH_ALIAS);
1959 break;
1960 case QEMU_OPTION_snapshot:
1961 snapshot = 1;
1962 break;
1963 case QEMU_OPTION_hdachs:
1965 const char *p;
1966 p = optarg;
1967 cyls = strtol(p, (char **)&p, 0);
1968 if (cyls < 1 || cyls > 16383)
1969 goto chs_fail;
1970 if (*p != ',')
1971 goto chs_fail;
1972 p++;
1973 heads = strtol(p, (char **)&p, 0);
1974 if (heads < 1 || heads > 16)
1975 goto chs_fail;
1976 if (*p != ',')
1977 goto chs_fail;
1978 p++;
1979 secs = strtol(p, (char **)&p, 0);
1980 if (secs < 1 || secs > 63)
1981 goto chs_fail;
1982 if (*p == ',') {
1983 p++;
1984 if (!strcmp(p, "none"))
1985 translation = BIOS_ATA_TRANSLATION_NONE;
1986 else if (!strcmp(p, "lba"))
1987 translation = BIOS_ATA_TRANSLATION_LBA;
1988 else if (!strcmp(p, "auto"))
1989 translation = BIOS_ATA_TRANSLATION_AUTO;
1990 else
1991 goto chs_fail;
1992 } else if (*p != '\0') {
1993 chs_fail:
1994 fprintf(stderr, "qemu: invalid physical CHS format\n");
1995 exit(1);
1997 if (hda_opts != NULL) {
1998 char num[16];
1999 snprintf(num, sizeof(num), "%d", cyls);
2000 qemu_opt_set(hda_opts, "cyls", num);
2001 snprintf(num, sizeof(num), "%d", heads);
2002 qemu_opt_set(hda_opts, "heads", num);
2003 snprintf(num, sizeof(num), "%d", secs);
2004 qemu_opt_set(hda_opts, "secs", num);
2005 if (translation == BIOS_ATA_TRANSLATION_LBA)
2006 qemu_opt_set(hda_opts, "trans", "lba");
2007 if (translation == BIOS_ATA_TRANSLATION_NONE)
2008 qemu_opt_set(hda_opts, "trans", "none");
2011 break;
2012 case QEMU_OPTION_numa:
2013 if (nb_numa_nodes >= MAX_NODES) {
2014 fprintf(stderr, "qemu: too many NUMA nodes\n");
2015 exit(1);
2017 numa_add(optarg);
2018 break;
2019 case QEMU_OPTION_nographic:
2020 display_type = DT_NOGRAPHIC;
2021 break;
2022 #ifdef CONFIG_CURSES
2023 case QEMU_OPTION_curses:
2024 display_type = DT_CURSES;
2025 break;
2026 #endif
2027 case QEMU_OPTION_portrait:
2028 graphic_rotate = 1;
2029 break;
2030 case QEMU_OPTION_kernel:
2031 kernel_filename = optarg;
2032 break;
2033 case QEMU_OPTION_append:
2034 kernel_cmdline = optarg;
2035 break;
2036 case QEMU_OPTION_cdrom:
2037 drive_add(optarg, CDROM_ALIAS);
2038 break;
2039 case QEMU_OPTION_boot:
2041 static const char * const params[] = {
2042 "order", "once", "menu", NULL
2044 char buf[sizeof(boot_devices)];
2045 char *standard_boot_devices;
2046 int legacy = 0;
2048 if (!strchr(optarg, '=')) {
2049 legacy = 1;
2050 pstrcpy(buf, sizeof(buf), optarg);
2051 } else if (check_params(buf, sizeof(buf), params, optarg) < 0) {
2052 fprintf(stderr,
2053 "qemu: unknown boot parameter '%s' in '%s'\n",
2054 buf, optarg);
2055 exit(1);
2058 if (legacy ||
2059 get_param_value(buf, sizeof(buf), "order", optarg)) {
2060 validate_bootdevices(buf);
2061 pstrcpy(boot_devices, sizeof(boot_devices), buf);
2063 if (!legacy) {
2064 if (get_param_value(buf, sizeof(buf),
2065 "once", optarg)) {
2066 validate_bootdevices(buf);
2067 standard_boot_devices = qemu_strdup(boot_devices);
2068 pstrcpy(boot_devices, sizeof(boot_devices), buf);
2069 qemu_register_reset(restore_boot_devices,
2070 standard_boot_devices);
2072 if (get_param_value(buf, sizeof(buf),
2073 "menu", optarg)) {
2074 if (!strcmp(buf, "on")) {
2075 boot_menu = 1;
2076 } else if (!strcmp(buf, "off")) {
2077 boot_menu = 0;
2078 } else {
2079 fprintf(stderr,
2080 "qemu: invalid option value '%s'\n",
2081 buf);
2082 exit(1);
2087 break;
2088 case QEMU_OPTION_fda:
2089 case QEMU_OPTION_fdb:
2090 drive_add(optarg, FD_ALIAS, popt->index - QEMU_OPTION_fda);
2091 break;
2092 case QEMU_OPTION_no_fd_bootchk:
2093 fd_bootchk = 0;
2094 break;
2095 case QEMU_OPTION_netdev:
2096 if (net_client_parse(qemu_find_opts("netdev"), optarg) == -1) {
2097 exit(1);
2099 break;
2100 case QEMU_OPTION_net:
2101 if (net_client_parse(qemu_find_opts("net"), optarg) == -1) {
2102 exit(1);
2104 break;
2105 #ifdef CONFIG_SLIRP
2106 case QEMU_OPTION_tftp:
2107 legacy_tftp_prefix = optarg;
2108 break;
2109 case QEMU_OPTION_bootp:
2110 legacy_bootp_filename = optarg;
2111 break;
2112 case QEMU_OPTION_redir:
2113 if (net_slirp_redir(optarg) < 0)
2114 exit(1);
2115 break;
2116 #endif
2117 case QEMU_OPTION_bt:
2118 add_device_config(DEV_BT, optarg);
2119 break;
2120 case QEMU_OPTION_audio_help:
2121 if (!(audio_available())) {
2122 printf("Option %s not supported for this target\n", popt->name);
2123 exit(1);
2125 AUD_help ();
2126 exit (0);
2127 break;
2128 case QEMU_OPTION_soundhw:
2129 if (!(audio_available())) {
2130 printf("Option %s not supported for this target\n", popt->name);
2131 exit(1);
2133 select_soundhw (optarg);
2134 break;
2135 case QEMU_OPTION_h:
2136 help(0);
2137 break;
2138 case QEMU_OPTION_version:
2139 version();
2140 exit(0);
2141 break;
2142 case QEMU_OPTION_m: {
2143 ssize_t value;
2145 value = strtosz(optarg, NULL);
2146 if (value < 0) {
2147 fprintf(stderr, "qemu: invalid ram size: %s\n", optarg);
2148 exit(1);
2151 /* On 32-bit hosts, QEMU is limited by virtual address space */
2152 if (value > (2047 << 20) && HOST_LONG_BITS == 32) {
2153 fprintf(stderr, "qemu: at most 2047 MB RAM can be simulated\n");
2154 exit(1);
2156 if (value != (uint64_t)(ram_addr_t)value) {
2157 fprintf(stderr, "qemu: ram size too large\n");
2158 exit(1);
2160 ram_size = value;
2161 break;
2163 case QEMU_OPTION_mempath:
2164 mem_path = optarg;
2165 break;
2166 #ifdef MAP_POPULATE
2167 case QEMU_OPTION_mem_prealloc:
2168 mem_prealloc = 1;
2169 break;
2170 #endif
2171 case QEMU_OPTION_d:
2172 set_cpu_log(optarg);
2173 break;
2174 case QEMU_OPTION_s:
2175 gdbstub_dev = "tcp::" DEFAULT_GDBSTUB_PORT;
2176 break;
2177 case QEMU_OPTION_gdb:
2178 gdbstub_dev = optarg;
2179 break;
2180 case QEMU_OPTION_L:
2181 data_dir = optarg;
2182 break;
2183 case QEMU_OPTION_bios:
2184 bios_name = optarg;
2185 break;
2186 case QEMU_OPTION_singlestep:
2187 singlestep = 1;
2188 break;
2189 case QEMU_OPTION_S:
2190 autostart = 0;
2191 break;
2192 case QEMU_OPTION_k:
2193 keyboard_layout = optarg;
2194 break;
2195 case QEMU_OPTION_localtime:
2196 rtc_utc = 0;
2197 break;
2198 case QEMU_OPTION_vga:
2199 select_vgahw (optarg);
2200 break;
2201 case QEMU_OPTION_g:
2203 const char *p;
2204 int w, h, depth;
2205 p = optarg;
2206 w = strtol(p, (char **)&p, 10);
2207 if (w <= 0) {
2208 graphic_error:
2209 fprintf(stderr, "qemu: invalid resolution or depth\n");
2210 exit(1);
2212 if (*p != 'x')
2213 goto graphic_error;
2214 p++;
2215 h = strtol(p, (char **)&p, 10);
2216 if (h <= 0)
2217 goto graphic_error;
2218 if (*p == 'x') {
2219 p++;
2220 depth = strtol(p, (char **)&p, 10);
2221 if (depth != 8 && depth != 15 && depth != 16 &&
2222 depth != 24 && depth != 32)
2223 goto graphic_error;
2224 } else if (*p == '\0') {
2225 depth = graphic_depth;
2226 } else {
2227 goto graphic_error;
2230 graphic_width = w;
2231 graphic_height = h;
2232 graphic_depth = depth;
2234 break;
2235 case QEMU_OPTION_echr:
2237 char *r;
2238 term_escape_char = strtol(optarg, &r, 0);
2239 if (r == optarg)
2240 printf("Bad argument to echr\n");
2241 break;
2243 case QEMU_OPTION_monitor:
2244 monitor_parse(optarg, "readline");
2245 default_monitor = 0;
2246 break;
2247 case QEMU_OPTION_qmp:
2248 monitor_parse(optarg, "control");
2249 default_monitor = 0;
2250 break;
2251 case QEMU_OPTION_mon:
2252 opts = qemu_opts_parse(qemu_find_opts("mon"), optarg, 1);
2253 if (!opts) {
2254 exit(1);
2256 default_monitor = 0;
2257 break;
2258 case QEMU_OPTION_chardev:
2259 opts = qemu_opts_parse(qemu_find_opts("chardev"), optarg, 1);
2260 if (!opts) {
2261 exit(1);
2263 break;
2264 case QEMU_OPTION_fsdev:
2265 olist = qemu_find_opts("fsdev");
2266 if (!olist) {
2267 fprintf(stderr, "fsdev is not supported by this qemu build.\n");
2268 exit(1);
2270 opts = qemu_opts_parse(olist, optarg, 1);
2271 if (!opts) {
2272 fprintf(stderr, "parse error: %s\n", optarg);
2273 exit(1);
2275 break;
2276 case QEMU_OPTION_virtfs: {
2277 char *arg_fsdev = NULL;
2278 char *arg_9p = NULL;
2279 int len = 0;
2281 olist = qemu_find_opts("virtfs");
2282 if (!olist) {
2283 fprintf(stderr, "virtfs is not supported by this qemu build.\n");
2284 exit(1);
2286 opts = qemu_opts_parse(olist, optarg, 1);
2287 if (!opts) {
2288 fprintf(stderr, "parse error: %s\n", optarg);
2289 exit(1);
2292 if (qemu_opt_get(opts, "fstype") == NULL ||
2293 qemu_opt_get(opts, "mount_tag") == NULL ||
2294 qemu_opt_get(opts, "path") == NULL ||
2295 qemu_opt_get(opts, "security_model") == NULL) {
2296 fprintf(stderr, "Usage: -virtfs fstype,path=/share_path/,"
2297 "security_model=[mapped|passthrough|none],"
2298 "mnt_tag=tag.\n");
2299 exit(1);
2302 len = strlen(",id=,path=,security_model=");
2303 len += strlen(qemu_opt_get(opts, "fstype"));
2304 len += strlen(qemu_opt_get(opts, "mount_tag"));
2305 len += strlen(qemu_opt_get(opts, "path"));
2306 len += strlen(qemu_opt_get(opts, "security_model"));
2307 arg_fsdev = qemu_malloc((len + 1) * sizeof(*arg_fsdev));
2309 snprintf(arg_fsdev, (len + 1) * sizeof(*arg_fsdev),
2310 "%s,id=%s,path=%s,security_model=%s",
2311 qemu_opt_get(opts, "fstype"),
2312 qemu_opt_get(opts, "mount_tag"),
2313 qemu_opt_get(opts, "path"),
2314 qemu_opt_get(opts, "security_model"));
2316 len = strlen("virtio-9p-pci,fsdev=,mount_tag=");
2317 len += 2*strlen(qemu_opt_get(opts, "mount_tag"));
2318 arg_9p = qemu_malloc((len + 1) * sizeof(*arg_9p));
2320 snprintf(arg_9p, (len + 1) * sizeof(*arg_9p),
2321 "virtio-9p-pci,fsdev=%s,mount_tag=%s",
2322 qemu_opt_get(opts, "mount_tag"),
2323 qemu_opt_get(opts, "mount_tag"));
2325 if (!qemu_opts_parse(qemu_find_opts("fsdev"), arg_fsdev, 1)) {
2326 fprintf(stderr, "parse error [fsdev]: %s\n", optarg);
2327 exit(1);
2330 if (!qemu_opts_parse(qemu_find_opts("device"), arg_9p, 1)) {
2331 fprintf(stderr, "parse error [device]: %s\n", optarg);
2332 exit(1);
2335 qemu_free(arg_fsdev);
2336 qemu_free(arg_9p);
2337 break;
2339 case QEMU_OPTION_serial:
2340 add_device_config(DEV_SERIAL, optarg);
2341 default_serial = 0;
2342 if (strncmp(optarg, "mon:", 4) == 0) {
2343 default_monitor = 0;
2345 break;
2346 case QEMU_OPTION_watchdog:
2347 if (watchdog) {
2348 fprintf(stderr,
2349 "qemu: only one watchdog option may be given\n");
2350 return 1;
2352 watchdog = optarg;
2353 break;
2354 case QEMU_OPTION_watchdog_action:
2355 if (select_watchdog_action(optarg) == -1) {
2356 fprintf(stderr, "Unknown -watchdog-action parameter\n");
2357 exit(1);
2359 break;
2360 case QEMU_OPTION_virtiocon:
2361 add_device_config(DEV_VIRTCON, optarg);
2362 default_virtcon = 0;
2363 if (strncmp(optarg, "mon:", 4) == 0) {
2364 default_monitor = 0;
2366 break;
2367 case QEMU_OPTION_parallel:
2368 add_device_config(DEV_PARALLEL, optarg);
2369 default_parallel = 0;
2370 if (strncmp(optarg, "mon:", 4) == 0) {
2371 default_monitor = 0;
2373 break;
2374 case QEMU_OPTION_debugcon:
2375 add_device_config(DEV_DEBUGCON, optarg);
2376 break;
2377 case QEMU_OPTION_loadvm:
2378 loadvm = optarg;
2379 break;
2380 case QEMU_OPTION_full_screen:
2381 full_screen = 1;
2382 break;
2383 #ifdef CONFIG_SDL
2384 case QEMU_OPTION_no_frame:
2385 no_frame = 1;
2386 break;
2387 case QEMU_OPTION_alt_grab:
2388 alt_grab = 1;
2389 break;
2390 case QEMU_OPTION_ctrl_grab:
2391 ctrl_grab = 1;
2392 break;
2393 case QEMU_OPTION_no_quit:
2394 no_quit = 1;
2395 break;
2396 case QEMU_OPTION_sdl:
2397 display_type = DT_SDL;
2398 break;
2399 #endif
2400 case QEMU_OPTION_pidfile:
2401 pid_file = optarg;
2402 break;
2403 case QEMU_OPTION_win2k_hack:
2404 win2k_install_hack = 1;
2405 break;
2406 case QEMU_OPTION_rtc_td_hack:
2407 rtc_td_hack = 1;
2408 break;
2409 case QEMU_OPTION_acpitable:
2410 do_acpitable_option(optarg);
2411 break;
2412 case QEMU_OPTION_smbios:
2413 do_smbios_option(optarg);
2414 break;
2415 case QEMU_OPTION_enable_kvm:
2416 kvm_allowed = 1;
2417 break;
2418 case QEMU_OPTION_usb:
2419 usb_enabled = 1;
2420 break;
2421 case QEMU_OPTION_usbdevice:
2422 usb_enabled = 1;
2423 add_device_config(DEV_USB, optarg);
2424 break;
2425 case QEMU_OPTION_device:
2426 if (!qemu_opts_parse(qemu_find_opts("device"), optarg, 1)) {
2427 exit(1);
2429 break;
2430 case QEMU_OPTION_smp:
2431 smp_parse(optarg);
2432 if (smp_cpus < 1) {
2433 fprintf(stderr, "Invalid number of CPUs\n");
2434 exit(1);
2436 if (max_cpus < smp_cpus) {
2437 fprintf(stderr, "maxcpus must be equal to or greater than "
2438 "smp\n");
2439 exit(1);
2441 if (max_cpus > 255) {
2442 fprintf(stderr, "Unsupported number of maxcpus\n");
2443 exit(1);
2445 break;
2446 case QEMU_OPTION_vnc:
2447 display_remote++;
2448 vnc_display = optarg;
2449 break;
2450 case QEMU_OPTION_no_acpi:
2451 acpi_enabled = 0;
2452 break;
2453 case QEMU_OPTION_no_hpet:
2454 no_hpet = 1;
2455 break;
2456 case QEMU_OPTION_balloon:
2457 if (balloon_parse(optarg) < 0) {
2458 fprintf(stderr, "Unknown -balloon argument %s\n", optarg);
2459 exit(1);
2461 break;
2462 case QEMU_OPTION_no_reboot:
2463 no_reboot = 1;
2464 break;
2465 case QEMU_OPTION_no_shutdown:
2466 no_shutdown = 1;
2467 break;
2468 case QEMU_OPTION_show_cursor:
2469 cursor_hide = 0;
2470 break;
2471 case QEMU_OPTION_uuid:
2472 if(qemu_uuid_parse(optarg, qemu_uuid) < 0) {
2473 fprintf(stderr, "Fail to parse UUID string."
2474 " Wrong format.\n");
2475 exit(1);
2477 break;
2478 case QEMU_OPTION_option_rom:
2479 if (nb_option_roms >= MAX_OPTION_ROMS) {
2480 fprintf(stderr, "Too many option ROMs\n");
2481 exit(1);
2483 option_rom[nb_option_roms] = optarg;
2484 nb_option_roms++;
2485 break;
2486 case QEMU_OPTION_semihosting:
2487 semihosting_enabled = 1;
2488 break;
2489 case QEMU_OPTION_name:
2490 qemu_name = qemu_strdup(optarg);
2492 char *p = strchr(qemu_name, ',');
2493 if (p != NULL) {
2494 *p++ = 0;
2495 if (strncmp(p, "process=", 8)) {
2496 fprintf(stderr, "Unknown subargument %s to -name", p);
2497 exit(1);
2499 p += 8;
2500 os_set_proc_name(p);
2503 break;
2504 case QEMU_OPTION_prom_env:
2505 if (nb_prom_envs >= MAX_PROM_ENVS) {
2506 fprintf(stderr, "Too many prom variables\n");
2507 exit(1);
2509 prom_envs[nb_prom_envs] = optarg;
2510 nb_prom_envs++;
2511 break;
2512 case QEMU_OPTION_old_param:
2513 old_param = 1;
2514 break;
2515 case QEMU_OPTION_clock:
2516 configure_alarms(optarg);
2517 break;
2518 case QEMU_OPTION_startdate:
2519 configure_rtc_date_offset(optarg, 1);
2520 break;
2521 case QEMU_OPTION_rtc:
2522 opts = qemu_opts_parse(qemu_find_opts("rtc"), optarg, 0);
2523 if (!opts) {
2524 exit(1);
2526 configure_rtc(opts);
2527 break;
2528 case QEMU_OPTION_tb_size:
2529 tb_size = strtol(optarg, NULL, 0);
2530 if (tb_size < 0)
2531 tb_size = 0;
2532 break;
2533 case QEMU_OPTION_icount:
2534 icount_option = optarg;
2535 break;
2536 case QEMU_OPTION_incoming:
2537 incoming = optarg;
2538 incoming_expected = true;
2539 break;
2540 case QEMU_OPTION_nodefaults:
2541 default_serial = 0;
2542 default_parallel = 0;
2543 default_virtcon = 0;
2544 default_monitor = 0;
2545 default_vga = 0;
2546 default_net = 0;
2547 default_floppy = 0;
2548 default_cdrom = 0;
2549 default_sdcard = 0;
2550 break;
2551 case QEMU_OPTION_xen_domid:
2552 if (!(xen_available())) {
2553 printf("Option %s not supported for this target\n", popt->name);
2554 exit(1);
2556 xen_domid = atoi(optarg);
2557 break;
2558 case QEMU_OPTION_xen_create:
2559 if (!(xen_available())) {
2560 printf("Option %s not supported for this target\n", popt->name);
2561 exit(1);
2563 xen_mode = XEN_CREATE;
2564 break;
2565 case QEMU_OPTION_xen_attach:
2566 if (!(xen_available())) {
2567 printf("Option %s not supported for this target\n", popt->name);
2568 exit(1);
2570 xen_mode = XEN_ATTACH;
2571 break;
2572 #ifdef CONFIG_SIMPLE_TRACE
2573 case QEMU_OPTION_trace:
2574 opts = qemu_opts_parse(qemu_find_opts("trace"), optarg, 0);
2575 if (opts) {
2576 trace_file = qemu_opt_get(opts, "file");
2578 break;
2579 #endif
2580 case QEMU_OPTION_readconfig:
2582 int ret = qemu_read_config_file(optarg);
2583 if (ret < 0) {
2584 fprintf(stderr, "read config %s: %s\n", optarg,
2585 strerror(-ret));
2586 exit(1);
2588 break;
2590 case QEMU_OPTION_spice:
2591 olist = qemu_find_opts("spice");
2592 if (!olist) {
2593 fprintf(stderr, "spice is not supported by this qemu build.\n");
2594 exit(1);
2596 opts = qemu_opts_parse(olist, optarg, 0);
2597 if (!opts) {
2598 fprintf(stderr, "parse error: %s\n", optarg);
2599 exit(1);
2601 break;
2602 case QEMU_OPTION_writeconfig:
2604 FILE *fp;
2605 if (strcmp(optarg, "-") == 0) {
2606 fp = stdout;
2607 } else {
2608 fp = fopen(optarg, "w");
2609 if (fp == NULL) {
2610 fprintf(stderr, "open %s: %s\n", optarg, strerror(errno));
2611 exit(1);
2614 qemu_config_write(fp);
2615 fclose(fp);
2616 break;
2618 default:
2619 os_parse_cmd_args(popt->index, optarg);
2623 loc_set_none();
2625 /* If no data_dir is specified then try to find it relative to the
2626 executable path. */
2627 if (!data_dir) {
2628 data_dir = os_find_datadir(argv[0]);
2630 /* If all else fails use the install patch specified when building. */
2631 if (!data_dir) {
2632 data_dir = CONFIG_QEMU_DATADIR;
2635 #ifdef CONFIG_SIMPLE_TRACE
2637 * Set the trace file name, if specified.
2639 st_set_trace_file(trace_file);
2640 #endif
2642 * Default to max_cpus = smp_cpus, in case the user doesn't
2643 * specify a max_cpus value.
2645 if (!max_cpus)
2646 max_cpus = smp_cpus;
2648 machine->max_cpus = machine->max_cpus ?: 1; /* Default to UP */
2649 if (smp_cpus > machine->max_cpus) {
2650 fprintf(stderr, "Number of SMP cpus requested (%d), exceeds max cpus "
2651 "supported by machine `%s' (%d)\n", smp_cpus, machine->name,
2652 machine->max_cpus);
2653 exit(1);
2656 qemu_opts_foreach(qemu_find_opts("device"), default_driver_check, NULL, 0);
2657 qemu_opts_foreach(qemu_find_opts("global"), default_driver_check, NULL, 0);
2659 if (machine->no_serial) {
2660 default_serial = 0;
2662 if (machine->no_parallel) {
2663 default_parallel = 0;
2665 if (!machine->use_virtcon) {
2666 default_virtcon = 0;
2668 if (machine->no_vga) {
2669 default_vga = 0;
2671 if (machine->no_floppy) {
2672 default_floppy = 0;
2674 if (machine->no_cdrom) {
2675 default_cdrom = 0;
2677 if (machine->no_sdcard) {
2678 default_sdcard = 0;
2681 if (display_type == DT_NOGRAPHIC) {
2682 if (default_parallel)
2683 add_device_config(DEV_PARALLEL, "null");
2684 if (default_serial && default_monitor) {
2685 add_device_config(DEV_SERIAL, "mon:stdio");
2686 } else if (default_virtcon && default_monitor) {
2687 add_device_config(DEV_VIRTCON, "mon:stdio");
2688 } else {
2689 if (default_serial)
2690 add_device_config(DEV_SERIAL, "stdio");
2691 if (default_virtcon)
2692 add_device_config(DEV_VIRTCON, "stdio");
2693 if (default_monitor)
2694 monitor_parse("stdio", "readline");
2696 } else {
2697 if (default_serial)
2698 add_device_config(DEV_SERIAL, "vc:80Cx24C");
2699 if (default_parallel)
2700 add_device_config(DEV_PARALLEL, "vc:80Cx24C");
2701 if (default_monitor)
2702 monitor_parse("vc:80Cx24C", "readline");
2703 if (default_virtcon)
2704 add_device_config(DEV_VIRTCON, "vc:80Cx24C");
2706 if (default_vga)
2707 vga_interface_type = VGA_CIRRUS;
2709 socket_init();
2711 if (qemu_opts_foreach(qemu_find_opts("chardev"), chardev_init_func, NULL, 1) != 0)
2712 exit(1);
2713 #ifdef CONFIG_VIRTFS
2714 if (qemu_opts_foreach(qemu_find_opts("fsdev"), fsdev_init_func, NULL, 1) != 0) {
2715 exit(1);
2717 #endif
2719 os_daemonize();
2721 if (pid_file && qemu_create_pidfile(pid_file) != 0) {
2722 os_pidfile_error();
2723 exit(1);
2726 if (kvm_allowed) {
2727 int ret = kvm_init(smp_cpus);
2728 if (ret < 0) {
2729 if (!kvm_available()) {
2730 printf("KVM not supported for this target\n");
2731 } else {
2732 fprintf(stderr, "failed to initialize KVM: %s\n", strerror(-ret));
2734 exit(1);
2738 if (qemu_init_main_loop()) {
2739 fprintf(stderr, "qemu_init_main_loop failed\n");
2740 exit(1);
2742 linux_boot = (kernel_filename != NULL);
2744 if (!linux_boot && *kernel_cmdline != '\0') {
2745 fprintf(stderr, "-append only allowed with -kernel option\n");
2746 exit(1);
2749 if (!linux_boot && initrd_filename != NULL) {
2750 fprintf(stderr, "-initrd only allowed with -kernel option\n");
2751 exit(1);
2754 os_set_line_buffering();
2756 if (init_timer_alarm() < 0) {
2757 fprintf(stderr, "could not initialize alarm timer\n");
2758 exit(1);
2760 configure_icount(icount_option);
2762 if (net_init_clients() < 0) {
2763 exit(1);
2766 /* init the bluetooth world */
2767 if (foreach_device_config(DEV_BT, bt_parse))
2768 exit(1);
2770 /* init the memory */
2771 if (ram_size == 0)
2772 ram_size = DEFAULT_RAM_SIZE * 1024 * 1024;
2774 /* init the dynamic translator */
2775 cpu_exec_init_all(tb_size * 1024 * 1024);
2777 bdrv_init_with_whitelist();
2779 blk_mig_init();
2781 if (default_cdrom) {
2782 /* we always create the cdrom drive, even if no disk is there */
2783 drive_add(NULL, CDROM_ALIAS);
2786 if (default_floppy) {
2787 /* we always create at least one floppy */
2788 drive_add(NULL, FD_ALIAS, 0);
2791 if (default_sdcard) {
2792 /* we always create one sd slot, even if no card is in it */
2793 drive_add(NULL, SD_ALIAS);
2796 /* open the virtual block devices */
2797 if (snapshot)
2798 qemu_opts_foreach(qemu_find_opts("drive"), drive_enable_snapshot, NULL, 0);
2799 if (qemu_opts_foreach(qemu_find_opts("drive"), drive_init_func, &machine->use_scsi, 1) != 0)
2800 exit(1);
2802 register_savevm_live(NULL, "ram", 0, 4, NULL, ram_save_live, NULL,
2803 ram_load, NULL);
2805 if (nb_numa_nodes > 0) {
2806 int i;
2808 if (nb_numa_nodes > smp_cpus) {
2809 nb_numa_nodes = smp_cpus;
2812 /* If no memory size if given for any node, assume the default case
2813 * and distribute the available memory equally across all nodes
2815 for (i = 0; i < nb_numa_nodes; i++) {
2816 if (node_mem[i] != 0)
2817 break;
2819 if (i == nb_numa_nodes) {
2820 uint64_t usedmem = 0;
2822 /* On Linux, the each node's border has to be 8MB aligned,
2823 * the final node gets the rest.
2825 for (i = 0; i < nb_numa_nodes - 1; i++) {
2826 node_mem[i] = (ram_size / nb_numa_nodes) & ~((1 << 23UL) - 1);
2827 usedmem += node_mem[i];
2829 node_mem[i] = ram_size - usedmem;
2832 for (i = 0; i < nb_numa_nodes; i++) {
2833 if (node_cpumask[i] != 0)
2834 break;
2836 /* assigning the VCPUs round-robin is easier to implement, guest OSes
2837 * must cope with this anyway, because there are BIOSes out there in
2838 * real machines which also use this scheme.
2840 if (i == nb_numa_nodes) {
2841 for (i = 0; i < smp_cpus; i++) {
2842 node_cpumask[i % nb_numa_nodes] |= 1 << i;
2847 if (qemu_opts_foreach(qemu_find_opts("mon"), mon_init_func, NULL, 1) != 0) {
2848 exit(1);
2851 if (foreach_device_config(DEV_SERIAL, serial_parse) < 0)
2852 exit(1);
2853 if (foreach_device_config(DEV_PARALLEL, parallel_parse) < 0)
2854 exit(1);
2855 if (foreach_device_config(DEV_VIRTCON, virtcon_parse) < 0)
2856 exit(1);
2857 if (foreach_device_config(DEV_DEBUGCON, debugcon_parse) < 0)
2858 exit(1);
2860 module_call_init(MODULE_INIT_DEVICE);
2862 if (qemu_opts_foreach(qemu_find_opts("device"), device_help_func, NULL, 0) != 0)
2863 exit(0);
2865 if (watchdog) {
2866 i = select_watchdog(watchdog);
2867 if (i > 0)
2868 exit (i == 1 ? 1 : 0);
2871 if (machine->compat_props) {
2872 qdev_prop_register_global_list(machine->compat_props);
2874 qemu_add_globals();
2876 machine->init(ram_size, boot_devices,
2877 kernel_filename, kernel_cmdline, initrd_filename, cpu_model);
2879 cpu_synchronize_all_post_init();
2881 /* must be after terminal init, SDL library changes signal handlers */
2882 os_setup_signal_handling();
2884 set_numa_modes();
2886 current_machine = machine;
2888 /* init USB devices */
2889 if (usb_enabled) {
2890 if (foreach_device_config(DEV_USB, usb_parse) < 0)
2891 exit(1);
2894 /* init generic devices */
2895 if (qemu_opts_foreach(qemu_find_opts("device"), device_init_func, NULL, 1) != 0)
2896 exit(1);
2898 net_check_clients();
2900 /* just use the first displaystate for the moment */
2901 ds = get_displaystate();
2903 if (using_spice)
2904 display_remote++;
2905 if (display_type == DT_DEFAULT && !display_remote) {
2906 #if defined(CONFIG_SDL) || defined(CONFIG_COCOA)
2907 display_type = DT_SDL;
2908 #else
2909 vnc_display = "localhost:0,to=99";
2910 show_vnc_port = 1;
2911 #endif
2915 /* init local displays */
2916 switch (display_type) {
2917 case DT_NOGRAPHIC:
2918 break;
2919 #if defined(CONFIG_CURSES)
2920 case DT_CURSES:
2921 curses_display_init(ds, full_screen);
2922 break;
2923 #endif
2924 #if defined(CONFIG_SDL)
2925 case DT_SDL:
2926 sdl_display_init(ds, full_screen, no_frame);
2927 break;
2928 #elif defined(CONFIG_COCOA)
2929 case DT_SDL:
2930 cocoa_display_init(ds, full_screen);
2931 break;
2932 #endif
2933 default:
2934 break;
2937 /* init remote displays */
2938 if (vnc_display) {
2939 vnc_display_init(ds);
2940 if (vnc_display_open(ds, vnc_display) < 0)
2941 exit(1);
2943 if (show_vnc_port) {
2944 printf("VNC server running on `%s'\n", vnc_display_local_addr(ds));
2947 #ifdef CONFIG_SPICE
2948 if (using_spice) {
2949 qemu_spice_display_init(ds);
2951 #endif
2953 /* display setup */
2954 dpy_resize(ds);
2955 dcl = ds->listeners;
2956 while (dcl != NULL) {
2957 if (dcl->dpy_refresh != NULL) {
2958 ds->gui_timer = qemu_new_timer(rt_clock, gui_update, ds);
2959 qemu_mod_timer(ds->gui_timer, qemu_get_clock(rt_clock));
2960 break;
2962 dcl = dcl->next;
2964 if (ds->gui_timer == NULL) {
2965 nographic_timer = qemu_new_timer(rt_clock, nographic_update, NULL);
2966 qemu_mod_timer(nographic_timer, qemu_get_clock(rt_clock));
2968 text_consoles_set_display(ds);
2970 if (gdbstub_dev && gdbserver_start(gdbstub_dev) < 0) {
2971 fprintf(stderr, "qemu: could not open gdbserver on device '%s'\n",
2972 gdbstub_dev);
2973 exit(1);
2976 qdev_machine_creation_done();
2978 if (rom_load_all() != 0) {
2979 fprintf(stderr, "rom loading failed\n");
2980 exit(1);
2983 qemu_register_reset((void *)qbus_reset_all, sysbus_get_default());
2984 qemu_system_reset();
2985 if (loadvm) {
2986 if (load_vmstate(loadvm) < 0) {
2987 autostart = 0;
2991 if (incoming) {
2992 int ret = qemu_start_incoming_migration(incoming);
2993 if (ret < 0) {
2994 fprintf(stderr, "Migration failed. Exit code %s(%d), exiting.\n",
2995 incoming, ret);
2996 exit(ret);
2998 } else if (autostart) {
2999 vm_start();
3002 os_setup_post();
3004 main_loop();
3005 quit_timers();
3006 net_cleanup();
3008 return 0;