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 "config-host.h"
34 #include "sysemu/seccomp.h"
37 #if defined(CONFIG_VDE)
38 #include <libvdeplug.h>
42 #if defined(__APPLE__) || defined(main)
44 int qemu_main(int argc
, char **argv
, char **envp
);
45 int main(int argc
, char **argv
)
47 return qemu_main(argc
, argv
, NULL
);
50 #define main qemu_main
52 #endif /* CONFIG_SDL */
56 #define main qemu_main
57 #endif /* CONFIG_COCOA */
61 #include "qemu/sockets.h"
63 #include "hw/boards.h"
64 #include "sysemu/accel.h"
66 #include "hw/i386/pc.h"
67 #include "hw/isa/isa.h"
69 #include "sysemu/watchdog.h"
70 #include "hw/i386/smbios.h"
71 #include "hw/xen/xen.h"
73 #include "hw/loader.h"
74 #include "monitor/qdev.h"
75 #include "sysemu/bt.h"
77 #include "net/slirp.h"
78 #include "monitor/monitor.h"
79 #include "ui/console.h"
80 #include "sysemu/sysemu.h"
81 #include "exec/gdbstub.h"
82 #include "qemu/timer.h"
83 #include "sysemu/char.h"
84 #include "qemu/bitmap.h"
85 #include "sysemu/blockdev.h"
86 #include "hw/block/block.h"
87 #include "migration/block.h"
88 #include "sysemu/tpm.h"
89 #include "sysemu/dma.h"
90 #include "audio/audio.h"
91 #include "migration/migration.h"
92 #include "sysemu/kvm.h"
93 #include "qapi/qmp/qjson.h"
94 #include "qemu/option.h"
95 #include "qemu/config-file.h"
96 #include "qemu-options.h"
97 #include "qmp-commands.h"
98 #include "qemu/main-loop.h"
100 #include "fsdev/qemu-fsdev.h"
102 #include "sysemu/qtest.h"
104 #include "disas/disas.h"
107 #include "slirp/libslirp.h"
110 #include "trace/control.h"
111 #include "qemu/queue.h"
112 #include "sysemu/cpus.h"
113 #include "sysemu/arch_init.h"
114 #include "qemu/osdep.h"
116 #include "ui/qemu-spice.h"
117 #include "qapi/string-input-visitor.h"
118 #include "qapi/opts-visitor.h"
119 #include "qom/object_interfaces.h"
120 #include "qapi-event.h"
122 #define DEFAULT_RAM_SIZE 128
124 #define MAX_VIRTIO_CONSOLES 1
125 #define MAX_SCLP_CONSOLES 1
127 static const char *data_dir
[16];
128 static int data_dir_idx
;
129 const char *bios_name
= NULL
;
130 enum vga_retrace_method vga_retrace_method
= VGA_RETRACE_DUMB
;
131 DisplayType display_type
= DT_DEFAULT
;
132 static int display_remote
;
133 const char* keyboard_layout
= NULL
;
135 const char *mem_path
= NULL
;
136 int mem_prealloc
= 0; /* force preallocation of physical target memory */
137 bool enable_mlock
= false;
139 NICInfo nd_table
[MAX_NICS
];
141 static int rtc_utc
= 1;
142 static int rtc_date_offset
= -1; /* -1 means no change */
143 QEMUClockType rtc_clock
;
144 int vga_interface_type
= VGA_NONE
;
145 static int full_screen
= 0;
146 static int no_frame
= 0;
149 static bool grab_on_hover
;
151 CharDriverState
*serial_hds
[MAX_SERIAL_PORTS
];
152 CharDriverState
*parallel_hds
[MAX_PARALLEL_PORTS
];
153 CharDriverState
*virtcon_hds
[MAX_VIRTIO_CONSOLES
];
154 CharDriverState
*sclp_hds
[MAX_SCLP_CONSOLES
];
155 int win2k_install_hack
= 0;
162 const char *vnc_display
;
164 int acpi_enabled
= 1;
167 static int no_reboot
;
170 int graphic_rotate
= 0;
171 const char *watchdog
;
172 QEMUOptionRom option_rom
[MAX_OPTION_ROMS
];
174 int semihosting_enabled
= 0;
176 const char *qemu_name
;
179 unsigned int nb_prom_envs
= 0;
180 const char *prom_envs
[MAX_PROM_ENVS
];
183 uint8_t *boot_splash_filedata
;
184 size_t boot_splash_filedata_size
;
185 uint8_t qemu_extra_params_fw
[2];
187 int icount_align_option
;
191 NodeInfo numa_info
[MAX_NODES
];
193 uint8_t qemu_uuid
[16];
196 static QEMUBootSetHandler
*boot_set_handler
;
197 static void *boot_set_opaque
;
199 static NotifierList exit_notifiers
=
200 NOTIFIER_LIST_INITIALIZER(exit_notifiers
);
202 static NotifierList machine_init_done_notifiers
=
203 NOTIFIER_LIST_INITIALIZER(machine_init_done_notifiers
);
207 enum xen_mode xen_mode
= XEN_EMULATE
;
209 static int has_defaults
= 1;
210 static int default_serial
= 1;
211 static int default_parallel
= 1;
212 static int default_virtcon
= 1;
213 static int default_sclp
= 1;
214 static int default_monitor
= 1;
215 static int default_floppy
= 1;
216 static int default_cdrom
= 1;
217 static int default_sdcard
= 1;
218 static int default_vga
= 1;
224 { .driver
= "isa-serial", .flag
= &default_serial
},
225 { .driver
= "isa-parallel", .flag
= &default_parallel
},
226 { .driver
= "isa-fdc", .flag
= &default_floppy
},
227 { .driver
= "ide-cd", .flag
= &default_cdrom
},
228 { .driver
= "ide-hd", .flag
= &default_cdrom
},
229 { .driver
= "ide-drive", .flag
= &default_cdrom
},
230 { .driver
= "scsi-cd", .flag
= &default_cdrom
},
231 { .driver
= "virtio-serial-pci", .flag
= &default_virtcon
},
232 { .driver
= "virtio-serial-s390", .flag
= &default_virtcon
},
233 { .driver
= "virtio-serial", .flag
= &default_virtcon
},
234 { .driver
= "VGA", .flag
= &default_vga
},
235 { .driver
= "isa-vga", .flag
= &default_vga
},
236 { .driver
= "cirrus-vga", .flag
= &default_vga
},
237 { .driver
= "isa-cirrus-vga", .flag
= &default_vga
},
238 { .driver
= "vmware-svga", .flag
= &default_vga
},
239 { .driver
= "qxl-vga", .flag
= &default_vga
},
242 static QemuOptsList qemu_rtc_opts
= {
244 .head
= QTAILQ_HEAD_INITIALIZER(qemu_rtc_opts
.head
),
248 .type
= QEMU_OPT_STRING
,
251 .type
= QEMU_OPT_STRING
,
254 .type
= QEMU_OPT_STRING
,
256 { /* end of list */ }
260 static QemuOptsList qemu_sandbox_opts
= {
262 .implied_opt_name
= "enable",
263 .head
= QTAILQ_HEAD_INITIALIZER(qemu_sandbox_opts
.head
),
267 .type
= QEMU_OPT_BOOL
,
269 { /* end of list */ }
273 static QemuOptsList qemu_trace_opts
= {
275 .implied_opt_name
= "trace",
276 .head
= QTAILQ_HEAD_INITIALIZER(qemu_trace_opts
.head
),
280 .type
= QEMU_OPT_STRING
,
283 .type
= QEMU_OPT_STRING
,
285 { /* end of list */ }
289 static QemuOptsList qemu_option_rom_opts
= {
290 .name
= "option-rom",
291 .implied_opt_name
= "romfile",
292 .head
= QTAILQ_HEAD_INITIALIZER(qemu_option_rom_opts
.head
),
296 .type
= QEMU_OPT_NUMBER
,
299 .type
= QEMU_OPT_STRING
,
301 { /* end of list */ }
305 static QemuOptsList qemu_machine_opts
= {
307 .implied_opt_name
= "type",
309 .head
= QTAILQ_HEAD_INITIALIZER(qemu_machine_opts
.head
),
313 .type
= QEMU_OPT_STRING
,
314 .help
= "emulated machine"
317 .type
= QEMU_OPT_STRING
,
318 .help
= "accelerator list",
320 .name
= "kernel_irqchip",
321 .type
= QEMU_OPT_BOOL
,
322 .help
= "use KVM in-kernel irqchip",
324 .name
= "kvm_shadow_mem",
325 .type
= QEMU_OPT_SIZE
,
326 .help
= "KVM shadow MMU size",
329 .type
= QEMU_OPT_STRING
,
330 .help
= "Linux kernel image file",
333 .type
= QEMU_OPT_STRING
,
334 .help
= "Linux initial ramdisk file",
337 .type
= QEMU_OPT_STRING
,
338 .help
= "Linux kernel command line",
341 .type
= QEMU_OPT_STRING
,
342 .help
= "Linux kernel device tree file",
345 .type
= QEMU_OPT_STRING
,
346 .help
= "Dump current dtb to a file and quit",
348 .name
= "phandle_start",
349 .type
= QEMU_OPT_NUMBER
,
350 .help
= "The first phandle ID we may generate dynamically",
352 .name
= "dt_compatible",
353 .type
= QEMU_OPT_STRING
,
354 .help
= "Overrides the \"compatible\" property of the dt root node",
356 .name
= "dump-guest-core",
357 .type
= QEMU_OPT_BOOL
,
358 .help
= "Include guest memory in a core dump",
361 .type
= QEMU_OPT_BOOL
,
362 .help
= "enable/disable memory merge support",
365 .type
= QEMU_OPT_BOOL
,
366 .help
= "Set on/off to enable/disable usb",
369 .type
= QEMU_OPT_STRING
,
370 .help
= "firmware image",
373 .type
= QEMU_OPT_STRING
,
374 .help
= "Specifies the KVM virtualization mode (HV, PR)",
376 .name
= PC_MACHINE_MAX_RAM_BELOW_4G
,
377 .type
= QEMU_OPT_SIZE
,
378 .help
= "maximum ram below the 4G boundary (32bit boundary)",
381 .type
= QEMU_OPT_BOOL
,
382 .help
= "Set on/off to enable/disable Intel IOMMU (VT-d)",
384 { /* End of list */ }
388 static QemuOptsList qemu_boot_opts
= {
390 .implied_opt_name
= "order",
392 .head
= QTAILQ_HEAD_INITIALIZER(qemu_boot_opts
.head
),
396 .type
= QEMU_OPT_STRING
,
399 .type
= QEMU_OPT_STRING
,
402 .type
= QEMU_OPT_BOOL
,
405 .type
= QEMU_OPT_STRING
,
407 .name
= "splash-time",
408 .type
= QEMU_OPT_STRING
,
410 .name
= "reboot-timeout",
411 .type
= QEMU_OPT_STRING
,
414 .type
= QEMU_OPT_BOOL
,
420 static QemuOptsList qemu_add_fd_opts
= {
422 .head
= QTAILQ_HEAD_INITIALIZER(qemu_add_fd_opts
.head
),
426 .type
= QEMU_OPT_NUMBER
,
427 .help
= "file descriptor of which a duplicate is added to fd set",
430 .type
= QEMU_OPT_NUMBER
,
431 .help
= "ID of the fd set to add fd to",
434 .type
= QEMU_OPT_STRING
,
435 .help
= "free-form string used to describe fd",
437 { /* end of list */ }
441 static QemuOptsList qemu_object_opts
= {
443 .implied_opt_name
= "qom-type",
444 .head
= QTAILQ_HEAD_INITIALIZER(qemu_object_opts
.head
),
450 static QemuOptsList qemu_tpmdev_opts
= {
452 .implied_opt_name
= "type",
453 .head
= QTAILQ_HEAD_INITIALIZER(qemu_tpmdev_opts
.head
),
455 /* options are defined in the TPM backends */
456 { /* end of list */ }
460 static QemuOptsList qemu_realtime_opts
= {
462 .head
= QTAILQ_HEAD_INITIALIZER(qemu_realtime_opts
.head
),
466 .type
= QEMU_OPT_BOOL
,
468 { /* end of list */ }
472 static QemuOptsList qemu_msg_opts
= {
474 .head
= QTAILQ_HEAD_INITIALIZER(qemu_msg_opts
.head
),
478 .type
= QEMU_OPT_BOOL
,
480 { /* end of list */ }
484 static QemuOptsList qemu_name_opts
= {
486 .implied_opt_name
= "guest",
488 .head
= QTAILQ_HEAD_INITIALIZER(qemu_name_opts
.head
),
492 .type
= QEMU_OPT_STRING
,
493 .help
= "Sets the name of the guest.\n"
494 "This name will be displayed in the SDL window caption.\n"
495 "The name will also be used for the VNC server",
498 .type
= QEMU_OPT_STRING
,
499 .help
= "Sets the name of the QEMU process, as shown in top etc",
501 .name
= "debug-threads",
502 .type
= QEMU_OPT_BOOL
,
503 .help
= "When enabled, name the individual threads; defaults off.\n"
504 "NOTE: The thread names are for debugging and not a\n"
507 { /* End of list */ }
511 static QemuOptsList qemu_mem_opts
= {
513 .implied_opt_name
= "size",
514 .head
= QTAILQ_HEAD_INITIALIZER(qemu_mem_opts
.head
),
519 .type
= QEMU_OPT_SIZE
,
523 .type
= QEMU_OPT_NUMBER
,
527 .type
= QEMU_OPT_SIZE
,
529 { /* end of list */ }
533 static QemuOptsList qemu_icount_opts
= {
535 .implied_opt_name
= "shift",
537 .head
= QTAILQ_HEAD_INITIALIZER(qemu_icount_opts
.head
),
541 .type
= QEMU_OPT_STRING
,
544 .type
= QEMU_OPT_BOOL
,
546 { /* end of list */ }
551 * Get machine options
553 * Returns: machine options (never null).
555 QemuOpts
*qemu_get_machine_opts(void)
557 return qemu_find_opts_singleton("machine");
560 const char *qemu_get_vm_name(void)
565 static void res_free(void)
567 if (boot_splash_filedata
!= NULL
) {
568 g_free(boot_splash_filedata
);
569 boot_splash_filedata
= NULL
;
573 static int default_driver_check(QemuOpts
*opts
, void *opaque
)
575 const char *driver
= qemu_opt_get(opts
, "driver");
580 for (i
= 0; i
< ARRAY_SIZE(default_list
); i
++) {
581 if (strcmp(default_list
[i
].driver
, driver
) != 0)
583 *(default_list
[i
].flag
) = 0;
588 /***********************************************************/
591 static RunState current_run_state
= RUN_STATE_PRELAUNCH
;
593 /* We use RUN_STATE_MAX but any invalid value will do */
594 static RunState vmstop_requested
= RUN_STATE_MAX
;
595 static QemuMutex vmstop_lock
;
600 } RunStateTransition
;
602 static const RunStateTransition runstate_transitions_def
[] = {
604 { RUN_STATE_DEBUG
, RUN_STATE_RUNNING
},
605 { RUN_STATE_DEBUG
, RUN_STATE_FINISH_MIGRATE
},
607 { RUN_STATE_INMIGRATE
, RUN_STATE_RUNNING
},
608 { RUN_STATE_INMIGRATE
, RUN_STATE_PAUSED
},
610 { RUN_STATE_INTERNAL_ERROR
, RUN_STATE_PAUSED
},
611 { RUN_STATE_INTERNAL_ERROR
, RUN_STATE_FINISH_MIGRATE
},
613 { RUN_STATE_IO_ERROR
, RUN_STATE_RUNNING
},
614 { RUN_STATE_IO_ERROR
, RUN_STATE_FINISH_MIGRATE
},
616 { RUN_STATE_PAUSED
, RUN_STATE_RUNNING
},
617 { RUN_STATE_PAUSED
, RUN_STATE_FINISH_MIGRATE
},
619 { RUN_STATE_POSTMIGRATE
, RUN_STATE_RUNNING
},
620 { RUN_STATE_POSTMIGRATE
, RUN_STATE_FINISH_MIGRATE
},
622 { RUN_STATE_PRELAUNCH
, RUN_STATE_RUNNING
},
623 { RUN_STATE_PRELAUNCH
, RUN_STATE_FINISH_MIGRATE
},
624 { RUN_STATE_PRELAUNCH
, RUN_STATE_INMIGRATE
},
626 { RUN_STATE_FINISH_MIGRATE
, RUN_STATE_RUNNING
},
627 { RUN_STATE_FINISH_MIGRATE
, RUN_STATE_POSTMIGRATE
},
629 { RUN_STATE_RESTORE_VM
, RUN_STATE_RUNNING
},
631 { RUN_STATE_RUNNING
, RUN_STATE_DEBUG
},
632 { RUN_STATE_RUNNING
, RUN_STATE_INTERNAL_ERROR
},
633 { RUN_STATE_RUNNING
, RUN_STATE_IO_ERROR
},
634 { RUN_STATE_RUNNING
, RUN_STATE_PAUSED
},
635 { RUN_STATE_RUNNING
, RUN_STATE_FINISH_MIGRATE
},
636 { RUN_STATE_RUNNING
, RUN_STATE_RESTORE_VM
},
637 { RUN_STATE_RUNNING
, RUN_STATE_SAVE_VM
},
638 { RUN_STATE_RUNNING
, RUN_STATE_SHUTDOWN
},
639 { RUN_STATE_RUNNING
, RUN_STATE_WATCHDOG
},
640 { RUN_STATE_RUNNING
, RUN_STATE_GUEST_PANICKED
},
642 { RUN_STATE_SAVE_VM
, RUN_STATE_RUNNING
},
644 { RUN_STATE_SHUTDOWN
, RUN_STATE_PAUSED
},
645 { RUN_STATE_SHUTDOWN
, RUN_STATE_FINISH_MIGRATE
},
647 { RUN_STATE_DEBUG
, RUN_STATE_SUSPENDED
},
648 { RUN_STATE_RUNNING
, RUN_STATE_SUSPENDED
},
649 { RUN_STATE_SUSPENDED
, RUN_STATE_RUNNING
},
650 { RUN_STATE_SUSPENDED
, RUN_STATE_FINISH_MIGRATE
},
652 { RUN_STATE_WATCHDOG
, RUN_STATE_RUNNING
},
653 { RUN_STATE_WATCHDOG
, RUN_STATE_FINISH_MIGRATE
},
655 { RUN_STATE_GUEST_PANICKED
, RUN_STATE_RUNNING
},
656 { RUN_STATE_GUEST_PANICKED
, RUN_STATE_FINISH_MIGRATE
},
658 { RUN_STATE_MAX
, RUN_STATE_MAX
},
661 static bool runstate_valid_transitions
[RUN_STATE_MAX
][RUN_STATE_MAX
];
663 bool runstate_check(RunState state
)
665 return current_run_state
== state
;
668 static void runstate_init(void)
670 const RunStateTransition
*p
;
672 memset(&runstate_valid_transitions
, 0, sizeof(runstate_valid_transitions
));
673 for (p
= &runstate_transitions_def
[0]; p
->from
!= RUN_STATE_MAX
; p
++) {
674 runstate_valid_transitions
[p
->from
][p
->to
] = true;
677 qemu_mutex_init(&vmstop_lock
);
680 /* This function will abort() on invalid state transitions */
681 void runstate_set(RunState new_state
)
683 assert(new_state
< RUN_STATE_MAX
);
685 if (!runstate_valid_transitions
[current_run_state
][new_state
]) {
686 fprintf(stderr
, "ERROR: invalid runstate transition: '%s' -> '%s'\n",
687 RunState_lookup
[current_run_state
],
688 RunState_lookup
[new_state
]);
691 trace_runstate_set(new_state
);
692 current_run_state
= new_state
;
695 int runstate_is_running(void)
697 return runstate_check(RUN_STATE_RUNNING
);
700 bool runstate_needs_reset(void)
702 return runstate_check(RUN_STATE_INTERNAL_ERROR
) ||
703 runstate_check(RUN_STATE_SHUTDOWN
);
706 StatusInfo
*qmp_query_status(Error
**errp
)
708 StatusInfo
*info
= g_malloc0(sizeof(*info
));
710 info
->running
= runstate_is_running();
711 info
->singlestep
= singlestep
;
712 info
->status
= current_run_state
;
717 static bool qemu_vmstop_requested(RunState
*r
)
719 qemu_mutex_lock(&vmstop_lock
);
720 *r
= vmstop_requested
;
721 vmstop_requested
= RUN_STATE_MAX
;
722 qemu_mutex_unlock(&vmstop_lock
);
723 return *r
< RUN_STATE_MAX
;
726 void qemu_system_vmstop_request_prepare(void)
728 qemu_mutex_lock(&vmstop_lock
);
731 void qemu_system_vmstop_request(RunState state
)
733 vmstop_requested
= state
;
734 qemu_mutex_unlock(&vmstop_lock
);
742 qemu_vmstop_requested(&requested
);
743 if (runstate_is_running() && requested
== RUN_STATE_MAX
) {
747 /* Ensure that a STOP/RESUME pair of events is emitted if a
748 * vmstop request was pending. The BLOCK_IO_ERROR event, for
749 * example, according to documentation is always followed by
752 if (runstate_is_running()) {
753 qapi_event_send_stop(&error_abort
);
756 runstate_set(RUN_STATE_RUNNING
);
757 vm_state_notify(1, RUN_STATE_RUNNING
);
761 qapi_event_send_resume(&error_abort
);
765 /***********************************************************/
766 /* real time host monotonic timer */
768 /***********************************************************/
769 /* host time/date access */
770 void qemu_get_timedate(struct tm
*tm
, int offset
)
776 if (rtc_date_offset
== -1) {
780 localtime_r(&ti
, tm
);
782 ti
-= rtc_date_offset
;
787 int qemu_timedate_diff(struct tm
*tm
)
791 if (rtc_date_offset
== -1)
793 seconds
= mktimegm(tm
);
796 tmp
.tm_isdst
= -1; /* use timezone to figure it out */
797 seconds
= mktime(&tmp
);
800 seconds
= mktimegm(tm
) + rtc_date_offset
;
802 return seconds
- time(NULL
);
805 static void configure_rtc_date_offset(const char *startdate
, int legacy
)
807 time_t rtc_start_date
;
810 if (!strcmp(startdate
, "now") && legacy
) {
811 rtc_date_offset
= -1;
813 if (sscanf(startdate
, "%d-%d-%dT%d:%d:%d",
821 } else if (sscanf(startdate
, "%d-%d-%d",
833 rtc_start_date
= mktimegm(&tm
);
834 if (rtc_start_date
== -1) {
836 fprintf(stderr
, "Invalid date format. Valid formats are:\n"
837 "'2006-06-17T16:01:21' or '2006-06-17'\n");
840 rtc_date_offset
= time(NULL
) - rtc_start_date
;
844 static void configure_rtc(QemuOpts
*opts
)
848 value
= qemu_opt_get(opts
, "base");
850 if (!strcmp(value
, "utc")) {
852 } else if (!strcmp(value
, "localtime")) {
855 configure_rtc_date_offset(value
, 0);
858 value
= qemu_opt_get(opts
, "clock");
860 if (!strcmp(value
, "host")) {
861 rtc_clock
= QEMU_CLOCK_HOST
;
862 } else if (!strcmp(value
, "rt")) {
863 rtc_clock
= QEMU_CLOCK_REALTIME
;
864 } else if (!strcmp(value
, "vm")) {
865 rtc_clock
= QEMU_CLOCK_VIRTUAL
;
867 fprintf(stderr
, "qemu: invalid option value '%s'\n", value
);
871 value
= qemu_opt_get(opts
, "driftfix");
873 if (!strcmp(value
, "slew")) {
874 static GlobalProperty slew_lost_ticks
[] = {
876 .driver
= "mc146818rtc",
877 .property
= "lost_tick_policy",
880 { /* end of list */ }
883 qdev_prop_register_global_list(slew_lost_ticks
);
884 } else if (!strcmp(value
, "none")) {
885 /* discard is default */
887 fprintf(stderr
, "qemu: invalid option value '%s'\n", value
);
893 /***********************************************************/
894 /* Bluetooth support */
897 static struct HCIInfo
*hci_table
[MAX_NICS
];
899 struct HCIInfo
*qemu_next_hci(void)
901 if (cur_hci
== nb_hcis
)
904 return hci_table
[cur_hci
++];
907 static int bt_hci_parse(const char *str
)
912 if (nb_hcis
>= MAX_NICS
) {
913 fprintf(stderr
, "qemu: Too many bluetooth HCIs (max %i).\n", MAX_NICS
);
926 bdaddr
.b
[5] = 0x56 + nb_hcis
;
927 hci
->bdaddr_set(hci
, bdaddr
.b
);
929 hci_table
[nb_hcis
++] = hci
;
934 static void bt_vhci_add(int vlan_id
)
936 struct bt_scatternet_s
*vlan
= qemu_find_bt_vlan(vlan_id
);
939 fprintf(stderr
, "qemu: warning: adding a VHCI to "
940 "an empty scatternet %i\n", vlan_id
);
942 bt_vhci_init(bt_new_hci(vlan
));
945 static struct bt_device_s
*bt_device_add(const char *opt
)
947 struct bt_scatternet_s
*vlan
;
949 char *endp
= strstr(opt
, ",vlan=");
950 int len
= (endp
? endp
- opt
: strlen(opt
)) + 1;
953 pstrcpy(devname
, MIN(sizeof(devname
), len
), opt
);
956 vlan_id
= strtol(endp
+ 6, &endp
, 0);
958 fprintf(stderr
, "qemu: unrecognised bluetooth vlan Id\n");
963 vlan
= qemu_find_bt_vlan(vlan_id
);
966 fprintf(stderr
, "qemu: warning: adding a slave device to "
967 "an empty scatternet %i\n", vlan_id
);
969 if (!strcmp(devname
, "keyboard"))
970 return bt_keyboard_init(vlan
);
972 fprintf(stderr
, "qemu: unsupported bluetooth device `%s'\n", devname
);
976 static int bt_parse(const char *opt
)
978 const char *endp
, *p
;
981 if (strstart(opt
, "hci", &endp
)) {
982 if (!*endp
|| *endp
== ',') {
984 if (!strstart(endp
, ",vlan=", 0))
987 return bt_hci_parse(opt
);
989 } else if (strstart(opt
, "vhci", &endp
)) {
990 if (!*endp
|| *endp
== ',') {
992 if (strstart(endp
, ",vlan=", &p
)) {
993 vlan
= strtol(p
, (char **) &endp
, 0);
995 fprintf(stderr
, "qemu: bad scatternet '%s'\n", p
);
999 fprintf(stderr
, "qemu: bad parameter '%s'\n", endp
+ 1);
1008 } else if (strstart(opt
, "device:", &endp
))
1009 return !bt_device_add(endp
);
1011 fprintf(stderr
, "qemu: bad bluetooth parameter '%s'\n", opt
);
1015 static int parse_sandbox(QemuOpts
*opts
, void *opaque
)
1017 /* FIXME: change this to true for 1.3 */
1018 if (qemu_opt_get_bool(opts
, "enable", false)) {
1019 #ifdef CONFIG_SECCOMP
1020 if (seccomp_start() < 0) {
1021 qerror_report(ERROR_CLASS_GENERIC_ERROR
,
1022 "failed to install seccomp syscall filter in the kernel");
1026 qerror_report(ERROR_CLASS_GENERIC_ERROR
,
1027 "sandboxing request but seccomp is not compiled into this build");
1035 static int parse_name(QemuOpts
*opts
, void *opaque
)
1037 const char *proc_name
;
1039 if (qemu_opt_get(opts
, "debug-threads")) {
1040 qemu_thread_naming(qemu_opt_get_bool(opts
, "debug-threads", false));
1042 qemu_name
= qemu_opt_get(opts
, "guest");
1044 proc_name
= qemu_opt_get(opts
, "process");
1046 os_set_proc_name(proc_name
);
1052 bool usb_enabled(bool default_usb
)
1054 return qemu_opt_get_bool(qemu_get_machine_opts(), "usb",
1055 has_defaults
&& default_usb
);
1059 static int parse_add_fd(QemuOpts
*opts
, void *opaque
)
1061 int fd
, dupfd
, flags
;
1063 const char *fd_opaque
= NULL
;
1065 fd
= qemu_opt_get_number(opts
, "fd", -1);
1066 fdset_id
= qemu_opt_get_number(opts
, "set", -1);
1067 fd_opaque
= qemu_opt_get(opts
, "opaque");
1070 qerror_report(ERROR_CLASS_GENERIC_ERROR
,
1071 "fd option is required and must be non-negative");
1075 if (fd
<= STDERR_FILENO
) {
1076 qerror_report(ERROR_CLASS_GENERIC_ERROR
,
1077 "fd cannot be a standard I/O stream");
1082 * All fds inherited across exec() necessarily have FD_CLOEXEC
1083 * clear, while qemu sets FD_CLOEXEC on all other fds used internally.
1085 flags
= fcntl(fd
, F_GETFD
);
1086 if (flags
== -1 || (flags
& FD_CLOEXEC
)) {
1087 qerror_report(ERROR_CLASS_GENERIC_ERROR
,
1088 "fd is not valid or already in use");
1093 qerror_report(ERROR_CLASS_GENERIC_ERROR
,
1094 "set option is required and must be non-negative");
1098 #ifdef F_DUPFD_CLOEXEC
1099 dupfd
= fcntl(fd
, F_DUPFD_CLOEXEC
, 0);
1103 qemu_set_cloexec(dupfd
);
1107 qerror_report(ERROR_CLASS_GENERIC_ERROR
,
1108 "Error duplicating fd: %s", strerror(errno
));
1112 /* add the duplicate fd, and optionally the opaque string, to the fd set */
1113 monitor_fdset_add_fd(dupfd
, true, fdset_id
, fd_opaque
? true : false,
1119 static int cleanup_add_fd(QemuOpts
*opts
, void *opaque
)
1123 fd
= qemu_opt_get_number(opts
, "fd", -1);
1130 /***********************************************************/
1131 /* QEMU Block devices */
1133 #define HD_OPTS "media=disk"
1134 #define CDROM_OPTS "media=cdrom"
1136 #define PFLASH_OPTS ""
1140 static int drive_init_func(QemuOpts
*opts
, void *opaque
)
1142 BlockInterfaceType
*block_default_type
= opaque
;
1144 return drive_new(opts
, *block_default_type
) == NULL
;
1147 static int drive_enable_snapshot(QemuOpts
*opts
, void *opaque
)
1149 if (qemu_opt_get(opts
, "snapshot") == NULL
) {
1150 qemu_opt_set(opts
, "snapshot", "on");
1155 static void default_drive(int enable
, int snapshot
, BlockInterfaceType type
,
1156 int index
, const char *optstr
)
1161 if (!enable
|| drive_get_by_index(type
, index
)) {
1165 opts
= drive_add(type
, index
, NULL
, optstr
);
1167 drive_enable_snapshot(opts
, NULL
);
1170 dinfo
= drive_new(opts
, type
);
1174 dinfo
->is_default
= true;
1178 void qemu_register_boot_set(QEMUBootSetHandler
*func
, void *opaque
)
1180 boot_set_handler
= func
;
1181 boot_set_opaque
= opaque
;
1184 int qemu_boot_set(const char *boot_order
)
1186 if (!boot_set_handler
) {
1189 return boot_set_handler(boot_set_opaque
, boot_order
);
1192 static void validate_bootdevices(const char *devices
)
1194 /* We just do some generic consistency checks */
1198 for (p
= devices
; *p
!= '\0'; p
++) {
1199 /* Allowed boot devices are:
1200 * a-b: floppy disk drives
1201 * c-f: IDE disk drives
1202 * g-m: machine implementation dependent drives
1203 * n-p: network devices
1204 * It's up to each machine implementation to check if the given boot
1205 * devices match the actual hardware implementation and firmware
1208 if (*p
< 'a' || *p
> 'p') {
1209 fprintf(stderr
, "Invalid boot device '%c'\n", *p
);
1212 if (bitmap
& (1 << (*p
- 'a'))) {
1213 fprintf(stderr
, "Boot device '%c' was given twice\n", *p
);
1216 bitmap
|= 1 << (*p
- 'a');
1220 static void restore_boot_order(void *opaque
)
1222 char *normal_boot_order
= opaque
;
1223 static int first
= 1;
1225 /* Restore boot order and remove ourselves after the first boot */
1231 qemu_boot_set(normal_boot_order
);
1233 qemu_unregister_reset(restore_boot_order
, normal_boot_order
);
1234 g_free(normal_boot_order
);
1237 static QemuOptsList qemu_smp_opts
= {
1239 .implied_opt_name
= "cpus",
1240 .merge_lists
= true,
1241 .head
= QTAILQ_HEAD_INITIALIZER(qemu_smp_opts
.head
),
1245 .type
= QEMU_OPT_NUMBER
,
1248 .type
= QEMU_OPT_NUMBER
,
1251 .type
= QEMU_OPT_NUMBER
,
1254 .type
= QEMU_OPT_NUMBER
,
1257 .type
= QEMU_OPT_NUMBER
,
1259 { /*End of list */ }
1263 static void smp_parse(QemuOpts
*opts
)
1267 unsigned cpus
= qemu_opt_get_number(opts
, "cpus", 0);
1268 unsigned sockets
= qemu_opt_get_number(opts
, "sockets", 0);
1269 unsigned cores
= qemu_opt_get_number(opts
, "cores", 0);
1270 unsigned threads
= qemu_opt_get_number(opts
, "threads", 0);
1272 /* compute missing values, prefer sockets over cores over threads */
1273 if (cpus
== 0 || sockets
== 0) {
1274 sockets
= sockets
> 0 ? sockets
: 1;
1275 cores
= cores
> 0 ? cores
: 1;
1276 threads
= threads
> 0 ? threads
: 1;
1278 cpus
= cores
* threads
* sockets
;
1282 threads
= threads
> 0 ? threads
: 1;
1283 cores
= cpus
/ (sockets
* threads
);
1285 threads
= cpus
/ (cores
* sockets
);
1289 max_cpus
= qemu_opt_get_number(opts
, "maxcpus", 0);
1292 smp_cores
= cores
> 0 ? cores
: 1;
1293 smp_threads
= threads
> 0 ? threads
: 1;
1297 if (max_cpus
== 0) {
1298 max_cpus
= smp_cpus
;
1301 if (max_cpus
> MAX_CPUMASK_BITS
) {
1302 fprintf(stderr
, "Unsupported number of maxcpus\n");
1305 if (max_cpus
< smp_cpus
) {
1306 fprintf(stderr
, "maxcpus must be equal to or greater than smp\n");
1312 static void realtime_init(void)
1315 if (os_mlock() < 0) {
1316 fprintf(stderr
, "qemu: locking memory failed\n");
1323 static void configure_msg(QemuOpts
*opts
)
1325 enable_timestamp_msg
= qemu_opt_get_bool(opts
, "timestamp", true);
1328 /***********************************************************/
1331 static int usb_device_add(const char *devname
)
1333 USBDevice
*dev
= NULL
;
1334 #ifndef CONFIG_LINUX
1338 if (!usb_enabled(false)) {
1342 /* drivers with .usbdevice_name entry in USBDeviceInfo */
1343 dev
= usbdevice_create(devname
);
1347 /* the other ones */
1348 #ifndef CONFIG_LINUX
1349 /* only the linux version is qdev-ified, usb-bsd still needs this */
1350 if (strstart(devname
, "host:", &p
)) {
1351 dev
= usb_host_device_open(usb_bus_find(-1), p
);
1361 static int usb_device_del(const char *devname
)
1366 if (strstart(devname
, "host:", &p
)) {
1370 if (!usb_enabled(false)) {
1374 p
= strchr(devname
, '.');
1377 bus_num
= strtoul(devname
, NULL
, 0);
1378 addr
= strtoul(p
+ 1, NULL
, 0);
1380 return usb_device_delete_addr(bus_num
, addr
);
1383 static int usb_parse(const char *cmdline
)
1386 r
= usb_device_add(cmdline
);
1388 fprintf(stderr
, "qemu: could not add USB device '%s'\n", cmdline
);
1393 void do_usb_add(Monitor
*mon
, const QDict
*qdict
)
1395 const char *devname
= qdict_get_str(qdict
, "devname");
1396 if (usb_device_add(devname
) < 0) {
1397 error_report("could not add USB device '%s'", devname
);
1401 void do_usb_del(Monitor
*mon
, const QDict
*qdict
)
1403 const char *devname
= qdict_get_str(qdict
, "devname");
1404 if (usb_device_del(devname
) < 0) {
1405 error_report("could not delete USB device '%s'", devname
);
1409 /***********************************************************/
1410 /* machine registration */
1412 MachineState
*current_machine
;
1414 static void machine_class_init(ObjectClass
*oc
, void *data
)
1416 MachineClass
*mc
= MACHINE_CLASS(oc
);
1417 QEMUMachine
*qm
= data
;
1419 mc
->name
= qm
->name
;
1420 mc
->alias
= qm
->alias
;
1421 mc
->desc
= qm
->desc
;
1422 mc
->init
= qm
->init
;
1423 mc
->reset
= qm
->reset
;
1424 mc
->hot_add_cpu
= qm
->hot_add_cpu
;
1425 mc
->kvm_type
= qm
->kvm_type
;
1426 mc
->block_default_type
= qm
->block_default_type
;
1427 mc
->units_per_default_bus
= qm
->units_per_default_bus
;
1428 mc
->max_cpus
= qm
->max_cpus
;
1429 mc
->no_serial
= qm
->no_serial
;
1430 mc
->no_parallel
= qm
->no_parallel
;
1431 mc
->use_virtcon
= qm
->use_virtcon
;
1432 mc
->use_sclp
= qm
->use_sclp
;
1433 mc
->no_floppy
= qm
->no_floppy
;
1434 mc
->no_cdrom
= qm
->no_cdrom
;
1435 mc
->no_sdcard
= qm
->no_sdcard
;
1436 mc
->is_default
= qm
->is_default
;
1437 mc
->default_machine_opts
= qm
->default_machine_opts
;
1438 mc
->default_boot_order
= qm
->default_boot_order
;
1439 mc
->compat_props
= qm
->compat_props
;
1440 mc
->hw_version
= qm
->hw_version
;
1443 int qemu_register_machine(QEMUMachine
*m
)
1445 char *name
= g_strconcat(m
->name
, TYPE_MACHINE_SUFFIX
, NULL
);
1448 .parent
= TYPE_MACHINE
,
1449 .class_init
= machine_class_init
,
1450 .class_data
= (void *)m
,
1459 static MachineClass
*find_machine(const char *name
)
1461 GSList
*el
, *machines
= object_class_get_list(TYPE_MACHINE
, false);
1462 MachineClass
*mc
= NULL
;
1464 for (el
= machines
; el
; el
= el
->next
) {
1465 MachineClass
*temp
= el
->data
;
1467 if (!strcmp(temp
->name
, name
)) {
1472 !strcmp(temp
->alias
, name
)) {
1478 g_slist_free(machines
);
1482 MachineClass
*find_default_machine(void)
1484 GSList
*el
, *machines
= object_class_get_list(TYPE_MACHINE
, false);
1485 MachineClass
*mc
= NULL
;
1487 for (el
= machines
; el
; el
= el
->next
) {
1488 MachineClass
*temp
= el
->data
;
1490 if (temp
->is_default
) {
1496 g_slist_free(machines
);
1500 MachineInfoList
*qmp_query_machines(Error
**errp
)
1502 GSList
*el
, *machines
= object_class_get_list(TYPE_MACHINE
, false);
1503 MachineInfoList
*mach_list
= NULL
;
1505 for (el
= machines
; el
; el
= el
->next
) {
1506 MachineClass
*mc
= el
->data
;
1507 MachineInfoList
*entry
;
1510 info
= g_malloc0(sizeof(*info
));
1511 if (mc
->is_default
) {
1512 info
->has_is_default
= true;
1513 info
->is_default
= true;
1517 info
->has_alias
= true;
1518 info
->alias
= g_strdup(mc
->alias
);
1521 info
->name
= g_strdup(mc
->name
);
1522 info
->cpu_max
= !mc
->max_cpus
? 1 : mc
->max_cpus
;
1524 entry
= g_malloc0(sizeof(*entry
));
1525 entry
->value
= info
;
1526 entry
->next
= mach_list
;
1530 g_slist_free(machines
);
1534 /***********************************************************/
1535 /* main execution loop */
1537 struct vm_change_state_entry
{
1538 VMChangeStateHandler
*cb
;
1540 QLIST_ENTRY (vm_change_state_entry
) entries
;
1543 static QLIST_HEAD(vm_change_state_head
, vm_change_state_entry
) vm_change_state_head
;
1545 VMChangeStateEntry
*qemu_add_vm_change_state_handler(VMChangeStateHandler
*cb
,
1548 VMChangeStateEntry
*e
;
1550 e
= g_malloc0(sizeof (*e
));
1554 QLIST_INSERT_HEAD(&vm_change_state_head
, e
, entries
);
1558 void qemu_del_vm_change_state_handler(VMChangeStateEntry
*e
)
1560 QLIST_REMOVE (e
, entries
);
1564 void vm_state_notify(int running
, RunState state
)
1566 VMChangeStateEntry
*e
, *next
;
1568 trace_vm_state_notify(running
, state
);
1570 QLIST_FOREACH_SAFE(e
, &vm_change_state_head
, entries
, next
) {
1571 e
->cb(e
->opaque
, running
, state
);
1575 /* reset/shutdown handler */
1577 typedef struct QEMUResetEntry
{
1578 QTAILQ_ENTRY(QEMUResetEntry
) entry
;
1579 QEMUResetHandler
*func
;
1583 static QTAILQ_HEAD(reset_handlers
, QEMUResetEntry
) reset_handlers
=
1584 QTAILQ_HEAD_INITIALIZER(reset_handlers
);
1585 static int reset_requested
;
1586 static int shutdown_requested
, shutdown_signal
= -1;
1587 static pid_t shutdown_pid
;
1588 static int powerdown_requested
;
1589 static int debug_requested
;
1590 static int suspend_requested
;
1591 static WakeupReason wakeup_reason
;
1592 static NotifierList powerdown_notifiers
=
1593 NOTIFIER_LIST_INITIALIZER(powerdown_notifiers
);
1594 static NotifierList suspend_notifiers
=
1595 NOTIFIER_LIST_INITIALIZER(suspend_notifiers
);
1596 static NotifierList wakeup_notifiers
=
1597 NOTIFIER_LIST_INITIALIZER(wakeup_notifiers
);
1598 static uint32_t wakeup_reason_mask
= ~(1 << QEMU_WAKEUP_REASON_NONE
);
1600 int qemu_shutdown_requested_get(void)
1602 return shutdown_requested
;
1605 int qemu_reset_requested_get(void)
1607 return reset_requested
;
1610 static int qemu_shutdown_requested(void)
1612 return atomic_xchg(&shutdown_requested
, 0);
1615 static void qemu_kill_report(void)
1617 if (!qtest_driver() && shutdown_signal
!= -1) {
1618 fprintf(stderr
, "qemu: terminating on signal %d", shutdown_signal
);
1619 if (shutdown_pid
== 0) {
1620 /* This happens for eg ^C at the terminal, so it's worth
1621 * avoiding printing an odd message in that case.
1623 fputc('\n', stderr
);
1625 fprintf(stderr
, " from pid " FMT_pid
"\n", shutdown_pid
);
1627 shutdown_signal
= -1;
1631 static int qemu_reset_requested(void)
1633 int r
= reset_requested
;
1634 reset_requested
= 0;
1638 static int qemu_suspend_requested(void)
1640 int r
= suspend_requested
;
1641 suspend_requested
= 0;
1645 static WakeupReason
qemu_wakeup_requested(void)
1647 return wakeup_reason
;
1650 static int qemu_powerdown_requested(void)
1652 int r
= powerdown_requested
;
1653 powerdown_requested
= 0;
1657 static int qemu_debug_requested(void)
1659 int r
= debug_requested
;
1660 debug_requested
= 0;
1664 void qemu_register_reset(QEMUResetHandler
*func
, void *opaque
)
1666 QEMUResetEntry
*re
= g_malloc0(sizeof(QEMUResetEntry
));
1669 re
->opaque
= opaque
;
1670 QTAILQ_INSERT_TAIL(&reset_handlers
, re
, entry
);
1673 void qemu_unregister_reset(QEMUResetHandler
*func
, void *opaque
)
1677 QTAILQ_FOREACH(re
, &reset_handlers
, entry
) {
1678 if (re
->func
== func
&& re
->opaque
== opaque
) {
1679 QTAILQ_REMOVE(&reset_handlers
, re
, entry
);
1686 void qemu_devices_reset(void)
1688 QEMUResetEntry
*re
, *nre
;
1690 /* reset all devices */
1691 QTAILQ_FOREACH_SAFE(re
, &reset_handlers
, entry
, nre
) {
1692 re
->func(re
->opaque
);
1696 void qemu_system_reset(bool report
)
1700 mc
= current_machine
? MACHINE_GET_CLASS(current_machine
) : NULL
;
1702 if (mc
&& mc
->reset
) {
1705 qemu_devices_reset();
1708 qapi_event_send_reset(&error_abort
);
1710 cpu_synchronize_all_post_reset();
1713 void qemu_system_reset_request(void)
1716 shutdown_requested
= 1;
1718 reset_requested
= 1;
1721 qemu_notify_event();
1724 static void qemu_system_suspend(void)
1727 notifier_list_notify(&suspend_notifiers
, NULL
);
1728 runstate_set(RUN_STATE_SUSPENDED
);
1729 qapi_event_send_suspend(&error_abort
);
1732 void qemu_system_suspend_request(void)
1734 if (runstate_check(RUN_STATE_SUSPENDED
)) {
1737 suspend_requested
= 1;
1739 qemu_notify_event();
1742 void qemu_register_suspend_notifier(Notifier
*notifier
)
1744 notifier_list_add(&suspend_notifiers
, notifier
);
1747 void qemu_system_wakeup_request(WakeupReason reason
)
1749 trace_system_wakeup_request(reason
);
1751 if (!runstate_check(RUN_STATE_SUSPENDED
)) {
1754 if (!(wakeup_reason_mask
& (1 << reason
))) {
1757 runstate_set(RUN_STATE_RUNNING
);
1758 wakeup_reason
= reason
;
1759 qemu_notify_event();
1762 void qemu_system_wakeup_enable(WakeupReason reason
, bool enabled
)
1765 wakeup_reason_mask
|= (1 << reason
);
1767 wakeup_reason_mask
&= ~(1 << reason
);
1771 void qemu_register_wakeup_notifier(Notifier
*notifier
)
1773 notifier_list_add(&wakeup_notifiers
, notifier
);
1776 void qemu_system_killed(int signal
, pid_t pid
)
1778 shutdown_signal
= signal
;
1781 qemu_system_shutdown_request();
1784 void qemu_system_shutdown_request(void)
1786 trace_qemu_system_shutdown_request();
1787 shutdown_requested
= 1;
1788 qemu_notify_event();
1791 static void qemu_system_powerdown(void)
1793 qapi_event_send_powerdown(&error_abort
);
1794 notifier_list_notify(&powerdown_notifiers
, NULL
);
1797 void qemu_system_powerdown_request(void)
1799 trace_qemu_system_powerdown_request();
1800 powerdown_requested
= 1;
1801 qemu_notify_event();
1804 void qemu_register_powerdown_notifier(Notifier
*notifier
)
1806 notifier_list_add(&powerdown_notifiers
, notifier
);
1809 void qemu_system_debug_request(void)
1811 debug_requested
= 1;
1812 qemu_notify_event();
1815 static bool main_loop_should_exit(void)
1818 if (qemu_debug_requested()) {
1819 vm_stop(RUN_STATE_DEBUG
);
1821 if (qemu_suspend_requested()) {
1822 qemu_system_suspend();
1824 if (qemu_shutdown_requested()) {
1826 qapi_event_send_shutdown(&error_abort
);
1828 vm_stop(RUN_STATE_SHUTDOWN
);
1833 if (qemu_reset_requested()) {
1835 cpu_synchronize_all_states();
1836 qemu_system_reset(VMRESET_REPORT
);
1838 if (runstate_needs_reset()) {
1839 runstate_set(RUN_STATE_PAUSED
);
1842 if (qemu_wakeup_requested()) {
1844 cpu_synchronize_all_states();
1845 qemu_system_reset(VMRESET_SILENT
);
1846 notifier_list_notify(&wakeup_notifiers
, &wakeup_reason
);
1847 wakeup_reason
= QEMU_WAKEUP_REASON_NONE
;
1849 qapi_event_send_wakeup(&error_abort
);
1851 if (qemu_powerdown_requested()) {
1852 qemu_system_powerdown();
1854 if (qemu_vmstop_requested(&r
)) {
1860 static void main_loop(void)
1864 #ifdef CONFIG_PROFILER
1868 nonblocking
= !kvm_enabled() && !xen_enabled() && last_io
> 0;
1869 #ifdef CONFIG_PROFILER
1870 ti
= profile_getclock();
1872 last_io
= main_loop_wait(nonblocking
);
1873 #ifdef CONFIG_PROFILER
1874 dev_time
+= profile_getclock() - ti
;
1876 } while (!main_loop_should_exit());
1879 static void version(void)
1881 printf("QEMU emulator version " QEMU_VERSION QEMU_PKGVERSION
", Copyright (c) 2003-2008 Fabrice Bellard\n");
1884 static void help(int exitcode
)
1887 printf("usage: %s [options] [disk_image]\n\n"
1888 "'disk_image' is a raw hard disk image for IDE hard disk 0\n\n",
1889 error_get_progname());
1891 #define QEMU_OPTIONS_GENERATE_HELP
1892 #include "qemu-options-wrapper.h"
1894 printf("\nDuring emulation, the following keys are useful:\n"
1895 "ctrl-alt-f toggle full screen\n"
1896 "ctrl-alt-n switch to virtual console 'n'\n"
1897 "ctrl-alt toggle mouse and keyboard grab\n"
1899 "When using -nographic, press 'ctrl-a h' to get some help.\n");
1904 #define HAS_ARG 0x0001
1906 typedef struct QEMUOption
{
1913 static const QEMUOption qemu_options
[] = {
1914 { "h", 0, QEMU_OPTION_h
, QEMU_ARCH_ALL
},
1915 #define QEMU_OPTIONS_GENERATE_OPTIONS
1916 #include "qemu-options-wrapper.h"
1920 static bool vga_available(void)
1922 return object_class_by_name("VGA") || object_class_by_name("isa-vga");
1925 static bool cirrus_vga_available(void)
1927 return object_class_by_name("cirrus-vga")
1928 || object_class_by_name("isa-cirrus-vga");
1931 static bool vmware_vga_available(void)
1933 return object_class_by_name("vmware-svga");
1936 static bool qxl_vga_available(void)
1938 return object_class_by_name("qxl-vga");
1941 static bool tcx_vga_available(void)
1943 return object_class_by_name("SUNW,tcx");
1946 static bool cg3_vga_available(void)
1948 return object_class_by_name("cgthree");
1951 static void select_vgahw (const char *p
)
1955 assert(vga_interface_type
== VGA_NONE
);
1956 if (strstart(p
, "std", &opts
)) {
1957 if (vga_available()) {
1958 vga_interface_type
= VGA_STD
;
1960 fprintf(stderr
, "Error: standard VGA not available\n");
1963 } else if (strstart(p
, "cirrus", &opts
)) {
1964 if (cirrus_vga_available()) {
1965 vga_interface_type
= VGA_CIRRUS
;
1967 fprintf(stderr
, "Error: Cirrus VGA not available\n");
1970 } else if (strstart(p
, "vmware", &opts
)) {
1971 if (vmware_vga_available()) {
1972 vga_interface_type
= VGA_VMWARE
;
1974 fprintf(stderr
, "Error: VMWare SVGA not available\n");
1977 } else if (strstart(p
, "xenfb", &opts
)) {
1978 vga_interface_type
= VGA_XENFB
;
1979 } else if (strstart(p
, "qxl", &opts
)) {
1980 if (qxl_vga_available()) {
1981 vga_interface_type
= VGA_QXL
;
1983 fprintf(stderr
, "Error: QXL VGA not available\n");
1986 } else if (strstart(p
, "tcx", &opts
)) {
1987 if (tcx_vga_available()) {
1988 vga_interface_type
= VGA_TCX
;
1990 fprintf(stderr
, "Error: TCX framebuffer not available\n");
1993 } else if (strstart(p
, "cg3", &opts
)) {
1994 if (cg3_vga_available()) {
1995 vga_interface_type
= VGA_CG3
;
1997 fprintf(stderr
, "Error: CG3 framebuffer not available\n");
2000 } else if (!strstart(p
, "none", &opts
)) {
2002 fprintf(stderr
, "Unknown vga type: %s\n", p
);
2006 const char *nextopt
;
2008 if (strstart(opts
, ",retrace=", &nextopt
)) {
2010 if (strstart(opts
, "dumb", &nextopt
))
2011 vga_retrace_method
= VGA_RETRACE_DUMB
;
2012 else if (strstart(opts
, "precise", &nextopt
))
2013 vga_retrace_method
= VGA_RETRACE_PRECISE
;
2014 else goto invalid_vga
;
2015 } else goto invalid_vga
;
2020 static DisplayType
select_display(const char *p
)
2023 DisplayType display
= DT_DEFAULT
;
2025 if (strstart(p
, "sdl", &opts
)) {
2029 const char *nextopt
;
2031 if (strstart(opts
, ",frame=", &nextopt
)) {
2033 if (strstart(opts
, "on", &nextopt
)) {
2035 } else if (strstart(opts
, "off", &nextopt
)) {
2038 goto invalid_sdl_args
;
2040 } else if (strstart(opts
, ",alt_grab=", &nextopt
)) {
2042 if (strstart(opts
, "on", &nextopt
)) {
2044 } else if (strstart(opts
, "off", &nextopt
)) {
2047 goto invalid_sdl_args
;
2049 } else if (strstart(opts
, ",ctrl_grab=", &nextopt
)) {
2051 if (strstart(opts
, "on", &nextopt
)) {
2053 } else if (strstart(opts
, "off", &nextopt
)) {
2056 goto invalid_sdl_args
;
2058 } else if (strstart(opts
, ",window_close=", &nextopt
)) {
2060 if (strstart(opts
, "on", &nextopt
)) {
2062 } else if (strstart(opts
, "off", &nextopt
)) {
2065 goto invalid_sdl_args
;
2069 fprintf(stderr
, "Invalid SDL option string: %s\n", p
);
2075 fprintf(stderr
, "SDL support is disabled\n");
2078 } else if (strstart(p
, "vnc", &opts
)) {
2083 const char *nextopt
;
2085 if (strstart(opts
, "=", &nextopt
)) {
2086 vnc_display
= nextopt
;
2090 fprintf(stderr
, "VNC requires a display argument vnc=<display>\n");
2094 fprintf(stderr
, "VNC support is disabled\n");
2097 } else if (strstart(p
, "curses", &opts
)) {
2098 #ifdef CONFIG_CURSES
2099 display
= DT_CURSES
;
2101 fprintf(stderr
, "Curses support is disabled\n");
2104 } else if (strstart(p
, "gtk", &opts
)) {
2108 const char *nextopt
;
2110 if (strstart(opts
, ",grab_on_hover=", &nextopt
)) {
2112 if (strstart(opts
, "on", &nextopt
)) {
2113 grab_on_hover
= true;
2114 } else if (strstart(opts
, "off", &nextopt
)) {
2115 grab_on_hover
= false;
2117 goto invalid_gtk_args
;
2121 fprintf(stderr
, "Invalid GTK option string: %s\n", p
);
2127 fprintf(stderr
, "GTK support is disabled\n");
2130 } else if (strstart(p
, "none", &opts
)) {
2133 fprintf(stderr
, "Unknown display type: %s\n", p
);
2140 static int balloon_parse(const char *arg
)
2144 if (strcmp(arg
, "none") == 0) {
2148 if (!strncmp(arg
, "virtio", 6)) {
2149 if (arg
[6] == ',') {
2150 /* have params -> parse them */
2151 opts
= qemu_opts_parse(qemu_find_opts("device"), arg
+7, 0);
2155 /* create empty opts */
2156 opts
= qemu_opts_create(qemu_find_opts("device"), NULL
, 0,
2159 qemu_opt_set(opts
, "driver", "virtio-balloon");
2166 char *qemu_find_file(int type
, const char *name
)
2172 /* Try the name as a straight path first */
2173 if (access(name
, R_OK
) == 0) {
2174 trace_load_file(name
, name
);
2175 return g_strdup(name
);
2179 case QEMU_FILE_TYPE_BIOS
:
2182 case QEMU_FILE_TYPE_KEYMAP
:
2183 subdir
= "keymaps/";
2189 for (i
= 0; i
< data_dir_idx
; i
++) {
2190 buf
= g_strdup_printf("%s/%s%s", data_dir
[i
], subdir
, name
);
2191 if (access(buf
, R_OK
) == 0) {
2192 trace_load_file(name
, buf
);
2200 static int device_help_func(QemuOpts
*opts
, void *opaque
)
2202 return qdev_device_help(opts
);
2205 static int device_init_func(QemuOpts
*opts
, void *opaque
)
2209 dev
= qdev_device_add(opts
);
2212 object_unref(OBJECT(dev
));
2216 static int chardev_init_func(QemuOpts
*opts
, void *opaque
)
2218 Error
*local_err
= NULL
;
2220 qemu_chr_new_from_opts(opts
, NULL
, &local_err
);
2222 error_report("%s", error_get_pretty(local_err
));
2223 error_free(local_err
);
2229 #ifdef CONFIG_VIRTFS
2230 static int fsdev_init_func(QemuOpts
*opts
, void *opaque
)
2233 ret
= qemu_fsdev_add(opts
);
2239 static int mon_init_func(QemuOpts
*opts
, void *opaque
)
2241 CharDriverState
*chr
;
2242 const char *chardev
;
2246 mode
= qemu_opt_get(opts
, "mode");
2250 if (strcmp(mode
, "readline") == 0) {
2251 flags
= MONITOR_USE_READLINE
;
2252 } else if (strcmp(mode
, "control") == 0) {
2253 flags
= MONITOR_USE_CONTROL
;
2255 fprintf(stderr
, "unknown monitor mode \"%s\"\n", mode
);
2259 if (qemu_opt_get_bool(opts
, "pretty", 0))
2260 flags
|= MONITOR_USE_PRETTY
;
2262 if (qemu_opt_get_bool(opts
, "default", 0))
2263 flags
|= MONITOR_IS_DEFAULT
;
2265 chardev
= qemu_opt_get(opts
, "chardev");
2266 chr
= qemu_chr_find(chardev
);
2268 fprintf(stderr
, "chardev \"%s\" not found\n", chardev
);
2272 qemu_chr_fe_claim_no_fail(chr
);
2273 monitor_init(chr
, flags
);
2277 static void monitor_parse(const char *optarg
, const char *mode
)
2279 static int monitor_device_index
= 0;
2285 if (strstart(optarg
, "chardev:", &p
)) {
2286 snprintf(label
, sizeof(label
), "%s", p
);
2288 snprintf(label
, sizeof(label
), "compat_monitor%d",
2289 monitor_device_index
);
2290 if (monitor_device_index
== 0) {
2293 opts
= qemu_chr_parse_compat(label
, optarg
);
2295 fprintf(stderr
, "parse error: %s\n", optarg
);
2300 opts
= qemu_opts_create(qemu_find_opts("mon"), label
, 1, NULL
);
2302 fprintf(stderr
, "duplicate chardev: %s\n", label
);
2305 qemu_opt_set(opts
, "mode", mode
);
2306 qemu_opt_set(opts
, "chardev", label
);
2308 qemu_opt_set(opts
, "default", "on");
2309 monitor_device_index
++;
2312 struct device_config
{
2314 DEV_USB
, /* -usbdevice */
2316 DEV_SERIAL
, /* -serial */
2317 DEV_PARALLEL
, /* -parallel */
2318 DEV_VIRTCON
, /* -virtioconsole */
2319 DEV_DEBUGCON
, /* -debugcon */
2320 DEV_GDB
, /* -gdb, -s */
2321 DEV_SCLP
, /* s390 sclp */
2323 const char *cmdline
;
2325 QTAILQ_ENTRY(device_config
) next
;
2328 static QTAILQ_HEAD(, device_config
) device_configs
=
2329 QTAILQ_HEAD_INITIALIZER(device_configs
);
2331 static void add_device_config(int type
, const char *cmdline
)
2333 struct device_config
*conf
;
2335 conf
= g_malloc0(sizeof(*conf
));
2337 conf
->cmdline
= cmdline
;
2338 loc_save(&conf
->loc
);
2339 QTAILQ_INSERT_TAIL(&device_configs
, conf
, next
);
2342 static int foreach_device_config(int type
, int (*func
)(const char *cmdline
))
2344 struct device_config
*conf
;
2347 QTAILQ_FOREACH(conf
, &device_configs
, next
) {
2348 if (conf
->type
!= type
)
2350 loc_push_restore(&conf
->loc
);
2351 rc
= func(conf
->cmdline
);
2352 loc_pop(&conf
->loc
);
2360 static int serial_parse(const char *devname
)
2362 static int index
= 0;
2365 if (strcmp(devname
, "none") == 0)
2367 if (index
== MAX_SERIAL_PORTS
) {
2368 fprintf(stderr
, "qemu: too many serial ports\n");
2371 snprintf(label
, sizeof(label
), "serial%d", index
);
2372 serial_hds
[index
] = qemu_chr_new(label
, devname
, NULL
);
2373 if (!serial_hds
[index
]) {
2374 fprintf(stderr
, "qemu: could not connect serial device"
2375 " to character backend '%s'\n", devname
);
2382 static int parallel_parse(const char *devname
)
2384 static int index
= 0;
2387 if (strcmp(devname
, "none") == 0)
2389 if (index
== MAX_PARALLEL_PORTS
) {
2390 fprintf(stderr
, "qemu: too many parallel ports\n");
2393 snprintf(label
, sizeof(label
), "parallel%d", index
);
2394 parallel_hds
[index
] = qemu_chr_new(label
, devname
, NULL
);
2395 if (!parallel_hds
[index
]) {
2396 fprintf(stderr
, "qemu: could not connect parallel device"
2397 " to character backend '%s'\n", devname
);
2404 static int virtcon_parse(const char *devname
)
2406 QemuOptsList
*device
= qemu_find_opts("device");
2407 static int index
= 0;
2409 QemuOpts
*bus_opts
, *dev_opts
;
2411 if (strcmp(devname
, "none") == 0)
2413 if (index
== MAX_VIRTIO_CONSOLES
) {
2414 fprintf(stderr
, "qemu: too many virtio consoles\n");
2418 bus_opts
= qemu_opts_create(device
, NULL
, 0, &error_abort
);
2419 if (arch_type
== QEMU_ARCH_S390X
) {
2420 qemu_opt_set(bus_opts
, "driver", "virtio-serial-s390");
2422 qemu_opt_set(bus_opts
, "driver", "virtio-serial-pci");
2425 dev_opts
= qemu_opts_create(device
, NULL
, 0, &error_abort
);
2426 qemu_opt_set(dev_opts
, "driver", "virtconsole");
2428 snprintf(label
, sizeof(label
), "virtcon%d", index
);
2429 virtcon_hds
[index
] = qemu_chr_new(label
, devname
, NULL
);
2430 if (!virtcon_hds
[index
]) {
2431 fprintf(stderr
, "qemu: could not connect virtio console"
2432 " to character backend '%s'\n", devname
);
2435 qemu_opt_set(dev_opts
, "chardev", label
);
2441 static int sclp_parse(const char *devname
)
2443 QemuOptsList
*device
= qemu_find_opts("device");
2444 static int index
= 0;
2448 if (strcmp(devname
, "none") == 0) {
2451 if (index
== MAX_SCLP_CONSOLES
) {
2452 fprintf(stderr
, "qemu: too many sclp consoles\n");
2456 assert(arch_type
== QEMU_ARCH_S390X
);
2458 dev_opts
= qemu_opts_create(device
, NULL
, 0, NULL
);
2459 qemu_opt_set(dev_opts
, "driver", "sclpconsole");
2461 snprintf(label
, sizeof(label
), "sclpcon%d", index
);
2462 sclp_hds
[index
] = qemu_chr_new(label
, devname
, NULL
);
2463 if (!sclp_hds
[index
]) {
2464 fprintf(stderr
, "qemu: could not connect sclp console"
2465 " to character backend '%s'\n", devname
);
2468 qemu_opt_set(dev_opts
, "chardev", label
);
2474 static int debugcon_parse(const char *devname
)
2478 if (!qemu_chr_new("debugcon", devname
, NULL
)) {
2481 opts
= qemu_opts_create(qemu_find_opts("device"), "debugcon", 1, NULL
);
2483 fprintf(stderr
, "qemu: already have a debugcon device\n");
2486 qemu_opt_set(opts
, "driver", "isa-debugcon");
2487 qemu_opt_set(opts
, "chardev", "debugcon");
2491 static MachineClass
*machine_parse(const char *name
)
2493 MachineClass
*mc
= NULL
;
2494 GSList
*el
, *machines
= object_class_get_list(TYPE_MACHINE
, false);
2497 mc
= find_machine(name
);
2502 if (name
&& !is_help_option(name
)) {
2503 error_report("Unsupported machine type");
2504 error_printf("Use -machine help to list supported machines!\n");
2506 printf("Supported machines are:\n");
2507 for (el
= machines
; el
; el
= el
->next
) {
2508 MachineClass
*mc
= el
->data
;
2510 printf("%-20s %s (alias of %s)\n", mc
->alias
, mc
->desc
, mc
->name
);
2512 printf("%-20s %s%s\n", mc
->name
, mc
->desc
,
2513 mc
->is_default
? " (default)" : "");
2517 g_slist_free(machines
);
2518 exit(!name
|| !is_help_option(name
));
2521 void qemu_add_exit_notifier(Notifier
*notify
)
2523 notifier_list_add(&exit_notifiers
, notify
);
2526 void qemu_remove_exit_notifier(Notifier
*notify
)
2528 notifier_remove(notify
);
2531 static void qemu_run_exit_notifiers(void)
2533 notifier_list_notify(&exit_notifiers
, NULL
);
2536 void qemu_add_machine_init_done_notifier(Notifier
*notify
)
2538 notifier_list_add(&machine_init_done_notifiers
, notify
);
2541 static void qemu_run_machine_init_done_notifiers(void)
2543 notifier_list_notify(&machine_init_done_notifiers
, NULL
);
2546 static const QEMUOption
*lookup_opt(int argc
, char **argv
,
2547 const char **poptarg
, int *poptind
)
2549 const QEMUOption
*popt
;
2550 int optind
= *poptind
;
2551 char *r
= argv
[optind
];
2554 loc_set_cmdline(argv
, optind
, 1);
2556 /* Treat --foo the same as -foo. */
2559 popt
= qemu_options
;
2562 error_report("invalid option");
2565 if (!strcmp(popt
->name
, r
+ 1))
2569 if (popt
->flags
& HAS_ARG
) {
2570 if (optind
>= argc
) {
2571 error_report("requires an argument");
2574 optarg
= argv
[optind
++];
2575 loc_set_cmdline(argv
, optind
- 2, 2);
2586 static gpointer
malloc_and_trace(gsize n_bytes
)
2588 void *ptr
= malloc(n_bytes
);
2589 trace_g_malloc(n_bytes
, ptr
);
2593 static gpointer
realloc_and_trace(gpointer mem
, gsize n_bytes
)
2595 void *ptr
= realloc(mem
, n_bytes
);
2596 trace_g_realloc(mem
, n_bytes
, ptr
);
2600 static void free_and_trace(gpointer mem
)
2606 static int machine_set_property(const char *name
, const char *value
,
2609 Object
*obj
= OBJECT(opaque
);
2610 StringInputVisitor
*siv
;
2611 Error
*local_err
= NULL
;
2614 if (strcmp(name
, "type") == 0) {
2618 qom_name
= g_strdup(name
);
2626 siv
= string_input_visitor_new(value
);
2627 object_property_set(obj
, string_input_get_visitor(siv
), qom_name
, &local_err
);
2628 string_input_visitor_cleanup(siv
);
2632 qerror_report_err(local_err
);
2633 error_free(local_err
);
2640 static int object_create(QemuOpts
*opts
, void *opaque
)
2649 ov
= opts_visitor_new(opts
);
2650 pdict
= qemu_opts_to_qdict(opts
, NULL
);
2652 visit_start_struct(opts_get_visitor(ov
), &dummy
, NULL
, NULL
, 0, &err
);
2657 qdict_del(pdict
, "qom-type");
2658 visit_type_str(opts_get_visitor(ov
), &type
, "qom-type", &err
);
2663 qdict_del(pdict
, "id");
2664 visit_type_str(opts_get_visitor(ov
), &id
, "id", &err
);
2669 object_add(type
, id
, pdict
, opts_get_visitor(ov
), &err
);
2673 visit_end_struct(opts_get_visitor(ov
), &err
);
2675 qmp_object_del(id
, NULL
);
2679 opts_visitor_cleanup(ov
);
2686 qerror_report_err(err
);
2693 int main(int argc
, char **argv
, char **envp
)
2696 int snapshot
, linux_boot
;
2697 const char *initrd_filename
;
2698 const char *kernel_filename
, *kernel_cmdline
;
2699 const char *boot_order
;
2701 int cyls
, heads
, secs
, translation
;
2702 QemuOpts
*hda_opts
= NULL
, *opts
, *machine_opts
, *icount_opts
= NULL
;
2703 QemuOptsList
*olist
;
2706 const char *loadvm
= NULL
;
2707 MachineClass
*machine_class
;
2708 const char *cpu_model
;
2709 const char *vga_model
= NULL
;
2710 const char *qtest_chrdev
= NULL
;
2711 const char *qtest_log
= NULL
;
2712 const char *pid_file
= NULL
;
2713 const char *incoming
= NULL
;
2715 int show_vnc_port
= 0;
2717 bool defconfig
= true;
2718 bool userconfig
= true;
2719 const char *log_mask
= NULL
;
2720 const char *log_file
= NULL
;
2721 GMemVTable mem_trace
= {
2722 .malloc
= malloc_and_trace
,
2723 .realloc
= realloc_and_trace
,
2724 .free
= free_and_trace
,
2726 const char *trace_events
= NULL
;
2727 const char *trace_file
= NULL
;
2728 const ram_addr_t default_ram_size
= (ram_addr_t
)DEFAULT_RAM_SIZE
*
2730 ram_addr_t maxram_size
= default_ram_size
;
2731 uint64_t ram_slots
= 0;
2732 FILE *vmstate_dump_file
= NULL
;
2733 Error
*main_loop_err
= NULL
;
2735 atexit(qemu_run_exit_notifiers
);
2736 error_set_progname(argv
[0]);
2737 qemu_init_exec_dir(argv
[0]);
2739 g_mem_set_vtable(&mem_trace
);
2741 module_call_init(MODULE_INIT_QOM
);
2743 qemu_add_opts(&qemu_drive_opts
);
2744 qemu_add_drive_opts(&qemu_legacy_drive_opts
);
2745 qemu_add_drive_opts(&qemu_common_drive_opts
);
2746 qemu_add_drive_opts(&qemu_drive_opts
);
2747 qemu_add_opts(&qemu_chardev_opts
);
2748 qemu_add_opts(&qemu_device_opts
);
2749 qemu_add_opts(&qemu_netdev_opts
);
2750 qemu_add_opts(&qemu_net_opts
);
2751 qemu_add_opts(&qemu_rtc_opts
);
2752 qemu_add_opts(&qemu_global_opts
);
2753 qemu_add_opts(&qemu_mon_opts
);
2754 qemu_add_opts(&qemu_trace_opts
);
2755 qemu_add_opts(&qemu_option_rom_opts
);
2756 qemu_add_opts(&qemu_machine_opts
);
2757 qemu_add_opts(&qemu_mem_opts
);
2758 qemu_add_opts(&qemu_smp_opts
);
2759 qemu_add_opts(&qemu_boot_opts
);
2760 qemu_add_opts(&qemu_sandbox_opts
);
2761 qemu_add_opts(&qemu_add_fd_opts
);
2762 qemu_add_opts(&qemu_object_opts
);
2763 qemu_add_opts(&qemu_tpmdev_opts
);
2764 qemu_add_opts(&qemu_realtime_opts
);
2765 qemu_add_opts(&qemu_msg_opts
);
2766 qemu_add_opts(&qemu_name_opts
);
2767 qemu_add_opts(&qemu_numa_opts
);
2768 qemu_add_opts(&qemu_icount_opts
);
2772 rtc_clock
= QEMU_CLOCK_HOST
;
2774 QLIST_INIT (&vm_change_state_head
);
2775 os_setup_early_signal_handling();
2777 module_call_init(MODULE_INIT_MACHINE
);
2778 machine_class
= find_default_machine();
2780 ram_size
= default_ram_size
;
2782 cyls
= heads
= secs
= 0;
2783 translation
= BIOS_ATA_TRANSLATION_AUTO
;
2785 for (i
= 0; i
< MAX_NODES
; i
++) {
2786 numa_info
[i
].node_mem
= 0;
2787 numa_info
[i
].present
= false;
2788 bitmap_zero(numa_info
[i
].node_cpu
, MAX_CPUMASK_BITS
);
2792 max_numa_nodeid
= 0;
2795 bdrv_init_with_whitelist();
2799 /* first pass of option parsing */
2801 while (optind
< argc
) {
2802 if (argv
[optind
][0] != '-') {
2806 const QEMUOption
*popt
;
2808 popt
= lookup_opt(argc
, argv
, &optarg
, &optind
);
2809 switch (popt
->index
) {
2810 case QEMU_OPTION_nodefconfig
:
2813 case QEMU_OPTION_nouserconfig
:
2822 ret
= qemu_read_default_config_files(userconfig
);
2828 /* second pass of option parsing */
2833 if (argv
[optind
][0] != '-') {
2834 hda_opts
= drive_add(IF_DEFAULT
, 0, argv
[optind
++], HD_OPTS
);
2836 const QEMUOption
*popt
;
2838 popt
= lookup_opt(argc
, argv
, &optarg
, &optind
);
2839 if (!(popt
->arch_mask
& arch_type
)) {
2840 printf("Option %s not supported for this target\n", popt
->name
);
2843 switch(popt
->index
) {
2845 machine_class
= machine_parse(optarg
);
2847 case QEMU_OPTION_no_kvm_irqchip
: {
2848 olist
= qemu_find_opts("machine");
2849 qemu_opts_parse(olist
, "kernel_irqchip=off", 0);
2852 case QEMU_OPTION_cpu
:
2853 /* hw initialization will check this */
2856 case QEMU_OPTION_hda
:
2860 snprintf(buf
, sizeof(buf
), "%s", HD_OPTS
);
2862 snprintf(buf
, sizeof(buf
),
2863 "%s,cyls=%d,heads=%d,secs=%d%s",
2864 HD_OPTS
, cyls
, heads
, secs
,
2865 translation
== BIOS_ATA_TRANSLATION_LBA
?
2867 translation
== BIOS_ATA_TRANSLATION_NONE
?
2868 ",trans=none" : "");
2869 drive_add(IF_DEFAULT
, 0, optarg
, buf
);
2872 case QEMU_OPTION_hdb
:
2873 case QEMU_OPTION_hdc
:
2874 case QEMU_OPTION_hdd
:
2875 drive_add(IF_DEFAULT
, popt
->index
- QEMU_OPTION_hda
, optarg
,
2878 case QEMU_OPTION_drive
:
2879 if (drive_def(optarg
) == NULL
) {
2883 case QEMU_OPTION_set
:
2884 if (qemu_set_option(optarg
) != 0)
2887 case QEMU_OPTION_global
:
2888 if (qemu_global_option(optarg
) != 0)
2891 case QEMU_OPTION_mtdblock
:
2892 drive_add(IF_MTD
, -1, optarg
, MTD_OPTS
);
2894 case QEMU_OPTION_sd
:
2895 drive_add(IF_SD
, -1, optarg
, SD_OPTS
);
2897 case QEMU_OPTION_pflash
:
2898 drive_add(IF_PFLASH
, -1, optarg
, PFLASH_OPTS
);
2900 case QEMU_OPTION_snapshot
:
2903 case QEMU_OPTION_hdachs
:
2907 cyls
= strtol(p
, (char **)&p
, 0);
2908 if (cyls
< 1 || cyls
> 16383)
2913 heads
= strtol(p
, (char **)&p
, 0);
2914 if (heads
< 1 || heads
> 16)
2919 secs
= strtol(p
, (char **)&p
, 0);
2920 if (secs
< 1 || secs
> 63)
2924 if (!strcmp(p
, "large")) {
2925 translation
= BIOS_ATA_TRANSLATION_LARGE
;
2926 } else if (!strcmp(p
, "rechs")) {
2927 translation
= BIOS_ATA_TRANSLATION_RECHS
;
2928 } else if (!strcmp(p
, "none")) {
2929 translation
= BIOS_ATA_TRANSLATION_NONE
;
2930 } else if (!strcmp(p
, "lba")) {
2931 translation
= BIOS_ATA_TRANSLATION_LBA
;
2932 } else if (!strcmp(p
, "auto")) {
2933 translation
= BIOS_ATA_TRANSLATION_AUTO
;
2937 } else if (*p
!= '\0') {
2939 fprintf(stderr
, "qemu: invalid physical CHS format\n");
2942 if (hda_opts
!= NULL
) {
2944 snprintf(num
, sizeof(num
), "%d", cyls
);
2945 qemu_opt_set(hda_opts
, "cyls", num
);
2946 snprintf(num
, sizeof(num
), "%d", heads
);
2947 qemu_opt_set(hda_opts
, "heads", num
);
2948 snprintf(num
, sizeof(num
), "%d", secs
);
2949 qemu_opt_set(hda_opts
, "secs", num
);
2950 if (translation
== BIOS_ATA_TRANSLATION_LARGE
) {
2951 qemu_opt_set(hda_opts
, "trans", "large");
2952 } else if (translation
== BIOS_ATA_TRANSLATION_RECHS
) {
2953 qemu_opt_set(hda_opts
, "trans", "rechs");
2954 } else if (translation
== BIOS_ATA_TRANSLATION_LBA
) {
2955 qemu_opt_set(hda_opts
, "trans", "lba");
2956 } else if (translation
== BIOS_ATA_TRANSLATION_NONE
) {
2957 qemu_opt_set(hda_opts
, "trans", "none");
2962 case QEMU_OPTION_numa
:
2963 opts
= qemu_opts_parse(qemu_find_opts("numa"), optarg
, 1);
2968 case QEMU_OPTION_display
:
2969 display_type
= select_display(optarg
);
2971 case QEMU_OPTION_nographic
:
2972 display_type
= DT_NOGRAPHIC
;
2974 case QEMU_OPTION_curses
:
2975 #ifdef CONFIG_CURSES
2976 display_type
= DT_CURSES
;
2978 fprintf(stderr
, "Curses support is disabled\n");
2982 case QEMU_OPTION_portrait
:
2983 graphic_rotate
= 90;
2985 case QEMU_OPTION_rotate
:
2986 graphic_rotate
= strtol(optarg
, (char **) &optarg
, 10);
2987 if (graphic_rotate
!= 0 && graphic_rotate
!= 90 &&
2988 graphic_rotate
!= 180 && graphic_rotate
!= 270) {
2990 "qemu: only 90, 180, 270 deg rotation is available\n");
2994 case QEMU_OPTION_kernel
:
2995 qemu_opts_set(qemu_find_opts("machine"), 0, "kernel", optarg
);
2997 case QEMU_OPTION_initrd
:
2998 qemu_opts_set(qemu_find_opts("machine"), 0, "initrd", optarg
);
3000 case QEMU_OPTION_append
:
3001 qemu_opts_set(qemu_find_opts("machine"), 0, "append", optarg
);
3003 case QEMU_OPTION_dtb
:
3004 qemu_opts_set(qemu_find_opts("machine"), 0, "dtb", optarg
);
3006 case QEMU_OPTION_cdrom
:
3007 drive_add(IF_DEFAULT
, 2, optarg
, CDROM_OPTS
);
3009 case QEMU_OPTION_boot
:
3010 opts
= qemu_opts_parse(qemu_find_opts("boot-opts"), optarg
, 1);
3015 case QEMU_OPTION_fda
:
3016 case QEMU_OPTION_fdb
:
3017 drive_add(IF_FLOPPY
, popt
->index
- QEMU_OPTION_fda
,
3020 case QEMU_OPTION_no_fd_bootchk
:
3023 case QEMU_OPTION_netdev
:
3024 if (net_client_parse(qemu_find_opts("netdev"), optarg
) == -1) {
3028 case QEMU_OPTION_net
:
3029 if (net_client_parse(qemu_find_opts("net"), optarg
) == -1) {
3033 #ifdef CONFIG_LIBISCSI
3034 case QEMU_OPTION_iscsi
:
3035 opts
= qemu_opts_parse(qemu_find_opts("iscsi"), optarg
, 0);
3042 case QEMU_OPTION_tftp
:
3043 legacy_tftp_prefix
= optarg
;
3045 case QEMU_OPTION_bootp
:
3046 legacy_bootp_filename
= optarg
;
3048 case QEMU_OPTION_redir
:
3049 if (net_slirp_redir(optarg
) < 0)
3053 case QEMU_OPTION_bt
:
3054 add_device_config(DEV_BT
, optarg
);
3056 case QEMU_OPTION_audio_help
:
3060 case QEMU_OPTION_soundhw
:
3061 select_soundhw (optarg
);
3066 case QEMU_OPTION_version
:
3070 case QEMU_OPTION_m
: {
3072 const char *mem_str
;
3073 const char *maxmem_str
, *slots_str
;
3075 opts
= qemu_opts_parse(qemu_find_opts("memory"),
3081 mem_str
= qemu_opt_get(opts
, "size");
3083 error_report("invalid -m option, missing 'size' option");
3087 error_report("missing 'size' option value");
3091 sz
= qemu_opt_get_size(opts
, "size", ram_size
);
3093 /* Fix up legacy suffix-less format */
3094 if (g_ascii_isdigit(mem_str
[strlen(mem_str
) - 1])) {
3095 uint64_t overflow_check
= sz
;
3098 if ((sz
>> 20) != overflow_check
) {
3099 error_report("too large 'size' option value");
3104 /* backward compatibility behaviour for case "-m 0" */
3106 sz
= default_ram_size
;
3109 sz
= QEMU_ALIGN_UP(sz
, 8192);
3111 if (ram_size
!= sz
) {
3112 error_report("ram size too large");
3115 maxram_size
= ram_size
;
3117 maxmem_str
= qemu_opt_get(opts
, "maxmem");
3118 slots_str
= qemu_opt_get(opts
, "slots");
3119 if (maxmem_str
&& slots_str
) {
3122 sz
= qemu_opt_get_size(opts
, "maxmem", 0);
3123 if (sz
< ram_size
) {
3124 error_report("invalid -m option value: maxmem "
3125 "(0x%" PRIx64
") <= initial memory (0x"
3126 RAM_ADDR_FMT
")", sz
, ram_size
);
3130 slots
= qemu_opt_get_number(opts
, "slots", 0);
3131 if ((sz
> ram_size
) && !slots
) {
3132 error_report("invalid -m option value: maxmem "
3133 "(0x%" PRIx64
") more than initial memory (0x"
3134 RAM_ADDR_FMT
") but no hotplug slots where "
3135 "specified", sz
, ram_size
);
3139 if ((sz
<= ram_size
) && slots
) {
3140 error_report("invalid -m option value: %"
3141 PRIu64
" hotplug slots where specified but "
3142 "maxmem (0x%" PRIx64
") <= initial memory (0x"
3143 RAM_ADDR_FMT
")", slots
, sz
, ram_size
);
3148 } else if ((!maxmem_str
&& slots_str
) ||
3149 (maxmem_str
&& !slots_str
)) {
3150 error_report("invalid -m option value: missing "
3151 "'%s' option", slots_str
? "maxmem" : "slots");
3157 case QEMU_OPTION_tpmdev
:
3158 if (tpm_config_parse(qemu_find_opts("tpmdev"), optarg
) < 0) {
3163 case QEMU_OPTION_mempath
:
3166 case QEMU_OPTION_mem_prealloc
:
3176 add_device_config(DEV_GDB
, "tcp::" DEFAULT_GDBSTUB_PORT
);
3178 case QEMU_OPTION_gdb
:
3179 add_device_config(DEV_GDB
, optarg
);
3182 if (data_dir_idx
< ARRAY_SIZE(data_dir
)) {
3183 data_dir
[data_dir_idx
++] = optarg
;
3186 case QEMU_OPTION_bios
:
3187 qemu_opts_set(qemu_find_opts("machine"), 0, "firmware", optarg
);
3189 case QEMU_OPTION_singlestep
:
3196 keyboard_layout
= optarg
;
3198 case QEMU_OPTION_localtime
:
3201 case QEMU_OPTION_vga
:
3210 w
= strtol(p
, (char **)&p
, 10);
3213 fprintf(stderr
, "qemu: invalid resolution or depth\n");
3219 h
= strtol(p
, (char **)&p
, 10);
3224 depth
= strtol(p
, (char **)&p
, 10);
3225 if (depth
!= 8 && depth
!= 15 && depth
!= 16 &&
3226 depth
!= 24 && depth
!= 32)
3228 } else if (*p
== '\0') {
3229 depth
= graphic_depth
;
3236 graphic_depth
= depth
;
3239 case QEMU_OPTION_echr
:
3242 term_escape_char
= strtol(optarg
, &r
, 0);
3244 printf("Bad argument to echr\n");
3247 case QEMU_OPTION_monitor
:
3248 default_monitor
= 0;
3249 if (strncmp(optarg
, "none", 4)) {
3250 monitor_parse(optarg
, "readline");
3253 case QEMU_OPTION_qmp
:
3254 monitor_parse(optarg
, "control");
3255 default_monitor
= 0;
3257 case QEMU_OPTION_mon
:
3258 opts
= qemu_opts_parse(qemu_find_opts("mon"), optarg
, 1);
3262 default_monitor
= 0;
3264 case QEMU_OPTION_chardev
:
3265 opts
= qemu_opts_parse(qemu_find_opts("chardev"), optarg
, 1);
3270 case QEMU_OPTION_fsdev
:
3271 olist
= qemu_find_opts("fsdev");
3273 fprintf(stderr
, "fsdev is not supported by this qemu build.\n");
3276 opts
= qemu_opts_parse(olist
, optarg
, 1);
3281 case QEMU_OPTION_virtfs
: {
3284 const char *writeout
, *sock_fd
, *socket
;
3286 olist
= qemu_find_opts("virtfs");
3288 fprintf(stderr
, "virtfs is not supported by this qemu build.\n");
3291 opts
= qemu_opts_parse(olist
, optarg
, 1);
3296 if (qemu_opt_get(opts
, "fsdriver") == NULL
||
3297 qemu_opt_get(opts
, "mount_tag") == NULL
) {
3298 fprintf(stderr
, "Usage: -virtfs fsdriver,mount_tag=tag.\n");
3301 fsdev
= qemu_opts_create(qemu_find_opts("fsdev"),
3302 qemu_opt_get(opts
, "mount_tag"),
3305 fprintf(stderr
, "duplicate fsdev id: %s\n",
3306 qemu_opt_get(opts
, "mount_tag"));
3310 writeout
= qemu_opt_get(opts
, "writeout");
3312 #ifdef CONFIG_SYNC_FILE_RANGE
3313 qemu_opt_set(fsdev
, "writeout", writeout
);
3315 fprintf(stderr
, "writeout=immediate not supported on "
3320 qemu_opt_set(fsdev
, "fsdriver", qemu_opt_get(opts
, "fsdriver"));
3321 qemu_opt_set(fsdev
, "path", qemu_opt_get(opts
, "path"));
3322 qemu_opt_set(fsdev
, "security_model",
3323 qemu_opt_get(opts
, "security_model"));
3324 socket
= qemu_opt_get(opts
, "socket");
3326 qemu_opt_set(fsdev
, "socket", socket
);
3328 sock_fd
= qemu_opt_get(opts
, "sock_fd");
3330 qemu_opt_set(fsdev
, "sock_fd", sock_fd
);
3333 qemu_opt_set_bool(fsdev
, "readonly",
3334 qemu_opt_get_bool(opts
, "readonly", 0));
3335 device
= qemu_opts_create(qemu_find_opts("device"), NULL
, 0,
3337 qemu_opt_set(device
, "driver", "virtio-9p-pci");
3338 qemu_opt_set(device
, "fsdev",
3339 qemu_opt_get(opts
, "mount_tag"));
3340 qemu_opt_set(device
, "mount_tag",
3341 qemu_opt_get(opts
, "mount_tag"));
3344 case QEMU_OPTION_virtfs_synth
: {
3348 fsdev
= qemu_opts_create(qemu_find_opts("fsdev"), "v_synth",
3351 fprintf(stderr
, "duplicate option: %s\n", "virtfs_synth");
3354 qemu_opt_set(fsdev
, "fsdriver", "synth");
3356 device
= qemu_opts_create(qemu_find_opts("device"), NULL
, 0,
3358 qemu_opt_set(device
, "driver", "virtio-9p-pci");
3359 qemu_opt_set(device
, "fsdev", "v_synth");
3360 qemu_opt_set(device
, "mount_tag", "v_synth");
3363 case QEMU_OPTION_serial
:
3364 add_device_config(DEV_SERIAL
, optarg
);
3366 if (strncmp(optarg
, "mon:", 4) == 0) {
3367 default_monitor
= 0;
3370 case QEMU_OPTION_watchdog
:
3373 "qemu: only one watchdog option may be given\n");
3378 case QEMU_OPTION_watchdog_action
:
3379 if (select_watchdog_action(optarg
) == -1) {
3380 fprintf(stderr
, "Unknown -watchdog-action parameter\n");
3384 case QEMU_OPTION_virtiocon
:
3385 add_device_config(DEV_VIRTCON
, optarg
);
3386 default_virtcon
= 0;
3387 if (strncmp(optarg
, "mon:", 4) == 0) {
3388 default_monitor
= 0;
3391 case QEMU_OPTION_parallel
:
3392 add_device_config(DEV_PARALLEL
, optarg
);
3393 default_parallel
= 0;
3394 if (strncmp(optarg
, "mon:", 4) == 0) {
3395 default_monitor
= 0;
3398 case QEMU_OPTION_debugcon
:
3399 add_device_config(DEV_DEBUGCON
, optarg
);
3401 case QEMU_OPTION_loadvm
:
3404 case QEMU_OPTION_full_screen
:
3407 case QEMU_OPTION_no_frame
:
3410 case QEMU_OPTION_alt_grab
:
3413 case QEMU_OPTION_ctrl_grab
:
3416 case QEMU_OPTION_no_quit
:
3419 case QEMU_OPTION_sdl
:
3421 display_type
= DT_SDL
;
3424 fprintf(stderr
, "SDL support is disabled\n");
3427 case QEMU_OPTION_pidfile
:
3430 case QEMU_OPTION_win2k_hack
:
3431 win2k_install_hack
= 1;
3433 case QEMU_OPTION_rtc_td_hack
: {
3434 static GlobalProperty slew_lost_ticks
[] = {
3436 .driver
= "mc146818rtc",
3437 .property
= "lost_tick_policy",
3440 { /* end of list */ }
3443 qdev_prop_register_global_list(slew_lost_ticks
);
3446 case QEMU_OPTION_acpitable
:
3447 opts
= qemu_opts_parse(qemu_find_opts("acpi"), optarg
, 1);
3451 do_acpitable_option(opts
);
3453 case QEMU_OPTION_smbios
:
3454 opts
= qemu_opts_parse(qemu_find_opts("smbios"), optarg
, 0);
3458 do_smbios_option(opts
);
3460 case QEMU_OPTION_enable_kvm
:
3461 olist
= qemu_find_opts("machine");
3462 qemu_opts_parse(olist
, "accel=kvm", 0);
3464 case QEMU_OPTION_machine
:
3465 olist
= qemu_find_opts("machine");
3466 opts
= qemu_opts_parse(olist
, optarg
, 1);
3470 optarg
= qemu_opt_get(opts
, "type");
3472 machine_class
= machine_parse(optarg
);
3475 case QEMU_OPTION_no_kvm
:
3476 olist
= qemu_find_opts("machine");
3477 qemu_opts_parse(olist
, "accel=tcg", 0);
3479 case QEMU_OPTION_no_kvm_pit
: {
3480 fprintf(stderr
, "Warning: KVM PIT can no longer be disabled "
3484 case QEMU_OPTION_no_kvm_pit_reinjection
: {
3485 static GlobalProperty kvm_pit_lost_tick_policy
[] = {
3487 .driver
= "kvm-pit",
3488 .property
= "lost_tick_policy",
3491 { /* end of list */ }
3494 fprintf(stderr
, "Warning: option deprecated, use "
3495 "lost_tick_policy property of kvm-pit instead.\n");
3496 qdev_prop_register_global_list(kvm_pit_lost_tick_policy
);
3499 case QEMU_OPTION_usb
:
3500 olist
= qemu_find_opts("machine");
3501 qemu_opts_parse(olist
, "usb=on", 0);
3503 case QEMU_OPTION_usbdevice
:
3504 olist
= qemu_find_opts("machine");
3505 qemu_opts_parse(olist
, "usb=on", 0);
3506 add_device_config(DEV_USB
, optarg
);
3508 case QEMU_OPTION_device
:
3509 if (!qemu_opts_parse(qemu_find_opts("device"), optarg
, 1)) {
3513 case QEMU_OPTION_smp
:
3514 if (!qemu_opts_parse(qemu_find_opts("smp-opts"), optarg
, 1)) {
3518 case QEMU_OPTION_vnc
:
3521 vnc_display
= optarg
;
3523 fprintf(stderr
, "VNC support is disabled\n");
3527 case QEMU_OPTION_no_acpi
:
3530 case QEMU_OPTION_no_hpet
:
3533 case QEMU_OPTION_balloon
:
3534 if (balloon_parse(optarg
) < 0) {
3535 fprintf(stderr
, "Unknown -balloon argument %s\n", optarg
);
3539 case QEMU_OPTION_no_reboot
:
3542 case QEMU_OPTION_no_shutdown
:
3545 case QEMU_OPTION_show_cursor
:
3548 case QEMU_OPTION_uuid
:
3549 if(qemu_uuid_parse(optarg
, qemu_uuid
) < 0) {
3550 fprintf(stderr
, "Fail to parse UUID string."
3551 " Wrong format.\n");
3554 qemu_uuid_set
= true;
3556 case QEMU_OPTION_option_rom
:
3557 if (nb_option_roms
>= MAX_OPTION_ROMS
) {
3558 fprintf(stderr
, "Too many option ROMs\n");
3561 opts
= qemu_opts_parse(qemu_find_opts("option-rom"), optarg
, 1);
3565 option_rom
[nb_option_roms
].name
= qemu_opt_get(opts
, "romfile");
3566 option_rom
[nb_option_roms
].bootindex
=
3567 qemu_opt_get_number(opts
, "bootindex", -1);
3568 if (!option_rom
[nb_option_roms
].name
) {
3569 fprintf(stderr
, "Option ROM file is not specified\n");
3574 case QEMU_OPTION_semihosting
:
3575 semihosting_enabled
= 1;
3577 case QEMU_OPTION_tdf
:
3578 fprintf(stderr
, "Warning: user space PIT time drift fix "
3579 "is no longer supported.\n");
3581 case QEMU_OPTION_name
:
3582 opts
= qemu_opts_parse(qemu_find_opts("name"), optarg
, 1);
3587 case QEMU_OPTION_prom_env
:
3588 if (nb_prom_envs
>= MAX_PROM_ENVS
) {
3589 fprintf(stderr
, "Too many prom variables\n");
3592 prom_envs
[nb_prom_envs
] = optarg
;
3595 case QEMU_OPTION_old_param
:
3598 case QEMU_OPTION_clock
:
3599 /* Clock options no longer exist. Keep this option for
3600 * backward compatibility.
3603 case QEMU_OPTION_startdate
:
3604 configure_rtc_date_offset(optarg
, 1);
3606 case QEMU_OPTION_rtc
:
3607 opts
= qemu_opts_parse(qemu_find_opts("rtc"), optarg
, 0);
3611 configure_rtc(opts
);
3613 case QEMU_OPTION_tb_size
:
3614 tcg_tb_size
= strtol(optarg
, NULL
, 0);
3615 if (tcg_tb_size
< 0) {
3619 case QEMU_OPTION_icount
:
3620 icount_opts
= qemu_opts_parse(qemu_find_opts("icount"),
3626 case QEMU_OPTION_incoming
:
3628 runstate_set(RUN_STATE_INMIGRATE
);
3630 case QEMU_OPTION_nodefaults
:
3633 case QEMU_OPTION_xen_domid
:
3634 if (!(xen_available())) {
3635 printf("Option %s not supported for this target\n", popt
->name
);
3638 xen_domid
= atoi(optarg
);
3640 case QEMU_OPTION_xen_create
:
3641 if (!(xen_available())) {
3642 printf("Option %s not supported for this target\n", popt
->name
);
3645 xen_mode
= XEN_CREATE
;
3647 case QEMU_OPTION_xen_attach
:
3648 if (!(xen_available())) {
3649 printf("Option %s not supported for this target\n", popt
->name
);
3652 xen_mode
= XEN_ATTACH
;
3654 case QEMU_OPTION_trace
:
3656 opts
= qemu_opts_parse(qemu_find_opts("trace"), optarg
, 0);
3660 trace_events
= qemu_opt_get(opts
, "events");
3661 trace_file
= qemu_opt_get(opts
, "file");
3664 case QEMU_OPTION_readconfig
:
3666 int ret
= qemu_read_config_file(optarg
);
3668 fprintf(stderr
, "read config %s: %s\n", optarg
,
3674 case QEMU_OPTION_spice
:
3675 olist
= qemu_find_opts("spice");
3677 fprintf(stderr
, "spice is not supported by this qemu build.\n");
3680 opts
= qemu_opts_parse(olist
, optarg
, 0);
3686 case QEMU_OPTION_writeconfig
:
3689 if (strcmp(optarg
, "-") == 0) {
3692 fp
= fopen(optarg
, "w");
3694 fprintf(stderr
, "open %s: %s\n", optarg
, strerror(errno
));
3698 qemu_config_write(fp
);
3704 case QEMU_OPTION_qtest
:
3705 qtest_chrdev
= optarg
;
3707 case QEMU_OPTION_qtest_log
:
3710 case QEMU_OPTION_sandbox
:
3711 opts
= qemu_opts_parse(qemu_find_opts("sandbox"), optarg
, 1);
3716 case QEMU_OPTION_add_fd
:
3718 opts
= qemu_opts_parse(qemu_find_opts("add-fd"), optarg
, 0);
3723 error_report("File descriptor passing is disabled on this "
3728 case QEMU_OPTION_object
:
3729 opts
= qemu_opts_parse(qemu_find_opts("object"), optarg
, 1);
3734 case QEMU_OPTION_realtime
:
3735 opts
= qemu_opts_parse(qemu_find_opts("realtime"), optarg
, 0);
3739 enable_mlock
= qemu_opt_get_bool(opts
, "mlock", true);
3741 case QEMU_OPTION_msg
:
3742 opts
= qemu_opts_parse(qemu_find_opts("msg"), optarg
, 0);
3746 configure_msg(opts
);
3748 case QEMU_OPTION_dump_vmstate
:
3749 vmstate_dump_file
= fopen(optarg
, "w");
3750 if (vmstate_dump_file
== NULL
) {
3751 fprintf(stderr
, "open %s: %s\n", optarg
, strerror(errno
));
3756 os_parse_cmd_args(popt
->index
, optarg
);
3764 if (qemu_init_main_loop(&main_loop_err
)) {
3765 error_report("%s", error_get_pretty(main_loop_err
));
3769 if (qemu_opts_foreach(qemu_find_opts("sandbox"), parse_sandbox
, NULL
, 0)) {
3773 if (qemu_opts_foreach(qemu_find_opts("name"), parse_name
, NULL
, 1)) {
3778 if (qemu_opts_foreach(qemu_find_opts("add-fd"), parse_add_fd
, NULL
, 1)) {
3782 if (qemu_opts_foreach(qemu_find_opts("add-fd"), cleanup_add_fd
, NULL
, 1)) {
3787 if (machine_class
== NULL
) {
3788 fprintf(stderr
, "No machine specified, and there is no default.\n"
3789 "Use -machine help to list supported machines!\n");
3793 current_machine
= MACHINE(object_new(object_class_get_name(
3794 OBJECT_CLASS(machine_class
))));
3795 object_property_add_child(object_get_root(), "machine",
3796 OBJECT(current_machine
), &error_abort
);
3797 cpu_exec_init_all();
3799 if (machine_class
->hw_version
) {
3800 qemu_set_version(machine_class
->hw_version
);
3803 /* Init CPU def lists, based on config
3804 * - Must be called after all the qemu_read_config_file() calls
3805 * - Must be called before list_cpus()
3806 * - Must be called before machine->init()
3810 if (cpu_model
&& is_help_option(cpu_model
)) {
3811 list_cpus(stdout
, &fprintf
, cpu_model
);
3815 /* Open the logfile at this point, if necessary. We can't open the logfile
3816 * when encountering either of the logging options (-d or -D) because the
3817 * other one may be encountered later on the command line, changing the
3818 * location or level of logging.
3823 qemu_set_log_filename(log_file
);
3826 mask
= qemu_str_to_log_mask(log_mask
);
3828 qemu_print_log_usage(stdout
);
3834 if (!is_daemonized()) {
3835 if (!trace_init_backends(trace_events
, trace_file
)) {
3840 /* If no data_dir is specified then try to find it relative to the
3842 if (data_dir_idx
< ARRAY_SIZE(data_dir
)) {
3843 data_dir
[data_dir_idx
] = os_find_datadir();
3844 if (data_dir
[data_dir_idx
] != NULL
) {
3848 /* If all else fails use the install path specified when building. */
3849 if (data_dir_idx
< ARRAY_SIZE(data_dir
)) {
3850 data_dir
[data_dir_idx
++] = CONFIG_QEMU_DATADIR
;
3853 smp_parse(qemu_opts_find(qemu_find_opts("smp-opts"), NULL
));
3855 machine_class
->max_cpus
= machine_class
->max_cpus
?: 1; /* Default to UP */
3856 if (smp_cpus
> machine_class
->max_cpus
) {
3857 fprintf(stderr
, "Number of SMP cpus requested (%d), exceeds max cpus "
3858 "supported by machine `%s' (%d)\n", smp_cpus
,
3859 machine_class
->name
, machine_class
->max_cpus
);
3864 * Get the default machine options from the machine if it is not already
3865 * specified either by the configuration file or by the command line.
3867 if (machine_class
->default_machine_opts
) {
3868 qemu_opts_set_defaults(qemu_find_opts("machine"),
3869 machine_class
->default_machine_opts
, 0);
3872 qemu_opts_foreach(qemu_find_opts("device"), default_driver_check
, NULL
, 0);
3873 qemu_opts_foreach(qemu_find_opts("global"), default_driver_check
, NULL
, 0);
3875 if (!vga_model
&& !default_vga
) {
3876 vga_interface_type
= VGA_DEVICE
;
3878 if (!has_defaults
|| machine_class
->no_serial
) {
3881 if (!has_defaults
|| machine_class
->no_parallel
) {
3882 default_parallel
= 0;
3884 if (!has_defaults
|| !machine_class
->use_virtcon
) {
3885 default_virtcon
= 0;
3887 if (!has_defaults
|| !machine_class
->use_sclp
) {
3890 if (!has_defaults
|| machine_class
->no_floppy
) {
3893 if (!has_defaults
|| machine_class
->no_cdrom
) {
3896 if (!has_defaults
|| machine_class
->no_sdcard
) {
3899 if (!has_defaults
) {
3900 default_monitor
= 0;
3905 if (is_daemonized()) {
3906 /* According to documentation and historically, -nographic redirects
3907 * serial port, parallel port and monitor to stdio, which does not work
3908 * with -daemonize. We can redirect these to null instead, but since
3909 * -nographic is legacy, let's just error out.
3910 * We disallow -nographic only if all other ports are not redirected
3911 * explicitly, to not break existing legacy setups which uses
3912 * -nographic _and_ redirects all ports explicitly - this is valid
3913 * usage, -nographic is just a no-op in this case.
3915 if (display_type
== DT_NOGRAPHIC
3916 && (default_parallel
|| default_serial
3917 || default_monitor
|| default_virtcon
)) {
3918 fprintf(stderr
, "-nographic can not be used with -daemonize\n");
3921 #ifdef CONFIG_CURSES
3922 if (display_type
== DT_CURSES
) {
3923 fprintf(stderr
, "curses display can not be used with -daemonize\n");
3929 if (display_type
== DT_NOGRAPHIC
) {
3930 if (default_parallel
)
3931 add_device_config(DEV_PARALLEL
, "null");
3932 if (default_serial
&& default_monitor
) {
3933 add_device_config(DEV_SERIAL
, "mon:stdio");
3934 } else if (default_virtcon
&& default_monitor
) {
3935 add_device_config(DEV_VIRTCON
, "mon:stdio");
3936 } else if (default_sclp
&& default_monitor
) {
3937 add_device_config(DEV_SCLP
, "mon:stdio");
3940 add_device_config(DEV_SERIAL
, "stdio");
3941 if (default_virtcon
)
3942 add_device_config(DEV_VIRTCON
, "stdio");
3944 add_device_config(DEV_SCLP
, "stdio");
3946 if (default_monitor
)
3947 monitor_parse("stdio", "readline");
3951 add_device_config(DEV_SERIAL
, "vc:80Cx24C");
3952 if (default_parallel
)
3953 add_device_config(DEV_PARALLEL
, "vc:80Cx24C");
3954 if (default_monitor
)
3955 monitor_parse("vc:80Cx24C", "readline");
3956 if (default_virtcon
)
3957 add_device_config(DEV_VIRTCON
, "vc:80Cx24C");
3959 add_device_config(DEV_SCLP
, "vc:80Cx24C");
3963 if (display_type
== DT_DEFAULT
&& !display_remote
) {
3964 #if defined(CONFIG_GTK)
3965 display_type
= DT_GTK
;
3966 #elif defined(CONFIG_SDL) || defined(CONFIG_COCOA)
3967 display_type
= DT_SDL
;
3968 #elif defined(CONFIG_VNC)
3969 vnc_display
= "localhost:0,to=99";
3972 display_type
= DT_NONE
;
3976 if ((no_frame
|| alt_grab
|| ctrl_grab
) && display_type
!= DT_SDL
) {
3977 fprintf(stderr
, "-no-frame, -alt-grab and -ctrl-grab are only valid "
3978 "for SDL, ignoring option\n");
3980 if (no_quit
&& (display_type
!= DT_GTK
&& display_type
!= DT_SDL
)) {
3981 fprintf(stderr
, "-no-quit is only valid for GTK and SDL, "
3982 "ignoring option\n");
3985 #if defined(CONFIG_GTK)
3986 if (display_type
== DT_GTK
) {
3987 early_gtk_display_init();
3993 if (qemu_opts_foreach(qemu_find_opts("chardev"), chardev_init_func
, NULL
, 1) != 0)
3995 #ifdef CONFIG_VIRTFS
3996 if (qemu_opts_foreach(qemu_find_opts("fsdev"), fsdev_init_func
, NULL
, 1) != 0) {
4001 if (pid_file
&& qemu_create_pidfile(pid_file
) != 0) {
4006 /* store value for the future use */
4007 qemu_opt_set_number(qemu_find_opts_singleton("memory"), "size", ram_size
);
4009 if (qemu_opts_foreach(qemu_find_opts("device"), device_help_func
, NULL
, 0)
4014 if (qemu_opts_foreach(qemu_find_opts("object"),
4015 object_create
, NULL
, 0) != 0) {
4019 machine_opts
= qemu_get_machine_opts();
4020 if (qemu_opt_foreach(machine_opts
, machine_set_property
, current_machine
,
4022 object_unref(OBJECT(current_machine
));
4026 configure_accelerator(current_machine
);
4029 Error
*local_err
= NULL
;
4030 qtest_init(qtest_chrdev
, qtest_log
, &local_err
);
4032 error_report("%s", error_get_pretty(local_err
));
4033 error_free(local_err
);
4038 machine_opts
= qemu_get_machine_opts();
4039 kernel_filename
= qemu_opt_get(machine_opts
, "kernel");
4040 initrd_filename
= qemu_opt_get(machine_opts
, "initrd");
4041 kernel_cmdline
= qemu_opt_get(machine_opts
, "append");
4042 bios_name
= qemu_opt_get(machine_opts
, "firmware");
4044 boot_order
= machine_class
->default_boot_order
;
4045 opts
= qemu_opts_find(qemu_find_opts("boot-opts"), NULL
);
4047 char *normal_boot_order
;
4048 const char *order
, *once
;
4050 order
= qemu_opt_get(opts
, "order");
4052 validate_bootdevices(order
);
4056 once
= qemu_opt_get(opts
, "once");
4058 validate_bootdevices(once
);
4059 normal_boot_order
= g_strdup(boot_order
);
4061 qemu_register_reset(restore_boot_order
, normal_boot_order
);
4064 boot_menu
= qemu_opt_get_bool(opts
, "menu", boot_menu
);
4065 boot_strict
= qemu_opt_get_bool(opts
, "strict", false);
4068 if (!kernel_cmdline
) {
4069 kernel_cmdline
= "";
4070 current_machine
->kernel_cmdline
= (char *)kernel_cmdline
;
4073 linux_boot
= (kernel_filename
!= NULL
);
4075 if (!linux_boot
&& *kernel_cmdline
!= '\0') {
4076 fprintf(stderr
, "-append only allowed with -kernel option\n");
4080 if (!linux_boot
&& initrd_filename
!= NULL
) {
4081 fprintf(stderr
, "-initrd only allowed with -kernel option\n");
4085 if (!linux_boot
&& qemu_opt_get(machine_opts
, "dtb")) {
4086 fprintf(stderr
, "-dtb only allowed with -kernel option\n");
4090 os_set_line_buffering();
4092 qemu_init_cpu_loop();
4093 qemu_mutex_lock_iothread();
4096 /* spice needs the timers to be initialized by this point */
4102 if (kvm_enabled() || xen_enabled()) {
4103 fprintf(stderr
, "-icount is not allowed with kvm or xen\n");
4106 configure_icount(icount_opts
, &error_abort
);
4107 qemu_opts_del(icount_opts
);
4110 /* clean up network at qemu process termination */
4111 atexit(&net_cleanup
);
4113 if (net_init_clients() < 0) {
4118 if (tpm_init() < 0) {
4123 /* init the bluetooth world */
4124 if (foreach_device_config(DEV_BT
, bt_parse
))
4127 if (!xen_enabled()) {
4128 /* On 32-bit hosts, QEMU is limited by virtual address space */
4129 if (ram_size
> (2047 << 20) && HOST_LONG_BITS
== 32) {
4130 fprintf(stderr
, "qemu: at most 2047 MB RAM can be simulated\n");
4138 /* If the currently selected machine wishes to override the units-per-bus
4139 * property of its default HBA interface type, do so now. */
4140 if (machine_class
->units_per_default_bus
) {
4141 override_max_devs(machine_class
->block_default_type
,
4142 machine_class
->units_per_default_bus
);
4145 /* open the virtual block devices */
4147 qemu_opts_foreach(qemu_find_opts("drive"), drive_enable_snapshot
, NULL
, 0);
4148 if (qemu_opts_foreach(qemu_find_opts("drive"), drive_init_func
,
4149 &machine_class
->block_default_type
, 1) != 0) {
4153 default_drive(default_cdrom
, snapshot
, machine_class
->block_default_type
, 2,
4155 default_drive(default_floppy
, snapshot
, IF_FLOPPY
, 0, FD_OPTS
);
4156 default_drive(default_sdcard
, snapshot
, IF_SD
, 0, SD_OPTS
);
4158 if (qemu_opts_foreach(qemu_find_opts("numa"), numa_init_func
,
4165 if (qemu_opts_foreach(qemu_find_opts("mon"), mon_init_func
, NULL
, 1) != 0) {
4169 if (foreach_device_config(DEV_SERIAL
, serial_parse
) < 0)
4171 if (foreach_device_config(DEV_PARALLEL
, parallel_parse
) < 0)
4173 if (foreach_device_config(DEV_VIRTCON
, virtcon_parse
) < 0)
4175 if (foreach_device_config(DEV_SCLP
, sclp_parse
) < 0) {
4178 if (foreach_device_config(DEV_DEBUGCON
, debugcon_parse
) < 0)
4181 /* If no default VGA is requested, the default is "none". */
4183 if (cirrus_vga_available()) {
4184 vga_model
= "cirrus";
4185 } else if (vga_available()) {
4190 select_vgahw(vga_model
);
4194 i
= select_watchdog(watchdog
);
4196 exit (i
== 1 ? 1 : 0);
4199 if (machine_class
->compat_props
) {
4200 qdev_prop_register_global_list(machine_class
->compat_props
);
4204 qdev_machine_init();
4206 current_machine
->ram_size
= ram_size
;
4207 current_machine
->maxram_size
= maxram_size
;
4208 current_machine
->ram_slots
= ram_slots
;
4209 current_machine
->boot_order
= boot_order
;
4210 current_machine
->cpu_model
= cpu_model
;
4212 machine_class
->init(current_machine
);
4218 cpu_synchronize_all_post_init();
4222 /* init USB devices */
4223 if (usb_enabled(false)) {
4224 if (foreach_device_config(DEV_USB
, usb_parse
) < 0)
4228 /* init generic devices */
4229 if (qemu_opts_foreach(qemu_find_opts("device"), device_init_func
, NULL
, 1) != 0)
4232 /* Did we create any drives that we failed to create a device for? */
4233 drive_check_orphaned();
4235 net_check_clients();
4237 ds
= init_displaystate();
4239 /* init local displays */
4240 switch (display_type
) {
4242 (void)ds
; /* avoid warning if no display is configured */
4244 #if defined(CONFIG_CURSES)
4246 curses_display_init(ds
, full_screen
);
4249 #if defined(CONFIG_SDL)
4251 sdl_display_init(ds
, full_screen
, no_frame
);
4253 #elif defined(CONFIG_COCOA)
4255 cocoa_display_init(ds
, full_screen
);
4258 #if defined(CONFIG_GTK)
4260 gtk_display_init(ds
, full_screen
, grab_on_hover
);
4267 /* must be after terminal init, SDL library changes signal handlers */
4268 os_setup_signal_handling();
4271 /* init remote displays */
4273 Error
*local_err
= NULL
;
4274 vnc_display_init(ds
);
4275 vnc_display_open(ds
, vnc_display
, &local_err
);
4276 if (local_err
!= NULL
) {
4277 error_report("Failed to start VNC server on `%s': %s",
4278 vnc_display
, error_get_pretty(local_err
));
4279 error_free(local_err
);
4283 if (show_vnc_port
) {
4284 printf("VNC server running on `%s'\n", vnc_display_local_addr(ds
));
4290 qemu_spice_display_init();
4294 if (foreach_device_config(DEV_GDB
, gdbserver_start
) < 0) {
4298 qdev_machine_creation_done();
4300 if (rom_load_all() != 0) {
4301 fprintf(stderr
, "rom loading failed\n");
4305 /* TODO: once all bus devices are qdevified, this should be done
4306 * when bus is created by qdev.c */
4307 qemu_register_reset(qbus_reset_all_fn
, sysbus_get_default());
4308 qemu_run_machine_init_done_notifiers();
4310 /* Done notifiers can load ROMs */
4313 qemu_system_reset(VMRESET_SILENT
);
4315 if (load_vmstate(loadvm
) < 0) {
4320 qdev_prop_check_globals();
4321 if (vmstate_dump_file
) {
4323 dump_vmstate_json_to_file(vmstate_dump_file
);
4328 Error
*local_err
= NULL
;
4329 qemu_start_incoming_migration(incoming
, &local_err
);
4331 error_report("-incoming %s: %s", incoming
,
4332 error_get_pretty(local_err
));
4333 error_free(local_err
);
4336 } else if (autostart
) {
4342 if (is_daemonized()) {
4343 if (!trace_init_backends(trace_events
, trace_file
)) {