Add notifier that will be called when machine is fully created.
[qemu.git] / vl.c
blob0d20d261804b4f600b70acbcd4066f5b079aac22
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);
739 static void numa_add(const char *optarg)
741 char option[128];
742 char *endptr;
743 unsigned long long value, endvalue;
744 int nodenr;
746 optarg = get_opt_name(option, 128, optarg, ',') + 1;
747 if (!strcmp(option, "node")) {
748 if (get_param_value(option, 128, "nodeid", optarg) == 0) {
749 nodenr = nb_numa_nodes;
750 } else {
751 nodenr = strtoull(option, NULL, 10);
754 if (get_param_value(option, 128, "mem", optarg) == 0) {
755 node_mem[nodenr] = 0;
756 } else {
757 ssize_t sval;
758 sval = strtosz(option, NULL);
759 if (sval < 0) {
760 fprintf(stderr, "qemu: invalid numa mem size: %s\n", optarg);
761 exit(1);
763 node_mem[nodenr] = sval;
765 if (get_param_value(option, 128, "cpus", optarg) == 0) {
766 node_cpumask[nodenr] = 0;
767 } else {
768 value = strtoull(option, &endptr, 10);
769 if (value >= 64) {
770 value = 63;
771 fprintf(stderr, "only 64 CPUs in NUMA mode supported.\n");
772 } else {
773 if (*endptr == '-') {
774 endvalue = strtoull(endptr+1, &endptr, 10);
775 if (endvalue >= 63) {
776 endvalue = 62;
777 fprintf(stderr,
778 "only 63 CPUs in NUMA mode supported.\n");
780 value = (2ULL << endvalue) - (1ULL << value);
781 } else {
782 value = 1ULL << value;
785 node_cpumask[nodenr] = value;
787 nb_numa_nodes++;
789 return;
792 static void smp_parse(const char *optarg)
794 int smp, sockets = 0, threads = 0, cores = 0;
795 char *endptr;
796 char option[128];
798 smp = strtoul(optarg, &endptr, 10);
799 if (endptr != optarg) {
800 if (*endptr == ',') {
801 endptr++;
804 if (get_param_value(option, 128, "sockets", endptr) != 0)
805 sockets = strtoull(option, NULL, 10);
806 if (get_param_value(option, 128, "cores", endptr) != 0)
807 cores = strtoull(option, NULL, 10);
808 if (get_param_value(option, 128, "threads", endptr) != 0)
809 threads = strtoull(option, NULL, 10);
810 if (get_param_value(option, 128, "maxcpus", endptr) != 0)
811 max_cpus = strtoull(option, NULL, 10);
813 /* compute missing values, prefer sockets over cores over threads */
814 if (smp == 0 || sockets == 0) {
815 sockets = sockets > 0 ? sockets : 1;
816 cores = cores > 0 ? cores : 1;
817 threads = threads > 0 ? threads : 1;
818 if (smp == 0) {
819 smp = cores * threads * sockets;
821 } else {
822 if (cores == 0) {
823 threads = threads > 0 ? threads : 1;
824 cores = smp / (sockets * threads);
825 } else {
826 threads = smp / (cores * sockets);
829 smp_cpus = smp;
830 smp_cores = cores > 0 ? cores : 1;
831 smp_threads = threads > 0 ? threads : 1;
832 if (max_cpus == 0)
833 max_cpus = smp_cpus;
836 /***********************************************************/
837 /* USB devices */
839 static int usb_device_add(const char *devname)
841 const char *p;
842 USBDevice *dev = NULL;
844 if (!usb_enabled)
845 return -1;
847 /* drivers with .usbdevice_name entry in USBDeviceInfo */
848 dev = usbdevice_create(devname);
849 if (dev)
850 goto done;
852 /* the other ones */
853 if (strstart(devname, "host:", &p)) {
854 dev = usb_host_device_open(p);
855 } else if (!strcmp(devname, "bt") || strstart(devname, "bt:", &p)) {
856 dev = usb_bt_init(devname[2] ? hci_init(p) :
857 bt_new_hci(qemu_find_bt_vlan(0)));
858 } else {
859 return -1;
861 if (!dev)
862 return -1;
864 done:
865 return 0;
868 static int usb_device_del(const char *devname)
870 int bus_num, addr;
871 const char *p;
873 if (strstart(devname, "host:", &p))
874 return usb_host_device_close(p);
876 if (!usb_enabled)
877 return -1;
879 p = strchr(devname, '.');
880 if (!p)
881 return -1;
882 bus_num = strtoul(devname, NULL, 0);
883 addr = strtoul(p + 1, NULL, 0);
885 return usb_device_delete_addr(bus_num, addr);
888 static int usb_parse(const char *cmdline)
890 int r;
891 r = usb_device_add(cmdline);
892 if (r < 0) {
893 fprintf(stderr, "qemu: could not add USB device '%s'\n", cmdline);
895 return r;
898 void do_usb_add(Monitor *mon, const QDict *qdict)
900 const char *devname = qdict_get_str(qdict, "devname");
901 if (usb_device_add(devname) < 0) {
902 error_report("could not add USB device '%s'", devname);
906 void do_usb_del(Monitor *mon, const QDict *qdict)
908 const char *devname = qdict_get_str(qdict, "devname");
909 if (usb_device_del(devname) < 0) {
910 error_report("could not delete USB device '%s'", devname);
914 /***********************************************************/
915 /* PCMCIA/Cardbus */
917 static struct pcmcia_socket_entry_s {
918 PCMCIASocket *socket;
919 struct pcmcia_socket_entry_s *next;
920 } *pcmcia_sockets = 0;
922 void pcmcia_socket_register(PCMCIASocket *socket)
924 struct pcmcia_socket_entry_s *entry;
926 entry = qemu_malloc(sizeof(struct pcmcia_socket_entry_s));
927 entry->socket = socket;
928 entry->next = pcmcia_sockets;
929 pcmcia_sockets = entry;
932 void pcmcia_socket_unregister(PCMCIASocket *socket)
934 struct pcmcia_socket_entry_s *entry, **ptr;
936 ptr = &pcmcia_sockets;
937 for (entry = *ptr; entry; ptr = &entry->next, entry = *ptr)
938 if (entry->socket == socket) {
939 *ptr = entry->next;
940 qemu_free(entry);
944 void pcmcia_info(Monitor *mon)
946 struct pcmcia_socket_entry_s *iter;
948 if (!pcmcia_sockets)
949 monitor_printf(mon, "No PCMCIA sockets\n");
951 for (iter = pcmcia_sockets; iter; iter = iter->next)
952 monitor_printf(mon, "%s: %s\n", iter->socket->slot_string,
953 iter->socket->attached ? iter->socket->card_string :
954 "Empty");
957 /***********************************************************/
958 /* I/O handling */
960 typedef struct IOHandlerRecord {
961 int fd;
962 IOCanReadHandler *fd_read_poll;
963 IOHandler *fd_read;
964 IOHandler *fd_write;
965 int deleted;
966 void *opaque;
967 /* temporary data */
968 struct pollfd *ufd;
969 QLIST_ENTRY(IOHandlerRecord) next;
970 } IOHandlerRecord;
972 static QLIST_HEAD(, IOHandlerRecord) io_handlers =
973 QLIST_HEAD_INITIALIZER(io_handlers);
976 /* XXX: fd_read_poll should be suppressed, but an API change is
977 necessary in the character devices to suppress fd_can_read(). */
978 int qemu_set_fd_handler2(int fd,
979 IOCanReadHandler *fd_read_poll,
980 IOHandler *fd_read,
981 IOHandler *fd_write,
982 void *opaque)
984 IOHandlerRecord *ioh;
986 if (!fd_read && !fd_write) {
987 QLIST_FOREACH(ioh, &io_handlers, next) {
988 if (ioh->fd == fd) {
989 ioh->deleted = 1;
990 break;
993 } else {
994 QLIST_FOREACH(ioh, &io_handlers, next) {
995 if (ioh->fd == fd)
996 goto found;
998 ioh = qemu_mallocz(sizeof(IOHandlerRecord));
999 QLIST_INSERT_HEAD(&io_handlers, ioh, next);
1000 found:
1001 ioh->fd = fd;
1002 ioh->fd_read_poll = fd_read_poll;
1003 ioh->fd_read = fd_read;
1004 ioh->fd_write = fd_write;
1005 ioh->opaque = opaque;
1006 ioh->deleted = 0;
1008 return 0;
1011 int qemu_set_fd_handler(int fd,
1012 IOHandler *fd_read,
1013 IOHandler *fd_write,
1014 void *opaque)
1016 return qemu_set_fd_handler2(fd, NULL, fd_read, fd_write, opaque);
1019 /***********************************************************/
1020 /* machine registration */
1022 static QEMUMachine *first_machine = NULL;
1023 QEMUMachine *current_machine = NULL;
1025 int qemu_register_machine(QEMUMachine *m)
1027 QEMUMachine **pm;
1028 pm = &first_machine;
1029 while (*pm != NULL)
1030 pm = &(*pm)->next;
1031 m->next = NULL;
1032 *pm = m;
1033 return 0;
1036 static QEMUMachine *find_machine(const char *name)
1038 QEMUMachine *m;
1040 for(m = first_machine; m != NULL; m = m->next) {
1041 if (!strcmp(m->name, name))
1042 return m;
1043 if (m->alias && !strcmp(m->alias, name))
1044 return m;
1046 return NULL;
1049 static QEMUMachine *find_default_machine(void)
1051 QEMUMachine *m;
1053 for(m = first_machine; m != NULL; m = m->next) {
1054 if (m->is_default) {
1055 return m;
1058 return NULL;
1061 /***********************************************************/
1062 /* main execution loop */
1064 static void gui_update(void *opaque)
1066 uint64_t interval = GUI_REFRESH_INTERVAL;
1067 DisplayState *ds = opaque;
1068 DisplayChangeListener *dcl = ds->listeners;
1070 qemu_flush_coalesced_mmio_buffer();
1071 dpy_refresh(ds);
1073 while (dcl != NULL) {
1074 if (dcl->gui_timer_interval &&
1075 dcl->gui_timer_interval < interval)
1076 interval = dcl->gui_timer_interval;
1077 dcl = dcl->next;
1079 qemu_mod_timer(ds->gui_timer, interval + qemu_get_clock(rt_clock));
1082 static void nographic_update(void *opaque)
1084 uint64_t interval = GUI_REFRESH_INTERVAL;
1086 qemu_flush_coalesced_mmio_buffer();
1087 qemu_mod_timer(nographic_timer, interval + qemu_get_clock(rt_clock));
1090 struct vm_change_state_entry {
1091 VMChangeStateHandler *cb;
1092 void *opaque;
1093 QLIST_ENTRY (vm_change_state_entry) entries;
1096 static QLIST_HEAD(vm_change_state_head, vm_change_state_entry) vm_change_state_head;
1098 VMChangeStateEntry *qemu_add_vm_change_state_handler(VMChangeStateHandler *cb,
1099 void *opaque)
1101 VMChangeStateEntry *e;
1103 e = qemu_mallocz(sizeof (*e));
1105 e->cb = cb;
1106 e->opaque = opaque;
1107 QLIST_INSERT_HEAD(&vm_change_state_head, e, entries);
1108 return e;
1111 void qemu_del_vm_change_state_handler(VMChangeStateEntry *e)
1113 QLIST_REMOVE (e, entries);
1114 qemu_free (e);
1117 void vm_state_notify(int running, int reason)
1119 VMChangeStateEntry *e;
1121 trace_vm_state_notify(running, reason);
1123 for (e = vm_change_state_head.lh_first; e; e = e->entries.le_next) {
1124 e->cb(e->opaque, running, reason);
1128 void vm_start(void)
1130 if (!vm_running) {
1131 cpu_enable_ticks();
1132 vm_running = 1;
1133 vm_state_notify(1, 0);
1134 resume_all_vcpus();
1135 monitor_protocol_event(QEVENT_RESUME, NULL);
1139 /* reset/shutdown handler */
1141 typedef struct QEMUResetEntry {
1142 QTAILQ_ENTRY(QEMUResetEntry) entry;
1143 QEMUResetHandler *func;
1144 void *opaque;
1145 } QEMUResetEntry;
1147 static QTAILQ_HEAD(reset_handlers, QEMUResetEntry) reset_handlers =
1148 QTAILQ_HEAD_INITIALIZER(reset_handlers);
1149 static int reset_requested;
1150 static int shutdown_requested;
1151 static int powerdown_requested;
1152 int debug_requested;
1153 int vmstop_requested;
1155 int qemu_shutdown_requested(void)
1157 int r = shutdown_requested;
1158 shutdown_requested = 0;
1159 return r;
1162 int qemu_reset_requested(void)
1164 int r = reset_requested;
1165 reset_requested = 0;
1166 return r;
1169 int qemu_powerdown_requested(void)
1171 int r = powerdown_requested;
1172 powerdown_requested = 0;
1173 return r;
1176 static int qemu_debug_requested(void)
1178 int r = debug_requested;
1179 debug_requested = 0;
1180 return r;
1183 static int qemu_vmstop_requested(void)
1185 int r = vmstop_requested;
1186 vmstop_requested = 0;
1187 return r;
1190 void qemu_register_reset(QEMUResetHandler *func, void *opaque)
1192 QEMUResetEntry *re = qemu_mallocz(sizeof(QEMUResetEntry));
1194 re->func = func;
1195 re->opaque = opaque;
1196 QTAILQ_INSERT_TAIL(&reset_handlers, re, entry);
1199 void qemu_unregister_reset(QEMUResetHandler *func, void *opaque)
1201 QEMUResetEntry *re;
1203 QTAILQ_FOREACH(re, &reset_handlers, entry) {
1204 if (re->func == func && re->opaque == opaque) {
1205 QTAILQ_REMOVE(&reset_handlers, re, entry);
1206 qemu_free(re);
1207 return;
1212 void qemu_system_reset(void)
1214 QEMUResetEntry *re, *nre;
1216 /* reset all devices */
1217 QTAILQ_FOREACH_SAFE(re, &reset_handlers, entry, nre) {
1218 re->func(re->opaque);
1220 monitor_protocol_event(QEVENT_RESET, NULL);
1221 cpu_synchronize_all_post_reset();
1224 void qemu_system_reset_request(void)
1226 if (no_reboot) {
1227 shutdown_requested = 1;
1228 } else {
1229 reset_requested = 1;
1231 qemu_notify_event();
1234 void qemu_system_shutdown_request(void)
1236 shutdown_requested = 1;
1237 qemu_notify_event();
1240 void qemu_system_powerdown_request(void)
1242 powerdown_requested = 1;
1243 qemu_notify_event();
1246 void main_loop_wait(int nonblocking)
1248 IOHandlerRecord *ioh;
1249 fd_set rfds, wfds, xfds;
1250 int ret, nfds;
1251 struct timeval tv;
1252 int timeout;
1254 if (nonblocking)
1255 timeout = 0;
1256 else {
1257 timeout = qemu_calculate_timeout();
1258 qemu_bh_update_timeout(&timeout);
1261 os_host_main_loop_wait(&timeout);
1263 /* poll any events */
1264 /* XXX: separate device handlers from system ones */
1265 nfds = -1;
1266 FD_ZERO(&rfds);
1267 FD_ZERO(&wfds);
1268 FD_ZERO(&xfds);
1269 QLIST_FOREACH(ioh, &io_handlers, next) {
1270 if (ioh->deleted)
1271 continue;
1272 if (ioh->fd_read &&
1273 (!ioh->fd_read_poll ||
1274 ioh->fd_read_poll(ioh->opaque) != 0)) {
1275 FD_SET(ioh->fd, &rfds);
1276 if (ioh->fd > nfds)
1277 nfds = ioh->fd;
1279 if (ioh->fd_write) {
1280 FD_SET(ioh->fd, &wfds);
1281 if (ioh->fd > nfds)
1282 nfds = ioh->fd;
1286 tv.tv_sec = timeout / 1000;
1287 tv.tv_usec = (timeout % 1000) * 1000;
1289 slirp_select_fill(&nfds, &rfds, &wfds, &xfds);
1291 qemu_mutex_unlock_iothread();
1292 ret = select(nfds + 1, &rfds, &wfds, &xfds, &tv);
1293 qemu_mutex_lock_iothread();
1294 if (ret > 0) {
1295 IOHandlerRecord *pioh;
1297 QLIST_FOREACH_SAFE(ioh, &io_handlers, next, pioh) {
1298 if (!ioh->deleted && ioh->fd_read && FD_ISSET(ioh->fd, &rfds)) {
1299 ioh->fd_read(ioh->opaque);
1301 if (!ioh->deleted && ioh->fd_write && FD_ISSET(ioh->fd, &wfds)) {
1302 ioh->fd_write(ioh->opaque);
1305 /* Do this last in case read/write handlers marked it for deletion */
1306 if (ioh->deleted) {
1307 QLIST_REMOVE(ioh, next);
1308 qemu_free(ioh);
1313 slirp_select_poll(&rfds, &wfds, &xfds, (ret < 0));
1315 qemu_run_all_timers();
1317 /* Check bottom-halves last in case any of the earlier events triggered
1318 them. */
1319 qemu_bh_poll();
1323 static int vm_can_run(void)
1325 if (powerdown_requested)
1326 return 0;
1327 if (reset_requested)
1328 return 0;
1329 if (shutdown_requested)
1330 return 0;
1331 if (debug_requested)
1332 return 0;
1333 return 1;
1336 qemu_irq qemu_system_powerdown;
1338 static void main_loop(void)
1340 int r;
1342 qemu_main_loop_start();
1344 for (;;) {
1345 do {
1346 bool nonblocking = false;
1347 #ifdef CONFIG_PROFILER
1348 int64_t ti;
1349 #endif
1350 #ifndef CONFIG_IOTHREAD
1351 nonblocking = cpu_exec_all();
1352 #endif
1353 #ifdef CONFIG_PROFILER
1354 ti = profile_getclock();
1355 #endif
1356 main_loop_wait(nonblocking);
1357 #ifdef CONFIG_PROFILER
1358 dev_time += profile_getclock() - ti;
1359 #endif
1360 } while (vm_can_run());
1362 if ((r = qemu_debug_requested())) {
1363 vm_stop(r);
1365 if (qemu_shutdown_requested()) {
1366 monitor_protocol_event(QEVENT_SHUTDOWN, NULL);
1367 if (no_shutdown) {
1368 vm_stop(0);
1369 no_shutdown = 0;
1370 } else
1371 break;
1373 if (qemu_reset_requested()) {
1374 pause_all_vcpus();
1375 qemu_system_reset();
1376 resume_all_vcpus();
1378 if (qemu_powerdown_requested()) {
1379 monitor_protocol_event(QEVENT_POWERDOWN, NULL);
1380 qemu_irq_raise(qemu_system_powerdown);
1382 if ((r = qemu_vmstop_requested())) {
1383 vm_stop(r);
1386 bdrv_close_all();
1387 pause_all_vcpus();
1390 static void version(void)
1392 printf("QEMU emulator version " QEMU_VERSION QEMU_PKGVERSION ", Copyright (c) 2003-2008 Fabrice Bellard\n");
1395 static void help(int exitcode)
1397 const char *options_help =
1398 #define DEF(option, opt_arg, opt_enum, opt_help, arch_mask) \
1399 opt_help
1400 #define DEFHEADING(text) stringify(text) "\n"
1401 #include "qemu-options.def"
1402 #undef DEF
1403 #undef DEFHEADING
1404 #undef GEN_DOCS
1406 version();
1407 printf("usage: %s [options] [disk_image]\n"
1408 "\n"
1409 "'disk_image' is a raw hard disk image for IDE hard disk 0\n"
1410 "\n"
1411 "%s\n"
1412 "During emulation, the following keys are useful:\n"
1413 "ctrl-alt-f toggle full screen\n"
1414 "ctrl-alt-n switch to virtual console 'n'\n"
1415 "ctrl-alt toggle mouse and keyboard grab\n"
1416 "\n"
1417 "When using -nographic, press 'ctrl-a h' to get some help.\n",
1418 "qemu",
1419 options_help);
1420 exit(exitcode);
1423 #define HAS_ARG 0x0001
1425 typedef struct QEMUOption {
1426 const char *name;
1427 int flags;
1428 int index;
1429 uint32_t arch_mask;
1430 } QEMUOption;
1432 static const QEMUOption qemu_options[] = {
1433 { "h", 0, QEMU_OPTION_h, QEMU_ARCH_ALL },
1434 #define DEF(option, opt_arg, opt_enum, opt_help, arch_mask) \
1435 { option, opt_arg, opt_enum, arch_mask },
1436 #define DEFHEADING(text)
1437 #include "qemu-options.def"
1438 #undef DEF
1439 #undef DEFHEADING
1440 #undef GEN_DOCS
1441 { NULL },
1443 static void select_vgahw (const char *p)
1445 const char *opts;
1447 default_vga = 0;
1448 vga_interface_type = VGA_NONE;
1449 if (strstart(p, "std", &opts)) {
1450 vga_interface_type = VGA_STD;
1451 } else if (strstart(p, "cirrus", &opts)) {
1452 vga_interface_type = VGA_CIRRUS;
1453 } else if (strstart(p, "vmware", &opts)) {
1454 vga_interface_type = VGA_VMWARE;
1455 } else if (strstart(p, "xenfb", &opts)) {
1456 vga_interface_type = VGA_XENFB;
1457 } else if (!strstart(p, "none", &opts)) {
1458 invalid_vga:
1459 fprintf(stderr, "Unknown vga type: %s\n", p);
1460 exit(1);
1462 while (*opts) {
1463 const char *nextopt;
1465 if (strstart(opts, ",retrace=", &nextopt)) {
1466 opts = nextopt;
1467 if (strstart(opts, "dumb", &nextopt))
1468 vga_retrace_method = VGA_RETRACE_DUMB;
1469 else if (strstart(opts, "precise", &nextopt))
1470 vga_retrace_method = VGA_RETRACE_PRECISE;
1471 else goto invalid_vga;
1472 } else goto invalid_vga;
1473 opts = nextopt;
1477 static int balloon_parse(const char *arg)
1479 QemuOpts *opts;
1481 if (strcmp(arg, "none") == 0) {
1482 return 0;
1485 if (!strncmp(arg, "virtio", 6)) {
1486 if (arg[6] == ',') {
1487 /* have params -> parse them */
1488 opts = qemu_opts_parse(qemu_find_opts("device"), arg+7, 0);
1489 if (!opts)
1490 return -1;
1491 } else {
1492 /* create empty opts */
1493 opts = qemu_opts_create(qemu_find_opts("device"), NULL, 0);
1495 qemu_opt_set(opts, "driver", "virtio-balloon-pci");
1496 return 0;
1499 return -1;
1502 char *qemu_find_file(int type, const char *name)
1504 int len;
1505 const char *subdir;
1506 char *buf;
1508 /* If name contains path separators then try it as a straight path. */
1509 if ((strchr(name, '/') || strchr(name, '\\'))
1510 && access(name, R_OK) == 0) {
1511 return qemu_strdup(name);
1513 switch (type) {
1514 case QEMU_FILE_TYPE_BIOS:
1515 subdir = "";
1516 break;
1517 case QEMU_FILE_TYPE_KEYMAP:
1518 subdir = "keymaps/";
1519 break;
1520 default:
1521 abort();
1523 len = strlen(data_dir) + strlen(name) + strlen(subdir) + 2;
1524 buf = qemu_mallocz(len);
1525 snprintf(buf, len, "%s/%s%s", data_dir, subdir, name);
1526 if (access(buf, R_OK)) {
1527 qemu_free(buf);
1528 return NULL;
1530 return buf;
1533 static int device_help_func(QemuOpts *opts, void *opaque)
1535 return qdev_device_help(opts);
1538 static int device_init_func(QemuOpts *opts, void *opaque)
1540 DeviceState *dev;
1542 dev = qdev_device_add(opts);
1543 if (!dev)
1544 return -1;
1545 return 0;
1548 static int chardev_init_func(QemuOpts *opts, void *opaque)
1550 CharDriverState *chr;
1552 chr = qemu_chr_open_opts(opts, NULL);
1553 if (!chr)
1554 return -1;
1555 return 0;
1558 #ifdef CONFIG_VIRTFS
1559 static int fsdev_init_func(QemuOpts *opts, void *opaque)
1561 int ret;
1562 ret = qemu_fsdev_add(opts);
1564 return ret;
1566 #endif
1568 static int mon_init_func(QemuOpts *opts, void *opaque)
1570 CharDriverState *chr;
1571 const char *chardev;
1572 const char *mode;
1573 int flags;
1575 mode = qemu_opt_get(opts, "mode");
1576 if (mode == NULL) {
1577 mode = "readline";
1579 if (strcmp(mode, "readline") == 0) {
1580 flags = MONITOR_USE_READLINE;
1581 } else if (strcmp(mode, "control") == 0) {
1582 flags = MONITOR_USE_CONTROL;
1583 } else {
1584 fprintf(stderr, "unknown monitor mode \"%s\"\n", mode);
1585 exit(1);
1588 if (qemu_opt_get_bool(opts, "pretty", 0))
1589 flags |= MONITOR_USE_PRETTY;
1591 if (qemu_opt_get_bool(opts, "default", 0))
1592 flags |= MONITOR_IS_DEFAULT;
1594 chardev = qemu_opt_get(opts, "chardev");
1595 chr = qemu_chr_find(chardev);
1596 if (chr == NULL) {
1597 fprintf(stderr, "chardev \"%s\" not found\n", chardev);
1598 exit(1);
1601 monitor_init(chr, flags);
1602 return 0;
1605 static void monitor_parse(const char *optarg, const char *mode)
1607 static int monitor_device_index = 0;
1608 QemuOpts *opts;
1609 const char *p;
1610 char label[32];
1611 int def = 0;
1613 if (strstart(optarg, "chardev:", &p)) {
1614 snprintf(label, sizeof(label), "%s", p);
1615 } else {
1616 snprintf(label, sizeof(label), "compat_monitor%d",
1617 monitor_device_index);
1618 if (monitor_device_index == 0) {
1619 def = 1;
1621 opts = qemu_chr_parse_compat(label, optarg);
1622 if (!opts) {
1623 fprintf(stderr, "parse error: %s\n", optarg);
1624 exit(1);
1628 opts = qemu_opts_create(qemu_find_opts("mon"), label, 1);
1629 if (!opts) {
1630 fprintf(stderr, "duplicate chardev: %s\n", label);
1631 exit(1);
1633 qemu_opt_set(opts, "mode", mode);
1634 qemu_opt_set(opts, "chardev", label);
1635 if (def)
1636 qemu_opt_set(opts, "default", "on");
1637 monitor_device_index++;
1640 struct device_config {
1641 enum {
1642 DEV_USB, /* -usbdevice */
1643 DEV_BT, /* -bt */
1644 DEV_SERIAL, /* -serial */
1645 DEV_PARALLEL, /* -parallel */
1646 DEV_VIRTCON, /* -virtioconsole */
1647 DEV_DEBUGCON, /* -debugcon */
1648 } type;
1649 const char *cmdline;
1650 QTAILQ_ENTRY(device_config) next;
1652 QTAILQ_HEAD(, device_config) device_configs = QTAILQ_HEAD_INITIALIZER(device_configs);
1654 static void add_device_config(int type, const char *cmdline)
1656 struct device_config *conf;
1658 conf = qemu_mallocz(sizeof(*conf));
1659 conf->type = type;
1660 conf->cmdline = cmdline;
1661 QTAILQ_INSERT_TAIL(&device_configs, conf, next);
1664 static int foreach_device_config(int type, int (*func)(const char *cmdline))
1666 struct device_config *conf;
1667 int rc;
1669 QTAILQ_FOREACH(conf, &device_configs, next) {
1670 if (conf->type != type)
1671 continue;
1672 rc = func(conf->cmdline);
1673 if (0 != rc)
1674 return rc;
1676 return 0;
1679 static int serial_parse(const char *devname)
1681 static int index = 0;
1682 char label[32];
1684 if (strcmp(devname, "none") == 0)
1685 return 0;
1686 if (index == MAX_SERIAL_PORTS) {
1687 fprintf(stderr, "qemu: too many serial ports\n");
1688 exit(1);
1690 snprintf(label, sizeof(label), "serial%d", index);
1691 serial_hds[index] = qemu_chr_open(label, devname, NULL);
1692 if (!serial_hds[index]) {
1693 fprintf(stderr, "qemu: could not open serial device '%s': %s\n",
1694 devname, strerror(errno));
1695 return -1;
1697 index++;
1698 return 0;
1701 static int parallel_parse(const char *devname)
1703 static int index = 0;
1704 char label[32];
1706 if (strcmp(devname, "none") == 0)
1707 return 0;
1708 if (index == MAX_PARALLEL_PORTS) {
1709 fprintf(stderr, "qemu: too many parallel ports\n");
1710 exit(1);
1712 snprintf(label, sizeof(label), "parallel%d", index);
1713 parallel_hds[index] = qemu_chr_open(label, devname, NULL);
1714 if (!parallel_hds[index]) {
1715 fprintf(stderr, "qemu: could not open parallel device '%s': %s\n",
1716 devname, strerror(errno));
1717 return -1;
1719 index++;
1720 return 0;
1723 static int virtcon_parse(const char *devname)
1725 QemuOptsList *device = qemu_find_opts("device");
1726 static int index = 0;
1727 char label[32];
1728 QemuOpts *bus_opts, *dev_opts;
1730 if (strcmp(devname, "none") == 0)
1731 return 0;
1732 if (index == MAX_VIRTIO_CONSOLES) {
1733 fprintf(stderr, "qemu: too many virtio consoles\n");
1734 exit(1);
1737 bus_opts = qemu_opts_create(device, NULL, 0);
1738 qemu_opt_set(bus_opts, "driver", "virtio-serial");
1740 dev_opts = qemu_opts_create(device, NULL, 0);
1741 qemu_opt_set(dev_opts, "driver", "virtconsole");
1743 snprintf(label, sizeof(label), "virtcon%d", index);
1744 virtcon_hds[index] = qemu_chr_open(label, devname, NULL);
1745 if (!virtcon_hds[index]) {
1746 fprintf(stderr, "qemu: could not open virtio console '%s': %s\n",
1747 devname, strerror(errno));
1748 return -1;
1750 qemu_opt_set(dev_opts, "chardev", label);
1752 index++;
1753 return 0;
1756 static int debugcon_parse(const char *devname)
1758 QemuOpts *opts;
1760 if (!qemu_chr_open("debugcon", devname, NULL)) {
1761 exit(1);
1763 opts = qemu_opts_create(qemu_find_opts("device"), "debugcon", 1);
1764 if (!opts) {
1765 fprintf(stderr, "qemu: already have a debugcon device\n");
1766 exit(1);
1768 qemu_opt_set(opts, "driver", "isa-debugcon");
1769 qemu_opt_set(opts, "chardev", "debugcon");
1770 return 0;
1773 void qemu_add_exit_notifier(Notifier *notify)
1775 notifier_list_add(&exit_notifiers, notify);
1778 void qemu_remove_exit_notifier(Notifier *notify)
1780 notifier_list_remove(&exit_notifiers, notify);
1783 static void qemu_run_exit_notifiers(void)
1785 notifier_list_notify(&exit_notifiers);
1788 void qemu_add_machine_init_done_notifier(Notifier *notify)
1790 notifier_list_add(&machine_init_done_notifiers, notify);
1793 static void qemu_run_machine_init_done_notifiers(void)
1795 notifier_list_notify(&machine_init_done_notifiers);
1798 static const QEMUOption *lookup_opt(int argc, char **argv,
1799 const char **poptarg, int *poptind)
1801 const QEMUOption *popt;
1802 int optind = *poptind;
1803 char *r = argv[optind];
1804 const char *optarg;
1806 loc_set_cmdline(argv, optind, 1);
1807 optind++;
1808 /* Treat --foo the same as -foo. */
1809 if (r[1] == '-')
1810 r++;
1811 popt = qemu_options;
1812 for(;;) {
1813 if (!popt->name) {
1814 error_report("invalid option");
1815 exit(1);
1817 if (!strcmp(popt->name, r + 1))
1818 break;
1819 popt++;
1821 if (popt->flags & HAS_ARG) {
1822 if (optind >= argc) {
1823 error_report("requires an argument");
1824 exit(1);
1826 optarg = argv[optind++];
1827 loc_set_cmdline(argv, optind - 2, 2);
1828 } else {
1829 optarg = NULL;
1832 *poptarg = optarg;
1833 *poptind = optind;
1835 return popt;
1838 int main(int argc, char **argv, char **envp)
1840 const char *gdbstub_dev = NULL;
1841 int i;
1842 int snapshot, linux_boot;
1843 const char *icount_option = NULL;
1844 const char *initrd_filename;
1845 const char *kernel_filename, *kernel_cmdline;
1846 char boot_devices[33] = "cad"; /* default to HD->floppy->CD-ROM */
1847 DisplayState *ds;
1848 DisplayChangeListener *dcl;
1849 int cyls, heads, secs, translation;
1850 QemuOpts *hda_opts = NULL, *opts;
1851 QemuOptsList *olist;
1852 int optind;
1853 const char *optarg;
1854 const char *loadvm = NULL;
1855 QEMUMachine *machine;
1856 const char *cpu_model;
1857 int tb_size;
1858 const char *pid_file = NULL;
1859 const char *incoming = NULL;
1860 int show_vnc_port = 0;
1861 int defconfig = 1;
1863 #ifdef CONFIG_SIMPLE_TRACE
1864 const char *trace_file = NULL;
1865 #endif
1866 atexit(qemu_run_exit_notifiers);
1867 error_set_progname(argv[0]);
1869 init_clocks();
1871 qemu_cache_utils_init(envp);
1873 QLIST_INIT (&vm_change_state_head);
1874 os_setup_early_signal_handling();
1876 module_call_init(MODULE_INIT_MACHINE);
1877 machine = find_default_machine();
1878 cpu_model = NULL;
1879 initrd_filename = NULL;
1880 ram_size = 0;
1881 snapshot = 0;
1882 kernel_filename = NULL;
1883 kernel_cmdline = "";
1884 cyls = heads = secs = 0;
1885 translation = BIOS_ATA_TRANSLATION_AUTO;
1887 for (i = 0; i < MAX_NODES; i++) {
1888 node_mem[i] = 0;
1889 node_cpumask[i] = 0;
1892 nb_numa_nodes = 0;
1893 nb_nics = 0;
1895 tb_size = 0;
1896 autostart= 1;
1898 /* first pass of option parsing */
1899 optind = 1;
1900 while (optind < argc) {
1901 if (argv[optind][0] != '-') {
1902 /* disk image */
1903 optind++;
1904 continue;
1905 } else {
1906 const QEMUOption *popt;
1908 popt = lookup_opt(argc, argv, &optarg, &optind);
1909 switch (popt->index) {
1910 case QEMU_OPTION_nodefconfig:
1911 defconfig=0;
1912 break;
1917 if (defconfig) {
1918 int ret;
1920 ret = qemu_read_config_file(CONFIG_QEMU_CONFDIR "/qemu.conf");
1921 if (ret < 0 && ret != -ENOENT) {
1922 exit(1);
1925 ret = qemu_read_config_file(arch_config_name);
1926 if (ret < 0 && ret != -ENOENT) {
1927 exit(1);
1930 cpudef_init();
1932 /* second pass of option parsing */
1933 optind = 1;
1934 for(;;) {
1935 if (optind >= argc)
1936 break;
1937 if (argv[optind][0] != '-') {
1938 hda_opts = drive_add(argv[optind++], HD_ALIAS, 0);
1939 } else {
1940 const QEMUOption *popt;
1942 popt = lookup_opt(argc, argv, &optarg, &optind);
1943 if (!(popt->arch_mask & arch_type)) {
1944 printf("Option %s not supported for this target\n", popt->name);
1945 exit(1);
1947 switch(popt->index) {
1948 case QEMU_OPTION_M:
1949 machine = find_machine(optarg);
1950 if (!machine) {
1951 QEMUMachine *m;
1952 printf("Supported machines are:\n");
1953 for(m = first_machine; m != NULL; m = m->next) {
1954 if (m->alias)
1955 printf("%-10s %s (alias of %s)\n",
1956 m->alias, m->desc, m->name);
1957 printf("%-10s %s%s\n",
1958 m->name, m->desc,
1959 m->is_default ? " (default)" : "");
1961 exit(*optarg != '?');
1963 break;
1964 case QEMU_OPTION_cpu:
1965 /* hw initialization will check this */
1966 if (*optarg == '?') {
1967 list_cpus(stdout, &fprintf, optarg);
1968 exit(0);
1969 } else {
1970 cpu_model = optarg;
1972 break;
1973 case QEMU_OPTION_initrd:
1974 initrd_filename = optarg;
1975 break;
1976 case QEMU_OPTION_hda:
1977 if (cyls == 0)
1978 hda_opts = drive_add(optarg, HD_ALIAS, 0);
1979 else
1980 hda_opts = drive_add(optarg, HD_ALIAS
1981 ",cyls=%d,heads=%d,secs=%d%s",
1982 0, cyls, heads, secs,
1983 translation == BIOS_ATA_TRANSLATION_LBA ?
1984 ",trans=lba" :
1985 translation == BIOS_ATA_TRANSLATION_NONE ?
1986 ",trans=none" : "");
1987 break;
1988 case QEMU_OPTION_hdb:
1989 case QEMU_OPTION_hdc:
1990 case QEMU_OPTION_hdd:
1991 drive_add(optarg, HD_ALIAS, popt->index - QEMU_OPTION_hda);
1992 break;
1993 case QEMU_OPTION_drive:
1994 drive_add(NULL, "%s", optarg);
1995 break;
1996 case QEMU_OPTION_set:
1997 if (qemu_set_option(optarg) != 0)
1998 exit(1);
1999 break;
2000 case QEMU_OPTION_global:
2001 if (qemu_global_option(optarg) != 0)
2002 exit(1);
2003 break;
2004 case QEMU_OPTION_mtdblock:
2005 drive_add(optarg, MTD_ALIAS);
2006 break;
2007 case QEMU_OPTION_sd:
2008 drive_add(optarg, SD_ALIAS);
2009 break;
2010 case QEMU_OPTION_pflash:
2011 drive_add(optarg, PFLASH_ALIAS);
2012 break;
2013 case QEMU_OPTION_snapshot:
2014 snapshot = 1;
2015 break;
2016 case QEMU_OPTION_hdachs:
2018 const char *p;
2019 p = optarg;
2020 cyls = strtol(p, (char **)&p, 0);
2021 if (cyls < 1 || cyls > 16383)
2022 goto chs_fail;
2023 if (*p != ',')
2024 goto chs_fail;
2025 p++;
2026 heads = strtol(p, (char **)&p, 0);
2027 if (heads < 1 || heads > 16)
2028 goto chs_fail;
2029 if (*p != ',')
2030 goto chs_fail;
2031 p++;
2032 secs = strtol(p, (char **)&p, 0);
2033 if (secs < 1 || secs > 63)
2034 goto chs_fail;
2035 if (*p == ',') {
2036 p++;
2037 if (!strcmp(p, "none"))
2038 translation = BIOS_ATA_TRANSLATION_NONE;
2039 else if (!strcmp(p, "lba"))
2040 translation = BIOS_ATA_TRANSLATION_LBA;
2041 else if (!strcmp(p, "auto"))
2042 translation = BIOS_ATA_TRANSLATION_AUTO;
2043 else
2044 goto chs_fail;
2045 } else if (*p != '\0') {
2046 chs_fail:
2047 fprintf(stderr, "qemu: invalid physical CHS format\n");
2048 exit(1);
2050 if (hda_opts != NULL) {
2051 char num[16];
2052 snprintf(num, sizeof(num), "%d", cyls);
2053 qemu_opt_set(hda_opts, "cyls", num);
2054 snprintf(num, sizeof(num), "%d", heads);
2055 qemu_opt_set(hda_opts, "heads", num);
2056 snprintf(num, sizeof(num), "%d", secs);
2057 qemu_opt_set(hda_opts, "secs", num);
2058 if (translation == BIOS_ATA_TRANSLATION_LBA)
2059 qemu_opt_set(hda_opts, "trans", "lba");
2060 if (translation == BIOS_ATA_TRANSLATION_NONE)
2061 qemu_opt_set(hda_opts, "trans", "none");
2064 break;
2065 case QEMU_OPTION_numa:
2066 if (nb_numa_nodes >= MAX_NODES) {
2067 fprintf(stderr, "qemu: too many NUMA nodes\n");
2068 exit(1);
2070 numa_add(optarg);
2071 break;
2072 case QEMU_OPTION_nographic:
2073 display_type = DT_NOGRAPHIC;
2074 break;
2075 #ifdef CONFIG_CURSES
2076 case QEMU_OPTION_curses:
2077 display_type = DT_CURSES;
2078 break;
2079 #endif
2080 case QEMU_OPTION_portrait:
2081 graphic_rotate = 1;
2082 break;
2083 case QEMU_OPTION_kernel:
2084 kernel_filename = optarg;
2085 break;
2086 case QEMU_OPTION_append:
2087 kernel_cmdline = optarg;
2088 break;
2089 case QEMU_OPTION_cdrom:
2090 drive_add(optarg, CDROM_ALIAS);
2091 break;
2092 case QEMU_OPTION_boot:
2094 static const char * const params[] = {
2095 "order", "once", "menu", NULL
2097 char buf[sizeof(boot_devices)];
2098 char *standard_boot_devices;
2099 int legacy = 0;
2101 if (!strchr(optarg, '=')) {
2102 legacy = 1;
2103 pstrcpy(buf, sizeof(buf), optarg);
2104 } else if (check_params(buf, sizeof(buf), params, optarg) < 0) {
2105 fprintf(stderr,
2106 "qemu: unknown boot parameter '%s' in '%s'\n",
2107 buf, optarg);
2108 exit(1);
2111 if (legacy ||
2112 get_param_value(buf, sizeof(buf), "order", optarg)) {
2113 validate_bootdevices(buf);
2114 pstrcpy(boot_devices, sizeof(boot_devices), buf);
2116 if (!legacy) {
2117 if (get_param_value(buf, sizeof(buf),
2118 "once", optarg)) {
2119 validate_bootdevices(buf);
2120 standard_boot_devices = qemu_strdup(boot_devices);
2121 pstrcpy(boot_devices, sizeof(boot_devices), buf);
2122 qemu_register_reset(restore_boot_devices,
2123 standard_boot_devices);
2125 if (get_param_value(buf, sizeof(buf),
2126 "menu", optarg)) {
2127 if (!strcmp(buf, "on")) {
2128 boot_menu = 1;
2129 } else if (!strcmp(buf, "off")) {
2130 boot_menu = 0;
2131 } else {
2132 fprintf(stderr,
2133 "qemu: invalid option value '%s'\n",
2134 buf);
2135 exit(1);
2140 break;
2141 case QEMU_OPTION_fda:
2142 case QEMU_OPTION_fdb:
2143 drive_add(optarg, FD_ALIAS, popt->index - QEMU_OPTION_fda);
2144 break;
2145 case QEMU_OPTION_no_fd_bootchk:
2146 fd_bootchk = 0;
2147 break;
2148 case QEMU_OPTION_netdev:
2149 if (net_client_parse(qemu_find_opts("netdev"), optarg) == -1) {
2150 exit(1);
2152 break;
2153 case QEMU_OPTION_net:
2154 if (net_client_parse(qemu_find_opts("net"), optarg) == -1) {
2155 exit(1);
2157 break;
2158 #ifdef CONFIG_SLIRP
2159 case QEMU_OPTION_tftp:
2160 legacy_tftp_prefix = optarg;
2161 break;
2162 case QEMU_OPTION_bootp:
2163 legacy_bootp_filename = optarg;
2164 break;
2165 case QEMU_OPTION_redir:
2166 if (net_slirp_redir(optarg) < 0)
2167 exit(1);
2168 break;
2169 #endif
2170 case QEMU_OPTION_bt:
2171 add_device_config(DEV_BT, optarg);
2172 break;
2173 case QEMU_OPTION_audio_help:
2174 if (!(audio_available())) {
2175 printf("Option %s not supported for this target\n", popt->name);
2176 exit(1);
2178 AUD_help ();
2179 exit (0);
2180 break;
2181 case QEMU_OPTION_soundhw:
2182 if (!(audio_available())) {
2183 printf("Option %s not supported for this target\n", popt->name);
2184 exit(1);
2186 select_soundhw (optarg);
2187 break;
2188 case QEMU_OPTION_h:
2189 help(0);
2190 break;
2191 case QEMU_OPTION_version:
2192 version();
2193 exit(0);
2194 break;
2195 case QEMU_OPTION_m: {
2196 ssize_t value;
2198 value = strtosz(optarg, NULL);
2199 if (value < 0) {
2200 fprintf(stderr, "qemu: invalid ram size: %s\n", optarg);
2201 exit(1);
2204 /* On 32-bit hosts, QEMU is limited by virtual address space */
2205 if (value > (2047 << 20) && HOST_LONG_BITS == 32) {
2206 fprintf(stderr, "qemu: at most 2047 MB RAM can be simulated\n");
2207 exit(1);
2209 if (value != (uint64_t)(ram_addr_t)value) {
2210 fprintf(stderr, "qemu: ram size too large\n");
2211 exit(1);
2213 ram_size = value;
2214 break;
2216 case QEMU_OPTION_mempath:
2217 mem_path = optarg;
2218 break;
2219 #ifdef MAP_POPULATE
2220 case QEMU_OPTION_mem_prealloc:
2221 mem_prealloc = 1;
2222 break;
2223 #endif
2224 case QEMU_OPTION_d:
2225 set_cpu_log(optarg);
2226 break;
2227 case QEMU_OPTION_s:
2228 gdbstub_dev = "tcp::" DEFAULT_GDBSTUB_PORT;
2229 break;
2230 case QEMU_OPTION_gdb:
2231 gdbstub_dev = optarg;
2232 break;
2233 case QEMU_OPTION_L:
2234 data_dir = optarg;
2235 break;
2236 case QEMU_OPTION_bios:
2237 bios_name = optarg;
2238 break;
2239 case QEMU_OPTION_singlestep:
2240 singlestep = 1;
2241 break;
2242 case QEMU_OPTION_S:
2243 autostart = 0;
2244 break;
2245 case QEMU_OPTION_k:
2246 keyboard_layout = optarg;
2247 break;
2248 case QEMU_OPTION_localtime:
2249 rtc_utc = 0;
2250 break;
2251 case QEMU_OPTION_vga:
2252 select_vgahw (optarg);
2253 break;
2254 case QEMU_OPTION_g:
2256 const char *p;
2257 int w, h, depth;
2258 p = optarg;
2259 w = strtol(p, (char **)&p, 10);
2260 if (w <= 0) {
2261 graphic_error:
2262 fprintf(stderr, "qemu: invalid resolution or depth\n");
2263 exit(1);
2265 if (*p != 'x')
2266 goto graphic_error;
2267 p++;
2268 h = strtol(p, (char **)&p, 10);
2269 if (h <= 0)
2270 goto graphic_error;
2271 if (*p == 'x') {
2272 p++;
2273 depth = strtol(p, (char **)&p, 10);
2274 if (depth != 8 && depth != 15 && depth != 16 &&
2275 depth != 24 && depth != 32)
2276 goto graphic_error;
2277 } else if (*p == '\0') {
2278 depth = graphic_depth;
2279 } else {
2280 goto graphic_error;
2283 graphic_width = w;
2284 graphic_height = h;
2285 graphic_depth = depth;
2287 break;
2288 case QEMU_OPTION_echr:
2290 char *r;
2291 term_escape_char = strtol(optarg, &r, 0);
2292 if (r == optarg)
2293 printf("Bad argument to echr\n");
2294 break;
2296 case QEMU_OPTION_monitor:
2297 monitor_parse(optarg, "readline");
2298 default_monitor = 0;
2299 break;
2300 case QEMU_OPTION_qmp:
2301 monitor_parse(optarg, "control");
2302 default_monitor = 0;
2303 break;
2304 case QEMU_OPTION_mon:
2305 opts = qemu_opts_parse(qemu_find_opts("mon"), optarg, 1);
2306 if (!opts) {
2307 exit(1);
2309 default_monitor = 0;
2310 break;
2311 case QEMU_OPTION_chardev:
2312 opts = qemu_opts_parse(qemu_find_opts("chardev"), optarg, 1);
2313 if (!opts) {
2314 exit(1);
2316 break;
2317 case QEMU_OPTION_fsdev:
2318 olist = qemu_find_opts("fsdev");
2319 if (!olist) {
2320 fprintf(stderr, "fsdev is not supported by this qemu build.\n");
2321 exit(1);
2323 opts = qemu_opts_parse(olist, optarg, 1);
2324 if (!opts) {
2325 fprintf(stderr, "parse error: %s\n", optarg);
2326 exit(1);
2328 break;
2329 case QEMU_OPTION_virtfs: {
2330 char *arg_fsdev = NULL;
2331 char *arg_9p = NULL;
2332 int len = 0;
2334 olist = qemu_find_opts("virtfs");
2335 if (!olist) {
2336 fprintf(stderr, "virtfs is not supported by this qemu build.\n");
2337 exit(1);
2339 opts = qemu_opts_parse(olist, optarg, 1);
2340 if (!opts) {
2341 fprintf(stderr, "parse error: %s\n", optarg);
2342 exit(1);
2345 if (qemu_opt_get(opts, "fstype") == NULL ||
2346 qemu_opt_get(opts, "mount_tag") == NULL ||
2347 qemu_opt_get(opts, "path") == NULL ||
2348 qemu_opt_get(opts, "security_model") == NULL) {
2349 fprintf(stderr, "Usage: -virtfs fstype,path=/share_path/,"
2350 "security_model=[mapped|passthrough|none],"
2351 "mnt_tag=tag.\n");
2352 exit(1);
2355 len = strlen(",id=,path=,security_model=");
2356 len += strlen(qemu_opt_get(opts, "fstype"));
2357 len += strlen(qemu_opt_get(opts, "mount_tag"));
2358 len += strlen(qemu_opt_get(opts, "path"));
2359 len += strlen(qemu_opt_get(opts, "security_model"));
2360 arg_fsdev = qemu_malloc((len + 1) * sizeof(*arg_fsdev));
2362 snprintf(arg_fsdev, (len + 1) * sizeof(*arg_fsdev),
2363 "%s,id=%s,path=%s,security_model=%s",
2364 qemu_opt_get(opts, "fstype"),
2365 qemu_opt_get(opts, "mount_tag"),
2366 qemu_opt_get(opts, "path"),
2367 qemu_opt_get(opts, "security_model"));
2369 len = strlen("virtio-9p-pci,fsdev=,mount_tag=");
2370 len += 2*strlen(qemu_opt_get(opts, "mount_tag"));
2371 arg_9p = qemu_malloc((len + 1) * sizeof(*arg_9p));
2373 snprintf(arg_9p, (len + 1) * sizeof(*arg_9p),
2374 "virtio-9p-pci,fsdev=%s,mount_tag=%s",
2375 qemu_opt_get(opts, "mount_tag"),
2376 qemu_opt_get(opts, "mount_tag"));
2378 if (!qemu_opts_parse(qemu_find_opts("fsdev"), arg_fsdev, 1)) {
2379 fprintf(stderr, "parse error [fsdev]: %s\n", optarg);
2380 exit(1);
2383 if (!qemu_opts_parse(qemu_find_opts("device"), arg_9p, 1)) {
2384 fprintf(stderr, "parse error [device]: %s\n", optarg);
2385 exit(1);
2388 qemu_free(arg_fsdev);
2389 qemu_free(arg_9p);
2390 break;
2392 case QEMU_OPTION_serial:
2393 add_device_config(DEV_SERIAL, optarg);
2394 default_serial = 0;
2395 if (strncmp(optarg, "mon:", 4) == 0) {
2396 default_monitor = 0;
2398 break;
2399 case QEMU_OPTION_watchdog:
2400 if (watchdog) {
2401 fprintf(stderr,
2402 "qemu: only one watchdog option may be given\n");
2403 return 1;
2405 watchdog = optarg;
2406 break;
2407 case QEMU_OPTION_watchdog_action:
2408 if (select_watchdog_action(optarg) == -1) {
2409 fprintf(stderr, "Unknown -watchdog-action parameter\n");
2410 exit(1);
2412 break;
2413 case QEMU_OPTION_virtiocon:
2414 add_device_config(DEV_VIRTCON, optarg);
2415 default_virtcon = 0;
2416 if (strncmp(optarg, "mon:", 4) == 0) {
2417 default_monitor = 0;
2419 break;
2420 case QEMU_OPTION_parallel:
2421 add_device_config(DEV_PARALLEL, optarg);
2422 default_parallel = 0;
2423 if (strncmp(optarg, "mon:", 4) == 0) {
2424 default_monitor = 0;
2426 break;
2427 case QEMU_OPTION_debugcon:
2428 add_device_config(DEV_DEBUGCON, optarg);
2429 break;
2430 case QEMU_OPTION_loadvm:
2431 loadvm = optarg;
2432 break;
2433 case QEMU_OPTION_full_screen:
2434 full_screen = 1;
2435 break;
2436 #ifdef CONFIG_SDL
2437 case QEMU_OPTION_no_frame:
2438 no_frame = 1;
2439 break;
2440 case QEMU_OPTION_alt_grab:
2441 alt_grab = 1;
2442 break;
2443 case QEMU_OPTION_ctrl_grab:
2444 ctrl_grab = 1;
2445 break;
2446 case QEMU_OPTION_no_quit:
2447 no_quit = 1;
2448 break;
2449 case QEMU_OPTION_sdl:
2450 display_type = DT_SDL;
2451 break;
2452 #endif
2453 case QEMU_OPTION_pidfile:
2454 pid_file = optarg;
2455 break;
2456 case QEMU_OPTION_win2k_hack:
2457 win2k_install_hack = 1;
2458 break;
2459 case QEMU_OPTION_rtc_td_hack:
2460 rtc_td_hack = 1;
2461 break;
2462 case QEMU_OPTION_acpitable:
2463 do_acpitable_option(optarg);
2464 break;
2465 case QEMU_OPTION_smbios:
2466 do_smbios_option(optarg);
2467 break;
2468 case QEMU_OPTION_enable_kvm:
2469 kvm_allowed = 1;
2470 break;
2471 case QEMU_OPTION_usb:
2472 usb_enabled = 1;
2473 break;
2474 case QEMU_OPTION_usbdevice:
2475 usb_enabled = 1;
2476 add_device_config(DEV_USB, optarg);
2477 break;
2478 case QEMU_OPTION_device:
2479 if (!qemu_opts_parse(qemu_find_opts("device"), optarg, 1)) {
2480 exit(1);
2482 break;
2483 case QEMU_OPTION_smp:
2484 smp_parse(optarg);
2485 if (smp_cpus < 1) {
2486 fprintf(stderr, "Invalid number of CPUs\n");
2487 exit(1);
2489 if (max_cpus < smp_cpus) {
2490 fprintf(stderr, "maxcpus must be equal to or greater than "
2491 "smp\n");
2492 exit(1);
2494 if (max_cpus > 255) {
2495 fprintf(stderr, "Unsupported number of maxcpus\n");
2496 exit(1);
2498 break;
2499 case QEMU_OPTION_vnc:
2500 display_remote++;
2501 vnc_display = optarg;
2502 break;
2503 case QEMU_OPTION_no_acpi:
2504 acpi_enabled = 0;
2505 break;
2506 case QEMU_OPTION_no_hpet:
2507 no_hpet = 1;
2508 break;
2509 case QEMU_OPTION_balloon:
2510 if (balloon_parse(optarg) < 0) {
2511 fprintf(stderr, "Unknown -balloon argument %s\n", optarg);
2512 exit(1);
2514 break;
2515 case QEMU_OPTION_no_reboot:
2516 no_reboot = 1;
2517 break;
2518 case QEMU_OPTION_no_shutdown:
2519 no_shutdown = 1;
2520 break;
2521 case QEMU_OPTION_show_cursor:
2522 cursor_hide = 0;
2523 break;
2524 case QEMU_OPTION_uuid:
2525 if(qemu_uuid_parse(optarg, qemu_uuid) < 0) {
2526 fprintf(stderr, "Fail to parse UUID string."
2527 " Wrong format.\n");
2528 exit(1);
2530 break;
2531 case QEMU_OPTION_option_rom:
2532 if (nb_option_roms >= MAX_OPTION_ROMS) {
2533 fprintf(stderr, "Too many option ROMs\n");
2534 exit(1);
2536 opts = qemu_opts_parse(qemu_find_opts("option-rom"), optarg, 1);
2537 option_rom[nb_option_roms].name = qemu_opt_get(opts, "romfile");
2538 option_rom[nb_option_roms].bootindex =
2539 qemu_opt_get_number(opts, "bootindex", -1);
2540 if (!option_rom[nb_option_roms].name) {
2541 fprintf(stderr, "Option ROM file is not specified\n");
2542 exit(1);
2544 nb_option_roms++;
2545 break;
2546 case QEMU_OPTION_semihosting:
2547 semihosting_enabled = 1;
2548 break;
2549 case QEMU_OPTION_name:
2550 qemu_name = qemu_strdup(optarg);
2552 char *p = strchr(qemu_name, ',');
2553 if (p != NULL) {
2554 *p++ = 0;
2555 if (strncmp(p, "process=", 8)) {
2556 fprintf(stderr, "Unknown subargument %s to -name", p);
2557 exit(1);
2559 p += 8;
2560 os_set_proc_name(p);
2563 break;
2564 case QEMU_OPTION_prom_env:
2565 if (nb_prom_envs >= MAX_PROM_ENVS) {
2566 fprintf(stderr, "Too many prom variables\n");
2567 exit(1);
2569 prom_envs[nb_prom_envs] = optarg;
2570 nb_prom_envs++;
2571 break;
2572 case QEMU_OPTION_old_param:
2573 old_param = 1;
2574 break;
2575 case QEMU_OPTION_clock:
2576 configure_alarms(optarg);
2577 break;
2578 case QEMU_OPTION_startdate:
2579 configure_rtc_date_offset(optarg, 1);
2580 break;
2581 case QEMU_OPTION_rtc:
2582 opts = qemu_opts_parse(qemu_find_opts("rtc"), optarg, 0);
2583 if (!opts) {
2584 exit(1);
2586 configure_rtc(opts);
2587 break;
2588 case QEMU_OPTION_tb_size:
2589 tb_size = strtol(optarg, NULL, 0);
2590 if (tb_size < 0)
2591 tb_size = 0;
2592 break;
2593 case QEMU_OPTION_icount:
2594 icount_option = optarg;
2595 break;
2596 case QEMU_OPTION_incoming:
2597 incoming = optarg;
2598 incoming_expected = true;
2599 break;
2600 case QEMU_OPTION_nodefaults:
2601 default_serial = 0;
2602 default_parallel = 0;
2603 default_virtcon = 0;
2604 default_monitor = 0;
2605 default_vga = 0;
2606 default_net = 0;
2607 default_floppy = 0;
2608 default_cdrom = 0;
2609 default_sdcard = 0;
2610 break;
2611 case QEMU_OPTION_xen_domid:
2612 if (!(xen_available())) {
2613 printf("Option %s not supported for this target\n", popt->name);
2614 exit(1);
2616 xen_domid = atoi(optarg);
2617 break;
2618 case QEMU_OPTION_xen_create:
2619 if (!(xen_available())) {
2620 printf("Option %s not supported for this target\n", popt->name);
2621 exit(1);
2623 xen_mode = XEN_CREATE;
2624 break;
2625 case QEMU_OPTION_xen_attach:
2626 if (!(xen_available())) {
2627 printf("Option %s not supported for this target\n", popt->name);
2628 exit(1);
2630 xen_mode = XEN_ATTACH;
2631 break;
2632 #ifdef CONFIG_SIMPLE_TRACE
2633 case QEMU_OPTION_trace:
2634 opts = qemu_opts_parse(qemu_find_opts("trace"), optarg, 0);
2635 if (opts) {
2636 trace_file = qemu_opt_get(opts, "file");
2638 break;
2639 #endif
2640 case QEMU_OPTION_readconfig:
2642 int ret = qemu_read_config_file(optarg);
2643 if (ret < 0) {
2644 fprintf(stderr, "read config %s: %s\n", optarg,
2645 strerror(-ret));
2646 exit(1);
2648 break;
2650 case QEMU_OPTION_spice:
2651 olist = qemu_find_opts("spice");
2652 if (!olist) {
2653 fprintf(stderr, "spice is not supported by this qemu build.\n");
2654 exit(1);
2656 opts = qemu_opts_parse(olist, optarg, 0);
2657 if (!opts) {
2658 fprintf(stderr, "parse error: %s\n", optarg);
2659 exit(1);
2661 break;
2662 case QEMU_OPTION_writeconfig:
2664 FILE *fp;
2665 if (strcmp(optarg, "-") == 0) {
2666 fp = stdout;
2667 } else {
2668 fp = fopen(optarg, "w");
2669 if (fp == NULL) {
2670 fprintf(stderr, "open %s: %s\n", optarg, strerror(errno));
2671 exit(1);
2674 qemu_config_write(fp);
2675 fclose(fp);
2676 break;
2678 default:
2679 os_parse_cmd_args(popt->index, optarg);
2683 loc_set_none();
2685 /* If no data_dir is specified then try to find it relative to the
2686 executable path. */
2687 if (!data_dir) {
2688 data_dir = os_find_datadir(argv[0]);
2690 /* If all else fails use the install patch specified when building. */
2691 if (!data_dir) {
2692 data_dir = CONFIG_QEMU_DATADIR;
2695 #ifdef CONFIG_SIMPLE_TRACE
2697 * Set the trace file name, if specified.
2699 st_set_trace_file(trace_file);
2700 #endif
2702 * Default to max_cpus = smp_cpus, in case the user doesn't
2703 * specify a max_cpus value.
2705 if (!max_cpus)
2706 max_cpus = smp_cpus;
2708 machine->max_cpus = machine->max_cpus ?: 1; /* Default to UP */
2709 if (smp_cpus > machine->max_cpus) {
2710 fprintf(stderr, "Number of SMP cpus requested (%d), exceeds max cpus "
2711 "supported by machine `%s' (%d)\n", smp_cpus, machine->name,
2712 machine->max_cpus);
2713 exit(1);
2716 qemu_opts_foreach(qemu_find_opts("device"), default_driver_check, NULL, 0);
2717 qemu_opts_foreach(qemu_find_opts("global"), default_driver_check, NULL, 0);
2719 if (machine->no_serial) {
2720 default_serial = 0;
2722 if (machine->no_parallel) {
2723 default_parallel = 0;
2725 if (!machine->use_virtcon) {
2726 default_virtcon = 0;
2728 if (machine->no_vga) {
2729 default_vga = 0;
2731 if (machine->no_floppy) {
2732 default_floppy = 0;
2734 if (machine->no_cdrom) {
2735 default_cdrom = 0;
2737 if (machine->no_sdcard) {
2738 default_sdcard = 0;
2741 if (display_type == DT_NOGRAPHIC) {
2742 if (default_parallel)
2743 add_device_config(DEV_PARALLEL, "null");
2744 if (default_serial && default_monitor) {
2745 add_device_config(DEV_SERIAL, "mon:stdio");
2746 } else if (default_virtcon && default_monitor) {
2747 add_device_config(DEV_VIRTCON, "mon:stdio");
2748 } else {
2749 if (default_serial)
2750 add_device_config(DEV_SERIAL, "stdio");
2751 if (default_virtcon)
2752 add_device_config(DEV_VIRTCON, "stdio");
2753 if (default_monitor)
2754 monitor_parse("stdio", "readline");
2756 } else {
2757 if (default_serial)
2758 add_device_config(DEV_SERIAL, "vc:80Cx24C");
2759 if (default_parallel)
2760 add_device_config(DEV_PARALLEL, "vc:80Cx24C");
2761 if (default_monitor)
2762 monitor_parse("vc:80Cx24C", "readline");
2763 if (default_virtcon)
2764 add_device_config(DEV_VIRTCON, "vc:80Cx24C");
2766 if (default_vga)
2767 vga_interface_type = VGA_CIRRUS;
2769 socket_init();
2771 if (qemu_opts_foreach(qemu_find_opts("chardev"), chardev_init_func, NULL, 1) != 0)
2772 exit(1);
2773 #ifdef CONFIG_VIRTFS
2774 if (qemu_opts_foreach(qemu_find_opts("fsdev"), fsdev_init_func, NULL, 1) != 0) {
2775 exit(1);
2777 #endif
2779 os_daemonize();
2781 if (pid_file && qemu_create_pidfile(pid_file) != 0) {
2782 os_pidfile_error();
2783 exit(1);
2786 if (kvm_allowed) {
2787 int ret = kvm_init(smp_cpus);
2788 if (ret < 0) {
2789 if (!kvm_available()) {
2790 printf("KVM not supported for this target\n");
2791 } else {
2792 fprintf(stderr, "failed to initialize KVM: %s\n", strerror(-ret));
2794 exit(1);
2798 if (qemu_init_main_loop()) {
2799 fprintf(stderr, "qemu_init_main_loop failed\n");
2800 exit(1);
2802 linux_boot = (kernel_filename != NULL);
2804 if (!linux_boot && *kernel_cmdline != '\0') {
2805 fprintf(stderr, "-append only allowed with -kernel option\n");
2806 exit(1);
2809 if (!linux_boot && initrd_filename != NULL) {
2810 fprintf(stderr, "-initrd only allowed with -kernel option\n");
2811 exit(1);
2814 os_set_line_buffering();
2816 if (init_timer_alarm() < 0) {
2817 fprintf(stderr, "could not initialize alarm timer\n");
2818 exit(1);
2820 configure_icount(icount_option);
2822 if (net_init_clients() < 0) {
2823 exit(1);
2826 /* init the bluetooth world */
2827 if (foreach_device_config(DEV_BT, bt_parse))
2828 exit(1);
2830 /* init the memory */
2831 if (ram_size == 0)
2832 ram_size = DEFAULT_RAM_SIZE * 1024 * 1024;
2834 /* init the dynamic translator */
2835 cpu_exec_init_all(tb_size * 1024 * 1024);
2837 bdrv_init_with_whitelist();
2839 blk_mig_init();
2841 if (default_cdrom) {
2842 /* we always create the cdrom drive, even if no disk is there */
2843 drive_add(NULL, CDROM_ALIAS);
2846 if (default_floppy) {
2847 /* we always create at least one floppy */
2848 drive_add(NULL, FD_ALIAS, 0);
2851 if (default_sdcard) {
2852 /* we always create one sd slot, even if no card is in it */
2853 drive_add(NULL, SD_ALIAS);
2856 /* open the virtual block devices */
2857 if (snapshot)
2858 qemu_opts_foreach(qemu_find_opts("drive"), drive_enable_snapshot, NULL, 0);
2859 if (qemu_opts_foreach(qemu_find_opts("drive"), drive_init_func, &machine->use_scsi, 1) != 0)
2860 exit(1);
2862 register_savevm_live(NULL, "ram", 0, 4, NULL, ram_save_live, NULL,
2863 ram_load, NULL);
2865 if (nb_numa_nodes > 0) {
2866 int i;
2868 if (nb_numa_nodes > smp_cpus) {
2869 nb_numa_nodes = smp_cpus;
2872 /* If no memory size if given for any node, assume the default case
2873 * and distribute the available memory equally across all nodes
2875 for (i = 0; i < nb_numa_nodes; i++) {
2876 if (node_mem[i] != 0)
2877 break;
2879 if (i == nb_numa_nodes) {
2880 uint64_t usedmem = 0;
2882 /* On Linux, the each node's border has to be 8MB aligned,
2883 * the final node gets the rest.
2885 for (i = 0; i < nb_numa_nodes - 1; i++) {
2886 node_mem[i] = (ram_size / nb_numa_nodes) & ~((1 << 23UL) - 1);
2887 usedmem += node_mem[i];
2889 node_mem[i] = ram_size - usedmem;
2892 for (i = 0; i < nb_numa_nodes; i++) {
2893 if (node_cpumask[i] != 0)
2894 break;
2896 /* assigning the VCPUs round-robin is easier to implement, guest OSes
2897 * must cope with this anyway, because there are BIOSes out there in
2898 * real machines which also use this scheme.
2900 if (i == nb_numa_nodes) {
2901 for (i = 0; i < smp_cpus; i++) {
2902 node_cpumask[i % nb_numa_nodes] |= 1 << i;
2907 if (qemu_opts_foreach(qemu_find_opts("mon"), mon_init_func, NULL, 1) != 0) {
2908 exit(1);
2911 if (foreach_device_config(DEV_SERIAL, serial_parse) < 0)
2912 exit(1);
2913 if (foreach_device_config(DEV_PARALLEL, parallel_parse) < 0)
2914 exit(1);
2915 if (foreach_device_config(DEV_VIRTCON, virtcon_parse) < 0)
2916 exit(1);
2917 if (foreach_device_config(DEV_DEBUGCON, debugcon_parse) < 0)
2918 exit(1);
2920 module_call_init(MODULE_INIT_DEVICE);
2922 if (qemu_opts_foreach(qemu_find_opts("device"), device_help_func, NULL, 0) != 0)
2923 exit(0);
2925 if (watchdog) {
2926 i = select_watchdog(watchdog);
2927 if (i > 0)
2928 exit (i == 1 ? 1 : 0);
2931 if (machine->compat_props) {
2932 qdev_prop_register_global_list(machine->compat_props);
2934 qemu_add_globals();
2936 machine->init(ram_size, boot_devices,
2937 kernel_filename, kernel_cmdline, initrd_filename, cpu_model);
2939 cpu_synchronize_all_post_init();
2941 /* must be after terminal init, SDL library changes signal handlers */
2942 os_setup_signal_handling();
2944 set_numa_modes();
2946 current_machine = machine;
2948 /* init USB devices */
2949 if (usb_enabled) {
2950 if (foreach_device_config(DEV_USB, usb_parse) < 0)
2951 exit(1);
2954 /* init generic devices */
2955 if (qemu_opts_foreach(qemu_find_opts("device"), device_init_func, NULL, 1) != 0)
2956 exit(1);
2958 net_check_clients();
2960 /* just use the first displaystate for the moment */
2961 ds = get_displaystate();
2963 if (using_spice)
2964 display_remote++;
2965 if (display_type == DT_DEFAULT && !display_remote) {
2966 #if defined(CONFIG_SDL) || defined(CONFIG_COCOA)
2967 display_type = DT_SDL;
2968 #else
2969 vnc_display = "localhost:0,to=99";
2970 show_vnc_port = 1;
2971 #endif
2975 /* init local displays */
2976 switch (display_type) {
2977 case DT_NOGRAPHIC:
2978 break;
2979 #if defined(CONFIG_CURSES)
2980 case DT_CURSES:
2981 curses_display_init(ds, full_screen);
2982 break;
2983 #endif
2984 #if defined(CONFIG_SDL)
2985 case DT_SDL:
2986 sdl_display_init(ds, full_screen, no_frame);
2987 break;
2988 #elif defined(CONFIG_COCOA)
2989 case DT_SDL:
2990 cocoa_display_init(ds, full_screen);
2991 break;
2992 #endif
2993 default:
2994 break;
2997 /* init remote displays */
2998 if (vnc_display) {
2999 vnc_display_init(ds);
3000 if (vnc_display_open(ds, vnc_display) < 0)
3001 exit(1);
3003 if (show_vnc_port) {
3004 printf("VNC server running on `%s'\n", vnc_display_local_addr(ds));
3007 #ifdef CONFIG_SPICE
3008 if (using_spice) {
3009 qemu_spice_display_init(ds);
3011 #endif
3013 /* display setup */
3014 dpy_resize(ds);
3015 dcl = ds->listeners;
3016 while (dcl != NULL) {
3017 if (dcl->dpy_refresh != NULL) {
3018 ds->gui_timer = qemu_new_timer(rt_clock, gui_update, ds);
3019 qemu_mod_timer(ds->gui_timer, qemu_get_clock(rt_clock));
3020 break;
3022 dcl = dcl->next;
3024 if (ds->gui_timer == NULL) {
3025 nographic_timer = qemu_new_timer(rt_clock, nographic_update, NULL);
3026 qemu_mod_timer(nographic_timer, qemu_get_clock(rt_clock));
3028 text_consoles_set_display(ds);
3030 if (gdbstub_dev && gdbserver_start(gdbstub_dev) < 0) {
3031 fprintf(stderr, "qemu: could not open gdbserver on device '%s'\n",
3032 gdbstub_dev);
3033 exit(1);
3036 qdev_machine_creation_done();
3038 if (rom_load_all() != 0) {
3039 fprintf(stderr, "rom loading failed\n");
3040 exit(1);
3043 qemu_register_reset((void *)qbus_reset_all, sysbus_get_default());
3044 qemu_run_machine_init_done_notifiers();
3046 qemu_system_reset();
3047 if (loadvm) {
3048 if (load_vmstate(loadvm) < 0) {
3049 autostart = 0;
3053 if (incoming) {
3054 int ret = qemu_start_incoming_migration(incoming);
3055 if (ret < 0) {
3056 fprintf(stderr, "Migration failed. Exit code %s(%d), exiting.\n",
3057 incoming, ret);
3058 exit(ret);
3060 } else if (autostart) {
3061 vm_start();
3064 os_setup_post();
3066 main_loop();
3067 quit_timers();
3068 net_cleanup();
3070 return 0;