blob: 862cf20f3cb42fe8356ae6f747bcea8c4b7063af [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
blueswir1511d2b12009-03-07 15:32:56 +000061#include "hw/hw.h"
62#include "hw/boards.h"
63#include "hw/usb.h"
64#include "hw/pcmcia.h"
Paolo Bonzini0d09e412013-02-05 17:06:20 +010065#include "hw/i386/pc.h"
66#include "hw/isa/isa.h"
blueswir1511d2b12009-03-07 15:32:56 +000067#include "hw/bt.h"
Paolo Bonzini0d09e412013-02-05 17:06:20 +010068#include "sysemu/watchdog.h"
69#include "hw/i386/smbios.h"
70#include "hw/xen/xen.h"
Gerd Hoffmannbd3c9482009-07-15 13:59:26 +020071#include "hw/qdev.h"
Gerd Hoffmann45a50b12009-10-01 16:42:33 +020072#include "hw/loader.h"
Paolo Bonzinib4a42f82013-02-04 11:37:52 +010073#include "monitor/qdev.h"
Paolo Bonzinidccfcd02013-04-08 16:55:25 +020074#include "sysemu/bt.h"
Paolo Bonzini1422e322012-10-24 08:43:34 +020075#include "net/net.h"
Mark McLoughlin68ac40d2009-11-25 18:48:54 +000076#include "net/slirp.h"
Paolo Bonzini83c90892012-12-17 18:19:49 +010077#include "monitor/monitor.h"
Paolo Bonzini28ecbae2012-11-28 12:06:30 +010078#include "ui/console.h"
Paolo Bonzini9c17d612012-12-17 18:20:04 +010079#include "sysemu/sysemu.h"
Paolo Bonzini022c62c2012-12-17 18:19:49 +010080#include "exec/gdbstub.h"
Paolo Bonzini1de7afc2012-12-17 18:20:00 +010081#include "qemu/timer.h"
Paolo Bonzinidccfcd02013-04-08 16:55:25 +020082#include "sysemu/char.h"
Michael Tokarevb33276a2014-02-07 12:26:14 +040083#include "qemu/bitmap.h"
Paolo Bonzini1de7afc2012-12-17 18:20:00 +010084#include "qemu/cache-utils.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
Paolo Bonzini1de7afc2012-12-17 18:20:00 +0100106#include "qemu/sockets.h"
blueswir1511d2b12009-03-07 15:32:56 +0000107
Jan Kiszkad918f232009-06-24 14:42:30 +0200108#include "slirp/libslirp.h"
blueswir1511d2b12009-03-07 15:32:56 +0000109
Stefan Hajnoczi94b0b5f2010-11-16 12:20:25 +0000110#include "trace.h"
Lluíse4858972011-08-31 20:31:03 +0200111#include "trace/control.h"
Paolo Bonzini1de7afc2012-12-17 18:20:00 +0100112#include "qemu/queue.h"
Paolo Bonzini9c17d612012-12-17 18:20:04 +0100113#include "sysemu/cpus.h"
114#include "sysemu/arch_init.h"
Paolo Bonzini1de7afc2012-12-17 18:20:00 +0100115#include "qemu/osdep.h"
Blue Swirl72cf2d42009-09-12 07:36:22 +0000116
Gerd Hoffmann29b00402010-03-11 11:13:27 -0300117#include "ui/qemu-spice.h"
Anthony Liguori68d98d32012-06-25 14:36:33 -0500118#include "qapi/string-input-visitor.h"
Igor Mammedov269e09f2014-01-16 17:34:38 +0100119#include "qom/object_interfaces.h"
Gerd Hoffmann29b00402010-03-11 11:13:27 -0300120
bellard1bfe8562004-07-08 21:17:50 +0000121#define DEFAULT_RAM_SIZE 128
bellard313aa562003-08-10 21:52:11 +0000122
Amit Shah98b19252010-01-20 00:36:52 +0530123#define MAX_VIRTIO_CONSOLES 1
Alexander Graf3ef669e2013-01-24 12:18:52 +0100124#define MAX_SCLP_CONSOLES 1
Amit Shah98b19252010-01-20 00:36:52 +0530125
Gerd Hoffmann45240512013-03-08 11:42:24 +0100126static const char *data_dir[16];
127static int data_dir_idx;
j_mayer1192dad2007-10-05 13:08:35 +0000128const char *bios_name = NULL;
malccb5a7aa2008-09-28 00:42:12 +0000129enum vga_retrace_method vga_retrace_method = VGA_RETRACE_DUMB;
Anthony Liguori993fbfd2009-05-21 16:54:00 -0500130DisplayType display_type = DT_DEFAULT;
Blue Swirl4fdcac02012-10-28 11:04:47 +0000131static int display_remote;
bellard3d11d0e2004-12-12 16:56:30 +0000132const char* keyboard_layout = NULL;
Anthony Liguoric227f092009-10-01 16:12:16 -0500133ram_addr_t ram_size;
Marcelo Tosattic9027602010-03-01 20:25:08 -0300134const char *mem_path = NULL;
Marcelo Tosattic9027602010-03-01 20:25:08 -0300135int mem_prealloc = 0; /* force preallocation of physical target memory */
bellardc4b1fcc2004-03-14 21:44:30 +0000136int nb_nics;
bellard7c9d8e02005-11-15 22:16:05 +0000137NICInfo nd_table[MAX_NICS];
Paolo Bonzinid399f672009-07-27 23:17:51 +0200138int autostart;
balrogf6503052008-02-17 11:42:19 +0000139static int rtc_utc = 1;
140static int rtc_date_offset = -1; /* -1 means no change */
Alex Bligh884f17c2013-08-21 16:03:04 +0100141QEMUClockType rtc_clock;
Gerd Hoffmann64465292009-12-08 13:11:45 +0100142int vga_interface_type = VGA_NONE;
blueswir1dbed7e42008-10-01 19:38:09 +0000143static int full_screen = 0;
144static int no_frame = 0;
ths667acca2006-12-11 02:08:05 +0000145int no_quit = 0;
bellard8d11df92004-08-24 21:13:40 +0000146CharDriverState *serial_hds[MAX_SERIAL_PORTS];
bellard6508fe52005-01-15 12:02:56 +0000147CharDriverState *parallel_hds[MAX_PARALLEL_PORTS];
aliguori9ede2fd2009-01-15 20:05:25 +0000148CharDriverState *virtcon_hds[MAX_VIRTIO_CONSOLES];
Alexander Graf3ef669e2013-01-24 12:18:52 +0100149CharDriverState *sclp_hds[MAX_SCLP_CONSOLES];
bellarda09db212005-04-30 16:10:35 +0000150int win2k_install_hack = 0;
aurel321b530a62009-04-05 20:08:59 +0000151int singlestep = 0;
bellard6a00d602005-11-21 23:25:50 +0000152int smp_cpus = 1;
Jes Sorensen6be68d72009-07-23 17:03:42 +0200153int max_cpus = 0;
Andre Przywaradc6b1c02009-08-19 15:42:40 +0200154int smp_cores = 1;
155int smp_threads = 1;
Jes Sorensen821601e2011-03-16 13:33:36 +0100156#ifdef CONFIG_VNC
ths73fc9742006-12-22 02:09:07 +0000157const char *vnc_display;
Jes Sorensen821601e2011-03-16 13:33:36 +0100158#endif
bellard6515b202006-05-03 22:02:44 +0000159int acpi_enabled = 1;
aliguori16b29ae2008-12-17 23:28:44 +0000160int no_hpet = 0;
bellard52ca8d62006-06-14 16:03:05 +0000161int fd_bootchk = 1;
Blue Swirl4fdcac02012-10-28 11:04:47 +0000162static int no_reboot;
aurel32b2f76162008-04-11 21:35:52 +0000163int no_shutdown = 0;
balrog9467cd42007-05-01 01:34:14 +0000164int cursor_hide = 1;
balroga171fe32007-04-30 01:48:07 +0000165int graphic_rotate = 0;
Markus Armbruster09aaa162009-08-21 10:31:34 +0200166const char *watchdog;
Gleb Natapov2e55e842010-12-08 13:35:07 +0200167QEMUOptionRom option_rom[MAX_OPTION_ROMS];
ths9ae02552007-01-05 17:39:04 +0000168int nb_option_roms;
pbrook8e716212007-01-20 17:12:09 +0000169int semihosting_enabled = 0;
balrog2b8f2d42007-07-27 22:08:46 +0000170int old_param = 0;
thsc35734b2007-03-19 15:17:08 +0000171const char *qemu_name;
ths3780e192007-06-21 21:08:02 +0000172int alt_grab = 0;
Dustin Kirkland0ca9f8a2009-09-17 15:48:04 -0500173int ctrl_grab = 0;
blueswir166508602007-05-01 14:16:52 +0000174unsigned int nb_prom_envs = 0;
175const char *prom_envs[MAX_PROM_ENVS];
Jan Kiszka95387492009-07-02 00:19:02 +0200176int boot_menu;
Michael Tokarev6f2bfda2013-12-09 12:18:46 +0400177static bool boot_strict;
wayne3d3b8302011-07-27 18:04:55 +0800178uint8_t *boot_splash_filedata;
Markus Armbrusterd09acb92013-01-23 18:25:08 +0100179size_t boot_splash_filedata_size;
wayne3d3b8302011-07-27 18:04:55 +0800180uint8_t qemu_extra_params_fw[2];
bellard0824d6f2003-06-24 13:42:40 +0000181
Gleb Natapov1ca4d092010-12-08 13:35:05 +0200182typedef struct FWBootEntry FWBootEntry;
183
184struct FWBootEntry {
185 QTAILQ_ENTRY(FWBootEntry) link;
186 int32_t bootindex;
187 DeviceState *dev;
188 char *suffix;
189};
190
Blue Swirl4fdcac02012-10-28 11:04:47 +0000191static QTAILQ_HEAD(, FWBootEntry) fw_boot_order =
192 QTAILQ_HEAD_INITIALIZER(fw_boot_order);
Gleb Natapov1ca4d092010-12-08 13:35:05 +0200193
aliguori268a3622009-04-21 22:30:27 +0000194int nb_numa_nodes;
195uint64_t node_mem[MAX_NODES];
Chegu Vinodee785fe2012-07-16 21:31:30 -0700196unsigned long *node_cpumask[MAX_NODES];
aliguori268a3622009-04-21 22:30:27 +0000197
blueswir18fcb1b92008-09-18 18:29:08 +0000198uint8_t qemu_uuid[16];
Markus Armbrusterfc3b3292013-08-16 15:18:31 +0200199bool qemu_uuid_set;
blueswir18fcb1b92008-09-18 18:29:08 +0000200
Jan Kiszka76e30d02009-07-02 00:19:02 +0200201static QEMUBootSetHandler *boot_set_handler;
202static void *boot_set_opaque;
203
Gerd Hoffmannfd42dee2010-06-04 14:08:07 +0200204static NotifierList exit_notifiers =
205 NOTIFIER_LIST_INITIALIZER(exit_notifiers);
206
Gleb Natapov4cab9462010-12-08 13:35:08 +0200207static NotifierList machine_init_done_notifiers =
208 NOTIFIER_LIST_INITIALIZER(machine_init_done_notifiers);
209
liguangd5286af2013-01-24 13:03:27 +0800210static bool tcg_allowed = true;
liguangd5286af2013-01-24 13:03:27 +0800211bool xen_allowed;
Blue Swirld745bef2010-03-29 19:23:49 +0000212uint32_t xen_domid;
213enum xen_mode xen_mode = XEN_EMULATE;
Jan Kiszkad5ab9712011-08-02 16:10:21 +0200214static int tcg_tb_size;
Blue Swirld745bef2010-03-29 19:23:49 +0000215
Gerd Hoffmann998bbd72009-12-08 13:11:41 +0100216static int default_serial = 1;
Gerd Hoffmann6a5e8b02009-12-08 13:11:42 +0100217static int default_parallel = 1;
Gerd Hoffmann986c5f72009-12-08 13:11:54 +0100218static int default_virtcon = 1;
Alexander Graf3ef669e2013-01-24 12:18:52 +0100219static int default_sclp = 1;
Gerd Hoffmannabdeed02009-12-08 13:11:43 +0100220static int default_monitor = 1;
Gerd Hoffmannac33f8f2009-12-16 14:25:39 +0100221static int default_floppy = 1;
222static int default_cdrom = 1;
223static int default_sdcard = 1;
Paolo Bonzini7f1b17f2012-05-10 09:39:17 +0200224static int default_vga = 1;
Gerd Hoffmann998bbd72009-12-08 13:11:41 +0100225
226static struct {
227 const char *driver;
228 int *flag;
229} default_list[] = {
Gerd Hoffmann6a5e8b02009-12-08 13:11:42 +0100230 { .driver = "isa-serial", .flag = &default_serial },
231 { .driver = "isa-parallel", .flag = &default_parallel },
Gerd Hoffmannd8bcbab2009-12-16 14:25:40 +0100232 { .driver = "isa-fdc", .flag = &default_floppy },
Markus Armbrusteraf6bf132011-05-18 18:31:02 +0200233 { .driver = "ide-cd", .flag = &default_cdrom },
234 { .driver = "ide-hd", .flag = &default_cdrom },
Gerd Hoffmannd8bcbab2009-12-16 14:25:40 +0100235 { .driver = "ide-drive", .flag = &default_cdrom },
Markus Armbrusteraf6bf132011-05-18 18:31:02 +0200236 { .driver = "scsi-cd", .flag = &default_cdrom },
Amit Shah392ecf52010-01-21 16:19:23 +0530237 { .driver = "virtio-serial-pci", .flag = &default_virtcon },
238 { .driver = "virtio-serial-s390", .flag = &default_virtcon },
239 { .driver = "virtio-serial", .flag = &default_virtcon },
Paolo Bonzini7f1b17f2012-05-10 09:39:17 +0200240 { .driver = "VGA", .flag = &default_vga },
241 { .driver = "isa-vga", .flag = &default_vga },
242 { .driver = "cirrus-vga", .flag = &default_vga },
243 { .driver = "isa-cirrus-vga", .flag = &default_vga },
244 { .driver = "vmware-svga", .flag = &default_vga },
245 { .driver = "qxl-vga", .flag = &default_vga },
Gerd Hoffmann998bbd72009-12-08 13:11:41 +0100246};
247
Paolo Bonzini4d454572012-11-26 16:03:42 +0100248static QemuOptsList qemu_rtc_opts = {
249 .name = "rtc",
250 .head = QTAILQ_HEAD_INITIALIZER(qemu_rtc_opts.head),
251 .desc = {
252 {
253 .name = "base",
254 .type = QEMU_OPT_STRING,
255 },{
256 .name = "clock",
257 .type = QEMU_OPT_STRING,
258 },{
259 .name = "driftfix",
260 .type = QEMU_OPT_STRING,
261 },
262 { /* end of list */ }
263 },
264};
265
266static QemuOptsList qemu_sandbox_opts = {
267 .name = "sandbox",
268 .implied_opt_name = "enable",
269 .head = QTAILQ_HEAD_INITIALIZER(qemu_sandbox_opts.head),
270 .desc = {
271 {
272 .name = "enable",
273 .type = QEMU_OPT_BOOL,
274 },
275 { /* end of list */ }
276 },
277};
278
279static QemuOptsList qemu_trace_opts = {
280 .name = "trace",
281 .implied_opt_name = "trace",
282 .head = QTAILQ_HEAD_INITIALIZER(qemu_trace_opts.head),
283 .desc = {
284 {
285 .name = "events",
286 .type = QEMU_OPT_STRING,
287 },{
288 .name = "file",
289 .type = QEMU_OPT_STRING,
290 },
291 { /* end of list */ }
292 },
293};
294
295static QemuOptsList qemu_option_rom_opts = {
296 .name = "option-rom",
297 .implied_opt_name = "romfile",
298 .head = QTAILQ_HEAD_INITIALIZER(qemu_option_rom_opts.head),
299 .desc = {
300 {
301 .name = "bootindex",
302 .type = QEMU_OPT_NUMBER,
303 }, {
304 .name = "romfile",
305 .type = QEMU_OPT_STRING,
306 },
307 { /* end of list */ }
308 },
309};
310
311static QemuOptsList qemu_machine_opts = {
312 .name = "machine",
313 .implied_opt_name = "type",
314 .merge_lists = true,
315 .head = QTAILQ_HEAD_INITIALIZER(qemu_machine_opts.head),
316 .desc = {
317 {
318 .name = "type",
319 .type = QEMU_OPT_STRING,
320 .help = "emulated machine"
321 }, {
322 .name = "accel",
323 .type = QEMU_OPT_STRING,
324 .help = "accelerator list",
325 }, {
326 .name = "kernel_irqchip",
327 .type = QEMU_OPT_BOOL,
328 .help = "use KVM in-kernel irqchip",
329 }, {
330 .name = "kvm_shadow_mem",
331 .type = QEMU_OPT_SIZE,
332 .help = "KVM shadow MMU size",
333 }, {
334 .name = "kernel",
335 .type = QEMU_OPT_STRING,
336 .help = "Linux kernel image file",
337 }, {
338 .name = "initrd",
339 .type = QEMU_OPT_STRING,
340 .help = "Linux initial ramdisk file",
341 }, {
342 .name = "append",
343 .type = QEMU_OPT_STRING,
344 .help = "Linux kernel command line",
345 }, {
346 .name = "dtb",
347 .type = QEMU_OPT_STRING,
348 .help = "Linux kernel device tree file",
349 }, {
350 .name = "dumpdtb",
351 .type = QEMU_OPT_STRING,
352 .help = "Dump current dtb to a file and quit",
353 }, {
354 .name = "phandle_start",
Markus Armbrusterc1b71b02013-07-04 15:09:23 +0200355 .type = QEMU_OPT_NUMBER,
Paolo Bonzini4d454572012-11-26 16:03:42 +0100356 .help = "The first phandle ID we may generate dynamically",
357 }, {
358 .name = "dt_compatible",
359 .type = QEMU_OPT_STRING,
360 .help = "Overrides the \"compatible\" property of the dt root node",
361 }, {
362 .name = "dump-guest-core",
363 .type = QEMU_OPT_BOOL,
364 .help = "Include guest memory in a core dump",
365 }, {
366 .name = "mem-merge",
367 .type = QEMU_OPT_BOOL,
368 .help = "enable/disable memory merge support",
369 },{
370 .name = "usb",
371 .type = QEMU_OPT_BOOL,
372 .help = "Set on/off to enable/disable usb",
Gerd Hoffmann1442d3e2013-10-15 16:57:45 +0200373 },{
374 .name = "firmware",
375 .type = QEMU_OPT_STRING,
376 .help = "firmware image",
Aneesh Kumar K.V135a1292013-12-23 21:10:40 +0530377 },{
378 .name = "kvm-type",
379 .type = QEMU_OPT_STRING,
380 .help = "Specifies the KVM virtualization mode (HV, PR)",
Paolo Bonzini4d454572012-11-26 16:03:42 +0100381 },
382 { /* End of list */ }
383 },
384};
385
386static QemuOptsList qemu_boot_opts = {
387 .name = "boot-opts",
Markus Armbruster6ef47162013-06-14 13:15:01 +0200388 .implied_opt_name = "order",
389 .merge_lists = true,
Paolo Bonzini4d454572012-11-26 16:03:42 +0100390 .head = QTAILQ_HEAD_INITIALIZER(qemu_boot_opts.head),
391 .desc = {
Paolo Bonzini4d454572012-11-26 16:03:42 +0100392 {
393 .name = "order",
394 .type = QEMU_OPT_STRING,
395 }, {
396 .name = "once",
397 .type = QEMU_OPT_STRING,
398 }, {
399 .name = "menu",
Markus Armbruster6ef47162013-06-14 13:15:01 +0200400 .type = QEMU_OPT_BOOL,
Paolo Bonzini4d454572012-11-26 16:03:42 +0100401 }, {
402 .name = "splash",
403 .type = QEMU_OPT_STRING,
404 }, {
405 .name = "splash-time",
406 .type = QEMU_OPT_STRING,
407 }, {
408 .name = "reboot-timeout",
409 .type = QEMU_OPT_STRING,
Amos Kongc8a6ae82013-03-19 14:23:27 +0800410 }, {
411 .name = "strict",
Amos Konge5187b52013-12-09 19:53:15 +0800412 .type = QEMU_OPT_BOOL,
Paolo Bonzini4d454572012-11-26 16:03:42 +0100413 },
414 { /*End of list */ }
415 },
416};
417
418static QemuOptsList qemu_add_fd_opts = {
419 .name = "add-fd",
420 .head = QTAILQ_HEAD_INITIALIZER(qemu_add_fd_opts.head),
421 .desc = {
422 {
423 .name = "fd",
424 .type = QEMU_OPT_NUMBER,
425 .help = "file descriptor of which a duplicate is added to fd set",
426 },{
427 .name = "set",
428 .type = QEMU_OPT_NUMBER,
429 .help = "ID of the fd set to add fd to",
430 },{
431 .name = "opaque",
432 .type = QEMU_OPT_STRING,
433 .help = "free-form string used to describe fd",
434 },
435 { /* end of list */ }
436 },
437};
438
439static QemuOptsList qemu_object_opts = {
440 .name = "object",
441 .implied_opt_name = "qom-type",
442 .head = QTAILQ_HEAD_INITIALIZER(qemu_object_opts.head),
443 .desc = {
444 { }
445 },
446};
447
Stefan Bergerd1a0cf72013-02-27 12:47:49 -0500448static QemuOptsList qemu_tpmdev_opts = {
449 .name = "tpmdev",
450 .implied_opt_name = "type",
451 .head = QTAILQ_HEAD_INITIALIZER(qemu_tpmdev_opts.head),
452 .desc = {
Stefan Bergerbb716232013-04-22 10:41:39 -0400453 /* options are defined in the TPM backends */
Stefan Bergerd1a0cf72013-02-27 12:47:49 -0500454 { /* end of list */ }
455 },
456};
457
Satoru Moriya888a6bc2013-04-19 16:42:06 +0200458static QemuOptsList qemu_realtime_opts = {
459 .name = "realtime",
460 .head = QTAILQ_HEAD_INITIALIZER(qemu_realtime_opts.head),
461 .desc = {
462 {
463 .name = "mlock",
464 .type = QEMU_OPT_BOOL,
465 },
466 { /* end of list */ }
467 },
468};
469
Seiji Aguchi5e2ac512013-07-03 23:02:46 -0400470static QemuOptsList qemu_msg_opts = {
471 .name = "msg",
472 .head = QTAILQ_HEAD_INITIALIZER(qemu_msg_opts.head),
473 .desc = {
474 {
475 .name = "timestamp",
476 .type = QEMU_OPT_BOOL,
477 },
478 { /* end of list */ }
479 },
480};
481
Dr. David Alan Gilbert5d12f962014-01-30 10:20:30 +0000482static QemuOptsList qemu_name_opts = {
483 .name = "name",
484 .implied_opt_name = "guest",
485 .merge_lists = true,
486 .head = QTAILQ_HEAD_INITIALIZER(qemu_name_opts.head),
487 .desc = {
488 {
489 .name = "guest",
490 .type = QEMU_OPT_STRING,
491 .help = "Sets the name of the guest.\n"
492 "This name will be displayed in the SDL window caption.\n"
493 "The name will also be used for the VNC server",
494 }, {
495 .name = "process",
496 .type = QEMU_OPT_STRING,
497 .help = "Sets the name of the QEMU process, as shown in top etc",
Dr. David Alan Gilbert8f480de2014-01-30 10:20:31 +0000498 }, {
499 .name = "debug-threads",
500 .type = QEMU_OPT_BOOL,
501 .help = "When enabled, name the individual threads; defaults off.\n"
502 "NOTE: The thread names are for debugging and not a\n"
503 "stable API.",
Dr. David Alan Gilbert5d12f962014-01-30 10:20:30 +0000504 },
505 { /* End of list */ }
506 },
507};
508
Markus Armbruster7f9d6e52013-07-04 15:09:19 +0200509/**
510 * Get machine options
511 *
512 * Returns: machine options (never null).
513 */
514QemuOpts *qemu_get_machine_opts(void)
515{
516 QemuOptsList *list;
517 QemuOpts *opts;
518
519 list = qemu_find_opts("machine");
520 assert(list);
521 opts = qemu_opts_find(list, NULL);
522 if (!opts) {
Peter Crosthwaite87ea75d2014-01-01 18:49:17 -0800523 opts = qemu_opts_create(list, NULL, 0, &error_abort);
Markus Armbruster7f9d6e52013-07-04 15:09:19 +0200524 }
525 return opts;
526}
527
Ronnie Sahlberg31459f42012-08-06 18:24:55 +1000528const char *qemu_get_vm_name(void)
529{
530 return qemu_name;
531}
532
wayne3d3b8302011-07-27 18:04:55 +0800533static void res_free(void)
534{
535 if (boot_splash_filedata != NULL) {
Anthony Liguori7267c092011-08-20 22:09:37 -0500536 g_free(boot_splash_filedata);
wayne3d3b8302011-07-27 18:04:55 +0800537 boot_splash_filedata = NULL;
538 }
539}
540
Gerd Hoffmann998bbd72009-12-08 13:11:41 +0100541static int default_driver_check(QemuOpts *opts, void *opaque)
542{
543 const char *driver = qemu_opt_get(opts, "driver");
544 int i;
545
546 if (!driver)
547 return 0;
548 for (i = 0; i < ARRAY_SIZE(default_list); i++) {
549 if (strcmp(default_list[i].driver, driver) != 0)
550 continue;
551 *(default_list[i].flag) = 0;
552 }
553 return 0;
554}
555
bellard0824d6f2003-06-24 13:42:40 +0000556/***********************************************************/
Luiz Capitulinof5bbfba2011-07-29 15:04:45 -0300557/* QEMU state */
558
Luiz Capitulino0461d5a2011-09-30 14:45:27 -0300559static RunState current_run_state = RUN_STATE_PRELAUNCH;
Luiz Capitulinof5bbfba2011-07-29 15:04:45 -0300560
Luiz Capitulino5db9d4d2011-09-05 16:36:31 -0300561typedef struct {
562 RunState from;
563 RunState to;
564} RunStateTransition;
565
566static const RunStateTransition runstate_transitions_def[] = {
567 /* from -> to */
Luiz Capitulino0461d5a2011-09-30 14:45:27 -0300568 { RUN_STATE_DEBUG, RUN_STATE_RUNNING },
Paolo Bonzinieca01d32013-12-03 13:00:15 +0100569 { RUN_STATE_DEBUG, RUN_STATE_FINISH_MIGRATE },
Luiz Capitulino5db9d4d2011-09-05 16:36:31 -0300570
Luiz Capitulino0461d5a2011-09-30 14:45:27 -0300571 { RUN_STATE_INMIGRATE, RUN_STATE_RUNNING },
Paolo Bonzini29ed72f2012-10-19 16:45:24 +0200572 { RUN_STATE_INMIGRATE, RUN_STATE_PAUSED },
Luiz Capitulino5db9d4d2011-09-05 16:36:31 -0300573
Luiz Capitulino0461d5a2011-09-30 14:45:27 -0300574 { RUN_STATE_INTERNAL_ERROR, RUN_STATE_PAUSED },
Luiz Capitulino8a9236f2011-10-14 11:18:09 -0300575 { RUN_STATE_INTERNAL_ERROR, RUN_STATE_FINISH_MIGRATE },
Luiz Capitulino5db9d4d2011-09-05 16:36:31 -0300576
Luiz Capitulino0461d5a2011-09-30 14:45:27 -0300577 { RUN_STATE_IO_ERROR, RUN_STATE_RUNNING },
Luiz Capitulino8a9236f2011-10-14 11:18:09 -0300578 { RUN_STATE_IO_ERROR, RUN_STATE_FINISH_MIGRATE },
Luiz Capitulino5db9d4d2011-09-05 16:36:31 -0300579
Luiz Capitulino0461d5a2011-09-30 14:45:27 -0300580 { RUN_STATE_PAUSED, RUN_STATE_RUNNING },
Luiz Capitulino8a9236f2011-10-14 11:18:09 -0300581 { RUN_STATE_PAUSED, RUN_STATE_FINISH_MIGRATE },
Luiz Capitulino5db9d4d2011-09-05 16:36:31 -0300582
Luiz Capitulino0461d5a2011-09-30 14:45:27 -0300583 { RUN_STATE_POSTMIGRATE, RUN_STATE_RUNNING },
Luiz Capitulino8a9236f2011-10-14 11:18:09 -0300584 { RUN_STATE_POSTMIGRATE, RUN_STATE_FINISH_MIGRATE },
Luiz Capitulino5db9d4d2011-09-05 16:36:31 -0300585
Luiz Capitulino0461d5a2011-09-30 14:45:27 -0300586 { RUN_STATE_PRELAUNCH, RUN_STATE_RUNNING },
Luiz Capitulino8a9236f2011-10-14 11:18:09 -0300587 { RUN_STATE_PRELAUNCH, RUN_STATE_FINISH_MIGRATE },
Luiz Capitulino0461d5a2011-09-30 14:45:27 -0300588 { RUN_STATE_PRELAUNCH, RUN_STATE_INMIGRATE },
Luiz Capitulino5db9d4d2011-09-05 16:36:31 -0300589
Luiz Capitulino0461d5a2011-09-30 14:45:27 -0300590 { RUN_STATE_FINISH_MIGRATE, RUN_STATE_RUNNING },
591 { RUN_STATE_FINISH_MIGRATE, RUN_STATE_POSTMIGRATE },
Luiz Capitulino5db9d4d2011-09-05 16:36:31 -0300592
Luiz Capitulino0461d5a2011-09-30 14:45:27 -0300593 { RUN_STATE_RESTORE_VM, RUN_STATE_RUNNING },
Luiz Capitulino5db9d4d2011-09-05 16:36:31 -0300594
Luiz Capitulino0461d5a2011-09-30 14:45:27 -0300595 { RUN_STATE_RUNNING, RUN_STATE_DEBUG },
596 { RUN_STATE_RUNNING, RUN_STATE_INTERNAL_ERROR },
597 { RUN_STATE_RUNNING, RUN_STATE_IO_ERROR },
598 { RUN_STATE_RUNNING, RUN_STATE_PAUSED },
599 { RUN_STATE_RUNNING, RUN_STATE_FINISH_MIGRATE },
600 { RUN_STATE_RUNNING, RUN_STATE_RESTORE_VM },
601 { RUN_STATE_RUNNING, RUN_STATE_SAVE_VM },
602 { RUN_STATE_RUNNING, RUN_STATE_SHUTDOWN },
603 { RUN_STATE_RUNNING, RUN_STATE_WATCHDOG },
Hu Taoede085b2013-04-26 11:24:40 +0800604 { RUN_STATE_RUNNING, RUN_STATE_GUEST_PANICKED },
Luiz Capitulino5db9d4d2011-09-05 16:36:31 -0300605
Luiz Capitulino0461d5a2011-09-30 14:45:27 -0300606 { RUN_STATE_SAVE_VM, RUN_STATE_RUNNING },
Luiz Capitulino5db9d4d2011-09-05 16:36:31 -0300607
Luiz Capitulino0461d5a2011-09-30 14:45:27 -0300608 { RUN_STATE_SHUTDOWN, RUN_STATE_PAUSED },
Luiz Capitulino8a9236f2011-10-14 11:18:09 -0300609 { RUN_STATE_SHUTDOWN, RUN_STATE_FINISH_MIGRATE },
Luiz Capitulino5db9d4d2011-09-05 16:36:31 -0300610
Luiz Capitulinoad02b962012-04-27 13:33:36 -0300611 { RUN_STATE_DEBUG, RUN_STATE_SUSPENDED },
612 { RUN_STATE_RUNNING, RUN_STATE_SUSPENDED },
613 { RUN_STATE_SUSPENDED, RUN_STATE_RUNNING },
614 { RUN_STATE_SUSPENDED, RUN_STATE_FINISH_MIGRATE },
615
Luiz Capitulino0461d5a2011-09-30 14:45:27 -0300616 { RUN_STATE_WATCHDOG, RUN_STATE_RUNNING },
Luiz Capitulino8a9236f2011-10-14 11:18:09 -0300617 { RUN_STATE_WATCHDOG, RUN_STATE_FINISH_MIGRATE },
Luiz Capitulino5db9d4d2011-09-05 16:36:31 -0300618
Paolo Bonzinidf390762013-11-04 14:30:47 +0100619 { RUN_STATE_GUEST_PANICKED, RUN_STATE_RUNNING },
Hu Taofd2a2e12013-05-20 12:46:20 +0200620 { RUN_STATE_GUEST_PANICKED, RUN_STATE_FINISH_MIGRATE },
Hu Taoede085b2013-04-26 11:24:40 +0800621
Luiz Capitulino0461d5a2011-09-30 14:45:27 -0300622 { RUN_STATE_MAX, RUN_STATE_MAX },
Luiz Capitulino5db9d4d2011-09-05 16:36:31 -0300623};
624
Luiz Capitulino0461d5a2011-09-30 14:45:27 -0300625static bool runstate_valid_transitions[RUN_STATE_MAX][RUN_STATE_MAX];
Luiz Capitulino5db9d4d2011-09-05 16:36:31 -0300626
Luiz Capitulinof5bbfba2011-07-29 15:04:45 -0300627bool runstate_check(RunState state)
628{
629 return current_run_state == state;
630}
631
Blue Swirl4fdcac02012-10-28 11:04:47 +0000632static void runstate_init(void)
Luiz Capitulino5db9d4d2011-09-05 16:36:31 -0300633{
634 const RunStateTransition *p;
635
636 memset(&runstate_valid_transitions, 0, sizeof(runstate_valid_transitions));
637
Luiz Capitulino0461d5a2011-09-30 14:45:27 -0300638 for (p = &runstate_transitions_def[0]; p->from != RUN_STATE_MAX; p++) {
Luiz Capitulino5db9d4d2011-09-05 16:36:31 -0300639 runstate_valid_transitions[p->from][p->to] = true;
640 }
641}
642
643/* This function will abort() on invalid state transitions */
Luiz Capitulinof5bbfba2011-07-29 15:04:45 -0300644void runstate_set(RunState new_state)
645{
Luiz Capitulino207c5cd2011-10-13 10:59:07 -0300646 assert(new_state < RUN_STATE_MAX);
647
648 if (!runstate_valid_transitions[current_run_state][new_state]) {
649 fprintf(stderr, "ERROR: invalid runstate transition: '%s' -> '%s'\n",
650 RunState_lookup[current_run_state],
651 RunState_lookup[new_state]);
Luiz Capitulino5db9d4d2011-09-05 16:36:31 -0300652 abort();
653 }
Kazuya Saito7e866002013-03-22 17:26:59 +0900654 trace_runstate_set(new_state);
Luiz Capitulinof5bbfba2011-07-29 15:04:45 -0300655 current_run_state = new_state;
656}
657
Luiz Capitulino13548692011-07-29 15:36:43 -0300658int runstate_is_running(void)
659{
Luiz Capitulino0461d5a2011-09-30 14:45:27 -0300660 return runstate_check(RUN_STATE_RUNNING);
Luiz Capitulino13548692011-07-29 15:36:43 -0300661}
662
Hu Taoede085b2013-04-26 11:24:40 +0800663bool runstate_needs_reset(void)
664{
665 return runstate_check(RUN_STATE_INTERNAL_ERROR) ||
Paolo Bonzinidf390762013-11-04 14:30:47 +0100666 runstate_check(RUN_STATE_SHUTDOWN);
Hu Taoede085b2013-04-26 11:24:40 +0800667}
668
Luiz Capitulino1fa9a5e2011-09-12 17:54:20 -0300669StatusInfo *qmp_query_status(Error **errp)
670{
671 StatusInfo *info = g_malloc0(sizeof(*info));
672
673 info->running = runstate_is_running();
674 info->singlestep = singlestep;
675 info->status = current_run_state;
676
677 return info;
678}
679
Luiz Capitulinof5bbfba2011-07-29 15:04:45 -0300680/***********************************************************/
Paolo Bonzini8f0056b2010-01-13 14:05:34 +0100681/* real time host monotonic timer */
bellard09b26c52006-04-12 21:09:08 +0000682
bellardc4b1fcc2004-03-14 21:44:30 +0000683/***********************************************************/
balrogf6503052008-02-17 11:42:19 +0000684/* host time/date access */
685void qemu_get_timedate(struct tm *tm, int offset)
686{
687 time_t ti;
balrogf6503052008-02-17 11:42:19 +0000688
689 time(&ti);
690 ti += offset;
691 if (rtc_date_offset == -1) {
692 if (rtc_utc)
Stefan Weileb7ff6f2013-01-07 23:08:13 +0100693 gmtime_r(&ti, tm);
balrogf6503052008-02-17 11:42:19 +0000694 else
Stefan Weileb7ff6f2013-01-07 23:08:13 +0100695 localtime_r(&ti, tm);
balrogf6503052008-02-17 11:42:19 +0000696 } else {
697 ti -= rtc_date_offset;
Stefan Weileb7ff6f2013-01-07 23:08:13 +0100698 gmtime_r(&ti, tm);
balrogf6503052008-02-17 11:42:19 +0000699 }
balrogf6503052008-02-17 11:42:19 +0000700}
701
702int qemu_timedate_diff(struct tm *tm)
703{
704 time_t seconds;
705
706 if (rtc_date_offset == -1)
707 if (rtc_utc)
708 seconds = mktimegm(tm);
Gleb Natapovf54c5562011-11-06 18:00:22 +0200709 else {
710 struct tm tmp = *tm;
711 tmp.tm_isdst = -1; /* use timezone to figure it out */
712 seconds = mktime(&tmp);
713 }
balrogf6503052008-02-17 11:42:19 +0000714 else
715 seconds = mktimegm(tm) + rtc_date_offset;
716
717 return seconds - time(NULL);
718}
719
Luiz Capitulino80cd3472010-02-25 12:11:44 -0300720void rtc_change_mon_event(struct tm *tm)
721{
722 QObject *data;
723
724 data = qobject_from_jsonf("{ 'offset': %d }", qemu_timedate_diff(tm));
725 monitor_protocol_event(QEVENT_RTC_CHANGE, data);
726 qobject_decref(data);
727}
728
Jan Kiszka1ed2fc12009-09-15 13:36:04 +0200729static void configure_rtc_date_offset(const char *startdate, int legacy)
730{
731 time_t rtc_start_date;
732 struct tm tm;
733
734 if (!strcmp(startdate, "now") && legacy) {
735 rtc_date_offset = -1;
736 } else {
737 if (sscanf(startdate, "%d-%d-%dT%d:%d:%d",
738 &tm.tm_year,
739 &tm.tm_mon,
740 &tm.tm_mday,
741 &tm.tm_hour,
742 &tm.tm_min,
743 &tm.tm_sec) == 6) {
744 /* OK */
745 } else if (sscanf(startdate, "%d-%d-%d",
746 &tm.tm_year,
747 &tm.tm_mon,
748 &tm.tm_mday) == 3) {
749 tm.tm_hour = 0;
750 tm.tm_min = 0;
751 tm.tm_sec = 0;
752 } else {
753 goto date_fail;
754 }
755 tm.tm_year -= 1900;
756 tm.tm_mon--;
757 rtc_start_date = mktimegm(&tm);
758 if (rtc_start_date == -1) {
759 date_fail:
760 fprintf(stderr, "Invalid date format. Valid formats are:\n"
761 "'2006-06-17T16:01:21' or '2006-06-17'\n");
762 exit(1);
763 }
764 rtc_date_offset = time(NULL) - rtc_start_date;
765 }
766}
767
768static void configure_rtc(QemuOpts *opts)
769{
770 const char *value;
771
772 value = qemu_opt_get(opts, "base");
773 if (value) {
774 if (!strcmp(value, "utc")) {
775 rtc_utc = 1;
776 } else if (!strcmp(value, "localtime")) {
777 rtc_utc = 0;
778 } else {
779 configure_rtc_date_offset(value, 0);
780 }
781 }
Jan Kiszka68752042009-09-15 13:36:04 +0200782 value = qemu_opt_get(opts, "clock");
783 if (value) {
784 if (!strcmp(value, "host")) {
Alex Bligh884f17c2013-08-21 16:03:04 +0100785 rtc_clock = QEMU_CLOCK_HOST;
Paolo Bonzini78808142012-03-30 10:31:21 +0000786 } else if (!strcmp(value, "rt")) {
Alex Bligh884f17c2013-08-21 16:03:04 +0100787 rtc_clock = QEMU_CLOCK_REALTIME;
Jan Kiszka68752042009-09-15 13:36:04 +0200788 } else if (!strcmp(value, "vm")) {
Alex Bligh884f17c2013-08-21 16:03:04 +0100789 rtc_clock = QEMU_CLOCK_VIRTUAL;
Jan Kiszka68752042009-09-15 13:36:04 +0200790 } else {
791 fprintf(stderr, "qemu: invalid option value '%s'\n", value);
792 exit(1);
793 }
794 }
Jan Kiszka1ed2fc12009-09-15 13:36:04 +0200795 value = qemu_opt_get(opts, "driftfix");
796 if (value) {
Blue Swirl7e4c0332010-03-27 21:33:46 +0000797 if (!strcmp(value, "slew")) {
Jan Kiszka433acf02012-01-23 20:15:12 +0100798 static GlobalProperty slew_lost_ticks[] = {
799 {
800 .driver = "mc146818rtc",
801 .property = "lost_tick_policy",
802 .value = "slew",
803 },
804 { /* end of list */ }
805 };
806
807 qdev_prop_register_global_list(slew_lost_ticks);
Blue Swirl7e4c0332010-03-27 21:33:46 +0000808 } else if (!strcmp(value, "none")) {
Jan Kiszka433acf02012-01-23 20:15:12 +0100809 /* discard is default */
Jan Kiszka1ed2fc12009-09-15 13:36:04 +0200810 } else {
811 fprintf(stderr, "qemu: invalid option value '%s'\n", value);
812 exit(1);
813 }
814 }
Jan Kiszka1ed2fc12009-09-15 13:36:04 +0200815}
816
balrog1ae26a12008-09-28 23:19:47 +0000817/***********************************************************/
818/* Bluetooth support */
819static int nb_hcis;
820static int cur_hci;
821static struct HCIInfo *hci_table[MAX_NICS];
balrogdc72ac12008-11-09 00:04:26 +0000822
balrog1ae26a12008-09-28 23:19:47 +0000823struct HCIInfo *qemu_next_hci(void)
824{
825 if (cur_hci == nb_hcis)
826 return &null_hci;
827
828 return hci_table[cur_hci++];
829}
830
balrogdc72ac12008-11-09 00:04:26 +0000831static int bt_hci_parse(const char *str)
832{
833 struct HCIInfo *hci;
Anthony Liguoric227f092009-10-01 16:12:16 -0500834 bdaddr_t bdaddr;
balrogdc72ac12008-11-09 00:04:26 +0000835
836 if (nb_hcis >= MAX_NICS) {
837 fprintf(stderr, "qemu: Too many bluetooth HCIs (max %i).\n", MAX_NICS);
838 return -1;
839 }
840
841 hci = hci_init(str);
842 if (!hci)
843 return -1;
844
845 bdaddr.b[0] = 0x52;
846 bdaddr.b[1] = 0x54;
847 bdaddr.b[2] = 0x00;
848 bdaddr.b[3] = 0x12;
849 bdaddr.b[4] = 0x34;
850 bdaddr.b[5] = 0x56 + nb_hcis;
851 hci->bdaddr_set(hci, bdaddr.b);
852
853 hci_table[nb_hcis++] = hci;
854
855 return 0;
856}
857
858static void bt_vhci_add(int vlan_id)
859{
860 struct bt_scatternet_s *vlan = qemu_find_bt_vlan(vlan_id);
861
862 if (!vlan->slave)
863 fprintf(stderr, "qemu: warning: adding a VHCI to "
864 "an empty scatternet %i\n", vlan_id);
865
866 bt_vhci_init(bt_new_hci(vlan));
867}
868
869static struct bt_device_s *bt_device_add(const char *opt)
870{
871 struct bt_scatternet_s *vlan;
872 int vlan_id = 0;
873 char *endp = strstr(opt, ",vlan=");
874 int len = (endp ? endp - opt : strlen(opt)) + 1;
875 char devname[10];
876
877 pstrcpy(devname, MIN(sizeof(devname), len), opt);
878
879 if (endp) {
880 vlan_id = strtol(endp + 6, &endp, 0);
881 if (*endp) {
882 fprintf(stderr, "qemu: unrecognised bluetooth vlan Id\n");
883 return 0;
884 }
885 }
886
887 vlan = qemu_find_bt_vlan(vlan_id);
888
889 if (!vlan->slave)
890 fprintf(stderr, "qemu: warning: adding a slave device to "
891 "an empty scatternet %i\n", vlan_id);
892
893 if (!strcmp(devname, "keyboard"))
894 return bt_keyboard_init(vlan);
895
896 fprintf(stderr, "qemu: unsupported bluetooth device `%s'\n", devname);
897 return 0;
898}
899
900static int bt_parse(const char *opt)
901{
902 const char *endp, *p;
903 int vlan;
904
905 if (strstart(opt, "hci", &endp)) {
906 if (!*endp || *endp == ',') {
907 if (*endp)
908 if (!strstart(endp, ",vlan=", 0))
909 opt = endp + 1;
910
911 return bt_hci_parse(opt);
912 }
913 } else if (strstart(opt, "vhci", &endp)) {
914 if (!*endp || *endp == ',') {
915 if (*endp) {
916 if (strstart(endp, ",vlan=", &p)) {
917 vlan = strtol(p, (char **) &endp, 0);
918 if (*endp) {
919 fprintf(stderr, "qemu: bad scatternet '%s'\n", p);
920 return 1;
921 }
922 } else {
923 fprintf(stderr, "qemu: bad parameter '%s'\n", endp + 1);
924 return 1;
925 }
926 } else
927 vlan = 0;
928
929 bt_vhci_add(vlan);
930 return 0;
931 }
932 } else if (strstart(opt, "device:", &endp))
933 return !bt_device_add(endp);
934
935 fprintf(stderr, "qemu: bad bluetooth parameter '%s'\n", opt);
936 return 1;
937}
938
Eduardo Otubo7d76ad42012-08-14 18:44:08 -0300939static int parse_sandbox(QemuOpts *opts, void *opaque)
940{
941 /* FIXME: change this to true for 1.3 */
942 if (qemu_opt_get_bool(opts, "enable", false)) {
943#ifdef CONFIG_SECCOMP
944 if (seccomp_start() < 0) {
945 qerror_report(ERROR_CLASS_GENERIC_ERROR,
946 "failed to install seccomp syscall filter in the kernel");
947 return -1;
948 }
949#else
950 qerror_report(ERROR_CLASS_GENERIC_ERROR,
951 "sandboxing request but seccomp is not compiled into this build");
952 return -1;
953#endif
954 }
955
956 return 0;
957}
958
Dr. David Alan Gilbert5d12f962014-01-30 10:20:30 +0000959static void parse_name(QemuOpts *opts)
960{
961 const char *proc_name;
962
Dr. David Alan Gilbert8f480de2014-01-30 10:20:31 +0000963 if (qemu_opt_get(opts, "debug-threads")) {
964 qemu_thread_naming(qemu_opt_get_bool(opts, "debug-threads", false));
965 }
Dr. David Alan Gilbert5d12f962014-01-30 10:20:30 +0000966 qemu_name = qemu_opt_get(opts, "guest");
967
968 proc_name = qemu_opt_get(opts, "process");
969 if (proc_name) {
970 os_set_proc_name(proc_name);
971 }
972}
973
zhlcindy@gmail.com094b2872012-09-02 19:25:28 +0000974bool usb_enabled(bool default_usb)
975{
Markus Armbruster2ff3de62013-07-04 15:09:22 +0200976 return qemu_opt_get_bool(qemu_get_machine_opts(), "usb", default_usb);
zhlcindy@gmail.com094b2872012-09-02 19:25:28 +0000977}
978
Corey Bryant587ed6b2012-10-18 15:19:34 -0400979#ifndef _WIN32
980static int parse_add_fd(QemuOpts *opts, void *opaque)
981{
982 int fd, dupfd, flags;
983 int64_t fdset_id;
984 const char *fd_opaque = NULL;
985
986 fd = qemu_opt_get_number(opts, "fd", -1);
987 fdset_id = qemu_opt_get_number(opts, "set", -1);
988 fd_opaque = qemu_opt_get(opts, "opaque");
989
990 if (fd < 0) {
991 qerror_report(ERROR_CLASS_GENERIC_ERROR,
992 "fd option is required and must be non-negative");
993 return -1;
994 }
995
996 if (fd <= STDERR_FILENO) {
997 qerror_report(ERROR_CLASS_GENERIC_ERROR,
998 "fd cannot be a standard I/O stream");
999 return -1;
1000 }
1001
1002 /*
1003 * All fds inherited across exec() necessarily have FD_CLOEXEC
1004 * clear, while qemu sets FD_CLOEXEC on all other fds used internally.
1005 */
1006 flags = fcntl(fd, F_GETFD);
1007 if (flags == -1 || (flags & FD_CLOEXEC)) {
1008 qerror_report(ERROR_CLASS_GENERIC_ERROR,
1009 "fd is not valid or already in use");
1010 return -1;
1011 }
1012
1013 if (fdset_id < 0) {
1014 qerror_report(ERROR_CLASS_GENERIC_ERROR,
1015 "set option is required and must be non-negative");
1016 return -1;
1017 }
1018
1019#ifdef F_DUPFD_CLOEXEC
1020 dupfd = fcntl(fd, F_DUPFD_CLOEXEC, 0);
1021#else
1022 dupfd = dup(fd);
1023 if (dupfd != -1) {
1024 qemu_set_cloexec(dupfd);
1025 }
1026#endif
1027 if (dupfd == -1) {
1028 qerror_report(ERROR_CLASS_GENERIC_ERROR,
1029 "Error duplicating fd: %s", strerror(errno));
1030 return -1;
1031 }
1032
1033 /* add the duplicate fd, and optionally the opaque string, to the fd set */
1034 monitor_fdset_add_fd(dupfd, true, fdset_id, fd_opaque ? true : false,
1035 fd_opaque, NULL);
1036
1037 return 0;
1038}
1039
1040static int cleanup_add_fd(QemuOpts *opts, void *opaque)
1041{
1042 int fd;
1043
1044 fd = qemu_opt_get_number(opts, "fd", -1);
1045 close(fd);
1046
1047 return 0;
1048}
1049#endif
1050
balrog1ae26a12008-09-28 23:19:47 +00001051/***********************************************************/
1052/* QEMU Block devices */
1053
Markus Armbruster2292dda2011-01-28 11:21:41 +01001054#define HD_OPTS "media=disk"
1055#define CDROM_OPTS "media=cdrom"
1056#define FD_OPTS ""
1057#define PFLASH_OPTS ""
1058#define MTD_OPTS ""
1059#define SD_OPTS ""
thse4bcb142007-12-02 04:51:10 +00001060
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +02001061static int drive_init_func(QemuOpts *opts, void *opaque)
1062{
Christian Borntraeger2d0d2832012-11-20 15:30:34 +01001063 BlockInterfaceType *block_default_type = opaque;
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +02001064
Christian Borntraeger2d0d2832012-11-20 15:30:34 +01001065 return drive_init(opts, *block_default_type) == NULL;
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +02001066}
1067
1068static int drive_enable_snapshot(QemuOpts *opts, void *opaque)
1069{
1070 if (NULL == qemu_opt_get(opts, "snapshot")) {
1071 qemu_opt_set(opts, "snapshot", "on");
1072 }
1073 return 0;
1074}
1075
Christian Borntraeger3c42ea62012-11-22 21:02:55 +01001076static void default_drive(int enable, int snapshot, BlockInterfaceType type,
1077 int index, const char *optstr)
Markus Armbruster4e5d9b52011-01-28 11:21:45 +01001078{
1079 QemuOpts *opts;
1080
Markus Armbruster4e5d9b52011-01-28 11:21:45 +01001081 if (!enable || drive_get_by_index(type, index)) {
1082 return;
1083 }
1084
1085 opts = drive_add(type, index, NULL, optstr);
1086 if (snapshot) {
1087 drive_enable_snapshot(opts, NULL);
1088 }
Christian Borntraeger2d0d2832012-11-20 15:30:34 +01001089 if (!drive_init(opts, type)) {
Markus Armbruster4e5d9b52011-01-28 11:21:45 +01001090 exit(1);
1091 }
1092}
1093
Jan Kiszka76e30d02009-07-02 00:19:02 +02001094void qemu_register_boot_set(QEMUBootSetHandler *func, void *opaque)
1095{
1096 boot_set_handler = func;
1097 boot_set_opaque = opaque;
1098}
1099
Markus Armbruster083b79c2013-06-14 13:15:04 +02001100int qemu_boot_set(const char *boot_order)
Jan Kiszka76e30d02009-07-02 00:19:02 +02001101{
1102 if (!boot_set_handler) {
1103 return -EINVAL;
1104 }
Markus Armbruster083b79c2013-06-14 13:15:04 +02001105 return boot_set_handler(boot_set_opaque, boot_order);
Jan Kiszka76e30d02009-07-02 00:19:02 +02001106}
1107
Markus Armbruster6ef47162013-06-14 13:15:01 +02001108static void validate_bootdevices(const char *devices)
Jan Kiszkaef3adf62009-07-02 00:19:02 +02001109{
1110 /* We just do some generic consistency checks */
1111 const char *p;
1112 int bitmap = 0;
1113
1114 for (p = devices; *p != '\0'; p++) {
1115 /* Allowed boot devices are:
1116 * a-b: floppy disk drives
1117 * c-f: IDE disk drives
Dong Xu Wang07f35072011-11-22 18:06:26 +08001118 * g-m: machine implementation dependent drives
Jan Kiszkaef3adf62009-07-02 00:19:02 +02001119 * n-p: network devices
1120 * It's up to each machine implementation to check if the given boot
1121 * devices match the actual hardware implementation and firmware
1122 * features.
1123 */
1124 if (*p < 'a' || *p > 'p') {
1125 fprintf(stderr, "Invalid boot device '%c'\n", *p);
1126 exit(1);
1127 }
1128 if (bitmap & (1 << (*p - 'a'))) {
1129 fprintf(stderr, "Boot device '%c' was given twice\n", *p);
1130 exit(1);
1131 }
1132 bitmap |= 1 << (*p - 'a');
1133 }
Jan Kiszkaef3adf62009-07-02 00:19:02 +02001134}
1135
Markus Armbruster083b79c2013-06-14 13:15:04 +02001136static void restore_boot_order(void *opaque)
Jan Kiszkae0f084b2009-07-02 00:19:02 +02001137{
Markus Armbruster083b79c2013-06-14 13:15:04 +02001138 char *normal_boot_order = opaque;
Alex Williamson37905d62010-04-30 15:21:11 -06001139 static int first = 1;
1140
1141 /* Restore boot order and remove ourselves after the first boot */
1142 if (first) {
1143 first = 0;
1144 return;
1145 }
Jan Kiszkae0f084b2009-07-02 00:19:02 +02001146
Markus Armbruster083b79c2013-06-14 13:15:04 +02001147 qemu_boot_set(normal_boot_order);
Jan Kiszkae0f084b2009-07-02 00:19:02 +02001148
Markus Armbruster083b79c2013-06-14 13:15:04 +02001149 qemu_unregister_reset(restore_boot_order, normal_boot_order);
1150 g_free(normal_boot_order);
Jan Kiszkae0f084b2009-07-02 00:19:02 +02001151}
1152
Gleb Natapov1ca4d092010-12-08 13:35:05 +02001153void add_boot_device_path(int32_t bootindex, DeviceState *dev,
1154 const char *suffix)
1155{
1156 FWBootEntry *node, *i;
1157
1158 if (bootindex < 0) {
1159 return;
1160 }
1161
1162 assert(dev != NULL || suffix != NULL);
1163
Anthony Liguori7267c092011-08-20 22:09:37 -05001164 node = g_malloc0(sizeof(FWBootEntry));
Gleb Natapov1ca4d092010-12-08 13:35:05 +02001165 node->bootindex = bootindex;
Dong Xu Wang7f303ad2013-05-09 15:53:49 +08001166 node->suffix = g_strdup(suffix);
Gleb Natapov1ca4d092010-12-08 13:35:05 +02001167 node->dev = dev;
1168
1169 QTAILQ_FOREACH(i, &fw_boot_order, link) {
1170 if (i->bootindex == bootindex) {
1171 fprintf(stderr, "Two devices with same boot index %d\n", bootindex);
1172 exit(1);
1173 } else if (i->bootindex < bootindex) {
1174 continue;
1175 }
1176 QTAILQ_INSERT_BEFORE(i, node, link);
1177 return;
1178 }
1179 QTAILQ_INSERT_TAIL(&fw_boot_order, node, link);
1180}
1181
Dominik Dingel7dc5af52013-04-26 02:12:49 +00001182DeviceState *get_boot_device(uint32_t position)
1183{
1184 uint32_t counter = 0;
1185 FWBootEntry *i = NULL;
1186 DeviceState *res = NULL;
1187
1188 if (!QTAILQ_EMPTY(&fw_boot_order)) {
1189 QTAILQ_FOREACH(i, &fw_boot_order, link) {
1190 if (counter == position) {
1191 res = i->dev;
1192 break;
1193 }
1194 counter++;
1195 }
1196 }
1197 return res;
1198}
1199
Gleb Natapov962630f2010-12-08 13:35:09 +02001200/*
1201 * This function returns null terminated string that consist of new line
Brad Hards71785ab2011-04-23 21:50:06 +10001202 * separated device paths.
Gleb Natapov962630f2010-12-08 13:35:09 +02001203 *
1204 * memory pointed by "size" is assigned total length of the array in bytes
1205 *
1206 */
Markus Armbruster0e7a7592013-01-16 14:50:29 +01001207char *get_boot_devices_list(size_t *size)
Gleb Natapov962630f2010-12-08 13:35:09 +02001208{
1209 FWBootEntry *i;
Markus Armbruster0e7a7592013-01-16 14:50:29 +01001210 size_t total = 0;
Gleb Natapov962630f2010-12-08 13:35:09 +02001211 char *list = NULL;
1212
1213 QTAILQ_FOREACH(i, &fw_boot_order, link) {
1214 char *devpath = NULL, *bootpath;
Markus Armbruster0e7a7592013-01-16 14:50:29 +01001215 size_t len;
Gleb Natapov962630f2010-12-08 13:35:09 +02001216
1217 if (i->dev) {
1218 devpath = qdev_get_fw_dev_path(i->dev);
1219 assert(devpath);
1220 }
1221
1222 if (i->suffix && devpath) {
Blue Swirl4fd37a92010-12-19 14:05:43 +00001223 size_t bootpathlen = strlen(devpath) + strlen(i->suffix) + 1;
1224
Anthony Liguori7267c092011-08-20 22:09:37 -05001225 bootpath = g_malloc(bootpathlen);
Blue Swirl4fd37a92010-12-19 14:05:43 +00001226 snprintf(bootpath, bootpathlen, "%s%s", devpath, i->suffix);
Anthony Liguori7267c092011-08-20 22:09:37 -05001227 g_free(devpath);
Gleb Natapov962630f2010-12-08 13:35:09 +02001228 } else if (devpath) {
1229 bootpath = devpath;
1230 } else {
Markus Armbruster1bf6ccd2011-11-08 10:58:00 +01001231 assert(i->suffix);
Anthony Liguori7267c092011-08-20 22:09:37 -05001232 bootpath = g_strdup(i->suffix);
Gleb Natapov962630f2010-12-08 13:35:09 +02001233 }
1234
1235 if (total) {
1236 list[total-1] = '\n';
1237 }
1238 len = strlen(bootpath) + 1;
Anthony Liguori7267c092011-08-20 22:09:37 -05001239 list = g_realloc(list, total + len);
Gleb Natapov962630f2010-12-08 13:35:09 +02001240 memcpy(&list[total], bootpath, len);
1241 total += len;
Anthony Liguori7267c092011-08-20 22:09:37 -05001242 g_free(bootpath);
Gleb Natapov962630f2010-12-08 13:35:09 +02001243 }
1244
1245 *size = total;
1246
Amos Kongc8a6ae82013-03-19 14:23:27 +08001247 if (boot_strict && *size > 0) {
1248 list[total-1] = '\n';
Amos Kong46905792013-03-20 18:16:34 +08001249 list = g_realloc(list, total + 5);
1250 memcpy(&list[total], "HALT", 5);
1251 *size = total + 5;
Amos Kongc8a6ae82013-03-19 14:23:27 +08001252 }
Gleb Natapov962630f2010-12-08 13:35:09 +02001253 return list;
1254}
1255
Eduardo Habkost845e5bf2013-02-04 16:27:51 -02001256static void numa_node_parse_cpus(int nodenr, const char *cpus)
1257{
1258 char *endptr;
1259 unsigned long long value, endvalue;
1260
Eduardo Habkostc881e202013-02-04 16:27:52 -02001261 /* Empty CPU range strings will be considered valid, they will simply
1262 * not set any bit in the CPU bitmap.
1263 */
1264 if (!*cpus) {
1265 return;
Eduardo Habkost845e5bf2013-02-04 16:27:51 -02001266 }
1267
Eduardo Habkostc881e202013-02-04 16:27:52 -02001268 if (parse_uint(cpus, &value, &endptr, 10) < 0) {
1269 goto error;
1270 }
1271 if (*endptr == '-') {
1272 if (parse_uint_full(endptr + 1, &endvalue, 10) < 0) {
1273 goto error;
1274 }
1275 } else if (*endptr == '\0') {
1276 endvalue = value;
1277 } else {
1278 goto error;
1279 }
1280
1281 if (endvalue >= MAX_CPUMASK_BITS) {
Eduardo Habkost845e5bf2013-02-04 16:27:51 -02001282 endvalue = MAX_CPUMASK_BITS - 1;
1283 fprintf(stderr,
Eduardo Habkostc881e202013-02-04 16:27:52 -02001284 "qemu: NUMA: A max of %d VCPUs are supported\n",
Eduardo Habkost845e5bf2013-02-04 16:27:51 -02001285 MAX_CPUMASK_BITS);
1286 }
1287
Eduardo Habkostc881e202013-02-04 16:27:52 -02001288 if (endvalue < value) {
1289 goto error;
1290 }
1291
Eduardo Habkost845e5bf2013-02-04 16:27:51 -02001292 bitmap_set(node_cpumask[nodenr], value, endvalue-value+1);
Eduardo Habkostc881e202013-02-04 16:27:52 -02001293 return;
1294
1295error:
1296 fprintf(stderr, "qemu: Invalid NUMA CPU range: %s\n", cpus);
1297 exit(1);
Eduardo Habkost845e5bf2013-02-04 16:27:51 -02001298}
1299
aliguori268a3622009-04-21 22:30:27 +00001300static void numa_add(const char *optarg)
1301{
1302 char option[128];
1303 char *endptr;
Eduardo Habkoste4ce85b2013-02-04 16:27:49 -02001304 unsigned long long nodenr;
aliguori268a3622009-04-21 22:30:27 +00001305
Eduardo Habkost8f302cb2013-02-04 16:27:46 -02001306 optarg = get_opt_name(option, 128, optarg, ',');
1307 if (*optarg == ',') {
1308 optarg++;
1309 }
aliguori268a3622009-04-21 22:30:27 +00001310 if (!strcmp(option, "node")) {
Eduardo Habkostca4c6d32013-02-04 16:27:48 -02001311
1312 if (nb_numa_nodes >= MAX_NODES) {
1313 fprintf(stderr, "qemu: too many NUMA nodes\n");
1314 exit(1);
1315 }
1316
aliguori268a3622009-04-21 22:30:27 +00001317 if (get_param_value(option, 128, "nodeid", optarg) == 0) {
1318 nodenr = nb_numa_nodes;
1319 } else {
Eduardo Habkost5f139962013-02-04 16:27:50 -02001320 if (parse_uint_full(option, &nodenr, 10) < 0) {
1321 fprintf(stderr, "qemu: Invalid NUMA nodeid: %s\n", option);
1322 exit(1);
1323 }
aliguori268a3622009-04-21 22:30:27 +00001324 }
1325
Eduardo Habkoste4ce85b2013-02-04 16:27:49 -02001326 if (nodenr >= MAX_NODES) {
1327 fprintf(stderr, "qemu: invalid NUMA nodeid: %llu\n", nodenr);
1328 exit(1);
1329 }
1330
aliguori268a3622009-04-21 22:30:27 +00001331 if (get_param_value(option, 128, "mem", optarg) == 0) {
1332 node_mem[nodenr] = 0;
1333 } else {
Jes Sorensen70b4f4b2011-01-05 11:41:02 +01001334 int64_t sval;
Markus Armbrusterc03417b2011-11-22 09:46:02 +01001335 sval = strtosz(option, &endptr);
1336 if (sval < 0 || *endptr) {
Jes Sorensen9f9b17a2010-10-21 17:15:46 +02001337 fprintf(stderr, "qemu: invalid numa mem size: %s\n", optarg);
1338 exit(1);
aliguori268a3622009-04-21 22:30:27 +00001339 }
Jes Sorensen9f9b17a2010-10-21 17:15:46 +02001340 node_mem[nodenr] = sval;
aliguori268a3622009-04-21 22:30:27 +00001341 }
Chegu Vinodee785fe2012-07-16 21:31:30 -07001342 if (get_param_value(option, 128, "cpus", optarg) != 0) {
Eduardo Habkost845e5bf2013-02-04 16:27:51 -02001343 numa_node_parse_cpus(nodenr, option);
aliguori268a3622009-04-21 22:30:27 +00001344 }
1345 nb_numa_nodes++;
Eduardo Habkost12e53a92013-02-04 16:27:47 -02001346 } else {
1347 fprintf(stderr, "Invalid -numa option: %s\n", option);
1348 exit(1);
aliguori268a3622009-04-21 22:30:27 +00001349 }
aliguori268a3622009-04-21 22:30:27 +00001350}
1351
Michael Tokarev12b7f572013-06-24 15:06:52 +04001352static QemuOptsList qemu_smp_opts = {
1353 .name = "smp-opts",
1354 .implied_opt_name = "cpus",
1355 .merge_lists = true,
1356 .head = QTAILQ_HEAD_INITIALIZER(qemu_smp_opts.head),
1357 .desc = {
1358 {
1359 .name = "cpus",
1360 .type = QEMU_OPT_NUMBER,
1361 }, {
1362 .name = "sockets",
1363 .type = QEMU_OPT_NUMBER,
1364 }, {
1365 .name = "cores",
1366 .type = QEMU_OPT_NUMBER,
1367 }, {
1368 .name = "threads",
1369 .type = QEMU_OPT_NUMBER,
1370 }, {
1371 .name = "maxcpus",
1372 .type = QEMU_OPT_NUMBER,
1373 },
1374 { /*End of list */ }
1375 },
1376};
1377
1378static void smp_parse(QemuOpts *opts)
Andre Przywaradc6b1c02009-08-19 15:42:40 +02001379{
Michael Tokarev12b7f572013-06-24 15:06:52 +04001380 if (opts) {
Andre Przywaradc6b1c02009-08-19 15:42:40 +02001381
Michael Tokarev12b7f572013-06-24 15:06:52 +04001382 unsigned cpus = qemu_opt_get_number(opts, "cpus", 0);
1383 unsigned sockets = qemu_opt_get_number(opts, "sockets", 0);
1384 unsigned cores = qemu_opt_get_number(opts, "cores", 0);
1385 unsigned threads = qemu_opt_get_number(opts, "threads", 0);
Andre Przywaradc6b1c02009-08-19 15:42:40 +02001386
Michael Tokarev12b7f572013-06-24 15:06:52 +04001387 /* compute missing values, prefer sockets over cores over threads */
1388 if (cpus == 0 || sockets == 0) {
1389 sockets = sockets > 0 ? sockets : 1;
1390 cores = cores > 0 ? cores : 1;
Andre Przywaradc6b1c02009-08-19 15:42:40 +02001391 threads = threads > 0 ? threads : 1;
Michael Tokarev12b7f572013-06-24 15:06:52 +04001392 if (cpus == 0) {
1393 cpus = cores * threads * sockets;
1394 }
Andre Przywaradc6b1c02009-08-19 15:42:40 +02001395 } else {
Michael Tokarev12b7f572013-06-24 15:06:52 +04001396 if (cores == 0) {
1397 threads = threads > 0 ? threads : 1;
1398 cores = cpus / (sockets * threads);
1399 } else {
1400 threads = cpus / (cores * sockets);
1401 }
Andre Przywaradc6b1c02009-08-19 15:42:40 +02001402 }
Michael Tokarev12b7f572013-06-24 15:06:52 +04001403
1404 max_cpus = qemu_opt_get_number(opts, "maxcpus", 0);
1405
1406 smp_cpus = cpus;
1407 smp_cores = cores > 0 ? cores : 1;
1408 smp_threads = threads > 0 ? threads : 1;
1409
Andre Przywaradc6b1c02009-08-19 15:42:40 +02001410 }
Michael Tokarev12b7f572013-06-24 15:06:52 +04001411
1412 if (max_cpus == 0) {
Andre Przywaradc6b1c02009-08-19 15:42:40 +02001413 max_cpus = smp_cpus;
Michael Tokarev12b7f572013-06-24 15:06:52 +04001414 }
1415
1416 if (max_cpus > 255) {
1417 fprintf(stderr, "Unsupported number of maxcpus\n");
1418 exit(1);
1419 }
1420 if (max_cpus < smp_cpus) {
1421 fprintf(stderr, "maxcpus must be equal to or greater than smp\n");
1422 exit(1);
1423 }
1424
Andre Przywaradc6b1c02009-08-19 15:42:40 +02001425}
1426
Satoru Moriya888a6bc2013-04-19 16:42:06 +02001427static void configure_realtime(QemuOpts *opts)
1428{
1429 bool enable_mlock;
1430
1431 enable_mlock = qemu_opt_get_bool(opts, "mlock", true);
1432
1433 if (enable_mlock) {
1434 if (os_mlock() < 0) {
1435 fprintf(stderr, "qemu: locking memory failed\n");
1436 exit(1);
1437 }
1438 }
1439}
1440
Seiji Aguchi5e2ac512013-07-03 23:02:46 -04001441
1442static void configure_msg(QemuOpts *opts)
1443{
1444 enable_timestamp_msg = qemu_opt_get_bool(opts, "timestamp", true);
1445}
1446
bellard330d0412003-07-26 18:11:40 +00001447/***********************************************************/
bellarda594cfb2005-11-06 16:13:29 +00001448/* USB devices */
1449
Markus Armbrusterfb080002010-05-28 15:38:44 +02001450static int usb_device_add(const char *devname)
bellarda594cfb2005-11-06 16:13:29 +00001451{
Gerd Hoffmanna5d2f722009-08-31 14:24:00 +02001452 USBDevice *dev = NULL;
Miroslav Rezanina615fe4d2013-09-03 11:23:09 +02001453#ifndef CONFIG_LINUX
1454 const char *p;
1455#endif
bellarda594cfb2005-11-06 16:13:29 +00001456
zhlcindy@gmail.com094b2872012-09-02 19:25:28 +00001457 if (!usb_enabled(false)) {
bellarda594cfb2005-11-06 16:13:29 +00001458 return -1;
zhlcindy@gmail.com094b2872012-09-02 19:25:28 +00001459 }
bellarda594cfb2005-11-06 16:13:29 +00001460
Gerd Hoffmann0958b4c2009-10-26 15:56:45 +01001461 /* drivers with .usbdevice_name entry in USBDeviceInfo */
1462 dev = usbdevice_create(devname);
1463 if (dev)
1464 goto done;
1465
Gerd Hoffmanna5d2f722009-08-31 14:24:00 +02001466 /* the other ones */
Gerd Hoffmanne447fc62011-06-01 14:41:59 +02001467#ifndef CONFIG_LINUX
1468 /* only the linux version is qdev-ified, usb-bsd still needs this */
bellarda594cfb2005-11-06 16:13:29 +00001469 if (strstart(devname, "host:", &p)) {
Jan Kiszka37417152012-02-27 15:18:47 +01001470 dev = usb_host_device_open(usb_bus_find(-1), p);
bellarda594cfb2005-11-06 16:13:29 +00001471 }
Miroslav Rezanina615fe4d2013-09-03 11:23:09 +02001472#endif
pbrook0d92ed32006-05-21 16:30:15 +00001473 if (!dev)
1474 return -1;
1475
Gerd Hoffmanna5d2f722009-08-31 14:24:00 +02001476done:
bellarda594cfb2005-11-06 16:13:29 +00001477 return 0;
1478}
1479
aliguori1f3870a2008-08-21 19:27:48 +00001480static int usb_device_del(const char *devname)
1481{
1482 int bus_num, addr;
1483 const char *p;
1484
Gerd Hoffmann1a3973b2012-11-30 13:02:47 +01001485 if (strstart(devname, "host:", &p)) {
1486 return -1;
1487 }
aliguori5d0c5752008-09-14 01:07:41 +00001488
zhlcindy@gmail.com094b2872012-09-02 19:25:28 +00001489 if (!usb_enabled(false)) {
aliguori1f3870a2008-08-21 19:27:48 +00001490 return -1;
zhlcindy@gmail.com094b2872012-09-02 19:25:28 +00001491 }
aliguori1f3870a2008-08-21 19:27:48 +00001492
1493 p = strchr(devname, '.');
1494 if (!p)
1495 return -1;
1496 bus_num = strtoul(devname, NULL, 0);
1497 addr = strtoul(p + 1, NULL, 0);
1498
Gerd Hoffmanna5d2f722009-08-31 14:24:00 +02001499 return usb_device_delete_addr(bus_num, addr);
aliguori1f3870a2008-08-21 19:27:48 +00001500}
1501
Gerd Hoffmannbd3c9482009-07-15 13:59:26 +02001502static int usb_parse(const char *cmdline)
1503{
Scott Tsai59d1c1c2009-12-23 04:30:18 +08001504 int r;
Markus Armbrusterfb080002010-05-28 15:38:44 +02001505 r = usb_device_add(cmdline);
Scott Tsai59d1c1c2009-12-23 04:30:18 +08001506 if (r < 0) {
1507 fprintf(stderr, "qemu: could not add USB device '%s'\n", cmdline);
1508 }
1509 return r;
Gerd Hoffmannbd3c9482009-07-15 13:59:26 +02001510}
1511
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001512void do_usb_add(Monitor *mon, const QDict *qdict)
bellarda594cfb2005-11-06 16:13:29 +00001513{
Scott Tsai59d1c1c2009-12-23 04:30:18 +08001514 const char *devname = qdict_get_str(qdict, "devname");
Markus Armbrusterfb080002010-05-28 15:38:44 +02001515 if (usb_device_add(devname) < 0) {
Markus Armbruster1ecda022010-02-18 17:25:24 +01001516 error_report("could not add USB device '%s'", devname);
Scott Tsai59d1c1c2009-12-23 04:30:18 +08001517 }
bellarda594cfb2005-11-06 16:13:29 +00001518}
1519
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001520void do_usb_del(Monitor *mon, const QDict *qdict)
bellarda594cfb2005-11-06 16:13:29 +00001521{
Scott Tsai59d1c1c2009-12-23 04:30:18 +08001522 const char *devname = qdict_get_str(qdict, "devname");
1523 if (usb_device_del(devname) < 0) {
Markus Armbruster1ecda022010-02-18 17:25:24 +01001524 error_report("could not delete USB device '%s'", devname);
Scott Tsai59d1c1c2009-12-23 04:30:18 +08001525 }
bellarda594cfb2005-11-06 16:13:29 +00001526}
1527
bellardf7cce892004-12-08 22:21:25 +00001528/***********************************************************/
balrog201a51f2007-04-30 00:51:09 +00001529/* PCMCIA/Cardbus */
1530
1531static struct pcmcia_socket_entry_s {
Paul Brookbc24a222009-05-10 01:44:56 +01001532 PCMCIASocket *socket;
balrog201a51f2007-04-30 00:51:09 +00001533 struct pcmcia_socket_entry_s *next;
1534} *pcmcia_sockets = 0;
1535
Paul Brookbc24a222009-05-10 01:44:56 +01001536void pcmcia_socket_register(PCMCIASocket *socket)
balrog201a51f2007-04-30 00:51:09 +00001537{
1538 struct pcmcia_socket_entry_s *entry;
1539
Anthony Liguori7267c092011-08-20 22:09:37 -05001540 entry = g_malloc(sizeof(struct pcmcia_socket_entry_s));
balrog201a51f2007-04-30 00:51:09 +00001541 entry->socket = socket;
1542 entry->next = pcmcia_sockets;
1543 pcmcia_sockets = entry;
1544}
1545
Paul Brookbc24a222009-05-10 01:44:56 +01001546void pcmcia_socket_unregister(PCMCIASocket *socket)
balrog201a51f2007-04-30 00:51:09 +00001547{
1548 struct pcmcia_socket_entry_s *entry, **ptr;
1549
1550 ptr = &pcmcia_sockets;
1551 for (entry = *ptr; entry; ptr = &entry->next, entry = *ptr)
1552 if (entry->socket == socket) {
1553 *ptr = entry->next;
Anthony Liguori7267c092011-08-20 22:09:37 -05001554 g_free(entry);
balrog201a51f2007-04-30 00:51:09 +00001555 }
1556}
1557
Wenchao Xia84f2d0e2013-01-14 14:06:25 +08001558void pcmcia_info(Monitor *mon, const QDict *qdict)
balrog201a51f2007-04-30 00:51:09 +00001559{
1560 struct pcmcia_socket_entry_s *iter;
aliguori376253e2009-03-05 23:01:23 +00001561
balrog201a51f2007-04-30 00:51:09 +00001562 if (!pcmcia_sockets)
aliguori376253e2009-03-05 23:01:23 +00001563 monitor_printf(mon, "No PCMCIA sockets\n");
balrog201a51f2007-04-30 00:51:09 +00001564
1565 for (iter = pcmcia_sockets; iter; iter = iter->next)
aliguori376253e2009-03-05 23:01:23 +00001566 monitor_printf(mon, "%s: %s\n", iter->socket->slot_string,
1567 iter->socket->attached ? iter->socket->card_string :
1568 "Empty");
balrog201a51f2007-04-30 00:51:09 +00001569}
1570
1571/***********************************************************/
bellardcc1daa42005-06-05 14:49:17 +00001572/* machine registration */
1573
Marcel Apfelbaum0056ae22014-03-05 19:30:47 +02001574MachineState *current_machine;
bellardcc1daa42005-06-05 14:49:17 +00001575
Marcel Apfelbaum261747f2014-03-05 19:30:46 +02001576static void machine_class_init(ObjectClass *oc, void *data)
1577{
1578 MachineClass *mc = MACHINE_CLASS(oc);
1579
1580 mc->qemu_machine = data;
1581}
1582
bellardcc1daa42005-06-05 14:49:17 +00001583int qemu_register_machine(QEMUMachine *m)
1584{
Marcel Apfelbaum261747f2014-03-05 19:30:46 +02001585 TypeInfo ti = {
1586 .name = g_strconcat(m->name, TYPE_MACHINE_SUFFIX, NULL),
1587 .parent = TYPE_MACHINE,
1588 .class_init = machine_class_init,
1589 .class_data = (void *)m,
1590 };
1591
1592 type_register(&ti);
1593
bellardcc1daa42005-06-05 14:49:17 +00001594 return 0;
1595}
1596
Marcel Apfelbaum0056ae22014-03-05 19:30:47 +02001597static MachineClass *find_machine(const char *name)
bellardcc1daa42005-06-05 14:49:17 +00001598{
Marcel Apfelbaum261747f2014-03-05 19:30:46 +02001599 GSList *el, *machines = object_class_get_list(TYPE_MACHINE, false);
Marcel Apfelbaum0056ae22014-03-05 19:30:47 +02001600 MachineClass *mc = NULL;
bellardcc1daa42005-06-05 14:49:17 +00001601
Marcel Apfelbaum261747f2014-03-05 19:30:46 +02001602 for (el = machines; el; el = el->next) {
Marcel Apfelbaum0056ae22014-03-05 19:30:47 +02001603 MachineClass *temp = el->data;
Marcel Apfelbaum261747f2014-03-05 19:30:46 +02001604
Marcel Apfelbaum0056ae22014-03-05 19:30:47 +02001605 if (!strcmp(temp->qemu_machine->name, name)) {
1606 mc = temp;
Marcel Apfelbaum261747f2014-03-05 19:30:46 +02001607 break;
1608 }
Marcel Apfelbaum0056ae22014-03-05 19:30:47 +02001609 if (temp->qemu_machine->alias &&
1610 !strcmp(temp->qemu_machine->alias, name)) {
1611 mc = temp;
Marcel Apfelbaum261747f2014-03-05 19:30:46 +02001612 break;
1613 }
bellardcc1daa42005-06-05 14:49:17 +00001614 }
Marcel Apfelbaum261747f2014-03-05 19:30:46 +02001615
1616 g_slist_free(machines);
Marcel Apfelbaum0056ae22014-03-05 19:30:47 +02001617 return mc;
bellardcc1daa42005-06-05 14:49:17 +00001618}
1619
Marcel Apfelbaum0056ae22014-03-05 19:30:47 +02001620MachineClass *find_default_machine(void)
Anthony Liguori0c257432009-05-21 20:41:01 -05001621{
Marcel Apfelbaum261747f2014-03-05 19:30:46 +02001622 GSList *el, *machines = object_class_get_list(TYPE_MACHINE, false);
Marcel Apfelbaum0056ae22014-03-05 19:30:47 +02001623 MachineClass *mc = NULL;
Anthony Liguori0c257432009-05-21 20:41:01 -05001624
Marcel Apfelbaum261747f2014-03-05 19:30:46 +02001625 for (el = machines; el; el = el->next) {
Marcel Apfelbaum0056ae22014-03-05 19:30:47 +02001626 MachineClass *temp = el->data;
Marcel Apfelbaum261747f2014-03-05 19:30:46 +02001627
Marcel Apfelbaum0056ae22014-03-05 19:30:47 +02001628 if (temp->qemu_machine->is_default) {
1629 mc = temp;
Marcel Apfelbaum261747f2014-03-05 19:30:46 +02001630 break;
Anthony Liguori0c257432009-05-21 20:41:01 -05001631 }
1632 }
Marcel Apfelbaum261747f2014-03-05 19:30:46 +02001633
1634 g_slist_free(machines);
Marcel Apfelbaum0056ae22014-03-05 19:30:47 +02001635 return mc;
Anthony Liguori0c257432009-05-21 20:41:01 -05001636}
1637
Anthony Liguori01d3c802012-08-10 11:04:11 -05001638MachineInfoList *qmp_query_machines(Error **errp)
1639{
Marcel Apfelbaum261747f2014-03-05 19:30:46 +02001640 GSList *el, *machines = object_class_get_list(TYPE_MACHINE, false);
Anthony Liguori01d3c802012-08-10 11:04:11 -05001641 MachineInfoList *mach_list = NULL;
1642 QEMUMachine *m;
1643
Marcel Apfelbaum261747f2014-03-05 19:30:46 +02001644 for (el = machines; el; el = el->next) {
1645 MachineClass *mc = el->data;
Anthony Liguori01d3c802012-08-10 11:04:11 -05001646 MachineInfoList *entry;
1647 MachineInfo *info;
1648
Marcel Apfelbaum261747f2014-03-05 19:30:46 +02001649 m = mc->qemu_machine;
Anthony Liguori01d3c802012-08-10 11:04:11 -05001650 info = g_malloc0(sizeof(*info));
1651 if (m->is_default) {
1652 info->has_is_default = true;
1653 info->is_default = true;
1654 }
1655
1656 if (m->alias) {
1657 info->has_alias = true;
1658 info->alias = g_strdup(m->alias);
1659 }
1660
1661 info->name = g_strdup(m->name);
Michal Novotnyc72e7682013-04-08 18:21:02 +02001662 info->cpu_max = !m->max_cpus ? 1 : m->max_cpus;
Anthony Liguori01d3c802012-08-10 11:04:11 -05001663
1664 entry = g_malloc0(sizeof(*entry));
1665 entry->value = info;
1666 entry->next = mach_list;
1667 mach_list = entry;
1668 }
1669
Marcel Apfelbaum261747f2014-03-05 19:30:46 +02001670 g_slist_free(machines);
Anthony Liguori01d3c802012-08-10 11:04:11 -05001671 return mach_list;
1672}
1673
bellardcc1daa42005-06-05 14:49:17 +00001674/***********************************************************/
bellard8a7ddc32004-03-31 19:00:16 +00001675/* main execution loop */
1676
bellard0bd48852005-11-11 00:00:47 +00001677struct vm_change_state_entry {
1678 VMChangeStateHandler *cb;
1679 void *opaque;
Blue Swirl72cf2d42009-09-12 07:36:22 +00001680 QLIST_ENTRY (vm_change_state_entry) entries;
bellard0bd48852005-11-11 00:00:47 +00001681};
1682
Blue Swirl72cf2d42009-09-12 07:36:22 +00001683static QLIST_HEAD(vm_change_state_head, vm_change_state_entry) vm_change_state_head;
bellard0bd48852005-11-11 00:00:47 +00001684
1685VMChangeStateEntry *qemu_add_vm_change_state_handler(VMChangeStateHandler *cb,
1686 void *opaque)
1687{
1688 VMChangeStateEntry *e;
1689
Anthony Liguori7267c092011-08-20 22:09:37 -05001690 e = g_malloc0(sizeof (*e));
bellard0bd48852005-11-11 00:00:47 +00001691
1692 e->cb = cb;
1693 e->opaque = opaque;
Blue Swirl72cf2d42009-09-12 07:36:22 +00001694 QLIST_INSERT_HEAD(&vm_change_state_head, e, entries);
bellard0bd48852005-11-11 00:00:47 +00001695 return e;
1696}
1697
1698void qemu_del_vm_change_state_handler(VMChangeStateEntry *e)
1699{
Blue Swirl72cf2d42009-09-12 07:36:22 +00001700 QLIST_REMOVE (e, entries);
Anthony Liguori7267c092011-08-20 22:09:37 -05001701 g_free (e);
bellard0bd48852005-11-11 00:00:47 +00001702}
1703
Luiz Capitulino1dfb4dd2011-07-29 14:26:33 -03001704void vm_state_notify(int running, RunState state)
bellard0bd48852005-11-11 00:00:47 +00001705{
1706 VMChangeStateEntry *e;
1707
Luiz Capitulino1dfb4dd2011-07-29 14:26:33 -03001708 trace_vm_state_notify(running, state);
Stefan Hajnoczi94b0b5f2010-11-16 12:20:25 +00001709
bellard0bd48852005-11-11 00:00:47 +00001710 for (e = vm_change_state_head.lh_first; e; e = e->entries.le_next) {
Luiz Capitulino1dfb4dd2011-07-29 14:26:33 -03001711 e->cb(e->opaque, running, state);
bellard0bd48852005-11-11 00:00:47 +00001712 }
1713}
1714
bellard8a7ddc32004-03-31 19:00:16 +00001715void vm_start(void)
1716{
Luiz Capitulino13548692011-07-29 15:36:43 -03001717 if (!runstate_is_running()) {
bellard8a7ddc32004-03-31 19:00:16 +00001718 cpu_enable_ticks();
Luiz Capitulino0461d5a2011-09-30 14:45:27 -03001719 runstate_set(RUN_STATE_RUNNING);
1720 vm_state_notify(1, RUN_STATE_RUNNING);
aliguorid6dc3d42009-04-24 18:04:07 +00001721 resume_all_vcpus();
Luiz Capitulino6ed2c482010-04-27 20:35:59 -03001722 monitor_protocol_event(QEVENT_RESUME, NULL);
bellard8a7ddc32004-03-31 19:00:16 +00001723 }
1724}
1725
bellardbb0c6722004-06-20 12:37:32 +00001726/* reset/shutdown handler */
1727
1728typedef struct QEMUResetEntry {
Blue Swirl72cf2d42009-09-12 07:36:22 +00001729 QTAILQ_ENTRY(QEMUResetEntry) entry;
bellardbb0c6722004-06-20 12:37:32 +00001730 QEMUResetHandler *func;
1731 void *opaque;
bellardbb0c6722004-06-20 12:37:32 +00001732} QEMUResetEntry;
1733
Blue Swirl72cf2d42009-09-12 07:36:22 +00001734static QTAILQ_HEAD(reset_handlers, QEMUResetEntry) reset_handlers =
1735 QTAILQ_HEAD_INITIALIZER(reset_handlers);
bellardbb0c6722004-06-20 12:37:32 +00001736static int reset_requested;
Gleb Natapovf64622c2011-03-15 13:56:04 +02001737static int shutdown_requested, shutdown_signal = -1;
1738static pid_t shutdown_pid;
bellard34751872005-07-02 14:31:34 +00001739static int powerdown_requested;
Jan Kiszka8cf71712011-02-07 12:19:16 +01001740static int debug_requested;
Gerd Hoffmann95b363b2012-02-23 13:45:19 +01001741static int suspend_requested;
Liu, Jinsong4bc78a82013-09-25 16:38:29 +00001742static WakeupReason wakeup_reason;
Igor Mammedova9552c82012-09-05 23:06:21 +02001743static NotifierList powerdown_notifiers =
1744 NOTIFIER_LIST_INITIALIZER(powerdown_notifiers);
Gerd Hoffmann95b363b2012-02-23 13:45:19 +01001745static NotifierList suspend_notifiers =
1746 NOTIFIER_LIST_INITIALIZER(suspend_notifiers);
1747static NotifierList wakeup_notifiers =
1748 NOTIFIER_LIST_INITIALIZER(wakeup_notifiers);
Liu, Jinsong4bc78a82013-09-25 16:38:29 +00001749static uint32_t wakeup_reason_mask = ~(1 << QEMU_WAKEUP_REASON_NONE);
Luiz Capitulino0461d5a2011-09-30 14:45:27 -03001750static RunState vmstop_requested = RUN_STATE_MAX;
bellardbb0c6722004-06-20 12:37:32 +00001751
Anthony PERARD1291eb32010-07-22 15:52:48 +01001752int qemu_shutdown_requested_get(void)
1753{
1754 return shutdown_requested;
1755}
1756
1757int qemu_reset_requested_get(void)
1758{
1759 return reset_requested;
1760}
1761
Blue Swirl4fdcac02012-10-28 11:04:47 +00001762static int qemu_shutdown_requested(void)
aurel32cf7a2fe2008-03-18 06:53:05 +00001763{
1764 int r = shutdown_requested;
1765 shutdown_requested = 0;
1766 return r;
1767}
1768
Blue Swirl4fdcac02012-10-28 11:04:47 +00001769static void qemu_kill_report(void)
Gleb Natapovf64622c2011-03-15 13:56:04 +02001770{
Michael S. Tsirkin35b30712014-02-04 20:04:21 +02001771 if (!qtest_driver() && shutdown_signal != -1) {
Peter Maydellf1d3fb02011-03-30 22:03:38 +01001772 fprintf(stderr, "qemu: terminating on signal %d", shutdown_signal);
1773 if (shutdown_pid == 0) {
1774 /* This happens for eg ^C at the terminal, so it's worth
1775 * avoiding printing an odd message in that case.
1776 */
1777 fputc('\n', stderr);
1778 } else {
Andreas Färber953ffe02011-06-02 19:58:06 +02001779 fprintf(stderr, " from pid " FMT_pid "\n", shutdown_pid);
Peter Maydellf1d3fb02011-03-30 22:03:38 +01001780 }
Gleb Natapovf64622c2011-03-15 13:56:04 +02001781 shutdown_signal = -1;
1782 }
1783}
1784
Blue Swirl4fdcac02012-10-28 11:04:47 +00001785static int qemu_reset_requested(void)
aurel32cf7a2fe2008-03-18 06:53:05 +00001786{
1787 int r = reset_requested;
1788 reset_requested = 0;
1789 return r;
1790}
1791
Gerd Hoffmann95b363b2012-02-23 13:45:19 +01001792static int qemu_suspend_requested(void)
1793{
1794 int r = suspend_requested;
1795 suspend_requested = 0;
1796 return r;
1797}
1798
Liu, Jinsong4bc78a82013-09-25 16:38:29 +00001799static WakeupReason qemu_wakeup_requested(void)
Luiz Capitulino14058192012-08-08 17:29:17 -03001800{
Liu, Jinsong4bc78a82013-09-25 16:38:29 +00001801 return wakeup_reason;
Luiz Capitulino14058192012-08-08 17:29:17 -03001802}
1803
Blue Swirl4fdcac02012-10-28 11:04:47 +00001804static int qemu_powerdown_requested(void)
aurel32cf7a2fe2008-03-18 06:53:05 +00001805{
1806 int r = powerdown_requested;
1807 powerdown_requested = 0;
1808 return r;
1809}
1810
aliguorie5689022009-04-24 18:03:54 +00001811static int qemu_debug_requested(void)
1812{
1813 int r = debug_requested;
1814 debug_requested = 0;
1815 return r;
1816}
1817
Luiz Capitulino0461d5a2011-09-30 14:45:27 -03001818/* We use RUN_STATE_MAX but any invalid value will do */
Luiz Capitulino0d45b702011-09-30 14:27:11 -03001819static bool qemu_vmstop_requested(RunState *r)
aliguori6e29f5d2009-04-24 18:04:02 +00001820{
Luiz Capitulino0461d5a2011-09-30 14:45:27 -03001821 if (vmstop_requested < RUN_STATE_MAX) {
Luiz Capitulino0d45b702011-09-30 14:27:11 -03001822 *r = vmstop_requested;
Luiz Capitulino0461d5a2011-09-30 14:45:27 -03001823 vmstop_requested = RUN_STATE_MAX;
Luiz Capitulino0d45b702011-09-30 14:27:11 -03001824 return true;
1825 }
1826
1827 return false;
aliguori6e29f5d2009-04-24 18:04:02 +00001828}
1829
Jan Kiszkaa08d4362009-06-27 09:25:07 +02001830void qemu_register_reset(QEMUResetHandler *func, void *opaque)
bellardbb0c6722004-06-20 12:37:32 +00001831{
Anthony Liguori7267c092011-08-20 22:09:37 -05001832 QEMUResetEntry *re = g_malloc0(sizeof(QEMUResetEntry));
bellardbb0c6722004-06-20 12:37:32 +00001833
bellardbb0c6722004-06-20 12:37:32 +00001834 re->func = func;
1835 re->opaque = opaque;
Blue Swirl72cf2d42009-09-12 07:36:22 +00001836 QTAILQ_INSERT_TAIL(&reset_handlers, re, entry);
bellardbb0c6722004-06-20 12:37:32 +00001837}
1838
Jan Kiszkadda9b292009-07-02 00:19:02 +02001839void qemu_unregister_reset(QEMUResetHandler *func, void *opaque)
bellardbb0c6722004-06-20 12:37:32 +00001840{
1841 QEMUResetEntry *re;
1842
Blue Swirl72cf2d42009-09-12 07:36:22 +00001843 QTAILQ_FOREACH(re, &reset_handlers, entry) {
Jan Kiszkadda9b292009-07-02 00:19:02 +02001844 if (re->func == func && re->opaque == opaque) {
Blue Swirl72cf2d42009-09-12 07:36:22 +00001845 QTAILQ_REMOVE(&reset_handlers, re, entry);
Anthony Liguori7267c092011-08-20 22:09:37 -05001846 g_free(re);
Jan Kiszkadda9b292009-07-02 00:19:02 +02001847 return;
1848 }
1849 }
1850}
1851
David Gibsonbe522022012-08-07 16:41:51 +10001852void qemu_devices_reset(void)
Jan Kiszkadda9b292009-07-02 00:19:02 +02001853{
1854 QEMUResetEntry *re, *nre;
1855
1856 /* reset all devices */
Blue Swirl72cf2d42009-09-12 07:36:22 +00001857 QTAILQ_FOREACH_SAFE(re, &reset_handlers, entry, nre) {
bellardbb0c6722004-06-20 12:37:32 +00001858 re->func(re->opaque);
1859 }
David Gibsonbe522022012-08-07 16:41:51 +10001860}
1861
1862void qemu_system_reset(bool report)
1863{
Marcel Apfelbaum0056ae22014-03-05 19:30:47 +02001864 MachineClass *mc;
1865
1866 mc = current_machine ? MACHINE_GET_CLASS(current_machine) : NULL;
1867
1868 if (mc && mc->qemu_machine->reset) {
1869 mc->qemu_machine->reset();
David Gibsonbe522022012-08-07 16:41:51 +10001870 } else {
1871 qemu_devices_reset();
1872 }
Jan Kiszkae063eb12011-06-14 18:29:43 +02001873 if (report) {
1874 monitor_protocol_event(QEVENT_RESET, NULL);
1875 }
Jan Kiszkaea375f92010-03-01 19:10:30 +01001876 cpu_synchronize_all_post_reset();
bellardbb0c6722004-06-20 12:37:32 +00001877}
1878
1879void qemu_system_reset_request(void)
1880{
bellardd1beab82006-10-02 19:44:22 +00001881 if (no_reboot) {
1882 shutdown_requested = 1;
1883 } else {
1884 reset_requested = 1;
1885 }
Jan Kiszkab4a3d962011-02-01 22:15:43 +01001886 cpu_stop_current();
aliguorid9f75a42009-04-24 18:03:11 +00001887 qemu_notify_event();
bellardbb0c6722004-06-20 12:37:32 +00001888}
1889
Gerd Hoffmann95b363b2012-02-23 13:45:19 +01001890static void qemu_system_suspend(void)
1891{
1892 pause_all_vcpus();
1893 notifier_list_notify(&suspend_notifiers, NULL);
Luiz Capitulinoad02b962012-04-27 13:33:36 -03001894 runstate_set(RUN_STATE_SUSPENDED);
Gerd Hoffmann53370b72012-02-23 13:45:26 +01001895 monitor_protocol_event(QEVENT_SUSPEND, NULL);
Gerd Hoffmann95b363b2012-02-23 13:45:19 +01001896}
1897
1898void qemu_system_suspend_request(void)
1899{
Luiz Capitulino9abc62f2012-04-27 14:31:12 -03001900 if (runstate_check(RUN_STATE_SUSPENDED)) {
Gerd Hoffmann95b363b2012-02-23 13:45:19 +01001901 return;
1902 }
1903 suspend_requested = 1;
1904 cpu_stop_current();
1905 qemu_notify_event();
1906}
1907
1908void qemu_register_suspend_notifier(Notifier *notifier)
1909{
1910 notifier_list_add(&suspend_notifiers, notifier);
1911}
1912
1913void qemu_system_wakeup_request(WakeupReason reason)
1914{
Alexey Kardashevskiy4fed9422014-03-07 01:33:36 +05301915 trace_system_wakeup_request(reason);
1916
Luiz Capitulino9abc62f2012-04-27 14:31:12 -03001917 if (!runstate_check(RUN_STATE_SUSPENDED)) {
Gerd Hoffmann95b363b2012-02-23 13:45:19 +01001918 return;
1919 }
1920 if (!(wakeup_reason_mask & (1 << reason))) {
1921 return;
1922 }
Luiz Capitulinoad02b962012-04-27 13:33:36 -03001923 runstate_set(RUN_STATE_RUNNING);
Liu, Jinsong4bc78a82013-09-25 16:38:29 +00001924 wakeup_reason = reason;
Gerd Hoffmann95b363b2012-02-23 13:45:19 +01001925 qemu_notify_event();
Gerd Hoffmann95b363b2012-02-23 13:45:19 +01001926}
1927
1928void qemu_system_wakeup_enable(WakeupReason reason, bool enabled)
1929{
1930 if (enabled) {
1931 wakeup_reason_mask |= (1 << reason);
1932 } else {
1933 wakeup_reason_mask &= ~(1 << reason);
1934 }
1935}
1936
1937void qemu_register_wakeup_notifier(Notifier *notifier)
1938{
1939 notifier_list_add(&wakeup_notifiers, notifier);
1940}
1941
Gleb Natapovf64622c2011-03-15 13:56:04 +02001942void qemu_system_killed(int signal, pid_t pid)
1943{
1944 shutdown_signal = signal;
1945 shutdown_pid = pid;
Kevin Wolfd9389b92011-09-14 15:38:40 +02001946 no_shutdown = 0;
Gleb Natapovf64622c2011-03-15 13:56:04 +02001947 qemu_system_shutdown_request();
1948}
1949
bellardbb0c6722004-06-20 12:37:32 +00001950void qemu_system_shutdown_request(void)
1951{
1952 shutdown_requested = 1;
aliguorid9f75a42009-04-24 18:03:11 +00001953 qemu_notify_event();
bellardbb0c6722004-06-20 12:37:32 +00001954}
1955
Igor Mammedov013c2f12012-09-05 23:06:25 +02001956static void qemu_system_powerdown(void)
1957{
1958 monitor_protocol_event(QEVENT_POWERDOWN, NULL);
1959 notifier_list_notify(&powerdown_notifiers, NULL);
1960}
1961
bellard34751872005-07-02 14:31:34 +00001962void qemu_system_powerdown_request(void)
1963{
1964 powerdown_requested = 1;
aliguorid9f75a42009-04-24 18:03:11 +00001965 qemu_notify_event();
1966}
1967
Igor Mammedova9552c82012-09-05 23:06:21 +02001968void qemu_register_powerdown_notifier(Notifier *notifier)
1969{
1970 notifier_list_add(&powerdown_notifiers, notifier);
1971}
1972
Jan Kiszka8cf71712011-02-07 12:19:16 +01001973void qemu_system_debug_request(void)
1974{
1975 debug_requested = 1;
Jan Kiszka83f338f2011-02-07 12:19:17 +01001976 qemu_notify_event();
Jan Kiszka8cf71712011-02-07 12:19:16 +01001977}
1978
Luiz Capitulino1dfb4dd2011-07-29 14:26:33 -03001979void qemu_system_vmstop_request(RunState state)
Jan Kiszka8cf71712011-02-07 12:19:16 +01001980{
Luiz Capitulino1dfb4dd2011-07-29 14:26:33 -03001981 vmstop_requested = state;
Jan Kiszka8cf71712011-02-07 12:19:16 +01001982 qemu_notify_event();
1983}
1984
Paolo Bonzini99435902011-09-12 14:03:13 +02001985static bool main_loop_should_exit(void)
1986{
1987 RunState r;
1988 if (qemu_debug_requested()) {
1989 vm_stop(RUN_STATE_DEBUG);
1990 }
Gerd Hoffmann95b363b2012-02-23 13:45:19 +01001991 if (qemu_suspend_requested()) {
1992 qemu_system_suspend();
1993 }
Paolo Bonzini99435902011-09-12 14:03:13 +02001994 if (qemu_shutdown_requested()) {
1995 qemu_kill_report();
1996 monitor_protocol_event(QEVENT_SHUTDOWN, NULL);
1997 if (no_shutdown) {
1998 vm_stop(RUN_STATE_SHUTDOWN);
1999 } else {
2000 return true;
2001 }
2002 }
2003 if (qemu_reset_requested()) {
2004 pause_all_vcpus();
2005 cpu_synchronize_all_states();
2006 qemu_system_reset(VMRESET_REPORT);
2007 resume_all_vcpus();
Hu Taoede085b2013-04-26 11:24:40 +08002008 if (runstate_needs_reset()) {
Paolo Bonzini99435902011-09-12 14:03:13 +02002009 runstate_set(RUN_STATE_PAUSED);
2010 }
2011 }
Luiz Capitulino14058192012-08-08 17:29:17 -03002012 if (qemu_wakeup_requested()) {
2013 pause_all_vcpus();
2014 cpu_synchronize_all_states();
2015 qemu_system_reset(VMRESET_SILENT);
Liu, Jinsong4bc78a82013-09-25 16:38:29 +00002016 notifier_list_notify(&wakeup_notifiers, &wakeup_reason);
2017 wakeup_reason = QEMU_WAKEUP_REASON_NONE;
Luiz Capitulino14058192012-08-08 17:29:17 -03002018 resume_all_vcpus();
Luiz Capitulino17c86602012-08-09 11:27:30 -03002019 monitor_protocol_event(QEVENT_WAKEUP, NULL);
Luiz Capitulino14058192012-08-08 17:29:17 -03002020 }
Paolo Bonzini99435902011-09-12 14:03:13 +02002021 if (qemu_powerdown_requested()) {
Igor Mammedov013c2f12012-09-05 23:06:25 +02002022 qemu_system_powerdown();
Paolo Bonzini99435902011-09-12 14:03:13 +02002023 }
2024 if (qemu_vmstop_requested(&r)) {
2025 vm_stop(r);
2026 }
2027 return false;
2028}
2029
aliguori43b96852009-04-24 18:03:33 +00002030static void main_loop(void)
2031{
Jan Kiszkac9f711a2011-08-22 17:46:02 +02002032 bool nonblocking;
Paolo Bonzini99435902011-09-12 14:03:13 +02002033 int last_io = 0;
Jan Kiszka8e1b90e2011-02-01 22:15:46 +01002034#ifdef CONFIG_PROFILER
2035 int64_t ti;
2036#endif
Paolo Bonzini99435902011-09-12 14:03:13 +02002037 do {
Stefano Stabellinia7d42072013-06-03 15:38:43 +00002038 nonblocking = !kvm_enabled() && !xen_enabled() && last_io > 0;
aliguori43b96852009-04-24 18:03:33 +00002039#ifdef CONFIG_PROFILER
Jan Kiszka46481d32011-02-01 22:15:47 +01002040 ti = profile_getclock();
aliguori43b96852009-04-24 18:03:33 +00002041#endif
Jan Kiszkac9f711a2011-08-22 17:46:02 +02002042 last_io = main_loop_wait(nonblocking);
aliguori43b96852009-04-24 18:03:33 +00002043#ifdef CONFIG_PROFILER
Jan Kiszka46481d32011-02-01 22:15:47 +01002044 dev_time += profile_getclock() - ti;
aliguori43b96852009-04-24 18:03:33 +00002045#endif
Paolo Bonzini99435902011-09-12 14:03:13 +02002046 } while (!main_loop_should_exit());
bellardb4608c02003-06-27 17:34:32 +00002047}
2048
pbrook9bd7e6d2009-04-07 22:58:45 +00002049static void version(void)
2050{
Thomas Monjalonf75ca1a2010-04-28 14:42:01 +02002051 printf("QEMU emulator version " QEMU_VERSION QEMU_PKGVERSION ", Copyright (c) 2003-2008 Fabrice Bellard\n");
pbrook9bd7e6d2009-04-07 22:58:45 +00002052}
2053
ths15f82202007-06-29 23:26:08 +00002054static void help(int exitcode)
bellard0824d6f2003-06-24 13:42:40 +00002055{
Michael Ellermana3adb7a2011-12-19 17:19:31 +11002056 version();
2057 printf("usage: %s [options] [disk_image]\n\n"
2058 "'disk_image' is a raw hard disk image for IDE hard disk 0\n\n",
2059 error_get_progname());
2060
Michael Ellerman77bd1112011-12-19 17:19:30 +11002061#define QEMU_OPTIONS_GENERATE_HELP
2062#include "qemu-options-wrapper.h"
Michael Ellermana3adb7a2011-12-19 17:19:31 +11002063
2064 printf("\nDuring emulation, the following keys are useful:\n"
malc3f020d72010-02-08 12:04:56 +03002065 "ctrl-alt-f toggle full screen\n"
2066 "ctrl-alt-n switch to virtual console 'n'\n"
2067 "ctrl-alt toggle mouse and keyboard grab\n"
2068 "\n"
Michael Ellermana3adb7a2011-12-19 17:19:31 +11002069 "When using -nographic, press 'ctrl-a h' to get some help.\n");
2070
ths15f82202007-06-29 23:26:08 +00002071 exit(exitcode);
bellard0824d6f2003-06-24 13:42:40 +00002072}
2073
bellardcd6f1162004-05-13 22:02:20 +00002074#define HAS_ARG 0x0001
2075
bellardcd6f1162004-05-13 22:02:20 +00002076typedef struct QEMUOption {
2077 const char *name;
2078 int flags;
2079 int index;
Blue Swirlad960902010-03-29 19:23:52 +00002080 uint32_t arch_mask;
bellardcd6f1162004-05-13 22:02:20 +00002081} QEMUOption;
2082
blueswir1dbed7e42008-10-01 19:38:09 +00002083static const QEMUOption qemu_options[] = {
Blue Swirlad960902010-03-29 19:23:52 +00002084 { "h", 0, QEMU_OPTION_h, QEMU_ARCH_ALL },
Michael Ellerman77bd1112011-12-19 17:19:30 +11002085#define QEMU_OPTIONS_GENERATE_OPTIONS
2086#include "qemu-options-wrapper.h"
bellardcd6f1162004-05-13 22:02:20 +00002087 { NULL },
bellardfc01f7e2003-06-30 10:03:06 +00002088};
Blue Swirla369da52011-09-27 19:15:42 +00002089
2090static bool vga_available(void)
2091{
Aurelien Jarno36b7f272012-09-08 16:01:20 +02002092 return object_class_by_name("VGA") || object_class_by_name("isa-vga");
Blue Swirla369da52011-09-27 19:15:42 +00002093}
2094
2095static bool cirrus_vga_available(void)
2096{
Aurelien Jarno36b7f272012-09-08 16:01:20 +02002097 return object_class_by_name("cirrus-vga")
2098 || object_class_by_name("isa-cirrus-vga");
Blue Swirla369da52011-09-27 19:15:42 +00002099}
2100
2101static bool vmware_vga_available(void)
2102{
Aurelien Jarno36b7f272012-09-08 16:01:20 +02002103 return object_class_by_name("vmware-svga");
Blue Swirla369da52011-09-27 19:15:42 +00002104}
2105
Aurelien Jarno879049a2012-09-08 12:01:06 +02002106static bool qxl_vga_available(void)
2107{
2108 return object_class_by_name("qxl-vga");
2109}
2110
Mark Cave-Aylandaf87bf22013-10-15 21:03:04 +01002111static bool tcx_vga_available(void)
2112{
2113 return object_class_by_name("SUNW,tcx");
2114}
2115
2116static bool cg3_vga_available(void)
2117{
2118 return object_class_by_name("cgthree");
2119}
2120
malc3893c122008-09-28 00:42:05 +00002121static void select_vgahw (const char *p)
2122{
2123 const char *opts;
2124
Zachary Amsden86176752009-07-30 00:15:02 -10002125 vga_interface_type = VGA_NONE;
malc3893c122008-09-28 00:42:05 +00002126 if (strstart(p, "std", &opts)) {
Blue Swirla369da52011-09-27 19:15:42 +00002127 if (vga_available()) {
2128 vga_interface_type = VGA_STD;
2129 } else {
2130 fprintf(stderr, "Error: standard VGA not available\n");
2131 exit(0);
2132 }
malc3893c122008-09-28 00:42:05 +00002133 } else if (strstart(p, "cirrus", &opts)) {
Blue Swirla369da52011-09-27 19:15:42 +00002134 if (cirrus_vga_available()) {
2135 vga_interface_type = VGA_CIRRUS;
2136 } else {
2137 fprintf(stderr, "Error: Cirrus VGA not available\n");
2138 exit(0);
2139 }
malc3893c122008-09-28 00:42:05 +00002140 } else if (strstart(p, "vmware", &opts)) {
Blue Swirla369da52011-09-27 19:15:42 +00002141 if (vmware_vga_available()) {
2142 vga_interface_type = VGA_VMWARE;
2143 } else {
2144 fprintf(stderr, "Error: VMWare SVGA not available\n");
2145 exit(0);
2146 }
aliguori94909d92009-04-22 15:19:53 +00002147 } else if (strstart(p, "xenfb", &opts)) {
Zachary Amsden86176752009-07-30 00:15:02 -10002148 vga_interface_type = VGA_XENFB;
Gerd Hoffmanna19cbfb2010-04-27 11:50:11 +02002149 } else if (strstart(p, "qxl", &opts)) {
Aurelien Jarno879049a2012-09-08 12:01:06 +02002150 if (qxl_vga_available()) {
2151 vga_interface_type = VGA_QXL;
2152 } else {
2153 fprintf(stderr, "Error: QXL VGA not available\n");
2154 exit(0);
2155 }
Mark Cave-Aylandaf87bf22013-10-15 21:03:04 +01002156 } else if (strstart(p, "tcx", &opts)) {
2157 if (tcx_vga_available()) {
2158 vga_interface_type = VGA_TCX;
2159 } else {
2160 fprintf(stderr, "Error: TCX framebuffer not available\n");
2161 exit(0);
2162 }
2163 } else if (strstart(p, "cg3", &opts)) {
2164 if (cg3_vga_available()) {
2165 vga_interface_type = VGA_CG3;
2166 } else {
2167 fprintf(stderr, "Error: CG3 framebuffer not available\n");
2168 exit(0);
2169 }
aliguori28b85ed2009-04-22 15:19:48 +00002170 } else if (!strstart(p, "none", &opts)) {
malc3893c122008-09-28 00:42:05 +00002171 invalid_vga:
2172 fprintf(stderr, "Unknown vga type: %s\n", p);
2173 exit(1);
2174 }
malccb5a7aa2008-09-28 00:42:12 +00002175 while (*opts) {
2176 const char *nextopt;
2177
2178 if (strstart(opts, ",retrace=", &nextopt)) {
2179 opts = nextopt;
2180 if (strstart(opts, "dumb", &nextopt))
2181 vga_retrace_method = VGA_RETRACE_DUMB;
2182 else if (strstart(opts, "precise", &nextopt))
2183 vga_retrace_method = VGA_RETRACE_PRECISE;
2184 else goto invalid_vga;
2185 } else goto invalid_vga;
2186 opts = nextopt;
2187 }
malc3893c122008-09-28 00:42:05 +00002188}
2189
Jes Sorensen1472a952011-03-16 13:33:31 +01002190static DisplayType select_display(const char *p)
2191{
2192 const char *opts;
2193 DisplayType display = DT_DEFAULT;
2194
2195 if (strstart(p, "sdl", &opts)) {
2196#ifdef CONFIG_SDL
2197 display = DT_SDL;
2198 while (*opts) {
2199 const char *nextopt;
2200
2201 if (strstart(opts, ",frame=", &nextopt)) {
2202 opts = nextopt;
2203 if (strstart(opts, "on", &nextopt)) {
2204 no_frame = 0;
2205 } else if (strstart(opts, "off", &nextopt)) {
2206 no_frame = 1;
2207 } else {
Peter Maydell05175532011-03-23 03:40:57 +00002208 goto invalid_sdl_args;
Jes Sorensen1472a952011-03-16 13:33:31 +01002209 }
2210 } else if (strstart(opts, ",alt_grab=", &nextopt)) {
2211 opts = nextopt;
2212 if (strstart(opts, "on", &nextopt)) {
2213 alt_grab = 1;
2214 } else if (strstart(opts, "off", &nextopt)) {
2215 alt_grab = 0;
2216 } else {
Peter Maydell05175532011-03-23 03:40:57 +00002217 goto invalid_sdl_args;
Jes Sorensen1472a952011-03-16 13:33:31 +01002218 }
2219 } else if (strstart(opts, ",ctrl_grab=", &nextopt)) {
2220 opts = nextopt;
2221 if (strstart(opts, "on", &nextopt)) {
2222 ctrl_grab = 1;
2223 } else if (strstart(opts, "off", &nextopt)) {
2224 ctrl_grab = 0;
2225 } else {
Peter Maydell05175532011-03-23 03:40:57 +00002226 goto invalid_sdl_args;
Jes Sorensen1472a952011-03-16 13:33:31 +01002227 }
2228 } else if (strstart(opts, ",window_close=", &nextopt)) {
2229 opts = nextopt;
2230 if (strstart(opts, "on", &nextopt)) {
2231 no_quit = 0;
2232 } else if (strstart(opts, "off", &nextopt)) {
2233 no_quit = 1;
2234 } else {
Peter Maydell05175532011-03-23 03:40:57 +00002235 goto invalid_sdl_args;
Jes Sorensen1472a952011-03-16 13:33:31 +01002236 }
2237 } else {
Peter Maydell05175532011-03-23 03:40:57 +00002238 invalid_sdl_args:
2239 fprintf(stderr, "Invalid SDL option string: %s\n", p);
2240 exit(1);
Jes Sorensen1472a952011-03-16 13:33:31 +01002241 }
2242 opts = nextopt;
2243 }
2244#else
2245 fprintf(stderr, "SDL support is disabled\n");
2246 exit(1);
2247#endif
Jes Sorensen3264ff12011-03-16 13:33:33 +01002248 } else if (strstart(p, "vnc", &opts)) {
Jes Sorensen821601e2011-03-16 13:33:36 +01002249#ifdef CONFIG_VNC
Jes Sorensen3264ff12011-03-16 13:33:33 +01002250 display_remote++;
2251
2252 if (*opts) {
2253 const char *nextopt;
2254
2255 if (strstart(opts, "=", &nextopt)) {
2256 vnc_display = nextopt;
2257 }
2258 }
2259 if (!vnc_display) {
2260 fprintf(stderr, "VNC requires a display argument vnc=<display>\n");
2261 exit(1);
2262 }
Jes Sorensen821601e2011-03-16 13:33:36 +01002263#else
2264 fprintf(stderr, "VNC support is disabled\n");
2265 exit(1);
2266#endif
Jes Sorensen1472a952011-03-16 13:33:31 +01002267 } else if (strstart(p, "curses", &opts)) {
2268#ifdef CONFIG_CURSES
2269 display = DT_CURSES;
2270#else
2271 fprintf(stderr, "Curses support is disabled\n");
2272 exit(1);
2273#endif
Anthony Liguori15546422013-02-20 07:43:25 -06002274 } else if (strstart(p, "gtk", &opts)) {
2275#ifdef CONFIG_GTK
2276 display = DT_GTK;
2277#else
2278 fprintf(stderr, "GTK support is disabled\n");
2279 exit(1);
2280#endif
Jes Sorensen4171d322011-03-16 13:33:32 +01002281 } else if (strstart(p, "none", &opts)) {
2282 display = DT_NONE;
Jes Sorensen1472a952011-03-16 13:33:31 +01002283 } else {
Jes Sorensen1472a952011-03-16 13:33:31 +01002284 fprintf(stderr, "Unknown display type: %s\n", p);
2285 exit(1);
2286 }
2287
2288 return display;
2289}
2290
Markus Armbruster7d4c3d52009-06-26 19:15:14 +02002291static int balloon_parse(const char *arg)
2292{
Gerd Hoffmann382f0742009-08-14 10:34:22 +02002293 QemuOpts *opts;
Markus Armbruster7d4c3d52009-06-26 19:15:14 +02002294
Gerd Hoffmann382f0742009-08-14 10:34:22 +02002295 if (strcmp(arg, "none") == 0) {
2296 return 0;
Markus Armbruster7d4c3d52009-06-26 19:15:14 +02002297 }
Gerd Hoffmann382f0742009-08-14 10:34:22 +02002298
2299 if (!strncmp(arg, "virtio", 6)) {
2300 if (arg[6] == ',') {
2301 /* have params -> parse them */
Gerd Hoffmann3329f072010-08-20 13:52:01 +02002302 opts = qemu_opts_parse(qemu_find_opts("device"), arg+7, 0);
Gerd Hoffmann382f0742009-08-14 10:34:22 +02002303 if (!opts)
2304 return -1;
2305 } else {
2306 /* create empty opts */
Peter Crosthwaite87ea75d2014-01-01 18:49:17 -08002307 opts = qemu_opts_create(qemu_find_opts("device"), NULL, 0,
2308 &error_abort);
Gerd Hoffmann382f0742009-08-14 10:34:22 +02002309 }
Alexander Graf29f82b32011-03-29 15:29:29 +02002310 qemu_opt_set(opts, "driver", "virtio-balloon");
Gerd Hoffmann382f0742009-08-14 10:34:22 +02002311 return 0;
2312 }
2313
2314 return -1;
Markus Armbruster7d4c3d52009-06-26 19:15:14 +02002315}
Markus Armbruster7d4c3d52009-06-26 19:15:14 +02002316
Paul Brook5cea8592009-05-30 00:52:44 +01002317char *qemu_find_file(int type, const char *name)
2318{
Gerd Hoffmann45240512013-03-08 11:42:24 +01002319 int i;
Paul Brook5cea8592009-05-30 00:52:44 +01002320 const char *subdir;
2321 char *buf;
2322
Peter Maydell31783202012-05-25 13:07:01 +01002323 /* Try the name as a straight path first */
2324 if (access(name, R_OK) == 0) {
Gerd Hoffmann45240512013-03-08 11:42:24 +01002325 trace_load_file(name, name);
Anthony Liguori7267c092011-08-20 22:09:37 -05002326 return g_strdup(name);
Paul Brook5cea8592009-05-30 00:52:44 +01002327 }
Gerd Hoffmann45240512013-03-08 11:42:24 +01002328
Paul Brook5cea8592009-05-30 00:52:44 +01002329 switch (type) {
2330 case QEMU_FILE_TYPE_BIOS:
2331 subdir = "";
2332 break;
2333 case QEMU_FILE_TYPE_KEYMAP:
2334 subdir = "keymaps/";
2335 break;
2336 default:
2337 abort();
2338 }
Gerd Hoffmann45240512013-03-08 11:42:24 +01002339
2340 for (i = 0; i < data_dir_idx; i++) {
2341 buf = g_strdup_printf("%s/%s%s", data_dir[i], subdir, name);
2342 if (access(buf, R_OK) == 0) {
2343 trace_load_file(name, buf);
2344 return buf;
2345 }
Anthony Liguori7267c092011-08-20 22:09:37 -05002346 g_free(buf);
Paul Brook5cea8592009-05-30 00:52:44 +01002347 }
Gerd Hoffmann45240512013-03-08 11:42:24 +01002348 return NULL;
Paul Brook5cea8592009-05-30 00:52:44 +01002349}
2350
Markus Armbrusterff952ba2010-01-29 19:48:57 +01002351static int device_help_func(QemuOpts *opts, void *opaque)
2352{
2353 return qdev_device_help(opts);
2354}
2355
Gerd Hoffmannf31d07d2009-07-31 12:25:37 +02002356static int device_init_func(QemuOpts *opts, void *opaque)
2357{
2358 DeviceState *dev;
2359
2360 dev = qdev_device_add(opts);
2361 if (!dev)
2362 return -1;
Paolo Bonzinib09995a2013-01-25 14:12:37 +01002363 object_unref(OBJECT(dev));
Gerd Hoffmannf31d07d2009-07-31 12:25:37 +02002364 return 0;
2365}
2366
Gerd Hoffmann1a688d32009-12-08 13:11:36 +01002367static int chardev_init_func(QemuOpts *opts, void *opaque)
2368{
Gerd Hoffmannbd2d80b2012-10-15 09:28:05 +02002369 Error *local_err = NULL;
Gerd Hoffmann1a688d32009-12-08 13:11:36 +01002370
Gerd Hoffmannbd2d80b2012-10-15 09:28:05 +02002371 qemu_chr_new_from_opts(opts, NULL, &local_err);
Markus Armbruster84d18f02014-01-30 15:07:28 +01002372 if (local_err) {
Seiji Aguchi4a44d852013-08-05 15:40:44 -04002373 error_report("%s", error_get_pretty(local_err));
Gerd Hoffmannbd2d80b2012-10-15 09:28:05 +02002374 error_free(local_err);
Gerd Hoffmann1a688d32009-12-08 13:11:36 +01002375 return -1;
Gerd Hoffmannbd2d80b2012-10-15 09:28:05 +02002376 }
Gerd Hoffmann1a688d32009-12-08 13:11:36 +01002377 return 0;
2378}
2379
Venkateswararao Jujjuri (JV)758e8e32010-06-14 13:34:41 -07002380#ifdef CONFIG_VIRTFS
Gautham R Shenoy74db9202010-04-29 17:44:43 +05302381static int fsdev_init_func(QemuOpts *opts, void *opaque)
2382{
2383 int ret;
2384 ret = qemu_fsdev_add(opts);
2385
2386 return ret;
2387}
2388#endif
2389
Gerd Hoffmann88589342009-12-08 13:11:50 +01002390static int mon_init_func(QemuOpts *opts, void *opaque)
2391{
2392 CharDriverState *chr;
2393 const char *chardev;
2394 const char *mode;
2395 int flags;
2396
2397 mode = qemu_opt_get(opts, "mode");
2398 if (mode == NULL) {
2399 mode = "readline";
2400 }
2401 if (strcmp(mode, "readline") == 0) {
2402 flags = MONITOR_USE_READLINE;
2403 } else if (strcmp(mode, "control") == 0) {
2404 flags = MONITOR_USE_CONTROL;
2405 } else {
2406 fprintf(stderr, "unknown monitor mode \"%s\"\n", mode);
2407 exit(1);
2408 }
2409
Daniel P. Berrange39eaab92010-06-07 15:42:31 +01002410 if (qemu_opt_get_bool(opts, "pretty", 0))
2411 flags |= MONITOR_USE_PRETTY;
2412
Gerd Hoffmann88589342009-12-08 13:11:50 +01002413 if (qemu_opt_get_bool(opts, "default", 0))
2414 flags |= MONITOR_IS_DEFAULT;
2415
2416 chardev = qemu_opt_get(opts, "chardev");
2417 chr = qemu_chr_find(chardev);
2418 if (chr == NULL) {
2419 fprintf(stderr, "chardev \"%s\" not found\n", chardev);
2420 exit(1);
2421 }
2422
Hans de Goede456d6062013-03-27 20:29:40 +01002423 qemu_chr_fe_claim_no_fail(chr);
Gerd Hoffmann88589342009-12-08 13:11:50 +01002424 monitor_init(chr, flags);
2425 return 0;
2426}
2427
Gerd Hoffmann6ca55822009-12-08 13:11:52 +01002428static void monitor_parse(const char *optarg, const char *mode)
Gerd Hoffmann88589342009-12-08 13:11:50 +01002429{
2430 static int monitor_device_index = 0;
2431 QemuOpts *opts;
2432 const char *p;
2433 char label[32];
2434 int def = 0;
2435
2436 if (strstart(optarg, "chardev:", &p)) {
2437 snprintf(label, sizeof(label), "%s", p);
2438 } else {
Jan Kiszka140e0652010-04-06 16:55:52 +02002439 snprintf(label, sizeof(label), "compat_monitor%d",
2440 monitor_device_index);
2441 if (monitor_device_index == 0) {
Gerd Hoffmann88589342009-12-08 13:11:50 +01002442 def = 1;
2443 }
2444 opts = qemu_chr_parse_compat(label, optarg);
2445 if (!opts) {
2446 fprintf(stderr, "parse error: %s\n", optarg);
2447 exit(1);
2448 }
2449 }
2450
Luiz Capitulino8be7e7e2012-03-20 15:51:57 -03002451 opts = qemu_opts_create(qemu_find_opts("mon"), label, 1, NULL);
Gerd Hoffmann88589342009-12-08 13:11:50 +01002452 if (!opts) {
2453 fprintf(stderr, "duplicate chardev: %s\n", label);
2454 exit(1);
2455 }
Gerd Hoffmann6ca55822009-12-08 13:11:52 +01002456 qemu_opt_set(opts, "mode", mode);
Gerd Hoffmann88589342009-12-08 13:11:50 +01002457 qemu_opt_set(opts, "chardev", label);
2458 if (def)
2459 qemu_opt_set(opts, "default", "on");
2460 monitor_device_index++;
2461}
2462
Gerd Hoffmannbd3c9482009-07-15 13:59:26 +02002463struct device_config {
2464 enum {
Gerd Hoffmannaee1b932009-12-08 13:11:53 +01002465 DEV_USB, /* -usbdevice */
2466 DEV_BT, /* -bt */
2467 DEV_SERIAL, /* -serial */
2468 DEV_PARALLEL, /* -parallel */
2469 DEV_VIRTCON, /* -virtioconsole */
H. Peter Anvinc9f398e2009-12-29 13:51:36 -08002470 DEV_DEBUGCON, /* -debugcon */
Markus Armbrusteref0c4a02012-02-07 15:09:13 +01002471 DEV_GDB, /* -gdb, -s */
Alexander Graf3ef669e2013-01-24 12:18:52 +01002472 DEV_SCLP, /* s390 sclp */
Gerd Hoffmannbd3c9482009-07-15 13:59:26 +02002473 } type;
2474 const char *cmdline;
Markus Armbrusterd9a59542012-02-07 15:09:12 +01002475 Location loc;
Blue Swirl72cf2d42009-09-12 07:36:22 +00002476 QTAILQ_ENTRY(device_config) next;
Gerd Hoffmannbd3c9482009-07-15 13:59:26 +02002477};
Blue Swirl4fdcac02012-10-28 11:04:47 +00002478
2479static QTAILQ_HEAD(, device_config) device_configs =
2480 QTAILQ_HEAD_INITIALIZER(device_configs);
Gerd Hoffmannbd3c9482009-07-15 13:59:26 +02002481
2482static void add_device_config(int type, const char *cmdline)
2483{
2484 struct device_config *conf;
2485
Anthony Liguori7267c092011-08-20 22:09:37 -05002486 conf = g_malloc0(sizeof(*conf));
Gerd Hoffmannbd3c9482009-07-15 13:59:26 +02002487 conf->type = type;
2488 conf->cmdline = cmdline;
Markus Armbrusterd9a59542012-02-07 15:09:12 +01002489 loc_save(&conf->loc);
Blue Swirl72cf2d42009-09-12 07:36:22 +00002490 QTAILQ_INSERT_TAIL(&device_configs, conf, next);
Gerd Hoffmannbd3c9482009-07-15 13:59:26 +02002491}
2492
2493static int foreach_device_config(int type, int (*func)(const char *cmdline))
2494{
2495 struct device_config *conf;
2496 int rc;
2497
Blue Swirl72cf2d42009-09-12 07:36:22 +00002498 QTAILQ_FOREACH(conf, &device_configs, next) {
Gerd Hoffmannbd3c9482009-07-15 13:59:26 +02002499 if (conf->type != type)
2500 continue;
Markus Armbrusterd9a59542012-02-07 15:09:12 +01002501 loc_push_restore(&conf->loc);
Gerd Hoffmannbd3c9482009-07-15 13:59:26 +02002502 rc = func(conf->cmdline);
Markus Armbrusterd9a59542012-02-07 15:09:12 +01002503 loc_pop(&conf->loc);
Gerd Hoffmannbd3c9482009-07-15 13:59:26 +02002504 if (0 != rc)
2505 return rc;
2506 }
2507 return 0;
2508}
2509
Gerd Hoffmann998bbd72009-12-08 13:11:41 +01002510static int serial_parse(const char *devname)
2511{
2512 static int index = 0;
2513 char label[32];
2514
2515 if (strcmp(devname, "none") == 0)
2516 return 0;
2517 if (index == MAX_SERIAL_PORTS) {
2518 fprintf(stderr, "qemu: too many serial ports\n");
2519 exit(1);
2520 }
2521 snprintf(label, sizeof(label), "serial%d", index);
Anthony Liguori27143a42011-08-15 11:17:36 -05002522 serial_hds[index] = qemu_chr_new(label, devname, NULL);
Gerd Hoffmann998bbd72009-12-08 13:11:41 +01002523 if (!serial_hds[index]) {
Peter Maydell52d06132012-07-09 04:28:30 +00002524 fprintf(stderr, "qemu: could not connect serial device"
2525 " to character backend '%s'\n", devname);
Gerd Hoffmann998bbd72009-12-08 13:11:41 +01002526 return -1;
2527 }
2528 index++;
2529 return 0;
2530}
2531
Gerd Hoffmann6a5e8b02009-12-08 13:11:42 +01002532static int parallel_parse(const char *devname)
2533{
2534 static int index = 0;
2535 char label[32];
2536
2537 if (strcmp(devname, "none") == 0)
2538 return 0;
2539 if (index == MAX_PARALLEL_PORTS) {
2540 fprintf(stderr, "qemu: too many parallel ports\n");
2541 exit(1);
2542 }
2543 snprintf(label, sizeof(label), "parallel%d", index);
Anthony Liguori27143a42011-08-15 11:17:36 -05002544 parallel_hds[index] = qemu_chr_new(label, devname, NULL);
Gerd Hoffmann6a5e8b02009-12-08 13:11:42 +01002545 if (!parallel_hds[index]) {
Peter Maydell52d06132012-07-09 04:28:30 +00002546 fprintf(stderr, "qemu: could not connect parallel device"
2547 " to character backend '%s'\n", devname);
Gerd Hoffmann6a5e8b02009-12-08 13:11:42 +01002548 return -1;
2549 }
2550 index++;
2551 return 0;
2552}
2553
Gerd Hoffmannaee1b932009-12-08 13:11:53 +01002554static int virtcon_parse(const char *devname)
2555{
Gerd Hoffmann3329f072010-08-20 13:52:01 +02002556 QemuOptsList *device = qemu_find_opts("device");
Gerd Hoffmannaee1b932009-12-08 13:11:53 +01002557 static int index = 0;
2558 char label[32];
Amit Shah392ecf52010-01-21 16:19:23 +05302559 QemuOpts *bus_opts, *dev_opts;
Gerd Hoffmannaee1b932009-12-08 13:11:53 +01002560
2561 if (strcmp(devname, "none") == 0)
2562 return 0;
2563 if (index == MAX_VIRTIO_CONSOLES) {
2564 fprintf(stderr, "qemu: too many virtio consoles\n");
2565 exit(1);
2566 }
Amit Shah392ecf52010-01-21 16:19:23 +05302567
Peter Crosthwaite87ea75d2014-01-01 18:49:17 -08002568 bus_opts = qemu_opts_create(device, NULL, 0, &error_abort);
Anthony Liguorie87f7fc2012-02-06 11:07:18 -06002569 if (arch_type == QEMU_ARCH_S390X) {
2570 qemu_opt_set(bus_opts, "driver", "virtio-serial-s390");
2571 } else {
2572 qemu_opt_set(bus_opts, "driver", "virtio-serial-pci");
Laszlo Ersek4d8b3c62013-03-21 00:23:13 +01002573 }
Amit Shah392ecf52010-01-21 16:19:23 +05302574
Peter Crosthwaite87ea75d2014-01-01 18:49:17 -08002575 dev_opts = qemu_opts_create(device, NULL, 0, &error_abort);
Amit Shah392ecf52010-01-21 16:19:23 +05302576 qemu_opt_set(dev_opts, "driver", "virtconsole");
2577
Gerd Hoffmannaee1b932009-12-08 13:11:53 +01002578 snprintf(label, sizeof(label), "virtcon%d", index);
Anthony Liguori27143a42011-08-15 11:17:36 -05002579 virtcon_hds[index] = qemu_chr_new(label, devname, NULL);
Gerd Hoffmannaee1b932009-12-08 13:11:53 +01002580 if (!virtcon_hds[index]) {
Peter Maydell52d06132012-07-09 04:28:30 +00002581 fprintf(stderr, "qemu: could not connect virtio console"
2582 " to character backend '%s'\n", devname);
Gerd Hoffmannaee1b932009-12-08 13:11:53 +01002583 return -1;
2584 }
Amit Shah392ecf52010-01-21 16:19:23 +05302585 qemu_opt_set(dev_opts, "chardev", label);
2586
Gerd Hoffmannaee1b932009-12-08 13:11:53 +01002587 index++;
2588 return 0;
2589}
2590
Alexander Graf3ef669e2013-01-24 12:18:52 +01002591static int sclp_parse(const char *devname)
2592{
2593 QemuOptsList *device = qemu_find_opts("device");
2594 static int index = 0;
2595 char label[32];
2596 QemuOpts *dev_opts;
2597
2598 if (strcmp(devname, "none") == 0) {
2599 return 0;
2600 }
2601 if (index == MAX_SCLP_CONSOLES) {
2602 fprintf(stderr, "qemu: too many sclp consoles\n");
2603 exit(1);
2604 }
2605
2606 assert(arch_type == QEMU_ARCH_S390X);
2607
2608 dev_opts = qemu_opts_create(device, NULL, 0, NULL);
2609 qemu_opt_set(dev_opts, "driver", "sclpconsole");
2610
2611 snprintf(label, sizeof(label), "sclpcon%d", index);
2612 sclp_hds[index] = qemu_chr_new(label, devname, NULL);
2613 if (!sclp_hds[index]) {
2614 fprintf(stderr, "qemu: could not connect sclp console"
2615 " to character backend '%s'\n", devname);
2616 return -1;
2617 }
2618 qemu_opt_set(dev_opts, "chardev", label);
2619
2620 index++;
2621 return 0;
2622}
2623
H. Peter Anvinc9f398e2009-12-29 13:51:36 -08002624static int debugcon_parse(const char *devname)
Laszlo Ersek4d8b3c62013-03-21 00:23:13 +01002625{
H. Peter Anvinc9f398e2009-12-29 13:51:36 -08002626 QemuOpts *opts;
2627
Anthony Liguori27143a42011-08-15 11:17:36 -05002628 if (!qemu_chr_new("debugcon", devname, NULL)) {
H. Peter Anvinc9f398e2009-12-29 13:51:36 -08002629 exit(1);
2630 }
Luiz Capitulino8be7e7e2012-03-20 15:51:57 -03002631 opts = qemu_opts_create(qemu_find_opts("device"), "debugcon", 1, NULL);
H. Peter Anvinc9f398e2009-12-29 13:51:36 -08002632 if (!opts) {
2633 fprintf(stderr, "qemu: already have a debugcon device\n");
2634 exit(1);
2635 }
2636 qemu_opt_set(opts, "driver", "isa-debugcon");
2637 qemu_opt_set(opts, "chardev", "debugcon");
2638 return 0;
2639}
2640
Marcel Apfelbaum0056ae22014-03-05 19:30:47 +02002641static MachineClass *machine_parse(const char *name)
Jan Kiszka9052ea62011-07-23 12:38:37 +02002642{
Marcel Apfelbaum0056ae22014-03-05 19:30:47 +02002643 MachineClass *mc = NULL;
Marcel Apfelbaum261747f2014-03-05 19:30:46 +02002644 GSList *el, *machines = object_class_get_list(TYPE_MACHINE, false);
Jan Kiszka9052ea62011-07-23 12:38:37 +02002645
2646 if (name) {
Marcel Apfelbaum0056ae22014-03-05 19:30:47 +02002647 mc = find_machine(name);
Jan Kiszka9052ea62011-07-23 12:38:37 +02002648 }
Marcel Apfelbaum0056ae22014-03-05 19:30:47 +02002649 if (mc) {
2650 return mc;
Jan Kiszka9052ea62011-07-23 12:38:37 +02002651 }
2652 printf("Supported machines are:\n");
Marcel Apfelbaum261747f2014-03-05 19:30:46 +02002653 for (el = machines; el; el = el->next) {
2654 MachineClass *mc = el->data;
Marcel Apfelbaum0056ae22014-03-05 19:30:47 +02002655 QEMUMachine *m = mc->qemu_machine;
Jan Kiszka9052ea62011-07-23 12:38:37 +02002656 if (m->alias) {
Peter Maydell3b264862012-02-22 22:13:11 +00002657 printf("%-20s %s (alias of %s)\n", m->alias, m->desc, m->name);
Jan Kiszka9052ea62011-07-23 12:38:37 +02002658 }
Peter Maydell3b264862012-02-22 22:13:11 +00002659 printf("%-20s %s%s\n", m->name, m->desc,
Jan Kiszka9052ea62011-07-23 12:38:37 +02002660 m->is_default ? " (default)" : "");
2661 }
Marcel Apfelbaum261747f2014-03-05 19:30:46 +02002662
2663 g_slist_free(machines);
Peter Maydellc8057f92012-08-02 13:45:54 +01002664 exit(!name || !is_help_option(name));
Jan Kiszka9052ea62011-07-23 12:38:37 +02002665}
2666
Aneesh Kumar K.V135a1292013-12-23 21:10:40 +05302667static int tcg_init(QEMUMachine *machine)
Anthony PERARD303d4e82010-09-21 20:05:31 +01002668{
Jan Kiszkad5ab9712011-08-02 16:10:21 +02002669 tcg_exec_init(tcg_tb_size * 1024 * 1024);
Anthony PERARD303d4e82010-09-21 20:05:31 +01002670 return 0;
2671}
2672
2673static struct {
2674 const char *opt_name;
2675 const char *name;
2676 int (*available)(void);
Aneesh Kumar K.V135a1292013-12-23 21:10:40 +05302677 int (*init)(QEMUMachine *);
liguangd5286af2013-01-24 13:03:27 +08002678 bool *allowed;
Anthony PERARD303d4e82010-09-21 20:05:31 +01002679} accel_list[] = {
2680 { "tcg", "tcg", tcg_available, tcg_init, &tcg_allowed },
Anthony PERARD3285cf42010-08-19 12:27:56 +01002681 { "xen", "Xen", xen_available, xen_init, &xen_allowed },
Anthony PERARD303d4e82010-09-21 20:05:31 +01002682 { "kvm", "KVM", kvm_available, kvm_init, &kvm_allowed },
Paolo Bonzinid4fce242013-10-18 13:51:11 +02002683 { "qtest", "QTest", qtest_available, qtest_init_accel, &qtest_allowed },
Anthony PERARD303d4e82010-09-21 20:05:31 +01002684};
2685
Aneesh Kumar K.V135a1292013-12-23 21:10:40 +05302686static int configure_accelerator(QEMUMachine *machine)
Anthony PERARD303d4e82010-09-21 20:05:31 +01002687{
Markus Armbruster36ad0e92013-07-04 15:09:20 +02002688 const char *p;
Anthony PERARD303d4e82010-09-21 20:05:31 +01002689 char buf[10];
2690 int i, ret;
liguang217e21b2013-01-24 13:03:26 +08002691 bool accel_initialised = false;
2692 bool init_failed = false;
Anthony PERARD303d4e82010-09-21 20:05:31 +01002693
Markus Armbruster36ad0e92013-07-04 15:09:20 +02002694 p = qemu_opt_get(qemu_get_machine_opts(), "accel");
Anthony PERARD303d4e82010-09-21 20:05:31 +01002695 if (p == NULL) {
2696 /* Use the default "accelerator", tcg */
2697 p = "tcg";
2698 }
2699
Peter Maydell1b785a92012-02-07 20:57:27 +00002700 while (!accel_initialised && *p != '\0') {
Anthony PERARD303d4e82010-09-21 20:05:31 +01002701 if (*p == ':') {
2702 p++;
2703 }
2704 p = get_opt_name(buf, sizeof (buf), p, ':');
2705 for (i = 0; i < ARRAY_SIZE(accel_list); i++) {
2706 if (strcmp(accel_list[i].opt_name, buf) == 0) {
liguange3c66d92013-01-24 13:03:25 +08002707 if (!accel_list[i].available()) {
2708 printf("%s not supported for this target\n",
2709 accel_list[i].name);
2710 continue;
2711 }
liguangd5286af2013-01-24 13:03:27 +08002712 *(accel_list[i].allowed) = true;
Aneesh Kumar K.V135a1292013-12-23 21:10:40 +05302713 ret = accel_list[i].init(machine);
Anthony PERARD303d4e82010-09-21 20:05:31 +01002714 if (ret < 0) {
liguang217e21b2013-01-24 13:03:26 +08002715 init_failed = true;
liguange3c66d92013-01-24 13:03:25 +08002716 fprintf(stderr, "failed to initialize %s: %s\n",
2717 accel_list[i].name,
2718 strerror(-ret));
liguangd5286af2013-01-24 13:03:27 +08002719 *(accel_list[i].allowed) = false;
Anthony PERARD303d4e82010-09-21 20:05:31 +01002720 } else {
liguang217e21b2013-01-24 13:03:26 +08002721 accel_initialised = true;
Anthony PERARD303d4e82010-09-21 20:05:31 +01002722 }
2723 break;
2724 }
2725 }
2726 if (i == ARRAY_SIZE(accel_list)) {
2727 fprintf(stderr, "\"%s\" accelerator does not exist.\n", buf);
2728 }
2729 }
2730
Peter Maydell1b785a92012-02-07 20:57:27 +00002731 if (!accel_initialised) {
liguang217e21b2013-01-24 13:03:26 +08002732 if (!init_failed) {
2733 fprintf(stderr, "No accelerator found!\n");
2734 }
Anthony PERARD303d4e82010-09-21 20:05:31 +01002735 exit(1);
2736 }
2737
2738 if (init_failed) {
2739 fprintf(stderr, "Back to %s accelerator.\n", accel_list[i].name);
2740 }
2741
Peter Maydell1b785a92012-02-07 20:57:27 +00002742 return !accel_initialised;
Anthony PERARD303d4e82010-09-21 20:05:31 +01002743}
2744
Gerd Hoffmannfd42dee2010-06-04 14:08:07 +02002745void qemu_add_exit_notifier(Notifier *notify)
2746{
2747 notifier_list_add(&exit_notifiers, notify);
2748}
2749
2750void qemu_remove_exit_notifier(Notifier *notify)
2751{
Paolo Bonzini31552522012-01-13 17:34:01 +01002752 notifier_remove(notify);
Gerd Hoffmannfd42dee2010-06-04 14:08:07 +02002753}
2754
2755static void qemu_run_exit_notifiers(void)
2756{
Jan Kiszka9e8dd452011-06-20 14:06:26 +02002757 notifier_list_notify(&exit_notifiers, NULL);
Gerd Hoffmannfd42dee2010-06-04 14:08:07 +02002758}
2759
Gleb Natapov4cab9462010-12-08 13:35:08 +02002760void qemu_add_machine_init_done_notifier(Notifier *notify)
2761{
2762 notifier_list_add(&machine_init_done_notifiers, notify);
2763}
2764
2765static void qemu_run_machine_init_done_notifiers(void)
2766{
Jan Kiszka9e8dd452011-06-20 14:06:26 +02002767 notifier_list_notify(&machine_init_done_notifiers, NULL);
Gleb Natapov4cab9462010-12-08 13:35:08 +02002768}
2769
Anthony Liguori6530a972010-01-22 09:18:06 -06002770static const QEMUOption *lookup_opt(int argc, char **argv,
2771 const char **poptarg, int *poptind)
2772{
2773 const QEMUOption *popt;
2774 int optind = *poptind;
2775 char *r = argv[optind];
2776 const char *optarg;
2777
Markus Armbruster0f0bc3f2010-02-18 20:13:51 +01002778 loc_set_cmdline(argv, optind, 1);
Anthony Liguori6530a972010-01-22 09:18:06 -06002779 optind++;
2780 /* Treat --foo the same as -foo. */
2781 if (r[1] == '-')
2782 r++;
2783 popt = qemu_options;
2784 for(;;) {
2785 if (!popt->name) {
Markus Armbruster0f0bc3f2010-02-18 20:13:51 +01002786 error_report("invalid option");
Anthony Liguori6530a972010-01-22 09:18:06 -06002787 exit(1);
2788 }
2789 if (!strcmp(popt->name, r + 1))
2790 break;
2791 popt++;
2792 }
2793 if (popt->flags & HAS_ARG) {
2794 if (optind >= argc) {
Markus Armbruster0f0bc3f2010-02-18 20:13:51 +01002795 error_report("requires an argument");
Anthony Liguori6530a972010-01-22 09:18:06 -06002796 exit(1);
2797 }
2798 optarg = argv[optind++];
Markus Armbruster0f0bc3f2010-02-18 20:13:51 +01002799 loc_set_cmdline(argv, optind - 2, 2);
Anthony Liguori6530a972010-01-22 09:18:06 -06002800 } else {
2801 optarg = NULL;
2802 }
2803
2804 *poptarg = optarg;
2805 *poptind = optind;
2806
2807 return popt;
2808}
2809
Anthony Liguori07501122011-08-20 22:38:31 -05002810static gpointer malloc_and_trace(gsize n_bytes)
2811{
2812 void *ptr = malloc(n_bytes);
Frediano Ziglioa74cd8c2011-08-31 09:25:35 +02002813 trace_g_malloc(n_bytes, ptr);
Anthony Liguori07501122011-08-20 22:38:31 -05002814 return ptr;
2815}
2816
2817static gpointer realloc_and_trace(gpointer mem, gsize n_bytes)
2818{
2819 void *ptr = realloc(mem, n_bytes);
Frediano Ziglioa74cd8c2011-08-31 09:25:35 +02002820 trace_g_realloc(mem, n_bytes, ptr);
Anthony Liguori07501122011-08-20 22:38:31 -05002821 return ptr;
2822}
2823
2824static void free_and_trace(gpointer mem)
2825{
Frediano Ziglioa74cd8c2011-08-31 09:25:35 +02002826 trace_g_free(mem);
Anthony Liguori07501122011-08-20 22:38:31 -05002827 free(mem);
2828}
2829
Anthony Liguori68d98d32012-06-25 14:36:33 -05002830static int object_set_property(const char *name, const char *value, void *opaque)
2831{
2832 Object *obj = OBJECT(opaque);
2833 StringInputVisitor *siv;
2834 Error *local_err = NULL;
2835
2836 if (strcmp(name, "qom-type") == 0 || strcmp(name, "id") == 0) {
2837 return 0;
2838 }
2839
2840 siv = string_input_visitor_new(value);
2841 object_property_set(obj, string_input_get_visitor(siv), name, &local_err);
2842 string_input_visitor_cleanup(siv);
2843
2844 if (local_err) {
2845 qerror_report_err(local_err);
2846 error_free(local_err);
2847 return -1;
2848 }
2849
2850 return 0;
2851}
2852
2853static int object_create(QemuOpts *opts, void *opaque)
2854{
2855 const char *type = qemu_opt_get(opts, "qom-type");
2856 const char *id = qemu_opts_id(opts);
Igor Mammedov90e9cf22014-01-16 17:34:37 +01002857 Error *local_err = NULL;
Anthony Liguori68d98d32012-06-25 14:36:33 -05002858 Object *obj;
2859
2860 g_assert(type != NULL);
2861
2862 if (id == NULL) {
2863 qerror_report(QERR_MISSING_PARAMETER, "id");
2864 return -1;
2865 }
2866
2867 obj = object_new(type);
2868 if (qemu_opt_foreach(opts, object_set_property, obj, 1) < 0) {
Paolo Bonzini28ec2592013-12-20 23:21:07 +01002869 object_unref(obj);
Anthony Liguori68d98d32012-06-25 14:36:33 -05002870 return -1;
2871 }
2872
Igor Mammedov269e09f2014-01-16 17:34:38 +01002873 if (!object_dynamic_cast(obj, TYPE_USER_CREATABLE)) {
2874 error_setg(&local_err, "object '%s' isn't supported by -object",
2875 id);
2876 goto out;
2877 }
2878
2879 user_creatable_complete(obj, &local_err);
2880 if (local_err) {
2881 goto out;
2882 }
2883
Anthony Liguori68d98d32012-06-25 14:36:33 -05002884 object_property_add_child(container_get(object_get_root(), "/objects"),
Igor Mammedov90e9cf22014-01-16 17:34:37 +01002885 id, obj, &local_err);
2886
Igor Mammedov269e09f2014-01-16 17:34:38 +01002887out:
Paolo Bonzini28ec2592013-12-20 23:21:07 +01002888 object_unref(obj);
Igor Mammedov90e9cf22014-01-16 17:34:37 +01002889 if (local_err) {
2890 qerror_report_err(local_err);
2891 error_free(local_err);
2892 return -1;
2893 }
Anthony Liguori68d98d32012-06-25 14:36:33 -05002894 return 0;
2895}
2896
malc902b3d52008-12-10 19:18:40 +00002897int main(int argc, char **argv, char **envp)
bellard0824d6f2003-06-24 13:42:40 +00002898{
thse4bcb142007-12-02 04:51:10 +00002899 int i;
Eduardo Habkostda1fcfd2010-04-06 19:22:07 -03002900 int snapshot, linux_boot;
Paolo Bonzini4e3de9e2010-03-10 11:38:48 +01002901 const char *icount_option = NULL;
bellard7f7f9872003-10-30 01:11:23 +00002902 const char *initrd_filename;
bellarda20dd502003-09-30 21:07:02 +00002903 const char *kernel_filename, *kernel_cmdline;
Markus Armbrustere3fdc532013-10-01 13:47:22 +02002904 const char *boot_order;
aliguori3023f332009-01-16 19:04:14 +00002905 DisplayState *ds;
malc9f227bc2012-08-27 18:33:22 +04002906 int cyls, heads, secs, translation;
2907 QemuOpts *hda_opts = NULL, *opts, *machine_opts;
Gerd Hoffmann03b0ba72010-08-20 13:52:02 +02002908 QemuOptsList *olist;
bellardcd6f1162004-05-13 22:02:20 +00002909 int optind;
Anthony Liguori6530a972010-01-22 09:18:06 -06002910 const char *optarg;
bellardd63d3072004-10-03 13:29:03 +00002911 const char *loadvm = NULL;
Marcel Apfelbaum0056ae22014-03-05 19:30:47 +02002912 MachineClass *machine_class;
bellardcc1daa42005-06-05 14:49:17 +00002913 QEMUMachine *machine;
j_mayer94fc95c2007-03-05 19:44:02 +00002914 const char *cpu_model;
Paolo Bonzini7f1b17f2012-05-10 09:39:17 +02002915 const char *vga_model = "none";
Paolo Bonzinid4fce242013-10-18 13:51:11 +02002916 const char *qtest_chrdev = NULL;
2917 const char *qtest_log = NULL;
ths93815bc2007-03-19 15:58:31 +00002918 const char *pid_file = NULL;
aliguori5bb79102008-10-13 03:12:02 +00002919 const char *incoming = NULL;
Jes Sorensen821601e2011-03-16 13:33:36 +01002920#ifdef CONFIG_VNC
Anthony Liguori993fbfd2009-05-21 16:54:00 -05002921 int show_vnc_port = 0;
Jes Sorensen821601e2011-03-16 13:33:36 +01002922#endif
Eduardo Habkost3ed2d9e2012-05-02 13:07:28 -03002923 bool defconfig = true;
Eduardo Habkostf29a5612012-05-02 13:07:29 -03002924 bool userconfig = true;
Matthew Fernandezc235d732011-06-07 16:32:40 +00002925 const char *log_mask = NULL;
2926 const char *log_file = NULL;
Anthony Liguori07501122011-08-20 22:38:31 -05002927 GMemVTable mem_trace = {
2928 .malloc = malloc_and_trace,
2929 .realloc = realloc_and_trace,
2930 .free = free_and_trace,
2931 };
Lluís23d15e82011-08-31 20:31:31 +02002932 const char *trace_events = NULL;
Lluíse4858972011-08-31 20:31:03 +02002933 const char *trace_file = NULL;
Stefan Hajnoczi0b5538c2011-02-26 18:38:39 +00002934
Gerd Hoffmannfd42dee2010-06-04 14:08:07 +02002935 atexit(qemu_run_exit_notifiers);
Markus Armbruster65abca02010-02-24 14:37:14 +01002936 error_set_progname(argv[0]);
Fam Zheng10f5bff2014-02-10 14:48:51 +08002937 qemu_init_exec_dir(argv[0]);
Markus Armbruster65abca02010-02-24 14:37:14 +01002938
Anthony Liguori07501122011-08-20 22:38:31 -05002939 g_mem_set_vtable(&mem_trace);
Andreas Färberdb529aa2011-10-31 19:14:56 +01002940 if (!g_thread_supported()) {
Alon Levy42ed3722011-12-20 13:41:04 +02002941#if !GLIB_CHECK_VERSION(2, 31, 0)
Andreas Färberdb529aa2011-10-31 19:14:56 +01002942 g_thread_init(NULL);
Alon Levy42ed3722011-12-20 13:41:04 +02002943#else
2944 fprintf(stderr, "glib threading failed to initialize.\n");
2945 exit(1);
2946#endif
Andreas Färberdb529aa2011-10-31 19:14:56 +01002947 }
Anthony Liguori07501122011-08-20 22:38:31 -05002948
Andreas Färber1b71f7c2012-03-04 21:32:35 +01002949 module_call_init(MODULE_INIT_QOM);
2950
Paolo Bonzini4d454572012-11-26 16:03:42 +01002951 qemu_add_opts(&qemu_drive_opts);
Amos Kong968854c2013-11-09 12:15:47 +08002952 qemu_add_drive_opts(&qemu_legacy_drive_opts);
2953 qemu_add_drive_opts(&qemu_common_drive_opts);
2954 qemu_add_drive_opts(&qemu_drive_opts);
Paolo Bonzini4d454572012-11-26 16:03:42 +01002955 qemu_add_opts(&qemu_chardev_opts);
2956 qemu_add_opts(&qemu_device_opts);
2957 qemu_add_opts(&qemu_netdev_opts);
2958 qemu_add_opts(&qemu_net_opts);
2959 qemu_add_opts(&qemu_rtc_opts);
2960 qemu_add_opts(&qemu_global_opts);
2961 qemu_add_opts(&qemu_mon_opts);
2962 qemu_add_opts(&qemu_trace_opts);
2963 qemu_add_opts(&qemu_option_rom_opts);
2964 qemu_add_opts(&qemu_machine_opts);
Michael Tokarev12b7f572013-06-24 15:06:52 +04002965 qemu_add_opts(&qemu_smp_opts);
Paolo Bonzini4d454572012-11-26 16:03:42 +01002966 qemu_add_opts(&qemu_boot_opts);
2967 qemu_add_opts(&qemu_sandbox_opts);
2968 qemu_add_opts(&qemu_add_fd_opts);
2969 qemu_add_opts(&qemu_object_opts);
Stefan Bergerd1a0cf72013-02-27 12:47:49 -05002970 qemu_add_opts(&qemu_tpmdev_opts);
Satoru Moriya888a6bc2013-04-19 16:42:06 +02002971 qemu_add_opts(&qemu_realtime_opts);
Seiji Aguchi5e2ac512013-07-03 23:02:46 -04002972 qemu_add_opts(&qemu_msg_opts);
Dr. David Alan Gilbert5d12f962014-01-30 10:20:30 +00002973 qemu_add_opts(&qemu_name_opts);
Paolo Bonzini4d454572012-11-26 16:03:42 +01002974
Luiz Capitulino5db9d4d2011-09-05 16:36:31 -03002975 runstate_init();
2976
Jan Kiszka68752042009-09-15 13:36:04 +02002977 init_clocks();
Alex Bligh884f17c2013-08-21 16:03:04 +01002978 rtc_clock = QEMU_CLOCK_HOST;
Jan Kiszka68752042009-09-15 13:36:04 +02002979
Richard Hendersonb6a3e692013-06-04 11:24:49 -07002980 qemu_init_auxval(envp);
Richard Henderson664d2c42013-06-10 09:05:09 -07002981 qemu_cache_utils_init();
malc902b3d52008-12-10 19:18:40 +00002982
Blue Swirl72cf2d42009-09-12 07:36:22 +00002983 QLIST_INIT (&vm_change_state_head);
Jes Sorensenfe98ac12010-06-10 11:42:21 +02002984 os_setup_early_signal_handling();
bellardbe995c22006-06-25 16:25:21 +00002985
Anthony Liguorif80f9ec2009-05-20 18:38:09 -05002986 module_call_init(MODULE_INIT_MACHINE);
Marcel Apfelbaum0056ae22014-03-05 19:30:47 +02002987 machine_class = find_default_machine();
j_mayer94fc95c2007-03-05 19:44:02 +00002988 cpu_model = NULL;
aurel324fc5d072008-04-27 21:39:40 +00002989 ram_size = 0;
bellard33e39632003-07-06 17:15:21 +00002990 snapshot = 0;
malc9f227bc2012-08-27 18:33:22 +04002991 cyls = heads = secs = 0;
2992 translation = BIOS_ATA_TRANSLATION_AUTO;
bellardc4b1fcc2004-03-14 21:44:30 +00002993
aliguori268a3622009-04-21 22:30:27 +00002994 for (i = 0; i < MAX_NODES; i++) {
2995 node_mem[i] = 0;
Chegu Vinodee785fe2012-07-16 21:31:30 -07002996 node_cpumask[i] = bitmap_new(MAX_CPUMASK_BITS);
aliguori268a3622009-04-21 22:30:27 +00002997 }
2998
aliguori268a3622009-04-21 22:30:27 +00002999 nb_numa_nodes = 0;
bellard7c9d8e02005-11-15 22:16:05 +00003000 nb_nics = 0;
ths3b46e622007-09-17 08:09:54 +00003001
Peter Lieven142c6b12013-03-21 13:07:10 +01003002 bdrv_init_with_whitelist();
3003
Kewei Yu2c02d1a2013-12-31 18:36:08 +08003004 autostart = 1;
blueswir141bd6392008-10-05 09:56:21 +00003005
Anthony Liguori292444c2010-01-21 10:57:58 -06003006 /* first pass of option parsing */
3007 optind = 1;
3008 while (optind < argc) {
3009 if (argv[optind][0] != '-') {
3010 /* disk image */
Anthony Liguori28e68d62010-01-27 10:46:00 -06003011 optind++;
Anthony Liguori292444c2010-01-21 10:57:58 -06003012 continue;
3013 } else {
3014 const QEMUOption *popt;
3015
3016 popt = lookup_opt(argc, argv, &optarg, &optind);
3017 switch (popt->index) {
3018 case QEMU_OPTION_nodefconfig:
Eduardo Habkost3ed2d9e2012-05-02 13:07:28 -03003019 defconfig = false;
Anthony Liguori292444c2010-01-21 10:57:58 -06003020 break;
Eduardo Habkostf29a5612012-05-02 13:07:29 -03003021 case QEMU_OPTION_nouserconfig:
3022 userconfig = false;
3023 break;
Anthony Liguori292444c2010-01-21 10:57:58 -06003024 }
3025 }
3026 }
3027
3028 if (defconfig) {
Kevin Wolfdcfb0932010-03-05 17:25:55 +01003029 int ret;
Eduardo Habkostf29a5612012-05-02 13:07:29 -03003030 ret = qemu_read_default_config_files(userconfig);
Eduardo Habkostb5a8fe52012-05-02 13:07:25 -03003031 if (ret < 0) {
Kevin Wolfdcfb0932010-03-05 17:25:55 +01003032 exit(1);
Anthony Liguori292444c2010-01-21 10:57:58 -06003033 }
3034 }
3035
3036 /* second pass of option parsing */
bellardcd6f1162004-05-13 22:02:20 +00003037 optind = 1;
bellard0824d6f2003-06-24 13:42:40 +00003038 for(;;) {
bellardcd6f1162004-05-13 22:02:20 +00003039 if (optind >= argc)
bellard0824d6f2003-06-24 13:42:40 +00003040 break;
Anthony Liguori6530a972010-01-22 09:18:06 -06003041 if (argv[optind][0] != '-') {
malc9f227bc2012-08-27 18:33:22 +04003042 hda_opts = drive_add(IF_DEFAULT, 0, argv[optind++], HD_OPTS);
bellardcd6f1162004-05-13 22:02:20 +00003043 } else {
3044 const QEMUOption *popt;
3045
Anthony Liguori6530a972010-01-22 09:18:06 -06003046 popt = lookup_opt(argc, argv, &optarg, &optind);
Blue Swirlad960902010-03-29 19:23:52 +00003047 if (!(popt->arch_mask & arch_type)) {
3048 printf("Option %s not supported for this target\n", popt->name);
3049 exit(1);
3050 }
bellardcd6f1162004-05-13 22:02:20 +00003051 switch(popt->index) {
bellardcc1daa42005-06-05 14:49:17 +00003052 case QEMU_OPTION_M:
Marcel Apfelbaum0056ae22014-03-05 19:30:47 +02003053 machine_class = machine_parse(optarg);
bellardcc1daa42005-06-05 14:49:17 +00003054 break;
Jan Kiszkae43d5942012-10-05 14:51:40 -03003055 case QEMU_OPTION_no_kvm_irqchip: {
3056 olist = qemu_find_opts("machine");
3057 qemu_opts_parse(olist, "kernel_irqchip=off", 0);
3058 break;
3059 }
j_mayer94fc95c2007-03-05 19:44:02 +00003060 case QEMU_OPTION_cpu:
3061 /* hw initialization will check this */
Eduardo Habkostecf40be2012-03-09 16:19:07 -03003062 cpu_model = optarg;
j_mayer94fc95c2007-03-05 19:44:02 +00003063 break;
bellardcd6f1162004-05-13 22:02:20 +00003064 case QEMU_OPTION_hda:
malc9f227bc2012-08-27 18:33:22 +04003065 {
3066 char buf[256];
3067 if (cyls == 0)
3068 snprintf(buf, sizeof(buf), "%s", HD_OPTS);
3069 else
3070 snprintf(buf, sizeof(buf),
3071 "%s,cyls=%d,heads=%d,secs=%d%s",
3072 HD_OPTS , cyls, heads, secs,
3073 translation == BIOS_ATA_TRANSLATION_LBA ?
3074 ",trans=lba" :
3075 translation == BIOS_ATA_TRANSLATION_NONE ?
3076 ",trans=none" : "");
3077 drive_add(IF_DEFAULT, 0, optarg, buf);
3078 break;
3079 }
bellardcd6f1162004-05-13 22:02:20 +00003080 case QEMU_OPTION_hdb:
bellardcc1daa42005-06-05 14:49:17 +00003081 case QEMU_OPTION_hdc:
3082 case QEMU_OPTION_hdd:
Markus Armbruster2292dda2011-01-28 11:21:41 +01003083 drive_add(IF_DEFAULT, popt->index - QEMU_OPTION_hda, optarg,
3084 HD_OPTS);
bellardfc01f7e2003-06-30 10:03:06 +00003085 break;
thse4bcb142007-12-02 04:51:10 +00003086 case QEMU_OPTION_drive:
Michael Tokareve2982c32011-03-30 16:31:05 +04003087 if (drive_def(optarg) == NULL) {
3088 exit(1);
3089 }
thse4bcb142007-12-02 04:51:10 +00003090 break;
Gerd Hoffmannd058fe02009-07-31 12:25:36 +02003091 case QEMU_OPTION_set:
3092 if (qemu_set_option(optarg) != 0)
3093 exit(1);
3094 break;
Gerd Hoffmannd0fef6f2009-12-08 13:11:34 +01003095 case QEMU_OPTION_global:
3096 if (qemu_global_option(optarg) != 0)
3097 exit(1);
3098 break;
balrog3e3d5812007-04-30 02:09:25 +00003099 case QEMU_OPTION_mtdblock:
Markus Armbruster2292dda2011-01-28 11:21:41 +01003100 drive_add(IF_MTD, -1, optarg, MTD_OPTS);
balrog3e3d5812007-04-30 02:09:25 +00003101 break;
pbrooka1bb27b2007-04-06 16:49:48 +00003102 case QEMU_OPTION_sd:
Peter Crosthwaite80f4d9f2013-02-28 18:23:14 +00003103 drive_add(IF_SD, -1, optarg, SD_OPTS);
pbrooka1bb27b2007-04-06 16:49:48 +00003104 break;
j_mayer86f55662007-04-24 06:52:59 +00003105 case QEMU_OPTION_pflash:
Markus Armbruster2292dda2011-01-28 11:21:41 +01003106 drive_add(IF_PFLASH, -1, optarg, PFLASH_OPTS);
j_mayer86f55662007-04-24 06:52:59 +00003107 break;
bellardcd6f1162004-05-13 22:02:20 +00003108 case QEMU_OPTION_snapshot:
bellard33e39632003-07-06 17:15:21 +00003109 snapshot = 1;
3110 break;
bellardcd6f1162004-05-13 22:02:20 +00003111 case QEMU_OPTION_hdachs:
bellard330d0412003-07-26 18:11:40 +00003112 {
bellard330d0412003-07-26 18:11:40 +00003113 const char *p;
3114 p = optarg;
3115 cyls = strtol(p, (char **)&p, 0);
bellard46d47672004-11-16 01:45:27 +00003116 if (cyls < 1 || cyls > 16383)
3117 goto chs_fail;
bellard330d0412003-07-26 18:11:40 +00003118 if (*p != ',')
3119 goto chs_fail;
3120 p++;
3121 heads = strtol(p, (char **)&p, 0);
bellard46d47672004-11-16 01:45:27 +00003122 if (heads < 1 || heads > 16)
3123 goto chs_fail;
bellard330d0412003-07-26 18:11:40 +00003124 if (*p != ',')
3125 goto chs_fail;
3126 p++;
3127 secs = strtol(p, (char **)&p, 0);
bellard46d47672004-11-16 01:45:27 +00003128 if (secs < 1 || secs > 63)
3129 goto chs_fail;
3130 if (*p == ',') {
3131 p++;
Paolo Bonzinif31c41f2014-02-08 11:01:54 +01003132 if (!strcmp(p, "large")) {
3133 translation = BIOS_ATA_TRANSLATION_LARGE;
3134 } else if (!strcmp(p, "rechs")) {
3135 translation = BIOS_ATA_TRANSLATION_RECHS;
3136 } else if (!strcmp(p, "none")) {
bellard46d47672004-11-16 01:45:27 +00003137 translation = BIOS_ATA_TRANSLATION_NONE;
Paolo Bonzinif31c41f2014-02-08 11:01:54 +01003138 } else if (!strcmp(p, "lba")) {
bellard46d47672004-11-16 01:45:27 +00003139 translation = BIOS_ATA_TRANSLATION_LBA;
Paolo Bonzinif31c41f2014-02-08 11:01:54 +01003140 } else if (!strcmp(p, "auto")) {
bellard46d47672004-11-16 01:45:27 +00003141 translation = BIOS_ATA_TRANSLATION_AUTO;
Paolo Bonzinif31c41f2014-02-08 11:01:54 +01003142 } else {
bellard46d47672004-11-16 01:45:27 +00003143 goto chs_fail;
Paolo Bonzinif31c41f2014-02-08 11:01:54 +01003144 }
bellard46d47672004-11-16 01:45:27 +00003145 } else if (*p != '\0') {
bellardc4b1fcc2004-03-14 21:44:30 +00003146 chs_fail:
bellard46d47672004-11-16 01:45:27 +00003147 fprintf(stderr, "qemu: invalid physical CHS format\n");
3148 exit(1);
bellardc4b1fcc2004-03-14 21:44:30 +00003149 }
malc9f227bc2012-08-27 18:33:22 +04003150 if (hda_opts != NULL) {
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +02003151 char num[16];
3152 snprintf(num, sizeof(num), "%d", cyls);
3153 qemu_opt_set(hda_opts, "cyls", num);
3154 snprintf(num, sizeof(num), "%d", heads);
3155 qemu_opt_set(hda_opts, "heads", num);
3156 snprintf(num, sizeof(num), "%d", secs);
3157 qemu_opt_set(hda_opts, "secs", num);
Paolo Bonzinif31c41f2014-02-08 11:01:54 +01003158 if (translation == BIOS_ATA_TRANSLATION_LARGE) {
3159 qemu_opt_set(hda_opts, "trans", "large");
3160 } else if (translation == BIOS_ATA_TRANSLATION_RECHS) {
3161 qemu_opt_set(hda_opts, "trans", "rechs");
3162 } else if (translation == BIOS_ATA_TRANSLATION_LBA) {
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +02003163 qemu_opt_set(hda_opts, "trans", "lba");
Paolo Bonzinif31c41f2014-02-08 11:01:54 +01003164 } else if (translation == BIOS_ATA_TRANSLATION_NONE) {
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +02003165 qemu_opt_set(hda_opts, "trans", "none");
Paolo Bonzinif31c41f2014-02-08 11:01:54 +01003166 }
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +02003167 }
bellard330d0412003-07-26 18:11:40 +00003168 }
3169 break;
aliguori268a3622009-04-21 22:30:27 +00003170 case QEMU_OPTION_numa:
aliguori268a3622009-04-21 22:30:27 +00003171 numa_add(optarg);
3172 break;
Jes Sorensen1472a952011-03-16 13:33:31 +01003173 case QEMU_OPTION_display:
3174 display_type = select_display(optarg);
3175 break;
bellardcd6f1162004-05-13 22:02:20 +00003176 case QEMU_OPTION_nographic:
Anthony Liguori993fbfd2009-05-21 16:54:00 -05003177 display_type = DT_NOGRAPHIC;
bellarda20dd502003-09-30 21:07:02 +00003178 break;
balrog4d3b6f62008-02-10 16:33:14 +00003179 case QEMU_OPTION_curses:
Jes Sorensen47b05362011-03-16 13:33:35 +01003180#ifdef CONFIG_CURSES
Anthony Liguori993fbfd2009-05-21 16:54:00 -05003181 display_type = DT_CURSES;
Jes Sorensen47b05362011-03-16 13:33:35 +01003182#else
3183 fprintf(stderr, "Curses support is disabled\n");
3184 exit(1);
balrog4d3b6f62008-02-10 16:33:14 +00003185#endif
Jes Sorensen47b05362011-03-16 13:33:35 +01003186 break;
balroga171fe32007-04-30 01:48:07 +00003187 case QEMU_OPTION_portrait:
Vasily Khoruzhick93128052011-06-17 13:04:36 +03003188 graphic_rotate = 90;
3189 break;
3190 case QEMU_OPTION_rotate:
3191 graphic_rotate = strtol(optarg, (char **) &optarg, 10);
3192 if (graphic_rotate != 0 && graphic_rotate != 90 &&
3193 graphic_rotate != 180 && graphic_rotate != 270) {
3194 fprintf(stderr,
3195 "qemu: only 90, 180, 270 deg rotation is available\n");
3196 exit(1);
3197 }
balroga171fe32007-04-30 01:48:07 +00003198 break;
bellardcd6f1162004-05-13 22:02:20 +00003199 case QEMU_OPTION_kernel:
Peter Maydella0abe472012-02-08 05:41:39 +00003200 qemu_opts_set(qemu_find_opts("machine"), 0, "kernel", optarg);
3201 break;
3202 case QEMU_OPTION_initrd:
3203 qemu_opts_set(qemu_find_opts("machine"), 0, "initrd", optarg);
bellarda20dd502003-09-30 21:07:02 +00003204 break;
bellardcd6f1162004-05-13 22:02:20 +00003205 case QEMU_OPTION_append:
Peter Maydella0abe472012-02-08 05:41:39 +00003206 qemu_opts_set(qemu_find_opts("machine"), 0, "append", optarg);
bellard313aa562003-08-10 21:52:11 +00003207 break;
Grant Likely412beee2012-03-02 11:56:38 +00003208 case QEMU_OPTION_dtb:
3209 qemu_opts_set(qemu_find_opts("machine"), 0, "dtb", optarg);
3210 break;
bellardcd6f1162004-05-13 22:02:20 +00003211 case QEMU_OPTION_cdrom:
Markus Armbruster2292dda2011-01-28 11:21:41 +01003212 drive_add(IF_DEFAULT, 2, optarg, CDROM_OPTS);
bellard36b486b2003-11-11 13:36:08 +00003213 break;
bellardcd6f1162004-05-13 22:02:20 +00003214 case QEMU_OPTION_boot:
Markus Armbruster8281abd2013-06-14 13:15:03 +02003215 opts = qemu_opts_parse(qemu_find_opts("boot-opts"), optarg, 1);
3216 if (!opts) {
3217 exit(1);
bellard36b486b2003-11-11 13:36:08 +00003218 }
3219 break;
bellardcd6f1162004-05-13 22:02:20 +00003220 case QEMU_OPTION_fda:
bellardcd6f1162004-05-13 22:02:20 +00003221 case QEMU_OPTION_fdb:
Markus Armbruster2292dda2011-01-28 11:21:41 +01003222 drive_add(IF_FLOPPY, popt->index - QEMU_OPTION_fda,
3223 optarg, FD_OPTS);
bellardc45886d2004-01-05 00:02:06 +00003224 break;
bellard52ca8d62006-06-14 16:03:05 +00003225 case QEMU_OPTION_no_fd_bootchk:
3226 fd_bootchk = 0;
3227 break;
Mark McLoughlina1ea4582009-10-08 19:58:26 +01003228 case QEMU_OPTION_netdev:
Gerd Hoffmann3329f072010-08-20 13:52:01 +02003229 if (net_client_parse(qemu_find_opts("netdev"), optarg) == -1) {
Mark McLoughlina1ea4582009-10-08 19:58:26 +01003230 exit(1);
3231 }
3232 break;
bellard7c9d8e02005-11-15 22:16:05 +00003233 case QEMU_OPTION_net:
Gerd Hoffmann3329f072010-08-20 13:52:01 +02003234 if (net_client_parse(qemu_find_opts("net"), optarg) == -1) {
bellardc4b1fcc2004-03-14 21:44:30 +00003235 exit(1);
3236 }
bellard702c6512004-04-02 21:21:32 +00003237 break;
Ronnie Sahlbergf9dadc92012-01-26 09:39:02 +11003238#ifdef CONFIG_LIBISCSI
3239 case QEMU_OPTION_iscsi:
3240 opts = qemu_opts_parse(qemu_find_opts("iscsi"), optarg, 0);
3241 if (!opts) {
3242 exit(1);
3243 }
3244 break;
3245#endif
bellardc7f74642004-08-24 21:57:12 +00003246#ifdef CONFIG_SLIRP
3247 case QEMU_OPTION_tftp:
Jan Kiszkaad196a92009-06-24 14:42:28 +02003248 legacy_tftp_prefix = optarg;
bellard9bf05442004-08-25 22:12:49 +00003249 break;
ths47d5d012007-02-20 00:05:08 +00003250 case QEMU_OPTION_bootp:
Jan Kiszkaad196a92009-06-24 14:42:28 +02003251 legacy_bootp_filename = optarg;
ths47d5d012007-02-20 00:05:08 +00003252 break;
bellard9bf05442004-08-25 22:12:49 +00003253 case QEMU_OPTION_redir:
Markus Armbruster07527062009-10-06 12:16:57 +01003254 if (net_slirp_redir(optarg) < 0)
3255 exit(1);
bellard9bf05442004-08-25 22:12:49 +00003256 break;
bellardc7f74642004-08-24 21:57:12 +00003257#endif
balrogdc72ac12008-11-09 00:04:26 +00003258 case QEMU_OPTION_bt:
Gerd Hoffmannbd3c9482009-07-15 13:59:26 +02003259 add_device_config(DEV_BT, optarg);
balrogdc72ac12008-11-09 00:04:26 +00003260 break;
bellard1d14ffa2005-10-30 18:58:22 +00003261 case QEMU_OPTION_audio_help:
3262 AUD_help ();
3263 exit (0);
3264 break;
3265 case QEMU_OPTION_soundhw:
3266 select_soundhw (optarg);
3267 break;
bellardcd6f1162004-05-13 22:02:20 +00003268 case QEMU_OPTION_h:
ths15f82202007-06-29 23:26:08 +00003269 help(0);
bellardcd6f1162004-05-13 22:02:20 +00003270 break;
pbrook9bd7e6d2009-04-07 22:58:45 +00003271 case QEMU_OPTION_version:
3272 version();
3273 exit(0);
3274 break;
aurel3200f82b82008-04-27 21:12:55 +00003275 case QEMU_OPTION_m: {
Jes Sorensen70b4f4b2011-01-05 11:41:02 +01003276 int64_t value;
Markus Armbrusterff961012012-08-15 13:12:19 +02003277 uint64_t sz;
Markus Armbruster961b42b2011-11-22 09:46:03 +01003278 char *end;
aurel3200f82b82008-04-27 21:12:55 +00003279
Markus Armbruster961b42b2011-11-22 09:46:03 +01003280 value = strtosz(optarg, &end);
3281 if (value < 0 || *end) {
aurel3200f82b82008-04-27 21:12:55 +00003282 fprintf(stderr, "qemu: invalid ram size: %s\n", optarg);
bellardcd6f1162004-05-13 22:02:20 +00003283 exit(1);
3284 }
Markus Armbrusterff961012012-08-15 13:12:19 +02003285 sz = QEMU_ALIGN_UP((uint64_t)value, 8192);
3286 ram_size = sz;
3287 if (ram_size != sz) {
aurel3200f82b82008-04-27 21:12:55 +00003288 fprintf(stderr, "qemu: ram size too large\n");
3289 exit(1);
3290 }
bellardcd6f1162004-05-13 22:02:20 +00003291 break;
aurel3200f82b82008-04-27 21:12:55 +00003292 }
Stefan Bergerd1a0cf72013-02-27 12:47:49 -05003293#ifdef CONFIG_TPM
3294 case QEMU_OPTION_tpmdev:
3295 if (tpm_config_parse(qemu_find_opts("tpmdev"), optarg) < 0) {
3296 exit(1);
3297 }
3298 break;
3299#endif
Marcelo Tosattic9027602010-03-01 20:25:08 -03003300 case QEMU_OPTION_mempath:
3301 mem_path = optarg;
3302 break;
Marcelo Tosattic9027602010-03-01 20:25:08 -03003303 case QEMU_OPTION_mem_prealloc:
3304 mem_prealloc = 1;
3305 break;
bellardcd6f1162004-05-13 22:02:20 +00003306 case QEMU_OPTION_d:
Matthew Fernandezc235d732011-06-07 16:32:40 +00003307 log_mask = optarg;
3308 break;
3309 case QEMU_OPTION_D:
3310 log_file = optarg;
bellardcd6f1162004-05-13 22:02:20 +00003311 break;
bellardcd6f1162004-05-13 22:02:20 +00003312 case QEMU_OPTION_s:
Markus Armbrusteref0c4a02012-02-07 15:09:13 +01003313 add_device_config(DEV_GDB, "tcp::" DEFAULT_GDBSTUB_PORT);
bellardcd6f1162004-05-13 22:02:20 +00003314 break;
aliguori59030a82009-04-05 18:43:41 +00003315 case QEMU_OPTION_gdb:
Markus Armbrusteref0c4a02012-02-07 15:09:13 +01003316 add_device_config(DEV_GDB, optarg);
bellardcd6f1162004-05-13 22:02:20 +00003317 break;
bellardcd6f1162004-05-13 22:02:20 +00003318 case QEMU_OPTION_L:
Gerd Hoffmann45240512013-03-08 11:42:24 +01003319 if (data_dir_idx < ARRAY_SIZE(data_dir)) {
3320 data_dir[data_dir_idx++] = optarg;
3321 }
bellardcd6f1162004-05-13 22:02:20 +00003322 break;
j_mayer1192dad2007-10-05 13:08:35 +00003323 case QEMU_OPTION_bios:
Gerd Hoffmann1442d3e2013-10-15 16:57:45 +02003324 qemu_opts_set(qemu_find_opts("machine"), 0, "firmware", optarg);
j_mayer1192dad2007-10-05 13:08:35 +00003325 break;
aurel321b530a62009-04-05 20:08:59 +00003326 case QEMU_OPTION_singlestep:
3327 singlestep = 1;
3328 break;
bellardcd6f1162004-05-13 22:02:20 +00003329 case QEMU_OPTION_S:
pbrook3c07f8e2007-01-21 16:47:01 +00003330 autostart = 0;
bellardcd6f1162004-05-13 22:02:20 +00003331 break;
bellard3d11d0e2004-12-12 16:56:30 +00003332 case QEMU_OPTION_k:
3333 keyboard_layout = optarg;
3334 break;
bellardee22c2f2004-06-03 12:49:50 +00003335 case QEMU_OPTION_localtime:
3336 rtc_utc = 0;
3337 break;
malc3893c122008-09-28 00:42:05 +00003338 case QEMU_OPTION_vga:
Blue Swirla369da52011-09-27 19:15:42 +00003339 vga_model = optarg;
Paolo Bonzini7f1b17f2012-05-10 09:39:17 +02003340 default_vga = 0;
bellard1bfe8562004-07-08 21:17:50 +00003341 break;
bellarde9b137c2004-06-21 16:46:10 +00003342 case QEMU_OPTION_g:
3343 {
3344 const char *p;
3345 int w, h, depth;
3346 p = optarg;
3347 w = strtol(p, (char **)&p, 10);
3348 if (w <= 0) {
3349 graphic_error:
3350 fprintf(stderr, "qemu: invalid resolution or depth\n");
3351 exit(1);
3352 }
3353 if (*p != 'x')
3354 goto graphic_error;
3355 p++;
3356 h = strtol(p, (char **)&p, 10);
3357 if (h <= 0)
3358 goto graphic_error;
3359 if (*p == 'x') {
3360 p++;
3361 depth = strtol(p, (char **)&p, 10);
ths5fafdf22007-09-16 21:08:06 +00003362 if (depth != 8 && depth != 15 && depth != 16 &&
bellarde9b137c2004-06-21 16:46:10 +00003363 depth != 24 && depth != 32)
3364 goto graphic_error;
3365 } else if (*p == '\0') {
3366 depth = graphic_depth;
3367 } else {
3368 goto graphic_error;
3369 }
ths3b46e622007-09-17 08:09:54 +00003370
bellarde9b137c2004-06-21 16:46:10 +00003371 graphic_width = w;
3372 graphic_height = h;
3373 graphic_depth = depth;
3374 }
3375 break;
ths20d8a3e2007-02-18 17:04:49 +00003376 case QEMU_OPTION_echr:
3377 {
3378 char *r;
3379 term_escape_char = strtol(optarg, &r, 0);
3380 if (r == optarg)
3381 printf("Bad argument to echr\n");
3382 break;
3383 }
bellard82c643f2004-07-14 17:28:13 +00003384 case QEMU_OPTION_monitor:
Gerd Hoffmann6ca55822009-12-08 13:11:52 +01003385 default_monitor = 0;
Luiz Capitulino70e098a2013-05-16 12:02:55 -04003386 if (strncmp(optarg, "none", 4)) {
3387 monitor_parse(optarg, "readline");
3388 }
Gerd Hoffmann6ca55822009-12-08 13:11:52 +01003389 break;
3390 case QEMU_OPTION_qmp:
3391 monitor_parse(optarg, "control");
Anthony Liguori2d114dc2010-03-21 14:14:38 -05003392 default_monitor = 0;
bellard82c643f2004-07-14 17:28:13 +00003393 break;
Gerd Hoffmann22a0e042009-12-08 13:11:51 +01003394 case QEMU_OPTION_mon:
Gerd Hoffmann3329f072010-08-20 13:52:01 +02003395 opts = qemu_opts_parse(qemu_find_opts("mon"), optarg, 1);
Gerd Hoffmann22a0e042009-12-08 13:11:51 +01003396 if (!opts) {
Gerd Hoffmann22a0e042009-12-08 13:11:51 +01003397 exit(1);
3398 }
Anthony Liguori2d114dc2010-03-21 14:14:38 -05003399 default_monitor = 0;
Gerd Hoffmann22a0e042009-12-08 13:11:51 +01003400 break;
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003401 case QEMU_OPTION_chardev:
Gerd Hoffmann3329f072010-08-20 13:52:01 +02003402 opts = qemu_opts_parse(qemu_find_opts("chardev"), optarg, 1);
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003403 if (!opts) {
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003404 exit(1);
3405 }
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003406 break;
Gautham R Shenoy74db9202010-04-29 17:44:43 +05303407 case QEMU_OPTION_fsdev:
Gerd Hoffmann03b0ba72010-08-20 13:52:02 +02003408 olist = qemu_find_opts("fsdev");
3409 if (!olist) {
3410 fprintf(stderr, "fsdev is not supported by this qemu build.\n");
3411 exit(1);
3412 }
3413 opts = qemu_opts_parse(olist, optarg, 1);
Gautham R Shenoy74db9202010-04-29 17:44:43 +05303414 if (!opts) {
Gautham R Shenoy74db9202010-04-29 17:44:43 +05303415 exit(1);
3416 }
3417 break;
Gautham R Shenoy3d54abc2010-04-29 17:45:03 +05303418 case QEMU_OPTION_virtfs: {
Stefan Hajnoczie14ea472011-03-16 08:31:43 +00003419 QemuOpts *fsdev;
3420 QemuOpts *device;
M. Mohan Kumar84a87cc2011-12-14 13:58:47 +05303421 const char *writeout, *sock_fd, *socket;
Gautham R Shenoy3d54abc2010-04-29 17:45:03 +05303422
Gerd Hoffmann03b0ba72010-08-20 13:52:02 +02003423 olist = qemu_find_opts("virtfs");
3424 if (!olist) {
3425 fprintf(stderr, "virtfs is not supported by this qemu build.\n");
3426 exit(1);
3427 }
3428 opts = qemu_opts_parse(olist, optarg, 1);
Gautham R Shenoy3d54abc2010-04-29 17:45:03 +05303429 if (!opts) {
Gautham R Shenoy3d54abc2010-04-29 17:45:03 +05303430 exit(1);
3431 }
3432
Aneesh Kumar K.Vfbcbf102011-10-13 12:28:04 +05303433 if (qemu_opt_get(opts, "fsdriver") == NULL ||
Aneesh Kumar K.V99519f02011-12-14 13:48:59 +05303434 qemu_opt_get(opts, "mount_tag") == NULL) {
3435 fprintf(stderr, "Usage: -virtfs fsdriver,mount_tag=tag.\n");
Venkateswararao Jujjuri (JV)9ce56db2010-06-14 13:34:40 -07003436 exit(1);
3437 }
Stefan Hajnoczie14ea472011-03-16 08:31:43 +00003438 fsdev = qemu_opts_create(qemu_find_opts("fsdev"),
Luiz Capitulino8be7e7e2012-03-20 15:51:57 -03003439 qemu_opt_get(opts, "mount_tag"),
3440 1, NULL);
Stefan Hajnoczie14ea472011-03-16 08:31:43 +00003441 if (!fsdev) {
3442 fprintf(stderr, "duplicate fsdev id: %s\n",
3443 qemu_opt_get(opts, "mount_tag"));
Gautham R Shenoy3d54abc2010-04-29 17:45:03 +05303444 exit(1);
3445 }
Aneesh Kumar K.Vd3ab98e2011-10-12 19:11:23 +05303446
3447 writeout = qemu_opt_get(opts, "writeout");
3448 if (writeout) {
3449#ifdef CONFIG_SYNC_FILE_RANGE
3450 qemu_opt_set(fsdev, "writeout", writeout);
3451#else
3452 fprintf(stderr, "writeout=immediate not supported on "
3453 "this platform\n");
3454 exit(1);
3455#endif
3456 }
Aneesh Kumar K.Vfbcbf102011-10-13 12:28:04 +05303457 qemu_opt_set(fsdev, "fsdriver", qemu_opt_get(opts, "fsdriver"));
Stefan Hajnoczie14ea472011-03-16 08:31:43 +00003458 qemu_opt_set(fsdev, "path", qemu_opt_get(opts, "path"));
3459 qemu_opt_set(fsdev, "security_model",
3460 qemu_opt_get(opts, "security_model"));
M. Mohan Kumar84a87cc2011-12-14 13:58:47 +05303461 socket = qemu_opt_get(opts, "socket");
3462 if (socket) {
3463 qemu_opt_set(fsdev, "socket", socket);
3464 }
M. Mohan Kumar4c793dd2011-12-14 13:49:28 +05303465 sock_fd = qemu_opt_get(opts, "sock_fd");
3466 if (sock_fd) {
3467 qemu_opt_set(fsdev, "sock_fd", sock_fd);
3468 }
Gautham R Shenoy3d54abc2010-04-29 17:45:03 +05303469
M. Mohan Kumar2c74c2c2011-10-25 12:10:39 +05303470 qemu_opt_set_bool(fsdev, "readonly",
3471 qemu_opt_get_bool(opts, "readonly", 0));
Peter Crosthwaite87ea75d2014-01-01 18:49:17 -08003472 device = qemu_opts_create(qemu_find_opts("device"), NULL, 0,
3473 &error_abort);
Stefan Hajnoczie14ea472011-03-16 08:31:43 +00003474 qemu_opt_set(device, "driver", "virtio-9p-pci");
3475 qemu_opt_set(device, "fsdev",
3476 qemu_opt_get(opts, "mount_tag"));
3477 qemu_opt_set(device, "mount_tag",
3478 qemu_opt_get(opts, "mount_tag"));
Gautham R Shenoy3d54abc2010-04-29 17:45:03 +05303479 break;
3480 }
Aneesh Kumar K.V9db221a2011-10-25 12:10:40 +05303481 case QEMU_OPTION_virtfs_synth: {
3482 QemuOpts *fsdev;
3483 QemuOpts *device;
3484
Luiz Capitulino8be7e7e2012-03-20 15:51:57 -03003485 fsdev = qemu_opts_create(qemu_find_opts("fsdev"), "v_synth",
3486 1, NULL);
Aneesh Kumar K.V9db221a2011-10-25 12:10:40 +05303487 if (!fsdev) {
3488 fprintf(stderr, "duplicate option: %s\n", "virtfs_synth");
3489 exit(1);
3490 }
3491 qemu_opt_set(fsdev, "fsdriver", "synth");
Aneesh Kumar K.V9db221a2011-10-25 12:10:40 +05303492
Peter Crosthwaite87ea75d2014-01-01 18:49:17 -08003493 device = qemu_opts_create(qemu_find_opts("device"), NULL, 0,
3494 &error_abort);
Aneesh Kumar K.V9db221a2011-10-25 12:10:40 +05303495 qemu_opt_set(device, "driver", "virtio-9p-pci");
3496 qemu_opt_set(device, "fsdev", "v_synth");
3497 qemu_opt_set(device, "mount_tag", "v_synth");
3498 break;
3499 }
bellard82c643f2004-07-14 17:28:13 +00003500 case QEMU_OPTION_serial:
Gerd Hoffmann998bbd72009-12-08 13:11:41 +01003501 add_device_config(DEV_SERIAL, optarg);
3502 default_serial = 0;
Jan Kiszka18141ed2010-03-07 11:28:40 +01003503 if (strncmp(optarg, "mon:", 4) == 0) {
3504 default_monitor = 0;
3505 }
bellard82c643f2004-07-14 17:28:13 +00003506 break;
Richard W.M. Jones9dd986c2009-04-25 13:56:19 +01003507 case QEMU_OPTION_watchdog:
Markus Armbruster09aaa162009-08-21 10:31:34 +02003508 if (watchdog) {
3509 fprintf(stderr,
3510 "qemu: only one watchdog option may be given\n");
3511 return 1;
3512 }
3513 watchdog = optarg;
Richard W.M. Jones9dd986c2009-04-25 13:56:19 +01003514 break;
3515 case QEMU_OPTION_watchdog_action:
3516 if (select_watchdog_action(optarg) == -1) {
3517 fprintf(stderr, "Unknown -watchdog-action parameter\n");
3518 exit(1);
3519 }
3520 break;
aliguori51ecf132009-01-15 20:06:40 +00003521 case QEMU_OPTION_virtiocon:
Gerd Hoffmannaee1b932009-12-08 13:11:53 +01003522 add_device_config(DEV_VIRTCON, optarg);
3523 default_virtcon = 0;
Jan Kiszka18141ed2010-03-07 11:28:40 +01003524 if (strncmp(optarg, "mon:", 4) == 0) {
3525 default_monitor = 0;
3526 }
aliguori51ecf132009-01-15 20:06:40 +00003527 break;
bellard6508fe52005-01-15 12:02:56 +00003528 case QEMU_OPTION_parallel:
Gerd Hoffmann6a5e8b02009-12-08 13:11:42 +01003529 add_device_config(DEV_PARALLEL, optarg);
3530 default_parallel = 0;
Jan Kiszka18141ed2010-03-07 11:28:40 +01003531 if (strncmp(optarg, "mon:", 4) == 0) {
3532 default_monitor = 0;
3533 }
bellard6508fe52005-01-15 12:02:56 +00003534 break;
H. Peter Anvinc9f398e2009-12-29 13:51:36 -08003535 case QEMU_OPTION_debugcon:
3536 add_device_config(DEV_DEBUGCON, optarg);
3537 break;
bellardd63d3072004-10-03 13:29:03 +00003538 case QEMU_OPTION_loadvm:
3539 loadvm = optarg;
3540 break;
3541 case QEMU_OPTION_full_screen:
3542 full_screen = 1;
3543 break;
ths43523e92007-02-18 18:19:32 +00003544 case QEMU_OPTION_no_frame:
3545 no_frame = 1;
3546 break;
ths3780e192007-06-21 21:08:02 +00003547 case QEMU_OPTION_alt_grab:
3548 alt_grab = 1;
3549 break;
Dustin Kirkland0ca9f8a2009-09-17 15:48:04 -05003550 case QEMU_OPTION_ctrl_grab:
3551 ctrl_grab = 1;
3552 break;
ths667acca2006-12-11 02:08:05 +00003553 case QEMU_OPTION_no_quit:
3554 no_quit = 1;
3555 break;
aliguori7d957bd2009-01-15 22:14:11 +00003556 case QEMU_OPTION_sdl:
Michael Tokarev24f6ff82013-06-15 14:44:20 +04003557#ifdef CONFIG_SDL
Anthony Liguori993fbfd2009-05-21 16:54:00 -05003558 display_type = DT_SDL;
aliguori7d957bd2009-01-15 22:14:11 +00003559 break;
Jes Sorensen58fc0962011-03-16 13:33:34 +01003560#else
Jes Sorensen58fc0962011-03-16 13:33:34 +01003561 fprintf(stderr, "SDL support is disabled\n");
3562 exit(1);
ths667acca2006-12-11 02:08:05 +00003563#endif
bellardf7cce892004-12-08 22:21:25 +00003564 case QEMU_OPTION_pidfile:
ths93815bc2007-03-19 15:58:31 +00003565 pid_file = optarg;
bellardf7cce892004-12-08 22:21:25 +00003566 break;
bellarda09db212005-04-30 16:10:35 +00003567 case QEMU_OPTION_win2k_hack:
3568 win2k_install_hack = 1;
3569 break;
Jan Kiszka433acf02012-01-23 20:15:12 +01003570 case QEMU_OPTION_rtc_td_hack: {
3571 static GlobalProperty slew_lost_ticks[] = {
3572 {
3573 .driver = "mc146818rtc",
3574 .property = "lost_tick_policy",
3575 .value = "slew",
3576 },
3577 { /* end of list */ }
3578 };
3579
3580 qdev_prop_register_global_list(slew_lost_ticks);
aliguori73822ec2009-01-15 20:11:34 +00003581 break;
Jan Kiszka433acf02012-01-23 20:15:12 +01003582 }
aliguori8a92ea22009-02-27 20:12:36 +00003583 case QEMU_OPTION_acpitable:
Laszlo Ersek0c764a92013-03-21 00:23:17 +01003584 opts = qemu_opts_parse(qemu_find_opts("acpi"), optarg, 1);
Laszlo Ersekf46e7202013-11-28 18:12:59 +01003585 if (!opts) {
3586 exit(1);
3587 }
Laszlo Ersek0c764a92013-03-21 00:23:17 +01003588 do_acpitable_option(opts);
aliguori8a92ea22009-02-27 20:12:36 +00003589 break;
aliguorib6f6e3d2009-04-17 18:59:56 +00003590 case QEMU_OPTION_smbios:
Markus Armbruster4f953d22013-08-16 15:18:29 +02003591 opts = qemu_opts_parse(qemu_find_opts("smbios"), optarg, 0);
Laszlo Ersekf46e7202013-11-28 18:12:59 +01003592 if (!opts) {
3593 exit(1);
3594 }
Markus Armbruster4f953d22013-08-16 15:18:29 +02003595 do_smbios_option(opts);
aliguorib6f6e3d2009-04-17 18:59:56 +00003596 break;
aliguori7ba1e612008-11-05 16:04:33 +00003597 case QEMU_OPTION_enable_kvm:
Anthony PERARD303d4e82010-09-21 20:05:31 +01003598 olist = qemu_find_opts("machine");
Anthony PERARD303d4e82010-09-21 20:05:31 +01003599 qemu_opts_parse(olist, "accel=kvm", 0);
3600 break;
3601 case QEMU_OPTION_machine:
3602 olist = qemu_find_opts("machine");
Jan Kiszka9052ea62011-07-23 12:38:37 +02003603 opts = qemu_opts_parse(olist, optarg, 1);
Anthony PERARD303d4e82010-09-21 20:05:31 +01003604 if (!opts) {
Anthony PERARD303d4e82010-09-21 20:05:31 +01003605 exit(1);
3606 }
Jan Kiszka2645c6d2011-07-25 18:11:20 +02003607 optarg = qemu_opt_get(opts, "type");
3608 if (optarg) {
Marcel Apfelbaum0056ae22014-03-05 19:30:47 +02003609 machine_class = machine_parse(optarg);
Jan Kiszka2645c6d2011-07-25 18:11:20 +02003610 }
aliguori7ba1e612008-11-05 16:04:33 +00003611 break;
Jan Kiszkaa0dac022012-10-05 14:51:45 -03003612 case QEMU_OPTION_no_kvm:
3613 olist = qemu_find_opts("machine");
3614 qemu_opts_parse(olist, "accel=tcg", 0);
3615 break;
Jan Kiszka4086bde2012-10-05 14:51:41 -03003616 case QEMU_OPTION_no_kvm_pit: {
3617 fprintf(stderr, "Warning: KVM PIT can no longer be disabled "
3618 "separately.\n");
3619 break;
3620 }
Jan Kiszkac21fb4f2012-10-05 14:51:42 -03003621 case QEMU_OPTION_no_kvm_pit_reinjection: {
3622 static GlobalProperty kvm_pit_lost_tick_policy[] = {
3623 {
3624 .driver = "kvm-pit",
3625 .property = "lost_tick_policy",
3626 .value = "discard",
3627 },
3628 { /* end of list */ }
3629 };
3630
3631 fprintf(stderr, "Warning: option deprecated, use "
3632 "lost_tick_policy property of kvm-pit instead.\n");
3633 qdev_prop_register_global_list(kvm_pit_lost_tick_policy);
3634 break;
3635 }
bellardbb36d472005-11-05 14:22:28 +00003636 case QEMU_OPTION_usb:
Peter Maydellfa5358c2012-11-22 16:48:45 +00003637 olist = qemu_find_opts("machine");
3638 qemu_opts_parse(olist, "usb=on", 0);
bellardbb36d472005-11-05 14:22:28 +00003639 break;
bellarda594cfb2005-11-06 16:13:29 +00003640 case QEMU_OPTION_usbdevice:
Peter Maydellfa5358c2012-11-22 16:48:45 +00003641 olist = qemu_find_opts("machine");
3642 qemu_opts_parse(olist, "usb=on", 0);
Gerd Hoffmannbd3c9482009-07-15 13:59:26 +02003643 add_device_config(DEV_USB, optarg);
3644 break;
3645 case QEMU_OPTION_device:
Gerd Hoffmann3329f072010-08-20 13:52:01 +02003646 if (!qemu_opts_parse(qemu_find_opts("device"), optarg, 1)) {
Gerd Hoffmannf31d07d2009-07-31 12:25:37 +02003647 exit(1);
3648 }
bellarda594cfb2005-11-06 16:13:29 +00003649 break;
bellard6a00d602005-11-21 23:25:50 +00003650 case QEMU_OPTION_smp:
Michael Tokarev12b7f572013-06-24 15:06:52 +04003651 if (!qemu_opts_parse(qemu_find_opts("smp-opts"), optarg, 1)) {
Jes Sorensen6be68d72009-07-23 17:03:42 +02003652 exit(1);
3653 }
bellard6a00d602005-11-21 23:25:50 +00003654 break;
bellard24236862006-04-30 21:28:36 +00003655 case QEMU_OPTION_vnc:
Jes Sorensen821601e2011-03-16 13:33:36 +01003656#ifdef CONFIG_VNC
Gerd Hoffmann6b62dc22010-08-25 10:51:06 +02003657 display_remote++;
Jes Sorensen821601e2011-03-16 13:33:36 +01003658 vnc_display = optarg;
3659#else
3660 fprintf(stderr, "VNC support is disabled\n");
3661 exit(1);
3662#endif
3663 break;
bellard6515b202006-05-03 22:02:44 +00003664 case QEMU_OPTION_no_acpi:
3665 acpi_enabled = 0;
3666 break;
aliguori16b29ae2008-12-17 23:28:44 +00003667 case QEMU_OPTION_no_hpet:
3668 no_hpet = 1;
3669 break;
Markus Armbruster7d4c3d52009-06-26 19:15:14 +02003670 case QEMU_OPTION_balloon:
3671 if (balloon_parse(optarg) < 0) {
3672 fprintf(stderr, "Unknown -balloon argument %s\n", optarg);
3673 exit(1);
3674 }
Eduardo Habkostdf97b922009-06-10 16:34:08 -03003675 break;
bellardd1beab82006-10-02 19:44:22 +00003676 case QEMU_OPTION_no_reboot:
3677 no_reboot = 1;
3678 break;
aurel32b2f76162008-04-11 21:35:52 +00003679 case QEMU_OPTION_no_shutdown:
3680 no_shutdown = 1;
3681 break;
balrog9467cd42007-05-01 01:34:14 +00003682 case QEMU_OPTION_show_cursor:
3683 cursor_hide = 0;
3684 break;
blueswir18fcb1b92008-09-18 18:29:08 +00003685 case QEMU_OPTION_uuid:
3686 if(qemu_uuid_parse(optarg, qemu_uuid) < 0) {
3687 fprintf(stderr, "Fail to parse UUID string."
3688 " Wrong format.\n");
3689 exit(1);
3690 }
Markus Armbrusterfc3b3292013-08-16 15:18:31 +02003691 qemu_uuid_set = true;
blueswir18fcb1b92008-09-18 18:29:08 +00003692 break;
ths9ae02552007-01-05 17:39:04 +00003693 case QEMU_OPTION_option_rom:
3694 if (nb_option_roms >= MAX_OPTION_ROMS) {
3695 fprintf(stderr, "Too many option ROMs\n");
3696 exit(1);
3697 }
Gleb Natapov2e55e842010-12-08 13:35:07 +02003698 opts = qemu_opts_parse(qemu_find_opts("option-rom"), optarg, 1);
Markus Armbruster49295eb2013-02-08 21:22:19 +01003699 if (!opts) {
3700 exit(1);
3701 }
Gleb Natapov2e55e842010-12-08 13:35:07 +02003702 option_rom[nb_option_roms].name = qemu_opt_get(opts, "romfile");
3703 option_rom[nb_option_roms].bootindex =
3704 qemu_opt_get_number(opts, "bootindex", -1);
3705 if (!option_rom[nb_option_roms].name) {
3706 fprintf(stderr, "Option ROM file is not specified\n");
3707 exit(1);
3708 }
ths9ae02552007-01-05 17:39:04 +00003709 nb_option_roms++;
3710 break;
pbrook8e716212007-01-20 17:12:09 +00003711 case QEMU_OPTION_semihosting:
3712 semihosting_enabled = 1;
3713 break;
Jan Kiszka88eed342012-10-05 14:51:44 -03003714 case QEMU_OPTION_tdf:
3715 fprintf(stderr, "Warning: user space PIT time drift fix "
3716 "is no longer supported.\n");
3717 break;
thsc35734b2007-03-19 15:17:08 +00003718 case QEMU_OPTION_name:
Dr. David Alan Gilbert5d12f962014-01-30 10:20:30 +00003719 opts = qemu_opts_parse(qemu_find_opts("name"), optarg, 1);
3720 if (!opts) {
3721 exit(1);
3722 }
3723 parse_name(opts);
thsc35734b2007-03-19 15:17:08 +00003724 break;
blueswir166508602007-05-01 14:16:52 +00003725 case QEMU_OPTION_prom_env:
3726 if (nb_prom_envs >= MAX_PROM_ENVS) {
3727 fprintf(stderr, "Too many prom variables\n");
3728 exit(1);
3729 }
3730 prom_envs[nb_prom_envs] = optarg;
3731 nb_prom_envs++;
3732 break;
balrog2b8f2d42007-07-27 22:08:46 +00003733 case QEMU_OPTION_old_param:
3734 old_param = 1;
ths05ebd532008-01-08 19:32:16 +00003735 break;
thsf3dcfad2007-08-24 01:26:02 +00003736 case QEMU_OPTION_clock:
Alex Bligh6d327172013-08-21 16:02:59 +01003737 /* Clock options no longer exist. Keep this option for
3738 * backward compatibility.
3739 */
thsf3dcfad2007-08-24 01:26:02 +00003740 break;
bellard7e0af5d02007-11-07 16:24:33 +00003741 case QEMU_OPTION_startdate:
Jan Kiszka1ed2fc12009-09-15 13:36:04 +02003742 configure_rtc_date_offset(optarg, 1);
3743 break;
3744 case QEMU_OPTION_rtc:
Gerd Hoffmann3329f072010-08-20 13:52:01 +02003745 opts = qemu_opts_parse(qemu_find_opts("rtc"), optarg, 0);
Jan Kiszka1ed2fc12009-09-15 13:36:04 +02003746 if (!opts) {
Jan Kiszka1ed2fc12009-09-15 13:36:04 +02003747 exit(1);
bellard7e0af5d02007-11-07 16:24:33 +00003748 }
Jan Kiszka1ed2fc12009-09-15 13:36:04 +02003749 configure_rtc(opts);
bellard7e0af5d02007-11-07 16:24:33 +00003750 break;
bellard26a5f132008-05-28 12:30:31 +00003751 case QEMU_OPTION_tb_size:
Jan Kiszkad5ab9712011-08-02 16:10:21 +02003752 tcg_tb_size = strtol(optarg, NULL, 0);
3753 if (tcg_tb_size < 0) {
3754 tcg_tb_size = 0;
3755 }
bellard26a5f132008-05-28 12:30:31 +00003756 break;
pbrook2e70f6e2008-06-29 01:03:05 +00003757 case QEMU_OPTION_icount:
Paolo Bonzini4e3de9e2010-03-10 11:38:48 +01003758 icount_option = optarg;
pbrook2e70f6e2008-06-29 01:03:05 +00003759 break;
aliguori5bb79102008-10-13 03:12:02 +00003760 case QEMU_OPTION_incoming:
3761 incoming = optarg;
Stefano Stabellini81323a62012-01-18 12:23:13 +00003762 runstate_set(RUN_STATE_INMIGRATE);
aliguori5bb79102008-10-13 03:12:02 +00003763 break;
Gerd Hoffmannd8c208d2009-12-08 13:11:46 +01003764 case QEMU_OPTION_nodefaults:
3765 default_serial = 0;
3766 default_parallel = 0;
Gerd Hoffmannaee1b932009-12-08 13:11:53 +01003767 default_virtcon = 0;
Christian Borntraeger2e788492013-01-25 06:00:19 +00003768 default_sclp = 0;
Gerd Hoffmannd8c208d2009-12-08 13:11:46 +01003769 default_monitor = 0;
Gerd Hoffmanncb4522c2009-12-08 13:11:47 +01003770 default_net = 0;
Gerd Hoffmannac33f8f2009-12-16 14:25:39 +01003771 default_floppy = 0;
3772 default_cdrom = 0;
3773 default_sdcard = 0;
Paolo Bonzini7f1b17f2012-05-10 09:39:17 +02003774 default_vga = 0;
Gerd Hoffmannd8c208d2009-12-08 13:11:46 +01003775 break;
aliguorie37630c2009-04-22 15:19:10 +00003776 case QEMU_OPTION_xen_domid:
Blue Swirlad960902010-03-29 19:23:52 +00003777 if (!(xen_available())) {
3778 printf("Option %s not supported for this target\n", popt->name);
3779 exit(1);
3780 }
aliguorie37630c2009-04-22 15:19:10 +00003781 xen_domid = atoi(optarg);
3782 break;
3783 case QEMU_OPTION_xen_create:
Blue Swirlad960902010-03-29 19:23:52 +00003784 if (!(xen_available())) {
3785 printf("Option %s not supported for this target\n", popt->name);
3786 exit(1);
3787 }
aliguorie37630c2009-04-22 15:19:10 +00003788 xen_mode = XEN_CREATE;
3789 break;
3790 case QEMU_OPTION_xen_attach:
Blue Swirlad960902010-03-29 19:23:52 +00003791 if (!(xen_available())) {
3792 printf("Option %s not supported for this target\n", popt->name);
3793 exit(1);
3794 }
aliguorie37630c2009-04-22 15:19:10 +00003795 xen_mode = XEN_ATTACH;
3796 break;
Prerna Saxenaab6540d2010-08-09 11:48:32 +01003797 case QEMU_OPTION_trace:
Lluíse4858972011-08-31 20:31:03 +02003798 {
Prerna Saxenaab6540d2010-08-09 11:48:32 +01003799 opts = qemu_opts_parse(qemu_find_opts("trace"), optarg, 0);
Lluíse4858972011-08-31 20:31:03 +02003800 if (!opts) {
3801 exit(1);
Prerna Saxenaab6540d2010-08-09 11:48:32 +01003802 }
Lluís23d15e82011-08-31 20:31:31 +02003803 trace_events = qemu_opt_get(opts, "events");
Lluíse4858972011-08-31 20:31:03 +02003804 trace_file = qemu_opt_get(opts, "file");
Prerna Saxenaab6540d2010-08-09 11:48:32 +01003805 break;
Lluíse4858972011-08-31 20:31:03 +02003806 }
Gerd Hoffmann715a6642009-10-14 10:39:28 +02003807 case QEMU_OPTION_readconfig:
3808 {
Kevin Wolfdcfb0932010-03-05 17:25:55 +01003809 int ret = qemu_read_config_file(optarg);
3810 if (ret < 0) {
3811 fprintf(stderr, "read config %s: %s\n", optarg,
3812 strerror(-ret));
Gerd Hoffmann715a6642009-10-14 10:39:28 +02003813 exit(1);
3814 }
Gerd Hoffmann715a6642009-10-14 10:39:28 +02003815 break;
3816 }
Gerd Hoffmann29b00402010-03-11 11:13:27 -03003817 case QEMU_OPTION_spice:
3818 olist = qemu_find_opts("spice");
3819 if (!olist) {
3820 fprintf(stderr, "spice is not supported by this qemu build.\n");
3821 exit(1);
3822 }
3823 opts = qemu_opts_parse(olist, optarg, 0);
3824 if (!opts) {
Gerd Hoffmann29b00402010-03-11 11:13:27 -03003825 exit(1);
3826 }
Gerd Hoffmannf963e4d2013-02-25 16:02:30 +01003827 display_remote++;
Gerd Hoffmann29b00402010-03-11 11:13:27 -03003828 break;
Gerd Hoffmann715a6642009-10-14 10:39:28 +02003829 case QEMU_OPTION_writeconfig:
3830 {
3831 FILE *fp;
3832 if (strcmp(optarg, "-") == 0) {
3833 fp = stdout;
3834 } else {
3835 fp = fopen(optarg, "w");
3836 if (fp == NULL) {
3837 fprintf(stderr, "open %s: %s\n", optarg, strerror(errno));
3838 exit(1);
3839 }
3840 }
3841 qemu_config_write(fp);
3842 fclose(fp);
3843 break;
3844 }
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +02003845 case QEMU_OPTION_qtest:
3846 qtest_chrdev = optarg;
3847 break;
3848 case QEMU_OPTION_qtest_log:
3849 qtest_log = optarg;
3850 break;
Eduardo Otubo7d76ad42012-08-14 18:44:08 -03003851 case QEMU_OPTION_sandbox:
3852 opts = qemu_opts_parse(qemu_find_opts("sandbox"), optarg, 1);
3853 if (!opts) {
Markus Armbruster49295eb2013-02-08 21:22:19 +01003854 exit(1);
Eduardo Otubo7d76ad42012-08-14 18:44:08 -03003855 }
3856 break;
Corey Bryant587ed6b2012-10-18 15:19:34 -04003857 case QEMU_OPTION_add_fd:
3858#ifndef _WIN32
3859 opts = qemu_opts_parse(qemu_find_opts("add-fd"), optarg, 0);
3860 if (!opts) {
Markus Armbruster49295eb2013-02-08 21:22:19 +01003861 exit(1);
Corey Bryant587ed6b2012-10-18 15:19:34 -04003862 }
3863#else
3864 error_report("File descriptor passing is disabled on this "
3865 "platform");
3866 exit(1);
3867#endif
3868 break;
Anthony Liguori68d98d32012-06-25 14:36:33 -05003869 case QEMU_OPTION_object:
3870 opts = qemu_opts_parse(qemu_find_opts("object"), optarg, 1);
Markus Armbruster49295eb2013-02-08 21:22:19 +01003871 if (!opts) {
3872 exit(1);
3873 }
Anthony Liguori68d98d32012-06-25 14:36:33 -05003874 break;
Satoru Moriya888a6bc2013-04-19 16:42:06 +02003875 case QEMU_OPTION_realtime:
3876 opts = qemu_opts_parse(qemu_find_opts("realtime"), optarg, 0);
3877 if (!opts) {
3878 exit(1);
3879 }
3880 configure_realtime(opts);
3881 break;
Seiji Aguchi5e2ac512013-07-03 23:02:46 -04003882 case QEMU_OPTION_msg:
3883 opts = qemu_opts_parse(qemu_find_opts("msg"), optarg, 0);
3884 if (!opts) {
3885 exit(1);
3886 }
3887 configure_msg(opts);
3888 break;
Jes Sorensen59a52642010-06-10 11:42:25 +02003889 default:
3890 os_parse_cmd_args(popt->index, optarg);
bellardcd6f1162004-05-13 22:02:20 +00003891 }
bellard0824d6f2003-06-24 13:42:40 +00003892 }
3893 }
Markus Armbruster0f0bc3f2010-02-18 20:13:51 +01003894 loc_set_none();
bellard330d0412003-07-26 18:11:40 +00003895
Paolo Bonzini1c537862012-10-30 00:17:12 +01003896 if (qemu_init_main_loop()) {
3897 fprintf(stderr, "qemu_init_main_loop failed\n");
3898 exit(1);
3899 }
3900
Eduardo Otubo7d76ad42012-08-14 18:44:08 -03003901 if (qemu_opts_foreach(qemu_find_opts("sandbox"), parse_sandbox, NULL, 0)) {
3902 exit(1);
3903 }
3904
Corey Bryant587ed6b2012-10-18 15:19:34 -04003905#ifndef _WIN32
3906 if (qemu_opts_foreach(qemu_find_opts("add-fd"), parse_add_fd, NULL, 1)) {
3907 exit(1);
3908 }
3909
3910 if (qemu_opts_foreach(qemu_find_opts("add-fd"), cleanup_add_fd, NULL, 1)) {
3911 exit(1);
3912 }
3913#endif
3914
Marcel Apfelbaum0056ae22014-03-05 19:30:47 +02003915 if (machine_class == NULL) {
Dunrong Huangfb7c2692012-07-24 00:42:20 +08003916 fprintf(stderr, "No machine found.\n");
3917 exit(1);
3918 }
3919
Marcel Apfelbaum0056ae22014-03-05 19:30:47 +02003920 current_machine = MACHINE(object_new(object_class_get_name(
3921 OBJECT_CLASS(machine_class))));
3922 object_property_add_child(object_get_root(), "machine",
3923 OBJECT(current_machine), &error_abort);
3924
3925 machine = machine_class->qemu_machine;
Crístian Viana93bfef42012-05-30 00:35:51 -03003926 if (machine->hw_version) {
3927 qemu_set_version(machine->hw_version);
3928 }
3929
Anthony Liguori68d98d32012-06-25 14:36:33 -05003930 if (qemu_opts_foreach(qemu_find_opts("object"),
3931 object_create, NULL, 0) != 0) {
3932 exit(1);
3933 }
3934
Eduardo Habkostecf40be2012-03-09 16:19:07 -03003935 /* Init CPU def lists, based on config
3936 * - Must be called after all the qemu_read_config_file() calls
3937 * - Must be called before list_cpus()
3938 * - Must be called before machine->init()
3939 */
3940 cpudef_init();
3941
Peter Maydellc8057f92012-08-02 13:45:54 +01003942 if (cpu_model && is_help_option(cpu_model)) {
Eduardo Habkost1d6528a2012-03-21 09:33:40 -03003943 list_cpus(stdout, &fprintf, cpu_model);
Eduardo Habkostecf40be2012-03-09 16:19:07 -03003944 exit(0);
3945 }
3946
Matthew Fernandezc235d732011-06-07 16:32:40 +00003947 /* Open the logfile at this point, if necessary. We can't open the logfile
3948 * when encountering either of the logging options (-d or -D) because the
3949 * other one may be encountered later on the command line, changing the
3950 * location or level of logging.
3951 */
3952 if (log_mask) {
Peter Maydellb946bff2013-02-11 16:41:24 +00003953 int mask;
Matthew Fernandezc235d732011-06-07 16:32:40 +00003954 if (log_file) {
Peter Maydell9a7e5422013-02-11 16:41:20 +00003955 qemu_set_log_filename(log_file);
Matthew Fernandezc235d732011-06-07 16:32:40 +00003956 }
Peter Maydellb946bff2013-02-11 16:41:24 +00003957
3958 mask = qemu_str_to_log_mask(log_mask);
3959 if (!mask) {
3960 qemu_print_log_usage(stdout);
3961 exit(1);
3962 }
3963 qemu_set_log(mask);
Matthew Fernandezc235d732011-06-07 16:32:40 +00003964 }
3965
Michael Mueller8a745f22013-09-23 16:36:54 +02003966 if (!is_daemonized()) {
3967 if (!trace_backend_init(trace_events, trace_file)) {
3968 exit(1);
3969 }
Stefan Hajnoczi31d3c9b2011-03-13 20:14:30 +00003970 }
Stefan Hajnoczi0b5538c2011-02-26 18:38:39 +00003971
Paul Brook5cea8592009-05-30 00:52:44 +01003972 /* If no data_dir is specified then try to find it relative to the
3973 executable path. */
Gerd Hoffmann45240512013-03-08 11:42:24 +01003974 if (data_dir_idx < ARRAY_SIZE(data_dir)) {
Fam Zheng10f5bff2014-02-10 14:48:51 +08003975 data_dir[data_dir_idx] = os_find_datadir();
Gerd Hoffmann45240512013-03-08 11:42:24 +01003976 if (data_dir[data_dir_idx] != NULL) {
3977 data_dir_idx++;
3978 }
Paul Brook5cea8592009-05-30 00:52:44 +01003979 }
Stefan Weil60474fb2011-09-04 15:17:46 +02003980 /* If all else fails use the install path specified when building. */
Gerd Hoffmann45240512013-03-08 11:42:24 +01003981 if (data_dir_idx < ARRAY_SIZE(data_dir)) {
3982 data_dir[data_dir_idx++] = CONFIG_QEMU_DATADIR;
Paul Brook5cea8592009-05-30 00:52:44 +01003983 }
3984
Michael Tokarev12b7f572013-06-24 15:06:52 +04003985 smp_parse(qemu_opts_find(qemu_find_opts("smp-opts"), NULL));
Jes Sorensen6be68d72009-07-23 17:03:42 +02003986
balrog3d878ca2008-10-28 10:59:59 +00003987 machine->max_cpus = machine->max_cpus ?: 1; /* Default to UP */
aliguorib2097002008-10-07 20:39:39 +00003988 if (smp_cpus > machine->max_cpus) {
3989 fprintf(stderr, "Number of SMP cpus requested (%d), exceeds max cpus "
3990 "supported by machine `%s' (%d)\n", smp_cpus, machine->name,
3991 machine->max_cpus);
3992 exit(1);
3993 }
3994
Anthony PERARD67b724e2010-11-22 15:44:15 +00003995 /*
3996 * Get the default machine options from the machine if it is not already
3997 * specified either by the configuration file or by the command line.
3998 */
3999 if (machine->default_machine_opts) {
Jan Kiszka25de5932012-01-27 19:55:43 +01004000 qemu_opts_set_defaults(qemu_find_opts("machine"),
4001 machine->default_machine_opts, 0);
Anthony PERARD67b724e2010-11-22 15:44:15 +00004002 }
4003
Gerd Hoffmann3329f072010-08-20 13:52:01 +02004004 qemu_opts_foreach(qemu_find_opts("device"), default_driver_check, NULL, 0);
4005 qemu_opts_foreach(qemu_find_opts("global"), default_driver_check, NULL, 0);
Gerd Hoffmann998bbd72009-12-08 13:11:41 +01004006
Gerd Hoffmann986c5f72009-12-08 13:11:54 +01004007 if (machine->no_serial) {
4008 default_serial = 0;
4009 }
4010 if (machine->no_parallel) {
4011 default_parallel = 0;
4012 }
4013 if (!machine->use_virtcon) {
4014 default_virtcon = 0;
4015 }
Alexander Graf3ef669e2013-01-24 12:18:52 +01004016 if (!machine->use_sclp) {
4017 default_sclp = 0;
4018 }
Gerd Hoffmannac33f8f2009-12-16 14:25:39 +01004019 if (machine->no_floppy) {
4020 default_floppy = 0;
4021 }
4022 if (machine->no_cdrom) {
4023 default_cdrom = 0;
4024 }
4025 if (machine->no_sdcard) {
4026 default_sdcard = 0;
4027 }
Gerd Hoffmann986c5f72009-12-08 13:11:54 +01004028
Michael Tokarevab51b1d2012-12-30 12:48:14 +04004029 if (is_daemonized()) {
4030 /* According to documentation and historically, -nographic redirects
4031 * serial port, parallel port and monitor to stdio, which does not work
4032 * with -daemonize. We can redirect these to null instead, but since
4033 * -nographic is legacy, let's just error out.
4034 * We disallow -nographic only if all other ports are not redirected
4035 * explicitly, to not break existing legacy setups which uses
4036 * -nographic _and_ redirects all ports explicitly - this is valid
4037 * usage, -nographic is just a no-op in this case.
4038 */
4039 if (display_type == DT_NOGRAPHIC
4040 && (default_parallel || default_serial
4041 || default_monitor || default_virtcon)) {
4042 fprintf(stderr, "-nographic can not be used with -daemonize\n");
4043 exit(1);
4044 }
4045#ifdef CONFIG_CURSES
4046 if (display_type == DT_CURSES) {
4047 fprintf(stderr, "curses display can not be used with -daemonize\n");
4048 exit(1);
4049 }
4050#endif
4051 }
4052
Anthony Liguori993fbfd2009-05-21 16:54:00 -05004053 if (display_type == DT_NOGRAPHIC) {
Gerd Hoffmann6a5e8b02009-12-08 13:11:42 +01004054 if (default_parallel)
4055 add_device_config(DEV_PARALLEL, "null");
Gerd Hoffmanne1c09172009-12-08 13:11:44 +01004056 if (default_serial && default_monitor) {
4057 add_device_config(DEV_SERIAL, "mon:stdio");
Gerd Hoffmann986c5f72009-12-08 13:11:54 +01004058 } else if (default_virtcon && default_monitor) {
4059 add_device_config(DEV_VIRTCON, "mon:stdio");
Alexander Graf3ef669e2013-01-24 12:18:52 +01004060 } else if (default_sclp && default_monitor) {
4061 add_device_config(DEV_SCLP, "mon:stdio");
Gerd Hoffmanne1c09172009-12-08 13:11:44 +01004062 } else {
4063 if (default_serial)
4064 add_device_config(DEV_SERIAL, "stdio");
Gerd Hoffmann986c5f72009-12-08 13:11:54 +01004065 if (default_virtcon)
4066 add_device_config(DEV_VIRTCON, "stdio");
Alexander Graf3ef669e2013-01-24 12:18:52 +01004067 if (default_sclp) {
4068 add_device_config(DEV_SCLP, "stdio");
4069 }
Gerd Hoffmanne1c09172009-12-08 13:11:44 +01004070 if (default_monitor)
Gerd Hoffmann6ca55822009-12-08 13:11:52 +01004071 monitor_parse("stdio", "readline");
Gerd Hoffmanne1c09172009-12-08 13:11:44 +01004072 }
Gerd Hoffmann998bbd72009-12-08 13:11:41 +01004073 } else {
4074 if (default_serial)
4075 add_device_config(DEV_SERIAL, "vc:80Cx24C");
Gerd Hoffmann6a5e8b02009-12-08 13:11:42 +01004076 if (default_parallel)
4077 add_device_config(DEV_PARALLEL, "vc:80Cx24C");
Gerd Hoffmannabdeed02009-12-08 13:11:43 +01004078 if (default_monitor)
Gerd Hoffmann6ca55822009-12-08 13:11:52 +01004079 monitor_parse("vc:80Cx24C", "readline");
Alexander Graf38536da2009-12-17 13:06:08 +01004080 if (default_virtcon)
4081 add_device_config(DEV_VIRTCON, "vc:80Cx24C");
Alexander Graf3ef669e2013-01-24 12:18:52 +01004082 if (default_sclp) {
4083 add_device_config(DEV_SCLP, "vc:80Cx24C");
4084 }
aliguoribc0129d2008-08-01 15:12:34 +00004085 }
4086
Anthony Liguori15546422013-02-20 07:43:25 -06004087 if (display_type == DT_DEFAULT && !display_remote) {
4088#if defined(CONFIG_GTK)
4089 display_type = DT_GTK;
4090#elif defined(CONFIG_SDL) || defined(CONFIG_COCOA)
4091 display_type = DT_SDL;
4092#elif defined(CONFIG_VNC)
4093 vnc_display = "localhost:0,to=99";
4094 show_vnc_port = 1;
4095#else
4096 display_type = DT_NONE;
4097#endif
4098 }
4099
Peter Wu047d4e12013-06-11 10:55:08 +02004100 if ((no_frame || alt_grab || ctrl_grab) && display_type != DT_SDL) {
4101 fprintf(stderr, "-no-frame, -alt-grab and -ctrl-grab are only valid "
4102 "for SDL, ignoring option\n");
4103 }
4104 if (no_quit && (display_type != DT_GTK && display_type != DT_SDL)) {
4105 fprintf(stderr, "-no-quit is only valid for GTK and SDL, "
4106 "ignoring option\n");
4107 }
4108
Anthony Liguori15546422013-02-20 07:43:25 -06004109#if defined(CONFIG_GTK)
4110 if (display_type == DT_GTK) {
4111 early_gtk_display_init();
4112 }
4113#endif
4114
TeLeMana5829fd2010-04-15 12:37:55 +08004115 socket_init();
4116
Gerd Hoffmann3329f072010-08-20 13:52:01 +02004117 if (qemu_opts_foreach(qemu_find_opts("chardev"), chardev_init_func, NULL, 1) != 0)
Gerd Hoffmann1a688d32009-12-08 13:11:36 +01004118 exit(1);
Venkateswararao Jujjuri (JV)758e8e32010-06-14 13:34:41 -07004119#ifdef CONFIG_VIRTFS
Gerd Hoffmann3329f072010-08-20 13:52:01 +02004120 if (qemu_opts_foreach(qemu_find_opts("fsdev"), fsdev_init_func, NULL, 1) != 0) {
Gautham R Shenoy74db9202010-04-29 17:44:43 +05304121 exit(1);
4122 }
4123#endif
Gerd Hoffmann1a688d32009-12-08 13:11:36 +01004124
Jes Sorenseneb505be2010-06-10 11:42:28 +02004125 os_daemonize();
ths71e3ceb2006-12-22 02:11:31 +00004126
thsaa26bb22007-03-25 21:33:06 +00004127 if (pid_file && qemu_create_pidfile(pid_file) != 0) {
Jes Sorenseneb505be2010-06-10 11:42:28 +02004128 os_pidfile_error();
ths93815bc2007-03-19 15:58:31 +00004129 exit(1);
4130 }
4131
Jan Kiszka0ac543d2011-08-15 16:18:57 -07004132 /* init the memory */
4133 if (ram_size == 0) {
4134 ram_size = DEFAULT_RAM_SIZE * 1024 * 1024;
4135 }
4136
Bruce Rogers3d1d9652012-08-09 12:47:40 -06004137 if (qemu_opts_foreach(qemu_find_opts("device"), device_help_func, NULL, 0)
4138 != 0) {
4139 exit(0);
4140 }
4141
Aneesh Kumar K.V135a1292013-12-23 21:10:40 +05304142 configure_accelerator(machine);
Marcelo Tosatti214910a2009-09-18 02:41:23 -03004143
Paolo Bonzinid4fce242013-10-18 13:51:11 +02004144 if (qtest_chrdev) {
Fam Zheng23802b42014-02-10 09:28:02 +08004145 Error *local_err = NULL;
4146 qtest_init(qtest_chrdev, qtest_log, &local_err);
4147 if (local_err) {
4148 error_report("%s", error_get_pretty(local_err));
4149 error_free(local_err);
4150 exit(1);
4151 }
Edgar E. Iglesiasa907cf52013-04-15 21:28:17 +02004152 }
4153
Markus Armbruster2ff3de62013-07-04 15:09:22 +02004154 machine_opts = qemu_get_machine_opts();
4155 kernel_filename = qemu_opt_get(machine_opts, "kernel");
4156 initrd_filename = qemu_opt_get(machine_opts, "initrd");
4157 kernel_cmdline = qemu_opt_get(machine_opts, "append");
Gerd Hoffmann1442d3e2013-10-15 16:57:45 +02004158 bios_name = qemu_opt_get(machine_opts, "firmware");
Peter Maydell967c0da2012-02-22 22:40:00 +00004159
Markus Armbrustere3fdc532013-10-01 13:47:22 +02004160 boot_order = machine->default_boot_order;
Markus Armbruster8281abd2013-06-14 13:15:03 +02004161 opts = qemu_opts_find(qemu_find_opts("boot-opts"), NULL);
4162 if (opts) {
4163 char *normal_boot_order;
4164 const char *order, *once;
4165
4166 order = qemu_opt_get(opts, "order");
4167 if (order) {
4168 validate_bootdevices(order);
4169 boot_order = order;
4170 }
4171
4172 once = qemu_opt_get(opts, "once");
4173 if (once) {
4174 validate_bootdevices(once);
4175 normal_boot_order = g_strdup(boot_order);
4176 boot_order = once;
Markus Armbruster083b79c2013-06-14 13:15:04 +02004177 qemu_register_reset(restore_boot_order, normal_boot_order);
Markus Armbruster8281abd2013-06-14 13:15:03 +02004178 }
4179
4180 boot_menu = qemu_opt_get_bool(opts, "menu", boot_menu);
Amos Konge5187b52013-12-09 19:53:15 +08004181 boot_strict = qemu_opt_get_bool(opts, "strict", false);
Markus Armbruster8281abd2013-06-14 13:15:03 +02004182 }
4183
Peter Maydella0abe472012-02-08 05:41:39 +00004184 if (!kernel_cmdline) {
4185 kernel_cmdline = "";
4186 }
4187
bellarda20dd502003-09-30 21:07:02 +00004188 linux_boot = (kernel_filename != NULL);
balrog6c41b272007-11-17 12:12:29 +00004189
thsf8d39c02008-07-03 10:01:15 +00004190 if (!linux_boot && *kernel_cmdline != '\0') {
4191 fprintf(stderr, "-append only allowed with -kernel option\n");
4192 exit(1);
4193 }
4194
4195 if (!linux_boot && initrd_filename != NULL) {
4196 fprintf(stderr, "-initrd only allowed with -kernel option\n");
4197 exit(1);
4198 }
4199
Markus Armbruster2ff3de62013-07-04 15:09:22 +02004200 if (!linux_boot && qemu_opt_get(machine_opts, "dtb")) {
Grant Likely412beee2012-03-02 11:56:38 +00004201 fprintf(stderr, "-dtb only allowed with -kernel option\n");
4202 exit(1);
4203 }
4204
Jes Sorensen9156d762010-06-10 11:42:30 +02004205 os_set_line_buffering();
ths3b46e622007-09-17 08:09:54 +00004206
Paolo Bonzini49cf5722012-11-02 15:43:24 +01004207 qemu_init_cpu_loop();
4208 qemu_mutex_lock_iothread();
4209
Alon Levyad1be892012-03-14 20:33:37 +02004210#ifdef CONFIG_SPICE
4211 /* spice needs the timers to be initialized by this point */
4212 qemu_spice_init();
4213#endif
4214
Max Filippov0abe9052011-11-10 15:38:42 +04004215 if (icount_option && (kvm_enabled() || xen_enabled())) {
4216 fprintf(stderr, "-icount is not allowed with kvm or xen\n");
4217 exit(1);
4218 }
Paolo Bonzini4e3de9e2010-03-10 11:38:48 +01004219 configure_icount(icount_option);
pbrook634fce92006-07-15 17:40:09 +00004220
Amos Kongf30dbae2012-12-11 22:20:15 +08004221 /* clean up network at qemu process termination */
4222 atexit(&net_cleanup);
4223
Mark McLoughlindc1c9fe2009-10-06 12:17:16 +01004224 if (net_init_clients() < 0) {
4225 exit(1);
bellard702c6512004-04-02 21:21:32 +00004226 }
bellardf1510b22003-06-25 00:07:40 +00004227
Stefan Bergerd1a0cf72013-02-27 12:47:49 -05004228#ifdef CONFIG_TPM
4229 if (tpm_init() < 0) {
4230 exit(1);
4231 }
4232#endif
4233
balrogdc72ac12008-11-09 00:04:26 +00004234 /* init the bluetooth world */
Gerd Hoffmannbd3c9482009-07-15 13:59:26 +02004235 if (foreach_device_config(DEV_BT, bt_parse))
4236 exit(1);
balrogdc72ac12008-11-09 00:04:26 +00004237
Anthony PERARD834e76e2011-07-20 08:17:44 +00004238 if (!xen_enabled()) {
4239 /* On 32-bit hosts, QEMU is limited by virtual address space */
4240 if (ram_size > (2047 << 20) && HOST_LONG_BITS == 32) {
4241 fprintf(stderr, "qemu: at most 2047 MB RAM can be simulated\n");
4242 exit(1);
4243 }
4244 }
balrog7fb4fdc2008-04-24 17:59:27 +00004245
Jan Kiszkad5ab9712011-08-02 16:10:21 +02004246 cpu_exec_init_all();
bellard26a5f132008-05-28 12:30:31 +00004247
lirans@il.ibm.comc163b5c2009-11-02 15:40:58 +02004248 blk_mig_init();
4249
ths96d30e42007-01-07 20:42:14 +00004250 /* open the virtual block devices */
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +02004251 if (snapshot)
Gerd Hoffmann3329f072010-08-20 13:52:01 +02004252 qemu_opts_foreach(qemu_find_opts("drive"), drive_enable_snapshot, NULL, 0);
Christian Borntraeger2d0d2832012-11-20 15:30:34 +01004253 if (qemu_opts_foreach(qemu_find_opts("drive"), drive_init_func,
4254 &machine->block_default_type, 1) != 0) {
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +02004255 exit(1);
Christian Borntraeger2d0d2832012-11-20 15:30:34 +01004256 }
balrog3e3d5812007-04-30 02:09:25 +00004257
Christian Borntraeger3c42ea62012-11-22 21:02:55 +01004258 default_drive(default_cdrom, snapshot, machine->block_default_type, 2,
4259 CDROM_OPTS);
4260 default_drive(default_floppy, snapshot, IF_FLOPPY, 0, FD_OPTS);
4261 default_drive(default_sdcard, snapshot, IF_SD, 0, SD_OPTS);
Markus Armbruster4e5d9b52011-01-28 11:21:45 +01004262
Juan Quintela7908c782012-06-26 18:46:10 +02004263 register_savevm_live(NULL, "ram", 0, 4, &savevm_ram_handlers, NULL);
bellard8a7ddc32004-03-31 19:00:16 +00004264
aliguori268a3622009-04-21 22:30:27 +00004265 if (nb_numa_nodes > 0) {
4266 int i;
4267
Sasha Levinea0e5412011-06-29 23:29:39 -04004268 if (nb_numa_nodes > MAX_NODES) {
4269 nb_numa_nodes = MAX_NODES;
aliguori268a3622009-04-21 22:30:27 +00004270 }
4271
4272 /* If no memory size if given for any node, assume the default case
4273 * and distribute the available memory equally across all nodes
4274 */
4275 for (i = 0; i < nb_numa_nodes; i++) {
4276 if (node_mem[i] != 0)
4277 break;
4278 }
4279 if (i == nb_numa_nodes) {
4280 uint64_t usedmem = 0;
4281
4282 /* On Linux, the each node's border has to be 8MB aligned,
4283 * the final node gets the rest.
4284 */
4285 for (i = 0; i < nb_numa_nodes - 1; i++) {
4286 node_mem[i] = (ram_size / nb_numa_nodes) & ~((1 << 23UL) - 1);
4287 usedmem += node_mem[i];
4288 }
4289 node_mem[i] = ram_size - usedmem;
4290 }
4291
4292 for (i = 0; i < nb_numa_nodes; i++) {
Chegu Vinodee785fe2012-07-16 21:31:30 -07004293 if (!bitmap_empty(node_cpumask[i], MAX_CPUMASK_BITS)) {
aliguori268a3622009-04-21 22:30:27 +00004294 break;
Chegu Vinodee785fe2012-07-16 21:31:30 -07004295 }
aliguori268a3622009-04-21 22:30:27 +00004296 }
4297 /* assigning the VCPUs round-robin is easier to implement, guest OSes
4298 * must cope with this anyway, because there are BIOSes out there in
4299 * real machines which also use this scheme.
4300 */
4301 if (i == nb_numa_nodes) {
Vasilis Liaskovitis991dfef2011-10-26 14:19:00 +02004302 for (i = 0; i < max_cpus; i++) {
Chegu Vinodee785fe2012-07-16 21:31:30 -07004303 set_bit(i, node_cpumask[i % nb_numa_nodes]);
aliguori268a3622009-04-21 22:30:27 +00004304 }
4305 }
4306 }
4307
Gerd Hoffmann3329f072010-08-20 13:52:01 +02004308 if (qemu_opts_foreach(qemu_find_opts("mon"), mon_init_func, NULL, 1) != 0) {
Jan Kiszka157b9312010-04-06 16:55:53 +02004309 exit(1);
4310 }
4311
Gerd Hoffmann998bbd72009-12-08 13:11:41 +01004312 if (foreach_device_config(DEV_SERIAL, serial_parse) < 0)
4313 exit(1);
Gerd Hoffmann6a5e8b02009-12-08 13:11:42 +01004314 if (foreach_device_config(DEV_PARALLEL, parallel_parse) < 0)
4315 exit(1);
Gerd Hoffmannaee1b932009-12-08 13:11:53 +01004316 if (foreach_device_config(DEV_VIRTCON, virtcon_parse) < 0)
4317 exit(1);
Alexander Graf3ef669e2013-01-24 12:18:52 +01004318 if (foreach_device_config(DEV_SCLP, sclp_parse) < 0) {
4319 exit(1);
4320 }
H. Peter Anvinc9f398e2009-12-29 13:51:36 -08004321 if (foreach_device_config(DEV_DEBUGCON, debugcon_parse) < 0)
4322 exit(1);
aliguori2796dae2009-01-16 20:23:27 +00004323
Paolo Bonzini7f1b17f2012-05-10 09:39:17 +02004324 /* If no default VGA is requested, the default is "none". */
Aurelien Jarno3605ded2012-09-10 01:01:44 +02004325 if (default_vga) {
4326 if (cirrus_vga_available()) {
4327 vga_model = "cirrus";
4328 } else if (vga_available()) {
4329 vga_model = "std";
4330 }
Blue Swirla369da52011-09-27 19:15:42 +00004331 }
Paolo Bonzini7f1b17f2012-05-10 09:39:17 +02004332 select_vgahw(vga_model);
Blue Swirla369da52011-09-27 19:15:42 +00004333
Markus Armbruster09aaa162009-08-21 10:31:34 +02004334 if (watchdog) {
4335 i = select_watchdog(watchdog);
4336 if (i > 0)
4337 exit (i == 1 ? 1 : 0);
4338 }
4339
Gerd Hoffmannb6b61142009-07-15 13:48:21 +02004340 if (machine->compat_props) {
Gerd Hoffmann458fb672009-12-08 13:11:33 +01004341 qdev_prop_register_global_list(machine->compat_props);
Gerd Hoffmannb6b61142009-07-15 13:48:21 +02004342 }
Gerd Hoffmannd0fef6f2009-12-08 13:11:34 +01004343 qemu_add_globals();
4344
Anthony Liguori1de81d22011-12-19 16:37:46 -06004345 qdev_machine_init();
4346
Markus Armbrustere689f7c2013-10-30 13:56:39 +01004347 QEMUMachineInitArgs args = { .machine = machine,
4348 .ram_size = ram_size,
Markus Armbrusterc1654732013-08-16 13:13:50 +02004349 .boot_order = boot_order,
Eduardo Habkost5f072e12012-10-15 17:22:02 -03004350 .kernel_filename = kernel_filename,
4351 .kernel_cmdline = kernel_cmdline,
4352 .initrd_filename = initrd_filename,
4353 .cpu_model = cpu_model };
Marcel Apfelbaum0056ae22014-03-05 19:30:47 +02004354
4355 current_machine->init_args = args;
4356 machine->init(&current_machine->init_args);
aliguori3023f332009-01-16 19:04:14 +00004357
Paolo Bonzinib3e6d592013-04-18 18:44:04 +02004358 audio_init();
4359
Jan Kiszkaea375f92010-03-01 19:10:30 +01004360 cpu_synchronize_all_post_init();
aliguori268a3622009-04-21 22:30:27 +00004361
Blue Swirl87d0a282010-03-27 18:24:45 +00004362 set_numa_modes();
aliguori268a3622009-04-21 22:30:27 +00004363
aliguori3023f332009-01-16 19:04:14 +00004364 /* init USB devices */
zhlcindy@gmail.com094b2872012-09-02 19:25:28 +00004365 if (usb_enabled(false)) {
Markus Armbruster07527062009-10-06 12:16:57 +01004366 if (foreach_device_config(DEV_USB, usb_parse) < 0)
4367 exit(1);
aliguori3023f332009-01-16 19:04:14 +00004368 }
4369
Gerd Hoffmannbd3c9482009-07-15 13:59:26 +02004370 /* init generic devices */
Gerd Hoffmann3329f072010-08-20 13:52:01 +02004371 if (qemu_opts_foreach(qemu_find_opts("device"), device_init_func, NULL, 1) != 0)
Gerd Hoffmannbd3c9482009-07-15 13:59:26 +02004372 exit(1);
4373
Markus Armbruster668680f2010-02-11 14:44:58 +01004374 net_check_clients();
4375
Gerd Hoffmann64840c62013-03-07 17:08:29 +01004376 ds = init_displaystate();
Anthony Liguori993fbfd2009-05-21 16:54:00 -05004377
Gerd Hoffmann6b62dc22010-08-25 10:51:06 +02004378 /* init local displays */
Anthony Liguori993fbfd2009-05-21 16:54:00 -05004379 switch (display_type) {
4380 case DT_NOGRAPHIC:
Antony Pavlov8111d022013-10-29 08:36:31 +04004381 (void)ds; /* avoid warning if no display is configured */
Anthony Liguori993fbfd2009-05-21 16:54:00 -05004382 break;
4383#if defined(CONFIG_CURSES)
4384 case DT_CURSES:
Michael Tokarevab51b1d2012-12-30 12:48:14 +04004385 curses_display_init(ds, full_screen);
Anthony Liguori993fbfd2009-05-21 16:54:00 -05004386 break;
4387#endif
bellard5b0753e2005-03-01 21:37:28 +00004388#if defined(CONFIG_SDL)
Anthony Liguori993fbfd2009-05-21 16:54:00 -05004389 case DT_SDL:
4390 sdl_display_init(ds, full_screen, no_frame);
4391 break;
bellard5b0753e2005-03-01 21:37:28 +00004392#elif defined(CONFIG_COCOA)
Anthony Liguori993fbfd2009-05-21 16:54:00 -05004393 case DT_SDL:
4394 cocoa_display_init(ds, full_screen);
4395 break;
bellard313aa562003-08-10 21:52:11 +00004396#endif
Anthony Liguori15546422013-02-20 07:43:25 -06004397#if defined(CONFIG_GTK)
4398 case DT_GTK:
Peter Wu787ba4f2013-06-10 20:04:43 +02004399 gtk_display_init(ds, full_screen);
Anthony Liguori15546422013-02-20 07:43:25 -06004400 break;
4401#endif
Gerd Hoffmann6b62dc22010-08-25 10:51:06 +02004402 default:
4403 break;
4404 }
4405
Gleb Natapov0ce235a2011-03-31 11:27:23 +02004406 /* must be after terminal init, SDL library changes signal handlers */
4407 os_setup_signal_handling();
4408
Jes Sorensen821601e2011-03-16 13:33:36 +01004409#ifdef CONFIG_VNC
Gerd Hoffmann6b62dc22010-08-25 10:51:06 +02004410 /* init remote displays */
4411 if (vnc_display) {
Paolo Bonzini2d55f0e2012-10-02 10:17:21 +02004412 Error *local_err = NULL;
Anthony Liguori993fbfd2009-05-21 16:54:00 -05004413 vnc_display_init(ds);
Paolo Bonzini2d55f0e2012-10-02 10:17:21 +02004414 vnc_display_open(ds, vnc_display, &local_err);
4415 if (local_err != NULL) {
Seiji Aguchi4a44d852013-08-05 15:40:44 -04004416 error_report("Failed to start VNC server on `%s': %s",
4417 vnc_display, error_get_pretty(local_err));
Paolo Bonzini2d55f0e2012-10-02 10:17:21 +02004418 error_free(local_err);
Anthony Liguori993fbfd2009-05-21 16:54:00 -05004419 exit(1);
Amos Kong94b204c2012-06-30 10:02:20 +08004420 }
Anthony Liguorif92f8af2009-05-20 13:01:02 -05004421
Anthony Liguori993fbfd2009-05-21 16:54:00 -05004422 if (show_vnc_port) {
4423 printf("VNC server running on `%s'\n", vnc_display_local_addr(ds));
Anthony Liguorif92f8af2009-05-20 13:01:02 -05004424 }
bellard313aa562003-08-10 21:52:11 +00004425 }
Jes Sorensen821601e2011-03-16 13:33:36 +01004426#endif
Gerd Hoffmanna3e22262010-08-25 15:32:06 +02004427#ifdef CONFIG_SPICE
Gerd Hoffmann9fa03282013-10-11 22:39:59 +02004428 if (using_spice) {
4429 qemu_spice_display_init();
Gerd Hoffmanna3e22262010-08-25 15:32:06 +02004430 }
4431#endif
aliguori5b08fc12008-08-21 20:08:03 +00004432
Markus Armbrusteref0c4a02012-02-07 15:09:13 +01004433 if (foreach_device_config(DEV_GDB, gdbserver_start) < 0) {
aliguori59030a82009-04-05 18:43:41 +00004434 exit(1);
balrog45669e02007-07-02 13:20:17 +00004435 }
balrog45669e02007-07-02 13:20:17 +00004436
Gerd Hoffmann3418bd22009-09-25 21:42:41 +02004437 qdev_machine_creation_done();
4438
Gerd Hoffmann15ff7702009-12-14 16:07:35 +01004439 if (rom_load_all() != 0) {
4440 fprintf(stderr, "rom loading failed\n");
4441 exit(1);
4442 }
Gerd Hoffmann45a50b12009-10-01 16:42:33 +02004443
Isaku Yamahata80376c32010-12-20 14:33:35 +09004444 /* TODO: once all bus devices are qdevified, this should be done
4445 * when bus is created by qdev.c */
4446 qemu_register_reset(qbus_reset_all_fn, sysbus_get_default());
Gleb Natapov4cab9462010-12-08 13:35:08 +02004447 qemu_run_machine_init_done_notifiers();
4448
Michael S. Tsirkind916b462013-07-24 18:56:08 +03004449 /* Done notifiers can load ROMs */
4450 rom_load_done();
4451
Jan Kiszkae063eb12011-06-14 18:29:43 +02004452 qemu_system_reset(VMRESET_SILENT);
Juan Quintela05f24012009-08-20 19:42:22 +02004453 if (loadvm) {
Markus Armbruster03cd4652010-02-17 16:24:10 +01004454 if (load_vmstate(loadvm) < 0) {
Juan Quintela05f24012009-08-20 19:42:22 +02004455 autostart = 0;
4456 }
4457 }
bellardd63d3072004-10-03 13:29:03 +00004458
Glauber Costa2bb8c102009-07-24 16:20:23 -04004459 if (incoming) {
Paolo Bonzini43eaae22012-10-02 18:21:18 +02004460 Error *local_err = NULL;
4461 qemu_start_incoming_migration(incoming, &local_err);
4462 if (local_err) {
Seiji Aguchi4a44d852013-08-05 15:40:44 -04004463 error_report("-incoming %s: %s", incoming,
4464 error_get_pretty(local_err));
Paolo Bonzini43eaae22012-10-02 18:21:18 +02004465 error_free(local_err);
4466 exit(1);
Juan Quintela8ca5e802010-06-09 14:10:54 +02004467 }
Avi Kivity6b99dad2009-08-09 14:39:20 +03004468 } else if (autostart) {
aliguoric0f4ce72009-03-05 23:01:01 +00004469 vm_start();
Avi Kivity6b99dad2009-08-09 14:39:20 +03004470 }
thsffd843b2006-12-21 19:46:43 +00004471
Jes Sorenseneb505be2010-06-10 11:42:28 +02004472 os_setup_post();
ths71e3ceb2006-12-22 02:11:31 +00004473
Michael Mueller8a745f22013-09-23 16:36:54 +02004474 if (is_daemonized()) {
4475 if (!trace_backend_init(trace_events, trace_file)) {
4476 exit(1);
4477 }
4478 }
4479
bellard8a7ddc32004-03-31 19:00:16 +00004480 main_loop();
Paolo Bonzini99435902011-09-12 14:03:13 +02004481 bdrv_close_all();
4482 pause_all_vcpus();
wayne3d3b8302011-07-27 18:04:55 +08004483 res_free();
Stefan Bergerd1a0cf72013-02-27 12:47:49 -05004484#ifdef CONFIG_TPM
4485 tpm_cleanup();
4486#endif
thsb46a8902007-10-21 23:20:45 +00004487
bellard0824d6f2003-06-24 13:42:40 +00004488 return 0;
4489}