nbd: Haiku has _IO() in its BSD compatibility layer
[qemu/agraf.git] / vl.c
blobd352d18cfe61bf67ec59a98f8014a56e2778a977
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 //#define DEBUG_NET
166 //#define DEBUG_SLIRP
168 #define DEFAULT_RAM_SIZE 128
170 #define MAX_VIRTIO_CONSOLES 1
172 static const char *data_dir;
173 const char *bios_name = NULL;
174 enum vga_retrace_method vga_retrace_method = VGA_RETRACE_DUMB;
175 DisplayType display_type = DT_DEFAULT;
176 const char* keyboard_layout = NULL;
177 ram_addr_t ram_size;
178 const char *mem_path = NULL;
179 #ifdef MAP_POPULATE
180 int mem_prealloc = 0; /* force preallocation of physical target memory */
181 #endif
182 int nb_nics;
183 NICInfo nd_table[MAX_NICS];
184 int vm_running;
185 int autostart;
186 int incoming_expected; /* Started with -incoming and waiting for incoming */
187 static int rtc_utc = 1;
188 static int rtc_date_offset = -1; /* -1 means no change */
189 QEMUClock *rtc_clock;
190 int vga_interface_type = VGA_NONE;
191 static int full_screen = 0;
192 #ifdef CONFIG_SDL
193 static int no_frame = 0;
194 #endif
195 int no_quit = 0;
196 CharDriverState *serial_hds[MAX_SERIAL_PORTS];
197 CharDriverState *parallel_hds[MAX_PARALLEL_PORTS];
198 CharDriverState *virtcon_hds[MAX_VIRTIO_CONSOLES];
199 int win2k_install_hack = 0;
200 int rtc_td_hack = 0;
201 int usb_enabled = 0;
202 int singlestep = 0;
203 int smp_cpus = 1;
204 int max_cpus = 0;
205 int smp_cores = 1;
206 int smp_threads = 1;
207 const char *vnc_display;
208 int acpi_enabled = 1;
209 int no_hpet = 0;
210 int fd_bootchk = 1;
211 int no_reboot = 0;
212 int no_shutdown = 0;
213 int cursor_hide = 1;
214 int graphic_rotate = 0;
215 uint8_t irq0override = 1;
216 const char *watchdog;
217 const char *option_rom[MAX_OPTION_ROMS];
218 int nb_option_roms;
219 int semihosting_enabled = 0;
220 int old_param = 0;
221 const char *qemu_name;
222 int alt_grab = 0;
223 int ctrl_grab = 0;
224 unsigned int nb_prom_envs = 0;
225 const char *prom_envs[MAX_PROM_ENVS];
226 int boot_menu;
228 int nb_numa_nodes;
229 uint64_t node_mem[MAX_NODES];
230 uint64_t node_cpumask[MAX_NODES];
232 static QEMUTimer *nographic_timer;
234 uint8_t qemu_uuid[16];
236 static QEMUBootSetHandler *boot_set_handler;
237 static void *boot_set_opaque;
239 static NotifierList exit_notifiers =
240 NOTIFIER_LIST_INITIALIZER(exit_notifiers);
242 int kvm_allowed = 0;
243 uint32_t xen_domid;
244 enum xen_mode xen_mode = XEN_EMULATE;
246 static int default_serial = 1;
247 static int default_parallel = 1;
248 static int default_virtcon = 1;
249 static int default_monitor = 1;
250 static int default_vga = 1;
251 static int default_floppy = 1;
252 static int default_cdrom = 1;
253 static int default_sdcard = 1;
255 static struct {
256 const char *driver;
257 int *flag;
258 } default_list[] = {
259 { .driver = "isa-serial", .flag = &default_serial },
260 { .driver = "isa-parallel", .flag = &default_parallel },
261 { .driver = "isa-fdc", .flag = &default_floppy },
262 { .driver = "ide-drive", .flag = &default_cdrom },
263 { .driver = "virtio-serial-pci", .flag = &default_virtcon },
264 { .driver = "virtio-serial-s390", .flag = &default_virtcon },
265 { .driver = "virtio-serial", .flag = &default_virtcon },
266 { .driver = "VGA", .flag = &default_vga },
267 { .driver = "cirrus-vga", .flag = &default_vga },
268 { .driver = "vmware-svga", .flag = &default_vga },
271 static int default_driver_check(QemuOpts *opts, void *opaque)
273 const char *driver = qemu_opt_get(opts, "driver");
274 int i;
276 if (!driver)
277 return 0;
278 for (i = 0; i < ARRAY_SIZE(default_list); i++) {
279 if (strcmp(default_list[i].driver, driver) != 0)
280 continue;
281 *(default_list[i].flag) = 0;
283 return 0;
286 /***********************************************************/
287 /* real time host monotonic timer */
289 /* compute with 96 bit intermediate result: (a*b)/c */
290 uint64_t muldiv64(uint64_t a, uint32_t b, uint32_t c)
292 union {
293 uint64_t ll;
294 struct {
295 #ifdef HOST_WORDS_BIGENDIAN
296 uint32_t high, low;
297 #else
298 uint32_t low, high;
299 #endif
300 } l;
301 } u, res;
302 uint64_t rl, rh;
304 u.ll = a;
305 rl = (uint64_t)u.l.low * (uint64_t)b;
306 rh = (uint64_t)u.l.high * (uint64_t)b;
307 rh += (rl >> 32);
308 res.l.high = rh / c;
309 res.l.low = (((rh % c) << 32) + (rl & 0xffffffff)) / c;
310 return res.ll;
313 /***********************************************************/
314 /* host time/date access */
315 void qemu_get_timedate(struct tm *tm, int offset)
317 time_t ti;
318 struct tm *ret;
320 time(&ti);
321 ti += offset;
322 if (rtc_date_offset == -1) {
323 if (rtc_utc)
324 ret = gmtime(&ti);
325 else
326 ret = localtime(&ti);
327 } else {
328 ti -= rtc_date_offset;
329 ret = gmtime(&ti);
332 memcpy(tm, ret, sizeof(struct tm));
335 int qemu_timedate_diff(struct tm *tm)
337 time_t seconds;
339 if (rtc_date_offset == -1)
340 if (rtc_utc)
341 seconds = mktimegm(tm);
342 else
343 seconds = mktime(tm);
344 else
345 seconds = mktimegm(tm) + rtc_date_offset;
347 return seconds - time(NULL);
350 void rtc_change_mon_event(struct tm *tm)
352 QObject *data;
354 data = qobject_from_jsonf("{ 'offset': %d }", qemu_timedate_diff(tm));
355 monitor_protocol_event(QEVENT_RTC_CHANGE, data);
356 qobject_decref(data);
359 static void configure_rtc_date_offset(const char *startdate, int legacy)
361 time_t rtc_start_date;
362 struct tm tm;
364 if (!strcmp(startdate, "now") && legacy) {
365 rtc_date_offset = -1;
366 } else {
367 if (sscanf(startdate, "%d-%d-%dT%d:%d:%d",
368 &tm.tm_year,
369 &tm.tm_mon,
370 &tm.tm_mday,
371 &tm.tm_hour,
372 &tm.tm_min,
373 &tm.tm_sec) == 6) {
374 /* OK */
375 } else if (sscanf(startdate, "%d-%d-%d",
376 &tm.tm_year,
377 &tm.tm_mon,
378 &tm.tm_mday) == 3) {
379 tm.tm_hour = 0;
380 tm.tm_min = 0;
381 tm.tm_sec = 0;
382 } else {
383 goto date_fail;
385 tm.tm_year -= 1900;
386 tm.tm_mon--;
387 rtc_start_date = mktimegm(&tm);
388 if (rtc_start_date == -1) {
389 date_fail:
390 fprintf(stderr, "Invalid date format. Valid formats are:\n"
391 "'2006-06-17T16:01:21' or '2006-06-17'\n");
392 exit(1);
394 rtc_date_offset = time(NULL) - rtc_start_date;
398 static void configure_rtc(QemuOpts *opts)
400 const char *value;
402 value = qemu_opt_get(opts, "base");
403 if (value) {
404 if (!strcmp(value, "utc")) {
405 rtc_utc = 1;
406 } else if (!strcmp(value, "localtime")) {
407 rtc_utc = 0;
408 } else {
409 configure_rtc_date_offset(value, 0);
412 value = qemu_opt_get(opts, "clock");
413 if (value) {
414 if (!strcmp(value, "host")) {
415 rtc_clock = host_clock;
416 } else if (!strcmp(value, "vm")) {
417 rtc_clock = vm_clock;
418 } else {
419 fprintf(stderr, "qemu: invalid option value '%s'\n", value);
420 exit(1);
423 value = qemu_opt_get(opts, "driftfix");
424 if (value) {
425 if (!strcmp(value, "slew")) {
426 rtc_td_hack = 1;
427 } else if (!strcmp(value, "none")) {
428 rtc_td_hack = 0;
429 } else {
430 fprintf(stderr, "qemu: invalid option value '%s'\n", value);
431 exit(1);
436 /***********************************************************/
437 /* Bluetooth support */
438 static int nb_hcis;
439 static int cur_hci;
440 static struct HCIInfo *hci_table[MAX_NICS];
442 static struct bt_vlan_s {
443 struct bt_scatternet_s net;
444 int id;
445 struct bt_vlan_s *next;
446 } *first_bt_vlan;
448 /* find or alloc a new bluetooth "VLAN" */
449 static struct bt_scatternet_s *qemu_find_bt_vlan(int id)
451 struct bt_vlan_s **pvlan, *vlan;
452 for (vlan = first_bt_vlan; vlan != NULL; vlan = vlan->next) {
453 if (vlan->id == id)
454 return &vlan->net;
456 vlan = qemu_mallocz(sizeof(struct bt_vlan_s));
457 vlan->id = id;
458 pvlan = &first_bt_vlan;
459 while (*pvlan != NULL)
460 pvlan = &(*pvlan)->next;
461 *pvlan = vlan;
462 return &vlan->net;
465 static void null_hci_send(struct HCIInfo *hci, const uint8_t *data, int len)
469 static int null_hci_addr_set(struct HCIInfo *hci, const uint8_t *bd_addr)
471 return -ENOTSUP;
474 static struct HCIInfo null_hci = {
475 .cmd_send = null_hci_send,
476 .sco_send = null_hci_send,
477 .acl_send = null_hci_send,
478 .bdaddr_set = null_hci_addr_set,
481 struct HCIInfo *qemu_next_hci(void)
483 if (cur_hci == nb_hcis)
484 return &null_hci;
486 return hci_table[cur_hci++];
489 static struct HCIInfo *hci_init(const char *str)
491 char *endp;
492 struct bt_scatternet_s *vlan = 0;
494 if (!strcmp(str, "null"))
495 /* null */
496 return &null_hci;
497 else if (!strncmp(str, "host", 4) && (str[4] == '\0' || str[4] == ':'))
498 /* host[:hciN] */
499 return bt_host_hci(str[4] ? str + 5 : "hci0");
500 else if (!strncmp(str, "hci", 3)) {
501 /* hci[,vlan=n] */
502 if (str[3]) {
503 if (!strncmp(str + 3, ",vlan=", 6)) {
504 vlan = qemu_find_bt_vlan(strtol(str + 9, &endp, 0));
505 if (*endp)
506 vlan = 0;
508 } else
509 vlan = qemu_find_bt_vlan(0);
510 if (vlan)
511 return bt_new_hci(vlan);
514 fprintf(stderr, "qemu: Unknown bluetooth HCI `%s'.\n", str);
516 return 0;
519 static int bt_hci_parse(const char *str)
521 struct HCIInfo *hci;
522 bdaddr_t bdaddr;
524 if (nb_hcis >= MAX_NICS) {
525 fprintf(stderr, "qemu: Too many bluetooth HCIs (max %i).\n", MAX_NICS);
526 return -1;
529 hci = hci_init(str);
530 if (!hci)
531 return -1;
533 bdaddr.b[0] = 0x52;
534 bdaddr.b[1] = 0x54;
535 bdaddr.b[2] = 0x00;
536 bdaddr.b[3] = 0x12;
537 bdaddr.b[4] = 0x34;
538 bdaddr.b[5] = 0x56 + nb_hcis;
539 hci->bdaddr_set(hci, bdaddr.b);
541 hci_table[nb_hcis++] = hci;
543 return 0;
546 static void bt_vhci_add(int vlan_id)
548 struct bt_scatternet_s *vlan = qemu_find_bt_vlan(vlan_id);
550 if (!vlan->slave)
551 fprintf(stderr, "qemu: warning: adding a VHCI to "
552 "an empty scatternet %i\n", vlan_id);
554 bt_vhci_init(bt_new_hci(vlan));
557 static struct bt_device_s *bt_device_add(const char *opt)
559 struct bt_scatternet_s *vlan;
560 int vlan_id = 0;
561 char *endp = strstr(opt, ",vlan=");
562 int len = (endp ? endp - opt : strlen(opt)) + 1;
563 char devname[10];
565 pstrcpy(devname, MIN(sizeof(devname), len), opt);
567 if (endp) {
568 vlan_id = strtol(endp + 6, &endp, 0);
569 if (*endp) {
570 fprintf(stderr, "qemu: unrecognised bluetooth vlan Id\n");
571 return 0;
575 vlan = qemu_find_bt_vlan(vlan_id);
577 if (!vlan->slave)
578 fprintf(stderr, "qemu: warning: adding a slave device to "
579 "an empty scatternet %i\n", vlan_id);
581 if (!strcmp(devname, "keyboard"))
582 return bt_keyboard_init(vlan);
584 fprintf(stderr, "qemu: unsupported bluetooth device `%s'\n", devname);
585 return 0;
588 static int bt_parse(const char *opt)
590 const char *endp, *p;
591 int vlan;
593 if (strstart(opt, "hci", &endp)) {
594 if (!*endp || *endp == ',') {
595 if (*endp)
596 if (!strstart(endp, ",vlan=", 0))
597 opt = endp + 1;
599 return bt_hci_parse(opt);
601 } else if (strstart(opt, "vhci", &endp)) {
602 if (!*endp || *endp == ',') {
603 if (*endp) {
604 if (strstart(endp, ",vlan=", &p)) {
605 vlan = strtol(p, (char **) &endp, 0);
606 if (*endp) {
607 fprintf(stderr, "qemu: bad scatternet '%s'\n", p);
608 return 1;
610 } else {
611 fprintf(stderr, "qemu: bad parameter '%s'\n", endp + 1);
612 return 1;
614 } else
615 vlan = 0;
617 bt_vhci_add(vlan);
618 return 0;
620 } else if (strstart(opt, "device:", &endp))
621 return !bt_device_add(endp);
623 fprintf(stderr, "qemu: bad bluetooth parameter '%s'\n", opt);
624 return 1;
627 /***********************************************************/
628 /* QEMU Block devices */
630 #define HD_ALIAS "index=%d,media=disk"
631 #define CDROM_ALIAS "index=2,media=cdrom"
632 #define FD_ALIAS "index=%d,if=floppy"
633 #define PFLASH_ALIAS "if=pflash"
634 #define MTD_ALIAS "if=mtd"
635 #define SD_ALIAS "index=0,if=sd"
637 static int drive_init_func(QemuOpts *opts, void *opaque)
639 int *use_scsi = opaque;
640 int fatal_error = 0;
642 if (drive_init(opts, *use_scsi, &fatal_error) == NULL) {
643 if (fatal_error)
644 return 1;
646 return 0;
649 static int drive_enable_snapshot(QemuOpts *opts, void *opaque)
651 if (NULL == qemu_opt_get(opts, "snapshot")) {
652 qemu_opt_set(opts, "snapshot", "on");
654 return 0;
657 void qemu_register_boot_set(QEMUBootSetHandler *func, void *opaque)
659 boot_set_handler = func;
660 boot_set_opaque = opaque;
663 int qemu_boot_set(const char *boot_devices)
665 if (!boot_set_handler) {
666 return -EINVAL;
668 return boot_set_handler(boot_set_opaque, boot_devices);
671 static void validate_bootdevices(char *devices)
673 /* We just do some generic consistency checks */
674 const char *p;
675 int bitmap = 0;
677 for (p = devices; *p != '\0'; p++) {
678 /* Allowed boot devices are:
679 * a-b: floppy disk drives
680 * c-f: IDE disk drives
681 * g-m: machine implementation dependant drives
682 * n-p: network devices
683 * It's up to each machine implementation to check if the given boot
684 * devices match the actual hardware implementation and firmware
685 * features.
687 if (*p < 'a' || *p > 'p') {
688 fprintf(stderr, "Invalid boot device '%c'\n", *p);
689 exit(1);
691 if (bitmap & (1 << (*p - 'a'))) {
692 fprintf(stderr, "Boot device '%c' was given twice\n", *p);
693 exit(1);
695 bitmap |= 1 << (*p - 'a');
699 static void restore_boot_devices(void *opaque)
701 char *standard_boot_devices = opaque;
702 static int first = 1;
704 /* Restore boot order and remove ourselves after the first boot */
705 if (first) {
706 first = 0;
707 return;
710 qemu_boot_set(standard_boot_devices);
712 qemu_unregister_reset(restore_boot_devices, standard_boot_devices);
713 qemu_free(standard_boot_devices);
716 static void numa_add(const char *optarg)
718 char option[128];
719 char *endptr;
720 unsigned long long value, endvalue;
721 int nodenr;
723 optarg = get_opt_name(option, 128, optarg, ',') + 1;
724 if (!strcmp(option, "node")) {
725 if (get_param_value(option, 128, "nodeid", optarg) == 0) {
726 nodenr = nb_numa_nodes;
727 } else {
728 nodenr = strtoull(option, NULL, 10);
731 if (get_param_value(option, 128, "mem", optarg) == 0) {
732 node_mem[nodenr] = 0;
733 } else {
734 value = strtoull(option, &endptr, 0);
735 switch (*endptr) {
736 case 0: case 'M': case 'm':
737 value <<= 20;
738 break;
739 case 'G': case 'g':
740 value <<= 30;
741 break;
743 node_mem[nodenr] = value;
745 if (get_param_value(option, 128, "cpus", optarg) == 0) {
746 node_cpumask[nodenr] = 0;
747 } else {
748 value = strtoull(option, &endptr, 10);
749 if (value >= 64) {
750 value = 63;
751 fprintf(stderr, "only 64 CPUs in NUMA mode supported.\n");
752 } else {
753 if (*endptr == '-') {
754 endvalue = strtoull(endptr+1, &endptr, 10);
755 if (endvalue >= 63) {
756 endvalue = 62;
757 fprintf(stderr,
758 "only 63 CPUs in NUMA mode supported.\n");
760 value = (2ULL << endvalue) - (1ULL << value);
761 } else {
762 value = 1ULL << value;
765 node_cpumask[nodenr] = value;
767 nb_numa_nodes++;
769 return;
772 static void smp_parse(const char *optarg)
774 int smp, sockets = 0, threads = 0, cores = 0;
775 char *endptr;
776 char option[128];
778 smp = strtoul(optarg, &endptr, 10);
779 if (endptr != optarg) {
780 if (*endptr == ',') {
781 endptr++;
784 if (get_param_value(option, 128, "sockets", endptr) != 0)
785 sockets = strtoull(option, NULL, 10);
786 if (get_param_value(option, 128, "cores", endptr) != 0)
787 cores = strtoull(option, NULL, 10);
788 if (get_param_value(option, 128, "threads", endptr) != 0)
789 threads = strtoull(option, NULL, 10);
790 if (get_param_value(option, 128, "maxcpus", endptr) != 0)
791 max_cpus = strtoull(option, NULL, 10);
793 /* compute missing values, prefer sockets over cores over threads */
794 if (smp == 0 || sockets == 0) {
795 sockets = sockets > 0 ? sockets : 1;
796 cores = cores > 0 ? cores : 1;
797 threads = threads > 0 ? threads : 1;
798 if (smp == 0) {
799 smp = cores * threads * sockets;
801 } else {
802 if (cores == 0) {
803 threads = threads > 0 ? threads : 1;
804 cores = smp / (sockets * threads);
805 } else {
806 threads = smp / (cores * sockets);
809 smp_cpus = smp;
810 smp_cores = cores > 0 ? cores : 1;
811 smp_threads = threads > 0 ? threads : 1;
812 if (max_cpus == 0)
813 max_cpus = smp_cpus;
816 /***********************************************************/
817 /* USB devices */
819 static int usb_device_add(const char *devname)
821 const char *p;
822 USBDevice *dev = NULL;
824 if (!usb_enabled)
825 return -1;
827 /* drivers with .usbdevice_name entry in USBDeviceInfo */
828 dev = usbdevice_create(devname);
829 if (dev)
830 goto done;
832 /* the other ones */
833 if (strstart(devname, "host:", &p)) {
834 dev = usb_host_device_open(p);
835 } else if (!strcmp(devname, "bt") || strstart(devname, "bt:", &p)) {
836 dev = usb_bt_init(devname[2] ? hci_init(p) :
837 bt_new_hci(qemu_find_bt_vlan(0)));
838 } else {
839 return -1;
841 if (!dev)
842 return -1;
844 done:
845 return 0;
848 static int usb_device_del(const char *devname)
850 int bus_num, addr;
851 const char *p;
853 if (strstart(devname, "host:", &p))
854 return usb_host_device_close(p);
856 if (!usb_enabled)
857 return -1;
859 p = strchr(devname, '.');
860 if (!p)
861 return -1;
862 bus_num = strtoul(devname, NULL, 0);
863 addr = strtoul(p + 1, NULL, 0);
865 return usb_device_delete_addr(bus_num, addr);
868 static int usb_parse(const char *cmdline)
870 int r;
871 r = usb_device_add(cmdline);
872 if (r < 0) {
873 fprintf(stderr, "qemu: could not add USB device '%s'\n", cmdline);
875 return r;
878 void do_usb_add(Monitor *mon, const QDict *qdict)
880 const char *devname = qdict_get_str(qdict, "devname");
881 if (usb_device_add(devname) < 0) {
882 error_report("could not add USB device '%s'", devname);
886 void do_usb_del(Monitor *mon, const QDict *qdict)
888 const char *devname = qdict_get_str(qdict, "devname");
889 if (usb_device_del(devname) < 0) {
890 error_report("could not delete USB device '%s'", devname);
894 /***********************************************************/
895 /* PCMCIA/Cardbus */
897 static struct pcmcia_socket_entry_s {
898 PCMCIASocket *socket;
899 struct pcmcia_socket_entry_s *next;
900 } *pcmcia_sockets = 0;
902 void pcmcia_socket_register(PCMCIASocket *socket)
904 struct pcmcia_socket_entry_s *entry;
906 entry = qemu_malloc(sizeof(struct pcmcia_socket_entry_s));
907 entry->socket = socket;
908 entry->next = pcmcia_sockets;
909 pcmcia_sockets = entry;
912 void pcmcia_socket_unregister(PCMCIASocket *socket)
914 struct pcmcia_socket_entry_s *entry, **ptr;
916 ptr = &pcmcia_sockets;
917 for (entry = *ptr; entry; ptr = &entry->next, entry = *ptr)
918 if (entry->socket == socket) {
919 *ptr = entry->next;
920 qemu_free(entry);
924 void pcmcia_info(Monitor *mon)
926 struct pcmcia_socket_entry_s *iter;
928 if (!pcmcia_sockets)
929 monitor_printf(mon, "No PCMCIA sockets\n");
931 for (iter = pcmcia_sockets; iter; iter = iter->next)
932 monitor_printf(mon, "%s: %s\n", iter->socket->slot_string,
933 iter->socket->attached ? iter->socket->card_string :
934 "Empty");
937 /***********************************************************/
938 /* I/O handling */
940 typedef struct IOHandlerRecord {
941 int fd;
942 IOCanReadHandler *fd_read_poll;
943 IOHandler *fd_read;
944 IOHandler *fd_write;
945 int deleted;
946 void *opaque;
947 /* temporary data */
948 struct pollfd *ufd;
949 QLIST_ENTRY(IOHandlerRecord) next;
950 } IOHandlerRecord;
952 static QLIST_HEAD(, IOHandlerRecord) io_handlers =
953 QLIST_HEAD_INITIALIZER(io_handlers);
956 /* XXX: fd_read_poll should be suppressed, but an API change is
957 necessary in the character devices to suppress fd_can_read(). */
958 int qemu_set_fd_handler2(int fd,
959 IOCanReadHandler *fd_read_poll,
960 IOHandler *fd_read,
961 IOHandler *fd_write,
962 void *opaque)
964 IOHandlerRecord *ioh;
966 if (!fd_read && !fd_write) {
967 QLIST_FOREACH(ioh, &io_handlers, next) {
968 if (ioh->fd == fd) {
969 ioh->deleted = 1;
970 break;
973 } else {
974 QLIST_FOREACH(ioh, &io_handlers, next) {
975 if (ioh->fd == fd)
976 goto found;
978 ioh = qemu_mallocz(sizeof(IOHandlerRecord));
979 QLIST_INSERT_HEAD(&io_handlers, ioh, next);
980 found:
981 ioh->fd = fd;
982 ioh->fd_read_poll = fd_read_poll;
983 ioh->fd_read = fd_read;
984 ioh->fd_write = fd_write;
985 ioh->opaque = opaque;
986 ioh->deleted = 0;
988 return 0;
991 int qemu_set_fd_handler(int fd,
992 IOHandler *fd_read,
993 IOHandler *fd_write,
994 void *opaque)
996 return qemu_set_fd_handler2(fd, NULL, fd_read, fd_write, opaque);
999 /***********************************************************/
1000 /* machine registration */
1002 static QEMUMachine *first_machine = NULL;
1003 QEMUMachine *current_machine = NULL;
1005 int qemu_register_machine(QEMUMachine *m)
1007 QEMUMachine **pm;
1008 pm = &first_machine;
1009 while (*pm != NULL)
1010 pm = &(*pm)->next;
1011 m->next = NULL;
1012 *pm = m;
1013 return 0;
1016 static QEMUMachine *find_machine(const char *name)
1018 QEMUMachine *m;
1020 for(m = first_machine; m != NULL; m = m->next) {
1021 if (!strcmp(m->name, name))
1022 return m;
1023 if (m->alias && !strcmp(m->alias, name))
1024 return m;
1026 return NULL;
1029 static QEMUMachine *find_default_machine(void)
1031 QEMUMachine *m;
1033 for(m = first_machine; m != NULL; m = m->next) {
1034 if (m->is_default) {
1035 return m;
1038 return NULL;
1041 /***********************************************************/
1042 /* main execution loop */
1044 static void gui_update(void *opaque)
1046 uint64_t interval = GUI_REFRESH_INTERVAL;
1047 DisplayState *ds = opaque;
1048 DisplayChangeListener *dcl = ds->listeners;
1050 qemu_flush_coalesced_mmio_buffer();
1051 dpy_refresh(ds);
1053 while (dcl != NULL) {
1054 if (dcl->gui_timer_interval &&
1055 dcl->gui_timer_interval < interval)
1056 interval = dcl->gui_timer_interval;
1057 dcl = dcl->next;
1059 qemu_mod_timer(ds->gui_timer, interval + qemu_get_clock(rt_clock));
1062 static void nographic_update(void *opaque)
1064 uint64_t interval = GUI_REFRESH_INTERVAL;
1066 qemu_flush_coalesced_mmio_buffer();
1067 qemu_mod_timer(nographic_timer, interval + qemu_get_clock(rt_clock));
1070 struct vm_change_state_entry {
1071 VMChangeStateHandler *cb;
1072 void *opaque;
1073 QLIST_ENTRY (vm_change_state_entry) entries;
1076 static QLIST_HEAD(vm_change_state_head, vm_change_state_entry) vm_change_state_head;
1078 VMChangeStateEntry *qemu_add_vm_change_state_handler(VMChangeStateHandler *cb,
1079 void *opaque)
1081 VMChangeStateEntry *e;
1083 e = qemu_mallocz(sizeof (*e));
1085 e->cb = cb;
1086 e->opaque = opaque;
1087 QLIST_INSERT_HEAD(&vm_change_state_head, e, entries);
1088 return e;
1091 void qemu_del_vm_change_state_handler(VMChangeStateEntry *e)
1093 QLIST_REMOVE (e, entries);
1094 qemu_free (e);
1097 void vm_state_notify(int running, int reason)
1099 VMChangeStateEntry *e;
1101 for (e = vm_change_state_head.lh_first; e; e = e->entries.le_next) {
1102 e->cb(e->opaque, running, reason);
1106 void vm_start(void)
1108 if (!vm_running) {
1109 cpu_enable_ticks();
1110 vm_running = 1;
1111 vm_state_notify(1, 0);
1112 resume_all_vcpus();
1113 monitor_protocol_event(QEVENT_RESUME, NULL);
1117 /* reset/shutdown handler */
1119 typedef struct QEMUResetEntry {
1120 QTAILQ_ENTRY(QEMUResetEntry) entry;
1121 QEMUResetHandler *func;
1122 void *opaque;
1123 } QEMUResetEntry;
1125 static QTAILQ_HEAD(reset_handlers, QEMUResetEntry) reset_handlers =
1126 QTAILQ_HEAD_INITIALIZER(reset_handlers);
1127 static int reset_requested;
1128 static int shutdown_requested;
1129 static int powerdown_requested;
1130 int debug_requested;
1131 int vmstop_requested;
1133 int qemu_shutdown_requested(void)
1135 int r = shutdown_requested;
1136 shutdown_requested = 0;
1137 return r;
1140 int qemu_reset_requested(void)
1142 int r = reset_requested;
1143 reset_requested = 0;
1144 return r;
1147 int qemu_powerdown_requested(void)
1149 int r = powerdown_requested;
1150 powerdown_requested = 0;
1151 return r;
1154 static int qemu_debug_requested(void)
1156 int r = debug_requested;
1157 debug_requested = 0;
1158 return r;
1161 static int qemu_vmstop_requested(void)
1163 int r = vmstop_requested;
1164 vmstop_requested = 0;
1165 return r;
1168 void qemu_register_reset(QEMUResetHandler *func, void *opaque)
1170 QEMUResetEntry *re = qemu_mallocz(sizeof(QEMUResetEntry));
1172 re->func = func;
1173 re->opaque = opaque;
1174 QTAILQ_INSERT_TAIL(&reset_handlers, re, entry);
1177 void qemu_unregister_reset(QEMUResetHandler *func, void *opaque)
1179 QEMUResetEntry *re;
1181 QTAILQ_FOREACH(re, &reset_handlers, entry) {
1182 if (re->func == func && re->opaque == opaque) {
1183 QTAILQ_REMOVE(&reset_handlers, re, entry);
1184 qemu_free(re);
1185 return;
1190 void qemu_system_reset(void)
1192 QEMUResetEntry *re, *nre;
1194 /* reset all devices */
1195 QTAILQ_FOREACH_SAFE(re, &reset_handlers, entry, nre) {
1196 re->func(re->opaque);
1198 monitor_protocol_event(QEVENT_RESET, NULL);
1199 cpu_synchronize_all_post_reset();
1202 void qemu_system_reset_request(void)
1204 if (no_reboot) {
1205 shutdown_requested = 1;
1206 } else {
1207 reset_requested = 1;
1209 qemu_notify_event();
1212 void qemu_system_shutdown_request(void)
1214 shutdown_requested = 1;
1215 qemu_notify_event();
1218 void qemu_system_powerdown_request(void)
1220 powerdown_requested = 1;
1221 qemu_notify_event();
1224 void main_loop_wait(int nonblocking)
1226 IOHandlerRecord *ioh;
1227 fd_set rfds, wfds, xfds;
1228 int ret, nfds;
1229 struct timeval tv;
1230 int timeout;
1232 if (nonblocking)
1233 timeout = 0;
1234 else {
1235 timeout = qemu_calculate_timeout();
1236 qemu_bh_update_timeout(&timeout);
1239 os_host_main_loop_wait(&timeout);
1241 /* poll any events */
1242 /* XXX: separate device handlers from system ones */
1243 nfds = -1;
1244 FD_ZERO(&rfds);
1245 FD_ZERO(&wfds);
1246 FD_ZERO(&xfds);
1247 QLIST_FOREACH(ioh, &io_handlers, next) {
1248 if (ioh->deleted)
1249 continue;
1250 if (ioh->fd_read &&
1251 (!ioh->fd_read_poll ||
1252 ioh->fd_read_poll(ioh->opaque) != 0)) {
1253 FD_SET(ioh->fd, &rfds);
1254 if (ioh->fd > nfds)
1255 nfds = ioh->fd;
1257 if (ioh->fd_write) {
1258 FD_SET(ioh->fd, &wfds);
1259 if (ioh->fd > nfds)
1260 nfds = ioh->fd;
1264 tv.tv_sec = timeout / 1000;
1265 tv.tv_usec = (timeout % 1000) * 1000;
1267 slirp_select_fill(&nfds, &rfds, &wfds, &xfds);
1269 qemu_mutex_unlock_iothread();
1270 ret = select(nfds + 1, &rfds, &wfds, &xfds, &tv);
1271 qemu_mutex_lock_iothread();
1272 if (ret > 0) {
1273 IOHandlerRecord *pioh;
1275 QLIST_FOREACH_SAFE(ioh, &io_handlers, next, pioh) {
1276 if (ioh->deleted) {
1277 QLIST_REMOVE(ioh, next);
1278 qemu_free(ioh);
1279 continue;
1281 if (ioh->fd_read && FD_ISSET(ioh->fd, &rfds)) {
1282 ioh->fd_read(ioh->opaque);
1284 if (ioh->fd_write && FD_ISSET(ioh->fd, &wfds)) {
1285 ioh->fd_write(ioh->opaque);
1290 slirp_select_poll(&rfds, &wfds, &xfds, (ret < 0));
1292 qemu_run_all_timers();
1294 /* Check bottom-halves last in case any of the earlier events triggered
1295 them. */
1296 qemu_bh_poll();
1300 static int vm_can_run(void)
1302 if (powerdown_requested)
1303 return 0;
1304 if (reset_requested)
1305 return 0;
1306 if (shutdown_requested)
1307 return 0;
1308 if (debug_requested)
1309 return 0;
1310 return 1;
1313 qemu_irq qemu_system_powerdown;
1315 static void main_loop(void)
1317 int r;
1319 qemu_main_loop_start();
1321 for (;;) {
1322 do {
1323 bool nonblocking = false;
1324 #ifdef CONFIG_PROFILER
1325 int64_t ti;
1326 #endif
1327 #ifndef CONFIG_IOTHREAD
1328 nonblocking = cpu_exec_all();
1329 #endif
1330 #ifdef CONFIG_PROFILER
1331 ti = profile_getclock();
1332 #endif
1333 main_loop_wait(nonblocking);
1334 #ifdef CONFIG_PROFILER
1335 dev_time += profile_getclock() - ti;
1336 #endif
1337 } while (vm_can_run());
1339 if ((r = qemu_debug_requested())) {
1340 vm_stop(r);
1342 if (qemu_shutdown_requested()) {
1343 monitor_protocol_event(QEVENT_SHUTDOWN, NULL);
1344 if (no_shutdown) {
1345 vm_stop(0);
1346 no_shutdown = 0;
1347 } else
1348 break;
1350 if (qemu_reset_requested()) {
1351 pause_all_vcpus();
1352 qemu_system_reset();
1353 resume_all_vcpus();
1355 if (qemu_powerdown_requested()) {
1356 monitor_protocol_event(QEVENT_POWERDOWN, NULL);
1357 qemu_irq_raise(qemu_system_powerdown);
1359 if ((r = qemu_vmstop_requested())) {
1360 vm_stop(r);
1363 bdrv_close_all();
1364 pause_all_vcpus();
1367 static void version(void)
1369 printf("QEMU emulator version " QEMU_VERSION QEMU_PKGVERSION ", Copyright (c) 2003-2008 Fabrice Bellard\n");
1372 static void help(int exitcode)
1374 const char *options_help =
1375 #define DEF(option, opt_arg, opt_enum, opt_help, arch_mask) \
1376 opt_help
1377 #define DEFHEADING(text) stringify(text) "\n"
1378 #include "qemu-options.def"
1379 #undef DEF
1380 #undef DEFHEADING
1381 #undef GEN_DOCS
1383 version();
1384 printf("usage: %s [options] [disk_image]\n"
1385 "\n"
1386 "'disk_image' is a raw hard disk image for IDE hard disk 0\n"
1387 "\n"
1388 "%s\n"
1389 "During emulation, the following keys are useful:\n"
1390 "ctrl-alt-f toggle full screen\n"
1391 "ctrl-alt-n switch to virtual console 'n'\n"
1392 "ctrl-alt toggle mouse and keyboard grab\n"
1393 "\n"
1394 "When using -nographic, press 'ctrl-a h' to get some help.\n",
1395 "qemu",
1396 options_help);
1397 exit(exitcode);
1400 #define HAS_ARG 0x0001
1402 typedef struct QEMUOption {
1403 const char *name;
1404 int flags;
1405 int index;
1406 uint32_t arch_mask;
1407 } QEMUOption;
1409 static const QEMUOption qemu_options[] = {
1410 { "h", 0, QEMU_OPTION_h, QEMU_ARCH_ALL },
1411 #define DEF(option, opt_arg, opt_enum, opt_help, arch_mask) \
1412 { option, opt_arg, opt_enum, arch_mask },
1413 #define DEFHEADING(text)
1414 #include "qemu-options.def"
1415 #undef DEF
1416 #undef DEFHEADING
1417 #undef GEN_DOCS
1418 { NULL },
1420 static void select_vgahw (const char *p)
1422 const char *opts;
1424 default_vga = 0;
1425 vga_interface_type = VGA_NONE;
1426 if (strstart(p, "std", &opts)) {
1427 vga_interface_type = VGA_STD;
1428 } else if (strstart(p, "cirrus", &opts)) {
1429 vga_interface_type = VGA_CIRRUS;
1430 } else if (strstart(p, "vmware", &opts)) {
1431 vga_interface_type = VGA_VMWARE;
1432 } else if (strstart(p, "xenfb", &opts)) {
1433 vga_interface_type = VGA_XENFB;
1434 } else if (!strstart(p, "none", &opts)) {
1435 invalid_vga:
1436 fprintf(stderr, "Unknown vga type: %s\n", p);
1437 exit(1);
1439 while (*opts) {
1440 const char *nextopt;
1442 if (strstart(opts, ",retrace=", &nextopt)) {
1443 opts = nextopt;
1444 if (strstart(opts, "dumb", &nextopt))
1445 vga_retrace_method = VGA_RETRACE_DUMB;
1446 else if (strstart(opts, "precise", &nextopt))
1447 vga_retrace_method = VGA_RETRACE_PRECISE;
1448 else goto invalid_vga;
1449 } else goto invalid_vga;
1450 opts = nextopt;
1454 static int balloon_parse(const char *arg)
1456 QemuOpts *opts;
1458 if (strcmp(arg, "none") == 0) {
1459 return 0;
1462 if (!strncmp(arg, "virtio", 6)) {
1463 if (arg[6] == ',') {
1464 /* have params -> parse them */
1465 opts = qemu_opts_parse(qemu_find_opts("device"), arg+7, 0);
1466 if (!opts)
1467 return -1;
1468 } else {
1469 /* create empty opts */
1470 opts = qemu_opts_create(qemu_find_opts("device"), NULL, 0);
1472 qemu_opt_set(opts, "driver", "virtio-balloon-pci");
1473 return 0;
1476 return -1;
1479 char *qemu_find_file(int type, const char *name)
1481 int len;
1482 const char *subdir;
1483 char *buf;
1485 /* If name contains path separators then try it as a straight path. */
1486 if ((strchr(name, '/') || strchr(name, '\\'))
1487 && access(name, R_OK) == 0) {
1488 return qemu_strdup(name);
1490 switch (type) {
1491 case QEMU_FILE_TYPE_BIOS:
1492 subdir = "";
1493 break;
1494 case QEMU_FILE_TYPE_KEYMAP:
1495 subdir = "keymaps/";
1496 break;
1497 default:
1498 abort();
1500 len = strlen(data_dir) + strlen(name) + strlen(subdir) + 2;
1501 buf = qemu_mallocz(len);
1502 snprintf(buf, len, "%s/%s%s", data_dir, subdir, name);
1503 if (access(buf, R_OK)) {
1504 qemu_free(buf);
1505 return NULL;
1507 return buf;
1510 static int device_help_func(QemuOpts *opts, void *opaque)
1512 return qdev_device_help(opts);
1515 static int device_init_func(QemuOpts *opts, void *opaque)
1517 DeviceState *dev;
1519 dev = qdev_device_add(opts);
1520 if (!dev)
1521 return -1;
1522 return 0;
1525 static int chardev_init_func(QemuOpts *opts, void *opaque)
1527 CharDriverState *chr;
1529 chr = qemu_chr_open_opts(opts, NULL);
1530 if (!chr)
1531 return -1;
1532 return 0;
1535 #ifdef CONFIG_VIRTFS
1536 static int fsdev_init_func(QemuOpts *opts, void *opaque)
1538 int ret;
1539 ret = qemu_fsdev_add(opts);
1541 return ret;
1543 #endif
1545 static int mon_init_func(QemuOpts *opts, void *opaque)
1547 CharDriverState *chr;
1548 const char *chardev;
1549 const char *mode;
1550 int flags;
1552 mode = qemu_opt_get(opts, "mode");
1553 if (mode == NULL) {
1554 mode = "readline";
1556 if (strcmp(mode, "readline") == 0) {
1557 flags = MONITOR_USE_READLINE;
1558 } else if (strcmp(mode, "control") == 0) {
1559 flags = MONITOR_USE_CONTROL;
1560 } else {
1561 fprintf(stderr, "unknown monitor mode \"%s\"\n", mode);
1562 exit(1);
1565 if (qemu_opt_get_bool(opts, "default", 0))
1566 flags |= MONITOR_IS_DEFAULT;
1568 chardev = qemu_opt_get(opts, "chardev");
1569 chr = qemu_chr_find(chardev);
1570 if (chr == NULL) {
1571 fprintf(stderr, "chardev \"%s\" not found\n", chardev);
1572 exit(1);
1575 monitor_init(chr, flags);
1576 return 0;
1579 static void monitor_parse(const char *optarg, const char *mode)
1581 static int monitor_device_index = 0;
1582 QemuOpts *opts;
1583 const char *p;
1584 char label[32];
1585 int def = 0;
1587 if (strstart(optarg, "chardev:", &p)) {
1588 snprintf(label, sizeof(label), "%s", p);
1589 } else {
1590 snprintf(label, sizeof(label), "compat_monitor%d",
1591 monitor_device_index);
1592 if (monitor_device_index == 0) {
1593 def = 1;
1595 opts = qemu_chr_parse_compat(label, optarg);
1596 if (!opts) {
1597 fprintf(stderr, "parse error: %s\n", optarg);
1598 exit(1);
1602 opts = qemu_opts_create(qemu_find_opts("mon"), label, 1);
1603 if (!opts) {
1604 fprintf(stderr, "duplicate chardev: %s\n", label);
1605 exit(1);
1607 qemu_opt_set(opts, "mode", mode);
1608 qemu_opt_set(opts, "chardev", label);
1609 if (def)
1610 qemu_opt_set(opts, "default", "on");
1611 monitor_device_index++;
1614 struct device_config {
1615 enum {
1616 DEV_USB, /* -usbdevice */
1617 DEV_BT, /* -bt */
1618 DEV_SERIAL, /* -serial */
1619 DEV_PARALLEL, /* -parallel */
1620 DEV_VIRTCON, /* -virtioconsole */
1621 DEV_DEBUGCON, /* -debugcon */
1622 } type;
1623 const char *cmdline;
1624 QTAILQ_ENTRY(device_config) next;
1626 QTAILQ_HEAD(, device_config) device_configs = QTAILQ_HEAD_INITIALIZER(device_configs);
1628 static void add_device_config(int type, const char *cmdline)
1630 struct device_config *conf;
1632 conf = qemu_mallocz(sizeof(*conf));
1633 conf->type = type;
1634 conf->cmdline = cmdline;
1635 QTAILQ_INSERT_TAIL(&device_configs, conf, next);
1638 static int foreach_device_config(int type, int (*func)(const char *cmdline))
1640 struct device_config *conf;
1641 int rc;
1643 QTAILQ_FOREACH(conf, &device_configs, next) {
1644 if (conf->type != type)
1645 continue;
1646 rc = func(conf->cmdline);
1647 if (0 != rc)
1648 return rc;
1650 return 0;
1653 static int serial_parse(const char *devname)
1655 static int index = 0;
1656 char label[32];
1658 if (strcmp(devname, "none") == 0)
1659 return 0;
1660 if (index == MAX_SERIAL_PORTS) {
1661 fprintf(stderr, "qemu: too many serial ports\n");
1662 exit(1);
1664 snprintf(label, sizeof(label), "serial%d", index);
1665 serial_hds[index] = qemu_chr_open(label, devname, NULL);
1666 if (!serial_hds[index]) {
1667 fprintf(stderr, "qemu: could not open serial device '%s': %s\n",
1668 devname, strerror(errno));
1669 return -1;
1671 index++;
1672 return 0;
1675 static int parallel_parse(const char *devname)
1677 static int index = 0;
1678 char label[32];
1680 if (strcmp(devname, "none") == 0)
1681 return 0;
1682 if (index == MAX_PARALLEL_PORTS) {
1683 fprintf(stderr, "qemu: too many parallel ports\n");
1684 exit(1);
1686 snprintf(label, sizeof(label), "parallel%d", index);
1687 parallel_hds[index] = qemu_chr_open(label, devname, NULL);
1688 if (!parallel_hds[index]) {
1689 fprintf(stderr, "qemu: could not open parallel device '%s': %s\n",
1690 devname, strerror(errno));
1691 return -1;
1693 index++;
1694 return 0;
1697 static int virtcon_parse(const char *devname)
1699 QemuOptsList *device = qemu_find_opts("device");
1700 static int index = 0;
1701 char label[32];
1702 QemuOpts *bus_opts, *dev_opts;
1704 if (strcmp(devname, "none") == 0)
1705 return 0;
1706 if (index == MAX_VIRTIO_CONSOLES) {
1707 fprintf(stderr, "qemu: too many virtio consoles\n");
1708 exit(1);
1711 bus_opts = qemu_opts_create(device, NULL, 0);
1712 qemu_opt_set(bus_opts, "driver", "virtio-serial");
1714 dev_opts = qemu_opts_create(device, NULL, 0);
1715 qemu_opt_set(dev_opts, "driver", "virtconsole");
1717 snprintf(label, sizeof(label), "virtcon%d", index);
1718 virtcon_hds[index] = qemu_chr_open(label, devname, NULL);
1719 if (!virtcon_hds[index]) {
1720 fprintf(stderr, "qemu: could not open virtio console '%s': %s\n",
1721 devname, strerror(errno));
1722 return -1;
1724 qemu_opt_set(dev_opts, "chardev", label);
1726 index++;
1727 return 0;
1730 static int debugcon_parse(const char *devname)
1732 QemuOpts *opts;
1734 if (!qemu_chr_open("debugcon", devname, NULL)) {
1735 exit(1);
1737 opts = qemu_opts_create(qemu_find_opts("device"), "debugcon", 1);
1738 if (!opts) {
1739 fprintf(stderr, "qemu: already have a debugcon device\n");
1740 exit(1);
1742 qemu_opt_set(opts, "driver", "isa-debugcon");
1743 qemu_opt_set(opts, "chardev", "debugcon");
1744 return 0;
1747 void qemu_add_exit_notifier(Notifier *notify)
1749 notifier_list_add(&exit_notifiers, notify);
1752 void qemu_remove_exit_notifier(Notifier *notify)
1754 notifier_list_remove(&exit_notifiers, notify);
1757 static void qemu_run_exit_notifiers(void)
1759 notifier_list_notify(&exit_notifiers);
1762 static const QEMUOption *lookup_opt(int argc, char **argv,
1763 const char **poptarg, int *poptind)
1765 const QEMUOption *popt;
1766 int optind = *poptind;
1767 char *r = argv[optind];
1768 const char *optarg;
1770 loc_set_cmdline(argv, optind, 1);
1771 optind++;
1772 /* Treat --foo the same as -foo. */
1773 if (r[1] == '-')
1774 r++;
1775 popt = qemu_options;
1776 for(;;) {
1777 if (!popt->name) {
1778 error_report("invalid option");
1779 exit(1);
1781 if (!strcmp(popt->name, r + 1))
1782 break;
1783 popt++;
1785 if (popt->flags & HAS_ARG) {
1786 if (optind >= argc) {
1787 error_report("requires an argument");
1788 exit(1);
1790 optarg = argv[optind++];
1791 loc_set_cmdline(argv, optind - 2, 2);
1792 } else {
1793 optarg = NULL;
1796 *poptarg = optarg;
1797 *poptind = optind;
1799 return popt;
1802 int main(int argc, char **argv, char **envp)
1804 const char *gdbstub_dev = NULL;
1805 int i;
1806 int snapshot, linux_boot;
1807 const char *icount_option = NULL;
1808 const char *initrd_filename;
1809 const char *kernel_filename, *kernel_cmdline;
1810 char boot_devices[33] = "cad"; /* default to HD->floppy->CD-ROM */
1811 DisplayState *ds;
1812 DisplayChangeListener *dcl;
1813 int cyls, heads, secs, translation;
1814 QemuOpts *hda_opts = NULL, *opts;
1815 QemuOptsList *olist;
1816 int optind;
1817 const char *optarg;
1818 const char *loadvm = NULL;
1819 QEMUMachine *machine;
1820 const char *cpu_model;
1821 int tb_size;
1822 const char *pid_file = NULL;
1823 const char *incoming = NULL;
1824 int show_vnc_port = 0;
1825 int defconfig = 1;
1827 #ifdef CONFIG_SIMPLE_TRACE
1828 const char *trace_file = NULL;
1829 #endif
1830 atexit(qemu_run_exit_notifiers);
1831 error_set_progname(argv[0]);
1833 init_clocks();
1835 qemu_cache_utils_init(envp);
1837 QLIST_INIT (&vm_change_state_head);
1838 os_setup_early_signal_handling();
1840 module_call_init(MODULE_INIT_MACHINE);
1841 machine = find_default_machine();
1842 cpu_model = NULL;
1843 initrd_filename = NULL;
1844 ram_size = 0;
1845 snapshot = 0;
1846 kernel_filename = NULL;
1847 kernel_cmdline = "";
1848 cyls = heads = secs = 0;
1849 translation = BIOS_ATA_TRANSLATION_AUTO;
1851 for (i = 0; i < MAX_NODES; i++) {
1852 node_mem[i] = 0;
1853 node_cpumask[i] = 0;
1856 nb_numa_nodes = 0;
1857 nb_nics = 0;
1859 tb_size = 0;
1860 autostart= 1;
1862 #ifdef CONFIG_VIRTFS
1863 qemu_add_opts(&qemu_fsdev_opts);
1864 qemu_add_opts(&qemu_virtfs_opts);
1865 #endif
1867 /* first pass of option parsing */
1868 optind = 1;
1869 while (optind < argc) {
1870 if (argv[optind][0] != '-') {
1871 /* disk image */
1872 optind++;
1873 continue;
1874 } else {
1875 const QEMUOption *popt;
1877 popt = lookup_opt(argc, argv, &optarg, &optind);
1878 switch (popt->index) {
1879 case QEMU_OPTION_nodefconfig:
1880 defconfig=0;
1881 break;
1886 if (defconfig) {
1887 int ret;
1889 ret = qemu_read_config_file(CONFIG_QEMU_CONFDIR "/qemu.conf");
1890 if (ret < 0 && ret != -ENOENT) {
1891 exit(1);
1894 ret = qemu_read_config_file(arch_config_name);
1895 if (ret < 0 && ret != -ENOENT) {
1896 exit(1);
1899 cpudef_init();
1901 /* second pass of option parsing */
1902 optind = 1;
1903 for(;;) {
1904 if (optind >= argc)
1905 break;
1906 if (argv[optind][0] != '-') {
1907 hda_opts = drive_add(argv[optind++], HD_ALIAS, 0);
1908 } else {
1909 const QEMUOption *popt;
1911 popt = lookup_opt(argc, argv, &optarg, &optind);
1912 if (!(popt->arch_mask & arch_type)) {
1913 printf("Option %s not supported for this target\n", popt->name);
1914 exit(1);
1916 switch(popt->index) {
1917 case QEMU_OPTION_M:
1918 machine = find_machine(optarg);
1919 if (!machine) {
1920 QEMUMachine *m;
1921 printf("Supported machines are:\n");
1922 for(m = first_machine; m != NULL; m = m->next) {
1923 if (m->alias)
1924 printf("%-10s %s (alias of %s)\n",
1925 m->alias, m->desc, m->name);
1926 printf("%-10s %s%s\n",
1927 m->name, m->desc,
1928 m->is_default ? " (default)" : "");
1930 exit(*optarg != '?');
1932 break;
1933 case QEMU_OPTION_cpu:
1934 /* hw initialization will check this */
1935 if (*optarg == '?') {
1936 list_cpus(stdout, &fprintf, optarg);
1937 exit(0);
1938 } else {
1939 cpu_model = optarg;
1941 break;
1942 case QEMU_OPTION_initrd:
1943 initrd_filename = optarg;
1944 break;
1945 case QEMU_OPTION_hda:
1946 if (cyls == 0)
1947 hda_opts = drive_add(optarg, HD_ALIAS, 0);
1948 else
1949 hda_opts = drive_add(optarg, HD_ALIAS
1950 ",cyls=%d,heads=%d,secs=%d%s",
1951 0, cyls, heads, secs,
1952 translation == BIOS_ATA_TRANSLATION_LBA ?
1953 ",trans=lba" :
1954 translation == BIOS_ATA_TRANSLATION_NONE ?
1955 ",trans=none" : "");
1956 break;
1957 case QEMU_OPTION_hdb:
1958 case QEMU_OPTION_hdc:
1959 case QEMU_OPTION_hdd:
1960 drive_add(optarg, HD_ALIAS, popt->index - QEMU_OPTION_hda);
1961 break;
1962 case QEMU_OPTION_drive:
1963 drive_add(NULL, "%s", optarg);
1964 break;
1965 case QEMU_OPTION_set:
1966 if (qemu_set_option(optarg) != 0)
1967 exit(1);
1968 break;
1969 case QEMU_OPTION_global:
1970 if (qemu_global_option(optarg) != 0)
1971 exit(1);
1972 break;
1973 case QEMU_OPTION_mtdblock:
1974 drive_add(optarg, MTD_ALIAS);
1975 break;
1976 case QEMU_OPTION_sd:
1977 drive_add(optarg, SD_ALIAS);
1978 break;
1979 case QEMU_OPTION_pflash:
1980 drive_add(optarg, PFLASH_ALIAS);
1981 break;
1982 case QEMU_OPTION_snapshot:
1983 snapshot = 1;
1984 break;
1985 case QEMU_OPTION_hdachs:
1987 const char *p;
1988 p = optarg;
1989 cyls = strtol(p, (char **)&p, 0);
1990 if (cyls < 1 || cyls > 16383)
1991 goto chs_fail;
1992 if (*p != ',')
1993 goto chs_fail;
1994 p++;
1995 heads = strtol(p, (char **)&p, 0);
1996 if (heads < 1 || heads > 16)
1997 goto chs_fail;
1998 if (*p != ',')
1999 goto chs_fail;
2000 p++;
2001 secs = strtol(p, (char **)&p, 0);
2002 if (secs < 1 || secs > 63)
2003 goto chs_fail;
2004 if (*p == ',') {
2005 p++;
2006 if (!strcmp(p, "none"))
2007 translation = BIOS_ATA_TRANSLATION_NONE;
2008 else if (!strcmp(p, "lba"))
2009 translation = BIOS_ATA_TRANSLATION_LBA;
2010 else if (!strcmp(p, "auto"))
2011 translation = BIOS_ATA_TRANSLATION_AUTO;
2012 else
2013 goto chs_fail;
2014 } else if (*p != '\0') {
2015 chs_fail:
2016 fprintf(stderr, "qemu: invalid physical CHS format\n");
2017 exit(1);
2019 if (hda_opts != NULL) {
2020 char num[16];
2021 snprintf(num, sizeof(num), "%d", cyls);
2022 qemu_opt_set(hda_opts, "cyls", num);
2023 snprintf(num, sizeof(num), "%d", heads);
2024 qemu_opt_set(hda_opts, "heads", num);
2025 snprintf(num, sizeof(num), "%d", secs);
2026 qemu_opt_set(hda_opts, "secs", num);
2027 if (translation == BIOS_ATA_TRANSLATION_LBA)
2028 qemu_opt_set(hda_opts, "trans", "lba");
2029 if (translation == BIOS_ATA_TRANSLATION_NONE)
2030 qemu_opt_set(hda_opts, "trans", "none");
2033 break;
2034 case QEMU_OPTION_numa:
2035 if (nb_numa_nodes >= MAX_NODES) {
2036 fprintf(stderr, "qemu: too many NUMA nodes\n");
2037 exit(1);
2039 numa_add(optarg);
2040 break;
2041 case QEMU_OPTION_nographic:
2042 display_type = DT_NOGRAPHIC;
2043 break;
2044 #ifdef CONFIG_CURSES
2045 case QEMU_OPTION_curses:
2046 display_type = DT_CURSES;
2047 break;
2048 #endif
2049 case QEMU_OPTION_portrait:
2050 graphic_rotate = 1;
2051 break;
2052 case QEMU_OPTION_kernel:
2053 kernel_filename = optarg;
2054 break;
2055 case QEMU_OPTION_append:
2056 kernel_cmdline = optarg;
2057 break;
2058 case QEMU_OPTION_cdrom:
2059 drive_add(optarg, CDROM_ALIAS);
2060 break;
2061 case QEMU_OPTION_boot:
2063 static const char * const params[] = {
2064 "order", "once", "menu", NULL
2066 char buf[sizeof(boot_devices)];
2067 char *standard_boot_devices;
2068 int legacy = 0;
2070 if (!strchr(optarg, '=')) {
2071 legacy = 1;
2072 pstrcpy(buf, sizeof(buf), optarg);
2073 } else if (check_params(buf, sizeof(buf), params, optarg) < 0) {
2074 fprintf(stderr,
2075 "qemu: unknown boot parameter '%s' in '%s'\n",
2076 buf, optarg);
2077 exit(1);
2080 if (legacy ||
2081 get_param_value(buf, sizeof(buf), "order", optarg)) {
2082 validate_bootdevices(buf);
2083 pstrcpy(boot_devices, sizeof(boot_devices), buf);
2085 if (!legacy) {
2086 if (get_param_value(buf, sizeof(buf),
2087 "once", optarg)) {
2088 validate_bootdevices(buf);
2089 standard_boot_devices = qemu_strdup(boot_devices);
2090 pstrcpy(boot_devices, sizeof(boot_devices), buf);
2091 qemu_register_reset(restore_boot_devices,
2092 standard_boot_devices);
2094 if (get_param_value(buf, sizeof(buf),
2095 "menu", optarg)) {
2096 if (!strcmp(buf, "on")) {
2097 boot_menu = 1;
2098 } else if (!strcmp(buf, "off")) {
2099 boot_menu = 0;
2100 } else {
2101 fprintf(stderr,
2102 "qemu: invalid option value '%s'\n",
2103 buf);
2104 exit(1);
2109 break;
2110 case QEMU_OPTION_fda:
2111 case QEMU_OPTION_fdb:
2112 drive_add(optarg, FD_ALIAS, popt->index - QEMU_OPTION_fda);
2113 break;
2114 case QEMU_OPTION_no_fd_bootchk:
2115 fd_bootchk = 0;
2116 break;
2117 case QEMU_OPTION_netdev:
2118 if (net_client_parse(qemu_find_opts("netdev"), optarg) == -1) {
2119 exit(1);
2121 break;
2122 case QEMU_OPTION_net:
2123 if (net_client_parse(qemu_find_opts("net"), optarg) == -1) {
2124 exit(1);
2126 break;
2127 #ifdef CONFIG_SLIRP
2128 case QEMU_OPTION_tftp:
2129 legacy_tftp_prefix = optarg;
2130 break;
2131 case QEMU_OPTION_bootp:
2132 legacy_bootp_filename = optarg;
2133 break;
2134 case QEMU_OPTION_redir:
2135 if (net_slirp_redir(optarg) < 0)
2136 exit(1);
2137 break;
2138 #endif
2139 case QEMU_OPTION_bt:
2140 add_device_config(DEV_BT, optarg);
2141 break;
2142 case QEMU_OPTION_audio_help:
2143 if (!(audio_available())) {
2144 printf("Option %s not supported for this target\n", popt->name);
2145 exit(1);
2147 AUD_help ();
2148 exit (0);
2149 break;
2150 case QEMU_OPTION_soundhw:
2151 if (!(audio_available())) {
2152 printf("Option %s not supported for this target\n", popt->name);
2153 exit(1);
2155 select_soundhw (optarg);
2156 break;
2157 case QEMU_OPTION_h:
2158 help(0);
2159 break;
2160 case QEMU_OPTION_version:
2161 version();
2162 exit(0);
2163 break;
2164 case QEMU_OPTION_m: {
2165 uint64_t value;
2166 char *ptr;
2168 value = strtoul(optarg, &ptr, 10);
2169 switch (*ptr) {
2170 case 0: case 'M': case 'm':
2171 value <<= 20;
2172 break;
2173 case 'G': case 'g':
2174 value <<= 30;
2175 break;
2176 default:
2177 fprintf(stderr, "qemu: invalid ram size: %s\n", optarg);
2178 exit(1);
2181 /* On 32-bit hosts, QEMU is limited by virtual address space */
2182 if (value > (2047 << 20) && HOST_LONG_BITS == 32) {
2183 fprintf(stderr, "qemu: at most 2047 MB RAM can be simulated\n");
2184 exit(1);
2186 if (value != (uint64_t)(ram_addr_t)value) {
2187 fprintf(stderr, "qemu: ram size too large\n");
2188 exit(1);
2190 ram_size = value;
2191 break;
2193 case QEMU_OPTION_mempath:
2194 mem_path = optarg;
2195 break;
2196 #ifdef MAP_POPULATE
2197 case QEMU_OPTION_mem_prealloc:
2198 mem_prealloc = 1;
2199 break;
2200 #endif
2201 case QEMU_OPTION_d:
2202 set_cpu_log(optarg);
2203 break;
2204 case QEMU_OPTION_s:
2205 gdbstub_dev = "tcp::" DEFAULT_GDBSTUB_PORT;
2206 break;
2207 case QEMU_OPTION_gdb:
2208 gdbstub_dev = optarg;
2209 break;
2210 case QEMU_OPTION_L:
2211 data_dir = optarg;
2212 break;
2213 case QEMU_OPTION_bios:
2214 bios_name = optarg;
2215 break;
2216 case QEMU_OPTION_singlestep:
2217 singlestep = 1;
2218 break;
2219 case QEMU_OPTION_S:
2220 autostart = 0;
2221 break;
2222 case QEMU_OPTION_k:
2223 keyboard_layout = optarg;
2224 break;
2225 case QEMU_OPTION_localtime:
2226 rtc_utc = 0;
2227 break;
2228 case QEMU_OPTION_vga:
2229 select_vgahw (optarg);
2230 break;
2231 case QEMU_OPTION_g:
2233 const char *p;
2234 int w, h, depth;
2235 p = optarg;
2236 w = strtol(p, (char **)&p, 10);
2237 if (w <= 0) {
2238 graphic_error:
2239 fprintf(stderr, "qemu: invalid resolution or depth\n");
2240 exit(1);
2242 if (*p != 'x')
2243 goto graphic_error;
2244 p++;
2245 h = strtol(p, (char **)&p, 10);
2246 if (h <= 0)
2247 goto graphic_error;
2248 if (*p == 'x') {
2249 p++;
2250 depth = strtol(p, (char **)&p, 10);
2251 if (depth != 8 && depth != 15 && depth != 16 &&
2252 depth != 24 && depth != 32)
2253 goto graphic_error;
2254 } else if (*p == '\0') {
2255 depth = graphic_depth;
2256 } else {
2257 goto graphic_error;
2260 graphic_width = w;
2261 graphic_height = h;
2262 graphic_depth = depth;
2264 break;
2265 case QEMU_OPTION_echr:
2267 char *r;
2268 term_escape_char = strtol(optarg, &r, 0);
2269 if (r == optarg)
2270 printf("Bad argument to echr\n");
2271 break;
2273 case QEMU_OPTION_monitor:
2274 monitor_parse(optarg, "readline");
2275 default_monitor = 0;
2276 break;
2277 case QEMU_OPTION_qmp:
2278 monitor_parse(optarg, "control");
2279 default_monitor = 0;
2280 break;
2281 case QEMU_OPTION_mon:
2282 opts = qemu_opts_parse(qemu_find_opts("mon"), optarg, 1);
2283 if (!opts) {
2284 exit(1);
2286 default_monitor = 0;
2287 break;
2288 case QEMU_OPTION_chardev:
2289 opts = qemu_opts_parse(qemu_find_opts("chardev"), optarg, 1);
2290 if (!opts) {
2291 exit(1);
2293 break;
2294 case QEMU_OPTION_fsdev:
2295 olist = qemu_find_opts("fsdev");
2296 if (!olist) {
2297 fprintf(stderr, "fsdev is not supported by this qemu build.\n");
2298 exit(1);
2300 opts = qemu_opts_parse(olist, optarg, 1);
2301 if (!opts) {
2302 fprintf(stderr, "parse error: %s\n", optarg);
2303 exit(1);
2305 break;
2306 case QEMU_OPTION_virtfs: {
2307 char *arg_fsdev = NULL;
2308 char *arg_9p = NULL;
2309 int len = 0;
2311 olist = qemu_find_opts("virtfs");
2312 if (!olist) {
2313 fprintf(stderr, "virtfs is not supported by this qemu build.\n");
2314 exit(1);
2316 opts = qemu_opts_parse(olist, optarg, 1);
2317 if (!opts) {
2318 fprintf(stderr, "parse error: %s\n", optarg);
2319 exit(1);
2322 if (qemu_opt_get(opts, "fstype") == NULL ||
2323 qemu_opt_get(opts, "mount_tag") == NULL ||
2324 qemu_opt_get(opts, "path") == NULL ||
2325 qemu_opt_get(opts, "security_model") == NULL) {
2326 fprintf(stderr, "Usage: -virtfs fstype,path=/share_path/,"
2327 "security_model=[mapped|passthrough|none],"
2328 "mnt_tag=tag.\n");
2329 exit(1);
2332 len = strlen(",id=,path=,security_model=");
2333 len += strlen(qemu_opt_get(opts, "fstype"));
2334 len += strlen(qemu_opt_get(opts, "mount_tag"));
2335 len += strlen(qemu_opt_get(opts, "path"));
2336 len += strlen(qemu_opt_get(opts, "security_model"));
2337 arg_fsdev = qemu_malloc((len + 1) * sizeof(*arg_fsdev));
2339 snprintf(arg_fsdev, (len + 1) * sizeof(*arg_fsdev),
2340 "%s,id=%s,path=%s,security_model=%s",
2341 qemu_opt_get(opts, "fstype"),
2342 qemu_opt_get(opts, "mount_tag"),
2343 qemu_opt_get(opts, "path"),
2344 qemu_opt_get(opts, "security_model"));
2346 len = strlen("virtio-9p-pci,fsdev=,mount_tag=");
2347 len += 2*strlen(qemu_opt_get(opts, "mount_tag"));
2348 arg_9p = qemu_malloc((len + 1) * sizeof(*arg_9p));
2350 snprintf(arg_9p, (len + 1) * sizeof(*arg_9p),
2351 "virtio-9p-pci,fsdev=%s,mount_tag=%s",
2352 qemu_opt_get(opts, "mount_tag"),
2353 qemu_opt_get(opts, "mount_tag"));
2355 if (!qemu_opts_parse(qemu_find_opts("fsdev"), arg_fsdev, 1)) {
2356 fprintf(stderr, "parse error [fsdev]: %s\n", optarg);
2357 exit(1);
2360 if (!qemu_opts_parse(qemu_find_opts("device"), arg_9p, 1)) {
2361 fprintf(stderr, "parse error [device]: %s\n", optarg);
2362 exit(1);
2365 qemu_free(arg_fsdev);
2366 qemu_free(arg_9p);
2367 break;
2369 case QEMU_OPTION_serial:
2370 add_device_config(DEV_SERIAL, optarg);
2371 default_serial = 0;
2372 if (strncmp(optarg, "mon:", 4) == 0) {
2373 default_monitor = 0;
2375 break;
2376 case QEMU_OPTION_watchdog:
2377 if (watchdog) {
2378 fprintf(stderr,
2379 "qemu: only one watchdog option may be given\n");
2380 return 1;
2382 watchdog = optarg;
2383 break;
2384 case QEMU_OPTION_watchdog_action:
2385 if (select_watchdog_action(optarg) == -1) {
2386 fprintf(stderr, "Unknown -watchdog-action parameter\n");
2387 exit(1);
2389 break;
2390 case QEMU_OPTION_virtiocon:
2391 add_device_config(DEV_VIRTCON, optarg);
2392 default_virtcon = 0;
2393 if (strncmp(optarg, "mon:", 4) == 0) {
2394 default_monitor = 0;
2396 break;
2397 case QEMU_OPTION_parallel:
2398 add_device_config(DEV_PARALLEL, optarg);
2399 default_parallel = 0;
2400 if (strncmp(optarg, "mon:", 4) == 0) {
2401 default_monitor = 0;
2403 break;
2404 case QEMU_OPTION_debugcon:
2405 add_device_config(DEV_DEBUGCON, optarg);
2406 break;
2407 case QEMU_OPTION_loadvm:
2408 loadvm = optarg;
2409 break;
2410 case QEMU_OPTION_full_screen:
2411 full_screen = 1;
2412 break;
2413 #ifdef CONFIG_SDL
2414 case QEMU_OPTION_no_frame:
2415 no_frame = 1;
2416 break;
2417 case QEMU_OPTION_alt_grab:
2418 alt_grab = 1;
2419 break;
2420 case QEMU_OPTION_ctrl_grab:
2421 ctrl_grab = 1;
2422 break;
2423 case QEMU_OPTION_no_quit:
2424 no_quit = 1;
2425 break;
2426 case QEMU_OPTION_sdl:
2427 display_type = DT_SDL;
2428 break;
2429 #endif
2430 case QEMU_OPTION_pidfile:
2431 pid_file = optarg;
2432 break;
2433 case QEMU_OPTION_win2k_hack:
2434 win2k_install_hack = 1;
2435 break;
2436 case QEMU_OPTION_rtc_td_hack:
2437 rtc_td_hack = 1;
2438 break;
2439 case QEMU_OPTION_acpitable:
2440 do_acpitable_option(optarg);
2441 break;
2442 case QEMU_OPTION_smbios:
2443 do_smbios_option(optarg);
2444 break;
2445 case QEMU_OPTION_enable_kvm:
2446 kvm_allowed = 1;
2447 break;
2448 case QEMU_OPTION_usb:
2449 usb_enabled = 1;
2450 break;
2451 case QEMU_OPTION_usbdevice:
2452 usb_enabled = 1;
2453 add_device_config(DEV_USB, optarg);
2454 break;
2455 case QEMU_OPTION_device:
2456 if (!qemu_opts_parse(qemu_find_opts("device"), optarg, 1)) {
2457 exit(1);
2459 break;
2460 case QEMU_OPTION_smp:
2461 smp_parse(optarg);
2462 if (smp_cpus < 1) {
2463 fprintf(stderr, "Invalid number of CPUs\n");
2464 exit(1);
2466 if (max_cpus < smp_cpus) {
2467 fprintf(stderr, "maxcpus must be equal to or greater than "
2468 "smp\n");
2469 exit(1);
2471 if (max_cpus > 255) {
2472 fprintf(stderr, "Unsupported number of maxcpus\n");
2473 exit(1);
2475 break;
2476 case QEMU_OPTION_vnc:
2477 display_type = DT_VNC;
2478 vnc_display = optarg;
2479 break;
2480 case QEMU_OPTION_no_acpi:
2481 acpi_enabled = 0;
2482 break;
2483 case QEMU_OPTION_no_hpet:
2484 no_hpet = 1;
2485 break;
2486 case QEMU_OPTION_balloon:
2487 if (balloon_parse(optarg) < 0) {
2488 fprintf(stderr, "Unknown -balloon argument %s\n", optarg);
2489 exit(1);
2491 break;
2492 case QEMU_OPTION_no_reboot:
2493 no_reboot = 1;
2494 break;
2495 case QEMU_OPTION_no_shutdown:
2496 no_shutdown = 1;
2497 break;
2498 case QEMU_OPTION_show_cursor:
2499 cursor_hide = 0;
2500 break;
2501 case QEMU_OPTION_uuid:
2502 if(qemu_uuid_parse(optarg, qemu_uuid) < 0) {
2503 fprintf(stderr, "Fail to parse UUID string."
2504 " Wrong format.\n");
2505 exit(1);
2507 break;
2508 case QEMU_OPTION_option_rom:
2509 if (nb_option_roms >= MAX_OPTION_ROMS) {
2510 fprintf(stderr, "Too many option ROMs\n");
2511 exit(1);
2513 option_rom[nb_option_roms] = optarg;
2514 nb_option_roms++;
2515 break;
2516 case QEMU_OPTION_semihosting:
2517 semihosting_enabled = 1;
2518 break;
2519 case QEMU_OPTION_name:
2520 qemu_name = qemu_strdup(optarg);
2522 char *p = strchr(qemu_name, ',');
2523 if (p != NULL) {
2524 *p++ = 0;
2525 if (strncmp(p, "process=", 8)) {
2526 fprintf(stderr, "Unknown subargument %s to -name", p);
2527 exit(1);
2529 p += 8;
2530 os_set_proc_name(p);
2533 break;
2534 case QEMU_OPTION_prom_env:
2535 if (nb_prom_envs >= MAX_PROM_ENVS) {
2536 fprintf(stderr, "Too many prom variables\n");
2537 exit(1);
2539 prom_envs[nb_prom_envs] = optarg;
2540 nb_prom_envs++;
2541 break;
2542 case QEMU_OPTION_old_param:
2543 old_param = 1;
2544 break;
2545 case QEMU_OPTION_clock:
2546 configure_alarms(optarg);
2547 break;
2548 case QEMU_OPTION_startdate:
2549 configure_rtc_date_offset(optarg, 1);
2550 break;
2551 case QEMU_OPTION_rtc:
2552 opts = qemu_opts_parse(qemu_find_opts("rtc"), optarg, 0);
2553 if (!opts) {
2554 exit(1);
2556 configure_rtc(opts);
2557 break;
2558 case QEMU_OPTION_tb_size:
2559 tb_size = strtol(optarg, NULL, 0);
2560 if (tb_size < 0)
2561 tb_size = 0;
2562 break;
2563 case QEMU_OPTION_icount:
2564 icount_option = optarg;
2565 break;
2566 case QEMU_OPTION_incoming:
2567 incoming = optarg;
2568 incoming_expected = true;
2569 break;
2570 case QEMU_OPTION_nodefaults:
2571 default_serial = 0;
2572 default_parallel = 0;
2573 default_virtcon = 0;
2574 default_monitor = 0;
2575 default_vga = 0;
2576 default_net = 0;
2577 default_floppy = 0;
2578 default_cdrom = 0;
2579 default_sdcard = 0;
2580 break;
2581 case QEMU_OPTION_xen_domid:
2582 if (!(xen_available())) {
2583 printf("Option %s not supported for this target\n", popt->name);
2584 exit(1);
2586 xen_domid = atoi(optarg);
2587 break;
2588 case QEMU_OPTION_xen_create:
2589 if (!(xen_available())) {
2590 printf("Option %s not supported for this target\n", popt->name);
2591 exit(1);
2593 xen_mode = XEN_CREATE;
2594 break;
2595 case QEMU_OPTION_xen_attach:
2596 if (!(xen_available())) {
2597 printf("Option %s not supported for this target\n", popt->name);
2598 exit(1);
2600 xen_mode = XEN_ATTACH;
2601 break;
2602 #ifdef CONFIG_SIMPLE_TRACE
2603 case QEMU_OPTION_trace:
2604 opts = qemu_opts_parse(qemu_find_opts("trace"), optarg, 0);
2605 if (opts) {
2606 trace_file = qemu_opt_get(opts, "file");
2608 break;
2609 #endif
2610 case QEMU_OPTION_readconfig:
2612 int ret = qemu_read_config_file(optarg);
2613 if (ret < 0) {
2614 fprintf(stderr, "read config %s: %s\n", optarg,
2615 strerror(-ret));
2616 exit(1);
2618 break;
2620 case QEMU_OPTION_writeconfig:
2622 FILE *fp;
2623 if (strcmp(optarg, "-") == 0) {
2624 fp = stdout;
2625 } else {
2626 fp = fopen(optarg, "w");
2627 if (fp == NULL) {
2628 fprintf(stderr, "open %s: %s\n", optarg, strerror(errno));
2629 exit(1);
2632 qemu_config_write(fp);
2633 fclose(fp);
2634 break;
2636 default:
2637 os_parse_cmd_args(popt->index, optarg);
2641 loc_set_none();
2643 /* If no data_dir is specified then try to find it relative to the
2644 executable path. */
2645 if (!data_dir) {
2646 data_dir = os_find_datadir(argv[0]);
2648 /* If all else fails use the install patch specified when building. */
2649 if (!data_dir) {
2650 data_dir = CONFIG_QEMU_DATADIR;
2653 #ifdef CONFIG_SIMPLE_TRACE
2655 * Set the trace file name, if specified.
2657 st_set_trace_file(trace_file);
2658 #endif
2660 * Default to max_cpus = smp_cpus, in case the user doesn't
2661 * specify a max_cpus value.
2663 if (!max_cpus)
2664 max_cpus = smp_cpus;
2666 machine->max_cpus = machine->max_cpus ?: 1; /* Default to UP */
2667 if (smp_cpus > machine->max_cpus) {
2668 fprintf(stderr, "Number of SMP cpus requested (%d), exceeds max cpus "
2669 "supported by machine `%s' (%d)\n", smp_cpus, machine->name,
2670 machine->max_cpus);
2671 exit(1);
2674 qemu_opts_foreach(qemu_find_opts("device"), default_driver_check, NULL, 0);
2675 qemu_opts_foreach(qemu_find_opts("global"), default_driver_check, NULL, 0);
2677 if (machine->no_serial) {
2678 default_serial = 0;
2680 if (machine->no_parallel) {
2681 default_parallel = 0;
2683 if (!machine->use_virtcon) {
2684 default_virtcon = 0;
2686 if (machine->no_vga) {
2687 default_vga = 0;
2689 if (machine->no_floppy) {
2690 default_floppy = 0;
2692 if (machine->no_cdrom) {
2693 default_cdrom = 0;
2695 if (machine->no_sdcard) {
2696 default_sdcard = 0;
2699 if (display_type == DT_NOGRAPHIC) {
2700 if (default_parallel)
2701 add_device_config(DEV_PARALLEL, "null");
2702 if (default_serial && default_monitor) {
2703 add_device_config(DEV_SERIAL, "mon:stdio");
2704 } else if (default_virtcon && default_monitor) {
2705 add_device_config(DEV_VIRTCON, "mon:stdio");
2706 } else {
2707 if (default_serial)
2708 add_device_config(DEV_SERIAL, "stdio");
2709 if (default_virtcon)
2710 add_device_config(DEV_VIRTCON, "stdio");
2711 if (default_monitor)
2712 monitor_parse("stdio", "readline");
2714 } else {
2715 if (default_serial)
2716 add_device_config(DEV_SERIAL, "vc:80Cx24C");
2717 if (default_parallel)
2718 add_device_config(DEV_PARALLEL, "vc:80Cx24C");
2719 if (default_monitor)
2720 monitor_parse("vc:80Cx24C", "readline");
2721 if (default_virtcon)
2722 add_device_config(DEV_VIRTCON, "vc:80Cx24C");
2724 if (default_vga)
2725 vga_interface_type = VGA_CIRRUS;
2727 socket_init();
2729 if (qemu_opts_foreach(qemu_find_opts("chardev"), chardev_init_func, NULL, 1) != 0)
2730 exit(1);
2731 #ifdef CONFIG_VIRTFS
2732 if (qemu_opts_foreach(qemu_find_opts("fsdev"), fsdev_init_func, NULL, 1) != 0) {
2733 exit(1);
2735 #endif
2737 os_daemonize();
2739 if (pid_file && qemu_create_pidfile(pid_file) != 0) {
2740 os_pidfile_error();
2741 exit(1);
2744 if (kvm_allowed) {
2745 int ret = kvm_init(smp_cpus);
2746 if (ret < 0) {
2747 if (!kvm_available()) {
2748 printf("KVM not supported for this target\n");
2749 } else {
2750 fprintf(stderr, "failed to initialize KVM: %s\n", strerror(-ret));
2752 exit(1);
2756 if (qemu_init_main_loop()) {
2757 fprintf(stderr, "qemu_init_main_loop failed\n");
2758 exit(1);
2760 linux_boot = (kernel_filename != NULL);
2762 if (!linux_boot && *kernel_cmdline != '\0') {
2763 fprintf(stderr, "-append only allowed with -kernel option\n");
2764 exit(1);
2767 if (!linux_boot && initrd_filename != NULL) {
2768 fprintf(stderr, "-initrd only allowed with -kernel option\n");
2769 exit(1);
2772 os_set_line_buffering();
2774 if (init_timer_alarm() < 0) {
2775 fprintf(stderr, "could not initialize alarm timer\n");
2776 exit(1);
2778 configure_icount(icount_option);
2780 if (net_init_clients() < 0) {
2781 exit(1);
2784 /* init the bluetooth world */
2785 if (foreach_device_config(DEV_BT, bt_parse))
2786 exit(1);
2788 /* init the memory */
2789 if (ram_size == 0)
2790 ram_size = DEFAULT_RAM_SIZE * 1024 * 1024;
2792 /* init the dynamic translator */
2793 cpu_exec_init_all(tb_size * 1024 * 1024);
2795 bdrv_init_with_whitelist();
2797 blk_mig_init();
2799 if (default_cdrom) {
2800 /* we always create the cdrom drive, even if no disk is there */
2801 drive_add(NULL, CDROM_ALIAS);
2804 if (default_floppy) {
2805 /* we always create at least one floppy */
2806 drive_add(NULL, FD_ALIAS, 0);
2809 if (default_sdcard) {
2810 /* we always create one sd slot, even if no card is in it */
2811 drive_add(NULL, SD_ALIAS);
2814 /* open the virtual block devices */
2815 if (snapshot)
2816 qemu_opts_foreach(qemu_find_opts("drive"), drive_enable_snapshot, NULL, 0);
2817 if (qemu_opts_foreach(qemu_find_opts("drive"), drive_init_func, &machine->use_scsi, 1) != 0)
2818 exit(1);
2820 register_savevm_live(NULL, "ram", 0, 4, NULL, ram_save_live, NULL,
2821 ram_load, NULL);
2823 if (nb_numa_nodes > 0) {
2824 int i;
2826 if (nb_numa_nodes > smp_cpus) {
2827 nb_numa_nodes = smp_cpus;
2830 /* If no memory size if given for any node, assume the default case
2831 * and distribute the available memory equally across all nodes
2833 for (i = 0; i < nb_numa_nodes; i++) {
2834 if (node_mem[i] != 0)
2835 break;
2837 if (i == nb_numa_nodes) {
2838 uint64_t usedmem = 0;
2840 /* On Linux, the each node's border has to be 8MB aligned,
2841 * the final node gets the rest.
2843 for (i = 0; i < nb_numa_nodes - 1; i++) {
2844 node_mem[i] = (ram_size / nb_numa_nodes) & ~((1 << 23UL) - 1);
2845 usedmem += node_mem[i];
2847 node_mem[i] = ram_size - usedmem;
2850 for (i = 0; i < nb_numa_nodes; i++) {
2851 if (node_cpumask[i] != 0)
2852 break;
2854 /* assigning the VCPUs round-robin is easier to implement, guest OSes
2855 * must cope with this anyway, because there are BIOSes out there in
2856 * real machines which also use this scheme.
2858 if (i == nb_numa_nodes) {
2859 for (i = 0; i < smp_cpus; i++) {
2860 node_cpumask[i % nb_numa_nodes] |= 1 << i;
2865 if (qemu_opts_foreach(qemu_find_opts("mon"), mon_init_func, NULL, 1) != 0) {
2866 exit(1);
2869 if (foreach_device_config(DEV_SERIAL, serial_parse) < 0)
2870 exit(1);
2871 if (foreach_device_config(DEV_PARALLEL, parallel_parse) < 0)
2872 exit(1);
2873 if (foreach_device_config(DEV_VIRTCON, virtcon_parse) < 0)
2874 exit(1);
2875 if (foreach_device_config(DEV_DEBUGCON, debugcon_parse) < 0)
2876 exit(1);
2878 module_call_init(MODULE_INIT_DEVICE);
2880 if (qemu_opts_foreach(qemu_find_opts("device"), device_help_func, NULL, 0) != 0)
2881 exit(0);
2883 if (watchdog) {
2884 i = select_watchdog(watchdog);
2885 if (i > 0)
2886 exit (i == 1 ? 1 : 0);
2889 if (machine->compat_props) {
2890 qdev_prop_register_global_list(machine->compat_props);
2892 qemu_add_globals();
2894 machine->init(ram_size, boot_devices,
2895 kernel_filename, kernel_cmdline, initrd_filename, cpu_model);
2897 cpu_synchronize_all_post_init();
2899 /* must be after terminal init, SDL library changes signal handlers */
2900 os_setup_signal_handling();
2902 set_numa_modes();
2904 current_machine = machine;
2906 /* init USB devices */
2907 if (usb_enabled) {
2908 if (foreach_device_config(DEV_USB, usb_parse) < 0)
2909 exit(1);
2912 /* init generic devices */
2913 if (qemu_opts_foreach(qemu_find_opts("device"), device_init_func, NULL, 1) != 0)
2914 exit(1);
2916 net_check_clients();
2918 /* just use the first displaystate for the moment */
2919 ds = get_displaystate();
2921 if (display_type == DT_DEFAULT) {
2922 #if defined(CONFIG_SDL) || defined(CONFIG_COCOA)
2923 display_type = DT_SDL;
2924 #else
2925 display_type = DT_VNC;
2926 vnc_display = "localhost:0,to=99";
2927 show_vnc_port = 1;
2928 #endif
2932 switch (display_type) {
2933 case DT_NOGRAPHIC:
2934 break;
2935 #if defined(CONFIG_CURSES)
2936 case DT_CURSES:
2937 curses_display_init(ds, full_screen);
2938 break;
2939 #endif
2940 #if defined(CONFIG_SDL)
2941 case DT_SDL:
2942 sdl_display_init(ds, full_screen, no_frame);
2943 break;
2944 #elif defined(CONFIG_COCOA)
2945 case DT_SDL:
2946 cocoa_display_init(ds, full_screen);
2947 break;
2948 #endif
2949 case DT_VNC:
2950 vnc_display_init(ds);
2951 if (vnc_display_open(ds, vnc_display) < 0)
2952 exit(1);
2954 if (show_vnc_port) {
2955 printf("VNC server running on `%s'\n", vnc_display_local_addr(ds));
2957 break;
2958 default:
2959 break;
2961 dpy_resize(ds);
2963 dcl = ds->listeners;
2964 while (dcl != NULL) {
2965 if (dcl->dpy_refresh != NULL) {
2966 ds->gui_timer = qemu_new_timer(rt_clock, gui_update, ds);
2967 qemu_mod_timer(ds->gui_timer, qemu_get_clock(rt_clock));
2968 break;
2970 dcl = dcl->next;
2973 if (display_type == DT_NOGRAPHIC || display_type == DT_VNC) {
2974 nographic_timer = qemu_new_timer(rt_clock, nographic_update, NULL);
2975 qemu_mod_timer(nographic_timer, qemu_get_clock(rt_clock));
2978 text_consoles_set_display(ds);
2980 if (gdbstub_dev && gdbserver_start(gdbstub_dev) < 0) {
2981 fprintf(stderr, "qemu: could not open gdbserver on device '%s'\n",
2982 gdbstub_dev);
2983 exit(1);
2986 qdev_machine_creation_done();
2988 if (rom_load_all() != 0) {
2989 fprintf(stderr, "rom loading failed\n");
2990 exit(1);
2993 qemu_system_reset();
2994 if (loadvm) {
2995 if (load_vmstate(loadvm) < 0) {
2996 autostart = 0;
3000 if (incoming) {
3001 int ret = qemu_start_incoming_migration(incoming);
3002 if (ret < 0) {
3003 fprintf(stderr, "Migration failed. Exit code %s(%d), exiting.\n",
3004 incoming, ret);
3005 exit(ret);
3007 } else if (autostart) {
3008 vm_start();
3011 os_setup_post();
3013 main_loop();
3014 quit_timers();
3015 net_cleanup();
3017 return 0;