sun4m: avoid structure holes spotted by pahole
[qemu/mdroth.git] / vl.c
blob06a6f807a497a98e28344732386039eac13a2bf3
1 /*
2 * QEMU System Emulator
4 * Copyright (c) 2003-2008 Fabrice Bellard
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 * THE SOFTWARE.
24 #include <unistd.h>
25 #include <fcntl.h>
26 #include <signal.h>
27 #include <time.h>
28 #include <errno.h>
29 #include <sys/time.h>
30 #include <zlib.h>
32 /* Needed early for CONFIG_BSD etc. */
33 #include "config-host.h"
35 #ifndef _WIN32
36 #include <libgen.h>
37 #include <sys/times.h>
38 #include <sys/wait.h>
39 #include <termios.h>
40 #include <sys/mman.h>
41 #include <sys/ioctl.h>
42 #include <sys/resource.h>
43 #include <sys/socket.h>
44 #include <netinet/in.h>
45 #include <net/if.h>
46 #include <arpa/inet.h>
47 #include <dirent.h>
48 #include <netdb.h>
49 #include <sys/select.h>
51 #ifdef CONFIG_BSD
52 #include <sys/stat.h>
53 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__DragonFly__)
54 #include <libutil.h>
55 #include <sys/sysctl.h>
56 #else
57 #include <util.h>
58 #endif
59 #else
60 #ifdef __linux__
61 #include <pty.h>
62 #include <malloc.h>
64 #include <linux/ppdev.h>
65 #include <linux/parport.h>
66 #endif
67 #ifdef __sun__
68 #include <sys/stat.h>
69 #include <sys/ethernet.h>
70 #include <sys/sockio.h>
71 #include <netinet/arp.h>
72 #include <netinet/in_systm.h>
73 #include <netinet/ip.h>
74 #include <netinet/ip_icmp.h> // must come after ip.h
75 #include <netinet/udp.h>
76 #include <netinet/tcp.h>
77 #include <net/if.h>
78 #include <syslog.h>
79 #include <stropts.h>
80 #endif
81 #endif
82 #endif
84 #if defined(__OpenBSD__)
85 #include <util.h>
86 #endif
88 #if defined(CONFIG_VDE)
89 #include <libvdeplug.h>
90 #endif
92 #ifdef _WIN32
93 #include <windows.h>
94 #endif
96 #ifdef CONFIG_SDL
97 #if defined(__APPLE__) || defined(main)
98 #include <SDL.h>
99 int qemu_main(int argc, char **argv, char **envp);
100 int main(int argc, char **argv)
102 return qemu_main(argc, argv, NULL);
104 #undef main
105 #define main qemu_main
106 #endif
107 #endif /* CONFIG_SDL */
109 #ifdef CONFIG_COCOA
110 #undef main
111 #define main qemu_main
112 #endif /* CONFIG_COCOA */
114 #include "hw/hw.h"
115 #include "hw/boards.h"
116 #include "hw/usb.h"
117 #include "hw/pcmcia.h"
118 #include "hw/pc.h"
119 #include "hw/isa.h"
120 #include "hw/baum.h"
121 #include "hw/bt.h"
122 #include "hw/watchdog.h"
123 #include "hw/smbios.h"
124 #include "hw/xen.h"
125 #include "hw/qdev.h"
126 #include "hw/loader.h"
127 #include "bt-host.h"
128 #include "net.h"
129 #include "net/slirp.h"
130 #include "monitor.h"
131 #include "console.h"
132 #include "sysemu.h"
133 #include "gdbstub.h"
134 #include "qemu-timer.h"
135 #include "qemu-char.h"
136 #include "cache-utils.h"
137 #include "block.h"
138 #include "blockdev.h"
139 #include "block-migration.h"
140 #include "dma.h"
141 #include "audio/audio.h"
142 #include "migration.h"
143 #include "kvm.h"
144 #include "qemu-option.h"
145 #include "qemu-config.h"
146 #include "qemu-objects.h"
147 #include "qemu-options.h"
148 #ifdef CONFIG_VIRTFS
149 #include "fsdev/qemu-fsdev.h"
150 #endif
152 #include "disas.h"
154 #include "qemu_socket.h"
156 #include "slirp/libslirp.h"
158 #include "trace.h"
159 #include "simpletrace.h"
160 #include "qemu-queue.h"
161 #include "cpus.h"
162 #include "arch_init.h"
164 #include "ui/qemu-spice.h"
166 //#define DEBUG_NET
167 //#define DEBUG_SLIRP
169 #define DEFAULT_RAM_SIZE 128
171 #define MAX_VIRTIO_CONSOLES 1
173 static const char *data_dir;
174 const char *bios_name = NULL;
175 enum vga_retrace_method vga_retrace_method = VGA_RETRACE_DUMB;
176 DisplayType display_type = DT_DEFAULT;
177 int display_remote = 0;
178 const char* keyboard_layout = NULL;
179 ram_addr_t ram_size;
180 const char *mem_path = NULL;
181 #ifdef MAP_POPULATE
182 int mem_prealloc = 0; /* force preallocation of physical target memory */
183 #endif
184 int nb_nics;
185 NICInfo nd_table[MAX_NICS];
186 int vm_running;
187 int autostart;
188 int incoming_expected; /* Started with -incoming and waiting for incoming */
189 static int rtc_utc = 1;
190 static int rtc_date_offset = -1; /* -1 means no change */
191 QEMUClock *rtc_clock;
192 int vga_interface_type = VGA_NONE;
193 static int full_screen = 0;
194 #ifdef CONFIG_SDL
195 static int no_frame = 0;
196 #endif
197 int no_quit = 0;
198 CharDriverState *serial_hds[MAX_SERIAL_PORTS];
199 CharDriverState *parallel_hds[MAX_PARALLEL_PORTS];
200 CharDriverState *virtcon_hds[MAX_VIRTIO_CONSOLES];
201 int win2k_install_hack = 0;
202 int rtc_td_hack = 0;
203 int usb_enabled = 0;
204 int singlestep = 0;
205 int smp_cpus = 1;
206 int max_cpus = 0;
207 int smp_cores = 1;
208 int smp_threads = 1;
209 #ifdef CONFIG_VNC
210 const char *vnc_display;
211 #endif
212 int acpi_enabled = 1;
213 int no_hpet = 0;
214 int fd_bootchk = 1;
215 int no_reboot = 0;
216 int no_shutdown = 0;
217 int cursor_hide = 1;
218 int graphic_rotate = 0;
219 uint8_t irq0override = 1;
220 const char *watchdog;
221 QEMUOptionRom option_rom[MAX_OPTION_ROMS];
222 int nb_option_roms;
223 int semihosting_enabled = 0;
224 int old_param = 0;
225 const char *qemu_name;
226 int alt_grab = 0;
227 int ctrl_grab = 0;
228 unsigned int nb_prom_envs = 0;
229 const char *prom_envs[MAX_PROM_ENVS];
230 int boot_menu;
231 uint8_t *boot_splash_filedata;
232 int boot_splash_filedata_size;
233 uint8_t qemu_extra_params_fw[2];
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;
268 static int tcg_tb_size;
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-cd", .flag = &default_cdrom },
287 { .driver = "ide-hd", .flag = &default_cdrom },
288 { .driver = "ide-drive", .flag = &default_cdrom },
289 { .driver = "scsi-cd", .flag = &default_cdrom },
290 { .driver = "virtio-serial-pci", .flag = &default_virtcon },
291 { .driver = "virtio-serial-s390", .flag = &default_virtcon },
292 { .driver = "virtio-serial", .flag = &default_virtcon },
293 { .driver = "VGA", .flag = &default_vga },
294 { .driver = "cirrus-vga", .flag = &default_vga },
295 { .driver = "vmware-svga", .flag = &default_vga },
296 { .driver = "isa-vga", .flag = &default_vga },
297 { .driver = "qxl-vga", .flag = &default_vga },
300 static void res_free(void)
302 if (boot_splash_filedata != NULL) {
303 g_free(boot_splash_filedata);
304 boot_splash_filedata = NULL;
308 static int default_driver_check(QemuOpts *opts, void *opaque)
310 const char *driver = qemu_opt_get(opts, "driver");
311 int i;
313 if (!driver)
314 return 0;
315 for (i = 0; i < ARRAY_SIZE(default_list); i++) {
316 if (strcmp(default_list[i].driver, driver) != 0)
317 continue;
318 *(default_list[i].flag) = 0;
320 return 0;
323 /***********************************************************/
324 /* real time host monotonic timer */
326 /***********************************************************/
327 /* host time/date access */
328 void qemu_get_timedate(struct tm *tm, int offset)
330 time_t ti;
331 struct tm *ret;
333 time(&ti);
334 ti += offset;
335 if (rtc_date_offset == -1) {
336 if (rtc_utc)
337 ret = gmtime(&ti);
338 else
339 ret = localtime(&ti);
340 } else {
341 ti -= rtc_date_offset;
342 ret = gmtime(&ti);
345 memcpy(tm, ret, sizeof(struct tm));
348 int qemu_timedate_diff(struct tm *tm)
350 time_t seconds;
352 if (rtc_date_offset == -1)
353 if (rtc_utc)
354 seconds = mktimegm(tm);
355 else
356 seconds = mktime(tm);
357 else
358 seconds = mktimegm(tm) + rtc_date_offset;
360 return seconds - time(NULL);
363 void rtc_change_mon_event(struct tm *tm)
365 QObject *data;
367 data = qobject_from_jsonf("{ 'offset': %d }", qemu_timedate_diff(tm));
368 monitor_protocol_event(QEVENT_RTC_CHANGE, data);
369 qobject_decref(data);
372 static void configure_rtc_date_offset(const char *startdate, int legacy)
374 time_t rtc_start_date;
375 struct tm tm;
377 if (!strcmp(startdate, "now") && legacy) {
378 rtc_date_offset = -1;
379 } else {
380 if (sscanf(startdate, "%d-%d-%dT%d:%d:%d",
381 &tm.tm_year,
382 &tm.tm_mon,
383 &tm.tm_mday,
384 &tm.tm_hour,
385 &tm.tm_min,
386 &tm.tm_sec) == 6) {
387 /* OK */
388 } else if (sscanf(startdate, "%d-%d-%d",
389 &tm.tm_year,
390 &tm.tm_mon,
391 &tm.tm_mday) == 3) {
392 tm.tm_hour = 0;
393 tm.tm_min = 0;
394 tm.tm_sec = 0;
395 } else {
396 goto date_fail;
398 tm.tm_year -= 1900;
399 tm.tm_mon--;
400 rtc_start_date = mktimegm(&tm);
401 if (rtc_start_date == -1) {
402 date_fail:
403 fprintf(stderr, "Invalid date format. Valid formats are:\n"
404 "'2006-06-17T16:01:21' or '2006-06-17'\n");
405 exit(1);
407 rtc_date_offset = time(NULL) - rtc_start_date;
411 static void configure_rtc(QemuOpts *opts)
413 const char *value;
415 value = qemu_opt_get(opts, "base");
416 if (value) {
417 if (!strcmp(value, "utc")) {
418 rtc_utc = 1;
419 } else if (!strcmp(value, "localtime")) {
420 rtc_utc = 0;
421 } else {
422 configure_rtc_date_offset(value, 0);
425 value = qemu_opt_get(opts, "clock");
426 if (value) {
427 if (!strcmp(value, "host")) {
428 rtc_clock = host_clock;
429 } else if (!strcmp(value, "vm")) {
430 rtc_clock = vm_clock;
431 } else {
432 fprintf(stderr, "qemu: invalid option value '%s'\n", value);
433 exit(1);
436 value = qemu_opt_get(opts, "driftfix");
437 if (value) {
438 if (!strcmp(value, "slew")) {
439 rtc_td_hack = 1;
440 } else if (!strcmp(value, "none")) {
441 rtc_td_hack = 0;
442 } else {
443 fprintf(stderr, "qemu: invalid option value '%s'\n", value);
444 exit(1);
449 /***********************************************************/
450 /* Bluetooth support */
451 static int nb_hcis;
452 static int cur_hci;
453 static struct HCIInfo *hci_table[MAX_NICS];
455 static struct bt_vlan_s {
456 struct bt_scatternet_s net;
457 int id;
458 struct bt_vlan_s *next;
459 } *first_bt_vlan;
461 /* find or alloc a new bluetooth "VLAN" */
462 static struct bt_scatternet_s *qemu_find_bt_vlan(int id)
464 struct bt_vlan_s **pvlan, *vlan;
465 for (vlan = first_bt_vlan; vlan != NULL; vlan = vlan->next) {
466 if (vlan->id == id)
467 return &vlan->net;
469 vlan = g_malloc0(sizeof(struct bt_vlan_s));
470 vlan->id = id;
471 pvlan = &first_bt_vlan;
472 while (*pvlan != NULL)
473 pvlan = &(*pvlan)->next;
474 *pvlan = vlan;
475 return &vlan->net;
478 static void null_hci_send(struct HCIInfo *hci, const uint8_t *data, int len)
482 static int null_hci_addr_set(struct HCIInfo *hci, const uint8_t *bd_addr)
484 return -ENOTSUP;
487 static struct HCIInfo null_hci = {
488 .cmd_send = null_hci_send,
489 .sco_send = null_hci_send,
490 .acl_send = null_hci_send,
491 .bdaddr_set = null_hci_addr_set,
494 struct HCIInfo *qemu_next_hci(void)
496 if (cur_hci == nb_hcis)
497 return &null_hci;
499 return hci_table[cur_hci++];
502 static struct HCIInfo *hci_init(const char *str)
504 char *endp;
505 struct bt_scatternet_s *vlan = 0;
507 if (!strcmp(str, "null"))
508 /* null */
509 return &null_hci;
510 else if (!strncmp(str, "host", 4) && (str[4] == '\0' || str[4] == ':'))
511 /* host[:hciN] */
512 return bt_host_hci(str[4] ? str + 5 : "hci0");
513 else if (!strncmp(str, "hci", 3)) {
514 /* hci[,vlan=n] */
515 if (str[3]) {
516 if (!strncmp(str + 3, ",vlan=", 6)) {
517 vlan = qemu_find_bt_vlan(strtol(str + 9, &endp, 0));
518 if (*endp)
519 vlan = 0;
521 } else
522 vlan = qemu_find_bt_vlan(0);
523 if (vlan)
524 return bt_new_hci(vlan);
527 fprintf(stderr, "qemu: Unknown bluetooth HCI `%s'.\n", str);
529 return 0;
532 static int bt_hci_parse(const char *str)
534 struct HCIInfo *hci;
535 bdaddr_t bdaddr;
537 if (nb_hcis >= MAX_NICS) {
538 fprintf(stderr, "qemu: Too many bluetooth HCIs (max %i).\n", MAX_NICS);
539 return -1;
542 hci = hci_init(str);
543 if (!hci)
544 return -1;
546 bdaddr.b[0] = 0x52;
547 bdaddr.b[1] = 0x54;
548 bdaddr.b[2] = 0x00;
549 bdaddr.b[3] = 0x12;
550 bdaddr.b[4] = 0x34;
551 bdaddr.b[5] = 0x56 + nb_hcis;
552 hci->bdaddr_set(hci, bdaddr.b);
554 hci_table[nb_hcis++] = hci;
556 return 0;
559 static void bt_vhci_add(int vlan_id)
561 struct bt_scatternet_s *vlan = qemu_find_bt_vlan(vlan_id);
563 if (!vlan->slave)
564 fprintf(stderr, "qemu: warning: adding a VHCI to "
565 "an empty scatternet %i\n", vlan_id);
567 bt_vhci_init(bt_new_hci(vlan));
570 static struct bt_device_s *bt_device_add(const char *opt)
572 struct bt_scatternet_s *vlan;
573 int vlan_id = 0;
574 char *endp = strstr(opt, ",vlan=");
575 int len = (endp ? endp - opt : strlen(opt)) + 1;
576 char devname[10];
578 pstrcpy(devname, MIN(sizeof(devname), len), opt);
580 if (endp) {
581 vlan_id = strtol(endp + 6, &endp, 0);
582 if (*endp) {
583 fprintf(stderr, "qemu: unrecognised bluetooth vlan Id\n");
584 return 0;
588 vlan = qemu_find_bt_vlan(vlan_id);
590 if (!vlan->slave)
591 fprintf(stderr, "qemu: warning: adding a slave device to "
592 "an empty scatternet %i\n", vlan_id);
594 if (!strcmp(devname, "keyboard"))
595 return bt_keyboard_init(vlan);
597 fprintf(stderr, "qemu: unsupported bluetooth device `%s'\n", devname);
598 return 0;
601 static int bt_parse(const char *opt)
603 const char *endp, *p;
604 int vlan;
606 if (strstart(opt, "hci", &endp)) {
607 if (!*endp || *endp == ',') {
608 if (*endp)
609 if (!strstart(endp, ",vlan=", 0))
610 opt = endp + 1;
612 return bt_hci_parse(opt);
614 } else if (strstart(opt, "vhci", &endp)) {
615 if (!*endp || *endp == ',') {
616 if (*endp) {
617 if (strstart(endp, ",vlan=", &p)) {
618 vlan = strtol(p, (char **) &endp, 0);
619 if (*endp) {
620 fprintf(stderr, "qemu: bad scatternet '%s'\n", p);
621 return 1;
623 } else {
624 fprintf(stderr, "qemu: bad parameter '%s'\n", endp + 1);
625 return 1;
627 } else
628 vlan = 0;
630 bt_vhci_add(vlan);
631 return 0;
633 } else if (strstart(opt, "device:", &endp))
634 return !bt_device_add(endp);
636 fprintf(stderr, "qemu: bad bluetooth parameter '%s'\n", opt);
637 return 1;
640 /***********************************************************/
641 /* QEMU Block devices */
643 #define HD_OPTS "media=disk"
644 #define CDROM_OPTS "media=cdrom"
645 #define FD_OPTS ""
646 #define PFLASH_OPTS ""
647 #define MTD_OPTS ""
648 #define SD_OPTS ""
650 static int drive_init_func(QemuOpts *opts, void *opaque)
652 int *use_scsi = opaque;
654 return drive_init(opts, *use_scsi) == NULL;
657 static int drive_enable_snapshot(QemuOpts *opts, void *opaque)
659 if (NULL == qemu_opt_get(opts, "snapshot")) {
660 qemu_opt_set(opts, "snapshot", "on");
662 return 0;
665 static void default_drive(int enable, int snapshot, int use_scsi,
666 BlockInterfaceType type, int index,
667 const char *optstr)
669 QemuOpts *opts;
671 if (type == IF_DEFAULT) {
672 type = use_scsi ? IF_SCSI : IF_IDE;
675 if (!enable || drive_get_by_index(type, index)) {
676 return;
679 opts = drive_add(type, index, NULL, optstr);
680 if (snapshot) {
681 drive_enable_snapshot(opts, NULL);
683 if (!drive_init(opts, use_scsi)) {
684 exit(1);
688 void qemu_register_boot_set(QEMUBootSetHandler *func, void *opaque)
690 boot_set_handler = func;
691 boot_set_opaque = opaque;
694 int qemu_boot_set(const char *boot_devices)
696 if (!boot_set_handler) {
697 return -EINVAL;
699 return boot_set_handler(boot_set_opaque, boot_devices);
702 static void validate_bootdevices(char *devices)
704 /* We just do some generic consistency checks */
705 const char *p;
706 int bitmap = 0;
708 for (p = devices; *p != '\0'; p++) {
709 /* Allowed boot devices are:
710 * a-b: floppy disk drives
711 * c-f: IDE disk drives
712 * g-m: machine implementation dependant drives
713 * n-p: network devices
714 * It's up to each machine implementation to check if the given boot
715 * devices match the actual hardware implementation and firmware
716 * features.
718 if (*p < 'a' || *p > 'p') {
719 fprintf(stderr, "Invalid boot device '%c'\n", *p);
720 exit(1);
722 if (bitmap & (1 << (*p - 'a'))) {
723 fprintf(stderr, "Boot device '%c' was given twice\n", *p);
724 exit(1);
726 bitmap |= 1 << (*p - 'a');
730 static void restore_boot_devices(void *opaque)
732 char *standard_boot_devices = opaque;
733 static int first = 1;
735 /* Restore boot order and remove ourselves after the first boot */
736 if (first) {
737 first = 0;
738 return;
741 qemu_boot_set(standard_boot_devices);
743 qemu_unregister_reset(restore_boot_devices, standard_boot_devices);
744 g_free(standard_boot_devices);
747 void add_boot_device_path(int32_t bootindex, DeviceState *dev,
748 const char *suffix)
750 FWBootEntry *node, *i;
752 if (bootindex < 0) {
753 return;
756 assert(dev != NULL || suffix != NULL);
758 node = g_malloc0(sizeof(FWBootEntry));
759 node->bootindex = bootindex;
760 node->suffix = suffix ? g_strdup(suffix) : NULL;
761 node->dev = dev;
763 QTAILQ_FOREACH(i, &fw_boot_order, link) {
764 if (i->bootindex == bootindex) {
765 fprintf(stderr, "Two devices with same boot index %d\n", bootindex);
766 exit(1);
767 } else if (i->bootindex < bootindex) {
768 continue;
770 QTAILQ_INSERT_BEFORE(i, node, link);
771 return;
773 QTAILQ_INSERT_TAIL(&fw_boot_order, node, link);
777 * This function returns null terminated string that consist of new line
778 * separated device paths.
780 * memory pointed by "size" is assigned total length of the array in bytes
783 char *get_boot_devices_list(uint32_t *size)
785 FWBootEntry *i;
786 uint32_t total = 0;
787 char *list = NULL;
789 QTAILQ_FOREACH(i, &fw_boot_order, link) {
790 char *devpath = NULL, *bootpath;
791 int len;
793 if (i->dev) {
794 devpath = qdev_get_fw_dev_path(i->dev);
795 assert(devpath);
798 if (i->suffix && devpath) {
799 size_t bootpathlen = strlen(devpath) + strlen(i->suffix) + 1;
801 bootpath = g_malloc(bootpathlen);
802 snprintf(bootpath, bootpathlen, "%s%s", devpath, i->suffix);
803 g_free(devpath);
804 } else if (devpath) {
805 bootpath = devpath;
806 } else {
807 bootpath = g_strdup(i->suffix);
808 assert(bootpath);
811 if (total) {
812 list[total-1] = '\n';
814 len = strlen(bootpath) + 1;
815 list = g_realloc(list, total + len);
816 memcpy(&list[total], bootpath, len);
817 total += len;
818 g_free(bootpath);
821 *size = total;
823 return list;
826 static void numa_add(const char *optarg)
828 char option[128];
829 char *endptr;
830 unsigned long long value, endvalue;
831 int nodenr;
833 optarg = get_opt_name(option, 128, optarg, ',') + 1;
834 if (!strcmp(option, "node")) {
835 if (get_param_value(option, 128, "nodeid", optarg) == 0) {
836 nodenr = nb_numa_nodes;
837 } else {
838 nodenr = strtoull(option, NULL, 10);
841 if (get_param_value(option, 128, "mem", optarg) == 0) {
842 node_mem[nodenr] = 0;
843 } else {
844 int64_t sval;
845 sval = strtosz(option, NULL);
846 if (sval < 0) {
847 fprintf(stderr, "qemu: invalid numa mem size: %s\n", optarg);
848 exit(1);
850 node_mem[nodenr] = sval;
852 if (get_param_value(option, 128, "cpus", optarg) == 0) {
853 node_cpumask[nodenr] = 0;
854 } else {
855 value = strtoull(option, &endptr, 10);
856 if (value >= 64) {
857 value = 63;
858 fprintf(stderr, "only 64 CPUs in NUMA mode supported.\n");
859 } else {
860 if (*endptr == '-') {
861 endvalue = strtoull(endptr+1, &endptr, 10);
862 if (endvalue >= 63) {
863 endvalue = 62;
864 fprintf(stderr,
865 "only 63 CPUs in NUMA mode supported.\n");
867 value = (2ULL << endvalue) - (1ULL << value);
868 } else {
869 value = 1ULL << value;
872 node_cpumask[nodenr] = value;
874 nb_numa_nodes++;
876 return;
879 static void smp_parse(const char *optarg)
881 int smp, sockets = 0, threads = 0, cores = 0;
882 char *endptr;
883 char option[128];
885 smp = strtoul(optarg, &endptr, 10);
886 if (endptr != optarg) {
887 if (*endptr == ',') {
888 endptr++;
891 if (get_param_value(option, 128, "sockets", endptr) != 0)
892 sockets = strtoull(option, NULL, 10);
893 if (get_param_value(option, 128, "cores", endptr) != 0)
894 cores = strtoull(option, NULL, 10);
895 if (get_param_value(option, 128, "threads", endptr) != 0)
896 threads = strtoull(option, NULL, 10);
897 if (get_param_value(option, 128, "maxcpus", endptr) != 0)
898 max_cpus = strtoull(option, NULL, 10);
900 /* compute missing values, prefer sockets over cores over threads */
901 if (smp == 0 || sockets == 0) {
902 sockets = sockets > 0 ? sockets : 1;
903 cores = cores > 0 ? cores : 1;
904 threads = threads > 0 ? threads : 1;
905 if (smp == 0) {
906 smp = cores * threads * sockets;
908 } else {
909 if (cores == 0) {
910 threads = threads > 0 ? threads : 1;
911 cores = smp / (sockets * threads);
912 } else {
913 threads = smp / (cores * sockets);
916 smp_cpus = smp;
917 smp_cores = cores > 0 ? cores : 1;
918 smp_threads = threads > 0 ? threads : 1;
919 if (max_cpus == 0)
920 max_cpus = smp_cpus;
923 /***********************************************************/
924 /* USB devices */
926 static int usb_device_add(const char *devname)
928 const char *p;
929 USBDevice *dev = NULL;
931 if (!usb_enabled)
932 return -1;
934 /* drivers with .usbdevice_name entry in USBDeviceInfo */
935 dev = usbdevice_create(devname);
936 if (dev)
937 goto done;
939 /* the other ones */
940 #ifndef CONFIG_LINUX
941 /* only the linux version is qdev-ified, usb-bsd still needs this */
942 if (strstart(devname, "host:", &p)) {
943 dev = usb_host_device_open(p);
944 } else
945 #endif
946 if (!strcmp(devname, "bt") || strstart(devname, "bt:", &p)) {
947 dev = usb_bt_init(devname[2] ? hci_init(p) :
948 bt_new_hci(qemu_find_bt_vlan(0)));
949 } else {
950 return -1;
952 if (!dev)
953 return -1;
955 done:
956 return 0;
959 static int usb_device_del(const char *devname)
961 int bus_num, addr;
962 const char *p;
964 if (strstart(devname, "host:", &p))
965 return usb_host_device_close(p);
967 if (!usb_enabled)
968 return -1;
970 p = strchr(devname, '.');
971 if (!p)
972 return -1;
973 bus_num = strtoul(devname, NULL, 0);
974 addr = strtoul(p + 1, NULL, 0);
976 return usb_device_delete_addr(bus_num, addr);
979 static int usb_parse(const char *cmdline)
981 int r;
982 r = usb_device_add(cmdline);
983 if (r < 0) {
984 fprintf(stderr, "qemu: could not add USB device '%s'\n", cmdline);
986 return r;
989 void do_usb_add(Monitor *mon, const QDict *qdict)
991 const char *devname = qdict_get_str(qdict, "devname");
992 if (usb_device_add(devname) < 0) {
993 error_report("could not add USB device '%s'", devname);
997 void do_usb_del(Monitor *mon, const QDict *qdict)
999 const char *devname = qdict_get_str(qdict, "devname");
1000 if (usb_device_del(devname) < 0) {
1001 error_report("could not delete USB device '%s'", devname);
1005 /***********************************************************/
1006 /* PCMCIA/Cardbus */
1008 static struct pcmcia_socket_entry_s {
1009 PCMCIASocket *socket;
1010 struct pcmcia_socket_entry_s *next;
1011 } *pcmcia_sockets = 0;
1013 void pcmcia_socket_register(PCMCIASocket *socket)
1015 struct pcmcia_socket_entry_s *entry;
1017 entry = g_malloc(sizeof(struct pcmcia_socket_entry_s));
1018 entry->socket = socket;
1019 entry->next = pcmcia_sockets;
1020 pcmcia_sockets = entry;
1023 void pcmcia_socket_unregister(PCMCIASocket *socket)
1025 struct pcmcia_socket_entry_s *entry, **ptr;
1027 ptr = &pcmcia_sockets;
1028 for (entry = *ptr; entry; ptr = &entry->next, entry = *ptr)
1029 if (entry->socket == socket) {
1030 *ptr = entry->next;
1031 g_free(entry);
1035 void pcmcia_info(Monitor *mon)
1037 struct pcmcia_socket_entry_s *iter;
1039 if (!pcmcia_sockets)
1040 monitor_printf(mon, "No PCMCIA sockets\n");
1042 for (iter = pcmcia_sockets; iter; iter = iter->next)
1043 monitor_printf(mon, "%s: %s\n", iter->socket->slot_string,
1044 iter->socket->attached ? iter->socket->card_string :
1045 "Empty");
1048 /***********************************************************/
1049 /* machine registration */
1051 static QEMUMachine *first_machine = NULL;
1052 QEMUMachine *current_machine = NULL;
1054 int qemu_register_machine(QEMUMachine *m)
1056 QEMUMachine **pm;
1057 pm = &first_machine;
1058 while (*pm != NULL)
1059 pm = &(*pm)->next;
1060 m->next = NULL;
1061 *pm = m;
1062 return 0;
1065 static QEMUMachine *find_machine(const char *name)
1067 QEMUMachine *m;
1069 for(m = first_machine; m != NULL; m = m->next) {
1070 if (!strcmp(m->name, name))
1071 return m;
1072 if (m->alias && !strcmp(m->alias, name))
1073 return m;
1075 return NULL;
1078 static QEMUMachine *find_default_machine(void)
1080 QEMUMachine *m;
1082 for(m = first_machine; m != NULL; m = m->next) {
1083 if (m->is_default) {
1084 return m;
1087 return NULL;
1090 /***********************************************************/
1091 /* main execution loop */
1093 static void gui_update(void *opaque)
1095 uint64_t interval = GUI_REFRESH_INTERVAL;
1096 DisplayState *ds = opaque;
1097 DisplayChangeListener *dcl = ds->listeners;
1099 qemu_flush_coalesced_mmio_buffer();
1100 dpy_refresh(ds);
1102 while (dcl != NULL) {
1103 if (dcl->gui_timer_interval &&
1104 dcl->gui_timer_interval < interval)
1105 interval = dcl->gui_timer_interval;
1106 dcl = dcl->next;
1108 qemu_mod_timer(ds->gui_timer, interval + qemu_get_clock_ms(rt_clock));
1111 static void nographic_update(void *opaque)
1113 uint64_t interval = GUI_REFRESH_INTERVAL;
1115 qemu_flush_coalesced_mmio_buffer();
1116 qemu_mod_timer(nographic_timer, interval + qemu_get_clock_ms(rt_clock));
1119 struct vm_change_state_entry {
1120 VMChangeStateHandler *cb;
1121 void *opaque;
1122 QLIST_ENTRY (vm_change_state_entry) entries;
1125 static QLIST_HEAD(vm_change_state_head, vm_change_state_entry) vm_change_state_head;
1127 VMChangeStateEntry *qemu_add_vm_change_state_handler(VMChangeStateHandler *cb,
1128 void *opaque)
1130 VMChangeStateEntry *e;
1132 e = g_malloc0(sizeof (*e));
1134 e->cb = cb;
1135 e->opaque = opaque;
1136 QLIST_INSERT_HEAD(&vm_change_state_head, e, entries);
1137 return e;
1140 void qemu_del_vm_change_state_handler(VMChangeStateEntry *e)
1142 QLIST_REMOVE (e, entries);
1143 g_free (e);
1146 void vm_state_notify(int running, int reason)
1148 VMChangeStateEntry *e;
1150 trace_vm_state_notify(running, reason);
1152 for (e = vm_change_state_head.lh_first; e; e = e->entries.le_next) {
1153 e->cb(e->opaque, running, reason);
1157 void vm_start(void)
1159 if (!vm_running) {
1160 cpu_enable_ticks();
1161 vm_running = 1;
1162 vm_state_notify(1, 0);
1163 resume_all_vcpus();
1164 monitor_protocol_event(QEVENT_RESUME, NULL);
1168 /* reset/shutdown handler */
1170 typedef struct QEMUResetEntry {
1171 QTAILQ_ENTRY(QEMUResetEntry) entry;
1172 QEMUResetHandler *func;
1173 void *opaque;
1174 } QEMUResetEntry;
1176 static QTAILQ_HEAD(reset_handlers, QEMUResetEntry) reset_handlers =
1177 QTAILQ_HEAD_INITIALIZER(reset_handlers);
1178 static int reset_requested;
1179 static int shutdown_requested, shutdown_signal = -1;
1180 static pid_t shutdown_pid;
1181 static int powerdown_requested;
1182 static int debug_requested;
1183 static int vmstop_requested;
1185 int qemu_shutdown_requested_get(void)
1187 return shutdown_requested;
1190 int qemu_reset_requested_get(void)
1192 return reset_requested;
1195 int qemu_shutdown_requested(void)
1197 int r = shutdown_requested;
1198 shutdown_requested = 0;
1199 return r;
1202 void qemu_kill_report(void)
1204 if (shutdown_signal != -1) {
1205 fprintf(stderr, "qemu: terminating on signal %d", shutdown_signal);
1206 if (shutdown_pid == 0) {
1207 /* This happens for eg ^C at the terminal, so it's worth
1208 * avoiding printing an odd message in that case.
1210 fputc('\n', stderr);
1211 } else {
1212 fprintf(stderr, " from pid " FMT_pid "\n", shutdown_pid);
1214 shutdown_signal = -1;
1218 int qemu_reset_requested(void)
1220 int r = reset_requested;
1221 reset_requested = 0;
1222 return r;
1225 int qemu_powerdown_requested(void)
1227 int r = powerdown_requested;
1228 powerdown_requested = 0;
1229 return r;
1232 static int qemu_debug_requested(void)
1234 int r = debug_requested;
1235 debug_requested = 0;
1236 return r;
1239 static int qemu_vmstop_requested(void)
1241 int r = vmstop_requested;
1242 vmstop_requested = 0;
1243 return r;
1246 void qemu_register_reset(QEMUResetHandler *func, void *opaque)
1248 QEMUResetEntry *re = g_malloc0(sizeof(QEMUResetEntry));
1250 re->func = func;
1251 re->opaque = opaque;
1252 QTAILQ_INSERT_TAIL(&reset_handlers, re, entry);
1255 void qemu_unregister_reset(QEMUResetHandler *func, void *opaque)
1257 QEMUResetEntry *re;
1259 QTAILQ_FOREACH(re, &reset_handlers, entry) {
1260 if (re->func == func && re->opaque == opaque) {
1261 QTAILQ_REMOVE(&reset_handlers, re, entry);
1262 g_free(re);
1263 return;
1268 void qemu_system_reset(bool report)
1270 QEMUResetEntry *re, *nre;
1272 /* reset all devices */
1273 QTAILQ_FOREACH_SAFE(re, &reset_handlers, entry, nre) {
1274 re->func(re->opaque);
1276 if (report) {
1277 monitor_protocol_event(QEVENT_RESET, NULL);
1279 cpu_synchronize_all_post_reset();
1282 void qemu_system_reset_request(void)
1284 if (no_reboot) {
1285 shutdown_requested = 1;
1286 } else {
1287 reset_requested = 1;
1289 cpu_stop_current();
1290 qemu_notify_event();
1293 void qemu_system_killed(int signal, pid_t pid)
1295 shutdown_signal = signal;
1296 shutdown_pid = pid;
1297 qemu_system_shutdown_request();
1300 void qemu_system_shutdown_request(void)
1302 shutdown_requested = 1;
1303 qemu_notify_event();
1306 void qemu_system_powerdown_request(void)
1308 powerdown_requested = 1;
1309 qemu_notify_event();
1312 void qemu_system_debug_request(void)
1314 debug_requested = 1;
1315 qemu_notify_event();
1318 void qemu_system_vmstop_request(int reason)
1320 vmstop_requested = reason;
1321 qemu_notify_event();
1324 void main_loop_wait(int nonblocking)
1326 fd_set rfds, wfds, xfds;
1327 int ret, nfds;
1328 struct timeval tv;
1329 int timeout;
1331 if (nonblocking)
1332 timeout = 0;
1333 else {
1334 timeout = qemu_calculate_timeout();
1335 qemu_bh_update_timeout(&timeout);
1338 os_host_main_loop_wait(&timeout);
1340 tv.tv_sec = timeout / 1000;
1341 tv.tv_usec = (timeout % 1000) * 1000;
1343 /* poll any events */
1344 /* XXX: separate device handlers from system ones */
1345 nfds = -1;
1346 FD_ZERO(&rfds);
1347 FD_ZERO(&wfds);
1348 FD_ZERO(&xfds);
1349 qemu_iohandler_fill(&nfds, &rfds, &wfds, &xfds);
1350 slirp_select_fill(&nfds, &rfds, &wfds, &xfds);
1352 qemu_mutex_unlock_iothread();
1353 ret = select(nfds + 1, &rfds, &wfds, &xfds, &tv);
1354 qemu_mutex_lock_iothread();
1356 qemu_iohandler_poll(&rfds, &wfds, &xfds, ret);
1357 slirp_select_poll(&rfds, &wfds, &xfds, (ret < 0));
1359 qemu_run_all_timers();
1361 /* Check bottom-halves last in case any of the earlier events triggered
1362 them. */
1363 qemu_bh_poll();
1367 #ifndef CONFIG_IOTHREAD
1368 static int vm_request_pending(void)
1370 return powerdown_requested ||
1371 reset_requested ||
1372 shutdown_requested ||
1373 debug_requested ||
1374 vmstop_requested;
1376 #endif
1378 qemu_irq qemu_system_powerdown;
1380 static void main_loop(void)
1382 bool nonblocking = false;
1383 #ifdef CONFIG_PROFILER
1384 int64_t ti;
1385 #endif
1386 int r;
1388 qemu_main_loop_start();
1390 for (;;) {
1391 #ifndef CONFIG_IOTHREAD
1392 nonblocking = cpu_exec_all();
1393 if (vm_request_pending()) {
1394 nonblocking = true;
1396 #endif
1397 #ifdef CONFIG_PROFILER
1398 ti = profile_getclock();
1399 #endif
1400 main_loop_wait(nonblocking);
1401 #ifdef CONFIG_PROFILER
1402 dev_time += profile_getclock() - ti;
1403 #endif
1405 if (qemu_debug_requested()) {
1406 vm_stop(VMSTOP_DEBUG);
1408 if (qemu_shutdown_requested()) {
1409 qemu_kill_report();
1410 monitor_protocol_event(QEVENT_SHUTDOWN, NULL);
1411 if (no_shutdown) {
1412 vm_stop(VMSTOP_SHUTDOWN);
1413 } else
1414 break;
1416 if (qemu_reset_requested()) {
1417 pause_all_vcpus();
1418 cpu_synchronize_all_states();
1419 qemu_system_reset(VMRESET_REPORT);
1420 resume_all_vcpus();
1422 if (qemu_powerdown_requested()) {
1423 monitor_protocol_event(QEVENT_POWERDOWN, NULL);
1424 qemu_irq_raise(qemu_system_powerdown);
1426 if ((r = qemu_vmstop_requested())) {
1427 vm_stop(r);
1430 bdrv_close_all();
1431 pause_all_vcpus();
1434 static void version(void)
1436 printf("QEMU emulator version " QEMU_VERSION QEMU_PKGVERSION ", Copyright (c) 2003-2008 Fabrice Bellard\n");
1439 static void help(int exitcode)
1441 const char *options_help =
1442 #define DEF(option, opt_arg, opt_enum, opt_help, arch_mask) \
1443 opt_help
1444 #define DEFHEADING(text) stringify(text) "\n"
1445 #include "qemu-options.def"
1446 #undef DEF
1447 #undef DEFHEADING
1448 #undef GEN_DOCS
1450 version();
1451 printf("usage: %s [options] [disk_image]\n"
1452 "\n"
1453 "'disk_image' is a raw hard disk image for IDE hard disk 0\n"
1454 "\n"
1455 "%s\n"
1456 "During emulation, the following keys are useful:\n"
1457 "ctrl-alt-f toggle full screen\n"
1458 "ctrl-alt-n switch to virtual console 'n'\n"
1459 "ctrl-alt toggle mouse and keyboard grab\n"
1460 "\n"
1461 "When using -nographic, press 'ctrl-a h' to get some help.\n",
1462 "qemu",
1463 options_help);
1464 exit(exitcode);
1467 #define HAS_ARG 0x0001
1469 typedef struct QEMUOption {
1470 const char *name;
1471 int flags;
1472 int index;
1473 uint32_t arch_mask;
1474 } QEMUOption;
1476 static const QEMUOption qemu_options[] = {
1477 { "h", 0, QEMU_OPTION_h, QEMU_ARCH_ALL },
1478 #define DEF(option, opt_arg, opt_enum, opt_help, arch_mask) \
1479 { option, opt_arg, opt_enum, arch_mask },
1480 #define DEFHEADING(text)
1481 #include "qemu-options.def"
1482 #undef DEF
1483 #undef DEFHEADING
1484 #undef GEN_DOCS
1485 { NULL },
1487 static void select_vgahw (const char *p)
1489 const char *opts;
1491 default_vga = 0;
1492 vga_interface_type = VGA_NONE;
1493 if (strstart(p, "std", &opts)) {
1494 vga_interface_type = VGA_STD;
1495 } else if (strstart(p, "cirrus", &opts)) {
1496 vga_interface_type = VGA_CIRRUS;
1497 } else if (strstart(p, "vmware", &opts)) {
1498 vga_interface_type = VGA_VMWARE;
1499 } else if (strstart(p, "xenfb", &opts)) {
1500 vga_interface_type = VGA_XENFB;
1501 } else if (strstart(p, "qxl", &opts)) {
1502 vga_interface_type = VGA_QXL;
1503 } else if (!strstart(p, "none", &opts)) {
1504 invalid_vga:
1505 fprintf(stderr, "Unknown vga type: %s\n", p);
1506 exit(1);
1508 while (*opts) {
1509 const char *nextopt;
1511 if (strstart(opts, ",retrace=", &nextopt)) {
1512 opts = nextopt;
1513 if (strstart(opts, "dumb", &nextopt))
1514 vga_retrace_method = VGA_RETRACE_DUMB;
1515 else if (strstart(opts, "precise", &nextopt))
1516 vga_retrace_method = VGA_RETRACE_PRECISE;
1517 else goto invalid_vga;
1518 } else goto invalid_vga;
1519 opts = nextopt;
1523 static DisplayType select_display(const char *p)
1525 const char *opts;
1526 DisplayType display = DT_DEFAULT;
1528 if (strstart(p, "sdl", &opts)) {
1529 #ifdef CONFIG_SDL
1530 display = DT_SDL;
1531 while (*opts) {
1532 const char *nextopt;
1534 if (strstart(opts, ",frame=", &nextopt)) {
1535 opts = nextopt;
1536 if (strstart(opts, "on", &nextopt)) {
1537 no_frame = 0;
1538 } else if (strstart(opts, "off", &nextopt)) {
1539 no_frame = 1;
1540 } else {
1541 goto invalid_sdl_args;
1543 } else if (strstart(opts, ",alt_grab=", &nextopt)) {
1544 opts = nextopt;
1545 if (strstart(opts, "on", &nextopt)) {
1546 alt_grab = 1;
1547 } else if (strstart(opts, "off", &nextopt)) {
1548 alt_grab = 0;
1549 } else {
1550 goto invalid_sdl_args;
1552 } else if (strstart(opts, ",ctrl_grab=", &nextopt)) {
1553 opts = nextopt;
1554 if (strstart(opts, "on", &nextopt)) {
1555 ctrl_grab = 1;
1556 } else if (strstart(opts, "off", &nextopt)) {
1557 ctrl_grab = 0;
1558 } else {
1559 goto invalid_sdl_args;
1561 } else if (strstart(opts, ",window_close=", &nextopt)) {
1562 opts = nextopt;
1563 if (strstart(opts, "on", &nextopt)) {
1564 no_quit = 0;
1565 } else if (strstart(opts, "off", &nextopt)) {
1566 no_quit = 1;
1567 } else {
1568 goto invalid_sdl_args;
1570 } else {
1571 invalid_sdl_args:
1572 fprintf(stderr, "Invalid SDL option string: %s\n", p);
1573 exit(1);
1575 opts = nextopt;
1577 #else
1578 fprintf(stderr, "SDL support is disabled\n");
1579 exit(1);
1580 #endif
1581 } else if (strstart(p, "vnc", &opts)) {
1582 #ifdef CONFIG_VNC
1583 display_remote++;
1585 if (*opts) {
1586 const char *nextopt;
1588 if (strstart(opts, "=", &nextopt)) {
1589 vnc_display = nextopt;
1592 if (!vnc_display) {
1593 fprintf(stderr, "VNC requires a display argument vnc=<display>\n");
1594 exit(1);
1596 #else
1597 fprintf(stderr, "VNC support is disabled\n");
1598 exit(1);
1599 #endif
1600 } else if (strstart(p, "curses", &opts)) {
1601 #ifdef CONFIG_CURSES
1602 display = DT_CURSES;
1603 #else
1604 fprintf(stderr, "Curses support is disabled\n");
1605 exit(1);
1606 #endif
1607 } else if (strstart(p, "none", &opts)) {
1608 display = DT_NONE;
1609 } else {
1610 fprintf(stderr, "Unknown display type: %s\n", p);
1611 exit(1);
1614 return display;
1617 static int balloon_parse(const char *arg)
1619 QemuOpts *opts;
1621 if (strcmp(arg, "none") == 0) {
1622 return 0;
1625 if (!strncmp(arg, "virtio", 6)) {
1626 if (arg[6] == ',') {
1627 /* have params -> parse them */
1628 opts = qemu_opts_parse(qemu_find_opts("device"), arg+7, 0);
1629 if (!opts)
1630 return -1;
1631 } else {
1632 /* create empty opts */
1633 opts = qemu_opts_create(qemu_find_opts("device"), NULL, 0);
1635 qemu_opt_set(opts, "driver", "virtio-balloon");
1636 return 0;
1639 return -1;
1642 char *qemu_find_file(int type, const char *name)
1644 int len;
1645 const char *subdir;
1646 char *buf;
1648 /* If name contains path separators then try it as a straight path. */
1649 if ((strchr(name, '/') || strchr(name, '\\'))
1650 && access(name, R_OK) == 0) {
1651 return g_strdup(name);
1653 switch (type) {
1654 case QEMU_FILE_TYPE_BIOS:
1655 subdir = "";
1656 break;
1657 case QEMU_FILE_TYPE_KEYMAP:
1658 subdir = "keymaps/";
1659 break;
1660 default:
1661 abort();
1663 len = strlen(data_dir) + strlen(name) + strlen(subdir) + 2;
1664 buf = g_malloc0(len);
1665 snprintf(buf, len, "%s/%s%s", data_dir, subdir, name);
1666 if (access(buf, R_OK)) {
1667 g_free(buf);
1668 return NULL;
1670 return buf;
1673 static int device_help_func(QemuOpts *opts, void *opaque)
1675 return qdev_device_help(opts);
1678 static int device_init_func(QemuOpts *opts, void *opaque)
1680 DeviceState *dev;
1682 dev = qdev_device_add(opts);
1683 if (!dev)
1684 return -1;
1685 return 0;
1688 static int chardev_init_func(QemuOpts *opts, void *opaque)
1690 CharDriverState *chr;
1692 chr = qemu_chr_open_opts(opts, NULL);
1693 if (!chr)
1694 return -1;
1695 return 0;
1698 #ifdef CONFIG_VIRTFS
1699 static int fsdev_init_func(QemuOpts *opts, void *opaque)
1701 int ret;
1702 ret = qemu_fsdev_add(opts);
1704 return ret;
1706 #endif
1708 static int mon_init_func(QemuOpts *opts, void *opaque)
1710 CharDriverState *chr;
1711 const char *chardev;
1712 const char *mode;
1713 int flags;
1715 mode = qemu_opt_get(opts, "mode");
1716 if (mode == NULL) {
1717 mode = "readline";
1719 if (strcmp(mode, "readline") == 0) {
1720 flags = MONITOR_USE_READLINE;
1721 } else if (strcmp(mode, "control") == 0) {
1722 flags = MONITOR_USE_CONTROL;
1723 } else {
1724 fprintf(stderr, "unknown monitor mode \"%s\"\n", mode);
1725 exit(1);
1728 if (qemu_opt_get_bool(opts, "pretty", 0))
1729 flags |= MONITOR_USE_PRETTY;
1731 if (qemu_opt_get_bool(opts, "default", 0))
1732 flags |= MONITOR_IS_DEFAULT;
1734 chardev = qemu_opt_get(opts, "chardev");
1735 chr = qemu_chr_find(chardev);
1736 if (chr == NULL) {
1737 fprintf(stderr, "chardev \"%s\" not found\n", chardev);
1738 exit(1);
1741 monitor_init(chr, flags);
1742 return 0;
1745 static void monitor_parse(const char *optarg, const char *mode)
1747 static int monitor_device_index = 0;
1748 QemuOpts *opts;
1749 const char *p;
1750 char label[32];
1751 int def = 0;
1753 if (strstart(optarg, "chardev:", &p)) {
1754 snprintf(label, sizeof(label), "%s", p);
1755 } else {
1756 snprintf(label, sizeof(label), "compat_monitor%d",
1757 monitor_device_index);
1758 if (monitor_device_index == 0) {
1759 def = 1;
1761 opts = qemu_chr_parse_compat(label, optarg);
1762 if (!opts) {
1763 fprintf(stderr, "parse error: %s\n", optarg);
1764 exit(1);
1768 opts = qemu_opts_create(qemu_find_opts("mon"), label, 1);
1769 if (!opts) {
1770 fprintf(stderr, "duplicate chardev: %s\n", label);
1771 exit(1);
1773 qemu_opt_set(opts, "mode", mode);
1774 qemu_opt_set(opts, "chardev", label);
1775 if (def)
1776 qemu_opt_set(opts, "default", "on");
1777 monitor_device_index++;
1780 struct device_config {
1781 enum {
1782 DEV_USB, /* -usbdevice */
1783 DEV_BT, /* -bt */
1784 DEV_SERIAL, /* -serial */
1785 DEV_PARALLEL, /* -parallel */
1786 DEV_VIRTCON, /* -virtioconsole */
1787 DEV_DEBUGCON, /* -debugcon */
1788 } type;
1789 const char *cmdline;
1790 QTAILQ_ENTRY(device_config) next;
1792 QTAILQ_HEAD(, device_config) device_configs = QTAILQ_HEAD_INITIALIZER(device_configs);
1794 static void add_device_config(int type, const char *cmdline)
1796 struct device_config *conf;
1798 conf = g_malloc0(sizeof(*conf));
1799 conf->type = type;
1800 conf->cmdline = cmdline;
1801 QTAILQ_INSERT_TAIL(&device_configs, conf, next);
1804 static int foreach_device_config(int type, int (*func)(const char *cmdline))
1806 struct device_config *conf;
1807 int rc;
1809 QTAILQ_FOREACH(conf, &device_configs, next) {
1810 if (conf->type != type)
1811 continue;
1812 rc = func(conf->cmdline);
1813 if (0 != rc)
1814 return rc;
1816 return 0;
1819 static int serial_parse(const char *devname)
1821 static int index = 0;
1822 char label[32];
1824 if (strcmp(devname, "none") == 0)
1825 return 0;
1826 if (index == MAX_SERIAL_PORTS) {
1827 fprintf(stderr, "qemu: too many serial ports\n");
1828 exit(1);
1830 snprintf(label, sizeof(label), "serial%d", index);
1831 serial_hds[index] = qemu_chr_open(label, devname, NULL);
1832 if (!serial_hds[index]) {
1833 fprintf(stderr, "qemu: could not open serial device '%s': %s\n",
1834 devname, strerror(errno));
1835 return -1;
1837 index++;
1838 return 0;
1841 static int parallel_parse(const char *devname)
1843 static int index = 0;
1844 char label[32];
1846 if (strcmp(devname, "none") == 0)
1847 return 0;
1848 if (index == MAX_PARALLEL_PORTS) {
1849 fprintf(stderr, "qemu: too many parallel ports\n");
1850 exit(1);
1852 snprintf(label, sizeof(label), "parallel%d", index);
1853 parallel_hds[index] = qemu_chr_open(label, devname, NULL);
1854 if (!parallel_hds[index]) {
1855 fprintf(stderr, "qemu: could not open parallel device '%s': %s\n",
1856 devname, strerror(errno));
1857 return -1;
1859 index++;
1860 return 0;
1863 static int virtcon_parse(const char *devname)
1865 QemuOptsList *device = qemu_find_opts("device");
1866 static int index = 0;
1867 char label[32];
1868 QemuOpts *bus_opts, *dev_opts;
1870 if (strcmp(devname, "none") == 0)
1871 return 0;
1872 if (index == MAX_VIRTIO_CONSOLES) {
1873 fprintf(stderr, "qemu: too many virtio consoles\n");
1874 exit(1);
1877 bus_opts = qemu_opts_create(device, NULL, 0);
1878 qemu_opt_set(bus_opts, "driver", "virtio-serial");
1880 dev_opts = qemu_opts_create(device, NULL, 0);
1881 qemu_opt_set(dev_opts, "driver", "virtconsole");
1883 snprintf(label, sizeof(label), "virtcon%d", index);
1884 virtcon_hds[index] = qemu_chr_open(label, devname, NULL);
1885 if (!virtcon_hds[index]) {
1886 fprintf(stderr, "qemu: could not open virtio console '%s': %s\n",
1887 devname, strerror(errno));
1888 return -1;
1890 qemu_opt_set(dev_opts, "chardev", label);
1892 index++;
1893 return 0;
1896 static int debugcon_parse(const char *devname)
1898 QemuOpts *opts;
1900 if (!qemu_chr_open("debugcon", devname, NULL)) {
1901 exit(1);
1903 opts = qemu_opts_create(qemu_find_opts("device"), "debugcon", 1);
1904 if (!opts) {
1905 fprintf(stderr, "qemu: already have a debugcon device\n");
1906 exit(1);
1908 qemu_opt_set(opts, "driver", "isa-debugcon");
1909 qemu_opt_set(opts, "chardev", "debugcon");
1910 return 0;
1913 static QEMUMachine *machine_parse(const char *name)
1915 QEMUMachine *m, *machine = NULL;
1917 if (name) {
1918 machine = find_machine(name);
1920 if (machine) {
1921 return machine;
1923 printf("Supported machines are:\n");
1924 for (m = first_machine; m != NULL; m = m->next) {
1925 if (m->alias) {
1926 printf("%-10s %s (alias of %s)\n", m->alias, m->desc, m->name);
1928 printf("%-10s %s%s\n", m->name, m->desc,
1929 m->is_default ? " (default)" : "");
1931 exit(!name || *name != '?');
1934 static int tcg_init(void)
1936 tcg_exec_init(tcg_tb_size * 1024 * 1024);
1937 return 0;
1940 static struct {
1941 const char *opt_name;
1942 const char *name;
1943 int (*available)(void);
1944 int (*init)(void);
1945 int *allowed;
1946 } accel_list[] = {
1947 { "tcg", "tcg", tcg_available, tcg_init, &tcg_allowed },
1948 { "xen", "Xen", xen_available, xen_init, &xen_allowed },
1949 { "kvm", "KVM", kvm_available, kvm_init, &kvm_allowed },
1952 static int configure_accelerator(void)
1954 const char *p = NULL;
1955 char buf[10];
1956 int i, ret;
1957 bool accel_initalised = 0;
1958 bool init_failed = 0;
1960 QemuOptsList *list = qemu_find_opts("machine");
1961 if (!QTAILQ_EMPTY(&list->head)) {
1962 p = qemu_opt_get(QTAILQ_FIRST(&list->head), "accel");
1965 if (p == NULL) {
1966 /* Use the default "accelerator", tcg */
1967 p = "tcg";
1970 while (!accel_initalised && *p != '\0') {
1971 if (*p == ':') {
1972 p++;
1974 p = get_opt_name(buf, sizeof (buf), p, ':');
1975 for (i = 0; i < ARRAY_SIZE(accel_list); i++) {
1976 if (strcmp(accel_list[i].opt_name, buf) == 0) {
1977 *(accel_list[i].allowed) = 1;
1978 ret = accel_list[i].init();
1979 if (ret < 0) {
1980 init_failed = 1;
1981 if (!accel_list[i].available()) {
1982 printf("%s not supported for this target\n",
1983 accel_list[i].name);
1984 } else {
1985 fprintf(stderr, "failed to initialize %s: %s\n",
1986 accel_list[i].name,
1987 strerror(-ret));
1989 *(accel_list[i].allowed) = 0;
1990 } else {
1991 accel_initalised = 1;
1993 break;
1996 if (i == ARRAY_SIZE(accel_list)) {
1997 fprintf(stderr, "\"%s\" accelerator does not exist.\n", buf);
2001 if (!accel_initalised) {
2002 fprintf(stderr, "No accelerator found!\n");
2003 exit(1);
2006 if (init_failed) {
2007 fprintf(stderr, "Back to %s accelerator.\n", accel_list[i].name);
2010 return !accel_initalised;
2013 void qemu_add_exit_notifier(Notifier *notify)
2015 notifier_list_add(&exit_notifiers, notify);
2018 void qemu_remove_exit_notifier(Notifier *notify)
2020 notifier_list_remove(&exit_notifiers, notify);
2023 static void qemu_run_exit_notifiers(void)
2025 notifier_list_notify(&exit_notifiers, NULL);
2028 void qemu_add_machine_init_done_notifier(Notifier *notify)
2030 notifier_list_add(&machine_init_done_notifiers, notify);
2033 static void qemu_run_machine_init_done_notifiers(void)
2035 notifier_list_notify(&machine_init_done_notifiers, NULL);
2038 static const QEMUOption *lookup_opt(int argc, char **argv,
2039 const char **poptarg, int *poptind)
2041 const QEMUOption *popt;
2042 int optind = *poptind;
2043 char *r = argv[optind];
2044 const char *optarg;
2046 loc_set_cmdline(argv, optind, 1);
2047 optind++;
2048 /* Treat --foo the same as -foo. */
2049 if (r[1] == '-')
2050 r++;
2051 popt = qemu_options;
2052 for(;;) {
2053 if (!popt->name) {
2054 error_report("invalid option");
2055 exit(1);
2057 if (!strcmp(popt->name, r + 1))
2058 break;
2059 popt++;
2061 if (popt->flags & HAS_ARG) {
2062 if (optind >= argc) {
2063 error_report("requires an argument");
2064 exit(1);
2066 optarg = argv[optind++];
2067 loc_set_cmdline(argv, optind - 2, 2);
2068 } else {
2069 optarg = NULL;
2072 *poptarg = optarg;
2073 *poptind = optind;
2075 return popt;
2078 static gpointer malloc_and_trace(gsize n_bytes)
2080 void *ptr = malloc(n_bytes);
2081 trace_qemu_malloc(n_bytes, ptr);
2082 return ptr;
2085 static gpointer realloc_and_trace(gpointer mem, gsize n_bytes)
2087 void *ptr = realloc(mem, n_bytes);
2088 trace_qemu_realloc(mem, n_bytes, ptr);
2089 return ptr;
2092 static void free_and_trace(gpointer mem)
2094 trace_qemu_free(mem);
2095 free(mem);
2098 int main(int argc, char **argv, char **envp)
2100 const char *gdbstub_dev = NULL;
2101 int i;
2102 int snapshot, linux_boot;
2103 const char *icount_option = NULL;
2104 const char *initrd_filename;
2105 const char *kernel_filename, *kernel_cmdline;
2106 char boot_devices[33] = "cad"; /* default to HD->floppy->CD-ROM */
2107 DisplayState *ds;
2108 DisplayChangeListener *dcl;
2109 int cyls, heads, secs, translation;
2110 QemuOpts *hda_opts = NULL, *opts;
2111 QemuOptsList *olist;
2112 int optind;
2113 const char *optarg;
2114 const char *loadvm = NULL;
2115 QEMUMachine *machine;
2116 const char *cpu_model;
2117 const char *pid_file = NULL;
2118 const char *incoming = NULL;
2119 #ifdef CONFIG_VNC
2120 int show_vnc_port = 0;
2121 #endif
2122 int defconfig = 1;
2123 const char *trace_file = NULL;
2124 const char *log_mask = NULL;
2125 const char *log_file = NULL;
2126 GMemVTable mem_trace = {
2127 .malloc = malloc_and_trace,
2128 .realloc = realloc_and_trace,
2129 .free = free_and_trace,
2132 atexit(qemu_run_exit_notifiers);
2133 error_set_progname(argv[0]);
2135 g_mem_set_vtable(&mem_trace);
2137 init_clocks();
2139 qemu_cache_utils_init(envp);
2141 QLIST_INIT (&vm_change_state_head);
2142 os_setup_early_signal_handling();
2144 module_call_init(MODULE_INIT_MACHINE);
2145 machine = find_default_machine();
2146 cpu_model = NULL;
2147 initrd_filename = NULL;
2148 ram_size = 0;
2149 snapshot = 0;
2150 kernel_filename = NULL;
2151 kernel_cmdline = "";
2152 cyls = heads = secs = 0;
2153 translation = BIOS_ATA_TRANSLATION_AUTO;
2155 for (i = 0; i < MAX_NODES; i++) {
2156 node_mem[i] = 0;
2157 node_cpumask[i] = 0;
2160 nb_numa_nodes = 0;
2161 nb_nics = 0;
2163 autostart= 1;
2165 /* first pass of option parsing */
2166 optind = 1;
2167 while (optind < argc) {
2168 if (argv[optind][0] != '-') {
2169 /* disk image */
2170 optind++;
2171 continue;
2172 } else {
2173 const QEMUOption *popt;
2175 popt = lookup_opt(argc, argv, &optarg, &optind);
2176 switch (popt->index) {
2177 case QEMU_OPTION_nodefconfig:
2178 defconfig=0;
2179 break;
2184 if (defconfig) {
2185 int ret;
2187 ret = qemu_read_config_file(CONFIG_QEMU_CONFDIR "/qemu.conf");
2188 if (ret < 0 && ret != -ENOENT) {
2189 exit(1);
2192 ret = qemu_read_config_file(arch_config_name);
2193 if (ret < 0 && ret != -ENOENT) {
2194 exit(1);
2197 cpudef_init();
2199 /* second pass of option parsing */
2200 optind = 1;
2201 for(;;) {
2202 if (optind >= argc)
2203 break;
2204 if (argv[optind][0] != '-') {
2205 hda_opts = drive_add(IF_DEFAULT, 0, argv[optind++], HD_OPTS);
2206 } else {
2207 const QEMUOption *popt;
2209 popt = lookup_opt(argc, argv, &optarg, &optind);
2210 if (!(popt->arch_mask & arch_type)) {
2211 printf("Option %s not supported for this target\n", popt->name);
2212 exit(1);
2214 switch(popt->index) {
2215 case QEMU_OPTION_M:
2216 machine = machine_parse(optarg);
2217 break;
2218 case QEMU_OPTION_cpu:
2219 /* hw initialization will check this */
2220 if (*optarg == '?') {
2221 list_cpus(stdout, &fprintf, optarg);
2222 exit(0);
2223 } else {
2224 cpu_model = optarg;
2226 break;
2227 case QEMU_OPTION_initrd:
2228 initrd_filename = optarg;
2229 break;
2230 case QEMU_OPTION_hda:
2232 char buf[256];
2233 if (cyls == 0)
2234 snprintf(buf, sizeof(buf), "%s", HD_OPTS);
2235 else
2236 snprintf(buf, sizeof(buf),
2237 "%s,cyls=%d,heads=%d,secs=%d%s",
2238 HD_OPTS , cyls, heads, secs,
2239 translation == BIOS_ATA_TRANSLATION_LBA ?
2240 ",trans=lba" :
2241 translation == BIOS_ATA_TRANSLATION_NONE ?
2242 ",trans=none" : "");
2243 drive_add(IF_DEFAULT, 0, optarg, buf);
2244 break;
2246 case QEMU_OPTION_hdb:
2247 case QEMU_OPTION_hdc:
2248 case QEMU_OPTION_hdd:
2249 drive_add(IF_DEFAULT, popt->index - QEMU_OPTION_hda, optarg,
2250 HD_OPTS);
2251 break;
2252 case QEMU_OPTION_drive:
2253 if (drive_def(optarg) == NULL) {
2254 exit(1);
2256 break;
2257 case QEMU_OPTION_set:
2258 if (qemu_set_option(optarg) != 0)
2259 exit(1);
2260 break;
2261 case QEMU_OPTION_global:
2262 if (qemu_global_option(optarg) != 0)
2263 exit(1);
2264 break;
2265 case QEMU_OPTION_mtdblock:
2266 drive_add(IF_MTD, -1, optarg, MTD_OPTS);
2267 break;
2268 case QEMU_OPTION_sd:
2269 drive_add(IF_SD, 0, optarg, SD_OPTS);
2270 break;
2271 case QEMU_OPTION_pflash:
2272 drive_add(IF_PFLASH, -1, optarg, PFLASH_OPTS);
2273 break;
2274 case QEMU_OPTION_snapshot:
2275 snapshot = 1;
2276 break;
2277 case QEMU_OPTION_hdachs:
2279 const char *p;
2280 p = optarg;
2281 cyls = strtol(p, (char **)&p, 0);
2282 if (cyls < 1 || cyls > 16383)
2283 goto chs_fail;
2284 if (*p != ',')
2285 goto chs_fail;
2286 p++;
2287 heads = strtol(p, (char **)&p, 0);
2288 if (heads < 1 || heads > 16)
2289 goto chs_fail;
2290 if (*p != ',')
2291 goto chs_fail;
2292 p++;
2293 secs = strtol(p, (char **)&p, 0);
2294 if (secs < 1 || secs > 63)
2295 goto chs_fail;
2296 if (*p == ',') {
2297 p++;
2298 if (!strcmp(p, "none"))
2299 translation = BIOS_ATA_TRANSLATION_NONE;
2300 else if (!strcmp(p, "lba"))
2301 translation = BIOS_ATA_TRANSLATION_LBA;
2302 else if (!strcmp(p, "auto"))
2303 translation = BIOS_ATA_TRANSLATION_AUTO;
2304 else
2305 goto chs_fail;
2306 } else if (*p != '\0') {
2307 chs_fail:
2308 fprintf(stderr, "qemu: invalid physical CHS format\n");
2309 exit(1);
2311 if (hda_opts != NULL) {
2312 char num[16];
2313 snprintf(num, sizeof(num), "%d", cyls);
2314 qemu_opt_set(hda_opts, "cyls", num);
2315 snprintf(num, sizeof(num), "%d", heads);
2316 qemu_opt_set(hda_opts, "heads", num);
2317 snprintf(num, sizeof(num), "%d", secs);
2318 qemu_opt_set(hda_opts, "secs", num);
2319 if (translation == BIOS_ATA_TRANSLATION_LBA)
2320 qemu_opt_set(hda_opts, "trans", "lba");
2321 if (translation == BIOS_ATA_TRANSLATION_NONE)
2322 qemu_opt_set(hda_opts, "trans", "none");
2325 break;
2326 case QEMU_OPTION_numa:
2327 if (nb_numa_nodes >= MAX_NODES) {
2328 fprintf(stderr, "qemu: too many NUMA nodes\n");
2329 exit(1);
2331 numa_add(optarg);
2332 break;
2333 case QEMU_OPTION_display:
2334 display_type = select_display(optarg);
2335 break;
2336 case QEMU_OPTION_nographic:
2337 display_type = DT_NOGRAPHIC;
2338 break;
2339 case QEMU_OPTION_curses:
2340 #ifdef CONFIG_CURSES
2341 display_type = DT_CURSES;
2342 #else
2343 fprintf(stderr, "Curses support is disabled\n");
2344 exit(1);
2345 #endif
2346 break;
2347 case QEMU_OPTION_portrait:
2348 graphic_rotate = 90;
2349 break;
2350 case QEMU_OPTION_rotate:
2351 graphic_rotate = strtol(optarg, (char **) &optarg, 10);
2352 if (graphic_rotate != 0 && graphic_rotate != 90 &&
2353 graphic_rotate != 180 && graphic_rotate != 270) {
2354 fprintf(stderr,
2355 "qemu: only 90, 180, 270 deg rotation is available\n");
2356 exit(1);
2358 break;
2359 case QEMU_OPTION_kernel:
2360 kernel_filename = optarg;
2361 break;
2362 case QEMU_OPTION_append:
2363 kernel_cmdline = optarg;
2364 break;
2365 case QEMU_OPTION_cdrom:
2366 drive_add(IF_DEFAULT, 2, optarg, CDROM_OPTS);
2367 break;
2368 case QEMU_OPTION_boot:
2370 static const char * const params[] = {
2371 "order", "once", "menu",
2372 "splash", "splash-time", NULL
2374 char buf[sizeof(boot_devices)];
2375 char *standard_boot_devices;
2376 int legacy = 0;
2378 if (!strchr(optarg, '=')) {
2379 legacy = 1;
2380 pstrcpy(buf, sizeof(buf), optarg);
2381 } else if (check_params(buf, sizeof(buf), params, optarg) < 0) {
2382 fprintf(stderr,
2383 "qemu: unknown boot parameter '%s' in '%s'\n",
2384 buf, optarg);
2385 exit(1);
2388 if (legacy ||
2389 get_param_value(buf, sizeof(buf), "order", optarg)) {
2390 validate_bootdevices(buf);
2391 pstrcpy(boot_devices, sizeof(boot_devices), buf);
2393 if (!legacy) {
2394 if (get_param_value(buf, sizeof(buf),
2395 "once", optarg)) {
2396 validate_bootdevices(buf);
2397 standard_boot_devices = g_strdup(boot_devices);
2398 pstrcpy(boot_devices, sizeof(boot_devices), buf);
2399 qemu_register_reset(restore_boot_devices,
2400 standard_boot_devices);
2402 if (get_param_value(buf, sizeof(buf),
2403 "menu", optarg)) {
2404 if (!strcmp(buf, "on")) {
2405 boot_menu = 1;
2406 } else if (!strcmp(buf, "off")) {
2407 boot_menu = 0;
2408 } else {
2409 fprintf(stderr,
2410 "qemu: invalid option value '%s'\n",
2411 buf);
2412 exit(1);
2415 qemu_opts_parse(qemu_find_opts("boot-opts"),
2416 optarg, 0);
2419 break;
2420 case QEMU_OPTION_fda:
2421 case QEMU_OPTION_fdb:
2422 drive_add(IF_FLOPPY, popt->index - QEMU_OPTION_fda,
2423 optarg, FD_OPTS);
2424 break;
2425 case QEMU_OPTION_no_fd_bootchk:
2426 fd_bootchk = 0;
2427 break;
2428 case QEMU_OPTION_netdev:
2429 if (net_client_parse(qemu_find_opts("netdev"), optarg) == -1) {
2430 exit(1);
2432 break;
2433 case QEMU_OPTION_net:
2434 if (net_client_parse(qemu_find_opts("net"), optarg) == -1) {
2435 exit(1);
2437 break;
2438 #ifdef CONFIG_SLIRP
2439 case QEMU_OPTION_tftp:
2440 legacy_tftp_prefix = optarg;
2441 break;
2442 case QEMU_OPTION_bootp:
2443 legacy_bootp_filename = optarg;
2444 break;
2445 case QEMU_OPTION_redir:
2446 if (net_slirp_redir(optarg) < 0)
2447 exit(1);
2448 break;
2449 #endif
2450 case QEMU_OPTION_bt:
2451 add_device_config(DEV_BT, optarg);
2452 break;
2453 case QEMU_OPTION_audio_help:
2454 if (!(audio_available())) {
2455 printf("Option %s not supported for this target\n", popt->name);
2456 exit(1);
2458 AUD_help ();
2459 exit (0);
2460 break;
2461 case QEMU_OPTION_soundhw:
2462 if (!(audio_available())) {
2463 printf("Option %s not supported for this target\n", popt->name);
2464 exit(1);
2466 select_soundhw (optarg);
2467 break;
2468 case QEMU_OPTION_h:
2469 help(0);
2470 break;
2471 case QEMU_OPTION_version:
2472 version();
2473 exit(0);
2474 break;
2475 case QEMU_OPTION_m: {
2476 int64_t value;
2478 value = strtosz(optarg, NULL);
2479 if (value < 0) {
2480 fprintf(stderr, "qemu: invalid ram size: %s\n", optarg);
2481 exit(1);
2484 if (value != (uint64_t)(ram_addr_t)value) {
2485 fprintf(stderr, "qemu: ram size too large\n");
2486 exit(1);
2488 ram_size = value;
2489 break;
2491 case QEMU_OPTION_mempath:
2492 mem_path = optarg;
2493 break;
2494 #ifdef MAP_POPULATE
2495 case QEMU_OPTION_mem_prealloc:
2496 mem_prealloc = 1;
2497 break;
2498 #endif
2499 case QEMU_OPTION_d:
2500 log_mask = optarg;
2501 break;
2502 case QEMU_OPTION_D:
2503 log_file = optarg;
2504 break;
2505 case QEMU_OPTION_s:
2506 gdbstub_dev = "tcp::" DEFAULT_GDBSTUB_PORT;
2507 break;
2508 case QEMU_OPTION_gdb:
2509 gdbstub_dev = optarg;
2510 break;
2511 case QEMU_OPTION_L:
2512 data_dir = optarg;
2513 break;
2514 case QEMU_OPTION_bios:
2515 bios_name = optarg;
2516 break;
2517 case QEMU_OPTION_singlestep:
2518 singlestep = 1;
2519 break;
2520 case QEMU_OPTION_S:
2521 autostart = 0;
2522 break;
2523 case QEMU_OPTION_k:
2524 keyboard_layout = optarg;
2525 break;
2526 case QEMU_OPTION_localtime:
2527 rtc_utc = 0;
2528 break;
2529 case QEMU_OPTION_vga:
2530 select_vgahw (optarg);
2531 break;
2532 case QEMU_OPTION_g:
2534 const char *p;
2535 int w, h, depth;
2536 p = optarg;
2537 w = strtol(p, (char **)&p, 10);
2538 if (w <= 0) {
2539 graphic_error:
2540 fprintf(stderr, "qemu: invalid resolution or depth\n");
2541 exit(1);
2543 if (*p != 'x')
2544 goto graphic_error;
2545 p++;
2546 h = strtol(p, (char **)&p, 10);
2547 if (h <= 0)
2548 goto graphic_error;
2549 if (*p == 'x') {
2550 p++;
2551 depth = strtol(p, (char **)&p, 10);
2552 if (depth != 8 && depth != 15 && depth != 16 &&
2553 depth != 24 && depth != 32)
2554 goto graphic_error;
2555 } else if (*p == '\0') {
2556 depth = graphic_depth;
2557 } else {
2558 goto graphic_error;
2561 graphic_width = w;
2562 graphic_height = h;
2563 graphic_depth = depth;
2565 break;
2566 case QEMU_OPTION_echr:
2568 char *r;
2569 term_escape_char = strtol(optarg, &r, 0);
2570 if (r == optarg)
2571 printf("Bad argument to echr\n");
2572 break;
2574 case QEMU_OPTION_monitor:
2575 monitor_parse(optarg, "readline");
2576 default_monitor = 0;
2577 break;
2578 case QEMU_OPTION_qmp:
2579 monitor_parse(optarg, "control");
2580 default_monitor = 0;
2581 break;
2582 case QEMU_OPTION_mon:
2583 opts = qemu_opts_parse(qemu_find_opts("mon"), optarg, 1);
2584 if (!opts) {
2585 exit(1);
2587 default_monitor = 0;
2588 break;
2589 case QEMU_OPTION_chardev:
2590 opts = qemu_opts_parse(qemu_find_opts("chardev"), optarg, 1);
2591 if (!opts) {
2592 exit(1);
2594 break;
2595 case QEMU_OPTION_fsdev:
2596 olist = qemu_find_opts("fsdev");
2597 if (!olist) {
2598 fprintf(stderr, "fsdev is not supported by this qemu build.\n");
2599 exit(1);
2601 opts = qemu_opts_parse(olist, optarg, 1);
2602 if (!opts) {
2603 fprintf(stderr, "parse error: %s\n", optarg);
2604 exit(1);
2606 break;
2607 case QEMU_OPTION_virtfs: {
2608 QemuOpts *fsdev;
2609 QemuOpts *device;
2611 olist = qemu_find_opts("virtfs");
2612 if (!olist) {
2613 fprintf(stderr, "virtfs is not supported by this qemu build.\n");
2614 exit(1);
2616 opts = qemu_opts_parse(olist, optarg, 1);
2617 if (!opts) {
2618 fprintf(stderr, "parse error: %s\n", optarg);
2619 exit(1);
2622 if (qemu_opt_get(opts, "fstype") == NULL ||
2623 qemu_opt_get(opts, "mount_tag") == NULL ||
2624 qemu_opt_get(opts, "path") == NULL ||
2625 qemu_opt_get(opts, "security_model") == NULL) {
2626 fprintf(stderr, "Usage: -virtfs fstype,path=/share_path/,"
2627 "security_model=[mapped|passthrough|none],"
2628 "mount_tag=tag.\n");
2629 exit(1);
2632 fsdev = qemu_opts_create(qemu_find_opts("fsdev"),
2633 qemu_opt_get(opts, "mount_tag"), 1);
2634 if (!fsdev) {
2635 fprintf(stderr, "duplicate fsdev id: %s\n",
2636 qemu_opt_get(opts, "mount_tag"));
2637 exit(1);
2639 qemu_opt_set(fsdev, "fstype", qemu_opt_get(opts, "fstype"));
2640 qemu_opt_set(fsdev, "path", qemu_opt_get(opts, "path"));
2641 qemu_opt_set(fsdev, "security_model",
2642 qemu_opt_get(opts, "security_model"));
2644 device = qemu_opts_create(qemu_find_opts("device"), NULL, 0);
2645 qemu_opt_set(device, "driver", "virtio-9p-pci");
2646 qemu_opt_set(device, "fsdev",
2647 qemu_opt_get(opts, "mount_tag"));
2648 qemu_opt_set(device, "mount_tag",
2649 qemu_opt_get(opts, "mount_tag"));
2650 break;
2652 case QEMU_OPTION_serial:
2653 add_device_config(DEV_SERIAL, optarg);
2654 default_serial = 0;
2655 if (strncmp(optarg, "mon:", 4) == 0) {
2656 default_monitor = 0;
2658 break;
2659 case QEMU_OPTION_watchdog:
2660 if (watchdog) {
2661 fprintf(stderr,
2662 "qemu: only one watchdog option may be given\n");
2663 return 1;
2665 watchdog = optarg;
2666 break;
2667 case QEMU_OPTION_watchdog_action:
2668 if (select_watchdog_action(optarg) == -1) {
2669 fprintf(stderr, "Unknown -watchdog-action parameter\n");
2670 exit(1);
2672 break;
2673 case QEMU_OPTION_virtiocon:
2674 add_device_config(DEV_VIRTCON, optarg);
2675 default_virtcon = 0;
2676 if (strncmp(optarg, "mon:", 4) == 0) {
2677 default_monitor = 0;
2679 break;
2680 case QEMU_OPTION_parallel:
2681 add_device_config(DEV_PARALLEL, optarg);
2682 default_parallel = 0;
2683 if (strncmp(optarg, "mon:", 4) == 0) {
2684 default_monitor = 0;
2686 break;
2687 case QEMU_OPTION_debugcon:
2688 add_device_config(DEV_DEBUGCON, optarg);
2689 break;
2690 case QEMU_OPTION_loadvm:
2691 loadvm = optarg;
2692 break;
2693 case QEMU_OPTION_full_screen:
2694 full_screen = 1;
2695 break;
2696 #ifdef CONFIG_SDL
2697 case QEMU_OPTION_no_frame:
2698 no_frame = 1;
2699 break;
2700 case QEMU_OPTION_alt_grab:
2701 alt_grab = 1;
2702 break;
2703 case QEMU_OPTION_ctrl_grab:
2704 ctrl_grab = 1;
2705 break;
2706 case QEMU_OPTION_no_quit:
2707 no_quit = 1;
2708 break;
2709 case QEMU_OPTION_sdl:
2710 display_type = DT_SDL;
2711 break;
2712 #else
2713 case QEMU_OPTION_no_frame:
2714 case QEMU_OPTION_alt_grab:
2715 case QEMU_OPTION_ctrl_grab:
2716 case QEMU_OPTION_no_quit:
2717 case QEMU_OPTION_sdl:
2718 fprintf(stderr, "SDL support is disabled\n");
2719 exit(1);
2720 #endif
2721 case QEMU_OPTION_pidfile:
2722 pid_file = optarg;
2723 break;
2724 case QEMU_OPTION_win2k_hack:
2725 win2k_install_hack = 1;
2726 break;
2727 case QEMU_OPTION_rtc_td_hack:
2728 rtc_td_hack = 1;
2729 break;
2730 case QEMU_OPTION_acpitable:
2731 do_acpitable_option(optarg);
2732 break;
2733 case QEMU_OPTION_smbios:
2734 do_smbios_option(optarg);
2735 break;
2736 case QEMU_OPTION_enable_kvm:
2737 olist = qemu_find_opts("machine");
2738 qemu_opts_reset(olist);
2739 qemu_opts_parse(olist, "accel=kvm", 0);
2740 break;
2741 case QEMU_OPTION_machine:
2742 olist = qemu_find_opts("machine");
2743 qemu_opts_reset(olist);
2744 opts = qemu_opts_parse(olist, optarg, 1);
2745 if (!opts) {
2746 fprintf(stderr, "parse error: %s\n", optarg);
2747 exit(1);
2749 optarg = qemu_opt_get(opts, "type");
2750 if (optarg) {
2751 machine = machine_parse(optarg);
2753 break;
2754 case QEMU_OPTION_usb:
2755 usb_enabled = 1;
2756 break;
2757 case QEMU_OPTION_usbdevice:
2758 usb_enabled = 1;
2759 add_device_config(DEV_USB, optarg);
2760 break;
2761 case QEMU_OPTION_device:
2762 if (!qemu_opts_parse(qemu_find_opts("device"), optarg, 1)) {
2763 exit(1);
2765 break;
2766 case QEMU_OPTION_smp:
2767 smp_parse(optarg);
2768 if (smp_cpus < 1) {
2769 fprintf(stderr, "Invalid number of CPUs\n");
2770 exit(1);
2772 if (max_cpus < smp_cpus) {
2773 fprintf(stderr, "maxcpus must be equal to or greater than "
2774 "smp\n");
2775 exit(1);
2777 if (max_cpus > 255) {
2778 fprintf(stderr, "Unsupported number of maxcpus\n");
2779 exit(1);
2781 break;
2782 case QEMU_OPTION_vnc:
2783 #ifdef CONFIG_VNC
2784 display_remote++;
2785 vnc_display = optarg;
2786 #else
2787 fprintf(stderr, "VNC support is disabled\n");
2788 exit(1);
2789 #endif
2790 break;
2791 case QEMU_OPTION_no_acpi:
2792 acpi_enabled = 0;
2793 break;
2794 case QEMU_OPTION_no_hpet:
2795 no_hpet = 1;
2796 break;
2797 case QEMU_OPTION_balloon:
2798 if (balloon_parse(optarg) < 0) {
2799 fprintf(stderr, "Unknown -balloon argument %s\n", optarg);
2800 exit(1);
2802 break;
2803 case QEMU_OPTION_no_reboot:
2804 no_reboot = 1;
2805 break;
2806 case QEMU_OPTION_no_shutdown:
2807 no_shutdown = 1;
2808 break;
2809 case QEMU_OPTION_show_cursor:
2810 cursor_hide = 0;
2811 break;
2812 case QEMU_OPTION_uuid:
2813 if(qemu_uuid_parse(optarg, qemu_uuid) < 0) {
2814 fprintf(stderr, "Fail to parse UUID string."
2815 " Wrong format.\n");
2816 exit(1);
2818 break;
2819 case QEMU_OPTION_option_rom:
2820 if (nb_option_roms >= MAX_OPTION_ROMS) {
2821 fprintf(stderr, "Too many option ROMs\n");
2822 exit(1);
2824 opts = qemu_opts_parse(qemu_find_opts("option-rom"), optarg, 1);
2825 option_rom[nb_option_roms].name = qemu_opt_get(opts, "romfile");
2826 option_rom[nb_option_roms].bootindex =
2827 qemu_opt_get_number(opts, "bootindex", -1);
2828 if (!option_rom[nb_option_roms].name) {
2829 fprintf(stderr, "Option ROM file is not specified\n");
2830 exit(1);
2832 nb_option_roms++;
2833 break;
2834 case QEMU_OPTION_semihosting:
2835 semihosting_enabled = 1;
2836 break;
2837 case QEMU_OPTION_name:
2838 qemu_name = g_strdup(optarg);
2840 char *p = strchr(qemu_name, ',');
2841 if (p != NULL) {
2842 *p++ = 0;
2843 if (strncmp(p, "process=", 8)) {
2844 fprintf(stderr, "Unknown subargument %s to -name\n", p);
2845 exit(1);
2847 p += 8;
2848 os_set_proc_name(p);
2851 break;
2852 case QEMU_OPTION_prom_env:
2853 if (nb_prom_envs >= MAX_PROM_ENVS) {
2854 fprintf(stderr, "Too many prom variables\n");
2855 exit(1);
2857 prom_envs[nb_prom_envs] = optarg;
2858 nb_prom_envs++;
2859 break;
2860 case QEMU_OPTION_old_param:
2861 old_param = 1;
2862 break;
2863 case QEMU_OPTION_clock:
2864 configure_alarms(optarg);
2865 break;
2866 case QEMU_OPTION_startdate:
2867 configure_rtc_date_offset(optarg, 1);
2868 break;
2869 case QEMU_OPTION_rtc:
2870 opts = qemu_opts_parse(qemu_find_opts("rtc"), optarg, 0);
2871 if (!opts) {
2872 exit(1);
2874 configure_rtc(opts);
2875 break;
2876 case QEMU_OPTION_tb_size:
2877 tcg_tb_size = strtol(optarg, NULL, 0);
2878 if (tcg_tb_size < 0) {
2879 tcg_tb_size = 0;
2881 break;
2882 case QEMU_OPTION_icount:
2883 icount_option = optarg;
2884 break;
2885 case QEMU_OPTION_incoming:
2886 incoming = optarg;
2887 incoming_expected = true;
2888 break;
2889 case QEMU_OPTION_nodefaults:
2890 default_serial = 0;
2891 default_parallel = 0;
2892 default_virtcon = 0;
2893 default_monitor = 0;
2894 default_vga = 0;
2895 default_net = 0;
2896 default_floppy = 0;
2897 default_cdrom = 0;
2898 default_sdcard = 0;
2899 break;
2900 case QEMU_OPTION_xen_domid:
2901 if (!(xen_available())) {
2902 printf("Option %s not supported for this target\n", popt->name);
2903 exit(1);
2905 xen_domid = atoi(optarg);
2906 break;
2907 case QEMU_OPTION_xen_create:
2908 if (!(xen_available())) {
2909 printf("Option %s not supported for this target\n", popt->name);
2910 exit(1);
2912 xen_mode = XEN_CREATE;
2913 break;
2914 case QEMU_OPTION_xen_attach:
2915 if (!(xen_available())) {
2916 printf("Option %s not supported for this target\n", popt->name);
2917 exit(1);
2919 xen_mode = XEN_ATTACH;
2920 break;
2921 #ifdef CONFIG_SIMPLE_TRACE
2922 case QEMU_OPTION_trace:
2923 opts = qemu_opts_parse(qemu_find_opts("trace"), optarg, 0);
2924 if (opts) {
2925 trace_file = qemu_opt_get(opts, "file");
2927 break;
2928 #endif
2929 case QEMU_OPTION_readconfig:
2931 int ret = qemu_read_config_file(optarg);
2932 if (ret < 0) {
2933 fprintf(stderr, "read config %s: %s\n", optarg,
2934 strerror(-ret));
2935 exit(1);
2937 break;
2939 case QEMU_OPTION_spice:
2940 olist = qemu_find_opts("spice");
2941 if (!olist) {
2942 fprintf(stderr, "spice is not supported by this qemu build.\n");
2943 exit(1);
2945 opts = qemu_opts_parse(olist, optarg, 0);
2946 if (!opts) {
2947 fprintf(stderr, "parse error: %s\n", optarg);
2948 exit(1);
2950 break;
2951 case QEMU_OPTION_writeconfig:
2953 FILE *fp;
2954 if (strcmp(optarg, "-") == 0) {
2955 fp = stdout;
2956 } else {
2957 fp = fopen(optarg, "w");
2958 if (fp == NULL) {
2959 fprintf(stderr, "open %s: %s\n", optarg, strerror(errno));
2960 exit(1);
2963 qemu_config_write(fp);
2964 fclose(fp);
2965 break;
2967 default:
2968 os_parse_cmd_args(popt->index, optarg);
2972 loc_set_none();
2974 /* Open the logfile at this point, if necessary. We can't open the logfile
2975 * when encountering either of the logging options (-d or -D) because the
2976 * other one may be encountered later on the command line, changing the
2977 * location or level of logging.
2979 if (log_mask) {
2980 if (log_file) {
2981 set_cpu_log_filename(log_file);
2983 set_cpu_log(log_mask);
2986 if (!st_init(trace_file)) {
2987 fprintf(stderr, "warning: unable to initialize simple trace backend\n");
2990 /* If no data_dir is specified then try to find it relative to the
2991 executable path. */
2992 if (!data_dir) {
2993 data_dir = os_find_datadir(argv[0]);
2995 /* If all else fails use the install patch specified when building. */
2996 if (!data_dir) {
2997 data_dir = CONFIG_QEMU_DATADIR;
3001 * Default to max_cpus = smp_cpus, in case the user doesn't
3002 * specify a max_cpus value.
3004 if (!max_cpus)
3005 max_cpus = smp_cpus;
3007 machine->max_cpus = machine->max_cpus ?: 1; /* Default to UP */
3008 if (smp_cpus > machine->max_cpus) {
3009 fprintf(stderr, "Number of SMP cpus requested (%d), exceeds max cpus "
3010 "supported by machine `%s' (%d)\n", smp_cpus, machine->name,
3011 machine->max_cpus);
3012 exit(1);
3016 * Get the default machine options from the machine if it is not already
3017 * specified either by the configuration file or by the command line.
3019 if (machine->default_machine_opts) {
3020 QemuOptsList *list = qemu_find_opts("machine");
3021 const char *p = NULL;
3023 if (!QTAILQ_EMPTY(&list->head)) {
3024 p = qemu_opt_get(QTAILQ_FIRST(&list->head), "accel");
3026 if (p == NULL) {
3027 qemu_opts_reset(list);
3028 opts = qemu_opts_parse(list, machine->default_machine_opts, 0);
3029 if (!opts) {
3030 fprintf(stderr, "parse error for machine %s: %s\n",
3031 machine->name, machine->default_machine_opts);
3032 exit(1);
3037 qemu_opts_foreach(qemu_find_opts("device"), default_driver_check, NULL, 0);
3038 qemu_opts_foreach(qemu_find_opts("global"), default_driver_check, NULL, 0);
3040 if (machine->no_serial) {
3041 default_serial = 0;
3043 if (machine->no_parallel) {
3044 default_parallel = 0;
3046 if (!machine->use_virtcon) {
3047 default_virtcon = 0;
3049 if (machine->no_vga) {
3050 default_vga = 0;
3052 if (machine->no_floppy) {
3053 default_floppy = 0;
3055 if (machine->no_cdrom) {
3056 default_cdrom = 0;
3058 if (machine->no_sdcard) {
3059 default_sdcard = 0;
3062 if (display_type == DT_NOGRAPHIC) {
3063 if (default_parallel)
3064 add_device_config(DEV_PARALLEL, "null");
3065 if (default_serial && default_monitor) {
3066 add_device_config(DEV_SERIAL, "mon:stdio");
3067 } else if (default_virtcon && default_monitor) {
3068 add_device_config(DEV_VIRTCON, "mon:stdio");
3069 } else {
3070 if (default_serial)
3071 add_device_config(DEV_SERIAL, "stdio");
3072 if (default_virtcon)
3073 add_device_config(DEV_VIRTCON, "stdio");
3074 if (default_monitor)
3075 monitor_parse("stdio", "readline");
3077 } else {
3078 if (default_serial)
3079 add_device_config(DEV_SERIAL, "vc:80Cx24C");
3080 if (default_parallel)
3081 add_device_config(DEV_PARALLEL, "vc:80Cx24C");
3082 if (default_monitor)
3083 monitor_parse("vc:80Cx24C", "readline");
3084 if (default_virtcon)
3085 add_device_config(DEV_VIRTCON, "vc:80Cx24C");
3087 if (default_vga)
3088 vga_interface_type = VGA_CIRRUS;
3090 socket_init();
3092 if (qemu_opts_foreach(qemu_find_opts("chardev"), chardev_init_func, NULL, 1) != 0)
3093 exit(1);
3094 #ifdef CONFIG_VIRTFS
3095 if (qemu_opts_foreach(qemu_find_opts("fsdev"), fsdev_init_func, NULL, 1) != 0) {
3096 exit(1);
3098 #endif
3100 os_daemonize();
3102 if (pid_file && qemu_create_pidfile(pid_file) != 0) {
3103 os_pidfile_error();
3104 exit(1);
3107 /* init the memory */
3108 if (ram_size == 0) {
3109 ram_size = DEFAULT_RAM_SIZE * 1024 * 1024;
3112 configure_accelerator();
3114 if (qemu_init_main_loop()) {
3115 fprintf(stderr, "qemu_init_main_loop failed\n");
3116 exit(1);
3118 linux_boot = (kernel_filename != NULL);
3120 if (!linux_boot && *kernel_cmdline != '\0') {
3121 fprintf(stderr, "-append only allowed with -kernel option\n");
3122 exit(1);
3125 if (!linux_boot && initrd_filename != NULL) {
3126 fprintf(stderr, "-initrd only allowed with -kernel option\n");
3127 exit(1);
3130 os_set_line_buffering();
3132 if (init_timer_alarm() < 0) {
3133 fprintf(stderr, "could not initialize alarm timer\n");
3134 exit(1);
3136 configure_icount(icount_option);
3138 if (net_init_clients() < 0) {
3139 exit(1);
3142 /* init the bluetooth world */
3143 if (foreach_device_config(DEV_BT, bt_parse))
3144 exit(1);
3146 if (!xen_enabled()) {
3147 /* On 32-bit hosts, QEMU is limited by virtual address space */
3148 if (ram_size > (2047 << 20) && HOST_LONG_BITS == 32) {
3149 fprintf(stderr, "qemu: at most 2047 MB RAM can be simulated\n");
3150 exit(1);
3154 cpu_exec_init_all();
3156 bdrv_init_with_whitelist();
3158 blk_mig_init();
3160 /* open the virtual block devices */
3161 if (snapshot)
3162 qemu_opts_foreach(qemu_find_opts("drive"), drive_enable_snapshot, NULL, 0);
3163 if (qemu_opts_foreach(qemu_find_opts("drive"), drive_init_func, &machine->use_scsi, 1) != 0)
3164 exit(1);
3166 default_drive(default_cdrom, snapshot, machine->use_scsi,
3167 IF_DEFAULT, 2, CDROM_OPTS);
3168 default_drive(default_floppy, snapshot, machine->use_scsi,
3169 IF_FLOPPY, 0, FD_OPTS);
3170 default_drive(default_sdcard, snapshot, machine->use_scsi,
3171 IF_SD, 0, SD_OPTS);
3173 register_savevm_live(NULL, "ram", 0, 4, NULL, ram_save_live, NULL,
3174 ram_load, NULL);
3176 if (nb_numa_nodes > 0) {
3177 int i;
3179 if (nb_numa_nodes > MAX_NODES) {
3180 nb_numa_nodes = MAX_NODES;
3183 /* If no memory size if given for any node, assume the default case
3184 * and distribute the available memory equally across all nodes
3186 for (i = 0; i < nb_numa_nodes; i++) {
3187 if (node_mem[i] != 0)
3188 break;
3190 if (i == nb_numa_nodes) {
3191 uint64_t usedmem = 0;
3193 /* On Linux, the each node's border has to be 8MB aligned,
3194 * the final node gets the rest.
3196 for (i = 0; i < nb_numa_nodes - 1; i++) {
3197 node_mem[i] = (ram_size / nb_numa_nodes) & ~((1 << 23UL) - 1);
3198 usedmem += node_mem[i];
3200 node_mem[i] = ram_size - usedmem;
3203 for (i = 0; i < nb_numa_nodes; i++) {
3204 if (node_cpumask[i] != 0)
3205 break;
3207 /* assigning the VCPUs round-robin is easier to implement, guest OSes
3208 * must cope with this anyway, because there are BIOSes out there in
3209 * real machines which also use this scheme.
3211 if (i == nb_numa_nodes) {
3212 for (i = 0; i < smp_cpus; i++) {
3213 node_cpumask[i % nb_numa_nodes] |= 1 << i;
3218 if (qemu_opts_foreach(qemu_find_opts("mon"), mon_init_func, NULL, 1) != 0) {
3219 exit(1);
3222 if (foreach_device_config(DEV_SERIAL, serial_parse) < 0)
3223 exit(1);
3224 if (foreach_device_config(DEV_PARALLEL, parallel_parse) < 0)
3225 exit(1);
3226 if (foreach_device_config(DEV_VIRTCON, virtcon_parse) < 0)
3227 exit(1);
3228 if (foreach_device_config(DEV_DEBUGCON, debugcon_parse) < 0)
3229 exit(1);
3231 module_call_init(MODULE_INIT_DEVICE);
3233 if (qemu_opts_foreach(qemu_find_opts("device"), device_help_func, NULL, 0) != 0)
3234 exit(0);
3236 if (watchdog) {
3237 i = select_watchdog(watchdog);
3238 if (i > 0)
3239 exit (i == 1 ? 1 : 0);
3242 if (machine->compat_props) {
3243 qdev_prop_register_global_list(machine->compat_props);
3245 qemu_add_globals();
3247 machine->init(ram_size, boot_devices,
3248 kernel_filename, kernel_cmdline, initrd_filename, cpu_model);
3250 cpu_synchronize_all_post_init();
3252 set_numa_modes();
3254 current_machine = machine;
3256 /* init USB devices */
3257 if (usb_enabled) {
3258 if (foreach_device_config(DEV_USB, usb_parse) < 0)
3259 exit(1);
3262 /* init generic devices */
3263 if (qemu_opts_foreach(qemu_find_opts("device"), device_init_func, NULL, 1) != 0)
3264 exit(1);
3266 net_check_clients();
3268 /* just use the first displaystate for the moment */
3269 ds = get_displaystate();
3271 if (using_spice)
3272 display_remote++;
3273 if (display_type == DT_DEFAULT && !display_remote) {
3274 #if defined(CONFIG_SDL) || defined(CONFIG_COCOA)
3275 display_type = DT_SDL;
3276 #elif defined(CONFIG_VNC)
3277 vnc_display = "localhost:0,to=99";
3278 show_vnc_port = 1;
3279 #else
3280 display_type = DT_NONE;
3281 #endif
3285 /* init local displays */
3286 switch (display_type) {
3287 case DT_NOGRAPHIC:
3288 break;
3289 #if defined(CONFIG_CURSES)
3290 case DT_CURSES:
3291 curses_display_init(ds, full_screen);
3292 break;
3293 #endif
3294 #if defined(CONFIG_SDL)
3295 case DT_SDL:
3296 sdl_display_init(ds, full_screen, no_frame);
3297 break;
3298 #elif defined(CONFIG_COCOA)
3299 case DT_SDL:
3300 cocoa_display_init(ds, full_screen);
3301 break;
3302 #endif
3303 default:
3304 break;
3307 /* must be after terminal init, SDL library changes signal handlers */
3308 os_setup_signal_handling();
3310 #ifdef CONFIG_VNC
3311 /* init remote displays */
3312 if (vnc_display) {
3313 vnc_display_init(ds);
3314 if (vnc_display_open(ds, vnc_display) < 0)
3315 exit(1);
3317 if (show_vnc_port) {
3318 printf("VNC server running on `%s'\n", vnc_display_local_addr(ds));
3321 #endif
3322 #ifdef CONFIG_SPICE
3323 if (using_spice && !qxl_enabled) {
3324 qemu_spice_display_init(ds);
3326 #endif
3328 /* display setup */
3329 dpy_resize(ds);
3330 dcl = ds->listeners;
3331 while (dcl != NULL) {
3332 if (dcl->dpy_refresh != NULL) {
3333 ds->gui_timer = qemu_new_timer_ms(rt_clock, gui_update, ds);
3334 qemu_mod_timer(ds->gui_timer, qemu_get_clock_ms(rt_clock));
3335 break;
3337 dcl = dcl->next;
3339 if (ds->gui_timer == NULL) {
3340 nographic_timer = qemu_new_timer_ms(rt_clock, nographic_update, NULL);
3341 qemu_mod_timer(nographic_timer, qemu_get_clock_ms(rt_clock));
3343 text_consoles_set_display(ds);
3345 if (gdbstub_dev && gdbserver_start(gdbstub_dev) < 0) {
3346 fprintf(stderr, "qemu: could not open gdbserver on device '%s'\n",
3347 gdbstub_dev);
3348 exit(1);
3351 qdev_machine_creation_done();
3353 if (rom_load_all() != 0) {
3354 fprintf(stderr, "rom loading failed\n");
3355 exit(1);
3358 /* TODO: once all bus devices are qdevified, this should be done
3359 * when bus is created by qdev.c */
3360 qemu_register_reset(qbus_reset_all_fn, sysbus_get_default());
3361 qemu_run_machine_init_done_notifiers();
3363 qemu_system_reset(VMRESET_SILENT);
3364 if (loadvm) {
3365 if (load_vmstate(loadvm) < 0) {
3366 autostart = 0;
3370 if (incoming) {
3371 int ret = qemu_start_incoming_migration(incoming);
3372 if (ret < 0) {
3373 fprintf(stderr, "Migration failed. Exit code %s(%d), exiting.\n",
3374 incoming, ret);
3375 exit(ret);
3377 } else if (autostart) {
3378 vm_start();
3381 os_setup_post();
3383 main_loop();
3384 quit_timers();
3385 net_cleanup();
3386 res_free();
3388 return 0;