cirrus_vga: remove unneeded reset
[qemu/ar7.git] / vl.c
blob192a240b52e3b3f9ee9bb0b457880519f60ff6e4
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 "hw/hw.h"
115 #include "hw/boards.h"
116 #include "hw/usb.h"
117 #include "hw/pcmcia.h"
118 #include "hw/pc.h"
119 #include "hw/isa.h"
120 #include "hw/baum.h"
121 #include "hw/bt.h"
122 #include "hw/watchdog.h"
123 #include "hw/smbios.h"
124 #include "hw/xen.h"
125 #include "hw/qdev.h"
126 #include "hw/loader.h"
127 #include "bt-host.h"
128 #include "net.h"
129 #include "net/slirp.h"
130 #include "monitor.h"
131 #include "console.h"
132 #include "sysemu.h"
133 #include "gdbstub.h"
134 #include "qemu-timer.h"
135 #include "qemu-char.h"
136 #include "cache-utils.h"
137 #include "block.h"
138 #include "blockdev.h"
139 #include "block-migration.h"
140 #include "dma.h"
141 #include "audio/audio.h"
142 #include "migration.h"
143 #include "kvm.h"
144 #include "qemu-option.h"
145 #include "qemu-config.h"
146 #include "qemu-objects.h"
147 #include "qemu-options.h"
148 #ifdef CONFIG_VIRTFS
149 #include "fsdev/qemu-fsdev.h"
150 #endif
152 #include "disas.h"
154 #include "qemu_socket.h"
156 #include "slirp/libslirp.h"
158 #include "trace.h"
159 #include "simpletrace.h"
160 #include "qemu-queue.h"
161 #include "cpus.h"
162 #include "arch_init.h"
164 #include "ui/qemu-spice.h"
166 //#define DEBUG_NET
167 //#define DEBUG_SLIRP
169 #define DEFAULT_RAM_SIZE 128
171 #define MAX_VIRTIO_CONSOLES 1
173 static const char *data_dir;
174 const char *bios_name = NULL;
175 enum vga_retrace_method vga_retrace_method = VGA_RETRACE_DUMB;
176 DisplayType display_type = DT_DEFAULT;
177 int display_remote = 0;
178 const char* keyboard_layout = NULL;
179 ram_addr_t ram_size;
180 const char *mem_path = NULL;
181 #ifdef MAP_POPULATE
182 int mem_prealloc = 0; /* force preallocation of physical target memory */
183 #endif
184 int nb_nics;
185 NICInfo nd_table[MAX_NICS];
186 int vm_running;
187 int autostart;
188 int incoming_expected; /* Started with -incoming and waiting for incoming */
189 static int rtc_utc = 1;
190 static int rtc_date_offset = -1; /* -1 means no change */
191 QEMUClock *rtc_clock;
192 int vga_interface_type = VGA_NONE;
193 static int full_screen = 0;
194 #ifdef CONFIG_SDL
195 static int no_frame = 0;
196 #endif
197 int no_quit = 0;
198 CharDriverState *serial_hds[MAX_SERIAL_PORTS];
199 CharDriverState *parallel_hds[MAX_PARALLEL_PORTS];
200 CharDriverState *virtcon_hds[MAX_VIRTIO_CONSOLES];
201 int win2k_install_hack = 0;
202 int rtc_td_hack = 0;
203 int usb_enabled = 0;
204 int singlestep = 0;
205 int smp_cpus = 1;
206 int max_cpus = 0;
207 int smp_cores = 1;
208 int smp_threads = 1;
209 #ifdef CONFIG_VNC
210 const char *vnc_display;
211 #endif
212 int acpi_enabled = 1;
213 int no_hpet = 0;
214 int fd_bootchk = 1;
215 int no_reboot = 0;
216 int no_shutdown = 0;
217 int cursor_hide = 1;
218 int graphic_rotate = 0;
219 uint8_t irq0override = 1;
220 const char *watchdog;
221 QEMUOptionRom option_rom[MAX_OPTION_ROMS];
222 int nb_option_roms;
223 int semihosting_enabled = 0;
224 int old_param = 0;
225 const char *qemu_name;
226 int alt_grab = 0;
227 int ctrl_grab = 0;
228 unsigned int nb_prom_envs = 0;
229 const char *prom_envs[MAX_PROM_ENVS];
230 int boot_menu;
232 typedef struct FWBootEntry FWBootEntry;
234 struct FWBootEntry {
235 QTAILQ_ENTRY(FWBootEntry) link;
236 int32_t bootindex;
237 DeviceState *dev;
238 char *suffix;
241 QTAILQ_HEAD(, FWBootEntry) fw_boot_order = QTAILQ_HEAD_INITIALIZER(fw_boot_order);
243 int nb_numa_nodes;
244 uint64_t node_mem[MAX_NODES];
245 uint64_t node_cpumask[MAX_NODES];
247 static QEMUTimer *nographic_timer;
249 uint8_t qemu_uuid[16];
251 static QEMUBootSetHandler *boot_set_handler;
252 static void *boot_set_opaque;
254 static NotifierList exit_notifiers =
255 NOTIFIER_LIST_INITIALIZER(exit_notifiers);
257 static NotifierList machine_init_done_notifiers =
258 NOTIFIER_LIST_INITIALIZER(machine_init_done_notifiers);
260 int kvm_allowed = 0;
261 uint32_t xen_domid;
262 enum xen_mode xen_mode = XEN_EMULATE;
264 static int default_serial = 1;
265 static int default_parallel = 1;
266 static int default_virtcon = 1;
267 static int default_monitor = 1;
268 static int default_vga = 1;
269 static int default_floppy = 1;
270 static int default_cdrom = 1;
271 static int default_sdcard = 1;
273 static struct {
274 const char *driver;
275 int *flag;
276 } default_list[] = {
277 { .driver = "isa-serial", .flag = &default_serial },
278 { .driver = "isa-parallel", .flag = &default_parallel },
279 { .driver = "isa-fdc", .flag = &default_floppy },
280 { .driver = "ide-drive", .flag = &default_cdrom },
281 { .driver = "virtio-serial-pci", .flag = &default_virtcon },
282 { .driver = "virtio-serial-s390", .flag = &default_virtcon },
283 { .driver = "virtio-serial", .flag = &default_virtcon },
284 { .driver = "VGA", .flag = &default_vga },
285 { .driver = "cirrus-vga", .flag = &default_vga },
286 { .driver = "vmware-svga", .flag = &default_vga },
289 static int default_driver_check(QemuOpts *opts, void *opaque)
291 const char *driver = qemu_opt_get(opts, "driver");
292 int i;
294 if (!driver)
295 return 0;
296 for (i = 0; i < ARRAY_SIZE(default_list); i++) {
297 if (strcmp(default_list[i].driver, driver) != 0)
298 continue;
299 *(default_list[i].flag) = 0;
301 return 0;
304 /***********************************************************/
305 /* real time host monotonic timer */
307 /***********************************************************/
308 /* host time/date access */
309 void qemu_get_timedate(struct tm *tm, int offset)
311 time_t ti;
312 struct tm *ret;
314 time(&ti);
315 ti += offset;
316 if (rtc_date_offset == -1) {
317 if (rtc_utc)
318 ret = gmtime(&ti);
319 else
320 ret = localtime(&ti);
321 } else {
322 ti -= rtc_date_offset;
323 ret = gmtime(&ti);
326 memcpy(tm, ret, sizeof(struct tm));
329 int qemu_timedate_diff(struct tm *tm)
331 time_t seconds;
333 if (rtc_date_offset == -1)
334 if (rtc_utc)
335 seconds = mktimegm(tm);
336 else
337 seconds = mktime(tm);
338 else
339 seconds = mktimegm(tm) + rtc_date_offset;
341 return seconds - time(NULL);
344 void rtc_change_mon_event(struct tm *tm)
346 QObject *data;
348 data = qobject_from_jsonf("{ 'offset': %d }", qemu_timedate_diff(tm));
349 monitor_protocol_event(QEVENT_RTC_CHANGE, data);
350 qobject_decref(data);
353 static void configure_rtc_date_offset(const char *startdate, int legacy)
355 time_t rtc_start_date;
356 struct tm tm;
358 if (!strcmp(startdate, "now") && legacy) {
359 rtc_date_offset = -1;
360 } else {
361 if (sscanf(startdate, "%d-%d-%dT%d:%d:%d",
362 &tm.tm_year,
363 &tm.tm_mon,
364 &tm.tm_mday,
365 &tm.tm_hour,
366 &tm.tm_min,
367 &tm.tm_sec) == 6) {
368 /* OK */
369 } else if (sscanf(startdate, "%d-%d-%d",
370 &tm.tm_year,
371 &tm.tm_mon,
372 &tm.tm_mday) == 3) {
373 tm.tm_hour = 0;
374 tm.tm_min = 0;
375 tm.tm_sec = 0;
376 } else {
377 goto date_fail;
379 tm.tm_year -= 1900;
380 tm.tm_mon--;
381 rtc_start_date = mktimegm(&tm);
382 if (rtc_start_date == -1) {
383 date_fail:
384 fprintf(stderr, "Invalid date format. Valid formats are:\n"
385 "'2006-06-17T16:01:21' or '2006-06-17'\n");
386 exit(1);
388 rtc_date_offset = time(NULL) - rtc_start_date;
392 static void configure_rtc(QemuOpts *opts)
394 const char *value;
396 value = qemu_opt_get(opts, "base");
397 if (value) {
398 if (!strcmp(value, "utc")) {
399 rtc_utc = 1;
400 } else if (!strcmp(value, "localtime")) {
401 rtc_utc = 0;
402 } else {
403 configure_rtc_date_offset(value, 0);
406 value = qemu_opt_get(opts, "clock");
407 if (value) {
408 if (!strcmp(value, "host")) {
409 rtc_clock = host_clock;
410 } else if (!strcmp(value, "vm")) {
411 rtc_clock = vm_clock;
412 } else {
413 fprintf(stderr, "qemu: invalid option value '%s'\n", value);
414 exit(1);
417 value = qemu_opt_get(opts, "driftfix");
418 if (value) {
419 if (!strcmp(value, "slew")) {
420 rtc_td_hack = 1;
421 } else if (!strcmp(value, "none")) {
422 rtc_td_hack = 0;
423 } else {
424 fprintf(stderr, "qemu: invalid option value '%s'\n", value);
425 exit(1);
430 /***********************************************************/
431 /* Bluetooth support */
432 static int nb_hcis;
433 static int cur_hci;
434 static struct HCIInfo *hci_table[MAX_NICS];
436 static struct bt_vlan_s {
437 struct bt_scatternet_s net;
438 int id;
439 struct bt_vlan_s *next;
440 } *first_bt_vlan;
442 /* find or alloc a new bluetooth "VLAN" */
443 static struct bt_scatternet_s *qemu_find_bt_vlan(int id)
445 struct bt_vlan_s **pvlan, *vlan;
446 for (vlan = first_bt_vlan; vlan != NULL; vlan = vlan->next) {
447 if (vlan->id == id)
448 return &vlan->net;
450 vlan = qemu_mallocz(sizeof(struct bt_vlan_s));
451 vlan->id = id;
452 pvlan = &first_bt_vlan;
453 while (*pvlan != NULL)
454 pvlan = &(*pvlan)->next;
455 *pvlan = vlan;
456 return &vlan->net;
459 static void null_hci_send(struct HCIInfo *hci, const uint8_t *data, int len)
463 static int null_hci_addr_set(struct HCIInfo *hci, const uint8_t *bd_addr)
465 return -ENOTSUP;
468 static struct HCIInfo null_hci = {
469 .cmd_send = null_hci_send,
470 .sco_send = null_hci_send,
471 .acl_send = null_hci_send,
472 .bdaddr_set = null_hci_addr_set,
475 struct HCIInfo *qemu_next_hci(void)
477 if (cur_hci == nb_hcis)
478 return &null_hci;
480 return hci_table[cur_hci++];
483 static struct HCIInfo *hci_init(const char *str)
485 char *endp;
486 struct bt_scatternet_s *vlan = 0;
488 if (!strcmp(str, "null"))
489 /* null */
490 return &null_hci;
491 else if (!strncmp(str, "host", 4) && (str[4] == '\0' || str[4] == ':'))
492 /* host[:hciN] */
493 return bt_host_hci(str[4] ? str + 5 : "hci0");
494 else if (!strncmp(str, "hci", 3)) {
495 /* hci[,vlan=n] */
496 if (str[3]) {
497 if (!strncmp(str + 3, ",vlan=", 6)) {
498 vlan = qemu_find_bt_vlan(strtol(str + 9, &endp, 0));
499 if (*endp)
500 vlan = 0;
502 } else
503 vlan = qemu_find_bt_vlan(0);
504 if (vlan)
505 return bt_new_hci(vlan);
508 fprintf(stderr, "qemu: Unknown bluetooth HCI `%s'.\n", str);
510 return 0;
513 static int bt_hci_parse(const char *str)
515 struct HCIInfo *hci;
516 bdaddr_t bdaddr;
518 if (nb_hcis >= MAX_NICS) {
519 fprintf(stderr, "qemu: Too many bluetooth HCIs (max %i).\n", MAX_NICS);
520 return -1;
523 hci = hci_init(str);
524 if (!hci)
525 return -1;
527 bdaddr.b[0] = 0x52;
528 bdaddr.b[1] = 0x54;
529 bdaddr.b[2] = 0x00;
530 bdaddr.b[3] = 0x12;
531 bdaddr.b[4] = 0x34;
532 bdaddr.b[5] = 0x56 + nb_hcis;
533 hci->bdaddr_set(hci, bdaddr.b);
535 hci_table[nb_hcis++] = hci;
537 return 0;
540 static void bt_vhci_add(int vlan_id)
542 struct bt_scatternet_s *vlan = qemu_find_bt_vlan(vlan_id);
544 if (!vlan->slave)
545 fprintf(stderr, "qemu: warning: adding a VHCI to "
546 "an empty scatternet %i\n", vlan_id);
548 bt_vhci_init(bt_new_hci(vlan));
551 static struct bt_device_s *bt_device_add(const char *opt)
553 struct bt_scatternet_s *vlan;
554 int vlan_id = 0;
555 char *endp = strstr(opt, ",vlan=");
556 int len = (endp ? endp - opt : strlen(opt)) + 1;
557 char devname[10];
559 pstrcpy(devname, MIN(sizeof(devname), len), opt);
561 if (endp) {
562 vlan_id = strtol(endp + 6, &endp, 0);
563 if (*endp) {
564 fprintf(stderr, "qemu: unrecognised bluetooth vlan Id\n");
565 return 0;
569 vlan = qemu_find_bt_vlan(vlan_id);
571 if (!vlan->slave)
572 fprintf(stderr, "qemu: warning: adding a slave device to "
573 "an empty scatternet %i\n", vlan_id);
575 if (!strcmp(devname, "keyboard"))
576 return bt_keyboard_init(vlan);
578 fprintf(stderr, "qemu: unsupported bluetooth device `%s'\n", devname);
579 return 0;
582 static int bt_parse(const char *opt)
584 const char *endp, *p;
585 int vlan;
587 if (strstart(opt, "hci", &endp)) {
588 if (!*endp || *endp == ',') {
589 if (*endp)
590 if (!strstart(endp, ",vlan=", 0))
591 opt = endp + 1;
593 return bt_hci_parse(opt);
595 } else if (strstart(opt, "vhci", &endp)) {
596 if (!*endp || *endp == ',') {
597 if (*endp) {
598 if (strstart(endp, ",vlan=", &p)) {
599 vlan = strtol(p, (char **) &endp, 0);
600 if (*endp) {
601 fprintf(stderr, "qemu: bad scatternet '%s'\n", p);
602 return 1;
604 } else {
605 fprintf(stderr, "qemu: bad parameter '%s'\n", endp + 1);
606 return 1;
608 } else
609 vlan = 0;
611 bt_vhci_add(vlan);
612 return 0;
614 } else if (strstart(opt, "device:", &endp))
615 return !bt_device_add(endp);
617 fprintf(stderr, "qemu: bad bluetooth parameter '%s'\n", opt);
618 return 1;
621 /***********************************************************/
622 /* QEMU Block devices */
624 #define HD_OPTS "media=disk"
625 #define CDROM_OPTS "media=cdrom"
626 #define FD_OPTS ""
627 #define PFLASH_OPTS ""
628 #define MTD_OPTS ""
629 #define SD_OPTS ""
631 static int drive_init_func(QemuOpts *opts, void *opaque)
633 int *use_scsi = opaque;
635 return drive_init(opts, *use_scsi) == NULL;
638 static int drive_enable_snapshot(QemuOpts *opts, void *opaque)
640 if (NULL == qemu_opt_get(opts, "snapshot")) {
641 qemu_opt_set(opts, "snapshot", "on");
643 return 0;
646 static void default_drive(int enable, int snapshot, int use_scsi,
647 BlockInterfaceType type, int index,
648 const char *optstr)
650 QemuOpts *opts;
652 if (type == IF_DEFAULT) {
653 type = use_scsi ? IF_SCSI : IF_IDE;
656 if (!enable || drive_get_by_index(type, index)) {
657 return;
660 opts = drive_add(type, index, NULL, optstr);
661 if (snapshot) {
662 drive_enable_snapshot(opts, NULL);
664 if (!drive_init(opts, use_scsi)) {
665 exit(1);
669 void qemu_register_boot_set(QEMUBootSetHandler *func, void *opaque)
671 boot_set_handler = func;
672 boot_set_opaque = opaque;
675 int qemu_boot_set(const char *boot_devices)
677 if (!boot_set_handler) {
678 return -EINVAL;
680 return boot_set_handler(boot_set_opaque, boot_devices);
683 static void validate_bootdevices(char *devices)
685 /* We just do some generic consistency checks */
686 const char *p;
687 int bitmap = 0;
689 for (p = devices; *p != '\0'; p++) {
690 /* Allowed boot devices are:
691 * a-b: floppy disk drives
692 * c-f: IDE disk drives
693 * g-m: machine implementation dependant drives
694 * n-p: network devices
695 * It's up to each machine implementation to check if the given boot
696 * devices match the actual hardware implementation and firmware
697 * features.
699 if (*p < 'a' || *p > 'p') {
700 fprintf(stderr, "Invalid boot device '%c'\n", *p);
701 exit(1);
703 if (bitmap & (1 << (*p - 'a'))) {
704 fprintf(stderr, "Boot device '%c' was given twice\n", *p);
705 exit(1);
707 bitmap |= 1 << (*p - 'a');
711 static void restore_boot_devices(void *opaque)
713 char *standard_boot_devices = opaque;
714 static int first = 1;
716 /* Restore boot order and remove ourselves after the first boot */
717 if (first) {
718 first = 0;
719 return;
722 qemu_boot_set(standard_boot_devices);
724 qemu_unregister_reset(restore_boot_devices, standard_boot_devices);
725 qemu_free(standard_boot_devices);
728 void add_boot_device_path(int32_t bootindex, DeviceState *dev,
729 const char *suffix)
731 FWBootEntry *node, *i;
733 if (bootindex < 0) {
734 return;
737 assert(dev != NULL || suffix != NULL);
739 node = qemu_mallocz(sizeof(FWBootEntry));
740 node->bootindex = bootindex;
741 node->suffix = suffix ? qemu_strdup(suffix) : NULL;
742 node->dev = dev;
744 QTAILQ_FOREACH(i, &fw_boot_order, link) {
745 if (i->bootindex == bootindex) {
746 fprintf(stderr, "Two devices with same boot index %d\n", bootindex);
747 exit(1);
748 } else if (i->bootindex < bootindex) {
749 continue;
751 QTAILQ_INSERT_BEFORE(i, node, link);
752 return;
754 QTAILQ_INSERT_TAIL(&fw_boot_order, node, link);
758 * This function returns null terminated string that consist of new line
759 * separated device pathes.
761 * memory pointed by "size" is assigned total length of the array in bytes
764 char *get_boot_devices_list(uint32_t *size)
766 FWBootEntry *i;
767 uint32_t total = 0;
768 char *list = NULL;
770 QTAILQ_FOREACH(i, &fw_boot_order, link) {
771 char *devpath = NULL, *bootpath;
772 int len;
774 if (i->dev) {
775 devpath = qdev_get_fw_dev_path(i->dev);
776 assert(devpath);
779 if (i->suffix && devpath) {
780 size_t bootpathlen = strlen(devpath) + strlen(i->suffix) + 1;
782 bootpath = qemu_malloc(bootpathlen);
783 snprintf(bootpath, bootpathlen, "%s%s", devpath, i->suffix);
784 qemu_free(devpath);
785 } else if (devpath) {
786 bootpath = devpath;
787 } else {
788 bootpath = qemu_strdup(i->suffix);
789 assert(bootpath);
792 if (total) {
793 list[total-1] = '\n';
795 len = strlen(bootpath) + 1;
796 list = qemu_realloc(list, total + len);
797 memcpy(&list[total], bootpath, len);
798 total += len;
799 qemu_free(bootpath);
802 *size = total;
804 return list;
807 static void numa_add(const char *optarg)
809 char option[128];
810 char *endptr;
811 unsigned long long value, endvalue;
812 int nodenr;
814 optarg = get_opt_name(option, 128, optarg, ',') + 1;
815 if (!strcmp(option, "node")) {
816 if (get_param_value(option, 128, "nodeid", optarg) == 0) {
817 nodenr = nb_numa_nodes;
818 } else {
819 nodenr = strtoull(option, NULL, 10);
822 if (get_param_value(option, 128, "mem", optarg) == 0) {
823 node_mem[nodenr] = 0;
824 } else {
825 int64_t sval;
826 sval = strtosz(option, NULL);
827 if (sval < 0) {
828 fprintf(stderr, "qemu: invalid numa mem size: %s\n", optarg);
829 exit(1);
831 node_mem[nodenr] = sval;
833 if (get_param_value(option, 128, "cpus", optarg) == 0) {
834 node_cpumask[nodenr] = 0;
835 } else {
836 value = strtoull(option, &endptr, 10);
837 if (value >= 64) {
838 value = 63;
839 fprintf(stderr, "only 64 CPUs in NUMA mode supported.\n");
840 } else {
841 if (*endptr == '-') {
842 endvalue = strtoull(endptr+1, &endptr, 10);
843 if (endvalue >= 63) {
844 endvalue = 62;
845 fprintf(stderr,
846 "only 63 CPUs in NUMA mode supported.\n");
848 value = (2ULL << endvalue) - (1ULL << value);
849 } else {
850 value = 1ULL << value;
853 node_cpumask[nodenr] = value;
855 nb_numa_nodes++;
857 return;
860 static void smp_parse(const char *optarg)
862 int smp, sockets = 0, threads = 0, cores = 0;
863 char *endptr;
864 char option[128];
866 smp = strtoul(optarg, &endptr, 10);
867 if (endptr != optarg) {
868 if (*endptr == ',') {
869 endptr++;
872 if (get_param_value(option, 128, "sockets", endptr) != 0)
873 sockets = strtoull(option, NULL, 10);
874 if (get_param_value(option, 128, "cores", endptr) != 0)
875 cores = strtoull(option, NULL, 10);
876 if (get_param_value(option, 128, "threads", endptr) != 0)
877 threads = strtoull(option, NULL, 10);
878 if (get_param_value(option, 128, "maxcpus", endptr) != 0)
879 max_cpus = strtoull(option, NULL, 10);
881 /* compute missing values, prefer sockets over cores over threads */
882 if (smp == 0 || sockets == 0) {
883 sockets = sockets > 0 ? sockets : 1;
884 cores = cores > 0 ? cores : 1;
885 threads = threads > 0 ? threads : 1;
886 if (smp == 0) {
887 smp = cores * threads * sockets;
889 } else {
890 if (cores == 0) {
891 threads = threads > 0 ? threads : 1;
892 cores = smp / (sockets * threads);
893 } else {
894 threads = smp / (cores * sockets);
897 smp_cpus = smp;
898 smp_cores = cores > 0 ? cores : 1;
899 smp_threads = threads > 0 ? threads : 1;
900 if (max_cpus == 0)
901 max_cpus = smp_cpus;
904 /***********************************************************/
905 /* USB devices */
907 static int usb_device_add(const char *devname)
909 const char *p;
910 USBDevice *dev = NULL;
912 if (!usb_enabled)
913 return -1;
915 /* drivers with .usbdevice_name entry in USBDeviceInfo */
916 dev = usbdevice_create(devname);
917 if (dev)
918 goto done;
920 /* the other ones */
921 if (strstart(devname, "host:", &p)) {
922 dev = usb_host_device_open(p);
923 } else if (!strcmp(devname, "bt") || strstart(devname, "bt:", &p)) {
924 dev = usb_bt_init(devname[2] ? hci_init(p) :
925 bt_new_hci(qemu_find_bt_vlan(0)));
926 } else {
927 return -1;
929 if (!dev)
930 return -1;
932 done:
933 return 0;
936 static int usb_device_del(const char *devname)
938 int bus_num, addr;
939 const char *p;
941 if (strstart(devname, "host:", &p))
942 return usb_host_device_close(p);
944 if (!usb_enabled)
945 return -1;
947 p = strchr(devname, '.');
948 if (!p)
949 return -1;
950 bus_num = strtoul(devname, NULL, 0);
951 addr = strtoul(p + 1, NULL, 0);
953 return usb_device_delete_addr(bus_num, addr);
956 static int usb_parse(const char *cmdline)
958 int r;
959 r = usb_device_add(cmdline);
960 if (r < 0) {
961 fprintf(stderr, "qemu: could not add USB device '%s'\n", cmdline);
963 return r;
966 void do_usb_add(Monitor *mon, const QDict *qdict)
968 const char *devname = qdict_get_str(qdict, "devname");
969 if (usb_device_add(devname) < 0) {
970 error_report("could not add USB device '%s'", devname);
974 void do_usb_del(Monitor *mon, const QDict *qdict)
976 const char *devname = qdict_get_str(qdict, "devname");
977 if (usb_device_del(devname) < 0) {
978 error_report("could not delete USB device '%s'", devname);
982 /***********************************************************/
983 /* PCMCIA/Cardbus */
985 static struct pcmcia_socket_entry_s {
986 PCMCIASocket *socket;
987 struct pcmcia_socket_entry_s *next;
988 } *pcmcia_sockets = 0;
990 void pcmcia_socket_register(PCMCIASocket *socket)
992 struct pcmcia_socket_entry_s *entry;
994 entry = qemu_malloc(sizeof(struct pcmcia_socket_entry_s));
995 entry->socket = socket;
996 entry->next = pcmcia_sockets;
997 pcmcia_sockets = entry;
1000 void pcmcia_socket_unregister(PCMCIASocket *socket)
1002 struct pcmcia_socket_entry_s *entry, **ptr;
1004 ptr = &pcmcia_sockets;
1005 for (entry = *ptr; entry; ptr = &entry->next, entry = *ptr)
1006 if (entry->socket == socket) {
1007 *ptr = entry->next;
1008 qemu_free(entry);
1012 void pcmcia_info(Monitor *mon)
1014 struct pcmcia_socket_entry_s *iter;
1016 if (!pcmcia_sockets)
1017 monitor_printf(mon, "No PCMCIA sockets\n");
1019 for (iter = pcmcia_sockets; iter; iter = iter->next)
1020 monitor_printf(mon, "%s: %s\n", iter->socket->slot_string,
1021 iter->socket->attached ? iter->socket->card_string :
1022 "Empty");
1025 /***********************************************************/
1026 /* I/O handling */
1028 typedef struct IOHandlerRecord {
1029 int fd;
1030 IOCanReadHandler *fd_read_poll;
1031 IOHandler *fd_read;
1032 IOHandler *fd_write;
1033 int deleted;
1034 void *opaque;
1035 /* temporary data */
1036 struct pollfd *ufd;
1037 QLIST_ENTRY(IOHandlerRecord) next;
1038 } IOHandlerRecord;
1040 static QLIST_HEAD(, IOHandlerRecord) io_handlers =
1041 QLIST_HEAD_INITIALIZER(io_handlers);
1044 /* XXX: fd_read_poll should be suppressed, but an API change is
1045 necessary in the character devices to suppress fd_can_read(). */
1046 int qemu_set_fd_handler2(int fd,
1047 IOCanReadHandler *fd_read_poll,
1048 IOHandler *fd_read,
1049 IOHandler *fd_write,
1050 void *opaque)
1052 IOHandlerRecord *ioh;
1054 if (!fd_read && !fd_write) {
1055 QLIST_FOREACH(ioh, &io_handlers, next) {
1056 if (ioh->fd == fd) {
1057 ioh->deleted = 1;
1058 break;
1061 } else {
1062 QLIST_FOREACH(ioh, &io_handlers, next) {
1063 if (ioh->fd == fd)
1064 goto found;
1066 ioh = qemu_mallocz(sizeof(IOHandlerRecord));
1067 QLIST_INSERT_HEAD(&io_handlers, ioh, next);
1068 found:
1069 ioh->fd = fd;
1070 ioh->fd_read_poll = fd_read_poll;
1071 ioh->fd_read = fd_read;
1072 ioh->fd_write = fd_write;
1073 ioh->opaque = opaque;
1074 ioh->deleted = 0;
1076 return 0;
1079 int qemu_set_fd_handler(int fd,
1080 IOHandler *fd_read,
1081 IOHandler *fd_write,
1082 void *opaque)
1084 return qemu_set_fd_handler2(fd, NULL, fd_read, fd_write, opaque);
1087 /***********************************************************/
1088 /* machine registration */
1090 static QEMUMachine *first_machine = NULL;
1091 QEMUMachine *current_machine = NULL;
1093 int qemu_register_machine(QEMUMachine *m)
1095 QEMUMachine **pm;
1096 pm = &first_machine;
1097 while (*pm != NULL)
1098 pm = &(*pm)->next;
1099 m->next = NULL;
1100 *pm = m;
1101 return 0;
1104 static QEMUMachine *find_machine(const char *name)
1106 QEMUMachine *m;
1108 for(m = first_machine; m != NULL; m = m->next) {
1109 if (!strcmp(m->name, name))
1110 return m;
1111 if (m->alias && !strcmp(m->alias, name))
1112 return m;
1114 return NULL;
1117 static QEMUMachine *find_default_machine(void)
1119 QEMUMachine *m;
1121 for(m = first_machine; m != NULL; m = m->next) {
1122 if (m->is_default) {
1123 return m;
1126 return NULL;
1129 /***********************************************************/
1130 /* main execution loop */
1132 static void gui_update(void *opaque)
1134 uint64_t interval = GUI_REFRESH_INTERVAL;
1135 DisplayState *ds = opaque;
1136 DisplayChangeListener *dcl = ds->listeners;
1138 qemu_flush_coalesced_mmio_buffer();
1139 dpy_refresh(ds);
1141 while (dcl != NULL) {
1142 if (dcl->gui_timer_interval &&
1143 dcl->gui_timer_interval < interval)
1144 interval = dcl->gui_timer_interval;
1145 dcl = dcl->next;
1147 qemu_mod_timer(ds->gui_timer, interval + qemu_get_clock_ms(rt_clock));
1150 static void nographic_update(void *opaque)
1152 uint64_t interval = GUI_REFRESH_INTERVAL;
1154 qemu_flush_coalesced_mmio_buffer();
1155 qemu_mod_timer(nographic_timer, interval + qemu_get_clock_ms(rt_clock));
1158 struct vm_change_state_entry {
1159 VMChangeStateHandler *cb;
1160 void *opaque;
1161 QLIST_ENTRY (vm_change_state_entry) entries;
1164 static QLIST_HEAD(vm_change_state_head, vm_change_state_entry) vm_change_state_head;
1166 VMChangeStateEntry *qemu_add_vm_change_state_handler(VMChangeStateHandler *cb,
1167 void *opaque)
1169 VMChangeStateEntry *e;
1171 e = qemu_mallocz(sizeof (*e));
1173 e->cb = cb;
1174 e->opaque = opaque;
1175 QLIST_INSERT_HEAD(&vm_change_state_head, e, entries);
1176 return e;
1179 void qemu_del_vm_change_state_handler(VMChangeStateEntry *e)
1181 QLIST_REMOVE (e, entries);
1182 qemu_free (e);
1185 void vm_state_notify(int running, int reason)
1187 VMChangeStateEntry *e;
1189 trace_vm_state_notify(running, reason);
1191 for (e = vm_change_state_head.lh_first; e; e = e->entries.le_next) {
1192 e->cb(e->opaque, running, reason);
1196 void vm_start(void)
1198 if (!vm_running) {
1199 cpu_enable_ticks();
1200 vm_running = 1;
1201 vm_state_notify(1, 0);
1202 resume_all_vcpus();
1203 monitor_protocol_event(QEVENT_RESUME, NULL);
1207 /* reset/shutdown handler */
1209 typedef struct QEMUResetEntry {
1210 QTAILQ_ENTRY(QEMUResetEntry) entry;
1211 QEMUResetHandler *func;
1212 void *opaque;
1213 } QEMUResetEntry;
1215 static QTAILQ_HEAD(reset_handlers, QEMUResetEntry) reset_handlers =
1216 QTAILQ_HEAD_INITIALIZER(reset_handlers);
1217 static int reset_requested;
1218 static int shutdown_requested, shutdown_signal = -1;
1219 static pid_t shutdown_pid;
1220 static int powerdown_requested;
1221 static int debug_requested;
1222 static int vmstop_requested;
1224 int qemu_shutdown_requested(void)
1226 int r = shutdown_requested;
1227 shutdown_requested = 0;
1228 return r;
1231 void qemu_kill_report(void)
1233 if (shutdown_signal != -1) {
1234 fprintf(stderr, "Got signal %d from pid %d\n",
1235 shutdown_signal, shutdown_pid);
1236 shutdown_signal = -1;
1240 int qemu_reset_requested(void)
1242 int r = reset_requested;
1243 reset_requested = 0;
1244 return r;
1247 int qemu_powerdown_requested(void)
1249 int r = powerdown_requested;
1250 powerdown_requested = 0;
1251 return r;
1254 static int qemu_debug_requested(void)
1256 int r = debug_requested;
1257 debug_requested = 0;
1258 return r;
1261 static int qemu_vmstop_requested(void)
1263 int r = vmstop_requested;
1264 vmstop_requested = 0;
1265 return r;
1268 void qemu_register_reset(QEMUResetHandler *func, void *opaque)
1270 QEMUResetEntry *re = qemu_mallocz(sizeof(QEMUResetEntry));
1272 re->func = func;
1273 re->opaque = opaque;
1274 QTAILQ_INSERT_TAIL(&reset_handlers, re, entry);
1277 void qemu_unregister_reset(QEMUResetHandler *func, void *opaque)
1279 QEMUResetEntry *re;
1281 QTAILQ_FOREACH(re, &reset_handlers, entry) {
1282 if (re->func == func && re->opaque == opaque) {
1283 QTAILQ_REMOVE(&reset_handlers, re, entry);
1284 qemu_free(re);
1285 return;
1290 void qemu_system_reset(void)
1292 QEMUResetEntry *re, *nre;
1294 /* reset all devices */
1295 QTAILQ_FOREACH_SAFE(re, &reset_handlers, entry, nre) {
1296 re->func(re->opaque);
1298 monitor_protocol_event(QEVENT_RESET, NULL);
1299 cpu_synchronize_all_post_reset();
1302 void qemu_system_reset_request(void)
1304 if (no_reboot) {
1305 shutdown_requested = 1;
1306 } else {
1307 reset_requested = 1;
1309 cpu_stop_current();
1310 qemu_notify_event();
1313 void qemu_system_killed(int signal, pid_t pid)
1315 shutdown_signal = signal;
1316 shutdown_pid = pid;
1317 qemu_system_shutdown_request();
1320 void qemu_system_shutdown_request(void)
1322 shutdown_requested = 1;
1323 qemu_notify_event();
1326 void qemu_system_powerdown_request(void)
1328 powerdown_requested = 1;
1329 qemu_notify_event();
1332 void qemu_system_debug_request(void)
1334 debug_requested = 1;
1335 qemu_notify_event();
1338 void qemu_system_vmstop_request(int reason)
1340 vmstop_requested = reason;
1341 qemu_notify_event();
1344 void main_loop_wait(int nonblocking)
1346 IOHandlerRecord *ioh;
1347 fd_set rfds, wfds, xfds;
1348 int ret, nfds;
1349 struct timeval tv;
1350 int timeout;
1352 if (nonblocking)
1353 timeout = 0;
1354 else {
1355 timeout = qemu_calculate_timeout();
1356 qemu_bh_update_timeout(&timeout);
1359 os_host_main_loop_wait(&timeout);
1361 /* poll any events */
1362 /* XXX: separate device handlers from system ones */
1363 nfds = -1;
1364 FD_ZERO(&rfds);
1365 FD_ZERO(&wfds);
1366 FD_ZERO(&xfds);
1367 QLIST_FOREACH(ioh, &io_handlers, next) {
1368 if (ioh->deleted)
1369 continue;
1370 if (ioh->fd_read &&
1371 (!ioh->fd_read_poll ||
1372 ioh->fd_read_poll(ioh->opaque) != 0)) {
1373 FD_SET(ioh->fd, &rfds);
1374 if (ioh->fd > nfds)
1375 nfds = ioh->fd;
1377 if (ioh->fd_write) {
1378 FD_SET(ioh->fd, &wfds);
1379 if (ioh->fd > nfds)
1380 nfds = ioh->fd;
1384 tv.tv_sec = timeout / 1000;
1385 tv.tv_usec = (timeout % 1000) * 1000;
1387 slirp_select_fill(&nfds, &rfds, &wfds, &xfds);
1389 qemu_mutex_unlock_iothread();
1390 ret = select(nfds + 1, &rfds, &wfds, &xfds, &tv);
1391 qemu_mutex_lock_iothread();
1392 if (ret > 0) {
1393 IOHandlerRecord *pioh;
1395 QLIST_FOREACH_SAFE(ioh, &io_handlers, next, pioh) {
1396 if (!ioh->deleted && ioh->fd_read && FD_ISSET(ioh->fd, &rfds)) {
1397 ioh->fd_read(ioh->opaque);
1399 if (!ioh->deleted && ioh->fd_write && FD_ISSET(ioh->fd, &wfds)) {
1400 ioh->fd_write(ioh->opaque);
1403 /* Do this last in case read/write handlers marked it for deletion */
1404 if (ioh->deleted) {
1405 QLIST_REMOVE(ioh, next);
1406 qemu_free(ioh);
1411 slirp_select_poll(&rfds, &wfds, &xfds, (ret < 0));
1413 qemu_run_all_timers();
1415 /* Check bottom-halves last in case any of the earlier events triggered
1416 them. */
1417 qemu_bh_poll();
1421 #ifndef CONFIG_IOTHREAD
1422 static int vm_request_pending(void)
1424 return powerdown_requested ||
1425 reset_requested ||
1426 shutdown_requested ||
1427 debug_requested ||
1428 vmstop_requested;
1430 #endif
1432 qemu_irq qemu_system_powerdown;
1434 static void main_loop(void)
1436 bool nonblocking = false;
1437 #ifdef CONFIG_PROFILER
1438 int64_t ti;
1439 #endif
1440 int r;
1442 qemu_main_loop_start();
1444 for (;;) {
1445 #ifndef CONFIG_IOTHREAD
1446 nonblocking = cpu_exec_all();
1447 if (vm_request_pending()) {
1448 nonblocking = true;
1450 #endif
1451 #ifdef CONFIG_PROFILER
1452 ti = profile_getclock();
1453 #endif
1454 main_loop_wait(nonblocking);
1455 #ifdef CONFIG_PROFILER
1456 dev_time += profile_getclock() - ti;
1457 #endif
1459 if (qemu_debug_requested()) {
1460 vm_stop(VMSTOP_DEBUG);
1462 if (qemu_shutdown_requested()) {
1463 qemu_kill_report();
1464 monitor_protocol_event(QEVENT_SHUTDOWN, NULL);
1465 if (no_shutdown) {
1466 vm_stop(VMSTOP_SHUTDOWN);
1467 no_shutdown = 0;
1468 } else
1469 break;
1471 if (qemu_reset_requested()) {
1472 pause_all_vcpus();
1473 cpu_synchronize_all_states();
1474 qemu_system_reset();
1475 resume_all_vcpus();
1477 if (qemu_powerdown_requested()) {
1478 monitor_protocol_event(QEVENT_POWERDOWN, NULL);
1479 qemu_irq_raise(qemu_system_powerdown);
1481 if ((r = qemu_vmstop_requested())) {
1482 vm_stop(r);
1485 bdrv_close_all();
1486 pause_all_vcpus();
1489 static void version(void)
1491 printf("QEMU emulator version " QEMU_VERSION QEMU_PKGVERSION ", Copyright (c) 2003-2008 Fabrice Bellard\n");
1494 static void help(int exitcode)
1496 const char *options_help =
1497 #define DEF(option, opt_arg, opt_enum, opt_help, arch_mask) \
1498 opt_help
1499 #define DEFHEADING(text) stringify(text) "\n"
1500 #include "qemu-options.def"
1501 #undef DEF
1502 #undef DEFHEADING
1503 #undef GEN_DOCS
1505 version();
1506 printf("usage: %s [options] [disk_image]\n"
1507 "\n"
1508 "'disk_image' is a raw hard disk image for IDE hard disk 0\n"
1509 "\n"
1510 "%s\n"
1511 "During emulation, the following keys are useful:\n"
1512 "ctrl-alt-f toggle full screen\n"
1513 "ctrl-alt-n switch to virtual console 'n'\n"
1514 "ctrl-alt toggle mouse and keyboard grab\n"
1515 "\n"
1516 "When using -nographic, press 'ctrl-a h' to get some help.\n",
1517 "qemu",
1518 options_help);
1519 exit(exitcode);
1522 #define HAS_ARG 0x0001
1524 typedef struct QEMUOption {
1525 const char *name;
1526 int flags;
1527 int index;
1528 uint32_t arch_mask;
1529 } QEMUOption;
1531 static const QEMUOption qemu_options[] = {
1532 { "h", 0, QEMU_OPTION_h, QEMU_ARCH_ALL },
1533 #define DEF(option, opt_arg, opt_enum, opt_help, arch_mask) \
1534 { option, opt_arg, opt_enum, arch_mask },
1535 #define DEFHEADING(text)
1536 #include "qemu-options.def"
1537 #undef DEF
1538 #undef DEFHEADING
1539 #undef GEN_DOCS
1540 { NULL },
1542 static void select_vgahw (const char *p)
1544 const char *opts;
1546 default_vga = 0;
1547 vga_interface_type = VGA_NONE;
1548 if (strstart(p, "std", &opts)) {
1549 vga_interface_type = VGA_STD;
1550 } else if (strstart(p, "cirrus", &opts)) {
1551 vga_interface_type = VGA_CIRRUS;
1552 } else if (strstart(p, "vmware", &opts)) {
1553 vga_interface_type = VGA_VMWARE;
1554 } else if (strstart(p, "xenfb", &opts)) {
1555 vga_interface_type = VGA_XENFB;
1556 } else if (strstart(p, "qxl", &opts)) {
1557 vga_interface_type = VGA_QXL;
1558 } else if (!strstart(p, "none", &opts)) {
1559 invalid_vga:
1560 fprintf(stderr, "Unknown vga type: %s\n", p);
1561 exit(1);
1563 while (*opts) {
1564 const char *nextopt;
1566 if (strstart(opts, ",retrace=", &nextopt)) {
1567 opts = nextopt;
1568 if (strstart(opts, "dumb", &nextopt))
1569 vga_retrace_method = VGA_RETRACE_DUMB;
1570 else if (strstart(opts, "precise", &nextopt))
1571 vga_retrace_method = VGA_RETRACE_PRECISE;
1572 else goto invalid_vga;
1573 } else goto invalid_vga;
1574 opts = nextopt;
1578 static DisplayType select_display(const char *p)
1580 const char *opts;
1581 DisplayType display = DT_DEFAULT;
1583 if (strstart(p, "sdl", &opts)) {
1584 #ifdef CONFIG_SDL
1585 display = DT_SDL;
1586 while (*opts) {
1587 const char *nextopt;
1589 if (strstart(opts, ",frame=", &nextopt)) {
1590 opts = nextopt;
1591 if (strstart(opts, "on", &nextopt)) {
1592 no_frame = 0;
1593 } else if (strstart(opts, "off", &nextopt)) {
1594 no_frame = 1;
1595 } else {
1596 goto invalid_sdl_args;
1598 } else if (strstart(opts, ",alt_grab=", &nextopt)) {
1599 opts = nextopt;
1600 if (strstart(opts, "on", &nextopt)) {
1601 alt_grab = 1;
1602 } else if (strstart(opts, "off", &nextopt)) {
1603 alt_grab = 0;
1604 } else {
1605 goto invalid_sdl_args;
1607 } else if (strstart(opts, ",ctrl_grab=", &nextopt)) {
1608 opts = nextopt;
1609 if (strstart(opts, "on", &nextopt)) {
1610 ctrl_grab = 1;
1611 } else if (strstart(opts, "off", &nextopt)) {
1612 ctrl_grab = 0;
1613 } else {
1614 goto invalid_sdl_args;
1616 } else if (strstart(opts, ",window_close=", &nextopt)) {
1617 opts = nextopt;
1618 if (strstart(opts, "on", &nextopt)) {
1619 no_quit = 0;
1620 } else if (strstart(opts, "off", &nextopt)) {
1621 no_quit = 1;
1622 } else {
1623 goto invalid_sdl_args;
1625 } else {
1626 invalid_sdl_args:
1627 fprintf(stderr, "Invalid SDL option string: %s\n", p);
1628 exit(1);
1630 opts = nextopt;
1632 #else
1633 fprintf(stderr, "SDL support is disabled\n");
1634 exit(1);
1635 #endif
1636 } else if (strstart(p, "vnc", &opts)) {
1637 #ifdef CONFIG_VNC
1638 display_remote++;
1640 if (*opts) {
1641 const char *nextopt;
1643 if (strstart(opts, "=", &nextopt)) {
1644 vnc_display = nextopt;
1647 if (!vnc_display) {
1648 fprintf(stderr, "VNC requires a display argument vnc=<display>\n");
1649 exit(1);
1651 #else
1652 fprintf(stderr, "VNC support is disabled\n");
1653 exit(1);
1654 #endif
1655 } else if (strstart(p, "curses", &opts)) {
1656 #ifdef CONFIG_CURSES
1657 display = DT_CURSES;
1658 #else
1659 fprintf(stderr, "Curses support is disabled\n");
1660 exit(1);
1661 #endif
1662 } else if (strstart(p, "none", &opts)) {
1663 display = DT_NONE;
1664 } else {
1665 fprintf(stderr, "Unknown display type: %s\n", p);
1666 exit(1);
1669 return display;
1672 static int balloon_parse(const char *arg)
1674 QemuOpts *opts;
1676 if (strcmp(arg, "none") == 0) {
1677 return 0;
1680 if (!strncmp(arg, "virtio", 6)) {
1681 if (arg[6] == ',') {
1682 /* have params -> parse them */
1683 opts = qemu_opts_parse(qemu_find_opts("device"), arg+7, 0);
1684 if (!opts)
1685 return -1;
1686 } else {
1687 /* create empty opts */
1688 opts = qemu_opts_create(qemu_find_opts("device"), NULL, 0);
1690 qemu_opt_set(opts, "driver", "virtio-balloon-pci");
1691 return 0;
1694 return -1;
1697 char *qemu_find_file(int type, const char *name)
1699 int len;
1700 const char *subdir;
1701 char *buf;
1703 /* If name contains path separators then try it as a straight path. */
1704 if ((strchr(name, '/') || strchr(name, '\\'))
1705 && access(name, R_OK) == 0) {
1706 return qemu_strdup(name);
1708 switch (type) {
1709 case QEMU_FILE_TYPE_BIOS:
1710 subdir = "";
1711 break;
1712 case QEMU_FILE_TYPE_KEYMAP:
1713 subdir = "keymaps/";
1714 break;
1715 default:
1716 abort();
1718 len = strlen(data_dir) + strlen(name) + strlen(subdir) + 2;
1719 buf = qemu_mallocz(len);
1720 snprintf(buf, len, "%s/%s%s", data_dir, subdir, name);
1721 if (access(buf, R_OK)) {
1722 qemu_free(buf);
1723 return NULL;
1725 return buf;
1728 static int device_help_func(QemuOpts *opts, void *opaque)
1730 return qdev_device_help(opts);
1733 static int device_init_func(QemuOpts *opts, void *opaque)
1735 DeviceState *dev;
1737 dev = qdev_device_add(opts);
1738 if (!dev)
1739 return -1;
1740 return 0;
1743 static int chardev_init_func(QemuOpts *opts, void *opaque)
1745 CharDriverState *chr;
1747 chr = qemu_chr_open_opts(opts, NULL);
1748 if (!chr)
1749 return -1;
1750 return 0;
1753 #ifdef CONFIG_VIRTFS
1754 static int fsdev_init_func(QemuOpts *opts, void *opaque)
1756 int ret;
1757 ret = qemu_fsdev_add(opts);
1759 return ret;
1761 #endif
1763 static int mon_init_func(QemuOpts *opts, void *opaque)
1765 CharDriverState *chr;
1766 const char *chardev;
1767 const char *mode;
1768 int flags;
1770 mode = qemu_opt_get(opts, "mode");
1771 if (mode == NULL) {
1772 mode = "readline";
1774 if (strcmp(mode, "readline") == 0) {
1775 flags = MONITOR_USE_READLINE;
1776 } else if (strcmp(mode, "control") == 0) {
1777 flags = MONITOR_USE_CONTROL;
1778 } else {
1779 fprintf(stderr, "unknown monitor mode \"%s\"\n", mode);
1780 exit(1);
1783 if (qemu_opt_get_bool(opts, "pretty", 0))
1784 flags |= MONITOR_USE_PRETTY;
1786 if (qemu_opt_get_bool(opts, "default", 0))
1787 flags |= MONITOR_IS_DEFAULT;
1789 chardev = qemu_opt_get(opts, "chardev");
1790 chr = qemu_chr_find(chardev);
1791 if (chr == NULL) {
1792 fprintf(stderr, "chardev \"%s\" not found\n", chardev);
1793 exit(1);
1796 monitor_init(chr, flags);
1797 return 0;
1800 static void monitor_parse(const char *optarg, const char *mode)
1802 static int monitor_device_index = 0;
1803 QemuOpts *opts;
1804 const char *p;
1805 char label[32];
1806 int def = 0;
1808 if (strstart(optarg, "chardev:", &p)) {
1809 snprintf(label, sizeof(label), "%s", p);
1810 } else {
1811 snprintf(label, sizeof(label), "compat_monitor%d",
1812 monitor_device_index);
1813 if (monitor_device_index == 0) {
1814 def = 1;
1816 opts = qemu_chr_parse_compat(label, optarg);
1817 if (!opts) {
1818 fprintf(stderr, "parse error: %s\n", optarg);
1819 exit(1);
1823 opts = qemu_opts_create(qemu_find_opts("mon"), label, 1);
1824 if (!opts) {
1825 fprintf(stderr, "duplicate chardev: %s\n", label);
1826 exit(1);
1828 qemu_opt_set(opts, "mode", mode);
1829 qemu_opt_set(opts, "chardev", label);
1830 if (def)
1831 qemu_opt_set(opts, "default", "on");
1832 monitor_device_index++;
1835 struct device_config {
1836 enum {
1837 DEV_USB, /* -usbdevice */
1838 DEV_BT, /* -bt */
1839 DEV_SERIAL, /* -serial */
1840 DEV_PARALLEL, /* -parallel */
1841 DEV_VIRTCON, /* -virtioconsole */
1842 DEV_DEBUGCON, /* -debugcon */
1843 } type;
1844 const char *cmdline;
1845 QTAILQ_ENTRY(device_config) next;
1847 QTAILQ_HEAD(, device_config) device_configs = QTAILQ_HEAD_INITIALIZER(device_configs);
1849 static void add_device_config(int type, const char *cmdline)
1851 struct device_config *conf;
1853 conf = qemu_mallocz(sizeof(*conf));
1854 conf->type = type;
1855 conf->cmdline = cmdline;
1856 QTAILQ_INSERT_TAIL(&device_configs, conf, next);
1859 static int foreach_device_config(int type, int (*func)(const char *cmdline))
1861 struct device_config *conf;
1862 int rc;
1864 QTAILQ_FOREACH(conf, &device_configs, next) {
1865 if (conf->type != type)
1866 continue;
1867 rc = func(conf->cmdline);
1868 if (0 != rc)
1869 return rc;
1871 return 0;
1874 static int serial_parse(const char *devname)
1876 static int index = 0;
1877 char label[32];
1879 if (strcmp(devname, "none") == 0)
1880 return 0;
1881 if (index == MAX_SERIAL_PORTS) {
1882 fprintf(stderr, "qemu: too many serial ports\n");
1883 exit(1);
1885 snprintf(label, sizeof(label), "serial%d", index);
1886 serial_hds[index] = qemu_chr_open(label, devname, NULL);
1887 if (!serial_hds[index]) {
1888 fprintf(stderr, "qemu: could not open serial device '%s': %s\n",
1889 devname, strerror(errno));
1890 return -1;
1892 index++;
1893 return 0;
1896 static int parallel_parse(const char *devname)
1898 static int index = 0;
1899 char label[32];
1901 if (strcmp(devname, "none") == 0)
1902 return 0;
1903 if (index == MAX_PARALLEL_PORTS) {
1904 fprintf(stderr, "qemu: too many parallel ports\n");
1905 exit(1);
1907 snprintf(label, sizeof(label), "parallel%d", index);
1908 parallel_hds[index] = qemu_chr_open(label, devname, NULL);
1909 if (!parallel_hds[index]) {
1910 fprintf(stderr, "qemu: could not open parallel device '%s': %s\n",
1911 devname, strerror(errno));
1912 return -1;
1914 index++;
1915 return 0;
1918 static int virtcon_parse(const char *devname)
1920 QemuOptsList *device = qemu_find_opts("device");
1921 static int index = 0;
1922 char label[32];
1923 QemuOpts *bus_opts, *dev_opts;
1925 if (strcmp(devname, "none") == 0)
1926 return 0;
1927 if (index == MAX_VIRTIO_CONSOLES) {
1928 fprintf(stderr, "qemu: too many virtio consoles\n");
1929 exit(1);
1932 bus_opts = qemu_opts_create(device, NULL, 0);
1933 qemu_opt_set(bus_opts, "driver", "virtio-serial");
1935 dev_opts = qemu_opts_create(device, NULL, 0);
1936 qemu_opt_set(dev_opts, "driver", "virtconsole");
1938 snprintf(label, sizeof(label), "virtcon%d", index);
1939 virtcon_hds[index] = qemu_chr_open(label, devname, NULL);
1940 if (!virtcon_hds[index]) {
1941 fprintf(stderr, "qemu: could not open virtio console '%s': %s\n",
1942 devname, strerror(errno));
1943 return -1;
1945 qemu_opt_set(dev_opts, "chardev", label);
1947 index++;
1948 return 0;
1951 static int debugcon_parse(const char *devname)
1953 QemuOpts *opts;
1955 if (!qemu_chr_open("debugcon", devname, NULL)) {
1956 exit(1);
1958 opts = qemu_opts_create(qemu_find_opts("device"), "debugcon", 1);
1959 if (!opts) {
1960 fprintf(stderr, "qemu: already have a debugcon device\n");
1961 exit(1);
1963 qemu_opt_set(opts, "driver", "isa-debugcon");
1964 qemu_opt_set(opts, "chardev", "debugcon");
1965 return 0;
1968 void qemu_add_exit_notifier(Notifier *notify)
1970 notifier_list_add(&exit_notifiers, notify);
1973 void qemu_remove_exit_notifier(Notifier *notify)
1975 notifier_list_remove(&exit_notifiers, notify);
1978 static void qemu_run_exit_notifiers(void)
1980 notifier_list_notify(&exit_notifiers);
1983 void qemu_add_machine_init_done_notifier(Notifier *notify)
1985 notifier_list_add(&machine_init_done_notifiers, notify);
1988 static void qemu_run_machine_init_done_notifiers(void)
1990 notifier_list_notify(&machine_init_done_notifiers);
1993 static const QEMUOption *lookup_opt(int argc, char **argv,
1994 const char **poptarg, int *poptind)
1996 const QEMUOption *popt;
1997 int optind = *poptind;
1998 char *r = argv[optind];
1999 const char *optarg;
2001 loc_set_cmdline(argv, optind, 1);
2002 optind++;
2003 /* Treat --foo the same as -foo. */
2004 if (r[1] == '-')
2005 r++;
2006 popt = qemu_options;
2007 for(;;) {
2008 if (!popt->name) {
2009 error_report("invalid option");
2010 exit(1);
2012 if (!strcmp(popt->name, r + 1))
2013 break;
2014 popt++;
2016 if (popt->flags & HAS_ARG) {
2017 if (optind >= argc) {
2018 error_report("requires an argument");
2019 exit(1);
2021 optarg = argv[optind++];
2022 loc_set_cmdline(argv, optind - 2, 2);
2023 } else {
2024 optarg = NULL;
2027 *poptarg = optarg;
2028 *poptind = optind;
2030 return popt;
2033 int main(int argc, char **argv, char **envp)
2035 const char *gdbstub_dev = NULL;
2036 int i;
2037 int snapshot, linux_boot;
2038 const char *icount_option = NULL;
2039 const char *initrd_filename;
2040 const char *kernel_filename, *kernel_cmdline;
2041 char boot_devices[33] = "cad"; /* default to HD->floppy->CD-ROM */
2042 DisplayState *ds;
2043 DisplayChangeListener *dcl;
2044 int cyls, heads, secs, translation;
2045 QemuOpts *hda_opts = NULL, *opts;
2046 QemuOptsList *olist;
2047 int optind;
2048 const char *optarg;
2049 const char *loadvm = NULL;
2050 QEMUMachine *machine;
2051 const char *cpu_model;
2052 int tb_size;
2053 const char *pid_file = NULL;
2054 const char *incoming = NULL;
2055 #ifdef CONFIG_VNC
2056 int show_vnc_port = 0;
2057 #endif
2058 int defconfig = 1;
2059 const char *trace_file = NULL;
2061 atexit(qemu_run_exit_notifiers);
2062 error_set_progname(argv[0]);
2064 init_clocks();
2066 qemu_cache_utils_init(envp);
2068 QLIST_INIT (&vm_change_state_head);
2069 os_setup_early_signal_handling();
2071 module_call_init(MODULE_INIT_MACHINE);
2072 machine = find_default_machine();
2073 cpu_model = NULL;
2074 initrd_filename = NULL;
2075 ram_size = 0;
2076 snapshot = 0;
2077 kernel_filename = NULL;
2078 kernel_cmdline = "";
2079 cyls = heads = secs = 0;
2080 translation = BIOS_ATA_TRANSLATION_AUTO;
2082 for (i = 0; i < MAX_NODES; i++) {
2083 node_mem[i] = 0;
2084 node_cpumask[i] = 0;
2087 nb_numa_nodes = 0;
2088 nb_nics = 0;
2090 tb_size = 0;
2091 autostart= 1;
2093 /* first pass of option parsing */
2094 optind = 1;
2095 while (optind < argc) {
2096 if (argv[optind][0] != '-') {
2097 /* disk image */
2098 optind++;
2099 continue;
2100 } else {
2101 const QEMUOption *popt;
2103 popt = lookup_opt(argc, argv, &optarg, &optind);
2104 switch (popt->index) {
2105 case QEMU_OPTION_nodefconfig:
2106 defconfig=0;
2107 break;
2112 if (defconfig) {
2113 int ret;
2115 ret = qemu_read_config_file(CONFIG_QEMU_CONFDIR "/qemu.conf");
2116 if (ret < 0 && ret != -ENOENT) {
2117 exit(1);
2120 ret = qemu_read_config_file(arch_config_name);
2121 if (ret < 0 && ret != -ENOENT) {
2122 exit(1);
2125 cpudef_init();
2127 /* second pass of option parsing */
2128 optind = 1;
2129 for(;;) {
2130 if (optind >= argc)
2131 break;
2132 if (argv[optind][0] != '-') {
2133 hda_opts = drive_add(IF_DEFAULT, 0, argv[optind++], HD_OPTS);
2134 } else {
2135 const QEMUOption *popt;
2137 popt = lookup_opt(argc, argv, &optarg, &optind);
2138 if (!(popt->arch_mask & arch_type)) {
2139 printf("Option %s not supported for this target\n", popt->name);
2140 exit(1);
2142 switch(popt->index) {
2143 case QEMU_OPTION_M:
2144 machine = find_machine(optarg);
2145 if (!machine) {
2146 QEMUMachine *m;
2147 printf("Supported machines are:\n");
2148 for(m = first_machine; m != NULL; m = m->next) {
2149 if (m->alias)
2150 printf("%-10s %s (alias of %s)\n",
2151 m->alias, m->desc, m->name);
2152 printf("%-10s %s%s\n",
2153 m->name, m->desc,
2154 m->is_default ? " (default)" : "");
2156 exit(*optarg != '?');
2158 break;
2159 case QEMU_OPTION_cpu:
2160 /* hw initialization will check this */
2161 if (*optarg == '?') {
2162 list_cpus(stdout, &fprintf, optarg);
2163 exit(0);
2164 } else {
2165 cpu_model = optarg;
2167 break;
2168 case QEMU_OPTION_initrd:
2169 initrd_filename = optarg;
2170 break;
2171 case QEMU_OPTION_hda:
2173 char buf[256];
2174 if (cyls == 0)
2175 snprintf(buf, sizeof(buf), "%s", HD_OPTS);
2176 else
2177 snprintf(buf, sizeof(buf),
2178 "%s,cyls=%d,heads=%d,secs=%d%s",
2179 HD_OPTS , cyls, heads, secs,
2180 translation == BIOS_ATA_TRANSLATION_LBA ?
2181 ",trans=lba" :
2182 translation == BIOS_ATA_TRANSLATION_NONE ?
2183 ",trans=none" : "");
2184 drive_add(IF_DEFAULT, 0, optarg, buf);
2185 break;
2187 case QEMU_OPTION_hdb:
2188 case QEMU_OPTION_hdc:
2189 case QEMU_OPTION_hdd:
2190 drive_add(IF_DEFAULT, popt->index - QEMU_OPTION_hda, optarg,
2191 HD_OPTS);
2192 break;
2193 case QEMU_OPTION_drive:
2194 drive_def(optarg);
2195 break;
2196 case QEMU_OPTION_set:
2197 if (qemu_set_option(optarg) != 0)
2198 exit(1);
2199 break;
2200 case QEMU_OPTION_global:
2201 if (qemu_global_option(optarg) != 0)
2202 exit(1);
2203 break;
2204 case QEMU_OPTION_mtdblock:
2205 drive_add(IF_MTD, -1, optarg, MTD_OPTS);
2206 break;
2207 case QEMU_OPTION_sd:
2208 drive_add(IF_SD, 0, optarg, SD_OPTS);
2209 break;
2210 case QEMU_OPTION_pflash:
2211 drive_add(IF_PFLASH, -1, optarg, PFLASH_OPTS);
2212 break;
2213 case QEMU_OPTION_snapshot:
2214 snapshot = 1;
2215 break;
2216 case QEMU_OPTION_hdachs:
2218 const char *p;
2219 p = optarg;
2220 cyls = strtol(p, (char **)&p, 0);
2221 if (cyls < 1 || cyls > 16383)
2222 goto chs_fail;
2223 if (*p != ',')
2224 goto chs_fail;
2225 p++;
2226 heads = strtol(p, (char **)&p, 0);
2227 if (heads < 1 || heads > 16)
2228 goto chs_fail;
2229 if (*p != ',')
2230 goto chs_fail;
2231 p++;
2232 secs = strtol(p, (char **)&p, 0);
2233 if (secs < 1 || secs > 63)
2234 goto chs_fail;
2235 if (*p == ',') {
2236 p++;
2237 if (!strcmp(p, "none"))
2238 translation = BIOS_ATA_TRANSLATION_NONE;
2239 else if (!strcmp(p, "lba"))
2240 translation = BIOS_ATA_TRANSLATION_LBA;
2241 else if (!strcmp(p, "auto"))
2242 translation = BIOS_ATA_TRANSLATION_AUTO;
2243 else
2244 goto chs_fail;
2245 } else if (*p != '\0') {
2246 chs_fail:
2247 fprintf(stderr, "qemu: invalid physical CHS format\n");
2248 exit(1);
2250 if (hda_opts != NULL) {
2251 char num[16];
2252 snprintf(num, sizeof(num), "%d", cyls);
2253 qemu_opt_set(hda_opts, "cyls", num);
2254 snprintf(num, sizeof(num), "%d", heads);
2255 qemu_opt_set(hda_opts, "heads", num);
2256 snprintf(num, sizeof(num), "%d", secs);
2257 qemu_opt_set(hda_opts, "secs", num);
2258 if (translation == BIOS_ATA_TRANSLATION_LBA)
2259 qemu_opt_set(hda_opts, "trans", "lba");
2260 if (translation == BIOS_ATA_TRANSLATION_NONE)
2261 qemu_opt_set(hda_opts, "trans", "none");
2264 break;
2265 case QEMU_OPTION_numa:
2266 if (nb_numa_nodes >= MAX_NODES) {
2267 fprintf(stderr, "qemu: too many NUMA nodes\n");
2268 exit(1);
2270 numa_add(optarg);
2271 break;
2272 case QEMU_OPTION_display:
2273 display_type = select_display(optarg);
2274 break;
2275 case QEMU_OPTION_nographic:
2276 display_type = DT_NOGRAPHIC;
2277 break;
2278 case QEMU_OPTION_curses:
2279 #ifdef CONFIG_CURSES
2280 display_type = DT_CURSES;
2281 #else
2282 fprintf(stderr, "Curses support is disabled\n");
2283 exit(1);
2284 #endif
2285 break;
2286 case QEMU_OPTION_portrait:
2287 graphic_rotate = 1;
2288 break;
2289 case QEMU_OPTION_kernel:
2290 kernel_filename = optarg;
2291 break;
2292 case QEMU_OPTION_append:
2293 kernel_cmdline = optarg;
2294 break;
2295 case QEMU_OPTION_cdrom:
2296 drive_add(IF_DEFAULT, 2, optarg, CDROM_OPTS);
2297 break;
2298 case QEMU_OPTION_boot:
2300 static const char * const params[] = {
2301 "order", "once", "menu", NULL
2303 char buf[sizeof(boot_devices)];
2304 char *standard_boot_devices;
2305 int legacy = 0;
2307 if (!strchr(optarg, '=')) {
2308 legacy = 1;
2309 pstrcpy(buf, sizeof(buf), optarg);
2310 } else if (check_params(buf, sizeof(buf), params, optarg) < 0) {
2311 fprintf(stderr,
2312 "qemu: unknown boot parameter '%s' in '%s'\n",
2313 buf, optarg);
2314 exit(1);
2317 if (legacy ||
2318 get_param_value(buf, sizeof(buf), "order", optarg)) {
2319 validate_bootdevices(buf);
2320 pstrcpy(boot_devices, sizeof(boot_devices), buf);
2322 if (!legacy) {
2323 if (get_param_value(buf, sizeof(buf),
2324 "once", optarg)) {
2325 validate_bootdevices(buf);
2326 standard_boot_devices = qemu_strdup(boot_devices);
2327 pstrcpy(boot_devices, sizeof(boot_devices), buf);
2328 qemu_register_reset(restore_boot_devices,
2329 standard_boot_devices);
2331 if (get_param_value(buf, sizeof(buf),
2332 "menu", optarg)) {
2333 if (!strcmp(buf, "on")) {
2334 boot_menu = 1;
2335 } else if (!strcmp(buf, "off")) {
2336 boot_menu = 0;
2337 } else {
2338 fprintf(stderr,
2339 "qemu: invalid option value '%s'\n",
2340 buf);
2341 exit(1);
2346 break;
2347 case QEMU_OPTION_fda:
2348 case QEMU_OPTION_fdb:
2349 drive_add(IF_FLOPPY, popt->index - QEMU_OPTION_fda,
2350 optarg, FD_OPTS);
2351 break;
2352 case QEMU_OPTION_no_fd_bootchk:
2353 fd_bootchk = 0;
2354 break;
2355 case QEMU_OPTION_netdev:
2356 if (net_client_parse(qemu_find_opts("netdev"), optarg) == -1) {
2357 exit(1);
2359 break;
2360 case QEMU_OPTION_net:
2361 if (net_client_parse(qemu_find_opts("net"), optarg) == -1) {
2362 exit(1);
2364 break;
2365 #ifdef CONFIG_SLIRP
2366 case QEMU_OPTION_tftp:
2367 legacy_tftp_prefix = optarg;
2368 break;
2369 case QEMU_OPTION_bootp:
2370 legacy_bootp_filename = optarg;
2371 break;
2372 case QEMU_OPTION_redir:
2373 if (net_slirp_redir(optarg) < 0)
2374 exit(1);
2375 break;
2376 #endif
2377 case QEMU_OPTION_bt:
2378 add_device_config(DEV_BT, optarg);
2379 break;
2380 case QEMU_OPTION_audio_help:
2381 if (!(audio_available())) {
2382 printf("Option %s not supported for this target\n", popt->name);
2383 exit(1);
2385 AUD_help ();
2386 exit (0);
2387 break;
2388 case QEMU_OPTION_soundhw:
2389 if (!(audio_available())) {
2390 printf("Option %s not supported for this target\n", popt->name);
2391 exit(1);
2393 select_soundhw (optarg);
2394 break;
2395 case QEMU_OPTION_h:
2396 help(0);
2397 break;
2398 case QEMU_OPTION_version:
2399 version();
2400 exit(0);
2401 break;
2402 case QEMU_OPTION_m: {
2403 int64_t value;
2405 value = strtosz(optarg, NULL);
2406 if (value < 0) {
2407 fprintf(stderr, "qemu: invalid ram size: %s\n", optarg);
2408 exit(1);
2411 /* On 32-bit hosts, QEMU is limited by virtual address space */
2412 if (value > (2047 << 20) && HOST_LONG_BITS == 32) {
2413 fprintf(stderr, "qemu: at most 2047 MB RAM can be simulated\n");
2414 exit(1);
2416 if (value != (uint64_t)(ram_addr_t)value) {
2417 fprintf(stderr, "qemu: ram size too large\n");
2418 exit(1);
2420 ram_size = value;
2421 break;
2423 case QEMU_OPTION_mempath:
2424 mem_path = optarg;
2425 break;
2426 #ifdef MAP_POPULATE
2427 case QEMU_OPTION_mem_prealloc:
2428 mem_prealloc = 1;
2429 break;
2430 #endif
2431 case QEMU_OPTION_d:
2432 set_cpu_log(optarg);
2433 break;
2434 case QEMU_OPTION_s:
2435 gdbstub_dev = "tcp::" DEFAULT_GDBSTUB_PORT;
2436 break;
2437 case QEMU_OPTION_gdb:
2438 gdbstub_dev = optarg;
2439 break;
2440 case QEMU_OPTION_L:
2441 data_dir = optarg;
2442 break;
2443 case QEMU_OPTION_bios:
2444 bios_name = optarg;
2445 break;
2446 case QEMU_OPTION_singlestep:
2447 singlestep = 1;
2448 break;
2449 case QEMU_OPTION_S:
2450 autostart = 0;
2451 break;
2452 case QEMU_OPTION_k:
2453 keyboard_layout = optarg;
2454 break;
2455 case QEMU_OPTION_localtime:
2456 rtc_utc = 0;
2457 break;
2458 case QEMU_OPTION_vga:
2459 select_vgahw (optarg);
2460 break;
2461 case QEMU_OPTION_g:
2463 const char *p;
2464 int w, h, depth;
2465 p = optarg;
2466 w = strtol(p, (char **)&p, 10);
2467 if (w <= 0) {
2468 graphic_error:
2469 fprintf(stderr, "qemu: invalid resolution or depth\n");
2470 exit(1);
2472 if (*p != 'x')
2473 goto graphic_error;
2474 p++;
2475 h = strtol(p, (char **)&p, 10);
2476 if (h <= 0)
2477 goto graphic_error;
2478 if (*p == 'x') {
2479 p++;
2480 depth = strtol(p, (char **)&p, 10);
2481 if (depth != 8 && depth != 15 && depth != 16 &&
2482 depth != 24 && depth != 32)
2483 goto graphic_error;
2484 } else if (*p == '\0') {
2485 depth = graphic_depth;
2486 } else {
2487 goto graphic_error;
2490 graphic_width = w;
2491 graphic_height = h;
2492 graphic_depth = depth;
2494 break;
2495 case QEMU_OPTION_echr:
2497 char *r;
2498 term_escape_char = strtol(optarg, &r, 0);
2499 if (r == optarg)
2500 printf("Bad argument to echr\n");
2501 break;
2503 case QEMU_OPTION_monitor:
2504 monitor_parse(optarg, "readline");
2505 default_monitor = 0;
2506 break;
2507 case QEMU_OPTION_qmp:
2508 monitor_parse(optarg, "control");
2509 default_monitor = 0;
2510 break;
2511 case QEMU_OPTION_mon:
2512 opts = qemu_opts_parse(qemu_find_opts("mon"), optarg, 1);
2513 if (!opts) {
2514 exit(1);
2516 default_monitor = 0;
2517 break;
2518 case QEMU_OPTION_chardev:
2519 opts = qemu_opts_parse(qemu_find_opts("chardev"), optarg, 1);
2520 if (!opts) {
2521 exit(1);
2523 break;
2524 case QEMU_OPTION_fsdev:
2525 olist = qemu_find_opts("fsdev");
2526 if (!olist) {
2527 fprintf(stderr, "fsdev is not supported by this qemu build.\n");
2528 exit(1);
2530 opts = qemu_opts_parse(olist, optarg, 1);
2531 if (!opts) {
2532 fprintf(stderr, "parse error: %s\n", optarg);
2533 exit(1);
2535 break;
2536 case QEMU_OPTION_virtfs: {
2537 char *arg_fsdev = NULL;
2538 char *arg_9p = NULL;
2539 int len = 0;
2541 olist = qemu_find_opts("virtfs");
2542 if (!olist) {
2543 fprintf(stderr, "virtfs is not supported by this qemu build.\n");
2544 exit(1);
2546 opts = qemu_opts_parse(olist, optarg, 1);
2547 if (!opts) {
2548 fprintf(stderr, "parse error: %s\n", optarg);
2549 exit(1);
2552 if (qemu_opt_get(opts, "fstype") == NULL ||
2553 qemu_opt_get(opts, "mount_tag") == NULL ||
2554 qemu_opt_get(opts, "path") == NULL ||
2555 qemu_opt_get(opts, "security_model") == NULL) {
2556 fprintf(stderr, "Usage: -virtfs fstype,path=/share_path/,"
2557 "security_model=[mapped|passthrough|none],"
2558 "mnt_tag=tag.\n");
2559 exit(1);
2562 len = strlen(",id=,path=,security_model=");
2563 len += strlen(qemu_opt_get(opts, "fstype"));
2564 len += strlen(qemu_opt_get(opts, "mount_tag"));
2565 len += strlen(qemu_opt_get(opts, "path"));
2566 len += strlen(qemu_opt_get(opts, "security_model"));
2567 arg_fsdev = qemu_malloc((len + 1) * sizeof(*arg_fsdev));
2569 snprintf(arg_fsdev, (len + 1) * sizeof(*arg_fsdev),
2570 "%s,id=%s,path=%s,security_model=%s",
2571 qemu_opt_get(opts, "fstype"),
2572 qemu_opt_get(opts, "mount_tag"),
2573 qemu_opt_get(opts, "path"),
2574 qemu_opt_get(opts, "security_model"));
2576 len = strlen("virtio-9p-pci,fsdev=,mount_tag=");
2577 len += 2*strlen(qemu_opt_get(opts, "mount_tag"));
2578 arg_9p = qemu_malloc((len + 1) * sizeof(*arg_9p));
2580 snprintf(arg_9p, (len + 1) * sizeof(*arg_9p),
2581 "virtio-9p-pci,fsdev=%s,mount_tag=%s",
2582 qemu_opt_get(opts, "mount_tag"),
2583 qemu_opt_get(opts, "mount_tag"));
2585 if (!qemu_opts_parse(qemu_find_opts("fsdev"), arg_fsdev, 1)) {
2586 fprintf(stderr, "parse error [fsdev]: %s\n", optarg);
2587 exit(1);
2590 if (!qemu_opts_parse(qemu_find_opts("device"), arg_9p, 1)) {
2591 fprintf(stderr, "parse error [device]: %s\n", optarg);
2592 exit(1);
2595 qemu_free(arg_fsdev);
2596 qemu_free(arg_9p);
2597 break;
2599 case QEMU_OPTION_serial:
2600 add_device_config(DEV_SERIAL, optarg);
2601 default_serial = 0;
2602 if (strncmp(optarg, "mon:", 4) == 0) {
2603 default_monitor = 0;
2605 break;
2606 case QEMU_OPTION_watchdog:
2607 if (watchdog) {
2608 fprintf(stderr,
2609 "qemu: only one watchdog option may be given\n");
2610 return 1;
2612 watchdog = optarg;
2613 break;
2614 case QEMU_OPTION_watchdog_action:
2615 if (select_watchdog_action(optarg) == -1) {
2616 fprintf(stderr, "Unknown -watchdog-action parameter\n");
2617 exit(1);
2619 break;
2620 case QEMU_OPTION_virtiocon:
2621 add_device_config(DEV_VIRTCON, optarg);
2622 default_virtcon = 0;
2623 if (strncmp(optarg, "mon:", 4) == 0) {
2624 default_monitor = 0;
2626 break;
2627 case QEMU_OPTION_parallel:
2628 add_device_config(DEV_PARALLEL, optarg);
2629 default_parallel = 0;
2630 if (strncmp(optarg, "mon:", 4) == 0) {
2631 default_monitor = 0;
2633 break;
2634 case QEMU_OPTION_debugcon:
2635 add_device_config(DEV_DEBUGCON, optarg);
2636 break;
2637 case QEMU_OPTION_loadvm:
2638 loadvm = optarg;
2639 break;
2640 case QEMU_OPTION_full_screen:
2641 full_screen = 1;
2642 break;
2643 #ifdef CONFIG_SDL
2644 case QEMU_OPTION_no_frame:
2645 no_frame = 1;
2646 break;
2647 case QEMU_OPTION_alt_grab:
2648 alt_grab = 1;
2649 break;
2650 case QEMU_OPTION_ctrl_grab:
2651 ctrl_grab = 1;
2652 break;
2653 case QEMU_OPTION_no_quit:
2654 no_quit = 1;
2655 break;
2656 case QEMU_OPTION_sdl:
2657 display_type = DT_SDL;
2658 break;
2659 #else
2660 case QEMU_OPTION_no_frame:
2661 case QEMU_OPTION_alt_grab:
2662 case QEMU_OPTION_ctrl_grab:
2663 case QEMU_OPTION_no_quit:
2664 case QEMU_OPTION_sdl:
2665 fprintf(stderr, "SDL support is disabled\n");
2666 exit(1);
2667 #endif
2668 case QEMU_OPTION_pidfile:
2669 pid_file = optarg;
2670 break;
2671 case QEMU_OPTION_win2k_hack:
2672 win2k_install_hack = 1;
2673 break;
2674 case QEMU_OPTION_rtc_td_hack:
2675 rtc_td_hack = 1;
2676 break;
2677 case QEMU_OPTION_acpitable:
2678 do_acpitable_option(optarg);
2679 break;
2680 case QEMU_OPTION_smbios:
2681 do_smbios_option(optarg);
2682 break;
2683 case QEMU_OPTION_enable_kvm:
2684 kvm_allowed = 1;
2685 break;
2686 case QEMU_OPTION_usb:
2687 usb_enabled = 1;
2688 break;
2689 case QEMU_OPTION_usbdevice:
2690 usb_enabled = 1;
2691 add_device_config(DEV_USB, optarg);
2692 break;
2693 case QEMU_OPTION_device:
2694 if (!qemu_opts_parse(qemu_find_opts("device"), optarg, 1)) {
2695 exit(1);
2697 break;
2698 case QEMU_OPTION_smp:
2699 smp_parse(optarg);
2700 if (smp_cpus < 1) {
2701 fprintf(stderr, "Invalid number of CPUs\n");
2702 exit(1);
2704 if (max_cpus < smp_cpus) {
2705 fprintf(stderr, "maxcpus must be equal to or greater than "
2706 "smp\n");
2707 exit(1);
2709 if (max_cpus > 255) {
2710 fprintf(stderr, "Unsupported number of maxcpus\n");
2711 exit(1);
2713 break;
2714 case QEMU_OPTION_vnc:
2715 #ifdef CONFIG_VNC
2716 display_remote++;
2717 vnc_display = optarg;
2718 #else
2719 fprintf(stderr, "VNC support is disabled\n");
2720 exit(1);
2721 #endif
2722 break;
2723 case QEMU_OPTION_no_acpi:
2724 acpi_enabled = 0;
2725 break;
2726 case QEMU_OPTION_no_hpet:
2727 no_hpet = 1;
2728 break;
2729 case QEMU_OPTION_balloon:
2730 if (balloon_parse(optarg) < 0) {
2731 fprintf(stderr, "Unknown -balloon argument %s\n", optarg);
2732 exit(1);
2734 break;
2735 case QEMU_OPTION_no_reboot:
2736 no_reboot = 1;
2737 break;
2738 case QEMU_OPTION_no_shutdown:
2739 no_shutdown = 1;
2740 break;
2741 case QEMU_OPTION_show_cursor:
2742 cursor_hide = 0;
2743 break;
2744 case QEMU_OPTION_uuid:
2745 if(qemu_uuid_parse(optarg, qemu_uuid) < 0) {
2746 fprintf(stderr, "Fail to parse UUID string."
2747 " Wrong format.\n");
2748 exit(1);
2750 break;
2751 case QEMU_OPTION_option_rom:
2752 if (nb_option_roms >= MAX_OPTION_ROMS) {
2753 fprintf(stderr, "Too many option ROMs\n");
2754 exit(1);
2756 opts = qemu_opts_parse(qemu_find_opts("option-rom"), optarg, 1);
2757 option_rom[nb_option_roms].name = qemu_opt_get(opts, "romfile");
2758 option_rom[nb_option_roms].bootindex =
2759 qemu_opt_get_number(opts, "bootindex", -1);
2760 if (!option_rom[nb_option_roms].name) {
2761 fprintf(stderr, "Option ROM file is not specified\n");
2762 exit(1);
2764 nb_option_roms++;
2765 break;
2766 case QEMU_OPTION_semihosting:
2767 semihosting_enabled = 1;
2768 break;
2769 case QEMU_OPTION_name:
2770 qemu_name = qemu_strdup(optarg);
2772 char *p = strchr(qemu_name, ',');
2773 if (p != NULL) {
2774 *p++ = 0;
2775 if (strncmp(p, "process=", 8)) {
2776 fprintf(stderr, "Unknown subargument %s to -name\n", p);
2777 exit(1);
2779 p += 8;
2780 os_set_proc_name(p);
2783 break;
2784 case QEMU_OPTION_prom_env:
2785 if (nb_prom_envs >= MAX_PROM_ENVS) {
2786 fprintf(stderr, "Too many prom variables\n");
2787 exit(1);
2789 prom_envs[nb_prom_envs] = optarg;
2790 nb_prom_envs++;
2791 break;
2792 case QEMU_OPTION_old_param:
2793 old_param = 1;
2794 break;
2795 case QEMU_OPTION_clock:
2796 configure_alarms(optarg);
2797 break;
2798 case QEMU_OPTION_startdate:
2799 configure_rtc_date_offset(optarg, 1);
2800 break;
2801 case QEMU_OPTION_rtc:
2802 opts = qemu_opts_parse(qemu_find_opts("rtc"), optarg, 0);
2803 if (!opts) {
2804 exit(1);
2806 configure_rtc(opts);
2807 break;
2808 case QEMU_OPTION_tb_size:
2809 tb_size = strtol(optarg, NULL, 0);
2810 if (tb_size < 0)
2811 tb_size = 0;
2812 break;
2813 case QEMU_OPTION_icount:
2814 icount_option = optarg;
2815 break;
2816 case QEMU_OPTION_incoming:
2817 incoming = optarg;
2818 incoming_expected = true;
2819 break;
2820 case QEMU_OPTION_nodefaults:
2821 default_serial = 0;
2822 default_parallel = 0;
2823 default_virtcon = 0;
2824 default_monitor = 0;
2825 default_vga = 0;
2826 default_net = 0;
2827 default_floppy = 0;
2828 default_cdrom = 0;
2829 default_sdcard = 0;
2830 break;
2831 case QEMU_OPTION_xen_domid:
2832 if (!(xen_available())) {
2833 printf("Option %s not supported for this target\n", popt->name);
2834 exit(1);
2836 xen_domid = atoi(optarg);
2837 break;
2838 case QEMU_OPTION_xen_create:
2839 if (!(xen_available())) {
2840 printf("Option %s not supported for this target\n", popt->name);
2841 exit(1);
2843 xen_mode = XEN_CREATE;
2844 break;
2845 case QEMU_OPTION_xen_attach:
2846 if (!(xen_available())) {
2847 printf("Option %s not supported for this target\n", popt->name);
2848 exit(1);
2850 xen_mode = XEN_ATTACH;
2851 break;
2852 #ifdef CONFIG_SIMPLE_TRACE
2853 case QEMU_OPTION_trace:
2854 opts = qemu_opts_parse(qemu_find_opts("trace"), optarg, 0);
2855 if (opts) {
2856 trace_file = qemu_opt_get(opts, "file");
2858 break;
2859 #endif
2860 case QEMU_OPTION_readconfig:
2862 int ret = qemu_read_config_file(optarg);
2863 if (ret < 0) {
2864 fprintf(stderr, "read config %s: %s\n", optarg,
2865 strerror(-ret));
2866 exit(1);
2868 break;
2870 case QEMU_OPTION_spice:
2871 olist = qemu_find_opts("spice");
2872 if (!olist) {
2873 fprintf(stderr, "spice is not supported by this qemu build.\n");
2874 exit(1);
2876 opts = qemu_opts_parse(olist, optarg, 0);
2877 if (!opts) {
2878 fprintf(stderr, "parse error: %s\n", optarg);
2879 exit(1);
2881 break;
2882 case QEMU_OPTION_writeconfig:
2884 FILE *fp;
2885 if (strcmp(optarg, "-") == 0) {
2886 fp = stdout;
2887 } else {
2888 fp = fopen(optarg, "w");
2889 if (fp == NULL) {
2890 fprintf(stderr, "open %s: %s\n", optarg, strerror(errno));
2891 exit(1);
2894 qemu_config_write(fp);
2895 fclose(fp);
2896 break;
2898 default:
2899 os_parse_cmd_args(popt->index, optarg);
2903 loc_set_none();
2905 if (!st_init(trace_file)) {
2906 fprintf(stderr, "warning: unable to initialize simple trace backend\n");
2909 /* If no data_dir is specified then try to find it relative to the
2910 executable path. */
2911 if (!data_dir) {
2912 data_dir = os_find_datadir(argv[0]);
2914 /* If all else fails use the install patch specified when building. */
2915 if (!data_dir) {
2916 data_dir = CONFIG_QEMU_DATADIR;
2920 * Default to max_cpus = smp_cpus, in case the user doesn't
2921 * specify a max_cpus value.
2923 if (!max_cpus)
2924 max_cpus = smp_cpus;
2926 machine->max_cpus = machine->max_cpus ?: 1; /* Default to UP */
2927 if (smp_cpus > machine->max_cpus) {
2928 fprintf(stderr, "Number of SMP cpus requested (%d), exceeds max cpus "
2929 "supported by machine `%s' (%d)\n", smp_cpus, machine->name,
2930 machine->max_cpus);
2931 exit(1);
2934 qemu_opts_foreach(qemu_find_opts("device"), default_driver_check, NULL, 0);
2935 qemu_opts_foreach(qemu_find_opts("global"), default_driver_check, NULL, 0);
2937 if (machine->no_serial) {
2938 default_serial = 0;
2940 if (machine->no_parallel) {
2941 default_parallel = 0;
2943 if (!machine->use_virtcon) {
2944 default_virtcon = 0;
2946 if (machine->no_vga) {
2947 default_vga = 0;
2949 if (machine->no_floppy) {
2950 default_floppy = 0;
2952 if (machine->no_cdrom) {
2953 default_cdrom = 0;
2955 if (machine->no_sdcard) {
2956 default_sdcard = 0;
2959 if (display_type == DT_NOGRAPHIC) {
2960 if (default_parallel)
2961 add_device_config(DEV_PARALLEL, "null");
2962 if (default_serial && default_monitor) {
2963 add_device_config(DEV_SERIAL, "mon:stdio");
2964 } else if (default_virtcon && default_monitor) {
2965 add_device_config(DEV_VIRTCON, "mon:stdio");
2966 } else {
2967 if (default_serial)
2968 add_device_config(DEV_SERIAL, "stdio");
2969 if (default_virtcon)
2970 add_device_config(DEV_VIRTCON, "stdio");
2971 if (default_monitor)
2972 monitor_parse("stdio", "readline");
2974 } else {
2975 if (default_serial)
2976 add_device_config(DEV_SERIAL, "vc:80Cx24C");
2977 if (default_parallel)
2978 add_device_config(DEV_PARALLEL, "vc:80Cx24C");
2979 if (default_monitor)
2980 monitor_parse("vc:80Cx24C", "readline");
2981 if (default_virtcon)
2982 add_device_config(DEV_VIRTCON, "vc:80Cx24C");
2984 if (default_vga)
2985 vga_interface_type = VGA_CIRRUS;
2987 socket_init();
2989 if (qemu_opts_foreach(qemu_find_opts("chardev"), chardev_init_func, NULL, 1) != 0)
2990 exit(1);
2991 #ifdef CONFIG_VIRTFS
2992 if (qemu_opts_foreach(qemu_find_opts("fsdev"), fsdev_init_func, NULL, 1) != 0) {
2993 exit(1);
2995 #endif
2997 os_daemonize();
2999 if (pid_file && qemu_create_pidfile(pid_file) != 0) {
3000 os_pidfile_error();
3001 exit(1);
3004 if (kvm_allowed) {
3005 int ret = kvm_init();
3006 if (ret < 0) {
3007 if (!kvm_available()) {
3008 printf("KVM not supported for this target\n");
3009 } else {
3010 fprintf(stderr, "failed to initialize KVM: %s\n", strerror(-ret));
3012 exit(1);
3016 if (qemu_init_main_loop()) {
3017 fprintf(stderr, "qemu_init_main_loop failed\n");
3018 exit(1);
3020 linux_boot = (kernel_filename != NULL);
3022 if (!linux_boot && *kernel_cmdline != '\0') {
3023 fprintf(stderr, "-append only allowed with -kernel option\n");
3024 exit(1);
3027 if (!linux_boot && initrd_filename != NULL) {
3028 fprintf(stderr, "-initrd only allowed with -kernel option\n");
3029 exit(1);
3032 os_set_line_buffering();
3034 if (init_timer_alarm() < 0) {
3035 fprintf(stderr, "could not initialize alarm timer\n");
3036 exit(1);
3038 configure_icount(icount_option);
3040 if (net_init_clients() < 0) {
3041 exit(1);
3044 /* init the bluetooth world */
3045 if (foreach_device_config(DEV_BT, bt_parse))
3046 exit(1);
3048 /* init the memory */
3049 if (ram_size == 0)
3050 ram_size = DEFAULT_RAM_SIZE * 1024 * 1024;
3052 /* init the dynamic translator */
3053 cpu_exec_init_all(tb_size * 1024 * 1024);
3055 bdrv_init_with_whitelist();
3057 blk_mig_init();
3059 /* open the virtual block devices */
3060 if (snapshot)
3061 qemu_opts_foreach(qemu_find_opts("drive"), drive_enable_snapshot, NULL, 0);
3062 if (qemu_opts_foreach(qemu_find_opts("drive"), drive_init_func, &machine->use_scsi, 1) != 0)
3063 exit(1);
3065 default_drive(default_cdrom, snapshot, machine->use_scsi,
3066 IF_DEFAULT, 2, CDROM_OPTS);
3067 default_drive(default_floppy, snapshot, machine->use_scsi,
3068 IF_FLOPPY, 0, FD_OPTS);
3069 default_drive(default_sdcard, snapshot, machine->use_scsi,
3070 IF_SD, 0, SD_OPTS);
3072 register_savevm_live(NULL, "ram", 0, 4, NULL, ram_save_live, NULL,
3073 ram_load, NULL);
3075 if (nb_numa_nodes > 0) {
3076 int i;
3078 if (nb_numa_nodes > smp_cpus) {
3079 nb_numa_nodes = smp_cpus;
3082 /* If no memory size if given for any node, assume the default case
3083 * and distribute the available memory equally across all nodes
3085 for (i = 0; i < nb_numa_nodes; i++) {
3086 if (node_mem[i] != 0)
3087 break;
3089 if (i == nb_numa_nodes) {
3090 uint64_t usedmem = 0;
3092 /* On Linux, the each node's border has to be 8MB aligned,
3093 * the final node gets the rest.
3095 for (i = 0; i < nb_numa_nodes - 1; i++) {
3096 node_mem[i] = (ram_size / nb_numa_nodes) & ~((1 << 23UL) - 1);
3097 usedmem += node_mem[i];
3099 node_mem[i] = ram_size - usedmem;
3102 for (i = 0; i < nb_numa_nodes; i++) {
3103 if (node_cpumask[i] != 0)
3104 break;
3106 /* assigning the VCPUs round-robin is easier to implement, guest OSes
3107 * must cope with this anyway, because there are BIOSes out there in
3108 * real machines which also use this scheme.
3110 if (i == nb_numa_nodes) {
3111 for (i = 0; i < smp_cpus; i++) {
3112 node_cpumask[i % nb_numa_nodes] |= 1 << i;
3117 if (qemu_opts_foreach(qemu_find_opts("mon"), mon_init_func, NULL, 1) != 0) {
3118 exit(1);
3121 if (foreach_device_config(DEV_SERIAL, serial_parse) < 0)
3122 exit(1);
3123 if (foreach_device_config(DEV_PARALLEL, parallel_parse) < 0)
3124 exit(1);
3125 if (foreach_device_config(DEV_VIRTCON, virtcon_parse) < 0)
3126 exit(1);
3127 if (foreach_device_config(DEV_DEBUGCON, debugcon_parse) < 0)
3128 exit(1);
3130 module_call_init(MODULE_INIT_DEVICE);
3132 if (qemu_opts_foreach(qemu_find_opts("device"), device_help_func, NULL, 0) != 0)
3133 exit(0);
3135 if (watchdog) {
3136 i = select_watchdog(watchdog);
3137 if (i > 0)
3138 exit (i == 1 ? 1 : 0);
3141 if (machine->compat_props) {
3142 qdev_prop_register_global_list(machine->compat_props);
3144 qemu_add_globals();
3146 machine->init(ram_size, boot_devices,
3147 kernel_filename, kernel_cmdline, initrd_filename, cpu_model);
3149 cpu_synchronize_all_post_init();
3151 /* must be after terminal init, SDL library changes signal handlers */
3152 os_setup_signal_handling();
3154 set_numa_modes();
3156 current_machine = machine;
3158 /* init USB devices */
3159 if (usb_enabled) {
3160 if (foreach_device_config(DEV_USB, usb_parse) < 0)
3161 exit(1);
3164 /* init generic devices */
3165 if (qemu_opts_foreach(qemu_find_opts("device"), device_init_func, NULL, 1) != 0)
3166 exit(1);
3168 net_check_clients();
3170 /* just use the first displaystate for the moment */
3171 ds = get_displaystate();
3173 if (using_spice)
3174 display_remote++;
3175 if (display_type == DT_DEFAULT && !display_remote) {
3176 #if defined(CONFIG_SDL) || defined(CONFIG_COCOA)
3177 display_type = DT_SDL;
3178 #elif defined(CONFIG_VNC)
3179 vnc_display = "localhost:0,to=99";
3180 show_vnc_port = 1;
3181 #else
3182 display_type = DT_NONE;
3183 #endif
3187 /* init local displays */
3188 switch (display_type) {
3189 case DT_NOGRAPHIC:
3190 break;
3191 #if defined(CONFIG_CURSES)
3192 case DT_CURSES:
3193 curses_display_init(ds, full_screen);
3194 break;
3195 #endif
3196 #if defined(CONFIG_SDL)
3197 case DT_SDL:
3198 sdl_display_init(ds, full_screen, no_frame);
3199 break;
3200 #elif defined(CONFIG_COCOA)
3201 case DT_SDL:
3202 cocoa_display_init(ds, full_screen);
3203 break;
3204 #endif
3205 default:
3206 break;
3209 #ifdef CONFIG_VNC
3210 /* init remote displays */
3211 if (vnc_display) {
3212 vnc_display_init(ds);
3213 if (vnc_display_open(ds, vnc_display) < 0)
3214 exit(1);
3216 if (show_vnc_port) {
3217 printf("VNC server running on `%s'\n", vnc_display_local_addr(ds));
3220 #endif
3221 #ifdef CONFIG_SPICE
3222 if (using_spice && !qxl_enabled) {
3223 qemu_spice_display_init(ds);
3225 #endif
3227 /* display setup */
3228 dpy_resize(ds);
3229 dcl = ds->listeners;
3230 while (dcl != NULL) {
3231 if (dcl->dpy_refresh != NULL) {
3232 ds->gui_timer = qemu_new_timer_ms(rt_clock, gui_update, ds);
3233 qemu_mod_timer(ds->gui_timer, qemu_get_clock_ms(rt_clock));
3234 break;
3236 dcl = dcl->next;
3238 if (ds->gui_timer == NULL) {
3239 nographic_timer = qemu_new_timer_ms(rt_clock, nographic_update, NULL);
3240 qemu_mod_timer(nographic_timer, qemu_get_clock_ms(rt_clock));
3242 text_consoles_set_display(ds);
3244 if (gdbstub_dev && gdbserver_start(gdbstub_dev) < 0) {
3245 fprintf(stderr, "qemu: could not open gdbserver on device '%s'\n",
3246 gdbstub_dev);
3247 exit(1);
3250 qdev_machine_creation_done();
3252 if (rom_load_all() != 0) {
3253 fprintf(stderr, "rom loading failed\n");
3254 exit(1);
3257 /* TODO: once all bus devices are qdevified, this should be done
3258 * when bus is created by qdev.c */
3259 qemu_register_reset(qbus_reset_all_fn, sysbus_get_default());
3260 qemu_run_machine_init_done_notifiers();
3262 qemu_system_reset();
3263 if (loadvm) {
3264 if (load_vmstate(loadvm) < 0) {
3265 autostart = 0;
3269 if (incoming) {
3270 int ret = qemu_start_incoming_migration(incoming);
3271 if (ret < 0) {
3272 fprintf(stderr, "Migration failed. Exit code %s(%d), exiting.\n",
3273 incoming, ret);
3274 exit(ret);
3276 } else if (autostart) {
3277 vm_start();
3280 os_setup_post();
3282 main_loop();
3283 quit_timers();
3284 net_cleanup();
3286 return 0;