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"
65 #include "hw/pcmcia.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 "qemu/cache-utils.h"
86 #include "sysemu/blockdev.h"
87 #include "hw/block/block.h"
88 #include "migration/block.h"
89 #include "sysemu/tpm.h"
90 #include "sysemu/dma.h"
91 #include "audio/audio.h"
92 #include "migration/migration.h"
93 #include "sysemu/kvm.h"
94 #include "qapi/qmp/qjson.h"
95 #include "qemu/option.h"
96 #include "qemu/config-file.h"
97 #include "qemu-options.h"
98 #include "qmp-commands.h"
99 #include "qemu/main-loop.h"
101 #include "fsdev/qemu-fsdev.h"
103 #include "sysemu/qtest.h"
105 #include "disas/disas.h"
108 #include "slirp/libslirp.h"
111 #include "trace/control.h"
112 #include "qemu/queue.h"
113 #include "sysemu/cpus.h"
114 #include "sysemu/arch_init.h"
115 #include "qemu/osdep.h"
117 #include "ui/qemu-spice.h"
118 #include "qapi/string-input-visitor.h"
119 #include "qapi/opts-visitor.h"
120 #include "qom/object_interfaces.h"
121 #include "qapi-event.h"
123 #define DEFAULT_RAM_SIZE 128
125 #define MAX_VIRTIO_CONSOLES 1
126 #define MAX_SCLP_CONSOLES 1
128 static const char *data_dir
[16];
129 static int data_dir_idx
;
130 const char *bios_name
= NULL
;
131 enum vga_retrace_method vga_retrace_method
= VGA_RETRACE_DUMB
;
132 DisplayType display_type
= DT_DEFAULT
;
133 static int display_remote
;
134 const char* keyboard_layout
= NULL
;
136 const char *mem_path
= NULL
;
137 int mem_prealloc
= 0; /* force preallocation of physical target memory */
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
];
182 static bool boot_strict
;
183 uint8_t *boot_splash_filedata
;
184 size_t boot_splash_filedata_size
;
185 uint8_t qemu_extra_params_fw
[2];
187 typedef struct FWBootEntry FWBootEntry
;
190 QTAILQ_ENTRY(FWBootEntry
) link
;
196 static QTAILQ_HEAD(, FWBootEntry
) fw_boot_order
=
197 QTAILQ_HEAD_INITIALIZER(fw_boot_order
);
200 NodeInfo numa_info
[MAX_NODES
];
202 uint8_t qemu_uuid
[16];
205 static QEMUBootSetHandler
*boot_set_handler
;
206 static void *boot_set_opaque
;
208 static NotifierList exit_notifiers
=
209 NOTIFIER_LIST_INITIALIZER(exit_notifiers
);
211 static NotifierList machine_init_done_notifiers
=
212 NOTIFIER_LIST_INITIALIZER(machine_init_done_notifiers
);
214 static bool tcg_allowed
= true;
217 enum xen_mode xen_mode
= XEN_EMULATE
;
218 static int tcg_tb_size
;
220 static int has_defaults
= 1;
221 static int default_serial
= 1;
222 static int default_parallel
= 1;
223 static int default_virtcon
= 1;
224 static int default_sclp
= 1;
225 static int default_monitor
= 1;
226 static int default_floppy
= 1;
227 static int default_cdrom
= 1;
228 static int default_sdcard
= 1;
229 static int default_vga
= 1;
235 { .driver
= "isa-serial", .flag
= &default_serial
},
236 { .driver
= "isa-parallel", .flag
= &default_parallel
},
237 { .driver
= "isa-fdc", .flag
= &default_floppy
},
238 { .driver
= "ide-cd", .flag
= &default_cdrom
},
239 { .driver
= "ide-hd", .flag
= &default_cdrom
},
240 { .driver
= "ide-drive", .flag
= &default_cdrom
},
241 { .driver
= "scsi-cd", .flag
= &default_cdrom
},
242 { .driver
= "virtio-serial-pci", .flag
= &default_virtcon
},
243 { .driver
= "virtio-serial-s390", .flag
= &default_virtcon
},
244 { .driver
= "virtio-serial", .flag
= &default_virtcon
},
245 { .driver
= "VGA", .flag
= &default_vga
},
246 { .driver
= "isa-vga", .flag
= &default_vga
},
247 { .driver
= "cirrus-vga", .flag
= &default_vga
},
248 { .driver
= "isa-cirrus-vga", .flag
= &default_vga
},
249 { .driver
= "vmware-svga", .flag
= &default_vga
},
250 { .driver
= "qxl-vga", .flag
= &default_vga
},
253 static QemuOptsList qemu_rtc_opts
= {
255 .head
= QTAILQ_HEAD_INITIALIZER(qemu_rtc_opts
.head
),
259 .type
= QEMU_OPT_STRING
,
262 .type
= QEMU_OPT_STRING
,
265 .type
= QEMU_OPT_STRING
,
267 { /* end of list */ }
271 static QemuOptsList qemu_sandbox_opts
= {
273 .implied_opt_name
= "enable",
274 .head
= QTAILQ_HEAD_INITIALIZER(qemu_sandbox_opts
.head
),
278 .type
= QEMU_OPT_BOOL
,
280 { /* end of list */ }
284 static QemuOptsList qemu_trace_opts
= {
286 .implied_opt_name
= "trace",
287 .head
= QTAILQ_HEAD_INITIALIZER(qemu_trace_opts
.head
),
291 .type
= QEMU_OPT_STRING
,
294 .type
= QEMU_OPT_STRING
,
296 { /* end of list */ }
300 static QemuOptsList qemu_option_rom_opts
= {
301 .name
= "option-rom",
302 .implied_opt_name
= "romfile",
303 .head
= QTAILQ_HEAD_INITIALIZER(qemu_option_rom_opts
.head
),
307 .type
= QEMU_OPT_NUMBER
,
310 .type
= QEMU_OPT_STRING
,
312 { /* end of list */ }
316 static QemuOptsList qemu_machine_opts
= {
318 .implied_opt_name
= "type",
320 .head
= QTAILQ_HEAD_INITIALIZER(qemu_machine_opts
.head
),
324 .type
= QEMU_OPT_STRING
,
325 .help
= "emulated machine"
328 .type
= QEMU_OPT_STRING
,
329 .help
= "accelerator list",
331 .name
= "kernel_irqchip",
332 .type
= QEMU_OPT_BOOL
,
333 .help
= "use KVM in-kernel irqchip",
335 .name
= "kvm_shadow_mem",
336 .type
= QEMU_OPT_SIZE
,
337 .help
= "KVM shadow MMU size",
340 .type
= QEMU_OPT_STRING
,
341 .help
= "Linux kernel image file",
344 .type
= QEMU_OPT_STRING
,
345 .help
= "Linux initial ramdisk file",
348 .type
= QEMU_OPT_STRING
,
349 .help
= "Linux kernel command line",
352 .type
= QEMU_OPT_STRING
,
353 .help
= "Linux kernel device tree file",
356 .type
= QEMU_OPT_STRING
,
357 .help
= "Dump current dtb to a file and quit",
359 .name
= "phandle_start",
360 .type
= QEMU_OPT_NUMBER
,
361 .help
= "The first phandle ID we may generate dynamically",
363 .name
= "dt_compatible",
364 .type
= QEMU_OPT_STRING
,
365 .help
= "Overrides the \"compatible\" property of the dt root node",
367 .name
= "dump-guest-core",
368 .type
= QEMU_OPT_BOOL
,
369 .help
= "Include guest memory in a core dump",
372 .type
= QEMU_OPT_BOOL
,
373 .help
= "enable/disable memory merge support",
376 .type
= QEMU_OPT_BOOL
,
377 .help
= "Set on/off to enable/disable usb",
380 .type
= QEMU_OPT_STRING
,
381 .help
= "firmware image",
384 .type
= QEMU_OPT_STRING
,
385 .help
= "Specifies the KVM virtualization mode (HV, PR)",
387 { /* End of list */ }
391 static QemuOptsList qemu_boot_opts
= {
393 .implied_opt_name
= "order",
395 .head
= QTAILQ_HEAD_INITIALIZER(qemu_boot_opts
.head
),
399 .type
= QEMU_OPT_STRING
,
402 .type
= QEMU_OPT_STRING
,
405 .type
= QEMU_OPT_BOOL
,
408 .type
= QEMU_OPT_STRING
,
410 .name
= "splash-time",
411 .type
= QEMU_OPT_STRING
,
413 .name
= "reboot-timeout",
414 .type
= QEMU_OPT_STRING
,
417 .type
= QEMU_OPT_BOOL
,
423 static QemuOptsList qemu_add_fd_opts
= {
425 .head
= QTAILQ_HEAD_INITIALIZER(qemu_add_fd_opts
.head
),
429 .type
= QEMU_OPT_NUMBER
,
430 .help
= "file descriptor of which a duplicate is added to fd set",
433 .type
= QEMU_OPT_NUMBER
,
434 .help
= "ID of the fd set to add fd to",
437 .type
= QEMU_OPT_STRING
,
438 .help
= "free-form string used to describe fd",
440 { /* end of list */ }
444 static QemuOptsList qemu_object_opts
= {
446 .implied_opt_name
= "qom-type",
447 .head
= QTAILQ_HEAD_INITIALIZER(qemu_object_opts
.head
),
453 static QemuOptsList qemu_tpmdev_opts
= {
455 .implied_opt_name
= "type",
456 .head
= QTAILQ_HEAD_INITIALIZER(qemu_tpmdev_opts
.head
),
458 /* options are defined in the TPM backends */
459 { /* end of list */ }
463 static QemuOptsList qemu_realtime_opts
= {
465 .head
= QTAILQ_HEAD_INITIALIZER(qemu_realtime_opts
.head
),
469 .type
= QEMU_OPT_BOOL
,
471 { /* end of list */ }
475 static QemuOptsList qemu_msg_opts
= {
477 .head
= QTAILQ_HEAD_INITIALIZER(qemu_msg_opts
.head
),
481 .type
= QEMU_OPT_BOOL
,
483 { /* end of list */ }
487 static QemuOptsList qemu_name_opts
= {
489 .implied_opt_name
= "guest",
491 .head
= QTAILQ_HEAD_INITIALIZER(qemu_name_opts
.head
),
495 .type
= QEMU_OPT_STRING
,
496 .help
= "Sets the name of the guest.\n"
497 "This name will be displayed in the SDL window caption.\n"
498 "The name will also be used for the VNC server",
501 .type
= QEMU_OPT_STRING
,
502 .help
= "Sets the name of the QEMU process, as shown in top etc",
504 .name
= "debug-threads",
505 .type
= QEMU_OPT_BOOL
,
506 .help
= "When enabled, name the individual threads; defaults off.\n"
507 "NOTE: The thread names are for debugging and not a\n"
510 { /* End of list */ }
514 static QemuOptsList qemu_mem_opts
= {
516 .implied_opt_name
= "size",
517 .head
= QTAILQ_HEAD_INITIALIZER(qemu_mem_opts
.head
),
522 .type
= QEMU_OPT_SIZE
,
526 .type
= QEMU_OPT_NUMBER
,
530 .type
= QEMU_OPT_SIZE
,
532 { /* end of list */ }
537 * Get machine options
539 * Returns: machine options (never null).
541 QemuOpts
*qemu_get_machine_opts(void)
543 return qemu_find_opts_singleton("machine");
546 const char *qemu_get_vm_name(void)
551 static void res_free(void)
553 if (boot_splash_filedata
!= NULL
) {
554 g_free(boot_splash_filedata
);
555 boot_splash_filedata
= NULL
;
559 static int default_driver_check(QemuOpts
*opts
, void *opaque
)
561 const char *driver
= qemu_opt_get(opts
, "driver");
566 for (i
= 0; i
< ARRAY_SIZE(default_list
); i
++) {
567 if (strcmp(default_list
[i
].driver
, driver
) != 0)
569 *(default_list
[i
].flag
) = 0;
574 /***********************************************************/
577 static RunState current_run_state
= RUN_STATE_PRELAUNCH
;
579 /* We use RUN_STATE_MAX but any invalid value will do */
580 static RunState vmstop_requested
= RUN_STATE_MAX
;
581 static QemuMutex vmstop_lock
;
586 } RunStateTransition
;
588 static const RunStateTransition runstate_transitions_def
[] = {
590 { RUN_STATE_DEBUG
, RUN_STATE_RUNNING
},
591 { RUN_STATE_DEBUG
, RUN_STATE_FINISH_MIGRATE
},
593 { RUN_STATE_INMIGRATE
, RUN_STATE_RUNNING
},
594 { RUN_STATE_INMIGRATE
, RUN_STATE_PAUSED
},
596 { RUN_STATE_INTERNAL_ERROR
, RUN_STATE_PAUSED
},
597 { RUN_STATE_INTERNAL_ERROR
, RUN_STATE_FINISH_MIGRATE
},
599 { RUN_STATE_IO_ERROR
, RUN_STATE_RUNNING
},
600 { RUN_STATE_IO_ERROR
, RUN_STATE_FINISH_MIGRATE
},
602 { RUN_STATE_PAUSED
, RUN_STATE_RUNNING
},
603 { RUN_STATE_PAUSED
, RUN_STATE_FINISH_MIGRATE
},
605 { RUN_STATE_POSTMIGRATE
, RUN_STATE_RUNNING
},
606 { RUN_STATE_POSTMIGRATE
, RUN_STATE_FINISH_MIGRATE
},
608 { RUN_STATE_PRELAUNCH
, RUN_STATE_RUNNING
},
609 { RUN_STATE_PRELAUNCH
, RUN_STATE_FINISH_MIGRATE
},
610 { RUN_STATE_PRELAUNCH
, RUN_STATE_INMIGRATE
},
612 { RUN_STATE_FINISH_MIGRATE
, RUN_STATE_RUNNING
},
613 { RUN_STATE_FINISH_MIGRATE
, RUN_STATE_POSTMIGRATE
},
615 { RUN_STATE_RESTORE_VM
, RUN_STATE_RUNNING
},
617 { RUN_STATE_RUNNING
, RUN_STATE_DEBUG
},
618 { RUN_STATE_RUNNING
, RUN_STATE_INTERNAL_ERROR
},
619 { RUN_STATE_RUNNING
, RUN_STATE_IO_ERROR
},
620 { RUN_STATE_RUNNING
, RUN_STATE_PAUSED
},
621 { RUN_STATE_RUNNING
, RUN_STATE_FINISH_MIGRATE
},
622 { RUN_STATE_RUNNING
, RUN_STATE_RESTORE_VM
},
623 { RUN_STATE_RUNNING
, RUN_STATE_SAVE_VM
},
624 { RUN_STATE_RUNNING
, RUN_STATE_SHUTDOWN
},
625 { RUN_STATE_RUNNING
, RUN_STATE_WATCHDOG
},
626 { RUN_STATE_RUNNING
, RUN_STATE_GUEST_PANICKED
},
628 { RUN_STATE_SAVE_VM
, RUN_STATE_RUNNING
},
630 { RUN_STATE_SHUTDOWN
, RUN_STATE_PAUSED
},
631 { RUN_STATE_SHUTDOWN
, RUN_STATE_FINISH_MIGRATE
},
633 { RUN_STATE_DEBUG
, RUN_STATE_SUSPENDED
},
634 { RUN_STATE_RUNNING
, RUN_STATE_SUSPENDED
},
635 { RUN_STATE_SUSPENDED
, RUN_STATE_RUNNING
},
636 { RUN_STATE_SUSPENDED
, RUN_STATE_FINISH_MIGRATE
},
638 { RUN_STATE_WATCHDOG
, RUN_STATE_RUNNING
},
639 { RUN_STATE_WATCHDOG
, RUN_STATE_FINISH_MIGRATE
},
641 { RUN_STATE_GUEST_PANICKED
, RUN_STATE_RUNNING
},
642 { RUN_STATE_GUEST_PANICKED
, RUN_STATE_FINISH_MIGRATE
},
644 { RUN_STATE_MAX
, RUN_STATE_MAX
},
647 static bool runstate_valid_transitions
[RUN_STATE_MAX
][RUN_STATE_MAX
];
649 bool runstate_check(RunState state
)
651 return current_run_state
== state
;
654 static void runstate_init(void)
656 const RunStateTransition
*p
;
658 memset(&runstate_valid_transitions
, 0, sizeof(runstate_valid_transitions
));
659 for (p
= &runstate_transitions_def
[0]; p
->from
!= RUN_STATE_MAX
; p
++) {
660 runstate_valid_transitions
[p
->from
][p
->to
] = true;
663 qemu_mutex_init(&vmstop_lock
);
666 /* This function will abort() on invalid state transitions */
667 void runstate_set(RunState new_state
)
669 assert(new_state
< RUN_STATE_MAX
);
671 if (!runstate_valid_transitions
[current_run_state
][new_state
]) {
672 fprintf(stderr
, "ERROR: invalid runstate transition: '%s' -> '%s'\n",
673 RunState_lookup
[current_run_state
],
674 RunState_lookup
[new_state
]);
677 trace_runstate_set(new_state
);
678 current_run_state
= new_state
;
681 int runstate_is_running(void)
683 return runstate_check(RUN_STATE_RUNNING
);
686 bool runstate_needs_reset(void)
688 return runstate_check(RUN_STATE_INTERNAL_ERROR
) ||
689 runstate_check(RUN_STATE_SHUTDOWN
);
692 StatusInfo
*qmp_query_status(Error
**errp
)
694 StatusInfo
*info
= g_malloc0(sizeof(*info
));
696 info
->running
= runstate_is_running();
697 info
->singlestep
= singlestep
;
698 info
->status
= current_run_state
;
703 static bool qemu_vmstop_requested(RunState
*r
)
705 qemu_mutex_lock(&vmstop_lock
);
706 *r
= vmstop_requested
;
707 vmstop_requested
= RUN_STATE_MAX
;
708 qemu_mutex_unlock(&vmstop_lock
);
709 return *r
< RUN_STATE_MAX
;
712 void qemu_system_vmstop_request_prepare(void)
714 qemu_mutex_lock(&vmstop_lock
);
717 void qemu_system_vmstop_request(RunState state
)
719 vmstop_requested
= state
;
720 qemu_mutex_unlock(&vmstop_lock
);
728 qemu_vmstop_requested(&requested
);
729 if (runstate_is_running() && requested
== RUN_STATE_MAX
) {
733 /* Ensure that a STOP/RESUME pair of events is emitted if a
734 * vmstop request was pending. The BLOCK_IO_ERROR event, for
735 * example, according to documentation is always followed by
738 if (runstate_is_running()) {
739 qapi_event_send_stop(&error_abort
);
742 runstate_set(RUN_STATE_RUNNING
);
743 vm_state_notify(1, RUN_STATE_RUNNING
);
747 qapi_event_send_resume(&error_abort
);
751 /***********************************************************/
752 /* real time host monotonic timer */
754 /***********************************************************/
755 /* host time/date access */
756 void qemu_get_timedate(struct tm
*tm
, int offset
)
762 if (rtc_date_offset
== -1) {
766 localtime_r(&ti
, tm
);
768 ti
-= rtc_date_offset
;
773 int qemu_timedate_diff(struct tm
*tm
)
777 if (rtc_date_offset
== -1)
779 seconds
= mktimegm(tm
);
782 tmp
.tm_isdst
= -1; /* use timezone to figure it out */
783 seconds
= mktime(&tmp
);
786 seconds
= mktimegm(tm
) + rtc_date_offset
;
788 return seconds
- time(NULL
);
791 void rtc_change_mon_event(struct tm
*tm
)
795 data
= qobject_from_jsonf("{ 'offset': %d }", qemu_timedate_diff(tm
));
796 monitor_protocol_event(QEVENT_RTC_CHANGE
, data
);
797 qobject_decref(data
);
800 static void configure_rtc_date_offset(const char *startdate
, int legacy
)
802 time_t rtc_start_date
;
805 if (!strcmp(startdate
, "now") && legacy
) {
806 rtc_date_offset
= -1;
808 if (sscanf(startdate
, "%d-%d-%dT%d:%d:%d",
816 } else if (sscanf(startdate
, "%d-%d-%d",
828 rtc_start_date
= mktimegm(&tm
);
829 if (rtc_start_date
== -1) {
831 fprintf(stderr
, "Invalid date format. Valid formats are:\n"
832 "'2006-06-17T16:01:21' or '2006-06-17'\n");
835 rtc_date_offset
= time(NULL
) - rtc_start_date
;
839 static void configure_rtc(QemuOpts
*opts
)
843 value
= qemu_opt_get(opts
, "base");
845 if (!strcmp(value
, "utc")) {
847 } else if (!strcmp(value
, "localtime")) {
850 configure_rtc_date_offset(value
, 0);
853 value
= qemu_opt_get(opts
, "clock");
855 if (!strcmp(value
, "host")) {
856 rtc_clock
= QEMU_CLOCK_HOST
;
857 } else if (!strcmp(value
, "rt")) {
858 rtc_clock
= QEMU_CLOCK_REALTIME
;
859 } else if (!strcmp(value
, "vm")) {
860 rtc_clock
= QEMU_CLOCK_VIRTUAL
;
862 fprintf(stderr
, "qemu: invalid option value '%s'\n", value
);
866 value
= qemu_opt_get(opts
, "driftfix");
868 if (!strcmp(value
, "slew")) {
869 static GlobalProperty slew_lost_ticks
[] = {
871 .driver
= "mc146818rtc",
872 .property
= "lost_tick_policy",
875 { /* end of list */ }
878 qdev_prop_register_global_list(slew_lost_ticks
);
879 } else if (!strcmp(value
, "none")) {
880 /* discard is default */
882 fprintf(stderr
, "qemu: invalid option value '%s'\n", value
);
888 /***********************************************************/
889 /* Bluetooth support */
892 static struct HCIInfo
*hci_table
[MAX_NICS
];
894 struct HCIInfo
*qemu_next_hci(void)
896 if (cur_hci
== nb_hcis
)
899 return hci_table
[cur_hci
++];
902 static int bt_hci_parse(const char *str
)
907 if (nb_hcis
>= MAX_NICS
) {
908 fprintf(stderr
, "qemu: Too many bluetooth HCIs (max %i).\n", MAX_NICS
);
921 bdaddr
.b
[5] = 0x56 + nb_hcis
;
922 hci
->bdaddr_set(hci
, bdaddr
.b
);
924 hci_table
[nb_hcis
++] = hci
;
929 static void bt_vhci_add(int vlan_id
)
931 struct bt_scatternet_s
*vlan
= qemu_find_bt_vlan(vlan_id
);
934 fprintf(stderr
, "qemu: warning: adding a VHCI to "
935 "an empty scatternet %i\n", vlan_id
);
937 bt_vhci_init(bt_new_hci(vlan
));
940 static struct bt_device_s
*bt_device_add(const char *opt
)
942 struct bt_scatternet_s
*vlan
;
944 char *endp
= strstr(opt
, ",vlan=");
945 int len
= (endp
? endp
- opt
: strlen(opt
)) + 1;
948 pstrcpy(devname
, MIN(sizeof(devname
), len
), opt
);
951 vlan_id
= strtol(endp
+ 6, &endp
, 0);
953 fprintf(stderr
, "qemu: unrecognised bluetooth vlan Id\n");
958 vlan
= qemu_find_bt_vlan(vlan_id
);
961 fprintf(stderr
, "qemu: warning: adding a slave device to "
962 "an empty scatternet %i\n", vlan_id
);
964 if (!strcmp(devname
, "keyboard"))
965 return bt_keyboard_init(vlan
);
967 fprintf(stderr
, "qemu: unsupported bluetooth device `%s'\n", devname
);
971 static int bt_parse(const char *opt
)
973 const char *endp
, *p
;
976 if (strstart(opt
, "hci", &endp
)) {
977 if (!*endp
|| *endp
== ',') {
979 if (!strstart(endp
, ",vlan=", 0))
982 return bt_hci_parse(opt
);
984 } else if (strstart(opt
, "vhci", &endp
)) {
985 if (!*endp
|| *endp
== ',') {
987 if (strstart(endp
, ",vlan=", &p
)) {
988 vlan
= strtol(p
, (char **) &endp
, 0);
990 fprintf(stderr
, "qemu: bad scatternet '%s'\n", p
);
994 fprintf(stderr
, "qemu: bad parameter '%s'\n", endp
+ 1);
1003 } else if (strstart(opt
, "device:", &endp
))
1004 return !bt_device_add(endp
);
1006 fprintf(stderr
, "qemu: bad bluetooth parameter '%s'\n", opt
);
1010 static int parse_sandbox(QemuOpts
*opts
, void *opaque
)
1012 /* FIXME: change this to true for 1.3 */
1013 if (qemu_opt_get_bool(opts
, "enable", false)) {
1014 #ifdef CONFIG_SECCOMP
1015 if (seccomp_start() < 0) {
1016 qerror_report(ERROR_CLASS_GENERIC_ERROR
,
1017 "failed to install seccomp syscall filter in the kernel");
1021 qerror_report(ERROR_CLASS_GENERIC_ERROR
,
1022 "sandboxing request but seccomp is not compiled into this build");
1030 static int parse_name(QemuOpts
*opts
, void *opaque
)
1032 const char *proc_name
;
1034 if (qemu_opt_get(opts
, "debug-threads")) {
1035 qemu_thread_naming(qemu_opt_get_bool(opts
, "debug-threads", false));
1037 qemu_name
= qemu_opt_get(opts
, "guest");
1039 proc_name
= qemu_opt_get(opts
, "process");
1041 os_set_proc_name(proc_name
);
1047 bool usb_enabled(bool default_usb
)
1049 return qemu_opt_get_bool(qemu_get_machine_opts(), "usb",
1050 has_defaults
&& default_usb
);
1054 static int parse_add_fd(QemuOpts
*opts
, void *opaque
)
1056 int fd
, dupfd
, flags
;
1058 const char *fd_opaque
= NULL
;
1060 fd
= qemu_opt_get_number(opts
, "fd", -1);
1061 fdset_id
= qemu_opt_get_number(opts
, "set", -1);
1062 fd_opaque
= qemu_opt_get(opts
, "opaque");
1065 qerror_report(ERROR_CLASS_GENERIC_ERROR
,
1066 "fd option is required and must be non-negative");
1070 if (fd
<= STDERR_FILENO
) {
1071 qerror_report(ERROR_CLASS_GENERIC_ERROR
,
1072 "fd cannot be a standard I/O stream");
1077 * All fds inherited across exec() necessarily have FD_CLOEXEC
1078 * clear, while qemu sets FD_CLOEXEC on all other fds used internally.
1080 flags
= fcntl(fd
, F_GETFD
);
1081 if (flags
== -1 || (flags
& FD_CLOEXEC
)) {
1082 qerror_report(ERROR_CLASS_GENERIC_ERROR
,
1083 "fd is not valid or already in use");
1088 qerror_report(ERROR_CLASS_GENERIC_ERROR
,
1089 "set option is required and must be non-negative");
1093 #ifdef F_DUPFD_CLOEXEC
1094 dupfd
= fcntl(fd
, F_DUPFD_CLOEXEC
, 0);
1098 qemu_set_cloexec(dupfd
);
1102 qerror_report(ERROR_CLASS_GENERIC_ERROR
,
1103 "Error duplicating fd: %s", strerror(errno
));
1107 /* add the duplicate fd, and optionally the opaque string, to the fd set */
1108 monitor_fdset_add_fd(dupfd
, true, fdset_id
, fd_opaque
? true : false,
1114 static int cleanup_add_fd(QemuOpts
*opts
, void *opaque
)
1118 fd
= qemu_opt_get_number(opts
, "fd", -1);
1125 /***********************************************************/
1126 /* QEMU Block devices */
1128 #define HD_OPTS "media=disk"
1129 #define CDROM_OPTS "media=cdrom"
1131 #define PFLASH_OPTS ""
1135 static int drive_init_func(QemuOpts
*opts
, void *opaque
)
1137 BlockInterfaceType
*block_default_type
= opaque
;
1139 return drive_new(opts
, *block_default_type
) == NULL
;
1142 static int drive_enable_snapshot(QemuOpts
*opts
, void *opaque
)
1144 if (NULL
== qemu_opt_get(opts
, "snapshot")) {
1145 qemu_opt_set(opts
, "snapshot", "on");
1150 static void default_drive(int enable
, int snapshot
, BlockInterfaceType type
,
1151 int index
, const char *optstr
)
1155 if (!enable
|| drive_get_by_index(type
, index
)) {
1159 opts
= drive_add(type
, index
, NULL
, optstr
);
1161 drive_enable_snapshot(opts
, NULL
);
1163 if (!drive_new(opts
, type
)) {
1168 void qemu_register_boot_set(QEMUBootSetHandler
*func
, void *opaque
)
1170 boot_set_handler
= func
;
1171 boot_set_opaque
= opaque
;
1174 int qemu_boot_set(const char *boot_order
)
1176 if (!boot_set_handler
) {
1179 return boot_set_handler(boot_set_opaque
, boot_order
);
1182 static void validate_bootdevices(const char *devices
)
1184 /* We just do some generic consistency checks */
1188 for (p
= devices
; *p
!= '\0'; p
++) {
1189 /* Allowed boot devices are:
1190 * a-b: floppy disk drives
1191 * c-f: IDE disk drives
1192 * g-m: machine implementation dependent drives
1193 * n-p: network devices
1194 * It's up to each machine implementation to check if the given boot
1195 * devices match the actual hardware implementation and firmware
1198 if (*p
< 'a' || *p
> 'p') {
1199 fprintf(stderr
, "Invalid boot device '%c'\n", *p
);
1202 if (bitmap
& (1 << (*p
- 'a'))) {
1203 fprintf(stderr
, "Boot device '%c' was given twice\n", *p
);
1206 bitmap
|= 1 << (*p
- 'a');
1210 static void restore_boot_order(void *opaque
)
1212 char *normal_boot_order
= opaque
;
1213 static int first
= 1;
1215 /* Restore boot order and remove ourselves after the first boot */
1221 qemu_boot_set(normal_boot_order
);
1223 qemu_unregister_reset(restore_boot_order
, normal_boot_order
);
1224 g_free(normal_boot_order
);
1227 void add_boot_device_path(int32_t bootindex
, DeviceState
*dev
,
1230 FWBootEntry
*node
, *i
;
1232 if (bootindex
< 0) {
1236 assert(dev
!= NULL
|| suffix
!= NULL
);
1238 node
= g_malloc0(sizeof(FWBootEntry
));
1239 node
->bootindex
= bootindex
;
1240 node
->suffix
= g_strdup(suffix
);
1243 QTAILQ_FOREACH(i
, &fw_boot_order
, link
) {
1244 if (i
->bootindex
== bootindex
) {
1245 fprintf(stderr
, "Two devices with same boot index %d\n", bootindex
);
1247 } else if (i
->bootindex
< bootindex
) {
1250 QTAILQ_INSERT_BEFORE(i
, node
, link
);
1253 QTAILQ_INSERT_TAIL(&fw_boot_order
, node
, link
);
1256 DeviceState
*get_boot_device(uint32_t position
)
1258 uint32_t counter
= 0;
1259 FWBootEntry
*i
= NULL
;
1260 DeviceState
*res
= NULL
;
1262 if (!QTAILQ_EMPTY(&fw_boot_order
)) {
1263 QTAILQ_FOREACH(i
, &fw_boot_order
, link
) {
1264 if (counter
== position
) {
1275 * This function returns null terminated string that consist of new line
1276 * separated device paths.
1278 * memory pointed by "size" is assigned total length of the array in bytes
1281 char *get_boot_devices_list(size_t *size
, bool ignore_suffixes
)
1287 QTAILQ_FOREACH(i
, &fw_boot_order
, link
) {
1288 char *devpath
= NULL
, *bootpath
;
1292 devpath
= qdev_get_fw_dev_path(i
->dev
);
1296 if (i
->suffix
&& !ignore_suffixes
&& devpath
) {
1297 size_t bootpathlen
= strlen(devpath
) + strlen(i
->suffix
) + 1;
1299 bootpath
= g_malloc(bootpathlen
);
1300 snprintf(bootpath
, bootpathlen
, "%s%s", devpath
, i
->suffix
);
1302 } else if (devpath
) {
1304 } else if (!ignore_suffixes
) {
1306 bootpath
= g_strdup(i
->suffix
);
1308 bootpath
= g_strdup("");
1312 list
[total
-1] = '\n';
1314 len
= strlen(bootpath
) + 1;
1315 list
= g_realloc(list
, total
+ len
);
1316 memcpy(&list
[total
], bootpath
, len
);
1323 if (boot_strict
&& *size
> 0) {
1324 list
[total
-1] = '\n';
1325 list
= g_realloc(list
, total
+ 5);
1326 memcpy(&list
[total
], "HALT", 5);
1332 static QemuOptsList qemu_smp_opts
= {
1334 .implied_opt_name
= "cpus",
1335 .merge_lists
= true,
1336 .head
= QTAILQ_HEAD_INITIALIZER(qemu_smp_opts
.head
),
1340 .type
= QEMU_OPT_NUMBER
,
1343 .type
= QEMU_OPT_NUMBER
,
1346 .type
= QEMU_OPT_NUMBER
,
1349 .type
= QEMU_OPT_NUMBER
,
1352 .type
= QEMU_OPT_NUMBER
,
1354 { /*End of list */ }
1358 static void smp_parse(QemuOpts
*opts
)
1362 unsigned cpus
= qemu_opt_get_number(opts
, "cpus", 0);
1363 unsigned sockets
= qemu_opt_get_number(opts
, "sockets", 0);
1364 unsigned cores
= qemu_opt_get_number(opts
, "cores", 0);
1365 unsigned threads
= qemu_opt_get_number(opts
, "threads", 0);
1367 /* compute missing values, prefer sockets over cores over threads */
1368 if (cpus
== 0 || sockets
== 0) {
1369 sockets
= sockets
> 0 ? sockets
: 1;
1370 cores
= cores
> 0 ? cores
: 1;
1371 threads
= threads
> 0 ? threads
: 1;
1373 cpus
= cores
* threads
* sockets
;
1377 threads
= threads
> 0 ? threads
: 1;
1378 cores
= cpus
/ (sockets
* threads
);
1380 threads
= cpus
/ (cores
* sockets
);
1384 max_cpus
= qemu_opt_get_number(opts
, "maxcpus", 0);
1387 smp_cores
= cores
> 0 ? cores
: 1;
1388 smp_threads
= threads
> 0 ? threads
: 1;
1392 if (max_cpus
== 0) {
1393 max_cpus
= smp_cpus
;
1396 if (max_cpus
> MAX_CPUMASK_BITS
) {
1397 fprintf(stderr
, "Unsupported number of maxcpus\n");
1400 if (max_cpus
< smp_cpus
) {
1401 fprintf(stderr
, "maxcpus must be equal to or greater than smp\n");
1407 static void configure_realtime(QemuOpts
*opts
)
1411 enable_mlock
= qemu_opt_get_bool(opts
, "mlock", true);
1414 if (os_mlock() < 0) {
1415 fprintf(stderr
, "qemu: locking memory failed\n");
1422 static void configure_msg(QemuOpts
*opts
)
1424 enable_timestamp_msg
= qemu_opt_get_bool(opts
, "timestamp", true);
1427 /***********************************************************/
1430 static int usb_device_add(const char *devname
)
1432 USBDevice
*dev
= NULL
;
1433 #ifndef CONFIG_LINUX
1437 if (!usb_enabled(false)) {
1441 /* drivers with .usbdevice_name entry in USBDeviceInfo */
1442 dev
= usbdevice_create(devname
);
1446 /* the other ones */
1447 #ifndef CONFIG_LINUX
1448 /* only the linux version is qdev-ified, usb-bsd still needs this */
1449 if (strstart(devname
, "host:", &p
)) {
1450 dev
= usb_host_device_open(usb_bus_find(-1), p
);
1460 static int usb_device_del(const char *devname
)
1465 if (strstart(devname
, "host:", &p
)) {
1469 if (!usb_enabled(false)) {
1473 p
= strchr(devname
, '.');
1476 bus_num
= strtoul(devname
, NULL
, 0);
1477 addr
= strtoul(p
+ 1, NULL
, 0);
1479 return usb_device_delete_addr(bus_num
, addr
);
1482 static int usb_parse(const char *cmdline
)
1485 r
= usb_device_add(cmdline
);
1487 fprintf(stderr
, "qemu: could not add USB device '%s'\n", cmdline
);
1492 void do_usb_add(Monitor
*mon
, const QDict
*qdict
)
1494 const char *devname
= qdict_get_str(qdict
, "devname");
1495 if (usb_device_add(devname
) < 0) {
1496 error_report("could not add USB device '%s'", devname
);
1500 void do_usb_del(Monitor
*mon
, const QDict
*qdict
)
1502 const char *devname
= qdict_get_str(qdict
, "devname");
1503 if (usb_device_del(devname
) < 0) {
1504 error_report("could not delete USB device '%s'", devname
);
1508 /***********************************************************/
1509 /* PCMCIA/Cardbus */
1511 static struct pcmcia_socket_entry_s
{
1512 PCMCIASocket
*socket
;
1513 struct pcmcia_socket_entry_s
*next
;
1514 } *pcmcia_sockets
= 0;
1516 void pcmcia_socket_register(PCMCIASocket
*socket
)
1518 struct pcmcia_socket_entry_s
*entry
;
1520 entry
= g_malloc(sizeof(struct pcmcia_socket_entry_s
));
1521 entry
->socket
= socket
;
1522 entry
->next
= pcmcia_sockets
;
1523 pcmcia_sockets
= entry
;
1526 void pcmcia_socket_unregister(PCMCIASocket
*socket
)
1528 struct pcmcia_socket_entry_s
*entry
, **ptr
;
1530 ptr
= &pcmcia_sockets
;
1531 for (entry
= *ptr
; entry
; ptr
= &entry
->next
, entry
= *ptr
)
1532 if (entry
->socket
== socket
) {
1538 void pcmcia_info(Monitor
*mon
, const QDict
*qdict
)
1540 struct pcmcia_socket_entry_s
*iter
;
1542 if (!pcmcia_sockets
)
1543 monitor_printf(mon
, "No PCMCIA sockets\n");
1545 for (iter
= pcmcia_sockets
; iter
; iter
= iter
->next
)
1546 monitor_printf(mon
, "%s: %s\n", iter
->socket
->slot_string
,
1547 iter
->socket
->attached
? iter
->socket
->card_string
:
1551 /***********************************************************/
1552 /* machine registration */
1554 MachineState
*current_machine
;
1556 static void machine_class_init(ObjectClass
*oc
, void *data
)
1558 MachineClass
*mc
= MACHINE_CLASS(oc
);
1559 QEMUMachine
*qm
= data
;
1561 mc
->name
= qm
->name
;
1562 mc
->alias
= qm
->alias
;
1563 mc
->desc
= qm
->desc
;
1564 mc
->init
= qm
->init
;
1565 mc
->reset
= qm
->reset
;
1566 mc
->hot_add_cpu
= qm
->hot_add_cpu
;
1567 mc
->kvm_type
= qm
->kvm_type
;
1568 mc
->block_default_type
= qm
->block_default_type
;
1569 mc
->max_cpus
= qm
->max_cpus
;
1570 mc
->no_serial
= qm
->no_serial
;
1571 mc
->no_parallel
= qm
->no_parallel
;
1572 mc
->use_virtcon
= qm
->use_virtcon
;
1573 mc
->use_sclp
= qm
->use_sclp
;
1574 mc
->no_floppy
= qm
->no_floppy
;
1575 mc
->no_cdrom
= qm
->no_cdrom
;
1576 mc
->no_sdcard
= qm
->no_sdcard
;
1577 mc
->is_default
= qm
->is_default
;
1578 mc
->default_machine_opts
= qm
->default_machine_opts
;
1579 mc
->default_boot_order
= qm
->default_boot_order
;
1580 mc
->compat_props
= qm
->compat_props
;
1581 mc
->hw_version
= qm
->hw_version
;
1584 int qemu_register_machine(QEMUMachine
*m
)
1586 char *name
= g_strconcat(m
->name
, TYPE_MACHINE_SUFFIX
, NULL
);
1589 .parent
= TYPE_MACHINE
,
1590 .class_init
= machine_class_init
,
1591 .class_data
= (void *)m
,
1600 static MachineClass
*find_machine(const char *name
)
1602 GSList
*el
, *machines
= object_class_get_list(TYPE_MACHINE
, false);
1603 MachineClass
*mc
= NULL
;
1605 for (el
= machines
; el
; el
= el
->next
) {
1606 MachineClass
*temp
= el
->data
;
1608 if (!strcmp(temp
->name
, name
)) {
1613 !strcmp(temp
->alias
, name
)) {
1619 g_slist_free(machines
);
1623 MachineClass
*find_default_machine(void)
1625 GSList
*el
, *machines
= object_class_get_list(TYPE_MACHINE
, false);
1626 MachineClass
*mc
= NULL
;
1628 for (el
= machines
; el
; el
= el
->next
) {
1629 MachineClass
*temp
= el
->data
;
1631 if (temp
->is_default
) {
1637 g_slist_free(machines
);
1641 MachineInfoList
*qmp_query_machines(Error
**errp
)
1643 GSList
*el
, *machines
= object_class_get_list(TYPE_MACHINE
, false);
1644 MachineInfoList
*mach_list
= NULL
;
1646 for (el
= machines
; el
; el
= el
->next
) {
1647 MachineClass
*mc
= el
->data
;
1648 MachineInfoList
*entry
;
1651 info
= g_malloc0(sizeof(*info
));
1652 if (mc
->is_default
) {
1653 info
->has_is_default
= true;
1654 info
->is_default
= true;
1658 info
->has_alias
= true;
1659 info
->alias
= g_strdup(mc
->alias
);
1662 info
->name
= g_strdup(mc
->name
);
1663 info
->cpu_max
= !mc
->max_cpus
? 1 : mc
->max_cpus
;
1665 entry
= g_malloc0(sizeof(*entry
));
1666 entry
->value
= info
;
1667 entry
->next
= mach_list
;
1671 g_slist_free(machines
);
1675 /***********************************************************/
1676 /* main execution loop */
1678 struct vm_change_state_entry
{
1679 VMChangeStateHandler
*cb
;
1681 QLIST_ENTRY (vm_change_state_entry
) entries
;
1684 static QLIST_HEAD(vm_change_state_head
, vm_change_state_entry
) vm_change_state_head
;
1686 VMChangeStateEntry
*qemu_add_vm_change_state_handler(VMChangeStateHandler
*cb
,
1689 VMChangeStateEntry
*e
;
1691 e
= g_malloc0(sizeof (*e
));
1695 QLIST_INSERT_HEAD(&vm_change_state_head
, e
, entries
);
1699 void qemu_del_vm_change_state_handler(VMChangeStateEntry
*e
)
1701 QLIST_REMOVE (e
, entries
);
1705 void vm_state_notify(int running
, RunState state
)
1707 VMChangeStateEntry
*e
;
1709 trace_vm_state_notify(running
, state
);
1711 for (e
= vm_change_state_head
.lh_first
; e
; e
= e
->entries
.le_next
) {
1712 e
->cb(e
->opaque
, running
, state
);
1716 /* reset/shutdown handler */
1718 typedef struct QEMUResetEntry
{
1719 QTAILQ_ENTRY(QEMUResetEntry
) entry
;
1720 QEMUResetHandler
*func
;
1724 static QTAILQ_HEAD(reset_handlers
, QEMUResetEntry
) reset_handlers
=
1725 QTAILQ_HEAD_INITIALIZER(reset_handlers
);
1726 static int reset_requested
;
1727 static int shutdown_requested
, shutdown_signal
= -1;
1728 static pid_t shutdown_pid
;
1729 static int powerdown_requested
;
1730 static int debug_requested
;
1731 static int suspend_requested
;
1732 static WakeupReason wakeup_reason
;
1733 static NotifierList powerdown_notifiers
=
1734 NOTIFIER_LIST_INITIALIZER(powerdown_notifiers
);
1735 static NotifierList suspend_notifiers
=
1736 NOTIFIER_LIST_INITIALIZER(suspend_notifiers
);
1737 static NotifierList wakeup_notifiers
=
1738 NOTIFIER_LIST_INITIALIZER(wakeup_notifiers
);
1739 static uint32_t wakeup_reason_mask
= ~(1 << QEMU_WAKEUP_REASON_NONE
);
1741 int qemu_shutdown_requested_get(void)
1743 return shutdown_requested
;
1746 int qemu_reset_requested_get(void)
1748 return reset_requested
;
1751 static int qemu_shutdown_requested(void)
1753 int r
= shutdown_requested
;
1754 shutdown_requested
= 0;
1758 static void qemu_kill_report(void)
1760 if (!qtest_driver() && shutdown_signal
!= -1) {
1761 fprintf(stderr
, "qemu: terminating on signal %d", shutdown_signal
);
1762 if (shutdown_pid
== 0) {
1763 /* This happens for eg ^C at the terminal, so it's worth
1764 * avoiding printing an odd message in that case.
1766 fputc('\n', stderr
);
1768 fprintf(stderr
, " from pid " FMT_pid
"\n", shutdown_pid
);
1770 shutdown_signal
= -1;
1774 static int qemu_reset_requested(void)
1776 int r
= reset_requested
;
1777 reset_requested
= 0;
1781 static int qemu_suspend_requested(void)
1783 int r
= suspend_requested
;
1784 suspend_requested
= 0;
1788 static WakeupReason
qemu_wakeup_requested(void)
1790 return wakeup_reason
;
1793 static int qemu_powerdown_requested(void)
1795 int r
= powerdown_requested
;
1796 powerdown_requested
= 0;
1800 static int qemu_debug_requested(void)
1802 int r
= debug_requested
;
1803 debug_requested
= 0;
1807 void qemu_register_reset(QEMUResetHandler
*func
, void *opaque
)
1809 QEMUResetEntry
*re
= g_malloc0(sizeof(QEMUResetEntry
));
1812 re
->opaque
= opaque
;
1813 QTAILQ_INSERT_TAIL(&reset_handlers
, re
, entry
);
1816 void qemu_unregister_reset(QEMUResetHandler
*func
, void *opaque
)
1820 QTAILQ_FOREACH(re
, &reset_handlers
, entry
) {
1821 if (re
->func
== func
&& re
->opaque
== opaque
) {
1822 QTAILQ_REMOVE(&reset_handlers
, re
, entry
);
1829 void qemu_devices_reset(void)
1831 QEMUResetEntry
*re
, *nre
;
1833 /* reset all devices */
1834 QTAILQ_FOREACH_SAFE(re
, &reset_handlers
, entry
, nre
) {
1835 re
->func(re
->opaque
);
1839 void qemu_system_reset(bool report
)
1843 mc
= current_machine
? MACHINE_GET_CLASS(current_machine
) : NULL
;
1845 if (mc
&& mc
->reset
) {
1848 qemu_devices_reset();
1851 qapi_event_send_reset(&error_abort
);
1853 cpu_synchronize_all_post_reset();
1856 void qemu_system_reset_request(void)
1859 shutdown_requested
= 1;
1861 reset_requested
= 1;
1864 qemu_notify_event();
1867 static void qemu_system_suspend(void)
1870 notifier_list_notify(&suspend_notifiers
, NULL
);
1871 runstate_set(RUN_STATE_SUSPENDED
);
1872 qapi_event_send_suspend(&error_abort
);
1875 void qemu_system_suspend_request(void)
1877 if (runstate_check(RUN_STATE_SUSPENDED
)) {
1880 suspend_requested
= 1;
1882 qemu_notify_event();
1885 void qemu_register_suspend_notifier(Notifier
*notifier
)
1887 notifier_list_add(&suspend_notifiers
, notifier
);
1890 void qemu_system_wakeup_request(WakeupReason reason
)
1892 trace_system_wakeup_request(reason
);
1894 if (!runstate_check(RUN_STATE_SUSPENDED
)) {
1897 if (!(wakeup_reason_mask
& (1 << reason
))) {
1900 runstate_set(RUN_STATE_RUNNING
);
1901 wakeup_reason
= reason
;
1902 qemu_notify_event();
1905 void qemu_system_wakeup_enable(WakeupReason reason
, bool enabled
)
1908 wakeup_reason_mask
|= (1 << reason
);
1910 wakeup_reason_mask
&= ~(1 << reason
);
1914 void qemu_register_wakeup_notifier(Notifier
*notifier
)
1916 notifier_list_add(&wakeup_notifiers
, notifier
);
1919 void qemu_system_killed(int signal
, pid_t pid
)
1921 shutdown_signal
= signal
;
1924 qemu_system_shutdown_request();
1927 void qemu_system_shutdown_request(void)
1929 shutdown_requested
= 1;
1930 qemu_notify_event();
1933 static void qemu_system_powerdown(void)
1935 qapi_event_send_powerdown(&error_abort
);
1936 notifier_list_notify(&powerdown_notifiers
, NULL
);
1939 void qemu_system_powerdown_request(void)
1941 powerdown_requested
= 1;
1942 qemu_notify_event();
1945 void qemu_register_powerdown_notifier(Notifier
*notifier
)
1947 notifier_list_add(&powerdown_notifiers
, notifier
);
1950 void qemu_system_debug_request(void)
1952 debug_requested
= 1;
1953 qemu_notify_event();
1956 static bool main_loop_should_exit(void)
1959 if (qemu_debug_requested()) {
1960 vm_stop(RUN_STATE_DEBUG
);
1962 if (qemu_suspend_requested()) {
1963 qemu_system_suspend();
1965 if (qemu_shutdown_requested()) {
1967 qapi_event_send_shutdown(&error_abort
);
1969 vm_stop(RUN_STATE_SHUTDOWN
);
1974 if (qemu_reset_requested()) {
1976 cpu_synchronize_all_states();
1977 qemu_system_reset(VMRESET_REPORT
);
1979 if (runstate_needs_reset()) {
1980 runstate_set(RUN_STATE_PAUSED
);
1983 if (qemu_wakeup_requested()) {
1985 cpu_synchronize_all_states();
1986 qemu_system_reset(VMRESET_SILENT
);
1987 notifier_list_notify(&wakeup_notifiers
, &wakeup_reason
);
1988 wakeup_reason
= QEMU_WAKEUP_REASON_NONE
;
1990 qapi_event_send_wakeup(&error_abort
);
1992 if (qemu_powerdown_requested()) {
1993 qemu_system_powerdown();
1995 if (qemu_vmstop_requested(&r
)) {
2001 static void main_loop(void)
2005 #ifdef CONFIG_PROFILER
2009 nonblocking
= !kvm_enabled() && !xen_enabled() && last_io
> 0;
2010 #ifdef CONFIG_PROFILER
2011 ti
= profile_getclock();
2013 last_io
= main_loop_wait(nonblocking
);
2014 #ifdef CONFIG_PROFILER
2015 dev_time
+= profile_getclock() - ti
;
2017 } while (!main_loop_should_exit());
2020 static void version(void)
2022 printf("QEMU emulator version " QEMU_VERSION QEMU_PKGVERSION
", Copyright (c) 2003-2008 Fabrice Bellard\n");
2025 static void help(int exitcode
)
2028 printf("usage: %s [options] [disk_image]\n\n"
2029 "'disk_image' is a raw hard disk image for IDE hard disk 0\n\n",
2030 error_get_progname());
2032 #define QEMU_OPTIONS_GENERATE_HELP
2033 #include "qemu-options-wrapper.h"
2035 printf("\nDuring emulation, the following keys are useful:\n"
2036 "ctrl-alt-f toggle full screen\n"
2037 "ctrl-alt-n switch to virtual console 'n'\n"
2038 "ctrl-alt toggle mouse and keyboard grab\n"
2040 "When using -nographic, press 'ctrl-a h' to get some help.\n");
2045 #define HAS_ARG 0x0001
2047 typedef struct QEMUOption
{
2054 static const QEMUOption qemu_options
[] = {
2055 { "h", 0, QEMU_OPTION_h
, QEMU_ARCH_ALL
},
2056 #define QEMU_OPTIONS_GENERATE_OPTIONS
2057 #include "qemu-options-wrapper.h"
2061 static bool vga_available(void)
2063 return object_class_by_name("VGA") || object_class_by_name("isa-vga");
2066 static bool cirrus_vga_available(void)
2068 return object_class_by_name("cirrus-vga")
2069 || object_class_by_name("isa-cirrus-vga");
2072 static bool vmware_vga_available(void)
2074 return object_class_by_name("vmware-svga");
2077 static bool qxl_vga_available(void)
2079 return object_class_by_name("qxl-vga");
2082 static bool tcx_vga_available(void)
2084 return object_class_by_name("SUNW,tcx");
2087 static bool cg3_vga_available(void)
2089 return object_class_by_name("cgthree");
2092 static void select_vgahw (const char *p
)
2096 assert(vga_interface_type
== VGA_NONE
);
2097 if (strstart(p
, "std", &opts
)) {
2098 if (vga_available()) {
2099 vga_interface_type
= VGA_STD
;
2101 fprintf(stderr
, "Error: standard VGA not available\n");
2104 } else if (strstart(p
, "cirrus", &opts
)) {
2105 if (cirrus_vga_available()) {
2106 vga_interface_type
= VGA_CIRRUS
;
2108 fprintf(stderr
, "Error: Cirrus VGA not available\n");
2111 } else if (strstart(p
, "vmware", &opts
)) {
2112 if (vmware_vga_available()) {
2113 vga_interface_type
= VGA_VMWARE
;
2115 fprintf(stderr
, "Error: VMWare SVGA not available\n");
2118 } else if (strstart(p
, "xenfb", &opts
)) {
2119 vga_interface_type
= VGA_XENFB
;
2120 } else if (strstart(p
, "qxl", &opts
)) {
2121 if (qxl_vga_available()) {
2122 vga_interface_type
= VGA_QXL
;
2124 fprintf(stderr
, "Error: QXL VGA not available\n");
2127 } else if (strstart(p
, "tcx", &opts
)) {
2128 if (tcx_vga_available()) {
2129 vga_interface_type
= VGA_TCX
;
2131 fprintf(stderr
, "Error: TCX framebuffer not available\n");
2134 } else if (strstart(p
, "cg3", &opts
)) {
2135 if (cg3_vga_available()) {
2136 vga_interface_type
= VGA_CG3
;
2138 fprintf(stderr
, "Error: CG3 framebuffer not available\n");
2141 } else if (!strstart(p
, "none", &opts
)) {
2143 fprintf(stderr
, "Unknown vga type: %s\n", p
);
2147 const char *nextopt
;
2149 if (strstart(opts
, ",retrace=", &nextopt
)) {
2151 if (strstart(opts
, "dumb", &nextopt
))
2152 vga_retrace_method
= VGA_RETRACE_DUMB
;
2153 else if (strstart(opts
, "precise", &nextopt
))
2154 vga_retrace_method
= VGA_RETRACE_PRECISE
;
2155 else goto invalid_vga
;
2156 } else goto invalid_vga
;
2161 static DisplayType
select_display(const char *p
)
2164 DisplayType display
= DT_DEFAULT
;
2166 if (strstart(p
, "sdl", &opts
)) {
2170 const char *nextopt
;
2172 if (strstart(opts
, ",frame=", &nextopt
)) {
2174 if (strstart(opts
, "on", &nextopt
)) {
2176 } else if (strstart(opts
, "off", &nextopt
)) {
2179 goto invalid_sdl_args
;
2181 } else if (strstart(opts
, ",alt_grab=", &nextopt
)) {
2183 if (strstart(opts
, "on", &nextopt
)) {
2185 } else if (strstart(opts
, "off", &nextopt
)) {
2188 goto invalid_sdl_args
;
2190 } else if (strstart(opts
, ",ctrl_grab=", &nextopt
)) {
2192 if (strstart(opts
, "on", &nextopt
)) {
2194 } else if (strstart(opts
, "off", &nextopt
)) {
2197 goto invalid_sdl_args
;
2199 } else if (strstart(opts
, ",window_close=", &nextopt
)) {
2201 if (strstart(opts
, "on", &nextopt
)) {
2203 } else if (strstart(opts
, "off", &nextopt
)) {
2206 goto invalid_sdl_args
;
2210 fprintf(stderr
, "Invalid SDL option string: %s\n", p
);
2216 fprintf(stderr
, "SDL support is disabled\n");
2219 } else if (strstart(p
, "vnc", &opts
)) {
2224 const char *nextopt
;
2226 if (strstart(opts
, "=", &nextopt
)) {
2227 vnc_display
= nextopt
;
2231 fprintf(stderr
, "VNC requires a display argument vnc=<display>\n");
2235 fprintf(stderr
, "VNC support is disabled\n");
2238 } else if (strstart(p
, "curses", &opts
)) {
2239 #ifdef CONFIG_CURSES
2240 display
= DT_CURSES
;
2242 fprintf(stderr
, "Curses support is disabled\n");
2245 } else if (strstart(p
, "gtk", &opts
)) {
2249 const char *nextopt
;
2251 if (strstart(opts
, ",grab_on_hover=", &nextopt
)) {
2253 if (strstart(opts
, "on", &nextopt
)) {
2254 grab_on_hover
= true;
2255 } else if (strstart(opts
, "off", &nextopt
)) {
2256 grab_on_hover
= false;
2258 goto invalid_gtk_args
;
2262 fprintf(stderr
, "Invalid GTK option string: %s\n", p
);
2268 fprintf(stderr
, "GTK support is disabled\n");
2271 } else if (strstart(p
, "none", &opts
)) {
2274 fprintf(stderr
, "Unknown display type: %s\n", p
);
2281 static int balloon_parse(const char *arg
)
2285 if (strcmp(arg
, "none") == 0) {
2289 if (!strncmp(arg
, "virtio", 6)) {
2290 if (arg
[6] == ',') {
2291 /* have params -> parse them */
2292 opts
= qemu_opts_parse(qemu_find_opts("device"), arg
+7, 0);
2296 /* create empty opts */
2297 opts
= qemu_opts_create(qemu_find_opts("device"), NULL
, 0,
2300 qemu_opt_set(opts
, "driver", "virtio-balloon");
2307 char *qemu_find_file(int type
, const char *name
)
2313 /* Try the name as a straight path first */
2314 if (access(name
, R_OK
) == 0) {
2315 trace_load_file(name
, name
);
2316 return g_strdup(name
);
2320 case QEMU_FILE_TYPE_BIOS
:
2323 case QEMU_FILE_TYPE_KEYMAP
:
2324 subdir
= "keymaps/";
2330 for (i
= 0; i
< data_dir_idx
; i
++) {
2331 buf
= g_strdup_printf("%s/%s%s", data_dir
[i
], subdir
, name
);
2332 if (access(buf
, R_OK
) == 0) {
2333 trace_load_file(name
, buf
);
2341 static int device_help_func(QemuOpts
*opts
, void *opaque
)
2343 return qdev_device_help(opts
);
2346 static int device_init_func(QemuOpts
*opts
, void *opaque
)
2350 dev
= qdev_device_add(opts
);
2353 object_unref(OBJECT(dev
));
2357 static int chardev_init_func(QemuOpts
*opts
, void *opaque
)
2359 Error
*local_err
= NULL
;
2361 qemu_chr_new_from_opts(opts
, NULL
, &local_err
);
2363 error_report("%s", error_get_pretty(local_err
));
2364 error_free(local_err
);
2370 #ifdef CONFIG_VIRTFS
2371 static int fsdev_init_func(QemuOpts
*opts
, void *opaque
)
2374 ret
= qemu_fsdev_add(opts
);
2380 static int mon_init_func(QemuOpts
*opts
, void *opaque
)
2382 CharDriverState
*chr
;
2383 const char *chardev
;
2387 mode
= qemu_opt_get(opts
, "mode");
2391 if (strcmp(mode
, "readline") == 0) {
2392 flags
= MONITOR_USE_READLINE
;
2393 } else if (strcmp(mode
, "control") == 0) {
2394 flags
= MONITOR_USE_CONTROL
;
2396 fprintf(stderr
, "unknown monitor mode \"%s\"\n", mode
);
2400 if (qemu_opt_get_bool(opts
, "pretty", 0))
2401 flags
|= MONITOR_USE_PRETTY
;
2403 if (qemu_opt_get_bool(opts
, "default", 0))
2404 flags
|= MONITOR_IS_DEFAULT
;
2406 chardev
= qemu_opt_get(opts
, "chardev");
2407 chr
= qemu_chr_find(chardev
);
2409 fprintf(stderr
, "chardev \"%s\" not found\n", chardev
);
2413 qemu_chr_fe_claim_no_fail(chr
);
2414 monitor_init(chr
, flags
);
2418 static void monitor_parse(const char *optarg
, const char *mode
)
2420 static int monitor_device_index
= 0;
2426 if (strstart(optarg
, "chardev:", &p
)) {
2427 snprintf(label
, sizeof(label
), "%s", p
);
2429 snprintf(label
, sizeof(label
), "compat_monitor%d",
2430 monitor_device_index
);
2431 if (monitor_device_index
== 0) {
2434 opts
= qemu_chr_parse_compat(label
, optarg
);
2436 fprintf(stderr
, "parse error: %s\n", optarg
);
2441 opts
= qemu_opts_create(qemu_find_opts("mon"), label
, 1, NULL
);
2443 fprintf(stderr
, "duplicate chardev: %s\n", label
);
2446 qemu_opt_set(opts
, "mode", mode
);
2447 qemu_opt_set(opts
, "chardev", label
);
2449 qemu_opt_set(opts
, "default", "on");
2450 monitor_device_index
++;
2453 struct device_config
{
2455 DEV_USB
, /* -usbdevice */
2457 DEV_SERIAL
, /* -serial */
2458 DEV_PARALLEL
, /* -parallel */
2459 DEV_VIRTCON
, /* -virtioconsole */
2460 DEV_DEBUGCON
, /* -debugcon */
2461 DEV_GDB
, /* -gdb, -s */
2462 DEV_SCLP
, /* s390 sclp */
2464 const char *cmdline
;
2466 QTAILQ_ENTRY(device_config
) next
;
2469 static QTAILQ_HEAD(, device_config
) device_configs
=
2470 QTAILQ_HEAD_INITIALIZER(device_configs
);
2472 static void add_device_config(int type
, const char *cmdline
)
2474 struct device_config
*conf
;
2476 conf
= g_malloc0(sizeof(*conf
));
2478 conf
->cmdline
= cmdline
;
2479 loc_save(&conf
->loc
);
2480 QTAILQ_INSERT_TAIL(&device_configs
, conf
, next
);
2483 static int foreach_device_config(int type
, int (*func
)(const char *cmdline
))
2485 struct device_config
*conf
;
2488 QTAILQ_FOREACH(conf
, &device_configs
, next
) {
2489 if (conf
->type
!= type
)
2491 loc_push_restore(&conf
->loc
);
2492 rc
= func(conf
->cmdline
);
2493 loc_pop(&conf
->loc
);
2500 static int serial_parse(const char *devname
)
2502 static int index
= 0;
2505 if (strcmp(devname
, "none") == 0)
2507 if (index
== MAX_SERIAL_PORTS
) {
2508 fprintf(stderr
, "qemu: too many serial ports\n");
2511 snprintf(label
, sizeof(label
), "serial%d", index
);
2512 serial_hds
[index
] = qemu_chr_new(label
, devname
, NULL
);
2513 if (!serial_hds
[index
]) {
2514 fprintf(stderr
, "qemu: could not connect serial device"
2515 " to character backend '%s'\n", devname
);
2522 static int parallel_parse(const char *devname
)
2524 static int index
= 0;
2527 if (strcmp(devname
, "none") == 0)
2529 if (index
== MAX_PARALLEL_PORTS
) {
2530 fprintf(stderr
, "qemu: too many parallel ports\n");
2533 snprintf(label
, sizeof(label
), "parallel%d", index
);
2534 parallel_hds
[index
] = qemu_chr_new(label
, devname
, NULL
);
2535 if (!parallel_hds
[index
]) {
2536 fprintf(stderr
, "qemu: could not connect parallel device"
2537 " to character backend '%s'\n", devname
);
2544 static int virtcon_parse(const char *devname
)
2546 QemuOptsList
*device
= qemu_find_opts("device");
2547 static int index
= 0;
2549 QemuOpts
*bus_opts
, *dev_opts
;
2551 if (strcmp(devname
, "none") == 0)
2553 if (index
== MAX_VIRTIO_CONSOLES
) {
2554 fprintf(stderr
, "qemu: too many virtio consoles\n");
2558 bus_opts
= qemu_opts_create(device
, NULL
, 0, &error_abort
);
2559 if (arch_type
== QEMU_ARCH_S390X
) {
2560 qemu_opt_set(bus_opts
, "driver", "virtio-serial-s390");
2562 qemu_opt_set(bus_opts
, "driver", "virtio-serial-pci");
2565 dev_opts
= qemu_opts_create(device
, NULL
, 0, &error_abort
);
2566 qemu_opt_set(dev_opts
, "driver", "virtconsole");
2568 snprintf(label
, sizeof(label
), "virtcon%d", index
);
2569 virtcon_hds
[index
] = qemu_chr_new(label
, devname
, NULL
);
2570 if (!virtcon_hds
[index
]) {
2571 fprintf(stderr
, "qemu: could not connect virtio console"
2572 " to character backend '%s'\n", devname
);
2575 qemu_opt_set(dev_opts
, "chardev", label
);
2581 static int sclp_parse(const char *devname
)
2583 QemuOptsList
*device
= qemu_find_opts("device");
2584 static int index
= 0;
2588 if (strcmp(devname
, "none") == 0) {
2591 if (index
== MAX_SCLP_CONSOLES
) {
2592 fprintf(stderr
, "qemu: too many sclp consoles\n");
2596 assert(arch_type
== QEMU_ARCH_S390X
);
2598 dev_opts
= qemu_opts_create(device
, NULL
, 0, NULL
);
2599 qemu_opt_set(dev_opts
, "driver", "sclpconsole");
2601 snprintf(label
, sizeof(label
), "sclpcon%d", index
);
2602 sclp_hds
[index
] = qemu_chr_new(label
, devname
, NULL
);
2603 if (!sclp_hds
[index
]) {
2604 fprintf(stderr
, "qemu: could not connect sclp console"
2605 " to character backend '%s'\n", devname
);
2608 qemu_opt_set(dev_opts
, "chardev", label
);
2614 static int debugcon_parse(const char *devname
)
2618 if (!qemu_chr_new("debugcon", devname
, NULL
)) {
2621 opts
= qemu_opts_create(qemu_find_opts("device"), "debugcon", 1, NULL
);
2623 fprintf(stderr
, "qemu: already have a debugcon device\n");
2626 qemu_opt_set(opts
, "driver", "isa-debugcon");
2627 qemu_opt_set(opts
, "chardev", "debugcon");
2631 static MachineClass
*machine_parse(const char *name
)
2633 MachineClass
*mc
= NULL
;
2634 GSList
*el
, *machines
= object_class_get_list(TYPE_MACHINE
, false);
2637 mc
= find_machine(name
);
2642 if (name
&& !is_help_option(name
)) {
2643 error_report("Unsupported machine type");
2644 error_printf("Use -machine help to list supported machines!\n");
2646 printf("Supported machines are:\n");
2647 for (el
= machines
; el
; el
= el
->next
) {
2648 MachineClass
*mc
= el
->data
;
2650 printf("%-20s %s (alias of %s)\n", mc
->alias
, mc
->desc
, mc
->name
);
2652 printf("%-20s %s%s\n", mc
->name
, mc
->desc
,
2653 mc
->is_default
? " (default)" : "");
2657 g_slist_free(machines
);
2658 exit(!name
|| !is_help_option(name
));
2661 static int tcg_init(MachineClass
*mc
)
2663 tcg_exec_init(tcg_tb_size
* 1024 * 1024);
2668 const char *opt_name
;
2670 int (*available
)(void);
2671 int (*init
)(MachineClass
*mc
);
2674 { "tcg", "tcg", tcg_available
, tcg_init
, &tcg_allowed
},
2675 { "xen", "Xen", xen_available
, xen_init
, &xen_allowed
},
2676 { "kvm", "KVM", kvm_available
, kvm_init
, &kvm_allowed
},
2677 { "qtest", "QTest", qtest_available
, qtest_init_accel
, &qtest_allowed
},
2680 static int configure_accelerator(MachineClass
*mc
)
2685 bool accel_initialised
= false;
2686 bool init_failed
= false;
2688 p
= qemu_opt_get(qemu_get_machine_opts(), "accel");
2690 /* Use the default "accelerator", tcg */
2694 while (!accel_initialised
&& *p
!= '\0') {
2698 p
= get_opt_name(buf
, sizeof (buf
), p
, ':');
2699 for (i
= 0; i
< ARRAY_SIZE(accel_list
); i
++) {
2700 if (strcmp(accel_list
[i
].opt_name
, buf
) == 0) {
2701 if (!accel_list
[i
].available()) {
2702 printf("%s not supported for this target\n",
2703 accel_list
[i
].name
);
2706 *(accel_list
[i
].allowed
) = true;
2707 ret
= accel_list
[i
].init(mc
);
2710 fprintf(stderr
, "failed to initialize %s: %s\n",
2713 *(accel_list
[i
].allowed
) = false;
2715 accel_initialised
= true;
2720 if (i
== ARRAY_SIZE(accel_list
)) {
2721 fprintf(stderr
, "\"%s\" accelerator does not exist.\n", buf
);
2725 if (!accel_initialised
) {
2727 fprintf(stderr
, "No accelerator found!\n");
2733 fprintf(stderr
, "Back to %s accelerator.\n", accel_list
[i
].name
);
2736 return !accel_initialised
;
2739 void qemu_add_exit_notifier(Notifier
*notify
)
2741 notifier_list_add(&exit_notifiers
, notify
);
2744 void qemu_remove_exit_notifier(Notifier
*notify
)
2746 notifier_remove(notify
);
2749 static void qemu_run_exit_notifiers(void)
2751 notifier_list_notify(&exit_notifiers
, NULL
);
2754 void qemu_add_machine_init_done_notifier(Notifier
*notify
)
2756 notifier_list_add(&machine_init_done_notifiers
, notify
);
2759 static void qemu_run_machine_init_done_notifiers(void)
2761 notifier_list_notify(&machine_init_done_notifiers
, NULL
);
2764 static const QEMUOption
*lookup_opt(int argc
, char **argv
,
2765 const char **poptarg
, int *poptind
)
2767 const QEMUOption
*popt
;
2768 int optind
= *poptind
;
2769 char *r
= argv
[optind
];
2772 loc_set_cmdline(argv
, optind
, 1);
2774 /* Treat --foo the same as -foo. */
2777 popt
= qemu_options
;
2780 error_report("invalid option");
2783 if (!strcmp(popt
->name
, r
+ 1))
2787 if (popt
->flags
& HAS_ARG
) {
2788 if (optind
>= argc
) {
2789 error_report("requires an argument");
2792 optarg
= argv
[optind
++];
2793 loc_set_cmdline(argv
, optind
- 2, 2);
2804 static gpointer
malloc_and_trace(gsize n_bytes
)
2806 void *ptr
= malloc(n_bytes
);
2807 trace_g_malloc(n_bytes
, ptr
);
2811 static gpointer
realloc_and_trace(gpointer mem
, gsize n_bytes
)
2813 void *ptr
= realloc(mem
, n_bytes
);
2814 trace_g_realloc(mem
, n_bytes
, ptr
);
2818 static void free_and_trace(gpointer mem
)
2824 static int object_set_property(const char *name
, const char *value
, void *opaque
)
2826 Object
*obj
= OBJECT(opaque
);
2827 StringInputVisitor
*siv
;
2828 Error
*local_err
= NULL
;
2830 if (strcmp(name
, "qom-type") == 0 || strcmp(name
, "id") == 0 ||
2831 strcmp(name
, "type") == 0) {
2835 siv
= string_input_visitor_new(value
);
2836 object_property_set(obj
, string_input_get_visitor(siv
), name
, &local_err
);
2837 string_input_visitor_cleanup(siv
);
2840 qerror_report_err(local_err
);
2841 error_free(local_err
);
2848 static int object_create(QemuOpts
*opts
, void *opaque
)
2857 ov
= opts_visitor_new(opts
);
2858 pdict
= qemu_opts_to_qdict(opts
, NULL
);
2860 visit_start_struct(opts_get_visitor(ov
), &dummy
, NULL
, NULL
, 0, &err
);
2865 qdict_del(pdict
, "qom-type");
2866 visit_type_str(opts_get_visitor(ov
), &type
, "qom-type", &err
);
2871 qdict_del(pdict
, "id");
2872 visit_type_str(opts_get_visitor(ov
), &id
, "id", &err
);
2877 object_add(type
, id
, pdict
, opts_get_visitor(ov
), &err
);
2881 visit_end_struct(opts_get_visitor(ov
), &err
);
2883 qmp_object_del(id
, NULL
);
2887 opts_visitor_cleanup(ov
);
2894 qerror_report_err(err
);
2900 int main(int argc
, char **argv
, char **envp
)
2903 int snapshot
, linux_boot
;
2904 const char *icount_option
= NULL
;
2905 const char *initrd_filename
;
2906 const char *kernel_filename
, *kernel_cmdline
;
2907 const char *boot_order
;
2909 int cyls
, heads
, secs
, translation
;
2910 QemuOpts
*hda_opts
= NULL
, *opts
, *machine_opts
;
2911 QemuOptsList
*olist
;
2914 const char *loadvm
= NULL
;
2915 MachineClass
*machine_class
;
2916 const char *cpu_model
;
2917 const char *vga_model
= NULL
;
2918 const char *qtest_chrdev
= NULL
;
2919 const char *qtest_log
= NULL
;
2920 const char *pid_file
= NULL
;
2921 const char *incoming
= NULL
;
2923 int show_vnc_port
= 0;
2925 bool defconfig
= true;
2926 bool userconfig
= true;
2927 const char *log_mask
= NULL
;
2928 const char *log_file
= NULL
;
2929 GMemVTable mem_trace
= {
2930 .malloc
= malloc_and_trace
,
2931 .realloc
= realloc_and_trace
,
2932 .free
= free_and_trace
,
2934 const char *trace_events
= NULL
;
2935 const char *trace_file
= NULL
;
2936 const ram_addr_t default_ram_size
= (ram_addr_t
)DEFAULT_RAM_SIZE
*
2938 ram_addr_t maxram_size
= default_ram_size
;
2939 uint64_t ram_slots
= 0;
2941 atexit(qemu_run_exit_notifiers
);
2942 error_set_progname(argv
[0]);
2943 qemu_init_exec_dir(argv
[0]);
2945 g_mem_set_vtable(&mem_trace
);
2947 module_call_init(MODULE_INIT_QOM
);
2949 qemu_add_opts(&qemu_drive_opts
);
2950 qemu_add_drive_opts(&qemu_legacy_drive_opts
);
2951 qemu_add_drive_opts(&qemu_common_drive_opts
);
2952 qemu_add_drive_opts(&qemu_drive_opts
);
2953 qemu_add_opts(&qemu_chardev_opts
);
2954 qemu_add_opts(&qemu_device_opts
);
2955 qemu_add_opts(&qemu_netdev_opts
);
2956 qemu_add_opts(&qemu_net_opts
);
2957 qemu_add_opts(&qemu_rtc_opts
);
2958 qemu_add_opts(&qemu_global_opts
);
2959 qemu_add_opts(&qemu_mon_opts
);
2960 qemu_add_opts(&qemu_trace_opts
);
2961 qemu_add_opts(&qemu_option_rom_opts
);
2962 qemu_add_opts(&qemu_machine_opts
);
2963 qemu_add_opts(&qemu_mem_opts
);
2964 qemu_add_opts(&qemu_smp_opts
);
2965 qemu_add_opts(&qemu_boot_opts
);
2966 qemu_add_opts(&qemu_sandbox_opts
);
2967 qemu_add_opts(&qemu_add_fd_opts
);
2968 qemu_add_opts(&qemu_object_opts
);
2969 qemu_add_opts(&qemu_tpmdev_opts
);
2970 qemu_add_opts(&qemu_realtime_opts
);
2971 qemu_add_opts(&qemu_msg_opts
);
2972 qemu_add_opts(&qemu_name_opts
);
2973 qemu_add_opts(&qemu_numa_opts
);
2977 rtc_clock
= QEMU_CLOCK_HOST
;
2979 qemu_init_auxval(envp
);
2980 qemu_cache_utils_init();
2982 QLIST_INIT (&vm_change_state_head
);
2983 os_setup_early_signal_handling();
2985 module_call_init(MODULE_INIT_MACHINE
);
2986 machine_class
= find_default_machine();
2988 ram_size
= default_ram_size
;
2990 cyls
= heads
= secs
= 0;
2991 translation
= BIOS_ATA_TRANSLATION_AUTO
;
2993 for (i
= 0; i
< MAX_NODES
; i
++) {
2994 numa_info
[i
].node_mem
= 0;
2995 bitmap_zero(numa_info
[i
].node_cpu
, MAX_CPUMASK_BITS
);
3001 bdrv_init_with_whitelist();
3005 /* first pass of option parsing */
3007 while (optind
< argc
) {
3008 if (argv
[optind
][0] != '-') {
3012 const QEMUOption
*popt
;
3014 popt
= lookup_opt(argc
, argv
, &optarg
, &optind
);
3015 switch (popt
->index
) {
3016 case QEMU_OPTION_nodefconfig
:
3019 case QEMU_OPTION_nouserconfig
:
3028 ret
= qemu_read_default_config_files(userconfig
);
3034 /* second pass of option parsing */
3039 if (argv
[optind
][0] != '-') {
3040 hda_opts
= drive_add(IF_DEFAULT
, 0, argv
[optind
++], HD_OPTS
);
3042 const QEMUOption
*popt
;
3044 popt
= lookup_opt(argc
, argv
, &optarg
, &optind
);
3045 if (!(popt
->arch_mask
& arch_type
)) {
3046 printf("Option %s not supported for this target\n", popt
->name
);
3049 switch(popt
->index
) {
3051 machine_class
= machine_parse(optarg
);
3053 case QEMU_OPTION_no_kvm_irqchip
: {
3054 olist
= qemu_find_opts("machine");
3055 qemu_opts_parse(olist
, "kernel_irqchip=off", 0);
3058 case QEMU_OPTION_cpu
:
3059 /* hw initialization will check this */
3062 case QEMU_OPTION_hda
:
3066 snprintf(buf
, sizeof(buf
), "%s", HD_OPTS
);
3068 snprintf(buf
, sizeof(buf
),
3069 "%s,cyls=%d,heads=%d,secs=%d%s",
3070 HD_OPTS
, cyls
, heads
, secs
,
3071 translation
== BIOS_ATA_TRANSLATION_LBA
?
3073 translation
== BIOS_ATA_TRANSLATION_NONE
?
3074 ",trans=none" : "");
3075 drive_add(IF_DEFAULT
, 0, optarg
, buf
);
3078 case QEMU_OPTION_hdb
:
3079 case QEMU_OPTION_hdc
:
3080 case QEMU_OPTION_hdd
:
3081 drive_add(IF_DEFAULT
, popt
->index
- QEMU_OPTION_hda
, optarg
,
3084 case QEMU_OPTION_drive
:
3085 if (drive_def(optarg
) == NULL
) {
3089 case QEMU_OPTION_set
:
3090 if (qemu_set_option(optarg
) != 0)
3093 case QEMU_OPTION_global
:
3094 if (qemu_global_option(optarg
) != 0)
3097 case QEMU_OPTION_mtdblock
:
3098 drive_add(IF_MTD
, -1, optarg
, MTD_OPTS
);
3100 case QEMU_OPTION_sd
:
3101 drive_add(IF_SD
, -1, optarg
, SD_OPTS
);
3103 case QEMU_OPTION_pflash
:
3104 drive_add(IF_PFLASH
, -1, optarg
, PFLASH_OPTS
);
3106 case QEMU_OPTION_snapshot
:
3109 case QEMU_OPTION_hdachs
:
3113 cyls
= strtol(p
, (char **)&p
, 0);
3114 if (cyls
< 1 || cyls
> 16383)
3119 heads
= strtol(p
, (char **)&p
, 0);
3120 if (heads
< 1 || heads
> 16)
3125 secs
= strtol(p
, (char **)&p
, 0);
3126 if (secs
< 1 || secs
> 63)
3130 if (!strcmp(p
, "large")) {
3131 translation
= BIOS_ATA_TRANSLATION_LARGE
;
3132 } else if (!strcmp(p
, "rechs")) {
3133 translation
= BIOS_ATA_TRANSLATION_RECHS
;
3134 } else if (!strcmp(p
, "none")) {
3135 translation
= BIOS_ATA_TRANSLATION_NONE
;
3136 } else if (!strcmp(p
, "lba")) {
3137 translation
= BIOS_ATA_TRANSLATION_LBA
;
3138 } else if (!strcmp(p
, "auto")) {
3139 translation
= BIOS_ATA_TRANSLATION_AUTO
;
3143 } else if (*p
!= '\0') {
3145 fprintf(stderr
, "qemu: invalid physical CHS format\n");
3148 if (hda_opts
!= NULL
) {
3150 snprintf(num
, sizeof(num
), "%d", cyls
);
3151 qemu_opt_set(hda_opts
, "cyls", num
);
3152 snprintf(num
, sizeof(num
), "%d", heads
);
3153 qemu_opt_set(hda_opts
, "heads", num
);
3154 snprintf(num
, sizeof(num
), "%d", secs
);
3155 qemu_opt_set(hda_opts
, "secs", num
);
3156 if (translation
== BIOS_ATA_TRANSLATION_LARGE
) {
3157 qemu_opt_set(hda_opts
, "trans", "large");
3158 } else if (translation
== BIOS_ATA_TRANSLATION_RECHS
) {
3159 qemu_opt_set(hda_opts
, "trans", "rechs");
3160 } else if (translation
== BIOS_ATA_TRANSLATION_LBA
) {
3161 qemu_opt_set(hda_opts
, "trans", "lba");
3162 } else if (translation
== BIOS_ATA_TRANSLATION_NONE
) {
3163 qemu_opt_set(hda_opts
, "trans", "none");
3168 case QEMU_OPTION_numa
:
3169 opts
= qemu_opts_parse(qemu_find_opts("numa"), optarg
, 1);
3174 case QEMU_OPTION_display
:
3175 display_type
= select_display(optarg
);
3177 case QEMU_OPTION_nographic
:
3178 display_type
= DT_NOGRAPHIC
;
3180 case QEMU_OPTION_curses
:
3181 #ifdef CONFIG_CURSES
3182 display_type
= DT_CURSES
;
3184 fprintf(stderr
, "Curses support is disabled\n");
3188 case QEMU_OPTION_portrait
:
3189 graphic_rotate
= 90;
3191 case QEMU_OPTION_rotate
:
3192 graphic_rotate
= strtol(optarg
, (char **) &optarg
, 10);
3193 if (graphic_rotate
!= 0 && graphic_rotate
!= 90 &&
3194 graphic_rotate
!= 180 && graphic_rotate
!= 270) {
3196 "qemu: only 90, 180, 270 deg rotation is available\n");
3200 case QEMU_OPTION_kernel
:
3201 qemu_opts_set(qemu_find_opts("machine"), 0, "kernel", optarg
);
3203 case QEMU_OPTION_initrd
:
3204 qemu_opts_set(qemu_find_opts("machine"), 0, "initrd", optarg
);
3206 case QEMU_OPTION_append
:
3207 qemu_opts_set(qemu_find_opts("machine"), 0, "append", optarg
);
3209 case QEMU_OPTION_dtb
:
3210 qemu_opts_set(qemu_find_opts("machine"), 0, "dtb", optarg
);
3212 case QEMU_OPTION_cdrom
:
3213 drive_add(IF_DEFAULT
, 2, optarg
, CDROM_OPTS
);
3215 case QEMU_OPTION_boot
:
3216 opts
= qemu_opts_parse(qemu_find_opts("boot-opts"), optarg
, 1);
3221 case QEMU_OPTION_fda
:
3222 case QEMU_OPTION_fdb
:
3223 drive_add(IF_FLOPPY
, popt
->index
- QEMU_OPTION_fda
,
3226 case QEMU_OPTION_no_fd_bootchk
:
3229 case QEMU_OPTION_netdev
:
3230 if (net_client_parse(qemu_find_opts("netdev"), optarg
) == -1) {
3234 case QEMU_OPTION_net
:
3235 if (net_client_parse(qemu_find_opts("net"), optarg
) == -1) {
3239 #ifdef CONFIG_LIBISCSI
3240 case QEMU_OPTION_iscsi
:
3241 opts
= qemu_opts_parse(qemu_find_opts("iscsi"), optarg
, 0);
3248 case QEMU_OPTION_tftp
:
3249 legacy_tftp_prefix
= optarg
;
3251 case QEMU_OPTION_bootp
:
3252 legacy_bootp_filename
= optarg
;
3254 case QEMU_OPTION_redir
:
3255 if (net_slirp_redir(optarg
) < 0)
3259 case QEMU_OPTION_bt
:
3260 add_device_config(DEV_BT
, optarg
);
3262 case QEMU_OPTION_audio_help
:
3266 case QEMU_OPTION_soundhw
:
3267 select_soundhw (optarg
);
3272 case QEMU_OPTION_version
:
3276 case QEMU_OPTION_m
: {
3278 const char *mem_str
;
3279 const char *maxmem_str
, *slots_str
;
3281 opts
= qemu_opts_parse(qemu_find_opts("memory"),
3287 mem_str
= qemu_opt_get(opts
, "size");
3289 error_report("invalid -m option, missing 'size' option");
3293 error_report("missing 'size' option value");
3297 sz
= qemu_opt_get_size(opts
, "size", ram_size
);
3299 /* Fix up legacy suffix-less format */
3300 if (g_ascii_isdigit(mem_str
[strlen(mem_str
) - 1])) {
3301 uint64_t overflow_check
= sz
;
3304 if ((sz
>> 20) != overflow_check
) {
3305 error_report("too large 'size' option value");
3310 /* backward compatibility behaviour for case "-m 0" */
3312 sz
= default_ram_size
;
3315 sz
= QEMU_ALIGN_UP(sz
, 8192);
3317 if (ram_size
!= sz
) {
3318 error_report("ram size too large");
3322 maxmem_str
= qemu_opt_get(opts
, "maxmem");
3323 slots_str
= qemu_opt_get(opts
, "slots");
3324 if (maxmem_str
&& slots_str
) {
3327 sz
= qemu_opt_get_size(opts
, "maxmem", 0);
3328 if (sz
< ram_size
) {
3329 fprintf(stderr
, "qemu: invalid -m option value: maxmem "
3330 "(%" PRIu64
") <= initial memory ("
3331 RAM_ADDR_FMT
")\n", sz
, ram_size
);
3335 slots
= qemu_opt_get_number(opts
, "slots", 0);
3336 if ((sz
> ram_size
) && !slots
) {
3337 fprintf(stderr
, "qemu: invalid -m option value: maxmem "
3338 "(%" PRIu64
") more than initial memory ("
3339 RAM_ADDR_FMT
") but no hotplug slots where "
3340 "specified\n", sz
, ram_size
);
3344 if ((sz
<= ram_size
) && slots
) {
3345 fprintf(stderr
, "qemu: invalid -m option value: %"
3346 PRIu64
" hotplug slots where specified but "
3347 "maxmem (%" PRIu64
") <= initial memory ("
3348 RAM_ADDR_FMT
")\n", slots
, sz
, ram_size
);
3353 } else if ((!maxmem_str
&& slots_str
) ||
3354 (maxmem_str
&& !slots_str
)) {
3355 fprintf(stderr
, "qemu: invalid -m option value: missing "
3356 "'%s' option\n", slots_str
? "maxmem" : "slots");
3362 case QEMU_OPTION_tpmdev
:
3363 if (tpm_config_parse(qemu_find_opts("tpmdev"), optarg
) < 0) {
3368 case QEMU_OPTION_mempath
:
3371 case QEMU_OPTION_mem_prealloc
:
3381 add_device_config(DEV_GDB
, "tcp::" DEFAULT_GDBSTUB_PORT
);
3383 case QEMU_OPTION_gdb
:
3384 add_device_config(DEV_GDB
, optarg
);
3387 if (data_dir_idx
< ARRAY_SIZE(data_dir
)) {
3388 data_dir
[data_dir_idx
++] = optarg
;
3391 case QEMU_OPTION_bios
:
3392 qemu_opts_set(qemu_find_opts("machine"), 0, "firmware", optarg
);
3394 case QEMU_OPTION_singlestep
:
3401 keyboard_layout
= optarg
;
3403 case QEMU_OPTION_localtime
:
3406 case QEMU_OPTION_vga
:
3415 w
= strtol(p
, (char **)&p
, 10);
3418 fprintf(stderr
, "qemu: invalid resolution or depth\n");
3424 h
= strtol(p
, (char **)&p
, 10);
3429 depth
= strtol(p
, (char **)&p
, 10);
3430 if (depth
!= 8 && depth
!= 15 && depth
!= 16 &&
3431 depth
!= 24 && depth
!= 32)
3433 } else if (*p
== '\0') {
3434 depth
= graphic_depth
;
3441 graphic_depth
= depth
;
3444 case QEMU_OPTION_echr
:
3447 term_escape_char
= strtol(optarg
, &r
, 0);
3449 printf("Bad argument to echr\n");
3452 case QEMU_OPTION_monitor
:
3453 default_monitor
= 0;
3454 if (strncmp(optarg
, "none", 4)) {
3455 monitor_parse(optarg
, "readline");
3458 case QEMU_OPTION_qmp
:
3459 monitor_parse(optarg
, "control");
3460 default_monitor
= 0;
3462 case QEMU_OPTION_mon
:
3463 opts
= qemu_opts_parse(qemu_find_opts("mon"), optarg
, 1);
3467 default_monitor
= 0;
3469 case QEMU_OPTION_chardev
:
3470 opts
= qemu_opts_parse(qemu_find_opts("chardev"), optarg
, 1);
3475 case QEMU_OPTION_fsdev
:
3476 olist
= qemu_find_opts("fsdev");
3478 fprintf(stderr
, "fsdev is not supported by this qemu build.\n");
3481 opts
= qemu_opts_parse(olist
, optarg
, 1);
3486 case QEMU_OPTION_virtfs
: {
3489 const char *writeout
, *sock_fd
, *socket
;
3491 olist
= qemu_find_opts("virtfs");
3493 fprintf(stderr
, "virtfs is not supported by this qemu build.\n");
3496 opts
= qemu_opts_parse(olist
, optarg
, 1);
3501 if (qemu_opt_get(opts
, "fsdriver") == NULL
||
3502 qemu_opt_get(opts
, "mount_tag") == NULL
) {
3503 fprintf(stderr
, "Usage: -virtfs fsdriver,mount_tag=tag.\n");
3506 fsdev
= qemu_opts_create(qemu_find_opts("fsdev"),
3507 qemu_opt_get(opts
, "mount_tag"),
3510 fprintf(stderr
, "duplicate fsdev id: %s\n",
3511 qemu_opt_get(opts
, "mount_tag"));
3515 writeout
= qemu_opt_get(opts
, "writeout");
3517 #ifdef CONFIG_SYNC_FILE_RANGE
3518 qemu_opt_set(fsdev
, "writeout", writeout
);
3520 fprintf(stderr
, "writeout=immediate not supported on "
3525 qemu_opt_set(fsdev
, "fsdriver", qemu_opt_get(opts
, "fsdriver"));
3526 qemu_opt_set(fsdev
, "path", qemu_opt_get(opts
, "path"));
3527 qemu_opt_set(fsdev
, "security_model",
3528 qemu_opt_get(opts
, "security_model"));
3529 socket
= qemu_opt_get(opts
, "socket");
3531 qemu_opt_set(fsdev
, "socket", socket
);
3533 sock_fd
= qemu_opt_get(opts
, "sock_fd");
3535 qemu_opt_set(fsdev
, "sock_fd", sock_fd
);
3538 qemu_opt_set_bool(fsdev
, "readonly",
3539 qemu_opt_get_bool(opts
, "readonly", 0));
3540 device
= qemu_opts_create(qemu_find_opts("device"), NULL
, 0,
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(qemu_find_opts("device"), NULL
, 0,
3563 qemu_opt_set(device
, "driver", "virtio-9p-pci");
3564 qemu_opt_set(device
, "fsdev", "v_synth");
3565 qemu_opt_set(device
, "mount_tag", "v_synth");
3568 case QEMU_OPTION_serial
:
3569 add_device_config(DEV_SERIAL
, optarg
);
3571 if (strncmp(optarg
, "mon:", 4) == 0) {
3572 default_monitor
= 0;
3575 case QEMU_OPTION_watchdog
:
3578 "qemu: only one watchdog option may be given\n");
3583 case QEMU_OPTION_watchdog_action
:
3584 if (select_watchdog_action(optarg
) == -1) {
3585 fprintf(stderr
, "Unknown -watchdog-action parameter\n");
3589 case QEMU_OPTION_virtiocon
:
3590 add_device_config(DEV_VIRTCON
, optarg
);
3591 default_virtcon
= 0;
3592 if (strncmp(optarg
, "mon:", 4) == 0) {
3593 default_monitor
= 0;
3596 case QEMU_OPTION_parallel
:
3597 add_device_config(DEV_PARALLEL
, optarg
);
3598 default_parallel
= 0;
3599 if (strncmp(optarg
, "mon:", 4) == 0) {
3600 default_monitor
= 0;
3603 case QEMU_OPTION_debugcon
:
3604 add_device_config(DEV_DEBUGCON
, optarg
);
3606 case QEMU_OPTION_loadvm
:
3609 case QEMU_OPTION_full_screen
:
3612 case QEMU_OPTION_no_frame
:
3615 case QEMU_OPTION_alt_grab
:
3618 case QEMU_OPTION_ctrl_grab
:
3621 case QEMU_OPTION_no_quit
:
3624 case QEMU_OPTION_sdl
:
3626 display_type
= DT_SDL
;
3629 fprintf(stderr
, "SDL support is disabled\n");
3632 case QEMU_OPTION_pidfile
:
3635 case QEMU_OPTION_win2k_hack
:
3636 win2k_install_hack
= 1;
3638 case QEMU_OPTION_rtc_td_hack
: {
3639 static GlobalProperty slew_lost_ticks
[] = {
3641 .driver
= "mc146818rtc",
3642 .property
= "lost_tick_policy",
3645 { /* end of list */ }
3648 qdev_prop_register_global_list(slew_lost_ticks
);
3651 case QEMU_OPTION_acpitable
:
3652 opts
= qemu_opts_parse(qemu_find_opts("acpi"), optarg
, 1);
3656 do_acpitable_option(opts
);
3658 case QEMU_OPTION_smbios
:
3659 opts
= qemu_opts_parse(qemu_find_opts("smbios"), optarg
, 0);
3663 do_smbios_option(opts
);
3665 case QEMU_OPTION_enable_kvm
:
3666 olist
= qemu_find_opts("machine");
3667 qemu_opts_parse(olist
, "accel=kvm", 0);
3669 case QEMU_OPTION_machine
:
3670 olist
= qemu_find_opts("machine");
3671 opts
= qemu_opts_parse(olist
, optarg
, 1);
3675 optarg
= qemu_opt_get(opts
, "type");
3677 machine_class
= machine_parse(optarg
);
3680 case QEMU_OPTION_no_kvm
:
3681 olist
= qemu_find_opts("machine");
3682 qemu_opts_parse(olist
, "accel=tcg", 0);
3684 case QEMU_OPTION_no_kvm_pit
: {
3685 fprintf(stderr
, "Warning: KVM PIT can no longer be disabled "
3689 case QEMU_OPTION_no_kvm_pit_reinjection
: {
3690 static GlobalProperty kvm_pit_lost_tick_policy
[] = {
3692 .driver
= "kvm-pit",
3693 .property
= "lost_tick_policy",
3696 { /* end of list */ }
3699 fprintf(stderr
, "Warning: option deprecated, use "
3700 "lost_tick_policy property of kvm-pit instead.\n");
3701 qdev_prop_register_global_list(kvm_pit_lost_tick_policy
);
3704 case QEMU_OPTION_usb
:
3705 olist
= qemu_find_opts("machine");
3706 qemu_opts_parse(olist
, "usb=on", 0);
3708 case QEMU_OPTION_usbdevice
:
3709 olist
= qemu_find_opts("machine");
3710 qemu_opts_parse(olist
, "usb=on", 0);
3711 add_device_config(DEV_USB
, optarg
);
3713 case QEMU_OPTION_device
:
3714 if (!qemu_opts_parse(qemu_find_opts("device"), optarg
, 1)) {
3718 case QEMU_OPTION_smp
:
3719 if (!qemu_opts_parse(qemu_find_opts("smp-opts"), optarg
, 1)) {
3723 case QEMU_OPTION_vnc
:
3726 vnc_display
= optarg
;
3728 fprintf(stderr
, "VNC support is disabled\n");
3732 case QEMU_OPTION_no_acpi
:
3735 case QEMU_OPTION_no_hpet
:
3738 case QEMU_OPTION_balloon
:
3739 if (balloon_parse(optarg
) < 0) {
3740 fprintf(stderr
, "Unknown -balloon argument %s\n", optarg
);
3744 case QEMU_OPTION_no_reboot
:
3747 case QEMU_OPTION_no_shutdown
:
3750 case QEMU_OPTION_show_cursor
:
3753 case QEMU_OPTION_uuid
:
3754 if(qemu_uuid_parse(optarg
, qemu_uuid
) < 0) {
3755 fprintf(stderr
, "Fail to parse UUID string."
3756 " Wrong format.\n");
3759 qemu_uuid_set
= true;
3761 case QEMU_OPTION_option_rom
:
3762 if (nb_option_roms
>= MAX_OPTION_ROMS
) {
3763 fprintf(stderr
, "Too many option ROMs\n");
3766 opts
= qemu_opts_parse(qemu_find_opts("option-rom"), optarg
, 1);
3770 option_rom
[nb_option_roms
].name
= qemu_opt_get(opts
, "romfile");
3771 option_rom
[nb_option_roms
].bootindex
=
3772 qemu_opt_get_number(opts
, "bootindex", -1);
3773 if (!option_rom
[nb_option_roms
].name
) {
3774 fprintf(stderr
, "Option ROM file is not specified\n");
3779 case QEMU_OPTION_semihosting
:
3780 semihosting_enabled
= 1;
3782 case QEMU_OPTION_tdf
:
3783 fprintf(stderr
, "Warning: user space PIT time drift fix "
3784 "is no longer supported.\n");
3786 case QEMU_OPTION_name
:
3787 opts
= qemu_opts_parse(qemu_find_opts("name"), optarg
, 1);
3792 case QEMU_OPTION_prom_env
:
3793 if (nb_prom_envs
>= MAX_PROM_ENVS
) {
3794 fprintf(stderr
, "Too many prom variables\n");
3797 prom_envs
[nb_prom_envs
] = optarg
;
3800 case QEMU_OPTION_old_param
:
3803 case QEMU_OPTION_clock
:
3804 /* Clock options no longer exist. Keep this option for
3805 * backward compatibility.
3808 case QEMU_OPTION_startdate
:
3809 configure_rtc_date_offset(optarg
, 1);
3811 case QEMU_OPTION_rtc
:
3812 opts
= qemu_opts_parse(qemu_find_opts("rtc"), optarg
, 0);
3816 configure_rtc(opts
);
3818 case QEMU_OPTION_tb_size
:
3819 tcg_tb_size
= strtol(optarg
, NULL
, 0);
3820 if (tcg_tb_size
< 0) {
3824 case QEMU_OPTION_icount
:
3825 icount_option
= optarg
;
3827 case QEMU_OPTION_incoming
:
3829 runstate_set(RUN_STATE_INMIGRATE
);
3831 case QEMU_OPTION_nodefaults
:
3834 case QEMU_OPTION_xen_domid
:
3835 if (!(xen_available())) {
3836 printf("Option %s not supported for this target\n", popt
->name
);
3839 xen_domid
= atoi(optarg
);
3841 case QEMU_OPTION_xen_create
:
3842 if (!(xen_available())) {
3843 printf("Option %s not supported for this target\n", popt
->name
);
3846 xen_mode
= XEN_CREATE
;
3848 case QEMU_OPTION_xen_attach
:
3849 if (!(xen_available())) {
3850 printf("Option %s not supported for this target\n", popt
->name
);
3853 xen_mode
= XEN_ATTACH
;
3855 case QEMU_OPTION_trace
:
3857 opts
= qemu_opts_parse(qemu_find_opts("trace"), optarg
, 0);
3861 trace_events
= qemu_opt_get(opts
, "events");
3862 trace_file
= qemu_opt_get(opts
, "file");
3865 case QEMU_OPTION_readconfig
:
3867 int ret
= qemu_read_config_file(optarg
);
3869 fprintf(stderr
, "read config %s: %s\n", optarg
,
3875 case QEMU_OPTION_spice
:
3876 olist
= qemu_find_opts("spice");
3878 fprintf(stderr
, "spice is not supported by this qemu build.\n");
3881 opts
= qemu_opts_parse(olist
, optarg
, 0);
3887 case QEMU_OPTION_writeconfig
:
3890 if (strcmp(optarg
, "-") == 0) {
3893 fp
= fopen(optarg
, "w");
3895 fprintf(stderr
, "open %s: %s\n", optarg
, strerror(errno
));
3899 qemu_config_write(fp
);
3905 case QEMU_OPTION_qtest
:
3906 qtest_chrdev
= optarg
;
3908 case QEMU_OPTION_qtest_log
:
3911 case QEMU_OPTION_sandbox
:
3912 opts
= qemu_opts_parse(qemu_find_opts("sandbox"), optarg
, 1);
3917 case QEMU_OPTION_add_fd
:
3919 opts
= qemu_opts_parse(qemu_find_opts("add-fd"), optarg
, 0);
3924 error_report("File descriptor passing is disabled on this "
3929 case QEMU_OPTION_object
:
3930 opts
= qemu_opts_parse(qemu_find_opts("object"), optarg
, 1);
3935 case QEMU_OPTION_realtime
:
3936 opts
= qemu_opts_parse(qemu_find_opts("realtime"), optarg
, 0);
3940 configure_realtime(opts
);
3942 case QEMU_OPTION_msg
:
3943 opts
= qemu_opts_parse(qemu_find_opts("msg"), optarg
, 0);
3947 configure_msg(opts
);
3950 os_parse_cmd_args(popt
->index
, optarg
);
3958 if (qemu_init_main_loop()) {
3959 fprintf(stderr
, "qemu_init_main_loop failed\n");
3963 if (qemu_opts_foreach(qemu_find_opts("sandbox"), parse_sandbox
, NULL
, 0)) {
3967 if (qemu_opts_foreach(qemu_find_opts("name"), parse_name
, NULL
, 1)) {
3972 if (qemu_opts_foreach(qemu_find_opts("add-fd"), parse_add_fd
, NULL
, 1)) {
3976 if (qemu_opts_foreach(qemu_find_opts("add-fd"), cleanup_add_fd
, NULL
, 1)) {
3981 if (machine_class
== NULL
) {
3982 fprintf(stderr
, "No machine specified, and there is no default.\n"
3983 "Use -machine help to list supported machines!\n");
3987 cpu_exec_init_all();
3989 current_machine
= MACHINE(object_new(object_class_get_name(
3990 OBJECT_CLASS(machine_class
))));
3991 object_property_add_child(object_get_root(), "machine",
3992 OBJECT(current_machine
), &error_abort
);
3994 if (machine_class
->hw_version
) {
3995 qemu_set_version(machine_class
->hw_version
);
3998 if (qemu_opts_foreach(qemu_find_opts("object"),
3999 object_create
, NULL
, 0) != 0) {
4003 /* Init CPU def lists, based on config
4004 * - Must be called after all the qemu_read_config_file() calls
4005 * - Must be called before list_cpus()
4006 * - Must be called before machine->init()
4010 if (cpu_model
&& is_help_option(cpu_model
)) {
4011 list_cpus(stdout
, &fprintf
, cpu_model
);
4015 /* Open the logfile at this point, if necessary. We can't open the logfile
4016 * when encountering either of the logging options (-d or -D) because the
4017 * other one may be encountered later on the command line, changing the
4018 * location or level of logging.
4023 qemu_set_log_filename(log_file
);
4026 mask
= qemu_str_to_log_mask(log_mask
);
4028 qemu_print_log_usage(stdout
);
4034 if (!is_daemonized()) {
4035 if (!trace_init_backends(trace_events
, trace_file
)) {
4040 /* If no data_dir is specified then try to find it relative to the
4042 if (data_dir_idx
< ARRAY_SIZE(data_dir
)) {
4043 data_dir
[data_dir_idx
] = os_find_datadir();
4044 if (data_dir
[data_dir_idx
] != NULL
) {
4048 /* If all else fails use the install path specified when building. */
4049 if (data_dir_idx
< ARRAY_SIZE(data_dir
)) {
4050 data_dir
[data_dir_idx
++] = CONFIG_QEMU_DATADIR
;
4053 smp_parse(qemu_opts_find(qemu_find_opts("smp-opts"), NULL
));
4055 machine_class
->max_cpus
= machine_class
->max_cpus
?: 1; /* Default to UP */
4056 if (smp_cpus
> machine_class
->max_cpus
) {
4057 fprintf(stderr
, "Number of SMP cpus requested (%d), exceeds max cpus "
4058 "supported by machine `%s' (%d)\n", smp_cpus
,
4059 machine_class
->name
, machine_class
->max_cpus
);
4064 * Get the default machine options from the machine if it is not already
4065 * specified either by the configuration file or by the command line.
4067 if (machine_class
->default_machine_opts
) {
4068 qemu_opts_set_defaults(qemu_find_opts("machine"),
4069 machine_class
->default_machine_opts
, 0);
4072 qemu_opts_foreach(qemu_find_opts("device"), default_driver_check
, NULL
, 0);
4073 qemu_opts_foreach(qemu_find_opts("global"), default_driver_check
, NULL
, 0);
4075 if (!vga_model
&& !default_vga
) {
4076 vga_interface_type
= VGA_DEVICE
;
4078 if (!has_defaults
|| machine_class
->no_serial
) {
4081 if (!has_defaults
|| machine_class
->no_parallel
) {
4082 default_parallel
= 0;
4084 if (!has_defaults
|| !machine_class
->use_virtcon
) {
4085 default_virtcon
= 0;
4087 if (!has_defaults
|| !machine_class
->use_sclp
) {
4090 if (!has_defaults
|| machine_class
->no_floppy
) {
4093 if (!has_defaults
|| machine_class
->no_cdrom
) {
4096 if (!has_defaults
|| machine_class
->no_sdcard
) {
4099 if (!has_defaults
) {
4100 default_monitor
= 0;
4105 if (is_daemonized()) {
4106 /* According to documentation and historically, -nographic redirects
4107 * serial port, parallel port and monitor to stdio, which does not work
4108 * with -daemonize. We can redirect these to null instead, but since
4109 * -nographic is legacy, let's just error out.
4110 * We disallow -nographic only if all other ports are not redirected
4111 * explicitly, to not break existing legacy setups which uses
4112 * -nographic _and_ redirects all ports explicitly - this is valid
4113 * usage, -nographic is just a no-op in this case.
4115 if (display_type
== DT_NOGRAPHIC
4116 && (default_parallel
|| default_serial
4117 || default_monitor
|| default_virtcon
)) {
4118 fprintf(stderr
, "-nographic can not be used with -daemonize\n");
4121 #ifdef CONFIG_CURSES
4122 if (display_type
== DT_CURSES
) {
4123 fprintf(stderr
, "curses display can not be used with -daemonize\n");
4129 if (display_type
== DT_NOGRAPHIC
) {
4130 if (default_parallel
)
4131 add_device_config(DEV_PARALLEL
, "null");
4132 if (default_serial
&& default_monitor
) {
4133 add_device_config(DEV_SERIAL
, "mon:stdio");
4134 } else if (default_virtcon
&& default_monitor
) {
4135 add_device_config(DEV_VIRTCON
, "mon:stdio");
4136 } else if (default_sclp
&& default_monitor
) {
4137 add_device_config(DEV_SCLP
, "mon:stdio");
4140 add_device_config(DEV_SERIAL
, "stdio");
4141 if (default_virtcon
)
4142 add_device_config(DEV_VIRTCON
, "stdio");
4144 add_device_config(DEV_SCLP
, "stdio");
4146 if (default_monitor
)
4147 monitor_parse("stdio", "readline");
4151 add_device_config(DEV_SERIAL
, "vc:80Cx24C");
4152 if (default_parallel
)
4153 add_device_config(DEV_PARALLEL
, "vc:80Cx24C");
4154 if (default_monitor
)
4155 monitor_parse("vc:80Cx24C", "readline");
4156 if (default_virtcon
)
4157 add_device_config(DEV_VIRTCON
, "vc:80Cx24C");
4159 add_device_config(DEV_SCLP
, "vc:80Cx24C");
4163 if (display_type
== DT_DEFAULT
&& !display_remote
) {
4164 #if defined(CONFIG_GTK)
4165 display_type
= DT_GTK
;
4166 #elif defined(CONFIG_SDL) || defined(CONFIG_COCOA)
4167 display_type
= DT_SDL
;
4168 #elif defined(CONFIG_VNC)
4169 vnc_display
= "localhost:0,to=99";
4172 display_type
= DT_NONE
;
4176 if ((no_frame
|| alt_grab
|| ctrl_grab
) && display_type
!= DT_SDL
) {
4177 fprintf(stderr
, "-no-frame, -alt-grab and -ctrl-grab are only valid "
4178 "for SDL, ignoring option\n");
4180 if (no_quit
&& (display_type
!= DT_GTK
&& display_type
!= DT_SDL
)) {
4181 fprintf(stderr
, "-no-quit is only valid for GTK and SDL, "
4182 "ignoring option\n");
4185 #if defined(CONFIG_GTK)
4186 if (display_type
== DT_GTK
) {
4187 early_gtk_display_init();
4193 if (qemu_opts_foreach(qemu_find_opts("chardev"), chardev_init_func
, NULL
, 1) != 0)
4195 #ifdef CONFIG_VIRTFS
4196 if (qemu_opts_foreach(qemu_find_opts("fsdev"), fsdev_init_func
, NULL
, 1) != 0) {
4201 if (pid_file
&& qemu_create_pidfile(pid_file
) != 0) {
4206 /* store value for the future use */
4207 qemu_opt_set_number(qemu_find_opts_singleton("memory"), "size", ram_size
);
4209 if (qemu_opts_foreach(qemu_find_opts("device"), device_help_func
, NULL
, 0)
4214 machine_opts
= qemu_get_machine_opts();
4215 if (qemu_opt_foreach(machine_opts
, object_set_property
, current_machine
,
4217 object_unref(OBJECT(current_machine
));
4221 configure_accelerator(machine_class
);
4224 Error
*local_err
= NULL
;
4225 qtest_init(qtest_chrdev
, qtest_log
, &local_err
);
4227 error_report("%s", error_get_pretty(local_err
));
4228 error_free(local_err
);
4233 machine_opts
= qemu_get_machine_opts();
4234 kernel_filename
= qemu_opt_get(machine_opts
, "kernel");
4235 initrd_filename
= qemu_opt_get(machine_opts
, "initrd");
4236 kernel_cmdline
= qemu_opt_get(machine_opts
, "append");
4237 bios_name
= qemu_opt_get(machine_opts
, "firmware");
4239 boot_order
= machine_class
->default_boot_order
;
4240 opts
= qemu_opts_find(qemu_find_opts("boot-opts"), NULL
);
4242 char *normal_boot_order
;
4243 const char *order
, *once
;
4245 order
= qemu_opt_get(opts
, "order");
4247 validate_bootdevices(order
);
4251 once
= qemu_opt_get(opts
, "once");
4253 validate_bootdevices(once
);
4254 normal_boot_order
= g_strdup(boot_order
);
4256 qemu_register_reset(restore_boot_order
, normal_boot_order
);
4259 boot_menu
= qemu_opt_get_bool(opts
, "menu", boot_menu
);
4260 boot_strict
= qemu_opt_get_bool(opts
, "strict", false);
4263 if (!kernel_cmdline
) {
4264 kernel_cmdline
= "";
4265 current_machine
->kernel_cmdline
= (char *)kernel_cmdline
;
4268 linux_boot
= (kernel_filename
!= NULL
);
4270 if (!linux_boot
&& *kernel_cmdline
!= '\0') {
4271 fprintf(stderr
, "-append only allowed with -kernel option\n");
4275 if (!linux_boot
&& initrd_filename
!= NULL
) {
4276 fprintf(stderr
, "-initrd only allowed with -kernel option\n");
4280 if (!linux_boot
&& qemu_opt_get(machine_opts
, "dtb")) {
4281 fprintf(stderr
, "-dtb only allowed with -kernel option\n");
4285 os_set_line_buffering();
4287 qemu_init_cpu_loop();
4288 qemu_mutex_lock_iothread();
4291 /* spice needs the timers to be initialized by this point */
4295 if (icount_option
&& (kvm_enabled() || xen_enabled())) {
4296 fprintf(stderr
, "-icount is not allowed with kvm or xen\n");
4299 configure_icount(icount_option
);
4301 /* clean up network at qemu process termination */
4302 atexit(&net_cleanup
);
4304 if (net_init_clients() < 0) {
4309 if (tpm_init() < 0) {
4314 /* init the bluetooth world */
4315 if (foreach_device_config(DEV_BT
, bt_parse
))
4318 if (!xen_enabled()) {
4319 /* On 32-bit hosts, QEMU is limited by virtual address space */
4320 if (ram_size
> (2047 << 20) && HOST_LONG_BITS
== 32) {
4321 fprintf(stderr
, "qemu: at most 2047 MB RAM can be simulated\n");
4329 /* open the virtual block devices */
4331 qemu_opts_foreach(qemu_find_opts("drive"), drive_enable_snapshot
, NULL
, 0);
4332 if (qemu_opts_foreach(qemu_find_opts("drive"), drive_init_func
,
4333 &machine_class
->block_default_type
, 1) != 0) {
4337 default_drive(default_cdrom
, snapshot
, machine_class
->block_default_type
, 2,
4339 default_drive(default_floppy
, snapshot
, IF_FLOPPY
, 0, FD_OPTS
);
4340 default_drive(default_sdcard
, snapshot
, IF_SD
, 0, SD_OPTS
);
4342 if (qemu_opts_foreach(qemu_find_opts("numa"), numa_init_func
,
4349 if (qemu_opts_foreach(qemu_find_opts("mon"), mon_init_func
, NULL
, 1) != 0) {
4353 if (foreach_device_config(DEV_SERIAL
, serial_parse
) < 0)
4355 if (foreach_device_config(DEV_PARALLEL
, parallel_parse
) < 0)
4357 if (foreach_device_config(DEV_VIRTCON
, virtcon_parse
) < 0)
4359 if (foreach_device_config(DEV_SCLP
, sclp_parse
) < 0) {
4362 if (foreach_device_config(DEV_DEBUGCON
, debugcon_parse
) < 0)
4365 /* If no default VGA is requested, the default is "none". */
4367 if (cirrus_vga_available()) {
4368 vga_model
= "cirrus";
4369 } else if (vga_available()) {
4374 select_vgahw(vga_model
);
4378 i
= select_watchdog(watchdog
);
4380 exit (i
== 1 ? 1 : 0);
4383 if (machine_class
->compat_props
) {
4384 qdev_prop_register_global_list(machine_class
->compat_props
);
4388 qdev_machine_init();
4390 current_machine
->ram_size
= ram_size
;
4391 current_machine
->maxram_size
= maxram_size
;
4392 current_machine
->ram_slots
= ram_slots
;
4393 current_machine
->boot_order
= boot_order
;
4394 current_machine
->cpu_model
= cpu_model
;
4396 machine_class
->init(current_machine
);
4400 cpu_synchronize_all_post_init();
4404 /* init USB devices */
4405 if (usb_enabled(false)) {
4406 if (foreach_device_config(DEV_USB
, usb_parse
) < 0)
4410 /* init generic devices */
4411 if (qemu_opts_foreach(qemu_find_opts("device"), device_init_func
, NULL
, 1) != 0)
4414 net_check_clients();
4416 ds
= init_displaystate();
4418 /* init local displays */
4419 switch (display_type
) {
4421 (void)ds
; /* avoid warning if no display is configured */
4423 #if defined(CONFIG_CURSES)
4425 curses_display_init(ds
, full_screen
);
4428 #if defined(CONFIG_SDL)
4430 sdl_display_init(ds
, full_screen
, no_frame
);
4432 #elif defined(CONFIG_COCOA)
4434 cocoa_display_init(ds
, full_screen
);
4437 #if defined(CONFIG_GTK)
4439 gtk_display_init(ds
, full_screen
, grab_on_hover
);
4446 /* must be after terminal init, SDL library changes signal handlers */
4447 os_setup_signal_handling();
4450 /* init remote displays */
4452 Error
*local_err
= NULL
;
4453 vnc_display_init(ds
);
4454 vnc_display_open(ds
, vnc_display
, &local_err
);
4455 if (local_err
!= NULL
) {
4456 error_report("Failed to start VNC server on `%s': %s",
4457 vnc_display
, error_get_pretty(local_err
));
4458 error_free(local_err
);
4462 if (show_vnc_port
) {
4463 printf("VNC server running on `%s'\n", vnc_display_local_addr(ds
));
4469 qemu_spice_display_init();
4473 if (foreach_device_config(DEV_GDB
, gdbserver_start
) < 0) {
4477 qdev_machine_creation_done();
4479 if (rom_load_all() != 0) {
4480 fprintf(stderr
, "rom loading failed\n");
4484 /* TODO: once all bus devices are qdevified, this should be done
4485 * when bus is created by qdev.c */
4486 qemu_register_reset(qbus_reset_all_fn
, sysbus_get_default());
4487 qemu_run_machine_init_done_notifiers();
4489 /* Done notifiers can load ROMs */
4492 qemu_system_reset(VMRESET_SILENT
);
4494 if (load_vmstate(loadvm
) < 0) {
4499 qdev_prop_check_global();
4502 Error
*local_err
= NULL
;
4503 qemu_start_incoming_migration(incoming
, &local_err
);
4505 error_report("-incoming %s: %s", incoming
,
4506 error_get_pretty(local_err
));
4507 error_free(local_err
);
4510 } else if (autostart
) {
4516 if (is_daemonized()) {
4517 if (!trace_init_backends(trace_events
, trace_file
)) {