Added cache configuration to command line options.
[qemu/ovp.git] / vl.c
blobc2e7cc1d6ab0c72b565f311c7896495cd67af3e8
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 if (sockets) {
806 threads = smp / (cores * sockets);
810 smp_cpus = smp;
811 smp_cores = cores > 0 ? cores : 1;
812 smp_threads = threads > 0 ? threads : 1;
813 if (max_cpus == 0)
814 max_cpus = smp_cpus;
817 /***********************************************************/
818 /* USB devices */
820 static int usb_device_add(const char *devname)
822 const char *p;
823 USBDevice *dev = NULL;
825 if (!usb_enabled)
826 return -1;
828 /* drivers with .usbdevice_name entry in USBDeviceInfo */
829 dev = usbdevice_create(devname);
830 if (dev)
831 goto done;
833 /* the other ones */
834 if (strstart(devname, "host:", &p)) {
835 dev = usb_host_device_open(p);
836 } else if (!strcmp(devname, "bt") || strstart(devname, "bt:", &p)) {
837 dev = usb_bt_init(devname[2] ? hci_init(p) :
838 bt_new_hci(qemu_find_bt_vlan(0)));
839 } else {
840 return -1;
842 if (!dev)
843 return -1;
845 done:
846 return 0;
849 static int usb_device_del(const char *devname)
851 int bus_num, addr;
852 const char *p;
854 if (strstart(devname, "host:", &p))
855 return usb_host_device_close(p);
857 if (!usb_enabled)
858 return -1;
860 p = strchr(devname, '.');
861 if (!p)
862 return -1;
863 bus_num = strtoul(devname, NULL, 0);
864 addr = strtoul(p + 1, NULL, 0);
866 return usb_device_delete_addr(bus_num, addr);
869 static int usb_parse(const char *cmdline)
871 int r;
872 r = usb_device_add(cmdline);
873 if (r < 0) {
874 fprintf(stderr, "qemu: could not add USB device '%s'\n", cmdline);
876 return r;
879 void do_usb_add(Monitor *mon, const QDict *qdict)
881 const char *devname = qdict_get_str(qdict, "devname");
882 if (usb_device_add(devname) < 0) {
883 error_report("could not add USB device '%s'", devname);
887 void do_usb_del(Monitor *mon, const QDict *qdict)
889 const char *devname = qdict_get_str(qdict, "devname");
890 if (usb_device_del(devname) < 0) {
891 error_report("could not delete USB device '%s'", devname);
895 /***********************************************************/
896 /* PCMCIA/Cardbus */
898 static struct pcmcia_socket_entry_s {
899 PCMCIASocket *socket;
900 struct pcmcia_socket_entry_s *next;
901 } *pcmcia_sockets = 0;
903 void pcmcia_socket_register(PCMCIASocket *socket)
905 struct pcmcia_socket_entry_s *entry;
907 entry = qemu_malloc(sizeof(struct pcmcia_socket_entry_s));
908 entry->socket = socket;
909 entry->next = pcmcia_sockets;
910 pcmcia_sockets = entry;
913 void pcmcia_socket_unregister(PCMCIASocket *socket)
915 struct pcmcia_socket_entry_s *entry, **ptr;
917 ptr = &pcmcia_sockets;
918 for (entry = *ptr; entry; ptr = &entry->next, entry = *ptr)
919 if (entry->socket == socket) {
920 *ptr = entry->next;
921 qemu_free(entry);
925 void pcmcia_info(Monitor *mon)
927 struct pcmcia_socket_entry_s *iter;
929 if (!pcmcia_sockets)
930 monitor_printf(mon, "No PCMCIA sockets\n");
932 for (iter = pcmcia_sockets; iter; iter = iter->next)
933 monitor_printf(mon, "%s: %s\n", iter->socket->slot_string,
934 iter->socket->attached ? iter->socket->card_string :
935 "Empty");
938 /***********************************************************/
939 /* I/O handling */
941 typedef struct IOHandlerRecord {
942 int fd;
943 IOCanReadHandler *fd_read_poll;
944 IOHandler *fd_read;
945 IOHandler *fd_write;
946 int deleted;
947 void *opaque;
948 /* temporary data */
949 struct pollfd *ufd;
950 QLIST_ENTRY(IOHandlerRecord) next;
951 } IOHandlerRecord;
953 static QLIST_HEAD(, IOHandlerRecord) io_handlers =
954 QLIST_HEAD_INITIALIZER(io_handlers);
957 /* XXX: fd_read_poll should be suppressed, but an API change is
958 necessary in the character devices to suppress fd_can_read(). */
959 int qemu_set_fd_handler2(int fd,
960 IOCanReadHandler *fd_read_poll,
961 IOHandler *fd_read,
962 IOHandler *fd_write,
963 void *opaque)
965 IOHandlerRecord *ioh;
967 if (!fd_read && !fd_write) {
968 QLIST_FOREACH(ioh, &io_handlers, next) {
969 if (ioh->fd == fd) {
970 ioh->deleted = 1;
971 break;
974 } else {
975 QLIST_FOREACH(ioh, &io_handlers, next) {
976 if (ioh->fd == fd)
977 goto found;
979 ioh = qemu_mallocz(sizeof(IOHandlerRecord));
980 QLIST_INSERT_HEAD(&io_handlers, ioh, next);
981 found:
982 ioh->fd = fd;
983 ioh->fd_read_poll = fd_read_poll;
984 ioh->fd_read = fd_read;
985 ioh->fd_write = fd_write;
986 ioh->opaque = opaque;
987 ioh->deleted = 0;
989 return 0;
992 int qemu_set_fd_handler(int fd,
993 IOHandler *fd_read,
994 IOHandler *fd_write,
995 void *opaque)
997 return qemu_set_fd_handler2(fd, NULL, fd_read, fd_write, opaque);
1000 /***********************************************************/
1001 /* machine registration */
1003 static QEMUMachine *first_machine = NULL;
1004 QEMUMachine *current_machine = NULL;
1006 int qemu_register_machine(QEMUMachine *m)
1008 QEMUMachine **pm;
1009 pm = &first_machine;
1010 while (*pm != NULL)
1011 pm = &(*pm)->next;
1012 m->next = NULL;
1013 *pm = m;
1014 return 0;
1017 static QEMUMachine *find_machine(const char *name)
1019 QEMUMachine *m;
1021 for(m = first_machine; m != NULL; m = m->next) {
1022 if (!strcmp(m->name, name))
1023 return m;
1024 if (m->alias && !strcmp(m->alias, name))
1025 return m;
1027 return NULL;
1030 static QEMUMachine *find_default_machine(void)
1032 QEMUMachine *m;
1034 for(m = first_machine; m != NULL; m = m->next) {
1035 if (m->is_default) {
1036 return m;
1039 return NULL;
1042 /***********************************************************/
1043 /* main execution loop */
1045 static void gui_update(void *opaque)
1047 uint64_t interval = GUI_REFRESH_INTERVAL;
1048 DisplayState *ds = opaque;
1049 DisplayChangeListener *dcl = ds->listeners;
1051 qemu_flush_coalesced_mmio_buffer();
1052 dpy_refresh(ds);
1054 while (dcl != NULL) {
1055 if (dcl->gui_timer_interval &&
1056 dcl->gui_timer_interval < interval)
1057 interval = dcl->gui_timer_interval;
1058 dcl = dcl->next;
1060 qemu_mod_timer(ds->gui_timer, interval + qemu_get_clock(rt_clock));
1063 static void nographic_update(void *opaque)
1065 uint64_t interval = GUI_REFRESH_INTERVAL;
1067 qemu_flush_coalesced_mmio_buffer();
1068 qemu_mod_timer(nographic_timer, interval + qemu_get_clock(rt_clock));
1071 struct vm_change_state_entry {
1072 VMChangeStateHandler *cb;
1073 void *opaque;
1074 QLIST_ENTRY (vm_change_state_entry) entries;
1077 static QLIST_HEAD(vm_change_state_head, vm_change_state_entry) vm_change_state_head;
1079 VMChangeStateEntry *qemu_add_vm_change_state_handler(VMChangeStateHandler *cb,
1080 void *opaque)
1082 VMChangeStateEntry *e;
1084 e = qemu_mallocz(sizeof (*e));
1086 e->cb = cb;
1087 e->opaque = opaque;
1088 QLIST_INSERT_HEAD(&vm_change_state_head, e, entries);
1089 return e;
1092 void qemu_del_vm_change_state_handler(VMChangeStateEntry *e)
1094 QLIST_REMOVE (e, entries);
1095 qemu_free (e);
1098 void vm_state_notify(int running, int reason)
1100 VMChangeStateEntry *e;
1102 for (e = vm_change_state_head.lh_first; e; e = e->entries.le_next) {
1103 e->cb(e->opaque, running, reason);
1107 void vm_start(void)
1109 if (!vm_running) {
1110 cpu_enable_ticks();
1111 vm_running = 1;
1112 vm_state_notify(1, 0);
1113 resume_all_vcpus();
1114 monitor_protocol_event(QEVENT_RESUME, NULL);
1118 /* reset/shutdown handler */
1120 typedef struct QEMUResetEntry {
1121 QTAILQ_ENTRY(QEMUResetEntry) entry;
1122 QEMUResetHandler *func;
1123 void *opaque;
1124 } QEMUResetEntry;
1126 static QTAILQ_HEAD(reset_handlers, QEMUResetEntry) reset_handlers =
1127 QTAILQ_HEAD_INITIALIZER(reset_handlers);
1128 static int reset_requested;
1129 static int shutdown_requested;
1130 static int powerdown_requested;
1131 int debug_requested;
1132 int vmstop_requested;
1134 int qemu_shutdown_requested(void)
1136 int r = shutdown_requested;
1137 shutdown_requested = 0;
1138 return r;
1141 int qemu_reset_requested(void)
1143 int r = reset_requested;
1144 reset_requested = 0;
1145 return r;
1148 int qemu_powerdown_requested(void)
1150 int r = powerdown_requested;
1151 powerdown_requested = 0;
1152 return r;
1155 static int qemu_debug_requested(void)
1157 int r = debug_requested;
1158 debug_requested = 0;
1159 return r;
1162 static int qemu_vmstop_requested(void)
1164 int r = vmstop_requested;
1165 vmstop_requested = 0;
1166 return r;
1169 void qemu_register_reset(QEMUResetHandler *func, void *opaque)
1171 QEMUResetEntry *re = qemu_mallocz(sizeof(QEMUResetEntry));
1173 re->func = func;
1174 re->opaque = opaque;
1175 QTAILQ_INSERT_TAIL(&reset_handlers, re, entry);
1178 void qemu_unregister_reset(QEMUResetHandler *func, void *opaque)
1180 QEMUResetEntry *re;
1182 QTAILQ_FOREACH(re, &reset_handlers, entry) {
1183 if (re->func == func && re->opaque == opaque) {
1184 QTAILQ_REMOVE(&reset_handlers, re, entry);
1185 qemu_free(re);
1186 return;
1191 void qemu_system_reset(void)
1193 QEMUResetEntry *re, *nre;
1195 /* reset all devices */
1196 QTAILQ_FOREACH_SAFE(re, &reset_handlers, entry, nre) {
1197 re->func(re->opaque);
1199 monitor_protocol_event(QEVENT_RESET, NULL);
1200 cpu_synchronize_all_post_reset();
1203 void qemu_system_reset_request(void)
1205 if (no_reboot) {
1206 shutdown_requested = 1;
1207 } else {
1208 reset_requested = 1;
1210 qemu_notify_event();
1213 void qemu_system_shutdown_request(void)
1215 shutdown_requested = 1;
1216 qemu_notify_event();
1219 void qemu_system_powerdown_request(void)
1221 powerdown_requested = 1;
1222 qemu_notify_event();
1225 void main_loop_wait(int nonblocking)
1227 IOHandlerRecord *ioh;
1228 fd_set rfds, wfds, xfds;
1229 int ret, nfds;
1230 struct timeval tv;
1231 int timeout;
1233 if (nonblocking)
1234 timeout = 0;
1235 else {
1236 timeout = qemu_calculate_timeout();
1237 qemu_bh_update_timeout(&timeout);
1240 os_host_main_loop_wait(&timeout);
1242 /* poll any events */
1243 /* XXX: separate device handlers from system ones */
1244 nfds = -1;
1245 FD_ZERO(&rfds);
1246 FD_ZERO(&wfds);
1247 FD_ZERO(&xfds);
1248 QLIST_FOREACH(ioh, &io_handlers, next) {
1249 if (ioh->deleted)
1250 continue;
1251 if (ioh->fd_read &&
1252 (!ioh->fd_read_poll ||
1253 ioh->fd_read_poll(ioh->opaque) != 0)) {
1254 FD_SET(ioh->fd, &rfds);
1255 if (ioh->fd > nfds)
1256 nfds = ioh->fd;
1258 if (ioh->fd_write) {
1259 FD_SET(ioh->fd, &wfds);
1260 if (ioh->fd > nfds)
1261 nfds = ioh->fd;
1265 tv.tv_sec = timeout / 1000;
1266 tv.tv_usec = (timeout % 1000) * 1000;
1268 slirp_select_fill(&nfds, &rfds, &wfds, &xfds);
1270 qemu_mutex_unlock_iothread();
1271 ret = select(nfds + 1, &rfds, &wfds, &xfds, &tv);
1272 qemu_mutex_lock_iothread();
1273 if (ret > 0) {
1274 IOHandlerRecord *pioh;
1276 QLIST_FOREACH_SAFE(ioh, &io_handlers, next, pioh) {
1277 if (ioh->deleted) {
1278 QLIST_REMOVE(ioh, next);
1279 qemu_free(ioh);
1280 continue;
1282 if (ioh->fd_read && FD_ISSET(ioh->fd, &rfds)) {
1283 ioh->fd_read(ioh->opaque);
1285 if (ioh->fd_write && FD_ISSET(ioh->fd, &wfds)) {
1286 ioh->fd_write(ioh->opaque);
1291 slirp_select_poll(&rfds, &wfds, &xfds, (ret < 0));
1293 qemu_run_all_timers();
1295 /* Check bottom-halves last in case any of the earlier events triggered
1296 them. */
1297 qemu_bh_poll();
1301 static int vm_can_run(void)
1303 if (powerdown_requested)
1304 return 0;
1305 if (reset_requested)
1306 return 0;
1307 if (shutdown_requested)
1308 return 0;
1309 if (debug_requested)
1310 return 0;
1311 return 1;
1314 qemu_irq qemu_system_powerdown;
1316 static void main_loop(void)
1318 int r;
1320 qemu_main_loop_start();
1322 for (;;) {
1323 do {
1324 bool nonblocking = false;
1325 #ifdef CONFIG_PROFILER
1326 int64_t ti;
1327 #endif
1328 #ifndef CONFIG_IOTHREAD
1329 nonblocking = cpu_exec_all();
1330 #endif
1331 #ifdef CONFIG_PROFILER
1332 ti = profile_getclock();
1333 #endif
1334 main_loop_wait(nonblocking);
1335 #ifdef CONFIG_PROFILER
1336 dev_time += profile_getclock() - ti;
1337 #endif
1338 } while (vm_can_run());
1340 if ((r = qemu_debug_requested())) {
1341 vm_stop(r);
1343 if (qemu_shutdown_requested()) {
1344 monitor_protocol_event(QEVENT_SHUTDOWN, NULL);
1345 if (no_shutdown) {
1346 vm_stop(0);
1347 no_shutdown = 0;
1348 } else
1349 break;
1351 if (qemu_reset_requested()) {
1352 pause_all_vcpus();
1353 qemu_system_reset();
1354 resume_all_vcpus();
1356 if (qemu_powerdown_requested()) {
1357 monitor_protocol_event(QEVENT_POWERDOWN, NULL);
1358 qemu_irq_raise(qemu_system_powerdown);
1360 if ((r = qemu_vmstop_requested())) {
1361 vm_stop(r);
1364 bdrv_close_all();
1365 pause_all_vcpus();
1368 static void version(void)
1370 printf("QEMU emulator version " QEMU_VERSION QEMU_PKGVERSION ", Copyright (c) 2003-2008 Fabrice Bellard\n");
1373 static void help(int exitcode)
1375 const char *options_help =
1376 #define DEF(option, opt_arg, opt_enum, opt_help, arch_mask) \
1377 opt_help
1378 #define DEFHEADING(text) stringify(text) "\n"
1379 #include "qemu-options.def"
1380 #undef DEF
1381 #undef DEFHEADING
1382 #undef GEN_DOCS
1384 version();
1385 printf("usage: %s [options] [disk_image]\n"
1386 "\n"
1387 "'disk_image' is a raw hard disk image for IDE hard disk 0\n"
1388 "\n"
1389 "%s\n"
1390 "During emulation, the following keys are useful:\n"
1391 "ctrl-alt-f toggle full screen\n"
1392 "ctrl-alt-n switch to virtual console 'n'\n"
1393 "ctrl-alt toggle mouse and keyboard grab\n"
1394 "\n"
1395 "When using -nographic, press 'ctrl-a h' to get some help.\n",
1396 "qemu",
1397 options_help);
1398 exit(exitcode);
1401 #define HAS_ARG 0x0001
1403 typedef struct QEMUOption {
1404 const char *name;
1405 int flags;
1406 int index;
1407 uint32_t arch_mask;
1408 } QEMUOption;
1410 static const QEMUOption qemu_options[] = {
1411 { "h", 0, QEMU_OPTION_h, QEMU_ARCH_ALL },
1412 #define DEF(option, opt_arg, opt_enum, opt_help, arch_mask) \
1413 { option, opt_arg, opt_enum, arch_mask },
1414 #define DEFHEADING(text)
1415 #include "qemu-options.def"
1416 #undef DEF
1417 #undef DEFHEADING
1418 #undef GEN_DOCS
1419 { NULL },
1421 static void select_vgahw (const char *p)
1423 const char *opts;
1425 default_vga = 0;
1426 vga_interface_type = VGA_NONE;
1427 if (strstart(p, "std", &opts)) {
1428 vga_interface_type = VGA_STD;
1429 } else if (strstart(p, "cirrus", &opts)) {
1430 vga_interface_type = VGA_CIRRUS;
1431 } else if (strstart(p, "vmware", &opts)) {
1432 vga_interface_type = VGA_VMWARE;
1433 } else if (strstart(p, "xenfb", &opts)) {
1434 vga_interface_type = VGA_XENFB;
1435 } else if (!strstart(p, "none", &opts)) {
1436 invalid_vga:
1437 fprintf(stderr, "Unknown vga type: %s\n", p);
1438 exit(1);
1440 while (*opts) {
1441 const char *nextopt;
1443 if (strstart(opts, ",retrace=", &nextopt)) {
1444 opts = nextopt;
1445 if (strstart(opts, "dumb", &nextopt))
1446 vga_retrace_method = VGA_RETRACE_DUMB;
1447 else if (strstart(opts, "precise", &nextopt))
1448 vga_retrace_method = VGA_RETRACE_PRECISE;
1449 else goto invalid_vga;
1450 } else goto invalid_vga;
1451 opts = nextopt;
1455 static int balloon_parse(const char *arg)
1457 QemuOpts *opts;
1459 if (strcmp(arg, "none") == 0) {
1460 return 0;
1463 if (!strncmp(arg, "virtio", 6)) {
1464 if (arg[6] == ',') {
1465 /* have params -> parse them */
1466 opts = qemu_opts_parse(&qemu_device_opts, arg+7, 0);
1467 if (!opts)
1468 return -1;
1469 } else {
1470 /* create empty opts */
1471 opts = qemu_opts_create(&qemu_device_opts, NULL, 0);
1473 qemu_opt_set(opts, "driver", "virtio-balloon-pci");
1474 return 0;
1477 return -1;
1480 char *qemu_find_file(int type, const char *name)
1482 int len;
1483 const char *subdir;
1484 char *buf;
1486 /* If name contains path separators then try it as a straight path. */
1487 if ((strchr(name, '/') || strchr(name, '\\'))
1488 && access(name, R_OK) == 0) {
1489 return qemu_strdup(name);
1491 switch (type) {
1492 case QEMU_FILE_TYPE_BIOS:
1493 subdir = "";
1494 break;
1495 case QEMU_FILE_TYPE_KEYMAP:
1496 subdir = "keymaps/";
1497 break;
1498 default:
1499 abort();
1501 len = strlen(data_dir) + strlen(name) + strlen(subdir) + 2;
1502 buf = qemu_mallocz(len);
1503 snprintf(buf, len, "%s/%s%s", data_dir, subdir, name);
1504 if (access(buf, R_OK)) {
1505 qemu_free(buf);
1506 return NULL;
1508 return buf;
1511 static int device_help_func(QemuOpts *opts, void *opaque)
1513 return qdev_device_help(opts);
1516 static int device_init_func(QemuOpts *opts, void *opaque)
1518 DeviceState *dev;
1520 dev = qdev_device_add(opts);
1521 if (!dev)
1522 return -1;
1523 return 0;
1526 static int chardev_init_func(QemuOpts *opts, void *opaque)
1528 CharDriverState *chr;
1530 chr = qemu_chr_open_opts(opts, NULL);
1531 if (!chr)
1532 return -1;
1533 return 0;
1536 #ifdef CONFIG_VIRTFS
1537 static int fsdev_init_func(QemuOpts *opts, void *opaque)
1539 int ret;
1540 ret = qemu_fsdev_add(opts);
1542 return ret;
1544 #endif
1546 static int mon_init_func(QemuOpts *opts, void *opaque)
1548 CharDriverState *chr;
1549 const char *chardev;
1550 const char *mode;
1551 int flags;
1553 mode = qemu_opt_get(opts, "mode");
1554 if (mode == NULL) {
1555 mode = "readline";
1557 if (strcmp(mode, "readline") == 0) {
1558 flags = MONITOR_USE_READLINE;
1559 } else if (strcmp(mode, "control") == 0) {
1560 flags = MONITOR_USE_CONTROL;
1561 } else {
1562 fprintf(stderr, "unknown monitor mode \"%s\"\n", mode);
1563 exit(1);
1566 if (qemu_opt_get_bool(opts, "default", 0))
1567 flags |= MONITOR_IS_DEFAULT;
1569 chardev = qemu_opt_get(opts, "chardev");
1570 chr = qemu_chr_find(chardev);
1571 if (chr == NULL) {
1572 fprintf(stderr, "chardev \"%s\" not found\n", chardev);
1573 exit(1);
1576 monitor_init(chr, flags);
1577 return 0;
1580 static void monitor_parse(const char *optarg, const char *mode)
1582 static int monitor_device_index = 0;
1583 QemuOpts *opts;
1584 const char *p;
1585 char label[32];
1586 int def = 0;
1588 if (strstart(optarg, "chardev:", &p)) {
1589 snprintf(label, sizeof(label), "%s", p);
1590 } else {
1591 snprintf(label, sizeof(label), "compat_monitor%d",
1592 monitor_device_index);
1593 if (monitor_device_index == 0) {
1594 def = 1;
1596 opts = qemu_chr_parse_compat(label, optarg);
1597 if (!opts) {
1598 fprintf(stderr, "parse error: %s\n", optarg);
1599 exit(1);
1603 opts = qemu_opts_create(&qemu_mon_opts, label, 1);
1604 if (!opts) {
1605 fprintf(stderr, "duplicate chardev: %s\n", label);
1606 exit(1);
1608 qemu_opt_set(opts, "mode", mode);
1609 qemu_opt_set(opts, "chardev", label);
1610 if (def)
1611 qemu_opt_set(opts, "default", "on");
1612 monitor_device_index++;
1615 struct device_config {
1616 enum {
1617 DEV_USB, /* -usbdevice */
1618 DEV_BT, /* -bt */
1619 DEV_SERIAL, /* -serial */
1620 DEV_PARALLEL, /* -parallel */
1621 DEV_VIRTCON, /* -virtioconsole */
1622 DEV_DEBUGCON, /* -debugcon */
1623 } type;
1624 const char *cmdline;
1625 QTAILQ_ENTRY(device_config) next;
1627 QTAILQ_HEAD(, device_config) device_configs = QTAILQ_HEAD_INITIALIZER(device_configs);
1629 static void add_device_config(int type, const char *cmdline)
1631 struct device_config *conf;
1633 conf = qemu_mallocz(sizeof(*conf));
1634 conf->type = type;
1635 conf->cmdline = cmdline;
1636 QTAILQ_INSERT_TAIL(&device_configs, conf, next);
1639 static int foreach_device_config(int type, int (*func)(const char *cmdline))
1641 struct device_config *conf;
1642 int rc;
1644 QTAILQ_FOREACH(conf, &device_configs, next) {
1645 if (conf->type != type)
1646 continue;
1647 rc = func(conf->cmdline);
1648 if (0 != rc)
1649 return rc;
1651 return 0;
1654 static int serial_parse(const char *devname)
1656 static int index = 0;
1657 char label[32];
1659 if (strcmp(devname, "none") == 0)
1660 return 0;
1661 if (index == MAX_SERIAL_PORTS) {
1662 fprintf(stderr, "qemu: too many serial ports\n");
1663 exit(1);
1665 snprintf(label, sizeof(label), "serial%d", index);
1666 serial_hds[index] = qemu_chr_open(label, devname, NULL);
1667 if (!serial_hds[index]) {
1668 fprintf(stderr, "qemu: could not open serial device '%s': %s\n",
1669 devname, strerror(errno));
1670 return -1;
1672 index++;
1673 return 0;
1676 static int parallel_parse(const char *devname)
1678 static int index = 0;
1679 char label[32];
1681 if (strcmp(devname, "none") == 0)
1682 return 0;
1683 if (index == MAX_PARALLEL_PORTS) {
1684 fprintf(stderr, "qemu: too many parallel ports\n");
1685 exit(1);
1687 snprintf(label, sizeof(label), "parallel%d", index);
1688 parallel_hds[index] = qemu_chr_open(label, devname, NULL);
1689 if (!parallel_hds[index]) {
1690 fprintf(stderr, "qemu: could not open parallel device '%s': %s\n",
1691 devname, strerror(errno));
1692 return -1;
1694 index++;
1695 return 0;
1698 static int virtcon_parse(const char *devname)
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(&qemu_device_opts, NULL, 0);
1712 qemu_opt_set(bus_opts, "driver", "virtio-serial");
1714 dev_opts = qemu_opts_create(&qemu_device_opts, 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_device_opts, "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 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 /* first pass of option parsing */
1859 optind = 1;
1860 while (optind < argc) {
1861 if (argv[optind][0] != '-') {
1862 /* disk image */
1863 optind++;
1864 continue;
1865 } else {
1866 const QEMUOption *popt;
1868 popt = lookup_opt(argc, argv, &optarg, &optind);
1869 switch (popt->index) {
1870 case QEMU_OPTION_nodefconfig:
1871 defconfig=0;
1872 break;
1877 if (defconfig) {
1878 int ret;
1880 ret = qemu_read_config_file(CONFIG_QEMU_CONFDIR "/qemu.conf");
1881 if (ret < 0 && ret != -ENOENT) {
1882 exit(1);
1885 ret = qemu_read_config_file(arch_config_name);
1886 if (ret < 0 && ret != -ENOENT) {
1887 exit(1);
1890 cpudef_init();
1892 /* second pass of option parsing */
1893 optind = 1;
1894 for(;;) {
1895 if (optind >= argc)
1896 break;
1897 if (argv[optind][0] != '-') {
1898 hda_opts = drive_add(argv[optind++], HD_ALIAS, 0);
1899 } else {
1900 const QEMUOption *popt;
1902 popt = lookup_opt(argc, argv, &optarg, &optind);
1903 if (!(popt->arch_mask & arch_type)) {
1904 printf("Option %s not supported for this target\n", popt->name);
1905 exit(1);
1907 switch(popt->index) {
1908 case QEMU_OPTION_M:
1909 machine = find_machine(optarg);
1910 if (!machine) {
1911 QEMUMachine *m;
1912 printf("Supported machines are:\n");
1913 for(m = first_machine; m != NULL; m = m->next) {
1914 if (m->alias)
1915 printf("%-10s %s (alias of %s)\n",
1916 m->alias, m->desc, m->name);
1917 printf("%-10s %s%s\n",
1918 m->name, m->desc,
1919 m->is_default ? " (default)" : "");
1921 exit(*optarg != '?');
1923 break;
1924 case QEMU_OPTION_cpu:
1925 /* hw initialization will check this */
1926 if (*optarg == '?') {
1927 list_cpus(stdout, &fprintf, optarg);
1928 exit(0);
1929 } else {
1930 cpu_model = optarg;
1932 break;
1933 case QEMU_OPTION_initrd:
1934 initrd_filename = optarg;
1935 break;
1936 case QEMU_OPTION_hda:
1937 if (cyls == 0)
1938 hda_opts = drive_add(optarg, HD_ALIAS, 0);
1939 else
1940 hda_opts = drive_add(optarg, HD_ALIAS
1941 ",cyls=%d,heads=%d,secs=%d%s",
1942 0, cyls, heads, secs,
1943 translation == BIOS_ATA_TRANSLATION_LBA ?
1944 ",trans=lba" :
1945 translation == BIOS_ATA_TRANSLATION_NONE ?
1946 ",trans=none" : "");
1947 break;
1948 case QEMU_OPTION_hdb:
1949 case QEMU_OPTION_hdc:
1950 case QEMU_OPTION_hdd:
1951 drive_add(optarg, HD_ALIAS, popt->index - QEMU_OPTION_hda);
1952 break;
1953 case QEMU_OPTION_drive:
1954 drive_add(NULL, "%s", optarg);
1955 break;
1956 case QEMU_OPTION_set:
1957 if (qemu_set_option(optarg) != 0)
1958 exit(1);
1959 break;
1960 case QEMU_OPTION_global:
1961 if (qemu_global_option(optarg) != 0)
1962 exit(1);
1963 break;
1964 case QEMU_OPTION_mtdblock:
1965 drive_add(optarg, MTD_ALIAS);
1966 break;
1967 case QEMU_OPTION_sd:
1968 drive_add(optarg, SD_ALIAS);
1969 break;
1970 case QEMU_OPTION_pflash:
1971 drive_add(optarg, PFLASH_ALIAS);
1972 break;
1973 case QEMU_OPTION_snapshot:
1974 snapshot = 1;
1975 break;
1976 case QEMU_OPTION_hdachs:
1978 const char *p;
1979 p = optarg;
1980 cyls = strtol(p, (char **)&p, 0);
1981 if (cyls < 1 || cyls > 16383)
1982 goto chs_fail;
1983 if (*p != ',')
1984 goto chs_fail;
1985 p++;
1986 heads = strtol(p, (char **)&p, 0);
1987 if (heads < 1 || heads > 16)
1988 goto chs_fail;
1989 if (*p != ',')
1990 goto chs_fail;
1991 p++;
1992 secs = strtol(p, (char **)&p, 0);
1993 if (secs < 1 || secs > 63)
1994 goto chs_fail;
1995 if (*p == ',') {
1996 p++;
1997 if (!strcmp(p, "none"))
1998 translation = BIOS_ATA_TRANSLATION_NONE;
1999 else if (!strcmp(p, "lba"))
2000 translation = BIOS_ATA_TRANSLATION_LBA;
2001 else if (!strcmp(p, "auto"))
2002 translation = BIOS_ATA_TRANSLATION_AUTO;
2003 else
2004 goto chs_fail;
2005 } else if (*p != '\0') {
2006 chs_fail:
2007 fprintf(stderr, "qemu: invalid physical CHS format\n");
2008 exit(1);
2010 if (hda_opts != NULL) {
2011 char num[16];
2012 snprintf(num, sizeof(num), "%d", cyls);
2013 qemu_opt_set(hda_opts, "cyls", num);
2014 snprintf(num, sizeof(num), "%d", heads);
2015 qemu_opt_set(hda_opts, "heads", num);
2016 snprintf(num, sizeof(num), "%d", secs);
2017 qemu_opt_set(hda_opts, "secs", num);
2018 if (translation == BIOS_ATA_TRANSLATION_LBA)
2019 qemu_opt_set(hda_opts, "trans", "lba");
2020 if (translation == BIOS_ATA_TRANSLATION_NONE)
2021 qemu_opt_set(hda_opts, "trans", "none");
2024 break;
2025 case QEMU_OPTION_numa:
2026 if (nb_numa_nodes >= MAX_NODES) {
2027 fprintf(stderr, "qemu: too many NUMA nodes\n");
2028 exit(1);
2030 numa_add(optarg);
2031 break;
2032 case QEMU_OPTION_nographic:
2033 display_type = DT_NOGRAPHIC;
2034 break;
2035 #ifdef CONFIG_CURSES
2036 case QEMU_OPTION_curses:
2037 display_type = DT_CURSES;
2038 break;
2039 #endif
2040 case QEMU_OPTION_portrait:
2041 graphic_rotate = 1;
2042 break;
2043 case QEMU_OPTION_kernel:
2044 kernel_filename = optarg;
2045 break;
2046 case QEMU_OPTION_append:
2047 kernel_cmdline = optarg;
2048 break;
2049 case QEMU_OPTION_cdrom:
2050 drive_add(optarg, CDROM_ALIAS);
2051 break;
2052 case QEMU_OPTION_boot:
2054 static const char * const params[] = {
2055 "order", "once", "menu", NULL
2057 char buf[sizeof(boot_devices)];
2058 char *standard_boot_devices;
2059 int legacy = 0;
2061 if (!strchr(optarg, '=')) {
2062 legacy = 1;
2063 pstrcpy(buf, sizeof(buf), optarg);
2064 } else if (check_params(buf, sizeof(buf), params, optarg) < 0) {
2065 fprintf(stderr,
2066 "qemu: unknown boot parameter '%s' in '%s'\n",
2067 buf, optarg);
2068 exit(1);
2071 if (legacy ||
2072 get_param_value(buf, sizeof(buf), "order", optarg)) {
2073 validate_bootdevices(buf);
2074 pstrcpy(boot_devices, sizeof(boot_devices), buf);
2076 if (!legacy) {
2077 if (get_param_value(buf, sizeof(buf),
2078 "once", optarg)) {
2079 validate_bootdevices(buf);
2080 standard_boot_devices = qemu_strdup(boot_devices);
2081 pstrcpy(boot_devices, sizeof(boot_devices), buf);
2082 qemu_register_reset(restore_boot_devices,
2083 standard_boot_devices);
2085 if (get_param_value(buf, sizeof(buf),
2086 "menu", optarg)) {
2087 if (!strcmp(buf, "on")) {
2088 boot_menu = 1;
2089 } else if (!strcmp(buf, "off")) {
2090 boot_menu = 0;
2091 } else {
2092 fprintf(stderr,
2093 "qemu: invalid option value '%s'\n",
2094 buf);
2095 exit(1);
2100 break;
2101 case QEMU_OPTION_fda:
2102 case QEMU_OPTION_fdb:
2103 drive_add(optarg, FD_ALIAS, popt->index - QEMU_OPTION_fda);
2104 break;
2105 case QEMU_OPTION_no_fd_bootchk:
2106 fd_bootchk = 0;
2107 break;
2108 case QEMU_OPTION_netdev:
2109 if (net_client_parse(&qemu_netdev_opts, optarg) == -1) {
2110 exit(1);
2112 break;
2113 case QEMU_OPTION_net:
2114 if (net_client_parse(&qemu_net_opts, optarg) == -1) {
2115 exit(1);
2117 break;
2118 #ifdef CONFIG_SLIRP
2119 case QEMU_OPTION_tftp:
2120 legacy_tftp_prefix = optarg;
2121 break;
2122 case QEMU_OPTION_bootp:
2123 legacy_bootp_filename = optarg;
2124 break;
2125 case QEMU_OPTION_redir:
2126 if (net_slirp_redir(optarg) < 0)
2127 exit(1);
2128 break;
2129 #endif
2130 case QEMU_OPTION_bt:
2131 add_device_config(DEV_BT, optarg);
2132 break;
2133 case QEMU_OPTION_audio_help:
2134 if (!(audio_available())) {
2135 printf("Option %s not supported for this target\n", popt->name);
2136 exit(1);
2138 AUD_help ();
2139 exit (0);
2140 break;
2141 case QEMU_OPTION_soundhw:
2142 if (!(audio_available())) {
2143 printf("Option %s not supported for this target\n", popt->name);
2144 exit(1);
2146 select_soundhw (optarg);
2147 break;
2148 case QEMU_OPTION_h:
2149 help(0);
2150 break;
2151 case QEMU_OPTION_version:
2152 version();
2153 exit(0);
2154 break;
2155 case QEMU_OPTION_m: {
2156 uint64_t value;
2157 char *ptr;
2159 value = strtoul(optarg, &ptr, 10);
2160 switch (*ptr) {
2161 case 0: case 'M': case 'm':
2162 value <<= 20;
2163 break;
2164 case 'G': case 'g':
2165 value <<= 30;
2166 break;
2167 default:
2168 fprintf(stderr, "qemu: invalid ram size: %s\n", optarg);
2169 exit(1);
2172 /* On 32-bit hosts, QEMU is limited by virtual address space */
2173 if (value > (2047 << 20) && HOST_LONG_BITS == 32) {
2174 fprintf(stderr, "qemu: at most 2047 MB RAM can be simulated\n");
2175 exit(1);
2177 if (value != (uint64_t)(ram_addr_t)value) {
2178 fprintf(stderr, "qemu: ram size too large\n");
2179 exit(1);
2181 ram_size = value;
2182 break;
2184 case QEMU_OPTION_mempath:
2185 mem_path = optarg;
2186 break;
2187 #ifdef MAP_POPULATE
2188 case QEMU_OPTION_mem_prealloc:
2189 mem_prealloc = 1;
2190 break;
2191 #endif
2192 case QEMU_OPTION_d:
2193 set_cpu_log(optarg);
2194 break;
2195 case QEMU_OPTION_s:
2196 gdbstub_dev = "tcp::" DEFAULT_GDBSTUB_PORT;
2197 break;
2198 case QEMU_OPTION_gdb:
2199 gdbstub_dev = optarg;
2200 break;
2201 case QEMU_OPTION_L:
2202 data_dir = optarg;
2203 break;
2204 case QEMU_OPTION_bios:
2205 bios_name = optarg;
2206 break;
2207 case QEMU_OPTION_singlestep:
2208 singlestep = 1;
2209 break;
2210 case QEMU_OPTION_S:
2211 autostart = 0;
2212 break;
2213 case QEMU_OPTION_k:
2214 keyboard_layout = optarg;
2215 break;
2216 case QEMU_OPTION_localtime:
2217 rtc_utc = 0;
2218 break;
2219 case QEMU_OPTION_vga:
2220 select_vgahw (optarg);
2221 break;
2222 case QEMU_OPTION_g:
2224 const char *p;
2225 int w, h, depth;
2226 p = optarg;
2227 w = strtol(p, (char **)&p, 10);
2228 if (w <= 0) {
2229 graphic_error:
2230 fprintf(stderr, "qemu: invalid resolution or depth\n");
2231 exit(1);
2233 if (*p != 'x')
2234 goto graphic_error;
2235 p++;
2236 h = strtol(p, (char **)&p, 10);
2237 if (h <= 0)
2238 goto graphic_error;
2239 if (*p == 'x') {
2240 p++;
2241 depth = strtol(p, (char **)&p, 10);
2242 if (depth != 8 && depth != 15 && depth != 16 &&
2243 depth != 24 && depth != 32)
2244 goto graphic_error;
2245 } else if (*p == '\0') {
2246 depth = graphic_depth;
2247 } else {
2248 goto graphic_error;
2251 graphic_width = w;
2252 graphic_height = h;
2253 graphic_depth = depth;
2255 break;
2256 case QEMU_OPTION_echr:
2258 char *r;
2259 term_escape_char = strtol(optarg, &r, 0);
2260 if (r == optarg)
2261 printf("Bad argument to echr\n");
2262 break;
2264 case QEMU_OPTION_monitor:
2265 monitor_parse(optarg, "readline");
2266 default_monitor = 0;
2267 break;
2268 case QEMU_OPTION_qmp:
2269 monitor_parse(optarg, "control");
2270 default_monitor = 0;
2271 break;
2272 case QEMU_OPTION_mon:
2273 opts = qemu_opts_parse(&qemu_mon_opts, optarg, 1);
2274 if (!opts) {
2275 exit(1);
2277 default_monitor = 0;
2278 break;
2279 case QEMU_OPTION_chardev:
2280 opts = qemu_opts_parse(&qemu_chardev_opts, optarg, 1);
2281 if (!opts) {
2282 exit(1);
2284 break;
2285 #ifdef CONFIG_VIRTFS
2286 case QEMU_OPTION_fsdev:
2287 opts = qemu_opts_parse(&qemu_fsdev_opts, optarg, 1);
2288 if (!opts) {
2289 fprintf(stderr, "parse error: %s\n", optarg);
2290 exit(1);
2292 break;
2293 case QEMU_OPTION_virtfs: {
2294 char *arg_fsdev = NULL;
2295 char *arg_9p = NULL;
2296 int len = 0;
2298 opts = qemu_opts_parse(&qemu_virtfs_opts, optarg, 1);
2299 if (!opts) {
2300 fprintf(stderr, "parse error: %s\n", optarg);
2301 exit(1);
2304 if (qemu_opt_get(opts, "fstype") == NULL ||
2305 qemu_opt_get(opts, "mount_tag") == NULL ||
2306 qemu_opt_get(opts, "path") == NULL ||
2307 qemu_opt_get(opts, "security_model") == NULL) {
2308 fprintf(stderr, "Usage: -virtfs fstype,path=/share_path/,"
2309 "security_model=[mapped|passthrough],"
2310 "mnt_tag=tag.\n");
2311 exit(1);
2314 len = strlen(",id=,path=,security_model=");
2315 len += strlen(qemu_opt_get(opts, "fstype"));
2316 len += strlen(qemu_opt_get(opts, "mount_tag"));
2317 len += strlen(qemu_opt_get(opts, "path"));
2318 len += strlen(qemu_opt_get(opts, "security_model"));
2319 arg_fsdev = qemu_malloc((len + 1) * sizeof(*arg_fsdev));
2321 if (!arg_fsdev) {
2322 fprintf(stderr, "No memory to parse -fsdev for %s\n",
2323 optarg);
2324 exit(1);
2327 sprintf(arg_fsdev, "%s,id=%s,path=%s,security_model=%s",
2328 qemu_opt_get(opts, "fstype"),
2329 qemu_opt_get(opts, "mount_tag"),
2330 qemu_opt_get(opts, "path"),
2331 qemu_opt_get(opts, "security_model"));
2333 len = strlen("virtio-9p-pci,fsdev=,mount_tag=");
2334 len += 2*strlen(qemu_opt_get(opts, "mount_tag"));
2335 arg_9p = qemu_malloc((len + 1) * sizeof(*arg_9p));
2337 if (!arg_9p) {
2338 fprintf(stderr, "No memory to parse -device for %s\n",
2339 optarg);
2340 exit(1);
2343 sprintf(arg_9p, "virtio-9p-pci,fsdev=%s,mount_tag=%s",
2344 qemu_opt_get(opts, "mount_tag"),
2345 qemu_opt_get(opts, "mount_tag"));
2347 if (!qemu_opts_parse(&qemu_fsdev_opts, arg_fsdev, 1)) {
2348 fprintf(stderr, "parse error [fsdev]: %s\n", optarg);
2349 exit(1);
2352 if (!qemu_opts_parse(&qemu_device_opts, arg_9p, 1)) {
2353 fprintf(stderr, "parse error [device]: %s\n", optarg);
2354 exit(1);
2357 qemu_free(arg_fsdev);
2358 qemu_free(arg_9p);
2359 break;
2361 #endif
2362 case QEMU_OPTION_serial:
2363 add_device_config(DEV_SERIAL, optarg);
2364 default_serial = 0;
2365 if (strncmp(optarg, "mon:", 4) == 0) {
2366 default_monitor = 0;
2368 break;
2369 case QEMU_OPTION_watchdog:
2370 if (watchdog) {
2371 fprintf(stderr,
2372 "qemu: only one watchdog option may be given\n");
2373 return 1;
2375 watchdog = optarg;
2376 break;
2377 case QEMU_OPTION_watchdog_action:
2378 if (select_watchdog_action(optarg) == -1) {
2379 fprintf(stderr, "Unknown -watchdog-action parameter\n");
2380 exit(1);
2382 break;
2383 case QEMU_OPTION_virtiocon:
2384 add_device_config(DEV_VIRTCON, optarg);
2385 default_virtcon = 0;
2386 if (strncmp(optarg, "mon:", 4) == 0) {
2387 default_monitor = 0;
2389 break;
2390 case QEMU_OPTION_parallel:
2391 add_device_config(DEV_PARALLEL, optarg);
2392 default_parallel = 0;
2393 if (strncmp(optarg, "mon:", 4) == 0) {
2394 default_monitor = 0;
2396 break;
2397 case QEMU_OPTION_debugcon:
2398 add_device_config(DEV_DEBUGCON, optarg);
2399 break;
2400 case QEMU_OPTION_loadvm:
2401 loadvm = optarg;
2402 break;
2403 case QEMU_OPTION_full_screen:
2404 full_screen = 1;
2405 break;
2406 #ifdef CONFIG_SDL
2407 case QEMU_OPTION_no_frame:
2408 no_frame = 1;
2409 break;
2410 case QEMU_OPTION_alt_grab:
2411 alt_grab = 1;
2412 break;
2413 case QEMU_OPTION_ctrl_grab:
2414 ctrl_grab = 1;
2415 break;
2416 case QEMU_OPTION_no_quit:
2417 no_quit = 1;
2418 break;
2419 case QEMU_OPTION_sdl:
2420 display_type = DT_SDL;
2421 break;
2422 #endif
2423 case QEMU_OPTION_pidfile:
2424 pid_file = optarg;
2425 break;
2426 case QEMU_OPTION_win2k_hack:
2427 win2k_install_hack = 1;
2428 break;
2429 case QEMU_OPTION_rtc_td_hack:
2430 rtc_td_hack = 1;
2431 break;
2432 case QEMU_OPTION_acpitable:
2433 do_acpitable_option(optarg);
2434 break;
2435 case QEMU_OPTION_smbios:
2436 do_smbios_option(optarg);
2437 break;
2438 case QEMU_OPTION_enable_kvm:
2439 kvm_allowed = 1;
2440 break;
2441 case QEMU_OPTION_usb:
2442 usb_enabled = 1;
2443 break;
2444 case QEMU_OPTION_usbdevice:
2445 usb_enabled = 1;
2446 add_device_config(DEV_USB, optarg);
2447 break;
2448 case QEMU_OPTION_device:
2449 if (!qemu_opts_parse(&qemu_device_opts, optarg, 1)) {
2450 exit(1);
2452 break;
2453 case QEMU_OPTION_smp:
2454 smp_parse(optarg);
2455 if (smp_cpus < 1) {
2456 fprintf(stderr, "Invalid number of CPUs\n");
2457 exit(1);
2459 if (max_cpus < smp_cpus) {
2460 fprintf(stderr, "maxcpus must be equal to or greater than "
2461 "smp\n");
2462 exit(1);
2464 if (max_cpus > 255) {
2465 fprintf(stderr, "Unsupported number of maxcpus\n");
2466 exit(1);
2468 break;
2469 case QEMU_OPTION_vnc:
2470 display_type = DT_VNC;
2471 vnc_display = optarg;
2472 break;
2473 case QEMU_OPTION_no_acpi:
2474 acpi_enabled = 0;
2475 break;
2476 case QEMU_OPTION_no_hpet:
2477 no_hpet = 1;
2478 break;
2479 case QEMU_OPTION_balloon:
2480 if (balloon_parse(optarg) < 0) {
2481 fprintf(stderr, "Unknown -balloon argument %s\n", optarg);
2482 exit(1);
2484 break;
2485 case QEMU_OPTION_no_reboot:
2486 no_reboot = 1;
2487 break;
2488 case QEMU_OPTION_no_shutdown:
2489 no_shutdown = 1;
2490 break;
2491 case QEMU_OPTION_show_cursor:
2492 cursor_hide = 0;
2493 break;
2494 case QEMU_OPTION_uuid:
2495 if(qemu_uuid_parse(optarg, qemu_uuid) < 0) {
2496 fprintf(stderr, "Fail to parse UUID string."
2497 " Wrong format.\n");
2498 exit(1);
2500 break;
2501 case QEMU_OPTION_option_rom:
2502 if (nb_option_roms >= MAX_OPTION_ROMS) {
2503 fprintf(stderr, "Too many option ROMs\n");
2504 exit(1);
2506 option_rom[nb_option_roms] = optarg;
2507 nb_option_roms++;
2508 break;
2509 case QEMU_OPTION_semihosting:
2510 semihosting_enabled = 1;
2511 break;
2512 case QEMU_OPTION_name:
2513 qemu_name = qemu_strdup(optarg);
2515 char *p = strchr(qemu_name, ',');
2516 if (p != NULL) {
2517 *p++ = 0;
2518 if (strncmp(p, "process=", 8)) {
2519 fprintf(stderr, "Unknown subargument %s to -name", p);
2520 exit(1);
2522 p += 8;
2523 os_set_proc_name(p);
2526 break;
2527 case QEMU_OPTION_prom_env:
2528 if (nb_prom_envs >= MAX_PROM_ENVS) {
2529 fprintf(stderr, "Too many prom variables\n");
2530 exit(1);
2532 prom_envs[nb_prom_envs] = optarg;
2533 nb_prom_envs++;
2534 break;
2535 case QEMU_OPTION_old_param:
2536 old_param = 1;
2537 break;
2538 case QEMU_OPTION_clock:
2539 configure_alarms(optarg);
2540 break;
2541 case QEMU_OPTION_startdate:
2542 configure_rtc_date_offset(optarg, 1);
2543 break;
2544 case QEMU_OPTION_rtc:
2545 opts = qemu_opts_parse(&qemu_rtc_opts, optarg, 0);
2546 if (!opts) {
2547 exit(1);
2549 configure_rtc(opts);
2550 break;
2551 case QEMU_OPTION_tb_size:
2552 tb_size = strtol(optarg, NULL, 0);
2553 if (tb_size < 0)
2554 tb_size = 0;
2555 break;
2556 case QEMU_OPTION_icount:
2557 icount_option = optarg;
2558 break;
2559 case QEMU_OPTION_incoming:
2560 incoming = optarg;
2561 incoming_expected = true;
2562 break;
2563 case QEMU_OPTION_nodefaults:
2564 default_serial = 0;
2565 default_parallel = 0;
2566 default_virtcon = 0;
2567 default_monitor = 0;
2568 default_vga = 0;
2569 default_net = 0;
2570 default_floppy = 0;
2571 default_cdrom = 0;
2572 default_sdcard = 0;
2573 break;
2574 case QEMU_OPTION_xen_domid:
2575 if (!(xen_available())) {
2576 printf("Option %s not supported for this target\n", popt->name);
2577 exit(1);
2579 xen_domid = atoi(optarg);
2580 break;
2581 case QEMU_OPTION_xen_create:
2582 if (!(xen_available())) {
2583 printf("Option %s not supported for this target\n", popt->name);
2584 exit(1);
2586 xen_mode = XEN_CREATE;
2587 break;
2588 case QEMU_OPTION_xen_attach:
2589 if (!(xen_available())) {
2590 printf("Option %s not supported for this target\n", popt->name);
2591 exit(1);
2593 xen_mode = XEN_ATTACH;
2594 break;
2595 case QEMU_OPTION_readconfig:
2597 int ret = qemu_read_config_file(optarg);
2598 if (ret < 0) {
2599 fprintf(stderr, "read config %s: %s\n", optarg,
2600 strerror(-ret));
2601 exit(1);
2603 break;
2605 case QEMU_OPTION_writeconfig:
2607 FILE *fp;
2608 if (strcmp(optarg, "-") == 0) {
2609 fp = stdout;
2610 } else {
2611 fp = fopen(optarg, "w");
2612 if (fp == NULL) {
2613 fprintf(stderr, "open %s: %s\n", optarg, strerror(errno));
2614 exit(1);
2617 qemu_config_write(fp);
2618 fclose(fp);
2619 break;
2621 default:
2622 os_parse_cmd_args(popt->index, optarg);
2626 loc_set_none();
2628 /* If no data_dir is specified then try to find it relative to the
2629 executable path. */
2630 if (!data_dir) {
2631 data_dir = os_find_datadir(argv[0]);
2633 /* If all else fails use the install patch specified when building. */
2634 if (!data_dir) {
2635 data_dir = CONFIG_QEMU_DATADIR;
2639 * Default to max_cpus = smp_cpus, in case the user doesn't
2640 * specify a max_cpus value.
2642 if (!max_cpus)
2643 max_cpus = smp_cpus;
2645 machine->max_cpus = machine->max_cpus ?: 1; /* Default to UP */
2646 if (smp_cpus > machine->max_cpus) {
2647 fprintf(stderr, "Number of SMP cpus requested (%d), exceeds max cpus "
2648 "supported by machine `%s' (%d)\n", smp_cpus, machine->name,
2649 machine->max_cpus);
2650 exit(1);
2653 qemu_opts_foreach(&qemu_device_opts, default_driver_check, NULL, 0);
2654 qemu_opts_foreach(&qemu_global_opts, default_driver_check, NULL, 0);
2656 if (machine->no_serial) {
2657 default_serial = 0;
2659 if (machine->no_parallel) {
2660 default_parallel = 0;
2662 if (!machine->use_virtcon) {
2663 default_virtcon = 0;
2665 if (machine->no_vga) {
2666 default_vga = 0;
2668 if (machine->no_floppy) {
2669 default_floppy = 0;
2671 if (machine->no_cdrom) {
2672 default_cdrom = 0;
2674 if (machine->no_sdcard) {
2675 default_sdcard = 0;
2678 if (display_type == DT_NOGRAPHIC) {
2679 if (default_parallel)
2680 add_device_config(DEV_PARALLEL, "null");
2681 if (default_serial && default_monitor) {
2682 add_device_config(DEV_SERIAL, "mon:stdio");
2683 } else if (default_virtcon && default_monitor) {
2684 add_device_config(DEV_VIRTCON, "mon:stdio");
2685 } else {
2686 if (default_serial)
2687 add_device_config(DEV_SERIAL, "stdio");
2688 if (default_virtcon)
2689 add_device_config(DEV_VIRTCON, "stdio");
2690 if (default_monitor)
2691 monitor_parse("stdio", "readline");
2693 } else {
2694 if (default_serial)
2695 add_device_config(DEV_SERIAL, "vc:80Cx24C");
2696 if (default_parallel)
2697 add_device_config(DEV_PARALLEL, "vc:80Cx24C");
2698 if (default_monitor)
2699 monitor_parse("vc:80Cx24C", "readline");
2700 if (default_virtcon)
2701 add_device_config(DEV_VIRTCON, "vc:80Cx24C");
2703 if (default_vga)
2704 vga_interface_type = VGA_CIRRUS;
2706 socket_init();
2708 if (qemu_opts_foreach(&qemu_chardev_opts, chardev_init_func, NULL, 1) != 0)
2709 exit(1);
2710 #ifdef CONFIG_VIRTFS
2711 if (qemu_opts_foreach(&qemu_fsdev_opts, fsdev_init_func, NULL, 1) != 0) {
2712 exit(1);
2714 #endif
2716 os_daemonize();
2718 if (pid_file && qemu_create_pidfile(pid_file) != 0) {
2719 os_pidfile_error();
2720 exit(1);
2723 if (kvm_allowed) {
2724 int ret = kvm_init(smp_cpus);
2725 if (ret < 0) {
2726 if (!kvm_available()) {
2727 printf("KVM not supported for this target\n");
2728 } else {
2729 fprintf(stderr, "failed to initialize KVM: %s\n", strerror(-ret));
2731 exit(1);
2735 if (qemu_init_main_loop()) {
2736 fprintf(stderr, "qemu_init_main_loop failed\n");
2737 exit(1);
2739 linux_boot = (kernel_filename != NULL);
2741 if (!linux_boot && *kernel_cmdline != '\0') {
2742 fprintf(stderr, "-append only allowed with -kernel option\n");
2743 exit(1);
2746 if (!linux_boot && initrd_filename != NULL) {
2747 fprintf(stderr, "-initrd only allowed with -kernel option\n");
2748 exit(1);
2751 os_set_line_buffering();
2753 if (init_timer_alarm() < 0) {
2754 fprintf(stderr, "could not initialize alarm timer\n");
2755 exit(1);
2757 configure_icount(icount_option);
2759 if (net_init_clients() < 0) {
2760 exit(1);
2763 /* init the bluetooth world */
2764 if (foreach_device_config(DEV_BT, bt_parse))
2765 exit(1);
2767 /* init the memory */
2768 if (ram_size == 0)
2769 ram_size = DEFAULT_RAM_SIZE * 1024 * 1024;
2771 /* init the dynamic translator */
2772 cpu_exec_init_all(tb_size * 1024 * 1024);
2774 bdrv_init_with_whitelist();
2776 blk_mig_init();
2778 if (default_cdrom) {
2779 /* we always create the cdrom drive, even if no disk is there */
2780 drive_add(NULL, CDROM_ALIAS);
2783 if (default_floppy) {
2784 /* we always create at least one floppy */
2785 drive_add(NULL, FD_ALIAS, 0);
2788 if (default_sdcard) {
2789 /* we always create one sd slot, even if no card is in it */
2790 drive_add(NULL, SD_ALIAS);
2793 /* open the virtual block devices */
2794 if (snapshot)
2795 qemu_opts_foreach(&qemu_drive_opts, drive_enable_snapshot, NULL, 0);
2796 if (qemu_opts_foreach(&qemu_drive_opts, drive_init_func, &machine->use_scsi, 1) != 0)
2797 exit(1);
2799 register_savevm_live(NULL, "ram", 0, 4, NULL, ram_save_live, NULL,
2800 ram_load, NULL);
2802 if (nb_numa_nodes > 0) {
2803 int i;
2805 if (nb_numa_nodes > smp_cpus) {
2806 nb_numa_nodes = smp_cpus;
2809 /* If no memory size if given for any node, assume the default case
2810 * and distribute the available memory equally across all nodes
2812 for (i = 0; i < nb_numa_nodes; i++) {
2813 if (node_mem[i] != 0)
2814 break;
2816 if (i == nb_numa_nodes) {
2817 uint64_t usedmem = 0;
2819 /* On Linux, the each node's border has to be 8MB aligned,
2820 * the final node gets the rest.
2822 for (i = 0; i < nb_numa_nodes - 1; i++) {
2823 node_mem[i] = (ram_size / nb_numa_nodes) & ~((1 << 23UL) - 1);
2824 usedmem += node_mem[i];
2826 node_mem[i] = ram_size - usedmem;
2829 for (i = 0; i < nb_numa_nodes; i++) {
2830 if (node_cpumask[i] != 0)
2831 break;
2833 /* assigning the VCPUs round-robin is easier to implement, guest OSes
2834 * must cope with this anyway, because there are BIOSes out there in
2835 * real machines which also use this scheme.
2837 if (i == nb_numa_nodes) {
2838 for (i = 0; i < smp_cpus; i++) {
2839 node_cpumask[i % nb_numa_nodes] |= 1 << i;
2844 if (qemu_opts_foreach(&qemu_mon_opts, mon_init_func, NULL, 1) != 0) {
2845 exit(1);
2848 if (foreach_device_config(DEV_SERIAL, serial_parse) < 0)
2849 exit(1);
2850 if (foreach_device_config(DEV_PARALLEL, parallel_parse) < 0)
2851 exit(1);
2852 if (foreach_device_config(DEV_VIRTCON, virtcon_parse) < 0)
2853 exit(1);
2854 if (foreach_device_config(DEV_DEBUGCON, debugcon_parse) < 0)
2855 exit(1);
2857 module_call_init(MODULE_INIT_DEVICE);
2859 if (qemu_opts_foreach(&qemu_device_opts, device_help_func, NULL, 0) != 0)
2860 exit(0);
2862 if (watchdog) {
2863 i = select_watchdog(watchdog);
2864 if (i > 0)
2865 exit (i == 1 ? 1 : 0);
2868 if (machine->compat_props) {
2869 qdev_prop_register_global_list(machine->compat_props);
2871 qemu_add_globals();
2873 machine->init(ram_size, boot_devices,
2874 kernel_filename, kernel_cmdline, initrd_filename, cpu_model);
2876 cpu_synchronize_all_post_init();
2878 /* must be after terminal init, SDL library changes signal handlers */
2879 os_setup_signal_handling();
2881 set_numa_modes();
2883 current_machine = machine;
2885 /* init USB devices */
2886 if (usb_enabled) {
2887 if (foreach_device_config(DEV_USB, usb_parse) < 0)
2888 exit(1);
2891 /* init generic devices */
2892 if (qemu_opts_foreach(&qemu_device_opts, device_init_func, NULL, 1) != 0)
2893 exit(1);
2895 net_check_clients();
2897 /* just use the first displaystate for the moment */
2898 ds = get_displaystate();
2900 if (display_type == DT_DEFAULT) {
2901 #if defined(CONFIG_SDL) || defined(CONFIG_COCOA)
2902 display_type = DT_SDL;
2903 #else
2904 display_type = DT_VNC;
2905 vnc_display = "localhost:0,to=99";
2906 show_vnc_port = 1;
2907 #endif
2911 switch (display_type) {
2912 case DT_NOGRAPHIC:
2913 break;
2914 #if defined(CONFIG_CURSES)
2915 case DT_CURSES:
2916 curses_display_init(ds, full_screen);
2917 break;
2918 #endif
2919 #if defined(CONFIG_SDL)
2920 case DT_SDL:
2921 sdl_display_init(ds, full_screen, no_frame);
2922 break;
2923 #elif defined(CONFIG_COCOA)
2924 case DT_SDL:
2925 cocoa_display_init(ds, full_screen);
2926 break;
2927 #endif
2928 case DT_VNC:
2929 vnc_display_init(ds);
2930 if (vnc_display_open(ds, vnc_display) < 0)
2931 exit(1);
2933 if (show_vnc_port) {
2934 printf("VNC server running on `%s'\n", vnc_display_local_addr(ds));
2936 break;
2937 default:
2938 break;
2940 dpy_resize(ds);
2942 dcl = ds->listeners;
2943 while (dcl != NULL) {
2944 if (dcl->dpy_refresh != NULL) {
2945 ds->gui_timer = qemu_new_timer(rt_clock, gui_update, ds);
2946 qemu_mod_timer(ds->gui_timer, qemu_get_clock(rt_clock));
2947 break;
2949 dcl = dcl->next;
2952 if (display_type == DT_NOGRAPHIC || display_type == DT_VNC) {
2953 nographic_timer = qemu_new_timer(rt_clock, nographic_update, NULL);
2954 qemu_mod_timer(nographic_timer, qemu_get_clock(rt_clock));
2957 text_consoles_set_display(ds);
2959 if (gdbstub_dev && gdbserver_start(gdbstub_dev) < 0) {
2960 fprintf(stderr, "qemu: could not open gdbserver on device '%s'\n",
2961 gdbstub_dev);
2962 exit(1);
2965 qdev_machine_creation_done();
2967 if (rom_load_all() != 0) {
2968 fprintf(stderr, "rom loading failed\n");
2969 exit(1);
2972 qemu_system_reset();
2973 if (loadvm) {
2974 if (load_vmstate(loadvm) < 0) {
2975 autostart = 0;
2979 if (incoming) {
2980 int ret = qemu_start_incoming_migration(incoming);
2981 if (ret < 0) {
2982 fprintf(stderr, "Migration failed. Exit code %s(%d), exiting.\n",
2983 incoming, ret);
2984 exit(ret);
2986 } else if (autostart) {
2987 vm_start();
2990 os_setup_post();
2992 main_loop();
2993 quit_timers();
2994 net_cleanup();
2996 return 0;