device-assignment: Byte-wise ROM read
[qemu-kvm/stefanha.git] / vl.c
blob468dbac83c4f986a583a8567651dfa8f368cbe55
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 #include "hw/device-assignment.h"
151 #ifdef CONFIG_VIRTFS
152 #include "fsdev/qemu-fsdev.h"
153 #endif
155 #include "disas.h"
157 #include "qemu_socket.h"
159 #include "slirp/libslirp.h"
161 #include "qemu-queue.h"
162 #include "cpus.h"
163 #include "arch_init.h"
165 //#define DEBUG_NET
166 //#define DEBUG_SLIRP
168 #define DEFAULT_RAM_SIZE 128
170 #define MAX_VIRTIO_CONSOLES 1
172 static const char *data_dir;
173 const char *bios_name = NULL;
174 enum vga_retrace_method vga_retrace_method = VGA_RETRACE_DUMB;
175 DisplayType display_type = DT_DEFAULT;
176 const char* keyboard_layout = NULL;
177 ram_addr_t ram_size;
178 const char *mem_path = NULL;
179 #ifdef MAP_POPULATE
180 int mem_prealloc = 0; /* force preallocation of physical target memory */
181 #endif
182 int nb_nics;
183 NICInfo nd_table[MAX_NICS];
184 int vm_running;
185 int autostart;
186 int incoming_expected; /* Started with -incoming and waiting for incoming */
187 static int rtc_utc = 1;
188 static int rtc_date_offset = -1; /* -1 means no change */
189 QEMUClock *rtc_clock;
190 int vga_interface_type = VGA_NONE;
191 static int full_screen = 0;
192 #ifdef CONFIG_SDL
193 static int no_frame = 0;
194 #endif
195 int no_quit = 0;
196 CharDriverState *serial_hds[MAX_SERIAL_PORTS];
197 CharDriverState *parallel_hds[MAX_PARALLEL_PORTS];
198 CharDriverState *virtcon_hds[MAX_VIRTIO_CONSOLES];
199 int win2k_install_hack = 0;
200 int rtc_td_hack = 0;
201 int usb_enabled = 0;
202 int singlestep = 0;
203 const char *assigned_devices[MAX_DEV_ASSIGN_CMDLINE];
204 int assigned_devices_index;
205 int smp_cpus = 1;
206 int max_cpus = 0;
207 int smp_cores = 1;
208 int smp_threads = 1;
209 const char *vnc_display;
210 int acpi_enabled = 1;
211 int no_hpet = 0;
212 int fd_bootchk = 1;
213 int no_reboot = 0;
214 int no_shutdown = 0;
215 int cursor_hide = 1;
216 int graphic_rotate = 0;
217 uint8_t irq0override = 1;
218 const char *watchdog;
219 const char *option_rom[MAX_OPTION_ROMS];
220 int nb_option_roms;
221 int semihosting_enabled = 0;
222 int time_drift_fix = 0;
223 unsigned int kvm_shadow_memory = 0;
224 int old_param = 0;
225 const char *qemu_name;
226 int alt_grab = 0;
227 int ctrl_grab = 0;
228 unsigned int nb_prom_envs = 0;
229 const char *prom_envs[MAX_PROM_ENVS];
230 const char *nvram = NULL;
231 int boot_menu;
233 int nb_numa_nodes;
234 uint64_t node_mem[MAX_NODES];
235 uint64_t node_cpumask[MAX_NODES];
237 static QEMUTimer *nographic_timer;
239 uint8_t qemu_uuid[16];
241 static QEMUBootSetHandler *boot_set_handler;
242 static void *boot_set_opaque;
244 static NotifierList exit_notifiers =
245 NOTIFIER_LIST_INITIALIZER(exit_notifiers);
247 int kvm_allowed = 1;
248 uint32_t xen_domid;
249 enum xen_mode xen_mode = XEN_EMULATE;
251 static int default_serial = 1;
252 static int default_parallel = 1;
253 static int default_virtcon = 1;
254 static int default_monitor = 1;
255 static int default_vga = 1;
256 static int default_floppy = 1;
257 static int default_cdrom = 1;
258 static int default_sdcard = 1;
260 static struct {
261 const char *driver;
262 int *flag;
263 } default_list[] = {
264 { .driver = "isa-serial", .flag = &default_serial },
265 { .driver = "isa-parallel", .flag = &default_parallel },
266 { .driver = "isa-fdc", .flag = &default_floppy },
267 { .driver = "ide-drive", .flag = &default_cdrom },
268 { .driver = "virtio-serial-pci", .flag = &default_virtcon },
269 { .driver = "virtio-serial-s390", .flag = &default_virtcon },
270 { .driver = "virtio-serial", .flag = &default_virtcon },
271 { .driver = "VGA", .flag = &default_vga },
272 { .driver = "cirrus-vga", .flag = &default_vga },
273 { .driver = "vmware-svga", .flag = &default_vga },
276 static int default_driver_check(QemuOpts *opts, void *opaque)
278 const char *driver = qemu_opt_get(opts, "driver");
279 int i;
281 if (!driver)
282 return 0;
283 for (i = 0; i < ARRAY_SIZE(default_list); i++) {
284 if (strcmp(default_list[i].driver, driver) != 0)
285 continue;
286 *(default_list[i].flag) = 0;
288 return 0;
291 /***********************************************************/
292 /* real time host monotonic timer */
294 /* compute with 96 bit intermediate result: (a*b)/c */
295 uint64_t muldiv64(uint64_t a, uint32_t b, uint32_t c)
297 union {
298 uint64_t ll;
299 struct {
300 #ifdef HOST_WORDS_BIGENDIAN
301 uint32_t high, low;
302 #else
303 uint32_t low, high;
304 #endif
305 } l;
306 } u, res;
307 uint64_t rl, rh;
309 u.ll = a;
310 rl = (uint64_t)u.l.low * (uint64_t)b;
311 rh = (uint64_t)u.l.high * (uint64_t)b;
312 rh += (rl >> 32);
313 res.l.high = rh / c;
314 res.l.low = (((rh % c) << 32) + (rl & 0xffffffff)) / c;
315 return res.ll;
318 /***********************************************************/
319 /* host time/date access */
320 void qemu_get_timedate(struct tm *tm, int offset)
322 time_t ti;
323 struct tm *ret;
325 time(&ti);
326 ti += offset;
327 if (rtc_date_offset == -1) {
328 if (rtc_utc)
329 ret = gmtime(&ti);
330 else
331 ret = localtime(&ti);
332 } else {
333 ti -= rtc_date_offset;
334 ret = gmtime(&ti);
337 memcpy(tm, ret, sizeof(struct tm));
340 int qemu_timedate_diff(struct tm *tm)
342 time_t seconds;
344 if (rtc_date_offset == -1)
345 if (rtc_utc)
346 seconds = mktimegm(tm);
347 else
348 seconds = mktime(tm);
349 else
350 seconds = mktimegm(tm) + rtc_date_offset;
352 return seconds - time(NULL);
355 void rtc_change_mon_event(struct tm *tm)
357 QObject *data;
359 data = qobject_from_jsonf("{ 'offset': %d }", qemu_timedate_diff(tm));
360 monitor_protocol_event(QEVENT_RTC_CHANGE, data);
361 qobject_decref(data);
364 static void configure_rtc_date_offset(const char *startdate, int legacy)
366 time_t rtc_start_date;
367 struct tm tm;
369 if (!strcmp(startdate, "now") && legacy) {
370 rtc_date_offset = -1;
371 } else {
372 if (sscanf(startdate, "%d-%d-%dT%d:%d:%d",
373 &tm.tm_year,
374 &tm.tm_mon,
375 &tm.tm_mday,
376 &tm.tm_hour,
377 &tm.tm_min,
378 &tm.tm_sec) == 6) {
379 /* OK */
380 } else if (sscanf(startdate, "%d-%d-%d",
381 &tm.tm_year,
382 &tm.tm_mon,
383 &tm.tm_mday) == 3) {
384 tm.tm_hour = 0;
385 tm.tm_min = 0;
386 tm.tm_sec = 0;
387 } else {
388 goto date_fail;
390 tm.tm_year -= 1900;
391 tm.tm_mon--;
392 rtc_start_date = mktimegm(&tm);
393 if (rtc_start_date == -1) {
394 date_fail:
395 fprintf(stderr, "Invalid date format. Valid formats are:\n"
396 "'2006-06-17T16:01:21' or '2006-06-17'\n");
397 exit(1);
399 rtc_date_offset = time(NULL) - rtc_start_date;
403 static void configure_rtc(QemuOpts *opts)
405 const char *value;
407 value = qemu_opt_get(opts, "base");
408 if (value) {
409 if (!strcmp(value, "utc")) {
410 rtc_utc = 1;
411 } else if (!strcmp(value, "localtime")) {
412 rtc_utc = 0;
413 } else {
414 configure_rtc_date_offset(value, 0);
417 value = qemu_opt_get(opts, "clock");
418 if (value) {
419 if (!strcmp(value, "host")) {
420 rtc_clock = host_clock;
421 } else if (!strcmp(value, "vm")) {
422 rtc_clock = vm_clock;
423 } else {
424 fprintf(stderr, "qemu: invalid option value '%s'\n", value);
425 exit(1);
428 value = qemu_opt_get(opts, "driftfix");
429 if (value) {
430 if (!strcmp(value, "slew")) {
431 rtc_td_hack = 1;
432 } else if (!strcmp(value, "none")) {
433 rtc_td_hack = 0;
434 } else {
435 fprintf(stderr, "qemu: invalid option value '%s'\n", value);
436 exit(1);
441 /***********************************************************/
442 /* Bluetooth support */
443 static int nb_hcis;
444 static int cur_hci;
445 static struct HCIInfo *hci_table[MAX_NICS];
447 static struct bt_vlan_s {
448 struct bt_scatternet_s net;
449 int id;
450 struct bt_vlan_s *next;
451 } *first_bt_vlan;
453 /* find or alloc a new bluetooth "VLAN" */
454 static struct bt_scatternet_s *qemu_find_bt_vlan(int id)
456 struct bt_vlan_s **pvlan, *vlan;
457 for (vlan = first_bt_vlan; vlan != NULL; vlan = vlan->next) {
458 if (vlan->id == id)
459 return &vlan->net;
461 vlan = qemu_mallocz(sizeof(struct bt_vlan_s));
462 vlan->id = id;
463 pvlan = &first_bt_vlan;
464 while (*pvlan != NULL)
465 pvlan = &(*pvlan)->next;
466 *pvlan = vlan;
467 return &vlan->net;
470 static void null_hci_send(struct HCIInfo *hci, const uint8_t *data, int len)
474 static int null_hci_addr_set(struct HCIInfo *hci, const uint8_t *bd_addr)
476 return -ENOTSUP;
479 static struct HCIInfo null_hci = {
480 .cmd_send = null_hci_send,
481 .sco_send = null_hci_send,
482 .acl_send = null_hci_send,
483 .bdaddr_set = null_hci_addr_set,
486 struct HCIInfo *qemu_next_hci(void)
488 if (cur_hci == nb_hcis)
489 return &null_hci;
491 return hci_table[cur_hci++];
494 static struct HCIInfo *hci_init(const char *str)
496 char *endp;
497 struct bt_scatternet_s *vlan = 0;
499 if (!strcmp(str, "null"))
500 /* null */
501 return &null_hci;
502 else if (!strncmp(str, "host", 4) && (str[4] == '\0' || str[4] == ':'))
503 /* host[:hciN] */
504 return bt_host_hci(str[4] ? str + 5 : "hci0");
505 else if (!strncmp(str, "hci", 3)) {
506 /* hci[,vlan=n] */
507 if (str[3]) {
508 if (!strncmp(str + 3, ",vlan=", 6)) {
509 vlan = qemu_find_bt_vlan(strtol(str + 9, &endp, 0));
510 if (*endp)
511 vlan = 0;
513 } else
514 vlan = qemu_find_bt_vlan(0);
515 if (vlan)
516 return bt_new_hci(vlan);
519 fprintf(stderr, "qemu: Unknown bluetooth HCI `%s'.\n", str);
521 return 0;
524 static int bt_hci_parse(const char *str)
526 struct HCIInfo *hci;
527 bdaddr_t bdaddr;
529 if (nb_hcis >= MAX_NICS) {
530 fprintf(stderr, "qemu: Too many bluetooth HCIs (max %i).\n", MAX_NICS);
531 return -1;
534 hci = hci_init(str);
535 if (!hci)
536 return -1;
538 bdaddr.b[0] = 0x52;
539 bdaddr.b[1] = 0x54;
540 bdaddr.b[2] = 0x00;
541 bdaddr.b[3] = 0x12;
542 bdaddr.b[4] = 0x34;
543 bdaddr.b[5] = 0x56 + nb_hcis;
544 hci->bdaddr_set(hci, bdaddr.b);
546 hci_table[nb_hcis++] = hci;
548 return 0;
551 static void bt_vhci_add(int vlan_id)
553 struct bt_scatternet_s *vlan = qemu_find_bt_vlan(vlan_id);
555 if (!vlan->slave)
556 fprintf(stderr, "qemu: warning: adding a VHCI to "
557 "an empty scatternet %i\n", vlan_id);
559 bt_vhci_init(bt_new_hci(vlan));
562 static struct bt_device_s *bt_device_add(const char *opt)
564 struct bt_scatternet_s *vlan;
565 int vlan_id = 0;
566 char *endp = strstr(opt, ",vlan=");
567 int len = (endp ? endp - opt : strlen(opt)) + 1;
568 char devname[10];
570 pstrcpy(devname, MIN(sizeof(devname), len), opt);
572 if (endp) {
573 vlan_id = strtol(endp + 6, &endp, 0);
574 if (*endp) {
575 fprintf(stderr, "qemu: unrecognised bluetooth vlan Id\n");
576 return 0;
580 vlan = qemu_find_bt_vlan(vlan_id);
582 if (!vlan->slave)
583 fprintf(stderr, "qemu: warning: adding a slave device to "
584 "an empty scatternet %i\n", vlan_id);
586 if (!strcmp(devname, "keyboard"))
587 return bt_keyboard_init(vlan);
589 fprintf(stderr, "qemu: unsupported bluetooth device `%s'\n", devname);
590 return 0;
593 static int bt_parse(const char *opt)
595 const char *endp, *p;
596 int vlan;
598 if (strstart(opt, "hci", &endp)) {
599 if (!*endp || *endp == ',') {
600 if (*endp)
601 if (!strstart(endp, ",vlan=", 0))
602 opt = endp + 1;
604 return bt_hci_parse(opt);
606 } else if (strstart(opt, "vhci", &endp)) {
607 if (!*endp || *endp == ',') {
608 if (*endp) {
609 if (strstart(endp, ",vlan=", &p)) {
610 vlan = strtol(p, (char **) &endp, 0);
611 if (*endp) {
612 fprintf(stderr, "qemu: bad scatternet '%s'\n", p);
613 return 1;
615 } else {
616 fprintf(stderr, "qemu: bad parameter '%s'\n", endp + 1);
617 return 1;
619 } else
620 vlan = 0;
622 bt_vhci_add(vlan);
623 return 0;
625 } else if (strstart(opt, "device:", &endp))
626 return !bt_device_add(endp);
628 fprintf(stderr, "qemu: bad bluetooth parameter '%s'\n", opt);
629 return 1;
632 /***********************************************************/
633 /* QEMU Block devices */
635 #define HD_ALIAS "index=%d,media=disk"
636 #define CDROM_ALIAS "index=2,media=cdrom"
637 #define FD_ALIAS "index=%d,if=floppy"
638 #define PFLASH_ALIAS "if=pflash"
639 #define MTD_ALIAS "if=mtd"
640 #define SD_ALIAS "index=0,if=sd"
642 static int drive_init_func(QemuOpts *opts, void *opaque)
644 int *use_scsi = opaque;
645 int fatal_error = 0;
647 if (drive_init(opts, *use_scsi, &fatal_error) == NULL) {
648 if (fatal_error)
649 return 1;
651 return 0;
654 static int drive_enable_snapshot(QemuOpts *opts, void *opaque)
656 if (NULL == qemu_opt_get(opts, "snapshot")) {
657 qemu_opt_set(opts, "snapshot", "on");
659 return 0;
662 void qemu_register_boot_set(QEMUBootSetHandler *func, void *opaque)
664 boot_set_handler = func;
665 boot_set_opaque = opaque;
668 int qemu_boot_set(const char *boot_devices)
670 if (!boot_set_handler) {
671 return -EINVAL;
673 return boot_set_handler(boot_set_opaque, boot_devices);
676 static void validate_bootdevices(char *devices)
678 /* We just do some generic consistency checks */
679 const char *p;
680 int bitmap = 0;
682 for (p = devices; *p != '\0'; p++) {
683 /* Allowed boot devices are:
684 * a-b: floppy disk drives
685 * c-f: IDE disk drives
686 * g-m: machine implementation dependant drives
687 * n-p: network devices
688 * It's up to each machine implementation to check if the given boot
689 * devices match the actual hardware implementation and firmware
690 * features.
692 if (*p < 'a' || *p > 'p') {
693 fprintf(stderr, "Invalid boot device '%c'\n", *p);
694 exit(1);
696 if (bitmap & (1 << (*p - 'a'))) {
697 fprintf(stderr, "Boot device '%c' was given twice\n", *p);
698 exit(1);
700 bitmap |= 1 << (*p - 'a');
704 static void restore_boot_devices(void *opaque)
706 char *standard_boot_devices = opaque;
707 static int first = 1;
709 /* Restore boot order and remove ourselves after the first boot */
710 if (first) {
711 first = 0;
712 return;
715 qemu_boot_set(standard_boot_devices);
717 qemu_unregister_reset(restore_boot_devices, standard_boot_devices);
718 qemu_free(standard_boot_devices);
721 static void numa_add(const char *optarg)
723 char option[128];
724 char *endptr;
725 unsigned long long value, endvalue;
726 int nodenr;
728 optarg = get_opt_name(option, 128, optarg, ',') + 1;
729 if (!strcmp(option, "node")) {
730 if (get_param_value(option, 128, "nodeid", optarg) == 0) {
731 nodenr = nb_numa_nodes;
732 } else {
733 nodenr = strtoull(option, NULL, 10);
736 if (get_param_value(option, 128, "mem", optarg) == 0) {
737 node_mem[nodenr] = 0;
738 } else {
739 value = strtoull(option, &endptr, 0);
740 switch (*endptr) {
741 case 0: case 'M': case 'm':
742 value <<= 20;
743 break;
744 case 'G': case 'g':
745 value <<= 30;
746 break;
748 node_mem[nodenr] = value;
750 if (get_param_value(option, 128, "cpus", optarg) == 0) {
751 node_cpumask[nodenr] = 0;
752 } else {
753 value = strtoull(option, &endptr, 10);
754 if (value >= 64) {
755 value = 63;
756 fprintf(stderr, "only 64 CPUs in NUMA mode supported.\n");
757 } else {
758 if (*endptr == '-') {
759 endvalue = strtoull(endptr+1, &endptr, 10);
760 if (endvalue >= 63) {
761 endvalue = 62;
762 fprintf(stderr,
763 "only 63 CPUs in NUMA mode supported.\n");
765 value = (2ULL << endvalue) - (1ULL << value);
766 } else {
767 value = 1ULL << value;
770 node_cpumask[nodenr] = value;
772 nb_numa_nodes++;
774 return;
777 static void smp_parse(const char *optarg)
779 int smp, sockets = 0, threads = 0, cores = 0;
780 char *endptr;
781 char option[128];
783 smp = strtoul(optarg, &endptr, 10);
784 if (endptr != optarg) {
785 if (*endptr == ',') {
786 endptr++;
789 if (get_param_value(option, 128, "sockets", endptr) != 0)
790 sockets = strtoull(option, NULL, 10);
791 if (get_param_value(option, 128, "cores", endptr) != 0)
792 cores = strtoull(option, NULL, 10);
793 if (get_param_value(option, 128, "threads", endptr) != 0)
794 threads = strtoull(option, NULL, 10);
795 if (get_param_value(option, 128, "maxcpus", endptr) != 0)
796 max_cpus = strtoull(option, NULL, 10);
798 /* compute missing values, prefer sockets over cores over threads */
799 if (smp == 0 || sockets == 0) {
800 sockets = sockets > 0 ? sockets : 1;
801 cores = cores > 0 ? cores : 1;
802 threads = threads > 0 ? threads : 1;
803 if (smp == 0) {
804 smp = cores * threads * sockets;
806 } else {
807 if (cores == 0) {
808 threads = threads > 0 ? threads : 1;
809 cores = smp / (sockets * threads);
810 } else {
811 threads = smp / (cores * sockets);
814 smp_cpus = smp;
815 smp_cores = cores > 0 ? cores : 1;
816 smp_threads = threads > 0 ? threads : 1;
817 if (max_cpus == 0)
818 max_cpus = smp_cpus;
821 /***********************************************************/
822 /* USB devices */
824 static int usb_device_add(const char *devname)
826 const char *p;
827 USBDevice *dev = NULL;
829 if (!usb_enabled)
830 return -1;
832 /* drivers with .usbdevice_name entry in USBDeviceInfo */
833 dev = usbdevice_create(devname);
834 if (dev)
835 goto done;
837 /* the other ones */
838 if (strstart(devname, "host:", &p)) {
839 dev = usb_host_device_open(p);
840 } else if (!strcmp(devname, "bt") || strstart(devname, "bt:", &p)) {
841 dev = usb_bt_init(devname[2] ? hci_init(p) :
842 bt_new_hci(qemu_find_bt_vlan(0)));
843 } else {
844 return -1;
846 if (!dev)
847 return -1;
849 done:
850 return 0;
853 static int usb_device_del(const char *devname)
855 int bus_num, addr;
856 const char *p;
858 if (strstart(devname, "host:", &p))
859 return usb_host_device_close(p);
861 if (!usb_enabled)
862 return -1;
864 p = strchr(devname, '.');
865 if (!p)
866 return -1;
867 bus_num = strtoul(devname, NULL, 0);
868 addr = strtoul(p + 1, NULL, 0);
870 return usb_device_delete_addr(bus_num, addr);
873 static int usb_parse(const char *cmdline)
875 int r;
876 r = usb_device_add(cmdline);
877 if (r < 0) {
878 fprintf(stderr, "qemu: could not add USB device '%s'\n", cmdline);
880 return r;
883 void do_usb_add(Monitor *mon, const QDict *qdict)
885 const char *devname = qdict_get_str(qdict, "devname");
886 if (usb_device_add(devname) < 0) {
887 error_report("could not add USB device '%s'", devname);
891 void do_usb_del(Monitor *mon, const QDict *qdict)
893 const char *devname = qdict_get_str(qdict, "devname");
894 if (usb_device_del(devname) < 0) {
895 error_report("could not delete USB device '%s'", devname);
899 /***********************************************************/
900 /* PCMCIA/Cardbus */
902 static struct pcmcia_socket_entry_s {
903 PCMCIASocket *socket;
904 struct pcmcia_socket_entry_s *next;
905 } *pcmcia_sockets = 0;
907 void pcmcia_socket_register(PCMCIASocket *socket)
909 struct pcmcia_socket_entry_s *entry;
911 entry = qemu_malloc(sizeof(struct pcmcia_socket_entry_s));
912 entry->socket = socket;
913 entry->next = pcmcia_sockets;
914 pcmcia_sockets = entry;
917 void pcmcia_socket_unregister(PCMCIASocket *socket)
919 struct pcmcia_socket_entry_s *entry, **ptr;
921 ptr = &pcmcia_sockets;
922 for (entry = *ptr; entry; ptr = &entry->next, entry = *ptr)
923 if (entry->socket == socket) {
924 *ptr = entry->next;
925 qemu_free(entry);
929 void pcmcia_info(Monitor *mon)
931 struct pcmcia_socket_entry_s *iter;
933 if (!pcmcia_sockets)
934 monitor_printf(mon, "No PCMCIA sockets\n");
936 for (iter = pcmcia_sockets; iter; iter = iter->next)
937 monitor_printf(mon, "%s: %s\n", iter->socket->slot_string,
938 iter->socket->attached ? iter->socket->card_string :
939 "Empty");
942 /***********************************************************/
943 /* I/O handling */
945 typedef struct IOHandlerRecord {
946 int fd;
947 IOCanReadHandler *fd_read_poll;
948 IOHandler *fd_read;
949 IOHandler *fd_write;
950 int deleted;
951 void *opaque;
952 /* temporary data */
953 struct pollfd *ufd;
954 QLIST_ENTRY(IOHandlerRecord) next;
955 } IOHandlerRecord;
957 static QLIST_HEAD(, IOHandlerRecord) io_handlers =
958 QLIST_HEAD_INITIALIZER(io_handlers);
961 /* XXX: fd_read_poll should be suppressed, but an API change is
962 necessary in the character devices to suppress fd_can_read(). */
963 int qemu_set_fd_handler2(int fd,
964 IOCanReadHandler *fd_read_poll,
965 IOHandler *fd_read,
966 IOHandler *fd_write,
967 void *opaque)
969 IOHandlerRecord *ioh;
971 if (!fd_read && !fd_write) {
972 QLIST_FOREACH(ioh, &io_handlers, next) {
973 if (ioh->fd == fd) {
974 ioh->deleted = 1;
975 break;
978 } else {
979 QLIST_FOREACH(ioh, &io_handlers, next) {
980 if (ioh->fd == fd)
981 goto found;
983 ioh = qemu_mallocz(sizeof(IOHandlerRecord));
984 QLIST_INSERT_HEAD(&io_handlers, ioh, next);
985 found:
986 ioh->fd = fd;
987 ioh->fd_read_poll = fd_read_poll;
988 ioh->fd_read = fd_read;
989 ioh->fd_write = fd_write;
990 ioh->opaque = opaque;
991 ioh->deleted = 0;
993 qemu_notify_event();
994 return 0;
997 int qemu_set_fd_handler(int fd,
998 IOHandler *fd_read,
999 IOHandler *fd_write,
1000 void *opaque)
1002 return qemu_set_fd_handler2(fd, NULL, fd_read, fd_write, opaque);
1005 /***********************************************************/
1006 /* machine registration */
1008 static QEMUMachine *first_machine = NULL;
1009 QEMUMachine *current_machine = NULL;
1011 int qemu_register_machine(QEMUMachine *m)
1013 QEMUMachine **pm;
1014 pm = &first_machine;
1015 while (*pm != NULL)
1016 pm = &(*pm)->next;
1017 m->next = NULL;
1018 *pm = m;
1019 return 0;
1022 static QEMUMachine *find_machine(const char *name)
1024 QEMUMachine *m;
1026 for(m = first_machine; m != NULL; m = m->next) {
1027 if (!strcmp(m->name, name))
1028 return m;
1029 if (m->alias && !strcmp(m->alias, name))
1030 return m;
1032 return NULL;
1035 static QEMUMachine *find_default_machine(void)
1037 QEMUMachine *m;
1039 for(m = first_machine; m != NULL; m = m->next) {
1040 if (m->is_default) {
1041 return m;
1044 return NULL;
1047 /***********************************************************/
1048 /* main execution loop */
1050 static void gui_update(void *opaque)
1052 uint64_t interval = GUI_REFRESH_INTERVAL;
1053 DisplayState *ds = opaque;
1054 DisplayChangeListener *dcl = ds->listeners;
1056 qemu_flush_coalesced_mmio_buffer();
1057 dpy_refresh(ds);
1059 while (dcl != NULL) {
1060 if (dcl->gui_timer_interval &&
1061 dcl->gui_timer_interval < interval)
1062 interval = dcl->gui_timer_interval;
1063 dcl = dcl->next;
1065 qemu_mod_timer(ds->gui_timer, interval + qemu_get_clock(rt_clock));
1068 static void nographic_update(void *opaque)
1070 uint64_t interval = GUI_REFRESH_INTERVAL;
1072 qemu_flush_coalesced_mmio_buffer();
1073 qemu_mod_timer(nographic_timer, interval + qemu_get_clock(rt_clock));
1076 struct vm_change_state_entry {
1077 VMChangeStateHandler *cb;
1078 void *opaque;
1079 QLIST_ENTRY (vm_change_state_entry) entries;
1082 static QLIST_HEAD(vm_change_state_head, vm_change_state_entry) vm_change_state_head;
1084 VMChangeStateEntry *qemu_add_vm_change_state_handler(VMChangeStateHandler *cb,
1085 void *opaque)
1087 VMChangeStateEntry *e;
1089 e = qemu_mallocz(sizeof (*e));
1091 e->cb = cb;
1092 e->opaque = opaque;
1093 QLIST_INSERT_HEAD(&vm_change_state_head, e, entries);
1094 return e;
1097 void qemu_del_vm_change_state_handler(VMChangeStateEntry *e)
1099 QLIST_REMOVE (e, entries);
1100 qemu_free (e);
1103 void vm_state_notify(int running, int reason)
1105 VMChangeStateEntry *e;
1107 for (e = vm_change_state_head.lh_first; e; e = e->entries.le_next) {
1108 e->cb(e->opaque, running, reason);
1112 void vm_start(void)
1114 if (!vm_running) {
1115 cpu_enable_ticks();
1116 vm_running = 1;
1117 vm_state_notify(1, 0);
1118 resume_all_vcpus();
1119 monitor_protocol_event(QEVENT_RESUME, NULL);
1123 /* reset/shutdown handler */
1125 typedef struct QEMUResetEntry {
1126 QTAILQ_ENTRY(QEMUResetEntry) entry;
1127 QEMUResetHandler *func;
1128 void *opaque;
1129 } QEMUResetEntry;
1131 static QTAILQ_HEAD(reset_handlers, QEMUResetEntry) reset_handlers =
1132 QTAILQ_HEAD_INITIALIZER(reset_handlers);
1133 static int reset_requested;
1134 static int shutdown_requested;
1135 static int powerdown_requested;
1136 int debug_requested;
1137 int vmstop_requested;
1139 int qemu_no_shutdown(void)
1141 int r = no_shutdown;
1142 no_shutdown = 0;
1143 return r;
1146 int qemu_shutdown_requested(void)
1148 int r = shutdown_requested;
1149 shutdown_requested = 0;
1150 return r;
1153 int qemu_reset_requested(void)
1155 int r = reset_requested;
1156 reset_requested = 0;
1157 return r;
1160 int qemu_powerdown_requested(void)
1162 int r = powerdown_requested;
1163 powerdown_requested = 0;
1164 return r;
1167 static int qemu_debug_requested(void)
1169 int r = debug_requested;
1170 debug_requested = 0;
1171 return r;
1174 static int qemu_vmstop_requested(void)
1176 int r = vmstop_requested;
1177 vmstop_requested = 0;
1178 return r;
1181 void qemu_register_reset(QEMUResetHandler *func, void *opaque)
1183 QEMUResetEntry *re = qemu_mallocz(sizeof(QEMUResetEntry));
1185 re->func = func;
1186 re->opaque = opaque;
1187 QTAILQ_INSERT_TAIL(&reset_handlers, re, entry);
1190 void qemu_unregister_reset(QEMUResetHandler *func, void *opaque)
1192 QEMUResetEntry *re;
1194 QTAILQ_FOREACH(re, &reset_handlers, entry) {
1195 if (re->func == func && re->opaque == opaque) {
1196 QTAILQ_REMOVE(&reset_handlers, re, entry);
1197 qemu_free(re);
1198 return;
1203 void qemu_system_reset(void)
1205 QEMUResetEntry *re, *nre;
1207 /* reset all devices */
1208 QTAILQ_FOREACH_SAFE(re, &reset_handlers, entry, nre) {
1209 re->func(re->opaque);
1211 monitor_protocol_event(QEVENT_RESET, NULL);
1212 cpu_synchronize_all_post_reset();
1215 void qemu_system_reset_request(void)
1217 if (no_reboot) {
1218 shutdown_requested = 1;
1219 } else {
1220 reset_requested = 1;
1222 if (cpu_single_env) {
1223 cpu_single_env->stopped = 1;
1224 cpu_exit(cpu_single_env);
1226 qemu_notify_event();
1229 void qemu_system_shutdown_request(void)
1231 shutdown_requested = 1;
1232 qemu_notify_event();
1235 void qemu_system_powerdown_request(void)
1237 powerdown_requested = 1;
1238 qemu_notify_event();
1241 void main_loop_wait(int nonblocking)
1243 IOHandlerRecord *ioh;
1244 fd_set rfds, wfds, xfds;
1245 int ret, nfds;
1246 struct timeval tv;
1247 int timeout;
1249 if (nonblocking)
1250 timeout = 0;
1251 else {
1252 timeout = qemu_calculate_timeout();
1253 qemu_bh_update_timeout(&timeout);
1256 os_host_main_loop_wait(&timeout);
1258 /* poll any events */
1259 /* XXX: separate device handlers from system ones */
1260 nfds = -1;
1261 FD_ZERO(&rfds);
1262 FD_ZERO(&wfds);
1263 FD_ZERO(&xfds);
1264 QLIST_FOREACH(ioh, &io_handlers, next) {
1265 if (ioh->deleted)
1266 continue;
1267 if (ioh->fd_read &&
1268 (!ioh->fd_read_poll ||
1269 ioh->fd_read_poll(ioh->opaque) != 0)) {
1270 FD_SET(ioh->fd, &rfds);
1271 if (ioh->fd > nfds)
1272 nfds = ioh->fd;
1274 if (ioh->fd_write) {
1275 FD_SET(ioh->fd, &wfds);
1276 if (ioh->fd > nfds)
1277 nfds = ioh->fd;
1281 tv.tv_sec = timeout / 1000;
1282 tv.tv_usec = (timeout % 1000) * 1000;
1284 slirp_select_fill(&nfds, &rfds, &wfds, &xfds);
1286 qemu_mutex_unlock_iothread();
1287 ret = select(nfds + 1, &rfds, &wfds, &xfds, &tv);
1288 qemu_mutex_lock_iothread();
1289 if (ret > 0) {
1290 IOHandlerRecord *pioh;
1292 QLIST_FOREACH_SAFE(ioh, &io_handlers, next, pioh) {
1293 if (ioh->deleted) {
1294 QLIST_REMOVE(ioh, next);
1295 qemu_free(ioh);
1296 continue;
1298 if (ioh->fd_read && FD_ISSET(ioh->fd, &rfds)) {
1299 ioh->fd_read(ioh->opaque);
1300 if (!(ioh->fd_read_poll && ioh->fd_read_poll(ioh->opaque)))
1301 FD_CLR(ioh->fd, &rfds);
1303 if (ioh->fd_write && FD_ISSET(ioh->fd, &wfds)) {
1304 ioh->fd_write(ioh->opaque);
1309 slirp_select_poll(&rfds, &wfds, &xfds, (ret < 0));
1311 qemu_run_all_timers();
1313 /* Check bottom-halves last in case any of the earlier events triggered
1314 them. */
1315 qemu_bh_poll();
1319 static int vm_can_run(void)
1321 if (powerdown_requested)
1322 return 0;
1323 if (reset_requested)
1324 return 0;
1325 if (shutdown_requested)
1326 return 0;
1327 if (debug_requested)
1328 return 0;
1329 return 1;
1332 qemu_irq qemu_system_powerdown;
1334 static void main_loop(void)
1336 int r;
1338 if (kvm_enabled()) {
1339 kvm_main_loop();
1340 cpu_disable_ticks();
1341 return;
1344 qemu_main_loop_start();
1346 for (;;) {
1347 do {
1348 bool nonblocking = false;
1349 #ifdef CONFIG_PROFILER
1350 int64_t ti;
1351 #endif
1352 #ifndef CONFIG_IOTHREAD
1353 nonblocking = cpu_exec_all();
1354 #endif
1355 #ifdef CONFIG_PROFILER
1356 ti = profile_getclock();
1357 #endif
1358 main_loop_wait(nonblocking);
1359 #ifdef CONFIG_PROFILER
1360 dev_time += profile_getclock() - ti;
1361 #endif
1362 } while (vm_can_run());
1364 if ((r = qemu_debug_requested())) {
1365 vm_stop(r);
1367 if (qemu_shutdown_requested()) {
1368 monitor_protocol_event(QEVENT_SHUTDOWN, NULL);
1369 if (no_shutdown) {
1370 vm_stop(0);
1371 no_shutdown = 0;
1372 } else
1373 break;
1375 if (qemu_reset_requested()) {
1376 pause_all_vcpus();
1377 qemu_system_reset();
1378 resume_all_vcpus();
1380 if (qemu_powerdown_requested()) {
1381 monitor_protocol_event(QEVENT_POWERDOWN, NULL);
1382 qemu_irq_raise(qemu_system_powerdown);
1384 if ((r = qemu_vmstop_requested())) {
1385 vm_stop(r);
1388 bdrv_close_all();
1389 pause_all_vcpus();
1392 static void version(void)
1394 printf("QEMU emulator version " QEMU_VERSION QEMU_PKGVERSION ", Copyright (c) 2003-2008 Fabrice Bellard\n");
1397 static void help(int exitcode)
1399 const char *options_help =
1400 #define DEF(option, opt_arg, opt_enum, opt_help, arch_mask) \
1401 opt_help
1402 #define DEFHEADING(text) stringify(text) "\n"
1403 #include "qemu-options.def"
1404 #undef DEF
1405 #undef DEFHEADING
1406 #undef GEN_DOCS
1408 version();
1409 printf("usage: %s [options] [disk_image]\n"
1410 "\n"
1411 "'disk_image' is a raw hard disk image for IDE hard disk 0\n"
1412 "\n"
1413 "%s\n"
1414 "During emulation, the following keys are useful:\n"
1415 "ctrl-alt-f toggle full screen\n"
1416 "ctrl-alt-n switch to virtual console 'n'\n"
1417 "ctrl-alt toggle mouse and keyboard grab\n"
1418 "\n"
1419 "When using -nographic, press 'ctrl-a h' to get some help.\n",
1420 "qemu",
1421 options_help);
1422 exit(exitcode);
1425 #define HAS_ARG 0x0001
1427 typedef struct QEMUOption {
1428 const char *name;
1429 int flags;
1430 int index;
1431 uint32_t arch_mask;
1432 } QEMUOption;
1434 static const QEMUOption qemu_options[] = {
1435 { "h", 0, QEMU_OPTION_h, QEMU_ARCH_ALL },
1436 #define DEF(option, opt_arg, opt_enum, opt_help, arch_mask) \
1437 { option, opt_arg, opt_enum, arch_mask },
1438 #define DEFHEADING(text)
1439 #include "qemu-options.def"
1440 #undef DEF
1441 #undef DEFHEADING
1442 #undef GEN_DOCS
1443 { NULL },
1445 static void select_vgahw (const char *p)
1447 const char *opts;
1449 default_vga = 0;
1450 vga_interface_type = VGA_NONE;
1451 if (strstart(p, "std", &opts)) {
1452 vga_interface_type = VGA_STD;
1453 } else if (strstart(p, "cirrus", &opts)) {
1454 vga_interface_type = VGA_CIRRUS;
1455 } else if (strstart(p, "vmware", &opts)) {
1456 vga_interface_type = VGA_VMWARE;
1457 } else if (strstart(p, "xenfb", &opts)) {
1458 vga_interface_type = VGA_XENFB;
1459 } else if (!strstart(p, "none", &opts)) {
1460 invalid_vga:
1461 fprintf(stderr, "Unknown vga type: %s\n", p);
1462 exit(1);
1464 while (*opts) {
1465 const char *nextopt;
1467 if (strstart(opts, ",retrace=", &nextopt)) {
1468 opts = nextopt;
1469 if (strstart(opts, "dumb", &nextopt))
1470 vga_retrace_method = VGA_RETRACE_DUMB;
1471 else if (strstart(opts, "precise", &nextopt))
1472 vga_retrace_method = VGA_RETRACE_PRECISE;
1473 else goto invalid_vga;
1474 } else goto invalid_vga;
1475 opts = nextopt;
1479 static int balloon_parse(const char *arg)
1481 QemuOpts *opts;
1483 if (strcmp(arg, "none") == 0) {
1484 return 0;
1487 if (!strncmp(arg, "virtio", 6)) {
1488 if (arg[6] == ',') {
1489 /* have params -> parse them */
1490 opts = qemu_opts_parse(&qemu_device_opts, arg+7, 0);
1491 if (!opts)
1492 return -1;
1493 } else {
1494 /* create empty opts */
1495 opts = qemu_opts_create(&qemu_device_opts, NULL, 0);
1497 qemu_opt_set(opts, "driver", "virtio-balloon-pci");
1498 return 0;
1501 return -1;
1504 char *qemu_find_file(int type, const char *name)
1506 int len;
1507 const char *subdir;
1508 char *buf;
1510 /* If name contains path separators then try it as a straight path. */
1511 if ((strchr(name, '/') || strchr(name, '\\'))
1512 && access(name, R_OK) == 0) {
1513 return qemu_strdup(name);
1515 switch (type) {
1516 case QEMU_FILE_TYPE_BIOS:
1517 subdir = "";
1518 break;
1519 case QEMU_FILE_TYPE_KEYMAP:
1520 subdir = "keymaps/";
1521 break;
1522 default:
1523 abort();
1525 len = strlen(data_dir) + strlen(name) + strlen(subdir) + 2;
1526 buf = qemu_mallocz(len);
1527 snprintf(buf, len, "%s/%s%s", data_dir, subdir, name);
1528 if (access(buf, R_OK)) {
1529 qemu_free(buf);
1530 return NULL;
1532 return buf;
1535 static int device_help_func(QemuOpts *opts, void *opaque)
1537 return qdev_device_help(opts);
1540 static int device_init_func(QemuOpts *opts, void *opaque)
1542 DeviceState *dev;
1544 dev = qdev_device_add(opts);
1545 if (!dev)
1546 return -1;
1547 return 0;
1550 static int chardev_init_func(QemuOpts *opts, void *opaque)
1552 CharDriverState *chr;
1554 chr = qemu_chr_open_opts(opts, NULL);
1555 if (!chr)
1556 return -1;
1557 return 0;
1560 #ifdef CONFIG_VIRTFS
1561 static int fsdev_init_func(QemuOpts *opts, void *opaque)
1563 int ret;
1564 ret = qemu_fsdev_add(opts);
1566 return ret;
1568 #endif
1570 static int mon_init_func(QemuOpts *opts, void *opaque)
1572 CharDriverState *chr;
1573 const char *chardev;
1574 const char *mode;
1575 int flags;
1577 mode = qemu_opt_get(opts, "mode");
1578 if (mode == NULL) {
1579 mode = "readline";
1581 if (strcmp(mode, "readline") == 0) {
1582 flags = MONITOR_USE_READLINE;
1583 } else if (strcmp(mode, "control") == 0) {
1584 flags = MONITOR_USE_CONTROL;
1585 } else {
1586 fprintf(stderr, "unknown monitor mode \"%s\"\n", mode);
1587 exit(1);
1590 if (qemu_opt_get_bool(opts, "default", 0))
1591 flags |= MONITOR_IS_DEFAULT;
1593 chardev = qemu_opt_get(opts, "chardev");
1594 chr = qemu_chr_find(chardev);
1595 if (chr == NULL) {
1596 fprintf(stderr, "chardev \"%s\" not found\n", chardev);
1597 exit(1);
1600 monitor_init(chr, flags);
1601 return 0;
1604 static void monitor_parse(const char *optarg, const char *mode)
1606 static int monitor_device_index = 0;
1607 QemuOpts *opts;
1608 const char *p;
1609 char label[32];
1610 int def = 0;
1612 if (strstart(optarg, "chardev:", &p)) {
1613 snprintf(label, sizeof(label), "%s", p);
1614 } else {
1615 snprintf(label, sizeof(label), "compat_monitor%d",
1616 monitor_device_index);
1617 if (monitor_device_index == 0) {
1618 def = 1;
1620 opts = qemu_chr_parse_compat(label, optarg);
1621 if (!opts) {
1622 fprintf(stderr, "parse error: %s\n", optarg);
1623 exit(1);
1627 opts = qemu_opts_create(&qemu_mon_opts, label, 1);
1628 if (!opts) {
1629 fprintf(stderr, "duplicate chardev: %s\n", label);
1630 exit(1);
1632 qemu_opt_set(opts, "mode", mode);
1633 qemu_opt_set(opts, "chardev", label);
1634 if (def)
1635 qemu_opt_set(opts, "default", "on");
1636 monitor_device_index++;
1639 struct device_config {
1640 enum {
1641 DEV_USB, /* -usbdevice */
1642 DEV_BT, /* -bt */
1643 DEV_SERIAL, /* -serial */
1644 DEV_PARALLEL, /* -parallel */
1645 DEV_VIRTCON, /* -virtioconsole */
1646 DEV_DEBUGCON, /* -debugcon */
1647 } type;
1648 const char *cmdline;
1649 QTAILQ_ENTRY(device_config) next;
1651 QTAILQ_HEAD(, device_config) device_configs = QTAILQ_HEAD_INITIALIZER(device_configs);
1653 static void add_device_config(int type, const char *cmdline)
1655 struct device_config *conf;
1657 conf = qemu_mallocz(sizeof(*conf));
1658 conf->type = type;
1659 conf->cmdline = cmdline;
1660 QTAILQ_INSERT_TAIL(&device_configs, conf, next);
1663 static int foreach_device_config(int type, int (*func)(const char *cmdline))
1665 struct device_config *conf;
1666 int rc;
1668 QTAILQ_FOREACH(conf, &device_configs, next) {
1669 if (conf->type != type)
1670 continue;
1671 rc = func(conf->cmdline);
1672 if (0 != rc)
1673 return rc;
1675 return 0;
1678 static int serial_parse(const char *devname)
1680 static int index = 0;
1681 char label[32];
1683 if (strcmp(devname, "none") == 0)
1684 return 0;
1685 if (index == MAX_SERIAL_PORTS) {
1686 fprintf(stderr, "qemu: too many serial ports\n");
1687 exit(1);
1689 snprintf(label, sizeof(label), "serial%d", index);
1690 serial_hds[index] = qemu_chr_open(label, devname, NULL);
1691 if (!serial_hds[index]) {
1692 fprintf(stderr, "qemu: could not open serial device '%s': %s\n",
1693 devname, strerror(errno));
1694 return -1;
1696 index++;
1697 return 0;
1700 static int parallel_parse(const char *devname)
1702 static int index = 0;
1703 char label[32];
1705 if (strcmp(devname, "none") == 0)
1706 return 0;
1707 if (index == MAX_PARALLEL_PORTS) {
1708 fprintf(stderr, "qemu: too many parallel ports\n");
1709 exit(1);
1711 snprintf(label, sizeof(label), "parallel%d", index);
1712 parallel_hds[index] = qemu_chr_open(label, devname, NULL);
1713 if (!parallel_hds[index]) {
1714 fprintf(stderr, "qemu: could not open parallel device '%s': %s\n",
1715 devname, strerror(errno));
1716 return -1;
1718 index++;
1719 return 0;
1722 static int virtcon_parse(const char *devname)
1724 static int index = 0;
1725 char label[32];
1726 QemuOpts *bus_opts, *dev_opts;
1728 if (strcmp(devname, "none") == 0)
1729 return 0;
1730 if (index == MAX_VIRTIO_CONSOLES) {
1731 fprintf(stderr, "qemu: too many virtio consoles\n");
1732 exit(1);
1735 bus_opts = qemu_opts_create(&qemu_device_opts, NULL, 0);
1736 qemu_opt_set(bus_opts, "driver", "virtio-serial");
1738 dev_opts = qemu_opts_create(&qemu_device_opts, NULL, 0);
1739 qemu_opt_set(dev_opts, "driver", "virtconsole");
1741 snprintf(label, sizeof(label), "virtcon%d", index);
1742 virtcon_hds[index] = qemu_chr_open(label, devname, NULL);
1743 if (!virtcon_hds[index]) {
1744 fprintf(stderr, "qemu: could not open virtio console '%s': %s\n",
1745 devname, strerror(errno));
1746 return -1;
1748 qemu_opt_set(dev_opts, "chardev", label);
1750 index++;
1751 return 0;
1754 static int debugcon_parse(const char *devname)
1756 QemuOpts *opts;
1758 if (!qemu_chr_open("debugcon", devname, NULL)) {
1759 exit(1);
1761 opts = qemu_opts_create(&qemu_device_opts, "debugcon", 1);
1762 if (!opts) {
1763 fprintf(stderr, "qemu: already have a debugcon device\n");
1764 exit(1);
1766 qemu_opt_set(opts, "driver", "isa-debugcon");
1767 qemu_opt_set(opts, "chardev", "debugcon");
1768 return 0;
1771 void qemu_add_exit_notifier(Notifier *notify)
1773 notifier_list_add(&exit_notifiers, notify);
1776 void qemu_remove_exit_notifier(Notifier *notify)
1778 notifier_list_remove(&exit_notifiers, notify);
1781 static void qemu_run_exit_notifiers(void)
1783 notifier_list_notify(&exit_notifiers);
1786 static const QEMUOption *lookup_opt(int argc, char **argv,
1787 const char **poptarg, int *poptind)
1789 const QEMUOption *popt;
1790 int optind = *poptind;
1791 char *r = argv[optind];
1792 const char *optarg;
1794 loc_set_cmdline(argv, optind, 1);
1795 optind++;
1796 /* Treat --foo the same as -foo. */
1797 if (r[1] == '-')
1798 r++;
1799 popt = qemu_options;
1800 for(;;) {
1801 if (!popt->name) {
1802 error_report("invalid option");
1803 exit(1);
1805 if (!strcmp(popt->name, r + 1))
1806 break;
1807 popt++;
1809 if (popt->flags & HAS_ARG) {
1810 if (optind >= argc) {
1811 error_report("requires an argument");
1812 exit(1);
1814 optarg = argv[optind++];
1815 loc_set_cmdline(argv, optind - 2, 2);
1816 } else {
1817 optarg = NULL;
1820 *poptarg = optarg;
1821 *poptind = optind;
1823 return popt;
1826 int main(int argc, char **argv, char **envp)
1828 const char *gdbstub_dev = NULL;
1829 int i;
1830 int snapshot, linux_boot;
1831 const char *icount_option = NULL;
1832 const char *initrd_filename;
1833 const char *kernel_filename, *kernel_cmdline;
1834 char boot_devices[33] = "cad"; /* default to HD->floppy->CD-ROM */
1835 DisplayState *ds;
1836 DisplayChangeListener *dcl;
1837 int cyls, heads, secs, translation;
1838 QemuOpts *hda_opts = NULL, *opts;
1839 int optind;
1840 const char *optarg;
1841 const char *loadvm = NULL;
1842 QEMUMachine *machine;
1843 const char *cpu_model;
1844 int tb_size;
1845 const char *pid_file = NULL;
1846 const char *incoming = NULL;
1847 int show_vnc_port = 0;
1848 int defconfig = 1;
1850 atexit(qemu_run_exit_notifiers);
1851 error_set_progname(argv[0]);
1853 init_clocks();
1855 qemu_cache_utils_init(envp);
1857 QLIST_INIT (&vm_change_state_head);
1858 os_setup_early_signal_handling();
1860 module_call_init(MODULE_INIT_MACHINE);
1861 machine = find_default_machine();
1862 cpu_model = NULL;
1863 initrd_filename = NULL;
1864 ram_size = 0;
1865 snapshot = 0;
1866 kernel_filename = NULL;
1867 kernel_cmdline = "";
1868 cyls = heads = secs = 0;
1869 translation = BIOS_ATA_TRANSLATION_AUTO;
1871 for (i = 0; i < MAX_NODES; i++) {
1872 node_mem[i] = 0;
1873 node_cpumask[i] = 0;
1876 assigned_devices_index = 0;
1878 nb_numa_nodes = 0;
1879 nb_nics = 0;
1881 tb_size = 0;
1882 autostart= 1;
1884 /* first pass of option parsing */
1885 optind = 1;
1886 while (optind < argc) {
1887 if (argv[optind][0] != '-') {
1888 /* disk image */
1889 optind++;
1890 continue;
1891 } else {
1892 const QEMUOption *popt;
1894 popt = lookup_opt(argc, argv, &optarg, &optind);
1895 switch (popt->index) {
1896 case QEMU_OPTION_nodefconfig:
1897 defconfig=0;
1898 break;
1903 if (defconfig) {
1904 int ret;
1906 ret = qemu_read_config_file(CONFIG_QEMU_CONFDIR "/qemu.conf");
1907 if (ret < 0 && ret != -ENOENT) {
1908 exit(1);
1911 ret = qemu_read_config_file(arch_config_name);
1912 if (ret < 0 && ret != -ENOENT) {
1913 exit(1);
1916 cpudef_init();
1918 /* second pass of option parsing */
1919 optind = 1;
1920 for(;;) {
1921 if (optind >= argc)
1922 break;
1923 if (argv[optind][0] != '-') {
1924 hda_opts = drive_add(argv[optind++], HD_ALIAS, 0);
1925 } else {
1926 const QEMUOption *popt;
1928 popt = lookup_opt(argc, argv, &optarg, &optind);
1929 if (!(popt->arch_mask & arch_type)) {
1930 printf("Option %s not supported for this target\n", popt->name);
1931 exit(1);
1933 switch(popt->index) {
1934 case QEMU_OPTION_M:
1935 machine = find_machine(optarg);
1936 if (!machine) {
1937 QEMUMachine *m;
1938 printf("Supported machines are:\n");
1939 for(m = first_machine; m != NULL; m = m->next) {
1940 if (m->alias)
1941 printf("%-10s %s (alias of %s)\n",
1942 m->alias, m->desc, m->name);
1943 printf("%-10s %s%s\n",
1944 m->name, m->desc,
1945 m->is_default ? " (default)" : "");
1947 exit(*optarg != '?');
1949 break;
1950 case QEMU_OPTION_cpu:
1951 /* hw initialization will check this */
1952 if (*optarg == '?') {
1953 list_cpus(stdout, &fprintf, optarg);
1954 exit(0);
1955 } else {
1956 cpu_model = optarg;
1958 break;
1959 case QEMU_OPTION_initrd:
1960 initrd_filename = optarg;
1961 break;
1962 case QEMU_OPTION_hda:
1963 if (cyls == 0)
1964 hda_opts = drive_add(optarg, HD_ALIAS, 0);
1965 else
1966 hda_opts = drive_add(optarg, HD_ALIAS
1967 ",cyls=%d,heads=%d,secs=%d%s",
1968 0, cyls, heads, secs,
1969 translation == BIOS_ATA_TRANSLATION_LBA ?
1970 ",trans=lba" :
1971 translation == BIOS_ATA_TRANSLATION_NONE ?
1972 ",trans=none" : "");
1973 break;
1974 case QEMU_OPTION_hdb:
1975 case QEMU_OPTION_hdc:
1976 case QEMU_OPTION_hdd:
1977 drive_add(optarg, HD_ALIAS, popt->index - QEMU_OPTION_hda);
1978 break;
1979 case QEMU_OPTION_drive:
1980 drive_add(NULL, "%s", optarg);
1981 break;
1982 case QEMU_OPTION_set:
1983 if (qemu_set_option(optarg) != 0)
1984 exit(1);
1985 break;
1986 case QEMU_OPTION_global:
1987 if (qemu_global_option(optarg) != 0)
1988 exit(1);
1989 break;
1990 case QEMU_OPTION_mtdblock:
1991 drive_add(optarg, MTD_ALIAS);
1992 break;
1993 case QEMU_OPTION_sd:
1994 drive_add(optarg, SD_ALIAS);
1995 break;
1996 case QEMU_OPTION_pflash:
1997 drive_add(optarg, PFLASH_ALIAS);
1998 break;
1999 case QEMU_OPTION_snapshot:
2000 snapshot = 1;
2001 break;
2002 case QEMU_OPTION_hdachs:
2004 const char *p;
2005 p = optarg;
2006 cyls = strtol(p, (char **)&p, 0);
2007 if (cyls < 1 || cyls > 16383)
2008 goto chs_fail;
2009 if (*p != ',')
2010 goto chs_fail;
2011 p++;
2012 heads = strtol(p, (char **)&p, 0);
2013 if (heads < 1 || heads > 16)
2014 goto chs_fail;
2015 if (*p != ',')
2016 goto chs_fail;
2017 p++;
2018 secs = strtol(p, (char **)&p, 0);
2019 if (secs < 1 || secs > 63)
2020 goto chs_fail;
2021 if (*p == ',') {
2022 p++;
2023 if (!strcmp(p, "none"))
2024 translation = BIOS_ATA_TRANSLATION_NONE;
2025 else if (!strcmp(p, "lba"))
2026 translation = BIOS_ATA_TRANSLATION_LBA;
2027 else if (!strcmp(p, "auto"))
2028 translation = BIOS_ATA_TRANSLATION_AUTO;
2029 else
2030 goto chs_fail;
2031 } else if (*p != '\0') {
2032 chs_fail:
2033 fprintf(stderr, "qemu: invalid physical CHS format\n");
2034 exit(1);
2036 if (hda_opts != NULL) {
2037 char num[16];
2038 snprintf(num, sizeof(num), "%d", cyls);
2039 qemu_opt_set(hda_opts, "cyls", num);
2040 snprintf(num, sizeof(num), "%d", heads);
2041 qemu_opt_set(hda_opts, "heads", num);
2042 snprintf(num, sizeof(num), "%d", secs);
2043 qemu_opt_set(hda_opts, "secs", num);
2044 if (translation == BIOS_ATA_TRANSLATION_LBA)
2045 qemu_opt_set(hda_opts, "trans", "lba");
2046 if (translation == BIOS_ATA_TRANSLATION_NONE)
2047 qemu_opt_set(hda_opts, "trans", "none");
2050 break;
2051 case QEMU_OPTION_numa:
2052 if (nb_numa_nodes >= MAX_NODES) {
2053 fprintf(stderr, "qemu: too many NUMA nodes\n");
2054 exit(1);
2056 numa_add(optarg);
2057 break;
2058 case QEMU_OPTION_nographic:
2059 display_type = DT_NOGRAPHIC;
2060 break;
2061 #ifdef CONFIG_CURSES
2062 case QEMU_OPTION_curses:
2063 display_type = DT_CURSES;
2064 break;
2065 #endif
2066 case QEMU_OPTION_portrait:
2067 graphic_rotate = 1;
2068 break;
2069 case QEMU_OPTION_kernel:
2070 kernel_filename = optarg;
2071 break;
2072 case QEMU_OPTION_append:
2073 kernel_cmdline = optarg;
2074 break;
2075 case QEMU_OPTION_cdrom:
2076 drive_add(optarg, CDROM_ALIAS);
2077 break;
2078 case QEMU_OPTION_boot:
2080 static const char * const params[] = {
2081 "order", "once", "menu", NULL
2083 char buf[sizeof(boot_devices)];
2084 char *standard_boot_devices;
2085 int legacy = 0;
2087 if (!strchr(optarg, '=')) {
2088 legacy = 1;
2089 pstrcpy(buf, sizeof(buf), optarg);
2090 } else if (check_params(buf, sizeof(buf), params, optarg) < 0) {
2091 fprintf(stderr,
2092 "qemu: unknown boot parameter '%s' in '%s'\n",
2093 buf, optarg);
2094 exit(1);
2097 if (legacy ||
2098 get_param_value(buf, sizeof(buf), "order", optarg)) {
2099 validate_bootdevices(buf);
2100 pstrcpy(boot_devices, sizeof(boot_devices), buf);
2102 if (!legacy) {
2103 if (get_param_value(buf, sizeof(buf),
2104 "once", optarg)) {
2105 validate_bootdevices(buf);
2106 standard_boot_devices = qemu_strdup(boot_devices);
2107 pstrcpy(boot_devices, sizeof(boot_devices), buf);
2108 qemu_register_reset(restore_boot_devices,
2109 standard_boot_devices);
2111 if (get_param_value(buf, sizeof(buf),
2112 "menu", optarg)) {
2113 if (!strcmp(buf, "on")) {
2114 boot_menu = 1;
2115 } else if (!strcmp(buf, "off")) {
2116 boot_menu = 0;
2117 } else {
2118 fprintf(stderr,
2119 "qemu: invalid option value '%s'\n",
2120 buf);
2121 exit(1);
2126 break;
2127 case QEMU_OPTION_fda:
2128 case QEMU_OPTION_fdb:
2129 drive_add(optarg, FD_ALIAS, popt->index - QEMU_OPTION_fda);
2130 break;
2131 case QEMU_OPTION_no_fd_bootchk:
2132 fd_bootchk = 0;
2133 break;
2134 case QEMU_OPTION_netdev:
2135 if (net_client_parse(&qemu_netdev_opts, optarg) == -1) {
2136 exit(1);
2138 break;
2139 case QEMU_OPTION_net:
2140 if (net_client_parse(&qemu_net_opts, optarg) == -1) {
2141 exit(1);
2143 break;
2144 #ifdef CONFIG_SLIRP
2145 case QEMU_OPTION_tftp:
2146 legacy_tftp_prefix = optarg;
2147 break;
2148 case QEMU_OPTION_bootp:
2149 legacy_bootp_filename = optarg;
2150 break;
2151 case QEMU_OPTION_redir:
2152 if (net_slirp_redir(optarg) < 0)
2153 exit(1);
2154 break;
2155 #endif
2156 case QEMU_OPTION_bt:
2157 add_device_config(DEV_BT, optarg);
2158 break;
2159 case QEMU_OPTION_audio_help:
2160 if (!(audio_available())) {
2161 printf("Option %s not supported for this target\n", popt->name);
2162 exit(1);
2164 AUD_help ();
2165 exit (0);
2166 break;
2167 case QEMU_OPTION_soundhw:
2168 if (!(audio_available())) {
2169 printf("Option %s not supported for this target\n", popt->name);
2170 exit(1);
2172 select_soundhw (optarg);
2173 break;
2174 case QEMU_OPTION_h:
2175 help(0);
2176 break;
2177 case QEMU_OPTION_version:
2178 version();
2179 exit(0);
2180 break;
2181 case QEMU_OPTION_m: {
2182 uint64_t value;
2183 char *ptr;
2185 value = strtoul(optarg, &ptr, 10);
2186 switch (*ptr) {
2187 case 0: case 'M': case 'm':
2188 value <<= 20;
2189 break;
2190 case 'G': case 'g':
2191 value <<= 30;
2192 break;
2193 default:
2194 fprintf(stderr, "qemu: invalid ram size: %s\n", optarg);
2195 exit(1);
2198 /* On 32-bit hosts, QEMU is limited by virtual address space */
2199 if (value > (2047 << 20) && HOST_LONG_BITS == 32) {
2200 fprintf(stderr, "qemu: at most 2047 MB RAM can be simulated\n");
2201 exit(1);
2203 if (value != (uint64_t)(ram_addr_t)value) {
2204 fprintf(stderr, "qemu: ram size too large\n");
2205 exit(1);
2207 ram_size = value;
2208 break;
2210 case QEMU_OPTION_mempath:
2211 mem_path = optarg;
2212 break;
2213 #ifdef MAP_POPULATE
2214 case QEMU_OPTION_mem_prealloc:
2215 mem_prealloc = 1;
2216 break;
2217 #endif
2218 case QEMU_OPTION_d:
2219 set_cpu_log(optarg);
2220 break;
2221 case QEMU_OPTION_s:
2222 gdbstub_dev = "tcp::" DEFAULT_GDBSTUB_PORT;
2223 break;
2224 case QEMU_OPTION_gdb:
2225 gdbstub_dev = optarg;
2226 break;
2227 case QEMU_OPTION_L:
2228 data_dir = optarg;
2229 break;
2230 case QEMU_OPTION_bios:
2231 bios_name = optarg;
2232 break;
2233 case QEMU_OPTION_singlestep:
2234 singlestep = 1;
2235 break;
2236 case QEMU_OPTION_S:
2237 autostart = 0;
2238 break;
2239 case QEMU_OPTION_k:
2240 keyboard_layout = optarg;
2241 break;
2242 case QEMU_OPTION_localtime:
2243 rtc_utc = 0;
2244 break;
2245 case QEMU_OPTION_vga:
2246 select_vgahw (optarg);
2247 break;
2248 case QEMU_OPTION_g:
2250 const char *p;
2251 int w, h, depth;
2252 p = optarg;
2253 w = strtol(p, (char **)&p, 10);
2254 if (w <= 0) {
2255 graphic_error:
2256 fprintf(stderr, "qemu: invalid resolution or depth\n");
2257 exit(1);
2259 if (*p != 'x')
2260 goto graphic_error;
2261 p++;
2262 h = strtol(p, (char **)&p, 10);
2263 if (h <= 0)
2264 goto graphic_error;
2265 if (*p == 'x') {
2266 p++;
2267 depth = strtol(p, (char **)&p, 10);
2268 if (depth != 8 && depth != 15 && depth != 16 &&
2269 depth != 24 && depth != 32)
2270 goto graphic_error;
2271 } else if (*p == '\0') {
2272 depth = graphic_depth;
2273 } else {
2274 goto graphic_error;
2277 graphic_width = w;
2278 graphic_height = h;
2279 graphic_depth = depth;
2281 break;
2282 case QEMU_OPTION_echr:
2284 char *r;
2285 term_escape_char = strtol(optarg, &r, 0);
2286 if (r == optarg)
2287 printf("Bad argument to echr\n");
2288 break;
2290 case QEMU_OPTION_monitor:
2291 monitor_parse(optarg, "readline");
2292 default_monitor = 0;
2293 break;
2294 case QEMU_OPTION_qmp:
2295 monitor_parse(optarg, "control");
2296 default_monitor = 0;
2297 break;
2298 case QEMU_OPTION_mon:
2299 opts = qemu_opts_parse(&qemu_mon_opts, optarg, 1);
2300 if (!opts) {
2301 exit(1);
2303 default_monitor = 0;
2304 break;
2305 case QEMU_OPTION_chardev:
2306 opts = qemu_opts_parse(&qemu_chardev_opts, optarg, 1);
2307 if (!opts) {
2308 exit(1);
2310 break;
2311 #ifdef CONFIG_VIRTFS
2312 case QEMU_OPTION_fsdev:
2313 opts = qemu_opts_parse(&qemu_fsdev_opts, optarg, 1);
2314 if (!opts) {
2315 fprintf(stderr, "parse error: %s\n", optarg);
2316 exit(1);
2318 break;
2319 case QEMU_OPTION_virtfs: {
2320 char *arg_fsdev = NULL;
2321 char *arg_9p = NULL;
2322 int len = 0;
2324 opts = qemu_opts_parse(&qemu_virtfs_opts, optarg, 1);
2325 if (!opts) {
2326 fprintf(stderr, "parse error: %s\n", optarg);
2327 exit(1);
2330 if (qemu_opt_get(opts, "fstype") == NULL ||
2331 qemu_opt_get(opts, "mount_tag") == NULL ||
2332 qemu_opt_get(opts, "path") == NULL ||
2333 qemu_opt_get(opts, "security_model") == NULL) {
2334 fprintf(stderr, "Usage: -virtfs fstype,path=/share_path/,"
2335 "security_model=[mapped|passthrough],"
2336 "mnt_tag=tag.\n");
2337 exit(1);
2340 len = strlen(",id=,path=,security_model=");
2341 len += strlen(qemu_opt_get(opts, "fstype"));
2342 len += strlen(qemu_opt_get(opts, "mount_tag"));
2343 len += strlen(qemu_opt_get(opts, "path"));
2344 len += strlen(qemu_opt_get(opts, "security_model"));
2345 arg_fsdev = qemu_malloc((len + 1) * sizeof(*arg_fsdev));
2347 if (!arg_fsdev) {
2348 fprintf(stderr, "No memory to parse -fsdev for %s\n",
2349 optarg);
2350 exit(1);
2353 sprintf(arg_fsdev, "%s,id=%s,path=%s,security_model=%s",
2354 qemu_opt_get(opts, "fstype"),
2355 qemu_opt_get(opts, "mount_tag"),
2356 qemu_opt_get(opts, "path"),
2357 qemu_opt_get(opts, "security_model"));
2359 len = strlen("virtio-9p-pci,fsdev=,mount_tag=");
2360 len += 2*strlen(qemu_opt_get(opts, "mount_tag"));
2361 arg_9p = qemu_malloc((len + 1) * sizeof(*arg_9p));
2363 if (!arg_9p) {
2364 fprintf(stderr, "No memory to parse -device for %s\n",
2365 optarg);
2366 exit(1);
2369 sprintf(arg_9p, "virtio-9p-pci,fsdev=%s,mount_tag=%s",
2370 qemu_opt_get(opts, "mount_tag"),
2371 qemu_opt_get(opts, "mount_tag"));
2373 if (!qemu_opts_parse(&qemu_fsdev_opts, arg_fsdev, 1)) {
2374 fprintf(stderr, "parse error [fsdev]: %s\n", optarg);
2375 exit(1);
2378 if (!qemu_opts_parse(&qemu_device_opts, arg_9p, 1)) {
2379 fprintf(stderr, "parse error [device]: %s\n", optarg);
2380 exit(1);
2383 qemu_free(arg_fsdev);
2384 qemu_free(arg_9p);
2385 break;
2387 #endif
2388 case QEMU_OPTION_serial:
2389 add_device_config(DEV_SERIAL, optarg);
2390 default_serial = 0;
2391 if (strncmp(optarg, "mon:", 4) == 0) {
2392 default_monitor = 0;
2394 break;
2395 case QEMU_OPTION_watchdog:
2396 if (watchdog) {
2397 fprintf(stderr,
2398 "qemu: only one watchdog option may be given\n");
2399 return 1;
2401 watchdog = optarg;
2402 break;
2403 case QEMU_OPTION_watchdog_action:
2404 if (select_watchdog_action(optarg) == -1) {
2405 fprintf(stderr, "Unknown -watchdog-action parameter\n");
2406 exit(1);
2408 break;
2409 case QEMU_OPTION_virtiocon:
2410 add_device_config(DEV_VIRTCON, optarg);
2411 default_virtcon = 0;
2412 if (strncmp(optarg, "mon:", 4) == 0) {
2413 default_monitor = 0;
2415 break;
2416 case QEMU_OPTION_parallel:
2417 add_device_config(DEV_PARALLEL, optarg);
2418 default_parallel = 0;
2419 if (strncmp(optarg, "mon:", 4) == 0) {
2420 default_monitor = 0;
2422 break;
2423 case QEMU_OPTION_debugcon:
2424 add_device_config(DEV_DEBUGCON, optarg);
2425 break;
2426 case QEMU_OPTION_loadvm:
2427 loadvm = optarg;
2428 break;
2429 case QEMU_OPTION_full_screen:
2430 full_screen = 1;
2431 break;
2432 #ifdef CONFIG_SDL
2433 case QEMU_OPTION_no_frame:
2434 no_frame = 1;
2435 break;
2436 case QEMU_OPTION_alt_grab:
2437 alt_grab = 1;
2438 break;
2439 case QEMU_OPTION_ctrl_grab:
2440 ctrl_grab = 1;
2441 break;
2442 case QEMU_OPTION_no_quit:
2443 no_quit = 1;
2444 break;
2445 case QEMU_OPTION_sdl:
2446 display_type = DT_SDL;
2447 break;
2448 #endif
2449 case QEMU_OPTION_pidfile:
2450 pid_file = optarg;
2451 break;
2452 case QEMU_OPTION_win2k_hack:
2453 win2k_install_hack = 1;
2454 break;
2455 case QEMU_OPTION_rtc_td_hack:
2456 rtc_td_hack = 1;
2457 break;
2458 case QEMU_OPTION_acpitable:
2459 do_acpitable_option(optarg);
2460 break;
2461 case QEMU_OPTION_smbios:
2462 do_smbios_option(optarg);
2463 break;
2464 #ifdef KVM_UPSTREAM
2465 case QEMU_OPTION_enable_kvm:
2466 kvm_allowed = 1;
2467 #endif
2468 break;
2469 case QEMU_OPTION_no_kvm:
2470 kvm_allowed = 0;
2471 break;
2472 #ifdef CONFIG_KVM
2473 case QEMU_OPTION_no_kvm_irqchip: {
2474 kvm_irqchip = 0;
2475 kvm_pit = 0;
2476 break;
2478 case QEMU_OPTION_no_kvm_pit: {
2479 kvm_pit = 0;
2480 break;
2482 case QEMU_OPTION_no_kvm_pit_reinjection: {
2483 kvm_pit_reinject = 0;
2484 break;
2486 case QEMU_OPTION_enable_nesting: {
2487 kvm_nested = 1;
2488 break;
2490 #endif
2491 #if defined(TARGET_I386) || defined(TARGET_X86_64) || defined(TARGET_IA64) || defined(__linux__)
2492 case QEMU_OPTION_pcidevice:
2493 if (assigned_devices_index >= MAX_DEV_ASSIGN_CMDLINE) {
2494 fprintf(stderr, "Too many assigned devices\n");
2495 exit(1);
2497 assigned_devices[assigned_devices_index] = optarg;
2498 assigned_devices_index++;
2499 break;
2500 #endif
2501 case QEMU_OPTION_usb:
2502 usb_enabled = 1;
2503 break;
2504 case QEMU_OPTION_usbdevice:
2505 usb_enabled = 1;
2506 add_device_config(DEV_USB, optarg);
2507 break;
2508 case QEMU_OPTION_device:
2509 if (!qemu_opts_parse(&qemu_device_opts, optarg, 1)) {
2510 exit(1);
2512 break;
2513 case QEMU_OPTION_smp:
2514 smp_parse(optarg);
2515 if (smp_cpus < 1) {
2516 fprintf(stderr, "Invalid number of CPUs\n");
2517 exit(1);
2519 if (max_cpus < smp_cpus) {
2520 fprintf(stderr, "maxcpus must be equal to or greater than "
2521 "smp\n");
2522 exit(1);
2524 if (max_cpus > 255) {
2525 fprintf(stderr, "Unsupported number of maxcpus\n");
2526 exit(1);
2528 break;
2529 case QEMU_OPTION_vnc:
2530 display_type = DT_VNC;
2531 vnc_display = optarg;
2532 break;
2533 case QEMU_OPTION_no_acpi:
2534 acpi_enabled = 0;
2535 break;
2536 case QEMU_OPTION_no_hpet:
2537 no_hpet = 1;
2538 break;
2539 case QEMU_OPTION_balloon:
2540 if (balloon_parse(optarg) < 0) {
2541 fprintf(stderr, "Unknown -balloon argument %s\n", optarg);
2542 exit(1);
2544 break;
2545 case QEMU_OPTION_no_reboot:
2546 no_reboot = 1;
2547 break;
2548 case QEMU_OPTION_no_shutdown:
2549 no_shutdown = 1;
2550 break;
2551 case QEMU_OPTION_show_cursor:
2552 cursor_hide = 0;
2553 break;
2554 case QEMU_OPTION_uuid:
2555 if(qemu_uuid_parse(optarg, qemu_uuid) < 0) {
2556 fprintf(stderr, "Fail to parse UUID string."
2557 " Wrong format.\n");
2558 exit(1);
2560 break;
2561 case QEMU_OPTION_option_rom:
2562 if (nb_option_roms >= MAX_OPTION_ROMS) {
2563 fprintf(stderr, "Too many option ROMs\n");
2564 exit(1);
2566 option_rom[nb_option_roms] = optarg;
2567 nb_option_roms++;
2568 break;
2569 case QEMU_OPTION_semihosting:
2570 semihosting_enabled = 1;
2571 break;
2572 case QEMU_OPTION_tdf:
2573 time_drift_fix = 1;
2574 break;
2575 case QEMU_OPTION_kvm_shadow_memory:
2576 kvm_shadow_memory = (int64_t)atoi(optarg) * 1024 * 1024 / 4096;
2577 break;
2578 case QEMU_OPTION_name:
2579 qemu_name = qemu_strdup(optarg);
2581 char *p = strchr(qemu_name, ',');
2582 if (p != NULL) {
2583 *p++ = 0;
2584 if (strncmp(p, "process=", 8)) {
2585 fprintf(stderr, "Unknown subargument %s to -name", p);
2586 exit(1);
2588 p += 8;
2589 os_set_proc_name(p);
2592 break;
2593 case QEMU_OPTION_prom_env:
2594 if (nb_prom_envs >= MAX_PROM_ENVS) {
2595 fprintf(stderr, "Too many prom variables\n");
2596 exit(1);
2598 prom_envs[nb_prom_envs] = optarg;
2599 nb_prom_envs++;
2600 break;
2601 case QEMU_OPTION_old_param:
2602 old_param = 1;
2603 break;
2604 case QEMU_OPTION_clock:
2605 configure_alarms(optarg);
2606 break;
2607 case QEMU_OPTION_startdate:
2608 configure_rtc_date_offset(optarg, 1);
2609 break;
2610 case QEMU_OPTION_rtc:
2611 opts = qemu_opts_parse(&qemu_rtc_opts, optarg, 0);
2612 if (!opts) {
2613 exit(1);
2615 configure_rtc(opts);
2616 break;
2617 case QEMU_OPTION_tb_size:
2618 tb_size = strtol(optarg, NULL, 0);
2619 if (tb_size < 0)
2620 tb_size = 0;
2621 break;
2622 case QEMU_OPTION_icount:
2623 icount_option = optarg;
2624 break;
2625 case QEMU_OPTION_incoming:
2626 incoming = optarg;
2627 incoming_expected = true;
2628 break;
2629 case QEMU_OPTION_nodefaults:
2630 default_serial = 0;
2631 default_parallel = 0;
2632 default_virtcon = 0;
2633 default_monitor = 0;
2634 default_vga = 0;
2635 default_net = 0;
2636 default_floppy = 0;
2637 default_cdrom = 0;
2638 default_sdcard = 0;
2639 break;
2640 #ifndef _WIN32
2641 case QEMU_OPTION_nvram:
2642 nvram = optarg;
2643 break;
2644 #endif
2645 case QEMU_OPTION_xen_domid:
2646 if (!(xen_available())) {
2647 printf("Option %s not supported for this target\n", popt->name);
2648 exit(1);
2650 xen_domid = atoi(optarg);
2651 break;
2652 case QEMU_OPTION_xen_create:
2653 if (!(xen_available())) {
2654 printf("Option %s not supported for this target\n", popt->name);
2655 exit(1);
2657 xen_mode = XEN_CREATE;
2658 break;
2659 case QEMU_OPTION_xen_attach:
2660 if (!(xen_available())) {
2661 printf("Option %s not supported for this target\n", popt->name);
2662 exit(1);
2664 xen_mode = XEN_ATTACH;
2665 break;
2666 case QEMU_OPTION_readconfig:
2668 int ret = qemu_read_config_file(optarg);
2669 if (ret < 0) {
2670 fprintf(stderr, "read config %s: %s\n", optarg,
2671 strerror(-ret));
2672 exit(1);
2674 break;
2676 case QEMU_OPTION_writeconfig:
2678 FILE *fp;
2679 if (strcmp(optarg, "-") == 0) {
2680 fp = stdout;
2681 } else {
2682 fp = fopen(optarg, "w");
2683 if (fp == NULL) {
2684 fprintf(stderr, "open %s: %s\n", optarg, strerror(errno));
2685 exit(1);
2688 qemu_config_write(fp);
2689 fclose(fp);
2690 break;
2692 default:
2693 os_parse_cmd_args(popt->index, optarg);
2697 loc_set_none();
2699 /* If no data_dir is specified then try to find it relative to the
2700 executable path. */
2701 if (!data_dir) {
2702 data_dir = os_find_datadir(argv[0]);
2704 /* If all else fails use the install patch specified when building. */
2705 if (!data_dir) {
2706 data_dir = CONFIG_QEMU_DATADIR;
2710 * Default to max_cpus = smp_cpus, in case the user doesn't
2711 * specify a max_cpus value.
2713 if (!max_cpus)
2714 max_cpus = smp_cpus;
2716 machine->max_cpus = machine->max_cpus ?: 1; /* Default to UP */
2717 if (smp_cpus > machine->max_cpus) {
2718 fprintf(stderr, "Number of SMP cpus requested (%d), exceeds max cpus "
2719 "supported by machine `%s' (%d)\n", smp_cpus, machine->name,
2720 machine->max_cpus);
2721 exit(1);
2724 qemu_opts_foreach(&qemu_device_opts, default_driver_check, NULL, 0);
2725 qemu_opts_foreach(&qemu_global_opts, default_driver_check, NULL, 0);
2727 if (machine->no_serial) {
2728 default_serial = 0;
2730 if (machine->no_parallel) {
2731 default_parallel = 0;
2733 if (!machine->use_virtcon) {
2734 default_virtcon = 0;
2736 if (machine->no_vga) {
2737 default_vga = 0;
2739 if (machine->no_floppy) {
2740 default_floppy = 0;
2742 if (machine->no_cdrom) {
2743 default_cdrom = 0;
2745 if (machine->no_sdcard) {
2746 default_sdcard = 0;
2749 if (display_type == DT_NOGRAPHIC) {
2750 if (default_parallel)
2751 add_device_config(DEV_PARALLEL, "null");
2752 if (default_serial && default_monitor) {
2753 add_device_config(DEV_SERIAL, "mon:stdio");
2754 } else if (default_virtcon && default_monitor) {
2755 add_device_config(DEV_VIRTCON, "mon:stdio");
2756 } else {
2757 if (default_serial)
2758 add_device_config(DEV_SERIAL, "stdio");
2759 if (default_virtcon)
2760 add_device_config(DEV_VIRTCON, "stdio");
2761 if (default_monitor)
2762 monitor_parse("stdio", "readline");
2764 } else {
2765 if (default_serial)
2766 add_device_config(DEV_SERIAL, "vc:80Cx24C");
2767 if (default_parallel)
2768 add_device_config(DEV_PARALLEL, "vc:80Cx24C");
2769 if (default_monitor)
2770 monitor_parse("vc:80Cx24C", "readline");
2771 if (default_virtcon)
2772 add_device_config(DEV_VIRTCON, "vc:80Cx24C");
2774 if (default_vga)
2775 vga_interface_type = VGA_CIRRUS;
2777 socket_init();
2779 if (qemu_opts_foreach(&qemu_chardev_opts, chardev_init_func, NULL, 1) != 0)
2780 exit(1);
2781 #ifdef CONFIG_VIRTFS
2782 if (qemu_opts_foreach(&qemu_fsdev_opts, fsdev_init_func, NULL, 1) != 0) {
2783 exit(1);
2785 #endif
2787 os_daemonize();
2789 if (pid_file && qemu_create_pidfile(pid_file) != 0) {
2790 os_pidfile_error();
2791 exit(1);
2794 if (kvm_allowed) {
2795 int ret = kvm_init(smp_cpus);
2796 if (ret < 0) {
2797 #if defined(KVM_UPSTREAM) || defined(CONFIG_NO_CPU_EMULATION)
2798 if (!kvm_available()) {
2799 printf("KVM not supported for this target\n");
2800 } else {
2801 fprintf(stderr, "failed to initialize KVM: %s\n", strerror(-ret));
2803 exit(1);
2804 #endif
2805 #ifdef CONFIG_KVM
2806 fprintf(stderr, "Could not initialize KVM, will disable KVM support\n");
2807 kvm_allowed = 0;
2808 #endif
2812 if (qemu_init_main_loop()) {
2813 fprintf(stderr, "qemu_init_main_loop failed\n");
2814 exit(1);
2816 linux_boot = (kernel_filename != NULL);
2818 if (!linux_boot && *kernel_cmdline != '\0') {
2819 fprintf(stderr, "-append only allowed with -kernel option\n");
2820 exit(1);
2823 if (!linux_boot && initrd_filename != NULL) {
2824 fprintf(stderr, "-initrd only allowed with -kernel option\n");
2825 exit(1);
2828 os_set_line_buffering();
2830 if (init_timer_alarm() < 0) {
2831 fprintf(stderr, "could not initialize alarm timer\n");
2832 exit(1);
2834 configure_icount(icount_option);
2836 if (net_init_clients() < 0) {
2837 exit(1);
2840 /* init the bluetooth world */
2841 if (foreach_device_config(DEV_BT, bt_parse))
2842 exit(1);
2844 /* init the memory */
2845 if (ram_size == 0)
2846 ram_size = DEFAULT_RAM_SIZE * 1024 * 1024;
2848 /* init the dynamic translator */
2849 cpu_exec_init_all(tb_size * 1024 * 1024);
2851 bdrv_init_with_whitelist();
2853 blk_mig_init();
2855 if (default_cdrom) {
2856 /* we always create the cdrom drive, even if no disk is there */
2857 drive_add(NULL, CDROM_ALIAS);
2860 if (default_floppy) {
2861 /* we always create at least one floppy */
2862 drive_add(NULL, FD_ALIAS, 0);
2865 if (default_sdcard) {
2866 /* we always create one sd slot, even if no card is in it */
2867 drive_add(NULL, SD_ALIAS);
2870 /* open the virtual block devices */
2871 if (snapshot)
2872 qemu_opts_foreach(&qemu_drive_opts, drive_enable_snapshot, NULL, 0);
2873 if (qemu_opts_foreach(&qemu_drive_opts, drive_init_func, &machine->use_scsi, 1) != 0)
2874 exit(1);
2876 register_savevm_live(NULL, "ram", 0, 4, NULL, ram_save_live, NULL,
2877 ram_load, NULL);
2879 if (nb_numa_nodes > 0) {
2880 int i;
2882 if (nb_numa_nodes > smp_cpus) {
2883 nb_numa_nodes = smp_cpus;
2886 /* If no memory size if given for any node, assume the default case
2887 * and distribute the available memory equally across all nodes
2889 for (i = 0; i < nb_numa_nodes; i++) {
2890 if (node_mem[i] != 0)
2891 break;
2893 if (i == nb_numa_nodes) {
2894 uint64_t usedmem = 0;
2896 /* On Linux, the each node's border has to be 8MB aligned,
2897 * the final node gets the rest.
2899 for (i = 0; i < nb_numa_nodes - 1; i++) {
2900 node_mem[i] = (ram_size / nb_numa_nodes) & ~((1 << 23UL) - 1);
2901 usedmem += node_mem[i];
2903 node_mem[i] = ram_size - usedmem;
2906 for (i = 0; i < nb_numa_nodes; i++) {
2907 if (node_cpumask[i] != 0)
2908 break;
2910 /* assigning the VCPUs round-robin is easier to implement, guest OSes
2911 * must cope with this anyway, because there are BIOSes out there in
2912 * real machines which also use this scheme.
2914 if (i == nb_numa_nodes) {
2915 for (i = 0; i < smp_cpus; i++) {
2916 node_cpumask[i % nb_numa_nodes] |= 1 << i;
2921 if (qemu_opts_foreach(&qemu_mon_opts, mon_init_func, NULL, 1) != 0) {
2922 exit(1);
2925 if (foreach_device_config(DEV_SERIAL, serial_parse) < 0)
2926 exit(1);
2927 if (foreach_device_config(DEV_PARALLEL, parallel_parse) < 0)
2928 exit(1);
2929 if (foreach_device_config(DEV_VIRTCON, virtcon_parse) < 0)
2930 exit(1);
2931 if (foreach_device_config(DEV_DEBUGCON, debugcon_parse) < 0)
2932 exit(1);
2934 module_call_init(MODULE_INIT_DEVICE);
2936 if (qemu_opts_foreach(&qemu_device_opts, device_help_func, NULL, 0) != 0)
2937 exit(0);
2939 if (watchdog) {
2940 i = select_watchdog(watchdog);
2941 if (i > 0)
2942 exit (i == 1 ? 1 : 0);
2945 if (machine->compat_props) {
2946 qdev_prop_register_global_list(machine->compat_props);
2948 qemu_add_globals();
2950 machine->init(ram_size, boot_devices,
2951 kernel_filename, kernel_cmdline, initrd_filename, cpu_model);
2953 cpu_synchronize_all_post_init();
2955 /* must be after terminal init, SDL library changes signal handlers */
2956 os_setup_signal_handling();
2958 set_numa_modes();
2960 current_machine = machine;
2962 /* init USB devices */
2963 if (usb_enabled) {
2964 if (foreach_device_config(DEV_USB, usb_parse) < 0)
2965 exit(1);
2968 /* init generic devices */
2969 if (qemu_opts_foreach(&qemu_device_opts, device_init_func, NULL, 1) != 0)
2970 exit(1);
2972 net_check_clients();
2974 /* just use the first displaystate for the moment */
2975 ds = get_displaystate();
2977 if (display_type == DT_DEFAULT) {
2978 #if defined(CONFIG_SDL) || defined(CONFIG_COCOA)
2979 display_type = DT_SDL;
2980 #else
2981 display_type = DT_VNC;
2982 vnc_display = "localhost:0,to=99";
2983 show_vnc_port = 1;
2984 #endif
2988 switch (display_type) {
2989 case DT_NOGRAPHIC:
2990 break;
2991 #if defined(CONFIG_CURSES)
2992 case DT_CURSES:
2993 curses_display_init(ds, full_screen);
2994 break;
2995 #endif
2996 #if defined(CONFIG_SDL)
2997 case DT_SDL:
2998 sdl_display_init(ds, full_screen, no_frame);
2999 break;
3000 #elif defined(CONFIG_COCOA)
3001 case DT_SDL:
3002 cocoa_display_init(ds, full_screen);
3003 break;
3004 #endif
3005 case DT_VNC:
3006 vnc_display_init(ds);
3007 if (vnc_display_open(ds, vnc_display) < 0)
3008 exit(1);
3010 if (show_vnc_port) {
3011 printf("VNC server running on `%s'\n", vnc_display_local_addr(ds));
3013 break;
3014 default:
3015 break;
3017 dpy_resize(ds);
3019 dcl = ds->listeners;
3020 while (dcl != NULL) {
3021 if (dcl->dpy_refresh != NULL) {
3022 ds->gui_timer = qemu_new_timer(rt_clock, gui_update, ds);
3023 qemu_mod_timer(ds->gui_timer, qemu_get_clock(rt_clock));
3024 break;
3026 dcl = dcl->next;
3029 if (display_type == DT_NOGRAPHIC || display_type == DT_VNC) {
3030 nographic_timer = qemu_new_timer(rt_clock, nographic_update, NULL);
3031 qemu_mod_timer(nographic_timer, qemu_get_clock(rt_clock));
3034 text_consoles_set_display(ds);
3036 if (gdbstub_dev && gdbserver_start(gdbstub_dev) < 0) {
3037 fprintf(stderr, "qemu: could not open gdbserver on device '%s'\n",
3038 gdbstub_dev);
3039 exit(1);
3042 qdev_machine_creation_done();
3044 if (rom_load_all() != 0) {
3045 fprintf(stderr, "rom loading failed\n");
3046 exit(1);
3049 qemu_system_reset();
3050 if (loadvm) {
3051 if (load_vmstate(loadvm) < 0) {
3052 autostart = 0;
3056 if (incoming) {
3057 int ret = qemu_start_incoming_migration(incoming);
3058 if (ret < 0) {
3059 fprintf(stderr, "Migration failed. Exit code %s(%d), exiting.\n",
3060 incoming, ret);
3061 exit(ret);
3063 } else if (autostart) {
3064 vm_start();
3067 os_setup_post();
3069 main_loop();
3070 quit_timers();
3071 net_cleanup();
3073 return 0;