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
31 #include "qemu/bitmap.h"
33 /* Needed early for CONFIG_BSD etc. */
34 #include "config-host.h"
38 #include <sys/times.h>
42 #include <sys/ioctl.h>
43 #include <sys/resource.h>
44 #include <sys/socket.h>
45 #include <netinet/in.h>
47 #include <arpa/inet.h>
50 #include <sys/select.h>
54 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__DragonFly__)
55 #include <sys/sysctl.h>
63 #include <linux/ppdev.h>
64 #include <linux/parport.h>
68 #include "sysemu/seccomp.h"
73 #include <sys/ethernet.h>
74 #include <sys/sockio.h>
75 #include <netinet/arp.h>
76 #include <netinet/in_systm.h>
77 #include <netinet/ip.h>
78 #include <netinet/ip_icmp.h> // must come after ip.h
79 #include <netinet/udp.h>
80 #include <netinet/tcp.h>
88 #if defined(CONFIG_VDE)
89 #include <libvdeplug.h>
97 #if defined(__APPLE__) || defined(main)
99 int qemu_main(int argc, char **argv, char **envp);
100 int main(int argc, char **argv)
102 return qemu_main(argc, argv, NULL);
105 #define main qemu_main
107 #endif /* CONFIG_SDL */
111 #define main qemu_main
112 #endif /* CONFIG_COCOA */
117 #include "hw/boards.h"
119 #include "hw/pcmcia.h"
124 #include "hw/watchdog.h"
125 #include "hw/smbios.h"
128 #include "hw/loader.h"
131 #include "net/slirp.h"
132 #include "monitor/monitor.h"
133 #include "ui/console.h"
134 #include "sysemu/sysemu.h"
135 #include "exec/gdbstub.h"
136 #include "qemu/timer.h"
137 #include "char/char.h"
138 #include "qemu/cache-utils.h"
139 #include "sysemu/blockdev.h"
140 #include "hw/block-common.h"
141 #include "migration/block.h"
142 #include "sysemu/dma.h"
143 #include "audio/audio.h"
144 #include "migration/migration.h"
145 #include "sysemu/kvm.h"
146 #include "qapi/qmp/qjson.h"
147 #include "qemu/option.h"
148 #include "qemu/config-file.h"
149 #include "qemu-options.h"
150 #include "qmp-commands.h"
151 #include "qemu/main-loop.h"
153 #include "fsdev/qemu-fsdev.h"
155 #include "sysemu/qtest.h"
157 #include "disas/disas.h"
159 #include "qemu/sockets.h"
161 #include "slirp/libslirp.h"
164 #include "trace/control.h"
165 #include "qemu/queue.h"
166 #include "sysemu/cpus.h"
167 #include "sysemu/arch_init.h"
168 #include "qemu/osdep.h"
170 #include "ui/qemu-spice.h"
171 #include "qapi/string-input-visitor.h"
174 //#define DEBUG_SLIRP
176 #define DEFAULT_RAM_SIZE 128
178 #define MAX_VIRTIO_CONSOLES 1
180 static const char *data_dir;
181 const char *bios_name = NULL;
182 enum vga_retrace_method vga_retrace_method = VGA_RETRACE_DUMB;
183 DisplayType display_type = DT_DEFAULT;
184 static int display_remote;
185 const char* keyboard_layout = NULL;
187 const char *mem_path = NULL;
189 int mem_prealloc = 0; /* force preallocation of physical target memory */
192 NICInfo nd_table[MAX_NICS];
194 static int rtc_utc = 1;
195 static int rtc_date_offset = -1; /* -1 means no change */
196 QEMUClock *rtc_clock;
197 int vga_interface_type = VGA_NONE;
198 static int full_screen = 0;
200 static int no_frame = 0;
203 CharDriverState *serial_hds[MAX_SERIAL_PORTS];
204 CharDriverState *parallel_hds[MAX_PARALLEL_PORTS];
205 CharDriverState *virtcon_hds[MAX_VIRTIO_CONSOLES];
206 int win2k_install_hack = 0;
213 const char *vnc_display;
215 int acpi_enabled = 1;
218 static int no_reboot;
221 int graphic_rotate = 0;
222 const char *watchdog;
223 QEMUOptionRom option_rom[MAX_OPTION_ROMS];
225 int semihosting_enabled = 0;
227 const char *qemu_name;
230 unsigned int nb_prom_envs = 0;
231 const char *prom_envs[MAX_PROM_ENVS];
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;
240 QTAILQ_ENTRY(FWBootEntry) link;
246 static QTAILQ_HEAD(, FWBootEntry) fw_boot_order =
247 QTAILQ_HEAD_INITIALIZER(fw_boot_order);
250 uint64_t node_mem[MAX_NODES];
251 unsigned long *node_cpumask[MAX_NODES];
253 uint8_t qemu_uuid[16];
255 static QEMUBootSetHandler *boot_set_handler;
256 static void *boot_set_opaque;
258 static NotifierList exit_notifiers =
259 NOTIFIER_LIST_INITIALIZER(exit_notifiers);
261 static NotifierList machine_init_done_notifiers =
262 NOTIFIER_LIST_INITIALIZER(machine_init_done_notifiers);
264 static int tcg_allowed = 1;
268 enum xen_mode xen_mode = XEN_EMULATE;
269 static int tcg_tb_size;
271 static int default_serial = 1;
272 static int default_parallel = 1;
273 static int default_virtcon = 1;
274 static int default_monitor = 1;
275 static int default_floppy = 1;
276 static int default_cdrom = 1;
277 static int default_sdcard = 1;
278 static int default_vga = 1;
284 { .driver = "isa-serial", .flag = &default_serial },
285 { .driver = "isa-parallel", .flag = &default_parallel },
286 { .driver = "isa-fdc", .flag = &default_floppy },
287 { .driver = "ide-cd", .flag = &default_cdrom },
288 { .driver = "ide-hd", .flag = &default_cdrom },
289 { .driver = "ide-drive", .flag = &default_cdrom },
290 { .driver = "scsi-cd", .flag = &default_cdrom },
291 { .driver = "virtio-serial-pci", .flag = &default_virtcon },
292 { .driver = "virtio-serial-s390", .flag = &default_virtcon },
293 { .driver = "virtio-serial", .flag = &default_virtcon },
294 { .driver = "VGA", .flag = &default_vga },
295 { .driver = "isa-vga", .flag = &default_vga },
296 { .driver = "cirrus-vga", .flag = &default_vga },
297 { .driver = "isa-cirrus-vga", .flag = &default_vga },
298 { .driver = "vmware-svga", .flag = &default_vga },
299 { .driver = "qxl-vga", .flag = &default_vga },
302 static QemuOptsList qemu_rtc_opts = {
304 .head = QTAILQ_HEAD_INITIALIZER(qemu_rtc_opts.head),
308 .type = QEMU_OPT_STRING,
311 .type = QEMU_OPT_STRING,
314 .type = QEMU_OPT_STRING,
316 { /* end of list */ }
320 static QemuOptsList qemu_sandbox_opts = {
322 .implied_opt_name = "enable",
323 .head = QTAILQ_HEAD_INITIALIZER(qemu_sandbox_opts.head),
327 .type = QEMU_OPT_BOOL,
329 { /* end of list */ }
333 static QemuOptsList qemu_trace_opts = {
335 .implied_opt_name = "trace",
336 .head = QTAILQ_HEAD_INITIALIZER(qemu_trace_opts.head),
340 .type = QEMU_OPT_STRING,
343 .type = QEMU_OPT_STRING,
345 { /* end of list */ }
349 static QemuOptsList qemu_option_rom_opts = {
350 .name = "option-rom",
351 .implied_opt_name = "romfile",
352 .head = QTAILQ_HEAD_INITIALIZER(qemu_option_rom_opts.head),
356 .type = QEMU_OPT_NUMBER,
359 .type = QEMU_OPT_STRING,
361 { /* end of list */ }
365 static QemuOptsList qemu_machine_opts = {
367 .implied_opt_name = "type",
369 .head = QTAILQ_HEAD_INITIALIZER(qemu_machine_opts.head),
373 .type = QEMU_OPT_STRING,
374 .help = "emulated machine"
377 .type = QEMU_OPT_STRING,
378 .help = "accelerator list",
380 .name = "kernel_irqchip",
381 .type = QEMU_OPT_BOOL,
382 .help = "use KVM in-kernel irqchip",
384 .name = "kvm_shadow_mem",
385 .type = QEMU_OPT_SIZE,
386 .help = "KVM shadow MMU size",
389 .type = QEMU_OPT_STRING,
390 .help = "Linux kernel image file",
393 .type = QEMU_OPT_STRING,
394 .help = "Linux initial ramdisk file",
397 .type = QEMU_OPT_STRING,
398 .help = "Linux kernel command line",
401 .type = QEMU_OPT_STRING,
402 .help = "Linux kernel device tree file",
405 .type = QEMU_OPT_STRING,
406 .help = "Dump current dtb to a file and quit",
408 .name = "phandle_start",
409 .type = QEMU_OPT_STRING,
410 .help = "The first phandle ID we may generate dynamically",
412 .name = "dt_compatible",
413 .type = QEMU_OPT_STRING,
414 .help = "Overrides the \"compatible\" property of the dt root node",
416 .name = "dump-guest-core",
417 .type = QEMU_OPT_BOOL,
418 .help = "Include guest memory in a core dump",
421 .type = QEMU_OPT_BOOL,
422 .help = "enable/disable memory merge support",
425 .type = QEMU_OPT_BOOL,
426 .help = "Set on/off to enable/disable usb",
428 { /* End of list */ }
432 static QemuOptsList qemu_boot_opts = {
434 .head = QTAILQ_HEAD_INITIALIZER(qemu_boot_opts.head),
436 /* the three names below are not used now */
439 .type = QEMU_OPT_STRING,
442 .type = QEMU_OPT_STRING,
445 .type = QEMU_OPT_STRING,
446 /* following are really used */
449 .type = QEMU_OPT_STRING,
451 .name = "splash-time",
452 .type = QEMU_OPT_STRING,
454 .name = "reboot-timeout",
455 .type = QEMU_OPT_STRING,
461 static QemuOptsList qemu_add_fd_opts = {
463 .head = QTAILQ_HEAD_INITIALIZER(qemu_add_fd_opts.head),
467 .type = QEMU_OPT_NUMBER,
468 .help = "file descriptor of which a duplicate is added to fd set",
471 .type = QEMU_OPT_NUMBER,
472 .help = "ID of the fd set to add fd to",
475 .type = QEMU_OPT_STRING,
476 .help = "free-form string used to describe fd",
478 { /* end of list */ }
482 static QemuOptsList qemu_object_opts = {
484 .implied_opt_name = "qom-type",
485 .head = QTAILQ_HEAD_INITIALIZER(qemu_object_opts.head),
491 const char *qemu_get_vm_name(void)
496 static void res_free(void)
498 if (boot_splash_filedata != NULL) {
499 g_free(boot_splash_filedata);
500 boot_splash_filedata = NULL;
504 static int default_driver_check(QemuOpts *opts, void *opaque)
506 const char *driver = qemu_opt_get(opts, "driver");
511 for (i = 0; i < ARRAY_SIZE(default_list); i++) {
512 if (strcmp(default_list[i].driver, driver) != 0)
514 *(default_list[i].flag) = 0;
519 /***********************************************************/
522 static RunState current_run_state = RUN_STATE_PRELAUNCH;
527 } RunStateTransition;
529 static const RunStateTransition runstate_transitions_def[] = {
531 { RUN_STATE_DEBUG, RUN_STATE_RUNNING },
533 { RUN_STATE_INMIGRATE, RUN_STATE_RUNNING },
534 { RUN_STATE_INMIGRATE, RUN_STATE_PAUSED },
536 { RUN_STATE_INTERNAL_ERROR, RUN_STATE_PAUSED },
537 { RUN_STATE_INTERNAL_ERROR, RUN_STATE_FINISH_MIGRATE },
539 { RUN_STATE_IO_ERROR, RUN_STATE_RUNNING },
540 { RUN_STATE_IO_ERROR, RUN_STATE_FINISH_MIGRATE },
542 { RUN_STATE_PAUSED, RUN_STATE_RUNNING },
543 { RUN_STATE_PAUSED, RUN_STATE_FINISH_MIGRATE },
545 { RUN_STATE_POSTMIGRATE, RUN_STATE_RUNNING },
546 { RUN_STATE_POSTMIGRATE, RUN_STATE_FINISH_MIGRATE },
548 { RUN_STATE_PRELAUNCH, RUN_STATE_RUNNING },
549 { RUN_STATE_PRELAUNCH, RUN_STATE_FINISH_MIGRATE },
550 { RUN_STATE_PRELAUNCH, RUN_STATE_INMIGRATE },
552 { RUN_STATE_FINISH_MIGRATE, RUN_STATE_RUNNING },
553 { RUN_STATE_FINISH_MIGRATE, RUN_STATE_POSTMIGRATE },
555 { RUN_STATE_RESTORE_VM, RUN_STATE_RUNNING },
557 { RUN_STATE_RUNNING, RUN_STATE_DEBUG },
558 { RUN_STATE_RUNNING, RUN_STATE_INTERNAL_ERROR },
559 { RUN_STATE_RUNNING, RUN_STATE_IO_ERROR },
560 { RUN_STATE_RUNNING, RUN_STATE_PAUSED },
561 { RUN_STATE_RUNNING, RUN_STATE_FINISH_MIGRATE },
562 { RUN_STATE_RUNNING, RUN_STATE_RESTORE_VM },
563 { RUN_STATE_RUNNING, RUN_STATE_SAVE_VM },
564 { RUN_STATE_RUNNING, RUN_STATE_SHUTDOWN },
565 { RUN_STATE_RUNNING, RUN_STATE_WATCHDOG },
567 { RUN_STATE_SAVE_VM, RUN_STATE_RUNNING },
569 { RUN_STATE_SHUTDOWN, RUN_STATE_PAUSED },
570 { RUN_STATE_SHUTDOWN, RUN_STATE_FINISH_MIGRATE },
572 { RUN_STATE_DEBUG, RUN_STATE_SUSPENDED },
573 { RUN_STATE_RUNNING, RUN_STATE_SUSPENDED },
574 { RUN_STATE_SUSPENDED, RUN_STATE_RUNNING },
575 { RUN_STATE_SUSPENDED, RUN_STATE_FINISH_MIGRATE },
577 { RUN_STATE_WATCHDOG, RUN_STATE_RUNNING },
578 { RUN_STATE_WATCHDOG, RUN_STATE_FINISH_MIGRATE },
580 { RUN_STATE_MAX, RUN_STATE_MAX },
583 static bool runstate_valid_transitions[RUN_STATE_MAX][RUN_STATE_MAX];
585 bool runstate_check(RunState state)
587 return current_run_state == state;
590 static void runstate_init(void)
592 const RunStateTransition *p;
594 memset(&runstate_valid_transitions, 0, sizeof(runstate_valid_transitions));
596 for (p = &runstate_transitions_def[0]; p->from != RUN_STATE_MAX; p++) {
597 runstate_valid_transitions[p->from][p->to] = true;
601 /* This function will abort() on invalid state transitions */
602 void runstate_set(RunState new_state)
604 assert(new_state < RUN_STATE_MAX);
606 if (!runstate_valid_transitions[current_run_state][new_state]) {
607 fprintf(stderr, "ERROR: invalid runstate transition: '%s' -> '%s'\n",
608 RunState_lookup[current_run_state],
609 RunState_lookup[new_state]);
613 current_run_state = new_state;
616 int runstate_is_running(void)
618 return runstate_check(RUN_STATE_RUNNING);
621 StatusInfo *qmp_query_status(Error **errp)
623 StatusInfo *info = g_malloc0(sizeof(*info));
625 info->running = runstate_is_running();
626 info->singlestep = singlestep;
627 info->status = current_run_state;
632 /***********************************************************/
633 /* real time host monotonic timer */
635 /***********************************************************/
636 /* host time/date access */
637 void qemu_get_timedate(struct tm *tm, int offset)
643 if (rtc_date_offset == -1) {
647 localtime_r(&ti, tm);
649 ti -= rtc_date_offset;
654 int qemu_timedate_diff(struct tm *tm)
658 if (rtc_date_offset == -1)
660 seconds = mktimegm(tm);
663 tmp.tm_isdst = -1; /* use timezone to figure it out */
664 seconds = mktime(&tmp);
667 seconds = mktimegm(tm) + rtc_date_offset;
669 return seconds - time(NULL);
672 void rtc_change_mon_event(struct tm *tm)
676 data = qobject_from_jsonf("{ 'offset': %d }", qemu_timedate_diff(tm));
677 monitor_protocol_event(QEVENT_RTC_CHANGE, data);
678 qobject_decref(data);
681 static void configure_rtc_date_offset(const char *startdate, int legacy)
683 time_t rtc_start_date;
686 if (!strcmp(startdate, "now") && legacy) {
687 rtc_date_offset = -1;
689 if (sscanf(startdate, "%d-%d-%dT%d:%d:%d",
697 } else if (sscanf(startdate, "%d-%d-%d",
709 rtc_start_date = mktimegm(&tm);
710 if (rtc_start_date == -1) {
712 fprintf(stderr, "Invalid date format. Valid formats are:\n"
713 "'2006-06-17T16:01:21' or '2006-06-17'\n");
716 rtc_date_offset = time(NULL) - rtc_start_date;
720 static void configure_rtc(QemuOpts *opts)
724 value = qemu_opt_get(opts, "base");
726 if (!strcmp(value, "utc")) {
728 } else if (!strcmp(value, "localtime")) {
731 configure_rtc_date_offset(value, 0);
734 value = qemu_opt_get(opts, "clock");
736 if (!strcmp(value, "host")) {
737 rtc_clock = host_clock;
738 } else if (!strcmp(value, "rt")) {
739 rtc_clock = rt_clock;
740 } else if (!strcmp(value, "vm")) {
741 rtc_clock = vm_clock;
743 fprintf(stderr, "qemu: invalid option value '%s'\n", value);
747 value = qemu_opt_get(opts, "driftfix");
749 if (!strcmp(value, "slew")) {
750 static GlobalProperty slew_lost_ticks[] = {
752 .driver = "mc146818rtc",
753 .property = "lost_tick_policy",
756 { /* end of list */ }
759 qdev_prop_register_global_list(slew_lost_ticks);
760 } else if (!strcmp(value, "none")) {
761 /* discard is default */
763 fprintf(stderr, "qemu: invalid option value '%s'\n", value);
769 /***********************************************************/
770 /* Bluetooth support */
773 static struct HCIInfo *hci_table[MAX_NICS];
775 static struct bt_vlan_s {
776 struct bt_scatternet_s net;
778 struct bt_vlan_s *next;
781 /* find or alloc a new bluetooth "VLAN" */
782 static struct bt_scatternet_s *qemu_find_bt_vlan(int id)
784 struct bt_vlan_s **pvlan, *vlan;
785 for (vlan = first_bt_vlan; vlan != NULL; vlan = vlan->next) {
789 vlan = g_malloc0(sizeof(struct bt_vlan_s));
791 pvlan = &first_bt_vlan;
792 while (*pvlan != NULL)
793 pvlan = &(*pvlan)->next;
798 static void null_hci_send(struct HCIInfo *hci, const uint8_t *data, int len)
802 static int null_hci_addr_set(struct HCIInfo *hci, const uint8_t *bd_addr)
807 static struct HCIInfo null_hci = {
808 .cmd_send = null_hci_send,
809 .sco_send = null_hci_send,
810 .acl_send = null_hci_send,
811 .bdaddr_set = null_hci_addr_set,
814 struct HCIInfo *qemu_next_hci(void)
816 if (cur_hci == nb_hcis)
819 return hci_table[cur_hci++];
822 static struct HCIInfo *hci_init(const char *str)
825 struct bt_scatternet_s *vlan = 0;
827 if (!strcmp(str, "null"))
830 else if (!strncmp(str, "host", 4) && (str[4] == '\0' || str[4] == ':'))
832 return bt_host_hci(str[4] ? str + 5 : "hci0");
833 else if (!strncmp(str, "hci", 3)) {
836 if (!strncmp(str + 3, ",vlan=", 6)) {
837 vlan = qemu_find_bt_vlan(strtol(str + 9, &endp, 0));
842 vlan = qemu_find_bt_vlan(0);
844 return bt_new_hci(vlan);
847 fprintf(stderr, "qemu: Unknown bluetooth HCI `%s'.\n", str);
852 static int bt_hci_parse(const char *str)
857 if (nb_hcis >= MAX_NICS) {
858 fprintf(stderr, "qemu: Too many bluetooth HCIs (max %i).\n", MAX_NICS);
871 bdaddr.b[5] = 0x56 + nb_hcis;
872 hci->bdaddr_set(hci, bdaddr.b);
874 hci_table[nb_hcis++] = hci;
879 static void bt_vhci_add(int vlan_id)
881 struct bt_scatternet_s *vlan = qemu_find_bt_vlan(vlan_id);
884 fprintf(stderr, "qemu: warning: adding a VHCI to "
885 "an empty scatternet %i\n", vlan_id);
887 bt_vhci_init(bt_new_hci(vlan));
890 static struct bt_device_s *bt_device_add(const char *opt)
892 struct bt_scatternet_s *vlan;
894 char *endp = strstr(opt, ",vlan=");
895 int len = (endp ? endp - opt : strlen(opt)) + 1;
898 pstrcpy(devname, MIN(sizeof(devname), len), opt);
901 vlan_id = strtol(endp + 6, &endp, 0);
903 fprintf(stderr, "qemu: unrecognised bluetooth vlan Id\n");
908 vlan = qemu_find_bt_vlan(vlan_id);
911 fprintf(stderr, "qemu: warning: adding a slave device to "
912 "an empty scatternet %i\n", vlan_id);
914 if (!strcmp(devname, "keyboard"))
915 return bt_keyboard_init(vlan);
917 fprintf(stderr, "qemu: unsupported bluetooth device `%s'\n", devname);
921 static int bt_parse(const char *opt)
923 const char *endp, *p;
926 if (strstart(opt, "hci", &endp)) {
927 if (!*endp || *endp == ',') {
929 if (!strstart(endp, ",vlan=", 0))
932 return bt_hci_parse(opt);
934 } else if (strstart(opt, "vhci", &endp)) {
935 if (!*endp || *endp == ',') {
937 if (strstart(endp, ",vlan=", &p)) {
938 vlan = strtol(p, (char **) &endp, 0);
940 fprintf(stderr, "qemu: bad scatternet '%s'\n", p);
944 fprintf(stderr, "qemu: bad parameter '%s'\n", endp + 1);
953 } else if (strstart(opt, "device:", &endp))
954 return !bt_device_add(endp);
956 fprintf(stderr, "qemu: bad bluetooth parameter '%s'\n", opt);
960 static int parse_sandbox(QemuOpts *opts, void *opaque)
962 /* FIXME: change this to true for 1.3 */
963 if (qemu_opt_get_bool(opts, "enable", false)) {
964 #ifdef CONFIG_SECCOMP
965 if (seccomp_start() < 0) {
966 qerror_report(ERROR_CLASS_GENERIC_ERROR,
967 "failed to install seccomp syscall filter in the kernel");
971 qerror_report(ERROR_CLASS_GENERIC_ERROR,
972 "sandboxing request but seccomp is not compiled into this build");
980 /*********QEMU USB setting******/
981 bool usb_enabled(bool default_usb)
984 mach_opts = qemu_opts_find(qemu_find_opts("machine"), 0);
986 return qemu_opt_get_bool(mach_opts, "usb", default_usb);
992 static int parse_add_fd(QemuOpts *opts, void *opaque)
994 int fd, dupfd, flags;
996 const char *fd_opaque = NULL;
998 fd = qemu_opt_get_number(opts, "fd", -1);
999 fdset_id = qemu_opt_get_number(opts, "set", -1);
1000 fd_opaque = qemu_opt_get(opts, "opaque");
1003 qerror_report(ERROR_CLASS_GENERIC_ERROR,
1004 "fd option is required and must be non-negative");
1008 if (fd <= STDERR_FILENO) {
1009 qerror_report(ERROR_CLASS_GENERIC_ERROR,
1010 "fd cannot be a standard I/O stream");
1015 * All fds inherited across exec() necessarily have FD_CLOEXEC
1016 * clear, while qemu sets FD_CLOEXEC on all other fds used internally.
1018 flags = fcntl(fd, F_GETFD);
1019 if (flags == -1 || (flags & FD_CLOEXEC)) {
1020 qerror_report(ERROR_CLASS_GENERIC_ERROR,
1021 "fd is not valid or already in use");
1026 qerror_report(ERROR_CLASS_GENERIC_ERROR,
1027 "set option is required and must be non-negative");
1031 #ifdef F_DUPFD_CLOEXEC
1032 dupfd = fcntl(fd, F_DUPFD_CLOEXEC, 0);
1036 qemu_set_cloexec(dupfd);
1040 qerror_report(ERROR_CLASS_GENERIC_ERROR,
1041 "Error duplicating fd: %s", strerror(errno));
1045 /* add the duplicate fd, and optionally the opaque string, to the fd set */
1046 monitor_fdset_add_fd(dupfd, true, fdset_id, fd_opaque ? true : false,
1052 static int cleanup_add_fd(QemuOpts *opts, void *opaque)
1056 fd = qemu_opt_get_number(opts, "fd", -1);
1063 /***********************************************************/
1064 /* QEMU Block devices */
1066 #define HD_OPTS "media=disk"
1067 #define CDROM_OPTS "media=cdrom"
1069 #define PFLASH_OPTS ""
1073 static int drive_init_func(QemuOpts *opts, void *opaque)
1075 BlockInterfaceType *block_default_type = opaque;
1077 return drive_init(opts, *block_default_type) == NULL;
1080 static int drive_enable_snapshot(QemuOpts *opts, void *opaque)
1082 if (NULL == qemu_opt_get(opts, "snapshot")) {
1083 qemu_opt_set(opts, "snapshot", "on");
1088 static void default_drive(int enable, int snapshot, BlockInterfaceType type,
1089 int index, const char *optstr)
1093 if (!enable || drive_get_by_index(type, index)) {
1097 opts = drive_add(type, index, NULL, optstr);
1099 drive_enable_snapshot(opts, NULL);
1101 if (!drive_init(opts, type)) {
1106 void qemu_register_boot_set(QEMUBootSetHandler *func, void *opaque)
1108 boot_set_handler = func;
1109 boot_set_opaque = opaque;
1112 int qemu_boot_set(const char *boot_devices)
1114 if (!boot_set_handler) {
1117 return boot_set_handler(boot_set_opaque, boot_devices);
1120 static void validate_bootdevices(char *devices)
1122 /* We just do some generic consistency checks */
1126 for (p = devices; *p != '\0'; p++) {
1127 /* Allowed boot devices are:
1128 * a-b: floppy disk drives
1129 * c-f: IDE disk drives
1130 * g-m: machine implementation dependent drives
1131 * n-p: network devices
1132 * It's up to each machine implementation to check if the given boot
1133 * devices match the actual hardware implementation and firmware
1136 if (*p < 'a' || *p > 'p') {
1137 fprintf(stderr, "Invalid boot device '%c'\n", *p);
1140 if (bitmap & (1 << (*p - 'a'))) {
1141 fprintf(stderr, "Boot device '%c' was given twice\n", *p);
1144 bitmap |= 1 << (*p - 'a');
1148 static void restore_boot_devices(void *opaque)
1150 char *standard_boot_devices = opaque;
1151 static int first = 1;
1153 /* Restore boot order and remove ourselves after the first boot */
1159 qemu_boot_set(standard_boot_devices);
1161 qemu_unregister_reset(restore_boot_devices, standard_boot_devices);
1162 g_free(standard_boot_devices);
1165 void add_boot_device_path(int32_t bootindex, DeviceState *dev,
1168 FWBootEntry *node, *i;
1170 if (bootindex < 0) {
1174 assert(dev != NULL || suffix != NULL);
1176 node = g_malloc0(sizeof(FWBootEntry));
1177 node->bootindex = bootindex;
1178 node->suffix = suffix ? g_strdup(suffix) : NULL;
1181 QTAILQ_FOREACH(i, &fw_boot_order, link) {
1182 if (i->bootindex == bootindex) {
1183 fprintf(stderr, "Two devices with same boot index %d\n", bootindex);
1185 } else if (i->bootindex < bootindex) {
1188 QTAILQ_INSERT_BEFORE(i, node, link);
1191 QTAILQ_INSERT_TAIL(&fw_boot_order, node, link);
1195 * This function returns null terminated string that consist of new line
1196 * separated device paths.
1198 * memory pointed by "size" is assigned total length of the array in bytes
1201 char *get_boot_devices_list(uint32_t *size)
1207 QTAILQ_FOREACH(i, &fw_boot_order, link) {
1208 char *devpath = NULL, *bootpath;
1212 devpath = qdev_get_fw_dev_path(i->dev);
1216 if (i->suffix && devpath) {
1217 size_t bootpathlen = strlen(devpath) + strlen(i->suffix) + 1;
1219 bootpath = g_malloc(bootpathlen);
1220 snprintf(bootpath, bootpathlen, "%s%s", devpath, i->suffix);
1222 } else if (devpath) {
1226 bootpath = g_strdup(i->suffix);
1230 list[total-1] = '\n';
1232 len = strlen(bootpath) + 1;
1233 list = g_realloc(list, total + len);
1234 memcpy(&list[total], bootpath, len);
1244 static void numa_add(const char *optarg)
1248 unsigned long long value, endvalue;
1251 value = endvalue = 0ULL;
1253 optarg = get_opt_name(option, 128, optarg, ',') + 1;
1254 if (!strcmp(option, "node")) {
1255 if (get_param_value(option, 128, "nodeid", optarg) == 0) {
1256 nodenr = nb_numa_nodes;
1258 nodenr = strtoull(option, NULL, 10);
1261 if (get_param_value(option, 128, "mem", optarg) == 0) {
1262 node_mem[nodenr] = 0;
1265 sval = strtosz(option, &endptr);
1266 if (sval < 0 || *endptr) {
1267 fprintf(stderr, "qemu: invalid numa mem size: %s\n", optarg);
1270 node_mem[nodenr] = sval;
1272 if (get_param_value(option, 128, "cpus", optarg) != 0) {
1273 value = strtoull(option, &endptr, 10);
1274 if (*endptr == '-') {
1275 endvalue = strtoull(endptr+1, &endptr, 10);
1280 if (!(endvalue < MAX_CPUMASK_BITS)) {
1281 endvalue = MAX_CPUMASK_BITS - 1;
1283 "A max of %d CPUs are supported in a guest\n",
1287 bitmap_set(node_cpumask[nodenr], value, endvalue-value+1);
1293 static void smp_parse(const char *optarg)
1295 int smp, sockets = 0, threads = 0, cores = 0;
1299 smp = strtoul(optarg, &endptr, 10);
1300 if (endptr != optarg) {
1301 if (*endptr == ',') {
1305 if (get_param_value(option, 128, "sockets", endptr) != 0)
1306 sockets = strtoull(option, NULL, 10);
1307 if (get_param_value(option, 128, "cores", endptr) != 0)
1308 cores = strtoull(option, NULL, 10);
1309 if (get_param_value(option, 128, "threads", endptr) != 0)
1310 threads = strtoull(option, NULL, 10);
1311 if (get_param_value(option, 128, "maxcpus", endptr) != 0)
1312 max_cpus = strtoull(option, NULL, 10);
1314 /* compute missing values, prefer sockets over cores over threads */
1315 if (smp == 0 || sockets == 0) {
1316 sockets = sockets > 0 ? sockets : 1;
1317 cores = cores > 0 ? cores : 1;
1318 threads = threads > 0 ? threads : 1;
1320 smp = cores * threads * sockets;
1324 threads = threads > 0 ? threads : 1;
1325 cores = smp / (sockets * threads);
1327 threads = smp / (cores * sockets);
1331 smp_cores = cores > 0 ? cores : 1;
1332 smp_threads = threads > 0 ? threads : 1;
1334 max_cpus = smp_cpus;
1337 /***********************************************************/
1340 static int usb_device_add(const char *devname)
1343 USBDevice *dev = NULL;
1345 if (!usb_enabled(false)) {
1349 /* drivers with .usbdevice_name entry in USBDeviceInfo */
1350 dev = usbdevice_create(devname);
1354 /* the other ones */
1355 #ifndef CONFIG_LINUX
1356 /* only the linux version is qdev-ified, usb-bsd still needs this */
1357 if (strstart(devname, "host:", &p)) {
1358 dev = usb_host_device_open(usb_bus_find(-1), p);
1361 if (!strcmp(devname, "bt") || strstart(devname, "bt:", &p)) {
1362 dev = usb_bt_init(usb_bus_find(-1),
1363 devname[2] ? hci_init(p)
1364 : bt_new_hci(qemu_find_bt_vlan(0)));
1375 static int usb_device_del(const char *devname)
1380 if (strstart(devname, "host:", &p))
1381 return usb_host_device_close(p);
1383 if (!usb_enabled(false)) {
1387 p = strchr(devname, '.');
1390 bus_num = strtoul(devname, NULL, 0);
1391 addr = strtoul(p + 1, NULL, 0);
1393 return usb_device_delete_addr(bus_num, addr);
1396 static int usb_parse(const char *cmdline)
1399 r = usb_device_add(cmdline);
1401 fprintf(stderr, "qemu: could not add USB device '%s'\n", cmdline);
1406 void do_usb_add(Monitor *mon, const QDict *qdict)
1408 const char *devname = qdict_get_str(qdict, "devname");
1409 if (usb_device_add(devname) < 0) {
1410 error_report("could not add USB device '%s'", devname);
1414 void do_usb_del(Monitor *mon, const QDict *qdict)
1416 const char *devname = qdict_get_str(qdict, "devname");
1417 if (usb_device_del(devname) < 0) {
1418 error_report("could not delete USB device '%s'", devname);
1422 /***********************************************************/
1423 /* PCMCIA/Cardbus */
1425 static struct pcmcia_socket_entry_s {
1426 PCMCIASocket *socket;
1427 struct pcmcia_socket_entry_s *next;
1428 } *pcmcia_sockets = 0;
1430 void pcmcia_socket_register(PCMCIASocket *socket)
1432 struct pcmcia_socket_entry_s *entry;
1434 entry = g_malloc(sizeof(struct pcmcia_socket_entry_s));
1435 entry->socket = socket;
1436 entry->next = pcmcia_sockets;
1437 pcmcia_sockets = entry;
1440 void pcmcia_socket_unregister(PCMCIASocket *socket)
1442 struct pcmcia_socket_entry_s *entry, **ptr;
1444 ptr = &pcmcia_sockets;
1445 for (entry = *ptr; entry; ptr = &entry->next, entry = *ptr)
1446 if (entry->socket == socket) {
1452 void pcmcia_info(Monitor *mon, const QDict *qdict)
1454 struct pcmcia_socket_entry_s *iter;
1456 if (!pcmcia_sockets)
1457 monitor_printf(mon, "No PCMCIA sockets\n");
1459 for (iter = pcmcia_sockets; iter; iter = iter->next)
1460 monitor_printf(mon, "%s: %s\n", iter->socket->slot_string,
1461 iter->socket->attached ? iter->socket->card_string :
1465 /***********************************************************/
1466 /* machine registration */
1468 static QEMUMachine *first_machine = NULL;
1469 QEMUMachine *current_machine = NULL;
1471 int qemu_register_machine(QEMUMachine *m)
1474 pm = &first_machine;
1482 static QEMUMachine *find_machine(const char *name)
1486 for(m = first_machine; m != NULL; m = m->next) {
1487 if (!strcmp(m->name, name))
1489 if (m->alias && !strcmp(m->alias, name))
1495 QEMUMachine *find_default_machine(void)
1499 for(m = first_machine; m != NULL; m = m->next) {
1500 if (m->is_default) {
1507 MachineInfoList *qmp_query_machines(Error **errp)
1509 MachineInfoList *mach_list = NULL;
1512 for (m = first_machine; m; m = m->next) {
1513 MachineInfoList *entry;
1516 info = g_malloc0(sizeof(*info));
1517 if (m->is_default) {
1518 info->has_is_default = true;
1519 info->is_default = true;
1523 info->has_alias = true;
1524 info->alias = g_strdup(m->alias);
1527 info->name = g_strdup(m->name);
1529 entry = g_malloc0(sizeof(*entry));
1530 entry->value = info;
1531 entry->next = mach_list;
1538 /***********************************************************/
1539 /* main execution loop */
1541 static void gui_update(void *opaque)
1543 uint64_t interval = GUI_REFRESH_INTERVAL;
1544 DisplayState *ds = opaque;
1545 DisplayChangeListener *dcl;
1549 QLIST_FOREACH(dcl, &ds->listeners, next) {
1550 if (dcl->gui_timer_interval &&
1551 dcl->gui_timer_interval < interval)
1552 interval = dcl->gui_timer_interval;
1554 qemu_mod_timer(ds->gui_timer, interval + qemu_get_clock_ms(rt_clock));
1557 void gui_setup_refresh(DisplayState *ds)
1559 DisplayChangeListener *dcl;
1560 bool need_timer = false;
1561 bool have_gfx = false;
1562 bool have_text = false;
1564 QLIST_FOREACH(dcl, &ds->listeners, next) {
1565 if (dcl->dpy_refresh != NULL) {
1568 if (dcl->dpy_gfx_update != NULL) {
1571 if (dcl->dpy_text_update != NULL) {
1576 if (need_timer && ds->gui_timer == NULL) {
1577 ds->gui_timer = qemu_new_timer_ms(rt_clock, gui_update, ds);
1578 qemu_mod_timer(ds->gui_timer, qemu_get_clock_ms(rt_clock));
1580 if (!need_timer && ds->gui_timer != NULL) {
1581 qemu_del_timer(ds->gui_timer);
1582 qemu_free_timer(ds->gui_timer);
1583 ds->gui_timer = NULL;
1586 ds->have_gfx = have_gfx;
1587 ds->have_text = have_text;
1590 struct vm_change_state_entry {
1591 VMChangeStateHandler *cb;
1593 QLIST_ENTRY (vm_change_state_entry) entries;
1596 static QLIST_HEAD(vm_change_state_head, vm_change_state_entry) vm_change_state_head;
1598 VMChangeStateEntry *qemu_add_vm_change_state_handler(VMChangeStateHandler *cb,
1601 VMChangeStateEntry *e;
1603 e = g_malloc0(sizeof (*e));
1607 QLIST_INSERT_HEAD(&vm_change_state_head, e, entries);
1611 void qemu_del_vm_change_state_handler(VMChangeStateEntry *e)
1613 QLIST_REMOVE (e, entries);
1617 void vm_state_notify(int running, RunState state)
1619 VMChangeStateEntry *e;
1621 trace_vm_state_notify(running, state);
1623 for (e = vm_change_state_head.lh_first; e; e = e->entries.le_next) {
1624 e->cb(e->opaque, running, state);
1630 if (!runstate_is_running()) {
1632 runstate_set(RUN_STATE_RUNNING);
1633 vm_state_notify(1, RUN_STATE_RUNNING);
1635 monitor_protocol_event(QEVENT_RESUME, NULL);
1639 /* reset/shutdown handler */
1641 typedef struct QEMUResetEntry {
1642 QTAILQ_ENTRY(QEMUResetEntry) entry;
1643 QEMUResetHandler *func;
1647 static QTAILQ_HEAD(reset_handlers, QEMUResetEntry) reset_handlers =
1648 QTAILQ_HEAD_INITIALIZER(reset_handlers);
1649 static int reset_requested;
1650 static int shutdown_requested, shutdown_signal = -1;
1651 static pid_t shutdown_pid;
1652 static int powerdown_requested;
1653 static int debug_requested;
1654 static int suspend_requested;
1655 static int wakeup_requested;
1656 static NotifierList powerdown_notifiers =
1657 NOTIFIER_LIST_INITIALIZER(powerdown_notifiers);
1658 static NotifierList suspend_notifiers =
1659 NOTIFIER_LIST_INITIALIZER(suspend_notifiers);
1660 static NotifierList wakeup_notifiers =
1661 NOTIFIER_LIST_INITIALIZER(wakeup_notifiers);
1662 static uint32_t wakeup_reason_mask = ~0;
1663 static RunState vmstop_requested = RUN_STATE_MAX;
1665 int qemu_shutdown_requested_get(void)
1667 return shutdown_requested;
1670 int qemu_reset_requested_get(void)
1672 return reset_requested;
1675 static int qemu_shutdown_requested(void)
1677 int r = shutdown_requested;
1678 shutdown_requested = 0;
1682 static void qemu_kill_report(void)
1684 if (!qtest_enabled() && shutdown_signal != -1) {
1685 fprintf(stderr, "qemu: terminating on signal %d", shutdown_signal);
1686 if (shutdown_pid == 0) {
1687 /* This happens for eg ^C at the terminal, so it's worth
1688 * avoiding printing an odd message in that case.
1690 fputc('\n', stderr);
1692 fprintf(stderr, " from pid " FMT_pid "\n", shutdown_pid);
1694 shutdown_signal = -1;
1698 static int qemu_reset_requested(void)
1700 int r = reset_requested;
1701 reset_requested = 0;
1705 static int qemu_suspend_requested(void)
1707 int r = suspend_requested;
1708 suspend_requested = 0;
1712 static int qemu_wakeup_requested(void)
1714 int r = wakeup_requested;
1715 wakeup_requested = 0;
1719 static int qemu_powerdown_requested(void)
1721 int r = powerdown_requested;
1722 powerdown_requested = 0;
1726 static int qemu_debug_requested(void)
1728 int r = debug_requested;
1729 debug_requested = 0;
1733 /* We use RUN_STATE_MAX but any invalid value will do */
1734 static bool qemu_vmstop_requested(RunState *r)
1736 if (vmstop_requested < RUN_STATE_MAX) {
1737 *r = vmstop_requested;
1738 vmstop_requested = RUN_STATE_MAX;
1745 void qemu_register_reset(QEMUResetHandler *func, void *opaque)
1747 QEMUResetEntry *re = g_malloc0(sizeof(QEMUResetEntry));
1750 re->opaque = opaque;
1751 QTAILQ_INSERT_TAIL(&reset_handlers, re, entry);
1754 void qemu_unregister_reset(QEMUResetHandler *func, void *opaque)
1758 QTAILQ_FOREACH(re, &reset_handlers, entry) {
1759 if (re->func == func && re->opaque == opaque) {
1760 QTAILQ_REMOVE(&reset_handlers, re, entry);
1767 void qemu_devices_reset(void)
1769 QEMUResetEntry *re, *nre;
1771 /* reset all devices */
1772 QTAILQ_FOREACH_SAFE(re, &reset_handlers, entry, nre) {
1773 re->func(re->opaque);
1777 void qemu_system_reset(bool report)
1779 if (current_machine && current_machine->reset) {
1780 current_machine->reset();
1782 qemu_devices_reset();
1785 monitor_protocol_event(QEVENT_RESET, NULL);
1787 cpu_synchronize_all_post_reset();
1790 void qemu_system_reset_request(void)
1793 shutdown_requested = 1;
1795 reset_requested = 1;
1798 qemu_notify_event();
1801 static void qemu_system_suspend(void)
1804 notifier_list_notify(&suspend_notifiers, NULL);
1805 runstate_set(RUN_STATE_SUSPENDED);
1806 monitor_protocol_event(QEVENT_SUSPEND, NULL);
1809 void qemu_system_suspend_request(void)
1811 if (runstate_check(RUN_STATE_SUSPENDED)) {
1814 suspend_requested = 1;
1816 qemu_notify_event();
1819 void qemu_register_suspend_notifier(Notifier *notifier)
1821 notifier_list_add(&suspend_notifiers, notifier);
1824 void qemu_system_wakeup_request(WakeupReason reason)
1826 if (!runstate_check(RUN_STATE_SUSPENDED)) {
1829 if (!(wakeup_reason_mask & (1 << reason))) {
1832 runstate_set(RUN_STATE_RUNNING);
1833 notifier_list_notify(&wakeup_notifiers, &reason);
1834 wakeup_requested = 1;
1835 qemu_notify_event();
1838 void qemu_system_wakeup_enable(WakeupReason reason, bool enabled)
1841 wakeup_reason_mask |= (1 << reason);
1843 wakeup_reason_mask &= ~(1 << reason);
1847 void qemu_register_wakeup_notifier(Notifier *notifier)
1849 notifier_list_add(&wakeup_notifiers, notifier);
1852 void qemu_system_killed(int signal, pid_t pid)
1854 shutdown_signal = signal;
1857 qemu_system_shutdown_request();
1860 void qemu_system_shutdown_request(void)
1862 shutdown_requested = 1;
1863 qemu_notify_event();
1866 static void qemu_system_powerdown(void)
1868 monitor_protocol_event(QEVENT_POWERDOWN, NULL);
1869 notifier_list_notify(&powerdown_notifiers, NULL);
1872 void qemu_system_powerdown_request(void)
1874 powerdown_requested = 1;
1875 qemu_notify_event();
1878 void qemu_register_powerdown_notifier(Notifier *notifier)
1880 notifier_list_add(&powerdown_notifiers, notifier);
1883 void qemu_system_debug_request(void)
1885 debug_requested = 1;
1886 qemu_notify_event();
1889 void qemu_system_vmstop_request(RunState state)
1891 vmstop_requested = state;
1892 qemu_notify_event();
1895 static bool main_loop_should_exit(void)
1898 if (qemu_debug_requested()) {
1899 vm_stop(RUN_STATE_DEBUG);
1901 if (qemu_suspend_requested()) {
1902 qemu_system_suspend();
1904 if (qemu_shutdown_requested()) {
1906 monitor_protocol_event(QEVENT_SHUTDOWN, NULL);
1908 vm_stop(RUN_STATE_SHUTDOWN);
1913 if (qemu_reset_requested()) {
1915 cpu_synchronize_all_states();
1916 qemu_system_reset(VMRESET_REPORT);
1918 if (runstate_check(RUN_STATE_INTERNAL_ERROR) ||
1919 runstate_check(RUN_STATE_SHUTDOWN)) {
1920 runstate_set(RUN_STATE_PAUSED);
1923 if (qemu_wakeup_requested()) {
1925 cpu_synchronize_all_states();
1926 qemu_system_reset(VMRESET_SILENT);
1928 monitor_protocol_event(QEVENT_WAKEUP, NULL);
1930 if (qemu_powerdown_requested()) {
1931 qemu_system_powerdown();
1933 if (qemu_vmstop_requested(&r)) {
1939 static void main_loop(void)
1943 #ifdef CONFIG_PROFILER
1947 nonblocking = !kvm_enabled() && last_io > 0;
1948 #ifdef CONFIG_PROFILER
1949 ti = profile_getclock();
1951 last_io = main_loop_wait(nonblocking);
1952 #ifdef CONFIG_PROFILER
1953 dev_time += profile_getclock() - ti;
1955 } while (!main_loop_should_exit());
1958 static void version(void)
1960 printf("QEMU emulator version " QEMU_VERSION QEMU_PKGVERSION ", Copyright (c) 2003-2008 Fabrice Bellard\n");
1963 static void help(int exitcode)
1966 printf("usage: %s [options] [disk_image]\n\n"
1967 "'disk_image' is a raw hard disk image for IDE hard disk 0\n\n",
1968 error_get_progname());
1970 #define QEMU_OPTIONS_GENERATE_HELP
1971 #include "qemu-options-wrapper.h"
1973 printf("\nDuring emulation, the following keys are useful:\n"
1974 "ctrl-alt-f toggle full screen\n"
1975 "ctrl-alt-n switch to virtual console 'n'\n"
1976 "ctrl-alt toggle mouse and keyboard grab\n"
1978 "When using -nographic, press 'ctrl-a h' to get some help.\n");
1983 #define HAS_ARG 0x0001
1985 typedef struct QEMUOption {
1992 static const QEMUOption qemu_options[] = {
1993 { "h", 0, QEMU_OPTION_h, QEMU_ARCH_ALL },
1994 #define QEMU_OPTIONS_GENERATE_OPTIONS
1995 #include "qemu-options-wrapper.h"
1999 static bool vga_available(void)
2001 return object_class_by_name("VGA") || object_class_by_name("isa-vga");
2004 static bool cirrus_vga_available(void)
2006 return object_class_by_name("cirrus-vga")
2007 || object_class_by_name("isa-cirrus-vga");
2010 static bool vmware_vga_available(void)
2012 return object_class_by_name("vmware-svga");
2015 static bool qxl_vga_available(void)
2017 return object_class_by_name("qxl-vga");
2020 static void select_vgahw (const char *p)
2024 vga_interface_type = VGA_NONE;
2025 if (strstart(p, "std", &opts)) {
2026 if (vga_available()) {
2027 vga_interface_type = VGA_STD;
2029 fprintf(stderr, "Error: standard VGA not available\n");
2032 } else if (strstart(p, "cirrus", &opts)) {
2033 if (cirrus_vga_available()) {
2034 vga_interface_type = VGA_CIRRUS;
2036 fprintf(stderr, "Error: Cirrus VGA not available\n");
2039 } else if (strstart(p, "vmware", &opts)) {
2040 if (vmware_vga_available()) {
2041 vga_interface_type = VGA_VMWARE;
2043 fprintf(stderr, "Error: VMWare SVGA not available\n");
2046 } else if (strstart(p, "xenfb", &opts)) {
2047 vga_interface_type = VGA_XENFB;
2048 } else if (strstart(p, "qxl", &opts)) {
2049 if (qxl_vga_available()) {
2050 vga_interface_type = VGA_QXL;
2052 fprintf(stderr, "Error: QXL VGA not available\n");
2055 } else if (!strstart(p, "none", &opts)) {
2057 fprintf(stderr, "Unknown vga type: %s\n", p);
2061 const char *nextopt;
2063 if (strstart(opts, ",retrace=", &nextopt)) {
2065 if (strstart(opts, "dumb", &nextopt))
2066 vga_retrace_method = VGA_RETRACE_DUMB;
2067 else if (strstart(opts, "precise", &nextopt))
2068 vga_retrace_method = VGA_RETRACE_PRECISE;
2069 else goto invalid_vga;
2070 } else goto invalid_vga;
2075 static DisplayType select_display(const char *p)
2078 DisplayType display = DT_DEFAULT;
2080 if (strstart(p, "sdl", &opts)) {
2084 const char *nextopt;
2086 if (strstart(opts, ",frame=", &nextopt)) {
2088 if (strstart(opts, "on", &nextopt)) {
2090 } else if (strstart(opts, "off", &nextopt)) {
2093 goto invalid_sdl_args;
2095 } else if (strstart(opts, ",alt_grab=", &nextopt)) {
2097 if (strstart(opts, "on", &nextopt)) {
2099 } else if (strstart(opts, "off", &nextopt)) {
2102 goto invalid_sdl_args;
2104 } else if (strstart(opts, ",ctrl_grab=", &nextopt)) {
2106 if (strstart(opts, "on", &nextopt)) {
2108 } else if (strstart(opts, "off", &nextopt)) {
2111 goto invalid_sdl_args;
2113 } else if (strstart(opts, ",window_close=", &nextopt)) {
2115 if (strstart(opts, "on", &nextopt)) {
2117 } else if (strstart(opts, "off", &nextopt)) {
2120 goto invalid_sdl_args;
2124 fprintf(stderr, "Invalid SDL option string: %s\n", p);
2130 fprintf(stderr, "SDL support is disabled\n");
2133 } else if (strstart(p, "vnc", &opts)) {
2138 const char *nextopt;
2140 if (strstart(opts, "=", &nextopt)) {
2141 vnc_display = nextopt;
2145 fprintf(stderr, "VNC requires a display argument vnc=<display>\n");
2149 fprintf(stderr, "VNC support is disabled\n");
2152 } else if (strstart(p, "curses", &opts)) {
2153 #ifdef CONFIG_CURSES
2154 display = DT_CURSES;
2156 fprintf(stderr, "Curses support is disabled\n");
2159 } else if (strstart(p, "none", &opts)) {
2162 fprintf(stderr, "Unknown display type: %s\n", p);
2169 static int balloon_parse(const char *arg)
2173 if (strcmp(arg, "none") == 0) {
2177 if (!strncmp(arg, "virtio", 6)) {
2178 if (arg[6] == ',') {
2179 /* have params -> parse them */
2180 opts = qemu_opts_parse(qemu_find_opts("device"), arg+7, 0);
2184 /* create empty opts */
2185 opts = qemu_opts_create_nofail(qemu_find_opts("device"));
2187 qemu_opt_set(opts, "driver", "virtio-balloon");
2194 char *qemu_find_file(int type, const char *name)
2200 /* Try the name as a straight path first */
2201 if (access(name, R_OK) == 0) {
2202 return g_strdup(name);
2205 case QEMU_FILE_TYPE_BIOS:
2208 case QEMU_FILE_TYPE_KEYMAP:
2209 subdir = "keymaps/";
2214 len = strlen(data_dir) + strlen(name) + strlen(subdir) + 2;
2215 buf = g_malloc0(len);
2216 snprintf(buf, len, "%s/%s%s", data_dir, subdir, name);
2217 if (access(buf, R_OK)) {
2224 static int device_help_func(QemuOpts *opts, void *opaque)
2226 return qdev_device_help(opts);
2229 static int device_init_func(QemuOpts *opts, void *opaque)
2233 dev = qdev_device_add(opts);
2239 static int chardev_init_func(QemuOpts *opts, void *opaque)
2241 Error *local_err = NULL;
2243 qemu_chr_new_from_opts(opts, NULL, &local_err);
2244 if (error_is_set(&local_err)) {
2245 fprintf(stderr, "%s\n", error_get_pretty(local_err));
2246 error_free(local_err);
2252 #ifdef CONFIG_VIRTFS
2253 static int fsdev_init_func(QemuOpts *opts, void *opaque)
2256 ret = qemu_fsdev_add(opts);
2262 static int mon_init_func(QemuOpts *opts, void *opaque)
2264 CharDriverState *chr;
2265 const char *chardev;
2269 mode = qemu_opt_get(opts, "mode");
2273 if (strcmp(mode, "readline") == 0) {
2274 flags = MONITOR_USE_READLINE;
2275 } else if (strcmp(mode, "control") == 0) {
2276 flags = MONITOR_USE_CONTROL;
2278 fprintf(stderr, "unknown monitor mode \"%s\"\n", mode);
2282 if (qemu_opt_get_bool(opts, "pretty", 0))
2283 flags |= MONITOR_USE_PRETTY;
2285 if (qemu_opt_get_bool(opts, "default", 0))
2286 flags |= MONITOR_IS_DEFAULT;
2288 chardev = qemu_opt_get(opts, "chardev");
2289 chr = qemu_chr_find(chardev);
2291 fprintf(stderr, "chardev \"%s\" not found\n", chardev);
2295 monitor_init(chr, flags);
2299 static void monitor_parse(const char *optarg, const char *mode)
2301 static int monitor_device_index = 0;
2307 if (strstart(optarg, "chardev:", &p)) {
2308 snprintf(label, sizeof(label), "%s", p);
2310 snprintf(label, sizeof(label), "compat_monitor%d",
2311 monitor_device_index);
2312 if (monitor_device_index == 0) {
2315 opts = qemu_chr_parse_compat(label, optarg);
2317 fprintf(stderr, "parse error: %s\n", optarg);
2322 opts = qemu_opts_create(qemu_find_opts("mon"), label, 1, NULL);
2324 fprintf(stderr, "duplicate chardev: %s\n", label);
2327 qemu_opt_set(opts, "mode", mode);
2328 qemu_opt_set(opts, "chardev", label);
2330 qemu_opt_set(opts, "default", "on");
2331 monitor_device_index++;
2334 struct device_config {
2336 DEV_USB, /* -usbdevice */
2338 DEV_SERIAL, /* -serial */
2339 DEV_PARALLEL, /* -parallel */
2340 DEV_VIRTCON, /* -virtioconsole */
2341 DEV_DEBUGCON, /* -debugcon */
2342 DEV_GDB, /* -gdb, -s */
2344 const char *cmdline;
2346 QTAILQ_ENTRY(device_config) next;
2349 static QTAILQ_HEAD(, device_config) device_configs =
2350 QTAILQ_HEAD_INITIALIZER(device_configs);
2352 static void add_device_config(int type, const char *cmdline)
2354 struct device_config *conf;
2356 conf = g_malloc0(sizeof(*conf));
2358 conf->cmdline = cmdline;
2359 loc_save(&conf->loc);
2360 QTAILQ_INSERT_TAIL(&device_configs, conf, next);
2363 static int foreach_device_config(int type, int (*func)(const char *cmdline))
2365 struct device_config *conf;
2368 QTAILQ_FOREACH(conf, &device_configs, next) {
2369 if (conf->type != type)
2371 loc_push_restore(&conf->loc);
2372 rc = func(conf->cmdline);
2373 loc_pop(&conf->loc);
2380 static int serial_parse(const char *devname)
2382 static int index = 0;
2385 if (strcmp(devname, "none") == 0)
2387 if (index == MAX_SERIAL_PORTS) {
2388 fprintf(stderr, "qemu: too many serial ports\n");
2391 snprintf(label, sizeof(label), "serial%d", index);
2392 serial_hds[index] = qemu_chr_new(label, devname, NULL);
2393 if (!serial_hds[index]) {
2394 fprintf(stderr, "qemu: could not connect serial device"
2395 " to character backend '%s'\n", devname);
2402 static int parallel_parse(const char *devname)
2404 static int index = 0;
2407 if (strcmp(devname, "none") == 0)
2409 if (index == MAX_PARALLEL_PORTS) {
2410 fprintf(stderr, "qemu: too many parallel ports\n");
2413 snprintf(label, sizeof(label), "parallel%d", index);
2414 parallel_hds[index] = qemu_chr_new(label, devname, NULL);
2415 if (!parallel_hds[index]) {
2416 fprintf(stderr, "qemu: could not connect parallel device"
2417 " to character backend '%s'\n", devname);
2424 static int virtcon_parse(const char *devname)
2426 QemuOptsList *device = qemu_find_opts("device");
2427 static int index = 0;
2429 QemuOpts *bus_opts, *dev_opts;
2431 if (strcmp(devname, "none") == 0)
2433 if (index == MAX_VIRTIO_CONSOLES) {
2434 fprintf(stderr, "qemu: too many virtio consoles\n");
2438 bus_opts = qemu_opts_create_nofail(device);
2439 if (arch_type == QEMU_ARCH_S390X) {
2440 qemu_opt_set(bus_opts, "driver", "virtio-serial-s390");
2442 qemu_opt_set(bus_opts, "driver", "virtio-serial-pci");
2445 dev_opts = qemu_opts_create_nofail(device);
2446 qemu_opt_set(dev_opts, "driver", "virtconsole");
2448 snprintf(label, sizeof(label), "virtcon%d", index);
2449 virtcon_hds[index] = qemu_chr_new(label, devname, NULL);
2450 if (!virtcon_hds[index]) {
2451 fprintf(stderr, "qemu: could not connect virtio console"
2452 " to character backend '%s'\n", devname);
2455 qemu_opt_set(dev_opts, "chardev", label);
2461 static int debugcon_parse(const char *devname)
2465 if (!qemu_chr_new("debugcon", devname, NULL)) {
2468 opts = qemu_opts_create(qemu_find_opts("device"), "debugcon", 1, NULL);
2470 fprintf(stderr, "qemu: already have a debugcon device\n");
2473 qemu_opt_set(opts, "driver", "isa-debugcon");
2474 qemu_opt_set(opts, "chardev", "debugcon");
2478 static QEMUMachine *machine_parse(const char *name)
2480 QEMUMachine *m, *machine = NULL;
2483 machine = find_machine(name);
2488 printf("Supported machines are:\n");
2489 for (m = first_machine; m != NULL; m = m->next) {
2491 printf("%-20s %s (alias of %s)\n", m->alias, m->desc, m->name);
2493 printf("%-20s %s%s\n", m->name, m->desc,
2494 m->is_default ? " (default)" : "");
2496 exit(!name || !is_help_option(name));
2499 static int tcg_init(void)
2501 tcg_exec_init(tcg_tb_size * 1024 * 1024);
2506 const char *opt_name;
2508 int (*available)(void);
2512 { "tcg", "tcg", tcg_available, tcg_init, &tcg_allowed },
2513 { "xen", "Xen", xen_available, xen_init, &xen_allowed },
2514 { "kvm", "KVM", kvm_available, kvm_init, &kvm_allowed },
2515 { "qtest", "QTest", qtest_available, qtest_init, &qtest_allowed },
2518 static int configure_accelerator(void)
2520 const char *p = NULL;
2523 bool accel_initialised = 0;
2524 bool init_failed = 0;
2526 QemuOptsList *list = qemu_find_opts("machine");
2527 if (!QTAILQ_EMPTY(&list->head)) {
2528 p = qemu_opt_get(QTAILQ_FIRST(&list->head), "accel");
2532 /* Use the default "accelerator", tcg */
2536 while (!accel_initialised && *p != '\0') {
2540 p = get_opt_name(buf, sizeof (buf), p, ':');
2541 for (i = 0; i < ARRAY_SIZE(accel_list); i++) {
2542 if (strcmp(accel_list[i].opt_name, buf) == 0) {
2543 *(accel_list[i].allowed) = 1;
2544 ret = accel_list[i].init();
2547 if (!accel_list[i].available()) {
2548 printf("%s not supported for this target\n",
2549 accel_list[i].name);
2551 fprintf(stderr, "failed to initialize %s: %s\n",
2555 *(accel_list[i].allowed) = 0;
2557 accel_initialised = 1;
2562 if (i == ARRAY_SIZE(accel_list)) {
2563 fprintf(stderr, "\"%s\" accelerator does not exist.\n", buf);
2567 if (!accel_initialised) {
2568 fprintf(stderr, "No accelerator found!\n");
2573 fprintf(stderr, "Back to %s accelerator.\n", accel_list[i].name);
2576 return !accel_initialised;
2579 void qemu_add_exit_notifier(Notifier *notify)
2581 notifier_list_add(&exit_notifiers, notify);
2584 void qemu_remove_exit_notifier(Notifier *notify)
2586 notifier_remove(notify);
2589 static void qemu_run_exit_notifiers(void)
2591 notifier_list_notify(&exit_notifiers, NULL);
2594 void qemu_add_machine_init_done_notifier(Notifier *notify)
2596 notifier_list_add(&machine_init_done_notifiers, notify);
2599 static void qemu_run_machine_init_done_notifiers(void)
2601 notifier_list_notify(&machine_init_done_notifiers, NULL);
2604 static const QEMUOption *lookup_opt(int argc, char **argv,
2605 const char **poptarg, int *poptind)
2607 const QEMUOption *popt;
2608 int optind = *poptind;
2609 char *r = argv[optind];
2612 loc_set_cmdline(argv, optind, 1);
2614 /* Treat --foo the same as -foo. */
2617 popt = qemu_options;
2620 error_report("invalid option");
2623 if (!strcmp(popt->name, r + 1))
2627 if (popt->flags & HAS_ARG) {
2628 if (optind >= argc) {
2629 error_report("requires an argument");
2632 optarg = argv[optind++];
2633 loc_set_cmdline(argv, optind - 2, 2);
2644 static gpointer malloc_and_trace(gsize n_bytes)
2646 void *ptr = malloc(n_bytes);
2647 trace_g_malloc(n_bytes, ptr);
2651 static gpointer realloc_and_trace(gpointer mem, gsize n_bytes)
2653 void *ptr = realloc(mem, n_bytes);
2654 trace_g_realloc(mem, n_bytes, ptr);
2658 static void free_and_trace(gpointer mem)
2664 static int object_set_property(const char *name, const char *value, void *opaque)
2666 Object *obj = OBJECT(opaque);
2667 StringInputVisitor *siv;
2668 Error *local_err = NULL;
2670 if (strcmp(name, "qom-type") == 0 || strcmp(name, "id") == 0) {
2674 siv = string_input_visitor_new(value);
2675 object_property_set(obj, string_input_get_visitor(siv), name, &local_err);
2676 string_input_visitor_cleanup(siv);
2679 qerror_report_err(local_err);
2680 error_free(local_err);
2687 static int object_create(QemuOpts *opts, void *opaque)
2689 const char *type = qemu_opt_get(opts, "qom-type");
2690 const char *id = qemu_opts_id(opts);
2693 g_assert(type != NULL);
2696 qerror_report(QERR_MISSING_PARAMETER, "id");
2700 obj = object_new(type);
2701 if (qemu_opt_foreach(opts, object_set_property, obj, 1) < 0) {
2705 object_property_add_child(container_get(object_get_root(), "/objects"),
2711 int main(int argc, char **argv, char **envp)
2714 int snapshot, linux_boot;
2715 const char *icount_option = NULL;
2716 const char *initrd_filename;
2717 const char *kernel_filename, *kernel_cmdline;
2718 char boot_devices[33] = "";
2720 int cyls, heads, secs, translation;
2721 QemuOpts *hda_opts = NULL, *opts, *machine_opts;
2722 QemuOptsList *olist;
2725 const char *loadvm = NULL;
2726 QEMUMachine *machine;
2727 const char *cpu_model;
2728 const char *vga_model = "none";
2729 const char *pid_file = NULL;
2730 const char *incoming = NULL;
2732 int show_vnc_port = 0;
2734 bool defconfig = true;
2735 bool userconfig = true;
2736 const char *log_mask = NULL;
2737 const char *log_file = NULL;
2738 GMemVTable mem_trace = {
2739 .malloc = malloc_and_trace,
2740 .realloc = realloc_and_trace,
2741 .free = free_and_trace,
2743 const char *trace_events = NULL;
2744 const char *trace_file = NULL;
2746 atexit(qemu_run_exit_notifiers);
2747 error_set_progname(argv[0]);
2749 g_mem_set_vtable(&mem_trace);
2750 if (!g_thread_supported()) {
2751 #if !GLIB_CHECK_VERSION(2, 31, 0)
2752 g_thread_init(NULL);
2754 fprintf(stderr, "glib threading failed to initialize.\n");
2759 module_call_init(MODULE_INIT_QOM);
2761 qemu_add_opts(&qemu_drive_opts);
2762 qemu_add_opts(&qemu_chardev_opts);
2763 qemu_add_opts(&qemu_device_opts);
2764 qemu_add_opts(&qemu_netdev_opts);
2765 qemu_add_opts(&qemu_net_opts);
2766 qemu_add_opts(&qemu_rtc_opts);
2767 qemu_add_opts(&qemu_global_opts);
2768 qemu_add_opts(&qemu_mon_opts);
2769 qemu_add_opts(&qemu_trace_opts);
2770 qemu_add_opts(&qemu_option_rom_opts);
2771 qemu_add_opts(&qemu_machine_opts);
2772 qemu_add_opts(&qemu_boot_opts);
2773 qemu_add_opts(&qemu_sandbox_opts);
2774 qemu_add_opts(&qemu_add_fd_opts);
2775 qemu_add_opts(&qemu_object_opts);
2780 rtc_clock = host_clock;
2782 qemu_cache_utils_init(envp);
2784 QLIST_INIT (&vm_change_state_head);
2785 os_setup_early_signal_handling();
2787 module_call_init(MODULE_INIT_MACHINE);
2788 machine = find_default_machine();
2792 cyls = heads = secs = 0;
2793 translation = BIOS_ATA_TRANSLATION_AUTO;
2795 for (i = 0; i < MAX_NODES; i++) {
2797 node_cpumask[i] = bitmap_new(MAX_CPUMASK_BITS);
2805 /* first pass of option parsing */
2807 while (optind < argc) {
2808 if (argv[optind][0] != '-') {
2813 const QEMUOption *popt;
2815 popt = lookup_opt(argc, argv, &optarg, &optind);
2816 switch (popt->index) {
2817 case QEMU_OPTION_nodefconfig:
2820 case QEMU_OPTION_nouserconfig:
2829 ret = qemu_read_default_config_files(userconfig);
2835 /* second pass of option parsing */
2840 if (argv[optind][0] != '-') {
2841 hda_opts = drive_add(IF_DEFAULT, 0, argv[optind++], HD_OPTS);
2843 const QEMUOption *popt;
2845 popt = lookup_opt(argc, argv, &optarg, &optind);
2846 if (!(popt->arch_mask & arch_type)) {
2847 printf("Option %s not supported for this target\n", popt->name);
2850 switch(popt->index) {
2852 machine = machine_parse(optarg);
2854 case QEMU_OPTION_no_kvm_irqchip: {
2855 olist = qemu_find_opts("machine");
2856 qemu_opts_parse(olist, "kernel_irqchip=off", 0);
2859 case QEMU_OPTION_cpu:
2860 /* hw initialization will check this */
2863 case QEMU_OPTION_hda:
2867 snprintf(buf, sizeof(buf), "%s", HD_OPTS);
2869 snprintf(buf, sizeof(buf),
2870 "%s,cyls=%d,heads=%d,secs=%d%s",
2871 HD_OPTS , cyls, heads, secs,
2872 translation == BIOS_ATA_TRANSLATION_LBA ?
2874 translation == BIOS_ATA_TRANSLATION_NONE ?
2875 ",trans=none" : "");
2876 drive_add(IF_DEFAULT, 0, optarg, buf);
2879 case QEMU_OPTION_hdb:
2880 case QEMU_OPTION_hdc:
2881 case QEMU_OPTION_hdd:
2882 drive_add(IF_DEFAULT, popt->index - QEMU_OPTION_hda, optarg,
2885 case QEMU_OPTION_drive:
2886 if (drive_def(optarg) == NULL) {
2890 case QEMU_OPTION_set:
2891 if (qemu_set_option(optarg) != 0)
2894 case QEMU_OPTION_global:
2895 if (qemu_global_option(optarg) != 0)
2898 case QEMU_OPTION_mtdblock:
2899 drive_add(IF_MTD, -1, optarg, MTD_OPTS);
2901 case QEMU_OPTION_sd:
2902 drive_add(IF_SD, 0, optarg, SD_OPTS);
2904 case QEMU_OPTION_pflash:
2905 drive_add(IF_PFLASH, -1, optarg, PFLASH_OPTS);
2907 case QEMU_OPTION_snapshot:
2910 case QEMU_OPTION_hdachs:
2914 cyls = strtol(p, (char **)&p, 0);
2915 if (cyls < 1 || cyls > 16383)
2920 heads = strtol(p, (char **)&p, 0);
2921 if (heads < 1 || heads > 16)
2926 secs = strtol(p, (char **)&p, 0);
2927 if (secs < 1 || secs > 63)
2931 if (!strcmp(p, "none"))
2932 translation = BIOS_ATA_TRANSLATION_NONE;
2933 else if (!strcmp(p, "lba"))
2934 translation = BIOS_ATA_TRANSLATION_LBA;
2935 else if (!strcmp(p, "auto"))
2936 translation = BIOS_ATA_TRANSLATION_AUTO;
2939 } else if (*p != '\0') {
2941 fprintf(stderr, "qemu: invalid physical CHS format\n");
2944 if (hda_opts != NULL) {
2946 snprintf(num, sizeof(num), "%d", cyls);
2947 qemu_opt_set(hda_opts, "cyls", num);
2948 snprintf(num, sizeof(num), "%d", heads);
2949 qemu_opt_set(hda_opts, "heads", num);
2950 snprintf(num, sizeof(num), "%d", secs);
2951 qemu_opt_set(hda_opts, "secs", num);
2952 if (translation == BIOS_ATA_TRANSLATION_LBA)
2953 qemu_opt_set(hda_opts, "trans", "lba");
2954 if (translation == BIOS_ATA_TRANSLATION_NONE)
2955 qemu_opt_set(hda_opts, "trans", "none");
2959 case QEMU_OPTION_numa:
2960 if (nb_numa_nodes >= MAX_NODES) {
2961 fprintf(stderr, "qemu: too many NUMA nodes\n");
2966 case QEMU_OPTION_display:
2967 display_type = select_display(optarg);
2969 case QEMU_OPTION_nographic:
2970 display_type = DT_NOGRAPHIC;
2972 case QEMU_OPTION_curses:
2973 #ifdef CONFIG_CURSES
2974 display_type = DT_CURSES;
2976 fprintf(stderr, "Curses support is disabled\n");
2980 case QEMU_OPTION_portrait:
2981 graphic_rotate = 90;
2983 case QEMU_OPTION_rotate:
2984 graphic_rotate = strtol(optarg, (char **) &optarg, 10);
2985 if (graphic_rotate != 0 && graphic_rotate != 90 &&
2986 graphic_rotate != 180 && graphic_rotate != 270) {
2988 "qemu: only 90, 180, 270 deg rotation is available\n");
2992 case QEMU_OPTION_kernel:
2993 qemu_opts_set(qemu_find_opts("machine"), 0, "kernel", optarg);
2995 case QEMU_OPTION_initrd:
2996 qemu_opts_set(qemu_find_opts("machine"), 0, "initrd", optarg);
2998 case QEMU_OPTION_append:
2999 qemu_opts_set(qemu_find_opts("machine"), 0, "append", optarg);
3001 case QEMU_OPTION_dtb:
3002 qemu_opts_set(qemu_find_opts("machine"), 0, "dtb", optarg);
3004 case QEMU_OPTION_cdrom:
3005 drive_add(IF_DEFAULT, 2, optarg, CDROM_OPTS);
3007 case QEMU_OPTION_boot:
3009 static const char * const params[] = {
3010 "order", "once", "menu",
3011 "splash", "splash-time",
3012 "reboot-timeout", NULL
3014 char buf[sizeof(boot_devices)];
3015 char *standard_boot_devices;
3018 if (!strchr(optarg, '=')) {
3020 pstrcpy(buf, sizeof(buf), optarg);
3021 } else if (check_params(buf, sizeof(buf), params, optarg) < 0) {
3023 "qemu: unknown boot parameter '%s' in '%s'\n",
3029 get_param_value(buf, sizeof(buf), "order", optarg)) {
3030 validate_bootdevices(buf);
3031 pstrcpy(boot_devices, sizeof(boot_devices), buf);
3034 if (get_param_value(buf, sizeof(buf),
3036 validate_bootdevices(buf);
3037 standard_boot_devices = g_strdup(boot_devices);
3038 pstrcpy(boot_devices, sizeof(boot_devices), buf);
3039 qemu_register_reset(restore_boot_devices,
3040 standard_boot_devices);
3042 if (get_param_value(buf, sizeof(buf),
3044 if (!strcmp(buf, "on")) {
3046 } else if (!strcmp(buf, "off")) {
3050 "qemu: invalid option value '%s'\n",
3055 qemu_opts_parse(qemu_find_opts("boot-opts"),
3060 case QEMU_OPTION_fda:
3061 case QEMU_OPTION_fdb:
3062 drive_add(IF_FLOPPY, popt->index - QEMU_OPTION_fda,
3065 case QEMU_OPTION_no_fd_bootchk:
3068 case QEMU_OPTION_netdev:
3069 if (net_client_parse(qemu_find_opts("netdev"), optarg) == -1) {
3073 case QEMU_OPTION_net:
3074 if (net_client_parse(qemu_find_opts("net"), optarg) == -1) {
3078 #ifdef CONFIG_LIBISCSI
3079 case QEMU_OPTION_iscsi:
3080 opts = qemu_opts_parse(qemu_find_opts("iscsi"), optarg, 0);
3087 case QEMU_OPTION_tftp:
3088 legacy_tftp_prefix = optarg;
3090 case QEMU_OPTION_bootp:
3091 legacy_bootp_filename = optarg;
3093 case QEMU_OPTION_redir:
3094 if (net_slirp_redir(optarg) < 0)
3098 case QEMU_OPTION_bt:
3099 add_device_config(DEV_BT, optarg);
3101 case QEMU_OPTION_audio_help:
3102 if (!(audio_available())) {
3103 printf("Option %s not supported for this target\n", popt->name);
3109 case QEMU_OPTION_soundhw:
3110 if (!(audio_available())) {
3111 printf("Option %s not supported for this target\n", popt->name);
3114 select_soundhw (optarg);
3119 case QEMU_OPTION_version:
3123 case QEMU_OPTION_m: {
3128 value = strtosz(optarg, &end);
3129 if (value < 0 || *end) {
3130 fprintf(stderr, "qemu: invalid ram size: %s\n", optarg);
3133 sz = QEMU_ALIGN_UP((uint64_t)value, 8192);
3135 if (ram_size != sz) {
3136 fprintf(stderr, "qemu: ram size too large\n");
3141 case QEMU_OPTION_mempath:
3145 case QEMU_OPTION_mem_prealloc:
3156 add_device_config(DEV_GDB, "tcp::" DEFAULT_GDBSTUB_PORT);
3158 case QEMU_OPTION_gdb:
3159 add_device_config(DEV_GDB, optarg);
3164 case QEMU_OPTION_bios:
3167 case QEMU_OPTION_singlestep:
3174 keyboard_layout = optarg;
3176 case QEMU_OPTION_localtime:
3179 case QEMU_OPTION_vga:
3188 w = strtol(p, (char **)&p, 10);
3191 fprintf(stderr, "qemu: invalid resolution or depth\n");
3197 h = strtol(p, (char **)&p, 10);
3202 depth = strtol(p, (char **)&p, 10);
3203 if (depth != 8 && depth != 15 && depth != 16 &&
3204 depth != 24 && depth != 32)
3206 } else if (*p == '\0') {
3207 depth = graphic_depth;
3214 graphic_depth = depth;
3217 case QEMU_OPTION_echr:
3220 term_escape_char = strtol(optarg, &r, 0);
3222 printf("Bad argument to echr\n");
3225 case QEMU_OPTION_monitor:
3226 monitor_parse(optarg, "readline");
3227 default_monitor = 0;
3229 case QEMU_OPTION_qmp:
3230 monitor_parse(optarg, "control");
3231 default_monitor = 0;
3233 case QEMU_OPTION_mon:
3234 opts = qemu_opts_parse(qemu_find_opts("mon"), optarg, 1);
3238 default_monitor = 0;
3240 case QEMU_OPTION_chardev:
3241 opts = qemu_opts_parse(qemu_find_opts("chardev"), optarg, 1);
3246 case QEMU_OPTION_fsdev:
3247 olist = qemu_find_opts("fsdev");
3249 fprintf(stderr, "fsdev is not supported by this qemu build.\n");
3252 opts = qemu_opts_parse(olist, optarg, 1);
3254 fprintf(stderr, "parse error: %s\n", optarg);
3258 case QEMU_OPTION_virtfs: {
3261 const char *writeout, *sock_fd, *socket;
3263 olist = qemu_find_opts("virtfs");
3265 fprintf(stderr, "virtfs is not supported by this qemu build.\n");
3268 opts = qemu_opts_parse(olist, optarg, 1);
3270 fprintf(stderr, "parse error: %s\n", optarg);
3274 if (qemu_opt_get(opts, "fsdriver") == NULL ||
3275 qemu_opt_get(opts, "mount_tag") == NULL) {
3276 fprintf(stderr, "Usage: -virtfs fsdriver,mount_tag=tag.\n");
3279 fsdev = qemu_opts_create(qemu_find_opts("fsdev"),
3280 qemu_opt_get(opts, "mount_tag"),
3283 fprintf(stderr, "duplicate fsdev id: %s\n",
3284 qemu_opt_get(opts, "mount_tag"));
3288 writeout = qemu_opt_get(opts, "writeout");
3290 #ifdef CONFIG_SYNC_FILE_RANGE
3291 qemu_opt_set(fsdev, "writeout", writeout);
3293 fprintf(stderr, "writeout=immediate not supported on "
3298 qemu_opt_set(fsdev, "fsdriver", qemu_opt_get(opts, "fsdriver"));
3299 qemu_opt_set(fsdev, "path", qemu_opt_get(opts, "path"));
3300 qemu_opt_set(fsdev, "security_model",
3301 qemu_opt_get(opts, "security_model"));
3302 socket = qemu_opt_get(opts, "socket");
3304 qemu_opt_set(fsdev, "socket", socket);
3306 sock_fd = qemu_opt_get(opts, "sock_fd");
3308 qemu_opt_set(fsdev, "sock_fd", sock_fd);
3311 qemu_opt_set_bool(fsdev, "readonly",
3312 qemu_opt_get_bool(opts, "readonly", 0));
3313 device = qemu_opts_create_nofail(qemu_find_opts("device"));
3314 qemu_opt_set(device, "driver", "virtio-9p-pci");
3315 qemu_opt_set(device, "fsdev",
3316 qemu_opt_get(opts, "mount_tag"));
3317 qemu_opt_set(device, "mount_tag",
3318 qemu_opt_get(opts, "mount_tag"));
3321 case QEMU_OPTION_virtfs_synth: {
3325 fsdev = qemu_opts_create(qemu_find_opts("fsdev"), "v_synth",
3328 fprintf(stderr, "duplicate option: %s\n", "virtfs_synth");
3331 qemu_opt_set(fsdev, "fsdriver", "synth");
3333 device = qemu_opts_create_nofail(qemu_find_opts("device"));
3334 qemu_opt_set(device, "driver", "virtio-9p-pci");
3335 qemu_opt_set(device, "fsdev", "v_synth");
3336 qemu_opt_set(device, "mount_tag", "v_synth");
3339 case QEMU_OPTION_serial:
3340 add_device_config(DEV_SERIAL, optarg);
3342 if (strncmp(optarg, "mon:", 4) == 0) {
3343 default_monitor = 0;
3346 case QEMU_OPTION_watchdog:
3349 "qemu: only one watchdog option may be given\n");
3354 case QEMU_OPTION_watchdog_action:
3355 if (select_watchdog_action(optarg) == -1) {
3356 fprintf(stderr, "Unknown -watchdog-action parameter\n");
3360 case QEMU_OPTION_virtiocon:
3361 add_device_config(DEV_VIRTCON, optarg);
3362 default_virtcon = 0;
3363 if (strncmp(optarg, "mon:", 4) == 0) {
3364 default_monitor = 0;
3367 case QEMU_OPTION_parallel:
3368 add_device_config(DEV_PARALLEL, optarg);
3369 default_parallel = 0;
3370 if (strncmp(optarg, "mon:", 4) == 0) {
3371 default_monitor = 0;
3374 case QEMU_OPTION_debugcon:
3375 add_device_config(DEV_DEBUGCON, optarg);
3377 case QEMU_OPTION_loadvm:
3380 case QEMU_OPTION_full_screen:
3384 case QEMU_OPTION_no_frame:
3387 case QEMU_OPTION_alt_grab:
3390 case QEMU_OPTION_ctrl_grab:
3393 case QEMU_OPTION_no_quit:
3396 case QEMU_OPTION_sdl:
3397 display_type = DT_SDL;
3400 case QEMU_OPTION_no_frame:
3401 case QEMU_OPTION_alt_grab:
3402 case QEMU_OPTION_ctrl_grab:
3403 case QEMU_OPTION_no_quit:
3404 case QEMU_OPTION_sdl:
3405 fprintf(stderr, "SDL support is disabled\n");
3408 case QEMU_OPTION_pidfile:
3411 case QEMU_OPTION_win2k_hack:
3412 win2k_install_hack = 1;
3414 case QEMU_OPTION_rtc_td_hack: {
3415 static GlobalProperty slew_lost_ticks[] = {
3417 .driver = "mc146818rtc",
3418 .property = "lost_tick_policy",
3421 { /* end of list */ }
3424 qdev_prop_register_global_list(slew_lost_ticks);
3427 case QEMU_OPTION_acpitable:
3428 do_acpitable_option(optarg);
3430 case QEMU_OPTION_smbios:
3431 do_smbios_option(optarg);
3433 case QEMU_OPTION_enable_kvm:
3434 olist = qemu_find_opts("machine");
3435 qemu_opts_parse(olist, "accel=kvm", 0);
3437 case QEMU_OPTION_machine:
3438 olist = qemu_find_opts("machine");
3439 opts = qemu_opts_parse(olist, optarg, 1);
3441 fprintf(stderr, "parse error: %s\n", optarg);
3444 optarg = qemu_opt_get(opts, "type");
3446 machine = machine_parse(optarg);
3449 case QEMU_OPTION_no_kvm:
3450 olist = qemu_find_opts("machine");
3451 qemu_opts_parse(olist, "accel=tcg", 0);
3453 case QEMU_OPTION_no_kvm_pit: {
3454 fprintf(stderr, "Warning: KVM PIT can no longer be disabled "
3458 case QEMU_OPTION_no_kvm_pit_reinjection: {
3459 static GlobalProperty kvm_pit_lost_tick_policy[] = {
3461 .driver = "kvm-pit",
3462 .property = "lost_tick_policy",
3465 { /* end of list */ }
3468 fprintf(stderr, "Warning: option deprecated, use "
3469 "lost_tick_policy property of kvm-pit instead.\n");
3470 qdev_prop_register_global_list(kvm_pit_lost_tick_policy);
3473 case QEMU_OPTION_usb:
3474 olist = qemu_find_opts("machine");
3475 qemu_opts_parse(olist, "usb=on", 0);
3477 case QEMU_OPTION_usbdevice:
3478 olist = qemu_find_opts("machine");
3479 qemu_opts_parse(olist, "usb=on", 0);
3480 add_device_config(DEV_USB, optarg);
3482 case QEMU_OPTION_device:
3483 if (!qemu_opts_parse(qemu_find_opts("device"), optarg, 1)) {
3487 case QEMU_OPTION_smp:
3490 fprintf(stderr, "Invalid number of CPUs\n");
3493 if (max_cpus < smp_cpus) {
3494 fprintf(stderr, "maxcpus must be equal to or greater than "
3498 if (max_cpus > 255) {
3499 fprintf(stderr, "Unsupported number of maxcpus\n");
3503 case QEMU_OPTION_vnc:
3506 vnc_display = optarg;
3508 fprintf(stderr, "VNC support is disabled\n");
3512 case QEMU_OPTION_no_acpi:
3515 case QEMU_OPTION_no_hpet:
3518 case QEMU_OPTION_balloon:
3519 if (balloon_parse(optarg) < 0) {
3520 fprintf(stderr, "Unknown -balloon argument %s\n", optarg);
3524 case QEMU_OPTION_no_reboot:
3527 case QEMU_OPTION_no_shutdown:
3530 case QEMU_OPTION_show_cursor:
3533 case QEMU_OPTION_uuid:
3534 if(qemu_uuid_parse(optarg, qemu_uuid) < 0) {
3535 fprintf(stderr, "Fail to parse UUID string."
3536 " Wrong format.\n");
3540 case QEMU_OPTION_option_rom:
3541 if (nb_option_roms >= MAX_OPTION_ROMS) {
3542 fprintf(stderr, "Too many option ROMs\n");
3545 opts = qemu_opts_parse(qemu_find_opts("option-rom"), optarg, 1);
3546 option_rom[nb_option_roms].name = qemu_opt_get(opts, "romfile");
3547 option_rom[nb_option_roms].bootindex =
3548 qemu_opt_get_number(opts, "bootindex", -1);
3549 if (!option_rom[nb_option_roms].name) {
3550 fprintf(stderr, "Option ROM file is not specified\n");
3555 case QEMU_OPTION_semihosting:
3556 semihosting_enabled = 1;
3558 case QEMU_OPTION_tdf:
3559 fprintf(stderr, "Warning: user space PIT time drift fix "
3560 "is no longer supported.\n");
3562 case QEMU_OPTION_name:
3563 qemu_name = g_strdup(optarg);
3565 char *p = strchr(qemu_name, ',');
3568 if (strncmp(p, "process=", 8)) {
3569 fprintf(stderr, "Unknown subargument %s to -name\n", p);
3573 os_set_proc_name(p);
3577 case QEMU_OPTION_prom_env:
3578 if (nb_prom_envs >= MAX_PROM_ENVS) {
3579 fprintf(stderr, "Too many prom variables\n");
3582 prom_envs[nb_prom_envs] = optarg;
3585 case QEMU_OPTION_old_param:
3588 case QEMU_OPTION_clock:
3589 configure_alarms(optarg);
3591 case QEMU_OPTION_startdate:
3592 configure_rtc_date_offset(optarg, 1);
3594 case QEMU_OPTION_rtc:
3595 opts = qemu_opts_parse(qemu_find_opts("rtc"), optarg, 0);
3599 configure_rtc(opts);
3601 case QEMU_OPTION_tb_size:
3602 tcg_tb_size = strtol(optarg, NULL, 0);
3603 if (tcg_tb_size < 0) {
3607 case QEMU_OPTION_icount:
3608 icount_option = optarg;
3610 case QEMU_OPTION_incoming:
3612 runstate_set(RUN_STATE_INMIGRATE);
3614 case QEMU_OPTION_nodefaults:
3616 default_parallel = 0;
3617 default_virtcon = 0;
3618 default_monitor = 0;
3625 case QEMU_OPTION_xen_domid:
3626 if (!(xen_available())) {
3627 printf("Option %s not supported for this target\n", popt->name);
3630 xen_domid = atoi(optarg);
3632 case QEMU_OPTION_xen_create:
3633 if (!(xen_available())) {
3634 printf("Option %s not supported for this target\n", popt->name);
3637 xen_mode = XEN_CREATE;
3639 case QEMU_OPTION_xen_attach:
3640 if (!(xen_available())) {
3641 printf("Option %s not supported for this target\n", popt->name);
3644 xen_mode = XEN_ATTACH;
3646 case QEMU_OPTION_trace:
3648 opts = qemu_opts_parse(qemu_find_opts("trace"), optarg, 0);
3652 trace_events = qemu_opt_get(opts, "events");
3653 trace_file = qemu_opt_get(opts, "file");
3656 case QEMU_OPTION_readconfig:
3658 int ret = qemu_read_config_file(optarg);
3660 fprintf(stderr, "read config %s: %s\n", optarg,
3666 case QEMU_OPTION_spice:
3667 olist = qemu_find_opts("spice");
3669 fprintf(stderr, "spice is not supported by this qemu build.\n");
3672 opts = qemu_opts_parse(olist, optarg, 0);
3674 fprintf(stderr, "parse error: %s\n", optarg);
3678 case QEMU_OPTION_writeconfig:
3681 if (strcmp(optarg, "-") == 0) {
3684 fp = fopen(optarg, "w");
3686 fprintf(stderr, "open %s: %s\n", optarg, strerror(errno));
3690 qemu_config_write(fp);
3694 case QEMU_OPTION_qtest:
3695 qtest_chrdev = optarg;
3697 case QEMU_OPTION_qtest_log:
3700 case QEMU_OPTION_sandbox:
3701 opts = qemu_opts_parse(qemu_find_opts("sandbox"), optarg, 1);
3706 case QEMU_OPTION_add_fd:
3708 opts = qemu_opts_parse(qemu_find_opts("add-fd"), optarg, 0);
3713 error_report("File descriptor passing is disabled on this "
3718 case QEMU_OPTION_object:
3719 opts = qemu_opts_parse(qemu_find_opts("object"), optarg, 1);
3722 os_parse_cmd_args(popt->index, optarg);
3728 if (qemu_init_main_loop()) {
3729 fprintf(stderr, "qemu_init_main_loop failed\n");
3733 if (qemu_opts_foreach(qemu_find_opts("sandbox"), parse_sandbox, NULL, 0)) {
3738 if (qemu_opts_foreach(qemu_find_opts("add-fd"), parse_add_fd, NULL, 1)) {
3742 if (qemu_opts_foreach(qemu_find_opts("add-fd"), cleanup_add_fd, NULL, 1)) {
3747 if (machine == NULL) {
3748 fprintf(stderr, "No machine found.\n");
3752 if (machine->hw_version) {
3753 qemu_set_version(machine->hw_version);
3756 if (qemu_opts_foreach(qemu_find_opts("object"),
3757 object_create, NULL, 0) != 0) {
3761 /* Init CPU def lists, based on config
3762 * - Must be called after all the qemu_read_config_file() calls
3763 * - Must be called before list_cpus()
3764 * - Must be called before machine->init()
3768 if (cpu_model && is_help_option(cpu_model)) {
3769 list_cpus(stdout, &fprintf, cpu_model);
3773 /* Open the logfile at this point, if necessary. We can't open the logfile
3774 * when encountering either of the logging options (-d or -D) because the
3775 * other one may be encountered later on the command line, changing the
3776 * location or level of logging.
3780 set_cpu_log_filename(log_file);
3782 set_cpu_log(log_mask);
3785 if (!trace_backend_init(trace_events, trace_file)) {
3789 /* If no data_dir is specified then try to find it relative to the
3792 data_dir = os_find_datadir(argv[0]);
3794 /* If all else fails use the install path specified when building. */
3796 data_dir = CONFIG_QEMU_DATADIR;
3800 * Default to max_cpus = smp_cpus, in case the user doesn't
3801 * specify a max_cpus value.
3804 max_cpus = smp_cpus;
3806 machine->max_cpus = machine->max_cpus ?: 1; /* Default to UP */
3807 if (smp_cpus > machine->max_cpus) {
3808 fprintf(stderr, "Number of SMP cpus requested (%d), exceeds max cpus "
3809 "supported by machine `%s' (%d)\n", smp_cpus, machine->name,
3815 * Get the default machine options from the machine if it is not already
3816 * specified either by the configuration file or by the command line.
3818 if (machine->default_machine_opts) {
3819 qemu_opts_set_defaults(qemu_find_opts("machine"),
3820 machine->default_machine_opts, 0);
3823 qemu_opts_foreach(qemu_find_opts("device"), default_driver_check, NULL, 0);
3824 qemu_opts_foreach(qemu_find_opts("global"), default_driver_check, NULL, 0);
3826 if (machine->no_serial) {
3829 if (machine->no_parallel) {
3830 default_parallel = 0;
3832 if (!machine->use_virtcon) {
3833 default_virtcon = 0;
3835 if (machine->no_floppy) {
3838 if (machine->no_cdrom) {
3841 if (machine->no_sdcard) {
3845 if (is_daemonized()) {
3846 /* According to documentation and historically, -nographic redirects
3847 * serial port, parallel port and monitor to stdio, which does not work
3848 * with -daemonize. We can redirect these to null instead, but since
3849 * -nographic is legacy, let's just error out.
3850 * We disallow -nographic only if all other ports are not redirected
3851 * explicitly, to not break existing legacy setups which uses
3852 * -nographic _and_ redirects all ports explicitly - this is valid
3853 * usage, -nographic is just a no-op in this case.
3855 if (display_type == DT_NOGRAPHIC
3856 && (default_parallel || default_serial
3857 || default_monitor || default_virtcon)) {
3858 fprintf(stderr, "-nographic can not be used with -daemonize\n");
3861 #ifdef CONFIG_CURSES
3862 if (display_type == DT_CURSES) {
3863 fprintf(stderr, "curses display can not be used with -daemonize\n");
3869 if (display_type == DT_NOGRAPHIC) {
3870 if (default_parallel)
3871 add_device_config(DEV_PARALLEL, "null");
3872 if (default_serial && default_monitor) {
3873 add_device_config(DEV_SERIAL, "mon:stdio");
3874 } else if (default_virtcon && default_monitor) {
3875 add_device_config(DEV_VIRTCON, "mon:stdio");
3878 add_device_config(DEV_SERIAL, "stdio");
3879 if (default_virtcon)
3880 add_device_config(DEV_VIRTCON, "stdio");
3881 if (default_monitor)
3882 monitor_parse("stdio", "readline");
3886 add_device_config(DEV_SERIAL, "vc:80Cx24C");
3887 if (default_parallel)
3888 add_device_config(DEV_PARALLEL, "vc:80Cx24C");
3889 if (default_monitor)
3890 monitor_parse("vc:80Cx24C", "readline");
3891 if (default_virtcon)
3892 add_device_config(DEV_VIRTCON, "vc:80Cx24C");
3897 if (qemu_opts_foreach(qemu_find_opts("chardev"), chardev_init_func, NULL, 1) != 0)
3899 #ifdef CONFIG_VIRTFS
3900 if (qemu_opts_foreach(qemu_find_opts("fsdev"), fsdev_init_func, NULL, 1) != 0) {
3907 if (pid_file && qemu_create_pidfile(pid_file) != 0) {
3912 /* init the memory */
3913 if (ram_size == 0) {
3914 ram_size = DEFAULT_RAM_SIZE * 1024 * 1024;
3917 if (qemu_opts_foreach(qemu_find_opts("device"), device_help_func, NULL, 0)
3922 configure_accelerator();
3924 machine_opts = qemu_opts_find(qemu_find_opts("machine"), 0);
3926 kernel_filename = qemu_opt_get(machine_opts, "kernel");
3927 initrd_filename = qemu_opt_get(machine_opts, "initrd");
3928 kernel_cmdline = qemu_opt_get(machine_opts, "append");
3930 kernel_filename = initrd_filename = kernel_cmdline = NULL;
3933 if (!kernel_cmdline) {
3934 kernel_cmdline = "";
3937 linux_boot = (kernel_filename != NULL);
3939 if (!linux_boot && *kernel_cmdline != '\0') {
3940 fprintf(stderr, "-append only allowed with -kernel option\n");
3944 if (!linux_boot && initrd_filename != NULL) {
3945 fprintf(stderr, "-initrd only allowed with -kernel option\n");
3949 if (!linux_boot && machine_opts && qemu_opt_get(machine_opts, "dtb")) {
3950 fprintf(stderr, "-dtb only allowed with -kernel option\n");
3954 os_set_line_buffering();
3956 qemu_init_cpu_loop();
3957 qemu_mutex_lock_iothread();
3960 /* spice needs the timers to be initialized by this point */
3964 if (icount_option && (kvm_enabled() || xen_enabled())) {
3965 fprintf(stderr, "-icount is not allowed with kvm or xen\n");
3968 configure_icount(icount_option);
3970 /* clean up network at qemu process termination */
3971 atexit(&net_cleanup);
3973 if (net_init_clients() < 0) {
3977 /* init the bluetooth world */
3978 if (foreach_device_config(DEV_BT, bt_parse))
3981 if (!xen_enabled()) {
3982 /* On 32-bit hosts, QEMU is limited by virtual address space */
3983 if (ram_size > (2047 << 20) && HOST_LONG_BITS == 32) {
3984 fprintf(stderr, "qemu: at most 2047 MB RAM can be simulated\n");
3989 cpu_exec_init_all();
3991 bdrv_init_with_whitelist();
3995 /* open the virtual block devices */
3997 qemu_opts_foreach(qemu_find_opts("drive"), drive_enable_snapshot, NULL, 0);
3998 if (qemu_opts_foreach(qemu_find_opts("drive"), drive_init_func,
3999 &machine->block_default_type, 1) != 0) {
4003 default_drive(default_cdrom, snapshot, machine->block_default_type, 2,
4005 default_drive(default_floppy, snapshot, IF_FLOPPY, 0, FD_OPTS);
4006 default_drive(default_sdcard, snapshot, IF_SD, 0, SD_OPTS);
4008 register_savevm_live(NULL, "ram", 0, 4, &savevm_ram_handlers, NULL);
4010 if (nb_numa_nodes > 0) {
4013 if (nb_numa_nodes > MAX_NODES) {
4014 nb_numa_nodes = MAX_NODES;
4017 /* If no memory size if given for any node, assume the default case
4018 * and distribute the available memory equally across all nodes
4020 for (i = 0; i < nb_numa_nodes; i++) {
4021 if (node_mem[i] != 0)
4024 if (i == nb_numa_nodes) {
4025 uint64_t usedmem = 0;
4027 /* On Linux, the each node's border has to be 8MB aligned,
4028 * the final node gets the rest.
4030 for (i = 0; i < nb_numa_nodes - 1; i++) {
4031 node_mem[i] = (ram_size / nb_numa_nodes) & ~((1 << 23UL) - 1);
4032 usedmem += node_mem[i];
4034 node_mem[i] = ram_size - usedmem;
4037 for (i = 0; i < nb_numa_nodes; i++) {
4038 if (!bitmap_empty(node_cpumask[i], MAX_CPUMASK_BITS)) {
4042 /* assigning the VCPUs round-robin is easier to implement, guest OSes
4043 * must cope with this anyway, because there are BIOSes out there in
4044 * real machines which also use this scheme.
4046 if (i == nb_numa_nodes) {
4047 for (i = 0; i < max_cpus; i++) {
4048 set_bit(i, node_cpumask[i % nb_numa_nodes]);
4053 if (qemu_opts_foreach(qemu_find_opts("mon"), mon_init_func, NULL, 1) != 0) {
4057 if (foreach_device_config(DEV_SERIAL, serial_parse) < 0)
4059 if (foreach_device_config(DEV_PARALLEL, parallel_parse) < 0)
4061 if (foreach_device_config(DEV_VIRTCON, virtcon_parse) < 0)
4063 if (foreach_device_config(DEV_DEBUGCON, debugcon_parse) < 0)
4066 /* If no default VGA is requested, the default is "none". */
4068 if (cirrus_vga_available()) {
4069 vga_model = "cirrus";
4070 } else if (vga_available()) {
4074 select_vgahw(vga_model);
4077 i = select_watchdog(watchdog);
4079 exit (i == 1 ? 1 : 0);
4082 if (machine->compat_props) {
4083 qdev_prop_register_global_list(machine->compat_props);
4087 qdev_machine_init();
4089 QEMUMachineInitArgs args = { .ram_size = ram_size,
4090 .boot_device = (boot_devices[0] == '\0') ?
4091 machine->boot_order :
4093 .kernel_filename = kernel_filename,
4094 .kernel_cmdline = kernel_cmdline,
4095 .initrd_filename = initrd_filename,
4096 .cpu_model = cpu_model };
4097 machine->init(&args);
4099 cpu_synchronize_all_post_init();
4103 current_machine = machine;
4105 /* init USB devices */
4106 if (usb_enabled(false)) {
4107 if (foreach_device_config(DEV_USB, usb_parse) < 0)
4111 /* init generic devices */
4112 if (qemu_opts_foreach(qemu_find_opts("device"), device_init_func, NULL, 1) != 0)
4115 net_check_clients();
4117 /* just use the first displaystate for the moment */
4118 ds = get_displaystate();
4122 if (display_type == DT_DEFAULT && !display_remote) {
4123 #if defined(CONFIG_SDL) || defined(CONFIG_COCOA)
4124 display_type = DT_SDL;
4125 #elif defined(CONFIG_VNC)
4126 vnc_display = "localhost:0,to=99";
4129 display_type = DT_NONE;
4134 /* init local displays */
4135 switch (display_type) {
4138 #if defined(CONFIG_CURSES)
4140 curses_display_init(ds, full_screen);
4143 #if defined(CONFIG_SDL)
4145 sdl_display_init(ds, full_screen, no_frame);
4147 #elif defined(CONFIG_COCOA)
4149 cocoa_display_init(ds, full_screen);
4156 /* must be after terminal init, SDL library changes signal handlers */
4157 os_setup_signal_handling();
4160 /* init remote displays */
4162 Error *local_err = NULL;
4163 vnc_display_init(ds);
4164 vnc_display_open(ds, vnc_display, &local_err);
4165 if (local_err != NULL) {
4166 fprintf(stderr, "Failed to start VNC server on `%s': %s\n",
4167 vnc_display, error_get_pretty(local_err));
4168 error_free(local_err);
4172 if (show_vnc_port) {
4173 printf("VNC server running on `%s'\n", vnc_display_local_addr(ds));
4178 if (using_spice && !qxl_enabled) {
4179 qemu_spice_display_init(ds);
4184 text_consoles_set_display(ds);
4186 if (foreach_device_config(DEV_GDB, gdbserver_start) < 0) {
4190 qdev_machine_creation_done();
4192 if (rom_load_all() != 0) {
4193 fprintf(stderr, "rom loading failed\n");
4197 /* TODO: once all bus devices are qdevified, this should be done
4198 * when bus is created by qdev.c */
4199 qemu_register_reset(qbus_reset_all_fn, sysbus_get_default());
4200 qemu_run_machine_init_done_notifiers();
4202 qemu_system_reset(VMRESET_SILENT);
4204 if (load_vmstate(loadvm) < 0) {
4210 Error *local_err = NULL;
4211 qemu_start_incoming_migration(incoming, &local_err);
4213 fprintf(stderr, "-incoming %s: %s\n", incoming, error_get_pretty(local_err));
4214 error_free(local_err);
4217 } else if (autostart) {