ioeventfd: error handling cleanup
[qemu/agraf.git] / vl.c
blob029218427318a01bd4d02639093f5a9bd5a3848c
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_ALIAS "index=%d,media=disk"
625 #define CDROM_ALIAS "index=2,media=cdrom"
626 #define FD_ALIAS "index=%d,if=floppy"
627 #define PFLASH_ALIAS "if=pflash"
628 #define MTD_ALIAS "if=mtd"
629 #define SD_ALIAS "index=0,if=sd"
631 static int drive_init_func(QemuOpts *opts, void *opaque)
633 int *use_scsi = opaque;
634 int fatal_error = 0;
636 if (drive_init(opts, *use_scsi, &fatal_error) == NULL) {
637 if (fatal_error)
638 return 1;
640 return 0;
643 static int drive_enable_snapshot(QemuOpts *opts, void *opaque)
645 if (NULL == qemu_opt_get(opts, "snapshot")) {
646 qemu_opt_set(opts, "snapshot", "on");
648 return 0;
651 void qemu_register_boot_set(QEMUBootSetHandler *func, void *opaque)
653 boot_set_handler = func;
654 boot_set_opaque = opaque;
657 int qemu_boot_set(const char *boot_devices)
659 if (!boot_set_handler) {
660 return -EINVAL;
662 return boot_set_handler(boot_set_opaque, boot_devices);
665 static void validate_bootdevices(char *devices)
667 /* We just do some generic consistency checks */
668 const char *p;
669 int bitmap = 0;
671 for (p = devices; *p != '\0'; p++) {
672 /* Allowed boot devices are:
673 * a-b: floppy disk drives
674 * c-f: IDE disk drives
675 * g-m: machine implementation dependant drives
676 * n-p: network devices
677 * It's up to each machine implementation to check if the given boot
678 * devices match the actual hardware implementation and firmware
679 * features.
681 if (*p < 'a' || *p > 'p') {
682 fprintf(stderr, "Invalid boot device '%c'\n", *p);
683 exit(1);
685 if (bitmap & (1 << (*p - 'a'))) {
686 fprintf(stderr, "Boot device '%c' was given twice\n", *p);
687 exit(1);
689 bitmap |= 1 << (*p - 'a');
693 static void restore_boot_devices(void *opaque)
695 char *standard_boot_devices = opaque;
696 static int first = 1;
698 /* Restore boot order and remove ourselves after the first boot */
699 if (first) {
700 first = 0;
701 return;
704 qemu_boot_set(standard_boot_devices);
706 qemu_unregister_reset(restore_boot_devices, standard_boot_devices);
707 qemu_free(standard_boot_devices);
710 void add_boot_device_path(int32_t bootindex, DeviceState *dev,
711 const char *suffix)
713 FWBootEntry *node, *i;
715 if (bootindex < 0) {
716 return;
719 assert(dev != NULL || suffix != NULL);
721 node = qemu_mallocz(sizeof(FWBootEntry));
722 node->bootindex = bootindex;
723 node->suffix = strdup(suffix);
724 node->dev = dev;
726 QTAILQ_FOREACH(i, &fw_boot_order, link) {
727 if (i->bootindex == bootindex) {
728 fprintf(stderr, "Two devices with same boot index %d\n", bootindex);
729 exit(1);
730 } else if (i->bootindex < bootindex) {
731 continue;
733 QTAILQ_INSERT_BEFORE(i, node, link);
734 return;
736 QTAILQ_INSERT_TAIL(&fw_boot_order, node, link);
740 * This function returns null terminated string that consist of new line
741 * separated device pathes.
743 * memory pointed by "size" is assigned total length of the array in bytes
746 char *get_boot_devices_list(uint32_t *size)
748 FWBootEntry *i;
749 uint32_t total = 0;
750 char *list = NULL;
752 QTAILQ_FOREACH(i, &fw_boot_order, link) {
753 char *devpath = NULL, *bootpath;
754 int len;
756 if (i->dev) {
757 devpath = qdev_get_fw_dev_path(i->dev);
758 assert(devpath);
761 if (i->suffix && devpath) {
762 size_t bootpathlen = strlen(devpath) + strlen(i->suffix) + 1;
764 bootpath = qemu_malloc(bootpathlen);
765 snprintf(bootpath, bootpathlen, "%s%s", devpath, i->suffix);
766 qemu_free(devpath);
767 } else if (devpath) {
768 bootpath = devpath;
769 } else {
770 bootpath = strdup(i->suffix);
771 assert(bootpath);
774 if (total) {
775 list[total-1] = '\n';
777 len = strlen(bootpath) + 1;
778 list = qemu_realloc(list, total + len);
779 memcpy(&list[total], bootpath, len);
780 total += len;
781 qemu_free(bootpath);
784 *size = total;
786 return list;
789 static void numa_add(const char *optarg)
791 char option[128];
792 char *endptr;
793 unsigned long long value, endvalue;
794 int nodenr;
796 optarg = get_opt_name(option, 128, optarg, ',') + 1;
797 if (!strcmp(option, "node")) {
798 if (get_param_value(option, 128, "nodeid", optarg) == 0) {
799 nodenr = nb_numa_nodes;
800 } else {
801 nodenr = strtoull(option, NULL, 10);
804 if (get_param_value(option, 128, "mem", optarg) == 0) {
805 node_mem[nodenr] = 0;
806 } else {
807 ssize_t sval;
808 sval = strtosz(option, NULL);
809 if (sval < 0) {
810 fprintf(stderr, "qemu: invalid numa mem size: %s\n", optarg);
811 exit(1);
813 node_mem[nodenr] = sval;
815 if (get_param_value(option, 128, "cpus", optarg) == 0) {
816 node_cpumask[nodenr] = 0;
817 } else {
818 value = strtoull(option, &endptr, 10);
819 if (value >= 64) {
820 value = 63;
821 fprintf(stderr, "only 64 CPUs in NUMA mode supported.\n");
822 } else {
823 if (*endptr == '-') {
824 endvalue = strtoull(endptr+1, &endptr, 10);
825 if (endvalue >= 63) {
826 endvalue = 62;
827 fprintf(stderr,
828 "only 63 CPUs in NUMA mode supported.\n");
830 value = (2ULL << endvalue) - (1ULL << value);
831 } else {
832 value = 1ULL << value;
835 node_cpumask[nodenr] = value;
837 nb_numa_nodes++;
839 return;
842 static void smp_parse(const char *optarg)
844 int smp, sockets = 0, threads = 0, cores = 0;
845 char *endptr;
846 char option[128];
848 smp = strtoul(optarg, &endptr, 10);
849 if (endptr != optarg) {
850 if (*endptr == ',') {
851 endptr++;
854 if (get_param_value(option, 128, "sockets", endptr) != 0)
855 sockets = strtoull(option, NULL, 10);
856 if (get_param_value(option, 128, "cores", endptr) != 0)
857 cores = strtoull(option, NULL, 10);
858 if (get_param_value(option, 128, "threads", endptr) != 0)
859 threads = strtoull(option, NULL, 10);
860 if (get_param_value(option, 128, "maxcpus", endptr) != 0)
861 max_cpus = strtoull(option, NULL, 10);
863 /* compute missing values, prefer sockets over cores over threads */
864 if (smp == 0 || sockets == 0) {
865 sockets = sockets > 0 ? sockets : 1;
866 cores = cores > 0 ? cores : 1;
867 threads = threads > 0 ? threads : 1;
868 if (smp == 0) {
869 smp = cores * threads * sockets;
871 } else {
872 if (cores == 0) {
873 threads = threads > 0 ? threads : 1;
874 cores = smp / (sockets * threads);
875 } else {
876 threads = smp / (cores * sockets);
879 smp_cpus = smp;
880 smp_cores = cores > 0 ? cores : 1;
881 smp_threads = threads > 0 ? threads : 1;
882 if (max_cpus == 0)
883 max_cpus = smp_cpus;
886 /***********************************************************/
887 /* USB devices */
889 static int usb_device_add(const char *devname)
891 const char *p;
892 USBDevice *dev = NULL;
894 if (!usb_enabled)
895 return -1;
897 /* drivers with .usbdevice_name entry in USBDeviceInfo */
898 dev = usbdevice_create(devname);
899 if (dev)
900 goto done;
902 /* the other ones */
903 if (strstart(devname, "host:", &p)) {
904 dev = usb_host_device_open(p);
905 } else if (!strcmp(devname, "bt") || strstart(devname, "bt:", &p)) {
906 dev = usb_bt_init(devname[2] ? hci_init(p) :
907 bt_new_hci(qemu_find_bt_vlan(0)));
908 } else {
909 return -1;
911 if (!dev)
912 return -1;
914 done:
915 return 0;
918 static int usb_device_del(const char *devname)
920 int bus_num, addr;
921 const char *p;
923 if (strstart(devname, "host:", &p))
924 return usb_host_device_close(p);
926 if (!usb_enabled)
927 return -1;
929 p = strchr(devname, '.');
930 if (!p)
931 return -1;
932 bus_num = strtoul(devname, NULL, 0);
933 addr = strtoul(p + 1, NULL, 0);
935 return usb_device_delete_addr(bus_num, addr);
938 static int usb_parse(const char *cmdline)
940 int r;
941 r = usb_device_add(cmdline);
942 if (r < 0) {
943 fprintf(stderr, "qemu: could not add USB device '%s'\n", cmdline);
945 return r;
948 void do_usb_add(Monitor *mon, const QDict *qdict)
950 const char *devname = qdict_get_str(qdict, "devname");
951 if (usb_device_add(devname) < 0) {
952 error_report("could not add USB device '%s'", devname);
956 void do_usb_del(Monitor *mon, const QDict *qdict)
958 const char *devname = qdict_get_str(qdict, "devname");
959 if (usb_device_del(devname) < 0) {
960 error_report("could not delete USB device '%s'", devname);
964 /***********************************************************/
965 /* PCMCIA/Cardbus */
967 static struct pcmcia_socket_entry_s {
968 PCMCIASocket *socket;
969 struct pcmcia_socket_entry_s *next;
970 } *pcmcia_sockets = 0;
972 void pcmcia_socket_register(PCMCIASocket *socket)
974 struct pcmcia_socket_entry_s *entry;
976 entry = qemu_malloc(sizeof(struct pcmcia_socket_entry_s));
977 entry->socket = socket;
978 entry->next = pcmcia_sockets;
979 pcmcia_sockets = entry;
982 void pcmcia_socket_unregister(PCMCIASocket *socket)
984 struct pcmcia_socket_entry_s *entry, **ptr;
986 ptr = &pcmcia_sockets;
987 for (entry = *ptr; entry; ptr = &entry->next, entry = *ptr)
988 if (entry->socket == socket) {
989 *ptr = entry->next;
990 qemu_free(entry);
994 void pcmcia_info(Monitor *mon)
996 struct pcmcia_socket_entry_s *iter;
998 if (!pcmcia_sockets)
999 monitor_printf(mon, "No PCMCIA sockets\n");
1001 for (iter = pcmcia_sockets; iter; iter = iter->next)
1002 monitor_printf(mon, "%s: %s\n", iter->socket->slot_string,
1003 iter->socket->attached ? iter->socket->card_string :
1004 "Empty");
1007 /***********************************************************/
1008 /* I/O handling */
1010 typedef struct IOHandlerRecord {
1011 int fd;
1012 IOCanReadHandler *fd_read_poll;
1013 IOHandler *fd_read;
1014 IOHandler *fd_write;
1015 int deleted;
1016 void *opaque;
1017 /* temporary data */
1018 struct pollfd *ufd;
1019 QLIST_ENTRY(IOHandlerRecord) next;
1020 } IOHandlerRecord;
1022 static QLIST_HEAD(, IOHandlerRecord) io_handlers =
1023 QLIST_HEAD_INITIALIZER(io_handlers);
1026 /* XXX: fd_read_poll should be suppressed, but an API change is
1027 necessary in the character devices to suppress fd_can_read(). */
1028 int qemu_set_fd_handler2(int fd,
1029 IOCanReadHandler *fd_read_poll,
1030 IOHandler *fd_read,
1031 IOHandler *fd_write,
1032 void *opaque)
1034 IOHandlerRecord *ioh;
1036 if (!fd_read && !fd_write) {
1037 QLIST_FOREACH(ioh, &io_handlers, next) {
1038 if (ioh->fd == fd) {
1039 ioh->deleted = 1;
1040 break;
1043 } else {
1044 QLIST_FOREACH(ioh, &io_handlers, next) {
1045 if (ioh->fd == fd)
1046 goto found;
1048 ioh = qemu_mallocz(sizeof(IOHandlerRecord));
1049 QLIST_INSERT_HEAD(&io_handlers, ioh, next);
1050 found:
1051 ioh->fd = fd;
1052 ioh->fd_read_poll = fd_read_poll;
1053 ioh->fd_read = fd_read;
1054 ioh->fd_write = fd_write;
1055 ioh->opaque = opaque;
1056 ioh->deleted = 0;
1058 return 0;
1061 int qemu_set_fd_handler(int fd,
1062 IOHandler *fd_read,
1063 IOHandler *fd_write,
1064 void *opaque)
1066 return qemu_set_fd_handler2(fd, NULL, fd_read, fd_write, opaque);
1069 /***********************************************************/
1070 /* machine registration */
1072 static QEMUMachine *first_machine = NULL;
1073 QEMUMachine *current_machine = NULL;
1075 int qemu_register_machine(QEMUMachine *m)
1077 QEMUMachine **pm;
1078 pm = &first_machine;
1079 while (*pm != NULL)
1080 pm = &(*pm)->next;
1081 m->next = NULL;
1082 *pm = m;
1083 return 0;
1086 static QEMUMachine *find_machine(const char *name)
1088 QEMUMachine *m;
1090 for(m = first_machine; m != NULL; m = m->next) {
1091 if (!strcmp(m->name, name))
1092 return m;
1093 if (m->alias && !strcmp(m->alias, name))
1094 return m;
1096 return NULL;
1099 static QEMUMachine *find_default_machine(void)
1101 QEMUMachine *m;
1103 for(m = first_machine; m != NULL; m = m->next) {
1104 if (m->is_default) {
1105 return m;
1108 return NULL;
1111 /***********************************************************/
1112 /* main execution loop */
1114 static void gui_update(void *opaque)
1116 uint64_t interval = GUI_REFRESH_INTERVAL;
1117 DisplayState *ds = opaque;
1118 DisplayChangeListener *dcl = ds->listeners;
1120 qemu_flush_coalesced_mmio_buffer();
1121 dpy_refresh(ds);
1123 while (dcl != NULL) {
1124 if (dcl->gui_timer_interval &&
1125 dcl->gui_timer_interval < interval)
1126 interval = dcl->gui_timer_interval;
1127 dcl = dcl->next;
1129 qemu_mod_timer(ds->gui_timer, interval + qemu_get_clock(rt_clock));
1132 static void nographic_update(void *opaque)
1134 uint64_t interval = GUI_REFRESH_INTERVAL;
1136 qemu_flush_coalesced_mmio_buffer();
1137 qemu_mod_timer(nographic_timer, interval + qemu_get_clock(rt_clock));
1140 struct vm_change_state_entry {
1141 VMChangeStateHandler *cb;
1142 void *opaque;
1143 QLIST_ENTRY (vm_change_state_entry) entries;
1146 static QLIST_HEAD(vm_change_state_head, vm_change_state_entry) vm_change_state_head;
1148 VMChangeStateEntry *qemu_add_vm_change_state_handler(VMChangeStateHandler *cb,
1149 void *opaque)
1151 VMChangeStateEntry *e;
1153 e = qemu_mallocz(sizeof (*e));
1155 e->cb = cb;
1156 e->opaque = opaque;
1157 QLIST_INSERT_HEAD(&vm_change_state_head, e, entries);
1158 return e;
1161 void qemu_del_vm_change_state_handler(VMChangeStateEntry *e)
1163 QLIST_REMOVE (e, entries);
1164 qemu_free (e);
1167 void vm_state_notify(int running, int reason)
1169 VMChangeStateEntry *e;
1171 trace_vm_state_notify(running, reason);
1173 for (e = vm_change_state_head.lh_first; e; e = e->entries.le_next) {
1174 e->cb(e->opaque, running, reason);
1178 void vm_start(void)
1180 if (!vm_running) {
1181 cpu_enable_ticks();
1182 vm_running = 1;
1183 vm_state_notify(1, 0);
1184 resume_all_vcpus();
1185 monitor_protocol_event(QEVENT_RESUME, NULL);
1189 /* reset/shutdown handler */
1191 typedef struct QEMUResetEntry {
1192 QTAILQ_ENTRY(QEMUResetEntry) entry;
1193 QEMUResetHandler *func;
1194 void *opaque;
1195 } QEMUResetEntry;
1197 static QTAILQ_HEAD(reset_handlers, QEMUResetEntry) reset_handlers =
1198 QTAILQ_HEAD_INITIALIZER(reset_handlers);
1199 static int reset_requested;
1200 static int shutdown_requested;
1201 static int powerdown_requested;
1202 int debug_requested;
1203 int vmstop_requested;
1205 int qemu_shutdown_requested(void)
1207 int r = shutdown_requested;
1208 shutdown_requested = 0;
1209 return r;
1212 int qemu_reset_requested(void)
1214 int r = reset_requested;
1215 reset_requested = 0;
1216 return r;
1219 int qemu_powerdown_requested(void)
1221 int r = powerdown_requested;
1222 powerdown_requested = 0;
1223 return r;
1226 static int qemu_debug_requested(void)
1228 int r = debug_requested;
1229 debug_requested = 0;
1230 return r;
1233 static int qemu_vmstop_requested(void)
1235 int r = vmstop_requested;
1236 vmstop_requested = 0;
1237 return r;
1240 void qemu_register_reset(QEMUResetHandler *func, void *opaque)
1242 QEMUResetEntry *re = qemu_mallocz(sizeof(QEMUResetEntry));
1244 re->func = func;
1245 re->opaque = opaque;
1246 QTAILQ_INSERT_TAIL(&reset_handlers, re, entry);
1249 void qemu_unregister_reset(QEMUResetHandler *func, void *opaque)
1251 QEMUResetEntry *re;
1253 QTAILQ_FOREACH(re, &reset_handlers, entry) {
1254 if (re->func == func && re->opaque == opaque) {
1255 QTAILQ_REMOVE(&reset_handlers, re, entry);
1256 qemu_free(re);
1257 return;
1262 void qemu_system_reset(void)
1264 QEMUResetEntry *re, *nre;
1266 /* reset all devices */
1267 QTAILQ_FOREACH_SAFE(re, &reset_handlers, entry, nre) {
1268 re->func(re->opaque);
1270 monitor_protocol_event(QEVENT_RESET, NULL);
1271 cpu_synchronize_all_post_reset();
1274 void qemu_system_reset_request(void)
1276 if (no_reboot) {
1277 shutdown_requested = 1;
1278 } else {
1279 reset_requested = 1;
1281 qemu_notify_event();
1284 void qemu_system_shutdown_request(void)
1286 shutdown_requested = 1;
1287 qemu_notify_event();
1290 void qemu_system_powerdown_request(void)
1292 powerdown_requested = 1;
1293 qemu_notify_event();
1296 void main_loop_wait(int nonblocking)
1298 IOHandlerRecord *ioh;
1299 fd_set rfds, wfds, xfds;
1300 int ret, nfds;
1301 struct timeval tv;
1302 int timeout;
1304 if (nonblocking)
1305 timeout = 0;
1306 else {
1307 timeout = qemu_calculate_timeout();
1308 qemu_bh_update_timeout(&timeout);
1311 os_host_main_loop_wait(&timeout);
1313 /* poll any events */
1314 /* XXX: separate device handlers from system ones */
1315 nfds = -1;
1316 FD_ZERO(&rfds);
1317 FD_ZERO(&wfds);
1318 FD_ZERO(&xfds);
1319 QLIST_FOREACH(ioh, &io_handlers, next) {
1320 if (ioh->deleted)
1321 continue;
1322 if (ioh->fd_read &&
1323 (!ioh->fd_read_poll ||
1324 ioh->fd_read_poll(ioh->opaque) != 0)) {
1325 FD_SET(ioh->fd, &rfds);
1326 if (ioh->fd > nfds)
1327 nfds = ioh->fd;
1329 if (ioh->fd_write) {
1330 FD_SET(ioh->fd, &wfds);
1331 if (ioh->fd > nfds)
1332 nfds = ioh->fd;
1336 tv.tv_sec = timeout / 1000;
1337 tv.tv_usec = (timeout % 1000) * 1000;
1339 slirp_select_fill(&nfds, &rfds, &wfds, &xfds);
1341 qemu_mutex_unlock_iothread();
1342 ret = select(nfds + 1, &rfds, &wfds, &xfds, &tv);
1343 qemu_mutex_lock_iothread();
1344 if (ret > 0) {
1345 IOHandlerRecord *pioh;
1347 QLIST_FOREACH_SAFE(ioh, &io_handlers, next, pioh) {
1348 if (!ioh->deleted && ioh->fd_read && FD_ISSET(ioh->fd, &rfds)) {
1349 ioh->fd_read(ioh->opaque);
1351 if (!ioh->deleted && ioh->fd_write && FD_ISSET(ioh->fd, &wfds)) {
1352 ioh->fd_write(ioh->opaque);
1355 /* Do this last in case read/write handlers marked it for deletion */
1356 if (ioh->deleted) {
1357 QLIST_REMOVE(ioh, next);
1358 qemu_free(ioh);
1363 slirp_select_poll(&rfds, &wfds, &xfds, (ret < 0));
1365 qemu_run_all_timers();
1367 /* Check bottom-halves last in case any of the earlier events triggered
1368 them. */
1369 qemu_bh_poll();
1373 static int vm_can_run(void)
1375 if (powerdown_requested)
1376 return 0;
1377 if (reset_requested)
1378 return 0;
1379 if (shutdown_requested)
1380 return 0;
1381 if (debug_requested)
1382 return 0;
1383 return 1;
1386 qemu_irq qemu_system_powerdown;
1388 static void main_loop(void)
1390 int r;
1392 qemu_main_loop_start();
1394 for (;;) {
1395 do {
1396 bool nonblocking = false;
1397 #ifdef CONFIG_PROFILER
1398 int64_t ti;
1399 #endif
1400 #ifndef CONFIG_IOTHREAD
1401 nonblocking = cpu_exec_all();
1402 #endif
1403 #ifdef CONFIG_PROFILER
1404 ti = profile_getclock();
1405 #endif
1406 main_loop_wait(nonblocking);
1407 #ifdef CONFIG_PROFILER
1408 dev_time += profile_getclock() - ti;
1409 #endif
1410 } while (vm_can_run());
1412 if ((r = qemu_debug_requested())) {
1413 vm_stop(r);
1415 if (qemu_shutdown_requested()) {
1416 monitor_protocol_event(QEVENT_SHUTDOWN, NULL);
1417 if (no_shutdown) {
1418 vm_stop(0);
1419 no_shutdown = 0;
1420 } else
1421 break;
1423 if (qemu_reset_requested()) {
1424 pause_all_vcpus();
1425 qemu_system_reset();
1426 resume_all_vcpus();
1428 if (qemu_powerdown_requested()) {
1429 monitor_protocol_event(QEVENT_POWERDOWN, NULL);
1430 qemu_irq_raise(qemu_system_powerdown);
1432 if ((r = qemu_vmstop_requested())) {
1433 vm_stop(r);
1436 bdrv_close_all();
1437 pause_all_vcpus();
1440 static void version(void)
1442 printf("QEMU emulator version " QEMU_VERSION QEMU_PKGVERSION ", Copyright (c) 2003-2008 Fabrice Bellard\n");
1445 static void help(int exitcode)
1447 const char *options_help =
1448 #define DEF(option, opt_arg, opt_enum, opt_help, arch_mask) \
1449 opt_help
1450 #define DEFHEADING(text) stringify(text) "\n"
1451 #include "qemu-options.def"
1452 #undef DEF
1453 #undef DEFHEADING
1454 #undef GEN_DOCS
1456 version();
1457 printf("usage: %s [options] [disk_image]\n"
1458 "\n"
1459 "'disk_image' is a raw hard disk image for IDE hard disk 0\n"
1460 "\n"
1461 "%s\n"
1462 "During emulation, the following keys are useful:\n"
1463 "ctrl-alt-f toggle full screen\n"
1464 "ctrl-alt-n switch to virtual console 'n'\n"
1465 "ctrl-alt toggle mouse and keyboard grab\n"
1466 "\n"
1467 "When using -nographic, press 'ctrl-a h' to get some help.\n",
1468 "qemu",
1469 options_help);
1470 exit(exitcode);
1473 #define HAS_ARG 0x0001
1475 typedef struct QEMUOption {
1476 const char *name;
1477 int flags;
1478 int index;
1479 uint32_t arch_mask;
1480 } QEMUOption;
1482 static const QEMUOption qemu_options[] = {
1483 { "h", 0, QEMU_OPTION_h, QEMU_ARCH_ALL },
1484 #define DEF(option, opt_arg, opt_enum, opt_help, arch_mask) \
1485 { option, opt_arg, opt_enum, arch_mask },
1486 #define DEFHEADING(text)
1487 #include "qemu-options.def"
1488 #undef DEF
1489 #undef DEFHEADING
1490 #undef GEN_DOCS
1491 { NULL },
1493 static void select_vgahw (const char *p)
1495 const char *opts;
1497 default_vga = 0;
1498 vga_interface_type = VGA_NONE;
1499 if (strstart(p, "std", &opts)) {
1500 vga_interface_type = VGA_STD;
1501 } else if (strstart(p, "cirrus", &opts)) {
1502 vga_interface_type = VGA_CIRRUS;
1503 } else if (strstart(p, "vmware", &opts)) {
1504 vga_interface_type = VGA_VMWARE;
1505 } else if (strstart(p, "xenfb", &opts)) {
1506 vga_interface_type = VGA_XENFB;
1507 } else if (strstart(p, "qxl", &opts)) {
1508 vga_interface_type = VGA_QXL;
1509 } else if (!strstart(p, "none", &opts)) {
1510 invalid_vga:
1511 fprintf(stderr, "Unknown vga type: %s\n", p);
1512 exit(1);
1514 while (*opts) {
1515 const char *nextopt;
1517 if (strstart(opts, ",retrace=", &nextopt)) {
1518 opts = nextopt;
1519 if (strstart(opts, "dumb", &nextopt))
1520 vga_retrace_method = VGA_RETRACE_DUMB;
1521 else if (strstart(opts, "precise", &nextopt))
1522 vga_retrace_method = VGA_RETRACE_PRECISE;
1523 else goto invalid_vga;
1524 } else goto invalid_vga;
1525 opts = nextopt;
1529 static int balloon_parse(const char *arg)
1531 QemuOpts *opts;
1533 if (strcmp(arg, "none") == 0) {
1534 return 0;
1537 if (!strncmp(arg, "virtio", 6)) {
1538 if (arg[6] == ',') {
1539 /* have params -> parse them */
1540 opts = qemu_opts_parse(qemu_find_opts("device"), arg+7, 0);
1541 if (!opts)
1542 return -1;
1543 } else {
1544 /* create empty opts */
1545 opts = qemu_opts_create(qemu_find_opts("device"), NULL, 0);
1547 qemu_opt_set(opts, "driver", "virtio-balloon-pci");
1548 return 0;
1551 return -1;
1554 char *qemu_find_file(int type, const char *name)
1556 int len;
1557 const char *subdir;
1558 char *buf;
1560 /* If name contains path separators then try it as a straight path. */
1561 if ((strchr(name, '/') || strchr(name, '\\'))
1562 && access(name, R_OK) == 0) {
1563 return qemu_strdup(name);
1565 switch (type) {
1566 case QEMU_FILE_TYPE_BIOS:
1567 subdir = "";
1568 break;
1569 case QEMU_FILE_TYPE_KEYMAP:
1570 subdir = "keymaps/";
1571 break;
1572 default:
1573 abort();
1575 len = strlen(data_dir) + strlen(name) + strlen(subdir) + 2;
1576 buf = qemu_mallocz(len);
1577 snprintf(buf, len, "%s/%s%s", data_dir, subdir, name);
1578 if (access(buf, R_OK)) {
1579 qemu_free(buf);
1580 return NULL;
1582 return buf;
1585 static int device_help_func(QemuOpts *opts, void *opaque)
1587 return qdev_device_help(opts);
1590 static int device_init_func(QemuOpts *opts, void *opaque)
1592 DeviceState *dev;
1594 dev = qdev_device_add(opts);
1595 if (!dev)
1596 return -1;
1597 return 0;
1600 static int chardev_init_func(QemuOpts *opts, void *opaque)
1602 CharDriverState *chr;
1604 chr = qemu_chr_open_opts(opts, NULL);
1605 if (!chr)
1606 return -1;
1607 return 0;
1610 #ifdef CONFIG_VIRTFS
1611 static int fsdev_init_func(QemuOpts *opts, void *opaque)
1613 int ret;
1614 ret = qemu_fsdev_add(opts);
1616 return ret;
1618 #endif
1620 static int mon_init_func(QemuOpts *opts, void *opaque)
1622 CharDriverState *chr;
1623 const char *chardev;
1624 const char *mode;
1625 int flags;
1627 mode = qemu_opt_get(opts, "mode");
1628 if (mode == NULL) {
1629 mode = "readline";
1631 if (strcmp(mode, "readline") == 0) {
1632 flags = MONITOR_USE_READLINE;
1633 } else if (strcmp(mode, "control") == 0) {
1634 flags = MONITOR_USE_CONTROL;
1635 } else {
1636 fprintf(stderr, "unknown monitor mode \"%s\"\n", mode);
1637 exit(1);
1640 if (qemu_opt_get_bool(opts, "pretty", 0))
1641 flags |= MONITOR_USE_PRETTY;
1643 if (qemu_opt_get_bool(opts, "default", 0))
1644 flags |= MONITOR_IS_DEFAULT;
1646 chardev = qemu_opt_get(opts, "chardev");
1647 chr = qemu_chr_find(chardev);
1648 if (chr == NULL) {
1649 fprintf(stderr, "chardev \"%s\" not found\n", chardev);
1650 exit(1);
1653 monitor_init(chr, flags);
1654 return 0;
1657 static void monitor_parse(const char *optarg, const char *mode)
1659 static int monitor_device_index = 0;
1660 QemuOpts *opts;
1661 const char *p;
1662 char label[32];
1663 int def = 0;
1665 if (strstart(optarg, "chardev:", &p)) {
1666 snprintf(label, sizeof(label), "%s", p);
1667 } else {
1668 snprintf(label, sizeof(label), "compat_monitor%d",
1669 monitor_device_index);
1670 if (monitor_device_index == 0) {
1671 def = 1;
1673 opts = qemu_chr_parse_compat(label, optarg);
1674 if (!opts) {
1675 fprintf(stderr, "parse error: %s\n", optarg);
1676 exit(1);
1680 opts = qemu_opts_create(qemu_find_opts("mon"), label, 1);
1681 if (!opts) {
1682 fprintf(stderr, "duplicate chardev: %s\n", label);
1683 exit(1);
1685 qemu_opt_set(opts, "mode", mode);
1686 qemu_opt_set(opts, "chardev", label);
1687 if (def)
1688 qemu_opt_set(opts, "default", "on");
1689 monitor_device_index++;
1692 struct device_config {
1693 enum {
1694 DEV_USB, /* -usbdevice */
1695 DEV_BT, /* -bt */
1696 DEV_SERIAL, /* -serial */
1697 DEV_PARALLEL, /* -parallel */
1698 DEV_VIRTCON, /* -virtioconsole */
1699 DEV_DEBUGCON, /* -debugcon */
1700 } type;
1701 const char *cmdline;
1702 QTAILQ_ENTRY(device_config) next;
1704 QTAILQ_HEAD(, device_config) device_configs = QTAILQ_HEAD_INITIALIZER(device_configs);
1706 static void add_device_config(int type, const char *cmdline)
1708 struct device_config *conf;
1710 conf = qemu_mallocz(sizeof(*conf));
1711 conf->type = type;
1712 conf->cmdline = cmdline;
1713 QTAILQ_INSERT_TAIL(&device_configs, conf, next);
1716 static int foreach_device_config(int type, int (*func)(const char *cmdline))
1718 struct device_config *conf;
1719 int rc;
1721 QTAILQ_FOREACH(conf, &device_configs, next) {
1722 if (conf->type != type)
1723 continue;
1724 rc = func(conf->cmdline);
1725 if (0 != rc)
1726 return rc;
1728 return 0;
1731 static int serial_parse(const char *devname)
1733 static int index = 0;
1734 char label[32];
1736 if (strcmp(devname, "none") == 0)
1737 return 0;
1738 if (index == MAX_SERIAL_PORTS) {
1739 fprintf(stderr, "qemu: too many serial ports\n");
1740 exit(1);
1742 snprintf(label, sizeof(label), "serial%d", index);
1743 serial_hds[index] = qemu_chr_open(label, devname, NULL);
1744 if (!serial_hds[index]) {
1745 fprintf(stderr, "qemu: could not open serial device '%s': %s\n",
1746 devname, strerror(errno));
1747 return -1;
1749 index++;
1750 return 0;
1753 static int parallel_parse(const char *devname)
1755 static int index = 0;
1756 char label[32];
1758 if (strcmp(devname, "none") == 0)
1759 return 0;
1760 if (index == MAX_PARALLEL_PORTS) {
1761 fprintf(stderr, "qemu: too many parallel ports\n");
1762 exit(1);
1764 snprintf(label, sizeof(label), "parallel%d", index);
1765 parallel_hds[index] = qemu_chr_open(label, devname, NULL);
1766 if (!parallel_hds[index]) {
1767 fprintf(stderr, "qemu: could not open parallel device '%s': %s\n",
1768 devname, strerror(errno));
1769 return -1;
1771 index++;
1772 return 0;
1775 static int virtcon_parse(const char *devname)
1777 QemuOptsList *device = qemu_find_opts("device");
1778 static int index = 0;
1779 char label[32];
1780 QemuOpts *bus_opts, *dev_opts;
1782 if (strcmp(devname, "none") == 0)
1783 return 0;
1784 if (index == MAX_VIRTIO_CONSOLES) {
1785 fprintf(stderr, "qemu: too many virtio consoles\n");
1786 exit(1);
1789 bus_opts = qemu_opts_create(device, NULL, 0);
1790 qemu_opt_set(bus_opts, "driver", "virtio-serial");
1792 dev_opts = qemu_opts_create(device, NULL, 0);
1793 qemu_opt_set(dev_opts, "driver", "virtconsole");
1795 snprintf(label, sizeof(label), "virtcon%d", index);
1796 virtcon_hds[index] = qemu_chr_open(label, devname, NULL);
1797 if (!virtcon_hds[index]) {
1798 fprintf(stderr, "qemu: could not open virtio console '%s': %s\n",
1799 devname, strerror(errno));
1800 return -1;
1802 qemu_opt_set(dev_opts, "chardev", label);
1804 index++;
1805 return 0;
1808 static int debugcon_parse(const char *devname)
1810 QemuOpts *opts;
1812 if (!qemu_chr_open("debugcon", devname, NULL)) {
1813 exit(1);
1815 opts = qemu_opts_create(qemu_find_opts("device"), "debugcon", 1);
1816 if (!opts) {
1817 fprintf(stderr, "qemu: already have a debugcon device\n");
1818 exit(1);
1820 qemu_opt_set(opts, "driver", "isa-debugcon");
1821 qemu_opt_set(opts, "chardev", "debugcon");
1822 return 0;
1825 void qemu_add_exit_notifier(Notifier *notify)
1827 notifier_list_add(&exit_notifiers, notify);
1830 void qemu_remove_exit_notifier(Notifier *notify)
1832 notifier_list_remove(&exit_notifiers, notify);
1835 static void qemu_run_exit_notifiers(void)
1837 notifier_list_notify(&exit_notifiers);
1840 void qemu_add_machine_init_done_notifier(Notifier *notify)
1842 notifier_list_add(&machine_init_done_notifiers, notify);
1845 static void qemu_run_machine_init_done_notifiers(void)
1847 notifier_list_notify(&machine_init_done_notifiers);
1850 static const QEMUOption *lookup_opt(int argc, char **argv,
1851 const char **poptarg, int *poptind)
1853 const QEMUOption *popt;
1854 int optind = *poptind;
1855 char *r = argv[optind];
1856 const char *optarg;
1858 loc_set_cmdline(argv, optind, 1);
1859 optind++;
1860 /* Treat --foo the same as -foo. */
1861 if (r[1] == '-')
1862 r++;
1863 popt = qemu_options;
1864 for(;;) {
1865 if (!popt->name) {
1866 error_report("invalid option");
1867 exit(1);
1869 if (!strcmp(popt->name, r + 1))
1870 break;
1871 popt++;
1873 if (popt->flags & HAS_ARG) {
1874 if (optind >= argc) {
1875 error_report("requires an argument");
1876 exit(1);
1878 optarg = argv[optind++];
1879 loc_set_cmdline(argv, optind - 2, 2);
1880 } else {
1881 optarg = NULL;
1884 *poptarg = optarg;
1885 *poptind = optind;
1887 return popt;
1890 int main(int argc, char **argv, char **envp)
1892 const char *gdbstub_dev = NULL;
1893 int i;
1894 int snapshot, linux_boot;
1895 const char *icount_option = NULL;
1896 const char *initrd_filename;
1897 const char *kernel_filename, *kernel_cmdline;
1898 char boot_devices[33] = "cad"; /* default to HD->floppy->CD-ROM */
1899 DisplayState *ds;
1900 DisplayChangeListener *dcl;
1901 int cyls, heads, secs, translation;
1902 QemuOpts *hda_opts = NULL, *opts;
1903 QemuOptsList *olist;
1904 int optind;
1905 const char *optarg;
1906 const char *loadvm = NULL;
1907 QEMUMachine *machine;
1908 const char *cpu_model;
1909 int tb_size;
1910 const char *pid_file = NULL;
1911 const char *incoming = NULL;
1912 int show_vnc_port = 0;
1913 int defconfig = 1;
1915 #ifdef CONFIG_SIMPLE_TRACE
1916 const char *trace_file = NULL;
1917 #endif
1918 atexit(qemu_run_exit_notifiers);
1919 error_set_progname(argv[0]);
1921 init_clocks();
1923 qemu_cache_utils_init(envp);
1925 QLIST_INIT (&vm_change_state_head);
1926 os_setup_early_signal_handling();
1928 module_call_init(MODULE_INIT_MACHINE);
1929 machine = find_default_machine();
1930 cpu_model = NULL;
1931 initrd_filename = NULL;
1932 ram_size = 0;
1933 snapshot = 0;
1934 kernel_filename = NULL;
1935 kernel_cmdline = "";
1936 cyls = heads = secs = 0;
1937 translation = BIOS_ATA_TRANSLATION_AUTO;
1939 for (i = 0; i < MAX_NODES; i++) {
1940 node_mem[i] = 0;
1941 node_cpumask[i] = 0;
1944 nb_numa_nodes = 0;
1945 nb_nics = 0;
1947 tb_size = 0;
1948 autostart= 1;
1950 /* first pass of option parsing */
1951 optind = 1;
1952 while (optind < argc) {
1953 if (argv[optind][0] != '-') {
1954 /* disk image */
1955 optind++;
1956 continue;
1957 } else {
1958 const QEMUOption *popt;
1960 popt = lookup_opt(argc, argv, &optarg, &optind);
1961 switch (popt->index) {
1962 case QEMU_OPTION_nodefconfig:
1963 defconfig=0;
1964 break;
1969 if (defconfig) {
1970 int ret;
1972 ret = qemu_read_config_file(CONFIG_QEMU_CONFDIR "/qemu.conf");
1973 if (ret < 0 && ret != -ENOENT) {
1974 exit(1);
1977 ret = qemu_read_config_file(arch_config_name);
1978 if (ret < 0 && ret != -ENOENT) {
1979 exit(1);
1982 cpudef_init();
1984 /* second pass of option parsing */
1985 optind = 1;
1986 for(;;) {
1987 if (optind >= argc)
1988 break;
1989 if (argv[optind][0] != '-') {
1990 hda_opts = drive_add(argv[optind++], HD_ALIAS, 0);
1991 } else {
1992 const QEMUOption *popt;
1994 popt = lookup_opt(argc, argv, &optarg, &optind);
1995 if (!(popt->arch_mask & arch_type)) {
1996 printf("Option %s not supported for this target\n", popt->name);
1997 exit(1);
1999 switch(popt->index) {
2000 case QEMU_OPTION_M:
2001 machine = find_machine(optarg);
2002 if (!machine) {
2003 QEMUMachine *m;
2004 printf("Supported machines are:\n");
2005 for(m = first_machine; m != NULL; m = m->next) {
2006 if (m->alias)
2007 printf("%-10s %s (alias of %s)\n",
2008 m->alias, m->desc, m->name);
2009 printf("%-10s %s%s\n",
2010 m->name, m->desc,
2011 m->is_default ? " (default)" : "");
2013 exit(*optarg != '?');
2015 break;
2016 case QEMU_OPTION_cpu:
2017 /* hw initialization will check this */
2018 if (*optarg == '?') {
2019 list_cpus(stdout, &fprintf, optarg);
2020 exit(0);
2021 } else {
2022 cpu_model = optarg;
2024 break;
2025 case QEMU_OPTION_initrd:
2026 initrd_filename = optarg;
2027 break;
2028 case QEMU_OPTION_hda:
2029 if (cyls == 0)
2030 hda_opts = drive_add(optarg, HD_ALIAS, 0);
2031 else
2032 hda_opts = drive_add(optarg, HD_ALIAS
2033 ",cyls=%d,heads=%d,secs=%d%s",
2034 0, cyls, heads, secs,
2035 translation == BIOS_ATA_TRANSLATION_LBA ?
2036 ",trans=lba" :
2037 translation == BIOS_ATA_TRANSLATION_NONE ?
2038 ",trans=none" : "");
2039 break;
2040 case QEMU_OPTION_hdb:
2041 case QEMU_OPTION_hdc:
2042 case QEMU_OPTION_hdd:
2043 drive_add(optarg, HD_ALIAS, popt->index - QEMU_OPTION_hda);
2044 break;
2045 case QEMU_OPTION_drive:
2046 drive_add(NULL, "%s", optarg);
2047 break;
2048 case QEMU_OPTION_set:
2049 if (qemu_set_option(optarg) != 0)
2050 exit(1);
2051 break;
2052 case QEMU_OPTION_global:
2053 if (qemu_global_option(optarg) != 0)
2054 exit(1);
2055 break;
2056 case QEMU_OPTION_mtdblock:
2057 drive_add(optarg, MTD_ALIAS);
2058 break;
2059 case QEMU_OPTION_sd:
2060 drive_add(optarg, SD_ALIAS);
2061 break;
2062 case QEMU_OPTION_pflash:
2063 drive_add(optarg, PFLASH_ALIAS);
2064 break;
2065 case QEMU_OPTION_snapshot:
2066 snapshot = 1;
2067 break;
2068 case QEMU_OPTION_hdachs:
2070 const char *p;
2071 p = optarg;
2072 cyls = strtol(p, (char **)&p, 0);
2073 if (cyls < 1 || cyls > 16383)
2074 goto chs_fail;
2075 if (*p != ',')
2076 goto chs_fail;
2077 p++;
2078 heads = strtol(p, (char **)&p, 0);
2079 if (heads < 1 || heads > 16)
2080 goto chs_fail;
2081 if (*p != ',')
2082 goto chs_fail;
2083 p++;
2084 secs = strtol(p, (char **)&p, 0);
2085 if (secs < 1 || secs > 63)
2086 goto chs_fail;
2087 if (*p == ',') {
2088 p++;
2089 if (!strcmp(p, "none"))
2090 translation = BIOS_ATA_TRANSLATION_NONE;
2091 else if (!strcmp(p, "lba"))
2092 translation = BIOS_ATA_TRANSLATION_LBA;
2093 else if (!strcmp(p, "auto"))
2094 translation = BIOS_ATA_TRANSLATION_AUTO;
2095 else
2096 goto chs_fail;
2097 } else if (*p != '\0') {
2098 chs_fail:
2099 fprintf(stderr, "qemu: invalid physical CHS format\n");
2100 exit(1);
2102 if (hda_opts != NULL) {
2103 char num[16];
2104 snprintf(num, sizeof(num), "%d", cyls);
2105 qemu_opt_set(hda_opts, "cyls", num);
2106 snprintf(num, sizeof(num), "%d", heads);
2107 qemu_opt_set(hda_opts, "heads", num);
2108 snprintf(num, sizeof(num), "%d", secs);
2109 qemu_opt_set(hda_opts, "secs", num);
2110 if (translation == BIOS_ATA_TRANSLATION_LBA)
2111 qemu_opt_set(hda_opts, "trans", "lba");
2112 if (translation == BIOS_ATA_TRANSLATION_NONE)
2113 qemu_opt_set(hda_opts, "trans", "none");
2116 break;
2117 case QEMU_OPTION_numa:
2118 if (nb_numa_nodes >= MAX_NODES) {
2119 fprintf(stderr, "qemu: too many NUMA nodes\n");
2120 exit(1);
2122 numa_add(optarg);
2123 break;
2124 case QEMU_OPTION_nographic:
2125 display_type = DT_NOGRAPHIC;
2126 break;
2127 #ifdef CONFIG_CURSES
2128 case QEMU_OPTION_curses:
2129 display_type = DT_CURSES;
2130 break;
2131 #endif
2132 case QEMU_OPTION_portrait:
2133 graphic_rotate = 1;
2134 break;
2135 case QEMU_OPTION_kernel:
2136 kernel_filename = optarg;
2137 break;
2138 case QEMU_OPTION_append:
2139 kernel_cmdline = optarg;
2140 break;
2141 case QEMU_OPTION_cdrom:
2142 drive_add(optarg, CDROM_ALIAS);
2143 break;
2144 case QEMU_OPTION_boot:
2146 static const char * const params[] = {
2147 "order", "once", "menu", NULL
2149 char buf[sizeof(boot_devices)];
2150 char *standard_boot_devices;
2151 int legacy = 0;
2153 if (!strchr(optarg, '=')) {
2154 legacy = 1;
2155 pstrcpy(buf, sizeof(buf), optarg);
2156 } else if (check_params(buf, sizeof(buf), params, optarg) < 0) {
2157 fprintf(stderr,
2158 "qemu: unknown boot parameter '%s' in '%s'\n",
2159 buf, optarg);
2160 exit(1);
2163 if (legacy ||
2164 get_param_value(buf, sizeof(buf), "order", optarg)) {
2165 validate_bootdevices(buf);
2166 pstrcpy(boot_devices, sizeof(boot_devices), buf);
2168 if (!legacy) {
2169 if (get_param_value(buf, sizeof(buf),
2170 "once", optarg)) {
2171 validate_bootdevices(buf);
2172 standard_boot_devices = qemu_strdup(boot_devices);
2173 pstrcpy(boot_devices, sizeof(boot_devices), buf);
2174 qemu_register_reset(restore_boot_devices,
2175 standard_boot_devices);
2177 if (get_param_value(buf, sizeof(buf),
2178 "menu", optarg)) {
2179 if (!strcmp(buf, "on")) {
2180 boot_menu = 1;
2181 } else if (!strcmp(buf, "off")) {
2182 boot_menu = 0;
2183 } else {
2184 fprintf(stderr,
2185 "qemu: invalid option value '%s'\n",
2186 buf);
2187 exit(1);
2192 break;
2193 case QEMU_OPTION_fda:
2194 case QEMU_OPTION_fdb:
2195 drive_add(optarg, FD_ALIAS, popt->index - QEMU_OPTION_fda);
2196 break;
2197 case QEMU_OPTION_no_fd_bootchk:
2198 fd_bootchk = 0;
2199 break;
2200 case QEMU_OPTION_netdev:
2201 if (net_client_parse(qemu_find_opts("netdev"), optarg) == -1) {
2202 exit(1);
2204 break;
2205 case QEMU_OPTION_net:
2206 if (net_client_parse(qemu_find_opts("net"), optarg) == -1) {
2207 exit(1);
2209 break;
2210 #ifdef CONFIG_SLIRP
2211 case QEMU_OPTION_tftp:
2212 legacy_tftp_prefix = optarg;
2213 break;
2214 case QEMU_OPTION_bootp:
2215 legacy_bootp_filename = optarg;
2216 break;
2217 case QEMU_OPTION_redir:
2218 if (net_slirp_redir(optarg) < 0)
2219 exit(1);
2220 break;
2221 #endif
2222 case QEMU_OPTION_bt:
2223 add_device_config(DEV_BT, optarg);
2224 break;
2225 case QEMU_OPTION_audio_help:
2226 if (!(audio_available())) {
2227 printf("Option %s not supported for this target\n", popt->name);
2228 exit(1);
2230 AUD_help ();
2231 exit (0);
2232 break;
2233 case QEMU_OPTION_soundhw:
2234 if (!(audio_available())) {
2235 printf("Option %s not supported for this target\n", popt->name);
2236 exit(1);
2238 select_soundhw (optarg);
2239 break;
2240 case QEMU_OPTION_h:
2241 help(0);
2242 break;
2243 case QEMU_OPTION_version:
2244 version();
2245 exit(0);
2246 break;
2247 case QEMU_OPTION_m: {
2248 ssize_t value;
2250 value = strtosz(optarg, NULL);
2251 if (value < 0) {
2252 fprintf(stderr, "qemu: invalid ram size: %s\n", optarg);
2253 exit(1);
2256 /* On 32-bit hosts, QEMU is limited by virtual address space */
2257 if (value > (2047 << 20) && HOST_LONG_BITS == 32) {
2258 fprintf(stderr, "qemu: at most 2047 MB RAM can be simulated\n");
2259 exit(1);
2261 if (value != (uint64_t)(ram_addr_t)value) {
2262 fprintf(stderr, "qemu: ram size too large\n");
2263 exit(1);
2265 ram_size = value;
2266 break;
2268 case QEMU_OPTION_mempath:
2269 mem_path = optarg;
2270 break;
2271 #ifdef MAP_POPULATE
2272 case QEMU_OPTION_mem_prealloc:
2273 mem_prealloc = 1;
2274 break;
2275 #endif
2276 case QEMU_OPTION_d:
2277 set_cpu_log(optarg);
2278 break;
2279 case QEMU_OPTION_s:
2280 gdbstub_dev = "tcp::" DEFAULT_GDBSTUB_PORT;
2281 break;
2282 case QEMU_OPTION_gdb:
2283 gdbstub_dev = optarg;
2284 break;
2285 case QEMU_OPTION_L:
2286 data_dir = optarg;
2287 break;
2288 case QEMU_OPTION_bios:
2289 bios_name = optarg;
2290 break;
2291 case QEMU_OPTION_singlestep:
2292 singlestep = 1;
2293 break;
2294 case QEMU_OPTION_S:
2295 autostart = 0;
2296 break;
2297 case QEMU_OPTION_k:
2298 keyboard_layout = optarg;
2299 break;
2300 case QEMU_OPTION_localtime:
2301 rtc_utc = 0;
2302 break;
2303 case QEMU_OPTION_vga:
2304 select_vgahw (optarg);
2305 break;
2306 case QEMU_OPTION_g:
2308 const char *p;
2309 int w, h, depth;
2310 p = optarg;
2311 w = strtol(p, (char **)&p, 10);
2312 if (w <= 0) {
2313 graphic_error:
2314 fprintf(stderr, "qemu: invalid resolution or depth\n");
2315 exit(1);
2317 if (*p != 'x')
2318 goto graphic_error;
2319 p++;
2320 h = strtol(p, (char **)&p, 10);
2321 if (h <= 0)
2322 goto graphic_error;
2323 if (*p == 'x') {
2324 p++;
2325 depth = strtol(p, (char **)&p, 10);
2326 if (depth != 8 && depth != 15 && depth != 16 &&
2327 depth != 24 && depth != 32)
2328 goto graphic_error;
2329 } else if (*p == '\0') {
2330 depth = graphic_depth;
2331 } else {
2332 goto graphic_error;
2335 graphic_width = w;
2336 graphic_height = h;
2337 graphic_depth = depth;
2339 break;
2340 case QEMU_OPTION_echr:
2342 char *r;
2343 term_escape_char = strtol(optarg, &r, 0);
2344 if (r == optarg)
2345 printf("Bad argument to echr\n");
2346 break;
2348 case QEMU_OPTION_monitor:
2349 monitor_parse(optarg, "readline");
2350 default_monitor = 0;
2351 break;
2352 case QEMU_OPTION_qmp:
2353 monitor_parse(optarg, "control");
2354 default_monitor = 0;
2355 break;
2356 case QEMU_OPTION_mon:
2357 opts = qemu_opts_parse(qemu_find_opts("mon"), optarg, 1);
2358 if (!opts) {
2359 exit(1);
2361 default_monitor = 0;
2362 break;
2363 case QEMU_OPTION_chardev:
2364 opts = qemu_opts_parse(qemu_find_opts("chardev"), optarg, 1);
2365 if (!opts) {
2366 exit(1);
2368 break;
2369 case QEMU_OPTION_fsdev:
2370 olist = qemu_find_opts("fsdev");
2371 if (!olist) {
2372 fprintf(stderr, "fsdev is not supported by this qemu build.\n");
2373 exit(1);
2375 opts = qemu_opts_parse(olist, optarg, 1);
2376 if (!opts) {
2377 fprintf(stderr, "parse error: %s\n", optarg);
2378 exit(1);
2380 break;
2381 case QEMU_OPTION_virtfs: {
2382 char *arg_fsdev = NULL;
2383 char *arg_9p = NULL;
2384 int len = 0;
2386 olist = qemu_find_opts("virtfs");
2387 if (!olist) {
2388 fprintf(stderr, "virtfs is not supported by this qemu build.\n");
2389 exit(1);
2391 opts = qemu_opts_parse(olist, optarg, 1);
2392 if (!opts) {
2393 fprintf(stderr, "parse error: %s\n", optarg);
2394 exit(1);
2397 if (qemu_opt_get(opts, "fstype") == NULL ||
2398 qemu_opt_get(opts, "mount_tag") == NULL ||
2399 qemu_opt_get(opts, "path") == NULL ||
2400 qemu_opt_get(opts, "security_model") == NULL) {
2401 fprintf(stderr, "Usage: -virtfs fstype,path=/share_path/,"
2402 "security_model=[mapped|passthrough|none],"
2403 "mnt_tag=tag.\n");
2404 exit(1);
2407 len = strlen(",id=,path=,security_model=");
2408 len += strlen(qemu_opt_get(opts, "fstype"));
2409 len += strlen(qemu_opt_get(opts, "mount_tag"));
2410 len += strlen(qemu_opt_get(opts, "path"));
2411 len += strlen(qemu_opt_get(opts, "security_model"));
2412 arg_fsdev = qemu_malloc((len + 1) * sizeof(*arg_fsdev));
2414 snprintf(arg_fsdev, (len + 1) * sizeof(*arg_fsdev),
2415 "%s,id=%s,path=%s,security_model=%s",
2416 qemu_opt_get(opts, "fstype"),
2417 qemu_opt_get(opts, "mount_tag"),
2418 qemu_opt_get(opts, "path"),
2419 qemu_opt_get(opts, "security_model"));
2421 len = strlen("virtio-9p-pci,fsdev=,mount_tag=");
2422 len += 2*strlen(qemu_opt_get(opts, "mount_tag"));
2423 arg_9p = qemu_malloc((len + 1) * sizeof(*arg_9p));
2425 snprintf(arg_9p, (len + 1) * sizeof(*arg_9p),
2426 "virtio-9p-pci,fsdev=%s,mount_tag=%s",
2427 qemu_opt_get(opts, "mount_tag"),
2428 qemu_opt_get(opts, "mount_tag"));
2430 if (!qemu_opts_parse(qemu_find_opts("fsdev"), arg_fsdev, 1)) {
2431 fprintf(stderr, "parse error [fsdev]: %s\n", optarg);
2432 exit(1);
2435 if (!qemu_opts_parse(qemu_find_opts("device"), arg_9p, 1)) {
2436 fprintf(stderr, "parse error [device]: %s\n", optarg);
2437 exit(1);
2440 qemu_free(arg_fsdev);
2441 qemu_free(arg_9p);
2442 break;
2444 case QEMU_OPTION_serial:
2445 add_device_config(DEV_SERIAL, optarg);
2446 default_serial = 0;
2447 if (strncmp(optarg, "mon:", 4) == 0) {
2448 default_monitor = 0;
2450 break;
2451 case QEMU_OPTION_watchdog:
2452 if (watchdog) {
2453 fprintf(stderr,
2454 "qemu: only one watchdog option may be given\n");
2455 return 1;
2457 watchdog = optarg;
2458 break;
2459 case QEMU_OPTION_watchdog_action:
2460 if (select_watchdog_action(optarg) == -1) {
2461 fprintf(stderr, "Unknown -watchdog-action parameter\n");
2462 exit(1);
2464 break;
2465 case QEMU_OPTION_virtiocon:
2466 add_device_config(DEV_VIRTCON, optarg);
2467 default_virtcon = 0;
2468 if (strncmp(optarg, "mon:", 4) == 0) {
2469 default_monitor = 0;
2471 break;
2472 case QEMU_OPTION_parallel:
2473 add_device_config(DEV_PARALLEL, optarg);
2474 default_parallel = 0;
2475 if (strncmp(optarg, "mon:", 4) == 0) {
2476 default_monitor = 0;
2478 break;
2479 case QEMU_OPTION_debugcon:
2480 add_device_config(DEV_DEBUGCON, optarg);
2481 break;
2482 case QEMU_OPTION_loadvm:
2483 loadvm = optarg;
2484 break;
2485 case QEMU_OPTION_full_screen:
2486 full_screen = 1;
2487 break;
2488 #ifdef CONFIG_SDL
2489 case QEMU_OPTION_no_frame:
2490 no_frame = 1;
2491 break;
2492 case QEMU_OPTION_alt_grab:
2493 alt_grab = 1;
2494 break;
2495 case QEMU_OPTION_ctrl_grab:
2496 ctrl_grab = 1;
2497 break;
2498 case QEMU_OPTION_no_quit:
2499 no_quit = 1;
2500 break;
2501 case QEMU_OPTION_sdl:
2502 display_type = DT_SDL;
2503 break;
2504 #endif
2505 case QEMU_OPTION_pidfile:
2506 pid_file = optarg;
2507 break;
2508 case QEMU_OPTION_win2k_hack:
2509 win2k_install_hack = 1;
2510 break;
2511 case QEMU_OPTION_rtc_td_hack:
2512 rtc_td_hack = 1;
2513 break;
2514 case QEMU_OPTION_acpitable:
2515 do_acpitable_option(optarg);
2516 break;
2517 case QEMU_OPTION_smbios:
2518 do_smbios_option(optarg);
2519 break;
2520 case QEMU_OPTION_enable_kvm:
2521 kvm_allowed = 1;
2522 break;
2523 case QEMU_OPTION_usb:
2524 usb_enabled = 1;
2525 break;
2526 case QEMU_OPTION_usbdevice:
2527 usb_enabled = 1;
2528 add_device_config(DEV_USB, optarg);
2529 break;
2530 case QEMU_OPTION_device:
2531 if (!qemu_opts_parse(qemu_find_opts("device"), optarg, 1)) {
2532 exit(1);
2534 break;
2535 case QEMU_OPTION_smp:
2536 smp_parse(optarg);
2537 if (smp_cpus < 1) {
2538 fprintf(stderr, "Invalid number of CPUs\n");
2539 exit(1);
2541 if (max_cpus < smp_cpus) {
2542 fprintf(stderr, "maxcpus must be equal to or greater than "
2543 "smp\n");
2544 exit(1);
2546 if (max_cpus > 255) {
2547 fprintf(stderr, "Unsupported number of maxcpus\n");
2548 exit(1);
2550 break;
2551 case QEMU_OPTION_vnc:
2552 display_remote++;
2553 vnc_display = optarg;
2554 break;
2555 case QEMU_OPTION_no_acpi:
2556 acpi_enabled = 0;
2557 break;
2558 case QEMU_OPTION_no_hpet:
2559 no_hpet = 1;
2560 break;
2561 case QEMU_OPTION_balloon:
2562 if (balloon_parse(optarg) < 0) {
2563 fprintf(stderr, "Unknown -balloon argument %s\n", optarg);
2564 exit(1);
2566 break;
2567 case QEMU_OPTION_no_reboot:
2568 no_reboot = 1;
2569 break;
2570 case QEMU_OPTION_no_shutdown:
2571 no_shutdown = 1;
2572 break;
2573 case QEMU_OPTION_show_cursor:
2574 cursor_hide = 0;
2575 break;
2576 case QEMU_OPTION_uuid:
2577 if(qemu_uuid_parse(optarg, qemu_uuid) < 0) {
2578 fprintf(stderr, "Fail to parse UUID string."
2579 " Wrong format.\n");
2580 exit(1);
2582 break;
2583 case QEMU_OPTION_option_rom:
2584 if (nb_option_roms >= MAX_OPTION_ROMS) {
2585 fprintf(stderr, "Too many option ROMs\n");
2586 exit(1);
2588 opts = qemu_opts_parse(qemu_find_opts("option-rom"), optarg, 1);
2589 option_rom[nb_option_roms].name = qemu_opt_get(opts, "romfile");
2590 option_rom[nb_option_roms].bootindex =
2591 qemu_opt_get_number(opts, "bootindex", -1);
2592 if (!option_rom[nb_option_roms].name) {
2593 fprintf(stderr, "Option ROM file is not specified\n");
2594 exit(1);
2596 nb_option_roms++;
2597 break;
2598 case QEMU_OPTION_semihosting:
2599 semihosting_enabled = 1;
2600 break;
2601 case QEMU_OPTION_name:
2602 qemu_name = qemu_strdup(optarg);
2604 char *p = strchr(qemu_name, ',');
2605 if (p != NULL) {
2606 *p++ = 0;
2607 if (strncmp(p, "process=", 8)) {
2608 fprintf(stderr, "Unknown subargument %s to -name\n", p);
2609 exit(1);
2611 p += 8;
2612 os_set_proc_name(p);
2615 break;
2616 case QEMU_OPTION_prom_env:
2617 if (nb_prom_envs >= MAX_PROM_ENVS) {
2618 fprintf(stderr, "Too many prom variables\n");
2619 exit(1);
2621 prom_envs[nb_prom_envs] = optarg;
2622 nb_prom_envs++;
2623 break;
2624 case QEMU_OPTION_old_param:
2625 old_param = 1;
2626 break;
2627 case QEMU_OPTION_clock:
2628 configure_alarms(optarg);
2629 break;
2630 case QEMU_OPTION_startdate:
2631 configure_rtc_date_offset(optarg, 1);
2632 break;
2633 case QEMU_OPTION_rtc:
2634 opts = qemu_opts_parse(qemu_find_opts("rtc"), optarg, 0);
2635 if (!opts) {
2636 exit(1);
2638 configure_rtc(opts);
2639 break;
2640 case QEMU_OPTION_tb_size:
2641 tb_size = strtol(optarg, NULL, 0);
2642 if (tb_size < 0)
2643 tb_size = 0;
2644 break;
2645 case QEMU_OPTION_icount:
2646 icount_option = optarg;
2647 break;
2648 case QEMU_OPTION_incoming:
2649 incoming = optarg;
2650 incoming_expected = true;
2651 break;
2652 case QEMU_OPTION_nodefaults:
2653 default_serial = 0;
2654 default_parallel = 0;
2655 default_virtcon = 0;
2656 default_monitor = 0;
2657 default_vga = 0;
2658 default_net = 0;
2659 default_floppy = 0;
2660 default_cdrom = 0;
2661 default_sdcard = 0;
2662 break;
2663 case QEMU_OPTION_xen_domid:
2664 if (!(xen_available())) {
2665 printf("Option %s not supported for this target\n", popt->name);
2666 exit(1);
2668 xen_domid = atoi(optarg);
2669 break;
2670 case QEMU_OPTION_xen_create:
2671 if (!(xen_available())) {
2672 printf("Option %s not supported for this target\n", popt->name);
2673 exit(1);
2675 xen_mode = XEN_CREATE;
2676 break;
2677 case QEMU_OPTION_xen_attach:
2678 if (!(xen_available())) {
2679 printf("Option %s not supported for this target\n", popt->name);
2680 exit(1);
2682 xen_mode = XEN_ATTACH;
2683 break;
2684 #ifdef CONFIG_SIMPLE_TRACE
2685 case QEMU_OPTION_trace:
2686 opts = qemu_opts_parse(qemu_find_opts("trace"), optarg, 0);
2687 if (opts) {
2688 trace_file = qemu_opt_get(opts, "file");
2690 break;
2691 #endif
2692 case QEMU_OPTION_readconfig:
2694 int ret = qemu_read_config_file(optarg);
2695 if (ret < 0) {
2696 fprintf(stderr, "read config %s: %s\n", optarg,
2697 strerror(-ret));
2698 exit(1);
2700 break;
2702 case QEMU_OPTION_spice:
2703 olist = qemu_find_opts("spice");
2704 if (!olist) {
2705 fprintf(stderr, "spice is not supported by this qemu build.\n");
2706 exit(1);
2708 opts = qemu_opts_parse(olist, optarg, 0);
2709 if (!opts) {
2710 fprintf(stderr, "parse error: %s\n", optarg);
2711 exit(1);
2713 break;
2714 case QEMU_OPTION_writeconfig:
2716 FILE *fp;
2717 if (strcmp(optarg, "-") == 0) {
2718 fp = stdout;
2719 } else {
2720 fp = fopen(optarg, "w");
2721 if (fp == NULL) {
2722 fprintf(stderr, "open %s: %s\n", optarg, strerror(errno));
2723 exit(1);
2726 qemu_config_write(fp);
2727 fclose(fp);
2728 break;
2730 default:
2731 os_parse_cmd_args(popt->index, optarg);
2735 loc_set_none();
2737 /* If no data_dir is specified then try to find it relative to the
2738 executable path. */
2739 if (!data_dir) {
2740 data_dir = os_find_datadir(argv[0]);
2742 /* If all else fails use the install patch specified when building. */
2743 if (!data_dir) {
2744 data_dir = CONFIG_QEMU_DATADIR;
2747 #ifdef CONFIG_SIMPLE_TRACE
2749 * Set the trace file name, if specified.
2751 st_set_trace_file(trace_file);
2752 #endif
2754 * Default to max_cpus = smp_cpus, in case the user doesn't
2755 * specify a max_cpus value.
2757 if (!max_cpus)
2758 max_cpus = smp_cpus;
2760 machine->max_cpus = machine->max_cpus ?: 1; /* Default to UP */
2761 if (smp_cpus > machine->max_cpus) {
2762 fprintf(stderr, "Number of SMP cpus requested (%d), exceeds max cpus "
2763 "supported by machine `%s' (%d)\n", smp_cpus, machine->name,
2764 machine->max_cpus);
2765 exit(1);
2768 qemu_opts_foreach(qemu_find_opts("device"), default_driver_check, NULL, 0);
2769 qemu_opts_foreach(qemu_find_opts("global"), default_driver_check, NULL, 0);
2771 if (machine->no_serial) {
2772 default_serial = 0;
2774 if (machine->no_parallel) {
2775 default_parallel = 0;
2777 if (!machine->use_virtcon) {
2778 default_virtcon = 0;
2780 if (machine->no_vga) {
2781 default_vga = 0;
2783 if (machine->no_floppy) {
2784 default_floppy = 0;
2786 if (machine->no_cdrom) {
2787 default_cdrom = 0;
2789 if (machine->no_sdcard) {
2790 default_sdcard = 0;
2793 if (display_type == DT_NOGRAPHIC) {
2794 if (default_parallel)
2795 add_device_config(DEV_PARALLEL, "null");
2796 if (default_serial && default_monitor) {
2797 add_device_config(DEV_SERIAL, "mon:stdio");
2798 } else if (default_virtcon && default_monitor) {
2799 add_device_config(DEV_VIRTCON, "mon:stdio");
2800 } else {
2801 if (default_serial)
2802 add_device_config(DEV_SERIAL, "stdio");
2803 if (default_virtcon)
2804 add_device_config(DEV_VIRTCON, "stdio");
2805 if (default_monitor)
2806 monitor_parse("stdio", "readline");
2808 } else {
2809 if (default_serial)
2810 add_device_config(DEV_SERIAL, "vc:80Cx24C");
2811 if (default_parallel)
2812 add_device_config(DEV_PARALLEL, "vc:80Cx24C");
2813 if (default_monitor)
2814 monitor_parse("vc:80Cx24C", "readline");
2815 if (default_virtcon)
2816 add_device_config(DEV_VIRTCON, "vc:80Cx24C");
2818 if (default_vga)
2819 vga_interface_type = VGA_CIRRUS;
2821 socket_init();
2823 if (qemu_opts_foreach(qemu_find_opts("chardev"), chardev_init_func, NULL, 1) != 0)
2824 exit(1);
2825 #ifdef CONFIG_VIRTFS
2826 if (qemu_opts_foreach(qemu_find_opts("fsdev"), fsdev_init_func, NULL, 1) != 0) {
2827 exit(1);
2829 #endif
2831 os_daemonize();
2833 if (pid_file && qemu_create_pidfile(pid_file) != 0) {
2834 os_pidfile_error();
2835 exit(1);
2838 if (kvm_allowed) {
2839 int ret = kvm_init(smp_cpus);
2840 if (ret < 0) {
2841 if (!kvm_available()) {
2842 printf("KVM not supported for this target\n");
2843 } else {
2844 fprintf(stderr, "failed to initialize KVM: %s\n", strerror(-ret));
2846 exit(1);
2850 if (qemu_init_main_loop()) {
2851 fprintf(stderr, "qemu_init_main_loop failed\n");
2852 exit(1);
2854 linux_boot = (kernel_filename != NULL);
2856 if (!linux_boot && *kernel_cmdline != '\0') {
2857 fprintf(stderr, "-append only allowed with -kernel option\n");
2858 exit(1);
2861 if (!linux_boot && initrd_filename != NULL) {
2862 fprintf(stderr, "-initrd only allowed with -kernel option\n");
2863 exit(1);
2866 os_set_line_buffering();
2868 if (init_timer_alarm() < 0) {
2869 fprintf(stderr, "could not initialize alarm timer\n");
2870 exit(1);
2872 configure_icount(icount_option);
2874 if (net_init_clients() < 0) {
2875 exit(1);
2878 /* init the bluetooth world */
2879 if (foreach_device_config(DEV_BT, bt_parse))
2880 exit(1);
2882 /* init the memory */
2883 if (ram_size == 0)
2884 ram_size = DEFAULT_RAM_SIZE * 1024 * 1024;
2886 /* init the dynamic translator */
2887 cpu_exec_init_all(tb_size * 1024 * 1024);
2889 bdrv_init_with_whitelist();
2891 blk_mig_init();
2893 if (default_cdrom) {
2894 /* we always create the cdrom drive, even if no disk is there */
2895 drive_add(NULL, CDROM_ALIAS);
2898 if (default_floppy) {
2899 /* we always create at least one floppy */
2900 drive_add(NULL, FD_ALIAS, 0);
2903 if (default_sdcard) {
2904 /* we always create one sd slot, even if no card is in it */
2905 drive_add(NULL, SD_ALIAS);
2908 /* open the virtual block devices */
2909 if (snapshot)
2910 qemu_opts_foreach(qemu_find_opts("drive"), drive_enable_snapshot, NULL, 0);
2911 if (qemu_opts_foreach(qemu_find_opts("drive"), drive_init_func, &machine->use_scsi, 1) != 0)
2912 exit(1);
2914 register_savevm_live(NULL, "ram", 0, 4, NULL, ram_save_live, NULL,
2915 ram_load, NULL);
2917 if (nb_numa_nodes > 0) {
2918 int i;
2920 if (nb_numa_nodes > smp_cpus) {
2921 nb_numa_nodes = smp_cpus;
2924 /* If no memory size if given for any node, assume the default case
2925 * and distribute the available memory equally across all nodes
2927 for (i = 0; i < nb_numa_nodes; i++) {
2928 if (node_mem[i] != 0)
2929 break;
2931 if (i == nb_numa_nodes) {
2932 uint64_t usedmem = 0;
2934 /* On Linux, the each node's border has to be 8MB aligned,
2935 * the final node gets the rest.
2937 for (i = 0; i < nb_numa_nodes - 1; i++) {
2938 node_mem[i] = (ram_size / nb_numa_nodes) & ~((1 << 23UL) - 1);
2939 usedmem += node_mem[i];
2941 node_mem[i] = ram_size - usedmem;
2944 for (i = 0; i < nb_numa_nodes; i++) {
2945 if (node_cpumask[i] != 0)
2946 break;
2948 /* assigning the VCPUs round-robin is easier to implement, guest OSes
2949 * must cope with this anyway, because there are BIOSes out there in
2950 * real machines which also use this scheme.
2952 if (i == nb_numa_nodes) {
2953 for (i = 0; i < smp_cpus; i++) {
2954 node_cpumask[i % nb_numa_nodes] |= 1 << i;
2959 if (qemu_opts_foreach(qemu_find_opts("mon"), mon_init_func, NULL, 1) != 0) {
2960 exit(1);
2963 if (foreach_device_config(DEV_SERIAL, serial_parse) < 0)
2964 exit(1);
2965 if (foreach_device_config(DEV_PARALLEL, parallel_parse) < 0)
2966 exit(1);
2967 if (foreach_device_config(DEV_VIRTCON, virtcon_parse) < 0)
2968 exit(1);
2969 if (foreach_device_config(DEV_DEBUGCON, debugcon_parse) < 0)
2970 exit(1);
2972 module_call_init(MODULE_INIT_DEVICE);
2974 if (qemu_opts_foreach(qemu_find_opts("device"), device_help_func, NULL, 0) != 0)
2975 exit(0);
2977 if (watchdog) {
2978 i = select_watchdog(watchdog);
2979 if (i > 0)
2980 exit (i == 1 ? 1 : 0);
2983 if (machine->compat_props) {
2984 qdev_prop_register_global_list(machine->compat_props);
2986 qemu_add_globals();
2988 machine->init(ram_size, boot_devices,
2989 kernel_filename, kernel_cmdline, initrd_filename, cpu_model);
2991 cpu_synchronize_all_post_init();
2993 /* must be after terminal init, SDL library changes signal handlers */
2994 os_setup_signal_handling();
2996 set_numa_modes();
2998 current_machine = machine;
3000 /* init USB devices */
3001 if (usb_enabled) {
3002 if (foreach_device_config(DEV_USB, usb_parse) < 0)
3003 exit(1);
3006 /* init generic devices */
3007 if (qemu_opts_foreach(qemu_find_opts("device"), device_init_func, NULL, 1) != 0)
3008 exit(1);
3010 net_check_clients();
3012 /* just use the first displaystate for the moment */
3013 ds = get_displaystate();
3015 if (using_spice)
3016 display_remote++;
3017 if (display_type == DT_DEFAULT && !display_remote) {
3018 #if defined(CONFIG_SDL) || defined(CONFIG_COCOA)
3019 display_type = DT_SDL;
3020 #else
3021 vnc_display = "localhost:0,to=99";
3022 show_vnc_port = 1;
3023 #endif
3027 /* init local displays */
3028 switch (display_type) {
3029 case DT_NOGRAPHIC:
3030 break;
3031 #if defined(CONFIG_CURSES)
3032 case DT_CURSES:
3033 curses_display_init(ds, full_screen);
3034 break;
3035 #endif
3036 #if defined(CONFIG_SDL)
3037 case DT_SDL:
3038 sdl_display_init(ds, full_screen, no_frame);
3039 break;
3040 #elif defined(CONFIG_COCOA)
3041 case DT_SDL:
3042 cocoa_display_init(ds, full_screen);
3043 break;
3044 #endif
3045 default:
3046 break;
3049 /* init remote displays */
3050 if (vnc_display) {
3051 vnc_display_init(ds);
3052 if (vnc_display_open(ds, vnc_display) < 0)
3053 exit(1);
3055 if (show_vnc_port) {
3056 printf("VNC server running on `%s'\n", vnc_display_local_addr(ds));
3059 #ifdef CONFIG_SPICE
3060 if (using_spice && !qxl_enabled) {
3061 qemu_spice_display_init(ds);
3063 #endif
3065 /* display setup */
3066 dpy_resize(ds);
3067 dcl = ds->listeners;
3068 while (dcl != NULL) {
3069 if (dcl->dpy_refresh != NULL) {
3070 ds->gui_timer = qemu_new_timer(rt_clock, gui_update, ds);
3071 qemu_mod_timer(ds->gui_timer, qemu_get_clock(rt_clock));
3072 break;
3074 dcl = dcl->next;
3076 if (ds->gui_timer == NULL) {
3077 nographic_timer = qemu_new_timer(rt_clock, nographic_update, NULL);
3078 qemu_mod_timer(nographic_timer, qemu_get_clock(rt_clock));
3080 text_consoles_set_display(ds);
3082 if (gdbstub_dev && gdbserver_start(gdbstub_dev) < 0) {
3083 fprintf(stderr, "qemu: could not open gdbserver on device '%s'\n",
3084 gdbstub_dev);
3085 exit(1);
3088 qdev_machine_creation_done();
3090 if (rom_load_all() != 0) {
3091 fprintf(stderr, "rom loading failed\n");
3092 exit(1);
3095 /* TODO: once all bus devices are qdevified, this should be done
3096 * when bus is created by qdev.c */
3097 qemu_register_reset(qbus_reset_all_fn, sysbus_get_default());
3098 qemu_run_machine_init_done_notifiers();
3100 qemu_system_reset();
3101 if (loadvm) {
3102 if (load_vmstate(loadvm) < 0) {
3103 autostart = 0;
3107 if (incoming) {
3108 int ret = qemu_start_incoming_migration(incoming);
3109 if (ret < 0) {
3110 fprintf(stderr, "Migration failed. Exit code %s(%d), exiting.\n",
3111 incoming, ret);
3112 exit(ret);
3114 } else if (autostart) {
3115 vm_start();
3118 os_setup_post();
3120 main_loop();
3121 quit_timers();
3122 net_cleanup();
3124 return 0;