qemu-kvm: Use upstream qemu_mutex_lock/unlock_iothread
[qemu-kvm.git] / vl.c
blob2587aae343306730765308a35a10554960f9977c
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 #include "hw/device-assignment.h"
149 #ifdef CONFIG_VIRTFS
150 #include "fsdev/qemu-fsdev.h"
151 #endif
153 #include "disas.h"
155 #include "qemu_socket.h"
157 #include "slirp/libslirp.h"
159 #include "trace.h"
160 #include "simpletrace.h"
161 #include "qemu-queue.h"
162 #include "cpus.h"
163 #include "arch_init.h"
165 #include "ui/qemu-spice.h"
167 //#define DEBUG_NET
168 //#define DEBUG_SLIRP
170 #define DEFAULT_RAM_SIZE 128
172 #define MAX_VIRTIO_CONSOLES 1
174 static const char *data_dir;
175 const char *bios_name = NULL;
176 enum vga_retrace_method vga_retrace_method = VGA_RETRACE_DUMB;
177 DisplayType display_type = DT_DEFAULT;
178 int display_remote = 0;
179 const char* keyboard_layout = NULL;
180 ram_addr_t ram_size;
181 const char *mem_path = NULL;
182 #ifdef MAP_POPULATE
183 int mem_prealloc = 0; /* force preallocation of physical target memory */
184 #endif
185 int nb_nics;
186 NICInfo nd_table[MAX_NICS];
187 int vm_running;
188 int autostart;
189 int incoming_expected; /* Started with -incoming and waiting for incoming */
190 static int rtc_utc = 1;
191 static int rtc_date_offset = -1; /* -1 means no change */
192 QEMUClock *rtc_clock;
193 int vga_interface_type = VGA_NONE;
194 static int full_screen = 0;
195 #ifdef CONFIG_SDL
196 static int no_frame = 0;
197 #endif
198 int no_quit = 0;
199 CharDriverState *serial_hds[MAX_SERIAL_PORTS];
200 CharDriverState *parallel_hds[MAX_PARALLEL_PORTS];
201 CharDriverState *virtcon_hds[MAX_VIRTIO_CONSOLES];
202 int win2k_install_hack = 0;
203 int rtc_td_hack = 0;
204 int usb_enabled = 0;
205 int singlestep = 0;
206 int smp_cpus = 1;
207 int max_cpus = 0;
208 int smp_cores = 1;
209 int smp_threads = 1;
210 #ifdef CONFIG_VNC
211 const char *vnc_display;
212 #endif
213 int acpi_enabled = 1;
214 int no_hpet = 0;
215 int fd_bootchk = 1;
216 int no_reboot = 0;
217 int no_shutdown = 0;
218 int cursor_hide = 1;
219 int graphic_rotate = 0;
220 const char *watchdog;
221 QEMUOptionRom option_rom[MAX_OPTION_ROMS];
222 int nb_option_roms;
223 int semihosting_enabled = 0;
224 int time_drift_fix = 0;
225 unsigned int kvm_shadow_memory = 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 const char *nvram = NULL;
233 int boot_menu;
235 typedef struct FWBootEntry FWBootEntry;
237 struct FWBootEntry {
238 QTAILQ_ENTRY(FWBootEntry) link;
239 int32_t bootindex;
240 DeviceState *dev;
241 char *suffix;
244 QTAILQ_HEAD(, FWBootEntry) fw_boot_order = QTAILQ_HEAD_INITIALIZER(fw_boot_order);
246 int nb_numa_nodes;
247 uint64_t node_mem[MAX_NODES];
248 uint64_t node_cpumask[MAX_NODES];
250 static QEMUTimer *nographic_timer;
252 uint8_t qemu_uuid[16];
254 static QEMUBootSetHandler *boot_set_handler;
255 static void *boot_set_opaque;
257 static NotifierList exit_notifiers =
258 NOTIFIER_LIST_INITIALIZER(exit_notifiers);
260 static NotifierList machine_init_done_notifiers =
261 NOTIFIER_LIST_INITIALIZER(machine_init_done_notifiers);
263 static int tcg_allowed = 1;
264 int kvm_allowed = 0;
265 int xen_allowed = 0;
266 uint32_t xen_domid;
267 enum xen_mode xen_mode = XEN_EMULATE;
269 static int default_serial = 1;
270 static int default_parallel = 1;
271 static int default_virtcon = 1;
272 static int default_monitor = 1;
273 static int default_vga = 1;
274 static int default_floppy = 1;
275 static int default_cdrom = 1;
276 static int default_sdcard = 1;
278 static struct {
279 const char *driver;
280 int *flag;
281 } default_list[] = {
282 { .driver = "isa-serial", .flag = &default_serial },
283 { .driver = "isa-parallel", .flag = &default_parallel },
284 { .driver = "isa-fdc", .flag = &default_floppy },
285 { .driver = "ide-drive", .flag = &default_cdrom },
286 { .driver = "virtio-serial-pci", .flag = &default_virtcon },
287 { .driver = "virtio-serial-s390", .flag = &default_virtcon },
288 { .driver = "virtio-serial", .flag = &default_virtcon },
289 { .driver = "VGA", .flag = &default_vga },
290 { .driver = "cirrus-vga", .flag = &default_vga },
291 { .driver = "vmware-svga", .flag = &default_vga },
294 static int default_driver_check(QemuOpts *opts, void *opaque)
296 const char *driver = qemu_opt_get(opts, "driver");
297 int i;
299 if (!driver)
300 return 0;
301 for (i = 0; i < ARRAY_SIZE(default_list); i++) {
302 if (strcmp(default_list[i].driver, driver) != 0)
303 continue;
304 *(default_list[i].flag) = 0;
306 return 0;
309 /***********************************************************/
310 /* real time host monotonic timer */
312 /***********************************************************/
313 /* host time/date access */
314 void qemu_get_timedate(struct tm *tm, int offset)
316 time_t ti;
317 struct tm *ret;
319 time(&ti);
320 ti += offset;
321 if (rtc_date_offset == -1) {
322 if (rtc_utc)
323 ret = gmtime(&ti);
324 else
325 ret = localtime(&ti);
326 } else {
327 ti -= rtc_date_offset;
328 ret = gmtime(&ti);
331 memcpy(tm, ret, sizeof(struct tm));
334 int qemu_timedate_diff(struct tm *tm)
336 time_t seconds;
338 if (rtc_date_offset == -1)
339 if (rtc_utc)
340 seconds = mktimegm(tm);
341 else
342 seconds = mktime(tm);
343 else
344 seconds = mktimegm(tm) + rtc_date_offset;
346 return seconds - time(NULL);
349 void rtc_change_mon_event(struct tm *tm)
351 QObject *data;
353 data = qobject_from_jsonf("{ 'offset': %d }", qemu_timedate_diff(tm));
354 monitor_protocol_event(QEVENT_RTC_CHANGE, data);
355 qobject_decref(data);
358 static void configure_rtc_date_offset(const char *startdate, int legacy)
360 time_t rtc_start_date;
361 struct tm tm;
363 if (!strcmp(startdate, "now") && legacy) {
364 rtc_date_offset = -1;
365 } else {
366 if (sscanf(startdate, "%d-%d-%dT%d:%d:%d",
367 &tm.tm_year,
368 &tm.tm_mon,
369 &tm.tm_mday,
370 &tm.tm_hour,
371 &tm.tm_min,
372 &tm.tm_sec) == 6) {
373 /* OK */
374 } else if (sscanf(startdate, "%d-%d-%d",
375 &tm.tm_year,
376 &tm.tm_mon,
377 &tm.tm_mday) == 3) {
378 tm.tm_hour = 0;
379 tm.tm_min = 0;
380 tm.tm_sec = 0;
381 } else {
382 goto date_fail;
384 tm.tm_year -= 1900;
385 tm.tm_mon--;
386 rtc_start_date = mktimegm(&tm);
387 if (rtc_start_date == -1) {
388 date_fail:
389 fprintf(stderr, "Invalid date format. Valid formats are:\n"
390 "'2006-06-17T16:01:21' or '2006-06-17'\n");
391 exit(1);
393 rtc_date_offset = time(NULL) - rtc_start_date;
397 static void configure_rtc(QemuOpts *opts)
399 const char *value;
401 value = qemu_opt_get(opts, "base");
402 if (value) {
403 if (!strcmp(value, "utc")) {
404 rtc_utc = 1;
405 } else if (!strcmp(value, "localtime")) {
406 rtc_utc = 0;
407 } else {
408 configure_rtc_date_offset(value, 0);
411 value = qemu_opt_get(opts, "clock");
412 if (value) {
413 if (!strcmp(value, "host")) {
414 rtc_clock = host_clock;
415 } else if (!strcmp(value, "vm")) {
416 rtc_clock = vm_clock;
417 } else {
418 fprintf(stderr, "qemu: invalid option value '%s'\n", value);
419 exit(1);
422 value = qemu_opt_get(opts, "driftfix");
423 if (value) {
424 if (!strcmp(value, "slew")) {
425 rtc_td_hack = 1;
426 } else if (!strcmp(value, "none")) {
427 rtc_td_hack = 0;
428 } else {
429 fprintf(stderr, "qemu: invalid option value '%s'\n", value);
430 exit(1);
435 /***********************************************************/
436 /* Bluetooth support */
437 static int nb_hcis;
438 static int cur_hci;
439 static struct HCIInfo *hci_table[MAX_NICS];
441 static struct bt_vlan_s {
442 struct bt_scatternet_s net;
443 int id;
444 struct bt_vlan_s *next;
445 } *first_bt_vlan;
447 /* find or alloc a new bluetooth "VLAN" */
448 static struct bt_scatternet_s *qemu_find_bt_vlan(int id)
450 struct bt_vlan_s **pvlan, *vlan;
451 for (vlan = first_bt_vlan; vlan != NULL; vlan = vlan->next) {
452 if (vlan->id == id)
453 return &vlan->net;
455 vlan = qemu_mallocz(sizeof(struct bt_vlan_s));
456 vlan->id = id;
457 pvlan = &first_bt_vlan;
458 while (*pvlan != NULL)
459 pvlan = &(*pvlan)->next;
460 *pvlan = vlan;
461 return &vlan->net;
464 static void null_hci_send(struct HCIInfo *hci, const uint8_t *data, int len)
468 static int null_hci_addr_set(struct HCIInfo *hci, const uint8_t *bd_addr)
470 return -ENOTSUP;
473 static struct HCIInfo null_hci = {
474 .cmd_send = null_hci_send,
475 .sco_send = null_hci_send,
476 .acl_send = null_hci_send,
477 .bdaddr_set = null_hci_addr_set,
480 struct HCIInfo *qemu_next_hci(void)
482 if (cur_hci == nb_hcis)
483 return &null_hci;
485 return hci_table[cur_hci++];
488 static struct HCIInfo *hci_init(const char *str)
490 char *endp;
491 struct bt_scatternet_s *vlan = 0;
493 if (!strcmp(str, "null"))
494 /* null */
495 return &null_hci;
496 else if (!strncmp(str, "host", 4) && (str[4] == '\0' || str[4] == ':'))
497 /* host[:hciN] */
498 return bt_host_hci(str[4] ? str + 5 : "hci0");
499 else if (!strncmp(str, "hci", 3)) {
500 /* hci[,vlan=n] */
501 if (str[3]) {
502 if (!strncmp(str + 3, ",vlan=", 6)) {
503 vlan = qemu_find_bt_vlan(strtol(str + 9, &endp, 0));
504 if (*endp)
505 vlan = 0;
507 } else
508 vlan = qemu_find_bt_vlan(0);
509 if (vlan)
510 return bt_new_hci(vlan);
513 fprintf(stderr, "qemu: Unknown bluetooth HCI `%s'.\n", str);
515 return 0;
518 static int bt_hci_parse(const char *str)
520 struct HCIInfo *hci;
521 bdaddr_t bdaddr;
523 if (nb_hcis >= MAX_NICS) {
524 fprintf(stderr, "qemu: Too many bluetooth HCIs (max %i).\n", MAX_NICS);
525 return -1;
528 hci = hci_init(str);
529 if (!hci)
530 return -1;
532 bdaddr.b[0] = 0x52;
533 bdaddr.b[1] = 0x54;
534 bdaddr.b[2] = 0x00;
535 bdaddr.b[3] = 0x12;
536 bdaddr.b[4] = 0x34;
537 bdaddr.b[5] = 0x56 + nb_hcis;
538 hci->bdaddr_set(hci, bdaddr.b);
540 hci_table[nb_hcis++] = hci;
542 return 0;
545 static void bt_vhci_add(int vlan_id)
547 struct bt_scatternet_s *vlan = qemu_find_bt_vlan(vlan_id);
549 if (!vlan->slave)
550 fprintf(stderr, "qemu: warning: adding a VHCI to "
551 "an empty scatternet %i\n", vlan_id);
553 bt_vhci_init(bt_new_hci(vlan));
556 static struct bt_device_s *bt_device_add(const char *opt)
558 struct bt_scatternet_s *vlan;
559 int vlan_id = 0;
560 char *endp = strstr(opt, ",vlan=");
561 int len = (endp ? endp - opt : strlen(opt)) + 1;
562 char devname[10];
564 pstrcpy(devname, MIN(sizeof(devname), len), opt);
566 if (endp) {
567 vlan_id = strtol(endp + 6, &endp, 0);
568 if (*endp) {
569 fprintf(stderr, "qemu: unrecognised bluetooth vlan Id\n");
570 return 0;
574 vlan = qemu_find_bt_vlan(vlan_id);
576 if (!vlan->slave)
577 fprintf(stderr, "qemu: warning: adding a slave device to "
578 "an empty scatternet %i\n", vlan_id);
580 if (!strcmp(devname, "keyboard"))
581 return bt_keyboard_init(vlan);
583 fprintf(stderr, "qemu: unsupported bluetooth device `%s'\n", devname);
584 return 0;
587 static int bt_parse(const char *opt)
589 const char *endp, *p;
590 int vlan;
592 if (strstart(opt, "hci", &endp)) {
593 if (!*endp || *endp == ',') {
594 if (*endp)
595 if (!strstart(endp, ",vlan=", 0))
596 opt = endp + 1;
598 return bt_hci_parse(opt);
600 } else if (strstart(opt, "vhci", &endp)) {
601 if (!*endp || *endp == ',') {
602 if (*endp) {
603 if (strstart(endp, ",vlan=", &p)) {
604 vlan = strtol(p, (char **) &endp, 0);
605 if (*endp) {
606 fprintf(stderr, "qemu: bad scatternet '%s'\n", p);
607 return 1;
609 } else {
610 fprintf(stderr, "qemu: bad parameter '%s'\n", endp + 1);
611 return 1;
613 } else
614 vlan = 0;
616 bt_vhci_add(vlan);
617 return 0;
619 } else if (strstart(opt, "device:", &endp))
620 return !bt_device_add(endp);
622 fprintf(stderr, "qemu: bad bluetooth parameter '%s'\n", opt);
623 return 1;
626 /***********************************************************/
627 /* QEMU Block devices */
629 #define HD_OPTS "media=disk"
630 #define CDROM_OPTS "media=cdrom"
631 #define FD_OPTS ""
632 #define PFLASH_OPTS ""
633 #define MTD_OPTS ""
634 #define SD_OPTS ""
636 static int drive_init_func(QemuOpts *opts, void *opaque)
638 int *use_scsi = opaque;
640 return drive_init(opts, *use_scsi) == NULL;
643 static int drive_enable_snapshot(QemuOpts *opts, void *opaque)
645 if (NULL == qemu_opt_get(opts, "snapshot")) {
646 qemu_opt_set(opts, "snapshot", "on");
648 return 0;
651 static void default_drive(int enable, int snapshot, int use_scsi,
652 BlockInterfaceType type, int index,
653 const char *optstr)
655 QemuOpts *opts;
657 if (type == IF_DEFAULT) {
658 type = use_scsi ? IF_SCSI : IF_IDE;
661 if (!enable || drive_get_by_index(type, index)) {
662 return;
665 opts = drive_add(type, index, NULL, optstr);
666 if (snapshot) {
667 drive_enable_snapshot(opts, NULL);
669 if (!drive_init(opts, use_scsi)) {
670 exit(1);
674 void qemu_register_boot_set(QEMUBootSetHandler *func, void *opaque)
676 boot_set_handler = func;
677 boot_set_opaque = opaque;
680 int qemu_boot_set(const char *boot_devices)
682 if (!boot_set_handler) {
683 return -EINVAL;
685 return boot_set_handler(boot_set_opaque, boot_devices);
688 static void validate_bootdevices(char *devices)
690 /* We just do some generic consistency checks */
691 const char *p;
692 int bitmap = 0;
694 for (p = devices; *p != '\0'; p++) {
695 /* Allowed boot devices are:
696 * a-b: floppy disk drives
697 * c-f: IDE disk drives
698 * g-m: machine implementation dependant drives
699 * n-p: network devices
700 * It's up to each machine implementation to check if the given boot
701 * devices match the actual hardware implementation and firmware
702 * features.
704 if (*p < 'a' || *p > 'p') {
705 fprintf(stderr, "Invalid boot device '%c'\n", *p);
706 exit(1);
708 if (bitmap & (1 << (*p - 'a'))) {
709 fprintf(stderr, "Boot device '%c' was given twice\n", *p);
710 exit(1);
712 bitmap |= 1 << (*p - 'a');
716 static void restore_boot_devices(void *opaque)
718 char *standard_boot_devices = opaque;
719 static int first = 1;
721 /* Restore boot order and remove ourselves after the first boot */
722 if (first) {
723 first = 0;
724 return;
727 qemu_boot_set(standard_boot_devices);
729 qemu_unregister_reset(restore_boot_devices, standard_boot_devices);
730 qemu_free(standard_boot_devices);
733 void add_boot_device_path(int32_t bootindex, DeviceState *dev,
734 const char *suffix)
736 FWBootEntry *node, *i;
738 if (bootindex < 0) {
739 return;
742 assert(dev != NULL || suffix != NULL);
744 node = qemu_mallocz(sizeof(FWBootEntry));
745 node->bootindex = bootindex;
746 node->suffix = suffix ? qemu_strdup(suffix) : NULL;
747 node->dev = dev;
749 QTAILQ_FOREACH(i, &fw_boot_order, link) {
750 if (i->bootindex == bootindex) {
751 fprintf(stderr, "Two devices with same boot index %d\n", bootindex);
752 exit(1);
753 } else if (i->bootindex < bootindex) {
754 continue;
756 QTAILQ_INSERT_BEFORE(i, node, link);
757 return;
759 QTAILQ_INSERT_TAIL(&fw_boot_order, node, link);
763 * This function returns null terminated string that consist of new line
764 * separated device paths.
766 * memory pointed by "size" is assigned total length of the array in bytes
769 char *get_boot_devices_list(uint32_t *size)
771 FWBootEntry *i;
772 uint32_t total = 0;
773 char *list = NULL;
775 QTAILQ_FOREACH(i, &fw_boot_order, link) {
776 char *devpath = NULL, *bootpath;
777 int len;
779 if (i->dev) {
780 devpath = qdev_get_fw_dev_path(i->dev);
781 assert(devpath);
784 if (i->suffix && devpath) {
785 size_t bootpathlen = strlen(devpath) + strlen(i->suffix) + 1;
787 bootpath = qemu_malloc(bootpathlen);
788 snprintf(bootpath, bootpathlen, "%s%s", devpath, i->suffix);
789 qemu_free(devpath);
790 } else if (devpath) {
791 bootpath = devpath;
792 } else {
793 bootpath = qemu_strdup(i->suffix);
794 assert(bootpath);
797 if (total) {
798 list[total-1] = '\n';
800 len = strlen(bootpath) + 1;
801 list = qemu_realloc(list, total + len);
802 memcpy(&list[total], bootpath, len);
803 total += len;
804 qemu_free(bootpath);
807 *size = total;
809 return list;
812 static void numa_add(const char *optarg)
814 char option[128];
815 char *endptr;
816 unsigned long long value, endvalue;
817 int nodenr;
819 optarg = get_opt_name(option, 128, optarg, ',') + 1;
820 if (!strcmp(option, "node")) {
821 if (get_param_value(option, 128, "nodeid", optarg) == 0) {
822 nodenr = nb_numa_nodes;
823 } else {
824 nodenr = strtoull(option, NULL, 10);
827 if (get_param_value(option, 128, "mem", optarg) == 0) {
828 node_mem[nodenr] = 0;
829 } else {
830 int64_t sval;
831 sval = strtosz(option, NULL);
832 if (sval < 0) {
833 fprintf(stderr, "qemu: invalid numa mem size: %s\n", optarg);
834 exit(1);
836 node_mem[nodenr] = sval;
838 if (get_param_value(option, 128, "cpus", optarg) == 0) {
839 node_cpumask[nodenr] = 0;
840 } else {
841 value = strtoull(option, &endptr, 10);
842 if (value >= 64) {
843 value = 63;
844 fprintf(stderr, "only 64 CPUs in NUMA mode supported.\n");
845 } else {
846 if (*endptr == '-') {
847 endvalue = strtoull(endptr+1, &endptr, 10);
848 if (endvalue >= 63) {
849 endvalue = 62;
850 fprintf(stderr,
851 "only 63 CPUs in NUMA mode supported.\n");
853 value = (2ULL << endvalue) - (1ULL << value);
854 } else {
855 value = 1ULL << value;
858 node_cpumask[nodenr] = value;
860 nb_numa_nodes++;
862 return;
865 static void smp_parse(const char *optarg)
867 int smp, sockets = 0, threads = 0, cores = 0;
868 char *endptr;
869 char option[128];
871 smp = strtoul(optarg, &endptr, 10);
872 if (endptr != optarg) {
873 if (*endptr == ',') {
874 endptr++;
877 if (get_param_value(option, 128, "sockets", endptr) != 0)
878 sockets = strtoull(option, NULL, 10);
879 if (get_param_value(option, 128, "cores", endptr) != 0)
880 cores = strtoull(option, NULL, 10);
881 if (get_param_value(option, 128, "threads", endptr) != 0)
882 threads = strtoull(option, NULL, 10);
883 if (get_param_value(option, 128, "maxcpus", endptr) != 0)
884 max_cpus = strtoull(option, NULL, 10);
886 /* compute missing values, prefer sockets over cores over threads */
887 if (smp == 0 || sockets == 0) {
888 sockets = sockets > 0 ? sockets : 1;
889 cores = cores > 0 ? cores : 1;
890 threads = threads > 0 ? threads : 1;
891 if (smp == 0) {
892 smp = cores * threads * sockets;
894 } else {
895 if (cores == 0) {
896 threads = threads > 0 ? threads : 1;
897 cores = smp / (sockets * threads);
898 } else {
899 threads = smp / (cores * sockets);
902 smp_cpus = smp;
903 smp_cores = cores > 0 ? cores : 1;
904 smp_threads = threads > 0 ? threads : 1;
905 if (max_cpus == 0)
906 max_cpus = smp_cpus;
909 /***********************************************************/
910 /* USB devices */
912 static int usb_device_add(const char *devname)
914 const char *p;
915 USBDevice *dev = NULL;
917 if (!usb_enabled)
918 return -1;
920 /* drivers with .usbdevice_name entry in USBDeviceInfo */
921 dev = usbdevice_create(devname);
922 if (dev)
923 goto done;
925 /* the other ones */
926 if (strstart(devname, "host:", &p)) {
927 dev = usb_host_device_open(p);
928 } else if (!strcmp(devname, "bt") || strstart(devname, "bt:", &p)) {
929 dev = usb_bt_init(devname[2] ? hci_init(p) :
930 bt_new_hci(qemu_find_bt_vlan(0)));
931 } else {
932 return -1;
934 if (!dev)
935 return -1;
937 done:
938 return 0;
941 static int usb_device_del(const char *devname)
943 int bus_num, addr;
944 const char *p;
946 if (strstart(devname, "host:", &p))
947 return usb_host_device_close(p);
949 if (!usb_enabled)
950 return -1;
952 p = strchr(devname, '.');
953 if (!p)
954 return -1;
955 bus_num = strtoul(devname, NULL, 0);
956 addr = strtoul(p + 1, NULL, 0);
958 return usb_device_delete_addr(bus_num, addr);
961 static int usb_parse(const char *cmdline)
963 int r;
964 r = usb_device_add(cmdline);
965 if (r < 0) {
966 fprintf(stderr, "qemu: could not add USB device '%s'\n", cmdline);
968 return r;
971 void do_usb_add(Monitor *mon, const QDict *qdict)
973 const char *devname = qdict_get_str(qdict, "devname");
974 if (usb_device_add(devname) < 0) {
975 error_report("could not add USB device '%s'", devname);
979 void do_usb_del(Monitor *mon, const QDict *qdict)
981 const char *devname = qdict_get_str(qdict, "devname");
982 if (usb_device_del(devname) < 0) {
983 error_report("could not delete USB device '%s'", devname);
987 /***********************************************************/
988 /* PCMCIA/Cardbus */
990 static struct pcmcia_socket_entry_s {
991 PCMCIASocket *socket;
992 struct pcmcia_socket_entry_s *next;
993 } *pcmcia_sockets = 0;
995 void pcmcia_socket_register(PCMCIASocket *socket)
997 struct pcmcia_socket_entry_s *entry;
999 entry = qemu_malloc(sizeof(struct pcmcia_socket_entry_s));
1000 entry->socket = socket;
1001 entry->next = pcmcia_sockets;
1002 pcmcia_sockets = entry;
1005 void pcmcia_socket_unregister(PCMCIASocket *socket)
1007 struct pcmcia_socket_entry_s *entry, **ptr;
1009 ptr = &pcmcia_sockets;
1010 for (entry = *ptr; entry; ptr = &entry->next, entry = *ptr)
1011 if (entry->socket == socket) {
1012 *ptr = entry->next;
1013 qemu_free(entry);
1017 void pcmcia_info(Monitor *mon)
1019 struct pcmcia_socket_entry_s *iter;
1021 if (!pcmcia_sockets)
1022 monitor_printf(mon, "No PCMCIA sockets\n");
1024 for (iter = pcmcia_sockets; iter; iter = iter->next)
1025 monitor_printf(mon, "%s: %s\n", iter->socket->slot_string,
1026 iter->socket->attached ? iter->socket->card_string :
1027 "Empty");
1030 /***********************************************************/
1031 /* machine registration */
1033 static QEMUMachine *first_machine = NULL;
1034 QEMUMachine *current_machine = NULL;
1036 int qemu_register_machine(QEMUMachine *m)
1038 QEMUMachine **pm;
1039 pm = &first_machine;
1040 while (*pm != NULL)
1041 pm = &(*pm)->next;
1042 m->next = NULL;
1043 *pm = m;
1044 return 0;
1047 static QEMUMachine *find_machine(const char *name)
1049 QEMUMachine *m;
1051 for(m = first_machine; m != NULL; m = m->next) {
1052 if (!strcmp(m->name, name))
1053 return m;
1054 if (m->alias && !strcmp(m->alias, name))
1055 return m;
1057 return NULL;
1060 static QEMUMachine *find_default_machine(void)
1062 QEMUMachine *m;
1064 for(m = first_machine; m != NULL; m = m->next) {
1065 if (m->is_default) {
1066 return m;
1069 return NULL;
1072 /***********************************************************/
1073 /* main execution loop */
1075 static void gui_update(void *opaque)
1077 uint64_t interval = GUI_REFRESH_INTERVAL;
1078 DisplayState *ds = opaque;
1079 DisplayChangeListener *dcl = ds->listeners;
1081 qemu_flush_coalesced_mmio_buffer();
1082 dpy_refresh(ds);
1084 while (dcl != NULL) {
1085 if (dcl->gui_timer_interval &&
1086 dcl->gui_timer_interval < interval)
1087 interval = dcl->gui_timer_interval;
1088 dcl = dcl->next;
1090 qemu_mod_timer(ds->gui_timer, interval + qemu_get_clock_ms(rt_clock));
1093 static void nographic_update(void *opaque)
1095 uint64_t interval = GUI_REFRESH_INTERVAL;
1097 qemu_flush_coalesced_mmio_buffer();
1098 qemu_mod_timer(nographic_timer, interval + qemu_get_clock_ms(rt_clock));
1101 struct vm_change_state_entry {
1102 VMChangeStateHandler *cb;
1103 void *opaque;
1104 QLIST_ENTRY (vm_change_state_entry) entries;
1107 static QLIST_HEAD(vm_change_state_head, vm_change_state_entry) vm_change_state_head;
1109 VMChangeStateEntry *qemu_add_vm_change_state_handler(VMChangeStateHandler *cb,
1110 void *opaque)
1112 VMChangeStateEntry *e;
1114 e = qemu_mallocz(sizeof (*e));
1116 e->cb = cb;
1117 e->opaque = opaque;
1118 QLIST_INSERT_HEAD(&vm_change_state_head, e, entries);
1119 return e;
1122 void qemu_del_vm_change_state_handler(VMChangeStateEntry *e)
1124 QLIST_REMOVE (e, entries);
1125 qemu_free (e);
1128 void vm_state_notify(int running, int reason)
1130 VMChangeStateEntry *e;
1132 trace_vm_state_notify(running, reason);
1134 for (e = vm_change_state_head.lh_first; e; e = e->entries.le_next) {
1135 e->cb(e->opaque, running, reason);
1139 void vm_start(void)
1141 if (!vm_running) {
1142 cpu_enable_ticks();
1143 vm_running = 1;
1144 vm_state_notify(1, 0);
1145 resume_all_vcpus();
1146 monitor_protocol_event(QEVENT_RESUME, NULL);
1150 /* reset/shutdown handler */
1152 typedef struct QEMUResetEntry {
1153 QTAILQ_ENTRY(QEMUResetEntry) entry;
1154 QEMUResetHandler *func;
1155 void *opaque;
1156 } QEMUResetEntry;
1158 static QTAILQ_HEAD(reset_handlers, QEMUResetEntry) reset_handlers =
1159 QTAILQ_HEAD_INITIALIZER(reset_handlers);
1160 static int reset_requested;
1161 static int shutdown_requested, shutdown_signal = -1;
1162 static pid_t shutdown_pid;
1163 static int powerdown_requested;
1164 static int debug_requested;
1165 static int vmstop_requested;
1167 int qemu_no_shutdown(void)
1169 int r = no_shutdown;
1170 no_shutdown = 0;
1171 return r;
1174 int qemu_shutdown_requested_get(void)
1176 return shutdown_requested;
1179 int qemu_reset_requested_get(void)
1181 return reset_requested;
1184 int qemu_shutdown_requested(void)
1186 int r = shutdown_requested;
1187 shutdown_requested = 0;
1188 return r;
1191 void qemu_kill_report(void)
1193 if (shutdown_signal != -1) {
1194 fprintf(stderr, "qemu: terminating on signal %d", shutdown_signal);
1195 if (shutdown_pid == 0) {
1196 /* This happens for eg ^C at the terminal, so it's worth
1197 * avoiding printing an odd message in that case.
1199 fputc('\n', stderr);
1200 } else {
1201 fprintf(stderr, " from pid %d\n", shutdown_pid);
1203 shutdown_signal = -1;
1207 int qemu_reset_requested(void)
1209 int r = reset_requested;
1210 reset_requested = 0;
1211 return r;
1214 int qemu_powerdown_requested(void)
1216 int r = powerdown_requested;
1217 powerdown_requested = 0;
1218 return r;
1221 static int qemu_debug_requested(void)
1223 int r = debug_requested;
1224 debug_requested = 0;
1225 return r;
1228 static int qemu_vmstop_requested(void)
1230 int r = vmstop_requested;
1231 vmstop_requested = 0;
1232 return r;
1235 void qemu_register_reset(QEMUResetHandler *func, void *opaque)
1237 QEMUResetEntry *re = qemu_mallocz(sizeof(QEMUResetEntry));
1239 re->func = func;
1240 re->opaque = opaque;
1241 QTAILQ_INSERT_TAIL(&reset_handlers, re, entry);
1244 void qemu_unregister_reset(QEMUResetHandler *func, void *opaque)
1246 QEMUResetEntry *re;
1248 QTAILQ_FOREACH(re, &reset_handlers, entry) {
1249 if (re->func == func && re->opaque == opaque) {
1250 QTAILQ_REMOVE(&reset_handlers, re, entry);
1251 qemu_free(re);
1252 return;
1257 void qemu_system_reset(void)
1259 QEMUResetEntry *re, *nre;
1261 /* reset all devices */
1262 QTAILQ_FOREACH_SAFE(re, &reset_handlers, entry, nre) {
1263 re->func(re->opaque);
1265 monitor_protocol_event(QEVENT_RESET, NULL);
1266 cpu_synchronize_all_post_reset();
1269 void qemu_system_reset_request(void)
1271 if (no_reboot) {
1272 shutdown_requested = 1;
1273 } else {
1274 reset_requested = 1;
1276 if (cpu_single_env) {
1277 cpu_single_env->stopped = 1;
1278 cpu_exit(cpu_single_env);
1280 cpu_stop_current();
1281 qemu_notify_event();
1284 void qemu_system_killed(int signal, pid_t pid)
1286 shutdown_signal = signal;
1287 shutdown_pid = pid;
1288 qemu_system_shutdown_request();
1291 void qemu_system_shutdown_request(void)
1293 shutdown_requested = 1;
1294 qemu_notify_event();
1297 void qemu_system_powerdown_request(void)
1299 powerdown_requested = 1;
1300 qemu_notify_event();
1303 void qemu_system_debug_request(void)
1305 debug_requested = 1;
1306 qemu_notify_event();
1309 void qemu_system_vmstop_request(int reason)
1311 vmstop_requested = reason;
1312 qemu_notify_event();
1315 void main_loop_wait(int nonblocking)
1317 fd_set rfds, wfds, xfds;
1318 int ret, nfds;
1319 struct timeval tv;
1320 int timeout;
1322 if (nonblocking)
1323 timeout = 0;
1324 else {
1325 timeout = qemu_calculate_timeout();
1326 qemu_bh_update_timeout(&timeout);
1329 os_host_main_loop_wait(&timeout);
1331 tv.tv_sec = timeout / 1000;
1332 tv.tv_usec = (timeout % 1000) * 1000;
1334 /* poll any events */
1335 /* XXX: separate device handlers from system ones */
1336 nfds = -1;
1337 FD_ZERO(&rfds);
1338 FD_ZERO(&wfds);
1339 FD_ZERO(&xfds);
1340 qemu_iohandler_fill(&nfds, &rfds, &wfds, &xfds);
1341 slirp_select_fill(&nfds, &rfds, &wfds, &xfds);
1343 qemu_mutex_unlock_iothread();
1344 ret = select(nfds + 1, &rfds, &wfds, &xfds, &tv);
1345 qemu_mutex_lock_iothread();
1347 qemu_iohandler_poll(&rfds, &wfds, &xfds, ret);
1348 slirp_select_poll(&rfds, &wfds, &xfds, (ret < 0));
1350 qemu_run_all_timers();
1352 /* Check bottom-halves last in case any of the earlier events triggered
1353 them. */
1354 qemu_bh_poll();
1358 #ifndef CONFIG_IOTHREAD
1359 static int vm_request_pending(void)
1361 return powerdown_requested ||
1362 reset_requested ||
1363 shutdown_requested ||
1364 debug_requested ||
1365 vmstop_requested;
1367 #endif
1369 qemu_irq qemu_system_powerdown;
1371 static void main_loop(void)
1373 bool nonblocking = false;
1374 #ifdef CONFIG_PROFILER
1375 int64_t ti;
1376 #endif
1377 int r;
1379 if (kvm_enabled()) {
1380 kvm_main_loop();
1381 cpu_disable_ticks();
1382 return;
1385 qemu_main_loop_start();
1387 for (;;) {
1388 #ifndef CONFIG_IOTHREAD
1389 nonblocking = cpu_exec_all();
1390 if (vm_request_pending()) {
1391 nonblocking = true;
1393 #endif
1394 #ifdef CONFIG_PROFILER
1395 ti = profile_getclock();
1396 #endif
1397 main_loop_wait(nonblocking);
1398 #ifdef CONFIG_PROFILER
1399 dev_time += profile_getclock() - ti;
1400 #endif
1402 if (qemu_debug_requested()) {
1403 vm_stop(VMSTOP_DEBUG);
1405 if (qemu_shutdown_requested()) {
1406 qemu_kill_report();
1407 monitor_protocol_event(QEVENT_SHUTDOWN, NULL);
1408 if (no_shutdown) {
1409 vm_stop(VMSTOP_SHUTDOWN);
1410 no_shutdown = 0;
1411 } else
1412 break;
1414 if (qemu_reset_requested()) {
1415 pause_all_vcpus();
1416 cpu_synchronize_all_states();
1417 qemu_system_reset();
1418 resume_all_vcpus();
1420 if (qemu_powerdown_requested()) {
1421 monitor_protocol_event(QEVENT_POWERDOWN, NULL);
1422 qemu_irq_raise(qemu_system_powerdown);
1424 if ((r = qemu_vmstop_requested())) {
1425 vm_stop(r);
1428 bdrv_close_all();
1429 pause_all_vcpus();
1432 static void version(void)
1434 printf("QEMU emulator version " QEMU_VERSION QEMU_PKGVERSION ", Copyright (c) 2003-2008 Fabrice Bellard\n");
1437 static void help(int exitcode)
1439 const char *options_help =
1440 #define DEF(option, opt_arg, opt_enum, opt_help, arch_mask) \
1441 opt_help
1442 #define DEFHEADING(text) stringify(text) "\n"
1443 #include "qemu-options.def"
1444 #undef DEF
1445 #undef DEFHEADING
1446 #undef GEN_DOCS
1448 version();
1449 printf("usage: %s [options] [disk_image]\n"
1450 "\n"
1451 "'disk_image' is a raw hard disk image for IDE hard disk 0\n"
1452 "\n"
1453 "%s\n"
1454 "During emulation, the following keys are useful:\n"
1455 "ctrl-alt-f toggle full screen\n"
1456 "ctrl-alt-n switch to virtual console 'n'\n"
1457 "ctrl-alt toggle mouse and keyboard grab\n"
1458 "\n"
1459 "When using -nographic, press 'ctrl-a h' to get some help.\n",
1460 "qemu",
1461 options_help);
1462 exit(exitcode);
1465 #define HAS_ARG 0x0001
1467 typedef struct QEMUOption {
1468 const char *name;
1469 int flags;
1470 int index;
1471 uint32_t arch_mask;
1472 } QEMUOption;
1474 static const QEMUOption qemu_options[] = {
1475 { "h", 0, QEMU_OPTION_h, QEMU_ARCH_ALL },
1476 #define DEF(option, opt_arg, opt_enum, opt_help, arch_mask) \
1477 { option, opt_arg, opt_enum, arch_mask },
1478 #define DEFHEADING(text)
1479 #include "qemu-options.def"
1480 #undef DEF
1481 #undef DEFHEADING
1482 #undef GEN_DOCS
1483 { NULL },
1485 static void select_vgahw (const char *p)
1487 const char *opts;
1489 default_vga = 0;
1490 vga_interface_type = VGA_NONE;
1491 if (strstart(p, "std", &opts)) {
1492 vga_interface_type = VGA_STD;
1493 } else if (strstart(p, "cirrus", &opts)) {
1494 vga_interface_type = VGA_CIRRUS;
1495 } else if (strstart(p, "vmware", &opts)) {
1496 vga_interface_type = VGA_VMWARE;
1497 } else if (strstart(p, "xenfb", &opts)) {
1498 vga_interface_type = VGA_XENFB;
1499 } else if (strstart(p, "qxl", &opts)) {
1500 vga_interface_type = VGA_QXL;
1501 } else if (!strstart(p, "none", &opts)) {
1502 invalid_vga:
1503 fprintf(stderr, "Unknown vga type: %s\n", p);
1504 exit(1);
1506 while (*opts) {
1507 const char *nextopt;
1509 if (strstart(opts, ",retrace=", &nextopt)) {
1510 opts = nextopt;
1511 if (strstart(opts, "dumb", &nextopt))
1512 vga_retrace_method = VGA_RETRACE_DUMB;
1513 else if (strstart(opts, "precise", &nextopt))
1514 vga_retrace_method = VGA_RETRACE_PRECISE;
1515 else goto invalid_vga;
1516 } else goto invalid_vga;
1517 opts = nextopt;
1521 static DisplayType select_display(const char *p)
1523 const char *opts;
1524 DisplayType display = DT_DEFAULT;
1526 if (strstart(p, "sdl", &opts)) {
1527 #ifdef CONFIG_SDL
1528 display = DT_SDL;
1529 while (*opts) {
1530 const char *nextopt;
1532 if (strstart(opts, ",frame=", &nextopt)) {
1533 opts = nextopt;
1534 if (strstart(opts, "on", &nextopt)) {
1535 no_frame = 0;
1536 } else if (strstart(opts, "off", &nextopt)) {
1537 no_frame = 1;
1538 } else {
1539 goto invalid_sdl_args;
1541 } else if (strstart(opts, ",alt_grab=", &nextopt)) {
1542 opts = nextopt;
1543 if (strstart(opts, "on", &nextopt)) {
1544 alt_grab = 1;
1545 } else if (strstart(opts, "off", &nextopt)) {
1546 alt_grab = 0;
1547 } else {
1548 goto invalid_sdl_args;
1550 } else if (strstart(opts, ",ctrl_grab=", &nextopt)) {
1551 opts = nextopt;
1552 if (strstart(opts, "on", &nextopt)) {
1553 ctrl_grab = 1;
1554 } else if (strstart(opts, "off", &nextopt)) {
1555 ctrl_grab = 0;
1556 } else {
1557 goto invalid_sdl_args;
1559 } else if (strstart(opts, ",window_close=", &nextopt)) {
1560 opts = nextopt;
1561 if (strstart(opts, "on", &nextopt)) {
1562 no_quit = 0;
1563 } else if (strstart(opts, "off", &nextopt)) {
1564 no_quit = 1;
1565 } else {
1566 goto invalid_sdl_args;
1568 } else {
1569 invalid_sdl_args:
1570 fprintf(stderr, "Invalid SDL option string: %s\n", p);
1571 exit(1);
1573 opts = nextopt;
1575 #else
1576 fprintf(stderr, "SDL support is disabled\n");
1577 exit(1);
1578 #endif
1579 } else if (strstart(p, "vnc", &opts)) {
1580 #ifdef CONFIG_VNC
1581 display_remote++;
1583 if (*opts) {
1584 const char *nextopt;
1586 if (strstart(opts, "=", &nextopt)) {
1587 vnc_display = nextopt;
1590 if (!vnc_display) {
1591 fprintf(stderr, "VNC requires a display argument vnc=<display>\n");
1592 exit(1);
1594 #else
1595 fprintf(stderr, "VNC support is disabled\n");
1596 exit(1);
1597 #endif
1598 } else if (strstart(p, "curses", &opts)) {
1599 #ifdef CONFIG_CURSES
1600 display = DT_CURSES;
1601 #else
1602 fprintf(stderr, "Curses support is disabled\n");
1603 exit(1);
1604 #endif
1605 } else if (strstart(p, "none", &opts)) {
1606 display = DT_NONE;
1607 } else {
1608 fprintf(stderr, "Unknown display type: %s\n", p);
1609 exit(1);
1612 return display;
1615 static int balloon_parse(const char *arg)
1617 QemuOpts *opts;
1619 if (strcmp(arg, "none") == 0) {
1620 return 0;
1623 if (!strncmp(arg, "virtio", 6)) {
1624 if (arg[6] == ',') {
1625 /* have params -> parse them */
1626 opts = qemu_opts_parse(qemu_find_opts("device"), arg+7, 0);
1627 if (!opts)
1628 return -1;
1629 } else {
1630 /* create empty opts */
1631 opts = qemu_opts_create(qemu_find_opts("device"), NULL, 0);
1633 qemu_opt_set(opts, "driver", "virtio-balloon");
1634 return 0;
1637 return -1;
1640 char *qemu_find_file(int type, const char *name)
1642 int len;
1643 const char *subdir;
1644 char *buf;
1646 /* If name contains path separators then try it as a straight path. */
1647 if ((strchr(name, '/') || strchr(name, '\\'))
1648 && access(name, R_OK) == 0) {
1649 return qemu_strdup(name);
1651 switch (type) {
1652 case QEMU_FILE_TYPE_BIOS:
1653 subdir = "";
1654 break;
1655 case QEMU_FILE_TYPE_KEYMAP:
1656 subdir = "keymaps/";
1657 break;
1658 default:
1659 abort();
1661 len = strlen(data_dir) + strlen(name) + strlen(subdir) + 2;
1662 buf = qemu_mallocz(len);
1663 snprintf(buf, len, "%s/%s%s", data_dir, subdir, name);
1664 if (access(buf, R_OK)) {
1665 qemu_free(buf);
1666 return NULL;
1668 return buf;
1671 static int device_help_func(QemuOpts *opts, void *opaque)
1673 return qdev_device_help(opts);
1676 static int device_init_func(QemuOpts *opts, void *opaque)
1678 DeviceState *dev;
1680 dev = qdev_device_add(opts);
1681 if (!dev)
1682 return -1;
1683 return 0;
1686 static int chardev_init_func(QemuOpts *opts, void *opaque)
1688 CharDriverState *chr;
1690 chr = qemu_chr_open_opts(opts, NULL);
1691 if (!chr)
1692 return -1;
1693 return 0;
1696 #ifdef CONFIG_VIRTFS
1697 static int fsdev_init_func(QemuOpts *opts, void *opaque)
1699 int ret;
1700 ret = qemu_fsdev_add(opts);
1702 return ret;
1704 #endif
1706 static int mon_init_func(QemuOpts *opts, void *opaque)
1708 CharDriverState *chr;
1709 const char *chardev;
1710 const char *mode;
1711 int flags;
1713 mode = qemu_opt_get(opts, "mode");
1714 if (mode == NULL) {
1715 mode = "readline";
1717 if (strcmp(mode, "readline") == 0) {
1718 flags = MONITOR_USE_READLINE;
1719 } else if (strcmp(mode, "control") == 0) {
1720 flags = MONITOR_USE_CONTROL;
1721 } else {
1722 fprintf(stderr, "unknown monitor mode \"%s\"\n", mode);
1723 exit(1);
1726 if (qemu_opt_get_bool(opts, "pretty", 0))
1727 flags |= MONITOR_USE_PRETTY;
1729 if (qemu_opt_get_bool(opts, "default", 0))
1730 flags |= MONITOR_IS_DEFAULT;
1732 chardev = qemu_opt_get(opts, "chardev");
1733 chr = qemu_chr_find(chardev);
1734 if (chr == NULL) {
1735 fprintf(stderr, "chardev \"%s\" not found\n", chardev);
1736 exit(1);
1739 monitor_init(chr, flags);
1740 return 0;
1743 static void monitor_parse(const char *optarg, const char *mode)
1745 static int monitor_device_index = 0;
1746 QemuOpts *opts;
1747 const char *p;
1748 char label[32];
1749 int def = 0;
1751 if (strstart(optarg, "chardev:", &p)) {
1752 snprintf(label, sizeof(label), "%s", p);
1753 } else {
1754 snprintf(label, sizeof(label), "compat_monitor%d",
1755 monitor_device_index);
1756 if (monitor_device_index == 0) {
1757 def = 1;
1759 opts = qemu_chr_parse_compat(label, optarg);
1760 if (!opts) {
1761 fprintf(stderr, "parse error: %s\n", optarg);
1762 exit(1);
1766 opts = qemu_opts_create(qemu_find_opts("mon"), label, 1);
1767 if (!opts) {
1768 fprintf(stderr, "duplicate chardev: %s\n", label);
1769 exit(1);
1771 qemu_opt_set(opts, "mode", mode);
1772 qemu_opt_set(opts, "chardev", label);
1773 if (def)
1774 qemu_opt_set(opts, "default", "on");
1775 monitor_device_index++;
1778 struct device_config {
1779 enum {
1780 DEV_USB, /* -usbdevice */
1781 DEV_BT, /* -bt */
1782 DEV_SERIAL, /* -serial */
1783 DEV_PARALLEL, /* -parallel */
1784 DEV_VIRTCON, /* -virtioconsole */
1785 DEV_DEBUGCON, /* -debugcon */
1786 } type;
1787 const char *cmdline;
1788 QTAILQ_ENTRY(device_config) next;
1790 QTAILQ_HEAD(, device_config) device_configs = QTAILQ_HEAD_INITIALIZER(device_configs);
1792 static void add_device_config(int type, const char *cmdline)
1794 struct device_config *conf;
1796 conf = qemu_mallocz(sizeof(*conf));
1797 conf->type = type;
1798 conf->cmdline = cmdline;
1799 QTAILQ_INSERT_TAIL(&device_configs, conf, next);
1802 static int foreach_device_config(int type, int (*func)(const char *cmdline))
1804 struct device_config *conf;
1805 int rc;
1807 QTAILQ_FOREACH(conf, &device_configs, next) {
1808 if (conf->type != type)
1809 continue;
1810 rc = func(conf->cmdline);
1811 if (0 != rc)
1812 return rc;
1814 return 0;
1817 static int serial_parse(const char *devname)
1819 static int index = 0;
1820 char label[32];
1822 if (strcmp(devname, "none") == 0)
1823 return 0;
1824 if (index == MAX_SERIAL_PORTS) {
1825 fprintf(stderr, "qemu: too many serial ports\n");
1826 exit(1);
1828 snprintf(label, sizeof(label), "serial%d", index);
1829 serial_hds[index] = qemu_chr_open(label, devname, NULL);
1830 if (!serial_hds[index]) {
1831 fprintf(stderr, "qemu: could not open serial device '%s': %s\n",
1832 devname, strerror(errno));
1833 return -1;
1835 index++;
1836 return 0;
1839 static int parallel_parse(const char *devname)
1841 static int index = 0;
1842 char label[32];
1844 if (strcmp(devname, "none") == 0)
1845 return 0;
1846 if (index == MAX_PARALLEL_PORTS) {
1847 fprintf(stderr, "qemu: too many parallel ports\n");
1848 exit(1);
1850 snprintf(label, sizeof(label), "parallel%d", index);
1851 parallel_hds[index] = qemu_chr_open(label, devname, NULL);
1852 if (!parallel_hds[index]) {
1853 fprintf(stderr, "qemu: could not open parallel device '%s': %s\n",
1854 devname, strerror(errno));
1855 return -1;
1857 index++;
1858 return 0;
1861 static int virtcon_parse(const char *devname)
1863 QemuOptsList *device = qemu_find_opts("device");
1864 static int index = 0;
1865 char label[32];
1866 QemuOpts *bus_opts, *dev_opts;
1868 if (strcmp(devname, "none") == 0)
1869 return 0;
1870 if (index == MAX_VIRTIO_CONSOLES) {
1871 fprintf(stderr, "qemu: too many virtio consoles\n");
1872 exit(1);
1875 bus_opts = qemu_opts_create(device, NULL, 0);
1876 qemu_opt_set(bus_opts, "driver", "virtio-serial");
1878 dev_opts = qemu_opts_create(device, NULL, 0);
1879 qemu_opt_set(dev_opts, "driver", "virtconsole");
1881 snprintf(label, sizeof(label), "virtcon%d", index);
1882 virtcon_hds[index] = qemu_chr_open(label, devname, NULL);
1883 if (!virtcon_hds[index]) {
1884 fprintf(stderr, "qemu: could not open virtio console '%s': %s\n",
1885 devname, strerror(errno));
1886 return -1;
1888 qemu_opt_set(dev_opts, "chardev", label);
1890 index++;
1891 return 0;
1894 static int debugcon_parse(const char *devname)
1896 QemuOpts *opts;
1898 if (!qemu_chr_open("debugcon", devname, NULL)) {
1899 exit(1);
1901 opts = qemu_opts_create(qemu_find_opts("device"), "debugcon", 1);
1902 if (!opts) {
1903 fprintf(stderr, "qemu: already have a debugcon device\n");
1904 exit(1);
1906 qemu_opt_set(opts, "driver", "isa-debugcon");
1907 qemu_opt_set(opts, "chardev", "debugcon");
1908 return 0;
1911 static int tcg_init(void)
1913 fprintf(stderr, "Emulation temporarily broken\n");
1914 return -1;
1917 static struct {
1918 const char *opt_name;
1919 const char *name;
1920 int (*available)(void);
1921 int (*init)(void);
1922 int *allowed;
1923 } accel_list[] = {
1924 { "tcg", "tcg", tcg_available, tcg_init, &tcg_allowed },
1925 { "xen", "Xen", xen_available, xen_init, &xen_allowed },
1926 { "kvm", "KVM", kvm_available, kvm_init, &kvm_allowed },
1929 static int configure_accelerator(void)
1931 const char *p = NULL;
1932 char buf[10];
1933 int i, ret;
1934 bool accel_initalised = 0;
1935 bool init_failed = 0;
1937 QemuOptsList *list = qemu_find_opts("machine");
1938 if (!QTAILQ_EMPTY(&list->head)) {
1939 p = qemu_opt_get(QTAILQ_FIRST(&list->head), "accel");
1942 if (p == NULL) {
1943 /* Use the default "accelerator", kvm */
1944 p = "kvm";
1947 while (!accel_initalised && *p != '\0') {
1948 if (*p == ':') {
1949 p++;
1951 p = get_opt_name(buf, sizeof (buf), p, ':');
1952 for (i = 0; i < ARRAY_SIZE(accel_list); i++) {
1953 if (strcmp(accel_list[i].opt_name, buf) == 0) {
1954 ret = accel_list[i].init();
1955 if (ret < 0) {
1956 init_failed = 1;
1957 if (!accel_list[i].available()) {
1958 printf("%s not supported for this target\n",
1959 accel_list[i].name);
1960 } else {
1961 fprintf(stderr, "failed to initialize %s: %s\n",
1962 accel_list[i].name,
1963 strerror(-ret));
1965 } else {
1966 accel_initalised = 1;
1967 *(accel_list[i].allowed) = 1;
1969 break;
1972 if (i == ARRAY_SIZE(accel_list)) {
1973 fprintf(stderr, "\"%s\" accelerator does not exist.\n", buf);
1977 if (!accel_initalised) {
1978 fprintf(stderr, "No accelerator found!\n");
1979 exit(1);
1982 if (init_failed) {
1983 fprintf(stderr, "Back to %s accelerator.\n", accel_list[i].name);
1986 return !accel_initalised;
1989 void qemu_add_exit_notifier(Notifier *notify)
1991 notifier_list_add(&exit_notifiers, notify);
1994 void qemu_remove_exit_notifier(Notifier *notify)
1996 notifier_list_remove(&exit_notifiers, notify);
1999 static void qemu_run_exit_notifiers(void)
2001 notifier_list_notify(&exit_notifiers);
2004 void qemu_add_machine_init_done_notifier(Notifier *notify)
2006 notifier_list_add(&machine_init_done_notifiers, notify);
2009 static void qemu_run_machine_init_done_notifiers(void)
2011 notifier_list_notify(&machine_init_done_notifiers);
2014 static const QEMUOption *lookup_opt(int argc, char **argv,
2015 const char **poptarg, int *poptind)
2017 const QEMUOption *popt;
2018 int optind = *poptind;
2019 char *r = argv[optind];
2020 const char *optarg;
2022 loc_set_cmdline(argv, optind, 1);
2023 optind++;
2024 /* Treat --foo the same as -foo. */
2025 if (r[1] == '-')
2026 r++;
2027 popt = qemu_options;
2028 for(;;) {
2029 if (!popt->name) {
2030 error_report("invalid option");
2031 exit(1);
2033 if (!strcmp(popt->name, r + 1))
2034 break;
2035 popt++;
2037 if (popt->flags & HAS_ARG) {
2038 if (optind >= argc) {
2039 error_report("requires an argument");
2040 exit(1);
2042 optarg = argv[optind++];
2043 loc_set_cmdline(argv, optind - 2, 2);
2044 } else {
2045 optarg = NULL;
2048 *poptarg = optarg;
2049 *poptind = optind;
2051 return popt;
2054 int main(int argc, char **argv, char **envp)
2056 const char *gdbstub_dev = NULL;
2057 int i;
2058 int snapshot, linux_boot;
2059 const char *icount_option = NULL;
2060 const char *initrd_filename;
2061 const char *kernel_filename, *kernel_cmdline;
2062 char boot_devices[33] = "cad"; /* default to HD->floppy->CD-ROM */
2063 DisplayState *ds;
2064 DisplayChangeListener *dcl;
2065 int cyls, heads, secs, translation;
2066 QemuOpts *hda_opts = NULL, *opts;
2067 QemuOptsList *olist;
2068 int optind;
2069 const char *optarg;
2070 const char *loadvm = NULL;
2071 QEMUMachine *machine;
2072 const char *cpu_model;
2073 int tb_size;
2074 const char *pid_file = NULL;
2075 const char *incoming = NULL;
2076 #ifdef CONFIG_VNC
2077 int show_vnc_port = 0;
2078 #endif
2079 int defconfig = 1;
2080 const char *trace_file = NULL;
2082 atexit(qemu_run_exit_notifiers);
2083 error_set_progname(argv[0]);
2085 init_clocks();
2087 qemu_cache_utils_init(envp);
2089 QLIST_INIT (&vm_change_state_head);
2090 os_setup_early_signal_handling();
2092 module_call_init(MODULE_INIT_MACHINE);
2093 machine = find_default_machine();
2094 cpu_model = NULL;
2095 initrd_filename = NULL;
2096 ram_size = 0;
2097 snapshot = 0;
2098 kernel_filename = NULL;
2099 kernel_cmdline = "";
2100 cyls = heads = secs = 0;
2101 translation = BIOS_ATA_TRANSLATION_AUTO;
2103 for (i = 0; i < MAX_NODES; i++) {
2104 node_mem[i] = 0;
2105 node_cpumask[i] = 0;
2108 nb_numa_nodes = 0;
2109 nb_nics = 0;
2111 tb_size = 0;
2112 autostart= 1;
2114 /* first pass of option parsing */
2115 optind = 1;
2116 while (optind < argc) {
2117 if (argv[optind][0] != '-') {
2118 /* disk image */
2119 optind++;
2120 continue;
2121 } else {
2122 const QEMUOption *popt;
2124 popt = lookup_opt(argc, argv, &optarg, &optind);
2125 switch (popt->index) {
2126 case QEMU_OPTION_nodefconfig:
2127 defconfig=0;
2128 break;
2133 if (defconfig) {
2134 int ret;
2136 ret = qemu_read_config_file(CONFIG_QEMU_CONFDIR "/qemu.conf");
2137 if (ret < 0 && ret != -ENOENT) {
2138 exit(1);
2141 ret = qemu_read_config_file(arch_config_name);
2142 if (ret < 0 && ret != -ENOENT) {
2143 exit(1);
2146 cpudef_init();
2148 /* second pass of option parsing */
2149 optind = 1;
2150 for(;;) {
2151 if (optind >= argc)
2152 break;
2153 if (argv[optind][0] != '-') {
2154 hda_opts = drive_add(IF_DEFAULT, 0, argv[optind++], HD_OPTS);
2155 } else {
2156 const QEMUOption *popt;
2158 popt = lookup_opt(argc, argv, &optarg, &optind);
2159 if (!(popt->arch_mask & arch_type)) {
2160 printf("Option %s not supported for this target\n", popt->name);
2161 exit(1);
2163 switch(popt->index) {
2164 case QEMU_OPTION_M:
2165 machine = find_machine(optarg);
2166 if (!machine) {
2167 QEMUMachine *m;
2168 printf("Supported machines are:\n");
2169 for(m = first_machine; m != NULL; m = m->next) {
2170 if (m->alias)
2171 printf("%-10s %s (alias of %s)\n",
2172 m->alias, m->desc, m->name);
2173 printf("%-10s %s%s\n",
2174 m->name, m->desc,
2175 m->is_default ? " (default)" : "");
2177 exit(*optarg != '?');
2179 break;
2180 case QEMU_OPTION_cpu:
2181 /* hw initialization will check this */
2182 if (*optarg == '?') {
2183 list_cpus(stdout, &fprintf, optarg);
2184 exit(0);
2185 } else {
2186 cpu_model = optarg;
2188 break;
2189 case QEMU_OPTION_initrd:
2190 initrd_filename = optarg;
2191 break;
2192 case QEMU_OPTION_hda:
2194 char buf[256];
2195 if (cyls == 0)
2196 snprintf(buf, sizeof(buf), "%s", HD_OPTS);
2197 else
2198 snprintf(buf, sizeof(buf),
2199 "%s,cyls=%d,heads=%d,secs=%d%s",
2200 HD_OPTS , cyls, heads, secs,
2201 translation == BIOS_ATA_TRANSLATION_LBA ?
2202 ",trans=lba" :
2203 translation == BIOS_ATA_TRANSLATION_NONE ?
2204 ",trans=none" : "");
2205 drive_add(IF_DEFAULT, 0, optarg, buf);
2206 break;
2208 case QEMU_OPTION_hdb:
2209 case QEMU_OPTION_hdc:
2210 case QEMU_OPTION_hdd:
2211 drive_add(IF_DEFAULT, popt->index - QEMU_OPTION_hda, optarg,
2212 HD_OPTS);
2213 break;
2214 case QEMU_OPTION_drive:
2215 if (drive_def(optarg) == NULL) {
2216 exit(1);
2218 break;
2219 case QEMU_OPTION_set:
2220 if (qemu_set_option(optarg) != 0)
2221 exit(1);
2222 break;
2223 case QEMU_OPTION_global:
2224 if (qemu_global_option(optarg) != 0)
2225 exit(1);
2226 break;
2227 case QEMU_OPTION_mtdblock:
2228 drive_add(IF_MTD, -1, optarg, MTD_OPTS);
2229 break;
2230 case QEMU_OPTION_sd:
2231 drive_add(IF_SD, 0, optarg, SD_OPTS);
2232 break;
2233 case QEMU_OPTION_pflash:
2234 drive_add(IF_PFLASH, -1, optarg, PFLASH_OPTS);
2235 break;
2236 case QEMU_OPTION_snapshot:
2237 snapshot = 1;
2238 break;
2239 case QEMU_OPTION_hdachs:
2241 const char *p;
2242 p = optarg;
2243 cyls = strtol(p, (char **)&p, 0);
2244 if (cyls < 1 || cyls > 16383)
2245 goto chs_fail;
2246 if (*p != ',')
2247 goto chs_fail;
2248 p++;
2249 heads = strtol(p, (char **)&p, 0);
2250 if (heads < 1 || heads > 16)
2251 goto chs_fail;
2252 if (*p != ',')
2253 goto chs_fail;
2254 p++;
2255 secs = strtol(p, (char **)&p, 0);
2256 if (secs < 1 || secs > 63)
2257 goto chs_fail;
2258 if (*p == ',') {
2259 p++;
2260 if (!strcmp(p, "none"))
2261 translation = BIOS_ATA_TRANSLATION_NONE;
2262 else if (!strcmp(p, "lba"))
2263 translation = BIOS_ATA_TRANSLATION_LBA;
2264 else if (!strcmp(p, "auto"))
2265 translation = BIOS_ATA_TRANSLATION_AUTO;
2266 else
2267 goto chs_fail;
2268 } else if (*p != '\0') {
2269 chs_fail:
2270 fprintf(stderr, "qemu: invalid physical CHS format\n");
2271 exit(1);
2273 if (hda_opts != NULL) {
2274 char num[16];
2275 snprintf(num, sizeof(num), "%d", cyls);
2276 qemu_opt_set(hda_opts, "cyls", num);
2277 snprintf(num, sizeof(num), "%d", heads);
2278 qemu_opt_set(hda_opts, "heads", num);
2279 snprintf(num, sizeof(num), "%d", secs);
2280 qemu_opt_set(hda_opts, "secs", num);
2281 if (translation == BIOS_ATA_TRANSLATION_LBA)
2282 qemu_opt_set(hda_opts, "trans", "lba");
2283 if (translation == BIOS_ATA_TRANSLATION_NONE)
2284 qemu_opt_set(hda_opts, "trans", "none");
2287 break;
2288 case QEMU_OPTION_numa:
2289 if (nb_numa_nodes >= MAX_NODES) {
2290 fprintf(stderr, "qemu: too many NUMA nodes\n");
2291 exit(1);
2293 numa_add(optarg);
2294 break;
2295 case QEMU_OPTION_display:
2296 display_type = select_display(optarg);
2297 break;
2298 case QEMU_OPTION_nographic:
2299 display_type = DT_NOGRAPHIC;
2300 break;
2301 case QEMU_OPTION_curses:
2302 #ifdef CONFIG_CURSES
2303 display_type = DT_CURSES;
2304 #else
2305 fprintf(stderr, "Curses support is disabled\n");
2306 exit(1);
2307 #endif
2308 break;
2309 case QEMU_OPTION_portrait:
2310 graphic_rotate = 1;
2311 break;
2312 case QEMU_OPTION_kernel:
2313 kernel_filename = optarg;
2314 break;
2315 case QEMU_OPTION_append:
2316 kernel_cmdline = optarg;
2317 break;
2318 case QEMU_OPTION_cdrom:
2319 drive_add(IF_DEFAULT, 2, optarg, CDROM_OPTS);
2320 break;
2321 case QEMU_OPTION_boot:
2323 static const char * const params[] = {
2324 "order", "once", "menu", NULL
2326 char buf[sizeof(boot_devices)];
2327 char *standard_boot_devices;
2328 int legacy = 0;
2330 if (!strchr(optarg, '=')) {
2331 legacy = 1;
2332 pstrcpy(buf, sizeof(buf), optarg);
2333 } else if (check_params(buf, sizeof(buf), params, optarg) < 0) {
2334 fprintf(stderr,
2335 "qemu: unknown boot parameter '%s' in '%s'\n",
2336 buf, optarg);
2337 exit(1);
2340 if (legacy ||
2341 get_param_value(buf, sizeof(buf), "order", optarg)) {
2342 validate_bootdevices(buf);
2343 pstrcpy(boot_devices, sizeof(boot_devices), buf);
2345 if (!legacy) {
2346 if (get_param_value(buf, sizeof(buf),
2347 "once", optarg)) {
2348 validate_bootdevices(buf);
2349 standard_boot_devices = qemu_strdup(boot_devices);
2350 pstrcpy(boot_devices, sizeof(boot_devices), buf);
2351 qemu_register_reset(restore_boot_devices,
2352 standard_boot_devices);
2354 if (get_param_value(buf, sizeof(buf),
2355 "menu", optarg)) {
2356 if (!strcmp(buf, "on")) {
2357 boot_menu = 1;
2358 } else if (!strcmp(buf, "off")) {
2359 boot_menu = 0;
2360 } else {
2361 fprintf(stderr,
2362 "qemu: invalid option value '%s'\n",
2363 buf);
2364 exit(1);
2369 break;
2370 case QEMU_OPTION_fda:
2371 case QEMU_OPTION_fdb:
2372 drive_add(IF_FLOPPY, popt->index - QEMU_OPTION_fda,
2373 optarg, FD_OPTS);
2374 break;
2375 case QEMU_OPTION_no_fd_bootchk:
2376 fd_bootchk = 0;
2377 break;
2378 case QEMU_OPTION_netdev:
2379 if (net_client_parse(qemu_find_opts("netdev"), optarg) == -1) {
2380 exit(1);
2382 break;
2383 case QEMU_OPTION_net:
2384 if (net_client_parse(qemu_find_opts("net"), optarg) == -1) {
2385 exit(1);
2387 break;
2388 #ifdef CONFIG_SLIRP
2389 case QEMU_OPTION_tftp:
2390 legacy_tftp_prefix = optarg;
2391 break;
2392 case QEMU_OPTION_bootp:
2393 legacy_bootp_filename = optarg;
2394 break;
2395 case QEMU_OPTION_redir:
2396 if (net_slirp_redir(optarg) < 0)
2397 exit(1);
2398 break;
2399 #endif
2400 case QEMU_OPTION_bt:
2401 add_device_config(DEV_BT, optarg);
2402 break;
2403 case QEMU_OPTION_audio_help:
2404 if (!(audio_available())) {
2405 printf("Option %s not supported for this target\n", popt->name);
2406 exit(1);
2408 AUD_help ();
2409 exit (0);
2410 break;
2411 case QEMU_OPTION_soundhw:
2412 if (!(audio_available())) {
2413 printf("Option %s not supported for this target\n", popt->name);
2414 exit(1);
2416 select_soundhw (optarg);
2417 break;
2418 case QEMU_OPTION_h:
2419 help(0);
2420 break;
2421 case QEMU_OPTION_version:
2422 version();
2423 exit(0);
2424 break;
2425 case QEMU_OPTION_m: {
2426 int64_t value;
2428 value = strtosz(optarg, NULL);
2429 if (value < 0) {
2430 fprintf(stderr, "qemu: invalid ram size: %s\n", optarg);
2431 exit(1);
2434 /* On 32-bit hosts, QEMU is limited by virtual address space */
2435 if (value > (2047 << 20) && HOST_LONG_BITS == 32) {
2436 fprintf(stderr, "qemu: at most 2047 MB RAM can be simulated\n");
2437 exit(1);
2439 if (value != (uint64_t)(ram_addr_t)value) {
2440 fprintf(stderr, "qemu: ram size too large\n");
2441 exit(1);
2443 ram_size = value;
2444 break;
2446 case QEMU_OPTION_mempath:
2447 mem_path = optarg;
2448 break;
2449 #ifdef MAP_POPULATE
2450 case QEMU_OPTION_mem_prealloc:
2451 mem_prealloc = 1;
2452 break;
2453 #endif
2454 case QEMU_OPTION_d:
2455 set_cpu_log(optarg);
2456 break;
2457 case QEMU_OPTION_s:
2458 gdbstub_dev = "tcp::" DEFAULT_GDBSTUB_PORT;
2459 break;
2460 case QEMU_OPTION_gdb:
2461 gdbstub_dev = optarg;
2462 break;
2463 case QEMU_OPTION_L:
2464 data_dir = optarg;
2465 break;
2466 case QEMU_OPTION_bios:
2467 bios_name = optarg;
2468 break;
2469 case QEMU_OPTION_singlestep:
2470 singlestep = 1;
2471 break;
2472 case QEMU_OPTION_S:
2473 autostart = 0;
2474 break;
2475 case QEMU_OPTION_k:
2476 keyboard_layout = optarg;
2477 break;
2478 case QEMU_OPTION_localtime:
2479 rtc_utc = 0;
2480 break;
2481 case QEMU_OPTION_vga:
2482 select_vgahw (optarg);
2483 break;
2484 case QEMU_OPTION_g:
2486 const char *p;
2487 int w, h, depth;
2488 p = optarg;
2489 w = strtol(p, (char **)&p, 10);
2490 if (w <= 0) {
2491 graphic_error:
2492 fprintf(stderr, "qemu: invalid resolution or depth\n");
2493 exit(1);
2495 if (*p != 'x')
2496 goto graphic_error;
2497 p++;
2498 h = strtol(p, (char **)&p, 10);
2499 if (h <= 0)
2500 goto graphic_error;
2501 if (*p == 'x') {
2502 p++;
2503 depth = strtol(p, (char **)&p, 10);
2504 if (depth != 8 && depth != 15 && depth != 16 &&
2505 depth != 24 && depth != 32)
2506 goto graphic_error;
2507 } else if (*p == '\0') {
2508 depth = graphic_depth;
2509 } else {
2510 goto graphic_error;
2513 graphic_width = w;
2514 graphic_height = h;
2515 graphic_depth = depth;
2517 break;
2518 case QEMU_OPTION_echr:
2520 char *r;
2521 term_escape_char = strtol(optarg, &r, 0);
2522 if (r == optarg)
2523 printf("Bad argument to echr\n");
2524 break;
2526 case QEMU_OPTION_monitor:
2527 monitor_parse(optarg, "readline");
2528 default_monitor = 0;
2529 break;
2530 case QEMU_OPTION_qmp:
2531 monitor_parse(optarg, "control");
2532 default_monitor = 0;
2533 break;
2534 case QEMU_OPTION_mon:
2535 opts = qemu_opts_parse(qemu_find_opts("mon"), optarg, 1);
2536 if (!opts) {
2537 exit(1);
2539 default_monitor = 0;
2540 break;
2541 case QEMU_OPTION_chardev:
2542 opts = qemu_opts_parse(qemu_find_opts("chardev"), optarg, 1);
2543 if (!opts) {
2544 exit(1);
2546 break;
2547 case QEMU_OPTION_fsdev:
2548 olist = qemu_find_opts("fsdev");
2549 if (!olist) {
2550 fprintf(stderr, "fsdev is not supported by this qemu build.\n");
2551 exit(1);
2553 opts = qemu_opts_parse(olist, optarg, 1);
2554 if (!opts) {
2555 fprintf(stderr, "parse error: %s\n", optarg);
2556 exit(1);
2558 break;
2559 case QEMU_OPTION_virtfs: {
2560 QemuOpts *fsdev;
2561 QemuOpts *device;
2563 olist = qemu_find_opts("virtfs");
2564 if (!olist) {
2565 fprintf(stderr, "virtfs is not supported by this qemu build.\n");
2566 exit(1);
2568 opts = qemu_opts_parse(olist, optarg, 1);
2569 if (!opts) {
2570 fprintf(stderr, "parse error: %s\n", optarg);
2571 exit(1);
2574 if (qemu_opt_get(opts, "fstype") == NULL ||
2575 qemu_opt_get(opts, "mount_tag") == NULL ||
2576 qemu_opt_get(opts, "path") == NULL ||
2577 qemu_opt_get(opts, "security_model") == NULL) {
2578 fprintf(stderr, "Usage: -virtfs fstype,path=/share_path/,"
2579 "security_model=[mapped|passthrough|none],"
2580 "mount_tag=tag.\n");
2581 exit(1);
2584 fsdev = qemu_opts_create(qemu_find_opts("fsdev"),
2585 qemu_opt_get(opts, "mount_tag"), 1);
2586 if (!fsdev) {
2587 fprintf(stderr, "duplicate fsdev id: %s\n",
2588 qemu_opt_get(opts, "mount_tag"));
2589 exit(1);
2591 qemu_opt_set(fsdev, "fstype", qemu_opt_get(opts, "fstype"));
2592 qemu_opt_set(fsdev, "path", qemu_opt_get(opts, "path"));
2593 qemu_opt_set(fsdev, "security_model",
2594 qemu_opt_get(opts, "security_model"));
2596 device = qemu_opts_create(qemu_find_opts("device"), NULL, 0);
2597 qemu_opt_set(device, "driver", "virtio-9p-pci");
2598 qemu_opt_set(device, "fsdev",
2599 qemu_opt_get(opts, "mount_tag"));
2600 qemu_opt_set(device, "mount_tag",
2601 qemu_opt_get(opts, "mount_tag"));
2602 break;
2604 case QEMU_OPTION_serial:
2605 add_device_config(DEV_SERIAL, optarg);
2606 default_serial = 0;
2607 if (strncmp(optarg, "mon:", 4) == 0) {
2608 default_monitor = 0;
2610 break;
2611 case QEMU_OPTION_watchdog:
2612 if (watchdog) {
2613 fprintf(stderr,
2614 "qemu: only one watchdog option may be given\n");
2615 return 1;
2617 watchdog = optarg;
2618 break;
2619 case QEMU_OPTION_watchdog_action:
2620 if (select_watchdog_action(optarg) == -1) {
2621 fprintf(stderr, "Unknown -watchdog-action parameter\n");
2622 exit(1);
2624 break;
2625 case QEMU_OPTION_virtiocon:
2626 add_device_config(DEV_VIRTCON, optarg);
2627 default_virtcon = 0;
2628 if (strncmp(optarg, "mon:", 4) == 0) {
2629 default_monitor = 0;
2631 break;
2632 case QEMU_OPTION_parallel:
2633 add_device_config(DEV_PARALLEL, optarg);
2634 default_parallel = 0;
2635 if (strncmp(optarg, "mon:", 4) == 0) {
2636 default_monitor = 0;
2638 break;
2639 case QEMU_OPTION_debugcon:
2640 add_device_config(DEV_DEBUGCON, optarg);
2641 break;
2642 case QEMU_OPTION_loadvm:
2643 loadvm = optarg;
2644 break;
2645 case QEMU_OPTION_full_screen:
2646 full_screen = 1;
2647 break;
2648 #ifdef CONFIG_SDL
2649 case QEMU_OPTION_no_frame:
2650 no_frame = 1;
2651 break;
2652 case QEMU_OPTION_alt_grab:
2653 alt_grab = 1;
2654 break;
2655 case QEMU_OPTION_ctrl_grab:
2656 ctrl_grab = 1;
2657 break;
2658 case QEMU_OPTION_no_quit:
2659 no_quit = 1;
2660 break;
2661 case QEMU_OPTION_sdl:
2662 display_type = DT_SDL;
2663 break;
2664 #else
2665 case QEMU_OPTION_no_frame:
2666 case QEMU_OPTION_alt_grab:
2667 case QEMU_OPTION_ctrl_grab:
2668 case QEMU_OPTION_no_quit:
2669 case QEMU_OPTION_sdl:
2670 fprintf(stderr, "SDL support is disabled\n");
2671 exit(1);
2672 #endif
2673 case QEMU_OPTION_pidfile:
2674 pid_file = optarg;
2675 break;
2676 case QEMU_OPTION_win2k_hack:
2677 win2k_install_hack = 1;
2678 break;
2679 case QEMU_OPTION_rtc_td_hack:
2680 rtc_td_hack = 1;
2681 break;
2682 case QEMU_OPTION_acpitable:
2683 do_acpitable_option(optarg);
2684 break;
2685 case QEMU_OPTION_smbios:
2686 do_smbios_option(optarg);
2687 break;
2688 case QEMU_OPTION_enable_kvm:
2689 olist = qemu_find_opts("machine");
2690 qemu_opts_reset(olist);
2691 qemu_opts_parse(olist, "accel=kvm", 0);
2692 break;
2693 case QEMU_OPTION_machine:
2694 olist = qemu_find_opts("machine");
2695 qemu_opts_reset(olist);
2696 opts = qemu_opts_parse(olist, optarg, 0);
2697 if (!opts) {
2698 fprintf(stderr, "parse error: %s\n", optarg);
2699 exit(1);
2701 break;
2702 case QEMU_OPTION_no_kvm:
2703 fprintf(stderr, "Emulation temporarily broken\n");
2704 exit(1);
2705 olist = qemu_find_opts("machine");
2706 qemu_opts_reset(olist);
2707 qemu_opts_parse(olist, "accel=tcg", 0);
2708 break;
2709 #ifdef CONFIG_KVM
2710 case QEMU_OPTION_no_kvm_irqchip: {
2711 kvm_irqchip = 0;
2712 kvm_pit = 0;
2713 break;
2715 case QEMU_OPTION_no_kvm_pit: {
2716 kvm_pit = 0;
2717 break;
2719 case QEMU_OPTION_no_kvm_pit_reinjection: {
2720 kvm_pit_reinject = 0;
2721 break;
2723 case QEMU_OPTION_enable_nesting: {
2724 kvm_nested = 1;
2725 break;
2727 #endif
2728 case QEMU_OPTION_usb:
2729 usb_enabled = 1;
2730 break;
2731 case QEMU_OPTION_usbdevice:
2732 usb_enabled = 1;
2733 add_device_config(DEV_USB, optarg);
2734 break;
2735 case QEMU_OPTION_device:
2736 if (!qemu_opts_parse(qemu_find_opts("device"), optarg, 1)) {
2737 exit(1);
2739 break;
2740 case QEMU_OPTION_smp:
2741 smp_parse(optarg);
2742 if (smp_cpus < 1) {
2743 fprintf(stderr, "Invalid number of CPUs\n");
2744 exit(1);
2746 if (max_cpus < smp_cpus) {
2747 fprintf(stderr, "maxcpus must be equal to or greater than "
2748 "smp\n");
2749 exit(1);
2751 if (max_cpus > 255) {
2752 fprintf(stderr, "Unsupported number of maxcpus\n");
2753 exit(1);
2755 break;
2756 case QEMU_OPTION_vnc:
2757 #ifdef CONFIG_VNC
2758 display_remote++;
2759 vnc_display = optarg;
2760 #else
2761 fprintf(stderr, "VNC support is disabled\n");
2762 exit(1);
2763 #endif
2764 break;
2765 case QEMU_OPTION_no_acpi:
2766 acpi_enabled = 0;
2767 break;
2768 case QEMU_OPTION_no_hpet:
2769 no_hpet = 1;
2770 break;
2771 case QEMU_OPTION_balloon:
2772 if (balloon_parse(optarg) < 0) {
2773 fprintf(stderr, "Unknown -balloon argument %s\n", optarg);
2774 exit(1);
2776 break;
2777 case QEMU_OPTION_no_reboot:
2778 no_reboot = 1;
2779 break;
2780 case QEMU_OPTION_no_shutdown:
2781 no_shutdown = 1;
2782 break;
2783 case QEMU_OPTION_show_cursor:
2784 cursor_hide = 0;
2785 break;
2786 case QEMU_OPTION_uuid:
2787 if(qemu_uuid_parse(optarg, qemu_uuid) < 0) {
2788 fprintf(stderr, "Fail to parse UUID string."
2789 " Wrong format.\n");
2790 exit(1);
2792 break;
2793 case QEMU_OPTION_option_rom:
2794 if (nb_option_roms >= MAX_OPTION_ROMS) {
2795 fprintf(stderr, "Too many option ROMs\n");
2796 exit(1);
2798 opts = qemu_opts_parse(qemu_find_opts("option-rom"), optarg, 1);
2799 option_rom[nb_option_roms].name = qemu_opt_get(opts, "romfile");
2800 option_rom[nb_option_roms].bootindex =
2801 qemu_opt_get_number(opts, "bootindex", -1);
2802 if (!option_rom[nb_option_roms].name) {
2803 fprintf(stderr, "Option ROM file is not specified\n");
2804 exit(1);
2806 nb_option_roms++;
2807 break;
2808 case QEMU_OPTION_semihosting:
2809 semihosting_enabled = 1;
2810 break;
2811 case QEMU_OPTION_tdf:
2812 time_drift_fix = 1;
2813 break;
2814 case QEMU_OPTION_kvm_shadow_memory:
2815 kvm_shadow_memory = (int64_t)atoi(optarg) * 1024 * 1024 / 4096;
2816 break;
2817 case QEMU_OPTION_name:
2818 qemu_name = qemu_strdup(optarg);
2820 char *p = strchr(qemu_name, ',');
2821 if (p != NULL) {
2822 *p++ = 0;
2823 if (strncmp(p, "process=", 8)) {
2824 fprintf(stderr, "Unknown subargument %s to -name\n", p);
2825 exit(1);
2827 p += 8;
2828 os_set_proc_name(p);
2831 break;
2832 case QEMU_OPTION_prom_env:
2833 if (nb_prom_envs >= MAX_PROM_ENVS) {
2834 fprintf(stderr, "Too many prom variables\n");
2835 exit(1);
2837 prom_envs[nb_prom_envs] = optarg;
2838 nb_prom_envs++;
2839 break;
2840 case QEMU_OPTION_old_param:
2841 old_param = 1;
2842 break;
2843 case QEMU_OPTION_clock:
2844 configure_alarms(optarg);
2845 break;
2846 case QEMU_OPTION_startdate:
2847 configure_rtc_date_offset(optarg, 1);
2848 break;
2849 case QEMU_OPTION_rtc:
2850 opts = qemu_opts_parse(qemu_find_opts("rtc"), optarg, 0);
2851 if (!opts) {
2852 exit(1);
2854 configure_rtc(opts);
2855 break;
2856 case QEMU_OPTION_tb_size:
2857 tb_size = strtol(optarg, NULL, 0);
2858 if (tb_size < 0)
2859 tb_size = 0;
2860 break;
2861 case QEMU_OPTION_icount:
2862 icount_option = optarg;
2863 break;
2864 case QEMU_OPTION_incoming:
2865 incoming = optarg;
2866 incoming_expected = true;
2867 break;
2868 case QEMU_OPTION_nodefaults:
2869 default_serial = 0;
2870 default_parallel = 0;
2871 default_virtcon = 0;
2872 default_monitor = 0;
2873 default_vga = 0;
2874 default_net = 0;
2875 default_floppy = 0;
2876 default_cdrom = 0;
2877 default_sdcard = 0;
2878 break;
2879 #ifndef _WIN32
2880 case QEMU_OPTION_nvram:
2881 nvram = optarg;
2882 break;
2883 #endif
2884 case QEMU_OPTION_xen_domid:
2885 if (!(xen_available())) {
2886 printf("Option %s not supported for this target\n", popt->name);
2887 exit(1);
2889 xen_domid = atoi(optarg);
2890 break;
2891 case QEMU_OPTION_xen_create:
2892 if (!(xen_available())) {
2893 printf("Option %s not supported for this target\n", popt->name);
2894 exit(1);
2896 xen_mode = XEN_CREATE;
2897 break;
2898 case QEMU_OPTION_xen_attach:
2899 if (!(xen_available())) {
2900 printf("Option %s not supported for this target\n", popt->name);
2901 exit(1);
2903 xen_mode = XEN_ATTACH;
2904 break;
2905 #ifdef CONFIG_SIMPLE_TRACE
2906 case QEMU_OPTION_trace:
2907 opts = qemu_opts_parse(qemu_find_opts("trace"), optarg, 0);
2908 if (opts) {
2909 trace_file = qemu_opt_get(opts, "file");
2911 break;
2912 #endif
2913 case QEMU_OPTION_readconfig:
2915 int ret = qemu_read_config_file(optarg);
2916 if (ret < 0) {
2917 fprintf(stderr, "read config %s: %s\n", optarg,
2918 strerror(-ret));
2919 exit(1);
2921 break;
2923 case QEMU_OPTION_spice:
2924 olist = qemu_find_opts("spice");
2925 if (!olist) {
2926 fprintf(stderr, "spice is not supported by this qemu build.\n");
2927 exit(1);
2929 opts = qemu_opts_parse(olist, optarg, 0);
2930 if (!opts) {
2931 fprintf(stderr, "parse error: %s\n", optarg);
2932 exit(1);
2934 break;
2935 case QEMU_OPTION_writeconfig:
2937 FILE *fp;
2938 if (strcmp(optarg, "-") == 0) {
2939 fp = stdout;
2940 } else {
2941 fp = fopen(optarg, "w");
2942 if (fp == NULL) {
2943 fprintf(stderr, "open %s: %s\n", optarg, strerror(errno));
2944 exit(1);
2947 qemu_config_write(fp);
2948 fclose(fp);
2949 break;
2951 default:
2952 os_parse_cmd_args(popt->index, optarg);
2956 loc_set_none();
2958 if (!st_init(trace_file)) {
2959 fprintf(stderr, "warning: unable to initialize simple trace backend\n");
2962 /* If no data_dir is specified then try to find it relative to the
2963 executable path. */
2964 if (!data_dir) {
2965 data_dir = os_find_datadir(argv[0]);
2967 /* If all else fails use the install patch specified when building. */
2968 if (!data_dir) {
2969 data_dir = CONFIG_QEMU_DATADIR;
2973 * Default to max_cpus = smp_cpus, in case the user doesn't
2974 * specify a max_cpus value.
2976 if (!max_cpus)
2977 max_cpus = smp_cpus;
2979 machine->max_cpus = machine->max_cpus ?: 1; /* Default to UP */
2980 if (smp_cpus > machine->max_cpus) {
2981 fprintf(stderr, "Number of SMP cpus requested (%d), exceeds max cpus "
2982 "supported by machine `%s' (%d)\n", smp_cpus, machine->name,
2983 machine->max_cpus);
2984 exit(1);
2988 * Get the default machine options from the machine if it is not already
2989 * specified either by the configuration file or by the command line.
2991 if (machine->default_machine_opts) {
2992 QemuOptsList *list = qemu_find_opts("machine");
2993 const char *p = NULL;
2995 if (!QTAILQ_EMPTY(&list->head)) {
2996 p = qemu_opt_get(QTAILQ_FIRST(&list->head), "accel");
2998 if (p == NULL) {
2999 opts = qemu_opts_parse(qemu_find_opts("machine"),
3000 machine->default_machine_opts, 0);
3001 if (!opts) {
3002 fprintf(stderr, "parse error for machine %s: %s\n",
3003 machine->name, machine->default_machine_opts);
3004 exit(1);
3009 qemu_opts_foreach(qemu_find_opts("device"), default_driver_check, NULL, 0);
3010 qemu_opts_foreach(qemu_find_opts("global"), default_driver_check, NULL, 0);
3012 if (machine->no_serial) {
3013 default_serial = 0;
3015 if (machine->no_parallel) {
3016 default_parallel = 0;
3018 if (!machine->use_virtcon) {
3019 default_virtcon = 0;
3021 if (machine->no_vga) {
3022 default_vga = 0;
3024 if (machine->no_floppy) {
3025 default_floppy = 0;
3027 if (machine->no_cdrom) {
3028 default_cdrom = 0;
3030 if (machine->no_sdcard) {
3031 default_sdcard = 0;
3034 if (display_type == DT_NOGRAPHIC) {
3035 if (default_parallel)
3036 add_device_config(DEV_PARALLEL, "null");
3037 if (default_serial && default_monitor) {
3038 add_device_config(DEV_SERIAL, "mon:stdio");
3039 } else if (default_virtcon && default_monitor) {
3040 add_device_config(DEV_VIRTCON, "mon:stdio");
3041 } else {
3042 if (default_serial)
3043 add_device_config(DEV_SERIAL, "stdio");
3044 if (default_virtcon)
3045 add_device_config(DEV_VIRTCON, "stdio");
3046 if (default_monitor)
3047 monitor_parse("stdio", "readline");
3049 } else {
3050 if (default_serial)
3051 add_device_config(DEV_SERIAL, "vc:80Cx24C");
3052 if (default_parallel)
3053 add_device_config(DEV_PARALLEL, "vc:80Cx24C");
3054 if (default_monitor)
3055 monitor_parse("vc:80Cx24C", "readline");
3056 if (default_virtcon)
3057 add_device_config(DEV_VIRTCON, "vc:80Cx24C");
3059 if (default_vga)
3060 vga_interface_type = VGA_CIRRUS;
3062 socket_init();
3064 if (qemu_opts_foreach(qemu_find_opts("chardev"), chardev_init_func, NULL, 1) != 0)
3065 exit(1);
3066 #ifdef CONFIG_VIRTFS
3067 if (qemu_opts_foreach(qemu_find_opts("fsdev"), fsdev_init_func, NULL, 1) != 0) {
3068 exit(1);
3070 #endif
3072 os_daemonize();
3074 if (pid_file && qemu_create_pidfile(pid_file) != 0) {
3075 os_pidfile_error();
3076 exit(1);
3079 configure_accelerator();
3081 if (qemu_init_main_loop()) {
3082 fprintf(stderr, "qemu_init_main_loop failed\n");
3083 exit(1);
3085 linux_boot = (kernel_filename != NULL);
3087 if (!linux_boot && *kernel_cmdline != '\0') {
3088 fprintf(stderr, "-append only allowed with -kernel option\n");
3089 exit(1);
3092 if (!linux_boot && initrd_filename != NULL) {
3093 fprintf(stderr, "-initrd only allowed with -kernel option\n");
3094 exit(1);
3097 os_set_line_buffering();
3099 if (init_timer_alarm() < 0) {
3100 fprintf(stderr, "could not initialize alarm timer\n");
3101 exit(1);
3103 configure_icount(icount_option);
3105 if (net_init_clients() < 0) {
3106 exit(1);
3109 /* init the bluetooth world */
3110 if (foreach_device_config(DEV_BT, bt_parse))
3111 exit(1);
3113 /* init the memory */
3114 if (ram_size == 0)
3115 ram_size = DEFAULT_RAM_SIZE * 1024 * 1024;
3117 /* init the dynamic translator */
3118 cpu_exec_init_all(tb_size * 1024 * 1024);
3120 bdrv_init_with_whitelist();
3122 blk_mig_init();
3124 /* open the virtual block devices */
3125 if (snapshot)
3126 qemu_opts_foreach(qemu_find_opts("drive"), drive_enable_snapshot, NULL, 0);
3127 if (qemu_opts_foreach(qemu_find_opts("drive"), drive_init_func, &machine->use_scsi, 1) != 0)
3128 exit(1);
3130 default_drive(default_cdrom, snapshot, machine->use_scsi,
3131 IF_DEFAULT, 2, CDROM_OPTS);
3132 default_drive(default_floppy, snapshot, machine->use_scsi,
3133 IF_FLOPPY, 0, FD_OPTS);
3134 default_drive(default_sdcard, snapshot, machine->use_scsi,
3135 IF_SD, 0, SD_OPTS);
3137 register_savevm_live(NULL, "ram", 0, 4, NULL, ram_save_live, NULL,
3138 ram_load, NULL);
3140 if (nb_numa_nodes > 0) {
3141 int i;
3143 if (nb_numa_nodes > smp_cpus) {
3144 nb_numa_nodes = smp_cpus;
3147 /* If no memory size if given for any node, assume the default case
3148 * and distribute the available memory equally across all nodes
3150 for (i = 0; i < nb_numa_nodes; i++) {
3151 if (node_mem[i] != 0)
3152 break;
3154 if (i == nb_numa_nodes) {
3155 uint64_t usedmem = 0;
3157 /* On Linux, the each node's border has to be 8MB aligned,
3158 * the final node gets the rest.
3160 for (i = 0; i < nb_numa_nodes - 1; i++) {
3161 node_mem[i] = (ram_size / nb_numa_nodes) & ~((1 << 23UL) - 1);
3162 usedmem += node_mem[i];
3164 node_mem[i] = ram_size - usedmem;
3167 for (i = 0; i < nb_numa_nodes; i++) {
3168 if (node_cpumask[i] != 0)
3169 break;
3171 /* assigning the VCPUs round-robin is easier to implement, guest OSes
3172 * must cope with this anyway, because there are BIOSes out there in
3173 * real machines which also use this scheme.
3175 if (i == nb_numa_nodes) {
3176 for (i = 0; i < smp_cpus; i++) {
3177 node_cpumask[i % nb_numa_nodes] |= 1 << i;
3182 if (qemu_opts_foreach(qemu_find_opts("mon"), mon_init_func, NULL, 1) != 0) {
3183 exit(1);
3186 if (foreach_device_config(DEV_SERIAL, serial_parse) < 0)
3187 exit(1);
3188 if (foreach_device_config(DEV_PARALLEL, parallel_parse) < 0)
3189 exit(1);
3190 if (foreach_device_config(DEV_VIRTCON, virtcon_parse) < 0)
3191 exit(1);
3192 if (foreach_device_config(DEV_DEBUGCON, debugcon_parse) < 0)
3193 exit(1);
3195 module_call_init(MODULE_INIT_DEVICE);
3197 if (qemu_opts_foreach(qemu_find_opts("device"), device_help_func, NULL, 0) != 0)
3198 exit(0);
3200 if (watchdog) {
3201 i = select_watchdog(watchdog);
3202 if (i > 0)
3203 exit (i == 1 ? 1 : 0);
3206 if (machine->compat_props) {
3207 qdev_prop_register_global_list(machine->compat_props);
3209 qemu_add_globals();
3211 machine->init(ram_size, boot_devices,
3212 kernel_filename, kernel_cmdline, initrd_filename, cpu_model);
3214 cpu_synchronize_all_post_init();
3216 set_numa_modes();
3218 current_machine = machine;
3220 /* init USB devices */
3221 if (usb_enabled) {
3222 if (foreach_device_config(DEV_USB, usb_parse) < 0)
3223 exit(1);
3226 /* init generic devices */
3227 if (qemu_opts_foreach(qemu_find_opts("device"), device_init_func, NULL, 1) != 0)
3228 exit(1);
3230 net_check_clients();
3232 /* just use the first displaystate for the moment */
3233 ds = get_displaystate();
3235 if (using_spice)
3236 display_remote++;
3237 if (display_type == DT_DEFAULT && !display_remote) {
3238 #if defined(CONFIG_SDL) || defined(CONFIG_COCOA)
3239 display_type = DT_SDL;
3240 #elif defined(CONFIG_VNC)
3241 vnc_display = "localhost:0,to=99";
3242 show_vnc_port = 1;
3243 #else
3244 display_type = DT_NONE;
3245 #endif
3249 /* init local displays */
3250 switch (display_type) {
3251 case DT_NOGRAPHIC:
3252 break;
3253 #if defined(CONFIG_CURSES)
3254 case DT_CURSES:
3255 curses_display_init(ds, full_screen);
3256 break;
3257 #endif
3258 #if defined(CONFIG_SDL)
3259 case DT_SDL:
3260 sdl_display_init(ds, full_screen, no_frame);
3261 break;
3262 #elif defined(CONFIG_COCOA)
3263 case DT_SDL:
3264 cocoa_display_init(ds, full_screen);
3265 break;
3266 #endif
3267 default:
3268 break;
3271 /* must be after terminal init, SDL library changes signal handlers */
3272 os_setup_signal_handling();
3274 #ifdef CONFIG_VNC
3275 /* init remote displays */
3276 if (vnc_display) {
3277 vnc_display_init(ds);
3278 if (vnc_display_open(ds, vnc_display) < 0)
3279 exit(1);
3281 if (show_vnc_port) {
3282 printf("VNC server running on `%s'\n", vnc_display_local_addr(ds));
3285 #endif
3286 #ifdef CONFIG_SPICE
3287 if (using_spice && !qxl_enabled) {
3288 qemu_spice_display_init(ds);
3290 #endif
3292 /* display setup */
3293 dpy_resize(ds);
3294 dcl = ds->listeners;
3295 while (dcl != NULL) {
3296 if (dcl->dpy_refresh != NULL) {
3297 ds->gui_timer = qemu_new_timer_ms(rt_clock, gui_update, ds);
3298 qemu_mod_timer(ds->gui_timer, qemu_get_clock_ms(rt_clock));
3299 break;
3301 dcl = dcl->next;
3303 if (ds->gui_timer == NULL) {
3304 nographic_timer = qemu_new_timer_ms(rt_clock, nographic_update, NULL);
3305 qemu_mod_timer(nographic_timer, qemu_get_clock_ms(rt_clock));
3307 text_consoles_set_display(ds);
3309 if (gdbstub_dev && gdbserver_start(gdbstub_dev) < 0) {
3310 fprintf(stderr, "qemu: could not open gdbserver on device '%s'\n",
3311 gdbstub_dev);
3312 exit(1);
3315 qdev_machine_creation_done();
3317 if (rom_load_all() != 0) {
3318 fprintf(stderr, "rom loading failed\n");
3319 exit(1);
3322 /* TODO: once all bus devices are qdevified, this should be done
3323 * when bus is created by qdev.c */
3324 qemu_register_reset(qbus_reset_all_fn, sysbus_get_default());
3325 qemu_run_machine_init_done_notifiers();
3327 qemu_system_reset();
3328 if (loadvm) {
3329 if (load_vmstate(loadvm) < 0) {
3330 autostart = 0;
3334 if (incoming) {
3335 int ret = qemu_start_incoming_migration(incoming);
3336 if (ret < 0) {
3337 fprintf(stderr, "Migration failed. Exit code %s(%d), exiting.\n",
3338 incoming, ret);
3339 exit(ret);
3341 } else if (autostart) {
3342 vm_start();
3345 os_setup_post();
3347 main_loop();
3348 quit_timers();
3349 net_cleanup();
3351 return 0;