qemu-kvm: use upstream sregs save/restore code
[qemu-kvm/stefanha.git] / vl.c
blob42617c2525eb3e1b16748bae071c09a6a11daf7b
1 /*
2 * QEMU System Emulator
4 * Copyright (c) 2003-2008 Fabrice Bellard
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 * THE SOFTWARE.
24 #include <unistd.h>
25 #include <fcntl.h>
26 #include <signal.h>
27 #include <time.h>
28 #include <errno.h>
29 #include <sys/time.h>
30 #include <zlib.h>
32 /* Needed early for CONFIG_BSD etc. */
33 #include "config-host.h"
35 #ifndef _WIN32
36 #include <libgen.h>
37 #include <sys/times.h>
38 #include <sys/wait.h>
39 #include <termios.h>
40 #include <sys/mman.h>
41 #include <sys/ioctl.h>
42 #include <sys/resource.h>
43 #include <sys/socket.h>
44 #include <netinet/in.h>
45 #include <net/if.h>
46 #include <arpa/inet.h>
47 #include <dirent.h>
48 #include <netdb.h>
49 #include <sys/select.h>
50 #ifdef CONFIG_SIMPLE_TRACE
51 #include "trace.h"
52 #endif
54 #ifdef CONFIG_BSD
55 #include <sys/stat.h>
56 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__DragonFly__)
57 #include <libutil.h>
58 #include <sys/sysctl.h>
59 #else
60 #include <util.h>
61 #endif
62 #else
63 #ifdef __linux__
64 #include <pty.h>
65 #include <malloc.h>
67 #include <linux/ppdev.h>
68 #include <linux/parport.h>
69 #endif
70 #ifdef __sun__
71 #include <sys/stat.h>
72 #include <sys/ethernet.h>
73 #include <sys/sockio.h>
74 #include <netinet/arp.h>
75 #include <netinet/in_systm.h>
76 #include <netinet/ip.h>
77 #include <netinet/ip_icmp.h> // must come after ip.h
78 #include <netinet/udp.h>
79 #include <netinet/tcp.h>
80 #include <net/if.h>
81 #include <syslog.h>
82 #include <stropts.h>
83 #endif
84 #endif
85 #endif
87 #if defined(__OpenBSD__)
88 #include <util.h>
89 #endif
91 #if defined(CONFIG_VDE)
92 #include <libvdeplug.h>
93 #endif
95 #ifdef _WIN32
96 #include <windows.h>
97 #endif
99 #ifdef CONFIG_SDL
100 #if defined(__APPLE__) || defined(main)
101 #include <SDL.h>
102 int qemu_main(int argc, char **argv, char **envp);
103 int main(int argc, char **argv)
105 return qemu_main(argc, argv, NULL);
107 #undef main
108 #define main qemu_main
109 #endif
110 #endif /* CONFIG_SDL */
112 #ifdef CONFIG_COCOA
113 #undef main
114 #define main qemu_main
115 #endif /* CONFIG_COCOA */
117 #include "hw/hw.h"
118 #include "hw/boards.h"
119 #include "hw/usb.h"
120 #include "hw/pcmcia.h"
121 #include "hw/pc.h"
122 #include "hw/isa.h"
123 #include "hw/baum.h"
124 #include "hw/bt.h"
125 #include "hw/watchdog.h"
126 #include "hw/smbios.h"
127 #include "hw/xen.h"
128 #include "hw/qdev.h"
129 #include "hw/loader.h"
130 #include "bt-host.h"
131 #include "net.h"
132 #include "net/slirp.h"
133 #include "monitor.h"
134 #include "console.h"
135 #include "sysemu.h"
136 #include "gdbstub.h"
137 #include "qemu-timer.h"
138 #include "qemu-char.h"
139 #include "cache-utils.h"
140 #include "block.h"
141 #include "blockdev.h"
142 #include "block-migration.h"
143 #include "dma.h"
144 #include "audio/audio.h"
145 #include "migration.h"
146 #include "kvm.h"
147 #include "qemu-option.h"
148 #include "qemu-config.h"
149 #include "qemu-objects.h"
150 #include "qemu-options.h"
151 #include "hw/device-assignment.h"
152 #ifdef CONFIG_VIRTFS
153 #include "fsdev/qemu-fsdev.h"
154 #endif
156 #include "disas.h"
158 #include "qemu_socket.h"
160 #include "slirp/libslirp.h"
162 #include "qemu-queue.h"
163 #include "cpus.h"
164 #include "arch_init.h"
166 #include "ui/qemu-spice.h"
168 //#define DEBUG_NET
169 //#define DEBUG_SLIRP
171 #define DEFAULT_RAM_SIZE 128
173 #define MAX_VIRTIO_CONSOLES 1
175 static const char *data_dir;
176 const char *bios_name = NULL;
177 enum vga_retrace_method vga_retrace_method = VGA_RETRACE_DUMB;
178 DisplayType display_type = DT_DEFAULT;
179 int display_remote = 0;
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, "pretty", 0))
1595 flags |= MONITOR_USE_PRETTY;
1597 if (qemu_opt_get_bool(opts, "default", 0))
1598 flags |= MONITOR_IS_DEFAULT;
1600 chardev = qemu_opt_get(opts, "chardev");
1601 chr = qemu_chr_find(chardev);
1602 if (chr == NULL) {
1603 fprintf(stderr, "chardev \"%s\" not found\n", chardev);
1604 exit(1);
1607 monitor_init(chr, flags);
1608 return 0;
1611 static void monitor_parse(const char *optarg, const char *mode)
1613 static int monitor_device_index = 0;
1614 QemuOpts *opts;
1615 const char *p;
1616 char label[32];
1617 int def = 0;
1619 if (strstart(optarg, "chardev:", &p)) {
1620 snprintf(label, sizeof(label), "%s", p);
1621 } else {
1622 snprintf(label, sizeof(label), "compat_monitor%d",
1623 monitor_device_index);
1624 if (monitor_device_index == 0) {
1625 def = 1;
1627 opts = qemu_chr_parse_compat(label, optarg);
1628 if (!opts) {
1629 fprintf(stderr, "parse error: %s\n", optarg);
1630 exit(1);
1634 opts = qemu_opts_create(qemu_find_opts("mon"), label, 1);
1635 if (!opts) {
1636 fprintf(stderr, "duplicate chardev: %s\n", label);
1637 exit(1);
1639 qemu_opt_set(opts, "mode", mode);
1640 qemu_opt_set(opts, "chardev", label);
1641 if (def)
1642 qemu_opt_set(opts, "default", "on");
1643 monitor_device_index++;
1646 struct device_config {
1647 enum {
1648 DEV_USB, /* -usbdevice */
1649 DEV_BT, /* -bt */
1650 DEV_SERIAL, /* -serial */
1651 DEV_PARALLEL, /* -parallel */
1652 DEV_VIRTCON, /* -virtioconsole */
1653 DEV_DEBUGCON, /* -debugcon */
1654 } type;
1655 const char *cmdline;
1656 QTAILQ_ENTRY(device_config) next;
1658 QTAILQ_HEAD(, device_config) device_configs = QTAILQ_HEAD_INITIALIZER(device_configs);
1660 static void add_device_config(int type, const char *cmdline)
1662 struct device_config *conf;
1664 conf = qemu_mallocz(sizeof(*conf));
1665 conf->type = type;
1666 conf->cmdline = cmdline;
1667 QTAILQ_INSERT_TAIL(&device_configs, conf, next);
1670 static int foreach_device_config(int type, int (*func)(const char *cmdline))
1672 struct device_config *conf;
1673 int rc;
1675 QTAILQ_FOREACH(conf, &device_configs, next) {
1676 if (conf->type != type)
1677 continue;
1678 rc = func(conf->cmdline);
1679 if (0 != rc)
1680 return rc;
1682 return 0;
1685 static int serial_parse(const char *devname)
1687 static int index = 0;
1688 char label[32];
1690 if (strcmp(devname, "none") == 0)
1691 return 0;
1692 if (index == MAX_SERIAL_PORTS) {
1693 fprintf(stderr, "qemu: too many serial ports\n");
1694 exit(1);
1696 snprintf(label, sizeof(label), "serial%d", index);
1697 serial_hds[index] = qemu_chr_open(label, devname, NULL);
1698 if (!serial_hds[index]) {
1699 fprintf(stderr, "qemu: could not open serial device '%s': %s\n",
1700 devname, strerror(errno));
1701 return -1;
1703 index++;
1704 return 0;
1707 static int parallel_parse(const char *devname)
1709 static int index = 0;
1710 char label[32];
1712 if (strcmp(devname, "none") == 0)
1713 return 0;
1714 if (index == MAX_PARALLEL_PORTS) {
1715 fprintf(stderr, "qemu: too many parallel ports\n");
1716 exit(1);
1718 snprintf(label, sizeof(label), "parallel%d", index);
1719 parallel_hds[index] = qemu_chr_open(label, devname, NULL);
1720 if (!parallel_hds[index]) {
1721 fprintf(stderr, "qemu: could not open parallel device '%s': %s\n",
1722 devname, strerror(errno));
1723 return -1;
1725 index++;
1726 return 0;
1729 static int virtcon_parse(const char *devname)
1731 QemuOptsList *device = qemu_find_opts("device");
1732 static int index = 0;
1733 char label[32];
1734 QemuOpts *bus_opts, *dev_opts;
1736 if (strcmp(devname, "none") == 0)
1737 return 0;
1738 if (index == MAX_VIRTIO_CONSOLES) {
1739 fprintf(stderr, "qemu: too many virtio consoles\n");
1740 exit(1);
1743 bus_opts = qemu_opts_create(device, NULL, 0);
1744 qemu_opt_set(bus_opts, "driver", "virtio-serial");
1746 dev_opts = qemu_opts_create(device, NULL, 0);
1747 qemu_opt_set(dev_opts, "driver", "virtconsole");
1749 snprintf(label, sizeof(label), "virtcon%d", index);
1750 virtcon_hds[index] = qemu_chr_open(label, devname, NULL);
1751 if (!virtcon_hds[index]) {
1752 fprintf(stderr, "qemu: could not open virtio console '%s': %s\n",
1753 devname, strerror(errno));
1754 return -1;
1756 qemu_opt_set(dev_opts, "chardev", label);
1758 index++;
1759 return 0;
1762 static int debugcon_parse(const char *devname)
1764 QemuOpts *opts;
1766 if (!qemu_chr_open("debugcon", devname, NULL)) {
1767 exit(1);
1769 opts = qemu_opts_create(qemu_find_opts("device"), "debugcon", 1);
1770 if (!opts) {
1771 fprintf(stderr, "qemu: already have a debugcon device\n");
1772 exit(1);
1774 qemu_opt_set(opts, "driver", "isa-debugcon");
1775 qemu_opt_set(opts, "chardev", "debugcon");
1776 return 0;
1779 void qemu_add_exit_notifier(Notifier *notify)
1781 notifier_list_add(&exit_notifiers, notify);
1784 void qemu_remove_exit_notifier(Notifier *notify)
1786 notifier_list_remove(&exit_notifiers, notify);
1789 static void qemu_run_exit_notifiers(void)
1791 notifier_list_notify(&exit_notifiers);
1794 static const QEMUOption *lookup_opt(int argc, char **argv,
1795 const char **poptarg, int *poptind)
1797 const QEMUOption *popt;
1798 int optind = *poptind;
1799 char *r = argv[optind];
1800 const char *optarg;
1802 loc_set_cmdline(argv, optind, 1);
1803 optind++;
1804 /* Treat --foo the same as -foo. */
1805 if (r[1] == '-')
1806 r++;
1807 popt = qemu_options;
1808 for(;;) {
1809 if (!popt->name) {
1810 error_report("invalid option");
1811 exit(1);
1813 if (!strcmp(popt->name, r + 1))
1814 break;
1815 popt++;
1817 if (popt->flags & HAS_ARG) {
1818 if (optind >= argc) {
1819 error_report("requires an argument");
1820 exit(1);
1822 optarg = argv[optind++];
1823 loc_set_cmdline(argv, optind - 2, 2);
1824 } else {
1825 optarg = NULL;
1828 *poptarg = optarg;
1829 *poptind = optind;
1831 return popt;
1834 int main(int argc, char **argv, char **envp)
1836 const char *gdbstub_dev = NULL;
1837 int i;
1838 int snapshot, linux_boot;
1839 const char *icount_option = NULL;
1840 const char *initrd_filename;
1841 const char *kernel_filename, *kernel_cmdline;
1842 char boot_devices[33] = "cad"; /* default to HD->floppy->CD-ROM */
1843 DisplayState *ds;
1844 DisplayChangeListener *dcl;
1845 int cyls, heads, secs, translation;
1846 QemuOpts *hda_opts = NULL, *opts;
1847 QemuOptsList *olist;
1848 int optind;
1849 const char *optarg;
1850 const char *loadvm = NULL;
1851 QEMUMachine *machine;
1852 const char *cpu_model;
1853 int tb_size;
1854 const char *pid_file = NULL;
1855 const char *incoming = NULL;
1856 int show_vnc_port = 0;
1857 int defconfig = 1;
1859 #ifdef CONFIG_SIMPLE_TRACE
1860 const char *trace_file = NULL;
1861 #endif
1862 atexit(qemu_run_exit_notifiers);
1863 error_set_progname(argv[0]);
1865 init_clocks();
1867 qemu_cache_utils_init(envp);
1869 QLIST_INIT (&vm_change_state_head);
1870 os_setup_early_signal_handling();
1872 module_call_init(MODULE_INIT_MACHINE);
1873 machine = find_default_machine();
1874 cpu_model = NULL;
1875 initrd_filename = NULL;
1876 ram_size = 0;
1877 snapshot = 0;
1878 kernel_filename = NULL;
1879 kernel_cmdline = "";
1880 cyls = heads = secs = 0;
1881 translation = BIOS_ATA_TRANSLATION_AUTO;
1883 for (i = 0; i < MAX_NODES; i++) {
1884 node_mem[i] = 0;
1885 node_cpumask[i] = 0;
1888 assigned_devices_index = 0;
1890 nb_numa_nodes = 0;
1891 nb_nics = 0;
1893 tb_size = 0;
1894 autostart= 1;
1896 /* first pass of option parsing */
1897 optind = 1;
1898 while (optind < argc) {
1899 if (argv[optind][0] != '-') {
1900 /* disk image */
1901 optind++;
1902 continue;
1903 } else {
1904 const QEMUOption *popt;
1906 popt = lookup_opt(argc, argv, &optarg, &optind);
1907 switch (popt->index) {
1908 case QEMU_OPTION_nodefconfig:
1909 defconfig=0;
1910 break;
1915 if (defconfig) {
1916 int ret;
1918 ret = qemu_read_config_file(CONFIG_QEMU_CONFDIR "/qemu.conf");
1919 if (ret < 0 && ret != -ENOENT) {
1920 exit(1);
1923 ret = qemu_read_config_file(arch_config_name);
1924 if (ret < 0 && ret != -ENOENT) {
1925 exit(1);
1928 cpudef_init();
1930 /* second pass of option parsing */
1931 optind = 1;
1932 for(;;) {
1933 if (optind >= argc)
1934 break;
1935 if (argv[optind][0] != '-') {
1936 hda_opts = drive_add(argv[optind++], HD_ALIAS, 0);
1937 } else {
1938 const QEMUOption *popt;
1940 popt = lookup_opt(argc, argv, &optarg, &optind);
1941 if (!(popt->arch_mask & arch_type)) {
1942 printf("Option %s not supported for this target\n", popt->name);
1943 exit(1);
1945 switch(popt->index) {
1946 case QEMU_OPTION_M:
1947 machine = find_machine(optarg);
1948 if (!machine) {
1949 QEMUMachine *m;
1950 printf("Supported machines are:\n");
1951 for(m = first_machine; m != NULL; m = m->next) {
1952 if (m->alias)
1953 printf("%-10s %s (alias of %s)\n",
1954 m->alias, m->desc, m->name);
1955 printf("%-10s %s%s\n",
1956 m->name, m->desc,
1957 m->is_default ? " (default)" : "");
1959 exit(*optarg != '?');
1961 break;
1962 case QEMU_OPTION_cpu:
1963 /* hw initialization will check this */
1964 if (*optarg == '?') {
1965 list_cpus(stdout, &fprintf, optarg);
1966 exit(0);
1967 } else {
1968 cpu_model = optarg;
1970 break;
1971 case QEMU_OPTION_initrd:
1972 initrd_filename = optarg;
1973 break;
1974 case QEMU_OPTION_hda:
1975 if (cyls == 0)
1976 hda_opts = drive_add(optarg, HD_ALIAS, 0);
1977 else
1978 hda_opts = drive_add(optarg, HD_ALIAS
1979 ",cyls=%d,heads=%d,secs=%d%s",
1980 0, cyls, heads, secs,
1981 translation == BIOS_ATA_TRANSLATION_LBA ?
1982 ",trans=lba" :
1983 translation == BIOS_ATA_TRANSLATION_NONE ?
1984 ",trans=none" : "");
1985 break;
1986 case QEMU_OPTION_hdb:
1987 case QEMU_OPTION_hdc:
1988 case QEMU_OPTION_hdd:
1989 drive_add(optarg, HD_ALIAS, popt->index - QEMU_OPTION_hda);
1990 break;
1991 case QEMU_OPTION_drive:
1992 drive_add(NULL, "%s", optarg);
1993 break;
1994 case QEMU_OPTION_set:
1995 if (qemu_set_option(optarg) != 0)
1996 exit(1);
1997 break;
1998 case QEMU_OPTION_global:
1999 if (qemu_global_option(optarg) != 0)
2000 exit(1);
2001 break;
2002 case QEMU_OPTION_mtdblock:
2003 drive_add(optarg, MTD_ALIAS);
2004 break;
2005 case QEMU_OPTION_sd:
2006 drive_add(optarg, SD_ALIAS);
2007 break;
2008 case QEMU_OPTION_pflash:
2009 drive_add(optarg, PFLASH_ALIAS);
2010 break;
2011 case QEMU_OPTION_snapshot:
2012 snapshot = 1;
2013 break;
2014 case QEMU_OPTION_hdachs:
2016 const char *p;
2017 p = optarg;
2018 cyls = strtol(p, (char **)&p, 0);
2019 if (cyls < 1 || cyls > 16383)
2020 goto chs_fail;
2021 if (*p != ',')
2022 goto chs_fail;
2023 p++;
2024 heads = strtol(p, (char **)&p, 0);
2025 if (heads < 1 || heads > 16)
2026 goto chs_fail;
2027 if (*p != ',')
2028 goto chs_fail;
2029 p++;
2030 secs = strtol(p, (char **)&p, 0);
2031 if (secs < 1 || secs > 63)
2032 goto chs_fail;
2033 if (*p == ',') {
2034 p++;
2035 if (!strcmp(p, "none"))
2036 translation = BIOS_ATA_TRANSLATION_NONE;
2037 else if (!strcmp(p, "lba"))
2038 translation = BIOS_ATA_TRANSLATION_LBA;
2039 else if (!strcmp(p, "auto"))
2040 translation = BIOS_ATA_TRANSLATION_AUTO;
2041 else
2042 goto chs_fail;
2043 } else if (*p != '\0') {
2044 chs_fail:
2045 fprintf(stderr, "qemu: invalid physical CHS format\n");
2046 exit(1);
2048 if (hda_opts != NULL) {
2049 char num[16];
2050 snprintf(num, sizeof(num), "%d", cyls);
2051 qemu_opt_set(hda_opts, "cyls", num);
2052 snprintf(num, sizeof(num), "%d", heads);
2053 qemu_opt_set(hda_opts, "heads", num);
2054 snprintf(num, sizeof(num), "%d", secs);
2055 qemu_opt_set(hda_opts, "secs", num);
2056 if (translation == BIOS_ATA_TRANSLATION_LBA)
2057 qemu_opt_set(hda_opts, "trans", "lba");
2058 if (translation == BIOS_ATA_TRANSLATION_NONE)
2059 qemu_opt_set(hda_opts, "trans", "none");
2062 break;
2063 case QEMU_OPTION_numa:
2064 if (nb_numa_nodes >= MAX_NODES) {
2065 fprintf(stderr, "qemu: too many NUMA nodes\n");
2066 exit(1);
2068 numa_add(optarg);
2069 break;
2070 case QEMU_OPTION_nographic:
2071 display_type = DT_NOGRAPHIC;
2072 break;
2073 #ifdef CONFIG_CURSES
2074 case QEMU_OPTION_curses:
2075 display_type = DT_CURSES;
2076 break;
2077 #endif
2078 case QEMU_OPTION_portrait:
2079 graphic_rotate = 1;
2080 break;
2081 case QEMU_OPTION_kernel:
2082 kernel_filename = optarg;
2083 break;
2084 case QEMU_OPTION_append:
2085 kernel_cmdline = optarg;
2086 break;
2087 case QEMU_OPTION_cdrom:
2088 drive_add(optarg, CDROM_ALIAS);
2089 break;
2090 case QEMU_OPTION_boot:
2092 static const char * const params[] = {
2093 "order", "once", "menu", NULL
2095 char buf[sizeof(boot_devices)];
2096 char *standard_boot_devices;
2097 int legacy = 0;
2099 if (!strchr(optarg, '=')) {
2100 legacy = 1;
2101 pstrcpy(buf, sizeof(buf), optarg);
2102 } else if (check_params(buf, sizeof(buf), params, optarg) < 0) {
2103 fprintf(stderr,
2104 "qemu: unknown boot parameter '%s' in '%s'\n",
2105 buf, optarg);
2106 exit(1);
2109 if (legacy ||
2110 get_param_value(buf, sizeof(buf), "order", optarg)) {
2111 validate_bootdevices(buf);
2112 pstrcpy(boot_devices, sizeof(boot_devices), buf);
2114 if (!legacy) {
2115 if (get_param_value(buf, sizeof(buf),
2116 "once", optarg)) {
2117 validate_bootdevices(buf);
2118 standard_boot_devices = qemu_strdup(boot_devices);
2119 pstrcpy(boot_devices, sizeof(boot_devices), buf);
2120 qemu_register_reset(restore_boot_devices,
2121 standard_boot_devices);
2123 if (get_param_value(buf, sizeof(buf),
2124 "menu", optarg)) {
2125 if (!strcmp(buf, "on")) {
2126 boot_menu = 1;
2127 } else if (!strcmp(buf, "off")) {
2128 boot_menu = 0;
2129 } else {
2130 fprintf(stderr,
2131 "qemu: invalid option value '%s'\n",
2132 buf);
2133 exit(1);
2138 break;
2139 case QEMU_OPTION_fda:
2140 case QEMU_OPTION_fdb:
2141 drive_add(optarg, FD_ALIAS, popt->index - QEMU_OPTION_fda);
2142 break;
2143 case QEMU_OPTION_no_fd_bootchk:
2144 fd_bootchk = 0;
2145 break;
2146 case QEMU_OPTION_netdev:
2147 if (net_client_parse(qemu_find_opts("netdev"), optarg) == -1) {
2148 exit(1);
2150 break;
2151 case QEMU_OPTION_net:
2152 if (net_client_parse(qemu_find_opts("net"), optarg) == -1) {
2153 exit(1);
2155 break;
2156 #ifdef CONFIG_SLIRP
2157 case QEMU_OPTION_tftp:
2158 legacy_tftp_prefix = optarg;
2159 break;
2160 case QEMU_OPTION_bootp:
2161 legacy_bootp_filename = optarg;
2162 break;
2163 case QEMU_OPTION_redir:
2164 if (net_slirp_redir(optarg) < 0)
2165 exit(1);
2166 break;
2167 #endif
2168 case QEMU_OPTION_bt:
2169 add_device_config(DEV_BT, optarg);
2170 break;
2171 case QEMU_OPTION_audio_help:
2172 if (!(audio_available())) {
2173 printf("Option %s not supported for this target\n", popt->name);
2174 exit(1);
2176 AUD_help ();
2177 exit (0);
2178 break;
2179 case QEMU_OPTION_soundhw:
2180 if (!(audio_available())) {
2181 printf("Option %s not supported for this target\n", popt->name);
2182 exit(1);
2184 select_soundhw (optarg);
2185 break;
2186 case QEMU_OPTION_h:
2187 help(0);
2188 break;
2189 case QEMU_OPTION_version:
2190 version();
2191 exit(0);
2192 break;
2193 case QEMU_OPTION_m: {
2194 uint64_t value;
2195 char *ptr;
2197 value = strtoul(optarg, &ptr, 10);
2198 switch (*ptr) {
2199 case 0: case 'M': case 'm':
2200 value <<= 20;
2201 break;
2202 case 'G': case 'g':
2203 value <<= 30;
2204 break;
2205 default:
2206 fprintf(stderr, "qemu: invalid ram size: %s\n", optarg);
2207 exit(1);
2210 /* On 32-bit hosts, QEMU is limited by virtual address space */
2211 if (value > (2047 << 20) && HOST_LONG_BITS == 32) {
2212 fprintf(stderr, "qemu: at most 2047 MB RAM can be simulated\n");
2213 exit(1);
2215 if (value != (uint64_t)(ram_addr_t)value) {
2216 fprintf(stderr, "qemu: ram size too large\n");
2217 exit(1);
2219 ram_size = value;
2220 break;
2222 case QEMU_OPTION_mempath:
2223 mem_path = optarg;
2224 break;
2225 #ifdef MAP_POPULATE
2226 case QEMU_OPTION_mem_prealloc:
2227 mem_prealloc = 1;
2228 break;
2229 #endif
2230 case QEMU_OPTION_d:
2231 set_cpu_log(optarg);
2232 break;
2233 case QEMU_OPTION_s:
2234 gdbstub_dev = "tcp::" DEFAULT_GDBSTUB_PORT;
2235 break;
2236 case QEMU_OPTION_gdb:
2237 gdbstub_dev = optarg;
2238 break;
2239 case QEMU_OPTION_L:
2240 data_dir = optarg;
2241 break;
2242 case QEMU_OPTION_bios:
2243 bios_name = optarg;
2244 break;
2245 case QEMU_OPTION_singlestep:
2246 singlestep = 1;
2247 break;
2248 case QEMU_OPTION_S:
2249 autostart = 0;
2250 break;
2251 case QEMU_OPTION_k:
2252 keyboard_layout = optarg;
2253 break;
2254 case QEMU_OPTION_localtime:
2255 rtc_utc = 0;
2256 break;
2257 case QEMU_OPTION_vga:
2258 select_vgahw (optarg);
2259 break;
2260 case QEMU_OPTION_g:
2262 const char *p;
2263 int w, h, depth;
2264 p = optarg;
2265 w = strtol(p, (char **)&p, 10);
2266 if (w <= 0) {
2267 graphic_error:
2268 fprintf(stderr, "qemu: invalid resolution or depth\n");
2269 exit(1);
2271 if (*p != 'x')
2272 goto graphic_error;
2273 p++;
2274 h = strtol(p, (char **)&p, 10);
2275 if (h <= 0)
2276 goto graphic_error;
2277 if (*p == 'x') {
2278 p++;
2279 depth = strtol(p, (char **)&p, 10);
2280 if (depth != 8 && depth != 15 && depth != 16 &&
2281 depth != 24 && depth != 32)
2282 goto graphic_error;
2283 } else if (*p == '\0') {
2284 depth = graphic_depth;
2285 } else {
2286 goto graphic_error;
2289 graphic_width = w;
2290 graphic_height = h;
2291 graphic_depth = depth;
2293 break;
2294 case QEMU_OPTION_echr:
2296 char *r;
2297 term_escape_char = strtol(optarg, &r, 0);
2298 if (r == optarg)
2299 printf("Bad argument to echr\n");
2300 break;
2302 case QEMU_OPTION_monitor:
2303 monitor_parse(optarg, "readline");
2304 default_monitor = 0;
2305 break;
2306 case QEMU_OPTION_qmp:
2307 monitor_parse(optarg, "control");
2308 default_monitor = 0;
2309 break;
2310 case QEMU_OPTION_mon:
2311 opts = qemu_opts_parse(qemu_find_opts("mon"), optarg, 1);
2312 if (!opts) {
2313 exit(1);
2315 default_monitor = 0;
2316 break;
2317 case QEMU_OPTION_chardev:
2318 opts = qemu_opts_parse(qemu_find_opts("chardev"), optarg, 1);
2319 if (!opts) {
2320 exit(1);
2322 break;
2323 case QEMU_OPTION_fsdev:
2324 olist = qemu_find_opts("fsdev");
2325 if (!olist) {
2326 fprintf(stderr, "fsdev is not supported by this qemu build.\n");
2327 exit(1);
2329 opts = qemu_opts_parse(olist, optarg, 1);
2330 if (!opts) {
2331 fprintf(stderr, "parse error: %s\n", optarg);
2332 exit(1);
2334 break;
2335 case QEMU_OPTION_virtfs: {
2336 char *arg_fsdev = NULL;
2337 char *arg_9p = NULL;
2338 int len = 0;
2340 olist = qemu_find_opts("virtfs");
2341 if (!olist) {
2342 fprintf(stderr, "virtfs is not supported by this qemu build.\n");
2343 exit(1);
2345 opts = qemu_opts_parse(olist, optarg, 1);
2346 if (!opts) {
2347 fprintf(stderr, "parse error: %s\n", optarg);
2348 exit(1);
2351 if (qemu_opt_get(opts, "fstype") == NULL ||
2352 qemu_opt_get(opts, "mount_tag") == NULL ||
2353 qemu_opt_get(opts, "path") == NULL ||
2354 qemu_opt_get(opts, "security_model") == NULL) {
2355 fprintf(stderr, "Usage: -virtfs fstype,path=/share_path/,"
2356 "security_model=[mapped|passthrough|none],"
2357 "mnt_tag=tag.\n");
2358 exit(1);
2361 len = strlen(",id=,path=,security_model=");
2362 len += strlen(qemu_opt_get(opts, "fstype"));
2363 len += strlen(qemu_opt_get(opts, "mount_tag"));
2364 len += strlen(qemu_opt_get(opts, "path"));
2365 len += strlen(qemu_opt_get(opts, "security_model"));
2366 arg_fsdev = qemu_malloc((len + 1) * sizeof(*arg_fsdev));
2368 snprintf(arg_fsdev, (len + 1) * sizeof(*arg_fsdev),
2369 "%s,id=%s,path=%s,security_model=%s",
2370 qemu_opt_get(opts, "fstype"),
2371 qemu_opt_get(opts, "mount_tag"),
2372 qemu_opt_get(opts, "path"),
2373 qemu_opt_get(opts, "security_model"));
2375 len = strlen("virtio-9p-pci,fsdev=,mount_tag=");
2376 len += 2*strlen(qemu_opt_get(opts, "mount_tag"));
2377 arg_9p = qemu_malloc((len + 1) * sizeof(*arg_9p));
2379 snprintf(arg_9p, (len + 1) * sizeof(*arg_9p),
2380 "virtio-9p-pci,fsdev=%s,mount_tag=%s",
2381 qemu_opt_get(opts, "mount_tag"),
2382 qemu_opt_get(opts, "mount_tag"));
2384 if (!qemu_opts_parse(qemu_find_opts("fsdev"), arg_fsdev, 1)) {
2385 fprintf(stderr, "parse error [fsdev]: %s\n", optarg);
2386 exit(1);
2389 if (!qemu_opts_parse(qemu_find_opts("device"), arg_9p, 1)) {
2390 fprintf(stderr, "parse error [device]: %s\n", optarg);
2391 exit(1);
2394 qemu_free(arg_fsdev);
2395 qemu_free(arg_9p);
2396 break;
2398 case QEMU_OPTION_serial:
2399 add_device_config(DEV_SERIAL, optarg);
2400 default_serial = 0;
2401 if (strncmp(optarg, "mon:", 4) == 0) {
2402 default_monitor = 0;
2404 break;
2405 case QEMU_OPTION_watchdog:
2406 if (watchdog) {
2407 fprintf(stderr,
2408 "qemu: only one watchdog option may be given\n");
2409 return 1;
2411 watchdog = optarg;
2412 break;
2413 case QEMU_OPTION_watchdog_action:
2414 if (select_watchdog_action(optarg) == -1) {
2415 fprintf(stderr, "Unknown -watchdog-action parameter\n");
2416 exit(1);
2418 break;
2419 case QEMU_OPTION_virtiocon:
2420 add_device_config(DEV_VIRTCON, optarg);
2421 default_virtcon = 0;
2422 if (strncmp(optarg, "mon:", 4) == 0) {
2423 default_monitor = 0;
2425 break;
2426 case QEMU_OPTION_parallel:
2427 add_device_config(DEV_PARALLEL, optarg);
2428 default_parallel = 0;
2429 if (strncmp(optarg, "mon:", 4) == 0) {
2430 default_monitor = 0;
2432 break;
2433 case QEMU_OPTION_debugcon:
2434 add_device_config(DEV_DEBUGCON, optarg);
2435 break;
2436 case QEMU_OPTION_loadvm:
2437 loadvm = optarg;
2438 break;
2439 case QEMU_OPTION_full_screen:
2440 full_screen = 1;
2441 break;
2442 #ifdef CONFIG_SDL
2443 case QEMU_OPTION_no_frame:
2444 no_frame = 1;
2445 break;
2446 case QEMU_OPTION_alt_grab:
2447 alt_grab = 1;
2448 break;
2449 case QEMU_OPTION_ctrl_grab:
2450 ctrl_grab = 1;
2451 break;
2452 case QEMU_OPTION_no_quit:
2453 no_quit = 1;
2454 break;
2455 case QEMU_OPTION_sdl:
2456 display_type = DT_SDL;
2457 break;
2458 #endif
2459 case QEMU_OPTION_pidfile:
2460 pid_file = optarg;
2461 break;
2462 case QEMU_OPTION_win2k_hack:
2463 win2k_install_hack = 1;
2464 break;
2465 case QEMU_OPTION_rtc_td_hack:
2466 rtc_td_hack = 1;
2467 break;
2468 case QEMU_OPTION_acpitable:
2469 do_acpitable_option(optarg);
2470 break;
2471 case QEMU_OPTION_smbios:
2472 do_smbios_option(optarg);
2473 break;
2474 #ifdef OBSOLETE_KVM_IMPL
2475 case QEMU_OPTION_enable_kvm:
2476 kvm_allowed = 1;
2477 #endif
2478 break;
2479 case QEMU_OPTION_no_kvm:
2480 kvm_allowed = 0;
2481 #ifdef CONFIG_NO_CPU_EMULATION
2482 fprintf(stderr, "cpu emulation not configured\n");
2483 exit(1);
2484 #endif
2485 break;
2486 #ifdef CONFIG_KVM
2487 case QEMU_OPTION_no_kvm_irqchip: {
2488 kvm_irqchip = 0;
2489 kvm_pit = 0;
2490 break;
2492 case QEMU_OPTION_no_kvm_pit: {
2493 kvm_pit = 0;
2494 break;
2496 case QEMU_OPTION_no_kvm_pit_reinjection: {
2497 kvm_pit_reinject = 0;
2498 break;
2500 case QEMU_OPTION_enable_nesting: {
2501 kvm_nested = 1;
2502 break;
2504 #endif
2505 #if defined(TARGET_I386) || defined(TARGET_X86_64) || defined(TARGET_IA64) || defined(__linux__)
2506 case QEMU_OPTION_pcidevice:
2507 if (assigned_devices_index >= MAX_DEV_ASSIGN_CMDLINE) {
2508 fprintf(stderr, "Too many assigned devices\n");
2509 exit(1);
2511 assigned_devices[assigned_devices_index] = optarg;
2512 assigned_devices_index++;
2513 break;
2514 #endif
2515 case QEMU_OPTION_usb:
2516 usb_enabled = 1;
2517 break;
2518 case QEMU_OPTION_usbdevice:
2519 usb_enabled = 1;
2520 add_device_config(DEV_USB, optarg);
2521 break;
2522 case QEMU_OPTION_device:
2523 if (!qemu_opts_parse(qemu_find_opts("device"), optarg, 1)) {
2524 exit(1);
2526 break;
2527 case QEMU_OPTION_smp:
2528 smp_parse(optarg);
2529 if (smp_cpus < 1) {
2530 fprintf(stderr, "Invalid number of CPUs\n");
2531 exit(1);
2533 if (max_cpus < smp_cpus) {
2534 fprintf(stderr, "maxcpus must be equal to or greater than "
2535 "smp\n");
2536 exit(1);
2538 if (max_cpus > 255) {
2539 fprintf(stderr, "Unsupported number of maxcpus\n");
2540 exit(1);
2542 break;
2543 case QEMU_OPTION_vnc:
2544 display_remote++;
2545 vnc_display = optarg;
2546 break;
2547 case QEMU_OPTION_no_acpi:
2548 acpi_enabled = 0;
2549 break;
2550 case QEMU_OPTION_no_hpet:
2551 no_hpet = 1;
2552 break;
2553 case QEMU_OPTION_balloon:
2554 if (balloon_parse(optarg) < 0) {
2555 fprintf(stderr, "Unknown -balloon argument %s\n", optarg);
2556 exit(1);
2558 break;
2559 case QEMU_OPTION_no_reboot:
2560 no_reboot = 1;
2561 break;
2562 case QEMU_OPTION_no_shutdown:
2563 no_shutdown = 1;
2564 break;
2565 case QEMU_OPTION_show_cursor:
2566 cursor_hide = 0;
2567 break;
2568 case QEMU_OPTION_uuid:
2569 if(qemu_uuid_parse(optarg, qemu_uuid) < 0) {
2570 fprintf(stderr, "Fail to parse UUID string."
2571 " Wrong format.\n");
2572 exit(1);
2574 break;
2575 case QEMU_OPTION_option_rom:
2576 if (nb_option_roms >= MAX_OPTION_ROMS) {
2577 fprintf(stderr, "Too many option ROMs\n");
2578 exit(1);
2580 option_rom[nb_option_roms] = optarg;
2581 nb_option_roms++;
2582 break;
2583 case QEMU_OPTION_semihosting:
2584 semihosting_enabled = 1;
2585 break;
2586 case QEMU_OPTION_tdf:
2587 time_drift_fix = 1;
2588 break;
2589 case QEMU_OPTION_kvm_shadow_memory:
2590 kvm_shadow_memory = (int64_t)atoi(optarg) * 1024 * 1024 / 4096;
2591 break;
2592 case QEMU_OPTION_name:
2593 qemu_name = qemu_strdup(optarg);
2595 char *p = strchr(qemu_name, ',');
2596 if (p != NULL) {
2597 *p++ = 0;
2598 if (strncmp(p, "process=", 8)) {
2599 fprintf(stderr, "Unknown subargument %s to -name", p);
2600 exit(1);
2602 p += 8;
2603 os_set_proc_name(p);
2606 break;
2607 case QEMU_OPTION_prom_env:
2608 if (nb_prom_envs >= MAX_PROM_ENVS) {
2609 fprintf(stderr, "Too many prom variables\n");
2610 exit(1);
2612 prom_envs[nb_prom_envs] = optarg;
2613 nb_prom_envs++;
2614 break;
2615 case QEMU_OPTION_old_param:
2616 old_param = 1;
2617 break;
2618 case QEMU_OPTION_clock:
2619 configure_alarms(optarg);
2620 break;
2621 case QEMU_OPTION_startdate:
2622 configure_rtc_date_offset(optarg, 1);
2623 break;
2624 case QEMU_OPTION_rtc:
2625 opts = qemu_opts_parse(qemu_find_opts("rtc"), optarg, 0);
2626 if (!opts) {
2627 exit(1);
2629 configure_rtc(opts);
2630 break;
2631 case QEMU_OPTION_tb_size:
2632 tb_size = strtol(optarg, NULL, 0);
2633 if (tb_size < 0)
2634 tb_size = 0;
2635 break;
2636 case QEMU_OPTION_icount:
2637 icount_option = optarg;
2638 break;
2639 case QEMU_OPTION_incoming:
2640 incoming = optarg;
2641 incoming_expected = true;
2642 break;
2643 case QEMU_OPTION_nodefaults:
2644 default_serial = 0;
2645 default_parallel = 0;
2646 default_virtcon = 0;
2647 default_monitor = 0;
2648 default_vga = 0;
2649 default_net = 0;
2650 default_floppy = 0;
2651 default_cdrom = 0;
2652 default_sdcard = 0;
2653 break;
2654 #ifndef _WIN32
2655 case QEMU_OPTION_nvram:
2656 nvram = optarg;
2657 break;
2658 #endif
2659 case QEMU_OPTION_xen_domid:
2660 if (!(xen_available())) {
2661 printf("Option %s not supported for this target\n", popt->name);
2662 exit(1);
2664 xen_domid = atoi(optarg);
2665 break;
2666 case QEMU_OPTION_xen_create:
2667 if (!(xen_available())) {
2668 printf("Option %s not supported for this target\n", popt->name);
2669 exit(1);
2671 xen_mode = XEN_CREATE;
2672 break;
2673 case QEMU_OPTION_xen_attach:
2674 if (!(xen_available())) {
2675 printf("Option %s not supported for this target\n", popt->name);
2676 exit(1);
2678 xen_mode = XEN_ATTACH;
2679 break;
2680 #ifdef CONFIG_SIMPLE_TRACE
2681 case QEMU_OPTION_trace:
2682 opts = qemu_opts_parse(qemu_find_opts("trace"), optarg, 0);
2683 if (opts) {
2684 trace_file = qemu_opt_get(opts, "file");
2686 break;
2687 #endif
2688 case QEMU_OPTION_readconfig:
2690 int ret = qemu_read_config_file(optarg);
2691 if (ret < 0) {
2692 fprintf(stderr, "read config %s: %s\n", optarg,
2693 strerror(-ret));
2694 exit(1);
2696 break;
2698 case QEMU_OPTION_spice:
2699 olist = qemu_find_opts("spice");
2700 if (!olist) {
2701 fprintf(stderr, "spice is not supported by this qemu build.\n");
2702 exit(1);
2704 opts = qemu_opts_parse(olist, optarg, 0);
2705 if (!opts) {
2706 fprintf(stderr, "parse error: %s\n", optarg);
2707 exit(1);
2709 break;
2710 case QEMU_OPTION_writeconfig:
2712 FILE *fp;
2713 if (strcmp(optarg, "-") == 0) {
2714 fp = stdout;
2715 } else {
2716 fp = fopen(optarg, "w");
2717 if (fp == NULL) {
2718 fprintf(stderr, "open %s: %s\n", optarg, strerror(errno));
2719 exit(1);
2722 qemu_config_write(fp);
2723 fclose(fp);
2724 break;
2726 default:
2727 os_parse_cmd_args(popt->index, optarg);
2731 loc_set_none();
2733 /* If no data_dir is specified then try to find it relative to the
2734 executable path. */
2735 if (!data_dir) {
2736 data_dir = os_find_datadir(argv[0]);
2738 /* If all else fails use the install patch specified when building. */
2739 if (!data_dir) {
2740 data_dir = CONFIG_QEMU_DATADIR;
2743 #ifdef CONFIG_SIMPLE_TRACE
2745 * Set the trace file name, if specified.
2747 st_set_trace_file(trace_file);
2748 #endif
2750 * Default to max_cpus = smp_cpus, in case the user doesn't
2751 * specify a max_cpus value.
2753 if (!max_cpus)
2754 max_cpus = smp_cpus;
2756 machine->max_cpus = machine->max_cpus ?: 1; /* Default to UP */
2757 if (smp_cpus > machine->max_cpus) {
2758 fprintf(stderr, "Number of SMP cpus requested (%d), exceeds max cpus "
2759 "supported by machine `%s' (%d)\n", smp_cpus, machine->name,
2760 machine->max_cpus);
2761 exit(1);
2764 qemu_opts_foreach(qemu_find_opts("device"), default_driver_check, NULL, 0);
2765 qemu_opts_foreach(qemu_find_opts("global"), default_driver_check, NULL, 0);
2767 if (machine->no_serial) {
2768 default_serial = 0;
2770 if (machine->no_parallel) {
2771 default_parallel = 0;
2773 if (!machine->use_virtcon) {
2774 default_virtcon = 0;
2776 if (machine->no_vga) {
2777 default_vga = 0;
2779 if (machine->no_floppy) {
2780 default_floppy = 0;
2782 if (machine->no_cdrom) {
2783 default_cdrom = 0;
2785 if (machine->no_sdcard) {
2786 default_sdcard = 0;
2789 if (display_type == DT_NOGRAPHIC) {
2790 if (default_parallel)
2791 add_device_config(DEV_PARALLEL, "null");
2792 if (default_serial && default_monitor) {
2793 add_device_config(DEV_SERIAL, "mon:stdio");
2794 } else if (default_virtcon && default_monitor) {
2795 add_device_config(DEV_VIRTCON, "mon:stdio");
2796 } else {
2797 if (default_serial)
2798 add_device_config(DEV_SERIAL, "stdio");
2799 if (default_virtcon)
2800 add_device_config(DEV_VIRTCON, "stdio");
2801 if (default_monitor)
2802 monitor_parse("stdio", "readline");
2804 } else {
2805 if (default_serial)
2806 add_device_config(DEV_SERIAL, "vc:80Cx24C");
2807 if (default_parallel)
2808 add_device_config(DEV_PARALLEL, "vc:80Cx24C");
2809 if (default_monitor)
2810 monitor_parse("vc:80Cx24C", "readline");
2811 if (default_virtcon)
2812 add_device_config(DEV_VIRTCON, "vc:80Cx24C");
2814 if (default_vga)
2815 vga_interface_type = VGA_CIRRUS;
2817 socket_init();
2819 if (qemu_opts_foreach(qemu_find_opts("chardev"), chardev_init_func, NULL, 1) != 0)
2820 exit(1);
2821 #ifdef CONFIG_VIRTFS
2822 if (qemu_opts_foreach(qemu_find_opts("fsdev"), fsdev_init_func, NULL, 1) != 0) {
2823 exit(1);
2825 #endif
2827 os_daemonize();
2829 if (pid_file && qemu_create_pidfile(pid_file) != 0) {
2830 os_pidfile_error();
2831 exit(1);
2834 if (kvm_allowed) {
2835 int ret = kvm_init(smp_cpus);
2836 if (ret < 0) {
2837 #if defined(OBSOLETE_KVM_IMPL) || defined(CONFIG_NO_CPU_EMULATION)
2838 if (!kvm_available()) {
2839 printf("KVM not supported for this target\n");
2840 } else {
2841 fprintf(stderr, "failed to initialize KVM: %s\n", strerror(-ret));
2843 exit(1);
2844 #endif
2845 #ifdef CONFIG_KVM
2846 fprintf(stderr, "Could not initialize KVM, will disable KVM support\n");
2847 kvm_allowed = 0;
2848 #endif
2852 if (qemu_init_main_loop()) {
2853 fprintf(stderr, "qemu_init_main_loop failed\n");
2854 exit(1);
2856 linux_boot = (kernel_filename != NULL);
2858 if (!linux_boot && *kernel_cmdline != '\0') {
2859 fprintf(stderr, "-append only allowed with -kernel option\n");
2860 exit(1);
2863 if (!linux_boot && initrd_filename != NULL) {
2864 fprintf(stderr, "-initrd only allowed with -kernel option\n");
2865 exit(1);
2868 os_set_line_buffering();
2870 if (init_timer_alarm() < 0) {
2871 fprintf(stderr, "could not initialize alarm timer\n");
2872 exit(1);
2874 configure_icount(icount_option);
2876 if (net_init_clients() < 0) {
2877 exit(1);
2880 /* init the bluetooth world */
2881 if (foreach_device_config(DEV_BT, bt_parse))
2882 exit(1);
2884 /* init the memory */
2885 if (ram_size == 0)
2886 ram_size = DEFAULT_RAM_SIZE * 1024 * 1024;
2888 /* init the dynamic translator */
2889 cpu_exec_init_all(tb_size * 1024 * 1024);
2891 bdrv_init_with_whitelist();
2893 blk_mig_init();
2895 if (default_cdrom) {
2896 /* we always create the cdrom drive, even if no disk is there */
2897 drive_add(NULL, CDROM_ALIAS);
2900 if (default_floppy) {
2901 /* we always create at least one floppy */
2902 drive_add(NULL, FD_ALIAS, 0);
2905 if (default_sdcard) {
2906 /* we always create one sd slot, even if no card is in it */
2907 drive_add(NULL, SD_ALIAS);
2910 /* open the virtual block devices */
2911 if (snapshot)
2912 qemu_opts_foreach(qemu_find_opts("drive"), drive_enable_snapshot, NULL, 0);
2913 if (qemu_opts_foreach(qemu_find_opts("drive"), drive_init_func, &machine->use_scsi, 1) != 0)
2914 exit(1);
2916 register_savevm_live(NULL, "ram", 0, 4, NULL, ram_save_live, NULL,
2917 ram_load, NULL);
2919 if (nb_numa_nodes > 0) {
2920 int i;
2922 if (nb_numa_nodes > smp_cpus) {
2923 nb_numa_nodes = smp_cpus;
2926 /* If no memory size if given for any node, assume the default case
2927 * and distribute the available memory equally across all nodes
2929 for (i = 0; i < nb_numa_nodes; i++) {
2930 if (node_mem[i] != 0)
2931 break;
2933 if (i == nb_numa_nodes) {
2934 uint64_t usedmem = 0;
2936 /* On Linux, the each node's border has to be 8MB aligned,
2937 * the final node gets the rest.
2939 for (i = 0; i < nb_numa_nodes - 1; i++) {
2940 node_mem[i] = (ram_size / nb_numa_nodes) & ~((1 << 23UL) - 1);
2941 usedmem += node_mem[i];
2943 node_mem[i] = ram_size - usedmem;
2946 for (i = 0; i < nb_numa_nodes; i++) {
2947 if (node_cpumask[i] != 0)
2948 break;
2950 /* assigning the VCPUs round-robin is easier to implement, guest OSes
2951 * must cope with this anyway, because there are BIOSes out there in
2952 * real machines which also use this scheme.
2954 if (i == nb_numa_nodes) {
2955 for (i = 0; i < smp_cpus; i++) {
2956 node_cpumask[i % nb_numa_nodes] |= 1 << i;
2961 if (qemu_opts_foreach(qemu_find_opts("mon"), mon_init_func, NULL, 1) != 0) {
2962 exit(1);
2965 if (foreach_device_config(DEV_SERIAL, serial_parse) < 0)
2966 exit(1);
2967 if (foreach_device_config(DEV_PARALLEL, parallel_parse) < 0)
2968 exit(1);
2969 if (foreach_device_config(DEV_VIRTCON, virtcon_parse) < 0)
2970 exit(1);
2971 if (foreach_device_config(DEV_DEBUGCON, debugcon_parse) < 0)
2972 exit(1);
2974 module_call_init(MODULE_INIT_DEVICE);
2976 if (qemu_opts_foreach(qemu_find_opts("device"), device_help_func, NULL, 0) != 0)
2977 exit(0);
2979 if (watchdog) {
2980 i = select_watchdog(watchdog);
2981 if (i > 0)
2982 exit (i == 1 ? 1 : 0);
2985 if (machine->compat_props) {
2986 qdev_prop_register_global_list(machine->compat_props);
2988 qemu_add_globals();
2990 machine->init(ram_size, boot_devices,
2991 kernel_filename, kernel_cmdline, initrd_filename, cpu_model);
2993 cpu_synchronize_all_post_init();
2995 /* must be after terminal init, SDL library changes signal handlers */
2996 os_setup_signal_handling();
2998 set_numa_modes();
3000 current_machine = machine;
3002 /* init USB devices */
3003 if (usb_enabled) {
3004 if (foreach_device_config(DEV_USB, usb_parse) < 0)
3005 exit(1);
3008 /* init generic devices */
3009 if (qemu_opts_foreach(qemu_find_opts("device"), device_init_func, NULL, 1) != 0)
3010 exit(1);
3012 net_check_clients();
3014 /* just use the first displaystate for the moment */
3015 ds = get_displaystate();
3017 if (using_spice)
3018 display_remote++;
3019 if (display_type == DT_DEFAULT && !display_remote) {
3020 #if defined(CONFIG_SDL) || defined(CONFIG_COCOA)
3021 display_type = DT_SDL;
3022 #else
3023 vnc_display = "localhost:0,to=99";
3024 show_vnc_port = 1;
3025 #endif
3029 /* init local displays */
3030 switch (display_type) {
3031 case DT_NOGRAPHIC:
3032 break;
3033 #if defined(CONFIG_CURSES)
3034 case DT_CURSES:
3035 curses_display_init(ds, full_screen);
3036 break;
3037 #endif
3038 #if defined(CONFIG_SDL)
3039 case DT_SDL:
3040 sdl_display_init(ds, full_screen, no_frame);
3041 break;
3042 #elif defined(CONFIG_COCOA)
3043 case DT_SDL:
3044 cocoa_display_init(ds, full_screen);
3045 break;
3046 #endif
3047 default:
3048 break;
3051 /* init remote displays */
3052 if (vnc_display) {
3053 vnc_display_init(ds);
3054 if (vnc_display_open(ds, vnc_display) < 0)
3055 exit(1);
3057 if (show_vnc_port) {
3058 printf("VNC server running on `%s'\n", vnc_display_local_addr(ds));
3061 #ifdef CONFIG_SPICE
3062 if (using_spice) {
3063 qemu_spice_display_init(ds);
3065 #endif
3067 /* display setup */
3068 dpy_resize(ds);
3069 dcl = ds->listeners;
3070 while (dcl != NULL) {
3071 if (dcl->dpy_refresh != NULL) {
3072 ds->gui_timer = qemu_new_timer(rt_clock, gui_update, ds);
3073 qemu_mod_timer(ds->gui_timer, qemu_get_clock(rt_clock));
3074 break;
3076 dcl = dcl->next;
3078 if (ds->gui_timer == NULL) {
3079 nographic_timer = qemu_new_timer(rt_clock, nographic_update, NULL);
3080 qemu_mod_timer(nographic_timer, qemu_get_clock(rt_clock));
3082 text_consoles_set_display(ds);
3084 if (gdbstub_dev && gdbserver_start(gdbstub_dev) < 0) {
3085 fprintf(stderr, "qemu: could not open gdbserver on device '%s'\n",
3086 gdbstub_dev);
3087 exit(1);
3090 qdev_machine_creation_done();
3092 if (rom_load_all() != 0) {
3093 fprintf(stderr, "rom loading failed\n");
3094 exit(1);
3097 qemu_system_reset();
3098 if (loadvm) {
3099 if (load_vmstate(loadvm) < 0) {
3100 autostart = 0;
3104 if (incoming) {
3105 int ret = qemu_start_incoming_migration(incoming);
3106 if (ret < 0) {
3107 fprintf(stderr, "Migration failed. Exit code %s(%d), exiting.\n",
3108 incoming, ret);
3109 exit(ret);
3111 } else if (autostart) {
3112 vm_start();
3115 os_setup_post();
3117 main_loop();
3118 quit_timers();
3119 net_cleanup();
3121 return 0;