qemu-img.c: Re-factor img_create()
[qemu/cris-port.git] / vl.c
blobc4d3fc0d3d2ee2f24fd5bfb67396ae5f85d77a05
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 bootpath = qemu_malloc(strlen(devpath) + strlen(i->suffix) + 1);
763 sprintf(bootpath, "%s%s", devpath, i->suffix);
764 qemu_free(devpath);
765 } else if (devpath) {
766 bootpath = devpath;
767 } else {
768 bootpath = strdup(i->suffix);
769 assert(bootpath);
772 if (total) {
773 list[total-1] = '\n';
775 len = strlen(bootpath) + 1;
776 list = qemu_realloc(list, total + len);
777 memcpy(&list[total], bootpath, len);
778 total += len;
779 qemu_free(bootpath);
782 *size = total;
784 return list;
787 static void numa_add(const char *optarg)
789 char option[128];
790 char *endptr;
791 unsigned long long value, endvalue;
792 int nodenr;
794 optarg = get_opt_name(option, 128, optarg, ',') + 1;
795 if (!strcmp(option, "node")) {
796 if (get_param_value(option, 128, "nodeid", optarg) == 0) {
797 nodenr = nb_numa_nodes;
798 } else {
799 nodenr = strtoull(option, NULL, 10);
802 if (get_param_value(option, 128, "mem", optarg) == 0) {
803 node_mem[nodenr] = 0;
804 } else {
805 ssize_t sval;
806 sval = strtosz(option, NULL);
807 if (sval < 0) {
808 fprintf(stderr, "qemu: invalid numa mem size: %s\n", optarg);
809 exit(1);
811 node_mem[nodenr] = sval;
813 if (get_param_value(option, 128, "cpus", optarg) == 0) {
814 node_cpumask[nodenr] = 0;
815 } else {
816 value = strtoull(option, &endptr, 10);
817 if (value >= 64) {
818 value = 63;
819 fprintf(stderr, "only 64 CPUs in NUMA mode supported.\n");
820 } else {
821 if (*endptr == '-') {
822 endvalue = strtoull(endptr+1, &endptr, 10);
823 if (endvalue >= 63) {
824 endvalue = 62;
825 fprintf(stderr,
826 "only 63 CPUs in NUMA mode supported.\n");
828 value = (2ULL << endvalue) - (1ULL << value);
829 } else {
830 value = 1ULL << value;
833 node_cpumask[nodenr] = value;
835 nb_numa_nodes++;
837 return;
840 static void smp_parse(const char *optarg)
842 int smp, sockets = 0, threads = 0, cores = 0;
843 char *endptr;
844 char option[128];
846 smp = strtoul(optarg, &endptr, 10);
847 if (endptr != optarg) {
848 if (*endptr == ',') {
849 endptr++;
852 if (get_param_value(option, 128, "sockets", endptr) != 0)
853 sockets = strtoull(option, NULL, 10);
854 if (get_param_value(option, 128, "cores", endptr) != 0)
855 cores = strtoull(option, NULL, 10);
856 if (get_param_value(option, 128, "threads", endptr) != 0)
857 threads = strtoull(option, NULL, 10);
858 if (get_param_value(option, 128, "maxcpus", endptr) != 0)
859 max_cpus = strtoull(option, NULL, 10);
861 /* compute missing values, prefer sockets over cores over threads */
862 if (smp == 0 || sockets == 0) {
863 sockets = sockets > 0 ? sockets : 1;
864 cores = cores > 0 ? cores : 1;
865 threads = threads > 0 ? threads : 1;
866 if (smp == 0) {
867 smp = cores * threads * sockets;
869 } else {
870 if (cores == 0) {
871 threads = threads > 0 ? threads : 1;
872 cores = smp / (sockets * threads);
873 } else {
874 threads = smp / (cores * sockets);
877 smp_cpus = smp;
878 smp_cores = cores > 0 ? cores : 1;
879 smp_threads = threads > 0 ? threads : 1;
880 if (max_cpus == 0)
881 max_cpus = smp_cpus;
884 /***********************************************************/
885 /* USB devices */
887 static int usb_device_add(const char *devname)
889 const char *p;
890 USBDevice *dev = NULL;
892 if (!usb_enabled)
893 return -1;
895 /* drivers with .usbdevice_name entry in USBDeviceInfo */
896 dev = usbdevice_create(devname);
897 if (dev)
898 goto done;
900 /* the other ones */
901 if (strstart(devname, "host:", &p)) {
902 dev = usb_host_device_open(p);
903 } else if (!strcmp(devname, "bt") || strstart(devname, "bt:", &p)) {
904 dev = usb_bt_init(devname[2] ? hci_init(p) :
905 bt_new_hci(qemu_find_bt_vlan(0)));
906 } else {
907 return -1;
909 if (!dev)
910 return -1;
912 done:
913 return 0;
916 static int usb_device_del(const char *devname)
918 int bus_num, addr;
919 const char *p;
921 if (strstart(devname, "host:", &p))
922 return usb_host_device_close(p);
924 if (!usb_enabled)
925 return -1;
927 p = strchr(devname, '.');
928 if (!p)
929 return -1;
930 bus_num = strtoul(devname, NULL, 0);
931 addr = strtoul(p + 1, NULL, 0);
933 return usb_device_delete_addr(bus_num, addr);
936 static int usb_parse(const char *cmdline)
938 int r;
939 r = usb_device_add(cmdline);
940 if (r < 0) {
941 fprintf(stderr, "qemu: could not add USB device '%s'\n", cmdline);
943 return r;
946 void do_usb_add(Monitor *mon, const QDict *qdict)
948 const char *devname = qdict_get_str(qdict, "devname");
949 if (usb_device_add(devname) < 0) {
950 error_report("could not add USB device '%s'", devname);
954 void do_usb_del(Monitor *mon, const QDict *qdict)
956 const char *devname = qdict_get_str(qdict, "devname");
957 if (usb_device_del(devname) < 0) {
958 error_report("could not delete USB device '%s'", devname);
962 /***********************************************************/
963 /* PCMCIA/Cardbus */
965 static struct pcmcia_socket_entry_s {
966 PCMCIASocket *socket;
967 struct pcmcia_socket_entry_s *next;
968 } *pcmcia_sockets = 0;
970 void pcmcia_socket_register(PCMCIASocket *socket)
972 struct pcmcia_socket_entry_s *entry;
974 entry = qemu_malloc(sizeof(struct pcmcia_socket_entry_s));
975 entry->socket = socket;
976 entry->next = pcmcia_sockets;
977 pcmcia_sockets = entry;
980 void pcmcia_socket_unregister(PCMCIASocket *socket)
982 struct pcmcia_socket_entry_s *entry, **ptr;
984 ptr = &pcmcia_sockets;
985 for (entry = *ptr; entry; ptr = &entry->next, entry = *ptr)
986 if (entry->socket == socket) {
987 *ptr = entry->next;
988 qemu_free(entry);
992 void pcmcia_info(Monitor *mon)
994 struct pcmcia_socket_entry_s *iter;
996 if (!pcmcia_sockets)
997 monitor_printf(mon, "No PCMCIA sockets\n");
999 for (iter = pcmcia_sockets; iter; iter = iter->next)
1000 monitor_printf(mon, "%s: %s\n", iter->socket->slot_string,
1001 iter->socket->attached ? iter->socket->card_string :
1002 "Empty");
1005 /***********************************************************/
1006 /* I/O handling */
1008 typedef struct IOHandlerRecord {
1009 int fd;
1010 IOCanReadHandler *fd_read_poll;
1011 IOHandler *fd_read;
1012 IOHandler *fd_write;
1013 int deleted;
1014 void *opaque;
1015 /* temporary data */
1016 struct pollfd *ufd;
1017 QLIST_ENTRY(IOHandlerRecord) next;
1018 } IOHandlerRecord;
1020 static QLIST_HEAD(, IOHandlerRecord) io_handlers =
1021 QLIST_HEAD_INITIALIZER(io_handlers);
1024 /* XXX: fd_read_poll should be suppressed, but an API change is
1025 necessary in the character devices to suppress fd_can_read(). */
1026 int qemu_set_fd_handler2(int fd,
1027 IOCanReadHandler *fd_read_poll,
1028 IOHandler *fd_read,
1029 IOHandler *fd_write,
1030 void *opaque)
1032 IOHandlerRecord *ioh;
1034 if (!fd_read && !fd_write) {
1035 QLIST_FOREACH(ioh, &io_handlers, next) {
1036 if (ioh->fd == fd) {
1037 ioh->deleted = 1;
1038 break;
1041 } else {
1042 QLIST_FOREACH(ioh, &io_handlers, next) {
1043 if (ioh->fd == fd)
1044 goto found;
1046 ioh = qemu_mallocz(sizeof(IOHandlerRecord));
1047 QLIST_INSERT_HEAD(&io_handlers, ioh, next);
1048 found:
1049 ioh->fd = fd;
1050 ioh->fd_read_poll = fd_read_poll;
1051 ioh->fd_read = fd_read;
1052 ioh->fd_write = fd_write;
1053 ioh->opaque = opaque;
1054 ioh->deleted = 0;
1056 return 0;
1059 int qemu_set_fd_handler(int fd,
1060 IOHandler *fd_read,
1061 IOHandler *fd_write,
1062 void *opaque)
1064 return qemu_set_fd_handler2(fd, NULL, fd_read, fd_write, opaque);
1067 /***********************************************************/
1068 /* machine registration */
1070 static QEMUMachine *first_machine = NULL;
1071 QEMUMachine *current_machine = NULL;
1073 int qemu_register_machine(QEMUMachine *m)
1075 QEMUMachine **pm;
1076 pm = &first_machine;
1077 while (*pm != NULL)
1078 pm = &(*pm)->next;
1079 m->next = NULL;
1080 *pm = m;
1081 return 0;
1084 static QEMUMachine *find_machine(const char *name)
1086 QEMUMachine *m;
1088 for(m = first_machine; m != NULL; m = m->next) {
1089 if (!strcmp(m->name, name))
1090 return m;
1091 if (m->alias && !strcmp(m->alias, name))
1092 return m;
1094 return NULL;
1097 static QEMUMachine *find_default_machine(void)
1099 QEMUMachine *m;
1101 for(m = first_machine; m != NULL; m = m->next) {
1102 if (m->is_default) {
1103 return m;
1106 return NULL;
1109 /***********************************************************/
1110 /* main execution loop */
1112 static void gui_update(void *opaque)
1114 uint64_t interval = GUI_REFRESH_INTERVAL;
1115 DisplayState *ds = opaque;
1116 DisplayChangeListener *dcl = ds->listeners;
1118 qemu_flush_coalesced_mmio_buffer();
1119 dpy_refresh(ds);
1121 while (dcl != NULL) {
1122 if (dcl->gui_timer_interval &&
1123 dcl->gui_timer_interval < interval)
1124 interval = dcl->gui_timer_interval;
1125 dcl = dcl->next;
1127 qemu_mod_timer(ds->gui_timer, interval + qemu_get_clock(rt_clock));
1130 static void nographic_update(void *opaque)
1132 uint64_t interval = GUI_REFRESH_INTERVAL;
1134 qemu_flush_coalesced_mmio_buffer();
1135 qemu_mod_timer(nographic_timer, interval + qemu_get_clock(rt_clock));
1138 struct vm_change_state_entry {
1139 VMChangeStateHandler *cb;
1140 void *opaque;
1141 QLIST_ENTRY (vm_change_state_entry) entries;
1144 static QLIST_HEAD(vm_change_state_head, vm_change_state_entry) vm_change_state_head;
1146 VMChangeStateEntry *qemu_add_vm_change_state_handler(VMChangeStateHandler *cb,
1147 void *opaque)
1149 VMChangeStateEntry *e;
1151 e = qemu_mallocz(sizeof (*e));
1153 e->cb = cb;
1154 e->opaque = opaque;
1155 QLIST_INSERT_HEAD(&vm_change_state_head, e, entries);
1156 return e;
1159 void qemu_del_vm_change_state_handler(VMChangeStateEntry *e)
1161 QLIST_REMOVE (e, entries);
1162 qemu_free (e);
1165 void vm_state_notify(int running, int reason)
1167 VMChangeStateEntry *e;
1169 trace_vm_state_notify(running, reason);
1171 for (e = vm_change_state_head.lh_first; e; e = e->entries.le_next) {
1172 e->cb(e->opaque, running, reason);
1176 void vm_start(void)
1178 if (!vm_running) {
1179 cpu_enable_ticks();
1180 vm_running = 1;
1181 vm_state_notify(1, 0);
1182 resume_all_vcpus();
1183 monitor_protocol_event(QEVENT_RESUME, NULL);
1187 /* reset/shutdown handler */
1189 typedef struct QEMUResetEntry {
1190 QTAILQ_ENTRY(QEMUResetEntry) entry;
1191 QEMUResetHandler *func;
1192 void *opaque;
1193 } QEMUResetEntry;
1195 static QTAILQ_HEAD(reset_handlers, QEMUResetEntry) reset_handlers =
1196 QTAILQ_HEAD_INITIALIZER(reset_handlers);
1197 static int reset_requested;
1198 static int shutdown_requested;
1199 static int powerdown_requested;
1200 int debug_requested;
1201 int vmstop_requested;
1203 int qemu_shutdown_requested(void)
1205 int r = shutdown_requested;
1206 shutdown_requested = 0;
1207 return r;
1210 int qemu_reset_requested(void)
1212 int r = reset_requested;
1213 reset_requested = 0;
1214 return r;
1217 int qemu_powerdown_requested(void)
1219 int r = powerdown_requested;
1220 powerdown_requested = 0;
1221 return r;
1224 static int qemu_debug_requested(void)
1226 int r = debug_requested;
1227 debug_requested = 0;
1228 return r;
1231 static int qemu_vmstop_requested(void)
1233 int r = vmstop_requested;
1234 vmstop_requested = 0;
1235 return r;
1238 void qemu_register_reset(QEMUResetHandler *func, void *opaque)
1240 QEMUResetEntry *re = qemu_mallocz(sizeof(QEMUResetEntry));
1242 re->func = func;
1243 re->opaque = opaque;
1244 QTAILQ_INSERT_TAIL(&reset_handlers, re, entry);
1247 void qemu_unregister_reset(QEMUResetHandler *func, void *opaque)
1249 QEMUResetEntry *re;
1251 QTAILQ_FOREACH(re, &reset_handlers, entry) {
1252 if (re->func == func && re->opaque == opaque) {
1253 QTAILQ_REMOVE(&reset_handlers, re, entry);
1254 qemu_free(re);
1255 return;
1260 void qemu_system_reset(void)
1262 QEMUResetEntry *re, *nre;
1264 /* reset all devices */
1265 QTAILQ_FOREACH_SAFE(re, &reset_handlers, entry, nre) {
1266 re->func(re->opaque);
1268 monitor_protocol_event(QEVENT_RESET, NULL);
1269 cpu_synchronize_all_post_reset();
1272 void qemu_system_reset_request(void)
1274 if (no_reboot) {
1275 shutdown_requested = 1;
1276 } else {
1277 reset_requested = 1;
1279 qemu_notify_event();
1282 void qemu_system_shutdown_request(void)
1284 shutdown_requested = 1;
1285 qemu_notify_event();
1288 void qemu_system_powerdown_request(void)
1290 powerdown_requested = 1;
1291 qemu_notify_event();
1294 void main_loop_wait(int nonblocking)
1296 IOHandlerRecord *ioh;
1297 fd_set rfds, wfds, xfds;
1298 int ret, nfds;
1299 struct timeval tv;
1300 int timeout;
1302 if (nonblocking)
1303 timeout = 0;
1304 else {
1305 timeout = qemu_calculate_timeout();
1306 qemu_bh_update_timeout(&timeout);
1309 os_host_main_loop_wait(&timeout);
1311 /* poll any events */
1312 /* XXX: separate device handlers from system ones */
1313 nfds = -1;
1314 FD_ZERO(&rfds);
1315 FD_ZERO(&wfds);
1316 FD_ZERO(&xfds);
1317 QLIST_FOREACH(ioh, &io_handlers, next) {
1318 if (ioh->deleted)
1319 continue;
1320 if (ioh->fd_read &&
1321 (!ioh->fd_read_poll ||
1322 ioh->fd_read_poll(ioh->opaque) != 0)) {
1323 FD_SET(ioh->fd, &rfds);
1324 if (ioh->fd > nfds)
1325 nfds = ioh->fd;
1327 if (ioh->fd_write) {
1328 FD_SET(ioh->fd, &wfds);
1329 if (ioh->fd > nfds)
1330 nfds = ioh->fd;
1334 tv.tv_sec = timeout / 1000;
1335 tv.tv_usec = (timeout % 1000) * 1000;
1337 slirp_select_fill(&nfds, &rfds, &wfds, &xfds);
1339 qemu_mutex_unlock_iothread();
1340 ret = select(nfds + 1, &rfds, &wfds, &xfds, &tv);
1341 qemu_mutex_lock_iothread();
1342 if (ret > 0) {
1343 IOHandlerRecord *pioh;
1345 QLIST_FOREACH_SAFE(ioh, &io_handlers, next, pioh) {
1346 if (!ioh->deleted && ioh->fd_read && FD_ISSET(ioh->fd, &rfds)) {
1347 ioh->fd_read(ioh->opaque);
1349 if (!ioh->deleted && ioh->fd_write && FD_ISSET(ioh->fd, &wfds)) {
1350 ioh->fd_write(ioh->opaque);
1353 /* Do this last in case read/write handlers marked it for deletion */
1354 if (ioh->deleted) {
1355 QLIST_REMOVE(ioh, next);
1356 qemu_free(ioh);
1361 slirp_select_poll(&rfds, &wfds, &xfds, (ret < 0));
1363 qemu_run_all_timers();
1365 /* Check bottom-halves last in case any of the earlier events triggered
1366 them. */
1367 qemu_bh_poll();
1371 static int vm_can_run(void)
1373 if (powerdown_requested)
1374 return 0;
1375 if (reset_requested)
1376 return 0;
1377 if (shutdown_requested)
1378 return 0;
1379 if (debug_requested)
1380 return 0;
1381 return 1;
1384 qemu_irq qemu_system_powerdown;
1386 static void main_loop(void)
1388 int r;
1390 qemu_main_loop_start();
1392 for (;;) {
1393 do {
1394 bool nonblocking = false;
1395 #ifdef CONFIG_PROFILER
1396 int64_t ti;
1397 #endif
1398 #ifndef CONFIG_IOTHREAD
1399 nonblocking = cpu_exec_all();
1400 #endif
1401 #ifdef CONFIG_PROFILER
1402 ti = profile_getclock();
1403 #endif
1404 main_loop_wait(nonblocking);
1405 #ifdef CONFIG_PROFILER
1406 dev_time += profile_getclock() - ti;
1407 #endif
1408 } while (vm_can_run());
1410 if ((r = qemu_debug_requested())) {
1411 vm_stop(r);
1413 if (qemu_shutdown_requested()) {
1414 monitor_protocol_event(QEVENT_SHUTDOWN, NULL);
1415 if (no_shutdown) {
1416 vm_stop(0);
1417 no_shutdown = 0;
1418 } else
1419 break;
1421 if (qemu_reset_requested()) {
1422 pause_all_vcpus();
1423 qemu_system_reset();
1424 resume_all_vcpus();
1426 if (qemu_powerdown_requested()) {
1427 monitor_protocol_event(QEVENT_POWERDOWN, NULL);
1428 qemu_irq_raise(qemu_system_powerdown);
1430 if ((r = qemu_vmstop_requested())) {
1431 vm_stop(r);
1434 bdrv_close_all();
1435 pause_all_vcpus();
1438 static void version(void)
1440 printf("QEMU emulator version " QEMU_VERSION QEMU_PKGVERSION ", Copyright (c) 2003-2008 Fabrice Bellard\n");
1443 static void help(int exitcode)
1445 const char *options_help =
1446 #define DEF(option, opt_arg, opt_enum, opt_help, arch_mask) \
1447 opt_help
1448 #define DEFHEADING(text) stringify(text) "\n"
1449 #include "qemu-options.def"
1450 #undef DEF
1451 #undef DEFHEADING
1452 #undef GEN_DOCS
1454 version();
1455 printf("usage: %s [options] [disk_image]\n"
1456 "\n"
1457 "'disk_image' is a raw hard disk image for IDE hard disk 0\n"
1458 "\n"
1459 "%s\n"
1460 "During emulation, the following keys are useful:\n"
1461 "ctrl-alt-f toggle full screen\n"
1462 "ctrl-alt-n switch to virtual console 'n'\n"
1463 "ctrl-alt toggle mouse and keyboard grab\n"
1464 "\n"
1465 "When using -nographic, press 'ctrl-a h' to get some help.\n",
1466 "qemu",
1467 options_help);
1468 exit(exitcode);
1471 #define HAS_ARG 0x0001
1473 typedef struct QEMUOption {
1474 const char *name;
1475 int flags;
1476 int index;
1477 uint32_t arch_mask;
1478 } QEMUOption;
1480 static const QEMUOption qemu_options[] = {
1481 { "h", 0, QEMU_OPTION_h, QEMU_ARCH_ALL },
1482 #define DEF(option, opt_arg, opt_enum, opt_help, arch_mask) \
1483 { option, opt_arg, opt_enum, arch_mask },
1484 #define DEFHEADING(text)
1485 #include "qemu-options.def"
1486 #undef DEF
1487 #undef DEFHEADING
1488 #undef GEN_DOCS
1489 { NULL },
1491 static void select_vgahw (const char *p)
1493 const char *opts;
1495 default_vga = 0;
1496 vga_interface_type = VGA_NONE;
1497 if (strstart(p, "std", &opts)) {
1498 vga_interface_type = VGA_STD;
1499 } else if (strstart(p, "cirrus", &opts)) {
1500 vga_interface_type = VGA_CIRRUS;
1501 } else if (strstart(p, "vmware", &opts)) {
1502 vga_interface_type = VGA_VMWARE;
1503 } else if (strstart(p, "xenfb", &opts)) {
1504 vga_interface_type = VGA_XENFB;
1505 } else if (!strstart(p, "none", &opts)) {
1506 invalid_vga:
1507 fprintf(stderr, "Unknown vga type: %s\n", p);
1508 exit(1);
1510 while (*opts) {
1511 const char *nextopt;
1513 if (strstart(opts, ",retrace=", &nextopt)) {
1514 opts = nextopt;
1515 if (strstart(opts, "dumb", &nextopt))
1516 vga_retrace_method = VGA_RETRACE_DUMB;
1517 else if (strstart(opts, "precise", &nextopt))
1518 vga_retrace_method = VGA_RETRACE_PRECISE;
1519 else goto invalid_vga;
1520 } else goto invalid_vga;
1521 opts = nextopt;
1525 static int balloon_parse(const char *arg)
1527 QemuOpts *opts;
1529 if (strcmp(arg, "none") == 0) {
1530 return 0;
1533 if (!strncmp(arg, "virtio", 6)) {
1534 if (arg[6] == ',') {
1535 /* have params -> parse them */
1536 opts = qemu_opts_parse(qemu_find_opts("device"), arg+7, 0);
1537 if (!opts)
1538 return -1;
1539 } else {
1540 /* create empty opts */
1541 opts = qemu_opts_create(qemu_find_opts("device"), NULL, 0);
1543 qemu_opt_set(opts, "driver", "virtio-balloon-pci");
1544 return 0;
1547 return -1;
1550 char *qemu_find_file(int type, const char *name)
1552 int len;
1553 const char *subdir;
1554 char *buf;
1556 /* If name contains path separators then try it as a straight path. */
1557 if ((strchr(name, '/') || strchr(name, '\\'))
1558 && access(name, R_OK) == 0) {
1559 return qemu_strdup(name);
1561 switch (type) {
1562 case QEMU_FILE_TYPE_BIOS:
1563 subdir = "";
1564 break;
1565 case QEMU_FILE_TYPE_KEYMAP:
1566 subdir = "keymaps/";
1567 break;
1568 default:
1569 abort();
1571 len = strlen(data_dir) + strlen(name) + strlen(subdir) + 2;
1572 buf = qemu_mallocz(len);
1573 snprintf(buf, len, "%s/%s%s", data_dir, subdir, name);
1574 if (access(buf, R_OK)) {
1575 qemu_free(buf);
1576 return NULL;
1578 return buf;
1581 static int device_help_func(QemuOpts *opts, void *opaque)
1583 return qdev_device_help(opts);
1586 static int device_init_func(QemuOpts *opts, void *opaque)
1588 DeviceState *dev;
1590 dev = qdev_device_add(opts);
1591 if (!dev)
1592 return -1;
1593 return 0;
1596 static int chardev_init_func(QemuOpts *opts, void *opaque)
1598 CharDriverState *chr;
1600 chr = qemu_chr_open_opts(opts, NULL);
1601 if (!chr)
1602 return -1;
1603 return 0;
1606 #ifdef CONFIG_VIRTFS
1607 static int fsdev_init_func(QemuOpts *opts, void *opaque)
1609 int ret;
1610 ret = qemu_fsdev_add(opts);
1612 return ret;
1614 #endif
1616 static int mon_init_func(QemuOpts *opts, void *opaque)
1618 CharDriverState *chr;
1619 const char *chardev;
1620 const char *mode;
1621 int flags;
1623 mode = qemu_opt_get(opts, "mode");
1624 if (mode == NULL) {
1625 mode = "readline";
1627 if (strcmp(mode, "readline") == 0) {
1628 flags = MONITOR_USE_READLINE;
1629 } else if (strcmp(mode, "control") == 0) {
1630 flags = MONITOR_USE_CONTROL;
1631 } else {
1632 fprintf(stderr, "unknown monitor mode \"%s\"\n", mode);
1633 exit(1);
1636 if (qemu_opt_get_bool(opts, "pretty", 0))
1637 flags |= MONITOR_USE_PRETTY;
1639 if (qemu_opt_get_bool(opts, "default", 0))
1640 flags |= MONITOR_IS_DEFAULT;
1642 chardev = qemu_opt_get(opts, "chardev");
1643 chr = qemu_chr_find(chardev);
1644 if (chr == NULL) {
1645 fprintf(stderr, "chardev \"%s\" not found\n", chardev);
1646 exit(1);
1649 monitor_init(chr, flags);
1650 return 0;
1653 static void monitor_parse(const char *optarg, const char *mode)
1655 static int monitor_device_index = 0;
1656 QemuOpts *opts;
1657 const char *p;
1658 char label[32];
1659 int def = 0;
1661 if (strstart(optarg, "chardev:", &p)) {
1662 snprintf(label, sizeof(label), "%s", p);
1663 } else {
1664 snprintf(label, sizeof(label), "compat_monitor%d",
1665 monitor_device_index);
1666 if (monitor_device_index == 0) {
1667 def = 1;
1669 opts = qemu_chr_parse_compat(label, optarg);
1670 if (!opts) {
1671 fprintf(stderr, "parse error: %s\n", optarg);
1672 exit(1);
1676 opts = qemu_opts_create(qemu_find_opts("mon"), label, 1);
1677 if (!opts) {
1678 fprintf(stderr, "duplicate chardev: %s\n", label);
1679 exit(1);
1681 qemu_opt_set(opts, "mode", mode);
1682 qemu_opt_set(opts, "chardev", label);
1683 if (def)
1684 qemu_opt_set(opts, "default", "on");
1685 monitor_device_index++;
1688 struct device_config {
1689 enum {
1690 DEV_USB, /* -usbdevice */
1691 DEV_BT, /* -bt */
1692 DEV_SERIAL, /* -serial */
1693 DEV_PARALLEL, /* -parallel */
1694 DEV_VIRTCON, /* -virtioconsole */
1695 DEV_DEBUGCON, /* -debugcon */
1696 } type;
1697 const char *cmdline;
1698 QTAILQ_ENTRY(device_config) next;
1700 QTAILQ_HEAD(, device_config) device_configs = QTAILQ_HEAD_INITIALIZER(device_configs);
1702 static void add_device_config(int type, const char *cmdline)
1704 struct device_config *conf;
1706 conf = qemu_mallocz(sizeof(*conf));
1707 conf->type = type;
1708 conf->cmdline = cmdline;
1709 QTAILQ_INSERT_TAIL(&device_configs, conf, next);
1712 static int foreach_device_config(int type, int (*func)(const char *cmdline))
1714 struct device_config *conf;
1715 int rc;
1717 QTAILQ_FOREACH(conf, &device_configs, next) {
1718 if (conf->type != type)
1719 continue;
1720 rc = func(conf->cmdline);
1721 if (0 != rc)
1722 return rc;
1724 return 0;
1727 static int serial_parse(const char *devname)
1729 static int index = 0;
1730 char label[32];
1732 if (strcmp(devname, "none") == 0)
1733 return 0;
1734 if (index == MAX_SERIAL_PORTS) {
1735 fprintf(stderr, "qemu: too many serial ports\n");
1736 exit(1);
1738 snprintf(label, sizeof(label), "serial%d", index);
1739 serial_hds[index] = qemu_chr_open(label, devname, NULL);
1740 if (!serial_hds[index]) {
1741 fprintf(stderr, "qemu: could not open serial device '%s': %s\n",
1742 devname, strerror(errno));
1743 return -1;
1745 index++;
1746 return 0;
1749 static int parallel_parse(const char *devname)
1751 static int index = 0;
1752 char label[32];
1754 if (strcmp(devname, "none") == 0)
1755 return 0;
1756 if (index == MAX_PARALLEL_PORTS) {
1757 fprintf(stderr, "qemu: too many parallel ports\n");
1758 exit(1);
1760 snprintf(label, sizeof(label), "parallel%d", index);
1761 parallel_hds[index] = qemu_chr_open(label, devname, NULL);
1762 if (!parallel_hds[index]) {
1763 fprintf(stderr, "qemu: could not open parallel device '%s': %s\n",
1764 devname, strerror(errno));
1765 return -1;
1767 index++;
1768 return 0;
1771 static int virtcon_parse(const char *devname)
1773 QemuOptsList *device = qemu_find_opts("device");
1774 static int index = 0;
1775 char label[32];
1776 QemuOpts *bus_opts, *dev_opts;
1778 if (strcmp(devname, "none") == 0)
1779 return 0;
1780 if (index == MAX_VIRTIO_CONSOLES) {
1781 fprintf(stderr, "qemu: too many virtio consoles\n");
1782 exit(1);
1785 bus_opts = qemu_opts_create(device, NULL, 0);
1786 qemu_opt_set(bus_opts, "driver", "virtio-serial");
1788 dev_opts = qemu_opts_create(device, NULL, 0);
1789 qemu_opt_set(dev_opts, "driver", "virtconsole");
1791 snprintf(label, sizeof(label), "virtcon%d", index);
1792 virtcon_hds[index] = qemu_chr_open(label, devname, NULL);
1793 if (!virtcon_hds[index]) {
1794 fprintf(stderr, "qemu: could not open virtio console '%s': %s\n",
1795 devname, strerror(errno));
1796 return -1;
1798 qemu_opt_set(dev_opts, "chardev", label);
1800 index++;
1801 return 0;
1804 static int debugcon_parse(const char *devname)
1806 QemuOpts *opts;
1808 if (!qemu_chr_open("debugcon", devname, NULL)) {
1809 exit(1);
1811 opts = qemu_opts_create(qemu_find_opts("device"), "debugcon", 1);
1812 if (!opts) {
1813 fprintf(stderr, "qemu: already have a debugcon device\n");
1814 exit(1);
1816 qemu_opt_set(opts, "driver", "isa-debugcon");
1817 qemu_opt_set(opts, "chardev", "debugcon");
1818 return 0;
1821 void qemu_add_exit_notifier(Notifier *notify)
1823 notifier_list_add(&exit_notifiers, notify);
1826 void qemu_remove_exit_notifier(Notifier *notify)
1828 notifier_list_remove(&exit_notifiers, notify);
1831 static void qemu_run_exit_notifiers(void)
1833 notifier_list_notify(&exit_notifiers);
1836 void qemu_add_machine_init_done_notifier(Notifier *notify)
1838 notifier_list_add(&machine_init_done_notifiers, notify);
1841 static void qemu_run_machine_init_done_notifiers(void)
1843 notifier_list_notify(&machine_init_done_notifiers);
1846 static const QEMUOption *lookup_opt(int argc, char **argv,
1847 const char **poptarg, int *poptind)
1849 const QEMUOption *popt;
1850 int optind = *poptind;
1851 char *r = argv[optind];
1852 const char *optarg;
1854 loc_set_cmdline(argv, optind, 1);
1855 optind++;
1856 /* Treat --foo the same as -foo. */
1857 if (r[1] == '-')
1858 r++;
1859 popt = qemu_options;
1860 for(;;) {
1861 if (!popt->name) {
1862 error_report("invalid option");
1863 exit(1);
1865 if (!strcmp(popt->name, r + 1))
1866 break;
1867 popt++;
1869 if (popt->flags & HAS_ARG) {
1870 if (optind >= argc) {
1871 error_report("requires an argument");
1872 exit(1);
1874 optarg = argv[optind++];
1875 loc_set_cmdline(argv, optind - 2, 2);
1876 } else {
1877 optarg = NULL;
1880 *poptarg = optarg;
1881 *poptind = optind;
1883 return popt;
1886 int main(int argc, char **argv, char **envp)
1888 const char *gdbstub_dev = NULL;
1889 int i;
1890 int snapshot, linux_boot;
1891 const char *icount_option = NULL;
1892 const char *initrd_filename;
1893 const char *kernel_filename, *kernel_cmdline;
1894 char boot_devices[33] = "cad"; /* default to HD->floppy->CD-ROM */
1895 DisplayState *ds;
1896 DisplayChangeListener *dcl;
1897 int cyls, heads, secs, translation;
1898 QemuOpts *hda_opts = NULL, *opts;
1899 QemuOptsList *olist;
1900 int optind;
1901 const char *optarg;
1902 const char *loadvm = NULL;
1903 QEMUMachine *machine;
1904 const char *cpu_model;
1905 int tb_size;
1906 const char *pid_file = NULL;
1907 const char *incoming = NULL;
1908 int show_vnc_port = 0;
1909 int defconfig = 1;
1911 #ifdef CONFIG_SIMPLE_TRACE
1912 const char *trace_file = NULL;
1913 #endif
1914 atexit(qemu_run_exit_notifiers);
1915 error_set_progname(argv[0]);
1917 init_clocks();
1919 qemu_cache_utils_init(envp);
1921 QLIST_INIT (&vm_change_state_head);
1922 os_setup_early_signal_handling();
1924 module_call_init(MODULE_INIT_MACHINE);
1925 machine = find_default_machine();
1926 cpu_model = NULL;
1927 initrd_filename = NULL;
1928 ram_size = 0;
1929 snapshot = 0;
1930 kernel_filename = NULL;
1931 kernel_cmdline = "";
1932 cyls = heads = secs = 0;
1933 translation = BIOS_ATA_TRANSLATION_AUTO;
1935 for (i = 0; i < MAX_NODES; i++) {
1936 node_mem[i] = 0;
1937 node_cpumask[i] = 0;
1940 nb_numa_nodes = 0;
1941 nb_nics = 0;
1943 tb_size = 0;
1944 autostart= 1;
1946 /* first pass of option parsing */
1947 optind = 1;
1948 while (optind < argc) {
1949 if (argv[optind][0] != '-') {
1950 /* disk image */
1951 optind++;
1952 continue;
1953 } else {
1954 const QEMUOption *popt;
1956 popt = lookup_opt(argc, argv, &optarg, &optind);
1957 switch (popt->index) {
1958 case QEMU_OPTION_nodefconfig:
1959 defconfig=0;
1960 break;
1965 if (defconfig) {
1966 int ret;
1968 ret = qemu_read_config_file(CONFIG_QEMU_CONFDIR "/qemu.conf");
1969 if (ret < 0 && ret != -ENOENT) {
1970 exit(1);
1973 ret = qemu_read_config_file(arch_config_name);
1974 if (ret < 0 && ret != -ENOENT) {
1975 exit(1);
1978 cpudef_init();
1980 /* second pass of option parsing */
1981 optind = 1;
1982 for(;;) {
1983 if (optind >= argc)
1984 break;
1985 if (argv[optind][0] != '-') {
1986 hda_opts = drive_add(argv[optind++], HD_ALIAS, 0);
1987 } else {
1988 const QEMUOption *popt;
1990 popt = lookup_opt(argc, argv, &optarg, &optind);
1991 if (!(popt->arch_mask & arch_type)) {
1992 printf("Option %s not supported for this target\n", popt->name);
1993 exit(1);
1995 switch(popt->index) {
1996 case QEMU_OPTION_M:
1997 machine = find_machine(optarg);
1998 if (!machine) {
1999 QEMUMachine *m;
2000 printf("Supported machines are:\n");
2001 for(m = first_machine; m != NULL; m = m->next) {
2002 if (m->alias)
2003 printf("%-10s %s (alias of %s)\n",
2004 m->alias, m->desc, m->name);
2005 printf("%-10s %s%s\n",
2006 m->name, m->desc,
2007 m->is_default ? " (default)" : "");
2009 exit(*optarg != '?');
2011 break;
2012 case QEMU_OPTION_cpu:
2013 /* hw initialization will check this */
2014 if (*optarg == '?') {
2015 list_cpus(stdout, &fprintf, optarg);
2016 exit(0);
2017 } else {
2018 cpu_model = optarg;
2020 break;
2021 case QEMU_OPTION_initrd:
2022 initrd_filename = optarg;
2023 break;
2024 case QEMU_OPTION_hda:
2025 if (cyls == 0)
2026 hda_opts = drive_add(optarg, HD_ALIAS, 0);
2027 else
2028 hda_opts = drive_add(optarg, HD_ALIAS
2029 ",cyls=%d,heads=%d,secs=%d%s",
2030 0, cyls, heads, secs,
2031 translation == BIOS_ATA_TRANSLATION_LBA ?
2032 ",trans=lba" :
2033 translation == BIOS_ATA_TRANSLATION_NONE ?
2034 ",trans=none" : "");
2035 break;
2036 case QEMU_OPTION_hdb:
2037 case QEMU_OPTION_hdc:
2038 case QEMU_OPTION_hdd:
2039 drive_add(optarg, HD_ALIAS, popt->index - QEMU_OPTION_hda);
2040 break;
2041 case QEMU_OPTION_drive:
2042 drive_add(NULL, "%s", optarg);
2043 break;
2044 case QEMU_OPTION_set:
2045 if (qemu_set_option(optarg) != 0)
2046 exit(1);
2047 break;
2048 case QEMU_OPTION_global:
2049 if (qemu_global_option(optarg) != 0)
2050 exit(1);
2051 break;
2052 case QEMU_OPTION_mtdblock:
2053 drive_add(optarg, MTD_ALIAS);
2054 break;
2055 case QEMU_OPTION_sd:
2056 drive_add(optarg, SD_ALIAS);
2057 break;
2058 case QEMU_OPTION_pflash:
2059 drive_add(optarg, PFLASH_ALIAS);
2060 break;
2061 case QEMU_OPTION_snapshot:
2062 snapshot = 1;
2063 break;
2064 case QEMU_OPTION_hdachs:
2066 const char *p;
2067 p = optarg;
2068 cyls = strtol(p, (char **)&p, 0);
2069 if (cyls < 1 || cyls > 16383)
2070 goto chs_fail;
2071 if (*p != ',')
2072 goto chs_fail;
2073 p++;
2074 heads = strtol(p, (char **)&p, 0);
2075 if (heads < 1 || heads > 16)
2076 goto chs_fail;
2077 if (*p != ',')
2078 goto chs_fail;
2079 p++;
2080 secs = strtol(p, (char **)&p, 0);
2081 if (secs < 1 || secs > 63)
2082 goto chs_fail;
2083 if (*p == ',') {
2084 p++;
2085 if (!strcmp(p, "none"))
2086 translation = BIOS_ATA_TRANSLATION_NONE;
2087 else if (!strcmp(p, "lba"))
2088 translation = BIOS_ATA_TRANSLATION_LBA;
2089 else if (!strcmp(p, "auto"))
2090 translation = BIOS_ATA_TRANSLATION_AUTO;
2091 else
2092 goto chs_fail;
2093 } else if (*p != '\0') {
2094 chs_fail:
2095 fprintf(stderr, "qemu: invalid physical CHS format\n");
2096 exit(1);
2098 if (hda_opts != NULL) {
2099 char num[16];
2100 snprintf(num, sizeof(num), "%d", cyls);
2101 qemu_opt_set(hda_opts, "cyls", num);
2102 snprintf(num, sizeof(num), "%d", heads);
2103 qemu_opt_set(hda_opts, "heads", num);
2104 snprintf(num, sizeof(num), "%d", secs);
2105 qemu_opt_set(hda_opts, "secs", num);
2106 if (translation == BIOS_ATA_TRANSLATION_LBA)
2107 qemu_opt_set(hda_opts, "trans", "lba");
2108 if (translation == BIOS_ATA_TRANSLATION_NONE)
2109 qemu_opt_set(hda_opts, "trans", "none");
2112 break;
2113 case QEMU_OPTION_numa:
2114 if (nb_numa_nodes >= MAX_NODES) {
2115 fprintf(stderr, "qemu: too many NUMA nodes\n");
2116 exit(1);
2118 numa_add(optarg);
2119 break;
2120 case QEMU_OPTION_nographic:
2121 display_type = DT_NOGRAPHIC;
2122 break;
2123 #ifdef CONFIG_CURSES
2124 case QEMU_OPTION_curses:
2125 display_type = DT_CURSES;
2126 break;
2127 #endif
2128 case QEMU_OPTION_portrait:
2129 graphic_rotate = 1;
2130 break;
2131 case QEMU_OPTION_kernel:
2132 kernel_filename = optarg;
2133 break;
2134 case QEMU_OPTION_append:
2135 kernel_cmdline = optarg;
2136 break;
2137 case QEMU_OPTION_cdrom:
2138 drive_add(optarg, CDROM_ALIAS);
2139 break;
2140 case QEMU_OPTION_boot:
2142 static const char * const params[] = {
2143 "order", "once", "menu", NULL
2145 char buf[sizeof(boot_devices)];
2146 char *standard_boot_devices;
2147 int legacy = 0;
2149 if (!strchr(optarg, '=')) {
2150 legacy = 1;
2151 pstrcpy(buf, sizeof(buf), optarg);
2152 } else if (check_params(buf, sizeof(buf), params, optarg) < 0) {
2153 fprintf(stderr,
2154 "qemu: unknown boot parameter '%s' in '%s'\n",
2155 buf, optarg);
2156 exit(1);
2159 if (legacy ||
2160 get_param_value(buf, sizeof(buf), "order", optarg)) {
2161 validate_bootdevices(buf);
2162 pstrcpy(boot_devices, sizeof(boot_devices), buf);
2164 if (!legacy) {
2165 if (get_param_value(buf, sizeof(buf),
2166 "once", optarg)) {
2167 validate_bootdevices(buf);
2168 standard_boot_devices = qemu_strdup(boot_devices);
2169 pstrcpy(boot_devices, sizeof(boot_devices), buf);
2170 qemu_register_reset(restore_boot_devices,
2171 standard_boot_devices);
2173 if (get_param_value(buf, sizeof(buf),
2174 "menu", optarg)) {
2175 if (!strcmp(buf, "on")) {
2176 boot_menu = 1;
2177 } else if (!strcmp(buf, "off")) {
2178 boot_menu = 0;
2179 } else {
2180 fprintf(stderr,
2181 "qemu: invalid option value '%s'\n",
2182 buf);
2183 exit(1);
2188 break;
2189 case QEMU_OPTION_fda:
2190 case QEMU_OPTION_fdb:
2191 drive_add(optarg, FD_ALIAS, popt->index - QEMU_OPTION_fda);
2192 break;
2193 case QEMU_OPTION_no_fd_bootchk:
2194 fd_bootchk = 0;
2195 break;
2196 case QEMU_OPTION_netdev:
2197 if (net_client_parse(qemu_find_opts("netdev"), optarg) == -1) {
2198 exit(1);
2200 break;
2201 case QEMU_OPTION_net:
2202 if (net_client_parse(qemu_find_opts("net"), optarg) == -1) {
2203 exit(1);
2205 break;
2206 #ifdef CONFIG_SLIRP
2207 case QEMU_OPTION_tftp:
2208 legacy_tftp_prefix = optarg;
2209 break;
2210 case QEMU_OPTION_bootp:
2211 legacy_bootp_filename = optarg;
2212 break;
2213 case QEMU_OPTION_redir:
2214 if (net_slirp_redir(optarg) < 0)
2215 exit(1);
2216 break;
2217 #endif
2218 case QEMU_OPTION_bt:
2219 add_device_config(DEV_BT, optarg);
2220 break;
2221 case QEMU_OPTION_audio_help:
2222 if (!(audio_available())) {
2223 printf("Option %s not supported for this target\n", popt->name);
2224 exit(1);
2226 AUD_help ();
2227 exit (0);
2228 break;
2229 case QEMU_OPTION_soundhw:
2230 if (!(audio_available())) {
2231 printf("Option %s not supported for this target\n", popt->name);
2232 exit(1);
2234 select_soundhw (optarg);
2235 break;
2236 case QEMU_OPTION_h:
2237 help(0);
2238 break;
2239 case QEMU_OPTION_version:
2240 version();
2241 exit(0);
2242 break;
2243 case QEMU_OPTION_m: {
2244 ssize_t value;
2246 value = strtosz(optarg, NULL);
2247 if (value < 0) {
2248 fprintf(stderr, "qemu: invalid ram size: %s\n", optarg);
2249 exit(1);
2252 /* On 32-bit hosts, QEMU is limited by virtual address space */
2253 if (value > (2047 << 20) && HOST_LONG_BITS == 32) {
2254 fprintf(stderr, "qemu: at most 2047 MB RAM can be simulated\n");
2255 exit(1);
2257 if (value != (uint64_t)(ram_addr_t)value) {
2258 fprintf(stderr, "qemu: ram size too large\n");
2259 exit(1);
2261 ram_size = value;
2262 break;
2264 case QEMU_OPTION_mempath:
2265 mem_path = optarg;
2266 break;
2267 #ifdef MAP_POPULATE
2268 case QEMU_OPTION_mem_prealloc:
2269 mem_prealloc = 1;
2270 break;
2271 #endif
2272 case QEMU_OPTION_d:
2273 set_cpu_log(optarg);
2274 break;
2275 case QEMU_OPTION_s:
2276 gdbstub_dev = "tcp::" DEFAULT_GDBSTUB_PORT;
2277 break;
2278 case QEMU_OPTION_gdb:
2279 gdbstub_dev = optarg;
2280 break;
2281 case QEMU_OPTION_L:
2282 data_dir = optarg;
2283 break;
2284 case QEMU_OPTION_bios:
2285 bios_name = optarg;
2286 break;
2287 case QEMU_OPTION_singlestep:
2288 singlestep = 1;
2289 break;
2290 case QEMU_OPTION_S:
2291 autostart = 0;
2292 break;
2293 case QEMU_OPTION_k:
2294 keyboard_layout = optarg;
2295 break;
2296 case QEMU_OPTION_localtime:
2297 rtc_utc = 0;
2298 break;
2299 case QEMU_OPTION_vga:
2300 select_vgahw (optarg);
2301 break;
2302 case QEMU_OPTION_g:
2304 const char *p;
2305 int w, h, depth;
2306 p = optarg;
2307 w = strtol(p, (char **)&p, 10);
2308 if (w <= 0) {
2309 graphic_error:
2310 fprintf(stderr, "qemu: invalid resolution or depth\n");
2311 exit(1);
2313 if (*p != 'x')
2314 goto graphic_error;
2315 p++;
2316 h = strtol(p, (char **)&p, 10);
2317 if (h <= 0)
2318 goto graphic_error;
2319 if (*p == 'x') {
2320 p++;
2321 depth = strtol(p, (char **)&p, 10);
2322 if (depth != 8 && depth != 15 && depth != 16 &&
2323 depth != 24 && depth != 32)
2324 goto graphic_error;
2325 } else if (*p == '\0') {
2326 depth = graphic_depth;
2327 } else {
2328 goto graphic_error;
2331 graphic_width = w;
2332 graphic_height = h;
2333 graphic_depth = depth;
2335 break;
2336 case QEMU_OPTION_echr:
2338 char *r;
2339 term_escape_char = strtol(optarg, &r, 0);
2340 if (r == optarg)
2341 printf("Bad argument to echr\n");
2342 break;
2344 case QEMU_OPTION_monitor:
2345 monitor_parse(optarg, "readline");
2346 default_monitor = 0;
2347 break;
2348 case QEMU_OPTION_qmp:
2349 monitor_parse(optarg, "control");
2350 default_monitor = 0;
2351 break;
2352 case QEMU_OPTION_mon:
2353 opts = qemu_opts_parse(qemu_find_opts("mon"), optarg, 1);
2354 if (!opts) {
2355 exit(1);
2357 default_monitor = 0;
2358 break;
2359 case QEMU_OPTION_chardev:
2360 opts = qemu_opts_parse(qemu_find_opts("chardev"), optarg, 1);
2361 if (!opts) {
2362 exit(1);
2364 break;
2365 case QEMU_OPTION_fsdev:
2366 olist = qemu_find_opts("fsdev");
2367 if (!olist) {
2368 fprintf(stderr, "fsdev is not supported by this qemu build.\n");
2369 exit(1);
2371 opts = qemu_opts_parse(olist, optarg, 1);
2372 if (!opts) {
2373 fprintf(stderr, "parse error: %s\n", optarg);
2374 exit(1);
2376 break;
2377 case QEMU_OPTION_virtfs: {
2378 char *arg_fsdev = NULL;
2379 char *arg_9p = NULL;
2380 int len = 0;
2382 olist = qemu_find_opts("virtfs");
2383 if (!olist) {
2384 fprintf(stderr, "virtfs is not supported by this qemu build.\n");
2385 exit(1);
2387 opts = qemu_opts_parse(olist, optarg, 1);
2388 if (!opts) {
2389 fprintf(stderr, "parse error: %s\n", optarg);
2390 exit(1);
2393 if (qemu_opt_get(opts, "fstype") == NULL ||
2394 qemu_opt_get(opts, "mount_tag") == NULL ||
2395 qemu_opt_get(opts, "path") == NULL ||
2396 qemu_opt_get(opts, "security_model") == NULL) {
2397 fprintf(stderr, "Usage: -virtfs fstype,path=/share_path/,"
2398 "security_model=[mapped|passthrough|none],"
2399 "mnt_tag=tag.\n");
2400 exit(1);
2403 len = strlen(",id=,path=,security_model=");
2404 len += strlen(qemu_opt_get(opts, "fstype"));
2405 len += strlen(qemu_opt_get(opts, "mount_tag"));
2406 len += strlen(qemu_opt_get(opts, "path"));
2407 len += strlen(qemu_opt_get(opts, "security_model"));
2408 arg_fsdev = qemu_malloc((len + 1) * sizeof(*arg_fsdev));
2410 snprintf(arg_fsdev, (len + 1) * sizeof(*arg_fsdev),
2411 "%s,id=%s,path=%s,security_model=%s",
2412 qemu_opt_get(opts, "fstype"),
2413 qemu_opt_get(opts, "mount_tag"),
2414 qemu_opt_get(opts, "path"),
2415 qemu_opt_get(opts, "security_model"));
2417 len = strlen("virtio-9p-pci,fsdev=,mount_tag=");
2418 len += 2*strlen(qemu_opt_get(opts, "mount_tag"));
2419 arg_9p = qemu_malloc((len + 1) * sizeof(*arg_9p));
2421 snprintf(arg_9p, (len + 1) * sizeof(*arg_9p),
2422 "virtio-9p-pci,fsdev=%s,mount_tag=%s",
2423 qemu_opt_get(opts, "mount_tag"),
2424 qemu_opt_get(opts, "mount_tag"));
2426 if (!qemu_opts_parse(qemu_find_opts("fsdev"), arg_fsdev, 1)) {
2427 fprintf(stderr, "parse error [fsdev]: %s\n", optarg);
2428 exit(1);
2431 if (!qemu_opts_parse(qemu_find_opts("device"), arg_9p, 1)) {
2432 fprintf(stderr, "parse error [device]: %s\n", optarg);
2433 exit(1);
2436 qemu_free(arg_fsdev);
2437 qemu_free(arg_9p);
2438 break;
2440 case QEMU_OPTION_serial:
2441 add_device_config(DEV_SERIAL, optarg);
2442 default_serial = 0;
2443 if (strncmp(optarg, "mon:", 4) == 0) {
2444 default_monitor = 0;
2446 break;
2447 case QEMU_OPTION_watchdog:
2448 if (watchdog) {
2449 fprintf(stderr,
2450 "qemu: only one watchdog option may be given\n");
2451 return 1;
2453 watchdog = optarg;
2454 break;
2455 case QEMU_OPTION_watchdog_action:
2456 if (select_watchdog_action(optarg) == -1) {
2457 fprintf(stderr, "Unknown -watchdog-action parameter\n");
2458 exit(1);
2460 break;
2461 case QEMU_OPTION_virtiocon:
2462 add_device_config(DEV_VIRTCON, optarg);
2463 default_virtcon = 0;
2464 if (strncmp(optarg, "mon:", 4) == 0) {
2465 default_monitor = 0;
2467 break;
2468 case QEMU_OPTION_parallel:
2469 add_device_config(DEV_PARALLEL, optarg);
2470 default_parallel = 0;
2471 if (strncmp(optarg, "mon:", 4) == 0) {
2472 default_monitor = 0;
2474 break;
2475 case QEMU_OPTION_debugcon:
2476 add_device_config(DEV_DEBUGCON, optarg);
2477 break;
2478 case QEMU_OPTION_loadvm:
2479 loadvm = optarg;
2480 break;
2481 case QEMU_OPTION_full_screen:
2482 full_screen = 1;
2483 break;
2484 #ifdef CONFIG_SDL
2485 case QEMU_OPTION_no_frame:
2486 no_frame = 1;
2487 break;
2488 case QEMU_OPTION_alt_grab:
2489 alt_grab = 1;
2490 break;
2491 case QEMU_OPTION_ctrl_grab:
2492 ctrl_grab = 1;
2493 break;
2494 case QEMU_OPTION_no_quit:
2495 no_quit = 1;
2496 break;
2497 case QEMU_OPTION_sdl:
2498 display_type = DT_SDL;
2499 break;
2500 #endif
2501 case QEMU_OPTION_pidfile:
2502 pid_file = optarg;
2503 break;
2504 case QEMU_OPTION_win2k_hack:
2505 win2k_install_hack = 1;
2506 break;
2507 case QEMU_OPTION_rtc_td_hack:
2508 rtc_td_hack = 1;
2509 break;
2510 case QEMU_OPTION_acpitable:
2511 do_acpitable_option(optarg);
2512 break;
2513 case QEMU_OPTION_smbios:
2514 do_smbios_option(optarg);
2515 break;
2516 case QEMU_OPTION_enable_kvm:
2517 kvm_allowed = 1;
2518 break;
2519 case QEMU_OPTION_usb:
2520 usb_enabled = 1;
2521 break;
2522 case QEMU_OPTION_usbdevice:
2523 usb_enabled = 1;
2524 add_device_config(DEV_USB, optarg);
2525 break;
2526 case QEMU_OPTION_device:
2527 if (!qemu_opts_parse(qemu_find_opts("device"), optarg, 1)) {
2528 exit(1);
2530 break;
2531 case QEMU_OPTION_smp:
2532 smp_parse(optarg);
2533 if (smp_cpus < 1) {
2534 fprintf(stderr, "Invalid number of CPUs\n");
2535 exit(1);
2537 if (max_cpus < smp_cpus) {
2538 fprintf(stderr, "maxcpus must be equal to or greater than "
2539 "smp\n");
2540 exit(1);
2542 if (max_cpus > 255) {
2543 fprintf(stderr, "Unsupported number of maxcpus\n");
2544 exit(1);
2546 break;
2547 case QEMU_OPTION_vnc:
2548 display_remote++;
2549 vnc_display = optarg;
2550 break;
2551 case QEMU_OPTION_no_acpi:
2552 acpi_enabled = 0;
2553 break;
2554 case QEMU_OPTION_no_hpet:
2555 no_hpet = 1;
2556 break;
2557 case QEMU_OPTION_balloon:
2558 if (balloon_parse(optarg) < 0) {
2559 fprintf(stderr, "Unknown -balloon argument %s\n", optarg);
2560 exit(1);
2562 break;
2563 case QEMU_OPTION_no_reboot:
2564 no_reboot = 1;
2565 break;
2566 case QEMU_OPTION_no_shutdown:
2567 no_shutdown = 1;
2568 break;
2569 case QEMU_OPTION_show_cursor:
2570 cursor_hide = 0;
2571 break;
2572 case QEMU_OPTION_uuid:
2573 if(qemu_uuid_parse(optarg, qemu_uuid) < 0) {
2574 fprintf(stderr, "Fail to parse UUID string."
2575 " Wrong format.\n");
2576 exit(1);
2578 break;
2579 case QEMU_OPTION_option_rom:
2580 if (nb_option_roms >= MAX_OPTION_ROMS) {
2581 fprintf(stderr, "Too many option ROMs\n");
2582 exit(1);
2584 opts = qemu_opts_parse(qemu_find_opts("option-rom"), optarg, 1);
2585 option_rom[nb_option_roms].name = qemu_opt_get(opts, "romfile");
2586 option_rom[nb_option_roms].bootindex =
2587 qemu_opt_get_number(opts, "bootindex", -1);
2588 if (!option_rom[nb_option_roms].name) {
2589 fprintf(stderr, "Option ROM file is not specified\n");
2590 exit(1);
2592 nb_option_roms++;
2593 break;
2594 case QEMU_OPTION_semihosting:
2595 semihosting_enabled = 1;
2596 break;
2597 case QEMU_OPTION_name:
2598 qemu_name = qemu_strdup(optarg);
2600 char *p = strchr(qemu_name, ',');
2601 if (p != NULL) {
2602 *p++ = 0;
2603 if (strncmp(p, "process=", 8)) {
2604 fprintf(stderr, "Unknown subargument %s to -name", p);
2605 exit(1);
2607 p += 8;
2608 os_set_proc_name(p);
2611 break;
2612 case QEMU_OPTION_prom_env:
2613 if (nb_prom_envs >= MAX_PROM_ENVS) {
2614 fprintf(stderr, "Too many prom variables\n");
2615 exit(1);
2617 prom_envs[nb_prom_envs] = optarg;
2618 nb_prom_envs++;
2619 break;
2620 case QEMU_OPTION_old_param:
2621 old_param = 1;
2622 break;
2623 case QEMU_OPTION_clock:
2624 configure_alarms(optarg);
2625 break;
2626 case QEMU_OPTION_startdate:
2627 configure_rtc_date_offset(optarg, 1);
2628 break;
2629 case QEMU_OPTION_rtc:
2630 opts = qemu_opts_parse(qemu_find_opts("rtc"), optarg, 0);
2631 if (!opts) {
2632 exit(1);
2634 configure_rtc(opts);
2635 break;
2636 case QEMU_OPTION_tb_size:
2637 tb_size = strtol(optarg, NULL, 0);
2638 if (tb_size < 0)
2639 tb_size = 0;
2640 break;
2641 case QEMU_OPTION_icount:
2642 icount_option = optarg;
2643 break;
2644 case QEMU_OPTION_incoming:
2645 incoming = optarg;
2646 incoming_expected = true;
2647 break;
2648 case QEMU_OPTION_nodefaults:
2649 default_serial = 0;
2650 default_parallel = 0;
2651 default_virtcon = 0;
2652 default_monitor = 0;
2653 default_vga = 0;
2654 default_net = 0;
2655 default_floppy = 0;
2656 default_cdrom = 0;
2657 default_sdcard = 0;
2658 break;
2659 case QEMU_OPTION_xen_domid:
2660 if (!(xen_available())) {
2661 printf("Option %s not supported for this target\n", popt->name);
2662 exit(1);
2664 xen_domid = atoi(optarg);
2665 break;
2666 case QEMU_OPTION_xen_create:
2667 if (!(xen_available())) {
2668 printf("Option %s not supported for this target\n", popt->name);
2669 exit(1);
2671 xen_mode = XEN_CREATE;
2672 break;
2673 case QEMU_OPTION_xen_attach:
2674 if (!(xen_available())) {
2675 printf("Option %s not supported for this target\n", popt->name);
2676 exit(1);
2678 xen_mode = XEN_ATTACH;
2679 break;
2680 #ifdef CONFIG_SIMPLE_TRACE
2681 case QEMU_OPTION_trace:
2682 opts = qemu_opts_parse(qemu_find_opts("trace"), optarg, 0);
2683 if (opts) {
2684 trace_file = qemu_opt_get(opts, "file");
2686 break;
2687 #endif
2688 case QEMU_OPTION_readconfig:
2690 int ret = qemu_read_config_file(optarg);
2691 if (ret < 0) {
2692 fprintf(stderr, "read config %s: %s\n", optarg,
2693 strerror(-ret));
2694 exit(1);
2696 break;
2698 case QEMU_OPTION_spice:
2699 olist = qemu_find_opts("spice");
2700 if (!olist) {
2701 fprintf(stderr, "spice is not supported by this qemu build.\n");
2702 exit(1);
2704 opts = qemu_opts_parse(olist, optarg, 0);
2705 if (!opts) {
2706 fprintf(stderr, "parse error: %s\n", optarg);
2707 exit(1);
2709 break;
2710 case QEMU_OPTION_writeconfig:
2712 FILE *fp;
2713 if (strcmp(optarg, "-") == 0) {
2714 fp = stdout;
2715 } else {
2716 fp = fopen(optarg, "w");
2717 if (fp == NULL) {
2718 fprintf(stderr, "open %s: %s\n", optarg, strerror(errno));
2719 exit(1);
2722 qemu_config_write(fp);
2723 fclose(fp);
2724 break;
2726 default:
2727 os_parse_cmd_args(popt->index, optarg);
2731 loc_set_none();
2733 /* If no data_dir is specified then try to find it relative to the
2734 executable path. */
2735 if (!data_dir) {
2736 data_dir = os_find_datadir(argv[0]);
2738 /* If all else fails use the install patch specified when building. */
2739 if (!data_dir) {
2740 data_dir = CONFIG_QEMU_DATADIR;
2743 #ifdef CONFIG_SIMPLE_TRACE
2745 * Set the trace file name, if specified.
2747 st_set_trace_file(trace_file);
2748 #endif
2750 * Default to max_cpus = smp_cpus, in case the user doesn't
2751 * specify a max_cpus value.
2753 if (!max_cpus)
2754 max_cpus = smp_cpus;
2756 machine->max_cpus = machine->max_cpus ?: 1; /* Default to UP */
2757 if (smp_cpus > machine->max_cpus) {
2758 fprintf(stderr, "Number of SMP cpus requested (%d), exceeds max cpus "
2759 "supported by machine `%s' (%d)\n", smp_cpus, machine->name,
2760 machine->max_cpus);
2761 exit(1);
2764 qemu_opts_foreach(qemu_find_opts("device"), default_driver_check, NULL, 0);
2765 qemu_opts_foreach(qemu_find_opts("global"), default_driver_check, NULL, 0);
2767 if (machine->no_serial) {
2768 default_serial = 0;
2770 if (machine->no_parallel) {
2771 default_parallel = 0;
2773 if (!machine->use_virtcon) {
2774 default_virtcon = 0;
2776 if (machine->no_vga) {
2777 default_vga = 0;
2779 if (machine->no_floppy) {
2780 default_floppy = 0;
2782 if (machine->no_cdrom) {
2783 default_cdrom = 0;
2785 if (machine->no_sdcard) {
2786 default_sdcard = 0;
2789 if (display_type == DT_NOGRAPHIC) {
2790 if (default_parallel)
2791 add_device_config(DEV_PARALLEL, "null");
2792 if (default_serial && default_monitor) {
2793 add_device_config(DEV_SERIAL, "mon:stdio");
2794 } else if (default_virtcon && default_monitor) {
2795 add_device_config(DEV_VIRTCON, "mon:stdio");
2796 } else {
2797 if (default_serial)
2798 add_device_config(DEV_SERIAL, "stdio");
2799 if (default_virtcon)
2800 add_device_config(DEV_VIRTCON, "stdio");
2801 if (default_monitor)
2802 monitor_parse("stdio", "readline");
2804 } else {
2805 if (default_serial)
2806 add_device_config(DEV_SERIAL, "vc:80Cx24C");
2807 if (default_parallel)
2808 add_device_config(DEV_PARALLEL, "vc:80Cx24C");
2809 if (default_monitor)
2810 monitor_parse("vc:80Cx24C", "readline");
2811 if (default_virtcon)
2812 add_device_config(DEV_VIRTCON, "vc:80Cx24C");
2814 if (default_vga)
2815 vga_interface_type = VGA_CIRRUS;
2817 socket_init();
2819 if (qemu_opts_foreach(qemu_find_opts("chardev"), chardev_init_func, NULL, 1) != 0)
2820 exit(1);
2821 #ifdef CONFIG_VIRTFS
2822 if (qemu_opts_foreach(qemu_find_opts("fsdev"), fsdev_init_func, NULL, 1) != 0) {
2823 exit(1);
2825 #endif
2827 os_daemonize();
2829 if (pid_file && qemu_create_pidfile(pid_file) != 0) {
2830 os_pidfile_error();
2831 exit(1);
2834 if (kvm_allowed) {
2835 int ret = kvm_init(smp_cpus);
2836 if (ret < 0) {
2837 if (!kvm_available()) {
2838 printf("KVM not supported for this target\n");
2839 } else {
2840 fprintf(stderr, "failed to initialize KVM: %s\n", strerror(-ret));
2842 exit(1);
2846 if (qemu_init_main_loop()) {
2847 fprintf(stderr, "qemu_init_main_loop failed\n");
2848 exit(1);
2850 linux_boot = (kernel_filename != NULL);
2852 if (!linux_boot && *kernel_cmdline != '\0') {
2853 fprintf(stderr, "-append only allowed with -kernel option\n");
2854 exit(1);
2857 if (!linux_boot && initrd_filename != NULL) {
2858 fprintf(stderr, "-initrd only allowed with -kernel option\n");
2859 exit(1);
2862 os_set_line_buffering();
2864 if (init_timer_alarm() < 0) {
2865 fprintf(stderr, "could not initialize alarm timer\n");
2866 exit(1);
2868 configure_icount(icount_option);
2870 if (net_init_clients() < 0) {
2871 exit(1);
2874 /* init the bluetooth world */
2875 if (foreach_device_config(DEV_BT, bt_parse))
2876 exit(1);
2878 /* init the memory */
2879 if (ram_size == 0)
2880 ram_size = DEFAULT_RAM_SIZE * 1024 * 1024;
2882 /* init the dynamic translator */
2883 cpu_exec_init_all(tb_size * 1024 * 1024);
2885 bdrv_init_with_whitelist();
2887 blk_mig_init();
2889 if (default_cdrom) {
2890 /* we always create the cdrom drive, even if no disk is there */
2891 drive_add(NULL, CDROM_ALIAS);
2894 if (default_floppy) {
2895 /* we always create at least one floppy */
2896 drive_add(NULL, FD_ALIAS, 0);
2899 if (default_sdcard) {
2900 /* we always create one sd slot, even if no card is in it */
2901 drive_add(NULL, SD_ALIAS);
2904 /* open the virtual block devices */
2905 if (snapshot)
2906 qemu_opts_foreach(qemu_find_opts("drive"), drive_enable_snapshot, NULL, 0);
2907 if (qemu_opts_foreach(qemu_find_opts("drive"), drive_init_func, &machine->use_scsi, 1) != 0)
2908 exit(1);
2910 register_savevm_live(NULL, "ram", 0, 4, NULL, ram_save_live, NULL,
2911 ram_load, NULL);
2913 if (nb_numa_nodes > 0) {
2914 int i;
2916 if (nb_numa_nodes > smp_cpus) {
2917 nb_numa_nodes = smp_cpus;
2920 /* If no memory size if given for any node, assume the default case
2921 * and distribute the available memory equally across all nodes
2923 for (i = 0; i < nb_numa_nodes; i++) {
2924 if (node_mem[i] != 0)
2925 break;
2927 if (i == nb_numa_nodes) {
2928 uint64_t usedmem = 0;
2930 /* On Linux, the each node's border has to be 8MB aligned,
2931 * the final node gets the rest.
2933 for (i = 0; i < nb_numa_nodes - 1; i++) {
2934 node_mem[i] = (ram_size / nb_numa_nodes) & ~((1 << 23UL) - 1);
2935 usedmem += node_mem[i];
2937 node_mem[i] = ram_size - usedmem;
2940 for (i = 0; i < nb_numa_nodes; i++) {
2941 if (node_cpumask[i] != 0)
2942 break;
2944 /* assigning the VCPUs round-robin is easier to implement, guest OSes
2945 * must cope with this anyway, because there are BIOSes out there in
2946 * real machines which also use this scheme.
2948 if (i == nb_numa_nodes) {
2949 for (i = 0; i < smp_cpus; i++) {
2950 node_cpumask[i % nb_numa_nodes] |= 1 << i;
2955 if (qemu_opts_foreach(qemu_find_opts("mon"), mon_init_func, NULL, 1) != 0) {
2956 exit(1);
2959 if (foreach_device_config(DEV_SERIAL, serial_parse) < 0)
2960 exit(1);
2961 if (foreach_device_config(DEV_PARALLEL, parallel_parse) < 0)
2962 exit(1);
2963 if (foreach_device_config(DEV_VIRTCON, virtcon_parse) < 0)
2964 exit(1);
2965 if (foreach_device_config(DEV_DEBUGCON, debugcon_parse) < 0)
2966 exit(1);
2968 module_call_init(MODULE_INIT_DEVICE);
2970 if (qemu_opts_foreach(qemu_find_opts("device"), device_help_func, NULL, 0) != 0)
2971 exit(0);
2973 if (watchdog) {
2974 i = select_watchdog(watchdog);
2975 if (i > 0)
2976 exit (i == 1 ? 1 : 0);
2979 if (machine->compat_props) {
2980 qdev_prop_register_global_list(machine->compat_props);
2982 qemu_add_globals();
2984 machine->init(ram_size, boot_devices,
2985 kernel_filename, kernel_cmdline, initrd_filename, cpu_model);
2987 cpu_synchronize_all_post_init();
2989 /* must be after terminal init, SDL library changes signal handlers */
2990 os_setup_signal_handling();
2992 set_numa_modes();
2994 current_machine = machine;
2996 /* init USB devices */
2997 if (usb_enabled) {
2998 if (foreach_device_config(DEV_USB, usb_parse) < 0)
2999 exit(1);
3002 /* init generic devices */
3003 if (qemu_opts_foreach(qemu_find_opts("device"), device_init_func, NULL, 1) != 0)
3004 exit(1);
3006 net_check_clients();
3008 /* just use the first displaystate for the moment */
3009 ds = get_displaystate();
3011 if (using_spice)
3012 display_remote++;
3013 if (display_type == DT_DEFAULT && !display_remote) {
3014 #if defined(CONFIG_SDL) || defined(CONFIG_COCOA)
3015 display_type = DT_SDL;
3016 #else
3017 vnc_display = "localhost:0,to=99";
3018 show_vnc_port = 1;
3019 #endif
3023 /* init local displays */
3024 switch (display_type) {
3025 case DT_NOGRAPHIC:
3026 break;
3027 #if defined(CONFIG_CURSES)
3028 case DT_CURSES:
3029 curses_display_init(ds, full_screen);
3030 break;
3031 #endif
3032 #if defined(CONFIG_SDL)
3033 case DT_SDL:
3034 sdl_display_init(ds, full_screen, no_frame);
3035 break;
3036 #elif defined(CONFIG_COCOA)
3037 case DT_SDL:
3038 cocoa_display_init(ds, full_screen);
3039 break;
3040 #endif
3041 default:
3042 break;
3045 /* init remote displays */
3046 if (vnc_display) {
3047 vnc_display_init(ds);
3048 if (vnc_display_open(ds, vnc_display) < 0)
3049 exit(1);
3051 if (show_vnc_port) {
3052 printf("VNC server running on `%s'\n", vnc_display_local_addr(ds));
3055 #ifdef CONFIG_SPICE
3056 if (using_spice) {
3057 qemu_spice_display_init(ds);
3059 #endif
3061 /* display setup */
3062 dpy_resize(ds);
3063 dcl = ds->listeners;
3064 while (dcl != NULL) {
3065 if (dcl->dpy_refresh != NULL) {
3066 ds->gui_timer = qemu_new_timer(rt_clock, gui_update, ds);
3067 qemu_mod_timer(ds->gui_timer, qemu_get_clock(rt_clock));
3068 break;
3070 dcl = dcl->next;
3072 if (ds->gui_timer == NULL) {
3073 nographic_timer = qemu_new_timer(rt_clock, nographic_update, NULL);
3074 qemu_mod_timer(nographic_timer, qemu_get_clock(rt_clock));
3076 text_consoles_set_display(ds);
3078 if (gdbstub_dev && gdbserver_start(gdbstub_dev) < 0) {
3079 fprintf(stderr, "qemu: could not open gdbserver on device '%s'\n",
3080 gdbstub_dev);
3081 exit(1);
3084 qdev_machine_creation_done();
3086 if (rom_load_all() != 0) {
3087 fprintf(stderr, "rom loading failed\n");
3088 exit(1);
3091 qemu_register_reset((void *)qbus_reset_all, sysbus_get_default());
3092 qemu_run_machine_init_done_notifiers();
3094 qemu_system_reset();
3095 if (loadvm) {
3096 if (load_vmstate(loadvm) < 0) {
3097 autostart = 0;
3101 if (incoming) {
3102 int ret = qemu_start_incoming_migration(incoming);
3103 if (ret < 0) {
3104 fprintf(stderr, "Migration failed. Exit code %s(%d), exiting.\n",
3105 incoming, ret);
3106 exit(ret);
3108 } else if (autostart) {
3109 vm_start();
3112 os_setup_post();
3114 main_loop();
3115 quit_timers();
3116 net_cleanup();
3118 return 0;