Add new vgabios binaries to blobs list.
[qemu/mdroth.git] / vl.c
blobc58583da48d6309eab9a3cdb09eef9571c599fd1
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 "qemu-queue.h"
162 #include "cpus.h"
163 #include "arch_init.h"
165 #include "ui/qemu-spice.h"
167 //#define DEBUG_NET
168 //#define DEBUG_SLIRP
170 #define DEFAULT_RAM_SIZE 128
172 #define MAX_VIRTIO_CONSOLES 1
174 static const char *data_dir;
175 const char *bios_name = NULL;
176 enum vga_retrace_method vga_retrace_method = VGA_RETRACE_DUMB;
177 DisplayType display_type = DT_DEFAULT;
178 int display_remote = 0;
179 const char* keyboard_layout = NULL;
180 ram_addr_t ram_size;
181 const char *mem_path = NULL;
182 #ifdef MAP_POPULATE
183 int mem_prealloc = 0; /* force preallocation of physical target memory */
184 #endif
185 int nb_nics;
186 NICInfo nd_table[MAX_NICS];
187 int vm_running;
188 int autostart;
189 int incoming_expected; /* Started with -incoming and waiting for incoming */
190 static int rtc_utc = 1;
191 static int rtc_date_offset = -1; /* -1 means no change */
192 QEMUClock *rtc_clock;
193 int vga_interface_type = VGA_NONE;
194 static int full_screen = 0;
195 #ifdef CONFIG_SDL
196 static int no_frame = 0;
197 #endif
198 int no_quit = 0;
199 CharDriverState *serial_hds[MAX_SERIAL_PORTS];
200 CharDriverState *parallel_hds[MAX_PARALLEL_PORTS];
201 CharDriverState *virtcon_hds[MAX_VIRTIO_CONSOLES];
202 int win2k_install_hack = 0;
203 int rtc_td_hack = 0;
204 int usb_enabled = 0;
205 int singlestep = 0;
206 int smp_cpus = 1;
207 int max_cpus = 0;
208 int smp_cores = 1;
209 int smp_threads = 1;
210 const char *vnc_display;
211 int acpi_enabled = 1;
212 int no_hpet = 0;
213 int fd_bootchk = 1;
214 int no_reboot = 0;
215 int no_shutdown = 0;
216 int cursor_hide = 1;
217 int graphic_rotate = 0;
218 uint8_t irq0override = 1;
219 const char *watchdog;
220 const char *option_rom[MAX_OPTION_ROMS];
221 int nb_option_roms;
222 int semihosting_enabled = 0;
223 int old_param = 0;
224 const char *qemu_name;
225 int alt_grab = 0;
226 int ctrl_grab = 0;
227 unsigned int nb_prom_envs = 0;
228 const char *prom_envs[MAX_PROM_ENVS];
229 int boot_menu;
231 int nb_numa_nodes;
232 uint64_t node_mem[MAX_NODES];
233 uint64_t node_cpumask[MAX_NODES];
235 static QEMUTimer *nographic_timer;
237 uint8_t qemu_uuid[16];
239 static QEMUBootSetHandler *boot_set_handler;
240 static void *boot_set_opaque;
242 static NotifierList exit_notifiers =
243 NOTIFIER_LIST_INITIALIZER(exit_notifiers);
245 int kvm_allowed = 0;
246 uint32_t xen_domid;
247 enum xen_mode xen_mode = XEN_EMULATE;
249 static int default_serial = 1;
250 static int default_parallel = 1;
251 static int default_virtcon = 1;
252 static int default_monitor = 1;
253 static int default_vga = 1;
254 static int default_floppy = 1;
255 static int default_cdrom = 1;
256 static int default_sdcard = 1;
258 static struct {
259 const char *driver;
260 int *flag;
261 } default_list[] = {
262 { .driver = "isa-serial", .flag = &default_serial },
263 { .driver = "isa-parallel", .flag = &default_parallel },
264 { .driver = "isa-fdc", .flag = &default_floppy },
265 { .driver = "ide-drive", .flag = &default_cdrom },
266 { .driver = "virtio-serial-pci", .flag = &default_virtcon },
267 { .driver = "virtio-serial-s390", .flag = &default_virtcon },
268 { .driver = "virtio-serial", .flag = &default_virtcon },
269 { .driver = "VGA", .flag = &default_vga },
270 { .driver = "cirrus-vga", .flag = &default_vga },
271 { .driver = "vmware-svga", .flag = &default_vga },
274 static int default_driver_check(QemuOpts *opts, void *opaque)
276 const char *driver = qemu_opt_get(opts, "driver");
277 int i;
279 if (!driver)
280 return 0;
281 for (i = 0; i < ARRAY_SIZE(default_list); i++) {
282 if (strcmp(default_list[i].driver, driver) != 0)
283 continue;
284 *(default_list[i].flag) = 0;
286 return 0;
289 /***********************************************************/
290 /* real time host monotonic timer */
292 /***********************************************************/
293 /* host time/date access */
294 void qemu_get_timedate(struct tm *tm, int offset)
296 time_t ti;
297 struct tm *ret;
299 time(&ti);
300 ti += offset;
301 if (rtc_date_offset == -1) {
302 if (rtc_utc)
303 ret = gmtime(&ti);
304 else
305 ret = localtime(&ti);
306 } else {
307 ti -= rtc_date_offset;
308 ret = gmtime(&ti);
311 memcpy(tm, ret, sizeof(struct tm));
314 int qemu_timedate_diff(struct tm *tm)
316 time_t seconds;
318 if (rtc_date_offset == -1)
319 if (rtc_utc)
320 seconds = mktimegm(tm);
321 else
322 seconds = mktime(tm);
323 else
324 seconds = mktimegm(tm) + rtc_date_offset;
326 return seconds - time(NULL);
329 void rtc_change_mon_event(struct tm *tm)
331 QObject *data;
333 data = qobject_from_jsonf("{ 'offset': %d }", qemu_timedate_diff(tm));
334 monitor_protocol_event(QEVENT_RTC_CHANGE, data);
335 qobject_decref(data);
338 static void configure_rtc_date_offset(const char *startdate, int legacy)
340 time_t rtc_start_date;
341 struct tm tm;
343 if (!strcmp(startdate, "now") && legacy) {
344 rtc_date_offset = -1;
345 } else {
346 if (sscanf(startdate, "%d-%d-%dT%d:%d:%d",
347 &tm.tm_year,
348 &tm.tm_mon,
349 &tm.tm_mday,
350 &tm.tm_hour,
351 &tm.tm_min,
352 &tm.tm_sec) == 6) {
353 /* OK */
354 } else if (sscanf(startdate, "%d-%d-%d",
355 &tm.tm_year,
356 &tm.tm_mon,
357 &tm.tm_mday) == 3) {
358 tm.tm_hour = 0;
359 tm.tm_min = 0;
360 tm.tm_sec = 0;
361 } else {
362 goto date_fail;
364 tm.tm_year -= 1900;
365 tm.tm_mon--;
366 rtc_start_date = mktimegm(&tm);
367 if (rtc_start_date == -1) {
368 date_fail:
369 fprintf(stderr, "Invalid date format. Valid formats are:\n"
370 "'2006-06-17T16:01:21' or '2006-06-17'\n");
371 exit(1);
373 rtc_date_offset = time(NULL) - rtc_start_date;
377 static void configure_rtc(QemuOpts *opts)
379 const char *value;
381 value = qemu_opt_get(opts, "base");
382 if (value) {
383 if (!strcmp(value, "utc")) {
384 rtc_utc = 1;
385 } else if (!strcmp(value, "localtime")) {
386 rtc_utc = 0;
387 } else {
388 configure_rtc_date_offset(value, 0);
391 value = qemu_opt_get(opts, "clock");
392 if (value) {
393 if (!strcmp(value, "host")) {
394 rtc_clock = host_clock;
395 } else if (!strcmp(value, "vm")) {
396 rtc_clock = vm_clock;
397 } else {
398 fprintf(stderr, "qemu: invalid option value '%s'\n", value);
399 exit(1);
402 value = qemu_opt_get(opts, "driftfix");
403 if (value) {
404 if (!strcmp(value, "slew")) {
405 rtc_td_hack = 1;
406 } else if (!strcmp(value, "none")) {
407 rtc_td_hack = 0;
408 } else {
409 fprintf(stderr, "qemu: invalid option value '%s'\n", value);
410 exit(1);
415 /***********************************************************/
416 /* Bluetooth support */
417 static int nb_hcis;
418 static int cur_hci;
419 static struct HCIInfo *hci_table[MAX_NICS];
421 static struct bt_vlan_s {
422 struct bt_scatternet_s net;
423 int id;
424 struct bt_vlan_s *next;
425 } *first_bt_vlan;
427 /* find or alloc a new bluetooth "VLAN" */
428 static struct bt_scatternet_s *qemu_find_bt_vlan(int id)
430 struct bt_vlan_s **pvlan, *vlan;
431 for (vlan = first_bt_vlan; vlan != NULL; vlan = vlan->next) {
432 if (vlan->id == id)
433 return &vlan->net;
435 vlan = qemu_mallocz(sizeof(struct bt_vlan_s));
436 vlan->id = id;
437 pvlan = &first_bt_vlan;
438 while (*pvlan != NULL)
439 pvlan = &(*pvlan)->next;
440 *pvlan = vlan;
441 return &vlan->net;
444 static void null_hci_send(struct HCIInfo *hci, const uint8_t *data, int len)
448 static int null_hci_addr_set(struct HCIInfo *hci, const uint8_t *bd_addr)
450 return -ENOTSUP;
453 static struct HCIInfo null_hci = {
454 .cmd_send = null_hci_send,
455 .sco_send = null_hci_send,
456 .acl_send = null_hci_send,
457 .bdaddr_set = null_hci_addr_set,
460 struct HCIInfo *qemu_next_hci(void)
462 if (cur_hci == nb_hcis)
463 return &null_hci;
465 return hci_table[cur_hci++];
468 static struct HCIInfo *hci_init(const char *str)
470 char *endp;
471 struct bt_scatternet_s *vlan = 0;
473 if (!strcmp(str, "null"))
474 /* null */
475 return &null_hci;
476 else if (!strncmp(str, "host", 4) && (str[4] == '\0' || str[4] == ':'))
477 /* host[:hciN] */
478 return bt_host_hci(str[4] ? str + 5 : "hci0");
479 else if (!strncmp(str, "hci", 3)) {
480 /* hci[,vlan=n] */
481 if (str[3]) {
482 if (!strncmp(str + 3, ",vlan=", 6)) {
483 vlan = qemu_find_bt_vlan(strtol(str + 9, &endp, 0));
484 if (*endp)
485 vlan = 0;
487 } else
488 vlan = qemu_find_bt_vlan(0);
489 if (vlan)
490 return bt_new_hci(vlan);
493 fprintf(stderr, "qemu: Unknown bluetooth HCI `%s'.\n", str);
495 return 0;
498 static int bt_hci_parse(const char *str)
500 struct HCIInfo *hci;
501 bdaddr_t bdaddr;
503 if (nb_hcis >= MAX_NICS) {
504 fprintf(stderr, "qemu: Too many bluetooth HCIs (max %i).\n", MAX_NICS);
505 return -1;
508 hci = hci_init(str);
509 if (!hci)
510 return -1;
512 bdaddr.b[0] = 0x52;
513 bdaddr.b[1] = 0x54;
514 bdaddr.b[2] = 0x00;
515 bdaddr.b[3] = 0x12;
516 bdaddr.b[4] = 0x34;
517 bdaddr.b[5] = 0x56 + nb_hcis;
518 hci->bdaddr_set(hci, bdaddr.b);
520 hci_table[nb_hcis++] = hci;
522 return 0;
525 static void bt_vhci_add(int vlan_id)
527 struct bt_scatternet_s *vlan = qemu_find_bt_vlan(vlan_id);
529 if (!vlan->slave)
530 fprintf(stderr, "qemu: warning: adding a VHCI to "
531 "an empty scatternet %i\n", vlan_id);
533 bt_vhci_init(bt_new_hci(vlan));
536 static struct bt_device_s *bt_device_add(const char *opt)
538 struct bt_scatternet_s *vlan;
539 int vlan_id = 0;
540 char *endp = strstr(opt, ",vlan=");
541 int len = (endp ? endp - opt : strlen(opt)) + 1;
542 char devname[10];
544 pstrcpy(devname, MIN(sizeof(devname), len), opt);
546 if (endp) {
547 vlan_id = strtol(endp + 6, &endp, 0);
548 if (*endp) {
549 fprintf(stderr, "qemu: unrecognised bluetooth vlan Id\n");
550 return 0;
554 vlan = qemu_find_bt_vlan(vlan_id);
556 if (!vlan->slave)
557 fprintf(stderr, "qemu: warning: adding a slave device to "
558 "an empty scatternet %i\n", vlan_id);
560 if (!strcmp(devname, "keyboard"))
561 return bt_keyboard_init(vlan);
563 fprintf(stderr, "qemu: unsupported bluetooth device `%s'\n", devname);
564 return 0;
567 static int bt_parse(const char *opt)
569 const char *endp, *p;
570 int vlan;
572 if (strstart(opt, "hci", &endp)) {
573 if (!*endp || *endp == ',') {
574 if (*endp)
575 if (!strstart(endp, ",vlan=", 0))
576 opt = endp + 1;
578 return bt_hci_parse(opt);
580 } else if (strstart(opt, "vhci", &endp)) {
581 if (!*endp || *endp == ',') {
582 if (*endp) {
583 if (strstart(endp, ",vlan=", &p)) {
584 vlan = strtol(p, (char **) &endp, 0);
585 if (*endp) {
586 fprintf(stderr, "qemu: bad scatternet '%s'\n", p);
587 return 1;
589 } else {
590 fprintf(stderr, "qemu: bad parameter '%s'\n", endp + 1);
591 return 1;
593 } else
594 vlan = 0;
596 bt_vhci_add(vlan);
597 return 0;
599 } else if (strstart(opt, "device:", &endp))
600 return !bt_device_add(endp);
602 fprintf(stderr, "qemu: bad bluetooth parameter '%s'\n", opt);
603 return 1;
606 /***********************************************************/
607 /* QEMU Block devices */
609 #define HD_ALIAS "index=%d,media=disk"
610 #define CDROM_ALIAS "index=2,media=cdrom"
611 #define FD_ALIAS "index=%d,if=floppy"
612 #define PFLASH_ALIAS "if=pflash"
613 #define MTD_ALIAS "if=mtd"
614 #define SD_ALIAS "index=0,if=sd"
616 static int drive_init_func(QemuOpts *opts, void *opaque)
618 int *use_scsi = opaque;
619 int fatal_error = 0;
621 if (drive_init(opts, *use_scsi, &fatal_error) == NULL) {
622 if (fatal_error)
623 return 1;
625 return 0;
628 static int drive_enable_snapshot(QemuOpts *opts, void *opaque)
630 if (NULL == qemu_opt_get(opts, "snapshot")) {
631 qemu_opt_set(opts, "snapshot", "on");
633 return 0;
636 void qemu_register_boot_set(QEMUBootSetHandler *func, void *opaque)
638 boot_set_handler = func;
639 boot_set_opaque = opaque;
642 int qemu_boot_set(const char *boot_devices)
644 if (!boot_set_handler) {
645 return -EINVAL;
647 return boot_set_handler(boot_set_opaque, boot_devices);
650 static void validate_bootdevices(char *devices)
652 /* We just do some generic consistency checks */
653 const char *p;
654 int bitmap = 0;
656 for (p = devices; *p != '\0'; p++) {
657 /* Allowed boot devices are:
658 * a-b: floppy disk drives
659 * c-f: IDE disk drives
660 * g-m: machine implementation dependant drives
661 * n-p: network devices
662 * It's up to each machine implementation to check if the given boot
663 * devices match the actual hardware implementation and firmware
664 * features.
666 if (*p < 'a' || *p > 'p') {
667 fprintf(stderr, "Invalid boot device '%c'\n", *p);
668 exit(1);
670 if (bitmap & (1 << (*p - 'a'))) {
671 fprintf(stderr, "Boot device '%c' was given twice\n", *p);
672 exit(1);
674 bitmap |= 1 << (*p - 'a');
678 static void restore_boot_devices(void *opaque)
680 char *standard_boot_devices = opaque;
681 static int first = 1;
683 /* Restore boot order and remove ourselves after the first boot */
684 if (first) {
685 first = 0;
686 return;
689 qemu_boot_set(standard_boot_devices);
691 qemu_unregister_reset(restore_boot_devices, standard_boot_devices);
692 qemu_free(standard_boot_devices);
695 static void numa_add(const char *optarg)
697 char option[128];
698 char *endptr;
699 unsigned long long value, endvalue;
700 int nodenr;
702 optarg = get_opt_name(option, 128, optarg, ',') + 1;
703 if (!strcmp(option, "node")) {
704 if (get_param_value(option, 128, "nodeid", optarg) == 0) {
705 nodenr = nb_numa_nodes;
706 } else {
707 nodenr = strtoull(option, NULL, 10);
710 if (get_param_value(option, 128, "mem", optarg) == 0) {
711 node_mem[nodenr] = 0;
712 } else {
713 ssize_t sval;
714 sval = strtosz(option, NULL);
715 if (sval < 0) {
716 fprintf(stderr, "qemu: invalid numa mem size: %s\n", optarg);
717 exit(1);
719 node_mem[nodenr] = sval;
721 if (get_param_value(option, 128, "cpus", optarg) == 0) {
722 node_cpumask[nodenr] = 0;
723 } else {
724 value = strtoull(option, &endptr, 10);
725 if (value >= 64) {
726 value = 63;
727 fprintf(stderr, "only 64 CPUs in NUMA mode supported.\n");
728 } else {
729 if (*endptr == '-') {
730 endvalue = strtoull(endptr+1, &endptr, 10);
731 if (endvalue >= 63) {
732 endvalue = 62;
733 fprintf(stderr,
734 "only 63 CPUs in NUMA mode supported.\n");
736 value = (2ULL << endvalue) - (1ULL << value);
737 } else {
738 value = 1ULL << value;
741 node_cpumask[nodenr] = value;
743 nb_numa_nodes++;
745 return;
748 static void smp_parse(const char *optarg)
750 int smp, sockets = 0, threads = 0, cores = 0;
751 char *endptr;
752 char option[128];
754 smp = strtoul(optarg, &endptr, 10);
755 if (endptr != optarg) {
756 if (*endptr == ',') {
757 endptr++;
760 if (get_param_value(option, 128, "sockets", endptr) != 0)
761 sockets = strtoull(option, NULL, 10);
762 if (get_param_value(option, 128, "cores", endptr) != 0)
763 cores = strtoull(option, NULL, 10);
764 if (get_param_value(option, 128, "threads", endptr) != 0)
765 threads = strtoull(option, NULL, 10);
766 if (get_param_value(option, 128, "maxcpus", endptr) != 0)
767 max_cpus = strtoull(option, NULL, 10);
769 /* compute missing values, prefer sockets over cores over threads */
770 if (smp == 0 || sockets == 0) {
771 sockets = sockets > 0 ? sockets : 1;
772 cores = cores > 0 ? cores : 1;
773 threads = threads > 0 ? threads : 1;
774 if (smp == 0) {
775 smp = cores * threads * sockets;
777 } else {
778 if (cores == 0) {
779 threads = threads > 0 ? threads : 1;
780 cores = smp / (sockets * threads);
781 } else {
782 threads = smp / (cores * sockets);
785 smp_cpus = smp;
786 smp_cores = cores > 0 ? cores : 1;
787 smp_threads = threads > 0 ? threads : 1;
788 if (max_cpus == 0)
789 max_cpus = smp_cpus;
792 /***********************************************************/
793 /* USB devices */
795 static int usb_device_add(const char *devname)
797 const char *p;
798 USBDevice *dev = NULL;
800 if (!usb_enabled)
801 return -1;
803 /* drivers with .usbdevice_name entry in USBDeviceInfo */
804 dev = usbdevice_create(devname);
805 if (dev)
806 goto done;
808 /* the other ones */
809 if (strstart(devname, "host:", &p)) {
810 dev = usb_host_device_open(p);
811 } else if (!strcmp(devname, "bt") || strstart(devname, "bt:", &p)) {
812 dev = usb_bt_init(devname[2] ? hci_init(p) :
813 bt_new_hci(qemu_find_bt_vlan(0)));
814 } else {
815 return -1;
817 if (!dev)
818 return -1;
820 done:
821 return 0;
824 static int usb_device_del(const char *devname)
826 int bus_num, addr;
827 const char *p;
829 if (strstart(devname, "host:", &p))
830 return usb_host_device_close(p);
832 if (!usb_enabled)
833 return -1;
835 p = strchr(devname, '.');
836 if (!p)
837 return -1;
838 bus_num = strtoul(devname, NULL, 0);
839 addr = strtoul(p + 1, NULL, 0);
841 return usb_device_delete_addr(bus_num, addr);
844 static int usb_parse(const char *cmdline)
846 int r;
847 r = usb_device_add(cmdline);
848 if (r < 0) {
849 fprintf(stderr, "qemu: could not add USB device '%s'\n", cmdline);
851 return r;
854 void do_usb_add(Monitor *mon, const QDict *qdict)
856 const char *devname = qdict_get_str(qdict, "devname");
857 if (usb_device_add(devname) < 0) {
858 error_report("could not add USB device '%s'", devname);
862 void do_usb_del(Monitor *mon, const QDict *qdict)
864 const char *devname = qdict_get_str(qdict, "devname");
865 if (usb_device_del(devname) < 0) {
866 error_report("could not delete USB device '%s'", devname);
870 /***********************************************************/
871 /* PCMCIA/Cardbus */
873 static struct pcmcia_socket_entry_s {
874 PCMCIASocket *socket;
875 struct pcmcia_socket_entry_s *next;
876 } *pcmcia_sockets = 0;
878 void pcmcia_socket_register(PCMCIASocket *socket)
880 struct pcmcia_socket_entry_s *entry;
882 entry = qemu_malloc(sizeof(struct pcmcia_socket_entry_s));
883 entry->socket = socket;
884 entry->next = pcmcia_sockets;
885 pcmcia_sockets = entry;
888 void pcmcia_socket_unregister(PCMCIASocket *socket)
890 struct pcmcia_socket_entry_s *entry, **ptr;
892 ptr = &pcmcia_sockets;
893 for (entry = *ptr; entry; ptr = &entry->next, entry = *ptr)
894 if (entry->socket == socket) {
895 *ptr = entry->next;
896 qemu_free(entry);
900 void pcmcia_info(Monitor *mon)
902 struct pcmcia_socket_entry_s *iter;
904 if (!pcmcia_sockets)
905 monitor_printf(mon, "No PCMCIA sockets\n");
907 for (iter = pcmcia_sockets; iter; iter = iter->next)
908 monitor_printf(mon, "%s: %s\n", iter->socket->slot_string,
909 iter->socket->attached ? iter->socket->card_string :
910 "Empty");
913 /***********************************************************/
914 /* I/O handling */
916 typedef struct IOHandlerRecord {
917 int fd;
918 IOCanReadHandler *fd_read_poll;
919 IOHandler *fd_read;
920 IOHandler *fd_write;
921 int deleted;
922 void *opaque;
923 /* temporary data */
924 struct pollfd *ufd;
925 QLIST_ENTRY(IOHandlerRecord) next;
926 } IOHandlerRecord;
928 static QLIST_HEAD(, IOHandlerRecord) io_handlers =
929 QLIST_HEAD_INITIALIZER(io_handlers);
932 /* XXX: fd_read_poll should be suppressed, but an API change is
933 necessary in the character devices to suppress fd_can_read(). */
934 int qemu_set_fd_handler2(int fd,
935 IOCanReadHandler *fd_read_poll,
936 IOHandler *fd_read,
937 IOHandler *fd_write,
938 void *opaque)
940 IOHandlerRecord *ioh;
942 if (!fd_read && !fd_write) {
943 QLIST_FOREACH(ioh, &io_handlers, next) {
944 if (ioh->fd == fd) {
945 ioh->deleted = 1;
946 break;
949 } else {
950 QLIST_FOREACH(ioh, &io_handlers, next) {
951 if (ioh->fd == fd)
952 goto found;
954 ioh = qemu_mallocz(sizeof(IOHandlerRecord));
955 QLIST_INSERT_HEAD(&io_handlers, ioh, next);
956 found:
957 ioh->fd = fd;
958 ioh->fd_read_poll = fd_read_poll;
959 ioh->fd_read = fd_read;
960 ioh->fd_write = fd_write;
961 ioh->opaque = opaque;
962 ioh->deleted = 0;
964 return 0;
967 int qemu_set_fd_handler(int fd,
968 IOHandler *fd_read,
969 IOHandler *fd_write,
970 void *opaque)
972 return qemu_set_fd_handler2(fd, NULL, fd_read, fd_write, opaque);
975 /***********************************************************/
976 /* machine registration */
978 static QEMUMachine *first_machine = NULL;
979 QEMUMachine *current_machine = NULL;
981 int qemu_register_machine(QEMUMachine *m)
983 QEMUMachine **pm;
984 pm = &first_machine;
985 while (*pm != NULL)
986 pm = &(*pm)->next;
987 m->next = NULL;
988 *pm = m;
989 return 0;
992 static QEMUMachine *find_machine(const char *name)
994 QEMUMachine *m;
996 for(m = first_machine; m != NULL; m = m->next) {
997 if (!strcmp(m->name, name))
998 return m;
999 if (m->alias && !strcmp(m->alias, name))
1000 return m;
1002 return NULL;
1005 static QEMUMachine *find_default_machine(void)
1007 QEMUMachine *m;
1009 for(m = first_machine; m != NULL; m = m->next) {
1010 if (m->is_default) {
1011 return m;
1014 return NULL;
1017 /***********************************************************/
1018 /* main execution loop */
1020 static void gui_update(void *opaque)
1022 uint64_t interval = GUI_REFRESH_INTERVAL;
1023 DisplayState *ds = opaque;
1024 DisplayChangeListener *dcl = ds->listeners;
1026 qemu_flush_coalesced_mmio_buffer();
1027 dpy_refresh(ds);
1029 while (dcl != NULL) {
1030 if (dcl->gui_timer_interval &&
1031 dcl->gui_timer_interval < interval)
1032 interval = dcl->gui_timer_interval;
1033 dcl = dcl->next;
1035 qemu_mod_timer(ds->gui_timer, interval + qemu_get_clock(rt_clock));
1038 static void nographic_update(void *opaque)
1040 uint64_t interval = GUI_REFRESH_INTERVAL;
1042 qemu_flush_coalesced_mmio_buffer();
1043 qemu_mod_timer(nographic_timer, interval + qemu_get_clock(rt_clock));
1046 struct vm_change_state_entry {
1047 VMChangeStateHandler *cb;
1048 void *opaque;
1049 QLIST_ENTRY (vm_change_state_entry) entries;
1052 static QLIST_HEAD(vm_change_state_head, vm_change_state_entry) vm_change_state_head;
1054 VMChangeStateEntry *qemu_add_vm_change_state_handler(VMChangeStateHandler *cb,
1055 void *opaque)
1057 VMChangeStateEntry *e;
1059 e = qemu_mallocz(sizeof (*e));
1061 e->cb = cb;
1062 e->opaque = opaque;
1063 QLIST_INSERT_HEAD(&vm_change_state_head, e, entries);
1064 return e;
1067 void qemu_del_vm_change_state_handler(VMChangeStateEntry *e)
1069 QLIST_REMOVE (e, entries);
1070 qemu_free (e);
1073 void vm_state_notify(int running, int reason)
1075 VMChangeStateEntry *e;
1077 for (e = vm_change_state_head.lh_first; e; e = e->entries.le_next) {
1078 e->cb(e->opaque, running, reason);
1082 void vm_start(void)
1084 if (!vm_running) {
1085 cpu_enable_ticks();
1086 vm_running = 1;
1087 vm_state_notify(1, 0);
1088 resume_all_vcpus();
1089 monitor_protocol_event(QEVENT_RESUME, NULL);
1093 /* reset/shutdown handler */
1095 typedef struct QEMUResetEntry {
1096 QTAILQ_ENTRY(QEMUResetEntry) entry;
1097 QEMUResetHandler *func;
1098 void *opaque;
1099 } QEMUResetEntry;
1101 static QTAILQ_HEAD(reset_handlers, QEMUResetEntry) reset_handlers =
1102 QTAILQ_HEAD_INITIALIZER(reset_handlers);
1103 static int reset_requested;
1104 static int shutdown_requested;
1105 static int powerdown_requested;
1106 int debug_requested;
1107 int vmstop_requested;
1109 int qemu_shutdown_requested(void)
1111 int r = shutdown_requested;
1112 shutdown_requested = 0;
1113 return r;
1116 int qemu_reset_requested(void)
1118 int r = reset_requested;
1119 reset_requested = 0;
1120 return r;
1123 int qemu_powerdown_requested(void)
1125 int r = powerdown_requested;
1126 powerdown_requested = 0;
1127 return r;
1130 static int qemu_debug_requested(void)
1132 int r = debug_requested;
1133 debug_requested = 0;
1134 return r;
1137 static int qemu_vmstop_requested(void)
1139 int r = vmstop_requested;
1140 vmstop_requested = 0;
1141 return r;
1144 void qemu_register_reset(QEMUResetHandler *func, void *opaque)
1146 QEMUResetEntry *re = qemu_mallocz(sizeof(QEMUResetEntry));
1148 re->func = func;
1149 re->opaque = opaque;
1150 QTAILQ_INSERT_TAIL(&reset_handlers, re, entry);
1153 void qemu_unregister_reset(QEMUResetHandler *func, void *opaque)
1155 QEMUResetEntry *re;
1157 QTAILQ_FOREACH(re, &reset_handlers, entry) {
1158 if (re->func == func && re->opaque == opaque) {
1159 QTAILQ_REMOVE(&reset_handlers, re, entry);
1160 qemu_free(re);
1161 return;
1166 void qemu_system_reset(void)
1168 QEMUResetEntry *re, *nre;
1170 /* reset all devices */
1171 QTAILQ_FOREACH_SAFE(re, &reset_handlers, entry, nre) {
1172 re->func(re->opaque);
1174 monitor_protocol_event(QEVENT_RESET, NULL);
1175 cpu_synchronize_all_post_reset();
1178 void qemu_system_reset_request(void)
1180 if (no_reboot) {
1181 shutdown_requested = 1;
1182 } else {
1183 reset_requested = 1;
1185 qemu_notify_event();
1188 void qemu_system_shutdown_request(void)
1190 shutdown_requested = 1;
1191 qemu_notify_event();
1194 void qemu_system_powerdown_request(void)
1196 powerdown_requested = 1;
1197 qemu_notify_event();
1200 void main_loop_wait(int nonblocking)
1202 IOHandlerRecord *ioh;
1203 fd_set rfds, wfds, xfds;
1204 int ret, nfds;
1205 struct timeval tv;
1206 int timeout;
1208 if (nonblocking)
1209 timeout = 0;
1210 else {
1211 timeout = qemu_calculate_timeout();
1212 qemu_bh_update_timeout(&timeout);
1215 os_host_main_loop_wait(&timeout);
1217 /* poll any events */
1218 /* XXX: separate device handlers from system ones */
1219 nfds = -1;
1220 FD_ZERO(&rfds);
1221 FD_ZERO(&wfds);
1222 FD_ZERO(&xfds);
1223 QLIST_FOREACH(ioh, &io_handlers, next) {
1224 if (ioh->deleted)
1225 continue;
1226 if (ioh->fd_read &&
1227 (!ioh->fd_read_poll ||
1228 ioh->fd_read_poll(ioh->opaque) != 0)) {
1229 FD_SET(ioh->fd, &rfds);
1230 if (ioh->fd > nfds)
1231 nfds = ioh->fd;
1233 if (ioh->fd_write) {
1234 FD_SET(ioh->fd, &wfds);
1235 if (ioh->fd > nfds)
1236 nfds = ioh->fd;
1240 tv.tv_sec = timeout / 1000;
1241 tv.tv_usec = (timeout % 1000) * 1000;
1243 slirp_select_fill(&nfds, &rfds, &wfds, &xfds);
1245 qemu_mutex_unlock_iothread();
1246 ret = select(nfds + 1, &rfds, &wfds, &xfds, &tv);
1247 qemu_mutex_lock_iothread();
1248 if (ret > 0) {
1249 IOHandlerRecord *pioh;
1251 QLIST_FOREACH_SAFE(ioh, &io_handlers, next, pioh) {
1252 if (ioh->deleted) {
1253 QLIST_REMOVE(ioh, next);
1254 qemu_free(ioh);
1255 continue;
1257 if (ioh->fd_read && FD_ISSET(ioh->fd, &rfds)) {
1258 ioh->fd_read(ioh->opaque);
1260 if (ioh->fd_write && FD_ISSET(ioh->fd, &wfds)) {
1261 ioh->fd_write(ioh->opaque);
1266 slirp_select_poll(&rfds, &wfds, &xfds, (ret < 0));
1268 qemu_run_all_timers();
1270 /* Check bottom-halves last in case any of the earlier events triggered
1271 them. */
1272 qemu_bh_poll();
1276 static int vm_can_run(void)
1278 if (powerdown_requested)
1279 return 0;
1280 if (reset_requested)
1281 return 0;
1282 if (shutdown_requested)
1283 return 0;
1284 if (debug_requested)
1285 return 0;
1286 return 1;
1289 qemu_irq qemu_system_powerdown;
1291 static void main_loop(void)
1293 int r;
1295 qemu_main_loop_start();
1297 for (;;) {
1298 do {
1299 bool nonblocking = false;
1300 #ifdef CONFIG_PROFILER
1301 int64_t ti;
1302 #endif
1303 #ifndef CONFIG_IOTHREAD
1304 nonblocking = cpu_exec_all();
1305 #endif
1306 #ifdef CONFIG_PROFILER
1307 ti = profile_getclock();
1308 #endif
1309 main_loop_wait(nonblocking);
1310 #ifdef CONFIG_PROFILER
1311 dev_time += profile_getclock() - ti;
1312 #endif
1313 } while (vm_can_run());
1315 if ((r = qemu_debug_requested())) {
1316 vm_stop(r);
1318 if (qemu_shutdown_requested()) {
1319 monitor_protocol_event(QEVENT_SHUTDOWN, NULL);
1320 if (no_shutdown) {
1321 vm_stop(0);
1322 no_shutdown = 0;
1323 } else
1324 break;
1326 if (qemu_reset_requested()) {
1327 pause_all_vcpus();
1328 qemu_system_reset();
1329 resume_all_vcpus();
1331 if (qemu_powerdown_requested()) {
1332 monitor_protocol_event(QEVENT_POWERDOWN, NULL);
1333 qemu_irq_raise(qemu_system_powerdown);
1335 if ((r = qemu_vmstop_requested())) {
1336 vm_stop(r);
1339 bdrv_close_all();
1340 pause_all_vcpus();
1343 static void version(void)
1345 printf("QEMU emulator version " QEMU_VERSION QEMU_PKGVERSION ", Copyright (c) 2003-2008 Fabrice Bellard\n");
1348 static void help(int exitcode)
1350 const char *options_help =
1351 #define DEF(option, opt_arg, opt_enum, opt_help, arch_mask) \
1352 opt_help
1353 #define DEFHEADING(text) stringify(text) "\n"
1354 #include "qemu-options.def"
1355 #undef DEF
1356 #undef DEFHEADING
1357 #undef GEN_DOCS
1359 version();
1360 printf("usage: %s [options] [disk_image]\n"
1361 "\n"
1362 "'disk_image' is a raw hard disk image for IDE hard disk 0\n"
1363 "\n"
1364 "%s\n"
1365 "During emulation, the following keys are useful:\n"
1366 "ctrl-alt-f toggle full screen\n"
1367 "ctrl-alt-n switch to virtual console 'n'\n"
1368 "ctrl-alt toggle mouse and keyboard grab\n"
1369 "\n"
1370 "When using -nographic, press 'ctrl-a h' to get some help.\n",
1371 "qemu",
1372 options_help);
1373 exit(exitcode);
1376 #define HAS_ARG 0x0001
1378 typedef struct QEMUOption {
1379 const char *name;
1380 int flags;
1381 int index;
1382 uint32_t arch_mask;
1383 } QEMUOption;
1385 static const QEMUOption qemu_options[] = {
1386 { "h", 0, QEMU_OPTION_h, QEMU_ARCH_ALL },
1387 #define DEF(option, opt_arg, opt_enum, opt_help, arch_mask) \
1388 { option, opt_arg, opt_enum, arch_mask },
1389 #define DEFHEADING(text)
1390 #include "qemu-options.def"
1391 #undef DEF
1392 #undef DEFHEADING
1393 #undef GEN_DOCS
1394 { NULL },
1396 static void select_vgahw (const char *p)
1398 const char *opts;
1400 default_vga = 0;
1401 vga_interface_type = VGA_NONE;
1402 if (strstart(p, "std", &opts)) {
1403 vga_interface_type = VGA_STD;
1404 } else if (strstart(p, "cirrus", &opts)) {
1405 vga_interface_type = VGA_CIRRUS;
1406 } else if (strstart(p, "vmware", &opts)) {
1407 vga_interface_type = VGA_VMWARE;
1408 } else if (strstart(p, "xenfb", &opts)) {
1409 vga_interface_type = VGA_XENFB;
1410 } else if (!strstart(p, "none", &opts)) {
1411 invalid_vga:
1412 fprintf(stderr, "Unknown vga type: %s\n", p);
1413 exit(1);
1415 while (*opts) {
1416 const char *nextopt;
1418 if (strstart(opts, ",retrace=", &nextopt)) {
1419 opts = nextopt;
1420 if (strstart(opts, "dumb", &nextopt))
1421 vga_retrace_method = VGA_RETRACE_DUMB;
1422 else if (strstart(opts, "precise", &nextopt))
1423 vga_retrace_method = VGA_RETRACE_PRECISE;
1424 else goto invalid_vga;
1425 } else goto invalid_vga;
1426 opts = nextopt;
1430 static int balloon_parse(const char *arg)
1432 QemuOpts *opts;
1434 if (strcmp(arg, "none") == 0) {
1435 return 0;
1438 if (!strncmp(arg, "virtio", 6)) {
1439 if (arg[6] == ',') {
1440 /* have params -> parse them */
1441 opts = qemu_opts_parse(qemu_find_opts("device"), arg+7, 0);
1442 if (!opts)
1443 return -1;
1444 } else {
1445 /* create empty opts */
1446 opts = qemu_opts_create(qemu_find_opts("device"), NULL, 0);
1448 qemu_opt_set(opts, "driver", "virtio-balloon-pci");
1449 return 0;
1452 return -1;
1455 char *qemu_find_file(int type, const char *name)
1457 int len;
1458 const char *subdir;
1459 char *buf;
1461 /* If name contains path separators then try it as a straight path. */
1462 if ((strchr(name, '/') || strchr(name, '\\'))
1463 && access(name, R_OK) == 0) {
1464 return qemu_strdup(name);
1466 switch (type) {
1467 case QEMU_FILE_TYPE_BIOS:
1468 subdir = "";
1469 break;
1470 case QEMU_FILE_TYPE_KEYMAP:
1471 subdir = "keymaps/";
1472 break;
1473 default:
1474 abort();
1476 len = strlen(data_dir) + strlen(name) + strlen(subdir) + 2;
1477 buf = qemu_mallocz(len);
1478 snprintf(buf, len, "%s/%s%s", data_dir, subdir, name);
1479 if (access(buf, R_OK)) {
1480 qemu_free(buf);
1481 return NULL;
1483 return buf;
1486 static int device_help_func(QemuOpts *opts, void *opaque)
1488 return qdev_device_help(opts);
1491 static int device_init_func(QemuOpts *opts, void *opaque)
1493 DeviceState *dev;
1495 dev = qdev_device_add(opts);
1496 if (!dev)
1497 return -1;
1498 return 0;
1501 static int chardev_init_func(QemuOpts *opts, void *opaque)
1503 CharDriverState *chr;
1505 chr = qemu_chr_open_opts(opts, NULL);
1506 if (!chr)
1507 return -1;
1508 return 0;
1511 #ifdef CONFIG_VIRTFS
1512 static int fsdev_init_func(QemuOpts *opts, void *opaque)
1514 int ret;
1515 ret = qemu_fsdev_add(opts);
1517 return ret;
1519 #endif
1521 static int mon_init_func(QemuOpts *opts, void *opaque)
1523 CharDriverState *chr;
1524 const char *chardev;
1525 const char *mode;
1526 int flags;
1528 mode = qemu_opt_get(opts, "mode");
1529 if (mode == NULL) {
1530 mode = "readline";
1532 if (strcmp(mode, "readline") == 0) {
1533 flags = MONITOR_USE_READLINE;
1534 } else if (strcmp(mode, "control") == 0) {
1535 flags = MONITOR_USE_CONTROL;
1536 } else {
1537 fprintf(stderr, "unknown monitor mode \"%s\"\n", mode);
1538 exit(1);
1541 if (qemu_opt_get_bool(opts, "pretty", 0))
1542 flags |= MONITOR_USE_PRETTY;
1544 if (qemu_opt_get_bool(opts, "default", 0))
1545 flags |= MONITOR_IS_DEFAULT;
1547 chardev = qemu_opt_get(opts, "chardev");
1548 chr = qemu_chr_find(chardev);
1549 if (chr == NULL) {
1550 fprintf(stderr, "chardev \"%s\" not found\n", chardev);
1551 exit(1);
1554 monitor_init(chr, flags);
1555 return 0;
1558 static void monitor_parse(const char *optarg, const char *mode)
1560 static int monitor_device_index = 0;
1561 QemuOpts *opts;
1562 const char *p;
1563 char label[32];
1564 int def = 0;
1566 if (strstart(optarg, "chardev:", &p)) {
1567 snprintf(label, sizeof(label), "%s", p);
1568 } else {
1569 snprintf(label, sizeof(label), "compat_monitor%d",
1570 monitor_device_index);
1571 if (monitor_device_index == 0) {
1572 def = 1;
1574 opts = qemu_chr_parse_compat(label, optarg);
1575 if (!opts) {
1576 fprintf(stderr, "parse error: %s\n", optarg);
1577 exit(1);
1581 opts = qemu_opts_create(qemu_find_opts("mon"), label, 1);
1582 if (!opts) {
1583 fprintf(stderr, "duplicate chardev: %s\n", label);
1584 exit(1);
1586 qemu_opt_set(opts, "mode", mode);
1587 qemu_opt_set(opts, "chardev", label);
1588 if (def)
1589 qemu_opt_set(opts, "default", "on");
1590 monitor_device_index++;
1593 struct device_config {
1594 enum {
1595 DEV_USB, /* -usbdevice */
1596 DEV_BT, /* -bt */
1597 DEV_SERIAL, /* -serial */
1598 DEV_PARALLEL, /* -parallel */
1599 DEV_VIRTCON, /* -virtioconsole */
1600 DEV_DEBUGCON, /* -debugcon */
1601 } type;
1602 const char *cmdline;
1603 QTAILQ_ENTRY(device_config) next;
1605 QTAILQ_HEAD(, device_config) device_configs = QTAILQ_HEAD_INITIALIZER(device_configs);
1607 static void add_device_config(int type, const char *cmdline)
1609 struct device_config *conf;
1611 conf = qemu_mallocz(sizeof(*conf));
1612 conf->type = type;
1613 conf->cmdline = cmdline;
1614 QTAILQ_INSERT_TAIL(&device_configs, conf, next);
1617 static int foreach_device_config(int type, int (*func)(const char *cmdline))
1619 struct device_config *conf;
1620 int rc;
1622 QTAILQ_FOREACH(conf, &device_configs, next) {
1623 if (conf->type != type)
1624 continue;
1625 rc = func(conf->cmdline);
1626 if (0 != rc)
1627 return rc;
1629 return 0;
1632 static int serial_parse(const char *devname)
1634 static int index = 0;
1635 char label[32];
1637 if (strcmp(devname, "none") == 0)
1638 return 0;
1639 if (index == MAX_SERIAL_PORTS) {
1640 fprintf(stderr, "qemu: too many serial ports\n");
1641 exit(1);
1643 snprintf(label, sizeof(label), "serial%d", index);
1644 serial_hds[index] = qemu_chr_open(label, devname, NULL);
1645 if (!serial_hds[index]) {
1646 fprintf(stderr, "qemu: could not open serial device '%s': %s\n",
1647 devname, strerror(errno));
1648 return -1;
1650 index++;
1651 return 0;
1654 static int parallel_parse(const char *devname)
1656 static int index = 0;
1657 char label[32];
1659 if (strcmp(devname, "none") == 0)
1660 return 0;
1661 if (index == MAX_PARALLEL_PORTS) {
1662 fprintf(stderr, "qemu: too many parallel ports\n");
1663 exit(1);
1665 snprintf(label, sizeof(label), "parallel%d", index);
1666 parallel_hds[index] = qemu_chr_open(label, devname, NULL);
1667 if (!parallel_hds[index]) {
1668 fprintf(stderr, "qemu: could not open parallel device '%s': %s\n",
1669 devname, strerror(errno));
1670 return -1;
1672 index++;
1673 return 0;
1676 static int virtcon_parse(const char *devname)
1678 QemuOptsList *device = qemu_find_opts("device");
1679 static int index = 0;
1680 char label[32];
1681 QemuOpts *bus_opts, *dev_opts;
1683 if (strcmp(devname, "none") == 0)
1684 return 0;
1685 if (index == MAX_VIRTIO_CONSOLES) {
1686 fprintf(stderr, "qemu: too many virtio consoles\n");
1687 exit(1);
1690 bus_opts = qemu_opts_create(device, NULL, 0);
1691 qemu_opt_set(bus_opts, "driver", "virtio-serial");
1693 dev_opts = qemu_opts_create(device, NULL, 0);
1694 qemu_opt_set(dev_opts, "driver", "virtconsole");
1696 snprintf(label, sizeof(label), "virtcon%d", index);
1697 virtcon_hds[index] = qemu_chr_open(label, devname, NULL);
1698 if (!virtcon_hds[index]) {
1699 fprintf(stderr, "qemu: could not open virtio console '%s': %s\n",
1700 devname, strerror(errno));
1701 return -1;
1703 qemu_opt_set(dev_opts, "chardev", label);
1705 index++;
1706 return 0;
1709 static int debugcon_parse(const char *devname)
1711 QemuOpts *opts;
1713 if (!qemu_chr_open("debugcon", devname, NULL)) {
1714 exit(1);
1716 opts = qemu_opts_create(qemu_find_opts("device"), "debugcon", 1);
1717 if (!opts) {
1718 fprintf(stderr, "qemu: already have a debugcon device\n");
1719 exit(1);
1721 qemu_opt_set(opts, "driver", "isa-debugcon");
1722 qemu_opt_set(opts, "chardev", "debugcon");
1723 return 0;
1726 void qemu_add_exit_notifier(Notifier *notify)
1728 notifier_list_add(&exit_notifiers, notify);
1731 void qemu_remove_exit_notifier(Notifier *notify)
1733 notifier_list_remove(&exit_notifiers, notify);
1736 static void qemu_run_exit_notifiers(void)
1738 notifier_list_notify(&exit_notifiers);
1741 static const QEMUOption *lookup_opt(int argc, char **argv,
1742 const char **poptarg, int *poptind)
1744 const QEMUOption *popt;
1745 int optind = *poptind;
1746 char *r = argv[optind];
1747 const char *optarg;
1749 loc_set_cmdline(argv, optind, 1);
1750 optind++;
1751 /* Treat --foo the same as -foo. */
1752 if (r[1] == '-')
1753 r++;
1754 popt = qemu_options;
1755 for(;;) {
1756 if (!popt->name) {
1757 error_report("invalid option");
1758 exit(1);
1760 if (!strcmp(popt->name, r + 1))
1761 break;
1762 popt++;
1764 if (popt->flags & HAS_ARG) {
1765 if (optind >= argc) {
1766 error_report("requires an argument");
1767 exit(1);
1769 optarg = argv[optind++];
1770 loc_set_cmdline(argv, optind - 2, 2);
1771 } else {
1772 optarg = NULL;
1775 *poptarg = optarg;
1776 *poptind = optind;
1778 return popt;
1781 int main(int argc, char **argv, char **envp)
1783 const char *gdbstub_dev = NULL;
1784 int i;
1785 int snapshot, linux_boot;
1786 const char *icount_option = NULL;
1787 const char *initrd_filename;
1788 const char *kernel_filename, *kernel_cmdline;
1789 char boot_devices[33] = "cad"; /* default to HD->floppy->CD-ROM */
1790 DisplayState *ds;
1791 DisplayChangeListener *dcl;
1792 int cyls, heads, secs, translation;
1793 QemuOpts *hda_opts = NULL, *opts;
1794 QemuOptsList *olist;
1795 int optind;
1796 const char *optarg;
1797 const char *loadvm = NULL;
1798 QEMUMachine *machine;
1799 const char *cpu_model;
1800 int tb_size;
1801 const char *pid_file = NULL;
1802 const char *incoming = NULL;
1803 int show_vnc_port = 0;
1804 int defconfig = 1;
1806 #ifdef CONFIG_SIMPLE_TRACE
1807 const char *trace_file = NULL;
1808 #endif
1809 atexit(qemu_run_exit_notifiers);
1810 error_set_progname(argv[0]);
1812 init_clocks();
1814 qemu_cache_utils_init(envp);
1816 QLIST_INIT (&vm_change_state_head);
1817 os_setup_early_signal_handling();
1819 module_call_init(MODULE_INIT_MACHINE);
1820 machine = find_default_machine();
1821 cpu_model = NULL;
1822 initrd_filename = NULL;
1823 ram_size = 0;
1824 snapshot = 0;
1825 kernel_filename = NULL;
1826 kernel_cmdline = "";
1827 cyls = heads = secs = 0;
1828 translation = BIOS_ATA_TRANSLATION_AUTO;
1830 for (i = 0; i < MAX_NODES; i++) {
1831 node_mem[i] = 0;
1832 node_cpumask[i] = 0;
1835 nb_numa_nodes = 0;
1836 nb_nics = 0;
1838 tb_size = 0;
1839 autostart= 1;
1841 /* first pass of option parsing */
1842 optind = 1;
1843 while (optind < argc) {
1844 if (argv[optind][0] != '-') {
1845 /* disk image */
1846 optind++;
1847 continue;
1848 } else {
1849 const QEMUOption *popt;
1851 popt = lookup_opt(argc, argv, &optarg, &optind);
1852 switch (popt->index) {
1853 case QEMU_OPTION_nodefconfig:
1854 defconfig=0;
1855 break;
1860 if (defconfig) {
1861 int ret;
1863 ret = qemu_read_config_file(CONFIG_QEMU_CONFDIR "/qemu.conf");
1864 if (ret < 0 && ret != -ENOENT) {
1865 exit(1);
1868 ret = qemu_read_config_file(arch_config_name);
1869 if (ret < 0 && ret != -ENOENT) {
1870 exit(1);
1873 cpudef_init();
1875 /* second pass of option parsing */
1876 optind = 1;
1877 for(;;) {
1878 if (optind >= argc)
1879 break;
1880 if (argv[optind][0] != '-') {
1881 hda_opts = drive_add(argv[optind++], HD_ALIAS, 0);
1882 } else {
1883 const QEMUOption *popt;
1885 popt = lookup_opt(argc, argv, &optarg, &optind);
1886 if (!(popt->arch_mask & arch_type)) {
1887 printf("Option %s not supported for this target\n", popt->name);
1888 exit(1);
1890 switch(popt->index) {
1891 case QEMU_OPTION_M:
1892 machine = find_machine(optarg);
1893 if (!machine) {
1894 QEMUMachine *m;
1895 printf("Supported machines are:\n");
1896 for(m = first_machine; m != NULL; m = m->next) {
1897 if (m->alias)
1898 printf("%-10s %s (alias of %s)\n",
1899 m->alias, m->desc, m->name);
1900 printf("%-10s %s%s\n",
1901 m->name, m->desc,
1902 m->is_default ? " (default)" : "");
1904 exit(*optarg != '?');
1906 break;
1907 case QEMU_OPTION_cpu:
1908 /* hw initialization will check this */
1909 if (*optarg == '?') {
1910 list_cpus(stdout, &fprintf, optarg);
1911 exit(0);
1912 } else {
1913 cpu_model = optarg;
1915 break;
1916 case QEMU_OPTION_initrd:
1917 initrd_filename = optarg;
1918 break;
1919 case QEMU_OPTION_hda:
1920 if (cyls == 0)
1921 hda_opts = drive_add(optarg, HD_ALIAS, 0);
1922 else
1923 hda_opts = drive_add(optarg, HD_ALIAS
1924 ",cyls=%d,heads=%d,secs=%d%s",
1925 0, cyls, heads, secs,
1926 translation == BIOS_ATA_TRANSLATION_LBA ?
1927 ",trans=lba" :
1928 translation == BIOS_ATA_TRANSLATION_NONE ?
1929 ",trans=none" : "");
1930 break;
1931 case QEMU_OPTION_hdb:
1932 case QEMU_OPTION_hdc:
1933 case QEMU_OPTION_hdd:
1934 drive_add(optarg, HD_ALIAS, popt->index - QEMU_OPTION_hda);
1935 break;
1936 case QEMU_OPTION_drive:
1937 drive_add(NULL, "%s", optarg);
1938 break;
1939 case QEMU_OPTION_set:
1940 if (qemu_set_option(optarg) != 0)
1941 exit(1);
1942 break;
1943 case QEMU_OPTION_global:
1944 if (qemu_global_option(optarg) != 0)
1945 exit(1);
1946 break;
1947 case QEMU_OPTION_mtdblock:
1948 drive_add(optarg, MTD_ALIAS);
1949 break;
1950 case QEMU_OPTION_sd:
1951 drive_add(optarg, SD_ALIAS);
1952 break;
1953 case QEMU_OPTION_pflash:
1954 drive_add(optarg, PFLASH_ALIAS);
1955 break;
1956 case QEMU_OPTION_snapshot:
1957 snapshot = 1;
1958 break;
1959 case QEMU_OPTION_hdachs:
1961 const char *p;
1962 p = optarg;
1963 cyls = strtol(p, (char **)&p, 0);
1964 if (cyls < 1 || cyls > 16383)
1965 goto chs_fail;
1966 if (*p != ',')
1967 goto chs_fail;
1968 p++;
1969 heads = strtol(p, (char **)&p, 0);
1970 if (heads < 1 || heads > 16)
1971 goto chs_fail;
1972 if (*p != ',')
1973 goto chs_fail;
1974 p++;
1975 secs = strtol(p, (char **)&p, 0);
1976 if (secs < 1 || secs > 63)
1977 goto chs_fail;
1978 if (*p == ',') {
1979 p++;
1980 if (!strcmp(p, "none"))
1981 translation = BIOS_ATA_TRANSLATION_NONE;
1982 else if (!strcmp(p, "lba"))
1983 translation = BIOS_ATA_TRANSLATION_LBA;
1984 else if (!strcmp(p, "auto"))
1985 translation = BIOS_ATA_TRANSLATION_AUTO;
1986 else
1987 goto chs_fail;
1988 } else if (*p != '\0') {
1989 chs_fail:
1990 fprintf(stderr, "qemu: invalid physical CHS format\n");
1991 exit(1);
1993 if (hda_opts != NULL) {
1994 char num[16];
1995 snprintf(num, sizeof(num), "%d", cyls);
1996 qemu_opt_set(hda_opts, "cyls", num);
1997 snprintf(num, sizeof(num), "%d", heads);
1998 qemu_opt_set(hda_opts, "heads", num);
1999 snprintf(num, sizeof(num), "%d", secs);
2000 qemu_opt_set(hda_opts, "secs", num);
2001 if (translation == BIOS_ATA_TRANSLATION_LBA)
2002 qemu_opt_set(hda_opts, "trans", "lba");
2003 if (translation == BIOS_ATA_TRANSLATION_NONE)
2004 qemu_opt_set(hda_opts, "trans", "none");
2007 break;
2008 case QEMU_OPTION_numa:
2009 if (nb_numa_nodes >= MAX_NODES) {
2010 fprintf(stderr, "qemu: too many NUMA nodes\n");
2011 exit(1);
2013 numa_add(optarg);
2014 break;
2015 case QEMU_OPTION_nographic:
2016 display_type = DT_NOGRAPHIC;
2017 break;
2018 #ifdef CONFIG_CURSES
2019 case QEMU_OPTION_curses:
2020 display_type = DT_CURSES;
2021 break;
2022 #endif
2023 case QEMU_OPTION_portrait:
2024 graphic_rotate = 1;
2025 break;
2026 case QEMU_OPTION_kernel:
2027 kernel_filename = optarg;
2028 break;
2029 case QEMU_OPTION_append:
2030 kernel_cmdline = optarg;
2031 break;
2032 case QEMU_OPTION_cdrom:
2033 drive_add(optarg, CDROM_ALIAS);
2034 break;
2035 case QEMU_OPTION_boot:
2037 static const char * const params[] = {
2038 "order", "once", "menu", NULL
2040 char buf[sizeof(boot_devices)];
2041 char *standard_boot_devices;
2042 int legacy = 0;
2044 if (!strchr(optarg, '=')) {
2045 legacy = 1;
2046 pstrcpy(buf, sizeof(buf), optarg);
2047 } else if (check_params(buf, sizeof(buf), params, optarg) < 0) {
2048 fprintf(stderr,
2049 "qemu: unknown boot parameter '%s' in '%s'\n",
2050 buf, optarg);
2051 exit(1);
2054 if (legacy ||
2055 get_param_value(buf, sizeof(buf), "order", optarg)) {
2056 validate_bootdevices(buf);
2057 pstrcpy(boot_devices, sizeof(boot_devices), buf);
2059 if (!legacy) {
2060 if (get_param_value(buf, sizeof(buf),
2061 "once", optarg)) {
2062 validate_bootdevices(buf);
2063 standard_boot_devices = qemu_strdup(boot_devices);
2064 pstrcpy(boot_devices, sizeof(boot_devices), buf);
2065 qemu_register_reset(restore_boot_devices,
2066 standard_boot_devices);
2068 if (get_param_value(buf, sizeof(buf),
2069 "menu", optarg)) {
2070 if (!strcmp(buf, "on")) {
2071 boot_menu = 1;
2072 } else if (!strcmp(buf, "off")) {
2073 boot_menu = 0;
2074 } else {
2075 fprintf(stderr,
2076 "qemu: invalid option value '%s'\n",
2077 buf);
2078 exit(1);
2083 break;
2084 case QEMU_OPTION_fda:
2085 case QEMU_OPTION_fdb:
2086 drive_add(optarg, FD_ALIAS, popt->index - QEMU_OPTION_fda);
2087 break;
2088 case QEMU_OPTION_no_fd_bootchk:
2089 fd_bootchk = 0;
2090 break;
2091 case QEMU_OPTION_netdev:
2092 if (net_client_parse(qemu_find_opts("netdev"), optarg) == -1) {
2093 exit(1);
2095 break;
2096 case QEMU_OPTION_net:
2097 if (net_client_parse(qemu_find_opts("net"), optarg) == -1) {
2098 exit(1);
2100 break;
2101 #ifdef CONFIG_SLIRP
2102 case QEMU_OPTION_tftp:
2103 legacy_tftp_prefix = optarg;
2104 break;
2105 case QEMU_OPTION_bootp:
2106 legacy_bootp_filename = optarg;
2107 break;
2108 case QEMU_OPTION_redir:
2109 if (net_slirp_redir(optarg) < 0)
2110 exit(1);
2111 break;
2112 #endif
2113 case QEMU_OPTION_bt:
2114 add_device_config(DEV_BT, optarg);
2115 break;
2116 case QEMU_OPTION_audio_help:
2117 if (!(audio_available())) {
2118 printf("Option %s not supported for this target\n", popt->name);
2119 exit(1);
2121 AUD_help ();
2122 exit (0);
2123 break;
2124 case QEMU_OPTION_soundhw:
2125 if (!(audio_available())) {
2126 printf("Option %s not supported for this target\n", popt->name);
2127 exit(1);
2129 select_soundhw (optarg);
2130 break;
2131 case QEMU_OPTION_h:
2132 help(0);
2133 break;
2134 case QEMU_OPTION_version:
2135 version();
2136 exit(0);
2137 break;
2138 case QEMU_OPTION_m: {
2139 ssize_t value;
2141 value = strtosz(optarg, NULL);
2142 if (value < 0) {
2143 fprintf(stderr, "qemu: invalid ram size: %s\n", optarg);
2144 exit(1);
2147 /* On 32-bit hosts, QEMU is limited by virtual address space */
2148 if (value > (2047 << 20) && HOST_LONG_BITS == 32) {
2149 fprintf(stderr, "qemu: at most 2047 MB RAM can be simulated\n");
2150 exit(1);
2152 if (value != (uint64_t)(ram_addr_t)value) {
2153 fprintf(stderr, "qemu: ram size too large\n");
2154 exit(1);
2156 ram_size = value;
2157 break;
2159 case QEMU_OPTION_mempath:
2160 mem_path = optarg;
2161 break;
2162 #ifdef MAP_POPULATE
2163 case QEMU_OPTION_mem_prealloc:
2164 mem_prealloc = 1;
2165 break;
2166 #endif
2167 case QEMU_OPTION_d:
2168 set_cpu_log(optarg);
2169 break;
2170 case QEMU_OPTION_s:
2171 gdbstub_dev = "tcp::" DEFAULT_GDBSTUB_PORT;
2172 break;
2173 case QEMU_OPTION_gdb:
2174 gdbstub_dev = optarg;
2175 break;
2176 case QEMU_OPTION_L:
2177 data_dir = optarg;
2178 break;
2179 case QEMU_OPTION_bios:
2180 bios_name = optarg;
2181 break;
2182 case QEMU_OPTION_singlestep:
2183 singlestep = 1;
2184 break;
2185 case QEMU_OPTION_S:
2186 autostart = 0;
2187 break;
2188 case QEMU_OPTION_k:
2189 keyboard_layout = optarg;
2190 break;
2191 case QEMU_OPTION_localtime:
2192 rtc_utc = 0;
2193 break;
2194 case QEMU_OPTION_vga:
2195 select_vgahw (optarg);
2196 break;
2197 case QEMU_OPTION_g:
2199 const char *p;
2200 int w, h, depth;
2201 p = optarg;
2202 w = strtol(p, (char **)&p, 10);
2203 if (w <= 0) {
2204 graphic_error:
2205 fprintf(stderr, "qemu: invalid resolution or depth\n");
2206 exit(1);
2208 if (*p != 'x')
2209 goto graphic_error;
2210 p++;
2211 h = strtol(p, (char **)&p, 10);
2212 if (h <= 0)
2213 goto graphic_error;
2214 if (*p == 'x') {
2215 p++;
2216 depth = strtol(p, (char **)&p, 10);
2217 if (depth != 8 && depth != 15 && depth != 16 &&
2218 depth != 24 && depth != 32)
2219 goto graphic_error;
2220 } else if (*p == '\0') {
2221 depth = graphic_depth;
2222 } else {
2223 goto graphic_error;
2226 graphic_width = w;
2227 graphic_height = h;
2228 graphic_depth = depth;
2230 break;
2231 case QEMU_OPTION_echr:
2233 char *r;
2234 term_escape_char = strtol(optarg, &r, 0);
2235 if (r == optarg)
2236 printf("Bad argument to echr\n");
2237 break;
2239 case QEMU_OPTION_monitor:
2240 monitor_parse(optarg, "readline");
2241 default_monitor = 0;
2242 break;
2243 case QEMU_OPTION_qmp:
2244 monitor_parse(optarg, "control");
2245 default_monitor = 0;
2246 break;
2247 case QEMU_OPTION_mon:
2248 opts = qemu_opts_parse(qemu_find_opts("mon"), optarg, 1);
2249 if (!opts) {
2250 exit(1);
2252 default_monitor = 0;
2253 break;
2254 case QEMU_OPTION_chardev:
2255 opts = qemu_opts_parse(qemu_find_opts("chardev"), optarg, 1);
2256 if (!opts) {
2257 exit(1);
2259 break;
2260 case QEMU_OPTION_fsdev:
2261 olist = qemu_find_opts("fsdev");
2262 if (!olist) {
2263 fprintf(stderr, "fsdev is not supported by this qemu build.\n");
2264 exit(1);
2266 opts = qemu_opts_parse(olist, optarg, 1);
2267 if (!opts) {
2268 fprintf(stderr, "parse error: %s\n", optarg);
2269 exit(1);
2271 break;
2272 case QEMU_OPTION_virtfs: {
2273 char *arg_fsdev = NULL;
2274 char *arg_9p = NULL;
2275 int len = 0;
2277 olist = qemu_find_opts("virtfs");
2278 if (!olist) {
2279 fprintf(stderr, "virtfs is not supported by this qemu build.\n");
2280 exit(1);
2282 opts = qemu_opts_parse(olist, optarg, 1);
2283 if (!opts) {
2284 fprintf(stderr, "parse error: %s\n", optarg);
2285 exit(1);
2288 if (qemu_opt_get(opts, "fstype") == NULL ||
2289 qemu_opt_get(opts, "mount_tag") == NULL ||
2290 qemu_opt_get(opts, "path") == NULL ||
2291 qemu_opt_get(opts, "security_model") == NULL) {
2292 fprintf(stderr, "Usage: -virtfs fstype,path=/share_path/,"
2293 "security_model=[mapped|passthrough|none],"
2294 "mnt_tag=tag.\n");
2295 exit(1);
2298 len = strlen(",id=,path=,security_model=");
2299 len += strlen(qemu_opt_get(opts, "fstype"));
2300 len += strlen(qemu_opt_get(opts, "mount_tag"));
2301 len += strlen(qemu_opt_get(opts, "path"));
2302 len += strlen(qemu_opt_get(opts, "security_model"));
2303 arg_fsdev = qemu_malloc((len + 1) * sizeof(*arg_fsdev));
2305 snprintf(arg_fsdev, (len + 1) * sizeof(*arg_fsdev),
2306 "%s,id=%s,path=%s,security_model=%s",
2307 qemu_opt_get(opts, "fstype"),
2308 qemu_opt_get(opts, "mount_tag"),
2309 qemu_opt_get(opts, "path"),
2310 qemu_opt_get(opts, "security_model"));
2312 len = strlen("virtio-9p-pci,fsdev=,mount_tag=");
2313 len += 2*strlen(qemu_opt_get(opts, "mount_tag"));
2314 arg_9p = qemu_malloc((len + 1) * sizeof(*arg_9p));
2316 snprintf(arg_9p, (len + 1) * sizeof(*arg_9p),
2317 "virtio-9p-pci,fsdev=%s,mount_tag=%s",
2318 qemu_opt_get(opts, "mount_tag"),
2319 qemu_opt_get(opts, "mount_tag"));
2321 if (!qemu_opts_parse(qemu_find_opts("fsdev"), arg_fsdev, 1)) {
2322 fprintf(stderr, "parse error [fsdev]: %s\n", optarg);
2323 exit(1);
2326 if (!qemu_opts_parse(qemu_find_opts("device"), arg_9p, 1)) {
2327 fprintf(stderr, "parse error [device]: %s\n", optarg);
2328 exit(1);
2331 qemu_free(arg_fsdev);
2332 qemu_free(arg_9p);
2333 break;
2335 case QEMU_OPTION_serial:
2336 add_device_config(DEV_SERIAL, optarg);
2337 default_serial = 0;
2338 if (strncmp(optarg, "mon:", 4) == 0) {
2339 default_monitor = 0;
2341 break;
2342 case QEMU_OPTION_watchdog:
2343 if (watchdog) {
2344 fprintf(stderr,
2345 "qemu: only one watchdog option may be given\n");
2346 return 1;
2348 watchdog = optarg;
2349 break;
2350 case QEMU_OPTION_watchdog_action:
2351 if (select_watchdog_action(optarg) == -1) {
2352 fprintf(stderr, "Unknown -watchdog-action parameter\n");
2353 exit(1);
2355 break;
2356 case QEMU_OPTION_virtiocon:
2357 add_device_config(DEV_VIRTCON, optarg);
2358 default_virtcon = 0;
2359 if (strncmp(optarg, "mon:", 4) == 0) {
2360 default_monitor = 0;
2362 break;
2363 case QEMU_OPTION_parallel:
2364 add_device_config(DEV_PARALLEL, optarg);
2365 default_parallel = 0;
2366 if (strncmp(optarg, "mon:", 4) == 0) {
2367 default_monitor = 0;
2369 break;
2370 case QEMU_OPTION_debugcon:
2371 add_device_config(DEV_DEBUGCON, optarg);
2372 break;
2373 case QEMU_OPTION_loadvm:
2374 loadvm = optarg;
2375 break;
2376 case QEMU_OPTION_full_screen:
2377 full_screen = 1;
2378 break;
2379 #ifdef CONFIG_SDL
2380 case QEMU_OPTION_no_frame:
2381 no_frame = 1;
2382 break;
2383 case QEMU_OPTION_alt_grab:
2384 alt_grab = 1;
2385 break;
2386 case QEMU_OPTION_ctrl_grab:
2387 ctrl_grab = 1;
2388 break;
2389 case QEMU_OPTION_no_quit:
2390 no_quit = 1;
2391 break;
2392 case QEMU_OPTION_sdl:
2393 display_type = DT_SDL;
2394 break;
2395 #endif
2396 case QEMU_OPTION_pidfile:
2397 pid_file = optarg;
2398 break;
2399 case QEMU_OPTION_win2k_hack:
2400 win2k_install_hack = 1;
2401 break;
2402 case QEMU_OPTION_rtc_td_hack:
2403 rtc_td_hack = 1;
2404 break;
2405 case QEMU_OPTION_acpitable:
2406 do_acpitable_option(optarg);
2407 break;
2408 case QEMU_OPTION_smbios:
2409 do_smbios_option(optarg);
2410 break;
2411 case QEMU_OPTION_enable_kvm:
2412 kvm_allowed = 1;
2413 break;
2414 case QEMU_OPTION_usb:
2415 usb_enabled = 1;
2416 break;
2417 case QEMU_OPTION_usbdevice:
2418 usb_enabled = 1;
2419 add_device_config(DEV_USB, optarg);
2420 break;
2421 case QEMU_OPTION_device:
2422 if (!qemu_opts_parse(qemu_find_opts("device"), optarg, 1)) {
2423 exit(1);
2425 break;
2426 case QEMU_OPTION_smp:
2427 smp_parse(optarg);
2428 if (smp_cpus < 1) {
2429 fprintf(stderr, "Invalid number of CPUs\n");
2430 exit(1);
2432 if (max_cpus < smp_cpus) {
2433 fprintf(stderr, "maxcpus must be equal to or greater than "
2434 "smp\n");
2435 exit(1);
2437 if (max_cpus > 255) {
2438 fprintf(stderr, "Unsupported number of maxcpus\n");
2439 exit(1);
2441 break;
2442 case QEMU_OPTION_vnc:
2443 display_remote++;
2444 vnc_display = optarg;
2445 break;
2446 case QEMU_OPTION_no_acpi:
2447 acpi_enabled = 0;
2448 break;
2449 case QEMU_OPTION_no_hpet:
2450 no_hpet = 1;
2451 break;
2452 case QEMU_OPTION_balloon:
2453 if (balloon_parse(optarg) < 0) {
2454 fprintf(stderr, "Unknown -balloon argument %s\n", optarg);
2455 exit(1);
2457 break;
2458 case QEMU_OPTION_no_reboot:
2459 no_reboot = 1;
2460 break;
2461 case QEMU_OPTION_no_shutdown:
2462 no_shutdown = 1;
2463 break;
2464 case QEMU_OPTION_show_cursor:
2465 cursor_hide = 0;
2466 break;
2467 case QEMU_OPTION_uuid:
2468 if(qemu_uuid_parse(optarg, qemu_uuid) < 0) {
2469 fprintf(stderr, "Fail to parse UUID string."
2470 " Wrong format.\n");
2471 exit(1);
2473 break;
2474 case QEMU_OPTION_option_rom:
2475 if (nb_option_roms >= MAX_OPTION_ROMS) {
2476 fprintf(stderr, "Too many option ROMs\n");
2477 exit(1);
2479 option_rom[nb_option_roms] = optarg;
2480 nb_option_roms++;
2481 break;
2482 case QEMU_OPTION_semihosting:
2483 semihosting_enabled = 1;
2484 break;
2485 case QEMU_OPTION_name:
2486 qemu_name = qemu_strdup(optarg);
2488 char *p = strchr(qemu_name, ',');
2489 if (p != NULL) {
2490 *p++ = 0;
2491 if (strncmp(p, "process=", 8)) {
2492 fprintf(stderr, "Unknown subargument %s to -name", p);
2493 exit(1);
2495 p += 8;
2496 os_set_proc_name(p);
2499 break;
2500 case QEMU_OPTION_prom_env:
2501 if (nb_prom_envs >= MAX_PROM_ENVS) {
2502 fprintf(stderr, "Too many prom variables\n");
2503 exit(1);
2505 prom_envs[nb_prom_envs] = optarg;
2506 nb_prom_envs++;
2507 break;
2508 case QEMU_OPTION_old_param:
2509 old_param = 1;
2510 break;
2511 case QEMU_OPTION_clock:
2512 configure_alarms(optarg);
2513 break;
2514 case QEMU_OPTION_startdate:
2515 configure_rtc_date_offset(optarg, 1);
2516 break;
2517 case QEMU_OPTION_rtc:
2518 opts = qemu_opts_parse(qemu_find_opts("rtc"), optarg, 0);
2519 if (!opts) {
2520 exit(1);
2522 configure_rtc(opts);
2523 break;
2524 case QEMU_OPTION_tb_size:
2525 tb_size = strtol(optarg, NULL, 0);
2526 if (tb_size < 0)
2527 tb_size = 0;
2528 break;
2529 case QEMU_OPTION_icount:
2530 icount_option = optarg;
2531 break;
2532 case QEMU_OPTION_incoming:
2533 incoming = optarg;
2534 incoming_expected = true;
2535 break;
2536 case QEMU_OPTION_nodefaults:
2537 default_serial = 0;
2538 default_parallel = 0;
2539 default_virtcon = 0;
2540 default_monitor = 0;
2541 default_vga = 0;
2542 default_net = 0;
2543 default_floppy = 0;
2544 default_cdrom = 0;
2545 default_sdcard = 0;
2546 break;
2547 case QEMU_OPTION_xen_domid:
2548 if (!(xen_available())) {
2549 printf("Option %s not supported for this target\n", popt->name);
2550 exit(1);
2552 xen_domid = atoi(optarg);
2553 break;
2554 case QEMU_OPTION_xen_create:
2555 if (!(xen_available())) {
2556 printf("Option %s not supported for this target\n", popt->name);
2557 exit(1);
2559 xen_mode = XEN_CREATE;
2560 break;
2561 case QEMU_OPTION_xen_attach:
2562 if (!(xen_available())) {
2563 printf("Option %s not supported for this target\n", popt->name);
2564 exit(1);
2566 xen_mode = XEN_ATTACH;
2567 break;
2568 #ifdef CONFIG_SIMPLE_TRACE
2569 case QEMU_OPTION_trace:
2570 opts = qemu_opts_parse(qemu_find_opts("trace"), optarg, 0);
2571 if (opts) {
2572 trace_file = qemu_opt_get(opts, "file");
2574 break;
2575 #endif
2576 case QEMU_OPTION_readconfig:
2578 int ret = qemu_read_config_file(optarg);
2579 if (ret < 0) {
2580 fprintf(stderr, "read config %s: %s\n", optarg,
2581 strerror(-ret));
2582 exit(1);
2584 break;
2586 case QEMU_OPTION_spice:
2587 olist = qemu_find_opts("spice");
2588 if (!olist) {
2589 fprintf(stderr, "spice is not supported by this qemu build.\n");
2590 exit(1);
2592 opts = qemu_opts_parse(olist, optarg, 0);
2593 if (!opts) {
2594 fprintf(stderr, "parse error: %s\n", optarg);
2595 exit(1);
2597 break;
2598 case QEMU_OPTION_writeconfig:
2600 FILE *fp;
2601 if (strcmp(optarg, "-") == 0) {
2602 fp = stdout;
2603 } else {
2604 fp = fopen(optarg, "w");
2605 if (fp == NULL) {
2606 fprintf(stderr, "open %s: %s\n", optarg, strerror(errno));
2607 exit(1);
2610 qemu_config_write(fp);
2611 fclose(fp);
2612 break;
2614 default:
2615 os_parse_cmd_args(popt->index, optarg);
2619 loc_set_none();
2621 /* If no data_dir is specified then try to find it relative to the
2622 executable path. */
2623 if (!data_dir) {
2624 data_dir = os_find_datadir(argv[0]);
2626 /* If all else fails use the install patch specified when building. */
2627 if (!data_dir) {
2628 data_dir = CONFIG_QEMU_DATADIR;
2631 #ifdef CONFIG_SIMPLE_TRACE
2633 * Set the trace file name, if specified.
2635 st_set_trace_file(trace_file);
2636 #endif
2638 * Default to max_cpus = smp_cpus, in case the user doesn't
2639 * specify a max_cpus value.
2641 if (!max_cpus)
2642 max_cpus = smp_cpus;
2644 machine->max_cpus = machine->max_cpus ?: 1; /* Default to UP */
2645 if (smp_cpus > machine->max_cpus) {
2646 fprintf(stderr, "Number of SMP cpus requested (%d), exceeds max cpus "
2647 "supported by machine `%s' (%d)\n", smp_cpus, machine->name,
2648 machine->max_cpus);
2649 exit(1);
2652 qemu_opts_foreach(qemu_find_opts("device"), default_driver_check, NULL, 0);
2653 qemu_opts_foreach(qemu_find_opts("global"), default_driver_check, NULL, 0);
2655 if (machine->no_serial) {
2656 default_serial = 0;
2658 if (machine->no_parallel) {
2659 default_parallel = 0;
2661 if (!machine->use_virtcon) {
2662 default_virtcon = 0;
2664 if (machine->no_vga) {
2665 default_vga = 0;
2667 if (machine->no_floppy) {
2668 default_floppy = 0;
2670 if (machine->no_cdrom) {
2671 default_cdrom = 0;
2673 if (machine->no_sdcard) {
2674 default_sdcard = 0;
2677 if (display_type == DT_NOGRAPHIC) {
2678 if (default_parallel)
2679 add_device_config(DEV_PARALLEL, "null");
2680 if (default_serial && default_monitor) {
2681 add_device_config(DEV_SERIAL, "mon:stdio");
2682 } else if (default_virtcon && default_monitor) {
2683 add_device_config(DEV_VIRTCON, "mon:stdio");
2684 } else {
2685 if (default_serial)
2686 add_device_config(DEV_SERIAL, "stdio");
2687 if (default_virtcon)
2688 add_device_config(DEV_VIRTCON, "stdio");
2689 if (default_monitor)
2690 monitor_parse("stdio", "readline");
2692 } else {
2693 if (default_serial)
2694 add_device_config(DEV_SERIAL, "vc:80Cx24C");
2695 if (default_parallel)
2696 add_device_config(DEV_PARALLEL, "vc:80Cx24C");
2697 if (default_monitor)
2698 monitor_parse("vc:80Cx24C", "readline");
2699 if (default_virtcon)
2700 add_device_config(DEV_VIRTCON, "vc:80Cx24C");
2702 if (default_vga)
2703 vga_interface_type = VGA_CIRRUS;
2705 socket_init();
2707 if (qemu_opts_foreach(qemu_find_opts("chardev"), chardev_init_func, NULL, 1) != 0)
2708 exit(1);
2709 #ifdef CONFIG_VIRTFS
2710 if (qemu_opts_foreach(qemu_find_opts("fsdev"), fsdev_init_func, NULL, 1) != 0) {
2711 exit(1);
2713 #endif
2715 os_daemonize();
2717 if (pid_file && qemu_create_pidfile(pid_file) != 0) {
2718 os_pidfile_error();
2719 exit(1);
2722 if (kvm_allowed) {
2723 int ret = kvm_init(smp_cpus);
2724 if (ret < 0) {
2725 if (!kvm_available()) {
2726 printf("KVM not supported for this target\n");
2727 } else {
2728 fprintf(stderr, "failed to initialize KVM: %s\n", strerror(-ret));
2730 exit(1);
2734 if (qemu_init_main_loop()) {
2735 fprintf(stderr, "qemu_init_main_loop failed\n");
2736 exit(1);
2738 linux_boot = (kernel_filename != NULL);
2740 if (!linux_boot && *kernel_cmdline != '\0') {
2741 fprintf(stderr, "-append only allowed with -kernel option\n");
2742 exit(1);
2745 if (!linux_boot && initrd_filename != NULL) {
2746 fprintf(stderr, "-initrd only allowed with -kernel option\n");
2747 exit(1);
2750 os_set_line_buffering();
2752 if (init_timer_alarm() < 0) {
2753 fprintf(stderr, "could not initialize alarm timer\n");
2754 exit(1);
2756 configure_icount(icount_option);
2758 if (net_init_clients() < 0) {
2759 exit(1);
2762 /* init the bluetooth world */
2763 if (foreach_device_config(DEV_BT, bt_parse))
2764 exit(1);
2766 /* init the memory */
2767 if (ram_size == 0)
2768 ram_size = DEFAULT_RAM_SIZE * 1024 * 1024;
2770 /* init the dynamic translator */
2771 cpu_exec_init_all(tb_size * 1024 * 1024);
2773 bdrv_init_with_whitelist();
2775 blk_mig_init();
2777 if (default_cdrom) {
2778 /* we always create the cdrom drive, even if no disk is there */
2779 drive_add(NULL, CDROM_ALIAS);
2782 if (default_floppy) {
2783 /* we always create at least one floppy */
2784 drive_add(NULL, FD_ALIAS, 0);
2787 if (default_sdcard) {
2788 /* we always create one sd slot, even if no card is in it */
2789 drive_add(NULL, SD_ALIAS);
2792 /* open the virtual block devices */
2793 if (snapshot)
2794 qemu_opts_foreach(qemu_find_opts("drive"), drive_enable_snapshot, NULL, 0);
2795 if (qemu_opts_foreach(qemu_find_opts("drive"), drive_init_func, &machine->use_scsi, 1) != 0)
2796 exit(1);
2798 register_savevm_live(NULL, "ram", 0, 4, NULL, ram_save_live, NULL,
2799 ram_load, NULL);
2801 if (nb_numa_nodes > 0) {
2802 int i;
2804 if (nb_numa_nodes > smp_cpus) {
2805 nb_numa_nodes = smp_cpus;
2808 /* If no memory size if given for any node, assume the default case
2809 * and distribute the available memory equally across all nodes
2811 for (i = 0; i < nb_numa_nodes; i++) {
2812 if (node_mem[i] != 0)
2813 break;
2815 if (i == nb_numa_nodes) {
2816 uint64_t usedmem = 0;
2818 /* On Linux, the each node's border has to be 8MB aligned,
2819 * the final node gets the rest.
2821 for (i = 0; i < nb_numa_nodes - 1; i++) {
2822 node_mem[i] = (ram_size / nb_numa_nodes) & ~((1 << 23UL) - 1);
2823 usedmem += node_mem[i];
2825 node_mem[i] = ram_size - usedmem;
2828 for (i = 0; i < nb_numa_nodes; i++) {
2829 if (node_cpumask[i] != 0)
2830 break;
2832 /* assigning the VCPUs round-robin is easier to implement, guest OSes
2833 * must cope with this anyway, because there are BIOSes out there in
2834 * real machines which also use this scheme.
2836 if (i == nb_numa_nodes) {
2837 for (i = 0; i < smp_cpus; i++) {
2838 node_cpumask[i % nb_numa_nodes] |= 1 << i;
2843 if (qemu_opts_foreach(qemu_find_opts("mon"), mon_init_func, NULL, 1) != 0) {
2844 exit(1);
2847 if (foreach_device_config(DEV_SERIAL, serial_parse) < 0)
2848 exit(1);
2849 if (foreach_device_config(DEV_PARALLEL, parallel_parse) < 0)
2850 exit(1);
2851 if (foreach_device_config(DEV_VIRTCON, virtcon_parse) < 0)
2852 exit(1);
2853 if (foreach_device_config(DEV_DEBUGCON, debugcon_parse) < 0)
2854 exit(1);
2856 module_call_init(MODULE_INIT_DEVICE);
2858 if (qemu_opts_foreach(qemu_find_opts("device"), device_help_func, NULL, 0) != 0)
2859 exit(0);
2861 if (watchdog) {
2862 i = select_watchdog(watchdog);
2863 if (i > 0)
2864 exit (i == 1 ? 1 : 0);
2867 if (machine->compat_props) {
2868 qdev_prop_register_global_list(machine->compat_props);
2870 qemu_add_globals();
2872 machine->init(ram_size, boot_devices,
2873 kernel_filename, kernel_cmdline, initrd_filename, cpu_model);
2875 cpu_synchronize_all_post_init();
2877 /* must be after terminal init, SDL library changes signal handlers */
2878 os_setup_signal_handling();
2880 set_numa_modes();
2882 current_machine = machine;
2884 /* init USB devices */
2885 if (usb_enabled) {
2886 if (foreach_device_config(DEV_USB, usb_parse) < 0)
2887 exit(1);
2890 /* init generic devices */
2891 if (qemu_opts_foreach(qemu_find_opts("device"), device_init_func, NULL, 1) != 0)
2892 exit(1);
2894 net_check_clients();
2896 /* just use the first displaystate for the moment */
2897 ds = get_displaystate();
2899 if (using_spice)
2900 display_remote++;
2901 if (display_type == DT_DEFAULT && !display_remote) {
2902 #if defined(CONFIG_SDL) || defined(CONFIG_COCOA)
2903 display_type = DT_SDL;
2904 #else
2905 vnc_display = "localhost:0,to=99";
2906 show_vnc_port = 1;
2907 #endif
2911 /* init local displays */
2912 switch (display_type) {
2913 case DT_NOGRAPHIC:
2914 break;
2915 #if defined(CONFIG_CURSES)
2916 case DT_CURSES:
2917 curses_display_init(ds, full_screen);
2918 break;
2919 #endif
2920 #if defined(CONFIG_SDL)
2921 case DT_SDL:
2922 sdl_display_init(ds, full_screen, no_frame);
2923 break;
2924 #elif defined(CONFIG_COCOA)
2925 case DT_SDL:
2926 cocoa_display_init(ds, full_screen);
2927 break;
2928 #endif
2929 default:
2930 break;
2933 /* init remote displays */
2934 if (vnc_display) {
2935 vnc_display_init(ds);
2936 if (vnc_display_open(ds, vnc_display) < 0)
2937 exit(1);
2939 if (show_vnc_port) {
2940 printf("VNC server running on `%s'\n", vnc_display_local_addr(ds));
2943 #ifdef CONFIG_SPICE
2944 if (using_spice) {
2945 qemu_spice_display_init(ds);
2947 #endif
2949 /* display setup */
2950 dpy_resize(ds);
2951 dcl = ds->listeners;
2952 while (dcl != NULL) {
2953 if (dcl->dpy_refresh != NULL) {
2954 ds->gui_timer = qemu_new_timer(rt_clock, gui_update, ds);
2955 qemu_mod_timer(ds->gui_timer, qemu_get_clock(rt_clock));
2956 break;
2958 dcl = dcl->next;
2960 if (ds->gui_timer == NULL) {
2961 nographic_timer = qemu_new_timer(rt_clock, nographic_update, NULL);
2962 qemu_mod_timer(nographic_timer, qemu_get_clock(rt_clock));
2964 text_consoles_set_display(ds);
2966 if (gdbstub_dev && gdbserver_start(gdbstub_dev) < 0) {
2967 fprintf(stderr, "qemu: could not open gdbserver on device '%s'\n",
2968 gdbstub_dev);
2969 exit(1);
2972 qdev_machine_creation_done();
2974 if (rom_load_all() != 0) {
2975 fprintf(stderr, "rom loading failed\n");
2976 exit(1);
2979 qemu_system_reset();
2980 if (loadvm) {
2981 if (load_vmstate(loadvm) < 0) {
2982 autostart = 0;
2986 if (incoming) {
2987 int ret = qemu_start_incoming_migration(incoming);
2988 if (ret < 0) {
2989 fprintf(stderr, "Migration failed. Exit code %s(%d), exiting.\n",
2990 incoming, ret);
2991 exit(ret);
2993 } else if (autostart) {
2994 vm_start();
2997 os_setup_post();
2999 main_loop();
3000 quit_timers();
3001 net_cleanup();
3003 return 0;