Merge commit '6e15cb5f6d87d0039b4c1590d1577ae14db17b50' into upstream-merge
[qemu-kvm/stefanha.git] / vl.c
blobbafc61f6b476402f10b870785ee5050585e601d4
1 /*
2 * QEMU System Emulator
4 * Copyright (c) 2003-2008 Fabrice Bellard
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 * THE SOFTWARE.
24 #include <unistd.h>
25 #include <fcntl.h>
26 #include <signal.h>
27 #include <time.h>
28 #include <errno.h>
29 #include <sys/time.h>
30 #include <zlib.h>
32 /* Needed early for CONFIG_BSD etc. */
33 #include "config-host.h"
35 #ifndef _WIN32
36 #include <libgen.h>
37 #include <sys/times.h>
38 #include <sys/wait.h>
39 #include <termios.h>
40 #include <sys/mman.h>
41 #include <sys/ioctl.h>
42 #include <sys/resource.h>
43 #include <sys/socket.h>
44 #include <netinet/in.h>
45 #include <net/if.h>
46 #include <arpa/inet.h>
47 #include <dirent.h>
48 #include <netdb.h>
49 #include <sys/select.h>
50 #ifdef CONFIG_SIMPLE_TRACE
51 #include "trace.h"
52 #endif
54 #ifdef CONFIG_BSD
55 #include <sys/stat.h>
56 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__DragonFly__)
57 #include <libutil.h>
58 #include <sys/sysctl.h>
59 #else
60 #include <util.h>
61 #endif
62 #else
63 #ifdef __linux__
64 #include <pty.h>
65 #include <malloc.h>
67 #include <linux/ppdev.h>
68 #include <linux/parport.h>
69 #endif
70 #ifdef __sun__
71 #include <sys/stat.h>
72 #include <sys/ethernet.h>
73 #include <sys/sockio.h>
74 #include <netinet/arp.h>
75 #include <netinet/in_systm.h>
76 #include <netinet/ip.h>
77 #include <netinet/ip_icmp.h> // must come after ip.h
78 #include <netinet/udp.h>
79 #include <netinet/tcp.h>
80 #include <net/if.h>
81 #include <syslog.h>
82 #include <stropts.h>
83 /* See MySQL bug #7156 (http://bugs.mysql.com/bug.php?id=7156) for
84 discussion about Solaris header problems */
85 extern int madvise(caddr_t, size_t, int);
86 #endif
87 #endif
88 #endif
90 #if defined(__OpenBSD__)
91 #include <util.h>
92 #endif
94 #if defined(CONFIG_VDE)
95 #include <libvdeplug.h>
96 #endif
98 #ifdef _WIN32
99 #include <windows.h>
100 #endif
102 #ifdef CONFIG_SDL
103 #if defined(__APPLE__) || defined(main)
104 #include <SDL.h>
105 int qemu_main(int argc, char **argv, char **envp);
106 int main(int argc, char **argv)
108 return qemu_main(argc, argv, NULL);
110 #undef main
111 #define main qemu_main
112 #endif
113 #endif /* CONFIG_SDL */
115 #ifdef CONFIG_COCOA
116 #undef main
117 #define main qemu_main
118 #endif /* CONFIG_COCOA */
120 #include "hw/hw.h"
121 #include "hw/boards.h"
122 #include "hw/usb.h"
123 #include "hw/pcmcia.h"
124 #include "hw/pc.h"
125 #include "hw/isa.h"
126 #include "hw/baum.h"
127 #include "hw/bt.h"
128 #include "hw/watchdog.h"
129 #include "hw/smbios.h"
130 #include "hw/xen.h"
131 #include "hw/qdev.h"
132 #include "hw/loader.h"
133 #include "bt-host.h"
134 #include "net.h"
135 #include "net/slirp.h"
136 #include "monitor.h"
137 #include "console.h"
138 #include "sysemu.h"
139 #include "gdbstub.h"
140 #include "qemu-timer.h"
141 #include "qemu-char.h"
142 #include "cache-utils.h"
143 #include "block.h"
144 #include "blockdev.h"
145 #include "block-migration.h"
146 #include "dma.h"
147 #include "audio/audio.h"
148 #include "migration.h"
149 #include "kvm.h"
150 #include "qemu-option.h"
151 #include "qemu-config.h"
152 #include "qemu-objects.h"
153 #include "qemu-options.h"
154 #include "hw/device-assignment.h"
155 #ifdef CONFIG_VIRTFS
156 #include "fsdev/qemu-fsdev.h"
157 #endif
159 #include "disas.h"
161 #include "qemu_socket.h"
163 #include "slirp/libslirp.h"
165 #include "qemu-queue.h"
166 #include "cpus.h"
167 #include "arch_init.h"
169 //#define DEBUG_NET
170 //#define DEBUG_SLIRP
172 #define DEFAULT_RAM_SIZE 128
174 #define MAX_VIRTIO_CONSOLES 1
176 static const char *data_dir;
177 const char *bios_name = NULL;
178 enum vga_retrace_method vga_retrace_method = VGA_RETRACE_DUMB;
179 DisplayType display_type = DT_DEFAULT;
180 const char* keyboard_layout = NULL;
181 ram_addr_t ram_size;
182 const char *mem_path = NULL;
183 #ifdef MAP_POPULATE
184 int mem_prealloc = 0; /* force preallocation of physical target memory */
185 #endif
186 int nb_nics;
187 NICInfo nd_table[MAX_NICS];
188 int vm_running;
189 int autostart;
190 int incoming_expected; /* Started with -incoming and waiting for incoming */
191 static int rtc_utc = 1;
192 static int rtc_date_offset = -1; /* -1 means no change */
193 QEMUClock *rtc_clock;
194 int vga_interface_type = VGA_NONE;
195 static int full_screen = 0;
196 #ifdef CONFIG_SDL
197 static int no_frame = 0;
198 #endif
199 int no_quit = 0;
200 CharDriverState *serial_hds[MAX_SERIAL_PORTS];
201 CharDriverState *parallel_hds[MAX_PARALLEL_PORTS];
202 CharDriverState *virtcon_hds[MAX_VIRTIO_CONSOLES];
203 int win2k_install_hack = 0;
204 int rtc_td_hack = 0;
205 int usb_enabled = 0;
206 int singlestep = 0;
207 const char *assigned_devices[MAX_DEV_ASSIGN_CMDLINE];
208 int assigned_devices_index;
209 int smp_cpus = 1;
210 int max_cpus = 0;
211 int smp_cores = 1;
212 int smp_threads = 1;
213 const char *vnc_display;
214 int acpi_enabled = 1;
215 int no_hpet = 0;
216 int fd_bootchk = 1;
217 int no_reboot = 0;
218 int no_shutdown = 0;
219 int cursor_hide = 1;
220 int graphic_rotate = 0;
221 uint8_t irq0override = 1;
222 const char *watchdog;
223 const char *option_rom[MAX_OPTION_ROMS];
224 int nb_option_roms;
225 int semihosting_enabled = 0;
226 int time_drift_fix = 0;
227 unsigned int kvm_shadow_memory = 0;
228 int old_param = 0;
229 const char *qemu_name;
230 int alt_grab = 0;
231 int ctrl_grab = 0;
232 unsigned int nb_prom_envs = 0;
233 const char *prom_envs[MAX_PROM_ENVS];
234 const char *nvram = NULL;
235 int boot_menu;
237 int nb_numa_nodes;
238 uint64_t node_mem[MAX_NODES];
239 uint64_t node_cpumask[MAX_NODES];
241 static QEMUTimer *nographic_timer;
243 uint8_t qemu_uuid[16];
245 static QEMUBootSetHandler *boot_set_handler;
246 static void *boot_set_opaque;
248 static NotifierList exit_notifiers =
249 NOTIFIER_LIST_INITIALIZER(exit_notifiers);
251 int kvm_allowed = 1;
252 uint32_t xen_domid;
253 enum xen_mode xen_mode = XEN_EMULATE;
255 static int default_serial = 1;
256 static int default_parallel = 1;
257 static int default_virtcon = 1;
258 static int default_monitor = 1;
259 static int default_vga = 1;
260 static int default_floppy = 1;
261 static int default_cdrom = 1;
262 static int default_sdcard = 1;
264 static struct {
265 const char *driver;
266 int *flag;
267 } default_list[] = {
268 { .driver = "isa-serial", .flag = &default_serial },
269 { .driver = "isa-parallel", .flag = &default_parallel },
270 { .driver = "isa-fdc", .flag = &default_floppy },
271 { .driver = "ide-drive", .flag = &default_cdrom },
272 { .driver = "virtio-serial-pci", .flag = &default_virtcon },
273 { .driver = "virtio-serial-s390", .flag = &default_virtcon },
274 { .driver = "virtio-serial", .flag = &default_virtcon },
275 { .driver = "VGA", .flag = &default_vga },
276 { .driver = "cirrus-vga", .flag = &default_vga },
277 { .driver = "vmware-svga", .flag = &default_vga },
280 static int default_driver_check(QemuOpts *opts, void *opaque)
282 const char *driver = qemu_opt_get(opts, "driver");
283 int i;
285 if (!driver)
286 return 0;
287 for (i = 0; i < ARRAY_SIZE(default_list); i++) {
288 if (strcmp(default_list[i].driver, driver) != 0)
289 continue;
290 *(default_list[i].flag) = 0;
292 return 0;
295 /***********************************************************/
296 /* real time host monotonic timer */
298 /* compute with 96 bit intermediate result: (a*b)/c */
299 uint64_t muldiv64(uint64_t a, uint32_t b, uint32_t c)
301 union {
302 uint64_t ll;
303 struct {
304 #ifdef HOST_WORDS_BIGENDIAN
305 uint32_t high, low;
306 #else
307 uint32_t low, high;
308 #endif
309 } l;
310 } u, res;
311 uint64_t rl, rh;
313 u.ll = a;
314 rl = (uint64_t)u.l.low * (uint64_t)b;
315 rh = (uint64_t)u.l.high * (uint64_t)b;
316 rh += (rl >> 32);
317 res.l.high = rh / c;
318 res.l.low = (((rh % c) << 32) + (rl & 0xffffffff)) / c;
319 return res.ll;
322 /***********************************************************/
323 /* host time/date access */
324 void qemu_get_timedate(struct tm *tm, int offset)
326 time_t ti;
327 struct tm *ret;
329 time(&ti);
330 ti += offset;
331 if (rtc_date_offset == -1) {
332 if (rtc_utc)
333 ret = gmtime(&ti);
334 else
335 ret = localtime(&ti);
336 } else {
337 ti -= rtc_date_offset;
338 ret = gmtime(&ti);
341 memcpy(tm, ret, sizeof(struct tm));
344 int qemu_timedate_diff(struct tm *tm)
346 time_t seconds;
348 if (rtc_date_offset == -1)
349 if (rtc_utc)
350 seconds = mktimegm(tm);
351 else
352 seconds = mktime(tm);
353 else
354 seconds = mktimegm(tm) + rtc_date_offset;
356 return seconds - time(NULL);
359 void rtc_change_mon_event(struct tm *tm)
361 QObject *data;
363 data = qobject_from_jsonf("{ 'offset': %d }", qemu_timedate_diff(tm));
364 monitor_protocol_event(QEVENT_RTC_CHANGE, data);
365 qobject_decref(data);
368 static void configure_rtc_date_offset(const char *startdate, int legacy)
370 time_t rtc_start_date;
371 struct tm tm;
373 if (!strcmp(startdate, "now") && legacy) {
374 rtc_date_offset = -1;
375 } else {
376 if (sscanf(startdate, "%d-%d-%dT%d:%d:%d",
377 &tm.tm_year,
378 &tm.tm_mon,
379 &tm.tm_mday,
380 &tm.tm_hour,
381 &tm.tm_min,
382 &tm.tm_sec) == 6) {
383 /* OK */
384 } else if (sscanf(startdate, "%d-%d-%d",
385 &tm.tm_year,
386 &tm.tm_mon,
387 &tm.tm_mday) == 3) {
388 tm.tm_hour = 0;
389 tm.tm_min = 0;
390 tm.tm_sec = 0;
391 } else {
392 goto date_fail;
394 tm.tm_year -= 1900;
395 tm.tm_mon--;
396 rtc_start_date = mktimegm(&tm);
397 if (rtc_start_date == -1) {
398 date_fail:
399 fprintf(stderr, "Invalid date format. Valid formats are:\n"
400 "'2006-06-17T16:01:21' or '2006-06-17'\n");
401 exit(1);
403 rtc_date_offset = time(NULL) - rtc_start_date;
407 static void configure_rtc(QemuOpts *opts)
409 const char *value;
411 value = qemu_opt_get(opts, "base");
412 if (value) {
413 if (!strcmp(value, "utc")) {
414 rtc_utc = 1;
415 } else if (!strcmp(value, "localtime")) {
416 rtc_utc = 0;
417 } else {
418 configure_rtc_date_offset(value, 0);
421 value = qemu_opt_get(opts, "clock");
422 if (value) {
423 if (!strcmp(value, "host")) {
424 rtc_clock = host_clock;
425 } else if (!strcmp(value, "vm")) {
426 rtc_clock = vm_clock;
427 } else {
428 fprintf(stderr, "qemu: invalid option value '%s'\n", value);
429 exit(1);
432 value = qemu_opt_get(opts, "driftfix");
433 if (value) {
434 if (!strcmp(value, "slew")) {
435 rtc_td_hack = 1;
436 } else if (!strcmp(value, "none")) {
437 rtc_td_hack = 0;
438 } else {
439 fprintf(stderr, "qemu: invalid option value '%s'\n", value);
440 exit(1);
445 /***********************************************************/
446 /* Bluetooth support */
447 static int nb_hcis;
448 static int cur_hci;
449 static struct HCIInfo *hci_table[MAX_NICS];
451 static struct bt_vlan_s {
452 struct bt_scatternet_s net;
453 int id;
454 struct bt_vlan_s *next;
455 } *first_bt_vlan;
457 /* find or alloc a new bluetooth "VLAN" */
458 static struct bt_scatternet_s *qemu_find_bt_vlan(int id)
460 struct bt_vlan_s **pvlan, *vlan;
461 for (vlan = first_bt_vlan; vlan != NULL; vlan = vlan->next) {
462 if (vlan->id == id)
463 return &vlan->net;
465 vlan = qemu_mallocz(sizeof(struct bt_vlan_s));
466 vlan->id = id;
467 pvlan = &first_bt_vlan;
468 while (*pvlan != NULL)
469 pvlan = &(*pvlan)->next;
470 *pvlan = vlan;
471 return &vlan->net;
474 static void null_hci_send(struct HCIInfo *hci, const uint8_t *data, int len)
478 static int null_hci_addr_set(struct HCIInfo *hci, const uint8_t *bd_addr)
480 return -ENOTSUP;
483 static struct HCIInfo null_hci = {
484 .cmd_send = null_hci_send,
485 .sco_send = null_hci_send,
486 .acl_send = null_hci_send,
487 .bdaddr_set = null_hci_addr_set,
490 struct HCIInfo *qemu_next_hci(void)
492 if (cur_hci == nb_hcis)
493 return &null_hci;
495 return hci_table[cur_hci++];
498 static struct HCIInfo *hci_init(const char *str)
500 char *endp;
501 struct bt_scatternet_s *vlan = 0;
503 if (!strcmp(str, "null"))
504 /* null */
505 return &null_hci;
506 else if (!strncmp(str, "host", 4) && (str[4] == '\0' || str[4] == ':'))
507 /* host[:hciN] */
508 return bt_host_hci(str[4] ? str + 5 : "hci0");
509 else if (!strncmp(str, "hci", 3)) {
510 /* hci[,vlan=n] */
511 if (str[3]) {
512 if (!strncmp(str + 3, ",vlan=", 6)) {
513 vlan = qemu_find_bt_vlan(strtol(str + 9, &endp, 0));
514 if (*endp)
515 vlan = 0;
517 } else
518 vlan = qemu_find_bt_vlan(0);
519 if (vlan)
520 return bt_new_hci(vlan);
523 fprintf(stderr, "qemu: Unknown bluetooth HCI `%s'.\n", str);
525 return 0;
528 static int bt_hci_parse(const char *str)
530 struct HCIInfo *hci;
531 bdaddr_t bdaddr;
533 if (nb_hcis >= MAX_NICS) {
534 fprintf(stderr, "qemu: Too many bluetooth HCIs (max %i).\n", MAX_NICS);
535 return -1;
538 hci = hci_init(str);
539 if (!hci)
540 return -1;
542 bdaddr.b[0] = 0x52;
543 bdaddr.b[1] = 0x54;
544 bdaddr.b[2] = 0x00;
545 bdaddr.b[3] = 0x12;
546 bdaddr.b[4] = 0x34;
547 bdaddr.b[5] = 0x56 + nb_hcis;
548 hci->bdaddr_set(hci, bdaddr.b);
550 hci_table[nb_hcis++] = hci;
552 return 0;
555 static void bt_vhci_add(int vlan_id)
557 struct bt_scatternet_s *vlan = qemu_find_bt_vlan(vlan_id);
559 if (!vlan->slave)
560 fprintf(stderr, "qemu: warning: adding a VHCI to "
561 "an empty scatternet %i\n", vlan_id);
563 bt_vhci_init(bt_new_hci(vlan));
566 static struct bt_device_s *bt_device_add(const char *opt)
568 struct bt_scatternet_s *vlan;
569 int vlan_id = 0;
570 char *endp = strstr(opt, ",vlan=");
571 int len = (endp ? endp - opt : strlen(opt)) + 1;
572 char devname[10];
574 pstrcpy(devname, MIN(sizeof(devname), len), opt);
576 if (endp) {
577 vlan_id = strtol(endp + 6, &endp, 0);
578 if (*endp) {
579 fprintf(stderr, "qemu: unrecognised bluetooth vlan Id\n");
580 return 0;
584 vlan = qemu_find_bt_vlan(vlan_id);
586 if (!vlan->slave)
587 fprintf(stderr, "qemu: warning: adding a slave device to "
588 "an empty scatternet %i\n", vlan_id);
590 if (!strcmp(devname, "keyboard"))
591 return bt_keyboard_init(vlan);
593 fprintf(stderr, "qemu: unsupported bluetooth device `%s'\n", devname);
594 return 0;
597 static int bt_parse(const char *opt)
599 const char *endp, *p;
600 int vlan;
602 if (strstart(opt, "hci", &endp)) {
603 if (!*endp || *endp == ',') {
604 if (*endp)
605 if (!strstart(endp, ",vlan=", 0))
606 opt = endp + 1;
608 return bt_hci_parse(opt);
610 } else if (strstart(opt, "vhci", &endp)) {
611 if (!*endp || *endp == ',') {
612 if (*endp) {
613 if (strstart(endp, ",vlan=", &p)) {
614 vlan = strtol(p, (char **) &endp, 0);
615 if (*endp) {
616 fprintf(stderr, "qemu: bad scatternet '%s'\n", p);
617 return 1;
619 } else {
620 fprintf(stderr, "qemu: bad parameter '%s'\n", endp + 1);
621 return 1;
623 } else
624 vlan = 0;
626 bt_vhci_add(vlan);
627 return 0;
629 } else if (strstart(opt, "device:", &endp))
630 return !bt_device_add(endp);
632 fprintf(stderr, "qemu: bad bluetooth parameter '%s'\n", opt);
633 return 1;
636 /***********************************************************/
637 /* QEMU Block devices */
639 #define HD_ALIAS "index=%d,media=disk"
640 #define CDROM_ALIAS "index=2,media=cdrom"
641 #define FD_ALIAS "index=%d,if=floppy"
642 #define PFLASH_ALIAS "if=pflash"
643 #define MTD_ALIAS "if=mtd"
644 #define SD_ALIAS "index=0,if=sd"
646 static int drive_init_func(QemuOpts *opts, void *opaque)
648 int *use_scsi = opaque;
649 int fatal_error = 0;
651 if (drive_init(opts, *use_scsi, &fatal_error) == NULL) {
652 if (fatal_error)
653 return 1;
655 return 0;
658 static int drive_enable_snapshot(QemuOpts *opts, void *opaque)
660 if (NULL == qemu_opt_get(opts, "snapshot")) {
661 qemu_opt_set(opts, "snapshot", "on");
663 return 0;
666 void qemu_register_boot_set(QEMUBootSetHandler *func, void *opaque)
668 boot_set_handler = func;
669 boot_set_opaque = opaque;
672 int qemu_boot_set(const char *boot_devices)
674 if (!boot_set_handler) {
675 return -EINVAL;
677 return boot_set_handler(boot_set_opaque, boot_devices);
680 static void validate_bootdevices(char *devices)
682 /* We just do some generic consistency checks */
683 const char *p;
684 int bitmap = 0;
686 for (p = devices; *p != '\0'; p++) {
687 /* Allowed boot devices are:
688 * a-b: floppy disk drives
689 * c-f: IDE disk drives
690 * g-m: machine implementation dependant drives
691 * n-p: network devices
692 * It's up to each machine implementation to check if the given boot
693 * devices match the actual hardware implementation and firmware
694 * features.
696 if (*p < 'a' || *p > 'p') {
697 fprintf(stderr, "Invalid boot device '%c'\n", *p);
698 exit(1);
700 if (bitmap & (1 << (*p - 'a'))) {
701 fprintf(stderr, "Boot device '%c' was given twice\n", *p);
702 exit(1);
704 bitmap |= 1 << (*p - 'a');
708 static void restore_boot_devices(void *opaque)
710 char *standard_boot_devices = opaque;
711 static int first = 1;
713 /* Restore boot order and remove ourselves after the first boot */
714 if (first) {
715 first = 0;
716 return;
719 qemu_boot_set(standard_boot_devices);
721 qemu_unregister_reset(restore_boot_devices, standard_boot_devices);
722 qemu_free(standard_boot_devices);
725 static void numa_add(const char *optarg)
727 char option[128];
728 char *endptr;
729 unsigned long long value, endvalue;
730 int nodenr;
732 optarg = get_opt_name(option, 128, optarg, ',') + 1;
733 if (!strcmp(option, "node")) {
734 if (get_param_value(option, 128, "nodeid", optarg) == 0) {
735 nodenr = nb_numa_nodes;
736 } else {
737 nodenr = strtoull(option, NULL, 10);
740 if (get_param_value(option, 128, "mem", optarg) == 0) {
741 node_mem[nodenr] = 0;
742 } else {
743 value = strtoull(option, &endptr, 0);
744 switch (*endptr) {
745 case 0: case 'M': case 'm':
746 value <<= 20;
747 break;
748 case 'G': case 'g':
749 value <<= 30;
750 break;
752 node_mem[nodenr] = value;
754 if (get_param_value(option, 128, "cpus", optarg) == 0) {
755 node_cpumask[nodenr] = 0;
756 } else {
757 value = strtoull(option, &endptr, 10);
758 if (value >= 64) {
759 value = 63;
760 fprintf(stderr, "only 64 CPUs in NUMA mode supported.\n");
761 } else {
762 if (*endptr == '-') {
763 endvalue = strtoull(endptr+1, &endptr, 10);
764 if (endvalue >= 63) {
765 endvalue = 62;
766 fprintf(stderr,
767 "only 63 CPUs in NUMA mode supported.\n");
769 value = (2ULL << endvalue) - (1ULL << value);
770 } else {
771 value = 1ULL << value;
774 node_cpumask[nodenr] = value;
776 nb_numa_nodes++;
778 return;
781 static void smp_parse(const char *optarg)
783 int smp, sockets = 0, threads = 0, cores = 0;
784 char *endptr;
785 char option[128];
787 smp = strtoul(optarg, &endptr, 10);
788 if (endptr != optarg) {
789 if (*endptr == ',') {
790 endptr++;
793 if (get_param_value(option, 128, "sockets", endptr) != 0)
794 sockets = strtoull(option, NULL, 10);
795 if (get_param_value(option, 128, "cores", endptr) != 0)
796 cores = strtoull(option, NULL, 10);
797 if (get_param_value(option, 128, "threads", endptr) != 0)
798 threads = strtoull(option, NULL, 10);
799 if (get_param_value(option, 128, "maxcpus", endptr) != 0)
800 max_cpus = strtoull(option, NULL, 10);
802 /* compute missing values, prefer sockets over cores over threads */
803 if (smp == 0 || sockets == 0) {
804 sockets = sockets > 0 ? sockets : 1;
805 cores = cores > 0 ? cores : 1;
806 threads = threads > 0 ? threads : 1;
807 if (smp == 0) {
808 smp = cores * threads * sockets;
810 } else {
811 if (cores == 0) {
812 threads = threads > 0 ? threads : 1;
813 cores = smp / (sockets * threads);
814 } else {
815 threads = smp / (cores * sockets);
818 smp_cpus = smp;
819 smp_cores = cores > 0 ? cores : 1;
820 smp_threads = threads > 0 ? threads : 1;
821 if (max_cpus == 0)
822 max_cpus = smp_cpus;
825 /***********************************************************/
826 /* USB devices */
828 static int usb_device_add(const char *devname)
830 const char *p;
831 USBDevice *dev = NULL;
833 if (!usb_enabled)
834 return -1;
836 /* drivers with .usbdevice_name entry in USBDeviceInfo */
837 dev = usbdevice_create(devname);
838 if (dev)
839 goto done;
841 /* the other ones */
842 if (strstart(devname, "host:", &p)) {
843 dev = usb_host_device_open(p);
844 } else if (!strcmp(devname, "bt") || strstart(devname, "bt:", &p)) {
845 dev = usb_bt_init(devname[2] ? hci_init(p) :
846 bt_new_hci(qemu_find_bt_vlan(0)));
847 } else {
848 return -1;
850 if (!dev)
851 return -1;
853 done:
854 return 0;
857 static int usb_device_del(const char *devname)
859 int bus_num, addr;
860 const char *p;
862 if (strstart(devname, "host:", &p))
863 return usb_host_device_close(p);
865 if (!usb_enabled)
866 return -1;
868 p = strchr(devname, '.');
869 if (!p)
870 return -1;
871 bus_num = strtoul(devname, NULL, 0);
872 addr = strtoul(p + 1, NULL, 0);
874 return usb_device_delete_addr(bus_num, addr);
877 static int usb_parse(const char *cmdline)
879 int r;
880 r = usb_device_add(cmdline);
881 if (r < 0) {
882 fprintf(stderr, "qemu: could not add USB device '%s'\n", cmdline);
884 return r;
887 void do_usb_add(Monitor *mon, const QDict *qdict)
889 const char *devname = qdict_get_str(qdict, "devname");
890 if (usb_device_add(devname) < 0) {
891 error_report("could not add USB device '%s'", devname);
895 void do_usb_del(Monitor *mon, const QDict *qdict)
897 const char *devname = qdict_get_str(qdict, "devname");
898 if (usb_device_del(devname) < 0) {
899 error_report("could not delete USB device '%s'", devname);
903 /***********************************************************/
904 /* PCMCIA/Cardbus */
906 static struct pcmcia_socket_entry_s {
907 PCMCIASocket *socket;
908 struct pcmcia_socket_entry_s *next;
909 } *pcmcia_sockets = 0;
911 void pcmcia_socket_register(PCMCIASocket *socket)
913 struct pcmcia_socket_entry_s *entry;
915 entry = qemu_malloc(sizeof(struct pcmcia_socket_entry_s));
916 entry->socket = socket;
917 entry->next = pcmcia_sockets;
918 pcmcia_sockets = entry;
921 void pcmcia_socket_unregister(PCMCIASocket *socket)
923 struct pcmcia_socket_entry_s *entry, **ptr;
925 ptr = &pcmcia_sockets;
926 for (entry = *ptr; entry; ptr = &entry->next, entry = *ptr)
927 if (entry->socket == socket) {
928 *ptr = entry->next;
929 qemu_free(entry);
933 void pcmcia_info(Monitor *mon)
935 struct pcmcia_socket_entry_s *iter;
937 if (!pcmcia_sockets)
938 monitor_printf(mon, "No PCMCIA sockets\n");
940 for (iter = pcmcia_sockets; iter; iter = iter->next)
941 monitor_printf(mon, "%s: %s\n", iter->socket->slot_string,
942 iter->socket->attached ? iter->socket->card_string :
943 "Empty");
946 /***********************************************************/
947 /* I/O handling */
949 typedef struct IOHandlerRecord {
950 int fd;
951 IOCanReadHandler *fd_read_poll;
952 IOHandler *fd_read;
953 IOHandler *fd_write;
954 int deleted;
955 void *opaque;
956 /* temporary data */
957 struct pollfd *ufd;
958 QLIST_ENTRY(IOHandlerRecord) next;
959 } IOHandlerRecord;
961 static QLIST_HEAD(, IOHandlerRecord) io_handlers =
962 QLIST_HEAD_INITIALIZER(io_handlers);
965 /* XXX: fd_read_poll should be suppressed, but an API change is
966 necessary in the character devices to suppress fd_can_read(). */
967 int qemu_set_fd_handler2(int fd,
968 IOCanReadHandler *fd_read_poll,
969 IOHandler *fd_read,
970 IOHandler *fd_write,
971 void *opaque)
973 IOHandlerRecord *ioh;
975 if (!fd_read && !fd_write) {
976 QLIST_FOREACH(ioh, &io_handlers, next) {
977 if (ioh->fd == fd) {
978 ioh->deleted = 1;
979 break;
982 } else {
983 QLIST_FOREACH(ioh, &io_handlers, next) {
984 if (ioh->fd == fd)
985 goto found;
987 ioh = qemu_mallocz(sizeof(IOHandlerRecord));
988 QLIST_INSERT_HEAD(&io_handlers, ioh, next);
989 found:
990 ioh->fd = fd;
991 ioh->fd_read_poll = fd_read_poll;
992 ioh->fd_read = fd_read;
993 ioh->fd_write = fd_write;
994 ioh->opaque = opaque;
995 ioh->deleted = 0;
997 qemu_notify_event();
998 return 0;
1001 int qemu_set_fd_handler(int fd,
1002 IOHandler *fd_read,
1003 IOHandler *fd_write,
1004 void *opaque)
1006 return qemu_set_fd_handler2(fd, NULL, fd_read, fd_write, opaque);
1009 /***********************************************************/
1010 /* machine registration */
1012 static QEMUMachine *first_machine = NULL;
1013 QEMUMachine *current_machine = NULL;
1015 int qemu_register_machine(QEMUMachine *m)
1017 QEMUMachine **pm;
1018 pm = &first_machine;
1019 while (*pm != NULL)
1020 pm = &(*pm)->next;
1021 m->next = NULL;
1022 *pm = m;
1023 return 0;
1026 static QEMUMachine *find_machine(const char *name)
1028 QEMUMachine *m;
1030 for(m = first_machine; m != NULL; m = m->next) {
1031 if (!strcmp(m->name, name))
1032 return m;
1033 if (m->alias && !strcmp(m->alias, name))
1034 return m;
1036 return NULL;
1039 static QEMUMachine *find_default_machine(void)
1041 QEMUMachine *m;
1043 for(m = first_machine; m != NULL; m = m->next) {
1044 if (m->is_default) {
1045 return m;
1048 return NULL;
1051 /***********************************************************/
1052 /* main execution loop */
1054 static void gui_update(void *opaque)
1056 uint64_t interval = GUI_REFRESH_INTERVAL;
1057 DisplayState *ds = opaque;
1058 DisplayChangeListener *dcl = ds->listeners;
1060 qemu_flush_coalesced_mmio_buffer();
1061 dpy_refresh(ds);
1063 while (dcl != NULL) {
1064 if (dcl->gui_timer_interval &&
1065 dcl->gui_timer_interval < interval)
1066 interval = dcl->gui_timer_interval;
1067 dcl = dcl->next;
1069 qemu_mod_timer(ds->gui_timer, interval + qemu_get_clock(rt_clock));
1072 static void nographic_update(void *opaque)
1074 uint64_t interval = GUI_REFRESH_INTERVAL;
1076 qemu_flush_coalesced_mmio_buffer();
1077 qemu_mod_timer(nographic_timer, interval + qemu_get_clock(rt_clock));
1080 struct vm_change_state_entry {
1081 VMChangeStateHandler *cb;
1082 void *opaque;
1083 QLIST_ENTRY (vm_change_state_entry) entries;
1086 static QLIST_HEAD(vm_change_state_head, vm_change_state_entry) vm_change_state_head;
1088 VMChangeStateEntry *qemu_add_vm_change_state_handler(VMChangeStateHandler *cb,
1089 void *opaque)
1091 VMChangeStateEntry *e;
1093 e = qemu_mallocz(sizeof (*e));
1095 e->cb = cb;
1096 e->opaque = opaque;
1097 QLIST_INSERT_HEAD(&vm_change_state_head, e, entries);
1098 return e;
1101 void qemu_del_vm_change_state_handler(VMChangeStateEntry *e)
1103 QLIST_REMOVE (e, entries);
1104 qemu_free (e);
1107 void vm_state_notify(int running, int reason)
1109 VMChangeStateEntry *e;
1111 for (e = vm_change_state_head.lh_first; e; e = e->entries.le_next) {
1112 e->cb(e->opaque, running, reason);
1116 void vm_start(void)
1118 if (!vm_running) {
1119 cpu_enable_ticks();
1120 vm_running = 1;
1121 vm_state_notify(1, 0);
1122 resume_all_vcpus();
1123 monitor_protocol_event(QEVENT_RESUME, NULL);
1127 /* reset/shutdown handler */
1129 typedef struct QEMUResetEntry {
1130 QTAILQ_ENTRY(QEMUResetEntry) entry;
1131 QEMUResetHandler *func;
1132 void *opaque;
1133 } QEMUResetEntry;
1135 static QTAILQ_HEAD(reset_handlers, QEMUResetEntry) reset_handlers =
1136 QTAILQ_HEAD_INITIALIZER(reset_handlers);
1137 static int reset_requested;
1138 static int shutdown_requested;
1139 static int powerdown_requested;
1140 int debug_requested;
1141 int vmstop_requested;
1143 int qemu_no_shutdown(void)
1145 int r = no_shutdown;
1146 no_shutdown = 0;
1147 return r;
1150 int qemu_shutdown_requested(void)
1152 int r = shutdown_requested;
1153 shutdown_requested = 0;
1154 return r;
1157 int qemu_reset_requested(void)
1159 int r = reset_requested;
1160 reset_requested = 0;
1161 return r;
1164 int qemu_powerdown_requested(void)
1166 int r = powerdown_requested;
1167 powerdown_requested = 0;
1168 return r;
1171 static int qemu_debug_requested(void)
1173 int r = debug_requested;
1174 debug_requested = 0;
1175 return r;
1178 static int qemu_vmstop_requested(void)
1180 int r = vmstop_requested;
1181 vmstop_requested = 0;
1182 return r;
1185 void qemu_register_reset(QEMUResetHandler *func, void *opaque)
1187 QEMUResetEntry *re = qemu_mallocz(sizeof(QEMUResetEntry));
1189 re->func = func;
1190 re->opaque = opaque;
1191 QTAILQ_INSERT_TAIL(&reset_handlers, re, entry);
1194 void qemu_unregister_reset(QEMUResetHandler *func, void *opaque)
1196 QEMUResetEntry *re;
1198 QTAILQ_FOREACH(re, &reset_handlers, entry) {
1199 if (re->func == func && re->opaque == opaque) {
1200 QTAILQ_REMOVE(&reset_handlers, re, entry);
1201 qemu_free(re);
1202 return;
1207 void qemu_system_reset(void)
1209 QEMUResetEntry *re, *nre;
1211 /* reset all devices */
1212 QTAILQ_FOREACH_SAFE(re, &reset_handlers, entry, nre) {
1213 re->func(re->opaque);
1215 monitor_protocol_event(QEVENT_RESET, NULL);
1216 cpu_synchronize_all_post_reset();
1219 void qemu_system_reset_request(void)
1221 if (no_reboot) {
1222 shutdown_requested = 1;
1223 } else {
1224 reset_requested = 1;
1226 if (cpu_single_env) {
1227 cpu_single_env->stopped = 1;
1228 cpu_exit(cpu_single_env);
1230 qemu_notify_event();
1233 void qemu_system_shutdown_request(void)
1235 shutdown_requested = 1;
1236 qemu_notify_event();
1239 void qemu_system_powerdown_request(void)
1241 powerdown_requested = 1;
1242 qemu_notify_event();
1245 void main_loop_wait(int nonblocking)
1247 IOHandlerRecord *ioh;
1248 fd_set rfds, wfds, xfds;
1249 int ret, nfds;
1250 struct timeval tv;
1251 int timeout;
1253 if (nonblocking)
1254 timeout = 0;
1255 else {
1256 timeout = qemu_calculate_timeout();
1257 qemu_bh_update_timeout(&timeout);
1260 os_host_main_loop_wait(&timeout);
1262 /* poll any events */
1263 /* XXX: separate device handlers from system ones */
1264 nfds = -1;
1265 FD_ZERO(&rfds);
1266 FD_ZERO(&wfds);
1267 FD_ZERO(&xfds);
1268 QLIST_FOREACH(ioh, &io_handlers, next) {
1269 if (ioh->deleted)
1270 continue;
1271 if (ioh->fd_read &&
1272 (!ioh->fd_read_poll ||
1273 ioh->fd_read_poll(ioh->opaque) != 0)) {
1274 FD_SET(ioh->fd, &rfds);
1275 if (ioh->fd > nfds)
1276 nfds = ioh->fd;
1278 if (ioh->fd_write) {
1279 FD_SET(ioh->fd, &wfds);
1280 if (ioh->fd > nfds)
1281 nfds = ioh->fd;
1285 tv.tv_sec = timeout / 1000;
1286 tv.tv_usec = (timeout % 1000) * 1000;
1288 slirp_select_fill(&nfds, &rfds, &wfds, &xfds);
1290 qemu_mutex_unlock_iothread();
1291 ret = select(nfds + 1, &rfds, &wfds, &xfds, &tv);
1292 qemu_mutex_lock_iothread();
1293 if (ret > 0) {
1294 IOHandlerRecord *pioh;
1296 QLIST_FOREACH_SAFE(ioh, &io_handlers, next, pioh) {
1297 if (ioh->deleted) {
1298 QLIST_REMOVE(ioh, next);
1299 qemu_free(ioh);
1300 continue;
1302 if (ioh->fd_read && FD_ISSET(ioh->fd, &rfds)) {
1303 ioh->fd_read(ioh->opaque);
1304 if (!(ioh->fd_read_poll && ioh->fd_read_poll(ioh->opaque)))
1305 FD_CLR(ioh->fd, &rfds);
1307 if (ioh->fd_write && FD_ISSET(ioh->fd, &wfds)) {
1308 ioh->fd_write(ioh->opaque);
1313 slirp_select_poll(&rfds, &wfds, &xfds, (ret < 0));
1315 qemu_run_all_timers();
1317 /* Check bottom-halves last in case any of the earlier events triggered
1318 them. */
1319 qemu_bh_poll();
1323 static int vm_can_run(void)
1325 if (powerdown_requested)
1326 return 0;
1327 if (reset_requested)
1328 return 0;
1329 if (shutdown_requested)
1330 return 0;
1331 if (debug_requested)
1332 return 0;
1333 return 1;
1336 qemu_irq qemu_system_powerdown;
1338 static void main_loop(void)
1340 int r;
1342 if (kvm_enabled()) {
1343 kvm_main_loop();
1344 cpu_disable_ticks();
1345 return;
1348 qemu_main_loop_start();
1350 for (;;) {
1351 do {
1352 bool nonblocking = false;
1353 #ifdef CONFIG_PROFILER
1354 int64_t ti;
1355 #endif
1356 #ifndef CONFIG_IOTHREAD
1357 nonblocking = cpu_exec_all();
1358 #endif
1359 #ifdef CONFIG_PROFILER
1360 ti = profile_getclock();
1361 #endif
1362 main_loop_wait(nonblocking);
1363 #ifdef CONFIG_PROFILER
1364 dev_time += profile_getclock() - ti;
1365 #endif
1366 } while (vm_can_run());
1368 if ((r = qemu_debug_requested())) {
1369 vm_stop(r);
1371 if (qemu_shutdown_requested()) {
1372 monitor_protocol_event(QEVENT_SHUTDOWN, NULL);
1373 if (no_shutdown) {
1374 vm_stop(0);
1375 no_shutdown = 0;
1376 } else
1377 break;
1379 if (qemu_reset_requested()) {
1380 pause_all_vcpus();
1381 qemu_system_reset();
1382 resume_all_vcpus();
1384 if (qemu_powerdown_requested()) {
1385 monitor_protocol_event(QEVENT_POWERDOWN, NULL);
1386 qemu_irq_raise(qemu_system_powerdown);
1388 if ((r = qemu_vmstop_requested())) {
1389 vm_stop(r);
1392 bdrv_close_all();
1393 pause_all_vcpus();
1396 static void version(void)
1398 printf("QEMU emulator version " QEMU_VERSION QEMU_PKGVERSION ", Copyright (c) 2003-2008 Fabrice Bellard\n");
1401 static void help(int exitcode)
1403 const char *options_help =
1404 #define DEF(option, opt_arg, opt_enum, opt_help, arch_mask) \
1405 opt_help
1406 #define DEFHEADING(text) stringify(text) "\n"
1407 #include "qemu-options.def"
1408 #undef DEF
1409 #undef DEFHEADING
1410 #undef GEN_DOCS
1412 version();
1413 printf("usage: %s [options] [disk_image]\n"
1414 "\n"
1415 "'disk_image' is a raw hard disk image for IDE hard disk 0\n"
1416 "\n"
1417 "%s\n"
1418 "During emulation, the following keys are useful:\n"
1419 "ctrl-alt-f toggle full screen\n"
1420 "ctrl-alt-n switch to virtual console 'n'\n"
1421 "ctrl-alt toggle mouse and keyboard grab\n"
1422 "\n"
1423 "When using -nographic, press 'ctrl-a h' to get some help.\n",
1424 "qemu",
1425 options_help);
1426 exit(exitcode);
1429 #define HAS_ARG 0x0001
1431 typedef struct QEMUOption {
1432 const char *name;
1433 int flags;
1434 int index;
1435 uint32_t arch_mask;
1436 } QEMUOption;
1438 static const QEMUOption qemu_options[] = {
1439 { "h", 0, QEMU_OPTION_h, QEMU_ARCH_ALL },
1440 #define DEF(option, opt_arg, opt_enum, opt_help, arch_mask) \
1441 { option, opt_arg, opt_enum, arch_mask },
1442 #define DEFHEADING(text)
1443 #include "qemu-options.def"
1444 #undef DEF
1445 #undef DEFHEADING
1446 #undef GEN_DOCS
1447 { NULL },
1449 static void select_vgahw (const char *p)
1451 const char *opts;
1453 default_vga = 0;
1454 vga_interface_type = VGA_NONE;
1455 if (strstart(p, "std", &opts)) {
1456 vga_interface_type = VGA_STD;
1457 } else if (strstart(p, "cirrus", &opts)) {
1458 vga_interface_type = VGA_CIRRUS;
1459 } else if (strstart(p, "vmware", &opts)) {
1460 vga_interface_type = VGA_VMWARE;
1461 } else if (strstart(p, "xenfb", &opts)) {
1462 vga_interface_type = VGA_XENFB;
1463 } else if (!strstart(p, "none", &opts)) {
1464 invalid_vga:
1465 fprintf(stderr, "Unknown vga type: %s\n", p);
1466 exit(1);
1468 while (*opts) {
1469 const char *nextopt;
1471 if (strstart(opts, ",retrace=", &nextopt)) {
1472 opts = nextopt;
1473 if (strstart(opts, "dumb", &nextopt))
1474 vga_retrace_method = VGA_RETRACE_DUMB;
1475 else if (strstart(opts, "precise", &nextopt))
1476 vga_retrace_method = VGA_RETRACE_PRECISE;
1477 else goto invalid_vga;
1478 } else goto invalid_vga;
1479 opts = nextopt;
1483 static int balloon_parse(const char *arg)
1485 QemuOpts *opts;
1487 if (strcmp(arg, "none") == 0) {
1488 return 0;
1491 if (!strncmp(arg, "virtio", 6)) {
1492 if (arg[6] == ',') {
1493 /* have params -> parse them */
1494 opts = qemu_opts_parse(qemu_find_opts("device"), arg+7, 0);
1495 if (!opts)
1496 return -1;
1497 } else {
1498 /* create empty opts */
1499 opts = qemu_opts_create(qemu_find_opts("device"), NULL, 0);
1501 qemu_opt_set(opts, "driver", "virtio-balloon-pci");
1502 return 0;
1505 return -1;
1508 char *qemu_find_file(int type, const char *name)
1510 int len;
1511 const char *subdir;
1512 char *buf;
1514 /* If name contains path separators then try it as a straight path. */
1515 if ((strchr(name, '/') || strchr(name, '\\'))
1516 && access(name, R_OK) == 0) {
1517 return qemu_strdup(name);
1519 switch (type) {
1520 case QEMU_FILE_TYPE_BIOS:
1521 subdir = "";
1522 break;
1523 case QEMU_FILE_TYPE_KEYMAP:
1524 subdir = "keymaps/";
1525 break;
1526 default:
1527 abort();
1529 len = strlen(data_dir) + strlen(name) + strlen(subdir) + 2;
1530 buf = qemu_mallocz(len);
1531 snprintf(buf, len, "%s/%s%s", data_dir, subdir, name);
1532 if (access(buf, R_OK)) {
1533 qemu_free(buf);
1534 return NULL;
1536 return buf;
1539 static int device_help_func(QemuOpts *opts, void *opaque)
1541 return qdev_device_help(opts);
1544 static int device_init_func(QemuOpts *opts, void *opaque)
1546 DeviceState *dev;
1548 dev = qdev_device_add(opts);
1549 if (!dev)
1550 return -1;
1551 return 0;
1554 static int chardev_init_func(QemuOpts *opts, void *opaque)
1556 CharDriverState *chr;
1558 chr = qemu_chr_open_opts(opts, NULL);
1559 if (!chr)
1560 return -1;
1561 return 0;
1564 #ifdef CONFIG_VIRTFS
1565 static int fsdev_init_func(QemuOpts *opts, void *opaque)
1567 int ret;
1568 ret = qemu_fsdev_add(opts);
1570 return ret;
1572 #endif
1574 static int mon_init_func(QemuOpts *opts, void *opaque)
1576 CharDriverState *chr;
1577 const char *chardev;
1578 const char *mode;
1579 int flags;
1581 mode = qemu_opt_get(opts, "mode");
1582 if (mode == NULL) {
1583 mode = "readline";
1585 if (strcmp(mode, "readline") == 0) {
1586 flags = MONITOR_USE_READLINE;
1587 } else if (strcmp(mode, "control") == 0) {
1588 flags = MONITOR_USE_CONTROL;
1589 } else {
1590 fprintf(stderr, "unknown monitor mode \"%s\"\n", mode);
1591 exit(1);
1594 if (qemu_opt_get_bool(opts, "default", 0))
1595 flags |= MONITOR_IS_DEFAULT;
1597 chardev = qemu_opt_get(opts, "chardev");
1598 chr = qemu_chr_find(chardev);
1599 if (chr == NULL) {
1600 fprintf(stderr, "chardev \"%s\" not found\n", chardev);
1601 exit(1);
1604 monitor_init(chr, flags);
1605 return 0;
1608 static void monitor_parse(const char *optarg, const char *mode)
1610 static int monitor_device_index = 0;
1611 QemuOpts *opts;
1612 const char *p;
1613 char label[32];
1614 int def = 0;
1616 if (strstart(optarg, "chardev:", &p)) {
1617 snprintf(label, sizeof(label), "%s", p);
1618 } else {
1619 snprintf(label, sizeof(label), "compat_monitor%d",
1620 monitor_device_index);
1621 if (monitor_device_index == 0) {
1622 def = 1;
1624 opts = qemu_chr_parse_compat(label, optarg);
1625 if (!opts) {
1626 fprintf(stderr, "parse error: %s\n", optarg);
1627 exit(1);
1631 opts = qemu_opts_create(qemu_find_opts("mon"), label, 1);
1632 if (!opts) {
1633 fprintf(stderr, "duplicate chardev: %s\n", label);
1634 exit(1);
1636 qemu_opt_set(opts, "mode", mode);
1637 qemu_opt_set(opts, "chardev", label);
1638 if (def)
1639 qemu_opt_set(opts, "default", "on");
1640 monitor_device_index++;
1643 struct device_config {
1644 enum {
1645 DEV_USB, /* -usbdevice */
1646 DEV_BT, /* -bt */
1647 DEV_SERIAL, /* -serial */
1648 DEV_PARALLEL, /* -parallel */
1649 DEV_VIRTCON, /* -virtioconsole */
1650 DEV_DEBUGCON, /* -debugcon */
1651 } type;
1652 const char *cmdline;
1653 QTAILQ_ENTRY(device_config) next;
1655 QTAILQ_HEAD(, device_config) device_configs = QTAILQ_HEAD_INITIALIZER(device_configs);
1657 static void add_device_config(int type, const char *cmdline)
1659 struct device_config *conf;
1661 conf = qemu_mallocz(sizeof(*conf));
1662 conf->type = type;
1663 conf->cmdline = cmdline;
1664 QTAILQ_INSERT_TAIL(&device_configs, conf, next);
1667 static int foreach_device_config(int type, int (*func)(const char *cmdline))
1669 struct device_config *conf;
1670 int rc;
1672 QTAILQ_FOREACH(conf, &device_configs, next) {
1673 if (conf->type != type)
1674 continue;
1675 rc = func(conf->cmdline);
1676 if (0 != rc)
1677 return rc;
1679 return 0;
1682 static int serial_parse(const char *devname)
1684 static int index = 0;
1685 char label[32];
1687 if (strcmp(devname, "none") == 0)
1688 return 0;
1689 if (index == MAX_SERIAL_PORTS) {
1690 fprintf(stderr, "qemu: too many serial ports\n");
1691 exit(1);
1693 snprintf(label, sizeof(label), "serial%d", index);
1694 serial_hds[index] = qemu_chr_open(label, devname, NULL);
1695 if (!serial_hds[index]) {
1696 fprintf(stderr, "qemu: could not open serial device '%s': %s\n",
1697 devname, strerror(errno));
1698 return -1;
1700 index++;
1701 return 0;
1704 static int parallel_parse(const char *devname)
1706 static int index = 0;
1707 char label[32];
1709 if (strcmp(devname, "none") == 0)
1710 return 0;
1711 if (index == MAX_PARALLEL_PORTS) {
1712 fprintf(stderr, "qemu: too many parallel ports\n");
1713 exit(1);
1715 snprintf(label, sizeof(label), "parallel%d", index);
1716 parallel_hds[index] = qemu_chr_open(label, devname, NULL);
1717 if (!parallel_hds[index]) {
1718 fprintf(stderr, "qemu: could not open parallel device '%s': %s\n",
1719 devname, strerror(errno));
1720 return -1;
1722 index++;
1723 return 0;
1726 static int virtcon_parse(const char *devname)
1728 QemuOptsList *device = qemu_find_opts("device");
1729 static int index = 0;
1730 char label[32];
1731 QemuOpts *bus_opts, *dev_opts;
1733 if (strcmp(devname, "none") == 0)
1734 return 0;
1735 if (index == MAX_VIRTIO_CONSOLES) {
1736 fprintf(stderr, "qemu: too many virtio consoles\n");
1737 exit(1);
1740 bus_opts = qemu_opts_create(device, NULL, 0);
1741 qemu_opt_set(bus_opts, "driver", "virtio-serial");
1743 dev_opts = qemu_opts_create(device, NULL, 0);
1744 qemu_opt_set(dev_opts, "driver", "virtconsole");
1746 snprintf(label, sizeof(label), "virtcon%d", index);
1747 virtcon_hds[index] = qemu_chr_open(label, devname, NULL);
1748 if (!virtcon_hds[index]) {
1749 fprintf(stderr, "qemu: could not open virtio console '%s': %s\n",
1750 devname, strerror(errno));
1751 return -1;
1753 qemu_opt_set(dev_opts, "chardev", label);
1755 index++;
1756 return 0;
1759 static int debugcon_parse(const char *devname)
1761 QemuOpts *opts;
1763 if (!qemu_chr_open("debugcon", devname, NULL)) {
1764 exit(1);
1766 opts = qemu_opts_create(qemu_find_opts("device"), "debugcon", 1);
1767 if (!opts) {
1768 fprintf(stderr, "qemu: already have a debugcon device\n");
1769 exit(1);
1771 qemu_opt_set(opts, "driver", "isa-debugcon");
1772 qemu_opt_set(opts, "chardev", "debugcon");
1773 return 0;
1776 void qemu_add_exit_notifier(Notifier *notify)
1778 notifier_list_add(&exit_notifiers, notify);
1781 void qemu_remove_exit_notifier(Notifier *notify)
1783 notifier_list_remove(&exit_notifiers, notify);
1786 static void qemu_run_exit_notifiers(void)
1788 notifier_list_notify(&exit_notifiers);
1791 static const QEMUOption *lookup_opt(int argc, char **argv,
1792 const char **poptarg, int *poptind)
1794 const QEMUOption *popt;
1795 int optind = *poptind;
1796 char *r = argv[optind];
1797 const char *optarg;
1799 loc_set_cmdline(argv, optind, 1);
1800 optind++;
1801 /* Treat --foo the same as -foo. */
1802 if (r[1] == '-')
1803 r++;
1804 popt = qemu_options;
1805 for(;;) {
1806 if (!popt->name) {
1807 error_report("invalid option");
1808 exit(1);
1810 if (!strcmp(popt->name, r + 1))
1811 break;
1812 popt++;
1814 if (popt->flags & HAS_ARG) {
1815 if (optind >= argc) {
1816 error_report("requires an argument");
1817 exit(1);
1819 optarg = argv[optind++];
1820 loc_set_cmdline(argv, optind - 2, 2);
1821 } else {
1822 optarg = NULL;
1825 *poptarg = optarg;
1826 *poptind = optind;
1828 return popt;
1831 int main(int argc, char **argv, char **envp)
1833 const char *gdbstub_dev = NULL;
1834 int i;
1835 int snapshot, linux_boot;
1836 const char *icount_option = NULL;
1837 const char *initrd_filename;
1838 const char *kernel_filename, *kernel_cmdline;
1839 char boot_devices[33] = "cad"; /* default to HD->floppy->CD-ROM */
1840 DisplayState *ds;
1841 DisplayChangeListener *dcl;
1842 int cyls, heads, secs, translation;
1843 QemuOpts *hda_opts = NULL, *opts;
1844 QemuOptsList *olist;
1845 int optind;
1846 const char *optarg;
1847 const char *loadvm = NULL;
1848 QEMUMachine *machine;
1849 const char *cpu_model;
1850 int tb_size;
1851 const char *pid_file = NULL;
1852 const char *incoming = NULL;
1853 int show_vnc_port = 0;
1854 int defconfig = 1;
1856 #ifdef CONFIG_SIMPLE_TRACE
1857 const char *trace_file = NULL;
1858 #endif
1859 atexit(qemu_run_exit_notifiers);
1860 error_set_progname(argv[0]);
1862 init_clocks();
1864 qemu_cache_utils_init(envp);
1866 QLIST_INIT (&vm_change_state_head);
1867 os_setup_early_signal_handling();
1869 module_call_init(MODULE_INIT_MACHINE);
1870 machine = find_default_machine();
1871 cpu_model = NULL;
1872 initrd_filename = NULL;
1873 ram_size = 0;
1874 snapshot = 0;
1875 kernel_filename = NULL;
1876 kernel_cmdline = "";
1877 cyls = heads = secs = 0;
1878 translation = BIOS_ATA_TRANSLATION_AUTO;
1880 for (i = 0; i < MAX_NODES; i++) {
1881 node_mem[i] = 0;
1882 node_cpumask[i] = 0;
1885 assigned_devices_index = 0;
1887 nb_numa_nodes = 0;
1888 nb_nics = 0;
1890 tb_size = 0;
1891 autostart= 1;
1893 #ifdef CONFIG_VIRTFS
1894 qemu_add_opts(&qemu_fsdev_opts);
1895 qemu_add_opts(&qemu_virtfs_opts);
1896 #endif
1898 /* first pass of option parsing */
1899 optind = 1;
1900 while (optind < argc) {
1901 if (argv[optind][0] != '-') {
1902 /* disk image */
1903 optind++;
1904 continue;
1905 } else {
1906 const QEMUOption *popt;
1908 popt = lookup_opt(argc, argv, &optarg, &optind);
1909 switch (popt->index) {
1910 case QEMU_OPTION_nodefconfig:
1911 defconfig=0;
1912 break;
1917 if (defconfig) {
1918 int ret;
1920 ret = qemu_read_config_file(CONFIG_QEMU_CONFDIR "/qemu.conf");
1921 if (ret < 0 && ret != -ENOENT) {
1922 exit(1);
1925 ret = qemu_read_config_file(arch_config_name);
1926 if (ret < 0 && ret != -ENOENT) {
1927 exit(1);
1930 cpudef_init();
1932 /* second pass of option parsing */
1933 optind = 1;
1934 for(;;) {
1935 if (optind >= argc)
1936 break;
1937 if (argv[optind][0] != '-') {
1938 hda_opts = drive_add(argv[optind++], HD_ALIAS, 0);
1939 } else {
1940 const QEMUOption *popt;
1942 popt = lookup_opt(argc, argv, &optarg, &optind);
1943 if (!(popt->arch_mask & arch_type)) {
1944 printf("Option %s not supported for this target\n", popt->name);
1945 exit(1);
1947 switch(popt->index) {
1948 case QEMU_OPTION_M:
1949 machine = find_machine(optarg);
1950 if (!machine) {
1951 QEMUMachine *m;
1952 printf("Supported machines are:\n");
1953 for(m = first_machine; m != NULL; m = m->next) {
1954 if (m->alias)
1955 printf("%-10s %s (alias of %s)\n",
1956 m->alias, m->desc, m->name);
1957 printf("%-10s %s%s\n",
1958 m->name, m->desc,
1959 m->is_default ? " (default)" : "");
1961 exit(*optarg != '?');
1963 break;
1964 case QEMU_OPTION_cpu:
1965 /* hw initialization will check this */
1966 if (*optarg == '?') {
1967 list_cpus(stdout, &fprintf, optarg);
1968 exit(0);
1969 } else {
1970 cpu_model = optarg;
1972 break;
1973 case QEMU_OPTION_initrd:
1974 initrd_filename = optarg;
1975 break;
1976 case QEMU_OPTION_hda:
1977 if (cyls == 0)
1978 hda_opts = drive_add(optarg, HD_ALIAS, 0);
1979 else
1980 hda_opts = drive_add(optarg, HD_ALIAS
1981 ",cyls=%d,heads=%d,secs=%d%s",
1982 0, cyls, heads, secs,
1983 translation == BIOS_ATA_TRANSLATION_LBA ?
1984 ",trans=lba" :
1985 translation == BIOS_ATA_TRANSLATION_NONE ?
1986 ",trans=none" : "");
1987 break;
1988 case QEMU_OPTION_hdb:
1989 case QEMU_OPTION_hdc:
1990 case QEMU_OPTION_hdd:
1991 drive_add(optarg, HD_ALIAS, popt->index - QEMU_OPTION_hda);
1992 break;
1993 case QEMU_OPTION_drive:
1994 drive_add(NULL, "%s", optarg);
1995 break;
1996 case QEMU_OPTION_set:
1997 if (qemu_set_option(optarg) != 0)
1998 exit(1);
1999 break;
2000 case QEMU_OPTION_global:
2001 if (qemu_global_option(optarg) != 0)
2002 exit(1);
2003 break;
2004 case QEMU_OPTION_mtdblock:
2005 drive_add(optarg, MTD_ALIAS);
2006 break;
2007 case QEMU_OPTION_sd:
2008 drive_add(optarg, SD_ALIAS);
2009 break;
2010 case QEMU_OPTION_pflash:
2011 drive_add(optarg, PFLASH_ALIAS);
2012 break;
2013 case QEMU_OPTION_snapshot:
2014 snapshot = 1;
2015 break;
2016 case QEMU_OPTION_hdachs:
2018 const char *p;
2019 p = optarg;
2020 cyls = strtol(p, (char **)&p, 0);
2021 if (cyls < 1 || cyls > 16383)
2022 goto chs_fail;
2023 if (*p != ',')
2024 goto chs_fail;
2025 p++;
2026 heads = strtol(p, (char **)&p, 0);
2027 if (heads < 1 || heads > 16)
2028 goto chs_fail;
2029 if (*p != ',')
2030 goto chs_fail;
2031 p++;
2032 secs = strtol(p, (char **)&p, 0);
2033 if (secs < 1 || secs > 63)
2034 goto chs_fail;
2035 if (*p == ',') {
2036 p++;
2037 if (!strcmp(p, "none"))
2038 translation = BIOS_ATA_TRANSLATION_NONE;
2039 else if (!strcmp(p, "lba"))
2040 translation = BIOS_ATA_TRANSLATION_LBA;
2041 else if (!strcmp(p, "auto"))
2042 translation = BIOS_ATA_TRANSLATION_AUTO;
2043 else
2044 goto chs_fail;
2045 } else if (*p != '\0') {
2046 chs_fail:
2047 fprintf(stderr, "qemu: invalid physical CHS format\n");
2048 exit(1);
2050 if (hda_opts != NULL) {
2051 char num[16];
2052 snprintf(num, sizeof(num), "%d", cyls);
2053 qemu_opt_set(hda_opts, "cyls", num);
2054 snprintf(num, sizeof(num), "%d", heads);
2055 qemu_opt_set(hda_opts, "heads", num);
2056 snprintf(num, sizeof(num), "%d", secs);
2057 qemu_opt_set(hda_opts, "secs", num);
2058 if (translation == BIOS_ATA_TRANSLATION_LBA)
2059 qemu_opt_set(hda_opts, "trans", "lba");
2060 if (translation == BIOS_ATA_TRANSLATION_NONE)
2061 qemu_opt_set(hda_opts, "trans", "none");
2064 break;
2065 case QEMU_OPTION_numa:
2066 if (nb_numa_nodes >= MAX_NODES) {
2067 fprintf(stderr, "qemu: too many NUMA nodes\n");
2068 exit(1);
2070 numa_add(optarg);
2071 break;
2072 case QEMU_OPTION_nographic:
2073 display_type = DT_NOGRAPHIC;
2074 break;
2075 #ifdef CONFIG_CURSES
2076 case QEMU_OPTION_curses:
2077 display_type = DT_CURSES;
2078 break;
2079 #endif
2080 case QEMU_OPTION_portrait:
2081 graphic_rotate = 1;
2082 break;
2083 case QEMU_OPTION_kernel:
2084 kernel_filename = optarg;
2085 break;
2086 case QEMU_OPTION_append:
2087 kernel_cmdline = optarg;
2088 break;
2089 case QEMU_OPTION_cdrom:
2090 drive_add(optarg, CDROM_ALIAS);
2091 break;
2092 case QEMU_OPTION_boot:
2094 static const char * const params[] = {
2095 "order", "once", "menu", NULL
2097 char buf[sizeof(boot_devices)];
2098 char *standard_boot_devices;
2099 int legacy = 0;
2101 if (!strchr(optarg, '=')) {
2102 legacy = 1;
2103 pstrcpy(buf, sizeof(buf), optarg);
2104 } else if (check_params(buf, sizeof(buf), params, optarg) < 0) {
2105 fprintf(stderr,
2106 "qemu: unknown boot parameter '%s' in '%s'\n",
2107 buf, optarg);
2108 exit(1);
2111 if (legacy ||
2112 get_param_value(buf, sizeof(buf), "order", optarg)) {
2113 validate_bootdevices(buf);
2114 pstrcpy(boot_devices, sizeof(boot_devices), buf);
2116 if (!legacy) {
2117 if (get_param_value(buf, sizeof(buf),
2118 "once", optarg)) {
2119 validate_bootdevices(buf);
2120 standard_boot_devices = qemu_strdup(boot_devices);
2121 pstrcpy(boot_devices, sizeof(boot_devices), buf);
2122 qemu_register_reset(restore_boot_devices,
2123 standard_boot_devices);
2125 if (get_param_value(buf, sizeof(buf),
2126 "menu", optarg)) {
2127 if (!strcmp(buf, "on")) {
2128 boot_menu = 1;
2129 } else if (!strcmp(buf, "off")) {
2130 boot_menu = 0;
2131 } else {
2132 fprintf(stderr,
2133 "qemu: invalid option value '%s'\n",
2134 buf);
2135 exit(1);
2140 break;
2141 case QEMU_OPTION_fda:
2142 case QEMU_OPTION_fdb:
2143 drive_add(optarg, FD_ALIAS, popt->index - QEMU_OPTION_fda);
2144 break;
2145 case QEMU_OPTION_no_fd_bootchk:
2146 fd_bootchk = 0;
2147 break;
2148 case QEMU_OPTION_netdev:
2149 if (net_client_parse(qemu_find_opts("netdev"), optarg) == -1) {
2150 exit(1);
2152 break;
2153 case QEMU_OPTION_net:
2154 if (net_client_parse(qemu_find_opts("net"), optarg) == -1) {
2155 exit(1);
2157 break;
2158 #ifdef CONFIG_SLIRP
2159 case QEMU_OPTION_tftp:
2160 legacy_tftp_prefix = optarg;
2161 break;
2162 case QEMU_OPTION_bootp:
2163 legacy_bootp_filename = optarg;
2164 break;
2165 case QEMU_OPTION_redir:
2166 if (net_slirp_redir(optarg) < 0)
2167 exit(1);
2168 break;
2169 #endif
2170 case QEMU_OPTION_bt:
2171 add_device_config(DEV_BT, optarg);
2172 break;
2173 case QEMU_OPTION_audio_help:
2174 if (!(audio_available())) {
2175 printf("Option %s not supported for this target\n", popt->name);
2176 exit(1);
2178 AUD_help ();
2179 exit (0);
2180 break;
2181 case QEMU_OPTION_soundhw:
2182 if (!(audio_available())) {
2183 printf("Option %s not supported for this target\n", popt->name);
2184 exit(1);
2186 select_soundhw (optarg);
2187 break;
2188 case QEMU_OPTION_h:
2189 help(0);
2190 break;
2191 case QEMU_OPTION_version:
2192 version();
2193 exit(0);
2194 break;
2195 case QEMU_OPTION_m: {
2196 uint64_t value;
2197 char *ptr;
2199 value = strtoul(optarg, &ptr, 10);
2200 switch (*ptr) {
2201 case 0: case 'M': case 'm':
2202 value <<= 20;
2203 break;
2204 case 'G': case 'g':
2205 value <<= 30;
2206 break;
2207 default:
2208 fprintf(stderr, "qemu: invalid ram size: %s\n", optarg);
2209 exit(1);
2212 /* On 32-bit hosts, QEMU is limited by virtual address space */
2213 if (value > (2047 << 20) && HOST_LONG_BITS == 32) {
2214 fprintf(stderr, "qemu: at most 2047 MB RAM can be simulated\n");
2215 exit(1);
2217 if (value != (uint64_t)(ram_addr_t)value) {
2218 fprintf(stderr, "qemu: ram size too large\n");
2219 exit(1);
2221 ram_size = value;
2222 break;
2224 case QEMU_OPTION_mempath:
2225 mem_path = optarg;
2226 break;
2227 #ifdef MAP_POPULATE
2228 case QEMU_OPTION_mem_prealloc:
2229 mem_prealloc = 1;
2230 break;
2231 #endif
2232 case QEMU_OPTION_d:
2233 set_cpu_log(optarg);
2234 break;
2235 case QEMU_OPTION_s:
2236 gdbstub_dev = "tcp::" DEFAULT_GDBSTUB_PORT;
2237 break;
2238 case QEMU_OPTION_gdb:
2239 gdbstub_dev = optarg;
2240 break;
2241 case QEMU_OPTION_L:
2242 data_dir = optarg;
2243 break;
2244 case QEMU_OPTION_bios:
2245 bios_name = optarg;
2246 break;
2247 case QEMU_OPTION_singlestep:
2248 singlestep = 1;
2249 break;
2250 case QEMU_OPTION_S:
2251 autostart = 0;
2252 break;
2253 case QEMU_OPTION_k:
2254 keyboard_layout = optarg;
2255 break;
2256 case QEMU_OPTION_localtime:
2257 rtc_utc = 0;
2258 break;
2259 case QEMU_OPTION_vga:
2260 select_vgahw (optarg);
2261 break;
2262 case QEMU_OPTION_g:
2264 const char *p;
2265 int w, h, depth;
2266 p = optarg;
2267 w = strtol(p, (char **)&p, 10);
2268 if (w <= 0) {
2269 graphic_error:
2270 fprintf(stderr, "qemu: invalid resolution or depth\n");
2271 exit(1);
2273 if (*p != 'x')
2274 goto graphic_error;
2275 p++;
2276 h = strtol(p, (char **)&p, 10);
2277 if (h <= 0)
2278 goto graphic_error;
2279 if (*p == 'x') {
2280 p++;
2281 depth = strtol(p, (char **)&p, 10);
2282 if (depth != 8 && depth != 15 && depth != 16 &&
2283 depth != 24 && depth != 32)
2284 goto graphic_error;
2285 } else if (*p == '\0') {
2286 depth = graphic_depth;
2287 } else {
2288 goto graphic_error;
2291 graphic_width = w;
2292 graphic_height = h;
2293 graphic_depth = depth;
2295 break;
2296 case QEMU_OPTION_echr:
2298 char *r;
2299 term_escape_char = strtol(optarg, &r, 0);
2300 if (r == optarg)
2301 printf("Bad argument to echr\n");
2302 break;
2304 case QEMU_OPTION_monitor:
2305 monitor_parse(optarg, "readline");
2306 default_monitor = 0;
2307 break;
2308 case QEMU_OPTION_qmp:
2309 monitor_parse(optarg, "control");
2310 default_monitor = 0;
2311 break;
2312 case QEMU_OPTION_mon:
2313 opts = qemu_opts_parse(qemu_find_opts("mon"), optarg, 1);
2314 if (!opts) {
2315 exit(1);
2317 default_monitor = 0;
2318 break;
2319 case QEMU_OPTION_chardev:
2320 opts = qemu_opts_parse(qemu_find_opts("chardev"), optarg, 1);
2321 if (!opts) {
2322 exit(1);
2324 break;
2325 case QEMU_OPTION_fsdev:
2326 olist = qemu_find_opts("fsdev");
2327 if (!olist) {
2328 fprintf(stderr, "fsdev is not supported by this qemu build.\n");
2329 exit(1);
2331 opts = qemu_opts_parse(olist, optarg, 1);
2332 if (!opts) {
2333 fprintf(stderr, "parse error: %s\n", optarg);
2334 exit(1);
2336 break;
2337 case QEMU_OPTION_virtfs: {
2338 char *arg_fsdev = NULL;
2339 char *arg_9p = NULL;
2340 int len = 0;
2342 olist = qemu_find_opts("virtfs");
2343 if (!olist) {
2344 fprintf(stderr, "virtfs is not supported by this qemu build.\n");
2345 exit(1);
2347 opts = qemu_opts_parse(olist, optarg, 1);
2348 if (!opts) {
2349 fprintf(stderr, "parse error: %s\n", optarg);
2350 exit(1);
2353 if (qemu_opt_get(opts, "fstype") == NULL ||
2354 qemu_opt_get(opts, "mount_tag") == NULL ||
2355 qemu_opt_get(opts, "path") == NULL ||
2356 qemu_opt_get(opts, "security_model") == NULL) {
2357 fprintf(stderr, "Usage: -virtfs fstype,path=/share_path/,"
2358 "security_model=[mapped|passthrough|none],"
2359 "mnt_tag=tag.\n");
2360 exit(1);
2363 len = strlen(",id=,path=,security_model=");
2364 len += strlen(qemu_opt_get(opts, "fstype"));
2365 len += strlen(qemu_opt_get(opts, "mount_tag"));
2366 len += strlen(qemu_opt_get(opts, "path"));
2367 len += strlen(qemu_opt_get(opts, "security_model"));
2368 arg_fsdev = qemu_malloc((len + 1) * sizeof(*arg_fsdev));
2370 snprintf(arg_fsdev, (len + 1) * sizeof(*arg_fsdev),
2371 "%s,id=%s,path=%s,security_model=%s",
2372 qemu_opt_get(opts, "fstype"),
2373 qemu_opt_get(opts, "mount_tag"),
2374 qemu_opt_get(opts, "path"),
2375 qemu_opt_get(opts, "security_model"));
2377 len = strlen("virtio-9p-pci,fsdev=,mount_tag=");
2378 len += 2*strlen(qemu_opt_get(opts, "mount_tag"));
2379 arg_9p = qemu_malloc((len + 1) * sizeof(*arg_9p));
2381 snprintf(arg_9p, (len + 1) * sizeof(*arg_9p),
2382 "virtio-9p-pci,fsdev=%s,mount_tag=%s",
2383 qemu_opt_get(opts, "mount_tag"),
2384 qemu_opt_get(opts, "mount_tag"));
2386 if (!qemu_opts_parse(qemu_find_opts("fsdev"), arg_fsdev, 1)) {
2387 fprintf(stderr, "parse error [fsdev]: %s\n", optarg);
2388 exit(1);
2391 if (!qemu_opts_parse(qemu_find_opts("device"), arg_9p, 1)) {
2392 fprintf(stderr, "parse error [device]: %s\n", optarg);
2393 exit(1);
2396 qemu_free(arg_fsdev);
2397 qemu_free(arg_9p);
2398 break;
2400 case QEMU_OPTION_serial:
2401 add_device_config(DEV_SERIAL, optarg);
2402 default_serial = 0;
2403 if (strncmp(optarg, "mon:", 4) == 0) {
2404 default_monitor = 0;
2406 break;
2407 case QEMU_OPTION_watchdog:
2408 if (watchdog) {
2409 fprintf(stderr,
2410 "qemu: only one watchdog option may be given\n");
2411 return 1;
2413 watchdog = optarg;
2414 break;
2415 case QEMU_OPTION_watchdog_action:
2416 if (select_watchdog_action(optarg) == -1) {
2417 fprintf(stderr, "Unknown -watchdog-action parameter\n");
2418 exit(1);
2420 break;
2421 case QEMU_OPTION_virtiocon:
2422 add_device_config(DEV_VIRTCON, optarg);
2423 default_virtcon = 0;
2424 if (strncmp(optarg, "mon:", 4) == 0) {
2425 default_monitor = 0;
2427 break;
2428 case QEMU_OPTION_parallel:
2429 add_device_config(DEV_PARALLEL, optarg);
2430 default_parallel = 0;
2431 if (strncmp(optarg, "mon:", 4) == 0) {
2432 default_monitor = 0;
2434 break;
2435 case QEMU_OPTION_debugcon:
2436 add_device_config(DEV_DEBUGCON, optarg);
2437 break;
2438 case QEMU_OPTION_loadvm:
2439 loadvm = optarg;
2440 break;
2441 case QEMU_OPTION_full_screen:
2442 full_screen = 1;
2443 break;
2444 #ifdef CONFIG_SDL
2445 case QEMU_OPTION_no_frame:
2446 no_frame = 1;
2447 break;
2448 case QEMU_OPTION_alt_grab:
2449 alt_grab = 1;
2450 break;
2451 case QEMU_OPTION_ctrl_grab:
2452 ctrl_grab = 1;
2453 break;
2454 case QEMU_OPTION_no_quit:
2455 no_quit = 1;
2456 break;
2457 case QEMU_OPTION_sdl:
2458 display_type = DT_SDL;
2459 break;
2460 #endif
2461 case QEMU_OPTION_pidfile:
2462 pid_file = optarg;
2463 break;
2464 case QEMU_OPTION_win2k_hack:
2465 win2k_install_hack = 1;
2466 break;
2467 case QEMU_OPTION_rtc_td_hack:
2468 rtc_td_hack = 1;
2469 break;
2470 case QEMU_OPTION_acpitable:
2471 do_acpitable_option(optarg);
2472 break;
2473 case QEMU_OPTION_smbios:
2474 do_smbios_option(optarg);
2475 break;
2476 #ifdef OBSOLETE_KVM_IMPL
2477 case QEMU_OPTION_enable_kvm:
2478 kvm_allowed = 1;
2479 #endif
2480 break;
2481 case QEMU_OPTION_no_kvm:
2482 kvm_allowed = 0;
2483 #ifdef CONFIG_NO_CPU_EMULATION
2484 fprintf(stderr, "cpu emulation not configured\n");
2485 exit(1);
2486 #endif
2487 break;
2488 #ifdef CONFIG_KVM
2489 case QEMU_OPTION_no_kvm_irqchip: {
2490 kvm_irqchip = 0;
2491 kvm_pit = 0;
2492 break;
2494 case QEMU_OPTION_no_kvm_pit: {
2495 kvm_pit = 0;
2496 break;
2498 case QEMU_OPTION_no_kvm_pit_reinjection: {
2499 kvm_pit_reinject = 0;
2500 break;
2502 case QEMU_OPTION_enable_nesting: {
2503 kvm_nested = 1;
2504 break;
2506 #endif
2507 #if defined(TARGET_I386) || defined(TARGET_X86_64) || defined(TARGET_IA64) || defined(__linux__)
2508 case QEMU_OPTION_pcidevice:
2509 if (assigned_devices_index >= MAX_DEV_ASSIGN_CMDLINE) {
2510 fprintf(stderr, "Too many assigned devices\n");
2511 exit(1);
2513 assigned_devices[assigned_devices_index] = optarg;
2514 assigned_devices_index++;
2515 break;
2516 #endif
2517 case QEMU_OPTION_usb:
2518 usb_enabled = 1;
2519 break;
2520 case QEMU_OPTION_usbdevice:
2521 usb_enabled = 1;
2522 add_device_config(DEV_USB, optarg);
2523 break;
2524 case QEMU_OPTION_device:
2525 if (!qemu_opts_parse(qemu_find_opts("device"), optarg, 1)) {
2526 exit(1);
2528 break;
2529 case QEMU_OPTION_smp:
2530 smp_parse(optarg);
2531 if (smp_cpus < 1) {
2532 fprintf(stderr, "Invalid number of CPUs\n");
2533 exit(1);
2535 if (max_cpus < smp_cpus) {
2536 fprintf(stderr, "maxcpus must be equal to or greater than "
2537 "smp\n");
2538 exit(1);
2540 if (max_cpus > 255) {
2541 fprintf(stderr, "Unsupported number of maxcpus\n");
2542 exit(1);
2544 break;
2545 case QEMU_OPTION_vnc:
2546 display_type = DT_VNC;
2547 vnc_display = optarg;
2548 break;
2549 case QEMU_OPTION_no_acpi:
2550 acpi_enabled = 0;
2551 break;
2552 case QEMU_OPTION_no_hpet:
2553 no_hpet = 1;
2554 break;
2555 case QEMU_OPTION_balloon:
2556 if (balloon_parse(optarg) < 0) {
2557 fprintf(stderr, "Unknown -balloon argument %s\n", optarg);
2558 exit(1);
2560 break;
2561 case QEMU_OPTION_no_reboot:
2562 no_reboot = 1;
2563 break;
2564 case QEMU_OPTION_no_shutdown:
2565 no_shutdown = 1;
2566 break;
2567 case QEMU_OPTION_show_cursor:
2568 cursor_hide = 0;
2569 break;
2570 case QEMU_OPTION_uuid:
2571 if(qemu_uuid_parse(optarg, qemu_uuid) < 0) {
2572 fprintf(stderr, "Fail to parse UUID string."
2573 " Wrong format.\n");
2574 exit(1);
2576 break;
2577 case QEMU_OPTION_option_rom:
2578 if (nb_option_roms >= MAX_OPTION_ROMS) {
2579 fprintf(stderr, "Too many option ROMs\n");
2580 exit(1);
2582 option_rom[nb_option_roms] = optarg;
2583 nb_option_roms++;
2584 break;
2585 case QEMU_OPTION_semihosting:
2586 semihosting_enabled = 1;
2587 break;
2588 case QEMU_OPTION_tdf:
2589 time_drift_fix = 1;
2590 break;
2591 case QEMU_OPTION_kvm_shadow_memory:
2592 kvm_shadow_memory = (int64_t)atoi(optarg) * 1024 * 1024 / 4096;
2593 break;
2594 case QEMU_OPTION_name:
2595 qemu_name = qemu_strdup(optarg);
2597 char *p = strchr(qemu_name, ',');
2598 if (p != NULL) {
2599 *p++ = 0;
2600 if (strncmp(p, "process=", 8)) {
2601 fprintf(stderr, "Unknown subargument %s to -name", p);
2602 exit(1);
2604 p += 8;
2605 os_set_proc_name(p);
2608 break;
2609 case QEMU_OPTION_prom_env:
2610 if (nb_prom_envs >= MAX_PROM_ENVS) {
2611 fprintf(stderr, "Too many prom variables\n");
2612 exit(1);
2614 prom_envs[nb_prom_envs] = optarg;
2615 nb_prom_envs++;
2616 break;
2617 case QEMU_OPTION_old_param:
2618 old_param = 1;
2619 break;
2620 case QEMU_OPTION_clock:
2621 configure_alarms(optarg);
2622 break;
2623 case QEMU_OPTION_startdate:
2624 configure_rtc_date_offset(optarg, 1);
2625 break;
2626 case QEMU_OPTION_rtc:
2627 opts = qemu_opts_parse(qemu_find_opts("rtc"), optarg, 0);
2628 if (!opts) {
2629 exit(1);
2631 configure_rtc(opts);
2632 break;
2633 case QEMU_OPTION_tb_size:
2634 tb_size = strtol(optarg, NULL, 0);
2635 if (tb_size < 0)
2636 tb_size = 0;
2637 break;
2638 case QEMU_OPTION_icount:
2639 icount_option = optarg;
2640 break;
2641 case QEMU_OPTION_incoming:
2642 incoming = optarg;
2643 incoming_expected = true;
2644 break;
2645 case QEMU_OPTION_nodefaults:
2646 default_serial = 0;
2647 default_parallel = 0;
2648 default_virtcon = 0;
2649 default_monitor = 0;
2650 default_vga = 0;
2651 default_net = 0;
2652 default_floppy = 0;
2653 default_cdrom = 0;
2654 default_sdcard = 0;
2655 break;
2656 #ifndef _WIN32
2657 case QEMU_OPTION_nvram:
2658 nvram = optarg;
2659 break;
2660 #endif
2661 case QEMU_OPTION_xen_domid:
2662 if (!(xen_available())) {
2663 printf("Option %s not supported for this target\n", popt->name);
2664 exit(1);
2666 xen_domid = atoi(optarg);
2667 break;
2668 case QEMU_OPTION_xen_create:
2669 if (!(xen_available())) {
2670 printf("Option %s not supported for this target\n", popt->name);
2671 exit(1);
2673 xen_mode = XEN_CREATE;
2674 break;
2675 case QEMU_OPTION_xen_attach:
2676 if (!(xen_available())) {
2677 printf("Option %s not supported for this target\n", popt->name);
2678 exit(1);
2680 xen_mode = XEN_ATTACH;
2681 break;
2682 #ifdef CONFIG_SIMPLE_TRACE
2683 case QEMU_OPTION_trace:
2684 opts = qemu_opts_parse(qemu_find_opts("trace"), optarg, 0);
2685 if (opts) {
2686 trace_file = qemu_opt_get(opts, "file");
2688 break;
2689 #endif
2690 case QEMU_OPTION_readconfig:
2692 int ret = qemu_read_config_file(optarg);
2693 if (ret < 0) {
2694 fprintf(stderr, "read config %s: %s\n", optarg,
2695 strerror(-ret));
2696 exit(1);
2698 break;
2700 case QEMU_OPTION_writeconfig:
2702 FILE *fp;
2703 if (strcmp(optarg, "-") == 0) {
2704 fp = stdout;
2705 } else {
2706 fp = fopen(optarg, "w");
2707 if (fp == NULL) {
2708 fprintf(stderr, "open %s: %s\n", optarg, strerror(errno));
2709 exit(1);
2712 qemu_config_write(fp);
2713 fclose(fp);
2714 break;
2716 default:
2717 os_parse_cmd_args(popt->index, optarg);
2721 loc_set_none();
2723 /* If no data_dir is specified then try to find it relative to the
2724 executable path. */
2725 if (!data_dir) {
2726 data_dir = os_find_datadir(argv[0]);
2728 /* If all else fails use the install patch specified when building. */
2729 if (!data_dir) {
2730 data_dir = CONFIG_QEMU_DATADIR;
2733 #ifdef CONFIG_SIMPLE_TRACE
2735 * Set the trace file name, if specified.
2737 st_set_trace_file(trace_file);
2738 #endif
2740 * Default to max_cpus = smp_cpus, in case the user doesn't
2741 * specify a max_cpus value.
2743 if (!max_cpus)
2744 max_cpus = smp_cpus;
2746 machine->max_cpus = machine->max_cpus ?: 1; /* Default to UP */
2747 if (smp_cpus > machine->max_cpus) {
2748 fprintf(stderr, "Number of SMP cpus requested (%d), exceeds max cpus "
2749 "supported by machine `%s' (%d)\n", smp_cpus, machine->name,
2750 machine->max_cpus);
2751 exit(1);
2754 qemu_opts_foreach(qemu_find_opts("device"), default_driver_check, NULL, 0);
2755 qemu_opts_foreach(qemu_find_opts("global"), default_driver_check, NULL, 0);
2757 if (machine->no_serial) {
2758 default_serial = 0;
2760 if (machine->no_parallel) {
2761 default_parallel = 0;
2763 if (!machine->use_virtcon) {
2764 default_virtcon = 0;
2766 if (machine->no_vga) {
2767 default_vga = 0;
2769 if (machine->no_floppy) {
2770 default_floppy = 0;
2772 if (machine->no_cdrom) {
2773 default_cdrom = 0;
2775 if (machine->no_sdcard) {
2776 default_sdcard = 0;
2779 if (display_type == DT_NOGRAPHIC) {
2780 if (default_parallel)
2781 add_device_config(DEV_PARALLEL, "null");
2782 if (default_serial && default_monitor) {
2783 add_device_config(DEV_SERIAL, "mon:stdio");
2784 } else if (default_virtcon && default_monitor) {
2785 add_device_config(DEV_VIRTCON, "mon:stdio");
2786 } else {
2787 if (default_serial)
2788 add_device_config(DEV_SERIAL, "stdio");
2789 if (default_virtcon)
2790 add_device_config(DEV_VIRTCON, "stdio");
2791 if (default_monitor)
2792 monitor_parse("stdio", "readline");
2794 } else {
2795 if (default_serial)
2796 add_device_config(DEV_SERIAL, "vc:80Cx24C");
2797 if (default_parallel)
2798 add_device_config(DEV_PARALLEL, "vc:80Cx24C");
2799 if (default_monitor)
2800 monitor_parse("vc:80Cx24C", "readline");
2801 if (default_virtcon)
2802 add_device_config(DEV_VIRTCON, "vc:80Cx24C");
2804 if (default_vga)
2805 vga_interface_type = VGA_CIRRUS;
2807 socket_init();
2809 if (qemu_opts_foreach(qemu_find_opts("chardev"), chardev_init_func, NULL, 1) != 0)
2810 exit(1);
2811 #ifdef CONFIG_VIRTFS
2812 if (qemu_opts_foreach(qemu_find_opts("fsdev"), fsdev_init_func, NULL, 1) != 0) {
2813 exit(1);
2815 #endif
2817 os_daemonize();
2819 if (pid_file && qemu_create_pidfile(pid_file) != 0) {
2820 os_pidfile_error();
2821 exit(1);
2824 if (kvm_allowed) {
2825 int ret = kvm_init(smp_cpus);
2826 if (ret < 0) {
2827 #if defined(OBSOLETE_KVM_IMPL) || defined(CONFIG_NO_CPU_EMULATION)
2828 if (!kvm_available()) {
2829 printf("KVM not supported for this target\n");
2830 } else {
2831 fprintf(stderr, "failed to initialize KVM: %s\n", strerror(-ret));
2833 exit(1);
2834 #endif
2835 #ifdef CONFIG_KVM
2836 fprintf(stderr, "Could not initialize KVM, will disable KVM support\n");
2837 kvm_allowed = 0;
2838 #endif
2842 if (qemu_init_main_loop()) {
2843 fprintf(stderr, "qemu_init_main_loop failed\n");
2844 exit(1);
2846 linux_boot = (kernel_filename != NULL);
2848 if (!linux_boot && *kernel_cmdline != '\0') {
2849 fprintf(stderr, "-append only allowed with -kernel option\n");
2850 exit(1);
2853 if (!linux_boot && initrd_filename != NULL) {
2854 fprintf(stderr, "-initrd only allowed with -kernel option\n");
2855 exit(1);
2858 os_set_line_buffering();
2860 if (init_timer_alarm() < 0) {
2861 fprintf(stderr, "could not initialize alarm timer\n");
2862 exit(1);
2864 configure_icount(icount_option);
2866 if (net_init_clients() < 0) {
2867 exit(1);
2870 /* init the bluetooth world */
2871 if (foreach_device_config(DEV_BT, bt_parse))
2872 exit(1);
2874 /* init the memory */
2875 if (ram_size == 0)
2876 ram_size = DEFAULT_RAM_SIZE * 1024 * 1024;
2878 /* init the dynamic translator */
2879 cpu_exec_init_all(tb_size * 1024 * 1024);
2881 bdrv_init_with_whitelist();
2883 blk_mig_init();
2885 if (default_cdrom) {
2886 /* we always create the cdrom drive, even if no disk is there */
2887 drive_add(NULL, CDROM_ALIAS);
2890 if (default_floppy) {
2891 /* we always create at least one floppy */
2892 drive_add(NULL, FD_ALIAS, 0);
2895 if (default_sdcard) {
2896 /* we always create one sd slot, even if no card is in it */
2897 drive_add(NULL, SD_ALIAS);
2900 /* open the virtual block devices */
2901 if (snapshot)
2902 qemu_opts_foreach(qemu_find_opts("drive"), drive_enable_snapshot, NULL, 0);
2903 if (qemu_opts_foreach(qemu_find_opts("drive"), drive_init_func, &machine->use_scsi, 1) != 0)
2904 exit(1);
2906 register_savevm_live(NULL, "ram", 0, 4, NULL, ram_save_live, NULL,
2907 ram_load, NULL);
2909 if (nb_numa_nodes > 0) {
2910 int i;
2912 if (nb_numa_nodes > smp_cpus) {
2913 nb_numa_nodes = smp_cpus;
2916 /* If no memory size if given for any node, assume the default case
2917 * and distribute the available memory equally across all nodes
2919 for (i = 0; i < nb_numa_nodes; i++) {
2920 if (node_mem[i] != 0)
2921 break;
2923 if (i == nb_numa_nodes) {
2924 uint64_t usedmem = 0;
2926 /* On Linux, the each node's border has to be 8MB aligned,
2927 * the final node gets the rest.
2929 for (i = 0; i < nb_numa_nodes - 1; i++) {
2930 node_mem[i] = (ram_size / nb_numa_nodes) & ~((1 << 23UL) - 1);
2931 usedmem += node_mem[i];
2933 node_mem[i] = ram_size - usedmem;
2936 for (i = 0; i < nb_numa_nodes; i++) {
2937 if (node_cpumask[i] != 0)
2938 break;
2940 /* assigning the VCPUs round-robin is easier to implement, guest OSes
2941 * must cope with this anyway, because there are BIOSes out there in
2942 * real machines which also use this scheme.
2944 if (i == nb_numa_nodes) {
2945 for (i = 0; i < smp_cpus; i++) {
2946 node_cpumask[i % nb_numa_nodes] |= 1 << i;
2951 if (qemu_opts_foreach(qemu_find_opts("mon"), mon_init_func, NULL, 1) != 0) {
2952 exit(1);
2955 if (foreach_device_config(DEV_SERIAL, serial_parse) < 0)
2956 exit(1);
2957 if (foreach_device_config(DEV_PARALLEL, parallel_parse) < 0)
2958 exit(1);
2959 if (foreach_device_config(DEV_VIRTCON, virtcon_parse) < 0)
2960 exit(1);
2961 if (foreach_device_config(DEV_DEBUGCON, debugcon_parse) < 0)
2962 exit(1);
2964 module_call_init(MODULE_INIT_DEVICE);
2966 if (qemu_opts_foreach(qemu_find_opts("device"), device_help_func, NULL, 0) != 0)
2967 exit(0);
2969 if (watchdog) {
2970 i = select_watchdog(watchdog);
2971 if (i > 0)
2972 exit (i == 1 ? 1 : 0);
2975 if (machine->compat_props) {
2976 qdev_prop_register_global_list(machine->compat_props);
2978 qemu_add_globals();
2980 machine->init(ram_size, boot_devices,
2981 kernel_filename, kernel_cmdline, initrd_filename, cpu_model);
2983 cpu_synchronize_all_post_init();
2985 /* must be after terminal init, SDL library changes signal handlers */
2986 os_setup_signal_handling();
2988 set_numa_modes();
2990 current_machine = machine;
2992 /* init USB devices */
2993 if (usb_enabled) {
2994 if (foreach_device_config(DEV_USB, usb_parse) < 0)
2995 exit(1);
2998 /* init generic devices */
2999 if (qemu_opts_foreach(qemu_find_opts("device"), device_init_func, NULL, 1) != 0)
3000 exit(1);
3002 net_check_clients();
3004 /* just use the first displaystate for the moment */
3005 ds = get_displaystate();
3007 if (display_type == DT_DEFAULT) {
3008 #if defined(CONFIG_SDL) || defined(CONFIG_COCOA)
3009 display_type = DT_SDL;
3010 #else
3011 display_type = DT_VNC;
3012 vnc_display = "localhost:0,to=99";
3013 show_vnc_port = 1;
3014 #endif
3018 switch (display_type) {
3019 case DT_NOGRAPHIC:
3020 break;
3021 #if defined(CONFIG_CURSES)
3022 case DT_CURSES:
3023 curses_display_init(ds, full_screen);
3024 break;
3025 #endif
3026 #if defined(CONFIG_SDL)
3027 case DT_SDL:
3028 sdl_display_init(ds, full_screen, no_frame);
3029 break;
3030 #elif defined(CONFIG_COCOA)
3031 case DT_SDL:
3032 cocoa_display_init(ds, full_screen);
3033 break;
3034 #endif
3035 case DT_VNC:
3036 vnc_display_init(ds);
3037 if (vnc_display_open(ds, vnc_display) < 0)
3038 exit(1);
3040 if (show_vnc_port) {
3041 printf("VNC server running on `%s'\n", vnc_display_local_addr(ds));
3043 break;
3044 default:
3045 break;
3047 dpy_resize(ds);
3049 dcl = ds->listeners;
3050 while (dcl != NULL) {
3051 if (dcl->dpy_refresh != NULL) {
3052 ds->gui_timer = qemu_new_timer(rt_clock, gui_update, ds);
3053 qemu_mod_timer(ds->gui_timer, qemu_get_clock(rt_clock));
3054 break;
3056 dcl = dcl->next;
3059 if (display_type == DT_NOGRAPHIC || display_type == DT_VNC) {
3060 nographic_timer = qemu_new_timer(rt_clock, nographic_update, NULL);
3061 qemu_mod_timer(nographic_timer, qemu_get_clock(rt_clock));
3064 text_consoles_set_display(ds);
3066 if (gdbstub_dev && gdbserver_start(gdbstub_dev) < 0) {
3067 fprintf(stderr, "qemu: could not open gdbserver on device '%s'\n",
3068 gdbstub_dev);
3069 exit(1);
3072 qdev_machine_creation_done();
3074 if (rom_load_all() != 0) {
3075 fprintf(stderr, "rom loading failed\n");
3076 exit(1);
3079 qemu_system_reset();
3080 if (loadvm) {
3081 if (load_vmstate(loadvm) < 0) {
3082 autostart = 0;
3086 if (incoming) {
3087 int ret = qemu_start_incoming_migration(incoming);
3088 if (ret < 0) {
3089 fprintf(stderr, "Migration failed. Exit code %s(%d), exiting.\n",
3090 incoming, ret);
3091 exit(ret);
3093 } else if (autostart) {
3094 vm_start();
3097 os_setup_post();
3099 main_loop();
3100 quit_timers();
3101 net_cleanup();
3103 return 0;