Merge branch 'master' of git://git.qemu.org/qemu into next
[qemu/qemu-dev-zwu.git] / vl.c
blob7708254dbc7ce4a4aeb99717b624fe99c8288a96
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 "kvm.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-cd", .flag = &default_cdrom },
286 { .driver = "ide-hd", .flag = &default_cdrom },
287 { .driver = "ide-drive", .flag = &default_cdrom },
288 { .driver = "scsi-cd", .flag = &default_cdrom },
289 { .driver = "virtio-serial-pci", .flag = &default_virtcon },
290 { .driver = "virtio-serial-s390", .flag = &default_virtcon },
291 { .driver = "virtio-serial", .flag = &default_virtcon },
292 { .driver = "VGA", .flag = &default_vga },
293 { .driver = "cirrus-vga", .flag = &default_vga },
294 { .driver = "vmware-svga", .flag = &default_vga },
295 { .driver = "isa-vga", .flag = &default_vga },
296 { .driver = "qxl-vga", .flag = &default_vga },
299 static int default_driver_check(QemuOpts *opts, void *opaque)
301 const char *driver = qemu_opt_get(opts, "driver");
302 int i;
304 if (!driver)
305 return 0;
306 for (i = 0; i < ARRAY_SIZE(default_list); i++) {
307 if (strcmp(default_list[i].driver, driver) != 0)
308 continue;
309 *(default_list[i].flag) = 0;
311 return 0;
314 /***********************************************************/
315 /* real time host monotonic timer */
317 /***********************************************************/
318 /* host time/date access */
319 void qemu_get_timedate(struct tm *tm, int offset)
321 time_t ti;
322 struct tm *ret;
324 time(&ti);
325 ti += offset;
326 if (rtc_date_offset == -1) {
327 if (rtc_utc)
328 ret = gmtime(&ti);
329 else
330 ret = localtime(&ti);
331 } else {
332 ti -= rtc_date_offset;
333 ret = gmtime(&ti);
336 memcpy(tm, ret, sizeof(struct tm));
339 int qemu_timedate_diff(struct tm *tm)
341 time_t seconds;
343 if (rtc_date_offset == -1)
344 if (rtc_utc)
345 seconds = mktimegm(tm);
346 else
347 seconds = mktime(tm);
348 else
349 seconds = mktimegm(tm) + rtc_date_offset;
351 return seconds - time(NULL);
354 void rtc_change_mon_event(struct tm *tm)
356 QObject *data;
358 data = qobject_from_jsonf("{ 'offset': %d }", qemu_timedate_diff(tm));
359 monitor_protocol_event(QEVENT_RTC_CHANGE, data);
360 qobject_decref(data);
363 static void configure_rtc_date_offset(const char *startdate, int legacy)
365 time_t rtc_start_date;
366 struct tm tm;
368 if (!strcmp(startdate, "now") && legacy) {
369 rtc_date_offset = -1;
370 } else {
371 if (sscanf(startdate, "%d-%d-%dT%d:%d:%d",
372 &tm.tm_year,
373 &tm.tm_mon,
374 &tm.tm_mday,
375 &tm.tm_hour,
376 &tm.tm_min,
377 &tm.tm_sec) == 6) {
378 /* OK */
379 } else if (sscanf(startdate, "%d-%d-%d",
380 &tm.tm_year,
381 &tm.tm_mon,
382 &tm.tm_mday) == 3) {
383 tm.tm_hour = 0;
384 tm.tm_min = 0;
385 tm.tm_sec = 0;
386 } else {
387 goto date_fail;
389 tm.tm_year -= 1900;
390 tm.tm_mon--;
391 rtc_start_date = mktimegm(&tm);
392 if (rtc_start_date == -1) {
393 date_fail:
394 fprintf(stderr, "Invalid date format. Valid formats are:\n"
395 "'2006-06-17T16:01:21' or '2006-06-17'\n");
396 exit(1);
398 rtc_date_offset = time(NULL) - rtc_start_date;
402 static void configure_rtc(QemuOpts *opts)
404 const char *value;
406 value = qemu_opt_get(opts, "base");
407 if (value) {
408 if (!strcmp(value, "utc")) {
409 rtc_utc = 1;
410 } else if (!strcmp(value, "localtime")) {
411 rtc_utc = 0;
412 } else {
413 configure_rtc_date_offset(value, 0);
416 value = qemu_opt_get(opts, "clock");
417 if (value) {
418 if (!strcmp(value, "host")) {
419 rtc_clock = host_clock;
420 } else if (!strcmp(value, "vm")) {
421 rtc_clock = vm_clock;
422 } else {
423 fprintf(stderr, "qemu: invalid option value '%s'\n", value);
424 exit(1);
427 value = qemu_opt_get(opts, "driftfix");
428 if (value) {
429 if (!strcmp(value, "slew")) {
430 rtc_td_hack = 1;
431 } else if (!strcmp(value, "none")) {
432 rtc_td_hack = 0;
433 } else {
434 fprintf(stderr, "qemu: invalid option value '%s'\n", value);
435 exit(1);
440 /***********************************************************/
441 /* Bluetooth support */
442 static int nb_hcis;
443 static int cur_hci;
444 static struct HCIInfo *hci_table[MAX_NICS];
446 static struct bt_vlan_s {
447 struct bt_scatternet_s net;
448 int id;
449 struct bt_vlan_s *next;
450 } *first_bt_vlan;
452 /* find or alloc a new bluetooth "VLAN" */
453 static struct bt_scatternet_s *qemu_find_bt_vlan(int id)
455 struct bt_vlan_s **pvlan, *vlan;
456 for (vlan = first_bt_vlan; vlan != NULL; vlan = vlan->next) {
457 if (vlan->id == id)
458 return &vlan->net;
460 vlan = qemu_mallocz(sizeof(struct bt_vlan_s));
461 vlan->id = id;
462 pvlan = &first_bt_vlan;
463 while (*pvlan != NULL)
464 pvlan = &(*pvlan)->next;
465 *pvlan = vlan;
466 return &vlan->net;
469 static void null_hci_send(struct HCIInfo *hci, const uint8_t *data, int len)
473 static int null_hci_addr_set(struct HCIInfo *hci, const uint8_t *bd_addr)
475 return -ENOTSUP;
478 static struct HCIInfo null_hci = {
479 .cmd_send = null_hci_send,
480 .sco_send = null_hci_send,
481 .acl_send = null_hci_send,
482 .bdaddr_set = null_hci_addr_set,
485 struct HCIInfo *qemu_next_hci(void)
487 if (cur_hci == nb_hcis)
488 return &null_hci;
490 return hci_table[cur_hci++];
493 static struct HCIInfo *hci_init(const char *str)
495 char *endp;
496 struct bt_scatternet_s *vlan = 0;
498 if (!strcmp(str, "null"))
499 /* null */
500 return &null_hci;
501 else if (!strncmp(str, "host", 4) && (str[4] == '\0' || str[4] == ':'))
502 /* host[:hciN] */
503 return bt_host_hci(str[4] ? str + 5 : "hci0");
504 else if (!strncmp(str, "hci", 3)) {
505 /* hci[,vlan=n] */
506 if (str[3]) {
507 if (!strncmp(str + 3, ",vlan=", 6)) {
508 vlan = qemu_find_bt_vlan(strtol(str + 9, &endp, 0));
509 if (*endp)
510 vlan = 0;
512 } else
513 vlan = qemu_find_bt_vlan(0);
514 if (vlan)
515 return bt_new_hci(vlan);
518 fprintf(stderr, "qemu: Unknown bluetooth HCI `%s'.\n", str);
520 return 0;
523 static int bt_hci_parse(const char *str)
525 struct HCIInfo *hci;
526 bdaddr_t bdaddr;
528 if (nb_hcis >= MAX_NICS) {
529 fprintf(stderr, "qemu: Too many bluetooth HCIs (max %i).\n", MAX_NICS);
530 return -1;
533 hci = hci_init(str);
534 if (!hci)
535 return -1;
537 bdaddr.b[0] = 0x52;
538 bdaddr.b[1] = 0x54;
539 bdaddr.b[2] = 0x00;
540 bdaddr.b[3] = 0x12;
541 bdaddr.b[4] = 0x34;
542 bdaddr.b[5] = 0x56 + nb_hcis;
543 hci->bdaddr_set(hci, bdaddr.b);
545 hci_table[nb_hcis++] = hci;
547 return 0;
550 static void bt_vhci_add(int vlan_id)
552 struct bt_scatternet_s *vlan = qemu_find_bt_vlan(vlan_id);
554 if (!vlan->slave)
555 fprintf(stderr, "qemu: warning: adding a VHCI to "
556 "an empty scatternet %i\n", vlan_id);
558 bt_vhci_init(bt_new_hci(vlan));
561 static struct bt_device_s *bt_device_add(const char *opt)
563 struct bt_scatternet_s *vlan;
564 int vlan_id = 0;
565 char *endp = strstr(opt, ",vlan=");
566 int len = (endp ? endp - opt : strlen(opt)) + 1;
567 char devname[10];
569 pstrcpy(devname, MIN(sizeof(devname), len), opt);
571 if (endp) {
572 vlan_id = strtol(endp + 6, &endp, 0);
573 if (*endp) {
574 fprintf(stderr, "qemu: unrecognised bluetooth vlan Id\n");
575 return 0;
579 vlan = qemu_find_bt_vlan(vlan_id);
581 if (!vlan->slave)
582 fprintf(stderr, "qemu: warning: adding a slave device to "
583 "an empty scatternet %i\n", vlan_id);
585 if (!strcmp(devname, "keyboard"))
586 return bt_keyboard_init(vlan);
588 fprintf(stderr, "qemu: unsupported bluetooth device `%s'\n", devname);
589 return 0;
592 static int bt_parse(const char *opt)
594 const char *endp, *p;
595 int vlan;
597 if (strstart(opt, "hci", &endp)) {
598 if (!*endp || *endp == ',') {
599 if (*endp)
600 if (!strstart(endp, ",vlan=", 0))
601 opt = endp + 1;
603 return bt_hci_parse(opt);
605 } else if (strstart(opt, "vhci", &endp)) {
606 if (!*endp || *endp == ',') {
607 if (*endp) {
608 if (strstart(endp, ",vlan=", &p)) {
609 vlan = strtol(p, (char **) &endp, 0);
610 if (*endp) {
611 fprintf(stderr, "qemu: bad scatternet '%s'\n", p);
612 return 1;
614 } else {
615 fprintf(stderr, "qemu: bad parameter '%s'\n", endp + 1);
616 return 1;
618 } else
619 vlan = 0;
621 bt_vhci_add(vlan);
622 return 0;
624 } else if (strstart(opt, "device:", &endp))
625 return !bt_device_add(endp);
627 fprintf(stderr, "qemu: bad bluetooth parameter '%s'\n", opt);
628 return 1;
631 /***********************************************************/
632 /* QEMU Block devices */
634 #define HD_OPTS "media=disk"
635 #define CDROM_OPTS "media=cdrom"
636 #define FD_OPTS ""
637 #define PFLASH_OPTS ""
638 #define MTD_OPTS ""
639 #define SD_OPTS ""
641 static int drive_init_func(QemuOpts *opts, void *opaque)
643 int *use_scsi = opaque;
645 return drive_init(opts, *use_scsi) == NULL;
648 static int drive_enable_snapshot(QemuOpts *opts, void *opaque)
650 if (NULL == qemu_opt_get(opts, "snapshot")) {
651 qemu_opt_set(opts, "snapshot", "on");
653 return 0;
656 static void default_drive(int enable, int snapshot, int use_scsi,
657 BlockInterfaceType type, int index,
658 const char *optstr)
660 QemuOpts *opts;
662 if (type == IF_DEFAULT) {
663 type = use_scsi ? IF_SCSI : IF_IDE;
666 if (!enable || drive_get_by_index(type, index)) {
667 return;
670 opts = drive_add(type, index, NULL, optstr);
671 if (snapshot) {
672 drive_enable_snapshot(opts, NULL);
674 if (!drive_init(opts, use_scsi)) {
675 exit(1);
679 void qemu_register_boot_set(QEMUBootSetHandler *func, void *opaque)
681 boot_set_handler = func;
682 boot_set_opaque = opaque;
685 int qemu_boot_set(const char *boot_devices)
687 if (!boot_set_handler) {
688 return -EINVAL;
690 return boot_set_handler(boot_set_opaque, boot_devices);
693 static void validate_bootdevices(char *devices)
695 /* We just do some generic consistency checks */
696 const char *p;
697 int bitmap = 0;
699 for (p = devices; *p != '\0'; p++) {
700 /* Allowed boot devices are:
701 * a-b: floppy disk drives
702 * c-f: IDE disk drives
703 * g-m: machine implementation dependant drives
704 * n-p: network devices
705 * It's up to each machine implementation to check if the given boot
706 * devices match the actual hardware implementation and firmware
707 * features.
709 if (*p < 'a' || *p > 'p') {
710 fprintf(stderr, "Invalid boot device '%c'\n", *p);
711 exit(1);
713 if (bitmap & (1 << (*p - 'a'))) {
714 fprintf(stderr, "Boot device '%c' was given twice\n", *p);
715 exit(1);
717 bitmap |= 1 << (*p - 'a');
721 static void restore_boot_devices(void *opaque)
723 char *standard_boot_devices = opaque;
724 static int first = 1;
726 /* Restore boot order and remove ourselves after the first boot */
727 if (first) {
728 first = 0;
729 return;
732 qemu_boot_set(standard_boot_devices);
734 qemu_unregister_reset(restore_boot_devices, standard_boot_devices);
735 qemu_free(standard_boot_devices);
738 void add_boot_device_path(int32_t bootindex, DeviceState *dev,
739 const char *suffix)
741 FWBootEntry *node, *i;
743 if (bootindex < 0) {
744 return;
747 assert(dev != NULL || suffix != NULL);
749 node = qemu_mallocz(sizeof(FWBootEntry));
750 node->bootindex = bootindex;
751 node->suffix = suffix ? qemu_strdup(suffix) : NULL;
752 node->dev = dev;
754 QTAILQ_FOREACH(i, &fw_boot_order, link) {
755 if (i->bootindex == bootindex) {
756 fprintf(stderr, "Two devices with same boot index %d\n", bootindex);
757 exit(1);
758 } else if (i->bootindex < bootindex) {
759 continue;
761 QTAILQ_INSERT_BEFORE(i, node, link);
762 return;
764 QTAILQ_INSERT_TAIL(&fw_boot_order, node, link);
768 * This function returns null terminated string that consist of new line
769 * separated device paths.
771 * memory pointed by "size" is assigned total length of the array in bytes
774 char *get_boot_devices_list(uint32_t *size)
776 FWBootEntry *i;
777 uint32_t total = 0;
778 char *list = NULL;
780 QTAILQ_FOREACH(i, &fw_boot_order, link) {
781 char *devpath = NULL, *bootpath;
782 int len;
784 if (i->dev) {
785 devpath = qdev_get_fw_dev_path(i->dev);
786 assert(devpath);
789 if (i->suffix && devpath) {
790 size_t bootpathlen = strlen(devpath) + strlen(i->suffix) + 1;
792 bootpath = qemu_malloc(bootpathlen);
793 snprintf(bootpath, bootpathlen, "%s%s", devpath, i->suffix);
794 qemu_free(devpath);
795 } else if (devpath) {
796 bootpath = devpath;
797 } else {
798 bootpath = qemu_strdup(i->suffix);
799 assert(bootpath);
802 if (total) {
803 list[total-1] = '\n';
805 len = strlen(bootpath) + 1;
806 list = qemu_realloc(list, total + len);
807 memcpy(&list[total], bootpath, len);
808 total += len;
809 qemu_free(bootpath);
812 *size = total;
814 return list;
817 static void numa_add(const char *optarg)
819 char option[128];
820 char *endptr;
821 unsigned long long value, endvalue;
822 int nodenr;
824 optarg = get_opt_name(option, 128, optarg, ',') + 1;
825 if (!strcmp(option, "node")) {
826 if (get_param_value(option, 128, "nodeid", optarg) == 0) {
827 nodenr = nb_numa_nodes;
828 } else {
829 nodenr = strtoull(option, NULL, 10);
832 if (get_param_value(option, 128, "mem", optarg) == 0) {
833 node_mem[nodenr] = 0;
834 } else {
835 int64_t sval;
836 sval = strtosz(option, NULL);
837 if (sval < 0) {
838 fprintf(stderr, "qemu: invalid numa mem size: %s\n", optarg);
839 exit(1);
841 node_mem[nodenr] = sval;
843 if (get_param_value(option, 128, "cpus", optarg) == 0) {
844 node_cpumask[nodenr] = 0;
845 } else {
846 value = strtoull(option, &endptr, 10);
847 if (value >= 64) {
848 value = 63;
849 fprintf(stderr, "only 64 CPUs in NUMA mode supported.\n");
850 } else {
851 if (*endptr == '-') {
852 endvalue = strtoull(endptr+1, &endptr, 10);
853 if (endvalue >= 63) {
854 endvalue = 62;
855 fprintf(stderr,
856 "only 63 CPUs in NUMA mode supported.\n");
858 value = (2ULL << endvalue) - (1ULL << value);
859 } else {
860 value = 1ULL << value;
863 node_cpumask[nodenr] = value;
865 nb_numa_nodes++;
867 return;
870 static void smp_parse(const char *optarg)
872 int smp, sockets = 0, threads = 0, cores = 0;
873 char *endptr;
874 char option[128];
876 smp = strtoul(optarg, &endptr, 10);
877 if (endptr != optarg) {
878 if (*endptr == ',') {
879 endptr++;
882 if (get_param_value(option, 128, "sockets", endptr) != 0)
883 sockets = strtoull(option, NULL, 10);
884 if (get_param_value(option, 128, "cores", endptr) != 0)
885 cores = strtoull(option, NULL, 10);
886 if (get_param_value(option, 128, "threads", endptr) != 0)
887 threads = strtoull(option, NULL, 10);
888 if (get_param_value(option, 128, "maxcpus", endptr) != 0)
889 max_cpus = strtoull(option, NULL, 10);
891 /* compute missing values, prefer sockets over cores over threads */
892 if (smp == 0 || sockets == 0) {
893 sockets = sockets > 0 ? sockets : 1;
894 cores = cores > 0 ? cores : 1;
895 threads = threads > 0 ? threads : 1;
896 if (smp == 0) {
897 smp = cores * threads * sockets;
899 } else {
900 if (cores == 0) {
901 threads = threads > 0 ? threads : 1;
902 cores = smp / (sockets * threads);
903 } else {
904 threads = smp / (cores * sockets);
907 smp_cpus = smp;
908 smp_cores = cores > 0 ? cores : 1;
909 smp_threads = threads > 0 ? threads : 1;
910 if (max_cpus == 0)
911 max_cpus = smp_cpus;
914 /***********************************************************/
915 /* USB devices */
917 static int usb_device_add(const char *devname)
919 const char *p;
920 USBDevice *dev = NULL;
922 if (!usb_enabled)
923 return -1;
925 /* drivers with .usbdevice_name entry in USBDeviceInfo */
926 dev = usbdevice_create(devname);
927 if (dev)
928 goto done;
930 /* the other ones */
931 #ifndef CONFIG_LINUX
932 /* only the linux version is qdev-ified, usb-bsd still needs this */
933 if (strstart(devname, "host:", &p)) {
934 dev = usb_host_device_open(p);
935 } else
936 #endif
937 if (!strcmp(devname, "bt") || strstart(devname, "bt:", &p)) {
938 dev = usb_bt_init(devname[2] ? hci_init(p) :
939 bt_new_hci(qemu_find_bt_vlan(0)));
940 } else {
941 return -1;
943 if (!dev)
944 return -1;
946 done:
947 return 0;
950 static int usb_device_del(const char *devname)
952 int bus_num, addr;
953 const char *p;
955 if (strstart(devname, "host:", &p))
956 return usb_host_device_close(p);
958 if (!usb_enabled)
959 return -1;
961 p = strchr(devname, '.');
962 if (!p)
963 return -1;
964 bus_num = strtoul(devname, NULL, 0);
965 addr = strtoul(p + 1, NULL, 0);
967 return usb_device_delete_addr(bus_num, addr);
970 static int usb_parse(const char *cmdline)
972 int r;
973 r = usb_device_add(cmdline);
974 if (r < 0) {
975 fprintf(stderr, "qemu: could not add USB device '%s'\n", cmdline);
977 return r;
980 void do_usb_add(Monitor *mon, const QDict *qdict)
982 const char *devname = qdict_get_str(qdict, "devname");
983 if (usb_device_add(devname) < 0) {
984 error_report("could not add USB device '%s'", devname);
988 void do_usb_del(Monitor *mon, const QDict *qdict)
990 const char *devname = qdict_get_str(qdict, "devname");
991 if (usb_device_del(devname) < 0) {
992 error_report("could not delete USB device '%s'", devname);
996 /***********************************************************/
997 /* PCMCIA/Cardbus */
999 static struct pcmcia_socket_entry_s {
1000 PCMCIASocket *socket;
1001 struct pcmcia_socket_entry_s *next;
1002 } *pcmcia_sockets = 0;
1004 void pcmcia_socket_register(PCMCIASocket *socket)
1006 struct pcmcia_socket_entry_s *entry;
1008 entry = qemu_malloc(sizeof(struct pcmcia_socket_entry_s));
1009 entry->socket = socket;
1010 entry->next = pcmcia_sockets;
1011 pcmcia_sockets = entry;
1014 void pcmcia_socket_unregister(PCMCIASocket *socket)
1016 struct pcmcia_socket_entry_s *entry, **ptr;
1018 ptr = &pcmcia_sockets;
1019 for (entry = *ptr; entry; ptr = &entry->next, entry = *ptr)
1020 if (entry->socket == socket) {
1021 *ptr = entry->next;
1022 qemu_free(entry);
1026 void pcmcia_info(Monitor *mon)
1028 struct pcmcia_socket_entry_s *iter;
1030 if (!pcmcia_sockets)
1031 monitor_printf(mon, "No PCMCIA sockets\n");
1033 for (iter = pcmcia_sockets; iter; iter = iter->next)
1034 monitor_printf(mon, "%s: %s\n", iter->socket->slot_string,
1035 iter->socket->attached ? iter->socket->card_string :
1036 "Empty");
1039 /***********************************************************/
1040 /* machine registration */
1042 static QEMUMachine *first_machine = NULL;
1043 QEMUMachine *current_machine = NULL;
1045 int qemu_register_machine(QEMUMachine *m)
1047 QEMUMachine **pm;
1048 pm = &first_machine;
1049 while (*pm != NULL)
1050 pm = &(*pm)->next;
1051 m->next = NULL;
1052 *pm = m;
1053 return 0;
1056 static QEMUMachine *find_machine(const char *name)
1058 QEMUMachine *m;
1060 for(m = first_machine; m != NULL; m = m->next) {
1061 if (!strcmp(m->name, name))
1062 return m;
1063 if (m->alias && !strcmp(m->alias, name))
1064 return m;
1066 return NULL;
1069 static QEMUMachine *find_default_machine(void)
1071 QEMUMachine *m;
1073 for(m = first_machine; m != NULL; m = m->next) {
1074 if (m->is_default) {
1075 return m;
1078 return NULL;
1081 /***********************************************************/
1082 /* main execution loop */
1084 static void gui_update(void *opaque)
1086 uint64_t interval = GUI_REFRESH_INTERVAL;
1087 DisplayState *ds = opaque;
1088 DisplayChangeListener *dcl = ds->listeners;
1090 qemu_flush_coalesced_mmio_buffer();
1091 dpy_refresh(ds);
1093 while (dcl != NULL) {
1094 if (dcl->gui_timer_interval &&
1095 dcl->gui_timer_interval < interval)
1096 interval = dcl->gui_timer_interval;
1097 dcl = dcl->next;
1099 qemu_mod_timer(ds->gui_timer, interval + qemu_get_clock_ms(rt_clock));
1102 static void nographic_update(void *opaque)
1104 uint64_t interval = GUI_REFRESH_INTERVAL;
1106 qemu_flush_coalesced_mmio_buffer();
1107 qemu_mod_timer(nographic_timer, interval + qemu_get_clock_ms(rt_clock));
1110 struct vm_change_state_entry {
1111 VMChangeStateHandler *cb;
1112 void *opaque;
1113 QLIST_ENTRY (vm_change_state_entry) entries;
1116 static QLIST_HEAD(vm_change_state_head, vm_change_state_entry) vm_change_state_head;
1118 VMChangeStateEntry *qemu_add_vm_change_state_handler(VMChangeStateHandler *cb,
1119 void *opaque)
1121 VMChangeStateEntry *e;
1123 e = qemu_mallocz(sizeof (*e));
1125 e->cb = cb;
1126 e->opaque = opaque;
1127 QLIST_INSERT_HEAD(&vm_change_state_head, e, entries);
1128 return e;
1131 void qemu_del_vm_change_state_handler(VMChangeStateEntry *e)
1133 QLIST_REMOVE (e, entries);
1134 qemu_free (e);
1137 void vm_state_notify(int running, int reason)
1139 VMChangeStateEntry *e;
1141 trace_vm_state_notify(running, reason);
1143 for (e = vm_change_state_head.lh_first; e; e = e->entries.le_next) {
1144 e->cb(e->opaque, running, reason);
1148 void vm_start(void)
1150 if (!vm_running) {
1151 cpu_enable_ticks();
1152 vm_running = 1;
1153 vm_state_notify(1, 0);
1154 resume_all_vcpus();
1155 monitor_protocol_event(QEVENT_RESUME, NULL);
1159 /* reset/shutdown handler */
1161 typedef struct QEMUResetEntry {
1162 QTAILQ_ENTRY(QEMUResetEntry) entry;
1163 QEMUResetHandler *func;
1164 void *opaque;
1165 } QEMUResetEntry;
1167 static QTAILQ_HEAD(reset_handlers, QEMUResetEntry) reset_handlers =
1168 QTAILQ_HEAD_INITIALIZER(reset_handlers);
1169 static int reset_requested;
1170 static int shutdown_requested, shutdown_signal = -1;
1171 static pid_t shutdown_pid;
1172 static int powerdown_requested;
1173 static int debug_requested;
1174 static int vmstop_requested;
1176 int qemu_shutdown_requested_get(void)
1178 return shutdown_requested;
1181 int qemu_reset_requested_get(void)
1183 return reset_requested;
1186 int qemu_shutdown_requested(void)
1188 int r = shutdown_requested;
1189 shutdown_requested = 0;
1190 return r;
1193 void qemu_kill_report(void)
1195 if (shutdown_signal != -1) {
1196 fprintf(stderr, "qemu: terminating on signal %d", shutdown_signal);
1197 if (shutdown_pid == 0) {
1198 /* This happens for eg ^C at the terminal, so it's worth
1199 * avoiding printing an odd message in that case.
1201 fputc('\n', stderr);
1202 } else {
1203 fprintf(stderr, " from pid " FMT_pid "\n", shutdown_pid);
1205 shutdown_signal = -1;
1209 int qemu_reset_requested(void)
1211 int r = reset_requested;
1212 reset_requested = 0;
1213 return r;
1216 int qemu_powerdown_requested(void)
1218 int r = powerdown_requested;
1219 powerdown_requested = 0;
1220 return r;
1223 static int qemu_debug_requested(void)
1225 int r = debug_requested;
1226 debug_requested = 0;
1227 return r;
1230 static int qemu_vmstop_requested(void)
1232 int r = vmstop_requested;
1233 vmstop_requested = 0;
1234 return r;
1237 void qemu_register_reset(QEMUResetHandler *func, void *opaque)
1239 QEMUResetEntry *re = qemu_mallocz(sizeof(QEMUResetEntry));
1241 re->func = func;
1242 re->opaque = opaque;
1243 QTAILQ_INSERT_TAIL(&reset_handlers, re, entry);
1246 void qemu_unregister_reset(QEMUResetHandler *func, void *opaque)
1248 QEMUResetEntry *re;
1250 QTAILQ_FOREACH(re, &reset_handlers, entry) {
1251 if (re->func == func && re->opaque == opaque) {
1252 QTAILQ_REMOVE(&reset_handlers, re, entry);
1253 qemu_free(re);
1254 return;
1259 void qemu_system_reset(bool report)
1261 QEMUResetEntry *re, *nre;
1263 /* reset all devices */
1264 QTAILQ_FOREACH_SAFE(re, &reset_handlers, entry, nre) {
1265 re->func(re->opaque);
1267 if (report) {
1268 monitor_protocol_event(QEVENT_RESET, NULL);
1270 cpu_synchronize_all_post_reset();
1273 void qemu_system_reset_request(void)
1275 if (no_reboot) {
1276 shutdown_requested = 1;
1277 } else {
1278 reset_requested = 1;
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 qemu_main_loop_start();
1381 for (;;) {
1382 #ifndef CONFIG_IOTHREAD
1383 nonblocking = cpu_exec_all();
1384 if (vm_request_pending()) {
1385 nonblocking = true;
1387 #endif
1388 #ifdef CONFIG_PROFILER
1389 ti = profile_getclock();
1390 #endif
1391 main_loop_wait(nonblocking);
1392 #ifdef CONFIG_PROFILER
1393 dev_time += profile_getclock() - ti;
1394 #endif
1396 if (qemu_debug_requested()) {
1397 vm_stop(VMSTOP_DEBUG);
1399 if (qemu_shutdown_requested()) {
1400 qemu_kill_report();
1401 monitor_protocol_event(QEVENT_SHUTDOWN, NULL);
1402 if (no_shutdown) {
1403 vm_stop(VMSTOP_SHUTDOWN);
1404 no_shutdown = 0;
1405 } else
1406 break;
1408 if (qemu_reset_requested()) {
1409 pause_all_vcpus();
1410 cpu_synchronize_all_states();
1411 qemu_system_reset(VMRESET_REPORT);
1412 resume_all_vcpus();
1414 if (qemu_powerdown_requested()) {
1415 monitor_protocol_event(QEVENT_POWERDOWN, NULL);
1416 qemu_irq_raise(qemu_system_powerdown);
1418 if ((r = qemu_vmstop_requested())) {
1419 vm_stop(r);
1422 bdrv_close_all();
1423 pause_all_vcpus();
1426 static void version(void)
1428 printf("QEMU emulator version " QEMU_VERSION QEMU_PKGVERSION ", Copyright (c) 2003-2008 Fabrice Bellard\n");
1431 static void help(int exitcode)
1433 const char *options_help =
1434 #define DEF(option, opt_arg, opt_enum, opt_help, arch_mask) \
1435 opt_help
1436 #define DEFHEADING(text) stringify(text) "\n"
1437 #include "qemu-options.def"
1438 #undef DEF
1439 #undef DEFHEADING
1440 #undef GEN_DOCS
1442 version();
1443 printf("usage: %s [options] [disk_image]\n"
1444 "\n"
1445 "'disk_image' is a raw hard disk image for IDE hard disk 0\n"
1446 "\n"
1447 "%s\n"
1448 "During emulation, the following keys are useful:\n"
1449 "ctrl-alt-f toggle full screen\n"
1450 "ctrl-alt-n switch to virtual console 'n'\n"
1451 "ctrl-alt toggle mouse and keyboard grab\n"
1452 "\n"
1453 "When using -nographic, press 'ctrl-a h' to get some help.\n",
1454 "qemu",
1455 options_help);
1456 exit(exitcode);
1459 #define HAS_ARG 0x0001
1461 typedef struct QEMUOption {
1462 const char *name;
1463 int flags;
1464 int index;
1465 uint32_t arch_mask;
1466 } QEMUOption;
1468 static const QEMUOption qemu_options[] = {
1469 { "h", 0, QEMU_OPTION_h, QEMU_ARCH_ALL },
1470 #define DEF(option, opt_arg, opt_enum, opt_help, arch_mask) \
1471 { option, opt_arg, opt_enum, arch_mask },
1472 #define DEFHEADING(text)
1473 #include "qemu-options.def"
1474 #undef DEF
1475 #undef DEFHEADING
1476 #undef GEN_DOCS
1477 { NULL },
1479 static void select_vgahw (const char *p)
1481 const char *opts;
1483 default_vga = 0;
1484 vga_interface_type = VGA_NONE;
1485 if (strstart(p, "std", &opts)) {
1486 vga_interface_type = VGA_STD;
1487 } else if (strstart(p, "cirrus", &opts)) {
1488 vga_interface_type = VGA_CIRRUS;
1489 } else if (strstart(p, "vmware", &opts)) {
1490 vga_interface_type = VGA_VMWARE;
1491 } else if (strstart(p, "xenfb", &opts)) {
1492 vga_interface_type = VGA_XENFB;
1493 } else if (strstart(p, "qxl", &opts)) {
1494 vga_interface_type = VGA_QXL;
1495 } else if (!strstart(p, "none", &opts)) {
1496 invalid_vga:
1497 fprintf(stderr, "Unknown vga type: %s\n", p);
1498 exit(1);
1500 while (*opts) {
1501 const char *nextopt;
1503 if (strstart(opts, ",retrace=", &nextopt)) {
1504 opts = nextopt;
1505 if (strstart(opts, "dumb", &nextopt))
1506 vga_retrace_method = VGA_RETRACE_DUMB;
1507 else if (strstart(opts, "precise", &nextopt))
1508 vga_retrace_method = VGA_RETRACE_PRECISE;
1509 else goto invalid_vga;
1510 } else goto invalid_vga;
1511 opts = nextopt;
1515 static DisplayType select_display(const char *p)
1517 const char *opts;
1518 DisplayType display = DT_DEFAULT;
1520 if (strstart(p, "sdl", &opts)) {
1521 #ifdef CONFIG_SDL
1522 display = DT_SDL;
1523 while (*opts) {
1524 const char *nextopt;
1526 if (strstart(opts, ",frame=", &nextopt)) {
1527 opts = nextopt;
1528 if (strstart(opts, "on", &nextopt)) {
1529 no_frame = 0;
1530 } else if (strstart(opts, "off", &nextopt)) {
1531 no_frame = 1;
1532 } else {
1533 goto invalid_sdl_args;
1535 } else if (strstart(opts, ",alt_grab=", &nextopt)) {
1536 opts = nextopt;
1537 if (strstart(opts, "on", &nextopt)) {
1538 alt_grab = 1;
1539 } else if (strstart(opts, "off", &nextopt)) {
1540 alt_grab = 0;
1541 } else {
1542 goto invalid_sdl_args;
1544 } else if (strstart(opts, ",ctrl_grab=", &nextopt)) {
1545 opts = nextopt;
1546 if (strstart(opts, "on", &nextopt)) {
1547 ctrl_grab = 1;
1548 } else if (strstart(opts, "off", &nextopt)) {
1549 ctrl_grab = 0;
1550 } else {
1551 goto invalid_sdl_args;
1553 } else if (strstart(opts, ",window_close=", &nextopt)) {
1554 opts = nextopt;
1555 if (strstart(opts, "on", &nextopt)) {
1556 no_quit = 0;
1557 } else if (strstart(opts, "off", &nextopt)) {
1558 no_quit = 1;
1559 } else {
1560 goto invalid_sdl_args;
1562 } else {
1563 invalid_sdl_args:
1564 fprintf(stderr, "Invalid SDL option string: %s\n", p);
1565 exit(1);
1567 opts = nextopt;
1569 #else
1570 fprintf(stderr, "SDL support is disabled\n");
1571 exit(1);
1572 #endif
1573 } else if (strstart(p, "vnc", &opts)) {
1574 #ifdef CONFIG_VNC
1575 display_remote++;
1577 if (*opts) {
1578 const char *nextopt;
1580 if (strstart(opts, "=", &nextopt)) {
1581 vnc_display = nextopt;
1584 if (!vnc_display) {
1585 fprintf(stderr, "VNC requires a display argument vnc=<display>\n");
1586 exit(1);
1588 #else
1589 fprintf(stderr, "VNC support is disabled\n");
1590 exit(1);
1591 #endif
1592 } else if (strstart(p, "curses", &opts)) {
1593 #ifdef CONFIG_CURSES
1594 display = DT_CURSES;
1595 #else
1596 fprintf(stderr, "Curses support is disabled\n");
1597 exit(1);
1598 #endif
1599 } else if (strstart(p, "none", &opts)) {
1600 display = DT_NONE;
1601 } else {
1602 fprintf(stderr, "Unknown display type: %s\n", p);
1603 exit(1);
1606 return display;
1609 static int balloon_parse(const char *arg)
1611 QemuOpts *opts;
1613 if (strcmp(arg, "none") == 0) {
1614 return 0;
1617 if (!strncmp(arg, "virtio", 6)) {
1618 if (arg[6] == ',') {
1619 /* have params -> parse them */
1620 opts = qemu_opts_parse(qemu_find_opts("device"), arg+7, 0);
1621 if (!opts)
1622 return -1;
1623 } else {
1624 /* create empty opts */
1625 opts = qemu_opts_create(qemu_find_opts("device"), NULL, 0);
1627 qemu_opt_set(opts, "driver", "virtio-balloon");
1628 return 0;
1631 return -1;
1634 char *qemu_find_file(int type, const char *name)
1636 int len;
1637 const char *subdir;
1638 char *buf;
1640 /* If name contains path separators then try it as a straight path. */
1641 if ((strchr(name, '/') || strchr(name, '\\'))
1642 && access(name, R_OK) == 0) {
1643 return qemu_strdup(name);
1645 switch (type) {
1646 case QEMU_FILE_TYPE_BIOS:
1647 subdir = "";
1648 break;
1649 case QEMU_FILE_TYPE_KEYMAP:
1650 subdir = "keymaps/";
1651 break;
1652 default:
1653 abort();
1655 len = strlen(data_dir) + strlen(name) + strlen(subdir) + 2;
1656 buf = qemu_mallocz(len);
1657 snprintf(buf, len, "%s/%s%s", data_dir, subdir, name);
1658 if (access(buf, R_OK)) {
1659 qemu_free(buf);
1660 return NULL;
1662 return buf;
1665 static int device_help_func(QemuOpts *opts, void *opaque)
1667 return qdev_device_help(opts);
1670 static int device_init_func(QemuOpts *opts, void *opaque)
1672 DeviceState *dev;
1674 dev = qdev_device_add(opts);
1675 if (!dev)
1676 return -1;
1677 return 0;
1680 static int chardev_init_func(QemuOpts *opts, void *opaque)
1682 CharDriverState *chr;
1684 chr = qemu_chr_open_opts(opts, NULL);
1685 if (!chr)
1686 return -1;
1687 return 0;
1690 #ifdef CONFIG_VIRTFS
1691 static int fsdev_init_func(QemuOpts *opts, void *opaque)
1693 int ret;
1694 ret = qemu_fsdev_add(opts);
1696 return ret;
1698 #endif
1700 static int mon_init_func(QemuOpts *opts, void *opaque)
1702 CharDriverState *chr;
1703 const char *chardev;
1704 const char *mode;
1705 int flags;
1707 mode = qemu_opt_get(opts, "mode");
1708 if (mode == NULL) {
1709 mode = "readline";
1711 if (strcmp(mode, "readline") == 0) {
1712 flags = MONITOR_USE_READLINE;
1713 } else if (strcmp(mode, "control") == 0) {
1714 flags = MONITOR_USE_CONTROL;
1715 } else {
1716 fprintf(stderr, "unknown monitor mode \"%s\"\n", mode);
1717 exit(1);
1720 if (qemu_opt_get_bool(opts, "pretty", 0))
1721 flags |= MONITOR_USE_PRETTY;
1723 if (qemu_opt_get_bool(opts, "default", 0))
1724 flags |= MONITOR_IS_DEFAULT;
1726 chardev = qemu_opt_get(opts, "chardev");
1727 chr = qemu_chr_find(chardev);
1728 if (chr == NULL) {
1729 fprintf(stderr, "chardev \"%s\" not found\n", chardev);
1730 exit(1);
1733 monitor_init(chr, flags);
1734 return 0;
1737 static void monitor_parse(const char *optarg, const char *mode)
1739 static int monitor_device_index = 0;
1740 QemuOpts *opts;
1741 const char *p;
1742 char label[32];
1743 int def = 0;
1745 if (strstart(optarg, "chardev:", &p)) {
1746 snprintf(label, sizeof(label), "%s", p);
1747 } else {
1748 snprintf(label, sizeof(label), "compat_monitor%d",
1749 monitor_device_index);
1750 if (monitor_device_index == 0) {
1751 def = 1;
1753 opts = qemu_chr_parse_compat(label, optarg);
1754 if (!opts) {
1755 fprintf(stderr, "parse error: %s\n", optarg);
1756 exit(1);
1760 opts = qemu_opts_create(qemu_find_opts("mon"), label, 1);
1761 if (!opts) {
1762 fprintf(stderr, "duplicate chardev: %s\n", label);
1763 exit(1);
1765 qemu_opt_set(opts, "mode", mode);
1766 qemu_opt_set(opts, "chardev", label);
1767 if (def)
1768 qemu_opt_set(opts, "default", "on");
1769 monitor_device_index++;
1772 struct device_config {
1773 enum {
1774 DEV_USB, /* -usbdevice */
1775 DEV_BT, /* -bt */
1776 DEV_SERIAL, /* -serial */
1777 DEV_PARALLEL, /* -parallel */
1778 DEV_VIRTCON, /* -virtioconsole */
1779 DEV_DEBUGCON, /* -debugcon */
1780 } type;
1781 const char *cmdline;
1782 QTAILQ_ENTRY(device_config) next;
1784 QTAILQ_HEAD(, device_config) device_configs = QTAILQ_HEAD_INITIALIZER(device_configs);
1786 static void add_device_config(int type, const char *cmdline)
1788 struct device_config *conf;
1790 conf = qemu_mallocz(sizeof(*conf));
1791 conf->type = type;
1792 conf->cmdline = cmdline;
1793 QTAILQ_INSERT_TAIL(&device_configs, conf, next);
1796 static int foreach_device_config(int type, int (*func)(const char *cmdline))
1798 struct device_config *conf;
1799 int rc;
1801 QTAILQ_FOREACH(conf, &device_configs, next) {
1802 if (conf->type != type)
1803 continue;
1804 rc = func(conf->cmdline);
1805 if (0 != rc)
1806 return rc;
1808 return 0;
1811 static int serial_parse(const char *devname)
1813 static int index = 0;
1814 char label[32];
1816 if (strcmp(devname, "none") == 0)
1817 return 0;
1818 if (index == MAX_SERIAL_PORTS) {
1819 fprintf(stderr, "qemu: too many serial ports\n");
1820 exit(1);
1822 snprintf(label, sizeof(label), "serial%d", index);
1823 serial_hds[index] = qemu_chr_open(label, devname, NULL);
1824 if (!serial_hds[index]) {
1825 fprintf(stderr, "qemu: could not open serial device '%s': %s\n",
1826 devname, strerror(errno));
1827 return -1;
1829 index++;
1830 return 0;
1833 static int parallel_parse(const char *devname)
1835 static int index = 0;
1836 char label[32];
1838 if (strcmp(devname, "none") == 0)
1839 return 0;
1840 if (index == MAX_PARALLEL_PORTS) {
1841 fprintf(stderr, "qemu: too many parallel ports\n");
1842 exit(1);
1844 snprintf(label, sizeof(label), "parallel%d", index);
1845 parallel_hds[index] = qemu_chr_open(label, devname, NULL);
1846 if (!parallel_hds[index]) {
1847 fprintf(stderr, "qemu: could not open parallel device '%s': %s\n",
1848 devname, strerror(errno));
1849 return -1;
1851 index++;
1852 return 0;
1855 static int virtcon_parse(const char *devname)
1857 QemuOptsList *device = qemu_find_opts("device");
1858 static int index = 0;
1859 char label[32];
1860 QemuOpts *bus_opts, *dev_opts;
1862 if (strcmp(devname, "none") == 0)
1863 return 0;
1864 if (index == MAX_VIRTIO_CONSOLES) {
1865 fprintf(stderr, "qemu: too many virtio consoles\n");
1866 exit(1);
1869 bus_opts = qemu_opts_create(device, NULL, 0);
1870 qemu_opt_set(bus_opts, "driver", "virtio-serial");
1872 dev_opts = qemu_opts_create(device, NULL, 0);
1873 qemu_opt_set(dev_opts, "driver", "virtconsole");
1875 snprintf(label, sizeof(label), "virtcon%d", index);
1876 virtcon_hds[index] = qemu_chr_open(label, devname, NULL);
1877 if (!virtcon_hds[index]) {
1878 fprintf(stderr, "qemu: could not open virtio console '%s': %s\n",
1879 devname, strerror(errno));
1880 return -1;
1882 qemu_opt_set(dev_opts, "chardev", label);
1884 index++;
1885 return 0;
1888 static int debugcon_parse(const char *devname)
1890 QemuOpts *opts;
1892 if (!qemu_chr_open("debugcon", devname, NULL)) {
1893 exit(1);
1895 opts = qemu_opts_create(qemu_find_opts("device"), "debugcon", 1);
1896 if (!opts) {
1897 fprintf(stderr, "qemu: already have a debugcon device\n");
1898 exit(1);
1900 qemu_opt_set(opts, "driver", "isa-debugcon");
1901 qemu_opt_set(opts, "chardev", "debugcon");
1902 return 0;
1905 static int tcg_init(void)
1907 return 0;
1910 static struct {
1911 const char *opt_name;
1912 const char *name;
1913 int (*available)(void);
1914 int (*init)(void);
1915 int *allowed;
1916 } accel_list[] = {
1917 { "tcg", "tcg", tcg_available, tcg_init, &tcg_allowed },
1918 { "xen", "Xen", xen_available, xen_init, &xen_allowed },
1919 { "kvm", "KVM", kvm_available, kvm_init, &kvm_allowed },
1922 static int configure_accelerator(void)
1924 const char *p = NULL;
1925 char buf[10];
1926 int i, ret;
1927 bool accel_initalised = 0;
1928 bool init_failed = 0;
1930 QemuOptsList *list = qemu_find_opts("machine");
1931 if (!QTAILQ_EMPTY(&list->head)) {
1932 p = qemu_opt_get(QTAILQ_FIRST(&list->head), "accel");
1935 if (p == NULL) {
1936 /* Use the default "accelerator", kvm */
1937 p = "kvm";
1940 while (!accel_initalised && *p != '\0') {
1941 if (*p == ':') {
1942 p++;
1944 p = get_opt_name(buf, sizeof (buf), p, ':');
1945 for (i = 0; i < ARRAY_SIZE(accel_list); i++) {
1946 if (strcmp(accel_list[i].opt_name, buf) == 0) {
1947 *(accel_list[i].allowed) = 1;
1948 ret = accel_list[i].init();
1949 if (ret < 0) {
1950 init_failed = 1;
1951 if (!accel_list[i].available()) {
1952 printf("%s not supported for this target\n",
1953 accel_list[i].name);
1954 } else {
1955 fprintf(stderr, "failed to initialize %s: %s\n",
1956 accel_list[i].name,
1957 strerror(-ret));
1959 *(accel_list[i].allowed) = 0;
1960 } else {
1961 accel_initalised = 1;
1963 break;
1966 if (i == ARRAY_SIZE(accel_list)) {
1967 fprintf(stderr, "\"%s\" accelerator does not exist.\n", buf);
1971 if (!accel_initalised) {
1972 fprintf(stderr, "No accelerator found!\n");
1973 exit(1);
1976 if (init_failed) {
1977 fprintf(stderr, "Back to %s accelerator.\n", accel_list[i].name);
1980 return !accel_initalised;
1983 void qemu_add_exit_notifier(Notifier *notify)
1985 notifier_list_add(&exit_notifiers, notify);
1988 void qemu_remove_exit_notifier(Notifier *notify)
1990 notifier_list_remove(&exit_notifiers, notify);
1993 static void qemu_run_exit_notifiers(void)
1995 notifier_list_notify(&exit_notifiers);
1998 void qemu_add_machine_init_done_notifier(Notifier *notify)
2000 notifier_list_add(&machine_init_done_notifiers, notify);
2003 static void qemu_run_machine_init_done_notifiers(void)
2005 notifier_list_notify(&machine_init_done_notifiers);
2008 static const QEMUOption *lookup_opt(int argc, char **argv,
2009 const char **poptarg, int *poptind)
2011 const QEMUOption *popt;
2012 int optind = *poptind;
2013 char *r = argv[optind];
2014 const char *optarg;
2016 loc_set_cmdline(argv, optind, 1);
2017 optind++;
2018 /* Treat --foo the same as -foo. */
2019 if (r[1] == '-')
2020 r++;
2021 popt = qemu_options;
2022 for(;;) {
2023 if (!popt->name) {
2024 error_report("invalid option");
2025 exit(1);
2027 if (!strcmp(popt->name, r + 1))
2028 break;
2029 popt++;
2031 if (popt->flags & HAS_ARG) {
2032 if (optind >= argc) {
2033 error_report("requires an argument");
2034 exit(1);
2036 optarg = argv[optind++];
2037 loc_set_cmdline(argv, optind - 2, 2);
2038 } else {
2039 optarg = NULL;
2042 *poptarg = optarg;
2043 *poptind = optind;
2045 return popt;
2048 int main(int argc, char **argv, char **envp)
2050 const char *gdbstub_dev = NULL;
2051 int i;
2052 int snapshot, linux_boot;
2053 const char *icount_option = NULL;
2054 const char *initrd_filename;
2055 const char *kernel_filename, *kernel_cmdline;
2056 char boot_devices[33] = "cad"; /* default to HD->floppy->CD-ROM */
2057 DisplayState *ds;
2058 DisplayChangeListener *dcl;
2059 int cyls, heads, secs, translation;
2060 QemuOpts *hda_opts = NULL, *opts;
2061 QemuOptsList *olist;
2062 int optind;
2063 const char *optarg;
2064 const char *loadvm = NULL;
2065 QEMUMachine *machine;
2066 const char *cpu_model;
2067 int tb_size;
2068 const char *pid_file = NULL;
2069 const char *incoming = NULL;
2070 #ifdef CONFIG_VNC
2071 int show_vnc_port = 0;
2072 #endif
2073 int defconfig = 1;
2074 const char *trace_file = NULL;
2075 const char *log_mask = NULL;
2076 const char *log_file = NULL;
2078 atexit(qemu_run_exit_notifiers);
2079 error_set_progname(argv[0]);
2081 init_clocks();
2083 qemu_cache_utils_init(envp);
2085 QLIST_INIT (&vm_change_state_head);
2086 os_setup_early_signal_handling();
2088 module_call_init(MODULE_INIT_MACHINE);
2089 machine = find_default_machine();
2090 cpu_model = NULL;
2091 initrd_filename = NULL;
2092 ram_size = 0;
2093 snapshot = 0;
2094 kernel_filename = NULL;
2095 kernel_cmdline = "";
2096 cyls = heads = secs = 0;
2097 translation = BIOS_ATA_TRANSLATION_AUTO;
2099 for (i = 0; i < MAX_NODES; i++) {
2100 node_mem[i] = 0;
2101 node_cpumask[i] = 0;
2104 nb_numa_nodes = 0;
2105 nb_nics = 0;
2107 tb_size = 0;
2108 autostart= 1;
2110 /* first pass of option parsing */
2111 optind = 1;
2112 while (optind < argc) {
2113 if (argv[optind][0] != '-') {
2114 /* disk image */
2115 optind++;
2116 continue;
2117 } else {
2118 const QEMUOption *popt;
2120 popt = lookup_opt(argc, argv, &optarg, &optind);
2121 switch (popt->index) {
2122 case QEMU_OPTION_nodefconfig:
2123 defconfig=0;
2124 break;
2129 if (defconfig) {
2130 int ret;
2132 ret = qemu_read_config_file(CONFIG_QEMU_CONFDIR "/qemu.conf");
2133 if (ret < 0 && ret != -ENOENT) {
2134 exit(1);
2137 ret = qemu_read_config_file(arch_config_name);
2138 if (ret < 0 && ret != -ENOENT) {
2139 exit(1);
2142 cpudef_init();
2144 /* second pass of option parsing */
2145 optind = 1;
2146 for(;;) {
2147 if (optind >= argc)
2148 break;
2149 if (argv[optind][0] != '-') {
2150 hda_opts = drive_add(IF_DEFAULT, 0, argv[optind++], HD_OPTS);
2151 } else {
2152 const QEMUOption *popt;
2154 popt = lookup_opt(argc, argv, &optarg, &optind);
2155 if (!(popt->arch_mask & arch_type)) {
2156 printf("Option %s not supported for this target\n", popt->name);
2157 exit(1);
2159 switch(popt->index) {
2160 case QEMU_OPTION_M:
2161 machine = find_machine(optarg);
2162 if (!machine) {
2163 QEMUMachine *m;
2164 printf("Supported machines are:\n");
2165 for(m = first_machine; m != NULL; m = m->next) {
2166 if (m->alias)
2167 printf("%-10s %s (alias of %s)\n",
2168 m->alias, m->desc, m->name);
2169 printf("%-10s %s%s\n",
2170 m->name, m->desc,
2171 m->is_default ? " (default)" : "");
2173 exit(*optarg != '?');
2175 break;
2176 case QEMU_OPTION_cpu:
2177 /* hw initialization will check this */
2178 if (*optarg == '?') {
2179 list_cpus(stdout, &fprintf, optarg);
2180 exit(0);
2181 } else {
2182 cpu_model = optarg;
2184 break;
2185 case QEMU_OPTION_initrd:
2186 initrd_filename = optarg;
2187 break;
2188 case QEMU_OPTION_hda:
2190 char buf[256];
2191 if (cyls == 0)
2192 snprintf(buf, sizeof(buf), "%s", HD_OPTS);
2193 else
2194 snprintf(buf, sizeof(buf),
2195 "%s,cyls=%d,heads=%d,secs=%d%s",
2196 HD_OPTS , cyls, heads, secs,
2197 translation == BIOS_ATA_TRANSLATION_LBA ?
2198 ",trans=lba" :
2199 translation == BIOS_ATA_TRANSLATION_NONE ?
2200 ",trans=none" : "");
2201 drive_add(IF_DEFAULT, 0, optarg, buf);
2202 break;
2204 case QEMU_OPTION_hdb:
2205 case QEMU_OPTION_hdc:
2206 case QEMU_OPTION_hdd:
2207 drive_add(IF_DEFAULT, popt->index - QEMU_OPTION_hda, optarg,
2208 HD_OPTS);
2209 break;
2210 case QEMU_OPTION_drive:
2211 if (drive_def(optarg) == NULL) {
2212 exit(1);
2214 break;
2215 case QEMU_OPTION_set:
2216 if (qemu_set_option(optarg) != 0)
2217 exit(1);
2218 break;
2219 case QEMU_OPTION_global:
2220 if (qemu_global_option(optarg) != 0)
2221 exit(1);
2222 break;
2223 case QEMU_OPTION_mtdblock:
2224 drive_add(IF_MTD, -1, optarg, MTD_OPTS);
2225 break;
2226 case QEMU_OPTION_sd:
2227 drive_add(IF_SD, 0, optarg, SD_OPTS);
2228 break;
2229 case QEMU_OPTION_pflash:
2230 drive_add(IF_PFLASH, -1, optarg, PFLASH_OPTS);
2231 break;
2232 case QEMU_OPTION_snapshot:
2233 snapshot = 1;
2234 break;
2235 case QEMU_OPTION_hdachs:
2237 const char *p;
2238 p = optarg;
2239 cyls = strtol(p, (char **)&p, 0);
2240 if (cyls < 1 || cyls > 16383)
2241 goto chs_fail;
2242 if (*p != ',')
2243 goto chs_fail;
2244 p++;
2245 heads = strtol(p, (char **)&p, 0);
2246 if (heads < 1 || heads > 16)
2247 goto chs_fail;
2248 if (*p != ',')
2249 goto chs_fail;
2250 p++;
2251 secs = strtol(p, (char **)&p, 0);
2252 if (secs < 1 || secs > 63)
2253 goto chs_fail;
2254 if (*p == ',') {
2255 p++;
2256 if (!strcmp(p, "none"))
2257 translation = BIOS_ATA_TRANSLATION_NONE;
2258 else if (!strcmp(p, "lba"))
2259 translation = BIOS_ATA_TRANSLATION_LBA;
2260 else if (!strcmp(p, "auto"))
2261 translation = BIOS_ATA_TRANSLATION_AUTO;
2262 else
2263 goto chs_fail;
2264 } else if (*p != '\0') {
2265 chs_fail:
2266 fprintf(stderr, "qemu: invalid physical CHS format\n");
2267 exit(1);
2269 if (hda_opts != NULL) {
2270 char num[16];
2271 snprintf(num, sizeof(num), "%d", cyls);
2272 qemu_opt_set(hda_opts, "cyls", num);
2273 snprintf(num, sizeof(num), "%d", heads);
2274 qemu_opt_set(hda_opts, "heads", num);
2275 snprintf(num, sizeof(num), "%d", secs);
2276 qemu_opt_set(hda_opts, "secs", num);
2277 if (translation == BIOS_ATA_TRANSLATION_LBA)
2278 qemu_opt_set(hda_opts, "trans", "lba");
2279 if (translation == BIOS_ATA_TRANSLATION_NONE)
2280 qemu_opt_set(hda_opts, "trans", "none");
2283 break;
2284 case QEMU_OPTION_numa:
2285 if (nb_numa_nodes >= MAX_NODES) {
2286 fprintf(stderr, "qemu: too many NUMA nodes\n");
2287 exit(1);
2289 numa_add(optarg);
2290 break;
2291 case QEMU_OPTION_display:
2292 display_type = select_display(optarg);
2293 break;
2294 case QEMU_OPTION_nographic:
2295 display_type = DT_NOGRAPHIC;
2296 break;
2297 case QEMU_OPTION_curses:
2298 #ifdef CONFIG_CURSES
2299 display_type = DT_CURSES;
2300 #else
2301 fprintf(stderr, "Curses support is disabled\n");
2302 exit(1);
2303 #endif
2304 break;
2305 case QEMU_OPTION_portrait:
2306 graphic_rotate = 1;
2307 break;
2308 case QEMU_OPTION_kernel:
2309 kernel_filename = optarg;
2310 break;
2311 case QEMU_OPTION_append:
2312 kernel_cmdline = optarg;
2313 break;
2314 case QEMU_OPTION_cdrom:
2315 drive_add(IF_DEFAULT, 2, optarg, CDROM_OPTS);
2316 break;
2317 case QEMU_OPTION_boot:
2319 static const char * const params[] = {
2320 "order", "once", "menu", NULL
2322 char buf[sizeof(boot_devices)];
2323 char *standard_boot_devices;
2324 int legacy = 0;
2326 if (!strchr(optarg, '=')) {
2327 legacy = 1;
2328 pstrcpy(buf, sizeof(buf), optarg);
2329 } else if (check_params(buf, sizeof(buf), params, optarg) < 0) {
2330 fprintf(stderr,
2331 "qemu: unknown boot parameter '%s' in '%s'\n",
2332 buf, optarg);
2333 exit(1);
2336 if (legacy ||
2337 get_param_value(buf, sizeof(buf), "order", optarg)) {
2338 validate_bootdevices(buf);
2339 pstrcpy(boot_devices, sizeof(boot_devices), buf);
2341 if (!legacy) {
2342 if (get_param_value(buf, sizeof(buf),
2343 "once", optarg)) {
2344 validate_bootdevices(buf);
2345 standard_boot_devices = qemu_strdup(boot_devices);
2346 pstrcpy(boot_devices, sizeof(boot_devices), buf);
2347 qemu_register_reset(restore_boot_devices,
2348 standard_boot_devices);
2350 if (get_param_value(buf, sizeof(buf),
2351 "menu", optarg)) {
2352 if (!strcmp(buf, "on")) {
2353 boot_menu = 1;
2354 } else if (!strcmp(buf, "off")) {
2355 boot_menu = 0;
2356 } else {
2357 fprintf(stderr,
2358 "qemu: invalid option value '%s'\n",
2359 buf);
2360 exit(1);
2365 break;
2366 case QEMU_OPTION_fda:
2367 case QEMU_OPTION_fdb:
2368 drive_add(IF_FLOPPY, popt->index - QEMU_OPTION_fda,
2369 optarg, FD_OPTS);
2370 break;
2371 case QEMU_OPTION_no_fd_bootchk:
2372 fd_bootchk = 0;
2373 break;
2374 case QEMU_OPTION_netdev:
2375 if (net_client_parse(qemu_find_opts("netdev"), optarg) == -1) {
2376 exit(1);
2378 break;
2379 case QEMU_OPTION_net:
2380 if (net_client_parse(qemu_find_opts("net"), optarg) == -1) {
2381 exit(1);
2383 break;
2384 #ifdef CONFIG_SLIRP
2385 case QEMU_OPTION_tftp:
2386 legacy_tftp_prefix = optarg;
2387 break;
2388 case QEMU_OPTION_bootp:
2389 legacy_bootp_filename = optarg;
2390 break;
2391 case QEMU_OPTION_redir:
2392 if (net_slirp_redir(optarg) < 0)
2393 exit(1);
2394 break;
2395 #endif
2396 case QEMU_OPTION_bt:
2397 add_device_config(DEV_BT, optarg);
2398 break;
2399 case QEMU_OPTION_audio_help:
2400 if (!(audio_available())) {
2401 printf("Option %s not supported for this target\n", popt->name);
2402 exit(1);
2404 AUD_help ();
2405 exit (0);
2406 break;
2407 case QEMU_OPTION_soundhw:
2408 if (!(audio_available())) {
2409 printf("Option %s not supported for this target\n", popt->name);
2410 exit(1);
2412 select_soundhw (optarg);
2413 break;
2414 case QEMU_OPTION_h:
2415 help(0);
2416 break;
2417 case QEMU_OPTION_version:
2418 version();
2419 exit(0);
2420 break;
2421 case QEMU_OPTION_m: {
2422 int64_t value;
2424 value = strtosz(optarg, NULL);
2425 if (value < 0) {
2426 fprintf(stderr, "qemu: invalid ram size: %s\n", optarg);
2427 exit(1);
2430 /* On 32-bit hosts, QEMU is limited by virtual address space */
2431 if (value > (2047 << 20) && HOST_LONG_BITS == 32) {
2432 fprintf(stderr, "qemu: at most 2047 MB RAM can be simulated\n");
2433 exit(1);
2435 if (value != (uint64_t)(ram_addr_t)value) {
2436 fprintf(stderr, "qemu: ram size too large\n");
2437 exit(1);
2439 ram_size = value;
2440 break;
2442 case QEMU_OPTION_mempath:
2443 mem_path = optarg;
2444 break;
2445 #ifdef MAP_POPULATE
2446 case QEMU_OPTION_mem_prealloc:
2447 mem_prealloc = 1;
2448 break;
2449 #endif
2450 case QEMU_OPTION_d:
2451 log_mask = optarg;
2452 break;
2453 case QEMU_OPTION_D:
2454 log_file = optarg;
2455 break;
2456 case QEMU_OPTION_s:
2457 gdbstub_dev = "tcp::" DEFAULT_GDBSTUB_PORT;
2458 break;
2459 case QEMU_OPTION_gdb:
2460 gdbstub_dev = optarg;
2461 break;
2462 case QEMU_OPTION_L:
2463 data_dir = optarg;
2464 break;
2465 case QEMU_OPTION_bios:
2466 bios_name = optarg;
2467 break;
2468 case QEMU_OPTION_singlestep:
2469 singlestep = 1;
2470 break;
2471 case QEMU_OPTION_S:
2472 autostart = 0;
2473 break;
2474 case QEMU_OPTION_k:
2475 keyboard_layout = optarg;
2476 break;
2477 case QEMU_OPTION_localtime:
2478 rtc_utc = 0;
2479 break;
2480 case QEMU_OPTION_vga:
2481 select_vgahw (optarg);
2482 break;
2483 case QEMU_OPTION_g:
2485 const char *p;
2486 int w, h, depth;
2487 p = optarg;
2488 w = strtol(p, (char **)&p, 10);
2489 if (w <= 0) {
2490 graphic_error:
2491 fprintf(stderr, "qemu: invalid resolution or depth\n");
2492 exit(1);
2494 if (*p != 'x')
2495 goto graphic_error;
2496 p++;
2497 h = strtol(p, (char **)&p, 10);
2498 if (h <= 0)
2499 goto graphic_error;
2500 if (*p == 'x') {
2501 p++;
2502 depth = strtol(p, (char **)&p, 10);
2503 if (depth != 8 && depth != 15 && depth != 16 &&
2504 depth != 24 && depth != 32)
2505 goto graphic_error;
2506 } else if (*p == '\0') {
2507 depth = graphic_depth;
2508 } else {
2509 goto graphic_error;
2512 graphic_width = w;
2513 graphic_height = h;
2514 graphic_depth = depth;
2516 break;
2517 case QEMU_OPTION_echr:
2519 char *r;
2520 term_escape_char = strtol(optarg, &r, 0);
2521 if (r == optarg)
2522 printf("Bad argument to echr\n");
2523 break;
2525 case QEMU_OPTION_monitor:
2526 monitor_parse(optarg, "readline");
2527 default_monitor = 0;
2528 break;
2529 case QEMU_OPTION_qmp:
2530 monitor_parse(optarg, "control");
2531 default_monitor = 0;
2532 break;
2533 case QEMU_OPTION_mon:
2534 opts = qemu_opts_parse(qemu_find_opts("mon"), optarg, 1);
2535 if (!opts) {
2536 exit(1);
2538 default_monitor = 0;
2539 break;
2540 case QEMU_OPTION_chardev:
2541 opts = qemu_opts_parse(qemu_find_opts("chardev"), optarg, 1);
2542 if (!opts) {
2543 exit(1);
2545 break;
2546 case QEMU_OPTION_fsdev:
2547 olist = qemu_find_opts("fsdev");
2548 if (!olist) {
2549 fprintf(stderr, "fsdev is not supported by this qemu build.\n");
2550 exit(1);
2552 opts = qemu_opts_parse(olist, optarg, 1);
2553 if (!opts) {
2554 fprintf(stderr, "parse error: %s\n", optarg);
2555 exit(1);
2557 break;
2558 case QEMU_OPTION_virtfs: {
2559 QemuOpts *fsdev;
2560 QemuOpts *device;
2562 olist = qemu_find_opts("virtfs");
2563 if (!olist) {
2564 fprintf(stderr, "virtfs is not supported by this qemu build.\n");
2565 exit(1);
2567 opts = qemu_opts_parse(olist, optarg, 1);
2568 if (!opts) {
2569 fprintf(stderr, "parse error: %s\n", optarg);
2570 exit(1);
2573 if (qemu_opt_get(opts, "fstype") == NULL ||
2574 qemu_opt_get(opts, "mount_tag") == NULL ||
2575 qemu_opt_get(opts, "path") == NULL ||
2576 qemu_opt_get(opts, "security_model") == NULL) {
2577 fprintf(stderr, "Usage: -virtfs fstype,path=/share_path/,"
2578 "security_model=[mapped|passthrough|none],"
2579 "mount_tag=tag.\n");
2580 exit(1);
2583 fsdev = qemu_opts_create(qemu_find_opts("fsdev"),
2584 qemu_opt_get(opts, "mount_tag"), 1);
2585 if (!fsdev) {
2586 fprintf(stderr, "duplicate fsdev id: %s\n",
2587 qemu_opt_get(opts, "mount_tag"));
2588 exit(1);
2590 qemu_opt_set(fsdev, "fstype", qemu_opt_get(opts, "fstype"));
2591 qemu_opt_set(fsdev, "path", qemu_opt_get(opts, "path"));
2592 qemu_opt_set(fsdev, "security_model",
2593 qemu_opt_get(opts, "security_model"));
2595 device = qemu_opts_create(qemu_find_opts("device"), NULL, 0);
2596 qemu_opt_set(device, "driver", "virtio-9p-pci");
2597 qemu_opt_set(device, "fsdev",
2598 qemu_opt_get(opts, "mount_tag"));
2599 qemu_opt_set(device, "mount_tag",
2600 qemu_opt_get(opts, "mount_tag"));
2601 break;
2603 case QEMU_OPTION_serial:
2604 add_device_config(DEV_SERIAL, optarg);
2605 default_serial = 0;
2606 if (strncmp(optarg, "mon:", 4) == 0) {
2607 default_monitor = 0;
2609 break;
2610 case QEMU_OPTION_watchdog:
2611 if (watchdog) {
2612 fprintf(stderr,
2613 "qemu: only one watchdog option may be given\n");
2614 return 1;
2616 watchdog = optarg;
2617 break;
2618 case QEMU_OPTION_watchdog_action:
2619 if (select_watchdog_action(optarg) == -1) {
2620 fprintf(stderr, "Unknown -watchdog-action parameter\n");
2621 exit(1);
2623 break;
2624 case QEMU_OPTION_virtiocon:
2625 add_device_config(DEV_VIRTCON, optarg);
2626 default_virtcon = 0;
2627 if (strncmp(optarg, "mon:", 4) == 0) {
2628 default_monitor = 0;
2630 break;
2631 case QEMU_OPTION_parallel:
2632 add_device_config(DEV_PARALLEL, optarg);
2633 default_parallel = 0;
2634 if (strncmp(optarg, "mon:", 4) == 0) {
2635 default_monitor = 0;
2637 break;
2638 case QEMU_OPTION_debugcon:
2639 add_device_config(DEV_DEBUGCON, optarg);
2640 break;
2641 case QEMU_OPTION_loadvm:
2642 loadvm = optarg;
2643 break;
2644 case QEMU_OPTION_full_screen:
2645 full_screen = 1;
2646 break;
2647 #ifdef CONFIG_SDL
2648 case QEMU_OPTION_no_frame:
2649 no_frame = 1;
2650 break;
2651 case QEMU_OPTION_alt_grab:
2652 alt_grab = 1;
2653 break;
2654 case QEMU_OPTION_ctrl_grab:
2655 ctrl_grab = 1;
2656 break;
2657 case QEMU_OPTION_no_quit:
2658 no_quit = 1;
2659 break;
2660 case QEMU_OPTION_sdl:
2661 display_type = DT_SDL;
2662 break;
2663 #else
2664 case QEMU_OPTION_no_frame:
2665 case QEMU_OPTION_alt_grab:
2666 case QEMU_OPTION_ctrl_grab:
2667 case QEMU_OPTION_no_quit:
2668 case QEMU_OPTION_sdl:
2669 fprintf(stderr, "SDL support is disabled\n");
2670 exit(1);
2671 #endif
2672 case QEMU_OPTION_pidfile:
2673 pid_file = optarg;
2674 break;
2675 case QEMU_OPTION_win2k_hack:
2676 win2k_install_hack = 1;
2677 break;
2678 case QEMU_OPTION_rtc_td_hack:
2679 rtc_td_hack = 1;
2680 break;
2681 case QEMU_OPTION_acpitable:
2682 do_acpitable_option(optarg);
2683 break;
2684 case QEMU_OPTION_smbios:
2685 do_smbios_option(optarg);
2686 break;
2687 case QEMU_OPTION_enable_kvm:
2688 olist = qemu_find_opts("machine");
2689 qemu_opts_reset(olist);
2690 qemu_opts_parse(olist, "accel=kvm", 0);
2691 break;
2692 case QEMU_OPTION_machine:
2693 olist = qemu_find_opts("machine");
2694 qemu_opts_reset(olist);
2695 opts = qemu_opts_parse(olist, optarg, 0);
2696 if (!opts) {
2697 fprintf(stderr, "parse error: %s\n", optarg);
2698 exit(1);
2700 break;
2701 case QEMU_OPTION_no_kvm:
2702 olist = qemu_find_opts("machine");
2703 qemu_opts_reset(olist);
2704 qemu_opts_parse(olist, "accel=tcg", 0);
2705 break;
2706 #ifdef CONFIG_KVM
2707 case QEMU_OPTION_no_kvm_irqchip: {
2708 kvm_irqchip = 0;
2709 kvm_pit = 0;
2710 break;
2712 case QEMU_OPTION_no_kvm_pit: {
2713 kvm_pit = 0;
2714 break;
2716 case QEMU_OPTION_no_kvm_pit_reinjection: {
2717 kvm_pit_reinject = 0;
2718 break;
2720 #endif
2721 case QEMU_OPTION_usb:
2722 usb_enabled = 1;
2723 break;
2724 case QEMU_OPTION_usbdevice:
2725 usb_enabled = 1;
2726 add_device_config(DEV_USB, optarg);
2727 break;
2728 case QEMU_OPTION_device:
2729 if (!qemu_opts_parse(qemu_find_opts("device"), optarg, 1)) {
2730 exit(1);
2732 break;
2733 case QEMU_OPTION_smp:
2734 smp_parse(optarg);
2735 if (smp_cpus < 1) {
2736 fprintf(stderr, "Invalid number of CPUs\n");
2737 exit(1);
2739 if (max_cpus < smp_cpus) {
2740 fprintf(stderr, "maxcpus must be equal to or greater than "
2741 "smp\n");
2742 exit(1);
2744 if (max_cpus > 255) {
2745 fprintf(stderr, "Unsupported number of maxcpus\n");
2746 exit(1);
2748 break;
2749 case QEMU_OPTION_vnc:
2750 #ifdef CONFIG_VNC
2751 display_remote++;
2752 vnc_display = optarg;
2753 #else
2754 fprintf(stderr, "VNC support is disabled\n");
2755 exit(1);
2756 #endif
2757 break;
2758 case QEMU_OPTION_no_acpi:
2759 acpi_enabled = 0;
2760 break;
2761 case QEMU_OPTION_no_hpet:
2762 no_hpet = 1;
2763 break;
2764 case QEMU_OPTION_balloon:
2765 if (balloon_parse(optarg) < 0) {
2766 fprintf(stderr, "Unknown -balloon argument %s\n", optarg);
2767 exit(1);
2769 break;
2770 case QEMU_OPTION_no_reboot:
2771 no_reboot = 1;
2772 break;
2773 case QEMU_OPTION_no_shutdown:
2774 no_shutdown = 1;
2775 break;
2776 case QEMU_OPTION_show_cursor:
2777 cursor_hide = 0;
2778 break;
2779 case QEMU_OPTION_uuid:
2780 if(qemu_uuid_parse(optarg, qemu_uuid) < 0) {
2781 fprintf(stderr, "Fail to parse UUID string."
2782 " Wrong format.\n");
2783 exit(1);
2785 break;
2786 case QEMU_OPTION_option_rom:
2787 if (nb_option_roms >= MAX_OPTION_ROMS) {
2788 fprintf(stderr, "Too many option ROMs\n");
2789 exit(1);
2791 opts = qemu_opts_parse(qemu_find_opts("option-rom"), optarg, 1);
2792 option_rom[nb_option_roms].name = qemu_opt_get(opts, "romfile");
2793 option_rom[nb_option_roms].bootindex =
2794 qemu_opt_get_number(opts, "bootindex", -1);
2795 if (!option_rom[nb_option_roms].name) {
2796 fprintf(stderr, "Option ROM file is not specified\n");
2797 exit(1);
2799 nb_option_roms++;
2800 break;
2801 case QEMU_OPTION_semihosting:
2802 semihosting_enabled = 1;
2803 break;
2804 case QEMU_OPTION_tdf:
2805 time_drift_fix = 1;
2806 break;
2807 case QEMU_OPTION_kvm_shadow_memory:
2808 kvm_shadow_memory = (int64_t)atoi(optarg) * 1024 * 1024 / 4096;
2809 break;
2810 case QEMU_OPTION_name:
2811 qemu_name = qemu_strdup(optarg);
2813 char *p = strchr(qemu_name, ',');
2814 if (p != NULL) {
2815 *p++ = 0;
2816 if (strncmp(p, "process=", 8)) {
2817 fprintf(stderr, "Unknown subargument %s to -name\n", p);
2818 exit(1);
2820 p += 8;
2821 os_set_proc_name(p);
2824 break;
2825 case QEMU_OPTION_prom_env:
2826 if (nb_prom_envs >= MAX_PROM_ENVS) {
2827 fprintf(stderr, "Too many prom variables\n");
2828 exit(1);
2830 prom_envs[nb_prom_envs] = optarg;
2831 nb_prom_envs++;
2832 break;
2833 case QEMU_OPTION_old_param:
2834 old_param = 1;
2835 break;
2836 case QEMU_OPTION_clock:
2837 configure_alarms(optarg);
2838 break;
2839 case QEMU_OPTION_startdate:
2840 configure_rtc_date_offset(optarg, 1);
2841 break;
2842 case QEMU_OPTION_rtc:
2843 opts = qemu_opts_parse(qemu_find_opts("rtc"), optarg, 0);
2844 if (!opts) {
2845 exit(1);
2847 configure_rtc(opts);
2848 break;
2849 case QEMU_OPTION_tb_size:
2850 tb_size = strtol(optarg, NULL, 0);
2851 if (tb_size < 0)
2852 tb_size = 0;
2853 break;
2854 case QEMU_OPTION_icount:
2855 icount_option = optarg;
2856 break;
2857 case QEMU_OPTION_incoming:
2858 incoming = optarg;
2859 incoming_expected = true;
2860 break;
2861 case QEMU_OPTION_nodefaults:
2862 default_serial = 0;
2863 default_parallel = 0;
2864 default_virtcon = 0;
2865 default_monitor = 0;
2866 default_vga = 0;
2867 default_net = 0;
2868 default_floppy = 0;
2869 default_cdrom = 0;
2870 default_sdcard = 0;
2871 break;
2872 #ifndef _WIN32
2873 case QEMU_OPTION_nvram:
2874 nvram = optarg;
2875 break;
2876 #endif
2877 case QEMU_OPTION_xen_domid:
2878 if (!(xen_available())) {
2879 printf("Option %s not supported for this target\n", popt->name);
2880 exit(1);
2882 xen_domid = atoi(optarg);
2883 break;
2884 case QEMU_OPTION_xen_create:
2885 if (!(xen_available())) {
2886 printf("Option %s not supported for this target\n", popt->name);
2887 exit(1);
2889 xen_mode = XEN_CREATE;
2890 break;
2891 case QEMU_OPTION_xen_attach:
2892 if (!(xen_available())) {
2893 printf("Option %s not supported for this target\n", popt->name);
2894 exit(1);
2896 xen_mode = XEN_ATTACH;
2897 break;
2898 #ifdef CONFIG_SIMPLE_TRACE
2899 case QEMU_OPTION_trace:
2900 opts = qemu_opts_parse(qemu_find_opts("trace"), optarg, 0);
2901 if (opts) {
2902 trace_file = qemu_opt_get(opts, "file");
2904 break;
2905 #endif
2906 case QEMU_OPTION_readconfig:
2908 int ret = qemu_read_config_file(optarg);
2909 if (ret < 0) {
2910 fprintf(stderr, "read config %s: %s\n", optarg,
2911 strerror(-ret));
2912 exit(1);
2914 break;
2916 case QEMU_OPTION_spice:
2917 olist = qemu_find_opts("spice");
2918 if (!olist) {
2919 fprintf(stderr, "spice is not supported by this qemu build.\n");
2920 exit(1);
2922 opts = qemu_opts_parse(olist, optarg, 0);
2923 if (!opts) {
2924 fprintf(stderr, "parse error: %s\n", optarg);
2925 exit(1);
2927 break;
2928 case QEMU_OPTION_writeconfig:
2930 FILE *fp;
2931 if (strcmp(optarg, "-") == 0) {
2932 fp = stdout;
2933 } else {
2934 fp = fopen(optarg, "w");
2935 if (fp == NULL) {
2936 fprintf(stderr, "open %s: %s\n", optarg, strerror(errno));
2937 exit(1);
2940 qemu_config_write(fp);
2941 fclose(fp);
2942 break;
2944 default:
2945 os_parse_cmd_args(popt->index, optarg);
2949 loc_set_none();
2951 /* Open the logfile at this point, if necessary. We can't open the logfile
2952 * when encountering either of the logging options (-d or -D) because the
2953 * other one may be encountered later on the command line, changing the
2954 * location or level of logging.
2956 if (log_mask) {
2957 if (log_file) {
2958 set_cpu_log_filename(log_file);
2960 set_cpu_log(log_mask);
2963 if (!st_init(trace_file)) {
2964 fprintf(stderr, "warning: unable to initialize simple trace backend\n");
2967 /* If no data_dir is specified then try to find it relative to the
2968 executable path. */
2969 if (!data_dir) {
2970 data_dir = os_find_datadir(argv[0]);
2972 /* If all else fails use the install patch specified when building. */
2973 if (!data_dir) {
2974 data_dir = CONFIG_QEMU_DATADIR;
2978 * Default to max_cpus = smp_cpus, in case the user doesn't
2979 * specify a max_cpus value.
2981 if (!max_cpus)
2982 max_cpus = smp_cpus;
2984 machine->max_cpus = machine->max_cpus ?: 1; /* Default to UP */
2985 if (smp_cpus > machine->max_cpus) {
2986 fprintf(stderr, "Number of SMP cpus requested (%d), exceeds max cpus "
2987 "supported by machine `%s' (%d)\n", smp_cpus, machine->name,
2988 machine->max_cpus);
2989 exit(1);
2993 * Get the default machine options from the machine if it is not already
2994 * specified either by the configuration file or by the command line.
2996 if (machine->default_machine_opts) {
2997 QemuOptsList *list = qemu_find_opts("machine");
2998 const char *p = NULL;
3000 if (!QTAILQ_EMPTY(&list->head)) {
3001 p = qemu_opt_get(QTAILQ_FIRST(&list->head), "accel");
3003 if (p == NULL) {
3004 opts = qemu_opts_parse(qemu_find_opts("machine"),
3005 machine->default_machine_opts, 0);
3006 if (!opts) {
3007 fprintf(stderr, "parse error for machine %s: %s\n",
3008 machine->name, machine->default_machine_opts);
3009 exit(1);
3014 qemu_opts_foreach(qemu_find_opts("device"), default_driver_check, NULL, 0);
3015 qemu_opts_foreach(qemu_find_opts("global"), default_driver_check, NULL, 0);
3017 if (machine->no_serial) {
3018 default_serial = 0;
3020 if (machine->no_parallel) {
3021 default_parallel = 0;
3023 if (!machine->use_virtcon) {
3024 default_virtcon = 0;
3026 if (machine->no_vga) {
3027 default_vga = 0;
3029 if (machine->no_floppy) {
3030 default_floppy = 0;
3032 if (machine->no_cdrom) {
3033 default_cdrom = 0;
3035 if (machine->no_sdcard) {
3036 default_sdcard = 0;
3039 if (display_type == DT_NOGRAPHIC) {
3040 if (default_parallel)
3041 add_device_config(DEV_PARALLEL, "null");
3042 if (default_serial && default_monitor) {
3043 add_device_config(DEV_SERIAL, "mon:stdio");
3044 } else if (default_virtcon && default_monitor) {
3045 add_device_config(DEV_VIRTCON, "mon:stdio");
3046 } else {
3047 if (default_serial)
3048 add_device_config(DEV_SERIAL, "stdio");
3049 if (default_virtcon)
3050 add_device_config(DEV_VIRTCON, "stdio");
3051 if (default_monitor)
3052 monitor_parse("stdio", "readline");
3054 } else {
3055 if (default_serial)
3056 add_device_config(DEV_SERIAL, "vc:80Cx24C");
3057 if (default_parallel)
3058 add_device_config(DEV_PARALLEL, "vc:80Cx24C");
3059 if (default_monitor)
3060 monitor_parse("vc:80Cx24C", "readline");
3061 if (default_virtcon)
3062 add_device_config(DEV_VIRTCON, "vc:80Cx24C");
3064 if (default_vga)
3065 vga_interface_type = VGA_CIRRUS;
3067 socket_init();
3069 if (qemu_opts_foreach(qemu_find_opts("chardev"), chardev_init_func, NULL, 1) != 0)
3070 exit(1);
3071 #ifdef CONFIG_VIRTFS
3072 if (qemu_opts_foreach(qemu_find_opts("fsdev"), fsdev_init_func, NULL, 1) != 0) {
3073 exit(1);
3075 #endif
3077 os_daemonize();
3079 if (pid_file && qemu_create_pidfile(pid_file) != 0) {
3080 os_pidfile_error();
3081 exit(1);
3084 configure_accelerator();
3086 if (qemu_init_main_loop()) {
3087 fprintf(stderr, "qemu_init_main_loop failed\n");
3088 exit(1);
3090 linux_boot = (kernel_filename != NULL);
3092 if (!linux_boot && *kernel_cmdline != '\0') {
3093 fprintf(stderr, "-append only allowed with -kernel option\n");
3094 exit(1);
3097 if (!linux_boot && initrd_filename != NULL) {
3098 fprintf(stderr, "-initrd only allowed with -kernel option\n");
3099 exit(1);
3102 os_set_line_buffering();
3104 if (init_timer_alarm() < 0) {
3105 fprintf(stderr, "could not initialize alarm timer\n");
3106 exit(1);
3108 configure_icount(icount_option);
3110 if (net_init_clients() < 0) {
3111 exit(1);
3114 /* init the bluetooth world */
3115 if (foreach_device_config(DEV_BT, bt_parse))
3116 exit(1);
3118 /* init the memory */
3119 if (ram_size == 0)
3120 ram_size = DEFAULT_RAM_SIZE * 1024 * 1024;
3122 /* init the dynamic translator */
3123 cpu_exec_init_all(tb_size * 1024 * 1024);
3125 bdrv_init_with_whitelist();
3127 blk_mig_init();
3129 /* open the virtual block devices */
3130 if (snapshot)
3131 qemu_opts_foreach(qemu_find_opts("drive"), drive_enable_snapshot, NULL, 0);
3132 if (qemu_opts_foreach(qemu_find_opts("drive"), drive_init_func, &machine->use_scsi, 1) != 0)
3133 exit(1);
3135 default_drive(default_cdrom, snapshot, machine->use_scsi,
3136 IF_DEFAULT, 2, CDROM_OPTS);
3137 default_drive(default_floppy, snapshot, machine->use_scsi,
3138 IF_FLOPPY, 0, FD_OPTS);
3139 default_drive(default_sdcard, snapshot, machine->use_scsi,
3140 IF_SD, 0, SD_OPTS);
3142 register_savevm_live(NULL, "ram", 0, 4, NULL, ram_save_live, NULL,
3143 ram_load, NULL);
3145 if (nb_numa_nodes > 0) {
3146 int i;
3148 if (nb_numa_nodes > smp_cpus) {
3149 nb_numa_nodes = smp_cpus;
3152 /* If no memory size if given for any node, assume the default case
3153 * and distribute the available memory equally across all nodes
3155 for (i = 0; i < nb_numa_nodes; i++) {
3156 if (node_mem[i] != 0)
3157 break;
3159 if (i == nb_numa_nodes) {
3160 uint64_t usedmem = 0;
3162 /* On Linux, the each node's border has to be 8MB aligned,
3163 * the final node gets the rest.
3165 for (i = 0; i < nb_numa_nodes - 1; i++) {
3166 node_mem[i] = (ram_size / nb_numa_nodes) & ~((1 << 23UL) - 1);
3167 usedmem += node_mem[i];
3169 node_mem[i] = ram_size - usedmem;
3172 for (i = 0; i < nb_numa_nodes; i++) {
3173 if (node_cpumask[i] != 0)
3174 break;
3176 /* assigning the VCPUs round-robin is easier to implement, guest OSes
3177 * must cope with this anyway, because there are BIOSes out there in
3178 * real machines which also use this scheme.
3180 if (i == nb_numa_nodes) {
3181 for (i = 0; i < smp_cpus; i++) {
3182 node_cpumask[i % nb_numa_nodes] |= 1 << i;
3187 if (qemu_opts_foreach(qemu_find_opts("mon"), mon_init_func, NULL, 1) != 0) {
3188 exit(1);
3191 if (foreach_device_config(DEV_SERIAL, serial_parse) < 0)
3192 exit(1);
3193 if (foreach_device_config(DEV_PARALLEL, parallel_parse) < 0)
3194 exit(1);
3195 if (foreach_device_config(DEV_VIRTCON, virtcon_parse) < 0)
3196 exit(1);
3197 if (foreach_device_config(DEV_DEBUGCON, debugcon_parse) < 0)
3198 exit(1);
3200 module_call_init(MODULE_INIT_DEVICE);
3202 if (qemu_opts_foreach(qemu_find_opts("device"), device_help_func, NULL, 0) != 0)
3203 exit(0);
3205 if (watchdog) {
3206 i = select_watchdog(watchdog);
3207 if (i > 0)
3208 exit (i == 1 ? 1 : 0);
3211 if (machine->compat_props) {
3212 qdev_prop_register_global_list(machine->compat_props);
3214 qemu_add_globals();
3216 machine->init(ram_size, boot_devices,
3217 kernel_filename, kernel_cmdline, initrd_filename, cpu_model);
3219 cpu_synchronize_all_post_init();
3221 set_numa_modes();
3223 current_machine = machine;
3225 /* init USB devices */
3226 if (usb_enabled) {
3227 if (foreach_device_config(DEV_USB, usb_parse) < 0)
3228 exit(1);
3231 /* init generic devices */
3232 if (qemu_opts_foreach(qemu_find_opts("device"), device_init_func, NULL, 1) != 0)
3233 exit(1);
3235 net_check_clients();
3237 /* just use the first displaystate for the moment */
3238 ds = get_displaystate();
3240 if (using_spice)
3241 display_remote++;
3242 if (display_type == DT_DEFAULT && !display_remote) {
3243 #if defined(CONFIG_SDL) || defined(CONFIG_COCOA)
3244 display_type = DT_SDL;
3245 #elif defined(CONFIG_VNC)
3246 vnc_display = "localhost:0,to=99";
3247 show_vnc_port = 1;
3248 #else
3249 display_type = DT_NONE;
3250 #endif
3254 /* init local displays */
3255 switch (display_type) {
3256 case DT_NOGRAPHIC:
3257 break;
3258 #if defined(CONFIG_CURSES)
3259 case DT_CURSES:
3260 curses_display_init(ds, full_screen);
3261 break;
3262 #endif
3263 #if defined(CONFIG_SDL)
3264 case DT_SDL:
3265 sdl_display_init(ds, full_screen, no_frame);
3266 break;
3267 #elif defined(CONFIG_COCOA)
3268 case DT_SDL:
3269 cocoa_display_init(ds, full_screen);
3270 break;
3271 #endif
3272 default:
3273 break;
3276 /* must be after terminal init, SDL library changes signal handlers */
3277 os_setup_signal_handling();
3279 #ifdef CONFIG_VNC
3280 /* init remote displays */
3281 if (vnc_display) {
3282 vnc_display_init(ds);
3283 if (vnc_display_open(ds, vnc_display) < 0)
3284 exit(1);
3286 if (show_vnc_port) {
3287 printf("VNC server running on `%s'\n", vnc_display_local_addr(ds));
3290 #endif
3291 #ifdef CONFIG_SPICE
3292 if (using_spice && !qxl_enabled) {
3293 qemu_spice_display_init(ds);
3295 #endif
3297 /* display setup */
3298 dpy_resize(ds);
3299 dcl = ds->listeners;
3300 while (dcl != NULL) {
3301 if (dcl->dpy_refresh != NULL) {
3302 ds->gui_timer = qemu_new_timer_ms(rt_clock, gui_update, ds);
3303 qemu_mod_timer(ds->gui_timer, qemu_get_clock_ms(rt_clock));
3304 break;
3306 dcl = dcl->next;
3308 if (ds->gui_timer == NULL) {
3309 nographic_timer = qemu_new_timer_ms(rt_clock, nographic_update, NULL);
3310 qemu_mod_timer(nographic_timer, qemu_get_clock_ms(rt_clock));
3312 text_consoles_set_display(ds);
3314 if (gdbstub_dev && gdbserver_start(gdbstub_dev) < 0) {
3315 fprintf(stderr, "qemu: could not open gdbserver on device '%s'\n",
3316 gdbstub_dev);
3317 exit(1);
3320 qdev_machine_creation_done();
3322 if (rom_load_all() != 0) {
3323 fprintf(stderr, "rom loading failed\n");
3324 exit(1);
3327 /* TODO: once all bus devices are qdevified, this should be done
3328 * when bus is created by qdev.c */
3329 qemu_register_reset(qbus_reset_all_fn, sysbus_get_default());
3330 qemu_run_machine_init_done_notifiers();
3332 qemu_system_reset(VMRESET_SILENT);
3333 if (loadvm) {
3334 if (load_vmstate(loadvm) < 0) {
3335 autostart = 0;
3339 if (incoming) {
3340 int ret = qemu_start_incoming_migration(incoming);
3341 if (ret < 0) {
3342 fprintf(stderr, "Migration failed. Exit code %s(%d), exiting.\n",
3343 incoming, ret);
3344 exit(ret);
3346 } else if (autostart) {
3347 vm_start();
3350 os_setup_post();
3352 main_loop();
3353 quit_timers();
3354 net_cleanup();
3356 return 0;