main: switch qemu_set_fd_handler to g_io_add_watch
[qemu.git] / vl.c
bloba6f4c710d663a812869cafe897fe60179db0e830
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 #ifdef CONFIG_VIRTFS
151 #include "fsdev/qemu-fsdev.h"
152 #endif
154 #include "disas.h"
156 #include "qemu_socket.h"
158 #include "slirp/libslirp.h"
160 #include "trace.h"
161 #include "simpletrace.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 #ifdef CONFIG_VNC
212 const char *vnc_display;
213 #endif
214 int acpi_enabled = 1;
215 int no_hpet = 0;
216 int fd_bootchk = 1;
217 int no_reboot = 0;
218 int no_shutdown = 0;
219 int cursor_hide = 1;
220 int graphic_rotate = 0;
221 uint8_t irq0override = 1;
222 const char *watchdog;
223 QEMUOptionRom option_rom[MAX_OPTION_ROMS];
224 int nb_option_roms;
225 int semihosting_enabled = 0;
226 int old_param = 0;
227 const char *qemu_name;
228 int alt_grab = 0;
229 int ctrl_grab = 0;
230 unsigned int nb_prom_envs = 0;
231 const char *prom_envs[MAX_PROM_ENVS];
232 int boot_menu;
233 uint8_t *boot_splash_filedata;
234 int boot_splash_filedata_size;
235 uint8_t qemu_extra_params_fw[2];
237 typedef struct FWBootEntry FWBootEntry;
239 struct FWBootEntry {
240 QTAILQ_ENTRY(FWBootEntry) link;
241 int32_t bootindex;
242 DeviceState *dev;
243 char *suffix;
246 QTAILQ_HEAD(, FWBootEntry) fw_boot_order = QTAILQ_HEAD_INITIALIZER(fw_boot_order);
248 int nb_numa_nodes;
249 uint64_t node_mem[MAX_NODES];
250 uint64_t node_cpumask[MAX_NODES];
252 static QEMUTimer *nographic_timer;
254 uint8_t qemu_uuid[16];
256 static QEMUBootSetHandler *boot_set_handler;
257 static void *boot_set_opaque;
259 static NotifierList exit_notifiers =
260 NOTIFIER_LIST_INITIALIZER(exit_notifiers);
262 static NotifierList machine_init_done_notifiers =
263 NOTIFIER_LIST_INITIALIZER(machine_init_done_notifiers);
265 static int tcg_allowed = 1;
266 int kvm_allowed = 0;
267 int xen_allowed = 0;
268 uint32_t xen_domid;
269 enum xen_mode xen_mode = XEN_EMULATE;
270 static int tcg_tb_size;
272 static int default_serial = 1;
273 static int default_parallel = 1;
274 static int default_virtcon = 1;
275 static int default_monitor = 1;
276 static int default_vga = 1;
277 static int default_floppy = 1;
278 static int default_cdrom = 1;
279 static int default_sdcard = 1;
281 static struct {
282 const char *driver;
283 int *flag;
284 } default_list[] = {
285 { .driver = "isa-serial", .flag = &default_serial },
286 { .driver = "isa-parallel", .flag = &default_parallel },
287 { .driver = "isa-fdc", .flag = &default_floppy },
288 { .driver = "ide-cd", .flag = &default_cdrom },
289 { .driver = "ide-hd", .flag = &default_cdrom },
290 { .driver = "ide-drive", .flag = &default_cdrom },
291 { .driver = "scsi-cd", .flag = &default_cdrom },
292 { .driver = "virtio-serial-pci", .flag = &default_virtcon },
293 { .driver = "virtio-serial-s390", .flag = &default_virtcon },
294 { .driver = "virtio-serial", .flag = &default_virtcon },
295 { .driver = "VGA", .flag = &default_vga },
296 { .driver = "cirrus-vga", .flag = &default_vga },
297 { .driver = "vmware-svga", .flag = &default_vga },
298 { .driver = "isa-vga", .flag = &default_vga },
299 { .driver = "qxl-vga", .flag = &default_vga },
302 static void res_free(void)
304 if (boot_splash_filedata != NULL) {
305 g_free(boot_splash_filedata);
306 boot_splash_filedata = NULL;
310 static int default_driver_check(QemuOpts *opts, void *opaque)
312 const char *driver = qemu_opt_get(opts, "driver");
313 int i;
315 if (!driver)
316 return 0;
317 for (i = 0; i < ARRAY_SIZE(default_list); i++) {
318 if (strcmp(default_list[i].driver, driver) != 0)
319 continue;
320 *(default_list[i].flag) = 0;
322 return 0;
325 /***********************************************************/
326 /* real time host monotonic timer */
328 /***********************************************************/
329 /* host time/date access */
330 void qemu_get_timedate(struct tm *tm, int offset)
332 time_t ti;
333 struct tm *ret;
335 time(&ti);
336 ti += offset;
337 if (rtc_date_offset == -1) {
338 if (rtc_utc)
339 ret = gmtime(&ti);
340 else
341 ret = localtime(&ti);
342 } else {
343 ti -= rtc_date_offset;
344 ret = gmtime(&ti);
347 memcpy(tm, ret, sizeof(struct tm));
350 int qemu_timedate_diff(struct tm *tm)
352 time_t seconds;
354 if (rtc_date_offset == -1)
355 if (rtc_utc)
356 seconds = mktimegm(tm);
357 else
358 seconds = mktime(tm);
359 else
360 seconds = mktimegm(tm) + rtc_date_offset;
362 return seconds - time(NULL);
365 void rtc_change_mon_event(struct tm *tm)
367 QObject *data;
369 data = qobject_from_jsonf("{ 'offset': %d }", qemu_timedate_diff(tm));
370 monitor_protocol_event(QEVENT_RTC_CHANGE, data);
371 qobject_decref(data);
374 static void configure_rtc_date_offset(const char *startdate, int legacy)
376 time_t rtc_start_date;
377 struct tm tm;
379 if (!strcmp(startdate, "now") && legacy) {
380 rtc_date_offset = -1;
381 } else {
382 if (sscanf(startdate, "%d-%d-%dT%d:%d:%d",
383 &tm.tm_year,
384 &tm.tm_mon,
385 &tm.tm_mday,
386 &tm.tm_hour,
387 &tm.tm_min,
388 &tm.tm_sec) == 6) {
389 /* OK */
390 } else if (sscanf(startdate, "%d-%d-%d",
391 &tm.tm_year,
392 &tm.tm_mon,
393 &tm.tm_mday) == 3) {
394 tm.tm_hour = 0;
395 tm.tm_min = 0;
396 tm.tm_sec = 0;
397 } else {
398 goto date_fail;
400 tm.tm_year -= 1900;
401 tm.tm_mon--;
402 rtc_start_date = mktimegm(&tm);
403 if (rtc_start_date == -1) {
404 date_fail:
405 fprintf(stderr, "Invalid date format. Valid formats are:\n"
406 "'2006-06-17T16:01:21' or '2006-06-17'\n");
407 exit(1);
409 rtc_date_offset = time(NULL) - rtc_start_date;
413 static void configure_rtc(QemuOpts *opts)
415 const char *value;
417 value = qemu_opt_get(opts, "base");
418 if (value) {
419 if (!strcmp(value, "utc")) {
420 rtc_utc = 1;
421 } else if (!strcmp(value, "localtime")) {
422 rtc_utc = 0;
423 } else {
424 configure_rtc_date_offset(value, 0);
427 value = qemu_opt_get(opts, "clock");
428 if (value) {
429 if (!strcmp(value, "host")) {
430 rtc_clock = host_clock;
431 } else if (!strcmp(value, "vm")) {
432 rtc_clock = vm_clock;
433 } else {
434 fprintf(stderr, "qemu: invalid option value '%s'\n", value);
435 exit(1);
438 value = qemu_opt_get(opts, "driftfix");
439 if (value) {
440 if (!strcmp(value, "slew")) {
441 rtc_td_hack = 1;
442 } else if (!strcmp(value, "none")) {
443 rtc_td_hack = 0;
444 } else {
445 fprintf(stderr, "qemu: invalid option value '%s'\n", value);
446 exit(1);
451 /***********************************************************/
452 /* Bluetooth support */
453 static int nb_hcis;
454 static int cur_hci;
455 static struct HCIInfo *hci_table[MAX_NICS];
457 static struct bt_vlan_s {
458 struct bt_scatternet_s net;
459 int id;
460 struct bt_vlan_s *next;
461 } *first_bt_vlan;
463 /* find or alloc a new bluetooth "VLAN" */
464 static struct bt_scatternet_s *qemu_find_bt_vlan(int id)
466 struct bt_vlan_s **pvlan, *vlan;
467 for (vlan = first_bt_vlan; vlan != NULL; vlan = vlan->next) {
468 if (vlan->id == id)
469 return &vlan->net;
471 vlan = g_malloc0(sizeof(struct bt_vlan_s));
472 vlan->id = id;
473 pvlan = &first_bt_vlan;
474 while (*pvlan != NULL)
475 pvlan = &(*pvlan)->next;
476 *pvlan = vlan;
477 return &vlan->net;
480 static void null_hci_send(struct HCIInfo *hci, const uint8_t *data, int len)
484 static int null_hci_addr_set(struct HCIInfo *hci, const uint8_t *bd_addr)
486 return -ENOTSUP;
489 static struct HCIInfo null_hci = {
490 .cmd_send = null_hci_send,
491 .sco_send = null_hci_send,
492 .acl_send = null_hci_send,
493 .bdaddr_set = null_hci_addr_set,
496 struct HCIInfo *qemu_next_hci(void)
498 if (cur_hci == nb_hcis)
499 return &null_hci;
501 return hci_table[cur_hci++];
504 static struct HCIInfo *hci_init(const char *str)
506 char *endp;
507 struct bt_scatternet_s *vlan = 0;
509 if (!strcmp(str, "null"))
510 /* null */
511 return &null_hci;
512 else if (!strncmp(str, "host", 4) && (str[4] == '\0' || str[4] == ':'))
513 /* host[:hciN] */
514 return bt_host_hci(str[4] ? str + 5 : "hci0");
515 else if (!strncmp(str, "hci", 3)) {
516 /* hci[,vlan=n] */
517 if (str[3]) {
518 if (!strncmp(str + 3, ",vlan=", 6)) {
519 vlan = qemu_find_bt_vlan(strtol(str + 9, &endp, 0));
520 if (*endp)
521 vlan = 0;
523 } else
524 vlan = qemu_find_bt_vlan(0);
525 if (vlan)
526 return bt_new_hci(vlan);
529 fprintf(stderr, "qemu: Unknown bluetooth HCI `%s'.\n", str);
531 return 0;
534 static int bt_hci_parse(const char *str)
536 struct HCIInfo *hci;
537 bdaddr_t bdaddr;
539 if (nb_hcis >= MAX_NICS) {
540 fprintf(stderr, "qemu: Too many bluetooth HCIs (max %i).\n", MAX_NICS);
541 return -1;
544 hci = hci_init(str);
545 if (!hci)
546 return -1;
548 bdaddr.b[0] = 0x52;
549 bdaddr.b[1] = 0x54;
550 bdaddr.b[2] = 0x00;
551 bdaddr.b[3] = 0x12;
552 bdaddr.b[4] = 0x34;
553 bdaddr.b[5] = 0x56 + nb_hcis;
554 hci->bdaddr_set(hci, bdaddr.b);
556 hci_table[nb_hcis++] = hci;
558 return 0;
561 static void bt_vhci_add(int vlan_id)
563 struct bt_scatternet_s *vlan = qemu_find_bt_vlan(vlan_id);
565 if (!vlan->slave)
566 fprintf(stderr, "qemu: warning: adding a VHCI to "
567 "an empty scatternet %i\n", vlan_id);
569 bt_vhci_init(bt_new_hci(vlan));
572 static struct bt_device_s *bt_device_add(const char *opt)
574 struct bt_scatternet_s *vlan;
575 int vlan_id = 0;
576 char *endp = strstr(opt, ",vlan=");
577 int len = (endp ? endp - opt : strlen(opt)) + 1;
578 char devname[10];
580 pstrcpy(devname, MIN(sizeof(devname), len), opt);
582 if (endp) {
583 vlan_id = strtol(endp + 6, &endp, 0);
584 if (*endp) {
585 fprintf(stderr, "qemu: unrecognised bluetooth vlan Id\n");
586 return 0;
590 vlan = qemu_find_bt_vlan(vlan_id);
592 if (!vlan->slave)
593 fprintf(stderr, "qemu: warning: adding a slave device to "
594 "an empty scatternet %i\n", vlan_id);
596 if (!strcmp(devname, "keyboard"))
597 return bt_keyboard_init(vlan);
599 fprintf(stderr, "qemu: unsupported bluetooth device `%s'\n", devname);
600 return 0;
603 static int bt_parse(const char *opt)
605 const char *endp, *p;
606 int vlan;
608 if (strstart(opt, "hci", &endp)) {
609 if (!*endp || *endp == ',') {
610 if (*endp)
611 if (!strstart(endp, ",vlan=", 0))
612 opt = endp + 1;
614 return bt_hci_parse(opt);
616 } else if (strstart(opt, "vhci", &endp)) {
617 if (!*endp || *endp == ',') {
618 if (*endp) {
619 if (strstart(endp, ",vlan=", &p)) {
620 vlan = strtol(p, (char **) &endp, 0);
621 if (*endp) {
622 fprintf(stderr, "qemu: bad scatternet '%s'\n", p);
623 return 1;
625 } else {
626 fprintf(stderr, "qemu: bad parameter '%s'\n", endp + 1);
627 return 1;
629 } else
630 vlan = 0;
632 bt_vhci_add(vlan);
633 return 0;
635 } else if (strstart(opt, "device:", &endp))
636 return !bt_device_add(endp);
638 fprintf(stderr, "qemu: bad bluetooth parameter '%s'\n", opt);
639 return 1;
642 /***********************************************************/
643 /* QEMU Block devices */
645 #define HD_OPTS "media=disk"
646 #define CDROM_OPTS "media=cdrom"
647 #define FD_OPTS ""
648 #define PFLASH_OPTS ""
649 #define MTD_OPTS ""
650 #define SD_OPTS ""
652 static int drive_init_func(QemuOpts *opts, void *opaque)
654 int *use_scsi = opaque;
656 return drive_init(opts, *use_scsi) == NULL;
659 static int drive_enable_snapshot(QemuOpts *opts, void *opaque)
661 if (NULL == qemu_opt_get(opts, "snapshot")) {
662 qemu_opt_set(opts, "snapshot", "on");
664 return 0;
667 static void default_drive(int enable, int snapshot, int use_scsi,
668 BlockInterfaceType type, int index,
669 const char *optstr)
671 QemuOpts *opts;
673 if (type == IF_DEFAULT) {
674 type = use_scsi ? IF_SCSI : IF_IDE;
677 if (!enable || drive_get_by_index(type, index)) {
678 return;
681 opts = drive_add(type, index, NULL, optstr);
682 if (snapshot) {
683 drive_enable_snapshot(opts, NULL);
685 if (!drive_init(opts, use_scsi)) {
686 exit(1);
690 void qemu_register_boot_set(QEMUBootSetHandler *func, void *opaque)
692 boot_set_handler = func;
693 boot_set_opaque = opaque;
696 int qemu_boot_set(const char *boot_devices)
698 if (!boot_set_handler) {
699 return -EINVAL;
701 return boot_set_handler(boot_set_opaque, boot_devices);
704 static void validate_bootdevices(char *devices)
706 /* We just do some generic consistency checks */
707 const char *p;
708 int bitmap = 0;
710 for (p = devices; *p != '\0'; p++) {
711 /* Allowed boot devices are:
712 * a-b: floppy disk drives
713 * c-f: IDE disk drives
714 * g-m: machine implementation dependant drives
715 * n-p: network devices
716 * It's up to each machine implementation to check if the given boot
717 * devices match the actual hardware implementation and firmware
718 * features.
720 if (*p < 'a' || *p > 'p') {
721 fprintf(stderr, "Invalid boot device '%c'\n", *p);
722 exit(1);
724 if (bitmap & (1 << (*p - 'a'))) {
725 fprintf(stderr, "Boot device '%c' was given twice\n", *p);
726 exit(1);
728 bitmap |= 1 << (*p - 'a');
732 static void restore_boot_devices(void *opaque)
734 char *standard_boot_devices = opaque;
735 static int first = 1;
737 /* Restore boot order and remove ourselves after the first boot */
738 if (first) {
739 first = 0;
740 return;
743 qemu_boot_set(standard_boot_devices);
745 qemu_unregister_reset(restore_boot_devices, standard_boot_devices);
746 g_free(standard_boot_devices);
749 void add_boot_device_path(int32_t bootindex, DeviceState *dev,
750 const char *suffix)
752 FWBootEntry *node, *i;
754 if (bootindex < 0) {
755 return;
758 assert(dev != NULL || suffix != NULL);
760 node = g_malloc0(sizeof(FWBootEntry));
761 node->bootindex = bootindex;
762 node->suffix = suffix ? g_strdup(suffix) : NULL;
763 node->dev = dev;
765 QTAILQ_FOREACH(i, &fw_boot_order, link) {
766 if (i->bootindex == bootindex) {
767 fprintf(stderr, "Two devices with same boot index %d\n", bootindex);
768 exit(1);
769 } else if (i->bootindex < bootindex) {
770 continue;
772 QTAILQ_INSERT_BEFORE(i, node, link);
773 return;
775 QTAILQ_INSERT_TAIL(&fw_boot_order, node, link);
779 * This function returns null terminated string that consist of new line
780 * separated device paths.
782 * memory pointed by "size" is assigned total length of the array in bytes
785 char *get_boot_devices_list(uint32_t *size)
787 FWBootEntry *i;
788 uint32_t total = 0;
789 char *list = NULL;
791 QTAILQ_FOREACH(i, &fw_boot_order, link) {
792 char *devpath = NULL, *bootpath;
793 int len;
795 if (i->dev) {
796 devpath = qdev_get_fw_dev_path(i->dev);
797 assert(devpath);
800 if (i->suffix && devpath) {
801 size_t bootpathlen = strlen(devpath) + strlen(i->suffix) + 1;
803 bootpath = g_malloc(bootpathlen);
804 snprintf(bootpath, bootpathlen, "%s%s", devpath, i->suffix);
805 g_free(devpath);
806 } else if (devpath) {
807 bootpath = devpath;
808 } else {
809 bootpath = g_strdup(i->suffix);
810 assert(bootpath);
813 if (total) {
814 list[total-1] = '\n';
816 len = strlen(bootpath) + 1;
817 list = g_realloc(list, total + len);
818 memcpy(&list[total], bootpath, len);
819 total += len;
820 g_free(bootpath);
823 *size = total;
825 return list;
828 static void numa_add(const char *optarg)
830 char option[128];
831 char *endptr;
832 unsigned long long value, endvalue;
833 int nodenr;
835 optarg = get_opt_name(option, 128, optarg, ',') + 1;
836 if (!strcmp(option, "node")) {
837 if (get_param_value(option, 128, "nodeid", optarg) == 0) {
838 nodenr = nb_numa_nodes;
839 } else {
840 nodenr = strtoull(option, NULL, 10);
843 if (get_param_value(option, 128, "mem", optarg) == 0) {
844 node_mem[nodenr] = 0;
845 } else {
846 int64_t sval;
847 sval = strtosz(option, NULL);
848 if (sval < 0) {
849 fprintf(stderr, "qemu: invalid numa mem size: %s\n", optarg);
850 exit(1);
852 node_mem[nodenr] = sval;
854 if (get_param_value(option, 128, "cpus", optarg) == 0) {
855 node_cpumask[nodenr] = 0;
856 } else {
857 value = strtoull(option, &endptr, 10);
858 if (value >= 64) {
859 value = 63;
860 fprintf(stderr, "only 64 CPUs in NUMA mode supported.\n");
861 } else {
862 if (*endptr == '-') {
863 endvalue = strtoull(endptr+1, &endptr, 10);
864 if (endvalue >= 63) {
865 endvalue = 62;
866 fprintf(stderr,
867 "only 63 CPUs in NUMA mode supported.\n");
869 value = (2ULL << endvalue) - (1ULL << value);
870 } else {
871 value = 1ULL << value;
874 node_cpumask[nodenr] = value;
876 nb_numa_nodes++;
878 return;
881 static void smp_parse(const char *optarg)
883 int smp, sockets = 0, threads = 0, cores = 0;
884 char *endptr;
885 char option[128];
887 smp = strtoul(optarg, &endptr, 10);
888 if (endptr != optarg) {
889 if (*endptr == ',') {
890 endptr++;
893 if (get_param_value(option, 128, "sockets", endptr) != 0)
894 sockets = strtoull(option, NULL, 10);
895 if (get_param_value(option, 128, "cores", endptr) != 0)
896 cores = strtoull(option, NULL, 10);
897 if (get_param_value(option, 128, "threads", endptr) != 0)
898 threads = strtoull(option, NULL, 10);
899 if (get_param_value(option, 128, "maxcpus", endptr) != 0)
900 max_cpus = strtoull(option, NULL, 10);
902 /* compute missing values, prefer sockets over cores over threads */
903 if (smp == 0 || sockets == 0) {
904 sockets = sockets > 0 ? sockets : 1;
905 cores = cores > 0 ? cores : 1;
906 threads = threads > 0 ? threads : 1;
907 if (smp == 0) {
908 smp = cores * threads * sockets;
910 } else {
911 if (cores == 0) {
912 threads = threads > 0 ? threads : 1;
913 cores = smp / (sockets * threads);
914 } else {
915 threads = smp / (cores * sockets);
918 smp_cpus = smp;
919 smp_cores = cores > 0 ? cores : 1;
920 smp_threads = threads > 0 ? threads : 1;
921 if (max_cpus == 0)
922 max_cpus = smp_cpus;
925 /***********************************************************/
926 /* USB devices */
928 static int usb_device_add(const char *devname)
930 const char *p;
931 USBDevice *dev = NULL;
933 if (!usb_enabled)
934 return -1;
936 /* drivers with .usbdevice_name entry in USBDeviceInfo */
937 dev = usbdevice_create(devname);
938 if (dev)
939 goto done;
941 /* the other ones */
942 #ifndef CONFIG_LINUX
943 /* only the linux version is qdev-ified, usb-bsd still needs this */
944 if (strstart(devname, "host:", &p)) {
945 dev = usb_host_device_open(p);
946 } else
947 #endif
948 if (!strcmp(devname, "bt") || strstart(devname, "bt:", &p)) {
949 dev = usb_bt_init(devname[2] ? hci_init(p) :
950 bt_new_hci(qemu_find_bt_vlan(0)));
951 } else {
952 return -1;
954 if (!dev)
955 return -1;
957 done:
958 return 0;
961 static int usb_device_del(const char *devname)
963 int bus_num, addr;
964 const char *p;
966 if (strstart(devname, "host:", &p))
967 return usb_host_device_close(p);
969 if (!usb_enabled)
970 return -1;
972 p = strchr(devname, '.');
973 if (!p)
974 return -1;
975 bus_num = strtoul(devname, NULL, 0);
976 addr = strtoul(p + 1, NULL, 0);
978 return usb_device_delete_addr(bus_num, addr);
981 static int usb_parse(const char *cmdline)
983 int r;
984 r = usb_device_add(cmdline);
985 if (r < 0) {
986 fprintf(stderr, "qemu: could not add USB device '%s'\n", cmdline);
988 return r;
991 void do_usb_add(Monitor *mon, const QDict *qdict)
993 const char *devname = qdict_get_str(qdict, "devname");
994 if (usb_device_add(devname) < 0) {
995 error_report("could not add USB device '%s'", devname);
999 void do_usb_del(Monitor *mon, const QDict *qdict)
1001 const char *devname = qdict_get_str(qdict, "devname");
1002 if (usb_device_del(devname) < 0) {
1003 error_report("could not delete USB device '%s'", devname);
1007 /***********************************************************/
1008 /* PCMCIA/Cardbus */
1010 static struct pcmcia_socket_entry_s {
1011 PCMCIASocket *socket;
1012 struct pcmcia_socket_entry_s *next;
1013 } *pcmcia_sockets = 0;
1015 void pcmcia_socket_register(PCMCIASocket *socket)
1017 struct pcmcia_socket_entry_s *entry;
1019 entry = g_malloc(sizeof(struct pcmcia_socket_entry_s));
1020 entry->socket = socket;
1021 entry->next = pcmcia_sockets;
1022 pcmcia_sockets = entry;
1025 void pcmcia_socket_unregister(PCMCIASocket *socket)
1027 struct pcmcia_socket_entry_s *entry, **ptr;
1029 ptr = &pcmcia_sockets;
1030 for (entry = *ptr; entry; ptr = &entry->next, entry = *ptr)
1031 if (entry->socket == socket) {
1032 *ptr = entry->next;
1033 g_free(entry);
1037 void pcmcia_info(Monitor *mon)
1039 struct pcmcia_socket_entry_s *iter;
1041 if (!pcmcia_sockets)
1042 monitor_printf(mon, "No PCMCIA sockets\n");
1044 for (iter = pcmcia_sockets; iter; iter = iter->next)
1045 monitor_printf(mon, "%s: %s\n", iter->socket->slot_string,
1046 iter->socket->attached ? iter->socket->card_string :
1047 "Empty");
1050 /***********************************************************/
1051 /* machine registration */
1053 static QEMUMachine *first_machine = NULL;
1054 QEMUMachine *current_machine = NULL;
1056 int qemu_register_machine(QEMUMachine *m)
1058 QEMUMachine **pm;
1059 pm = &first_machine;
1060 while (*pm != NULL)
1061 pm = &(*pm)->next;
1062 m->next = NULL;
1063 *pm = m;
1064 return 0;
1067 static QEMUMachine *find_machine(const char *name)
1069 QEMUMachine *m;
1071 for(m = first_machine; m != NULL; m = m->next) {
1072 if (!strcmp(m->name, name))
1073 return m;
1074 if (m->alias && !strcmp(m->alias, name))
1075 return m;
1077 return NULL;
1080 static QEMUMachine *find_default_machine(void)
1082 QEMUMachine *m;
1084 for(m = first_machine; m != NULL; m = m->next) {
1085 if (m->is_default) {
1086 return m;
1089 return NULL;
1092 /***********************************************************/
1093 /* main execution loop */
1095 static void gui_update(void *opaque)
1097 uint64_t interval = GUI_REFRESH_INTERVAL;
1098 DisplayState *ds = opaque;
1099 DisplayChangeListener *dcl = ds->listeners;
1101 qemu_flush_coalesced_mmio_buffer();
1102 dpy_refresh(ds);
1104 while (dcl != NULL) {
1105 if (dcl->gui_timer_interval &&
1106 dcl->gui_timer_interval < interval)
1107 interval = dcl->gui_timer_interval;
1108 dcl = dcl->next;
1110 qemu_mod_timer(ds->gui_timer, interval + qemu_get_clock_ms(rt_clock));
1113 static void nographic_update(void *opaque)
1115 uint64_t interval = GUI_REFRESH_INTERVAL;
1117 qemu_flush_coalesced_mmio_buffer();
1118 qemu_mod_timer(nographic_timer, interval + qemu_get_clock_ms(rt_clock));
1121 struct vm_change_state_entry {
1122 VMChangeStateHandler *cb;
1123 void *opaque;
1124 QLIST_ENTRY (vm_change_state_entry) entries;
1127 static QLIST_HEAD(vm_change_state_head, vm_change_state_entry) vm_change_state_head;
1129 VMChangeStateEntry *qemu_add_vm_change_state_handler(VMChangeStateHandler *cb,
1130 void *opaque)
1132 VMChangeStateEntry *e;
1134 e = g_malloc0(sizeof (*e));
1136 e->cb = cb;
1137 e->opaque = opaque;
1138 QLIST_INSERT_HEAD(&vm_change_state_head, e, entries);
1139 return e;
1142 void qemu_del_vm_change_state_handler(VMChangeStateEntry *e)
1144 QLIST_REMOVE (e, entries);
1145 g_free (e);
1148 void vm_state_notify(int running, int reason)
1150 VMChangeStateEntry *e;
1152 trace_vm_state_notify(running, reason);
1154 for (e = vm_change_state_head.lh_first; e; e = e->entries.le_next) {
1155 e->cb(e->opaque, running, reason);
1159 void vm_start(void)
1161 if (!vm_running) {
1162 cpu_enable_ticks();
1163 vm_running = 1;
1164 vm_state_notify(1, 0);
1165 resume_all_vcpus();
1166 monitor_protocol_event(QEVENT_RESUME, NULL);
1170 /* reset/shutdown handler */
1172 typedef struct QEMUResetEntry {
1173 QTAILQ_ENTRY(QEMUResetEntry) entry;
1174 QEMUResetHandler *func;
1175 void *opaque;
1176 } QEMUResetEntry;
1178 static QTAILQ_HEAD(reset_handlers, QEMUResetEntry) reset_handlers =
1179 QTAILQ_HEAD_INITIALIZER(reset_handlers);
1180 static int reset_requested;
1181 static int shutdown_requested, shutdown_signal = -1;
1182 static pid_t shutdown_pid;
1183 static int powerdown_requested;
1184 static int debug_requested;
1185 static int vmstop_requested;
1187 int qemu_shutdown_requested_get(void)
1189 return shutdown_requested;
1192 int qemu_reset_requested_get(void)
1194 return reset_requested;
1197 int qemu_shutdown_requested(void)
1199 int r = shutdown_requested;
1200 shutdown_requested = 0;
1201 return r;
1204 void qemu_kill_report(void)
1206 if (shutdown_signal != -1) {
1207 fprintf(stderr, "qemu: terminating on signal %d", shutdown_signal);
1208 if (shutdown_pid == 0) {
1209 /* This happens for eg ^C at the terminal, so it's worth
1210 * avoiding printing an odd message in that case.
1212 fputc('\n', stderr);
1213 } else {
1214 fprintf(stderr, " from pid " FMT_pid "\n", shutdown_pid);
1216 shutdown_signal = -1;
1220 int qemu_reset_requested(void)
1222 int r = reset_requested;
1223 reset_requested = 0;
1224 return r;
1227 int qemu_powerdown_requested(void)
1229 int r = powerdown_requested;
1230 powerdown_requested = 0;
1231 return r;
1234 static int qemu_debug_requested(void)
1236 int r = debug_requested;
1237 debug_requested = 0;
1238 return r;
1241 static int qemu_vmstop_requested(void)
1243 int r = vmstop_requested;
1244 vmstop_requested = 0;
1245 return r;
1248 void qemu_register_reset(QEMUResetHandler *func, void *opaque)
1250 QEMUResetEntry *re = g_malloc0(sizeof(QEMUResetEntry));
1252 re->func = func;
1253 re->opaque = opaque;
1254 QTAILQ_INSERT_TAIL(&reset_handlers, re, entry);
1257 void qemu_unregister_reset(QEMUResetHandler *func, void *opaque)
1259 QEMUResetEntry *re;
1261 QTAILQ_FOREACH(re, &reset_handlers, entry) {
1262 if (re->func == func && re->opaque == opaque) {
1263 QTAILQ_REMOVE(&reset_handlers, re, entry);
1264 g_free(re);
1265 return;
1270 void qemu_system_reset(bool report)
1272 QEMUResetEntry *re, *nre;
1274 /* reset all devices */
1275 QTAILQ_FOREACH_SAFE(re, &reset_handlers, entry, nre) {
1276 re->func(re->opaque);
1278 if (report) {
1279 monitor_protocol_event(QEVENT_RESET, NULL);
1281 cpu_synchronize_all_post_reset();
1284 void qemu_system_reset_request(void)
1286 if (no_reboot) {
1287 shutdown_requested = 1;
1288 } else {
1289 reset_requested = 1;
1291 cpu_stop_current();
1292 qemu_notify_event();
1295 void qemu_system_killed(int signal, pid_t pid)
1297 shutdown_signal = signal;
1298 shutdown_pid = pid;
1299 qemu_system_shutdown_request();
1302 void qemu_system_shutdown_request(void)
1304 shutdown_requested = 1;
1305 qemu_notify_event();
1308 void qemu_system_powerdown_request(void)
1310 powerdown_requested = 1;
1311 qemu_notify_event();
1314 void qemu_system_debug_request(void)
1316 debug_requested = 1;
1317 qemu_notify_event();
1320 void qemu_system_vmstop_request(int reason)
1322 vmstop_requested = reason;
1323 qemu_notify_event();
1326 static GPollFD poll_fds[1024 * 2]; /* this is probably overkill */
1327 static int n_poll_fds;
1328 static int max_priority;
1330 static void glib_select_fill(int *max_fd, fd_set *rfds, fd_set *wfds,
1331 fd_set *xfds, struct timeval *tv)
1333 GMainContext *context = g_main_context_default();
1334 int i;
1335 int timeout = 0, cur_timeout;
1337 g_main_context_prepare(context, &max_priority);
1339 n_poll_fds = g_main_context_query(context, max_priority, &timeout,
1340 poll_fds, ARRAY_SIZE(poll_fds));
1341 g_assert(n_poll_fds <= ARRAY_SIZE(poll_fds));
1343 for (i = 0; i < n_poll_fds; i++) {
1344 GPollFD *p = &poll_fds[i];
1346 if ((p->events & G_IO_IN)) {
1347 FD_SET(p->fd, rfds);
1348 *max_fd = MAX(*max_fd, p->fd);
1350 if ((p->events & G_IO_OUT)) {
1351 FD_SET(p->fd, wfds);
1352 *max_fd = MAX(*max_fd, p->fd);
1354 if ((p->events & G_IO_ERR)) {
1355 FD_SET(p->fd, xfds);
1356 *max_fd = MAX(*max_fd, p->fd);
1360 cur_timeout = (tv->tv_sec * 1000) + ((tv->tv_usec + 500) / 1000);
1361 if (timeout >= 0 && timeout < cur_timeout) {
1362 tv->tv_sec = timeout / 1000;
1363 tv->tv_usec = (timeout % 1000) * 1000;
1367 static void glib_select_poll(fd_set *rfds, fd_set *wfds, fd_set *xfds,
1368 bool err)
1370 GMainContext *context = g_main_context_default();
1372 if (!err) {
1373 int i;
1375 for (i = 0; i < n_poll_fds; i++) {
1376 GPollFD *p = &poll_fds[i];
1378 if ((p->events & G_IO_IN) && FD_ISSET(p->fd, rfds)) {
1379 p->revents |= G_IO_IN;
1381 if ((p->events & G_IO_OUT) && FD_ISSET(p->fd, wfds)) {
1382 p->revents |= G_IO_OUT;
1384 if ((p->events & G_IO_ERR) && FD_ISSET(p->fd, xfds)) {
1385 p->revents |= G_IO_ERR;
1390 if (g_main_context_check(context, max_priority, poll_fds, n_poll_fds)) {
1391 g_main_context_dispatch(context);
1395 int main_loop_wait(int nonblocking)
1397 fd_set rfds, wfds, xfds;
1398 int ret, nfds;
1399 struct timeval tv;
1400 int timeout;
1402 if (nonblocking)
1403 timeout = 0;
1404 else {
1405 timeout = qemu_calculate_timeout();
1406 qemu_bh_update_timeout(&timeout);
1409 os_host_main_loop_wait(&timeout);
1411 tv.tv_sec = timeout / 1000;
1412 tv.tv_usec = (timeout % 1000) * 1000;
1414 /* poll any events */
1415 /* XXX: separate device handlers from system ones */
1416 nfds = -1;
1417 FD_ZERO(&rfds);
1418 FD_ZERO(&wfds);
1419 FD_ZERO(&xfds);
1421 qemu_iohandler_fill(&nfds, &rfds, &wfds, &xfds);
1422 slirp_select_fill(&nfds, &rfds, &wfds, &xfds);
1423 glib_select_fill(&nfds, &rfds, &wfds, &xfds, &tv);
1425 if (timeout > 0) {
1426 qemu_mutex_unlock_iothread();
1429 ret = select(nfds + 1, &rfds, &wfds, &xfds, &tv);
1431 if (timeout > 0) {
1432 qemu_mutex_lock_iothread();
1435 qemu_iohandler_poll(&rfds, &wfds, &xfds, ret);
1436 slirp_select_poll(&rfds, &wfds, &xfds, (ret < 0));
1437 glib_select_poll(&rfds, &wfds, &xfds, (ret < 0));
1439 qemu_run_all_timers();
1441 /* Check bottom-halves last in case any of the earlier events triggered
1442 them. */
1443 qemu_bh_poll();
1445 return ret;
1448 #ifndef CONFIG_IOTHREAD
1449 static int vm_request_pending(void)
1451 return powerdown_requested ||
1452 reset_requested ||
1453 shutdown_requested ||
1454 debug_requested ||
1455 vmstop_requested;
1457 #endif
1459 qemu_irq qemu_system_powerdown;
1461 static void main_loop(void)
1463 bool nonblocking;
1464 int last_io __attribute__ ((unused)) = 0;
1465 #ifdef CONFIG_PROFILER
1466 int64_t ti;
1467 #endif
1468 int r;
1470 qemu_main_loop_start();
1472 for (;;) {
1473 #ifdef CONFIG_IOTHREAD
1474 nonblocking = !kvm_enabled() && last_io > 0;
1475 #else
1476 nonblocking = cpu_exec_all();
1477 if (vm_request_pending()) {
1478 nonblocking = true;
1480 #endif
1481 #ifdef CONFIG_PROFILER
1482 ti = profile_getclock();
1483 #endif
1484 last_io = main_loop_wait(nonblocking);
1485 #ifdef CONFIG_PROFILER
1486 dev_time += profile_getclock() - ti;
1487 #endif
1489 if (qemu_debug_requested()) {
1490 vm_stop(VMSTOP_DEBUG);
1492 if (qemu_shutdown_requested()) {
1493 qemu_kill_report();
1494 monitor_protocol_event(QEVENT_SHUTDOWN, NULL);
1495 if (no_shutdown) {
1496 vm_stop(VMSTOP_SHUTDOWN);
1497 } else
1498 break;
1500 if (qemu_reset_requested()) {
1501 pause_all_vcpus();
1502 cpu_synchronize_all_states();
1503 qemu_system_reset(VMRESET_REPORT);
1504 resume_all_vcpus();
1506 if (qemu_powerdown_requested()) {
1507 monitor_protocol_event(QEVENT_POWERDOWN, NULL);
1508 qemu_irq_raise(qemu_system_powerdown);
1510 if ((r = qemu_vmstop_requested())) {
1511 vm_stop(r);
1514 bdrv_close_all();
1515 pause_all_vcpus();
1518 static void version(void)
1520 printf("QEMU emulator version " QEMU_VERSION QEMU_PKGVERSION ", Copyright (c) 2003-2008 Fabrice Bellard\n");
1523 static void help(int exitcode)
1525 const char *options_help =
1526 #define DEF(option, opt_arg, opt_enum, opt_help, arch_mask) \
1527 opt_help
1528 #define DEFHEADING(text) stringify(text) "\n"
1529 #include "qemu-options.def"
1530 #undef DEF
1531 #undef DEFHEADING
1532 #undef GEN_DOCS
1534 version();
1535 printf("usage: %s [options] [disk_image]\n"
1536 "\n"
1537 "'disk_image' is a raw hard disk image for IDE hard disk 0\n"
1538 "\n"
1539 "%s\n"
1540 "During emulation, the following keys are useful:\n"
1541 "ctrl-alt-f toggle full screen\n"
1542 "ctrl-alt-n switch to virtual console 'n'\n"
1543 "ctrl-alt toggle mouse and keyboard grab\n"
1544 "\n"
1545 "When using -nographic, press 'ctrl-a h' to get some help.\n",
1546 "qemu",
1547 options_help);
1548 exit(exitcode);
1551 #define HAS_ARG 0x0001
1553 typedef struct QEMUOption {
1554 const char *name;
1555 int flags;
1556 int index;
1557 uint32_t arch_mask;
1558 } QEMUOption;
1560 static const QEMUOption qemu_options[] = {
1561 { "h", 0, QEMU_OPTION_h, QEMU_ARCH_ALL },
1562 #define DEF(option, opt_arg, opt_enum, opt_help, arch_mask) \
1563 { option, opt_arg, opt_enum, arch_mask },
1564 #define DEFHEADING(text)
1565 #include "qemu-options.def"
1566 #undef DEF
1567 #undef DEFHEADING
1568 #undef GEN_DOCS
1569 { NULL },
1571 static void select_vgahw (const char *p)
1573 const char *opts;
1575 default_vga = 0;
1576 vga_interface_type = VGA_NONE;
1577 if (strstart(p, "std", &opts)) {
1578 vga_interface_type = VGA_STD;
1579 } else if (strstart(p, "cirrus", &opts)) {
1580 vga_interface_type = VGA_CIRRUS;
1581 } else if (strstart(p, "vmware", &opts)) {
1582 vga_interface_type = VGA_VMWARE;
1583 } else if (strstart(p, "xenfb", &opts)) {
1584 vga_interface_type = VGA_XENFB;
1585 } else if (strstart(p, "qxl", &opts)) {
1586 vga_interface_type = VGA_QXL;
1587 } else if (!strstart(p, "none", &opts)) {
1588 invalid_vga:
1589 fprintf(stderr, "Unknown vga type: %s\n", p);
1590 exit(1);
1592 while (*opts) {
1593 const char *nextopt;
1595 if (strstart(opts, ",retrace=", &nextopt)) {
1596 opts = nextopt;
1597 if (strstart(opts, "dumb", &nextopt))
1598 vga_retrace_method = VGA_RETRACE_DUMB;
1599 else if (strstart(opts, "precise", &nextopt))
1600 vga_retrace_method = VGA_RETRACE_PRECISE;
1601 else goto invalid_vga;
1602 } else goto invalid_vga;
1603 opts = nextopt;
1607 static DisplayType select_display(const char *p)
1609 const char *opts;
1610 DisplayType display = DT_DEFAULT;
1612 if (strstart(p, "sdl", &opts)) {
1613 #ifdef CONFIG_SDL
1614 display = DT_SDL;
1615 while (*opts) {
1616 const char *nextopt;
1618 if (strstart(opts, ",frame=", &nextopt)) {
1619 opts = nextopt;
1620 if (strstart(opts, "on", &nextopt)) {
1621 no_frame = 0;
1622 } else if (strstart(opts, "off", &nextopt)) {
1623 no_frame = 1;
1624 } else {
1625 goto invalid_sdl_args;
1627 } else if (strstart(opts, ",alt_grab=", &nextopt)) {
1628 opts = nextopt;
1629 if (strstart(opts, "on", &nextopt)) {
1630 alt_grab = 1;
1631 } else if (strstart(opts, "off", &nextopt)) {
1632 alt_grab = 0;
1633 } else {
1634 goto invalid_sdl_args;
1636 } else if (strstart(opts, ",ctrl_grab=", &nextopt)) {
1637 opts = nextopt;
1638 if (strstart(opts, "on", &nextopt)) {
1639 ctrl_grab = 1;
1640 } else if (strstart(opts, "off", &nextopt)) {
1641 ctrl_grab = 0;
1642 } else {
1643 goto invalid_sdl_args;
1645 } else if (strstart(opts, ",window_close=", &nextopt)) {
1646 opts = nextopt;
1647 if (strstart(opts, "on", &nextopt)) {
1648 no_quit = 0;
1649 } else if (strstart(opts, "off", &nextopt)) {
1650 no_quit = 1;
1651 } else {
1652 goto invalid_sdl_args;
1654 } else {
1655 invalid_sdl_args:
1656 fprintf(stderr, "Invalid SDL option string: %s\n", p);
1657 exit(1);
1659 opts = nextopt;
1661 #else
1662 fprintf(stderr, "SDL support is disabled\n");
1663 exit(1);
1664 #endif
1665 } else if (strstart(p, "vnc", &opts)) {
1666 #ifdef CONFIG_VNC
1667 display_remote++;
1669 if (*opts) {
1670 const char *nextopt;
1672 if (strstart(opts, "=", &nextopt)) {
1673 vnc_display = nextopt;
1676 if (!vnc_display) {
1677 fprintf(stderr, "VNC requires a display argument vnc=<display>\n");
1678 exit(1);
1680 #else
1681 fprintf(stderr, "VNC support is disabled\n");
1682 exit(1);
1683 #endif
1684 } else if (strstart(p, "curses", &opts)) {
1685 #ifdef CONFIG_CURSES
1686 display = DT_CURSES;
1687 #else
1688 fprintf(stderr, "Curses support is disabled\n");
1689 exit(1);
1690 #endif
1691 } else if (strstart(p, "none", &opts)) {
1692 display = DT_NONE;
1693 } else {
1694 fprintf(stderr, "Unknown display type: %s\n", p);
1695 exit(1);
1698 return display;
1701 static int balloon_parse(const char *arg)
1703 QemuOpts *opts;
1705 if (strcmp(arg, "none") == 0) {
1706 return 0;
1709 if (!strncmp(arg, "virtio", 6)) {
1710 if (arg[6] == ',') {
1711 /* have params -> parse them */
1712 opts = qemu_opts_parse(qemu_find_opts("device"), arg+7, 0);
1713 if (!opts)
1714 return -1;
1715 } else {
1716 /* create empty opts */
1717 opts = qemu_opts_create(qemu_find_opts("device"), NULL, 0);
1719 qemu_opt_set(opts, "driver", "virtio-balloon");
1720 return 0;
1723 return -1;
1726 char *qemu_find_file(int type, const char *name)
1728 int len;
1729 const char *subdir;
1730 char *buf;
1732 /* If name contains path separators then try it as a straight path. */
1733 if ((strchr(name, '/') || strchr(name, '\\'))
1734 && access(name, R_OK) == 0) {
1735 return g_strdup(name);
1737 switch (type) {
1738 case QEMU_FILE_TYPE_BIOS:
1739 subdir = "";
1740 break;
1741 case QEMU_FILE_TYPE_KEYMAP:
1742 subdir = "keymaps/";
1743 break;
1744 default:
1745 abort();
1747 len = strlen(data_dir) + strlen(name) + strlen(subdir) + 2;
1748 buf = g_malloc0(len);
1749 snprintf(buf, len, "%s/%s%s", data_dir, subdir, name);
1750 if (access(buf, R_OK)) {
1751 g_free(buf);
1752 return NULL;
1754 return buf;
1757 static int device_help_func(QemuOpts *opts, void *opaque)
1759 return qdev_device_help(opts);
1762 static int device_init_func(QemuOpts *opts, void *opaque)
1764 DeviceState *dev;
1766 dev = qdev_device_add(opts);
1767 if (!dev)
1768 return -1;
1769 return 0;
1772 static int chardev_init_func(QemuOpts *opts, void *opaque)
1774 CharDriverState *chr;
1776 chr = qemu_chr_new_from_opts(opts, NULL);
1777 if (!chr)
1778 return -1;
1779 return 0;
1782 #ifdef CONFIG_VIRTFS
1783 static int fsdev_init_func(QemuOpts *opts, void *opaque)
1785 int ret;
1786 ret = qemu_fsdev_add(opts);
1788 return ret;
1790 #endif
1792 static int mon_init_func(QemuOpts *opts, void *opaque)
1794 CharDriverState *chr;
1795 const char *chardev;
1796 const char *mode;
1797 int flags;
1799 mode = qemu_opt_get(opts, "mode");
1800 if (mode == NULL) {
1801 mode = "readline";
1803 if (strcmp(mode, "readline") == 0) {
1804 flags = MONITOR_USE_READLINE;
1805 } else if (strcmp(mode, "control") == 0) {
1806 flags = MONITOR_USE_CONTROL;
1807 } else {
1808 fprintf(stderr, "unknown monitor mode \"%s\"\n", mode);
1809 exit(1);
1812 if (qemu_opt_get_bool(opts, "pretty", 0))
1813 flags |= MONITOR_USE_PRETTY;
1815 if (qemu_opt_get_bool(opts, "default", 0))
1816 flags |= MONITOR_IS_DEFAULT;
1818 chardev = qemu_opt_get(opts, "chardev");
1819 chr = qemu_chr_find(chardev);
1820 if (chr == NULL) {
1821 fprintf(stderr, "chardev \"%s\" not found\n", chardev);
1822 exit(1);
1825 monitor_init(chr, flags);
1826 return 0;
1829 static void monitor_parse(const char *optarg, const char *mode)
1831 static int monitor_device_index = 0;
1832 QemuOpts *opts;
1833 const char *p;
1834 char label[32];
1835 int def = 0;
1837 if (strstart(optarg, "chardev:", &p)) {
1838 snprintf(label, sizeof(label), "%s", p);
1839 } else {
1840 snprintf(label, sizeof(label), "compat_monitor%d",
1841 monitor_device_index);
1842 if (monitor_device_index == 0) {
1843 def = 1;
1845 opts = qemu_chr_parse_compat(label, optarg);
1846 if (!opts) {
1847 fprintf(stderr, "parse error: %s\n", optarg);
1848 exit(1);
1852 opts = qemu_opts_create(qemu_find_opts("mon"), label, 1);
1853 if (!opts) {
1854 fprintf(stderr, "duplicate chardev: %s\n", label);
1855 exit(1);
1857 qemu_opt_set(opts, "mode", mode);
1858 qemu_opt_set(opts, "chardev", label);
1859 if (def)
1860 qemu_opt_set(opts, "default", "on");
1861 monitor_device_index++;
1864 struct device_config {
1865 enum {
1866 DEV_USB, /* -usbdevice */
1867 DEV_BT, /* -bt */
1868 DEV_SERIAL, /* -serial */
1869 DEV_PARALLEL, /* -parallel */
1870 DEV_VIRTCON, /* -virtioconsole */
1871 DEV_DEBUGCON, /* -debugcon */
1872 } type;
1873 const char *cmdline;
1874 QTAILQ_ENTRY(device_config) next;
1876 QTAILQ_HEAD(, device_config) device_configs = QTAILQ_HEAD_INITIALIZER(device_configs);
1878 static void add_device_config(int type, const char *cmdline)
1880 struct device_config *conf;
1882 conf = g_malloc0(sizeof(*conf));
1883 conf->type = type;
1884 conf->cmdline = cmdline;
1885 QTAILQ_INSERT_TAIL(&device_configs, conf, next);
1888 static int foreach_device_config(int type, int (*func)(const char *cmdline))
1890 struct device_config *conf;
1891 int rc;
1893 QTAILQ_FOREACH(conf, &device_configs, next) {
1894 if (conf->type != type)
1895 continue;
1896 rc = func(conf->cmdline);
1897 if (0 != rc)
1898 return rc;
1900 return 0;
1903 static int serial_parse(const char *devname)
1905 static int index = 0;
1906 char label[32];
1908 if (strcmp(devname, "none") == 0)
1909 return 0;
1910 if (index == MAX_SERIAL_PORTS) {
1911 fprintf(stderr, "qemu: too many serial ports\n");
1912 exit(1);
1914 snprintf(label, sizeof(label), "serial%d", index);
1915 serial_hds[index] = qemu_chr_new(label, devname, NULL);
1916 if (!serial_hds[index]) {
1917 fprintf(stderr, "qemu: could not open serial device '%s': %s\n",
1918 devname, strerror(errno));
1919 return -1;
1921 index++;
1922 return 0;
1925 static int parallel_parse(const char *devname)
1927 static int index = 0;
1928 char label[32];
1930 if (strcmp(devname, "none") == 0)
1931 return 0;
1932 if (index == MAX_PARALLEL_PORTS) {
1933 fprintf(stderr, "qemu: too many parallel ports\n");
1934 exit(1);
1936 snprintf(label, sizeof(label), "parallel%d", index);
1937 parallel_hds[index] = qemu_chr_new(label, devname, NULL);
1938 if (!parallel_hds[index]) {
1939 fprintf(stderr, "qemu: could not open parallel device '%s': %s\n",
1940 devname, strerror(errno));
1941 return -1;
1943 index++;
1944 return 0;
1947 static int virtcon_parse(const char *devname)
1949 QemuOptsList *device = qemu_find_opts("device");
1950 static int index = 0;
1951 char label[32];
1952 QemuOpts *bus_opts, *dev_opts;
1954 if (strcmp(devname, "none") == 0)
1955 return 0;
1956 if (index == MAX_VIRTIO_CONSOLES) {
1957 fprintf(stderr, "qemu: too many virtio consoles\n");
1958 exit(1);
1961 bus_opts = qemu_opts_create(device, NULL, 0);
1962 qemu_opt_set(bus_opts, "driver", "virtio-serial");
1964 dev_opts = qemu_opts_create(device, NULL, 0);
1965 qemu_opt_set(dev_opts, "driver", "virtconsole");
1967 snprintf(label, sizeof(label), "virtcon%d", index);
1968 virtcon_hds[index] = qemu_chr_new(label, devname, NULL);
1969 if (!virtcon_hds[index]) {
1970 fprintf(stderr, "qemu: could not open virtio console '%s': %s\n",
1971 devname, strerror(errno));
1972 return -1;
1974 qemu_opt_set(dev_opts, "chardev", label);
1976 index++;
1977 return 0;
1980 static int debugcon_parse(const char *devname)
1982 QemuOpts *opts;
1984 if (!qemu_chr_new("debugcon", devname, NULL)) {
1985 exit(1);
1987 opts = qemu_opts_create(qemu_find_opts("device"), "debugcon", 1);
1988 if (!opts) {
1989 fprintf(stderr, "qemu: already have a debugcon device\n");
1990 exit(1);
1992 qemu_opt_set(opts, "driver", "isa-debugcon");
1993 qemu_opt_set(opts, "chardev", "debugcon");
1994 return 0;
1997 static QEMUMachine *machine_parse(const char *name)
1999 QEMUMachine *m, *machine = NULL;
2001 if (name) {
2002 machine = find_machine(name);
2004 if (machine) {
2005 return machine;
2007 printf("Supported machines are:\n");
2008 for (m = first_machine; m != NULL; m = m->next) {
2009 if (m->alias) {
2010 printf("%-10s %s (alias of %s)\n", m->alias, m->desc, m->name);
2012 printf("%-10s %s%s\n", m->name, m->desc,
2013 m->is_default ? " (default)" : "");
2015 exit(!name || *name != '?');
2018 static int tcg_init(void)
2020 tcg_exec_init(tcg_tb_size * 1024 * 1024);
2021 return 0;
2024 static struct {
2025 const char *opt_name;
2026 const char *name;
2027 int (*available)(void);
2028 int (*init)(void);
2029 int *allowed;
2030 } accel_list[] = {
2031 { "tcg", "tcg", tcg_available, tcg_init, &tcg_allowed },
2032 { "xen", "Xen", xen_available, xen_init, &xen_allowed },
2033 { "kvm", "KVM", kvm_available, kvm_init, &kvm_allowed },
2036 static int configure_accelerator(void)
2038 const char *p = NULL;
2039 char buf[10];
2040 int i, ret;
2041 bool accel_initalised = 0;
2042 bool init_failed = 0;
2044 QemuOptsList *list = qemu_find_opts("machine");
2045 if (!QTAILQ_EMPTY(&list->head)) {
2046 p = qemu_opt_get(QTAILQ_FIRST(&list->head), "accel");
2049 if (p == NULL) {
2050 /* Use the default "accelerator", tcg */
2051 p = "tcg";
2054 while (!accel_initalised && *p != '\0') {
2055 if (*p == ':') {
2056 p++;
2058 p = get_opt_name(buf, sizeof (buf), p, ':');
2059 for (i = 0; i < ARRAY_SIZE(accel_list); i++) {
2060 if (strcmp(accel_list[i].opt_name, buf) == 0) {
2061 *(accel_list[i].allowed) = 1;
2062 ret = accel_list[i].init();
2063 if (ret < 0) {
2064 init_failed = 1;
2065 if (!accel_list[i].available()) {
2066 printf("%s not supported for this target\n",
2067 accel_list[i].name);
2068 } else {
2069 fprintf(stderr, "failed to initialize %s: %s\n",
2070 accel_list[i].name,
2071 strerror(-ret));
2073 *(accel_list[i].allowed) = 0;
2074 } else {
2075 accel_initalised = 1;
2077 break;
2080 if (i == ARRAY_SIZE(accel_list)) {
2081 fprintf(stderr, "\"%s\" accelerator does not exist.\n", buf);
2085 if (!accel_initalised) {
2086 fprintf(stderr, "No accelerator found!\n");
2087 exit(1);
2090 if (init_failed) {
2091 fprintf(stderr, "Back to %s accelerator.\n", accel_list[i].name);
2094 return !accel_initalised;
2097 void qemu_add_exit_notifier(Notifier *notify)
2099 notifier_list_add(&exit_notifiers, notify);
2102 void qemu_remove_exit_notifier(Notifier *notify)
2104 notifier_list_remove(&exit_notifiers, notify);
2107 static void qemu_run_exit_notifiers(void)
2109 notifier_list_notify(&exit_notifiers, NULL);
2112 void qemu_add_machine_init_done_notifier(Notifier *notify)
2114 notifier_list_add(&machine_init_done_notifiers, notify);
2117 static void qemu_run_machine_init_done_notifiers(void)
2119 notifier_list_notify(&machine_init_done_notifiers, NULL);
2122 static const QEMUOption *lookup_opt(int argc, char **argv,
2123 const char **poptarg, int *poptind)
2125 const QEMUOption *popt;
2126 int optind = *poptind;
2127 char *r = argv[optind];
2128 const char *optarg;
2130 loc_set_cmdline(argv, optind, 1);
2131 optind++;
2132 /* Treat --foo the same as -foo. */
2133 if (r[1] == '-')
2134 r++;
2135 popt = qemu_options;
2136 for(;;) {
2137 if (!popt->name) {
2138 error_report("invalid option");
2139 exit(1);
2141 if (!strcmp(popt->name, r + 1))
2142 break;
2143 popt++;
2145 if (popt->flags & HAS_ARG) {
2146 if (optind >= argc) {
2147 error_report("requires an argument");
2148 exit(1);
2150 optarg = argv[optind++];
2151 loc_set_cmdline(argv, optind - 2, 2);
2152 } else {
2153 optarg = NULL;
2156 *poptarg = optarg;
2157 *poptind = optind;
2159 return popt;
2162 static gpointer malloc_and_trace(gsize n_bytes)
2164 void *ptr = malloc(n_bytes);
2165 trace_qemu_malloc(n_bytes, ptr);
2166 return ptr;
2169 static gpointer realloc_and_trace(gpointer mem, gsize n_bytes)
2171 void *ptr = realloc(mem, n_bytes);
2172 trace_qemu_realloc(mem, n_bytes, ptr);
2173 return ptr;
2176 static void free_and_trace(gpointer mem)
2178 trace_qemu_free(mem);
2179 free(mem);
2182 int main(int argc, char **argv, char **envp)
2184 const char *gdbstub_dev = NULL;
2185 int i;
2186 int snapshot, linux_boot;
2187 const char *icount_option = NULL;
2188 const char *initrd_filename;
2189 const char *kernel_filename, *kernel_cmdline;
2190 char boot_devices[33] = "cad"; /* default to HD->floppy->CD-ROM */
2191 DisplayState *ds;
2192 DisplayChangeListener *dcl;
2193 int cyls, heads, secs, translation;
2194 QemuOpts *hda_opts = NULL, *opts;
2195 QemuOptsList *olist;
2196 int optind;
2197 const char *optarg;
2198 const char *loadvm = NULL;
2199 QEMUMachine *machine;
2200 const char *cpu_model;
2201 const char *pid_file = NULL;
2202 const char *incoming = NULL;
2203 #ifdef CONFIG_VNC
2204 int show_vnc_port = 0;
2205 #endif
2206 int defconfig = 1;
2207 const char *trace_file = NULL;
2208 const char *log_mask = NULL;
2209 const char *log_file = NULL;
2210 GMemVTable mem_trace = {
2211 .malloc = malloc_and_trace,
2212 .realloc = realloc_and_trace,
2213 .free = free_and_trace,
2216 atexit(qemu_run_exit_notifiers);
2217 error_set_progname(argv[0]);
2219 g_mem_set_vtable(&mem_trace);
2221 init_clocks();
2223 qemu_cache_utils_init(envp);
2225 QLIST_INIT (&vm_change_state_head);
2226 os_setup_early_signal_handling();
2228 module_call_init(MODULE_INIT_MACHINE);
2229 machine = find_default_machine();
2230 cpu_model = NULL;
2231 initrd_filename = NULL;
2232 ram_size = 0;
2233 snapshot = 0;
2234 kernel_filename = NULL;
2235 kernel_cmdline = "";
2236 cyls = heads = secs = 0;
2237 translation = BIOS_ATA_TRANSLATION_AUTO;
2239 for (i = 0; i < MAX_NODES; i++) {
2240 node_mem[i] = 0;
2241 node_cpumask[i] = 0;
2244 nb_numa_nodes = 0;
2245 nb_nics = 0;
2247 autostart= 1;
2249 /* first pass of option parsing */
2250 optind = 1;
2251 while (optind < argc) {
2252 if (argv[optind][0] != '-') {
2253 /* disk image */
2254 optind++;
2255 continue;
2256 } else {
2257 const QEMUOption *popt;
2259 popt = lookup_opt(argc, argv, &optarg, &optind);
2260 switch (popt->index) {
2261 case QEMU_OPTION_nodefconfig:
2262 defconfig=0;
2263 break;
2268 if (defconfig) {
2269 int ret;
2271 ret = qemu_read_config_file(CONFIG_QEMU_CONFDIR "/qemu.conf");
2272 if (ret < 0 && ret != -ENOENT) {
2273 exit(1);
2276 ret = qemu_read_config_file(arch_config_name);
2277 if (ret < 0 && ret != -ENOENT) {
2278 exit(1);
2281 cpudef_init();
2283 /* second pass of option parsing */
2284 optind = 1;
2285 for(;;) {
2286 if (optind >= argc)
2287 break;
2288 if (argv[optind][0] != '-') {
2289 hda_opts = drive_add(IF_DEFAULT, 0, argv[optind++], HD_OPTS);
2290 } else {
2291 const QEMUOption *popt;
2293 popt = lookup_opt(argc, argv, &optarg, &optind);
2294 if (!(popt->arch_mask & arch_type)) {
2295 printf("Option %s not supported for this target\n", popt->name);
2296 exit(1);
2298 switch(popt->index) {
2299 case QEMU_OPTION_M:
2300 machine = machine_parse(optarg);
2301 break;
2302 case QEMU_OPTION_cpu:
2303 /* hw initialization will check this */
2304 if (*optarg == '?') {
2305 list_cpus(stdout, &fprintf, optarg);
2306 exit(0);
2307 } else {
2308 cpu_model = optarg;
2310 break;
2311 case QEMU_OPTION_initrd:
2312 initrd_filename = optarg;
2313 break;
2314 case QEMU_OPTION_hda:
2316 char buf[256];
2317 if (cyls == 0)
2318 snprintf(buf, sizeof(buf), "%s", HD_OPTS);
2319 else
2320 snprintf(buf, sizeof(buf),
2321 "%s,cyls=%d,heads=%d,secs=%d%s",
2322 HD_OPTS , cyls, heads, secs,
2323 translation == BIOS_ATA_TRANSLATION_LBA ?
2324 ",trans=lba" :
2325 translation == BIOS_ATA_TRANSLATION_NONE ?
2326 ",trans=none" : "");
2327 drive_add(IF_DEFAULT, 0, optarg, buf);
2328 break;
2330 case QEMU_OPTION_hdb:
2331 case QEMU_OPTION_hdc:
2332 case QEMU_OPTION_hdd:
2333 drive_add(IF_DEFAULT, popt->index - QEMU_OPTION_hda, optarg,
2334 HD_OPTS);
2335 break;
2336 case QEMU_OPTION_drive:
2337 if (drive_def(optarg) == NULL) {
2338 exit(1);
2340 break;
2341 case QEMU_OPTION_set:
2342 if (qemu_set_option(optarg) != 0)
2343 exit(1);
2344 break;
2345 case QEMU_OPTION_global:
2346 if (qemu_global_option(optarg) != 0)
2347 exit(1);
2348 break;
2349 case QEMU_OPTION_mtdblock:
2350 drive_add(IF_MTD, -1, optarg, MTD_OPTS);
2351 break;
2352 case QEMU_OPTION_sd:
2353 drive_add(IF_SD, 0, optarg, SD_OPTS);
2354 break;
2355 case QEMU_OPTION_pflash:
2356 drive_add(IF_PFLASH, -1, optarg, PFLASH_OPTS);
2357 break;
2358 case QEMU_OPTION_snapshot:
2359 snapshot = 1;
2360 break;
2361 case QEMU_OPTION_hdachs:
2363 const char *p;
2364 p = optarg;
2365 cyls = strtol(p, (char **)&p, 0);
2366 if (cyls < 1 || cyls > 16383)
2367 goto chs_fail;
2368 if (*p != ',')
2369 goto chs_fail;
2370 p++;
2371 heads = strtol(p, (char **)&p, 0);
2372 if (heads < 1 || heads > 16)
2373 goto chs_fail;
2374 if (*p != ',')
2375 goto chs_fail;
2376 p++;
2377 secs = strtol(p, (char **)&p, 0);
2378 if (secs < 1 || secs > 63)
2379 goto chs_fail;
2380 if (*p == ',') {
2381 p++;
2382 if (!strcmp(p, "none"))
2383 translation = BIOS_ATA_TRANSLATION_NONE;
2384 else if (!strcmp(p, "lba"))
2385 translation = BIOS_ATA_TRANSLATION_LBA;
2386 else if (!strcmp(p, "auto"))
2387 translation = BIOS_ATA_TRANSLATION_AUTO;
2388 else
2389 goto chs_fail;
2390 } else if (*p != '\0') {
2391 chs_fail:
2392 fprintf(stderr, "qemu: invalid physical CHS format\n");
2393 exit(1);
2395 if (hda_opts != NULL) {
2396 char num[16];
2397 snprintf(num, sizeof(num), "%d", cyls);
2398 qemu_opt_set(hda_opts, "cyls", num);
2399 snprintf(num, sizeof(num), "%d", heads);
2400 qemu_opt_set(hda_opts, "heads", num);
2401 snprintf(num, sizeof(num), "%d", secs);
2402 qemu_opt_set(hda_opts, "secs", num);
2403 if (translation == BIOS_ATA_TRANSLATION_LBA)
2404 qemu_opt_set(hda_opts, "trans", "lba");
2405 if (translation == BIOS_ATA_TRANSLATION_NONE)
2406 qemu_opt_set(hda_opts, "trans", "none");
2409 break;
2410 case QEMU_OPTION_numa:
2411 if (nb_numa_nodes >= MAX_NODES) {
2412 fprintf(stderr, "qemu: too many NUMA nodes\n");
2413 exit(1);
2415 numa_add(optarg);
2416 break;
2417 case QEMU_OPTION_display:
2418 display_type = select_display(optarg);
2419 break;
2420 case QEMU_OPTION_nographic:
2421 display_type = DT_NOGRAPHIC;
2422 break;
2423 case QEMU_OPTION_curses:
2424 #ifdef CONFIG_CURSES
2425 display_type = DT_CURSES;
2426 #else
2427 fprintf(stderr, "Curses support is disabled\n");
2428 exit(1);
2429 #endif
2430 break;
2431 case QEMU_OPTION_portrait:
2432 graphic_rotate = 90;
2433 break;
2434 case QEMU_OPTION_rotate:
2435 graphic_rotate = strtol(optarg, (char **) &optarg, 10);
2436 if (graphic_rotate != 0 && graphic_rotate != 90 &&
2437 graphic_rotate != 180 && graphic_rotate != 270) {
2438 fprintf(stderr,
2439 "qemu: only 90, 180, 270 deg rotation is available\n");
2440 exit(1);
2442 break;
2443 case QEMU_OPTION_kernel:
2444 kernel_filename = optarg;
2445 break;
2446 case QEMU_OPTION_append:
2447 kernel_cmdline = optarg;
2448 break;
2449 case QEMU_OPTION_cdrom:
2450 drive_add(IF_DEFAULT, 2, optarg, CDROM_OPTS);
2451 break;
2452 case QEMU_OPTION_boot:
2454 static const char * const params[] = {
2455 "order", "once", "menu",
2456 "splash", "splash-time", NULL
2458 char buf[sizeof(boot_devices)];
2459 char *standard_boot_devices;
2460 int legacy = 0;
2462 if (!strchr(optarg, '=')) {
2463 legacy = 1;
2464 pstrcpy(buf, sizeof(buf), optarg);
2465 } else if (check_params(buf, sizeof(buf), params, optarg) < 0) {
2466 fprintf(stderr,
2467 "qemu: unknown boot parameter '%s' in '%s'\n",
2468 buf, optarg);
2469 exit(1);
2472 if (legacy ||
2473 get_param_value(buf, sizeof(buf), "order", optarg)) {
2474 validate_bootdevices(buf);
2475 pstrcpy(boot_devices, sizeof(boot_devices), buf);
2477 if (!legacy) {
2478 if (get_param_value(buf, sizeof(buf),
2479 "once", optarg)) {
2480 validate_bootdevices(buf);
2481 standard_boot_devices = g_strdup(boot_devices);
2482 pstrcpy(boot_devices, sizeof(boot_devices), buf);
2483 qemu_register_reset(restore_boot_devices,
2484 standard_boot_devices);
2486 if (get_param_value(buf, sizeof(buf),
2487 "menu", optarg)) {
2488 if (!strcmp(buf, "on")) {
2489 boot_menu = 1;
2490 } else if (!strcmp(buf, "off")) {
2491 boot_menu = 0;
2492 } else {
2493 fprintf(stderr,
2494 "qemu: invalid option value '%s'\n",
2495 buf);
2496 exit(1);
2499 qemu_opts_parse(qemu_find_opts("boot-opts"),
2500 optarg, 0);
2503 break;
2504 case QEMU_OPTION_fda:
2505 case QEMU_OPTION_fdb:
2506 drive_add(IF_FLOPPY, popt->index - QEMU_OPTION_fda,
2507 optarg, FD_OPTS);
2508 break;
2509 case QEMU_OPTION_no_fd_bootchk:
2510 fd_bootchk = 0;
2511 break;
2512 case QEMU_OPTION_netdev:
2513 if (net_client_parse(qemu_find_opts("netdev"), optarg) == -1) {
2514 exit(1);
2516 break;
2517 case QEMU_OPTION_net:
2518 if (net_client_parse(qemu_find_opts("net"), optarg) == -1) {
2519 exit(1);
2521 break;
2522 #ifdef CONFIG_SLIRP
2523 case QEMU_OPTION_tftp:
2524 legacy_tftp_prefix = optarg;
2525 break;
2526 case QEMU_OPTION_bootp:
2527 legacy_bootp_filename = optarg;
2528 break;
2529 case QEMU_OPTION_redir:
2530 if (net_slirp_redir(optarg) < 0)
2531 exit(1);
2532 break;
2533 #endif
2534 case QEMU_OPTION_bt:
2535 add_device_config(DEV_BT, optarg);
2536 break;
2537 case QEMU_OPTION_audio_help:
2538 if (!(audio_available())) {
2539 printf("Option %s not supported for this target\n", popt->name);
2540 exit(1);
2542 AUD_help ();
2543 exit (0);
2544 break;
2545 case QEMU_OPTION_soundhw:
2546 if (!(audio_available())) {
2547 printf("Option %s not supported for this target\n", popt->name);
2548 exit(1);
2550 select_soundhw (optarg);
2551 break;
2552 case QEMU_OPTION_h:
2553 help(0);
2554 break;
2555 case QEMU_OPTION_version:
2556 version();
2557 exit(0);
2558 break;
2559 case QEMU_OPTION_m: {
2560 int64_t value;
2562 value = strtosz(optarg, NULL);
2563 if (value < 0) {
2564 fprintf(stderr, "qemu: invalid ram size: %s\n", optarg);
2565 exit(1);
2568 if (value != (uint64_t)(ram_addr_t)value) {
2569 fprintf(stderr, "qemu: ram size too large\n");
2570 exit(1);
2572 ram_size = value;
2573 break;
2575 case QEMU_OPTION_mempath:
2576 mem_path = optarg;
2577 break;
2578 #ifdef MAP_POPULATE
2579 case QEMU_OPTION_mem_prealloc:
2580 mem_prealloc = 1;
2581 break;
2582 #endif
2583 case QEMU_OPTION_d:
2584 log_mask = optarg;
2585 break;
2586 case QEMU_OPTION_D:
2587 log_file = optarg;
2588 break;
2589 case QEMU_OPTION_s:
2590 gdbstub_dev = "tcp::" DEFAULT_GDBSTUB_PORT;
2591 break;
2592 case QEMU_OPTION_gdb:
2593 gdbstub_dev = optarg;
2594 break;
2595 case QEMU_OPTION_L:
2596 data_dir = optarg;
2597 break;
2598 case QEMU_OPTION_bios:
2599 bios_name = optarg;
2600 break;
2601 case QEMU_OPTION_singlestep:
2602 singlestep = 1;
2603 break;
2604 case QEMU_OPTION_S:
2605 autostart = 0;
2606 break;
2607 case QEMU_OPTION_k:
2608 keyboard_layout = optarg;
2609 break;
2610 case QEMU_OPTION_localtime:
2611 rtc_utc = 0;
2612 break;
2613 case QEMU_OPTION_vga:
2614 select_vgahw (optarg);
2615 break;
2616 case QEMU_OPTION_g:
2618 const char *p;
2619 int w, h, depth;
2620 p = optarg;
2621 w = strtol(p, (char **)&p, 10);
2622 if (w <= 0) {
2623 graphic_error:
2624 fprintf(stderr, "qemu: invalid resolution or depth\n");
2625 exit(1);
2627 if (*p != 'x')
2628 goto graphic_error;
2629 p++;
2630 h = strtol(p, (char **)&p, 10);
2631 if (h <= 0)
2632 goto graphic_error;
2633 if (*p == 'x') {
2634 p++;
2635 depth = strtol(p, (char **)&p, 10);
2636 if (depth != 8 && depth != 15 && depth != 16 &&
2637 depth != 24 && depth != 32)
2638 goto graphic_error;
2639 } else if (*p == '\0') {
2640 depth = graphic_depth;
2641 } else {
2642 goto graphic_error;
2645 graphic_width = w;
2646 graphic_height = h;
2647 graphic_depth = depth;
2649 break;
2650 case QEMU_OPTION_echr:
2652 char *r;
2653 term_escape_char = strtol(optarg, &r, 0);
2654 if (r == optarg)
2655 printf("Bad argument to echr\n");
2656 break;
2658 case QEMU_OPTION_monitor:
2659 monitor_parse(optarg, "readline");
2660 default_monitor = 0;
2661 break;
2662 case QEMU_OPTION_qmp:
2663 monitor_parse(optarg, "control");
2664 default_monitor = 0;
2665 break;
2666 case QEMU_OPTION_mon:
2667 opts = qemu_opts_parse(qemu_find_opts("mon"), optarg, 1);
2668 if (!opts) {
2669 exit(1);
2671 default_monitor = 0;
2672 break;
2673 case QEMU_OPTION_chardev:
2674 opts = qemu_opts_parse(qemu_find_opts("chardev"), optarg, 1);
2675 if (!opts) {
2676 exit(1);
2678 break;
2679 case QEMU_OPTION_fsdev:
2680 olist = qemu_find_opts("fsdev");
2681 if (!olist) {
2682 fprintf(stderr, "fsdev is not supported by this qemu build.\n");
2683 exit(1);
2685 opts = qemu_opts_parse(olist, optarg, 1);
2686 if (!opts) {
2687 fprintf(stderr, "parse error: %s\n", optarg);
2688 exit(1);
2690 break;
2691 case QEMU_OPTION_virtfs: {
2692 QemuOpts *fsdev;
2693 QemuOpts *device;
2695 olist = qemu_find_opts("virtfs");
2696 if (!olist) {
2697 fprintf(stderr, "virtfs is not supported by this qemu build.\n");
2698 exit(1);
2700 opts = qemu_opts_parse(olist, optarg, 1);
2701 if (!opts) {
2702 fprintf(stderr, "parse error: %s\n", optarg);
2703 exit(1);
2706 if (qemu_opt_get(opts, "fstype") == NULL ||
2707 qemu_opt_get(opts, "mount_tag") == NULL ||
2708 qemu_opt_get(opts, "path") == NULL ||
2709 qemu_opt_get(opts, "security_model") == NULL) {
2710 fprintf(stderr, "Usage: -virtfs fstype,path=/share_path/,"
2711 "security_model=[mapped|passthrough|none],"
2712 "mount_tag=tag.\n");
2713 exit(1);
2716 fsdev = qemu_opts_create(qemu_find_opts("fsdev"),
2717 qemu_opt_get(opts, "mount_tag"), 1);
2718 if (!fsdev) {
2719 fprintf(stderr, "duplicate fsdev id: %s\n",
2720 qemu_opt_get(opts, "mount_tag"));
2721 exit(1);
2723 qemu_opt_set(fsdev, "fstype", qemu_opt_get(opts, "fstype"));
2724 qemu_opt_set(fsdev, "path", qemu_opt_get(opts, "path"));
2725 qemu_opt_set(fsdev, "security_model",
2726 qemu_opt_get(opts, "security_model"));
2728 device = qemu_opts_create(qemu_find_opts("device"), NULL, 0);
2729 qemu_opt_set(device, "driver", "virtio-9p-pci");
2730 qemu_opt_set(device, "fsdev",
2731 qemu_opt_get(opts, "mount_tag"));
2732 qemu_opt_set(device, "mount_tag",
2733 qemu_opt_get(opts, "mount_tag"));
2734 break;
2736 case QEMU_OPTION_serial:
2737 add_device_config(DEV_SERIAL, optarg);
2738 default_serial = 0;
2739 if (strncmp(optarg, "mon:", 4) == 0) {
2740 default_monitor = 0;
2742 break;
2743 case QEMU_OPTION_watchdog:
2744 if (watchdog) {
2745 fprintf(stderr,
2746 "qemu: only one watchdog option may be given\n");
2747 return 1;
2749 watchdog = optarg;
2750 break;
2751 case QEMU_OPTION_watchdog_action:
2752 if (select_watchdog_action(optarg) == -1) {
2753 fprintf(stderr, "Unknown -watchdog-action parameter\n");
2754 exit(1);
2756 break;
2757 case QEMU_OPTION_virtiocon:
2758 add_device_config(DEV_VIRTCON, optarg);
2759 default_virtcon = 0;
2760 if (strncmp(optarg, "mon:", 4) == 0) {
2761 default_monitor = 0;
2763 break;
2764 case QEMU_OPTION_parallel:
2765 add_device_config(DEV_PARALLEL, optarg);
2766 default_parallel = 0;
2767 if (strncmp(optarg, "mon:", 4) == 0) {
2768 default_monitor = 0;
2770 break;
2771 case QEMU_OPTION_debugcon:
2772 add_device_config(DEV_DEBUGCON, optarg);
2773 break;
2774 case QEMU_OPTION_loadvm:
2775 loadvm = optarg;
2776 break;
2777 case QEMU_OPTION_full_screen:
2778 full_screen = 1;
2779 break;
2780 #ifdef CONFIG_SDL
2781 case QEMU_OPTION_no_frame:
2782 no_frame = 1;
2783 break;
2784 case QEMU_OPTION_alt_grab:
2785 alt_grab = 1;
2786 break;
2787 case QEMU_OPTION_ctrl_grab:
2788 ctrl_grab = 1;
2789 break;
2790 case QEMU_OPTION_no_quit:
2791 no_quit = 1;
2792 break;
2793 case QEMU_OPTION_sdl:
2794 display_type = DT_SDL;
2795 break;
2796 #else
2797 case QEMU_OPTION_no_frame:
2798 case QEMU_OPTION_alt_grab:
2799 case QEMU_OPTION_ctrl_grab:
2800 case QEMU_OPTION_no_quit:
2801 case QEMU_OPTION_sdl:
2802 fprintf(stderr, "SDL support is disabled\n");
2803 exit(1);
2804 #endif
2805 case QEMU_OPTION_pidfile:
2806 pid_file = optarg;
2807 break;
2808 case QEMU_OPTION_win2k_hack:
2809 win2k_install_hack = 1;
2810 break;
2811 case QEMU_OPTION_rtc_td_hack:
2812 rtc_td_hack = 1;
2813 break;
2814 case QEMU_OPTION_acpitable:
2815 do_acpitable_option(optarg);
2816 break;
2817 case QEMU_OPTION_smbios:
2818 do_smbios_option(optarg);
2819 break;
2820 case QEMU_OPTION_enable_kvm:
2821 olist = qemu_find_opts("machine");
2822 qemu_opts_reset(olist);
2823 qemu_opts_parse(olist, "accel=kvm", 0);
2824 break;
2825 case QEMU_OPTION_machine:
2826 olist = qemu_find_opts("machine");
2827 qemu_opts_reset(olist);
2828 opts = qemu_opts_parse(olist, optarg, 1);
2829 if (!opts) {
2830 fprintf(stderr, "parse error: %s\n", optarg);
2831 exit(1);
2833 optarg = qemu_opt_get(opts, "type");
2834 if (optarg) {
2835 machine = machine_parse(optarg);
2837 break;
2838 case QEMU_OPTION_usb:
2839 usb_enabled = 1;
2840 break;
2841 case QEMU_OPTION_usbdevice:
2842 usb_enabled = 1;
2843 add_device_config(DEV_USB, optarg);
2844 break;
2845 case QEMU_OPTION_device:
2846 if (!qemu_opts_parse(qemu_find_opts("device"), optarg, 1)) {
2847 exit(1);
2849 break;
2850 case QEMU_OPTION_smp:
2851 smp_parse(optarg);
2852 if (smp_cpus < 1) {
2853 fprintf(stderr, "Invalid number of CPUs\n");
2854 exit(1);
2856 if (max_cpus < smp_cpus) {
2857 fprintf(stderr, "maxcpus must be equal to or greater than "
2858 "smp\n");
2859 exit(1);
2861 if (max_cpus > 255) {
2862 fprintf(stderr, "Unsupported number of maxcpus\n");
2863 exit(1);
2865 break;
2866 case QEMU_OPTION_vnc:
2867 #ifdef CONFIG_VNC
2868 display_remote++;
2869 vnc_display = optarg;
2870 #else
2871 fprintf(stderr, "VNC support is disabled\n");
2872 exit(1);
2873 #endif
2874 break;
2875 case QEMU_OPTION_no_acpi:
2876 acpi_enabled = 0;
2877 break;
2878 case QEMU_OPTION_no_hpet:
2879 no_hpet = 1;
2880 break;
2881 case QEMU_OPTION_balloon:
2882 if (balloon_parse(optarg) < 0) {
2883 fprintf(stderr, "Unknown -balloon argument %s\n", optarg);
2884 exit(1);
2886 break;
2887 case QEMU_OPTION_no_reboot:
2888 no_reboot = 1;
2889 break;
2890 case QEMU_OPTION_no_shutdown:
2891 no_shutdown = 1;
2892 break;
2893 case QEMU_OPTION_show_cursor:
2894 cursor_hide = 0;
2895 break;
2896 case QEMU_OPTION_uuid:
2897 if(qemu_uuid_parse(optarg, qemu_uuid) < 0) {
2898 fprintf(stderr, "Fail to parse UUID string."
2899 " Wrong format.\n");
2900 exit(1);
2902 break;
2903 case QEMU_OPTION_option_rom:
2904 if (nb_option_roms >= MAX_OPTION_ROMS) {
2905 fprintf(stderr, "Too many option ROMs\n");
2906 exit(1);
2908 opts = qemu_opts_parse(qemu_find_opts("option-rom"), optarg, 1);
2909 option_rom[nb_option_roms].name = qemu_opt_get(opts, "romfile");
2910 option_rom[nb_option_roms].bootindex =
2911 qemu_opt_get_number(opts, "bootindex", -1);
2912 if (!option_rom[nb_option_roms].name) {
2913 fprintf(stderr, "Option ROM file is not specified\n");
2914 exit(1);
2916 nb_option_roms++;
2917 break;
2918 case QEMU_OPTION_semihosting:
2919 semihosting_enabled = 1;
2920 break;
2921 case QEMU_OPTION_name:
2922 qemu_name = g_strdup(optarg);
2924 char *p = strchr(qemu_name, ',');
2925 if (p != NULL) {
2926 *p++ = 0;
2927 if (strncmp(p, "process=", 8)) {
2928 fprintf(stderr, "Unknown subargument %s to -name\n", p);
2929 exit(1);
2931 p += 8;
2932 os_set_proc_name(p);
2935 break;
2936 case QEMU_OPTION_prom_env:
2937 if (nb_prom_envs >= MAX_PROM_ENVS) {
2938 fprintf(stderr, "Too many prom variables\n");
2939 exit(1);
2941 prom_envs[nb_prom_envs] = optarg;
2942 nb_prom_envs++;
2943 break;
2944 case QEMU_OPTION_old_param:
2945 old_param = 1;
2946 break;
2947 case QEMU_OPTION_clock:
2948 configure_alarms(optarg);
2949 break;
2950 case QEMU_OPTION_startdate:
2951 configure_rtc_date_offset(optarg, 1);
2952 break;
2953 case QEMU_OPTION_rtc:
2954 opts = qemu_opts_parse(qemu_find_opts("rtc"), optarg, 0);
2955 if (!opts) {
2956 exit(1);
2958 configure_rtc(opts);
2959 break;
2960 case QEMU_OPTION_tb_size:
2961 tcg_tb_size = strtol(optarg, NULL, 0);
2962 if (tcg_tb_size < 0) {
2963 tcg_tb_size = 0;
2965 break;
2966 case QEMU_OPTION_icount:
2967 icount_option = optarg;
2968 break;
2969 case QEMU_OPTION_incoming:
2970 incoming = optarg;
2971 incoming_expected = true;
2972 break;
2973 case QEMU_OPTION_nodefaults:
2974 default_serial = 0;
2975 default_parallel = 0;
2976 default_virtcon = 0;
2977 default_monitor = 0;
2978 default_vga = 0;
2979 default_net = 0;
2980 default_floppy = 0;
2981 default_cdrom = 0;
2982 default_sdcard = 0;
2983 break;
2984 case QEMU_OPTION_xen_domid:
2985 if (!(xen_available())) {
2986 printf("Option %s not supported for this target\n", popt->name);
2987 exit(1);
2989 xen_domid = atoi(optarg);
2990 break;
2991 case QEMU_OPTION_xen_create:
2992 if (!(xen_available())) {
2993 printf("Option %s not supported for this target\n", popt->name);
2994 exit(1);
2996 xen_mode = XEN_CREATE;
2997 break;
2998 case QEMU_OPTION_xen_attach:
2999 if (!(xen_available())) {
3000 printf("Option %s not supported for this target\n", popt->name);
3001 exit(1);
3003 xen_mode = XEN_ATTACH;
3004 break;
3005 #ifdef CONFIG_SIMPLE_TRACE
3006 case QEMU_OPTION_trace:
3007 opts = qemu_opts_parse(qemu_find_opts("trace"), optarg, 0);
3008 if (opts) {
3009 trace_file = qemu_opt_get(opts, "file");
3011 break;
3012 #endif
3013 case QEMU_OPTION_readconfig:
3015 int ret = qemu_read_config_file(optarg);
3016 if (ret < 0) {
3017 fprintf(stderr, "read config %s: %s\n", optarg,
3018 strerror(-ret));
3019 exit(1);
3021 break;
3023 case QEMU_OPTION_spice:
3024 olist = qemu_find_opts("spice");
3025 if (!olist) {
3026 fprintf(stderr, "spice is not supported by this qemu build.\n");
3027 exit(1);
3029 opts = qemu_opts_parse(olist, optarg, 0);
3030 if (!opts) {
3031 fprintf(stderr, "parse error: %s\n", optarg);
3032 exit(1);
3034 break;
3035 case QEMU_OPTION_writeconfig:
3037 FILE *fp;
3038 if (strcmp(optarg, "-") == 0) {
3039 fp = stdout;
3040 } else {
3041 fp = fopen(optarg, "w");
3042 if (fp == NULL) {
3043 fprintf(stderr, "open %s: %s\n", optarg, strerror(errno));
3044 exit(1);
3047 qemu_config_write(fp);
3048 fclose(fp);
3049 break;
3051 default:
3052 os_parse_cmd_args(popt->index, optarg);
3056 loc_set_none();
3058 /* Open the logfile at this point, if necessary. We can't open the logfile
3059 * when encountering either of the logging options (-d or -D) because the
3060 * other one may be encountered later on the command line, changing the
3061 * location or level of logging.
3063 if (log_mask) {
3064 if (log_file) {
3065 set_cpu_log_filename(log_file);
3067 set_cpu_log(log_mask);
3070 if (!st_init(trace_file)) {
3071 fprintf(stderr, "warning: unable to initialize simple trace backend\n");
3074 /* If no data_dir is specified then try to find it relative to the
3075 executable path. */
3076 if (!data_dir) {
3077 data_dir = os_find_datadir(argv[0]);
3079 /* If all else fails use the install patch specified when building. */
3080 if (!data_dir) {
3081 data_dir = CONFIG_QEMU_DATADIR;
3085 * Default to max_cpus = smp_cpus, in case the user doesn't
3086 * specify a max_cpus value.
3088 if (!max_cpus)
3089 max_cpus = smp_cpus;
3091 machine->max_cpus = machine->max_cpus ?: 1; /* Default to UP */
3092 if (smp_cpus > machine->max_cpus) {
3093 fprintf(stderr, "Number of SMP cpus requested (%d), exceeds max cpus "
3094 "supported by machine `%s' (%d)\n", smp_cpus, machine->name,
3095 machine->max_cpus);
3096 exit(1);
3100 * Get the default machine options from the machine if it is not already
3101 * specified either by the configuration file or by the command line.
3103 if (machine->default_machine_opts) {
3104 QemuOptsList *list = qemu_find_opts("machine");
3105 const char *p = NULL;
3107 if (!QTAILQ_EMPTY(&list->head)) {
3108 p = qemu_opt_get(QTAILQ_FIRST(&list->head), "accel");
3110 if (p == NULL) {
3111 qemu_opts_reset(list);
3112 opts = qemu_opts_parse(list, machine->default_machine_opts, 0);
3113 if (!opts) {
3114 fprintf(stderr, "parse error for machine %s: %s\n",
3115 machine->name, machine->default_machine_opts);
3116 exit(1);
3121 qemu_opts_foreach(qemu_find_opts("device"), default_driver_check, NULL, 0);
3122 qemu_opts_foreach(qemu_find_opts("global"), default_driver_check, NULL, 0);
3124 if (machine->no_serial) {
3125 default_serial = 0;
3127 if (machine->no_parallel) {
3128 default_parallel = 0;
3130 if (!machine->use_virtcon) {
3131 default_virtcon = 0;
3133 if (machine->no_vga) {
3134 default_vga = 0;
3136 if (machine->no_floppy) {
3137 default_floppy = 0;
3139 if (machine->no_cdrom) {
3140 default_cdrom = 0;
3142 if (machine->no_sdcard) {
3143 default_sdcard = 0;
3146 if (display_type == DT_NOGRAPHIC) {
3147 if (default_parallel)
3148 add_device_config(DEV_PARALLEL, "null");
3149 if (default_serial && default_monitor) {
3150 add_device_config(DEV_SERIAL, "mon:stdio");
3151 } else if (default_virtcon && default_monitor) {
3152 add_device_config(DEV_VIRTCON, "mon:stdio");
3153 } else {
3154 if (default_serial)
3155 add_device_config(DEV_SERIAL, "stdio");
3156 if (default_virtcon)
3157 add_device_config(DEV_VIRTCON, "stdio");
3158 if (default_monitor)
3159 monitor_parse("stdio", "readline");
3161 } else {
3162 if (default_serial)
3163 add_device_config(DEV_SERIAL, "vc:80Cx24C");
3164 if (default_parallel)
3165 add_device_config(DEV_PARALLEL, "vc:80Cx24C");
3166 if (default_monitor)
3167 monitor_parse("vc:80Cx24C", "readline");
3168 if (default_virtcon)
3169 add_device_config(DEV_VIRTCON, "vc:80Cx24C");
3171 if (default_vga)
3172 vga_interface_type = VGA_CIRRUS;
3174 socket_init();
3176 if (qemu_opts_foreach(qemu_find_opts("chardev"), chardev_init_func, NULL, 1) != 0)
3177 exit(1);
3178 #ifdef CONFIG_VIRTFS
3179 if (qemu_opts_foreach(qemu_find_opts("fsdev"), fsdev_init_func, NULL, 1) != 0) {
3180 exit(1);
3182 #endif
3184 os_daemonize();
3186 if (pid_file && qemu_create_pidfile(pid_file) != 0) {
3187 os_pidfile_error();
3188 exit(1);
3191 /* init the memory */
3192 if (ram_size == 0) {
3193 ram_size = DEFAULT_RAM_SIZE * 1024 * 1024;
3196 configure_accelerator();
3198 if (qemu_init_main_loop()) {
3199 fprintf(stderr, "qemu_init_main_loop failed\n");
3200 exit(1);
3202 linux_boot = (kernel_filename != NULL);
3204 if (!linux_boot && *kernel_cmdline != '\0') {
3205 fprintf(stderr, "-append only allowed with -kernel option\n");
3206 exit(1);
3209 if (!linux_boot && initrd_filename != NULL) {
3210 fprintf(stderr, "-initrd only allowed with -kernel option\n");
3211 exit(1);
3214 os_set_line_buffering();
3216 if (init_timer_alarm() < 0) {
3217 fprintf(stderr, "could not initialize alarm timer\n");
3218 exit(1);
3220 configure_icount(icount_option);
3222 if (net_init_clients() < 0) {
3223 exit(1);
3226 /* init the bluetooth world */
3227 if (foreach_device_config(DEV_BT, bt_parse))
3228 exit(1);
3230 if (!xen_enabled()) {
3231 /* On 32-bit hosts, QEMU is limited by virtual address space */
3232 if (ram_size > (2047 << 20) && HOST_LONG_BITS == 32) {
3233 fprintf(stderr, "qemu: at most 2047 MB RAM can be simulated\n");
3234 exit(1);
3238 cpu_exec_init_all();
3240 bdrv_init_with_whitelist();
3242 blk_mig_init();
3244 /* open the virtual block devices */
3245 if (snapshot)
3246 qemu_opts_foreach(qemu_find_opts("drive"), drive_enable_snapshot, NULL, 0);
3247 if (qemu_opts_foreach(qemu_find_opts("drive"), drive_init_func, &machine->use_scsi, 1) != 0)
3248 exit(1);
3250 default_drive(default_cdrom, snapshot, machine->use_scsi,
3251 IF_DEFAULT, 2, CDROM_OPTS);
3252 default_drive(default_floppy, snapshot, machine->use_scsi,
3253 IF_FLOPPY, 0, FD_OPTS);
3254 default_drive(default_sdcard, snapshot, machine->use_scsi,
3255 IF_SD, 0, SD_OPTS);
3257 register_savevm_live(NULL, "ram", 0, 4, NULL, ram_save_live, NULL,
3258 ram_load, NULL);
3260 if (nb_numa_nodes > 0) {
3261 int i;
3263 if (nb_numa_nodes > MAX_NODES) {
3264 nb_numa_nodes = MAX_NODES;
3267 /* If no memory size if given for any node, assume the default case
3268 * and distribute the available memory equally across all nodes
3270 for (i = 0; i < nb_numa_nodes; i++) {
3271 if (node_mem[i] != 0)
3272 break;
3274 if (i == nb_numa_nodes) {
3275 uint64_t usedmem = 0;
3277 /* On Linux, the each node's border has to be 8MB aligned,
3278 * the final node gets the rest.
3280 for (i = 0; i < nb_numa_nodes - 1; i++) {
3281 node_mem[i] = (ram_size / nb_numa_nodes) & ~((1 << 23UL) - 1);
3282 usedmem += node_mem[i];
3284 node_mem[i] = ram_size - usedmem;
3287 for (i = 0; i < nb_numa_nodes; i++) {
3288 if (node_cpumask[i] != 0)
3289 break;
3291 /* assigning the VCPUs round-robin is easier to implement, guest OSes
3292 * must cope with this anyway, because there are BIOSes out there in
3293 * real machines which also use this scheme.
3295 if (i == nb_numa_nodes) {
3296 for (i = 0; i < smp_cpus; i++) {
3297 node_cpumask[i % nb_numa_nodes] |= 1 << i;
3302 if (qemu_opts_foreach(qemu_find_opts("mon"), mon_init_func, NULL, 1) != 0) {
3303 exit(1);
3306 if (foreach_device_config(DEV_SERIAL, serial_parse) < 0)
3307 exit(1);
3308 if (foreach_device_config(DEV_PARALLEL, parallel_parse) < 0)
3309 exit(1);
3310 if (foreach_device_config(DEV_VIRTCON, virtcon_parse) < 0)
3311 exit(1);
3312 if (foreach_device_config(DEV_DEBUGCON, debugcon_parse) < 0)
3313 exit(1);
3315 module_call_init(MODULE_INIT_DEVICE);
3317 if (qemu_opts_foreach(qemu_find_opts("device"), device_help_func, NULL, 0) != 0)
3318 exit(0);
3320 if (watchdog) {
3321 i = select_watchdog(watchdog);
3322 if (i > 0)
3323 exit (i == 1 ? 1 : 0);
3326 if (machine->compat_props) {
3327 qdev_prop_register_global_list(machine->compat_props);
3329 qemu_add_globals();
3331 machine->init(ram_size, boot_devices,
3332 kernel_filename, kernel_cmdline, initrd_filename, cpu_model);
3334 cpu_synchronize_all_post_init();
3336 set_numa_modes();
3338 current_machine = machine;
3340 /* init USB devices */
3341 if (usb_enabled) {
3342 if (foreach_device_config(DEV_USB, usb_parse) < 0)
3343 exit(1);
3346 /* init generic devices */
3347 if (qemu_opts_foreach(qemu_find_opts("device"), device_init_func, NULL, 1) != 0)
3348 exit(1);
3350 net_check_clients();
3352 /* just use the first displaystate for the moment */
3353 ds = get_displaystate();
3355 if (using_spice)
3356 display_remote++;
3357 if (display_type == DT_DEFAULT && !display_remote) {
3358 #if defined(CONFIG_SDL) || defined(CONFIG_COCOA)
3359 display_type = DT_SDL;
3360 #elif defined(CONFIG_VNC)
3361 vnc_display = "localhost:0,to=99";
3362 show_vnc_port = 1;
3363 #else
3364 display_type = DT_NONE;
3365 #endif
3369 /* init local displays */
3370 switch (display_type) {
3371 case DT_NOGRAPHIC:
3372 break;
3373 #if defined(CONFIG_CURSES)
3374 case DT_CURSES:
3375 curses_display_init(ds, full_screen);
3376 break;
3377 #endif
3378 #if defined(CONFIG_SDL)
3379 case DT_SDL:
3380 sdl_display_init(ds, full_screen, no_frame);
3381 break;
3382 #elif defined(CONFIG_COCOA)
3383 case DT_SDL:
3384 cocoa_display_init(ds, full_screen);
3385 break;
3386 #endif
3387 default:
3388 break;
3391 /* must be after terminal init, SDL library changes signal handlers */
3392 os_setup_signal_handling();
3394 #ifdef CONFIG_VNC
3395 /* init remote displays */
3396 if (vnc_display) {
3397 vnc_display_init(ds);
3398 if (vnc_display_open(ds, vnc_display) < 0)
3399 exit(1);
3401 if (show_vnc_port) {
3402 printf("VNC server running on `%s'\n", vnc_display_local_addr(ds));
3405 #endif
3406 #ifdef CONFIG_SPICE
3407 if (using_spice && !qxl_enabled) {
3408 qemu_spice_display_init(ds);
3410 #endif
3412 /* display setup */
3413 dpy_resize(ds);
3414 dcl = ds->listeners;
3415 while (dcl != NULL) {
3416 if (dcl->dpy_refresh != NULL) {
3417 ds->gui_timer = qemu_new_timer_ms(rt_clock, gui_update, ds);
3418 qemu_mod_timer(ds->gui_timer, qemu_get_clock_ms(rt_clock));
3419 break;
3421 dcl = dcl->next;
3423 if (ds->gui_timer == NULL) {
3424 nographic_timer = qemu_new_timer_ms(rt_clock, nographic_update, NULL);
3425 qemu_mod_timer(nographic_timer, qemu_get_clock_ms(rt_clock));
3427 text_consoles_set_display(ds);
3429 if (gdbstub_dev && gdbserver_start(gdbstub_dev) < 0) {
3430 fprintf(stderr, "qemu: could not open gdbserver on device '%s'\n",
3431 gdbstub_dev);
3432 exit(1);
3435 qdev_machine_creation_done();
3437 if (rom_load_all() != 0) {
3438 fprintf(stderr, "rom loading failed\n");
3439 exit(1);
3442 /* TODO: once all bus devices are qdevified, this should be done
3443 * when bus is created by qdev.c */
3444 qemu_register_reset(qbus_reset_all_fn, sysbus_get_default());
3445 qemu_run_machine_init_done_notifiers();
3447 qemu_system_reset(VMRESET_SILENT);
3448 if (loadvm) {
3449 if (load_vmstate(loadvm) < 0) {
3450 autostart = 0;
3454 if (incoming) {
3455 int ret = qemu_start_incoming_migration(incoming);
3456 if (ret < 0) {
3457 fprintf(stderr, "Migration failed. Exit code %s(%d), exiting.\n",
3458 incoming, ret);
3459 exit(ret);
3461 } else if (autostart) {
3462 vm_start();
3465 os_setup_post();
3467 main_loop();
3468 quit_timers();
3469 net_cleanup();
3470 res_free();
3472 return 0;