target-arm: Correct conversion of Thumb Neon dp encodings into ARM
[qemu/wangdongxu.git] / vl.c
blobb436952d6cd8d2d262dc96e6227b82270d43252f
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 #ifdef CONFIG_VIRTFS
152 #include "fsdev/qemu-fsdev.h"
153 #endif
155 #include "disas.h"
157 #include "qemu_socket.h"
159 #include "slirp/libslirp.h"
161 #include "trace.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 int smp_cpus = 1;
208 int max_cpus = 0;
209 int smp_cores = 1;
210 int smp_threads = 1;
211 const char *vnc_display;
212 int acpi_enabled = 1;
213 int no_hpet = 0;
214 int fd_bootchk = 1;
215 int no_reboot = 0;
216 int no_shutdown = 0;
217 int cursor_hide = 1;
218 int graphic_rotate = 0;
219 uint8_t irq0override = 1;
220 const char *watchdog;
221 QEMUOptionRom option_rom[MAX_OPTION_ROMS];
222 int nb_option_roms;
223 int semihosting_enabled = 0;
224 int old_param = 0;
225 const char *qemu_name;
226 int alt_grab = 0;
227 int ctrl_grab = 0;
228 unsigned int nb_prom_envs = 0;
229 const char *prom_envs[MAX_PROM_ENVS];
230 int boot_menu;
232 typedef struct FWBootEntry FWBootEntry;
234 struct FWBootEntry {
235 QTAILQ_ENTRY(FWBootEntry) link;
236 int32_t bootindex;
237 DeviceState *dev;
238 char *suffix;
241 QTAILQ_HEAD(, FWBootEntry) fw_boot_order = QTAILQ_HEAD_INITIALIZER(fw_boot_order);
243 int nb_numa_nodes;
244 uint64_t node_mem[MAX_NODES];
245 uint64_t node_cpumask[MAX_NODES];
247 static QEMUTimer *nographic_timer;
249 uint8_t qemu_uuid[16];
251 static QEMUBootSetHandler *boot_set_handler;
252 static void *boot_set_opaque;
254 static NotifierList exit_notifiers =
255 NOTIFIER_LIST_INITIALIZER(exit_notifiers);
257 static NotifierList machine_init_done_notifiers =
258 NOTIFIER_LIST_INITIALIZER(machine_init_done_notifiers);
260 int kvm_allowed = 0;
261 uint32_t xen_domid;
262 enum xen_mode xen_mode = XEN_EMULATE;
264 static int default_serial = 1;
265 static int default_parallel = 1;
266 static int default_virtcon = 1;
267 static int default_monitor = 1;
268 static int default_vga = 1;
269 static int default_floppy = 1;
270 static int default_cdrom = 1;
271 static int default_sdcard = 1;
273 static struct {
274 const char *driver;
275 int *flag;
276 } default_list[] = {
277 { .driver = "isa-serial", .flag = &default_serial },
278 { .driver = "isa-parallel", .flag = &default_parallel },
279 { .driver = "isa-fdc", .flag = &default_floppy },
280 { .driver = "ide-drive", .flag = &default_cdrom },
281 { .driver = "virtio-serial-pci", .flag = &default_virtcon },
282 { .driver = "virtio-serial-s390", .flag = &default_virtcon },
283 { .driver = "virtio-serial", .flag = &default_virtcon },
284 { .driver = "VGA", .flag = &default_vga },
285 { .driver = "cirrus-vga", .flag = &default_vga },
286 { .driver = "vmware-svga", .flag = &default_vga },
289 static int default_driver_check(QemuOpts *opts, void *opaque)
291 const char *driver = qemu_opt_get(opts, "driver");
292 int i;
294 if (!driver)
295 return 0;
296 for (i = 0; i < ARRAY_SIZE(default_list); i++) {
297 if (strcmp(default_list[i].driver, driver) != 0)
298 continue;
299 *(default_list[i].flag) = 0;
301 return 0;
304 /***********************************************************/
305 /* real time host monotonic timer */
307 /***********************************************************/
308 /* host time/date access */
309 void qemu_get_timedate(struct tm *tm, int offset)
311 time_t ti;
312 struct tm *ret;
314 time(&ti);
315 ti += offset;
316 if (rtc_date_offset == -1) {
317 if (rtc_utc)
318 ret = gmtime(&ti);
319 else
320 ret = localtime(&ti);
321 } else {
322 ti -= rtc_date_offset;
323 ret = gmtime(&ti);
326 memcpy(tm, ret, sizeof(struct tm));
329 int qemu_timedate_diff(struct tm *tm)
331 time_t seconds;
333 if (rtc_date_offset == -1)
334 if (rtc_utc)
335 seconds = mktimegm(tm);
336 else
337 seconds = mktime(tm);
338 else
339 seconds = mktimegm(tm) + rtc_date_offset;
341 return seconds - time(NULL);
344 void rtc_change_mon_event(struct tm *tm)
346 QObject *data;
348 data = qobject_from_jsonf("{ 'offset': %d }", qemu_timedate_diff(tm));
349 monitor_protocol_event(QEVENT_RTC_CHANGE, data);
350 qobject_decref(data);
353 static void configure_rtc_date_offset(const char *startdate, int legacy)
355 time_t rtc_start_date;
356 struct tm tm;
358 if (!strcmp(startdate, "now") && legacy) {
359 rtc_date_offset = -1;
360 } else {
361 if (sscanf(startdate, "%d-%d-%dT%d:%d:%d",
362 &tm.tm_year,
363 &tm.tm_mon,
364 &tm.tm_mday,
365 &tm.tm_hour,
366 &tm.tm_min,
367 &tm.tm_sec) == 6) {
368 /* OK */
369 } else if (sscanf(startdate, "%d-%d-%d",
370 &tm.tm_year,
371 &tm.tm_mon,
372 &tm.tm_mday) == 3) {
373 tm.tm_hour = 0;
374 tm.tm_min = 0;
375 tm.tm_sec = 0;
376 } else {
377 goto date_fail;
379 tm.tm_year -= 1900;
380 tm.tm_mon--;
381 rtc_start_date = mktimegm(&tm);
382 if (rtc_start_date == -1) {
383 date_fail:
384 fprintf(stderr, "Invalid date format. Valid formats are:\n"
385 "'2006-06-17T16:01:21' or '2006-06-17'\n");
386 exit(1);
388 rtc_date_offset = time(NULL) - rtc_start_date;
392 static void configure_rtc(QemuOpts *opts)
394 const char *value;
396 value = qemu_opt_get(opts, "base");
397 if (value) {
398 if (!strcmp(value, "utc")) {
399 rtc_utc = 1;
400 } else if (!strcmp(value, "localtime")) {
401 rtc_utc = 0;
402 } else {
403 configure_rtc_date_offset(value, 0);
406 value = qemu_opt_get(opts, "clock");
407 if (value) {
408 if (!strcmp(value, "host")) {
409 rtc_clock = host_clock;
410 } else if (!strcmp(value, "vm")) {
411 rtc_clock = vm_clock;
412 } else {
413 fprintf(stderr, "qemu: invalid option value '%s'\n", value);
414 exit(1);
417 value = qemu_opt_get(opts, "driftfix");
418 if (value) {
419 if (!strcmp(value, "slew")) {
420 rtc_td_hack = 1;
421 } else if (!strcmp(value, "none")) {
422 rtc_td_hack = 0;
423 } else {
424 fprintf(stderr, "qemu: invalid option value '%s'\n", value);
425 exit(1);
430 /***********************************************************/
431 /* Bluetooth support */
432 static int nb_hcis;
433 static int cur_hci;
434 static struct HCIInfo *hci_table[MAX_NICS];
436 static struct bt_vlan_s {
437 struct bt_scatternet_s net;
438 int id;
439 struct bt_vlan_s *next;
440 } *first_bt_vlan;
442 /* find or alloc a new bluetooth "VLAN" */
443 static struct bt_scatternet_s *qemu_find_bt_vlan(int id)
445 struct bt_vlan_s **pvlan, *vlan;
446 for (vlan = first_bt_vlan; vlan != NULL; vlan = vlan->next) {
447 if (vlan->id == id)
448 return &vlan->net;
450 vlan = qemu_mallocz(sizeof(struct bt_vlan_s));
451 vlan->id = id;
452 pvlan = &first_bt_vlan;
453 while (*pvlan != NULL)
454 pvlan = &(*pvlan)->next;
455 *pvlan = vlan;
456 return &vlan->net;
459 static void null_hci_send(struct HCIInfo *hci, const uint8_t *data, int len)
463 static int null_hci_addr_set(struct HCIInfo *hci, const uint8_t *bd_addr)
465 return -ENOTSUP;
468 static struct HCIInfo null_hci = {
469 .cmd_send = null_hci_send,
470 .sco_send = null_hci_send,
471 .acl_send = null_hci_send,
472 .bdaddr_set = null_hci_addr_set,
475 struct HCIInfo *qemu_next_hci(void)
477 if (cur_hci == nb_hcis)
478 return &null_hci;
480 return hci_table[cur_hci++];
483 static struct HCIInfo *hci_init(const char *str)
485 char *endp;
486 struct bt_scatternet_s *vlan = 0;
488 if (!strcmp(str, "null"))
489 /* null */
490 return &null_hci;
491 else if (!strncmp(str, "host", 4) && (str[4] == '\0' || str[4] == ':'))
492 /* host[:hciN] */
493 return bt_host_hci(str[4] ? str + 5 : "hci0");
494 else if (!strncmp(str, "hci", 3)) {
495 /* hci[,vlan=n] */
496 if (str[3]) {
497 if (!strncmp(str + 3, ",vlan=", 6)) {
498 vlan = qemu_find_bt_vlan(strtol(str + 9, &endp, 0));
499 if (*endp)
500 vlan = 0;
502 } else
503 vlan = qemu_find_bt_vlan(0);
504 if (vlan)
505 return bt_new_hci(vlan);
508 fprintf(stderr, "qemu: Unknown bluetooth HCI `%s'.\n", str);
510 return 0;
513 static int bt_hci_parse(const char *str)
515 struct HCIInfo *hci;
516 bdaddr_t bdaddr;
518 if (nb_hcis >= MAX_NICS) {
519 fprintf(stderr, "qemu: Too many bluetooth HCIs (max %i).\n", MAX_NICS);
520 return -1;
523 hci = hci_init(str);
524 if (!hci)
525 return -1;
527 bdaddr.b[0] = 0x52;
528 bdaddr.b[1] = 0x54;
529 bdaddr.b[2] = 0x00;
530 bdaddr.b[3] = 0x12;
531 bdaddr.b[4] = 0x34;
532 bdaddr.b[5] = 0x56 + nb_hcis;
533 hci->bdaddr_set(hci, bdaddr.b);
535 hci_table[nb_hcis++] = hci;
537 return 0;
540 static void bt_vhci_add(int vlan_id)
542 struct bt_scatternet_s *vlan = qemu_find_bt_vlan(vlan_id);
544 if (!vlan->slave)
545 fprintf(stderr, "qemu: warning: adding a VHCI to "
546 "an empty scatternet %i\n", vlan_id);
548 bt_vhci_init(bt_new_hci(vlan));
551 static struct bt_device_s *bt_device_add(const char *opt)
553 struct bt_scatternet_s *vlan;
554 int vlan_id = 0;
555 char *endp = strstr(opt, ",vlan=");
556 int len = (endp ? endp - opt : strlen(opt)) + 1;
557 char devname[10];
559 pstrcpy(devname, MIN(sizeof(devname), len), opt);
561 if (endp) {
562 vlan_id = strtol(endp + 6, &endp, 0);
563 if (*endp) {
564 fprintf(stderr, "qemu: unrecognised bluetooth vlan Id\n");
565 return 0;
569 vlan = qemu_find_bt_vlan(vlan_id);
571 if (!vlan->slave)
572 fprintf(stderr, "qemu: warning: adding a slave device to "
573 "an empty scatternet %i\n", vlan_id);
575 if (!strcmp(devname, "keyboard"))
576 return bt_keyboard_init(vlan);
578 fprintf(stderr, "qemu: unsupported bluetooth device `%s'\n", devname);
579 return 0;
582 static int bt_parse(const char *opt)
584 const char *endp, *p;
585 int vlan;
587 if (strstart(opt, "hci", &endp)) {
588 if (!*endp || *endp == ',') {
589 if (*endp)
590 if (!strstart(endp, ",vlan=", 0))
591 opt = endp + 1;
593 return bt_hci_parse(opt);
595 } else if (strstart(opt, "vhci", &endp)) {
596 if (!*endp || *endp == ',') {
597 if (*endp) {
598 if (strstart(endp, ",vlan=", &p)) {
599 vlan = strtol(p, (char **) &endp, 0);
600 if (*endp) {
601 fprintf(stderr, "qemu: bad scatternet '%s'\n", p);
602 return 1;
604 } else {
605 fprintf(stderr, "qemu: bad parameter '%s'\n", endp + 1);
606 return 1;
608 } else
609 vlan = 0;
611 bt_vhci_add(vlan);
612 return 0;
614 } else if (strstart(opt, "device:", &endp))
615 return !bt_device_add(endp);
617 fprintf(stderr, "qemu: bad bluetooth parameter '%s'\n", opt);
618 return 1;
621 /***********************************************************/
622 /* QEMU Block devices */
624 #define HD_OPTS "media=disk"
625 #define CDROM_OPTS "media=cdrom"
626 #define FD_OPTS ""
627 #define PFLASH_OPTS ""
628 #define MTD_OPTS ""
629 #define SD_OPTS ""
631 static int drive_init_func(QemuOpts *opts, void *opaque)
633 int *use_scsi = opaque;
635 return drive_init(opts, *use_scsi) == NULL;
638 static int drive_enable_snapshot(QemuOpts *opts, void *opaque)
640 if (NULL == qemu_opt_get(opts, "snapshot")) {
641 qemu_opt_set(opts, "snapshot", "on");
643 return 0;
646 static void default_drive(int enable, int snapshot, int use_scsi,
647 BlockInterfaceType type, int index,
648 const char *optstr)
650 QemuOpts *opts;
652 if (type == IF_DEFAULT) {
653 type = use_scsi ? IF_SCSI : IF_IDE;
656 if (!enable || drive_get_by_index(type, index)) {
657 return;
660 opts = drive_add(type, index, NULL, optstr);
661 if (snapshot) {
662 drive_enable_snapshot(opts, NULL);
664 if (!drive_init(opts, use_scsi)) {
665 exit(1);
669 void qemu_register_boot_set(QEMUBootSetHandler *func, void *opaque)
671 boot_set_handler = func;
672 boot_set_opaque = opaque;
675 int qemu_boot_set(const char *boot_devices)
677 if (!boot_set_handler) {
678 return -EINVAL;
680 return boot_set_handler(boot_set_opaque, boot_devices);
683 static void validate_bootdevices(char *devices)
685 /* We just do some generic consistency checks */
686 const char *p;
687 int bitmap = 0;
689 for (p = devices; *p != '\0'; p++) {
690 /* Allowed boot devices are:
691 * a-b: floppy disk drives
692 * c-f: IDE disk drives
693 * g-m: machine implementation dependant drives
694 * n-p: network devices
695 * It's up to each machine implementation to check if the given boot
696 * devices match the actual hardware implementation and firmware
697 * features.
699 if (*p < 'a' || *p > 'p') {
700 fprintf(stderr, "Invalid boot device '%c'\n", *p);
701 exit(1);
703 if (bitmap & (1 << (*p - 'a'))) {
704 fprintf(stderr, "Boot device '%c' was given twice\n", *p);
705 exit(1);
707 bitmap |= 1 << (*p - 'a');
711 static void restore_boot_devices(void *opaque)
713 char *standard_boot_devices = opaque;
714 static int first = 1;
716 /* Restore boot order and remove ourselves after the first boot */
717 if (first) {
718 first = 0;
719 return;
722 qemu_boot_set(standard_boot_devices);
724 qemu_unregister_reset(restore_boot_devices, standard_boot_devices);
725 qemu_free(standard_boot_devices);
728 void add_boot_device_path(int32_t bootindex, DeviceState *dev,
729 const char *suffix)
731 FWBootEntry *node, *i;
733 if (bootindex < 0) {
734 return;
737 assert(dev != NULL || suffix != NULL);
739 node = qemu_mallocz(sizeof(FWBootEntry));
740 node->bootindex = bootindex;
741 node->suffix = suffix ? qemu_strdup(suffix) : NULL;
742 node->dev = dev;
744 QTAILQ_FOREACH(i, &fw_boot_order, link) {
745 if (i->bootindex == bootindex) {
746 fprintf(stderr, "Two devices with same boot index %d\n", bootindex);
747 exit(1);
748 } else if (i->bootindex < bootindex) {
749 continue;
751 QTAILQ_INSERT_BEFORE(i, node, link);
752 return;
754 QTAILQ_INSERT_TAIL(&fw_boot_order, node, link);
758 * This function returns null terminated string that consist of new line
759 * separated device pathes.
761 * memory pointed by "size" is assigned total length of the array in bytes
764 char *get_boot_devices_list(uint32_t *size)
766 FWBootEntry *i;
767 uint32_t total = 0;
768 char *list = NULL;
770 QTAILQ_FOREACH(i, &fw_boot_order, link) {
771 char *devpath = NULL, *bootpath;
772 int len;
774 if (i->dev) {
775 devpath = qdev_get_fw_dev_path(i->dev);
776 assert(devpath);
779 if (i->suffix && devpath) {
780 size_t bootpathlen = strlen(devpath) + strlen(i->suffix) + 1;
782 bootpath = qemu_malloc(bootpathlen);
783 snprintf(bootpath, bootpathlen, "%s%s", devpath, i->suffix);
784 qemu_free(devpath);
785 } else if (devpath) {
786 bootpath = devpath;
787 } else {
788 bootpath = qemu_strdup(i->suffix);
789 assert(bootpath);
792 if (total) {
793 list[total-1] = '\n';
795 len = strlen(bootpath) + 1;
796 list = qemu_realloc(list, total + len);
797 memcpy(&list[total], bootpath, len);
798 total += len;
799 qemu_free(bootpath);
802 *size = total;
804 return list;
807 static void numa_add(const char *optarg)
809 char option[128];
810 char *endptr;
811 unsigned long long value, endvalue;
812 int nodenr;
814 optarg = get_opt_name(option, 128, optarg, ',') + 1;
815 if (!strcmp(option, "node")) {
816 if (get_param_value(option, 128, "nodeid", optarg) == 0) {
817 nodenr = nb_numa_nodes;
818 } else {
819 nodenr = strtoull(option, NULL, 10);
822 if (get_param_value(option, 128, "mem", optarg) == 0) {
823 node_mem[nodenr] = 0;
824 } else {
825 int64_t sval;
826 sval = strtosz(option, NULL);
827 if (sval < 0) {
828 fprintf(stderr, "qemu: invalid numa mem size: %s\n", optarg);
829 exit(1);
831 node_mem[nodenr] = sval;
833 if (get_param_value(option, 128, "cpus", optarg) == 0) {
834 node_cpumask[nodenr] = 0;
835 } else {
836 value = strtoull(option, &endptr, 10);
837 if (value >= 64) {
838 value = 63;
839 fprintf(stderr, "only 64 CPUs in NUMA mode supported.\n");
840 } else {
841 if (*endptr == '-') {
842 endvalue = strtoull(endptr+1, &endptr, 10);
843 if (endvalue >= 63) {
844 endvalue = 62;
845 fprintf(stderr,
846 "only 63 CPUs in NUMA mode supported.\n");
848 value = (2ULL << endvalue) - (1ULL << value);
849 } else {
850 value = 1ULL << value;
853 node_cpumask[nodenr] = value;
855 nb_numa_nodes++;
857 return;
860 static void smp_parse(const char *optarg)
862 int smp, sockets = 0, threads = 0, cores = 0;
863 char *endptr;
864 char option[128];
866 smp = strtoul(optarg, &endptr, 10);
867 if (endptr != optarg) {
868 if (*endptr == ',') {
869 endptr++;
872 if (get_param_value(option, 128, "sockets", endptr) != 0)
873 sockets = strtoull(option, NULL, 10);
874 if (get_param_value(option, 128, "cores", endptr) != 0)
875 cores = strtoull(option, NULL, 10);
876 if (get_param_value(option, 128, "threads", endptr) != 0)
877 threads = strtoull(option, NULL, 10);
878 if (get_param_value(option, 128, "maxcpus", endptr) != 0)
879 max_cpus = strtoull(option, NULL, 10);
881 /* compute missing values, prefer sockets over cores over threads */
882 if (smp == 0 || sockets == 0) {
883 sockets = sockets > 0 ? sockets : 1;
884 cores = cores > 0 ? cores : 1;
885 threads = threads > 0 ? threads : 1;
886 if (smp == 0) {
887 smp = cores * threads * sockets;
889 } else {
890 if (cores == 0) {
891 threads = threads > 0 ? threads : 1;
892 cores = smp / (sockets * threads);
893 } else {
894 threads = smp / (cores * sockets);
897 smp_cpus = smp;
898 smp_cores = cores > 0 ? cores : 1;
899 smp_threads = threads > 0 ? threads : 1;
900 if (max_cpus == 0)
901 max_cpus = smp_cpus;
904 /***********************************************************/
905 /* USB devices */
907 static int usb_device_add(const char *devname)
909 const char *p;
910 USBDevice *dev = NULL;
912 if (!usb_enabled)
913 return -1;
915 /* drivers with .usbdevice_name entry in USBDeviceInfo */
916 dev = usbdevice_create(devname);
917 if (dev)
918 goto done;
920 /* the other ones */
921 if (strstart(devname, "host:", &p)) {
922 dev = usb_host_device_open(p);
923 } else if (!strcmp(devname, "bt") || strstart(devname, "bt:", &p)) {
924 dev = usb_bt_init(devname[2] ? hci_init(p) :
925 bt_new_hci(qemu_find_bt_vlan(0)));
926 } else {
927 return -1;
929 if (!dev)
930 return -1;
932 done:
933 return 0;
936 static int usb_device_del(const char *devname)
938 int bus_num, addr;
939 const char *p;
941 if (strstart(devname, "host:", &p))
942 return usb_host_device_close(p);
944 if (!usb_enabled)
945 return -1;
947 p = strchr(devname, '.');
948 if (!p)
949 return -1;
950 bus_num = strtoul(devname, NULL, 0);
951 addr = strtoul(p + 1, NULL, 0);
953 return usb_device_delete_addr(bus_num, addr);
956 static int usb_parse(const char *cmdline)
958 int r;
959 r = usb_device_add(cmdline);
960 if (r < 0) {
961 fprintf(stderr, "qemu: could not add USB device '%s'\n", cmdline);
963 return r;
966 void do_usb_add(Monitor *mon, const QDict *qdict)
968 const char *devname = qdict_get_str(qdict, "devname");
969 if (usb_device_add(devname) < 0) {
970 error_report("could not add USB device '%s'", devname);
974 void do_usb_del(Monitor *mon, const QDict *qdict)
976 const char *devname = qdict_get_str(qdict, "devname");
977 if (usb_device_del(devname) < 0) {
978 error_report("could not delete USB device '%s'", devname);
982 /***********************************************************/
983 /* PCMCIA/Cardbus */
985 static struct pcmcia_socket_entry_s {
986 PCMCIASocket *socket;
987 struct pcmcia_socket_entry_s *next;
988 } *pcmcia_sockets = 0;
990 void pcmcia_socket_register(PCMCIASocket *socket)
992 struct pcmcia_socket_entry_s *entry;
994 entry = qemu_malloc(sizeof(struct pcmcia_socket_entry_s));
995 entry->socket = socket;
996 entry->next = pcmcia_sockets;
997 pcmcia_sockets = entry;
1000 void pcmcia_socket_unregister(PCMCIASocket *socket)
1002 struct pcmcia_socket_entry_s *entry, **ptr;
1004 ptr = &pcmcia_sockets;
1005 for (entry = *ptr; entry; ptr = &entry->next, entry = *ptr)
1006 if (entry->socket == socket) {
1007 *ptr = entry->next;
1008 qemu_free(entry);
1012 void pcmcia_info(Monitor *mon)
1014 struct pcmcia_socket_entry_s *iter;
1016 if (!pcmcia_sockets)
1017 monitor_printf(mon, "No PCMCIA sockets\n");
1019 for (iter = pcmcia_sockets; iter; iter = iter->next)
1020 monitor_printf(mon, "%s: %s\n", iter->socket->slot_string,
1021 iter->socket->attached ? iter->socket->card_string :
1022 "Empty");
1025 /***********************************************************/
1026 /* I/O handling */
1028 typedef struct IOHandlerRecord {
1029 int fd;
1030 IOCanReadHandler *fd_read_poll;
1031 IOHandler *fd_read;
1032 IOHandler *fd_write;
1033 int deleted;
1034 void *opaque;
1035 /* temporary data */
1036 struct pollfd *ufd;
1037 QLIST_ENTRY(IOHandlerRecord) next;
1038 } IOHandlerRecord;
1040 static QLIST_HEAD(, IOHandlerRecord) io_handlers =
1041 QLIST_HEAD_INITIALIZER(io_handlers);
1044 /* XXX: fd_read_poll should be suppressed, but an API change is
1045 necessary in the character devices to suppress fd_can_read(). */
1046 int qemu_set_fd_handler2(int fd,
1047 IOCanReadHandler *fd_read_poll,
1048 IOHandler *fd_read,
1049 IOHandler *fd_write,
1050 void *opaque)
1052 IOHandlerRecord *ioh;
1054 if (!fd_read && !fd_write) {
1055 QLIST_FOREACH(ioh, &io_handlers, next) {
1056 if (ioh->fd == fd) {
1057 ioh->deleted = 1;
1058 break;
1061 } else {
1062 QLIST_FOREACH(ioh, &io_handlers, next) {
1063 if (ioh->fd == fd)
1064 goto found;
1066 ioh = qemu_mallocz(sizeof(IOHandlerRecord));
1067 QLIST_INSERT_HEAD(&io_handlers, ioh, next);
1068 found:
1069 ioh->fd = fd;
1070 ioh->fd_read_poll = fd_read_poll;
1071 ioh->fd_read = fd_read;
1072 ioh->fd_write = fd_write;
1073 ioh->opaque = opaque;
1074 ioh->deleted = 0;
1076 return 0;
1079 int qemu_set_fd_handler(int fd,
1080 IOHandler *fd_read,
1081 IOHandler *fd_write,
1082 void *opaque)
1084 return qemu_set_fd_handler2(fd, NULL, fd_read, fd_write, opaque);
1087 /***********************************************************/
1088 /* machine registration */
1090 static QEMUMachine *first_machine = NULL;
1091 QEMUMachine *current_machine = NULL;
1093 int qemu_register_machine(QEMUMachine *m)
1095 QEMUMachine **pm;
1096 pm = &first_machine;
1097 while (*pm != NULL)
1098 pm = &(*pm)->next;
1099 m->next = NULL;
1100 *pm = m;
1101 return 0;
1104 static QEMUMachine *find_machine(const char *name)
1106 QEMUMachine *m;
1108 for(m = first_machine; m != NULL; m = m->next) {
1109 if (!strcmp(m->name, name))
1110 return m;
1111 if (m->alias && !strcmp(m->alias, name))
1112 return m;
1114 return NULL;
1117 static QEMUMachine *find_default_machine(void)
1119 QEMUMachine *m;
1121 for(m = first_machine; m != NULL; m = m->next) {
1122 if (m->is_default) {
1123 return m;
1126 return NULL;
1129 /***********************************************************/
1130 /* main execution loop */
1132 static void gui_update(void *opaque)
1134 uint64_t interval = GUI_REFRESH_INTERVAL;
1135 DisplayState *ds = opaque;
1136 DisplayChangeListener *dcl = ds->listeners;
1138 qemu_flush_coalesced_mmio_buffer();
1139 dpy_refresh(ds);
1141 while (dcl != NULL) {
1142 if (dcl->gui_timer_interval &&
1143 dcl->gui_timer_interval < interval)
1144 interval = dcl->gui_timer_interval;
1145 dcl = dcl->next;
1147 qemu_mod_timer(ds->gui_timer, interval + qemu_get_clock(rt_clock));
1150 static void nographic_update(void *opaque)
1152 uint64_t interval = GUI_REFRESH_INTERVAL;
1154 qemu_flush_coalesced_mmio_buffer();
1155 qemu_mod_timer(nographic_timer, interval + qemu_get_clock(rt_clock));
1158 struct vm_change_state_entry {
1159 VMChangeStateHandler *cb;
1160 void *opaque;
1161 QLIST_ENTRY (vm_change_state_entry) entries;
1164 static QLIST_HEAD(vm_change_state_head, vm_change_state_entry) vm_change_state_head;
1166 VMChangeStateEntry *qemu_add_vm_change_state_handler(VMChangeStateHandler *cb,
1167 void *opaque)
1169 VMChangeStateEntry *e;
1171 e = qemu_mallocz(sizeof (*e));
1173 e->cb = cb;
1174 e->opaque = opaque;
1175 QLIST_INSERT_HEAD(&vm_change_state_head, e, entries);
1176 return e;
1179 void qemu_del_vm_change_state_handler(VMChangeStateEntry *e)
1181 QLIST_REMOVE (e, entries);
1182 qemu_free (e);
1185 void vm_state_notify(int running, int reason)
1187 VMChangeStateEntry *e;
1189 trace_vm_state_notify(running, reason);
1191 for (e = vm_change_state_head.lh_first; e; e = e->entries.le_next) {
1192 e->cb(e->opaque, running, reason);
1196 void vm_start(void)
1198 if (!vm_running) {
1199 cpu_enable_ticks();
1200 vm_running = 1;
1201 vm_state_notify(1, 0);
1202 resume_all_vcpus();
1203 monitor_protocol_event(QEVENT_RESUME, NULL);
1207 /* reset/shutdown handler */
1209 typedef struct QEMUResetEntry {
1210 QTAILQ_ENTRY(QEMUResetEntry) entry;
1211 QEMUResetHandler *func;
1212 void *opaque;
1213 } QEMUResetEntry;
1215 static QTAILQ_HEAD(reset_handlers, QEMUResetEntry) reset_handlers =
1216 QTAILQ_HEAD_INITIALIZER(reset_handlers);
1217 static int reset_requested;
1218 static int shutdown_requested;
1219 static int powerdown_requested;
1220 static int debug_requested;
1221 static int vmstop_requested;
1223 int qemu_shutdown_requested(void)
1225 int r = shutdown_requested;
1226 shutdown_requested = 0;
1227 return r;
1230 int qemu_reset_requested(void)
1232 int r = reset_requested;
1233 reset_requested = 0;
1234 return r;
1237 int qemu_powerdown_requested(void)
1239 int r = powerdown_requested;
1240 powerdown_requested = 0;
1241 return r;
1244 static int qemu_debug_requested(void)
1246 int r = debug_requested;
1247 debug_requested = 0;
1248 return r;
1251 static int qemu_vmstop_requested(void)
1253 int r = vmstop_requested;
1254 vmstop_requested = 0;
1255 return r;
1258 void qemu_register_reset(QEMUResetHandler *func, void *opaque)
1260 QEMUResetEntry *re = qemu_mallocz(sizeof(QEMUResetEntry));
1262 re->func = func;
1263 re->opaque = opaque;
1264 QTAILQ_INSERT_TAIL(&reset_handlers, re, entry);
1267 void qemu_unregister_reset(QEMUResetHandler *func, void *opaque)
1269 QEMUResetEntry *re;
1271 QTAILQ_FOREACH(re, &reset_handlers, entry) {
1272 if (re->func == func && re->opaque == opaque) {
1273 QTAILQ_REMOVE(&reset_handlers, re, entry);
1274 qemu_free(re);
1275 return;
1280 void qemu_system_reset(void)
1282 QEMUResetEntry *re, *nre;
1284 /* reset all devices */
1285 QTAILQ_FOREACH_SAFE(re, &reset_handlers, entry, nre) {
1286 re->func(re->opaque);
1288 monitor_protocol_event(QEVENT_RESET, NULL);
1289 cpu_synchronize_all_post_reset();
1292 void qemu_system_reset_request(void)
1294 if (no_reboot) {
1295 shutdown_requested = 1;
1296 } else {
1297 reset_requested = 1;
1299 cpu_stop_current();
1300 qemu_notify_event();
1303 void qemu_system_shutdown_request(void)
1305 shutdown_requested = 1;
1306 qemu_notify_event();
1309 void qemu_system_powerdown_request(void)
1311 powerdown_requested = 1;
1312 qemu_notify_event();
1315 void qemu_system_debug_request(void)
1317 debug_requested = 1;
1318 qemu_notify_event();
1321 void qemu_system_vmstop_request(int reason)
1323 vmstop_requested = reason;
1324 qemu_notify_event();
1327 void main_loop_wait(int nonblocking)
1329 IOHandlerRecord *ioh;
1330 fd_set rfds, wfds, xfds;
1331 int ret, nfds;
1332 struct timeval tv;
1333 int timeout;
1335 if (nonblocking)
1336 timeout = 0;
1337 else {
1338 timeout = qemu_calculate_timeout();
1339 qemu_bh_update_timeout(&timeout);
1342 os_host_main_loop_wait(&timeout);
1344 /* poll any events */
1345 /* XXX: separate device handlers from system ones */
1346 nfds = -1;
1347 FD_ZERO(&rfds);
1348 FD_ZERO(&wfds);
1349 FD_ZERO(&xfds);
1350 QLIST_FOREACH(ioh, &io_handlers, next) {
1351 if (ioh->deleted)
1352 continue;
1353 if (ioh->fd_read &&
1354 (!ioh->fd_read_poll ||
1355 ioh->fd_read_poll(ioh->opaque) != 0)) {
1356 FD_SET(ioh->fd, &rfds);
1357 if (ioh->fd > nfds)
1358 nfds = ioh->fd;
1360 if (ioh->fd_write) {
1361 FD_SET(ioh->fd, &wfds);
1362 if (ioh->fd > nfds)
1363 nfds = ioh->fd;
1367 tv.tv_sec = timeout / 1000;
1368 tv.tv_usec = (timeout % 1000) * 1000;
1370 slirp_select_fill(&nfds, &rfds, &wfds, &xfds);
1372 qemu_mutex_unlock_iothread();
1373 ret = select(nfds + 1, &rfds, &wfds, &xfds, &tv);
1374 qemu_mutex_lock_iothread();
1375 if (ret > 0) {
1376 IOHandlerRecord *pioh;
1378 QLIST_FOREACH_SAFE(ioh, &io_handlers, next, pioh) {
1379 if (!ioh->deleted && ioh->fd_read && FD_ISSET(ioh->fd, &rfds)) {
1380 ioh->fd_read(ioh->opaque);
1382 if (!ioh->deleted && ioh->fd_write && FD_ISSET(ioh->fd, &wfds)) {
1383 ioh->fd_write(ioh->opaque);
1386 /* Do this last in case read/write handlers marked it for deletion */
1387 if (ioh->deleted) {
1388 QLIST_REMOVE(ioh, next);
1389 qemu_free(ioh);
1394 slirp_select_poll(&rfds, &wfds, &xfds, (ret < 0));
1396 qemu_run_all_timers();
1398 /* Check bottom-halves last in case any of the earlier events triggered
1399 them. */
1400 qemu_bh_poll();
1404 #ifndef CONFIG_IOTHREAD
1405 static int vm_request_pending(void)
1407 return powerdown_requested ||
1408 reset_requested ||
1409 shutdown_requested ||
1410 debug_requested ||
1411 vmstop_requested;
1413 #endif
1415 qemu_irq qemu_system_powerdown;
1417 static void main_loop(void)
1419 bool nonblocking = false;
1420 #ifdef CONFIG_PROFILER
1421 int64_t ti;
1422 #endif
1423 int r;
1425 qemu_main_loop_start();
1427 for (;;) {
1428 #ifndef CONFIG_IOTHREAD
1429 nonblocking = cpu_exec_all();
1430 if (vm_request_pending()) {
1431 nonblocking = true;
1433 #endif
1434 #ifdef CONFIG_PROFILER
1435 ti = profile_getclock();
1436 #endif
1437 main_loop_wait(nonblocking);
1438 #ifdef CONFIG_PROFILER
1439 dev_time += profile_getclock() - ti;
1440 #endif
1442 if (qemu_debug_requested()) {
1443 vm_stop(VMSTOP_DEBUG);
1445 if (qemu_shutdown_requested()) {
1446 monitor_protocol_event(QEVENT_SHUTDOWN, NULL);
1447 if (no_shutdown) {
1448 vm_stop(VMSTOP_SHUTDOWN);
1449 no_shutdown = 0;
1450 } else
1451 break;
1453 if (qemu_reset_requested()) {
1454 pause_all_vcpus();
1455 qemu_system_reset();
1456 resume_all_vcpus();
1458 if (qemu_powerdown_requested()) {
1459 monitor_protocol_event(QEVENT_POWERDOWN, NULL);
1460 qemu_irq_raise(qemu_system_powerdown);
1462 if ((r = qemu_vmstop_requested())) {
1463 vm_stop(r);
1466 bdrv_close_all();
1467 pause_all_vcpus();
1470 static void version(void)
1472 printf("QEMU emulator version " QEMU_VERSION QEMU_PKGVERSION ", Copyright (c) 2003-2008 Fabrice Bellard\n");
1475 static void help(int exitcode)
1477 const char *options_help =
1478 #define DEF(option, opt_arg, opt_enum, opt_help, arch_mask) \
1479 opt_help
1480 #define DEFHEADING(text) stringify(text) "\n"
1481 #include "qemu-options.def"
1482 #undef DEF
1483 #undef DEFHEADING
1484 #undef GEN_DOCS
1486 version();
1487 printf("usage: %s [options] [disk_image]\n"
1488 "\n"
1489 "'disk_image' is a raw hard disk image for IDE hard disk 0\n"
1490 "\n"
1491 "%s\n"
1492 "During emulation, the following keys are useful:\n"
1493 "ctrl-alt-f toggle full screen\n"
1494 "ctrl-alt-n switch to virtual console 'n'\n"
1495 "ctrl-alt toggle mouse and keyboard grab\n"
1496 "\n"
1497 "When using -nographic, press 'ctrl-a h' to get some help.\n",
1498 "qemu",
1499 options_help);
1500 exit(exitcode);
1503 #define HAS_ARG 0x0001
1505 typedef struct QEMUOption {
1506 const char *name;
1507 int flags;
1508 int index;
1509 uint32_t arch_mask;
1510 } QEMUOption;
1512 static const QEMUOption qemu_options[] = {
1513 { "h", 0, QEMU_OPTION_h, QEMU_ARCH_ALL },
1514 #define DEF(option, opt_arg, opt_enum, opt_help, arch_mask) \
1515 { option, opt_arg, opt_enum, arch_mask },
1516 #define DEFHEADING(text)
1517 #include "qemu-options.def"
1518 #undef DEF
1519 #undef DEFHEADING
1520 #undef GEN_DOCS
1521 { NULL },
1523 static void select_vgahw (const char *p)
1525 const char *opts;
1527 default_vga = 0;
1528 vga_interface_type = VGA_NONE;
1529 if (strstart(p, "std", &opts)) {
1530 vga_interface_type = VGA_STD;
1531 } else if (strstart(p, "cirrus", &opts)) {
1532 vga_interface_type = VGA_CIRRUS;
1533 } else if (strstart(p, "vmware", &opts)) {
1534 vga_interface_type = VGA_VMWARE;
1535 } else if (strstart(p, "xenfb", &opts)) {
1536 vga_interface_type = VGA_XENFB;
1537 } else if (strstart(p, "qxl", &opts)) {
1538 vga_interface_type = VGA_QXL;
1539 } else if (!strstart(p, "none", &opts)) {
1540 invalid_vga:
1541 fprintf(stderr, "Unknown vga type: %s\n", p);
1542 exit(1);
1544 while (*opts) {
1545 const char *nextopt;
1547 if (strstart(opts, ",retrace=", &nextopt)) {
1548 opts = nextopt;
1549 if (strstart(opts, "dumb", &nextopt))
1550 vga_retrace_method = VGA_RETRACE_DUMB;
1551 else if (strstart(opts, "precise", &nextopt))
1552 vga_retrace_method = VGA_RETRACE_PRECISE;
1553 else goto invalid_vga;
1554 } else goto invalid_vga;
1555 opts = nextopt;
1559 static int balloon_parse(const char *arg)
1561 QemuOpts *opts;
1563 if (strcmp(arg, "none") == 0) {
1564 return 0;
1567 if (!strncmp(arg, "virtio", 6)) {
1568 if (arg[6] == ',') {
1569 /* have params -> parse them */
1570 opts = qemu_opts_parse(qemu_find_opts("device"), arg+7, 0);
1571 if (!opts)
1572 return -1;
1573 } else {
1574 /* create empty opts */
1575 opts = qemu_opts_create(qemu_find_opts("device"), NULL, 0);
1577 qemu_opt_set(opts, "driver", "virtio-balloon-pci");
1578 return 0;
1581 return -1;
1584 char *qemu_find_file(int type, const char *name)
1586 int len;
1587 const char *subdir;
1588 char *buf;
1590 /* If name contains path separators then try it as a straight path. */
1591 if ((strchr(name, '/') || strchr(name, '\\'))
1592 && access(name, R_OK) == 0) {
1593 return qemu_strdup(name);
1595 switch (type) {
1596 case QEMU_FILE_TYPE_BIOS:
1597 subdir = "";
1598 break;
1599 case QEMU_FILE_TYPE_KEYMAP:
1600 subdir = "keymaps/";
1601 break;
1602 default:
1603 abort();
1605 len = strlen(data_dir) + strlen(name) + strlen(subdir) + 2;
1606 buf = qemu_mallocz(len);
1607 snprintf(buf, len, "%s/%s%s", data_dir, subdir, name);
1608 if (access(buf, R_OK)) {
1609 qemu_free(buf);
1610 return NULL;
1612 return buf;
1615 static int device_help_func(QemuOpts *opts, void *opaque)
1617 return qdev_device_help(opts);
1620 static int device_init_func(QemuOpts *opts, void *opaque)
1622 DeviceState *dev;
1624 dev = qdev_device_add(opts);
1625 if (!dev)
1626 return -1;
1627 return 0;
1630 static int chardev_init_func(QemuOpts *opts, void *opaque)
1632 CharDriverState *chr;
1634 chr = qemu_chr_open_opts(opts, NULL);
1635 if (!chr)
1636 return -1;
1637 return 0;
1640 #ifdef CONFIG_VIRTFS
1641 static int fsdev_init_func(QemuOpts *opts, void *opaque)
1643 int ret;
1644 ret = qemu_fsdev_add(opts);
1646 return ret;
1648 #endif
1650 static int mon_init_func(QemuOpts *opts, void *opaque)
1652 CharDriverState *chr;
1653 const char *chardev;
1654 const char *mode;
1655 int flags;
1657 mode = qemu_opt_get(opts, "mode");
1658 if (mode == NULL) {
1659 mode = "readline";
1661 if (strcmp(mode, "readline") == 0) {
1662 flags = MONITOR_USE_READLINE;
1663 } else if (strcmp(mode, "control") == 0) {
1664 flags = MONITOR_USE_CONTROL;
1665 } else {
1666 fprintf(stderr, "unknown monitor mode \"%s\"\n", mode);
1667 exit(1);
1670 if (qemu_opt_get_bool(opts, "pretty", 0))
1671 flags |= MONITOR_USE_PRETTY;
1673 if (qemu_opt_get_bool(opts, "default", 0))
1674 flags |= MONITOR_IS_DEFAULT;
1676 chardev = qemu_opt_get(opts, "chardev");
1677 chr = qemu_chr_find(chardev);
1678 if (chr == NULL) {
1679 fprintf(stderr, "chardev \"%s\" not found\n", chardev);
1680 exit(1);
1683 monitor_init(chr, flags);
1684 return 0;
1687 static void monitor_parse(const char *optarg, const char *mode)
1689 static int monitor_device_index = 0;
1690 QemuOpts *opts;
1691 const char *p;
1692 char label[32];
1693 int def = 0;
1695 if (strstart(optarg, "chardev:", &p)) {
1696 snprintf(label, sizeof(label), "%s", p);
1697 } else {
1698 snprintf(label, sizeof(label), "compat_monitor%d",
1699 monitor_device_index);
1700 if (monitor_device_index == 0) {
1701 def = 1;
1703 opts = qemu_chr_parse_compat(label, optarg);
1704 if (!opts) {
1705 fprintf(stderr, "parse error: %s\n", optarg);
1706 exit(1);
1710 opts = qemu_opts_create(qemu_find_opts("mon"), label, 1);
1711 if (!opts) {
1712 fprintf(stderr, "duplicate chardev: %s\n", label);
1713 exit(1);
1715 qemu_opt_set(opts, "mode", mode);
1716 qemu_opt_set(opts, "chardev", label);
1717 if (def)
1718 qemu_opt_set(opts, "default", "on");
1719 monitor_device_index++;
1722 struct device_config {
1723 enum {
1724 DEV_USB, /* -usbdevice */
1725 DEV_BT, /* -bt */
1726 DEV_SERIAL, /* -serial */
1727 DEV_PARALLEL, /* -parallel */
1728 DEV_VIRTCON, /* -virtioconsole */
1729 DEV_DEBUGCON, /* -debugcon */
1730 } type;
1731 const char *cmdline;
1732 QTAILQ_ENTRY(device_config) next;
1734 QTAILQ_HEAD(, device_config) device_configs = QTAILQ_HEAD_INITIALIZER(device_configs);
1736 static void add_device_config(int type, const char *cmdline)
1738 struct device_config *conf;
1740 conf = qemu_mallocz(sizeof(*conf));
1741 conf->type = type;
1742 conf->cmdline = cmdline;
1743 QTAILQ_INSERT_TAIL(&device_configs, conf, next);
1746 static int foreach_device_config(int type, int (*func)(const char *cmdline))
1748 struct device_config *conf;
1749 int rc;
1751 QTAILQ_FOREACH(conf, &device_configs, next) {
1752 if (conf->type != type)
1753 continue;
1754 rc = func(conf->cmdline);
1755 if (0 != rc)
1756 return rc;
1758 return 0;
1761 static int serial_parse(const char *devname)
1763 static int index = 0;
1764 char label[32];
1766 if (strcmp(devname, "none") == 0)
1767 return 0;
1768 if (index == MAX_SERIAL_PORTS) {
1769 fprintf(stderr, "qemu: too many serial ports\n");
1770 exit(1);
1772 snprintf(label, sizeof(label), "serial%d", index);
1773 serial_hds[index] = qemu_chr_open(label, devname, NULL);
1774 if (!serial_hds[index]) {
1775 fprintf(stderr, "qemu: could not open serial device '%s': %s\n",
1776 devname, strerror(errno));
1777 return -1;
1779 index++;
1780 return 0;
1783 static int parallel_parse(const char *devname)
1785 static int index = 0;
1786 char label[32];
1788 if (strcmp(devname, "none") == 0)
1789 return 0;
1790 if (index == MAX_PARALLEL_PORTS) {
1791 fprintf(stderr, "qemu: too many parallel ports\n");
1792 exit(1);
1794 snprintf(label, sizeof(label), "parallel%d", index);
1795 parallel_hds[index] = qemu_chr_open(label, devname, NULL);
1796 if (!parallel_hds[index]) {
1797 fprintf(stderr, "qemu: could not open parallel device '%s': %s\n",
1798 devname, strerror(errno));
1799 return -1;
1801 index++;
1802 return 0;
1805 static int virtcon_parse(const char *devname)
1807 QemuOptsList *device = qemu_find_opts("device");
1808 static int index = 0;
1809 char label[32];
1810 QemuOpts *bus_opts, *dev_opts;
1812 if (strcmp(devname, "none") == 0)
1813 return 0;
1814 if (index == MAX_VIRTIO_CONSOLES) {
1815 fprintf(stderr, "qemu: too many virtio consoles\n");
1816 exit(1);
1819 bus_opts = qemu_opts_create(device, NULL, 0);
1820 qemu_opt_set(bus_opts, "driver", "virtio-serial");
1822 dev_opts = qemu_opts_create(device, NULL, 0);
1823 qemu_opt_set(dev_opts, "driver", "virtconsole");
1825 snprintf(label, sizeof(label), "virtcon%d", index);
1826 virtcon_hds[index] = qemu_chr_open(label, devname, NULL);
1827 if (!virtcon_hds[index]) {
1828 fprintf(stderr, "qemu: could not open virtio console '%s': %s\n",
1829 devname, strerror(errno));
1830 return -1;
1832 qemu_opt_set(dev_opts, "chardev", label);
1834 index++;
1835 return 0;
1838 static int debugcon_parse(const char *devname)
1840 QemuOpts *opts;
1842 if (!qemu_chr_open("debugcon", devname, NULL)) {
1843 exit(1);
1845 opts = qemu_opts_create(qemu_find_opts("device"), "debugcon", 1);
1846 if (!opts) {
1847 fprintf(stderr, "qemu: already have a debugcon device\n");
1848 exit(1);
1850 qemu_opt_set(opts, "driver", "isa-debugcon");
1851 qemu_opt_set(opts, "chardev", "debugcon");
1852 return 0;
1855 void qemu_add_exit_notifier(Notifier *notify)
1857 notifier_list_add(&exit_notifiers, notify);
1860 void qemu_remove_exit_notifier(Notifier *notify)
1862 notifier_list_remove(&exit_notifiers, notify);
1865 static void qemu_run_exit_notifiers(void)
1867 notifier_list_notify(&exit_notifiers);
1870 void qemu_add_machine_init_done_notifier(Notifier *notify)
1872 notifier_list_add(&machine_init_done_notifiers, notify);
1875 static void qemu_run_machine_init_done_notifiers(void)
1877 notifier_list_notify(&machine_init_done_notifiers);
1880 static const QEMUOption *lookup_opt(int argc, char **argv,
1881 const char **poptarg, int *poptind)
1883 const QEMUOption *popt;
1884 int optind = *poptind;
1885 char *r = argv[optind];
1886 const char *optarg;
1888 loc_set_cmdline(argv, optind, 1);
1889 optind++;
1890 /* Treat --foo the same as -foo. */
1891 if (r[1] == '-')
1892 r++;
1893 popt = qemu_options;
1894 for(;;) {
1895 if (!popt->name) {
1896 error_report("invalid option");
1897 exit(1);
1899 if (!strcmp(popt->name, r + 1))
1900 break;
1901 popt++;
1903 if (popt->flags & HAS_ARG) {
1904 if (optind >= argc) {
1905 error_report("requires an argument");
1906 exit(1);
1908 optarg = argv[optind++];
1909 loc_set_cmdline(argv, optind - 2, 2);
1910 } else {
1911 optarg = NULL;
1914 *poptarg = optarg;
1915 *poptind = optind;
1917 return popt;
1920 int main(int argc, char **argv, char **envp)
1922 const char *gdbstub_dev = NULL;
1923 int i;
1924 int snapshot, linux_boot;
1925 const char *icount_option = NULL;
1926 const char *initrd_filename;
1927 const char *kernel_filename, *kernel_cmdline;
1928 char boot_devices[33] = "cad"; /* default to HD->floppy->CD-ROM */
1929 DisplayState *ds;
1930 DisplayChangeListener *dcl;
1931 int cyls, heads, secs, translation;
1932 QemuOpts *hda_opts = NULL, *opts;
1933 QemuOptsList *olist;
1934 int optind;
1935 const char *optarg;
1936 const char *loadvm = NULL;
1937 QEMUMachine *machine;
1938 const char *cpu_model;
1939 int tb_size;
1940 const char *pid_file = NULL;
1941 const char *incoming = NULL;
1942 int show_vnc_port = 0;
1943 int defconfig = 1;
1945 #ifdef CONFIG_SIMPLE_TRACE
1946 const char *trace_file = NULL;
1947 #endif
1948 atexit(qemu_run_exit_notifiers);
1949 error_set_progname(argv[0]);
1951 init_clocks();
1953 qemu_cache_utils_init(envp);
1955 QLIST_INIT (&vm_change_state_head);
1956 os_setup_early_signal_handling();
1958 module_call_init(MODULE_INIT_MACHINE);
1959 machine = find_default_machine();
1960 cpu_model = NULL;
1961 initrd_filename = NULL;
1962 ram_size = 0;
1963 snapshot = 0;
1964 kernel_filename = NULL;
1965 kernel_cmdline = "";
1966 cyls = heads = secs = 0;
1967 translation = BIOS_ATA_TRANSLATION_AUTO;
1969 for (i = 0; i < MAX_NODES; i++) {
1970 node_mem[i] = 0;
1971 node_cpumask[i] = 0;
1974 nb_numa_nodes = 0;
1975 nb_nics = 0;
1977 tb_size = 0;
1978 autostart= 1;
1980 /* first pass of option parsing */
1981 optind = 1;
1982 while (optind < argc) {
1983 if (argv[optind][0] != '-') {
1984 /* disk image */
1985 optind++;
1986 continue;
1987 } else {
1988 const QEMUOption *popt;
1990 popt = lookup_opt(argc, argv, &optarg, &optind);
1991 switch (popt->index) {
1992 case QEMU_OPTION_nodefconfig:
1993 defconfig=0;
1994 break;
1999 if (defconfig) {
2000 int ret;
2002 ret = qemu_read_config_file(CONFIG_QEMU_CONFDIR "/qemu.conf");
2003 if (ret < 0 && ret != -ENOENT) {
2004 exit(1);
2007 ret = qemu_read_config_file(arch_config_name);
2008 if (ret < 0 && ret != -ENOENT) {
2009 exit(1);
2012 cpudef_init();
2014 /* second pass of option parsing */
2015 optind = 1;
2016 for(;;) {
2017 if (optind >= argc)
2018 break;
2019 if (argv[optind][0] != '-') {
2020 hda_opts = drive_add(IF_DEFAULT, 0, argv[optind++], HD_OPTS);
2021 } else {
2022 const QEMUOption *popt;
2024 popt = lookup_opt(argc, argv, &optarg, &optind);
2025 if (!(popt->arch_mask & arch_type)) {
2026 printf("Option %s not supported for this target\n", popt->name);
2027 exit(1);
2029 switch(popt->index) {
2030 case QEMU_OPTION_M:
2031 machine = find_machine(optarg);
2032 if (!machine) {
2033 QEMUMachine *m;
2034 printf("Supported machines are:\n");
2035 for(m = first_machine; m != NULL; m = m->next) {
2036 if (m->alias)
2037 printf("%-10s %s (alias of %s)\n",
2038 m->alias, m->desc, m->name);
2039 printf("%-10s %s%s\n",
2040 m->name, m->desc,
2041 m->is_default ? " (default)" : "");
2043 exit(*optarg != '?');
2045 break;
2046 case QEMU_OPTION_cpu:
2047 /* hw initialization will check this */
2048 if (*optarg == '?') {
2049 list_cpus(stdout, &fprintf, optarg);
2050 exit(0);
2051 } else {
2052 cpu_model = optarg;
2054 break;
2055 case QEMU_OPTION_initrd:
2056 initrd_filename = optarg;
2057 break;
2058 case QEMU_OPTION_hda:
2060 char buf[256];
2061 if (cyls == 0)
2062 snprintf(buf, sizeof(buf), "%s", HD_OPTS);
2063 else
2064 snprintf(buf, sizeof(buf),
2065 "%s,cyls=%d,heads=%d,secs=%d%s",
2066 HD_OPTS , cyls, heads, secs,
2067 translation == BIOS_ATA_TRANSLATION_LBA ?
2068 ",trans=lba" :
2069 translation == BIOS_ATA_TRANSLATION_NONE ?
2070 ",trans=none" : "");
2071 drive_add(IF_DEFAULT, 0, optarg, buf);
2072 break;
2074 case QEMU_OPTION_hdb:
2075 case QEMU_OPTION_hdc:
2076 case QEMU_OPTION_hdd:
2077 drive_add(IF_DEFAULT, popt->index - QEMU_OPTION_hda, optarg,
2078 HD_OPTS);
2079 break;
2080 case QEMU_OPTION_drive:
2081 drive_def(optarg);
2082 break;
2083 case QEMU_OPTION_set:
2084 if (qemu_set_option(optarg) != 0)
2085 exit(1);
2086 break;
2087 case QEMU_OPTION_global:
2088 if (qemu_global_option(optarg) != 0)
2089 exit(1);
2090 break;
2091 case QEMU_OPTION_mtdblock:
2092 drive_add(IF_MTD, -1, optarg, MTD_OPTS);
2093 break;
2094 case QEMU_OPTION_sd:
2095 drive_add(IF_SD, 0, optarg, SD_OPTS);
2096 break;
2097 case QEMU_OPTION_pflash:
2098 drive_add(IF_PFLASH, -1, optarg, PFLASH_OPTS);
2099 break;
2100 case QEMU_OPTION_snapshot:
2101 snapshot = 1;
2102 break;
2103 case QEMU_OPTION_hdachs:
2105 const char *p;
2106 p = optarg;
2107 cyls = strtol(p, (char **)&p, 0);
2108 if (cyls < 1 || cyls > 16383)
2109 goto chs_fail;
2110 if (*p != ',')
2111 goto chs_fail;
2112 p++;
2113 heads = strtol(p, (char **)&p, 0);
2114 if (heads < 1 || heads > 16)
2115 goto chs_fail;
2116 if (*p != ',')
2117 goto chs_fail;
2118 p++;
2119 secs = strtol(p, (char **)&p, 0);
2120 if (secs < 1 || secs > 63)
2121 goto chs_fail;
2122 if (*p == ',') {
2123 p++;
2124 if (!strcmp(p, "none"))
2125 translation = BIOS_ATA_TRANSLATION_NONE;
2126 else if (!strcmp(p, "lba"))
2127 translation = BIOS_ATA_TRANSLATION_LBA;
2128 else if (!strcmp(p, "auto"))
2129 translation = BIOS_ATA_TRANSLATION_AUTO;
2130 else
2131 goto chs_fail;
2132 } else if (*p != '\0') {
2133 chs_fail:
2134 fprintf(stderr, "qemu: invalid physical CHS format\n");
2135 exit(1);
2137 if (hda_opts != NULL) {
2138 char num[16];
2139 snprintf(num, sizeof(num), "%d", cyls);
2140 qemu_opt_set(hda_opts, "cyls", num);
2141 snprintf(num, sizeof(num), "%d", heads);
2142 qemu_opt_set(hda_opts, "heads", num);
2143 snprintf(num, sizeof(num), "%d", secs);
2144 qemu_opt_set(hda_opts, "secs", num);
2145 if (translation == BIOS_ATA_TRANSLATION_LBA)
2146 qemu_opt_set(hda_opts, "trans", "lba");
2147 if (translation == BIOS_ATA_TRANSLATION_NONE)
2148 qemu_opt_set(hda_opts, "trans", "none");
2151 break;
2152 case QEMU_OPTION_numa:
2153 if (nb_numa_nodes >= MAX_NODES) {
2154 fprintf(stderr, "qemu: too many NUMA nodes\n");
2155 exit(1);
2157 numa_add(optarg);
2158 break;
2159 case QEMU_OPTION_nographic:
2160 display_type = DT_NOGRAPHIC;
2161 break;
2162 #ifdef CONFIG_CURSES
2163 case QEMU_OPTION_curses:
2164 display_type = DT_CURSES;
2165 break;
2166 #endif
2167 case QEMU_OPTION_portrait:
2168 graphic_rotate = 1;
2169 break;
2170 case QEMU_OPTION_kernel:
2171 kernel_filename = optarg;
2172 break;
2173 case QEMU_OPTION_append:
2174 kernel_cmdline = optarg;
2175 break;
2176 case QEMU_OPTION_cdrom:
2177 drive_add(IF_DEFAULT, 2, optarg, CDROM_OPTS);
2178 break;
2179 case QEMU_OPTION_boot:
2181 static const char * const params[] = {
2182 "order", "once", "menu", NULL
2184 char buf[sizeof(boot_devices)];
2185 char *standard_boot_devices;
2186 int legacy = 0;
2188 if (!strchr(optarg, '=')) {
2189 legacy = 1;
2190 pstrcpy(buf, sizeof(buf), optarg);
2191 } else if (check_params(buf, sizeof(buf), params, optarg) < 0) {
2192 fprintf(stderr,
2193 "qemu: unknown boot parameter '%s' in '%s'\n",
2194 buf, optarg);
2195 exit(1);
2198 if (legacy ||
2199 get_param_value(buf, sizeof(buf), "order", optarg)) {
2200 validate_bootdevices(buf);
2201 pstrcpy(boot_devices, sizeof(boot_devices), buf);
2203 if (!legacy) {
2204 if (get_param_value(buf, sizeof(buf),
2205 "once", optarg)) {
2206 validate_bootdevices(buf);
2207 standard_boot_devices = qemu_strdup(boot_devices);
2208 pstrcpy(boot_devices, sizeof(boot_devices), buf);
2209 qemu_register_reset(restore_boot_devices,
2210 standard_boot_devices);
2212 if (get_param_value(buf, sizeof(buf),
2213 "menu", optarg)) {
2214 if (!strcmp(buf, "on")) {
2215 boot_menu = 1;
2216 } else if (!strcmp(buf, "off")) {
2217 boot_menu = 0;
2218 } else {
2219 fprintf(stderr,
2220 "qemu: invalid option value '%s'\n",
2221 buf);
2222 exit(1);
2227 break;
2228 case QEMU_OPTION_fda:
2229 case QEMU_OPTION_fdb:
2230 drive_add(IF_FLOPPY, popt->index - QEMU_OPTION_fda,
2231 optarg, FD_OPTS);
2232 break;
2233 case QEMU_OPTION_no_fd_bootchk:
2234 fd_bootchk = 0;
2235 break;
2236 case QEMU_OPTION_netdev:
2237 if (net_client_parse(qemu_find_opts("netdev"), optarg) == -1) {
2238 exit(1);
2240 break;
2241 case QEMU_OPTION_net:
2242 if (net_client_parse(qemu_find_opts("net"), optarg) == -1) {
2243 exit(1);
2245 break;
2246 #ifdef CONFIG_SLIRP
2247 case QEMU_OPTION_tftp:
2248 legacy_tftp_prefix = optarg;
2249 break;
2250 case QEMU_OPTION_bootp:
2251 legacy_bootp_filename = optarg;
2252 break;
2253 case QEMU_OPTION_redir:
2254 if (net_slirp_redir(optarg) < 0)
2255 exit(1);
2256 break;
2257 #endif
2258 case QEMU_OPTION_bt:
2259 add_device_config(DEV_BT, optarg);
2260 break;
2261 case QEMU_OPTION_audio_help:
2262 if (!(audio_available())) {
2263 printf("Option %s not supported for this target\n", popt->name);
2264 exit(1);
2266 AUD_help ();
2267 exit (0);
2268 break;
2269 case QEMU_OPTION_soundhw:
2270 if (!(audio_available())) {
2271 printf("Option %s not supported for this target\n", popt->name);
2272 exit(1);
2274 select_soundhw (optarg);
2275 break;
2276 case QEMU_OPTION_h:
2277 help(0);
2278 break;
2279 case QEMU_OPTION_version:
2280 version();
2281 exit(0);
2282 break;
2283 case QEMU_OPTION_m: {
2284 int64_t value;
2286 value = strtosz(optarg, NULL);
2287 if (value < 0) {
2288 fprintf(stderr, "qemu: invalid ram size: %s\n", optarg);
2289 exit(1);
2292 /* On 32-bit hosts, QEMU is limited by virtual address space */
2293 if (value > (2047 << 20) && HOST_LONG_BITS == 32) {
2294 fprintf(stderr, "qemu: at most 2047 MB RAM can be simulated\n");
2295 exit(1);
2297 if (value != (uint64_t)(ram_addr_t)value) {
2298 fprintf(stderr, "qemu: ram size too large\n");
2299 exit(1);
2301 ram_size = value;
2302 break;
2304 case QEMU_OPTION_mempath:
2305 mem_path = optarg;
2306 break;
2307 #ifdef MAP_POPULATE
2308 case QEMU_OPTION_mem_prealloc:
2309 mem_prealloc = 1;
2310 break;
2311 #endif
2312 case QEMU_OPTION_d:
2313 set_cpu_log(optarg);
2314 break;
2315 case QEMU_OPTION_s:
2316 gdbstub_dev = "tcp::" DEFAULT_GDBSTUB_PORT;
2317 break;
2318 case QEMU_OPTION_gdb:
2319 gdbstub_dev = optarg;
2320 break;
2321 case QEMU_OPTION_L:
2322 data_dir = optarg;
2323 break;
2324 case QEMU_OPTION_bios:
2325 bios_name = optarg;
2326 break;
2327 case QEMU_OPTION_singlestep:
2328 singlestep = 1;
2329 break;
2330 case QEMU_OPTION_S:
2331 autostart = 0;
2332 break;
2333 case QEMU_OPTION_k:
2334 keyboard_layout = optarg;
2335 break;
2336 case QEMU_OPTION_localtime:
2337 rtc_utc = 0;
2338 break;
2339 case QEMU_OPTION_vga:
2340 select_vgahw (optarg);
2341 break;
2342 case QEMU_OPTION_g:
2344 const char *p;
2345 int w, h, depth;
2346 p = optarg;
2347 w = strtol(p, (char **)&p, 10);
2348 if (w <= 0) {
2349 graphic_error:
2350 fprintf(stderr, "qemu: invalid resolution or depth\n");
2351 exit(1);
2353 if (*p != 'x')
2354 goto graphic_error;
2355 p++;
2356 h = strtol(p, (char **)&p, 10);
2357 if (h <= 0)
2358 goto graphic_error;
2359 if (*p == 'x') {
2360 p++;
2361 depth = strtol(p, (char **)&p, 10);
2362 if (depth != 8 && depth != 15 && depth != 16 &&
2363 depth != 24 && depth != 32)
2364 goto graphic_error;
2365 } else if (*p == '\0') {
2366 depth = graphic_depth;
2367 } else {
2368 goto graphic_error;
2371 graphic_width = w;
2372 graphic_height = h;
2373 graphic_depth = depth;
2375 break;
2376 case QEMU_OPTION_echr:
2378 char *r;
2379 term_escape_char = strtol(optarg, &r, 0);
2380 if (r == optarg)
2381 printf("Bad argument to echr\n");
2382 break;
2384 case QEMU_OPTION_monitor:
2385 monitor_parse(optarg, "readline");
2386 default_monitor = 0;
2387 break;
2388 case QEMU_OPTION_qmp:
2389 monitor_parse(optarg, "control");
2390 default_monitor = 0;
2391 break;
2392 case QEMU_OPTION_mon:
2393 opts = qemu_opts_parse(qemu_find_opts("mon"), optarg, 1);
2394 if (!opts) {
2395 exit(1);
2397 default_monitor = 0;
2398 break;
2399 case QEMU_OPTION_chardev:
2400 opts = qemu_opts_parse(qemu_find_opts("chardev"), optarg, 1);
2401 if (!opts) {
2402 exit(1);
2404 break;
2405 case QEMU_OPTION_fsdev:
2406 olist = qemu_find_opts("fsdev");
2407 if (!olist) {
2408 fprintf(stderr, "fsdev is not supported by this qemu build.\n");
2409 exit(1);
2411 opts = qemu_opts_parse(olist, optarg, 1);
2412 if (!opts) {
2413 fprintf(stderr, "parse error: %s\n", optarg);
2414 exit(1);
2416 break;
2417 case QEMU_OPTION_virtfs: {
2418 char *arg_fsdev = NULL;
2419 char *arg_9p = NULL;
2420 int len = 0;
2422 olist = qemu_find_opts("virtfs");
2423 if (!olist) {
2424 fprintf(stderr, "virtfs is not supported by this qemu build.\n");
2425 exit(1);
2427 opts = qemu_opts_parse(olist, optarg, 1);
2428 if (!opts) {
2429 fprintf(stderr, "parse error: %s\n", optarg);
2430 exit(1);
2433 if (qemu_opt_get(opts, "fstype") == NULL ||
2434 qemu_opt_get(opts, "mount_tag") == NULL ||
2435 qemu_opt_get(opts, "path") == NULL ||
2436 qemu_opt_get(opts, "security_model") == NULL) {
2437 fprintf(stderr, "Usage: -virtfs fstype,path=/share_path/,"
2438 "security_model=[mapped|passthrough|none],"
2439 "mnt_tag=tag.\n");
2440 exit(1);
2443 len = strlen(",id=,path=,security_model=");
2444 len += strlen(qemu_opt_get(opts, "fstype"));
2445 len += strlen(qemu_opt_get(opts, "mount_tag"));
2446 len += strlen(qemu_opt_get(opts, "path"));
2447 len += strlen(qemu_opt_get(opts, "security_model"));
2448 arg_fsdev = qemu_malloc((len + 1) * sizeof(*arg_fsdev));
2450 snprintf(arg_fsdev, (len + 1) * sizeof(*arg_fsdev),
2451 "%s,id=%s,path=%s,security_model=%s",
2452 qemu_opt_get(opts, "fstype"),
2453 qemu_opt_get(opts, "mount_tag"),
2454 qemu_opt_get(opts, "path"),
2455 qemu_opt_get(opts, "security_model"));
2457 len = strlen("virtio-9p-pci,fsdev=,mount_tag=");
2458 len += 2*strlen(qemu_opt_get(opts, "mount_tag"));
2459 arg_9p = qemu_malloc((len + 1) * sizeof(*arg_9p));
2461 snprintf(arg_9p, (len + 1) * sizeof(*arg_9p),
2462 "virtio-9p-pci,fsdev=%s,mount_tag=%s",
2463 qemu_opt_get(opts, "mount_tag"),
2464 qemu_opt_get(opts, "mount_tag"));
2466 if (!qemu_opts_parse(qemu_find_opts("fsdev"), arg_fsdev, 1)) {
2467 fprintf(stderr, "parse error [fsdev]: %s\n", optarg);
2468 exit(1);
2471 if (!qemu_opts_parse(qemu_find_opts("device"), arg_9p, 1)) {
2472 fprintf(stderr, "parse error [device]: %s\n", optarg);
2473 exit(1);
2476 qemu_free(arg_fsdev);
2477 qemu_free(arg_9p);
2478 break;
2480 case QEMU_OPTION_serial:
2481 add_device_config(DEV_SERIAL, optarg);
2482 default_serial = 0;
2483 if (strncmp(optarg, "mon:", 4) == 0) {
2484 default_monitor = 0;
2486 break;
2487 case QEMU_OPTION_watchdog:
2488 if (watchdog) {
2489 fprintf(stderr,
2490 "qemu: only one watchdog option may be given\n");
2491 return 1;
2493 watchdog = optarg;
2494 break;
2495 case QEMU_OPTION_watchdog_action:
2496 if (select_watchdog_action(optarg) == -1) {
2497 fprintf(stderr, "Unknown -watchdog-action parameter\n");
2498 exit(1);
2500 break;
2501 case QEMU_OPTION_virtiocon:
2502 add_device_config(DEV_VIRTCON, optarg);
2503 default_virtcon = 0;
2504 if (strncmp(optarg, "mon:", 4) == 0) {
2505 default_monitor = 0;
2507 break;
2508 case QEMU_OPTION_parallel:
2509 add_device_config(DEV_PARALLEL, optarg);
2510 default_parallel = 0;
2511 if (strncmp(optarg, "mon:", 4) == 0) {
2512 default_monitor = 0;
2514 break;
2515 case QEMU_OPTION_debugcon:
2516 add_device_config(DEV_DEBUGCON, optarg);
2517 break;
2518 case QEMU_OPTION_loadvm:
2519 loadvm = optarg;
2520 break;
2521 case QEMU_OPTION_full_screen:
2522 full_screen = 1;
2523 break;
2524 #ifdef CONFIG_SDL
2525 case QEMU_OPTION_no_frame:
2526 no_frame = 1;
2527 break;
2528 case QEMU_OPTION_alt_grab:
2529 alt_grab = 1;
2530 break;
2531 case QEMU_OPTION_ctrl_grab:
2532 ctrl_grab = 1;
2533 break;
2534 case QEMU_OPTION_no_quit:
2535 no_quit = 1;
2536 break;
2537 case QEMU_OPTION_sdl:
2538 display_type = DT_SDL;
2539 break;
2540 #endif
2541 case QEMU_OPTION_pidfile:
2542 pid_file = optarg;
2543 break;
2544 case QEMU_OPTION_win2k_hack:
2545 win2k_install_hack = 1;
2546 break;
2547 case QEMU_OPTION_rtc_td_hack:
2548 rtc_td_hack = 1;
2549 break;
2550 case QEMU_OPTION_acpitable:
2551 do_acpitable_option(optarg);
2552 break;
2553 case QEMU_OPTION_smbios:
2554 do_smbios_option(optarg);
2555 break;
2556 case QEMU_OPTION_enable_kvm:
2557 kvm_allowed = 1;
2558 break;
2559 case QEMU_OPTION_usb:
2560 usb_enabled = 1;
2561 break;
2562 case QEMU_OPTION_usbdevice:
2563 usb_enabled = 1;
2564 add_device_config(DEV_USB, optarg);
2565 break;
2566 case QEMU_OPTION_device:
2567 if (!qemu_opts_parse(qemu_find_opts("device"), optarg, 1)) {
2568 exit(1);
2570 break;
2571 case QEMU_OPTION_smp:
2572 smp_parse(optarg);
2573 if (smp_cpus < 1) {
2574 fprintf(stderr, "Invalid number of CPUs\n");
2575 exit(1);
2577 if (max_cpus < smp_cpus) {
2578 fprintf(stderr, "maxcpus must be equal to or greater than "
2579 "smp\n");
2580 exit(1);
2582 if (max_cpus > 255) {
2583 fprintf(stderr, "Unsupported number of maxcpus\n");
2584 exit(1);
2586 break;
2587 case QEMU_OPTION_vnc:
2588 display_remote++;
2589 vnc_display = optarg;
2590 break;
2591 case QEMU_OPTION_no_acpi:
2592 acpi_enabled = 0;
2593 break;
2594 case QEMU_OPTION_no_hpet:
2595 no_hpet = 1;
2596 break;
2597 case QEMU_OPTION_balloon:
2598 if (balloon_parse(optarg) < 0) {
2599 fprintf(stderr, "Unknown -balloon argument %s\n", optarg);
2600 exit(1);
2602 break;
2603 case QEMU_OPTION_no_reboot:
2604 no_reboot = 1;
2605 break;
2606 case QEMU_OPTION_no_shutdown:
2607 no_shutdown = 1;
2608 break;
2609 case QEMU_OPTION_show_cursor:
2610 cursor_hide = 0;
2611 break;
2612 case QEMU_OPTION_uuid:
2613 if(qemu_uuid_parse(optarg, qemu_uuid) < 0) {
2614 fprintf(stderr, "Fail to parse UUID string."
2615 " Wrong format.\n");
2616 exit(1);
2618 break;
2619 case QEMU_OPTION_option_rom:
2620 if (nb_option_roms >= MAX_OPTION_ROMS) {
2621 fprintf(stderr, "Too many option ROMs\n");
2622 exit(1);
2624 opts = qemu_opts_parse(qemu_find_opts("option-rom"), optarg, 1);
2625 option_rom[nb_option_roms].name = qemu_opt_get(opts, "romfile");
2626 option_rom[nb_option_roms].bootindex =
2627 qemu_opt_get_number(opts, "bootindex", -1);
2628 if (!option_rom[nb_option_roms].name) {
2629 fprintf(stderr, "Option ROM file is not specified\n");
2630 exit(1);
2632 nb_option_roms++;
2633 break;
2634 case QEMU_OPTION_semihosting:
2635 semihosting_enabled = 1;
2636 break;
2637 case QEMU_OPTION_name:
2638 qemu_name = qemu_strdup(optarg);
2640 char *p = strchr(qemu_name, ',');
2641 if (p != NULL) {
2642 *p++ = 0;
2643 if (strncmp(p, "process=", 8)) {
2644 fprintf(stderr, "Unknown subargument %s to -name\n", p);
2645 exit(1);
2647 p += 8;
2648 os_set_proc_name(p);
2651 break;
2652 case QEMU_OPTION_prom_env:
2653 if (nb_prom_envs >= MAX_PROM_ENVS) {
2654 fprintf(stderr, "Too many prom variables\n");
2655 exit(1);
2657 prom_envs[nb_prom_envs] = optarg;
2658 nb_prom_envs++;
2659 break;
2660 case QEMU_OPTION_old_param:
2661 old_param = 1;
2662 break;
2663 case QEMU_OPTION_clock:
2664 configure_alarms(optarg);
2665 break;
2666 case QEMU_OPTION_startdate:
2667 configure_rtc_date_offset(optarg, 1);
2668 break;
2669 case QEMU_OPTION_rtc:
2670 opts = qemu_opts_parse(qemu_find_opts("rtc"), optarg, 0);
2671 if (!opts) {
2672 exit(1);
2674 configure_rtc(opts);
2675 break;
2676 case QEMU_OPTION_tb_size:
2677 tb_size = strtol(optarg, NULL, 0);
2678 if (tb_size < 0)
2679 tb_size = 0;
2680 break;
2681 case QEMU_OPTION_icount:
2682 icount_option = optarg;
2683 break;
2684 case QEMU_OPTION_incoming:
2685 incoming = optarg;
2686 incoming_expected = true;
2687 break;
2688 case QEMU_OPTION_nodefaults:
2689 default_serial = 0;
2690 default_parallel = 0;
2691 default_virtcon = 0;
2692 default_monitor = 0;
2693 default_vga = 0;
2694 default_net = 0;
2695 default_floppy = 0;
2696 default_cdrom = 0;
2697 default_sdcard = 0;
2698 break;
2699 case QEMU_OPTION_xen_domid:
2700 if (!(xen_available())) {
2701 printf("Option %s not supported for this target\n", popt->name);
2702 exit(1);
2704 xen_domid = atoi(optarg);
2705 break;
2706 case QEMU_OPTION_xen_create:
2707 if (!(xen_available())) {
2708 printf("Option %s not supported for this target\n", popt->name);
2709 exit(1);
2711 xen_mode = XEN_CREATE;
2712 break;
2713 case QEMU_OPTION_xen_attach:
2714 if (!(xen_available())) {
2715 printf("Option %s not supported for this target\n", popt->name);
2716 exit(1);
2718 xen_mode = XEN_ATTACH;
2719 break;
2720 #ifdef CONFIG_SIMPLE_TRACE
2721 case QEMU_OPTION_trace:
2722 opts = qemu_opts_parse(qemu_find_opts("trace"), optarg, 0);
2723 if (opts) {
2724 trace_file = qemu_opt_get(opts, "file");
2726 break;
2727 #endif
2728 case QEMU_OPTION_readconfig:
2730 int ret = qemu_read_config_file(optarg);
2731 if (ret < 0) {
2732 fprintf(stderr, "read config %s: %s\n", optarg,
2733 strerror(-ret));
2734 exit(1);
2736 break;
2738 case QEMU_OPTION_spice:
2739 olist = qemu_find_opts("spice");
2740 if (!olist) {
2741 fprintf(stderr, "spice is not supported by this qemu build.\n");
2742 exit(1);
2744 opts = qemu_opts_parse(olist, optarg, 0);
2745 if (!opts) {
2746 fprintf(stderr, "parse error: %s\n", optarg);
2747 exit(1);
2749 break;
2750 case QEMU_OPTION_writeconfig:
2752 FILE *fp;
2753 if (strcmp(optarg, "-") == 0) {
2754 fp = stdout;
2755 } else {
2756 fp = fopen(optarg, "w");
2757 if (fp == NULL) {
2758 fprintf(stderr, "open %s: %s\n", optarg, strerror(errno));
2759 exit(1);
2762 qemu_config_write(fp);
2763 fclose(fp);
2764 break;
2766 default:
2767 os_parse_cmd_args(popt->index, optarg);
2771 loc_set_none();
2773 /* If no data_dir is specified then try to find it relative to the
2774 executable path. */
2775 if (!data_dir) {
2776 data_dir = os_find_datadir(argv[0]);
2778 /* If all else fails use the install patch specified when building. */
2779 if (!data_dir) {
2780 data_dir = CONFIG_QEMU_DATADIR;
2783 #ifdef CONFIG_SIMPLE_TRACE
2785 * Set the trace file name, if specified.
2787 st_set_trace_file(trace_file);
2788 #endif
2790 * Default to max_cpus = smp_cpus, in case the user doesn't
2791 * specify a max_cpus value.
2793 if (!max_cpus)
2794 max_cpus = smp_cpus;
2796 machine->max_cpus = machine->max_cpus ?: 1; /* Default to UP */
2797 if (smp_cpus > machine->max_cpus) {
2798 fprintf(stderr, "Number of SMP cpus requested (%d), exceeds max cpus "
2799 "supported by machine `%s' (%d)\n", smp_cpus, machine->name,
2800 machine->max_cpus);
2801 exit(1);
2804 qemu_opts_foreach(qemu_find_opts("device"), default_driver_check, NULL, 0);
2805 qemu_opts_foreach(qemu_find_opts("global"), default_driver_check, NULL, 0);
2807 if (machine->no_serial) {
2808 default_serial = 0;
2810 if (machine->no_parallel) {
2811 default_parallel = 0;
2813 if (!machine->use_virtcon) {
2814 default_virtcon = 0;
2816 if (machine->no_vga) {
2817 default_vga = 0;
2819 if (machine->no_floppy) {
2820 default_floppy = 0;
2822 if (machine->no_cdrom) {
2823 default_cdrom = 0;
2825 if (machine->no_sdcard) {
2826 default_sdcard = 0;
2829 if (display_type == DT_NOGRAPHIC) {
2830 if (default_parallel)
2831 add_device_config(DEV_PARALLEL, "null");
2832 if (default_serial && default_monitor) {
2833 add_device_config(DEV_SERIAL, "mon:stdio");
2834 } else if (default_virtcon && default_monitor) {
2835 add_device_config(DEV_VIRTCON, "mon:stdio");
2836 } else {
2837 if (default_serial)
2838 add_device_config(DEV_SERIAL, "stdio");
2839 if (default_virtcon)
2840 add_device_config(DEV_VIRTCON, "stdio");
2841 if (default_monitor)
2842 monitor_parse("stdio", "readline");
2844 } else {
2845 if (default_serial)
2846 add_device_config(DEV_SERIAL, "vc:80Cx24C");
2847 if (default_parallel)
2848 add_device_config(DEV_PARALLEL, "vc:80Cx24C");
2849 if (default_monitor)
2850 monitor_parse("vc:80Cx24C", "readline");
2851 if (default_virtcon)
2852 add_device_config(DEV_VIRTCON, "vc:80Cx24C");
2854 if (default_vga)
2855 vga_interface_type = VGA_CIRRUS;
2857 socket_init();
2859 if (qemu_opts_foreach(qemu_find_opts("chardev"), chardev_init_func, NULL, 1) != 0)
2860 exit(1);
2861 #ifdef CONFIG_VIRTFS
2862 if (qemu_opts_foreach(qemu_find_opts("fsdev"), fsdev_init_func, NULL, 1) != 0) {
2863 exit(1);
2865 #endif
2867 os_daemonize();
2869 if (pid_file && qemu_create_pidfile(pid_file) != 0) {
2870 os_pidfile_error();
2871 exit(1);
2874 if (kvm_allowed) {
2875 int ret = kvm_init();
2876 if (ret < 0) {
2877 if (!kvm_available()) {
2878 printf("KVM not supported for this target\n");
2879 } else {
2880 fprintf(stderr, "failed to initialize KVM: %s\n", strerror(-ret));
2882 exit(1);
2886 if (qemu_init_main_loop()) {
2887 fprintf(stderr, "qemu_init_main_loop failed\n");
2888 exit(1);
2890 linux_boot = (kernel_filename != NULL);
2892 if (!linux_boot && *kernel_cmdline != '\0') {
2893 fprintf(stderr, "-append only allowed with -kernel option\n");
2894 exit(1);
2897 if (!linux_boot && initrd_filename != NULL) {
2898 fprintf(stderr, "-initrd only allowed with -kernel option\n");
2899 exit(1);
2902 os_set_line_buffering();
2904 if (init_timer_alarm() < 0) {
2905 fprintf(stderr, "could not initialize alarm timer\n");
2906 exit(1);
2908 configure_icount(icount_option);
2910 if (net_init_clients() < 0) {
2911 exit(1);
2914 /* init the bluetooth world */
2915 if (foreach_device_config(DEV_BT, bt_parse))
2916 exit(1);
2918 /* init the memory */
2919 if (ram_size == 0)
2920 ram_size = DEFAULT_RAM_SIZE * 1024 * 1024;
2922 /* init the dynamic translator */
2923 cpu_exec_init_all(tb_size * 1024 * 1024);
2925 bdrv_init_with_whitelist();
2927 blk_mig_init();
2929 /* open the virtual block devices */
2930 if (snapshot)
2931 qemu_opts_foreach(qemu_find_opts("drive"), drive_enable_snapshot, NULL, 0);
2932 if (qemu_opts_foreach(qemu_find_opts("drive"), drive_init_func, &machine->use_scsi, 1) != 0)
2933 exit(1);
2935 default_drive(default_cdrom, snapshot, machine->use_scsi,
2936 IF_DEFAULT, 2, CDROM_OPTS);
2937 default_drive(default_floppy, snapshot, machine->use_scsi,
2938 IF_FLOPPY, 0, FD_OPTS);
2939 default_drive(default_sdcard, snapshot, machine->use_scsi,
2940 IF_SD, 0, SD_OPTS);
2942 register_savevm_live(NULL, "ram", 0, 4, NULL, ram_save_live, NULL,
2943 ram_load, NULL);
2945 if (nb_numa_nodes > 0) {
2946 int i;
2948 if (nb_numa_nodes > smp_cpus) {
2949 nb_numa_nodes = smp_cpus;
2952 /* If no memory size if given for any node, assume the default case
2953 * and distribute the available memory equally across all nodes
2955 for (i = 0; i < nb_numa_nodes; i++) {
2956 if (node_mem[i] != 0)
2957 break;
2959 if (i == nb_numa_nodes) {
2960 uint64_t usedmem = 0;
2962 /* On Linux, the each node's border has to be 8MB aligned,
2963 * the final node gets the rest.
2965 for (i = 0; i < nb_numa_nodes - 1; i++) {
2966 node_mem[i] = (ram_size / nb_numa_nodes) & ~((1 << 23UL) - 1);
2967 usedmem += node_mem[i];
2969 node_mem[i] = ram_size - usedmem;
2972 for (i = 0; i < nb_numa_nodes; i++) {
2973 if (node_cpumask[i] != 0)
2974 break;
2976 /* assigning the VCPUs round-robin is easier to implement, guest OSes
2977 * must cope with this anyway, because there are BIOSes out there in
2978 * real machines which also use this scheme.
2980 if (i == nb_numa_nodes) {
2981 for (i = 0; i < smp_cpus; i++) {
2982 node_cpumask[i % nb_numa_nodes] |= 1 << i;
2987 if (qemu_opts_foreach(qemu_find_opts("mon"), mon_init_func, NULL, 1) != 0) {
2988 exit(1);
2991 if (foreach_device_config(DEV_SERIAL, serial_parse) < 0)
2992 exit(1);
2993 if (foreach_device_config(DEV_PARALLEL, parallel_parse) < 0)
2994 exit(1);
2995 if (foreach_device_config(DEV_VIRTCON, virtcon_parse) < 0)
2996 exit(1);
2997 if (foreach_device_config(DEV_DEBUGCON, debugcon_parse) < 0)
2998 exit(1);
3000 module_call_init(MODULE_INIT_DEVICE);
3002 if (qemu_opts_foreach(qemu_find_opts("device"), device_help_func, NULL, 0) != 0)
3003 exit(0);
3005 if (watchdog) {
3006 i = select_watchdog(watchdog);
3007 if (i > 0)
3008 exit (i == 1 ? 1 : 0);
3011 if (machine->compat_props) {
3012 qdev_prop_register_global_list(machine->compat_props);
3014 qemu_add_globals();
3016 machine->init(ram_size, boot_devices,
3017 kernel_filename, kernel_cmdline, initrd_filename, cpu_model);
3019 cpu_synchronize_all_post_init();
3021 /* must be after terminal init, SDL library changes signal handlers */
3022 os_setup_signal_handling();
3024 set_numa_modes();
3026 current_machine = machine;
3028 /* init USB devices */
3029 if (usb_enabled) {
3030 if (foreach_device_config(DEV_USB, usb_parse) < 0)
3031 exit(1);
3034 /* init generic devices */
3035 if (qemu_opts_foreach(qemu_find_opts("device"), device_init_func, NULL, 1) != 0)
3036 exit(1);
3038 net_check_clients();
3040 /* just use the first displaystate for the moment */
3041 ds = get_displaystate();
3043 if (using_spice)
3044 display_remote++;
3045 if (display_type == DT_DEFAULT && !display_remote) {
3046 #if defined(CONFIG_SDL) || defined(CONFIG_COCOA)
3047 display_type = DT_SDL;
3048 #else
3049 vnc_display = "localhost:0,to=99";
3050 show_vnc_port = 1;
3051 #endif
3055 /* init local displays */
3056 switch (display_type) {
3057 case DT_NOGRAPHIC:
3058 break;
3059 #if defined(CONFIG_CURSES)
3060 case DT_CURSES:
3061 curses_display_init(ds, full_screen);
3062 break;
3063 #endif
3064 #if defined(CONFIG_SDL)
3065 case DT_SDL:
3066 sdl_display_init(ds, full_screen, no_frame);
3067 break;
3068 #elif defined(CONFIG_COCOA)
3069 case DT_SDL:
3070 cocoa_display_init(ds, full_screen);
3071 break;
3072 #endif
3073 default:
3074 break;
3077 /* init remote displays */
3078 if (vnc_display) {
3079 vnc_display_init(ds);
3080 if (vnc_display_open(ds, vnc_display) < 0)
3081 exit(1);
3083 if (show_vnc_port) {
3084 printf("VNC server running on `%s'\n", vnc_display_local_addr(ds));
3087 #ifdef CONFIG_SPICE
3088 if (using_spice && !qxl_enabled) {
3089 qemu_spice_display_init(ds);
3091 #endif
3093 /* display setup */
3094 dpy_resize(ds);
3095 dcl = ds->listeners;
3096 while (dcl != NULL) {
3097 if (dcl->dpy_refresh != NULL) {
3098 ds->gui_timer = qemu_new_timer(rt_clock, gui_update, ds);
3099 qemu_mod_timer(ds->gui_timer, qemu_get_clock(rt_clock));
3100 break;
3102 dcl = dcl->next;
3104 if (ds->gui_timer == NULL) {
3105 nographic_timer = qemu_new_timer(rt_clock, nographic_update, NULL);
3106 qemu_mod_timer(nographic_timer, qemu_get_clock(rt_clock));
3108 text_consoles_set_display(ds);
3110 if (gdbstub_dev && gdbserver_start(gdbstub_dev) < 0) {
3111 fprintf(stderr, "qemu: could not open gdbserver on device '%s'\n",
3112 gdbstub_dev);
3113 exit(1);
3116 qdev_machine_creation_done();
3118 if (rom_load_all() != 0) {
3119 fprintf(stderr, "rom loading failed\n");
3120 exit(1);
3123 /* TODO: once all bus devices are qdevified, this should be done
3124 * when bus is created by qdev.c */
3125 qemu_register_reset(qbus_reset_all_fn, sysbus_get_default());
3126 qemu_run_machine_init_done_notifiers();
3128 qemu_system_reset();
3129 if (loadvm) {
3130 if (load_vmstate(loadvm) < 0) {
3131 autostart = 0;
3135 if (incoming) {
3136 int ret = qemu_start_incoming_migration(incoming);
3137 if (ret < 0) {
3138 fprintf(stderr, "Migration failed. Exit code %s(%d), exiting.\n",
3139 incoming, ret);
3140 exit(ret);
3142 } else if (autostart) {
3143 vm_start();
3146 os_setup_post();
3148 main_loop();
3149 quit_timers();
3150 net_cleanup();
3152 return 0;