RunState: Add additional states
[qemu.git] / vl.c
blob059e727cb201cdf2031e8fc97fcc29c126015540
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 <glib.h>
116 #include "hw/hw.h"
117 #include "hw/boards.h"
118 #include "hw/usb.h"
119 #include "hw/pcmcia.h"
120 #include "hw/pc.h"
121 #include "hw/isa.h"
122 #include "hw/baum.h"
123 #include "hw/bt.h"
124 #include "hw/watchdog.h"
125 #include "hw/smbios.h"
126 #include "hw/xen.h"
127 #include "hw/qdev.h"
128 #include "hw/loader.h"
129 #include "bt-host.h"
130 #include "net.h"
131 #include "net/slirp.h"
132 #include "monitor.h"
133 #include "console.h"
134 #include "sysemu.h"
135 #include "gdbstub.h"
136 #include "qemu-timer.h"
137 #include "qemu-char.h"
138 #include "cache-utils.h"
139 #include "block.h"
140 #include "blockdev.h"
141 #include "block-migration.h"
142 #include "dma.h"
143 #include "audio/audio.h"
144 #include "migration.h"
145 #include "kvm.h"
146 #include "qemu-option.h"
147 #include "qemu-config.h"
148 #include "qemu-objects.h"
149 #include "qemu-options.h"
150 #ifdef CONFIG_VIRTFS
151 #include "fsdev/qemu-fsdev.h"
152 #endif
154 #include "disas.h"
156 #include "qemu_socket.h"
158 #include "slirp/libslirp.h"
160 #include "trace.h"
161 #include "trace/control.h"
162 #include "qemu-queue.h"
163 #include "cpus.h"
164 #include "arch_init.h"
166 #include "ui/qemu-spice.h"
168 //#define DEBUG_NET
169 //#define DEBUG_SLIRP
171 #define DEFAULT_RAM_SIZE 128
173 #define MAX_VIRTIO_CONSOLES 1
175 static const char *data_dir;
176 const char *bios_name = NULL;
177 enum vga_retrace_method vga_retrace_method = VGA_RETRACE_DUMB;
178 DisplayType display_type = DT_DEFAULT;
179 int display_remote = 0;
180 const char* keyboard_layout = NULL;
181 ram_addr_t ram_size;
182 const char *mem_path = NULL;
183 #ifdef MAP_POPULATE
184 int mem_prealloc = 0; /* force preallocation of physical target memory */
185 #endif
186 int nb_nics;
187 NICInfo nd_table[MAX_NICS];
188 int vm_running;
189 int autostart;
190 int incoming_expected; /* Started with -incoming and waiting for incoming */
191 static int rtc_utc = 1;
192 static int rtc_date_offset = -1; /* -1 means no change */
193 QEMUClock *rtc_clock;
194 int vga_interface_type = VGA_NONE;
195 static int full_screen = 0;
196 #ifdef CONFIG_SDL
197 static int no_frame = 0;
198 #endif
199 int no_quit = 0;
200 CharDriverState *serial_hds[MAX_SERIAL_PORTS];
201 CharDriverState *parallel_hds[MAX_PARALLEL_PORTS];
202 CharDriverState *virtcon_hds[MAX_VIRTIO_CONSOLES];
203 int win2k_install_hack = 0;
204 int rtc_td_hack = 0;
205 int usb_enabled = 0;
206 int singlestep = 0;
207 int smp_cpus = 1;
208 int max_cpus = 0;
209 int smp_cores = 1;
210 int smp_threads = 1;
211 #ifdef CONFIG_VNC
212 const char *vnc_display;
213 #endif
214 int acpi_enabled = 1;
215 int no_hpet = 0;
216 int fd_bootchk = 1;
217 int no_reboot = 0;
218 int no_shutdown = 0;
219 int cursor_hide = 1;
220 int graphic_rotate = 0;
221 uint8_t irq0override = 1;
222 const char *watchdog;
223 QEMUOptionRom option_rom[MAX_OPTION_ROMS];
224 int nb_option_roms;
225 int semihosting_enabled = 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 int boot_menu;
233 uint8_t *boot_splash_filedata;
234 int boot_splash_filedata_size;
235 uint8_t qemu_extra_params_fw[2];
237 typedef struct FWBootEntry FWBootEntry;
239 struct FWBootEntry {
240 QTAILQ_ENTRY(FWBootEntry) link;
241 int32_t bootindex;
242 DeviceState *dev;
243 char *suffix;
246 QTAILQ_HEAD(, FWBootEntry) fw_boot_order = QTAILQ_HEAD_INITIALIZER(fw_boot_order);
248 int nb_numa_nodes;
249 uint64_t node_mem[MAX_NODES];
250 uint64_t node_cpumask[MAX_NODES];
252 static QEMUTimer *nographic_timer;
254 uint8_t qemu_uuid[16];
256 static QEMUBootSetHandler *boot_set_handler;
257 static void *boot_set_opaque;
259 static NotifierList exit_notifiers =
260 NOTIFIER_LIST_INITIALIZER(exit_notifiers);
262 static NotifierList machine_init_done_notifiers =
263 NOTIFIER_LIST_INITIALIZER(machine_init_done_notifiers);
265 static int tcg_allowed = 1;
266 int kvm_allowed = 0;
267 int xen_allowed = 0;
268 uint32_t xen_domid;
269 enum xen_mode xen_mode = XEN_EMULATE;
270 static int tcg_tb_size;
272 static int default_serial = 1;
273 static int default_parallel = 1;
274 static int default_virtcon = 1;
275 static int default_monitor = 1;
276 static int default_vga = 1;
277 static int default_floppy = 1;
278 static int default_cdrom = 1;
279 static int default_sdcard = 1;
281 static struct {
282 const char *driver;
283 int *flag;
284 } default_list[] = {
285 { .driver = "isa-serial", .flag = &default_serial },
286 { .driver = "isa-parallel", .flag = &default_parallel },
287 { .driver = "isa-fdc", .flag = &default_floppy },
288 { .driver = "ide-cd", .flag = &default_cdrom },
289 { .driver = "ide-hd", .flag = &default_cdrom },
290 { .driver = "ide-drive", .flag = &default_cdrom },
291 { .driver = "scsi-cd", .flag = &default_cdrom },
292 { .driver = "virtio-serial-pci", .flag = &default_virtcon },
293 { .driver = "virtio-serial-s390", .flag = &default_virtcon },
294 { .driver = "virtio-serial", .flag = &default_virtcon },
295 { .driver = "VGA", .flag = &default_vga },
296 { .driver = "cirrus-vga", .flag = &default_vga },
297 { .driver = "vmware-svga", .flag = &default_vga },
298 { .driver = "isa-vga", .flag = &default_vga },
299 { .driver = "qxl-vga", .flag = &default_vga },
302 static void res_free(void)
304 if (boot_splash_filedata != NULL) {
305 g_free(boot_splash_filedata);
306 boot_splash_filedata = NULL;
310 static int default_driver_check(QemuOpts *opts, void *opaque)
312 const char *driver = qemu_opt_get(opts, "driver");
313 int i;
315 if (!driver)
316 return 0;
317 for (i = 0; i < ARRAY_SIZE(default_list); i++) {
318 if (strcmp(default_list[i].driver, driver) != 0)
319 continue;
320 *(default_list[i].flag) = 0;
322 return 0;
325 /***********************************************************/
326 /* QEMU state */
328 static RunState current_run_state = RSTATE_NO_STATE;
330 bool runstate_check(RunState state)
332 return current_run_state == state;
335 void runstate_set(RunState new_state)
337 assert(new_state < RSTATE_MAX);
338 current_run_state = new_state;
341 /***********************************************************/
342 /* real time host monotonic timer */
344 /***********************************************************/
345 /* host time/date access */
346 void qemu_get_timedate(struct tm *tm, int offset)
348 time_t ti;
349 struct tm *ret;
351 time(&ti);
352 ti += offset;
353 if (rtc_date_offset == -1) {
354 if (rtc_utc)
355 ret = gmtime(&ti);
356 else
357 ret = localtime(&ti);
358 } else {
359 ti -= rtc_date_offset;
360 ret = gmtime(&ti);
363 memcpy(tm, ret, sizeof(struct tm));
366 int qemu_timedate_diff(struct tm *tm)
368 time_t seconds;
370 if (rtc_date_offset == -1)
371 if (rtc_utc)
372 seconds = mktimegm(tm);
373 else
374 seconds = mktime(tm);
375 else
376 seconds = mktimegm(tm) + rtc_date_offset;
378 return seconds - time(NULL);
381 void rtc_change_mon_event(struct tm *tm)
383 QObject *data;
385 data = qobject_from_jsonf("{ 'offset': %d }", qemu_timedate_diff(tm));
386 monitor_protocol_event(QEVENT_RTC_CHANGE, data);
387 qobject_decref(data);
390 static void configure_rtc_date_offset(const char *startdate, int legacy)
392 time_t rtc_start_date;
393 struct tm tm;
395 if (!strcmp(startdate, "now") && legacy) {
396 rtc_date_offset = -1;
397 } else {
398 if (sscanf(startdate, "%d-%d-%dT%d:%d:%d",
399 &tm.tm_year,
400 &tm.tm_mon,
401 &tm.tm_mday,
402 &tm.tm_hour,
403 &tm.tm_min,
404 &tm.tm_sec) == 6) {
405 /* OK */
406 } else if (sscanf(startdate, "%d-%d-%d",
407 &tm.tm_year,
408 &tm.tm_mon,
409 &tm.tm_mday) == 3) {
410 tm.tm_hour = 0;
411 tm.tm_min = 0;
412 tm.tm_sec = 0;
413 } else {
414 goto date_fail;
416 tm.tm_year -= 1900;
417 tm.tm_mon--;
418 rtc_start_date = mktimegm(&tm);
419 if (rtc_start_date == -1) {
420 date_fail:
421 fprintf(stderr, "Invalid date format. Valid formats are:\n"
422 "'2006-06-17T16:01:21' or '2006-06-17'\n");
423 exit(1);
425 rtc_date_offset = time(NULL) - rtc_start_date;
429 static void configure_rtc(QemuOpts *opts)
431 const char *value;
433 value = qemu_opt_get(opts, "base");
434 if (value) {
435 if (!strcmp(value, "utc")) {
436 rtc_utc = 1;
437 } else if (!strcmp(value, "localtime")) {
438 rtc_utc = 0;
439 } else {
440 configure_rtc_date_offset(value, 0);
443 value = qemu_opt_get(opts, "clock");
444 if (value) {
445 if (!strcmp(value, "host")) {
446 rtc_clock = host_clock;
447 } else if (!strcmp(value, "vm")) {
448 rtc_clock = vm_clock;
449 } else {
450 fprintf(stderr, "qemu: invalid option value '%s'\n", value);
451 exit(1);
454 value = qemu_opt_get(opts, "driftfix");
455 if (value) {
456 if (!strcmp(value, "slew")) {
457 rtc_td_hack = 1;
458 } else if (!strcmp(value, "none")) {
459 rtc_td_hack = 0;
460 } else {
461 fprintf(stderr, "qemu: invalid option value '%s'\n", value);
462 exit(1);
467 /***********************************************************/
468 /* Bluetooth support */
469 static int nb_hcis;
470 static int cur_hci;
471 static struct HCIInfo *hci_table[MAX_NICS];
473 static struct bt_vlan_s {
474 struct bt_scatternet_s net;
475 int id;
476 struct bt_vlan_s *next;
477 } *first_bt_vlan;
479 /* find or alloc a new bluetooth "VLAN" */
480 static struct bt_scatternet_s *qemu_find_bt_vlan(int id)
482 struct bt_vlan_s **pvlan, *vlan;
483 for (vlan = first_bt_vlan; vlan != NULL; vlan = vlan->next) {
484 if (vlan->id == id)
485 return &vlan->net;
487 vlan = g_malloc0(sizeof(struct bt_vlan_s));
488 vlan->id = id;
489 pvlan = &first_bt_vlan;
490 while (*pvlan != NULL)
491 pvlan = &(*pvlan)->next;
492 *pvlan = vlan;
493 return &vlan->net;
496 static void null_hci_send(struct HCIInfo *hci, const uint8_t *data, int len)
500 static int null_hci_addr_set(struct HCIInfo *hci, const uint8_t *bd_addr)
502 return -ENOTSUP;
505 static struct HCIInfo null_hci = {
506 .cmd_send = null_hci_send,
507 .sco_send = null_hci_send,
508 .acl_send = null_hci_send,
509 .bdaddr_set = null_hci_addr_set,
512 struct HCIInfo *qemu_next_hci(void)
514 if (cur_hci == nb_hcis)
515 return &null_hci;
517 return hci_table[cur_hci++];
520 static struct HCIInfo *hci_init(const char *str)
522 char *endp;
523 struct bt_scatternet_s *vlan = 0;
525 if (!strcmp(str, "null"))
526 /* null */
527 return &null_hci;
528 else if (!strncmp(str, "host", 4) && (str[4] == '\0' || str[4] == ':'))
529 /* host[:hciN] */
530 return bt_host_hci(str[4] ? str + 5 : "hci0");
531 else if (!strncmp(str, "hci", 3)) {
532 /* hci[,vlan=n] */
533 if (str[3]) {
534 if (!strncmp(str + 3, ",vlan=", 6)) {
535 vlan = qemu_find_bt_vlan(strtol(str + 9, &endp, 0));
536 if (*endp)
537 vlan = 0;
539 } else
540 vlan = qemu_find_bt_vlan(0);
541 if (vlan)
542 return bt_new_hci(vlan);
545 fprintf(stderr, "qemu: Unknown bluetooth HCI `%s'.\n", str);
547 return 0;
550 static int bt_hci_parse(const char *str)
552 struct HCIInfo *hci;
553 bdaddr_t bdaddr;
555 if (nb_hcis >= MAX_NICS) {
556 fprintf(stderr, "qemu: Too many bluetooth HCIs (max %i).\n", MAX_NICS);
557 return -1;
560 hci = hci_init(str);
561 if (!hci)
562 return -1;
564 bdaddr.b[0] = 0x52;
565 bdaddr.b[1] = 0x54;
566 bdaddr.b[2] = 0x00;
567 bdaddr.b[3] = 0x12;
568 bdaddr.b[4] = 0x34;
569 bdaddr.b[5] = 0x56 + nb_hcis;
570 hci->bdaddr_set(hci, bdaddr.b);
572 hci_table[nb_hcis++] = hci;
574 return 0;
577 static void bt_vhci_add(int vlan_id)
579 struct bt_scatternet_s *vlan = qemu_find_bt_vlan(vlan_id);
581 if (!vlan->slave)
582 fprintf(stderr, "qemu: warning: adding a VHCI to "
583 "an empty scatternet %i\n", vlan_id);
585 bt_vhci_init(bt_new_hci(vlan));
588 static struct bt_device_s *bt_device_add(const char *opt)
590 struct bt_scatternet_s *vlan;
591 int vlan_id = 0;
592 char *endp = strstr(opt, ",vlan=");
593 int len = (endp ? endp - opt : strlen(opt)) + 1;
594 char devname[10];
596 pstrcpy(devname, MIN(sizeof(devname), len), opt);
598 if (endp) {
599 vlan_id = strtol(endp + 6, &endp, 0);
600 if (*endp) {
601 fprintf(stderr, "qemu: unrecognised bluetooth vlan Id\n");
602 return 0;
606 vlan = qemu_find_bt_vlan(vlan_id);
608 if (!vlan->slave)
609 fprintf(stderr, "qemu: warning: adding a slave device to "
610 "an empty scatternet %i\n", vlan_id);
612 if (!strcmp(devname, "keyboard"))
613 return bt_keyboard_init(vlan);
615 fprintf(stderr, "qemu: unsupported bluetooth device `%s'\n", devname);
616 return 0;
619 static int bt_parse(const char *opt)
621 const char *endp, *p;
622 int vlan;
624 if (strstart(opt, "hci", &endp)) {
625 if (!*endp || *endp == ',') {
626 if (*endp)
627 if (!strstart(endp, ",vlan=", 0))
628 opt = endp + 1;
630 return bt_hci_parse(opt);
632 } else if (strstart(opt, "vhci", &endp)) {
633 if (!*endp || *endp == ',') {
634 if (*endp) {
635 if (strstart(endp, ",vlan=", &p)) {
636 vlan = strtol(p, (char **) &endp, 0);
637 if (*endp) {
638 fprintf(stderr, "qemu: bad scatternet '%s'\n", p);
639 return 1;
641 } else {
642 fprintf(stderr, "qemu: bad parameter '%s'\n", endp + 1);
643 return 1;
645 } else
646 vlan = 0;
648 bt_vhci_add(vlan);
649 return 0;
651 } else if (strstart(opt, "device:", &endp))
652 return !bt_device_add(endp);
654 fprintf(stderr, "qemu: bad bluetooth parameter '%s'\n", opt);
655 return 1;
658 /***********************************************************/
659 /* QEMU Block devices */
661 #define HD_OPTS "media=disk"
662 #define CDROM_OPTS "media=cdrom"
663 #define FD_OPTS ""
664 #define PFLASH_OPTS ""
665 #define MTD_OPTS ""
666 #define SD_OPTS ""
668 static int drive_init_func(QemuOpts *opts, void *opaque)
670 int *use_scsi = opaque;
672 return drive_init(opts, *use_scsi) == NULL;
675 static int drive_enable_snapshot(QemuOpts *opts, void *opaque)
677 if (NULL == qemu_opt_get(opts, "snapshot")) {
678 qemu_opt_set(opts, "snapshot", "on");
680 return 0;
683 static void default_drive(int enable, int snapshot, int use_scsi,
684 BlockInterfaceType type, int index,
685 const char *optstr)
687 QemuOpts *opts;
689 if (type == IF_DEFAULT) {
690 type = use_scsi ? IF_SCSI : IF_IDE;
693 if (!enable || drive_get_by_index(type, index)) {
694 return;
697 opts = drive_add(type, index, NULL, optstr);
698 if (snapshot) {
699 drive_enable_snapshot(opts, NULL);
701 if (!drive_init(opts, use_scsi)) {
702 exit(1);
706 void qemu_register_boot_set(QEMUBootSetHandler *func, void *opaque)
708 boot_set_handler = func;
709 boot_set_opaque = opaque;
712 int qemu_boot_set(const char *boot_devices)
714 if (!boot_set_handler) {
715 return -EINVAL;
717 return boot_set_handler(boot_set_opaque, boot_devices);
720 static void validate_bootdevices(char *devices)
722 /* We just do some generic consistency checks */
723 const char *p;
724 int bitmap = 0;
726 for (p = devices; *p != '\0'; p++) {
727 /* Allowed boot devices are:
728 * a-b: floppy disk drives
729 * c-f: IDE disk drives
730 * g-m: machine implementation dependant drives
731 * n-p: network devices
732 * It's up to each machine implementation to check if the given boot
733 * devices match the actual hardware implementation and firmware
734 * features.
736 if (*p < 'a' || *p > 'p') {
737 fprintf(stderr, "Invalid boot device '%c'\n", *p);
738 exit(1);
740 if (bitmap & (1 << (*p - 'a'))) {
741 fprintf(stderr, "Boot device '%c' was given twice\n", *p);
742 exit(1);
744 bitmap |= 1 << (*p - 'a');
748 static void restore_boot_devices(void *opaque)
750 char *standard_boot_devices = opaque;
751 static int first = 1;
753 /* Restore boot order and remove ourselves after the first boot */
754 if (first) {
755 first = 0;
756 return;
759 qemu_boot_set(standard_boot_devices);
761 qemu_unregister_reset(restore_boot_devices, standard_boot_devices);
762 g_free(standard_boot_devices);
765 void add_boot_device_path(int32_t bootindex, DeviceState *dev,
766 const char *suffix)
768 FWBootEntry *node, *i;
770 if (bootindex < 0) {
771 return;
774 assert(dev != NULL || suffix != NULL);
776 node = g_malloc0(sizeof(FWBootEntry));
777 node->bootindex = bootindex;
778 node->suffix = suffix ? g_strdup(suffix) : NULL;
779 node->dev = dev;
781 QTAILQ_FOREACH(i, &fw_boot_order, link) {
782 if (i->bootindex == bootindex) {
783 fprintf(stderr, "Two devices with same boot index %d\n", bootindex);
784 exit(1);
785 } else if (i->bootindex < bootindex) {
786 continue;
788 QTAILQ_INSERT_BEFORE(i, node, link);
789 return;
791 QTAILQ_INSERT_TAIL(&fw_boot_order, node, link);
795 * This function returns null terminated string that consist of new line
796 * separated device paths.
798 * memory pointed by "size" is assigned total length of the array in bytes
801 char *get_boot_devices_list(uint32_t *size)
803 FWBootEntry *i;
804 uint32_t total = 0;
805 char *list = NULL;
807 QTAILQ_FOREACH(i, &fw_boot_order, link) {
808 char *devpath = NULL, *bootpath;
809 int len;
811 if (i->dev) {
812 devpath = qdev_get_fw_dev_path(i->dev);
813 assert(devpath);
816 if (i->suffix && devpath) {
817 size_t bootpathlen = strlen(devpath) + strlen(i->suffix) + 1;
819 bootpath = g_malloc(bootpathlen);
820 snprintf(bootpath, bootpathlen, "%s%s", devpath, i->suffix);
821 g_free(devpath);
822 } else if (devpath) {
823 bootpath = devpath;
824 } else {
825 bootpath = g_strdup(i->suffix);
826 assert(bootpath);
829 if (total) {
830 list[total-1] = '\n';
832 len = strlen(bootpath) + 1;
833 list = g_realloc(list, total + len);
834 memcpy(&list[total], bootpath, len);
835 total += len;
836 g_free(bootpath);
839 *size = total;
841 return list;
844 static void numa_add(const char *optarg)
846 char option[128];
847 char *endptr;
848 unsigned long long value, endvalue;
849 int nodenr;
851 optarg = get_opt_name(option, 128, optarg, ',') + 1;
852 if (!strcmp(option, "node")) {
853 if (get_param_value(option, 128, "nodeid", optarg) == 0) {
854 nodenr = nb_numa_nodes;
855 } else {
856 nodenr = strtoull(option, NULL, 10);
859 if (get_param_value(option, 128, "mem", optarg) == 0) {
860 node_mem[nodenr] = 0;
861 } else {
862 int64_t sval;
863 sval = strtosz(option, NULL);
864 if (sval < 0) {
865 fprintf(stderr, "qemu: invalid numa mem size: %s\n", optarg);
866 exit(1);
868 node_mem[nodenr] = sval;
870 if (get_param_value(option, 128, "cpus", optarg) == 0) {
871 node_cpumask[nodenr] = 0;
872 } else {
873 value = strtoull(option, &endptr, 10);
874 if (value >= 64) {
875 value = 63;
876 fprintf(stderr, "only 64 CPUs in NUMA mode supported.\n");
877 } else {
878 if (*endptr == '-') {
879 endvalue = strtoull(endptr+1, &endptr, 10);
880 if (endvalue >= 63) {
881 endvalue = 62;
882 fprintf(stderr,
883 "only 63 CPUs in NUMA mode supported.\n");
885 value = (2ULL << endvalue) - (1ULL << value);
886 } else {
887 value = 1ULL << value;
890 node_cpumask[nodenr] = value;
892 nb_numa_nodes++;
894 return;
897 static void smp_parse(const char *optarg)
899 int smp, sockets = 0, threads = 0, cores = 0;
900 char *endptr;
901 char option[128];
903 smp = strtoul(optarg, &endptr, 10);
904 if (endptr != optarg) {
905 if (*endptr == ',') {
906 endptr++;
909 if (get_param_value(option, 128, "sockets", endptr) != 0)
910 sockets = strtoull(option, NULL, 10);
911 if (get_param_value(option, 128, "cores", endptr) != 0)
912 cores = strtoull(option, NULL, 10);
913 if (get_param_value(option, 128, "threads", endptr) != 0)
914 threads = strtoull(option, NULL, 10);
915 if (get_param_value(option, 128, "maxcpus", endptr) != 0)
916 max_cpus = strtoull(option, NULL, 10);
918 /* compute missing values, prefer sockets over cores over threads */
919 if (smp == 0 || sockets == 0) {
920 sockets = sockets > 0 ? sockets : 1;
921 cores = cores > 0 ? cores : 1;
922 threads = threads > 0 ? threads : 1;
923 if (smp == 0) {
924 smp = cores * threads * sockets;
926 } else {
927 if (cores == 0) {
928 threads = threads > 0 ? threads : 1;
929 cores = smp / (sockets * threads);
930 } else {
931 threads = smp / (cores * sockets);
934 smp_cpus = smp;
935 smp_cores = cores > 0 ? cores : 1;
936 smp_threads = threads > 0 ? threads : 1;
937 if (max_cpus == 0)
938 max_cpus = smp_cpus;
941 /***********************************************************/
942 /* USB devices */
944 static int usb_device_add(const char *devname)
946 const char *p;
947 USBDevice *dev = NULL;
949 if (!usb_enabled)
950 return -1;
952 /* drivers with .usbdevice_name entry in USBDeviceInfo */
953 dev = usbdevice_create(devname);
954 if (dev)
955 goto done;
957 /* the other ones */
958 #ifndef CONFIG_LINUX
959 /* only the linux version is qdev-ified, usb-bsd still needs this */
960 if (strstart(devname, "host:", &p)) {
961 dev = usb_host_device_open(p);
962 } else
963 #endif
964 if (!strcmp(devname, "bt") || strstart(devname, "bt:", &p)) {
965 dev = usb_bt_init(devname[2] ? hci_init(p) :
966 bt_new_hci(qemu_find_bt_vlan(0)));
967 } else {
968 return -1;
970 if (!dev)
971 return -1;
973 done:
974 return 0;
977 static int usb_device_del(const char *devname)
979 int bus_num, addr;
980 const char *p;
982 if (strstart(devname, "host:", &p))
983 return usb_host_device_close(p);
985 if (!usb_enabled)
986 return -1;
988 p = strchr(devname, '.');
989 if (!p)
990 return -1;
991 bus_num = strtoul(devname, NULL, 0);
992 addr = strtoul(p + 1, NULL, 0);
994 return usb_device_delete_addr(bus_num, addr);
997 static int usb_parse(const char *cmdline)
999 int r;
1000 r = usb_device_add(cmdline);
1001 if (r < 0) {
1002 fprintf(stderr, "qemu: could not add USB device '%s'\n", cmdline);
1004 return r;
1007 void do_usb_add(Monitor *mon, const QDict *qdict)
1009 const char *devname = qdict_get_str(qdict, "devname");
1010 if (usb_device_add(devname) < 0) {
1011 error_report("could not add USB device '%s'", devname);
1015 void do_usb_del(Monitor *mon, const QDict *qdict)
1017 const char *devname = qdict_get_str(qdict, "devname");
1018 if (usb_device_del(devname) < 0) {
1019 error_report("could not delete USB device '%s'", devname);
1023 /***********************************************************/
1024 /* PCMCIA/Cardbus */
1026 static struct pcmcia_socket_entry_s {
1027 PCMCIASocket *socket;
1028 struct pcmcia_socket_entry_s *next;
1029 } *pcmcia_sockets = 0;
1031 void pcmcia_socket_register(PCMCIASocket *socket)
1033 struct pcmcia_socket_entry_s *entry;
1035 entry = g_malloc(sizeof(struct pcmcia_socket_entry_s));
1036 entry->socket = socket;
1037 entry->next = pcmcia_sockets;
1038 pcmcia_sockets = entry;
1041 void pcmcia_socket_unregister(PCMCIASocket *socket)
1043 struct pcmcia_socket_entry_s *entry, **ptr;
1045 ptr = &pcmcia_sockets;
1046 for (entry = *ptr; entry; ptr = &entry->next, entry = *ptr)
1047 if (entry->socket == socket) {
1048 *ptr = entry->next;
1049 g_free(entry);
1053 void pcmcia_info(Monitor *mon)
1055 struct pcmcia_socket_entry_s *iter;
1057 if (!pcmcia_sockets)
1058 monitor_printf(mon, "No PCMCIA sockets\n");
1060 for (iter = pcmcia_sockets; iter; iter = iter->next)
1061 monitor_printf(mon, "%s: %s\n", iter->socket->slot_string,
1062 iter->socket->attached ? iter->socket->card_string :
1063 "Empty");
1066 /***********************************************************/
1067 /* machine registration */
1069 static QEMUMachine *first_machine = NULL;
1070 QEMUMachine *current_machine = NULL;
1072 int qemu_register_machine(QEMUMachine *m)
1074 QEMUMachine **pm;
1075 pm = &first_machine;
1076 while (*pm != NULL)
1077 pm = &(*pm)->next;
1078 m->next = NULL;
1079 *pm = m;
1080 return 0;
1083 static QEMUMachine *find_machine(const char *name)
1085 QEMUMachine *m;
1087 for(m = first_machine; m != NULL; m = m->next) {
1088 if (!strcmp(m->name, name))
1089 return m;
1090 if (m->alias && !strcmp(m->alias, name))
1091 return m;
1093 return NULL;
1096 static QEMUMachine *find_default_machine(void)
1098 QEMUMachine *m;
1100 for(m = first_machine; m != NULL; m = m->next) {
1101 if (m->is_default) {
1102 return m;
1105 return NULL;
1108 /***********************************************************/
1109 /* main execution loop */
1111 static void gui_update(void *opaque)
1113 uint64_t interval = GUI_REFRESH_INTERVAL;
1114 DisplayState *ds = opaque;
1115 DisplayChangeListener *dcl = ds->listeners;
1117 qemu_flush_coalesced_mmio_buffer();
1118 dpy_refresh(ds);
1120 while (dcl != NULL) {
1121 if (dcl->gui_timer_interval &&
1122 dcl->gui_timer_interval < interval)
1123 interval = dcl->gui_timer_interval;
1124 dcl = dcl->next;
1126 qemu_mod_timer(ds->gui_timer, interval + qemu_get_clock_ms(rt_clock));
1129 static void nographic_update(void *opaque)
1131 uint64_t interval = GUI_REFRESH_INTERVAL;
1133 qemu_flush_coalesced_mmio_buffer();
1134 qemu_mod_timer(nographic_timer, interval + qemu_get_clock_ms(rt_clock));
1137 struct vm_change_state_entry {
1138 VMChangeStateHandler *cb;
1139 void *opaque;
1140 QLIST_ENTRY (vm_change_state_entry) entries;
1143 static QLIST_HEAD(vm_change_state_head, vm_change_state_entry) vm_change_state_head;
1145 VMChangeStateEntry *qemu_add_vm_change_state_handler(VMChangeStateHandler *cb,
1146 void *opaque)
1148 VMChangeStateEntry *e;
1150 e = g_malloc0(sizeof (*e));
1152 e->cb = cb;
1153 e->opaque = opaque;
1154 QLIST_INSERT_HEAD(&vm_change_state_head, e, entries);
1155 return e;
1158 void qemu_del_vm_change_state_handler(VMChangeStateEntry *e)
1160 QLIST_REMOVE (e, entries);
1161 g_free (e);
1164 void vm_state_notify(int running, RunState state)
1166 VMChangeStateEntry *e;
1168 trace_vm_state_notify(running, state);
1170 for (e = vm_change_state_head.lh_first; e; e = e->entries.le_next) {
1171 e->cb(e->opaque, running, state);
1175 void vm_start(void)
1177 if (!vm_running) {
1178 cpu_enable_ticks();
1179 vm_running = 1;
1180 runstate_set(RSTATE_RUNNING);
1181 vm_state_notify(1, RSTATE_RUNNING);
1182 resume_all_vcpus();
1183 monitor_protocol_event(QEVENT_RESUME, NULL);
1187 /* reset/shutdown handler */
1189 typedef struct QEMUResetEntry {
1190 QTAILQ_ENTRY(QEMUResetEntry) entry;
1191 QEMUResetHandler *func;
1192 void *opaque;
1193 } QEMUResetEntry;
1195 static QTAILQ_HEAD(reset_handlers, QEMUResetEntry) reset_handlers =
1196 QTAILQ_HEAD_INITIALIZER(reset_handlers);
1197 static int reset_requested;
1198 static int shutdown_requested, shutdown_signal = -1;
1199 static pid_t shutdown_pid;
1200 static int powerdown_requested;
1201 static int debug_requested;
1202 static RunState vmstop_requested = RSTATE_NO_STATE;
1204 int qemu_shutdown_requested_get(void)
1206 return shutdown_requested;
1209 int qemu_reset_requested_get(void)
1211 return reset_requested;
1214 int qemu_shutdown_requested(void)
1216 int r = shutdown_requested;
1217 shutdown_requested = 0;
1218 return r;
1221 void qemu_kill_report(void)
1223 if (shutdown_signal != -1) {
1224 fprintf(stderr, "qemu: terminating on signal %d", shutdown_signal);
1225 if (shutdown_pid == 0) {
1226 /* This happens for eg ^C at the terminal, so it's worth
1227 * avoiding printing an odd message in that case.
1229 fputc('\n', stderr);
1230 } else {
1231 fprintf(stderr, " from pid " FMT_pid "\n", shutdown_pid);
1233 shutdown_signal = -1;
1237 int qemu_reset_requested(void)
1239 int r = reset_requested;
1240 reset_requested = 0;
1241 return r;
1244 int qemu_powerdown_requested(void)
1246 int r = powerdown_requested;
1247 powerdown_requested = 0;
1248 return r;
1251 static int qemu_debug_requested(void)
1253 int r = debug_requested;
1254 debug_requested = 0;
1255 return r;
1258 static RunState qemu_vmstop_requested(void)
1260 RunState s = vmstop_requested;
1261 vmstop_requested = RSTATE_NO_STATE;
1262 return s;
1265 void qemu_register_reset(QEMUResetHandler *func, void *opaque)
1267 QEMUResetEntry *re = g_malloc0(sizeof(QEMUResetEntry));
1269 re->func = func;
1270 re->opaque = opaque;
1271 QTAILQ_INSERT_TAIL(&reset_handlers, re, entry);
1274 void qemu_unregister_reset(QEMUResetHandler *func, void *opaque)
1276 QEMUResetEntry *re;
1278 QTAILQ_FOREACH(re, &reset_handlers, entry) {
1279 if (re->func == func && re->opaque == opaque) {
1280 QTAILQ_REMOVE(&reset_handlers, re, entry);
1281 g_free(re);
1282 return;
1287 void qemu_system_reset(bool report)
1289 QEMUResetEntry *re, *nre;
1291 /* reset all devices */
1292 QTAILQ_FOREACH_SAFE(re, &reset_handlers, entry, nre) {
1293 re->func(re->opaque);
1295 if (report) {
1296 monitor_protocol_event(QEVENT_RESET, NULL);
1298 cpu_synchronize_all_post_reset();
1301 void qemu_system_reset_request(void)
1303 if (no_reboot) {
1304 shutdown_requested = 1;
1305 } else {
1306 reset_requested = 1;
1308 cpu_stop_current();
1309 qemu_notify_event();
1312 void qemu_system_killed(int signal, pid_t pid)
1314 shutdown_signal = signal;
1315 shutdown_pid = pid;
1316 qemu_system_shutdown_request();
1319 void qemu_system_shutdown_request(void)
1321 shutdown_requested = 1;
1322 qemu_notify_event();
1325 void qemu_system_powerdown_request(void)
1327 powerdown_requested = 1;
1328 qemu_notify_event();
1331 void qemu_system_debug_request(void)
1333 debug_requested = 1;
1334 qemu_notify_event();
1337 void qemu_system_vmstop_request(RunState state)
1339 vmstop_requested = state;
1340 qemu_notify_event();
1343 static GPollFD poll_fds[1024 * 2]; /* this is probably overkill */
1344 static int n_poll_fds;
1345 static int max_priority;
1347 static void glib_select_fill(int *max_fd, fd_set *rfds, fd_set *wfds,
1348 fd_set *xfds, struct timeval *tv)
1350 GMainContext *context = g_main_context_default();
1351 int i;
1352 int timeout = 0, cur_timeout;
1354 g_main_context_prepare(context, &max_priority);
1356 n_poll_fds = g_main_context_query(context, max_priority, &timeout,
1357 poll_fds, ARRAY_SIZE(poll_fds));
1358 g_assert(n_poll_fds <= ARRAY_SIZE(poll_fds));
1360 for (i = 0; i < n_poll_fds; i++) {
1361 GPollFD *p = &poll_fds[i];
1363 if ((p->events & G_IO_IN)) {
1364 FD_SET(p->fd, rfds);
1365 *max_fd = MAX(*max_fd, p->fd);
1367 if ((p->events & G_IO_OUT)) {
1368 FD_SET(p->fd, wfds);
1369 *max_fd = MAX(*max_fd, p->fd);
1371 if ((p->events & G_IO_ERR)) {
1372 FD_SET(p->fd, xfds);
1373 *max_fd = MAX(*max_fd, p->fd);
1377 cur_timeout = (tv->tv_sec * 1000) + ((tv->tv_usec + 500) / 1000);
1378 if (timeout >= 0 && timeout < cur_timeout) {
1379 tv->tv_sec = timeout / 1000;
1380 tv->tv_usec = (timeout % 1000) * 1000;
1384 static void glib_select_poll(fd_set *rfds, fd_set *wfds, fd_set *xfds,
1385 bool err)
1387 GMainContext *context = g_main_context_default();
1389 if (!err) {
1390 int i;
1392 for (i = 0; i < n_poll_fds; i++) {
1393 GPollFD *p = &poll_fds[i];
1395 if ((p->events & G_IO_IN) && FD_ISSET(p->fd, rfds)) {
1396 p->revents |= G_IO_IN;
1398 if ((p->events & G_IO_OUT) && FD_ISSET(p->fd, wfds)) {
1399 p->revents |= G_IO_OUT;
1401 if ((p->events & G_IO_ERR) && FD_ISSET(p->fd, xfds)) {
1402 p->revents |= G_IO_ERR;
1407 if (g_main_context_check(context, max_priority, poll_fds, n_poll_fds)) {
1408 g_main_context_dispatch(context);
1412 int main_loop_wait(int nonblocking)
1414 fd_set rfds, wfds, xfds;
1415 int ret, nfds;
1416 struct timeval tv;
1417 int timeout;
1419 if (nonblocking)
1420 timeout = 0;
1421 else {
1422 timeout = qemu_calculate_timeout();
1423 qemu_bh_update_timeout(&timeout);
1426 os_host_main_loop_wait(&timeout);
1428 tv.tv_sec = timeout / 1000;
1429 tv.tv_usec = (timeout % 1000) * 1000;
1431 /* poll any events */
1432 /* XXX: separate device handlers from system ones */
1433 nfds = -1;
1434 FD_ZERO(&rfds);
1435 FD_ZERO(&wfds);
1436 FD_ZERO(&xfds);
1438 qemu_iohandler_fill(&nfds, &rfds, &wfds, &xfds);
1439 slirp_select_fill(&nfds, &rfds, &wfds, &xfds);
1440 glib_select_fill(&nfds, &rfds, &wfds, &xfds, &tv);
1442 if (timeout > 0) {
1443 qemu_mutex_unlock_iothread();
1446 ret = select(nfds + 1, &rfds, &wfds, &xfds, &tv);
1448 if (timeout > 0) {
1449 qemu_mutex_lock_iothread();
1452 qemu_iohandler_poll(&rfds, &wfds, &xfds, ret);
1453 slirp_select_poll(&rfds, &wfds, &xfds, (ret < 0));
1454 glib_select_poll(&rfds, &wfds, &xfds, (ret < 0));
1456 qemu_run_all_timers();
1458 /* Check bottom-halves last in case any of the earlier events triggered
1459 them. */
1460 qemu_bh_poll();
1462 return ret;
1465 qemu_irq qemu_system_powerdown;
1467 static void main_loop(void)
1469 bool nonblocking;
1470 int last_io __attribute__ ((unused)) = 0;
1471 #ifdef CONFIG_PROFILER
1472 int64_t ti;
1473 #endif
1474 int r;
1476 qemu_main_loop_start();
1478 for (;;) {
1479 nonblocking = !kvm_enabled() && last_io > 0;
1480 #ifdef CONFIG_PROFILER
1481 ti = profile_getclock();
1482 #endif
1483 last_io = main_loop_wait(nonblocking);
1484 #ifdef CONFIG_PROFILER
1485 dev_time += profile_getclock() - ti;
1486 #endif
1488 if (qemu_debug_requested()) {
1489 vm_stop(RSTATE_DEBUG);
1491 if (qemu_shutdown_requested()) {
1492 qemu_kill_report();
1493 monitor_protocol_event(QEVENT_SHUTDOWN, NULL);
1494 if (no_shutdown) {
1495 vm_stop(RSTATE_SHUTDOWN);
1496 } else
1497 break;
1499 if (qemu_reset_requested()) {
1500 pause_all_vcpus();
1501 cpu_synchronize_all_states();
1502 qemu_system_reset(VMRESET_REPORT);
1503 resume_all_vcpus();
1505 if (qemu_powerdown_requested()) {
1506 monitor_protocol_event(QEVENT_POWERDOWN, NULL);
1507 qemu_irq_raise(qemu_system_powerdown);
1509 if ((r = qemu_vmstop_requested())) {
1510 vm_stop(r);
1513 bdrv_close_all();
1514 pause_all_vcpus();
1517 static void version(void)
1519 printf("QEMU emulator version " QEMU_VERSION QEMU_PKGVERSION ", Copyright (c) 2003-2008 Fabrice Bellard\n");
1522 static void help(int exitcode)
1524 const char *options_help =
1525 #define DEF(option, opt_arg, opt_enum, opt_help, arch_mask) \
1526 opt_help
1527 #define DEFHEADING(text) stringify(text) "\n"
1528 #include "qemu-options.def"
1529 #undef DEF
1530 #undef DEFHEADING
1531 #undef GEN_DOCS
1533 version();
1534 printf("usage: %s [options] [disk_image]\n"
1535 "\n"
1536 "'disk_image' is a raw hard disk image for IDE hard disk 0\n"
1537 "\n"
1538 "%s\n"
1539 "During emulation, the following keys are useful:\n"
1540 "ctrl-alt-f toggle full screen\n"
1541 "ctrl-alt-n switch to virtual console 'n'\n"
1542 "ctrl-alt toggle mouse and keyboard grab\n"
1543 "\n"
1544 "When using -nographic, press 'ctrl-a h' to get some help.\n",
1545 "qemu",
1546 options_help);
1547 exit(exitcode);
1550 #define HAS_ARG 0x0001
1552 typedef struct QEMUOption {
1553 const char *name;
1554 int flags;
1555 int index;
1556 uint32_t arch_mask;
1557 } QEMUOption;
1559 static const QEMUOption qemu_options[] = {
1560 { "h", 0, QEMU_OPTION_h, QEMU_ARCH_ALL },
1561 #define DEF(option, opt_arg, opt_enum, opt_help, arch_mask) \
1562 { option, opt_arg, opt_enum, arch_mask },
1563 #define DEFHEADING(text)
1564 #include "qemu-options.def"
1565 #undef DEF
1566 #undef DEFHEADING
1567 #undef GEN_DOCS
1568 { NULL },
1570 static void select_vgahw (const char *p)
1572 const char *opts;
1574 default_vga = 0;
1575 vga_interface_type = VGA_NONE;
1576 if (strstart(p, "std", &opts)) {
1577 vga_interface_type = VGA_STD;
1578 } else if (strstart(p, "cirrus", &opts)) {
1579 vga_interface_type = VGA_CIRRUS;
1580 } else if (strstart(p, "vmware", &opts)) {
1581 vga_interface_type = VGA_VMWARE;
1582 } else if (strstart(p, "xenfb", &opts)) {
1583 vga_interface_type = VGA_XENFB;
1584 } else if (strstart(p, "qxl", &opts)) {
1585 vga_interface_type = VGA_QXL;
1586 } else if (!strstart(p, "none", &opts)) {
1587 invalid_vga:
1588 fprintf(stderr, "Unknown vga type: %s\n", p);
1589 exit(1);
1591 while (*opts) {
1592 const char *nextopt;
1594 if (strstart(opts, ",retrace=", &nextopt)) {
1595 opts = nextopt;
1596 if (strstart(opts, "dumb", &nextopt))
1597 vga_retrace_method = VGA_RETRACE_DUMB;
1598 else if (strstart(opts, "precise", &nextopt))
1599 vga_retrace_method = VGA_RETRACE_PRECISE;
1600 else goto invalid_vga;
1601 } else goto invalid_vga;
1602 opts = nextopt;
1606 static DisplayType select_display(const char *p)
1608 const char *opts;
1609 DisplayType display = DT_DEFAULT;
1611 if (strstart(p, "sdl", &opts)) {
1612 #ifdef CONFIG_SDL
1613 display = DT_SDL;
1614 while (*opts) {
1615 const char *nextopt;
1617 if (strstart(opts, ",frame=", &nextopt)) {
1618 opts = nextopt;
1619 if (strstart(opts, "on", &nextopt)) {
1620 no_frame = 0;
1621 } else if (strstart(opts, "off", &nextopt)) {
1622 no_frame = 1;
1623 } else {
1624 goto invalid_sdl_args;
1626 } else if (strstart(opts, ",alt_grab=", &nextopt)) {
1627 opts = nextopt;
1628 if (strstart(opts, "on", &nextopt)) {
1629 alt_grab = 1;
1630 } else if (strstart(opts, "off", &nextopt)) {
1631 alt_grab = 0;
1632 } else {
1633 goto invalid_sdl_args;
1635 } else if (strstart(opts, ",ctrl_grab=", &nextopt)) {
1636 opts = nextopt;
1637 if (strstart(opts, "on", &nextopt)) {
1638 ctrl_grab = 1;
1639 } else if (strstart(opts, "off", &nextopt)) {
1640 ctrl_grab = 0;
1641 } else {
1642 goto invalid_sdl_args;
1644 } else if (strstart(opts, ",window_close=", &nextopt)) {
1645 opts = nextopt;
1646 if (strstart(opts, "on", &nextopt)) {
1647 no_quit = 0;
1648 } else if (strstart(opts, "off", &nextopt)) {
1649 no_quit = 1;
1650 } else {
1651 goto invalid_sdl_args;
1653 } else {
1654 invalid_sdl_args:
1655 fprintf(stderr, "Invalid SDL option string: %s\n", p);
1656 exit(1);
1658 opts = nextopt;
1660 #else
1661 fprintf(stderr, "SDL support is disabled\n");
1662 exit(1);
1663 #endif
1664 } else if (strstart(p, "vnc", &opts)) {
1665 #ifdef CONFIG_VNC
1666 display_remote++;
1668 if (*opts) {
1669 const char *nextopt;
1671 if (strstart(opts, "=", &nextopt)) {
1672 vnc_display = nextopt;
1675 if (!vnc_display) {
1676 fprintf(stderr, "VNC requires a display argument vnc=<display>\n");
1677 exit(1);
1679 #else
1680 fprintf(stderr, "VNC support is disabled\n");
1681 exit(1);
1682 #endif
1683 } else if (strstart(p, "curses", &opts)) {
1684 #ifdef CONFIG_CURSES
1685 display = DT_CURSES;
1686 #else
1687 fprintf(stderr, "Curses support is disabled\n");
1688 exit(1);
1689 #endif
1690 } else if (strstart(p, "none", &opts)) {
1691 display = DT_NONE;
1692 } else {
1693 fprintf(stderr, "Unknown display type: %s\n", p);
1694 exit(1);
1697 return display;
1700 static int balloon_parse(const char *arg)
1702 QemuOpts *opts;
1704 if (strcmp(arg, "none") == 0) {
1705 return 0;
1708 if (!strncmp(arg, "virtio", 6)) {
1709 if (arg[6] == ',') {
1710 /* have params -> parse them */
1711 opts = qemu_opts_parse(qemu_find_opts("device"), arg+7, 0);
1712 if (!opts)
1713 return -1;
1714 } else {
1715 /* create empty opts */
1716 opts = qemu_opts_create(qemu_find_opts("device"), NULL, 0);
1718 qemu_opt_set(opts, "driver", "virtio-balloon");
1719 return 0;
1722 return -1;
1725 char *qemu_find_file(int type, const char *name)
1727 int len;
1728 const char *subdir;
1729 char *buf;
1731 /* If name contains path separators then try it as a straight path. */
1732 if ((strchr(name, '/') || strchr(name, '\\'))
1733 && access(name, R_OK) == 0) {
1734 return g_strdup(name);
1736 switch (type) {
1737 case QEMU_FILE_TYPE_BIOS:
1738 subdir = "";
1739 break;
1740 case QEMU_FILE_TYPE_KEYMAP:
1741 subdir = "keymaps/";
1742 break;
1743 default:
1744 abort();
1746 len = strlen(data_dir) + strlen(name) + strlen(subdir) + 2;
1747 buf = g_malloc0(len);
1748 snprintf(buf, len, "%s/%s%s", data_dir, subdir, name);
1749 if (access(buf, R_OK)) {
1750 g_free(buf);
1751 return NULL;
1753 return buf;
1756 static int device_help_func(QemuOpts *opts, void *opaque)
1758 return qdev_device_help(opts);
1761 static int device_init_func(QemuOpts *opts, void *opaque)
1763 DeviceState *dev;
1765 dev = qdev_device_add(opts);
1766 if (!dev)
1767 return -1;
1768 return 0;
1771 static int chardev_init_func(QemuOpts *opts, void *opaque)
1773 CharDriverState *chr;
1775 chr = qemu_chr_new_from_opts(opts, NULL);
1776 if (!chr)
1777 return -1;
1778 return 0;
1781 #ifdef CONFIG_VIRTFS
1782 static int fsdev_init_func(QemuOpts *opts, void *opaque)
1784 int ret;
1785 ret = qemu_fsdev_add(opts);
1787 return ret;
1789 #endif
1791 static int mon_init_func(QemuOpts *opts, void *opaque)
1793 CharDriverState *chr;
1794 const char *chardev;
1795 const char *mode;
1796 int flags;
1798 mode = qemu_opt_get(opts, "mode");
1799 if (mode == NULL) {
1800 mode = "readline";
1802 if (strcmp(mode, "readline") == 0) {
1803 flags = MONITOR_USE_READLINE;
1804 } else if (strcmp(mode, "control") == 0) {
1805 flags = MONITOR_USE_CONTROL;
1806 } else {
1807 fprintf(stderr, "unknown monitor mode \"%s\"\n", mode);
1808 exit(1);
1811 if (qemu_opt_get_bool(opts, "pretty", 0))
1812 flags |= MONITOR_USE_PRETTY;
1814 if (qemu_opt_get_bool(opts, "default", 0))
1815 flags |= MONITOR_IS_DEFAULT;
1817 chardev = qemu_opt_get(opts, "chardev");
1818 chr = qemu_chr_find(chardev);
1819 if (chr == NULL) {
1820 fprintf(stderr, "chardev \"%s\" not found\n", chardev);
1821 exit(1);
1824 monitor_init(chr, flags);
1825 return 0;
1828 static void monitor_parse(const char *optarg, const char *mode)
1830 static int monitor_device_index = 0;
1831 QemuOpts *opts;
1832 const char *p;
1833 char label[32];
1834 int def = 0;
1836 if (strstart(optarg, "chardev:", &p)) {
1837 snprintf(label, sizeof(label), "%s", p);
1838 } else {
1839 snprintf(label, sizeof(label), "compat_monitor%d",
1840 monitor_device_index);
1841 if (monitor_device_index == 0) {
1842 def = 1;
1844 opts = qemu_chr_parse_compat(label, optarg);
1845 if (!opts) {
1846 fprintf(stderr, "parse error: %s\n", optarg);
1847 exit(1);
1851 opts = qemu_opts_create(qemu_find_opts("mon"), label, 1);
1852 if (!opts) {
1853 fprintf(stderr, "duplicate chardev: %s\n", label);
1854 exit(1);
1856 qemu_opt_set(opts, "mode", mode);
1857 qemu_opt_set(opts, "chardev", label);
1858 if (def)
1859 qemu_opt_set(opts, "default", "on");
1860 monitor_device_index++;
1863 struct device_config {
1864 enum {
1865 DEV_USB, /* -usbdevice */
1866 DEV_BT, /* -bt */
1867 DEV_SERIAL, /* -serial */
1868 DEV_PARALLEL, /* -parallel */
1869 DEV_VIRTCON, /* -virtioconsole */
1870 DEV_DEBUGCON, /* -debugcon */
1871 } type;
1872 const char *cmdline;
1873 QTAILQ_ENTRY(device_config) next;
1875 QTAILQ_HEAD(, device_config) device_configs = QTAILQ_HEAD_INITIALIZER(device_configs);
1877 static void add_device_config(int type, const char *cmdline)
1879 struct device_config *conf;
1881 conf = g_malloc0(sizeof(*conf));
1882 conf->type = type;
1883 conf->cmdline = cmdline;
1884 QTAILQ_INSERT_TAIL(&device_configs, conf, next);
1887 static int foreach_device_config(int type, int (*func)(const char *cmdline))
1889 struct device_config *conf;
1890 int rc;
1892 QTAILQ_FOREACH(conf, &device_configs, next) {
1893 if (conf->type != type)
1894 continue;
1895 rc = func(conf->cmdline);
1896 if (0 != rc)
1897 return rc;
1899 return 0;
1902 static int serial_parse(const char *devname)
1904 static int index = 0;
1905 char label[32];
1907 if (strcmp(devname, "none") == 0)
1908 return 0;
1909 if (index == MAX_SERIAL_PORTS) {
1910 fprintf(stderr, "qemu: too many serial ports\n");
1911 exit(1);
1913 snprintf(label, sizeof(label), "serial%d", index);
1914 serial_hds[index] = qemu_chr_new(label, devname, NULL);
1915 if (!serial_hds[index]) {
1916 fprintf(stderr, "qemu: could not open serial device '%s': %s\n",
1917 devname, strerror(errno));
1918 return -1;
1920 index++;
1921 return 0;
1924 static int parallel_parse(const char *devname)
1926 static int index = 0;
1927 char label[32];
1929 if (strcmp(devname, "none") == 0)
1930 return 0;
1931 if (index == MAX_PARALLEL_PORTS) {
1932 fprintf(stderr, "qemu: too many parallel ports\n");
1933 exit(1);
1935 snprintf(label, sizeof(label), "parallel%d", index);
1936 parallel_hds[index] = qemu_chr_new(label, devname, NULL);
1937 if (!parallel_hds[index]) {
1938 fprintf(stderr, "qemu: could not open parallel device '%s': %s\n",
1939 devname, strerror(errno));
1940 return -1;
1942 index++;
1943 return 0;
1946 static int virtcon_parse(const char *devname)
1948 QemuOptsList *device = qemu_find_opts("device");
1949 static int index = 0;
1950 char label[32];
1951 QemuOpts *bus_opts, *dev_opts;
1953 if (strcmp(devname, "none") == 0)
1954 return 0;
1955 if (index == MAX_VIRTIO_CONSOLES) {
1956 fprintf(stderr, "qemu: too many virtio consoles\n");
1957 exit(1);
1960 bus_opts = qemu_opts_create(device, NULL, 0);
1961 qemu_opt_set(bus_opts, "driver", "virtio-serial");
1963 dev_opts = qemu_opts_create(device, NULL, 0);
1964 qemu_opt_set(dev_opts, "driver", "virtconsole");
1966 snprintf(label, sizeof(label), "virtcon%d", index);
1967 virtcon_hds[index] = qemu_chr_new(label, devname, NULL);
1968 if (!virtcon_hds[index]) {
1969 fprintf(stderr, "qemu: could not open virtio console '%s': %s\n",
1970 devname, strerror(errno));
1971 return -1;
1973 qemu_opt_set(dev_opts, "chardev", label);
1975 index++;
1976 return 0;
1979 static int debugcon_parse(const char *devname)
1981 QemuOpts *opts;
1983 if (!qemu_chr_new("debugcon", devname, NULL)) {
1984 exit(1);
1986 opts = qemu_opts_create(qemu_find_opts("device"), "debugcon", 1);
1987 if (!opts) {
1988 fprintf(stderr, "qemu: already have a debugcon device\n");
1989 exit(1);
1991 qemu_opt_set(opts, "driver", "isa-debugcon");
1992 qemu_opt_set(opts, "chardev", "debugcon");
1993 return 0;
1996 static QEMUMachine *machine_parse(const char *name)
1998 QEMUMachine *m, *machine = NULL;
2000 if (name) {
2001 machine = find_machine(name);
2003 if (machine) {
2004 return machine;
2006 printf("Supported machines are:\n");
2007 for (m = first_machine; m != NULL; m = m->next) {
2008 if (m->alias) {
2009 printf("%-10s %s (alias of %s)\n", m->alias, m->desc, m->name);
2011 printf("%-10s %s%s\n", m->name, m->desc,
2012 m->is_default ? " (default)" : "");
2014 exit(!name || *name != '?');
2017 static int tcg_init(void)
2019 tcg_exec_init(tcg_tb_size * 1024 * 1024);
2020 return 0;
2023 static struct {
2024 const char *opt_name;
2025 const char *name;
2026 int (*available)(void);
2027 int (*init)(void);
2028 int *allowed;
2029 } accel_list[] = {
2030 { "tcg", "tcg", tcg_available, tcg_init, &tcg_allowed },
2031 { "xen", "Xen", xen_available, xen_init, &xen_allowed },
2032 { "kvm", "KVM", kvm_available, kvm_init, &kvm_allowed },
2035 static int configure_accelerator(void)
2037 const char *p = NULL;
2038 char buf[10];
2039 int i, ret;
2040 bool accel_initalised = 0;
2041 bool init_failed = 0;
2043 QemuOptsList *list = qemu_find_opts("machine");
2044 if (!QTAILQ_EMPTY(&list->head)) {
2045 p = qemu_opt_get(QTAILQ_FIRST(&list->head), "accel");
2048 if (p == NULL) {
2049 /* Use the default "accelerator", tcg */
2050 p = "tcg";
2053 while (!accel_initalised && *p != '\0') {
2054 if (*p == ':') {
2055 p++;
2057 p = get_opt_name(buf, sizeof (buf), p, ':');
2058 for (i = 0; i < ARRAY_SIZE(accel_list); i++) {
2059 if (strcmp(accel_list[i].opt_name, buf) == 0) {
2060 *(accel_list[i].allowed) = 1;
2061 ret = accel_list[i].init();
2062 if (ret < 0) {
2063 init_failed = 1;
2064 if (!accel_list[i].available()) {
2065 printf("%s not supported for this target\n",
2066 accel_list[i].name);
2067 } else {
2068 fprintf(stderr, "failed to initialize %s: %s\n",
2069 accel_list[i].name,
2070 strerror(-ret));
2072 *(accel_list[i].allowed) = 0;
2073 } else {
2074 accel_initalised = 1;
2076 break;
2079 if (i == ARRAY_SIZE(accel_list)) {
2080 fprintf(stderr, "\"%s\" accelerator does not exist.\n", buf);
2084 if (!accel_initalised) {
2085 fprintf(stderr, "No accelerator found!\n");
2086 exit(1);
2089 if (init_failed) {
2090 fprintf(stderr, "Back to %s accelerator.\n", accel_list[i].name);
2093 return !accel_initalised;
2096 void qemu_add_exit_notifier(Notifier *notify)
2098 notifier_list_add(&exit_notifiers, notify);
2101 void qemu_remove_exit_notifier(Notifier *notify)
2103 notifier_list_remove(&exit_notifiers, notify);
2106 static void qemu_run_exit_notifiers(void)
2108 notifier_list_notify(&exit_notifiers, NULL);
2111 void qemu_add_machine_init_done_notifier(Notifier *notify)
2113 notifier_list_add(&machine_init_done_notifiers, notify);
2116 static void qemu_run_machine_init_done_notifiers(void)
2118 notifier_list_notify(&machine_init_done_notifiers, NULL);
2121 static const QEMUOption *lookup_opt(int argc, char **argv,
2122 const char **poptarg, int *poptind)
2124 const QEMUOption *popt;
2125 int optind = *poptind;
2126 char *r = argv[optind];
2127 const char *optarg;
2129 loc_set_cmdline(argv, optind, 1);
2130 optind++;
2131 /* Treat --foo the same as -foo. */
2132 if (r[1] == '-')
2133 r++;
2134 popt = qemu_options;
2135 for(;;) {
2136 if (!popt->name) {
2137 error_report("invalid option");
2138 exit(1);
2140 if (!strcmp(popt->name, r + 1))
2141 break;
2142 popt++;
2144 if (popt->flags & HAS_ARG) {
2145 if (optind >= argc) {
2146 error_report("requires an argument");
2147 exit(1);
2149 optarg = argv[optind++];
2150 loc_set_cmdline(argv, optind - 2, 2);
2151 } else {
2152 optarg = NULL;
2155 *poptarg = optarg;
2156 *poptind = optind;
2158 return popt;
2161 static gpointer malloc_and_trace(gsize n_bytes)
2163 void *ptr = malloc(n_bytes);
2164 trace_g_malloc(n_bytes, ptr);
2165 return ptr;
2168 static gpointer realloc_and_trace(gpointer mem, gsize n_bytes)
2170 void *ptr = realloc(mem, n_bytes);
2171 trace_g_realloc(mem, n_bytes, ptr);
2172 return ptr;
2175 static void free_and_trace(gpointer mem)
2177 trace_g_free(mem);
2178 free(mem);
2181 int main(int argc, char **argv, char **envp)
2183 const char *gdbstub_dev = NULL;
2184 int i;
2185 int snapshot, linux_boot;
2186 const char *icount_option = NULL;
2187 const char *initrd_filename;
2188 const char *kernel_filename, *kernel_cmdline;
2189 char boot_devices[33] = "cad"; /* default to HD->floppy->CD-ROM */
2190 DisplayState *ds;
2191 DisplayChangeListener *dcl;
2192 int cyls, heads, secs, translation;
2193 QemuOpts *hda_opts = NULL, *opts;
2194 QemuOptsList *olist;
2195 int optind;
2196 const char *optarg;
2197 const char *loadvm = NULL;
2198 QEMUMachine *machine;
2199 const char *cpu_model;
2200 const char *pid_file = NULL;
2201 const char *incoming = NULL;
2202 #ifdef CONFIG_VNC
2203 int show_vnc_port = 0;
2204 #endif
2205 int defconfig = 1;
2206 const char *log_mask = NULL;
2207 const char *log_file = NULL;
2208 GMemVTable mem_trace = {
2209 .malloc = malloc_and_trace,
2210 .realloc = realloc_and_trace,
2211 .free = free_and_trace,
2213 const char *trace_events = NULL;
2214 const char *trace_file = NULL;
2216 atexit(qemu_run_exit_notifiers);
2217 error_set_progname(argv[0]);
2219 g_mem_set_vtable(&mem_trace);
2220 g_thread_init(NULL);
2222 init_clocks();
2224 qemu_cache_utils_init(envp);
2226 QLIST_INIT (&vm_change_state_head);
2227 os_setup_early_signal_handling();
2229 module_call_init(MODULE_INIT_MACHINE);
2230 machine = find_default_machine();
2231 cpu_model = NULL;
2232 initrd_filename = NULL;
2233 ram_size = 0;
2234 snapshot = 0;
2235 kernel_filename = NULL;
2236 kernel_cmdline = "";
2237 cyls = heads = secs = 0;
2238 translation = BIOS_ATA_TRANSLATION_AUTO;
2240 for (i = 0; i < MAX_NODES; i++) {
2241 node_mem[i] = 0;
2242 node_cpumask[i] = 0;
2245 nb_numa_nodes = 0;
2246 nb_nics = 0;
2248 autostart= 1;
2250 /* first pass of option parsing */
2251 optind = 1;
2252 while (optind < argc) {
2253 if (argv[optind][0] != '-') {
2254 /* disk image */
2255 optind++;
2256 continue;
2257 } else {
2258 const QEMUOption *popt;
2260 popt = lookup_opt(argc, argv, &optarg, &optind);
2261 switch (popt->index) {
2262 case QEMU_OPTION_nodefconfig:
2263 defconfig=0;
2264 break;
2269 if (defconfig) {
2270 int ret;
2272 ret = qemu_read_config_file(CONFIG_QEMU_CONFDIR "/qemu.conf");
2273 if (ret < 0 && ret != -ENOENT) {
2274 exit(1);
2277 ret = qemu_read_config_file(arch_config_name);
2278 if (ret < 0 && ret != -ENOENT) {
2279 exit(1);
2282 cpudef_init();
2284 /* second pass of option parsing */
2285 optind = 1;
2286 for(;;) {
2287 if (optind >= argc)
2288 break;
2289 if (argv[optind][0] != '-') {
2290 hda_opts = drive_add(IF_DEFAULT, 0, argv[optind++], HD_OPTS);
2291 } else {
2292 const QEMUOption *popt;
2294 popt = lookup_opt(argc, argv, &optarg, &optind);
2295 if (!(popt->arch_mask & arch_type)) {
2296 printf("Option %s not supported for this target\n", popt->name);
2297 exit(1);
2299 switch(popt->index) {
2300 case QEMU_OPTION_M:
2301 machine = machine_parse(optarg);
2302 break;
2303 case QEMU_OPTION_cpu:
2304 /* hw initialization will check this */
2305 if (*optarg == '?') {
2306 list_cpus(stdout, &fprintf, optarg);
2307 exit(0);
2308 } else {
2309 cpu_model = optarg;
2311 break;
2312 case QEMU_OPTION_initrd:
2313 initrd_filename = optarg;
2314 break;
2315 case QEMU_OPTION_hda:
2317 char buf[256];
2318 if (cyls == 0)
2319 snprintf(buf, sizeof(buf), "%s", HD_OPTS);
2320 else
2321 snprintf(buf, sizeof(buf),
2322 "%s,cyls=%d,heads=%d,secs=%d%s",
2323 HD_OPTS , cyls, heads, secs,
2324 translation == BIOS_ATA_TRANSLATION_LBA ?
2325 ",trans=lba" :
2326 translation == BIOS_ATA_TRANSLATION_NONE ?
2327 ",trans=none" : "");
2328 drive_add(IF_DEFAULT, 0, optarg, buf);
2329 break;
2331 case QEMU_OPTION_hdb:
2332 case QEMU_OPTION_hdc:
2333 case QEMU_OPTION_hdd:
2334 drive_add(IF_DEFAULT, popt->index - QEMU_OPTION_hda, optarg,
2335 HD_OPTS);
2336 break;
2337 case QEMU_OPTION_drive:
2338 if (drive_def(optarg) == NULL) {
2339 exit(1);
2341 break;
2342 case QEMU_OPTION_set:
2343 if (qemu_set_option(optarg) != 0)
2344 exit(1);
2345 break;
2346 case QEMU_OPTION_global:
2347 if (qemu_global_option(optarg) != 0)
2348 exit(1);
2349 break;
2350 case QEMU_OPTION_mtdblock:
2351 drive_add(IF_MTD, -1, optarg, MTD_OPTS);
2352 break;
2353 case QEMU_OPTION_sd:
2354 drive_add(IF_SD, 0, optarg, SD_OPTS);
2355 break;
2356 case QEMU_OPTION_pflash:
2357 drive_add(IF_PFLASH, -1, optarg, PFLASH_OPTS);
2358 break;
2359 case QEMU_OPTION_snapshot:
2360 snapshot = 1;
2361 break;
2362 case QEMU_OPTION_hdachs:
2364 const char *p;
2365 p = optarg;
2366 cyls = strtol(p, (char **)&p, 0);
2367 if (cyls < 1 || cyls > 16383)
2368 goto chs_fail;
2369 if (*p != ',')
2370 goto chs_fail;
2371 p++;
2372 heads = strtol(p, (char **)&p, 0);
2373 if (heads < 1 || heads > 16)
2374 goto chs_fail;
2375 if (*p != ',')
2376 goto chs_fail;
2377 p++;
2378 secs = strtol(p, (char **)&p, 0);
2379 if (secs < 1 || secs > 63)
2380 goto chs_fail;
2381 if (*p == ',') {
2382 p++;
2383 if (!strcmp(p, "none"))
2384 translation = BIOS_ATA_TRANSLATION_NONE;
2385 else if (!strcmp(p, "lba"))
2386 translation = BIOS_ATA_TRANSLATION_LBA;
2387 else if (!strcmp(p, "auto"))
2388 translation = BIOS_ATA_TRANSLATION_AUTO;
2389 else
2390 goto chs_fail;
2391 } else if (*p != '\0') {
2392 chs_fail:
2393 fprintf(stderr, "qemu: invalid physical CHS format\n");
2394 exit(1);
2396 if (hda_opts != NULL) {
2397 char num[16];
2398 snprintf(num, sizeof(num), "%d", cyls);
2399 qemu_opt_set(hda_opts, "cyls", num);
2400 snprintf(num, sizeof(num), "%d", heads);
2401 qemu_opt_set(hda_opts, "heads", num);
2402 snprintf(num, sizeof(num), "%d", secs);
2403 qemu_opt_set(hda_opts, "secs", num);
2404 if (translation == BIOS_ATA_TRANSLATION_LBA)
2405 qemu_opt_set(hda_opts, "trans", "lba");
2406 if (translation == BIOS_ATA_TRANSLATION_NONE)
2407 qemu_opt_set(hda_opts, "trans", "none");
2410 break;
2411 case QEMU_OPTION_numa:
2412 if (nb_numa_nodes >= MAX_NODES) {
2413 fprintf(stderr, "qemu: too many NUMA nodes\n");
2414 exit(1);
2416 numa_add(optarg);
2417 break;
2418 case QEMU_OPTION_display:
2419 display_type = select_display(optarg);
2420 break;
2421 case QEMU_OPTION_nographic:
2422 display_type = DT_NOGRAPHIC;
2423 break;
2424 case QEMU_OPTION_curses:
2425 #ifdef CONFIG_CURSES
2426 display_type = DT_CURSES;
2427 #else
2428 fprintf(stderr, "Curses support is disabled\n");
2429 exit(1);
2430 #endif
2431 break;
2432 case QEMU_OPTION_portrait:
2433 graphic_rotate = 90;
2434 break;
2435 case QEMU_OPTION_rotate:
2436 graphic_rotate = strtol(optarg, (char **) &optarg, 10);
2437 if (graphic_rotate != 0 && graphic_rotate != 90 &&
2438 graphic_rotate != 180 && graphic_rotate != 270) {
2439 fprintf(stderr,
2440 "qemu: only 90, 180, 270 deg rotation is available\n");
2441 exit(1);
2443 break;
2444 case QEMU_OPTION_kernel:
2445 kernel_filename = optarg;
2446 break;
2447 case QEMU_OPTION_append:
2448 kernel_cmdline = optarg;
2449 break;
2450 case QEMU_OPTION_cdrom:
2451 drive_add(IF_DEFAULT, 2, optarg, CDROM_OPTS);
2452 break;
2453 case QEMU_OPTION_boot:
2455 static const char * const params[] = {
2456 "order", "once", "menu",
2457 "splash", "splash-time", NULL
2459 char buf[sizeof(boot_devices)];
2460 char *standard_boot_devices;
2461 int legacy = 0;
2463 if (!strchr(optarg, '=')) {
2464 legacy = 1;
2465 pstrcpy(buf, sizeof(buf), optarg);
2466 } else if (check_params(buf, sizeof(buf), params, optarg) < 0) {
2467 fprintf(stderr,
2468 "qemu: unknown boot parameter '%s' in '%s'\n",
2469 buf, optarg);
2470 exit(1);
2473 if (legacy ||
2474 get_param_value(buf, sizeof(buf), "order", optarg)) {
2475 validate_bootdevices(buf);
2476 pstrcpy(boot_devices, sizeof(boot_devices), buf);
2478 if (!legacy) {
2479 if (get_param_value(buf, sizeof(buf),
2480 "once", optarg)) {
2481 validate_bootdevices(buf);
2482 standard_boot_devices = g_strdup(boot_devices);
2483 pstrcpy(boot_devices, sizeof(boot_devices), buf);
2484 qemu_register_reset(restore_boot_devices,
2485 standard_boot_devices);
2487 if (get_param_value(buf, sizeof(buf),
2488 "menu", optarg)) {
2489 if (!strcmp(buf, "on")) {
2490 boot_menu = 1;
2491 } else if (!strcmp(buf, "off")) {
2492 boot_menu = 0;
2493 } else {
2494 fprintf(stderr,
2495 "qemu: invalid option value '%s'\n",
2496 buf);
2497 exit(1);
2500 qemu_opts_parse(qemu_find_opts("boot-opts"),
2501 optarg, 0);
2504 break;
2505 case QEMU_OPTION_fda:
2506 case QEMU_OPTION_fdb:
2507 drive_add(IF_FLOPPY, popt->index - QEMU_OPTION_fda,
2508 optarg, FD_OPTS);
2509 break;
2510 case QEMU_OPTION_no_fd_bootchk:
2511 fd_bootchk = 0;
2512 break;
2513 case QEMU_OPTION_netdev:
2514 if (net_client_parse(qemu_find_opts("netdev"), optarg) == -1) {
2515 exit(1);
2517 break;
2518 case QEMU_OPTION_net:
2519 if (net_client_parse(qemu_find_opts("net"), optarg) == -1) {
2520 exit(1);
2522 break;
2523 #ifdef CONFIG_SLIRP
2524 case QEMU_OPTION_tftp:
2525 legacy_tftp_prefix = optarg;
2526 break;
2527 case QEMU_OPTION_bootp:
2528 legacy_bootp_filename = optarg;
2529 break;
2530 case QEMU_OPTION_redir:
2531 if (net_slirp_redir(optarg) < 0)
2532 exit(1);
2533 break;
2534 #endif
2535 case QEMU_OPTION_bt:
2536 add_device_config(DEV_BT, optarg);
2537 break;
2538 case QEMU_OPTION_audio_help:
2539 if (!(audio_available())) {
2540 printf("Option %s not supported for this target\n", popt->name);
2541 exit(1);
2543 AUD_help ();
2544 exit (0);
2545 break;
2546 case QEMU_OPTION_soundhw:
2547 if (!(audio_available())) {
2548 printf("Option %s not supported for this target\n", popt->name);
2549 exit(1);
2551 select_soundhw (optarg);
2552 break;
2553 case QEMU_OPTION_h:
2554 help(0);
2555 break;
2556 case QEMU_OPTION_version:
2557 version();
2558 exit(0);
2559 break;
2560 case QEMU_OPTION_m: {
2561 int64_t value;
2563 value = strtosz(optarg, NULL);
2564 if (value < 0) {
2565 fprintf(stderr, "qemu: invalid ram size: %s\n", optarg);
2566 exit(1);
2569 if (value != (uint64_t)(ram_addr_t)value) {
2570 fprintf(stderr, "qemu: ram size too large\n");
2571 exit(1);
2573 ram_size = value;
2574 break;
2576 case QEMU_OPTION_mempath:
2577 mem_path = optarg;
2578 break;
2579 #ifdef MAP_POPULATE
2580 case QEMU_OPTION_mem_prealloc:
2581 mem_prealloc = 1;
2582 break;
2583 #endif
2584 case QEMU_OPTION_d:
2585 log_mask = optarg;
2586 break;
2587 case QEMU_OPTION_D:
2588 log_file = optarg;
2589 break;
2590 case QEMU_OPTION_s:
2591 gdbstub_dev = "tcp::" DEFAULT_GDBSTUB_PORT;
2592 break;
2593 case QEMU_OPTION_gdb:
2594 gdbstub_dev = optarg;
2595 break;
2596 case QEMU_OPTION_L:
2597 data_dir = optarg;
2598 break;
2599 case QEMU_OPTION_bios:
2600 bios_name = optarg;
2601 break;
2602 case QEMU_OPTION_singlestep:
2603 singlestep = 1;
2604 break;
2605 case QEMU_OPTION_S:
2606 autostart = 0;
2607 break;
2608 case QEMU_OPTION_k:
2609 keyboard_layout = optarg;
2610 break;
2611 case QEMU_OPTION_localtime:
2612 rtc_utc = 0;
2613 break;
2614 case QEMU_OPTION_vga:
2615 select_vgahw (optarg);
2616 break;
2617 case QEMU_OPTION_g:
2619 const char *p;
2620 int w, h, depth;
2621 p = optarg;
2622 w = strtol(p, (char **)&p, 10);
2623 if (w <= 0) {
2624 graphic_error:
2625 fprintf(stderr, "qemu: invalid resolution or depth\n");
2626 exit(1);
2628 if (*p != 'x')
2629 goto graphic_error;
2630 p++;
2631 h = strtol(p, (char **)&p, 10);
2632 if (h <= 0)
2633 goto graphic_error;
2634 if (*p == 'x') {
2635 p++;
2636 depth = strtol(p, (char **)&p, 10);
2637 if (depth != 8 && depth != 15 && depth != 16 &&
2638 depth != 24 && depth != 32)
2639 goto graphic_error;
2640 } else if (*p == '\0') {
2641 depth = graphic_depth;
2642 } else {
2643 goto graphic_error;
2646 graphic_width = w;
2647 graphic_height = h;
2648 graphic_depth = depth;
2650 break;
2651 case QEMU_OPTION_echr:
2653 char *r;
2654 term_escape_char = strtol(optarg, &r, 0);
2655 if (r == optarg)
2656 printf("Bad argument to echr\n");
2657 break;
2659 case QEMU_OPTION_monitor:
2660 monitor_parse(optarg, "readline");
2661 default_monitor = 0;
2662 break;
2663 case QEMU_OPTION_qmp:
2664 monitor_parse(optarg, "control");
2665 default_monitor = 0;
2666 break;
2667 case QEMU_OPTION_mon:
2668 opts = qemu_opts_parse(qemu_find_opts("mon"), optarg, 1);
2669 if (!opts) {
2670 exit(1);
2672 default_monitor = 0;
2673 break;
2674 case QEMU_OPTION_chardev:
2675 opts = qemu_opts_parse(qemu_find_opts("chardev"), optarg, 1);
2676 if (!opts) {
2677 exit(1);
2679 break;
2680 case QEMU_OPTION_fsdev:
2681 olist = qemu_find_opts("fsdev");
2682 if (!olist) {
2683 fprintf(stderr, "fsdev is not supported by this qemu build.\n");
2684 exit(1);
2686 opts = qemu_opts_parse(olist, optarg, 1);
2687 if (!opts) {
2688 fprintf(stderr, "parse error: %s\n", optarg);
2689 exit(1);
2691 break;
2692 case QEMU_OPTION_virtfs: {
2693 QemuOpts *fsdev;
2694 QemuOpts *device;
2696 olist = qemu_find_opts("virtfs");
2697 if (!olist) {
2698 fprintf(stderr, "virtfs is not supported by this qemu build.\n");
2699 exit(1);
2701 opts = qemu_opts_parse(olist, optarg, 1);
2702 if (!opts) {
2703 fprintf(stderr, "parse error: %s\n", optarg);
2704 exit(1);
2707 if (qemu_opt_get(opts, "fstype") == NULL ||
2708 qemu_opt_get(opts, "mount_tag") == NULL ||
2709 qemu_opt_get(opts, "path") == NULL ||
2710 qemu_opt_get(opts, "security_model") == NULL) {
2711 fprintf(stderr, "Usage: -virtfs fstype,path=/share_path/,"
2712 "security_model=[mapped|passthrough|none],"
2713 "mount_tag=tag.\n");
2714 exit(1);
2717 fsdev = qemu_opts_create(qemu_find_opts("fsdev"),
2718 qemu_opt_get(opts, "mount_tag"), 1);
2719 if (!fsdev) {
2720 fprintf(stderr, "duplicate fsdev id: %s\n",
2721 qemu_opt_get(opts, "mount_tag"));
2722 exit(1);
2724 qemu_opt_set(fsdev, "fstype", qemu_opt_get(opts, "fstype"));
2725 qemu_opt_set(fsdev, "path", qemu_opt_get(opts, "path"));
2726 qemu_opt_set(fsdev, "security_model",
2727 qemu_opt_get(opts, "security_model"));
2729 device = qemu_opts_create(qemu_find_opts("device"), NULL, 0);
2730 qemu_opt_set(device, "driver", "virtio-9p-pci");
2731 qemu_opt_set(device, "fsdev",
2732 qemu_opt_get(opts, "mount_tag"));
2733 qemu_opt_set(device, "mount_tag",
2734 qemu_opt_get(opts, "mount_tag"));
2735 break;
2737 case QEMU_OPTION_serial:
2738 add_device_config(DEV_SERIAL, optarg);
2739 default_serial = 0;
2740 if (strncmp(optarg, "mon:", 4) == 0) {
2741 default_monitor = 0;
2743 break;
2744 case QEMU_OPTION_watchdog:
2745 if (watchdog) {
2746 fprintf(stderr,
2747 "qemu: only one watchdog option may be given\n");
2748 return 1;
2750 watchdog = optarg;
2751 break;
2752 case QEMU_OPTION_watchdog_action:
2753 if (select_watchdog_action(optarg) == -1) {
2754 fprintf(stderr, "Unknown -watchdog-action parameter\n");
2755 exit(1);
2757 break;
2758 case QEMU_OPTION_virtiocon:
2759 add_device_config(DEV_VIRTCON, optarg);
2760 default_virtcon = 0;
2761 if (strncmp(optarg, "mon:", 4) == 0) {
2762 default_monitor = 0;
2764 break;
2765 case QEMU_OPTION_parallel:
2766 add_device_config(DEV_PARALLEL, optarg);
2767 default_parallel = 0;
2768 if (strncmp(optarg, "mon:", 4) == 0) {
2769 default_monitor = 0;
2771 break;
2772 case QEMU_OPTION_debugcon:
2773 add_device_config(DEV_DEBUGCON, optarg);
2774 break;
2775 case QEMU_OPTION_loadvm:
2776 loadvm = optarg;
2777 break;
2778 case QEMU_OPTION_full_screen:
2779 full_screen = 1;
2780 break;
2781 #ifdef CONFIG_SDL
2782 case QEMU_OPTION_no_frame:
2783 no_frame = 1;
2784 break;
2785 case QEMU_OPTION_alt_grab:
2786 alt_grab = 1;
2787 break;
2788 case QEMU_OPTION_ctrl_grab:
2789 ctrl_grab = 1;
2790 break;
2791 case QEMU_OPTION_no_quit:
2792 no_quit = 1;
2793 break;
2794 case QEMU_OPTION_sdl:
2795 display_type = DT_SDL;
2796 break;
2797 #else
2798 case QEMU_OPTION_no_frame:
2799 case QEMU_OPTION_alt_grab:
2800 case QEMU_OPTION_ctrl_grab:
2801 case QEMU_OPTION_no_quit:
2802 case QEMU_OPTION_sdl:
2803 fprintf(stderr, "SDL support is disabled\n");
2804 exit(1);
2805 #endif
2806 case QEMU_OPTION_pidfile:
2807 pid_file = optarg;
2808 break;
2809 case QEMU_OPTION_win2k_hack:
2810 win2k_install_hack = 1;
2811 break;
2812 case QEMU_OPTION_rtc_td_hack:
2813 rtc_td_hack = 1;
2814 break;
2815 case QEMU_OPTION_acpitable:
2816 do_acpitable_option(optarg);
2817 break;
2818 case QEMU_OPTION_smbios:
2819 do_smbios_option(optarg);
2820 break;
2821 case QEMU_OPTION_enable_kvm:
2822 olist = qemu_find_opts("machine");
2823 qemu_opts_reset(olist);
2824 qemu_opts_parse(olist, "accel=kvm", 0);
2825 break;
2826 case QEMU_OPTION_machine:
2827 olist = qemu_find_opts("machine");
2828 qemu_opts_reset(olist);
2829 opts = qemu_opts_parse(olist, optarg, 1);
2830 if (!opts) {
2831 fprintf(stderr, "parse error: %s\n", optarg);
2832 exit(1);
2834 optarg = qemu_opt_get(opts, "type");
2835 if (optarg) {
2836 machine = machine_parse(optarg);
2838 break;
2839 case QEMU_OPTION_usb:
2840 usb_enabled = 1;
2841 break;
2842 case QEMU_OPTION_usbdevice:
2843 usb_enabled = 1;
2844 add_device_config(DEV_USB, optarg);
2845 break;
2846 case QEMU_OPTION_device:
2847 if (!qemu_opts_parse(qemu_find_opts("device"), optarg, 1)) {
2848 exit(1);
2850 break;
2851 case QEMU_OPTION_smp:
2852 smp_parse(optarg);
2853 if (smp_cpus < 1) {
2854 fprintf(stderr, "Invalid number of CPUs\n");
2855 exit(1);
2857 if (max_cpus < smp_cpus) {
2858 fprintf(stderr, "maxcpus must be equal to or greater than "
2859 "smp\n");
2860 exit(1);
2862 if (max_cpus > 255) {
2863 fprintf(stderr, "Unsupported number of maxcpus\n");
2864 exit(1);
2866 break;
2867 case QEMU_OPTION_vnc:
2868 #ifdef CONFIG_VNC
2869 display_remote++;
2870 vnc_display = optarg;
2871 #else
2872 fprintf(stderr, "VNC support is disabled\n");
2873 exit(1);
2874 #endif
2875 break;
2876 case QEMU_OPTION_no_acpi:
2877 acpi_enabled = 0;
2878 break;
2879 case QEMU_OPTION_no_hpet:
2880 no_hpet = 1;
2881 break;
2882 case QEMU_OPTION_balloon:
2883 if (balloon_parse(optarg) < 0) {
2884 fprintf(stderr, "Unknown -balloon argument %s\n", optarg);
2885 exit(1);
2887 break;
2888 case QEMU_OPTION_no_reboot:
2889 no_reboot = 1;
2890 break;
2891 case QEMU_OPTION_no_shutdown:
2892 no_shutdown = 1;
2893 break;
2894 case QEMU_OPTION_show_cursor:
2895 cursor_hide = 0;
2896 break;
2897 case QEMU_OPTION_uuid:
2898 if(qemu_uuid_parse(optarg, qemu_uuid) < 0) {
2899 fprintf(stderr, "Fail to parse UUID string."
2900 " Wrong format.\n");
2901 exit(1);
2903 break;
2904 case QEMU_OPTION_option_rom:
2905 if (nb_option_roms >= MAX_OPTION_ROMS) {
2906 fprintf(stderr, "Too many option ROMs\n");
2907 exit(1);
2909 opts = qemu_opts_parse(qemu_find_opts("option-rom"), optarg, 1);
2910 option_rom[nb_option_roms].name = qemu_opt_get(opts, "romfile");
2911 option_rom[nb_option_roms].bootindex =
2912 qemu_opt_get_number(opts, "bootindex", -1);
2913 if (!option_rom[nb_option_roms].name) {
2914 fprintf(stderr, "Option ROM file is not specified\n");
2915 exit(1);
2917 nb_option_roms++;
2918 break;
2919 case QEMU_OPTION_semihosting:
2920 semihosting_enabled = 1;
2921 break;
2922 case QEMU_OPTION_name:
2923 qemu_name = g_strdup(optarg);
2925 char *p = strchr(qemu_name, ',');
2926 if (p != NULL) {
2927 *p++ = 0;
2928 if (strncmp(p, "process=", 8)) {
2929 fprintf(stderr, "Unknown subargument %s to -name\n", p);
2930 exit(1);
2932 p += 8;
2933 os_set_proc_name(p);
2936 break;
2937 case QEMU_OPTION_prom_env:
2938 if (nb_prom_envs >= MAX_PROM_ENVS) {
2939 fprintf(stderr, "Too many prom variables\n");
2940 exit(1);
2942 prom_envs[nb_prom_envs] = optarg;
2943 nb_prom_envs++;
2944 break;
2945 case QEMU_OPTION_old_param:
2946 old_param = 1;
2947 break;
2948 case QEMU_OPTION_clock:
2949 configure_alarms(optarg);
2950 break;
2951 case QEMU_OPTION_startdate:
2952 configure_rtc_date_offset(optarg, 1);
2953 break;
2954 case QEMU_OPTION_rtc:
2955 opts = qemu_opts_parse(qemu_find_opts("rtc"), optarg, 0);
2956 if (!opts) {
2957 exit(1);
2959 configure_rtc(opts);
2960 break;
2961 case QEMU_OPTION_tb_size:
2962 tcg_tb_size = strtol(optarg, NULL, 0);
2963 if (tcg_tb_size < 0) {
2964 tcg_tb_size = 0;
2966 break;
2967 case QEMU_OPTION_icount:
2968 icount_option = optarg;
2969 break;
2970 case QEMU_OPTION_incoming:
2971 incoming = optarg;
2972 incoming_expected = true;
2973 break;
2974 case QEMU_OPTION_nodefaults:
2975 default_serial = 0;
2976 default_parallel = 0;
2977 default_virtcon = 0;
2978 default_monitor = 0;
2979 default_vga = 0;
2980 default_net = 0;
2981 default_floppy = 0;
2982 default_cdrom = 0;
2983 default_sdcard = 0;
2984 break;
2985 case QEMU_OPTION_xen_domid:
2986 if (!(xen_available())) {
2987 printf("Option %s not supported for this target\n", popt->name);
2988 exit(1);
2990 xen_domid = atoi(optarg);
2991 break;
2992 case QEMU_OPTION_xen_create:
2993 if (!(xen_available())) {
2994 printf("Option %s not supported for this target\n", popt->name);
2995 exit(1);
2997 xen_mode = XEN_CREATE;
2998 break;
2999 case QEMU_OPTION_xen_attach:
3000 if (!(xen_available())) {
3001 printf("Option %s not supported for this target\n", popt->name);
3002 exit(1);
3004 xen_mode = XEN_ATTACH;
3005 break;
3006 case QEMU_OPTION_trace:
3008 opts = qemu_opts_parse(qemu_find_opts("trace"), optarg, 0);
3009 if (!opts) {
3010 exit(1);
3012 trace_events = qemu_opt_get(opts, "events");
3013 trace_file = qemu_opt_get(opts, "file");
3014 break;
3016 case QEMU_OPTION_readconfig:
3018 int ret = qemu_read_config_file(optarg);
3019 if (ret < 0) {
3020 fprintf(stderr, "read config %s: %s\n", optarg,
3021 strerror(-ret));
3022 exit(1);
3024 break;
3026 case QEMU_OPTION_spice:
3027 olist = qemu_find_opts("spice");
3028 if (!olist) {
3029 fprintf(stderr, "spice is not supported by this qemu build.\n");
3030 exit(1);
3032 opts = qemu_opts_parse(olist, optarg, 0);
3033 if (!opts) {
3034 fprintf(stderr, "parse error: %s\n", optarg);
3035 exit(1);
3037 break;
3038 case QEMU_OPTION_writeconfig:
3040 FILE *fp;
3041 if (strcmp(optarg, "-") == 0) {
3042 fp = stdout;
3043 } else {
3044 fp = fopen(optarg, "w");
3045 if (fp == NULL) {
3046 fprintf(stderr, "open %s: %s\n", optarg, strerror(errno));
3047 exit(1);
3050 qemu_config_write(fp);
3051 fclose(fp);
3052 break;
3054 default:
3055 os_parse_cmd_args(popt->index, optarg);
3059 loc_set_none();
3061 /* Open the logfile at this point, if necessary. We can't open the logfile
3062 * when encountering either of the logging options (-d or -D) because the
3063 * other one may be encountered later on the command line, changing the
3064 * location or level of logging.
3066 if (log_mask) {
3067 if (log_file) {
3068 set_cpu_log_filename(log_file);
3070 set_cpu_log(log_mask);
3073 if (!trace_backend_init(trace_events, trace_file)) {
3074 exit(1);
3077 /* If no data_dir is specified then try to find it relative to the
3078 executable path. */
3079 if (!data_dir) {
3080 data_dir = os_find_datadir(argv[0]);
3082 /* If all else fails use the install path specified when building. */
3083 if (!data_dir) {
3084 data_dir = CONFIG_QEMU_DATADIR;
3088 * Default to max_cpus = smp_cpus, in case the user doesn't
3089 * specify a max_cpus value.
3091 if (!max_cpus)
3092 max_cpus = smp_cpus;
3094 machine->max_cpus = machine->max_cpus ?: 1; /* Default to UP */
3095 if (smp_cpus > machine->max_cpus) {
3096 fprintf(stderr, "Number of SMP cpus requested (%d), exceeds max cpus "
3097 "supported by machine `%s' (%d)\n", smp_cpus, machine->name,
3098 machine->max_cpus);
3099 exit(1);
3103 * Get the default machine options from the machine if it is not already
3104 * specified either by the configuration file or by the command line.
3106 if (machine->default_machine_opts) {
3107 QemuOptsList *list = qemu_find_opts("machine");
3108 const char *p = NULL;
3110 if (!QTAILQ_EMPTY(&list->head)) {
3111 p = qemu_opt_get(QTAILQ_FIRST(&list->head), "accel");
3113 if (p == NULL) {
3114 qemu_opts_reset(list);
3115 opts = qemu_opts_parse(list, machine->default_machine_opts, 0);
3116 if (!opts) {
3117 fprintf(stderr, "parse error for machine %s: %s\n",
3118 machine->name, machine->default_machine_opts);
3119 exit(1);
3124 qemu_opts_foreach(qemu_find_opts("device"), default_driver_check, NULL, 0);
3125 qemu_opts_foreach(qemu_find_opts("global"), default_driver_check, NULL, 0);
3127 if (machine->no_serial) {
3128 default_serial = 0;
3130 if (machine->no_parallel) {
3131 default_parallel = 0;
3133 if (!machine->use_virtcon) {
3134 default_virtcon = 0;
3136 if (machine->no_vga) {
3137 default_vga = 0;
3139 if (machine->no_floppy) {
3140 default_floppy = 0;
3142 if (machine->no_cdrom) {
3143 default_cdrom = 0;
3145 if (machine->no_sdcard) {
3146 default_sdcard = 0;
3149 if (display_type == DT_NOGRAPHIC) {
3150 if (default_parallel)
3151 add_device_config(DEV_PARALLEL, "null");
3152 if (default_serial && default_monitor) {
3153 add_device_config(DEV_SERIAL, "mon:stdio");
3154 } else if (default_virtcon && default_monitor) {
3155 add_device_config(DEV_VIRTCON, "mon:stdio");
3156 } else {
3157 if (default_serial)
3158 add_device_config(DEV_SERIAL, "stdio");
3159 if (default_virtcon)
3160 add_device_config(DEV_VIRTCON, "stdio");
3161 if (default_monitor)
3162 monitor_parse("stdio", "readline");
3164 } else {
3165 if (default_serial)
3166 add_device_config(DEV_SERIAL, "vc:80Cx24C");
3167 if (default_parallel)
3168 add_device_config(DEV_PARALLEL, "vc:80Cx24C");
3169 if (default_monitor)
3170 monitor_parse("vc:80Cx24C", "readline");
3171 if (default_virtcon)
3172 add_device_config(DEV_VIRTCON, "vc:80Cx24C");
3174 if (default_vga)
3175 vga_interface_type = VGA_CIRRUS;
3177 socket_init();
3179 if (qemu_opts_foreach(qemu_find_opts("chardev"), chardev_init_func, NULL, 1) != 0)
3180 exit(1);
3181 #ifdef CONFIG_VIRTFS
3182 if (qemu_opts_foreach(qemu_find_opts("fsdev"), fsdev_init_func, NULL, 1) != 0) {
3183 exit(1);
3185 #endif
3187 os_daemonize();
3189 if (pid_file && qemu_create_pidfile(pid_file) != 0) {
3190 os_pidfile_error();
3191 exit(1);
3194 /* init the memory */
3195 if (ram_size == 0) {
3196 ram_size = DEFAULT_RAM_SIZE * 1024 * 1024;
3199 configure_accelerator();
3201 if (qemu_init_main_loop()) {
3202 fprintf(stderr, "qemu_init_main_loop failed\n");
3203 exit(1);
3205 linux_boot = (kernel_filename != NULL);
3207 if (!linux_boot && *kernel_cmdline != '\0') {
3208 fprintf(stderr, "-append only allowed with -kernel option\n");
3209 exit(1);
3212 if (!linux_boot && initrd_filename != NULL) {
3213 fprintf(stderr, "-initrd only allowed with -kernel option\n");
3214 exit(1);
3217 os_set_line_buffering();
3219 if (init_timer_alarm() < 0) {
3220 fprintf(stderr, "could not initialize alarm timer\n");
3221 exit(1);
3223 configure_icount(icount_option);
3225 if (net_init_clients() < 0) {
3226 exit(1);
3229 /* init the bluetooth world */
3230 if (foreach_device_config(DEV_BT, bt_parse))
3231 exit(1);
3233 if (!xen_enabled()) {
3234 /* On 32-bit hosts, QEMU is limited by virtual address space */
3235 if (ram_size > (2047 << 20) && HOST_LONG_BITS == 32) {
3236 fprintf(stderr, "qemu: at most 2047 MB RAM can be simulated\n");
3237 exit(1);
3241 cpu_exec_init_all();
3243 bdrv_init_with_whitelist();
3245 blk_mig_init();
3247 /* open the virtual block devices */
3248 if (snapshot)
3249 qemu_opts_foreach(qemu_find_opts("drive"), drive_enable_snapshot, NULL, 0);
3250 if (qemu_opts_foreach(qemu_find_opts("drive"), drive_init_func, &machine->use_scsi, 1) != 0)
3251 exit(1);
3253 default_drive(default_cdrom, snapshot, machine->use_scsi,
3254 IF_DEFAULT, 2, CDROM_OPTS);
3255 default_drive(default_floppy, snapshot, machine->use_scsi,
3256 IF_FLOPPY, 0, FD_OPTS);
3257 default_drive(default_sdcard, snapshot, machine->use_scsi,
3258 IF_SD, 0, SD_OPTS);
3260 register_savevm_live(NULL, "ram", 0, 4, NULL, ram_save_live, NULL,
3261 ram_load, NULL);
3263 if (nb_numa_nodes > 0) {
3264 int i;
3266 if (nb_numa_nodes > MAX_NODES) {
3267 nb_numa_nodes = MAX_NODES;
3270 /* If no memory size if given for any node, assume the default case
3271 * and distribute the available memory equally across all nodes
3273 for (i = 0; i < nb_numa_nodes; i++) {
3274 if (node_mem[i] != 0)
3275 break;
3277 if (i == nb_numa_nodes) {
3278 uint64_t usedmem = 0;
3280 /* On Linux, the each node's border has to be 8MB aligned,
3281 * the final node gets the rest.
3283 for (i = 0; i < nb_numa_nodes - 1; i++) {
3284 node_mem[i] = (ram_size / nb_numa_nodes) & ~((1 << 23UL) - 1);
3285 usedmem += node_mem[i];
3287 node_mem[i] = ram_size - usedmem;
3290 for (i = 0; i < nb_numa_nodes; i++) {
3291 if (node_cpumask[i] != 0)
3292 break;
3294 /* assigning the VCPUs round-robin is easier to implement, guest OSes
3295 * must cope with this anyway, because there are BIOSes out there in
3296 * real machines which also use this scheme.
3298 if (i == nb_numa_nodes) {
3299 for (i = 0; i < smp_cpus; i++) {
3300 node_cpumask[i % nb_numa_nodes] |= 1 << i;
3305 if (qemu_opts_foreach(qemu_find_opts("mon"), mon_init_func, NULL, 1) != 0) {
3306 exit(1);
3309 if (foreach_device_config(DEV_SERIAL, serial_parse) < 0)
3310 exit(1);
3311 if (foreach_device_config(DEV_PARALLEL, parallel_parse) < 0)
3312 exit(1);
3313 if (foreach_device_config(DEV_VIRTCON, virtcon_parse) < 0)
3314 exit(1);
3315 if (foreach_device_config(DEV_DEBUGCON, debugcon_parse) < 0)
3316 exit(1);
3318 module_call_init(MODULE_INIT_DEVICE);
3320 if (qemu_opts_foreach(qemu_find_opts("device"), device_help_func, NULL, 0) != 0)
3321 exit(0);
3323 if (watchdog) {
3324 i = select_watchdog(watchdog);
3325 if (i > 0)
3326 exit (i == 1 ? 1 : 0);
3329 if (machine->compat_props) {
3330 qdev_prop_register_global_list(machine->compat_props);
3332 qemu_add_globals();
3334 machine->init(ram_size, boot_devices,
3335 kernel_filename, kernel_cmdline, initrd_filename, cpu_model);
3337 cpu_synchronize_all_post_init();
3339 set_numa_modes();
3341 current_machine = machine;
3343 /* init USB devices */
3344 if (usb_enabled) {
3345 if (foreach_device_config(DEV_USB, usb_parse) < 0)
3346 exit(1);
3349 /* init generic devices */
3350 if (qemu_opts_foreach(qemu_find_opts("device"), device_init_func, NULL, 1) != 0)
3351 exit(1);
3353 net_check_clients();
3355 /* just use the first displaystate for the moment */
3356 ds = get_displaystate();
3358 if (using_spice)
3359 display_remote++;
3360 if (display_type == DT_DEFAULT && !display_remote) {
3361 #if defined(CONFIG_SDL) || defined(CONFIG_COCOA)
3362 display_type = DT_SDL;
3363 #elif defined(CONFIG_VNC)
3364 vnc_display = "localhost:0,to=99";
3365 show_vnc_port = 1;
3366 #else
3367 display_type = DT_NONE;
3368 #endif
3372 /* init local displays */
3373 switch (display_type) {
3374 case DT_NOGRAPHIC:
3375 break;
3376 #if defined(CONFIG_CURSES)
3377 case DT_CURSES:
3378 curses_display_init(ds, full_screen);
3379 break;
3380 #endif
3381 #if defined(CONFIG_SDL)
3382 case DT_SDL:
3383 sdl_display_init(ds, full_screen, no_frame);
3384 break;
3385 #elif defined(CONFIG_COCOA)
3386 case DT_SDL:
3387 cocoa_display_init(ds, full_screen);
3388 break;
3389 #endif
3390 default:
3391 break;
3394 /* must be after terminal init, SDL library changes signal handlers */
3395 os_setup_signal_handling();
3397 #ifdef CONFIG_VNC
3398 /* init remote displays */
3399 if (vnc_display) {
3400 vnc_display_init(ds);
3401 if (vnc_display_open(ds, vnc_display) < 0)
3402 exit(1);
3404 if (show_vnc_port) {
3405 printf("VNC server running on `%s'\n", vnc_display_local_addr(ds));
3408 #endif
3409 #ifdef CONFIG_SPICE
3410 if (using_spice && !qxl_enabled) {
3411 qemu_spice_display_init(ds);
3413 #endif
3415 /* display setup */
3416 dpy_resize(ds);
3417 dcl = ds->listeners;
3418 while (dcl != NULL) {
3419 if (dcl->dpy_refresh != NULL) {
3420 ds->gui_timer = qemu_new_timer_ms(rt_clock, gui_update, ds);
3421 qemu_mod_timer(ds->gui_timer, qemu_get_clock_ms(rt_clock));
3422 break;
3424 dcl = dcl->next;
3426 if (ds->gui_timer == NULL) {
3427 nographic_timer = qemu_new_timer_ms(rt_clock, nographic_update, NULL);
3428 qemu_mod_timer(nographic_timer, qemu_get_clock_ms(rt_clock));
3430 text_consoles_set_display(ds);
3432 if (gdbstub_dev && gdbserver_start(gdbstub_dev) < 0) {
3433 fprintf(stderr, "qemu: could not open gdbserver on device '%s'\n",
3434 gdbstub_dev);
3435 exit(1);
3438 qdev_machine_creation_done();
3440 if (rom_load_all() != 0) {
3441 fprintf(stderr, "rom loading failed\n");
3442 exit(1);
3445 /* TODO: once all bus devices are qdevified, this should be done
3446 * when bus is created by qdev.c */
3447 qemu_register_reset(qbus_reset_all_fn, sysbus_get_default());
3448 qemu_run_machine_init_done_notifiers();
3450 qemu_system_reset(VMRESET_SILENT);
3451 if (loadvm) {
3452 if (load_vmstate(loadvm) < 0) {
3453 autostart = 0;
3457 if (incoming) {
3458 runstate_set(RSTATE_IN_MIGRATE);
3459 int ret = qemu_start_incoming_migration(incoming);
3460 if (ret < 0) {
3461 fprintf(stderr, "Migration failed. Exit code %s(%d), exiting.\n",
3462 incoming, ret);
3463 exit(ret);
3465 } else if (autostart) {
3466 vm_start();
3467 } else {
3468 runstate_set(RSTATE_PRE_LAUNCH);
3471 os_setup_post();
3473 main_loop();
3474 quit_timers();
3475 net_cleanup();
3476 res_free();
3478 return 0;