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 "sysemu/blockdev.h"
86 #include "hw/block/block.h"
87 #include "migration/block.h"
88 #include "sysemu/tpm.h"
89 #include "sysemu/dma.h"
90 #include "audio/audio.h"
91 #include "migration/migration.h"
92 #include "sysemu/kvm.h"
93 #include "qapi/qmp/qjson.h"
94 #include "qemu/option.h"
95 #include "qemu/config-file.h"
96 #include "qemu-options.h"
97 #include "qmp-commands.h"
98 #include "qemu/main-loop.h"
100 #include "fsdev/qemu-fsdev.h"
102 #include "sysemu/qtest.h"
104 #include "disas/disas.h"
107 #include "slirp/libslirp.h"
110 #include "trace/control.h"
111 #include "qemu/queue.h"
112 #include "sysemu/cpus.h"
113 #include "sysemu/arch_init.h"
114 #include "qemu/osdep.h"
116 #include "ui/qemu-spice.h"
117 #include "qapi/string-input-visitor.h"
118 #include "qapi/opts-visitor.h"
119 #include "qom/object_interfaces.h"
120 #include "qapi-event.h"
122 #define DEFAULT_RAM_SIZE 128
124 #define MAX_VIRTIO_CONSOLES 1
125 #define MAX_SCLP_CONSOLES 1
127 static const char *data_dir
[16];
128 static int data_dir_idx
;
129 const char *bios_name
= NULL
;
130 enum vga_retrace_method vga_retrace_method
= VGA_RETRACE_DUMB
;
131 DisplayType display_type
= DT_DEFAULT
;
132 static int display_remote
;
133 const char* keyboard_layout
= NULL
;
135 const char *mem_path
= NULL
;
136 int mem_prealloc
= 0; /* force preallocation of physical target memory */
138 NICInfo nd_table
[MAX_NICS
];
140 static int rtc_utc
= 1;
141 static int rtc_date_offset
= -1; /* -1 means no change */
142 QEMUClockType rtc_clock
;
143 int vga_interface_type
= VGA_NONE
;
144 static int full_screen
= 0;
145 static int no_frame
= 0;
148 static bool grab_on_hover
;
150 CharDriverState
*serial_hds
[MAX_SERIAL_PORTS
];
151 CharDriverState
*parallel_hds
[MAX_PARALLEL_PORTS
];
152 CharDriverState
*virtcon_hds
[MAX_VIRTIO_CONSOLES
];
153 CharDriverState
*sclp_hds
[MAX_SCLP_CONSOLES
];
154 int win2k_install_hack
= 0;
161 const char *vnc_display
;
163 int acpi_enabled
= 1;
166 static int no_reboot
;
169 int graphic_rotate
= 0;
170 const char *watchdog
;
171 QEMUOptionRom option_rom
[MAX_OPTION_ROMS
];
173 int semihosting_enabled
= 0;
175 const char *qemu_name
;
178 unsigned int nb_prom_envs
= 0;
179 const char *prom_envs
[MAX_PROM_ENVS
];
181 static bool boot_strict
;
182 uint8_t *boot_splash_filedata
;
183 size_t boot_splash_filedata_size
;
184 uint8_t qemu_extra_params_fw
[2];
186 typedef struct FWBootEntry FWBootEntry
;
189 QTAILQ_ENTRY(FWBootEntry
) link
;
195 static QTAILQ_HEAD(, FWBootEntry
) fw_boot_order
=
196 QTAILQ_HEAD_INITIALIZER(fw_boot_order
);
199 NodeInfo numa_info
[MAX_NODES
];
201 uint8_t qemu_uuid
[16];
204 static QEMUBootSetHandler
*boot_set_handler
;
205 static void *boot_set_opaque
;
207 static NotifierList exit_notifiers
=
208 NOTIFIER_LIST_INITIALIZER(exit_notifiers
);
210 static NotifierList machine_init_done_notifiers
=
211 NOTIFIER_LIST_INITIALIZER(machine_init_done_notifiers
);
213 static bool tcg_allowed
= true;
216 enum xen_mode xen_mode
= XEN_EMULATE
;
217 static int tcg_tb_size
;
219 static int has_defaults
= 1;
220 static int default_serial
= 1;
221 static int default_parallel
= 1;
222 static int default_virtcon
= 1;
223 static int default_sclp
= 1;
224 static int default_monitor
= 1;
225 static int default_floppy
= 1;
226 static int default_cdrom
= 1;
227 static int default_sdcard
= 1;
228 static int default_vga
= 1;
234 { .driver
= "isa-serial", .flag
= &default_serial
},
235 { .driver
= "isa-parallel", .flag
= &default_parallel
},
236 { .driver
= "isa-fdc", .flag
= &default_floppy
},
237 { .driver
= "ide-cd", .flag
= &default_cdrom
},
238 { .driver
= "ide-hd", .flag
= &default_cdrom
},
239 { .driver
= "ide-drive", .flag
= &default_cdrom
},
240 { .driver
= "scsi-cd", .flag
= &default_cdrom
},
241 { .driver
= "virtio-serial-pci", .flag
= &default_virtcon
},
242 { .driver
= "virtio-serial-s390", .flag
= &default_virtcon
},
243 { .driver
= "virtio-serial", .flag
= &default_virtcon
},
244 { .driver
= "VGA", .flag
= &default_vga
},
245 { .driver
= "isa-vga", .flag
= &default_vga
},
246 { .driver
= "cirrus-vga", .flag
= &default_vga
},
247 { .driver
= "isa-cirrus-vga", .flag
= &default_vga
},
248 { .driver
= "vmware-svga", .flag
= &default_vga
},
249 { .driver
= "qxl-vga", .flag
= &default_vga
},
252 static QemuOptsList qemu_rtc_opts
= {
254 .head
= QTAILQ_HEAD_INITIALIZER(qemu_rtc_opts
.head
),
258 .type
= QEMU_OPT_STRING
,
261 .type
= QEMU_OPT_STRING
,
264 .type
= QEMU_OPT_STRING
,
266 { /* end of list */ }
270 static QemuOptsList qemu_sandbox_opts
= {
272 .implied_opt_name
= "enable",
273 .head
= QTAILQ_HEAD_INITIALIZER(qemu_sandbox_opts
.head
),
277 .type
= QEMU_OPT_BOOL
,
279 { /* end of list */ }
283 static QemuOptsList qemu_trace_opts
= {
285 .implied_opt_name
= "trace",
286 .head
= QTAILQ_HEAD_INITIALIZER(qemu_trace_opts
.head
),
290 .type
= QEMU_OPT_STRING
,
293 .type
= QEMU_OPT_STRING
,
295 { /* end of list */ }
299 static QemuOptsList qemu_option_rom_opts
= {
300 .name
= "option-rom",
301 .implied_opt_name
= "romfile",
302 .head
= QTAILQ_HEAD_INITIALIZER(qemu_option_rom_opts
.head
),
306 .type
= QEMU_OPT_NUMBER
,
309 .type
= QEMU_OPT_STRING
,
311 { /* end of list */ }
315 static QemuOptsList qemu_machine_opts
= {
317 .implied_opt_name
= "type",
319 .head
= QTAILQ_HEAD_INITIALIZER(qemu_machine_opts
.head
),
323 .type
= QEMU_OPT_STRING
,
324 .help
= "emulated machine"
327 .type
= QEMU_OPT_STRING
,
328 .help
= "accelerator list",
330 .name
= "kernel_irqchip",
331 .type
= QEMU_OPT_BOOL
,
332 .help
= "use KVM in-kernel irqchip",
334 .name
= "kvm_shadow_mem",
335 .type
= QEMU_OPT_SIZE
,
336 .help
= "KVM shadow MMU size",
339 .type
= QEMU_OPT_STRING
,
340 .help
= "Linux kernel image file",
343 .type
= QEMU_OPT_STRING
,
344 .help
= "Linux initial ramdisk file",
347 .type
= QEMU_OPT_STRING
,
348 .help
= "Linux kernel command line",
351 .type
= QEMU_OPT_STRING
,
352 .help
= "Linux kernel device tree file",
355 .type
= QEMU_OPT_STRING
,
356 .help
= "Dump current dtb to a file and quit",
358 .name
= "phandle_start",
359 .type
= QEMU_OPT_NUMBER
,
360 .help
= "The first phandle ID we may generate dynamically",
362 .name
= "dt_compatible",
363 .type
= QEMU_OPT_STRING
,
364 .help
= "Overrides the \"compatible\" property of the dt root node",
366 .name
= "dump-guest-core",
367 .type
= QEMU_OPT_BOOL
,
368 .help
= "Include guest memory in a core dump",
371 .type
= QEMU_OPT_BOOL
,
372 .help
= "enable/disable memory merge support",
375 .type
= QEMU_OPT_BOOL
,
376 .help
= "Set on/off to enable/disable usb",
379 .type
= QEMU_OPT_STRING
,
380 .help
= "firmware image",
383 .type
= QEMU_OPT_STRING
,
384 .help
= "Specifies the KVM virtualization mode (HV, PR)",
386 .name
= PC_MACHINE_MAX_RAM_BELOW_4G
,
387 .type
= QEMU_OPT_SIZE
,
388 .help
= "maximum ram below the 4G boundary (32bit boundary)",
390 { /* End of list */ }
394 static QemuOptsList qemu_boot_opts
= {
396 .implied_opt_name
= "order",
398 .head
= QTAILQ_HEAD_INITIALIZER(qemu_boot_opts
.head
),
402 .type
= QEMU_OPT_STRING
,
405 .type
= QEMU_OPT_STRING
,
408 .type
= QEMU_OPT_BOOL
,
411 .type
= QEMU_OPT_STRING
,
413 .name
= "splash-time",
414 .type
= QEMU_OPT_STRING
,
416 .name
= "reboot-timeout",
417 .type
= QEMU_OPT_STRING
,
420 .type
= QEMU_OPT_BOOL
,
426 static QemuOptsList qemu_add_fd_opts
= {
428 .head
= QTAILQ_HEAD_INITIALIZER(qemu_add_fd_opts
.head
),
432 .type
= QEMU_OPT_NUMBER
,
433 .help
= "file descriptor of which a duplicate is added to fd set",
436 .type
= QEMU_OPT_NUMBER
,
437 .help
= "ID of the fd set to add fd to",
440 .type
= QEMU_OPT_STRING
,
441 .help
= "free-form string used to describe fd",
443 { /* end of list */ }
447 static QemuOptsList qemu_object_opts
= {
449 .implied_opt_name
= "qom-type",
450 .head
= QTAILQ_HEAD_INITIALIZER(qemu_object_opts
.head
),
456 static QemuOptsList qemu_tpmdev_opts
= {
458 .implied_opt_name
= "type",
459 .head
= QTAILQ_HEAD_INITIALIZER(qemu_tpmdev_opts
.head
),
461 /* options are defined in the TPM backends */
462 { /* end of list */ }
466 static QemuOptsList qemu_realtime_opts
= {
468 .head
= QTAILQ_HEAD_INITIALIZER(qemu_realtime_opts
.head
),
472 .type
= QEMU_OPT_BOOL
,
474 { /* end of list */ }
478 static QemuOptsList qemu_msg_opts
= {
480 .head
= QTAILQ_HEAD_INITIALIZER(qemu_msg_opts
.head
),
484 .type
= QEMU_OPT_BOOL
,
486 { /* end of list */ }
490 static QemuOptsList qemu_name_opts
= {
492 .implied_opt_name
= "guest",
494 .head
= QTAILQ_HEAD_INITIALIZER(qemu_name_opts
.head
),
498 .type
= QEMU_OPT_STRING
,
499 .help
= "Sets the name of the guest.\n"
500 "This name will be displayed in the SDL window caption.\n"
501 "The name will also be used for the VNC server",
504 .type
= QEMU_OPT_STRING
,
505 .help
= "Sets the name of the QEMU process, as shown in top etc",
507 .name
= "debug-threads",
508 .type
= QEMU_OPT_BOOL
,
509 .help
= "When enabled, name the individual threads; defaults off.\n"
510 "NOTE: The thread names are for debugging and not a\n"
513 { /* End of list */ }
517 static QemuOptsList qemu_mem_opts
= {
519 .implied_opt_name
= "size",
520 .head
= QTAILQ_HEAD_INITIALIZER(qemu_mem_opts
.head
),
525 .type
= QEMU_OPT_SIZE
,
529 .type
= QEMU_OPT_NUMBER
,
533 .type
= QEMU_OPT_SIZE
,
535 { /* end of list */ }
540 * Get machine options
542 * Returns: machine options (never null).
544 QemuOpts
*qemu_get_machine_opts(void)
546 return qemu_find_opts_singleton("machine");
549 const char *qemu_get_vm_name(void)
554 static void res_free(void)
556 if (boot_splash_filedata
!= NULL
) {
557 g_free(boot_splash_filedata
);
558 boot_splash_filedata
= NULL
;
562 static int default_driver_check(QemuOpts
*opts
, void *opaque
)
564 const char *driver
= qemu_opt_get(opts
, "driver");
569 for (i
= 0; i
< ARRAY_SIZE(default_list
); i
++) {
570 if (strcmp(default_list
[i
].driver
, driver
) != 0)
572 *(default_list
[i
].flag
) = 0;
577 /***********************************************************/
580 static RunState current_run_state
= RUN_STATE_PRELAUNCH
;
582 /* We use RUN_STATE_MAX but any invalid value will do */
583 static RunState vmstop_requested
= RUN_STATE_MAX
;
584 static QemuMutex vmstop_lock
;
589 } RunStateTransition
;
591 static const RunStateTransition runstate_transitions_def
[] = {
593 { RUN_STATE_DEBUG
, RUN_STATE_RUNNING
},
594 { RUN_STATE_DEBUG
, RUN_STATE_FINISH_MIGRATE
},
596 { RUN_STATE_INMIGRATE
, RUN_STATE_RUNNING
},
597 { RUN_STATE_INMIGRATE
, RUN_STATE_PAUSED
},
599 { RUN_STATE_INTERNAL_ERROR
, RUN_STATE_PAUSED
},
600 { RUN_STATE_INTERNAL_ERROR
, RUN_STATE_FINISH_MIGRATE
},
602 { RUN_STATE_IO_ERROR
, RUN_STATE_RUNNING
},
603 { RUN_STATE_IO_ERROR
, RUN_STATE_FINISH_MIGRATE
},
605 { RUN_STATE_PAUSED
, RUN_STATE_RUNNING
},
606 { RUN_STATE_PAUSED
, RUN_STATE_FINISH_MIGRATE
},
608 { RUN_STATE_POSTMIGRATE
, RUN_STATE_RUNNING
},
609 { RUN_STATE_POSTMIGRATE
, RUN_STATE_FINISH_MIGRATE
},
611 { RUN_STATE_PRELAUNCH
, RUN_STATE_RUNNING
},
612 { RUN_STATE_PRELAUNCH
, RUN_STATE_FINISH_MIGRATE
},
613 { RUN_STATE_PRELAUNCH
, RUN_STATE_INMIGRATE
},
615 { RUN_STATE_FINISH_MIGRATE
, RUN_STATE_RUNNING
},
616 { RUN_STATE_FINISH_MIGRATE
, RUN_STATE_POSTMIGRATE
},
618 { RUN_STATE_RESTORE_VM
, RUN_STATE_RUNNING
},
620 { RUN_STATE_RUNNING
, RUN_STATE_DEBUG
},
621 { RUN_STATE_RUNNING
, RUN_STATE_INTERNAL_ERROR
},
622 { RUN_STATE_RUNNING
, RUN_STATE_IO_ERROR
},
623 { RUN_STATE_RUNNING
, RUN_STATE_PAUSED
},
624 { RUN_STATE_RUNNING
, RUN_STATE_FINISH_MIGRATE
},
625 { RUN_STATE_RUNNING
, RUN_STATE_RESTORE_VM
},
626 { RUN_STATE_RUNNING
, RUN_STATE_SAVE_VM
},
627 { RUN_STATE_RUNNING
, RUN_STATE_SHUTDOWN
},
628 { RUN_STATE_RUNNING
, RUN_STATE_WATCHDOG
},
629 { RUN_STATE_RUNNING
, RUN_STATE_GUEST_PANICKED
},
631 { RUN_STATE_SAVE_VM
, RUN_STATE_RUNNING
},
633 { RUN_STATE_SHUTDOWN
, RUN_STATE_PAUSED
},
634 { RUN_STATE_SHUTDOWN
, RUN_STATE_FINISH_MIGRATE
},
636 { RUN_STATE_DEBUG
, RUN_STATE_SUSPENDED
},
637 { RUN_STATE_RUNNING
, RUN_STATE_SUSPENDED
},
638 { RUN_STATE_SUSPENDED
, RUN_STATE_RUNNING
},
639 { RUN_STATE_SUSPENDED
, RUN_STATE_FINISH_MIGRATE
},
641 { RUN_STATE_WATCHDOG
, RUN_STATE_RUNNING
},
642 { RUN_STATE_WATCHDOG
, RUN_STATE_FINISH_MIGRATE
},
644 { RUN_STATE_GUEST_PANICKED
, RUN_STATE_RUNNING
},
645 { RUN_STATE_GUEST_PANICKED
, RUN_STATE_FINISH_MIGRATE
},
647 { RUN_STATE_MAX
, RUN_STATE_MAX
},
650 static bool runstate_valid_transitions
[RUN_STATE_MAX
][RUN_STATE_MAX
];
652 bool runstate_check(RunState state
)
654 return current_run_state
== state
;
657 static void runstate_init(void)
659 const RunStateTransition
*p
;
661 memset(&runstate_valid_transitions
, 0, sizeof(runstate_valid_transitions
));
662 for (p
= &runstate_transitions_def
[0]; p
->from
!= RUN_STATE_MAX
; p
++) {
663 runstate_valid_transitions
[p
->from
][p
->to
] = true;
666 qemu_mutex_init(&vmstop_lock
);
669 /* This function will abort() on invalid state transitions */
670 void runstate_set(RunState new_state
)
672 assert(new_state
< RUN_STATE_MAX
);
674 if (!runstate_valid_transitions
[current_run_state
][new_state
]) {
675 fprintf(stderr
, "ERROR: invalid runstate transition: '%s' -> '%s'\n",
676 RunState_lookup
[current_run_state
],
677 RunState_lookup
[new_state
]);
680 trace_runstate_set(new_state
);
681 current_run_state
= new_state
;
684 int runstate_is_running(void)
686 return runstate_check(RUN_STATE_RUNNING
);
689 bool runstate_needs_reset(void)
691 return runstate_check(RUN_STATE_INTERNAL_ERROR
) ||
692 runstate_check(RUN_STATE_SHUTDOWN
);
695 StatusInfo
*qmp_query_status(Error
**errp
)
697 StatusInfo
*info
= g_malloc0(sizeof(*info
));
699 info
->running
= runstate_is_running();
700 info
->singlestep
= singlestep
;
701 info
->status
= current_run_state
;
706 static bool qemu_vmstop_requested(RunState
*r
)
708 qemu_mutex_lock(&vmstop_lock
);
709 *r
= vmstop_requested
;
710 vmstop_requested
= RUN_STATE_MAX
;
711 qemu_mutex_unlock(&vmstop_lock
);
712 return *r
< RUN_STATE_MAX
;
715 void qemu_system_vmstop_request_prepare(void)
717 qemu_mutex_lock(&vmstop_lock
);
720 void qemu_system_vmstop_request(RunState state
)
722 vmstop_requested
= state
;
723 qemu_mutex_unlock(&vmstop_lock
);
731 qemu_vmstop_requested(&requested
);
732 if (runstate_is_running() && requested
== RUN_STATE_MAX
) {
736 /* Ensure that a STOP/RESUME pair of events is emitted if a
737 * vmstop request was pending. The BLOCK_IO_ERROR event, for
738 * example, according to documentation is always followed by
741 if (runstate_is_running()) {
742 qapi_event_send_stop(&error_abort
);
745 runstate_set(RUN_STATE_RUNNING
);
746 vm_state_notify(1, RUN_STATE_RUNNING
);
750 qapi_event_send_resume(&error_abort
);
754 /***********************************************************/
755 /* real time host monotonic timer */
757 /***********************************************************/
758 /* host time/date access */
759 void qemu_get_timedate(struct tm
*tm
, int offset
)
765 if (rtc_date_offset
== -1) {
769 localtime_r(&ti
, tm
);
771 ti
-= rtc_date_offset
;
776 int qemu_timedate_diff(struct tm
*tm
)
780 if (rtc_date_offset
== -1)
782 seconds
= mktimegm(tm
);
785 tmp
.tm_isdst
= -1; /* use timezone to figure it out */
786 seconds
= mktime(&tmp
);
789 seconds
= mktimegm(tm
) + rtc_date_offset
;
791 return seconds
- time(NULL
);
794 static void configure_rtc_date_offset(const char *startdate
, int legacy
)
796 time_t rtc_start_date
;
799 if (!strcmp(startdate
, "now") && legacy
) {
800 rtc_date_offset
= -1;
802 if (sscanf(startdate
, "%d-%d-%dT%d:%d:%d",
810 } else if (sscanf(startdate
, "%d-%d-%d",
822 rtc_start_date
= mktimegm(&tm
);
823 if (rtc_start_date
== -1) {
825 fprintf(stderr
, "Invalid date format. Valid formats are:\n"
826 "'2006-06-17T16:01:21' or '2006-06-17'\n");
829 rtc_date_offset
= time(NULL
) - rtc_start_date
;
833 static void configure_rtc(QemuOpts
*opts
)
837 value
= qemu_opt_get(opts
, "base");
839 if (!strcmp(value
, "utc")) {
841 } else if (!strcmp(value
, "localtime")) {
844 configure_rtc_date_offset(value
, 0);
847 value
= qemu_opt_get(opts
, "clock");
849 if (!strcmp(value
, "host")) {
850 rtc_clock
= QEMU_CLOCK_HOST
;
851 } else if (!strcmp(value
, "rt")) {
852 rtc_clock
= QEMU_CLOCK_REALTIME
;
853 } else if (!strcmp(value
, "vm")) {
854 rtc_clock
= QEMU_CLOCK_VIRTUAL
;
856 fprintf(stderr
, "qemu: invalid option value '%s'\n", value
);
860 value
= qemu_opt_get(opts
, "driftfix");
862 if (!strcmp(value
, "slew")) {
863 static GlobalProperty slew_lost_ticks
[] = {
865 .driver
= "mc146818rtc",
866 .property
= "lost_tick_policy",
869 { /* end of list */ }
872 qdev_prop_register_global_list(slew_lost_ticks
);
873 } else if (!strcmp(value
, "none")) {
874 /* discard is default */
876 fprintf(stderr
, "qemu: invalid option value '%s'\n", value
);
882 /***********************************************************/
883 /* Bluetooth support */
886 static struct HCIInfo
*hci_table
[MAX_NICS
];
888 struct HCIInfo
*qemu_next_hci(void)
890 if (cur_hci
== nb_hcis
)
893 return hci_table
[cur_hci
++];
896 static int bt_hci_parse(const char *str
)
901 if (nb_hcis
>= MAX_NICS
) {
902 fprintf(stderr
, "qemu: Too many bluetooth HCIs (max %i).\n", MAX_NICS
);
915 bdaddr
.b
[5] = 0x56 + nb_hcis
;
916 hci
->bdaddr_set(hci
, bdaddr
.b
);
918 hci_table
[nb_hcis
++] = hci
;
923 static void bt_vhci_add(int vlan_id
)
925 struct bt_scatternet_s
*vlan
= qemu_find_bt_vlan(vlan_id
);
928 fprintf(stderr
, "qemu: warning: adding a VHCI to "
929 "an empty scatternet %i\n", vlan_id
);
931 bt_vhci_init(bt_new_hci(vlan
));
934 static struct bt_device_s
*bt_device_add(const char *opt
)
936 struct bt_scatternet_s
*vlan
;
938 char *endp
= strstr(opt
, ",vlan=");
939 int len
= (endp
? endp
- opt
: strlen(opt
)) + 1;
942 pstrcpy(devname
, MIN(sizeof(devname
), len
), opt
);
945 vlan_id
= strtol(endp
+ 6, &endp
, 0);
947 fprintf(stderr
, "qemu: unrecognised bluetooth vlan Id\n");
952 vlan
= qemu_find_bt_vlan(vlan_id
);
955 fprintf(stderr
, "qemu: warning: adding a slave device to "
956 "an empty scatternet %i\n", vlan_id
);
958 if (!strcmp(devname
, "keyboard"))
959 return bt_keyboard_init(vlan
);
961 fprintf(stderr
, "qemu: unsupported bluetooth device `%s'\n", devname
);
965 static int bt_parse(const char *opt
)
967 const char *endp
, *p
;
970 if (strstart(opt
, "hci", &endp
)) {
971 if (!*endp
|| *endp
== ',') {
973 if (!strstart(endp
, ",vlan=", 0))
976 return bt_hci_parse(opt
);
978 } else if (strstart(opt
, "vhci", &endp
)) {
979 if (!*endp
|| *endp
== ',') {
981 if (strstart(endp
, ",vlan=", &p
)) {
982 vlan
= strtol(p
, (char **) &endp
, 0);
984 fprintf(stderr
, "qemu: bad scatternet '%s'\n", p
);
988 fprintf(stderr
, "qemu: bad parameter '%s'\n", endp
+ 1);
997 } else if (strstart(opt
, "device:", &endp
))
998 return !bt_device_add(endp
);
1000 fprintf(stderr
, "qemu: bad bluetooth parameter '%s'\n", opt
);
1004 static int parse_sandbox(QemuOpts
*opts
, void *opaque
)
1006 /* FIXME: change this to true for 1.3 */
1007 if (qemu_opt_get_bool(opts
, "enable", false)) {
1008 #ifdef CONFIG_SECCOMP
1009 if (seccomp_start() < 0) {
1010 qerror_report(ERROR_CLASS_GENERIC_ERROR
,
1011 "failed to install seccomp syscall filter in the kernel");
1015 qerror_report(ERROR_CLASS_GENERIC_ERROR
,
1016 "sandboxing request but seccomp is not compiled into this build");
1024 static int parse_name(QemuOpts
*opts
, void *opaque
)
1026 const char *proc_name
;
1028 if (qemu_opt_get(opts
, "debug-threads")) {
1029 qemu_thread_naming(qemu_opt_get_bool(opts
, "debug-threads", false));
1031 qemu_name
= qemu_opt_get(opts
, "guest");
1033 proc_name
= qemu_opt_get(opts
, "process");
1035 os_set_proc_name(proc_name
);
1041 bool usb_enabled(bool default_usb
)
1043 return qemu_opt_get_bool(qemu_get_machine_opts(), "usb",
1044 has_defaults
&& default_usb
);
1048 static int parse_add_fd(QemuOpts
*opts
, void *opaque
)
1050 int fd
, dupfd
, flags
;
1052 const char *fd_opaque
= NULL
;
1054 fd
= qemu_opt_get_number(opts
, "fd", -1);
1055 fdset_id
= qemu_opt_get_number(opts
, "set", -1);
1056 fd_opaque
= qemu_opt_get(opts
, "opaque");
1059 qerror_report(ERROR_CLASS_GENERIC_ERROR
,
1060 "fd option is required and must be non-negative");
1064 if (fd
<= STDERR_FILENO
) {
1065 qerror_report(ERROR_CLASS_GENERIC_ERROR
,
1066 "fd cannot be a standard I/O stream");
1071 * All fds inherited across exec() necessarily have FD_CLOEXEC
1072 * clear, while qemu sets FD_CLOEXEC on all other fds used internally.
1074 flags
= fcntl(fd
, F_GETFD
);
1075 if (flags
== -1 || (flags
& FD_CLOEXEC
)) {
1076 qerror_report(ERROR_CLASS_GENERIC_ERROR
,
1077 "fd is not valid or already in use");
1082 qerror_report(ERROR_CLASS_GENERIC_ERROR
,
1083 "set option is required and must be non-negative");
1087 #ifdef F_DUPFD_CLOEXEC
1088 dupfd
= fcntl(fd
, F_DUPFD_CLOEXEC
, 0);
1092 qemu_set_cloexec(dupfd
);
1096 qerror_report(ERROR_CLASS_GENERIC_ERROR
,
1097 "Error duplicating fd: %s", strerror(errno
));
1101 /* add the duplicate fd, and optionally the opaque string, to the fd set */
1102 monitor_fdset_add_fd(dupfd
, true, fdset_id
, fd_opaque
? true : false,
1108 static int cleanup_add_fd(QemuOpts
*opts
, void *opaque
)
1112 fd
= qemu_opt_get_number(opts
, "fd", -1);
1119 /***********************************************************/
1120 /* QEMU Block devices */
1122 #define HD_OPTS "media=disk"
1123 #define CDROM_OPTS "media=cdrom"
1125 #define PFLASH_OPTS ""
1129 static int drive_init_func(QemuOpts
*opts
, void *opaque
)
1131 BlockInterfaceType
*block_default_type
= opaque
;
1133 return drive_new(opts
, *block_default_type
) == NULL
;
1136 static int drive_enable_snapshot(QemuOpts
*opts
, void *opaque
)
1138 if (NULL
== qemu_opt_get(opts
, "snapshot")) {
1139 qemu_opt_set(opts
, "snapshot", "on");
1144 static void default_drive(int enable
, int snapshot
, BlockInterfaceType type
,
1145 int index
, const char *optstr
)
1149 if (!enable
|| drive_get_by_index(type
, index
)) {
1153 opts
= drive_add(type
, index
, NULL
, optstr
);
1155 drive_enable_snapshot(opts
, NULL
);
1157 if (!drive_new(opts
, type
)) {
1162 void qemu_register_boot_set(QEMUBootSetHandler
*func
, void *opaque
)
1164 boot_set_handler
= func
;
1165 boot_set_opaque
= opaque
;
1168 int qemu_boot_set(const char *boot_order
)
1170 if (!boot_set_handler
) {
1173 return boot_set_handler(boot_set_opaque
, boot_order
);
1176 static void validate_bootdevices(const char *devices
)
1178 /* We just do some generic consistency checks */
1182 for (p
= devices
; *p
!= '\0'; p
++) {
1183 /* Allowed boot devices are:
1184 * a-b: floppy disk drives
1185 * c-f: IDE disk drives
1186 * g-m: machine implementation dependent drives
1187 * n-p: network devices
1188 * It's up to each machine implementation to check if the given boot
1189 * devices match the actual hardware implementation and firmware
1192 if (*p
< 'a' || *p
> 'p') {
1193 fprintf(stderr
, "Invalid boot device '%c'\n", *p
);
1196 if (bitmap
& (1 << (*p
- 'a'))) {
1197 fprintf(stderr
, "Boot device '%c' was given twice\n", *p
);
1200 bitmap
|= 1 << (*p
- 'a');
1204 static void restore_boot_order(void *opaque
)
1206 char *normal_boot_order
= opaque
;
1207 static int first
= 1;
1209 /* Restore boot order and remove ourselves after the first boot */
1215 qemu_boot_set(normal_boot_order
);
1217 qemu_unregister_reset(restore_boot_order
, normal_boot_order
);
1218 g_free(normal_boot_order
);
1221 void add_boot_device_path(int32_t bootindex
, DeviceState
*dev
,
1224 FWBootEntry
*node
, *i
;
1226 if (bootindex
< 0) {
1230 assert(dev
!= NULL
|| suffix
!= NULL
);
1232 node
= g_malloc0(sizeof(FWBootEntry
));
1233 node
->bootindex
= bootindex
;
1234 node
->suffix
= g_strdup(suffix
);
1237 QTAILQ_FOREACH(i
, &fw_boot_order
, link
) {
1238 if (i
->bootindex
== bootindex
) {
1239 fprintf(stderr
, "Two devices with same boot index %d\n", bootindex
);
1241 } else if (i
->bootindex
< bootindex
) {
1244 QTAILQ_INSERT_BEFORE(i
, node
, link
);
1247 QTAILQ_INSERT_TAIL(&fw_boot_order
, node
, link
);
1250 DeviceState
*get_boot_device(uint32_t position
)
1252 uint32_t counter
= 0;
1253 FWBootEntry
*i
= NULL
;
1254 DeviceState
*res
= NULL
;
1256 if (!QTAILQ_EMPTY(&fw_boot_order
)) {
1257 QTAILQ_FOREACH(i
, &fw_boot_order
, link
) {
1258 if (counter
== position
) {
1269 * This function returns null terminated string that consist of new line
1270 * separated device paths.
1272 * memory pointed by "size" is assigned total length of the array in bytes
1275 char *get_boot_devices_list(size_t *size
, bool ignore_suffixes
)
1281 QTAILQ_FOREACH(i
, &fw_boot_order
, link
) {
1282 char *devpath
= NULL
, *bootpath
;
1286 devpath
= qdev_get_fw_dev_path(i
->dev
);
1290 if (i
->suffix
&& !ignore_suffixes
&& devpath
) {
1291 size_t bootpathlen
= strlen(devpath
) + strlen(i
->suffix
) + 1;
1293 bootpath
= g_malloc(bootpathlen
);
1294 snprintf(bootpath
, bootpathlen
, "%s%s", devpath
, i
->suffix
);
1296 } else if (devpath
) {
1298 } else if (!ignore_suffixes
) {
1300 bootpath
= g_strdup(i
->suffix
);
1302 bootpath
= g_strdup("");
1306 list
[total
-1] = '\n';
1308 len
= strlen(bootpath
) + 1;
1309 list
= g_realloc(list
, total
+ len
);
1310 memcpy(&list
[total
], bootpath
, len
);
1317 if (boot_strict
&& *size
> 0) {
1318 list
[total
-1] = '\n';
1319 list
= g_realloc(list
, total
+ 5);
1320 memcpy(&list
[total
], "HALT", 5);
1326 static QemuOptsList qemu_smp_opts
= {
1328 .implied_opt_name
= "cpus",
1329 .merge_lists
= true,
1330 .head
= QTAILQ_HEAD_INITIALIZER(qemu_smp_opts
.head
),
1334 .type
= QEMU_OPT_NUMBER
,
1337 .type
= QEMU_OPT_NUMBER
,
1340 .type
= QEMU_OPT_NUMBER
,
1343 .type
= QEMU_OPT_NUMBER
,
1346 .type
= QEMU_OPT_NUMBER
,
1348 { /*End of list */ }
1352 static void smp_parse(QemuOpts
*opts
)
1356 unsigned cpus
= qemu_opt_get_number(opts
, "cpus", 0);
1357 unsigned sockets
= qemu_opt_get_number(opts
, "sockets", 0);
1358 unsigned cores
= qemu_opt_get_number(opts
, "cores", 0);
1359 unsigned threads
= qemu_opt_get_number(opts
, "threads", 0);
1361 /* compute missing values, prefer sockets over cores over threads */
1362 if (cpus
== 0 || sockets
== 0) {
1363 sockets
= sockets
> 0 ? sockets
: 1;
1364 cores
= cores
> 0 ? cores
: 1;
1365 threads
= threads
> 0 ? threads
: 1;
1367 cpus
= cores
* threads
* sockets
;
1371 threads
= threads
> 0 ? threads
: 1;
1372 cores
= cpus
/ (sockets
* threads
);
1374 threads
= cpus
/ (cores
* sockets
);
1378 max_cpus
= qemu_opt_get_number(opts
, "maxcpus", 0);
1381 smp_cores
= cores
> 0 ? cores
: 1;
1382 smp_threads
= threads
> 0 ? threads
: 1;
1386 if (max_cpus
== 0) {
1387 max_cpus
= smp_cpus
;
1390 if (max_cpus
> MAX_CPUMASK_BITS
) {
1391 fprintf(stderr
, "Unsupported number of maxcpus\n");
1394 if (max_cpus
< smp_cpus
) {
1395 fprintf(stderr
, "maxcpus must be equal to or greater than smp\n");
1401 static void configure_realtime(QemuOpts
*opts
)
1405 enable_mlock
= qemu_opt_get_bool(opts
, "mlock", true);
1408 if (os_mlock() < 0) {
1409 fprintf(stderr
, "qemu: locking memory failed\n");
1416 static void configure_msg(QemuOpts
*opts
)
1418 enable_timestamp_msg
= qemu_opt_get_bool(opts
, "timestamp", true);
1421 /***********************************************************/
1424 static int usb_device_add(const char *devname
)
1426 USBDevice
*dev
= NULL
;
1427 #ifndef CONFIG_LINUX
1431 if (!usb_enabled(false)) {
1435 /* drivers with .usbdevice_name entry in USBDeviceInfo */
1436 dev
= usbdevice_create(devname
);
1440 /* the other ones */
1441 #ifndef CONFIG_LINUX
1442 /* only the linux version is qdev-ified, usb-bsd still needs this */
1443 if (strstart(devname
, "host:", &p
)) {
1444 dev
= usb_host_device_open(usb_bus_find(-1), p
);
1454 static int usb_device_del(const char *devname
)
1459 if (strstart(devname
, "host:", &p
)) {
1463 if (!usb_enabled(false)) {
1467 p
= strchr(devname
, '.');
1470 bus_num
= strtoul(devname
, NULL
, 0);
1471 addr
= strtoul(p
+ 1, NULL
, 0);
1473 return usb_device_delete_addr(bus_num
, addr
);
1476 static int usb_parse(const char *cmdline
)
1479 r
= usb_device_add(cmdline
);
1481 fprintf(stderr
, "qemu: could not add USB device '%s'\n", cmdline
);
1486 void do_usb_add(Monitor
*mon
, const QDict
*qdict
)
1488 const char *devname
= qdict_get_str(qdict
, "devname");
1489 if (usb_device_add(devname
) < 0) {
1490 error_report("could not add USB device '%s'", devname
);
1494 void do_usb_del(Monitor
*mon
, const QDict
*qdict
)
1496 const char *devname
= qdict_get_str(qdict
, "devname");
1497 if (usb_device_del(devname
) < 0) {
1498 error_report("could not delete USB device '%s'", devname
);
1502 /***********************************************************/
1503 /* PCMCIA/Cardbus */
1505 static struct pcmcia_socket_entry_s
{
1506 PCMCIASocket
*socket
;
1507 struct pcmcia_socket_entry_s
*next
;
1508 } *pcmcia_sockets
= 0;
1510 void pcmcia_socket_register(PCMCIASocket
*socket
)
1512 struct pcmcia_socket_entry_s
*entry
;
1514 entry
= g_malloc(sizeof(struct pcmcia_socket_entry_s
));
1515 entry
->socket
= socket
;
1516 entry
->next
= pcmcia_sockets
;
1517 pcmcia_sockets
= entry
;
1520 void pcmcia_socket_unregister(PCMCIASocket
*socket
)
1522 struct pcmcia_socket_entry_s
*entry
, **ptr
;
1524 ptr
= &pcmcia_sockets
;
1525 for (entry
= *ptr
; entry
; ptr
= &entry
->next
, entry
= *ptr
)
1526 if (entry
->socket
== socket
) {
1532 void pcmcia_info(Monitor
*mon
, const QDict
*qdict
)
1534 struct pcmcia_socket_entry_s
*iter
;
1536 if (!pcmcia_sockets
)
1537 monitor_printf(mon
, "No PCMCIA sockets\n");
1539 for (iter
= pcmcia_sockets
; iter
; iter
= iter
->next
)
1540 monitor_printf(mon
, "%s: %s\n", iter
->socket
->slot_string
,
1541 iter
->socket
->attached
? iter
->socket
->card_string
:
1545 /***********************************************************/
1546 /* machine registration */
1548 MachineState
*current_machine
;
1550 static void machine_class_init(ObjectClass
*oc
, void *data
)
1552 MachineClass
*mc
= MACHINE_CLASS(oc
);
1553 QEMUMachine
*qm
= data
;
1555 mc
->name
= qm
->name
;
1556 mc
->alias
= qm
->alias
;
1557 mc
->desc
= qm
->desc
;
1558 mc
->init
= qm
->init
;
1559 mc
->reset
= qm
->reset
;
1560 mc
->hot_add_cpu
= qm
->hot_add_cpu
;
1561 mc
->kvm_type
= qm
->kvm_type
;
1562 mc
->block_default_type
= qm
->block_default_type
;
1563 mc
->max_cpus
= qm
->max_cpus
;
1564 mc
->no_serial
= qm
->no_serial
;
1565 mc
->no_parallel
= qm
->no_parallel
;
1566 mc
->use_virtcon
= qm
->use_virtcon
;
1567 mc
->use_sclp
= qm
->use_sclp
;
1568 mc
->no_floppy
= qm
->no_floppy
;
1569 mc
->no_cdrom
= qm
->no_cdrom
;
1570 mc
->no_sdcard
= qm
->no_sdcard
;
1571 mc
->is_default
= qm
->is_default
;
1572 mc
->default_machine_opts
= qm
->default_machine_opts
;
1573 mc
->default_boot_order
= qm
->default_boot_order
;
1574 mc
->compat_props
= qm
->compat_props
;
1575 mc
->hw_version
= qm
->hw_version
;
1578 int qemu_register_machine(QEMUMachine
*m
)
1580 char *name
= g_strconcat(m
->name
, TYPE_MACHINE_SUFFIX
, NULL
);
1583 .parent
= TYPE_MACHINE
,
1584 .class_init
= machine_class_init
,
1585 .class_data
= (void *)m
,
1594 static MachineClass
*find_machine(const char *name
)
1596 GSList
*el
, *machines
= object_class_get_list(TYPE_MACHINE
, false);
1597 MachineClass
*mc
= NULL
;
1599 for (el
= machines
; el
; el
= el
->next
) {
1600 MachineClass
*temp
= el
->data
;
1602 if (!strcmp(temp
->name
, name
)) {
1607 !strcmp(temp
->alias
, name
)) {
1613 g_slist_free(machines
);
1617 MachineClass
*find_default_machine(void)
1619 GSList
*el
, *machines
= object_class_get_list(TYPE_MACHINE
, false);
1620 MachineClass
*mc
= NULL
;
1622 for (el
= machines
; el
; el
= el
->next
) {
1623 MachineClass
*temp
= el
->data
;
1625 if (temp
->is_default
) {
1631 g_slist_free(machines
);
1635 MachineInfoList
*qmp_query_machines(Error
**errp
)
1637 GSList
*el
, *machines
= object_class_get_list(TYPE_MACHINE
, false);
1638 MachineInfoList
*mach_list
= NULL
;
1640 for (el
= machines
; el
; el
= el
->next
) {
1641 MachineClass
*mc
= el
->data
;
1642 MachineInfoList
*entry
;
1645 info
= g_malloc0(sizeof(*info
));
1646 if (mc
->is_default
) {
1647 info
->has_is_default
= true;
1648 info
->is_default
= true;
1652 info
->has_alias
= true;
1653 info
->alias
= g_strdup(mc
->alias
);
1656 info
->name
= g_strdup(mc
->name
);
1657 info
->cpu_max
= !mc
->max_cpus
? 1 : mc
->max_cpus
;
1659 entry
= g_malloc0(sizeof(*entry
));
1660 entry
->value
= info
;
1661 entry
->next
= mach_list
;
1665 g_slist_free(machines
);
1669 /***********************************************************/
1670 /* main execution loop */
1672 struct vm_change_state_entry
{
1673 VMChangeStateHandler
*cb
;
1675 QLIST_ENTRY (vm_change_state_entry
) entries
;
1678 static QLIST_HEAD(vm_change_state_head
, vm_change_state_entry
) vm_change_state_head
;
1680 VMChangeStateEntry
*qemu_add_vm_change_state_handler(VMChangeStateHandler
*cb
,
1683 VMChangeStateEntry
*e
;
1685 e
= g_malloc0(sizeof (*e
));
1689 QLIST_INSERT_HEAD(&vm_change_state_head
, e
, entries
);
1693 void qemu_del_vm_change_state_handler(VMChangeStateEntry
*e
)
1695 QLIST_REMOVE (e
, entries
);
1699 void vm_state_notify(int running
, RunState state
)
1701 VMChangeStateEntry
*e
;
1703 trace_vm_state_notify(running
, state
);
1705 for (e
= vm_change_state_head
.lh_first
; e
; e
= e
->entries
.le_next
) {
1706 e
->cb(e
->opaque
, running
, state
);
1710 /* reset/shutdown handler */
1712 typedef struct QEMUResetEntry
{
1713 QTAILQ_ENTRY(QEMUResetEntry
) entry
;
1714 QEMUResetHandler
*func
;
1718 static QTAILQ_HEAD(reset_handlers
, QEMUResetEntry
) reset_handlers
=
1719 QTAILQ_HEAD_INITIALIZER(reset_handlers
);
1720 static int reset_requested
;
1721 static int shutdown_requested
, shutdown_signal
= -1;
1722 static pid_t shutdown_pid
;
1723 static int powerdown_requested
;
1724 static int debug_requested
;
1725 static int suspend_requested
;
1726 static WakeupReason wakeup_reason
;
1727 static NotifierList powerdown_notifiers
=
1728 NOTIFIER_LIST_INITIALIZER(powerdown_notifiers
);
1729 static NotifierList suspend_notifiers
=
1730 NOTIFIER_LIST_INITIALIZER(suspend_notifiers
);
1731 static NotifierList wakeup_notifiers
=
1732 NOTIFIER_LIST_INITIALIZER(wakeup_notifiers
);
1733 static uint32_t wakeup_reason_mask
= ~(1 << QEMU_WAKEUP_REASON_NONE
);
1735 int qemu_shutdown_requested_get(void)
1737 return shutdown_requested
;
1740 int qemu_reset_requested_get(void)
1742 return reset_requested
;
1745 static int qemu_shutdown_requested(void)
1747 int r
= shutdown_requested
;
1748 shutdown_requested
= 0;
1752 static void qemu_kill_report(void)
1754 if (!qtest_driver() && shutdown_signal
!= -1) {
1755 fprintf(stderr
, "qemu: terminating on signal %d", shutdown_signal
);
1756 if (shutdown_pid
== 0) {
1757 /* This happens for eg ^C at the terminal, so it's worth
1758 * avoiding printing an odd message in that case.
1760 fputc('\n', stderr
);
1762 fprintf(stderr
, " from pid " FMT_pid
"\n", shutdown_pid
);
1764 shutdown_signal
= -1;
1768 static int qemu_reset_requested(void)
1770 int r
= reset_requested
;
1771 reset_requested
= 0;
1775 static int qemu_suspend_requested(void)
1777 int r
= suspend_requested
;
1778 suspend_requested
= 0;
1782 static WakeupReason
qemu_wakeup_requested(void)
1784 return wakeup_reason
;
1787 static int qemu_powerdown_requested(void)
1789 int r
= powerdown_requested
;
1790 powerdown_requested
= 0;
1794 static int qemu_debug_requested(void)
1796 int r
= debug_requested
;
1797 debug_requested
= 0;
1801 void qemu_register_reset(QEMUResetHandler
*func
, void *opaque
)
1803 QEMUResetEntry
*re
= g_malloc0(sizeof(QEMUResetEntry
));
1806 re
->opaque
= opaque
;
1807 QTAILQ_INSERT_TAIL(&reset_handlers
, re
, entry
);
1810 void qemu_unregister_reset(QEMUResetHandler
*func
, void *opaque
)
1814 QTAILQ_FOREACH(re
, &reset_handlers
, entry
) {
1815 if (re
->func
== func
&& re
->opaque
== opaque
) {
1816 QTAILQ_REMOVE(&reset_handlers
, re
, entry
);
1823 void qemu_devices_reset(void)
1825 QEMUResetEntry
*re
, *nre
;
1827 /* reset all devices */
1828 QTAILQ_FOREACH_SAFE(re
, &reset_handlers
, entry
, nre
) {
1829 re
->func(re
->opaque
);
1833 void qemu_system_reset(bool report
)
1837 mc
= current_machine
? MACHINE_GET_CLASS(current_machine
) : NULL
;
1839 if (mc
&& mc
->reset
) {
1842 qemu_devices_reset();
1845 qapi_event_send_reset(&error_abort
);
1847 cpu_synchronize_all_post_reset();
1850 void qemu_system_reset_request(void)
1853 shutdown_requested
= 1;
1855 reset_requested
= 1;
1858 qemu_notify_event();
1861 static void qemu_system_suspend(void)
1864 notifier_list_notify(&suspend_notifiers
, NULL
);
1865 runstate_set(RUN_STATE_SUSPENDED
);
1866 qapi_event_send_suspend(&error_abort
);
1869 void qemu_system_suspend_request(void)
1871 if (runstate_check(RUN_STATE_SUSPENDED
)) {
1874 suspend_requested
= 1;
1876 qemu_notify_event();
1879 void qemu_register_suspend_notifier(Notifier
*notifier
)
1881 notifier_list_add(&suspend_notifiers
, notifier
);
1884 void qemu_system_wakeup_request(WakeupReason reason
)
1886 trace_system_wakeup_request(reason
);
1888 if (!runstate_check(RUN_STATE_SUSPENDED
)) {
1891 if (!(wakeup_reason_mask
& (1 << reason
))) {
1894 runstate_set(RUN_STATE_RUNNING
);
1895 wakeup_reason
= reason
;
1896 qemu_notify_event();
1899 void qemu_system_wakeup_enable(WakeupReason reason
, bool enabled
)
1902 wakeup_reason_mask
|= (1 << reason
);
1904 wakeup_reason_mask
&= ~(1 << reason
);
1908 void qemu_register_wakeup_notifier(Notifier
*notifier
)
1910 notifier_list_add(&wakeup_notifiers
, notifier
);
1913 void qemu_system_killed(int signal
, pid_t pid
)
1915 shutdown_signal
= signal
;
1918 qemu_system_shutdown_request();
1921 void qemu_system_shutdown_request(void)
1923 shutdown_requested
= 1;
1924 qemu_notify_event();
1927 static void qemu_system_powerdown(void)
1929 qapi_event_send_powerdown(&error_abort
);
1930 notifier_list_notify(&powerdown_notifiers
, NULL
);
1933 void qemu_system_powerdown_request(void)
1935 powerdown_requested
= 1;
1936 qemu_notify_event();
1939 void qemu_register_powerdown_notifier(Notifier
*notifier
)
1941 notifier_list_add(&powerdown_notifiers
, notifier
);
1944 void qemu_system_debug_request(void)
1946 debug_requested
= 1;
1947 qemu_notify_event();
1950 static bool main_loop_should_exit(void)
1953 if (qemu_debug_requested()) {
1954 vm_stop(RUN_STATE_DEBUG
);
1956 if (qemu_suspend_requested()) {
1957 qemu_system_suspend();
1959 if (qemu_shutdown_requested()) {
1961 qapi_event_send_shutdown(&error_abort
);
1963 vm_stop(RUN_STATE_SHUTDOWN
);
1968 if (qemu_reset_requested()) {
1970 cpu_synchronize_all_states();
1971 qemu_system_reset(VMRESET_REPORT
);
1973 if (runstate_needs_reset()) {
1974 runstate_set(RUN_STATE_PAUSED
);
1977 if (qemu_wakeup_requested()) {
1979 cpu_synchronize_all_states();
1980 qemu_system_reset(VMRESET_SILENT
);
1981 notifier_list_notify(&wakeup_notifiers
, &wakeup_reason
);
1982 wakeup_reason
= QEMU_WAKEUP_REASON_NONE
;
1984 qapi_event_send_wakeup(&error_abort
);
1986 if (qemu_powerdown_requested()) {
1987 qemu_system_powerdown();
1989 if (qemu_vmstop_requested(&r
)) {
1995 static void main_loop(void)
1999 #ifdef CONFIG_PROFILER
2003 nonblocking
= !kvm_enabled() && !xen_enabled() && last_io
> 0;
2004 #ifdef CONFIG_PROFILER
2005 ti
= profile_getclock();
2007 last_io
= main_loop_wait(nonblocking
);
2008 #ifdef CONFIG_PROFILER
2009 dev_time
+= profile_getclock() - ti
;
2011 } while (!main_loop_should_exit());
2014 static void version(void)
2016 printf("QEMU emulator version " QEMU_VERSION QEMU_PKGVERSION
", Copyright (c) 2003-2008 Fabrice Bellard\n");
2019 static void help(int exitcode
)
2022 printf("usage: %s [options] [disk_image]\n\n"
2023 "'disk_image' is a raw hard disk image for IDE hard disk 0\n\n",
2024 error_get_progname());
2026 #define QEMU_OPTIONS_GENERATE_HELP
2027 #include "qemu-options-wrapper.h"
2029 printf("\nDuring emulation, the following keys are useful:\n"
2030 "ctrl-alt-f toggle full screen\n"
2031 "ctrl-alt-n switch to virtual console 'n'\n"
2032 "ctrl-alt toggle mouse and keyboard grab\n"
2034 "When using -nographic, press 'ctrl-a h' to get some help.\n");
2039 #define HAS_ARG 0x0001
2041 typedef struct QEMUOption
{
2048 static const QEMUOption qemu_options
[] = {
2049 { "h", 0, QEMU_OPTION_h
, QEMU_ARCH_ALL
},
2050 #define QEMU_OPTIONS_GENERATE_OPTIONS
2051 #include "qemu-options-wrapper.h"
2055 static bool vga_available(void)
2057 return object_class_by_name("VGA") || object_class_by_name("isa-vga");
2060 static bool cirrus_vga_available(void)
2062 return object_class_by_name("cirrus-vga")
2063 || object_class_by_name("isa-cirrus-vga");
2066 static bool vmware_vga_available(void)
2068 return object_class_by_name("vmware-svga");
2071 static bool qxl_vga_available(void)
2073 return object_class_by_name("qxl-vga");
2076 static bool tcx_vga_available(void)
2078 return object_class_by_name("SUNW,tcx");
2081 static bool cg3_vga_available(void)
2083 return object_class_by_name("cgthree");
2086 static void select_vgahw (const char *p
)
2090 assert(vga_interface_type
== VGA_NONE
);
2091 if (strstart(p
, "std", &opts
)) {
2092 if (vga_available()) {
2093 vga_interface_type
= VGA_STD
;
2095 fprintf(stderr
, "Error: standard VGA not available\n");
2098 } else if (strstart(p
, "cirrus", &opts
)) {
2099 if (cirrus_vga_available()) {
2100 vga_interface_type
= VGA_CIRRUS
;
2102 fprintf(stderr
, "Error: Cirrus VGA not available\n");
2105 } else if (strstart(p
, "vmware", &opts
)) {
2106 if (vmware_vga_available()) {
2107 vga_interface_type
= VGA_VMWARE
;
2109 fprintf(stderr
, "Error: VMWare SVGA not available\n");
2112 } else if (strstart(p
, "xenfb", &opts
)) {
2113 vga_interface_type
= VGA_XENFB
;
2114 } else if (strstart(p
, "qxl", &opts
)) {
2115 if (qxl_vga_available()) {
2116 vga_interface_type
= VGA_QXL
;
2118 fprintf(stderr
, "Error: QXL VGA not available\n");
2121 } else if (strstart(p
, "tcx", &opts
)) {
2122 if (tcx_vga_available()) {
2123 vga_interface_type
= VGA_TCX
;
2125 fprintf(stderr
, "Error: TCX framebuffer not available\n");
2128 } else if (strstart(p
, "cg3", &opts
)) {
2129 if (cg3_vga_available()) {
2130 vga_interface_type
= VGA_CG3
;
2132 fprintf(stderr
, "Error: CG3 framebuffer not available\n");
2135 } else if (!strstart(p
, "none", &opts
)) {
2137 fprintf(stderr
, "Unknown vga type: %s\n", p
);
2141 const char *nextopt
;
2143 if (strstart(opts
, ",retrace=", &nextopt
)) {
2145 if (strstart(opts
, "dumb", &nextopt
))
2146 vga_retrace_method
= VGA_RETRACE_DUMB
;
2147 else if (strstart(opts
, "precise", &nextopt
))
2148 vga_retrace_method
= VGA_RETRACE_PRECISE
;
2149 else goto invalid_vga
;
2150 } else goto invalid_vga
;
2155 static DisplayType
select_display(const char *p
)
2158 DisplayType display
= DT_DEFAULT
;
2160 if (strstart(p
, "sdl", &opts
)) {
2164 const char *nextopt
;
2166 if (strstart(opts
, ",frame=", &nextopt
)) {
2168 if (strstart(opts
, "on", &nextopt
)) {
2170 } else if (strstart(opts
, "off", &nextopt
)) {
2173 goto invalid_sdl_args
;
2175 } else if (strstart(opts
, ",alt_grab=", &nextopt
)) {
2177 if (strstart(opts
, "on", &nextopt
)) {
2179 } else if (strstart(opts
, "off", &nextopt
)) {
2182 goto invalid_sdl_args
;
2184 } else if (strstart(opts
, ",ctrl_grab=", &nextopt
)) {
2186 if (strstart(opts
, "on", &nextopt
)) {
2188 } else if (strstart(opts
, "off", &nextopt
)) {
2191 goto invalid_sdl_args
;
2193 } else if (strstart(opts
, ",window_close=", &nextopt
)) {
2195 if (strstart(opts
, "on", &nextopt
)) {
2197 } else if (strstart(opts
, "off", &nextopt
)) {
2200 goto invalid_sdl_args
;
2204 fprintf(stderr
, "Invalid SDL option string: %s\n", p
);
2210 fprintf(stderr
, "SDL support is disabled\n");
2213 } else if (strstart(p
, "vnc", &opts
)) {
2218 const char *nextopt
;
2220 if (strstart(opts
, "=", &nextopt
)) {
2221 vnc_display
= nextopt
;
2225 fprintf(stderr
, "VNC requires a display argument vnc=<display>\n");
2229 fprintf(stderr
, "VNC support is disabled\n");
2232 } else if (strstart(p
, "curses", &opts
)) {
2233 #ifdef CONFIG_CURSES
2234 display
= DT_CURSES
;
2236 fprintf(stderr
, "Curses support is disabled\n");
2239 } else if (strstart(p
, "gtk", &opts
)) {
2243 const char *nextopt
;
2245 if (strstart(opts
, ",grab_on_hover=", &nextopt
)) {
2247 if (strstart(opts
, "on", &nextopt
)) {
2248 grab_on_hover
= true;
2249 } else if (strstart(opts
, "off", &nextopt
)) {
2250 grab_on_hover
= false;
2252 goto invalid_gtk_args
;
2256 fprintf(stderr
, "Invalid GTK option string: %s\n", p
);
2262 fprintf(stderr
, "GTK support is disabled\n");
2265 } else if (strstart(p
, "none", &opts
)) {
2268 fprintf(stderr
, "Unknown display type: %s\n", p
);
2275 static int balloon_parse(const char *arg
)
2279 if (strcmp(arg
, "none") == 0) {
2283 if (!strncmp(arg
, "virtio", 6)) {
2284 if (arg
[6] == ',') {
2285 /* have params -> parse them */
2286 opts
= qemu_opts_parse(qemu_find_opts("device"), arg
+7, 0);
2290 /* create empty opts */
2291 opts
= qemu_opts_create(qemu_find_opts("device"), NULL
, 0,
2294 qemu_opt_set(opts
, "driver", "virtio-balloon");
2301 char *qemu_find_file(int type
, const char *name
)
2307 /* Try the name as a straight path first */
2308 if (access(name
, R_OK
) == 0) {
2309 trace_load_file(name
, name
);
2310 return g_strdup(name
);
2314 case QEMU_FILE_TYPE_BIOS
:
2317 case QEMU_FILE_TYPE_KEYMAP
:
2318 subdir
= "keymaps/";
2324 for (i
= 0; i
< data_dir_idx
; i
++) {
2325 buf
= g_strdup_printf("%s/%s%s", data_dir
[i
], subdir
, name
);
2326 if (access(buf
, R_OK
) == 0) {
2327 trace_load_file(name
, buf
);
2335 static int device_help_func(QemuOpts
*opts
, void *opaque
)
2337 return qdev_device_help(opts
);
2340 static int device_init_func(QemuOpts
*opts
, void *opaque
)
2344 dev
= qdev_device_add(opts
);
2347 object_unref(OBJECT(dev
));
2351 static int chardev_init_func(QemuOpts
*opts
, void *opaque
)
2353 Error
*local_err
= NULL
;
2355 qemu_chr_new_from_opts(opts
, NULL
, &local_err
);
2357 error_report("%s", error_get_pretty(local_err
));
2358 error_free(local_err
);
2364 #ifdef CONFIG_VIRTFS
2365 static int fsdev_init_func(QemuOpts
*opts
, void *opaque
)
2368 ret
= qemu_fsdev_add(opts
);
2374 static int mon_init_func(QemuOpts
*opts
, void *opaque
)
2376 CharDriverState
*chr
;
2377 const char *chardev
;
2381 mode
= qemu_opt_get(opts
, "mode");
2385 if (strcmp(mode
, "readline") == 0) {
2386 flags
= MONITOR_USE_READLINE
;
2387 } else if (strcmp(mode
, "control") == 0) {
2388 flags
= MONITOR_USE_CONTROL
;
2390 fprintf(stderr
, "unknown monitor mode \"%s\"\n", mode
);
2394 if (qemu_opt_get_bool(opts
, "pretty", 0))
2395 flags
|= MONITOR_USE_PRETTY
;
2397 if (qemu_opt_get_bool(opts
, "default", 0))
2398 flags
|= MONITOR_IS_DEFAULT
;
2400 chardev
= qemu_opt_get(opts
, "chardev");
2401 chr
= qemu_chr_find(chardev
);
2403 fprintf(stderr
, "chardev \"%s\" not found\n", chardev
);
2407 qemu_chr_fe_claim_no_fail(chr
);
2408 monitor_init(chr
, flags
);
2412 static void monitor_parse(const char *optarg
, const char *mode
)
2414 static int monitor_device_index
= 0;
2420 if (strstart(optarg
, "chardev:", &p
)) {
2421 snprintf(label
, sizeof(label
), "%s", p
);
2423 snprintf(label
, sizeof(label
), "compat_monitor%d",
2424 monitor_device_index
);
2425 if (monitor_device_index
== 0) {
2428 opts
= qemu_chr_parse_compat(label
, optarg
);
2430 fprintf(stderr
, "parse error: %s\n", optarg
);
2435 opts
= qemu_opts_create(qemu_find_opts("mon"), label
, 1, NULL
);
2437 fprintf(stderr
, "duplicate chardev: %s\n", label
);
2440 qemu_opt_set(opts
, "mode", mode
);
2441 qemu_opt_set(opts
, "chardev", label
);
2443 qemu_opt_set(opts
, "default", "on");
2444 monitor_device_index
++;
2447 struct device_config
{
2449 DEV_USB
, /* -usbdevice */
2451 DEV_SERIAL
, /* -serial */
2452 DEV_PARALLEL
, /* -parallel */
2453 DEV_VIRTCON
, /* -virtioconsole */
2454 DEV_DEBUGCON
, /* -debugcon */
2455 DEV_GDB
, /* -gdb, -s */
2456 DEV_SCLP
, /* s390 sclp */
2458 const char *cmdline
;
2460 QTAILQ_ENTRY(device_config
) next
;
2463 static QTAILQ_HEAD(, device_config
) device_configs
=
2464 QTAILQ_HEAD_INITIALIZER(device_configs
);
2466 static void add_device_config(int type
, const char *cmdline
)
2468 struct device_config
*conf
;
2470 conf
= g_malloc0(sizeof(*conf
));
2472 conf
->cmdline
= cmdline
;
2473 loc_save(&conf
->loc
);
2474 QTAILQ_INSERT_TAIL(&device_configs
, conf
, next
);
2477 static int foreach_device_config(int type
, int (*func
)(const char *cmdline
))
2479 struct device_config
*conf
;
2482 QTAILQ_FOREACH(conf
, &device_configs
, next
) {
2483 if (conf
->type
!= type
)
2485 loc_push_restore(&conf
->loc
);
2486 rc
= func(conf
->cmdline
);
2487 loc_pop(&conf
->loc
);
2494 static int serial_parse(const char *devname
)
2496 static int index
= 0;
2499 if (strcmp(devname
, "none") == 0)
2501 if (index
== MAX_SERIAL_PORTS
) {
2502 fprintf(stderr
, "qemu: too many serial ports\n");
2505 snprintf(label
, sizeof(label
), "serial%d", index
);
2506 serial_hds
[index
] = qemu_chr_new(label
, devname
, NULL
);
2507 if (!serial_hds
[index
]) {
2508 fprintf(stderr
, "qemu: could not connect serial device"
2509 " to character backend '%s'\n", devname
);
2516 static int parallel_parse(const char *devname
)
2518 static int index
= 0;
2521 if (strcmp(devname
, "none") == 0)
2523 if (index
== MAX_PARALLEL_PORTS
) {
2524 fprintf(stderr
, "qemu: too many parallel ports\n");
2527 snprintf(label
, sizeof(label
), "parallel%d", index
);
2528 parallel_hds
[index
] = qemu_chr_new(label
, devname
, NULL
);
2529 if (!parallel_hds
[index
]) {
2530 fprintf(stderr
, "qemu: could not connect parallel device"
2531 " to character backend '%s'\n", devname
);
2538 static int virtcon_parse(const char *devname
)
2540 QemuOptsList
*device
= qemu_find_opts("device");
2541 static int index
= 0;
2543 QemuOpts
*bus_opts
, *dev_opts
;
2545 if (strcmp(devname
, "none") == 0)
2547 if (index
== MAX_VIRTIO_CONSOLES
) {
2548 fprintf(stderr
, "qemu: too many virtio consoles\n");
2552 bus_opts
= qemu_opts_create(device
, NULL
, 0, &error_abort
);
2553 if (arch_type
== QEMU_ARCH_S390X
) {
2554 qemu_opt_set(bus_opts
, "driver", "virtio-serial-s390");
2556 qemu_opt_set(bus_opts
, "driver", "virtio-serial-pci");
2559 dev_opts
= qemu_opts_create(device
, NULL
, 0, &error_abort
);
2560 qemu_opt_set(dev_opts
, "driver", "virtconsole");
2562 snprintf(label
, sizeof(label
), "virtcon%d", index
);
2563 virtcon_hds
[index
] = qemu_chr_new(label
, devname
, NULL
);
2564 if (!virtcon_hds
[index
]) {
2565 fprintf(stderr
, "qemu: could not connect virtio console"
2566 " to character backend '%s'\n", devname
);
2569 qemu_opt_set(dev_opts
, "chardev", label
);
2575 static int sclp_parse(const char *devname
)
2577 QemuOptsList
*device
= qemu_find_opts("device");
2578 static int index
= 0;
2582 if (strcmp(devname
, "none") == 0) {
2585 if (index
== MAX_SCLP_CONSOLES
) {
2586 fprintf(stderr
, "qemu: too many sclp consoles\n");
2590 assert(arch_type
== QEMU_ARCH_S390X
);
2592 dev_opts
= qemu_opts_create(device
, NULL
, 0, NULL
);
2593 qemu_opt_set(dev_opts
, "driver", "sclpconsole");
2595 snprintf(label
, sizeof(label
), "sclpcon%d", index
);
2596 sclp_hds
[index
] = qemu_chr_new(label
, devname
, NULL
);
2597 if (!sclp_hds
[index
]) {
2598 fprintf(stderr
, "qemu: could not connect sclp console"
2599 " to character backend '%s'\n", devname
);
2602 qemu_opt_set(dev_opts
, "chardev", label
);
2608 static int debugcon_parse(const char *devname
)
2612 if (!qemu_chr_new("debugcon", devname
, NULL
)) {
2615 opts
= qemu_opts_create(qemu_find_opts("device"), "debugcon", 1, NULL
);
2617 fprintf(stderr
, "qemu: already have a debugcon device\n");
2620 qemu_opt_set(opts
, "driver", "isa-debugcon");
2621 qemu_opt_set(opts
, "chardev", "debugcon");
2625 static MachineClass
*machine_parse(const char *name
)
2627 MachineClass
*mc
= NULL
;
2628 GSList
*el
, *machines
= object_class_get_list(TYPE_MACHINE
, false);
2631 mc
= find_machine(name
);
2636 if (name
&& !is_help_option(name
)) {
2637 error_report("Unsupported machine type");
2638 error_printf("Use -machine help to list supported machines!\n");
2640 printf("Supported machines are:\n");
2641 for (el
= machines
; el
; el
= el
->next
) {
2642 MachineClass
*mc
= el
->data
;
2644 printf("%-20s %s (alias of %s)\n", mc
->alias
, mc
->desc
, mc
->name
);
2646 printf("%-20s %s%s\n", mc
->name
, mc
->desc
,
2647 mc
->is_default
? " (default)" : "");
2651 g_slist_free(machines
);
2652 exit(!name
|| !is_help_option(name
));
2655 static int tcg_init(MachineClass
*mc
)
2657 tcg_exec_init(tcg_tb_size
* 1024 * 1024);
2662 const char *opt_name
;
2664 int (*available
)(void);
2665 int (*init
)(MachineClass
*mc
);
2668 { "tcg", "tcg", tcg_available
, tcg_init
, &tcg_allowed
},
2669 { "xen", "Xen", xen_available
, xen_init
, &xen_allowed
},
2670 { "kvm", "KVM", kvm_available
, kvm_init
, &kvm_allowed
},
2671 { "qtest", "QTest", qtest_available
, qtest_init_accel
, &qtest_allowed
},
2674 static int configure_accelerator(MachineClass
*mc
)
2679 bool accel_initialised
= false;
2680 bool init_failed
= false;
2682 p
= qemu_opt_get(qemu_get_machine_opts(), "accel");
2684 /* Use the default "accelerator", tcg */
2688 while (!accel_initialised
&& *p
!= '\0') {
2692 p
= get_opt_name(buf
, sizeof (buf
), p
, ':');
2693 for (i
= 0; i
< ARRAY_SIZE(accel_list
); i
++) {
2694 if (strcmp(accel_list
[i
].opt_name
, buf
) == 0) {
2695 if (!accel_list
[i
].available()) {
2696 printf("%s not supported for this target\n",
2697 accel_list
[i
].name
);
2700 *(accel_list
[i
].allowed
) = true;
2701 ret
= accel_list
[i
].init(mc
);
2704 fprintf(stderr
, "failed to initialize %s: %s\n",
2707 *(accel_list
[i
].allowed
) = false;
2709 accel_initialised
= true;
2714 if (i
== ARRAY_SIZE(accel_list
)) {
2715 fprintf(stderr
, "\"%s\" accelerator does not exist.\n", buf
);
2719 if (!accel_initialised
) {
2721 fprintf(stderr
, "No accelerator found!\n");
2727 fprintf(stderr
, "Back to %s accelerator.\n", accel_list
[i
].name
);
2730 return !accel_initialised
;
2733 void qemu_add_exit_notifier(Notifier
*notify
)
2735 notifier_list_add(&exit_notifiers
, notify
);
2738 void qemu_remove_exit_notifier(Notifier
*notify
)
2740 notifier_remove(notify
);
2743 static void qemu_run_exit_notifiers(void)
2745 notifier_list_notify(&exit_notifiers
, NULL
);
2748 void qemu_add_machine_init_done_notifier(Notifier
*notify
)
2750 notifier_list_add(&machine_init_done_notifiers
, notify
);
2753 static void qemu_run_machine_init_done_notifiers(void)
2755 notifier_list_notify(&machine_init_done_notifiers
, NULL
);
2758 static const QEMUOption
*lookup_opt(int argc
, char **argv
,
2759 const char **poptarg
, int *poptind
)
2761 const QEMUOption
*popt
;
2762 int optind
= *poptind
;
2763 char *r
= argv
[optind
];
2766 loc_set_cmdline(argv
, optind
, 1);
2768 /* Treat --foo the same as -foo. */
2771 popt
= qemu_options
;
2774 error_report("invalid option");
2777 if (!strcmp(popt
->name
, r
+ 1))
2781 if (popt
->flags
& HAS_ARG
) {
2782 if (optind
>= argc
) {
2783 error_report("requires an argument");
2786 optarg
= argv
[optind
++];
2787 loc_set_cmdline(argv
, optind
- 2, 2);
2798 static gpointer
malloc_and_trace(gsize n_bytes
)
2800 void *ptr
= malloc(n_bytes
);
2801 trace_g_malloc(n_bytes
, ptr
);
2805 static gpointer
realloc_and_trace(gpointer mem
, gsize n_bytes
)
2807 void *ptr
= realloc(mem
, n_bytes
);
2808 trace_g_realloc(mem
, n_bytes
, ptr
);
2812 static void free_and_trace(gpointer mem
)
2818 static int object_set_property(const char *name
, const char *value
, void *opaque
)
2820 Object
*obj
= OBJECT(opaque
);
2821 StringInputVisitor
*siv
;
2822 Error
*local_err
= NULL
;
2824 if (strcmp(name
, "qom-type") == 0 || strcmp(name
, "id") == 0 ||
2825 strcmp(name
, "type") == 0) {
2829 siv
= string_input_visitor_new(value
);
2830 object_property_set(obj
, string_input_get_visitor(siv
), name
, &local_err
);
2831 string_input_visitor_cleanup(siv
);
2834 qerror_report_err(local_err
);
2835 error_free(local_err
);
2842 static int object_create(QemuOpts
*opts
, void *opaque
)
2851 ov
= opts_visitor_new(opts
);
2852 pdict
= qemu_opts_to_qdict(opts
, NULL
);
2854 visit_start_struct(opts_get_visitor(ov
), &dummy
, NULL
, NULL
, 0, &err
);
2859 qdict_del(pdict
, "qom-type");
2860 visit_type_str(opts_get_visitor(ov
), &type
, "qom-type", &err
);
2865 qdict_del(pdict
, "id");
2866 visit_type_str(opts_get_visitor(ov
), &id
, "id", &err
);
2871 object_add(type
, id
, pdict
, opts_get_visitor(ov
), &err
);
2875 visit_end_struct(opts_get_visitor(ov
), &err
);
2877 qmp_object_del(id
, NULL
);
2881 opts_visitor_cleanup(ov
);
2888 qerror_report_err(err
);
2894 int main(int argc
, char **argv
, char **envp
)
2897 int snapshot
, linux_boot
;
2898 const char *icount_option
= NULL
;
2899 const char *initrd_filename
;
2900 const char *kernel_filename
, *kernel_cmdline
;
2901 const char *boot_order
;
2903 int cyls
, heads
, secs
, translation
;
2904 QemuOpts
*hda_opts
= NULL
, *opts
, *machine_opts
;
2905 QemuOptsList
*olist
;
2908 const char *loadvm
= NULL
;
2909 MachineClass
*machine_class
;
2910 const char *cpu_model
;
2911 const char *vga_model
= NULL
;
2912 const char *qtest_chrdev
= NULL
;
2913 const char *qtest_log
= NULL
;
2914 const char *pid_file
= NULL
;
2915 const char *incoming
= NULL
;
2917 int show_vnc_port
= 0;
2919 bool defconfig
= true;
2920 bool userconfig
= true;
2921 const char *log_mask
= NULL
;
2922 const char *log_file
= NULL
;
2923 GMemVTable mem_trace
= {
2924 .malloc
= malloc_and_trace
,
2925 .realloc
= realloc_and_trace
,
2926 .free
= free_and_trace
,
2928 const char *trace_events
= NULL
;
2929 const char *trace_file
= NULL
;
2930 const ram_addr_t default_ram_size
= (ram_addr_t
)DEFAULT_RAM_SIZE
*
2932 ram_addr_t maxram_size
= default_ram_size
;
2933 uint64_t ram_slots
= 0;
2934 FILE *vmstate_dump_file
= NULL
;
2936 atexit(qemu_run_exit_notifiers
);
2937 error_set_progname(argv
[0]);
2938 qemu_init_exec_dir(argv
[0]);
2940 g_mem_set_vtable(&mem_trace
);
2942 module_call_init(MODULE_INIT_QOM
);
2944 qemu_add_opts(&qemu_drive_opts
);
2945 qemu_add_drive_opts(&qemu_legacy_drive_opts
);
2946 qemu_add_drive_opts(&qemu_common_drive_opts
);
2947 qemu_add_drive_opts(&qemu_drive_opts
);
2948 qemu_add_opts(&qemu_chardev_opts
);
2949 qemu_add_opts(&qemu_device_opts
);
2950 qemu_add_opts(&qemu_netdev_opts
);
2951 qemu_add_opts(&qemu_net_opts
);
2952 qemu_add_opts(&qemu_rtc_opts
);
2953 qemu_add_opts(&qemu_global_opts
);
2954 qemu_add_opts(&qemu_mon_opts
);
2955 qemu_add_opts(&qemu_trace_opts
);
2956 qemu_add_opts(&qemu_option_rom_opts
);
2957 qemu_add_opts(&qemu_machine_opts
);
2958 qemu_add_opts(&qemu_mem_opts
);
2959 qemu_add_opts(&qemu_smp_opts
);
2960 qemu_add_opts(&qemu_boot_opts
);
2961 qemu_add_opts(&qemu_sandbox_opts
);
2962 qemu_add_opts(&qemu_add_fd_opts
);
2963 qemu_add_opts(&qemu_object_opts
);
2964 qemu_add_opts(&qemu_tpmdev_opts
);
2965 qemu_add_opts(&qemu_realtime_opts
);
2966 qemu_add_opts(&qemu_msg_opts
);
2967 qemu_add_opts(&qemu_name_opts
);
2968 qemu_add_opts(&qemu_numa_opts
);
2972 rtc_clock
= QEMU_CLOCK_HOST
;
2974 QLIST_INIT (&vm_change_state_head
);
2975 os_setup_early_signal_handling();
2977 module_call_init(MODULE_INIT_MACHINE
);
2978 machine_class
= find_default_machine();
2980 ram_size
= default_ram_size
;
2982 cyls
= heads
= secs
= 0;
2983 translation
= BIOS_ATA_TRANSLATION_AUTO
;
2985 for (i
= 0; i
< MAX_NODES
; i
++) {
2986 numa_info
[i
].node_mem
= 0;
2987 bitmap_zero(numa_info
[i
].node_cpu
, MAX_CPUMASK_BITS
);
2993 bdrv_init_with_whitelist();
2997 /* first pass of option parsing */
2999 while (optind
< argc
) {
3000 if (argv
[optind
][0] != '-') {
3004 const QEMUOption
*popt
;
3006 popt
= lookup_opt(argc
, argv
, &optarg
, &optind
);
3007 switch (popt
->index
) {
3008 case QEMU_OPTION_nodefconfig
:
3011 case QEMU_OPTION_nouserconfig
:
3020 ret
= qemu_read_default_config_files(userconfig
);
3026 /* second pass of option parsing */
3031 if (argv
[optind
][0] != '-') {
3032 hda_opts
= drive_add(IF_DEFAULT
, 0, argv
[optind
++], HD_OPTS
);
3034 const QEMUOption
*popt
;
3036 popt
= lookup_opt(argc
, argv
, &optarg
, &optind
);
3037 if (!(popt
->arch_mask
& arch_type
)) {
3038 printf("Option %s not supported for this target\n", popt
->name
);
3041 switch(popt
->index
) {
3043 machine_class
= machine_parse(optarg
);
3045 case QEMU_OPTION_no_kvm_irqchip
: {
3046 olist
= qemu_find_opts("machine");
3047 qemu_opts_parse(olist
, "kernel_irqchip=off", 0);
3050 case QEMU_OPTION_cpu
:
3051 /* hw initialization will check this */
3054 case QEMU_OPTION_hda
:
3058 snprintf(buf
, sizeof(buf
), "%s", HD_OPTS
);
3060 snprintf(buf
, sizeof(buf
),
3061 "%s,cyls=%d,heads=%d,secs=%d%s",
3062 HD_OPTS
, cyls
, heads
, secs
,
3063 translation
== BIOS_ATA_TRANSLATION_LBA
?
3065 translation
== BIOS_ATA_TRANSLATION_NONE
?
3066 ",trans=none" : "");
3067 drive_add(IF_DEFAULT
, 0, optarg
, buf
);
3070 case QEMU_OPTION_hdb
:
3071 case QEMU_OPTION_hdc
:
3072 case QEMU_OPTION_hdd
:
3073 drive_add(IF_DEFAULT
, popt
->index
- QEMU_OPTION_hda
, optarg
,
3076 case QEMU_OPTION_drive
:
3077 if (drive_def(optarg
) == NULL
) {
3081 case QEMU_OPTION_set
:
3082 if (qemu_set_option(optarg
) != 0)
3085 case QEMU_OPTION_global
:
3086 if (qemu_global_option(optarg
) != 0)
3089 case QEMU_OPTION_mtdblock
:
3090 drive_add(IF_MTD
, -1, optarg
, MTD_OPTS
);
3092 case QEMU_OPTION_sd
:
3093 drive_add(IF_SD
, -1, optarg
, SD_OPTS
);
3095 case QEMU_OPTION_pflash
:
3096 drive_add(IF_PFLASH
, -1, optarg
, PFLASH_OPTS
);
3098 case QEMU_OPTION_snapshot
:
3101 case QEMU_OPTION_hdachs
:
3105 cyls
= strtol(p
, (char **)&p
, 0);
3106 if (cyls
< 1 || cyls
> 16383)
3111 heads
= strtol(p
, (char **)&p
, 0);
3112 if (heads
< 1 || heads
> 16)
3117 secs
= strtol(p
, (char **)&p
, 0);
3118 if (secs
< 1 || secs
> 63)
3122 if (!strcmp(p
, "large")) {
3123 translation
= BIOS_ATA_TRANSLATION_LARGE
;
3124 } else if (!strcmp(p
, "rechs")) {
3125 translation
= BIOS_ATA_TRANSLATION_RECHS
;
3126 } else if (!strcmp(p
, "none")) {
3127 translation
= BIOS_ATA_TRANSLATION_NONE
;
3128 } else if (!strcmp(p
, "lba")) {
3129 translation
= BIOS_ATA_TRANSLATION_LBA
;
3130 } else if (!strcmp(p
, "auto")) {
3131 translation
= BIOS_ATA_TRANSLATION_AUTO
;
3135 } else if (*p
!= '\0') {
3137 fprintf(stderr
, "qemu: invalid physical CHS format\n");
3140 if (hda_opts
!= NULL
) {
3142 snprintf(num
, sizeof(num
), "%d", cyls
);
3143 qemu_opt_set(hda_opts
, "cyls", num
);
3144 snprintf(num
, sizeof(num
), "%d", heads
);
3145 qemu_opt_set(hda_opts
, "heads", num
);
3146 snprintf(num
, sizeof(num
), "%d", secs
);
3147 qemu_opt_set(hda_opts
, "secs", num
);
3148 if (translation
== BIOS_ATA_TRANSLATION_LARGE
) {
3149 qemu_opt_set(hda_opts
, "trans", "large");
3150 } else if (translation
== BIOS_ATA_TRANSLATION_RECHS
) {
3151 qemu_opt_set(hda_opts
, "trans", "rechs");
3152 } else if (translation
== BIOS_ATA_TRANSLATION_LBA
) {
3153 qemu_opt_set(hda_opts
, "trans", "lba");
3154 } else if (translation
== BIOS_ATA_TRANSLATION_NONE
) {
3155 qemu_opt_set(hda_opts
, "trans", "none");
3160 case QEMU_OPTION_numa
:
3161 opts
= qemu_opts_parse(qemu_find_opts("numa"), optarg
, 1);
3166 case QEMU_OPTION_display
:
3167 display_type
= select_display(optarg
);
3169 case QEMU_OPTION_nographic
:
3170 display_type
= DT_NOGRAPHIC
;
3172 case QEMU_OPTION_curses
:
3173 #ifdef CONFIG_CURSES
3174 display_type
= DT_CURSES
;
3176 fprintf(stderr
, "Curses support is disabled\n");
3180 case QEMU_OPTION_portrait
:
3181 graphic_rotate
= 90;
3183 case QEMU_OPTION_rotate
:
3184 graphic_rotate
= strtol(optarg
, (char **) &optarg
, 10);
3185 if (graphic_rotate
!= 0 && graphic_rotate
!= 90 &&
3186 graphic_rotate
!= 180 && graphic_rotate
!= 270) {
3188 "qemu: only 90, 180, 270 deg rotation is available\n");
3192 case QEMU_OPTION_kernel
:
3193 qemu_opts_set(qemu_find_opts("machine"), 0, "kernel", optarg
);
3195 case QEMU_OPTION_initrd
:
3196 qemu_opts_set(qemu_find_opts("machine"), 0, "initrd", optarg
);
3198 case QEMU_OPTION_append
:
3199 qemu_opts_set(qemu_find_opts("machine"), 0, "append", optarg
);
3201 case QEMU_OPTION_dtb
:
3202 qemu_opts_set(qemu_find_opts("machine"), 0, "dtb", optarg
);
3204 case QEMU_OPTION_cdrom
:
3205 drive_add(IF_DEFAULT
, 2, optarg
, CDROM_OPTS
);
3207 case QEMU_OPTION_boot
:
3208 opts
= qemu_opts_parse(qemu_find_opts("boot-opts"), optarg
, 1);
3213 case QEMU_OPTION_fda
:
3214 case QEMU_OPTION_fdb
:
3215 drive_add(IF_FLOPPY
, popt
->index
- QEMU_OPTION_fda
,
3218 case QEMU_OPTION_no_fd_bootchk
:
3221 case QEMU_OPTION_netdev
:
3222 if (net_client_parse(qemu_find_opts("netdev"), optarg
) == -1) {
3226 case QEMU_OPTION_net
:
3227 if (net_client_parse(qemu_find_opts("net"), optarg
) == -1) {
3231 #ifdef CONFIG_LIBISCSI
3232 case QEMU_OPTION_iscsi
:
3233 opts
= qemu_opts_parse(qemu_find_opts("iscsi"), optarg
, 0);
3240 case QEMU_OPTION_tftp
:
3241 legacy_tftp_prefix
= optarg
;
3243 case QEMU_OPTION_bootp
:
3244 legacy_bootp_filename
= optarg
;
3246 case QEMU_OPTION_redir
:
3247 if (net_slirp_redir(optarg
) < 0)
3251 case QEMU_OPTION_bt
:
3252 add_device_config(DEV_BT
, optarg
);
3254 case QEMU_OPTION_audio_help
:
3258 case QEMU_OPTION_soundhw
:
3259 select_soundhw (optarg
);
3264 case QEMU_OPTION_version
:
3268 case QEMU_OPTION_m
: {
3270 const char *mem_str
;
3271 const char *maxmem_str
, *slots_str
;
3273 opts
= qemu_opts_parse(qemu_find_opts("memory"),
3279 mem_str
= qemu_opt_get(opts
, "size");
3281 error_report("invalid -m option, missing 'size' option");
3285 error_report("missing 'size' option value");
3289 sz
= qemu_opt_get_size(opts
, "size", ram_size
);
3291 /* Fix up legacy suffix-less format */
3292 if (g_ascii_isdigit(mem_str
[strlen(mem_str
) - 1])) {
3293 uint64_t overflow_check
= sz
;
3296 if ((sz
>> 20) != overflow_check
) {
3297 error_report("too large 'size' option value");
3302 /* backward compatibility behaviour for case "-m 0" */
3304 sz
= default_ram_size
;
3307 sz
= QEMU_ALIGN_UP(sz
, 8192);
3309 if (ram_size
!= sz
) {
3310 error_report("ram size too large");
3314 maxmem_str
= qemu_opt_get(opts
, "maxmem");
3315 slots_str
= qemu_opt_get(opts
, "slots");
3316 if (maxmem_str
&& slots_str
) {
3319 sz
= qemu_opt_get_size(opts
, "maxmem", 0);
3320 if (sz
< ram_size
) {
3321 fprintf(stderr
, "qemu: invalid -m option value: maxmem "
3322 "(%" PRIu64
") <= initial memory ("
3323 RAM_ADDR_FMT
")\n", sz
, ram_size
);
3327 slots
= qemu_opt_get_number(opts
, "slots", 0);
3328 if ((sz
> ram_size
) && !slots
) {
3329 fprintf(stderr
, "qemu: invalid -m option value: maxmem "
3330 "(%" PRIu64
") more than initial memory ("
3331 RAM_ADDR_FMT
") but no hotplug slots where "
3332 "specified\n", sz
, ram_size
);
3336 if ((sz
<= ram_size
) && slots
) {
3337 fprintf(stderr
, "qemu: invalid -m option value: %"
3338 PRIu64
" hotplug slots where specified but "
3339 "maxmem (%" PRIu64
") <= initial memory ("
3340 RAM_ADDR_FMT
")\n", slots
, sz
, ram_size
);
3345 } else if ((!maxmem_str
&& slots_str
) ||
3346 (maxmem_str
&& !slots_str
)) {
3347 fprintf(stderr
, "qemu: invalid -m option value: missing "
3348 "'%s' option\n", slots_str
? "maxmem" : "slots");
3354 case QEMU_OPTION_tpmdev
:
3355 if (tpm_config_parse(qemu_find_opts("tpmdev"), optarg
) < 0) {
3360 case QEMU_OPTION_mempath
:
3363 case QEMU_OPTION_mem_prealloc
:
3373 add_device_config(DEV_GDB
, "tcp::" DEFAULT_GDBSTUB_PORT
);
3375 case QEMU_OPTION_gdb
:
3376 add_device_config(DEV_GDB
, optarg
);
3379 if (data_dir_idx
< ARRAY_SIZE(data_dir
)) {
3380 data_dir
[data_dir_idx
++] = optarg
;
3383 case QEMU_OPTION_bios
:
3384 qemu_opts_set(qemu_find_opts("machine"), 0, "firmware", optarg
);
3386 case QEMU_OPTION_singlestep
:
3393 keyboard_layout
= optarg
;
3395 case QEMU_OPTION_localtime
:
3398 case QEMU_OPTION_vga
:
3407 w
= strtol(p
, (char **)&p
, 10);
3410 fprintf(stderr
, "qemu: invalid resolution or depth\n");
3416 h
= strtol(p
, (char **)&p
, 10);
3421 depth
= strtol(p
, (char **)&p
, 10);
3422 if (depth
!= 8 && depth
!= 15 && depth
!= 16 &&
3423 depth
!= 24 && depth
!= 32)
3425 } else if (*p
== '\0') {
3426 depth
= graphic_depth
;
3433 graphic_depth
= depth
;
3436 case QEMU_OPTION_echr
:
3439 term_escape_char
= strtol(optarg
, &r
, 0);
3441 printf("Bad argument to echr\n");
3444 case QEMU_OPTION_monitor
:
3445 default_monitor
= 0;
3446 if (strncmp(optarg
, "none", 4)) {
3447 monitor_parse(optarg
, "readline");
3450 case QEMU_OPTION_qmp
:
3451 monitor_parse(optarg
, "control");
3452 default_monitor
= 0;
3454 case QEMU_OPTION_mon
:
3455 opts
= qemu_opts_parse(qemu_find_opts("mon"), optarg
, 1);
3459 default_monitor
= 0;
3461 case QEMU_OPTION_chardev
:
3462 opts
= qemu_opts_parse(qemu_find_opts("chardev"), optarg
, 1);
3467 case QEMU_OPTION_fsdev
:
3468 olist
= qemu_find_opts("fsdev");
3470 fprintf(stderr
, "fsdev is not supported by this qemu build.\n");
3473 opts
= qemu_opts_parse(olist
, optarg
, 1);
3478 case QEMU_OPTION_virtfs
: {
3481 const char *writeout
, *sock_fd
, *socket
;
3483 olist
= qemu_find_opts("virtfs");
3485 fprintf(stderr
, "virtfs is not supported by this qemu build.\n");
3488 opts
= qemu_opts_parse(olist
, optarg
, 1);
3493 if (qemu_opt_get(opts
, "fsdriver") == NULL
||
3494 qemu_opt_get(opts
, "mount_tag") == NULL
) {
3495 fprintf(stderr
, "Usage: -virtfs fsdriver,mount_tag=tag.\n");
3498 fsdev
= qemu_opts_create(qemu_find_opts("fsdev"),
3499 qemu_opt_get(opts
, "mount_tag"),
3502 fprintf(stderr
, "duplicate fsdev id: %s\n",
3503 qemu_opt_get(opts
, "mount_tag"));
3507 writeout
= qemu_opt_get(opts
, "writeout");
3509 #ifdef CONFIG_SYNC_FILE_RANGE
3510 qemu_opt_set(fsdev
, "writeout", writeout
);
3512 fprintf(stderr
, "writeout=immediate not supported on "
3517 qemu_opt_set(fsdev
, "fsdriver", qemu_opt_get(opts
, "fsdriver"));
3518 qemu_opt_set(fsdev
, "path", qemu_opt_get(opts
, "path"));
3519 qemu_opt_set(fsdev
, "security_model",
3520 qemu_opt_get(opts
, "security_model"));
3521 socket
= qemu_opt_get(opts
, "socket");
3523 qemu_opt_set(fsdev
, "socket", socket
);
3525 sock_fd
= qemu_opt_get(opts
, "sock_fd");
3527 qemu_opt_set(fsdev
, "sock_fd", sock_fd
);
3530 qemu_opt_set_bool(fsdev
, "readonly",
3531 qemu_opt_get_bool(opts
, "readonly", 0));
3532 device
= qemu_opts_create(qemu_find_opts("device"), NULL
, 0,
3534 qemu_opt_set(device
, "driver", "virtio-9p-pci");
3535 qemu_opt_set(device
, "fsdev",
3536 qemu_opt_get(opts
, "mount_tag"));
3537 qemu_opt_set(device
, "mount_tag",
3538 qemu_opt_get(opts
, "mount_tag"));
3541 case QEMU_OPTION_virtfs_synth
: {
3545 fsdev
= qemu_opts_create(qemu_find_opts("fsdev"), "v_synth",
3548 fprintf(stderr
, "duplicate option: %s\n", "virtfs_synth");
3551 qemu_opt_set(fsdev
, "fsdriver", "synth");
3553 device
= qemu_opts_create(qemu_find_opts("device"), NULL
, 0,
3555 qemu_opt_set(device
, "driver", "virtio-9p-pci");
3556 qemu_opt_set(device
, "fsdev", "v_synth");
3557 qemu_opt_set(device
, "mount_tag", "v_synth");
3560 case QEMU_OPTION_serial
:
3561 add_device_config(DEV_SERIAL
, optarg
);
3563 if (strncmp(optarg
, "mon:", 4) == 0) {
3564 default_monitor
= 0;
3567 case QEMU_OPTION_watchdog
:
3570 "qemu: only one watchdog option may be given\n");
3575 case QEMU_OPTION_watchdog_action
:
3576 if (select_watchdog_action(optarg
) == -1) {
3577 fprintf(stderr
, "Unknown -watchdog-action parameter\n");
3581 case QEMU_OPTION_virtiocon
:
3582 add_device_config(DEV_VIRTCON
, optarg
);
3583 default_virtcon
= 0;
3584 if (strncmp(optarg
, "mon:", 4) == 0) {
3585 default_monitor
= 0;
3588 case QEMU_OPTION_parallel
:
3589 add_device_config(DEV_PARALLEL
, optarg
);
3590 default_parallel
= 0;
3591 if (strncmp(optarg
, "mon:", 4) == 0) {
3592 default_monitor
= 0;
3595 case QEMU_OPTION_debugcon
:
3596 add_device_config(DEV_DEBUGCON
, optarg
);
3598 case QEMU_OPTION_loadvm
:
3601 case QEMU_OPTION_full_screen
:
3604 case QEMU_OPTION_no_frame
:
3607 case QEMU_OPTION_alt_grab
:
3610 case QEMU_OPTION_ctrl_grab
:
3613 case QEMU_OPTION_no_quit
:
3616 case QEMU_OPTION_sdl
:
3618 display_type
= DT_SDL
;
3621 fprintf(stderr
, "SDL support is disabled\n");
3624 case QEMU_OPTION_pidfile
:
3627 case QEMU_OPTION_win2k_hack
:
3628 win2k_install_hack
= 1;
3630 case QEMU_OPTION_rtc_td_hack
: {
3631 static GlobalProperty slew_lost_ticks
[] = {
3633 .driver
= "mc146818rtc",
3634 .property
= "lost_tick_policy",
3637 { /* end of list */ }
3640 qdev_prop_register_global_list(slew_lost_ticks
);
3643 case QEMU_OPTION_acpitable
:
3644 opts
= qemu_opts_parse(qemu_find_opts("acpi"), optarg
, 1);
3648 do_acpitable_option(opts
);
3650 case QEMU_OPTION_smbios
:
3651 opts
= qemu_opts_parse(qemu_find_opts("smbios"), optarg
, 0);
3655 do_smbios_option(opts
);
3657 case QEMU_OPTION_enable_kvm
:
3658 olist
= qemu_find_opts("machine");
3659 qemu_opts_parse(olist
, "accel=kvm", 0);
3661 case QEMU_OPTION_machine
:
3662 olist
= qemu_find_opts("machine");
3663 opts
= qemu_opts_parse(olist
, optarg
, 1);
3667 optarg
= qemu_opt_get(opts
, "type");
3669 machine_class
= machine_parse(optarg
);
3672 case QEMU_OPTION_no_kvm
:
3673 olist
= qemu_find_opts("machine");
3674 qemu_opts_parse(olist
, "accel=tcg", 0);
3676 case QEMU_OPTION_no_kvm_pit
: {
3677 fprintf(stderr
, "Warning: KVM PIT can no longer be disabled "
3681 case QEMU_OPTION_no_kvm_pit_reinjection
: {
3682 static GlobalProperty kvm_pit_lost_tick_policy
[] = {
3684 .driver
= "kvm-pit",
3685 .property
= "lost_tick_policy",
3688 { /* end of list */ }
3691 fprintf(stderr
, "Warning: option deprecated, use "
3692 "lost_tick_policy property of kvm-pit instead.\n");
3693 qdev_prop_register_global_list(kvm_pit_lost_tick_policy
);
3696 case QEMU_OPTION_usb
:
3697 olist
= qemu_find_opts("machine");
3698 qemu_opts_parse(olist
, "usb=on", 0);
3700 case QEMU_OPTION_usbdevice
:
3701 olist
= qemu_find_opts("machine");
3702 qemu_opts_parse(olist
, "usb=on", 0);
3703 add_device_config(DEV_USB
, optarg
);
3705 case QEMU_OPTION_device
:
3706 if (!qemu_opts_parse(qemu_find_opts("device"), optarg
, 1)) {
3710 case QEMU_OPTION_smp
:
3711 if (!qemu_opts_parse(qemu_find_opts("smp-opts"), optarg
, 1)) {
3715 case QEMU_OPTION_vnc
:
3718 vnc_display
= optarg
;
3720 fprintf(stderr
, "VNC support is disabled\n");
3724 case QEMU_OPTION_no_acpi
:
3727 case QEMU_OPTION_no_hpet
:
3730 case QEMU_OPTION_balloon
:
3731 if (balloon_parse(optarg
) < 0) {
3732 fprintf(stderr
, "Unknown -balloon argument %s\n", optarg
);
3736 case QEMU_OPTION_no_reboot
:
3739 case QEMU_OPTION_no_shutdown
:
3742 case QEMU_OPTION_show_cursor
:
3745 case QEMU_OPTION_uuid
:
3746 if(qemu_uuid_parse(optarg
, qemu_uuid
) < 0) {
3747 fprintf(stderr
, "Fail to parse UUID string."
3748 " Wrong format.\n");
3751 qemu_uuid_set
= true;
3753 case QEMU_OPTION_option_rom
:
3754 if (nb_option_roms
>= MAX_OPTION_ROMS
) {
3755 fprintf(stderr
, "Too many option ROMs\n");
3758 opts
= qemu_opts_parse(qemu_find_opts("option-rom"), optarg
, 1);
3762 option_rom
[nb_option_roms
].name
= qemu_opt_get(opts
, "romfile");
3763 option_rom
[nb_option_roms
].bootindex
=
3764 qemu_opt_get_number(opts
, "bootindex", -1);
3765 if (!option_rom
[nb_option_roms
].name
) {
3766 fprintf(stderr
, "Option ROM file is not specified\n");
3771 case QEMU_OPTION_semihosting
:
3772 semihosting_enabled
= 1;
3774 case QEMU_OPTION_tdf
:
3775 fprintf(stderr
, "Warning: user space PIT time drift fix "
3776 "is no longer supported.\n");
3778 case QEMU_OPTION_name
:
3779 opts
= qemu_opts_parse(qemu_find_opts("name"), optarg
, 1);
3784 case QEMU_OPTION_prom_env
:
3785 if (nb_prom_envs
>= MAX_PROM_ENVS
) {
3786 fprintf(stderr
, "Too many prom variables\n");
3789 prom_envs
[nb_prom_envs
] = optarg
;
3792 case QEMU_OPTION_old_param
:
3795 case QEMU_OPTION_clock
:
3796 /* Clock options no longer exist. Keep this option for
3797 * backward compatibility.
3800 case QEMU_OPTION_startdate
:
3801 configure_rtc_date_offset(optarg
, 1);
3803 case QEMU_OPTION_rtc
:
3804 opts
= qemu_opts_parse(qemu_find_opts("rtc"), optarg
, 0);
3808 configure_rtc(opts
);
3810 case QEMU_OPTION_tb_size
:
3811 tcg_tb_size
= strtol(optarg
, NULL
, 0);
3812 if (tcg_tb_size
< 0) {
3816 case QEMU_OPTION_icount
:
3817 icount_option
= optarg
;
3819 case QEMU_OPTION_incoming
:
3821 runstate_set(RUN_STATE_INMIGRATE
);
3823 case QEMU_OPTION_nodefaults
:
3826 case QEMU_OPTION_xen_domid
:
3827 if (!(xen_available())) {
3828 printf("Option %s not supported for this target\n", popt
->name
);
3831 xen_domid
= atoi(optarg
);
3833 case QEMU_OPTION_xen_create
:
3834 if (!(xen_available())) {
3835 printf("Option %s not supported for this target\n", popt
->name
);
3838 xen_mode
= XEN_CREATE
;
3840 case QEMU_OPTION_xen_attach
:
3841 if (!(xen_available())) {
3842 printf("Option %s not supported for this target\n", popt
->name
);
3845 xen_mode
= XEN_ATTACH
;
3847 case QEMU_OPTION_trace
:
3849 opts
= qemu_opts_parse(qemu_find_opts("trace"), optarg
, 0);
3853 trace_events
= qemu_opt_get(opts
, "events");
3854 trace_file
= qemu_opt_get(opts
, "file");
3857 case QEMU_OPTION_readconfig
:
3859 int ret
= qemu_read_config_file(optarg
);
3861 fprintf(stderr
, "read config %s: %s\n", optarg
,
3867 case QEMU_OPTION_spice
:
3868 olist
= qemu_find_opts("spice");
3870 fprintf(stderr
, "spice is not supported by this qemu build.\n");
3873 opts
= qemu_opts_parse(olist
, optarg
, 0);
3879 case QEMU_OPTION_writeconfig
:
3882 if (strcmp(optarg
, "-") == 0) {
3885 fp
= fopen(optarg
, "w");
3887 fprintf(stderr
, "open %s: %s\n", optarg
, strerror(errno
));
3891 qemu_config_write(fp
);
3897 case QEMU_OPTION_qtest
:
3898 qtest_chrdev
= optarg
;
3900 case QEMU_OPTION_qtest_log
:
3903 case QEMU_OPTION_sandbox
:
3904 opts
= qemu_opts_parse(qemu_find_opts("sandbox"), optarg
, 1);
3909 case QEMU_OPTION_add_fd
:
3911 opts
= qemu_opts_parse(qemu_find_opts("add-fd"), optarg
, 0);
3916 error_report("File descriptor passing is disabled on this "
3921 case QEMU_OPTION_object
:
3922 opts
= qemu_opts_parse(qemu_find_opts("object"), optarg
, 1);
3927 case QEMU_OPTION_realtime
:
3928 opts
= qemu_opts_parse(qemu_find_opts("realtime"), optarg
, 0);
3932 configure_realtime(opts
);
3934 case QEMU_OPTION_msg
:
3935 opts
= qemu_opts_parse(qemu_find_opts("msg"), optarg
, 0);
3939 configure_msg(opts
);
3941 case QEMU_OPTION_dump_vmstate
:
3942 vmstate_dump_file
= fopen(optarg
, "w");
3943 if (vmstate_dump_file
== NULL
) {
3944 fprintf(stderr
, "open %s: %s\n", optarg
, strerror(errno
));
3949 os_parse_cmd_args(popt
->index
, optarg
);
3957 if (qemu_init_main_loop()) {
3958 fprintf(stderr
, "qemu_init_main_loop failed\n");
3962 if (qemu_opts_foreach(qemu_find_opts("sandbox"), parse_sandbox
, NULL
, 0)) {
3966 if (qemu_opts_foreach(qemu_find_opts("name"), parse_name
, NULL
, 1)) {
3971 if (qemu_opts_foreach(qemu_find_opts("add-fd"), parse_add_fd
, NULL
, 1)) {
3975 if (qemu_opts_foreach(qemu_find_opts("add-fd"), cleanup_add_fd
, NULL
, 1)) {
3980 if (machine_class
== NULL
) {
3981 fprintf(stderr
, "No machine specified, and there is no default.\n"
3982 "Use -machine help to list supported machines!\n");
3986 cpu_exec_init_all();
3988 current_machine
= MACHINE(object_new(object_class_get_name(
3989 OBJECT_CLASS(machine_class
))));
3990 object_property_add_child(object_get_root(), "machine",
3991 OBJECT(current_machine
), &error_abort
);
3993 if (machine_class
->hw_version
) {
3994 qemu_set_version(machine_class
->hw_version
);
3997 if (qemu_opts_foreach(qemu_find_opts("object"),
3998 object_create
, NULL
, 0) != 0) {
4002 /* Init CPU def lists, based on config
4003 * - Must be called after all the qemu_read_config_file() calls
4004 * - Must be called before list_cpus()
4005 * - Must be called before machine->init()
4009 if (cpu_model
&& is_help_option(cpu_model
)) {
4010 list_cpus(stdout
, &fprintf
, cpu_model
);
4014 /* Open the logfile at this point, if necessary. We can't open the logfile
4015 * when encountering either of the logging options (-d or -D) because the
4016 * other one may be encountered later on the command line, changing the
4017 * location or level of logging.
4022 qemu_set_log_filename(log_file
);
4025 mask
= qemu_str_to_log_mask(log_mask
);
4027 qemu_print_log_usage(stdout
);
4033 if (!is_daemonized()) {
4034 if (!trace_init_backends(trace_events
, trace_file
)) {
4039 /* If no data_dir is specified then try to find it relative to the
4041 if (data_dir_idx
< ARRAY_SIZE(data_dir
)) {
4042 data_dir
[data_dir_idx
] = os_find_datadir();
4043 if (data_dir
[data_dir_idx
] != NULL
) {
4047 /* If all else fails use the install path specified when building. */
4048 if (data_dir_idx
< ARRAY_SIZE(data_dir
)) {
4049 data_dir
[data_dir_idx
++] = CONFIG_QEMU_DATADIR
;
4052 smp_parse(qemu_opts_find(qemu_find_opts("smp-opts"), NULL
));
4054 machine_class
->max_cpus
= machine_class
->max_cpus
?: 1; /* Default to UP */
4055 if (smp_cpus
> machine_class
->max_cpus
) {
4056 fprintf(stderr
, "Number of SMP cpus requested (%d), exceeds max cpus "
4057 "supported by machine `%s' (%d)\n", smp_cpus
,
4058 machine_class
->name
, machine_class
->max_cpus
);
4063 * Get the default machine options from the machine if it is not already
4064 * specified either by the configuration file or by the command line.
4066 if (machine_class
->default_machine_opts
) {
4067 qemu_opts_set_defaults(qemu_find_opts("machine"),
4068 machine_class
->default_machine_opts
, 0);
4071 qemu_opts_foreach(qemu_find_opts("device"), default_driver_check
, NULL
, 0);
4072 qemu_opts_foreach(qemu_find_opts("global"), default_driver_check
, NULL
, 0);
4074 if (!vga_model
&& !default_vga
) {
4075 vga_interface_type
= VGA_DEVICE
;
4077 if (!has_defaults
|| machine_class
->no_serial
) {
4080 if (!has_defaults
|| machine_class
->no_parallel
) {
4081 default_parallel
= 0;
4083 if (!has_defaults
|| !machine_class
->use_virtcon
) {
4084 default_virtcon
= 0;
4086 if (!has_defaults
|| !machine_class
->use_sclp
) {
4089 if (!has_defaults
|| machine_class
->no_floppy
) {
4092 if (!has_defaults
|| machine_class
->no_cdrom
) {
4095 if (!has_defaults
|| machine_class
->no_sdcard
) {
4098 if (!has_defaults
) {
4099 default_monitor
= 0;
4104 if (is_daemonized()) {
4105 /* According to documentation and historically, -nographic redirects
4106 * serial port, parallel port and monitor to stdio, which does not work
4107 * with -daemonize. We can redirect these to null instead, but since
4108 * -nographic is legacy, let's just error out.
4109 * We disallow -nographic only if all other ports are not redirected
4110 * explicitly, to not break existing legacy setups which uses
4111 * -nographic _and_ redirects all ports explicitly - this is valid
4112 * usage, -nographic is just a no-op in this case.
4114 if (display_type
== DT_NOGRAPHIC
4115 && (default_parallel
|| default_serial
4116 || default_monitor
|| default_virtcon
)) {
4117 fprintf(stderr
, "-nographic can not be used with -daemonize\n");
4120 #ifdef CONFIG_CURSES
4121 if (display_type
== DT_CURSES
) {
4122 fprintf(stderr
, "curses display can not be used with -daemonize\n");
4128 if (display_type
== DT_NOGRAPHIC
) {
4129 if (default_parallel
)
4130 add_device_config(DEV_PARALLEL
, "null");
4131 if (default_serial
&& default_monitor
) {
4132 add_device_config(DEV_SERIAL
, "mon:stdio");
4133 } else if (default_virtcon
&& default_monitor
) {
4134 add_device_config(DEV_VIRTCON
, "mon:stdio");
4135 } else if (default_sclp
&& default_monitor
) {
4136 add_device_config(DEV_SCLP
, "mon:stdio");
4139 add_device_config(DEV_SERIAL
, "stdio");
4140 if (default_virtcon
)
4141 add_device_config(DEV_VIRTCON
, "stdio");
4143 add_device_config(DEV_SCLP
, "stdio");
4145 if (default_monitor
)
4146 monitor_parse("stdio", "readline");
4150 add_device_config(DEV_SERIAL
, "vc:80Cx24C");
4151 if (default_parallel
)
4152 add_device_config(DEV_PARALLEL
, "vc:80Cx24C");
4153 if (default_monitor
)
4154 monitor_parse("vc:80Cx24C", "readline");
4155 if (default_virtcon
)
4156 add_device_config(DEV_VIRTCON
, "vc:80Cx24C");
4158 add_device_config(DEV_SCLP
, "vc:80Cx24C");
4162 if (display_type
== DT_DEFAULT
&& !display_remote
) {
4163 #if defined(CONFIG_GTK)
4164 display_type
= DT_GTK
;
4165 #elif defined(CONFIG_SDL) || defined(CONFIG_COCOA)
4166 display_type
= DT_SDL
;
4167 #elif defined(CONFIG_VNC)
4168 vnc_display
= "localhost:0,to=99";
4171 display_type
= DT_NONE
;
4175 if ((no_frame
|| alt_grab
|| ctrl_grab
) && display_type
!= DT_SDL
) {
4176 fprintf(stderr
, "-no-frame, -alt-grab and -ctrl-grab are only valid "
4177 "for SDL, ignoring option\n");
4179 if (no_quit
&& (display_type
!= DT_GTK
&& display_type
!= DT_SDL
)) {
4180 fprintf(stderr
, "-no-quit is only valid for GTK and SDL, "
4181 "ignoring option\n");
4184 #if defined(CONFIG_GTK)
4185 if (display_type
== DT_GTK
) {
4186 early_gtk_display_init();
4192 if (qemu_opts_foreach(qemu_find_opts("chardev"), chardev_init_func
, NULL
, 1) != 0)
4194 #ifdef CONFIG_VIRTFS
4195 if (qemu_opts_foreach(qemu_find_opts("fsdev"), fsdev_init_func
, NULL
, 1) != 0) {
4200 if (pid_file
&& qemu_create_pidfile(pid_file
) != 0) {
4205 /* store value for the future use */
4206 qemu_opt_set_number(qemu_find_opts_singleton("memory"), "size", ram_size
);
4208 if (qemu_opts_foreach(qemu_find_opts("device"), device_help_func
, NULL
, 0)
4213 machine_opts
= qemu_get_machine_opts();
4214 if (qemu_opt_foreach(machine_opts
, object_set_property
, current_machine
,
4216 object_unref(OBJECT(current_machine
));
4220 configure_accelerator(machine_class
);
4223 Error
*local_err
= NULL
;
4224 qtest_init(qtest_chrdev
, qtest_log
, &local_err
);
4226 error_report("%s", error_get_pretty(local_err
));
4227 error_free(local_err
);
4232 machine_opts
= qemu_get_machine_opts();
4233 kernel_filename
= qemu_opt_get(machine_opts
, "kernel");
4234 initrd_filename
= qemu_opt_get(machine_opts
, "initrd");
4235 kernel_cmdline
= qemu_opt_get(machine_opts
, "append");
4236 bios_name
= qemu_opt_get(machine_opts
, "firmware");
4238 boot_order
= machine_class
->default_boot_order
;
4239 opts
= qemu_opts_find(qemu_find_opts("boot-opts"), NULL
);
4241 char *normal_boot_order
;
4242 const char *order
, *once
;
4244 order
= qemu_opt_get(opts
, "order");
4246 validate_bootdevices(order
);
4250 once
= qemu_opt_get(opts
, "once");
4252 validate_bootdevices(once
);
4253 normal_boot_order
= g_strdup(boot_order
);
4255 qemu_register_reset(restore_boot_order
, normal_boot_order
);
4258 boot_menu
= qemu_opt_get_bool(opts
, "menu", boot_menu
);
4259 boot_strict
= qemu_opt_get_bool(opts
, "strict", false);
4262 if (!kernel_cmdline
) {
4263 kernel_cmdline
= "";
4264 current_machine
->kernel_cmdline
= (char *)kernel_cmdline
;
4267 linux_boot
= (kernel_filename
!= NULL
);
4269 if (!linux_boot
&& *kernel_cmdline
!= '\0') {
4270 fprintf(stderr
, "-append only allowed with -kernel option\n");
4274 if (!linux_boot
&& initrd_filename
!= NULL
) {
4275 fprintf(stderr
, "-initrd only allowed with -kernel option\n");
4279 if (!linux_boot
&& qemu_opt_get(machine_opts
, "dtb")) {
4280 fprintf(stderr
, "-dtb only allowed with -kernel option\n");
4284 os_set_line_buffering();
4286 qemu_init_cpu_loop();
4287 qemu_mutex_lock_iothread();
4290 /* spice needs the timers to be initialized by this point */
4294 if (icount_option
&& (kvm_enabled() || xen_enabled())) {
4295 fprintf(stderr
, "-icount is not allowed with kvm or xen\n");
4298 configure_icount(icount_option
);
4300 /* clean up network at qemu process termination */
4301 atexit(&net_cleanup
);
4303 if (net_init_clients() < 0) {
4308 if (tpm_init() < 0) {
4313 /* init the bluetooth world */
4314 if (foreach_device_config(DEV_BT
, bt_parse
))
4317 if (!xen_enabled()) {
4318 /* On 32-bit hosts, QEMU is limited by virtual address space */
4319 if (ram_size
> (2047 << 20) && HOST_LONG_BITS
== 32) {
4320 fprintf(stderr
, "qemu: at most 2047 MB RAM can be simulated\n");
4328 /* open the virtual block devices */
4330 qemu_opts_foreach(qemu_find_opts("drive"), drive_enable_snapshot
, NULL
, 0);
4331 if (qemu_opts_foreach(qemu_find_opts("drive"), drive_init_func
,
4332 &machine_class
->block_default_type
, 1) != 0) {
4336 default_drive(default_cdrom
, snapshot
, machine_class
->block_default_type
, 2,
4338 default_drive(default_floppy
, snapshot
, IF_FLOPPY
, 0, FD_OPTS
);
4339 default_drive(default_sdcard
, snapshot
, IF_SD
, 0, SD_OPTS
);
4341 if (qemu_opts_foreach(qemu_find_opts("numa"), numa_init_func
,
4348 if (qemu_opts_foreach(qemu_find_opts("mon"), mon_init_func
, NULL
, 1) != 0) {
4352 if (foreach_device_config(DEV_SERIAL
, serial_parse
) < 0)
4354 if (foreach_device_config(DEV_PARALLEL
, parallel_parse
) < 0)
4356 if (foreach_device_config(DEV_VIRTCON
, virtcon_parse
) < 0)
4358 if (foreach_device_config(DEV_SCLP
, sclp_parse
) < 0) {
4361 if (foreach_device_config(DEV_DEBUGCON
, debugcon_parse
) < 0)
4364 /* If no default VGA is requested, the default is "none". */
4366 if (cirrus_vga_available()) {
4367 vga_model
= "cirrus";
4368 } else if (vga_available()) {
4373 select_vgahw(vga_model
);
4377 i
= select_watchdog(watchdog
);
4379 exit (i
== 1 ? 1 : 0);
4382 if (machine_class
->compat_props
) {
4383 qdev_prop_register_global_list(machine_class
->compat_props
);
4387 qdev_machine_init();
4389 current_machine
->ram_size
= ram_size
;
4390 current_machine
->maxram_size
= maxram_size
;
4391 current_machine
->ram_slots
= ram_slots
;
4392 current_machine
->boot_order
= boot_order
;
4393 current_machine
->cpu_model
= cpu_model
;
4395 machine_class
->init(current_machine
);
4399 cpu_synchronize_all_post_init();
4403 /* init USB devices */
4404 if (usb_enabled(false)) {
4405 if (foreach_device_config(DEV_USB
, usb_parse
) < 0)
4409 /* init generic devices */
4410 if (qemu_opts_foreach(qemu_find_opts("device"), device_init_func
, NULL
, 1) != 0)
4413 net_check_clients();
4415 ds
= init_displaystate();
4417 /* init local displays */
4418 switch (display_type
) {
4420 (void)ds
; /* avoid warning if no display is configured */
4422 #if defined(CONFIG_CURSES)
4424 curses_display_init(ds
, full_screen
);
4427 #if defined(CONFIG_SDL)
4429 sdl_display_init(ds
, full_screen
, no_frame
);
4431 #elif defined(CONFIG_COCOA)
4433 cocoa_display_init(ds
, full_screen
);
4436 #if defined(CONFIG_GTK)
4438 gtk_display_init(ds
, full_screen
, grab_on_hover
);
4445 /* must be after terminal init, SDL library changes signal handlers */
4446 os_setup_signal_handling();
4449 /* init remote displays */
4451 Error
*local_err
= NULL
;
4452 vnc_display_init(ds
);
4453 vnc_display_open(ds
, vnc_display
, &local_err
);
4454 if (local_err
!= NULL
) {
4455 error_report("Failed to start VNC server on `%s': %s",
4456 vnc_display
, error_get_pretty(local_err
));
4457 error_free(local_err
);
4461 if (show_vnc_port
) {
4462 printf("VNC server running on `%s'\n", vnc_display_local_addr(ds
));
4468 qemu_spice_display_init();
4472 if (foreach_device_config(DEV_GDB
, gdbserver_start
) < 0) {
4476 qdev_machine_creation_done();
4478 if (rom_load_all() != 0) {
4479 fprintf(stderr
, "rom loading failed\n");
4483 /* TODO: once all bus devices are qdevified, this should be done
4484 * when bus is created by qdev.c */
4485 qemu_register_reset(qbus_reset_all_fn
, sysbus_get_default());
4486 qemu_run_machine_init_done_notifiers();
4488 /* Done notifiers can load ROMs */
4491 qemu_system_reset(VMRESET_SILENT
);
4493 if (load_vmstate(loadvm
) < 0) {
4498 qdev_prop_check_global();
4499 if (vmstate_dump_file
) {
4501 dump_vmstate_json_to_file(vmstate_dump_file
);
4506 Error
*local_err
= NULL
;
4507 qemu_start_incoming_migration(incoming
, &local_err
);
4509 error_report("-incoming %s: %s", incoming
,
4510 error_get_pretty(local_err
));
4511 error_free(local_err
);
4514 } else if (autostart
) {
4520 if (is_daemonized()) {
4521 if (!trace_init_backends(trace_events
, trace_file
)) {