blob: 7643f163512bb5cf22a36797e9eeef5ba77eff0f [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>
bellardc88676f2006-08-06 13:36:11 +000030#include <zlib.h>
Paolo Bonzini1de7afc2012-12-17 18:20:00 +010031#include "qemu/bitmap.h"
bellard67b915a2004-03-31 23:37:16 +000032
Juan Quintela71e72a12009-07-27 16:12:56 +020033/* Needed early for CONFIG_BSD etc. */
blueswir1d40cdb12009-03-07 16:52:02 +000034#include "config-host.h"
35
bellard67b915a2004-03-31 23:37:16 +000036#ifndef _WIN32
Paul Brook5cea8592009-05-30 00:52:44 +010037#include <libgen.h>
bellard67b915a2004-03-31 23:37:16 +000038#include <sys/times.h>
bellardf1510b22003-06-25 00:07:40 +000039#include <sys/wait.h>
bellard67b915a2004-03-31 23:37:16 +000040#include <termios.h>
bellard67b915a2004-03-31 23:37:16 +000041#include <sys/mman.h>
bellardf1510b22003-06-25 00:07:40 +000042#include <sys/ioctl.h>
blueswir124646c72008-11-07 16:55:48 +000043#include <sys/resource.h>
bellardf1510b22003-06-25 00:07:40 +000044#include <sys/socket.h>
bellardc94c8d62004-09-13 21:37:34 +000045#include <netinet/in.h>
blueswir124646c72008-11-07 16:55:48 +000046#include <net/if.h>
blueswir124646c72008-11-07 16:55:48 +000047#include <arpa/inet.h>
bellard9d728e82004-09-05 23:09:03 +000048#include <dirent.h>
bellard7c9d8e02005-11-15 22:16:05 +000049#include <netdb.h>
thscb4b9762007-09-13 12:39:35 +000050#include <sys/select.h>
Prerna Saxenaab6540d2010-08-09 11:48:32 +010051
Juan Quintela71e72a12009-07-27 16:12:56 +020052#ifdef CONFIG_BSD
bellard7d3505c2004-05-12 19:32:15 +000053#include <sys/stat.h>
Aurelien Jarnoa167ba52009-11-29 18:00:41 +010054#if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__DragonFly__)
Juergen Lock92c0e652010-03-25 22:07:12 +010055#include <sys/sysctl.h>
blueswir124646c72008-11-07 16:55:48 +000056#else
57#include <util.h>
blueswir1128ab2f2008-08-15 18:33:42 +000058#endif
Aurelien Jarnobbe813a2009-11-30 15:42:59 +010059#else
blueswir1223f0d72008-09-30 18:12:18 +000060#ifdef __linux__
bellard7d3505c2004-05-12 19:32:15 +000061#include <malloc.h>
thsbd494f42007-09-16 20:03:23 +000062
bellarde57a8c02005-11-10 23:58:52 +000063#include <linux/ppdev.h>
ths5867c882007-02-17 23:44:43 +000064#include <linux/parport.h>
blueswir1223f0d72008-09-30 18:12:18 +000065#endif
Eduardo Otubo452dfbe2012-08-14 18:44:07 -030066
67#ifdef CONFIG_SECCOMP
Paolo Bonzini9c17d612012-12-17 18:20:04 +010068#include "sysemu/seccomp.h"
Eduardo Otubo452dfbe2012-08-14 18:44:07 -030069#endif
70
blueswir1223f0d72008-09-30 18:12:18 +000071#ifdef __sun__
thsd5d10bc2007-02-17 22:54:49 +000072#include <sys/stat.h>
73#include <sys/ethernet.h>
74#include <sys/sockio.h>
thsd5d10bc2007-02-17 22:54:49 +000075#include <netinet/arp.h>
thsd5d10bc2007-02-17 22:54:49 +000076#include <netinet/in_systm.h>
77#include <netinet/ip.h>
78#include <netinet/ip_icmp.h> // must come after ip.h
79#include <netinet/udp.h>
80#include <netinet/tcp.h>
81#include <net/if.h>
82#include <syslog.h>
83#include <stropts.h>
bellard67b915a2004-03-31 23:37:16 +000084#endif
bellard7d3505c2004-05-12 19:32:15 +000085#endif
bellardec530c82006-04-25 22:36:06 +000086#endif
bellard67b915a2004-03-31 23:37:16 +000087
ths8a16d272008-07-19 09:56:24 +000088#if defined(CONFIG_VDE)
89#include <libvdeplug.h>
90#endif
91
bellard67b915a2004-03-31 23:37:16 +000092#ifdef _WIN32
aliguori49dc7682009-03-08 16:26:59 +000093#include <windows.h>
bellard67b915a2004-03-31 23:37:16 +000094#endif
95
bellard73332e52004-04-04 20:22:28 +000096#ifdef CONFIG_SDL
Stefan Weil59a36a22009-06-18 20:11:03 +020097#if defined(__APPLE__) || defined(main)
Stefan Weil66936652009-06-13 13:19:11 +020098#include <SDL.h>
malc880fec52009-02-15 20:18:41 +000099int qemu_main(int argc, char **argv, char **envp);
100int main(int argc, char **argv)
101{
Stefan Weil59a36a22009-06-18 20:11:03 +0200102 return qemu_main(argc, argv, NULL);
malc880fec52009-02-15 20:18:41 +0000103}
104#undef main
105#define main qemu_main
bellard96bcd4f2004-07-10 16:26:15 +0000106#endif
bellard73332e52004-04-04 20:22:28 +0000107#endif /* CONFIG_SDL */
bellard0824d6f2003-06-24 13:42:40 +0000108
bellard5b0753e2005-03-01 21:37:28 +0000109#ifdef CONFIG_COCOA
110#undef main
111#define main qemu_main
112#endif /* CONFIG_COCOA */
113
Anthony Liguori69e5bb62011-08-22 08:12:52 -0500114#include <glib.h>
115
blueswir1511d2b12009-03-07 15:32:56 +0000116#include "hw/hw.h"
117#include "hw/boards.h"
118#include "hw/usb.h"
119#include "hw/pcmcia.h"
120#include "hw/pc.h"
blueswir1511d2b12009-03-07 15:32:56 +0000121#include "hw/isa.h"
blueswir1511d2b12009-03-07 15:32:56 +0000122#include "hw/bt.h"
Richard W.M. Jones9dd986c2009-04-25 13:56:19 +0100123#include "hw/watchdog.h"
aliguorib6f6e3d2009-04-17 18:59:56 +0000124#include "hw/smbios.h"
aliguorie37630c2009-04-22 15:19:10 +0000125#include "hw/xen.h"
Gerd Hoffmannbd3c9482009-07-15 13:59:26 +0200126#include "hw/qdev.h"
Gerd Hoffmann45a50b12009-10-01 16:42:33 +0200127#include "hw/loader.h"
Paolo Bonzinib4a42f82013-02-04 11:37:52 +0100128#include "monitor/qdev.h"
Paolo Bonzini927d4872012-12-17 18:20:05 +0100129#include "bt/bt.h"
Paolo Bonzini1422e322012-10-24 08:43:34 +0200130#include "net/net.h"
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000131#include "net/slirp.h"
Paolo Bonzini83c90892012-12-17 18:19:49 +0100132#include "monitor/monitor.h"
Paolo Bonzini28ecbae2012-11-28 12:06:30 +0100133#include "ui/console.h"
Paolo Bonzini9c17d612012-12-17 18:20:04 +0100134#include "sysemu/sysemu.h"
Paolo Bonzini022c62c2012-12-17 18:19:49 +0100135#include "exec/gdbstub.h"
Paolo Bonzini1de7afc2012-12-17 18:20:00 +0100136#include "qemu/timer.h"
Paolo Bonzini927d4872012-12-17 18:20:05 +0100137#include "char/char.h"
Paolo Bonzini1de7afc2012-12-17 18:20:00 +0100138#include "qemu/cache-utils.h"
Paolo Bonzini9c17d612012-12-17 18:20:04 +0100139#include "sysemu/blockdev.h"
Markus Armbruster2b584952012-07-10 11:12:50 +0200140#include "hw/block-common.h"
Paolo Bonzinicaf71f82012-12-17 18:19:50 +0100141#include "migration/block.h"
Stefan Bergerd1a0cf72013-02-27 12:47:49 -0500142#include "tpm/tpm.h"
Paolo Bonzini9c17d612012-12-17 18:20:04 +0100143#include "sysemu/dma.h"
blueswir1511d2b12009-03-07 15:32:56 +0000144#include "audio/audio.h"
Paolo Bonzinicaf71f82012-12-17 18:19:50 +0100145#include "migration/migration.h"
Paolo Bonzini9c17d612012-12-17 18:20:04 +0100146#include "sysemu/kvm.h"
Paolo Bonzini7b1b5d12012-12-17 18:19:43 +0100147#include "qapi/qmp/qjson.h"
Paolo Bonzini1de7afc2012-12-17 18:20:00 +0100148#include "qemu/option.h"
149#include "qemu/config-file.h"
Jes Sorensen59a52642010-06-10 11:42:25 +0200150#include "qemu-options.h"
Luiz Capitulino1fa9a5e2011-09-12 17:54:20 -0300151#include "qmp-commands.h"
Paolo Bonzini1de7afc2012-12-17 18:20:00 +0100152#include "qemu/main-loop.h"
Venkateswararao Jujjuri (JV)758e8e32010-06-14 13:34:41 -0700153#ifdef CONFIG_VIRTFS
Gautham R Shenoy74db9202010-04-29 17:44:43 +0530154#include "fsdev/qemu-fsdev.h"
155#endif
Paolo Bonzini9c17d612012-12-17 18:20:04 +0100156#include "sysemu/qtest.h"
blueswir1511d2b12009-03-07 15:32:56 +0000157
Paolo Bonzini76cad712012-10-24 11:12:21 +0200158#include "disas/disas.h"
bellardfc01f7e2003-06-30 10:03:06 +0000159
Paolo Bonzini1de7afc2012-12-17 18:20:00 +0100160#include "qemu/sockets.h"
blueswir1511d2b12009-03-07 15:32:56 +0000161
Jan Kiszkad918f232009-06-24 14:42:30 +0200162#include "slirp/libslirp.h"
blueswir1511d2b12009-03-07 15:32:56 +0000163
Stefan Hajnoczi94b0b5f2010-11-16 12:20:25 +0000164#include "trace.h"
Lluíse4858972011-08-31 20:31:03 +0200165#include "trace/control.h"
Paolo Bonzini1de7afc2012-12-17 18:20:00 +0100166#include "qemu/queue.h"
Paolo Bonzini9c17d612012-12-17 18:20:04 +0100167#include "sysemu/cpus.h"
168#include "sysemu/arch_init.h"
Paolo Bonzini1de7afc2012-12-17 18:20:00 +0100169#include "qemu/osdep.h"
Blue Swirl72cf2d42009-09-12 07:36:22 +0000170
Gerd Hoffmann29b00402010-03-11 11:13:27 -0300171#include "ui/qemu-spice.h"
Anthony Liguori68d98d32012-06-25 14:36:33 -0500172#include "qapi/string-input-visitor.h"
Gerd Hoffmann29b00402010-03-11 11:13:27 -0300173
blueswir19dc63a12008-10-04 07:25:46 +0000174//#define DEBUG_NET
175//#define DEBUG_SLIRP
bellard330d0412003-07-26 18:11:40 +0000176
bellard1bfe8562004-07-08 21:17:50 +0000177#define DEFAULT_RAM_SIZE 128
bellard313aa562003-08-10 21:52:11 +0000178
Amit Shah98b19252010-01-20 00:36:52 +0530179#define MAX_VIRTIO_CONSOLES 1
Alexander Graf3ef669e2013-01-24 12:18:52 +0100180#define MAX_SCLP_CONSOLES 1
Amit Shah98b19252010-01-20 00:36:52 +0530181
Gerd Hoffmann45240512013-03-08 11:42:24 +0100182static const char *data_dir[16];
183static int data_dir_idx;
j_mayer1192dad2007-10-05 13:08:35 +0000184const char *bios_name = NULL;
malccb5a7aa2008-09-28 00:42:12 +0000185enum vga_retrace_method vga_retrace_method = VGA_RETRACE_DUMB;
Anthony Liguori993fbfd2009-05-21 16:54:00 -0500186DisplayType display_type = DT_DEFAULT;
Blue Swirl4fdcac02012-10-28 11:04:47 +0000187static int display_remote;
bellard3d11d0e2004-12-12 16:56:30 +0000188const char* keyboard_layout = NULL;
Anthony Liguoric227f092009-10-01 16:12:16 -0500189ram_addr_t ram_size;
Marcelo Tosattic9027602010-03-01 20:25:08 -0300190const char *mem_path = NULL;
191#ifdef MAP_POPULATE
192int mem_prealloc = 0; /* force preallocation of physical target memory */
193#endif
bellardc4b1fcc2004-03-14 21:44:30 +0000194int nb_nics;
bellard7c9d8e02005-11-15 22:16:05 +0000195NICInfo nd_table[MAX_NICS];
Paolo Bonzinid399f672009-07-27 23:17:51 +0200196int autostart;
balrogf6503052008-02-17 11:42:19 +0000197static int rtc_utc = 1;
198static int rtc_date_offset = -1; /* -1 means no change */
Jan Kiszka68752042009-09-15 13:36:04 +0200199QEMUClock *rtc_clock;
Gerd Hoffmann64465292009-12-08 13:11:45 +0100200int vga_interface_type = VGA_NONE;
blueswir1dbed7e42008-10-01 19:38:09 +0000201static int full_screen = 0;
blueswir1634a21f2008-11-16 11:34:07 +0000202#ifdef CONFIG_SDL
blueswir1dbed7e42008-10-01 19:38:09 +0000203static int no_frame = 0;
blueswir1634a21f2008-11-16 11:34:07 +0000204#endif
ths667acca2006-12-11 02:08:05 +0000205int no_quit = 0;
bellard8d11df92004-08-24 21:13:40 +0000206CharDriverState *serial_hds[MAX_SERIAL_PORTS];
bellard6508fe52005-01-15 12:02:56 +0000207CharDriverState *parallel_hds[MAX_PARALLEL_PORTS];
aliguori9ede2fd2009-01-15 20:05:25 +0000208CharDriverState *virtcon_hds[MAX_VIRTIO_CONSOLES];
Alexander Graf3ef669e2013-01-24 12:18:52 +0100209CharDriverState *sclp_hds[MAX_SCLP_CONSOLES];
bellarda09db212005-04-30 16:10:35 +0000210int win2k_install_hack = 0;
aurel321b530a62009-04-05 20:08:59 +0000211int singlestep = 0;
bellard6a00d602005-11-21 23:25:50 +0000212int smp_cpus = 1;
Jes Sorensen6be68d72009-07-23 17:03:42 +0200213int max_cpus = 0;
Andre Przywaradc6b1c02009-08-19 15:42:40 +0200214int smp_cores = 1;
215int smp_threads = 1;
Jes Sorensen821601e2011-03-16 13:33:36 +0100216#ifdef CONFIG_VNC
ths73fc9742006-12-22 02:09:07 +0000217const char *vnc_display;
Jes Sorensen821601e2011-03-16 13:33:36 +0100218#endif
bellard6515b202006-05-03 22:02:44 +0000219int acpi_enabled = 1;
aliguori16b29ae2008-12-17 23:28:44 +0000220int no_hpet = 0;
bellard52ca8d62006-06-14 16:03:05 +0000221int fd_bootchk = 1;
Blue Swirl4fdcac02012-10-28 11:04:47 +0000222static int no_reboot;
aurel32b2f76162008-04-11 21:35:52 +0000223int no_shutdown = 0;
balrog9467cd42007-05-01 01:34:14 +0000224int cursor_hide = 1;
balroga171fe32007-04-30 01:48:07 +0000225int graphic_rotate = 0;
Markus Armbruster09aaa162009-08-21 10:31:34 +0200226const char *watchdog;
Gleb Natapov2e55e842010-12-08 13:35:07 +0200227QEMUOptionRom option_rom[MAX_OPTION_ROMS];
ths9ae02552007-01-05 17:39:04 +0000228int nb_option_roms;
pbrook8e716212007-01-20 17:12:09 +0000229int semihosting_enabled = 0;
balrog2b8f2d42007-07-27 22:08:46 +0000230int old_param = 0;
thsc35734b2007-03-19 15:17:08 +0000231const char *qemu_name;
ths3780e192007-06-21 21:08:02 +0000232int alt_grab = 0;
Dustin Kirkland0ca9f8a2009-09-17 15:48:04 -0500233int ctrl_grab = 0;
blueswir166508602007-05-01 14:16:52 +0000234unsigned int nb_prom_envs = 0;
235const char *prom_envs[MAX_PROM_ENVS];
Jan Kiszka95387492009-07-02 00:19:02 +0200236int boot_menu;
Amos Kongc8a6ae82013-03-19 14:23:27 +0800237bool boot_strict;
wayne3d3b8302011-07-27 18:04:55 +0800238uint8_t *boot_splash_filedata;
Markus Armbrusterd09acb92013-01-23 18:25:08 +0100239size_t boot_splash_filedata_size;
wayne3d3b8302011-07-27 18:04:55 +0800240uint8_t qemu_extra_params_fw[2];
bellard0824d6f2003-06-24 13:42:40 +0000241
Gleb Natapov1ca4d092010-12-08 13:35:05 +0200242typedef struct FWBootEntry FWBootEntry;
243
244struct FWBootEntry {
245 QTAILQ_ENTRY(FWBootEntry) link;
246 int32_t bootindex;
247 DeviceState *dev;
248 char *suffix;
249};
250
Blue Swirl4fdcac02012-10-28 11:04:47 +0000251static QTAILQ_HEAD(, FWBootEntry) fw_boot_order =
252 QTAILQ_HEAD_INITIALIZER(fw_boot_order);
Gleb Natapov1ca4d092010-12-08 13:35:05 +0200253
aliguori268a3622009-04-21 22:30:27 +0000254int nb_numa_nodes;
255uint64_t node_mem[MAX_NODES];
Chegu Vinodee785fe2012-07-16 21:31:30 -0700256unsigned long *node_cpumask[MAX_NODES];
aliguori268a3622009-04-21 22:30:27 +0000257
blueswir18fcb1b92008-09-18 18:29:08 +0000258uint8_t qemu_uuid[16];
259
Jan Kiszka76e30d02009-07-02 00:19:02 +0200260static QEMUBootSetHandler *boot_set_handler;
261static void *boot_set_opaque;
262
Gerd Hoffmannfd42dee2010-06-04 14:08:07 +0200263static NotifierList exit_notifiers =
264 NOTIFIER_LIST_INITIALIZER(exit_notifiers);
265
Gleb Natapov4cab9462010-12-08 13:35:08 +0200266static NotifierList machine_init_done_notifiers =
267 NOTIFIER_LIST_INITIALIZER(machine_init_done_notifiers);
268
liguangd5286af2013-01-24 13:03:27 +0800269static bool tcg_allowed = true;
270bool kvm_allowed;
271bool xen_allowed;
Blue Swirld745bef2010-03-29 19:23:49 +0000272uint32_t xen_domid;
273enum xen_mode xen_mode = XEN_EMULATE;
Jan Kiszkad5ab9712011-08-02 16:10:21 +0200274static int tcg_tb_size;
Blue Swirld745bef2010-03-29 19:23:49 +0000275
Gerd Hoffmann998bbd72009-12-08 13:11:41 +0100276static int default_serial = 1;
Gerd Hoffmann6a5e8b02009-12-08 13:11:42 +0100277static int default_parallel = 1;
Gerd Hoffmann986c5f72009-12-08 13:11:54 +0100278static int default_virtcon = 1;
Alexander Graf3ef669e2013-01-24 12:18:52 +0100279static int default_sclp = 1;
Gerd Hoffmannabdeed02009-12-08 13:11:43 +0100280static int default_monitor = 1;
Gerd Hoffmannac33f8f2009-12-16 14:25:39 +0100281static int default_floppy = 1;
282static int default_cdrom = 1;
283static int default_sdcard = 1;
Paolo Bonzini7f1b17f2012-05-10 09:39:17 +0200284static int default_vga = 1;
Gerd Hoffmann998bbd72009-12-08 13:11:41 +0100285
286static struct {
287 const char *driver;
288 int *flag;
289} default_list[] = {
Gerd Hoffmann6a5e8b02009-12-08 13:11:42 +0100290 { .driver = "isa-serial", .flag = &default_serial },
291 { .driver = "isa-parallel", .flag = &default_parallel },
Gerd Hoffmannd8bcbab2009-12-16 14:25:40 +0100292 { .driver = "isa-fdc", .flag = &default_floppy },
Markus Armbrusteraf6bf132011-05-18 18:31:02 +0200293 { .driver = "ide-cd", .flag = &default_cdrom },
294 { .driver = "ide-hd", .flag = &default_cdrom },
Gerd Hoffmannd8bcbab2009-12-16 14:25:40 +0100295 { .driver = "ide-drive", .flag = &default_cdrom },
Markus Armbrusteraf6bf132011-05-18 18:31:02 +0200296 { .driver = "scsi-cd", .flag = &default_cdrom },
Amit Shah392ecf52010-01-21 16:19:23 +0530297 { .driver = "virtio-serial-pci", .flag = &default_virtcon },
298 { .driver = "virtio-serial-s390", .flag = &default_virtcon },
299 { .driver = "virtio-serial", .flag = &default_virtcon },
Paolo Bonzini7f1b17f2012-05-10 09:39:17 +0200300 { .driver = "VGA", .flag = &default_vga },
301 { .driver = "isa-vga", .flag = &default_vga },
302 { .driver = "cirrus-vga", .flag = &default_vga },
303 { .driver = "isa-cirrus-vga", .flag = &default_vga },
304 { .driver = "vmware-svga", .flag = &default_vga },
305 { .driver = "qxl-vga", .flag = &default_vga },
Gerd Hoffmann998bbd72009-12-08 13:11:41 +0100306};
307
Paolo Bonzini4d454572012-11-26 16:03:42 +0100308static QemuOptsList qemu_rtc_opts = {
309 .name = "rtc",
310 .head = QTAILQ_HEAD_INITIALIZER(qemu_rtc_opts.head),
311 .desc = {
312 {
313 .name = "base",
314 .type = QEMU_OPT_STRING,
315 },{
316 .name = "clock",
317 .type = QEMU_OPT_STRING,
318 },{
319 .name = "driftfix",
320 .type = QEMU_OPT_STRING,
321 },
322 { /* end of list */ }
323 },
324};
325
326static QemuOptsList qemu_sandbox_opts = {
327 .name = "sandbox",
328 .implied_opt_name = "enable",
329 .head = QTAILQ_HEAD_INITIALIZER(qemu_sandbox_opts.head),
330 .desc = {
331 {
332 .name = "enable",
333 .type = QEMU_OPT_BOOL,
334 },
335 { /* end of list */ }
336 },
337};
338
339static QemuOptsList qemu_trace_opts = {
340 .name = "trace",
341 .implied_opt_name = "trace",
342 .head = QTAILQ_HEAD_INITIALIZER(qemu_trace_opts.head),
343 .desc = {
344 {
345 .name = "events",
346 .type = QEMU_OPT_STRING,
347 },{
348 .name = "file",
349 .type = QEMU_OPT_STRING,
350 },
351 { /* end of list */ }
352 },
353};
354
355static QemuOptsList qemu_option_rom_opts = {
356 .name = "option-rom",
357 .implied_opt_name = "romfile",
358 .head = QTAILQ_HEAD_INITIALIZER(qemu_option_rom_opts.head),
359 .desc = {
360 {
361 .name = "bootindex",
362 .type = QEMU_OPT_NUMBER,
363 }, {
364 .name = "romfile",
365 .type = QEMU_OPT_STRING,
366 },
367 { /* end of list */ }
368 },
369};
370
371static QemuOptsList qemu_machine_opts = {
372 .name = "machine",
373 .implied_opt_name = "type",
374 .merge_lists = true,
375 .head = QTAILQ_HEAD_INITIALIZER(qemu_machine_opts.head),
376 .desc = {
377 {
378 .name = "type",
379 .type = QEMU_OPT_STRING,
380 .help = "emulated machine"
381 }, {
382 .name = "accel",
383 .type = QEMU_OPT_STRING,
384 .help = "accelerator list",
385 }, {
386 .name = "kernel_irqchip",
387 .type = QEMU_OPT_BOOL,
388 .help = "use KVM in-kernel irqchip",
389 }, {
390 .name = "kvm_shadow_mem",
391 .type = QEMU_OPT_SIZE,
392 .help = "KVM shadow MMU size",
393 }, {
394 .name = "kernel",
395 .type = QEMU_OPT_STRING,
396 .help = "Linux kernel image file",
397 }, {
398 .name = "initrd",
399 .type = QEMU_OPT_STRING,
400 .help = "Linux initial ramdisk file",
401 }, {
402 .name = "append",
403 .type = QEMU_OPT_STRING,
404 .help = "Linux kernel command line",
405 }, {
406 .name = "dtb",
407 .type = QEMU_OPT_STRING,
408 .help = "Linux kernel device tree file",
409 }, {
410 .name = "dumpdtb",
411 .type = QEMU_OPT_STRING,
412 .help = "Dump current dtb to a file and quit",
413 }, {
414 .name = "phandle_start",
415 .type = QEMU_OPT_STRING,
416 .help = "The first phandle ID we may generate dynamically",
417 }, {
418 .name = "dt_compatible",
419 .type = QEMU_OPT_STRING,
420 .help = "Overrides the \"compatible\" property of the dt root node",
421 }, {
422 .name = "dump-guest-core",
423 .type = QEMU_OPT_BOOL,
424 .help = "Include guest memory in a core dump",
425 }, {
426 .name = "mem-merge",
427 .type = QEMU_OPT_BOOL,
428 .help = "enable/disable memory merge support",
429 },{
430 .name = "usb",
431 .type = QEMU_OPT_BOOL,
432 .help = "Set on/off to enable/disable usb",
433 },
434 { /* End of list */ }
435 },
436};
437
438static QemuOptsList qemu_boot_opts = {
439 .name = "boot-opts",
440 .head = QTAILQ_HEAD_INITIALIZER(qemu_boot_opts.head),
441 .desc = {
442 /* the three names below are not used now */
443 {
444 .name = "order",
445 .type = QEMU_OPT_STRING,
446 }, {
447 .name = "once",
448 .type = QEMU_OPT_STRING,
449 }, {
450 .name = "menu",
451 .type = QEMU_OPT_STRING,
452 /* following are really used */
453 }, {
454 .name = "splash",
455 .type = QEMU_OPT_STRING,
456 }, {
457 .name = "splash-time",
458 .type = QEMU_OPT_STRING,
459 }, {
460 .name = "reboot-timeout",
461 .type = QEMU_OPT_STRING,
Amos Kongc8a6ae82013-03-19 14:23:27 +0800462 }, {
463 .name = "strict",
464 .type = QEMU_OPT_STRING,
Paolo Bonzini4d454572012-11-26 16:03:42 +0100465 },
466 { /*End of list */ }
467 },
468};
469
470static QemuOptsList qemu_add_fd_opts = {
471 .name = "add-fd",
472 .head = QTAILQ_HEAD_INITIALIZER(qemu_add_fd_opts.head),
473 .desc = {
474 {
475 .name = "fd",
476 .type = QEMU_OPT_NUMBER,
477 .help = "file descriptor of which a duplicate is added to fd set",
478 },{
479 .name = "set",
480 .type = QEMU_OPT_NUMBER,
481 .help = "ID of the fd set to add fd to",
482 },{
483 .name = "opaque",
484 .type = QEMU_OPT_STRING,
485 .help = "free-form string used to describe fd",
486 },
487 { /* end of list */ }
488 },
489};
490
491static QemuOptsList qemu_object_opts = {
492 .name = "object",
493 .implied_opt_name = "qom-type",
494 .head = QTAILQ_HEAD_INITIALIZER(qemu_object_opts.head),
495 .desc = {
496 { }
497 },
498};
499
Stefan Bergerd1a0cf72013-02-27 12:47:49 -0500500static QemuOptsList qemu_tpmdev_opts = {
501 .name = "tpmdev",
502 .implied_opt_name = "type",
503 .head = QTAILQ_HEAD_INITIALIZER(qemu_tpmdev_opts.head),
504 .desc = {
505 {
506 .name = "type",
507 .type = QEMU_OPT_STRING,
508 .help = "Type of TPM backend",
509 },
510 {
Stefan Berger92dcc232013-02-27 12:47:54 -0500511 .name = "cancel-path",
512 .type = QEMU_OPT_STRING,
513 .help = "Sysfs file entry for canceling TPM commands",
514 },
515 {
Stefan Bergerd1a0cf72013-02-27 12:47:49 -0500516 .name = "path",
517 .type = QEMU_OPT_STRING,
518 .help = "Path to TPM device on the host",
519 },
520 { /* end of list */ }
521 },
522};
523
Ronnie Sahlberg31459f42012-08-06 18:24:55 +1000524const char *qemu_get_vm_name(void)
525{
526 return qemu_name;
527}
528
wayne3d3b8302011-07-27 18:04:55 +0800529static void res_free(void)
530{
531 if (boot_splash_filedata != NULL) {
Anthony Liguori7267c092011-08-20 22:09:37 -0500532 g_free(boot_splash_filedata);
wayne3d3b8302011-07-27 18:04:55 +0800533 boot_splash_filedata = NULL;
534 }
535}
536
Gerd Hoffmann998bbd72009-12-08 13:11:41 +0100537static int default_driver_check(QemuOpts *opts, void *opaque)
538{
539 const char *driver = qemu_opt_get(opts, "driver");
540 int i;
541
542 if (!driver)
543 return 0;
544 for (i = 0; i < ARRAY_SIZE(default_list); i++) {
545 if (strcmp(default_list[i].driver, driver) != 0)
546 continue;
547 *(default_list[i].flag) = 0;
548 }
549 return 0;
550}
551
bellard0824d6f2003-06-24 13:42:40 +0000552/***********************************************************/
Luiz Capitulinof5bbfba2011-07-29 15:04:45 -0300553/* QEMU state */
554
Luiz Capitulino0461d5a2011-09-30 14:45:27 -0300555static RunState current_run_state = RUN_STATE_PRELAUNCH;
Luiz Capitulinof5bbfba2011-07-29 15:04:45 -0300556
Luiz Capitulino5db9d4d2011-09-05 16:36:31 -0300557typedef struct {
558 RunState from;
559 RunState to;
560} RunStateTransition;
561
562static const RunStateTransition runstate_transitions_def[] = {
563 /* from -> to */
Luiz Capitulino0461d5a2011-09-30 14:45:27 -0300564 { RUN_STATE_DEBUG, RUN_STATE_RUNNING },
Luiz Capitulino5db9d4d2011-09-05 16:36:31 -0300565
Luiz Capitulino0461d5a2011-09-30 14:45:27 -0300566 { RUN_STATE_INMIGRATE, RUN_STATE_RUNNING },
Paolo Bonzini29ed72f2012-10-19 16:45:24 +0200567 { RUN_STATE_INMIGRATE, RUN_STATE_PAUSED },
Luiz Capitulino5db9d4d2011-09-05 16:36:31 -0300568
Luiz Capitulino0461d5a2011-09-30 14:45:27 -0300569 { RUN_STATE_INTERNAL_ERROR, RUN_STATE_PAUSED },
Luiz Capitulino8a9236f2011-10-14 11:18:09 -0300570 { RUN_STATE_INTERNAL_ERROR, RUN_STATE_FINISH_MIGRATE },
Luiz Capitulino5db9d4d2011-09-05 16:36:31 -0300571
Luiz Capitulino0461d5a2011-09-30 14:45:27 -0300572 { RUN_STATE_IO_ERROR, RUN_STATE_RUNNING },
Luiz Capitulino8a9236f2011-10-14 11:18:09 -0300573 { RUN_STATE_IO_ERROR, RUN_STATE_FINISH_MIGRATE },
Luiz Capitulino5db9d4d2011-09-05 16:36:31 -0300574
Luiz Capitulino0461d5a2011-09-30 14:45:27 -0300575 { RUN_STATE_PAUSED, RUN_STATE_RUNNING },
Luiz Capitulino8a9236f2011-10-14 11:18:09 -0300576 { RUN_STATE_PAUSED, RUN_STATE_FINISH_MIGRATE },
Luiz Capitulino5db9d4d2011-09-05 16:36:31 -0300577
Luiz Capitulino0461d5a2011-09-30 14:45:27 -0300578 { RUN_STATE_POSTMIGRATE, RUN_STATE_RUNNING },
Luiz Capitulino8a9236f2011-10-14 11:18:09 -0300579 { RUN_STATE_POSTMIGRATE, RUN_STATE_FINISH_MIGRATE },
Luiz Capitulino5db9d4d2011-09-05 16:36:31 -0300580
Luiz Capitulino0461d5a2011-09-30 14:45:27 -0300581 { RUN_STATE_PRELAUNCH, RUN_STATE_RUNNING },
Luiz Capitulino8a9236f2011-10-14 11:18:09 -0300582 { RUN_STATE_PRELAUNCH, RUN_STATE_FINISH_MIGRATE },
Luiz Capitulino0461d5a2011-09-30 14:45:27 -0300583 { RUN_STATE_PRELAUNCH, RUN_STATE_INMIGRATE },
Luiz Capitulino5db9d4d2011-09-05 16:36:31 -0300584
Luiz Capitulino0461d5a2011-09-30 14:45:27 -0300585 { RUN_STATE_FINISH_MIGRATE, RUN_STATE_RUNNING },
586 { RUN_STATE_FINISH_MIGRATE, RUN_STATE_POSTMIGRATE },
Luiz Capitulino5db9d4d2011-09-05 16:36:31 -0300587
Luiz Capitulino0461d5a2011-09-30 14:45:27 -0300588 { RUN_STATE_RESTORE_VM, RUN_STATE_RUNNING },
Luiz Capitulino5db9d4d2011-09-05 16:36:31 -0300589
Luiz Capitulino0461d5a2011-09-30 14:45:27 -0300590 { RUN_STATE_RUNNING, RUN_STATE_DEBUG },
591 { RUN_STATE_RUNNING, RUN_STATE_INTERNAL_ERROR },
592 { RUN_STATE_RUNNING, RUN_STATE_IO_ERROR },
593 { RUN_STATE_RUNNING, RUN_STATE_PAUSED },
594 { RUN_STATE_RUNNING, RUN_STATE_FINISH_MIGRATE },
595 { RUN_STATE_RUNNING, RUN_STATE_RESTORE_VM },
596 { RUN_STATE_RUNNING, RUN_STATE_SAVE_VM },
597 { RUN_STATE_RUNNING, RUN_STATE_SHUTDOWN },
598 { RUN_STATE_RUNNING, RUN_STATE_WATCHDOG },
Luiz Capitulino5db9d4d2011-09-05 16:36:31 -0300599
Luiz Capitulino0461d5a2011-09-30 14:45:27 -0300600 { RUN_STATE_SAVE_VM, RUN_STATE_RUNNING },
Luiz Capitulino5db9d4d2011-09-05 16:36:31 -0300601
Luiz Capitulino0461d5a2011-09-30 14:45:27 -0300602 { RUN_STATE_SHUTDOWN, RUN_STATE_PAUSED },
Luiz Capitulino8a9236f2011-10-14 11:18:09 -0300603 { RUN_STATE_SHUTDOWN, RUN_STATE_FINISH_MIGRATE },
Luiz Capitulino5db9d4d2011-09-05 16:36:31 -0300604
Luiz Capitulinoad02b962012-04-27 13:33:36 -0300605 { RUN_STATE_DEBUG, RUN_STATE_SUSPENDED },
606 { RUN_STATE_RUNNING, RUN_STATE_SUSPENDED },
607 { RUN_STATE_SUSPENDED, RUN_STATE_RUNNING },
608 { RUN_STATE_SUSPENDED, RUN_STATE_FINISH_MIGRATE },
609
Luiz Capitulino0461d5a2011-09-30 14:45:27 -0300610 { RUN_STATE_WATCHDOG, RUN_STATE_RUNNING },
Luiz Capitulino8a9236f2011-10-14 11:18:09 -0300611 { RUN_STATE_WATCHDOG, RUN_STATE_FINISH_MIGRATE },
Luiz Capitulino5db9d4d2011-09-05 16:36:31 -0300612
Luiz Capitulino0461d5a2011-09-30 14:45:27 -0300613 { RUN_STATE_MAX, RUN_STATE_MAX },
Luiz Capitulino5db9d4d2011-09-05 16:36:31 -0300614};
615
Luiz Capitulino0461d5a2011-09-30 14:45:27 -0300616static bool runstate_valid_transitions[RUN_STATE_MAX][RUN_STATE_MAX];
Luiz Capitulino5db9d4d2011-09-05 16:36:31 -0300617
Luiz Capitulinof5bbfba2011-07-29 15:04:45 -0300618bool runstate_check(RunState state)
619{
620 return current_run_state == state;
621}
622
Blue Swirl4fdcac02012-10-28 11:04:47 +0000623static void runstate_init(void)
Luiz Capitulino5db9d4d2011-09-05 16:36:31 -0300624{
625 const RunStateTransition *p;
626
627 memset(&runstate_valid_transitions, 0, sizeof(runstate_valid_transitions));
628
Luiz Capitulino0461d5a2011-09-30 14:45:27 -0300629 for (p = &runstate_transitions_def[0]; p->from != RUN_STATE_MAX; p++) {
Luiz Capitulino5db9d4d2011-09-05 16:36:31 -0300630 runstate_valid_transitions[p->from][p->to] = true;
631 }
632}
633
634/* This function will abort() on invalid state transitions */
Luiz Capitulinof5bbfba2011-07-29 15:04:45 -0300635void runstate_set(RunState new_state)
636{
Luiz Capitulino207c5cd2011-10-13 10:59:07 -0300637 assert(new_state < RUN_STATE_MAX);
638
639 if (!runstate_valid_transitions[current_run_state][new_state]) {
640 fprintf(stderr, "ERROR: invalid runstate transition: '%s' -> '%s'\n",
641 RunState_lookup[current_run_state],
642 RunState_lookup[new_state]);
Luiz Capitulino5db9d4d2011-09-05 16:36:31 -0300643 abort();
644 }
645
Luiz Capitulinof5bbfba2011-07-29 15:04:45 -0300646 current_run_state = new_state;
647}
648
Luiz Capitulino13548692011-07-29 15:36:43 -0300649int runstate_is_running(void)
650{
Luiz Capitulino0461d5a2011-09-30 14:45:27 -0300651 return runstate_check(RUN_STATE_RUNNING);
Luiz Capitulino13548692011-07-29 15:36:43 -0300652}
653
Luiz Capitulino1fa9a5e2011-09-12 17:54:20 -0300654StatusInfo *qmp_query_status(Error **errp)
655{
656 StatusInfo *info = g_malloc0(sizeof(*info));
657
658 info->running = runstate_is_running();
659 info->singlestep = singlestep;
660 info->status = current_run_state;
661
662 return info;
663}
664
Michal Novotny4d700432013-03-25 17:31:33 +0100665int64_t qmp_query_cpu_max(Error **errp)
666{
667 return current_machine->max_cpus;
668}
669
Luiz Capitulinof5bbfba2011-07-29 15:04:45 -0300670/***********************************************************/
Paolo Bonzini8f0056b2010-01-13 14:05:34 +0100671/* real time host monotonic timer */
bellard09b26c52006-04-12 21:09:08 +0000672
bellardc4b1fcc2004-03-14 21:44:30 +0000673/***********************************************************/
balrogf6503052008-02-17 11:42:19 +0000674/* host time/date access */
675void qemu_get_timedate(struct tm *tm, int offset)
676{
677 time_t ti;
balrogf6503052008-02-17 11:42:19 +0000678
679 time(&ti);
680 ti += offset;
681 if (rtc_date_offset == -1) {
682 if (rtc_utc)
Stefan Weileb7ff6f2013-01-07 23:08:13 +0100683 gmtime_r(&ti, tm);
balrogf6503052008-02-17 11:42:19 +0000684 else
Stefan Weileb7ff6f2013-01-07 23:08:13 +0100685 localtime_r(&ti, tm);
balrogf6503052008-02-17 11:42:19 +0000686 } else {
687 ti -= rtc_date_offset;
Stefan Weileb7ff6f2013-01-07 23:08:13 +0100688 gmtime_r(&ti, tm);
balrogf6503052008-02-17 11:42:19 +0000689 }
balrogf6503052008-02-17 11:42:19 +0000690}
691
692int qemu_timedate_diff(struct tm *tm)
693{
694 time_t seconds;
695
696 if (rtc_date_offset == -1)
697 if (rtc_utc)
698 seconds = mktimegm(tm);
Gleb Natapovf54c5562011-11-06 18:00:22 +0200699 else {
700 struct tm tmp = *tm;
701 tmp.tm_isdst = -1; /* use timezone to figure it out */
702 seconds = mktime(&tmp);
703 }
balrogf6503052008-02-17 11:42:19 +0000704 else
705 seconds = mktimegm(tm) + rtc_date_offset;
706
707 return seconds - time(NULL);
708}
709
Luiz Capitulino80cd3472010-02-25 12:11:44 -0300710void rtc_change_mon_event(struct tm *tm)
711{
712 QObject *data;
713
714 data = qobject_from_jsonf("{ 'offset': %d }", qemu_timedate_diff(tm));
715 monitor_protocol_event(QEVENT_RTC_CHANGE, data);
716 qobject_decref(data);
717}
718
Jan Kiszka1ed2fc12009-09-15 13:36:04 +0200719static void configure_rtc_date_offset(const char *startdate, int legacy)
720{
721 time_t rtc_start_date;
722 struct tm tm;
723
724 if (!strcmp(startdate, "now") && legacy) {
725 rtc_date_offset = -1;
726 } else {
727 if (sscanf(startdate, "%d-%d-%dT%d:%d:%d",
728 &tm.tm_year,
729 &tm.tm_mon,
730 &tm.tm_mday,
731 &tm.tm_hour,
732 &tm.tm_min,
733 &tm.tm_sec) == 6) {
734 /* OK */
735 } else if (sscanf(startdate, "%d-%d-%d",
736 &tm.tm_year,
737 &tm.tm_mon,
738 &tm.tm_mday) == 3) {
739 tm.tm_hour = 0;
740 tm.tm_min = 0;
741 tm.tm_sec = 0;
742 } else {
743 goto date_fail;
744 }
745 tm.tm_year -= 1900;
746 tm.tm_mon--;
747 rtc_start_date = mktimegm(&tm);
748 if (rtc_start_date == -1) {
749 date_fail:
750 fprintf(stderr, "Invalid date format. Valid formats are:\n"
751 "'2006-06-17T16:01:21' or '2006-06-17'\n");
752 exit(1);
753 }
754 rtc_date_offset = time(NULL) - rtc_start_date;
755 }
756}
757
758static void configure_rtc(QemuOpts *opts)
759{
760 const char *value;
761
762 value = qemu_opt_get(opts, "base");
763 if (value) {
764 if (!strcmp(value, "utc")) {
765 rtc_utc = 1;
766 } else if (!strcmp(value, "localtime")) {
767 rtc_utc = 0;
768 } else {
769 configure_rtc_date_offset(value, 0);
770 }
771 }
Jan Kiszka68752042009-09-15 13:36:04 +0200772 value = qemu_opt_get(opts, "clock");
773 if (value) {
774 if (!strcmp(value, "host")) {
775 rtc_clock = host_clock;
Paolo Bonzini78808142012-03-30 10:31:21 +0000776 } else if (!strcmp(value, "rt")) {
777 rtc_clock = rt_clock;
Jan Kiszka68752042009-09-15 13:36:04 +0200778 } else if (!strcmp(value, "vm")) {
779 rtc_clock = vm_clock;
780 } else {
781 fprintf(stderr, "qemu: invalid option value '%s'\n", value);
782 exit(1);
783 }
784 }
Jan Kiszka1ed2fc12009-09-15 13:36:04 +0200785 value = qemu_opt_get(opts, "driftfix");
786 if (value) {
Blue Swirl7e4c0332010-03-27 21:33:46 +0000787 if (!strcmp(value, "slew")) {
Jan Kiszka433acf02012-01-23 20:15:12 +0100788 static GlobalProperty slew_lost_ticks[] = {
789 {
790 .driver = "mc146818rtc",
791 .property = "lost_tick_policy",
792 .value = "slew",
793 },
794 { /* end of list */ }
795 };
796
797 qdev_prop_register_global_list(slew_lost_ticks);
Blue Swirl7e4c0332010-03-27 21:33:46 +0000798 } else if (!strcmp(value, "none")) {
Jan Kiszka433acf02012-01-23 20:15:12 +0100799 /* discard is default */
Jan Kiszka1ed2fc12009-09-15 13:36:04 +0200800 } else {
801 fprintf(stderr, "qemu: invalid option value '%s'\n", value);
802 exit(1);
803 }
804 }
Jan Kiszka1ed2fc12009-09-15 13:36:04 +0200805}
806
balrog1ae26a12008-09-28 23:19:47 +0000807/***********************************************************/
808/* Bluetooth support */
809static int nb_hcis;
810static int cur_hci;
811static struct HCIInfo *hci_table[MAX_NICS];
balrogdc72ac12008-11-09 00:04:26 +0000812
balrog1ae26a12008-09-28 23:19:47 +0000813static struct bt_vlan_s {
814 struct bt_scatternet_s net;
815 int id;
816 struct bt_vlan_s *next;
817} *first_bt_vlan;
818
819/* find or alloc a new bluetooth "VLAN" */
blueswir1674bb262008-09-30 18:18:27 +0000820static struct bt_scatternet_s *qemu_find_bt_vlan(int id)
balrog1ae26a12008-09-28 23:19:47 +0000821{
822 struct bt_vlan_s **pvlan, *vlan;
823 for (vlan = first_bt_vlan; vlan != NULL; vlan = vlan->next) {
824 if (vlan->id == id)
825 return &vlan->net;
826 }
Anthony Liguori7267c092011-08-20 22:09:37 -0500827 vlan = g_malloc0(sizeof(struct bt_vlan_s));
balrog1ae26a12008-09-28 23:19:47 +0000828 vlan->id = id;
829 pvlan = &first_bt_vlan;
830 while (*pvlan != NULL)
831 pvlan = &(*pvlan)->next;
832 *pvlan = vlan;
833 return &vlan->net;
834}
835
836static void null_hci_send(struct HCIInfo *hci, const uint8_t *data, int len)
837{
838}
839
840static int null_hci_addr_set(struct HCIInfo *hci, const uint8_t *bd_addr)
841{
842 return -ENOTSUP;
843}
844
845static struct HCIInfo null_hci = {
846 .cmd_send = null_hci_send,
847 .sco_send = null_hci_send,
848 .acl_send = null_hci_send,
849 .bdaddr_set = null_hci_addr_set,
850};
851
852struct HCIInfo *qemu_next_hci(void)
853{
854 if (cur_hci == nb_hcis)
855 return &null_hci;
856
857 return hci_table[cur_hci++];
858}
859
balrogdc72ac12008-11-09 00:04:26 +0000860static struct HCIInfo *hci_init(const char *str)
861{
862 char *endp;
863 struct bt_scatternet_s *vlan = 0;
864
865 if (!strcmp(str, "null"))
866 /* null */
867 return &null_hci;
868 else if (!strncmp(str, "host", 4) && (str[4] == '\0' || str[4] == ':'))
869 /* host[:hciN] */
870 return bt_host_hci(str[4] ? str + 5 : "hci0");
871 else if (!strncmp(str, "hci", 3)) {
872 /* hci[,vlan=n] */
873 if (str[3]) {
874 if (!strncmp(str + 3, ",vlan=", 6)) {
875 vlan = qemu_find_bt_vlan(strtol(str + 9, &endp, 0));
876 if (*endp)
877 vlan = 0;
878 }
879 } else
880 vlan = qemu_find_bt_vlan(0);
881 if (vlan)
882 return bt_new_hci(vlan);
883 }
884
885 fprintf(stderr, "qemu: Unknown bluetooth HCI `%s'.\n", str);
886
887 return 0;
888}
889
890static int bt_hci_parse(const char *str)
891{
892 struct HCIInfo *hci;
Anthony Liguoric227f092009-10-01 16:12:16 -0500893 bdaddr_t bdaddr;
balrogdc72ac12008-11-09 00:04:26 +0000894
895 if (nb_hcis >= MAX_NICS) {
896 fprintf(stderr, "qemu: Too many bluetooth HCIs (max %i).\n", MAX_NICS);
897 return -1;
898 }
899
900 hci = hci_init(str);
901 if (!hci)
902 return -1;
903
904 bdaddr.b[0] = 0x52;
905 bdaddr.b[1] = 0x54;
906 bdaddr.b[2] = 0x00;
907 bdaddr.b[3] = 0x12;
908 bdaddr.b[4] = 0x34;
909 bdaddr.b[5] = 0x56 + nb_hcis;
910 hci->bdaddr_set(hci, bdaddr.b);
911
912 hci_table[nb_hcis++] = hci;
913
914 return 0;
915}
916
917static void bt_vhci_add(int vlan_id)
918{
919 struct bt_scatternet_s *vlan = qemu_find_bt_vlan(vlan_id);
920
921 if (!vlan->slave)
922 fprintf(stderr, "qemu: warning: adding a VHCI to "
923 "an empty scatternet %i\n", vlan_id);
924
925 bt_vhci_init(bt_new_hci(vlan));
926}
927
928static struct bt_device_s *bt_device_add(const char *opt)
929{
930 struct bt_scatternet_s *vlan;
931 int vlan_id = 0;
932 char *endp = strstr(opt, ",vlan=");
933 int len = (endp ? endp - opt : strlen(opt)) + 1;
934 char devname[10];
935
936 pstrcpy(devname, MIN(sizeof(devname), len), opt);
937
938 if (endp) {
939 vlan_id = strtol(endp + 6, &endp, 0);
940 if (*endp) {
941 fprintf(stderr, "qemu: unrecognised bluetooth vlan Id\n");
942 return 0;
943 }
944 }
945
946 vlan = qemu_find_bt_vlan(vlan_id);
947
948 if (!vlan->slave)
949 fprintf(stderr, "qemu: warning: adding a slave device to "
950 "an empty scatternet %i\n", vlan_id);
951
952 if (!strcmp(devname, "keyboard"))
953 return bt_keyboard_init(vlan);
954
955 fprintf(stderr, "qemu: unsupported bluetooth device `%s'\n", devname);
956 return 0;
957}
958
959static int bt_parse(const char *opt)
960{
961 const char *endp, *p;
962 int vlan;
963
964 if (strstart(opt, "hci", &endp)) {
965 if (!*endp || *endp == ',') {
966 if (*endp)
967 if (!strstart(endp, ",vlan=", 0))
968 opt = endp + 1;
969
970 return bt_hci_parse(opt);
971 }
972 } else if (strstart(opt, "vhci", &endp)) {
973 if (!*endp || *endp == ',') {
974 if (*endp) {
975 if (strstart(endp, ",vlan=", &p)) {
976 vlan = strtol(p, (char **) &endp, 0);
977 if (*endp) {
978 fprintf(stderr, "qemu: bad scatternet '%s'\n", p);
979 return 1;
980 }
981 } else {
982 fprintf(stderr, "qemu: bad parameter '%s'\n", endp + 1);
983 return 1;
984 }
985 } else
986 vlan = 0;
987
988 bt_vhci_add(vlan);
989 return 0;
990 }
991 } else if (strstart(opt, "device:", &endp))
992 return !bt_device_add(endp);
993
994 fprintf(stderr, "qemu: bad bluetooth parameter '%s'\n", opt);
995 return 1;
996}
997
Eduardo Otubo7d76ad42012-08-14 18:44:08 -0300998static int parse_sandbox(QemuOpts *opts, void *opaque)
999{
1000 /* FIXME: change this to true for 1.3 */
1001 if (qemu_opt_get_bool(opts, "enable", false)) {
1002#ifdef CONFIG_SECCOMP
1003 if (seccomp_start() < 0) {
1004 qerror_report(ERROR_CLASS_GENERIC_ERROR,
1005 "failed to install seccomp syscall filter in the kernel");
1006 return -1;
1007 }
1008#else
1009 qerror_report(ERROR_CLASS_GENERIC_ERROR,
1010 "sandboxing request but seccomp is not compiled into this build");
1011 return -1;
1012#endif
1013 }
1014
1015 return 0;
1016}
1017
zhlcindy@gmail.com094b2872012-09-02 19:25:28 +00001018/*********QEMU USB setting******/
1019bool usb_enabled(bool default_usb)
1020{
1021 QemuOpts *mach_opts;
1022 mach_opts = qemu_opts_find(qemu_find_opts("machine"), 0);
1023 if (mach_opts) {
1024 return qemu_opt_get_bool(mach_opts, "usb", default_usb);
1025 }
1026 return default_usb;
1027}
1028
Corey Bryant587ed6b2012-10-18 15:19:34 -04001029#ifndef _WIN32
1030static int parse_add_fd(QemuOpts *opts, void *opaque)
1031{
1032 int fd, dupfd, flags;
1033 int64_t fdset_id;
1034 const char *fd_opaque = NULL;
1035
1036 fd = qemu_opt_get_number(opts, "fd", -1);
1037 fdset_id = qemu_opt_get_number(opts, "set", -1);
1038 fd_opaque = qemu_opt_get(opts, "opaque");
1039
1040 if (fd < 0) {
1041 qerror_report(ERROR_CLASS_GENERIC_ERROR,
1042 "fd option is required and must be non-negative");
1043 return -1;
1044 }
1045
1046 if (fd <= STDERR_FILENO) {
1047 qerror_report(ERROR_CLASS_GENERIC_ERROR,
1048 "fd cannot be a standard I/O stream");
1049 return -1;
1050 }
1051
1052 /*
1053 * All fds inherited across exec() necessarily have FD_CLOEXEC
1054 * clear, while qemu sets FD_CLOEXEC on all other fds used internally.
1055 */
1056 flags = fcntl(fd, F_GETFD);
1057 if (flags == -1 || (flags & FD_CLOEXEC)) {
1058 qerror_report(ERROR_CLASS_GENERIC_ERROR,
1059 "fd is not valid or already in use");
1060 return -1;
1061 }
1062
1063 if (fdset_id < 0) {
1064 qerror_report(ERROR_CLASS_GENERIC_ERROR,
1065 "set option is required and must be non-negative");
1066 return -1;
1067 }
1068
1069#ifdef F_DUPFD_CLOEXEC
1070 dupfd = fcntl(fd, F_DUPFD_CLOEXEC, 0);
1071#else
1072 dupfd = dup(fd);
1073 if (dupfd != -1) {
1074 qemu_set_cloexec(dupfd);
1075 }
1076#endif
1077 if (dupfd == -1) {
1078 qerror_report(ERROR_CLASS_GENERIC_ERROR,
1079 "Error duplicating fd: %s", strerror(errno));
1080 return -1;
1081 }
1082
1083 /* add the duplicate fd, and optionally the opaque string, to the fd set */
1084 monitor_fdset_add_fd(dupfd, true, fdset_id, fd_opaque ? true : false,
1085 fd_opaque, NULL);
1086
1087 return 0;
1088}
1089
1090static int cleanup_add_fd(QemuOpts *opts, void *opaque)
1091{
1092 int fd;
1093
1094 fd = qemu_opt_get_number(opts, "fd", -1);
1095 close(fd);
1096
1097 return 0;
1098}
1099#endif
1100
balrog1ae26a12008-09-28 23:19:47 +00001101/***********************************************************/
1102/* QEMU Block devices */
1103
Markus Armbruster2292dda2011-01-28 11:21:41 +01001104#define HD_OPTS "media=disk"
1105#define CDROM_OPTS "media=cdrom"
1106#define FD_OPTS ""
1107#define PFLASH_OPTS ""
1108#define MTD_OPTS ""
1109#define SD_OPTS ""
thse4bcb142007-12-02 04:51:10 +00001110
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +02001111static int drive_init_func(QemuOpts *opts, void *opaque)
1112{
Christian Borntraeger2d0d2832012-11-20 15:30:34 +01001113 BlockInterfaceType *block_default_type = opaque;
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +02001114
Christian Borntraeger2d0d2832012-11-20 15:30:34 +01001115 return drive_init(opts, *block_default_type) == NULL;
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +02001116}
1117
1118static int drive_enable_snapshot(QemuOpts *opts, void *opaque)
1119{
1120 if (NULL == qemu_opt_get(opts, "snapshot")) {
1121 qemu_opt_set(opts, "snapshot", "on");
1122 }
1123 return 0;
1124}
1125
Christian Borntraeger3c42ea62012-11-22 21:02:55 +01001126static void default_drive(int enable, int snapshot, BlockInterfaceType type,
1127 int index, const char *optstr)
Markus Armbruster4e5d9b52011-01-28 11:21:45 +01001128{
1129 QemuOpts *opts;
1130
Markus Armbruster4e5d9b52011-01-28 11:21:45 +01001131 if (!enable || drive_get_by_index(type, index)) {
1132 return;
1133 }
1134
1135 opts = drive_add(type, index, NULL, optstr);
1136 if (snapshot) {
1137 drive_enable_snapshot(opts, NULL);
1138 }
Christian Borntraeger2d0d2832012-11-20 15:30:34 +01001139 if (!drive_init(opts, type)) {
Markus Armbruster4e5d9b52011-01-28 11:21:45 +01001140 exit(1);
1141 }
1142}
1143
Jan Kiszka76e30d02009-07-02 00:19:02 +02001144void qemu_register_boot_set(QEMUBootSetHandler *func, void *opaque)
1145{
1146 boot_set_handler = func;
1147 boot_set_opaque = opaque;
1148}
1149
1150int qemu_boot_set(const char *boot_devices)
1151{
1152 if (!boot_set_handler) {
1153 return -EINVAL;
1154 }
1155 return boot_set_handler(boot_set_opaque, boot_devices);
1156}
1157
Eduardo Habkost4e9e9d62010-04-06 19:22:08 -03001158static void validate_bootdevices(char *devices)
Jan Kiszkaef3adf62009-07-02 00:19:02 +02001159{
1160 /* We just do some generic consistency checks */
1161 const char *p;
1162 int bitmap = 0;
1163
1164 for (p = devices; *p != '\0'; p++) {
1165 /* Allowed boot devices are:
1166 * a-b: floppy disk drives
1167 * c-f: IDE disk drives
Dong Xu Wang07f35072011-11-22 18:06:26 +08001168 * g-m: machine implementation dependent drives
Jan Kiszkaef3adf62009-07-02 00:19:02 +02001169 * n-p: network devices
1170 * It's up to each machine implementation to check if the given boot
1171 * devices match the actual hardware implementation and firmware
1172 * features.
1173 */
1174 if (*p < 'a' || *p > 'p') {
1175 fprintf(stderr, "Invalid boot device '%c'\n", *p);
1176 exit(1);
1177 }
1178 if (bitmap & (1 << (*p - 'a'))) {
1179 fprintf(stderr, "Boot device '%c' was given twice\n", *p);
1180 exit(1);
1181 }
1182 bitmap |= 1 << (*p - 'a');
1183 }
Jan Kiszkaef3adf62009-07-02 00:19:02 +02001184}
1185
Jan Kiszkae0f084b2009-07-02 00:19:02 +02001186static void restore_boot_devices(void *opaque)
1187{
1188 char *standard_boot_devices = opaque;
Alex Williamson37905d62010-04-30 15:21:11 -06001189 static int first = 1;
1190
1191 /* Restore boot order and remove ourselves after the first boot */
1192 if (first) {
1193 first = 0;
1194 return;
1195 }
Jan Kiszkae0f084b2009-07-02 00:19:02 +02001196
1197 qemu_boot_set(standard_boot_devices);
1198
1199 qemu_unregister_reset(restore_boot_devices, standard_boot_devices);
Anthony Liguori7267c092011-08-20 22:09:37 -05001200 g_free(standard_boot_devices);
Jan Kiszkae0f084b2009-07-02 00:19:02 +02001201}
1202
Gleb Natapov1ca4d092010-12-08 13:35:05 +02001203void add_boot_device_path(int32_t bootindex, DeviceState *dev,
1204 const char *suffix)
1205{
1206 FWBootEntry *node, *i;
1207
1208 if (bootindex < 0) {
1209 return;
1210 }
1211
1212 assert(dev != NULL || suffix != NULL);
1213
Anthony Liguori7267c092011-08-20 22:09:37 -05001214 node = g_malloc0(sizeof(FWBootEntry));
Gleb Natapov1ca4d092010-12-08 13:35:05 +02001215 node->bootindex = bootindex;
Anthony Liguori7267c092011-08-20 22:09:37 -05001216 node->suffix = suffix ? g_strdup(suffix) : NULL;
Gleb Natapov1ca4d092010-12-08 13:35:05 +02001217 node->dev = dev;
1218
1219 QTAILQ_FOREACH(i, &fw_boot_order, link) {
1220 if (i->bootindex == bootindex) {
1221 fprintf(stderr, "Two devices with same boot index %d\n", bootindex);
1222 exit(1);
1223 } else if (i->bootindex < bootindex) {
1224 continue;
1225 }
1226 QTAILQ_INSERT_BEFORE(i, node, link);
1227 return;
1228 }
1229 QTAILQ_INSERT_TAIL(&fw_boot_order, node, link);
1230}
1231
Gleb Natapov962630f2010-12-08 13:35:09 +02001232/*
1233 * This function returns null terminated string that consist of new line
Brad Hards71785ab2011-04-23 21:50:06 +10001234 * separated device paths.
Gleb Natapov962630f2010-12-08 13:35:09 +02001235 *
1236 * memory pointed by "size" is assigned total length of the array in bytes
1237 *
1238 */
Markus Armbruster0e7a7592013-01-16 14:50:29 +01001239char *get_boot_devices_list(size_t *size)
Gleb Natapov962630f2010-12-08 13:35:09 +02001240{
1241 FWBootEntry *i;
Markus Armbruster0e7a7592013-01-16 14:50:29 +01001242 size_t total = 0;
Gleb Natapov962630f2010-12-08 13:35:09 +02001243 char *list = NULL;
1244
1245 QTAILQ_FOREACH(i, &fw_boot_order, link) {
1246 char *devpath = NULL, *bootpath;
Markus Armbruster0e7a7592013-01-16 14:50:29 +01001247 size_t len;
Gleb Natapov962630f2010-12-08 13:35:09 +02001248
1249 if (i->dev) {
1250 devpath = qdev_get_fw_dev_path(i->dev);
1251 assert(devpath);
1252 }
1253
1254 if (i->suffix && devpath) {
Blue Swirl4fd37a92010-12-19 14:05:43 +00001255 size_t bootpathlen = strlen(devpath) + strlen(i->suffix) + 1;
1256
Anthony Liguori7267c092011-08-20 22:09:37 -05001257 bootpath = g_malloc(bootpathlen);
Blue Swirl4fd37a92010-12-19 14:05:43 +00001258 snprintf(bootpath, bootpathlen, "%s%s", devpath, i->suffix);
Anthony Liguori7267c092011-08-20 22:09:37 -05001259 g_free(devpath);
Gleb Natapov962630f2010-12-08 13:35:09 +02001260 } else if (devpath) {
1261 bootpath = devpath;
1262 } else {
Markus Armbruster1bf6ccd2011-11-08 10:58:00 +01001263 assert(i->suffix);
Anthony Liguori7267c092011-08-20 22:09:37 -05001264 bootpath = g_strdup(i->suffix);
Gleb Natapov962630f2010-12-08 13:35:09 +02001265 }
1266
1267 if (total) {
1268 list[total-1] = '\n';
1269 }
1270 len = strlen(bootpath) + 1;
Anthony Liguori7267c092011-08-20 22:09:37 -05001271 list = g_realloc(list, total + len);
Gleb Natapov962630f2010-12-08 13:35:09 +02001272 memcpy(&list[total], bootpath, len);
1273 total += len;
Anthony Liguori7267c092011-08-20 22:09:37 -05001274 g_free(bootpath);
Gleb Natapov962630f2010-12-08 13:35:09 +02001275 }
1276
1277 *size = total;
1278
Amos Kongc8a6ae82013-03-19 14:23:27 +08001279 if (boot_strict && *size > 0) {
1280 list[total-1] = '\n';
1281 list = g_realloc(list, total + 4);
1282 memcpy(&list[total], "HALT", 4);
1283 *size = total + 4;
1284 }
Gleb Natapov962630f2010-12-08 13:35:09 +02001285 return list;
1286}
1287
Eduardo Habkost845e5bf2013-02-04 16:27:51 -02001288static void numa_node_parse_cpus(int nodenr, const char *cpus)
1289{
1290 char *endptr;
1291 unsigned long long value, endvalue;
1292
Eduardo Habkostc881e202013-02-04 16:27:52 -02001293 /* Empty CPU range strings will be considered valid, they will simply
1294 * not set any bit in the CPU bitmap.
1295 */
1296 if (!*cpus) {
1297 return;
Eduardo Habkost845e5bf2013-02-04 16:27:51 -02001298 }
1299
Eduardo Habkostc881e202013-02-04 16:27:52 -02001300 if (parse_uint(cpus, &value, &endptr, 10) < 0) {
1301 goto error;
1302 }
1303 if (*endptr == '-') {
1304 if (parse_uint_full(endptr + 1, &endvalue, 10) < 0) {
1305 goto error;
1306 }
1307 } else if (*endptr == '\0') {
1308 endvalue = value;
1309 } else {
1310 goto error;
1311 }
1312
1313 if (endvalue >= MAX_CPUMASK_BITS) {
Eduardo Habkost845e5bf2013-02-04 16:27:51 -02001314 endvalue = MAX_CPUMASK_BITS - 1;
1315 fprintf(stderr,
Eduardo Habkostc881e202013-02-04 16:27:52 -02001316 "qemu: NUMA: A max of %d VCPUs are supported\n",
Eduardo Habkost845e5bf2013-02-04 16:27:51 -02001317 MAX_CPUMASK_BITS);
1318 }
1319
Eduardo Habkostc881e202013-02-04 16:27:52 -02001320 if (endvalue < value) {
1321 goto error;
1322 }
1323
Eduardo Habkost845e5bf2013-02-04 16:27:51 -02001324 bitmap_set(node_cpumask[nodenr], value, endvalue-value+1);
Eduardo Habkostc881e202013-02-04 16:27:52 -02001325 return;
1326
1327error:
1328 fprintf(stderr, "qemu: Invalid NUMA CPU range: %s\n", cpus);
1329 exit(1);
Eduardo Habkost845e5bf2013-02-04 16:27:51 -02001330}
1331
aliguori268a3622009-04-21 22:30:27 +00001332static void numa_add(const char *optarg)
1333{
1334 char option[128];
1335 char *endptr;
Eduardo Habkoste4ce85b2013-02-04 16:27:49 -02001336 unsigned long long nodenr;
aliguori268a3622009-04-21 22:30:27 +00001337
Eduardo Habkost8f302cb2013-02-04 16:27:46 -02001338 optarg = get_opt_name(option, 128, optarg, ',');
1339 if (*optarg == ',') {
1340 optarg++;
1341 }
aliguori268a3622009-04-21 22:30:27 +00001342 if (!strcmp(option, "node")) {
Eduardo Habkostca4c6d32013-02-04 16:27:48 -02001343
1344 if (nb_numa_nodes >= MAX_NODES) {
1345 fprintf(stderr, "qemu: too many NUMA nodes\n");
1346 exit(1);
1347 }
1348
aliguori268a3622009-04-21 22:30:27 +00001349 if (get_param_value(option, 128, "nodeid", optarg) == 0) {
1350 nodenr = nb_numa_nodes;
1351 } else {
Eduardo Habkost5f139962013-02-04 16:27:50 -02001352 if (parse_uint_full(option, &nodenr, 10) < 0) {
1353 fprintf(stderr, "qemu: Invalid NUMA nodeid: %s\n", option);
1354 exit(1);
1355 }
aliguori268a3622009-04-21 22:30:27 +00001356 }
1357
Eduardo Habkoste4ce85b2013-02-04 16:27:49 -02001358 if (nodenr >= MAX_NODES) {
1359 fprintf(stderr, "qemu: invalid NUMA nodeid: %llu\n", nodenr);
1360 exit(1);
1361 }
1362
aliguori268a3622009-04-21 22:30:27 +00001363 if (get_param_value(option, 128, "mem", optarg) == 0) {
1364 node_mem[nodenr] = 0;
1365 } else {
Jes Sorensen70b4f4b2011-01-05 11:41:02 +01001366 int64_t sval;
Markus Armbrusterc03417b2011-11-22 09:46:02 +01001367 sval = strtosz(option, &endptr);
1368 if (sval < 0 || *endptr) {
Jes Sorensen9f9b17a2010-10-21 17:15:46 +02001369 fprintf(stderr, "qemu: invalid numa mem size: %s\n", optarg);
1370 exit(1);
aliguori268a3622009-04-21 22:30:27 +00001371 }
Jes Sorensen9f9b17a2010-10-21 17:15:46 +02001372 node_mem[nodenr] = sval;
aliguori268a3622009-04-21 22:30:27 +00001373 }
Chegu Vinodee785fe2012-07-16 21:31:30 -07001374 if (get_param_value(option, 128, "cpus", optarg) != 0) {
Eduardo Habkost845e5bf2013-02-04 16:27:51 -02001375 numa_node_parse_cpus(nodenr, option);
aliguori268a3622009-04-21 22:30:27 +00001376 }
1377 nb_numa_nodes++;
Eduardo Habkost12e53a92013-02-04 16:27:47 -02001378 } else {
1379 fprintf(stderr, "Invalid -numa option: %s\n", option);
1380 exit(1);
aliguori268a3622009-04-21 22:30:27 +00001381 }
aliguori268a3622009-04-21 22:30:27 +00001382}
1383
Andre Przywaradc6b1c02009-08-19 15:42:40 +02001384static void smp_parse(const char *optarg)
1385{
1386 int smp, sockets = 0, threads = 0, cores = 0;
1387 char *endptr;
1388 char option[128];
1389
1390 smp = strtoul(optarg, &endptr, 10);
1391 if (endptr != optarg) {
1392 if (*endptr == ',') {
1393 endptr++;
1394 }
1395 }
1396 if (get_param_value(option, 128, "sockets", endptr) != 0)
1397 sockets = strtoull(option, NULL, 10);
1398 if (get_param_value(option, 128, "cores", endptr) != 0)
1399 cores = strtoull(option, NULL, 10);
1400 if (get_param_value(option, 128, "threads", endptr) != 0)
1401 threads = strtoull(option, NULL, 10);
1402 if (get_param_value(option, 128, "maxcpus", endptr) != 0)
1403 max_cpus = strtoull(option, NULL, 10);
1404
1405 /* compute missing values, prefer sockets over cores over threads */
1406 if (smp == 0 || sockets == 0) {
1407 sockets = sockets > 0 ? sockets : 1;
1408 cores = cores > 0 ? cores : 1;
1409 threads = threads > 0 ? threads : 1;
1410 if (smp == 0) {
1411 smp = cores * threads * sockets;
Andre Przywaradc6b1c02009-08-19 15:42:40 +02001412 }
1413 } else {
1414 if (cores == 0) {
1415 threads = threads > 0 ? threads : 1;
1416 cores = smp / (sockets * threads);
1417 } else {
Joel Schoppdca98162010-07-21 15:05:17 -05001418 threads = smp / (cores * sockets);
Andre Przywaradc6b1c02009-08-19 15:42:40 +02001419 }
1420 }
1421 smp_cpus = smp;
1422 smp_cores = cores > 0 ? cores : 1;
1423 smp_threads = threads > 0 ? threads : 1;
1424 if (max_cpus == 0)
1425 max_cpus = smp_cpus;
1426}
1427
bellard330d0412003-07-26 18:11:40 +00001428/***********************************************************/
bellarda594cfb2005-11-06 16:13:29 +00001429/* USB devices */
1430
Markus Armbrusterfb080002010-05-28 15:38:44 +02001431static int usb_device_add(const char *devname)
bellarda594cfb2005-11-06 16:13:29 +00001432{
1433 const char *p;
Gerd Hoffmanna5d2f722009-08-31 14:24:00 +02001434 USBDevice *dev = NULL;
bellarda594cfb2005-11-06 16:13:29 +00001435
zhlcindy@gmail.com094b2872012-09-02 19:25:28 +00001436 if (!usb_enabled(false)) {
bellarda594cfb2005-11-06 16:13:29 +00001437 return -1;
zhlcindy@gmail.com094b2872012-09-02 19:25:28 +00001438 }
bellarda594cfb2005-11-06 16:13:29 +00001439
Gerd Hoffmann0958b4c2009-10-26 15:56:45 +01001440 /* drivers with .usbdevice_name entry in USBDeviceInfo */
1441 dev = usbdevice_create(devname);
1442 if (dev)
1443 goto done;
1444
Gerd Hoffmanna5d2f722009-08-31 14:24:00 +02001445 /* the other ones */
Gerd Hoffmanne447fc62011-06-01 14:41:59 +02001446#ifndef CONFIG_LINUX
1447 /* only the linux version is qdev-ified, usb-bsd still needs this */
bellarda594cfb2005-11-06 16:13:29 +00001448 if (strstart(devname, "host:", &p)) {
Jan Kiszka37417152012-02-27 15:18:47 +01001449 dev = usb_host_device_open(usb_bus_find(-1), p);
Gerd Hoffmanne447fc62011-06-01 14:41:59 +02001450 } else
1451#endif
1452 if (!strcmp(devname, "bt") || strstart(devname, "bt:", &p)) {
Jan Kiszka37417152012-02-27 15:18:47 +01001453 dev = usb_bt_init(usb_bus_find(-1),
1454 devname[2] ? hci_init(p)
1455 : bt_new_hci(qemu_find_bt_vlan(0)));
bellarda594cfb2005-11-06 16:13:29 +00001456 } else {
1457 return -1;
1458 }
pbrook0d92ed32006-05-21 16:30:15 +00001459 if (!dev)
1460 return -1;
1461
Gerd Hoffmanna5d2f722009-08-31 14:24:00 +02001462done:
bellarda594cfb2005-11-06 16:13:29 +00001463 return 0;
1464}
1465
aliguori1f3870a2008-08-21 19:27:48 +00001466static int usb_device_del(const char *devname)
1467{
1468 int bus_num, addr;
1469 const char *p;
1470
Gerd Hoffmann1a3973b2012-11-30 13:02:47 +01001471 if (strstart(devname, "host:", &p)) {
1472 return -1;
1473 }
aliguori5d0c5752008-09-14 01:07:41 +00001474
zhlcindy@gmail.com094b2872012-09-02 19:25:28 +00001475 if (!usb_enabled(false)) {
aliguori1f3870a2008-08-21 19:27:48 +00001476 return -1;
zhlcindy@gmail.com094b2872012-09-02 19:25:28 +00001477 }
aliguori1f3870a2008-08-21 19:27:48 +00001478
1479 p = strchr(devname, '.');
1480 if (!p)
1481 return -1;
1482 bus_num = strtoul(devname, NULL, 0);
1483 addr = strtoul(p + 1, NULL, 0);
1484
Gerd Hoffmanna5d2f722009-08-31 14:24:00 +02001485 return usb_device_delete_addr(bus_num, addr);
aliguori1f3870a2008-08-21 19:27:48 +00001486}
1487
Gerd Hoffmannbd3c9482009-07-15 13:59:26 +02001488static int usb_parse(const char *cmdline)
1489{
Scott Tsai59d1c1c2009-12-23 04:30:18 +08001490 int r;
Markus Armbrusterfb080002010-05-28 15:38:44 +02001491 r = usb_device_add(cmdline);
Scott Tsai59d1c1c2009-12-23 04:30:18 +08001492 if (r < 0) {
1493 fprintf(stderr, "qemu: could not add USB device '%s'\n", cmdline);
1494 }
1495 return r;
Gerd Hoffmannbd3c9482009-07-15 13:59:26 +02001496}
1497
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001498void do_usb_add(Monitor *mon, const QDict *qdict)
bellarda594cfb2005-11-06 16:13:29 +00001499{
Scott Tsai59d1c1c2009-12-23 04:30:18 +08001500 const char *devname = qdict_get_str(qdict, "devname");
Markus Armbrusterfb080002010-05-28 15:38:44 +02001501 if (usb_device_add(devname) < 0) {
Markus Armbruster1ecda022010-02-18 17:25:24 +01001502 error_report("could not add USB device '%s'", devname);
Scott Tsai59d1c1c2009-12-23 04:30:18 +08001503 }
bellarda594cfb2005-11-06 16:13:29 +00001504}
1505
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001506void do_usb_del(Monitor *mon, const QDict *qdict)
bellarda594cfb2005-11-06 16:13:29 +00001507{
Scott Tsai59d1c1c2009-12-23 04:30:18 +08001508 const char *devname = qdict_get_str(qdict, "devname");
1509 if (usb_device_del(devname) < 0) {
Markus Armbruster1ecda022010-02-18 17:25:24 +01001510 error_report("could not delete USB device '%s'", devname);
Scott Tsai59d1c1c2009-12-23 04:30:18 +08001511 }
bellarda594cfb2005-11-06 16:13:29 +00001512}
1513
bellardf7cce892004-12-08 22:21:25 +00001514/***********************************************************/
balrog201a51f2007-04-30 00:51:09 +00001515/* PCMCIA/Cardbus */
1516
1517static struct pcmcia_socket_entry_s {
Paul Brookbc24a222009-05-10 01:44:56 +01001518 PCMCIASocket *socket;
balrog201a51f2007-04-30 00:51:09 +00001519 struct pcmcia_socket_entry_s *next;
1520} *pcmcia_sockets = 0;
1521
Paul Brookbc24a222009-05-10 01:44:56 +01001522void pcmcia_socket_register(PCMCIASocket *socket)
balrog201a51f2007-04-30 00:51:09 +00001523{
1524 struct pcmcia_socket_entry_s *entry;
1525
Anthony Liguori7267c092011-08-20 22:09:37 -05001526 entry = g_malloc(sizeof(struct pcmcia_socket_entry_s));
balrog201a51f2007-04-30 00:51:09 +00001527 entry->socket = socket;
1528 entry->next = pcmcia_sockets;
1529 pcmcia_sockets = entry;
1530}
1531
Paul Brookbc24a222009-05-10 01:44:56 +01001532void pcmcia_socket_unregister(PCMCIASocket *socket)
balrog201a51f2007-04-30 00:51:09 +00001533{
1534 struct pcmcia_socket_entry_s *entry, **ptr;
1535
1536 ptr = &pcmcia_sockets;
1537 for (entry = *ptr; entry; ptr = &entry->next, entry = *ptr)
1538 if (entry->socket == socket) {
1539 *ptr = entry->next;
Anthony Liguori7267c092011-08-20 22:09:37 -05001540 g_free(entry);
balrog201a51f2007-04-30 00:51:09 +00001541 }
1542}
1543
Wenchao Xia84f2d0e2013-01-14 14:06:25 +08001544void pcmcia_info(Monitor *mon, const QDict *qdict)
balrog201a51f2007-04-30 00:51:09 +00001545{
1546 struct pcmcia_socket_entry_s *iter;
aliguori376253e2009-03-05 23:01:23 +00001547
balrog201a51f2007-04-30 00:51:09 +00001548 if (!pcmcia_sockets)
aliguori376253e2009-03-05 23:01:23 +00001549 monitor_printf(mon, "No PCMCIA sockets\n");
balrog201a51f2007-04-30 00:51:09 +00001550
1551 for (iter = pcmcia_sockets; iter; iter = iter->next)
aliguori376253e2009-03-05 23:01:23 +00001552 monitor_printf(mon, "%s: %s\n", iter->socket->slot_string,
1553 iter->socket->attached ? iter->socket->card_string :
1554 "Empty");
balrog201a51f2007-04-30 00:51:09 +00001555}
1556
1557/***********************************************************/
bellardcc1daa42005-06-05 14:49:17 +00001558/* machine registration */
1559
blueswir1bdaf78e2008-10-04 07:24:27 +00001560static QEMUMachine *first_machine = NULL;
aliguori6f338c32009-02-11 15:21:54 +00001561QEMUMachine *current_machine = NULL;
bellardcc1daa42005-06-05 14:49:17 +00001562
1563int qemu_register_machine(QEMUMachine *m)
1564{
1565 QEMUMachine **pm;
1566 pm = &first_machine;
1567 while (*pm != NULL)
1568 pm = &(*pm)->next;
1569 m->next = NULL;
1570 *pm = m;
1571 return 0;
1572}
1573
pbrook9596ebb2007-11-18 01:44:38 +00001574static QEMUMachine *find_machine(const char *name)
bellardcc1daa42005-06-05 14:49:17 +00001575{
1576 QEMUMachine *m;
1577
1578 for(m = first_machine; m != NULL; m = m->next) {
1579 if (!strcmp(m->name, name))
1580 return m;
Mark McLoughlin3f6599e2009-07-22 10:02:50 +01001581 if (m->alias && !strcmp(m->alias, name))
1582 return m;
bellardcc1daa42005-06-05 14:49:17 +00001583 }
1584 return NULL;
1585}
1586
Jordan Justen2c8cffa2012-02-21 23:18:50 -08001587QEMUMachine *find_default_machine(void)
Anthony Liguori0c257432009-05-21 20:41:01 -05001588{
1589 QEMUMachine *m;
1590
1591 for(m = first_machine; m != NULL; m = m->next) {
1592 if (m->is_default) {
1593 return m;
1594 }
1595 }
1596 return NULL;
1597}
1598
Anthony Liguori01d3c802012-08-10 11:04:11 -05001599MachineInfoList *qmp_query_machines(Error **errp)
1600{
1601 MachineInfoList *mach_list = NULL;
1602 QEMUMachine *m;
1603
1604 for (m = first_machine; m; m = m->next) {
1605 MachineInfoList *entry;
1606 MachineInfo *info;
1607
1608 info = g_malloc0(sizeof(*info));
1609 if (m->is_default) {
1610 info->has_is_default = true;
1611 info->is_default = true;
1612 }
1613
1614 if (m->alias) {
1615 info->has_alias = true;
1616 info->alias = g_strdup(m->alias);
1617 }
1618
1619 info->name = g_strdup(m->name);
1620
1621 entry = g_malloc0(sizeof(*entry));
1622 entry->value = info;
1623 entry->next = mach_list;
1624 mach_list = entry;
1625 }
1626
1627 return mach_list;
1628}
1629
bellardcc1daa42005-06-05 14:49:17 +00001630/***********************************************************/
bellard8a7ddc32004-03-31 19:00:16 +00001631/* main execution loop */
1632
pbrook9596ebb2007-11-18 01:44:38 +00001633static void gui_update(void *opaque)
bellard8a7ddc32004-03-31 19:00:16 +00001634{
aliguori7d957bd2009-01-15 22:14:11 +00001635 uint64_t interval = GUI_REFRESH_INTERVAL;
ths740733b2007-06-08 01:57:56 +00001636 DisplayState *ds = opaque;
Gerd Hoffmann87e487a2010-06-04 11:46:35 +02001637 DisplayChangeListener *dcl;
aliguori7d957bd2009-01-15 22:14:11 +00001638
1639 dpy_refresh(ds);
1640
Gerd Hoffmann87e487a2010-06-04 11:46:35 +02001641 QLIST_FOREACH(dcl, &ds->listeners, next) {
aliguori7d957bd2009-01-15 22:14:11 +00001642 if (dcl->gui_timer_interval &&
1643 dcl->gui_timer_interval < interval)
1644 interval = dcl->gui_timer_interval;
aliguori7d957bd2009-01-15 22:14:11 +00001645 }
Paolo Bonzini7bd427d2011-03-11 16:47:48 +01001646 qemu_mod_timer(ds->gui_timer, interval + qemu_get_clock_ms(rt_clock));
bellard8a7ddc32004-03-31 19:00:16 +00001647}
1648
Gerd Hoffmann35c9e0a2010-06-04 11:51:31 +02001649void gui_setup_refresh(DisplayState *ds)
1650{
1651 DisplayChangeListener *dcl;
1652 bool need_timer = false;
Gerd Hoffmanna93a4a22012-09-28 15:02:08 +02001653 bool have_gfx = false;
1654 bool have_text = false;
Gerd Hoffmann35c9e0a2010-06-04 11:51:31 +02001655
1656 QLIST_FOREACH(dcl, &ds->listeners, next) {
Gerd Hoffmann7c20b4a2012-11-13 14:51:41 +01001657 if (dcl->ops->dpy_refresh != NULL) {
Gerd Hoffmann35c9e0a2010-06-04 11:51:31 +02001658 need_timer = true;
Gerd Hoffmanna93a4a22012-09-28 15:02:08 +02001659 }
Gerd Hoffmann7c20b4a2012-11-13 14:51:41 +01001660 if (dcl->ops->dpy_gfx_update != NULL) {
Gerd Hoffmanna93a4a22012-09-28 15:02:08 +02001661 have_gfx = true;
1662 }
Gerd Hoffmann7c20b4a2012-11-13 14:51:41 +01001663 if (dcl->ops->dpy_text_update != NULL) {
Gerd Hoffmanna93a4a22012-09-28 15:02:08 +02001664 have_text = true;
Gerd Hoffmann35c9e0a2010-06-04 11:51:31 +02001665 }
1666 }
1667
1668 if (need_timer && ds->gui_timer == NULL) {
1669 ds->gui_timer = qemu_new_timer_ms(rt_clock, gui_update, ds);
1670 qemu_mod_timer(ds->gui_timer, qemu_get_clock_ms(rt_clock));
1671 }
1672 if (!need_timer && ds->gui_timer != NULL) {
1673 qemu_del_timer(ds->gui_timer);
1674 qemu_free_timer(ds->gui_timer);
1675 ds->gui_timer = NULL;
1676 }
Gerd Hoffmanna93a4a22012-09-28 15:02:08 +02001677
1678 ds->have_gfx = have_gfx;
1679 ds->have_text = have_text;
Gerd Hoffmann35c9e0a2010-06-04 11:51:31 +02001680}
1681
bellard0bd48852005-11-11 00:00:47 +00001682struct vm_change_state_entry {
1683 VMChangeStateHandler *cb;
1684 void *opaque;
Blue Swirl72cf2d42009-09-12 07:36:22 +00001685 QLIST_ENTRY (vm_change_state_entry) entries;
bellard0bd48852005-11-11 00:00:47 +00001686};
1687
Blue Swirl72cf2d42009-09-12 07:36:22 +00001688static QLIST_HEAD(vm_change_state_head, vm_change_state_entry) vm_change_state_head;
bellard0bd48852005-11-11 00:00:47 +00001689
1690VMChangeStateEntry *qemu_add_vm_change_state_handler(VMChangeStateHandler *cb,
1691 void *opaque)
1692{
1693 VMChangeStateEntry *e;
1694
Anthony Liguori7267c092011-08-20 22:09:37 -05001695 e = g_malloc0(sizeof (*e));
bellard0bd48852005-11-11 00:00:47 +00001696
1697 e->cb = cb;
1698 e->opaque = opaque;
Blue Swirl72cf2d42009-09-12 07:36:22 +00001699 QLIST_INSERT_HEAD(&vm_change_state_head, e, entries);
bellard0bd48852005-11-11 00:00:47 +00001700 return e;
1701}
1702
1703void qemu_del_vm_change_state_handler(VMChangeStateEntry *e)
1704{
Blue Swirl72cf2d42009-09-12 07:36:22 +00001705 QLIST_REMOVE (e, entries);
Anthony Liguori7267c092011-08-20 22:09:37 -05001706 g_free (e);
bellard0bd48852005-11-11 00:00:47 +00001707}
1708
Luiz Capitulino1dfb4dd2011-07-29 14:26:33 -03001709void vm_state_notify(int running, RunState state)
bellard0bd48852005-11-11 00:00:47 +00001710{
1711 VMChangeStateEntry *e;
1712
Luiz Capitulino1dfb4dd2011-07-29 14:26:33 -03001713 trace_vm_state_notify(running, state);
Stefan Hajnoczi94b0b5f2010-11-16 12:20:25 +00001714
bellard0bd48852005-11-11 00:00:47 +00001715 for (e = vm_change_state_head.lh_first; e; e = e->entries.le_next) {
Luiz Capitulino1dfb4dd2011-07-29 14:26:33 -03001716 e->cb(e->opaque, running, state);
bellard0bd48852005-11-11 00:00:47 +00001717 }
1718}
1719
bellard8a7ddc32004-03-31 19:00:16 +00001720void vm_start(void)
1721{
Luiz Capitulino13548692011-07-29 15:36:43 -03001722 if (!runstate_is_running()) {
bellard8a7ddc32004-03-31 19:00:16 +00001723 cpu_enable_ticks();
Luiz Capitulino0461d5a2011-09-30 14:45:27 -03001724 runstate_set(RUN_STATE_RUNNING);
1725 vm_state_notify(1, RUN_STATE_RUNNING);
aliguorid6dc3d42009-04-24 18:04:07 +00001726 resume_all_vcpus();
Luiz Capitulino6ed2c482010-04-27 20:35:59 -03001727 monitor_protocol_event(QEVENT_RESUME, NULL);
bellard8a7ddc32004-03-31 19:00:16 +00001728 }
1729}
1730
bellardbb0c6722004-06-20 12:37:32 +00001731/* reset/shutdown handler */
1732
1733typedef struct QEMUResetEntry {
Blue Swirl72cf2d42009-09-12 07:36:22 +00001734 QTAILQ_ENTRY(QEMUResetEntry) entry;
bellardbb0c6722004-06-20 12:37:32 +00001735 QEMUResetHandler *func;
1736 void *opaque;
bellardbb0c6722004-06-20 12:37:32 +00001737} QEMUResetEntry;
1738
Blue Swirl72cf2d42009-09-12 07:36:22 +00001739static QTAILQ_HEAD(reset_handlers, QEMUResetEntry) reset_handlers =
1740 QTAILQ_HEAD_INITIALIZER(reset_handlers);
bellardbb0c6722004-06-20 12:37:32 +00001741static int reset_requested;
Gleb Natapovf64622c2011-03-15 13:56:04 +02001742static int shutdown_requested, shutdown_signal = -1;
1743static pid_t shutdown_pid;
bellard34751872005-07-02 14:31:34 +00001744static int powerdown_requested;
Jan Kiszka8cf71712011-02-07 12:19:16 +01001745static int debug_requested;
Gerd Hoffmann95b363b2012-02-23 13:45:19 +01001746static int suspend_requested;
Luiz Capitulino14058192012-08-08 17:29:17 -03001747static int wakeup_requested;
Igor Mammedova9552c82012-09-05 23:06:21 +02001748static NotifierList powerdown_notifiers =
1749 NOTIFIER_LIST_INITIALIZER(powerdown_notifiers);
Gerd Hoffmann95b363b2012-02-23 13:45:19 +01001750static NotifierList suspend_notifiers =
1751 NOTIFIER_LIST_INITIALIZER(suspend_notifiers);
1752static NotifierList wakeup_notifiers =
1753 NOTIFIER_LIST_INITIALIZER(wakeup_notifiers);
1754static uint32_t wakeup_reason_mask = ~0;
Luiz Capitulino0461d5a2011-09-30 14:45:27 -03001755static RunState vmstop_requested = RUN_STATE_MAX;
bellardbb0c6722004-06-20 12:37:32 +00001756
Anthony PERARD1291eb32010-07-22 15:52:48 +01001757int qemu_shutdown_requested_get(void)
1758{
1759 return shutdown_requested;
1760}
1761
1762int qemu_reset_requested_get(void)
1763{
1764 return reset_requested;
1765}
1766
Blue Swirl4fdcac02012-10-28 11:04:47 +00001767static int qemu_shutdown_requested(void)
aurel32cf7a2fe2008-03-18 06:53:05 +00001768{
1769 int r = shutdown_requested;
1770 shutdown_requested = 0;
1771 return r;
1772}
1773
Blue Swirl4fdcac02012-10-28 11:04:47 +00001774static void qemu_kill_report(void)
Gleb Natapovf64622c2011-03-15 13:56:04 +02001775{
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +02001776 if (!qtest_enabled() && shutdown_signal != -1) {
Peter Maydellf1d3fb02011-03-30 22:03:38 +01001777 fprintf(stderr, "qemu: terminating on signal %d", shutdown_signal);
1778 if (shutdown_pid == 0) {
1779 /* This happens for eg ^C at the terminal, so it's worth
1780 * avoiding printing an odd message in that case.
1781 */
1782 fputc('\n', stderr);
1783 } else {
Andreas Färber953ffe02011-06-02 19:58:06 +02001784 fprintf(stderr, " from pid " FMT_pid "\n", shutdown_pid);
Peter Maydellf1d3fb02011-03-30 22:03:38 +01001785 }
Gleb Natapovf64622c2011-03-15 13:56:04 +02001786 shutdown_signal = -1;
1787 }
1788}
1789
Blue Swirl4fdcac02012-10-28 11:04:47 +00001790static int qemu_reset_requested(void)
aurel32cf7a2fe2008-03-18 06:53:05 +00001791{
1792 int r = reset_requested;
1793 reset_requested = 0;
1794 return r;
1795}
1796
Gerd Hoffmann95b363b2012-02-23 13:45:19 +01001797static int qemu_suspend_requested(void)
1798{
1799 int r = suspend_requested;
1800 suspend_requested = 0;
1801 return r;
1802}
1803
Luiz Capitulino14058192012-08-08 17:29:17 -03001804static int qemu_wakeup_requested(void)
1805{
1806 int r = wakeup_requested;
1807 wakeup_requested = 0;
1808 return r;
1809}
1810
Blue Swirl4fdcac02012-10-28 11:04:47 +00001811static int qemu_powerdown_requested(void)
aurel32cf7a2fe2008-03-18 06:53:05 +00001812{
1813 int r = powerdown_requested;
1814 powerdown_requested = 0;
1815 return r;
1816}
1817
aliguorie5689022009-04-24 18:03:54 +00001818static int qemu_debug_requested(void)
1819{
1820 int r = debug_requested;
1821 debug_requested = 0;
1822 return r;
1823}
1824
Luiz Capitulino0461d5a2011-09-30 14:45:27 -03001825/* We use RUN_STATE_MAX but any invalid value will do */
Luiz Capitulino0d45b702011-09-30 14:27:11 -03001826static bool qemu_vmstop_requested(RunState *r)
aliguori6e29f5d2009-04-24 18:04:02 +00001827{
Luiz Capitulino0461d5a2011-09-30 14:45:27 -03001828 if (vmstop_requested < RUN_STATE_MAX) {
Luiz Capitulino0d45b702011-09-30 14:27:11 -03001829 *r = vmstop_requested;
Luiz Capitulino0461d5a2011-09-30 14:45:27 -03001830 vmstop_requested = RUN_STATE_MAX;
Luiz Capitulino0d45b702011-09-30 14:27:11 -03001831 return true;
1832 }
1833
1834 return false;
aliguori6e29f5d2009-04-24 18:04:02 +00001835}
1836
Jan Kiszkaa08d4362009-06-27 09:25:07 +02001837void qemu_register_reset(QEMUResetHandler *func, void *opaque)
bellardbb0c6722004-06-20 12:37:32 +00001838{
Anthony Liguori7267c092011-08-20 22:09:37 -05001839 QEMUResetEntry *re = g_malloc0(sizeof(QEMUResetEntry));
bellardbb0c6722004-06-20 12:37:32 +00001840
bellardbb0c6722004-06-20 12:37:32 +00001841 re->func = func;
1842 re->opaque = opaque;
Blue Swirl72cf2d42009-09-12 07:36:22 +00001843 QTAILQ_INSERT_TAIL(&reset_handlers, re, entry);
bellardbb0c6722004-06-20 12:37:32 +00001844}
1845
Jan Kiszkadda9b292009-07-02 00:19:02 +02001846void qemu_unregister_reset(QEMUResetHandler *func, void *opaque)
bellardbb0c6722004-06-20 12:37:32 +00001847{
1848 QEMUResetEntry *re;
1849
Blue Swirl72cf2d42009-09-12 07:36:22 +00001850 QTAILQ_FOREACH(re, &reset_handlers, entry) {
Jan Kiszkadda9b292009-07-02 00:19:02 +02001851 if (re->func == func && re->opaque == opaque) {
Blue Swirl72cf2d42009-09-12 07:36:22 +00001852 QTAILQ_REMOVE(&reset_handlers, re, entry);
Anthony Liguori7267c092011-08-20 22:09:37 -05001853 g_free(re);
Jan Kiszkadda9b292009-07-02 00:19:02 +02001854 return;
1855 }
1856 }
1857}
1858
David Gibsonbe522022012-08-07 16:41:51 +10001859void qemu_devices_reset(void)
Jan Kiszkadda9b292009-07-02 00:19:02 +02001860{
1861 QEMUResetEntry *re, *nre;
1862
1863 /* reset all devices */
Blue Swirl72cf2d42009-09-12 07:36:22 +00001864 QTAILQ_FOREACH_SAFE(re, &reset_handlers, entry, nre) {
bellardbb0c6722004-06-20 12:37:32 +00001865 re->func(re->opaque);
1866 }
David Gibsonbe522022012-08-07 16:41:51 +10001867}
1868
1869void qemu_system_reset(bool report)
1870{
1871 if (current_machine && current_machine->reset) {
1872 current_machine->reset();
1873 } else {
1874 qemu_devices_reset();
1875 }
Jan Kiszkae063eb12011-06-14 18:29:43 +02001876 if (report) {
1877 monitor_protocol_event(QEVENT_RESET, NULL);
1878 }
Jan Kiszkaea375f92010-03-01 19:10:30 +01001879 cpu_synchronize_all_post_reset();
bellardbb0c6722004-06-20 12:37:32 +00001880}
1881
1882void qemu_system_reset_request(void)
1883{
bellardd1beab82006-10-02 19:44:22 +00001884 if (no_reboot) {
1885 shutdown_requested = 1;
1886 } else {
1887 reset_requested = 1;
1888 }
Jan Kiszkab4a3d962011-02-01 22:15:43 +01001889 cpu_stop_current();
aliguorid9f75a42009-04-24 18:03:11 +00001890 qemu_notify_event();
bellardbb0c6722004-06-20 12:37:32 +00001891}
1892
Gerd Hoffmann95b363b2012-02-23 13:45:19 +01001893static void qemu_system_suspend(void)
1894{
1895 pause_all_vcpus();
1896 notifier_list_notify(&suspend_notifiers, NULL);
Luiz Capitulinoad02b962012-04-27 13:33:36 -03001897 runstate_set(RUN_STATE_SUSPENDED);
Gerd Hoffmann53370b72012-02-23 13:45:26 +01001898 monitor_protocol_event(QEVENT_SUSPEND, NULL);
Gerd Hoffmann95b363b2012-02-23 13:45:19 +01001899}
1900
1901void qemu_system_suspend_request(void)
1902{
Luiz Capitulino9abc62f2012-04-27 14:31:12 -03001903 if (runstate_check(RUN_STATE_SUSPENDED)) {
Gerd Hoffmann95b363b2012-02-23 13:45:19 +01001904 return;
1905 }
1906 suspend_requested = 1;
1907 cpu_stop_current();
1908 qemu_notify_event();
1909}
1910
1911void qemu_register_suspend_notifier(Notifier *notifier)
1912{
1913 notifier_list_add(&suspend_notifiers, notifier);
1914}
1915
1916void qemu_system_wakeup_request(WakeupReason reason)
1917{
Luiz Capitulino9abc62f2012-04-27 14:31:12 -03001918 if (!runstate_check(RUN_STATE_SUSPENDED)) {
Gerd Hoffmann95b363b2012-02-23 13:45:19 +01001919 return;
1920 }
1921 if (!(wakeup_reason_mask & (1 << reason))) {
1922 return;
1923 }
Luiz Capitulinoad02b962012-04-27 13:33:36 -03001924 runstate_set(RUN_STATE_RUNNING);
Gerd Hoffmann95b363b2012-02-23 13:45:19 +01001925 notifier_list_notify(&wakeup_notifiers, &reason);
Luiz Capitulino14058192012-08-08 17:29:17 -03001926 wakeup_requested = 1;
Gerd Hoffmann95b363b2012-02-23 13:45:19 +01001927 qemu_notify_event();
Gerd Hoffmann95b363b2012-02-23 13:45:19 +01001928}
1929
1930void qemu_system_wakeup_enable(WakeupReason reason, bool enabled)
1931{
1932 if (enabled) {
1933 wakeup_reason_mask |= (1 << reason);
1934 } else {
1935 wakeup_reason_mask &= ~(1 << reason);
1936 }
1937}
1938
1939void qemu_register_wakeup_notifier(Notifier *notifier)
1940{
1941 notifier_list_add(&wakeup_notifiers, notifier);
1942}
1943
Gleb Natapovf64622c2011-03-15 13:56:04 +02001944void qemu_system_killed(int signal, pid_t pid)
1945{
1946 shutdown_signal = signal;
1947 shutdown_pid = pid;
Kevin Wolfd9389b92011-09-14 15:38:40 +02001948 no_shutdown = 0;
Gleb Natapovf64622c2011-03-15 13:56:04 +02001949 qemu_system_shutdown_request();
1950}
1951
bellardbb0c6722004-06-20 12:37:32 +00001952void qemu_system_shutdown_request(void)
1953{
1954 shutdown_requested = 1;
aliguorid9f75a42009-04-24 18:03:11 +00001955 qemu_notify_event();
bellardbb0c6722004-06-20 12:37:32 +00001956}
1957
Igor Mammedov013c2f12012-09-05 23:06:25 +02001958static void qemu_system_powerdown(void)
1959{
1960 monitor_protocol_event(QEVENT_POWERDOWN, NULL);
1961 notifier_list_notify(&powerdown_notifiers, NULL);
1962}
1963
bellard34751872005-07-02 14:31:34 +00001964void qemu_system_powerdown_request(void)
1965{
1966 powerdown_requested = 1;
aliguorid9f75a42009-04-24 18:03:11 +00001967 qemu_notify_event();
1968}
1969
Igor Mammedova9552c82012-09-05 23:06:21 +02001970void qemu_register_powerdown_notifier(Notifier *notifier)
1971{
1972 notifier_list_add(&powerdown_notifiers, notifier);
1973}
1974
Jan Kiszka8cf71712011-02-07 12:19:16 +01001975void qemu_system_debug_request(void)
1976{
1977 debug_requested = 1;
Jan Kiszka83f338f2011-02-07 12:19:17 +01001978 qemu_notify_event();
Jan Kiszka8cf71712011-02-07 12:19:16 +01001979}
1980
Luiz Capitulino1dfb4dd2011-07-29 14:26:33 -03001981void qemu_system_vmstop_request(RunState state)
Jan Kiszka8cf71712011-02-07 12:19:16 +01001982{
Luiz Capitulino1dfb4dd2011-07-29 14:26:33 -03001983 vmstop_requested = state;
Jan Kiszka8cf71712011-02-07 12:19:16 +01001984 qemu_notify_event();
1985}
1986
Paolo Bonzini99435902011-09-12 14:03:13 +02001987static bool main_loop_should_exit(void)
1988{
1989 RunState r;
1990 if (qemu_debug_requested()) {
1991 vm_stop(RUN_STATE_DEBUG);
1992 }
Gerd Hoffmann95b363b2012-02-23 13:45:19 +01001993 if (qemu_suspend_requested()) {
1994 qemu_system_suspend();
1995 }
Paolo Bonzini99435902011-09-12 14:03:13 +02001996 if (qemu_shutdown_requested()) {
1997 qemu_kill_report();
1998 monitor_protocol_event(QEVENT_SHUTDOWN, NULL);
1999 if (no_shutdown) {
2000 vm_stop(RUN_STATE_SHUTDOWN);
2001 } else {
2002 return true;
2003 }
2004 }
2005 if (qemu_reset_requested()) {
2006 pause_all_vcpus();
2007 cpu_synchronize_all_states();
2008 qemu_system_reset(VMRESET_REPORT);
2009 resume_all_vcpus();
2010 if (runstate_check(RUN_STATE_INTERNAL_ERROR) ||
2011 runstate_check(RUN_STATE_SHUTDOWN)) {
2012 runstate_set(RUN_STATE_PAUSED);
2013 }
2014 }
Luiz Capitulino14058192012-08-08 17:29:17 -03002015 if (qemu_wakeup_requested()) {
2016 pause_all_vcpus();
2017 cpu_synchronize_all_states();
2018 qemu_system_reset(VMRESET_SILENT);
2019 resume_all_vcpus();
Luiz Capitulino17c86602012-08-09 11:27:30 -03002020 monitor_protocol_event(QEVENT_WAKEUP, NULL);
Luiz Capitulino14058192012-08-08 17:29:17 -03002021 }
Paolo Bonzini99435902011-09-12 14:03:13 +02002022 if (qemu_powerdown_requested()) {
Igor Mammedov013c2f12012-09-05 23:06:25 +02002023 qemu_system_powerdown();
Paolo Bonzini99435902011-09-12 14:03:13 +02002024 }
2025 if (qemu_vmstop_requested(&r)) {
2026 vm_stop(r);
2027 }
2028 return false;
2029}
2030
aliguori43b96852009-04-24 18:03:33 +00002031static void main_loop(void)
2032{
Jan Kiszkac9f711a2011-08-22 17:46:02 +02002033 bool nonblocking;
Paolo Bonzini99435902011-09-12 14:03:13 +02002034 int last_io = 0;
Jan Kiszka8e1b90e2011-02-01 22:15:46 +01002035#ifdef CONFIG_PROFILER
2036 int64_t ti;
2037#endif
Paolo Bonzini99435902011-09-12 14:03:13 +02002038 do {
Jan Kiszkac9f711a2011-08-22 17:46:02 +02002039 nonblocking = !kvm_enabled() && last_io > 0;
aliguori43b96852009-04-24 18:03:33 +00002040#ifdef CONFIG_PROFILER
Jan Kiszka46481d32011-02-01 22:15:47 +01002041 ti = profile_getclock();
aliguori43b96852009-04-24 18:03:33 +00002042#endif
Jan Kiszkac9f711a2011-08-22 17:46:02 +02002043 last_io = main_loop_wait(nonblocking);
aliguori43b96852009-04-24 18:03:33 +00002044#ifdef CONFIG_PROFILER
Jan Kiszka46481d32011-02-01 22:15:47 +01002045 dev_time += profile_getclock() - ti;
aliguori43b96852009-04-24 18:03:33 +00002046#endif
Paolo Bonzini99435902011-09-12 14:03:13 +02002047 } while (!main_loop_should_exit());
bellardb4608c02003-06-27 17:34:32 +00002048}
2049
pbrook9bd7e6d2009-04-07 22:58:45 +00002050static void version(void)
2051{
Thomas Monjalonf75ca1a2010-04-28 14:42:01 +02002052 printf("QEMU emulator version " QEMU_VERSION QEMU_PKGVERSION ", Copyright (c) 2003-2008 Fabrice Bellard\n");
pbrook9bd7e6d2009-04-07 22:58:45 +00002053}
2054
ths15f82202007-06-29 23:26:08 +00002055static void help(int exitcode)
bellard0824d6f2003-06-24 13:42:40 +00002056{
Michael Ellermana3adb7a2011-12-19 17:19:31 +11002057 version();
2058 printf("usage: %s [options] [disk_image]\n\n"
2059 "'disk_image' is a raw hard disk image for IDE hard disk 0\n\n",
2060 error_get_progname());
2061
Michael Ellerman77bd1112011-12-19 17:19:30 +11002062#define QEMU_OPTIONS_GENERATE_HELP
2063#include "qemu-options-wrapper.h"
Michael Ellermana3adb7a2011-12-19 17:19:31 +11002064
2065 printf("\nDuring emulation, the following keys are useful:\n"
malc3f020d72010-02-08 12:04:56 +03002066 "ctrl-alt-f toggle full screen\n"
2067 "ctrl-alt-n switch to virtual console 'n'\n"
2068 "ctrl-alt toggle mouse and keyboard grab\n"
2069 "\n"
Michael Ellermana3adb7a2011-12-19 17:19:31 +11002070 "When using -nographic, press 'ctrl-a h' to get some help.\n");
2071
ths15f82202007-06-29 23:26:08 +00002072 exit(exitcode);
bellard0824d6f2003-06-24 13:42:40 +00002073}
2074
bellardcd6f1162004-05-13 22:02:20 +00002075#define HAS_ARG 0x0001
2076
bellardcd6f1162004-05-13 22:02:20 +00002077typedef struct QEMUOption {
2078 const char *name;
2079 int flags;
2080 int index;
Blue Swirlad960902010-03-29 19:23:52 +00002081 uint32_t arch_mask;
bellardcd6f1162004-05-13 22:02:20 +00002082} QEMUOption;
2083
blueswir1dbed7e42008-10-01 19:38:09 +00002084static const QEMUOption qemu_options[] = {
Blue Swirlad960902010-03-29 19:23:52 +00002085 { "h", 0, QEMU_OPTION_h, QEMU_ARCH_ALL },
Michael Ellerman77bd1112011-12-19 17:19:30 +11002086#define QEMU_OPTIONS_GENERATE_OPTIONS
2087#include "qemu-options-wrapper.h"
bellardcd6f1162004-05-13 22:02:20 +00002088 { NULL },
bellardfc01f7e2003-06-30 10:03:06 +00002089};
Blue Swirla369da52011-09-27 19:15:42 +00002090
2091static bool vga_available(void)
2092{
Aurelien Jarno36b7f272012-09-08 16:01:20 +02002093 return object_class_by_name("VGA") || object_class_by_name("isa-vga");
Blue Swirla369da52011-09-27 19:15:42 +00002094}
2095
2096static bool cirrus_vga_available(void)
2097{
Aurelien Jarno36b7f272012-09-08 16:01:20 +02002098 return object_class_by_name("cirrus-vga")
2099 || object_class_by_name("isa-cirrus-vga");
Blue Swirla369da52011-09-27 19:15:42 +00002100}
2101
2102static bool vmware_vga_available(void)
2103{
Aurelien Jarno36b7f272012-09-08 16:01:20 +02002104 return object_class_by_name("vmware-svga");
Blue Swirla369da52011-09-27 19:15:42 +00002105}
2106
Aurelien Jarno879049a2012-09-08 12:01:06 +02002107static bool qxl_vga_available(void)
2108{
2109 return object_class_by_name("qxl-vga");
2110}
2111
malc3893c122008-09-28 00:42:05 +00002112static void select_vgahw (const char *p)
2113{
2114 const char *opts;
2115
Zachary Amsden86176752009-07-30 00:15:02 -10002116 vga_interface_type = VGA_NONE;
malc3893c122008-09-28 00:42:05 +00002117 if (strstart(p, "std", &opts)) {
Blue Swirla369da52011-09-27 19:15:42 +00002118 if (vga_available()) {
2119 vga_interface_type = VGA_STD;
2120 } else {
2121 fprintf(stderr, "Error: standard VGA not available\n");
2122 exit(0);
2123 }
malc3893c122008-09-28 00:42:05 +00002124 } else if (strstart(p, "cirrus", &opts)) {
Blue Swirla369da52011-09-27 19:15:42 +00002125 if (cirrus_vga_available()) {
2126 vga_interface_type = VGA_CIRRUS;
2127 } else {
2128 fprintf(stderr, "Error: Cirrus VGA not available\n");
2129 exit(0);
2130 }
malc3893c122008-09-28 00:42:05 +00002131 } else if (strstart(p, "vmware", &opts)) {
Blue Swirla369da52011-09-27 19:15:42 +00002132 if (vmware_vga_available()) {
2133 vga_interface_type = VGA_VMWARE;
2134 } else {
2135 fprintf(stderr, "Error: VMWare SVGA not available\n");
2136 exit(0);
2137 }
aliguori94909d92009-04-22 15:19:53 +00002138 } else if (strstart(p, "xenfb", &opts)) {
Zachary Amsden86176752009-07-30 00:15:02 -10002139 vga_interface_type = VGA_XENFB;
Gerd Hoffmanna19cbfb2010-04-27 11:50:11 +02002140 } else if (strstart(p, "qxl", &opts)) {
Aurelien Jarno879049a2012-09-08 12:01:06 +02002141 if (qxl_vga_available()) {
2142 vga_interface_type = VGA_QXL;
2143 } else {
2144 fprintf(stderr, "Error: QXL VGA not available\n");
2145 exit(0);
2146 }
aliguori28b85ed2009-04-22 15:19:48 +00002147 } else if (!strstart(p, "none", &opts)) {
malc3893c122008-09-28 00:42:05 +00002148 invalid_vga:
2149 fprintf(stderr, "Unknown vga type: %s\n", p);
2150 exit(1);
2151 }
malccb5a7aa2008-09-28 00:42:12 +00002152 while (*opts) {
2153 const char *nextopt;
2154
2155 if (strstart(opts, ",retrace=", &nextopt)) {
2156 opts = nextopt;
2157 if (strstart(opts, "dumb", &nextopt))
2158 vga_retrace_method = VGA_RETRACE_DUMB;
2159 else if (strstart(opts, "precise", &nextopt))
2160 vga_retrace_method = VGA_RETRACE_PRECISE;
2161 else goto invalid_vga;
2162 } else goto invalid_vga;
2163 opts = nextopt;
2164 }
malc3893c122008-09-28 00:42:05 +00002165}
2166
Jes Sorensen1472a952011-03-16 13:33:31 +01002167static DisplayType select_display(const char *p)
2168{
2169 const char *opts;
2170 DisplayType display = DT_DEFAULT;
2171
2172 if (strstart(p, "sdl", &opts)) {
2173#ifdef CONFIG_SDL
2174 display = DT_SDL;
2175 while (*opts) {
2176 const char *nextopt;
2177
2178 if (strstart(opts, ",frame=", &nextopt)) {
2179 opts = nextopt;
2180 if (strstart(opts, "on", &nextopt)) {
2181 no_frame = 0;
2182 } else if (strstart(opts, "off", &nextopt)) {
2183 no_frame = 1;
2184 } else {
Peter Maydell05175532011-03-23 03:40:57 +00002185 goto invalid_sdl_args;
Jes Sorensen1472a952011-03-16 13:33:31 +01002186 }
2187 } else if (strstart(opts, ",alt_grab=", &nextopt)) {
2188 opts = nextopt;
2189 if (strstart(opts, "on", &nextopt)) {
2190 alt_grab = 1;
2191 } else if (strstart(opts, "off", &nextopt)) {
2192 alt_grab = 0;
2193 } else {
Peter Maydell05175532011-03-23 03:40:57 +00002194 goto invalid_sdl_args;
Jes Sorensen1472a952011-03-16 13:33:31 +01002195 }
2196 } else if (strstart(opts, ",ctrl_grab=", &nextopt)) {
2197 opts = nextopt;
2198 if (strstart(opts, "on", &nextopt)) {
2199 ctrl_grab = 1;
2200 } else if (strstart(opts, "off", &nextopt)) {
2201 ctrl_grab = 0;
2202 } else {
Peter Maydell05175532011-03-23 03:40:57 +00002203 goto invalid_sdl_args;
Jes Sorensen1472a952011-03-16 13:33:31 +01002204 }
2205 } else if (strstart(opts, ",window_close=", &nextopt)) {
2206 opts = nextopt;
2207 if (strstart(opts, "on", &nextopt)) {
2208 no_quit = 0;
2209 } else if (strstart(opts, "off", &nextopt)) {
2210 no_quit = 1;
2211 } else {
Peter Maydell05175532011-03-23 03:40:57 +00002212 goto invalid_sdl_args;
Jes Sorensen1472a952011-03-16 13:33:31 +01002213 }
2214 } else {
Peter Maydell05175532011-03-23 03:40:57 +00002215 invalid_sdl_args:
2216 fprintf(stderr, "Invalid SDL option string: %s\n", p);
2217 exit(1);
Jes Sorensen1472a952011-03-16 13:33:31 +01002218 }
2219 opts = nextopt;
2220 }
2221#else
2222 fprintf(stderr, "SDL support is disabled\n");
2223 exit(1);
2224#endif
Jes Sorensen3264ff12011-03-16 13:33:33 +01002225 } else if (strstart(p, "vnc", &opts)) {
Jes Sorensen821601e2011-03-16 13:33:36 +01002226#ifdef CONFIG_VNC
Jes Sorensen3264ff12011-03-16 13:33:33 +01002227 display_remote++;
2228
2229 if (*opts) {
2230 const char *nextopt;
2231
2232 if (strstart(opts, "=", &nextopt)) {
2233 vnc_display = nextopt;
2234 }
2235 }
2236 if (!vnc_display) {
2237 fprintf(stderr, "VNC requires a display argument vnc=<display>\n");
2238 exit(1);
2239 }
Jes Sorensen821601e2011-03-16 13:33:36 +01002240#else
2241 fprintf(stderr, "VNC support is disabled\n");
2242 exit(1);
2243#endif
Jes Sorensen1472a952011-03-16 13:33:31 +01002244 } else if (strstart(p, "curses", &opts)) {
2245#ifdef CONFIG_CURSES
2246 display = DT_CURSES;
2247#else
2248 fprintf(stderr, "Curses support is disabled\n");
2249 exit(1);
2250#endif
Anthony Liguori15546422013-02-20 07:43:25 -06002251 } else if (strstart(p, "gtk", &opts)) {
2252#ifdef CONFIG_GTK
2253 display = DT_GTK;
2254#else
2255 fprintf(stderr, "GTK support is disabled\n");
2256 exit(1);
2257#endif
Jes Sorensen4171d322011-03-16 13:33:32 +01002258 } else if (strstart(p, "none", &opts)) {
2259 display = DT_NONE;
Jes Sorensen1472a952011-03-16 13:33:31 +01002260 } else {
Jes Sorensen1472a952011-03-16 13:33:31 +01002261 fprintf(stderr, "Unknown display type: %s\n", p);
2262 exit(1);
2263 }
2264
2265 return display;
2266}
2267
Markus Armbruster7d4c3d52009-06-26 19:15:14 +02002268static int balloon_parse(const char *arg)
2269{
Gerd Hoffmann382f0742009-08-14 10:34:22 +02002270 QemuOpts *opts;
Markus Armbruster7d4c3d52009-06-26 19:15:14 +02002271
Gerd Hoffmann382f0742009-08-14 10:34:22 +02002272 if (strcmp(arg, "none") == 0) {
2273 return 0;
Markus Armbruster7d4c3d52009-06-26 19:15:14 +02002274 }
Gerd Hoffmann382f0742009-08-14 10:34:22 +02002275
2276 if (!strncmp(arg, "virtio", 6)) {
2277 if (arg[6] == ',') {
2278 /* have params -> parse them */
Gerd Hoffmann3329f072010-08-20 13:52:01 +02002279 opts = qemu_opts_parse(qemu_find_opts("device"), arg+7, 0);
Gerd Hoffmann382f0742009-08-14 10:34:22 +02002280 if (!opts)
2281 return -1;
2282 } else {
2283 /* create empty opts */
Dong Xu Wange478b442012-12-06 14:47:22 +08002284 opts = qemu_opts_create_nofail(qemu_find_opts("device"));
Gerd Hoffmann382f0742009-08-14 10:34:22 +02002285 }
Alexander Graf29f82b32011-03-29 15:29:29 +02002286 qemu_opt_set(opts, "driver", "virtio-balloon");
Gerd Hoffmann382f0742009-08-14 10:34:22 +02002287 return 0;
2288 }
2289
2290 return -1;
Markus Armbruster7d4c3d52009-06-26 19:15:14 +02002291}
Markus Armbruster7d4c3d52009-06-26 19:15:14 +02002292
Paul Brook5cea8592009-05-30 00:52:44 +01002293char *qemu_find_file(int type, const char *name)
2294{
Gerd Hoffmann45240512013-03-08 11:42:24 +01002295 int i;
Paul Brook5cea8592009-05-30 00:52:44 +01002296 const char *subdir;
2297 char *buf;
2298
Peter Maydell31783202012-05-25 13:07:01 +01002299 /* Try the name as a straight path first */
2300 if (access(name, R_OK) == 0) {
Gerd Hoffmann45240512013-03-08 11:42:24 +01002301 trace_load_file(name, name);
Anthony Liguori7267c092011-08-20 22:09:37 -05002302 return g_strdup(name);
Paul Brook5cea8592009-05-30 00:52:44 +01002303 }
Gerd Hoffmann45240512013-03-08 11:42:24 +01002304
Paul Brook5cea8592009-05-30 00:52:44 +01002305 switch (type) {
2306 case QEMU_FILE_TYPE_BIOS:
2307 subdir = "";
2308 break;
2309 case QEMU_FILE_TYPE_KEYMAP:
2310 subdir = "keymaps/";
2311 break;
2312 default:
2313 abort();
2314 }
Gerd Hoffmann45240512013-03-08 11:42:24 +01002315
2316 for (i = 0; i < data_dir_idx; i++) {
2317 buf = g_strdup_printf("%s/%s%s", data_dir[i], subdir, name);
2318 if (access(buf, R_OK) == 0) {
2319 trace_load_file(name, buf);
2320 return buf;
2321 }
Anthony Liguori7267c092011-08-20 22:09:37 -05002322 g_free(buf);
Paul Brook5cea8592009-05-30 00:52:44 +01002323 }
Gerd Hoffmann45240512013-03-08 11:42:24 +01002324 return NULL;
Paul Brook5cea8592009-05-30 00:52:44 +01002325}
2326
Markus Armbrusterff952ba2010-01-29 19:48:57 +01002327static int device_help_func(QemuOpts *opts, void *opaque)
2328{
2329 return qdev_device_help(opts);
2330}
2331
Gerd Hoffmannf31d07d2009-07-31 12:25:37 +02002332static int device_init_func(QemuOpts *opts, void *opaque)
2333{
2334 DeviceState *dev;
2335
2336 dev = qdev_device_add(opts);
2337 if (!dev)
2338 return -1;
Paolo Bonzinib09995a2013-01-25 14:12:37 +01002339 object_unref(OBJECT(dev));
Gerd Hoffmannf31d07d2009-07-31 12:25:37 +02002340 return 0;
2341}
2342
Gerd Hoffmann1a688d32009-12-08 13:11:36 +01002343static int chardev_init_func(QemuOpts *opts, void *opaque)
2344{
Gerd Hoffmannbd2d80b2012-10-15 09:28:05 +02002345 Error *local_err = NULL;
Gerd Hoffmann1a688d32009-12-08 13:11:36 +01002346
Gerd Hoffmannbd2d80b2012-10-15 09:28:05 +02002347 qemu_chr_new_from_opts(opts, NULL, &local_err);
2348 if (error_is_set(&local_err)) {
2349 fprintf(stderr, "%s\n", error_get_pretty(local_err));
2350 error_free(local_err);
Gerd Hoffmann1a688d32009-12-08 13:11:36 +01002351 return -1;
Gerd Hoffmannbd2d80b2012-10-15 09:28:05 +02002352 }
Gerd Hoffmann1a688d32009-12-08 13:11:36 +01002353 return 0;
2354}
2355
Venkateswararao Jujjuri (JV)758e8e32010-06-14 13:34:41 -07002356#ifdef CONFIG_VIRTFS
Gautham R Shenoy74db9202010-04-29 17:44:43 +05302357static int fsdev_init_func(QemuOpts *opts, void *opaque)
2358{
2359 int ret;
2360 ret = qemu_fsdev_add(opts);
2361
2362 return ret;
2363}
2364#endif
2365
Gerd Hoffmann88589342009-12-08 13:11:50 +01002366static int mon_init_func(QemuOpts *opts, void *opaque)
2367{
2368 CharDriverState *chr;
2369 const char *chardev;
2370 const char *mode;
2371 int flags;
2372
2373 mode = qemu_opt_get(opts, "mode");
2374 if (mode == NULL) {
2375 mode = "readline";
2376 }
2377 if (strcmp(mode, "readline") == 0) {
2378 flags = MONITOR_USE_READLINE;
2379 } else if (strcmp(mode, "control") == 0) {
2380 flags = MONITOR_USE_CONTROL;
2381 } else {
2382 fprintf(stderr, "unknown monitor mode \"%s\"\n", mode);
2383 exit(1);
2384 }
2385
Daniel P. Berrange39eaab92010-06-07 15:42:31 +01002386 if (qemu_opt_get_bool(opts, "pretty", 0))
2387 flags |= MONITOR_USE_PRETTY;
2388
Gerd Hoffmann88589342009-12-08 13:11:50 +01002389 if (qemu_opt_get_bool(opts, "default", 0))
2390 flags |= MONITOR_IS_DEFAULT;
2391
2392 chardev = qemu_opt_get(opts, "chardev");
2393 chr = qemu_chr_find(chardev);
2394 if (chr == NULL) {
2395 fprintf(stderr, "chardev \"%s\" not found\n", chardev);
2396 exit(1);
2397 }
2398
2399 monitor_init(chr, flags);
2400 return 0;
2401}
2402
Gerd Hoffmann6ca55822009-12-08 13:11:52 +01002403static void monitor_parse(const char *optarg, const char *mode)
Gerd Hoffmann88589342009-12-08 13:11:50 +01002404{
2405 static int monitor_device_index = 0;
2406 QemuOpts *opts;
2407 const char *p;
2408 char label[32];
2409 int def = 0;
2410
2411 if (strstart(optarg, "chardev:", &p)) {
2412 snprintf(label, sizeof(label), "%s", p);
2413 } else {
Jan Kiszka140e0652010-04-06 16:55:52 +02002414 snprintf(label, sizeof(label), "compat_monitor%d",
2415 monitor_device_index);
2416 if (monitor_device_index == 0) {
Gerd Hoffmann88589342009-12-08 13:11:50 +01002417 def = 1;
2418 }
2419 opts = qemu_chr_parse_compat(label, optarg);
2420 if (!opts) {
2421 fprintf(stderr, "parse error: %s\n", optarg);
2422 exit(1);
2423 }
2424 }
2425
Luiz Capitulino8be7e7e2012-03-20 15:51:57 -03002426 opts = qemu_opts_create(qemu_find_opts("mon"), label, 1, NULL);
Gerd Hoffmann88589342009-12-08 13:11:50 +01002427 if (!opts) {
2428 fprintf(stderr, "duplicate chardev: %s\n", label);
2429 exit(1);
2430 }
Gerd Hoffmann6ca55822009-12-08 13:11:52 +01002431 qemu_opt_set(opts, "mode", mode);
Gerd Hoffmann88589342009-12-08 13:11:50 +01002432 qemu_opt_set(opts, "chardev", label);
2433 if (def)
2434 qemu_opt_set(opts, "default", "on");
2435 monitor_device_index++;
2436}
2437
Gerd Hoffmannbd3c9482009-07-15 13:59:26 +02002438struct device_config {
2439 enum {
Gerd Hoffmannaee1b932009-12-08 13:11:53 +01002440 DEV_USB, /* -usbdevice */
2441 DEV_BT, /* -bt */
2442 DEV_SERIAL, /* -serial */
2443 DEV_PARALLEL, /* -parallel */
2444 DEV_VIRTCON, /* -virtioconsole */
H. Peter Anvinc9f398e2009-12-29 13:51:36 -08002445 DEV_DEBUGCON, /* -debugcon */
Markus Armbrusteref0c4a02012-02-07 15:09:13 +01002446 DEV_GDB, /* -gdb, -s */
Alexander Graf3ef669e2013-01-24 12:18:52 +01002447 DEV_SCLP, /* s390 sclp */
Gerd Hoffmannbd3c9482009-07-15 13:59:26 +02002448 } type;
2449 const char *cmdline;
Markus Armbrusterd9a59542012-02-07 15:09:12 +01002450 Location loc;
Blue Swirl72cf2d42009-09-12 07:36:22 +00002451 QTAILQ_ENTRY(device_config) next;
Gerd Hoffmannbd3c9482009-07-15 13:59:26 +02002452};
Blue Swirl4fdcac02012-10-28 11:04:47 +00002453
2454static QTAILQ_HEAD(, device_config) device_configs =
2455 QTAILQ_HEAD_INITIALIZER(device_configs);
Gerd Hoffmannbd3c9482009-07-15 13:59:26 +02002456
2457static void add_device_config(int type, const char *cmdline)
2458{
2459 struct device_config *conf;
2460
Anthony Liguori7267c092011-08-20 22:09:37 -05002461 conf = g_malloc0(sizeof(*conf));
Gerd Hoffmannbd3c9482009-07-15 13:59:26 +02002462 conf->type = type;
2463 conf->cmdline = cmdline;
Markus Armbrusterd9a59542012-02-07 15:09:12 +01002464 loc_save(&conf->loc);
Blue Swirl72cf2d42009-09-12 07:36:22 +00002465 QTAILQ_INSERT_TAIL(&device_configs, conf, next);
Gerd Hoffmannbd3c9482009-07-15 13:59:26 +02002466}
2467
2468static int foreach_device_config(int type, int (*func)(const char *cmdline))
2469{
2470 struct device_config *conf;
2471 int rc;
2472
Blue Swirl72cf2d42009-09-12 07:36:22 +00002473 QTAILQ_FOREACH(conf, &device_configs, next) {
Gerd Hoffmannbd3c9482009-07-15 13:59:26 +02002474 if (conf->type != type)
2475 continue;
Markus Armbrusterd9a59542012-02-07 15:09:12 +01002476 loc_push_restore(&conf->loc);
Gerd Hoffmannbd3c9482009-07-15 13:59:26 +02002477 rc = func(conf->cmdline);
Markus Armbrusterd9a59542012-02-07 15:09:12 +01002478 loc_pop(&conf->loc);
Gerd Hoffmannbd3c9482009-07-15 13:59:26 +02002479 if (0 != rc)
2480 return rc;
2481 }
2482 return 0;
2483}
2484
Gerd Hoffmann998bbd72009-12-08 13:11:41 +01002485static int serial_parse(const char *devname)
2486{
2487 static int index = 0;
2488 char label[32];
2489
2490 if (strcmp(devname, "none") == 0)
2491 return 0;
2492 if (index == MAX_SERIAL_PORTS) {
2493 fprintf(stderr, "qemu: too many serial ports\n");
2494 exit(1);
2495 }
2496 snprintf(label, sizeof(label), "serial%d", index);
Anthony Liguori27143a42011-08-15 11:17:36 -05002497 serial_hds[index] = qemu_chr_new(label, devname, NULL);
Gerd Hoffmann998bbd72009-12-08 13:11:41 +01002498 if (!serial_hds[index]) {
Peter Maydell52d06132012-07-09 04:28:30 +00002499 fprintf(stderr, "qemu: could not connect serial device"
2500 " to character backend '%s'\n", devname);
Gerd Hoffmann998bbd72009-12-08 13:11:41 +01002501 return -1;
2502 }
2503 index++;
2504 return 0;
2505}
2506
Gerd Hoffmann6a5e8b02009-12-08 13:11:42 +01002507static int parallel_parse(const char *devname)
2508{
2509 static int index = 0;
2510 char label[32];
2511
2512 if (strcmp(devname, "none") == 0)
2513 return 0;
2514 if (index == MAX_PARALLEL_PORTS) {
2515 fprintf(stderr, "qemu: too many parallel ports\n");
2516 exit(1);
2517 }
2518 snprintf(label, sizeof(label), "parallel%d", index);
Anthony Liguori27143a42011-08-15 11:17:36 -05002519 parallel_hds[index] = qemu_chr_new(label, devname, NULL);
Gerd Hoffmann6a5e8b02009-12-08 13:11:42 +01002520 if (!parallel_hds[index]) {
Peter Maydell52d06132012-07-09 04:28:30 +00002521 fprintf(stderr, "qemu: could not connect parallel device"
2522 " to character backend '%s'\n", devname);
Gerd Hoffmann6a5e8b02009-12-08 13:11:42 +01002523 return -1;
2524 }
2525 index++;
2526 return 0;
2527}
2528
Gerd Hoffmannaee1b932009-12-08 13:11:53 +01002529static int virtcon_parse(const char *devname)
2530{
Gerd Hoffmann3329f072010-08-20 13:52:01 +02002531 QemuOptsList *device = qemu_find_opts("device");
Gerd Hoffmannaee1b932009-12-08 13:11:53 +01002532 static int index = 0;
2533 char label[32];
Amit Shah392ecf52010-01-21 16:19:23 +05302534 QemuOpts *bus_opts, *dev_opts;
Gerd Hoffmannaee1b932009-12-08 13:11:53 +01002535
2536 if (strcmp(devname, "none") == 0)
2537 return 0;
2538 if (index == MAX_VIRTIO_CONSOLES) {
2539 fprintf(stderr, "qemu: too many virtio consoles\n");
2540 exit(1);
2541 }
Amit Shah392ecf52010-01-21 16:19:23 +05302542
Dong Xu Wange478b442012-12-06 14:47:22 +08002543 bus_opts = qemu_opts_create_nofail(device);
Anthony Liguorie87f7fc2012-02-06 11:07:18 -06002544 if (arch_type == QEMU_ARCH_S390X) {
2545 qemu_opt_set(bus_opts, "driver", "virtio-serial-s390");
2546 } else {
2547 qemu_opt_set(bus_opts, "driver", "virtio-serial-pci");
2548 }
Amit Shah392ecf52010-01-21 16:19:23 +05302549
Dong Xu Wange478b442012-12-06 14:47:22 +08002550 dev_opts = qemu_opts_create_nofail(device);
Amit Shah392ecf52010-01-21 16:19:23 +05302551 qemu_opt_set(dev_opts, "driver", "virtconsole");
2552
Gerd Hoffmannaee1b932009-12-08 13:11:53 +01002553 snprintf(label, sizeof(label), "virtcon%d", index);
Anthony Liguori27143a42011-08-15 11:17:36 -05002554 virtcon_hds[index] = qemu_chr_new(label, devname, NULL);
Gerd Hoffmannaee1b932009-12-08 13:11:53 +01002555 if (!virtcon_hds[index]) {
Peter Maydell52d06132012-07-09 04:28:30 +00002556 fprintf(stderr, "qemu: could not connect virtio console"
2557 " to character backend '%s'\n", devname);
Gerd Hoffmannaee1b932009-12-08 13:11:53 +01002558 return -1;
2559 }
Amit Shah392ecf52010-01-21 16:19:23 +05302560 qemu_opt_set(dev_opts, "chardev", label);
2561
Gerd Hoffmannaee1b932009-12-08 13:11:53 +01002562 index++;
2563 return 0;
2564}
2565
Alexander Graf3ef669e2013-01-24 12:18:52 +01002566static int sclp_parse(const char *devname)
2567{
2568 QemuOptsList *device = qemu_find_opts("device");
2569 static int index = 0;
2570 char label[32];
2571 QemuOpts *dev_opts;
2572
2573 if (strcmp(devname, "none") == 0) {
2574 return 0;
2575 }
2576 if (index == MAX_SCLP_CONSOLES) {
2577 fprintf(stderr, "qemu: too many sclp consoles\n");
2578 exit(1);
2579 }
2580
2581 assert(arch_type == QEMU_ARCH_S390X);
2582
2583 dev_opts = qemu_opts_create(device, NULL, 0, NULL);
2584 qemu_opt_set(dev_opts, "driver", "sclpconsole");
2585
2586 snprintf(label, sizeof(label), "sclpcon%d", index);
2587 sclp_hds[index] = qemu_chr_new(label, devname, NULL);
2588 if (!sclp_hds[index]) {
2589 fprintf(stderr, "qemu: could not connect sclp console"
2590 " to character backend '%s'\n", devname);
2591 return -1;
2592 }
2593 qemu_opt_set(dev_opts, "chardev", label);
2594
2595 index++;
2596 return 0;
2597}
2598
H. Peter Anvinc9f398e2009-12-29 13:51:36 -08002599static int debugcon_parse(const char *devname)
2600{
2601 QemuOpts *opts;
2602
Anthony Liguori27143a42011-08-15 11:17:36 -05002603 if (!qemu_chr_new("debugcon", devname, NULL)) {
H. Peter Anvinc9f398e2009-12-29 13:51:36 -08002604 exit(1);
2605 }
Luiz Capitulino8be7e7e2012-03-20 15:51:57 -03002606 opts = qemu_opts_create(qemu_find_opts("device"), "debugcon", 1, NULL);
H. Peter Anvinc9f398e2009-12-29 13:51:36 -08002607 if (!opts) {
2608 fprintf(stderr, "qemu: already have a debugcon device\n");
2609 exit(1);
2610 }
2611 qemu_opt_set(opts, "driver", "isa-debugcon");
2612 qemu_opt_set(opts, "chardev", "debugcon");
2613 return 0;
2614}
2615
Jan Kiszka9052ea62011-07-23 12:38:37 +02002616static QEMUMachine *machine_parse(const char *name)
2617{
2618 QEMUMachine *m, *machine = NULL;
2619
2620 if (name) {
2621 machine = find_machine(name);
2622 }
2623 if (machine) {
2624 return machine;
2625 }
2626 printf("Supported machines are:\n");
2627 for (m = first_machine; m != NULL; m = m->next) {
2628 if (m->alias) {
Peter Maydell3b264862012-02-22 22:13:11 +00002629 printf("%-20s %s (alias of %s)\n", m->alias, m->desc, m->name);
Jan Kiszka9052ea62011-07-23 12:38:37 +02002630 }
Peter Maydell3b264862012-02-22 22:13:11 +00002631 printf("%-20s %s%s\n", m->name, m->desc,
Jan Kiszka9052ea62011-07-23 12:38:37 +02002632 m->is_default ? " (default)" : "");
2633 }
Peter Maydellc8057f92012-08-02 13:45:54 +01002634 exit(!name || !is_help_option(name));
Jan Kiszka9052ea62011-07-23 12:38:37 +02002635}
2636
Anthony PERARD303d4e82010-09-21 20:05:31 +01002637static int tcg_init(void)
2638{
Jan Kiszkad5ab9712011-08-02 16:10:21 +02002639 tcg_exec_init(tcg_tb_size * 1024 * 1024);
Anthony PERARD303d4e82010-09-21 20:05:31 +01002640 return 0;
2641}
2642
2643static struct {
2644 const char *opt_name;
2645 const char *name;
2646 int (*available)(void);
2647 int (*init)(void);
liguangd5286af2013-01-24 13:03:27 +08002648 bool *allowed;
Anthony PERARD303d4e82010-09-21 20:05:31 +01002649} accel_list[] = {
2650 { "tcg", "tcg", tcg_available, tcg_init, &tcg_allowed },
Anthony PERARD3285cf42010-08-19 12:27:56 +01002651 { "xen", "Xen", xen_available, xen_init, &xen_allowed },
Anthony PERARD303d4e82010-09-21 20:05:31 +01002652 { "kvm", "KVM", kvm_available, kvm_init, &kvm_allowed },
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +02002653 { "qtest", "QTest", qtest_available, qtest_init, &qtest_allowed },
Anthony PERARD303d4e82010-09-21 20:05:31 +01002654};
2655
2656static int configure_accelerator(void)
2657{
2658 const char *p = NULL;
2659 char buf[10];
2660 int i, ret;
liguang217e21b2013-01-24 13:03:26 +08002661 bool accel_initialised = false;
2662 bool init_failed = false;
Anthony PERARD303d4e82010-09-21 20:05:31 +01002663
2664 QemuOptsList *list = qemu_find_opts("machine");
2665 if (!QTAILQ_EMPTY(&list->head)) {
2666 p = qemu_opt_get(QTAILQ_FIRST(&list->head), "accel");
2667 }
2668
2669 if (p == NULL) {
2670 /* Use the default "accelerator", tcg */
2671 p = "tcg";
2672 }
2673
Peter Maydell1b785a92012-02-07 20:57:27 +00002674 while (!accel_initialised && *p != '\0') {
Anthony PERARD303d4e82010-09-21 20:05:31 +01002675 if (*p == ':') {
2676 p++;
2677 }
2678 p = get_opt_name(buf, sizeof (buf), p, ':');
2679 for (i = 0; i < ARRAY_SIZE(accel_list); i++) {
2680 if (strcmp(accel_list[i].opt_name, buf) == 0) {
liguange3c66d92013-01-24 13:03:25 +08002681 if (!accel_list[i].available()) {
2682 printf("%s not supported for this target\n",
2683 accel_list[i].name);
2684 continue;
2685 }
liguangd5286af2013-01-24 13:03:27 +08002686 *(accel_list[i].allowed) = true;
Anthony PERARD303d4e82010-09-21 20:05:31 +01002687 ret = accel_list[i].init();
2688 if (ret < 0) {
liguang217e21b2013-01-24 13:03:26 +08002689 init_failed = true;
liguange3c66d92013-01-24 13:03:25 +08002690 fprintf(stderr, "failed to initialize %s: %s\n",
2691 accel_list[i].name,
2692 strerror(-ret));
liguangd5286af2013-01-24 13:03:27 +08002693 *(accel_list[i].allowed) = false;
Anthony PERARD303d4e82010-09-21 20:05:31 +01002694 } else {
liguang217e21b2013-01-24 13:03:26 +08002695 accel_initialised = true;
Anthony PERARD303d4e82010-09-21 20:05:31 +01002696 }
2697 break;
2698 }
2699 }
2700 if (i == ARRAY_SIZE(accel_list)) {
2701 fprintf(stderr, "\"%s\" accelerator does not exist.\n", buf);
2702 }
2703 }
2704
Peter Maydell1b785a92012-02-07 20:57:27 +00002705 if (!accel_initialised) {
liguang217e21b2013-01-24 13:03:26 +08002706 if (!init_failed) {
2707 fprintf(stderr, "No accelerator found!\n");
2708 }
Anthony PERARD303d4e82010-09-21 20:05:31 +01002709 exit(1);
2710 }
2711
2712 if (init_failed) {
2713 fprintf(stderr, "Back to %s accelerator.\n", accel_list[i].name);
2714 }
2715
Peter Maydell1b785a92012-02-07 20:57:27 +00002716 return !accel_initialised;
Anthony PERARD303d4e82010-09-21 20:05:31 +01002717}
2718
Gerd Hoffmannfd42dee2010-06-04 14:08:07 +02002719void qemu_add_exit_notifier(Notifier *notify)
2720{
2721 notifier_list_add(&exit_notifiers, notify);
2722}
2723
2724void qemu_remove_exit_notifier(Notifier *notify)
2725{
Paolo Bonzini31552522012-01-13 17:34:01 +01002726 notifier_remove(notify);
Gerd Hoffmannfd42dee2010-06-04 14:08:07 +02002727}
2728
2729static void qemu_run_exit_notifiers(void)
2730{
Jan Kiszka9e8dd452011-06-20 14:06:26 +02002731 notifier_list_notify(&exit_notifiers, NULL);
Gerd Hoffmannfd42dee2010-06-04 14:08:07 +02002732}
2733
Gleb Natapov4cab9462010-12-08 13:35:08 +02002734void qemu_add_machine_init_done_notifier(Notifier *notify)
2735{
2736 notifier_list_add(&machine_init_done_notifiers, notify);
2737}
2738
2739static void qemu_run_machine_init_done_notifiers(void)
2740{
Jan Kiszka9e8dd452011-06-20 14:06:26 +02002741 notifier_list_notify(&machine_init_done_notifiers, NULL);
Gleb Natapov4cab9462010-12-08 13:35:08 +02002742}
2743
Anthony Liguori6530a972010-01-22 09:18:06 -06002744static const QEMUOption *lookup_opt(int argc, char **argv,
2745 const char **poptarg, int *poptind)
2746{
2747 const QEMUOption *popt;
2748 int optind = *poptind;
2749 char *r = argv[optind];
2750 const char *optarg;
2751
Markus Armbruster0f0bc3f2010-02-18 20:13:51 +01002752 loc_set_cmdline(argv, optind, 1);
Anthony Liguori6530a972010-01-22 09:18:06 -06002753 optind++;
2754 /* Treat --foo the same as -foo. */
2755 if (r[1] == '-')
2756 r++;
2757 popt = qemu_options;
2758 for(;;) {
2759 if (!popt->name) {
Markus Armbruster0f0bc3f2010-02-18 20:13:51 +01002760 error_report("invalid option");
Anthony Liguori6530a972010-01-22 09:18:06 -06002761 exit(1);
2762 }
2763 if (!strcmp(popt->name, r + 1))
2764 break;
2765 popt++;
2766 }
2767 if (popt->flags & HAS_ARG) {
2768 if (optind >= argc) {
Markus Armbruster0f0bc3f2010-02-18 20:13:51 +01002769 error_report("requires an argument");
Anthony Liguori6530a972010-01-22 09:18:06 -06002770 exit(1);
2771 }
2772 optarg = argv[optind++];
Markus Armbruster0f0bc3f2010-02-18 20:13:51 +01002773 loc_set_cmdline(argv, optind - 2, 2);
Anthony Liguori6530a972010-01-22 09:18:06 -06002774 } else {
2775 optarg = NULL;
2776 }
2777
2778 *poptarg = optarg;
2779 *poptind = optind;
2780
2781 return popt;
2782}
2783
Anthony Liguori07501122011-08-20 22:38:31 -05002784static gpointer malloc_and_trace(gsize n_bytes)
2785{
2786 void *ptr = malloc(n_bytes);
Frediano Ziglioa74cd8c2011-08-31 09:25:35 +02002787 trace_g_malloc(n_bytes, ptr);
Anthony Liguori07501122011-08-20 22:38:31 -05002788 return ptr;
2789}
2790
2791static gpointer realloc_and_trace(gpointer mem, gsize n_bytes)
2792{
2793 void *ptr = realloc(mem, n_bytes);
Frediano Ziglioa74cd8c2011-08-31 09:25:35 +02002794 trace_g_realloc(mem, n_bytes, ptr);
Anthony Liguori07501122011-08-20 22:38:31 -05002795 return ptr;
2796}
2797
2798static void free_and_trace(gpointer mem)
2799{
Frediano Ziglioa74cd8c2011-08-31 09:25:35 +02002800 trace_g_free(mem);
Anthony Liguori07501122011-08-20 22:38:31 -05002801 free(mem);
2802}
2803
Anthony Liguori68d98d32012-06-25 14:36:33 -05002804static int object_set_property(const char *name, const char *value, void *opaque)
2805{
2806 Object *obj = OBJECT(opaque);
2807 StringInputVisitor *siv;
2808 Error *local_err = NULL;
2809
2810 if (strcmp(name, "qom-type") == 0 || strcmp(name, "id") == 0) {
2811 return 0;
2812 }
2813
2814 siv = string_input_visitor_new(value);
2815 object_property_set(obj, string_input_get_visitor(siv), name, &local_err);
2816 string_input_visitor_cleanup(siv);
2817
2818 if (local_err) {
2819 qerror_report_err(local_err);
2820 error_free(local_err);
2821 return -1;
2822 }
2823
2824 return 0;
2825}
2826
2827static int object_create(QemuOpts *opts, void *opaque)
2828{
2829 const char *type = qemu_opt_get(opts, "qom-type");
2830 const char *id = qemu_opts_id(opts);
2831 Object *obj;
2832
2833 g_assert(type != NULL);
2834
2835 if (id == NULL) {
2836 qerror_report(QERR_MISSING_PARAMETER, "id");
2837 return -1;
2838 }
2839
2840 obj = object_new(type);
2841 if (qemu_opt_foreach(opts, object_set_property, obj, 1) < 0) {
2842 return -1;
2843 }
2844
2845 object_property_add_child(container_get(object_get_root(), "/objects"),
2846 id, obj, NULL);
2847
2848 return 0;
2849}
2850
malc902b3d52008-12-10 19:18:40 +00002851int main(int argc, char **argv, char **envp)
bellard0824d6f2003-06-24 13:42:40 +00002852{
thse4bcb142007-12-02 04:51:10 +00002853 int i;
Eduardo Habkostda1fcfd2010-04-06 19:22:07 -03002854 int snapshot, linux_boot;
Paolo Bonzini4e3de9e2010-03-10 11:38:48 +01002855 const char *icount_option = NULL;
bellard7f7f9872003-10-30 01:11:23 +00002856 const char *initrd_filename;
bellarda20dd502003-09-30 21:07:02 +00002857 const char *kernel_filename, *kernel_cmdline;
Avik Sile4ada292013-01-08 12:36:30 +05302858 char boot_devices[33] = "";
aliguori3023f3322009-01-16 19:04:14 +00002859 DisplayState *ds;
malc9f227bc2012-08-27 18:33:22 +04002860 int cyls, heads, secs, translation;
2861 QemuOpts *hda_opts = NULL, *opts, *machine_opts;
Gerd Hoffmann03b0ba72010-08-20 13:52:02 +02002862 QemuOptsList *olist;
bellardcd6f1162004-05-13 22:02:20 +00002863 int optind;
Anthony Liguori6530a972010-01-22 09:18:06 -06002864 const char *optarg;
bellardd63d3072004-10-03 13:29:03 +00002865 const char *loadvm = NULL;
bellardcc1daa42005-06-05 14:49:17 +00002866 QEMUMachine *machine;
j_mayer94fc95c2007-03-05 19:44:02 +00002867 const char *cpu_model;
Paolo Bonzini7f1b17f2012-05-10 09:39:17 +02002868 const char *vga_model = "none";
ths93815bc2007-03-19 15:58:31 +00002869 const char *pid_file = NULL;
aliguori5bb79102008-10-13 03:12:02 +00002870 const char *incoming = NULL;
Jes Sorensen821601e2011-03-16 13:33:36 +01002871#ifdef CONFIG_VNC
Anthony Liguori993fbfd2009-05-21 16:54:00 -05002872 int show_vnc_port = 0;
Jes Sorensen821601e2011-03-16 13:33:36 +01002873#endif
Eduardo Habkost3ed2d9e2012-05-02 13:07:28 -03002874 bool defconfig = true;
Eduardo Habkostf29a5612012-05-02 13:07:29 -03002875 bool userconfig = true;
Matthew Fernandezc235d732011-06-07 16:32:40 +00002876 const char *log_mask = NULL;
2877 const char *log_file = NULL;
Anthony Liguori07501122011-08-20 22:38:31 -05002878 GMemVTable mem_trace = {
2879 .malloc = malloc_and_trace,
2880 .realloc = realloc_and_trace,
2881 .free = free_and_trace,
2882 };
Lluís23d15e82011-08-31 20:31:31 +02002883 const char *trace_events = NULL;
Lluíse4858972011-08-31 20:31:03 +02002884 const char *trace_file = NULL;
Stefan Hajnoczi0b5538c2011-02-26 18:38:39 +00002885
Gerd Hoffmannfd42dee2010-06-04 14:08:07 +02002886 atexit(qemu_run_exit_notifiers);
Markus Armbruster65abca02010-02-24 14:37:14 +01002887 error_set_progname(argv[0]);
2888
Anthony Liguori07501122011-08-20 22:38:31 -05002889 g_mem_set_vtable(&mem_trace);
Andreas Färberdb529aa2011-10-31 19:14:56 +01002890 if (!g_thread_supported()) {
Alon Levy42ed3722011-12-20 13:41:04 +02002891#if !GLIB_CHECK_VERSION(2, 31, 0)
Andreas Färberdb529aa2011-10-31 19:14:56 +01002892 g_thread_init(NULL);
Alon Levy42ed3722011-12-20 13:41:04 +02002893#else
2894 fprintf(stderr, "glib threading failed to initialize.\n");
2895 exit(1);
2896#endif
Andreas Färberdb529aa2011-10-31 19:14:56 +01002897 }
Anthony Liguori07501122011-08-20 22:38:31 -05002898
Andreas Färber1b71f7c2012-03-04 21:32:35 +01002899 module_call_init(MODULE_INIT_QOM);
2900
Paolo Bonzini4d454572012-11-26 16:03:42 +01002901 qemu_add_opts(&qemu_drive_opts);
2902 qemu_add_opts(&qemu_chardev_opts);
2903 qemu_add_opts(&qemu_device_opts);
2904 qemu_add_opts(&qemu_netdev_opts);
2905 qemu_add_opts(&qemu_net_opts);
2906 qemu_add_opts(&qemu_rtc_opts);
2907 qemu_add_opts(&qemu_global_opts);
2908 qemu_add_opts(&qemu_mon_opts);
2909 qemu_add_opts(&qemu_trace_opts);
2910 qemu_add_opts(&qemu_option_rom_opts);
2911 qemu_add_opts(&qemu_machine_opts);
2912 qemu_add_opts(&qemu_boot_opts);
2913 qemu_add_opts(&qemu_sandbox_opts);
2914 qemu_add_opts(&qemu_add_fd_opts);
2915 qemu_add_opts(&qemu_object_opts);
Stefan Bergerd1a0cf72013-02-27 12:47:49 -05002916 qemu_add_opts(&qemu_tpmdev_opts);
Paolo Bonzini4d454572012-11-26 16:03:42 +01002917
Luiz Capitulino5db9d4d2011-09-05 16:36:31 -03002918 runstate_init();
2919
Jan Kiszka68752042009-09-15 13:36:04 +02002920 init_clocks();
Paolo Bonzini2ff68d02011-09-12 16:21:44 +02002921 rtc_clock = host_clock;
Jan Kiszka68752042009-09-15 13:36:04 +02002922
malc902b3d52008-12-10 19:18:40 +00002923 qemu_cache_utils_init(envp);
2924
Blue Swirl72cf2d42009-09-12 07:36:22 +00002925 QLIST_INIT (&vm_change_state_head);
Jes Sorensenfe98ac12010-06-10 11:42:21 +02002926 os_setup_early_signal_handling();
bellardbe995c22006-06-25 16:25:21 +00002927
Anthony Liguorif80f9ec2009-05-20 18:38:09 -05002928 module_call_init(MODULE_INIT_MACHINE);
Anthony Liguori0c257432009-05-21 20:41:01 -05002929 machine = find_default_machine();
j_mayer94fc95c2007-03-05 19:44:02 +00002930 cpu_model = NULL;
aurel324fc5d072008-04-27 21:39:40 +00002931 ram_size = 0;
bellard33e39632003-07-06 17:15:21 +00002932 snapshot = 0;
malc9f227bc2012-08-27 18:33:22 +04002933 cyls = heads = secs = 0;
2934 translation = BIOS_ATA_TRANSLATION_AUTO;
bellardc4b1fcc2004-03-14 21:44:30 +00002935
aliguori268a3622009-04-21 22:30:27 +00002936 for (i = 0; i < MAX_NODES; i++) {
2937 node_mem[i] = 0;
Chegu Vinodee785fe2012-07-16 21:31:30 -07002938 node_cpumask[i] = bitmap_new(MAX_CPUMASK_BITS);
aliguori268a3622009-04-21 22:30:27 +00002939 }
2940
aliguori268a3622009-04-21 22:30:27 +00002941 nb_numa_nodes = 0;
bellard7c9d8e02005-11-15 22:16:05 +00002942 nb_nics = 0;
ths3b46e622007-09-17 08:09:54 +00002943
blueswir141bd6392008-10-05 09:56:21 +00002944 autostart= 1;
2945
Anthony Liguori292444c2010-01-21 10:57:58 -06002946 /* first pass of option parsing */
2947 optind = 1;
2948 while (optind < argc) {
2949 if (argv[optind][0] != '-') {
2950 /* disk image */
Anthony Liguori28e68d62010-01-27 10:46:00 -06002951 optind++;
Anthony Liguori292444c2010-01-21 10:57:58 -06002952 continue;
2953 } else {
2954 const QEMUOption *popt;
2955
2956 popt = lookup_opt(argc, argv, &optarg, &optind);
2957 switch (popt->index) {
2958 case QEMU_OPTION_nodefconfig:
Eduardo Habkost3ed2d9e2012-05-02 13:07:28 -03002959 defconfig = false;
Anthony Liguori292444c2010-01-21 10:57:58 -06002960 break;
Eduardo Habkostf29a5612012-05-02 13:07:29 -03002961 case QEMU_OPTION_nouserconfig:
2962 userconfig = false;
2963 break;
Anthony Liguori292444c2010-01-21 10:57:58 -06002964 }
2965 }
2966 }
2967
2968 if (defconfig) {
Kevin Wolfdcfb0932010-03-05 17:25:55 +01002969 int ret;
Eduardo Habkostf29a5612012-05-02 13:07:29 -03002970 ret = qemu_read_default_config_files(userconfig);
Eduardo Habkostb5a8fe52012-05-02 13:07:25 -03002971 if (ret < 0) {
Kevin Wolfdcfb0932010-03-05 17:25:55 +01002972 exit(1);
Anthony Liguori292444c2010-01-21 10:57:58 -06002973 }
2974 }
2975
2976 /* second pass of option parsing */
bellardcd6f1162004-05-13 22:02:20 +00002977 optind = 1;
bellard0824d6f2003-06-24 13:42:40 +00002978 for(;;) {
bellardcd6f1162004-05-13 22:02:20 +00002979 if (optind >= argc)
bellard0824d6f2003-06-24 13:42:40 +00002980 break;
Anthony Liguori6530a972010-01-22 09:18:06 -06002981 if (argv[optind][0] != '-') {
malc9f227bc2012-08-27 18:33:22 +04002982 hda_opts = drive_add(IF_DEFAULT, 0, argv[optind++], HD_OPTS);
bellardcd6f1162004-05-13 22:02:20 +00002983 } else {
2984 const QEMUOption *popt;
2985
Anthony Liguori6530a972010-01-22 09:18:06 -06002986 popt = lookup_opt(argc, argv, &optarg, &optind);
Blue Swirlad960902010-03-29 19:23:52 +00002987 if (!(popt->arch_mask & arch_type)) {
2988 printf("Option %s not supported for this target\n", popt->name);
2989 exit(1);
2990 }
bellardcd6f1162004-05-13 22:02:20 +00002991 switch(popt->index) {
bellardcc1daa42005-06-05 14:49:17 +00002992 case QEMU_OPTION_M:
Jan Kiszka9052ea62011-07-23 12:38:37 +02002993 machine = machine_parse(optarg);
bellardcc1daa42005-06-05 14:49:17 +00002994 break;
Jan Kiszkae43d5942012-10-05 14:51:40 -03002995 case QEMU_OPTION_no_kvm_irqchip: {
2996 olist = qemu_find_opts("machine");
2997 qemu_opts_parse(olist, "kernel_irqchip=off", 0);
2998 break;
2999 }
j_mayer94fc95c2007-03-05 19:44:02 +00003000 case QEMU_OPTION_cpu:
3001 /* hw initialization will check this */
Eduardo Habkostecf40be2012-03-09 16:19:07 -03003002 cpu_model = optarg;
j_mayer94fc95c2007-03-05 19:44:02 +00003003 break;
bellardcd6f1162004-05-13 22:02:20 +00003004 case QEMU_OPTION_hda:
malc9f227bc2012-08-27 18:33:22 +04003005 {
3006 char buf[256];
3007 if (cyls == 0)
3008 snprintf(buf, sizeof(buf), "%s", HD_OPTS);
3009 else
3010 snprintf(buf, sizeof(buf),
3011 "%s,cyls=%d,heads=%d,secs=%d%s",
3012 HD_OPTS , cyls, heads, secs,
3013 translation == BIOS_ATA_TRANSLATION_LBA ?
3014 ",trans=lba" :
3015 translation == BIOS_ATA_TRANSLATION_NONE ?
3016 ",trans=none" : "");
3017 drive_add(IF_DEFAULT, 0, optarg, buf);
3018 break;
3019 }
bellardcd6f1162004-05-13 22:02:20 +00003020 case QEMU_OPTION_hdb:
bellardcc1daa42005-06-05 14:49:17 +00003021 case QEMU_OPTION_hdc:
3022 case QEMU_OPTION_hdd:
Markus Armbruster2292dda2011-01-28 11:21:41 +01003023 drive_add(IF_DEFAULT, popt->index - QEMU_OPTION_hda, optarg,
3024 HD_OPTS);
bellardfc01f7e2003-06-30 10:03:06 +00003025 break;
thse4bcb142007-12-02 04:51:10 +00003026 case QEMU_OPTION_drive:
Michael Tokareve2982c32011-03-30 16:31:05 +04003027 if (drive_def(optarg) == NULL) {
3028 exit(1);
3029 }
thse4bcb142007-12-02 04:51:10 +00003030 break;
Gerd Hoffmannd058fe02009-07-31 12:25:36 +02003031 case QEMU_OPTION_set:
3032 if (qemu_set_option(optarg) != 0)
3033 exit(1);
3034 break;
Gerd Hoffmannd0fef6f2009-12-08 13:11:34 +01003035 case QEMU_OPTION_global:
3036 if (qemu_global_option(optarg) != 0)
3037 exit(1);
3038 break;
balrog3e3d5812007-04-30 02:09:25 +00003039 case QEMU_OPTION_mtdblock:
Markus Armbruster2292dda2011-01-28 11:21:41 +01003040 drive_add(IF_MTD, -1, optarg, MTD_OPTS);
balrog3e3d5812007-04-30 02:09:25 +00003041 break;
pbrooka1bb27b2007-04-06 16:49:48 +00003042 case QEMU_OPTION_sd:
Peter Crosthwaite80f4d9f2013-02-28 18:23:14 +00003043 drive_add(IF_SD, -1, optarg, SD_OPTS);
pbrooka1bb27b2007-04-06 16:49:48 +00003044 break;
j_mayer86f55662007-04-24 06:52:59 +00003045 case QEMU_OPTION_pflash:
Markus Armbruster2292dda2011-01-28 11:21:41 +01003046 drive_add(IF_PFLASH, -1, optarg, PFLASH_OPTS);
j_mayer86f55662007-04-24 06:52:59 +00003047 break;
bellardcd6f1162004-05-13 22:02:20 +00003048 case QEMU_OPTION_snapshot:
bellard33e39632003-07-06 17:15:21 +00003049 snapshot = 1;
3050 break;
bellardcd6f1162004-05-13 22:02:20 +00003051 case QEMU_OPTION_hdachs:
bellard330d0412003-07-26 18:11:40 +00003052 {
bellard330d0412003-07-26 18:11:40 +00003053 const char *p;
3054 p = optarg;
3055 cyls = strtol(p, (char **)&p, 0);
bellard46d47672004-11-16 01:45:27 +00003056 if (cyls < 1 || cyls > 16383)
3057 goto chs_fail;
bellard330d0412003-07-26 18:11:40 +00003058 if (*p != ',')
3059 goto chs_fail;
3060 p++;
3061 heads = strtol(p, (char **)&p, 0);
bellard46d47672004-11-16 01:45:27 +00003062 if (heads < 1 || heads > 16)
3063 goto chs_fail;
bellard330d0412003-07-26 18:11:40 +00003064 if (*p != ',')
3065 goto chs_fail;
3066 p++;
3067 secs = strtol(p, (char **)&p, 0);
bellard46d47672004-11-16 01:45:27 +00003068 if (secs < 1 || secs > 63)
3069 goto chs_fail;
3070 if (*p == ',') {
3071 p++;
3072 if (!strcmp(p, "none"))
3073 translation = BIOS_ATA_TRANSLATION_NONE;
3074 else if (!strcmp(p, "lba"))
3075 translation = BIOS_ATA_TRANSLATION_LBA;
3076 else if (!strcmp(p, "auto"))
3077 translation = BIOS_ATA_TRANSLATION_AUTO;
3078 else
3079 goto chs_fail;
3080 } else if (*p != '\0') {
bellardc4b1fcc2004-03-14 21:44:30 +00003081 chs_fail:
bellard46d47672004-11-16 01:45:27 +00003082 fprintf(stderr, "qemu: invalid physical CHS format\n");
3083 exit(1);
bellardc4b1fcc2004-03-14 21:44:30 +00003084 }
malc9f227bc2012-08-27 18:33:22 +04003085 if (hda_opts != NULL) {
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +02003086 char num[16];
3087 snprintf(num, sizeof(num), "%d", cyls);
3088 qemu_opt_set(hda_opts, "cyls", num);
3089 snprintf(num, sizeof(num), "%d", heads);
3090 qemu_opt_set(hda_opts, "heads", num);
3091 snprintf(num, sizeof(num), "%d", secs);
3092 qemu_opt_set(hda_opts, "secs", num);
3093 if (translation == BIOS_ATA_TRANSLATION_LBA)
3094 qemu_opt_set(hda_opts, "trans", "lba");
3095 if (translation == BIOS_ATA_TRANSLATION_NONE)
3096 qemu_opt_set(hda_opts, "trans", "none");
3097 }
bellard330d0412003-07-26 18:11:40 +00003098 }
3099 break;
aliguori268a3622009-04-21 22:30:27 +00003100 case QEMU_OPTION_numa:
aliguori268a3622009-04-21 22:30:27 +00003101 numa_add(optarg);
3102 break;
Jes Sorensen1472a952011-03-16 13:33:31 +01003103 case QEMU_OPTION_display:
3104 display_type = select_display(optarg);
3105 break;
bellardcd6f1162004-05-13 22:02:20 +00003106 case QEMU_OPTION_nographic:
Anthony Liguori993fbfd2009-05-21 16:54:00 -05003107 display_type = DT_NOGRAPHIC;
bellarda20dd502003-09-30 21:07:02 +00003108 break;
balrog4d3b6f62008-02-10 16:33:14 +00003109 case QEMU_OPTION_curses:
Jes Sorensen47b05362011-03-16 13:33:35 +01003110#ifdef CONFIG_CURSES
Anthony Liguori993fbfd2009-05-21 16:54:00 -05003111 display_type = DT_CURSES;
Jes Sorensen47b05362011-03-16 13:33:35 +01003112#else
3113 fprintf(stderr, "Curses support is disabled\n");
3114 exit(1);
balrog4d3b6f62008-02-10 16:33:14 +00003115#endif
Jes Sorensen47b05362011-03-16 13:33:35 +01003116 break;
balroga171fe32007-04-30 01:48:07 +00003117 case QEMU_OPTION_portrait:
Vasily Khoruzhick93128052011-06-17 13:04:36 +03003118 graphic_rotate = 90;
3119 break;
3120 case QEMU_OPTION_rotate:
3121 graphic_rotate = strtol(optarg, (char **) &optarg, 10);
3122 if (graphic_rotate != 0 && graphic_rotate != 90 &&
3123 graphic_rotate != 180 && graphic_rotate != 270) {
3124 fprintf(stderr,
3125 "qemu: only 90, 180, 270 deg rotation is available\n");
3126 exit(1);
3127 }
balroga171fe32007-04-30 01:48:07 +00003128 break;
bellardcd6f1162004-05-13 22:02:20 +00003129 case QEMU_OPTION_kernel:
Peter Maydella0abe472012-02-08 05:41:39 +00003130 qemu_opts_set(qemu_find_opts("machine"), 0, "kernel", optarg);
3131 break;
3132 case QEMU_OPTION_initrd:
3133 qemu_opts_set(qemu_find_opts("machine"), 0, "initrd", optarg);
bellarda20dd502003-09-30 21:07:02 +00003134 break;
bellardcd6f1162004-05-13 22:02:20 +00003135 case QEMU_OPTION_append:
Peter Maydella0abe472012-02-08 05:41:39 +00003136 qemu_opts_set(qemu_find_opts("machine"), 0, "append", optarg);
bellard313aa562003-08-10 21:52:11 +00003137 break;
Grant Likely412beee2012-03-02 11:56:38 +00003138 case QEMU_OPTION_dtb:
3139 qemu_opts_set(qemu_find_opts("machine"), 0, "dtb", optarg);
3140 break;
bellardcd6f1162004-05-13 22:02:20 +00003141 case QEMU_OPTION_cdrom:
Markus Armbruster2292dda2011-01-28 11:21:41 +01003142 drive_add(IF_DEFAULT, 2, optarg, CDROM_OPTS);
bellard36b486b2003-11-11 13:36:08 +00003143 break;
bellardcd6f1162004-05-13 22:02:20 +00003144 case QEMU_OPTION_boot:
j_mayer28c5af52007-11-11 01:50:45 +00003145 {
Jan Kiszkaef3adf62009-07-02 00:19:02 +02003146 static const char * const params[] = {
wayne3d3b8302011-07-27 18:04:55 +08003147 "order", "once", "menu",
Amos Kongac05f342012-09-07 11:11:03 +08003148 "splash", "splash-time",
Amos Kongc8a6ae82013-03-19 14:23:27 +08003149 "reboot-timeout", "strict", NULL
Jan Kiszkaef3adf62009-07-02 00:19:02 +02003150 };
3151 char buf[sizeof(boot_devices)];
Jan Kiszkae0f084b2009-07-02 00:19:02 +02003152 char *standard_boot_devices;
Jan Kiszkaef3adf62009-07-02 00:19:02 +02003153 int legacy = 0;
3154
3155 if (!strchr(optarg, '=')) {
3156 legacy = 1;
3157 pstrcpy(buf, sizeof(buf), optarg);
3158 } else if (check_params(buf, sizeof(buf), params, optarg) < 0) {
3159 fprintf(stderr,
3160 "qemu: unknown boot parameter '%s' in '%s'\n",
3161 buf, optarg);
3162 exit(1);
3163 }
3164
3165 if (legacy ||
3166 get_param_value(buf, sizeof(buf), "order", optarg)) {
Eduardo Habkost4e9e9d62010-04-06 19:22:08 -03003167 validate_bootdevices(buf);
Jan Kiszkaef3adf62009-07-02 00:19:02 +02003168 pstrcpy(boot_devices, sizeof(boot_devices), buf);
j_mayer28c5af52007-11-11 01:50:45 +00003169 }
Jan Kiszkae0f084b2009-07-02 00:19:02 +02003170 if (!legacy) {
3171 if (get_param_value(buf, sizeof(buf),
3172 "once", optarg)) {
Eduardo Habkost4e9e9d62010-04-06 19:22:08 -03003173 validate_bootdevices(buf);
Anthony Liguori7267c092011-08-20 22:09:37 -05003174 standard_boot_devices = g_strdup(boot_devices);
Jan Kiszkae0f084b2009-07-02 00:19:02 +02003175 pstrcpy(boot_devices, sizeof(boot_devices), buf);
3176 qemu_register_reset(restore_boot_devices,
3177 standard_boot_devices);
3178 }
Jan Kiszka95387492009-07-02 00:19:02 +02003179 if (get_param_value(buf, sizeof(buf),
3180 "menu", optarg)) {
3181 if (!strcmp(buf, "on")) {
3182 boot_menu = 1;
3183 } else if (!strcmp(buf, "off")) {
3184 boot_menu = 0;
3185 } else {
3186 fprintf(stderr,
3187 "qemu: invalid option value '%s'\n",
3188 buf);
3189 exit(1);
3190 }
3191 }
Amos Kongc8a6ae82013-03-19 14:23:27 +08003192 if (get_param_value(buf, sizeof(buf),
3193 "strict", optarg)) {
3194 if (!strcmp(buf, "on")) {
3195 boot_strict = true;
3196 } else if (!strcmp(buf, "off")) {
3197 boot_strict = false;
3198 } else {
3199 fprintf(stderr,
3200 "qemu: invalid option value '%s'\n",
3201 buf);
3202 exit(1);
3203 }
3204 }
Markus Armbruster49295eb2013-02-08 21:22:19 +01003205 if (!qemu_opts_parse(qemu_find_opts("boot-opts"),
3206 optarg, 0)) {
3207 exit(1);
3208 }
Jan Kiszkae0f084b2009-07-02 00:19:02 +02003209 }
bellard36b486b2003-11-11 13:36:08 +00003210 }
3211 break;
bellardcd6f1162004-05-13 22:02:20 +00003212 case QEMU_OPTION_fda:
bellardcd6f1162004-05-13 22:02:20 +00003213 case QEMU_OPTION_fdb:
Markus Armbruster2292dda2011-01-28 11:21:41 +01003214 drive_add(IF_FLOPPY, popt->index - QEMU_OPTION_fda,
3215 optarg, FD_OPTS);
bellardc45886d2004-01-05 00:02:06 +00003216 break;
bellard52ca8d62006-06-14 16:03:05 +00003217 case QEMU_OPTION_no_fd_bootchk:
3218 fd_bootchk = 0;
3219 break;
Mark McLoughlina1ea4582009-10-08 19:58:26 +01003220 case QEMU_OPTION_netdev:
Gerd Hoffmann3329f072010-08-20 13:52:01 +02003221 if (net_client_parse(qemu_find_opts("netdev"), optarg) == -1) {
Mark McLoughlina1ea4582009-10-08 19:58:26 +01003222 exit(1);
3223 }
3224 break;
bellard7c9d8e02005-11-15 22:16:05 +00003225 case QEMU_OPTION_net:
Gerd Hoffmann3329f072010-08-20 13:52:01 +02003226 if (net_client_parse(qemu_find_opts("net"), optarg) == -1) {
bellardc4b1fcc2004-03-14 21:44:30 +00003227 exit(1);
3228 }
bellard702c6512004-04-02 21:21:32 +00003229 break;
Ronnie Sahlbergf9dadc92012-01-26 09:39:02 +11003230#ifdef CONFIG_LIBISCSI
3231 case QEMU_OPTION_iscsi:
3232 opts = qemu_opts_parse(qemu_find_opts("iscsi"), optarg, 0);
3233 if (!opts) {
3234 exit(1);
3235 }
3236 break;
3237#endif
bellardc7f74642004-08-24 21:57:12 +00003238#ifdef CONFIG_SLIRP
3239 case QEMU_OPTION_tftp:
Jan Kiszkaad196a92009-06-24 14:42:28 +02003240 legacy_tftp_prefix = optarg;
bellard9bf05442004-08-25 22:12:49 +00003241 break;
ths47d5d012007-02-20 00:05:08 +00003242 case QEMU_OPTION_bootp:
Jan Kiszkaad196a92009-06-24 14:42:28 +02003243 legacy_bootp_filename = optarg;
ths47d5d012007-02-20 00:05:08 +00003244 break;
bellard9bf05442004-08-25 22:12:49 +00003245 case QEMU_OPTION_redir:
Markus Armbruster07527062009-10-06 12:16:57 +01003246 if (net_slirp_redir(optarg) < 0)
3247 exit(1);
bellard9bf05442004-08-25 22:12:49 +00003248 break;
bellardc7f74642004-08-24 21:57:12 +00003249#endif
balrogdc72ac12008-11-09 00:04:26 +00003250 case QEMU_OPTION_bt:
Gerd Hoffmannbd3c9482009-07-15 13:59:26 +02003251 add_device_config(DEV_BT, optarg);
balrogdc72ac12008-11-09 00:04:26 +00003252 break;
bellard1d14ffa2005-10-30 18:58:22 +00003253 case QEMU_OPTION_audio_help:
Blue Swirlad960902010-03-29 19:23:52 +00003254 if (!(audio_available())) {
3255 printf("Option %s not supported for this target\n", popt->name);
3256 exit(1);
3257 }
bellard1d14ffa2005-10-30 18:58:22 +00003258 AUD_help ();
3259 exit (0);
3260 break;
3261 case QEMU_OPTION_soundhw:
Blue Swirlad960902010-03-29 19:23:52 +00003262 if (!(audio_available())) {
3263 printf("Option %s not supported for this target\n", popt->name);
3264 exit(1);
3265 }
bellard1d14ffa2005-10-30 18:58:22 +00003266 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;
3303#ifdef MAP_POPULATE
3304 case QEMU_OPTION_mem_prealloc:
3305 mem_prealloc = 1;
3306 break;
3307#endif
bellardcd6f1162004-05-13 22:02:20 +00003308 case QEMU_OPTION_d:
Matthew Fernandezc235d732011-06-07 16:32:40 +00003309 log_mask = optarg;
3310 break;
3311 case QEMU_OPTION_D:
3312 log_file = optarg;
bellardcd6f1162004-05-13 22:02:20 +00003313 break;
bellardcd6f1162004-05-13 22:02:20 +00003314 case QEMU_OPTION_s:
Markus Armbrusteref0c4a02012-02-07 15:09:13 +01003315 add_device_config(DEV_GDB, "tcp::" DEFAULT_GDBSTUB_PORT);
bellardcd6f1162004-05-13 22:02:20 +00003316 break;
aliguori59030a82009-04-05 18:43:41 +00003317 case QEMU_OPTION_gdb:
Markus Armbrusteref0c4a02012-02-07 15:09:13 +01003318 add_device_config(DEV_GDB, optarg);
bellardcd6f1162004-05-13 22:02:20 +00003319 break;
bellardcd6f1162004-05-13 22:02:20 +00003320 case QEMU_OPTION_L:
Gerd Hoffmann45240512013-03-08 11:42:24 +01003321 if (data_dir_idx < ARRAY_SIZE(data_dir)) {
3322 data_dir[data_dir_idx++] = optarg;
3323 }
bellardcd6f1162004-05-13 22:02:20 +00003324 break;
j_mayer1192dad2007-10-05 13:08:35 +00003325 case QEMU_OPTION_bios:
3326 bios_name = optarg;
3327 break;
aurel321b530a62009-04-05 20:08:59 +00003328 case QEMU_OPTION_singlestep:
3329 singlestep = 1;
3330 break;
bellardcd6f1162004-05-13 22:02:20 +00003331 case QEMU_OPTION_S:
pbrook3c07f8e2007-01-21 16:47:01 +00003332 autostart = 0;
bellardcd6f1162004-05-13 22:02:20 +00003333 break;
bellard3d11d0e2004-12-12 16:56:30 +00003334 case QEMU_OPTION_k:
3335 keyboard_layout = optarg;
3336 break;
bellardee22c2f2004-06-03 12:49:50 +00003337 case QEMU_OPTION_localtime:
3338 rtc_utc = 0;
3339 break;
malc3893c122008-09-28 00:42:05 +00003340 case QEMU_OPTION_vga:
Blue Swirla369da52011-09-27 19:15:42 +00003341 vga_model = optarg;
Paolo Bonzini7f1b17f2012-05-10 09:39:17 +02003342 default_vga = 0;
bellard1bfe8562004-07-08 21:17:50 +00003343 break;
bellarde9b137c2004-06-21 16:46:10 +00003344 case QEMU_OPTION_g:
3345 {
3346 const char *p;
3347 int w, h, depth;
3348 p = optarg;
3349 w = strtol(p, (char **)&p, 10);
3350 if (w <= 0) {
3351 graphic_error:
3352 fprintf(stderr, "qemu: invalid resolution or depth\n");
3353 exit(1);
3354 }
3355 if (*p != 'x')
3356 goto graphic_error;
3357 p++;
3358 h = strtol(p, (char **)&p, 10);
3359 if (h <= 0)
3360 goto graphic_error;
3361 if (*p == 'x') {
3362 p++;
3363 depth = strtol(p, (char **)&p, 10);
ths5fafdf22007-09-16 21:08:06 +00003364 if (depth != 8 && depth != 15 && depth != 16 &&
bellarde9b137c2004-06-21 16:46:10 +00003365 depth != 24 && depth != 32)
3366 goto graphic_error;
3367 } else if (*p == '\0') {
3368 depth = graphic_depth;
3369 } else {
3370 goto graphic_error;
3371 }
ths3b46e622007-09-17 08:09:54 +00003372
bellarde9b137c2004-06-21 16:46:10 +00003373 graphic_width = w;
3374 graphic_height = h;
3375 graphic_depth = depth;
3376 }
3377 break;
ths20d8a3e2007-02-18 17:04:49 +00003378 case QEMU_OPTION_echr:
3379 {
3380 char *r;
3381 term_escape_char = strtol(optarg, &r, 0);
3382 if (r == optarg)
3383 printf("Bad argument to echr\n");
3384 break;
3385 }
bellard82c643f2004-07-14 17:28:13 +00003386 case QEMU_OPTION_monitor:
Gerd Hoffmann6ca55822009-12-08 13:11:52 +01003387 monitor_parse(optarg, "readline");
3388 default_monitor = 0;
3389 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));
Dong Xu Wange478b442012-12-06 14:47:22 +08003472 device = qemu_opts_create_nofail(qemu_find_opts("device"));
Stefan Hajnoczie14ea472011-03-16 08:31:43 +00003473 qemu_opt_set(device, "driver", "virtio-9p-pci");
3474 qemu_opt_set(device, "fsdev",
3475 qemu_opt_get(opts, "mount_tag"));
3476 qemu_opt_set(device, "mount_tag",
3477 qemu_opt_get(opts, "mount_tag"));
Gautham R Shenoy3d54abc2010-04-29 17:45:03 +05303478 break;
3479 }
Aneesh Kumar K.V9db221a2011-10-25 12:10:40 +05303480 case QEMU_OPTION_virtfs_synth: {
3481 QemuOpts *fsdev;
3482 QemuOpts *device;
3483
Luiz Capitulino8be7e7e2012-03-20 15:51:57 -03003484 fsdev = qemu_opts_create(qemu_find_opts("fsdev"), "v_synth",
3485 1, NULL);
Aneesh Kumar K.V9db221a2011-10-25 12:10:40 +05303486 if (!fsdev) {
3487 fprintf(stderr, "duplicate option: %s\n", "virtfs_synth");
3488 exit(1);
3489 }
3490 qemu_opt_set(fsdev, "fsdriver", "synth");
Aneesh Kumar K.V9db221a2011-10-25 12:10:40 +05303491
Dong Xu Wange478b442012-12-06 14:47:22 +08003492 device = qemu_opts_create_nofail(qemu_find_opts("device"));
Aneesh Kumar K.V9db221a2011-10-25 12:10:40 +05303493 qemu_opt_set(device, "driver", "virtio-9p-pci");
3494 qemu_opt_set(device, "fsdev", "v_synth");
3495 qemu_opt_set(device, "mount_tag", "v_synth");
3496 break;
3497 }
bellard82c643f2004-07-14 17:28:13 +00003498 case QEMU_OPTION_serial:
Gerd Hoffmann998bbd72009-12-08 13:11:41 +01003499 add_device_config(DEV_SERIAL, optarg);
3500 default_serial = 0;
Jan Kiszka18141ed2010-03-07 11:28:40 +01003501 if (strncmp(optarg, "mon:", 4) == 0) {
3502 default_monitor = 0;
3503 }
bellard82c643f2004-07-14 17:28:13 +00003504 break;
Richard W.M. Jones9dd986c2009-04-25 13:56:19 +01003505 case QEMU_OPTION_watchdog:
Markus Armbruster09aaa162009-08-21 10:31:34 +02003506 if (watchdog) {
3507 fprintf(stderr,
3508 "qemu: only one watchdog option may be given\n");
3509 return 1;
3510 }
3511 watchdog = optarg;
Richard W.M. Jones9dd986c2009-04-25 13:56:19 +01003512 break;
3513 case QEMU_OPTION_watchdog_action:
3514 if (select_watchdog_action(optarg) == -1) {
3515 fprintf(stderr, "Unknown -watchdog-action parameter\n");
3516 exit(1);
3517 }
3518 break;
aliguori51ecf132009-01-15 20:06:40 +00003519 case QEMU_OPTION_virtiocon:
Gerd Hoffmannaee1b932009-12-08 13:11:53 +01003520 add_device_config(DEV_VIRTCON, optarg);
3521 default_virtcon = 0;
Jan Kiszka18141ed2010-03-07 11:28:40 +01003522 if (strncmp(optarg, "mon:", 4) == 0) {
3523 default_monitor = 0;
3524 }
aliguori51ecf132009-01-15 20:06:40 +00003525 break;
bellard6508fe52005-01-15 12:02:56 +00003526 case QEMU_OPTION_parallel:
Gerd Hoffmann6a5e8b02009-12-08 13:11:42 +01003527 add_device_config(DEV_PARALLEL, optarg);
3528 default_parallel = 0;
Jan Kiszka18141ed2010-03-07 11:28:40 +01003529 if (strncmp(optarg, "mon:", 4) == 0) {
3530 default_monitor = 0;
3531 }
bellard6508fe52005-01-15 12:02:56 +00003532 break;
H. Peter Anvinc9f398e2009-12-29 13:51:36 -08003533 case QEMU_OPTION_debugcon:
3534 add_device_config(DEV_DEBUGCON, optarg);
3535 break;
bellardd63d3072004-10-03 13:29:03 +00003536 case QEMU_OPTION_loadvm:
3537 loadvm = optarg;
3538 break;
3539 case QEMU_OPTION_full_screen:
3540 full_screen = 1;
3541 break;
ths667acca2006-12-11 02:08:05 +00003542#ifdef CONFIG_SDL
ths43523e92007-02-18 18:19:32 +00003543 case QEMU_OPTION_no_frame:
3544 no_frame = 1;
3545 break;
ths3780e192007-06-21 21:08:02 +00003546 case QEMU_OPTION_alt_grab:
3547 alt_grab = 1;
3548 break;
Dustin Kirkland0ca9f8a2009-09-17 15:48:04 -05003549 case QEMU_OPTION_ctrl_grab:
3550 ctrl_grab = 1;
3551 break;
ths667acca2006-12-11 02:08:05 +00003552 case QEMU_OPTION_no_quit:
3553 no_quit = 1;
3554 break;
aliguori7d957bd2009-01-15 22:14:11 +00003555 case QEMU_OPTION_sdl:
Anthony Liguori993fbfd2009-05-21 16:54:00 -05003556 display_type = DT_SDL;
aliguori7d957bd2009-01-15 22:14:11 +00003557 break;
Jes Sorensen58fc0962011-03-16 13:33:34 +01003558#else
3559 case QEMU_OPTION_no_frame:
3560 case QEMU_OPTION_alt_grab:
3561 case QEMU_OPTION_ctrl_grab:
3562 case QEMU_OPTION_no_quit:
3563 case QEMU_OPTION_sdl:
3564 fprintf(stderr, "SDL support is disabled\n");
3565 exit(1);
ths667acca2006-12-11 02:08:05 +00003566#endif
bellardf7cce892004-12-08 22:21:25 +00003567 case QEMU_OPTION_pidfile:
ths93815bc2007-03-19 15:58:31 +00003568 pid_file = optarg;
bellardf7cce892004-12-08 22:21:25 +00003569 break;
bellarda09db212005-04-30 16:10:35 +00003570 case QEMU_OPTION_win2k_hack:
3571 win2k_install_hack = 1;
3572 break;
Jan Kiszka433acf02012-01-23 20:15:12 +01003573 case QEMU_OPTION_rtc_td_hack: {
3574 static GlobalProperty slew_lost_ticks[] = {
3575 {
3576 .driver = "mc146818rtc",
3577 .property = "lost_tick_policy",
3578 .value = "slew",
3579 },
3580 { /* end of list */ }
3581 };
3582
3583 qdev_prop_register_global_list(slew_lost_ticks);
aliguori73822ec2009-01-15 20:11:34 +00003584 break;
Jan Kiszka433acf02012-01-23 20:15:12 +01003585 }
aliguori8a92ea22009-02-27 20:12:36 +00003586 case QEMU_OPTION_acpitable:
Blue Swirlde06f8d2010-03-29 19:23:50 +00003587 do_acpitable_option(optarg);
aliguori8a92ea22009-02-27 20:12:36 +00003588 break;
aliguorib6f6e3d2009-04-17 18:59:56 +00003589 case QEMU_OPTION_smbios:
Blue Swirlde06f8d2010-03-29 19:23:50 +00003590 do_smbios_option(optarg);
aliguorib6f6e3d2009-04-17 18:59:56 +00003591 break;
aliguori7ba1e612008-11-05 16:04:33 +00003592 case QEMU_OPTION_enable_kvm:
Anthony PERARD303d4e82010-09-21 20:05:31 +01003593 olist = qemu_find_opts("machine");
Anthony PERARD303d4e82010-09-21 20:05:31 +01003594 qemu_opts_parse(olist, "accel=kvm", 0);
3595 break;
3596 case QEMU_OPTION_machine:
3597 olist = qemu_find_opts("machine");
Jan Kiszka9052ea62011-07-23 12:38:37 +02003598 opts = qemu_opts_parse(olist, optarg, 1);
Anthony PERARD303d4e82010-09-21 20:05:31 +01003599 if (!opts) {
Anthony PERARD303d4e82010-09-21 20:05:31 +01003600 exit(1);
3601 }
Jan Kiszka2645c6d2011-07-25 18:11:20 +02003602 optarg = qemu_opt_get(opts, "type");
3603 if (optarg) {
3604 machine = machine_parse(optarg);
3605 }
aliguori7ba1e612008-11-05 16:04:33 +00003606 break;
Jan Kiszkaa0dac022012-10-05 14:51:45 -03003607 case QEMU_OPTION_no_kvm:
3608 olist = qemu_find_opts("machine");
3609 qemu_opts_parse(olist, "accel=tcg", 0);
3610 break;
Jan Kiszka4086bde2012-10-05 14:51:41 -03003611 case QEMU_OPTION_no_kvm_pit: {
3612 fprintf(stderr, "Warning: KVM PIT can no longer be disabled "
3613 "separately.\n");
3614 break;
3615 }
Jan Kiszkac21fb4f2012-10-05 14:51:42 -03003616 case QEMU_OPTION_no_kvm_pit_reinjection: {
3617 static GlobalProperty kvm_pit_lost_tick_policy[] = {
3618 {
3619 .driver = "kvm-pit",
3620 .property = "lost_tick_policy",
3621 .value = "discard",
3622 },
3623 { /* end of list */ }
3624 };
3625
3626 fprintf(stderr, "Warning: option deprecated, use "
3627 "lost_tick_policy property of kvm-pit instead.\n");
3628 qdev_prop_register_global_list(kvm_pit_lost_tick_policy);
3629 break;
3630 }
bellardbb36d472005-11-05 14:22:28 +00003631 case QEMU_OPTION_usb:
Peter Maydellfa5358c2012-11-22 16:48:45 +00003632 olist = qemu_find_opts("machine");
3633 qemu_opts_parse(olist, "usb=on", 0);
bellardbb36d472005-11-05 14:22:28 +00003634 break;
bellarda594cfb2005-11-06 16:13:29 +00003635 case QEMU_OPTION_usbdevice:
Peter Maydellfa5358c2012-11-22 16:48:45 +00003636 olist = qemu_find_opts("machine");
3637 qemu_opts_parse(olist, "usb=on", 0);
Gerd Hoffmannbd3c9482009-07-15 13:59:26 +02003638 add_device_config(DEV_USB, optarg);
3639 break;
3640 case QEMU_OPTION_device:
Gerd Hoffmann3329f072010-08-20 13:52:01 +02003641 if (!qemu_opts_parse(qemu_find_opts("device"), optarg, 1)) {
Gerd Hoffmannf31d07d2009-07-31 12:25:37 +02003642 exit(1);
3643 }
bellarda594cfb2005-11-06 16:13:29 +00003644 break;
bellard6a00d602005-11-21 23:25:50 +00003645 case QEMU_OPTION_smp:
Andre Przywaradc6b1c02009-08-19 15:42:40 +02003646 smp_parse(optarg);
aliguorib2097002008-10-07 20:39:39 +00003647 if (smp_cpus < 1) {
bellard6a00d602005-11-21 23:25:50 +00003648 fprintf(stderr, "Invalid number of CPUs\n");
3649 exit(1);
3650 }
Jes Sorensen6be68d72009-07-23 17:03:42 +02003651 if (max_cpus < smp_cpus) {
3652 fprintf(stderr, "maxcpus must be equal to or greater than "
3653 "smp\n");
3654 exit(1);
3655 }
3656 if (max_cpus > 255) {
3657 fprintf(stderr, "Unsupported number of maxcpus\n");
3658 exit(1);
3659 }
bellard6a00d602005-11-21 23:25:50 +00003660 break;
bellard24236862006-04-30 21:28:36 +00003661 case QEMU_OPTION_vnc:
Jes Sorensen821601e2011-03-16 13:33:36 +01003662#ifdef CONFIG_VNC
Gerd Hoffmann6b62dc22010-08-25 10:51:06 +02003663 display_remote++;
Jes Sorensen821601e2011-03-16 13:33:36 +01003664 vnc_display = optarg;
3665#else
3666 fprintf(stderr, "VNC support is disabled\n");
3667 exit(1);
3668#endif
3669 break;
bellard6515b202006-05-03 22:02:44 +00003670 case QEMU_OPTION_no_acpi:
3671 acpi_enabled = 0;
3672 break;
aliguori16b29ae2008-12-17 23:28:44 +00003673 case QEMU_OPTION_no_hpet:
3674 no_hpet = 1;
3675 break;
Markus Armbruster7d4c3d52009-06-26 19:15:14 +02003676 case QEMU_OPTION_balloon:
3677 if (balloon_parse(optarg) < 0) {
3678 fprintf(stderr, "Unknown -balloon argument %s\n", optarg);
3679 exit(1);
3680 }
Eduardo Habkostdf97b922009-06-10 16:34:08 -03003681 break;
bellardd1beab82006-10-02 19:44:22 +00003682 case QEMU_OPTION_no_reboot:
3683 no_reboot = 1;
3684 break;
aurel32b2f76162008-04-11 21:35:52 +00003685 case QEMU_OPTION_no_shutdown:
3686 no_shutdown = 1;
3687 break;
balrog9467cd42007-05-01 01:34:14 +00003688 case QEMU_OPTION_show_cursor:
3689 cursor_hide = 0;
3690 break;
blueswir18fcb1b92008-09-18 18:29:08 +00003691 case QEMU_OPTION_uuid:
3692 if(qemu_uuid_parse(optarg, qemu_uuid) < 0) {
3693 fprintf(stderr, "Fail to parse UUID string."
3694 " Wrong format.\n");
3695 exit(1);
3696 }
3697 break;
ths9ae02552007-01-05 17:39:04 +00003698 case QEMU_OPTION_option_rom:
3699 if (nb_option_roms >= MAX_OPTION_ROMS) {
3700 fprintf(stderr, "Too many option ROMs\n");
3701 exit(1);
3702 }
Gleb Natapov2e55e842010-12-08 13:35:07 +02003703 opts = qemu_opts_parse(qemu_find_opts("option-rom"), optarg, 1);
Markus Armbruster49295eb2013-02-08 21:22:19 +01003704 if (!opts) {
3705 exit(1);
3706 }
Gleb Natapov2e55e842010-12-08 13:35:07 +02003707 option_rom[nb_option_roms].name = qemu_opt_get(opts, "romfile");
3708 option_rom[nb_option_roms].bootindex =
3709 qemu_opt_get_number(opts, "bootindex", -1);
3710 if (!option_rom[nb_option_roms].name) {
3711 fprintf(stderr, "Option ROM file is not specified\n");
3712 exit(1);
3713 }
ths9ae02552007-01-05 17:39:04 +00003714 nb_option_roms++;
3715 break;
pbrook8e716212007-01-20 17:12:09 +00003716 case QEMU_OPTION_semihosting:
3717 semihosting_enabled = 1;
3718 break;
Jan Kiszka88eed342012-10-05 14:51:44 -03003719 case QEMU_OPTION_tdf:
3720 fprintf(stderr, "Warning: user space PIT time drift fix "
3721 "is no longer supported.\n");
3722 break;
thsc35734b2007-03-19 15:17:08 +00003723 case QEMU_OPTION_name:
Anthony Liguori7267c092011-08-20 22:09:37 -05003724 qemu_name = g_strdup(optarg);
Andi Kleen18894652009-07-02 09:34:17 +02003725 {
3726 char *p = strchr(qemu_name, ',');
3727 if (p != NULL) {
3728 *p++ = 0;
3729 if (strncmp(p, "process=", 8)) {
Aurelien Jarno5697f6a2010-12-27 18:29:20 +01003730 fprintf(stderr, "Unknown subargument %s to -name\n", p);
Andi Kleen18894652009-07-02 09:34:17 +02003731 exit(1);
3732 }
3733 p += 8;
Jes Sorensence798cf2010-06-10 11:42:31 +02003734 os_set_proc_name(p);
Andi Kleen18894652009-07-02 09:34:17 +02003735 }
3736 }
thsc35734b2007-03-19 15:17:08 +00003737 break;
blueswir166508602007-05-01 14:16:52 +00003738 case QEMU_OPTION_prom_env:
3739 if (nb_prom_envs >= MAX_PROM_ENVS) {
3740 fprintf(stderr, "Too many prom variables\n");
3741 exit(1);
3742 }
3743 prom_envs[nb_prom_envs] = optarg;
3744 nb_prom_envs++;
3745 break;
balrog2b8f2d42007-07-27 22:08:46 +00003746 case QEMU_OPTION_old_param:
3747 old_param = 1;
ths05ebd532008-01-08 19:32:16 +00003748 break;
thsf3dcfad2007-08-24 01:26:02 +00003749 case QEMU_OPTION_clock:
3750 configure_alarms(optarg);
3751 break;
bellard7e0af5d02007-11-07 16:24:33 +00003752 case QEMU_OPTION_startdate:
Jan Kiszka1ed2fc12009-09-15 13:36:04 +02003753 configure_rtc_date_offset(optarg, 1);
3754 break;
3755 case QEMU_OPTION_rtc:
Gerd Hoffmann3329f072010-08-20 13:52:01 +02003756 opts = qemu_opts_parse(qemu_find_opts("rtc"), optarg, 0);
Jan Kiszka1ed2fc12009-09-15 13:36:04 +02003757 if (!opts) {
Jan Kiszka1ed2fc12009-09-15 13:36:04 +02003758 exit(1);
bellard7e0af5d02007-11-07 16:24:33 +00003759 }
Jan Kiszka1ed2fc12009-09-15 13:36:04 +02003760 configure_rtc(opts);
bellard7e0af5d02007-11-07 16:24:33 +00003761 break;
bellard26a5f132008-05-28 12:30:31 +00003762 case QEMU_OPTION_tb_size:
Jan Kiszkad5ab9712011-08-02 16:10:21 +02003763 tcg_tb_size = strtol(optarg, NULL, 0);
3764 if (tcg_tb_size < 0) {
3765 tcg_tb_size = 0;
3766 }
bellard26a5f132008-05-28 12:30:31 +00003767 break;
pbrook2e70f6e2008-06-29 01:03:05 +00003768 case QEMU_OPTION_icount:
Paolo Bonzini4e3de9e2010-03-10 11:38:48 +01003769 icount_option = optarg;
pbrook2e70f6e2008-06-29 01:03:05 +00003770 break;
aliguori5bb79102008-10-13 03:12:02 +00003771 case QEMU_OPTION_incoming:
3772 incoming = optarg;
Stefano Stabellini81323a62012-01-18 12:23:13 +00003773 runstate_set(RUN_STATE_INMIGRATE);
aliguori5bb79102008-10-13 03:12:02 +00003774 break;
Gerd Hoffmannd8c208d2009-12-08 13:11:46 +01003775 case QEMU_OPTION_nodefaults:
3776 default_serial = 0;
3777 default_parallel = 0;
Gerd Hoffmannaee1b932009-12-08 13:11:53 +01003778 default_virtcon = 0;
Christian Borntraeger2e788492013-01-25 06:00:19 +00003779 default_sclp = 0;
Gerd Hoffmannd8c208d2009-12-08 13:11:46 +01003780 default_monitor = 0;
Gerd Hoffmanncb4522c2009-12-08 13:11:47 +01003781 default_net = 0;
Gerd Hoffmannac33f8f2009-12-16 14:25:39 +01003782 default_floppy = 0;
3783 default_cdrom = 0;
3784 default_sdcard = 0;
Paolo Bonzini7f1b17f2012-05-10 09:39:17 +02003785 default_vga = 0;
Gerd Hoffmannd8c208d2009-12-08 13:11:46 +01003786 break;
aliguorie37630c2009-04-22 15:19:10 +00003787 case QEMU_OPTION_xen_domid:
Blue Swirlad960902010-03-29 19:23:52 +00003788 if (!(xen_available())) {
3789 printf("Option %s not supported for this target\n", popt->name);
3790 exit(1);
3791 }
aliguorie37630c2009-04-22 15:19:10 +00003792 xen_domid = atoi(optarg);
3793 break;
3794 case QEMU_OPTION_xen_create:
Blue Swirlad960902010-03-29 19:23:52 +00003795 if (!(xen_available())) {
3796 printf("Option %s not supported for this target\n", popt->name);
3797 exit(1);
3798 }
aliguorie37630c2009-04-22 15:19:10 +00003799 xen_mode = XEN_CREATE;
3800 break;
3801 case QEMU_OPTION_xen_attach:
Blue Swirlad960902010-03-29 19:23:52 +00003802 if (!(xen_available())) {
3803 printf("Option %s not supported for this target\n", popt->name);
3804 exit(1);
3805 }
aliguorie37630c2009-04-22 15:19:10 +00003806 xen_mode = XEN_ATTACH;
3807 break;
Prerna Saxenaab6540d2010-08-09 11:48:32 +01003808 case QEMU_OPTION_trace:
Lluíse4858972011-08-31 20:31:03 +02003809 {
Prerna Saxenaab6540d2010-08-09 11:48:32 +01003810 opts = qemu_opts_parse(qemu_find_opts("trace"), optarg, 0);
Lluíse4858972011-08-31 20:31:03 +02003811 if (!opts) {
3812 exit(1);
Prerna Saxenaab6540d2010-08-09 11:48:32 +01003813 }
Lluís23d15e82011-08-31 20:31:31 +02003814 trace_events = qemu_opt_get(opts, "events");
Lluíse4858972011-08-31 20:31:03 +02003815 trace_file = qemu_opt_get(opts, "file");
Prerna Saxenaab6540d2010-08-09 11:48:32 +01003816 break;
Lluíse4858972011-08-31 20:31:03 +02003817 }
Gerd Hoffmann715a6642009-10-14 10:39:28 +02003818 case QEMU_OPTION_readconfig:
3819 {
Kevin Wolfdcfb0932010-03-05 17:25:55 +01003820 int ret = qemu_read_config_file(optarg);
3821 if (ret < 0) {
3822 fprintf(stderr, "read config %s: %s\n", optarg,
3823 strerror(-ret));
Gerd Hoffmann715a6642009-10-14 10:39:28 +02003824 exit(1);
3825 }
Gerd Hoffmann715a6642009-10-14 10:39:28 +02003826 break;
3827 }
Gerd Hoffmann29b00402010-03-11 11:13:27 -03003828 case QEMU_OPTION_spice:
3829 olist = qemu_find_opts("spice");
3830 if (!olist) {
3831 fprintf(stderr, "spice is not supported by this qemu build.\n");
3832 exit(1);
3833 }
3834 opts = qemu_opts_parse(olist, optarg, 0);
3835 if (!opts) {
Gerd Hoffmann29b00402010-03-11 11:13:27 -03003836 exit(1);
3837 }
Gerd Hoffmannf963e4d2013-02-25 16:02:30 +01003838 display_remote++;
Gerd Hoffmann29b00402010-03-11 11:13:27 -03003839 break;
Gerd Hoffmann715a6642009-10-14 10:39:28 +02003840 case QEMU_OPTION_writeconfig:
3841 {
3842 FILE *fp;
3843 if (strcmp(optarg, "-") == 0) {
3844 fp = stdout;
3845 } else {
3846 fp = fopen(optarg, "w");
3847 if (fp == NULL) {
3848 fprintf(stderr, "open %s: %s\n", optarg, strerror(errno));
3849 exit(1);
3850 }
3851 }
3852 qemu_config_write(fp);
3853 fclose(fp);
3854 break;
3855 }
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +02003856 case QEMU_OPTION_qtest:
3857 qtest_chrdev = optarg;
3858 break;
3859 case QEMU_OPTION_qtest_log:
3860 qtest_log = optarg;
3861 break;
Eduardo Otubo7d76ad42012-08-14 18:44:08 -03003862 case QEMU_OPTION_sandbox:
3863 opts = qemu_opts_parse(qemu_find_opts("sandbox"), optarg, 1);
3864 if (!opts) {
Markus Armbruster49295eb2013-02-08 21:22:19 +01003865 exit(1);
Eduardo Otubo7d76ad42012-08-14 18:44:08 -03003866 }
3867 break;
Corey Bryant587ed6b2012-10-18 15:19:34 -04003868 case QEMU_OPTION_add_fd:
3869#ifndef _WIN32
3870 opts = qemu_opts_parse(qemu_find_opts("add-fd"), optarg, 0);
3871 if (!opts) {
Markus Armbruster49295eb2013-02-08 21:22:19 +01003872 exit(1);
Corey Bryant587ed6b2012-10-18 15:19:34 -04003873 }
3874#else
3875 error_report("File descriptor passing is disabled on this "
3876 "platform");
3877 exit(1);
3878#endif
3879 break;
Anthony Liguori68d98d32012-06-25 14:36:33 -05003880 case QEMU_OPTION_object:
3881 opts = qemu_opts_parse(qemu_find_opts("object"), optarg, 1);
Markus Armbruster49295eb2013-02-08 21:22:19 +01003882 if (!opts) {
3883 exit(1);
3884 }
Anthony Liguori68d98d32012-06-25 14:36:33 -05003885 break;
Jes Sorensen59a52642010-06-10 11:42:25 +02003886 default:
3887 os_parse_cmd_args(popt->index, optarg);
bellardcd6f1162004-05-13 22:02:20 +00003888 }
bellard0824d6f2003-06-24 13:42:40 +00003889 }
3890 }
Markus Armbruster0f0bc3f2010-02-18 20:13:51 +01003891 loc_set_none();
bellard330d0412003-07-26 18:11:40 +00003892
Paolo Bonzini1c537862012-10-30 00:17:12 +01003893 if (qemu_init_main_loop()) {
3894 fprintf(stderr, "qemu_init_main_loop failed\n");
3895 exit(1);
3896 }
3897
Eduardo Otubo7d76ad42012-08-14 18:44:08 -03003898 if (qemu_opts_foreach(qemu_find_opts("sandbox"), parse_sandbox, NULL, 0)) {
3899 exit(1);
3900 }
3901
Corey Bryant587ed6b2012-10-18 15:19:34 -04003902#ifndef _WIN32
3903 if (qemu_opts_foreach(qemu_find_opts("add-fd"), parse_add_fd, NULL, 1)) {
3904 exit(1);
3905 }
3906
3907 if (qemu_opts_foreach(qemu_find_opts("add-fd"), cleanup_add_fd, NULL, 1)) {
3908 exit(1);
3909 }
3910#endif
3911
Dunrong Huangfb7c2692012-07-24 00:42:20 +08003912 if (machine == NULL) {
3913 fprintf(stderr, "No machine found.\n");
3914 exit(1);
3915 }
3916
Crístian Viana93bfef42012-05-30 00:35:51 -03003917 if (machine->hw_version) {
3918 qemu_set_version(machine->hw_version);
3919 }
3920
Anthony Liguori68d98d32012-06-25 14:36:33 -05003921 if (qemu_opts_foreach(qemu_find_opts("object"),
3922 object_create, NULL, 0) != 0) {
3923 exit(1);
3924 }
3925
Eduardo Habkostecf40be2012-03-09 16:19:07 -03003926 /* Init CPU def lists, based on config
3927 * - Must be called after all the qemu_read_config_file() calls
3928 * - Must be called before list_cpus()
3929 * - Must be called before machine->init()
3930 */
3931 cpudef_init();
3932
Peter Maydellc8057f92012-08-02 13:45:54 +01003933 if (cpu_model && is_help_option(cpu_model)) {
Eduardo Habkost1d6528a2012-03-21 09:33:40 -03003934 list_cpus(stdout, &fprintf, cpu_model);
Eduardo Habkostecf40be2012-03-09 16:19:07 -03003935 exit(0);
3936 }
3937
Matthew Fernandezc235d732011-06-07 16:32:40 +00003938 /* Open the logfile at this point, if necessary. We can't open the logfile
3939 * when encountering either of the logging options (-d or -D) because the
3940 * other one may be encountered later on the command line, changing the
3941 * location or level of logging.
3942 */
3943 if (log_mask) {
Peter Maydellb946bff2013-02-11 16:41:24 +00003944 int mask;
Matthew Fernandezc235d732011-06-07 16:32:40 +00003945 if (log_file) {
Peter Maydell9a7e5422013-02-11 16:41:20 +00003946 qemu_set_log_filename(log_file);
Matthew Fernandezc235d732011-06-07 16:32:40 +00003947 }
Peter Maydellb946bff2013-02-11 16:41:24 +00003948
3949 mask = qemu_str_to_log_mask(log_mask);
3950 if (!mask) {
3951 qemu_print_log_usage(stdout);
3952 exit(1);
3953 }
3954 qemu_set_log(mask);
Matthew Fernandezc235d732011-06-07 16:32:40 +00003955 }
3956
Lluís23d15e82011-08-31 20:31:31 +02003957 if (!trace_backend_init(trace_events, trace_file)) {
Lluíse4858972011-08-31 20:31:03 +02003958 exit(1);
Stefan Hajnoczi31d3c9b2011-03-13 20:14:30 +00003959 }
Stefan Hajnoczi0b5538c2011-02-26 18:38:39 +00003960
Paul Brook5cea8592009-05-30 00:52:44 +01003961 /* If no data_dir is specified then try to find it relative to the
3962 executable path. */
Gerd Hoffmann45240512013-03-08 11:42:24 +01003963 if (data_dir_idx < ARRAY_SIZE(data_dir)) {
3964 data_dir[data_dir_idx] = os_find_datadir(argv[0]);
3965 if (data_dir[data_dir_idx] != NULL) {
3966 data_dir_idx++;
3967 }
Paul Brook5cea8592009-05-30 00:52:44 +01003968 }
Stefan Weil60474fb2011-09-04 15:17:46 +02003969 /* If all else fails use the install path specified when building. */
Gerd Hoffmann45240512013-03-08 11:42:24 +01003970 if (data_dir_idx < ARRAY_SIZE(data_dir)) {
3971 data_dir[data_dir_idx++] = CONFIG_QEMU_DATADIR;
Paul Brook5cea8592009-05-30 00:52:44 +01003972 }
3973
Jes Sorensen6be68d72009-07-23 17:03:42 +02003974 /*
3975 * Default to max_cpus = smp_cpus, in case the user doesn't
3976 * specify a max_cpus value.
3977 */
3978 if (!max_cpus)
3979 max_cpus = smp_cpus;
3980
balrog3d878ca2008-10-28 10:59:59 +00003981 machine->max_cpus = machine->max_cpus ?: 1; /* Default to UP */
aliguorib2097002008-10-07 20:39:39 +00003982 if (smp_cpus > machine->max_cpus) {
3983 fprintf(stderr, "Number of SMP cpus requested (%d), exceeds max cpus "
3984 "supported by machine `%s' (%d)\n", smp_cpus, machine->name,
3985 machine->max_cpus);
3986 exit(1);
3987 }
3988
Anthony PERARD67b724e2010-11-22 15:44:15 +00003989 /*
3990 * Get the default machine options from the machine if it is not already
3991 * specified either by the configuration file or by the command line.
3992 */
3993 if (machine->default_machine_opts) {
Jan Kiszka25de5932012-01-27 19:55:43 +01003994 qemu_opts_set_defaults(qemu_find_opts("machine"),
3995 machine->default_machine_opts, 0);
Anthony PERARD67b724e2010-11-22 15:44:15 +00003996 }
3997
Gerd Hoffmann3329f072010-08-20 13:52:01 +02003998 qemu_opts_foreach(qemu_find_opts("device"), default_driver_check, NULL, 0);
3999 qemu_opts_foreach(qemu_find_opts("global"), default_driver_check, NULL, 0);
Gerd Hoffmann998bbd72009-12-08 13:11:41 +01004000
Gerd Hoffmann986c5f72009-12-08 13:11:54 +01004001 if (machine->no_serial) {
4002 default_serial = 0;
4003 }
4004 if (machine->no_parallel) {
4005 default_parallel = 0;
4006 }
4007 if (!machine->use_virtcon) {
4008 default_virtcon = 0;
4009 }
Alexander Graf3ef669e2013-01-24 12:18:52 +01004010 if (!machine->use_sclp) {
4011 default_sclp = 0;
4012 }
Gerd Hoffmannac33f8f2009-12-16 14:25:39 +01004013 if (machine->no_floppy) {
4014 default_floppy = 0;
4015 }
4016 if (machine->no_cdrom) {
4017 default_cdrom = 0;
4018 }
4019 if (machine->no_sdcard) {
4020 default_sdcard = 0;
4021 }
Gerd Hoffmann986c5f72009-12-08 13:11:54 +01004022
Michael Tokarevab51b1d2012-12-30 12:48:14 +04004023 if (is_daemonized()) {
4024 /* According to documentation and historically, -nographic redirects
4025 * serial port, parallel port and monitor to stdio, which does not work
4026 * with -daemonize. We can redirect these to null instead, but since
4027 * -nographic is legacy, let's just error out.
4028 * We disallow -nographic only if all other ports are not redirected
4029 * explicitly, to not break existing legacy setups which uses
4030 * -nographic _and_ redirects all ports explicitly - this is valid
4031 * usage, -nographic is just a no-op in this case.
4032 */
4033 if (display_type == DT_NOGRAPHIC
4034 && (default_parallel || default_serial
4035 || default_monitor || default_virtcon)) {
4036 fprintf(stderr, "-nographic can not be used with -daemonize\n");
4037 exit(1);
4038 }
4039#ifdef CONFIG_CURSES
4040 if (display_type == DT_CURSES) {
4041 fprintf(stderr, "curses display can not be used with -daemonize\n");
4042 exit(1);
4043 }
4044#endif
4045 }
4046
Anthony Liguori993fbfd2009-05-21 16:54:00 -05004047 if (display_type == DT_NOGRAPHIC) {
Gerd Hoffmann6a5e8b02009-12-08 13:11:42 +01004048 if (default_parallel)
4049 add_device_config(DEV_PARALLEL, "null");
Gerd Hoffmanne1c09172009-12-08 13:11:44 +01004050 if (default_serial && default_monitor) {
4051 add_device_config(DEV_SERIAL, "mon:stdio");
Gerd Hoffmann986c5f72009-12-08 13:11:54 +01004052 } else if (default_virtcon && default_monitor) {
4053 add_device_config(DEV_VIRTCON, "mon:stdio");
Alexander Graf3ef669e2013-01-24 12:18:52 +01004054 } else if (default_sclp && default_monitor) {
4055 add_device_config(DEV_SCLP, "mon:stdio");
Gerd Hoffmanne1c09172009-12-08 13:11:44 +01004056 } else {
4057 if (default_serial)
4058 add_device_config(DEV_SERIAL, "stdio");
Gerd Hoffmann986c5f72009-12-08 13:11:54 +01004059 if (default_virtcon)
4060 add_device_config(DEV_VIRTCON, "stdio");
Alexander Graf3ef669e2013-01-24 12:18:52 +01004061 if (default_sclp) {
4062 add_device_config(DEV_SCLP, "stdio");
4063 }
Gerd Hoffmanne1c09172009-12-08 13:11:44 +01004064 if (default_monitor)
Gerd Hoffmann6ca55822009-12-08 13:11:52 +01004065 monitor_parse("stdio", "readline");
Gerd Hoffmanne1c09172009-12-08 13:11:44 +01004066 }
Gerd Hoffmann998bbd72009-12-08 13:11:41 +01004067 } else {
4068 if (default_serial)
4069 add_device_config(DEV_SERIAL, "vc:80Cx24C");
Gerd Hoffmann6a5e8b02009-12-08 13:11:42 +01004070 if (default_parallel)
4071 add_device_config(DEV_PARALLEL, "vc:80Cx24C");
Gerd Hoffmannabdeed02009-12-08 13:11:43 +01004072 if (default_monitor)
Gerd Hoffmann6ca55822009-12-08 13:11:52 +01004073 monitor_parse("vc:80Cx24C", "readline");
Alexander Graf38536da2009-12-17 13:06:08 +01004074 if (default_virtcon)
4075 add_device_config(DEV_VIRTCON, "vc:80Cx24C");
Alexander Graf3ef669e2013-01-24 12:18:52 +01004076 if (default_sclp) {
4077 add_device_config(DEV_SCLP, "vc:80Cx24C");
4078 }
aliguoribc0129d2008-08-01 15:12:34 +00004079 }
4080
Anthony Liguori15546422013-02-20 07:43:25 -06004081 if (display_type == DT_DEFAULT && !display_remote) {
4082#if defined(CONFIG_GTK)
4083 display_type = DT_GTK;
4084#elif defined(CONFIG_SDL) || defined(CONFIG_COCOA)
4085 display_type = DT_SDL;
4086#elif defined(CONFIG_VNC)
4087 vnc_display = "localhost:0,to=99";
4088 show_vnc_port = 1;
4089#else
4090 display_type = DT_NONE;
4091#endif
4092 }
4093
4094#if defined(CONFIG_GTK)
4095 if (display_type == DT_GTK) {
4096 early_gtk_display_init();
4097 }
4098#endif
4099
TeLeMana5829fd2010-04-15 12:37:55 +08004100 socket_init();
4101
Gerd Hoffmann3329f072010-08-20 13:52:01 +02004102 if (qemu_opts_foreach(qemu_find_opts("chardev"), chardev_init_func, NULL, 1) != 0)
Gerd Hoffmann1a688d32009-12-08 13:11:36 +01004103 exit(1);
Venkateswararao Jujjuri (JV)758e8e32010-06-14 13:34:41 -07004104#ifdef CONFIG_VIRTFS
Gerd Hoffmann3329f072010-08-20 13:52:01 +02004105 if (qemu_opts_foreach(qemu_find_opts("fsdev"), fsdev_init_func, NULL, 1) != 0) {
Gautham R Shenoy74db9202010-04-29 17:44:43 +05304106 exit(1);
4107 }
4108#endif
Gerd Hoffmann1a688d32009-12-08 13:11:36 +01004109
Jes Sorenseneb505be2010-06-10 11:42:28 +02004110 os_daemonize();
ths71e3ceb2006-12-22 02:11:31 +00004111
thsaa26bb22007-03-25 21:33:06 +00004112 if (pid_file && qemu_create_pidfile(pid_file) != 0) {
Jes Sorenseneb505be2010-06-10 11:42:28 +02004113 os_pidfile_error();
ths93815bc2007-03-19 15:58:31 +00004114 exit(1);
4115 }
4116
Jan Kiszka0ac543d2011-08-15 16:18:57 -07004117 /* init the memory */
4118 if (ram_size == 0) {
4119 ram_size = DEFAULT_RAM_SIZE * 1024 * 1024;
4120 }
4121
Bruce Rogers3d1d9652012-08-09 12:47:40 -06004122 if (qemu_opts_foreach(qemu_find_opts("device"), device_help_func, NULL, 0)
4123 != 0) {
4124 exit(0);
4125 }
4126
Anthony PERARD303d4e82010-09-21 20:05:31 +01004127 configure_accelerator();
Marcelo Tosatti214910a2009-09-18 02:41:23 -03004128
Peter Maydell967c0da2012-02-22 22:40:00 +00004129 machine_opts = qemu_opts_find(qemu_find_opts("machine"), 0);
4130 if (machine_opts) {
4131 kernel_filename = qemu_opt_get(machine_opts, "kernel");
4132 initrd_filename = qemu_opt_get(machine_opts, "initrd");
4133 kernel_cmdline = qemu_opt_get(machine_opts, "append");
4134 } else {
4135 kernel_filename = initrd_filename = kernel_cmdline = NULL;
4136 }
4137
Peter Maydella0abe472012-02-08 05:41:39 +00004138 if (!kernel_cmdline) {
4139 kernel_cmdline = "";
4140 }
4141
bellarda20dd502003-09-30 21:07:02 +00004142 linux_boot = (kernel_filename != NULL);
balrog6c41b272007-11-17 12:12:29 +00004143
thsf8d39c02008-07-03 10:01:15 +00004144 if (!linux_boot && *kernel_cmdline != '\0') {
4145 fprintf(stderr, "-append only allowed with -kernel option\n");
4146 exit(1);
4147 }
4148
4149 if (!linux_boot && initrd_filename != NULL) {
4150 fprintf(stderr, "-initrd only allowed with -kernel option\n");
4151 exit(1);
4152 }
4153
Grant Likely412beee2012-03-02 11:56:38 +00004154 if (!linux_boot && machine_opts && qemu_opt_get(machine_opts, "dtb")) {
4155 fprintf(stderr, "-dtb only allowed with -kernel option\n");
4156 exit(1);
4157 }
4158
Jes Sorensen9156d762010-06-10 11:42:30 +02004159 os_set_line_buffering();
ths3b46e622007-09-17 08:09:54 +00004160
Paolo Bonzini49cf5722012-11-02 15:43:24 +01004161 qemu_init_cpu_loop();
4162 qemu_mutex_lock_iothread();
4163
Alon Levyad1be892012-03-14 20:33:37 +02004164#ifdef CONFIG_SPICE
4165 /* spice needs the timers to be initialized by this point */
4166 qemu_spice_init();
4167#endif
4168
Max Filippov0abe9052011-11-10 15:38:42 +04004169 if (icount_option && (kvm_enabled() || xen_enabled())) {
4170 fprintf(stderr, "-icount is not allowed with kvm or xen\n");
4171 exit(1);
4172 }
Paolo Bonzini4e3de9e2010-03-10 11:38:48 +01004173 configure_icount(icount_option);
pbrook634fce92006-07-15 17:40:09 +00004174
Amos Kongf30dbae2012-12-11 22:20:15 +08004175 /* clean up network at qemu process termination */
4176 atexit(&net_cleanup);
4177
Mark McLoughlindc1c9fe2009-10-06 12:17:16 +01004178 if (net_init_clients() < 0) {
4179 exit(1);
bellard702c6512004-04-02 21:21:32 +00004180 }
bellardf1510b22003-06-25 00:07:40 +00004181
Stefan Bergerd1a0cf72013-02-27 12:47:49 -05004182#ifdef CONFIG_TPM
4183 if (tpm_init() < 0) {
4184 exit(1);
4185 }
4186#endif
4187
balrogdc72ac12008-11-09 00:04:26 +00004188 /* init the bluetooth world */
Gerd Hoffmannbd3c9482009-07-15 13:59:26 +02004189 if (foreach_device_config(DEV_BT, bt_parse))
4190 exit(1);
balrogdc72ac12008-11-09 00:04:26 +00004191
Anthony PERARD834e76e2011-07-20 08:17:44 +00004192 if (!xen_enabled()) {
4193 /* On 32-bit hosts, QEMU is limited by virtual address space */
4194 if (ram_size > (2047 << 20) && HOST_LONG_BITS == 32) {
4195 fprintf(stderr, "qemu: at most 2047 MB RAM can be simulated\n");
4196 exit(1);
4197 }
4198 }
balrog7fb4fdc2008-04-24 17:59:27 +00004199
Jan Kiszkad5ab9712011-08-02 16:10:21 +02004200 cpu_exec_init_all();
bellard26a5f132008-05-28 12:30:31 +00004201
Markus Armbrustereb852012009-10-27 18:41:44 +01004202 bdrv_init_with_whitelist();
thse4bcb142007-12-02 04:51:10 +00004203
lirans@il.ibm.comc163b5c2009-11-02 15:40:58 +02004204 blk_mig_init();
4205
ths96d30e42007-01-07 20:42:14 +00004206 /* open the virtual block devices */
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +02004207 if (snapshot)
Gerd Hoffmann3329f072010-08-20 13:52:01 +02004208 qemu_opts_foreach(qemu_find_opts("drive"), drive_enable_snapshot, NULL, 0);
Christian Borntraeger2d0d2832012-11-20 15:30:34 +01004209 if (qemu_opts_foreach(qemu_find_opts("drive"), drive_init_func,
4210 &machine->block_default_type, 1) != 0) {
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +02004211 exit(1);
Christian Borntraeger2d0d2832012-11-20 15:30:34 +01004212 }
balrog3e3d5812007-04-30 02:09:25 +00004213
Christian Borntraeger3c42ea62012-11-22 21:02:55 +01004214 default_drive(default_cdrom, snapshot, machine->block_default_type, 2,
4215 CDROM_OPTS);
4216 default_drive(default_floppy, snapshot, IF_FLOPPY, 0, FD_OPTS);
4217 default_drive(default_sdcard, snapshot, IF_SD, 0, SD_OPTS);
Markus Armbruster4e5d9b52011-01-28 11:21:45 +01004218
Juan Quintela7908c782012-06-26 18:46:10 +02004219 register_savevm_live(NULL, "ram", 0, 4, &savevm_ram_handlers, NULL);
bellard8a7ddc32004-03-31 19:00:16 +00004220
aliguori268a3622009-04-21 22:30:27 +00004221 if (nb_numa_nodes > 0) {
4222 int i;
4223
Sasha Levinea0e5412011-06-29 23:29:39 -04004224 if (nb_numa_nodes > MAX_NODES) {
4225 nb_numa_nodes = MAX_NODES;
aliguori268a3622009-04-21 22:30:27 +00004226 }
4227
4228 /* If no memory size if given for any node, assume the default case
4229 * and distribute the available memory equally across all nodes
4230 */
4231 for (i = 0; i < nb_numa_nodes; i++) {
4232 if (node_mem[i] != 0)
4233 break;
4234 }
4235 if (i == nb_numa_nodes) {
4236 uint64_t usedmem = 0;
4237
4238 /* On Linux, the each node's border has to be 8MB aligned,
4239 * the final node gets the rest.
4240 */
4241 for (i = 0; i < nb_numa_nodes - 1; i++) {
4242 node_mem[i] = (ram_size / nb_numa_nodes) & ~((1 << 23UL) - 1);
4243 usedmem += node_mem[i];
4244 }
4245 node_mem[i] = ram_size - usedmem;
4246 }
4247
4248 for (i = 0; i < nb_numa_nodes; i++) {
Chegu Vinodee785fe2012-07-16 21:31:30 -07004249 if (!bitmap_empty(node_cpumask[i], MAX_CPUMASK_BITS)) {
aliguori268a3622009-04-21 22:30:27 +00004250 break;
Chegu Vinodee785fe2012-07-16 21:31:30 -07004251 }
aliguori268a3622009-04-21 22:30:27 +00004252 }
4253 /* assigning the VCPUs round-robin is easier to implement, guest OSes
4254 * must cope with this anyway, because there are BIOSes out there in
4255 * real machines which also use this scheme.
4256 */
4257 if (i == nb_numa_nodes) {
Vasilis Liaskovitis991dfef2011-10-26 14:19:00 +02004258 for (i = 0; i < max_cpus; i++) {
Chegu Vinodee785fe2012-07-16 21:31:30 -07004259 set_bit(i, node_cpumask[i % nb_numa_nodes]);
aliguori268a3622009-04-21 22:30:27 +00004260 }
4261 }
4262 }
4263
Gerd Hoffmann3329f072010-08-20 13:52:01 +02004264 if (qemu_opts_foreach(qemu_find_opts("mon"), mon_init_func, NULL, 1) != 0) {
Jan Kiszka157b9312010-04-06 16:55:53 +02004265 exit(1);
4266 }
4267
Gerd Hoffmann998bbd72009-12-08 13:11:41 +01004268 if (foreach_device_config(DEV_SERIAL, serial_parse) < 0)
4269 exit(1);
Gerd Hoffmann6a5e8b02009-12-08 13:11:42 +01004270 if (foreach_device_config(DEV_PARALLEL, parallel_parse) < 0)
4271 exit(1);
Gerd Hoffmannaee1b932009-12-08 13:11:53 +01004272 if (foreach_device_config(DEV_VIRTCON, virtcon_parse) < 0)
4273 exit(1);
Alexander Graf3ef669e2013-01-24 12:18:52 +01004274 if (foreach_device_config(DEV_SCLP, sclp_parse) < 0) {
4275 exit(1);
4276 }
H. Peter Anvinc9f398e2009-12-29 13:51:36 -08004277 if (foreach_device_config(DEV_DEBUGCON, debugcon_parse) < 0)
4278 exit(1);
aliguori2796dae2009-01-16 20:23:27 +00004279
Paolo Bonzini7f1b17f2012-05-10 09:39:17 +02004280 /* If no default VGA is requested, the default is "none". */
Aurelien Jarno3605ded2012-09-10 01:01:44 +02004281 if (default_vga) {
4282 if (cirrus_vga_available()) {
4283 vga_model = "cirrus";
4284 } else if (vga_available()) {
4285 vga_model = "std";
4286 }
Blue Swirla369da52011-09-27 19:15:42 +00004287 }
Paolo Bonzini7f1b17f2012-05-10 09:39:17 +02004288 select_vgahw(vga_model);
Blue Swirla369da52011-09-27 19:15:42 +00004289
Markus Armbruster09aaa162009-08-21 10:31:34 +02004290 if (watchdog) {
4291 i = select_watchdog(watchdog);
4292 if (i > 0)
4293 exit (i == 1 ? 1 : 0);
4294 }
4295
Gerd Hoffmannb6b61142009-07-15 13:48:21 +02004296 if (machine->compat_props) {
Gerd Hoffmann458fb672009-12-08 13:11:33 +01004297 qdev_prop_register_global_list(machine->compat_props);
Gerd Hoffmannb6b61142009-07-15 13:48:21 +02004298 }
Gerd Hoffmannd0fef6f2009-12-08 13:11:34 +01004299 qemu_add_globals();
4300
Anthony Liguori1de81d22011-12-19 16:37:46 -06004301 qdev_machine_init();
4302
Eduardo Habkost5f072e12012-10-15 17:22:02 -03004303 QEMUMachineInitArgs args = { .ram_size = ram_size,
Avik Sile4ada292013-01-08 12:36:30 +05304304 .boot_device = (boot_devices[0] == '\0') ?
4305 machine->boot_order :
4306 boot_devices,
Eduardo Habkost5f072e12012-10-15 17:22:02 -03004307 .kernel_filename = kernel_filename,
4308 .kernel_cmdline = kernel_cmdline,
4309 .initrd_filename = initrd_filename,
4310 .cpu_model = cpu_model };
4311 machine->init(&args);
aliguori3023f3322009-01-16 19:04:14 +00004312
Jan Kiszkaea375f92010-03-01 19:10:30 +01004313 cpu_synchronize_all_post_init();
aliguori268a3622009-04-21 22:30:27 +00004314
Blue Swirl87d0a282010-03-27 18:24:45 +00004315 set_numa_modes();
aliguori268a3622009-04-21 22:30:27 +00004316
aliguori6f338c32009-02-11 15:21:54 +00004317 current_machine = machine;
4318
aliguori3023f3322009-01-16 19:04:14 +00004319 /* init USB devices */
zhlcindy@gmail.com094b2872012-09-02 19:25:28 +00004320 if (usb_enabled(false)) {
Markus Armbruster07527062009-10-06 12:16:57 +01004321 if (foreach_device_config(DEV_USB, usb_parse) < 0)
4322 exit(1);
aliguori3023f3322009-01-16 19:04:14 +00004323 }
4324
Gerd Hoffmannbd3c9482009-07-15 13:59:26 +02004325 /* init generic devices */
Gerd Hoffmann3329f072010-08-20 13:52:01 +02004326 if (qemu_opts_foreach(qemu_find_opts("device"), device_init_func, NULL, 1) != 0)
Gerd Hoffmannbd3c9482009-07-15 13:59:26 +02004327 exit(1);
4328
Markus Armbruster668680f2010-02-11 14:44:58 +01004329 net_check_clients();
4330
aliguori3023f3322009-01-16 19:04:14 +00004331 /* just use the first displaystate for the moment */
Paolo Bonzinib473df62010-02-11 00:29:55 +01004332 ds = get_displaystate();
Anthony Liguori993fbfd2009-05-21 16:54:00 -05004333
Gerd Hoffmann6b62dc22010-08-25 10:51:06 +02004334 /* init local displays */
Anthony Liguori993fbfd2009-05-21 16:54:00 -05004335 switch (display_type) {
4336 case DT_NOGRAPHIC:
4337 break;
4338#if defined(CONFIG_CURSES)
4339 case DT_CURSES:
Michael Tokarevab51b1d2012-12-30 12:48:14 +04004340 curses_display_init(ds, full_screen);
Anthony Liguori993fbfd2009-05-21 16:54:00 -05004341 break;
4342#endif
bellard5b0753e2005-03-01 21:37:28 +00004343#if defined(CONFIG_SDL)
Anthony Liguori993fbfd2009-05-21 16:54:00 -05004344 case DT_SDL:
4345 sdl_display_init(ds, full_screen, no_frame);
4346 break;
bellard5b0753e2005-03-01 21:37:28 +00004347#elif defined(CONFIG_COCOA)
Anthony Liguori993fbfd2009-05-21 16:54:00 -05004348 case DT_SDL:
4349 cocoa_display_init(ds, full_screen);
4350 break;
bellard313aa562003-08-10 21:52:11 +00004351#endif
Anthony Liguori15546422013-02-20 07:43:25 -06004352#if defined(CONFIG_GTK)
4353 case DT_GTK:
4354 gtk_display_init(ds);
4355 break;
4356#endif
Gerd Hoffmann6b62dc22010-08-25 10:51:06 +02004357 default:
4358 break;
4359 }
4360
Gleb Natapov0ce235a2011-03-31 11:27:23 +02004361 /* must be after terminal init, SDL library changes signal handlers */
4362 os_setup_signal_handling();
4363
Jes Sorensen821601e2011-03-16 13:33:36 +01004364#ifdef CONFIG_VNC
Gerd Hoffmann6b62dc22010-08-25 10:51:06 +02004365 /* init remote displays */
4366 if (vnc_display) {
Paolo Bonzini2d55f0e2012-10-02 10:17:21 +02004367 Error *local_err = NULL;
Anthony Liguori993fbfd2009-05-21 16:54:00 -05004368 vnc_display_init(ds);
Paolo Bonzini2d55f0e2012-10-02 10:17:21 +02004369 vnc_display_open(ds, vnc_display, &local_err);
4370 if (local_err != NULL) {
4371 fprintf(stderr, "Failed to start VNC server on `%s': %s\n",
4372 vnc_display, error_get_pretty(local_err));
4373 error_free(local_err);
Anthony Liguori993fbfd2009-05-21 16:54:00 -05004374 exit(1);
Amos Kong94b204c2012-06-30 10:02:20 +08004375 }
Anthony Liguorif92f8af2009-05-20 13:01:02 -05004376
Anthony Liguori993fbfd2009-05-21 16:54:00 -05004377 if (show_vnc_port) {
4378 printf("VNC server running on `%s'\n", vnc_display_local_addr(ds));
Anthony Liguorif92f8af2009-05-20 13:01:02 -05004379 }
bellard313aa562003-08-10 21:52:11 +00004380 }
Jes Sorensen821601e2011-03-16 13:33:36 +01004381#endif
Gerd Hoffmanna3e22262010-08-25 15:32:06 +02004382#ifdef CONFIG_SPICE
Gerd Hoffmanna19cbfb2010-04-27 11:50:11 +02004383 if (using_spice && !qxl_enabled) {
Gerd Hoffmanna3e22262010-08-25 15:32:06 +02004384 qemu_spice_display_init(ds);
4385 }
4386#endif
aliguori5b08fc12008-08-21 20:08:03 +00004387
Gerd Hoffmann6b62dc22010-08-25 10:51:06 +02004388 /* display setup */
Paolo Bonzinib473df62010-02-11 00:29:55 +01004389 text_consoles_set_display(ds);
aliguori2796dae2009-01-16 20:23:27 +00004390
Markus Armbrusteref0c4a02012-02-07 15:09:13 +01004391 if (foreach_device_config(DEV_GDB, gdbserver_start) < 0) {
aliguori59030a82009-04-05 18:43:41 +00004392 exit(1);
balrog45669e02007-07-02 13:20:17 +00004393 }
balrog45669e02007-07-02 13:20:17 +00004394
Gerd Hoffmann3418bd22009-09-25 21:42:41 +02004395 qdev_machine_creation_done();
4396
Gerd Hoffmann15ff7702009-12-14 16:07:35 +01004397 if (rom_load_all() != 0) {
4398 fprintf(stderr, "rom loading failed\n");
4399 exit(1);
4400 }
Gerd Hoffmann45a50b12009-10-01 16:42:33 +02004401
Isaku Yamahata80376c32010-12-20 14:33:35 +09004402 /* TODO: once all bus devices are qdevified, this should be done
4403 * when bus is created by qdev.c */
4404 qemu_register_reset(qbus_reset_all_fn, sysbus_get_default());
Gleb Natapov4cab9462010-12-08 13:35:08 +02004405 qemu_run_machine_init_done_notifiers();
4406
Jan Kiszkae063eb12011-06-14 18:29:43 +02004407 qemu_system_reset(VMRESET_SILENT);
Juan Quintela05f24012009-08-20 19:42:22 +02004408 if (loadvm) {
Markus Armbruster03cd4652010-02-17 16:24:10 +01004409 if (load_vmstate(loadvm) < 0) {
Juan Quintela05f24012009-08-20 19:42:22 +02004410 autostart = 0;
4411 }
4412 }
bellardd63d3072004-10-03 13:29:03 +00004413
Glauber Costa2bb8c102009-07-24 16:20:23 -04004414 if (incoming) {
Paolo Bonzini43eaae22012-10-02 18:21:18 +02004415 Error *local_err = NULL;
4416 qemu_start_incoming_migration(incoming, &local_err);
4417 if (local_err) {
4418 fprintf(stderr, "-incoming %s: %s\n", incoming, error_get_pretty(local_err));
4419 error_free(local_err);
4420 exit(1);
Juan Quintela8ca5e802010-06-09 14:10:54 +02004421 }
Avi Kivity6b99dad2009-08-09 14:39:20 +03004422 } else if (autostart) {
aliguoric0f4ce72009-03-05 23:01:01 +00004423 vm_start();
Avi Kivity6b99dad2009-08-09 14:39:20 +03004424 }
thsffd843b2006-12-21 19:46:43 +00004425
Jes Sorenseneb505be2010-06-10 11:42:28 +02004426 os_setup_post();
ths71e3ceb2006-12-22 02:11:31 +00004427
Paolo Bonzini99435902011-09-12 14:03:13 +02004428 resume_all_vcpus();
bellard8a7ddc32004-03-31 19:00:16 +00004429 main_loop();
Paolo Bonzini99435902011-09-12 14:03:13 +02004430 bdrv_close_all();
4431 pause_all_vcpus();
wayne3d3b8302011-07-27 18:04:55 +08004432 res_free();
Stefan Bergerd1a0cf72013-02-27 12:47:49 -05004433#ifdef CONFIG_TPM
4434 tpm_cleanup();
4435#endif
thsb46a8902007-10-21 23:20:45 +00004436
bellard0824d6f2003-06-24 13:42:40 +00004437 return 0;
4438}