qemu: e1000 fix TOR math
[qemu.git] / vl.c
blobfd491bafa6c0685c6d3c3a7b52b6a7d877d16fe6
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_BSD
51 #include <sys/stat.h>
52 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__DragonFly__)
53 #include <libutil.h>
54 #include <sys/sysctl.h>
55 #else
56 #include <util.h>
57 #endif
58 #else
59 #ifdef __linux__
60 #include <pty.h>
61 #include <malloc.h>
63 #include <linux/ppdev.h>
64 #include <linux/parport.h>
65 #endif
66 #ifdef __sun__
67 #include <sys/stat.h>
68 #include <sys/ethernet.h>
69 #include <sys/sockio.h>
70 #include <netinet/arp.h>
71 #include <netinet/in_systm.h>
72 #include <netinet/ip.h>
73 #include <netinet/ip_icmp.h> // must come after ip.h
74 #include <netinet/udp.h>
75 #include <netinet/tcp.h>
76 #include <net/if.h>
77 #include <syslog.h>
78 #include <stropts.h>
79 /* See MySQL bug #7156 (http://bugs.mysql.com/bug.php?id=7156) for
80 discussion about Solaris header problems */
81 extern int madvise(caddr_t, size_t, int);
82 #endif
83 #endif
84 #endif
86 #if defined(__OpenBSD__)
87 #include <util.h>
88 #endif
90 #if defined(CONFIG_VDE)
91 #include <libvdeplug.h>
92 #endif
94 #ifdef _WIN32
95 #include <windows.h>
96 #endif
98 #ifdef CONFIG_SDL
99 #if defined(__APPLE__) || defined(main)
100 #include <SDL.h>
101 int qemu_main(int argc, char **argv, char **envp);
102 int main(int argc, char **argv)
104 return qemu_main(argc, argv, NULL);
106 #undef main
107 #define main qemu_main
108 #endif
109 #endif /* CONFIG_SDL */
111 #ifdef CONFIG_COCOA
112 #undef main
113 #define main qemu_main
114 #endif /* CONFIG_COCOA */
116 #include "hw/hw.h"
117 #include "hw/boards.h"
118 #include "hw/usb.h"
119 #include "hw/pcmcia.h"
120 #include "hw/pc.h"
121 #include "hw/isa.h"
122 #include "hw/baum.h"
123 #include "hw/bt.h"
124 #include "hw/watchdog.h"
125 #include "hw/smbios.h"
126 #include "hw/xen.h"
127 #include "hw/qdev.h"
128 #include "hw/loader.h"
129 #include "bt-host.h"
130 #include "net.h"
131 #include "net/slirp.h"
132 #include "monitor.h"
133 #include "console.h"
134 #include "sysemu.h"
135 #include "gdbstub.h"
136 #include "qemu-timer.h"
137 #include "qemu-char.h"
138 #include "cache-utils.h"
139 #include "block.h"
140 #include "blockdev.h"
141 #include "block-migration.h"
142 #include "dma.h"
143 #include "audio/audio.h"
144 #include "migration.h"
145 #include "kvm.h"
146 #include "qemu-option.h"
147 #include "qemu-config.h"
148 #include "qemu-objects.h"
149 #include "qemu-options.h"
150 #ifdef CONFIG_VIRTFS
151 #include "fsdev/qemu-fsdev.h"
152 #endif
154 #include "disas.h"
156 #include "qemu_socket.h"
158 #include "slirp/libslirp.h"
160 #include "qemu-queue.h"
161 #include "cpus.h"
162 #include "arch_init.h"
164 //#define DEBUG_NET
165 //#define DEBUG_SLIRP
167 #define DEFAULT_RAM_SIZE 128
169 #define MAX_VIRTIO_CONSOLES 1
171 static const char *data_dir;
172 const char *bios_name = NULL;
173 enum vga_retrace_method vga_retrace_method = VGA_RETRACE_DUMB;
174 DisplayType display_type = DT_DEFAULT;
175 const char* keyboard_layout = NULL;
176 ram_addr_t ram_size;
177 const char *mem_path = NULL;
178 #ifdef MAP_POPULATE
179 int mem_prealloc = 0; /* force preallocation of physical target memory */
180 #endif
181 int nb_nics;
182 NICInfo nd_table[MAX_NICS];
183 int vm_running;
184 int autostart;
185 int incoming_expected; /* Started with -incoming and waiting for incoming */
186 static int rtc_utc = 1;
187 static int rtc_date_offset = -1; /* -1 means no change */
188 QEMUClock *rtc_clock;
189 int vga_interface_type = VGA_NONE;
190 static int full_screen = 0;
191 #ifdef CONFIG_SDL
192 static int no_frame = 0;
193 #endif
194 int no_quit = 0;
195 CharDriverState *serial_hds[MAX_SERIAL_PORTS];
196 CharDriverState *parallel_hds[MAX_PARALLEL_PORTS];
197 CharDriverState *virtcon_hds[MAX_VIRTIO_CONSOLES];
198 int win2k_install_hack = 0;
199 int rtc_td_hack = 0;
200 int usb_enabled = 0;
201 int singlestep = 0;
202 int smp_cpus = 1;
203 int max_cpus = 0;
204 int smp_cores = 1;
205 int smp_threads = 1;
206 const char *vnc_display;
207 int acpi_enabled = 1;
208 int no_hpet = 0;
209 int fd_bootchk = 1;
210 int no_reboot = 0;
211 int no_shutdown = 0;
212 int cursor_hide = 1;
213 int graphic_rotate = 0;
214 uint8_t irq0override = 1;
215 const char *watchdog;
216 const char *option_rom[MAX_OPTION_ROMS];
217 int nb_option_roms;
218 int semihosting_enabled = 0;
219 int old_param = 0;
220 const char *qemu_name;
221 int alt_grab = 0;
222 int ctrl_grab = 0;
223 unsigned int nb_prom_envs = 0;
224 const char *prom_envs[MAX_PROM_ENVS];
225 int boot_menu;
227 int nb_numa_nodes;
228 uint64_t node_mem[MAX_NODES];
229 uint64_t node_cpumask[MAX_NODES];
231 static QEMUTimer *nographic_timer;
233 uint8_t qemu_uuid[16];
235 static QEMUBootSetHandler *boot_set_handler;
236 static void *boot_set_opaque;
238 static NotifierList exit_notifiers =
239 NOTIFIER_LIST_INITIALIZER(exit_notifiers);
241 int kvm_allowed = 0;
242 uint32_t xen_domid;
243 enum xen_mode xen_mode = XEN_EMULATE;
245 static int default_serial = 1;
246 static int default_parallel = 1;
247 static int default_virtcon = 1;
248 static int default_monitor = 1;
249 static int default_vga = 1;
250 static int default_floppy = 1;
251 static int default_cdrom = 1;
252 static int default_sdcard = 1;
254 static struct {
255 const char *driver;
256 int *flag;
257 } default_list[] = {
258 { .driver = "isa-serial", .flag = &default_serial },
259 { .driver = "isa-parallel", .flag = &default_parallel },
260 { .driver = "isa-fdc", .flag = &default_floppy },
261 { .driver = "ide-drive", .flag = &default_cdrom },
262 { .driver = "virtio-serial-pci", .flag = &default_virtcon },
263 { .driver = "virtio-serial-s390", .flag = &default_virtcon },
264 { .driver = "virtio-serial", .flag = &default_virtcon },
265 { .driver = "VGA", .flag = &default_vga },
266 { .driver = "cirrus-vga", .flag = &default_vga },
267 { .driver = "vmware-svga", .flag = &default_vga },
270 static int default_driver_check(QemuOpts *opts, void *opaque)
272 const char *driver = qemu_opt_get(opts, "driver");
273 int i;
275 if (!driver)
276 return 0;
277 for (i = 0; i < ARRAY_SIZE(default_list); i++) {
278 if (strcmp(default_list[i].driver, driver) != 0)
279 continue;
280 *(default_list[i].flag) = 0;
282 return 0;
285 /***********************************************************/
286 /* real time host monotonic timer */
288 /* compute with 96 bit intermediate result: (a*b)/c */
289 uint64_t muldiv64(uint64_t a, uint32_t b, uint32_t c)
291 union {
292 uint64_t ll;
293 struct {
294 #ifdef HOST_WORDS_BIGENDIAN
295 uint32_t high, low;
296 #else
297 uint32_t low, high;
298 #endif
299 } l;
300 } u, res;
301 uint64_t rl, rh;
303 u.ll = a;
304 rl = (uint64_t)u.l.low * (uint64_t)b;
305 rh = (uint64_t)u.l.high * (uint64_t)b;
306 rh += (rl >> 32);
307 res.l.high = rh / c;
308 res.l.low = (((rh % c) << 32) + (rl & 0xffffffff)) / c;
309 return res.ll;
312 /***********************************************************/
313 /* host time/date access */
314 void qemu_get_timedate(struct tm *tm, int offset)
316 time_t ti;
317 struct tm *ret;
319 time(&ti);
320 ti += offset;
321 if (rtc_date_offset == -1) {
322 if (rtc_utc)
323 ret = gmtime(&ti);
324 else
325 ret = localtime(&ti);
326 } else {
327 ti -= rtc_date_offset;
328 ret = gmtime(&ti);
331 memcpy(tm, ret, sizeof(struct tm));
334 int qemu_timedate_diff(struct tm *tm)
336 time_t seconds;
338 if (rtc_date_offset == -1)
339 if (rtc_utc)
340 seconds = mktimegm(tm);
341 else
342 seconds = mktime(tm);
343 else
344 seconds = mktimegm(tm) + rtc_date_offset;
346 return seconds - time(NULL);
349 void rtc_change_mon_event(struct tm *tm)
351 QObject *data;
353 data = qobject_from_jsonf("{ 'offset': %d }", qemu_timedate_diff(tm));
354 monitor_protocol_event(QEVENT_RTC_CHANGE, data);
355 qobject_decref(data);
358 static void configure_rtc_date_offset(const char *startdate, int legacy)
360 time_t rtc_start_date;
361 struct tm tm;
363 if (!strcmp(startdate, "now") && legacy) {
364 rtc_date_offset = -1;
365 } else {
366 if (sscanf(startdate, "%d-%d-%dT%d:%d:%d",
367 &tm.tm_year,
368 &tm.tm_mon,
369 &tm.tm_mday,
370 &tm.tm_hour,
371 &tm.tm_min,
372 &tm.tm_sec) == 6) {
373 /* OK */
374 } else if (sscanf(startdate, "%d-%d-%d",
375 &tm.tm_year,
376 &tm.tm_mon,
377 &tm.tm_mday) == 3) {
378 tm.tm_hour = 0;
379 tm.tm_min = 0;
380 tm.tm_sec = 0;
381 } else {
382 goto date_fail;
384 tm.tm_year -= 1900;
385 tm.tm_mon--;
386 rtc_start_date = mktimegm(&tm);
387 if (rtc_start_date == -1) {
388 date_fail:
389 fprintf(stderr, "Invalid date format. Valid formats are:\n"
390 "'2006-06-17T16:01:21' or '2006-06-17'\n");
391 exit(1);
393 rtc_date_offset = time(NULL) - rtc_start_date;
397 static void configure_rtc(QemuOpts *opts)
399 const char *value;
401 value = qemu_opt_get(opts, "base");
402 if (value) {
403 if (!strcmp(value, "utc")) {
404 rtc_utc = 1;
405 } else if (!strcmp(value, "localtime")) {
406 rtc_utc = 0;
407 } else {
408 configure_rtc_date_offset(value, 0);
411 value = qemu_opt_get(opts, "clock");
412 if (value) {
413 if (!strcmp(value, "host")) {
414 rtc_clock = host_clock;
415 } else if (!strcmp(value, "vm")) {
416 rtc_clock = vm_clock;
417 } else {
418 fprintf(stderr, "qemu: invalid option value '%s'\n", value);
419 exit(1);
422 value = qemu_opt_get(opts, "driftfix");
423 if (value) {
424 if (!strcmp(value, "slew")) {
425 rtc_td_hack = 1;
426 } else if (!strcmp(value, "none")) {
427 rtc_td_hack = 0;
428 } else {
429 fprintf(stderr, "qemu: invalid option value '%s'\n", value);
430 exit(1);
435 /***********************************************************/
436 /* Bluetooth support */
437 static int nb_hcis;
438 static int cur_hci;
439 static struct HCIInfo *hci_table[MAX_NICS];
441 static struct bt_vlan_s {
442 struct bt_scatternet_s net;
443 int id;
444 struct bt_vlan_s *next;
445 } *first_bt_vlan;
447 /* find or alloc a new bluetooth "VLAN" */
448 static struct bt_scatternet_s *qemu_find_bt_vlan(int id)
450 struct bt_vlan_s **pvlan, *vlan;
451 for (vlan = first_bt_vlan; vlan != NULL; vlan = vlan->next) {
452 if (vlan->id == id)
453 return &vlan->net;
455 vlan = qemu_mallocz(sizeof(struct bt_vlan_s));
456 vlan->id = id;
457 pvlan = &first_bt_vlan;
458 while (*pvlan != NULL)
459 pvlan = &(*pvlan)->next;
460 *pvlan = vlan;
461 return &vlan->net;
464 static void null_hci_send(struct HCIInfo *hci, const uint8_t *data, int len)
468 static int null_hci_addr_set(struct HCIInfo *hci, const uint8_t *bd_addr)
470 return -ENOTSUP;
473 static struct HCIInfo null_hci = {
474 .cmd_send = null_hci_send,
475 .sco_send = null_hci_send,
476 .acl_send = null_hci_send,
477 .bdaddr_set = null_hci_addr_set,
480 struct HCIInfo *qemu_next_hci(void)
482 if (cur_hci == nb_hcis)
483 return &null_hci;
485 return hci_table[cur_hci++];
488 static struct HCIInfo *hci_init(const char *str)
490 char *endp;
491 struct bt_scatternet_s *vlan = 0;
493 if (!strcmp(str, "null"))
494 /* null */
495 return &null_hci;
496 else if (!strncmp(str, "host", 4) && (str[4] == '\0' || str[4] == ':'))
497 /* host[:hciN] */
498 return bt_host_hci(str[4] ? str + 5 : "hci0");
499 else if (!strncmp(str, "hci", 3)) {
500 /* hci[,vlan=n] */
501 if (str[3]) {
502 if (!strncmp(str + 3, ",vlan=", 6)) {
503 vlan = qemu_find_bt_vlan(strtol(str + 9, &endp, 0));
504 if (*endp)
505 vlan = 0;
507 } else
508 vlan = qemu_find_bt_vlan(0);
509 if (vlan)
510 return bt_new_hci(vlan);
513 fprintf(stderr, "qemu: Unknown bluetooth HCI `%s'.\n", str);
515 return 0;
518 static int bt_hci_parse(const char *str)
520 struct HCIInfo *hci;
521 bdaddr_t bdaddr;
523 if (nb_hcis >= MAX_NICS) {
524 fprintf(stderr, "qemu: Too many bluetooth HCIs (max %i).\n", MAX_NICS);
525 return -1;
528 hci = hci_init(str);
529 if (!hci)
530 return -1;
532 bdaddr.b[0] = 0x52;
533 bdaddr.b[1] = 0x54;
534 bdaddr.b[2] = 0x00;
535 bdaddr.b[3] = 0x12;
536 bdaddr.b[4] = 0x34;
537 bdaddr.b[5] = 0x56 + nb_hcis;
538 hci->bdaddr_set(hci, bdaddr.b);
540 hci_table[nb_hcis++] = hci;
542 return 0;
545 static void bt_vhci_add(int vlan_id)
547 struct bt_scatternet_s *vlan = qemu_find_bt_vlan(vlan_id);
549 if (!vlan->slave)
550 fprintf(stderr, "qemu: warning: adding a VHCI to "
551 "an empty scatternet %i\n", vlan_id);
553 bt_vhci_init(bt_new_hci(vlan));
556 static struct bt_device_s *bt_device_add(const char *opt)
558 struct bt_scatternet_s *vlan;
559 int vlan_id = 0;
560 char *endp = strstr(opt, ",vlan=");
561 int len = (endp ? endp - opt : strlen(opt)) + 1;
562 char devname[10];
564 pstrcpy(devname, MIN(sizeof(devname), len), opt);
566 if (endp) {
567 vlan_id = strtol(endp + 6, &endp, 0);
568 if (*endp) {
569 fprintf(stderr, "qemu: unrecognised bluetooth vlan Id\n");
570 return 0;
574 vlan = qemu_find_bt_vlan(vlan_id);
576 if (!vlan->slave)
577 fprintf(stderr, "qemu: warning: adding a slave device to "
578 "an empty scatternet %i\n", vlan_id);
580 if (!strcmp(devname, "keyboard"))
581 return bt_keyboard_init(vlan);
583 fprintf(stderr, "qemu: unsupported bluetooth device `%s'\n", devname);
584 return 0;
587 static int bt_parse(const char *opt)
589 const char *endp, *p;
590 int vlan;
592 if (strstart(opt, "hci", &endp)) {
593 if (!*endp || *endp == ',') {
594 if (*endp)
595 if (!strstart(endp, ",vlan=", 0))
596 opt = endp + 1;
598 return bt_hci_parse(opt);
600 } else if (strstart(opt, "vhci", &endp)) {
601 if (!*endp || *endp == ',') {
602 if (*endp) {
603 if (strstart(endp, ",vlan=", &p)) {
604 vlan = strtol(p, (char **) &endp, 0);
605 if (*endp) {
606 fprintf(stderr, "qemu: bad scatternet '%s'\n", p);
607 return 1;
609 } else {
610 fprintf(stderr, "qemu: bad parameter '%s'\n", endp + 1);
611 return 1;
613 } else
614 vlan = 0;
616 bt_vhci_add(vlan);
617 return 0;
619 } else if (strstart(opt, "device:", &endp))
620 return !bt_device_add(endp);
622 fprintf(stderr, "qemu: bad bluetooth parameter '%s'\n", opt);
623 return 1;
626 /***********************************************************/
627 /* QEMU Block devices */
629 #define HD_ALIAS "index=%d,media=disk"
630 #define CDROM_ALIAS "index=2,media=cdrom"
631 #define FD_ALIAS "index=%d,if=floppy"
632 #define PFLASH_ALIAS "if=pflash"
633 #define MTD_ALIAS "if=mtd"
634 #define SD_ALIAS "index=0,if=sd"
636 static int drive_init_func(QemuOpts *opts, void *opaque)
638 int *use_scsi = opaque;
639 int fatal_error = 0;
641 if (drive_init(opts, *use_scsi, &fatal_error) == NULL) {
642 if (fatal_error)
643 return 1;
645 return 0;
648 static int drive_enable_snapshot(QemuOpts *opts, void *opaque)
650 if (NULL == qemu_opt_get(opts, "snapshot")) {
651 qemu_opt_set(opts, "snapshot", "on");
653 return 0;
656 void qemu_register_boot_set(QEMUBootSetHandler *func, void *opaque)
658 boot_set_handler = func;
659 boot_set_opaque = opaque;
662 int qemu_boot_set(const char *boot_devices)
664 if (!boot_set_handler) {
665 return -EINVAL;
667 return boot_set_handler(boot_set_opaque, boot_devices);
670 static void validate_bootdevices(char *devices)
672 /* We just do some generic consistency checks */
673 const char *p;
674 int bitmap = 0;
676 for (p = devices; *p != '\0'; p++) {
677 /* Allowed boot devices are:
678 * a-b: floppy disk drives
679 * c-f: IDE disk drives
680 * g-m: machine implementation dependant drives
681 * n-p: network devices
682 * It's up to each machine implementation to check if the given boot
683 * devices match the actual hardware implementation and firmware
684 * features.
686 if (*p < 'a' || *p > 'p') {
687 fprintf(stderr, "Invalid boot device '%c'\n", *p);
688 exit(1);
690 if (bitmap & (1 << (*p - 'a'))) {
691 fprintf(stderr, "Boot device '%c' was given twice\n", *p);
692 exit(1);
694 bitmap |= 1 << (*p - 'a');
698 static void restore_boot_devices(void *opaque)
700 char *standard_boot_devices = opaque;
701 static int first = 1;
703 /* Restore boot order and remove ourselves after the first boot */
704 if (first) {
705 first = 0;
706 return;
709 qemu_boot_set(standard_boot_devices);
711 qemu_unregister_reset(restore_boot_devices, standard_boot_devices);
712 qemu_free(standard_boot_devices);
715 static void numa_add(const char *optarg)
717 char option[128];
718 char *endptr;
719 unsigned long long value, endvalue;
720 int nodenr;
722 optarg = get_opt_name(option, 128, optarg, ',') + 1;
723 if (!strcmp(option, "node")) {
724 if (get_param_value(option, 128, "nodeid", optarg) == 0) {
725 nodenr = nb_numa_nodes;
726 } else {
727 nodenr = strtoull(option, NULL, 10);
730 if (get_param_value(option, 128, "mem", optarg) == 0) {
731 node_mem[nodenr] = 0;
732 } else {
733 value = strtoull(option, &endptr, 0);
734 switch (*endptr) {
735 case 0: case 'M': case 'm':
736 value <<= 20;
737 break;
738 case 'G': case 'g':
739 value <<= 30;
740 break;
742 node_mem[nodenr] = value;
744 if (get_param_value(option, 128, "cpus", optarg) == 0) {
745 node_cpumask[nodenr] = 0;
746 } else {
747 value = strtoull(option, &endptr, 10);
748 if (value >= 64) {
749 value = 63;
750 fprintf(stderr, "only 64 CPUs in NUMA mode supported.\n");
751 } else {
752 if (*endptr == '-') {
753 endvalue = strtoull(endptr+1, &endptr, 10);
754 if (endvalue >= 63) {
755 endvalue = 62;
756 fprintf(stderr,
757 "only 63 CPUs in NUMA mode supported.\n");
759 value = (2ULL << endvalue) - (1ULL << value);
760 } else {
761 value = 1ULL << value;
764 node_cpumask[nodenr] = value;
766 nb_numa_nodes++;
768 return;
771 static void smp_parse(const char *optarg)
773 int smp, sockets = 0, threads = 0, cores = 0;
774 char *endptr;
775 char option[128];
777 smp = strtoul(optarg, &endptr, 10);
778 if (endptr != optarg) {
779 if (*endptr == ',') {
780 endptr++;
783 if (get_param_value(option, 128, "sockets", endptr) != 0)
784 sockets = strtoull(option, NULL, 10);
785 if (get_param_value(option, 128, "cores", endptr) != 0)
786 cores = strtoull(option, NULL, 10);
787 if (get_param_value(option, 128, "threads", endptr) != 0)
788 threads = strtoull(option, NULL, 10);
789 if (get_param_value(option, 128, "maxcpus", endptr) != 0)
790 max_cpus = strtoull(option, NULL, 10);
792 /* compute missing values, prefer sockets over cores over threads */
793 if (smp == 0 || sockets == 0) {
794 sockets = sockets > 0 ? sockets : 1;
795 cores = cores > 0 ? cores : 1;
796 threads = threads > 0 ? threads : 1;
797 if (smp == 0) {
798 smp = cores * threads * sockets;
800 } else {
801 if (cores == 0) {
802 threads = threads > 0 ? threads : 1;
803 cores = smp / (sockets * threads);
804 } else {
805 threads = smp / (cores * sockets);
808 smp_cpus = smp;
809 smp_cores = cores > 0 ? cores : 1;
810 smp_threads = threads > 0 ? threads : 1;
811 if (max_cpus == 0)
812 max_cpus = smp_cpus;
815 /***********************************************************/
816 /* USB devices */
818 static int usb_device_add(const char *devname)
820 const char *p;
821 USBDevice *dev = NULL;
823 if (!usb_enabled)
824 return -1;
826 /* drivers with .usbdevice_name entry in USBDeviceInfo */
827 dev = usbdevice_create(devname);
828 if (dev)
829 goto done;
831 /* the other ones */
832 if (strstart(devname, "host:", &p)) {
833 dev = usb_host_device_open(p);
834 } else if (!strcmp(devname, "bt") || strstart(devname, "bt:", &p)) {
835 dev = usb_bt_init(devname[2] ? hci_init(p) :
836 bt_new_hci(qemu_find_bt_vlan(0)));
837 } else {
838 return -1;
840 if (!dev)
841 return -1;
843 done:
844 return 0;
847 static int usb_device_del(const char *devname)
849 int bus_num, addr;
850 const char *p;
852 if (strstart(devname, "host:", &p))
853 return usb_host_device_close(p);
855 if (!usb_enabled)
856 return -1;
858 p = strchr(devname, '.');
859 if (!p)
860 return -1;
861 bus_num = strtoul(devname, NULL, 0);
862 addr = strtoul(p + 1, NULL, 0);
864 return usb_device_delete_addr(bus_num, addr);
867 static int usb_parse(const char *cmdline)
869 int r;
870 r = usb_device_add(cmdline);
871 if (r < 0) {
872 fprintf(stderr, "qemu: could not add USB device '%s'\n", cmdline);
874 return r;
877 void do_usb_add(Monitor *mon, const QDict *qdict)
879 const char *devname = qdict_get_str(qdict, "devname");
880 if (usb_device_add(devname) < 0) {
881 error_report("could not add USB device '%s'", devname);
885 void do_usb_del(Monitor *mon, const QDict *qdict)
887 const char *devname = qdict_get_str(qdict, "devname");
888 if (usb_device_del(devname) < 0) {
889 error_report("could not delete USB device '%s'", devname);
893 /***********************************************************/
894 /* PCMCIA/Cardbus */
896 static struct pcmcia_socket_entry_s {
897 PCMCIASocket *socket;
898 struct pcmcia_socket_entry_s *next;
899 } *pcmcia_sockets = 0;
901 void pcmcia_socket_register(PCMCIASocket *socket)
903 struct pcmcia_socket_entry_s *entry;
905 entry = qemu_malloc(sizeof(struct pcmcia_socket_entry_s));
906 entry->socket = socket;
907 entry->next = pcmcia_sockets;
908 pcmcia_sockets = entry;
911 void pcmcia_socket_unregister(PCMCIASocket *socket)
913 struct pcmcia_socket_entry_s *entry, **ptr;
915 ptr = &pcmcia_sockets;
916 for (entry = *ptr; entry; ptr = &entry->next, entry = *ptr)
917 if (entry->socket == socket) {
918 *ptr = entry->next;
919 qemu_free(entry);
923 void pcmcia_info(Monitor *mon)
925 struct pcmcia_socket_entry_s *iter;
927 if (!pcmcia_sockets)
928 monitor_printf(mon, "No PCMCIA sockets\n");
930 for (iter = pcmcia_sockets; iter; iter = iter->next)
931 monitor_printf(mon, "%s: %s\n", iter->socket->slot_string,
932 iter->socket->attached ? iter->socket->card_string :
933 "Empty");
936 /***********************************************************/
937 /* I/O handling */
939 typedef struct IOHandlerRecord {
940 int fd;
941 IOCanReadHandler *fd_read_poll;
942 IOHandler *fd_read;
943 IOHandler *fd_write;
944 int deleted;
945 void *opaque;
946 /* temporary data */
947 struct pollfd *ufd;
948 QLIST_ENTRY(IOHandlerRecord) next;
949 } IOHandlerRecord;
951 static QLIST_HEAD(, IOHandlerRecord) io_handlers =
952 QLIST_HEAD_INITIALIZER(io_handlers);
955 /* XXX: fd_read_poll should be suppressed, but an API change is
956 necessary in the character devices to suppress fd_can_read(). */
957 int qemu_set_fd_handler2(int fd,
958 IOCanReadHandler *fd_read_poll,
959 IOHandler *fd_read,
960 IOHandler *fd_write,
961 void *opaque)
963 IOHandlerRecord *ioh;
965 if (!fd_read && !fd_write) {
966 QLIST_FOREACH(ioh, &io_handlers, next) {
967 if (ioh->fd == fd) {
968 ioh->deleted = 1;
969 break;
972 } else {
973 QLIST_FOREACH(ioh, &io_handlers, next) {
974 if (ioh->fd == fd)
975 goto found;
977 ioh = qemu_mallocz(sizeof(IOHandlerRecord));
978 QLIST_INSERT_HEAD(&io_handlers, ioh, next);
979 found:
980 ioh->fd = fd;
981 ioh->fd_read_poll = fd_read_poll;
982 ioh->fd_read = fd_read;
983 ioh->fd_write = fd_write;
984 ioh->opaque = opaque;
985 ioh->deleted = 0;
987 return 0;
990 int qemu_set_fd_handler(int fd,
991 IOHandler *fd_read,
992 IOHandler *fd_write,
993 void *opaque)
995 return qemu_set_fd_handler2(fd, NULL, fd_read, fd_write, opaque);
998 /***********************************************************/
999 /* machine registration */
1001 static QEMUMachine *first_machine = NULL;
1002 QEMUMachine *current_machine = NULL;
1004 int qemu_register_machine(QEMUMachine *m)
1006 QEMUMachine **pm;
1007 pm = &first_machine;
1008 while (*pm != NULL)
1009 pm = &(*pm)->next;
1010 m->next = NULL;
1011 *pm = m;
1012 return 0;
1015 static QEMUMachine *find_machine(const char *name)
1017 QEMUMachine *m;
1019 for(m = first_machine; m != NULL; m = m->next) {
1020 if (!strcmp(m->name, name))
1021 return m;
1022 if (m->alias && !strcmp(m->alias, name))
1023 return m;
1025 return NULL;
1028 static QEMUMachine *find_default_machine(void)
1030 QEMUMachine *m;
1032 for(m = first_machine; m != NULL; m = m->next) {
1033 if (m->is_default) {
1034 return m;
1037 return NULL;
1040 /***********************************************************/
1041 /* main execution loop */
1043 static void gui_update(void *opaque)
1045 uint64_t interval = GUI_REFRESH_INTERVAL;
1046 DisplayState *ds = opaque;
1047 DisplayChangeListener *dcl = ds->listeners;
1049 qemu_flush_coalesced_mmio_buffer();
1050 dpy_refresh(ds);
1052 while (dcl != NULL) {
1053 if (dcl->gui_timer_interval &&
1054 dcl->gui_timer_interval < interval)
1055 interval = dcl->gui_timer_interval;
1056 dcl = dcl->next;
1058 qemu_mod_timer(ds->gui_timer, interval + qemu_get_clock(rt_clock));
1061 static void nographic_update(void *opaque)
1063 uint64_t interval = GUI_REFRESH_INTERVAL;
1065 qemu_flush_coalesced_mmio_buffer();
1066 qemu_mod_timer(nographic_timer, interval + qemu_get_clock(rt_clock));
1069 struct vm_change_state_entry {
1070 VMChangeStateHandler *cb;
1071 void *opaque;
1072 QLIST_ENTRY (vm_change_state_entry) entries;
1075 static QLIST_HEAD(vm_change_state_head, vm_change_state_entry) vm_change_state_head;
1077 VMChangeStateEntry *qemu_add_vm_change_state_handler(VMChangeStateHandler *cb,
1078 void *opaque)
1080 VMChangeStateEntry *e;
1082 e = qemu_mallocz(sizeof (*e));
1084 e->cb = cb;
1085 e->opaque = opaque;
1086 QLIST_INSERT_HEAD(&vm_change_state_head, e, entries);
1087 return e;
1090 void qemu_del_vm_change_state_handler(VMChangeStateEntry *e)
1092 QLIST_REMOVE (e, entries);
1093 qemu_free (e);
1096 void vm_state_notify(int running, int reason)
1098 VMChangeStateEntry *e;
1100 for (e = vm_change_state_head.lh_first; e; e = e->entries.le_next) {
1101 e->cb(e->opaque, running, reason);
1105 void vm_start(void)
1107 if (!vm_running) {
1108 cpu_enable_ticks();
1109 vm_running = 1;
1110 vm_state_notify(1, 0);
1111 resume_all_vcpus();
1112 monitor_protocol_event(QEVENT_RESUME, NULL);
1116 /* reset/shutdown handler */
1118 typedef struct QEMUResetEntry {
1119 QTAILQ_ENTRY(QEMUResetEntry) entry;
1120 QEMUResetHandler *func;
1121 void *opaque;
1122 } QEMUResetEntry;
1124 static QTAILQ_HEAD(reset_handlers, QEMUResetEntry) reset_handlers =
1125 QTAILQ_HEAD_INITIALIZER(reset_handlers);
1126 static int reset_requested;
1127 static int shutdown_requested;
1128 static int powerdown_requested;
1129 int debug_requested;
1130 int vmstop_requested;
1132 int qemu_shutdown_requested(void)
1134 int r = shutdown_requested;
1135 shutdown_requested = 0;
1136 return r;
1139 int qemu_reset_requested(void)
1141 int r = reset_requested;
1142 reset_requested = 0;
1143 return r;
1146 int qemu_powerdown_requested(void)
1148 int r = powerdown_requested;
1149 powerdown_requested = 0;
1150 return r;
1153 static int qemu_debug_requested(void)
1155 int r = debug_requested;
1156 debug_requested = 0;
1157 return r;
1160 static int qemu_vmstop_requested(void)
1162 int r = vmstop_requested;
1163 vmstop_requested = 0;
1164 return r;
1167 void qemu_register_reset(QEMUResetHandler *func, void *opaque)
1169 QEMUResetEntry *re = qemu_mallocz(sizeof(QEMUResetEntry));
1171 re->func = func;
1172 re->opaque = opaque;
1173 QTAILQ_INSERT_TAIL(&reset_handlers, re, entry);
1176 void qemu_unregister_reset(QEMUResetHandler *func, void *opaque)
1178 QEMUResetEntry *re;
1180 QTAILQ_FOREACH(re, &reset_handlers, entry) {
1181 if (re->func == func && re->opaque == opaque) {
1182 QTAILQ_REMOVE(&reset_handlers, re, entry);
1183 qemu_free(re);
1184 return;
1189 void qemu_system_reset(void)
1191 QEMUResetEntry *re, *nre;
1193 /* reset all devices */
1194 QTAILQ_FOREACH_SAFE(re, &reset_handlers, entry, nre) {
1195 re->func(re->opaque);
1197 monitor_protocol_event(QEVENT_RESET, NULL);
1198 cpu_synchronize_all_post_reset();
1201 void qemu_system_reset_request(void)
1203 if (no_reboot) {
1204 shutdown_requested = 1;
1205 } else {
1206 reset_requested = 1;
1208 qemu_notify_event();
1211 void qemu_system_shutdown_request(void)
1213 shutdown_requested = 1;
1214 qemu_notify_event();
1217 void qemu_system_powerdown_request(void)
1219 powerdown_requested = 1;
1220 qemu_notify_event();
1223 void main_loop_wait(int nonblocking)
1225 IOHandlerRecord *ioh;
1226 fd_set rfds, wfds, xfds;
1227 int ret, nfds;
1228 struct timeval tv;
1229 int timeout;
1231 if (nonblocking)
1232 timeout = 0;
1233 else {
1234 timeout = qemu_calculate_timeout();
1235 qemu_bh_update_timeout(&timeout);
1238 os_host_main_loop_wait(&timeout);
1240 /* poll any events */
1241 /* XXX: separate device handlers from system ones */
1242 nfds = -1;
1243 FD_ZERO(&rfds);
1244 FD_ZERO(&wfds);
1245 FD_ZERO(&xfds);
1246 QLIST_FOREACH(ioh, &io_handlers, next) {
1247 if (ioh->deleted)
1248 continue;
1249 if (ioh->fd_read &&
1250 (!ioh->fd_read_poll ||
1251 ioh->fd_read_poll(ioh->opaque) != 0)) {
1252 FD_SET(ioh->fd, &rfds);
1253 if (ioh->fd > nfds)
1254 nfds = ioh->fd;
1256 if (ioh->fd_write) {
1257 FD_SET(ioh->fd, &wfds);
1258 if (ioh->fd > nfds)
1259 nfds = ioh->fd;
1263 tv.tv_sec = timeout / 1000;
1264 tv.tv_usec = (timeout % 1000) * 1000;
1266 slirp_select_fill(&nfds, &rfds, &wfds, &xfds);
1268 qemu_mutex_unlock_iothread();
1269 ret = select(nfds + 1, &rfds, &wfds, &xfds, &tv);
1270 qemu_mutex_lock_iothread();
1271 if (ret > 0) {
1272 IOHandlerRecord *pioh;
1274 QLIST_FOREACH_SAFE(ioh, &io_handlers, next, pioh) {
1275 if (ioh->deleted) {
1276 QLIST_REMOVE(ioh, next);
1277 qemu_free(ioh);
1278 continue;
1280 if (ioh->fd_read && FD_ISSET(ioh->fd, &rfds)) {
1281 ioh->fd_read(ioh->opaque);
1283 if (ioh->fd_write && FD_ISSET(ioh->fd, &wfds)) {
1284 ioh->fd_write(ioh->opaque);
1289 slirp_select_poll(&rfds, &wfds, &xfds, (ret < 0));
1291 qemu_run_all_timers();
1293 /* Check bottom-halves last in case any of the earlier events triggered
1294 them. */
1295 qemu_bh_poll();
1299 static int vm_can_run(void)
1301 if (powerdown_requested)
1302 return 0;
1303 if (reset_requested)
1304 return 0;
1305 if (shutdown_requested)
1306 return 0;
1307 if (debug_requested)
1308 return 0;
1309 return 1;
1312 qemu_irq qemu_system_powerdown;
1314 static void main_loop(void)
1316 int r;
1318 qemu_main_loop_start();
1320 for (;;) {
1321 do {
1322 bool nonblocking = false;
1323 #ifdef CONFIG_PROFILER
1324 int64_t ti;
1325 #endif
1326 #ifndef CONFIG_IOTHREAD
1327 nonblocking = cpu_exec_all();
1328 #endif
1329 #ifdef CONFIG_PROFILER
1330 ti = profile_getclock();
1331 #endif
1332 main_loop_wait(nonblocking);
1333 #ifdef CONFIG_PROFILER
1334 dev_time += profile_getclock() - ti;
1335 #endif
1336 } while (vm_can_run());
1338 if ((r = qemu_debug_requested())) {
1339 vm_stop(r);
1341 if (qemu_shutdown_requested()) {
1342 monitor_protocol_event(QEVENT_SHUTDOWN, NULL);
1343 if (no_shutdown) {
1344 vm_stop(0);
1345 no_shutdown = 0;
1346 } else
1347 break;
1349 if (qemu_reset_requested()) {
1350 pause_all_vcpus();
1351 qemu_system_reset();
1352 resume_all_vcpus();
1354 if (qemu_powerdown_requested()) {
1355 monitor_protocol_event(QEVENT_POWERDOWN, NULL);
1356 qemu_irq_raise(qemu_system_powerdown);
1358 if ((r = qemu_vmstop_requested())) {
1359 vm_stop(r);
1362 bdrv_close_all();
1363 pause_all_vcpus();
1366 static void version(void)
1368 printf("QEMU emulator version " QEMU_VERSION QEMU_PKGVERSION ", Copyright (c) 2003-2008 Fabrice Bellard\n");
1371 static void help(int exitcode)
1373 const char *options_help =
1374 #define DEF(option, opt_arg, opt_enum, opt_help, arch_mask) \
1375 opt_help
1376 #define DEFHEADING(text) stringify(text) "\n"
1377 #include "qemu-options.def"
1378 #undef DEF
1379 #undef DEFHEADING
1380 #undef GEN_DOCS
1382 version();
1383 printf("usage: %s [options] [disk_image]\n"
1384 "\n"
1385 "'disk_image' is a raw hard disk image for IDE hard disk 0\n"
1386 "\n"
1387 "%s\n"
1388 "During emulation, the following keys are useful:\n"
1389 "ctrl-alt-f toggle full screen\n"
1390 "ctrl-alt-n switch to virtual console 'n'\n"
1391 "ctrl-alt toggle mouse and keyboard grab\n"
1392 "\n"
1393 "When using -nographic, press 'ctrl-a h' to get some help.\n",
1394 "qemu",
1395 options_help);
1396 exit(exitcode);
1399 #define HAS_ARG 0x0001
1401 typedef struct QEMUOption {
1402 const char *name;
1403 int flags;
1404 int index;
1405 uint32_t arch_mask;
1406 } QEMUOption;
1408 static const QEMUOption qemu_options[] = {
1409 { "h", 0, QEMU_OPTION_h, QEMU_ARCH_ALL },
1410 #define DEF(option, opt_arg, opt_enum, opt_help, arch_mask) \
1411 { option, opt_arg, opt_enum, arch_mask },
1412 #define DEFHEADING(text)
1413 #include "qemu-options.def"
1414 #undef DEF
1415 #undef DEFHEADING
1416 #undef GEN_DOCS
1417 { NULL },
1419 static void select_vgahw (const char *p)
1421 const char *opts;
1423 default_vga = 0;
1424 vga_interface_type = VGA_NONE;
1425 if (strstart(p, "std", &opts)) {
1426 vga_interface_type = VGA_STD;
1427 } else if (strstart(p, "cirrus", &opts)) {
1428 vga_interface_type = VGA_CIRRUS;
1429 } else if (strstart(p, "vmware", &opts)) {
1430 vga_interface_type = VGA_VMWARE;
1431 } else if (strstart(p, "xenfb", &opts)) {
1432 vga_interface_type = VGA_XENFB;
1433 } else if (!strstart(p, "none", &opts)) {
1434 invalid_vga:
1435 fprintf(stderr, "Unknown vga type: %s\n", p);
1436 exit(1);
1438 while (*opts) {
1439 const char *nextopt;
1441 if (strstart(opts, ",retrace=", &nextopt)) {
1442 opts = nextopt;
1443 if (strstart(opts, "dumb", &nextopt))
1444 vga_retrace_method = VGA_RETRACE_DUMB;
1445 else if (strstart(opts, "precise", &nextopt))
1446 vga_retrace_method = VGA_RETRACE_PRECISE;
1447 else goto invalid_vga;
1448 } else goto invalid_vga;
1449 opts = nextopt;
1453 static int balloon_parse(const char *arg)
1455 QemuOpts *opts;
1457 if (strcmp(arg, "none") == 0) {
1458 return 0;
1461 if (!strncmp(arg, "virtio", 6)) {
1462 if (arg[6] == ',') {
1463 /* have params -> parse them */
1464 opts = qemu_opts_parse(qemu_find_opts("device"), arg+7, 0);
1465 if (!opts)
1466 return -1;
1467 } else {
1468 /* create empty opts */
1469 opts = qemu_opts_create(qemu_find_opts("device"), NULL, 0);
1471 qemu_opt_set(opts, "driver", "virtio-balloon-pci");
1472 return 0;
1475 return -1;
1478 char *qemu_find_file(int type, const char *name)
1480 int len;
1481 const char *subdir;
1482 char *buf;
1484 /* If name contains path separators then try it as a straight path. */
1485 if ((strchr(name, '/') || strchr(name, '\\'))
1486 && access(name, R_OK) == 0) {
1487 return qemu_strdup(name);
1489 switch (type) {
1490 case QEMU_FILE_TYPE_BIOS:
1491 subdir = "";
1492 break;
1493 case QEMU_FILE_TYPE_KEYMAP:
1494 subdir = "keymaps/";
1495 break;
1496 default:
1497 abort();
1499 len = strlen(data_dir) + strlen(name) + strlen(subdir) + 2;
1500 buf = qemu_mallocz(len);
1501 snprintf(buf, len, "%s/%s%s", data_dir, subdir, name);
1502 if (access(buf, R_OK)) {
1503 qemu_free(buf);
1504 return NULL;
1506 return buf;
1509 static int device_help_func(QemuOpts *opts, void *opaque)
1511 return qdev_device_help(opts);
1514 static int device_init_func(QemuOpts *opts, void *opaque)
1516 DeviceState *dev;
1518 dev = qdev_device_add(opts);
1519 if (!dev)
1520 return -1;
1521 return 0;
1524 static int chardev_init_func(QemuOpts *opts, void *opaque)
1526 CharDriverState *chr;
1528 chr = qemu_chr_open_opts(opts, NULL);
1529 if (!chr)
1530 return -1;
1531 return 0;
1534 #ifdef CONFIG_VIRTFS
1535 static int fsdev_init_func(QemuOpts *opts, void *opaque)
1537 int ret;
1538 ret = qemu_fsdev_add(opts);
1540 return ret;
1542 #endif
1544 static int mon_init_func(QemuOpts *opts, void *opaque)
1546 CharDriverState *chr;
1547 const char *chardev;
1548 const char *mode;
1549 int flags;
1551 mode = qemu_opt_get(opts, "mode");
1552 if (mode == NULL) {
1553 mode = "readline";
1555 if (strcmp(mode, "readline") == 0) {
1556 flags = MONITOR_USE_READLINE;
1557 } else if (strcmp(mode, "control") == 0) {
1558 flags = MONITOR_USE_CONTROL;
1559 } else {
1560 fprintf(stderr, "unknown monitor mode \"%s\"\n", mode);
1561 exit(1);
1564 if (qemu_opt_get_bool(opts, "default", 0))
1565 flags |= MONITOR_IS_DEFAULT;
1567 chardev = qemu_opt_get(opts, "chardev");
1568 chr = qemu_chr_find(chardev);
1569 if (chr == NULL) {
1570 fprintf(stderr, "chardev \"%s\" not found\n", chardev);
1571 exit(1);
1574 monitor_init(chr, flags);
1575 return 0;
1578 static void monitor_parse(const char *optarg, const char *mode)
1580 static int monitor_device_index = 0;
1581 QemuOpts *opts;
1582 const char *p;
1583 char label[32];
1584 int def = 0;
1586 if (strstart(optarg, "chardev:", &p)) {
1587 snprintf(label, sizeof(label), "%s", p);
1588 } else {
1589 snprintf(label, sizeof(label), "compat_monitor%d",
1590 monitor_device_index);
1591 if (monitor_device_index == 0) {
1592 def = 1;
1594 opts = qemu_chr_parse_compat(label, optarg);
1595 if (!opts) {
1596 fprintf(stderr, "parse error: %s\n", optarg);
1597 exit(1);
1601 opts = qemu_opts_create(qemu_find_opts("mon"), label, 1);
1602 if (!opts) {
1603 fprintf(stderr, "duplicate chardev: %s\n", label);
1604 exit(1);
1606 qemu_opt_set(opts, "mode", mode);
1607 qemu_opt_set(opts, "chardev", label);
1608 if (def)
1609 qemu_opt_set(opts, "default", "on");
1610 monitor_device_index++;
1613 struct device_config {
1614 enum {
1615 DEV_USB, /* -usbdevice */
1616 DEV_BT, /* -bt */
1617 DEV_SERIAL, /* -serial */
1618 DEV_PARALLEL, /* -parallel */
1619 DEV_VIRTCON, /* -virtioconsole */
1620 DEV_DEBUGCON, /* -debugcon */
1621 } type;
1622 const char *cmdline;
1623 QTAILQ_ENTRY(device_config) next;
1625 QTAILQ_HEAD(, device_config) device_configs = QTAILQ_HEAD_INITIALIZER(device_configs);
1627 static void add_device_config(int type, const char *cmdline)
1629 struct device_config *conf;
1631 conf = qemu_mallocz(sizeof(*conf));
1632 conf->type = type;
1633 conf->cmdline = cmdline;
1634 QTAILQ_INSERT_TAIL(&device_configs, conf, next);
1637 static int foreach_device_config(int type, int (*func)(const char *cmdline))
1639 struct device_config *conf;
1640 int rc;
1642 QTAILQ_FOREACH(conf, &device_configs, next) {
1643 if (conf->type != type)
1644 continue;
1645 rc = func(conf->cmdline);
1646 if (0 != rc)
1647 return rc;
1649 return 0;
1652 static int serial_parse(const char *devname)
1654 static int index = 0;
1655 char label[32];
1657 if (strcmp(devname, "none") == 0)
1658 return 0;
1659 if (index == MAX_SERIAL_PORTS) {
1660 fprintf(stderr, "qemu: too many serial ports\n");
1661 exit(1);
1663 snprintf(label, sizeof(label), "serial%d", index);
1664 serial_hds[index] = qemu_chr_open(label, devname, NULL);
1665 if (!serial_hds[index]) {
1666 fprintf(stderr, "qemu: could not open serial device '%s': %s\n",
1667 devname, strerror(errno));
1668 return -1;
1670 index++;
1671 return 0;
1674 static int parallel_parse(const char *devname)
1676 static int index = 0;
1677 char label[32];
1679 if (strcmp(devname, "none") == 0)
1680 return 0;
1681 if (index == MAX_PARALLEL_PORTS) {
1682 fprintf(stderr, "qemu: too many parallel ports\n");
1683 exit(1);
1685 snprintf(label, sizeof(label), "parallel%d", index);
1686 parallel_hds[index] = qemu_chr_open(label, devname, NULL);
1687 if (!parallel_hds[index]) {
1688 fprintf(stderr, "qemu: could not open parallel device '%s': %s\n",
1689 devname, strerror(errno));
1690 return -1;
1692 index++;
1693 return 0;
1696 static int virtcon_parse(const char *devname)
1698 QemuOptsList *device = qemu_find_opts("device");
1699 static int index = 0;
1700 char label[32];
1701 QemuOpts *bus_opts, *dev_opts;
1703 if (strcmp(devname, "none") == 0)
1704 return 0;
1705 if (index == MAX_VIRTIO_CONSOLES) {
1706 fprintf(stderr, "qemu: too many virtio consoles\n");
1707 exit(1);
1710 bus_opts = qemu_opts_create(device, NULL, 0);
1711 qemu_opt_set(bus_opts, "driver", "virtio-serial");
1713 dev_opts = qemu_opts_create(device, NULL, 0);
1714 qemu_opt_set(dev_opts, "driver", "virtconsole");
1716 snprintf(label, sizeof(label), "virtcon%d", index);
1717 virtcon_hds[index] = qemu_chr_open(label, devname, NULL);
1718 if (!virtcon_hds[index]) {
1719 fprintf(stderr, "qemu: could not open virtio console '%s': %s\n",
1720 devname, strerror(errno));
1721 return -1;
1723 qemu_opt_set(dev_opts, "chardev", label);
1725 index++;
1726 return 0;
1729 static int debugcon_parse(const char *devname)
1731 QemuOpts *opts;
1733 if (!qemu_chr_open("debugcon", devname, NULL)) {
1734 exit(1);
1736 opts = qemu_opts_create(qemu_find_opts("device"), "debugcon", 1);
1737 if (!opts) {
1738 fprintf(stderr, "qemu: already have a debugcon device\n");
1739 exit(1);
1741 qemu_opt_set(opts, "driver", "isa-debugcon");
1742 qemu_opt_set(opts, "chardev", "debugcon");
1743 return 0;
1746 void qemu_add_exit_notifier(Notifier *notify)
1748 notifier_list_add(&exit_notifiers, notify);
1751 void qemu_remove_exit_notifier(Notifier *notify)
1753 notifier_list_remove(&exit_notifiers, notify);
1756 static void qemu_run_exit_notifiers(void)
1758 notifier_list_notify(&exit_notifiers);
1761 static const QEMUOption *lookup_opt(int argc, char **argv,
1762 const char **poptarg, int *poptind)
1764 const QEMUOption *popt;
1765 int optind = *poptind;
1766 char *r = argv[optind];
1767 const char *optarg;
1769 loc_set_cmdline(argv, optind, 1);
1770 optind++;
1771 /* Treat --foo the same as -foo. */
1772 if (r[1] == '-')
1773 r++;
1774 popt = qemu_options;
1775 for(;;) {
1776 if (!popt->name) {
1777 error_report("invalid option");
1778 exit(1);
1780 if (!strcmp(popt->name, r + 1))
1781 break;
1782 popt++;
1784 if (popt->flags & HAS_ARG) {
1785 if (optind >= argc) {
1786 error_report("requires an argument");
1787 exit(1);
1789 optarg = argv[optind++];
1790 loc_set_cmdline(argv, optind - 2, 2);
1791 } else {
1792 optarg = NULL;
1795 *poptarg = optarg;
1796 *poptind = optind;
1798 return popt;
1801 int main(int argc, char **argv, char **envp)
1803 const char *gdbstub_dev = NULL;
1804 int i;
1805 int snapshot, linux_boot;
1806 const char *icount_option = NULL;
1807 const char *initrd_filename;
1808 const char *kernel_filename, *kernel_cmdline;
1809 char boot_devices[33] = "cad"; /* default to HD->floppy->CD-ROM */
1810 DisplayState *ds;
1811 DisplayChangeListener *dcl;
1812 int cyls, heads, secs, translation;
1813 QemuOpts *hda_opts = NULL, *opts;
1814 QemuOptsList *olist;
1815 int optind;
1816 const char *optarg;
1817 const char *loadvm = NULL;
1818 QEMUMachine *machine;
1819 const char *cpu_model;
1820 int tb_size;
1821 const char *pid_file = NULL;
1822 const char *incoming = NULL;
1823 int show_vnc_port = 0;
1824 int defconfig = 1;
1826 atexit(qemu_run_exit_notifiers);
1827 error_set_progname(argv[0]);
1829 init_clocks();
1831 qemu_cache_utils_init(envp);
1833 QLIST_INIT (&vm_change_state_head);
1834 os_setup_early_signal_handling();
1836 module_call_init(MODULE_INIT_MACHINE);
1837 machine = find_default_machine();
1838 cpu_model = NULL;
1839 initrd_filename = NULL;
1840 ram_size = 0;
1841 snapshot = 0;
1842 kernel_filename = NULL;
1843 kernel_cmdline = "";
1844 cyls = heads = secs = 0;
1845 translation = BIOS_ATA_TRANSLATION_AUTO;
1847 for (i = 0; i < MAX_NODES; i++) {
1848 node_mem[i] = 0;
1849 node_cpumask[i] = 0;
1852 nb_numa_nodes = 0;
1853 nb_nics = 0;
1855 tb_size = 0;
1856 autostart= 1;
1858 #ifdef CONFIG_VIRTFS
1859 qemu_add_opts(&qemu_fsdev_opts);
1860 qemu_add_opts(&qemu_virtfs_opts);
1861 #endif
1863 /* first pass of option parsing */
1864 optind = 1;
1865 while (optind < argc) {
1866 if (argv[optind][0] != '-') {
1867 /* disk image */
1868 optind++;
1869 continue;
1870 } else {
1871 const QEMUOption *popt;
1873 popt = lookup_opt(argc, argv, &optarg, &optind);
1874 switch (popt->index) {
1875 case QEMU_OPTION_nodefconfig:
1876 defconfig=0;
1877 break;
1882 if (defconfig) {
1883 int ret;
1885 ret = qemu_read_config_file(CONFIG_QEMU_CONFDIR "/qemu.conf");
1886 if (ret < 0 && ret != -ENOENT) {
1887 exit(1);
1890 ret = qemu_read_config_file(arch_config_name);
1891 if (ret < 0 && ret != -ENOENT) {
1892 exit(1);
1895 cpudef_init();
1897 /* second pass of option parsing */
1898 optind = 1;
1899 for(;;) {
1900 if (optind >= argc)
1901 break;
1902 if (argv[optind][0] != '-') {
1903 hda_opts = drive_add(argv[optind++], HD_ALIAS, 0);
1904 } else {
1905 const QEMUOption *popt;
1907 popt = lookup_opt(argc, argv, &optarg, &optind);
1908 if (!(popt->arch_mask & arch_type)) {
1909 printf("Option %s not supported for this target\n", popt->name);
1910 exit(1);
1912 switch(popt->index) {
1913 case QEMU_OPTION_M:
1914 machine = find_machine(optarg);
1915 if (!machine) {
1916 QEMUMachine *m;
1917 printf("Supported machines are:\n");
1918 for(m = first_machine; m != NULL; m = m->next) {
1919 if (m->alias)
1920 printf("%-10s %s (alias of %s)\n",
1921 m->alias, m->desc, m->name);
1922 printf("%-10s %s%s\n",
1923 m->name, m->desc,
1924 m->is_default ? " (default)" : "");
1926 exit(*optarg != '?');
1928 break;
1929 case QEMU_OPTION_cpu:
1930 /* hw initialization will check this */
1931 if (*optarg == '?') {
1932 list_cpus(stdout, &fprintf, optarg);
1933 exit(0);
1934 } else {
1935 cpu_model = optarg;
1937 break;
1938 case QEMU_OPTION_initrd:
1939 initrd_filename = optarg;
1940 break;
1941 case QEMU_OPTION_hda:
1942 if (cyls == 0)
1943 hda_opts = drive_add(optarg, HD_ALIAS, 0);
1944 else
1945 hda_opts = drive_add(optarg, HD_ALIAS
1946 ",cyls=%d,heads=%d,secs=%d%s",
1947 0, cyls, heads, secs,
1948 translation == BIOS_ATA_TRANSLATION_LBA ?
1949 ",trans=lba" :
1950 translation == BIOS_ATA_TRANSLATION_NONE ?
1951 ",trans=none" : "");
1952 break;
1953 case QEMU_OPTION_hdb:
1954 case QEMU_OPTION_hdc:
1955 case QEMU_OPTION_hdd:
1956 drive_add(optarg, HD_ALIAS, popt->index - QEMU_OPTION_hda);
1957 break;
1958 case QEMU_OPTION_drive:
1959 drive_add(NULL, "%s", optarg);
1960 break;
1961 case QEMU_OPTION_set:
1962 if (qemu_set_option(optarg) != 0)
1963 exit(1);
1964 break;
1965 case QEMU_OPTION_global:
1966 if (qemu_global_option(optarg) != 0)
1967 exit(1);
1968 break;
1969 case QEMU_OPTION_mtdblock:
1970 drive_add(optarg, MTD_ALIAS);
1971 break;
1972 case QEMU_OPTION_sd:
1973 drive_add(optarg, SD_ALIAS);
1974 break;
1975 case QEMU_OPTION_pflash:
1976 drive_add(optarg, PFLASH_ALIAS);
1977 break;
1978 case QEMU_OPTION_snapshot:
1979 snapshot = 1;
1980 break;
1981 case QEMU_OPTION_hdachs:
1983 const char *p;
1984 p = optarg;
1985 cyls = strtol(p, (char **)&p, 0);
1986 if (cyls < 1 || cyls > 16383)
1987 goto chs_fail;
1988 if (*p != ',')
1989 goto chs_fail;
1990 p++;
1991 heads = strtol(p, (char **)&p, 0);
1992 if (heads < 1 || heads > 16)
1993 goto chs_fail;
1994 if (*p != ',')
1995 goto chs_fail;
1996 p++;
1997 secs = strtol(p, (char **)&p, 0);
1998 if (secs < 1 || secs > 63)
1999 goto chs_fail;
2000 if (*p == ',') {
2001 p++;
2002 if (!strcmp(p, "none"))
2003 translation = BIOS_ATA_TRANSLATION_NONE;
2004 else if (!strcmp(p, "lba"))
2005 translation = BIOS_ATA_TRANSLATION_LBA;
2006 else if (!strcmp(p, "auto"))
2007 translation = BIOS_ATA_TRANSLATION_AUTO;
2008 else
2009 goto chs_fail;
2010 } else if (*p != '\0') {
2011 chs_fail:
2012 fprintf(stderr, "qemu: invalid physical CHS format\n");
2013 exit(1);
2015 if (hda_opts != NULL) {
2016 char num[16];
2017 snprintf(num, sizeof(num), "%d", cyls);
2018 qemu_opt_set(hda_opts, "cyls", num);
2019 snprintf(num, sizeof(num), "%d", heads);
2020 qemu_opt_set(hda_opts, "heads", num);
2021 snprintf(num, sizeof(num), "%d", secs);
2022 qemu_opt_set(hda_opts, "secs", num);
2023 if (translation == BIOS_ATA_TRANSLATION_LBA)
2024 qemu_opt_set(hda_opts, "trans", "lba");
2025 if (translation == BIOS_ATA_TRANSLATION_NONE)
2026 qemu_opt_set(hda_opts, "trans", "none");
2029 break;
2030 case QEMU_OPTION_numa:
2031 if (nb_numa_nodes >= MAX_NODES) {
2032 fprintf(stderr, "qemu: too many NUMA nodes\n");
2033 exit(1);
2035 numa_add(optarg);
2036 break;
2037 case QEMU_OPTION_nographic:
2038 display_type = DT_NOGRAPHIC;
2039 break;
2040 #ifdef CONFIG_CURSES
2041 case QEMU_OPTION_curses:
2042 display_type = DT_CURSES;
2043 break;
2044 #endif
2045 case QEMU_OPTION_portrait:
2046 graphic_rotate = 1;
2047 break;
2048 case QEMU_OPTION_kernel:
2049 kernel_filename = optarg;
2050 break;
2051 case QEMU_OPTION_append:
2052 kernel_cmdline = optarg;
2053 break;
2054 case QEMU_OPTION_cdrom:
2055 drive_add(optarg, CDROM_ALIAS);
2056 break;
2057 case QEMU_OPTION_boot:
2059 static const char * const params[] = {
2060 "order", "once", "menu", NULL
2062 char buf[sizeof(boot_devices)];
2063 char *standard_boot_devices;
2064 int legacy = 0;
2066 if (!strchr(optarg, '=')) {
2067 legacy = 1;
2068 pstrcpy(buf, sizeof(buf), optarg);
2069 } else if (check_params(buf, sizeof(buf), params, optarg) < 0) {
2070 fprintf(stderr,
2071 "qemu: unknown boot parameter '%s' in '%s'\n",
2072 buf, optarg);
2073 exit(1);
2076 if (legacy ||
2077 get_param_value(buf, sizeof(buf), "order", optarg)) {
2078 validate_bootdevices(buf);
2079 pstrcpy(boot_devices, sizeof(boot_devices), buf);
2081 if (!legacy) {
2082 if (get_param_value(buf, sizeof(buf),
2083 "once", optarg)) {
2084 validate_bootdevices(buf);
2085 standard_boot_devices = qemu_strdup(boot_devices);
2086 pstrcpy(boot_devices, sizeof(boot_devices), buf);
2087 qemu_register_reset(restore_boot_devices,
2088 standard_boot_devices);
2090 if (get_param_value(buf, sizeof(buf),
2091 "menu", optarg)) {
2092 if (!strcmp(buf, "on")) {
2093 boot_menu = 1;
2094 } else if (!strcmp(buf, "off")) {
2095 boot_menu = 0;
2096 } else {
2097 fprintf(stderr,
2098 "qemu: invalid option value '%s'\n",
2099 buf);
2100 exit(1);
2105 break;
2106 case QEMU_OPTION_fda:
2107 case QEMU_OPTION_fdb:
2108 drive_add(optarg, FD_ALIAS, popt->index - QEMU_OPTION_fda);
2109 break;
2110 case QEMU_OPTION_no_fd_bootchk:
2111 fd_bootchk = 0;
2112 break;
2113 case QEMU_OPTION_netdev:
2114 if (net_client_parse(qemu_find_opts("netdev"), optarg) == -1) {
2115 exit(1);
2117 break;
2118 case QEMU_OPTION_net:
2119 if (net_client_parse(qemu_find_opts("net"), optarg) == -1) {
2120 exit(1);
2122 break;
2123 #ifdef CONFIG_SLIRP
2124 case QEMU_OPTION_tftp:
2125 legacy_tftp_prefix = optarg;
2126 break;
2127 case QEMU_OPTION_bootp:
2128 legacy_bootp_filename = optarg;
2129 break;
2130 case QEMU_OPTION_redir:
2131 if (net_slirp_redir(optarg) < 0)
2132 exit(1);
2133 break;
2134 #endif
2135 case QEMU_OPTION_bt:
2136 add_device_config(DEV_BT, optarg);
2137 break;
2138 case QEMU_OPTION_audio_help:
2139 if (!(audio_available())) {
2140 printf("Option %s not supported for this target\n", popt->name);
2141 exit(1);
2143 AUD_help ();
2144 exit (0);
2145 break;
2146 case QEMU_OPTION_soundhw:
2147 if (!(audio_available())) {
2148 printf("Option %s not supported for this target\n", popt->name);
2149 exit(1);
2151 select_soundhw (optarg);
2152 break;
2153 case QEMU_OPTION_h:
2154 help(0);
2155 break;
2156 case QEMU_OPTION_version:
2157 version();
2158 exit(0);
2159 break;
2160 case QEMU_OPTION_m: {
2161 uint64_t value;
2162 char *ptr;
2164 value = strtoul(optarg, &ptr, 10);
2165 switch (*ptr) {
2166 case 0: case 'M': case 'm':
2167 value <<= 20;
2168 break;
2169 case 'G': case 'g':
2170 value <<= 30;
2171 break;
2172 default:
2173 fprintf(stderr, "qemu: invalid ram size: %s\n", optarg);
2174 exit(1);
2177 /* On 32-bit hosts, QEMU is limited by virtual address space */
2178 if (value > (2047 << 20) && HOST_LONG_BITS == 32) {
2179 fprintf(stderr, "qemu: at most 2047 MB RAM can be simulated\n");
2180 exit(1);
2182 if (value != (uint64_t)(ram_addr_t)value) {
2183 fprintf(stderr, "qemu: ram size too large\n");
2184 exit(1);
2186 ram_size = value;
2187 break;
2189 case QEMU_OPTION_mempath:
2190 mem_path = optarg;
2191 break;
2192 #ifdef MAP_POPULATE
2193 case QEMU_OPTION_mem_prealloc:
2194 mem_prealloc = 1;
2195 break;
2196 #endif
2197 case QEMU_OPTION_d:
2198 set_cpu_log(optarg);
2199 break;
2200 case QEMU_OPTION_s:
2201 gdbstub_dev = "tcp::" DEFAULT_GDBSTUB_PORT;
2202 break;
2203 case QEMU_OPTION_gdb:
2204 gdbstub_dev = optarg;
2205 break;
2206 case QEMU_OPTION_L:
2207 data_dir = optarg;
2208 break;
2209 case QEMU_OPTION_bios:
2210 bios_name = optarg;
2211 break;
2212 case QEMU_OPTION_singlestep:
2213 singlestep = 1;
2214 break;
2215 case QEMU_OPTION_S:
2216 autostart = 0;
2217 break;
2218 case QEMU_OPTION_k:
2219 keyboard_layout = optarg;
2220 break;
2221 case QEMU_OPTION_localtime:
2222 rtc_utc = 0;
2223 break;
2224 case QEMU_OPTION_vga:
2225 select_vgahw (optarg);
2226 break;
2227 case QEMU_OPTION_g:
2229 const char *p;
2230 int w, h, depth;
2231 p = optarg;
2232 w = strtol(p, (char **)&p, 10);
2233 if (w <= 0) {
2234 graphic_error:
2235 fprintf(stderr, "qemu: invalid resolution or depth\n");
2236 exit(1);
2238 if (*p != 'x')
2239 goto graphic_error;
2240 p++;
2241 h = strtol(p, (char **)&p, 10);
2242 if (h <= 0)
2243 goto graphic_error;
2244 if (*p == 'x') {
2245 p++;
2246 depth = strtol(p, (char **)&p, 10);
2247 if (depth != 8 && depth != 15 && depth != 16 &&
2248 depth != 24 && depth != 32)
2249 goto graphic_error;
2250 } else if (*p == '\0') {
2251 depth = graphic_depth;
2252 } else {
2253 goto graphic_error;
2256 graphic_width = w;
2257 graphic_height = h;
2258 graphic_depth = depth;
2260 break;
2261 case QEMU_OPTION_echr:
2263 char *r;
2264 term_escape_char = strtol(optarg, &r, 0);
2265 if (r == optarg)
2266 printf("Bad argument to echr\n");
2267 break;
2269 case QEMU_OPTION_monitor:
2270 monitor_parse(optarg, "readline");
2271 default_monitor = 0;
2272 break;
2273 case QEMU_OPTION_qmp:
2274 monitor_parse(optarg, "control");
2275 default_monitor = 0;
2276 break;
2277 case QEMU_OPTION_mon:
2278 opts = qemu_opts_parse(qemu_find_opts("mon"), optarg, 1);
2279 if (!opts) {
2280 exit(1);
2282 default_monitor = 0;
2283 break;
2284 case QEMU_OPTION_chardev:
2285 opts = qemu_opts_parse(qemu_find_opts("chardev"), optarg, 1);
2286 if (!opts) {
2287 exit(1);
2289 break;
2290 case QEMU_OPTION_fsdev:
2291 olist = qemu_find_opts("fsdev");
2292 if (!olist) {
2293 fprintf(stderr, "fsdev is not supported by this qemu build.\n");
2294 exit(1);
2296 opts = qemu_opts_parse(olist, optarg, 1);
2297 if (!opts) {
2298 fprintf(stderr, "parse error: %s\n", optarg);
2299 exit(1);
2301 break;
2302 case QEMU_OPTION_virtfs: {
2303 char *arg_fsdev = NULL;
2304 char *arg_9p = NULL;
2305 int len = 0;
2307 olist = qemu_find_opts("virtfs");
2308 if (!olist) {
2309 fprintf(stderr, "virtfs is not supported by this qemu build.\n");
2310 exit(1);
2312 opts = qemu_opts_parse(olist, optarg, 1);
2313 if (!opts) {
2314 fprintf(stderr, "parse error: %s\n", optarg);
2315 exit(1);
2318 if (qemu_opt_get(opts, "fstype") == NULL ||
2319 qemu_opt_get(opts, "mount_tag") == NULL ||
2320 qemu_opt_get(opts, "path") == NULL ||
2321 qemu_opt_get(opts, "security_model") == NULL) {
2322 fprintf(stderr, "Usage: -virtfs fstype,path=/share_path/,"
2323 "security_model=[mapped|passthrough],"
2324 "mnt_tag=tag.\n");
2325 exit(1);
2328 len = strlen(",id=,path=,security_model=");
2329 len += strlen(qemu_opt_get(opts, "fstype"));
2330 len += strlen(qemu_opt_get(opts, "mount_tag"));
2331 len += strlen(qemu_opt_get(opts, "path"));
2332 len += strlen(qemu_opt_get(opts, "security_model"));
2333 arg_fsdev = qemu_malloc((len + 1) * sizeof(*arg_fsdev));
2335 snprintf(arg_fsdev, (len + 1) * sizeof(*arg_fsdev),
2336 "%s,id=%s,path=%s,security_model=%s",
2337 qemu_opt_get(opts, "fstype"),
2338 qemu_opt_get(opts, "mount_tag"),
2339 qemu_opt_get(opts, "path"),
2340 qemu_opt_get(opts, "security_model"));
2342 len = strlen("virtio-9p-pci,fsdev=,mount_tag=");
2343 len += 2*strlen(qemu_opt_get(opts, "mount_tag"));
2344 arg_9p = qemu_malloc((len + 1) * sizeof(*arg_9p));
2346 snprintf(arg_9p, (len + 1) * sizeof(*arg_9p),
2347 "virtio-9p-pci,fsdev=%s,mount_tag=%s",
2348 qemu_opt_get(opts, "mount_tag"),
2349 qemu_opt_get(opts, "mount_tag"));
2351 if (!qemu_opts_parse(qemu_find_opts("fsdev"), arg_fsdev, 1)) {
2352 fprintf(stderr, "parse error [fsdev]: %s\n", optarg);
2353 exit(1);
2356 if (!qemu_opts_parse(qemu_find_opts("device"), arg_9p, 1)) {
2357 fprintf(stderr, "parse error [device]: %s\n", optarg);
2358 exit(1);
2361 qemu_free(arg_fsdev);
2362 qemu_free(arg_9p);
2363 break;
2365 case QEMU_OPTION_serial:
2366 add_device_config(DEV_SERIAL, optarg);
2367 default_serial = 0;
2368 if (strncmp(optarg, "mon:", 4) == 0) {
2369 default_monitor = 0;
2371 break;
2372 case QEMU_OPTION_watchdog:
2373 if (watchdog) {
2374 fprintf(stderr,
2375 "qemu: only one watchdog option may be given\n");
2376 return 1;
2378 watchdog = optarg;
2379 break;
2380 case QEMU_OPTION_watchdog_action:
2381 if (select_watchdog_action(optarg) == -1) {
2382 fprintf(stderr, "Unknown -watchdog-action parameter\n");
2383 exit(1);
2385 break;
2386 case QEMU_OPTION_virtiocon:
2387 add_device_config(DEV_VIRTCON, optarg);
2388 default_virtcon = 0;
2389 if (strncmp(optarg, "mon:", 4) == 0) {
2390 default_monitor = 0;
2392 break;
2393 case QEMU_OPTION_parallel:
2394 add_device_config(DEV_PARALLEL, optarg);
2395 default_parallel = 0;
2396 if (strncmp(optarg, "mon:", 4) == 0) {
2397 default_monitor = 0;
2399 break;
2400 case QEMU_OPTION_debugcon:
2401 add_device_config(DEV_DEBUGCON, optarg);
2402 break;
2403 case QEMU_OPTION_loadvm:
2404 loadvm = optarg;
2405 break;
2406 case QEMU_OPTION_full_screen:
2407 full_screen = 1;
2408 break;
2409 #ifdef CONFIG_SDL
2410 case QEMU_OPTION_no_frame:
2411 no_frame = 1;
2412 break;
2413 case QEMU_OPTION_alt_grab:
2414 alt_grab = 1;
2415 break;
2416 case QEMU_OPTION_ctrl_grab:
2417 ctrl_grab = 1;
2418 break;
2419 case QEMU_OPTION_no_quit:
2420 no_quit = 1;
2421 break;
2422 case QEMU_OPTION_sdl:
2423 display_type = DT_SDL;
2424 break;
2425 #endif
2426 case QEMU_OPTION_pidfile:
2427 pid_file = optarg;
2428 break;
2429 case QEMU_OPTION_win2k_hack:
2430 win2k_install_hack = 1;
2431 break;
2432 case QEMU_OPTION_rtc_td_hack:
2433 rtc_td_hack = 1;
2434 break;
2435 case QEMU_OPTION_acpitable:
2436 do_acpitable_option(optarg);
2437 break;
2438 case QEMU_OPTION_smbios:
2439 do_smbios_option(optarg);
2440 break;
2441 case QEMU_OPTION_enable_kvm:
2442 kvm_allowed = 1;
2443 break;
2444 case QEMU_OPTION_usb:
2445 usb_enabled = 1;
2446 break;
2447 case QEMU_OPTION_usbdevice:
2448 usb_enabled = 1;
2449 add_device_config(DEV_USB, optarg);
2450 break;
2451 case QEMU_OPTION_device:
2452 if (!qemu_opts_parse(qemu_find_opts("device"), optarg, 1)) {
2453 exit(1);
2455 break;
2456 case QEMU_OPTION_smp:
2457 smp_parse(optarg);
2458 if (smp_cpus < 1) {
2459 fprintf(stderr, "Invalid number of CPUs\n");
2460 exit(1);
2462 if (max_cpus < smp_cpus) {
2463 fprintf(stderr, "maxcpus must be equal to or greater than "
2464 "smp\n");
2465 exit(1);
2467 if (max_cpus > 255) {
2468 fprintf(stderr, "Unsupported number of maxcpus\n");
2469 exit(1);
2471 break;
2472 case QEMU_OPTION_vnc:
2473 display_type = DT_VNC;
2474 vnc_display = optarg;
2475 break;
2476 case QEMU_OPTION_no_acpi:
2477 acpi_enabled = 0;
2478 break;
2479 case QEMU_OPTION_no_hpet:
2480 no_hpet = 1;
2481 break;
2482 case QEMU_OPTION_balloon:
2483 if (balloon_parse(optarg) < 0) {
2484 fprintf(stderr, "Unknown -balloon argument %s\n", optarg);
2485 exit(1);
2487 break;
2488 case QEMU_OPTION_no_reboot:
2489 no_reboot = 1;
2490 break;
2491 case QEMU_OPTION_no_shutdown:
2492 no_shutdown = 1;
2493 break;
2494 case QEMU_OPTION_show_cursor:
2495 cursor_hide = 0;
2496 break;
2497 case QEMU_OPTION_uuid:
2498 if(qemu_uuid_parse(optarg, qemu_uuid) < 0) {
2499 fprintf(stderr, "Fail to parse UUID string."
2500 " Wrong format.\n");
2501 exit(1);
2503 break;
2504 case QEMU_OPTION_option_rom:
2505 if (nb_option_roms >= MAX_OPTION_ROMS) {
2506 fprintf(stderr, "Too many option ROMs\n");
2507 exit(1);
2509 option_rom[nb_option_roms] = optarg;
2510 nb_option_roms++;
2511 break;
2512 case QEMU_OPTION_semihosting:
2513 semihosting_enabled = 1;
2514 break;
2515 case QEMU_OPTION_name:
2516 qemu_name = qemu_strdup(optarg);
2518 char *p = strchr(qemu_name, ',');
2519 if (p != NULL) {
2520 *p++ = 0;
2521 if (strncmp(p, "process=", 8)) {
2522 fprintf(stderr, "Unknown subargument %s to -name", p);
2523 exit(1);
2525 p += 8;
2526 os_set_proc_name(p);
2529 break;
2530 case QEMU_OPTION_prom_env:
2531 if (nb_prom_envs >= MAX_PROM_ENVS) {
2532 fprintf(stderr, "Too many prom variables\n");
2533 exit(1);
2535 prom_envs[nb_prom_envs] = optarg;
2536 nb_prom_envs++;
2537 break;
2538 case QEMU_OPTION_old_param:
2539 old_param = 1;
2540 break;
2541 case QEMU_OPTION_clock:
2542 configure_alarms(optarg);
2543 break;
2544 case QEMU_OPTION_startdate:
2545 configure_rtc_date_offset(optarg, 1);
2546 break;
2547 case QEMU_OPTION_rtc:
2548 opts = qemu_opts_parse(qemu_find_opts("rtc"), optarg, 0);
2549 if (!opts) {
2550 exit(1);
2552 configure_rtc(opts);
2553 break;
2554 case QEMU_OPTION_tb_size:
2555 tb_size = strtol(optarg, NULL, 0);
2556 if (tb_size < 0)
2557 tb_size = 0;
2558 break;
2559 case QEMU_OPTION_icount:
2560 icount_option = optarg;
2561 break;
2562 case QEMU_OPTION_incoming:
2563 incoming = optarg;
2564 incoming_expected = true;
2565 break;
2566 case QEMU_OPTION_nodefaults:
2567 default_serial = 0;
2568 default_parallel = 0;
2569 default_virtcon = 0;
2570 default_monitor = 0;
2571 default_vga = 0;
2572 default_net = 0;
2573 default_floppy = 0;
2574 default_cdrom = 0;
2575 default_sdcard = 0;
2576 break;
2577 case QEMU_OPTION_xen_domid:
2578 if (!(xen_available())) {
2579 printf("Option %s not supported for this target\n", popt->name);
2580 exit(1);
2582 xen_domid = atoi(optarg);
2583 break;
2584 case QEMU_OPTION_xen_create:
2585 if (!(xen_available())) {
2586 printf("Option %s not supported for this target\n", popt->name);
2587 exit(1);
2589 xen_mode = XEN_CREATE;
2590 break;
2591 case QEMU_OPTION_xen_attach:
2592 if (!(xen_available())) {
2593 printf("Option %s not supported for this target\n", popt->name);
2594 exit(1);
2596 xen_mode = XEN_ATTACH;
2597 break;
2598 case QEMU_OPTION_readconfig:
2600 int ret = qemu_read_config_file(optarg);
2601 if (ret < 0) {
2602 fprintf(stderr, "read config %s: %s\n", optarg,
2603 strerror(-ret));
2604 exit(1);
2606 break;
2608 case QEMU_OPTION_writeconfig:
2610 FILE *fp;
2611 if (strcmp(optarg, "-") == 0) {
2612 fp = stdout;
2613 } else {
2614 fp = fopen(optarg, "w");
2615 if (fp == NULL) {
2616 fprintf(stderr, "open %s: %s\n", optarg, strerror(errno));
2617 exit(1);
2620 qemu_config_write(fp);
2621 fclose(fp);
2622 break;
2624 default:
2625 os_parse_cmd_args(popt->index, optarg);
2629 loc_set_none();
2631 /* If no data_dir is specified then try to find it relative to the
2632 executable path. */
2633 if (!data_dir) {
2634 data_dir = os_find_datadir(argv[0]);
2636 /* If all else fails use the install patch specified when building. */
2637 if (!data_dir) {
2638 data_dir = CONFIG_QEMU_DATADIR;
2642 * Default to max_cpus = smp_cpus, in case the user doesn't
2643 * specify a max_cpus value.
2645 if (!max_cpus)
2646 max_cpus = smp_cpus;
2648 machine->max_cpus = machine->max_cpus ?: 1; /* Default to UP */
2649 if (smp_cpus > machine->max_cpus) {
2650 fprintf(stderr, "Number of SMP cpus requested (%d), exceeds max cpus "
2651 "supported by machine `%s' (%d)\n", smp_cpus, machine->name,
2652 machine->max_cpus);
2653 exit(1);
2656 qemu_opts_foreach(qemu_find_opts("device"), default_driver_check, NULL, 0);
2657 qemu_opts_foreach(qemu_find_opts("global"), default_driver_check, NULL, 0);
2659 if (machine->no_serial) {
2660 default_serial = 0;
2662 if (machine->no_parallel) {
2663 default_parallel = 0;
2665 if (!machine->use_virtcon) {
2666 default_virtcon = 0;
2668 if (machine->no_vga) {
2669 default_vga = 0;
2671 if (machine->no_floppy) {
2672 default_floppy = 0;
2674 if (machine->no_cdrom) {
2675 default_cdrom = 0;
2677 if (machine->no_sdcard) {
2678 default_sdcard = 0;
2681 if (display_type == DT_NOGRAPHIC) {
2682 if (default_parallel)
2683 add_device_config(DEV_PARALLEL, "null");
2684 if (default_serial && default_monitor) {
2685 add_device_config(DEV_SERIAL, "mon:stdio");
2686 } else if (default_virtcon && default_monitor) {
2687 add_device_config(DEV_VIRTCON, "mon:stdio");
2688 } else {
2689 if (default_serial)
2690 add_device_config(DEV_SERIAL, "stdio");
2691 if (default_virtcon)
2692 add_device_config(DEV_VIRTCON, "stdio");
2693 if (default_monitor)
2694 monitor_parse("stdio", "readline");
2696 } else {
2697 if (default_serial)
2698 add_device_config(DEV_SERIAL, "vc:80Cx24C");
2699 if (default_parallel)
2700 add_device_config(DEV_PARALLEL, "vc:80Cx24C");
2701 if (default_monitor)
2702 monitor_parse("vc:80Cx24C", "readline");
2703 if (default_virtcon)
2704 add_device_config(DEV_VIRTCON, "vc:80Cx24C");
2706 if (default_vga)
2707 vga_interface_type = VGA_CIRRUS;
2709 socket_init();
2711 if (qemu_opts_foreach(qemu_find_opts("chardev"), chardev_init_func, NULL, 1) != 0)
2712 exit(1);
2713 #ifdef CONFIG_VIRTFS
2714 if (qemu_opts_foreach(qemu_find_opts("fsdev"), fsdev_init_func, NULL, 1) != 0) {
2715 exit(1);
2717 #endif
2719 os_daemonize();
2721 if (pid_file && qemu_create_pidfile(pid_file) != 0) {
2722 os_pidfile_error();
2723 exit(1);
2726 if (kvm_allowed) {
2727 int ret = kvm_init(smp_cpus);
2728 if (ret < 0) {
2729 if (!kvm_available()) {
2730 printf("KVM not supported for this target\n");
2731 } else {
2732 fprintf(stderr, "failed to initialize KVM: %s\n", strerror(-ret));
2734 exit(1);
2738 if (qemu_init_main_loop()) {
2739 fprintf(stderr, "qemu_init_main_loop failed\n");
2740 exit(1);
2742 linux_boot = (kernel_filename != NULL);
2744 if (!linux_boot && *kernel_cmdline != '\0') {
2745 fprintf(stderr, "-append only allowed with -kernel option\n");
2746 exit(1);
2749 if (!linux_boot && initrd_filename != NULL) {
2750 fprintf(stderr, "-initrd only allowed with -kernel option\n");
2751 exit(1);
2754 os_set_line_buffering();
2756 if (init_timer_alarm() < 0) {
2757 fprintf(stderr, "could not initialize alarm timer\n");
2758 exit(1);
2760 configure_icount(icount_option);
2762 if (net_init_clients() < 0) {
2763 exit(1);
2766 /* init the bluetooth world */
2767 if (foreach_device_config(DEV_BT, bt_parse))
2768 exit(1);
2770 /* init the memory */
2771 if (ram_size == 0)
2772 ram_size = DEFAULT_RAM_SIZE * 1024 * 1024;
2774 /* init the dynamic translator */
2775 cpu_exec_init_all(tb_size * 1024 * 1024);
2777 bdrv_init_with_whitelist();
2779 blk_mig_init();
2781 if (default_cdrom) {
2782 /* we always create the cdrom drive, even if no disk is there */
2783 drive_add(NULL, CDROM_ALIAS);
2786 if (default_floppy) {
2787 /* we always create at least one floppy */
2788 drive_add(NULL, FD_ALIAS, 0);
2791 if (default_sdcard) {
2792 /* we always create one sd slot, even if no card is in it */
2793 drive_add(NULL, SD_ALIAS);
2796 /* open the virtual block devices */
2797 if (snapshot)
2798 qemu_opts_foreach(qemu_find_opts("drive"), drive_enable_snapshot, NULL, 0);
2799 if (qemu_opts_foreach(qemu_find_opts("drive"), drive_init_func, &machine->use_scsi, 1) != 0)
2800 exit(1);
2802 register_savevm_live(NULL, "ram", 0, 4, NULL, ram_save_live, NULL,
2803 ram_load, NULL);
2805 if (nb_numa_nodes > 0) {
2806 int i;
2808 if (nb_numa_nodes > smp_cpus) {
2809 nb_numa_nodes = smp_cpus;
2812 /* If no memory size if given for any node, assume the default case
2813 * and distribute the available memory equally across all nodes
2815 for (i = 0; i < nb_numa_nodes; i++) {
2816 if (node_mem[i] != 0)
2817 break;
2819 if (i == nb_numa_nodes) {
2820 uint64_t usedmem = 0;
2822 /* On Linux, the each node's border has to be 8MB aligned,
2823 * the final node gets the rest.
2825 for (i = 0; i < nb_numa_nodes - 1; i++) {
2826 node_mem[i] = (ram_size / nb_numa_nodes) & ~((1 << 23UL) - 1);
2827 usedmem += node_mem[i];
2829 node_mem[i] = ram_size - usedmem;
2832 for (i = 0; i < nb_numa_nodes; i++) {
2833 if (node_cpumask[i] != 0)
2834 break;
2836 /* assigning the VCPUs round-robin is easier to implement, guest OSes
2837 * must cope with this anyway, because there are BIOSes out there in
2838 * real machines which also use this scheme.
2840 if (i == nb_numa_nodes) {
2841 for (i = 0; i < smp_cpus; i++) {
2842 node_cpumask[i % nb_numa_nodes] |= 1 << i;
2847 if (qemu_opts_foreach(qemu_find_opts("mon"), mon_init_func, NULL, 1) != 0) {
2848 exit(1);
2851 if (foreach_device_config(DEV_SERIAL, serial_parse) < 0)
2852 exit(1);
2853 if (foreach_device_config(DEV_PARALLEL, parallel_parse) < 0)
2854 exit(1);
2855 if (foreach_device_config(DEV_VIRTCON, virtcon_parse) < 0)
2856 exit(1);
2857 if (foreach_device_config(DEV_DEBUGCON, debugcon_parse) < 0)
2858 exit(1);
2860 module_call_init(MODULE_INIT_DEVICE);
2862 if (qemu_opts_foreach(qemu_find_opts("device"), device_help_func, NULL, 0) != 0)
2863 exit(0);
2865 if (watchdog) {
2866 i = select_watchdog(watchdog);
2867 if (i > 0)
2868 exit (i == 1 ? 1 : 0);
2871 if (machine->compat_props) {
2872 qdev_prop_register_global_list(machine->compat_props);
2874 qemu_add_globals();
2876 machine->init(ram_size, boot_devices,
2877 kernel_filename, kernel_cmdline, initrd_filename, cpu_model);
2879 cpu_synchronize_all_post_init();
2881 /* must be after terminal init, SDL library changes signal handlers */
2882 os_setup_signal_handling();
2884 set_numa_modes();
2886 current_machine = machine;
2888 /* init USB devices */
2889 if (usb_enabled) {
2890 if (foreach_device_config(DEV_USB, usb_parse) < 0)
2891 exit(1);
2894 /* init generic devices */
2895 if (qemu_opts_foreach(qemu_find_opts("device"), device_init_func, NULL, 1) != 0)
2896 exit(1);
2898 net_check_clients();
2900 /* just use the first displaystate for the moment */
2901 ds = get_displaystate();
2903 if (display_type == DT_DEFAULT) {
2904 #if defined(CONFIG_SDL) || defined(CONFIG_COCOA)
2905 display_type = DT_SDL;
2906 #else
2907 display_type = DT_VNC;
2908 vnc_display = "localhost:0,to=99";
2909 show_vnc_port = 1;
2910 #endif
2914 switch (display_type) {
2915 case DT_NOGRAPHIC:
2916 break;
2917 #if defined(CONFIG_CURSES)
2918 case DT_CURSES:
2919 curses_display_init(ds, full_screen);
2920 break;
2921 #endif
2922 #if defined(CONFIG_SDL)
2923 case DT_SDL:
2924 sdl_display_init(ds, full_screen, no_frame);
2925 break;
2926 #elif defined(CONFIG_COCOA)
2927 case DT_SDL:
2928 cocoa_display_init(ds, full_screen);
2929 break;
2930 #endif
2931 case DT_VNC:
2932 vnc_display_init(ds);
2933 if (vnc_display_open(ds, vnc_display) < 0)
2934 exit(1);
2936 if (show_vnc_port) {
2937 printf("VNC server running on `%s'\n", vnc_display_local_addr(ds));
2939 break;
2940 default:
2941 break;
2943 dpy_resize(ds);
2945 dcl = ds->listeners;
2946 while (dcl != NULL) {
2947 if (dcl->dpy_refresh != NULL) {
2948 ds->gui_timer = qemu_new_timer(rt_clock, gui_update, ds);
2949 qemu_mod_timer(ds->gui_timer, qemu_get_clock(rt_clock));
2950 break;
2952 dcl = dcl->next;
2955 if (display_type == DT_NOGRAPHIC || display_type == DT_VNC) {
2956 nographic_timer = qemu_new_timer(rt_clock, nographic_update, NULL);
2957 qemu_mod_timer(nographic_timer, qemu_get_clock(rt_clock));
2960 text_consoles_set_display(ds);
2962 if (gdbstub_dev && gdbserver_start(gdbstub_dev) < 0) {
2963 fprintf(stderr, "qemu: could not open gdbserver on device '%s'\n",
2964 gdbstub_dev);
2965 exit(1);
2968 qdev_machine_creation_done();
2970 if (rom_load_all() != 0) {
2971 fprintf(stderr, "rom loading failed\n");
2972 exit(1);
2975 qemu_system_reset();
2976 if (loadvm) {
2977 if (load_vmstate(loadvm) < 0) {
2978 autostart = 0;
2982 if (incoming) {
2983 int ret = qemu_start_incoming_migration(incoming);
2984 if (ret < 0) {
2985 fprintf(stderr, "Migration failed. Exit code %s(%d), exiting.\n",
2986 incoming, ret);
2987 exit(ret);
2989 } else if (autostart) {
2990 vm_start();
2993 os_setup_post();
2995 main_loop();
2996 quit_timers();
2997 net_cleanup();
2999 return 0;