qemu-kvm: pc: Do not start APIC timer spuriously
[qemu-kvm.git] / vl.c
blob465431842f640572579eb405ccc7d1cac1209416
1 /*
2 * QEMU System Emulator
4 * Copyright (c) 2003-2008 Fabrice Bellard
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 * THE SOFTWARE.
24 #include <unistd.h>
25 #include <fcntl.h>
26 #include <signal.h>
27 #include <time.h>
28 #include <errno.h>
29 #include <sys/time.h>
30 #include <zlib.h>
32 /* Needed early for CONFIG_BSD etc. */
33 #include "config-host.h"
35 #ifndef _WIN32
36 #include <libgen.h>
37 #include <sys/times.h>
38 #include <sys/wait.h>
39 #include <termios.h>
40 #include <sys/mman.h>
41 #include <sys/ioctl.h>
42 #include <sys/resource.h>
43 #include <sys/socket.h>
44 #include <netinet/in.h>
45 #include <net/if.h>
46 #include <arpa/inet.h>
47 #include <dirent.h>
48 #include <netdb.h>
49 #include <sys/select.h>
51 #ifdef CONFIG_BSD
52 #include <sys/stat.h>
53 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__DragonFly__)
54 #include <libutil.h>
55 #include <sys/sysctl.h>
56 #else
57 #include <util.h>
58 #endif
59 #else
60 #ifdef __linux__
61 #include <pty.h>
62 #include <malloc.h>
64 #include <linux/ppdev.h>
65 #include <linux/parport.h>
66 #endif
67 #ifdef __sun__
68 #include <sys/stat.h>
69 #include <sys/ethernet.h>
70 #include <sys/sockio.h>
71 #include <netinet/arp.h>
72 #include <netinet/in_systm.h>
73 #include <netinet/ip.h>
74 #include <netinet/ip_icmp.h> // must come after ip.h
75 #include <netinet/udp.h>
76 #include <netinet/tcp.h>
77 #include <net/if.h>
78 #include <syslog.h>
79 #include <stropts.h>
80 #endif
81 #endif
82 #endif
84 #if defined(__OpenBSD__)
85 #include <util.h>
86 #endif
88 #if defined(CONFIG_VDE)
89 #include <libvdeplug.h>
90 #endif
92 #ifdef _WIN32
93 #include <windows.h>
94 #endif
96 #ifdef CONFIG_SDL
97 #if defined(__APPLE__) || defined(main)
98 #include <SDL.h>
99 int qemu_main(int argc, char **argv, char **envp);
100 int main(int argc, char **argv)
102 return qemu_main(argc, argv, NULL);
104 #undef main
105 #define main qemu_main
106 #endif
107 #endif /* CONFIG_SDL */
109 #ifdef CONFIG_COCOA
110 #undef main
111 #define main qemu_main
112 #endif /* CONFIG_COCOA */
114 #include <glib.h>
116 #include "hw/hw.h"
117 #include "hw/boards.h"
118 #include "hw/usb.h"
119 #include "hw/pcmcia.h"
120 #include "hw/pc.h"
121 #include "hw/isa.h"
122 #include "hw/baum.h"
123 #include "hw/bt.h"
124 #include "hw/watchdog.h"
125 #include "hw/smbios.h"
126 #include "hw/xen.h"
127 #include "hw/qdev.h"
128 #include "hw/loader.h"
129 #include "bt-host.h"
130 #include "net.h"
131 #include "net/slirp.h"
132 #include "monitor.h"
133 #include "console.h"
134 #include "sysemu.h"
135 #include "gdbstub.h"
136 #include "qemu-timer.h"
137 #include "qemu-char.h"
138 #include "cache-utils.h"
139 #include "block.h"
140 #include "blockdev.h"
141 #include "block-migration.h"
142 #include "dma.h"
143 #include "audio/audio.h"
144 #include "migration.h"
145 #include "kvm.h"
146 #include "qemu-option.h"
147 #include "qemu-config.h"
148 #include "qemu-objects.h"
149 #include "qemu-options.h"
150 #include "kvm.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 "trace/control.h"
163 #include "qemu-queue.h"
164 #include "cpus.h"
165 #include "arch_init.h"
167 #include "ui/qemu-spice.h"
169 //#define DEBUG_NET
170 //#define DEBUG_SLIRP
172 #define DEFAULT_RAM_SIZE 128
174 #define MAX_VIRTIO_CONSOLES 1
176 static const char *data_dir;
177 const char *bios_name = NULL;
178 enum vga_retrace_method vga_retrace_method = VGA_RETRACE_DUMB;
179 DisplayType display_type = DT_DEFAULT;
180 int display_remote = 0;
181 const char* keyboard_layout = NULL;
182 ram_addr_t ram_size;
183 const char *mem_path = NULL;
184 #ifdef MAP_POPULATE
185 int mem_prealloc = 0; /* force preallocation of physical target memory */
186 #endif
187 int nb_nics;
188 NICInfo nd_table[MAX_NICS];
189 int vm_running;
190 int autostart;
191 int incoming_expected; /* Started with -incoming and waiting for incoming */
192 static int rtc_utc = 1;
193 static int rtc_date_offset = -1; /* -1 means no change */
194 QEMUClock *rtc_clock;
195 int vga_interface_type = VGA_NONE;
196 static int full_screen = 0;
197 #ifdef CONFIG_SDL
198 static int no_frame = 0;
199 #endif
200 int no_quit = 0;
201 CharDriverState *serial_hds[MAX_SERIAL_PORTS];
202 CharDriverState *parallel_hds[MAX_PARALLEL_PORTS];
203 CharDriverState *virtcon_hds[MAX_VIRTIO_CONSOLES];
204 int win2k_install_hack = 0;
205 int rtc_td_hack = 0;
206 int usb_enabled = 0;
207 int singlestep = 0;
208 int smp_cpus = 1;
209 int max_cpus = 0;
210 int smp_cores = 1;
211 int smp_threads = 1;
212 #ifdef CONFIG_VNC
213 const char *vnc_display;
214 #endif
215 int acpi_enabled = 1;
216 int no_hpet = 0;
217 int fd_bootchk = 1;
218 int no_reboot = 0;
219 int no_shutdown = 0;
220 int cursor_hide = 1;
221 int graphic_rotate = 0;
222 const char *watchdog;
223 QEMUOptionRom option_rom[MAX_OPTION_ROMS];
224 int nb_option_roms;
225 int semihosting_enabled = 0;
226 int time_drift_fix = 0;
227 unsigned int kvm_shadow_memory = 0;
228 int old_param = 0;
229 const char *qemu_name;
230 int alt_grab = 0;
231 int ctrl_grab = 0;
232 unsigned int nb_prom_envs = 0;
233 const char *prom_envs[MAX_PROM_ENVS];
234 const char *nvram = NULL;
235 int boot_menu;
236 uint8_t *boot_splash_filedata;
237 int boot_splash_filedata_size;
238 uint8_t qemu_extra_params_fw[2];
240 typedef struct FWBootEntry FWBootEntry;
242 struct FWBootEntry {
243 QTAILQ_ENTRY(FWBootEntry) link;
244 int32_t bootindex;
245 DeviceState *dev;
246 char *suffix;
249 QTAILQ_HEAD(, FWBootEntry) fw_boot_order = QTAILQ_HEAD_INITIALIZER(fw_boot_order);
251 int nb_numa_nodes;
252 uint64_t node_mem[MAX_NODES];
253 uint64_t node_cpumask[MAX_NODES];
255 static QEMUTimer *nographic_timer;
257 uint8_t qemu_uuid[16];
259 static QEMUBootSetHandler *boot_set_handler;
260 static void *boot_set_opaque;
262 static NotifierList exit_notifiers =
263 NOTIFIER_LIST_INITIALIZER(exit_notifiers);
265 static NotifierList machine_init_done_notifiers =
266 NOTIFIER_LIST_INITIALIZER(machine_init_done_notifiers);
268 static int tcg_allowed = 1;
269 int kvm_allowed = 0;
270 int xen_allowed = 0;
271 uint32_t xen_domid;
272 enum xen_mode xen_mode = XEN_EMULATE;
273 static int tcg_tb_size;
275 static int default_serial = 1;
276 static int default_parallel = 1;
277 static int default_virtcon = 1;
278 static int default_monitor = 1;
279 static int default_vga = 1;
280 static int default_floppy = 1;
281 static int default_cdrom = 1;
282 static int default_sdcard = 1;
284 static struct {
285 const char *driver;
286 int *flag;
287 } default_list[] = {
288 { .driver = "isa-serial", .flag = &default_serial },
289 { .driver = "isa-parallel", .flag = &default_parallel },
290 { .driver = "isa-fdc", .flag = &default_floppy },
291 { .driver = "ide-cd", .flag = &default_cdrom },
292 { .driver = "ide-hd", .flag = &default_cdrom },
293 { .driver = "ide-drive", .flag = &default_cdrom },
294 { .driver = "scsi-cd", .flag = &default_cdrom },
295 { .driver = "virtio-serial-pci", .flag = &default_virtcon },
296 { .driver = "virtio-serial-s390", .flag = &default_virtcon },
297 { .driver = "virtio-serial", .flag = &default_virtcon },
298 { .driver = "VGA", .flag = &default_vga },
299 { .driver = "cirrus-vga", .flag = &default_vga },
300 { .driver = "vmware-svga", .flag = &default_vga },
301 { .driver = "isa-vga", .flag = &default_vga },
302 { .driver = "qxl-vga", .flag = &default_vga },
305 static void res_free(void)
307 if (boot_splash_filedata != NULL) {
308 g_free(boot_splash_filedata);
309 boot_splash_filedata = NULL;
313 static int default_driver_check(QemuOpts *opts, void *opaque)
315 const char *driver = qemu_opt_get(opts, "driver");
316 int i;
318 if (!driver)
319 return 0;
320 for (i = 0; i < ARRAY_SIZE(default_list); i++) {
321 if (strcmp(default_list[i].driver, driver) != 0)
322 continue;
323 *(default_list[i].flag) = 0;
325 return 0;
328 /***********************************************************/
329 /* real time host monotonic timer */
331 /***********************************************************/
332 /* host time/date access */
333 void qemu_get_timedate(struct tm *tm, int offset)
335 time_t ti;
336 struct tm *ret;
338 time(&ti);
339 ti += offset;
340 if (rtc_date_offset == -1) {
341 if (rtc_utc)
342 ret = gmtime(&ti);
343 else
344 ret = localtime(&ti);
345 } else {
346 ti -= rtc_date_offset;
347 ret = gmtime(&ti);
350 memcpy(tm, ret, sizeof(struct tm));
353 int qemu_timedate_diff(struct tm *tm)
355 time_t seconds;
357 if (rtc_date_offset == -1)
358 if (rtc_utc)
359 seconds = mktimegm(tm);
360 else
361 seconds = mktime(tm);
362 else
363 seconds = mktimegm(tm) + rtc_date_offset;
365 return seconds - time(NULL);
368 void rtc_change_mon_event(struct tm *tm)
370 QObject *data;
372 data = qobject_from_jsonf("{ 'offset': %d }", qemu_timedate_diff(tm));
373 monitor_protocol_event(QEVENT_RTC_CHANGE, data);
374 qobject_decref(data);
377 static void configure_rtc_date_offset(const char *startdate, int legacy)
379 time_t rtc_start_date;
380 struct tm tm;
382 if (!strcmp(startdate, "now") && legacy) {
383 rtc_date_offset = -1;
384 } else {
385 if (sscanf(startdate, "%d-%d-%dT%d:%d:%d",
386 &tm.tm_year,
387 &tm.tm_mon,
388 &tm.tm_mday,
389 &tm.tm_hour,
390 &tm.tm_min,
391 &tm.tm_sec) == 6) {
392 /* OK */
393 } else if (sscanf(startdate, "%d-%d-%d",
394 &tm.tm_year,
395 &tm.tm_mon,
396 &tm.tm_mday) == 3) {
397 tm.tm_hour = 0;
398 tm.tm_min = 0;
399 tm.tm_sec = 0;
400 } else {
401 goto date_fail;
403 tm.tm_year -= 1900;
404 tm.tm_mon--;
405 rtc_start_date = mktimegm(&tm);
406 if (rtc_start_date == -1) {
407 date_fail:
408 fprintf(stderr, "Invalid date format. Valid formats are:\n"
409 "'2006-06-17T16:01:21' or '2006-06-17'\n");
410 exit(1);
412 rtc_date_offset = time(NULL) - rtc_start_date;
416 static void configure_rtc(QemuOpts *opts)
418 const char *value;
420 value = qemu_opt_get(opts, "base");
421 if (value) {
422 if (!strcmp(value, "utc")) {
423 rtc_utc = 1;
424 } else if (!strcmp(value, "localtime")) {
425 rtc_utc = 0;
426 } else {
427 configure_rtc_date_offset(value, 0);
430 value = qemu_opt_get(opts, "clock");
431 if (value) {
432 if (!strcmp(value, "host")) {
433 rtc_clock = host_clock;
434 } else if (!strcmp(value, "vm")) {
435 rtc_clock = vm_clock;
436 } else {
437 fprintf(stderr, "qemu: invalid option value '%s'\n", value);
438 exit(1);
441 value = qemu_opt_get(opts, "driftfix");
442 if (value) {
443 if (!strcmp(value, "slew")) {
444 rtc_td_hack = 1;
445 } else if (!strcmp(value, "none")) {
446 rtc_td_hack = 0;
447 } else {
448 fprintf(stderr, "qemu: invalid option value '%s'\n", value);
449 exit(1);
454 /***********************************************************/
455 /* Bluetooth support */
456 static int nb_hcis;
457 static int cur_hci;
458 static struct HCIInfo *hci_table[MAX_NICS];
460 static struct bt_vlan_s {
461 struct bt_scatternet_s net;
462 int id;
463 struct bt_vlan_s *next;
464 } *first_bt_vlan;
466 /* find or alloc a new bluetooth "VLAN" */
467 static struct bt_scatternet_s *qemu_find_bt_vlan(int id)
469 struct bt_vlan_s **pvlan, *vlan;
470 for (vlan = first_bt_vlan; vlan != NULL; vlan = vlan->next) {
471 if (vlan->id == id)
472 return &vlan->net;
474 vlan = g_malloc0(sizeof(struct bt_vlan_s));
475 vlan->id = id;
476 pvlan = &first_bt_vlan;
477 while (*pvlan != NULL)
478 pvlan = &(*pvlan)->next;
479 *pvlan = vlan;
480 return &vlan->net;
483 static void null_hci_send(struct HCIInfo *hci, const uint8_t *data, int len)
487 static int null_hci_addr_set(struct HCIInfo *hci, const uint8_t *bd_addr)
489 return -ENOTSUP;
492 static struct HCIInfo null_hci = {
493 .cmd_send = null_hci_send,
494 .sco_send = null_hci_send,
495 .acl_send = null_hci_send,
496 .bdaddr_set = null_hci_addr_set,
499 struct HCIInfo *qemu_next_hci(void)
501 if (cur_hci == nb_hcis)
502 return &null_hci;
504 return hci_table[cur_hci++];
507 static struct HCIInfo *hci_init(const char *str)
509 char *endp;
510 struct bt_scatternet_s *vlan = 0;
512 if (!strcmp(str, "null"))
513 /* null */
514 return &null_hci;
515 else if (!strncmp(str, "host", 4) && (str[4] == '\0' || str[4] == ':'))
516 /* host[:hciN] */
517 return bt_host_hci(str[4] ? str + 5 : "hci0");
518 else if (!strncmp(str, "hci", 3)) {
519 /* hci[,vlan=n] */
520 if (str[3]) {
521 if (!strncmp(str + 3, ",vlan=", 6)) {
522 vlan = qemu_find_bt_vlan(strtol(str + 9, &endp, 0));
523 if (*endp)
524 vlan = 0;
526 } else
527 vlan = qemu_find_bt_vlan(0);
528 if (vlan)
529 return bt_new_hci(vlan);
532 fprintf(stderr, "qemu: Unknown bluetooth HCI `%s'.\n", str);
534 return 0;
537 static int bt_hci_parse(const char *str)
539 struct HCIInfo *hci;
540 bdaddr_t bdaddr;
542 if (nb_hcis >= MAX_NICS) {
543 fprintf(stderr, "qemu: Too many bluetooth HCIs (max %i).\n", MAX_NICS);
544 return -1;
547 hci = hci_init(str);
548 if (!hci)
549 return -1;
551 bdaddr.b[0] = 0x52;
552 bdaddr.b[1] = 0x54;
553 bdaddr.b[2] = 0x00;
554 bdaddr.b[3] = 0x12;
555 bdaddr.b[4] = 0x34;
556 bdaddr.b[5] = 0x56 + nb_hcis;
557 hci->bdaddr_set(hci, bdaddr.b);
559 hci_table[nb_hcis++] = hci;
561 return 0;
564 static void bt_vhci_add(int vlan_id)
566 struct bt_scatternet_s *vlan = qemu_find_bt_vlan(vlan_id);
568 if (!vlan->slave)
569 fprintf(stderr, "qemu: warning: adding a VHCI to "
570 "an empty scatternet %i\n", vlan_id);
572 bt_vhci_init(bt_new_hci(vlan));
575 static struct bt_device_s *bt_device_add(const char *opt)
577 struct bt_scatternet_s *vlan;
578 int vlan_id = 0;
579 char *endp = strstr(opt, ",vlan=");
580 int len = (endp ? endp - opt : strlen(opt)) + 1;
581 char devname[10];
583 pstrcpy(devname, MIN(sizeof(devname), len), opt);
585 if (endp) {
586 vlan_id = strtol(endp + 6, &endp, 0);
587 if (*endp) {
588 fprintf(stderr, "qemu: unrecognised bluetooth vlan Id\n");
589 return 0;
593 vlan = qemu_find_bt_vlan(vlan_id);
595 if (!vlan->slave)
596 fprintf(stderr, "qemu: warning: adding a slave device to "
597 "an empty scatternet %i\n", vlan_id);
599 if (!strcmp(devname, "keyboard"))
600 return bt_keyboard_init(vlan);
602 fprintf(stderr, "qemu: unsupported bluetooth device `%s'\n", devname);
603 return 0;
606 static int bt_parse(const char *opt)
608 const char *endp, *p;
609 int vlan;
611 if (strstart(opt, "hci", &endp)) {
612 if (!*endp || *endp == ',') {
613 if (*endp)
614 if (!strstart(endp, ",vlan=", 0))
615 opt = endp + 1;
617 return bt_hci_parse(opt);
619 } else if (strstart(opt, "vhci", &endp)) {
620 if (!*endp || *endp == ',') {
621 if (*endp) {
622 if (strstart(endp, ",vlan=", &p)) {
623 vlan = strtol(p, (char **) &endp, 0);
624 if (*endp) {
625 fprintf(stderr, "qemu: bad scatternet '%s'\n", p);
626 return 1;
628 } else {
629 fprintf(stderr, "qemu: bad parameter '%s'\n", endp + 1);
630 return 1;
632 } else
633 vlan = 0;
635 bt_vhci_add(vlan);
636 return 0;
638 } else if (strstart(opt, "device:", &endp))
639 return !bt_device_add(endp);
641 fprintf(stderr, "qemu: bad bluetooth parameter '%s'\n", opt);
642 return 1;
645 /***********************************************************/
646 /* QEMU Block devices */
648 #define HD_OPTS "media=disk"
649 #define CDROM_OPTS "media=cdrom"
650 #define FD_OPTS ""
651 #define PFLASH_OPTS ""
652 #define MTD_OPTS ""
653 #define SD_OPTS ""
655 static int drive_init_func(QemuOpts *opts, void *opaque)
657 int *use_scsi = opaque;
659 return drive_init(opts, *use_scsi) == NULL;
662 static int drive_enable_snapshot(QemuOpts *opts, void *opaque)
664 if (NULL == qemu_opt_get(opts, "snapshot")) {
665 qemu_opt_set(opts, "snapshot", "on");
667 return 0;
670 static void default_drive(int enable, int snapshot, int use_scsi,
671 BlockInterfaceType type, int index,
672 const char *optstr)
674 QemuOpts *opts;
676 if (type == IF_DEFAULT) {
677 type = use_scsi ? IF_SCSI : IF_IDE;
680 if (!enable || drive_get_by_index(type, index)) {
681 return;
684 opts = drive_add(type, index, NULL, optstr);
685 if (snapshot) {
686 drive_enable_snapshot(opts, NULL);
688 if (!drive_init(opts, use_scsi)) {
689 exit(1);
693 void qemu_register_boot_set(QEMUBootSetHandler *func, void *opaque)
695 boot_set_handler = func;
696 boot_set_opaque = opaque;
699 int qemu_boot_set(const char *boot_devices)
701 if (!boot_set_handler) {
702 return -EINVAL;
704 return boot_set_handler(boot_set_opaque, boot_devices);
707 static void validate_bootdevices(char *devices)
709 /* We just do some generic consistency checks */
710 const char *p;
711 int bitmap = 0;
713 for (p = devices; *p != '\0'; p++) {
714 /* Allowed boot devices are:
715 * a-b: floppy disk drives
716 * c-f: IDE disk drives
717 * g-m: machine implementation dependant drives
718 * n-p: network devices
719 * It's up to each machine implementation to check if the given boot
720 * devices match the actual hardware implementation and firmware
721 * features.
723 if (*p < 'a' || *p > 'p') {
724 fprintf(stderr, "Invalid boot device '%c'\n", *p);
725 exit(1);
727 if (bitmap & (1 << (*p - 'a'))) {
728 fprintf(stderr, "Boot device '%c' was given twice\n", *p);
729 exit(1);
731 bitmap |= 1 << (*p - 'a');
735 static void restore_boot_devices(void *opaque)
737 char *standard_boot_devices = opaque;
738 static int first = 1;
740 /* Restore boot order and remove ourselves after the first boot */
741 if (first) {
742 first = 0;
743 return;
746 qemu_boot_set(standard_boot_devices);
748 qemu_unregister_reset(restore_boot_devices, standard_boot_devices);
749 g_free(standard_boot_devices);
752 void add_boot_device_path(int32_t bootindex, DeviceState *dev,
753 const char *suffix)
755 FWBootEntry *node, *i;
757 if (bootindex < 0) {
758 return;
761 assert(dev != NULL || suffix != NULL);
763 node = g_malloc0(sizeof(FWBootEntry));
764 node->bootindex = bootindex;
765 node->suffix = suffix ? g_strdup(suffix) : NULL;
766 node->dev = dev;
768 QTAILQ_FOREACH(i, &fw_boot_order, link) {
769 if (i->bootindex == bootindex) {
770 fprintf(stderr, "Two devices with same boot index %d\n", bootindex);
771 exit(1);
772 } else if (i->bootindex < bootindex) {
773 continue;
775 QTAILQ_INSERT_BEFORE(i, node, link);
776 return;
778 QTAILQ_INSERT_TAIL(&fw_boot_order, node, link);
782 * This function returns null terminated string that consist of new line
783 * separated device paths.
785 * memory pointed by "size" is assigned total length of the array in bytes
788 char *get_boot_devices_list(uint32_t *size)
790 FWBootEntry *i;
791 uint32_t total = 0;
792 char *list = NULL;
794 QTAILQ_FOREACH(i, &fw_boot_order, link) {
795 char *devpath = NULL, *bootpath;
796 int len;
798 if (i->dev) {
799 devpath = qdev_get_fw_dev_path(i->dev);
800 assert(devpath);
803 if (i->suffix && devpath) {
804 size_t bootpathlen = strlen(devpath) + strlen(i->suffix) + 1;
806 bootpath = g_malloc(bootpathlen);
807 snprintf(bootpath, bootpathlen, "%s%s", devpath, i->suffix);
808 g_free(devpath);
809 } else if (devpath) {
810 bootpath = devpath;
811 } else {
812 bootpath = g_strdup(i->suffix);
813 assert(bootpath);
816 if (total) {
817 list[total-1] = '\n';
819 len = strlen(bootpath) + 1;
820 list = g_realloc(list, total + len);
821 memcpy(&list[total], bootpath, len);
822 total += len;
823 g_free(bootpath);
826 *size = total;
828 return list;
831 static void numa_add(const char *optarg)
833 char option[128];
834 char *endptr;
835 unsigned long long value, endvalue;
836 int nodenr;
838 optarg = get_opt_name(option, 128, optarg, ',') + 1;
839 if (!strcmp(option, "node")) {
840 if (get_param_value(option, 128, "nodeid", optarg) == 0) {
841 nodenr = nb_numa_nodes;
842 } else {
843 nodenr = strtoull(option, NULL, 10);
846 if (get_param_value(option, 128, "mem", optarg) == 0) {
847 node_mem[nodenr] = 0;
848 } else {
849 int64_t sval;
850 sval = strtosz(option, NULL);
851 if (sval < 0) {
852 fprintf(stderr, "qemu: invalid numa mem size: %s\n", optarg);
853 exit(1);
855 node_mem[nodenr] = sval;
857 if (get_param_value(option, 128, "cpus", optarg) == 0) {
858 node_cpumask[nodenr] = 0;
859 } else {
860 value = strtoull(option, &endptr, 10);
861 if (value >= 64) {
862 value = 63;
863 fprintf(stderr, "only 64 CPUs in NUMA mode supported.\n");
864 } else {
865 if (*endptr == '-') {
866 endvalue = strtoull(endptr+1, &endptr, 10);
867 if (endvalue >= 63) {
868 endvalue = 62;
869 fprintf(stderr,
870 "only 63 CPUs in NUMA mode supported.\n");
872 value = (2ULL << endvalue) - (1ULL << value);
873 } else {
874 value = 1ULL << value;
877 node_cpumask[nodenr] = value;
879 nb_numa_nodes++;
881 return;
884 static void smp_parse(const char *optarg)
886 int smp, sockets = 0, threads = 0, cores = 0;
887 char *endptr;
888 char option[128];
890 smp = strtoul(optarg, &endptr, 10);
891 if (endptr != optarg) {
892 if (*endptr == ',') {
893 endptr++;
896 if (get_param_value(option, 128, "sockets", endptr) != 0)
897 sockets = strtoull(option, NULL, 10);
898 if (get_param_value(option, 128, "cores", endptr) != 0)
899 cores = strtoull(option, NULL, 10);
900 if (get_param_value(option, 128, "threads", endptr) != 0)
901 threads = strtoull(option, NULL, 10);
902 if (get_param_value(option, 128, "maxcpus", endptr) != 0)
903 max_cpus = strtoull(option, NULL, 10);
905 /* compute missing values, prefer sockets over cores over threads */
906 if (smp == 0 || sockets == 0) {
907 sockets = sockets > 0 ? sockets : 1;
908 cores = cores > 0 ? cores : 1;
909 threads = threads > 0 ? threads : 1;
910 if (smp == 0) {
911 smp = cores * threads * sockets;
913 } else {
914 if (cores == 0) {
915 threads = threads > 0 ? threads : 1;
916 cores = smp / (sockets * threads);
917 } else {
918 threads = smp / (cores * sockets);
921 smp_cpus = smp;
922 smp_cores = cores > 0 ? cores : 1;
923 smp_threads = threads > 0 ? threads : 1;
924 if (max_cpus == 0)
925 max_cpus = smp_cpus;
928 /***********************************************************/
929 /* USB devices */
931 static int usb_device_add(const char *devname)
933 const char *p;
934 USBDevice *dev = NULL;
936 if (!usb_enabled)
937 return -1;
939 /* drivers with .usbdevice_name entry in USBDeviceInfo */
940 dev = usbdevice_create(devname);
941 if (dev)
942 goto done;
944 /* the other ones */
945 #ifndef CONFIG_LINUX
946 /* only the linux version is qdev-ified, usb-bsd still needs this */
947 if (strstart(devname, "host:", &p)) {
948 dev = usb_host_device_open(p);
949 } else
950 #endif
951 if (!strcmp(devname, "bt") || strstart(devname, "bt:", &p)) {
952 dev = usb_bt_init(devname[2] ? hci_init(p) :
953 bt_new_hci(qemu_find_bt_vlan(0)));
954 } else {
955 return -1;
957 if (!dev)
958 return -1;
960 done:
961 return 0;
964 static int usb_device_del(const char *devname)
966 int bus_num, addr;
967 const char *p;
969 if (strstart(devname, "host:", &p))
970 return usb_host_device_close(p);
972 if (!usb_enabled)
973 return -1;
975 p = strchr(devname, '.');
976 if (!p)
977 return -1;
978 bus_num = strtoul(devname, NULL, 0);
979 addr = strtoul(p + 1, NULL, 0);
981 return usb_device_delete_addr(bus_num, addr);
984 static int usb_parse(const char *cmdline)
986 int r;
987 r = usb_device_add(cmdline);
988 if (r < 0) {
989 fprintf(stderr, "qemu: could not add USB device '%s'\n", cmdline);
991 return r;
994 void do_usb_add(Monitor *mon, const QDict *qdict)
996 const char *devname = qdict_get_str(qdict, "devname");
997 if (usb_device_add(devname) < 0) {
998 error_report("could not add USB device '%s'", devname);
1002 void do_usb_del(Monitor *mon, const QDict *qdict)
1004 const char *devname = qdict_get_str(qdict, "devname");
1005 if (usb_device_del(devname) < 0) {
1006 error_report("could not delete USB device '%s'", devname);
1010 /***********************************************************/
1011 /* PCMCIA/Cardbus */
1013 static struct pcmcia_socket_entry_s {
1014 PCMCIASocket *socket;
1015 struct pcmcia_socket_entry_s *next;
1016 } *pcmcia_sockets = 0;
1018 void pcmcia_socket_register(PCMCIASocket *socket)
1020 struct pcmcia_socket_entry_s *entry;
1022 entry = g_malloc(sizeof(struct pcmcia_socket_entry_s));
1023 entry->socket = socket;
1024 entry->next = pcmcia_sockets;
1025 pcmcia_sockets = entry;
1028 void pcmcia_socket_unregister(PCMCIASocket *socket)
1030 struct pcmcia_socket_entry_s *entry, **ptr;
1032 ptr = &pcmcia_sockets;
1033 for (entry = *ptr; entry; ptr = &entry->next, entry = *ptr)
1034 if (entry->socket == socket) {
1035 *ptr = entry->next;
1036 g_free(entry);
1040 void pcmcia_info(Monitor *mon)
1042 struct pcmcia_socket_entry_s *iter;
1044 if (!pcmcia_sockets)
1045 monitor_printf(mon, "No PCMCIA sockets\n");
1047 for (iter = pcmcia_sockets; iter; iter = iter->next)
1048 monitor_printf(mon, "%s: %s\n", iter->socket->slot_string,
1049 iter->socket->attached ? iter->socket->card_string :
1050 "Empty");
1053 /***********************************************************/
1054 /* machine registration */
1056 static QEMUMachine *first_machine = NULL;
1057 QEMUMachine *current_machine = NULL;
1059 int qemu_register_machine(QEMUMachine *m)
1061 QEMUMachine **pm;
1062 pm = &first_machine;
1063 while (*pm != NULL)
1064 pm = &(*pm)->next;
1065 m->next = NULL;
1066 *pm = m;
1067 return 0;
1070 static QEMUMachine *find_machine(const char *name)
1072 QEMUMachine *m;
1074 for(m = first_machine; m != NULL; m = m->next) {
1075 if (!strcmp(m->name, name))
1076 return m;
1077 if (m->alias && !strcmp(m->alias, name))
1078 return m;
1080 return NULL;
1083 static QEMUMachine *find_default_machine(void)
1085 QEMUMachine *m;
1087 for(m = first_machine; m != NULL; m = m->next) {
1088 if (m->is_default) {
1089 return m;
1092 return NULL;
1095 /***********************************************************/
1096 /* main execution loop */
1098 static void gui_update(void *opaque)
1100 uint64_t interval = GUI_REFRESH_INTERVAL;
1101 DisplayState *ds = opaque;
1102 DisplayChangeListener *dcl = ds->listeners;
1104 qemu_flush_coalesced_mmio_buffer();
1105 dpy_refresh(ds);
1107 while (dcl != NULL) {
1108 if (dcl->gui_timer_interval &&
1109 dcl->gui_timer_interval < interval)
1110 interval = dcl->gui_timer_interval;
1111 dcl = dcl->next;
1113 qemu_mod_timer(ds->gui_timer, interval + qemu_get_clock_ms(rt_clock));
1116 static void nographic_update(void *opaque)
1118 uint64_t interval = GUI_REFRESH_INTERVAL;
1120 qemu_flush_coalesced_mmio_buffer();
1121 qemu_mod_timer(nographic_timer, interval + qemu_get_clock_ms(rt_clock));
1124 struct vm_change_state_entry {
1125 VMChangeStateHandler *cb;
1126 void *opaque;
1127 QLIST_ENTRY (vm_change_state_entry) entries;
1130 static QLIST_HEAD(vm_change_state_head, vm_change_state_entry) vm_change_state_head;
1132 VMChangeStateEntry *qemu_add_vm_change_state_handler(VMChangeStateHandler *cb,
1133 void *opaque)
1135 VMChangeStateEntry *e;
1137 e = g_malloc0(sizeof (*e));
1139 e->cb = cb;
1140 e->opaque = opaque;
1141 QLIST_INSERT_HEAD(&vm_change_state_head, e, entries);
1142 return e;
1145 void qemu_del_vm_change_state_handler(VMChangeStateEntry *e)
1147 QLIST_REMOVE (e, entries);
1148 g_free (e);
1151 void vm_state_notify(int running, int reason)
1153 VMChangeStateEntry *e;
1155 trace_vm_state_notify(running, reason);
1157 for (e = vm_change_state_head.lh_first; e; e = e->entries.le_next) {
1158 e->cb(e->opaque, running, reason);
1162 void vm_start(void)
1164 if (!vm_running) {
1165 cpu_enable_ticks();
1166 vm_running = 1;
1167 vm_state_notify(1, 0);
1168 resume_all_vcpus();
1169 monitor_protocol_event(QEVENT_RESUME, NULL);
1173 /* reset/shutdown handler */
1175 typedef struct QEMUResetEntry {
1176 QTAILQ_ENTRY(QEMUResetEntry) entry;
1177 QEMUResetHandler *func;
1178 void *opaque;
1179 } QEMUResetEntry;
1181 static QTAILQ_HEAD(reset_handlers, QEMUResetEntry) reset_handlers =
1182 QTAILQ_HEAD_INITIALIZER(reset_handlers);
1183 static int reset_requested;
1184 static int shutdown_requested, shutdown_signal = -1;
1185 static pid_t shutdown_pid;
1186 static int powerdown_requested;
1187 static int debug_requested;
1188 static int vmstop_requested;
1190 int qemu_shutdown_requested_get(void)
1192 return shutdown_requested;
1195 int qemu_reset_requested_get(void)
1197 return reset_requested;
1200 int qemu_shutdown_requested(void)
1202 int r = shutdown_requested;
1203 shutdown_requested = 0;
1204 return r;
1207 void qemu_kill_report(void)
1209 if (shutdown_signal != -1) {
1210 fprintf(stderr, "qemu: terminating on signal %d", shutdown_signal);
1211 if (shutdown_pid == 0) {
1212 /* This happens for eg ^C at the terminal, so it's worth
1213 * avoiding printing an odd message in that case.
1215 fputc('\n', stderr);
1216 } else {
1217 fprintf(stderr, " from pid " FMT_pid "\n", shutdown_pid);
1219 shutdown_signal = -1;
1223 int qemu_reset_requested(void)
1225 int r = reset_requested;
1226 reset_requested = 0;
1227 return r;
1230 int qemu_powerdown_requested(void)
1232 int r = powerdown_requested;
1233 powerdown_requested = 0;
1234 return r;
1237 static int qemu_debug_requested(void)
1239 int r = debug_requested;
1240 debug_requested = 0;
1241 return r;
1244 static int qemu_vmstop_requested(void)
1246 int r = vmstop_requested;
1247 vmstop_requested = 0;
1248 return r;
1251 void qemu_register_reset(QEMUResetHandler *func, void *opaque)
1253 QEMUResetEntry *re = g_malloc0(sizeof(QEMUResetEntry));
1255 re->func = func;
1256 re->opaque = opaque;
1257 QTAILQ_INSERT_TAIL(&reset_handlers, re, entry);
1260 void qemu_unregister_reset(QEMUResetHandler *func, void *opaque)
1262 QEMUResetEntry *re;
1264 QTAILQ_FOREACH(re, &reset_handlers, entry) {
1265 if (re->func == func && re->opaque == opaque) {
1266 QTAILQ_REMOVE(&reset_handlers, re, entry);
1267 g_free(re);
1268 return;
1273 void qemu_system_reset(bool report)
1275 QEMUResetEntry *re, *nre;
1277 /* reset all devices */
1278 QTAILQ_FOREACH_SAFE(re, &reset_handlers, entry, nre) {
1279 re->func(re->opaque);
1281 if (report) {
1282 monitor_protocol_event(QEVENT_RESET, NULL);
1284 cpu_synchronize_all_post_reset();
1287 void qemu_system_reset_request(void)
1289 if (no_reboot) {
1290 shutdown_requested = 1;
1291 } else {
1292 reset_requested = 1;
1294 cpu_stop_current();
1295 qemu_notify_event();
1298 void qemu_system_killed(int signal, pid_t pid)
1300 shutdown_signal = signal;
1301 shutdown_pid = pid;
1302 qemu_system_shutdown_request();
1305 void qemu_system_shutdown_request(void)
1307 shutdown_requested = 1;
1308 qemu_notify_event();
1311 void qemu_system_powerdown_request(void)
1313 powerdown_requested = 1;
1314 qemu_notify_event();
1317 void qemu_system_debug_request(void)
1319 debug_requested = 1;
1320 qemu_notify_event();
1323 void qemu_system_vmstop_request(int reason)
1325 vmstop_requested = reason;
1326 qemu_notify_event();
1329 static GPollFD poll_fds[1024 * 2]; /* this is probably overkill */
1330 static int n_poll_fds;
1331 static int max_priority;
1333 static void glib_select_fill(int *max_fd, fd_set *rfds, fd_set *wfds,
1334 fd_set *xfds, struct timeval *tv)
1336 GMainContext *context = g_main_context_default();
1337 int i;
1338 int timeout = 0, cur_timeout;
1340 g_main_context_prepare(context, &max_priority);
1342 n_poll_fds = g_main_context_query(context, max_priority, &timeout,
1343 poll_fds, ARRAY_SIZE(poll_fds));
1344 g_assert(n_poll_fds <= ARRAY_SIZE(poll_fds));
1346 for (i = 0; i < n_poll_fds; i++) {
1347 GPollFD *p = &poll_fds[i];
1349 if ((p->events & G_IO_IN)) {
1350 FD_SET(p->fd, rfds);
1351 *max_fd = MAX(*max_fd, p->fd);
1353 if ((p->events & G_IO_OUT)) {
1354 FD_SET(p->fd, wfds);
1355 *max_fd = MAX(*max_fd, p->fd);
1357 if ((p->events & G_IO_ERR)) {
1358 FD_SET(p->fd, xfds);
1359 *max_fd = MAX(*max_fd, p->fd);
1363 cur_timeout = (tv->tv_sec * 1000) + ((tv->tv_usec + 500) / 1000);
1364 if (timeout >= 0 && timeout < cur_timeout) {
1365 tv->tv_sec = timeout / 1000;
1366 tv->tv_usec = (timeout % 1000) * 1000;
1370 static void glib_select_poll(fd_set *rfds, fd_set *wfds, fd_set *xfds,
1371 bool err)
1373 GMainContext *context = g_main_context_default();
1375 if (!err) {
1376 int i;
1378 for (i = 0; i < n_poll_fds; i++) {
1379 GPollFD *p = &poll_fds[i];
1381 if ((p->events & G_IO_IN) && FD_ISSET(p->fd, rfds)) {
1382 p->revents |= G_IO_IN;
1384 if ((p->events & G_IO_OUT) && FD_ISSET(p->fd, wfds)) {
1385 p->revents |= G_IO_OUT;
1387 if ((p->events & G_IO_ERR) && FD_ISSET(p->fd, xfds)) {
1388 p->revents |= G_IO_ERR;
1393 if (g_main_context_check(context, max_priority, poll_fds, n_poll_fds)) {
1394 g_main_context_dispatch(context);
1398 int main_loop_wait(int nonblocking)
1400 fd_set rfds, wfds, xfds;
1401 int ret, nfds;
1402 struct timeval tv;
1403 int timeout;
1405 if (nonblocking)
1406 timeout = 0;
1407 else {
1408 timeout = qemu_calculate_timeout();
1409 qemu_bh_update_timeout(&timeout);
1412 os_host_main_loop_wait(&timeout);
1414 tv.tv_sec = timeout / 1000;
1415 tv.tv_usec = (timeout % 1000) * 1000;
1417 /* poll any events */
1418 /* XXX: separate device handlers from system ones */
1419 nfds = -1;
1420 FD_ZERO(&rfds);
1421 FD_ZERO(&wfds);
1422 FD_ZERO(&xfds);
1424 qemu_iohandler_fill(&nfds, &rfds, &wfds, &xfds);
1425 slirp_select_fill(&nfds, &rfds, &wfds, &xfds);
1426 glib_select_fill(&nfds, &rfds, &wfds, &xfds, &tv);
1428 if (timeout > 0) {
1429 qemu_mutex_unlock_iothread();
1432 ret = select(nfds + 1, &rfds, &wfds, &xfds, &tv);
1434 if (timeout > 0) {
1435 qemu_mutex_lock_iothread();
1438 qemu_iohandler_poll(&rfds, &wfds, &xfds, ret);
1439 slirp_select_poll(&rfds, &wfds, &xfds, (ret < 0));
1440 glib_select_poll(&rfds, &wfds, &xfds, (ret < 0));
1442 qemu_run_all_timers();
1444 /* Check bottom-halves last in case any of the earlier events triggered
1445 them. */
1446 qemu_bh_poll();
1448 return ret;
1451 qemu_irq qemu_system_powerdown;
1453 static void main_loop(void)
1455 bool nonblocking;
1456 int last_io __attribute__ ((unused)) = 0;
1457 #ifdef CONFIG_PROFILER
1458 int64_t ti;
1459 #endif
1460 int r;
1462 qemu_main_loop_start();
1464 for (;;) {
1465 nonblocking = !kvm_enabled() && last_io > 0;
1466 #ifdef CONFIG_PROFILER
1467 ti = profile_getclock();
1468 #endif
1469 last_io = main_loop_wait(nonblocking);
1470 #ifdef CONFIG_PROFILER
1471 dev_time += profile_getclock() - ti;
1472 #endif
1474 if (qemu_debug_requested()) {
1475 vm_stop(VMSTOP_DEBUG);
1477 if (qemu_shutdown_requested()) {
1478 qemu_kill_report();
1479 monitor_protocol_event(QEVENT_SHUTDOWN, NULL);
1480 if (no_shutdown) {
1481 vm_stop(VMSTOP_SHUTDOWN);
1482 } else
1483 break;
1485 if (qemu_reset_requested()) {
1486 pause_all_vcpus();
1487 cpu_synchronize_all_states();
1488 qemu_system_reset(VMRESET_REPORT);
1489 resume_all_vcpus();
1491 if (qemu_powerdown_requested()) {
1492 monitor_protocol_event(QEVENT_POWERDOWN, NULL);
1493 qemu_irq_raise(qemu_system_powerdown);
1495 if ((r = qemu_vmstop_requested())) {
1496 vm_stop(r);
1499 bdrv_close_all();
1500 pause_all_vcpus();
1503 static void version(void)
1505 printf("QEMU emulator version " QEMU_VERSION QEMU_PKGVERSION ", Copyright (c) 2003-2008 Fabrice Bellard\n");
1508 static void help(int exitcode)
1510 const char *options_help =
1511 #define DEF(option, opt_arg, opt_enum, opt_help, arch_mask) \
1512 opt_help
1513 #define DEFHEADING(text) stringify(text) "\n"
1514 #include "qemu-options.def"
1515 #undef DEF
1516 #undef DEFHEADING
1517 #undef GEN_DOCS
1519 version();
1520 printf("usage: %s [options] [disk_image]\n"
1521 "\n"
1522 "'disk_image' is a raw hard disk image for IDE hard disk 0\n"
1523 "\n"
1524 "%s\n"
1525 "During emulation, the following keys are useful:\n"
1526 "ctrl-alt-f toggle full screen\n"
1527 "ctrl-alt-n switch to virtual console 'n'\n"
1528 "ctrl-alt toggle mouse and keyboard grab\n"
1529 "\n"
1530 "When using -nographic, press 'ctrl-a h' to get some help.\n",
1531 "qemu",
1532 options_help);
1533 exit(exitcode);
1536 #define HAS_ARG 0x0001
1538 typedef struct QEMUOption {
1539 const char *name;
1540 int flags;
1541 int index;
1542 uint32_t arch_mask;
1543 } QEMUOption;
1545 static const QEMUOption qemu_options[] = {
1546 { "h", 0, QEMU_OPTION_h, QEMU_ARCH_ALL },
1547 #define DEF(option, opt_arg, opt_enum, opt_help, arch_mask) \
1548 { option, opt_arg, opt_enum, arch_mask },
1549 #define DEFHEADING(text)
1550 #include "qemu-options.def"
1551 #undef DEF
1552 #undef DEFHEADING
1553 #undef GEN_DOCS
1554 { NULL },
1556 static void select_vgahw (const char *p)
1558 const char *opts;
1560 default_vga = 0;
1561 vga_interface_type = VGA_NONE;
1562 if (strstart(p, "std", &opts)) {
1563 vga_interface_type = VGA_STD;
1564 } else if (strstart(p, "cirrus", &opts)) {
1565 vga_interface_type = VGA_CIRRUS;
1566 } else if (strstart(p, "vmware", &opts)) {
1567 vga_interface_type = VGA_VMWARE;
1568 } else if (strstart(p, "xenfb", &opts)) {
1569 vga_interface_type = VGA_XENFB;
1570 } else if (strstart(p, "qxl", &opts)) {
1571 vga_interface_type = VGA_QXL;
1572 } else if (!strstart(p, "none", &opts)) {
1573 invalid_vga:
1574 fprintf(stderr, "Unknown vga type: %s\n", p);
1575 exit(1);
1577 while (*opts) {
1578 const char *nextopt;
1580 if (strstart(opts, ",retrace=", &nextopt)) {
1581 opts = nextopt;
1582 if (strstart(opts, "dumb", &nextopt))
1583 vga_retrace_method = VGA_RETRACE_DUMB;
1584 else if (strstart(opts, "precise", &nextopt))
1585 vga_retrace_method = VGA_RETRACE_PRECISE;
1586 else goto invalid_vga;
1587 } else goto invalid_vga;
1588 opts = nextopt;
1592 static DisplayType select_display(const char *p)
1594 const char *opts;
1595 DisplayType display = DT_DEFAULT;
1597 if (strstart(p, "sdl", &opts)) {
1598 #ifdef CONFIG_SDL
1599 display = DT_SDL;
1600 while (*opts) {
1601 const char *nextopt;
1603 if (strstart(opts, ",frame=", &nextopt)) {
1604 opts = nextopt;
1605 if (strstart(opts, "on", &nextopt)) {
1606 no_frame = 0;
1607 } else if (strstart(opts, "off", &nextopt)) {
1608 no_frame = 1;
1609 } else {
1610 goto invalid_sdl_args;
1612 } else if (strstart(opts, ",alt_grab=", &nextopt)) {
1613 opts = nextopt;
1614 if (strstart(opts, "on", &nextopt)) {
1615 alt_grab = 1;
1616 } else if (strstart(opts, "off", &nextopt)) {
1617 alt_grab = 0;
1618 } else {
1619 goto invalid_sdl_args;
1621 } else if (strstart(opts, ",ctrl_grab=", &nextopt)) {
1622 opts = nextopt;
1623 if (strstart(opts, "on", &nextopt)) {
1624 ctrl_grab = 1;
1625 } else if (strstart(opts, "off", &nextopt)) {
1626 ctrl_grab = 0;
1627 } else {
1628 goto invalid_sdl_args;
1630 } else if (strstart(opts, ",window_close=", &nextopt)) {
1631 opts = nextopt;
1632 if (strstart(opts, "on", &nextopt)) {
1633 no_quit = 0;
1634 } else if (strstart(opts, "off", &nextopt)) {
1635 no_quit = 1;
1636 } else {
1637 goto invalid_sdl_args;
1639 } else {
1640 invalid_sdl_args:
1641 fprintf(stderr, "Invalid SDL option string: %s\n", p);
1642 exit(1);
1644 opts = nextopt;
1646 #else
1647 fprintf(stderr, "SDL support is disabled\n");
1648 exit(1);
1649 #endif
1650 } else if (strstart(p, "vnc", &opts)) {
1651 #ifdef CONFIG_VNC
1652 display_remote++;
1654 if (*opts) {
1655 const char *nextopt;
1657 if (strstart(opts, "=", &nextopt)) {
1658 vnc_display = nextopt;
1661 if (!vnc_display) {
1662 fprintf(stderr, "VNC requires a display argument vnc=<display>\n");
1663 exit(1);
1665 #else
1666 fprintf(stderr, "VNC support is disabled\n");
1667 exit(1);
1668 #endif
1669 } else if (strstart(p, "curses", &opts)) {
1670 #ifdef CONFIG_CURSES
1671 display = DT_CURSES;
1672 #else
1673 fprintf(stderr, "Curses support is disabled\n");
1674 exit(1);
1675 #endif
1676 } else if (strstart(p, "none", &opts)) {
1677 display = DT_NONE;
1678 } else {
1679 fprintf(stderr, "Unknown display type: %s\n", p);
1680 exit(1);
1683 return display;
1686 static int balloon_parse(const char *arg)
1688 QemuOpts *opts;
1690 if (strcmp(arg, "none") == 0) {
1691 return 0;
1694 if (!strncmp(arg, "virtio", 6)) {
1695 if (arg[6] == ',') {
1696 /* have params -> parse them */
1697 opts = qemu_opts_parse(qemu_find_opts("device"), arg+7, 0);
1698 if (!opts)
1699 return -1;
1700 } else {
1701 /* create empty opts */
1702 opts = qemu_opts_create(qemu_find_opts("device"), NULL, 0);
1704 qemu_opt_set(opts, "driver", "virtio-balloon");
1705 return 0;
1708 return -1;
1711 char *qemu_find_file(int type, const char *name)
1713 int len;
1714 const char *subdir;
1715 char *buf;
1717 /* If name contains path separators then try it as a straight path. */
1718 if ((strchr(name, '/') || strchr(name, '\\'))
1719 && access(name, R_OK) == 0) {
1720 return g_strdup(name);
1722 switch (type) {
1723 case QEMU_FILE_TYPE_BIOS:
1724 subdir = "";
1725 break;
1726 case QEMU_FILE_TYPE_KEYMAP:
1727 subdir = "keymaps/";
1728 break;
1729 default:
1730 abort();
1732 len = strlen(data_dir) + strlen(name) + strlen(subdir) + 2;
1733 buf = g_malloc0(len);
1734 snprintf(buf, len, "%s/%s%s", data_dir, subdir, name);
1735 if (access(buf, R_OK)) {
1736 g_free(buf);
1737 return NULL;
1739 return buf;
1742 static int device_help_func(QemuOpts *opts, void *opaque)
1744 return qdev_device_help(opts);
1747 static int device_init_func(QemuOpts *opts, void *opaque)
1749 DeviceState *dev;
1751 dev = qdev_device_add(opts);
1752 if (!dev)
1753 return -1;
1754 return 0;
1757 static int chardev_init_func(QemuOpts *opts, void *opaque)
1759 CharDriverState *chr;
1761 chr = qemu_chr_new_from_opts(opts, NULL);
1762 if (!chr)
1763 return -1;
1764 return 0;
1767 #ifdef CONFIG_VIRTFS
1768 static int fsdev_init_func(QemuOpts *opts, void *opaque)
1770 int ret;
1771 ret = qemu_fsdev_add(opts);
1773 return ret;
1775 #endif
1777 static int mon_init_func(QemuOpts *opts, void *opaque)
1779 CharDriverState *chr;
1780 const char *chardev;
1781 const char *mode;
1782 int flags;
1784 mode = qemu_opt_get(opts, "mode");
1785 if (mode == NULL) {
1786 mode = "readline";
1788 if (strcmp(mode, "readline") == 0) {
1789 flags = MONITOR_USE_READLINE;
1790 } else if (strcmp(mode, "control") == 0) {
1791 flags = MONITOR_USE_CONTROL;
1792 } else {
1793 fprintf(stderr, "unknown monitor mode \"%s\"\n", mode);
1794 exit(1);
1797 if (qemu_opt_get_bool(opts, "pretty", 0))
1798 flags |= MONITOR_USE_PRETTY;
1800 if (qemu_opt_get_bool(opts, "default", 0))
1801 flags |= MONITOR_IS_DEFAULT;
1803 chardev = qemu_opt_get(opts, "chardev");
1804 chr = qemu_chr_find(chardev);
1805 if (chr == NULL) {
1806 fprintf(stderr, "chardev \"%s\" not found\n", chardev);
1807 exit(1);
1810 monitor_init(chr, flags);
1811 return 0;
1814 static void monitor_parse(const char *optarg, const char *mode)
1816 static int monitor_device_index = 0;
1817 QemuOpts *opts;
1818 const char *p;
1819 char label[32];
1820 int def = 0;
1822 if (strstart(optarg, "chardev:", &p)) {
1823 snprintf(label, sizeof(label), "%s", p);
1824 } else {
1825 snprintf(label, sizeof(label), "compat_monitor%d",
1826 monitor_device_index);
1827 if (monitor_device_index == 0) {
1828 def = 1;
1830 opts = qemu_chr_parse_compat(label, optarg);
1831 if (!opts) {
1832 fprintf(stderr, "parse error: %s\n", optarg);
1833 exit(1);
1837 opts = qemu_opts_create(qemu_find_opts("mon"), label, 1);
1838 if (!opts) {
1839 fprintf(stderr, "duplicate chardev: %s\n", label);
1840 exit(1);
1842 qemu_opt_set(opts, "mode", mode);
1843 qemu_opt_set(opts, "chardev", label);
1844 if (def)
1845 qemu_opt_set(opts, "default", "on");
1846 monitor_device_index++;
1849 struct device_config {
1850 enum {
1851 DEV_USB, /* -usbdevice */
1852 DEV_BT, /* -bt */
1853 DEV_SERIAL, /* -serial */
1854 DEV_PARALLEL, /* -parallel */
1855 DEV_VIRTCON, /* -virtioconsole */
1856 DEV_DEBUGCON, /* -debugcon */
1857 } type;
1858 const char *cmdline;
1859 QTAILQ_ENTRY(device_config) next;
1861 QTAILQ_HEAD(, device_config) device_configs = QTAILQ_HEAD_INITIALIZER(device_configs);
1863 static void add_device_config(int type, const char *cmdline)
1865 struct device_config *conf;
1867 conf = g_malloc0(sizeof(*conf));
1868 conf->type = type;
1869 conf->cmdline = cmdline;
1870 QTAILQ_INSERT_TAIL(&device_configs, conf, next);
1873 static int foreach_device_config(int type, int (*func)(const char *cmdline))
1875 struct device_config *conf;
1876 int rc;
1878 QTAILQ_FOREACH(conf, &device_configs, next) {
1879 if (conf->type != type)
1880 continue;
1881 rc = func(conf->cmdline);
1882 if (0 != rc)
1883 return rc;
1885 return 0;
1888 static int serial_parse(const char *devname)
1890 static int index = 0;
1891 char label[32];
1893 if (strcmp(devname, "none") == 0)
1894 return 0;
1895 if (index == MAX_SERIAL_PORTS) {
1896 fprintf(stderr, "qemu: too many serial ports\n");
1897 exit(1);
1899 snprintf(label, sizeof(label), "serial%d", index);
1900 serial_hds[index] = qemu_chr_new(label, devname, NULL);
1901 if (!serial_hds[index]) {
1902 fprintf(stderr, "qemu: could not open serial device '%s': %s\n",
1903 devname, strerror(errno));
1904 return -1;
1906 index++;
1907 return 0;
1910 static int parallel_parse(const char *devname)
1912 static int index = 0;
1913 char label[32];
1915 if (strcmp(devname, "none") == 0)
1916 return 0;
1917 if (index == MAX_PARALLEL_PORTS) {
1918 fprintf(stderr, "qemu: too many parallel ports\n");
1919 exit(1);
1921 snprintf(label, sizeof(label), "parallel%d", index);
1922 parallel_hds[index] = qemu_chr_new(label, devname, NULL);
1923 if (!parallel_hds[index]) {
1924 fprintf(stderr, "qemu: could not open parallel device '%s': %s\n",
1925 devname, strerror(errno));
1926 return -1;
1928 index++;
1929 return 0;
1932 static int virtcon_parse(const char *devname)
1934 QemuOptsList *device = qemu_find_opts("device");
1935 static int index = 0;
1936 char label[32];
1937 QemuOpts *bus_opts, *dev_opts;
1939 if (strcmp(devname, "none") == 0)
1940 return 0;
1941 if (index == MAX_VIRTIO_CONSOLES) {
1942 fprintf(stderr, "qemu: too many virtio consoles\n");
1943 exit(1);
1946 bus_opts = qemu_opts_create(device, NULL, 0);
1947 qemu_opt_set(bus_opts, "driver", "virtio-serial");
1949 dev_opts = qemu_opts_create(device, NULL, 0);
1950 qemu_opt_set(dev_opts, "driver", "virtconsole");
1952 snprintf(label, sizeof(label), "virtcon%d", index);
1953 virtcon_hds[index] = qemu_chr_new(label, devname, NULL);
1954 if (!virtcon_hds[index]) {
1955 fprintf(stderr, "qemu: could not open virtio console '%s': %s\n",
1956 devname, strerror(errno));
1957 return -1;
1959 qemu_opt_set(dev_opts, "chardev", label);
1961 index++;
1962 return 0;
1965 static int debugcon_parse(const char *devname)
1967 QemuOpts *opts;
1969 if (!qemu_chr_new("debugcon", devname, NULL)) {
1970 exit(1);
1972 opts = qemu_opts_create(qemu_find_opts("device"), "debugcon", 1);
1973 if (!opts) {
1974 fprintf(stderr, "qemu: already have a debugcon device\n");
1975 exit(1);
1977 qemu_opt_set(opts, "driver", "isa-debugcon");
1978 qemu_opt_set(opts, "chardev", "debugcon");
1979 return 0;
1982 static QEMUMachine *machine_parse(const char *name)
1984 QEMUMachine *m, *machine = NULL;
1986 if (name) {
1987 machine = find_machine(name);
1989 if (machine) {
1990 return machine;
1992 printf("Supported machines are:\n");
1993 for (m = first_machine; m != NULL; m = m->next) {
1994 if (m->alias) {
1995 printf("%-10s %s (alias of %s)\n", m->alias, m->desc, m->name);
1997 printf("%-10s %s%s\n", m->name, m->desc,
1998 m->is_default ? " (default)" : "");
2000 exit(!name || *name != '?');
2003 static int tcg_init(void)
2005 tcg_exec_init(tcg_tb_size * 1024 * 1024);
2006 return 0;
2009 static struct {
2010 const char *opt_name;
2011 const char *name;
2012 int (*available)(void);
2013 int (*init)(void);
2014 int *allowed;
2015 } accel_list[] = {
2016 { "tcg", "tcg", tcg_available, tcg_init, &tcg_allowed },
2017 { "xen", "Xen", xen_available, xen_init, &xen_allowed },
2018 { "kvm", "KVM", kvm_available, kvm_init, &kvm_allowed },
2021 static int configure_accelerator(void)
2023 const char *p = NULL;
2024 char buf[10];
2025 int i, ret;
2026 bool accel_initalised = 0;
2027 bool init_failed = 0;
2029 QemuOptsList *list = qemu_find_opts("machine");
2030 if (!QTAILQ_EMPTY(&list->head)) {
2031 p = qemu_opt_get(QTAILQ_FIRST(&list->head), "accel");
2034 if (p == NULL) {
2035 #ifdef CONFIG_KVM
2036 /* Use the default "accelerator", kvm */
2037 p = "kvm";
2038 #else
2039 /* Use the default "accelerator", tcg */
2040 p = "tcg";
2041 #endif
2044 while (!accel_initalised && *p != '\0') {
2045 if (*p == ':') {
2046 p++;
2048 p = get_opt_name(buf, sizeof (buf), p, ':');
2049 for (i = 0; i < ARRAY_SIZE(accel_list); i++) {
2050 if (strcmp(accel_list[i].opt_name, buf) == 0) {
2051 *(accel_list[i].allowed) = 1;
2052 ret = accel_list[i].init();
2053 if (ret < 0) {
2054 init_failed = 1;
2055 if (!accel_list[i].available()) {
2056 printf("%s not supported for this target\n",
2057 accel_list[i].name);
2058 } else {
2059 fprintf(stderr, "failed to initialize %s: %s\n",
2060 accel_list[i].name,
2061 strerror(-ret));
2063 *(accel_list[i].allowed) = 0;
2064 } else {
2065 accel_initalised = 1;
2067 break;
2070 if (i == ARRAY_SIZE(accel_list)) {
2071 fprintf(stderr, "\"%s\" accelerator does not exist.\n", buf);
2075 if (!accel_initalised) {
2076 fprintf(stderr, "No accelerator found!\n");
2077 exit(1);
2080 if (init_failed) {
2081 fprintf(stderr, "Back to %s accelerator.\n", accel_list[i].name);
2084 return !accel_initalised;
2087 void qemu_add_exit_notifier(Notifier *notify)
2089 notifier_list_add(&exit_notifiers, notify);
2092 void qemu_remove_exit_notifier(Notifier *notify)
2094 notifier_list_remove(&exit_notifiers, notify);
2097 static void qemu_run_exit_notifiers(void)
2099 notifier_list_notify(&exit_notifiers, NULL);
2102 void qemu_add_machine_init_done_notifier(Notifier *notify)
2104 notifier_list_add(&machine_init_done_notifiers, notify);
2107 static void qemu_run_machine_init_done_notifiers(void)
2109 notifier_list_notify(&machine_init_done_notifiers, NULL);
2112 static const QEMUOption *lookup_opt(int argc, char **argv,
2113 const char **poptarg, int *poptind)
2115 const QEMUOption *popt;
2116 int optind = *poptind;
2117 char *r = argv[optind];
2118 const char *optarg;
2120 loc_set_cmdline(argv, optind, 1);
2121 optind++;
2122 /* Treat --foo the same as -foo. */
2123 if (r[1] == '-')
2124 r++;
2125 popt = qemu_options;
2126 for(;;) {
2127 if (!popt->name) {
2128 error_report("invalid option");
2129 exit(1);
2131 if (!strcmp(popt->name, r + 1))
2132 break;
2133 popt++;
2135 if (popt->flags & HAS_ARG) {
2136 if (optind >= argc) {
2137 error_report("requires an argument");
2138 exit(1);
2140 optarg = argv[optind++];
2141 loc_set_cmdline(argv, optind - 2, 2);
2142 } else {
2143 optarg = NULL;
2146 *poptarg = optarg;
2147 *poptind = optind;
2149 return popt;
2152 static gpointer malloc_and_trace(gsize n_bytes)
2154 void *ptr = malloc(n_bytes);
2155 trace_g_malloc(n_bytes, ptr);
2156 return ptr;
2159 static gpointer realloc_and_trace(gpointer mem, gsize n_bytes)
2161 void *ptr = realloc(mem, n_bytes);
2162 trace_g_realloc(mem, n_bytes, ptr);
2163 return ptr;
2166 static void free_and_trace(gpointer mem)
2168 trace_g_free(mem);
2169 free(mem);
2172 int main(int argc, char **argv, char **envp)
2174 const char *gdbstub_dev = NULL;
2175 int i;
2176 int snapshot, linux_boot;
2177 const char *icount_option = NULL;
2178 const char *initrd_filename;
2179 const char *kernel_filename, *kernel_cmdline;
2180 char boot_devices[33] = "cad"; /* default to HD->floppy->CD-ROM */
2181 DisplayState *ds;
2182 DisplayChangeListener *dcl;
2183 int cyls, heads, secs, translation;
2184 QemuOpts *hda_opts = NULL, *opts;
2185 QemuOptsList *olist;
2186 int optind;
2187 const char *optarg;
2188 const char *loadvm = NULL;
2189 QEMUMachine *machine;
2190 const char *cpu_model;
2191 const char *pid_file = NULL;
2192 const char *incoming = NULL;
2193 #ifdef CONFIG_VNC
2194 int show_vnc_port = 0;
2195 #endif
2196 int defconfig = 1;
2197 const char *log_mask = NULL;
2198 const char *log_file = NULL;
2199 GMemVTable mem_trace = {
2200 .malloc = malloc_and_trace,
2201 .realloc = realloc_and_trace,
2202 .free = free_and_trace,
2204 const char *trace_events = NULL;
2205 const char *trace_file = NULL;
2207 atexit(qemu_run_exit_notifiers);
2208 error_set_progname(argv[0]);
2210 g_mem_set_vtable(&mem_trace);
2212 init_clocks();
2214 qemu_cache_utils_init(envp);
2216 QLIST_INIT (&vm_change_state_head);
2217 os_setup_early_signal_handling();
2219 module_call_init(MODULE_INIT_MACHINE);
2220 machine = find_default_machine();
2221 cpu_model = NULL;
2222 initrd_filename = NULL;
2223 ram_size = 0;
2224 snapshot = 0;
2225 kernel_filename = NULL;
2226 kernel_cmdline = "";
2227 cyls = heads = secs = 0;
2228 translation = BIOS_ATA_TRANSLATION_AUTO;
2230 for (i = 0; i < MAX_NODES; i++) {
2231 node_mem[i] = 0;
2232 node_cpumask[i] = 0;
2235 nb_numa_nodes = 0;
2236 nb_nics = 0;
2238 autostart= 1;
2240 /* first pass of option parsing */
2241 optind = 1;
2242 while (optind < argc) {
2243 if (argv[optind][0] != '-') {
2244 /* disk image */
2245 optind++;
2246 continue;
2247 } else {
2248 const QEMUOption *popt;
2250 popt = lookup_opt(argc, argv, &optarg, &optind);
2251 switch (popt->index) {
2252 case QEMU_OPTION_nodefconfig:
2253 defconfig=0;
2254 break;
2259 if (defconfig) {
2260 int ret;
2262 ret = qemu_read_config_file(CONFIG_QEMU_CONFDIR "/qemu.conf");
2263 if (ret < 0 && ret != -ENOENT) {
2264 exit(1);
2267 ret = qemu_read_config_file(arch_config_name);
2268 if (ret < 0 && ret != -ENOENT) {
2269 exit(1);
2272 cpudef_init();
2274 /* second pass of option parsing */
2275 optind = 1;
2276 for(;;) {
2277 if (optind >= argc)
2278 break;
2279 if (argv[optind][0] != '-') {
2280 hda_opts = drive_add(IF_DEFAULT, 0, argv[optind++], HD_OPTS);
2281 } else {
2282 const QEMUOption *popt;
2284 popt = lookup_opt(argc, argv, &optarg, &optind);
2285 if (!(popt->arch_mask & arch_type)) {
2286 printf("Option %s not supported for this target\n", popt->name);
2287 exit(1);
2289 switch(popt->index) {
2290 case QEMU_OPTION_M:
2291 machine = machine_parse(optarg);
2292 break;
2293 case QEMU_OPTION_cpu:
2294 /* hw initialization will check this */
2295 if (*optarg == '?') {
2296 list_cpus(stdout, &fprintf, optarg);
2297 exit(0);
2298 } else {
2299 cpu_model = optarg;
2301 break;
2302 case QEMU_OPTION_initrd:
2303 initrd_filename = optarg;
2304 break;
2305 case QEMU_OPTION_hda:
2307 char buf[256];
2308 if (cyls == 0)
2309 snprintf(buf, sizeof(buf), "%s", HD_OPTS);
2310 else
2311 snprintf(buf, sizeof(buf),
2312 "%s,cyls=%d,heads=%d,secs=%d%s",
2313 HD_OPTS , cyls, heads, secs,
2314 translation == BIOS_ATA_TRANSLATION_LBA ?
2315 ",trans=lba" :
2316 translation == BIOS_ATA_TRANSLATION_NONE ?
2317 ",trans=none" : "");
2318 drive_add(IF_DEFAULT, 0, optarg, buf);
2319 break;
2321 case QEMU_OPTION_hdb:
2322 case QEMU_OPTION_hdc:
2323 case QEMU_OPTION_hdd:
2324 drive_add(IF_DEFAULT, popt->index - QEMU_OPTION_hda, optarg,
2325 HD_OPTS);
2326 break;
2327 case QEMU_OPTION_drive:
2328 if (drive_def(optarg) == NULL) {
2329 exit(1);
2331 break;
2332 case QEMU_OPTION_set:
2333 if (qemu_set_option(optarg) != 0)
2334 exit(1);
2335 break;
2336 case QEMU_OPTION_global:
2337 if (qemu_global_option(optarg) != 0)
2338 exit(1);
2339 break;
2340 case QEMU_OPTION_mtdblock:
2341 drive_add(IF_MTD, -1, optarg, MTD_OPTS);
2342 break;
2343 case QEMU_OPTION_sd:
2344 drive_add(IF_SD, 0, optarg, SD_OPTS);
2345 break;
2346 case QEMU_OPTION_pflash:
2347 drive_add(IF_PFLASH, -1, optarg, PFLASH_OPTS);
2348 break;
2349 case QEMU_OPTION_snapshot:
2350 snapshot = 1;
2351 break;
2352 case QEMU_OPTION_hdachs:
2354 const char *p;
2355 p = optarg;
2356 cyls = strtol(p, (char **)&p, 0);
2357 if (cyls < 1 || cyls > 16383)
2358 goto chs_fail;
2359 if (*p != ',')
2360 goto chs_fail;
2361 p++;
2362 heads = strtol(p, (char **)&p, 0);
2363 if (heads < 1 || heads > 16)
2364 goto chs_fail;
2365 if (*p != ',')
2366 goto chs_fail;
2367 p++;
2368 secs = strtol(p, (char **)&p, 0);
2369 if (secs < 1 || secs > 63)
2370 goto chs_fail;
2371 if (*p == ',') {
2372 p++;
2373 if (!strcmp(p, "none"))
2374 translation = BIOS_ATA_TRANSLATION_NONE;
2375 else if (!strcmp(p, "lba"))
2376 translation = BIOS_ATA_TRANSLATION_LBA;
2377 else if (!strcmp(p, "auto"))
2378 translation = BIOS_ATA_TRANSLATION_AUTO;
2379 else
2380 goto chs_fail;
2381 } else if (*p != '\0') {
2382 chs_fail:
2383 fprintf(stderr, "qemu: invalid physical CHS format\n");
2384 exit(1);
2386 if (hda_opts != NULL) {
2387 char num[16];
2388 snprintf(num, sizeof(num), "%d", cyls);
2389 qemu_opt_set(hda_opts, "cyls", num);
2390 snprintf(num, sizeof(num), "%d", heads);
2391 qemu_opt_set(hda_opts, "heads", num);
2392 snprintf(num, sizeof(num), "%d", secs);
2393 qemu_opt_set(hda_opts, "secs", num);
2394 if (translation == BIOS_ATA_TRANSLATION_LBA)
2395 qemu_opt_set(hda_opts, "trans", "lba");
2396 if (translation == BIOS_ATA_TRANSLATION_NONE)
2397 qemu_opt_set(hda_opts, "trans", "none");
2400 break;
2401 case QEMU_OPTION_numa:
2402 if (nb_numa_nodes >= MAX_NODES) {
2403 fprintf(stderr, "qemu: too many NUMA nodes\n");
2404 exit(1);
2406 numa_add(optarg);
2407 break;
2408 case QEMU_OPTION_display:
2409 display_type = select_display(optarg);
2410 break;
2411 case QEMU_OPTION_nographic:
2412 display_type = DT_NOGRAPHIC;
2413 break;
2414 case QEMU_OPTION_curses:
2415 #ifdef CONFIG_CURSES
2416 display_type = DT_CURSES;
2417 #else
2418 fprintf(stderr, "Curses support is disabled\n");
2419 exit(1);
2420 #endif
2421 break;
2422 case QEMU_OPTION_portrait:
2423 graphic_rotate = 90;
2424 break;
2425 case QEMU_OPTION_rotate:
2426 graphic_rotate = strtol(optarg, (char **) &optarg, 10);
2427 if (graphic_rotate != 0 && graphic_rotate != 90 &&
2428 graphic_rotate != 180 && graphic_rotate != 270) {
2429 fprintf(stderr,
2430 "qemu: only 90, 180, 270 deg rotation is available\n");
2431 exit(1);
2433 break;
2434 case QEMU_OPTION_kernel:
2435 kernel_filename = optarg;
2436 break;
2437 case QEMU_OPTION_append:
2438 kernel_cmdline = optarg;
2439 break;
2440 case QEMU_OPTION_cdrom:
2441 drive_add(IF_DEFAULT, 2, optarg, CDROM_OPTS);
2442 break;
2443 case QEMU_OPTION_boot:
2445 static const char * const params[] = {
2446 "order", "once", "menu",
2447 "splash", "splash-time", NULL
2449 char buf[sizeof(boot_devices)];
2450 char *standard_boot_devices;
2451 int legacy = 0;
2453 if (!strchr(optarg, '=')) {
2454 legacy = 1;
2455 pstrcpy(buf, sizeof(buf), optarg);
2456 } else if (check_params(buf, sizeof(buf), params, optarg) < 0) {
2457 fprintf(stderr,
2458 "qemu: unknown boot parameter '%s' in '%s'\n",
2459 buf, optarg);
2460 exit(1);
2463 if (legacy ||
2464 get_param_value(buf, sizeof(buf), "order", optarg)) {
2465 validate_bootdevices(buf);
2466 pstrcpy(boot_devices, sizeof(boot_devices), buf);
2468 if (!legacy) {
2469 if (get_param_value(buf, sizeof(buf),
2470 "once", optarg)) {
2471 validate_bootdevices(buf);
2472 standard_boot_devices = g_strdup(boot_devices);
2473 pstrcpy(boot_devices, sizeof(boot_devices), buf);
2474 qemu_register_reset(restore_boot_devices,
2475 standard_boot_devices);
2477 if (get_param_value(buf, sizeof(buf),
2478 "menu", optarg)) {
2479 if (!strcmp(buf, "on")) {
2480 boot_menu = 1;
2481 } else if (!strcmp(buf, "off")) {
2482 boot_menu = 0;
2483 } else {
2484 fprintf(stderr,
2485 "qemu: invalid option value '%s'\n",
2486 buf);
2487 exit(1);
2490 qemu_opts_parse(qemu_find_opts("boot-opts"),
2491 optarg, 0);
2494 break;
2495 case QEMU_OPTION_fda:
2496 case QEMU_OPTION_fdb:
2497 drive_add(IF_FLOPPY, popt->index - QEMU_OPTION_fda,
2498 optarg, FD_OPTS);
2499 break;
2500 case QEMU_OPTION_no_fd_bootchk:
2501 fd_bootchk = 0;
2502 break;
2503 case QEMU_OPTION_netdev:
2504 if (net_client_parse(qemu_find_opts("netdev"), optarg) == -1) {
2505 exit(1);
2507 break;
2508 case QEMU_OPTION_net:
2509 if (net_client_parse(qemu_find_opts("net"), optarg) == -1) {
2510 exit(1);
2512 break;
2513 #ifdef CONFIG_SLIRP
2514 case QEMU_OPTION_tftp:
2515 legacy_tftp_prefix = optarg;
2516 break;
2517 case QEMU_OPTION_bootp:
2518 legacy_bootp_filename = optarg;
2519 break;
2520 case QEMU_OPTION_redir:
2521 if (net_slirp_redir(optarg) < 0)
2522 exit(1);
2523 break;
2524 #endif
2525 case QEMU_OPTION_bt:
2526 add_device_config(DEV_BT, optarg);
2527 break;
2528 case QEMU_OPTION_audio_help:
2529 if (!(audio_available())) {
2530 printf("Option %s not supported for this target\n", popt->name);
2531 exit(1);
2533 AUD_help ();
2534 exit (0);
2535 break;
2536 case QEMU_OPTION_soundhw:
2537 if (!(audio_available())) {
2538 printf("Option %s not supported for this target\n", popt->name);
2539 exit(1);
2541 select_soundhw (optarg);
2542 break;
2543 case QEMU_OPTION_h:
2544 help(0);
2545 break;
2546 case QEMU_OPTION_version:
2547 version();
2548 exit(0);
2549 break;
2550 case QEMU_OPTION_m: {
2551 int64_t value;
2553 value = strtosz(optarg, NULL);
2554 if (value < 0) {
2555 fprintf(stderr, "qemu: invalid ram size: %s\n", optarg);
2556 exit(1);
2559 if (value != (uint64_t)(ram_addr_t)value) {
2560 fprintf(stderr, "qemu: ram size too large\n");
2561 exit(1);
2563 ram_size = value;
2564 break;
2566 case QEMU_OPTION_mempath:
2567 mem_path = optarg;
2568 break;
2569 #ifdef MAP_POPULATE
2570 case QEMU_OPTION_mem_prealloc:
2571 mem_prealloc = 1;
2572 break;
2573 #endif
2574 case QEMU_OPTION_d:
2575 log_mask = optarg;
2576 break;
2577 case QEMU_OPTION_D:
2578 log_file = optarg;
2579 break;
2580 case QEMU_OPTION_s:
2581 gdbstub_dev = "tcp::" DEFAULT_GDBSTUB_PORT;
2582 break;
2583 case QEMU_OPTION_gdb:
2584 gdbstub_dev = optarg;
2585 break;
2586 case QEMU_OPTION_L:
2587 data_dir = optarg;
2588 break;
2589 case QEMU_OPTION_bios:
2590 bios_name = optarg;
2591 break;
2592 case QEMU_OPTION_singlestep:
2593 singlestep = 1;
2594 break;
2595 case QEMU_OPTION_S:
2596 autostart = 0;
2597 break;
2598 case QEMU_OPTION_k:
2599 keyboard_layout = optarg;
2600 break;
2601 case QEMU_OPTION_localtime:
2602 rtc_utc = 0;
2603 break;
2604 case QEMU_OPTION_vga:
2605 select_vgahw (optarg);
2606 break;
2607 case QEMU_OPTION_g:
2609 const char *p;
2610 int w, h, depth;
2611 p = optarg;
2612 w = strtol(p, (char **)&p, 10);
2613 if (w <= 0) {
2614 graphic_error:
2615 fprintf(stderr, "qemu: invalid resolution or depth\n");
2616 exit(1);
2618 if (*p != 'x')
2619 goto graphic_error;
2620 p++;
2621 h = strtol(p, (char **)&p, 10);
2622 if (h <= 0)
2623 goto graphic_error;
2624 if (*p == 'x') {
2625 p++;
2626 depth = strtol(p, (char **)&p, 10);
2627 if (depth != 8 && depth != 15 && depth != 16 &&
2628 depth != 24 && depth != 32)
2629 goto graphic_error;
2630 } else if (*p == '\0') {
2631 depth = graphic_depth;
2632 } else {
2633 goto graphic_error;
2636 graphic_width = w;
2637 graphic_height = h;
2638 graphic_depth = depth;
2640 break;
2641 case QEMU_OPTION_echr:
2643 char *r;
2644 term_escape_char = strtol(optarg, &r, 0);
2645 if (r == optarg)
2646 printf("Bad argument to echr\n");
2647 break;
2649 case QEMU_OPTION_monitor:
2650 monitor_parse(optarg, "readline");
2651 default_monitor = 0;
2652 break;
2653 case QEMU_OPTION_qmp:
2654 monitor_parse(optarg, "control");
2655 default_monitor = 0;
2656 break;
2657 case QEMU_OPTION_mon:
2658 opts = qemu_opts_parse(qemu_find_opts("mon"), optarg, 1);
2659 if (!opts) {
2660 exit(1);
2662 default_monitor = 0;
2663 break;
2664 case QEMU_OPTION_chardev:
2665 opts = qemu_opts_parse(qemu_find_opts("chardev"), optarg, 1);
2666 if (!opts) {
2667 exit(1);
2669 break;
2670 case QEMU_OPTION_fsdev:
2671 olist = qemu_find_opts("fsdev");
2672 if (!olist) {
2673 fprintf(stderr, "fsdev is not supported by this qemu build.\n");
2674 exit(1);
2676 opts = qemu_opts_parse(olist, optarg, 1);
2677 if (!opts) {
2678 fprintf(stderr, "parse error: %s\n", optarg);
2679 exit(1);
2681 break;
2682 case QEMU_OPTION_virtfs: {
2683 QemuOpts *fsdev;
2684 QemuOpts *device;
2686 olist = qemu_find_opts("virtfs");
2687 if (!olist) {
2688 fprintf(stderr, "virtfs is not supported by this qemu build.\n");
2689 exit(1);
2691 opts = qemu_opts_parse(olist, optarg, 1);
2692 if (!opts) {
2693 fprintf(stderr, "parse error: %s\n", optarg);
2694 exit(1);
2697 if (qemu_opt_get(opts, "fstype") == NULL ||
2698 qemu_opt_get(opts, "mount_tag") == NULL ||
2699 qemu_opt_get(opts, "path") == NULL ||
2700 qemu_opt_get(opts, "security_model") == NULL) {
2701 fprintf(stderr, "Usage: -virtfs fstype,path=/share_path/,"
2702 "security_model=[mapped|passthrough|none],"
2703 "mount_tag=tag.\n");
2704 exit(1);
2707 fsdev = qemu_opts_create(qemu_find_opts("fsdev"),
2708 qemu_opt_get(opts, "mount_tag"), 1);
2709 if (!fsdev) {
2710 fprintf(stderr, "duplicate fsdev id: %s\n",
2711 qemu_opt_get(opts, "mount_tag"));
2712 exit(1);
2714 qemu_opt_set(fsdev, "fstype", qemu_opt_get(opts, "fstype"));
2715 qemu_opt_set(fsdev, "path", qemu_opt_get(opts, "path"));
2716 qemu_opt_set(fsdev, "security_model",
2717 qemu_opt_get(opts, "security_model"));
2719 device = qemu_opts_create(qemu_find_opts("device"), NULL, 0);
2720 qemu_opt_set(device, "driver", "virtio-9p-pci");
2721 qemu_opt_set(device, "fsdev",
2722 qemu_opt_get(opts, "mount_tag"));
2723 qemu_opt_set(device, "mount_tag",
2724 qemu_opt_get(opts, "mount_tag"));
2725 break;
2727 case QEMU_OPTION_serial:
2728 add_device_config(DEV_SERIAL, optarg);
2729 default_serial = 0;
2730 if (strncmp(optarg, "mon:", 4) == 0) {
2731 default_monitor = 0;
2733 break;
2734 case QEMU_OPTION_watchdog:
2735 if (watchdog) {
2736 fprintf(stderr,
2737 "qemu: only one watchdog option may be given\n");
2738 return 1;
2740 watchdog = optarg;
2741 break;
2742 case QEMU_OPTION_watchdog_action:
2743 if (select_watchdog_action(optarg) == -1) {
2744 fprintf(stderr, "Unknown -watchdog-action parameter\n");
2745 exit(1);
2747 break;
2748 case QEMU_OPTION_virtiocon:
2749 add_device_config(DEV_VIRTCON, optarg);
2750 default_virtcon = 0;
2751 if (strncmp(optarg, "mon:", 4) == 0) {
2752 default_monitor = 0;
2754 break;
2755 case QEMU_OPTION_parallel:
2756 add_device_config(DEV_PARALLEL, optarg);
2757 default_parallel = 0;
2758 if (strncmp(optarg, "mon:", 4) == 0) {
2759 default_monitor = 0;
2761 break;
2762 case QEMU_OPTION_debugcon:
2763 add_device_config(DEV_DEBUGCON, optarg);
2764 break;
2765 case QEMU_OPTION_loadvm:
2766 loadvm = optarg;
2767 break;
2768 case QEMU_OPTION_full_screen:
2769 full_screen = 1;
2770 break;
2771 #ifdef CONFIG_SDL
2772 case QEMU_OPTION_no_frame:
2773 no_frame = 1;
2774 break;
2775 case QEMU_OPTION_alt_grab:
2776 alt_grab = 1;
2777 break;
2778 case QEMU_OPTION_ctrl_grab:
2779 ctrl_grab = 1;
2780 break;
2781 case QEMU_OPTION_no_quit:
2782 no_quit = 1;
2783 break;
2784 case QEMU_OPTION_sdl:
2785 display_type = DT_SDL;
2786 break;
2787 #else
2788 case QEMU_OPTION_no_frame:
2789 case QEMU_OPTION_alt_grab:
2790 case QEMU_OPTION_ctrl_grab:
2791 case QEMU_OPTION_no_quit:
2792 case QEMU_OPTION_sdl:
2793 fprintf(stderr, "SDL support is disabled\n");
2794 exit(1);
2795 #endif
2796 case QEMU_OPTION_pidfile:
2797 pid_file = optarg;
2798 break;
2799 case QEMU_OPTION_win2k_hack:
2800 win2k_install_hack = 1;
2801 break;
2802 case QEMU_OPTION_rtc_td_hack:
2803 rtc_td_hack = 1;
2804 break;
2805 case QEMU_OPTION_acpitable:
2806 do_acpitable_option(optarg);
2807 break;
2808 case QEMU_OPTION_smbios:
2809 do_smbios_option(optarg);
2810 break;
2811 case QEMU_OPTION_enable_kvm:
2812 olist = qemu_find_opts("machine");
2813 qemu_opts_reset(olist);
2814 qemu_opts_parse(olist, "accel=kvm", 0);
2815 break;
2816 case QEMU_OPTION_machine:
2817 olist = qemu_find_opts("machine");
2818 qemu_opts_reset(olist);
2819 opts = qemu_opts_parse(olist, optarg, 1);
2820 if (!opts) {
2821 fprintf(stderr, "parse error: %s\n", optarg);
2822 exit(1);
2824 optarg = qemu_opt_get(opts, "type");
2825 if (optarg) {
2826 machine = machine_parse(optarg);
2828 break;
2829 case QEMU_OPTION_no_kvm:
2830 olist = qemu_find_opts("machine");
2831 qemu_opts_reset(olist);
2832 qemu_opts_parse(olist, "accel=tcg", 0);
2833 break;
2834 #ifdef CONFIG_KVM
2835 case QEMU_OPTION_no_kvm_irqchip: {
2836 kvm_irqchip = 0;
2837 kvm_pit = 0;
2838 break;
2840 case QEMU_OPTION_no_kvm_pit: {
2841 kvm_pit = 0;
2842 break;
2844 case QEMU_OPTION_no_kvm_pit_reinjection: {
2845 kvm_pit_reinject = 0;
2846 break;
2848 #endif
2849 case QEMU_OPTION_usb:
2850 usb_enabled = 1;
2851 break;
2852 case QEMU_OPTION_usbdevice:
2853 usb_enabled = 1;
2854 add_device_config(DEV_USB, optarg);
2855 break;
2856 case QEMU_OPTION_device:
2857 if (!qemu_opts_parse(qemu_find_opts("device"), optarg, 1)) {
2858 exit(1);
2860 break;
2861 case QEMU_OPTION_smp:
2862 smp_parse(optarg);
2863 if (smp_cpus < 1) {
2864 fprintf(stderr, "Invalid number of CPUs\n");
2865 exit(1);
2867 if (max_cpus < smp_cpus) {
2868 fprintf(stderr, "maxcpus must be equal to or greater than "
2869 "smp\n");
2870 exit(1);
2872 if (max_cpus > 255) {
2873 fprintf(stderr, "Unsupported number of maxcpus\n");
2874 exit(1);
2876 break;
2877 case QEMU_OPTION_vnc:
2878 #ifdef CONFIG_VNC
2879 display_remote++;
2880 vnc_display = optarg;
2881 #else
2882 fprintf(stderr, "VNC support is disabled\n");
2883 exit(1);
2884 #endif
2885 break;
2886 case QEMU_OPTION_no_acpi:
2887 acpi_enabled = 0;
2888 break;
2889 case QEMU_OPTION_no_hpet:
2890 no_hpet = 1;
2891 break;
2892 case QEMU_OPTION_balloon:
2893 if (balloon_parse(optarg) < 0) {
2894 fprintf(stderr, "Unknown -balloon argument %s\n", optarg);
2895 exit(1);
2897 break;
2898 case QEMU_OPTION_no_reboot:
2899 no_reboot = 1;
2900 break;
2901 case QEMU_OPTION_no_shutdown:
2902 no_shutdown = 1;
2903 break;
2904 case QEMU_OPTION_show_cursor:
2905 cursor_hide = 0;
2906 break;
2907 case QEMU_OPTION_uuid:
2908 if(qemu_uuid_parse(optarg, qemu_uuid) < 0) {
2909 fprintf(stderr, "Fail to parse UUID string."
2910 " Wrong format.\n");
2911 exit(1);
2913 break;
2914 case QEMU_OPTION_option_rom:
2915 if (nb_option_roms >= MAX_OPTION_ROMS) {
2916 fprintf(stderr, "Too many option ROMs\n");
2917 exit(1);
2919 opts = qemu_opts_parse(qemu_find_opts("option-rom"), optarg, 1);
2920 option_rom[nb_option_roms].name = qemu_opt_get(opts, "romfile");
2921 option_rom[nb_option_roms].bootindex =
2922 qemu_opt_get_number(opts, "bootindex", -1);
2923 if (!option_rom[nb_option_roms].name) {
2924 fprintf(stderr, "Option ROM file is not specified\n");
2925 exit(1);
2927 nb_option_roms++;
2928 break;
2929 case QEMU_OPTION_semihosting:
2930 semihosting_enabled = 1;
2931 break;
2932 case QEMU_OPTION_tdf:
2933 time_drift_fix = 1;
2934 break;
2935 case QEMU_OPTION_kvm_shadow_memory:
2936 kvm_shadow_memory = (int64_t)atoi(optarg) * 1024 * 1024 / 4096;
2937 break;
2938 case QEMU_OPTION_name:
2939 qemu_name = g_strdup(optarg);
2941 char *p = strchr(qemu_name, ',');
2942 if (p != NULL) {
2943 *p++ = 0;
2944 if (strncmp(p, "process=", 8)) {
2945 fprintf(stderr, "Unknown subargument %s to -name\n", p);
2946 exit(1);
2948 p += 8;
2949 os_set_proc_name(p);
2952 break;
2953 case QEMU_OPTION_prom_env:
2954 if (nb_prom_envs >= MAX_PROM_ENVS) {
2955 fprintf(stderr, "Too many prom variables\n");
2956 exit(1);
2958 prom_envs[nb_prom_envs] = optarg;
2959 nb_prom_envs++;
2960 break;
2961 case QEMU_OPTION_old_param:
2962 old_param = 1;
2963 break;
2964 case QEMU_OPTION_clock:
2965 configure_alarms(optarg);
2966 break;
2967 case QEMU_OPTION_startdate:
2968 configure_rtc_date_offset(optarg, 1);
2969 break;
2970 case QEMU_OPTION_rtc:
2971 opts = qemu_opts_parse(qemu_find_opts("rtc"), optarg, 0);
2972 if (!opts) {
2973 exit(1);
2975 configure_rtc(opts);
2976 break;
2977 case QEMU_OPTION_tb_size:
2978 tcg_tb_size = strtol(optarg, NULL, 0);
2979 if (tcg_tb_size < 0) {
2980 tcg_tb_size = 0;
2982 break;
2983 case QEMU_OPTION_icount:
2984 icount_option = optarg;
2985 break;
2986 case QEMU_OPTION_incoming:
2987 incoming = optarg;
2988 incoming_expected = true;
2989 break;
2990 case QEMU_OPTION_nodefaults:
2991 default_serial = 0;
2992 default_parallel = 0;
2993 default_virtcon = 0;
2994 default_monitor = 0;
2995 default_vga = 0;
2996 default_net = 0;
2997 default_floppy = 0;
2998 default_cdrom = 0;
2999 default_sdcard = 0;
3000 break;
3001 #ifndef _WIN32
3002 case QEMU_OPTION_nvram:
3003 nvram = optarg;
3004 break;
3005 #endif
3006 case QEMU_OPTION_xen_domid:
3007 if (!(xen_available())) {
3008 printf("Option %s not supported for this target\n", popt->name);
3009 exit(1);
3011 xen_domid = atoi(optarg);
3012 break;
3013 case QEMU_OPTION_xen_create:
3014 if (!(xen_available())) {
3015 printf("Option %s not supported for this target\n", popt->name);
3016 exit(1);
3018 xen_mode = XEN_CREATE;
3019 break;
3020 case QEMU_OPTION_xen_attach:
3021 if (!(xen_available())) {
3022 printf("Option %s not supported for this target\n", popt->name);
3023 exit(1);
3025 xen_mode = XEN_ATTACH;
3026 break;
3027 case QEMU_OPTION_trace:
3029 opts = qemu_opts_parse(qemu_find_opts("trace"), optarg, 0);
3030 if (!opts) {
3031 exit(1);
3033 trace_events = qemu_opt_get(opts, "events");
3034 trace_file = qemu_opt_get(opts, "file");
3035 break;
3037 case QEMU_OPTION_readconfig:
3039 int ret = qemu_read_config_file(optarg);
3040 if (ret < 0) {
3041 fprintf(stderr, "read config %s: %s\n", optarg,
3042 strerror(-ret));
3043 exit(1);
3045 break;
3047 case QEMU_OPTION_spice:
3048 olist = qemu_find_opts("spice");
3049 if (!olist) {
3050 fprintf(stderr, "spice is not supported by this qemu build.\n");
3051 exit(1);
3053 opts = qemu_opts_parse(olist, optarg, 0);
3054 if (!opts) {
3055 fprintf(stderr, "parse error: %s\n", optarg);
3056 exit(1);
3058 break;
3059 case QEMU_OPTION_writeconfig:
3061 FILE *fp;
3062 if (strcmp(optarg, "-") == 0) {
3063 fp = stdout;
3064 } else {
3065 fp = fopen(optarg, "w");
3066 if (fp == NULL) {
3067 fprintf(stderr, "open %s: %s\n", optarg, strerror(errno));
3068 exit(1);
3071 qemu_config_write(fp);
3072 fclose(fp);
3073 break;
3075 default:
3076 os_parse_cmd_args(popt->index, optarg);
3080 loc_set_none();
3082 /* Open the logfile at this point, if necessary. We can't open the logfile
3083 * when encountering either of the logging options (-d or -D) because the
3084 * other one may be encountered later on the command line, changing the
3085 * location or level of logging.
3087 if (log_mask) {
3088 if (log_file) {
3089 set_cpu_log_filename(log_file);
3091 set_cpu_log(log_mask);
3094 if (!trace_backend_init(trace_events, trace_file)) {
3095 exit(1);
3098 /* If no data_dir is specified then try to find it relative to the
3099 executable path. */
3100 if (!data_dir) {
3101 data_dir = os_find_datadir(argv[0]);
3103 /* If all else fails use the install patch specified when building. */
3104 if (!data_dir) {
3105 data_dir = CONFIG_QEMU_DATADIR;
3109 * Default to max_cpus = smp_cpus, in case the user doesn't
3110 * specify a max_cpus value.
3112 if (!max_cpus)
3113 max_cpus = smp_cpus;
3115 machine->max_cpus = machine->max_cpus ?: 1; /* Default to UP */
3116 if (smp_cpus > machine->max_cpus) {
3117 fprintf(stderr, "Number of SMP cpus requested (%d), exceeds max cpus "
3118 "supported by machine `%s' (%d)\n", smp_cpus, machine->name,
3119 machine->max_cpus);
3120 exit(1);
3124 * Get the default machine options from the machine if it is not already
3125 * specified either by the configuration file or by the command line.
3127 if (machine->default_machine_opts) {
3128 QemuOptsList *list = qemu_find_opts("machine");
3129 const char *p = NULL;
3131 if (!QTAILQ_EMPTY(&list->head)) {
3132 p = qemu_opt_get(QTAILQ_FIRST(&list->head), "accel");
3134 if (p == NULL) {
3135 qemu_opts_reset(list);
3136 opts = qemu_opts_parse(list, machine->default_machine_opts, 0);
3137 if (!opts) {
3138 fprintf(stderr, "parse error for machine %s: %s\n",
3139 machine->name, machine->default_machine_opts);
3140 exit(1);
3145 qemu_opts_foreach(qemu_find_opts("device"), default_driver_check, NULL, 0);
3146 qemu_opts_foreach(qemu_find_opts("global"), default_driver_check, NULL, 0);
3148 if (machine->no_serial) {
3149 default_serial = 0;
3151 if (machine->no_parallel) {
3152 default_parallel = 0;
3154 if (!machine->use_virtcon) {
3155 default_virtcon = 0;
3157 if (machine->no_vga) {
3158 default_vga = 0;
3160 if (machine->no_floppy) {
3161 default_floppy = 0;
3163 if (machine->no_cdrom) {
3164 default_cdrom = 0;
3166 if (machine->no_sdcard) {
3167 default_sdcard = 0;
3170 if (display_type == DT_NOGRAPHIC) {
3171 if (default_parallel)
3172 add_device_config(DEV_PARALLEL, "null");
3173 if (default_serial && default_monitor) {
3174 add_device_config(DEV_SERIAL, "mon:stdio");
3175 } else if (default_virtcon && default_monitor) {
3176 add_device_config(DEV_VIRTCON, "mon:stdio");
3177 } else {
3178 if (default_serial)
3179 add_device_config(DEV_SERIAL, "stdio");
3180 if (default_virtcon)
3181 add_device_config(DEV_VIRTCON, "stdio");
3182 if (default_monitor)
3183 monitor_parse("stdio", "readline");
3185 } else {
3186 if (default_serial)
3187 add_device_config(DEV_SERIAL, "vc:80Cx24C");
3188 if (default_parallel)
3189 add_device_config(DEV_PARALLEL, "vc:80Cx24C");
3190 if (default_monitor)
3191 monitor_parse("vc:80Cx24C", "readline");
3192 if (default_virtcon)
3193 add_device_config(DEV_VIRTCON, "vc:80Cx24C");
3195 if (default_vga)
3196 vga_interface_type = VGA_CIRRUS;
3198 socket_init();
3200 if (qemu_opts_foreach(qemu_find_opts("chardev"), chardev_init_func, NULL, 1) != 0)
3201 exit(1);
3202 #ifdef CONFIG_VIRTFS
3203 if (qemu_opts_foreach(qemu_find_opts("fsdev"), fsdev_init_func, NULL, 1) != 0) {
3204 exit(1);
3206 #endif
3208 os_daemonize();
3210 if (pid_file && qemu_create_pidfile(pid_file) != 0) {
3211 os_pidfile_error();
3212 exit(1);
3215 /* init the memory */
3216 if (ram_size == 0) {
3217 ram_size = DEFAULT_RAM_SIZE * 1024 * 1024;
3220 configure_accelerator();
3222 if (qemu_init_main_loop()) {
3223 fprintf(stderr, "qemu_init_main_loop failed\n");
3224 exit(1);
3226 linux_boot = (kernel_filename != NULL);
3228 if (!linux_boot && *kernel_cmdline != '\0') {
3229 fprintf(stderr, "-append only allowed with -kernel option\n");
3230 exit(1);
3233 if (!linux_boot && initrd_filename != NULL) {
3234 fprintf(stderr, "-initrd only allowed with -kernel option\n");
3235 exit(1);
3238 os_set_line_buffering();
3240 if (init_timer_alarm() < 0) {
3241 fprintf(stderr, "could not initialize alarm timer\n");
3242 exit(1);
3244 configure_icount(icount_option);
3246 if (net_init_clients() < 0) {
3247 exit(1);
3250 /* init the bluetooth world */
3251 if (foreach_device_config(DEV_BT, bt_parse))
3252 exit(1);
3254 if (!xen_enabled()) {
3255 /* On 32-bit hosts, QEMU is limited by virtual address space */
3256 if (ram_size > (2047 << 20) && HOST_LONG_BITS == 32) {
3257 fprintf(stderr, "qemu: at most 2047 MB RAM can be simulated\n");
3258 exit(1);
3262 cpu_exec_init_all();
3264 bdrv_init_with_whitelist();
3266 blk_mig_init();
3268 /* open the virtual block devices */
3269 if (snapshot)
3270 qemu_opts_foreach(qemu_find_opts("drive"), drive_enable_snapshot, NULL, 0);
3271 if (qemu_opts_foreach(qemu_find_opts("drive"), drive_init_func, &machine->use_scsi, 1) != 0)
3272 exit(1);
3274 default_drive(default_cdrom, snapshot, machine->use_scsi,
3275 IF_DEFAULT, 2, CDROM_OPTS);
3276 default_drive(default_floppy, snapshot, machine->use_scsi,
3277 IF_FLOPPY, 0, FD_OPTS);
3278 default_drive(default_sdcard, snapshot, machine->use_scsi,
3279 IF_SD, 0, SD_OPTS);
3281 register_savevm_live(NULL, "ram", 0, 4, NULL, ram_save_live, NULL,
3282 ram_load, NULL);
3284 if (nb_numa_nodes > 0) {
3285 int i;
3287 if (nb_numa_nodes > MAX_NODES) {
3288 nb_numa_nodes = MAX_NODES;
3291 /* If no memory size if given for any node, assume the default case
3292 * and distribute the available memory equally across all nodes
3294 for (i = 0; i < nb_numa_nodes; i++) {
3295 if (node_mem[i] != 0)
3296 break;
3298 if (i == nb_numa_nodes) {
3299 uint64_t usedmem = 0;
3301 /* On Linux, the each node's border has to be 8MB aligned,
3302 * the final node gets the rest.
3304 for (i = 0; i < nb_numa_nodes - 1; i++) {
3305 node_mem[i] = (ram_size / nb_numa_nodes) & ~((1 << 23UL) - 1);
3306 usedmem += node_mem[i];
3308 node_mem[i] = ram_size - usedmem;
3311 for (i = 0; i < nb_numa_nodes; i++) {
3312 if (node_cpumask[i] != 0)
3313 break;
3315 /* assigning the VCPUs round-robin is easier to implement, guest OSes
3316 * must cope with this anyway, because there are BIOSes out there in
3317 * real machines which also use this scheme.
3319 if (i == nb_numa_nodes) {
3320 for (i = 0; i < smp_cpus; i++) {
3321 node_cpumask[i % nb_numa_nodes] |= 1 << i;
3326 if (qemu_opts_foreach(qemu_find_opts("mon"), mon_init_func, NULL, 1) != 0) {
3327 exit(1);
3330 if (foreach_device_config(DEV_SERIAL, serial_parse) < 0)
3331 exit(1);
3332 if (foreach_device_config(DEV_PARALLEL, parallel_parse) < 0)
3333 exit(1);
3334 if (foreach_device_config(DEV_VIRTCON, virtcon_parse) < 0)
3335 exit(1);
3336 if (foreach_device_config(DEV_DEBUGCON, debugcon_parse) < 0)
3337 exit(1);
3339 module_call_init(MODULE_INIT_DEVICE);
3341 if (qemu_opts_foreach(qemu_find_opts("device"), device_help_func, NULL, 0) != 0)
3342 exit(0);
3344 if (watchdog) {
3345 i = select_watchdog(watchdog);
3346 if (i > 0)
3347 exit (i == 1 ? 1 : 0);
3350 if (machine->compat_props) {
3351 qdev_prop_register_global_list(machine->compat_props);
3353 qemu_add_globals();
3355 machine->init(ram_size, boot_devices,
3356 kernel_filename, kernel_cmdline, initrd_filename, cpu_model);
3358 cpu_synchronize_all_post_init();
3360 set_numa_modes();
3362 current_machine = machine;
3364 /* init USB devices */
3365 if (usb_enabled) {
3366 if (foreach_device_config(DEV_USB, usb_parse) < 0)
3367 exit(1);
3370 /* init generic devices */
3371 if (qemu_opts_foreach(qemu_find_opts("device"), device_init_func, NULL, 1) != 0)
3372 exit(1);
3374 net_check_clients();
3376 /* just use the first displaystate for the moment */
3377 ds = get_displaystate();
3379 if (using_spice)
3380 display_remote++;
3381 if (display_type == DT_DEFAULT && !display_remote) {
3382 #if defined(CONFIG_SDL) || defined(CONFIG_COCOA)
3383 display_type = DT_SDL;
3384 #elif defined(CONFIG_VNC)
3385 vnc_display = "localhost:0,to=99";
3386 show_vnc_port = 1;
3387 #else
3388 display_type = DT_NONE;
3389 #endif
3393 /* init local displays */
3394 switch (display_type) {
3395 case DT_NOGRAPHIC:
3396 break;
3397 #if defined(CONFIG_CURSES)
3398 case DT_CURSES:
3399 curses_display_init(ds, full_screen);
3400 break;
3401 #endif
3402 #if defined(CONFIG_SDL)
3403 case DT_SDL:
3404 sdl_display_init(ds, full_screen, no_frame);
3405 break;
3406 #elif defined(CONFIG_COCOA)
3407 case DT_SDL:
3408 cocoa_display_init(ds, full_screen);
3409 break;
3410 #endif
3411 default:
3412 break;
3415 /* must be after terminal init, SDL library changes signal handlers */
3416 os_setup_signal_handling();
3418 #ifdef CONFIG_VNC
3419 /* init remote displays */
3420 if (vnc_display) {
3421 vnc_display_init(ds);
3422 if (vnc_display_open(ds, vnc_display) < 0)
3423 exit(1);
3425 if (show_vnc_port) {
3426 printf("VNC server running on `%s'\n", vnc_display_local_addr(ds));
3429 #endif
3430 #ifdef CONFIG_SPICE
3431 if (using_spice && !qxl_enabled) {
3432 qemu_spice_display_init(ds);
3434 #endif
3436 /* display setup */
3437 dpy_resize(ds);
3438 dcl = ds->listeners;
3439 while (dcl != NULL) {
3440 if (dcl->dpy_refresh != NULL) {
3441 ds->gui_timer = qemu_new_timer_ms(rt_clock, gui_update, ds);
3442 qemu_mod_timer(ds->gui_timer, qemu_get_clock_ms(rt_clock));
3443 break;
3445 dcl = dcl->next;
3447 if (ds->gui_timer == NULL) {
3448 nographic_timer = qemu_new_timer_ms(rt_clock, nographic_update, NULL);
3449 qemu_mod_timer(nographic_timer, qemu_get_clock_ms(rt_clock));
3451 text_consoles_set_display(ds);
3453 if (gdbstub_dev && gdbserver_start(gdbstub_dev) < 0) {
3454 fprintf(stderr, "qemu: could not open gdbserver on device '%s'\n",
3455 gdbstub_dev);
3456 exit(1);
3459 qdev_machine_creation_done();
3461 if (rom_load_all() != 0) {
3462 fprintf(stderr, "rom loading failed\n");
3463 exit(1);
3466 /* TODO: once all bus devices are qdevified, this should be done
3467 * when bus is created by qdev.c */
3468 qemu_register_reset(qbus_reset_all_fn, sysbus_get_default());
3469 qemu_run_machine_init_done_notifiers();
3471 qemu_system_reset(VMRESET_SILENT);
3472 if (loadvm) {
3473 if (load_vmstate(loadvm) < 0) {
3474 autostart = 0;
3478 if (incoming) {
3479 int ret = qemu_start_incoming_migration(incoming);
3480 if (ret < 0) {
3481 fprintf(stderr, "Migration failed. Exit code %s(%d), exiting.\n",
3482 incoming, ret);
3483 exit(ret);
3485 } else if (autostart) {
3486 vm_start();
3489 os_setup_post();
3491 main_loop();
3492 quit_timers();
3493 net_cleanup();
3494 res_free();
3496 return 0;