blob: 4bc8f97c25a2e300bf856fec177e69fbcc51125b [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
Marcel Apfelbaumc8897e82014-03-18 17:26:35 +020061#include "qemu/sockets.h"
blueswir1511d2b12009-03-07 15:32:56 +000062#include "hw/hw.h"
63#include "hw/boards.h"
64#include "hw/usb.h"
65#include "hw/pcmcia.h"
Paolo Bonzini0d09e412013-02-05 17:06:20 +010066#include "hw/i386/pc.h"
67#include "hw/isa/isa.h"
blueswir1511d2b12009-03-07 15:32:56 +000068#include "hw/bt.h"
Paolo Bonzini0d09e412013-02-05 17:06:20 +010069#include "sysemu/watchdog.h"
70#include "hw/i386/smbios.h"
71#include "hw/xen/xen.h"
Gerd Hoffmannbd3c9482009-07-15 13:59:26 +020072#include "hw/qdev.h"
Gerd Hoffmann45a50b12009-10-01 16:42:33 +020073#include "hw/loader.h"
Paolo Bonzinib4a42f82013-02-04 11:37:52 +010074#include "monitor/qdev.h"
Paolo Bonzinidccfcd02013-04-08 16:55:25 +020075#include "sysemu/bt.h"
Paolo Bonzini1422e322012-10-24 08:43:34 +020076#include "net/net.h"
Mark McLoughlin68ac40d2009-11-25 18:48:54 +000077#include "net/slirp.h"
Paolo Bonzini83c90892012-12-17 18:19:49 +010078#include "monitor/monitor.h"
Paolo Bonzini28ecbae2012-11-28 12:06:30 +010079#include "ui/console.h"
Paolo Bonzini9c17d612012-12-17 18:20:04 +010080#include "sysemu/sysemu.h"
Paolo Bonzini022c62c2012-12-17 18:19:49 +010081#include "exec/gdbstub.h"
Paolo Bonzini1de7afc2012-12-17 18:20:00 +010082#include "qemu/timer.h"
Paolo Bonzinidccfcd02013-04-08 16:55:25 +020083#include "sysemu/char.h"
Michael Tokarevb33276a2014-02-07 12:26:14 +040084#include "qemu/bitmap.h"
Paolo Bonzini9c17d612012-12-17 18:20:04 +010085#include "sysemu/blockdev.h"
Paolo Bonzini0d09e412013-02-05 17:06:20 +010086#include "hw/block/block.h"
Paolo Bonzinicaf71f82012-12-17 18:19:50 +010087#include "migration/block.h"
Paolo Bonzinibdee56f2013-04-02 18:28:41 +020088#include "sysemu/tpm.h"
Paolo Bonzini9c17d612012-12-17 18:20:04 +010089#include "sysemu/dma.h"
blueswir1511d2b12009-03-07 15:32:56 +000090#include "audio/audio.h"
Paolo Bonzinicaf71f82012-12-17 18:19:50 +010091#include "migration/migration.h"
Paolo Bonzini9c17d612012-12-17 18:20:04 +010092#include "sysemu/kvm.h"
Paolo Bonzini7b1b5d12012-12-17 18:19:43 +010093#include "qapi/qmp/qjson.h"
Paolo Bonzini1de7afc2012-12-17 18:20:00 +010094#include "qemu/option.h"
95#include "qemu/config-file.h"
Jes Sorensen59a52642010-06-10 11:42:25 +020096#include "qemu-options.h"
Luiz Capitulino1fa9a5e2011-09-12 17:54:20 -030097#include "qmp-commands.h"
Paolo Bonzini1de7afc2012-12-17 18:20:00 +010098#include "qemu/main-loop.h"
Venkateswararao Jujjuri (JV)758e8e32010-06-14 13:34:41 -070099#ifdef CONFIG_VIRTFS
Gautham R Shenoy74db9202010-04-29 17:44:43 +0530100#include "fsdev/qemu-fsdev.h"
101#endif
Paolo Bonzini9c17d612012-12-17 18:20:04 +0100102#include "sysemu/qtest.h"
blueswir1511d2b12009-03-07 15:32:56 +0000103
Paolo Bonzini76cad712012-10-24 11:12:21 +0200104#include "disas/disas.h"
bellardfc01f7e2003-06-30 10:03:06 +0000105
blueswir1511d2b12009-03-07 15:32:56 +0000106
Jan Kiszkad918f232009-06-24 14:42:30 +0200107#include "slirp/libslirp.h"
blueswir1511d2b12009-03-07 15:32:56 +0000108
Stefan Hajnoczi94b0b5f2010-11-16 12:20:25 +0000109#include "trace.h"
LluĂ­se4858972011-08-31 20:31:03 +0200110#include "trace/control.h"
Paolo Bonzini1de7afc2012-12-17 18:20:00 +0100111#include "qemu/queue.h"
Paolo Bonzini9c17d612012-12-17 18:20:04 +0100112#include "sysemu/cpus.h"
113#include "sysemu/arch_init.h"
Paolo Bonzini1de7afc2012-12-17 18:20:00 +0100114#include "qemu/osdep.h"
Blue Swirl72cf2d42009-09-12 07:36:22 +0000115
Gerd Hoffmann29b00402010-03-11 11:13:27 -0300116#include "ui/qemu-spice.h"
Anthony Liguori68d98d32012-06-25 14:36:33 -0500117#include "qapi/string-input-visitor.h"
Paolo Bonzinic4090f82014-06-10 19:15:15 +0800118#include "qapi/opts-visitor.h"
Wenchao Xia84321832014-06-18 08:43:33 +0200119#include "qom/object_interfaces.h"
120#include "qapi-event.h"
Gerd Hoffmann29b00402010-03-11 11:13:27 -0300121
bellard1bfe8562004-07-08 21:17:50 +0000122#define DEFAULT_RAM_SIZE 128
bellard313aa562003-08-10 21:52:11 +0000123
Amit Shah98b19252010-01-20 00:36:52 +0530124#define MAX_VIRTIO_CONSOLES 1
Alexander Graf3ef669e2013-01-24 12:18:52 +0100125#define MAX_SCLP_CONSOLES 1
Amit Shah98b19252010-01-20 00:36:52 +0530126
Gerd Hoffmann45240512013-03-08 11:42:24 +0100127static const char *data_dir[16];
128static int data_dir_idx;
j_mayer1192dad2007-10-05 13:08:35 +0000129const char *bios_name = NULL;
malccb5a7aa2008-09-28 00:42:12 +0000130enum vga_retrace_method vga_retrace_method = VGA_RETRACE_DUMB;
Anthony Liguori993fbfd2009-05-21 16:54:00 -0500131DisplayType display_type = DT_DEFAULT;
Blue Swirl4fdcac02012-10-28 11:04:47 +0000132static int display_remote;
bellard3d11d0e2004-12-12 16:56:30 +0000133const char* keyboard_layout = NULL;
Anthony Liguoric227f092009-10-01 16:12:16 -0500134ram_addr_t ram_size;
Marcelo Tosattic9027602010-03-01 20:25:08 -0300135const char *mem_path = NULL;
Marcelo Tosattic9027602010-03-01 20:25:08 -0300136int mem_prealloc = 0; /* force preallocation of physical target memory */
zhanghailiang28d16f32014-09-23 18:42:24 +0800137bool enable_mlock = false;
bellardc4b1fcc2004-03-14 21:44:30 +0000138int nb_nics;
bellard7c9d8e02005-11-15 22:16:05 +0000139NICInfo nd_table[MAX_NICS];
Paolo Bonzinid399f672009-07-27 23:17:51 +0200140int autostart;
balrogf6503052008-02-17 11:42:19 +0000141static int rtc_utc = 1;
142static int rtc_date_offset = -1; /* -1 means no change */
Alex Bligh884f17c2013-08-21 16:03:04 +0100143QEMUClockType rtc_clock;
Gerd Hoffmann64465292009-12-08 13:11:45 +0100144int vga_interface_type = VGA_NONE;
blueswir1dbed7e42008-10-01 19:38:09 +0000145static int full_screen = 0;
146static int no_frame = 0;
ths667acca2006-12-11 02:08:05 +0000147int no_quit = 0;
Jan Kiszka881249c2014-03-12 08:33:50 +0100148#ifdef CONFIG_GTK
149static bool grab_on_hover;
150#endif
bellard8d11df92004-08-24 21:13:40 +0000151CharDriverState *serial_hds[MAX_SERIAL_PORTS];
bellard6508fe52005-01-15 12:02:56 +0000152CharDriverState *parallel_hds[MAX_PARALLEL_PORTS];
aliguori9ede2fd2009-01-15 20:05:25 +0000153CharDriverState *virtcon_hds[MAX_VIRTIO_CONSOLES];
Alexander Graf3ef669e2013-01-24 12:18:52 +0100154CharDriverState *sclp_hds[MAX_SCLP_CONSOLES];
bellarda09db212005-04-30 16:10:35 +0000155int win2k_install_hack = 0;
aurel321b530a62009-04-05 20:08:59 +0000156int singlestep = 0;
bellard6a00d602005-11-21 23:25:50 +0000157int smp_cpus = 1;
Jes Sorensen6be68d72009-07-23 17:03:42 +0200158int max_cpus = 0;
Andre Przywaradc6b1c02009-08-19 15:42:40 +0200159int smp_cores = 1;
160int smp_threads = 1;
Jes Sorensen821601e2011-03-16 13:33:36 +0100161#ifdef CONFIG_VNC
ths73fc9742006-12-22 02:09:07 +0000162const char *vnc_display;
Jes Sorensen821601e2011-03-16 13:33:36 +0100163#endif
bellard6515b202006-05-03 22:02:44 +0000164int acpi_enabled = 1;
aliguori16b29ae2008-12-17 23:28:44 +0000165int no_hpet = 0;
bellard52ca8d62006-06-14 16:03:05 +0000166int fd_bootchk = 1;
Blue Swirl4fdcac02012-10-28 11:04:47 +0000167static int no_reboot;
aurel32b2f76162008-04-11 21:35:52 +0000168int no_shutdown = 0;
balrog9467cd42007-05-01 01:34:14 +0000169int cursor_hide = 1;
balroga171fe32007-04-30 01:48:07 +0000170int graphic_rotate = 0;
Markus Armbruster09aaa162009-08-21 10:31:34 +0200171const char *watchdog;
Gleb Natapov2e55e842010-12-08 13:35:07 +0200172QEMUOptionRom option_rom[MAX_OPTION_ROMS];
ths9ae02552007-01-05 17:39:04 +0000173int nb_option_roms;
pbrook8e716212007-01-20 17:12:09 +0000174int semihosting_enabled = 0;
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;
Michael Tokarev6f2bfda2013-12-09 12:18:46 +0400182static bool 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 +0200188typedef struct FWBootEntry FWBootEntry;
189
190struct FWBootEntry {
191 QTAILQ_ENTRY(FWBootEntry) link;
192 int32_t bootindex;
193 DeviceState *dev;
194 char *suffix;
195};
196
Blue Swirl4fdcac02012-10-28 11:04:47 +0000197static QTAILQ_HEAD(, FWBootEntry) fw_boot_order =
198 QTAILQ_HEAD_INITIALIZER(fw_boot_order);
Gleb Natapov1ca4d092010-12-08 13:35:05 +0200199
aliguori268a3622009-04-21 22:30:27 +0000200int nb_numa_nodes;
Eduardo Habkost1af878e2014-06-26 18:33:18 -0300201int max_numa_nodeid;
Wanlong Gao8c859012014-05-14 17:43:07 +0800202NodeInfo numa_info[MAX_NODES];
aliguori268a3622009-04-21 22:30:27 +0000203
blueswir18fcb1b92008-09-18 18:29:08 +0000204uint8_t qemu_uuid[16];
Markus Armbrusterfc3b3292013-08-16 15:18:31 +0200205bool qemu_uuid_set;
blueswir18fcb1b92008-09-18 18:29:08 +0000206
Jan Kiszka76e30d02009-07-02 00:19:02 +0200207static QEMUBootSetHandler *boot_set_handler;
208static void *boot_set_opaque;
209
Gerd Hoffmannfd42dee2010-06-04 14:08:07 +0200210static NotifierList exit_notifiers =
211 NOTIFIER_LIST_INITIALIZER(exit_notifiers);
212
Gleb Natapov4cab9462010-12-08 13:35:08 +0200213static NotifierList machine_init_done_notifiers =
214 NOTIFIER_LIST_INITIALIZER(machine_init_done_notifiers);
215
liguangd5286af2013-01-24 13:03:27 +0800216static bool tcg_allowed = true;
liguangd5286af2013-01-24 13:03:27 +0800217bool xen_allowed;
Blue Swirld745bef2010-03-29 19:23:49 +0000218uint32_t xen_domid;
219enum xen_mode xen_mode = XEN_EMULATE;
Jan Kiszkad5ab9712011-08-02 16:10:21 +0200220static int tcg_tb_size;
Blue Swirld745bef2010-03-29 19:23:49 +0000221
Mark Wud44229c2014-03-10 22:37:40 +0800222static int has_defaults = 1;
Gerd Hoffmann998bbd72009-12-08 13:11:41 +0100223static int default_serial = 1;
Gerd Hoffmann6a5e8b02009-12-08 13:11:42 +0100224static int default_parallel = 1;
Gerd Hoffmann986c5f72009-12-08 13:11:54 +0100225static int default_virtcon = 1;
Alexander Graf3ef669e2013-01-24 12:18:52 +0100226static int default_sclp = 1;
Gerd Hoffmannabdeed02009-12-08 13:11:43 +0100227static int default_monitor = 1;
Gerd Hoffmannac33f8f2009-12-16 14:25:39 +0100228static int default_floppy = 1;
229static int default_cdrom = 1;
230static int default_sdcard = 1;
Paolo Bonzini7f1b17f2012-05-10 09:39:17 +0200231static int default_vga = 1;
Gerd Hoffmann998bbd72009-12-08 13:11:41 +0100232
233static struct {
234 const char *driver;
235 int *flag;
236} default_list[] = {
Gerd Hoffmann6a5e8b02009-12-08 13:11:42 +0100237 { .driver = "isa-serial", .flag = &default_serial },
238 { .driver = "isa-parallel", .flag = &default_parallel },
Gerd Hoffmannd8bcbab2009-12-16 14:25:40 +0100239 { .driver = "isa-fdc", .flag = &default_floppy },
Markus Armbrusteraf6bf132011-05-18 18:31:02 +0200240 { .driver = "ide-cd", .flag = &default_cdrom },
241 { .driver = "ide-hd", .flag = &default_cdrom },
Gerd Hoffmannd8bcbab2009-12-16 14:25:40 +0100242 { .driver = "ide-drive", .flag = &default_cdrom },
Markus Armbrusteraf6bf132011-05-18 18:31:02 +0200243 { .driver = "scsi-cd", .flag = &default_cdrom },
Amit Shah392ecf52010-01-21 16:19:23 +0530244 { .driver = "virtio-serial-pci", .flag = &default_virtcon },
245 { .driver = "virtio-serial-s390", .flag = &default_virtcon },
246 { .driver = "virtio-serial", .flag = &default_virtcon },
Paolo Bonzini7f1b17f2012-05-10 09:39:17 +0200247 { .driver = "VGA", .flag = &default_vga },
248 { .driver = "isa-vga", .flag = &default_vga },
249 { .driver = "cirrus-vga", .flag = &default_vga },
250 { .driver = "isa-cirrus-vga", .flag = &default_vga },
251 { .driver = "vmware-svga", .flag = &default_vga },
252 { .driver = "qxl-vga", .flag = &default_vga },
Gerd Hoffmann998bbd72009-12-08 13:11:41 +0100253};
254
Paolo Bonzini4d454572012-11-26 16:03:42 +0100255static QemuOptsList qemu_rtc_opts = {
256 .name = "rtc",
257 .head = QTAILQ_HEAD_INITIALIZER(qemu_rtc_opts.head),
258 .desc = {
259 {
260 .name = "base",
261 .type = QEMU_OPT_STRING,
262 },{
263 .name = "clock",
264 .type = QEMU_OPT_STRING,
265 },{
266 .name = "driftfix",
267 .type = QEMU_OPT_STRING,
268 },
269 { /* end of list */ }
270 },
271};
272
273static QemuOptsList qemu_sandbox_opts = {
274 .name = "sandbox",
275 .implied_opt_name = "enable",
276 .head = QTAILQ_HEAD_INITIALIZER(qemu_sandbox_opts.head),
277 .desc = {
278 {
279 .name = "enable",
280 .type = QEMU_OPT_BOOL,
281 },
282 { /* end of list */ }
283 },
284};
285
286static QemuOptsList qemu_trace_opts = {
287 .name = "trace",
288 .implied_opt_name = "trace",
289 .head = QTAILQ_HEAD_INITIALIZER(qemu_trace_opts.head),
290 .desc = {
291 {
292 .name = "events",
293 .type = QEMU_OPT_STRING,
294 },{
295 .name = "file",
296 .type = QEMU_OPT_STRING,
297 },
298 { /* end of list */ }
299 },
300};
301
302static QemuOptsList qemu_option_rom_opts = {
303 .name = "option-rom",
304 .implied_opt_name = "romfile",
305 .head = QTAILQ_HEAD_INITIALIZER(qemu_option_rom_opts.head),
306 .desc = {
307 {
308 .name = "bootindex",
309 .type = QEMU_OPT_NUMBER,
310 }, {
311 .name = "romfile",
312 .type = QEMU_OPT_STRING,
313 },
314 { /* end of list */ }
315 },
316};
317
318static QemuOptsList qemu_machine_opts = {
319 .name = "machine",
320 .implied_opt_name = "type",
321 .merge_lists = true,
322 .head = QTAILQ_HEAD_INITIALIZER(qemu_machine_opts.head),
323 .desc = {
324 {
325 .name = "type",
326 .type = QEMU_OPT_STRING,
327 .help = "emulated machine"
328 }, {
329 .name = "accel",
330 .type = QEMU_OPT_STRING,
331 .help = "accelerator list",
332 }, {
333 .name = "kernel_irqchip",
334 .type = QEMU_OPT_BOOL,
335 .help = "use KVM in-kernel irqchip",
336 }, {
337 .name = "kvm_shadow_mem",
338 .type = QEMU_OPT_SIZE,
339 .help = "KVM shadow MMU size",
340 }, {
341 .name = "kernel",
342 .type = QEMU_OPT_STRING,
343 .help = "Linux kernel image file",
344 }, {
345 .name = "initrd",
346 .type = QEMU_OPT_STRING,
347 .help = "Linux initial ramdisk file",
348 }, {
349 .name = "append",
350 .type = QEMU_OPT_STRING,
351 .help = "Linux kernel command line",
352 }, {
353 .name = "dtb",
354 .type = QEMU_OPT_STRING,
355 .help = "Linux kernel device tree file",
356 }, {
357 .name = "dumpdtb",
358 .type = QEMU_OPT_STRING,
359 .help = "Dump current dtb to a file and quit",
360 }, {
361 .name = "phandle_start",
Markus Armbrusterc1b71b02013-07-04 15:09:23 +0200362 .type = QEMU_OPT_NUMBER,
Paolo Bonzini4d454572012-11-26 16:03:42 +0100363 .help = "The first phandle ID we may generate dynamically",
364 }, {
365 .name = "dt_compatible",
366 .type = QEMU_OPT_STRING,
367 .help = "Overrides the \"compatible\" property of the dt root node",
368 }, {
369 .name = "dump-guest-core",
370 .type = QEMU_OPT_BOOL,
371 .help = "Include guest memory in a core dump",
372 }, {
373 .name = "mem-merge",
374 .type = QEMU_OPT_BOOL,
375 .help = "enable/disable memory merge support",
376 },{
377 .name = "usb",
378 .type = QEMU_OPT_BOOL,
379 .help = "Set on/off to enable/disable usb",
Gerd Hoffmann1442d3e2013-10-15 16:57:45 +0200380 },{
381 .name = "firmware",
382 .type = QEMU_OPT_STRING,
383 .help = "firmware image",
Aneesh Kumar K.V135a1292013-12-23 21:10:40 +0530384 },{
385 .name = "kvm-type",
386 .type = QEMU_OPT_STRING,
387 .help = "Specifies the KVM virtualization mode (HV, PR)",
Don Slutzc87b1522014-06-19 21:40:25 -0400388 },{
389 .name = PC_MACHINE_MAX_RAM_BELOW_4G,
390 .type = QEMU_OPT_SIZE,
391 .help = "maximum ram below the 4G boundary (32bit boundary)",
Le Tana52a7fd2014-08-16 13:55:40 +0800392 },{
393 .name = "iommu",
394 .type = QEMU_OPT_BOOL,
395 .help = "Set on/off to enable/disable Intel IOMMU (VT-d)",
Paolo Bonzini4d454572012-11-26 16:03:42 +0100396 },
397 { /* End of list */ }
398 },
399};
400
401static QemuOptsList qemu_boot_opts = {
402 .name = "boot-opts",
Markus Armbruster6ef47162013-06-14 13:15:01 +0200403 .implied_opt_name = "order",
404 .merge_lists = true,
Paolo Bonzini4d454572012-11-26 16:03:42 +0100405 .head = QTAILQ_HEAD_INITIALIZER(qemu_boot_opts.head),
406 .desc = {
Paolo Bonzini4d454572012-11-26 16:03:42 +0100407 {
408 .name = "order",
409 .type = QEMU_OPT_STRING,
410 }, {
411 .name = "once",
412 .type = QEMU_OPT_STRING,
413 }, {
414 .name = "menu",
Markus Armbruster6ef47162013-06-14 13:15:01 +0200415 .type = QEMU_OPT_BOOL,
Paolo Bonzini4d454572012-11-26 16:03:42 +0100416 }, {
417 .name = "splash",
418 .type = QEMU_OPT_STRING,
419 }, {
420 .name = "splash-time",
421 .type = QEMU_OPT_STRING,
422 }, {
423 .name = "reboot-timeout",
424 .type = QEMU_OPT_STRING,
Amos Kongc8a6ae82013-03-19 14:23:27 +0800425 }, {
426 .name = "strict",
Amos Konge5187b52013-12-09 19:53:15 +0800427 .type = QEMU_OPT_BOOL,
Paolo Bonzini4d454572012-11-26 16:03:42 +0100428 },
429 { /*End of list */ }
430 },
431};
432
433static QemuOptsList qemu_add_fd_opts = {
434 .name = "add-fd",
435 .head = QTAILQ_HEAD_INITIALIZER(qemu_add_fd_opts.head),
436 .desc = {
437 {
438 .name = "fd",
439 .type = QEMU_OPT_NUMBER,
440 .help = "file descriptor of which a duplicate is added to fd set",
441 },{
442 .name = "set",
443 .type = QEMU_OPT_NUMBER,
444 .help = "ID of the fd set to add fd to",
445 },{
446 .name = "opaque",
447 .type = QEMU_OPT_STRING,
448 .help = "free-form string used to describe fd",
449 },
450 { /* end of list */ }
451 },
452};
453
454static QemuOptsList qemu_object_opts = {
455 .name = "object",
456 .implied_opt_name = "qom-type",
457 .head = QTAILQ_HEAD_INITIALIZER(qemu_object_opts.head),
458 .desc = {
459 { }
460 },
461};
462
Stefan Bergerd1a0cf72013-02-27 12:47:49 -0500463static QemuOptsList qemu_tpmdev_opts = {
464 .name = "tpmdev",
465 .implied_opt_name = "type",
466 .head = QTAILQ_HEAD_INITIALIZER(qemu_tpmdev_opts.head),
467 .desc = {
Stefan Bergerbb716232013-04-22 10:41:39 -0400468 /* options are defined in the TPM backends */
Stefan Bergerd1a0cf72013-02-27 12:47:49 -0500469 { /* end of list */ }
470 },
471};
472
Satoru Moriya888a6bc2013-04-19 16:42:06 +0200473static QemuOptsList qemu_realtime_opts = {
474 .name = "realtime",
475 .head = QTAILQ_HEAD_INITIALIZER(qemu_realtime_opts.head),
476 .desc = {
477 {
478 .name = "mlock",
479 .type = QEMU_OPT_BOOL,
480 },
481 { /* end of list */ }
482 },
483};
484
Seiji Aguchi5e2ac512013-07-03 23:02:46 -0400485static QemuOptsList qemu_msg_opts = {
486 .name = "msg",
487 .head = QTAILQ_HEAD_INITIALIZER(qemu_msg_opts.head),
488 .desc = {
489 {
490 .name = "timestamp",
491 .type = QEMU_OPT_BOOL,
492 },
493 { /* end of list */ }
494 },
495};
496
Dr. David Alan Gilbert5d12f962014-01-30 10:20:30 +0000497static QemuOptsList qemu_name_opts = {
498 .name = "name",
499 .implied_opt_name = "guest",
500 .merge_lists = true,
501 .head = QTAILQ_HEAD_INITIALIZER(qemu_name_opts.head),
502 .desc = {
503 {
504 .name = "guest",
505 .type = QEMU_OPT_STRING,
506 .help = "Sets the name of the guest.\n"
507 "This name will be displayed in the SDL window caption.\n"
508 "The name will also be used for the VNC server",
509 }, {
510 .name = "process",
511 .type = QEMU_OPT_STRING,
512 .help = "Sets the name of the QEMU process, as shown in top etc",
Dr. David Alan Gilbert8f480de2014-01-30 10:20:31 +0000513 }, {
514 .name = "debug-threads",
515 .type = QEMU_OPT_BOOL,
516 .help = "When enabled, name the individual threads; defaults off.\n"
517 "NOTE: The thread names are for debugging and not a\n"
518 "stable API.",
Dr. David Alan Gilbert5d12f962014-01-30 10:20:30 +0000519 },
520 { /* End of list */ }
521 },
522};
523
Igor Mammedov6e1d3c12013-11-27 01:27:35 +0100524static QemuOptsList qemu_mem_opts = {
525 .name = "memory",
526 .implied_opt_name = "size",
527 .head = QTAILQ_HEAD_INITIALIZER(qemu_mem_opts.head),
528 .merge_lists = true,
529 .desc = {
530 {
531 .name = "size",
532 .type = QEMU_OPT_SIZE,
533 },
Igor Mammedovc270fb92014-06-02 15:25:02 +0200534 {
535 .name = "slots",
536 .type = QEMU_OPT_NUMBER,
537 },
538 {
539 .name = "maxmem",
540 .type = QEMU_OPT_SIZE,
541 },
Igor Mammedov6e1d3c12013-11-27 01:27:35 +0100542 { /* end of list */ }
543 },
544};
545
Sebastian Tanase1ad95802014-07-25 11:56:28 +0200546static QemuOptsList qemu_icount_opts = {
547 .name = "icount",
548 .implied_opt_name = "shift",
549 .merge_lists = true,
550 .head = QTAILQ_HEAD_INITIALIZER(qemu_icount_opts.head),
551 .desc = {
552 {
553 .name = "shift",
554 .type = QEMU_OPT_STRING,
Sebastian Tanasea8bfac32014-07-25 11:56:29 +0200555 }, {
556 .name = "align",
557 .type = QEMU_OPT_BOOL,
Sebastian Tanase1ad95802014-07-25 11:56:28 +0200558 },
559 { /* end of list */ }
560 },
561};
562
Markus Armbruster7f9d6e52013-07-04 15:09:19 +0200563/**
564 * Get machine options
565 *
566 * Returns: machine options (never null).
567 */
568QemuOpts *qemu_get_machine_opts(void)
569{
Paolo Bonzinie96e5ae2014-03-06 10:39:24 +0100570 return qemu_find_opts_singleton("machine");
Markus Armbruster7f9d6e52013-07-04 15:09:19 +0200571}
572
Ronnie Sahlberg31459f42012-08-06 18:24:55 +1000573const char *qemu_get_vm_name(void)
574{
575 return qemu_name;
576}
577
wayne3d3b8302011-07-27 18:04:55 +0800578static void res_free(void)
579{
580 if (boot_splash_filedata != NULL) {
Anthony Liguori7267c092011-08-20 22:09:37 -0500581 g_free(boot_splash_filedata);
wayne3d3b8302011-07-27 18:04:55 +0800582 boot_splash_filedata = NULL;
583 }
584}
585
Gerd Hoffmann998bbd72009-12-08 13:11:41 +0100586static int default_driver_check(QemuOpts *opts, void *opaque)
587{
588 const char *driver = qemu_opt_get(opts, "driver");
589 int i;
590
591 if (!driver)
592 return 0;
593 for (i = 0; i < ARRAY_SIZE(default_list); i++) {
594 if (strcmp(default_list[i].driver, driver) != 0)
595 continue;
596 *(default_list[i].flag) = 0;
597 }
598 return 0;
599}
600
bellard0824d6f2003-06-24 13:42:40 +0000601/***********************************************************/
Luiz Capitulinof5bbfba2011-07-29 15:04:45 -0300602/* QEMU state */
603
Luiz Capitulino0461d5a2011-09-30 14:45:27 -0300604static RunState current_run_state = RUN_STATE_PRELAUNCH;
Luiz Capitulinof5bbfba2011-07-29 15:04:45 -0300605
Paolo Bonzini74892d22014-06-05 14:53:58 +0200606/* We use RUN_STATE_MAX but any invalid value will do */
607static RunState vmstop_requested = RUN_STATE_MAX;
608static QemuMutex vmstop_lock;
609
Luiz Capitulino5db9d4d2011-09-05 16:36:31 -0300610typedef struct {
611 RunState from;
612 RunState to;
613} RunStateTransition;
614
615static const RunStateTransition runstate_transitions_def[] = {
616 /* from -> to */
Luiz Capitulino0461d5a2011-09-30 14:45:27 -0300617 { RUN_STATE_DEBUG, RUN_STATE_RUNNING },
Paolo Bonzinieca01d32013-12-03 13:00:15 +0100618 { RUN_STATE_DEBUG, RUN_STATE_FINISH_MIGRATE },
Luiz Capitulino5db9d4d2011-09-05 16:36:31 -0300619
Luiz Capitulino0461d5a2011-09-30 14:45:27 -0300620 { RUN_STATE_INMIGRATE, RUN_STATE_RUNNING },
Paolo Bonzini29ed72f2012-10-19 16:45:24 +0200621 { RUN_STATE_INMIGRATE, RUN_STATE_PAUSED },
Luiz Capitulino5db9d4d2011-09-05 16:36:31 -0300622
Luiz Capitulino0461d5a2011-09-30 14:45:27 -0300623 { RUN_STATE_INTERNAL_ERROR, RUN_STATE_PAUSED },
Luiz Capitulino8a9236f2011-10-14 11:18:09 -0300624 { RUN_STATE_INTERNAL_ERROR, RUN_STATE_FINISH_MIGRATE },
Luiz Capitulino5db9d4d2011-09-05 16:36:31 -0300625
Luiz Capitulino0461d5a2011-09-30 14:45:27 -0300626 { RUN_STATE_IO_ERROR, RUN_STATE_RUNNING },
Luiz Capitulino8a9236f2011-10-14 11:18:09 -0300627 { RUN_STATE_IO_ERROR, RUN_STATE_FINISH_MIGRATE },
Luiz Capitulino5db9d4d2011-09-05 16:36:31 -0300628
Luiz Capitulino0461d5a2011-09-30 14:45:27 -0300629 { RUN_STATE_PAUSED, RUN_STATE_RUNNING },
Luiz Capitulino8a9236f2011-10-14 11:18:09 -0300630 { RUN_STATE_PAUSED, RUN_STATE_FINISH_MIGRATE },
Luiz Capitulino5db9d4d2011-09-05 16:36:31 -0300631
Luiz Capitulino0461d5a2011-09-30 14:45:27 -0300632 { RUN_STATE_POSTMIGRATE, RUN_STATE_RUNNING },
Luiz Capitulino8a9236f2011-10-14 11:18:09 -0300633 { RUN_STATE_POSTMIGRATE, RUN_STATE_FINISH_MIGRATE },
Luiz Capitulino5db9d4d2011-09-05 16:36:31 -0300634
Luiz Capitulino0461d5a2011-09-30 14:45:27 -0300635 { RUN_STATE_PRELAUNCH, RUN_STATE_RUNNING },
Luiz Capitulino8a9236f2011-10-14 11:18:09 -0300636 { RUN_STATE_PRELAUNCH, RUN_STATE_FINISH_MIGRATE },
Luiz Capitulino0461d5a2011-09-30 14:45:27 -0300637 { RUN_STATE_PRELAUNCH, RUN_STATE_INMIGRATE },
Luiz Capitulino5db9d4d2011-09-05 16:36:31 -0300638
Luiz Capitulino0461d5a2011-09-30 14:45:27 -0300639 { RUN_STATE_FINISH_MIGRATE, RUN_STATE_RUNNING },
640 { RUN_STATE_FINISH_MIGRATE, RUN_STATE_POSTMIGRATE },
Luiz Capitulino5db9d4d2011-09-05 16:36:31 -0300641
Luiz Capitulino0461d5a2011-09-30 14:45:27 -0300642 { RUN_STATE_RESTORE_VM, RUN_STATE_RUNNING },
Luiz Capitulino5db9d4d2011-09-05 16:36:31 -0300643
Luiz Capitulino0461d5a2011-09-30 14:45:27 -0300644 { RUN_STATE_RUNNING, RUN_STATE_DEBUG },
645 { RUN_STATE_RUNNING, RUN_STATE_INTERNAL_ERROR },
646 { RUN_STATE_RUNNING, RUN_STATE_IO_ERROR },
647 { RUN_STATE_RUNNING, RUN_STATE_PAUSED },
648 { RUN_STATE_RUNNING, RUN_STATE_FINISH_MIGRATE },
649 { RUN_STATE_RUNNING, RUN_STATE_RESTORE_VM },
650 { RUN_STATE_RUNNING, RUN_STATE_SAVE_VM },
651 { RUN_STATE_RUNNING, RUN_STATE_SHUTDOWN },
652 { RUN_STATE_RUNNING, RUN_STATE_WATCHDOG },
Hu Taoede085b2013-04-26 11:24:40 +0800653 { RUN_STATE_RUNNING, RUN_STATE_GUEST_PANICKED },
Luiz Capitulino5db9d4d2011-09-05 16:36:31 -0300654
Luiz Capitulino0461d5a2011-09-30 14:45:27 -0300655 { RUN_STATE_SAVE_VM, RUN_STATE_RUNNING },
Luiz Capitulino5db9d4d2011-09-05 16:36:31 -0300656
Luiz Capitulino0461d5a2011-09-30 14:45:27 -0300657 { RUN_STATE_SHUTDOWN, RUN_STATE_PAUSED },
Luiz Capitulino8a9236f2011-10-14 11:18:09 -0300658 { RUN_STATE_SHUTDOWN, RUN_STATE_FINISH_MIGRATE },
Luiz Capitulino5db9d4d2011-09-05 16:36:31 -0300659
Luiz Capitulinoad02b962012-04-27 13:33:36 -0300660 { RUN_STATE_DEBUG, RUN_STATE_SUSPENDED },
661 { RUN_STATE_RUNNING, RUN_STATE_SUSPENDED },
662 { RUN_STATE_SUSPENDED, RUN_STATE_RUNNING },
663 { RUN_STATE_SUSPENDED, RUN_STATE_FINISH_MIGRATE },
664
Luiz Capitulino0461d5a2011-09-30 14:45:27 -0300665 { RUN_STATE_WATCHDOG, RUN_STATE_RUNNING },
Luiz Capitulino8a9236f2011-10-14 11:18:09 -0300666 { RUN_STATE_WATCHDOG, RUN_STATE_FINISH_MIGRATE },
Luiz Capitulino5db9d4d2011-09-05 16:36:31 -0300667
Paolo Bonzinidf390762013-11-04 14:30:47 +0100668 { RUN_STATE_GUEST_PANICKED, RUN_STATE_RUNNING },
Hu Taofd2a2e12013-05-20 12:46:20 +0200669 { RUN_STATE_GUEST_PANICKED, RUN_STATE_FINISH_MIGRATE },
Hu Taoede085b2013-04-26 11:24:40 +0800670
Luiz Capitulino0461d5a2011-09-30 14:45:27 -0300671 { RUN_STATE_MAX, RUN_STATE_MAX },
Luiz Capitulino5db9d4d2011-09-05 16:36:31 -0300672};
673
Luiz Capitulino0461d5a2011-09-30 14:45:27 -0300674static bool runstate_valid_transitions[RUN_STATE_MAX][RUN_STATE_MAX];
Luiz Capitulino5db9d4d2011-09-05 16:36:31 -0300675
Luiz Capitulinof5bbfba2011-07-29 15:04:45 -0300676bool runstate_check(RunState state)
677{
678 return current_run_state == state;
679}
680
Blue Swirl4fdcac02012-10-28 11:04:47 +0000681static void runstate_init(void)
Luiz Capitulino5db9d4d2011-09-05 16:36:31 -0300682{
683 const RunStateTransition *p;
684
685 memset(&runstate_valid_transitions, 0, sizeof(runstate_valid_transitions));
Luiz Capitulino0461d5a2011-09-30 14:45:27 -0300686 for (p = &runstate_transitions_def[0]; p->from != RUN_STATE_MAX; p++) {
Luiz Capitulino5db9d4d2011-09-05 16:36:31 -0300687 runstate_valid_transitions[p->from][p->to] = true;
688 }
Paolo Bonzini74892d22014-06-05 14:53:58 +0200689
690 qemu_mutex_init(&vmstop_lock);
Luiz Capitulino5db9d4d2011-09-05 16:36:31 -0300691}
692
693/* This function will abort() on invalid state transitions */
Luiz Capitulinof5bbfba2011-07-29 15:04:45 -0300694void runstate_set(RunState new_state)
695{
Luiz Capitulino207c5cd2011-10-13 10:59:07 -0300696 assert(new_state < RUN_STATE_MAX);
697
698 if (!runstate_valid_transitions[current_run_state][new_state]) {
699 fprintf(stderr, "ERROR: invalid runstate transition: '%s' -> '%s'\n",
700 RunState_lookup[current_run_state],
701 RunState_lookup[new_state]);
Luiz Capitulino5db9d4d2011-09-05 16:36:31 -0300702 abort();
703 }
Kazuya Saito7e866002013-03-22 17:26:59 +0900704 trace_runstate_set(new_state);
Luiz Capitulinof5bbfba2011-07-29 15:04:45 -0300705 current_run_state = new_state;
706}
707
Luiz Capitulino13548692011-07-29 15:36:43 -0300708int runstate_is_running(void)
709{
Luiz Capitulino0461d5a2011-09-30 14:45:27 -0300710 return runstate_check(RUN_STATE_RUNNING);
Luiz Capitulino13548692011-07-29 15:36:43 -0300711}
712
Hu Taoede085b2013-04-26 11:24:40 +0800713bool runstate_needs_reset(void)
714{
715 return runstate_check(RUN_STATE_INTERNAL_ERROR) ||
Paolo Bonzinidf390762013-11-04 14:30:47 +0100716 runstate_check(RUN_STATE_SHUTDOWN);
Hu Taoede085b2013-04-26 11:24:40 +0800717}
718
Luiz Capitulino1fa9a5e2011-09-12 17:54:20 -0300719StatusInfo *qmp_query_status(Error **errp)
720{
721 StatusInfo *info = g_malloc0(sizeof(*info));
722
723 info->running = runstate_is_running();
724 info->singlestep = singlestep;
725 info->status = current_run_state;
726
727 return info;
728}
729
Paolo Bonzini74892d22014-06-05 14:53:58 +0200730static bool qemu_vmstop_requested(RunState *r)
731{
732 qemu_mutex_lock(&vmstop_lock);
733 *r = vmstop_requested;
734 vmstop_requested = RUN_STATE_MAX;
735 qemu_mutex_unlock(&vmstop_lock);
736 return *r < RUN_STATE_MAX;
737}
738
739void qemu_system_vmstop_request_prepare(void)
740{
741 qemu_mutex_lock(&vmstop_lock);
742}
743
744void qemu_system_vmstop_request(RunState state)
745{
746 vmstop_requested = state;
747 qemu_mutex_unlock(&vmstop_lock);
748 qemu_notify_event();
749}
750
751void vm_start(void)
752{
753 RunState requested;
754
755 qemu_vmstop_requested(&requested);
756 if (runstate_is_running() && requested == RUN_STATE_MAX) {
757 return;
758 }
759
760 /* Ensure that a STOP/RESUME pair of events is emitted if a
761 * vmstop request was pending. The BLOCK_IO_ERROR event, for
762 * example, according to documentation is always followed by
763 * the STOP event.
764 */
765 if (runstate_is_running()) {
Wenchao Xia591c48f2014-06-18 08:43:37 +0200766 qapi_event_send_stop(&error_abort);
Paolo Bonzini74892d22014-06-05 14:53:58 +0200767 } else {
768 cpu_enable_ticks();
769 runstate_set(RUN_STATE_RUNNING);
770 vm_state_notify(1, RUN_STATE_RUNNING);
771 resume_all_vcpus();
772 }
773
Wenchao Xia591c48f2014-06-18 08:43:37 +0200774 qapi_event_send_resume(&error_abort);
Paolo Bonzini74892d22014-06-05 14:53:58 +0200775}
776
777
Luiz Capitulinof5bbfba2011-07-29 15:04:45 -0300778/***********************************************************/
Paolo Bonzini8f0056b2010-01-13 14:05:34 +0100779/* real time host monotonic timer */
bellard09b26c52006-04-12 21:09:08 +0000780
bellardc4b1fcc2004-03-14 21:44:30 +0000781/***********************************************************/
balrogf6503052008-02-17 11:42:19 +0000782/* host time/date access */
783void qemu_get_timedate(struct tm *tm, int offset)
784{
785 time_t ti;
balrogf6503052008-02-17 11:42:19 +0000786
787 time(&ti);
788 ti += offset;
789 if (rtc_date_offset == -1) {
790 if (rtc_utc)
Stefan Weileb7ff6f2013-01-07 23:08:13 +0100791 gmtime_r(&ti, tm);
balrogf6503052008-02-17 11:42:19 +0000792 else
Stefan Weileb7ff6f2013-01-07 23:08:13 +0100793 localtime_r(&ti, tm);
balrogf6503052008-02-17 11:42:19 +0000794 } else {
795 ti -= rtc_date_offset;
Stefan Weileb7ff6f2013-01-07 23:08:13 +0100796 gmtime_r(&ti, tm);
balrogf6503052008-02-17 11:42:19 +0000797 }
balrogf6503052008-02-17 11:42:19 +0000798}
799
800int qemu_timedate_diff(struct tm *tm)
801{
802 time_t seconds;
803
804 if (rtc_date_offset == -1)
805 if (rtc_utc)
806 seconds = mktimegm(tm);
Gleb Natapovf54c5562011-11-06 18:00:22 +0200807 else {
808 struct tm tmp = *tm;
809 tmp.tm_isdst = -1; /* use timezone to figure it out */
810 seconds = mktime(&tmp);
811 }
balrogf6503052008-02-17 11:42:19 +0000812 else
813 seconds = mktimegm(tm) + rtc_date_offset;
814
815 return seconds - time(NULL);
816}
817
Jan Kiszka1ed2fc12009-09-15 13:36:04 +0200818static void configure_rtc_date_offset(const char *startdate, int legacy)
819{
820 time_t rtc_start_date;
821 struct tm tm;
822
823 if (!strcmp(startdate, "now") && legacy) {
824 rtc_date_offset = -1;
825 } else {
826 if (sscanf(startdate, "%d-%d-%dT%d:%d:%d",
827 &tm.tm_year,
828 &tm.tm_mon,
829 &tm.tm_mday,
830 &tm.tm_hour,
831 &tm.tm_min,
832 &tm.tm_sec) == 6) {
833 /* OK */
834 } else if (sscanf(startdate, "%d-%d-%d",
835 &tm.tm_year,
836 &tm.tm_mon,
837 &tm.tm_mday) == 3) {
838 tm.tm_hour = 0;
839 tm.tm_min = 0;
840 tm.tm_sec = 0;
841 } else {
842 goto date_fail;
843 }
844 tm.tm_year -= 1900;
845 tm.tm_mon--;
846 rtc_start_date = mktimegm(&tm);
847 if (rtc_start_date == -1) {
848 date_fail:
849 fprintf(stderr, "Invalid date format. Valid formats are:\n"
850 "'2006-06-17T16:01:21' or '2006-06-17'\n");
851 exit(1);
852 }
853 rtc_date_offset = time(NULL) - rtc_start_date;
854 }
855}
856
857static void configure_rtc(QemuOpts *opts)
858{
859 const char *value;
860
861 value = qemu_opt_get(opts, "base");
862 if (value) {
863 if (!strcmp(value, "utc")) {
864 rtc_utc = 1;
865 } else if (!strcmp(value, "localtime")) {
866 rtc_utc = 0;
867 } else {
868 configure_rtc_date_offset(value, 0);
869 }
870 }
Jan Kiszka68752042009-09-15 13:36:04 +0200871 value = qemu_opt_get(opts, "clock");
872 if (value) {
873 if (!strcmp(value, "host")) {
Alex Bligh884f17c2013-08-21 16:03:04 +0100874 rtc_clock = QEMU_CLOCK_HOST;
Paolo Bonzini78808142012-03-30 10:31:21 +0000875 } else if (!strcmp(value, "rt")) {
Alex Bligh884f17c2013-08-21 16:03:04 +0100876 rtc_clock = QEMU_CLOCK_REALTIME;
Jan Kiszka68752042009-09-15 13:36:04 +0200877 } else if (!strcmp(value, "vm")) {
Alex Bligh884f17c2013-08-21 16:03:04 +0100878 rtc_clock = QEMU_CLOCK_VIRTUAL;
Jan Kiszka68752042009-09-15 13:36:04 +0200879 } else {
880 fprintf(stderr, "qemu: invalid option value '%s'\n", value);
881 exit(1);
882 }
883 }
Jan Kiszka1ed2fc12009-09-15 13:36:04 +0200884 value = qemu_opt_get(opts, "driftfix");
885 if (value) {
Blue Swirl7e4c0332010-03-27 21:33:46 +0000886 if (!strcmp(value, "slew")) {
Jan Kiszka433acf02012-01-23 20:15:12 +0100887 static GlobalProperty slew_lost_ticks[] = {
888 {
889 .driver = "mc146818rtc",
890 .property = "lost_tick_policy",
891 .value = "slew",
892 },
893 { /* end of list */ }
894 };
895
896 qdev_prop_register_global_list(slew_lost_ticks);
Blue Swirl7e4c0332010-03-27 21:33:46 +0000897 } else if (!strcmp(value, "none")) {
Jan Kiszka433acf02012-01-23 20:15:12 +0100898 /* discard is default */
Jan Kiszka1ed2fc12009-09-15 13:36:04 +0200899 } else {
900 fprintf(stderr, "qemu: invalid option value '%s'\n", value);
901 exit(1);
902 }
903 }
Jan Kiszka1ed2fc12009-09-15 13:36:04 +0200904}
905
balrog1ae26a12008-09-28 23:19:47 +0000906/***********************************************************/
907/* Bluetooth support */
908static int nb_hcis;
909static int cur_hci;
910static struct HCIInfo *hci_table[MAX_NICS];
balrogdc72ac12008-11-09 00:04:26 +0000911
balrog1ae26a12008-09-28 23:19:47 +0000912struct HCIInfo *qemu_next_hci(void)
913{
914 if (cur_hci == nb_hcis)
915 return &null_hci;
916
917 return hci_table[cur_hci++];
918}
919
balrogdc72ac12008-11-09 00:04:26 +0000920static int bt_hci_parse(const char *str)
921{
922 struct HCIInfo *hci;
Anthony Liguoric227f092009-10-01 16:12:16 -0500923 bdaddr_t bdaddr;
balrogdc72ac12008-11-09 00:04:26 +0000924
925 if (nb_hcis >= MAX_NICS) {
926 fprintf(stderr, "qemu: Too many bluetooth HCIs (max %i).\n", MAX_NICS);
927 return -1;
928 }
929
930 hci = hci_init(str);
931 if (!hci)
932 return -1;
933
934 bdaddr.b[0] = 0x52;
935 bdaddr.b[1] = 0x54;
936 bdaddr.b[2] = 0x00;
937 bdaddr.b[3] = 0x12;
938 bdaddr.b[4] = 0x34;
939 bdaddr.b[5] = 0x56 + nb_hcis;
940 hci->bdaddr_set(hci, bdaddr.b);
941
942 hci_table[nb_hcis++] = hci;
943
944 return 0;
945}
946
947static void bt_vhci_add(int vlan_id)
948{
949 struct bt_scatternet_s *vlan = qemu_find_bt_vlan(vlan_id);
950
951 if (!vlan->slave)
952 fprintf(stderr, "qemu: warning: adding a VHCI to "
953 "an empty scatternet %i\n", vlan_id);
954
955 bt_vhci_init(bt_new_hci(vlan));
956}
957
958static struct bt_device_s *bt_device_add(const char *opt)
959{
960 struct bt_scatternet_s *vlan;
961 int vlan_id = 0;
962 char *endp = strstr(opt, ",vlan=");
963 int len = (endp ? endp - opt : strlen(opt)) + 1;
964 char devname[10];
965
966 pstrcpy(devname, MIN(sizeof(devname), len), opt);
967
968 if (endp) {
969 vlan_id = strtol(endp + 6, &endp, 0);
970 if (*endp) {
971 fprintf(stderr, "qemu: unrecognised bluetooth vlan Id\n");
972 return 0;
973 }
974 }
975
976 vlan = qemu_find_bt_vlan(vlan_id);
977
978 if (!vlan->slave)
979 fprintf(stderr, "qemu: warning: adding a slave device to "
980 "an empty scatternet %i\n", vlan_id);
981
982 if (!strcmp(devname, "keyboard"))
983 return bt_keyboard_init(vlan);
984
985 fprintf(stderr, "qemu: unsupported bluetooth device `%s'\n", devname);
986 return 0;
987}
988
989static int bt_parse(const char *opt)
990{
991 const char *endp, *p;
992 int vlan;
993
994 if (strstart(opt, "hci", &endp)) {
995 if (!*endp || *endp == ',') {
996 if (*endp)
997 if (!strstart(endp, ",vlan=", 0))
998 opt = endp + 1;
999
1000 return bt_hci_parse(opt);
1001 }
1002 } else if (strstart(opt, "vhci", &endp)) {
1003 if (!*endp || *endp == ',') {
1004 if (*endp) {
1005 if (strstart(endp, ",vlan=", &p)) {
1006 vlan = strtol(p, (char **) &endp, 0);
1007 if (*endp) {
1008 fprintf(stderr, "qemu: bad scatternet '%s'\n", p);
1009 return 1;
1010 }
1011 } else {
1012 fprintf(stderr, "qemu: bad parameter '%s'\n", endp + 1);
1013 return 1;
1014 }
1015 } else
1016 vlan = 0;
1017
1018 bt_vhci_add(vlan);
1019 return 0;
1020 }
1021 } else if (strstart(opt, "device:", &endp))
1022 return !bt_device_add(endp);
1023
1024 fprintf(stderr, "qemu: bad bluetooth parameter '%s'\n", opt);
1025 return 1;
1026}
1027
Eduardo Otubo7d76ad42012-08-14 18:44:08 -03001028static int parse_sandbox(QemuOpts *opts, void *opaque)
1029{
1030 /* FIXME: change this to true for 1.3 */
1031 if (qemu_opt_get_bool(opts, "enable", false)) {
1032#ifdef CONFIG_SECCOMP
1033 if (seccomp_start() < 0) {
1034 qerror_report(ERROR_CLASS_GENERIC_ERROR,
1035 "failed to install seccomp syscall filter in the kernel");
1036 return -1;
1037 }
1038#else
1039 qerror_report(ERROR_CLASS_GENERIC_ERROR,
1040 "sandboxing request but seccomp is not compiled into this build");
1041 return -1;
1042#endif
1043 }
1044
1045 return 0;
1046}
1047
Dr. David Alan Gilbert5b9d3132014-05-06 12:15:55 +01001048static int parse_name(QemuOpts *opts, void *opaque)
Dr. David Alan Gilbert5d12f962014-01-30 10:20:30 +00001049{
1050 const char *proc_name;
1051
Dr. David Alan Gilbert8f480de2014-01-30 10:20:31 +00001052 if (qemu_opt_get(opts, "debug-threads")) {
1053 qemu_thread_naming(qemu_opt_get_bool(opts, "debug-threads", false));
1054 }
Dr. David Alan Gilbert5d12f962014-01-30 10:20:30 +00001055 qemu_name = qemu_opt_get(opts, "guest");
1056
1057 proc_name = qemu_opt_get(opts, "process");
1058 if (proc_name) {
1059 os_set_proc_name(proc_name);
1060 }
Dr. David Alan Gilbert5b9d3132014-05-06 12:15:55 +01001061
1062 return 0;
Dr. David Alan Gilbert5d12f962014-01-30 10:20:30 +00001063}
1064
zhlcindy@gmail.com094b2872012-09-02 19:25:28 +00001065bool usb_enabled(bool default_usb)
1066{
Mark Wu7effdaa2014-03-10 22:37:41 +08001067 return qemu_opt_get_bool(qemu_get_machine_opts(), "usb",
1068 has_defaults && default_usb);
zhlcindy@gmail.com094b2872012-09-02 19:25:28 +00001069}
1070
Corey Bryant587ed6b2012-10-18 15:19:34 -04001071#ifndef _WIN32
1072static int parse_add_fd(QemuOpts *opts, void *opaque)
1073{
1074 int fd, dupfd, flags;
1075 int64_t fdset_id;
1076 const char *fd_opaque = NULL;
1077
1078 fd = qemu_opt_get_number(opts, "fd", -1);
1079 fdset_id = qemu_opt_get_number(opts, "set", -1);
1080 fd_opaque = qemu_opt_get(opts, "opaque");
1081
1082 if (fd < 0) {
1083 qerror_report(ERROR_CLASS_GENERIC_ERROR,
1084 "fd option is required and must be non-negative");
1085 return -1;
1086 }
1087
1088 if (fd <= STDERR_FILENO) {
1089 qerror_report(ERROR_CLASS_GENERIC_ERROR,
1090 "fd cannot be a standard I/O stream");
1091 return -1;
1092 }
1093
1094 /*
1095 * All fds inherited across exec() necessarily have FD_CLOEXEC
1096 * clear, while qemu sets FD_CLOEXEC on all other fds used internally.
1097 */
1098 flags = fcntl(fd, F_GETFD);
1099 if (flags == -1 || (flags & FD_CLOEXEC)) {
1100 qerror_report(ERROR_CLASS_GENERIC_ERROR,
1101 "fd is not valid or already in use");
1102 return -1;
1103 }
1104
1105 if (fdset_id < 0) {
1106 qerror_report(ERROR_CLASS_GENERIC_ERROR,
1107 "set option is required and must be non-negative");
1108 return -1;
1109 }
1110
1111#ifdef F_DUPFD_CLOEXEC
1112 dupfd = fcntl(fd, F_DUPFD_CLOEXEC, 0);
1113#else
1114 dupfd = dup(fd);
1115 if (dupfd != -1) {
1116 qemu_set_cloexec(dupfd);
1117 }
1118#endif
1119 if (dupfd == -1) {
1120 qerror_report(ERROR_CLASS_GENERIC_ERROR,
1121 "Error duplicating fd: %s", strerror(errno));
1122 return -1;
1123 }
1124
1125 /* add the duplicate fd, and optionally the opaque string, to the fd set */
1126 monitor_fdset_add_fd(dupfd, true, fdset_id, fd_opaque ? true : false,
1127 fd_opaque, NULL);
1128
1129 return 0;
1130}
1131
1132static int cleanup_add_fd(QemuOpts *opts, void *opaque)
1133{
1134 int fd;
1135
1136 fd = qemu_opt_get_number(opts, "fd", -1);
1137 close(fd);
1138
1139 return 0;
1140}
1141#endif
1142
balrog1ae26a12008-09-28 23:19:47 +00001143/***********************************************************/
1144/* QEMU Block devices */
1145
Markus Armbruster2292dda2011-01-28 11:21:41 +01001146#define HD_OPTS "media=disk"
1147#define CDROM_OPTS "media=cdrom"
1148#define FD_OPTS ""
1149#define PFLASH_OPTS ""
1150#define MTD_OPTS ""
1151#define SD_OPTS ""
thse4bcb142007-12-02 04:51:10 +00001152
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +02001153static int drive_init_func(QemuOpts *opts, void *opaque)
1154{
Christian Borntraeger2d0d2832012-11-20 15:30:34 +01001155 BlockInterfaceType *block_default_type = opaque;
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +02001156
Markus Armbruster60e19e02014-06-06 14:50:58 +02001157 return drive_new(opts, *block_default_type) == NULL;
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +02001158}
1159
1160static int drive_enable_snapshot(QemuOpts *opts, void *opaque)
1161{
Gonglei28de2f82014-08-11 21:00:57 +08001162 if (qemu_opt_get(opts, "snapshot") == NULL) {
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +02001163 qemu_opt_set(opts, "snapshot", "on");
1164 }
1165 return 0;
1166}
1167
Christian Borntraeger3c42ea62012-11-22 21:02:55 +01001168static void default_drive(int enable, int snapshot, BlockInterfaceType type,
1169 int index, const char *optstr)
Markus Armbruster4e5d9b52011-01-28 11:21:45 +01001170{
1171 QemuOpts *opts;
John Snowa66c9dc2014-10-01 14:19:24 -04001172 DriveInfo *dinfo;
Markus Armbruster4e5d9b52011-01-28 11:21:45 +01001173
Markus Armbruster4e5d9b52011-01-28 11:21:45 +01001174 if (!enable || drive_get_by_index(type, index)) {
1175 return;
1176 }
1177
1178 opts = drive_add(type, index, NULL, optstr);
1179 if (snapshot) {
1180 drive_enable_snapshot(opts, NULL);
1181 }
John Snowa66c9dc2014-10-01 14:19:24 -04001182
1183 dinfo = drive_new(opts, type);
1184 if (!dinfo) {
Markus Armbruster4e5d9b52011-01-28 11:21:45 +01001185 exit(1);
1186 }
John Snowa66c9dc2014-10-01 14:19:24 -04001187 dinfo->is_default = true;
1188
Markus Armbruster4e5d9b52011-01-28 11:21:45 +01001189}
1190
Jan Kiszka76e30d02009-07-02 00:19:02 +02001191void qemu_register_boot_set(QEMUBootSetHandler *func, void *opaque)
1192{
1193 boot_set_handler = func;
1194 boot_set_opaque = opaque;
1195}
1196
Markus Armbruster083b79c2013-06-14 13:15:04 +02001197int qemu_boot_set(const char *boot_order)
Jan Kiszka76e30d02009-07-02 00:19:02 +02001198{
1199 if (!boot_set_handler) {
1200 return -EINVAL;
1201 }
Markus Armbruster083b79c2013-06-14 13:15:04 +02001202 return boot_set_handler(boot_set_opaque, boot_order);
Jan Kiszka76e30d02009-07-02 00:19:02 +02001203}
1204
Markus Armbruster6ef47162013-06-14 13:15:01 +02001205static void validate_bootdevices(const char *devices)
Jan Kiszkaef3adf62009-07-02 00:19:02 +02001206{
1207 /* We just do some generic consistency checks */
1208 const char *p;
1209 int bitmap = 0;
1210
1211 for (p = devices; *p != '\0'; p++) {
1212 /* Allowed boot devices are:
1213 * a-b: floppy disk drives
1214 * c-f: IDE disk drives
Dong Xu Wang07f35072011-11-22 18:06:26 +08001215 * g-m: machine implementation dependent drives
Jan Kiszkaef3adf62009-07-02 00:19:02 +02001216 * n-p: network devices
1217 * It's up to each machine implementation to check if the given boot
1218 * devices match the actual hardware implementation and firmware
1219 * features.
1220 */
1221 if (*p < 'a' || *p > 'p') {
1222 fprintf(stderr, "Invalid boot device '%c'\n", *p);
1223 exit(1);
1224 }
1225 if (bitmap & (1 << (*p - 'a'))) {
1226 fprintf(stderr, "Boot device '%c' was given twice\n", *p);
1227 exit(1);
1228 }
1229 bitmap |= 1 << (*p - 'a');
1230 }
Jan Kiszkaef3adf62009-07-02 00:19:02 +02001231}
1232
Markus Armbruster083b79c2013-06-14 13:15:04 +02001233static void restore_boot_order(void *opaque)
Jan Kiszkae0f084b2009-07-02 00:19:02 +02001234{
Markus Armbruster083b79c2013-06-14 13:15:04 +02001235 char *normal_boot_order = opaque;
Alex Williamson37905d62010-04-30 15:21:11 -06001236 static int first = 1;
1237
1238 /* Restore boot order and remove ourselves after the first boot */
1239 if (first) {
1240 first = 0;
1241 return;
1242 }
Jan Kiszkae0f084b2009-07-02 00:19:02 +02001243
Markus Armbruster083b79c2013-06-14 13:15:04 +02001244 qemu_boot_set(normal_boot_order);
Jan Kiszkae0f084b2009-07-02 00:19:02 +02001245
Markus Armbruster083b79c2013-06-14 13:15:04 +02001246 qemu_unregister_reset(restore_boot_order, normal_boot_order);
1247 g_free(normal_boot_order);
Jan Kiszkae0f084b2009-07-02 00:19:02 +02001248}
1249
Gleb Natapov1ca4d092010-12-08 13:35:05 +02001250void add_boot_device_path(int32_t bootindex, DeviceState *dev,
1251 const char *suffix)
1252{
1253 FWBootEntry *node, *i;
1254
1255 if (bootindex < 0) {
1256 return;
1257 }
1258
1259 assert(dev != NULL || suffix != NULL);
1260
Anthony Liguori7267c092011-08-20 22:09:37 -05001261 node = g_malloc0(sizeof(FWBootEntry));
Gleb Natapov1ca4d092010-12-08 13:35:05 +02001262 node->bootindex = bootindex;
Dong Xu Wang7f303ad2013-05-09 15:53:49 +08001263 node->suffix = g_strdup(suffix);
Gleb Natapov1ca4d092010-12-08 13:35:05 +02001264 node->dev = dev;
1265
1266 QTAILQ_FOREACH(i, &fw_boot_order, link) {
1267 if (i->bootindex == bootindex) {
1268 fprintf(stderr, "Two devices with same boot index %d\n", bootindex);
1269 exit(1);
1270 } else if (i->bootindex < bootindex) {
1271 continue;
1272 }
1273 QTAILQ_INSERT_BEFORE(i, node, link);
1274 return;
1275 }
1276 QTAILQ_INSERT_TAIL(&fw_boot_order, node, link);
1277}
1278
Dominik Dingel7dc5af52013-04-26 02:12:49 +00001279DeviceState *get_boot_device(uint32_t position)
1280{
1281 uint32_t counter = 0;
1282 FWBootEntry *i = NULL;
1283 DeviceState *res = NULL;
1284
1285 if (!QTAILQ_EMPTY(&fw_boot_order)) {
1286 QTAILQ_FOREACH(i, &fw_boot_order, link) {
1287 if (counter == position) {
1288 res = i->dev;
1289 break;
1290 }
1291 counter++;
1292 }
1293 }
1294 return res;
1295}
1296
Gleb Natapov962630f2010-12-08 13:35:09 +02001297/*
1298 * This function returns null terminated string that consist of new line
Brad Hards71785ab2011-04-23 21:50:06 +10001299 * separated device paths.
Gleb Natapov962630f2010-12-08 13:35:09 +02001300 *
1301 * memory pointed by "size" is assigned total length of the array in bytes
1302 *
1303 */
Alexey Kardashevskiy30e32af2014-03-17 13:40:22 +11001304char *get_boot_devices_list(size_t *size, bool ignore_suffixes)
Gleb Natapov962630f2010-12-08 13:35:09 +02001305{
1306 FWBootEntry *i;
Markus Armbruster0e7a7592013-01-16 14:50:29 +01001307 size_t total = 0;
Gleb Natapov962630f2010-12-08 13:35:09 +02001308 char *list = NULL;
1309
1310 QTAILQ_FOREACH(i, &fw_boot_order, link) {
1311 char *devpath = NULL, *bootpath;
Markus Armbruster0e7a7592013-01-16 14:50:29 +01001312 size_t len;
Gleb Natapov962630f2010-12-08 13:35:09 +02001313
1314 if (i->dev) {
1315 devpath = qdev_get_fw_dev_path(i->dev);
1316 assert(devpath);
1317 }
1318
Alexey Kardashevskiy30e32af2014-03-17 13:40:22 +11001319 if (i->suffix && !ignore_suffixes && devpath) {
Blue Swirl4fd37a92010-12-19 14:05:43 +00001320 size_t bootpathlen = strlen(devpath) + strlen(i->suffix) + 1;
1321
Anthony Liguori7267c092011-08-20 22:09:37 -05001322 bootpath = g_malloc(bootpathlen);
Blue Swirl4fd37a92010-12-19 14:05:43 +00001323 snprintf(bootpath, bootpathlen, "%s%s", devpath, i->suffix);
Anthony Liguori7267c092011-08-20 22:09:37 -05001324 g_free(devpath);
Gleb Natapov962630f2010-12-08 13:35:09 +02001325 } else if (devpath) {
1326 bootpath = devpath;
Alexey Kardashevskiy30e32af2014-03-17 13:40:22 +11001327 } else if (!ignore_suffixes) {
Markus Armbruster1bf6ccd2011-11-08 10:58:00 +01001328 assert(i->suffix);
Anthony Liguori7267c092011-08-20 22:09:37 -05001329 bootpath = g_strdup(i->suffix);
Alexey Kardashevskiy30e32af2014-03-17 13:40:22 +11001330 } else {
1331 bootpath = g_strdup("");
Gleb Natapov962630f2010-12-08 13:35:09 +02001332 }
1333
1334 if (total) {
1335 list[total-1] = '\n';
1336 }
1337 len = strlen(bootpath) + 1;
Anthony Liguori7267c092011-08-20 22:09:37 -05001338 list = g_realloc(list, total + len);
Gleb Natapov962630f2010-12-08 13:35:09 +02001339 memcpy(&list[total], bootpath, len);
1340 total += len;
Anthony Liguori7267c092011-08-20 22:09:37 -05001341 g_free(bootpath);
Gleb Natapov962630f2010-12-08 13:35:09 +02001342 }
1343
1344 *size = total;
1345
Amos Kongc8a6ae82013-03-19 14:23:27 +08001346 if (boot_strict && *size > 0) {
1347 list[total-1] = '\n';
Amos Kong46905792013-03-20 18:16:34 +08001348 list = g_realloc(list, total + 5);
1349 memcpy(&list[total], "HALT", 5);
1350 *size = total + 5;
Amos Kongc8a6ae82013-03-19 14:23:27 +08001351 }
Gleb Natapov962630f2010-12-08 13:35:09 +02001352 return list;
1353}
1354
Michael Tokarev12b7f572013-06-24 15:06:52 +04001355static QemuOptsList qemu_smp_opts = {
1356 .name = "smp-opts",
1357 .implied_opt_name = "cpus",
1358 .merge_lists = true,
1359 .head = QTAILQ_HEAD_INITIALIZER(qemu_smp_opts.head),
1360 .desc = {
1361 {
1362 .name = "cpus",
1363 .type = QEMU_OPT_NUMBER,
1364 }, {
1365 .name = "sockets",
1366 .type = QEMU_OPT_NUMBER,
1367 }, {
1368 .name = "cores",
1369 .type = QEMU_OPT_NUMBER,
1370 }, {
1371 .name = "threads",
1372 .type = QEMU_OPT_NUMBER,
1373 }, {
1374 .name = "maxcpus",
1375 .type = QEMU_OPT_NUMBER,
1376 },
1377 { /*End of list */ }
1378 },
1379};
1380
1381static void smp_parse(QemuOpts *opts)
Andre Przywaradc6b1c02009-08-19 15:42:40 +02001382{
Michael Tokarev12b7f572013-06-24 15:06:52 +04001383 if (opts) {
Andre Przywaradc6b1c02009-08-19 15:42:40 +02001384
Michael Tokarev12b7f572013-06-24 15:06:52 +04001385 unsigned cpus = qemu_opt_get_number(opts, "cpus", 0);
1386 unsigned sockets = qemu_opt_get_number(opts, "sockets", 0);
1387 unsigned cores = qemu_opt_get_number(opts, "cores", 0);
1388 unsigned threads = qemu_opt_get_number(opts, "threads", 0);
Andre Przywaradc6b1c02009-08-19 15:42:40 +02001389
Michael Tokarev12b7f572013-06-24 15:06:52 +04001390 /* compute missing values, prefer sockets over cores over threads */
1391 if (cpus == 0 || sockets == 0) {
1392 sockets = sockets > 0 ? sockets : 1;
1393 cores = cores > 0 ? cores : 1;
Andre Przywaradc6b1c02009-08-19 15:42:40 +02001394 threads = threads > 0 ? threads : 1;
Michael Tokarev12b7f572013-06-24 15:06:52 +04001395 if (cpus == 0) {
1396 cpus = cores * threads * sockets;
1397 }
Andre Przywaradc6b1c02009-08-19 15:42:40 +02001398 } else {
Michael Tokarev12b7f572013-06-24 15:06:52 +04001399 if (cores == 0) {
1400 threads = threads > 0 ? threads : 1;
1401 cores = cpus / (sockets * threads);
1402 } else {
1403 threads = cpus / (cores * sockets);
1404 }
Andre Przywaradc6b1c02009-08-19 15:42:40 +02001405 }
Michael Tokarev12b7f572013-06-24 15:06:52 +04001406
1407 max_cpus = qemu_opt_get_number(opts, "maxcpus", 0);
1408
1409 smp_cpus = cpus;
1410 smp_cores = cores > 0 ? cores : 1;
1411 smp_threads = threads > 0 ? threads : 1;
1412
Andre Przywaradc6b1c02009-08-19 15:42:40 +02001413 }
Michael Tokarev12b7f572013-06-24 15:06:52 +04001414
1415 if (max_cpus == 0) {
Andre Przywaradc6b1c02009-08-19 15:42:40 +02001416 max_cpus = smp_cpus;
Michael Tokarev12b7f572013-06-24 15:06:52 +04001417 }
1418
Eduardo Habkostaf67ee92014-03-18 16:29:24 -03001419 if (max_cpus > MAX_CPUMASK_BITS) {
Michael Tokarev12b7f572013-06-24 15:06:52 +04001420 fprintf(stderr, "Unsupported number of maxcpus\n");
1421 exit(1);
1422 }
1423 if (max_cpus < smp_cpus) {
1424 fprintf(stderr, "maxcpus must be equal to or greater than smp\n");
1425 exit(1);
1426 }
1427
Andre Przywaradc6b1c02009-08-19 15:42:40 +02001428}
1429
zhanghailiang28d16f32014-09-23 18:42:24 +08001430static void realtime_init(void)
Satoru Moriya888a6bc2013-04-19 16:42:06 +02001431{
Satoru Moriya888a6bc2013-04-19 16:42:06 +02001432 if (enable_mlock) {
1433 if (os_mlock() < 0) {
1434 fprintf(stderr, "qemu: locking memory failed\n");
1435 exit(1);
1436 }
1437 }
1438}
1439
Seiji Aguchi5e2ac512013-07-03 23:02:46 -04001440
1441static void configure_msg(QemuOpts *opts)
1442{
1443 enable_timestamp_msg = qemu_opt_get_bool(opts, "timestamp", true);
1444}
1445
bellard330d0412003-07-26 18:11:40 +00001446/***********************************************************/
bellarda594cfb2005-11-06 16:13:29 +00001447/* USB devices */
1448
Markus Armbrusterfb080002010-05-28 15:38:44 +02001449static int usb_device_add(const char *devname)
bellarda594cfb2005-11-06 16:13:29 +00001450{
Gerd Hoffmanna5d2f722009-08-31 14:24:00 +02001451 USBDevice *dev = NULL;
Miroslav Rezanina615fe4d2013-09-03 11:23:09 +02001452#ifndef CONFIG_LINUX
1453 const char *p;
1454#endif
bellarda594cfb2005-11-06 16:13:29 +00001455
zhlcindy@gmail.com094b2872012-09-02 19:25:28 +00001456 if (!usb_enabled(false)) {
bellarda594cfb2005-11-06 16:13:29 +00001457 return -1;
zhlcindy@gmail.com094b2872012-09-02 19:25:28 +00001458 }
bellarda594cfb2005-11-06 16:13:29 +00001459
Gerd Hoffmann0958b4c2009-10-26 15:56:45 +01001460 /* drivers with .usbdevice_name entry in USBDeviceInfo */
1461 dev = usbdevice_create(devname);
1462 if (dev)
1463 goto done;
1464
Gerd Hoffmanna5d2f722009-08-31 14:24:00 +02001465 /* the other ones */
Gerd Hoffmanne447fc62011-06-01 14:41:59 +02001466#ifndef CONFIG_LINUX
1467 /* only the linux version is qdev-ified, usb-bsd still needs this */
bellarda594cfb2005-11-06 16:13:29 +00001468 if (strstart(devname, "host:", &p)) {
Jan Kiszka37417152012-02-27 15:18:47 +01001469 dev = usb_host_device_open(usb_bus_find(-1), p);
bellarda594cfb2005-11-06 16:13:29 +00001470 }
Miroslav Rezanina615fe4d2013-09-03 11:23:09 +02001471#endif
pbrook0d92ed32006-05-21 16:30:15 +00001472 if (!dev)
1473 return -1;
1474
Gerd Hoffmanna5d2f722009-08-31 14:24:00 +02001475done:
bellarda594cfb2005-11-06 16:13:29 +00001476 return 0;
1477}
1478
aliguori1f3870a2008-08-21 19:27:48 +00001479static int usb_device_del(const char *devname)
1480{
1481 int bus_num, addr;
1482 const char *p;
1483
Gerd Hoffmann1a3973b2012-11-30 13:02:47 +01001484 if (strstart(devname, "host:", &p)) {
1485 return -1;
1486 }
aliguori5d0c5752008-09-14 01:07:41 +00001487
zhlcindy@gmail.com094b2872012-09-02 19:25:28 +00001488 if (!usb_enabled(false)) {
aliguori1f3870a2008-08-21 19:27:48 +00001489 return -1;
zhlcindy@gmail.com094b2872012-09-02 19:25:28 +00001490 }
aliguori1f3870a2008-08-21 19:27:48 +00001491
1492 p = strchr(devname, '.');
1493 if (!p)
1494 return -1;
1495 bus_num = strtoul(devname, NULL, 0);
1496 addr = strtoul(p + 1, NULL, 0);
1497
Gerd Hoffmanna5d2f722009-08-31 14:24:00 +02001498 return usb_device_delete_addr(bus_num, addr);
aliguori1f3870a2008-08-21 19:27:48 +00001499}
1500
Gerd Hoffmannbd3c9482009-07-15 13:59:26 +02001501static int usb_parse(const char *cmdline)
1502{
Scott Tsai59d1c1c2009-12-23 04:30:18 +08001503 int r;
Markus Armbrusterfb080002010-05-28 15:38:44 +02001504 r = usb_device_add(cmdline);
Scott Tsai59d1c1c2009-12-23 04:30:18 +08001505 if (r < 0) {
1506 fprintf(stderr, "qemu: could not add USB device '%s'\n", cmdline);
1507 }
1508 return r;
Gerd Hoffmannbd3c9482009-07-15 13:59:26 +02001509}
1510
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001511void do_usb_add(Monitor *mon, const QDict *qdict)
bellarda594cfb2005-11-06 16:13:29 +00001512{
Scott Tsai59d1c1c2009-12-23 04:30:18 +08001513 const char *devname = qdict_get_str(qdict, "devname");
Markus Armbrusterfb080002010-05-28 15:38:44 +02001514 if (usb_device_add(devname) < 0) {
Markus Armbruster1ecda022010-02-18 17:25:24 +01001515 error_report("could not add USB device '%s'", devname);
Scott Tsai59d1c1c2009-12-23 04:30:18 +08001516 }
bellarda594cfb2005-11-06 16:13:29 +00001517}
1518
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001519void do_usb_del(Monitor *mon, const QDict *qdict)
bellarda594cfb2005-11-06 16:13:29 +00001520{
Scott Tsai59d1c1c2009-12-23 04:30:18 +08001521 const char *devname = qdict_get_str(qdict, "devname");
1522 if (usb_device_del(devname) < 0) {
Markus Armbruster1ecda022010-02-18 17:25:24 +01001523 error_report("could not delete USB device '%s'", devname);
Scott Tsai59d1c1c2009-12-23 04:30:18 +08001524 }
bellarda594cfb2005-11-06 16:13:29 +00001525}
1526
bellardf7cce892004-12-08 22:21:25 +00001527/***********************************************************/
balrog201a51f2007-04-30 00:51:09 +00001528/* PCMCIA/Cardbus */
1529
1530static struct pcmcia_socket_entry_s {
Paul Brookbc24a222009-05-10 01:44:56 +01001531 PCMCIASocket *socket;
balrog201a51f2007-04-30 00:51:09 +00001532 struct pcmcia_socket_entry_s *next;
1533} *pcmcia_sockets = 0;
1534
Paul Brookbc24a222009-05-10 01:44:56 +01001535void pcmcia_socket_register(PCMCIASocket *socket)
balrog201a51f2007-04-30 00:51:09 +00001536{
1537 struct pcmcia_socket_entry_s *entry;
1538
Anthony Liguori7267c092011-08-20 22:09:37 -05001539 entry = g_malloc(sizeof(struct pcmcia_socket_entry_s));
balrog201a51f2007-04-30 00:51:09 +00001540 entry->socket = socket;
1541 entry->next = pcmcia_sockets;
1542 pcmcia_sockets = entry;
1543}
1544
Paul Brookbc24a222009-05-10 01:44:56 +01001545void pcmcia_socket_unregister(PCMCIASocket *socket)
balrog201a51f2007-04-30 00:51:09 +00001546{
1547 struct pcmcia_socket_entry_s *entry, **ptr;
1548
1549 ptr = &pcmcia_sockets;
1550 for (entry = *ptr; entry; ptr = &entry->next, entry = *ptr)
1551 if (entry->socket == socket) {
1552 *ptr = entry->next;
Anthony Liguori7267c092011-08-20 22:09:37 -05001553 g_free(entry);
balrog201a51f2007-04-30 00:51:09 +00001554 }
1555}
1556
Wenchao Xia84f2d0e2013-01-14 14:06:25 +08001557void pcmcia_info(Monitor *mon, const QDict *qdict)
balrog201a51f2007-04-30 00:51:09 +00001558{
1559 struct pcmcia_socket_entry_s *iter;
aliguori376253e2009-03-05 23:01:23 +00001560
balrog201a51f2007-04-30 00:51:09 +00001561 if (!pcmcia_sockets)
aliguori376253e2009-03-05 23:01:23 +00001562 monitor_printf(mon, "No PCMCIA sockets\n");
balrog201a51f2007-04-30 00:51:09 +00001563
1564 for (iter = pcmcia_sockets; iter; iter = iter->next)
aliguori376253e2009-03-05 23:01:23 +00001565 monitor_printf(mon, "%s: %s\n", iter->socket->slot_string,
1566 iter->socket->attached ? iter->socket->card_string :
1567 "Empty");
balrog201a51f2007-04-30 00:51:09 +00001568}
1569
1570/***********************************************************/
bellardcc1daa42005-06-05 14:49:17 +00001571/* machine registration */
1572
Marcel Apfelbaum0056ae22014-03-05 19:30:47 +02001573MachineState *current_machine;
bellardcc1daa42005-06-05 14:49:17 +00001574
Marcel Apfelbaum261747f2014-03-05 19:30:46 +02001575static void machine_class_init(ObjectClass *oc, void *data)
1576{
1577 MachineClass *mc = MACHINE_CLASS(oc);
Marcel Apfelbaum00b4fbe2014-04-09 20:34:50 +03001578 QEMUMachine *qm = data;
Marcel Apfelbaum261747f2014-03-05 19:30:46 +02001579
Marcel Apfelbaum00b4fbe2014-04-09 20:34:50 +03001580 mc->name = qm->name;
1581 mc->alias = qm->alias;
1582 mc->desc = qm->desc;
1583 mc->init = qm->init;
1584 mc->reset = qm->reset;
1585 mc->hot_add_cpu = qm->hot_add_cpu;
1586 mc->kvm_type = qm->kvm_type;
1587 mc->block_default_type = qm->block_default_type;
1588 mc->max_cpus = qm->max_cpus;
1589 mc->no_serial = qm->no_serial;
1590 mc->no_parallel = qm->no_parallel;
1591 mc->use_virtcon = qm->use_virtcon;
1592 mc->use_sclp = qm->use_sclp;
1593 mc->no_floppy = qm->no_floppy;
1594 mc->no_cdrom = qm->no_cdrom;
1595 mc->no_sdcard = qm->no_sdcard;
1596 mc->is_default = qm->is_default;
1597 mc->default_machine_opts = qm->default_machine_opts;
1598 mc->default_boot_order = qm->default_boot_order;
1599 mc->compat_props = qm->compat_props;
1600 mc->hw_version = qm->hw_version;
Marcel Apfelbaum261747f2014-03-05 19:30:46 +02001601}
1602
bellardcc1daa42005-06-05 14:49:17 +00001603int qemu_register_machine(QEMUMachine *m)
1604{
Christian Borntraegerf5946db2014-03-19 12:24:27 +01001605 char *name = g_strconcat(m->name, TYPE_MACHINE_SUFFIX, NULL);
Marcel Apfelbaum261747f2014-03-05 19:30:46 +02001606 TypeInfo ti = {
Christian Borntraegerf5946db2014-03-19 12:24:27 +01001607 .name = name,
Marcel Apfelbaum261747f2014-03-05 19:30:46 +02001608 .parent = TYPE_MACHINE,
1609 .class_init = machine_class_init,
1610 .class_data = (void *)m,
1611 };
1612
1613 type_register(&ti);
Christian Borntraegerf5946db2014-03-19 12:24:27 +01001614 g_free(name);
Marcel Apfelbaum261747f2014-03-05 19:30:46 +02001615
bellardcc1daa42005-06-05 14:49:17 +00001616 return 0;
1617}
1618
Marcel Apfelbaum0056ae22014-03-05 19:30:47 +02001619static MachineClass *find_machine(const char *name)
bellardcc1daa42005-06-05 14:49:17 +00001620{
Marcel Apfelbaum261747f2014-03-05 19:30:46 +02001621 GSList *el, *machines = object_class_get_list(TYPE_MACHINE, false);
Marcel Apfelbaum0056ae22014-03-05 19:30:47 +02001622 MachineClass *mc = NULL;
bellardcc1daa42005-06-05 14:49:17 +00001623
Marcel Apfelbaum261747f2014-03-05 19:30:46 +02001624 for (el = machines; el; el = el->next) {
Marcel Apfelbaum0056ae22014-03-05 19:30:47 +02001625 MachineClass *temp = el->data;
Marcel Apfelbaum261747f2014-03-05 19:30:46 +02001626
Marcel Apfelbaum958db902014-04-09 20:34:53 +03001627 if (!strcmp(temp->name, name)) {
Marcel Apfelbaum0056ae22014-03-05 19:30:47 +02001628 mc = temp;
Marcel Apfelbaum261747f2014-03-05 19:30:46 +02001629 break;
1630 }
Marcel Apfelbaum958db902014-04-09 20:34:53 +03001631 if (temp->alias &&
1632 !strcmp(temp->alias, name)) {
Marcel Apfelbaum0056ae22014-03-05 19:30:47 +02001633 mc = temp;
Marcel Apfelbaum261747f2014-03-05 19:30:46 +02001634 break;
1635 }
bellardcc1daa42005-06-05 14:49:17 +00001636 }
Marcel Apfelbaum261747f2014-03-05 19:30:46 +02001637
1638 g_slist_free(machines);
Marcel Apfelbaum0056ae22014-03-05 19:30:47 +02001639 return mc;
bellardcc1daa42005-06-05 14:49:17 +00001640}
1641
Marcel Apfelbaum0056ae22014-03-05 19:30:47 +02001642MachineClass *find_default_machine(void)
Anthony Liguori0c257432009-05-21 20:41:01 -05001643{
Marcel Apfelbaum261747f2014-03-05 19:30:46 +02001644 GSList *el, *machines = object_class_get_list(TYPE_MACHINE, false);
Marcel Apfelbaum0056ae22014-03-05 19:30:47 +02001645 MachineClass *mc = NULL;
Anthony Liguori0c257432009-05-21 20:41:01 -05001646
Marcel Apfelbaum261747f2014-03-05 19:30:46 +02001647 for (el = machines; el; el = el->next) {
Marcel Apfelbaum0056ae22014-03-05 19:30:47 +02001648 MachineClass *temp = el->data;
Marcel Apfelbaum261747f2014-03-05 19:30:46 +02001649
Marcel Apfelbaum958db902014-04-09 20:34:53 +03001650 if (temp->is_default) {
Marcel Apfelbaum0056ae22014-03-05 19:30:47 +02001651 mc = temp;
Marcel Apfelbaum261747f2014-03-05 19:30:46 +02001652 break;
Anthony Liguori0c257432009-05-21 20:41:01 -05001653 }
1654 }
Marcel Apfelbaum261747f2014-03-05 19:30:46 +02001655
1656 g_slist_free(machines);
Marcel Apfelbaum0056ae22014-03-05 19:30:47 +02001657 return mc;
Anthony Liguori0c257432009-05-21 20:41:01 -05001658}
1659
Anthony Liguori01d3c802012-08-10 11:04:11 -05001660MachineInfoList *qmp_query_machines(Error **errp)
1661{
Marcel Apfelbaum261747f2014-03-05 19:30:46 +02001662 GSList *el, *machines = object_class_get_list(TYPE_MACHINE, false);
Anthony Liguori01d3c802012-08-10 11:04:11 -05001663 MachineInfoList *mach_list = NULL;
Anthony Liguori01d3c802012-08-10 11:04:11 -05001664
Marcel Apfelbaum261747f2014-03-05 19:30:46 +02001665 for (el = machines; el; el = el->next) {
1666 MachineClass *mc = el->data;
Anthony Liguori01d3c802012-08-10 11:04:11 -05001667 MachineInfoList *entry;
1668 MachineInfo *info;
1669
1670 info = g_malloc0(sizeof(*info));
Marcel Apfelbaum958db902014-04-09 20:34:53 +03001671 if (mc->is_default) {
Anthony Liguori01d3c802012-08-10 11:04:11 -05001672 info->has_is_default = true;
1673 info->is_default = true;
1674 }
1675
Marcel Apfelbaum958db902014-04-09 20:34:53 +03001676 if (mc->alias) {
Anthony Liguori01d3c802012-08-10 11:04:11 -05001677 info->has_alias = true;
Marcel Apfelbaum958db902014-04-09 20:34:53 +03001678 info->alias = g_strdup(mc->alias);
Anthony Liguori01d3c802012-08-10 11:04:11 -05001679 }
1680
Marcel Apfelbaum958db902014-04-09 20:34:53 +03001681 info->name = g_strdup(mc->name);
1682 info->cpu_max = !mc->max_cpus ? 1 : mc->max_cpus;
Anthony Liguori01d3c802012-08-10 11:04:11 -05001683
1684 entry = g_malloc0(sizeof(*entry));
1685 entry->value = info;
1686 entry->next = mach_list;
1687 mach_list = entry;
1688 }
1689
Marcel Apfelbaum261747f2014-03-05 19:30:46 +02001690 g_slist_free(machines);
Anthony Liguori01d3c802012-08-10 11:04:11 -05001691 return mach_list;
1692}
1693
bellardcc1daa42005-06-05 14:49:17 +00001694/***********************************************************/
bellard8a7ddc32004-03-31 19:00:16 +00001695/* main execution loop */
1696
bellard0bd48852005-11-11 00:00:47 +00001697struct vm_change_state_entry {
1698 VMChangeStateHandler *cb;
1699 void *opaque;
Blue Swirl72cf2d42009-09-12 07:36:22 +00001700 QLIST_ENTRY (vm_change_state_entry) entries;
bellard0bd48852005-11-11 00:00:47 +00001701};
1702
Blue Swirl72cf2d42009-09-12 07:36:22 +00001703static QLIST_HEAD(vm_change_state_head, vm_change_state_entry) vm_change_state_head;
bellard0bd48852005-11-11 00:00:47 +00001704
1705VMChangeStateEntry *qemu_add_vm_change_state_handler(VMChangeStateHandler *cb,
1706 void *opaque)
1707{
1708 VMChangeStateEntry *e;
1709
Anthony Liguori7267c092011-08-20 22:09:37 -05001710 e = g_malloc0(sizeof (*e));
bellard0bd48852005-11-11 00:00:47 +00001711
1712 e->cb = cb;
1713 e->opaque = opaque;
Blue Swirl72cf2d42009-09-12 07:36:22 +00001714 QLIST_INSERT_HEAD(&vm_change_state_head, e, entries);
bellard0bd48852005-11-11 00:00:47 +00001715 return e;
1716}
1717
1718void qemu_del_vm_change_state_handler(VMChangeStateEntry *e)
1719{
Blue Swirl72cf2d42009-09-12 07:36:22 +00001720 QLIST_REMOVE (e, entries);
Anthony Liguori7267c092011-08-20 22:09:37 -05001721 g_free (e);
bellard0bd48852005-11-11 00:00:47 +00001722}
1723
Luiz Capitulino1dfb4dd2011-07-29 14:26:33 -03001724void vm_state_notify(int running, RunState state)
bellard0bd48852005-11-11 00:00:47 +00001725{
Paolo Bonzini9b10ac82014-09-09 14:12:26 +02001726 VMChangeStateEntry *e, *next;
bellard0bd48852005-11-11 00:00:47 +00001727
Luiz Capitulino1dfb4dd2011-07-29 14:26:33 -03001728 trace_vm_state_notify(running, state);
Stefan Hajnoczi94b0b5f2010-11-16 12:20:25 +00001729
Paolo Bonzini9b10ac82014-09-09 14:12:26 +02001730 QLIST_FOREACH_SAFE(e, &vm_change_state_head, entries, next) {
Luiz Capitulino1dfb4dd2011-07-29 14:26:33 -03001731 e->cb(e->opaque, running, state);
bellard0bd48852005-11-11 00:00:47 +00001732 }
1733}
1734
bellardbb0c6722004-06-20 12:37:32 +00001735/* reset/shutdown handler */
1736
1737typedef struct QEMUResetEntry {
Blue Swirl72cf2d42009-09-12 07:36:22 +00001738 QTAILQ_ENTRY(QEMUResetEntry) entry;
bellardbb0c6722004-06-20 12:37:32 +00001739 QEMUResetHandler *func;
1740 void *opaque;
bellardbb0c6722004-06-20 12:37:32 +00001741} QEMUResetEntry;
1742
Blue Swirl72cf2d42009-09-12 07:36:22 +00001743static QTAILQ_HEAD(reset_handlers, QEMUResetEntry) reset_handlers =
1744 QTAILQ_HEAD_INITIALIZER(reset_handlers);
bellardbb0c6722004-06-20 12:37:32 +00001745static int reset_requested;
Gleb Natapovf64622c2011-03-15 13:56:04 +02001746static int shutdown_requested, shutdown_signal = -1;
1747static pid_t shutdown_pid;
bellard34751872005-07-02 14:31:34 +00001748static int powerdown_requested;
Jan Kiszka8cf71712011-02-07 12:19:16 +01001749static int debug_requested;
Gerd Hoffmann95b363b2012-02-23 13:45:19 +01001750static int suspend_requested;
Liu, Jinsong4bc78a82013-09-25 16:38:29 +00001751static WakeupReason wakeup_reason;
Igor Mammedova9552c82012-09-05 23:06:21 +02001752static NotifierList powerdown_notifiers =
1753 NOTIFIER_LIST_INITIALIZER(powerdown_notifiers);
Gerd Hoffmann95b363b2012-02-23 13:45:19 +01001754static NotifierList suspend_notifiers =
1755 NOTIFIER_LIST_INITIALIZER(suspend_notifiers);
1756static NotifierList wakeup_notifiers =
1757 NOTIFIER_LIST_INITIALIZER(wakeup_notifiers);
Liu, Jinsong4bc78a82013-09-25 16:38:29 +00001758static uint32_t wakeup_reason_mask = ~(1 << QEMU_WAKEUP_REASON_NONE);
bellardbb0c6722004-06-20 12:37:32 +00001759
Anthony PERARD1291eb32010-07-22 15:52:48 +01001760int qemu_shutdown_requested_get(void)
1761{
1762 return shutdown_requested;
1763}
1764
1765int qemu_reset_requested_get(void)
1766{
1767 return reset_requested;
1768}
1769
Blue Swirl4fdcac02012-10-28 11:04:47 +00001770static int qemu_shutdown_requested(void)
aurel32cf7a2fe2008-03-18 06:53:05 +00001771{
1772 int r = shutdown_requested;
1773 shutdown_requested = 0;
1774 return r;
1775}
1776
Blue Swirl4fdcac02012-10-28 11:04:47 +00001777static void qemu_kill_report(void)
Gleb Natapovf64622c2011-03-15 13:56:04 +02001778{
Michael S. Tsirkin35b30712014-02-04 20:04:21 +02001779 if (!qtest_driver() && shutdown_signal != -1) {
Peter Maydellf1d3fb02011-03-30 22:03:38 +01001780 fprintf(stderr, "qemu: terminating on signal %d", shutdown_signal);
1781 if (shutdown_pid == 0) {
1782 /* This happens for eg ^C at the terminal, so it's worth
1783 * avoiding printing an odd message in that case.
1784 */
1785 fputc('\n', stderr);
1786 } else {
Andreas Färber953ffe02011-06-02 19:58:06 +02001787 fprintf(stderr, " from pid " FMT_pid "\n", shutdown_pid);
Peter Maydellf1d3fb02011-03-30 22:03:38 +01001788 }
Gleb Natapovf64622c2011-03-15 13:56:04 +02001789 shutdown_signal = -1;
1790 }
1791}
1792
Blue Swirl4fdcac02012-10-28 11:04:47 +00001793static int qemu_reset_requested(void)
aurel32cf7a2fe2008-03-18 06:53:05 +00001794{
1795 int r = reset_requested;
1796 reset_requested = 0;
1797 return r;
1798}
1799
Gerd Hoffmann95b363b2012-02-23 13:45:19 +01001800static int qemu_suspend_requested(void)
1801{
1802 int r = suspend_requested;
1803 suspend_requested = 0;
1804 return r;
1805}
1806
Liu, Jinsong4bc78a82013-09-25 16:38:29 +00001807static WakeupReason qemu_wakeup_requested(void)
Luiz Capitulino14058192012-08-08 17:29:17 -03001808{
Liu, Jinsong4bc78a82013-09-25 16:38:29 +00001809 return wakeup_reason;
Luiz Capitulino14058192012-08-08 17:29:17 -03001810}
1811
Blue Swirl4fdcac02012-10-28 11:04:47 +00001812static int qemu_powerdown_requested(void)
aurel32cf7a2fe2008-03-18 06:53:05 +00001813{
1814 int r = powerdown_requested;
1815 powerdown_requested = 0;
1816 return r;
1817}
1818
aliguorie5689022009-04-24 18:03:54 +00001819static int qemu_debug_requested(void)
1820{
1821 int r = debug_requested;
1822 debug_requested = 0;
1823 return r;
1824}
1825
Jan Kiszkaa08d4362009-06-27 09:25:07 +02001826void qemu_register_reset(QEMUResetHandler *func, void *opaque)
bellardbb0c6722004-06-20 12:37:32 +00001827{
Anthony Liguori7267c092011-08-20 22:09:37 -05001828 QEMUResetEntry *re = g_malloc0(sizeof(QEMUResetEntry));
bellardbb0c6722004-06-20 12:37:32 +00001829
bellardbb0c6722004-06-20 12:37:32 +00001830 re->func = func;
1831 re->opaque = opaque;
Blue Swirl72cf2d42009-09-12 07:36:22 +00001832 QTAILQ_INSERT_TAIL(&reset_handlers, re, entry);
bellardbb0c6722004-06-20 12:37:32 +00001833}
1834
Jan Kiszkadda9b292009-07-02 00:19:02 +02001835void qemu_unregister_reset(QEMUResetHandler *func, void *opaque)
bellardbb0c6722004-06-20 12:37:32 +00001836{
1837 QEMUResetEntry *re;
1838
Blue Swirl72cf2d42009-09-12 07:36:22 +00001839 QTAILQ_FOREACH(re, &reset_handlers, entry) {
Jan Kiszkadda9b292009-07-02 00:19:02 +02001840 if (re->func == func && re->opaque == opaque) {
Blue Swirl72cf2d42009-09-12 07:36:22 +00001841 QTAILQ_REMOVE(&reset_handlers, re, entry);
Anthony Liguori7267c092011-08-20 22:09:37 -05001842 g_free(re);
Jan Kiszkadda9b292009-07-02 00:19:02 +02001843 return;
1844 }
1845 }
1846}
1847
David Gibsonbe522022012-08-07 16:41:51 +10001848void qemu_devices_reset(void)
Jan Kiszkadda9b292009-07-02 00:19:02 +02001849{
1850 QEMUResetEntry *re, *nre;
1851
1852 /* reset all devices */
Blue Swirl72cf2d42009-09-12 07:36:22 +00001853 QTAILQ_FOREACH_SAFE(re, &reset_handlers, entry, nre) {
bellardbb0c6722004-06-20 12:37:32 +00001854 re->func(re->opaque);
1855 }
David Gibsonbe522022012-08-07 16:41:51 +10001856}
1857
1858void qemu_system_reset(bool report)
1859{
Marcel Apfelbaum0056ae22014-03-05 19:30:47 +02001860 MachineClass *mc;
1861
1862 mc = current_machine ? MACHINE_GET_CLASS(current_machine) : NULL;
1863
Marcel Apfelbaum958db902014-04-09 20:34:53 +03001864 if (mc && mc->reset) {
1865 mc->reset();
David Gibsonbe522022012-08-07 16:41:51 +10001866 } else {
1867 qemu_devices_reset();
1868 }
Jan Kiszkae063eb12011-06-14 18:29:43 +02001869 if (report) {
Wenchao Xiaa6330782014-06-18 08:43:35 +02001870 qapi_event_send_reset(&error_abort);
Jan Kiszkae063eb12011-06-14 18:29:43 +02001871 }
Jan Kiszkaea375f92010-03-01 19:10:30 +01001872 cpu_synchronize_all_post_reset();
bellardbb0c6722004-06-20 12:37:32 +00001873}
1874
1875void qemu_system_reset_request(void)
1876{
bellardd1beab82006-10-02 19:44:22 +00001877 if (no_reboot) {
1878 shutdown_requested = 1;
1879 } else {
1880 reset_requested = 1;
1881 }
Jan Kiszkab4a3d962011-02-01 22:15:43 +01001882 cpu_stop_current();
aliguorid9f75a42009-04-24 18:03:11 +00001883 qemu_notify_event();
bellardbb0c6722004-06-20 12:37:32 +00001884}
1885
Gerd Hoffmann95b363b2012-02-23 13:45:19 +01001886static void qemu_system_suspend(void)
1887{
1888 pause_all_vcpus();
1889 notifier_list_notify(&suspend_notifiers, NULL);
Luiz Capitulinoad02b962012-04-27 13:33:36 -03001890 runstate_set(RUN_STATE_SUSPENDED);
Wenchao Xia1d11a952014-06-18 08:43:38 +02001891 qapi_event_send_suspend(&error_abort);
Gerd Hoffmann95b363b2012-02-23 13:45:19 +01001892}
1893
1894void qemu_system_suspend_request(void)
1895{
Luiz Capitulino9abc62f2012-04-27 14:31:12 -03001896 if (runstate_check(RUN_STATE_SUSPENDED)) {
Gerd Hoffmann95b363b2012-02-23 13:45:19 +01001897 return;
1898 }
1899 suspend_requested = 1;
1900 cpu_stop_current();
1901 qemu_notify_event();
1902}
1903
1904void qemu_register_suspend_notifier(Notifier *notifier)
1905{
1906 notifier_list_add(&suspend_notifiers, notifier);
1907}
1908
1909void qemu_system_wakeup_request(WakeupReason reason)
1910{
Alexey Kardashevskiy4fed9422014-03-07 01:33:36 +05301911 trace_system_wakeup_request(reason);
1912
Luiz Capitulino9abc62f2012-04-27 14:31:12 -03001913 if (!runstate_check(RUN_STATE_SUSPENDED)) {
Gerd Hoffmann95b363b2012-02-23 13:45:19 +01001914 return;
1915 }
1916 if (!(wakeup_reason_mask & (1 << reason))) {
1917 return;
1918 }
Luiz Capitulinoad02b962012-04-27 13:33:36 -03001919 runstate_set(RUN_STATE_RUNNING);
Liu, Jinsong4bc78a82013-09-25 16:38:29 +00001920 wakeup_reason = reason;
Gerd Hoffmann95b363b2012-02-23 13:45:19 +01001921 qemu_notify_event();
Gerd Hoffmann95b363b2012-02-23 13:45:19 +01001922}
1923
1924void qemu_system_wakeup_enable(WakeupReason reason, bool enabled)
1925{
1926 if (enabled) {
1927 wakeup_reason_mask |= (1 << reason);
1928 } else {
1929 wakeup_reason_mask &= ~(1 << reason);
1930 }
1931}
1932
1933void qemu_register_wakeup_notifier(Notifier *notifier)
1934{
1935 notifier_list_add(&wakeup_notifiers, notifier);
1936}
1937
Gleb Natapovf64622c2011-03-15 13:56:04 +02001938void qemu_system_killed(int signal, pid_t pid)
1939{
1940 shutdown_signal = signal;
1941 shutdown_pid = pid;
Kevin Wolfd9389b92011-09-14 15:38:40 +02001942 no_shutdown = 0;
Gleb Natapovf64622c2011-03-15 13:56:04 +02001943 qemu_system_shutdown_request();
1944}
1945
bellardbb0c6722004-06-20 12:37:32 +00001946void qemu_system_shutdown_request(void)
1947{
Yang Zhiyongbc78cff2014-06-22 02:43:03 +08001948 trace_qemu_system_shutdown_request();
bellardbb0c6722004-06-20 12:37:32 +00001949 shutdown_requested = 1;
aliguorid9f75a42009-04-24 18:03:11 +00001950 qemu_notify_event();
bellardbb0c6722004-06-20 12:37:32 +00001951}
1952
Igor Mammedov013c2f12012-09-05 23:06:25 +02001953static void qemu_system_powerdown(void)
1954{
Wenchao Xia0aab9ec2014-06-18 08:43:34 +02001955 qapi_event_send_powerdown(&error_abort);
Igor Mammedov013c2f12012-09-05 23:06:25 +02001956 notifier_list_notify(&powerdown_notifiers, NULL);
1957}
1958
bellard34751872005-07-02 14:31:34 +00001959void qemu_system_powerdown_request(void)
1960{
Yang Zhiyongbc78cff2014-06-22 02:43:03 +08001961 trace_qemu_system_powerdown_request();
bellard34751872005-07-02 14:31:34 +00001962 powerdown_requested = 1;
aliguorid9f75a42009-04-24 18:03:11 +00001963 qemu_notify_event();
1964}
1965
Igor Mammedova9552c82012-09-05 23:06:21 +02001966void qemu_register_powerdown_notifier(Notifier *notifier)
1967{
1968 notifier_list_add(&powerdown_notifiers, notifier);
1969}
1970
Jan Kiszka8cf71712011-02-07 12:19:16 +01001971void qemu_system_debug_request(void)
1972{
1973 debug_requested = 1;
Jan Kiszka83f338f2011-02-07 12:19:17 +01001974 qemu_notify_event();
Jan Kiszka8cf71712011-02-07 12:19:16 +01001975}
1976
Paolo Bonzini99435902011-09-12 14:03:13 +02001977static bool main_loop_should_exit(void)
1978{
1979 RunState r;
1980 if (qemu_debug_requested()) {
1981 vm_stop(RUN_STATE_DEBUG);
1982 }
Gerd Hoffmann95b363b2012-02-23 13:45:19 +01001983 if (qemu_suspend_requested()) {
1984 qemu_system_suspend();
1985 }
Paolo Bonzini99435902011-09-12 14:03:13 +02001986 if (qemu_shutdown_requested()) {
1987 qemu_kill_report();
Wenchao Xia84321832014-06-18 08:43:33 +02001988 qapi_event_send_shutdown(&error_abort);
Paolo Bonzini99435902011-09-12 14:03:13 +02001989 if (no_shutdown) {
1990 vm_stop(RUN_STATE_SHUTDOWN);
1991 } else {
1992 return true;
1993 }
1994 }
1995 if (qemu_reset_requested()) {
1996 pause_all_vcpus();
1997 cpu_synchronize_all_states();
1998 qemu_system_reset(VMRESET_REPORT);
1999 resume_all_vcpus();
Hu Taoede085b2013-04-26 11:24:40 +08002000 if (runstate_needs_reset()) {
Paolo Bonzini99435902011-09-12 14:03:13 +02002001 runstate_set(RUN_STATE_PAUSED);
2002 }
2003 }
Luiz Capitulino14058192012-08-08 17:29:17 -03002004 if (qemu_wakeup_requested()) {
2005 pause_all_vcpus();
2006 cpu_synchronize_all_states();
2007 qemu_system_reset(VMRESET_SILENT);
Liu, Jinsong4bc78a82013-09-25 16:38:29 +00002008 notifier_list_notify(&wakeup_notifiers, &wakeup_reason);
2009 wakeup_reason = QEMU_WAKEUP_REASON_NONE;
Luiz Capitulino14058192012-08-08 17:29:17 -03002010 resume_all_vcpus();
Wenchao Xia7a906f72014-06-18 08:43:40 +02002011 qapi_event_send_wakeup(&error_abort);
Luiz Capitulino14058192012-08-08 17:29:17 -03002012 }
Paolo Bonzini99435902011-09-12 14:03:13 +02002013 if (qemu_powerdown_requested()) {
Igor Mammedov013c2f12012-09-05 23:06:25 +02002014 qemu_system_powerdown();
Paolo Bonzini99435902011-09-12 14:03:13 +02002015 }
2016 if (qemu_vmstop_requested(&r)) {
2017 vm_stop(r);
2018 }
2019 return false;
2020}
2021
aliguori43b96852009-04-24 18:03:33 +00002022static void main_loop(void)
2023{
Jan Kiszkac9f711a2011-08-22 17:46:02 +02002024 bool nonblocking;
Paolo Bonzini99435902011-09-12 14:03:13 +02002025 int last_io = 0;
Jan Kiszka8e1b90e2011-02-01 22:15:46 +01002026#ifdef CONFIG_PROFILER
2027 int64_t ti;
2028#endif
Paolo Bonzini99435902011-09-12 14:03:13 +02002029 do {
Stefano Stabellinia7d42072013-06-03 15:38:43 +00002030 nonblocking = !kvm_enabled() && !xen_enabled() && last_io > 0;
aliguori43b96852009-04-24 18:03:33 +00002031#ifdef CONFIG_PROFILER
Jan Kiszka46481d32011-02-01 22:15:47 +01002032 ti = profile_getclock();
aliguori43b96852009-04-24 18:03:33 +00002033#endif
Jan Kiszkac9f711a2011-08-22 17:46:02 +02002034 last_io = main_loop_wait(nonblocking);
aliguori43b96852009-04-24 18:03:33 +00002035#ifdef CONFIG_PROFILER
Jan Kiszka46481d32011-02-01 22:15:47 +01002036 dev_time += profile_getclock() - ti;
aliguori43b96852009-04-24 18:03:33 +00002037#endif
Paolo Bonzini99435902011-09-12 14:03:13 +02002038 } while (!main_loop_should_exit());
bellardb4608c02003-06-27 17:34:32 +00002039}
2040
pbrook9bd7e6d2009-04-07 22:58:45 +00002041static void version(void)
2042{
Thomas Monjalonf75ca1a2010-04-28 14:42:01 +02002043 printf("QEMU emulator version " QEMU_VERSION QEMU_PKGVERSION ", Copyright (c) 2003-2008 Fabrice Bellard\n");
pbrook9bd7e6d2009-04-07 22:58:45 +00002044}
2045
ths15f82202007-06-29 23:26:08 +00002046static void help(int exitcode)
bellard0824d6f2003-06-24 13:42:40 +00002047{
Michael Ellermana3adb7a2011-12-19 17:19:31 +11002048 version();
2049 printf("usage: %s [options] [disk_image]\n\n"
2050 "'disk_image' is a raw hard disk image for IDE hard disk 0\n\n",
2051 error_get_progname());
2052
Michael Ellerman77bd1112011-12-19 17:19:30 +11002053#define QEMU_OPTIONS_GENERATE_HELP
2054#include "qemu-options-wrapper.h"
Michael Ellermana3adb7a2011-12-19 17:19:31 +11002055
2056 printf("\nDuring emulation, the following keys are useful:\n"
malc3f020d72010-02-08 12:04:56 +03002057 "ctrl-alt-f toggle full screen\n"
2058 "ctrl-alt-n switch to virtual console 'n'\n"
2059 "ctrl-alt toggle mouse and keyboard grab\n"
2060 "\n"
Michael Ellermana3adb7a2011-12-19 17:19:31 +11002061 "When using -nographic, press 'ctrl-a h' to get some help.\n");
2062
ths15f82202007-06-29 23:26:08 +00002063 exit(exitcode);
bellard0824d6f2003-06-24 13:42:40 +00002064}
2065
bellardcd6f1162004-05-13 22:02:20 +00002066#define HAS_ARG 0x0001
2067
bellardcd6f1162004-05-13 22:02:20 +00002068typedef struct QEMUOption {
2069 const char *name;
2070 int flags;
2071 int index;
Blue Swirlad960902010-03-29 19:23:52 +00002072 uint32_t arch_mask;
bellardcd6f1162004-05-13 22:02:20 +00002073} QEMUOption;
2074
blueswir1dbed7e42008-10-01 19:38:09 +00002075static const QEMUOption qemu_options[] = {
Blue Swirlad960902010-03-29 19:23:52 +00002076 { "h", 0, QEMU_OPTION_h, QEMU_ARCH_ALL },
Michael Ellerman77bd1112011-12-19 17:19:30 +11002077#define QEMU_OPTIONS_GENERATE_OPTIONS
2078#include "qemu-options-wrapper.h"
bellardcd6f1162004-05-13 22:02:20 +00002079 { NULL },
bellardfc01f7e2003-06-30 10:03:06 +00002080};
Blue Swirla369da52011-09-27 19:15:42 +00002081
2082static bool vga_available(void)
2083{
Aurelien Jarno36b7f272012-09-08 16:01:20 +02002084 return object_class_by_name("VGA") || object_class_by_name("isa-vga");
Blue Swirla369da52011-09-27 19:15:42 +00002085}
2086
2087static bool cirrus_vga_available(void)
2088{
Aurelien Jarno36b7f272012-09-08 16:01:20 +02002089 return object_class_by_name("cirrus-vga")
2090 || object_class_by_name("isa-cirrus-vga");
Blue Swirla369da52011-09-27 19:15:42 +00002091}
2092
2093static bool vmware_vga_available(void)
2094{
Aurelien Jarno36b7f272012-09-08 16:01:20 +02002095 return object_class_by_name("vmware-svga");
Blue Swirla369da52011-09-27 19:15:42 +00002096}
2097
Aurelien Jarno879049a2012-09-08 12:01:06 +02002098static bool qxl_vga_available(void)
2099{
2100 return object_class_by_name("qxl-vga");
2101}
2102
Mark Cave-Aylandaf87bf22013-10-15 21:03:04 +01002103static bool tcx_vga_available(void)
2104{
2105 return object_class_by_name("SUNW,tcx");
2106}
2107
2108static bool cg3_vga_available(void)
2109{
2110 return object_class_by_name("cgthree");
2111}
2112
malc3893c122008-09-28 00:42:05 +00002113static void select_vgahw (const char *p)
2114{
2115 const char *opts;
2116
Mark Wud44229c2014-03-10 22:37:40 +08002117 assert(vga_interface_type == VGA_NONE);
malc3893c122008-09-28 00:42:05 +00002118 if (strstart(p, "std", &opts)) {
Blue Swirla369da52011-09-27 19:15:42 +00002119 if (vga_available()) {
2120 vga_interface_type = VGA_STD;
2121 } else {
2122 fprintf(stderr, "Error: standard VGA not available\n");
2123 exit(0);
2124 }
malc3893c122008-09-28 00:42:05 +00002125 } else if (strstart(p, "cirrus", &opts)) {
Blue Swirla369da52011-09-27 19:15:42 +00002126 if (cirrus_vga_available()) {
2127 vga_interface_type = VGA_CIRRUS;
2128 } else {
2129 fprintf(stderr, "Error: Cirrus VGA not available\n");
2130 exit(0);
2131 }
malc3893c122008-09-28 00:42:05 +00002132 } else if (strstart(p, "vmware", &opts)) {
Blue Swirla369da52011-09-27 19:15:42 +00002133 if (vmware_vga_available()) {
2134 vga_interface_type = VGA_VMWARE;
2135 } else {
2136 fprintf(stderr, "Error: VMWare SVGA not available\n");
2137 exit(0);
2138 }
aliguori94909d92009-04-22 15:19:53 +00002139 } else if (strstart(p, "xenfb", &opts)) {
Zachary Amsden86176752009-07-30 00:15:02 -10002140 vga_interface_type = VGA_XENFB;
Gerd Hoffmanna19cbfb2010-04-27 11:50:11 +02002141 } else if (strstart(p, "qxl", &opts)) {
Aurelien Jarno879049a2012-09-08 12:01:06 +02002142 if (qxl_vga_available()) {
2143 vga_interface_type = VGA_QXL;
2144 } else {
2145 fprintf(stderr, "Error: QXL VGA not available\n");
2146 exit(0);
2147 }
Mark Cave-Aylandaf87bf22013-10-15 21:03:04 +01002148 } else if (strstart(p, "tcx", &opts)) {
2149 if (tcx_vga_available()) {
2150 vga_interface_type = VGA_TCX;
2151 } else {
2152 fprintf(stderr, "Error: TCX framebuffer not available\n");
2153 exit(0);
2154 }
2155 } else if (strstart(p, "cg3", &opts)) {
2156 if (cg3_vga_available()) {
2157 vga_interface_type = VGA_CG3;
2158 } else {
2159 fprintf(stderr, "Error: CG3 framebuffer not available\n");
2160 exit(0);
2161 }
aliguori28b85ed2009-04-22 15:19:48 +00002162 } else if (!strstart(p, "none", &opts)) {
malc3893c122008-09-28 00:42:05 +00002163 invalid_vga:
2164 fprintf(stderr, "Unknown vga type: %s\n", p);
2165 exit(1);
2166 }
malccb5a7aa2008-09-28 00:42:12 +00002167 while (*opts) {
2168 const char *nextopt;
2169
2170 if (strstart(opts, ",retrace=", &nextopt)) {
2171 opts = nextopt;
2172 if (strstart(opts, "dumb", &nextopt))
2173 vga_retrace_method = VGA_RETRACE_DUMB;
2174 else if (strstart(opts, "precise", &nextopt))
2175 vga_retrace_method = VGA_RETRACE_PRECISE;
2176 else goto invalid_vga;
2177 } else goto invalid_vga;
2178 opts = nextopt;
2179 }
malc3893c122008-09-28 00:42:05 +00002180}
2181
Jes Sorensen1472a952011-03-16 13:33:31 +01002182static DisplayType select_display(const char *p)
2183{
2184 const char *opts;
2185 DisplayType display = DT_DEFAULT;
2186
2187 if (strstart(p, "sdl", &opts)) {
2188#ifdef CONFIG_SDL
2189 display = DT_SDL;
2190 while (*opts) {
2191 const char *nextopt;
2192
2193 if (strstart(opts, ",frame=", &nextopt)) {
2194 opts = nextopt;
2195 if (strstart(opts, "on", &nextopt)) {
2196 no_frame = 0;
2197 } else if (strstart(opts, "off", &nextopt)) {
2198 no_frame = 1;
2199 } else {
Peter Maydell05175532011-03-23 03:40:57 +00002200 goto invalid_sdl_args;
Jes Sorensen1472a952011-03-16 13:33:31 +01002201 }
2202 } else if (strstart(opts, ",alt_grab=", &nextopt)) {
2203 opts = nextopt;
2204 if (strstart(opts, "on", &nextopt)) {
2205 alt_grab = 1;
2206 } else if (strstart(opts, "off", &nextopt)) {
2207 alt_grab = 0;
2208 } else {
Peter Maydell05175532011-03-23 03:40:57 +00002209 goto invalid_sdl_args;
Jes Sorensen1472a952011-03-16 13:33:31 +01002210 }
2211 } else if (strstart(opts, ",ctrl_grab=", &nextopt)) {
2212 opts = nextopt;
2213 if (strstart(opts, "on", &nextopt)) {
2214 ctrl_grab = 1;
2215 } else if (strstart(opts, "off", &nextopt)) {
2216 ctrl_grab = 0;
2217 } else {
Peter Maydell05175532011-03-23 03:40:57 +00002218 goto invalid_sdl_args;
Jes Sorensen1472a952011-03-16 13:33:31 +01002219 }
2220 } else if (strstart(opts, ",window_close=", &nextopt)) {
2221 opts = nextopt;
2222 if (strstart(opts, "on", &nextopt)) {
2223 no_quit = 0;
2224 } else if (strstart(opts, "off", &nextopt)) {
2225 no_quit = 1;
2226 } else {
Peter Maydell05175532011-03-23 03:40:57 +00002227 goto invalid_sdl_args;
Jes Sorensen1472a952011-03-16 13:33:31 +01002228 }
2229 } else {
Peter Maydell05175532011-03-23 03:40:57 +00002230 invalid_sdl_args:
2231 fprintf(stderr, "Invalid SDL option string: %s\n", p);
2232 exit(1);
Jes Sorensen1472a952011-03-16 13:33:31 +01002233 }
2234 opts = nextopt;
2235 }
2236#else
2237 fprintf(stderr, "SDL support is disabled\n");
2238 exit(1);
2239#endif
Jes Sorensen3264ff12011-03-16 13:33:33 +01002240 } else if (strstart(p, "vnc", &opts)) {
Jes Sorensen821601e2011-03-16 13:33:36 +01002241#ifdef CONFIG_VNC
Jes Sorensen3264ff12011-03-16 13:33:33 +01002242 display_remote++;
2243
2244 if (*opts) {
2245 const char *nextopt;
2246
2247 if (strstart(opts, "=", &nextopt)) {
2248 vnc_display = nextopt;
2249 }
2250 }
2251 if (!vnc_display) {
2252 fprintf(stderr, "VNC requires a display argument vnc=<display>\n");
2253 exit(1);
2254 }
Jes Sorensen821601e2011-03-16 13:33:36 +01002255#else
2256 fprintf(stderr, "VNC support is disabled\n");
2257 exit(1);
2258#endif
Jes Sorensen1472a952011-03-16 13:33:31 +01002259 } else if (strstart(p, "curses", &opts)) {
2260#ifdef CONFIG_CURSES
2261 display = DT_CURSES;
2262#else
2263 fprintf(stderr, "Curses support is disabled\n");
2264 exit(1);
2265#endif
Anthony Liguori15546422013-02-20 07:43:25 -06002266 } else if (strstart(p, "gtk", &opts)) {
2267#ifdef CONFIG_GTK
2268 display = DT_GTK;
Jan Kiszka881249c2014-03-12 08:33:50 +01002269 while (*opts) {
2270 const char *nextopt;
2271
2272 if (strstart(opts, ",grab_on_hover=", &nextopt)) {
2273 opts = nextopt;
2274 if (strstart(opts, "on", &nextopt)) {
2275 grab_on_hover = true;
2276 } else if (strstart(opts, "off", &nextopt)) {
2277 grab_on_hover = false;
2278 } else {
2279 goto invalid_gtk_args;
2280 }
2281 } else {
2282 invalid_gtk_args:
2283 fprintf(stderr, "Invalid GTK option string: %s\n", p);
2284 exit(1);
2285 }
2286 opts = nextopt;
2287 }
Anthony Liguori15546422013-02-20 07:43:25 -06002288#else
2289 fprintf(stderr, "GTK support is disabled\n");
2290 exit(1);
2291#endif
Jes Sorensen4171d322011-03-16 13:33:32 +01002292 } else if (strstart(p, "none", &opts)) {
2293 display = DT_NONE;
Jes Sorensen1472a952011-03-16 13:33:31 +01002294 } else {
Jes Sorensen1472a952011-03-16 13:33:31 +01002295 fprintf(stderr, "Unknown display type: %s\n", p);
2296 exit(1);
2297 }
2298
2299 return display;
2300}
2301
Markus Armbruster7d4c3d52009-06-26 19:15:14 +02002302static int balloon_parse(const char *arg)
2303{
Gerd Hoffmann382f0742009-08-14 10:34:22 +02002304 QemuOpts *opts;
Markus Armbruster7d4c3d52009-06-26 19:15:14 +02002305
Gerd Hoffmann382f0742009-08-14 10:34:22 +02002306 if (strcmp(arg, "none") == 0) {
2307 return 0;
Markus Armbruster7d4c3d52009-06-26 19:15:14 +02002308 }
Gerd Hoffmann382f0742009-08-14 10:34:22 +02002309
2310 if (!strncmp(arg, "virtio", 6)) {
2311 if (arg[6] == ',') {
2312 /* have params -> parse them */
Gerd Hoffmann3329f072010-08-20 13:52:01 +02002313 opts = qemu_opts_parse(qemu_find_opts("device"), arg+7, 0);
Gerd Hoffmann382f0742009-08-14 10:34:22 +02002314 if (!opts)
2315 return -1;
2316 } else {
2317 /* create empty opts */
Peter Crosthwaite87ea75d2014-01-01 18:49:17 -08002318 opts = qemu_opts_create(qemu_find_opts("device"), NULL, 0,
2319 &error_abort);
Gerd Hoffmann382f0742009-08-14 10:34:22 +02002320 }
Alexander Graf29f82b32011-03-29 15:29:29 +02002321 qemu_opt_set(opts, "driver", "virtio-balloon");
Gerd Hoffmann382f0742009-08-14 10:34:22 +02002322 return 0;
2323 }
2324
2325 return -1;
Markus Armbruster7d4c3d52009-06-26 19:15:14 +02002326}
Markus Armbruster7d4c3d52009-06-26 19:15:14 +02002327
Paul Brook5cea8592009-05-30 00:52:44 +01002328char *qemu_find_file(int type, const char *name)
2329{
Gerd Hoffmann45240512013-03-08 11:42:24 +01002330 int i;
Paul Brook5cea8592009-05-30 00:52:44 +01002331 const char *subdir;
2332 char *buf;
2333
Peter Maydell31783202012-05-25 13:07:01 +01002334 /* Try the name as a straight path first */
2335 if (access(name, R_OK) == 0) {
Gerd Hoffmann45240512013-03-08 11:42:24 +01002336 trace_load_file(name, name);
Anthony Liguori7267c092011-08-20 22:09:37 -05002337 return g_strdup(name);
Paul Brook5cea8592009-05-30 00:52:44 +01002338 }
Gerd Hoffmann45240512013-03-08 11:42:24 +01002339
Paul Brook5cea8592009-05-30 00:52:44 +01002340 switch (type) {
2341 case QEMU_FILE_TYPE_BIOS:
2342 subdir = "";
2343 break;
2344 case QEMU_FILE_TYPE_KEYMAP:
2345 subdir = "keymaps/";
2346 break;
2347 default:
2348 abort();
2349 }
Gerd Hoffmann45240512013-03-08 11:42:24 +01002350
2351 for (i = 0; i < data_dir_idx; i++) {
2352 buf = g_strdup_printf("%s/%s%s", data_dir[i], subdir, name);
2353 if (access(buf, R_OK) == 0) {
2354 trace_load_file(name, buf);
2355 return buf;
2356 }
Anthony Liguori7267c092011-08-20 22:09:37 -05002357 g_free(buf);
Paul Brook5cea8592009-05-30 00:52:44 +01002358 }
Gerd Hoffmann45240512013-03-08 11:42:24 +01002359 return NULL;
Paul Brook5cea8592009-05-30 00:52:44 +01002360}
2361
Markus Armbrusterff952ba2010-01-29 19:48:57 +01002362static int device_help_func(QemuOpts *opts, void *opaque)
2363{
2364 return qdev_device_help(opts);
2365}
2366
Gerd Hoffmannf31d07d2009-07-31 12:25:37 +02002367static int device_init_func(QemuOpts *opts, void *opaque)
2368{
2369 DeviceState *dev;
2370
2371 dev = qdev_device_add(opts);
2372 if (!dev)
2373 return -1;
Paolo Bonzinib09995a2013-01-25 14:12:37 +01002374 object_unref(OBJECT(dev));
Gerd Hoffmannf31d07d2009-07-31 12:25:37 +02002375 return 0;
2376}
2377
Gerd Hoffmann1a688d32009-12-08 13:11:36 +01002378static int chardev_init_func(QemuOpts *opts, void *opaque)
2379{
Gerd Hoffmannbd2d80b2012-10-15 09:28:05 +02002380 Error *local_err = NULL;
Gerd Hoffmann1a688d32009-12-08 13:11:36 +01002381
Gerd Hoffmannbd2d80b2012-10-15 09:28:05 +02002382 qemu_chr_new_from_opts(opts, NULL, &local_err);
Markus Armbruster84d18f02014-01-30 15:07:28 +01002383 if (local_err) {
Seiji Aguchi4a44d852013-08-05 15:40:44 -04002384 error_report("%s", error_get_pretty(local_err));
Gerd Hoffmannbd2d80b2012-10-15 09:28:05 +02002385 error_free(local_err);
Gerd Hoffmann1a688d32009-12-08 13:11:36 +01002386 return -1;
Gerd Hoffmannbd2d80b2012-10-15 09:28:05 +02002387 }
Gerd Hoffmann1a688d32009-12-08 13:11:36 +01002388 return 0;
2389}
2390
Venkateswararao Jujjuri (JV)758e8e32010-06-14 13:34:41 -07002391#ifdef CONFIG_VIRTFS
Gautham R Shenoy74db9202010-04-29 17:44:43 +05302392static int fsdev_init_func(QemuOpts *opts, void *opaque)
2393{
2394 int ret;
2395 ret = qemu_fsdev_add(opts);
2396
2397 return ret;
2398}
2399#endif
2400
Gerd Hoffmann88589342009-12-08 13:11:50 +01002401static int mon_init_func(QemuOpts *opts, void *opaque)
2402{
2403 CharDriverState *chr;
2404 const char *chardev;
2405 const char *mode;
2406 int flags;
2407
2408 mode = qemu_opt_get(opts, "mode");
2409 if (mode == NULL) {
2410 mode = "readline";
2411 }
2412 if (strcmp(mode, "readline") == 0) {
2413 flags = MONITOR_USE_READLINE;
2414 } else if (strcmp(mode, "control") == 0) {
2415 flags = MONITOR_USE_CONTROL;
2416 } else {
2417 fprintf(stderr, "unknown monitor mode \"%s\"\n", mode);
2418 exit(1);
2419 }
2420
Daniel P. Berrange39eaab92010-06-07 15:42:31 +01002421 if (qemu_opt_get_bool(opts, "pretty", 0))
2422 flags |= MONITOR_USE_PRETTY;
2423
Gerd Hoffmann88589342009-12-08 13:11:50 +01002424 if (qemu_opt_get_bool(opts, "default", 0))
2425 flags |= MONITOR_IS_DEFAULT;
2426
2427 chardev = qemu_opt_get(opts, "chardev");
2428 chr = qemu_chr_find(chardev);
2429 if (chr == NULL) {
2430 fprintf(stderr, "chardev \"%s\" not found\n", chardev);
2431 exit(1);
2432 }
2433
Hans de Goede456d6062013-03-27 20:29:40 +01002434 qemu_chr_fe_claim_no_fail(chr);
Gerd Hoffmann88589342009-12-08 13:11:50 +01002435 monitor_init(chr, flags);
2436 return 0;
2437}
2438
Gerd Hoffmann6ca55822009-12-08 13:11:52 +01002439static void monitor_parse(const char *optarg, const char *mode)
Gerd Hoffmann88589342009-12-08 13:11:50 +01002440{
2441 static int monitor_device_index = 0;
2442 QemuOpts *opts;
2443 const char *p;
2444 char label[32];
2445 int def = 0;
2446
2447 if (strstart(optarg, "chardev:", &p)) {
2448 snprintf(label, sizeof(label), "%s", p);
2449 } else {
Jan Kiszka140e0652010-04-06 16:55:52 +02002450 snprintf(label, sizeof(label), "compat_monitor%d",
2451 monitor_device_index);
2452 if (monitor_device_index == 0) {
Gerd Hoffmann88589342009-12-08 13:11:50 +01002453 def = 1;
2454 }
2455 opts = qemu_chr_parse_compat(label, optarg);
2456 if (!opts) {
2457 fprintf(stderr, "parse error: %s\n", optarg);
2458 exit(1);
2459 }
2460 }
2461
Luiz Capitulino8be7e7e2012-03-20 15:51:57 -03002462 opts = qemu_opts_create(qemu_find_opts("mon"), label, 1, NULL);
Gerd Hoffmann88589342009-12-08 13:11:50 +01002463 if (!opts) {
2464 fprintf(stderr, "duplicate chardev: %s\n", label);
2465 exit(1);
2466 }
Gerd Hoffmann6ca55822009-12-08 13:11:52 +01002467 qemu_opt_set(opts, "mode", mode);
Gerd Hoffmann88589342009-12-08 13:11:50 +01002468 qemu_opt_set(opts, "chardev", label);
2469 if (def)
2470 qemu_opt_set(opts, "default", "on");
2471 monitor_device_index++;
2472}
2473
Gerd Hoffmannbd3c9482009-07-15 13:59:26 +02002474struct device_config {
2475 enum {
Gerd Hoffmannaee1b932009-12-08 13:11:53 +01002476 DEV_USB, /* -usbdevice */
2477 DEV_BT, /* -bt */
2478 DEV_SERIAL, /* -serial */
2479 DEV_PARALLEL, /* -parallel */
2480 DEV_VIRTCON, /* -virtioconsole */
H. Peter Anvinc9f398e2009-12-29 13:51:36 -08002481 DEV_DEBUGCON, /* -debugcon */
Markus Armbrusteref0c4a02012-02-07 15:09:13 +01002482 DEV_GDB, /* -gdb, -s */
Alexander Graf3ef669e2013-01-24 12:18:52 +01002483 DEV_SCLP, /* s390 sclp */
Gerd Hoffmannbd3c9482009-07-15 13:59:26 +02002484 } type;
2485 const char *cmdline;
Markus Armbrusterd9a59542012-02-07 15:09:12 +01002486 Location loc;
Blue Swirl72cf2d42009-09-12 07:36:22 +00002487 QTAILQ_ENTRY(device_config) next;
Gerd Hoffmannbd3c9482009-07-15 13:59:26 +02002488};
Blue Swirl4fdcac02012-10-28 11:04:47 +00002489
2490static QTAILQ_HEAD(, device_config) device_configs =
2491 QTAILQ_HEAD_INITIALIZER(device_configs);
Gerd Hoffmannbd3c9482009-07-15 13:59:26 +02002492
2493static void add_device_config(int type, const char *cmdline)
2494{
2495 struct device_config *conf;
2496
Anthony Liguori7267c092011-08-20 22:09:37 -05002497 conf = g_malloc0(sizeof(*conf));
Gerd Hoffmannbd3c9482009-07-15 13:59:26 +02002498 conf->type = type;
2499 conf->cmdline = cmdline;
Markus Armbrusterd9a59542012-02-07 15:09:12 +01002500 loc_save(&conf->loc);
Blue Swirl72cf2d42009-09-12 07:36:22 +00002501 QTAILQ_INSERT_TAIL(&device_configs, conf, next);
Gerd Hoffmannbd3c9482009-07-15 13:59:26 +02002502}
2503
2504static int foreach_device_config(int type, int (*func)(const char *cmdline))
2505{
2506 struct device_config *conf;
2507 int rc;
2508
Blue Swirl72cf2d42009-09-12 07:36:22 +00002509 QTAILQ_FOREACH(conf, &device_configs, next) {
Gerd Hoffmannbd3c9482009-07-15 13:59:26 +02002510 if (conf->type != type)
2511 continue;
Markus Armbrusterd9a59542012-02-07 15:09:12 +01002512 loc_push_restore(&conf->loc);
Gerd Hoffmannbd3c9482009-07-15 13:59:26 +02002513 rc = func(conf->cmdline);
Markus Armbrusterd9a59542012-02-07 15:09:12 +01002514 loc_pop(&conf->loc);
Gonglei28de2f82014-08-11 21:00:57 +08002515 if (rc) {
Gerd Hoffmannbd3c9482009-07-15 13:59:26 +02002516 return rc;
Gonglei28de2f82014-08-11 21:00:57 +08002517 }
Gerd Hoffmannbd3c9482009-07-15 13:59:26 +02002518 }
2519 return 0;
2520}
2521
Gerd Hoffmann998bbd72009-12-08 13:11:41 +01002522static int serial_parse(const char *devname)
2523{
2524 static int index = 0;
2525 char label[32];
2526
2527 if (strcmp(devname, "none") == 0)
2528 return 0;
2529 if (index == MAX_SERIAL_PORTS) {
2530 fprintf(stderr, "qemu: too many serial ports\n");
2531 exit(1);
2532 }
2533 snprintf(label, sizeof(label), "serial%d", index);
Anthony Liguori27143a42011-08-15 11:17:36 -05002534 serial_hds[index] = qemu_chr_new(label, devname, NULL);
Gerd Hoffmann998bbd72009-12-08 13:11:41 +01002535 if (!serial_hds[index]) {
Peter Maydell52d06132012-07-09 04:28:30 +00002536 fprintf(stderr, "qemu: could not connect serial device"
2537 " to character backend '%s'\n", devname);
Gerd Hoffmann998bbd72009-12-08 13:11:41 +01002538 return -1;
2539 }
2540 index++;
2541 return 0;
2542}
2543
Gerd Hoffmann6a5e8b02009-12-08 13:11:42 +01002544static int parallel_parse(const char *devname)
2545{
2546 static int index = 0;
2547 char label[32];
2548
2549 if (strcmp(devname, "none") == 0)
2550 return 0;
2551 if (index == MAX_PARALLEL_PORTS) {
2552 fprintf(stderr, "qemu: too many parallel ports\n");
2553 exit(1);
2554 }
2555 snprintf(label, sizeof(label), "parallel%d", index);
Anthony Liguori27143a42011-08-15 11:17:36 -05002556 parallel_hds[index] = qemu_chr_new(label, devname, NULL);
Gerd Hoffmann6a5e8b02009-12-08 13:11:42 +01002557 if (!parallel_hds[index]) {
Peter Maydell52d06132012-07-09 04:28:30 +00002558 fprintf(stderr, "qemu: could not connect parallel device"
2559 " to character backend '%s'\n", devname);
Gerd Hoffmann6a5e8b02009-12-08 13:11:42 +01002560 return -1;
2561 }
2562 index++;
2563 return 0;
2564}
2565
Gerd Hoffmannaee1b932009-12-08 13:11:53 +01002566static int virtcon_parse(const char *devname)
2567{
Gerd Hoffmann3329f072010-08-20 13:52:01 +02002568 QemuOptsList *device = qemu_find_opts("device");
Gerd Hoffmannaee1b932009-12-08 13:11:53 +01002569 static int index = 0;
2570 char label[32];
Amit Shah392ecf52010-01-21 16:19:23 +05302571 QemuOpts *bus_opts, *dev_opts;
Gerd Hoffmannaee1b932009-12-08 13:11:53 +01002572
2573 if (strcmp(devname, "none") == 0)
2574 return 0;
2575 if (index == MAX_VIRTIO_CONSOLES) {
2576 fprintf(stderr, "qemu: too many virtio consoles\n");
2577 exit(1);
2578 }
Amit Shah392ecf52010-01-21 16:19:23 +05302579
Peter Crosthwaite87ea75d2014-01-01 18:49:17 -08002580 bus_opts = qemu_opts_create(device, NULL, 0, &error_abort);
Anthony Liguorie87f7fc2012-02-06 11:07:18 -06002581 if (arch_type == QEMU_ARCH_S390X) {
2582 qemu_opt_set(bus_opts, "driver", "virtio-serial-s390");
2583 } else {
2584 qemu_opt_set(bus_opts, "driver", "virtio-serial-pci");
Laszlo Ersek4d8b3c62013-03-21 00:23:13 +01002585 }
Amit Shah392ecf52010-01-21 16:19:23 +05302586
Peter Crosthwaite87ea75d2014-01-01 18:49:17 -08002587 dev_opts = qemu_opts_create(device, NULL, 0, &error_abort);
Amit Shah392ecf52010-01-21 16:19:23 +05302588 qemu_opt_set(dev_opts, "driver", "virtconsole");
2589
Gerd Hoffmannaee1b932009-12-08 13:11:53 +01002590 snprintf(label, sizeof(label), "virtcon%d", index);
Anthony Liguori27143a42011-08-15 11:17:36 -05002591 virtcon_hds[index] = qemu_chr_new(label, devname, NULL);
Gerd Hoffmannaee1b932009-12-08 13:11:53 +01002592 if (!virtcon_hds[index]) {
Peter Maydell52d06132012-07-09 04:28:30 +00002593 fprintf(stderr, "qemu: could not connect virtio console"
2594 " to character backend '%s'\n", devname);
Gerd Hoffmannaee1b932009-12-08 13:11:53 +01002595 return -1;
2596 }
Amit Shah392ecf52010-01-21 16:19:23 +05302597 qemu_opt_set(dev_opts, "chardev", label);
2598
Gerd Hoffmannaee1b932009-12-08 13:11:53 +01002599 index++;
2600 return 0;
2601}
2602
Alexander Graf3ef669e2013-01-24 12:18:52 +01002603static int sclp_parse(const char *devname)
2604{
2605 QemuOptsList *device = qemu_find_opts("device");
2606 static int index = 0;
2607 char label[32];
2608 QemuOpts *dev_opts;
2609
2610 if (strcmp(devname, "none") == 0) {
2611 return 0;
2612 }
2613 if (index == MAX_SCLP_CONSOLES) {
2614 fprintf(stderr, "qemu: too many sclp consoles\n");
2615 exit(1);
2616 }
2617
2618 assert(arch_type == QEMU_ARCH_S390X);
2619
2620 dev_opts = qemu_opts_create(device, NULL, 0, NULL);
2621 qemu_opt_set(dev_opts, "driver", "sclpconsole");
2622
2623 snprintf(label, sizeof(label), "sclpcon%d", index);
2624 sclp_hds[index] = qemu_chr_new(label, devname, NULL);
2625 if (!sclp_hds[index]) {
2626 fprintf(stderr, "qemu: could not connect sclp console"
2627 " to character backend '%s'\n", devname);
2628 return -1;
2629 }
2630 qemu_opt_set(dev_opts, "chardev", label);
2631
2632 index++;
2633 return 0;
2634}
2635
H. Peter Anvinc9f398e2009-12-29 13:51:36 -08002636static int debugcon_parse(const char *devname)
Laszlo Ersek4d8b3c62013-03-21 00:23:13 +01002637{
H. Peter Anvinc9f398e2009-12-29 13:51:36 -08002638 QemuOpts *opts;
2639
Anthony Liguori27143a42011-08-15 11:17:36 -05002640 if (!qemu_chr_new("debugcon", devname, NULL)) {
H. Peter Anvinc9f398e2009-12-29 13:51:36 -08002641 exit(1);
2642 }
Luiz Capitulino8be7e7e2012-03-20 15:51:57 -03002643 opts = qemu_opts_create(qemu_find_opts("device"), "debugcon", 1, NULL);
H. Peter Anvinc9f398e2009-12-29 13:51:36 -08002644 if (!opts) {
2645 fprintf(stderr, "qemu: already have a debugcon device\n");
2646 exit(1);
2647 }
2648 qemu_opt_set(opts, "driver", "isa-debugcon");
2649 qemu_opt_set(opts, "chardev", "debugcon");
2650 return 0;
2651}
2652
Marcel Apfelbaum0056ae22014-03-05 19:30:47 +02002653static MachineClass *machine_parse(const char *name)
Jan Kiszka9052ea62011-07-23 12:38:37 +02002654{
Marcel Apfelbaum0056ae22014-03-05 19:30:47 +02002655 MachineClass *mc = NULL;
Marcel Apfelbaum261747f2014-03-05 19:30:46 +02002656 GSList *el, *machines = object_class_get_list(TYPE_MACHINE, false);
Jan Kiszka9052ea62011-07-23 12:38:37 +02002657
2658 if (name) {
Marcel Apfelbaum0056ae22014-03-05 19:30:47 +02002659 mc = find_machine(name);
Jan Kiszka9052ea62011-07-23 12:38:37 +02002660 }
Marcel Apfelbaum0056ae22014-03-05 19:30:47 +02002661 if (mc) {
2662 return mc;
Jan Kiszka9052ea62011-07-23 12:38:37 +02002663 }
Miroslav Rezanina025172d2014-03-14 13:06:54 +01002664 if (name && !is_help_option(name)) {
2665 error_report("Unsupported machine type");
2666 error_printf("Use -machine help to list supported machines!\n");
2667 } else {
2668 printf("Supported machines are:\n");
2669 for (el = machines; el; el = el->next) {
2670 MachineClass *mc = el->data;
Marcel Apfelbaum958db902014-04-09 20:34:53 +03002671 if (mc->alias) {
2672 printf("%-20s %s (alias of %s)\n", mc->alias, mc->desc, mc->name);
Miroslav Rezanina025172d2014-03-14 13:06:54 +01002673 }
Marcel Apfelbaum958db902014-04-09 20:34:53 +03002674 printf("%-20s %s%s\n", mc->name, mc->desc,
2675 mc->is_default ? " (default)" : "");
Jan Kiszka9052ea62011-07-23 12:38:37 +02002676 }
Jan Kiszka9052ea62011-07-23 12:38:37 +02002677 }
Marcel Apfelbaum261747f2014-03-05 19:30:46 +02002678
2679 g_slist_free(machines);
Peter Maydellc8057f92012-08-02 13:45:54 +01002680 exit(!name || !is_help_option(name));
Jan Kiszka9052ea62011-07-23 12:38:37 +02002681}
2682
Marcel Apfelbaumf1e29872014-04-09 20:34:52 +03002683static int tcg_init(MachineClass *mc)
Anthony PERARD303d4e82010-09-21 20:05:31 +01002684{
Jan Kiszkad5ab9712011-08-02 16:10:21 +02002685 tcg_exec_init(tcg_tb_size * 1024 * 1024);
Anthony PERARD303d4e82010-09-21 20:05:31 +01002686 return 0;
2687}
2688
2689static struct {
2690 const char *opt_name;
2691 const char *name;
2692 int (*available)(void);
Marcel Apfelbaumf1e29872014-04-09 20:34:52 +03002693 int (*init)(MachineClass *mc);
liguangd5286af2013-01-24 13:03:27 +08002694 bool *allowed;
Anthony PERARD303d4e82010-09-21 20:05:31 +01002695} accel_list[] = {
2696 { "tcg", "tcg", tcg_available, tcg_init, &tcg_allowed },
Anthony PERARD3285cf42010-08-19 12:27:56 +01002697 { "xen", "Xen", xen_available, xen_init, &xen_allowed },
Anthony PERARD303d4e82010-09-21 20:05:31 +01002698 { "kvm", "KVM", kvm_available, kvm_init, &kvm_allowed },
Paolo Bonzinid4fce242013-10-18 13:51:11 +02002699 { "qtest", "QTest", qtest_available, qtest_init_accel, &qtest_allowed },
Anthony PERARD303d4e82010-09-21 20:05:31 +01002700};
2701
Marcel Apfelbaumf1e29872014-04-09 20:34:52 +03002702static int configure_accelerator(MachineClass *mc)
Anthony PERARD303d4e82010-09-21 20:05:31 +01002703{
Markus Armbruster36ad0e92013-07-04 15:09:20 +02002704 const char *p;
Anthony PERARD303d4e82010-09-21 20:05:31 +01002705 char buf[10];
2706 int i, ret;
liguang217e21b2013-01-24 13:03:26 +08002707 bool accel_initialised = false;
2708 bool init_failed = false;
Anthony PERARD303d4e82010-09-21 20:05:31 +01002709
Markus Armbruster36ad0e92013-07-04 15:09:20 +02002710 p = qemu_opt_get(qemu_get_machine_opts(), "accel");
Anthony PERARD303d4e82010-09-21 20:05:31 +01002711 if (p == NULL) {
2712 /* Use the default "accelerator", tcg */
2713 p = "tcg";
2714 }
2715
Peter Maydell1b785a92012-02-07 20:57:27 +00002716 while (!accel_initialised && *p != '\0') {
Anthony PERARD303d4e82010-09-21 20:05:31 +01002717 if (*p == ':') {
2718 p++;
2719 }
2720 p = get_opt_name(buf, sizeof (buf), p, ':');
2721 for (i = 0; i < ARRAY_SIZE(accel_list); i++) {
2722 if (strcmp(accel_list[i].opt_name, buf) == 0) {
liguange3c66d92013-01-24 13:03:25 +08002723 if (!accel_list[i].available()) {
2724 printf("%s not supported for this target\n",
2725 accel_list[i].name);
Chen Gangb321afb2014-04-04 17:39:33 +08002726 break;
liguange3c66d92013-01-24 13:03:25 +08002727 }
liguangd5286af2013-01-24 13:03:27 +08002728 *(accel_list[i].allowed) = true;
Marcel Apfelbaumf1e29872014-04-09 20:34:52 +03002729 ret = accel_list[i].init(mc);
Anthony PERARD303d4e82010-09-21 20:05:31 +01002730 if (ret < 0) {
liguang217e21b2013-01-24 13:03:26 +08002731 init_failed = true;
liguange3c66d92013-01-24 13:03:25 +08002732 fprintf(stderr, "failed to initialize %s: %s\n",
2733 accel_list[i].name,
2734 strerror(-ret));
liguangd5286af2013-01-24 13:03:27 +08002735 *(accel_list[i].allowed) = false;
Anthony PERARD303d4e82010-09-21 20:05:31 +01002736 } else {
liguang217e21b2013-01-24 13:03:26 +08002737 accel_initialised = true;
Anthony PERARD303d4e82010-09-21 20:05:31 +01002738 }
2739 break;
2740 }
2741 }
2742 if (i == ARRAY_SIZE(accel_list)) {
2743 fprintf(stderr, "\"%s\" accelerator does not exist.\n", buf);
2744 }
2745 }
2746
Peter Maydell1b785a92012-02-07 20:57:27 +00002747 if (!accel_initialised) {
liguang217e21b2013-01-24 13:03:26 +08002748 if (!init_failed) {
2749 fprintf(stderr, "No accelerator found!\n");
2750 }
Anthony PERARD303d4e82010-09-21 20:05:31 +01002751 exit(1);
2752 }
2753
2754 if (init_failed) {
2755 fprintf(stderr, "Back to %s accelerator.\n", accel_list[i].name);
2756 }
2757
Peter Maydell1b785a92012-02-07 20:57:27 +00002758 return !accel_initialised;
Anthony PERARD303d4e82010-09-21 20:05:31 +01002759}
2760
Gerd Hoffmannfd42dee2010-06-04 14:08:07 +02002761void qemu_add_exit_notifier(Notifier *notify)
2762{
2763 notifier_list_add(&exit_notifiers, notify);
2764}
2765
2766void qemu_remove_exit_notifier(Notifier *notify)
2767{
Paolo Bonzini31552522012-01-13 17:34:01 +01002768 notifier_remove(notify);
Gerd Hoffmannfd42dee2010-06-04 14:08:07 +02002769}
2770
2771static void qemu_run_exit_notifiers(void)
2772{
Jan Kiszka9e8dd452011-06-20 14:06:26 +02002773 notifier_list_notify(&exit_notifiers, NULL);
Gerd Hoffmannfd42dee2010-06-04 14:08:07 +02002774}
2775
Gleb Natapov4cab9462010-12-08 13:35:08 +02002776void qemu_add_machine_init_done_notifier(Notifier *notify)
2777{
2778 notifier_list_add(&machine_init_done_notifiers, notify);
2779}
2780
2781static void qemu_run_machine_init_done_notifiers(void)
2782{
Jan Kiszka9e8dd452011-06-20 14:06:26 +02002783 notifier_list_notify(&machine_init_done_notifiers, NULL);
Gleb Natapov4cab9462010-12-08 13:35:08 +02002784}
2785
Anthony Liguori6530a972010-01-22 09:18:06 -06002786static const QEMUOption *lookup_opt(int argc, char **argv,
2787 const char **poptarg, int *poptind)
2788{
2789 const QEMUOption *popt;
2790 int optind = *poptind;
2791 char *r = argv[optind];
2792 const char *optarg;
2793
Markus Armbruster0f0bc3f2010-02-18 20:13:51 +01002794 loc_set_cmdline(argv, optind, 1);
Anthony Liguori6530a972010-01-22 09:18:06 -06002795 optind++;
2796 /* Treat --foo the same as -foo. */
2797 if (r[1] == '-')
2798 r++;
2799 popt = qemu_options;
2800 for(;;) {
2801 if (!popt->name) {
Markus Armbruster0f0bc3f2010-02-18 20:13:51 +01002802 error_report("invalid option");
Anthony Liguori6530a972010-01-22 09:18:06 -06002803 exit(1);
2804 }
2805 if (!strcmp(popt->name, r + 1))
2806 break;
2807 popt++;
2808 }
2809 if (popt->flags & HAS_ARG) {
2810 if (optind >= argc) {
Markus Armbruster0f0bc3f2010-02-18 20:13:51 +01002811 error_report("requires an argument");
Anthony Liguori6530a972010-01-22 09:18:06 -06002812 exit(1);
2813 }
2814 optarg = argv[optind++];
Markus Armbruster0f0bc3f2010-02-18 20:13:51 +01002815 loc_set_cmdline(argv, optind - 2, 2);
Anthony Liguori6530a972010-01-22 09:18:06 -06002816 } else {
2817 optarg = NULL;
2818 }
2819
2820 *poptarg = optarg;
2821 *poptind = optind;
2822
2823 return popt;
2824}
2825
Anthony Liguori07501122011-08-20 22:38:31 -05002826static gpointer malloc_and_trace(gsize n_bytes)
2827{
2828 void *ptr = malloc(n_bytes);
Frediano Ziglioa74cd8c2011-08-31 09:25:35 +02002829 trace_g_malloc(n_bytes, ptr);
Anthony Liguori07501122011-08-20 22:38:31 -05002830 return ptr;
2831}
2832
2833static gpointer realloc_and_trace(gpointer mem, gsize n_bytes)
2834{
2835 void *ptr = realloc(mem, n_bytes);
Frediano Ziglioa74cd8c2011-08-31 09:25:35 +02002836 trace_g_realloc(mem, n_bytes, ptr);
Anthony Liguori07501122011-08-20 22:38:31 -05002837 return ptr;
2838}
2839
2840static void free_and_trace(gpointer mem)
2841{
Frediano Ziglioa74cd8c2011-08-31 09:25:35 +02002842 trace_g_free(mem);
Anthony Liguori07501122011-08-20 22:38:31 -05002843 free(mem);
2844}
2845
Andreas Färberd2659e22014-07-23 16:16:26 +02002846static int machine_set_property(const char *name, const char *value,
2847 void *opaque)
Anthony Liguori68d98d32012-06-25 14:36:33 -05002848{
2849 Object *obj = OBJECT(opaque);
2850 StringInputVisitor *siv;
2851 Error *local_err = NULL;
Marcel Apfelbaumb0ddb8b2014-07-18 19:32:37 +03002852 char *c, *qom_name;
Anthony Liguori68d98d32012-06-25 14:36:33 -05002853
Andreas Färberd2659e22014-07-23 16:16:26 +02002854 if (strcmp(name, "type") == 0) {
Anthony Liguori68d98d32012-06-25 14:36:33 -05002855 return 0;
2856 }
2857
Marcel Apfelbaumb0ddb8b2014-07-18 19:32:37 +03002858 qom_name = g_strdup(name);
2859 c = qom_name;
2860 while (*c++) {
2861 if (*c == '_') {
2862 *c = '-';
2863 }
2864 }
2865
Anthony Liguori68d98d32012-06-25 14:36:33 -05002866 siv = string_input_visitor_new(value);
Marcel Apfelbaumb0ddb8b2014-07-18 19:32:37 +03002867 object_property_set(obj, string_input_get_visitor(siv), qom_name, &local_err);
Anthony Liguori68d98d32012-06-25 14:36:33 -05002868 string_input_visitor_cleanup(siv);
Marcel Apfelbaumb0ddb8b2014-07-18 19:32:37 +03002869 g_free(qom_name);
Anthony Liguori68d98d32012-06-25 14:36:33 -05002870
2871 if (local_err) {
2872 qerror_report_err(local_err);
2873 error_free(local_err);
2874 return -1;
2875 }
2876
2877 return 0;
2878}
2879
2880static int object_create(QemuOpts *opts, void *opaque)
2881{
Paolo Bonzinic4090f82014-06-10 19:15:15 +08002882 Error *err = NULL;
2883 char *type = NULL;
2884 char *id = NULL;
2885 void *dummy = NULL;
2886 OptsVisitor *ov;
2887 QDict *pdict;
Anthony Liguori68d98d32012-06-25 14:36:33 -05002888
Paolo Bonzinic4090f82014-06-10 19:15:15 +08002889 ov = opts_visitor_new(opts);
2890 pdict = qemu_opts_to_qdict(opts, NULL);
Anthony Liguori68d98d32012-06-25 14:36:33 -05002891
Paolo Bonzinic4090f82014-06-10 19:15:15 +08002892 visit_start_struct(opts_get_visitor(ov), &dummy, NULL, NULL, 0, &err);
2893 if (err) {
Igor Mammedov269e09f2014-01-16 17:34:38 +01002894 goto out;
2895 }
2896
Paolo Bonzinic4090f82014-06-10 19:15:15 +08002897 qdict_del(pdict, "qom-type");
2898 visit_type_str(opts_get_visitor(ov), &type, "qom-type", &err);
2899 if (err) {
Igor Mammedova790f4e2014-06-02 15:24:59 +02002900 goto out;
2901 }
Paolo Bonzinic4090f82014-06-10 19:15:15 +08002902
2903 qdict_del(pdict, "id");
2904 visit_type_str(opts_get_visitor(ov), &id, "id", &err);
2905 if (err) {
2906 goto out;
2907 }
2908
2909 object_add(type, id, pdict, opts_get_visitor(ov), &err);
2910 if (err) {
2911 goto out;
2912 }
2913 visit_end_struct(opts_get_visitor(ov), &err);
2914 if (err) {
2915 qmp_object_del(id, NULL);
2916 }
2917
Igor Mammedov269e09f2014-01-16 17:34:38 +01002918out:
Paolo Bonzinic4090f82014-06-10 19:15:15 +08002919 opts_visitor_cleanup(ov);
2920
2921 QDECREF(pdict);
2922 g_free(id);
2923 g_free(type);
2924 g_free(dummy);
2925 if (err) {
2926 qerror_report_err(err);
Hu Tao3a9cbfe2014-08-15 18:13:59 +08002927 error_free(err);
Igor Mammedov90e9cf22014-01-16 17:34:37 +01002928 return -1;
2929 }
Anthony Liguori68d98d32012-06-25 14:36:33 -05002930 return 0;
2931}
2932
malc902b3d52008-12-10 19:18:40 +00002933int main(int argc, char **argv, char **envp)
bellard0824d6f2003-06-24 13:42:40 +00002934{
thse4bcb142007-12-02 04:51:10 +00002935 int i;
Eduardo Habkostda1fcfd2010-04-06 19:22:07 -03002936 int snapshot, linux_boot;
bellard7f7f9872003-10-30 01:11:23 +00002937 const char *initrd_filename;
bellarda20dd502003-09-30 21:07:02 +00002938 const char *kernel_filename, *kernel_cmdline;
Markus Armbrustere3fdc532013-10-01 13:47:22 +02002939 const char *boot_order;
aliguori3023f332009-01-16 19:04:14 +00002940 DisplayState *ds;
malc9f227bc2012-08-27 18:33:22 +04002941 int cyls, heads, secs, translation;
Sebastian Tanase1ad95802014-07-25 11:56:28 +02002942 QemuOpts *hda_opts = NULL, *opts, *machine_opts, *icount_opts = NULL;
Gerd Hoffmann03b0ba72010-08-20 13:52:02 +02002943 QemuOptsList *olist;
bellardcd6f1162004-05-13 22:02:20 +00002944 int optind;
Anthony Liguori6530a972010-01-22 09:18:06 -06002945 const char *optarg;
bellardd63d3072004-10-03 13:29:03 +00002946 const char *loadvm = NULL;
Marcel Apfelbaum0056ae22014-03-05 19:30:47 +02002947 MachineClass *machine_class;
j_mayer94fc95c2007-03-05 19:44:02 +00002948 const char *cpu_model;
Mark Wud44229c2014-03-10 22:37:40 +08002949 const char *vga_model = NULL;
Paolo Bonzinid4fce242013-10-18 13:51:11 +02002950 const char *qtest_chrdev = NULL;
2951 const char *qtest_log = NULL;
ths93815bc2007-03-19 15:58:31 +00002952 const char *pid_file = NULL;
aliguori5bb79102008-10-13 03:12:02 +00002953 const char *incoming = NULL;
Jes Sorensen821601e2011-03-16 13:33:36 +01002954#ifdef CONFIG_VNC
Anthony Liguori993fbfd2009-05-21 16:54:00 -05002955 int show_vnc_port = 0;
Jes Sorensen821601e2011-03-16 13:33:36 +01002956#endif
Eduardo Habkost3ed2d9e2012-05-02 13:07:28 -03002957 bool defconfig = true;
Eduardo Habkostf29a5612012-05-02 13:07:29 -03002958 bool userconfig = true;
Matthew Fernandezc235d732011-06-07 16:32:40 +00002959 const char *log_mask = NULL;
2960 const char *log_file = NULL;
Anthony Liguori07501122011-08-20 22:38:31 -05002961 GMemVTable mem_trace = {
2962 .malloc = malloc_and_trace,
2963 .realloc = realloc_and_trace,
2964 .free = free_and_trace,
2965 };
LluĂ­s23d15e82011-08-31 20:31:31 +02002966 const char *trace_events = NULL;
LluĂ­se4858972011-08-31 20:31:03 +02002967 const char *trace_file = NULL;
Igor Mammedov6e1d3c12013-11-27 01:27:35 +01002968 const ram_addr_t default_ram_size = (ram_addr_t)DEFAULT_RAM_SIZE *
2969 1024 * 1024;
Igor Mammedovc270fb92014-06-02 15:25:02 +02002970 ram_addr_t maxram_size = default_ram_size;
2971 uint64_t ram_slots = 0;
Amit Shahabfd9ce2014-06-20 18:56:08 +05302972 FILE *vmstate_dump_file = NULL;
Chrysostomos Nanakos2f78e492014-09-18 14:30:49 +03002973 Error *main_loop_err = NULL;
Stefan Hajnoczi0b5538c2011-02-26 18:38:39 +00002974
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
Anthony Liguori07501122011-08-20 22:38:31 -05002979 g_mem_set_vtable(&mem_trace);
2980
Andreas Färber1b71f7c2012-03-04 21:32:35 +01002981 module_call_init(MODULE_INIT_QOM);
2982
Paolo Bonzini4d454572012-11-26 16:03:42 +01002983 qemu_add_opts(&qemu_drive_opts);
Amos Kong968854c2013-11-09 12:15:47 +08002984 qemu_add_drive_opts(&qemu_legacy_drive_opts);
2985 qemu_add_drive_opts(&qemu_common_drive_opts);
2986 qemu_add_drive_opts(&qemu_drive_opts);
Paolo Bonzini4d454572012-11-26 16:03:42 +01002987 qemu_add_opts(&qemu_chardev_opts);
2988 qemu_add_opts(&qemu_device_opts);
2989 qemu_add_opts(&qemu_netdev_opts);
2990 qemu_add_opts(&qemu_net_opts);
2991 qemu_add_opts(&qemu_rtc_opts);
2992 qemu_add_opts(&qemu_global_opts);
2993 qemu_add_opts(&qemu_mon_opts);
2994 qemu_add_opts(&qemu_trace_opts);
2995 qemu_add_opts(&qemu_option_rom_opts);
2996 qemu_add_opts(&qemu_machine_opts);
Igor Mammedov6e1d3c12013-11-27 01:27:35 +01002997 qemu_add_opts(&qemu_mem_opts);
Michael Tokarev12b7f572013-06-24 15:06:52 +04002998 qemu_add_opts(&qemu_smp_opts);
Paolo Bonzini4d454572012-11-26 16:03:42 +01002999 qemu_add_opts(&qemu_boot_opts);
3000 qemu_add_opts(&qemu_sandbox_opts);
3001 qemu_add_opts(&qemu_add_fd_opts);
3002 qemu_add_opts(&qemu_object_opts);
Stefan Bergerd1a0cf72013-02-27 12:47:49 -05003003 qemu_add_opts(&qemu_tpmdev_opts);
Satoru Moriya888a6bc2013-04-19 16:42:06 +02003004 qemu_add_opts(&qemu_realtime_opts);
Seiji Aguchi5e2ac512013-07-03 23:02:46 -04003005 qemu_add_opts(&qemu_msg_opts);
Dr. David Alan Gilbert5d12f962014-01-30 10:20:30 +00003006 qemu_add_opts(&qemu_name_opts);
Wanlong Gao00421092014-05-14 17:43:08 +08003007 qemu_add_opts(&qemu_numa_opts);
Sebastian Tanase1ad95802014-07-25 11:56:28 +02003008 qemu_add_opts(&qemu_icount_opts);
Paolo Bonzini4d454572012-11-26 16:03:42 +01003009
Luiz Capitulino5db9d4d2011-09-05 16:36:31 -03003010 runstate_init();
3011
Alex Bligh884f17c2013-08-21 16:03:04 +01003012 rtc_clock = QEMU_CLOCK_HOST;
Jan Kiszka68752042009-09-15 13:36:04 +02003013
Blue Swirl72cf2d42009-09-12 07:36:22 +00003014 QLIST_INIT (&vm_change_state_head);
Jes Sorensenfe98ac12010-06-10 11:42:21 +02003015 os_setup_early_signal_handling();
bellardbe995c22006-06-25 16:25:21 +00003016
Anthony Liguorif80f9ec2009-05-20 18:38:09 -05003017 module_call_init(MODULE_INIT_MACHINE);
Marcel Apfelbaum0056ae22014-03-05 19:30:47 +02003018 machine_class = find_default_machine();
j_mayer94fc95c2007-03-05 19:44:02 +00003019 cpu_model = NULL;
Igor Mammedov6e1d3c12013-11-27 01:27:35 +01003020 ram_size = default_ram_size;
bellard33e39632003-07-06 17:15:21 +00003021 snapshot = 0;
malc9f227bc2012-08-27 18:33:22 +04003022 cyls = heads = secs = 0;
3023 translation = BIOS_ATA_TRANSLATION_AUTO;
bellardc4b1fcc2004-03-14 21:44:30 +00003024
aliguori268a3622009-04-21 22:30:27 +00003025 for (i = 0; i < MAX_NODES; i++) {
Wanlong Gao8c859012014-05-14 17:43:07 +08003026 numa_info[i].node_mem = 0;
Eduardo Habkost1af878e2014-06-26 18:33:18 -03003027 numa_info[i].present = false;
Wanlong Gao8c859012014-05-14 17:43:07 +08003028 bitmap_zero(numa_info[i].node_cpu, MAX_CPUMASK_BITS);
aliguori268a3622009-04-21 22:30:27 +00003029 }
3030
aliguori268a3622009-04-21 22:30:27 +00003031 nb_numa_nodes = 0;
Eduardo Habkost1af878e2014-06-26 18:33:18 -03003032 max_numa_nodeid = 0;
bellard7c9d8e02005-11-15 22:16:05 +00003033 nb_nics = 0;
ths3b46e622007-09-17 08:09:54 +00003034
Peter Lieven142c6b12013-03-21 13:07:10 +01003035 bdrv_init_with_whitelist();
3036
Kewei Yu2c02d1a2013-12-31 18:36:08 +08003037 autostart = 1;
blueswir141bd6392008-10-05 09:56:21 +00003038
Anthony Liguori292444c2010-01-21 10:57:58 -06003039 /* first pass of option parsing */
3040 optind = 1;
3041 while (optind < argc) {
3042 if (argv[optind][0] != '-') {
3043 /* disk image */
Anthony Liguori28e68d62010-01-27 10:46:00 -06003044 optind++;
Anthony Liguori292444c2010-01-21 10:57:58 -06003045 } else {
3046 const QEMUOption *popt;
3047
3048 popt = lookup_opt(argc, argv, &optarg, &optind);
3049 switch (popt->index) {
3050 case QEMU_OPTION_nodefconfig:
Eduardo Habkost3ed2d9e2012-05-02 13:07:28 -03003051 defconfig = false;
Anthony Liguori292444c2010-01-21 10:57:58 -06003052 break;
Eduardo Habkostf29a5612012-05-02 13:07:29 -03003053 case QEMU_OPTION_nouserconfig:
3054 userconfig = false;
3055 break;
Anthony Liguori292444c2010-01-21 10:57:58 -06003056 }
3057 }
3058 }
3059
3060 if (defconfig) {
Kevin Wolfdcfb0932010-03-05 17:25:55 +01003061 int ret;
Eduardo Habkostf29a5612012-05-02 13:07:29 -03003062 ret = qemu_read_default_config_files(userconfig);
Eduardo Habkostb5a8fe52012-05-02 13:07:25 -03003063 if (ret < 0) {
Kevin Wolfdcfb0932010-03-05 17:25:55 +01003064 exit(1);
Anthony Liguori292444c2010-01-21 10:57:58 -06003065 }
3066 }
3067
3068 /* second pass of option parsing */
bellardcd6f1162004-05-13 22:02:20 +00003069 optind = 1;
bellard0824d6f2003-06-24 13:42:40 +00003070 for(;;) {
bellardcd6f1162004-05-13 22:02:20 +00003071 if (optind >= argc)
bellard0824d6f2003-06-24 13:42:40 +00003072 break;
Anthony Liguori6530a972010-01-22 09:18:06 -06003073 if (argv[optind][0] != '-') {
malc9f227bc2012-08-27 18:33:22 +04003074 hda_opts = drive_add(IF_DEFAULT, 0, argv[optind++], HD_OPTS);
bellardcd6f1162004-05-13 22:02:20 +00003075 } else {
3076 const QEMUOption *popt;
3077
Anthony Liguori6530a972010-01-22 09:18:06 -06003078 popt = lookup_opt(argc, argv, &optarg, &optind);
Blue Swirlad960902010-03-29 19:23:52 +00003079 if (!(popt->arch_mask & arch_type)) {
3080 printf("Option %s not supported for this target\n", popt->name);
3081 exit(1);
3082 }
bellardcd6f1162004-05-13 22:02:20 +00003083 switch(popt->index) {
bellardcc1daa42005-06-05 14:49:17 +00003084 case QEMU_OPTION_M:
Marcel Apfelbaum0056ae22014-03-05 19:30:47 +02003085 machine_class = machine_parse(optarg);
bellardcc1daa42005-06-05 14:49:17 +00003086 break;
Jan Kiszkae43d5942012-10-05 14:51:40 -03003087 case QEMU_OPTION_no_kvm_irqchip: {
3088 olist = qemu_find_opts("machine");
3089 qemu_opts_parse(olist, "kernel_irqchip=off", 0);
3090 break;
3091 }
j_mayer94fc95c2007-03-05 19:44:02 +00003092 case QEMU_OPTION_cpu:
3093 /* hw initialization will check this */
Eduardo Habkostecf40be2012-03-09 16:19:07 -03003094 cpu_model = optarg;
j_mayer94fc95c2007-03-05 19:44:02 +00003095 break;
bellardcd6f1162004-05-13 22:02:20 +00003096 case QEMU_OPTION_hda:
malc9f227bc2012-08-27 18:33:22 +04003097 {
3098 char buf[256];
3099 if (cyls == 0)
3100 snprintf(buf, sizeof(buf), "%s", HD_OPTS);
3101 else
3102 snprintf(buf, sizeof(buf),
3103 "%s,cyls=%d,heads=%d,secs=%d%s",
3104 HD_OPTS , cyls, heads, secs,
3105 translation == BIOS_ATA_TRANSLATION_LBA ?
3106 ",trans=lba" :
3107 translation == BIOS_ATA_TRANSLATION_NONE ?
3108 ",trans=none" : "");
3109 drive_add(IF_DEFAULT, 0, optarg, buf);
3110 break;
3111 }
bellardcd6f1162004-05-13 22:02:20 +00003112 case QEMU_OPTION_hdb:
bellardcc1daa42005-06-05 14:49:17 +00003113 case QEMU_OPTION_hdc:
3114 case QEMU_OPTION_hdd:
Markus Armbruster2292dda2011-01-28 11:21:41 +01003115 drive_add(IF_DEFAULT, popt->index - QEMU_OPTION_hda, optarg,
3116 HD_OPTS);
bellardfc01f7e2003-06-30 10:03:06 +00003117 break;
thse4bcb142007-12-02 04:51:10 +00003118 case QEMU_OPTION_drive:
Michael Tokareve2982c32011-03-30 16:31:05 +04003119 if (drive_def(optarg) == NULL) {
3120 exit(1);
3121 }
thse4bcb142007-12-02 04:51:10 +00003122 break;
Gerd Hoffmannd058fe02009-07-31 12:25:36 +02003123 case QEMU_OPTION_set:
3124 if (qemu_set_option(optarg) != 0)
3125 exit(1);
3126 break;
Gerd Hoffmannd0fef6f2009-12-08 13:11:34 +01003127 case QEMU_OPTION_global:
3128 if (qemu_global_option(optarg) != 0)
3129 exit(1);
3130 break;
balrog3e3d5812007-04-30 02:09:25 +00003131 case QEMU_OPTION_mtdblock:
Markus Armbruster2292dda2011-01-28 11:21:41 +01003132 drive_add(IF_MTD, -1, optarg, MTD_OPTS);
balrog3e3d5812007-04-30 02:09:25 +00003133 break;
pbrooka1bb27b2007-04-06 16:49:48 +00003134 case QEMU_OPTION_sd:
Peter Crosthwaite80f4d9f2013-02-28 18:23:14 +00003135 drive_add(IF_SD, -1, optarg, SD_OPTS);
pbrooka1bb27b2007-04-06 16:49:48 +00003136 break;
j_mayer86f55662007-04-24 06:52:59 +00003137 case QEMU_OPTION_pflash:
Markus Armbruster2292dda2011-01-28 11:21:41 +01003138 drive_add(IF_PFLASH, -1, optarg, PFLASH_OPTS);
j_mayer86f55662007-04-24 06:52:59 +00003139 break;
bellardcd6f1162004-05-13 22:02:20 +00003140 case QEMU_OPTION_snapshot:
bellard33e39632003-07-06 17:15:21 +00003141 snapshot = 1;
3142 break;
bellardcd6f1162004-05-13 22:02:20 +00003143 case QEMU_OPTION_hdachs:
bellard330d0412003-07-26 18:11:40 +00003144 {
bellard330d0412003-07-26 18:11:40 +00003145 const char *p;
3146 p = optarg;
3147 cyls = strtol(p, (char **)&p, 0);
bellard46d47672004-11-16 01:45:27 +00003148 if (cyls < 1 || cyls > 16383)
3149 goto chs_fail;
bellard330d0412003-07-26 18:11:40 +00003150 if (*p != ',')
3151 goto chs_fail;
3152 p++;
3153 heads = strtol(p, (char **)&p, 0);
bellard46d47672004-11-16 01:45:27 +00003154 if (heads < 1 || heads > 16)
3155 goto chs_fail;
bellard330d0412003-07-26 18:11:40 +00003156 if (*p != ',')
3157 goto chs_fail;
3158 p++;
3159 secs = strtol(p, (char **)&p, 0);
bellard46d47672004-11-16 01:45:27 +00003160 if (secs < 1 || secs > 63)
3161 goto chs_fail;
3162 if (*p == ',') {
3163 p++;
Paolo Bonzinif31c41f2014-02-08 11:01:54 +01003164 if (!strcmp(p, "large")) {
3165 translation = BIOS_ATA_TRANSLATION_LARGE;
3166 } else if (!strcmp(p, "rechs")) {
3167 translation = BIOS_ATA_TRANSLATION_RECHS;
3168 } else if (!strcmp(p, "none")) {
bellard46d47672004-11-16 01:45:27 +00003169 translation = BIOS_ATA_TRANSLATION_NONE;
Paolo Bonzinif31c41f2014-02-08 11:01:54 +01003170 } else if (!strcmp(p, "lba")) {
bellard46d47672004-11-16 01:45:27 +00003171 translation = BIOS_ATA_TRANSLATION_LBA;
Paolo Bonzinif31c41f2014-02-08 11:01:54 +01003172 } else if (!strcmp(p, "auto")) {
bellard46d47672004-11-16 01:45:27 +00003173 translation = BIOS_ATA_TRANSLATION_AUTO;
Paolo Bonzinif31c41f2014-02-08 11:01:54 +01003174 } else {
bellard46d47672004-11-16 01:45:27 +00003175 goto chs_fail;
Paolo Bonzinif31c41f2014-02-08 11:01:54 +01003176 }
bellard46d47672004-11-16 01:45:27 +00003177 } else if (*p != '\0') {
bellardc4b1fcc2004-03-14 21:44:30 +00003178 chs_fail:
bellard46d47672004-11-16 01:45:27 +00003179 fprintf(stderr, "qemu: invalid physical CHS format\n");
3180 exit(1);
bellardc4b1fcc2004-03-14 21:44:30 +00003181 }
malc9f227bc2012-08-27 18:33:22 +04003182 if (hda_opts != NULL) {
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +02003183 char num[16];
3184 snprintf(num, sizeof(num), "%d", cyls);
3185 qemu_opt_set(hda_opts, "cyls", num);
3186 snprintf(num, sizeof(num), "%d", heads);
3187 qemu_opt_set(hda_opts, "heads", num);
3188 snprintf(num, sizeof(num), "%d", secs);
3189 qemu_opt_set(hda_opts, "secs", num);
Paolo Bonzinif31c41f2014-02-08 11:01:54 +01003190 if (translation == BIOS_ATA_TRANSLATION_LARGE) {
3191 qemu_opt_set(hda_opts, "trans", "large");
3192 } else if (translation == BIOS_ATA_TRANSLATION_RECHS) {
3193 qemu_opt_set(hda_opts, "trans", "rechs");
3194 } else if (translation == BIOS_ATA_TRANSLATION_LBA) {
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +02003195 qemu_opt_set(hda_opts, "trans", "lba");
Paolo Bonzinif31c41f2014-02-08 11:01:54 +01003196 } else if (translation == BIOS_ATA_TRANSLATION_NONE) {
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +02003197 qemu_opt_set(hda_opts, "trans", "none");
Paolo Bonzinif31c41f2014-02-08 11:01:54 +01003198 }
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +02003199 }
bellard330d0412003-07-26 18:11:40 +00003200 }
3201 break;
aliguori268a3622009-04-21 22:30:27 +00003202 case QEMU_OPTION_numa:
Wanlong Gao00421092014-05-14 17:43:08 +08003203 opts = qemu_opts_parse(qemu_find_opts("numa"), optarg, 1);
3204 if (!opts) {
3205 exit(1);
3206 }
aliguori268a3622009-04-21 22:30:27 +00003207 break;
Jes Sorensen1472a952011-03-16 13:33:31 +01003208 case QEMU_OPTION_display:
3209 display_type = select_display(optarg);
3210 break;
bellardcd6f1162004-05-13 22:02:20 +00003211 case QEMU_OPTION_nographic:
Anthony Liguori993fbfd2009-05-21 16:54:00 -05003212 display_type = DT_NOGRAPHIC;
bellarda20dd502003-09-30 21:07:02 +00003213 break;
balrog4d3b6f62008-02-10 16:33:14 +00003214 case QEMU_OPTION_curses:
Jes Sorensen47b05362011-03-16 13:33:35 +01003215#ifdef CONFIG_CURSES
Anthony Liguori993fbfd2009-05-21 16:54:00 -05003216 display_type = DT_CURSES;
Jes Sorensen47b05362011-03-16 13:33:35 +01003217#else
3218 fprintf(stderr, "Curses support is disabled\n");
3219 exit(1);
balrog4d3b6f62008-02-10 16:33:14 +00003220#endif
Jes Sorensen47b05362011-03-16 13:33:35 +01003221 break;
balroga171fe32007-04-30 01:48:07 +00003222 case QEMU_OPTION_portrait:
Vasily Khoruzhick93128052011-06-17 13:04:36 +03003223 graphic_rotate = 90;
3224 break;
3225 case QEMU_OPTION_rotate:
3226 graphic_rotate = strtol(optarg, (char **) &optarg, 10);
3227 if (graphic_rotate != 0 && graphic_rotate != 90 &&
3228 graphic_rotate != 180 && graphic_rotate != 270) {
3229 fprintf(stderr,
3230 "qemu: only 90, 180, 270 deg rotation is available\n");
3231 exit(1);
3232 }
balroga171fe32007-04-30 01:48:07 +00003233 break;
bellardcd6f1162004-05-13 22:02:20 +00003234 case QEMU_OPTION_kernel:
Peter Maydella0abe472012-02-08 05:41:39 +00003235 qemu_opts_set(qemu_find_opts("machine"), 0, "kernel", optarg);
3236 break;
3237 case QEMU_OPTION_initrd:
3238 qemu_opts_set(qemu_find_opts("machine"), 0, "initrd", optarg);
bellarda20dd502003-09-30 21:07:02 +00003239 break;
bellardcd6f1162004-05-13 22:02:20 +00003240 case QEMU_OPTION_append:
Peter Maydella0abe472012-02-08 05:41:39 +00003241 qemu_opts_set(qemu_find_opts("machine"), 0, "append", optarg);
bellard313aa562003-08-10 21:52:11 +00003242 break;
Grant Likely412beee2012-03-02 11:56:38 +00003243 case QEMU_OPTION_dtb:
3244 qemu_opts_set(qemu_find_opts("machine"), 0, "dtb", optarg);
3245 break;
bellardcd6f1162004-05-13 22:02:20 +00003246 case QEMU_OPTION_cdrom:
Markus Armbruster2292dda2011-01-28 11:21:41 +01003247 drive_add(IF_DEFAULT, 2, optarg, CDROM_OPTS);
bellard36b486b2003-11-11 13:36:08 +00003248 break;
bellardcd6f1162004-05-13 22:02:20 +00003249 case QEMU_OPTION_boot:
Markus Armbruster8281abd2013-06-14 13:15:03 +02003250 opts = qemu_opts_parse(qemu_find_opts("boot-opts"), optarg, 1);
3251 if (!opts) {
3252 exit(1);
bellard36b486b2003-11-11 13:36:08 +00003253 }
3254 break;
bellardcd6f1162004-05-13 22:02:20 +00003255 case QEMU_OPTION_fda:
bellardcd6f1162004-05-13 22:02:20 +00003256 case QEMU_OPTION_fdb:
Markus Armbruster2292dda2011-01-28 11:21:41 +01003257 drive_add(IF_FLOPPY, popt->index - QEMU_OPTION_fda,
3258 optarg, FD_OPTS);
bellardc45886d2004-01-05 00:02:06 +00003259 break;
bellard52ca8d62006-06-14 16:03:05 +00003260 case QEMU_OPTION_no_fd_bootchk:
3261 fd_bootchk = 0;
3262 break;
Mark McLoughlina1ea4582009-10-08 19:58:26 +01003263 case QEMU_OPTION_netdev:
Gerd Hoffmann3329f072010-08-20 13:52:01 +02003264 if (net_client_parse(qemu_find_opts("netdev"), optarg) == -1) {
Mark McLoughlina1ea4582009-10-08 19:58:26 +01003265 exit(1);
3266 }
3267 break;
bellard7c9d8e02005-11-15 22:16:05 +00003268 case QEMU_OPTION_net:
Gerd Hoffmann3329f072010-08-20 13:52:01 +02003269 if (net_client_parse(qemu_find_opts("net"), optarg) == -1) {
bellardc4b1fcc2004-03-14 21:44:30 +00003270 exit(1);
3271 }
bellard702c6512004-04-02 21:21:32 +00003272 break;
Ronnie Sahlbergf9dadc92012-01-26 09:39:02 +11003273#ifdef CONFIG_LIBISCSI
3274 case QEMU_OPTION_iscsi:
3275 opts = qemu_opts_parse(qemu_find_opts("iscsi"), optarg, 0);
3276 if (!opts) {
3277 exit(1);
3278 }
3279 break;
3280#endif
bellardc7f74642004-08-24 21:57:12 +00003281#ifdef CONFIG_SLIRP
3282 case QEMU_OPTION_tftp:
Jan Kiszkaad196a92009-06-24 14:42:28 +02003283 legacy_tftp_prefix = optarg;
bellard9bf05442004-08-25 22:12:49 +00003284 break;
ths47d5d012007-02-20 00:05:08 +00003285 case QEMU_OPTION_bootp:
Jan Kiszkaad196a92009-06-24 14:42:28 +02003286 legacy_bootp_filename = optarg;
ths47d5d012007-02-20 00:05:08 +00003287 break;
bellard9bf05442004-08-25 22:12:49 +00003288 case QEMU_OPTION_redir:
Markus Armbruster07527062009-10-06 12:16:57 +01003289 if (net_slirp_redir(optarg) < 0)
3290 exit(1);
bellard9bf05442004-08-25 22:12:49 +00003291 break;
bellardc7f74642004-08-24 21:57:12 +00003292#endif
balrogdc72ac12008-11-09 00:04:26 +00003293 case QEMU_OPTION_bt:
Gerd Hoffmannbd3c9482009-07-15 13:59:26 +02003294 add_device_config(DEV_BT, optarg);
balrogdc72ac12008-11-09 00:04:26 +00003295 break;
bellard1d14ffa2005-10-30 18:58:22 +00003296 case QEMU_OPTION_audio_help:
3297 AUD_help ();
3298 exit (0);
3299 break;
3300 case QEMU_OPTION_soundhw:
3301 select_soundhw (optarg);
3302 break;
bellardcd6f1162004-05-13 22:02:20 +00003303 case QEMU_OPTION_h:
ths15f82202007-06-29 23:26:08 +00003304 help(0);
bellardcd6f1162004-05-13 22:02:20 +00003305 break;
pbrook9bd7e6d2009-04-07 22:58:45 +00003306 case QEMU_OPTION_version:
3307 version();
3308 exit(0);
3309 break;
aurel3200f82b82008-04-27 21:12:55 +00003310 case QEMU_OPTION_m: {
Markus Armbrusterff961012012-08-15 13:12:19 +02003311 uint64_t sz;
Igor Mammedov6e1d3c12013-11-27 01:27:35 +01003312 const char *mem_str;
Igor Mammedovc270fb92014-06-02 15:25:02 +02003313 const char *maxmem_str, *slots_str;
aurel3200f82b82008-04-27 21:12:55 +00003314
Igor Mammedov6e1d3c12013-11-27 01:27:35 +01003315 opts = qemu_opts_parse(qemu_find_opts("memory"),
3316 optarg, 1);
3317 if (!opts) {
3318 exit(EXIT_FAILURE);
bellardcd6f1162004-05-13 22:02:20 +00003319 }
Igor Mammedov6e1d3c12013-11-27 01:27:35 +01003320
3321 mem_str = qemu_opt_get(opts, "size");
3322 if (!mem_str) {
3323 error_report("invalid -m option, missing 'size' option");
3324 exit(EXIT_FAILURE);
3325 }
3326 if (!*mem_str) {
3327 error_report("missing 'size' option value");
3328 exit(EXIT_FAILURE);
3329 }
3330
3331 sz = qemu_opt_get_size(opts, "size", ram_size);
3332
3333 /* Fix up legacy suffix-less format */
3334 if (g_ascii_isdigit(mem_str[strlen(mem_str) - 1])) {
3335 uint64_t overflow_check = sz;
3336
3337 sz <<= 20;
3338 if ((sz >> 20) != overflow_check) {
3339 error_report("too large 'size' option value");
3340 exit(EXIT_FAILURE);
3341 }
3342 }
3343
3344 /* backward compatibility behaviour for case "-m 0" */
3345 if (sz == 0) {
3346 sz = default_ram_size;
3347 }
3348
3349 sz = QEMU_ALIGN_UP(sz, 8192);
Markus Armbrusterff961012012-08-15 13:12:19 +02003350 ram_size = sz;
3351 if (ram_size != sz) {
Igor Mammedov6e1d3c12013-11-27 01:27:35 +01003352 error_report("ram size too large");
3353 exit(EXIT_FAILURE);
aurel3200f82b82008-04-27 21:12:55 +00003354 }
Igor Mammedov5734d032014-07-08 15:29:46 +02003355 maxram_size = ram_size;
Igor Mammedovc270fb92014-06-02 15:25:02 +02003356
3357 maxmem_str = qemu_opt_get(opts, "maxmem");
3358 slots_str = qemu_opt_get(opts, "slots");
3359 if (maxmem_str && slots_str) {
3360 uint64_t slots;
3361
3362 sz = qemu_opt_get_size(opts, "maxmem", 0);
3363 if (sz < ram_size) {
zhanghailiang4d633222014-09-16 18:45:36 +08003364 error_report("invalid -m option value: maxmem "
3365 "(0x%" PRIx64 ") <= initial memory (0x"
3366 RAM_ADDR_FMT ")", sz, ram_size);
Igor Mammedovc270fb92014-06-02 15:25:02 +02003367 exit(EXIT_FAILURE);
3368 }
3369
3370 slots = qemu_opt_get_number(opts, "slots", 0);
3371 if ((sz > ram_size) && !slots) {
zhanghailiang4d633222014-09-16 18:45:36 +08003372 error_report("invalid -m option value: maxmem "
3373 "(0x%" PRIx64 ") more than initial memory (0x"
Michael S. Tsirkin705456c2014-06-19 13:08:35 +03003374 RAM_ADDR_FMT ") but no hotplug slots where "
zhanghailiang4d633222014-09-16 18:45:36 +08003375 "specified", sz, ram_size);
Igor Mammedovc270fb92014-06-02 15:25:02 +02003376 exit(EXIT_FAILURE);
3377 }
3378
3379 if ((sz <= ram_size) && slots) {
zhanghailiang4d633222014-09-16 18:45:36 +08003380 error_report("invalid -m option value: %"
Igor Mammedovc270fb92014-06-02 15:25:02 +02003381 PRIu64 " hotplug slots where specified but "
zhanghailiang4d633222014-09-16 18:45:36 +08003382 "maxmem (0x%" PRIx64 ") <= initial memory (0x"
3383 RAM_ADDR_FMT ")", slots, sz, ram_size);
Igor Mammedovc270fb92014-06-02 15:25:02 +02003384 exit(EXIT_FAILURE);
3385 }
3386 maxram_size = sz;
3387 ram_slots = slots;
3388 } else if ((!maxmem_str && slots_str) ||
3389 (maxmem_str && !slots_str)) {
zhanghailiang4d633222014-09-16 18:45:36 +08003390 error_report("invalid -m option value: missing "
3391 "'%s' option", slots_str ? "maxmem" : "slots");
Igor Mammedovc270fb92014-06-02 15:25:02 +02003392 exit(EXIT_FAILURE);
3393 }
bellardcd6f1162004-05-13 22:02:20 +00003394 break;
aurel3200f82b82008-04-27 21:12:55 +00003395 }
Stefan Bergerd1a0cf72013-02-27 12:47:49 -05003396#ifdef CONFIG_TPM
3397 case QEMU_OPTION_tpmdev:
3398 if (tpm_config_parse(qemu_find_opts("tpmdev"), optarg) < 0) {
3399 exit(1);
3400 }
3401 break;
3402#endif
Marcelo Tosattic9027602010-03-01 20:25:08 -03003403 case QEMU_OPTION_mempath:
3404 mem_path = optarg;
3405 break;
Marcelo Tosattic9027602010-03-01 20:25:08 -03003406 case QEMU_OPTION_mem_prealloc:
3407 mem_prealloc = 1;
3408 break;
bellardcd6f1162004-05-13 22:02:20 +00003409 case QEMU_OPTION_d:
Matthew Fernandezc235d732011-06-07 16:32:40 +00003410 log_mask = optarg;
3411 break;
3412 case QEMU_OPTION_D:
3413 log_file = optarg;
bellardcd6f1162004-05-13 22:02:20 +00003414 break;
bellardcd6f1162004-05-13 22:02:20 +00003415 case QEMU_OPTION_s:
Markus Armbrusteref0c4a02012-02-07 15:09:13 +01003416 add_device_config(DEV_GDB, "tcp::" DEFAULT_GDBSTUB_PORT);
bellardcd6f1162004-05-13 22:02:20 +00003417 break;
aliguori59030a82009-04-05 18:43:41 +00003418 case QEMU_OPTION_gdb:
Markus Armbrusteref0c4a02012-02-07 15:09:13 +01003419 add_device_config(DEV_GDB, optarg);
bellardcd6f1162004-05-13 22:02:20 +00003420 break;
bellardcd6f1162004-05-13 22:02:20 +00003421 case QEMU_OPTION_L:
Gerd Hoffmann45240512013-03-08 11:42:24 +01003422 if (data_dir_idx < ARRAY_SIZE(data_dir)) {
3423 data_dir[data_dir_idx++] = optarg;
3424 }
bellardcd6f1162004-05-13 22:02:20 +00003425 break;
j_mayer1192dad2007-10-05 13:08:35 +00003426 case QEMU_OPTION_bios:
Gerd Hoffmann1442d3e2013-10-15 16:57:45 +02003427 qemu_opts_set(qemu_find_opts("machine"), 0, "firmware", optarg);
j_mayer1192dad2007-10-05 13:08:35 +00003428 break;
aurel321b530a62009-04-05 20:08:59 +00003429 case QEMU_OPTION_singlestep:
3430 singlestep = 1;
3431 break;
bellardcd6f1162004-05-13 22:02:20 +00003432 case QEMU_OPTION_S:
pbrook3c07f8e2007-01-21 16:47:01 +00003433 autostart = 0;
bellardcd6f1162004-05-13 22:02:20 +00003434 break;
bellard3d11d0e2004-12-12 16:56:30 +00003435 case QEMU_OPTION_k:
3436 keyboard_layout = optarg;
3437 break;
bellardee22c2f2004-06-03 12:49:50 +00003438 case QEMU_OPTION_localtime:
3439 rtc_utc = 0;
3440 break;
malc3893c122008-09-28 00:42:05 +00003441 case QEMU_OPTION_vga:
Blue Swirla369da52011-09-27 19:15:42 +00003442 vga_model = optarg;
Paolo Bonzini7f1b17f2012-05-10 09:39:17 +02003443 default_vga = 0;
bellard1bfe8562004-07-08 21:17:50 +00003444 break;
bellarde9b137c2004-06-21 16:46:10 +00003445 case QEMU_OPTION_g:
3446 {
3447 const char *p;
3448 int w, h, depth;
3449 p = optarg;
3450 w = strtol(p, (char **)&p, 10);
3451 if (w <= 0) {
3452 graphic_error:
3453 fprintf(stderr, "qemu: invalid resolution or depth\n");
3454 exit(1);
3455 }
3456 if (*p != 'x')
3457 goto graphic_error;
3458 p++;
3459 h = strtol(p, (char **)&p, 10);
3460 if (h <= 0)
3461 goto graphic_error;
3462 if (*p == 'x') {
3463 p++;
3464 depth = strtol(p, (char **)&p, 10);
ths5fafdf22007-09-16 21:08:06 +00003465 if (depth != 8 && depth != 15 && depth != 16 &&
bellarde9b137c2004-06-21 16:46:10 +00003466 depth != 24 && depth != 32)
3467 goto graphic_error;
3468 } else if (*p == '\0') {
3469 depth = graphic_depth;
3470 } else {
3471 goto graphic_error;
3472 }
ths3b46e622007-09-17 08:09:54 +00003473
bellarde9b137c2004-06-21 16:46:10 +00003474 graphic_width = w;
3475 graphic_height = h;
3476 graphic_depth = depth;
3477 }
3478 break;
ths20d8a3e2007-02-18 17:04:49 +00003479 case QEMU_OPTION_echr:
3480 {
3481 char *r;
3482 term_escape_char = strtol(optarg, &r, 0);
3483 if (r == optarg)
3484 printf("Bad argument to echr\n");
3485 break;
3486 }
bellard82c643f2004-07-14 17:28:13 +00003487 case QEMU_OPTION_monitor:
Gerd Hoffmann6ca55822009-12-08 13:11:52 +01003488 default_monitor = 0;
Luiz Capitulino70e098a2013-05-16 12:02:55 -04003489 if (strncmp(optarg, "none", 4)) {
3490 monitor_parse(optarg, "readline");
3491 }
Gerd Hoffmann6ca55822009-12-08 13:11:52 +01003492 break;
3493 case QEMU_OPTION_qmp:
3494 monitor_parse(optarg, "control");
Anthony Liguori2d114dc2010-03-21 14:14:38 -05003495 default_monitor = 0;
bellard82c643f2004-07-14 17:28:13 +00003496 break;
Gerd Hoffmann22a0e042009-12-08 13:11:51 +01003497 case QEMU_OPTION_mon:
Gerd Hoffmann3329f072010-08-20 13:52:01 +02003498 opts = qemu_opts_parse(qemu_find_opts("mon"), optarg, 1);
Gerd Hoffmann22a0e042009-12-08 13:11:51 +01003499 if (!opts) {
Gerd Hoffmann22a0e042009-12-08 13:11:51 +01003500 exit(1);
3501 }
Anthony Liguori2d114dc2010-03-21 14:14:38 -05003502 default_monitor = 0;
Gerd Hoffmann22a0e042009-12-08 13:11:51 +01003503 break;
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003504 case QEMU_OPTION_chardev:
Gerd Hoffmann3329f072010-08-20 13:52:01 +02003505 opts = qemu_opts_parse(qemu_find_opts("chardev"), optarg, 1);
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003506 if (!opts) {
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003507 exit(1);
3508 }
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003509 break;
Gautham R Shenoy74db9202010-04-29 17:44:43 +05303510 case QEMU_OPTION_fsdev:
Gerd Hoffmann03b0ba72010-08-20 13:52:02 +02003511 olist = qemu_find_opts("fsdev");
3512 if (!olist) {
3513 fprintf(stderr, "fsdev is not supported by this qemu build.\n");
3514 exit(1);
3515 }
3516 opts = qemu_opts_parse(olist, optarg, 1);
Gautham R Shenoy74db9202010-04-29 17:44:43 +05303517 if (!opts) {
Gautham R Shenoy74db9202010-04-29 17:44:43 +05303518 exit(1);
3519 }
3520 break;
Gautham R Shenoy3d54abc2010-04-29 17:45:03 +05303521 case QEMU_OPTION_virtfs: {
Stefan Hajnoczie14ea472011-03-16 08:31:43 +00003522 QemuOpts *fsdev;
3523 QemuOpts *device;
M. Mohan Kumar84a87cc2011-12-14 13:58:47 +05303524 const char *writeout, *sock_fd, *socket;
Gautham R Shenoy3d54abc2010-04-29 17:45:03 +05303525
Gerd Hoffmann03b0ba72010-08-20 13:52:02 +02003526 olist = qemu_find_opts("virtfs");
3527 if (!olist) {
3528 fprintf(stderr, "virtfs is not supported by this qemu build.\n");
3529 exit(1);
3530 }
3531 opts = qemu_opts_parse(olist, optarg, 1);
Gautham R Shenoy3d54abc2010-04-29 17:45:03 +05303532 if (!opts) {
Gautham R Shenoy3d54abc2010-04-29 17:45:03 +05303533 exit(1);
3534 }
3535
Aneesh Kumar K.Vfbcbf102011-10-13 12:28:04 +05303536 if (qemu_opt_get(opts, "fsdriver") == NULL ||
Aneesh Kumar K.V99519f02011-12-14 13:48:59 +05303537 qemu_opt_get(opts, "mount_tag") == NULL) {
3538 fprintf(stderr, "Usage: -virtfs fsdriver,mount_tag=tag.\n");
Venkateswararao Jujjuri (JV)9ce56db2010-06-14 13:34:40 -07003539 exit(1);
3540 }
Stefan Hajnoczie14ea472011-03-16 08:31:43 +00003541 fsdev = qemu_opts_create(qemu_find_opts("fsdev"),
Luiz Capitulino8be7e7e2012-03-20 15:51:57 -03003542 qemu_opt_get(opts, "mount_tag"),
3543 1, NULL);
Stefan Hajnoczie14ea472011-03-16 08:31:43 +00003544 if (!fsdev) {
3545 fprintf(stderr, "duplicate fsdev id: %s\n",
3546 qemu_opt_get(opts, "mount_tag"));
Gautham R Shenoy3d54abc2010-04-29 17:45:03 +05303547 exit(1);
3548 }
Aneesh Kumar K.Vd3ab98e2011-10-12 19:11:23 +05303549
3550 writeout = qemu_opt_get(opts, "writeout");
3551 if (writeout) {
3552#ifdef CONFIG_SYNC_FILE_RANGE
3553 qemu_opt_set(fsdev, "writeout", writeout);
3554#else
3555 fprintf(stderr, "writeout=immediate not supported on "
3556 "this platform\n");
3557 exit(1);
3558#endif
3559 }
Aneesh Kumar K.Vfbcbf102011-10-13 12:28:04 +05303560 qemu_opt_set(fsdev, "fsdriver", qemu_opt_get(opts, "fsdriver"));
Stefan Hajnoczie14ea472011-03-16 08:31:43 +00003561 qemu_opt_set(fsdev, "path", qemu_opt_get(opts, "path"));
3562 qemu_opt_set(fsdev, "security_model",
3563 qemu_opt_get(opts, "security_model"));
M. Mohan Kumar84a87cc2011-12-14 13:58:47 +05303564 socket = qemu_opt_get(opts, "socket");
3565 if (socket) {
3566 qemu_opt_set(fsdev, "socket", socket);
3567 }
M. Mohan Kumar4c793dd2011-12-14 13:49:28 +05303568 sock_fd = qemu_opt_get(opts, "sock_fd");
3569 if (sock_fd) {
3570 qemu_opt_set(fsdev, "sock_fd", sock_fd);
3571 }
Gautham R Shenoy3d54abc2010-04-29 17:45:03 +05303572
M. Mohan Kumar2c74c2c2011-10-25 12:10:39 +05303573 qemu_opt_set_bool(fsdev, "readonly",
3574 qemu_opt_get_bool(opts, "readonly", 0));
Peter Crosthwaite87ea75d2014-01-01 18:49:17 -08003575 device = qemu_opts_create(qemu_find_opts("device"), NULL, 0,
3576 &error_abort);
Stefan Hajnoczie14ea472011-03-16 08:31:43 +00003577 qemu_opt_set(device, "driver", "virtio-9p-pci");
3578 qemu_opt_set(device, "fsdev",
3579 qemu_opt_get(opts, "mount_tag"));
3580 qemu_opt_set(device, "mount_tag",
3581 qemu_opt_get(opts, "mount_tag"));
Gautham R Shenoy3d54abc2010-04-29 17:45:03 +05303582 break;
3583 }
Aneesh Kumar K.V9db221a2011-10-25 12:10:40 +05303584 case QEMU_OPTION_virtfs_synth: {
3585 QemuOpts *fsdev;
3586 QemuOpts *device;
3587
Luiz Capitulino8be7e7e2012-03-20 15:51:57 -03003588 fsdev = qemu_opts_create(qemu_find_opts("fsdev"), "v_synth",
3589 1, NULL);
Aneesh Kumar K.V9db221a2011-10-25 12:10:40 +05303590 if (!fsdev) {
3591 fprintf(stderr, "duplicate option: %s\n", "virtfs_synth");
3592 exit(1);
3593 }
3594 qemu_opt_set(fsdev, "fsdriver", "synth");
Aneesh Kumar K.V9db221a2011-10-25 12:10:40 +05303595
Peter Crosthwaite87ea75d2014-01-01 18:49:17 -08003596 device = qemu_opts_create(qemu_find_opts("device"), NULL, 0,
3597 &error_abort);
Aneesh Kumar K.V9db221a2011-10-25 12:10:40 +05303598 qemu_opt_set(device, "driver", "virtio-9p-pci");
3599 qemu_opt_set(device, "fsdev", "v_synth");
3600 qemu_opt_set(device, "mount_tag", "v_synth");
3601 break;
3602 }
bellard82c643f2004-07-14 17:28:13 +00003603 case QEMU_OPTION_serial:
Gerd Hoffmann998bbd72009-12-08 13:11:41 +01003604 add_device_config(DEV_SERIAL, optarg);
3605 default_serial = 0;
Jan Kiszka18141ed2010-03-07 11:28:40 +01003606 if (strncmp(optarg, "mon:", 4) == 0) {
3607 default_monitor = 0;
3608 }
bellard82c643f2004-07-14 17:28:13 +00003609 break;
Richard W.M. Jones9dd986c2009-04-25 13:56:19 +01003610 case QEMU_OPTION_watchdog:
Markus Armbruster09aaa162009-08-21 10:31:34 +02003611 if (watchdog) {
3612 fprintf(stderr,
3613 "qemu: only one watchdog option may be given\n");
3614 return 1;
3615 }
3616 watchdog = optarg;
Richard W.M. Jones9dd986c2009-04-25 13:56:19 +01003617 break;
3618 case QEMU_OPTION_watchdog_action:
3619 if (select_watchdog_action(optarg) == -1) {
3620 fprintf(stderr, "Unknown -watchdog-action parameter\n");
3621 exit(1);
3622 }
3623 break;
aliguori51ecf132009-01-15 20:06:40 +00003624 case QEMU_OPTION_virtiocon:
Gerd Hoffmannaee1b932009-12-08 13:11:53 +01003625 add_device_config(DEV_VIRTCON, optarg);
3626 default_virtcon = 0;
Jan Kiszka18141ed2010-03-07 11:28:40 +01003627 if (strncmp(optarg, "mon:", 4) == 0) {
3628 default_monitor = 0;
3629 }
aliguori51ecf132009-01-15 20:06:40 +00003630 break;
bellard6508fe52005-01-15 12:02:56 +00003631 case QEMU_OPTION_parallel:
Gerd Hoffmann6a5e8b02009-12-08 13:11:42 +01003632 add_device_config(DEV_PARALLEL, optarg);
3633 default_parallel = 0;
Jan Kiszka18141ed2010-03-07 11:28:40 +01003634 if (strncmp(optarg, "mon:", 4) == 0) {
3635 default_monitor = 0;
3636 }
bellard6508fe52005-01-15 12:02:56 +00003637 break;
H. Peter Anvinc9f398e2009-12-29 13:51:36 -08003638 case QEMU_OPTION_debugcon:
3639 add_device_config(DEV_DEBUGCON, optarg);
3640 break;
bellardd63d3072004-10-03 13:29:03 +00003641 case QEMU_OPTION_loadvm:
3642 loadvm = optarg;
3643 break;
3644 case QEMU_OPTION_full_screen:
3645 full_screen = 1;
3646 break;
ths43523e92007-02-18 18:19:32 +00003647 case QEMU_OPTION_no_frame:
3648 no_frame = 1;
3649 break;
ths3780e192007-06-21 21:08:02 +00003650 case QEMU_OPTION_alt_grab:
3651 alt_grab = 1;
3652 break;
Dustin Kirkland0ca9f8a2009-09-17 15:48:04 -05003653 case QEMU_OPTION_ctrl_grab:
3654 ctrl_grab = 1;
3655 break;
ths667acca2006-12-11 02:08:05 +00003656 case QEMU_OPTION_no_quit:
3657 no_quit = 1;
3658 break;
aliguori7d957bd2009-01-15 22:14:11 +00003659 case QEMU_OPTION_sdl:
Michael Tokarev24f6ff82013-06-15 14:44:20 +04003660#ifdef CONFIG_SDL
Anthony Liguori993fbfd2009-05-21 16:54:00 -05003661 display_type = DT_SDL;
aliguori7d957bd2009-01-15 22:14:11 +00003662 break;
Jes Sorensen58fc0962011-03-16 13:33:34 +01003663#else
Jes Sorensen58fc0962011-03-16 13:33:34 +01003664 fprintf(stderr, "SDL support is disabled\n");
3665 exit(1);
ths667acca2006-12-11 02:08:05 +00003666#endif
bellardf7cce892004-12-08 22:21:25 +00003667 case QEMU_OPTION_pidfile:
ths93815bc2007-03-19 15:58:31 +00003668 pid_file = optarg;
bellardf7cce892004-12-08 22:21:25 +00003669 break;
bellarda09db212005-04-30 16:10:35 +00003670 case QEMU_OPTION_win2k_hack:
3671 win2k_install_hack = 1;
3672 break;
Jan Kiszka433acf02012-01-23 20:15:12 +01003673 case QEMU_OPTION_rtc_td_hack: {
3674 static GlobalProperty slew_lost_ticks[] = {
3675 {
3676 .driver = "mc146818rtc",
3677 .property = "lost_tick_policy",
3678 .value = "slew",
3679 },
3680 { /* end of list */ }
3681 };
3682
3683 qdev_prop_register_global_list(slew_lost_ticks);
aliguori73822ec2009-01-15 20:11:34 +00003684 break;
Jan Kiszka433acf02012-01-23 20:15:12 +01003685 }
aliguori8a92ea22009-02-27 20:12:36 +00003686 case QEMU_OPTION_acpitable:
Laszlo Ersek0c764a92013-03-21 00:23:17 +01003687 opts = qemu_opts_parse(qemu_find_opts("acpi"), optarg, 1);
Laszlo Ersekf46e7202013-11-28 18:12:59 +01003688 if (!opts) {
3689 exit(1);
3690 }
Laszlo Ersek0c764a92013-03-21 00:23:17 +01003691 do_acpitable_option(opts);
aliguori8a92ea22009-02-27 20:12:36 +00003692 break;
aliguorib6f6e3d2009-04-17 18:59:56 +00003693 case QEMU_OPTION_smbios:
Markus Armbruster4f953d22013-08-16 15:18:29 +02003694 opts = qemu_opts_parse(qemu_find_opts("smbios"), optarg, 0);
Laszlo Ersekf46e7202013-11-28 18:12:59 +01003695 if (!opts) {
3696 exit(1);
3697 }
Markus Armbruster4f953d22013-08-16 15:18:29 +02003698 do_smbios_option(opts);
aliguorib6f6e3d2009-04-17 18:59:56 +00003699 break;
aliguori7ba1e612008-11-05 16:04:33 +00003700 case QEMU_OPTION_enable_kvm:
Anthony PERARD303d4e82010-09-21 20:05:31 +01003701 olist = qemu_find_opts("machine");
Anthony PERARD303d4e82010-09-21 20:05:31 +01003702 qemu_opts_parse(olist, "accel=kvm", 0);
3703 break;
3704 case QEMU_OPTION_machine:
3705 olist = qemu_find_opts("machine");
Jan Kiszka9052ea62011-07-23 12:38:37 +02003706 opts = qemu_opts_parse(olist, optarg, 1);
Anthony PERARD303d4e82010-09-21 20:05:31 +01003707 if (!opts) {
Anthony PERARD303d4e82010-09-21 20:05:31 +01003708 exit(1);
3709 }
Jan Kiszka2645c6d2011-07-25 18:11:20 +02003710 optarg = qemu_opt_get(opts, "type");
3711 if (optarg) {
Marcel Apfelbaum0056ae22014-03-05 19:30:47 +02003712 machine_class = machine_parse(optarg);
Jan Kiszka2645c6d2011-07-25 18:11:20 +02003713 }
aliguori7ba1e612008-11-05 16:04:33 +00003714 break;
Jan Kiszkaa0dac022012-10-05 14:51:45 -03003715 case QEMU_OPTION_no_kvm:
3716 olist = qemu_find_opts("machine");
3717 qemu_opts_parse(olist, "accel=tcg", 0);
3718 break;
Jan Kiszka4086bde2012-10-05 14:51:41 -03003719 case QEMU_OPTION_no_kvm_pit: {
3720 fprintf(stderr, "Warning: KVM PIT can no longer be disabled "
3721 "separately.\n");
3722 break;
3723 }
Jan Kiszkac21fb4f2012-10-05 14:51:42 -03003724 case QEMU_OPTION_no_kvm_pit_reinjection: {
3725 static GlobalProperty kvm_pit_lost_tick_policy[] = {
3726 {
3727 .driver = "kvm-pit",
3728 .property = "lost_tick_policy",
3729 .value = "discard",
3730 },
3731 { /* end of list */ }
3732 };
3733
3734 fprintf(stderr, "Warning: option deprecated, use "
3735 "lost_tick_policy property of kvm-pit instead.\n");
3736 qdev_prop_register_global_list(kvm_pit_lost_tick_policy);
3737 break;
3738 }
bellardbb36d472005-11-05 14:22:28 +00003739 case QEMU_OPTION_usb:
Peter Maydellfa5358c2012-11-22 16:48:45 +00003740 olist = qemu_find_opts("machine");
3741 qemu_opts_parse(olist, "usb=on", 0);
bellardbb36d472005-11-05 14:22:28 +00003742 break;
bellarda594cfb2005-11-06 16:13:29 +00003743 case QEMU_OPTION_usbdevice:
Peter Maydellfa5358c2012-11-22 16:48:45 +00003744 olist = qemu_find_opts("machine");
3745 qemu_opts_parse(olist, "usb=on", 0);
Gerd Hoffmannbd3c9482009-07-15 13:59:26 +02003746 add_device_config(DEV_USB, optarg);
3747 break;
3748 case QEMU_OPTION_device:
Gerd Hoffmann3329f072010-08-20 13:52:01 +02003749 if (!qemu_opts_parse(qemu_find_opts("device"), optarg, 1)) {
Gerd Hoffmannf31d07d2009-07-31 12:25:37 +02003750 exit(1);
3751 }
bellarda594cfb2005-11-06 16:13:29 +00003752 break;
bellard6a00d602005-11-21 23:25:50 +00003753 case QEMU_OPTION_smp:
Michael Tokarev12b7f572013-06-24 15:06:52 +04003754 if (!qemu_opts_parse(qemu_find_opts("smp-opts"), optarg, 1)) {
Jes Sorensen6be68d72009-07-23 17:03:42 +02003755 exit(1);
3756 }
bellard6a00d602005-11-21 23:25:50 +00003757 break;
bellard24236862006-04-30 21:28:36 +00003758 case QEMU_OPTION_vnc:
Jes Sorensen821601e2011-03-16 13:33:36 +01003759#ifdef CONFIG_VNC
Gerd Hoffmann6b62dc22010-08-25 10:51:06 +02003760 display_remote++;
Jes Sorensen821601e2011-03-16 13:33:36 +01003761 vnc_display = optarg;
3762#else
3763 fprintf(stderr, "VNC support is disabled\n");
3764 exit(1);
3765#endif
3766 break;
bellard6515b202006-05-03 22:02:44 +00003767 case QEMU_OPTION_no_acpi:
3768 acpi_enabled = 0;
3769 break;
aliguori16b29ae2008-12-17 23:28:44 +00003770 case QEMU_OPTION_no_hpet:
3771 no_hpet = 1;
3772 break;
Markus Armbruster7d4c3d52009-06-26 19:15:14 +02003773 case QEMU_OPTION_balloon:
3774 if (balloon_parse(optarg) < 0) {
3775 fprintf(stderr, "Unknown -balloon argument %s\n", optarg);
3776 exit(1);
3777 }
Eduardo Habkostdf97b922009-06-10 16:34:08 -03003778 break;
bellardd1beab82006-10-02 19:44:22 +00003779 case QEMU_OPTION_no_reboot:
3780 no_reboot = 1;
3781 break;
aurel32b2f76162008-04-11 21:35:52 +00003782 case QEMU_OPTION_no_shutdown:
3783 no_shutdown = 1;
3784 break;
balrog9467cd42007-05-01 01:34:14 +00003785 case QEMU_OPTION_show_cursor:
3786 cursor_hide = 0;
3787 break;
blueswir18fcb1b92008-09-18 18:29:08 +00003788 case QEMU_OPTION_uuid:
3789 if(qemu_uuid_parse(optarg, qemu_uuid) < 0) {
3790 fprintf(stderr, "Fail to parse UUID string."
3791 " Wrong format.\n");
3792 exit(1);
3793 }
Markus Armbrusterfc3b3292013-08-16 15:18:31 +02003794 qemu_uuid_set = true;
blueswir18fcb1b92008-09-18 18:29:08 +00003795 break;
ths9ae02552007-01-05 17:39:04 +00003796 case QEMU_OPTION_option_rom:
3797 if (nb_option_roms >= MAX_OPTION_ROMS) {
3798 fprintf(stderr, "Too many option ROMs\n");
3799 exit(1);
3800 }
Gleb Natapov2e55e842010-12-08 13:35:07 +02003801 opts = qemu_opts_parse(qemu_find_opts("option-rom"), optarg, 1);
Markus Armbruster49295eb2013-02-08 21:22:19 +01003802 if (!opts) {
3803 exit(1);
3804 }
Gleb Natapov2e55e842010-12-08 13:35:07 +02003805 option_rom[nb_option_roms].name = qemu_opt_get(opts, "romfile");
3806 option_rom[nb_option_roms].bootindex =
3807 qemu_opt_get_number(opts, "bootindex", -1);
3808 if (!option_rom[nb_option_roms].name) {
3809 fprintf(stderr, "Option ROM file is not specified\n");
3810 exit(1);
3811 }
ths9ae02552007-01-05 17:39:04 +00003812 nb_option_roms++;
3813 break;
pbrook8e716212007-01-20 17:12:09 +00003814 case QEMU_OPTION_semihosting:
3815 semihosting_enabled = 1;
3816 break;
Jan Kiszka88eed342012-10-05 14:51:44 -03003817 case QEMU_OPTION_tdf:
3818 fprintf(stderr, "Warning: user space PIT time drift fix "
3819 "is no longer supported.\n");
3820 break;
thsc35734b2007-03-19 15:17:08 +00003821 case QEMU_OPTION_name:
Dr. David Alan Gilbert5d12f962014-01-30 10:20:30 +00003822 opts = qemu_opts_parse(qemu_find_opts("name"), optarg, 1);
3823 if (!opts) {
3824 exit(1);
3825 }
thsc35734b2007-03-19 15:17:08 +00003826 break;
blueswir166508602007-05-01 14:16:52 +00003827 case QEMU_OPTION_prom_env:
3828 if (nb_prom_envs >= MAX_PROM_ENVS) {
3829 fprintf(stderr, "Too many prom variables\n");
3830 exit(1);
3831 }
3832 prom_envs[nb_prom_envs] = optarg;
3833 nb_prom_envs++;
3834 break;
balrog2b8f2d42007-07-27 22:08:46 +00003835 case QEMU_OPTION_old_param:
3836 old_param = 1;
ths05ebd532008-01-08 19:32:16 +00003837 break;
thsf3dcfad2007-08-24 01:26:02 +00003838 case QEMU_OPTION_clock:
Alex Bligh6d327172013-08-21 16:02:59 +01003839 /* Clock options no longer exist. Keep this option for
3840 * backward compatibility.
3841 */
thsf3dcfad2007-08-24 01:26:02 +00003842 break;
bellard7e0af5d02007-11-07 16:24:33 +00003843 case QEMU_OPTION_startdate:
Jan Kiszka1ed2fc12009-09-15 13:36:04 +02003844 configure_rtc_date_offset(optarg, 1);
3845 break;
3846 case QEMU_OPTION_rtc:
Gerd Hoffmann3329f072010-08-20 13:52:01 +02003847 opts = qemu_opts_parse(qemu_find_opts("rtc"), optarg, 0);
Jan Kiszka1ed2fc12009-09-15 13:36:04 +02003848 if (!opts) {
Jan Kiszka1ed2fc12009-09-15 13:36:04 +02003849 exit(1);
bellard7e0af5d02007-11-07 16:24:33 +00003850 }
Jan Kiszka1ed2fc12009-09-15 13:36:04 +02003851 configure_rtc(opts);
bellard7e0af5d02007-11-07 16:24:33 +00003852 break;
bellard26a5f132008-05-28 12:30:31 +00003853 case QEMU_OPTION_tb_size:
Jan Kiszkad5ab9712011-08-02 16:10:21 +02003854 tcg_tb_size = strtol(optarg, NULL, 0);
3855 if (tcg_tb_size < 0) {
3856 tcg_tb_size = 0;
3857 }
bellard26a5f132008-05-28 12:30:31 +00003858 break;
pbrook2e70f6e2008-06-29 01:03:05 +00003859 case QEMU_OPTION_icount:
Sebastian Tanase1ad95802014-07-25 11:56:28 +02003860 icount_opts = qemu_opts_parse(qemu_find_opts("icount"),
3861 optarg, 1);
3862 if (!icount_opts) {
3863 exit(1);
3864 }
pbrook2e70f6e2008-06-29 01:03:05 +00003865 break;
aliguori5bb79102008-10-13 03:12:02 +00003866 case QEMU_OPTION_incoming:
3867 incoming = optarg;
Stefano Stabellini81323a62012-01-18 12:23:13 +00003868 runstate_set(RUN_STATE_INMIGRATE);
aliguori5bb79102008-10-13 03:12:02 +00003869 break;
Gerd Hoffmannd8c208d2009-12-08 13:11:46 +01003870 case QEMU_OPTION_nodefaults:
Mark Wud44229c2014-03-10 22:37:40 +08003871 has_defaults = 0;
Gerd Hoffmannd8c208d2009-12-08 13:11:46 +01003872 break;
aliguorie37630c2009-04-22 15:19:10 +00003873 case QEMU_OPTION_xen_domid:
Blue Swirlad960902010-03-29 19:23:52 +00003874 if (!(xen_available())) {
3875 printf("Option %s not supported for this target\n", popt->name);
3876 exit(1);
3877 }
aliguorie37630c2009-04-22 15:19:10 +00003878 xen_domid = atoi(optarg);
3879 break;
3880 case QEMU_OPTION_xen_create:
Blue Swirlad960902010-03-29 19:23:52 +00003881 if (!(xen_available())) {
3882 printf("Option %s not supported for this target\n", popt->name);
3883 exit(1);
3884 }
aliguorie37630c2009-04-22 15:19:10 +00003885 xen_mode = XEN_CREATE;
3886 break;
3887 case QEMU_OPTION_xen_attach:
Blue Swirlad960902010-03-29 19:23:52 +00003888 if (!(xen_available())) {
3889 printf("Option %s not supported for this target\n", popt->name);
3890 exit(1);
3891 }
aliguorie37630c2009-04-22 15:19:10 +00003892 xen_mode = XEN_ATTACH;
3893 break;
Prerna Saxenaab6540d2010-08-09 11:48:32 +01003894 case QEMU_OPTION_trace:
LluĂ­se4858972011-08-31 20:31:03 +02003895 {
Prerna Saxenaab6540d2010-08-09 11:48:32 +01003896 opts = qemu_opts_parse(qemu_find_opts("trace"), optarg, 0);
LluĂ­se4858972011-08-31 20:31:03 +02003897 if (!opts) {
3898 exit(1);
Prerna Saxenaab6540d2010-08-09 11:48:32 +01003899 }
LluĂ­s23d15e82011-08-31 20:31:31 +02003900 trace_events = qemu_opt_get(opts, "events");
LluĂ­se4858972011-08-31 20:31:03 +02003901 trace_file = qemu_opt_get(opts, "file");
Prerna Saxenaab6540d2010-08-09 11:48:32 +01003902 break;
LluĂ­se4858972011-08-31 20:31:03 +02003903 }
Gerd Hoffmann715a6642009-10-14 10:39:28 +02003904 case QEMU_OPTION_readconfig:
3905 {
Kevin Wolfdcfb0932010-03-05 17:25:55 +01003906 int ret = qemu_read_config_file(optarg);
3907 if (ret < 0) {
3908 fprintf(stderr, "read config %s: %s\n", optarg,
3909 strerror(-ret));
Gerd Hoffmann715a6642009-10-14 10:39:28 +02003910 exit(1);
3911 }
Gerd Hoffmann715a6642009-10-14 10:39:28 +02003912 break;
3913 }
Gerd Hoffmann29b00402010-03-11 11:13:27 -03003914 case QEMU_OPTION_spice:
3915 olist = qemu_find_opts("spice");
3916 if (!olist) {
3917 fprintf(stderr, "spice is not supported by this qemu build.\n");
3918 exit(1);
3919 }
3920 opts = qemu_opts_parse(olist, optarg, 0);
3921 if (!opts) {
Gerd Hoffmann29b00402010-03-11 11:13:27 -03003922 exit(1);
3923 }
Gerd Hoffmannf963e4d2013-02-25 16:02:30 +01003924 display_remote++;
Gerd Hoffmann29b00402010-03-11 11:13:27 -03003925 break;
Gerd Hoffmann715a6642009-10-14 10:39:28 +02003926 case QEMU_OPTION_writeconfig:
3927 {
3928 FILE *fp;
3929 if (strcmp(optarg, "-") == 0) {
3930 fp = stdout;
3931 } else {
3932 fp = fopen(optarg, "w");
3933 if (fp == NULL) {
3934 fprintf(stderr, "open %s: %s\n", optarg, strerror(errno));
3935 exit(1);
3936 }
3937 }
3938 qemu_config_write(fp);
Chen Gang7fb8b5d2014-04-22 09:12:34 +08003939 if (fp != stdout) {
3940 fclose(fp);
3941 }
Gerd Hoffmann715a6642009-10-14 10:39:28 +02003942 break;
3943 }
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +02003944 case QEMU_OPTION_qtest:
3945 qtest_chrdev = optarg;
3946 break;
3947 case QEMU_OPTION_qtest_log:
3948 qtest_log = optarg;
3949 break;
Eduardo Otubo7d76ad42012-08-14 18:44:08 -03003950 case QEMU_OPTION_sandbox:
3951 opts = qemu_opts_parse(qemu_find_opts("sandbox"), optarg, 1);
3952 if (!opts) {
Markus Armbruster49295eb2013-02-08 21:22:19 +01003953 exit(1);
Eduardo Otubo7d76ad42012-08-14 18:44:08 -03003954 }
3955 break;
Corey Bryant587ed6b2012-10-18 15:19:34 -04003956 case QEMU_OPTION_add_fd:
3957#ifndef _WIN32
3958 opts = qemu_opts_parse(qemu_find_opts("add-fd"), optarg, 0);
3959 if (!opts) {
Markus Armbruster49295eb2013-02-08 21:22:19 +01003960 exit(1);
Corey Bryant587ed6b2012-10-18 15:19:34 -04003961 }
3962#else
3963 error_report("File descriptor passing is disabled on this "
3964 "platform");
3965 exit(1);
3966#endif
3967 break;
Anthony Liguori68d98d32012-06-25 14:36:33 -05003968 case QEMU_OPTION_object:
3969 opts = qemu_opts_parse(qemu_find_opts("object"), optarg, 1);
Markus Armbruster49295eb2013-02-08 21:22:19 +01003970 if (!opts) {
3971 exit(1);
3972 }
Anthony Liguori68d98d32012-06-25 14:36:33 -05003973 break;
Satoru Moriya888a6bc2013-04-19 16:42:06 +02003974 case QEMU_OPTION_realtime:
3975 opts = qemu_opts_parse(qemu_find_opts("realtime"), optarg, 0);
3976 if (!opts) {
3977 exit(1);
3978 }
zhanghailiang28d16f32014-09-23 18:42:24 +08003979 enable_mlock = qemu_opt_get_bool(opts, "mlock", true);
Satoru Moriya888a6bc2013-04-19 16:42:06 +02003980 break;
Seiji Aguchi5e2ac512013-07-03 23:02:46 -04003981 case QEMU_OPTION_msg:
3982 opts = qemu_opts_parse(qemu_find_opts("msg"), optarg, 0);
3983 if (!opts) {
3984 exit(1);
3985 }
3986 configure_msg(opts);
3987 break;
Amit Shahabfd9ce2014-06-20 18:56:08 +05303988 case QEMU_OPTION_dump_vmstate:
3989 vmstate_dump_file = fopen(optarg, "w");
3990 if (vmstate_dump_file == NULL) {
3991 fprintf(stderr, "open %s: %s\n", optarg, strerror(errno));
3992 exit(1);
3993 }
3994 break;
Jes Sorensen59a52642010-06-10 11:42:25 +02003995 default:
3996 os_parse_cmd_args(popt->index, optarg);
bellardcd6f1162004-05-13 22:02:20 +00003997 }
bellard0824d6f2003-06-24 13:42:40 +00003998 }
3999 }
Markus Armbruster0f0bc3f2010-02-18 20:13:51 +01004000 loc_set_none();
bellard330d0412003-07-26 18:11:40 +00004001
Igor Mammedov2d9c2722014-06-02 15:25:00 +02004002 os_daemonize();
4003
Chrysostomos Nanakos2f78e492014-09-18 14:30:49 +03004004 if (qemu_init_main_loop(&main_loop_err)) {
4005 error_report("%s", error_get_pretty(main_loop_err));
Paolo Bonzini1c537862012-10-30 00:17:12 +01004006 exit(1);
4007 }
4008
Eduardo Otubo7d76ad42012-08-14 18:44:08 -03004009 if (qemu_opts_foreach(qemu_find_opts("sandbox"), parse_sandbox, NULL, 0)) {
4010 exit(1);
4011 }
4012
Dr. David Alan Gilbert5b9d3132014-05-06 12:15:55 +01004013 if (qemu_opts_foreach(qemu_find_opts("name"), parse_name, NULL, 1)) {
4014 exit(1);
4015 }
4016
Corey Bryant587ed6b2012-10-18 15:19:34 -04004017#ifndef _WIN32
4018 if (qemu_opts_foreach(qemu_find_opts("add-fd"), parse_add_fd, NULL, 1)) {
4019 exit(1);
4020 }
4021
4022 if (qemu_opts_foreach(qemu_find_opts("add-fd"), cleanup_add_fd, NULL, 1)) {
4023 exit(1);
4024 }
4025#endif
4026
Marcel Apfelbaum0056ae22014-03-05 19:30:47 +02004027 if (machine_class == NULL) {
Peter Maydella7a55442014-03-27 14:00:52 +00004028 fprintf(stderr, "No machine specified, and there is no default.\n"
4029 "Use -machine help to list supported machines!\n");
Dunrong Huangfb7c2692012-07-24 00:42:20 +08004030 exit(1);
4031 }
4032
Marcel Apfelbaum0056ae22014-03-05 19:30:47 +02004033 current_machine = MACHINE(object_new(object_class_get_name(
4034 OBJECT_CLASS(machine_class))));
4035 object_property_add_child(object_get_root(), "machine",
4036 OBJECT(current_machine), &error_abort);
Paolo Bonzinib5c2c3d2014-06-11 10:52:32 +02004037 cpu_exec_init_all();
Marcel Apfelbaum0056ae22014-03-05 19:30:47 +02004038
Marcel Apfelbaum958db902014-04-09 20:34:53 +03004039 if (machine_class->hw_version) {
4040 qemu_set_version(machine_class->hw_version);
CrĂ­stian Viana93bfef42012-05-30 00:35:51 -03004041 }
4042
Eduardo Habkostecf40be2012-03-09 16:19:07 -03004043 /* Init CPU def lists, based on config
4044 * - Must be called after all the qemu_read_config_file() calls
4045 * - Must be called before list_cpus()
4046 * - Must be called before machine->init()
4047 */
4048 cpudef_init();
4049
Peter Maydellc8057f92012-08-02 13:45:54 +01004050 if (cpu_model && is_help_option(cpu_model)) {
Eduardo Habkost1d6528a2012-03-21 09:33:40 -03004051 list_cpus(stdout, &fprintf, cpu_model);
Eduardo Habkostecf40be2012-03-09 16:19:07 -03004052 exit(0);
4053 }
4054
Matthew Fernandezc235d732011-06-07 16:32:40 +00004055 /* Open the logfile at this point, if necessary. We can't open the logfile
4056 * when encountering either of the logging options (-d or -D) because the
4057 * other one may be encountered later on the command line, changing the
4058 * location or level of logging.
4059 */
4060 if (log_mask) {
Peter Maydellb946bff2013-02-11 16:41:24 +00004061 int mask;
Matthew Fernandezc235d732011-06-07 16:32:40 +00004062 if (log_file) {
Peter Maydell9a7e5422013-02-11 16:41:20 +00004063 qemu_set_log_filename(log_file);
Matthew Fernandezc235d732011-06-07 16:32:40 +00004064 }
Peter Maydellb946bff2013-02-11 16:41:24 +00004065
4066 mask = qemu_str_to_log_mask(log_mask);
4067 if (!mask) {
4068 qemu_print_log_usage(stdout);
4069 exit(1);
4070 }
4071 qemu_set_log(mask);
Matthew Fernandezc235d732011-06-07 16:32:40 +00004072 }
4073
Michael Mueller8a745f22013-09-23 16:36:54 +02004074 if (!is_daemonized()) {
LluĂ­s Vilanova5b808272014-05-27 15:02:14 +02004075 if (!trace_init_backends(trace_events, trace_file)) {
Michael Mueller8a745f22013-09-23 16:36:54 +02004076 exit(1);
4077 }
Stefan Hajnoczi31d3c9b2011-03-13 20:14:30 +00004078 }
Stefan Hajnoczi0b5538c2011-02-26 18:38:39 +00004079
Paul Brook5cea8592009-05-30 00:52:44 +01004080 /* If no data_dir is specified then try to find it relative to the
4081 executable path. */
Gerd Hoffmann45240512013-03-08 11:42:24 +01004082 if (data_dir_idx < ARRAY_SIZE(data_dir)) {
Fam Zheng10f5bff2014-02-10 14:48:51 +08004083 data_dir[data_dir_idx] = os_find_datadir();
Gerd Hoffmann45240512013-03-08 11:42:24 +01004084 if (data_dir[data_dir_idx] != NULL) {
4085 data_dir_idx++;
4086 }
Paul Brook5cea8592009-05-30 00:52:44 +01004087 }
Stefan Weil60474fb2011-09-04 15:17:46 +02004088 /* If all else fails use the install path specified when building. */
Gerd Hoffmann45240512013-03-08 11:42:24 +01004089 if (data_dir_idx < ARRAY_SIZE(data_dir)) {
4090 data_dir[data_dir_idx++] = CONFIG_QEMU_DATADIR;
Paul Brook5cea8592009-05-30 00:52:44 +01004091 }
4092
Michael Tokarev12b7f572013-06-24 15:06:52 +04004093 smp_parse(qemu_opts_find(qemu_find_opts("smp-opts"), NULL));
Jes Sorensen6be68d72009-07-23 17:03:42 +02004094
Marcel Apfelbaum958db902014-04-09 20:34:53 +03004095 machine_class->max_cpus = machine_class->max_cpus ?: 1; /* Default to UP */
4096 if (smp_cpus > machine_class->max_cpus) {
aliguorib2097002008-10-07 20:39:39 +00004097 fprintf(stderr, "Number of SMP cpus requested (%d), exceeds max cpus "
Marcel Apfelbaum958db902014-04-09 20:34:53 +03004098 "supported by machine `%s' (%d)\n", smp_cpus,
4099 machine_class->name, machine_class->max_cpus);
aliguorib2097002008-10-07 20:39:39 +00004100 exit(1);
4101 }
4102
Anthony PERARD67b724e2010-11-22 15:44:15 +00004103 /*
4104 * Get the default machine options from the machine if it is not already
4105 * specified either by the configuration file or by the command line.
4106 */
Marcel Apfelbaum958db902014-04-09 20:34:53 +03004107 if (machine_class->default_machine_opts) {
Jan Kiszka25de5932012-01-27 19:55:43 +01004108 qemu_opts_set_defaults(qemu_find_opts("machine"),
Marcel Apfelbaum958db902014-04-09 20:34:53 +03004109 machine_class->default_machine_opts, 0);
Anthony PERARD67b724e2010-11-22 15:44:15 +00004110 }
4111
Gerd Hoffmann3329f072010-08-20 13:52:01 +02004112 qemu_opts_foreach(qemu_find_opts("device"), default_driver_check, NULL, 0);
4113 qemu_opts_foreach(qemu_find_opts("global"), default_driver_check, NULL, 0);
Gerd Hoffmann998bbd72009-12-08 13:11:41 +01004114
Mark Wud44229c2014-03-10 22:37:40 +08004115 if (!vga_model && !default_vga) {
4116 vga_interface_type = VGA_DEVICE;
4117 }
Marcel Apfelbaum958db902014-04-09 20:34:53 +03004118 if (!has_defaults || machine_class->no_serial) {
Gerd Hoffmann986c5f72009-12-08 13:11:54 +01004119 default_serial = 0;
4120 }
Marcel Apfelbaum958db902014-04-09 20:34:53 +03004121 if (!has_defaults || machine_class->no_parallel) {
Gerd Hoffmann986c5f72009-12-08 13:11:54 +01004122 default_parallel = 0;
4123 }
Marcel Apfelbaum958db902014-04-09 20:34:53 +03004124 if (!has_defaults || !machine_class->use_virtcon) {
Gerd Hoffmann986c5f72009-12-08 13:11:54 +01004125 default_virtcon = 0;
4126 }
Marcel Apfelbaum958db902014-04-09 20:34:53 +03004127 if (!has_defaults || !machine_class->use_sclp) {
Alexander Graf3ef669e2013-01-24 12:18:52 +01004128 default_sclp = 0;
4129 }
Marcel Apfelbaum958db902014-04-09 20:34:53 +03004130 if (!has_defaults || machine_class->no_floppy) {
Gerd Hoffmannac33f8f2009-12-16 14:25:39 +01004131 default_floppy = 0;
4132 }
Marcel Apfelbaum958db902014-04-09 20:34:53 +03004133 if (!has_defaults || machine_class->no_cdrom) {
Gerd Hoffmannac33f8f2009-12-16 14:25:39 +01004134 default_cdrom = 0;
4135 }
Marcel Apfelbaum958db902014-04-09 20:34:53 +03004136 if (!has_defaults || machine_class->no_sdcard) {
Gerd Hoffmannac33f8f2009-12-16 14:25:39 +01004137 default_sdcard = 0;
4138 }
Mark Wud44229c2014-03-10 22:37:40 +08004139 if (!has_defaults) {
4140 default_monitor = 0;
4141 default_net = 0;
4142 default_vga = 0;
4143 }
Gerd Hoffmann986c5f72009-12-08 13:11:54 +01004144
Michael Tokarevab51b1d2012-12-30 12:48:14 +04004145 if (is_daemonized()) {
4146 /* According to documentation and historically, -nographic redirects
4147 * serial port, parallel port and monitor to stdio, which does not work
4148 * with -daemonize. We can redirect these to null instead, but since
4149 * -nographic is legacy, let's just error out.
4150 * We disallow -nographic only if all other ports are not redirected
4151 * explicitly, to not break existing legacy setups which uses
4152 * -nographic _and_ redirects all ports explicitly - this is valid
4153 * usage, -nographic is just a no-op in this case.
4154 */
4155 if (display_type == DT_NOGRAPHIC
4156 && (default_parallel || default_serial
4157 || default_monitor || default_virtcon)) {
4158 fprintf(stderr, "-nographic can not be used with -daemonize\n");
4159 exit(1);
4160 }
4161#ifdef CONFIG_CURSES
4162 if (display_type == DT_CURSES) {
4163 fprintf(stderr, "curses display can not be used with -daemonize\n");
4164 exit(1);
4165 }
4166#endif
4167 }
4168
Anthony Liguori993fbfd2009-05-21 16:54:00 -05004169 if (display_type == DT_NOGRAPHIC) {
Gerd Hoffmann6a5e8b02009-12-08 13:11:42 +01004170 if (default_parallel)
4171 add_device_config(DEV_PARALLEL, "null");
Gerd Hoffmanne1c09172009-12-08 13:11:44 +01004172 if (default_serial && default_monitor) {
4173 add_device_config(DEV_SERIAL, "mon:stdio");
Gerd Hoffmann986c5f72009-12-08 13:11:54 +01004174 } else if (default_virtcon && default_monitor) {
4175 add_device_config(DEV_VIRTCON, "mon:stdio");
Alexander Graf3ef669e2013-01-24 12:18:52 +01004176 } else if (default_sclp && default_monitor) {
4177 add_device_config(DEV_SCLP, "mon:stdio");
Gerd Hoffmanne1c09172009-12-08 13:11:44 +01004178 } else {
4179 if (default_serial)
4180 add_device_config(DEV_SERIAL, "stdio");
Gerd Hoffmann986c5f72009-12-08 13:11:54 +01004181 if (default_virtcon)
4182 add_device_config(DEV_VIRTCON, "stdio");
Alexander Graf3ef669e2013-01-24 12:18:52 +01004183 if (default_sclp) {
4184 add_device_config(DEV_SCLP, "stdio");
4185 }
Gerd Hoffmanne1c09172009-12-08 13:11:44 +01004186 if (default_monitor)
Gerd Hoffmann6ca55822009-12-08 13:11:52 +01004187 monitor_parse("stdio", "readline");
Gerd Hoffmanne1c09172009-12-08 13:11:44 +01004188 }
Gerd Hoffmann998bbd72009-12-08 13:11:41 +01004189 } else {
4190 if (default_serial)
4191 add_device_config(DEV_SERIAL, "vc:80Cx24C");
Gerd Hoffmann6a5e8b02009-12-08 13:11:42 +01004192 if (default_parallel)
4193 add_device_config(DEV_PARALLEL, "vc:80Cx24C");
Gerd Hoffmannabdeed02009-12-08 13:11:43 +01004194 if (default_monitor)
Gerd Hoffmann6ca55822009-12-08 13:11:52 +01004195 monitor_parse("vc:80Cx24C", "readline");
Alexander Graf38536da2009-12-17 13:06:08 +01004196 if (default_virtcon)
4197 add_device_config(DEV_VIRTCON, "vc:80Cx24C");
Alexander Graf3ef669e2013-01-24 12:18:52 +01004198 if (default_sclp) {
4199 add_device_config(DEV_SCLP, "vc:80Cx24C");
4200 }
aliguoribc0129d2008-08-01 15:12:34 +00004201 }
4202
Anthony Liguori15546422013-02-20 07:43:25 -06004203 if (display_type == DT_DEFAULT && !display_remote) {
4204#if defined(CONFIG_GTK)
4205 display_type = DT_GTK;
4206#elif defined(CONFIG_SDL) || defined(CONFIG_COCOA)
4207 display_type = DT_SDL;
4208#elif defined(CONFIG_VNC)
4209 vnc_display = "localhost:0,to=99";
4210 show_vnc_port = 1;
4211#else
4212 display_type = DT_NONE;
4213#endif
4214 }
4215
Peter Wu047d4e12013-06-11 10:55:08 +02004216 if ((no_frame || alt_grab || ctrl_grab) && display_type != DT_SDL) {
4217 fprintf(stderr, "-no-frame, -alt-grab and -ctrl-grab are only valid "
4218 "for SDL, ignoring option\n");
4219 }
4220 if (no_quit && (display_type != DT_GTK && display_type != DT_SDL)) {
4221 fprintf(stderr, "-no-quit is only valid for GTK and SDL, "
4222 "ignoring option\n");
4223 }
4224
Anthony Liguori15546422013-02-20 07:43:25 -06004225#if defined(CONFIG_GTK)
4226 if (display_type == DT_GTK) {
4227 early_gtk_display_init();
4228 }
4229#endif
4230
TeLeMana5829fd2010-04-15 12:37:55 +08004231 socket_init();
4232
Gerd Hoffmann3329f072010-08-20 13:52:01 +02004233 if (qemu_opts_foreach(qemu_find_opts("chardev"), chardev_init_func, NULL, 1) != 0)
Gerd Hoffmann1a688d32009-12-08 13:11:36 +01004234 exit(1);
Venkateswararao Jujjuri (JV)758e8e32010-06-14 13:34:41 -07004235#ifdef CONFIG_VIRTFS
Gerd Hoffmann3329f072010-08-20 13:52:01 +02004236 if (qemu_opts_foreach(qemu_find_opts("fsdev"), fsdev_init_func, NULL, 1) != 0) {
Gautham R Shenoy74db9202010-04-29 17:44:43 +05304237 exit(1);
4238 }
4239#endif
Gerd Hoffmann1a688d32009-12-08 13:11:36 +01004240
thsaa26bb22007-03-25 21:33:06 +00004241 if (pid_file && qemu_create_pidfile(pid_file) != 0) {
Jes Sorenseneb505be2010-06-10 11:42:28 +02004242 os_pidfile_error();
ths93815bc2007-03-19 15:58:31 +00004243 exit(1);
4244 }
4245
Igor Mammedov6e1d3c12013-11-27 01:27:35 +01004246 /* store value for the future use */
4247 qemu_opt_set_number(qemu_find_opts_singleton("memory"), "size", ram_size);
Jan Kiszka0ac543d2011-08-15 16:18:57 -07004248
Bruce Rogers3d1d9652012-08-09 12:47:40 -06004249 if (qemu_opts_foreach(qemu_find_opts("device"), device_help_func, NULL, 0)
4250 != 0) {
4251 exit(0);
4252 }
4253
Paolo Bonzini7b717582014-08-25 13:47:00 +02004254 if (qemu_opts_foreach(qemu_find_opts("object"),
4255 object_create, NULL, 0) != 0) {
4256 exit(1);
4257 }
4258
Marcel Apfelbaum6b1b1442014-05-26 15:40:58 +03004259 machine_opts = qemu_get_machine_opts();
Andreas Färberd2659e22014-07-23 16:16:26 +02004260 if (qemu_opt_foreach(machine_opts, machine_set_property, current_machine,
Marcel Apfelbaum6b1b1442014-05-26 15:40:58 +03004261 1) < 0) {
4262 object_unref(OBJECT(current_machine));
4263 exit(1);
4264 }
4265
Marcel Apfelbaumf1e29872014-04-09 20:34:52 +03004266 configure_accelerator(machine_class);
Marcelo Tosatti214910a2009-09-18 02:41:23 -03004267
Paolo Bonzinid4fce242013-10-18 13:51:11 +02004268 if (qtest_chrdev) {
Fam Zheng23802b42014-02-10 09:28:02 +08004269 Error *local_err = NULL;
4270 qtest_init(qtest_chrdev, qtest_log, &local_err);
4271 if (local_err) {
4272 error_report("%s", error_get_pretty(local_err));
4273 error_free(local_err);
4274 exit(1);
4275 }
Edgar E. Iglesiasa907cf52013-04-15 21:28:17 +02004276 }
4277
Markus Armbruster2ff3de62013-07-04 15:09:22 +02004278 machine_opts = qemu_get_machine_opts();
4279 kernel_filename = qemu_opt_get(machine_opts, "kernel");
4280 initrd_filename = qemu_opt_get(machine_opts, "initrd");
4281 kernel_cmdline = qemu_opt_get(machine_opts, "append");
Gerd Hoffmann1442d3e2013-10-15 16:57:45 +02004282 bios_name = qemu_opt_get(machine_opts, "firmware");
Peter Maydell967c0da2012-02-22 22:40:00 +00004283
Marcel Apfelbaum958db902014-04-09 20:34:53 +03004284 boot_order = machine_class->default_boot_order;
Markus Armbruster8281abd2013-06-14 13:15:03 +02004285 opts = qemu_opts_find(qemu_find_opts("boot-opts"), NULL);
4286 if (opts) {
4287 char *normal_boot_order;
4288 const char *order, *once;
4289
4290 order = qemu_opt_get(opts, "order");
4291 if (order) {
4292 validate_bootdevices(order);
4293 boot_order = order;
4294 }
4295
4296 once = qemu_opt_get(opts, "once");
4297 if (once) {
4298 validate_bootdevices(once);
4299 normal_boot_order = g_strdup(boot_order);
4300 boot_order = once;
Markus Armbruster083b79c2013-06-14 13:15:04 +02004301 qemu_register_reset(restore_boot_order, normal_boot_order);
Markus Armbruster8281abd2013-06-14 13:15:03 +02004302 }
4303
4304 boot_menu = qemu_opt_get_bool(opts, "menu", boot_menu);
Amos Konge5187b52013-12-09 19:53:15 +08004305 boot_strict = qemu_opt_get_bool(opts, "strict", false);
Markus Armbruster8281abd2013-06-14 13:15:03 +02004306 }
4307
Peter Maydella0abe472012-02-08 05:41:39 +00004308 if (!kernel_cmdline) {
4309 kernel_cmdline = "";
Marcel Apfelbaum6b1b1442014-05-26 15:40:58 +03004310 current_machine->kernel_cmdline = (char *)kernel_cmdline;
Peter Maydella0abe472012-02-08 05:41:39 +00004311 }
4312
bellarda20dd502003-09-30 21:07:02 +00004313 linux_boot = (kernel_filename != NULL);
balrog6c41b272007-11-17 12:12:29 +00004314
thsf8d39c02008-07-03 10:01:15 +00004315 if (!linux_boot && *kernel_cmdline != '\0') {
4316 fprintf(stderr, "-append only allowed with -kernel option\n");
4317 exit(1);
4318 }
4319
4320 if (!linux_boot && initrd_filename != NULL) {
4321 fprintf(stderr, "-initrd only allowed with -kernel option\n");
4322 exit(1);
4323 }
4324
Markus Armbruster2ff3de62013-07-04 15:09:22 +02004325 if (!linux_boot && qemu_opt_get(machine_opts, "dtb")) {
Grant Likely412beee2012-03-02 11:56:38 +00004326 fprintf(stderr, "-dtb only allowed with -kernel option\n");
4327 exit(1);
4328 }
4329
Jes Sorensen9156d762010-06-10 11:42:30 +02004330 os_set_line_buffering();
ths3b46e622007-09-17 08:09:54 +00004331
Paolo Bonzini49cf5722012-11-02 15:43:24 +01004332 qemu_init_cpu_loop();
4333 qemu_mutex_lock_iothread();
4334
Alon Levyad1be892012-03-14 20:33:37 +02004335#ifdef CONFIG_SPICE
4336 /* spice needs the timers to be initialized by this point */
4337 qemu_spice_init();
4338#endif
4339
Pavel Dovgalyuk4603ea02014-09-01 09:34:49 +04004340 cpu_ticks_init();
Sebastian Tanase1ad95802014-07-25 11:56:28 +02004341 if (icount_opts) {
4342 if (kvm_enabled() || xen_enabled()) {
4343 fprintf(stderr, "-icount is not allowed with kvm or xen\n");
4344 exit(1);
4345 }
4346 configure_icount(icount_opts, &error_abort);
4347 qemu_opts_del(icount_opts);
Max Filippov0abe9052011-11-10 15:38:42 +04004348 }
pbrook634fce92006-07-15 17:40:09 +00004349
Amos Kongf30dbae2012-12-11 22:20:15 +08004350 /* clean up network at qemu process termination */
4351 atexit(&net_cleanup);
4352
Mark McLoughlindc1c9fe2009-10-06 12:17:16 +01004353 if (net_init_clients() < 0) {
4354 exit(1);
bellard702c6512004-04-02 21:21:32 +00004355 }
bellardf1510b22003-06-25 00:07:40 +00004356
Stefan Bergerd1a0cf72013-02-27 12:47:49 -05004357#ifdef CONFIG_TPM
4358 if (tpm_init() < 0) {
4359 exit(1);
4360 }
4361#endif
4362
balrogdc72ac12008-11-09 00:04:26 +00004363 /* init the bluetooth world */
Gerd Hoffmannbd3c9482009-07-15 13:59:26 +02004364 if (foreach_device_config(DEV_BT, bt_parse))
4365 exit(1);
balrogdc72ac12008-11-09 00:04:26 +00004366
Anthony PERARD834e76e2011-07-20 08:17:44 +00004367 if (!xen_enabled()) {
4368 /* On 32-bit hosts, QEMU is limited by virtual address space */
4369 if (ram_size > (2047 << 20) && HOST_LONG_BITS == 32) {
4370 fprintf(stderr, "qemu: at most 2047 MB RAM can be simulated\n");
4371 exit(1);
4372 }
4373 }
balrog7fb4fdc2008-04-24 17:59:27 +00004374
lirans@il.ibm.comc163b5c2009-11-02 15:40:58 +02004375 blk_mig_init();
Dr. David Alan Gilbert0d6ab3a2014-03-19 18:32:30 +00004376 ram_mig_init();
lirans@il.ibm.comc163b5c2009-11-02 15:40:58 +02004377
ths96d30e42007-01-07 20:42:14 +00004378 /* open the virtual block devices */
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +02004379 if (snapshot)
Gerd Hoffmann3329f072010-08-20 13:52:01 +02004380 qemu_opts_foreach(qemu_find_opts("drive"), drive_enable_snapshot, NULL, 0);
Christian Borntraeger2d0d2832012-11-20 15:30:34 +01004381 if (qemu_opts_foreach(qemu_find_opts("drive"), drive_init_func,
Marcel Apfelbaum958db902014-04-09 20:34:53 +03004382 &machine_class->block_default_type, 1) != 0) {
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +02004383 exit(1);
Christian Borntraeger2d0d2832012-11-20 15:30:34 +01004384 }
balrog3e3d5812007-04-30 02:09:25 +00004385
Marcel Apfelbaum958db902014-04-09 20:34:53 +03004386 default_drive(default_cdrom, snapshot, machine_class->block_default_type, 2,
Christian Borntraeger3c42ea62012-11-22 21:02:55 +01004387 CDROM_OPTS);
4388 default_drive(default_floppy, snapshot, IF_FLOPPY, 0, FD_OPTS);
4389 default_drive(default_sdcard, snapshot, IF_SD, 0, SD_OPTS);
Markus Armbruster4e5d9b52011-01-28 11:21:45 +01004390
Wanlong Gao00421092014-05-14 17:43:08 +08004391 if (qemu_opts_foreach(qemu_find_opts("numa"), numa_init_func,
4392 NULL, 1) != 0) {
4393 exit(1);
4394 }
4395
Wanlong Gao96d0e262014-05-14 17:43:05 +08004396 set_numa_nodes();
aliguori268a3622009-04-21 22:30:27 +00004397
Gerd Hoffmann3329f072010-08-20 13:52:01 +02004398 if (qemu_opts_foreach(qemu_find_opts("mon"), mon_init_func, NULL, 1) != 0) {
Jan Kiszka157b9312010-04-06 16:55:53 +02004399 exit(1);
4400 }
4401
Gerd Hoffmann998bbd72009-12-08 13:11:41 +01004402 if (foreach_device_config(DEV_SERIAL, serial_parse) < 0)
4403 exit(1);
Gerd Hoffmann6a5e8b02009-12-08 13:11:42 +01004404 if (foreach_device_config(DEV_PARALLEL, parallel_parse) < 0)
4405 exit(1);
Gerd Hoffmannaee1b932009-12-08 13:11:53 +01004406 if (foreach_device_config(DEV_VIRTCON, virtcon_parse) < 0)
4407 exit(1);
Alexander Graf3ef669e2013-01-24 12:18:52 +01004408 if (foreach_device_config(DEV_SCLP, sclp_parse) < 0) {
4409 exit(1);
4410 }
H. Peter Anvinc9f398e2009-12-29 13:51:36 -08004411 if (foreach_device_config(DEV_DEBUGCON, debugcon_parse) < 0)
4412 exit(1);
aliguori2796dae2009-01-16 20:23:27 +00004413
Paolo Bonzini7f1b17f2012-05-10 09:39:17 +02004414 /* If no default VGA is requested, the default is "none". */
Aurelien Jarno3605ded2012-09-10 01:01:44 +02004415 if (default_vga) {
4416 if (cirrus_vga_available()) {
4417 vga_model = "cirrus";
4418 } else if (vga_available()) {
4419 vga_model = "std";
4420 }
Blue Swirla369da52011-09-27 19:15:42 +00004421 }
Mark Wud44229c2014-03-10 22:37:40 +08004422 if (vga_model) {
4423 select_vgahw(vga_model);
4424 }
Blue Swirla369da52011-09-27 19:15:42 +00004425
Markus Armbruster09aaa162009-08-21 10:31:34 +02004426 if (watchdog) {
4427 i = select_watchdog(watchdog);
4428 if (i > 0)
4429 exit (i == 1 ? 1 : 0);
4430 }
4431
Marcel Apfelbaum958db902014-04-09 20:34:53 +03004432 if (machine_class->compat_props) {
4433 qdev_prop_register_global_list(machine_class->compat_props);
Gerd Hoffmannb6b61142009-07-15 13:48:21 +02004434 }
Gerd Hoffmannd0fef6f2009-12-08 13:11:34 +01004435 qemu_add_globals();
4436
Anthony Liguori1de81d22011-12-19 16:37:46 -06004437 qdev_machine_init();
4438
Marcel Apfelbaum3ef96222014-05-07 17:42:57 +03004439 current_machine->ram_size = ram_size;
Igor Mammedovc270fb92014-06-02 15:25:02 +02004440 current_machine->maxram_size = maxram_size;
4441 current_machine->ram_slots = ram_slots;
Marcel Apfelbaum3ef96222014-05-07 17:42:57 +03004442 current_machine->boot_order = boot_order;
Marcel Apfelbaum3ef96222014-05-07 17:42:57 +03004443 current_machine->cpu_model = cpu_model;
Marcel Apfelbaum0056ae22014-03-05 19:30:47 +02004444
Marcel Apfelbaum3ef96222014-05-07 17:42:57 +03004445 machine_class->init(current_machine);
aliguori3023f332009-01-16 19:04:14 +00004446
zhanghailiang28d16f32014-09-23 18:42:24 +08004447 realtime_init();
4448
Paolo Bonzinib3e6d592013-04-18 18:44:04 +02004449 audio_init();
4450
Jan Kiszkaea375f92010-03-01 19:10:30 +01004451 cpu_synchronize_all_post_init();
aliguori268a3622009-04-21 22:30:27 +00004452
Blue Swirl87d0a282010-03-27 18:24:45 +00004453 set_numa_modes();
aliguori268a3622009-04-21 22:30:27 +00004454
aliguori3023f332009-01-16 19:04:14 +00004455 /* init USB devices */
zhlcindy@gmail.com094b2872012-09-02 19:25:28 +00004456 if (usb_enabled(false)) {
Markus Armbruster07527062009-10-06 12:16:57 +01004457 if (foreach_device_config(DEV_USB, usb_parse) < 0)
4458 exit(1);
aliguori3023f332009-01-16 19:04:14 +00004459 }
4460
Gerd Hoffmannbd3c9482009-07-15 13:59:26 +02004461 /* init generic devices */
Gerd Hoffmann3329f072010-08-20 13:52:01 +02004462 if (qemu_opts_foreach(qemu_find_opts("device"), device_init_func, NULL, 1) != 0)
Gerd Hoffmannbd3c9482009-07-15 13:59:26 +02004463 exit(1);
4464
John Snowa66c9dc2014-10-01 14:19:24 -04004465 /* Did we create any drives that we failed to create a device for? */
4466 drive_check_orphaned();
4467
Markus Armbruster668680f2010-02-11 14:44:58 +01004468 net_check_clients();
4469
Gerd Hoffmann64840c62013-03-07 17:08:29 +01004470 ds = init_displaystate();
Anthony Liguori993fbfd2009-05-21 16:54:00 -05004471
Gerd Hoffmann6b62dc22010-08-25 10:51:06 +02004472 /* init local displays */
Anthony Liguori993fbfd2009-05-21 16:54:00 -05004473 switch (display_type) {
4474 case DT_NOGRAPHIC:
Antony Pavlov8111d022013-10-29 08:36:31 +04004475 (void)ds; /* avoid warning if no display is configured */
Anthony Liguori993fbfd2009-05-21 16:54:00 -05004476 break;
4477#if defined(CONFIG_CURSES)
4478 case DT_CURSES:
Michael Tokarevab51b1d2012-12-30 12:48:14 +04004479 curses_display_init(ds, full_screen);
Anthony Liguori993fbfd2009-05-21 16:54:00 -05004480 break;
4481#endif
bellard5b0753e2005-03-01 21:37:28 +00004482#if defined(CONFIG_SDL)
Anthony Liguori993fbfd2009-05-21 16:54:00 -05004483 case DT_SDL:
4484 sdl_display_init(ds, full_screen, no_frame);
4485 break;
bellard5b0753e2005-03-01 21:37:28 +00004486#elif defined(CONFIG_COCOA)
Anthony Liguori993fbfd2009-05-21 16:54:00 -05004487 case DT_SDL:
4488 cocoa_display_init(ds, full_screen);
4489 break;
bellard313aa562003-08-10 21:52:11 +00004490#endif
Anthony Liguori15546422013-02-20 07:43:25 -06004491#if defined(CONFIG_GTK)
4492 case DT_GTK:
Jan Kiszka881249c2014-03-12 08:33:50 +01004493 gtk_display_init(ds, full_screen, grab_on_hover);
Anthony Liguori15546422013-02-20 07:43:25 -06004494 break;
4495#endif
Gerd Hoffmann6b62dc22010-08-25 10:51:06 +02004496 default:
4497 break;
4498 }
4499
Gleb Natapov0ce235a2011-03-31 11:27:23 +02004500 /* must be after terminal init, SDL library changes signal handlers */
4501 os_setup_signal_handling();
4502
Jes Sorensen821601e2011-03-16 13:33:36 +01004503#ifdef CONFIG_VNC
Gerd Hoffmann6b62dc22010-08-25 10:51:06 +02004504 /* init remote displays */
4505 if (vnc_display) {
Paolo Bonzini2d55f0e2012-10-02 10:17:21 +02004506 Error *local_err = NULL;
Anthony Liguori993fbfd2009-05-21 16:54:00 -05004507 vnc_display_init(ds);
Paolo Bonzini2d55f0e2012-10-02 10:17:21 +02004508 vnc_display_open(ds, vnc_display, &local_err);
4509 if (local_err != NULL) {
Seiji Aguchi4a44d852013-08-05 15:40:44 -04004510 error_report("Failed to start VNC server on `%s': %s",
4511 vnc_display, error_get_pretty(local_err));
Paolo Bonzini2d55f0e2012-10-02 10:17:21 +02004512 error_free(local_err);
Anthony Liguori993fbfd2009-05-21 16:54:00 -05004513 exit(1);
Amos Kong94b204c2012-06-30 10:02:20 +08004514 }
Anthony Liguorif92f8af2009-05-20 13:01:02 -05004515
Anthony Liguori993fbfd2009-05-21 16:54:00 -05004516 if (show_vnc_port) {
4517 printf("VNC server running on `%s'\n", vnc_display_local_addr(ds));
Anthony Liguorif92f8af2009-05-20 13:01:02 -05004518 }
bellard313aa562003-08-10 21:52:11 +00004519 }
Jes Sorensen821601e2011-03-16 13:33:36 +01004520#endif
Gerd Hoffmanna3e22262010-08-25 15:32:06 +02004521#ifdef CONFIG_SPICE
Gerd Hoffmann9fa03282013-10-11 22:39:59 +02004522 if (using_spice) {
4523 qemu_spice_display_init();
Gerd Hoffmanna3e22262010-08-25 15:32:06 +02004524 }
4525#endif
aliguori5b08fc12008-08-21 20:08:03 +00004526
Markus Armbrusteref0c4a02012-02-07 15:09:13 +01004527 if (foreach_device_config(DEV_GDB, gdbserver_start) < 0) {
aliguori59030a82009-04-05 18:43:41 +00004528 exit(1);
balrog45669e02007-07-02 13:20:17 +00004529 }
balrog45669e02007-07-02 13:20:17 +00004530
Gerd Hoffmann3418bd22009-09-25 21:42:41 +02004531 qdev_machine_creation_done();
4532
Gerd Hoffmann15ff7702009-12-14 16:07:35 +01004533 if (rom_load_all() != 0) {
4534 fprintf(stderr, "rom loading failed\n");
4535 exit(1);
4536 }
Gerd Hoffmann45a50b12009-10-01 16:42:33 +02004537
Isaku Yamahata80376c32010-12-20 14:33:35 +09004538 /* TODO: once all bus devices are qdevified, this should be done
4539 * when bus is created by qdev.c */
4540 qemu_register_reset(qbus_reset_all_fn, sysbus_get_default());
Gleb Natapov4cab9462010-12-08 13:35:08 +02004541 qemu_run_machine_init_done_notifiers();
4542
Michael S. Tsirkind916b462013-07-24 18:56:08 +03004543 /* Done notifiers can load ROMs */
4544 rom_load_done();
4545
Jan Kiszkae063eb12011-06-14 18:29:43 +02004546 qemu_system_reset(VMRESET_SILENT);
Juan Quintela05f24012009-08-20 19:42:22 +02004547 if (loadvm) {
Markus Armbruster03cd4652010-02-17 16:24:10 +01004548 if (load_vmstate(loadvm) < 0) {
Juan Quintela05f24012009-08-20 19:42:22 +02004549 autostart = 0;
4550 }
4551 }
bellardd63d3072004-10-03 13:29:03 +00004552
Eduardo Habkostd828c432014-08-08 16:03:30 -03004553 qdev_prop_check_globals();
Amit Shahabfd9ce2014-06-20 18:56:08 +05304554 if (vmstate_dump_file) {
4555 /* dump and exit */
4556 dump_vmstate_json_to_file(vmstate_dump_file);
4557 return 0;
4558 }
Don Slutz9f9260a2014-05-05 14:03:06 -04004559
Glauber Costa2bb8c102009-07-24 16:20:23 -04004560 if (incoming) {
Paolo Bonzini43eaae22012-10-02 18:21:18 +02004561 Error *local_err = NULL;
4562 qemu_start_incoming_migration(incoming, &local_err);
4563 if (local_err) {
Seiji Aguchi4a44d852013-08-05 15:40:44 -04004564 error_report("-incoming %s: %s", incoming,
4565 error_get_pretty(local_err));
Paolo Bonzini43eaae22012-10-02 18:21:18 +02004566 error_free(local_err);
4567 exit(1);
Juan Quintela8ca5e802010-06-09 14:10:54 +02004568 }
Avi Kivity6b99dad2009-08-09 14:39:20 +03004569 } else if (autostart) {
aliguoric0f4ce72009-03-05 23:01:01 +00004570 vm_start();
Avi Kivity6b99dad2009-08-09 14:39:20 +03004571 }
thsffd843b2006-12-21 19:46:43 +00004572
Jes Sorenseneb505be2010-06-10 11:42:28 +02004573 os_setup_post();
ths71e3ceb2006-12-22 02:11:31 +00004574
Michael Mueller8a745f22013-09-23 16:36:54 +02004575 if (is_daemonized()) {
LluĂ­s Vilanova5b808272014-05-27 15:02:14 +02004576 if (!trace_init_backends(trace_events, trace_file)) {
Michael Mueller8a745f22013-09-23 16:36:54 +02004577 exit(1);
4578 }
4579 }
4580
bellard8a7ddc32004-03-31 19:00:16 +00004581 main_loop();
Paolo Bonzini99435902011-09-12 14:03:13 +02004582 bdrv_close_all();
4583 pause_all_vcpus();
wayne3d3b8302011-07-27 18:04:55 +08004584 res_free();
Stefan Bergerd1a0cf72013-02-27 12:47:49 -05004585#ifdef CONFIG_TPM
4586 tpm_cleanup();
4587#endif
thsb46a8902007-10-21 23:20:45 +00004588
bellard0824d6f2003-06-24 13:42:40 +00004589 return 0;
4590}