Merge commit '60e0df25e415b00cf35c4d214eaba9dc19aaa9e6' into upstream-merge
[qemu/qemu-dev-zwu.git] / vl.c
blobbd79e61a40b11d52292f574dff65cd1dfa128a6a
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 uint8_t irq0override = 1;
221 const char *watchdog;
222 QEMUOptionRom option_rom[MAX_OPTION_ROMS];
223 int nb_option_roms;
224 int semihosting_enabled = 0;
225 int time_drift_fix = 0;
226 unsigned int kvm_shadow_memory = 0;
227 int old_param = 0;
228 const char *qemu_name;
229 int alt_grab = 0;
230 int ctrl_grab = 0;
231 unsigned int nb_prom_envs = 0;
232 const char *prom_envs[MAX_PROM_ENVS];
233 const char *nvram = NULL;
234 int boot_menu;
236 typedef struct FWBootEntry FWBootEntry;
238 struct FWBootEntry {
239 QTAILQ_ENTRY(FWBootEntry) link;
240 int32_t bootindex;
241 DeviceState *dev;
242 char *suffix;
245 QTAILQ_HEAD(, FWBootEntry) fw_boot_order = QTAILQ_HEAD_INITIALIZER(fw_boot_order);
247 int nb_numa_nodes;
248 uint64_t node_mem[MAX_NODES];
249 uint64_t node_cpumask[MAX_NODES];
251 static QEMUTimer *nographic_timer;
253 uint8_t qemu_uuid[16];
255 static QEMUBootSetHandler *boot_set_handler;
256 static void *boot_set_opaque;
258 static NotifierList exit_notifiers =
259 NOTIFIER_LIST_INITIALIZER(exit_notifiers);
261 static NotifierList machine_init_done_notifiers =
262 NOTIFIER_LIST_INITIALIZER(machine_init_done_notifiers);
264 static int tcg_allowed = 1;
265 int kvm_allowed = 0;
266 int xen_allowed = 0;
267 uint32_t xen_domid;
268 enum xen_mode xen_mode = XEN_EMULATE;
270 static int default_serial = 1;
271 static int default_parallel = 1;
272 static int default_virtcon = 1;
273 static int default_monitor = 1;
274 static int default_vga = 1;
275 static int default_floppy = 1;
276 static int default_cdrom = 1;
277 static int default_sdcard = 1;
279 static struct {
280 const char *driver;
281 int *flag;
282 } default_list[] = {
283 { .driver = "isa-serial", .flag = &default_serial },
284 { .driver = "isa-parallel", .flag = &default_parallel },
285 { .driver = "isa-fdc", .flag = &default_floppy },
286 { .driver = "ide-drive", .flag = &default_cdrom },
287 { .driver = "virtio-serial-pci", .flag = &default_virtcon },
288 { .driver = "virtio-serial-s390", .flag = &default_virtcon },
289 { .driver = "virtio-serial", .flag = &default_virtcon },
290 { .driver = "VGA", .flag = &default_vga },
291 { .driver = "cirrus-vga", .flag = &default_vga },
292 { .driver = "vmware-svga", .flag = &default_vga },
295 static int default_driver_check(QemuOpts *opts, void *opaque)
297 const char *driver = qemu_opt_get(opts, "driver");
298 int i;
300 if (!driver)
301 return 0;
302 for (i = 0; i < ARRAY_SIZE(default_list); i++) {
303 if (strcmp(default_list[i].driver, driver) != 0)
304 continue;
305 *(default_list[i].flag) = 0;
307 return 0;
310 /***********************************************************/
311 /* real time host monotonic timer */
313 /***********************************************************/
314 /* host time/date access */
315 void qemu_get_timedate(struct tm *tm, int offset)
317 time_t ti;
318 struct tm *ret;
320 time(&ti);
321 ti += offset;
322 if (rtc_date_offset == -1) {
323 if (rtc_utc)
324 ret = gmtime(&ti);
325 else
326 ret = localtime(&ti);
327 } else {
328 ti -= rtc_date_offset;
329 ret = gmtime(&ti);
332 memcpy(tm, ret, sizeof(struct tm));
335 int qemu_timedate_diff(struct tm *tm)
337 time_t seconds;
339 if (rtc_date_offset == -1)
340 if (rtc_utc)
341 seconds = mktimegm(tm);
342 else
343 seconds = mktime(tm);
344 else
345 seconds = mktimegm(tm) + rtc_date_offset;
347 return seconds - time(NULL);
350 void rtc_change_mon_event(struct tm *tm)
352 QObject *data;
354 data = qobject_from_jsonf("{ 'offset': %d }", qemu_timedate_diff(tm));
355 monitor_protocol_event(QEVENT_RTC_CHANGE, data);
356 qobject_decref(data);
359 static void configure_rtc_date_offset(const char *startdate, int legacy)
361 time_t rtc_start_date;
362 struct tm tm;
364 if (!strcmp(startdate, "now") && legacy) {
365 rtc_date_offset = -1;
366 } else {
367 if (sscanf(startdate, "%d-%d-%dT%d:%d:%d",
368 &tm.tm_year,
369 &tm.tm_mon,
370 &tm.tm_mday,
371 &tm.tm_hour,
372 &tm.tm_min,
373 &tm.tm_sec) == 6) {
374 /* OK */
375 } else if (sscanf(startdate, "%d-%d-%d",
376 &tm.tm_year,
377 &tm.tm_mon,
378 &tm.tm_mday) == 3) {
379 tm.tm_hour = 0;
380 tm.tm_min = 0;
381 tm.tm_sec = 0;
382 } else {
383 goto date_fail;
385 tm.tm_year -= 1900;
386 tm.tm_mon--;
387 rtc_start_date = mktimegm(&tm);
388 if (rtc_start_date == -1) {
389 date_fail:
390 fprintf(stderr, "Invalid date format. Valid formats are:\n"
391 "'2006-06-17T16:01:21' or '2006-06-17'\n");
392 exit(1);
394 rtc_date_offset = time(NULL) - rtc_start_date;
398 static void configure_rtc(QemuOpts *opts)
400 const char *value;
402 value = qemu_opt_get(opts, "base");
403 if (value) {
404 if (!strcmp(value, "utc")) {
405 rtc_utc = 1;
406 } else if (!strcmp(value, "localtime")) {
407 rtc_utc = 0;
408 } else {
409 configure_rtc_date_offset(value, 0);
412 value = qemu_opt_get(opts, "clock");
413 if (value) {
414 if (!strcmp(value, "host")) {
415 rtc_clock = host_clock;
416 } else if (!strcmp(value, "vm")) {
417 rtc_clock = vm_clock;
418 } else {
419 fprintf(stderr, "qemu: invalid option value '%s'\n", value);
420 exit(1);
423 value = qemu_opt_get(opts, "driftfix");
424 if (value) {
425 if (!strcmp(value, "slew")) {
426 rtc_td_hack = 1;
427 } else if (!strcmp(value, "none")) {
428 rtc_td_hack = 0;
429 } else {
430 fprintf(stderr, "qemu: invalid option value '%s'\n", value);
431 exit(1);
436 /***********************************************************/
437 /* Bluetooth support */
438 static int nb_hcis;
439 static int cur_hci;
440 static struct HCIInfo *hci_table[MAX_NICS];
442 static struct bt_vlan_s {
443 struct bt_scatternet_s net;
444 int id;
445 struct bt_vlan_s *next;
446 } *first_bt_vlan;
448 /* find or alloc a new bluetooth "VLAN" */
449 static struct bt_scatternet_s *qemu_find_bt_vlan(int id)
451 struct bt_vlan_s **pvlan, *vlan;
452 for (vlan = first_bt_vlan; vlan != NULL; vlan = vlan->next) {
453 if (vlan->id == id)
454 return &vlan->net;
456 vlan = qemu_mallocz(sizeof(struct bt_vlan_s));
457 vlan->id = id;
458 pvlan = &first_bt_vlan;
459 while (*pvlan != NULL)
460 pvlan = &(*pvlan)->next;
461 *pvlan = vlan;
462 return &vlan->net;
465 static void null_hci_send(struct HCIInfo *hci, const uint8_t *data, int len)
469 static int null_hci_addr_set(struct HCIInfo *hci, const uint8_t *bd_addr)
471 return -ENOTSUP;
474 static struct HCIInfo null_hci = {
475 .cmd_send = null_hci_send,
476 .sco_send = null_hci_send,
477 .acl_send = null_hci_send,
478 .bdaddr_set = null_hci_addr_set,
481 struct HCIInfo *qemu_next_hci(void)
483 if (cur_hci == nb_hcis)
484 return &null_hci;
486 return hci_table[cur_hci++];
489 static struct HCIInfo *hci_init(const char *str)
491 char *endp;
492 struct bt_scatternet_s *vlan = 0;
494 if (!strcmp(str, "null"))
495 /* null */
496 return &null_hci;
497 else if (!strncmp(str, "host", 4) && (str[4] == '\0' || str[4] == ':'))
498 /* host[:hciN] */
499 return bt_host_hci(str[4] ? str + 5 : "hci0");
500 else if (!strncmp(str, "hci", 3)) {
501 /* hci[,vlan=n] */
502 if (str[3]) {
503 if (!strncmp(str + 3, ",vlan=", 6)) {
504 vlan = qemu_find_bt_vlan(strtol(str + 9, &endp, 0));
505 if (*endp)
506 vlan = 0;
508 } else
509 vlan = qemu_find_bt_vlan(0);
510 if (vlan)
511 return bt_new_hci(vlan);
514 fprintf(stderr, "qemu: Unknown bluetooth HCI `%s'.\n", str);
516 return 0;
519 static int bt_hci_parse(const char *str)
521 struct HCIInfo *hci;
522 bdaddr_t bdaddr;
524 if (nb_hcis >= MAX_NICS) {
525 fprintf(stderr, "qemu: Too many bluetooth HCIs (max %i).\n", MAX_NICS);
526 return -1;
529 hci = hci_init(str);
530 if (!hci)
531 return -1;
533 bdaddr.b[0] = 0x52;
534 bdaddr.b[1] = 0x54;
535 bdaddr.b[2] = 0x00;
536 bdaddr.b[3] = 0x12;
537 bdaddr.b[4] = 0x34;
538 bdaddr.b[5] = 0x56 + nb_hcis;
539 hci->bdaddr_set(hci, bdaddr.b);
541 hci_table[nb_hcis++] = hci;
543 return 0;
546 static void bt_vhci_add(int vlan_id)
548 struct bt_scatternet_s *vlan = qemu_find_bt_vlan(vlan_id);
550 if (!vlan->slave)
551 fprintf(stderr, "qemu: warning: adding a VHCI to "
552 "an empty scatternet %i\n", vlan_id);
554 bt_vhci_init(bt_new_hci(vlan));
557 static struct bt_device_s *bt_device_add(const char *opt)
559 struct bt_scatternet_s *vlan;
560 int vlan_id = 0;
561 char *endp = strstr(opt, ",vlan=");
562 int len = (endp ? endp - opt : strlen(opt)) + 1;
563 char devname[10];
565 pstrcpy(devname, MIN(sizeof(devname), len), opt);
567 if (endp) {
568 vlan_id = strtol(endp + 6, &endp, 0);
569 if (*endp) {
570 fprintf(stderr, "qemu: unrecognised bluetooth vlan Id\n");
571 return 0;
575 vlan = qemu_find_bt_vlan(vlan_id);
577 if (!vlan->slave)
578 fprintf(stderr, "qemu: warning: adding a slave device to "
579 "an empty scatternet %i\n", vlan_id);
581 if (!strcmp(devname, "keyboard"))
582 return bt_keyboard_init(vlan);
584 fprintf(stderr, "qemu: unsupported bluetooth device `%s'\n", devname);
585 return 0;
588 static int bt_parse(const char *opt)
590 const char *endp, *p;
591 int vlan;
593 if (strstart(opt, "hci", &endp)) {
594 if (!*endp || *endp == ',') {
595 if (*endp)
596 if (!strstart(endp, ",vlan=", 0))
597 opt = endp + 1;
599 return bt_hci_parse(opt);
601 } else if (strstart(opt, "vhci", &endp)) {
602 if (!*endp || *endp == ',') {
603 if (*endp) {
604 if (strstart(endp, ",vlan=", &p)) {
605 vlan = strtol(p, (char **) &endp, 0);
606 if (*endp) {
607 fprintf(stderr, "qemu: bad scatternet '%s'\n", p);
608 return 1;
610 } else {
611 fprintf(stderr, "qemu: bad parameter '%s'\n", endp + 1);
612 return 1;
614 } else
615 vlan = 0;
617 bt_vhci_add(vlan);
618 return 0;
620 } else if (strstart(opt, "device:", &endp))
621 return !bt_device_add(endp);
623 fprintf(stderr, "qemu: bad bluetooth parameter '%s'\n", opt);
624 return 1;
627 /***********************************************************/
628 /* QEMU Block devices */
630 #define HD_OPTS "media=disk"
631 #define CDROM_OPTS "media=cdrom"
632 #define FD_OPTS ""
633 #define PFLASH_OPTS ""
634 #define MTD_OPTS ""
635 #define SD_OPTS ""
637 static int drive_init_func(QemuOpts *opts, void *opaque)
639 int *use_scsi = opaque;
641 return drive_init(opts, *use_scsi) == NULL;
644 static int drive_enable_snapshot(QemuOpts *opts, void *opaque)
646 if (NULL == qemu_opt_get(opts, "snapshot")) {
647 qemu_opt_set(opts, "snapshot", "on");
649 return 0;
652 static void default_drive(int enable, int snapshot, int use_scsi,
653 BlockInterfaceType type, int index,
654 const char *optstr)
656 QemuOpts *opts;
658 if (type == IF_DEFAULT) {
659 type = use_scsi ? IF_SCSI : IF_IDE;
662 if (!enable || drive_get_by_index(type, index)) {
663 return;
666 opts = drive_add(type, index, NULL, optstr);
667 if (snapshot) {
668 drive_enable_snapshot(opts, NULL);
670 if (!drive_init(opts, use_scsi)) {
671 exit(1);
675 void qemu_register_boot_set(QEMUBootSetHandler *func, void *opaque)
677 boot_set_handler = func;
678 boot_set_opaque = opaque;
681 int qemu_boot_set(const char *boot_devices)
683 if (!boot_set_handler) {
684 return -EINVAL;
686 return boot_set_handler(boot_set_opaque, boot_devices);
689 static void validate_bootdevices(char *devices)
691 /* We just do some generic consistency checks */
692 const char *p;
693 int bitmap = 0;
695 for (p = devices; *p != '\0'; p++) {
696 /* Allowed boot devices are:
697 * a-b: floppy disk drives
698 * c-f: IDE disk drives
699 * g-m: machine implementation dependant drives
700 * n-p: network devices
701 * It's up to each machine implementation to check if the given boot
702 * devices match the actual hardware implementation and firmware
703 * features.
705 if (*p < 'a' || *p > 'p') {
706 fprintf(stderr, "Invalid boot device '%c'\n", *p);
707 exit(1);
709 if (bitmap & (1 << (*p - 'a'))) {
710 fprintf(stderr, "Boot device '%c' was given twice\n", *p);
711 exit(1);
713 bitmap |= 1 << (*p - 'a');
717 static void restore_boot_devices(void *opaque)
719 char *standard_boot_devices = opaque;
720 static int first = 1;
722 /* Restore boot order and remove ourselves after the first boot */
723 if (first) {
724 first = 0;
725 return;
728 qemu_boot_set(standard_boot_devices);
730 qemu_unregister_reset(restore_boot_devices, standard_boot_devices);
731 qemu_free(standard_boot_devices);
734 void add_boot_device_path(int32_t bootindex, DeviceState *dev,
735 const char *suffix)
737 FWBootEntry *node, *i;
739 if (bootindex < 0) {
740 return;
743 assert(dev != NULL || suffix != NULL);
745 node = qemu_mallocz(sizeof(FWBootEntry));
746 node->bootindex = bootindex;
747 node->suffix = suffix ? qemu_strdup(suffix) : NULL;
748 node->dev = dev;
750 QTAILQ_FOREACH(i, &fw_boot_order, link) {
751 if (i->bootindex == bootindex) {
752 fprintf(stderr, "Two devices with same boot index %d\n", bootindex);
753 exit(1);
754 } else if (i->bootindex < bootindex) {
755 continue;
757 QTAILQ_INSERT_BEFORE(i, node, link);
758 return;
760 QTAILQ_INSERT_TAIL(&fw_boot_order, node, link);
764 * This function returns null terminated string that consist of new line
765 * separated device paths.
767 * memory pointed by "size" is assigned total length of the array in bytes
770 char *get_boot_devices_list(uint32_t *size)
772 FWBootEntry *i;
773 uint32_t total = 0;
774 char *list = NULL;
776 QTAILQ_FOREACH(i, &fw_boot_order, link) {
777 char *devpath = NULL, *bootpath;
778 int len;
780 if (i->dev) {
781 devpath = qdev_get_fw_dev_path(i->dev);
782 assert(devpath);
785 if (i->suffix && devpath) {
786 size_t bootpathlen = strlen(devpath) + strlen(i->suffix) + 1;
788 bootpath = qemu_malloc(bootpathlen);
789 snprintf(bootpath, bootpathlen, "%s%s", devpath, i->suffix);
790 qemu_free(devpath);
791 } else if (devpath) {
792 bootpath = devpath;
793 } else {
794 bootpath = qemu_strdup(i->suffix);
795 assert(bootpath);
798 if (total) {
799 list[total-1] = '\n';
801 len = strlen(bootpath) + 1;
802 list = qemu_realloc(list, total + len);
803 memcpy(&list[total], bootpath, len);
804 total += len;
805 qemu_free(bootpath);
808 *size = total;
810 return list;
813 static void numa_add(const char *optarg)
815 char option[128];
816 char *endptr;
817 unsigned long long value, endvalue;
818 int nodenr;
820 optarg = get_opt_name(option, 128, optarg, ',') + 1;
821 if (!strcmp(option, "node")) {
822 if (get_param_value(option, 128, "nodeid", optarg) == 0) {
823 nodenr = nb_numa_nodes;
824 } else {
825 nodenr = strtoull(option, NULL, 10);
828 if (get_param_value(option, 128, "mem", optarg) == 0) {
829 node_mem[nodenr] = 0;
830 } else {
831 int64_t sval;
832 sval = strtosz(option, NULL);
833 if (sval < 0) {
834 fprintf(stderr, "qemu: invalid numa mem size: %s\n", optarg);
835 exit(1);
837 node_mem[nodenr] = sval;
839 if (get_param_value(option, 128, "cpus", optarg) == 0) {
840 node_cpumask[nodenr] = 0;
841 } else {
842 value = strtoull(option, &endptr, 10);
843 if (value >= 64) {
844 value = 63;
845 fprintf(stderr, "only 64 CPUs in NUMA mode supported.\n");
846 } else {
847 if (*endptr == '-') {
848 endvalue = strtoull(endptr+1, &endptr, 10);
849 if (endvalue >= 63) {
850 endvalue = 62;
851 fprintf(stderr,
852 "only 63 CPUs in NUMA mode supported.\n");
854 value = (2ULL << endvalue) - (1ULL << value);
855 } else {
856 value = 1ULL << value;
859 node_cpumask[nodenr] = value;
861 nb_numa_nodes++;
863 return;
866 static void smp_parse(const char *optarg)
868 int smp, sockets = 0, threads = 0, cores = 0;
869 char *endptr;
870 char option[128];
872 smp = strtoul(optarg, &endptr, 10);
873 if (endptr != optarg) {
874 if (*endptr == ',') {
875 endptr++;
878 if (get_param_value(option, 128, "sockets", endptr) != 0)
879 sockets = strtoull(option, NULL, 10);
880 if (get_param_value(option, 128, "cores", endptr) != 0)
881 cores = strtoull(option, NULL, 10);
882 if (get_param_value(option, 128, "threads", endptr) != 0)
883 threads = strtoull(option, NULL, 10);
884 if (get_param_value(option, 128, "maxcpus", endptr) != 0)
885 max_cpus = strtoull(option, NULL, 10);
887 /* compute missing values, prefer sockets over cores over threads */
888 if (smp == 0 || sockets == 0) {
889 sockets = sockets > 0 ? sockets : 1;
890 cores = cores > 0 ? cores : 1;
891 threads = threads > 0 ? threads : 1;
892 if (smp == 0) {
893 smp = cores * threads * sockets;
895 } else {
896 if (cores == 0) {
897 threads = threads > 0 ? threads : 1;
898 cores = smp / (sockets * threads);
899 } else {
900 threads = smp / (cores * sockets);
903 smp_cpus = smp;
904 smp_cores = cores > 0 ? cores : 1;
905 smp_threads = threads > 0 ? threads : 1;
906 if (max_cpus == 0)
907 max_cpus = smp_cpus;
910 /***********************************************************/
911 /* USB devices */
913 static int usb_device_add(const char *devname)
915 const char *p;
916 USBDevice *dev = NULL;
918 if (!usb_enabled)
919 return -1;
921 /* drivers with .usbdevice_name entry in USBDeviceInfo */
922 dev = usbdevice_create(devname);
923 if (dev)
924 goto done;
926 /* the other ones */
927 if (strstart(devname, "host:", &p)) {
928 dev = usb_host_device_open(p);
929 } else if (!strcmp(devname, "bt") || strstart(devname, "bt:", &p)) {
930 dev = usb_bt_init(devname[2] ? hci_init(p) :
931 bt_new_hci(qemu_find_bt_vlan(0)));
932 } else {
933 return -1;
935 if (!dev)
936 return -1;
938 done:
939 return 0;
942 static int usb_device_del(const char *devname)
944 int bus_num, addr;
945 const char *p;
947 if (strstart(devname, "host:", &p))
948 return usb_host_device_close(p);
950 if (!usb_enabled)
951 return -1;
953 p = strchr(devname, '.');
954 if (!p)
955 return -1;
956 bus_num = strtoul(devname, NULL, 0);
957 addr = strtoul(p + 1, NULL, 0);
959 return usb_device_delete_addr(bus_num, addr);
962 static int usb_parse(const char *cmdline)
964 int r;
965 r = usb_device_add(cmdline);
966 if (r < 0) {
967 fprintf(stderr, "qemu: could not add USB device '%s'\n", cmdline);
969 return r;
972 void do_usb_add(Monitor *mon, const QDict *qdict)
974 const char *devname = qdict_get_str(qdict, "devname");
975 if (usb_device_add(devname) < 0) {
976 error_report("could not add USB device '%s'", devname);
980 void do_usb_del(Monitor *mon, const QDict *qdict)
982 const char *devname = qdict_get_str(qdict, "devname");
983 if (usb_device_del(devname) < 0) {
984 error_report("could not delete USB device '%s'", devname);
988 /***********************************************************/
989 /* PCMCIA/Cardbus */
991 static struct pcmcia_socket_entry_s {
992 PCMCIASocket *socket;
993 struct pcmcia_socket_entry_s *next;
994 } *pcmcia_sockets = 0;
996 void pcmcia_socket_register(PCMCIASocket *socket)
998 struct pcmcia_socket_entry_s *entry;
1000 entry = qemu_malloc(sizeof(struct pcmcia_socket_entry_s));
1001 entry->socket = socket;
1002 entry->next = pcmcia_sockets;
1003 pcmcia_sockets = entry;
1006 void pcmcia_socket_unregister(PCMCIASocket *socket)
1008 struct pcmcia_socket_entry_s *entry, **ptr;
1010 ptr = &pcmcia_sockets;
1011 for (entry = *ptr; entry; ptr = &entry->next, entry = *ptr)
1012 if (entry->socket == socket) {
1013 *ptr = entry->next;
1014 qemu_free(entry);
1018 void pcmcia_info(Monitor *mon)
1020 struct pcmcia_socket_entry_s *iter;
1022 if (!pcmcia_sockets)
1023 monitor_printf(mon, "No PCMCIA sockets\n");
1025 for (iter = pcmcia_sockets; iter; iter = iter->next)
1026 monitor_printf(mon, "%s: %s\n", iter->socket->slot_string,
1027 iter->socket->attached ? iter->socket->card_string :
1028 "Empty");
1031 /***********************************************************/
1032 /* machine registration */
1034 static QEMUMachine *first_machine = NULL;
1035 QEMUMachine *current_machine = NULL;
1037 int qemu_register_machine(QEMUMachine *m)
1039 QEMUMachine **pm;
1040 pm = &first_machine;
1041 while (*pm != NULL)
1042 pm = &(*pm)->next;
1043 m->next = NULL;
1044 *pm = m;
1045 return 0;
1048 static QEMUMachine *find_machine(const char *name)
1050 QEMUMachine *m;
1052 for(m = first_machine; m != NULL; m = m->next) {
1053 if (!strcmp(m->name, name))
1054 return m;
1055 if (m->alias && !strcmp(m->alias, name))
1056 return m;
1058 return NULL;
1061 static QEMUMachine *find_default_machine(void)
1063 QEMUMachine *m;
1065 for(m = first_machine; m != NULL; m = m->next) {
1066 if (m->is_default) {
1067 return m;
1070 return NULL;
1073 /***********************************************************/
1074 /* main execution loop */
1076 static void gui_update(void *opaque)
1078 uint64_t interval = GUI_REFRESH_INTERVAL;
1079 DisplayState *ds = opaque;
1080 DisplayChangeListener *dcl = ds->listeners;
1082 qemu_flush_coalesced_mmio_buffer();
1083 dpy_refresh(ds);
1085 while (dcl != NULL) {
1086 if (dcl->gui_timer_interval &&
1087 dcl->gui_timer_interval < interval)
1088 interval = dcl->gui_timer_interval;
1089 dcl = dcl->next;
1091 qemu_mod_timer(ds->gui_timer, interval + qemu_get_clock_ms(rt_clock));
1094 static void nographic_update(void *opaque)
1096 uint64_t interval = GUI_REFRESH_INTERVAL;
1098 qemu_flush_coalesced_mmio_buffer();
1099 qemu_mod_timer(nographic_timer, interval + qemu_get_clock_ms(rt_clock));
1102 struct vm_change_state_entry {
1103 VMChangeStateHandler *cb;
1104 void *opaque;
1105 QLIST_ENTRY (vm_change_state_entry) entries;
1108 static QLIST_HEAD(vm_change_state_head, vm_change_state_entry) vm_change_state_head;
1110 VMChangeStateEntry *qemu_add_vm_change_state_handler(VMChangeStateHandler *cb,
1111 void *opaque)
1113 VMChangeStateEntry *e;
1115 e = qemu_mallocz(sizeof (*e));
1117 e->cb = cb;
1118 e->opaque = opaque;
1119 QLIST_INSERT_HEAD(&vm_change_state_head, e, entries);
1120 return e;
1123 void qemu_del_vm_change_state_handler(VMChangeStateEntry *e)
1125 QLIST_REMOVE (e, entries);
1126 qemu_free (e);
1129 void vm_state_notify(int running, int reason)
1131 VMChangeStateEntry *e;
1133 trace_vm_state_notify(running, reason);
1135 for (e = vm_change_state_head.lh_first; e; e = e->entries.le_next) {
1136 e->cb(e->opaque, running, reason);
1140 void vm_start(void)
1142 if (!vm_running) {
1143 cpu_enable_ticks();
1144 vm_running = 1;
1145 vm_state_notify(1, 0);
1146 resume_all_vcpus();
1147 monitor_protocol_event(QEVENT_RESUME, NULL);
1151 /* reset/shutdown handler */
1153 typedef struct QEMUResetEntry {
1154 QTAILQ_ENTRY(QEMUResetEntry) entry;
1155 QEMUResetHandler *func;
1156 void *opaque;
1157 } QEMUResetEntry;
1159 static QTAILQ_HEAD(reset_handlers, QEMUResetEntry) reset_handlers =
1160 QTAILQ_HEAD_INITIALIZER(reset_handlers);
1161 static int reset_requested;
1162 static int shutdown_requested, shutdown_signal = -1;
1163 static pid_t shutdown_pid;
1164 static int powerdown_requested;
1165 static int debug_requested;
1166 static int vmstop_requested;
1168 int qemu_no_shutdown(void)
1170 int r = no_shutdown;
1171 no_shutdown = 0;
1172 return r;
1175 int qemu_shutdown_requested_get(void)
1177 return shutdown_requested;
1180 int qemu_reset_requested_get(void)
1182 return reset_requested;
1185 int qemu_shutdown_requested(void)
1187 int r = shutdown_requested;
1188 shutdown_requested = 0;
1189 return r;
1192 void qemu_kill_report(void)
1194 if (shutdown_signal != -1) {
1195 fprintf(stderr, "qemu: terminating on signal %d", shutdown_signal);
1196 if (shutdown_pid == 0) {
1197 /* This happens for eg ^C at the terminal, so it's worth
1198 * avoiding printing an odd message in that case.
1200 fputc('\n', stderr);
1201 } else {
1202 fprintf(stderr, " from pid %d\n", shutdown_pid);
1204 shutdown_signal = -1;
1208 int qemu_reset_requested(void)
1210 int r = reset_requested;
1211 reset_requested = 0;
1212 return r;
1215 int qemu_powerdown_requested(void)
1217 int r = powerdown_requested;
1218 powerdown_requested = 0;
1219 return r;
1222 static int qemu_debug_requested(void)
1224 int r = debug_requested;
1225 debug_requested = 0;
1226 return r;
1229 static int qemu_vmstop_requested(void)
1231 int r = vmstop_requested;
1232 vmstop_requested = 0;
1233 return r;
1236 void qemu_register_reset(QEMUResetHandler *func, void *opaque)
1238 QEMUResetEntry *re = qemu_mallocz(sizeof(QEMUResetEntry));
1240 re->func = func;
1241 re->opaque = opaque;
1242 QTAILQ_INSERT_TAIL(&reset_handlers, re, entry);
1245 void qemu_unregister_reset(QEMUResetHandler *func, void *opaque)
1247 QEMUResetEntry *re;
1249 QTAILQ_FOREACH(re, &reset_handlers, entry) {
1250 if (re->func == func && re->opaque == opaque) {
1251 QTAILQ_REMOVE(&reset_handlers, re, entry);
1252 qemu_free(re);
1253 return;
1258 void qemu_system_reset(void)
1260 QEMUResetEntry *re, *nre;
1262 /* reset all devices */
1263 QTAILQ_FOREACH_SAFE(re, &reset_handlers, entry, nre) {
1264 re->func(re->opaque);
1266 monitor_protocol_event(QEVENT_RESET, NULL);
1267 cpu_synchronize_all_post_reset();
1270 void qemu_system_reset_request(void)
1272 if (no_reboot) {
1273 shutdown_requested = 1;
1274 } else {
1275 reset_requested = 1;
1277 if (cpu_single_env) {
1278 cpu_single_env->stopped = 1;
1279 cpu_exit(cpu_single_env);
1281 cpu_stop_current();
1282 qemu_notify_event();
1285 void qemu_system_killed(int signal, pid_t pid)
1287 shutdown_signal = signal;
1288 shutdown_pid = pid;
1289 qemu_system_shutdown_request();
1292 void qemu_system_shutdown_request(void)
1294 shutdown_requested = 1;
1295 qemu_notify_event();
1298 void qemu_system_powerdown_request(void)
1300 powerdown_requested = 1;
1301 qemu_notify_event();
1304 void qemu_system_debug_request(void)
1306 debug_requested = 1;
1307 qemu_notify_event();
1310 void qemu_system_vmstop_request(int reason)
1312 vmstop_requested = reason;
1313 qemu_notify_event();
1316 void main_loop_wait(int nonblocking)
1318 fd_set rfds, wfds, xfds;
1319 int ret, nfds;
1320 struct timeval tv;
1321 int timeout;
1323 if (nonblocking)
1324 timeout = 0;
1325 else {
1326 timeout = qemu_calculate_timeout();
1327 qemu_bh_update_timeout(&timeout);
1330 os_host_main_loop_wait(&timeout);
1332 tv.tv_sec = timeout / 1000;
1333 tv.tv_usec = (timeout % 1000) * 1000;
1335 /* poll any events */
1336 /* XXX: separate device handlers from system ones */
1337 nfds = -1;
1338 FD_ZERO(&rfds);
1339 FD_ZERO(&wfds);
1340 FD_ZERO(&xfds);
1341 qemu_iohandler_fill(&nfds, &rfds, &wfds, &xfds);
1342 slirp_select_fill(&nfds, &rfds, &wfds, &xfds);
1344 qemu_mutex_unlock_iothread();
1345 ret = select(nfds + 1, &rfds, &wfds, &xfds, &tv);
1346 qemu_mutex_lock_iothread();
1348 qemu_iohandler_poll(&rfds, &wfds, &xfds, ret);
1349 slirp_select_poll(&rfds, &wfds, &xfds, (ret < 0));
1351 qemu_run_all_timers();
1353 /* Check bottom-halves last in case any of the earlier events triggered
1354 them. */
1355 qemu_bh_poll();
1359 #ifndef CONFIG_IOTHREAD
1360 static int vm_request_pending(void)
1362 return powerdown_requested ||
1363 reset_requested ||
1364 shutdown_requested ||
1365 debug_requested ||
1366 vmstop_requested;
1368 #endif
1370 qemu_irq qemu_system_powerdown;
1372 static void main_loop(void)
1374 bool nonblocking = false;
1375 #ifdef CONFIG_PROFILER
1376 int64_t ti;
1377 #endif
1378 int r;
1380 if (kvm_enabled()) {
1381 kvm_main_loop();
1382 cpu_disable_ticks();
1383 return;
1386 qemu_main_loop_start();
1388 for (;;) {
1389 #ifndef CONFIG_IOTHREAD
1390 nonblocking = cpu_exec_all();
1391 if (vm_request_pending()) {
1392 nonblocking = true;
1394 #endif
1395 #ifdef CONFIG_PROFILER
1396 ti = profile_getclock();
1397 #endif
1398 main_loop_wait(nonblocking);
1399 #ifdef CONFIG_PROFILER
1400 dev_time += profile_getclock() - ti;
1401 #endif
1403 if (qemu_debug_requested()) {
1404 vm_stop(VMSTOP_DEBUG);
1406 if (qemu_shutdown_requested()) {
1407 qemu_kill_report();
1408 monitor_protocol_event(QEVENT_SHUTDOWN, NULL);
1409 if (no_shutdown) {
1410 vm_stop(VMSTOP_SHUTDOWN);
1411 no_shutdown = 0;
1412 } else
1413 break;
1415 if (qemu_reset_requested()) {
1416 pause_all_vcpus();
1417 cpu_synchronize_all_states();
1418 qemu_system_reset();
1419 resume_all_vcpus();
1421 if (qemu_powerdown_requested()) {
1422 monitor_protocol_event(QEVENT_POWERDOWN, NULL);
1423 qemu_irq_raise(qemu_system_powerdown);
1425 if ((r = qemu_vmstop_requested())) {
1426 vm_stop(r);
1429 bdrv_close_all();
1430 pause_all_vcpus();
1433 static void version(void)
1435 printf("QEMU emulator version " QEMU_VERSION QEMU_PKGVERSION ", Copyright (c) 2003-2008 Fabrice Bellard\n");
1438 static void help(int exitcode)
1440 const char *options_help =
1441 #define DEF(option, opt_arg, opt_enum, opt_help, arch_mask) \
1442 opt_help
1443 #define DEFHEADING(text) stringify(text) "\n"
1444 #include "qemu-options.def"
1445 #undef DEF
1446 #undef DEFHEADING
1447 #undef GEN_DOCS
1449 version();
1450 printf("usage: %s [options] [disk_image]\n"
1451 "\n"
1452 "'disk_image' is a raw hard disk image for IDE hard disk 0\n"
1453 "\n"
1454 "%s\n"
1455 "During emulation, the following keys are useful:\n"
1456 "ctrl-alt-f toggle full screen\n"
1457 "ctrl-alt-n switch to virtual console 'n'\n"
1458 "ctrl-alt toggle mouse and keyboard grab\n"
1459 "\n"
1460 "When using -nographic, press 'ctrl-a h' to get some help.\n",
1461 "qemu",
1462 options_help);
1463 exit(exitcode);
1466 #define HAS_ARG 0x0001
1468 typedef struct QEMUOption {
1469 const char *name;
1470 int flags;
1471 int index;
1472 uint32_t arch_mask;
1473 } QEMUOption;
1475 static const QEMUOption qemu_options[] = {
1476 { "h", 0, QEMU_OPTION_h, QEMU_ARCH_ALL },
1477 #define DEF(option, opt_arg, opt_enum, opt_help, arch_mask) \
1478 { option, opt_arg, opt_enum, arch_mask },
1479 #define DEFHEADING(text)
1480 #include "qemu-options.def"
1481 #undef DEF
1482 #undef DEFHEADING
1483 #undef GEN_DOCS
1484 { NULL },
1486 static void select_vgahw (const char *p)
1488 const char *opts;
1490 default_vga = 0;
1491 vga_interface_type = VGA_NONE;
1492 if (strstart(p, "std", &opts)) {
1493 vga_interface_type = VGA_STD;
1494 } else if (strstart(p, "cirrus", &opts)) {
1495 vga_interface_type = VGA_CIRRUS;
1496 } else if (strstart(p, "vmware", &opts)) {
1497 vga_interface_type = VGA_VMWARE;
1498 } else if (strstart(p, "xenfb", &opts)) {
1499 vga_interface_type = VGA_XENFB;
1500 } else if (strstart(p, "qxl", &opts)) {
1501 vga_interface_type = VGA_QXL;
1502 } else if (!strstart(p, "none", &opts)) {
1503 invalid_vga:
1504 fprintf(stderr, "Unknown vga type: %s\n", p);
1505 exit(1);
1507 while (*opts) {
1508 const char *nextopt;
1510 if (strstart(opts, ",retrace=", &nextopt)) {
1511 opts = nextopt;
1512 if (strstart(opts, "dumb", &nextopt))
1513 vga_retrace_method = VGA_RETRACE_DUMB;
1514 else if (strstart(opts, "precise", &nextopt))
1515 vga_retrace_method = VGA_RETRACE_PRECISE;
1516 else goto invalid_vga;
1517 } else goto invalid_vga;
1518 opts = nextopt;
1522 static DisplayType select_display(const char *p)
1524 const char *opts;
1525 DisplayType display = DT_DEFAULT;
1527 if (strstart(p, "sdl", &opts)) {
1528 #ifdef CONFIG_SDL
1529 display = DT_SDL;
1530 while (*opts) {
1531 const char *nextopt;
1533 if (strstart(opts, ",frame=", &nextopt)) {
1534 opts = nextopt;
1535 if (strstart(opts, "on", &nextopt)) {
1536 no_frame = 0;
1537 } else if (strstart(opts, "off", &nextopt)) {
1538 no_frame = 1;
1539 } else {
1540 goto invalid_sdl_args;
1542 } else if (strstart(opts, ",alt_grab=", &nextopt)) {
1543 opts = nextopt;
1544 if (strstart(opts, "on", &nextopt)) {
1545 alt_grab = 1;
1546 } else if (strstart(opts, "off", &nextopt)) {
1547 alt_grab = 0;
1548 } else {
1549 goto invalid_sdl_args;
1551 } else if (strstart(opts, ",ctrl_grab=", &nextopt)) {
1552 opts = nextopt;
1553 if (strstart(opts, "on", &nextopt)) {
1554 ctrl_grab = 1;
1555 } else if (strstart(opts, "off", &nextopt)) {
1556 ctrl_grab = 0;
1557 } else {
1558 goto invalid_sdl_args;
1560 } else if (strstart(opts, ",window_close=", &nextopt)) {
1561 opts = nextopt;
1562 if (strstart(opts, "on", &nextopt)) {
1563 no_quit = 0;
1564 } else if (strstart(opts, "off", &nextopt)) {
1565 no_quit = 1;
1566 } else {
1567 goto invalid_sdl_args;
1569 } else {
1570 invalid_sdl_args:
1571 fprintf(stderr, "Invalid SDL option string: %s\n", p);
1572 exit(1);
1574 opts = nextopt;
1576 #else
1577 fprintf(stderr, "SDL support is disabled\n");
1578 exit(1);
1579 #endif
1580 } else if (strstart(p, "vnc", &opts)) {
1581 #ifdef CONFIG_VNC
1582 display_remote++;
1584 if (*opts) {
1585 const char *nextopt;
1587 if (strstart(opts, "=", &nextopt)) {
1588 vnc_display = nextopt;
1591 if (!vnc_display) {
1592 fprintf(stderr, "VNC requires a display argument vnc=<display>\n");
1593 exit(1);
1595 #else
1596 fprintf(stderr, "VNC support is disabled\n");
1597 exit(1);
1598 #endif
1599 } else if (strstart(p, "curses", &opts)) {
1600 #ifdef CONFIG_CURSES
1601 display = DT_CURSES;
1602 #else
1603 fprintf(stderr, "Curses support is disabled\n");
1604 exit(1);
1605 #endif
1606 } else if (strstart(p, "none", &opts)) {
1607 display = DT_NONE;
1608 } else {
1609 fprintf(stderr, "Unknown display type: %s\n", p);
1610 exit(1);
1613 return display;
1616 static int balloon_parse(const char *arg)
1618 QemuOpts *opts;
1620 if (strcmp(arg, "none") == 0) {
1621 return 0;
1624 if (!strncmp(arg, "virtio", 6)) {
1625 if (arg[6] == ',') {
1626 /* have params -> parse them */
1627 opts = qemu_opts_parse(qemu_find_opts("device"), arg+7, 0);
1628 if (!opts)
1629 return -1;
1630 } else {
1631 /* create empty opts */
1632 opts = qemu_opts_create(qemu_find_opts("device"), NULL, 0);
1634 qemu_opt_set(opts, "driver", "virtio-balloon");
1635 return 0;
1638 return -1;
1641 char *qemu_find_file(int type, const char *name)
1643 int len;
1644 const char *subdir;
1645 char *buf;
1647 /* If name contains path separators then try it as a straight path. */
1648 if ((strchr(name, '/') || strchr(name, '\\'))
1649 && access(name, R_OK) == 0) {
1650 return qemu_strdup(name);
1652 switch (type) {
1653 case QEMU_FILE_TYPE_BIOS:
1654 subdir = "";
1655 break;
1656 case QEMU_FILE_TYPE_KEYMAP:
1657 subdir = "keymaps/";
1658 break;
1659 default:
1660 abort();
1662 len = strlen(data_dir) + strlen(name) + strlen(subdir) + 2;
1663 buf = qemu_mallocz(len);
1664 snprintf(buf, len, "%s/%s%s", data_dir, subdir, name);
1665 if (access(buf, R_OK)) {
1666 qemu_free(buf);
1667 return NULL;
1669 return buf;
1672 static int device_help_func(QemuOpts *opts, void *opaque)
1674 return qdev_device_help(opts);
1677 static int device_init_func(QemuOpts *opts, void *opaque)
1679 DeviceState *dev;
1681 dev = qdev_device_add(opts);
1682 if (!dev)
1683 return -1;
1684 return 0;
1687 static int chardev_init_func(QemuOpts *opts, void *opaque)
1689 CharDriverState *chr;
1691 chr = qemu_chr_open_opts(opts, NULL);
1692 if (!chr)
1693 return -1;
1694 return 0;
1697 #ifdef CONFIG_VIRTFS
1698 static int fsdev_init_func(QemuOpts *opts, void *opaque)
1700 int ret;
1701 ret = qemu_fsdev_add(opts);
1703 return ret;
1705 #endif
1707 static int mon_init_func(QemuOpts *opts, void *opaque)
1709 CharDriverState *chr;
1710 const char *chardev;
1711 const char *mode;
1712 int flags;
1714 mode = qemu_opt_get(opts, "mode");
1715 if (mode == NULL) {
1716 mode = "readline";
1718 if (strcmp(mode, "readline") == 0) {
1719 flags = MONITOR_USE_READLINE;
1720 } else if (strcmp(mode, "control") == 0) {
1721 flags = MONITOR_USE_CONTROL;
1722 } else {
1723 fprintf(stderr, "unknown monitor mode \"%s\"\n", mode);
1724 exit(1);
1727 if (qemu_opt_get_bool(opts, "pretty", 0))
1728 flags |= MONITOR_USE_PRETTY;
1730 if (qemu_opt_get_bool(opts, "default", 0))
1731 flags |= MONITOR_IS_DEFAULT;
1733 chardev = qemu_opt_get(opts, "chardev");
1734 chr = qemu_chr_find(chardev);
1735 if (chr == NULL) {
1736 fprintf(stderr, "chardev \"%s\" not found\n", chardev);
1737 exit(1);
1740 monitor_init(chr, flags);
1741 return 0;
1744 static void monitor_parse(const char *optarg, const char *mode)
1746 static int monitor_device_index = 0;
1747 QemuOpts *opts;
1748 const char *p;
1749 char label[32];
1750 int def = 0;
1752 if (strstart(optarg, "chardev:", &p)) {
1753 snprintf(label, sizeof(label), "%s", p);
1754 } else {
1755 snprintf(label, sizeof(label), "compat_monitor%d",
1756 monitor_device_index);
1757 if (monitor_device_index == 0) {
1758 def = 1;
1760 opts = qemu_chr_parse_compat(label, optarg);
1761 if (!opts) {
1762 fprintf(stderr, "parse error: %s\n", optarg);
1763 exit(1);
1767 opts = qemu_opts_create(qemu_find_opts("mon"), label, 1);
1768 if (!opts) {
1769 fprintf(stderr, "duplicate chardev: %s\n", label);
1770 exit(1);
1772 qemu_opt_set(opts, "mode", mode);
1773 qemu_opt_set(opts, "chardev", label);
1774 if (def)
1775 qemu_opt_set(opts, "default", "on");
1776 monitor_device_index++;
1779 struct device_config {
1780 enum {
1781 DEV_USB, /* -usbdevice */
1782 DEV_BT, /* -bt */
1783 DEV_SERIAL, /* -serial */
1784 DEV_PARALLEL, /* -parallel */
1785 DEV_VIRTCON, /* -virtioconsole */
1786 DEV_DEBUGCON, /* -debugcon */
1787 } type;
1788 const char *cmdline;
1789 QTAILQ_ENTRY(device_config) next;
1791 QTAILQ_HEAD(, device_config) device_configs = QTAILQ_HEAD_INITIALIZER(device_configs);
1793 static void add_device_config(int type, const char *cmdline)
1795 struct device_config *conf;
1797 conf = qemu_mallocz(sizeof(*conf));
1798 conf->type = type;
1799 conf->cmdline = cmdline;
1800 QTAILQ_INSERT_TAIL(&device_configs, conf, next);
1803 static int foreach_device_config(int type, int (*func)(const char *cmdline))
1805 struct device_config *conf;
1806 int rc;
1808 QTAILQ_FOREACH(conf, &device_configs, next) {
1809 if (conf->type != type)
1810 continue;
1811 rc = func(conf->cmdline);
1812 if (0 != rc)
1813 return rc;
1815 return 0;
1818 static int serial_parse(const char *devname)
1820 static int index = 0;
1821 char label[32];
1823 if (strcmp(devname, "none") == 0)
1824 return 0;
1825 if (index == MAX_SERIAL_PORTS) {
1826 fprintf(stderr, "qemu: too many serial ports\n");
1827 exit(1);
1829 snprintf(label, sizeof(label), "serial%d", index);
1830 serial_hds[index] = qemu_chr_open(label, devname, NULL);
1831 if (!serial_hds[index]) {
1832 fprintf(stderr, "qemu: could not open serial device '%s': %s\n",
1833 devname, strerror(errno));
1834 return -1;
1836 index++;
1837 return 0;
1840 static int parallel_parse(const char *devname)
1842 static int index = 0;
1843 char label[32];
1845 if (strcmp(devname, "none") == 0)
1846 return 0;
1847 if (index == MAX_PARALLEL_PORTS) {
1848 fprintf(stderr, "qemu: too many parallel ports\n");
1849 exit(1);
1851 snprintf(label, sizeof(label), "parallel%d", index);
1852 parallel_hds[index] = qemu_chr_open(label, devname, NULL);
1853 if (!parallel_hds[index]) {
1854 fprintf(stderr, "qemu: could not open parallel device '%s': %s\n",
1855 devname, strerror(errno));
1856 return -1;
1858 index++;
1859 return 0;
1862 static int virtcon_parse(const char *devname)
1864 QemuOptsList *device = qemu_find_opts("device");
1865 static int index = 0;
1866 char label[32];
1867 QemuOpts *bus_opts, *dev_opts;
1869 if (strcmp(devname, "none") == 0)
1870 return 0;
1871 if (index == MAX_VIRTIO_CONSOLES) {
1872 fprintf(stderr, "qemu: too many virtio consoles\n");
1873 exit(1);
1876 bus_opts = qemu_opts_create(device, NULL, 0);
1877 qemu_opt_set(bus_opts, "driver", "virtio-serial");
1879 dev_opts = qemu_opts_create(device, NULL, 0);
1880 qemu_opt_set(dev_opts, "driver", "virtconsole");
1882 snprintf(label, sizeof(label), "virtcon%d", index);
1883 virtcon_hds[index] = qemu_chr_open(label, devname, NULL);
1884 if (!virtcon_hds[index]) {
1885 fprintf(stderr, "qemu: could not open virtio console '%s': %s\n",
1886 devname, strerror(errno));
1887 return -1;
1889 qemu_opt_set(dev_opts, "chardev", label);
1891 index++;
1892 return 0;
1895 static int debugcon_parse(const char *devname)
1897 QemuOpts *opts;
1899 if (!qemu_chr_open("debugcon", devname, NULL)) {
1900 exit(1);
1902 opts = qemu_opts_create(qemu_find_opts("device"), "debugcon", 1);
1903 if (!opts) {
1904 fprintf(stderr, "qemu: already have a debugcon device\n");
1905 exit(1);
1907 qemu_opt_set(opts, "driver", "isa-debugcon");
1908 qemu_opt_set(opts, "chardev", "debugcon");
1909 return 0;
1912 static int tcg_init(void)
1914 return 0;
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 olist = qemu_find_opts("machine");
2704 qemu_opts_reset(olist);
2705 qemu_opts_parse(olist, "accel=tcg", 0);
2706 break;
2707 #ifdef CONFIG_KVM
2708 case QEMU_OPTION_no_kvm_irqchip: {
2709 kvm_irqchip = 0;
2710 kvm_pit = 0;
2711 break;
2713 case QEMU_OPTION_no_kvm_pit: {
2714 kvm_pit = 0;
2715 break;
2717 case QEMU_OPTION_no_kvm_pit_reinjection: {
2718 kvm_pit_reinject = 0;
2719 break;
2721 case QEMU_OPTION_enable_nesting: {
2722 kvm_nested = 1;
2723 break;
2725 #endif
2726 case QEMU_OPTION_usb:
2727 usb_enabled = 1;
2728 break;
2729 case QEMU_OPTION_usbdevice:
2730 usb_enabled = 1;
2731 add_device_config(DEV_USB, optarg);
2732 break;
2733 case QEMU_OPTION_device:
2734 if (!qemu_opts_parse(qemu_find_opts("device"), optarg, 1)) {
2735 exit(1);
2737 break;
2738 case QEMU_OPTION_smp:
2739 smp_parse(optarg);
2740 if (smp_cpus < 1) {
2741 fprintf(stderr, "Invalid number of CPUs\n");
2742 exit(1);
2744 if (max_cpus < smp_cpus) {
2745 fprintf(stderr, "maxcpus must be equal to or greater than "
2746 "smp\n");
2747 exit(1);
2749 if (max_cpus > 255) {
2750 fprintf(stderr, "Unsupported number of maxcpus\n");
2751 exit(1);
2753 break;
2754 case QEMU_OPTION_vnc:
2755 #ifdef CONFIG_VNC
2756 display_remote++;
2757 vnc_display = optarg;
2758 #else
2759 fprintf(stderr, "VNC support is disabled\n");
2760 exit(1);
2761 #endif
2762 break;
2763 case QEMU_OPTION_no_acpi:
2764 acpi_enabled = 0;
2765 break;
2766 case QEMU_OPTION_no_hpet:
2767 no_hpet = 1;
2768 break;
2769 case QEMU_OPTION_balloon:
2770 if (balloon_parse(optarg) < 0) {
2771 fprintf(stderr, "Unknown -balloon argument %s\n", optarg);
2772 exit(1);
2774 break;
2775 case QEMU_OPTION_no_reboot:
2776 no_reboot = 1;
2777 break;
2778 case QEMU_OPTION_no_shutdown:
2779 no_shutdown = 1;
2780 break;
2781 case QEMU_OPTION_show_cursor:
2782 cursor_hide = 0;
2783 break;
2784 case QEMU_OPTION_uuid:
2785 if(qemu_uuid_parse(optarg, qemu_uuid) < 0) {
2786 fprintf(stderr, "Fail to parse UUID string."
2787 " Wrong format.\n");
2788 exit(1);
2790 break;
2791 case QEMU_OPTION_option_rom:
2792 if (nb_option_roms >= MAX_OPTION_ROMS) {
2793 fprintf(stderr, "Too many option ROMs\n");
2794 exit(1);
2796 opts = qemu_opts_parse(qemu_find_opts("option-rom"), optarg, 1);
2797 option_rom[nb_option_roms].name = qemu_opt_get(opts, "romfile");
2798 option_rom[nb_option_roms].bootindex =
2799 qemu_opt_get_number(opts, "bootindex", -1);
2800 if (!option_rom[nb_option_roms].name) {
2801 fprintf(stderr, "Option ROM file is not specified\n");
2802 exit(1);
2804 nb_option_roms++;
2805 break;
2806 case QEMU_OPTION_semihosting:
2807 semihosting_enabled = 1;
2808 break;
2809 case QEMU_OPTION_tdf:
2810 time_drift_fix = 1;
2811 break;
2812 case QEMU_OPTION_kvm_shadow_memory:
2813 kvm_shadow_memory = (int64_t)atoi(optarg) * 1024 * 1024 / 4096;
2814 break;
2815 case QEMU_OPTION_name:
2816 qemu_name = qemu_strdup(optarg);
2818 char *p = strchr(qemu_name, ',');
2819 if (p != NULL) {
2820 *p++ = 0;
2821 if (strncmp(p, "process=", 8)) {
2822 fprintf(stderr, "Unknown subargument %s to -name\n", p);
2823 exit(1);
2825 p += 8;
2826 os_set_proc_name(p);
2829 break;
2830 case QEMU_OPTION_prom_env:
2831 if (nb_prom_envs >= MAX_PROM_ENVS) {
2832 fprintf(stderr, "Too many prom variables\n");
2833 exit(1);
2835 prom_envs[nb_prom_envs] = optarg;
2836 nb_prom_envs++;
2837 break;
2838 case QEMU_OPTION_old_param:
2839 old_param = 1;
2840 break;
2841 case QEMU_OPTION_clock:
2842 configure_alarms(optarg);
2843 break;
2844 case QEMU_OPTION_startdate:
2845 configure_rtc_date_offset(optarg, 1);
2846 break;
2847 case QEMU_OPTION_rtc:
2848 opts = qemu_opts_parse(qemu_find_opts("rtc"), optarg, 0);
2849 if (!opts) {
2850 exit(1);
2852 configure_rtc(opts);
2853 break;
2854 case QEMU_OPTION_tb_size:
2855 tb_size = strtol(optarg, NULL, 0);
2856 if (tb_size < 0)
2857 tb_size = 0;
2858 break;
2859 case QEMU_OPTION_icount:
2860 icount_option = optarg;
2861 break;
2862 case QEMU_OPTION_incoming:
2863 incoming = optarg;
2864 incoming_expected = true;
2865 break;
2866 case QEMU_OPTION_nodefaults:
2867 default_serial = 0;
2868 default_parallel = 0;
2869 default_virtcon = 0;
2870 default_monitor = 0;
2871 default_vga = 0;
2872 default_net = 0;
2873 default_floppy = 0;
2874 default_cdrom = 0;
2875 default_sdcard = 0;
2876 break;
2877 #ifndef _WIN32
2878 case QEMU_OPTION_nvram:
2879 nvram = optarg;
2880 break;
2881 #endif
2882 case QEMU_OPTION_xen_domid:
2883 if (!(xen_available())) {
2884 printf("Option %s not supported for this target\n", popt->name);
2885 exit(1);
2887 xen_domid = atoi(optarg);
2888 break;
2889 case QEMU_OPTION_xen_create:
2890 if (!(xen_available())) {
2891 printf("Option %s not supported for this target\n", popt->name);
2892 exit(1);
2894 xen_mode = XEN_CREATE;
2895 break;
2896 case QEMU_OPTION_xen_attach:
2897 if (!(xen_available())) {
2898 printf("Option %s not supported for this target\n", popt->name);
2899 exit(1);
2901 xen_mode = XEN_ATTACH;
2902 break;
2903 #ifdef CONFIG_SIMPLE_TRACE
2904 case QEMU_OPTION_trace:
2905 opts = qemu_opts_parse(qemu_find_opts("trace"), optarg, 0);
2906 if (opts) {
2907 trace_file = qemu_opt_get(opts, "file");
2909 break;
2910 #endif
2911 case QEMU_OPTION_readconfig:
2913 int ret = qemu_read_config_file(optarg);
2914 if (ret < 0) {
2915 fprintf(stderr, "read config %s: %s\n", optarg,
2916 strerror(-ret));
2917 exit(1);
2919 break;
2921 case QEMU_OPTION_spice:
2922 olist = qemu_find_opts("spice");
2923 if (!olist) {
2924 fprintf(stderr, "spice is not supported by this qemu build.\n");
2925 exit(1);
2927 opts = qemu_opts_parse(olist, optarg, 0);
2928 if (!opts) {
2929 fprintf(stderr, "parse error: %s\n", optarg);
2930 exit(1);
2932 break;
2933 case QEMU_OPTION_writeconfig:
2935 FILE *fp;
2936 if (strcmp(optarg, "-") == 0) {
2937 fp = stdout;
2938 } else {
2939 fp = fopen(optarg, "w");
2940 if (fp == NULL) {
2941 fprintf(stderr, "open %s: %s\n", optarg, strerror(errno));
2942 exit(1);
2945 qemu_config_write(fp);
2946 fclose(fp);
2947 break;
2949 default:
2950 os_parse_cmd_args(popt->index, optarg);
2954 loc_set_none();
2956 if (!st_init(trace_file)) {
2957 fprintf(stderr, "warning: unable to initialize simple trace backend\n");
2960 /* If no data_dir is specified then try to find it relative to the
2961 executable path. */
2962 if (!data_dir) {
2963 data_dir = os_find_datadir(argv[0]);
2965 /* If all else fails use the install patch specified when building. */
2966 if (!data_dir) {
2967 data_dir = CONFIG_QEMU_DATADIR;
2971 * Default to max_cpus = smp_cpus, in case the user doesn't
2972 * specify a max_cpus value.
2974 if (!max_cpus)
2975 max_cpus = smp_cpus;
2977 machine->max_cpus = machine->max_cpus ?: 1; /* Default to UP */
2978 if (smp_cpus > machine->max_cpus) {
2979 fprintf(stderr, "Number of SMP cpus requested (%d), exceeds max cpus "
2980 "supported by machine `%s' (%d)\n", smp_cpus, machine->name,
2981 machine->max_cpus);
2982 exit(1);
2986 * Get the default machine options from the machine if it is not already
2987 * specified either by the configuration file or by the command line.
2989 if (machine->default_machine_opts) {
2990 QemuOptsList *list = qemu_find_opts("machine");
2991 const char *p = NULL;
2993 if (!QTAILQ_EMPTY(&list->head)) {
2994 p = qemu_opt_get(QTAILQ_FIRST(&list->head), "accel");
2996 if (p == NULL) {
2997 opts = qemu_opts_parse(qemu_find_opts("machine"),
2998 machine->default_machine_opts, 0);
2999 if (!opts) {
3000 fprintf(stderr, "parse error for machine %s: %s\n",
3001 machine->name, machine->default_machine_opts);
3002 exit(1);
3007 qemu_opts_foreach(qemu_find_opts("device"), default_driver_check, NULL, 0);
3008 qemu_opts_foreach(qemu_find_opts("global"), default_driver_check, NULL, 0);
3010 if (machine->no_serial) {
3011 default_serial = 0;
3013 if (machine->no_parallel) {
3014 default_parallel = 0;
3016 if (!machine->use_virtcon) {
3017 default_virtcon = 0;
3019 if (machine->no_vga) {
3020 default_vga = 0;
3022 if (machine->no_floppy) {
3023 default_floppy = 0;
3025 if (machine->no_cdrom) {
3026 default_cdrom = 0;
3028 if (machine->no_sdcard) {
3029 default_sdcard = 0;
3032 if (display_type == DT_NOGRAPHIC) {
3033 if (default_parallel)
3034 add_device_config(DEV_PARALLEL, "null");
3035 if (default_serial && default_monitor) {
3036 add_device_config(DEV_SERIAL, "mon:stdio");
3037 } else if (default_virtcon && default_monitor) {
3038 add_device_config(DEV_VIRTCON, "mon:stdio");
3039 } else {
3040 if (default_serial)
3041 add_device_config(DEV_SERIAL, "stdio");
3042 if (default_virtcon)
3043 add_device_config(DEV_VIRTCON, "stdio");
3044 if (default_monitor)
3045 monitor_parse("stdio", "readline");
3047 } else {
3048 if (default_serial)
3049 add_device_config(DEV_SERIAL, "vc:80Cx24C");
3050 if (default_parallel)
3051 add_device_config(DEV_PARALLEL, "vc:80Cx24C");
3052 if (default_monitor)
3053 monitor_parse("vc:80Cx24C", "readline");
3054 if (default_virtcon)
3055 add_device_config(DEV_VIRTCON, "vc:80Cx24C");
3057 if (default_vga)
3058 vga_interface_type = VGA_CIRRUS;
3060 socket_init();
3062 if (qemu_opts_foreach(qemu_find_opts("chardev"), chardev_init_func, NULL, 1) != 0)
3063 exit(1);
3064 #ifdef CONFIG_VIRTFS
3065 if (qemu_opts_foreach(qemu_find_opts("fsdev"), fsdev_init_func, NULL, 1) != 0) {
3066 exit(1);
3068 #endif
3070 os_daemonize();
3072 if (pid_file && qemu_create_pidfile(pid_file) != 0) {
3073 os_pidfile_error();
3074 exit(1);
3077 configure_accelerator();
3079 if (qemu_init_main_loop()) {
3080 fprintf(stderr, "qemu_init_main_loop failed\n");
3081 exit(1);
3083 linux_boot = (kernel_filename != NULL);
3085 if (!linux_boot && *kernel_cmdline != '\0') {
3086 fprintf(stderr, "-append only allowed with -kernel option\n");
3087 exit(1);
3090 if (!linux_boot && initrd_filename != NULL) {
3091 fprintf(stderr, "-initrd only allowed with -kernel option\n");
3092 exit(1);
3095 os_set_line_buffering();
3097 if (init_timer_alarm() < 0) {
3098 fprintf(stderr, "could not initialize alarm timer\n");
3099 exit(1);
3101 configure_icount(icount_option);
3103 if (net_init_clients() < 0) {
3104 exit(1);
3107 /* init the bluetooth world */
3108 if (foreach_device_config(DEV_BT, bt_parse))
3109 exit(1);
3111 /* init the memory */
3112 if (ram_size == 0)
3113 ram_size = DEFAULT_RAM_SIZE * 1024 * 1024;
3115 /* init the dynamic translator */
3116 cpu_exec_init_all(tb_size * 1024 * 1024);
3118 bdrv_init_with_whitelist();
3120 blk_mig_init();
3122 /* open the virtual block devices */
3123 if (snapshot)
3124 qemu_opts_foreach(qemu_find_opts("drive"), drive_enable_snapshot, NULL, 0);
3125 if (qemu_opts_foreach(qemu_find_opts("drive"), drive_init_func, &machine->use_scsi, 1) != 0)
3126 exit(1);
3128 default_drive(default_cdrom, snapshot, machine->use_scsi,
3129 IF_DEFAULT, 2, CDROM_OPTS);
3130 default_drive(default_floppy, snapshot, machine->use_scsi,
3131 IF_FLOPPY, 0, FD_OPTS);
3132 default_drive(default_sdcard, snapshot, machine->use_scsi,
3133 IF_SD, 0, SD_OPTS);
3135 register_savevm_live(NULL, "ram", 0, 4, NULL, ram_save_live, NULL,
3136 ram_load, NULL);
3138 if (nb_numa_nodes > 0) {
3139 int i;
3141 if (nb_numa_nodes > smp_cpus) {
3142 nb_numa_nodes = smp_cpus;
3145 /* If no memory size if given for any node, assume the default case
3146 * and distribute the available memory equally across all nodes
3148 for (i = 0; i < nb_numa_nodes; i++) {
3149 if (node_mem[i] != 0)
3150 break;
3152 if (i == nb_numa_nodes) {
3153 uint64_t usedmem = 0;
3155 /* On Linux, the each node's border has to be 8MB aligned,
3156 * the final node gets the rest.
3158 for (i = 0; i < nb_numa_nodes - 1; i++) {
3159 node_mem[i] = (ram_size / nb_numa_nodes) & ~((1 << 23UL) - 1);
3160 usedmem += node_mem[i];
3162 node_mem[i] = ram_size - usedmem;
3165 for (i = 0; i < nb_numa_nodes; i++) {
3166 if (node_cpumask[i] != 0)
3167 break;
3169 /* assigning the VCPUs round-robin is easier to implement, guest OSes
3170 * must cope with this anyway, because there are BIOSes out there in
3171 * real machines which also use this scheme.
3173 if (i == nb_numa_nodes) {
3174 for (i = 0; i < smp_cpus; i++) {
3175 node_cpumask[i % nb_numa_nodes] |= 1 << i;
3180 if (qemu_opts_foreach(qemu_find_opts("mon"), mon_init_func, NULL, 1) != 0) {
3181 exit(1);
3184 if (foreach_device_config(DEV_SERIAL, serial_parse) < 0)
3185 exit(1);
3186 if (foreach_device_config(DEV_PARALLEL, parallel_parse) < 0)
3187 exit(1);
3188 if (foreach_device_config(DEV_VIRTCON, virtcon_parse) < 0)
3189 exit(1);
3190 if (foreach_device_config(DEV_DEBUGCON, debugcon_parse) < 0)
3191 exit(1);
3193 module_call_init(MODULE_INIT_DEVICE);
3195 if (qemu_opts_foreach(qemu_find_opts("device"), device_help_func, NULL, 0) != 0)
3196 exit(0);
3198 if (watchdog) {
3199 i = select_watchdog(watchdog);
3200 if (i > 0)
3201 exit (i == 1 ? 1 : 0);
3204 if (machine->compat_props) {
3205 qdev_prop_register_global_list(machine->compat_props);
3207 qemu_add_globals();
3209 machine->init(ram_size, boot_devices,
3210 kernel_filename, kernel_cmdline, initrd_filename, cpu_model);
3212 cpu_synchronize_all_post_init();
3214 set_numa_modes();
3216 current_machine = machine;
3218 /* init USB devices */
3219 if (usb_enabled) {
3220 if (foreach_device_config(DEV_USB, usb_parse) < 0)
3221 exit(1);
3224 /* init generic devices */
3225 if (qemu_opts_foreach(qemu_find_opts("device"), device_init_func, NULL, 1) != 0)
3226 exit(1);
3228 net_check_clients();
3230 /* just use the first displaystate for the moment */
3231 ds = get_displaystate();
3233 if (using_spice)
3234 display_remote++;
3235 if (display_type == DT_DEFAULT && !display_remote) {
3236 #if defined(CONFIG_SDL) || defined(CONFIG_COCOA)
3237 display_type = DT_SDL;
3238 #elif defined(CONFIG_VNC)
3239 vnc_display = "localhost:0,to=99";
3240 show_vnc_port = 1;
3241 #else
3242 display_type = DT_NONE;
3243 #endif
3247 /* init local displays */
3248 switch (display_type) {
3249 case DT_NOGRAPHIC:
3250 break;
3251 #if defined(CONFIG_CURSES)
3252 case DT_CURSES:
3253 curses_display_init(ds, full_screen);
3254 break;
3255 #endif
3256 #if defined(CONFIG_SDL)
3257 case DT_SDL:
3258 sdl_display_init(ds, full_screen, no_frame);
3259 break;
3260 #elif defined(CONFIG_COCOA)
3261 case DT_SDL:
3262 cocoa_display_init(ds, full_screen);
3263 break;
3264 #endif
3265 default:
3266 break;
3269 /* must be after terminal init, SDL library changes signal handlers */
3270 os_setup_signal_handling();
3272 #ifdef CONFIG_VNC
3273 /* init remote displays */
3274 if (vnc_display) {
3275 vnc_display_init(ds);
3276 if (vnc_display_open(ds, vnc_display) < 0)
3277 exit(1);
3279 if (show_vnc_port) {
3280 printf("VNC server running on `%s'\n", vnc_display_local_addr(ds));
3283 #endif
3284 #ifdef CONFIG_SPICE
3285 if (using_spice && !qxl_enabled) {
3286 qemu_spice_display_init(ds);
3288 #endif
3290 /* display setup */
3291 dpy_resize(ds);
3292 dcl = ds->listeners;
3293 while (dcl != NULL) {
3294 if (dcl->dpy_refresh != NULL) {
3295 ds->gui_timer = qemu_new_timer_ms(rt_clock, gui_update, ds);
3296 qemu_mod_timer(ds->gui_timer, qemu_get_clock_ms(rt_clock));
3297 break;
3299 dcl = dcl->next;
3301 if (ds->gui_timer == NULL) {
3302 nographic_timer = qemu_new_timer_ms(rt_clock, nographic_update, NULL);
3303 qemu_mod_timer(nographic_timer, qemu_get_clock_ms(rt_clock));
3305 text_consoles_set_display(ds);
3307 if (gdbstub_dev && gdbserver_start(gdbstub_dev) < 0) {
3308 fprintf(stderr, "qemu: could not open gdbserver on device '%s'\n",
3309 gdbstub_dev);
3310 exit(1);
3313 qdev_machine_creation_done();
3315 if (rom_load_all() != 0) {
3316 fprintf(stderr, "rom loading failed\n");
3317 exit(1);
3320 /* TODO: once all bus devices are qdevified, this should be done
3321 * when bus is created by qdev.c */
3322 qemu_register_reset(qbus_reset_all_fn, sysbus_get_default());
3323 qemu_run_machine_init_done_notifiers();
3325 qemu_system_reset();
3326 if (loadvm) {
3327 if (load_vmstate(loadvm) < 0) {
3328 autostart = 0;
3332 if (incoming) {
3333 int ret = qemu_start_incoming_migration(incoming);
3334 if (ret < 0) {
3335 fprintf(stderr, "Migration failed. Exit code %s(%d), exiting.\n",
3336 incoming, ret);
3337 exit(ret);
3339 } else if (autostart) {
3340 vm_start();
3343 os_setup_post();
3345 main_loop();
3346 quit_timers();
3347 net_cleanup();
3349 return 0;