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)
100 int qemu_main(int argc, char **argv, char **envp);
101 int main(int argc, char **argv)
103 return qemu_main(argc, argv, NULL);
106 #define main qemu_main
109 #endif /* CONFIG_SDL */
113 #define main qemu_main
114 #endif /* CONFIG_COCOA */
120 int qemu_main(int argc, char **argv, char **envp);
121 #define main qemu_main
127 #include "hw/boards.h"
129 #include "hw/pcmcia.h"
130 #include "hw/i386/pc.h"
131 #include "hw/isa/isa.h"
133 #include "sysemu/watchdog.h"
134 #include "hw/i386/smbios.h"
135 #include "hw/xen/xen.h"
137 #include "hw/loader.h"
138 #include "monitor/qdev.h"
139 #include "sysemu/bt.h"
141 #include "net/slirp.h"
142 #include "monitor/monitor.h"
143 #include "ui/console.h"
144 #include "sysemu/sysemu.h"
145 #include "exec/gdbstub.h"
146 #include "qemu/timer.h"
147 #include "sysemu/char.h"
148 #include "qemu/cache-utils.h"
149 #include "sysemu/blockdev.h"
150 #include "hw/block/block.h"
151 #include "migration/block.h"
152 #include "sysemu/tpm.h"
153 #include "sysemu/dma.h"
154 #include "audio/audio.h"
155 #include "migration/migration.h"
156 #include "sysemu/kvm.h"
157 #include "sysemu/hax.h"
158 #include "qapi/qmp/qjson.h"
159 #include "qemu/option.h"
160 #include "qemu/config-file.h"
161 #include "qemu-options.h"
162 #include "qmp-commands.h"
163 #include "qemu/main-loop.h"
165 #include "fsdev/qemu-fsdev.h"
167 #include "sysemu/qtest.h"
169 #include "disas/disas.h"
171 #include "qemu/sockets.h"
173 #include "slirp/libslirp.h"
176 #include "trace/control.h"
177 #include "qemu/queue.h"
178 #include "sysemu/cpus.h"
179 #include "sysemu/arch_init.h"
180 #include "qemu/osdep.h"
182 #include "ui/qemu-spice.h"
183 #include "qapi/string-input-visitor.h"
186 #include "tizen/src/maru_common.h"
187 #include "tizen/src/emulator.h"
188 #include "tizen/src/maru_err_table.h"
189 #include "tizen/src/emul_state.h"
190 #include "tizen/src/maru_display.h"
191 #include "tizen/src/skin/maruskin_operation.h"
192 #include "tizen/src/ecs/ecs.h"
196 //#define DEBUG_SLIRP
198 #define DEFAULT_RAM_SIZE 128
200 #define MAX_VIRTIO_CONSOLES 1
201 #define MAX_SCLP_CONSOLES 1
204 int skin_disabled = 0;
206 extern int enable_gl;
207 extern int enable_yagl;
208 extern int enable_spice;
209 const char *yagl_backend = NULL;
211 char *vigs_backend = NULL;
214 static const char *data_dir[16];
215 static int data_dir_idx;
216 const char *bios_name = NULL;
217 enum vga_retrace_method vga_retrace_method = VGA_RETRACE_DUMB;
218 DisplayType display_type = DT_DEFAULT;
219 static int display_remote;
220 const char* keyboard_layout = NULL;
222 const char *mem_path = NULL;
224 int mem_prealloc = 0; /* force preallocation of physical target memory */
227 NICInfo nd_table[MAX_NICS];
229 static int rtc_utc = 1;
230 static int rtc_date_offset = -1; /* -1 means no change */
231 QEMUClock *rtc_clock;
232 int vga_interface_type = VGA_NONE;
233 static int full_screen = 0;
234 static int no_frame = 0;
236 CharDriverState *serial_hds[MAX_SERIAL_PORTS];
237 CharDriverState *parallel_hds[MAX_PARALLEL_PORTS];
238 CharDriverState *virtcon_hds[MAX_VIRTIO_CONSOLES];
239 CharDriverState *sclp_hds[MAX_SCLP_CONSOLES];
240 int win2k_install_hack = 0;
247 const char *vnc_display;
249 int acpi_enabled = 1;
252 static int no_reboot;
255 int graphic_rotate = 0;
256 const char *watchdog;
257 QEMUOptionRom option_rom[MAX_OPTION_ROMS];
259 int semihosting_enabled = 0;
261 const char *qemu_name;
264 unsigned int nb_prom_envs = 0;
265 const char *prom_envs[MAX_PROM_ENVS];
268 uint8_t *boot_splash_filedata;
269 size_t boot_splash_filedata_size;
270 uint8_t qemu_extra_params_fw[2];
272 typedef struct FWBootEntry FWBootEntry;
275 QTAILQ_ENTRY(FWBootEntry) link;
281 static QTAILQ_HEAD(, FWBootEntry) fw_boot_order =
282 QTAILQ_HEAD_INITIALIZER(fw_boot_order);
285 uint64_t node_mem[MAX_NODES];
286 unsigned long *node_cpumask[MAX_NODES];
288 uint8_t qemu_uuid[16];
290 static QEMUBootSetHandler *boot_set_handler;
291 static void *boot_set_opaque;
293 static NotifierList exit_notifiers =
294 NOTIFIER_LIST_INITIALIZER(exit_notifiers);
296 static NotifierList machine_init_done_notifiers =
297 NOTIFIER_LIST_INITIALIZER(machine_init_done_notifiers);
299 static bool tcg_allowed = true;
305 enum xen_mode xen_mode = XEN_EMULATE;
306 static int tcg_tb_size;
308 static int default_serial = 1;
309 static int default_parallel = 1;
310 static int default_virtcon = 1;
311 static int default_sclp = 1;
312 static int default_monitor = 1;
313 static int default_floppy = 1;
314 static int default_cdrom = 1;
315 static int default_sdcard = 1;
316 static int default_vga = 1;
322 { .driver = "isa-serial", .flag = &default_serial },
323 { .driver = "isa-parallel", .flag = &default_parallel },
324 { .driver = "isa-fdc", .flag = &default_floppy },
325 { .driver = "ide-cd", .flag = &default_cdrom },
326 { .driver = "ide-hd", .flag = &default_cdrom },
327 { .driver = "ide-drive", .flag = &default_cdrom },
328 { .driver = "scsi-cd", .flag = &default_cdrom },
329 { .driver = "virtio-serial-pci", .flag = &default_virtcon },
330 { .driver = "virtio-serial-s390", .flag = &default_virtcon },
331 { .driver = "virtio-serial", .flag = &default_virtcon },
332 { .driver = "VGA", .flag = &default_vga },
333 { .driver = "isa-vga", .flag = &default_vga },
334 { .driver = "cirrus-vga", .flag = &default_vga },
335 { .driver = "isa-cirrus-vga", .flag = &default_vga },
336 { .driver = "vmware-svga", .flag = &default_vga },
337 { .driver = "qxl-vga", .flag = &default_vga },
340 static QemuOptsList qemu_rtc_opts = {
342 .head = QTAILQ_HEAD_INITIALIZER(qemu_rtc_opts.head),
346 .type = QEMU_OPT_STRING,
349 .type = QEMU_OPT_STRING,
352 .type = QEMU_OPT_STRING,
354 { /* end of list */ }
358 static QemuOptsList qemu_sandbox_opts = {
360 .implied_opt_name = "enable",
361 .head = QTAILQ_HEAD_INITIALIZER(qemu_sandbox_opts.head),
365 .type = QEMU_OPT_BOOL,
367 { /* end of list */ }
371 static QemuOptsList qemu_trace_opts = {
373 .implied_opt_name = "trace",
374 .head = QTAILQ_HEAD_INITIALIZER(qemu_trace_opts.head),
378 .type = QEMU_OPT_STRING,
381 .type = QEMU_OPT_STRING,
383 { /* end of list */ }
387 static QemuOptsList qemu_option_rom_opts = {
388 .name = "option-rom",
389 .implied_opt_name = "romfile",
390 .head = QTAILQ_HEAD_INITIALIZER(qemu_option_rom_opts.head),
394 .type = QEMU_OPT_NUMBER,
397 .type = QEMU_OPT_STRING,
399 { /* end of list */ }
403 static QemuOptsList qemu_machine_opts = {
405 .implied_opt_name = "type",
407 .head = QTAILQ_HEAD_INITIALIZER(qemu_machine_opts.head),
411 .type = QEMU_OPT_STRING,
412 .help = "emulated machine"
415 .type = QEMU_OPT_STRING,
416 .help = "accelerator list",
418 .name = "kernel_irqchip",
419 .type = QEMU_OPT_BOOL,
420 .help = "use KVM in-kernel irqchip",
422 .name = "kvm_shadow_mem",
423 .type = QEMU_OPT_SIZE,
424 .help = "KVM shadow MMU size",
427 .type = QEMU_OPT_STRING,
428 .help = "Linux kernel image file",
431 .type = QEMU_OPT_STRING,
432 .help = "Linux initial ramdisk file",
435 .type = QEMU_OPT_STRING,
436 .help = "Linux kernel command line",
439 .type = QEMU_OPT_STRING,
440 .help = "Linux kernel device tree file",
443 .type = QEMU_OPT_STRING,
444 .help = "Dump current dtb to a file and quit",
446 .name = "phandle_start",
447 .type = QEMU_OPT_NUMBER,
448 .help = "The first phandle ID we may generate dynamically",
450 .name = "dt_compatible",
451 .type = QEMU_OPT_STRING,
452 .help = "Overrides the \"compatible\" property of the dt root node",
454 .name = "dump-guest-core",
455 .type = QEMU_OPT_BOOL,
456 .help = "Include guest memory in a core dump",
459 .type = QEMU_OPT_BOOL,
460 .help = "enable/disable memory merge support",
463 .type = QEMU_OPT_BOOL,
464 .help = "Set on/off to enable/disable usb",
466 { /* End of list */ }
470 static QemuOptsList qemu_boot_opts = {
472 .implied_opt_name = "order",
474 .head = QTAILQ_HEAD_INITIALIZER(qemu_boot_opts.head),
478 .type = QEMU_OPT_STRING,
481 .type = QEMU_OPT_STRING,
484 .type = QEMU_OPT_BOOL,
487 .type = QEMU_OPT_STRING,
489 .name = "splash-time",
490 .type = QEMU_OPT_STRING,
492 .name = "reboot-timeout",
493 .type = QEMU_OPT_STRING,
496 .type = QEMU_OPT_STRING,
502 static QemuOptsList qemu_add_fd_opts = {
504 .head = QTAILQ_HEAD_INITIALIZER(qemu_add_fd_opts.head),
508 .type = QEMU_OPT_NUMBER,
509 .help = "file descriptor of which a duplicate is added to fd set",
512 .type = QEMU_OPT_NUMBER,
513 .help = "ID of the fd set to add fd to",
516 .type = QEMU_OPT_STRING,
517 .help = "free-form string used to describe fd",
519 { /* end of list */ }
523 static QemuOptsList qemu_object_opts = {
525 .implied_opt_name = "qom-type",
526 .head = QTAILQ_HEAD_INITIALIZER(qemu_object_opts.head),
532 static QemuOptsList qemu_tpmdev_opts = {
534 .implied_opt_name = "type",
535 .head = QTAILQ_HEAD_INITIALIZER(qemu_tpmdev_opts.head),
537 /* options are defined in the TPM backends */
538 { /* end of list */ }
542 static QemuOptsList qemu_realtime_opts = {
544 .head = QTAILQ_HEAD_INITIALIZER(qemu_realtime_opts.head),
548 .type = QEMU_OPT_BOOL,
550 { /* end of list */ }
554 static QemuOptsList qemu_msg_opts = {
556 .head = QTAILQ_HEAD_INITIALIZER(qemu_msg_opts.head),
560 .type = QEMU_OPT_BOOL,
562 { /* end of list */ }
567 * Get machine options
569 * Returns: machine options (never null).
571 QemuOpts *qemu_get_machine_opts(void)
576 list = qemu_find_opts("machine");
578 opts = qemu_opts_find(list, NULL);
580 opts = qemu_opts_create_nofail(list);
585 const char *qemu_get_vm_name(void)
590 static void res_free(void)
592 if (boot_splash_filedata != NULL) {
593 g_free(boot_splash_filedata);
594 boot_splash_filedata = NULL;
598 static int default_driver_check(QemuOpts *opts, void *opaque)
600 const char *driver = qemu_opt_get(opts, "driver");
605 for (i = 0; i < ARRAY_SIZE(default_list); i++) {
606 if (strcmp(default_list[i].driver, driver) != 0)
608 *(default_list[i].flag) = 0;
613 /***********************************************************/
616 static RunState current_run_state = RUN_STATE_PRELAUNCH;
621 } RunStateTransition;
623 static const RunStateTransition runstate_transitions_def[] = {
625 { RUN_STATE_DEBUG, RUN_STATE_RUNNING },
627 { RUN_STATE_INMIGRATE, RUN_STATE_RUNNING },
628 { RUN_STATE_INMIGRATE, RUN_STATE_PAUSED },
630 { RUN_STATE_INTERNAL_ERROR, RUN_STATE_PAUSED },
631 { RUN_STATE_INTERNAL_ERROR, RUN_STATE_FINISH_MIGRATE },
633 { RUN_STATE_IO_ERROR, RUN_STATE_RUNNING },
634 { RUN_STATE_IO_ERROR, RUN_STATE_FINISH_MIGRATE },
636 { RUN_STATE_PAUSED, RUN_STATE_RUNNING },
637 { RUN_STATE_PAUSED, RUN_STATE_FINISH_MIGRATE },
639 { RUN_STATE_POSTMIGRATE, RUN_STATE_RUNNING },
640 { RUN_STATE_POSTMIGRATE, RUN_STATE_FINISH_MIGRATE },
642 { RUN_STATE_PRELAUNCH, RUN_STATE_RUNNING },
643 { RUN_STATE_PRELAUNCH, RUN_STATE_FINISH_MIGRATE },
644 { RUN_STATE_PRELAUNCH, RUN_STATE_INMIGRATE },
646 { RUN_STATE_FINISH_MIGRATE, RUN_STATE_RUNNING },
647 { RUN_STATE_FINISH_MIGRATE, RUN_STATE_POSTMIGRATE },
649 { RUN_STATE_RESTORE_VM, RUN_STATE_RUNNING },
651 { RUN_STATE_RUNNING, RUN_STATE_DEBUG },
652 { RUN_STATE_RUNNING, RUN_STATE_INTERNAL_ERROR },
653 { RUN_STATE_RUNNING, RUN_STATE_IO_ERROR },
654 { RUN_STATE_RUNNING, RUN_STATE_PAUSED },
655 { RUN_STATE_RUNNING, RUN_STATE_FINISH_MIGRATE },
656 { RUN_STATE_RUNNING, RUN_STATE_RESTORE_VM },
657 { RUN_STATE_RUNNING, RUN_STATE_SAVE_VM },
658 { RUN_STATE_RUNNING, RUN_STATE_SHUTDOWN },
659 { RUN_STATE_RUNNING, RUN_STATE_WATCHDOG },
660 { RUN_STATE_RUNNING, RUN_STATE_GUEST_PANICKED },
662 { RUN_STATE_SAVE_VM, RUN_STATE_RUNNING },
664 { RUN_STATE_SHUTDOWN, RUN_STATE_PAUSED },
665 { RUN_STATE_SHUTDOWN, RUN_STATE_FINISH_MIGRATE },
667 { RUN_STATE_DEBUG, RUN_STATE_SUSPENDED },
668 { RUN_STATE_RUNNING, RUN_STATE_SUSPENDED },
669 { RUN_STATE_SUSPENDED, RUN_STATE_RUNNING },
670 { RUN_STATE_SUSPENDED, RUN_STATE_FINISH_MIGRATE },
672 { RUN_STATE_WATCHDOG, RUN_STATE_RUNNING },
673 { RUN_STATE_WATCHDOG, RUN_STATE_FINISH_MIGRATE },
675 { RUN_STATE_GUEST_PANICKED, RUN_STATE_PAUSED },
676 { RUN_STATE_GUEST_PANICKED, RUN_STATE_FINISH_MIGRATE },
677 { RUN_STATE_GUEST_PANICKED, RUN_STATE_DEBUG },
679 { RUN_STATE_MAX, RUN_STATE_MAX },
682 static bool runstate_valid_transitions[RUN_STATE_MAX][RUN_STATE_MAX];
684 bool runstate_check(RunState state)
686 return current_run_state == state;
689 static void runstate_init(void)
691 const RunStateTransition *p;
693 memset(&runstate_valid_transitions, 0, sizeof(runstate_valid_transitions));
695 for (p = &runstate_transitions_def[0]; p->from != RUN_STATE_MAX; p++) {
696 runstate_valid_transitions[p->from][p->to] = true;
700 /* This function will abort() on invalid state transitions */
701 void runstate_set(RunState new_state)
703 assert(new_state < RUN_STATE_MAX);
705 if (!runstate_valid_transitions[current_run_state][new_state]) {
706 fprintf(stderr, "ERROR: invalid runstate transition: '%s' -> '%s'\n",
707 RunState_lookup[current_run_state],
708 RunState_lookup[new_state]);
711 trace_runstate_set(new_state);
712 current_run_state = new_state;
715 int runstate_is_running(void)
717 return runstate_check(RUN_STATE_RUNNING);
720 bool runstate_needs_reset(void)
722 return runstate_check(RUN_STATE_INTERNAL_ERROR) ||
723 runstate_check(RUN_STATE_SHUTDOWN) ||
724 runstate_check(RUN_STATE_GUEST_PANICKED);
727 StatusInfo *qmp_query_status(Error **errp)
729 StatusInfo *info = g_malloc0(sizeof(*info));
731 info->running = runstate_is_running();
732 info->singlestep = singlestep;
733 info->status = current_run_state;
738 /***********************************************************/
739 /* real time host monotonic timer */
741 /***********************************************************/
742 /* host time/date access */
743 void qemu_get_timedate(struct tm *tm, int offset)
749 if (rtc_date_offset == -1) {
753 localtime_r(&ti, tm);
755 ti -= rtc_date_offset;
760 int qemu_timedate_diff(struct tm *tm)
764 if (rtc_date_offset == -1)
766 seconds = mktimegm(tm);
769 tmp.tm_isdst = -1; /* use timezone to figure it out */
770 seconds = mktime(&tmp);
773 seconds = mktimegm(tm) + rtc_date_offset;
775 return seconds - time(NULL);
778 void rtc_change_mon_event(struct tm *tm)
782 data = qobject_from_jsonf("{ 'offset': %d }", qemu_timedate_diff(tm));
783 monitor_protocol_event(QEVENT_RTC_CHANGE, data);
784 qobject_decref(data);
787 static void configure_rtc_date_offset(const char *startdate, int legacy)
789 time_t rtc_start_date;
792 if (!strcmp(startdate, "now") && legacy) {
793 rtc_date_offset = -1;
795 if (sscanf(startdate, "%d-%d-%dT%d:%d:%d",
803 } else if (sscanf(startdate, "%d-%d-%d",
815 rtc_start_date = mktimegm(&tm);
816 if (rtc_start_date == -1) {
818 fprintf(stderr, "Invalid date format. Valid formats are:\n"
819 "'2006-06-17T16:01:21' or '2006-06-17'\n");
822 rtc_date_offset = time(NULL) - rtc_start_date;
826 static void configure_rtc(QemuOpts *opts)
830 value = qemu_opt_get(opts, "base");
832 if (!strcmp(value, "utc")) {
834 } else if (!strcmp(value, "localtime")) {
837 configure_rtc_date_offset(value, 0);
840 value = qemu_opt_get(opts, "clock");
842 if (!strcmp(value, "host")) {
843 rtc_clock = host_clock;
844 } else if (!strcmp(value, "rt")) {
845 rtc_clock = rt_clock;
846 } else if (!strcmp(value, "vm")) {
847 rtc_clock = vm_clock;
849 fprintf(stderr, "qemu: invalid option value '%s'\n", value);
853 value = qemu_opt_get(opts, "driftfix");
855 if (!strcmp(value, "slew")) {
856 static GlobalProperty slew_lost_ticks[] = {
858 .driver = "mc146818rtc",
859 .property = "lost_tick_policy",
862 { /* end of list */ }
865 qdev_prop_register_global_list(slew_lost_ticks);
866 } else if (!strcmp(value, "none")) {
867 /* discard is default */
869 fprintf(stderr, "qemu: invalid option value '%s'\n", value);
875 /***********************************************************/
876 /* Bluetooth support */
879 static struct HCIInfo *hci_table[MAX_NICS];
881 static struct bt_vlan_s {
882 struct bt_scatternet_s net;
884 struct bt_vlan_s *next;
887 /* find or alloc a new bluetooth "VLAN" */
888 static struct bt_scatternet_s *qemu_find_bt_vlan(int id)
890 struct bt_vlan_s **pvlan, *vlan;
891 for (vlan = first_bt_vlan; vlan != NULL; vlan = vlan->next) {
895 vlan = g_malloc0(sizeof(struct bt_vlan_s));
897 pvlan = &first_bt_vlan;
898 while (*pvlan != NULL)
899 pvlan = &(*pvlan)->next;
904 static void null_hci_send(struct HCIInfo *hci, const uint8_t *data, int len)
908 static int null_hci_addr_set(struct HCIInfo *hci, const uint8_t *bd_addr)
913 static struct HCIInfo null_hci = {
914 .cmd_send = null_hci_send,
915 .sco_send = null_hci_send,
916 .acl_send = null_hci_send,
917 .bdaddr_set = null_hci_addr_set,
920 struct HCIInfo *qemu_next_hci(void)
922 if (cur_hci == nb_hcis)
925 return hci_table[cur_hci++];
928 static struct HCIInfo *hci_init(const char *str)
931 struct bt_scatternet_s *vlan = 0;
933 if (!strcmp(str, "null"))
936 else if (!strncmp(str, "host", 4) && (str[4] == '\0' || str[4] == ':'))
938 return bt_host_hci(str[4] ? str + 5 : "hci0");
939 else if (!strncmp(str, "hci", 3)) {
942 if (!strncmp(str + 3, ",vlan=", 6)) {
943 vlan = qemu_find_bt_vlan(strtol(str + 9, &endp, 0));
948 vlan = qemu_find_bt_vlan(0);
950 return bt_new_hci(vlan);
953 fprintf(stderr, "qemu: Unknown bluetooth HCI `%s'.\n", str);
958 static int bt_hci_parse(const char *str)
963 if (nb_hcis >= MAX_NICS) {
964 fprintf(stderr, "qemu: Too many bluetooth HCIs (max %i).\n", MAX_NICS);
977 bdaddr.b[5] = 0x56 + nb_hcis;
978 hci->bdaddr_set(hci, bdaddr.b);
980 hci_table[nb_hcis++] = hci;
985 static void bt_vhci_add(int vlan_id)
987 struct bt_scatternet_s *vlan = qemu_find_bt_vlan(vlan_id);
990 fprintf(stderr, "qemu: warning: adding a VHCI to "
991 "an empty scatternet %i\n", vlan_id);
993 bt_vhci_init(bt_new_hci(vlan));
996 static struct bt_device_s *bt_device_add(const char *opt)
998 struct bt_scatternet_s *vlan;
1000 char *endp = strstr(opt, ",vlan=");
1001 int len = (endp ? endp - opt : strlen(opt)) + 1;
1004 pstrcpy(devname, MIN(sizeof(devname), len), opt);
1007 vlan_id = strtol(endp + 6, &endp, 0);
1009 fprintf(stderr, "qemu: unrecognised bluetooth vlan Id\n");
1014 vlan = qemu_find_bt_vlan(vlan_id);
1017 fprintf(stderr, "qemu: warning: adding a slave device to "
1018 "an empty scatternet %i\n", vlan_id);
1020 if (!strcmp(devname, "keyboard"))
1021 return bt_keyboard_init(vlan);
1023 fprintf(stderr, "qemu: unsupported bluetooth device `%s'\n", devname);
1027 static int bt_parse(const char *opt)
1029 const char *endp, *p;
1032 if (strstart(opt, "hci", &endp)) {
1033 if (!*endp || *endp == ',') {
1035 if (!strstart(endp, ",vlan=", 0))
1038 return bt_hci_parse(opt);
1040 } else if (strstart(opt, "vhci", &endp)) {
1041 if (!*endp || *endp == ',') {
1043 if (strstart(endp, ",vlan=", &p)) {
1044 vlan = strtol(p, (char **) &endp, 0);
1046 fprintf(stderr, "qemu: bad scatternet '%s'\n", p);
1050 fprintf(stderr, "qemu: bad parameter '%s'\n", endp + 1);
1059 } else if (strstart(opt, "device:", &endp))
1060 return !bt_device_add(endp);
1062 fprintf(stderr, "qemu: bad bluetooth parameter '%s'\n", opt);
1066 static int parse_sandbox(QemuOpts *opts, void *opaque)
1068 /* FIXME: change this to true for 1.3 */
1069 if (qemu_opt_get_bool(opts, "enable", false)) {
1070 #ifdef CONFIG_SECCOMP
1071 if (seccomp_start() < 0) {
1072 qerror_report(ERROR_CLASS_GENERIC_ERROR,
1073 "failed to install seccomp syscall filter in the kernel");
1077 qerror_report(ERROR_CLASS_GENERIC_ERROR,
1078 "sandboxing request but seccomp is not compiled into this build");
1086 bool usb_enabled(bool default_usb)
1088 return qemu_opt_get_bool(qemu_get_machine_opts(), "usb", default_usb);
1092 static int parse_add_fd(QemuOpts *opts, void *opaque)
1094 int fd, dupfd, flags;
1096 const char *fd_opaque = NULL;
1098 fd = qemu_opt_get_number(opts, "fd", -1);
1099 fdset_id = qemu_opt_get_number(opts, "set", -1);
1100 fd_opaque = qemu_opt_get(opts, "opaque");
1103 qerror_report(ERROR_CLASS_GENERIC_ERROR,
1104 "fd option is required and must be non-negative");
1108 if (fd <= STDERR_FILENO) {
1109 qerror_report(ERROR_CLASS_GENERIC_ERROR,
1110 "fd cannot be a standard I/O stream");
1115 * All fds inherited across exec() necessarily have FD_CLOEXEC
1116 * clear, while qemu sets FD_CLOEXEC on all other fds used internally.
1118 flags = fcntl(fd, F_GETFD);
1119 if (flags == -1 || (flags & FD_CLOEXEC)) {
1120 qerror_report(ERROR_CLASS_GENERIC_ERROR,
1121 "fd is not valid or already in use");
1126 qerror_report(ERROR_CLASS_GENERIC_ERROR,
1127 "set option is required and must be non-negative");
1131 #ifdef F_DUPFD_CLOEXEC
1132 dupfd = fcntl(fd, F_DUPFD_CLOEXEC, 0);
1136 qemu_set_cloexec(dupfd);
1140 qerror_report(ERROR_CLASS_GENERIC_ERROR,
1141 "Error duplicating fd: %s", strerror(errno));
1145 /* add the duplicate fd, and optionally the opaque string, to the fd set */
1146 monitor_fdset_add_fd(dupfd, true, fdset_id, fd_opaque ? true : false,
1152 static int cleanup_add_fd(QemuOpts *opts, void *opaque)
1156 fd = qemu_opt_get_number(opts, "fd", -1);
1163 /***********************************************************/
1164 /* QEMU Block devices */
1166 #define HD_OPTS "media=disk"
1167 #define CDROM_OPTS "media=cdrom"
1169 #define PFLASH_OPTS ""
1173 static int drive_init_func(QemuOpts *opts, void *opaque)
1175 BlockInterfaceType *block_default_type = opaque;
1177 return drive_init(opts, *block_default_type) == NULL;
1180 static int drive_enable_snapshot(QemuOpts *opts, void *opaque)
1182 if (NULL == qemu_opt_get(opts, "snapshot")) {
1183 qemu_opt_set(opts, "snapshot", "on");
1188 static void default_drive(int enable, int snapshot, BlockInterfaceType type,
1189 int index, const char *optstr)
1193 if (!enable || drive_get_by_index(type, index)) {
1197 opts = drive_add(type, index, NULL, optstr);
1199 drive_enable_snapshot(opts, NULL);
1201 if (!drive_init(opts, type)) {
1206 void qemu_register_boot_set(QEMUBootSetHandler *func, void *opaque)
1208 boot_set_handler = func;
1209 boot_set_opaque = opaque;
1212 int qemu_boot_set(const char *boot_order)
1214 if (!boot_set_handler) {
1217 return boot_set_handler(boot_set_opaque, boot_order);
1220 static void validate_bootdevices(const char *devices)
1222 /* We just do some generic consistency checks */
1226 for (p = devices; *p != '\0'; p++) {
1227 /* Allowed boot devices are:
1228 * a-b: floppy disk drives
1229 * c-f: IDE disk drives
1230 * g-m: machine implementation dependent drives
1231 * n-p: network devices
1232 * It's up to each machine implementation to check if the given boot
1233 * devices match the actual hardware implementation and firmware
1236 if (*p < 'a' || *p > 'p') {
1237 fprintf(stderr, "Invalid boot device '%c'\n", *p);
1240 if (bitmap & (1 << (*p - 'a'))) {
1241 fprintf(stderr, "Boot device '%c' was given twice\n", *p);
1244 bitmap |= 1 << (*p - 'a');
1248 static void restore_boot_order(void *opaque)
1250 char *normal_boot_order = opaque;
1251 static int first = 1;
1253 /* Restore boot order and remove ourselves after the first boot */
1259 qemu_boot_set(normal_boot_order);
1261 qemu_unregister_reset(restore_boot_order, normal_boot_order);
1262 g_free(normal_boot_order);
1265 void add_boot_device_path(int32_t bootindex, DeviceState *dev,
1268 FWBootEntry *node, *i;
1270 if (bootindex < 0) {
1274 assert(dev != NULL || suffix != NULL);
1276 node = g_malloc0(sizeof(FWBootEntry));
1277 node->bootindex = bootindex;
1278 node->suffix = g_strdup(suffix);
1281 QTAILQ_FOREACH(i, &fw_boot_order, link) {
1282 if (i->bootindex == bootindex) {
1283 fprintf(stderr, "Two devices with same boot index %d\n", bootindex);
1285 } else if (i->bootindex < bootindex) {
1288 QTAILQ_INSERT_BEFORE(i, node, link);
1291 QTAILQ_INSERT_TAIL(&fw_boot_order, node, link);
1294 DeviceState *get_boot_device(uint32_t position)
1296 uint32_t counter = 0;
1297 FWBootEntry *i = NULL;
1298 DeviceState *res = NULL;
1300 if (!QTAILQ_EMPTY(&fw_boot_order)) {
1301 QTAILQ_FOREACH(i, &fw_boot_order, link) {
1302 if (counter == position) {
1313 * This function returns null terminated string that consist of new line
1314 * separated device paths.
1316 * memory pointed by "size" is assigned total length of the array in bytes
1319 char *get_boot_devices_list(size_t *size)
1325 QTAILQ_FOREACH(i, &fw_boot_order, link) {
1326 char *devpath = NULL, *bootpath;
1330 devpath = qdev_get_fw_dev_path(i->dev);
1334 if (i->suffix && devpath) {
1335 size_t bootpathlen = strlen(devpath) + strlen(i->suffix) + 1;
1337 bootpath = g_malloc(bootpathlen);
1338 snprintf(bootpath, bootpathlen, "%s%s", devpath, i->suffix);
1340 } else if (devpath) {
1344 bootpath = g_strdup(i->suffix);
1348 list[total-1] = '\n';
1350 len = strlen(bootpath) + 1;
1351 list = g_realloc(list, total + len);
1352 memcpy(&list[total], bootpath, len);
1359 if (boot_strict && *size > 0) {
1360 list[total-1] = '\n';
1361 list = g_realloc(list, total + 5);
1362 memcpy(&list[total], "HALT", 5);
1368 static void numa_node_parse_cpus(int nodenr, const char *cpus)
1371 unsigned long long value, endvalue;
1373 /* Empty CPU range strings will be considered valid, they will simply
1374 * not set any bit in the CPU bitmap.
1380 if (parse_uint(cpus, &value, &endptr, 10) < 0) {
1383 if (*endptr == '-') {
1384 if (parse_uint_full(endptr + 1, &endvalue, 10) < 0) {
1387 } else if (*endptr == '\0') {
1393 if (endvalue >= MAX_CPUMASK_BITS) {
1394 endvalue = MAX_CPUMASK_BITS - 1;
1396 "qemu: NUMA: A max of %d VCPUs are supported\n",
1400 if (endvalue < value) {
1404 bitmap_set(node_cpumask[nodenr], value, endvalue-value+1);
1408 fprintf(stderr, "qemu: Invalid NUMA CPU range: %s\n", cpus);
1412 static void numa_add(const char *optarg)
1416 unsigned long long nodenr;
1418 optarg = get_opt_name(option, 128, optarg, ',');
1419 if (*optarg == ',') {
1422 if (!strcmp(option, "node")) {
1424 if (nb_numa_nodes >= MAX_NODES) {
1425 fprintf(stderr, "qemu: too many NUMA nodes\n");
1429 if (get_param_value(option, 128, "nodeid", optarg) == 0) {
1430 nodenr = nb_numa_nodes;
1432 if (parse_uint_full(option, &nodenr, 10) < 0) {
1433 fprintf(stderr, "qemu: Invalid NUMA nodeid: %s\n", option);
1438 if (nodenr >= MAX_NODES) {
1439 fprintf(stderr, "qemu: invalid NUMA nodeid: %llu\n", nodenr);
1443 if (get_param_value(option, 128, "mem", optarg) == 0) {
1444 node_mem[nodenr] = 0;
1447 sval = strtosz(option, &endptr);
1448 if (sval < 0 || *endptr) {
1449 fprintf(stderr, "qemu: invalid numa mem size: %s\n", optarg);
1452 node_mem[nodenr] = sval;
1454 if (get_param_value(option, 128, "cpus", optarg) != 0) {
1455 numa_node_parse_cpus(nodenr, option);
1459 fprintf(stderr, "Invalid -numa option: %s\n", option);
1464 static QemuOptsList qemu_smp_opts = {
1466 .implied_opt_name = "cpus",
1467 .merge_lists = true,
1468 .head = QTAILQ_HEAD_INITIALIZER(qemu_smp_opts.head),
1472 .type = QEMU_OPT_NUMBER,
1475 .type = QEMU_OPT_NUMBER,
1478 .type = QEMU_OPT_NUMBER,
1481 .type = QEMU_OPT_NUMBER,
1484 .type = QEMU_OPT_NUMBER,
1486 { /*End of list */ }
1490 static void smp_parse(QemuOpts *opts)
1494 unsigned cpus = qemu_opt_get_number(opts, "cpus", 0);
1495 unsigned sockets = qemu_opt_get_number(opts, "sockets", 0);
1496 unsigned cores = qemu_opt_get_number(opts, "cores", 0);
1497 unsigned threads = qemu_opt_get_number(opts, "threads", 0);
1499 /* compute missing values, prefer sockets over cores over threads */
1500 if (cpus == 0 || sockets == 0) {
1501 sockets = sockets > 0 ? sockets : 1;
1502 cores = cores > 0 ? cores : 1;
1503 threads = threads > 0 ? threads : 1;
1505 cpus = cores * threads * sockets;
1509 threads = threads > 0 ? threads : 1;
1510 cores = cpus / (sockets * threads);
1512 threads = cpus / (cores * sockets);
1516 max_cpus = qemu_opt_get_number(opts, "maxcpus", 0);
1519 smp_cores = cores > 0 ? cores : 1;
1520 smp_threads = threads > 0 ? threads : 1;
1524 if (max_cpus == 0) {
1525 max_cpus = smp_cpus;
1528 if (max_cpus > 255) {
1529 fprintf(stderr, "Unsupported number of maxcpus\n");
1532 if (max_cpus < smp_cpus) {
1533 fprintf(stderr, "maxcpus must be equal to or greater than smp\n");
1539 static void configure_realtime(QemuOpts *opts)
1543 enable_mlock = qemu_opt_get_bool(opts, "mlock", true);
1546 if (os_mlock() < 0) {
1547 fprintf(stderr, "qemu: locking memory failed\n");
1554 static void configure_msg(QemuOpts *opts)
1556 enable_timestamp_msg = qemu_opt_get_bool(opts, "timestamp", true);
1559 /***********************************************************/
1562 static int usb_device_add(const char *devname)
1565 USBDevice *dev = NULL;
1567 if (!usb_enabled(false)) {
1571 /* drivers with .usbdevice_name entry in USBDeviceInfo */
1572 dev = usbdevice_create(devname);
1576 /* the other ones */
1577 #ifndef CONFIG_LINUX
1578 /* only the linux version is qdev-ified, usb-bsd still needs this */
1579 if (strstart(devname, "host:", &p)) {
1580 dev = usb_host_device_open(usb_bus_find(-1), p);
1583 if (!strcmp(devname, "bt") || strstart(devname, "bt:", &p)) {
1584 dev = usb_bt_init(usb_bus_find(-1),
1585 devname[2] ? hci_init(p)
1586 : bt_new_hci(qemu_find_bt_vlan(0)));
1597 static int usb_device_del(const char *devname)
1602 if (strstart(devname, "host:", &p)) {
1606 if (!usb_enabled(false)) {
1610 p = strchr(devname, '.');
1613 bus_num = strtoul(devname, NULL, 0);
1614 addr = strtoul(p + 1, NULL, 0);
1616 return usb_device_delete_addr(bus_num, addr);
1619 static int usb_parse(const char *cmdline)
1622 r = usb_device_add(cmdline);
1624 fprintf(stderr, "qemu: could not add USB device '%s'\n", cmdline);
1629 void do_usb_add(Monitor *mon, const QDict *qdict)
1631 const char *devname = qdict_get_str(qdict, "devname");
1632 if (usb_device_add(devname) < 0) {
1633 error_report("could not add USB device '%s'", devname);
1637 void do_usb_del(Monitor *mon, const QDict *qdict)
1639 const char *devname = qdict_get_str(qdict, "devname");
1640 if (usb_device_del(devname) < 0) {
1641 error_report("could not delete USB device '%s'", devname);
1645 /***********************************************************/
1646 /* PCMCIA/Cardbus */
1648 static struct pcmcia_socket_entry_s {
1649 PCMCIASocket *socket;
1650 struct pcmcia_socket_entry_s *next;
1651 } *pcmcia_sockets = 0;
1653 void pcmcia_socket_register(PCMCIASocket *socket)
1655 struct pcmcia_socket_entry_s *entry;
1657 entry = g_malloc(sizeof(struct pcmcia_socket_entry_s));
1658 entry->socket = socket;
1659 entry->next = pcmcia_sockets;
1660 pcmcia_sockets = entry;
1663 void pcmcia_socket_unregister(PCMCIASocket *socket)
1665 struct pcmcia_socket_entry_s *entry, **ptr;
1667 ptr = &pcmcia_sockets;
1668 for (entry = *ptr; entry; ptr = &entry->next, entry = *ptr)
1669 if (entry->socket == socket) {
1675 void pcmcia_info(Monitor *mon, const QDict *qdict)
1677 struct pcmcia_socket_entry_s *iter;
1679 if (!pcmcia_sockets)
1680 monitor_printf(mon, "No PCMCIA sockets\n");
1682 for (iter = pcmcia_sockets; iter; iter = iter->next)
1683 monitor_printf(mon, "%s: %s\n", iter->socket->slot_string,
1684 iter->socket->attached ? iter->socket->card_string :
1688 /***********************************************************/
1689 /* machine registration */
1691 static QEMUMachine *first_machine = NULL;
1692 QEMUMachine *current_machine = NULL;
1694 int qemu_register_machine(QEMUMachine *m)
1697 pm = &first_machine;
1705 static QEMUMachine *find_machine(const char *name)
1709 for(m = first_machine; m != NULL; m = m->next) {
1710 if (!strcmp(m->name, name))
1712 if (m->alias && !strcmp(m->alias, name))
1718 QEMUMachine *find_default_machine(void)
1722 for(m = first_machine; m != NULL; m = m->next) {
1723 if (m->is_default) {
1730 MachineInfoList *qmp_query_machines(Error **errp)
1732 MachineInfoList *mach_list = NULL;
1735 for (m = first_machine; m; m = m->next) {
1736 MachineInfoList *entry;
1739 info = g_malloc0(sizeof(*info));
1740 if (m->is_default) {
1741 info->has_is_default = true;
1742 info->is_default = true;
1746 info->has_alias = true;
1747 info->alias = g_strdup(m->alias);
1750 info->name = g_strdup(m->name);
1751 info->cpu_max = !m->max_cpus ? 1 : m->max_cpus;
1753 entry = g_malloc0(sizeof(*entry));
1754 entry->value = info;
1755 entry->next = mach_list;
1762 /***********************************************************/
1763 /* main execution loop */
1765 struct vm_change_state_entry {
1766 VMChangeStateHandler *cb;
1768 QLIST_ENTRY (vm_change_state_entry) entries;
1771 static QLIST_HEAD(vm_change_state_head, vm_change_state_entry) vm_change_state_head;
1773 VMChangeStateEntry *qemu_add_vm_change_state_handler(VMChangeStateHandler *cb,
1776 VMChangeStateEntry *e;
1778 e = g_malloc0(sizeof (*e));
1782 QLIST_INSERT_HEAD(&vm_change_state_head, e, entries);
1786 void qemu_del_vm_change_state_handler(VMChangeStateEntry *e)
1788 QLIST_REMOVE (e, entries);
1792 void vm_state_notify(int running, RunState state)
1794 VMChangeStateEntry *e;
1796 trace_vm_state_notify(running, state);
1798 for (e = vm_change_state_head.lh_first; e; e = e->entries.le_next) {
1799 e->cb(e->opaque, running, state);
1805 if (!runstate_is_running()) {
1807 runstate_set(RUN_STATE_RUNNING);
1808 vm_state_notify(1, RUN_STATE_RUNNING);
1810 monitor_protocol_event(QEVENT_RESUME, NULL);
1814 /* reset/shutdown handler */
1816 typedef struct QEMUResetEntry {
1817 QTAILQ_ENTRY(QEMUResetEntry) entry;
1818 QEMUResetHandler *func;
1822 static QTAILQ_HEAD(reset_handlers, QEMUResetEntry) reset_handlers =
1823 QTAILQ_HEAD_INITIALIZER(reset_handlers);
1824 static int reset_requested;
1825 static int shutdown_requested, shutdown_signal = -1;
1826 static pid_t shutdown_pid;
1827 static int powerdown_requested;
1828 static int debug_requested;
1829 static int suspend_requested;
1830 static WakeupReason wakeup_reason;
1831 static NotifierList powerdown_notifiers =
1832 NOTIFIER_LIST_INITIALIZER(powerdown_notifiers);
1833 static NotifierList suspend_notifiers =
1834 NOTIFIER_LIST_INITIALIZER(suspend_notifiers);
1835 static NotifierList wakeup_notifiers =
1836 NOTIFIER_LIST_INITIALIZER(wakeup_notifiers);
1837 static uint32_t wakeup_reason_mask = ~(1 << QEMU_WAKEUP_REASON_NONE);
1838 static RunState vmstop_requested = RUN_STATE_MAX;
1840 int qemu_shutdown_requested_get(void)
1842 return shutdown_requested;
1845 int qemu_reset_requested_get(void)
1847 return reset_requested;
1850 static int qemu_shutdown_requested(void)
1852 int r = shutdown_requested;
1853 shutdown_requested = 0;
1857 static void qemu_kill_report(void)
1859 if (!qtest_enabled() && shutdown_signal != -1) {
1860 fprintf(stderr, "qemu: terminating on signal %d", shutdown_signal);
1861 if (shutdown_pid == 0) {
1862 /* This happens for eg ^C at the terminal, so it's worth
1863 * avoiding printing an odd message in that case.
1865 fputc('\n', stderr);
1867 fprintf(stderr, " from pid " FMT_pid "\n", shutdown_pid);
1869 shutdown_signal = -1;
1873 static int qemu_reset_requested(void)
1875 int r = reset_requested;
1876 reset_requested = 0;
1880 static int qemu_suspend_requested(void)
1882 int r = suspend_requested;
1883 suspend_requested = 0;
1887 static WakeupReason qemu_wakeup_requested(void)
1889 return wakeup_reason;
1892 static int qemu_powerdown_requested(void)
1894 int r = powerdown_requested;
1895 powerdown_requested = 0;
1899 static int qemu_debug_requested(void)
1901 int r = debug_requested;
1902 debug_requested = 0;
1906 /* We use RUN_STATE_MAX but any invalid value will do */
1907 static bool qemu_vmstop_requested(RunState *r)
1909 if (vmstop_requested < RUN_STATE_MAX) {
1910 *r = vmstop_requested;
1911 vmstop_requested = RUN_STATE_MAX;
1918 void qemu_register_reset(QEMUResetHandler *func, void *opaque)
1920 QEMUResetEntry *re = g_malloc0(sizeof(QEMUResetEntry));
1923 re->opaque = opaque;
1924 QTAILQ_INSERT_TAIL(&reset_handlers, re, entry);
1927 void qemu_unregister_reset(QEMUResetHandler *func, void *opaque)
1931 QTAILQ_FOREACH(re, &reset_handlers, entry) {
1932 if (re->func == func && re->opaque == opaque) {
1933 QTAILQ_REMOVE(&reset_handlers, re, entry);
1940 void qemu_devices_reset(void)
1942 QEMUResetEntry *re, *nre;
1944 /* reset all devices */
1945 QTAILQ_FOREACH_SAFE(re, &reset_handlers, entry, nre) {
1946 re->func(re->opaque);
1950 void qemu_system_reset(bool report)
1952 if (current_machine && current_machine->reset) {
1953 current_machine->reset();
1955 qemu_devices_reset();
1958 monitor_protocol_event(QEVENT_RESET, NULL);
1960 cpu_synchronize_all_post_reset();
1963 void qemu_system_reset_request(void)
1966 shutdown_requested = 1;
1968 reset_requested = 1;
1971 qemu_notify_event();
1974 static void qemu_system_suspend(void)
1977 notifier_list_notify(&suspend_notifiers, NULL);
1978 runstate_set(RUN_STATE_SUSPENDED);
1979 monitor_protocol_event(QEVENT_SUSPEND, NULL);
1982 void qemu_system_suspend_request(void)
1984 if (runstate_check(RUN_STATE_SUSPENDED)) {
1987 suspend_requested = 1;
1989 qemu_notify_event();
1992 void qemu_register_suspend_notifier(Notifier *notifier)
1994 notifier_list_add(&suspend_notifiers, notifier);
1997 void qemu_system_wakeup_request(WakeupReason reason)
2000 if (!runstate_check(RUN_STATE_SUSPENDED)) {
2003 if (!(wakeup_reason_mask & (1 << reason))) {
2006 runstate_set(RUN_STATE_RUNNING);
2007 wakeup_reason = reason;
2008 qemu_notify_event();
2011 void qemu_system_wakeup_enable(WakeupReason reason, bool enabled)
2014 wakeup_reason_mask |= (1 << reason);
2016 wakeup_reason_mask &= ~(1 << reason);
2020 void qemu_register_wakeup_notifier(Notifier *notifier)
2022 notifier_list_add(&wakeup_notifiers, notifier);
2025 void qemu_system_killed(int signal, pid_t pid)
2027 shutdown_signal = signal;
2032 shutdown_qemu_gracefully();
2034 qemu_system_shutdown_request();
2038 void qemu_system_shutdown_request(void)
2040 shutdown_requested = 1;
2041 qemu_notify_event();
2044 static void qemu_system_powerdown(void)
2046 monitor_protocol_event(QEVENT_POWERDOWN, NULL);
2047 notifier_list_notify(&powerdown_notifiers, NULL);
2050 void qemu_system_powerdown_request(void)
2052 powerdown_requested = 1;
2053 qemu_notify_event();
2056 void qemu_register_powerdown_notifier(Notifier *notifier)
2058 notifier_list_add(&powerdown_notifiers, notifier);
2061 void qemu_system_debug_request(void)
2063 debug_requested = 1;
2064 qemu_notify_event();
2067 void qemu_system_vmstop_request(RunState state)
2069 vmstop_requested = state;
2070 qemu_notify_event();
2073 static bool main_loop_should_exit(void)
2076 if (qemu_debug_requested()) {
2077 vm_stop(RUN_STATE_DEBUG);
2079 if (qemu_suspend_requested()) {
2080 qemu_system_suspend();
2082 if (qemu_shutdown_requested()) {
2084 monitor_protocol_event(QEVENT_SHUTDOWN, NULL);
2086 vm_stop(RUN_STATE_SHUTDOWN);
2091 if (qemu_reset_requested()) {
2093 cpu_synchronize_all_states();
2094 qemu_system_reset(VMRESET_REPORT);
2096 if (runstate_needs_reset()) {
2097 runstate_set(RUN_STATE_PAUSED);
2100 if (qemu_wakeup_requested()) {
2102 cpu_synchronize_all_states();
2104 // A "system reset" causes "virtio_queue" malfunction.
2105 // It might be a bug of virtio bus or virtio devices.
2106 // We don't want suspend(deep sleep), so It's OK now.
2107 // However, we should fix it later.
2108 qemu_system_reset(VMRESET_SILENT);
2110 notifier_list_notify(&wakeup_notifiers, &wakeup_reason);
2111 wakeup_reason = QEMU_WAKEUP_REASON_NONE;
2113 monitor_protocol_event(QEVENT_WAKEUP, NULL);
2115 if (qemu_powerdown_requested()) {
2116 qemu_system_powerdown();
2118 if (qemu_vmstop_requested(&r)) {
2124 static void main_loop(void)
2128 #ifdef CONFIG_PROFILER
2135 nonblocking = !kvm_enabled() && !xen_enabled() && !hax_enabled() && last_io > 0;
2136 #ifdef CONFIG_PROFILER
2137 ti = profile_getclock();
2139 last_io = main_loop_wait(nonblocking);
2140 #ifdef CONFIG_PROFILER
2141 dev_time += profile_getclock() - ti;
2143 } while (!main_loop_should_exit());
2146 static void version(void)
2148 printf("QEMU emulator version " QEMU_VERSION QEMU_PKGVERSION ", Copyright (c) 2003-2008 Fabrice Bellard\n");
2151 static void help(int exitcode)
2154 printf("usage: %s [options] [disk_image]\n\n"
2155 "'disk_image' is a raw hard disk image for IDE hard disk 0\n\n",
2156 error_get_progname());
2158 #define QEMU_OPTIONS_GENERATE_HELP
2159 #include "qemu-options-wrapper.h"
2161 printf("\nDuring emulation, the following keys are useful:\n"
2162 "ctrl-alt-f toggle full screen\n"
2163 "ctrl-alt-n switch to virtual console 'n'\n"
2164 "ctrl-alt toggle mouse and keyboard grab\n"
2166 "When using -nographic, press 'ctrl-a h' to get some help.\n");
2171 #define HAS_ARG 0x0001
2173 typedef struct QEMUOption {
2180 static const QEMUOption qemu_options[] = {
2181 { "h", 0, QEMU_OPTION_h, QEMU_ARCH_ALL },
2182 #define QEMU_OPTIONS_GENERATE_OPTIONS
2183 #include "qemu-options-wrapper.h"
2187 static bool vga_available(void)
2189 return object_class_by_name("VGA") || object_class_by_name("isa-vga");
2192 static bool cirrus_vga_available(void)
2194 return object_class_by_name("cirrus-vga")
2195 || object_class_by_name("isa-cirrus-vga");
2198 static bool vmware_vga_available(void)
2200 return object_class_by_name("vmware-svga");
2203 static bool qxl_vga_available(void)
2205 return object_class_by_name("qxl-vga");
2209 static bool maru_vga_available(void)
2211 return object_class_by_name("MARU_VGA");
2215 static void select_vgahw (const char *p)
2219 vga_interface_type = VGA_NONE;
2220 if (strstart(p, "std", &opts)) {
2221 if (vga_available()) {
2222 vga_interface_type = VGA_STD;
2224 fprintf(stderr, "Error: standard VGA not available\n");
2227 } else if (strstart(p, "cirrus", &opts)) {
2228 if (cirrus_vga_available()) {
2229 vga_interface_type = VGA_CIRRUS;
2231 fprintf(stderr, "Error: Cirrus VGA not available\n");
2234 } else if (strstart(p, "vmware", &opts)) {
2235 if (vmware_vga_available()) {
2236 vga_interface_type = VGA_VMWARE;
2238 fprintf(stderr, "Error: VMWare SVGA not available\n");
2241 } else if (strstart(p, "xenfb", &opts)) {
2242 vga_interface_type = VGA_XENFB;
2243 } else if (strstart(p, "qxl", &opts)) {
2244 if (qxl_vga_available()) {
2245 vga_interface_type = VGA_QXL;
2247 fprintf(stderr, "Error: QXL VGA not available\n");
2251 } else if (strstart(p, "maru", &opts)) {
2252 if (maru_vga_available()) {
2253 vga_interface_type = VGA_MARU;
2255 fprintf(stderr, "Error: MARU VGA not available\n");
2259 } else if (!strstart(p, "none", &opts)) {
2261 fprintf(stderr, "Unknown vga type: %s\n", p);
2265 const char *nextopt;
2267 if (strstart(opts, ",retrace=", &nextopt)) {
2269 if (strstart(opts, "dumb", &nextopt))
2270 vga_retrace_method = VGA_RETRACE_DUMB;
2271 else if (strstart(opts, "precise", &nextopt))
2272 vga_retrace_method = VGA_RETRACE_PRECISE;
2273 else goto invalid_vga;
2274 } else goto invalid_vga;
2279 static DisplayType select_display(const char *p)
2282 DisplayType display = DT_DEFAULT;
2284 if (strstart(p, "sdl", &opts)) {
2288 const char *nextopt;
2290 if (strstart(opts, ",frame=", &nextopt)) {
2292 if (strstart(opts, "on", &nextopt)) {
2294 } else if (strstart(opts, "off", &nextopt)) {
2297 goto invalid_sdl_args;
2299 } else if (strstart(opts, ",alt_grab=", &nextopt)) {
2301 if (strstart(opts, "on", &nextopt)) {
2303 } else if (strstart(opts, "off", &nextopt)) {
2306 goto invalid_sdl_args;
2308 } else if (strstart(opts, ",ctrl_grab=", &nextopt)) {
2310 if (strstart(opts, "on", &nextopt)) {
2312 } else if (strstart(opts, "off", &nextopt)) {
2315 goto invalid_sdl_args;
2317 } else if (strstart(opts, ",window_close=", &nextopt)) {
2319 if (strstart(opts, "on", &nextopt)) {
2321 } else if (strstart(opts, "off", &nextopt)) {
2324 goto invalid_sdl_args;
2328 fprintf(stderr, "Invalid SDL option string: %s\n", p);
2334 fprintf(stderr, "SDL support is disabled\n");
2337 } else if (strstart(p, "vnc", &opts)) {
2342 const char *nextopt;
2344 if (strstart(opts, "=", &nextopt)) {
2345 vnc_display = nextopt;
2349 fprintf(stderr, "VNC requires a display argument vnc=<display>\n");
2353 fprintf(stderr, "VNC support is disabled\n");
2356 } else if (strstart(p, "curses", &opts)) {
2357 #ifdef CONFIG_CURSES
2358 display = DT_CURSES;
2360 fprintf(stderr, "Curses support is disabled\n");
2363 } else if (strstart(p, "gtk", &opts)) {
2367 fprintf(stderr, "GTK support is disabled\n");
2370 } else if (strstart(p, "none", &opts)) {
2373 fprintf(stderr, "Unknown display type: %s\n", p);
2380 static int balloon_parse(const char *arg)
2384 if (strcmp(arg, "none") == 0) {
2388 if (!strncmp(arg, "virtio", 6)) {
2389 if (arg[6] == ',') {
2390 /* have params -> parse them */
2391 opts = qemu_opts_parse(qemu_find_opts("device"), arg+7, 0);
2395 /* create empty opts */
2396 opts = qemu_opts_create_nofail(qemu_find_opts("device"));
2398 qemu_opt_set(opts, "driver", "virtio-balloon");
2405 char *qemu_find_file(int type, const char *name)
2411 /* Try the name as a straight path first */
2412 if (access(name, R_OK) == 0) {
2413 trace_load_file(name, name);
2414 return g_strdup(name);
2418 case QEMU_FILE_TYPE_BIOS:
2421 case QEMU_FILE_TYPE_KEYMAP:
2422 subdir = "keymaps/";
2428 for (i = 0; i < data_dir_idx; i++) {
2429 buf = g_strdup_printf("%s/%s%s", data_dir[i], subdir, name);
2430 if (access(buf, R_OK) == 0) {
2431 trace_load_file(name, buf);
2439 static int device_help_func(QemuOpts *opts, void *opaque)
2441 return qdev_device_help(opts);
2444 static int device_init_func(QemuOpts *opts, void *opaque)
2449 if(maru_device_check(opts) == -1) {
2454 dev = qdev_device_add(opts);
2457 object_unref(OBJECT(dev));
2461 static int chardev_init_func(QemuOpts *opts, void *opaque)
2463 Error *local_err = NULL;
2465 qemu_chr_new_from_opts(opts, NULL, &local_err);
2466 if (error_is_set(&local_err)) {
2467 fprintf(stderr, "%s\n", error_get_pretty(local_err));
2468 error_free(local_err);
2474 #ifdef CONFIG_VIRTFS
2475 static int fsdev_init_func(QemuOpts *opts, void *opaque)
2478 ret = qemu_fsdev_add(opts);
2484 static int mon_init_func(QemuOpts *opts, void *opaque)
2486 CharDriverState *chr;
2487 const char *chardev;
2491 mode = qemu_opt_get(opts, "mode");
2495 if (strcmp(mode, "readline") == 0) {
2496 flags = MONITOR_USE_READLINE;
2497 } else if (strcmp(mode, "control") == 0) {
2498 flags = MONITOR_USE_CONTROL;
2500 fprintf(stderr, "unknown monitor mode \"%s\"\n", mode);
2504 if (qemu_opt_get_bool(opts, "pretty", 0))
2505 flags |= MONITOR_USE_PRETTY;
2507 if (qemu_opt_get_bool(opts, "default", 0))
2508 flags |= MONITOR_IS_DEFAULT;
2510 chardev = qemu_opt_get(opts, "chardev");
2511 chr = qemu_chr_find(chardev);
2513 fprintf(stderr, "chardev \"%s\" not found\n", chardev);
2517 qemu_chr_fe_claim_no_fail(chr);
2518 monitor_init(chr, flags);
2522 static void monitor_parse(const char *optarg, const char *mode)
2524 static int monitor_device_index = 0;
2530 if (strstart(optarg, "chardev:", &p)) {
2531 snprintf(label, sizeof(label), "%s", p);
2533 snprintf(label, sizeof(label), "compat_monitor%d",
2534 monitor_device_index);
2535 if (monitor_device_index == 0) {
2538 opts = qemu_chr_parse_compat(label, optarg);
2540 fprintf(stderr, "parse error: %s\n", optarg);
2545 opts = qemu_opts_create(qemu_find_opts("mon"), label, 1, NULL);
2547 fprintf(stderr, "duplicate chardev: %s\n", label);
2550 qemu_opt_set(opts, "mode", mode);
2551 qemu_opt_set(opts, "chardev", label);
2553 qemu_opt_set(opts, "default", "on");
2554 monitor_device_index++;
2557 struct device_config {
2559 DEV_USB, /* -usbdevice */
2561 DEV_SERIAL, /* -serial */
2562 DEV_PARALLEL, /* -parallel */
2563 DEV_VIRTCON, /* -virtioconsole */
2564 DEV_DEBUGCON, /* -debugcon */
2565 DEV_GDB, /* -gdb, -s */
2566 DEV_SCLP, /* s390 sclp */
2568 const char *cmdline;
2570 QTAILQ_ENTRY(device_config) next;
2573 static QTAILQ_HEAD(, device_config) device_configs =
2574 QTAILQ_HEAD_INITIALIZER(device_configs);
2576 static void add_device_config(int type, const char *cmdline)
2578 struct device_config *conf;
2580 conf = g_malloc0(sizeof(*conf));
2582 conf->cmdline = cmdline;
2583 loc_save(&conf->loc);
2584 QTAILQ_INSERT_TAIL(&device_configs, conf, next);
2587 static int foreach_device_config(int type, int (*func)(const char *cmdline))
2589 struct device_config *conf;
2592 QTAILQ_FOREACH(conf, &device_configs, next) {
2593 if (conf->type != type)
2595 loc_push_restore(&conf->loc);
2596 rc = func(conf->cmdline);
2597 loc_pop(&conf->loc);
2604 static int serial_parse(const char *devname)
2606 static int index = 0;
2609 if (strcmp(devname, "none") == 0)
2611 if (index == MAX_SERIAL_PORTS) {
2612 fprintf(stderr, "qemu: too many serial ports\n");
2615 snprintf(label, sizeof(label), "serial%d", index);
2616 serial_hds[index] = qemu_chr_new(label, devname, NULL);
2617 if (!serial_hds[index]) {
2618 fprintf(stderr, "qemu: could not connect serial device"
2619 " to character backend '%s'\n", devname);
2626 static int parallel_parse(const char *devname)
2628 static int index = 0;
2631 if (strcmp(devname, "none") == 0)
2633 if (index == MAX_PARALLEL_PORTS) {
2634 fprintf(stderr, "qemu: too many parallel ports\n");
2637 snprintf(label, sizeof(label), "parallel%d", index);
2638 parallel_hds[index] = qemu_chr_new(label, devname, NULL);
2639 if (!parallel_hds[index]) {
2640 fprintf(stderr, "qemu: could not connect parallel device"
2641 " to character backend '%s'\n", devname);
2648 static int virtcon_parse(const char *devname)
2650 QemuOptsList *device = qemu_find_opts("device");
2651 static int index = 0;
2653 QemuOpts *bus_opts, *dev_opts;
2655 if (strcmp(devname, "none") == 0)
2657 if (index == MAX_VIRTIO_CONSOLES) {
2658 fprintf(stderr, "qemu: too many virtio consoles\n");
2662 bus_opts = qemu_opts_create_nofail(device);
2663 if (arch_type == QEMU_ARCH_S390X) {
2664 qemu_opt_set(bus_opts, "driver", "virtio-serial-s390");
2666 qemu_opt_set(bus_opts, "driver", "virtio-serial-pci");
2669 dev_opts = qemu_opts_create_nofail(device);
2670 qemu_opt_set(dev_opts, "driver", "virtconsole");
2672 snprintf(label, sizeof(label), "virtcon%d", index);
2673 virtcon_hds[index] = qemu_chr_new(label, devname, NULL);
2674 if (!virtcon_hds[index]) {
2675 fprintf(stderr, "qemu: could not connect virtio console"
2676 " to character backend '%s'\n", devname);
2679 qemu_opt_set(dev_opts, "chardev", label);
2685 static int sclp_parse(const char *devname)
2687 QemuOptsList *device = qemu_find_opts("device");
2688 static int index = 0;
2692 if (strcmp(devname, "none") == 0) {
2695 if (index == MAX_SCLP_CONSOLES) {
2696 fprintf(stderr, "qemu: too many sclp consoles\n");
2700 assert(arch_type == QEMU_ARCH_S390X);
2702 dev_opts = qemu_opts_create(device, NULL, 0, NULL);
2703 qemu_opt_set(dev_opts, "driver", "sclpconsole");
2705 snprintf(label, sizeof(label), "sclpcon%d", index);
2706 sclp_hds[index] = qemu_chr_new(label, devname, NULL);
2707 if (!sclp_hds[index]) {
2708 fprintf(stderr, "qemu: could not connect sclp console"
2709 " to character backend '%s'\n", devname);
2712 qemu_opt_set(dev_opts, "chardev", label);
2718 static int debugcon_parse(const char *devname)
2722 if (!qemu_chr_new("debugcon", devname, NULL)) {
2725 opts = qemu_opts_create(qemu_find_opts("device"), "debugcon", 1, NULL);
2727 fprintf(stderr, "qemu: already have a debugcon device\n");
2730 qemu_opt_set(opts, "driver", "isa-debugcon");
2731 qemu_opt_set(opts, "chardev", "debugcon");
2735 static QEMUMachine *machine_parse(const char *name)
2737 QEMUMachine *m, *machine = NULL;
2740 machine = find_machine(name);
2745 printf("Supported machines are:\n");
2746 for (m = first_machine; m != NULL; m = m->next) {
2748 printf("%-20s %s (alias of %s)\n", m->alias, m->desc, m->name);
2750 printf("%-20s %s%s\n", m->name, m->desc,
2751 m->is_default ? " (default)" : "");
2753 exit(!name || !is_help_option(name));
2756 static int tcg_init(void)
2759 tcg_exec_init(tcg_tb_size * 1024 * 1024);
2760 ret = hax_accel_init();
2765 const char *opt_name;
2767 int (*available)(void);
2771 { "tcg", "tcg", tcg_available, tcg_init, &tcg_allowed },
2772 { "xen", "Xen", xen_available, xen_init, &xen_allowed },
2773 { "kvm", "KVM", kvm_available, kvm_init, &kvm_allowed },
2774 { "qtest", "QTest", qtest_available, qtest_init, &qtest_allowed },
2777 static int configure_accelerator(void)
2782 bool accel_initialised = false;
2783 bool init_failed = false;
2785 p = qemu_opt_get(qemu_get_machine_opts(), "accel");
2787 /* Use the default "accelerator", tcg */
2791 while (!accel_initialised && *p != '\0') {
2795 p = get_opt_name(buf, sizeof (buf), p, ':');
2796 for (i = 0; i < ARRAY_SIZE(accel_list); i++) {
2797 if (strcmp(accel_list[i].opt_name, buf) == 0) {
2798 if (!accel_list[i].available()) {
2799 printf("%s not supported for this target\n",
2800 accel_list[i].name);
2803 *(accel_list[i].allowed) = true;
2804 ret = accel_list[i].init();
2807 fprintf(stderr, "failed to initialize %s: %s\n",
2810 *(accel_list[i].allowed) = false;
2812 accel_initialised = true;
2817 if (i == ARRAY_SIZE(accel_list)) {
2818 fprintf(stderr, "\"%s\" accelerator does not exist.\n", buf);
2822 if (!accel_initialised) {
2824 fprintf(stderr, "No accelerator found!\n");
2827 maru_register_exit_msg(MARU_EXIT_UNKNOWN, "No accelerator found.");
2833 fprintf(stderr, "Back to %s accelerator.\n", accel_list[i].name);
2836 return !accel_initialised;
2839 void qemu_add_exit_notifier(Notifier *notify)
2841 notifier_list_add(&exit_notifiers, notify);
2844 void qemu_remove_exit_notifier(Notifier *notify)
2846 notifier_remove(notify);
2849 static void qemu_run_exit_notifiers(void)
2851 notifier_list_notify(&exit_notifiers, NULL);
2854 void qemu_add_machine_init_done_notifier(Notifier *notify)
2856 notifier_list_add(&machine_init_done_notifiers, notify);
2859 static void qemu_run_machine_init_done_notifiers(void)
2861 notifier_list_notify(&machine_init_done_notifiers, NULL);
2864 static const QEMUOption *lookup_opt(int argc, char **argv,
2865 const char **poptarg, int *poptind)
2867 const QEMUOption *popt;
2868 int optind = *poptind;
2869 char *r = argv[optind];
2872 loc_set_cmdline(argv, optind, 1);
2874 /* Treat --foo the same as -foo. */
2877 popt = qemu_options;
2880 error_report("invalid option");
2882 maru_register_exit_msg(MARU_EXIT_UNKNOWN, "invalid option.");
2886 if (!strcmp(popt->name, r + 1))
2890 if (popt->flags & HAS_ARG) {
2891 if (optind >= argc) {
2892 error_report("requires an argument");
2894 maru_register_exit_msg(MARU_EXIT_UNKNOWN, "requires an argument.");
2898 optarg = argv[optind++];
2899 loc_set_cmdline(argv, optind - 2, 2);
2910 static gpointer malloc_and_trace(gsize n_bytes)
2912 void *ptr = malloc(n_bytes);
2913 trace_g_malloc(n_bytes, ptr);
2917 static gpointer realloc_and_trace(gpointer mem, gsize n_bytes)
2919 void *ptr = realloc(mem, n_bytes);
2920 trace_g_realloc(mem, n_bytes, ptr);
2924 static void free_and_trace(gpointer mem)
2930 static int object_set_property(const char *name, const char *value, void *opaque)
2932 Object *obj = OBJECT(opaque);
2933 StringInputVisitor *siv;
2934 Error *local_err = NULL;
2936 if (strcmp(name, "qom-type") == 0 || strcmp(name, "id") == 0) {
2940 siv = string_input_visitor_new(value);
2941 object_property_set(obj, string_input_get_visitor(siv), name, &local_err);
2942 string_input_visitor_cleanup(siv);
2945 qerror_report_err(local_err);
2946 error_free(local_err);
2953 static int object_create(QemuOpts *opts, void *opaque)
2955 const char *type = qemu_opt_get(opts, "qom-type");
2956 const char *id = qemu_opts_id(opts);
2959 g_assert(type != NULL);
2962 qerror_report(QERR_MISSING_PARAMETER, "id");
2966 obj = object_new(type);
2967 if (qemu_opt_foreach(opts, object_set_property, obj, 1) < 0) {
2971 object_property_add_child(container_get(object_get_root(), "/objects"),
2978 int use_qemu_display = 0; //0:use tizen qemu sdl, 1:use original qemu sdl
2979 // W/A for preserve larger continuous heap for RAM.
2980 void *preallocated_ptr = 0;
2983 int main(int argc, char **argv, char **envp)
2986 int snapshot, linux_boot;
2987 const char *icount_option = NULL;
2988 const char *initrd_filename;
2989 const char *kernel_filename, *kernel_cmdline;
2990 const char *boot_order = NULL;
2992 int cyls, heads, secs, translation;
2993 QemuOpts *hda_opts = NULL, *opts, *machine_opts;
2994 QemuOptsList *olist;
2997 const char *loadvm = NULL;
2998 QEMUMachine *machine;
2999 const char *cpu_model;
3000 const char *vga_model = "none";
3001 const char *pid_file = NULL;
3002 const char *incoming = NULL;
3004 int show_vnc_port = 0;
3006 bool defconfig = true;
3007 bool userconfig = true;
3008 const char *log_mask = NULL;
3009 const char *log_file = NULL;
3010 GMemVTable mem_trace = {
3011 .malloc = malloc_and_trace,
3012 .realloc = realloc_and_trace,
3013 .free = free_and_trace,
3015 const char *trace_events = NULL;
3016 const char *trace_file = NULL;
3018 atexit(qemu_run_exit_notifiers);
3019 error_set_progname(argv[0]);
3021 g_mem_set_vtable(&mem_trace);
3022 if (!g_thread_supported()) {
3023 #if !GLIB_CHECK_VERSION(2, 31, 0)
3024 g_thread_init(NULL);
3026 fprintf(stderr, "glib threading failed to initialize.\n");
3031 module_call_init(MODULE_INIT_QOM);
3033 qemu_add_opts(&qemu_drive_opts);
3034 qemu_add_opts(&qemu_chardev_opts);
3035 qemu_add_opts(&qemu_device_opts);
3036 qemu_add_opts(&qemu_netdev_opts);
3037 qemu_add_opts(&qemu_net_opts);
3038 qemu_add_opts(&qemu_rtc_opts);
3039 qemu_add_opts(&qemu_global_opts);
3040 qemu_add_opts(&qemu_mon_opts);
3041 qemu_add_opts(&qemu_trace_opts);
3042 qemu_add_opts(&qemu_option_rom_opts);
3043 qemu_add_opts(&qemu_machine_opts);
3044 qemu_add_opts(&qemu_smp_opts);
3045 qemu_add_opts(&qemu_boot_opts);
3046 qemu_add_opts(&qemu_sandbox_opts);
3047 qemu_add_opts(&qemu_add_fd_opts);
3048 qemu_add_opts(&qemu_object_opts);
3049 qemu_add_opts(&qemu_tpmdev_opts);
3050 qemu_add_opts(&qemu_realtime_opts);
3051 qemu_add_opts(&qemu_msg_opts);
3056 rtc_clock = host_clock;
3058 qemu_cache_utils_init(envp);
3060 QLIST_INIT (&vm_change_state_head);
3061 os_setup_early_signal_handling();
3063 module_call_init(MODULE_INIT_MACHINE);
3064 machine = find_default_machine();
3068 cyls = heads = secs = 0;
3069 translation = BIOS_ATA_TRANSLATION_AUTO;
3071 for (i = 0; i < MAX_NODES; i++) {
3073 node_cpumask[i] = bitmap_new(MAX_CPUMASK_BITS);
3079 bdrv_init_with_whitelist();
3083 /* first pass of option parsing */
3085 while (optind < argc) {
3086 if (argv[optind][0] != '-') {
3091 const QEMUOption *popt;
3093 popt = lookup_opt(argc, argv, &optarg, &optind);
3094 switch (popt->index) {
3095 case QEMU_OPTION_nodefconfig:
3098 case QEMU_OPTION_nouserconfig:
3107 ret = qemu_read_default_config_files(userconfig);
3113 /* second pass of option parsing */
3118 if (argv[optind][0] != '-') {
3119 hda_opts = drive_add(IF_DEFAULT, 0, argv[optind++], HD_OPTS);
3121 const QEMUOption *popt;
3123 popt = lookup_opt(argc, argv, &optarg, &optind);
3124 if (!(popt->arch_mask & arch_type)) {
3125 printf("Option %s not supported for this target\n", popt->name);
3128 switch(popt->index) {
3130 machine = machine_parse(optarg);
3132 case QEMU_OPTION_no_kvm_irqchip: {
3133 olist = qemu_find_opts("machine");
3134 qemu_opts_parse(olist, "kernel_irqchip=off", 0);
3137 case QEMU_OPTION_cpu:
3138 /* hw initialization will check this */
3141 case QEMU_OPTION_hda:
3145 snprintf(buf, sizeof(buf), "%s", HD_OPTS);
3147 snprintf(buf, sizeof(buf),
3148 "%s,cyls=%d,heads=%d,secs=%d%s",
3149 HD_OPTS , cyls, heads, secs,
3150 translation == BIOS_ATA_TRANSLATION_LBA ?
3152 translation == BIOS_ATA_TRANSLATION_NONE ?
3153 ",trans=none" : "");
3154 drive_add(IF_DEFAULT, 0, optarg, buf);
3157 case QEMU_OPTION_hdb:
3158 case QEMU_OPTION_hdc:
3159 case QEMU_OPTION_hdd:
3160 drive_add(IF_DEFAULT, popt->index - QEMU_OPTION_hda, optarg,
3163 case QEMU_OPTION_drive:
3164 if (drive_def(optarg) == NULL) {
3168 case QEMU_OPTION_set:
3169 if (qemu_set_option(optarg) != 0)
3172 case QEMU_OPTION_global:
3173 if (qemu_global_option(optarg) != 0)
3176 case QEMU_OPTION_mtdblock:
3177 drive_add(IF_MTD, -1, optarg, MTD_OPTS);
3179 case QEMU_OPTION_sd:
3180 drive_add(IF_SD, -1, optarg, SD_OPTS);
3182 case QEMU_OPTION_pflash:
3183 drive_add(IF_PFLASH, -1, optarg, PFLASH_OPTS);
3185 case QEMU_OPTION_snapshot:
3188 case QEMU_OPTION_hdachs:
3192 cyls = strtol(p, (char **)&p, 0);
3193 if (cyls < 1 || cyls > 16383)
3198 heads = strtol(p, (char **)&p, 0);
3199 if (heads < 1 || heads > 16)
3204 secs = strtol(p, (char **)&p, 0);
3205 if (secs < 1 || secs > 63)
3209 if (!strcmp(p, "none"))
3210 translation = BIOS_ATA_TRANSLATION_NONE;
3211 else if (!strcmp(p, "lba"))
3212 translation = BIOS_ATA_TRANSLATION_LBA;
3213 else if (!strcmp(p, "auto"))
3214 translation = BIOS_ATA_TRANSLATION_AUTO;
3217 } else if (*p != '\0') {
3219 fprintf(stderr, "qemu: invalid physical CHS format\n");
3222 if (hda_opts != NULL) {
3224 snprintf(num, sizeof(num), "%d", cyls);
3225 qemu_opt_set(hda_opts, "cyls", num);
3226 snprintf(num, sizeof(num), "%d", heads);
3227 qemu_opt_set(hda_opts, "heads", num);
3228 snprintf(num, sizeof(num), "%d", secs);
3229 qemu_opt_set(hda_opts, "secs", num);
3230 if (translation == BIOS_ATA_TRANSLATION_LBA)
3231 qemu_opt_set(hda_opts, "trans", "lba");
3232 if (translation == BIOS_ATA_TRANSLATION_NONE)
3233 qemu_opt_set(hda_opts, "trans", "none");
3237 case QEMU_OPTION_numa:
3240 case QEMU_OPTION_display:
3241 display_type = select_display(optarg);
3243 case QEMU_OPTION_nographic:
3244 display_type = DT_NOGRAPHIC;
3246 case QEMU_OPTION_curses:
3247 #ifdef CONFIG_CURSES
3248 display_type = DT_CURSES;
3250 fprintf(stderr, "Curses support is disabled\n");
3254 case QEMU_OPTION_portrait:
3255 graphic_rotate = 90;
3257 case QEMU_OPTION_rotate:
3258 graphic_rotate = strtol(optarg, (char **) &optarg, 10);
3259 if (graphic_rotate != 0 && graphic_rotate != 90 &&
3260 graphic_rotate != 180 && graphic_rotate != 270) {
3262 "qemu: only 90, 180, 270 deg rotation is available\n");
3266 case QEMU_OPTION_kernel:
3267 qemu_opts_set(qemu_find_opts("machine"), 0, "kernel", optarg);
3269 case QEMU_OPTION_initrd:
3270 qemu_opts_set(qemu_find_opts("machine"), 0, "initrd", optarg);
3272 case QEMU_OPTION_append:
3273 qemu_opts_set(qemu_find_opts("machine"), 0, "append", optarg);
3275 case QEMU_OPTION_dtb:
3276 qemu_opts_set(qemu_find_opts("machine"), 0, "dtb", optarg);
3278 case QEMU_OPTION_cdrom:
3279 drive_add(IF_DEFAULT, 2, optarg, CDROM_OPTS);
3281 case QEMU_OPTION_boot:
3282 opts = qemu_opts_parse(qemu_find_opts("boot-opts"), optarg, 1);
3287 case QEMU_OPTION_fda:
3288 case QEMU_OPTION_fdb:
3289 drive_add(IF_FLOPPY, popt->index - QEMU_OPTION_fda,
3292 case QEMU_OPTION_no_fd_bootchk:
3295 case QEMU_OPTION_netdev:
3296 if (net_client_parse(qemu_find_opts("netdev"), optarg) == -1) {
3300 case QEMU_OPTION_net:
3301 if (net_client_parse(qemu_find_opts("net"), optarg) == -1) {
3305 #ifdef CONFIG_LIBISCSI
3306 case QEMU_OPTION_iscsi:
3307 opts = qemu_opts_parse(qemu_find_opts("iscsi"), optarg, 0);
3314 case QEMU_OPTION_tftp:
3315 legacy_tftp_prefix = optarg;
3317 case QEMU_OPTION_bootp:
3318 legacy_bootp_filename = optarg;
3320 case QEMU_OPTION_redir:
3321 if (net_slirp_redir(optarg) < 0)
3325 case QEMU_OPTION_bt:
3326 add_device_config(DEV_BT, optarg);
3328 case QEMU_OPTION_audio_help:
3332 case QEMU_OPTION_soundhw:
3333 select_soundhw (optarg);
3338 case QEMU_OPTION_version:
3342 case QEMU_OPTION_m: {
3347 value = strtosz(optarg, &end);
3348 if (value < 0 || *end) {
3349 fprintf(stderr, "qemu: invalid ram size: %s\n", optarg);
3352 sz = QEMU_ALIGN_UP((uint64_t)value, 8192);
3354 if (ram_size != sz) {
3355 fprintf(stderr, "qemu: ram size too large\n");
3361 case QEMU_OPTION_tpmdev:
3362 if (tpm_config_parse(qemu_find_opts("tpmdev"), optarg) < 0) {
3367 case QEMU_OPTION_mempath:
3371 case QEMU_OPTION_mem_prealloc:
3382 add_device_config(DEV_GDB, "tcp::" DEFAULT_GDBSTUB_PORT);
3384 case QEMU_OPTION_gdb:
3385 add_device_config(DEV_GDB, optarg);
3388 if (data_dir_idx < ARRAY_SIZE(data_dir)) {
3389 data_dir[data_dir_idx++] = optarg;
3392 case QEMU_OPTION_bios:
3395 case QEMU_OPTION_singlestep:
3402 keyboard_layout = optarg;
3404 case QEMU_OPTION_localtime:
3407 case QEMU_OPTION_vga:
3416 w = strtol(p, (char **)&p, 10);
3419 fprintf(stderr, "qemu: invalid resolution or depth\n");
3425 h = strtol(p, (char **)&p, 10);
3430 depth = strtol(p, (char **)&p, 10);
3431 if (depth != 8 && depth != 15 && depth != 16 &&
3432 depth != 24 && depth != 32)
3434 } else if (*p == '\0') {
3435 depth = graphic_depth;
3442 graphic_depth = depth;
3445 case QEMU_OPTION_echr:
3448 term_escape_char = strtol(optarg, &r, 0);
3450 printf("Bad argument to echr\n");
3453 case QEMU_OPTION_monitor:
3454 default_monitor = 0;
3455 if (strncmp(optarg, "none", 4)) {
3456 monitor_parse(optarg, "readline");
3459 case QEMU_OPTION_qmp:
3460 monitor_parse(optarg, "control");
3461 default_monitor = 0;
3463 case QEMU_OPTION_mon:
3464 opts = qemu_opts_parse(qemu_find_opts("mon"), optarg, 1);
3468 default_monitor = 0;
3470 case QEMU_OPTION_chardev:
3471 opts = qemu_opts_parse(qemu_find_opts("chardev"), optarg, 1);
3476 case QEMU_OPTION_fsdev:
3477 olist = qemu_find_opts("fsdev");
3479 fprintf(stderr, "fsdev is not supported by this qemu build.\n");
3482 opts = qemu_opts_parse(olist, optarg, 1);
3487 case QEMU_OPTION_virtfs: {
3490 const char *writeout, *sock_fd, *socket;
3492 olist = qemu_find_opts("virtfs");
3494 fprintf(stderr, "virtfs is not supported by this qemu build.\n");
3497 opts = qemu_opts_parse(olist, optarg, 1);
3502 if (qemu_opt_get(opts, "fsdriver") == NULL ||
3503 qemu_opt_get(opts, "mount_tag") == NULL) {
3504 fprintf(stderr, "Usage: -virtfs fsdriver,mount_tag=tag.\n");
3507 fsdev = qemu_opts_create(qemu_find_opts("fsdev"),
3508 qemu_opt_get(opts, "mount_tag"),
3511 fprintf(stderr, "duplicate fsdev id: %s\n",
3512 qemu_opt_get(opts, "mount_tag"));
3516 writeout = qemu_opt_get(opts, "writeout");
3518 #ifdef CONFIG_SYNC_FILE_RANGE
3519 qemu_opt_set(fsdev, "writeout", writeout);
3521 fprintf(stderr, "writeout=immediate not supported on "
3526 qemu_opt_set(fsdev, "fsdriver", qemu_opt_get(opts, "fsdriver"));
3527 qemu_opt_set(fsdev, "path", qemu_opt_get(opts, "path"));
3528 qemu_opt_set(fsdev, "security_model",
3529 qemu_opt_get(opts, "security_model"));
3530 socket = qemu_opt_get(opts, "socket");
3532 qemu_opt_set(fsdev, "socket", socket);
3534 sock_fd = qemu_opt_get(opts, "sock_fd");
3536 qemu_opt_set(fsdev, "sock_fd", sock_fd);
3539 qemu_opt_set_bool(fsdev, "readonly",
3540 qemu_opt_get_bool(opts, "readonly", 0));
3541 device = qemu_opts_create_nofail(qemu_find_opts("device"));
3542 qemu_opt_set(device, "driver", "virtio-9p-pci");
3543 qemu_opt_set(device, "fsdev",
3544 qemu_opt_get(opts, "mount_tag"));
3545 qemu_opt_set(device, "mount_tag",
3546 qemu_opt_get(opts, "mount_tag"));
3549 case QEMU_OPTION_virtfs_synth: {
3553 fsdev = qemu_opts_create(qemu_find_opts("fsdev"), "v_synth",
3556 fprintf(stderr, "duplicate option: %s\n", "virtfs_synth");
3559 qemu_opt_set(fsdev, "fsdriver", "synth");
3561 device = qemu_opts_create_nofail(qemu_find_opts("device"));
3562 qemu_opt_set(device, "driver", "virtio-9p-pci");
3563 qemu_opt_set(device, "fsdev", "v_synth");
3564 qemu_opt_set(device, "mount_tag", "v_synth");
3567 case QEMU_OPTION_serial:
3568 add_device_config(DEV_SERIAL, optarg);
3570 if (strncmp(optarg, "mon:", 4) == 0) {
3571 default_monitor = 0;
3574 case QEMU_OPTION_watchdog:
3577 "qemu: only one watchdog option may be given\n");
3582 case QEMU_OPTION_watchdog_action:
3583 if (select_watchdog_action(optarg) == -1) {
3584 fprintf(stderr, "Unknown -watchdog-action parameter\n");
3588 case QEMU_OPTION_virtiocon:
3589 add_device_config(DEV_VIRTCON, optarg);
3590 default_virtcon = 0;
3591 if (strncmp(optarg, "mon:", 4) == 0) {
3592 default_monitor = 0;
3595 case QEMU_OPTION_parallel:
3596 add_device_config(DEV_PARALLEL, optarg);
3597 default_parallel = 0;
3598 if (strncmp(optarg, "mon:", 4) == 0) {
3599 default_monitor = 0;
3602 case QEMU_OPTION_debugcon:
3603 add_device_config(DEV_DEBUGCON, optarg);
3605 case QEMU_OPTION_loadvm:
3608 case QEMU_OPTION_full_screen:
3611 case QEMU_OPTION_no_frame:
3614 case QEMU_OPTION_alt_grab:
3617 case QEMU_OPTION_ctrl_grab:
3620 case QEMU_OPTION_no_quit:
3623 case QEMU_OPTION_sdl:
3625 display_type = DT_SDL;
3628 fprintf(stderr, "SDL support is disabled\n");
3631 case QEMU_OPTION_pidfile:
3634 case QEMU_OPTION_win2k_hack:
3635 win2k_install_hack = 1;
3637 case QEMU_OPTION_rtc_td_hack: {
3638 static GlobalProperty slew_lost_ticks[] = {
3640 .driver = "mc146818rtc",
3641 .property = "lost_tick_policy",
3644 { /* end of list */ }
3647 qdev_prop_register_global_list(slew_lost_ticks);
3650 case QEMU_OPTION_acpitable:
3651 opts = qemu_opts_parse(qemu_find_opts("acpi"), optarg, 1);
3652 g_assert(opts != NULL);
3653 do_acpitable_option(opts);
3655 case QEMU_OPTION_smbios:
3656 do_smbios_option(optarg);
3658 case QEMU_OPTION_enable_kvm:
3659 olist = qemu_find_opts("machine");
3660 qemu_opts_parse(olist, "accel=kvm", 0);
3662 case QEMU_OPTION_enable_gl:
3663 #if defined(CONFIG_MARU) && defined(CONFIG_GL_BACKEND)
3666 fprintf(stderr, "Virtio GL support is disabled, ignoring -enable-gl\n");
3669 case QEMU_OPTION_enable_yagl:
3670 #if defined(CONFIG_YAGL)
3673 fprintf(stderr, "YaGL openGLES passthrough support is disabled,"
3674 " ignoring -enable-yagl\n");
3677 case QEMU_OPTION_yagl_backend:
3678 #if defined(CONFIG_YAGL)
3679 yagl_backend = optarg;
3681 fprintf(stderr, "YaGL openGLES passthrough support is disabled,"
3682 " ignoring -yagl-backend\n");
3685 case QEMU_OPTION_enable_vigs:
3686 #if defined(CONFIG_VIGS)
3689 fprintf(stderr, "VIGS support is disabled,"
3690 " ignoring -enable-vigs\n");
3693 case QEMU_OPTION_vigs_backend:
3694 #if defined(CONFIG_VIGS)
3695 vigs_backend = g_strdup(optarg);
3697 fprintf(stderr, "VIGS support is disabled,"
3698 " ignoring -vigs-backend\n");
3701 case QEMU_OPTION_machine:
3702 olist = qemu_find_opts("machine");
3703 opts = qemu_opts_parse(olist, optarg, 1);
3707 optarg = qemu_opt_get(opts, "type");
3709 machine = machine_parse(optarg);
3712 case QEMU_OPTION_no_kvm:
3713 olist = qemu_find_opts("machine");
3714 qemu_opts_parse(olist, "accel=tcg", 0);
3716 case QEMU_OPTION_no_kvm_pit: {
3717 fprintf(stderr, "Warning: KVM PIT can no longer be disabled "
3721 case QEMU_OPTION_no_kvm_pit_reinjection: {
3722 static GlobalProperty kvm_pit_lost_tick_policy[] = {
3724 .driver = "kvm-pit",
3725 .property = "lost_tick_policy",
3728 { /* end of list */ }
3731 fprintf(stderr, "Warning: option deprecated, use "
3732 "lost_tick_policy property of kvm-pit instead.\n");
3733 qdev_prop_register_global_list(kvm_pit_lost_tick_policy);
3736 case QEMU_OPTION_usb:
3737 olist = qemu_find_opts("machine");
3738 qemu_opts_parse(olist, "usb=on", 0);
3740 case QEMU_OPTION_usbdevice:
3741 olist = qemu_find_opts("machine");
3742 qemu_opts_parse(olist, "usb=on", 0);
3743 add_device_config(DEV_USB, optarg);
3745 case QEMU_OPTION_device:
3746 if (!qemu_opts_parse(qemu_find_opts("device"), optarg, 1)) {
3750 case QEMU_OPTION_smp:
3751 if (!qemu_opts_parse(qemu_find_opts("smp-opts"), optarg, 1)) {
3755 case QEMU_OPTION_vnc:
3758 vnc_display = optarg;
3760 fprintf(stderr, "VNC support is disabled\n");
3764 case QEMU_OPTION_no_acpi:
3767 case QEMU_OPTION_no_hpet:
3770 case QEMU_OPTION_balloon:
3771 if (balloon_parse(optarg) < 0) {
3772 fprintf(stderr, "Unknown -balloon argument %s\n", optarg);
3776 case QEMU_OPTION_no_reboot:
3779 case QEMU_OPTION_no_shutdown:
3782 case QEMU_OPTION_show_cursor:
3785 case QEMU_OPTION_uuid:
3786 if(qemu_uuid_parse(optarg, qemu_uuid) < 0) {
3787 fprintf(stderr, "Fail to parse UUID string."
3788 " Wrong format.\n");
3792 case QEMU_OPTION_option_rom:
3793 if (nb_option_roms >= MAX_OPTION_ROMS) {
3794 fprintf(stderr, "Too many option ROMs\n");
3797 opts = qemu_opts_parse(qemu_find_opts("option-rom"), optarg, 1);
3801 option_rom[nb_option_roms].name = qemu_opt_get(opts, "romfile");
3802 option_rom[nb_option_roms].bootindex =
3803 qemu_opt_get_number(opts, "bootindex", -1);
3804 if (!option_rom[nb_option_roms].name) {
3805 fprintf(stderr, "Option ROM file is not specified\n");
3810 case QEMU_OPTION_semihosting:
3811 semihosting_enabled = 1;
3813 case QEMU_OPTION_tdf:
3814 fprintf(stderr, "Warning: user space PIT time drift fix "
3815 "is no longer supported.\n");
3817 case QEMU_OPTION_name:
3818 qemu_name = g_strdup(optarg);
3820 char *p = strchr(qemu_name, ',');
3823 if (strncmp(p, "process=", 8)) {
3824 fprintf(stderr, "Unknown subargument %s to -name\n", p);
3828 os_set_proc_name(p);
3832 case QEMU_OPTION_prom_env:
3833 if (nb_prom_envs >= MAX_PROM_ENVS) {
3834 fprintf(stderr, "Too many prom variables\n");
3837 prom_envs[nb_prom_envs] = optarg;
3840 case QEMU_OPTION_old_param:
3843 case QEMU_OPTION_clock:
3844 configure_alarms(optarg);
3846 case QEMU_OPTION_startdate:
3847 configure_rtc_date_offset(optarg, 1);
3849 case QEMU_OPTION_rtc:
3850 opts = qemu_opts_parse(qemu_find_opts("rtc"), optarg, 0);
3854 configure_rtc(opts);
3856 case QEMU_OPTION_tb_size:
3857 tcg_tb_size = strtol(optarg, NULL, 0);
3858 if (tcg_tb_size < 0) {
3862 case QEMU_OPTION_icount:
3863 icount_option = optarg;
3865 case QEMU_OPTION_incoming:
3867 runstate_set(RUN_STATE_INMIGRATE);
3869 case QEMU_OPTION_nodefaults:
3871 default_parallel = 0;
3872 default_virtcon = 0;
3874 default_monitor = 0;
3881 case QEMU_OPTION_xen_domid:
3882 if (!(xen_available())) {
3883 printf("Option %s not supported for this target\n", popt->name);
3886 xen_domid = atoi(optarg);
3888 case QEMU_OPTION_xen_create:
3889 if (!(xen_available())) {
3890 printf("Option %s not supported for this target\n", popt->name);
3893 xen_mode = XEN_CREATE;
3895 case QEMU_OPTION_xen_attach:
3896 if (!(xen_available())) {
3897 printf("Option %s not supported for this target\n", popt->name);
3900 xen_mode = XEN_ATTACH;
3902 case QEMU_OPTION_trace:
3904 opts = qemu_opts_parse(qemu_find_opts("trace"), optarg, 0);
3908 trace_events = qemu_opt_get(opts, "events");
3909 trace_file = qemu_opt_get(opts, "file");
3912 case QEMU_OPTION_readconfig:
3914 int ret = qemu_read_config_file(optarg);
3916 fprintf(stderr, "read config %s: %s\n", optarg,
3922 case QEMU_OPTION_spice:
3923 olist = qemu_find_opts("spice");
3925 fprintf(stderr, "spice is not supported by this qemu build.\n");
3931 opts = qemu_opts_parse(olist, optarg, 0);
3937 case QEMU_OPTION_writeconfig:
3940 if (strcmp(optarg, "-") == 0) {
3943 fp = fopen(optarg, "w");
3945 fprintf(stderr, "open %s: %s\n", optarg, strerror(errno));
3949 qemu_config_write(fp);
3953 case QEMU_OPTION_qtest:
3954 qtest_chrdev = optarg;
3956 case QEMU_OPTION_qtest_log:
3959 case QEMU_OPTION_sandbox:
3960 opts = qemu_opts_parse(qemu_find_opts("sandbox"), optarg, 1);
3965 case QEMU_OPTION_enable_hax:
3966 #ifdef CONFIG_HAX_BACKEND
3967 olist = qemu_find_opts("machine");
3968 //qemu_opts_reset(olist);
3970 //qemu_opts_parse(olist, "accel=hax", 0);
3973 "HAX support is disabled, ignoring -enable-hax\n");
3977 case QEMU_OPTION_max_touch_point:
3979 int cnt = atoi(optarg);
3980 fprintf(stderr, "maxTouchPoint:%d\n", cnt);
3981 set_emul_max_touch_point(cnt);
3984 case QEMU_OPTION_disable_skin:
3988 case QEMU_OPTION_add_fd:
3990 opts = qemu_opts_parse(qemu_find_opts("add-fd"), optarg, 0);
3995 error_report("File descriptor passing is disabled on this "
4001 case QEMU_OPTION_enable_suspend:
4002 ecs_set_suspend_state(SUSPEND_UNLOCK);
4005 case QEMU_OPTION_object:
4006 opts = qemu_opts_parse(qemu_find_opts("object"), optarg, 1);
4011 case QEMU_OPTION_realtime:
4012 opts = qemu_opts_parse(qemu_find_opts("realtime"), optarg, 0);
4016 configure_realtime(opts);
4018 case QEMU_OPTION_msg:
4019 opts = qemu_opts_parse(qemu_find_opts("msg"), optarg, 0);
4023 configure_msg(opts);
4026 os_parse_cmd_args(popt->index, optarg);
4032 if (qemu_init_main_loop()) {
4033 fprintf(stderr, "qemu_init_main_loop failed\n");
4037 if (qemu_opts_foreach(qemu_find_opts("sandbox"), parse_sandbox, NULL, 0)) {
4042 if (qemu_opts_foreach(qemu_find_opts("add-fd"), parse_add_fd, NULL, 1)) {
4046 if (qemu_opts_foreach(qemu_find_opts("add-fd"), cleanup_add_fd, NULL, 1)) {
4051 if (machine == NULL) {
4052 fprintf(stderr, "No machine found.\n");
4056 if (machine->hw_version) {
4057 qemu_set_version(machine->hw_version);
4060 if (qemu_opts_foreach(qemu_find_opts("object"),
4061 object_create, NULL, 0) != 0) {
4065 /* Init CPU def lists, based on config
4066 * - Must be called after all the qemu_read_config_file() calls
4067 * - Must be called before list_cpus()
4068 * - Must be called before machine->init()
4072 if (cpu_model && is_help_option(cpu_model)) {
4073 list_cpus(stdout, &fprintf, cpu_model);
4077 #if defined(CONFIG_MARU)
4078 if (enable_gl && enable_yagl) {
4079 fprintf (stderr, "Error: only one openGL passthrough device can be used at one time!\n");
4084 if (!vigs_backend) {
4085 vigs_backend = g_strdup("gl");
4088 if (strcmp(vigs_backend, "gl") != 0 && strcmp(vigs_backend, "sw") != 0) {
4089 fprintf (stderr, "Error: Bad VIGS backend - %s!\n", vigs_backend);
4095 if (!yagl_backend) {
4096 yagl_backend = "offscreen";
4099 if (strcmp(yagl_backend, "offscreen") != 0) {
4100 if (strcmp(yagl_backend, "vigs") == 0) {
4102 fprintf (stderr, "Error: Bad YaGL backend - %s, VIGS not enabled!\n", yagl_backend);
4105 if (strcmp(vigs_backend, "gl") != 0) {
4106 fprintf (stderr, "Error: Bad YaGL backend - %s, VIGS is not configured with gl backend!\n", yagl_backend);
4110 fprintf (stderr, "Error: Bad YaGL backend - %s!\n", yagl_backend);
4116 /* Open the logfile at this point, if necessary. We can't open the logfile
4117 * when encountering either of the logging options (-d or -D) because the
4118 * other one may be encountered later on the command line, changing the
4119 * location or level of logging.
4124 qemu_set_log_filename(log_file);
4127 mask = qemu_str_to_log_mask(log_mask);
4129 qemu_print_log_usage(stdout);
4135 if (!trace_backend_init(trace_events, trace_file)) {
4139 /* If no data_dir is specified then try to find it relative to the
4141 if (data_dir_idx < ARRAY_SIZE(data_dir)) {
4142 data_dir[data_dir_idx] = os_find_datadir(argv[0]);
4143 if (data_dir[data_dir_idx] != NULL) {
4147 /* If all else fails use the install path specified when building. */
4148 if (data_dir_idx < ARRAY_SIZE(data_dir)) {
4149 data_dir[data_dir_idx++] = CONFIG_QEMU_DATADIR;
4152 smp_parse(qemu_opts_find(qemu_find_opts("smp-opts"), NULL));
4154 machine->max_cpus = machine->max_cpus ?: 1; /* Default to UP */
4155 if (smp_cpus > machine->max_cpus) {
4156 fprintf(stderr, "Number of SMP cpus requested (%d), exceeds max cpus "
4157 "supported by machine `%s' (%d)\n", smp_cpus, machine->name,
4163 * Get the default machine options from the machine if it is not already
4164 * specified either by the configuration file or by the command line.
4166 if (machine->default_machine_opts) {
4167 qemu_opts_set_defaults(qemu_find_opts("machine"),
4168 machine->default_machine_opts, 0);
4171 qemu_opts_foreach(qemu_find_opts("device"), default_driver_check, NULL, 0);
4172 qemu_opts_foreach(qemu_find_opts("global"), default_driver_check, NULL, 0);
4174 if (machine->no_serial) {
4177 if (machine->no_parallel) {
4178 default_parallel = 0;
4180 if (!machine->use_virtcon) {
4181 default_virtcon = 0;
4183 if (!machine->use_sclp) {
4186 if (machine->no_floppy) {
4189 if (machine->no_cdrom) {
4192 if (machine->no_sdcard) {
4196 if (is_daemonized()) {
4197 /* According to documentation and historically, -nographic redirects
4198 * serial port, parallel port and monitor to stdio, which does not work
4199 * with -daemonize. We can redirect these to null instead, but since
4200 * -nographic is legacy, let's just error out.
4201 * We disallow -nographic only if all other ports are not redirected
4202 * explicitly, to not break existing legacy setups which uses
4203 * -nographic _and_ redirects all ports explicitly - this is valid
4204 * usage, -nographic is just a no-op in this case.
4206 if (display_type == DT_NOGRAPHIC
4207 && (default_parallel || default_serial
4208 || default_monitor || default_virtcon)) {
4209 fprintf(stderr, "-nographic can not be used with -daemonize\n");
4212 #ifdef CONFIG_CURSES
4213 if (display_type == DT_CURSES) {
4214 fprintf(stderr, "curses display can not be used with -daemonize\n");
4220 if (display_type == DT_NOGRAPHIC) {
4221 if (default_parallel)
4222 add_device_config(DEV_PARALLEL, "null");
4223 if (default_serial && default_monitor) {
4224 add_device_config(DEV_SERIAL, "mon:stdio");
4225 } else if (default_virtcon && default_monitor) {
4226 add_device_config(DEV_VIRTCON, "mon:stdio");
4227 } else if (default_sclp && default_monitor) {
4228 add_device_config(DEV_SCLP, "mon:stdio");
4231 add_device_config(DEV_SERIAL, "stdio");
4232 if (default_virtcon)
4233 add_device_config(DEV_VIRTCON, "stdio");
4235 add_device_config(DEV_SCLP, "stdio");
4237 if (default_monitor)
4238 monitor_parse("stdio", "readline");
4242 add_device_config(DEV_SERIAL, "vc:80Cx24C");
4243 if (default_parallel)
4244 add_device_config(DEV_PARALLEL, "vc:80Cx24C");
4245 if (default_monitor)
4246 monitor_parse("vc:80Cx24C", "readline");
4247 if (default_virtcon)
4248 add_device_config(DEV_VIRTCON, "vc:80Cx24C");
4250 add_device_config(DEV_SCLP, "vc:80Cx24C");
4254 if (display_type == DT_DEFAULT && !display_remote) {
4255 #if defined(CONFIG_MARU)
4256 display_type = DT_MARU;
4257 #elif defined(CONFIG_GTK)
4258 display_type = DT_GTK;
4259 #elif defined(CONFIG_SDL) || defined(CONFIG_COCOA)
4260 display_type = DT_SDL;
4261 #elif defined(CONFIG_VNC)
4262 vnc_display = "localhost:0,to=99";
4265 display_type = DT_NONE;
4269 if ((no_frame || alt_grab || ctrl_grab) && display_type != DT_SDL) {
4270 fprintf(stderr, "-no-frame, -alt-grab and -ctrl-grab are only valid "
4271 "for SDL, ignoring option\n");
4273 if (no_quit && (display_type != DT_GTK && display_type != DT_SDL)) {
4274 fprintf(stderr, "-no-quit is only valid for GTK and SDL, "
4275 "ignoring option\n");
4278 #if defined(CONFIG_GTK)
4279 if (display_type == DT_GTK) {
4280 early_gtk_display_init();
4288 if (qemu_opts_foreach(qemu_find_opts("chardev"), chardev_init_func, NULL, 1) != 0)
4290 #ifdef CONFIG_VIRTFS
4291 if (qemu_opts_foreach(qemu_find_opts("fsdev"), fsdev_init_func, NULL, 1) != 0) {
4298 if (pid_file && qemu_create_pidfile(pid_file) != 0) {
4303 /* init the memory */
4304 if (ram_size == 0) {
4305 ram_size = DEFAULT_RAM_SIZE * 1024 * 1024;
4308 // W/A for preserve larger continuous heap for RAM.
4309 preallocated_ptr = g_malloc(ram_size);
4312 hax_pre_init(ram_size);
4314 if (qemu_opts_foreach(qemu_find_opts("device"), device_help_func, NULL, 0)
4319 configure_accelerator();
4321 if (!qtest_enabled() && qtest_chrdev) {
4325 machine_opts = qemu_get_machine_opts();
4326 kernel_filename = qemu_opt_get(machine_opts, "kernel");
4327 initrd_filename = qemu_opt_get(machine_opts, "initrd");
4328 kernel_cmdline = qemu_opt_get(machine_opts, "append");
4331 boot_order = machine->boot_order;
4333 opts = qemu_opts_find(qemu_find_opts("boot-opts"), NULL);
4335 char *normal_boot_order;
4336 const char *order, *once;
4338 order = qemu_opt_get(opts, "order");
4340 validate_bootdevices(order);
4344 once = qemu_opt_get(opts, "once");
4346 validate_bootdevices(once);
4347 normal_boot_order = g_strdup(boot_order);
4349 qemu_register_reset(restore_boot_order, normal_boot_order);
4352 boot_menu = qemu_opt_get_bool(opts, "menu", boot_menu);
4355 if (!kernel_cmdline) {
4356 kernel_cmdline = "";
4359 linux_boot = (kernel_filename != NULL);
4361 if (!linux_boot && *kernel_cmdline != '\0') {
4362 fprintf(stderr, "-append only allowed with -kernel option\n");
4366 if (!linux_boot && initrd_filename != NULL) {
4367 fprintf(stderr, "-initrd only allowed with -kernel option\n");
4371 if (!linux_boot && qemu_opt_get(machine_opts, "dtb")) {
4372 fprintf(stderr, "-dtb only allowed with -kernel option\n");
4376 os_set_line_buffering();
4378 qemu_init_cpu_loop();
4379 qemu_mutex_lock_iothread();
4382 /* spice needs the timers to be initialized by this point */
4386 if (icount_option && (kvm_enabled() || xen_enabled() || hax_enabled())) {
4387 fprintf(stderr, "-icount is not allowed with kvm or xen\n");
4390 configure_icount(icount_option);
4392 /* clean up network at qemu process termination */
4393 atexit(&net_cleanup);
4395 if (net_init_clients() < 0) {
4400 if (tpm_init() < 0) {
4405 /* init the bluetooth world */
4406 if (foreach_device_config(DEV_BT, bt_parse))
4409 if (!xen_enabled()) {
4410 /* On 32-bit hosts, QEMU is limited by virtual address space */
4411 if (ram_size > (2047 << 20) && HOST_LONG_BITS == 32) {
4412 fprintf(stderr, "qemu: at most 2047 MB RAM can be simulated\n");
4417 cpu_exec_init_all();
4421 /* open the virtual block devices */
4423 qemu_opts_foreach(qemu_find_opts("drive"), drive_enable_snapshot, NULL, 0);
4424 if (qemu_opts_foreach(qemu_find_opts("drive"), drive_init_func,
4425 &machine->block_default_type, 1) != 0) {
4429 default_drive(default_cdrom, snapshot, machine->block_default_type, 2,
4431 default_drive(default_floppy, snapshot, IF_FLOPPY, 0, FD_OPTS);
4432 default_drive(default_sdcard, snapshot, IF_SD, 0, SD_OPTS);
4434 register_savevm_live(NULL, "ram", 0, 4, &savevm_ram_handlers, NULL);
4436 if (nb_numa_nodes > 0) {
4439 if (nb_numa_nodes > MAX_NODES) {
4440 nb_numa_nodes = MAX_NODES;
4443 /* If no memory size if given for any node, assume the default case
4444 * and distribute the available memory equally across all nodes
4446 for (i = 0; i < nb_numa_nodes; i++) {
4447 if (node_mem[i] != 0)
4450 if (i == nb_numa_nodes) {
4451 uint64_t usedmem = 0;
4453 /* On Linux, the each node's border has to be 8MB aligned,
4454 * the final node gets the rest.
4456 for (i = 0; i < nb_numa_nodes - 1; i++) {
4457 node_mem[i] = (ram_size / nb_numa_nodes) & ~((1 << 23UL) - 1);
4458 usedmem += node_mem[i];
4460 node_mem[i] = ram_size - usedmem;
4463 for (i = 0; i < nb_numa_nodes; i++) {
4464 if (!bitmap_empty(node_cpumask[i], MAX_CPUMASK_BITS)) {
4468 /* assigning the VCPUs round-robin is easier to implement, guest OSes
4469 * must cope with this anyway, because there are BIOSes out there in
4470 * real machines which also use this scheme.
4472 if (i == nb_numa_nodes) {
4473 for (i = 0; i < max_cpus; i++) {
4474 set_bit(i, node_cpumask[i % nb_numa_nodes]);
4479 if (qemu_opts_foreach(qemu_find_opts("mon"), mon_init_func, NULL, 1) != 0) {
4483 if (foreach_device_config(DEV_SERIAL, serial_parse) < 0)
4485 if (foreach_device_config(DEV_PARALLEL, parallel_parse) < 0)
4487 if (foreach_device_config(DEV_VIRTCON, virtcon_parse) < 0)
4489 if (foreach_device_config(DEV_SCLP, sclp_parse) < 0) {
4492 if (foreach_device_config(DEV_DEBUGCON, debugcon_parse) < 0)
4495 /* If no default VGA is requested, the default is "none". */
4497 if (cirrus_vga_available()) {
4498 vga_model = "cirrus";
4499 } else if (vga_available()) {
4503 select_vgahw(vga_model);
4506 i = select_watchdog(watchdog);
4508 exit (i == 1 ? 1 : 0);
4511 if (machine->compat_props) {
4512 qdev_prop_register_global_list(machine->compat_props);
4516 qdev_machine_init();
4519 // Returned variable points different address from input variable.
4520 kernel_cmdline = prepare_maru_devices(kernel_cmdline);
4522 QEMUMachineInitArgs args = { .ram_size = ram_size,
4523 .boot_device = boot_order,
4524 .kernel_filename = kernel_filename,
4525 .kernel_cmdline = kernel_cmdline,
4526 .initrd_filename = initrd_filename,
4527 .cpu_model = cpu_model };
4528 machine->init(&args);
4530 // TODO: Check about it...
4533 cpu_synchronize_all_post_init();
4537 current_machine = machine;
4539 if (hax_enabled()) {
4543 /* init USB devices */
4544 if (usb_enabled(false)) {
4545 if (foreach_device_config(DEV_USB, usb_parse) < 0)
4549 /* init generic devices */
4550 if (qemu_opts_foreach(qemu_find_opts("device"), device_init_func, NULL, 1) != 0)
4553 net_check_clients();
4555 ds = init_displaystate();
4557 /* init local displays */
4558 switch (display_type) {
4561 #if defined(CONFIG_CURSES)
4563 curses_display_init(ds, full_screen);
4566 #if defined(CONFIG_MARU)
4568 maru_display_init(ds);
4570 if (skin_disabled == 1) {
4571 /* do not start skin client process */
4572 set_emul_skin_enable(0);
4574 set_emul_skin_enable(1);
4578 #if defined(CONFIG_SDL)
4580 sdl_display_init(ds, full_screen, no_frame);
4582 #elif defined(CONFIG_COCOA)
4584 cocoa_display_init(ds, full_screen);
4587 #if defined(CONFIG_GTK)
4589 gtk_display_init(ds, full_screen);
4596 /* must be after terminal init, SDL library changes signal handlers */
4597 os_setup_signal_handling();
4600 /* init remote displays */
4602 Error *local_err = NULL;
4603 vnc_display_init(ds);
4604 vnc_display_open(ds, vnc_display, &local_err);
4605 if (local_err != NULL) {
4606 fprintf(stderr, "Failed to start VNC server on `%s': %s\n",
4607 vnc_display, error_get_pretty(local_err));
4608 error_free(local_err);
4612 if (show_vnc_port) {
4613 printf("VNC server running on `%s'\n", vnc_display_local_addr(ds));
4618 if (using_spice && !spice_displays) {
4619 qemu_spice_display_init(ds);
4623 if (foreach_device_config(DEV_GDB, gdbserver_start) < 0) {
4627 qdev_machine_creation_done();
4629 if (rom_load_all() != 0) {
4630 fprintf(stderr, "rom loading failed\n");
4634 /* TODO: once all bus devices are qdevified, this should be done
4635 * when bus is created by qdev.c */
4636 qemu_register_reset(qbus_reset_all_fn, sysbus_get_default());
4637 qemu_run_machine_init_done_notifiers();
4639 qemu_system_reset(VMRESET_SILENT);
4641 if (load_vmstate(loadvm) < 0) {
4647 Error *local_err = NULL;
4648 qemu_start_incoming_migration(incoming, &local_err);
4650 fprintf(stderr, "-incoming %s: %s\n", incoming, error_get_pretty(local_err));
4651 error_free(local_err);
4654 } else if (autostart) {