Merge remote branch 'qemu-kvm/uq/master' into staging
[qemu/wangdongxu.git] / vl.c
blobac4721110c8323f67f7c2f9db56806d376b05435
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>
51 #ifdef CONFIG_BSD
52 #include <sys/stat.h>
53 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__DragonFly__)
54 #include <libutil.h>
55 #include <sys/sysctl.h>
56 #else
57 #include <util.h>
58 #endif
59 #else
60 #ifdef __linux__
61 #include <pty.h>
62 #include <malloc.h>
64 #include <linux/ppdev.h>
65 #include <linux/parport.h>
66 #endif
67 #ifdef __sun__
68 #include <sys/stat.h>
69 #include <sys/ethernet.h>
70 #include <sys/sockio.h>
71 #include <netinet/arp.h>
72 #include <netinet/in_systm.h>
73 #include <netinet/ip.h>
74 #include <netinet/ip_icmp.h> // must come after ip.h
75 #include <netinet/udp.h>
76 #include <netinet/tcp.h>
77 #include <net/if.h>
78 #include <syslog.h>
79 #include <stropts.h>
80 #endif
81 #endif
82 #endif
84 #if defined(__OpenBSD__)
85 #include <util.h>
86 #endif
88 #if defined(CONFIG_VDE)
89 #include <libvdeplug.h>
90 #endif
92 #ifdef _WIN32
93 #include <windows.h>
94 #endif
96 #ifdef CONFIG_SDL
97 #if defined(__APPLE__) || defined(main)
98 #include <SDL.h>
99 int qemu_main(int argc, char **argv, char **envp);
100 int main(int argc, char **argv)
102 return qemu_main(argc, argv, NULL);
104 #undef main
105 #define main qemu_main
106 #endif
107 #endif /* CONFIG_SDL */
109 #ifdef CONFIG_COCOA
110 #undef main
111 #define main qemu_main
112 #endif /* CONFIG_COCOA */
114 #include "hw/hw.h"
115 #include "hw/boards.h"
116 #include "hw/usb.h"
117 #include "hw/pcmcia.h"
118 #include "hw/pc.h"
119 #include "hw/isa.h"
120 #include "hw/baum.h"
121 #include "hw/bt.h"
122 #include "hw/watchdog.h"
123 #include "hw/smbios.h"
124 #include "hw/xen.h"
125 #include "hw/qdev.h"
126 #include "hw/loader.h"
127 #include "bt-host.h"
128 #include "net.h"
129 #include "net/slirp.h"
130 #include "monitor.h"
131 #include "console.h"
132 #include "sysemu.h"
133 #include "gdbstub.h"
134 #include "qemu-timer.h"
135 #include "qemu-char.h"
136 #include "cache-utils.h"
137 #include "block.h"
138 #include "blockdev.h"
139 #include "block-migration.h"
140 #include "dma.h"
141 #include "audio/audio.h"
142 #include "migration.h"
143 #include "kvm.h"
144 #include "qemu-option.h"
145 #include "qemu-config.h"
146 #include "qemu-objects.h"
147 #include "qemu-options.h"
148 #ifdef CONFIG_VIRTFS
149 #include "fsdev/qemu-fsdev.h"
150 #endif
152 #include "disas.h"
154 #include "qemu_socket.h"
156 #include "slirp/libslirp.h"
158 #include "trace.h"
159 #include "simpletrace.h"
160 #include "qemu-queue.h"
161 #include "cpus.h"
162 #include "arch_init.h"
164 #include "ui/qemu-spice.h"
166 //#define DEBUG_NET
167 //#define DEBUG_SLIRP
169 #define DEFAULT_RAM_SIZE 128
171 #define MAX_VIRTIO_CONSOLES 1
173 static const char *data_dir;
174 const char *bios_name = NULL;
175 enum vga_retrace_method vga_retrace_method = VGA_RETRACE_DUMB;
176 DisplayType display_type = DT_DEFAULT;
177 int display_remote = 0;
178 const char* keyboard_layout = NULL;
179 ram_addr_t ram_size;
180 const char *mem_path = NULL;
181 #ifdef MAP_POPULATE
182 int mem_prealloc = 0; /* force preallocation of physical target memory */
183 #endif
184 int nb_nics;
185 NICInfo nd_table[MAX_NICS];
186 int vm_running;
187 int autostart;
188 int incoming_expected; /* Started with -incoming and waiting for incoming */
189 static int rtc_utc = 1;
190 static int rtc_date_offset = -1; /* -1 means no change */
191 QEMUClock *rtc_clock;
192 int vga_interface_type = VGA_NONE;
193 static int full_screen = 0;
194 #ifdef CONFIG_SDL
195 static int no_frame = 0;
196 #endif
197 int no_quit = 0;
198 CharDriverState *serial_hds[MAX_SERIAL_PORTS];
199 CharDriverState *parallel_hds[MAX_PARALLEL_PORTS];
200 CharDriverState *virtcon_hds[MAX_VIRTIO_CONSOLES];
201 int win2k_install_hack = 0;
202 int rtc_td_hack = 0;
203 int usb_enabled = 0;
204 int singlestep = 0;
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 QEMUOptionRom option_rom[MAX_OPTION_ROMS];
220 int nb_option_roms;
221 int semihosting_enabled = 0;
222 int old_param = 0;
223 const char *qemu_name;
224 int alt_grab = 0;
225 int ctrl_grab = 0;
226 unsigned int nb_prom_envs = 0;
227 const char *prom_envs[MAX_PROM_ENVS];
228 int boot_menu;
230 typedef struct FWBootEntry FWBootEntry;
232 struct FWBootEntry {
233 QTAILQ_ENTRY(FWBootEntry) link;
234 int32_t bootindex;
235 DeviceState *dev;
236 char *suffix;
239 QTAILQ_HEAD(, FWBootEntry) fw_boot_order = QTAILQ_HEAD_INITIALIZER(fw_boot_order);
241 int nb_numa_nodes;
242 uint64_t node_mem[MAX_NODES];
243 uint64_t node_cpumask[MAX_NODES];
245 static QEMUTimer *nographic_timer;
247 uint8_t qemu_uuid[16];
249 static QEMUBootSetHandler *boot_set_handler;
250 static void *boot_set_opaque;
252 static NotifierList exit_notifiers =
253 NOTIFIER_LIST_INITIALIZER(exit_notifiers);
255 static NotifierList machine_init_done_notifiers =
256 NOTIFIER_LIST_INITIALIZER(machine_init_done_notifiers);
258 int kvm_allowed = 0;
259 uint32_t xen_domid;
260 enum xen_mode xen_mode = XEN_EMULATE;
262 static int default_serial = 1;
263 static int default_parallel = 1;
264 static int default_virtcon = 1;
265 static int default_monitor = 1;
266 static int default_vga = 1;
267 static int default_floppy = 1;
268 static int default_cdrom = 1;
269 static int default_sdcard = 1;
271 static struct {
272 const char *driver;
273 int *flag;
274 } default_list[] = {
275 { .driver = "isa-serial", .flag = &default_serial },
276 { .driver = "isa-parallel", .flag = &default_parallel },
277 { .driver = "isa-fdc", .flag = &default_floppy },
278 { .driver = "ide-drive", .flag = &default_cdrom },
279 { .driver = "virtio-serial-pci", .flag = &default_virtcon },
280 { .driver = "virtio-serial-s390", .flag = &default_virtcon },
281 { .driver = "virtio-serial", .flag = &default_virtcon },
282 { .driver = "VGA", .flag = &default_vga },
283 { .driver = "cirrus-vga", .flag = &default_vga },
284 { .driver = "vmware-svga", .flag = &default_vga },
287 static int default_driver_check(QemuOpts *opts, void *opaque)
289 const char *driver = qemu_opt_get(opts, "driver");
290 int i;
292 if (!driver)
293 return 0;
294 for (i = 0; i < ARRAY_SIZE(default_list); i++) {
295 if (strcmp(default_list[i].driver, driver) != 0)
296 continue;
297 *(default_list[i].flag) = 0;
299 return 0;
302 /***********************************************************/
303 /* real time host monotonic timer */
305 /***********************************************************/
306 /* host time/date access */
307 void qemu_get_timedate(struct tm *tm, int offset)
309 time_t ti;
310 struct tm *ret;
312 time(&ti);
313 ti += offset;
314 if (rtc_date_offset == -1) {
315 if (rtc_utc)
316 ret = gmtime(&ti);
317 else
318 ret = localtime(&ti);
319 } else {
320 ti -= rtc_date_offset;
321 ret = gmtime(&ti);
324 memcpy(tm, ret, sizeof(struct tm));
327 int qemu_timedate_diff(struct tm *tm)
329 time_t seconds;
331 if (rtc_date_offset == -1)
332 if (rtc_utc)
333 seconds = mktimegm(tm);
334 else
335 seconds = mktime(tm);
336 else
337 seconds = mktimegm(tm) + rtc_date_offset;
339 return seconds - time(NULL);
342 void rtc_change_mon_event(struct tm *tm)
344 QObject *data;
346 data = qobject_from_jsonf("{ 'offset': %d }", qemu_timedate_diff(tm));
347 monitor_protocol_event(QEVENT_RTC_CHANGE, data);
348 qobject_decref(data);
351 static void configure_rtc_date_offset(const char *startdate, int legacy)
353 time_t rtc_start_date;
354 struct tm tm;
356 if (!strcmp(startdate, "now") && legacy) {
357 rtc_date_offset = -1;
358 } else {
359 if (sscanf(startdate, "%d-%d-%dT%d:%d:%d",
360 &tm.tm_year,
361 &tm.tm_mon,
362 &tm.tm_mday,
363 &tm.tm_hour,
364 &tm.tm_min,
365 &tm.tm_sec) == 6) {
366 /* OK */
367 } else if (sscanf(startdate, "%d-%d-%d",
368 &tm.tm_year,
369 &tm.tm_mon,
370 &tm.tm_mday) == 3) {
371 tm.tm_hour = 0;
372 tm.tm_min = 0;
373 tm.tm_sec = 0;
374 } else {
375 goto date_fail;
377 tm.tm_year -= 1900;
378 tm.tm_mon--;
379 rtc_start_date = mktimegm(&tm);
380 if (rtc_start_date == -1) {
381 date_fail:
382 fprintf(stderr, "Invalid date format. Valid formats are:\n"
383 "'2006-06-17T16:01:21' or '2006-06-17'\n");
384 exit(1);
386 rtc_date_offset = time(NULL) - rtc_start_date;
390 static void configure_rtc(QemuOpts *opts)
392 const char *value;
394 value = qemu_opt_get(opts, "base");
395 if (value) {
396 if (!strcmp(value, "utc")) {
397 rtc_utc = 1;
398 } else if (!strcmp(value, "localtime")) {
399 rtc_utc = 0;
400 } else {
401 configure_rtc_date_offset(value, 0);
404 value = qemu_opt_get(opts, "clock");
405 if (value) {
406 if (!strcmp(value, "host")) {
407 rtc_clock = host_clock;
408 } else if (!strcmp(value, "vm")) {
409 rtc_clock = vm_clock;
410 } else {
411 fprintf(stderr, "qemu: invalid option value '%s'\n", value);
412 exit(1);
415 value = qemu_opt_get(opts, "driftfix");
416 if (value) {
417 if (!strcmp(value, "slew")) {
418 rtc_td_hack = 1;
419 } else if (!strcmp(value, "none")) {
420 rtc_td_hack = 0;
421 } else {
422 fprintf(stderr, "qemu: invalid option value '%s'\n", value);
423 exit(1);
428 /***********************************************************/
429 /* Bluetooth support */
430 static int nb_hcis;
431 static int cur_hci;
432 static struct HCIInfo *hci_table[MAX_NICS];
434 static struct bt_vlan_s {
435 struct bt_scatternet_s net;
436 int id;
437 struct bt_vlan_s *next;
438 } *first_bt_vlan;
440 /* find or alloc a new bluetooth "VLAN" */
441 static struct bt_scatternet_s *qemu_find_bt_vlan(int id)
443 struct bt_vlan_s **pvlan, *vlan;
444 for (vlan = first_bt_vlan; vlan != NULL; vlan = vlan->next) {
445 if (vlan->id == id)
446 return &vlan->net;
448 vlan = qemu_mallocz(sizeof(struct bt_vlan_s));
449 vlan->id = id;
450 pvlan = &first_bt_vlan;
451 while (*pvlan != NULL)
452 pvlan = &(*pvlan)->next;
453 *pvlan = vlan;
454 return &vlan->net;
457 static void null_hci_send(struct HCIInfo *hci, const uint8_t *data, int len)
461 static int null_hci_addr_set(struct HCIInfo *hci, const uint8_t *bd_addr)
463 return -ENOTSUP;
466 static struct HCIInfo null_hci = {
467 .cmd_send = null_hci_send,
468 .sco_send = null_hci_send,
469 .acl_send = null_hci_send,
470 .bdaddr_set = null_hci_addr_set,
473 struct HCIInfo *qemu_next_hci(void)
475 if (cur_hci == nb_hcis)
476 return &null_hci;
478 return hci_table[cur_hci++];
481 static struct HCIInfo *hci_init(const char *str)
483 char *endp;
484 struct bt_scatternet_s *vlan = 0;
486 if (!strcmp(str, "null"))
487 /* null */
488 return &null_hci;
489 else if (!strncmp(str, "host", 4) && (str[4] == '\0' || str[4] == ':'))
490 /* host[:hciN] */
491 return bt_host_hci(str[4] ? str + 5 : "hci0");
492 else if (!strncmp(str, "hci", 3)) {
493 /* hci[,vlan=n] */
494 if (str[3]) {
495 if (!strncmp(str + 3, ",vlan=", 6)) {
496 vlan = qemu_find_bt_vlan(strtol(str + 9, &endp, 0));
497 if (*endp)
498 vlan = 0;
500 } else
501 vlan = qemu_find_bt_vlan(0);
502 if (vlan)
503 return bt_new_hci(vlan);
506 fprintf(stderr, "qemu: Unknown bluetooth HCI `%s'.\n", str);
508 return 0;
511 static int bt_hci_parse(const char *str)
513 struct HCIInfo *hci;
514 bdaddr_t bdaddr;
516 if (nb_hcis >= MAX_NICS) {
517 fprintf(stderr, "qemu: Too many bluetooth HCIs (max %i).\n", MAX_NICS);
518 return -1;
521 hci = hci_init(str);
522 if (!hci)
523 return -1;
525 bdaddr.b[0] = 0x52;
526 bdaddr.b[1] = 0x54;
527 bdaddr.b[2] = 0x00;
528 bdaddr.b[3] = 0x12;
529 bdaddr.b[4] = 0x34;
530 bdaddr.b[5] = 0x56 + nb_hcis;
531 hci->bdaddr_set(hci, bdaddr.b);
533 hci_table[nb_hcis++] = hci;
535 return 0;
538 static void bt_vhci_add(int vlan_id)
540 struct bt_scatternet_s *vlan = qemu_find_bt_vlan(vlan_id);
542 if (!vlan->slave)
543 fprintf(stderr, "qemu: warning: adding a VHCI to "
544 "an empty scatternet %i\n", vlan_id);
546 bt_vhci_init(bt_new_hci(vlan));
549 static struct bt_device_s *bt_device_add(const char *opt)
551 struct bt_scatternet_s *vlan;
552 int vlan_id = 0;
553 char *endp = strstr(opt, ",vlan=");
554 int len = (endp ? endp - opt : strlen(opt)) + 1;
555 char devname[10];
557 pstrcpy(devname, MIN(sizeof(devname), len), opt);
559 if (endp) {
560 vlan_id = strtol(endp + 6, &endp, 0);
561 if (*endp) {
562 fprintf(stderr, "qemu: unrecognised bluetooth vlan Id\n");
563 return 0;
567 vlan = qemu_find_bt_vlan(vlan_id);
569 if (!vlan->slave)
570 fprintf(stderr, "qemu: warning: adding a slave device to "
571 "an empty scatternet %i\n", vlan_id);
573 if (!strcmp(devname, "keyboard"))
574 return bt_keyboard_init(vlan);
576 fprintf(stderr, "qemu: unsupported bluetooth device `%s'\n", devname);
577 return 0;
580 static int bt_parse(const char *opt)
582 const char *endp, *p;
583 int vlan;
585 if (strstart(opt, "hci", &endp)) {
586 if (!*endp || *endp == ',') {
587 if (*endp)
588 if (!strstart(endp, ",vlan=", 0))
589 opt = endp + 1;
591 return bt_hci_parse(opt);
593 } else if (strstart(opt, "vhci", &endp)) {
594 if (!*endp || *endp == ',') {
595 if (*endp) {
596 if (strstart(endp, ",vlan=", &p)) {
597 vlan = strtol(p, (char **) &endp, 0);
598 if (*endp) {
599 fprintf(stderr, "qemu: bad scatternet '%s'\n", p);
600 return 1;
602 } else {
603 fprintf(stderr, "qemu: bad parameter '%s'\n", endp + 1);
604 return 1;
606 } else
607 vlan = 0;
609 bt_vhci_add(vlan);
610 return 0;
612 } else if (strstart(opt, "device:", &endp))
613 return !bt_device_add(endp);
615 fprintf(stderr, "qemu: bad bluetooth parameter '%s'\n", opt);
616 return 1;
619 /***********************************************************/
620 /* QEMU Block devices */
622 #define HD_OPTS "media=disk"
623 #define CDROM_OPTS "media=cdrom"
624 #define FD_OPTS ""
625 #define PFLASH_OPTS ""
626 #define MTD_OPTS ""
627 #define SD_OPTS ""
629 static int drive_init_func(QemuOpts *opts, void *opaque)
631 int *use_scsi = opaque;
633 return drive_init(opts, *use_scsi) == NULL;
636 static int drive_enable_snapshot(QemuOpts *opts, void *opaque)
638 if (NULL == qemu_opt_get(opts, "snapshot")) {
639 qemu_opt_set(opts, "snapshot", "on");
641 return 0;
644 static void default_drive(int enable, int snapshot, int use_scsi,
645 BlockInterfaceType type, int index,
646 const char *optstr)
648 QemuOpts *opts;
650 if (type == IF_DEFAULT) {
651 type = use_scsi ? IF_SCSI : IF_IDE;
654 if (!enable || drive_get_by_index(type, index)) {
655 return;
658 opts = drive_add(type, index, NULL, optstr);
659 if (snapshot) {
660 drive_enable_snapshot(opts, NULL);
662 if (!drive_init(opts, use_scsi)) {
663 exit(1);
667 void qemu_register_boot_set(QEMUBootSetHandler *func, void *opaque)
669 boot_set_handler = func;
670 boot_set_opaque = opaque;
673 int qemu_boot_set(const char *boot_devices)
675 if (!boot_set_handler) {
676 return -EINVAL;
678 return boot_set_handler(boot_set_opaque, boot_devices);
681 static void validate_bootdevices(char *devices)
683 /* We just do some generic consistency checks */
684 const char *p;
685 int bitmap = 0;
687 for (p = devices; *p != '\0'; p++) {
688 /* Allowed boot devices are:
689 * a-b: floppy disk drives
690 * c-f: IDE disk drives
691 * g-m: machine implementation dependant drives
692 * n-p: network devices
693 * It's up to each machine implementation to check if the given boot
694 * devices match the actual hardware implementation and firmware
695 * features.
697 if (*p < 'a' || *p > 'p') {
698 fprintf(stderr, "Invalid boot device '%c'\n", *p);
699 exit(1);
701 if (bitmap & (1 << (*p - 'a'))) {
702 fprintf(stderr, "Boot device '%c' was given twice\n", *p);
703 exit(1);
705 bitmap |= 1 << (*p - 'a');
709 static void restore_boot_devices(void *opaque)
711 char *standard_boot_devices = opaque;
712 static int first = 1;
714 /* Restore boot order and remove ourselves after the first boot */
715 if (first) {
716 first = 0;
717 return;
720 qemu_boot_set(standard_boot_devices);
722 qemu_unregister_reset(restore_boot_devices, standard_boot_devices);
723 qemu_free(standard_boot_devices);
726 void add_boot_device_path(int32_t bootindex, DeviceState *dev,
727 const char *suffix)
729 FWBootEntry *node, *i;
731 if (bootindex < 0) {
732 return;
735 assert(dev != NULL || suffix != NULL);
737 node = qemu_mallocz(sizeof(FWBootEntry));
738 node->bootindex = bootindex;
739 node->suffix = suffix ? qemu_strdup(suffix) : NULL;
740 node->dev = dev;
742 QTAILQ_FOREACH(i, &fw_boot_order, link) {
743 if (i->bootindex == bootindex) {
744 fprintf(stderr, "Two devices with same boot index %d\n", bootindex);
745 exit(1);
746 } else if (i->bootindex < bootindex) {
747 continue;
749 QTAILQ_INSERT_BEFORE(i, node, link);
750 return;
752 QTAILQ_INSERT_TAIL(&fw_boot_order, node, link);
756 * This function returns null terminated string that consist of new line
757 * separated device pathes.
759 * memory pointed by "size" is assigned total length of the array in bytes
762 char *get_boot_devices_list(uint32_t *size)
764 FWBootEntry *i;
765 uint32_t total = 0;
766 char *list = NULL;
768 QTAILQ_FOREACH(i, &fw_boot_order, link) {
769 char *devpath = NULL, *bootpath;
770 int len;
772 if (i->dev) {
773 devpath = qdev_get_fw_dev_path(i->dev);
774 assert(devpath);
777 if (i->suffix && devpath) {
778 size_t bootpathlen = strlen(devpath) + strlen(i->suffix) + 1;
780 bootpath = qemu_malloc(bootpathlen);
781 snprintf(bootpath, bootpathlen, "%s%s", devpath, i->suffix);
782 qemu_free(devpath);
783 } else if (devpath) {
784 bootpath = devpath;
785 } else {
786 bootpath = qemu_strdup(i->suffix);
787 assert(bootpath);
790 if (total) {
791 list[total-1] = '\n';
793 len = strlen(bootpath) + 1;
794 list = qemu_realloc(list, total + len);
795 memcpy(&list[total], bootpath, len);
796 total += len;
797 qemu_free(bootpath);
800 *size = total;
802 return list;
805 static void numa_add(const char *optarg)
807 char option[128];
808 char *endptr;
809 unsigned long long value, endvalue;
810 int nodenr;
812 optarg = get_opt_name(option, 128, optarg, ',') + 1;
813 if (!strcmp(option, "node")) {
814 if (get_param_value(option, 128, "nodeid", optarg) == 0) {
815 nodenr = nb_numa_nodes;
816 } else {
817 nodenr = strtoull(option, NULL, 10);
820 if (get_param_value(option, 128, "mem", optarg) == 0) {
821 node_mem[nodenr] = 0;
822 } else {
823 int64_t sval;
824 sval = strtosz(option, NULL);
825 if (sval < 0) {
826 fprintf(stderr, "qemu: invalid numa mem size: %s\n", optarg);
827 exit(1);
829 node_mem[nodenr] = sval;
831 if (get_param_value(option, 128, "cpus", optarg) == 0) {
832 node_cpumask[nodenr] = 0;
833 } else {
834 value = strtoull(option, &endptr, 10);
835 if (value >= 64) {
836 value = 63;
837 fprintf(stderr, "only 64 CPUs in NUMA mode supported.\n");
838 } else {
839 if (*endptr == '-') {
840 endvalue = strtoull(endptr+1, &endptr, 10);
841 if (endvalue >= 63) {
842 endvalue = 62;
843 fprintf(stderr,
844 "only 63 CPUs in NUMA mode supported.\n");
846 value = (2ULL << endvalue) - (1ULL << value);
847 } else {
848 value = 1ULL << value;
851 node_cpumask[nodenr] = value;
853 nb_numa_nodes++;
855 return;
858 static void smp_parse(const char *optarg)
860 int smp, sockets = 0, threads = 0, cores = 0;
861 char *endptr;
862 char option[128];
864 smp = strtoul(optarg, &endptr, 10);
865 if (endptr != optarg) {
866 if (*endptr == ',') {
867 endptr++;
870 if (get_param_value(option, 128, "sockets", endptr) != 0)
871 sockets = strtoull(option, NULL, 10);
872 if (get_param_value(option, 128, "cores", endptr) != 0)
873 cores = strtoull(option, NULL, 10);
874 if (get_param_value(option, 128, "threads", endptr) != 0)
875 threads = strtoull(option, NULL, 10);
876 if (get_param_value(option, 128, "maxcpus", endptr) != 0)
877 max_cpus = strtoull(option, NULL, 10);
879 /* compute missing values, prefer sockets over cores over threads */
880 if (smp == 0 || sockets == 0) {
881 sockets = sockets > 0 ? sockets : 1;
882 cores = cores > 0 ? cores : 1;
883 threads = threads > 0 ? threads : 1;
884 if (smp == 0) {
885 smp = cores * threads * sockets;
887 } else {
888 if (cores == 0) {
889 threads = threads > 0 ? threads : 1;
890 cores = smp / (sockets * threads);
891 } else {
892 threads = smp / (cores * sockets);
895 smp_cpus = smp;
896 smp_cores = cores > 0 ? cores : 1;
897 smp_threads = threads > 0 ? threads : 1;
898 if (max_cpus == 0)
899 max_cpus = smp_cpus;
902 /***********************************************************/
903 /* USB devices */
905 static int usb_device_add(const char *devname)
907 const char *p;
908 USBDevice *dev = NULL;
910 if (!usb_enabled)
911 return -1;
913 /* drivers with .usbdevice_name entry in USBDeviceInfo */
914 dev = usbdevice_create(devname);
915 if (dev)
916 goto done;
918 /* the other ones */
919 if (strstart(devname, "host:", &p)) {
920 dev = usb_host_device_open(p);
921 } else if (!strcmp(devname, "bt") || strstart(devname, "bt:", &p)) {
922 dev = usb_bt_init(devname[2] ? hci_init(p) :
923 bt_new_hci(qemu_find_bt_vlan(0)));
924 } else {
925 return -1;
927 if (!dev)
928 return -1;
930 done:
931 return 0;
934 static int usb_device_del(const char *devname)
936 int bus_num, addr;
937 const char *p;
939 if (strstart(devname, "host:", &p))
940 return usb_host_device_close(p);
942 if (!usb_enabled)
943 return -1;
945 p = strchr(devname, '.');
946 if (!p)
947 return -1;
948 bus_num = strtoul(devname, NULL, 0);
949 addr = strtoul(p + 1, NULL, 0);
951 return usb_device_delete_addr(bus_num, addr);
954 static int usb_parse(const char *cmdline)
956 int r;
957 r = usb_device_add(cmdline);
958 if (r < 0) {
959 fprintf(stderr, "qemu: could not add USB device '%s'\n", cmdline);
961 return r;
964 void do_usb_add(Monitor *mon, const QDict *qdict)
966 const char *devname = qdict_get_str(qdict, "devname");
967 if (usb_device_add(devname) < 0) {
968 error_report("could not add USB device '%s'", devname);
972 void do_usb_del(Monitor *mon, const QDict *qdict)
974 const char *devname = qdict_get_str(qdict, "devname");
975 if (usb_device_del(devname) < 0) {
976 error_report("could not delete USB device '%s'", devname);
980 /***********************************************************/
981 /* PCMCIA/Cardbus */
983 static struct pcmcia_socket_entry_s {
984 PCMCIASocket *socket;
985 struct pcmcia_socket_entry_s *next;
986 } *pcmcia_sockets = 0;
988 void pcmcia_socket_register(PCMCIASocket *socket)
990 struct pcmcia_socket_entry_s *entry;
992 entry = qemu_malloc(sizeof(struct pcmcia_socket_entry_s));
993 entry->socket = socket;
994 entry->next = pcmcia_sockets;
995 pcmcia_sockets = entry;
998 void pcmcia_socket_unregister(PCMCIASocket *socket)
1000 struct pcmcia_socket_entry_s *entry, **ptr;
1002 ptr = &pcmcia_sockets;
1003 for (entry = *ptr; entry; ptr = &entry->next, entry = *ptr)
1004 if (entry->socket == socket) {
1005 *ptr = entry->next;
1006 qemu_free(entry);
1010 void pcmcia_info(Monitor *mon)
1012 struct pcmcia_socket_entry_s *iter;
1014 if (!pcmcia_sockets)
1015 monitor_printf(mon, "No PCMCIA sockets\n");
1017 for (iter = pcmcia_sockets; iter; iter = iter->next)
1018 monitor_printf(mon, "%s: %s\n", iter->socket->slot_string,
1019 iter->socket->attached ? iter->socket->card_string :
1020 "Empty");
1023 /***********************************************************/
1024 /* I/O handling */
1026 typedef struct IOHandlerRecord {
1027 int fd;
1028 IOCanReadHandler *fd_read_poll;
1029 IOHandler *fd_read;
1030 IOHandler *fd_write;
1031 int deleted;
1032 void *opaque;
1033 /* temporary data */
1034 struct pollfd *ufd;
1035 QLIST_ENTRY(IOHandlerRecord) next;
1036 } IOHandlerRecord;
1038 static QLIST_HEAD(, IOHandlerRecord) io_handlers =
1039 QLIST_HEAD_INITIALIZER(io_handlers);
1042 /* XXX: fd_read_poll should be suppressed, but an API change is
1043 necessary in the character devices to suppress fd_can_read(). */
1044 int qemu_set_fd_handler2(int fd,
1045 IOCanReadHandler *fd_read_poll,
1046 IOHandler *fd_read,
1047 IOHandler *fd_write,
1048 void *opaque)
1050 IOHandlerRecord *ioh;
1052 if (!fd_read && !fd_write) {
1053 QLIST_FOREACH(ioh, &io_handlers, next) {
1054 if (ioh->fd == fd) {
1055 ioh->deleted = 1;
1056 break;
1059 } else {
1060 QLIST_FOREACH(ioh, &io_handlers, next) {
1061 if (ioh->fd == fd)
1062 goto found;
1064 ioh = qemu_mallocz(sizeof(IOHandlerRecord));
1065 QLIST_INSERT_HEAD(&io_handlers, ioh, next);
1066 found:
1067 ioh->fd = fd;
1068 ioh->fd_read_poll = fd_read_poll;
1069 ioh->fd_read = fd_read;
1070 ioh->fd_write = fd_write;
1071 ioh->opaque = opaque;
1072 ioh->deleted = 0;
1074 return 0;
1077 int qemu_set_fd_handler(int fd,
1078 IOHandler *fd_read,
1079 IOHandler *fd_write,
1080 void *opaque)
1082 return qemu_set_fd_handler2(fd, NULL, fd_read, fd_write, opaque);
1085 /***********************************************************/
1086 /* machine registration */
1088 static QEMUMachine *first_machine = NULL;
1089 QEMUMachine *current_machine = NULL;
1091 int qemu_register_machine(QEMUMachine *m)
1093 QEMUMachine **pm;
1094 pm = &first_machine;
1095 while (*pm != NULL)
1096 pm = &(*pm)->next;
1097 m->next = NULL;
1098 *pm = m;
1099 return 0;
1102 static QEMUMachine *find_machine(const char *name)
1104 QEMUMachine *m;
1106 for(m = first_machine; m != NULL; m = m->next) {
1107 if (!strcmp(m->name, name))
1108 return m;
1109 if (m->alias && !strcmp(m->alias, name))
1110 return m;
1112 return NULL;
1115 static QEMUMachine *find_default_machine(void)
1117 QEMUMachine *m;
1119 for(m = first_machine; m != NULL; m = m->next) {
1120 if (m->is_default) {
1121 return m;
1124 return NULL;
1127 /***********************************************************/
1128 /* main execution loop */
1130 static void gui_update(void *opaque)
1132 uint64_t interval = GUI_REFRESH_INTERVAL;
1133 DisplayState *ds = opaque;
1134 DisplayChangeListener *dcl = ds->listeners;
1136 qemu_flush_coalesced_mmio_buffer();
1137 dpy_refresh(ds);
1139 while (dcl != NULL) {
1140 if (dcl->gui_timer_interval &&
1141 dcl->gui_timer_interval < interval)
1142 interval = dcl->gui_timer_interval;
1143 dcl = dcl->next;
1145 qemu_mod_timer(ds->gui_timer, interval + qemu_get_clock_ms(rt_clock));
1148 static void nographic_update(void *opaque)
1150 uint64_t interval = GUI_REFRESH_INTERVAL;
1152 qemu_flush_coalesced_mmio_buffer();
1153 qemu_mod_timer(nographic_timer, interval + qemu_get_clock_ms(rt_clock));
1156 struct vm_change_state_entry {
1157 VMChangeStateHandler *cb;
1158 void *opaque;
1159 QLIST_ENTRY (vm_change_state_entry) entries;
1162 static QLIST_HEAD(vm_change_state_head, vm_change_state_entry) vm_change_state_head;
1164 VMChangeStateEntry *qemu_add_vm_change_state_handler(VMChangeStateHandler *cb,
1165 void *opaque)
1167 VMChangeStateEntry *e;
1169 e = qemu_mallocz(sizeof (*e));
1171 e->cb = cb;
1172 e->opaque = opaque;
1173 QLIST_INSERT_HEAD(&vm_change_state_head, e, entries);
1174 return e;
1177 void qemu_del_vm_change_state_handler(VMChangeStateEntry *e)
1179 QLIST_REMOVE (e, entries);
1180 qemu_free (e);
1183 void vm_state_notify(int running, int reason)
1185 VMChangeStateEntry *e;
1187 trace_vm_state_notify(running, reason);
1189 for (e = vm_change_state_head.lh_first; e; e = e->entries.le_next) {
1190 e->cb(e->opaque, running, reason);
1194 void vm_start(void)
1196 if (!vm_running) {
1197 cpu_enable_ticks();
1198 vm_running = 1;
1199 vm_state_notify(1, 0);
1200 resume_all_vcpus();
1201 monitor_protocol_event(QEVENT_RESUME, NULL);
1205 /* reset/shutdown handler */
1207 typedef struct QEMUResetEntry {
1208 QTAILQ_ENTRY(QEMUResetEntry) entry;
1209 QEMUResetHandler *func;
1210 void *opaque;
1211 } QEMUResetEntry;
1213 static QTAILQ_HEAD(reset_handlers, QEMUResetEntry) reset_handlers =
1214 QTAILQ_HEAD_INITIALIZER(reset_handlers);
1215 static int reset_requested;
1216 static int shutdown_requested;
1217 static int powerdown_requested;
1218 static int debug_requested;
1219 static int vmstop_requested;
1221 int qemu_shutdown_requested(void)
1223 int r = shutdown_requested;
1224 shutdown_requested = 0;
1225 return r;
1228 int qemu_reset_requested(void)
1230 int r = reset_requested;
1231 reset_requested = 0;
1232 return r;
1235 int qemu_powerdown_requested(void)
1237 int r = powerdown_requested;
1238 powerdown_requested = 0;
1239 return r;
1242 static int qemu_debug_requested(void)
1244 int r = debug_requested;
1245 debug_requested = 0;
1246 return r;
1249 static int qemu_vmstop_requested(void)
1251 int r = vmstop_requested;
1252 vmstop_requested = 0;
1253 return r;
1256 void qemu_register_reset(QEMUResetHandler *func, void *opaque)
1258 QEMUResetEntry *re = qemu_mallocz(sizeof(QEMUResetEntry));
1260 re->func = func;
1261 re->opaque = opaque;
1262 QTAILQ_INSERT_TAIL(&reset_handlers, re, entry);
1265 void qemu_unregister_reset(QEMUResetHandler *func, void *opaque)
1267 QEMUResetEntry *re;
1269 QTAILQ_FOREACH(re, &reset_handlers, entry) {
1270 if (re->func == func && re->opaque == opaque) {
1271 QTAILQ_REMOVE(&reset_handlers, re, entry);
1272 qemu_free(re);
1273 return;
1278 void qemu_system_reset(void)
1280 QEMUResetEntry *re, *nre;
1282 /* reset all devices */
1283 QTAILQ_FOREACH_SAFE(re, &reset_handlers, entry, nre) {
1284 re->func(re->opaque);
1286 monitor_protocol_event(QEVENT_RESET, NULL);
1287 cpu_synchronize_all_post_reset();
1290 void qemu_system_reset_request(void)
1292 if (no_reboot) {
1293 shutdown_requested = 1;
1294 } else {
1295 reset_requested = 1;
1297 cpu_stop_current();
1298 qemu_notify_event();
1301 void qemu_system_shutdown_request(void)
1303 shutdown_requested = 1;
1304 qemu_notify_event();
1307 void qemu_system_powerdown_request(void)
1309 powerdown_requested = 1;
1310 qemu_notify_event();
1313 void qemu_system_debug_request(void)
1315 debug_requested = 1;
1316 qemu_notify_event();
1319 void qemu_system_vmstop_request(int reason)
1321 vmstop_requested = reason;
1322 qemu_notify_event();
1325 void main_loop_wait(int nonblocking)
1327 IOHandlerRecord *ioh;
1328 fd_set rfds, wfds, xfds;
1329 int ret, nfds;
1330 struct timeval tv;
1331 int timeout;
1333 if (nonblocking)
1334 timeout = 0;
1335 else {
1336 timeout = qemu_calculate_timeout();
1337 qemu_bh_update_timeout(&timeout);
1340 os_host_main_loop_wait(&timeout);
1342 /* poll any events */
1343 /* XXX: separate device handlers from system ones */
1344 nfds = -1;
1345 FD_ZERO(&rfds);
1346 FD_ZERO(&wfds);
1347 FD_ZERO(&xfds);
1348 QLIST_FOREACH(ioh, &io_handlers, next) {
1349 if (ioh->deleted)
1350 continue;
1351 if (ioh->fd_read &&
1352 (!ioh->fd_read_poll ||
1353 ioh->fd_read_poll(ioh->opaque) != 0)) {
1354 FD_SET(ioh->fd, &rfds);
1355 if (ioh->fd > nfds)
1356 nfds = ioh->fd;
1358 if (ioh->fd_write) {
1359 FD_SET(ioh->fd, &wfds);
1360 if (ioh->fd > nfds)
1361 nfds = ioh->fd;
1365 tv.tv_sec = timeout / 1000;
1366 tv.tv_usec = (timeout % 1000) * 1000;
1368 slirp_select_fill(&nfds, &rfds, &wfds, &xfds);
1370 qemu_mutex_unlock_iothread();
1371 ret = select(nfds + 1, &rfds, &wfds, &xfds, &tv);
1372 qemu_mutex_lock_iothread();
1373 if (ret > 0) {
1374 IOHandlerRecord *pioh;
1376 QLIST_FOREACH_SAFE(ioh, &io_handlers, next, pioh) {
1377 if (!ioh->deleted && ioh->fd_read && FD_ISSET(ioh->fd, &rfds)) {
1378 ioh->fd_read(ioh->opaque);
1380 if (!ioh->deleted && ioh->fd_write && FD_ISSET(ioh->fd, &wfds)) {
1381 ioh->fd_write(ioh->opaque);
1384 /* Do this last in case read/write handlers marked it for deletion */
1385 if (ioh->deleted) {
1386 QLIST_REMOVE(ioh, next);
1387 qemu_free(ioh);
1392 slirp_select_poll(&rfds, &wfds, &xfds, (ret < 0));
1394 qemu_run_all_timers();
1396 /* Check bottom-halves last in case any of the earlier events triggered
1397 them. */
1398 qemu_bh_poll();
1402 #ifndef CONFIG_IOTHREAD
1403 static int vm_request_pending(void)
1405 return powerdown_requested ||
1406 reset_requested ||
1407 shutdown_requested ||
1408 debug_requested ||
1409 vmstop_requested;
1411 #endif
1413 qemu_irq qemu_system_powerdown;
1415 static void main_loop(void)
1417 bool nonblocking = false;
1418 #ifdef CONFIG_PROFILER
1419 int64_t ti;
1420 #endif
1421 int r;
1423 qemu_main_loop_start();
1425 for (;;) {
1426 #ifndef CONFIG_IOTHREAD
1427 nonblocking = cpu_exec_all();
1428 if (vm_request_pending()) {
1429 nonblocking = true;
1431 #endif
1432 #ifdef CONFIG_PROFILER
1433 ti = profile_getclock();
1434 #endif
1435 main_loop_wait(nonblocking);
1436 #ifdef CONFIG_PROFILER
1437 dev_time += profile_getclock() - ti;
1438 #endif
1440 if (qemu_debug_requested()) {
1441 vm_stop(VMSTOP_DEBUG);
1443 if (qemu_shutdown_requested()) {
1444 monitor_protocol_event(QEVENT_SHUTDOWN, NULL);
1445 if (no_shutdown) {
1446 vm_stop(VMSTOP_SHUTDOWN);
1447 no_shutdown = 0;
1448 } else
1449 break;
1451 if (qemu_reset_requested()) {
1452 pause_all_vcpus();
1453 cpu_synchronize_all_states();
1454 qemu_system_reset();
1455 resume_all_vcpus();
1457 if (qemu_powerdown_requested()) {
1458 monitor_protocol_event(QEVENT_POWERDOWN, NULL);
1459 qemu_irq_raise(qemu_system_powerdown);
1461 if ((r = qemu_vmstop_requested())) {
1462 vm_stop(r);
1465 bdrv_close_all();
1466 pause_all_vcpus();
1469 static void version(void)
1471 printf("QEMU emulator version " QEMU_VERSION QEMU_PKGVERSION ", Copyright (c) 2003-2008 Fabrice Bellard\n");
1474 static void help(int exitcode)
1476 const char *options_help =
1477 #define DEF(option, opt_arg, opt_enum, opt_help, arch_mask) \
1478 opt_help
1479 #define DEFHEADING(text) stringify(text) "\n"
1480 #include "qemu-options.def"
1481 #undef DEF
1482 #undef DEFHEADING
1483 #undef GEN_DOCS
1485 version();
1486 printf("usage: %s [options] [disk_image]\n"
1487 "\n"
1488 "'disk_image' is a raw hard disk image for IDE hard disk 0\n"
1489 "\n"
1490 "%s\n"
1491 "During emulation, the following keys are useful:\n"
1492 "ctrl-alt-f toggle full screen\n"
1493 "ctrl-alt-n switch to virtual console 'n'\n"
1494 "ctrl-alt toggle mouse and keyboard grab\n"
1495 "\n"
1496 "When using -nographic, press 'ctrl-a h' to get some help.\n",
1497 "qemu",
1498 options_help);
1499 exit(exitcode);
1502 #define HAS_ARG 0x0001
1504 typedef struct QEMUOption {
1505 const char *name;
1506 int flags;
1507 int index;
1508 uint32_t arch_mask;
1509 } QEMUOption;
1511 static const QEMUOption qemu_options[] = {
1512 { "h", 0, QEMU_OPTION_h, QEMU_ARCH_ALL },
1513 #define DEF(option, opt_arg, opt_enum, opt_help, arch_mask) \
1514 { option, opt_arg, opt_enum, arch_mask },
1515 #define DEFHEADING(text)
1516 #include "qemu-options.def"
1517 #undef DEF
1518 #undef DEFHEADING
1519 #undef GEN_DOCS
1520 { NULL },
1522 static void select_vgahw (const char *p)
1524 const char *opts;
1526 default_vga = 0;
1527 vga_interface_type = VGA_NONE;
1528 if (strstart(p, "std", &opts)) {
1529 vga_interface_type = VGA_STD;
1530 } else if (strstart(p, "cirrus", &opts)) {
1531 vga_interface_type = VGA_CIRRUS;
1532 } else if (strstart(p, "vmware", &opts)) {
1533 vga_interface_type = VGA_VMWARE;
1534 } else if (strstart(p, "xenfb", &opts)) {
1535 vga_interface_type = VGA_XENFB;
1536 } else if (strstart(p, "qxl", &opts)) {
1537 vga_interface_type = VGA_QXL;
1538 } else if (!strstart(p, "none", &opts)) {
1539 invalid_vga:
1540 fprintf(stderr, "Unknown vga type: %s\n", p);
1541 exit(1);
1543 while (*opts) {
1544 const char *nextopt;
1546 if (strstart(opts, ",retrace=", &nextopt)) {
1547 opts = nextopt;
1548 if (strstart(opts, "dumb", &nextopt))
1549 vga_retrace_method = VGA_RETRACE_DUMB;
1550 else if (strstart(opts, "precise", &nextopt))
1551 vga_retrace_method = VGA_RETRACE_PRECISE;
1552 else goto invalid_vga;
1553 } else goto invalid_vga;
1554 opts = nextopt;
1558 static int balloon_parse(const char *arg)
1560 QemuOpts *opts;
1562 if (strcmp(arg, "none") == 0) {
1563 return 0;
1566 if (!strncmp(arg, "virtio", 6)) {
1567 if (arg[6] == ',') {
1568 /* have params -> parse them */
1569 opts = qemu_opts_parse(qemu_find_opts("device"), arg+7, 0);
1570 if (!opts)
1571 return -1;
1572 } else {
1573 /* create empty opts */
1574 opts = qemu_opts_create(qemu_find_opts("device"), NULL, 0);
1576 qemu_opt_set(opts, "driver", "virtio-balloon-pci");
1577 return 0;
1580 return -1;
1583 char *qemu_find_file(int type, const char *name)
1585 int len;
1586 const char *subdir;
1587 char *buf;
1589 /* If name contains path separators then try it as a straight path. */
1590 if ((strchr(name, '/') || strchr(name, '\\'))
1591 && access(name, R_OK) == 0) {
1592 return qemu_strdup(name);
1594 switch (type) {
1595 case QEMU_FILE_TYPE_BIOS:
1596 subdir = "";
1597 break;
1598 case QEMU_FILE_TYPE_KEYMAP:
1599 subdir = "keymaps/";
1600 break;
1601 default:
1602 abort();
1604 len = strlen(data_dir) + strlen(name) + strlen(subdir) + 2;
1605 buf = qemu_mallocz(len);
1606 snprintf(buf, len, "%s/%s%s", data_dir, subdir, name);
1607 if (access(buf, R_OK)) {
1608 qemu_free(buf);
1609 return NULL;
1611 return buf;
1614 static int device_help_func(QemuOpts *opts, void *opaque)
1616 return qdev_device_help(opts);
1619 static int device_init_func(QemuOpts *opts, void *opaque)
1621 DeviceState *dev;
1623 dev = qdev_device_add(opts);
1624 if (!dev)
1625 return -1;
1626 return 0;
1629 static int chardev_init_func(QemuOpts *opts, void *opaque)
1631 CharDriverState *chr;
1633 chr = qemu_chr_open_opts(opts, NULL);
1634 if (!chr)
1635 return -1;
1636 return 0;
1639 #ifdef CONFIG_VIRTFS
1640 static int fsdev_init_func(QemuOpts *opts, void *opaque)
1642 int ret;
1643 ret = qemu_fsdev_add(opts);
1645 return ret;
1647 #endif
1649 static int mon_init_func(QemuOpts *opts, void *opaque)
1651 CharDriverState *chr;
1652 const char *chardev;
1653 const char *mode;
1654 int flags;
1656 mode = qemu_opt_get(opts, "mode");
1657 if (mode == NULL) {
1658 mode = "readline";
1660 if (strcmp(mode, "readline") == 0) {
1661 flags = MONITOR_USE_READLINE;
1662 } else if (strcmp(mode, "control") == 0) {
1663 flags = MONITOR_USE_CONTROL;
1664 } else {
1665 fprintf(stderr, "unknown monitor mode \"%s\"\n", mode);
1666 exit(1);
1669 if (qemu_opt_get_bool(opts, "pretty", 0))
1670 flags |= MONITOR_USE_PRETTY;
1672 if (qemu_opt_get_bool(opts, "default", 0))
1673 flags |= MONITOR_IS_DEFAULT;
1675 chardev = qemu_opt_get(opts, "chardev");
1676 chr = qemu_chr_find(chardev);
1677 if (chr == NULL) {
1678 fprintf(stderr, "chardev \"%s\" not found\n", chardev);
1679 exit(1);
1682 monitor_init(chr, flags);
1683 return 0;
1686 static void monitor_parse(const char *optarg, const char *mode)
1688 static int monitor_device_index = 0;
1689 QemuOpts *opts;
1690 const char *p;
1691 char label[32];
1692 int def = 0;
1694 if (strstart(optarg, "chardev:", &p)) {
1695 snprintf(label, sizeof(label), "%s", p);
1696 } else {
1697 snprintf(label, sizeof(label), "compat_monitor%d",
1698 monitor_device_index);
1699 if (monitor_device_index == 0) {
1700 def = 1;
1702 opts = qemu_chr_parse_compat(label, optarg);
1703 if (!opts) {
1704 fprintf(stderr, "parse error: %s\n", optarg);
1705 exit(1);
1709 opts = qemu_opts_create(qemu_find_opts("mon"), label, 1);
1710 if (!opts) {
1711 fprintf(stderr, "duplicate chardev: %s\n", label);
1712 exit(1);
1714 qemu_opt_set(opts, "mode", mode);
1715 qemu_opt_set(opts, "chardev", label);
1716 if (def)
1717 qemu_opt_set(opts, "default", "on");
1718 monitor_device_index++;
1721 struct device_config {
1722 enum {
1723 DEV_USB, /* -usbdevice */
1724 DEV_BT, /* -bt */
1725 DEV_SERIAL, /* -serial */
1726 DEV_PARALLEL, /* -parallel */
1727 DEV_VIRTCON, /* -virtioconsole */
1728 DEV_DEBUGCON, /* -debugcon */
1729 } type;
1730 const char *cmdline;
1731 QTAILQ_ENTRY(device_config) next;
1733 QTAILQ_HEAD(, device_config) device_configs = QTAILQ_HEAD_INITIALIZER(device_configs);
1735 static void add_device_config(int type, const char *cmdline)
1737 struct device_config *conf;
1739 conf = qemu_mallocz(sizeof(*conf));
1740 conf->type = type;
1741 conf->cmdline = cmdline;
1742 QTAILQ_INSERT_TAIL(&device_configs, conf, next);
1745 static int foreach_device_config(int type, int (*func)(const char *cmdline))
1747 struct device_config *conf;
1748 int rc;
1750 QTAILQ_FOREACH(conf, &device_configs, next) {
1751 if (conf->type != type)
1752 continue;
1753 rc = func(conf->cmdline);
1754 if (0 != rc)
1755 return rc;
1757 return 0;
1760 static int serial_parse(const char *devname)
1762 static int index = 0;
1763 char label[32];
1765 if (strcmp(devname, "none") == 0)
1766 return 0;
1767 if (index == MAX_SERIAL_PORTS) {
1768 fprintf(stderr, "qemu: too many serial ports\n");
1769 exit(1);
1771 snprintf(label, sizeof(label), "serial%d", index);
1772 serial_hds[index] = qemu_chr_open(label, devname, NULL);
1773 if (!serial_hds[index]) {
1774 fprintf(stderr, "qemu: could not open serial device '%s': %s\n",
1775 devname, strerror(errno));
1776 return -1;
1778 index++;
1779 return 0;
1782 static int parallel_parse(const char *devname)
1784 static int index = 0;
1785 char label[32];
1787 if (strcmp(devname, "none") == 0)
1788 return 0;
1789 if (index == MAX_PARALLEL_PORTS) {
1790 fprintf(stderr, "qemu: too many parallel ports\n");
1791 exit(1);
1793 snprintf(label, sizeof(label), "parallel%d", index);
1794 parallel_hds[index] = qemu_chr_open(label, devname, NULL);
1795 if (!parallel_hds[index]) {
1796 fprintf(stderr, "qemu: could not open parallel device '%s': %s\n",
1797 devname, strerror(errno));
1798 return -1;
1800 index++;
1801 return 0;
1804 static int virtcon_parse(const char *devname)
1806 QemuOptsList *device = qemu_find_opts("device");
1807 static int index = 0;
1808 char label[32];
1809 QemuOpts *bus_opts, *dev_opts;
1811 if (strcmp(devname, "none") == 0)
1812 return 0;
1813 if (index == MAX_VIRTIO_CONSOLES) {
1814 fprintf(stderr, "qemu: too many virtio consoles\n");
1815 exit(1);
1818 bus_opts = qemu_opts_create(device, NULL, 0);
1819 qemu_opt_set(bus_opts, "driver", "virtio-serial");
1821 dev_opts = qemu_opts_create(device, NULL, 0);
1822 qemu_opt_set(dev_opts, "driver", "virtconsole");
1824 snprintf(label, sizeof(label), "virtcon%d", index);
1825 virtcon_hds[index] = qemu_chr_open(label, devname, NULL);
1826 if (!virtcon_hds[index]) {
1827 fprintf(stderr, "qemu: could not open virtio console '%s': %s\n",
1828 devname, strerror(errno));
1829 return -1;
1831 qemu_opt_set(dev_opts, "chardev", label);
1833 index++;
1834 return 0;
1837 static int debugcon_parse(const char *devname)
1839 QemuOpts *opts;
1841 if (!qemu_chr_open("debugcon", devname, NULL)) {
1842 exit(1);
1844 opts = qemu_opts_create(qemu_find_opts("device"), "debugcon", 1);
1845 if (!opts) {
1846 fprintf(stderr, "qemu: already have a debugcon device\n");
1847 exit(1);
1849 qemu_opt_set(opts, "driver", "isa-debugcon");
1850 qemu_opt_set(opts, "chardev", "debugcon");
1851 return 0;
1854 void qemu_add_exit_notifier(Notifier *notify)
1856 notifier_list_add(&exit_notifiers, notify);
1859 void qemu_remove_exit_notifier(Notifier *notify)
1861 notifier_list_remove(&exit_notifiers, notify);
1864 static void qemu_run_exit_notifiers(void)
1866 notifier_list_notify(&exit_notifiers);
1869 void qemu_add_machine_init_done_notifier(Notifier *notify)
1871 notifier_list_add(&machine_init_done_notifiers, notify);
1874 static void qemu_run_machine_init_done_notifiers(void)
1876 notifier_list_notify(&machine_init_done_notifiers);
1879 static const QEMUOption *lookup_opt(int argc, char **argv,
1880 const char **poptarg, int *poptind)
1882 const QEMUOption *popt;
1883 int optind = *poptind;
1884 char *r = argv[optind];
1885 const char *optarg;
1887 loc_set_cmdline(argv, optind, 1);
1888 optind++;
1889 /* Treat --foo the same as -foo. */
1890 if (r[1] == '-')
1891 r++;
1892 popt = qemu_options;
1893 for(;;) {
1894 if (!popt->name) {
1895 error_report("invalid option");
1896 exit(1);
1898 if (!strcmp(popt->name, r + 1))
1899 break;
1900 popt++;
1902 if (popt->flags & HAS_ARG) {
1903 if (optind >= argc) {
1904 error_report("requires an argument");
1905 exit(1);
1907 optarg = argv[optind++];
1908 loc_set_cmdline(argv, optind - 2, 2);
1909 } else {
1910 optarg = NULL;
1913 *poptarg = optarg;
1914 *poptind = optind;
1916 return popt;
1919 int main(int argc, char **argv, char **envp)
1921 const char *gdbstub_dev = NULL;
1922 int i;
1923 int snapshot, linux_boot;
1924 const char *icount_option = NULL;
1925 const char *initrd_filename;
1926 const char *kernel_filename, *kernel_cmdline;
1927 char boot_devices[33] = "cad"; /* default to HD->floppy->CD-ROM */
1928 DisplayState *ds;
1929 DisplayChangeListener *dcl;
1930 int cyls, heads, secs, translation;
1931 QemuOpts *hda_opts = NULL, *opts;
1932 QemuOptsList *olist;
1933 int optind;
1934 const char *optarg;
1935 const char *loadvm = NULL;
1936 QEMUMachine *machine;
1937 const char *cpu_model;
1938 int tb_size;
1939 const char *pid_file = NULL;
1940 const char *incoming = NULL;
1941 int show_vnc_port = 0;
1942 int defconfig = 1;
1943 const char *trace_file = NULL;
1945 atexit(qemu_run_exit_notifiers);
1946 error_set_progname(argv[0]);
1948 init_clocks();
1950 qemu_cache_utils_init(envp);
1952 QLIST_INIT (&vm_change_state_head);
1953 os_setup_early_signal_handling();
1955 module_call_init(MODULE_INIT_MACHINE);
1956 machine = find_default_machine();
1957 cpu_model = NULL;
1958 initrd_filename = NULL;
1959 ram_size = 0;
1960 snapshot = 0;
1961 kernel_filename = NULL;
1962 kernel_cmdline = "";
1963 cyls = heads = secs = 0;
1964 translation = BIOS_ATA_TRANSLATION_AUTO;
1966 for (i = 0; i < MAX_NODES; i++) {
1967 node_mem[i] = 0;
1968 node_cpumask[i] = 0;
1971 nb_numa_nodes = 0;
1972 nb_nics = 0;
1974 tb_size = 0;
1975 autostart= 1;
1977 /* first pass of option parsing */
1978 optind = 1;
1979 while (optind < argc) {
1980 if (argv[optind][0] != '-') {
1981 /* disk image */
1982 optind++;
1983 continue;
1984 } else {
1985 const QEMUOption *popt;
1987 popt = lookup_opt(argc, argv, &optarg, &optind);
1988 switch (popt->index) {
1989 case QEMU_OPTION_nodefconfig:
1990 defconfig=0;
1991 break;
1996 if (defconfig) {
1997 int ret;
1999 ret = qemu_read_config_file(CONFIG_QEMU_CONFDIR "/qemu.conf");
2000 if (ret < 0 && ret != -ENOENT) {
2001 exit(1);
2004 ret = qemu_read_config_file(arch_config_name);
2005 if (ret < 0 && ret != -ENOENT) {
2006 exit(1);
2009 cpudef_init();
2011 /* second pass of option parsing */
2012 optind = 1;
2013 for(;;) {
2014 if (optind >= argc)
2015 break;
2016 if (argv[optind][0] != '-') {
2017 hda_opts = drive_add(IF_DEFAULT, 0, argv[optind++], HD_OPTS);
2018 } else {
2019 const QEMUOption *popt;
2021 popt = lookup_opt(argc, argv, &optarg, &optind);
2022 if (!(popt->arch_mask & arch_type)) {
2023 printf("Option %s not supported for this target\n", popt->name);
2024 exit(1);
2026 switch(popt->index) {
2027 case QEMU_OPTION_M:
2028 machine = find_machine(optarg);
2029 if (!machine) {
2030 QEMUMachine *m;
2031 printf("Supported machines are:\n");
2032 for(m = first_machine; m != NULL; m = m->next) {
2033 if (m->alias)
2034 printf("%-10s %s (alias of %s)\n",
2035 m->alias, m->desc, m->name);
2036 printf("%-10s %s%s\n",
2037 m->name, m->desc,
2038 m->is_default ? " (default)" : "");
2040 exit(*optarg != '?');
2042 break;
2043 case QEMU_OPTION_cpu:
2044 /* hw initialization will check this */
2045 if (*optarg == '?') {
2046 list_cpus(stdout, &fprintf, optarg);
2047 exit(0);
2048 } else {
2049 cpu_model = optarg;
2051 break;
2052 case QEMU_OPTION_initrd:
2053 initrd_filename = optarg;
2054 break;
2055 case QEMU_OPTION_hda:
2057 char buf[256];
2058 if (cyls == 0)
2059 snprintf(buf, sizeof(buf), "%s", HD_OPTS);
2060 else
2061 snprintf(buf, sizeof(buf),
2062 "%s,cyls=%d,heads=%d,secs=%d%s",
2063 HD_OPTS , cyls, heads, secs,
2064 translation == BIOS_ATA_TRANSLATION_LBA ?
2065 ",trans=lba" :
2066 translation == BIOS_ATA_TRANSLATION_NONE ?
2067 ",trans=none" : "");
2068 drive_add(IF_DEFAULT, 0, optarg, buf);
2069 break;
2071 case QEMU_OPTION_hdb:
2072 case QEMU_OPTION_hdc:
2073 case QEMU_OPTION_hdd:
2074 drive_add(IF_DEFAULT, popt->index - QEMU_OPTION_hda, optarg,
2075 HD_OPTS);
2076 break;
2077 case QEMU_OPTION_drive:
2078 drive_def(optarg);
2079 break;
2080 case QEMU_OPTION_set:
2081 if (qemu_set_option(optarg) != 0)
2082 exit(1);
2083 break;
2084 case QEMU_OPTION_global:
2085 if (qemu_global_option(optarg) != 0)
2086 exit(1);
2087 break;
2088 case QEMU_OPTION_mtdblock:
2089 drive_add(IF_MTD, -1, optarg, MTD_OPTS);
2090 break;
2091 case QEMU_OPTION_sd:
2092 drive_add(IF_SD, 0, optarg, SD_OPTS);
2093 break;
2094 case QEMU_OPTION_pflash:
2095 drive_add(IF_PFLASH, -1, optarg, PFLASH_OPTS);
2096 break;
2097 case QEMU_OPTION_snapshot:
2098 snapshot = 1;
2099 break;
2100 case QEMU_OPTION_hdachs:
2102 const char *p;
2103 p = optarg;
2104 cyls = strtol(p, (char **)&p, 0);
2105 if (cyls < 1 || cyls > 16383)
2106 goto chs_fail;
2107 if (*p != ',')
2108 goto chs_fail;
2109 p++;
2110 heads = strtol(p, (char **)&p, 0);
2111 if (heads < 1 || heads > 16)
2112 goto chs_fail;
2113 if (*p != ',')
2114 goto chs_fail;
2115 p++;
2116 secs = strtol(p, (char **)&p, 0);
2117 if (secs < 1 || secs > 63)
2118 goto chs_fail;
2119 if (*p == ',') {
2120 p++;
2121 if (!strcmp(p, "none"))
2122 translation = BIOS_ATA_TRANSLATION_NONE;
2123 else if (!strcmp(p, "lba"))
2124 translation = BIOS_ATA_TRANSLATION_LBA;
2125 else if (!strcmp(p, "auto"))
2126 translation = BIOS_ATA_TRANSLATION_AUTO;
2127 else
2128 goto chs_fail;
2129 } else if (*p != '\0') {
2130 chs_fail:
2131 fprintf(stderr, "qemu: invalid physical CHS format\n");
2132 exit(1);
2134 if (hda_opts != NULL) {
2135 char num[16];
2136 snprintf(num, sizeof(num), "%d", cyls);
2137 qemu_opt_set(hda_opts, "cyls", num);
2138 snprintf(num, sizeof(num), "%d", heads);
2139 qemu_opt_set(hda_opts, "heads", num);
2140 snprintf(num, sizeof(num), "%d", secs);
2141 qemu_opt_set(hda_opts, "secs", num);
2142 if (translation == BIOS_ATA_TRANSLATION_LBA)
2143 qemu_opt_set(hda_opts, "trans", "lba");
2144 if (translation == BIOS_ATA_TRANSLATION_NONE)
2145 qemu_opt_set(hda_opts, "trans", "none");
2148 break;
2149 case QEMU_OPTION_numa:
2150 if (nb_numa_nodes >= MAX_NODES) {
2151 fprintf(stderr, "qemu: too many NUMA nodes\n");
2152 exit(1);
2154 numa_add(optarg);
2155 break;
2156 case QEMU_OPTION_nographic:
2157 display_type = DT_NOGRAPHIC;
2158 break;
2159 #ifdef CONFIG_CURSES
2160 case QEMU_OPTION_curses:
2161 display_type = DT_CURSES;
2162 break;
2163 #endif
2164 case QEMU_OPTION_portrait:
2165 graphic_rotate = 1;
2166 break;
2167 case QEMU_OPTION_kernel:
2168 kernel_filename = optarg;
2169 break;
2170 case QEMU_OPTION_append:
2171 kernel_cmdline = optarg;
2172 break;
2173 case QEMU_OPTION_cdrom:
2174 drive_add(IF_DEFAULT, 2, optarg, CDROM_OPTS);
2175 break;
2176 case QEMU_OPTION_boot:
2178 static const char * const params[] = {
2179 "order", "once", "menu", NULL
2181 char buf[sizeof(boot_devices)];
2182 char *standard_boot_devices;
2183 int legacy = 0;
2185 if (!strchr(optarg, '=')) {
2186 legacy = 1;
2187 pstrcpy(buf, sizeof(buf), optarg);
2188 } else if (check_params(buf, sizeof(buf), params, optarg) < 0) {
2189 fprintf(stderr,
2190 "qemu: unknown boot parameter '%s' in '%s'\n",
2191 buf, optarg);
2192 exit(1);
2195 if (legacy ||
2196 get_param_value(buf, sizeof(buf), "order", optarg)) {
2197 validate_bootdevices(buf);
2198 pstrcpy(boot_devices, sizeof(boot_devices), buf);
2200 if (!legacy) {
2201 if (get_param_value(buf, sizeof(buf),
2202 "once", optarg)) {
2203 validate_bootdevices(buf);
2204 standard_boot_devices = qemu_strdup(boot_devices);
2205 pstrcpy(boot_devices, sizeof(boot_devices), buf);
2206 qemu_register_reset(restore_boot_devices,
2207 standard_boot_devices);
2209 if (get_param_value(buf, sizeof(buf),
2210 "menu", optarg)) {
2211 if (!strcmp(buf, "on")) {
2212 boot_menu = 1;
2213 } else if (!strcmp(buf, "off")) {
2214 boot_menu = 0;
2215 } else {
2216 fprintf(stderr,
2217 "qemu: invalid option value '%s'\n",
2218 buf);
2219 exit(1);
2224 break;
2225 case QEMU_OPTION_fda:
2226 case QEMU_OPTION_fdb:
2227 drive_add(IF_FLOPPY, popt->index - QEMU_OPTION_fda,
2228 optarg, FD_OPTS);
2229 break;
2230 case QEMU_OPTION_no_fd_bootchk:
2231 fd_bootchk = 0;
2232 break;
2233 case QEMU_OPTION_netdev:
2234 if (net_client_parse(qemu_find_opts("netdev"), optarg) == -1) {
2235 exit(1);
2237 break;
2238 case QEMU_OPTION_net:
2239 if (net_client_parse(qemu_find_opts("net"), optarg) == -1) {
2240 exit(1);
2242 break;
2243 #ifdef CONFIG_SLIRP
2244 case QEMU_OPTION_tftp:
2245 legacy_tftp_prefix = optarg;
2246 break;
2247 case QEMU_OPTION_bootp:
2248 legacy_bootp_filename = optarg;
2249 break;
2250 case QEMU_OPTION_redir:
2251 if (net_slirp_redir(optarg) < 0)
2252 exit(1);
2253 break;
2254 #endif
2255 case QEMU_OPTION_bt:
2256 add_device_config(DEV_BT, optarg);
2257 break;
2258 case QEMU_OPTION_audio_help:
2259 if (!(audio_available())) {
2260 printf("Option %s not supported for this target\n", popt->name);
2261 exit(1);
2263 AUD_help ();
2264 exit (0);
2265 break;
2266 case QEMU_OPTION_soundhw:
2267 if (!(audio_available())) {
2268 printf("Option %s not supported for this target\n", popt->name);
2269 exit(1);
2271 select_soundhw (optarg);
2272 break;
2273 case QEMU_OPTION_h:
2274 help(0);
2275 break;
2276 case QEMU_OPTION_version:
2277 version();
2278 exit(0);
2279 break;
2280 case QEMU_OPTION_m: {
2281 int64_t value;
2283 value = strtosz(optarg, NULL);
2284 if (value < 0) {
2285 fprintf(stderr, "qemu: invalid ram size: %s\n", optarg);
2286 exit(1);
2289 /* On 32-bit hosts, QEMU is limited by virtual address space */
2290 if (value > (2047 << 20) && HOST_LONG_BITS == 32) {
2291 fprintf(stderr, "qemu: at most 2047 MB RAM can be simulated\n");
2292 exit(1);
2294 if (value != (uint64_t)(ram_addr_t)value) {
2295 fprintf(stderr, "qemu: ram size too large\n");
2296 exit(1);
2298 ram_size = value;
2299 break;
2301 case QEMU_OPTION_mempath:
2302 mem_path = optarg;
2303 break;
2304 #ifdef MAP_POPULATE
2305 case QEMU_OPTION_mem_prealloc:
2306 mem_prealloc = 1;
2307 break;
2308 #endif
2309 case QEMU_OPTION_d:
2310 set_cpu_log(optarg);
2311 break;
2312 case QEMU_OPTION_s:
2313 gdbstub_dev = "tcp::" DEFAULT_GDBSTUB_PORT;
2314 break;
2315 case QEMU_OPTION_gdb:
2316 gdbstub_dev = optarg;
2317 break;
2318 case QEMU_OPTION_L:
2319 data_dir = optarg;
2320 break;
2321 case QEMU_OPTION_bios:
2322 bios_name = optarg;
2323 break;
2324 case QEMU_OPTION_singlestep:
2325 singlestep = 1;
2326 break;
2327 case QEMU_OPTION_S:
2328 autostart = 0;
2329 break;
2330 case QEMU_OPTION_k:
2331 keyboard_layout = optarg;
2332 break;
2333 case QEMU_OPTION_localtime:
2334 rtc_utc = 0;
2335 break;
2336 case QEMU_OPTION_vga:
2337 select_vgahw (optarg);
2338 break;
2339 case QEMU_OPTION_g:
2341 const char *p;
2342 int w, h, depth;
2343 p = optarg;
2344 w = strtol(p, (char **)&p, 10);
2345 if (w <= 0) {
2346 graphic_error:
2347 fprintf(stderr, "qemu: invalid resolution or depth\n");
2348 exit(1);
2350 if (*p != 'x')
2351 goto graphic_error;
2352 p++;
2353 h = strtol(p, (char **)&p, 10);
2354 if (h <= 0)
2355 goto graphic_error;
2356 if (*p == 'x') {
2357 p++;
2358 depth = strtol(p, (char **)&p, 10);
2359 if (depth != 8 && depth != 15 && depth != 16 &&
2360 depth != 24 && depth != 32)
2361 goto graphic_error;
2362 } else if (*p == '\0') {
2363 depth = graphic_depth;
2364 } else {
2365 goto graphic_error;
2368 graphic_width = w;
2369 graphic_height = h;
2370 graphic_depth = depth;
2372 break;
2373 case QEMU_OPTION_echr:
2375 char *r;
2376 term_escape_char = strtol(optarg, &r, 0);
2377 if (r == optarg)
2378 printf("Bad argument to echr\n");
2379 break;
2381 case QEMU_OPTION_monitor:
2382 monitor_parse(optarg, "readline");
2383 default_monitor = 0;
2384 break;
2385 case QEMU_OPTION_qmp:
2386 monitor_parse(optarg, "control");
2387 default_monitor = 0;
2388 break;
2389 case QEMU_OPTION_mon:
2390 opts = qemu_opts_parse(qemu_find_opts("mon"), optarg, 1);
2391 if (!opts) {
2392 exit(1);
2394 default_monitor = 0;
2395 break;
2396 case QEMU_OPTION_chardev:
2397 opts = qemu_opts_parse(qemu_find_opts("chardev"), optarg, 1);
2398 if (!opts) {
2399 exit(1);
2401 break;
2402 case QEMU_OPTION_fsdev:
2403 olist = qemu_find_opts("fsdev");
2404 if (!olist) {
2405 fprintf(stderr, "fsdev is not supported by this qemu build.\n");
2406 exit(1);
2408 opts = qemu_opts_parse(olist, optarg, 1);
2409 if (!opts) {
2410 fprintf(stderr, "parse error: %s\n", optarg);
2411 exit(1);
2413 break;
2414 case QEMU_OPTION_virtfs: {
2415 char *arg_fsdev = NULL;
2416 char *arg_9p = NULL;
2417 int len = 0;
2419 olist = qemu_find_opts("virtfs");
2420 if (!olist) {
2421 fprintf(stderr, "virtfs is not supported by this qemu build.\n");
2422 exit(1);
2424 opts = qemu_opts_parse(olist, optarg, 1);
2425 if (!opts) {
2426 fprintf(stderr, "parse error: %s\n", optarg);
2427 exit(1);
2430 if (qemu_opt_get(opts, "fstype") == NULL ||
2431 qemu_opt_get(opts, "mount_tag") == NULL ||
2432 qemu_opt_get(opts, "path") == NULL ||
2433 qemu_opt_get(opts, "security_model") == NULL) {
2434 fprintf(stderr, "Usage: -virtfs fstype,path=/share_path/,"
2435 "security_model=[mapped|passthrough|none],"
2436 "mnt_tag=tag.\n");
2437 exit(1);
2440 len = strlen(",id=,path=,security_model=");
2441 len += strlen(qemu_opt_get(opts, "fstype"));
2442 len += strlen(qemu_opt_get(opts, "mount_tag"));
2443 len += strlen(qemu_opt_get(opts, "path"));
2444 len += strlen(qemu_opt_get(opts, "security_model"));
2445 arg_fsdev = qemu_malloc((len + 1) * sizeof(*arg_fsdev));
2447 snprintf(arg_fsdev, (len + 1) * sizeof(*arg_fsdev),
2448 "%s,id=%s,path=%s,security_model=%s",
2449 qemu_opt_get(opts, "fstype"),
2450 qemu_opt_get(opts, "mount_tag"),
2451 qemu_opt_get(opts, "path"),
2452 qemu_opt_get(opts, "security_model"));
2454 len = strlen("virtio-9p-pci,fsdev=,mount_tag=");
2455 len += 2*strlen(qemu_opt_get(opts, "mount_tag"));
2456 arg_9p = qemu_malloc((len + 1) * sizeof(*arg_9p));
2458 snprintf(arg_9p, (len + 1) * sizeof(*arg_9p),
2459 "virtio-9p-pci,fsdev=%s,mount_tag=%s",
2460 qemu_opt_get(opts, "mount_tag"),
2461 qemu_opt_get(opts, "mount_tag"));
2463 if (!qemu_opts_parse(qemu_find_opts("fsdev"), arg_fsdev, 1)) {
2464 fprintf(stderr, "parse error [fsdev]: %s\n", optarg);
2465 exit(1);
2468 if (!qemu_opts_parse(qemu_find_opts("device"), arg_9p, 1)) {
2469 fprintf(stderr, "parse error [device]: %s\n", optarg);
2470 exit(1);
2473 qemu_free(arg_fsdev);
2474 qemu_free(arg_9p);
2475 break;
2477 case QEMU_OPTION_serial:
2478 add_device_config(DEV_SERIAL, optarg);
2479 default_serial = 0;
2480 if (strncmp(optarg, "mon:", 4) == 0) {
2481 default_monitor = 0;
2483 break;
2484 case QEMU_OPTION_watchdog:
2485 if (watchdog) {
2486 fprintf(stderr,
2487 "qemu: only one watchdog option may be given\n");
2488 return 1;
2490 watchdog = optarg;
2491 break;
2492 case QEMU_OPTION_watchdog_action:
2493 if (select_watchdog_action(optarg) == -1) {
2494 fprintf(stderr, "Unknown -watchdog-action parameter\n");
2495 exit(1);
2497 break;
2498 case QEMU_OPTION_virtiocon:
2499 add_device_config(DEV_VIRTCON, optarg);
2500 default_virtcon = 0;
2501 if (strncmp(optarg, "mon:", 4) == 0) {
2502 default_monitor = 0;
2504 break;
2505 case QEMU_OPTION_parallel:
2506 add_device_config(DEV_PARALLEL, optarg);
2507 default_parallel = 0;
2508 if (strncmp(optarg, "mon:", 4) == 0) {
2509 default_monitor = 0;
2511 break;
2512 case QEMU_OPTION_debugcon:
2513 add_device_config(DEV_DEBUGCON, optarg);
2514 break;
2515 case QEMU_OPTION_loadvm:
2516 loadvm = optarg;
2517 break;
2518 case QEMU_OPTION_full_screen:
2519 full_screen = 1;
2520 break;
2521 #ifdef CONFIG_SDL
2522 case QEMU_OPTION_no_frame:
2523 no_frame = 1;
2524 break;
2525 case QEMU_OPTION_alt_grab:
2526 alt_grab = 1;
2527 break;
2528 case QEMU_OPTION_ctrl_grab:
2529 ctrl_grab = 1;
2530 break;
2531 case QEMU_OPTION_no_quit:
2532 no_quit = 1;
2533 break;
2534 case QEMU_OPTION_sdl:
2535 display_type = DT_SDL;
2536 break;
2537 #endif
2538 case QEMU_OPTION_pidfile:
2539 pid_file = optarg;
2540 break;
2541 case QEMU_OPTION_win2k_hack:
2542 win2k_install_hack = 1;
2543 break;
2544 case QEMU_OPTION_rtc_td_hack:
2545 rtc_td_hack = 1;
2546 break;
2547 case QEMU_OPTION_acpitable:
2548 do_acpitable_option(optarg);
2549 break;
2550 case QEMU_OPTION_smbios:
2551 do_smbios_option(optarg);
2552 break;
2553 case QEMU_OPTION_enable_kvm:
2554 kvm_allowed = 1;
2555 break;
2556 case QEMU_OPTION_usb:
2557 usb_enabled = 1;
2558 break;
2559 case QEMU_OPTION_usbdevice:
2560 usb_enabled = 1;
2561 add_device_config(DEV_USB, optarg);
2562 break;
2563 case QEMU_OPTION_device:
2564 if (!qemu_opts_parse(qemu_find_opts("device"), optarg, 1)) {
2565 exit(1);
2567 break;
2568 case QEMU_OPTION_smp:
2569 smp_parse(optarg);
2570 if (smp_cpus < 1) {
2571 fprintf(stderr, "Invalid number of CPUs\n");
2572 exit(1);
2574 if (max_cpus < smp_cpus) {
2575 fprintf(stderr, "maxcpus must be equal to or greater than "
2576 "smp\n");
2577 exit(1);
2579 if (max_cpus > 255) {
2580 fprintf(stderr, "Unsupported number of maxcpus\n");
2581 exit(1);
2583 break;
2584 case QEMU_OPTION_vnc:
2585 display_remote++;
2586 vnc_display = optarg;
2587 break;
2588 case QEMU_OPTION_no_acpi:
2589 acpi_enabled = 0;
2590 break;
2591 case QEMU_OPTION_no_hpet:
2592 no_hpet = 1;
2593 break;
2594 case QEMU_OPTION_balloon:
2595 if (balloon_parse(optarg) < 0) {
2596 fprintf(stderr, "Unknown -balloon argument %s\n", optarg);
2597 exit(1);
2599 break;
2600 case QEMU_OPTION_no_reboot:
2601 no_reboot = 1;
2602 break;
2603 case QEMU_OPTION_no_shutdown:
2604 no_shutdown = 1;
2605 break;
2606 case QEMU_OPTION_show_cursor:
2607 cursor_hide = 0;
2608 break;
2609 case QEMU_OPTION_uuid:
2610 if(qemu_uuid_parse(optarg, qemu_uuid) < 0) {
2611 fprintf(stderr, "Fail to parse UUID string."
2612 " Wrong format.\n");
2613 exit(1);
2615 break;
2616 case QEMU_OPTION_option_rom:
2617 if (nb_option_roms >= MAX_OPTION_ROMS) {
2618 fprintf(stderr, "Too many option ROMs\n");
2619 exit(1);
2621 opts = qemu_opts_parse(qemu_find_opts("option-rom"), optarg, 1);
2622 option_rom[nb_option_roms].name = qemu_opt_get(opts, "romfile");
2623 option_rom[nb_option_roms].bootindex =
2624 qemu_opt_get_number(opts, "bootindex", -1);
2625 if (!option_rom[nb_option_roms].name) {
2626 fprintf(stderr, "Option ROM file is not specified\n");
2627 exit(1);
2629 nb_option_roms++;
2630 break;
2631 case QEMU_OPTION_semihosting:
2632 semihosting_enabled = 1;
2633 break;
2634 case QEMU_OPTION_name:
2635 qemu_name = qemu_strdup(optarg);
2637 char *p = strchr(qemu_name, ',');
2638 if (p != NULL) {
2639 *p++ = 0;
2640 if (strncmp(p, "process=", 8)) {
2641 fprintf(stderr, "Unknown subargument %s to -name\n", p);
2642 exit(1);
2644 p += 8;
2645 os_set_proc_name(p);
2648 break;
2649 case QEMU_OPTION_prom_env:
2650 if (nb_prom_envs >= MAX_PROM_ENVS) {
2651 fprintf(stderr, "Too many prom variables\n");
2652 exit(1);
2654 prom_envs[nb_prom_envs] = optarg;
2655 nb_prom_envs++;
2656 break;
2657 case QEMU_OPTION_old_param:
2658 old_param = 1;
2659 break;
2660 case QEMU_OPTION_clock:
2661 configure_alarms(optarg);
2662 break;
2663 case QEMU_OPTION_startdate:
2664 configure_rtc_date_offset(optarg, 1);
2665 break;
2666 case QEMU_OPTION_rtc:
2667 opts = qemu_opts_parse(qemu_find_opts("rtc"), optarg, 0);
2668 if (!opts) {
2669 exit(1);
2671 configure_rtc(opts);
2672 break;
2673 case QEMU_OPTION_tb_size:
2674 tb_size = strtol(optarg, NULL, 0);
2675 if (tb_size < 0)
2676 tb_size = 0;
2677 break;
2678 case QEMU_OPTION_icount:
2679 icount_option = optarg;
2680 break;
2681 case QEMU_OPTION_incoming:
2682 incoming = optarg;
2683 incoming_expected = true;
2684 break;
2685 case QEMU_OPTION_nodefaults:
2686 default_serial = 0;
2687 default_parallel = 0;
2688 default_virtcon = 0;
2689 default_monitor = 0;
2690 default_vga = 0;
2691 default_net = 0;
2692 default_floppy = 0;
2693 default_cdrom = 0;
2694 default_sdcard = 0;
2695 break;
2696 case QEMU_OPTION_xen_domid:
2697 if (!(xen_available())) {
2698 printf("Option %s not supported for this target\n", popt->name);
2699 exit(1);
2701 xen_domid = atoi(optarg);
2702 break;
2703 case QEMU_OPTION_xen_create:
2704 if (!(xen_available())) {
2705 printf("Option %s not supported for this target\n", popt->name);
2706 exit(1);
2708 xen_mode = XEN_CREATE;
2709 break;
2710 case QEMU_OPTION_xen_attach:
2711 if (!(xen_available())) {
2712 printf("Option %s not supported for this target\n", popt->name);
2713 exit(1);
2715 xen_mode = XEN_ATTACH;
2716 break;
2717 #ifdef CONFIG_SIMPLE_TRACE
2718 case QEMU_OPTION_trace:
2719 opts = qemu_opts_parse(qemu_find_opts("trace"), optarg, 0);
2720 if (opts) {
2721 trace_file = qemu_opt_get(opts, "file");
2723 break;
2724 #endif
2725 case QEMU_OPTION_readconfig:
2727 int ret = qemu_read_config_file(optarg);
2728 if (ret < 0) {
2729 fprintf(stderr, "read config %s: %s\n", optarg,
2730 strerror(-ret));
2731 exit(1);
2733 break;
2735 case QEMU_OPTION_spice:
2736 olist = qemu_find_opts("spice");
2737 if (!olist) {
2738 fprintf(stderr, "spice is not supported by this qemu build.\n");
2739 exit(1);
2741 opts = qemu_opts_parse(olist, optarg, 0);
2742 if (!opts) {
2743 fprintf(stderr, "parse error: %s\n", optarg);
2744 exit(1);
2746 break;
2747 case QEMU_OPTION_writeconfig:
2749 FILE *fp;
2750 if (strcmp(optarg, "-") == 0) {
2751 fp = stdout;
2752 } else {
2753 fp = fopen(optarg, "w");
2754 if (fp == NULL) {
2755 fprintf(stderr, "open %s: %s\n", optarg, strerror(errno));
2756 exit(1);
2759 qemu_config_write(fp);
2760 fclose(fp);
2761 break;
2763 default:
2764 os_parse_cmd_args(popt->index, optarg);
2768 loc_set_none();
2770 if (!st_init(trace_file)) {
2771 fprintf(stderr, "warning: unable to initialize simple trace backend\n");
2774 /* If no data_dir is specified then try to find it relative to the
2775 executable path. */
2776 if (!data_dir) {
2777 data_dir = os_find_datadir(argv[0]);
2779 /* If all else fails use the install patch specified when building. */
2780 if (!data_dir) {
2781 data_dir = CONFIG_QEMU_DATADIR;
2785 * Default to max_cpus = smp_cpus, in case the user doesn't
2786 * specify a max_cpus value.
2788 if (!max_cpus)
2789 max_cpus = smp_cpus;
2791 machine->max_cpus = machine->max_cpus ?: 1; /* Default to UP */
2792 if (smp_cpus > machine->max_cpus) {
2793 fprintf(stderr, "Number of SMP cpus requested (%d), exceeds max cpus "
2794 "supported by machine `%s' (%d)\n", smp_cpus, machine->name,
2795 machine->max_cpus);
2796 exit(1);
2799 qemu_opts_foreach(qemu_find_opts("device"), default_driver_check, NULL, 0);
2800 qemu_opts_foreach(qemu_find_opts("global"), default_driver_check, NULL, 0);
2802 if (machine->no_serial) {
2803 default_serial = 0;
2805 if (machine->no_parallel) {
2806 default_parallel = 0;
2808 if (!machine->use_virtcon) {
2809 default_virtcon = 0;
2811 if (machine->no_vga) {
2812 default_vga = 0;
2814 if (machine->no_floppy) {
2815 default_floppy = 0;
2817 if (machine->no_cdrom) {
2818 default_cdrom = 0;
2820 if (machine->no_sdcard) {
2821 default_sdcard = 0;
2824 if (display_type == DT_NOGRAPHIC) {
2825 if (default_parallel)
2826 add_device_config(DEV_PARALLEL, "null");
2827 if (default_serial && default_monitor) {
2828 add_device_config(DEV_SERIAL, "mon:stdio");
2829 } else if (default_virtcon && default_monitor) {
2830 add_device_config(DEV_VIRTCON, "mon:stdio");
2831 } else {
2832 if (default_serial)
2833 add_device_config(DEV_SERIAL, "stdio");
2834 if (default_virtcon)
2835 add_device_config(DEV_VIRTCON, "stdio");
2836 if (default_monitor)
2837 monitor_parse("stdio", "readline");
2839 } else {
2840 if (default_serial)
2841 add_device_config(DEV_SERIAL, "vc:80Cx24C");
2842 if (default_parallel)
2843 add_device_config(DEV_PARALLEL, "vc:80Cx24C");
2844 if (default_monitor)
2845 monitor_parse("vc:80Cx24C", "readline");
2846 if (default_virtcon)
2847 add_device_config(DEV_VIRTCON, "vc:80Cx24C");
2849 if (default_vga)
2850 vga_interface_type = VGA_CIRRUS;
2852 socket_init();
2854 if (qemu_opts_foreach(qemu_find_opts("chardev"), chardev_init_func, NULL, 1) != 0)
2855 exit(1);
2856 #ifdef CONFIG_VIRTFS
2857 if (qemu_opts_foreach(qemu_find_opts("fsdev"), fsdev_init_func, NULL, 1) != 0) {
2858 exit(1);
2860 #endif
2862 os_daemonize();
2864 if (pid_file && qemu_create_pidfile(pid_file) != 0) {
2865 os_pidfile_error();
2866 exit(1);
2869 if (kvm_allowed) {
2870 int ret = kvm_init();
2871 if (ret < 0) {
2872 if (!kvm_available()) {
2873 printf("KVM not supported for this target\n");
2874 } else {
2875 fprintf(stderr, "failed to initialize KVM: %s\n", strerror(-ret));
2877 exit(1);
2881 if (qemu_init_main_loop()) {
2882 fprintf(stderr, "qemu_init_main_loop failed\n");
2883 exit(1);
2885 linux_boot = (kernel_filename != NULL);
2887 if (!linux_boot && *kernel_cmdline != '\0') {
2888 fprintf(stderr, "-append only allowed with -kernel option\n");
2889 exit(1);
2892 if (!linux_boot && initrd_filename != NULL) {
2893 fprintf(stderr, "-initrd only allowed with -kernel option\n");
2894 exit(1);
2897 os_set_line_buffering();
2899 if (init_timer_alarm() < 0) {
2900 fprintf(stderr, "could not initialize alarm timer\n");
2901 exit(1);
2903 configure_icount(icount_option);
2905 if (net_init_clients() < 0) {
2906 exit(1);
2909 /* init the bluetooth world */
2910 if (foreach_device_config(DEV_BT, bt_parse))
2911 exit(1);
2913 /* init the memory */
2914 if (ram_size == 0)
2915 ram_size = DEFAULT_RAM_SIZE * 1024 * 1024;
2917 /* init the dynamic translator */
2918 cpu_exec_init_all(tb_size * 1024 * 1024);
2920 bdrv_init_with_whitelist();
2922 blk_mig_init();
2924 /* open the virtual block devices */
2925 if (snapshot)
2926 qemu_opts_foreach(qemu_find_opts("drive"), drive_enable_snapshot, NULL, 0);
2927 if (qemu_opts_foreach(qemu_find_opts("drive"), drive_init_func, &machine->use_scsi, 1) != 0)
2928 exit(1);
2930 default_drive(default_cdrom, snapshot, machine->use_scsi,
2931 IF_DEFAULT, 2, CDROM_OPTS);
2932 default_drive(default_floppy, snapshot, machine->use_scsi,
2933 IF_FLOPPY, 0, FD_OPTS);
2934 default_drive(default_sdcard, snapshot, machine->use_scsi,
2935 IF_SD, 0, SD_OPTS);
2937 register_savevm_live(NULL, "ram", 0, 4, NULL, ram_save_live, NULL,
2938 ram_load, NULL);
2940 if (nb_numa_nodes > 0) {
2941 int i;
2943 if (nb_numa_nodes > smp_cpus) {
2944 nb_numa_nodes = smp_cpus;
2947 /* If no memory size if given for any node, assume the default case
2948 * and distribute the available memory equally across all nodes
2950 for (i = 0; i < nb_numa_nodes; i++) {
2951 if (node_mem[i] != 0)
2952 break;
2954 if (i == nb_numa_nodes) {
2955 uint64_t usedmem = 0;
2957 /* On Linux, the each node's border has to be 8MB aligned,
2958 * the final node gets the rest.
2960 for (i = 0; i < nb_numa_nodes - 1; i++) {
2961 node_mem[i] = (ram_size / nb_numa_nodes) & ~((1 << 23UL) - 1);
2962 usedmem += node_mem[i];
2964 node_mem[i] = ram_size - usedmem;
2967 for (i = 0; i < nb_numa_nodes; i++) {
2968 if (node_cpumask[i] != 0)
2969 break;
2971 /* assigning the VCPUs round-robin is easier to implement, guest OSes
2972 * must cope with this anyway, because there are BIOSes out there in
2973 * real machines which also use this scheme.
2975 if (i == nb_numa_nodes) {
2976 for (i = 0; i < smp_cpus; i++) {
2977 node_cpumask[i % nb_numa_nodes] |= 1 << i;
2982 if (qemu_opts_foreach(qemu_find_opts("mon"), mon_init_func, NULL, 1) != 0) {
2983 exit(1);
2986 if (foreach_device_config(DEV_SERIAL, serial_parse) < 0)
2987 exit(1);
2988 if (foreach_device_config(DEV_PARALLEL, parallel_parse) < 0)
2989 exit(1);
2990 if (foreach_device_config(DEV_VIRTCON, virtcon_parse) < 0)
2991 exit(1);
2992 if (foreach_device_config(DEV_DEBUGCON, debugcon_parse) < 0)
2993 exit(1);
2995 module_call_init(MODULE_INIT_DEVICE);
2997 if (qemu_opts_foreach(qemu_find_opts("device"), device_help_func, NULL, 0) != 0)
2998 exit(0);
3000 if (watchdog) {
3001 i = select_watchdog(watchdog);
3002 if (i > 0)
3003 exit (i == 1 ? 1 : 0);
3006 if (machine->compat_props) {
3007 qdev_prop_register_global_list(machine->compat_props);
3009 qemu_add_globals();
3011 machine->init(ram_size, boot_devices,
3012 kernel_filename, kernel_cmdline, initrd_filename, cpu_model);
3014 cpu_synchronize_all_post_init();
3016 /* must be after terminal init, SDL library changes signal handlers */
3017 os_setup_signal_handling();
3019 set_numa_modes();
3021 current_machine = machine;
3023 /* init USB devices */
3024 if (usb_enabled) {
3025 if (foreach_device_config(DEV_USB, usb_parse) < 0)
3026 exit(1);
3029 /* init generic devices */
3030 if (qemu_opts_foreach(qemu_find_opts("device"), device_init_func, NULL, 1) != 0)
3031 exit(1);
3033 net_check_clients();
3035 /* just use the first displaystate for the moment */
3036 ds = get_displaystate();
3038 if (using_spice)
3039 display_remote++;
3040 if (display_type == DT_DEFAULT && !display_remote) {
3041 #if defined(CONFIG_SDL) || defined(CONFIG_COCOA)
3042 display_type = DT_SDL;
3043 #else
3044 vnc_display = "localhost:0,to=99";
3045 show_vnc_port = 1;
3046 #endif
3050 /* init local displays */
3051 switch (display_type) {
3052 case DT_NOGRAPHIC:
3053 break;
3054 #if defined(CONFIG_CURSES)
3055 case DT_CURSES:
3056 curses_display_init(ds, full_screen);
3057 break;
3058 #endif
3059 #if defined(CONFIG_SDL)
3060 case DT_SDL:
3061 sdl_display_init(ds, full_screen, no_frame);
3062 break;
3063 #elif defined(CONFIG_COCOA)
3064 case DT_SDL:
3065 cocoa_display_init(ds, full_screen);
3066 break;
3067 #endif
3068 default:
3069 break;
3072 /* init remote displays */
3073 if (vnc_display) {
3074 vnc_display_init(ds);
3075 if (vnc_display_open(ds, vnc_display) < 0)
3076 exit(1);
3078 if (show_vnc_port) {
3079 printf("VNC server running on `%s'\n", vnc_display_local_addr(ds));
3082 #ifdef CONFIG_SPICE
3083 if (using_spice && !qxl_enabled) {
3084 qemu_spice_display_init(ds);
3086 #endif
3088 /* display setup */
3089 dpy_resize(ds);
3090 dcl = ds->listeners;
3091 while (dcl != NULL) {
3092 if (dcl->dpy_refresh != NULL) {
3093 ds->gui_timer = qemu_new_timer_ms(rt_clock, gui_update, ds);
3094 qemu_mod_timer(ds->gui_timer, qemu_get_clock_ms(rt_clock));
3095 break;
3097 dcl = dcl->next;
3099 if (ds->gui_timer == NULL) {
3100 nographic_timer = qemu_new_timer_ms(rt_clock, nographic_update, NULL);
3101 qemu_mod_timer(nographic_timer, qemu_get_clock_ms(rt_clock));
3103 text_consoles_set_display(ds);
3105 if (gdbstub_dev && gdbserver_start(gdbstub_dev) < 0) {
3106 fprintf(stderr, "qemu: could not open gdbserver on device '%s'\n",
3107 gdbstub_dev);
3108 exit(1);
3111 qdev_machine_creation_done();
3113 if (rom_load_all() != 0) {
3114 fprintf(stderr, "rom loading failed\n");
3115 exit(1);
3118 /* TODO: once all bus devices are qdevified, this should be done
3119 * when bus is created by qdev.c */
3120 qemu_register_reset(qbus_reset_all_fn, sysbus_get_default());
3121 qemu_run_machine_init_done_notifiers();
3123 qemu_system_reset();
3124 if (loadvm) {
3125 if (load_vmstate(loadvm) < 0) {
3126 autostart = 0;
3130 if (incoming) {
3131 int ret = qemu_start_incoming_migration(incoming);
3132 if (ret < 0) {
3133 fprintf(stderr, "Migration failed. Exit code %s(%d), exiting.\n",
3134 incoming, ret);
3135 exit(ret);
3137 } else if (autostart) {
3138 vm_start();
3141 os_setup_post();
3143 main_loop();
3144 quit_timers();
3145 net_cleanup();
3147 return 0;