blob: f5f7c3f7c455f43448f1b06ba78ba23603bc10e7 [file] [log] [blame]
bellard0824d6f2003-06-24 13:42:40 +00001/*
bellard80cabfa2004-03-14 12:20:30 +00002 * QEMU System Emulator
ths5fafdf22007-09-16 21:08:06 +00003 *
bellard68d0f702008-01-06 17:21:48 +00004 * Copyright (c) 2003-2008 Fabrice Bellard
ths5fafdf22007-09-16 21:08:06 +00005 *
bellard1df912c2003-06-25 16:20:35 +00006 * 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:
12 *
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
15 *
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
22 * THE SOFTWARE.
bellard0824d6f2003-06-24 13:42:40 +000023 */
bellard0824d6f2003-06-24 13:42:40 +000024#include <unistd.h>
bellard0824d6f2003-06-24 13:42:40 +000025#include <fcntl.h>
26#include <signal.h>
27#include <time.h>
bellard0824d6f2003-06-24 13:42:40 +000028#include <errno.h>
bellard67b915a2004-03-31 23:37:16 +000029#include <sys/time.h>
30
blueswir1d40cdb12009-03-07 16:52:02 +000031#include "config-host.h"
32
Eduardo Otubo452dfbe2012-08-14 18:44:07 -030033#ifdef CONFIG_SECCOMP
Paolo Bonzini9c17d612012-12-17 18:20:04 +010034#include "sysemu/seccomp.h"
Eduardo Otubo452dfbe2012-08-14 18:44:07 -030035#endif
36
ths8a16d272008-07-19 09:56:24 +000037#if defined(CONFIG_VDE)
38#include <libvdeplug.h>
39#endif
40
bellard73332e52004-04-04 20:22:28 +000041#ifdef CONFIG_SDL
Stefan Weil59a36a22009-06-18 20:11:03 +020042#if defined(__APPLE__) || defined(main)
Stefan Weil66936652009-06-13 13:19:11 +020043#include <SDL.h>
malc880fec52009-02-15 20:18:41 +000044int qemu_main(int argc, char **argv, char **envp);
45int main(int argc, char **argv)
46{
Stefan Weil59a36a22009-06-18 20:11:03 +020047 return qemu_main(argc, argv, NULL);
malc880fec52009-02-15 20:18:41 +000048}
49#undef main
50#define main qemu_main
bellard96bcd4f2004-07-10 16:26:15 +000051#endif
bellard73332e52004-04-04 20:22:28 +000052#endif /* CONFIG_SDL */
bellard0824d6f2003-06-24 13:42:40 +000053
bellard5b0753e2005-03-01 21:37:28 +000054#ifdef CONFIG_COCOA
55#undef main
56#define main qemu_main
57#endif /* CONFIG_COCOA */
58
Anthony Liguori69e5bb62011-08-22 08:12:52 -050059#include <glib.h>
60
Markus Armbrusterd49b6832015-03-17 18:29:20 +010061#include "qemu/error-report.h"
Marcel Apfelbaumc8897e82014-03-18 17:26:35 +020062#include "qemu/sockets.h"
blueswir1511d2b12009-03-07 15:32:56 +000063#include "hw/hw.h"
64#include "hw/boards.h"
Eduardo Habkosta1a9cb02014-09-26 17:45:17 -030065#include "sysemu/accel.h"
blueswir1511d2b12009-03-07 15:32:56 +000066#include "hw/usb.h"
Paolo Bonzini0d09e412013-02-05 17:06:20 +010067#include "hw/i386/pc.h"
68#include "hw/isa/isa.h"
blueswir1511d2b12009-03-07 15:32:56 +000069#include "hw/bt.h"
Paolo Bonzini0d09e412013-02-05 17:06:20 +010070#include "sysemu/watchdog.h"
Wei Huang60d8f322015-08-11 22:08:20 -040071#include "hw/smbios/smbios.h"
Paolo Bonzini0d09e412013-02-05 17:06:20 +010072#include "hw/xen/xen.h"
Gerd Hoffmannbd3c9482009-07-15 13:59:26 +020073#include "hw/qdev.h"
Gerd Hoffmann45a50b12009-10-01 16:42:33 +020074#include "hw/loader.h"
Paolo Bonzinib4a42f82013-02-04 11:37:52 +010075#include "monitor/qdev.h"
Paolo Bonzinidccfcd02013-04-08 16:55:25 +020076#include "sysemu/bt.h"
Paolo Bonzini1422e322012-10-24 08:43:34 +020077#include "net/net.h"
Mark McLoughlin68ac40d2009-11-25 18:48:54 +000078#include "net/slirp.h"
Paolo Bonzini83c90892012-12-17 18:19:49 +010079#include "monitor/monitor.h"
Paolo Bonzini28ecbae2012-11-28 12:06:30 +010080#include "ui/console.h"
Paolo Bonzini9c17d612012-12-17 18:20:04 +010081#include "sysemu/sysemu.h"
Eduardo Habkoste35704b2015-02-08 16:51:16 -020082#include "sysemu/numa.h"
Paolo Bonzini022c62c2012-12-17 18:19:49 +010083#include "exec/gdbstub.h"
Paolo Bonzini1de7afc2012-12-17 18:20:00 +010084#include "qemu/timer.h"
Paolo Bonzinidccfcd02013-04-08 16:55:25 +020085#include "sysemu/char.h"
Michael Tokarevb33276a2014-02-07 12:26:14 +040086#include "qemu/bitmap.h"
Paolo Bonzini9c17d612012-12-17 18:20:04 +010087#include "sysemu/blockdev.h"
Paolo Bonzini0d09e412013-02-05 17:06:20 +010088#include "hw/block/block.h"
Paolo Bonzinicaf71f82012-12-17 18:19:50 +010089#include "migration/block.h"
Paolo Bonzinibdee56f2013-04-02 18:28:41 +020090#include "sysemu/tpm.h"
Paolo Bonzini9c17d612012-12-17 18:20:04 +010091#include "sysemu/dma.h"
blueswir1511d2b12009-03-07 15:32:56 +000092#include "audio/audio.h"
Paolo Bonzinicaf71f82012-12-17 18:19:50 +010093#include "migration/migration.h"
Paolo Bonzini9c17d612012-12-17 18:20:04 +010094#include "sysemu/kvm.h"
Paolo Bonzini7b1b5d12012-12-17 18:19:43 +010095#include "qapi/qmp/qjson.h"
Paolo Bonzini1de7afc2012-12-17 18:20:00 +010096#include "qemu/option.h"
97#include "qemu/config-file.h"
Jes Sorensen59a52642010-06-10 11:42:25 +020098#include "qemu-options.h"
Luiz Capitulino1fa9a5e2011-09-12 17:54:20 -030099#include "qmp-commands.h"
Paolo Bonzini1de7afc2012-12-17 18:20:00 +0100100#include "qemu/main-loop.h"
Venkateswararao Jujjuri (JV)758e8e32010-06-14 13:34:41 -0700101#ifdef CONFIG_VIRTFS
Gautham R Shenoy74db9202010-04-29 17:44:43 +0530102#include "fsdev/qemu-fsdev.h"
103#endif
Paolo Bonzini9c17d612012-12-17 18:20:04 +0100104#include "sysemu/qtest.h"
blueswir1511d2b12009-03-07 15:32:56 +0000105
Paolo Bonzini76cad712012-10-24 11:12:21 +0200106#include "disas/disas.h"
bellardfc01f7e2003-06-30 10:03:06 +0000107
blueswir1511d2b12009-03-07 15:32:56 +0000108
Jan Kiszkad918f232009-06-24 14:42:30 +0200109#include "slirp/libslirp.h"
blueswir1511d2b12009-03-07 15:32:56 +0000110
Stefan Hajnoczi94b0b5f2010-11-16 12:20:25 +0000111#include "trace.h"
LluĂ­se4858972011-08-31 20:31:03 +0200112#include "trace/control.h"
Paolo Bonzini1de7afc2012-12-17 18:20:00 +0100113#include "qemu/queue.h"
Paolo Bonzini9c17d612012-12-17 18:20:04 +0100114#include "sysemu/cpus.h"
115#include "sysemu/arch_init.h"
Paolo Bonzini1de7afc2012-12-17 18:20:00 +0100116#include "qemu/osdep.h"
Blue Swirl72cf2d42009-09-12 07:36:22 +0000117
Gerd Hoffmann29b00402010-03-11 11:13:27 -0300118#include "ui/qemu-spice.h"
Anthony Liguori68d98d32012-06-25 14:36:33 -0500119#include "qapi/string-input-visitor.h"
Paolo Bonzinic4090f82014-06-10 19:15:15 +0800120#include "qapi/opts-visitor.h"
Wenchao Xia84321832014-06-18 08:43:33 +0200121#include "qom/object_interfaces.h"
122#include "qapi-event.h"
Leon Alraecfe67ce2015-06-19 14:17:45 +0100123#include "exec/semihost.h"
Daniel P. Berrangeddbb0d02015-07-01 18:10:29 +0100124#include "crypto/init.h"
Gerd Hoffmann29b00402010-03-11 11:13:27 -0300125
Amit Shah98b19252010-01-20 00:36:52 +0530126#define MAX_VIRTIO_CONSOLES 1
Alexander Graf3ef669e2013-01-24 12:18:52 +0100127#define MAX_SCLP_CONSOLES 1
Amit Shah98b19252010-01-20 00:36:52 +0530128
Gerd Hoffmann45240512013-03-08 11:42:24 +0100129static const char *data_dir[16];
130static int data_dir_idx;
j_mayer1192dad2007-10-05 13:08:35 +0000131const char *bios_name = NULL;
malccb5a7aa2008-09-28 00:42:12 +0000132enum vga_retrace_method vga_retrace_method = VGA_RETRACE_DUMB;
Anthony Liguori993fbfd2009-05-21 16:54:00 -0500133DisplayType display_type = DT_DEFAULT;
Gerd Hoffmann0b71a5d2014-11-11 16:54:45 +0100134int request_opengl = -1;
Gerd Hoffmannda076ff2014-11-20 09:49:44 +0100135int display_opengl;
Blue Swirl4fdcac02012-10-28 11:04:47 +0000136static int display_remote;
bellard3d11d0e2004-12-12 16:56:30 +0000137const char* keyboard_layout = NULL;
Anthony Liguoric227f092009-10-01 16:12:16 -0500138ram_addr_t ram_size;
Marcelo Tosattic9027602010-03-01 20:25:08 -0300139const char *mem_path = NULL;
Marcelo Tosattic9027602010-03-01 20:25:08 -0300140int mem_prealloc = 0; /* force preallocation of physical target memory */
zhanghailiang28d16f32014-09-23 18:42:24 +0800141bool enable_mlock = false;
bellardc4b1fcc2004-03-14 21:44:30 +0000142int nb_nics;
bellard7c9d8e02005-11-15 22:16:05 +0000143NICInfo nd_table[MAX_NICS];
Paolo Bonzinid399f672009-07-27 23:17:51 +0200144int autostart;
balrogf6503052008-02-17 11:42:19 +0000145static int rtc_utc = 1;
146static int rtc_date_offset = -1; /* -1 means no change */
Alex Bligh884f17c2013-08-21 16:03:04 +0100147QEMUClockType rtc_clock;
Gerd Hoffmann64465292009-12-08 13:11:45 +0100148int vga_interface_type = VGA_NONE;
blueswir1dbed7e42008-10-01 19:38:09 +0000149static int full_screen = 0;
150static int no_frame = 0;
ths667acca2006-12-11 02:08:05 +0000151int no_quit = 0;
Jan Kiszka881249c2014-03-12 08:33:50 +0100152#ifdef CONFIG_GTK
153static bool grab_on_hover;
154#endif
bellard8d11df92004-08-24 21:13:40 +0000155CharDriverState *serial_hds[MAX_SERIAL_PORTS];
bellard6508fe52005-01-15 12:02:56 +0000156CharDriverState *parallel_hds[MAX_PARALLEL_PORTS];
aliguori9ede2fd2009-01-15 20:05:25 +0000157CharDriverState *virtcon_hds[MAX_VIRTIO_CONSOLES];
Alexander Graf3ef669e2013-01-24 12:18:52 +0100158CharDriverState *sclp_hds[MAX_SCLP_CONSOLES];
bellarda09db212005-04-30 16:10:35 +0000159int win2k_install_hack = 0;
aurel321b530a62009-04-05 20:08:59 +0000160int singlestep = 0;
bellard6a00d602005-11-21 23:25:50 +0000161int smp_cpus = 1;
Jes Sorensen6be68d72009-07-23 17:03:42 +0200162int max_cpus = 0;
Andre Przywaradc6b1c02009-08-19 15:42:40 +0200163int smp_cores = 1;
164int smp_threads = 1;
bellard6515b202006-05-03 22:02:44 +0000165int acpi_enabled = 1;
aliguori16b29ae2008-12-17 23:28:44 +0000166int no_hpet = 0;
bellard52ca8d62006-06-14 16:03:05 +0000167int fd_bootchk = 1;
Blue Swirl4fdcac02012-10-28 11:04:47 +0000168static int no_reboot;
aurel32b2f76162008-04-11 21:35:52 +0000169int no_shutdown = 0;
balrog9467cd42007-05-01 01:34:14 +0000170int cursor_hide = 1;
balroga171fe32007-04-30 01:48:07 +0000171int graphic_rotate = 0;
Markus Armbruster09aaa162009-08-21 10:31:34 +0200172const char *watchdog;
Gleb Natapov2e55e842010-12-08 13:35:07 +0200173QEMUOptionRom option_rom[MAX_OPTION_ROMS];
ths9ae02552007-01-05 17:39:04 +0000174int nb_option_roms;
balrog2b8f2d42007-07-27 22:08:46 +0000175int old_param = 0;
thsc35734b2007-03-19 15:17:08 +0000176const char *qemu_name;
ths3780e192007-06-21 21:08:02 +0000177int alt_grab = 0;
Dustin Kirkland0ca9f8a2009-09-17 15:48:04 -0500178int ctrl_grab = 0;
blueswir166508602007-05-01 14:16:52 +0000179unsigned int nb_prom_envs = 0;
180const char *prom_envs[MAX_PROM_ENVS];
Jan Kiszka95387492009-07-02 00:19:02 +0200181int boot_menu;
Gongleibc741122014-10-07 16:00:05 +0800182bool boot_strict;
wayne3d3b8302011-07-27 18:04:55 +0800183uint8_t *boot_splash_filedata;
Markus Armbrusterd09acb92013-01-23 18:25:08 +0100184size_t boot_splash_filedata_size;
wayne3d3b8302011-07-27 18:04:55 +0800185uint8_t qemu_extra_params_fw[2];
bellard0824d6f2003-06-24 13:42:40 +0000186
Sebastian Tanasea8bfac32014-07-25 11:56:29 +0200187int icount_align_option;
Gleb Natapov1ca4d092010-12-08 13:35:05 +0200188
Eduardo Habkostcaad0572014-10-29 11:26:08 -0200189/* The bytes in qemu_uuid[] are in the order specified by RFC4122, _not_ in the
190 * little-endian "wire format" described in the SMBIOS 2.6 specification.
191 */
blueswir18fcb1b92008-09-18 18:29:08 +0000192uint8_t qemu_uuid[16];
Markus Armbrusterfc3b3292013-08-16 15:18:31 +0200193bool qemu_uuid_set;
blueswir18fcb1b92008-09-18 18:29:08 +0000194
Gerd Hoffmannfd42dee2010-06-04 14:08:07 +0200195static NotifierList exit_notifiers =
196 NOTIFIER_LIST_INITIALIZER(exit_notifiers);
197
Gleb Natapov4cab9462010-12-08 13:35:08 +0200198static NotifierList machine_init_done_notifiers =
199 NOTIFIER_LIST_INITIALIZER(machine_init_done_notifiers);
200
liguangd5286af2013-01-24 13:03:27 +0800201bool xen_allowed;
Blue Swirld745bef2010-03-29 19:23:49 +0000202uint32_t xen_domid;
203enum xen_mode xen_mode = XEN_EMULATE;
204
Mark Wud44229c2014-03-10 22:37:40 +0800205static int has_defaults = 1;
Gerd Hoffmann998bbd72009-12-08 13:11:41 +0100206static int default_serial = 1;
Gerd Hoffmann6a5e8b02009-12-08 13:11:42 +0100207static int default_parallel = 1;
Gerd Hoffmann986c5f72009-12-08 13:11:54 +0100208static int default_virtcon = 1;
Alexander Graf3ef669e2013-01-24 12:18:52 +0100209static int default_sclp = 1;
Gerd Hoffmannabdeed02009-12-08 13:11:43 +0100210static int default_monitor = 1;
Gerd Hoffmannac33f8f2009-12-16 14:25:39 +0100211static int default_floppy = 1;
212static int default_cdrom = 1;
213static int default_sdcard = 1;
Paolo Bonzini7f1b17f2012-05-10 09:39:17 +0200214static int default_vga = 1;
Gerd Hoffmann998bbd72009-12-08 13:11:41 +0100215
216static struct {
217 const char *driver;
218 int *flag;
219} default_list[] = {
Gerd Hoffmann6a5e8b02009-12-08 13:11:42 +0100220 { .driver = "isa-serial", .flag = &default_serial },
221 { .driver = "isa-parallel", .flag = &default_parallel },
Gerd Hoffmannd8bcbab2009-12-16 14:25:40 +0100222 { .driver = "isa-fdc", .flag = &default_floppy },
Markus Armbrusteraf6bf132011-05-18 18:31:02 +0200223 { .driver = "ide-cd", .flag = &default_cdrom },
224 { .driver = "ide-hd", .flag = &default_cdrom },
Gerd Hoffmannd8bcbab2009-12-16 14:25:40 +0100225 { .driver = "ide-drive", .flag = &default_cdrom },
Markus Armbrusteraf6bf132011-05-18 18:31:02 +0200226 { .driver = "scsi-cd", .flag = &default_cdrom },
Amit Shah392ecf52010-01-21 16:19:23 +0530227 { .driver = "virtio-serial-pci", .flag = &default_virtcon },
228 { .driver = "virtio-serial-s390", .flag = &default_virtcon },
229 { .driver = "virtio-serial", .flag = &default_virtcon },
Paolo Bonzini7f1b17f2012-05-10 09:39:17 +0200230 { .driver = "VGA", .flag = &default_vga },
231 { .driver = "isa-vga", .flag = &default_vga },
232 { .driver = "cirrus-vga", .flag = &default_vga },
233 { .driver = "isa-cirrus-vga", .flag = &default_vga },
234 { .driver = "vmware-svga", .flag = &default_vga },
235 { .driver = "qxl-vga", .flag = &default_vga },
Gerd Hoffmanna94f0c52014-09-10 14:28:48 +0200236 { .driver = "virtio-vga", .flag = &default_vga },
Gerd Hoffmann998bbd72009-12-08 13:11:41 +0100237};
238
Paolo Bonzini4d454572012-11-26 16:03:42 +0100239static QemuOptsList qemu_rtc_opts = {
240 .name = "rtc",
241 .head = QTAILQ_HEAD_INITIALIZER(qemu_rtc_opts.head),
242 .desc = {
243 {
244 .name = "base",
245 .type = QEMU_OPT_STRING,
246 },{
247 .name = "clock",
248 .type = QEMU_OPT_STRING,
249 },{
250 .name = "driftfix",
251 .type = QEMU_OPT_STRING,
252 },
253 { /* end of list */ }
254 },
255};
256
257static QemuOptsList qemu_sandbox_opts = {
258 .name = "sandbox",
259 .implied_opt_name = "enable",
260 .head = QTAILQ_HEAD_INITIALIZER(qemu_sandbox_opts.head),
261 .desc = {
262 {
263 .name = "enable",
264 .type = QEMU_OPT_BOOL,
265 },
266 { /* end of list */ }
267 },
268};
269
270static QemuOptsList qemu_trace_opts = {
271 .name = "trace",
272 .implied_opt_name = "trace",
273 .head = QTAILQ_HEAD_INITIALIZER(qemu_trace_opts.head),
274 .desc = {
275 {
276 .name = "events",
277 .type = QEMU_OPT_STRING,
278 },{
279 .name = "file",
280 .type = QEMU_OPT_STRING,
281 },
282 { /* end of list */ }
283 },
284};
285
286static QemuOptsList qemu_option_rom_opts = {
287 .name = "option-rom",
288 .implied_opt_name = "romfile",
289 .head = QTAILQ_HEAD_INITIALIZER(qemu_option_rom_opts.head),
290 .desc = {
291 {
292 .name = "bootindex",
293 .type = QEMU_OPT_NUMBER,
294 }, {
295 .name = "romfile",
296 .type = QEMU_OPT_STRING,
297 },
298 { /* end of list */ }
299 },
300};
301
302static QemuOptsList qemu_machine_opts = {
303 .name = "machine",
304 .implied_opt_name = "type",
305 .merge_lists = true,
306 .head = QTAILQ_HEAD_INITIALIZER(qemu_machine_opts.head),
307 .desc = {
Marcel Apfelbaum49d2e642014-12-16 16:58:05 +0000308 /*
309 * no elements => accept any
310 * sanity checking will happen later
311 * when setting machine properties
312 */
313 { }
Paolo Bonzini4d454572012-11-26 16:03:42 +0100314 },
315};
316
317static QemuOptsList qemu_boot_opts = {
318 .name = "boot-opts",
Markus Armbruster6ef47162013-06-14 13:15:01 +0200319 .implied_opt_name = "order",
320 .merge_lists = true,
Paolo Bonzini4d454572012-11-26 16:03:42 +0100321 .head = QTAILQ_HEAD_INITIALIZER(qemu_boot_opts.head),
322 .desc = {
Paolo Bonzini4d454572012-11-26 16:03:42 +0100323 {
324 .name = "order",
325 .type = QEMU_OPT_STRING,
326 }, {
327 .name = "once",
328 .type = QEMU_OPT_STRING,
329 }, {
330 .name = "menu",
Markus Armbruster6ef47162013-06-14 13:15:01 +0200331 .type = QEMU_OPT_BOOL,
Paolo Bonzini4d454572012-11-26 16:03:42 +0100332 }, {
333 .name = "splash",
334 .type = QEMU_OPT_STRING,
335 }, {
336 .name = "splash-time",
337 .type = QEMU_OPT_STRING,
338 }, {
339 .name = "reboot-timeout",
340 .type = QEMU_OPT_STRING,
Amos Kongc8a6ae82013-03-19 14:23:27 +0800341 }, {
342 .name = "strict",
Amos Konge5187b52013-12-09 19:53:15 +0800343 .type = QEMU_OPT_BOOL,
Paolo Bonzini4d454572012-11-26 16:03:42 +0100344 },
345 { /*End of list */ }
346 },
347};
348
349static QemuOptsList qemu_add_fd_opts = {
350 .name = "add-fd",
351 .head = QTAILQ_HEAD_INITIALIZER(qemu_add_fd_opts.head),
352 .desc = {
353 {
354 .name = "fd",
355 .type = QEMU_OPT_NUMBER,
356 .help = "file descriptor of which a duplicate is added to fd set",
357 },{
358 .name = "set",
359 .type = QEMU_OPT_NUMBER,
360 .help = "ID of the fd set to add fd to",
361 },{
362 .name = "opaque",
363 .type = QEMU_OPT_STRING,
364 .help = "free-form string used to describe fd",
365 },
366 { /* end of list */ }
367 },
368};
369
370static QemuOptsList qemu_object_opts = {
371 .name = "object",
372 .implied_opt_name = "qom-type",
373 .head = QTAILQ_HEAD_INITIALIZER(qemu_object_opts.head),
374 .desc = {
375 { }
376 },
377};
378
Stefan Bergerd1a0cf72013-02-27 12:47:49 -0500379static QemuOptsList qemu_tpmdev_opts = {
380 .name = "tpmdev",
381 .implied_opt_name = "type",
382 .head = QTAILQ_HEAD_INITIALIZER(qemu_tpmdev_opts.head),
383 .desc = {
Stefan Bergerbb716232013-04-22 10:41:39 -0400384 /* options are defined in the TPM backends */
Stefan Bergerd1a0cf72013-02-27 12:47:49 -0500385 { /* end of list */ }
386 },
387};
388
Satoru Moriya888a6bc2013-04-19 16:42:06 +0200389static QemuOptsList qemu_realtime_opts = {
390 .name = "realtime",
391 .head = QTAILQ_HEAD_INITIALIZER(qemu_realtime_opts.head),
392 .desc = {
393 {
394 .name = "mlock",
395 .type = QEMU_OPT_BOOL,
396 },
397 { /* end of list */ }
398 },
399};
400
Seiji Aguchi5e2ac512013-07-03 23:02:46 -0400401static QemuOptsList qemu_msg_opts = {
402 .name = "msg",
403 .head = QTAILQ_HEAD_INITIALIZER(qemu_msg_opts.head),
404 .desc = {
405 {
406 .name = "timestamp",
407 .type = QEMU_OPT_BOOL,
408 },
409 { /* end of list */ }
410 },
411};
412
Dr. David Alan Gilbert5d12f962014-01-30 10:20:30 +0000413static QemuOptsList qemu_name_opts = {
414 .name = "name",
415 .implied_opt_name = "guest",
416 .merge_lists = true,
417 .head = QTAILQ_HEAD_INITIALIZER(qemu_name_opts.head),
418 .desc = {
419 {
420 .name = "guest",
421 .type = QEMU_OPT_STRING,
422 .help = "Sets the name of the guest.\n"
423 "This name will be displayed in the SDL window caption.\n"
424 "The name will also be used for the VNC server",
425 }, {
426 .name = "process",
427 .type = QEMU_OPT_STRING,
428 .help = "Sets the name of the QEMU process, as shown in top etc",
Dr. David Alan Gilbert8f480de2014-01-30 10:20:31 +0000429 }, {
430 .name = "debug-threads",
431 .type = QEMU_OPT_BOOL,
432 .help = "When enabled, name the individual threads; defaults off.\n"
433 "NOTE: The thread names are for debugging and not a\n"
434 "stable API.",
Dr. David Alan Gilbert5d12f962014-01-30 10:20:30 +0000435 },
436 { /* End of list */ }
437 },
438};
439
Igor Mammedov6e1d3c12013-11-27 01:27:35 +0100440static QemuOptsList qemu_mem_opts = {
441 .name = "memory",
442 .implied_opt_name = "size",
443 .head = QTAILQ_HEAD_INITIALIZER(qemu_mem_opts.head),
444 .merge_lists = true,
445 .desc = {
446 {
447 .name = "size",
448 .type = QEMU_OPT_SIZE,
449 },
Igor Mammedovc270fb92014-06-02 15:25:02 +0200450 {
451 .name = "slots",
452 .type = QEMU_OPT_NUMBER,
453 },
454 {
455 .name = "maxmem",
456 .type = QEMU_OPT_SIZE,
457 },
Igor Mammedov6e1d3c12013-11-27 01:27:35 +0100458 { /* end of list */ }
459 },
460};
461
Sebastian Tanase1ad95802014-07-25 11:56:28 +0200462static QemuOptsList qemu_icount_opts = {
463 .name = "icount",
464 .implied_opt_name = "shift",
465 .merge_lists = true,
466 .head = QTAILQ_HEAD_INITIALIZER(qemu_icount_opts.head),
467 .desc = {
468 {
469 .name = "shift",
470 .type = QEMU_OPT_STRING,
Sebastian Tanasea8bfac32014-07-25 11:56:29 +0200471 }, {
472 .name = "align",
473 .type = QEMU_OPT_BOOL,
Victor CLEMENTf1f4b572015-05-29 17:14:05 +0200474 }, {
475 .name = "sleep",
476 .type = QEMU_OPT_BOOL,
Sebastian Tanase1ad95802014-07-25 11:56:28 +0200477 },
478 { /* end of list */ }
479 },
480};
481
Liviu Ionescua38bb072014-12-11 12:07:48 +0000482static QemuOptsList qemu_semihosting_config_opts = {
483 .name = "semihosting-config",
484 .implied_opt_name = "enable",
485 .head = QTAILQ_HEAD_INITIALIZER(qemu_semihosting_config_opts.head),
486 .desc = {
487 {
488 .name = "enable",
489 .type = QEMU_OPT_BOOL,
490 }, {
491 .name = "target",
492 .type = QEMU_OPT_STRING,
Leon Alraea59d31a2015-06-19 14:17:45 +0100493 }, {
494 .name = "arg",
495 .type = QEMU_OPT_STRING,
Liviu Ionescua38bb072014-12-11 12:07:48 +0000496 },
497 { /* end of list */ }
498 },
499};
500
Gabriel L. Somlo81b2b812015-04-29 11:21:53 -0400501static QemuOptsList qemu_fw_cfg_opts = {
502 .name = "fw_cfg",
503 .implied_opt_name = "name",
504 .head = QTAILQ_HEAD_INITIALIZER(qemu_fw_cfg_opts.head),
505 .desc = {
506 {
507 .name = "name",
508 .type = QEMU_OPT_STRING,
509 .help = "Sets the fw_cfg name of the blob to be inserted",
510 }, {
511 .name = "file",
512 .type = QEMU_OPT_STRING,
513 .help = "Sets the name of the file from which\n"
514 "the fw_cfg blob will be loaded",
Gabriel L. Somlo6407d762015-09-29 12:29:01 -0400515 }, {
516 .name = "string",
517 .type = QEMU_OPT_STRING,
518 .help = "Sets content of the blob to be inserted from a string",
Gabriel L. Somlo81b2b812015-04-29 11:21:53 -0400519 },
520 { /* end of list */ }
521 },
522};
523
Markus Armbruster7f9d6e52013-07-04 15:09:19 +0200524/**
525 * Get machine options
526 *
527 * Returns: machine options (never null).
528 */
529QemuOpts *qemu_get_machine_opts(void)
530{
Paolo Bonzinie96e5ae2014-03-06 10:39:24 +0100531 return qemu_find_opts_singleton("machine");
Markus Armbruster7f9d6e52013-07-04 15:09:19 +0200532}
533
Ronnie Sahlberg31459f42012-08-06 18:24:55 +1000534const char *qemu_get_vm_name(void)
535{
536 return qemu_name;
537}
538
wayne3d3b8302011-07-27 18:04:55 +0800539static void res_free(void)
540{
Markus Armbruster012aef02015-08-26 14:02:53 +0200541 g_free(boot_splash_filedata);
542 boot_splash_filedata = NULL;
wayne3d3b8302011-07-27 18:04:55 +0800543}
544
Markus Armbruster28d0de72015-03-13 13:35:14 +0100545static int default_driver_check(void *opaque, QemuOpts *opts, Error **errp)
Gerd Hoffmann998bbd72009-12-08 13:11:41 +0100546{
547 const char *driver = qemu_opt_get(opts, "driver");
548 int i;
549
550 if (!driver)
551 return 0;
552 for (i = 0; i < ARRAY_SIZE(default_list); i++) {
553 if (strcmp(default_list[i].driver, driver) != 0)
554 continue;
555 *(default_list[i].flag) = 0;
556 }
557 return 0;
558}
559
bellard0824d6f2003-06-24 13:42:40 +0000560/***********************************************************/
Luiz Capitulinof5bbfba2011-07-29 15:04:45 -0300561/* QEMU state */
562
Luiz Capitulino0461d5a2011-09-30 14:45:27 -0300563static RunState current_run_state = RUN_STATE_PRELAUNCH;
Luiz Capitulinof5bbfba2011-07-29 15:04:45 -0300564
Paolo Bonzini74892d22014-06-05 14:53:58 +0200565/* We use RUN_STATE_MAX but any invalid value will do */
566static RunState vmstop_requested = RUN_STATE_MAX;
567static QemuMutex vmstop_lock;
568
Luiz Capitulino5db9d4d2011-09-05 16:36:31 -0300569typedef struct {
570 RunState from;
571 RunState to;
572} RunStateTransition;
573
574static const RunStateTransition runstate_transitions_def[] = {
575 /* from -> to */
Luiz Capitulino0461d5a2011-09-30 14:45:27 -0300576 { RUN_STATE_DEBUG, RUN_STATE_RUNNING },
Paolo Bonzinieca01d32013-12-03 13:00:15 +0100577 { RUN_STATE_DEBUG, RUN_STATE_FINISH_MIGRATE },
Luiz Capitulino5db9d4d2011-09-05 16:36:31 -0300578
Juan Quintelaca3fc392014-10-08 12:47:08 +0200579 { RUN_STATE_INMIGRATE, RUN_STATE_INTERNAL_ERROR },
580 { RUN_STATE_INMIGRATE, RUN_STATE_IO_ERROR },
Paolo Bonzini29ed72f2012-10-19 16:45:24 +0200581 { RUN_STATE_INMIGRATE, RUN_STATE_PAUSED },
Juan Quintelaca3fc392014-10-08 12:47:08 +0200582 { RUN_STATE_INMIGRATE, RUN_STATE_RUNNING },
583 { RUN_STATE_INMIGRATE, RUN_STATE_SHUTDOWN },
584 { RUN_STATE_INMIGRATE, RUN_STATE_SUSPENDED },
585 { RUN_STATE_INMIGRATE, RUN_STATE_WATCHDOG },
586 { RUN_STATE_INMIGRATE, RUN_STATE_GUEST_PANICKED },
Juan Quintela2a6e6e52015-07-28 15:28:28 +0200587 { RUN_STATE_INMIGRATE, RUN_STATE_FINISH_MIGRATE },
Luiz Capitulino5db9d4d2011-09-05 16:36:31 -0300588
Luiz Capitulino0461d5a2011-09-30 14:45:27 -0300589 { RUN_STATE_INTERNAL_ERROR, RUN_STATE_PAUSED },
Luiz Capitulino8a9236f2011-10-14 11:18:09 -0300590 { RUN_STATE_INTERNAL_ERROR, RUN_STATE_FINISH_MIGRATE },
Luiz Capitulino5db9d4d2011-09-05 16:36:31 -0300591
Luiz Capitulino0461d5a2011-09-30 14:45:27 -0300592 { RUN_STATE_IO_ERROR, RUN_STATE_RUNNING },
Luiz Capitulino8a9236f2011-10-14 11:18:09 -0300593 { RUN_STATE_IO_ERROR, RUN_STATE_FINISH_MIGRATE },
Luiz Capitulino5db9d4d2011-09-05 16:36:31 -0300594
Luiz Capitulino0461d5a2011-09-30 14:45:27 -0300595 { RUN_STATE_PAUSED, RUN_STATE_RUNNING },
Luiz Capitulino8a9236f2011-10-14 11:18:09 -0300596 { RUN_STATE_PAUSED, RUN_STATE_FINISH_MIGRATE },
Luiz Capitulino5db9d4d2011-09-05 16:36:31 -0300597
Luiz Capitulino0461d5a2011-09-30 14:45:27 -0300598 { RUN_STATE_POSTMIGRATE, RUN_STATE_RUNNING },
Luiz Capitulino8a9236f2011-10-14 11:18:09 -0300599 { RUN_STATE_POSTMIGRATE, RUN_STATE_FINISH_MIGRATE },
Luiz Capitulino5db9d4d2011-09-05 16:36:31 -0300600
Luiz Capitulino0461d5a2011-09-30 14:45:27 -0300601 { RUN_STATE_PRELAUNCH, RUN_STATE_RUNNING },
Luiz Capitulino8a9236f2011-10-14 11:18:09 -0300602 { RUN_STATE_PRELAUNCH, RUN_STATE_FINISH_MIGRATE },
Luiz Capitulino0461d5a2011-09-30 14:45:27 -0300603 { RUN_STATE_PRELAUNCH, RUN_STATE_INMIGRATE },
Luiz Capitulino5db9d4d2011-09-05 16:36:31 -0300604
Luiz Capitulino0461d5a2011-09-30 14:45:27 -0300605 { RUN_STATE_FINISH_MIGRATE, RUN_STATE_RUNNING },
606 { RUN_STATE_FINISH_MIGRATE, RUN_STATE_POSTMIGRATE },
Luiz Capitulino5db9d4d2011-09-05 16:36:31 -0300607
Luiz Capitulino0461d5a2011-09-30 14:45:27 -0300608 { RUN_STATE_RESTORE_VM, RUN_STATE_RUNNING },
Luiz Capitulino5db9d4d2011-09-05 16:36:31 -0300609
Luiz Capitulino0461d5a2011-09-30 14:45:27 -0300610 { RUN_STATE_RUNNING, RUN_STATE_DEBUG },
611 { RUN_STATE_RUNNING, RUN_STATE_INTERNAL_ERROR },
612 { RUN_STATE_RUNNING, RUN_STATE_IO_ERROR },
613 { RUN_STATE_RUNNING, RUN_STATE_PAUSED },
614 { RUN_STATE_RUNNING, RUN_STATE_FINISH_MIGRATE },
615 { RUN_STATE_RUNNING, RUN_STATE_RESTORE_VM },
616 { RUN_STATE_RUNNING, RUN_STATE_SAVE_VM },
617 { RUN_STATE_RUNNING, RUN_STATE_SHUTDOWN },
618 { RUN_STATE_RUNNING, RUN_STATE_WATCHDOG },
Hu Taoede085b2013-04-26 11:24:40 +0800619 { RUN_STATE_RUNNING, RUN_STATE_GUEST_PANICKED },
Luiz Capitulino5db9d4d2011-09-05 16:36:31 -0300620
Luiz Capitulino0461d5a2011-09-30 14:45:27 -0300621 { RUN_STATE_SAVE_VM, RUN_STATE_RUNNING },
Luiz Capitulino5db9d4d2011-09-05 16:36:31 -0300622
Luiz Capitulino0461d5a2011-09-30 14:45:27 -0300623 { RUN_STATE_SHUTDOWN, RUN_STATE_PAUSED },
Luiz Capitulino8a9236f2011-10-14 11:18:09 -0300624 { RUN_STATE_SHUTDOWN, RUN_STATE_FINISH_MIGRATE },
Luiz Capitulino5db9d4d2011-09-05 16:36:31 -0300625
Luiz Capitulinoad02b962012-04-27 13:33:36 -0300626 { RUN_STATE_DEBUG, RUN_STATE_SUSPENDED },
627 { RUN_STATE_RUNNING, RUN_STATE_SUSPENDED },
628 { RUN_STATE_SUSPENDED, RUN_STATE_RUNNING },
629 { RUN_STATE_SUSPENDED, RUN_STATE_FINISH_MIGRATE },
630
Luiz Capitulino0461d5a2011-09-30 14:45:27 -0300631 { RUN_STATE_WATCHDOG, RUN_STATE_RUNNING },
Luiz Capitulino8a9236f2011-10-14 11:18:09 -0300632 { RUN_STATE_WATCHDOG, RUN_STATE_FINISH_MIGRATE },
Luiz Capitulino5db9d4d2011-09-05 16:36:31 -0300633
Paolo Bonzinidf390762013-11-04 14:30:47 +0100634 { RUN_STATE_GUEST_PANICKED, RUN_STATE_RUNNING },
Hu Taofd2a2e12013-05-20 12:46:20 +0200635 { RUN_STATE_GUEST_PANICKED, RUN_STATE_FINISH_MIGRATE },
Hu Taoede085b2013-04-26 11:24:40 +0800636
Luiz Capitulino0461d5a2011-09-30 14:45:27 -0300637 { RUN_STATE_MAX, RUN_STATE_MAX },
Luiz Capitulino5db9d4d2011-09-05 16:36:31 -0300638};
639
Luiz Capitulino0461d5a2011-09-30 14:45:27 -0300640static bool runstate_valid_transitions[RUN_STATE_MAX][RUN_STATE_MAX];
Luiz Capitulino5db9d4d2011-09-05 16:36:31 -0300641
Luiz Capitulinof5bbfba2011-07-29 15:04:45 -0300642bool runstate_check(RunState state)
643{
644 return current_run_state == state;
645}
646
Juan Quintela5e0f1942014-10-08 11:53:22 +0200647bool runstate_store(char *str, size_t size)
648{
649 const char *state = RunState_lookup[current_run_state];
650 size_t len = strlen(state) + 1;
651
652 if (len > size) {
653 return false;
654 }
655 memcpy(str, state, len);
656 return true;
657}
658
Blue Swirl4fdcac02012-10-28 11:04:47 +0000659static void runstate_init(void)
Luiz Capitulino5db9d4d2011-09-05 16:36:31 -0300660{
661 const RunStateTransition *p;
662
663 memset(&runstate_valid_transitions, 0, sizeof(runstate_valid_transitions));
Luiz Capitulino0461d5a2011-09-30 14:45:27 -0300664 for (p = &runstate_transitions_def[0]; p->from != RUN_STATE_MAX; p++) {
Luiz Capitulino5db9d4d2011-09-05 16:36:31 -0300665 runstate_valid_transitions[p->from][p->to] = true;
666 }
Paolo Bonzini74892d22014-06-05 14:53:58 +0200667
668 qemu_mutex_init(&vmstop_lock);
Luiz Capitulino5db9d4d2011-09-05 16:36:31 -0300669}
670
671/* This function will abort() on invalid state transitions */
Luiz Capitulinof5bbfba2011-07-29 15:04:45 -0300672void runstate_set(RunState new_state)
673{
Luiz Capitulino207c5cd2011-10-13 10:59:07 -0300674 assert(new_state < RUN_STATE_MAX);
675
676 if (!runstate_valid_transitions[current_run_state][new_state]) {
677 fprintf(stderr, "ERROR: invalid runstate transition: '%s' -> '%s'\n",
678 RunState_lookup[current_run_state],
679 RunState_lookup[new_state]);
Luiz Capitulino5db9d4d2011-09-05 16:36:31 -0300680 abort();
681 }
Kazuya Saito7e866002013-03-22 17:26:59 +0900682 trace_runstate_set(new_state);
Luiz Capitulinof5bbfba2011-07-29 15:04:45 -0300683 current_run_state = new_state;
684}
685
Luiz Capitulino13548692011-07-29 15:36:43 -0300686int runstate_is_running(void)
687{
Luiz Capitulino0461d5a2011-09-30 14:45:27 -0300688 return runstate_check(RUN_STATE_RUNNING);
Luiz Capitulino13548692011-07-29 15:36:43 -0300689}
690
Hu Taoede085b2013-04-26 11:24:40 +0800691bool runstate_needs_reset(void)
692{
693 return runstate_check(RUN_STATE_INTERNAL_ERROR) ||
Paolo Bonzinidf390762013-11-04 14:30:47 +0100694 runstate_check(RUN_STATE_SHUTDOWN);
Hu Taoede085b2013-04-26 11:24:40 +0800695}
696
Luiz Capitulino1fa9a5e2011-09-12 17:54:20 -0300697StatusInfo *qmp_query_status(Error **errp)
698{
699 StatusInfo *info = g_malloc0(sizeof(*info));
700
701 info->running = runstate_is_running();
702 info->singlestep = singlestep;
703 info->status = current_run_state;
704
705 return info;
706}
707
Paolo Bonzini74892d22014-06-05 14:53:58 +0200708static bool qemu_vmstop_requested(RunState *r)
709{
710 qemu_mutex_lock(&vmstop_lock);
711 *r = vmstop_requested;
712 vmstop_requested = RUN_STATE_MAX;
713 qemu_mutex_unlock(&vmstop_lock);
714 return *r < RUN_STATE_MAX;
715}
716
717void qemu_system_vmstop_request_prepare(void)
718{
719 qemu_mutex_lock(&vmstop_lock);
720}
721
722void qemu_system_vmstop_request(RunState state)
723{
724 vmstop_requested = state;
725 qemu_mutex_unlock(&vmstop_lock);
726 qemu_notify_event();
727}
728
729void vm_start(void)
730{
731 RunState requested;
732
733 qemu_vmstop_requested(&requested);
734 if (runstate_is_running() && requested == RUN_STATE_MAX) {
735 return;
736 }
737
738 /* Ensure that a STOP/RESUME pair of events is emitted if a
739 * vmstop request was pending. The BLOCK_IO_ERROR event, for
740 * example, according to documentation is always followed by
741 * the STOP event.
742 */
743 if (runstate_is_running()) {
Wenchao Xia591c48f2014-06-18 08:43:37 +0200744 qapi_event_send_stop(&error_abort);
Paolo Bonzini74892d22014-06-05 14:53:58 +0200745 } else {
746 cpu_enable_ticks();
747 runstate_set(RUN_STATE_RUNNING);
748 vm_state_notify(1, RUN_STATE_RUNNING);
749 resume_all_vcpus();
750 }
751
Wenchao Xia591c48f2014-06-18 08:43:37 +0200752 qapi_event_send_resume(&error_abort);
Paolo Bonzini74892d22014-06-05 14:53:58 +0200753}
754
755
Luiz Capitulinof5bbfba2011-07-29 15:04:45 -0300756/***********************************************************/
Paolo Bonzini8f0056b2010-01-13 14:05:34 +0100757/* real time host monotonic timer */
bellard09b26c52006-04-12 21:09:08 +0000758
Pavel Dovgalyuk2ed1ebc2015-02-27 16:11:02 +0300759static time_t qemu_time(void)
760{
761 return qemu_clock_get_ms(QEMU_CLOCK_HOST) / 1000;
762}
763
bellardc4b1fcc2004-03-14 21:44:30 +0000764/***********************************************************/
balrogf6503052008-02-17 11:42:19 +0000765/* host time/date access */
766void qemu_get_timedate(struct tm *tm, int offset)
767{
Pavel Dovgalyuk2ed1ebc2015-02-27 16:11:02 +0300768 time_t ti = qemu_time();
balrogf6503052008-02-17 11:42:19 +0000769
balrogf6503052008-02-17 11:42:19 +0000770 ti += offset;
771 if (rtc_date_offset == -1) {
772 if (rtc_utc)
Stefan Weileb7ff6f2013-01-07 23:08:13 +0100773 gmtime_r(&ti, tm);
balrogf6503052008-02-17 11:42:19 +0000774 else
Stefan Weileb7ff6f2013-01-07 23:08:13 +0100775 localtime_r(&ti, tm);
balrogf6503052008-02-17 11:42:19 +0000776 } else {
777 ti -= rtc_date_offset;
Stefan Weileb7ff6f2013-01-07 23:08:13 +0100778 gmtime_r(&ti, tm);
balrogf6503052008-02-17 11:42:19 +0000779 }
balrogf6503052008-02-17 11:42:19 +0000780}
781
782int qemu_timedate_diff(struct tm *tm)
783{
784 time_t seconds;
785
786 if (rtc_date_offset == -1)
787 if (rtc_utc)
788 seconds = mktimegm(tm);
Gleb Natapovf54c5562011-11-06 18:00:22 +0200789 else {
790 struct tm tmp = *tm;
791 tmp.tm_isdst = -1; /* use timezone to figure it out */
792 seconds = mktime(&tmp);
793 }
balrogf6503052008-02-17 11:42:19 +0000794 else
795 seconds = mktimegm(tm) + rtc_date_offset;
796
Pavel Dovgalyuk2ed1ebc2015-02-27 16:11:02 +0300797 return seconds - qemu_time();
balrogf6503052008-02-17 11:42:19 +0000798}
799
Jan Kiszka1ed2fc12009-09-15 13:36:04 +0200800static void configure_rtc_date_offset(const char *startdate, int legacy)
801{
802 time_t rtc_start_date;
803 struct tm tm;
804
805 if (!strcmp(startdate, "now") && legacy) {
806 rtc_date_offset = -1;
807 } else {
808 if (sscanf(startdate, "%d-%d-%dT%d:%d:%d",
809 &tm.tm_year,
810 &tm.tm_mon,
811 &tm.tm_mday,
812 &tm.tm_hour,
813 &tm.tm_min,
814 &tm.tm_sec) == 6) {
815 /* OK */
816 } else if (sscanf(startdate, "%d-%d-%d",
817 &tm.tm_year,
818 &tm.tm_mon,
819 &tm.tm_mday) == 3) {
820 tm.tm_hour = 0;
821 tm.tm_min = 0;
822 tm.tm_sec = 0;
823 } else {
824 goto date_fail;
825 }
826 tm.tm_year -= 1900;
827 tm.tm_mon--;
828 rtc_start_date = mktimegm(&tm);
829 if (rtc_start_date == -1) {
830 date_fail:
831 fprintf(stderr, "Invalid date format. Valid formats are:\n"
832 "'2006-06-17T16:01:21' or '2006-06-17'\n");
833 exit(1);
834 }
Pavel Dovgalyuk2ed1ebc2015-02-27 16:11:02 +0300835 rtc_date_offset = qemu_time() - rtc_start_date;
Jan Kiszka1ed2fc12009-09-15 13:36:04 +0200836 }
837}
838
839static void configure_rtc(QemuOpts *opts)
840{
841 const char *value;
842
843 value = qemu_opt_get(opts, "base");
844 if (value) {
845 if (!strcmp(value, "utc")) {
846 rtc_utc = 1;
847 } else if (!strcmp(value, "localtime")) {
848 rtc_utc = 0;
849 } else {
850 configure_rtc_date_offset(value, 0);
851 }
852 }
Jan Kiszka68752042009-09-15 13:36:04 +0200853 value = qemu_opt_get(opts, "clock");
854 if (value) {
855 if (!strcmp(value, "host")) {
Alex Bligh884f17c2013-08-21 16:03:04 +0100856 rtc_clock = QEMU_CLOCK_HOST;
Paolo Bonzini78808142012-03-30 10:31:21 +0000857 } else if (!strcmp(value, "rt")) {
Alex Bligh884f17c2013-08-21 16:03:04 +0100858 rtc_clock = QEMU_CLOCK_REALTIME;
Jan Kiszka68752042009-09-15 13:36:04 +0200859 } else if (!strcmp(value, "vm")) {
Alex Bligh884f17c2013-08-21 16:03:04 +0100860 rtc_clock = QEMU_CLOCK_VIRTUAL;
Jan Kiszka68752042009-09-15 13:36:04 +0200861 } else {
862 fprintf(stderr, "qemu: invalid option value '%s'\n", value);
863 exit(1);
864 }
865 }
Jan Kiszka1ed2fc12009-09-15 13:36:04 +0200866 value = qemu_opt_get(opts, "driftfix");
867 if (value) {
Blue Swirl7e4c0332010-03-27 21:33:46 +0000868 if (!strcmp(value, "slew")) {
Jan Kiszka433acf02012-01-23 20:15:12 +0100869 static GlobalProperty slew_lost_ticks[] = {
870 {
871 .driver = "mc146818rtc",
872 .property = "lost_tick_policy",
873 .value = "slew",
874 },
875 { /* end of list */ }
876 };
877
878 qdev_prop_register_global_list(slew_lost_ticks);
Blue Swirl7e4c0332010-03-27 21:33:46 +0000879 } else if (!strcmp(value, "none")) {
Jan Kiszka433acf02012-01-23 20:15:12 +0100880 /* discard is default */
Jan Kiszka1ed2fc12009-09-15 13:36:04 +0200881 } else {
882 fprintf(stderr, "qemu: invalid option value '%s'\n", value);
883 exit(1);
884 }
885 }
Jan Kiszka1ed2fc12009-09-15 13:36:04 +0200886}
887
balrog1ae26a12008-09-28 23:19:47 +0000888/***********************************************************/
889/* Bluetooth support */
890static int nb_hcis;
891static int cur_hci;
892static struct HCIInfo *hci_table[MAX_NICS];
balrogdc72ac12008-11-09 00:04:26 +0000893
balrog1ae26a12008-09-28 23:19:47 +0000894struct HCIInfo *qemu_next_hci(void)
895{
896 if (cur_hci == nb_hcis)
897 return &null_hci;
898
899 return hci_table[cur_hci++];
900}
901
balrogdc72ac12008-11-09 00:04:26 +0000902static int bt_hci_parse(const char *str)
903{
904 struct HCIInfo *hci;
Anthony Liguoric227f092009-10-01 16:12:16 -0500905 bdaddr_t bdaddr;
balrogdc72ac12008-11-09 00:04:26 +0000906
907 if (nb_hcis >= MAX_NICS) {
908 fprintf(stderr, "qemu: Too many bluetooth HCIs (max %i).\n", MAX_NICS);
909 return -1;
910 }
911
912 hci = hci_init(str);
913 if (!hci)
914 return -1;
915
916 bdaddr.b[0] = 0x52;
917 bdaddr.b[1] = 0x54;
918 bdaddr.b[2] = 0x00;
919 bdaddr.b[3] = 0x12;
920 bdaddr.b[4] = 0x34;
921 bdaddr.b[5] = 0x56 + nb_hcis;
922 hci->bdaddr_set(hci, bdaddr.b);
923
924 hci_table[nb_hcis++] = hci;
925
926 return 0;
927}
928
929static void bt_vhci_add(int vlan_id)
930{
931 struct bt_scatternet_s *vlan = qemu_find_bt_vlan(vlan_id);
932
933 if (!vlan->slave)
934 fprintf(stderr, "qemu: warning: adding a VHCI to "
935 "an empty scatternet %i\n", vlan_id);
936
937 bt_vhci_init(bt_new_hci(vlan));
938}
939
940static struct bt_device_s *bt_device_add(const char *opt)
941{
942 struct bt_scatternet_s *vlan;
943 int vlan_id = 0;
944 char *endp = strstr(opt, ",vlan=");
945 int len = (endp ? endp - opt : strlen(opt)) + 1;
946 char devname[10];
947
948 pstrcpy(devname, MIN(sizeof(devname), len), opt);
949
950 if (endp) {
951 vlan_id = strtol(endp + 6, &endp, 0);
952 if (*endp) {
953 fprintf(stderr, "qemu: unrecognised bluetooth vlan Id\n");
954 return 0;
955 }
956 }
957
958 vlan = qemu_find_bt_vlan(vlan_id);
959
960 if (!vlan->slave)
961 fprintf(stderr, "qemu: warning: adding a slave device to "
962 "an empty scatternet %i\n", vlan_id);
963
964 if (!strcmp(devname, "keyboard"))
965 return bt_keyboard_init(vlan);
966
967 fprintf(stderr, "qemu: unsupported bluetooth device `%s'\n", devname);
968 return 0;
969}
970
971static int bt_parse(const char *opt)
972{
973 const char *endp, *p;
974 int vlan;
975
976 if (strstart(opt, "hci", &endp)) {
977 if (!*endp || *endp == ',') {
978 if (*endp)
979 if (!strstart(endp, ",vlan=", 0))
980 opt = endp + 1;
981
982 return bt_hci_parse(opt);
983 }
984 } else if (strstart(opt, "vhci", &endp)) {
985 if (!*endp || *endp == ',') {
986 if (*endp) {
987 if (strstart(endp, ",vlan=", &p)) {
988 vlan = strtol(p, (char **) &endp, 0);
989 if (*endp) {
990 fprintf(stderr, "qemu: bad scatternet '%s'\n", p);
991 return 1;
992 }
993 } else {
994 fprintf(stderr, "qemu: bad parameter '%s'\n", endp + 1);
995 return 1;
996 }
997 } else
998 vlan = 0;
999
1000 bt_vhci_add(vlan);
1001 return 0;
1002 }
1003 } else if (strstart(opt, "device:", &endp))
1004 return !bt_device_add(endp);
1005
1006 fprintf(stderr, "qemu: bad bluetooth parameter '%s'\n", opt);
1007 return 1;
1008}
1009
Markus Armbruster28d0de72015-03-13 13:35:14 +01001010static int parse_sandbox(void *opaque, QemuOpts *opts, Error **errp)
Eduardo Otubo7d76ad42012-08-14 18:44:08 -03001011{
1012 /* FIXME: change this to true for 1.3 */
1013 if (qemu_opt_get_bool(opts, "enable", false)) {
1014#ifdef CONFIG_SECCOMP
1015 if (seccomp_start() < 0) {
Markus Armbruster14594072015-02-27 09:47:12 +01001016 error_report("failed to install seccomp syscall filter "
1017 "in the kernel");
Eduardo Otubo7d76ad42012-08-14 18:44:08 -03001018 return -1;
1019 }
1020#else
Markus Armbruster14594072015-02-27 09:47:12 +01001021 error_report("sandboxing request but seccomp is not compiled "
1022 "into this build");
Eduardo Otubo7d76ad42012-08-14 18:44:08 -03001023 return -1;
1024#endif
1025 }
1026
1027 return 0;
1028}
1029
Markus Armbruster28d0de72015-03-13 13:35:14 +01001030static int parse_name(void *opaque, QemuOpts *opts, Error **errp)
Dr. David Alan Gilbert5d12f962014-01-30 10:20:30 +00001031{
1032 const char *proc_name;
1033
Dr. David Alan Gilbert8f480de2014-01-30 10:20:31 +00001034 if (qemu_opt_get(opts, "debug-threads")) {
1035 qemu_thread_naming(qemu_opt_get_bool(opts, "debug-threads", false));
1036 }
Dr. David Alan Gilbert5d12f962014-01-30 10:20:30 +00001037 qemu_name = qemu_opt_get(opts, "guest");
1038
1039 proc_name = qemu_opt_get(opts, "process");
1040 if (proc_name) {
1041 os_set_proc_name(proc_name);
1042 }
Dr. David Alan Gilbert5b9d3132014-05-06 12:15:55 +01001043
1044 return 0;
Dr. David Alan Gilbert5d12f962014-01-30 10:20:30 +00001045}
1046
Marcel Apfelbaumf8b6f8e2015-01-06 15:29:12 +02001047bool defaults_enabled(void)
1048{
1049 return has_defaults;
1050}
1051
Marcel Apfelbaumde77a242015-01-06 15:29:14 +02001052bool usb_enabled(void)
zhlcindy@gmail.com094b2872012-09-02 19:25:28 +00001053{
Marcel Apfelbaumde77a242015-01-06 15:29:14 +02001054 return machine_usb(current_machine);
zhlcindy@gmail.com094b2872012-09-02 19:25:28 +00001055}
1056
Corey Bryant587ed6b2012-10-18 15:19:34 -04001057#ifndef _WIN32
Markus Armbruster28d0de72015-03-13 13:35:14 +01001058static int parse_add_fd(void *opaque, QemuOpts *opts, Error **errp)
Corey Bryant587ed6b2012-10-18 15:19:34 -04001059{
1060 int fd, dupfd, flags;
1061 int64_t fdset_id;
1062 const char *fd_opaque = NULL;
Paolo Bonzini636a30a2015-03-15 10:16:28 +01001063 AddfdInfo *fdinfo;
Corey Bryant587ed6b2012-10-18 15:19:34 -04001064
1065 fd = qemu_opt_get_number(opts, "fd", -1);
1066 fdset_id = qemu_opt_get_number(opts, "set", -1);
1067 fd_opaque = qemu_opt_get(opts, "opaque");
1068
1069 if (fd < 0) {
Markus Armbruster14594072015-02-27 09:47:12 +01001070 error_report("fd option is required and must be non-negative");
Corey Bryant587ed6b2012-10-18 15:19:34 -04001071 return -1;
1072 }
1073
1074 if (fd <= STDERR_FILENO) {
Markus Armbruster14594072015-02-27 09:47:12 +01001075 error_report("fd cannot be a standard I/O stream");
Corey Bryant587ed6b2012-10-18 15:19:34 -04001076 return -1;
1077 }
1078
1079 /*
1080 * All fds inherited across exec() necessarily have FD_CLOEXEC
1081 * clear, while qemu sets FD_CLOEXEC on all other fds used internally.
1082 */
1083 flags = fcntl(fd, F_GETFD);
1084 if (flags == -1 || (flags & FD_CLOEXEC)) {
Markus Armbruster14594072015-02-27 09:47:12 +01001085 error_report("fd is not valid or already in use");
Corey Bryant587ed6b2012-10-18 15:19:34 -04001086 return -1;
1087 }
1088
1089 if (fdset_id < 0) {
Markus Armbruster14594072015-02-27 09:47:12 +01001090 error_report("set option is required and must be non-negative");
Corey Bryant587ed6b2012-10-18 15:19:34 -04001091 return -1;
1092 }
1093
1094#ifdef F_DUPFD_CLOEXEC
1095 dupfd = fcntl(fd, F_DUPFD_CLOEXEC, 0);
1096#else
1097 dupfd = dup(fd);
1098 if (dupfd != -1) {
1099 qemu_set_cloexec(dupfd);
1100 }
1101#endif
1102 if (dupfd == -1) {
Markus Armbruster14594072015-02-27 09:47:12 +01001103 error_report("Error duplicating fd: %s", strerror(errno));
Corey Bryant587ed6b2012-10-18 15:19:34 -04001104 return -1;
1105 }
1106
1107 /* add the duplicate fd, and optionally the opaque string, to the fd set */
Paolo Bonzini636a30a2015-03-15 10:16:28 +01001108 fdinfo = monitor_fdset_add_fd(dupfd, true, fdset_id, !!fd_opaque, fd_opaque,
1109 &error_abort);
1110 g_free(fdinfo);
Corey Bryant587ed6b2012-10-18 15:19:34 -04001111
1112 return 0;
1113}
1114
Markus Armbruster28d0de72015-03-13 13:35:14 +01001115static int cleanup_add_fd(void *opaque, QemuOpts *opts, Error **errp)
Corey Bryant587ed6b2012-10-18 15:19:34 -04001116{
1117 int fd;
1118
1119 fd = qemu_opt_get_number(opts, "fd", -1);
1120 close(fd);
1121
1122 return 0;
1123}
1124#endif
1125
balrog1ae26a12008-09-28 23:19:47 +00001126/***********************************************************/
1127/* QEMU Block devices */
1128
Markus Armbruster2292dda2011-01-28 11:21:41 +01001129#define HD_OPTS "media=disk"
1130#define CDROM_OPTS "media=cdrom"
1131#define FD_OPTS ""
1132#define PFLASH_OPTS ""
1133#define MTD_OPTS ""
1134#define SD_OPTS ""
thse4bcb142007-12-02 04:51:10 +00001135
Markus Armbruster28d0de72015-03-13 13:35:14 +01001136static int drive_init_func(void *opaque, QemuOpts *opts, Error **errp)
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +02001137{
Christian Borntraeger2d0d2832012-11-20 15:30:34 +01001138 BlockInterfaceType *block_default_type = opaque;
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +02001139
Markus Armbruster60e19e02014-06-06 14:50:58 +02001140 return drive_new(opts, *block_default_type) == NULL;
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +02001141}
1142
Markus Armbruster28d0de72015-03-13 13:35:14 +01001143static int drive_enable_snapshot(void *opaque, QemuOpts *opts, Error **errp)
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +02001144{
Gonglei28de2f82014-08-11 21:00:57 +08001145 if (qemu_opt_get(opts, "snapshot") == NULL) {
Markus Armbrusterf43e47d2015-02-12 17:52:20 +01001146 qemu_opt_set(opts, "snapshot", "on", &error_abort);
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +02001147 }
1148 return 0;
1149}
1150
Christian Borntraeger3c42ea62012-11-22 21:02:55 +01001151static void default_drive(int enable, int snapshot, BlockInterfaceType type,
1152 int index, const char *optstr)
Markus Armbruster4e5d9b52011-01-28 11:21:45 +01001153{
1154 QemuOpts *opts;
John Snowa66c9dc2014-10-01 14:19:24 -04001155 DriveInfo *dinfo;
Markus Armbruster4e5d9b52011-01-28 11:21:45 +01001156
Markus Armbruster4e5d9b52011-01-28 11:21:45 +01001157 if (!enable || drive_get_by_index(type, index)) {
1158 return;
1159 }
1160
1161 opts = drive_add(type, index, NULL, optstr);
1162 if (snapshot) {
Markus Armbruster28d0de72015-03-13 13:35:14 +01001163 drive_enable_snapshot(NULL, opts, NULL);
Markus Armbruster4e5d9b52011-01-28 11:21:45 +01001164 }
John Snowa66c9dc2014-10-01 14:19:24 -04001165
1166 dinfo = drive_new(opts, type);
1167 if (!dinfo) {
Markus Armbruster4e5d9b52011-01-28 11:21:45 +01001168 exit(1);
1169 }
John Snowa66c9dc2014-10-01 14:19:24 -04001170 dinfo->is_default = true;
1171
Markus Armbruster4e5d9b52011-01-28 11:21:45 +01001172}
1173
Michael Tokarev12b7f572013-06-24 15:06:52 +04001174static QemuOptsList qemu_smp_opts = {
1175 .name = "smp-opts",
1176 .implied_opt_name = "cpus",
1177 .merge_lists = true,
1178 .head = QTAILQ_HEAD_INITIALIZER(qemu_smp_opts.head),
1179 .desc = {
1180 {
1181 .name = "cpus",
1182 .type = QEMU_OPT_NUMBER,
1183 }, {
1184 .name = "sockets",
1185 .type = QEMU_OPT_NUMBER,
1186 }, {
1187 .name = "cores",
1188 .type = QEMU_OPT_NUMBER,
1189 }, {
1190 .name = "threads",
1191 .type = QEMU_OPT_NUMBER,
1192 }, {
1193 .name = "maxcpus",
1194 .type = QEMU_OPT_NUMBER,
1195 },
1196 { /*End of list */ }
1197 },
1198};
1199
1200static void smp_parse(QemuOpts *opts)
Andre Przywaradc6b1c02009-08-19 15:42:40 +02001201{
Michael Tokarev12b7f572013-06-24 15:06:52 +04001202 if (opts) {
Andre Przywaradc6b1c02009-08-19 15:42:40 +02001203
Michael Tokarev12b7f572013-06-24 15:06:52 +04001204 unsigned cpus = qemu_opt_get_number(opts, "cpus", 0);
1205 unsigned sockets = qemu_opt_get_number(opts, "sockets", 0);
1206 unsigned cores = qemu_opt_get_number(opts, "cores", 0);
1207 unsigned threads = qemu_opt_get_number(opts, "threads", 0);
Andre Przywaradc6b1c02009-08-19 15:42:40 +02001208
Michael Tokarev12b7f572013-06-24 15:06:52 +04001209 /* compute missing values, prefer sockets over cores over threads */
1210 if (cpus == 0 || sockets == 0) {
1211 sockets = sockets > 0 ? sockets : 1;
1212 cores = cores > 0 ? cores : 1;
Andre Przywaradc6b1c02009-08-19 15:42:40 +02001213 threads = threads > 0 ? threads : 1;
Michael Tokarev12b7f572013-06-24 15:06:52 +04001214 if (cpus == 0) {
1215 cpus = cores * threads * sockets;
1216 }
Eduardo Habkost719cac12014-12-19 00:59:45 -02001217 } else if (cores == 0) {
1218 threads = threads > 0 ? threads : 1;
1219 cores = cpus / (sockets * threads);
Eduardo Habkostec2cbbd2014-12-19 00:59:47 -02001220 } else if (threads == 0) {
Eduardo Habkost719cac12014-12-19 00:59:45 -02001221 threads = cpus / (cores * sockets);
Eduardo Habkostec2cbbd2014-12-19 00:59:47 -02001222 } else if (sockets * cores * threads < cpus) {
1223 fprintf(stderr, "cpu topology: error: "
1224 "sockets (%u) * cores (%u) * threads (%u) < "
1225 "smp_cpus (%u)\n",
1226 sockets, cores, threads, cpus);
1227 exit(1);
Andre Przywaradc6b1c02009-08-19 15:42:40 +02001228 }
Michael Tokarev12b7f572013-06-24 15:06:52 +04001229
Thomas Hutha32ef3b2015-07-22 15:59:50 +02001230 max_cpus = qemu_opt_get_number(opts, "maxcpus", cpus);
1231 if (sockets * cores * threads > max_cpus) {
1232 fprintf(stderr, "cpu topology: error: "
1233 "sockets (%u) * cores (%u) * threads (%u) > maxcpus (%u)\n",
1234 sockets, cores, threads, max_cpus);
1235 exit(1);
1236 }
Michael Tokarev12b7f572013-06-24 15:06:52 +04001237
1238 smp_cpus = cpus;
1239 smp_cores = cores > 0 ? cores : 1;
1240 smp_threads = threads > 0 ? threads : 1;
1241
Andre Przywaradc6b1c02009-08-19 15:42:40 +02001242 }
Michael Tokarev12b7f572013-06-24 15:06:52 +04001243
1244 if (max_cpus == 0) {
Andre Przywaradc6b1c02009-08-19 15:42:40 +02001245 max_cpus = smp_cpus;
Michael Tokarev12b7f572013-06-24 15:06:52 +04001246 }
1247
Eduardo Habkostaf67ee92014-03-18 16:29:24 -03001248 if (max_cpus > MAX_CPUMASK_BITS) {
Michael Tokarev12b7f572013-06-24 15:06:52 +04001249 fprintf(stderr, "Unsupported number of maxcpus\n");
1250 exit(1);
1251 }
1252 if (max_cpus < smp_cpus) {
1253 fprintf(stderr, "maxcpus must be equal to or greater than smp\n");
1254 exit(1);
1255 }
1256
Andre Przywaradc6b1c02009-08-19 15:42:40 +02001257}
1258
zhanghailiang28d16f32014-09-23 18:42:24 +08001259static void realtime_init(void)
Satoru Moriya888a6bc2013-04-19 16:42:06 +02001260{
Satoru Moriya888a6bc2013-04-19 16:42:06 +02001261 if (enable_mlock) {
1262 if (os_mlock() < 0) {
1263 fprintf(stderr, "qemu: locking memory failed\n");
1264 exit(1);
1265 }
1266 }
1267}
1268
Seiji Aguchi5e2ac512013-07-03 23:02:46 -04001269
1270static void configure_msg(QemuOpts *opts)
1271{
1272 enable_timestamp_msg = qemu_opt_get_bool(opts, "timestamp", true);
1273}
1274
bellard330d0412003-07-26 18:11:40 +00001275/***********************************************************/
Leon Alraecfe67ce2015-06-19 14:17:45 +01001276/* Semihosting */
1277
1278typedef struct SemihostingConfig {
1279 bool enabled;
1280 SemihostingTarget target;
Leon Alraea59d31a2015-06-19 14:17:45 +01001281 const char **argv;
1282 int argc;
1283 const char *cmdline; /* concatenated argv */
Leon Alraecfe67ce2015-06-19 14:17:45 +01001284} SemihostingConfig;
1285
1286static SemihostingConfig semihosting;
1287
1288bool semihosting_enabled(void)
1289{
1290 return semihosting.enabled;
1291}
1292
1293SemihostingTarget semihosting_get_target(void)
1294{
1295 return semihosting.target;
1296}
1297
Leon Alraea59d31a2015-06-19 14:17:45 +01001298const char *semihosting_get_arg(int i)
1299{
1300 if (i >= semihosting.argc) {
1301 return NULL;
1302 }
1303 return semihosting.argv[i];
1304}
1305
1306int semihosting_get_argc(void)
1307{
1308 return semihosting.argc;
1309}
1310
1311const char *semihosting_get_cmdline(void)
1312{
1313 if (semihosting.cmdline == NULL && semihosting.argc > 0) {
1314 semihosting.cmdline = g_strjoinv(" ", (gchar **)semihosting.argv);
1315 }
1316 return semihosting.cmdline;
1317}
1318
1319static int add_semihosting_arg(void *opaque,
1320 const char *name, const char *val,
1321 Error **errp)
1322{
1323 SemihostingConfig *s = opaque;
1324 if (strcmp(name, "arg") == 0) {
1325 s->argc++;
1326 /* one extra element as g_strjoinv() expects NULL-terminated array */
1327 s->argv = g_realloc(s->argv, (s->argc + 1) * sizeof(void *));
1328 s->argv[s->argc - 1] = val;
1329 s->argv[s->argc] = NULL;
1330 }
1331 return 0;
1332}
1333
1334/* Use strings passed via -kernel/-append to initialize semihosting.argv[] */
1335static inline void semihosting_arg_fallback(const char *file, const char *cmd)
1336{
1337 char *cmd_token;
1338
1339 /* argv[0] */
1340 add_semihosting_arg(&semihosting, "arg", file, NULL);
1341
1342 /* split -append and initialize argv[1..n] */
1343 cmd_token = strtok(g_strdup(cmd), " ");
1344 while (cmd_token) {
1345 add_semihosting_arg(&semihosting, "arg", cmd_token, NULL);
1346 cmd_token = strtok(NULL, " ");
1347 }
1348}
1349
Tiejun Chen79814172015-07-15 13:37:45 +08001350/* Now we still need this for compatibility with XEN. */
1351bool has_igd_gfx_passthru;
1352static void igd_gfx_passthru(void)
1353{
1354 has_igd_gfx_passthru = current_machine->igd_gfx_passthru;
1355}
1356
Leon Alraecfe67ce2015-06-19 14:17:45 +01001357/***********************************************************/
bellarda594cfb2005-11-06 16:13:29 +00001358/* USB devices */
1359
Markus Armbrusterfb080002010-05-28 15:38:44 +02001360static int usb_device_add(const char *devname)
bellarda594cfb2005-11-06 16:13:29 +00001361{
Gerd Hoffmanna5d2f722009-08-31 14:24:00 +02001362 USBDevice *dev = NULL;
Miroslav Rezanina615fe4d2013-09-03 11:23:09 +02001363#ifndef CONFIG_LINUX
1364 const char *p;
1365#endif
bellarda594cfb2005-11-06 16:13:29 +00001366
Marcel Apfelbaumde77a242015-01-06 15:29:14 +02001367 if (!usb_enabled()) {
bellarda594cfb2005-11-06 16:13:29 +00001368 return -1;
zhlcindy@gmail.com094b2872012-09-02 19:25:28 +00001369 }
bellarda594cfb2005-11-06 16:13:29 +00001370
Gerd Hoffmann0958b4c2009-10-26 15:56:45 +01001371 /* drivers with .usbdevice_name entry in USBDeviceInfo */
1372 dev = usbdevice_create(devname);
1373 if (dev)
1374 goto done;
1375
Gerd Hoffmanna5d2f722009-08-31 14:24:00 +02001376 /* the other ones */
Gerd Hoffmanne447fc62011-06-01 14:41:59 +02001377#ifndef CONFIG_LINUX
1378 /* only the linux version is qdev-ified, usb-bsd still needs this */
bellarda594cfb2005-11-06 16:13:29 +00001379 if (strstart(devname, "host:", &p)) {
Jan Kiszka37417152012-02-27 15:18:47 +01001380 dev = usb_host_device_open(usb_bus_find(-1), p);
bellarda594cfb2005-11-06 16:13:29 +00001381 }
Miroslav Rezanina615fe4d2013-09-03 11:23:09 +02001382#endif
pbrook0d92ed32006-05-21 16:30:15 +00001383 if (!dev)
1384 return -1;
1385
Gerd Hoffmanna5d2f722009-08-31 14:24:00 +02001386done:
bellarda594cfb2005-11-06 16:13:29 +00001387 return 0;
1388}
1389
aliguori1f3870a2008-08-21 19:27:48 +00001390static int usb_device_del(const char *devname)
1391{
1392 int bus_num, addr;
1393 const char *p;
1394
Gerd Hoffmann1a3973b2012-11-30 13:02:47 +01001395 if (strstart(devname, "host:", &p)) {
1396 return -1;
1397 }
aliguori5d0c5752008-09-14 01:07:41 +00001398
Marcel Apfelbaumde77a242015-01-06 15:29:14 +02001399 if (!usb_enabled()) {
aliguori1f3870a2008-08-21 19:27:48 +00001400 return -1;
zhlcindy@gmail.com094b2872012-09-02 19:25:28 +00001401 }
aliguori1f3870a2008-08-21 19:27:48 +00001402
1403 p = strchr(devname, '.');
1404 if (!p)
1405 return -1;
1406 bus_num = strtoul(devname, NULL, 0);
1407 addr = strtoul(p + 1, NULL, 0);
1408
Gerd Hoffmanna5d2f722009-08-31 14:24:00 +02001409 return usb_device_delete_addr(bus_num, addr);
aliguori1f3870a2008-08-21 19:27:48 +00001410}
1411
Gerd Hoffmannbd3c9482009-07-15 13:59:26 +02001412static int usb_parse(const char *cmdline)
1413{
Scott Tsai59d1c1c2009-12-23 04:30:18 +08001414 int r;
Markus Armbrusterfb080002010-05-28 15:38:44 +02001415 r = usb_device_add(cmdline);
Scott Tsai59d1c1c2009-12-23 04:30:18 +08001416 if (r < 0) {
1417 fprintf(stderr, "qemu: could not add USB device '%s'\n", cmdline);
1418 }
1419 return r;
Gerd Hoffmannbd3c9482009-07-15 13:59:26 +02001420}
1421
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001422void hmp_usb_add(Monitor *mon, const QDict *qdict)
bellarda594cfb2005-11-06 16:13:29 +00001423{
Scott Tsai59d1c1c2009-12-23 04:30:18 +08001424 const char *devname = qdict_get_str(qdict, "devname");
Markus Armbrusterfb080002010-05-28 15:38:44 +02001425 if (usb_device_add(devname) < 0) {
Markus Armbruster1ecda022010-02-18 17:25:24 +01001426 error_report("could not add USB device '%s'", devname);
Scott Tsai59d1c1c2009-12-23 04:30:18 +08001427 }
bellarda594cfb2005-11-06 16:13:29 +00001428}
1429
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001430void hmp_usb_del(Monitor *mon, const QDict *qdict)
bellarda594cfb2005-11-06 16:13:29 +00001431{
Scott Tsai59d1c1c2009-12-23 04:30:18 +08001432 const char *devname = qdict_get_str(qdict, "devname");
1433 if (usb_device_del(devname) < 0) {
Markus Armbruster1ecda022010-02-18 17:25:24 +01001434 error_report("could not delete USB device '%s'", devname);
Scott Tsai59d1c1c2009-12-23 04:30:18 +08001435 }
bellarda594cfb2005-11-06 16:13:29 +00001436}
1437
bellardf7cce892004-12-08 22:21:25 +00001438/***********************************************************/
bellardcc1daa42005-06-05 14:49:17 +00001439/* machine registration */
1440
Marcel Apfelbaum0056ae22014-03-05 19:30:47 +02001441MachineState *current_machine;
bellardcc1daa42005-06-05 14:49:17 +00001442
Marcel Apfelbaum0056ae22014-03-05 19:30:47 +02001443static MachineClass *find_machine(const char *name)
bellardcc1daa42005-06-05 14:49:17 +00001444{
Marcel Apfelbaum261747f2014-03-05 19:30:46 +02001445 GSList *el, *machines = object_class_get_list(TYPE_MACHINE, false);
Marcel Apfelbaum0056ae22014-03-05 19:30:47 +02001446 MachineClass *mc = NULL;
bellardcc1daa42005-06-05 14:49:17 +00001447
Marcel Apfelbaum261747f2014-03-05 19:30:46 +02001448 for (el = machines; el; el = el->next) {
Marcel Apfelbaum0056ae22014-03-05 19:30:47 +02001449 MachineClass *temp = el->data;
Marcel Apfelbaum261747f2014-03-05 19:30:46 +02001450
Marcel Apfelbaum958db902014-04-09 20:34:53 +03001451 if (!strcmp(temp->name, name)) {
Marcel Apfelbaum0056ae22014-03-05 19:30:47 +02001452 mc = temp;
Marcel Apfelbaum261747f2014-03-05 19:30:46 +02001453 break;
1454 }
Marcel Apfelbaum958db902014-04-09 20:34:53 +03001455 if (temp->alias &&
1456 !strcmp(temp->alias, name)) {
Marcel Apfelbaum0056ae22014-03-05 19:30:47 +02001457 mc = temp;
Marcel Apfelbaum261747f2014-03-05 19:30:46 +02001458 break;
1459 }
bellardcc1daa42005-06-05 14:49:17 +00001460 }
Marcel Apfelbaum261747f2014-03-05 19:30:46 +02001461
1462 g_slist_free(machines);
Marcel Apfelbaum0056ae22014-03-05 19:30:47 +02001463 return mc;
bellardcc1daa42005-06-05 14:49:17 +00001464}
1465
Marcel Apfelbaum0056ae22014-03-05 19:30:47 +02001466MachineClass *find_default_machine(void)
Anthony Liguori0c257432009-05-21 20:41:01 -05001467{
Marcel Apfelbaum261747f2014-03-05 19:30:46 +02001468 GSList *el, *machines = object_class_get_list(TYPE_MACHINE, false);
Marcel Apfelbaum0056ae22014-03-05 19:30:47 +02001469 MachineClass *mc = NULL;
Anthony Liguori0c257432009-05-21 20:41:01 -05001470
Marcel Apfelbaum261747f2014-03-05 19:30:46 +02001471 for (el = machines; el; el = el->next) {
Marcel Apfelbaum0056ae22014-03-05 19:30:47 +02001472 MachineClass *temp = el->data;
Marcel Apfelbaum261747f2014-03-05 19:30:46 +02001473
Marcel Apfelbaum958db902014-04-09 20:34:53 +03001474 if (temp->is_default) {
Marcel Apfelbaum0056ae22014-03-05 19:30:47 +02001475 mc = temp;
Marcel Apfelbaum261747f2014-03-05 19:30:46 +02001476 break;
Anthony Liguori0c257432009-05-21 20:41:01 -05001477 }
1478 }
Marcel Apfelbaum261747f2014-03-05 19:30:46 +02001479
1480 g_slist_free(machines);
Marcel Apfelbaum0056ae22014-03-05 19:30:47 +02001481 return mc;
Anthony Liguori0c257432009-05-21 20:41:01 -05001482}
1483
Anthony Liguori01d3c802012-08-10 11:04:11 -05001484MachineInfoList *qmp_query_machines(Error **errp)
1485{
Marcel Apfelbaum261747f2014-03-05 19:30:46 +02001486 GSList *el, *machines = object_class_get_list(TYPE_MACHINE, false);
Anthony Liguori01d3c802012-08-10 11:04:11 -05001487 MachineInfoList *mach_list = NULL;
Anthony Liguori01d3c802012-08-10 11:04:11 -05001488
Marcel Apfelbaum261747f2014-03-05 19:30:46 +02001489 for (el = machines; el; el = el->next) {
1490 MachineClass *mc = el->data;
Anthony Liguori01d3c802012-08-10 11:04:11 -05001491 MachineInfoList *entry;
1492 MachineInfo *info;
1493
1494 info = g_malloc0(sizeof(*info));
Marcel Apfelbaum958db902014-04-09 20:34:53 +03001495 if (mc->is_default) {
Anthony Liguori01d3c802012-08-10 11:04:11 -05001496 info->has_is_default = true;
1497 info->is_default = true;
1498 }
1499
Marcel Apfelbaum958db902014-04-09 20:34:53 +03001500 if (mc->alias) {
Anthony Liguori01d3c802012-08-10 11:04:11 -05001501 info->has_alias = true;
Marcel Apfelbaum958db902014-04-09 20:34:53 +03001502 info->alias = g_strdup(mc->alias);
Anthony Liguori01d3c802012-08-10 11:04:11 -05001503 }
1504
Marcel Apfelbaum958db902014-04-09 20:34:53 +03001505 info->name = g_strdup(mc->name);
1506 info->cpu_max = !mc->max_cpus ? 1 : mc->max_cpus;
Anthony Liguori01d3c802012-08-10 11:04:11 -05001507
1508 entry = g_malloc0(sizeof(*entry));
1509 entry->value = info;
1510 entry->next = mach_list;
1511 mach_list = entry;
1512 }
1513
Marcel Apfelbaum261747f2014-03-05 19:30:46 +02001514 g_slist_free(machines);
Anthony Liguori01d3c802012-08-10 11:04:11 -05001515 return mach_list;
1516}
1517
Marcel Apfelbaum52eb3df2014-12-16 16:58:06 +00001518static int machine_help_func(QemuOpts *opts, MachineState *machine)
1519{
1520 ObjectProperty *prop;
1521
1522 if (!qemu_opt_has_help_opt(opts)) {
1523 return 0;
1524 }
1525
1526 QTAILQ_FOREACH(prop, &OBJECT(machine)->properties, node) {
1527 if (!prop->set) {
1528 continue;
1529 }
1530
1531 error_printf("%s.%s=%s", MACHINE_GET_CLASS(machine)->name,
1532 prop->name, prop->type);
1533 if (prop->description) {
1534 error_printf(" (%s)\n", prop->description);
1535 } else {
1536 error_printf("\n");
1537 }
1538 }
1539
1540 return 1;
1541}
1542
bellardcc1daa42005-06-05 14:49:17 +00001543/***********************************************************/
bellard8a7ddc32004-03-31 19:00:16 +00001544/* main execution loop */
1545
bellard0bd48852005-11-11 00:00:47 +00001546struct vm_change_state_entry {
1547 VMChangeStateHandler *cb;
1548 void *opaque;
Blue Swirl72cf2d42009-09-12 07:36:22 +00001549 QLIST_ENTRY (vm_change_state_entry) entries;
bellard0bd48852005-11-11 00:00:47 +00001550};
1551
Blue Swirl72cf2d42009-09-12 07:36:22 +00001552static QLIST_HEAD(vm_change_state_head, vm_change_state_entry) vm_change_state_head;
bellard0bd48852005-11-11 00:00:47 +00001553
1554VMChangeStateEntry *qemu_add_vm_change_state_handler(VMChangeStateHandler *cb,
1555 void *opaque)
1556{
1557 VMChangeStateEntry *e;
1558
Anthony Liguori7267c092011-08-20 22:09:37 -05001559 e = g_malloc0(sizeof (*e));
bellard0bd48852005-11-11 00:00:47 +00001560
1561 e->cb = cb;
1562 e->opaque = opaque;
Blue Swirl72cf2d42009-09-12 07:36:22 +00001563 QLIST_INSERT_HEAD(&vm_change_state_head, e, entries);
bellard0bd48852005-11-11 00:00:47 +00001564 return e;
1565}
1566
1567void qemu_del_vm_change_state_handler(VMChangeStateEntry *e)
1568{
Blue Swirl72cf2d42009-09-12 07:36:22 +00001569 QLIST_REMOVE (e, entries);
Anthony Liguori7267c092011-08-20 22:09:37 -05001570 g_free (e);
bellard0bd48852005-11-11 00:00:47 +00001571}
1572
Luiz Capitulino1dfb4dd2011-07-29 14:26:33 -03001573void vm_state_notify(int running, RunState state)
bellard0bd48852005-11-11 00:00:47 +00001574{
Paolo Bonzini9b10ac82014-09-09 14:12:26 +02001575 VMChangeStateEntry *e, *next;
bellard0bd48852005-11-11 00:00:47 +00001576
Luiz Capitulino1dfb4dd2011-07-29 14:26:33 -03001577 trace_vm_state_notify(running, state);
Stefan Hajnoczi94b0b5f2010-11-16 12:20:25 +00001578
Paolo Bonzini9b10ac82014-09-09 14:12:26 +02001579 QLIST_FOREACH_SAFE(e, &vm_change_state_head, entries, next) {
Luiz Capitulino1dfb4dd2011-07-29 14:26:33 -03001580 e->cb(e->opaque, running, state);
bellard0bd48852005-11-11 00:00:47 +00001581 }
1582}
1583
bellardbb0c6722004-06-20 12:37:32 +00001584/* reset/shutdown handler */
1585
1586typedef struct QEMUResetEntry {
Blue Swirl72cf2d42009-09-12 07:36:22 +00001587 QTAILQ_ENTRY(QEMUResetEntry) entry;
bellardbb0c6722004-06-20 12:37:32 +00001588 QEMUResetHandler *func;
1589 void *opaque;
bellardbb0c6722004-06-20 12:37:32 +00001590} QEMUResetEntry;
1591
Blue Swirl72cf2d42009-09-12 07:36:22 +00001592static QTAILQ_HEAD(reset_handlers, QEMUResetEntry) reset_handlers =
1593 QTAILQ_HEAD_INITIALIZER(reset_handlers);
bellardbb0c6722004-06-20 12:37:32 +00001594static int reset_requested;
Gleb Natapovf64622c2011-03-15 13:56:04 +02001595static int shutdown_requested, shutdown_signal = -1;
1596static pid_t shutdown_pid;
bellard34751872005-07-02 14:31:34 +00001597static int powerdown_requested;
Jan Kiszka8cf71712011-02-07 12:19:16 +01001598static int debug_requested;
Gerd Hoffmann95b363b2012-02-23 13:45:19 +01001599static int suspend_requested;
Liu, Jinsong4bc78a82013-09-25 16:38:29 +00001600static WakeupReason wakeup_reason;
Igor Mammedova9552c82012-09-05 23:06:21 +02001601static NotifierList powerdown_notifiers =
1602 NOTIFIER_LIST_INITIALIZER(powerdown_notifiers);
Gerd Hoffmann95b363b2012-02-23 13:45:19 +01001603static NotifierList suspend_notifiers =
1604 NOTIFIER_LIST_INITIALIZER(suspend_notifiers);
1605static NotifierList wakeup_notifiers =
1606 NOTIFIER_LIST_INITIALIZER(wakeup_notifiers);
Liu, Jinsong4bc78a82013-09-25 16:38:29 +00001607static uint32_t wakeup_reason_mask = ~(1 << QEMU_WAKEUP_REASON_NONE);
bellardbb0c6722004-06-20 12:37:32 +00001608
Anthony PERARD1291eb32010-07-22 15:52:48 +01001609int qemu_shutdown_requested_get(void)
1610{
1611 return shutdown_requested;
1612}
1613
1614int qemu_reset_requested_get(void)
1615{
1616 return reset_requested;
1617}
1618
Blue Swirl4fdcac02012-10-28 11:04:47 +00001619static int qemu_shutdown_requested(void)
aurel32cf7a2fe2008-03-18 06:53:05 +00001620{
Jan Kiszka817ef042014-10-27 14:05:10 +00001621 return atomic_xchg(&shutdown_requested, 0);
aurel32cf7a2fe2008-03-18 06:53:05 +00001622}
1623
Blue Swirl4fdcac02012-10-28 11:04:47 +00001624static void qemu_kill_report(void)
Gleb Natapovf64622c2011-03-15 13:56:04 +02001625{
Michael S. Tsirkin35b30712014-02-04 20:04:21 +02001626 if (!qtest_driver() && shutdown_signal != -1) {
Peter Maydellf1d3fb02011-03-30 22:03:38 +01001627 fprintf(stderr, "qemu: terminating on signal %d", shutdown_signal);
1628 if (shutdown_pid == 0) {
1629 /* This happens for eg ^C at the terminal, so it's worth
1630 * avoiding printing an odd message in that case.
1631 */
1632 fputc('\n', stderr);
1633 } else {
Andreas Färber953ffe02011-06-02 19:58:06 +02001634 fprintf(stderr, " from pid " FMT_pid "\n", shutdown_pid);
Peter Maydellf1d3fb02011-03-30 22:03:38 +01001635 }
Gleb Natapovf64622c2011-03-15 13:56:04 +02001636 shutdown_signal = -1;
1637 }
1638}
1639
Blue Swirl4fdcac02012-10-28 11:04:47 +00001640static int qemu_reset_requested(void)
aurel32cf7a2fe2008-03-18 06:53:05 +00001641{
1642 int r = reset_requested;
1643 reset_requested = 0;
1644 return r;
1645}
1646
Gerd Hoffmann95b363b2012-02-23 13:45:19 +01001647static int qemu_suspend_requested(void)
1648{
1649 int r = suspend_requested;
1650 suspend_requested = 0;
1651 return r;
1652}
1653
Liu, Jinsong4bc78a82013-09-25 16:38:29 +00001654static WakeupReason qemu_wakeup_requested(void)
Luiz Capitulino14058192012-08-08 17:29:17 -03001655{
Liu, Jinsong4bc78a82013-09-25 16:38:29 +00001656 return wakeup_reason;
Luiz Capitulino14058192012-08-08 17:29:17 -03001657}
1658
Blue Swirl4fdcac02012-10-28 11:04:47 +00001659static int qemu_powerdown_requested(void)
aurel32cf7a2fe2008-03-18 06:53:05 +00001660{
1661 int r = powerdown_requested;
1662 powerdown_requested = 0;
1663 return r;
1664}
1665
aliguorie5689022009-04-24 18:03:54 +00001666static int qemu_debug_requested(void)
1667{
1668 int r = debug_requested;
1669 debug_requested = 0;
1670 return r;
1671}
1672
Jan Kiszkaa08d4362009-06-27 09:25:07 +02001673void qemu_register_reset(QEMUResetHandler *func, void *opaque)
bellardbb0c6722004-06-20 12:37:32 +00001674{
Anthony Liguori7267c092011-08-20 22:09:37 -05001675 QEMUResetEntry *re = g_malloc0(sizeof(QEMUResetEntry));
bellardbb0c6722004-06-20 12:37:32 +00001676
bellardbb0c6722004-06-20 12:37:32 +00001677 re->func = func;
1678 re->opaque = opaque;
Blue Swirl72cf2d42009-09-12 07:36:22 +00001679 QTAILQ_INSERT_TAIL(&reset_handlers, re, entry);
bellardbb0c6722004-06-20 12:37:32 +00001680}
1681
Jan Kiszkadda9b292009-07-02 00:19:02 +02001682void qemu_unregister_reset(QEMUResetHandler *func, void *opaque)
bellardbb0c6722004-06-20 12:37:32 +00001683{
1684 QEMUResetEntry *re;
1685
Blue Swirl72cf2d42009-09-12 07:36:22 +00001686 QTAILQ_FOREACH(re, &reset_handlers, entry) {
Jan Kiszkadda9b292009-07-02 00:19:02 +02001687 if (re->func == func && re->opaque == opaque) {
Blue Swirl72cf2d42009-09-12 07:36:22 +00001688 QTAILQ_REMOVE(&reset_handlers, re, entry);
Anthony Liguori7267c092011-08-20 22:09:37 -05001689 g_free(re);
Jan Kiszkadda9b292009-07-02 00:19:02 +02001690 return;
1691 }
1692 }
1693}
1694
David Gibsonbe522022012-08-07 16:41:51 +10001695void qemu_devices_reset(void)
Jan Kiszkadda9b292009-07-02 00:19:02 +02001696{
1697 QEMUResetEntry *re, *nre;
1698
1699 /* reset all devices */
Blue Swirl72cf2d42009-09-12 07:36:22 +00001700 QTAILQ_FOREACH_SAFE(re, &reset_handlers, entry, nre) {
bellardbb0c6722004-06-20 12:37:32 +00001701 re->func(re->opaque);
1702 }
David Gibsonbe522022012-08-07 16:41:51 +10001703}
1704
1705void qemu_system_reset(bool report)
1706{
Marcel Apfelbaum0056ae22014-03-05 19:30:47 +02001707 MachineClass *mc;
1708
1709 mc = current_machine ? MACHINE_GET_CLASS(current_machine) : NULL;
1710
Marcel Apfelbaum958db902014-04-09 20:34:53 +03001711 if (mc && mc->reset) {
1712 mc->reset();
David Gibsonbe522022012-08-07 16:41:51 +10001713 } else {
1714 qemu_devices_reset();
1715 }
Jan Kiszkae063eb12011-06-14 18:29:43 +02001716 if (report) {
Wenchao Xiaa6330782014-06-18 08:43:35 +02001717 qapi_event_send_reset(&error_abort);
Jan Kiszkae063eb12011-06-14 18:29:43 +02001718 }
Jan Kiszkaea375f92010-03-01 19:10:30 +01001719 cpu_synchronize_all_post_reset();
bellardbb0c6722004-06-20 12:37:32 +00001720}
1721
Andrey Smetanin5f5b5942015-07-03 15:01:42 +03001722void qemu_system_guest_panicked(void)
1723{
Andrey Smetaninbac05aa2015-07-03 15:01:44 +03001724 if (current_cpu) {
1725 current_cpu->crash_occurred = true;
1726 }
Andrey Smetanin5f5b5942015-07-03 15:01:42 +03001727 qapi_event_send_guest_panicked(GUEST_PANIC_ACTION_PAUSE, &error_abort);
1728 vm_stop(RUN_STATE_GUEST_PANICKED);
1729}
1730
bellardbb0c6722004-06-20 12:37:32 +00001731void qemu_system_reset_request(void)
1732{
bellardd1beab82006-10-02 19:44:22 +00001733 if (no_reboot) {
1734 shutdown_requested = 1;
1735 } else {
1736 reset_requested = 1;
1737 }
Jan Kiszkab4a3d962011-02-01 22:15:43 +01001738 cpu_stop_current();
aliguorid9f75a42009-04-24 18:03:11 +00001739 qemu_notify_event();
bellardbb0c6722004-06-20 12:37:32 +00001740}
1741
Gerd Hoffmann95b363b2012-02-23 13:45:19 +01001742static void qemu_system_suspend(void)
1743{
1744 pause_all_vcpus();
1745 notifier_list_notify(&suspend_notifiers, NULL);
Luiz Capitulinoad02b962012-04-27 13:33:36 -03001746 runstate_set(RUN_STATE_SUSPENDED);
Wenchao Xia1d11a952014-06-18 08:43:38 +02001747 qapi_event_send_suspend(&error_abort);
Gerd Hoffmann95b363b2012-02-23 13:45:19 +01001748}
1749
1750void qemu_system_suspend_request(void)
1751{
Luiz Capitulino9abc62f2012-04-27 14:31:12 -03001752 if (runstate_check(RUN_STATE_SUSPENDED)) {
Gerd Hoffmann95b363b2012-02-23 13:45:19 +01001753 return;
1754 }
1755 suspend_requested = 1;
1756 cpu_stop_current();
1757 qemu_notify_event();
1758}
1759
1760void qemu_register_suspend_notifier(Notifier *notifier)
1761{
1762 notifier_list_add(&suspend_notifiers, notifier);
1763}
1764
1765void qemu_system_wakeup_request(WakeupReason reason)
1766{
Alexey Kardashevskiy4fed9422014-03-07 01:33:36 +05301767 trace_system_wakeup_request(reason);
1768
Luiz Capitulino9abc62f2012-04-27 14:31:12 -03001769 if (!runstate_check(RUN_STATE_SUSPENDED)) {
Gerd Hoffmann95b363b2012-02-23 13:45:19 +01001770 return;
1771 }
1772 if (!(wakeup_reason_mask & (1 << reason))) {
1773 return;
1774 }
Luiz Capitulinoad02b962012-04-27 13:33:36 -03001775 runstate_set(RUN_STATE_RUNNING);
Liu, Jinsong4bc78a82013-09-25 16:38:29 +00001776 wakeup_reason = reason;
Gerd Hoffmann95b363b2012-02-23 13:45:19 +01001777 qemu_notify_event();
Gerd Hoffmann95b363b2012-02-23 13:45:19 +01001778}
1779
1780void qemu_system_wakeup_enable(WakeupReason reason, bool enabled)
1781{
1782 if (enabled) {
1783 wakeup_reason_mask |= (1 << reason);
1784 } else {
1785 wakeup_reason_mask &= ~(1 << reason);
1786 }
1787}
1788
1789void qemu_register_wakeup_notifier(Notifier *notifier)
1790{
1791 notifier_list_add(&wakeup_notifiers, notifier);
1792}
1793
Gleb Natapovf64622c2011-03-15 13:56:04 +02001794void qemu_system_killed(int signal, pid_t pid)
1795{
1796 shutdown_signal = signal;
1797 shutdown_pid = pid;
Kevin Wolfd9389b92011-09-14 15:38:40 +02001798 no_shutdown = 0;
Gleb Natapovf64622c2011-03-15 13:56:04 +02001799 qemu_system_shutdown_request();
1800}
1801
bellardbb0c6722004-06-20 12:37:32 +00001802void qemu_system_shutdown_request(void)
1803{
Yang Zhiyongbc78cff2014-06-22 02:43:03 +08001804 trace_qemu_system_shutdown_request();
bellardbb0c6722004-06-20 12:37:32 +00001805 shutdown_requested = 1;
aliguorid9f75a42009-04-24 18:03:11 +00001806 qemu_notify_event();
bellardbb0c6722004-06-20 12:37:32 +00001807}
1808
Igor Mammedov013c2f12012-09-05 23:06:25 +02001809static void qemu_system_powerdown(void)
1810{
Wenchao Xia0aab9ec2014-06-18 08:43:34 +02001811 qapi_event_send_powerdown(&error_abort);
Igor Mammedov013c2f12012-09-05 23:06:25 +02001812 notifier_list_notify(&powerdown_notifiers, NULL);
1813}
1814
bellard34751872005-07-02 14:31:34 +00001815void qemu_system_powerdown_request(void)
1816{
Yang Zhiyongbc78cff2014-06-22 02:43:03 +08001817 trace_qemu_system_powerdown_request();
bellard34751872005-07-02 14:31:34 +00001818 powerdown_requested = 1;
aliguorid9f75a42009-04-24 18:03:11 +00001819 qemu_notify_event();
1820}
1821
Igor Mammedova9552c82012-09-05 23:06:21 +02001822void qemu_register_powerdown_notifier(Notifier *notifier)
1823{
1824 notifier_list_add(&powerdown_notifiers, notifier);
1825}
1826
Jan Kiszka8cf71712011-02-07 12:19:16 +01001827void qemu_system_debug_request(void)
1828{
1829 debug_requested = 1;
Jan Kiszka83f338f2011-02-07 12:19:17 +01001830 qemu_notify_event();
Jan Kiszka8cf71712011-02-07 12:19:16 +01001831}
1832
Paolo Bonzini99435902011-09-12 14:03:13 +02001833static bool main_loop_should_exit(void)
1834{
1835 RunState r;
1836 if (qemu_debug_requested()) {
1837 vm_stop(RUN_STATE_DEBUG);
1838 }
Gerd Hoffmann95b363b2012-02-23 13:45:19 +01001839 if (qemu_suspend_requested()) {
1840 qemu_system_suspend();
1841 }
Paolo Bonzini99435902011-09-12 14:03:13 +02001842 if (qemu_shutdown_requested()) {
1843 qemu_kill_report();
Wenchao Xia84321832014-06-18 08:43:33 +02001844 qapi_event_send_shutdown(&error_abort);
Paolo Bonzini99435902011-09-12 14:03:13 +02001845 if (no_shutdown) {
1846 vm_stop(RUN_STATE_SHUTDOWN);
1847 } else {
1848 return true;
1849 }
1850 }
1851 if (qemu_reset_requested()) {
1852 pause_all_vcpus();
1853 cpu_synchronize_all_states();
1854 qemu_system_reset(VMRESET_REPORT);
1855 resume_all_vcpus();
Hu Taoede085b2013-04-26 11:24:40 +08001856 if (runstate_needs_reset()) {
Paolo Bonzini99435902011-09-12 14:03:13 +02001857 runstate_set(RUN_STATE_PAUSED);
1858 }
1859 }
Luiz Capitulino14058192012-08-08 17:29:17 -03001860 if (qemu_wakeup_requested()) {
1861 pause_all_vcpus();
1862 cpu_synchronize_all_states();
1863 qemu_system_reset(VMRESET_SILENT);
Liu, Jinsong4bc78a82013-09-25 16:38:29 +00001864 notifier_list_notify(&wakeup_notifiers, &wakeup_reason);
1865 wakeup_reason = QEMU_WAKEUP_REASON_NONE;
Luiz Capitulino14058192012-08-08 17:29:17 -03001866 resume_all_vcpus();
Wenchao Xia7a906f72014-06-18 08:43:40 +02001867 qapi_event_send_wakeup(&error_abort);
Luiz Capitulino14058192012-08-08 17:29:17 -03001868 }
Paolo Bonzini99435902011-09-12 14:03:13 +02001869 if (qemu_powerdown_requested()) {
Igor Mammedov013c2f12012-09-05 23:06:25 +02001870 qemu_system_powerdown();
Paolo Bonzini99435902011-09-12 14:03:13 +02001871 }
1872 if (qemu_vmstop_requested(&r)) {
1873 vm_stop(r);
1874 }
1875 return false;
1876}
1877
aliguori43b96852009-04-24 18:03:33 +00001878static void main_loop(void)
1879{
Jan Kiszkac9f711a2011-08-22 17:46:02 +02001880 bool nonblocking;
Paolo Bonzini99435902011-09-12 14:03:13 +02001881 int last_io = 0;
Jan Kiszka8e1b90e2011-02-01 22:15:46 +01001882#ifdef CONFIG_PROFILER
1883 int64_t ti;
1884#endif
Paolo Bonzini99435902011-09-12 14:03:13 +02001885 do {
Stefano Stabellinia7d42072013-06-03 15:38:43 +00001886 nonblocking = !kvm_enabled() && !xen_enabled() && last_io > 0;
aliguori43b96852009-04-24 18:03:33 +00001887#ifdef CONFIG_PROFILER
Jan Kiszka46481d32011-02-01 22:15:47 +01001888 ti = profile_getclock();
aliguori43b96852009-04-24 18:03:33 +00001889#endif
Jan Kiszkac9f711a2011-08-22 17:46:02 +02001890 last_io = main_loop_wait(nonblocking);
aliguori43b96852009-04-24 18:03:33 +00001891#ifdef CONFIG_PROFILER
Jan Kiszka46481d32011-02-01 22:15:47 +01001892 dev_time += profile_getclock() - ti;
aliguori43b96852009-04-24 18:03:33 +00001893#endif
Paolo Bonzini99435902011-09-12 14:03:13 +02001894 } while (!main_loop_should_exit());
bellardb4608c02003-06-27 17:34:32 +00001895}
1896
pbrook9bd7e6d2009-04-07 22:58:45 +00001897static void version(void)
1898{
Thomas Monjalonf75ca1a2010-04-28 14:42:01 +02001899 printf("QEMU emulator version " QEMU_VERSION QEMU_PKGVERSION ", Copyright (c) 2003-2008 Fabrice Bellard\n");
pbrook9bd7e6d2009-04-07 22:58:45 +00001900}
1901
ths15f82202007-06-29 23:26:08 +00001902static void help(int exitcode)
bellard0824d6f2003-06-24 13:42:40 +00001903{
Michael Ellermana3adb7a2011-12-19 17:19:31 +11001904 version();
1905 printf("usage: %s [options] [disk_image]\n\n"
1906 "'disk_image' is a raw hard disk image for IDE hard disk 0\n\n",
1907 error_get_progname());
1908
Michael Ellerman77bd1112011-12-19 17:19:30 +11001909#define QEMU_OPTIONS_GENERATE_HELP
1910#include "qemu-options-wrapper.h"
Michael Ellermana3adb7a2011-12-19 17:19:31 +11001911
1912 printf("\nDuring emulation, the following keys are useful:\n"
malc3f020d72010-02-08 12:04:56 +03001913 "ctrl-alt-f toggle full screen\n"
1914 "ctrl-alt-n switch to virtual console 'n'\n"
1915 "ctrl-alt toggle mouse and keyboard grab\n"
1916 "\n"
Michael Ellermana3adb7a2011-12-19 17:19:31 +11001917 "When using -nographic, press 'ctrl-a h' to get some help.\n");
1918
ths15f82202007-06-29 23:26:08 +00001919 exit(exitcode);
bellard0824d6f2003-06-24 13:42:40 +00001920}
1921
bellardcd6f1162004-05-13 22:02:20 +00001922#define HAS_ARG 0x0001
1923
bellardcd6f1162004-05-13 22:02:20 +00001924typedef struct QEMUOption {
1925 const char *name;
1926 int flags;
1927 int index;
Blue Swirlad960902010-03-29 19:23:52 +00001928 uint32_t arch_mask;
bellardcd6f1162004-05-13 22:02:20 +00001929} QEMUOption;
1930
blueswir1dbed7e42008-10-01 19:38:09 +00001931static const QEMUOption qemu_options[] = {
Blue Swirlad960902010-03-29 19:23:52 +00001932 { "h", 0, QEMU_OPTION_h, QEMU_ARCH_ALL },
Michael Ellerman77bd1112011-12-19 17:19:30 +11001933#define QEMU_OPTIONS_GENERATE_OPTIONS
1934#include "qemu-options-wrapper.h"
bellardcd6f1162004-05-13 22:02:20 +00001935 { NULL },
bellardfc01f7e2003-06-30 10:03:06 +00001936};
Blue Swirla369da52011-09-27 19:15:42 +00001937
1938static bool vga_available(void)
1939{
Aurelien Jarno36b7f272012-09-08 16:01:20 +02001940 return object_class_by_name("VGA") || object_class_by_name("isa-vga");
Blue Swirla369da52011-09-27 19:15:42 +00001941}
1942
1943static bool cirrus_vga_available(void)
1944{
Aurelien Jarno36b7f272012-09-08 16:01:20 +02001945 return object_class_by_name("cirrus-vga")
1946 || object_class_by_name("isa-cirrus-vga");
Blue Swirla369da52011-09-27 19:15:42 +00001947}
1948
1949static bool vmware_vga_available(void)
1950{
Aurelien Jarno36b7f272012-09-08 16:01:20 +02001951 return object_class_by_name("vmware-svga");
Blue Swirla369da52011-09-27 19:15:42 +00001952}
1953
Aurelien Jarno879049a2012-09-08 12:01:06 +02001954static bool qxl_vga_available(void)
1955{
1956 return object_class_by_name("qxl-vga");
1957}
1958
Mark Cave-Aylandaf87bf22013-10-15 21:03:04 +01001959static bool tcx_vga_available(void)
1960{
1961 return object_class_by_name("SUNW,tcx");
1962}
1963
1964static bool cg3_vga_available(void)
1965{
1966 return object_class_by_name("cgthree");
1967}
1968
Gerd Hoffmanna94f0c52014-09-10 14:28:48 +02001969static bool virtio_vga_available(void)
1970{
1971 return object_class_by_name("virtio-vga");
1972}
1973
malc3893c122008-09-28 00:42:05 +00001974static void select_vgahw (const char *p)
1975{
1976 const char *opts;
1977
Mark Wud44229c2014-03-10 22:37:40 +08001978 assert(vga_interface_type == VGA_NONE);
malc3893c122008-09-28 00:42:05 +00001979 if (strstart(p, "std", &opts)) {
Blue Swirla369da52011-09-27 19:15:42 +00001980 if (vga_available()) {
1981 vga_interface_type = VGA_STD;
1982 } else {
1983 fprintf(stderr, "Error: standard VGA not available\n");
1984 exit(0);
1985 }
malc3893c122008-09-28 00:42:05 +00001986 } else if (strstart(p, "cirrus", &opts)) {
Blue Swirla369da52011-09-27 19:15:42 +00001987 if (cirrus_vga_available()) {
1988 vga_interface_type = VGA_CIRRUS;
1989 } else {
1990 fprintf(stderr, "Error: Cirrus VGA not available\n");
1991 exit(0);
1992 }
malc3893c122008-09-28 00:42:05 +00001993 } else if (strstart(p, "vmware", &opts)) {
Blue Swirla369da52011-09-27 19:15:42 +00001994 if (vmware_vga_available()) {
1995 vga_interface_type = VGA_VMWARE;
1996 } else {
1997 fprintf(stderr, "Error: VMWare SVGA not available\n");
1998 exit(0);
1999 }
Gerd Hoffmanna94f0c52014-09-10 14:28:48 +02002000 } else if (strstart(p, "virtio", &opts)) {
2001 if (virtio_vga_available()) {
2002 vga_interface_type = VGA_VIRTIO;
2003 } else {
2004 fprintf(stderr, "Error: Virtio VGA not available\n");
2005 exit(0);
2006 }
aliguori94909d92009-04-22 15:19:53 +00002007 } else if (strstart(p, "xenfb", &opts)) {
Zachary Amsden86176752009-07-30 00:15:02 -10002008 vga_interface_type = VGA_XENFB;
Gerd Hoffmanna19cbfb2010-04-27 11:50:11 +02002009 } else if (strstart(p, "qxl", &opts)) {
Aurelien Jarno879049a2012-09-08 12:01:06 +02002010 if (qxl_vga_available()) {
2011 vga_interface_type = VGA_QXL;
2012 } else {
2013 fprintf(stderr, "Error: QXL VGA not available\n");
2014 exit(0);
2015 }
Mark Cave-Aylandaf87bf22013-10-15 21:03:04 +01002016 } else if (strstart(p, "tcx", &opts)) {
2017 if (tcx_vga_available()) {
2018 vga_interface_type = VGA_TCX;
2019 } else {
2020 fprintf(stderr, "Error: TCX framebuffer not available\n");
2021 exit(0);
2022 }
2023 } else if (strstart(p, "cg3", &opts)) {
2024 if (cg3_vga_available()) {
2025 vga_interface_type = VGA_CG3;
2026 } else {
2027 fprintf(stderr, "Error: CG3 framebuffer not available\n");
2028 exit(0);
2029 }
aliguori28b85ed2009-04-22 15:19:48 +00002030 } else if (!strstart(p, "none", &opts)) {
malc3893c122008-09-28 00:42:05 +00002031 invalid_vga:
2032 fprintf(stderr, "Unknown vga type: %s\n", p);
2033 exit(1);
2034 }
malccb5a7aa2008-09-28 00:42:12 +00002035 while (*opts) {
2036 const char *nextopt;
2037
2038 if (strstart(opts, ",retrace=", &nextopt)) {
2039 opts = nextopt;
2040 if (strstart(opts, "dumb", &nextopt))
2041 vga_retrace_method = VGA_RETRACE_DUMB;
2042 else if (strstart(opts, "precise", &nextopt))
2043 vga_retrace_method = VGA_RETRACE_PRECISE;
2044 else goto invalid_vga;
2045 } else goto invalid_vga;
2046 opts = nextopt;
2047 }
malc3893c122008-09-28 00:42:05 +00002048}
2049
Jes Sorensen1472a952011-03-16 13:33:31 +01002050static DisplayType select_display(const char *p)
2051{
2052 const char *opts;
2053 DisplayType display = DT_DEFAULT;
2054
2055 if (strstart(p, "sdl", &opts)) {
2056#ifdef CONFIG_SDL
2057 display = DT_SDL;
2058 while (*opts) {
2059 const char *nextopt;
2060
2061 if (strstart(opts, ",frame=", &nextopt)) {
2062 opts = nextopt;
2063 if (strstart(opts, "on", &nextopt)) {
2064 no_frame = 0;
2065 } else if (strstart(opts, "off", &nextopt)) {
2066 no_frame = 1;
2067 } else {
Peter Maydell05175532011-03-23 03:40:57 +00002068 goto invalid_sdl_args;
Jes Sorensen1472a952011-03-16 13:33:31 +01002069 }
2070 } else if (strstart(opts, ",alt_grab=", &nextopt)) {
2071 opts = nextopt;
2072 if (strstart(opts, "on", &nextopt)) {
2073 alt_grab = 1;
2074 } else if (strstart(opts, "off", &nextopt)) {
2075 alt_grab = 0;
2076 } else {
Peter Maydell05175532011-03-23 03:40:57 +00002077 goto invalid_sdl_args;
Jes Sorensen1472a952011-03-16 13:33:31 +01002078 }
2079 } else if (strstart(opts, ",ctrl_grab=", &nextopt)) {
2080 opts = nextopt;
2081 if (strstart(opts, "on", &nextopt)) {
2082 ctrl_grab = 1;
2083 } else if (strstart(opts, "off", &nextopt)) {
2084 ctrl_grab = 0;
2085 } else {
Peter Maydell05175532011-03-23 03:40:57 +00002086 goto invalid_sdl_args;
Jes Sorensen1472a952011-03-16 13:33:31 +01002087 }
2088 } else if (strstart(opts, ",window_close=", &nextopt)) {
2089 opts = nextopt;
2090 if (strstart(opts, "on", &nextopt)) {
2091 no_quit = 0;
2092 } else if (strstart(opts, "off", &nextopt)) {
2093 no_quit = 1;
2094 } else {
Peter Maydell05175532011-03-23 03:40:57 +00002095 goto invalid_sdl_args;
Jes Sorensen1472a952011-03-16 13:33:31 +01002096 }
Gerd Hoffmann0b71a5d2014-11-11 16:54:45 +01002097 } else if (strstart(opts, ",gl=", &nextopt)) {
2098 opts = nextopt;
2099 if (strstart(opts, "on", &nextopt)) {
2100 request_opengl = 1;
2101 } else if (strstart(opts, "off", &nextopt)) {
2102 request_opengl = 0;
2103 } else {
2104 goto invalid_sdl_args;
2105 }
Jes Sorensen1472a952011-03-16 13:33:31 +01002106 } else {
Peter Maydell05175532011-03-23 03:40:57 +00002107 invalid_sdl_args:
Markus Armbrusterc6bf0f72015-02-13 18:23:45 +01002108 error_report("Invalid SDL option string");
Peter Maydell05175532011-03-23 03:40:57 +00002109 exit(1);
Jes Sorensen1472a952011-03-16 13:33:31 +01002110 }
2111 opts = nextopt;
2112 }
2113#else
Markus Armbrusterc6bf0f72015-02-13 18:23:45 +01002114 error_report("SDL support is disabled");
Jes Sorensen1472a952011-03-16 13:33:31 +01002115 exit(1);
2116#endif
Jes Sorensen3264ff12011-03-16 13:33:33 +01002117 } else if (strstart(p, "vnc", &opts)) {
Jes Sorensen821601e2011-03-16 13:33:36 +01002118#ifdef CONFIG_VNC
Gerd Hoffmann4db14622014-09-16 12:33:03 +02002119 if (*opts == '=') {
Stefan Weilfb430962015-07-22 19:53:30 +02002120 Error *err = NULL;
Markus Armbruster70b94332015-02-13 12:50:26 +01002121 if (vnc_parse(opts + 1, &err) == NULL) {
2122 error_report_err(err);
Gerd Hoffmann4db14622014-09-16 12:33:03 +02002123 exit(1);
Jes Sorensen3264ff12011-03-16 13:33:33 +01002124 }
Gerd Hoffmann4db14622014-09-16 12:33:03 +02002125 } else {
Markus Armbrusterc6bf0f72015-02-13 18:23:45 +01002126 error_report("VNC requires a display argument vnc=<display>");
Jes Sorensen3264ff12011-03-16 13:33:33 +01002127 exit(1);
2128 }
Jes Sorensen821601e2011-03-16 13:33:36 +01002129#else
Markus Armbrusterc6bf0f72015-02-13 18:23:45 +01002130 error_report("VNC support is disabled");
Jes Sorensen821601e2011-03-16 13:33:36 +01002131 exit(1);
2132#endif
Jes Sorensen1472a952011-03-16 13:33:31 +01002133 } else if (strstart(p, "curses", &opts)) {
2134#ifdef CONFIG_CURSES
2135 display = DT_CURSES;
2136#else
Markus Armbrusterc6bf0f72015-02-13 18:23:45 +01002137 error_report("Curses support is disabled");
Jes Sorensen1472a952011-03-16 13:33:31 +01002138 exit(1);
2139#endif
Anthony Liguori15546422013-02-20 07:43:25 -06002140 } else if (strstart(p, "gtk", &opts)) {
2141#ifdef CONFIG_GTK
2142 display = DT_GTK;
Jan Kiszka881249c2014-03-12 08:33:50 +01002143 while (*opts) {
2144 const char *nextopt;
2145
2146 if (strstart(opts, ",grab_on_hover=", &nextopt)) {
2147 opts = nextopt;
2148 if (strstart(opts, "on", &nextopt)) {
2149 grab_on_hover = true;
2150 } else if (strstart(opts, "off", &nextopt)) {
2151 grab_on_hover = false;
2152 } else {
2153 goto invalid_gtk_args;
2154 }
Gerd Hoffmann97edf3b2015-01-20 12:43:28 +01002155 } else if (strstart(opts, ",gl=", &nextopt)) {
2156 opts = nextopt;
2157 if (strstart(opts, "on", &nextopt)) {
2158 request_opengl = 1;
2159 } else if (strstart(opts, "off", &nextopt)) {
2160 request_opengl = 0;
2161 } else {
2162 goto invalid_gtk_args;
2163 }
Jan Kiszka881249c2014-03-12 08:33:50 +01002164 } else {
2165 invalid_gtk_args:
Markus Armbrusterc6bf0f72015-02-13 18:23:45 +01002166 error_report("Invalid GTK option string");
Jan Kiszka881249c2014-03-12 08:33:50 +01002167 exit(1);
2168 }
2169 opts = nextopt;
2170 }
Anthony Liguori15546422013-02-20 07:43:25 -06002171#else
Markus Armbrusterc6bf0f72015-02-13 18:23:45 +01002172 error_report("GTK support is disabled");
Anthony Liguori15546422013-02-20 07:43:25 -06002173 exit(1);
2174#endif
Jes Sorensen4171d322011-03-16 13:33:32 +01002175 } else if (strstart(p, "none", &opts)) {
2176 display = DT_NONE;
Jes Sorensen1472a952011-03-16 13:33:31 +01002177 } else {
Markus Armbrusterc6bf0f72015-02-13 18:23:45 +01002178 error_report("Unknown display type");
Jes Sorensen1472a952011-03-16 13:33:31 +01002179 exit(1);
2180 }
2181
2182 return display;
2183}
2184
Markus Armbruster7d4c3d52009-06-26 19:15:14 +02002185static int balloon_parse(const char *arg)
2186{
Gerd Hoffmann382f0742009-08-14 10:34:22 +02002187 QemuOpts *opts;
Markus Armbruster7d4c3d52009-06-26 19:15:14 +02002188
Gerd Hoffmann382f0742009-08-14 10:34:22 +02002189 if (strcmp(arg, "none") == 0) {
2190 return 0;
Markus Armbruster7d4c3d52009-06-26 19:15:14 +02002191 }
Gerd Hoffmann382f0742009-08-14 10:34:22 +02002192
2193 if (!strncmp(arg, "virtio", 6)) {
2194 if (arg[6] == ',') {
2195 /* have params -> parse them */
Markus Armbruster70b94332015-02-13 12:50:26 +01002196 opts = qemu_opts_parse_noisily(qemu_find_opts("device"), arg + 7,
2197 false);
Gerd Hoffmann382f0742009-08-14 10:34:22 +02002198 if (!opts)
2199 return -1;
2200 } else {
2201 /* create empty opts */
Peter Crosthwaite87ea75d2014-01-01 18:49:17 -08002202 opts = qemu_opts_create(qemu_find_opts("device"), NULL, 0,
2203 &error_abort);
Gerd Hoffmann382f0742009-08-14 10:34:22 +02002204 }
Markus Armbrusterf43e47d2015-02-12 17:52:20 +01002205 qemu_opt_set(opts, "driver", "virtio-balloon", &error_abort);
Gerd Hoffmann382f0742009-08-14 10:34:22 +02002206 return 0;
2207 }
2208
2209 return -1;
Markus Armbruster7d4c3d52009-06-26 19:15:14 +02002210}
Markus Armbruster7d4c3d52009-06-26 19:15:14 +02002211
Paul Brook5cea8592009-05-30 00:52:44 +01002212char *qemu_find_file(int type, const char *name)
2213{
Gerd Hoffmann45240512013-03-08 11:42:24 +01002214 int i;
Paul Brook5cea8592009-05-30 00:52:44 +01002215 const char *subdir;
2216 char *buf;
2217
Peter Maydell31783202012-05-25 13:07:01 +01002218 /* Try the name as a straight path first */
2219 if (access(name, R_OK) == 0) {
Gerd Hoffmann45240512013-03-08 11:42:24 +01002220 trace_load_file(name, name);
Anthony Liguori7267c092011-08-20 22:09:37 -05002221 return g_strdup(name);
Paul Brook5cea8592009-05-30 00:52:44 +01002222 }
Gerd Hoffmann45240512013-03-08 11:42:24 +01002223
Paul Brook5cea8592009-05-30 00:52:44 +01002224 switch (type) {
2225 case QEMU_FILE_TYPE_BIOS:
2226 subdir = "";
2227 break;
2228 case QEMU_FILE_TYPE_KEYMAP:
2229 subdir = "keymaps/";
2230 break;
2231 default:
2232 abort();
2233 }
Gerd Hoffmann45240512013-03-08 11:42:24 +01002234
2235 for (i = 0; i < data_dir_idx; i++) {
2236 buf = g_strdup_printf("%s/%s%s", data_dir[i], subdir, name);
2237 if (access(buf, R_OK) == 0) {
2238 trace_load_file(name, buf);
2239 return buf;
2240 }
Anthony Liguori7267c092011-08-20 22:09:37 -05002241 g_free(buf);
Paul Brook5cea8592009-05-30 00:52:44 +01002242 }
Gerd Hoffmann45240512013-03-08 11:42:24 +01002243 return NULL;
Paul Brook5cea8592009-05-30 00:52:44 +01002244}
2245
Gabriel L. Somlo6407d762015-09-29 12:29:01 -04002246static inline bool nonempty_str(const char *str)
2247{
2248 return str && *str;
2249}
2250
Gabriel L. Somlo81b2b812015-04-29 11:21:53 -04002251static int parse_fw_cfg(void *opaque, QemuOpts *opts, Error **errp)
2252{
2253 gchar *buf;
2254 size_t size;
Gabriel L. Somlo6407d762015-09-29 12:29:01 -04002255 const char *name, *file, *str;
Gabriel L. Somlo81b2b812015-04-29 11:21:53 -04002256
2257 if (opaque == NULL) {
2258 error_report("fw_cfg device not available");
2259 return -1;
2260 }
2261 name = qemu_opt_get(opts, "name");
2262 file = qemu_opt_get(opts, "file");
Gabriel L. Somlo6407d762015-09-29 12:29:01 -04002263 str = qemu_opt_get(opts, "string");
2264
2265 /* we need name and either a file or the content string */
2266 if (!(nonempty_str(name) && (nonempty_str(file) || nonempty_str(str)))) {
2267 error_report("invalid argument(s)");
2268 return -1;
2269 }
2270 if (nonempty_str(file) && nonempty_str(str)) {
2271 error_report("file and string are mutually exclusive");
Gabriel L. Somlo81b2b812015-04-29 11:21:53 -04002272 return -1;
2273 }
2274 if (strlen(name) > FW_CFG_MAX_FILE_PATH - 1) {
2275 error_report("name too long (max. %d char)", FW_CFG_MAX_FILE_PATH - 1);
2276 return -1;
2277 }
2278 if (strncmp(name, "opt/", 4) != 0) {
2279 error_report("WARNING: externally provided fw_cfg item names "
2280 "should be prefixed with \"opt/\"!");
2281 }
Gabriel L. Somlo6407d762015-09-29 12:29:01 -04002282 if (nonempty_str(str)) {
2283 size = strlen(str); /* NUL terminator NOT included in fw_cfg blob */
2284 buf = g_memdup(str, size);
2285 } else {
2286 if (!g_file_get_contents(file, &buf, &size, NULL)) {
2287 error_report("can't load %s", file);
2288 return -1;
2289 }
Gabriel L. Somlo81b2b812015-04-29 11:21:53 -04002290 }
2291 fw_cfg_add_file((FWCfgState *)opaque, name, buf, size);
2292 return 0;
2293}
2294
Markus Armbruster28d0de72015-03-13 13:35:14 +01002295static int device_help_func(void *opaque, QemuOpts *opts, Error **errp)
Markus Armbrusterff952ba2010-01-29 19:48:57 +01002296{
2297 return qdev_device_help(opts);
2298}
2299
Markus Armbruster28d0de72015-03-13 13:35:14 +01002300static int device_init_func(void *opaque, QemuOpts *opts, Error **errp)
Gerd Hoffmannf31d07d2009-07-31 12:25:37 +02002301{
Markus Armbrusterf006cf72015-03-12 14:00:41 +01002302 Error *err = NULL;
Gerd Hoffmannf31d07d2009-07-31 12:25:37 +02002303 DeviceState *dev;
2304
Markus Armbrusterf006cf72015-03-12 14:00:41 +01002305 dev = qdev_device_add(opts, &err);
2306 if (!dev) {
2307 error_report_err(err);
Gerd Hoffmannf31d07d2009-07-31 12:25:37 +02002308 return -1;
Markus Armbrusterf006cf72015-03-12 14:00:41 +01002309 }
Paolo Bonzinib09995a2013-01-25 14:12:37 +01002310 object_unref(OBJECT(dev));
Gerd Hoffmannf31d07d2009-07-31 12:25:37 +02002311 return 0;
2312}
2313
Markus Armbruster28d0de72015-03-13 13:35:14 +01002314static int chardev_init_func(void *opaque, QemuOpts *opts, Error **errp)
Gerd Hoffmann1a688d32009-12-08 13:11:36 +01002315{
Gerd Hoffmannbd2d80b2012-10-15 09:28:05 +02002316 Error *local_err = NULL;
Gerd Hoffmann1a688d32009-12-08 13:11:36 +01002317
Gerd Hoffmannbd2d80b2012-10-15 09:28:05 +02002318 qemu_chr_new_from_opts(opts, NULL, &local_err);
Markus Armbruster84d18f02014-01-30 15:07:28 +01002319 if (local_err) {
Markus Armbruster565f65d2015-02-12 13:55:05 +01002320 error_report_err(local_err);
Gerd Hoffmann1a688d32009-12-08 13:11:36 +01002321 return -1;
Gerd Hoffmannbd2d80b2012-10-15 09:28:05 +02002322 }
Gerd Hoffmann1a688d32009-12-08 13:11:36 +01002323 return 0;
2324}
2325
Venkateswararao Jujjuri (JV)758e8e32010-06-14 13:34:41 -07002326#ifdef CONFIG_VIRTFS
Markus Armbruster28d0de72015-03-13 13:35:14 +01002327static int fsdev_init_func(void *opaque, QemuOpts *opts, Error **errp)
Gautham R Shenoy74db9202010-04-29 17:44:43 +05302328{
2329 int ret;
2330 ret = qemu_fsdev_add(opts);
2331
2332 return ret;
2333}
2334#endif
2335
Markus Armbruster28d0de72015-03-13 13:35:14 +01002336static int mon_init_func(void *opaque, QemuOpts *opts, Error **errp)
Gerd Hoffmann88589342009-12-08 13:11:50 +01002337{
2338 CharDriverState *chr;
2339 const char *chardev;
2340 const char *mode;
2341 int flags;
2342
2343 mode = qemu_opt_get(opts, "mode");
2344 if (mode == NULL) {
2345 mode = "readline";
2346 }
2347 if (strcmp(mode, "readline") == 0) {
2348 flags = MONITOR_USE_READLINE;
2349 } else if (strcmp(mode, "control") == 0) {
2350 flags = MONITOR_USE_CONTROL;
2351 } else {
2352 fprintf(stderr, "unknown monitor mode \"%s\"\n", mode);
2353 exit(1);
2354 }
2355
Daniel P. Berrange39eaab92010-06-07 15:42:31 +01002356 if (qemu_opt_get_bool(opts, "pretty", 0))
2357 flags |= MONITOR_USE_PRETTY;
2358
Gerd Hoffmann88589342009-12-08 13:11:50 +01002359 if (qemu_opt_get_bool(opts, "default", 0))
2360 flags |= MONITOR_IS_DEFAULT;
2361
2362 chardev = qemu_opt_get(opts, "chardev");
2363 chr = qemu_chr_find(chardev);
2364 if (chr == NULL) {
2365 fprintf(stderr, "chardev \"%s\" not found\n", chardev);
2366 exit(1);
2367 }
2368
Hans de Goede456d6062013-03-27 20:29:40 +01002369 qemu_chr_fe_claim_no_fail(chr);
Gerd Hoffmann88589342009-12-08 13:11:50 +01002370 monitor_init(chr, flags);
2371 return 0;
2372}
2373
Max Reitz4821cd42014-11-17 13:31:04 +01002374static void monitor_parse(const char *optarg, const char *mode, bool pretty)
Gerd Hoffmann88589342009-12-08 13:11:50 +01002375{
2376 static int monitor_device_index = 0;
Markus Armbruster46ede582015-01-13 14:19:13 +01002377 Error *local_err = NULL;
Gerd Hoffmann88589342009-12-08 13:11:50 +01002378 QemuOpts *opts;
2379 const char *p;
2380 char label[32];
2381 int def = 0;
2382
2383 if (strstart(optarg, "chardev:", &p)) {
2384 snprintf(label, sizeof(label), "%s", p);
2385 } else {
Jan Kiszka140e0652010-04-06 16:55:52 +02002386 snprintf(label, sizeof(label), "compat_monitor%d",
2387 monitor_device_index);
2388 if (monitor_device_index == 0) {
Gerd Hoffmann88589342009-12-08 13:11:50 +01002389 def = 1;
2390 }
2391 opts = qemu_chr_parse_compat(label, optarg);
2392 if (!opts) {
2393 fprintf(stderr, "parse error: %s\n", optarg);
2394 exit(1);
2395 }
2396 }
2397
Markus Armbruster46ede582015-01-13 14:19:13 +01002398 opts = qemu_opts_create(qemu_find_opts("mon"), label, 1, &local_err);
Gerd Hoffmann88589342009-12-08 13:11:50 +01002399 if (!opts) {
Markus Armbruster565f65d2015-02-12 13:55:05 +01002400 error_report_err(local_err);
Gerd Hoffmann88589342009-12-08 13:11:50 +01002401 exit(1);
2402 }
Markus Armbrusterf43e47d2015-02-12 17:52:20 +01002403 qemu_opt_set(opts, "mode", mode, &error_abort);
2404 qemu_opt_set(opts, "chardev", label, &error_abort);
Markus Armbrustercccb7962015-02-12 16:37:44 +01002405 qemu_opt_set_bool(opts, "pretty", pretty, &error_abort);
Gerd Hoffmann88589342009-12-08 13:11:50 +01002406 if (def)
Markus Armbrusterf43e47d2015-02-12 17:52:20 +01002407 qemu_opt_set(opts, "default", "on", &error_abort);
Gerd Hoffmann88589342009-12-08 13:11:50 +01002408 monitor_device_index++;
2409}
2410
Gerd Hoffmannbd3c9482009-07-15 13:59:26 +02002411struct device_config {
2412 enum {
Gerd Hoffmannaee1b932009-12-08 13:11:53 +01002413 DEV_USB, /* -usbdevice */
2414 DEV_BT, /* -bt */
2415 DEV_SERIAL, /* -serial */
2416 DEV_PARALLEL, /* -parallel */
2417 DEV_VIRTCON, /* -virtioconsole */
H. Peter Anvinc9f398e2009-12-29 13:51:36 -08002418 DEV_DEBUGCON, /* -debugcon */
Markus Armbrusteref0c4a02012-02-07 15:09:13 +01002419 DEV_GDB, /* -gdb, -s */
Alexander Graf3ef669e2013-01-24 12:18:52 +01002420 DEV_SCLP, /* s390 sclp */
Gerd Hoffmannbd3c9482009-07-15 13:59:26 +02002421 } type;
2422 const char *cmdline;
Markus Armbrusterd9a59542012-02-07 15:09:12 +01002423 Location loc;
Blue Swirl72cf2d42009-09-12 07:36:22 +00002424 QTAILQ_ENTRY(device_config) next;
Gerd Hoffmannbd3c9482009-07-15 13:59:26 +02002425};
Blue Swirl4fdcac02012-10-28 11:04:47 +00002426
2427static QTAILQ_HEAD(, device_config) device_configs =
2428 QTAILQ_HEAD_INITIALIZER(device_configs);
Gerd Hoffmannbd3c9482009-07-15 13:59:26 +02002429
2430static void add_device_config(int type, const char *cmdline)
2431{
2432 struct device_config *conf;
2433
Anthony Liguori7267c092011-08-20 22:09:37 -05002434 conf = g_malloc0(sizeof(*conf));
Gerd Hoffmannbd3c9482009-07-15 13:59:26 +02002435 conf->type = type;
2436 conf->cmdline = cmdline;
Markus Armbrusterd9a59542012-02-07 15:09:12 +01002437 loc_save(&conf->loc);
Blue Swirl72cf2d42009-09-12 07:36:22 +00002438 QTAILQ_INSERT_TAIL(&device_configs, conf, next);
Gerd Hoffmannbd3c9482009-07-15 13:59:26 +02002439}
2440
2441static int foreach_device_config(int type, int (*func)(const char *cmdline))
2442{
2443 struct device_config *conf;
2444 int rc;
2445
Blue Swirl72cf2d42009-09-12 07:36:22 +00002446 QTAILQ_FOREACH(conf, &device_configs, next) {
Gerd Hoffmannbd3c9482009-07-15 13:59:26 +02002447 if (conf->type != type)
2448 continue;
Markus Armbrusterd9a59542012-02-07 15:09:12 +01002449 loc_push_restore(&conf->loc);
Gerd Hoffmannbd3c9482009-07-15 13:59:26 +02002450 rc = func(conf->cmdline);
Markus Armbrusterd9a59542012-02-07 15:09:12 +01002451 loc_pop(&conf->loc);
Gonglei28de2f82014-08-11 21:00:57 +08002452 if (rc) {
Gerd Hoffmannbd3c9482009-07-15 13:59:26 +02002453 return rc;
Gonglei28de2f82014-08-11 21:00:57 +08002454 }
Gerd Hoffmannbd3c9482009-07-15 13:59:26 +02002455 }
2456 return 0;
2457}
2458
Gerd Hoffmann998bbd72009-12-08 13:11:41 +01002459static int serial_parse(const char *devname)
2460{
2461 static int index = 0;
2462 char label[32];
2463
2464 if (strcmp(devname, "none") == 0)
2465 return 0;
2466 if (index == MAX_SERIAL_PORTS) {
2467 fprintf(stderr, "qemu: too many serial ports\n");
2468 exit(1);
2469 }
2470 snprintf(label, sizeof(label), "serial%d", index);
Anthony Liguori27143a42011-08-15 11:17:36 -05002471 serial_hds[index] = qemu_chr_new(label, devname, NULL);
Gerd Hoffmann998bbd72009-12-08 13:11:41 +01002472 if (!serial_hds[index]) {
Peter Maydell52d06132012-07-09 04:28:30 +00002473 fprintf(stderr, "qemu: could not connect serial device"
2474 " to character backend '%s'\n", devname);
Gerd Hoffmann998bbd72009-12-08 13:11:41 +01002475 return -1;
2476 }
2477 index++;
2478 return 0;
2479}
2480
Gerd Hoffmann6a5e8b02009-12-08 13:11:42 +01002481static int parallel_parse(const char *devname)
2482{
2483 static int index = 0;
2484 char label[32];
2485
2486 if (strcmp(devname, "none") == 0)
2487 return 0;
2488 if (index == MAX_PARALLEL_PORTS) {
2489 fprintf(stderr, "qemu: too many parallel ports\n");
2490 exit(1);
2491 }
2492 snprintf(label, sizeof(label), "parallel%d", index);
Anthony Liguori27143a42011-08-15 11:17:36 -05002493 parallel_hds[index] = qemu_chr_new(label, devname, NULL);
Gerd Hoffmann6a5e8b02009-12-08 13:11:42 +01002494 if (!parallel_hds[index]) {
Peter Maydell52d06132012-07-09 04:28:30 +00002495 fprintf(stderr, "qemu: could not connect parallel device"
2496 " to character backend '%s'\n", devname);
Gerd Hoffmann6a5e8b02009-12-08 13:11:42 +01002497 return -1;
2498 }
2499 index++;
2500 return 0;
2501}
2502
Gerd Hoffmannaee1b932009-12-08 13:11:53 +01002503static int virtcon_parse(const char *devname)
2504{
Gerd Hoffmann3329f072010-08-20 13:52:01 +02002505 QemuOptsList *device = qemu_find_opts("device");
Gerd Hoffmannaee1b932009-12-08 13:11:53 +01002506 static int index = 0;
2507 char label[32];
Amit Shah392ecf52010-01-21 16:19:23 +05302508 QemuOpts *bus_opts, *dev_opts;
Gerd Hoffmannaee1b932009-12-08 13:11:53 +01002509
2510 if (strcmp(devname, "none") == 0)
2511 return 0;
2512 if (index == MAX_VIRTIO_CONSOLES) {
2513 fprintf(stderr, "qemu: too many virtio consoles\n");
2514 exit(1);
2515 }
Amit Shah392ecf52010-01-21 16:19:23 +05302516
Peter Crosthwaite87ea75d2014-01-01 18:49:17 -08002517 bus_opts = qemu_opts_create(device, NULL, 0, &error_abort);
Anthony Liguorie87f7fc2012-02-06 11:07:18 -06002518 if (arch_type == QEMU_ARCH_S390X) {
Markus Armbrusterf43e47d2015-02-12 17:52:20 +01002519 qemu_opt_set(bus_opts, "driver", "virtio-serial-s390", &error_abort);
Anthony Liguorie87f7fc2012-02-06 11:07:18 -06002520 } else {
Markus Armbrusterf43e47d2015-02-12 17:52:20 +01002521 qemu_opt_set(bus_opts, "driver", "virtio-serial-pci", &error_abort);
Laszlo Ersek4d8b3c62013-03-21 00:23:13 +01002522 }
Amit Shah392ecf52010-01-21 16:19:23 +05302523
Peter Crosthwaite87ea75d2014-01-01 18:49:17 -08002524 dev_opts = qemu_opts_create(device, NULL, 0, &error_abort);
Markus Armbrusterf43e47d2015-02-12 17:52:20 +01002525 qemu_opt_set(dev_opts, "driver", "virtconsole", &error_abort);
Amit Shah392ecf52010-01-21 16:19:23 +05302526
Gerd Hoffmannaee1b932009-12-08 13:11:53 +01002527 snprintf(label, sizeof(label), "virtcon%d", index);
Anthony Liguori27143a42011-08-15 11:17:36 -05002528 virtcon_hds[index] = qemu_chr_new(label, devname, NULL);
Gerd Hoffmannaee1b932009-12-08 13:11:53 +01002529 if (!virtcon_hds[index]) {
Peter Maydell52d06132012-07-09 04:28:30 +00002530 fprintf(stderr, "qemu: could not connect virtio console"
2531 " to character backend '%s'\n", devname);
Gerd Hoffmannaee1b932009-12-08 13:11:53 +01002532 return -1;
2533 }
Markus Armbrusterf43e47d2015-02-12 17:52:20 +01002534 qemu_opt_set(dev_opts, "chardev", label, &error_abort);
Amit Shah392ecf52010-01-21 16:19:23 +05302535
Gerd Hoffmannaee1b932009-12-08 13:11:53 +01002536 index++;
2537 return 0;
2538}
2539
Alexander Graf3ef669e2013-01-24 12:18:52 +01002540static int sclp_parse(const char *devname)
2541{
2542 QemuOptsList *device = qemu_find_opts("device");
2543 static int index = 0;
2544 char label[32];
2545 QemuOpts *dev_opts;
2546
2547 if (strcmp(devname, "none") == 0) {
2548 return 0;
2549 }
2550 if (index == MAX_SCLP_CONSOLES) {
2551 fprintf(stderr, "qemu: too many sclp consoles\n");
2552 exit(1);
2553 }
2554
2555 assert(arch_type == QEMU_ARCH_S390X);
2556
2557 dev_opts = qemu_opts_create(device, NULL, 0, NULL);
Markus Armbrusterf43e47d2015-02-12 17:52:20 +01002558 qemu_opt_set(dev_opts, "driver", "sclpconsole", &error_abort);
Alexander Graf3ef669e2013-01-24 12:18:52 +01002559
2560 snprintf(label, sizeof(label), "sclpcon%d", index);
2561 sclp_hds[index] = qemu_chr_new(label, devname, NULL);
2562 if (!sclp_hds[index]) {
2563 fprintf(stderr, "qemu: could not connect sclp console"
2564 " to character backend '%s'\n", devname);
2565 return -1;
2566 }
Markus Armbrusterf43e47d2015-02-12 17:52:20 +01002567 qemu_opt_set(dev_opts, "chardev", label, &error_abort);
Alexander Graf3ef669e2013-01-24 12:18:52 +01002568
2569 index++;
2570 return 0;
2571}
2572
H. Peter Anvinc9f398e2009-12-29 13:51:36 -08002573static int debugcon_parse(const char *devname)
Laszlo Ersek4d8b3c62013-03-21 00:23:13 +01002574{
H. Peter Anvinc9f398e2009-12-29 13:51:36 -08002575 QemuOpts *opts;
2576
Anthony Liguori27143a42011-08-15 11:17:36 -05002577 if (!qemu_chr_new("debugcon", devname, NULL)) {
H. Peter Anvinc9f398e2009-12-29 13:51:36 -08002578 exit(1);
2579 }
Luiz Capitulino8be7e7e2012-03-20 15:51:57 -03002580 opts = qemu_opts_create(qemu_find_opts("device"), "debugcon", 1, NULL);
H. Peter Anvinc9f398e2009-12-29 13:51:36 -08002581 if (!opts) {
2582 fprintf(stderr, "qemu: already have a debugcon device\n");
2583 exit(1);
2584 }
Markus Armbrusterf43e47d2015-02-12 17:52:20 +01002585 qemu_opt_set(opts, "driver", "isa-debugcon", &error_abort);
2586 qemu_opt_set(opts, "chardev", "debugcon", &error_abort);
H. Peter Anvinc9f398e2009-12-29 13:51:36 -08002587 return 0;
2588}
2589
Laszlo Ersek2709f262014-09-22 22:38:35 +02002590static gint machine_class_cmp(gconstpointer a, gconstpointer b)
2591{
2592 const MachineClass *mc1 = a, *mc2 = b;
2593 int res;
2594
2595 if (mc1->family == NULL) {
2596 if (mc2->family == NULL) {
2597 /* Compare standalone machine types against each other; they sort
2598 * in increasing order.
2599 */
2600 return strcmp(object_class_get_name(OBJECT_CLASS(mc1)),
2601 object_class_get_name(OBJECT_CLASS(mc2)));
2602 }
2603
2604 /* Standalone machine types sort after families. */
2605 return 1;
2606 }
2607
2608 if (mc2->family == NULL) {
2609 /* Families sort before standalone machine types. */
2610 return -1;
2611 }
2612
2613 /* Families sort between each other alphabetically increasingly. */
2614 res = strcmp(mc1->family, mc2->family);
2615 if (res != 0) {
2616 return res;
2617 }
2618
2619 /* Within the same family, machine types sort in decreasing order. */
2620 return strcmp(object_class_get_name(OBJECT_CLASS(mc2)),
2621 object_class_get_name(OBJECT_CLASS(mc1)));
2622}
2623
2624 static MachineClass *machine_parse(const char *name)
Jan Kiszka9052ea62011-07-23 12:38:37 +02002625{
Marcel Apfelbaum0056ae22014-03-05 19:30:47 +02002626 MachineClass *mc = NULL;
Marcel Apfelbaum261747f2014-03-05 19:30:46 +02002627 GSList *el, *machines = object_class_get_list(TYPE_MACHINE, false);
Jan Kiszka9052ea62011-07-23 12:38:37 +02002628
2629 if (name) {
Marcel Apfelbaum0056ae22014-03-05 19:30:47 +02002630 mc = find_machine(name);
Jan Kiszka9052ea62011-07-23 12:38:37 +02002631 }
Marcel Apfelbaum0056ae22014-03-05 19:30:47 +02002632 if (mc) {
Christian Borntraeger02f292d2015-01-28 16:07:17 +01002633 g_slist_free(machines);
Marcel Apfelbaum0056ae22014-03-05 19:30:47 +02002634 return mc;
Jan Kiszka9052ea62011-07-23 12:38:37 +02002635 }
Miroslav Rezanina025172d2014-03-14 13:06:54 +01002636 if (name && !is_help_option(name)) {
2637 error_report("Unsupported machine type");
2638 error_printf("Use -machine help to list supported machines!\n");
2639 } else {
2640 printf("Supported machines are:\n");
Laszlo Ersek2709f262014-09-22 22:38:35 +02002641 machines = g_slist_sort(machines, machine_class_cmp);
Miroslav Rezanina025172d2014-03-14 13:06:54 +01002642 for (el = machines; el; el = el->next) {
2643 MachineClass *mc = el->data;
Marcel Apfelbaum958db902014-04-09 20:34:53 +03002644 if (mc->alias) {
2645 printf("%-20s %s (alias of %s)\n", mc->alias, mc->desc, mc->name);
Miroslav Rezanina025172d2014-03-14 13:06:54 +01002646 }
Marcel Apfelbaum958db902014-04-09 20:34:53 +03002647 printf("%-20s %s%s\n", mc->name, mc->desc,
2648 mc->is_default ? " (default)" : "");
Jan Kiszka9052ea62011-07-23 12:38:37 +02002649 }
Jan Kiszka9052ea62011-07-23 12:38:37 +02002650 }
Marcel Apfelbaum261747f2014-03-05 19:30:46 +02002651
2652 g_slist_free(machines);
Peter Maydellc8057f92012-08-02 13:45:54 +01002653 exit(!name || !is_help_option(name));
Jan Kiszka9052ea62011-07-23 12:38:37 +02002654}
2655
Gerd Hoffmannfd42dee2010-06-04 14:08:07 +02002656void qemu_add_exit_notifier(Notifier *notify)
2657{
2658 notifier_list_add(&exit_notifiers, notify);
2659}
2660
2661void qemu_remove_exit_notifier(Notifier *notify)
2662{
Paolo Bonzini31552522012-01-13 17:34:01 +01002663 notifier_remove(notify);
Gerd Hoffmannfd42dee2010-06-04 14:08:07 +02002664}
2665
2666static void qemu_run_exit_notifiers(void)
2667{
Jan Kiszka9e8dd452011-06-20 14:06:26 +02002668 notifier_list_notify(&exit_notifiers, NULL);
Gerd Hoffmannfd42dee2010-06-04 14:08:07 +02002669}
2670
Paolo Bonzini71cdd1c2015-03-31 14:01:06 +02002671static bool machine_init_done;
2672
Gleb Natapov4cab9462010-12-08 13:35:08 +02002673void qemu_add_machine_init_done_notifier(Notifier *notify)
2674{
2675 notifier_list_add(&machine_init_done_notifiers, notify);
Paolo Bonzini71cdd1c2015-03-31 14:01:06 +02002676 if (machine_init_done) {
2677 notify->notify(notify, NULL);
2678 }
Gleb Natapov4cab9462010-12-08 13:35:08 +02002679}
2680
2681static void qemu_run_machine_init_done_notifiers(void)
2682{
Jan Kiszka9e8dd452011-06-20 14:06:26 +02002683 notifier_list_notify(&machine_init_done_notifiers, NULL);
Paolo Bonzini71cdd1c2015-03-31 14:01:06 +02002684 machine_init_done = true;
Gleb Natapov4cab9462010-12-08 13:35:08 +02002685}
2686
Anthony Liguori6530a972010-01-22 09:18:06 -06002687static const QEMUOption *lookup_opt(int argc, char **argv,
2688 const char **poptarg, int *poptind)
2689{
2690 const QEMUOption *popt;
2691 int optind = *poptind;
2692 char *r = argv[optind];
2693 const char *optarg;
2694
Markus Armbruster0f0bc3f2010-02-18 20:13:51 +01002695 loc_set_cmdline(argv, optind, 1);
Anthony Liguori6530a972010-01-22 09:18:06 -06002696 optind++;
2697 /* Treat --foo the same as -foo. */
2698 if (r[1] == '-')
2699 r++;
2700 popt = qemu_options;
2701 for(;;) {
2702 if (!popt->name) {
Markus Armbruster0f0bc3f2010-02-18 20:13:51 +01002703 error_report("invalid option");
Anthony Liguori6530a972010-01-22 09:18:06 -06002704 exit(1);
2705 }
2706 if (!strcmp(popt->name, r + 1))
2707 break;
2708 popt++;
2709 }
2710 if (popt->flags & HAS_ARG) {
2711 if (optind >= argc) {
Markus Armbruster0f0bc3f2010-02-18 20:13:51 +01002712 error_report("requires an argument");
Anthony Liguori6530a972010-01-22 09:18:06 -06002713 exit(1);
2714 }
2715 optarg = argv[optind++];
Markus Armbruster0f0bc3f2010-02-18 20:13:51 +01002716 loc_set_cmdline(argv, optind - 2, 2);
Anthony Liguori6530a972010-01-22 09:18:06 -06002717 } else {
2718 optarg = NULL;
2719 }
2720
2721 *poptarg = optarg;
2722 *poptind = optind;
2723
2724 return popt;
2725}
2726
Markus Armbruster71df1d82015-03-12 08:40:25 +01002727static int machine_set_property(void *opaque,
2728 const char *name, const char *value,
2729 Error **errp)
Anthony Liguori68d98d32012-06-25 14:36:33 -05002730{
2731 Object *obj = OBJECT(opaque);
Anthony Liguori68d98d32012-06-25 14:36:33 -05002732 Error *local_err = NULL;
Marcel Apfelbaumb0ddb8b2014-07-18 19:32:37 +03002733 char *c, *qom_name;
Anthony Liguori68d98d32012-06-25 14:36:33 -05002734
Andreas Färberd2659e22014-07-23 16:16:26 +02002735 if (strcmp(name, "type") == 0) {
Anthony Liguori68d98d32012-06-25 14:36:33 -05002736 return 0;
2737 }
2738
Marcel Apfelbaumb0ddb8b2014-07-18 19:32:37 +03002739 qom_name = g_strdup(name);
2740 c = qom_name;
2741 while (*c++) {
2742 if (*c == '_') {
2743 *c = '-';
2744 }
2745 }
2746
Marcel Apfelbaum2e168982014-12-16 16:58:05 +00002747 object_property_parse(obj, value, qom_name, &local_err);
Marcel Apfelbaumb0ddb8b2014-07-18 19:32:37 +03002748 g_free(qom_name);
Anthony Liguori68d98d32012-06-25 14:36:33 -05002749
2750 if (local_err) {
Markus Armbrustera720a392015-02-10 15:12:59 +01002751 error_report_err(local_err);
Anthony Liguori68d98d32012-06-25 14:36:33 -05002752 return -1;
2753 }
2754
2755 return 0;
2756}
2757
Daniel P. Berrangef08f9272015-05-13 17:14:04 +01002758
2759/*
2760 * Initial object creation happens before all other
2761 * QEMU data types are created. The majority of objects
2762 * can be created at this point. The rng-egd object
2763 * cannot be created here, as it depends on the chardev
2764 * already existing.
2765 */
2766static bool object_create_initial(const char *type)
2767{
2768 if (g_str_equal(type, "rng-egd")) {
2769 return false;
2770 }
Yang Hongyang7dbb11c2015-10-07 11:52:21 +08002771
Thomas Huth9d3e12e2015-10-13 12:40:01 +02002772 /*
2773 * return false for concrete netfilters since
2774 * they depend on netdevs already existing
2775 */
2776 if (g_str_equal(type, "filter-buffer") ||
2777 g_str_equal(type, "filter-dump")) {
Yang Hongyang7dbb11c2015-10-07 11:52:21 +08002778 return false;
2779 }
2780
Daniel P. Berrangef08f9272015-05-13 17:14:04 +01002781 return true;
2782}
2783
2784
2785/*
2786 * The remainder of object creation happens after the
Yang Hongyang9abce562015-10-07 11:52:13 +08002787 * creation of chardev, fsdev, net clients and device data types.
Daniel P. Berrangef08f9272015-05-13 17:14:04 +01002788 */
2789static bool object_create_delayed(const char *type)
2790{
2791 return !object_create_initial(type);
2792}
2793
2794
Markus Armbruster28d0de72015-03-13 13:35:14 +01002795static int object_create(void *opaque, QemuOpts *opts, Error **errp)
Anthony Liguori68d98d32012-06-25 14:36:33 -05002796{
Paolo Bonzinic4090f82014-06-10 19:15:15 +08002797 Error *err = NULL;
2798 char *type = NULL;
2799 char *id = NULL;
2800 void *dummy = NULL;
2801 OptsVisitor *ov;
2802 QDict *pdict;
Daniel P. Berrangef08f9272015-05-13 17:14:04 +01002803 bool (*type_predicate)(const char *) = opaque;
Anthony Liguori68d98d32012-06-25 14:36:33 -05002804
Paolo Bonzinic4090f82014-06-10 19:15:15 +08002805 ov = opts_visitor_new(opts);
2806 pdict = qemu_opts_to_qdict(opts, NULL);
Anthony Liguori68d98d32012-06-25 14:36:33 -05002807
Paolo Bonzinic4090f82014-06-10 19:15:15 +08002808 visit_start_struct(opts_get_visitor(ov), &dummy, NULL, NULL, 0, &err);
2809 if (err) {
Igor Mammedov269e09f2014-01-16 17:34:38 +01002810 goto out;
2811 }
2812
Paolo Bonzinic4090f82014-06-10 19:15:15 +08002813 qdict_del(pdict, "qom-type");
2814 visit_type_str(opts_get_visitor(ov), &type, "qom-type", &err);
2815 if (err) {
Igor Mammedova790f4e2014-06-02 15:24:59 +02002816 goto out;
2817 }
Daniel P. Berrangef08f9272015-05-13 17:14:04 +01002818 if (!type_predicate(type)) {
2819 goto out;
2820 }
Paolo Bonzinic4090f82014-06-10 19:15:15 +08002821
2822 qdict_del(pdict, "id");
2823 visit_type_str(opts_get_visitor(ov), &id, "id", &err);
2824 if (err) {
2825 goto out;
2826 }
2827
2828 object_add(type, id, pdict, opts_get_visitor(ov), &err);
2829 if (err) {
2830 goto out;
2831 }
2832 visit_end_struct(opts_get_visitor(ov), &err);
2833 if (err) {
2834 qmp_object_del(id, NULL);
2835 }
2836
Igor Mammedov269e09f2014-01-16 17:34:38 +01002837out:
Paolo Bonzinic4090f82014-06-10 19:15:15 +08002838 opts_visitor_cleanup(ov);
2839
2840 QDECREF(pdict);
2841 g_free(id);
2842 g_free(type);
2843 g_free(dummy);
2844 if (err) {
Markus Armbrustera720a392015-02-10 15:12:59 +01002845 error_report_err(err);
Igor Mammedov90e9cf22014-01-16 17:34:37 +01002846 return -1;
2847 }
Anthony Liguori68d98d32012-06-25 14:36:33 -05002848 return 0;
2849}
2850
Nikunj A Dadhania076b35b2015-05-07 15:33:57 +10002851static void set_memory_options(uint64_t *ram_slots, ram_addr_t *maxram_size,
2852 MachineClass *mc)
Marcel Apfelbaum3b9985e2015-01-11 12:38:43 +02002853{
2854 uint64_t sz;
2855 const char *mem_str;
2856 const char *maxmem_str, *slots_str;
Nikunj A Dadhania076b35b2015-05-07 15:33:57 +10002857 const ram_addr_t default_ram_size = mc->default_ram_size;
Marcel Apfelbaum3b9985e2015-01-11 12:38:43 +02002858 QemuOpts *opts = qemu_find_opts_singleton("memory");
2859
2860 sz = 0;
2861 mem_str = qemu_opt_get(opts, "size");
2862 if (mem_str) {
2863 if (!*mem_str) {
2864 error_report("missing 'size' option value");
2865 exit(EXIT_FAILURE);
2866 }
2867
2868 sz = qemu_opt_get_size(opts, "size", ram_size);
2869
2870 /* Fix up legacy suffix-less format */
2871 if (g_ascii_isdigit(mem_str[strlen(mem_str) - 1])) {
2872 uint64_t overflow_check = sz;
2873
2874 sz <<= 20;
2875 if ((sz >> 20) != overflow_check) {
2876 error_report("too large 'size' option value");
2877 exit(EXIT_FAILURE);
2878 }
2879 }
2880 }
2881
2882 /* backward compatibility behaviour for case "-m 0" */
2883 if (sz == 0) {
2884 sz = default_ram_size;
2885 }
2886
2887 sz = QEMU_ALIGN_UP(sz, 8192);
2888 ram_size = sz;
2889 if (ram_size != sz) {
2890 error_report("ram size too large");
2891 exit(EXIT_FAILURE);
2892 }
2893
2894 /* store value for the future use */
Markus Armbruster39101f22015-02-12 16:46:36 +01002895 qemu_opt_set_number(opts, "size", ram_size, &error_abort);
Marcel Apfelbaum3b9985e2015-01-11 12:38:43 +02002896 *maxram_size = ram_size;
2897
2898 maxmem_str = qemu_opt_get(opts, "maxmem");
2899 slots_str = qemu_opt_get(opts, "slots");
2900 if (maxmem_str && slots_str) {
2901 uint64_t slots;
2902
2903 sz = qemu_opt_get_size(opts, "maxmem", 0);
Marcel Apfelbaum3b9985e2015-01-11 12:38:43 +02002904 slots = qemu_opt_get_number(opts, "slots", 0);
Peter Krempa214224a2015-01-29 14:48:40 +01002905 if (sz < ram_size) {
2906 error_report("invalid value of -m option maxmem: "
2907 "maximum memory size (0x%" PRIx64 ") must be at least "
2908 "the initial memory size (0x" RAM_ADDR_FMT ")",
2909 sz, ram_size);
2910 exit(EXIT_FAILURE);
2911 } else if (sz > ram_size) {
2912 if (!slots) {
2913 error_report("invalid value of -m option: maxmem was "
2914 "specified, but no hotplug slots were specified");
2915 exit(EXIT_FAILURE);
2916 }
2917 } else if (slots) {
2918 error_report("invalid value of -m option maxmem: "
2919 "memory slots were specified but maximum memory size "
2920 "(0x%" PRIx64 ") is equal to the initial memory size "
2921 "(0x" RAM_ADDR_FMT ")", sz, ram_size);
Marcel Apfelbaum3b9985e2015-01-11 12:38:43 +02002922 exit(EXIT_FAILURE);
2923 }
2924
Marcel Apfelbaum3b9985e2015-01-11 12:38:43 +02002925 *maxram_size = sz;
2926 *ram_slots = slots;
2927 } else if ((!maxmem_str && slots_str) ||
2928 (maxmem_str && !slots_str)) {
2929 error_report("invalid -m option value: missing "
2930 "'%s' option", slots_str ? "maxmem" : "slots");
2931 exit(EXIT_FAILURE);
2932 }
2933}
2934
malc902b3d52008-12-10 19:18:40 +00002935int main(int argc, char **argv, char **envp)
bellard0824d6f2003-06-24 13:42:40 +00002936{
thse4bcb142007-12-02 04:51:10 +00002937 int i;
Eduardo Habkostda1fcfd2010-04-06 19:22:07 -03002938 int snapshot, linux_boot;
bellard7f7f9872003-10-30 01:11:23 +00002939 const char *initrd_filename;
bellarda20dd502003-09-30 21:07:02 +00002940 const char *kernel_filename, *kernel_cmdline;
Gongleif05f47b2015-02-03 11:31:09 +00002941 const char *boot_order = NULL;
2942 const char *boot_once = NULL;
aliguori3023f332009-01-16 19:04:14 +00002943 DisplayState *ds;
malc9f227bc2012-08-27 18:33:22 +04002944 int cyls, heads, secs, translation;
Sebastian Tanase1ad95802014-07-25 11:56:28 +02002945 QemuOpts *hda_opts = NULL, *opts, *machine_opts, *icount_opts = NULL;
Gerd Hoffmann03b0ba72010-08-20 13:52:02 +02002946 QemuOptsList *olist;
bellardcd6f1162004-05-13 22:02:20 +00002947 int optind;
Anthony Liguori6530a972010-01-22 09:18:06 -06002948 const char *optarg;
bellardd63d3072004-10-03 13:29:03 +00002949 const char *loadvm = NULL;
Marcel Apfelbaum0056ae22014-03-05 19:30:47 +02002950 MachineClass *machine_class;
j_mayer94fc95c2007-03-05 19:44:02 +00002951 const char *cpu_model;
Mark Wud44229c2014-03-10 22:37:40 +08002952 const char *vga_model = NULL;
Paolo Bonzinid4fce242013-10-18 13:51:11 +02002953 const char *qtest_chrdev = NULL;
2954 const char *qtest_log = NULL;
ths93815bc2007-03-19 15:58:31 +00002955 const char *pid_file = NULL;
aliguori5bb79102008-10-13 03:12:02 +00002956 const char *incoming = NULL;
Jes Sorensen821601e2011-03-16 13:33:36 +01002957#ifdef CONFIG_VNC
Anthony Liguori993fbfd2009-05-21 16:54:00 -05002958 int show_vnc_port = 0;
Jes Sorensen821601e2011-03-16 13:33:36 +01002959#endif
Eduardo Habkost3ed2d9e2012-05-02 13:07:28 -03002960 bool defconfig = true;
Eduardo Habkostf29a5612012-05-02 13:07:29 -03002961 bool userconfig = true;
Matthew Fernandezc235d732011-06-07 16:32:40 +00002962 const char *log_mask = NULL;
2963 const char *log_file = NULL;
LluĂ­s23d15e82011-08-31 20:31:31 +02002964 const char *trace_events = NULL;
LluĂ­se4858972011-08-31 20:31:03 +02002965 const char *trace_file = NULL;
Marcel Apfelbaum3b9985e2015-01-11 12:38:43 +02002966 ram_addr_t maxram_size;
Igor Mammedovc270fb92014-06-02 15:25:02 +02002967 uint64_t ram_slots = 0;
Amit Shahabfd9ce2014-06-20 18:56:08 +05302968 FILE *vmstate_dump_file = NULL;
Chrysostomos Nanakos2f78e492014-09-18 14:30:49 +03002969 Error *main_loop_err = NULL;
Daniel P. Berrangeddbb0d02015-07-01 18:10:29 +01002970 Error *err = NULL;
Stefan Hajnoczi0b5538c2011-02-26 18:38:39 +00002971
Paolo Bonzini576a94d2015-03-03 11:36:09 +01002972 qemu_init_cpu_loop();
2973 qemu_mutex_lock_iothread();
2974
Gerd Hoffmannfd42dee2010-06-04 14:08:07 +02002975 atexit(qemu_run_exit_notifiers);
Markus Armbruster65abca02010-02-24 14:37:14 +01002976 error_set_progname(argv[0]);
Fam Zheng10f5bff2014-02-10 14:48:51 +08002977 qemu_init_exec_dir(argv[0]);
Markus Armbruster65abca02010-02-24 14:37:14 +01002978
Andreas Färber1b71f7c2012-03-04 21:32:35 +01002979 module_call_init(MODULE_INIT_QOM);
2980
Paolo Bonzini4d454572012-11-26 16:03:42 +01002981 qemu_add_opts(&qemu_drive_opts);
Amos Kong968854c2013-11-09 12:15:47 +08002982 qemu_add_drive_opts(&qemu_legacy_drive_opts);
2983 qemu_add_drive_opts(&qemu_common_drive_opts);
2984 qemu_add_drive_opts(&qemu_drive_opts);
Paolo Bonzini4d454572012-11-26 16:03:42 +01002985 qemu_add_opts(&qemu_chardev_opts);
2986 qemu_add_opts(&qemu_device_opts);
2987 qemu_add_opts(&qemu_netdev_opts);
2988 qemu_add_opts(&qemu_net_opts);
2989 qemu_add_opts(&qemu_rtc_opts);
2990 qemu_add_opts(&qemu_global_opts);
2991 qemu_add_opts(&qemu_mon_opts);
2992 qemu_add_opts(&qemu_trace_opts);
2993 qemu_add_opts(&qemu_option_rom_opts);
2994 qemu_add_opts(&qemu_machine_opts);
Igor Mammedov6e1d3c12013-11-27 01:27:35 +01002995 qemu_add_opts(&qemu_mem_opts);
Michael Tokarev12b7f572013-06-24 15:06:52 +04002996 qemu_add_opts(&qemu_smp_opts);
Paolo Bonzini4d454572012-11-26 16:03:42 +01002997 qemu_add_opts(&qemu_boot_opts);
2998 qemu_add_opts(&qemu_sandbox_opts);
2999 qemu_add_opts(&qemu_add_fd_opts);
3000 qemu_add_opts(&qemu_object_opts);
Stefan Bergerd1a0cf72013-02-27 12:47:49 -05003001 qemu_add_opts(&qemu_tpmdev_opts);
Satoru Moriya888a6bc2013-04-19 16:42:06 +02003002 qemu_add_opts(&qemu_realtime_opts);
Seiji Aguchi5e2ac512013-07-03 23:02:46 -04003003 qemu_add_opts(&qemu_msg_opts);
Dr. David Alan Gilbert5d12f962014-01-30 10:20:30 +00003004 qemu_add_opts(&qemu_name_opts);
Wanlong Gao00421092014-05-14 17:43:08 +08003005 qemu_add_opts(&qemu_numa_opts);
Sebastian Tanase1ad95802014-07-25 11:56:28 +02003006 qemu_add_opts(&qemu_icount_opts);
Liviu Ionescua38bb072014-12-11 12:07:48 +00003007 qemu_add_opts(&qemu_semihosting_config_opts);
Gabriel L. Somlo81b2b812015-04-29 11:21:53 -04003008 qemu_add_opts(&qemu_fw_cfg_opts);
Paolo Bonzini4d454572012-11-26 16:03:42 +01003009
Luiz Capitulino5db9d4d2011-09-05 16:36:31 -03003010 runstate_init();
3011
Daniel P. Berrangeddbb0d02015-07-01 18:10:29 +01003012 if (qcrypto_init(&err) < 0) {
3013 fprintf(stderr, "Cannot initialize crypto: %s\n",
3014 error_get_pretty(err));
3015 exit(1);
3016 }
Alex Bligh884f17c2013-08-21 16:03:04 +01003017 rtc_clock = QEMU_CLOCK_HOST;
Jan Kiszka68752042009-09-15 13:36:04 +02003018
Blue Swirl72cf2d42009-09-12 07:36:22 +00003019 QLIST_INIT (&vm_change_state_head);
Jes Sorensenfe98ac12010-06-10 11:42:21 +02003020 os_setup_early_signal_handling();
bellardbe995c22006-06-25 16:25:21 +00003021
Anthony Liguorif80f9ec2009-05-20 18:38:09 -05003022 module_call_init(MODULE_INIT_MACHINE);
Marcel Apfelbaum0056ae22014-03-05 19:30:47 +02003023 machine_class = find_default_machine();
j_mayer94fc95c2007-03-05 19:44:02 +00003024 cpu_model = NULL;
bellard33e39632003-07-06 17:15:21 +00003025 snapshot = 0;
malc9f227bc2012-08-27 18:33:22 +04003026 cyls = heads = secs = 0;
3027 translation = BIOS_ATA_TRANSLATION_AUTO;
bellardc4b1fcc2004-03-14 21:44:30 +00003028
bellard7c9d8e02005-11-15 22:16:05 +00003029 nb_nics = 0;
ths3b46e622007-09-17 08:09:54 +00003030
Peter Lieven142c6b12013-03-21 13:07:10 +01003031 bdrv_init_with_whitelist();
3032
Kewei Yu2c02d1a2013-12-31 18:36:08 +08003033 autostart = 1;
blueswir141bd6392008-10-05 09:56:21 +00003034
Anthony Liguori292444c2010-01-21 10:57:58 -06003035 /* first pass of option parsing */
3036 optind = 1;
3037 while (optind < argc) {
3038 if (argv[optind][0] != '-') {
3039 /* disk image */
Anthony Liguori28e68d62010-01-27 10:46:00 -06003040 optind++;
Anthony Liguori292444c2010-01-21 10:57:58 -06003041 } else {
3042 const QEMUOption *popt;
3043
3044 popt = lookup_opt(argc, argv, &optarg, &optind);
3045 switch (popt->index) {
3046 case QEMU_OPTION_nodefconfig:
Eduardo Habkost3ed2d9e2012-05-02 13:07:28 -03003047 defconfig = false;
Anthony Liguori292444c2010-01-21 10:57:58 -06003048 break;
Eduardo Habkostf29a5612012-05-02 13:07:29 -03003049 case QEMU_OPTION_nouserconfig:
3050 userconfig = false;
3051 break;
Anthony Liguori292444c2010-01-21 10:57:58 -06003052 }
3053 }
3054 }
3055
3056 if (defconfig) {
Kevin Wolfdcfb0932010-03-05 17:25:55 +01003057 int ret;
Eduardo Habkostf29a5612012-05-02 13:07:29 -03003058 ret = qemu_read_default_config_files(userconfig);
Eduardo Habkostb5a8fe52012-05-02 13:07:25 -03003059 if (ret < 0) {
Kevin Wolfdcfb0932010-03-05 17:25:55 +01003060 exit(1);
Anthony Liguori292444c2010-01-21 10:57:58 -06003061 }
3062 }
3063
3064 /* second pass of option parsing */
bellardcd6f1162004-05-13 22:02:20 +00003065 optind = 1;
bellard0824d6f2003-06-24 13:42:40 +00003066 for(;;) {
bellardcd6f1162004-05-13 22:02:20 +00003067 if (optind >= argc)
bellard0824d6f2003-06-24 13:42:40 +00003068 break;
Anthony Liguori6530a972010-01-22 09:18:06 -06003069 if (argv[optind][0] != '-') {
Marcel Apfelbaum99efa842015-01-11 12:56:51 +02003070 hda_opts = drive_add(IF_DEFAULT, 0, argv[optind++], HD_OPTS);
bellardcd6f1162004-05-13 22:02:20 +00003071 } else {
3072 const QEMUOption *popt;
3073
Anthony Liguori6530a972010-01-22 09:18:06 -06003074 popt = lookup_opt(argc, argv, &optarg, &optind);
Blue Swirlad960902010-03-29 19:23:52 +00003075 if (!(popt->arch_mask & arch_type)) {
3076 printf("Option %s not supported for this target\n", popt->name);
3077 exit(1);
3078 }
bellardcd6f1162004-05-13 22:02:20 +00003079 switch(popt->index) {
Jan Kiszkae43d5942012-10-05 14:51:40 -03003080 case QEMU_OPTION_no_kvm_irqchip: {
3081 olist = qemu_find_opts("machine");
Markus Armbruster70b94332015-02-13 12:50:26 +01003082 qemu_opts_parse_noisily(olist, "kernel_irqchip=off", false);
Jan Kiszkae43d5942012-10-05 14:51:40 -03003083 break;
3084 }
j_mayer94fc95c2007-03-05 19:44:02 +00003085 case QEMU_OPTION_cpu:
3086 /* hw initialization will check this */
Eduardo Habkostecf40be2012-03-09 16:19:07 -03003087 cpu_model = optarg;
j_mayer94fc95c2007-03-05 19:44:02 +00003088 break;
bellardcd6f1162004-05-13 22:02:20 +00003089 case QEMU_OPTION_hda:
malc9f227bc2012-08-27 18:33:22 +04003090 {
3091 char buf[256];
3092 if (cyls == 0)
3093 snprintf(buf, sizeof(buf), "%s", HD_OPTS);
3094 else
3095 snprintf(buf, sizeof(buf),
3096 "%s,cyls=%d,heads=%d,secs=%d%s",
3097 HD_OPTS , cyls, heads, secs,
3098 translation == BIOS_ATA_TRANSLATION_LBA ?
3099 ",trans=lba" :
3100 translation == BIOS_ATA_TRANSLATION_NONE ?
3101 ",trans=none" : "");
3102 drive_add(IF_DEFAULT, 0, optarg, buf);
3103 break;
3104 }
bellardcd6f1162004-05-13 22:02:20 +00003105 case QEMU_OPTION_hdb:
bellardcc1daa42005-06-05 14:49:17 +00003106 case QEMU_OPTION_hdc:
3107 case QEMU_OPTION_hdd:
Markus Armbruster2292dda2011-01-28 11:21:41 +01003108 drive_add(IF_DEFAULT, popt->index - QEMU_OPTION_hda, optarg,
3109 HD_OPTS);
bellardfc01f7e2003-06-30 10:03:06 +00003110 break;
thse4bcb142007-12-02 04:51:10 +00003111 case QEMU_OPTION_drive:
Michael Tokareve2982c32011-03-30 16:31:05 +04003112 if (drive_def(optarg) == NULL) {
3113 exit(1);
3114 }
Marcel Apfelbaum99efa842015-01-11 12:56:51 +02003115 break;
Gerd Hoffmannd058fe02009-07-31 12:25:36 +02003116 case QEMU_OPTION_set:
3117 if (qemu_set_option(optarg) != 0)
3118 exit(1);
Marcel Apfelbaum99efa842015-01-11 12:56:51 +02003119 break;
Gerd Hoffmannd0fef6f2009-12-08 13:11:34 +01003120 case QEMU_OPTION_global:
3121 if (qemu_global_option(optarg) != 0)
3122 exit(1);
Marcel Apfelbaum99efa842015-01-11 12:56:51 +02003123 break;
balrog3e3d5812007-04-30 02:09:25 +00003124 case QEMU_OPTION_mtdblock:
Markus Armbruster2292dda2011-01-28 11:21:41 +01003125 drive_add(IF_MTD, -1, optarg, MTD_OPTS);
balrog3e3d5812007-04-30 02:09:25 +00003126 break;
pbrooka1bb27b2007-04-06 16:49:48 +00003127 case QEMU_OPTION_sd:
Peter Crosthwaite80f4d9f2013-02-28 18:23:14 +00003128 drive_add(IF_SD, -1, optarg, SD_OPTS);
pbrooka1bb27b2007-04-06 16:49:48 +00003129 break;
j_mayer86f55662007-04-24 06:52:59 +00003130 case QEMU_OPTION_pflash:
Markus Armbruster2292dda2011-01-28 11:21:41 +01003131 drive_add(IF_PFLASH, -1, optarg, PFLASH_OPTS);
j_mayer86f55662007-04-24 06:52:59 +00003132 break;
bellardcd6f1162004-05-13 22:02:20 +00003133 case QEMU_OPTION_snapshot:
bellard33e39632003-07-06 17:15:21 +00003134 snapshot = 1;
3135 break;
bellardcd6f1162004-05-13 22:02:20 +00003136 case QEMU_OPTION_hdachs:
bellard330d0412003-07-26 18:11:40 +00003137 {
bellard330d0412003-07-26 18:11:40 +00003138 const char *p;
3139 p = optarg;
3140 cyls = strtol(p, (char **)&p, 0);
bellard46d47672004-11-16 01:45:27 +00003141 if (cyls < 1 || cyls > 16383)
3142 goto chs_fail;
bellard330d0412003-07-26 18:11:40 +00003143 if (*p != ',')
3144 goto chs_fail;
3145 p++;
3146 heads = strtol(p, (char **)&p, 0);
bellard46d47672004-11-16 01:45:27 +00003147 if (heads < 1 || heads > 16)
3148 goto chs_fail;
bellard330d0412003-07-26 18:11:40 +00003149 if (*p != ',')
3150 goto chs_fail;
3151 p++;
3152 secs = strtol(p, (char **)&p, 0);
bellard46d47672004-11-16 01:45:27 +00003153 if (secs < 1 || secs > 63)
3154 goto chs_fail;
3155 if (*p == ',') {
3156 p++;
Paolo Bonzinif31c41f2014-02-08 11:01:54 +01003157 if (!strcmp(p, "large")) {
3158 translation = BIOS_ATA_TRANSLATION_LARGE;
3159 } else if (!strcmp(p, "rechs")) {
3160 translation = BIOS_ATA_TRANSLATION_RECHS;
3161 } else if (!strcmp(p, "none")) {
bellard46d47672004-11-16 01:45:27 +00003162 translation = BIOS_ATA_TRANSLATION_NONE;
Paolo Bonzinif31c41f2014-02-08 11:01:54 +01003163 } else if (!strcmp(p, "lba")) {
bellard46d47672004-11-16 01:45:27 +00003164 translation = BIOS_ATA_TRANSLATION_LBA;
Paolo Bonzinif31c41f2014-02-08 11:01:54 +01003165 } else if (!strcmp(p, "auto")) {
bellard46d47672004-11-16 01:45:27 +00003166 translation = BIOS_ATA_TRANSLATION_AUTO;
Paolo Bonzinif31c41f2014-02-08 11:01:54 +01003167 } else {
bellard46d47672004-11-16 01:45:27 +00003168 goto chs_fail;
Paolo Bonzinif31c41f2014-02-08 11:01:54 +01003169 }
bellard46d47672004-11-16 01:45:27 +00003170 } else if (*p != '\0') {
bellardc4b1fcc2004-03-14 21:44:30 +00003171 chs_fail:
bellard46d47672004-11-16 01:45:27 +00003172 fprintf(stderr, "qemu: invalid physical CHS format\n");
3173 exit(1);
bellardc4b1fcc2004-03-14 21:44:30 +00003174 }
Marcel Apfelbaum99efa842015-01-11 12:56:51 +02003175 if (hda_opts != NULL) {
Markus Armbrustera8b18f82015-02-13 15:50:43 +01003176 qemu_opt_set_number(hda_opts, "cyls", cyls,
3177 &error_abort);
3178 qemu_opt_set_number(hda_opts, "heads", heads,
3179 &error_abort);
3180 qemu_opt_set_number(hda_opts, "secs", secs,
3181 &error_abort);
Paolo Bonzinif31c41f2014-02-08 11:01:54 +01003182 if (translation == BIOS_ATA_TRANSLATION_LARGE) {
Markus Armbrusterf43e47d2015-02-12 17:52:20 +01003183 qemu_opt_set(hda_opts, "trans", "large",
3184 &error_abort);
Paolo Bonzinif31c41f2014-02-08 11:01:54 +01003185 } else if (translation == BIOS_ATA_TRANSLATION_RECHS) {
Markus Armbrusterf43e47d2015-02-12 17:52:20 +01003186 qemu_opt_set(hda_opts, "trans", "rechs",
3187 &error_abort);
Paolo Bonzinif31c41f2014-02-08 11:01:54 +01003188 } else if (translation == BIOS_ATA_TRANSLATION_LBA) {
Markus Armbrusterf43e47d2015-02-12 17:52:20 +01003189 qemu_opt_set(hda_opts, "trans", "lba",
3190 &error_abort);
Paolo Bonzinif31c41f2014-02-08 11:01:54 +01003191 } else if (translation == BIOS_ATA_TRANSLATION_NONE) {
Markus Armbrusterf43e47d2015-02-12 17:52:20 +01003192 qemu_opt_set(hda_opts, "trans", "none",
3193 &error_abort);
Paolo Bonzinif31c41f2014-02-08 11:01:54 +01003194 }
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +02003195 }
bellard330d0412003-07-26 18:11:40 +00003196 }
3197 break;
aliguori268a3622009-04-21 22:30:27 +00003198 case QEMU_OPTION_numa:
Markus Armbruster70b94332015-02-13 12:50:26 +01003199 opts = qemu_opts_parse_noisily(qemu_find_opts("numa"),
3200 optarg, true);
Wanlong Gao00421092014-05-14 17:43:08 +08003201 if (!opts) {
3202 exit(1);
3203 }
aliguori268a3622009-04-21 22:30:27 +00003204 break;
Jes Sorensen1472a952011-03-16 13:33:31 +01003205 case QEMU_OPTION_display:
3206 display_type = select_display(optarg);
3207 break;
bellardcd6f1162004-05-13 22:02:20 +00003208 case QEMU_OPTION_nographic:
Anthony Liguori993fbfd2009-05-21 16:54:00 -05003209 display_type = DT_NOGRAPHIC;
bellarda20dd502003-09-30 21:07:02 +00003210 break;
balrog4d3b6f62008-02-10 16:33:14 +00003211 case QEMU_OPTION_curses:
Jes Sorensen47b05362011-03-16 13:33:35 +01003212#ifdef CONFIG_CURSES
Anthony Liguori993fbfd2009-05-21 16:54:00 -05003213 display_type = DT_CURSES;
Jes Sorensen47b05362011-03-16 13:33:35 +01003214#else
3215 fprintf(stderr, "Curses support is disabled\n");
3216 exit(1);
balrog4d3b6f62008-02-10 16:33:14 +00003217#endif
Jes Sorensen47b05362011-03-16 13:33:35 +01003218 break;
balroga171fe32007-04-30 01:48:07 +00003219 case QEMU_OPTION_portrait:
Vasily Khoruzhick93128052011-06-17 13:04:36 +03003220 graphic_rotate = 90;
3221 break;
3222 case QEMU_OPTION_rotate:
3223 graphic_rotate = strtol(optarg, (char **) &optarg, 10);
3224 if (graphic_rotate != 0 && graphic_rotate != 90 &&
3225 graphic_rotate != 180 && graphic_rotate != 270) {
3226 fprintf(stderr,
3227 "qemu: only 90, 180, 270 deg rotation is available\n");
3228 exit(1);
3229 }
balroga171fe32007-04-30 01:48:07 +00003230 break;
bellardcd6f1162004-05-13 22:02:20 +00003231 case QEMU_OPTION_kernel:
Markus Armbruster79087c72015-02-12 17:07:34 +01003232 qemu_opts_set(qemu_find_opts("machine"), 0, "kernel", optarg,
3233 &error_abort);
Peter Maydella0abe472012-02-08 05:41:39 +00003234 break;
3235 case QEMU_OPTION_initrd:
Markus Armbruster79087c72015-02-12 17:07:34 +01003236 qemu_opts_set(qemu_find_opts("machine"), 0, "initrd", optarg,
3237 &error_abort);
bellarda20dd502003-09-30 21:07:02 +00003238 break;
bellardcd6f1162004-05-13 22:02:20 +00003239 case QEMU_OPTION_append:
Markus Armbruster79087c72015-02-12 17:07:34 +01003240 qemu_opts_set(qemu_find_opts("machine"), 0, "append", optarg,
3241 &error_abort);
bellard313aa562003-08-10 21:52:11 +00003242 break;
Grant Likely412beee2012-03-02 11:56:38 +00003243 case QEMU_OPTION_dtb:
Markus Armbruster79087c72015-02-12 17:07:34 +01003244 qemu_opts_set(qemu_find_opts("machine"), 0, "dtb", optarg,
3245 &error_abort);
Grant Likely412beee2012-03-02 11:56:38 +00003246 break;
bellardcd6f1162004-05-13 22:02:20 +00003247 case QEMU_OPTION_cdrom:
Markus Armbruster2292dda2011-01-28 11:21:41 +01003248 drive_add(IF_DEFAULT, 2, optarg, CDROM_OPTS);
bellard36b486b2003-11-11 13:36:08 +00003249 break;
bellardcd6f1162004-05-13 22:02:20 +00003250 case QEMU_OPTION_boot:
Markus Armbruster70b94332015-02-13 12:50:26 +01003251 opts = qemu_opts_parse_noisily(qemu_find_opts("boot-opts"),
3252 optarg, true);
Markus Armbruster8281abd2013-06-14 13:15:03 +02003253 if (!opts) {
3254 exit(1);
bellard36b486b2003-11-11 13:36:08 +00003255 }
3256 break;
bellardcd6f1162004-05-13 22:02:20 +00003257 case QEMU_OPTION_fda:
bellardcd6f1162004-05-13 22:02:20 +00003258 case QEMU_OPTION_fdb:
Markus Armbruster2292dda2011-01-28 11:21:41 +01003259 drive_add(IF_FLOPPY, popt->index - QEMU_OPTION_fda,
3260 optarg, FD_OPTS);
bellardc45886d2004-01-05 00:02:06 +00003261 break;
bellard52ca8d62006-06-14 16:03:05 +00003262 case QEMU_OPTION_no_fd_bootchk:
3263 fd_bootchk = 0;
3264 break;
Mark McLoughlina1ea4582009-10-08 19:58:26 +01003265 case QEMU_OPTION_netdev:
Gerd Hoffmann3329f072010-08-20 13:52:01 +02003266 if (net_client_parse(qemu_find_opts("netdev"), optarg) == -1) {
Mark McLoughlina1ea4582009-10-08 19:58:26 +01003267 exit(1);
3268 }
3269 break;
bellard7c9d8e02005-11-15 22:16:05 +00003270 case QEMU_OPTION_net:
Gerd Hoffmann3329f072010-08-20 13:52:01 +02003271 if (net_client_parse(qemu_find_opts("net"), optarg) == -1) {
bellardc4b1fcc2004-03-14 21:44:30 +00003272 exit(1);
3273 }
bellard702c6512004-04-02 21:21:32 +00003274 break;
Ronnie Sahlbergf9dadc92012-01-26 09:39:02 +11003275#ifdef CONFIG_LIBISCSI
3276 case QEMU_OPTION_iscsi:
Markus Armbruster70b94332015-02-13 12:50:26 +01003277 opts = qemu_opts_parse_noisily(qemu_find_opts("iscsi"),
3278 optarg, false);
Ronnie Sahlbergf9dadc92012-01-26 09:39:02 +11003279 if (!opts) {
3280 exit(1);
3281 }
3282 break;
3283#endif
bellardc7f74642004-08-24 21:57:12 +00003284#ifdef CONFIG_SLIRP
3285 case QEMU_OPTION_tftp:
Jan Kiszkaad196a92009-06-24 14:42:28 +02003286 legacy_tftp_prefix = optarg;
bellard9bf05442004-08-25 22:12:49 +00003287 break;
ths47d5d012007-02-20 00:05:08 +00003288 case QEMU_OPTION_bootp:
Jan Kiszkaad196a92009-06-24 14:42:28 +02003289 legacy_bootp_filename = optarg;
ths47d5d012007-02-20 00:05:08 +00003290 break;
bellard9bf05442004-08-25 22:12:49 +00003291 case QEMU_OPTION_redir:
Markus Armbruster07527062009-10-06 12:16:57 +01003292 if (net_slirp_redir(optarg) < 0)
3293 exit(1);
bellard9bf05442004-08-25 22:12:49 +00003294 break;
bellardc7f74642004-08-24 21:57:12 +00003295#endif
balrogdc72ac12008-11-09 00:04:26 +00003296 case QEMU_OPTION_bt:
Gerd Hoffmannbd3c9482009-07-15 13:59:26 +02003297 add_device_config(DEV_BT, optarg);
balrogdc72ac12008-11-09 00:04:26 +00003298 break;
bellard1d14ffa2005-10-30 18:58:22 +00003299 case QEMU_OPTION_audio_help:
3300 AUD_help ();
3301 exit (0);
3302 break;
3303 case QEMU_OPTION_soundhw:
3304 select_soundhw (optarg);
3305 break;
bellardcd6f1162004-05-13 22:02:20 +00003306 case QEMU_OPTION_h:
ths15f82202007-06-29 23:26:08 +00003307 help(0);
bellardcd6f1162004-05-13 22:02:20 +00003308 break;
pbrook9bd7e6d2009-04-07 22:58:45 +00003309 case QEMU_OPTION_version:
3310 version();
3311 exit(0);
3312 break;
Marcel Apfelbaum3b9985e2015-01-11 12:38:43 +02003313 case QEMU_OPTION_m:
Markus Armbruster70b94332015-02-13 12:50:26 +01003314 opts = qemu_opts_parse_noisily(qemu_find_opts("memory"),
3315 optarg, true);
Igor Mammedov6e1d3c12013-11-27 01:27:35 +01003316 if (!opts) {
3317 exit(EXIT_FAILURE);
bellardcd6f1162004-05-13 22:02:20 +00003318 }
3319 break;
Stefan Bergerd1a0cf72013-02-27 12:47:49 -05003320#ifdef CONFIG_TPM
3321 case QEMU_OPTION_tpmdev:
3322 if (tpm_config_parse(qemu_find_opts("tpmdev"), optarg) < 0) {
3323 exit(1);
3324 }
3325 break;
3326#endif
Marcelo Tosattic9027602010-03-01 20:25:08 -03003327 case QEMU_OPTION_mempath:
3328 mem_path = optarg;
3329 break;
Marcelo Tosattic9027602010-03-01 20:25:08 -03003330 case QEMU_OPTION_mem_prealloc:
3331 mem_prealloc = 1;
3332 break;
bellardcd6f1162004-05-13 22:02:20 +00003333 case QEMU_OPTION_d:
Matthew Fernandezc235d732011-06-07 16:32:40 +00003334 log_mask = optarg;
3335 break;
3336 case QEMU_OPTION_D:
3337 log_file = optarg;
bellardcd6f1162004-05-13 22:02:20 +00003338 break;
bellardcd6f1162004-05-13 22:02:20 +00003339 case QEMU_OPTION_s:
Markus Armbrusteref0c4a02012-02-07 15:09:13 +01003340 add_device_config(DEV_GDB, "tcp::" DEFAULT_GDBSTUB_PORT);
bellardcd6f1162004-05-13 22:02:20 +00003341 break;
aliguori59030a82009-04-05 18:43:41 +00003342 case QEMU_OPTION_gdb:
Markus Armbrusteref0c4a02012-02-07 15:09:13 +01003343 add_device_config(DEV_GDB, optarg);
bellardcd6f1162004-05-13 22:02:20 +00003344 break;
bellardcd6f1162004-05-13 22:02:20 +00003345 case QEMU_OPTION_L:
Gerd Hoffmann45240512013-03-08 11:42:24 +01003346 if (data_dir_idx < ARRAY_SIZE(data_dir)) {
3347 data_dir[data_dir_idx++] = optarg;
3348 }
bellardcd6f1162004-05-13 22:02:20 +00003349 break;
j_mayer1192dad2007-10-05 13:08:35 +00003350 case QEMU_OPTION_bios:
Markus Armbruster79087c72015-02-12 17:07:34 +01003351 qemu_opts_set(qemu_find_opts("machine"), 0, "firmware", optarg,
3352 &error_abort);
j_mayer1192dad2007-10-05 13:08:35 +00003353 break;
aurel321b530a62009-04-05 20:08:59 +00003354 case QEMU_OPTION_singlestep:
3355 singlestep = 1;
3356 break;
bellardcd6f1162004-05-13 22:02:20 +00003357 case QEMU_OPTION_S:
pbrook3c07f8e2007-01-21 16:47:01 +00003358 autostart = 0;
bellardcd6f1162004-05-13 22:02:20 +00003359 break;
Marcel Apfelbaum99efa842015-01-11 12:56:51 +02003360 case QEMU_OPTION_k:
3361 keyboard_layout = optarg;
3362 break;
bellardee22c2f2004-06-03 12:49:50 +00003363 case QEMU_OPTION_localtime:
3364 rtc_utc = 0;
3365 break;
malc3893c122008-09-28 00:42:05 +00003366 case QEMU_OPTION_vga:
Blue Swirla369da52011-09-27 19:15:42 +00003367 vga_model = optarg;
Paolo Bonzini7f1b17f2012-05-10 09:39:17 +02003368 default_vga = 0;
bellard1bfe8562004-07-08 21:17:50 +00003369 break;
bellarde9b137c2004-06-21 16:46:10 +00003370 case QEMU_OPTION_g:
3371 {
3372 const char *p;
3373 int w, h, depth;
3374 p = optarg;
3375 w = strtol(p, (char **)&p, 10);
3376 if (w <= 0) {
3377 graphic_error:
3378 fprintf(stderr, "qemu: invalid resolution or depth\n");
3379 exit(1);
3380 }
3381 if (*p != 'x')
3382 goto graphic_error;
3383 p++;
3384 h = strtol(p, (char **)&p, 10);
3385 if (h <= 0)
3386 goto graphic_error;
3387 if (*p == 'x') {
3388 p++;
3389 depth = strtol(p, (char **)&p, 10);
ths5fafdf22007-09-16 21:08:06 +00003390 if (depth != 8 && depth != 15 && depth != 16 &&
bellarde9b137c2004-06-21 16:46:10 +00003391 depth != 24 && depth != 32)
3392 goto graphic_error;
3393 } else if (*p == '\0') {
3394 depth = graphic_depth;
3395 } else {
3396 goto graphic_error;
3397 }
ths3b46e622007-09-17 08:09:54 +00003398
bellarde9b137c2004-06-21 16:46:10 +00003399 graphic_width = w;
3400 graphic_height = h;
3401 graphic_depth = depth;
3402 }
3403 break;
ths20d8a3e2007-02-18 17:04:49 +00003404 case QEMU_OPTION_echr:
3405 {
3406 char *r;
3407 term_escape_char = strtol(optarg, &r, 0);
3408 if (r == optarg)
3409 printf("Bad argument to echr\n");
3410 break;
3411 }
bellard82c643f2004-07-14 17:28:13 +00003412 case QEMU_OPTION_monitor:
Gerd Hoffmann6ca55822009-12-08 13:11:52 +01003413 default_monitor = 0;
Luiz Capitulino70e098a2013-05-16 12:02:55 -04003414 if (strncmp(optarg, "none", 4)) {
Max Reitz4821cd42014-11-17 13:31:04 +01003415 monitor_parse(optarg, "readline", false);
Luiz Capitulino70e098a2013-05-16 12:02:55 -04003416 }
Gerd Hoffmann6ca55822009-12-08 13:11:52 +01003417 break;
3418 case QEMU_OPTION_qmp:
Max Reitz4821cd42014-11-17 13:31:04 +01003419 monitor_parse(optarg, "control", false);
3420 default_monitor = 0;
3421 break;
3422 case QEMU_OPTION_qmp_pretty:
3423 monitor_parse(optarg, "control", true);
Anthony Liguori2d114dc2010-03-21 14:14:38 -05003424 default_monitor = 0;
bellard82c643f2004-07-14 17:28:13 +00003425 break;
Gerd Hoffmann22a0e042009-12-08 13:11:51 +01003426 case QEMU_OPTION_mon:
Markus Armbruster70b94332015-02-13 12:50:26 +01003427 opts = qemu_opts_parse_noisily(qemu_find_opts("mon"), optarg,
3428 true);
Gerd Hoffmann22a0e042009-12-08 13:11:51 +01003429 if (!opts) {
Gerd Hoffmann22a0e042009-12-08 13:11:51 +01003430 exit(1);
3431 }
Anthony Liguori2d114dc2010-03-21 14:14:38 -05003432 default_monitor = 0;
Gerd Hoffmann22a0e042009-12-08 13:11:51 +01003433 break;
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003434 case QEMU_OPTION_chardev:
Markus Armbruster70b94332015-02-13 12:50:26 +01003435 opts = qemu_opts_parse_noisily(qemu_find_opts("chardev"),
3436 optarg, true);
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003437 if (!opts) {
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003438 exit(1);
3439 }
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003440 break;
Gautham R Shenoy74db9202010-04-29 17:44:43 +05303441 case QEMU_OPTION_fsdev:
Gerd Hoffmann03b0ba72010-08-20 13:52:02 +02003442 olist = qemu_find_opts("fsdev");
3443 if (!olist) {
3444 fprintf(stderr, "fsdev is not supported by this qemu build.\n");
3445 exit(1);
3446 }
Markus Armbruster70b94332015-02-13 12:50:26 +01003447 opts = qemu_opts_parse_noisily(olist, optarg, true);
Gautham R Shenoy74db9202010-04-29 17:44:43 +05303448 if (!opts) {
Gautham R Shenoy74db9202010-04-29 17:44:43 +05303449 exit(1);
3450 }
3451 break;
Gautham R Shenoy3d54abc2010-04-29 17:45:03 +05303452 case QEMU_OPTION_virtfs: {
Stefan Hajnoczie14ea472011-03-16 08:31:43 +00003453 QemuOpts *fsdev;
3454 QemuOpts *device;
M. Mohan Kumar84a87cc2011-12-14 13:58:47 +05303455 const char *writeout, *sock_fd, *socket;
Gautham R Shenoy3d54abc2010-04-29 17:45:03 +05303456
Gerd Hoffmann03b0ba72010-08-20 13:52:02 +02003457 olist = qemu_find_opts("virtfs");
3458 if (!olist) {
3459 fprintf(stderr, "virtfs is not supported by this qemu build.\n");
3460 exit(1);
3461 }
Markus Armbruster70b94332015-02-13 12:50:26 +01003462 opts = qemu_opts_parse_noisily(olist, optarg, true);
Gautham R Shenoy3d54abc2010-04-29 17:45:03 +05303463 if (!opts) {
Gautham R Shenoy3d54abc2010-04-29 17:45:03 +05303464 exit(1);
3465 }
3466
Aneesh Kumar K.Vfbcbf102011-10-13 12:28:04 +05303467 if (qemu_opt_get(opts, "fsdriver") == NULL ||
Aneesh Kumar K.V99519f02011-12-14 13:48:59 +05303468 qemu_opt_get(opts, "mount_tag") == NULL) {
3469 fprintf(stderr, "Usage: -virtfs fsdriver,mount_tag=tag.\n");
Venkateswararao Jujjuri (JV)9ce56db2010-06-14 13:34:40 -07003470 exit(1);
3471 }
Stefan Hajnoczie14ea472011-03-16 08:31:43 +00003472 fsdev = qemu_opts_create(qemu_find_opts("fsdev"),
Luiz Capitulino8be7e7e2012-03-20 15:51:57 -03003473 qemu_opt_get(opts, "mount_tag"),
3474 1, NULL);
Stefan Hajnoczie14ea472011-03-16 08:31:43 +00003475 if (!fsdev) {
3476 fprintf(stderr, "duplicate fsdev id: %s\n",
3477 qemu_opt_get(opts, "mount_tag"));
Gautham R Shenoy3d54abc2010-04-29 17:45:03 +05303478 exit(1);
3479 }
Aneesh Kumar K.Vd3ab98e2011-10-12 19:11:23 +05303480
3481 writeout = qemu_opt_get(opts, "writeout");
3482 if (writeout) {
3483#ifdef CONFIG_SYNC_FILE_RANGE
Markus Armbrusterf43e47d2015-02-12 17:52:20 +01003484 qemu_opt_set(fsdev, "writeout", writeout, &error_abort);
Aneesh Kumar K.Vd3ab98e2011-10-12 19:11:23 +05303485#else
3486 fprintf(stderr, "writeout=immediate not supported on "
3487 "this platform\n");
3488 exit(1);
3489#endif
3490 }
Markus Armbrusterf43e47d2015-02-12 17:52:20 +01003491 qemu_opt_set(fsdev, "fsdriver",
3492 qemu_opt_get(opts, "fsdriver"), &error_abort);
3493 qemu_opt_set(fsdev, "path", qemu_opt_get(opts, "path"),
3494 &error_abort);
Stefan Hajnoczie14ea472011-03-16 08:31:43 +00003495 qemu_opt_set(fsdev, "security_model",
Markus Armbrusterf43e47d2015-02-12 17:52:20 +01003496 qemu_opt_get(opts, "security_model"),
3497 &error_abort);
M. Mohan Kumar84a87cc2011-12-14 13:58:47 +05303498 socket = qemu_opt_get(opts, "socket");
3499 if (socket) {
Markus Armbrusterf43e47d2015-02-12 17:52:20 +01003500 qemu_opt_set(fsdev, "socket", socket, &error_abort);
M. Mohan Kumar84a87cc2011-12-14 13:58:47 +05303501 }
M. Mohan Kumar4c793dd2011-12-14 13:49:28 +05303502 sock_fd = qemu_opt_get(opts, "sock_fd");
3503 if (sock_fd) {
Markus Armbrusterf43e47d2015-02-12 17:52:20 +01003504 qemu_opt_set(fsdev, "sock_fd", sock_fd, &error_abort);
M. Mohan Kumar4c793dd2011-12-14 13:49:28 +05303505 }
Gautham R Shenoy3d54abc2010-04-29 17:45:03 +05303506
M. Mohan Kumar2c74c2c2011-10-25 12:10:39 +05303507 qemu_opt_set_bool(fsdev, "readonly",
Markus Armbrustercccb7962015-02-12 16:37:44 +01003508 qemu_opt_get_bool(opts, "readonly", 0),
3509 &error_abort);
Peter Crosthwaite87ea75d2014-01-01 18:49:17 -08003510 device = qemu_opts_create(qemu_find_opts("device"), NULL, 0,
3511 &error_abort);
Markus Armbrusterf43e47d2015-02-12 17:52:20 +01003512 qemu_opt_set(device, "driver", "virtio-9p-pci", &error_abort);
Stefan Hajnoczie14ea472011-03-16 08:31:43 +00003513 qemu_opt_set(device, "fsdev",
Markus Armbrusterf43e47d2015-02-12 17:52:20 +01003514 qemu_opt_get(opts, "mount_tag"), &error_abort);
Stefan Hajnoczie14ea472011-03-16 08:31:43 +00003515 qemu_opt_set(device, "mount_tag",
Markus Armbrusterf43e47d2015-02-12 17:52:20 +01003516 qemu_opt_get(opts, "mount_tag"), &error_abort);
Gautham R Shenoy3d54abc2010-04-29 17:45:03 +05303517 break;
3518 }
Aneesh Kumar K.V9db221a2011-10-25 12:10:40 +05303519 case QEMU_OPTION_virtfs_synth: {
3520 QemuOpts *fsdev;
3521 QemuOpts *device;
3522
Luiz Capitulino8be7e7e2012-03-20 15:51:57 -03003523 fsdev = qemu_opts_create(qemu_find_opts("fsdev"), "v_synth",
3524 1, NULL);
Aneesh Kumar K.V9db221a2011-10-25 12:10:40 +05303525 if (!fsdev) {
3526 fprintf(stderr, "duplicate option: %s\n", "virtfs_synth");
3527 exit(1);
3528 }
Markus Armbrusterf43e47d2015-02-12 17:52:20 +01003529 qemu_opt_set(fsdev, "fsdriver", "synth", &error_abort);
Aneesh Kumar K.V9db221a2011-10-25 12:10:40 +05303530
Peter Crosthwaite87ea75d2014-01-01 18:49:17 -08003531 device = qemu_opts_create(qemu_find_opts("device"), NULL, 0,
3532 &error_abort);
Markus Armbrusterf43e47d2015-02-12 17:52:20 +01003533 qemu_opt_set(device, "driver", "virtio-9p-pci", &error_abort);
3534 qemu_opt_set(device, "fsdev", "v_synth", &error_abort);
3535 qemu_opt_set(device, "mount_tag", "v_synth", &error_abort);
Aneesh Kumar K.V9db221a2011-10-25 12:10:40 +05303536 break;
3537 }
bellard82c643f2004-07-14 17:28:13 +00003538 case QEMU_OPTION_serial:
Gerd Hoffmann998bbd72009-12-08 13:11:41 +01003539 add_device_config(DEV_SERIAL, optarg);
3540 default_serial = 0;
Jan Kiszka18141ed2010-03-07 11:28:40 +01003541 if (strncmp(optarg, "mon:", 4) == 0) {
3542 default_monitor = 0;
3543 }
bellard82c643f2004-07-14 17:28:13 +00003544 break;
Richard W.M. Jones9dd986c2009-04-25 13:56:19 +01003545 case QEMU_OPTION_watchdog:
Markus Armbruster09aaa162009-08-21 10:31:34 +02003546 if (watchdog) {
3547 fprintf(stderr,
3548 "qemu: only one watchdog option may be given\n");
3549 return 1;
3550 }
3551 watchdog = optarg;
Richard W.M. Jones9dd986c2009-04-25 13:56:19 +01003552 break;
3553 case QEMU_OPTION_watchdog_action:
3554 if (select_watchdog_action(optarg) == -1) {
3555 fprintf(stderr, "Unknown -watchdog-action parameter\n");
3556 exit(1);
3557 }
3558 break;
aliguori51ecf132009-01-15 20:06:40 +00003559 case QEMU_OPTION_virtiocon:
Gerd Hoffmannaee1b932009-12-08 13:11:53 +01003560 add_device_config(DEV_VIRTCON, optarg);
3561 default_virtcon = 0;
Jan Kiszka18141ed2010-03-07 11:28:40 +01003562 if (strncmp(optarg, "mon:", 4) == 0) {
3563 default_monitor = 0;
3564 }
aliguori51ecf132009-01-15 20:06:40 +00003565 break;
bellard6508fe52005-01-15 12:02:56 +00003566 case QEMU_OPTION_parallel:
Gerd Hoffmann6a5e8b02009-12-08 13:11:42 +01003567 add_device_config(DEV_PARALLEL, optarg);
3568 default_parallel = 0;
Jan Kiszka18141ed2010-03-07 11:28:40 +01003569 if (strncmp(optarg, "mon:", 4) == 0) {
3570 default_monitor = 0;
3571 }
bellard6508fe52005-01-15 12:02:56 +00003572 break;
H. Peter Anvinc9f398e2009-12-29 13:51:36 -08003573 case QEMU_OPTION_debugcon:
3574 add_device_config(DEV_DEBUGCON, optarg);
3575 break;
Marcel Apfelbaum99efa842015-01-11 12:56:51 +02003576 case QEMU_OPTION_loadvm:
3577 loadvm = optarg;
3578 break;
bellardd63d3072004-10-03 13:29:03 +00003579 case QEMU_OPTION_full_screen:
3580 full_screen = 1;
3581 break;
ths43523e92007-02-18 18:19:32 +00003582 case QEMU_OPTION_no_frame:
3583 no_frame = 1;
3584 break;
ths3780e192007-06-21 21:08:02 +00003585 case QEMU_OPTION_alt_grab:
3586 alt_grab = 1;
3587 break;
Dustin Kirkland0ca9f8a2009-09-17 15:48:04 -05003588 case QEMU_OPTION_ctrl_grab:
3589 ctrl_grab = 1;
3590 break;
ths667acca2006-12-11 02:08:05 +00003591 case QEMU_OPTION_no_quit:
3592 no_quit = 1;
3593 break;
aliguori7d957bd2009-01-15 22:14:11 +00003594 case QEMU_OPTION_sdl:
Michael Tokarev24f6ff82013-06-15 14:44:20 +04003595#ifdef CONFIG_SDL
Anthony Liguori993fbfd2009-05-21 16:54:00 -05003596 display_type = DT_SDL;
aliguori7d957bd2009-01-15 22:14:11 +00003597 break;
Jes Sorensen58fc0962011-03-16 13:33:34 +01003598#else
Jes Sorensen58fc0962011-03-16 13:33:34 +01003599 fprintf(stderr, "SDL support is disabled\n");
3600 exit(1);
ths667acca2006-12-11 02:08:05 +00003601#endif
bellardf7cce892004-12-08 22:21:25 +00003602 case QEMU_OPTION_pidfile:
ths93815bc2007-03-19 15:58:31 +00003603 pid_file = optarg;
bellardf7cce892004-12-08 22:21:25 +00003604 break;
bellarda09db212005-04-30 16:10:35 +00003605 case QEMU_OPTION_win2k_hack:
3606 win2k_install_hack = 1;
3607 break;
Jan Kiszka433acf02012-01-23 20:15:12 +01003608 case QEMU_OPTION_rtc_td_hack: {
3609 static GlobalProperty slew_lost_ticks[] = {
3610 {
3611 .driver = "mc146818rtc",
3612 .property = "lost_tick_policy",
3613 .value = "slew",
3614 },
3615 { /* end of list */ }
3616 };
3617
3618 qdev_prop_register_global_list(slew_lost_ticks);
aliguori73822ec2009-01-15 20:11:34 +00003619 break;
Jan Kiszka433acf02012-01-23 20:15:12 +01003620 }
aliguori8a92ea22009-02-27 20:12:36 +00003621 case QEMU_OPTION_acpitable:
Markus Armbruster70b94332015-02-13 12:50:26 +01003622 opts = qemu_opts_parse_noisily(qemu_find_opts("acpi"),
3623 optarg, true);
Laszlo Ersekf46e7202013-11-28 18:12:59 +01003624 if (!opts) {
3625 exit(1);
3626 }
Laszlo Ersek0c764a92013-03-21 00:23:17 +01003627 do_acpitable_option(opts);
aliguori8a92ea22009-02-27 20:12:36 +00003628 break;
aliguorib6f6e3d2009-04-17 18:59:56 +00003629 case QEMU_OPTION_smbios:
Markus Armbruster70b94332015-02-13 12:50:26 +01003630 opts = qemu_opts_parse_noisily(qemu_find_opts("smbios"),
3631 optarg, false);
Laszlo Ersekf46e7202013-11-28 18:12:59 +01003632 if (!opts) {
3633 exit(1);
3634 }
Markus Armbruster4f953d22013-08-16 15:18:29 +02003635 do_smbios_option(opts);
aliguorib6f6e3d2009-04-17 18:59:56 +00003636 break;
Gabriel L. Somlo81b2b812015-04-29 11:21:53 -04003637 case QEMU_OPTION_fwcfg:
Markus Armbruster70b94332015-02-13 12:50:26 +01003638 opts = qemu_opts_parse_noisily(qemu_find_opts("fw_cfg"),
3639 optarg, true);
Gabriel L. Somlo81b2b812015-04-29 11:21:53 -04003640 if (opts == NULL) {
3641 exit(1);
3642 }
3643 break;
aliguori7ba1e612008-11-05 16:04:33 +00003644 case QEMU_OPTION_enable_kvm:
Anthony PERARD303d4e82010-09-21 20:05:31 +01003645 olist = qemu_find_opts("machine");
Markus Armbruster70b94332015-02-13 12:50:26 +01003646 qemu_opts_parse_noisily(olist, "accel=kvm", false);
Anthony PERARD303d4e82010-09-21 20:05:31 +01003647 break;
Marcel Apfelbaum364c3e62015-01-07 14:11:38 +02003648 case QEMU_OPTION_M:
Anthony PERARD303d4e82010-09-21 20:05:31 +01003649 case QEMU_OPTION_machine:
3650 olist = qemu_find_opts("machine");
Markus Armbruster70b94332015-02-13 12:50:26 +01003651 opts = qemu_opts_parse_noisily(olist, optarg, true);
Anthony PERARD303d4e82010-09-21 20:05:31 +01003652 if (!opts) {
Anthony PERARD303d4e82010-09-21 20:05:31 +01003653 exit(1);
3654 }
aliguori7ba1e612008-11-05 16:04:33 +00003655 break;
Jan Kiszkaa0dac022012-10-05 14:51:45 -03003656 case QEMU_OPTION_no_kvm:
3657 olist = qemu_find_opts("machine");
Markus Armbruster70b94332015-02-13 12:50:26 +01003658 qemu_opts_parse_noisily(olist, "accel=tcg", false);
Jan Kiszkaa0dac022012-10-05 14:51:45 -03003659 break;
Jan Kiszka4086bde2012-10-05 14:51:41 -03003660 case QEMU_OPTION_no_kvm_pit: {
3661 fprintf(stderr, "Warning: KVM PIT can no longer be disabled "
3662 "separately.\n");
3663 break;
3664 }
Jan Kiszkac21fb4f2012-10-05 14:51:42 -03003665 case QEMU_OPTION_no_kvm_pit_reinjection: {
3666 static GlobalProperty kvm_pit_lost_tick_policy[] = {
3667 {
3668 .driver = "kvm-pit",
3669 .property = "lost_tick_policy",
3670 .value = "discard",
3671 },
3672 { /* end of list */ }
3673 };
3674
3675 fprintf(stderr, "Warning: option deprecated, use "
3676 "lost_tick_policy property of kvm-pit instead.\n");
3677 qdev_prop_register_global_list(kvm_pit_lost_tick_policy);
3678 break;
3679 }
bellardbb36d472005-11-05 14:22:28 +00003680 case QEMU_OPTION_usb:
Peter Maydellfa5358c2012-11-22 16:48:45 +00003681 olist = qemu_find_opts("machine");
Markus Armbruster70b94332015-02-13 12:50:26 +01003682 qemu_opts_parse_noisily(olist, "usb=on", false);
bellardbb36d472005-11-05 14:22:28 +00003683 break;
bellarda594cfb2005-11-06 16:13:29 +00003684 case QEMU_OPTION_usbdevice:
Peter Maydellfa5358c2012-11-22 16:48:45 +00003685 olist = qemu_find_opts("machine");
Markus Armbruster70b94332015-02-13 12:50:26 +01003686 qemu_opts_parse_noisily(olist, "usb=on", false);
Gerd Hoffmannbd3c9482009-07-15 13:59:26 +02003687 add_device_config(DEV_USB, optarg);
3688 break;
3689 case QEMU_OPTION_device:
Markus Armbruster70b94332015-02-13 12:50:26 +01003690 if (!qemu_opts_parse_noisily(qemu_find_opts("device"),
3691 optarg, true)) {
Gerd Hoffmannf31d07d2009-07-31 12:25:37 +02003692 exit(1);
3693 }
bellarda594cfb2005-11-06 16:13:29 +00003694 break;
bellard6a00d602005-11-21 23:25:50 +00003695 case QEMU_OPTION_smp:
Markus Armbruster70b94332015-02-13 12:50:26 +01003696 if (!qemu_opts_parse_noisily(qemu_find_opts("smp-opts"),
3697 optarg, true)) {
Jes Sorensen6be68d72009-07-23 17:03:42 +02003698 exit(1);
3699 }
bellard6a00d602005-11-21 23:25:50 +00003700 break;
Marcel Apfelbaum99efa842015-01-11 12:56:51 +02003701 case QEMU_OPTION_vnc:
Markus Armbruster70b94332015-02-13 12:50:26 +01003702 {
Jes Sorensen821601e2011-03-16 13:33:36 +01003703#ifdef CONFIG_VNC
Markus Armbruster70b94332015-02-13 12:50:26 +01003704 Error *local_err = NULL;
3705
3706 if (vnc_parse(optarg, &local_err) == NULL) {
3707 error_report_err(local_err);
Gerd Hoffmann4db14622014-09-16 12:33:03 +02003708 exit(1);
3709 }
Jes Sorensen821601e2011-03-16 13:33:36 +01003710#else
3711 fprintf(stderr, "VNC support is disabled\n");
3712 exit(1);
3713#endif
3714 break;
Markus Armbruster70b94332015-02-13 12:50:26 +01003715 }
bellard6515b202006-05-03 22:02:44 +00003716 case QEMU_OPTION_no_acpi:
3717 acpi_enabled = 0;
3718 break;
aliguori16b29ae2008-12-17 23:28:44 +00003719 case QEMU_OPTION_no_hpet:
3720 no_hpet = 1;
3721 break;
Markus Armbruster7d4c3d52009-06-26 19:15:14 +02003722 case QEMU_OPTION_balloon:
3723 if (balloon_parse(optarg) < 0) {
3724 fprintf(stderr, "Unknown -balloon argument %s\n", optarg);
3725 exit(1);
3726 }
Eduardo Habkostdf97b922009-06-10 16:34:08 -03003727 break;
bellardd1beab82006-10-02 19:44:22 +00003728 case QEMU_OPTION_no_reboot:
3729 no_reboot = 1;
3730 break;
aurel32b2f76162008-04-11 21:35:52 +00003731 case QEMU_OPTION_no_shutdown:
3732 no_shutdown = 1;
3733 break;
balrog9467cd42007-05-01 01:34:14 +00003734 case QEMU_OPTION_show_cursor:
3735 cursor_hide = 0;
3736 break;
blueswir18fcb1b92008-09-18 18:29:08 +00003737 case QEMU_OPTION_uuid:
3738 if(qemu_uuid_parse(optarg, qemu_uuid) < 0) {
3739 fprintf(stderr, "Fail to parse UUID string."
3740 " Wrong format.\n");
3741 exit(1);
3742 }
Markus Armbrusterfc3b3292013-08-16 15:18:31 +02003743 qemu_uuid_set = true;
blueswir18fcb1b92008-09-18 18:29:08 +00003744 break;
Marcel Apfelbaum99efa842015-01-11 12:56:51 +02003745 case QEMU_OPTION_option_rom:
3746 if (nb_option_roms >= MAX_OPTION_ROMS) {
3747 fprintf(stderr, "Too many option ROMs\n");
3748 exit(1);
3749 }
Markus Armbruster70b94332015-02-13 12:50:26 +01003750 opts = qemu_opts_parse_noisily(qemu_find_opts("option-rom"),
3751 optarg, true);
Markus Armbruster49295eb2013-02-08 21:22:19 +01003752 if (!opts) {
3753 exit(1);
3754 }
Gleb Natapov2e55e842010-12-08 13:35:07 +02003755 option_rom[nb_option_roms].name = qemu_opt_get(opts, "romfile");
3756 option_rom[nb_option_roms].bootindex =
3757 qemu_opt_get_number(opts, "bootindex", -1);
3758 if (!option_rom[nb_option_roms].name) {
3759 fprintf(stderr, "Option ROM file is not specified\n");
3760 exit(1);
3761 }
Marcel Apfelbaum99efa842015-01-11 12:56:51 +02003762 nb_option_roms++;
3763 break;
pbrook8e716212007-01-20 17:12:09 +00003764 case QEMU_OPTION_semihosting:
Leon Alraecfe67ce2015-06-19 14:17:45 +01003765 semihosting.enabled = true;
3766 semihosting.target = SEMIHOSTING_TARGET_AUTO;
Liviu Ionescua38bb072014-12-11 12:07:48 +00003767 break;
3768 case QEMU_OPTION_semihosting_config:
Leon Alraecfe67ce2015-06-19 14:17:45 +01003769 semihosting.enabled = true;
Markus Armbruster70b94332015-02-13 12:50:26 +01003770 opts = qemu_opts_parse_noisily(qemu_find_opts("semihosting-config"),
3771 optarg, false);
Liviu Ionescua38bb072014-12-11 12:07:48 +00003772 if (opts != NULL) {
Leon Alraecfe67ce2015-06-19 14:17:45 +01003773 semihosting.enabled = qemu_opt_get_bool(opts, "enable",
Liviu Ionescua38bb072014-12-11 12:07:48 +00003774 true);
3775 const char *target = qemu_opt_get(opts, "target");
3776 if (target != NULL) {
3777 if (strcmp("native", target) == 0) {
Leon Alraecfe67ce2015-06-19 14:17:45 +01003778 semihosting.target = SEMIHOSTING_TARGET_NATIVE;
Liviu Ionescua38bb072014-12-11 12:07:48 +00003779 } else if (strcmp("gdb", target) == 0) {
Leon Alraecfe67ce2015-06-19 14:17:45 +01003780 semihosting.target = SEMIHOSTING_TARGET_GDB;
Liviu Ionescua38bb072014-12-11 12:07:48 +00003781 } else if (strcmp("auto", target) == 0) {
Leon Alraecfe67ce2015-06-19 14:17:45 +01003782 semihosting.target = SEMIHOSTING_TARGET_AUTO;
Liviu Ionescua38bb072014-12-11 12:07:48 +00003783 } else {
3784 fprintf(stderr, "Unsupported semihosting-config"
3785 " %s\n",
3786 optarg);
3787 exit(1);
3788 }
3789 } else {
Leon Alraecfe67ce2015-06-19 14:17:45 +01003790 semihosting.target = SEMIHOSTING_TARGET_AUTO;
Liviu Ionescua38bb072014-12-11 12:07:48 +00003791 }
Leon Alraea59d31a2015-06-19 14:17:45 +01003792 /* Set semihosting argument count and vector */
3793 qemu_opt_foreach(opts, add_semihosting_arg,
3794 &semihosting, NULL);
Liviu Ionescua38bb072014-12-11 12:07:48 +00003795 } else {
3796 fprintf(stderr, "Unsupported semihosting-config %s\n",
3797 optarg);
3798 exit(1);
3799 }
pbrook8e716212007-01-20 17:12:09 +00003800 break;
Jan Kiszka88eed342012-10-05 14:51:44 -03003801 case QEMU_OPTION_tdf:
3802 fprintf(stderr, "Warning: user space PIT time drift fix "
3803 "is no longer supported.\n");
3804 break;
thsc35734b2007-03-19 15:17:08 +00003805 case QEMU_OPTION_name:
Markus Armbruster70b94332015-02-13 12:50:26 +01003806 opts = qemu_opts_parse_noisily(qemu_find_opts("name"),
3807 optarg, true);
Dr. David Alan Gilbert5d12f962014-01-30 10:20:30 +00003808 if (!opts) {
3809 exit(1);
3810 }
thsc35734b2007-03-19 15:17:08 +00003811 break;
blueswir166508602007-05-01 14:16:52 +00003812 case QEMU_OPTION_prom_env:
3813 if (nb_prom_envs >= MAX_PROM_ENVS) {
3814 fprintf(stderr, "Too many prom variables\n");
3815 exit(1);
3816 }
3817 prom_envs[nb_prom_envs] = optarg;
3818 nb_prom_envs++;
3819 break;
balrog2b8f2d42007-07-27 22:08:46 +00003820 case QEMU_OPTION_old_param:
3821 old_param = 1;
ths05ebd532008-01-08 19:32:16 +00003822 break;
thsf3dcfad2007-08-24 01:26:02 +00003823 case QEMU_OPTION_clock:
Alex Bligh6d327172013-08-21 16:02:59 +01003824 /* Clock options no longer exist. Keep this option for
3825 * backward compatibility.
3826 */
thsf3dcfad2007-08-24 01:26:02 +00003827 break;
bellard7e0af5d02007-11-07 16:24:33 +00003828 case QEMU_OPTION_startdate:
Jan Kiszka1ed2fc12009-09-15 13:36:04 +02003829 configure_rtc_date_offset(optarg, 1);
3830 break;
3831 case QEMU_OPTION_rtc:
Markus Armbruster70b94332015-02-13 12:50:26 +01003832 opts = qemu_opts_parse_noisily(qemu_find_opts("rtc"), optarg,
3833 false);
Jan Kiszka1ed2fc12009-09-15 13:36:04 +02003834 if (!opts) {
Jan Kiszka1ed2fc12009-09-15 13:36:04 +02003835 exit(1);
bellard7e0af5d02007-11-07 16:24:33 +00003836 }
Jan Kiszka1ed2fc12009-09-15 13:36:04 +02003837 configure_rtc(opts);
bellard7e0af5d02007-11-07 16:24:33 +00003838 break;
bellard26a5f132008-05-28 12:30:31 +00003839 case QEMU_OPTION_tb_size:
Jan Kiszkad5ab9712011-08-02 16:10:21 +02003840 tcg_tb_size = strtol(optarg, NULL, 0);
3841 if (tcg_tb_size < 0) {
3842 tcg_tb_size = 0;
3843 }
bellard26a5f132008-05-28 12:30:31 +00003844 break;
pbrook2e70f6e2008-06-29 01:03:05 +00003845 case QEMU_OPTION_icount:
Markus Armbruster70b94332015-02-13 12:50:26 +01003846 icount_opts = qemu_opts_parse_noisily(qemu_find_opts("icount"),
3847 optarg, true);
Sebastian Tanase1ad95802014-07-25 11:56:28 +02003848 if (!icount_opts) {
3849 exit(1);
3850 }
pbrook2e70f6e2008-06-29 01:03:05 +00003851 break;
aliguori5bb79102008-10-13 03:12:02 +00003852 case QEMU_OPTION_incoming:
Dr. David Alan Gilbert7c762352015-02-26 14:16:06 +00003853 if (!incoming) {
3854 runstate_set(RUN_STATE_INMIGRATE);
3855 }
aliguori5bb79102008-10-13 03:12:02 +00003856 incoming = optarg;
3857 break;
Gerd Hoffmannd8c208d2009-12-08 13:11:46 +01003858 case QEMU_OPTION_nodefaults:
Mark Wud44229c2014-03-10 22:37:40 +08003859 has_defaults = 0;
Gerd Hoffmannd8c208d2009-12-08 13:11:46 +01003860 break;
aliguorie37630c2009-04-22 15:19:10 +00003861 case QEMU_OPTION_xen_domid:
Blue Swirlad960902010-03-29 19:23:52 +00003862 if (!(xen_available())) {
3863 printf("Option %s not supported for this target\n", popt->name);
3864 exit(1);
3865 }
aliguorie37630c2009-04-22 15:19:10 +00003866 xen_domid = atoi(optarg);
3867 break;
3868 case QEMU_OPTION_xen_create:
Blue Swirlad960902010-03-29 19:23:52 +00003869 if (!(xen_available())) {
3870 printf("Option %s not supported for this target\n", popt->name);
3871 exit(1);
3872 }
aliguorie37630c2009-04-22 15:19:10 +00003873 xen_mode = XEN_CREATE;
3874 break;
3875 case QEMU_OPTION_xen_attach:
Blue Swirlad960902010-03-29 19:23:52 +00003876 if (!(xen_available())) {
3877 printf("Option %s not supported for this target\n", popt->name);
3878 exit(1);
3879 }
aliguorie37630c2009-04-22 15:19:10 +00003880 xen_mode = XEN_ATTACH;
3881 break;
Prerna Saxenaab6540d2010-08-09 11:48:32 +01003882 case QEMU_OPTION_trace:
LluĂ­se4858972011-08-31 20:31:03 +02003883 {
Markus Armbruster70b94332015-02-13 12:50:26 +01003884 opts = qemu_opts_parse_noisily(qemu_find_opts("trace"),
3885 optarg, false);
LluĂ­se4858972011-08-31 20:31:03 +02003886 if (!opts) {
3887 exit(1);
Prerna Saxenaab6540d2010-08-09 11:48:32 +01003888 }
LluĂ­s23d15e82011-08-31 20:31:31 +02003889 trace_events = qemu_opt_get(opts, "events");
LluĂ­se4858972011-08-31 20:31:03 +02003890 trace_file = qemu_opt_get(opts, "file");
Prerna Saxenaab6540d2010-08-09 11:48:32 +01003891 break;
LluĂ­se4858972011-08-31 20:31:03 +02003892 }
Gerd Hoffmann715a6642009-10-14 10:39:28 +02003893 case QEMU_OPTION_readconfig:
3894 {
Kevin Wolfdcfb0932010-03-05 17:25:55 +01003895 int ret = qemu_read_config_file(optarg);
3896 if (ret < 0) {
3897 fprintf(stderr, "read config %s: %s\n", optarg,
3898 strerror(-ret));
Gerd Hoffmann715a6642009-10-14 10:39:28 +02003899 exit(1);
3900 }
Gerd Hoffmann715a6642009-10-14 10:39:28 +02003901 break;
3902 }
Gerd Hoffmann29b00402010-03-11 11:13:27 -03003903 case QEMU_OPTION_spice:
3904 olist = qemu_find_opts("spice");
3905 if (!olist) {
3906 fprintf(stderr, "spice is not supported by this qemu build.\n");
3907 exit(1);
3908 }
Markus Armbruster70b94332015-02-13 12:50:26 +01003909 opts = qemu_opts_parse_noisily(olist, optarg, false);
Gerd Hoffmann29b00402010-03-11 11:13:27 -03003910 if (!opts) {
Gerd Hoffmann29b00402010-03-11 11:13:27 -03003911 exit(1);
3912 }
Gerd Hoffmannf963e4d2013-02-25 16:02:30 +01003913 display_remote++;
Gerd Hoffmann29b00402010-03-11 11:13:27 -03003914 break;
Gerd Hoffmann715a6642009-10-14 10:39:28 +02003915 case QEMU_OPTION_writeconfig:
3916 {
3917 FILE *fp;
3918 if (strcmp(optarg, "-") == 0) {
3919 fp = stdout;
3920 } else {
3921 fp = fopen(optarg, "w");
3922 if (fp == NULL) {
3923 fprintf(stderr, "open %s: %s\n", optarg, strerror(errno));
3924 exit(1);
3925 }
3926 }
3927 qemu_config_write(fp);
Chen Gang7fb8b5d2014-04-22 09:12:34 +08003928 if (fp != stdout) {
3929 fclose(fp);
3930 }
Gerd Hoffmann715a6642009-10-14 10:39:28 +02003931 break;
3932 }
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +02003933 case QEMU_OPTION_qtest:
3934 qtest_chrdev = optarg;
3935 break;
3936 case QEMU_OPTION_qtest_log:
3937 qtest_log = optarg;
3938 break;
Eduardo Otubo7d76ad42012-08-14 18:44:08 -03003939 case QEMU_OPTION_sandbox:
Markus Armbruster70b94332015-02-13 12:50:26 +01003940 opts = qemu_opts_parse_noisily(qemu_find_opts("sandbox"),
3941 optarg, true);
Eduardo Otubo7d76ad42012-08-14 18:44:08 -03003942 if (!opts) {
Markus Armbruster49295eb2013-02-08 21:22:19 +01003943 exit(1);
Eduardo Otubo7d76ad42012-08-14 18:44:08 -03003944 }
3945 break;
Corey Bryant587ed6b2012-10-18 15:19:34 -04003946 case QEMU_OPTION_add_fd:
3947#ifndef _WIN32
Markus Armbruster70b94332015-02-13 12:50:26 +01003948 opts = qemu_opts_parse_noisily(qemu_find_opts("add-fd"),
3949 optarg, false);
Corey Bryant587ed6b2012-10-18 15:19:34 -04003950 if (!opts) {
Markus Armbruster49295eb2013-02-08 21:22:19 +01003951 exit(1);
Corey Bryant587ed6b2012-10-18 15:19:34 -04003952 }
3953#else
3954 error_report("File descriptor passing is disabled on this "
3955 "platform");
3956 exit(1);
3957#endif
3958 break;
Anthony Liguori68d98d32012-06-25 14:36:33 -05003959 case QEMU_OPTION_object:
Markus Armbruster70b94332015-02-13 12:50:26 +01003960 opts = qemu_opts_parse_noisily(qemu_find_opts("object"),
3961 optarg, true);
Markus Armbruster49295eb2013-02-08 21:22:19 +01003962 if (!opts) {
3963 exit(1);
3964 }
Anthony Liguori68d98d32012-06-25 14:36:33 -05003965 break;
Satoru Moriya888a6bc2013-04-19 16:42:06 +02003966 case QEMU_OPTION_realtime:
Markus Armbruster70b94332015-02-13 12:50:26 +01003967 opts = qemu_opts_parse_noisily(qemu_find_opts("realtime"),
3968 optarg, false);
Satoru Moriya888a6bc2013-04-19 16:42:06 +02003969 if (!opts) {
3970 exit(1);
3971 }
zhanghailiang28d16f32014-09-23 18:42:24 +08003972 enable_mlock = qemu_opt_get_bool(opts, "mlock", true);
Satoru Moriya888a6bc2013-04-19 16:42:06 +02003973 break;
Seiji Aguchi5e2ac512013-07-03 23:02:46 -04003974 case QEMU_OPTION_msg:
Markus Armbruster70b94332015-02-13 12:50:26 +01003975 opts = qemu_opts_parse_noisily(qemu_find_opts("msg"), optarg,
3976 false);
Seiji Aguchi5e2ac512013-07-03 23:02:46 -04003977 if (!opts) {
3978 exit(1);
3979 }
3980 configure_msg(opts);
3981 break;
Amit Shahabfd9ce2014-06-20 18:56:08 +05303982 case QEMU_OPTION_dump_vmstate:
Gonglei522abf62014-10-29 20:49:43 +08003983 if (vmstate_dump_file) {
3984 fprintf(stderr, "qemu: only one '-dump-vmstate' "
3985 "option may be given\n");
3986 exit(1);
3987 }
Amit Shahabfd9ce2014-06-20 18:56:08 +05303988 vmstate_dump_file = fopen(optarg, "w");
3989 if (vmstate_dump_file == NULL) {
3990 fprintf(stderr, "open %s: %s\n", optarg, strerror(errno));
3991 exit(1);
3992 }
3993 break;
Jes Sorensen59a52642010-06-10 11:42:25 +02003994 default:
3995 os_parse_cmd_args(popt->index, optarg);
bellardcd6f1162004-05-13 22:02:20 +00003996 }
bellard0824d6f2003-06-24 13:42:40 +00003997 }
3998 }
Marcel Apfelbaum364c3e62015-01-07 14:11:38 +02003999
4000 opts = qemu_get_machine_opts();
4001 optarg = qemu_opt_get(opts, "type");
4002 if (optarg) {
4003 machine_class = machine_parse(optarg);
4004 }
4005
Nikunj A Dadhania076b35b2015-05-07 15:33:57 +10004006 if (machine_class == NULL) {
4007 fprintf(stderr, "No machine specified, and there is no default.\n"
4008 "Use -machine help to list supported machines!\n");
4009 exit(1);
4010 }
4011
4012 set_memory_options(&ram_slots, &maxram_size, machine_class);
Marcel Apfelbaum3b9985e2015-01-11 12:38:43 +02004013
Markus Armbruster0f0bc3f2010-02-18 20:13:51 +01004014 loc_set_none();
bellard330d0412003-07-26 18:11:40 +00004015
Igor Mammedov2d9c2722014-06-02 15:25:00 +02004016 os_daemonize();
4017
Chrysostomos Nanakos2f78e492014-09-18 14:30:49 +03004018 if (qemu_init_main_loop(&main_loop_err)) {
Markus Armbruster565f65d2015-02-12 13:55:05 +01004019 error_report_err(main_loop_err);
Paolo Bonzini1c537862012-10-30 00:17:12 +01004020 exit(1);
4021 }
4022
Markus Armbruster28d0de72015-03-13 13:35:14 +01004023 if (qemu_opts_foreach(qemu_find_opts("sandbox"),
4024 parse_sandbox, NULL, NULL)) {
Eduardo Otubo7d76ad42012-08-14 18:44:08 -03004025 exit(1);
4026 }
4027
Markus Armbruster28d0de72015-03-13 13:35:14 +01004028 if (qemu_opts_foreach(qemu_find_opts("name"),
4029 parse_name, NULL, NULL)) {
Dr. David Alan Gilbert5b9d3132014-05-06 12:15:55 +01004030 exit(1);
4031 }
4032
Corey Bryant587ed6b2012-10-18 15:19:34 -04004033#ifndef _WIN32
Markus Armbruster28d0de72015-03-13 13:35:14 +01004034 if (qemu_opts_foreach(qemu_find_opts("add-fd"),
4035 parse_add_fd, NULL, NULL)) {
Corey Bryant587ed6b2012-10-18 15:19:34 -04004036 exit(1);
4037 }
4038
Markus Armbruster28d0de72015-03-13 13:35:14 +01004039 if (qemu_opts_foreach(qemu_find_opts("add-fd"),
4040 cleanup_add_fd, NULL, NULL)) {
Corey Bryant587ed6b2012-10-18 15:19:34 -04004041 exit(1);
4042 }
4043#endif
4044
Marcel Apfelbaum0056ae22014-03-05 19:30:47 +02004045 current_machine = MACHINE(object_new(object_class_get_name(
4046 OBJECT_CLASS(machine_class))));
Marcel Apfelbaum52eb3df2014-12-16 16:58:06 +00004047 if (machine_help_func(qemu_get_machine_opts(), current_machine)) {
4048 exit(0);
4049 }
Marcel Apfelbaum0056ae22014-03-05 19:30:47 +02004050 object_property_add_child(object_get_root(), "machine",
4051 OBJECT(current_machine), &error_abort);
Paolo Bonzinib5c2c3d2014-06-11 10:52:32 +02004052 cpu_exec_init_all();
Marcel Apfelbaum0056ae22014-03-05 19:30:47 +02004053
Marcel Apfelbaum958db902014-04-09 20:34:53 +03004054 if (machine_class->hw_version) {
4055 qemu_set_version(machine_class->hw_version);
CrĂ­stian Viana93bfef42012-05-30 00:35:51 -03004056 }
4057
Eduardo Habkostecf40be2012-03-09 16:19:07 -03004058 /* Init CPU def lists, based on config
4059 * - Must be called after all the qemu_read_config_file() calls
4060 * - Must be called before list_cpus()
4061 * - Must be called before machine->init()
4062 */
4063 cpudef_init();
4064
Peter Maydellc8057f92012-08-02 13:45:54 +01004065 if (cpu_model && is_help_option(cpu_model)) {
Eduardo Habkost1d6528a2012-03-21 09:33:40 -03004066 list_cpus(stdout, &fprintf, cpu_model);
Eduardo Habkostecf40be2012-03-09 16:19:07 -03004067 exit(0);
4068 }
4069
Pranith Kumar67633bb2015-06-10 10:20:24 -04004070 /* Open the logfile at this point and set the log mask if necessary.
Matthew Fernandezc235d732011-06-07 16:32:40 +00004071 */
Pranith Kumar67633bb2015-06-10 10:20:24 -04004072 if (log_file) {
4073 qemu_set_log_filename(log_file);
4074 }
4075
Matthew Fernandezc235d732011-06-07 16:32:40 +00004076 if (log_mask) {
Peter Maydellb946bff2013-02-11 16:41:24 +00004077 int mask;
Peter Maydellb946bff2013-02-11 16:41:24 +00004078 mask = qemu_str_to_log_mask(log_mask);
4079 if (!mask) {
4080 qemu_print_log_usage(stdout);
4081 exit(1);
4082 }
4083 qemu_set_log(mask);
Matthew Fernandezc235d732011-06-07 16:32:40 +00004084 }
4085
Michael Mueller8a745f22013-09-23 16:36:54 +02004086 if (!is_daemonized()) {
LluĂ­s Vilanova5b808272014-05-27 15:02:14 +02004087 if (!trace_init_backends(trace_events, trace_file)) {
Michael Mueller8a745f22013-09-23 16:36:54 +02004088 exit(1);
4089 }
Stefan Hajnoczi31d3c9b2011-03-13 20:14:30 +00004090 }
Stefan Hajnoczi0b5538c2011-02-26 18:38:39 +00004091
Paul Brook5cea8592009-05-30 00:52:44 +01004092 /* If no data_dir is specified then try to find it relative to the
4093 executable path. */
Gerd Hoffmann45240512013-03-08 11:42:24 +01004094 if (data_dir_idx < ARRAY_SIZE(data_dir)) {
Fam Zheng10f5bff2014-02-10 14:48:51 +08004095 data_dir[data_dir_idx] = os_find_datadir();
Gerd Hoffmann45240512013-03-08 11:42:24 +01004096 if (data_dir[data_dir_idx] != NULL) {
4097 data_dir_idx++;
4098 }
Paul Brook5cea8592009-05-30 00:52:44 +01004099 }
Stefan Weil60474fb2011-09-04 15:17:46 +02004100 /* If all else fails use the install path specified when building. */
Gerd Hoffmann45240512013-03-08 11:42:24 +01004101 if (data_dir_idx < ARRAY_SIZE(data_dir)) {
4102 data_dir[data_dir_idx++] = CONFIG_QEMU_DATADIR;
Paul Brook5cea8592009-05-30 00:52:44 +01004103 }
4104
Michael Tokarev12b7f572013-06-24 15:06:52 +04004105 smp_parse(qemu_opts_find(qemu_find_opts("smp-opts"), NULL));
Jes Sorensen6be68d72009-07-23 17:03:42 +02004106
Marcel Apfelbaum958db902014-04-09 20:34:53 +03004107 machine_class->max_cpus = machine_class->max_cpus ?: 1; /* Default to UP */
Andrew Jonesc00cd992014-12-19 00:59:46 -02004108 if (max_cpus > machine_class->max_cpus) {
Andrew Jones31bfa2a2015-10-18 19:35:27 +02004109 fprintf(stderr, "Number of SMP CPUs requested (%d) exceeds max CPUs "
4110 "supported by machine '%s' (%d)\n", max_cpus,
Marcel Apfelbaum958db902014-04-09 20:34:53 +03004111 machine_class->name, machine_class->max_cpus);
aliguorib2097002008-10-07 20:39:39 +00004112 exit(1);
4113 }
4114
Anthony PERARD67b724e2010-11-22 15:44:15 +00004115 /*
4116 * Get the default machine options from the machine if it is not already
4117 * specified either by the configuration file or by the command line.
4118 */
Marcel Apfelbaum958db902014-04-09 20:34:53 +03004119 if (machine_class->default_machine_opts) {
Jan Kiszka25de5932012-01-27 19:55:43 +01004120 qemu_opts_set_defaults(qemu_find_opts("machine"),
Marcel Apfelbaum958db902014-04-09 20:34:53 +03004121 machine_class->default_machine_opts, 0);
Anthony PERARD67b724e2010-11-22 15:44:15 +00004122 }
4123
Markus Armbruster28d0de72015-03-13 13:35:14 +01004124 qemu_opts_foreach(qemu_find_opts("device"),
4125 default_driver_check, NULL, NULL);
4126 qemu_opts_foreach(qemu_find_opts("global"),
4127 default_driver_check, NULL, NULL);
Gerd Hoffmann998bbd72009-12-08 13:11:41 +01004128
Mark Wud44229c2014-03-10 22:37:40 +08004129 if (!vga_model && !default_vga) {
4130 vga_interface_type = VGA_DEVICE;
4131 }
Marcel Apfelbaum958db902014-04-09 20:34:53 +03004132 if (!has_defaults || machine_class->no_serial) {
Gerd Hoffmann986c5f72009-12-08 13:11:54 +01004133 default_serial = 0;
4134 }
Marcel Apfelbaum958db902014-04-09 20:34:53 +03004135 if (!has_defaults || machine_class->no_parallel) {
Gerd Hoffmann986c5f72009-12-08 13:11:54 +01004136 default_parallel = 0;
4137 }
Marcel Apfelbaum958db902014-04-09 20:34:53 +03004138 if (!has_defaults || !machine_class->use_virtcon) {
Gerd Hoffmann986c5f72009-12-08 13:11:54 +01004139 default_virtcon = 0;
4140 }
Marcel Apfelbaum958db902014-04-09 20:34:53 +03004141 if (!has_defaults || !machine_class->use_sclp) {
Alexander Graf3ef669e2013-01-24 12:18:52 +01004142 default_sclp = 0;
4143 }
Marcel Apfelbaum958db902014-04-09 20:34:53 +03004144 if (!has_defaults || machine_class->no_floppy) {
Gerd Hoffmannac33f8f2009-12-16 14:25:39 +01004145 default_floppy = 0;
4146 }
Marcel Apfelbaum958db902014-04-09 20:34:53 +03004147 if (!has_defaults || machine_class->no_cdrom) {
Gerd Hoffmannac33f8f2009-12-16 14:25:39 +01004148 default_cdrom = 0;
4149 }
Marcel Apfelbaum958db902014-04-09 20:34:53 +03004150 if (!has_defaults || machine_class->no_sdcard) {
Gerd Hoffmannac33f8f2009-12-16 14:25:39 +01004151 default_sdcard = 0;
4152 }
Mark Wud44229c2014-03-10 22:37:40 +08004153 if (!has_defaults) {
4154 default_monitor = 0;
4155 default_net = 0;
4156 default_vga = 0;
4157 }
Gerd Hoffmann986c5f72009-12-08 13:11:54 +01004158
Michael Tokarevab51b1d2012-12-30 12:48:14 +04004159 if (is_daemonized()) {
4160 /* According to documentation and historically, -nographic redirects
4161 * serial port, parallel port and monitor to stdio, which does not work
4162 * with -daemonize. We can redirect these to null instead, but since
4163 * -nographic is legacy, let's just error out.
4164 * We disallow -nographic only if all other ports are not redirected
4165 * explicitly, to not break existing legacy setups which uses
4166 * -nographic _and_ redirects all ports explicitly - this is valid
4167 * usage, -nographic is just a no-op in this case.
4168 */
4169 if (display_type == DT_NOGRAPHIC
4170 && (default_parallel || default_serial
4171 || default_monitor || default_virtcon)) {
4172 fprintf(stderr, "-nographic can not be used with -daemonize\n");
4173 exit(1);
4174 }
4175#ifdef CONFIG_CURSES
4176 if (display_type == DT_CURSES) {
4177 fprintf(stderr, "curses display can not be used with -daemonize\n");
4178 exit(1);
4179 }
4180#endif
4181 }
4182
Anthony Liguori993fbfd2009-05-21 16:54:00 -05004183 if (display_type == DT_NOGRAPHIC) {
Gerd Hoffmann6a5e8b02009-12-08 13:11:42 +01004184 if (default_parallel)
4185 add_device_config(DEV_PARALLEL, "null");
Gerd Hoffmanne1c09172009-12-08 13:11:44 +01004186 if (default_serial && default_monitor) {
4187 add_device_config(DEV_SERIAL, "mon:stdio");
Gerd Hoffmann986c5f72009-12-08 13:11:54 +01004188 } else if (default_virtcon && default_monitor) {
4189 add_device_config(DEV_VIRTCON, "mon:stdio");
Alexander Graf3ef669e2013-01-24 12:18:52 +01004190 } else if (default_sclp && default_monitor) {
4191 add_device_config(DEV_SCLP, "mon:stdio");
Gerd Hoffmanne1c09172009-12-08 13:11:44 +01004192 } else {
4193 if (default_serial)
4194 add_device_config(DEV_SERIAL, "stdio");
Gerd Hoffmann986c5f72009-12-08 13:11:54 +01004195 if (default_virtcon)
4196 add_device_config(DEV_VIRTCON, "stdio");
Alexander Graf3ef669e2013-01-24 12:18:52 +01004197 if (default_sclp) {
4198 add_device_config(DEV_SCLP, "stdio");
4199 }
Gerd Hoffmanne1c09172009-12-08 13:11:44 +01004200 if (default_monitor)
Max Reitz4821cd42014-11-17 13:31:04 +01004201 monitor_parse("stdio", "readline", false);
Gerd Hoffmanne1c09172009-12-08 13:11:44 +01004202 }
Gerd Hoffmann998bbd72009-12-08 13:11:41 +01004203 } else {
4204 if (default_serial)
4205 add_device_config(DEV_SERIAL, "vc:80Cx24C");
Gerd Hoffmann6a5e8b02009-12-08 13:11:42 +01004206 if (default_parallel)
4207 add_device_config(DEV_PARALLEL, "vc:80Cx24C");
Gerd Hoffmannabdeed02009-12-08 13:11:43 +01004208 if (default_monitor)
Max Reitz4821cd42014-11-17 13:31:04 +01004209 monitor_parse("vc:80Cx24C", "readline", false);
Alexander Graf38536da2009-12-17 13:06:08 +01004210 if (default_virtcon)
4211 add_device_config(DEV_VIRTCON, "vc:80Cx24C");
Alexander Graf3ef669e2013-01-24 12:18:52 +01004212 if (default_sclp) {
4213 add_device_config(DEV_SCLP, "vc:80Cx24C");
4214 }
aliguoribc0129d2008-08-01 15:12:34 +00004215 }
4216
Gerd Hoffmann006decd2015-02-17 08:45:06 +01004217#if defined(CONFIG_VNC)
4218 if (!QTAILQ_EMPTY(&(qemu_find_opts("vnc")->head))) {
4219 display_remote++;
4220 }
4221#endif
Anthony Liguori15546422013-02-20 07:43:25 -06004222 if (display_type == DT_DEFAULT && !display_remote) {
4223#if defined(CONFIG_GTK)
4224 display_type = DT_GTK;
4225#elif defined(CONFIG_SDL) || defined(CONFIG_COCOA)
4226 display_type = DT_SDL;
4227#elif defined(CONFIG_VNC)
Markus Armbruster70b94332015-02-13 12:50:26 +01004228 vnc_parse("localhost:0,to=99,id=default", &error_abort);
Anthony Liguori15546422013-02-20 07:43:25 -06004229 show_vnc_port = 1;
4230#else
4231 display_type = DT_NONE;
4232#endif
4233 }
4234
Peter Wu047d4e12013-06-11 10:55:08 +02004235 if ((no_frame || alt_grab || ctrl_grab) && display_type != DT_SDL) {
4236 fprintf(stderr, "-no-frame, -alt-grab and -ctrl-grab are only valid "
4237 "for SDL, ignoring option\n");
4238 }
4239 if (no_quit && (display_type != DT_GTK && display_type != DT_SDL)) {
4240 fprintf(stderr, "-no-quit is only valid for GTK and SDL, "
4241 "ignoring option\n");
4242 }
4243
Anthony Liguori15546422013-02-20 07:43:25 -06004244#if defined(CONFIG_GTK)
4245 if (display_type == DT_GTK) {
Gerd Hoffmann97edf3b2015-01-20 12:43:28 +01004246 early_gtk_display_init(request_opengl);
Anthony Liguori15546422013-02-20 07:43:25 -06004247 }
4248#endif
Gerd Hoffmann0b71a5d2014-11-11 16:54:45 +01004249#if defined(CONFIG_SDL)
4250 if (display_type == DT_SDL) {
4251 sdl_display_early_init(request_opengl);
4252 }
4253#endif
4254 if (request_opengl == 1 && display_opengl == 0) {
4255#if defined(CONFIG_OPENGL)
4256 fprintf(stderr, "OpenGL is not supported by the display.\n");
4257#else
4258 fprintf(stderr, "QEMU was built without opengl support.\n");
4259#endif
4260 exit(1);
4261 }
Anthony Liguori15546422013-02-20 07:43:25 -06004262
TeLeMana5829fd2010-04-15 12:37:55 +08004263 socket_init();
4264
Daniel P. Berrangef08f9272015-05-13 17:14:04 +01004265 if (qemu_opts_foreach(qemu_find_opts("object"),
4266 object_create,
4267 object_create_initial, NULL)) {
4268 exit(1);
4269 }
4270
Markus Armbruster28d0de72015-03-13 13:35:14 +01004271 if (qemu_opts_foreach(qemu_find_opts("chardev"),
4272 chardev_init_func, NULL, NULL)) {
Gerd Hoffmann1a688d32009-12-08 13:11:36 +01004273 exit(1);
Markus Armbrustera4c73672015-03-13 11:07:24 +01004274 }
4275
Venkateswararao Jujjuri (JV)758e8e32010-06-14 13:34:41 -07004276#ifdef CONFIG_VIRTFS
Markus Armbruster28d0de72015-03-13 13:35:14 +01004277 if (qemu_opts_foreach(qemu_find_opts("fsdev"),
4278 fsdev_init_func, NULL, NULL)) {
Gautham R Shenoy74db9202010-04-29 17:44:43 +05304279 exit(1);
4280 }
4281#endif
Gerd Hoffmann1a688d32009-12-08 13:11:36 +01004282
thsaa26bb22007-03-25 21:33:06 +00004283 if (pid_file && qemu_create_pidfile(pid_file) != 0) {
Michael Tokarevfee78fd2014-10-30 17:40:48 +03004284 fprintf(stderr, "Could not acquire pid file: %s\n", strerror(errno));
ths93815bc2007-03-19 15:58:31 +00004285 exit(1);
4286 }
4287
Markus Armbruster28d0de72015-03-13 13:35:14 +01004288 if (qemu_opts_foreach(qemu_find_opts("device"),
4289 device_help_func, NULL, NULL)) {
Bruce Rogers3d1d9652012-08-09 12:47:40 -06004290 exit(0);
4291 }
4292
Marcel Apfelbaum6b1b1442014-05-26 15:40:58 +03004293 machine_opts = qemu_get_machine_opts();
Markus Armbruster71df1d82015-03-12 08:40:25 +01004294 if (qemu_opt_foreach(machine_opts, machine_set_property, current_machine,
4295 NULL)) {
Marcel Apfelbaum6b1b1442014-05-26 15:40:58 +03004296 object_unref(OBJECT(current_machine));
4297 exit(1);
4298 }
4299
Eduardo Habkostf6a1ef62014-09-26 17:45:30 -03004300 configure_accelerator(current_machine);
Marcelo Tosatti214910a2009-09-18 02:41:23 -03004301
Paolo Bonzinid4fce242013-10-18 13:51:11 +02004302 if (qtest_chrdev) {
Fam Zheng23802b42014-02-10 09:28:02 +08004303 Error *local_err = NULL;
4304 qtest_init(qtest_chrdev, qtest_log, &local_err);
4305 if (local_err) {
Markus Armbruster565f65d2015-02-12 13:55:05 +01004306 error_report_err(local_err);
Fam Zheng23802b42014-02-10 09:28:02 +08004307 exit(1);
4308 }
Edgar E. Iglesiasa907cf52013-04-15 21:28:17 +02004309 }
4310
Markus Armbruster2ff3de62013-07-04 15:09:22 +02004311 machine_opts = qemu_get_machine_opts();
4312 kernel_filename = qemu_opt_get(machine_opts, "kernel");
4313 initrd_filename = qemu_opt_get(machine_opts, "initrd");
4314 kernel_cmdline = qemu_opt_get(machine_opts, "append");
Gerd Hoffmann1442d3e2013-10-15 16:57:45 +02004315 bios_name = qemu_opt_get(machine_opts, "firmware");
Peter Maydell967c0da2012-02-22 22:40:00 +00004316
Markus Armbruster8281abd2013-06-14 13:15:03 +02004317 opts = qemu_opts_find(qemu_find_opts("boot-opts"), NULL);
4318 if (opts) {
Gonglei703008e2014-12-03 17:11:39 +00004319 Error *local_err = NULL;
Markus Armbruster8281abd2013-06-14 13:15:03 +02004320
Gongleif05f47b2015-02-03 11:31:09 +00004321 boot_order = qemu_opt_get(opts, "order");
4322 if (boot_order) {
4323 validate_bootdevices(boot_order, &local_err);
Gonglei703008e2014-12-03 17:11:39 +00004324 if (local_err) {
Markus Armbruster565f65d2015-02-12 13:55:05 +01004325 error_report_err(local_err);
Gonglei703008e2014-12-03 17:11:39 +00004326 exit(1);
4327 }
Markus Armbruster8281abd2013-06-14 13:15:03 +02004328 }
4329
Gongleif05f47b2015-02-03 11:31:09 +00004330 boot_once = qemu_opt_get(opts, "once");
4331 if (boot_once) {
4332 validate_bootdevices(boot_once, &local_err);
Gonglei703008e2014-12-03 17:11:39 +00004333 if (local_err) {
Markus Armbruster565f65d2015-02-12 13:55:05 +01004334 error_report_err(local_err);
Gonglei703008e2014-12-03 17:11:39 +00004335 exit(1);
4336 }
Markus Armbruster8281abd2013-06-14 13:15:03 +02004337 }
4338
4339 boot_menu = qemu_opt_get_bool(opts, "menu", boot_menu);
Amos Konge5187b52013-12-09 19:53:15 +08004340 boot_strict = qemu_opt_get_bool(opts, "strict", false);
Markus Armbruster8281abd2013-06-14 13:15:03 +02004341 }
4342
Gongleif05f47b2015-02-03 11:31:09 +00004343 if (!boot_order) {
4344 boot_order = machine_class->default_boot_order;
4345 }
4346
Peter Maydella0abe472012-02-08 05:41:39 +00004347 if (!kernel_cmdline) {
4348 kernel_cmdline = "";
Marcel Apfelbaum6b1b1442014-05-26 15:40:58 +03004349 current_machine->kernel_cmdline = (char *)kernel_cmdline;
Peter Maydella0abe472012-02-08 05:41:39 +00004350 }
4351
bellarda20dd502003-09-30 21:07:02 +00004352 linux_boot = (kernel_filename != NULL);
balrog6c41b272007-11-17 12:12:29 +00004353
thsf8d39c02008-07-03 10:01:15 +00004354 if (!linux_boot && *kernel_cmdline != '\0') {
4355 fprintf(stderr, "-append only allowed with -kernel option\n");
4356 exit(1);
4357 }
4358
4359 if (!linux_boot && initrd_filename != NULL) {
4360 fprintf(stderr, "-initrd only allowed with -kernel option\n");
4361 exit(1);
4362 }
4363
Markus Armbruster2ff3de62013-07-04 15:09:22 +02004364 if (!linux_boot && qemu_opt_get(machine_opts, "dtb")) {
Grant Likely412beee2012-03-02 11:56:38 +00004365 fprintf(stderr, "-dtb only allowed with -kernel option\n");
4366 exit(1);
4367 }
4368
Leon Alraea59d31a2015-06-19 14:17:45 +01004369 if (semihosting_enabled() && !semihosting_get_argc() && kernel_filename) {
4370 /* fall back to the -kernel/-append */
4371 semihosting_arg_fallback(kernel_filename, kernel_cmdline);
4372 }
4373
Jes Sorensen9156d762010-06-10 11:42:30 +02004374 os_set_line_buffering();
ths3b46e622007-09-17 08:09:54 +00004375
Alon Levyad1be892012-03-14 20:33:37 +02004376#ifdef CONFIG_SPICE
4377 /* spice needs the timers to be initialized by this point */
4378 qemu_spice_init();
4379#endif
4380
Pavel Dovgalyuk4603ea02014-09-01 09:34:49 +04004381 cpu_ticks_init();
Sebastian Tanase1ad95802014-07-25 11:56:28 +02004382 if (icount_opts) {
4383 if (kvm_enabled() || xen_enabled()) {
4384 fprintf(stderr, "-icount is not allowed with kvm or xen\n");
4385 exit(1);
4386 }
4387 configure_icount(icount_opts, &error_abort);
4388 qemu_opts_del(icount_opts);
Max Filippov0abe9052011-11-10 15:38:42 +04004389 }
pbrook634fce92006-07-15 17:40:09 +00004390
Amos Kongf30dbae2012-12-11 22:20:15 +08004391 /* clean up network at qemu process termination */
4392 atexit(&net_cleanup);
4393
Mark McLoughlindc1c9fe2009-10-06 12:17:16 +01004394 if (net_init_clients() < 0) {
4395 exit(1);
bellard702c6512004-04-02 21:21:32 +00004396 }
bellardf1510b22003-06-25 00:07:40 +00004397
Yang Hongyang9abce562015-10-07 11:52:13 +08004398 if (qemu_opts_foreach(qemu_find_opts("object"),
4399 object_create,
4400 object_create_delayed, NULL)) {
4401 exit(1);
4402 }
4403
Stefan Bergerd1a0cf72013-02-27 12:47:49 -05004404#ifdef CONFIG_TPM
4405 if (tpm_init() < 0) {
4406 exit(1);
4407 }
4408#endif
4409
balrogdc72ac12008-11-09 00:04:26 +00004410 /* init the bluetooth world */
Gerd Hoffmannbd3c9482009-07-15 13:59:26 +02004411 if (foreach_device_config(DEV_BT, bt_parse))
4412 exit(1);
balrogdc72ac12008-11-09 00:04:26 +00004413
Anthony PERARD834e76e2011-07-20 08:17:44 +00004414 if (!xen_enabled()) {
4415 /* On 32-bit hosts, QEMU is limited by virtual address space */
4416 if (ram_size > (2047 << 20) && HOST_LONG_BITS == 32) {
4417 fprintf(stderr, "qemu: at most 2047 MB RAM can be simulated\n");
4418 exit(1);
4419 }
4420 }
balrog7fb4fdc2008-04-24 17:59:27 +00004421
lirans@il.ibm.comc163b5c2009-11-02 15:40:58 +02004422 blk_mig_init();
Dr. David Alan Gilbert0d6ab3a2014-03-19 18:32:30 +00004423 ram_mig_init();
lirans@il.ibm.comc163b5c2009-11-02 15:40:58 +02004424
John Snow16026512014-10-01 14:19:26 -04004425 /* If the currently selected machine wishes to override the units-per-bus
4426 * property of its default HBA interface type, do so now. */
4427 if (machine_class->units_per_default_bus) {
4428 override_max_devs(machine_class->block_default_type,
4429 machine_class->units_per_default_bus);
4430 }
4431
ths96d30e42007-01-07 20:42:14 +00004432 /* open the virtual block devices */
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +02004433 if (snapshot)
Markus Armbruster28d0de72015-03-13 13:35:14 +01004434 qemu_opts_foreach(qemu_find_opts("drive"),
4435 drive_enable_snapshot, NULL, NULL);
Christian Borntraeger2d0d2832012-11-20 15:30:34 +01004436 if (qemu_opts_foreach(qemu_find_opts("drive"), drive_init_func,
Markus Armbruster28d0de72015-03-13 13:35:14 +01004437 &machine_class->block_default_type, NULL)) {
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +02004438 exit(1);
Christian Borntraeger2d0d2832012-11-20 15:30:34 +01004439 }
balrog3e3d5812007-04-30 02:09:25 +00004440
Marcel Apfelbaum958db902014-04-09 20:34:53 +03004441 default_drive(default_cdrom, snapshot, machine_class->block_default_type, 2,
Christian Borntraeger3c42ea62012-11-22 21:02:55 +01004442 CDROM_OPTS);
4443 default_drive(default_floppy, snapshot, IF_FLOPPY, 0, FD_OPTS);
4444 default_drive(default_sdcard, snapshot, IF_SD, 0, SD_OPTS);
Markus Armbruster4e5d9b52011-01-28 11:21:45 +01004445
Igor Mammedov57924bc2015-03-19 17:09:21 +00004446 parse_numa_opts(machine_class);
aliguori268a3622009-04-21 22:30:27 +00004447
Markus Armbruster28d0de72015-03-13 13:35:14 +01004448 if (qemu_opts_foreach(qemu_find_opts("mon"),
4449 mon_init_func, NULL, NULL)) {
Jan Kiszka157b9312010-04-06 16:55:53 +02004450 exit(1);
4451 }
4452
Gerd Hoffmann998bbd72009-12-08 13:11:41 +01004453 if (foreach_device_config(DEV_SERIAL, serial_parse) < 0)
4454 exit(1);
Gerd Hoffmann6a5e8b02009-12-08 13:11:42 +01004455 if (foreach_device_config(DEV_PARALLEL, parallel_parse) < 0)
4456 exit(1);
Gerd Hoffmannaee1b932009-12-08 13:11:53 +01004457 if (foreach_device_config(DEV_VIRTCON, virtcon_parse) < 0)
4458 exit(1);
Alexander Graf3ef669e2013-01-24 12:18:52 +01004459 if (foreach_device_config(DEV_SCLP, sclp_parse) < 0) {
4460 exit(1);
4461 }
H. Peter Anvinc9f398e2009-12-29 13:51:36 -08004462 if (foreach_device_config(DEV_DEBUGCON, debugcon_parse) < 0)
4463 exit(1);
aliguori2796dae2009-01-16 20:23:27 +00004464
Paolo Bonzini7f1b17f2012-05-10 09:39:17 +02004465 /* If no default VGA is requested, the default is "none". */
Aurelien Jarno3605ded2012-09-10 01:01:44 +02004466 if (default_vga) {
Gerd Hoffmann6f004942014-10-28 10:09:11 +01004467 if (machine_class->default_display) {
4468 vga_model = machine_class->default_display;
4469 } else if (cirrus_vga_available()) {
Aurelien Jarno3605ded2012-09-10 01:01:44 +02004470 vga_model = "cirrus";
4471 } else if (vga_available()) {
4472 vga_model = "std";
4473 }
Blue Swirla369da52011-09-27 19:15:42 +00004474 }
Mark Wud44229c2014-03-10 22:37:40 +08004475 if (vga_model) {
4476 select_vgahw(vga_model);
4477 }
Blue Swirla369da52011-09-27 19:15:42 +00004478
Markus Armbruster09aaa162009-08-21 10:31:34 +02004479 if (watchdog) {
4480 i = select_watchdog(watchdog);
4481 if (i > 0)
4482 exit (i == 1 ? 1 : 0);
4483 }
4484
Marcel Apfelbaum958db902014-04-09 20:34:53 +03004485 if (machine_class->compat_props) {
4486 qdev_prop_register_global_list(machine_class->compat_props);
Gerd Hoffmannb6b61142009-07-15 13:48:21 +02004487 }
Gerd Hoffmannd0fef6f2009-12-08 13:11:34 +01004488 qemu_add_globals();
4489
Anthony Liguori1de81d22011-12-19 16:37:46 -06004490 qdev_machine_init();
4491
Marcel Apfelbaum3ef96222014-05-07 17:42:57 +03004492 current_machine->ram_size = ram_size;
Igor Mammedovc270fb92014-06-02 15:25:02 +02004493 current_machine->maxram_size = maxram_size;
4494 current_machine->ram_slots = ram_slots;
Marcel Apfelbaum3ef96222014-05-07 17:42:57 +03004495 current_machine->boot_order = boot_order;
Marcel Apfelbaum3ef96222014-05-07 17:42:57 +03004496 current_machine->cpu_model = cpu_model;
Marcel Apfelbaum0056ae22014-03-05 19:30:47 +02004497
Marcel Apfelbaum3ef96222014-05-07 17:42:57 +03004498 machine_class->init(current_machine);
aliguori3023f332009-01-16 19:04:14 +00004499
zhanghailiang28d16f32014-09-23 18:42:24 +08004500 realtime_init();
4501
Paolo Bonzinib3e6d592013-04-18 18:44:04 +02004502 audio_init();
4503
Jan Kiszkaea375f92010-03-01 19:10:30 +01004504 cpu_synchronize_all_post_init();
aliguori268a3622009-04-21 22:30:27 +00004505
Eduardo Habkostdde11112015-02-08 16:51:22 -02004506 numa_post_machine_init();
aliguori268a3622009-04-21 22:30:27 +00004507
Gabriel L. Somlo81b2b812015-04-29 11:21:53 -04004508 if (qemu_opts_foreach(qemu_find_opts("fw_cfg"),
4509 parse_fw_cfg, fw_cfg_find(), NULL) != 0) {
4510 exit(1);
4511 }
4512
aliguori3023f332009-01-16 19:04:14 +00004513 /* init USB devices */
Marcel Apfelbaumde77a242015-01-06 15:29:14 +02004514 if (usb_enabled()) {
Markus Armbruster07527062009-10-06 12:16:57 +01004515 if (foreach_device_config(DEV_USB, usb_parse) < 0)
4516 exit(1);
aliguori3023f332009-01-16 19:04:14 +00004517 }
4518
Tiejun Chen79814172015-07-15 13:37:45 +08004519 /* Check if IGD GFX passthrough. */
4520 igd_gfx_passthru();
4521
Gerd Hoffmannbd3c9482009-07-15 13:59:26 +02004522 /* init generic devices */
Markus Armbruster28d0de72015-03-13 13:35:14 +01004523 if (qemu_opts_foreach(qemu_find_opts("device"),
4524 device_init_func, NULL, NULL)) {
Gerd Hoffmannbd3c9482009-07-15 13:59:26 +02004525 exit(1);
Markus Armbrustera4c73672015-03-13 11:07:24 +01004526 }
Gerd Hoffmannbd3c9482009-07-15 13:59:26 +02004527
John Snowa66c9dc2014-10-01 14:19:24 -04004528 /* Did we create any drives that we failed to create a device for? */
4529 drive_check_orphaned();
4530
Markus Armbruster668680f2010-02-11 14:44:58 +01004531 net_check_clients();
4532
Gongleif05f47b2015-02-03 11:31:09 +00004533 if (boot_once) {
4534 Error *local_err = NULL;
4535 qemu_boot_set(boot_once, &local_err);
4536 if (local_err) {
4537 error_report("%s", error_get_pretty(local_err));
4538 exit(1);
4539 }
4540 qemu_register_reset(restore_boot_order, g_strdup(boot_order));
4541 }
4542
Gerd Hoffmann64840c62013-03-07 17:08:29 +01004543 ds = init_displaystate();
Anthony Liguori993fbfd2009-05-21 16:54:00 -05004544
Gerd Hoffmann6b62dc22010-08-25 10:51:06 +02004545 /* init local displays */
Anthony Liguori993fbfd2009-05-21 16:54:00 -05004546 switch (display_type) {
4547 case DT_NOGRAPHIC:
Antony Pavlov8111d022013-10-29 08:36:31 +04004548 (void)ds; /* avoid warning if no display is configured */
Anthony Liguori993fbfd2009-05-21 16:54:00 -05004549 break;
4550#if defined(CONFIG_CURSES)
4551 case DT_CURSES:
Michael Tokarevab51b1d2012-12-30 12:48:14 +04004552 curses_display_init(ds, full_screen);
Anthony Liguori993fbfd2009-05-21 16:54:00 -05004553 break;
4554#endif
bellard5b0753e2005-03-01 21:37:28 +00004555#if defined(CONFIG_SDL)
Anthony Liguori993fbfd2009-05-21 16:54:00 -05004556 case DT_SDL:
4557 sdl_display_init(ds, full_screen, no_frame);
4558 break;
bellard5b0753e2005-03-01 21:37:28 +00004559#elif defined(CONFIG_COCOA)
Anthony Liguori993fbfd2009-05-21 16:54:00 -05004560 case DT_SDL:
4561 cocoa_display_init(ds, full_screen);
4562 break;
bellard313aa562003-08-10 21:52:11 +00004563#endif
Anthony Liguori15546422013-02-20 07:43:25 -06004564#if defined(CONFIG_GTK)
4565 case DT_GTK:
Jan Kiszka881249c2014-03-12 08:33:50 +01004566 gtk_display_init(ds, full_screen, grab_on_hover);
Anthony Liguori15546422013-02-20 07:43:25 -06004567 break;
4568#endif
Gerd Hoffmann6b62dc22010-08-25 10:51:06 +02004569 default:
4570 break;
4571 }
4572
Gleb Natapov0ce235a2011-03-31 11:27:23 +02004573 /* must be after terminal init, SDL library changes signal handlers */
4574 os_setup_signal_handling();
4575
Jes Sorensen821601e2011-03-16 13:33:36 +01004576#ifdef CONFIG_VNC
Gerd Hoffmann6b62dc22010-08-25 10:51:06 +02004577 /* init remote displays */
Markus Armbruster28d0de72015-03-13 13:35:14 +01004578 qemu_opts_foreach(qemu_find_opts("vnc"),
4579 vnc_init_func, NULL, NULL);
Gerd Hoffmann4db14622014-09-16 12:33:03 +02004580 if (show_vnc_port) {
Shannon Zhaobd4baf62015-05-25 14:47:25 +08004581 char *ret = vnc_display_local_addr("default");
4582 printf("VNC server running on `%s'\n", ret);
4583 g_free(ret);
bellard313aa562003-08-10 21:52:11 +00004584 }
Jes Sorensen821601e2011-03-16 13:33:36 +01004585#endif
Gerd Hoffmanna3e22262010-08-25 15:32:06 +02004586#ifdef CONFIG_SPICE
Gerd Hoffmann9fa03282013-10-11 22:39:59 +02004587 if (using_spice) {
4588 qemu_spice_display_init();
Gerd Hoffmanna3e22262010-08-25 15:32:06 +02004589 }
4590#endif
aliguori5b08fc12008-08-21 20:08:03 +00004591
Markus Armbrusteref0c4a02012-02-07 15:09:13 +01004592 if (foreach_device_config(DEV_GDB, gdbserver_start) < 0) {
aliguori59030a82009-04-05 18:43:41 +00004593 exit(1);
balrog45669e02007-07-02 13:20:17 +00004594 }
balrog45669e02007-07-02 13:20:17 +00004595
Gerd Hoffmann3418bd22009-09-25 21:42:41 +02004596 qdev_machine_creation_done();
4597
Isaku Yamahata80376c32010-12-20 14:33:35 +09004598 /* TODO: once all bus devices are qdevified, this should be done
4599 * when bus is created by qdev.c */
4600 qemu_register_reset(qbus_reset_all_fn, sysbus_get_default());
Gleb Natapov4cab9462010-12-08 13:35:08 +02004601 qemu_run_machine_init_done_notifiers();
4602
Eric Auger6b3f7f62015-06-16 17:07:54 +01004603 if (rom_check_and_register_reset() != 0) {
4604 fprintf(stderr, "rom check and register reset failed\n");
4605 exit(1);
4606 }
Michael S. Tsirkind916b462013-07-24 18:56:08 +03004607
Jan Kiszkae063eb12011-06-14 18:29:43 +02004608 qemu_system_reset(VMRESET_SILENT);
Juan Quintela48212d82015-07-10 14:51:58 +02004609 register_global_state();
Juan Quintela05f24012009-08-20 19:42:22 +02004610 if (loadvm) {
Markus Armbruster03cd4652010-02-17 16:24:10 +01004611 if (load_vmstate(loadvm) < 0) {
Juan Quintela05f24012009-08-20 19:42:22 +02004612 autostart = 0;
4613 }
4614 }
bellardd63d3072004-10-03 13:29:03 +00004615
Eduardo Habkostd828c432014-08-08 16:03:30 -03004616 qdev_prop_check_globals();
Amit Shahabfd9ce2014-06-20 18:56:08 +05304617 if (vmstate_dump_file) {
4618 /* dump and exit */
4619 dump_vmstate_json_to_file(vmstate_dump_file);
4620 return 0;
4621 }
Don Slutz9f9260a2014-05-05 14:03:06 -04004622
Glauber Costa2bb8c102009-07-24 16:20:23 -04004623 if (incoming) {
Paolo Bonzini43eaae22012-10-02 18:21:18 +02004624 Error *local_err = NULL;
4625 qemu_start_incoming_migration(incoming, &local_err);
4626 if (local_err) {
Seiji Aguchi4a44d852013-08-05 15:40:44 -04004627 error_report("-incoming %s: %s", incoming,
4628 error_get_pretty(local_err));
Paolo Bonzini43eaae22012-10-02 18:21:18 +02004629 error_free(local_err);
4630 exit(1);
Juan Quintela8ca5e802010-06-09 14:10:54 +02004631 }
Avi Kivity6b99dad2009-08-09 14:39:20 +03004632 } else if (autostart) {
aliguoric0f4ce72009-03-05 23:01:01 +00004633 vm_start();
Avi Kivity6b99dad2009-08-09 14:39:20 +03004634 }
thsffd843b2006-12-21 19:46:43 +00004635
Jes Sorenseneb505be2010-06-10 11:42:28 +02004636 os_setup_post();
ths71e3ceb2006-12-22 02:11:31 +00004637
Michael Mueller8a745f22013-09-23 16:36:54 +02004638 if (is_daemonized()) {
LluĂ­s Vilanova5b808272014-05-27 15:02:14 +02004639 if (!trace_init_backends(trace_events, trace_file)) {
Michael Mueller8a745f22013-09-23 16:36:54 +02004640 exit(1);
4641 }
4642 }
4643
bellard8a7ddc32004-03-31 19:00:16 +00004644 main_loop();
Paolo Bonzini99435902011-09-12 14:03:13 +02004645 bdrv_close_all();
4646 pause_all_vcpus();
wayne3d3b8302011-07-27 18:04:55 +08004647 res_free();
Stefan Bergerd1a0cf72013-02-27 12:47:49 -05004648#ifdef CONFIG_TPM
4649 tpm_cleanup();
4650#endif
thsb46a8902007-10-21 23:20:45 +00004651
bellard0824d6f2003-06-24 13:42:40 +00004652 return 0;
4653}