blob: a8dc73d61d7c70fafbe22fc9e5b04c56e4e6132a [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"
122#include "hw/baum.h"
123#include "hw/bt.h"
Richard W.M. Jones9dd986c2009-04-25 13:56:19 +0100124#include "hw/watchdog.h"
aliguorib6f6e3d2009-04-17 18:59:56 +0000125#include "hw/smbios.h"
aliguorie37630c2009-04-22 15:19:10 +0000126#include "hw/xen.h"
Gerd Hoffmannbd3c9482009-07-15 13:59:26 +0200127#include "hw/qdev.h"
Gerd Hoffmann45a50b12009-10-01 16:42:33 +0200128#include "hw/loader.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"
Paolo Bonzini9c17d612012-12-17 18:20:04 +0100142#include "sysemu/dma.h"
blueswir1511d2b12009-03-07 15:32:56 +0000143#include "audio/audio.h"
Paolo Bonzinicaf71f82012-12-17 18:19:50 +0100144#include "migration/migration.h"
Paolo Bonzini9c17d612012-12-17 18:20:04 +0100145#include "sysemu/kvm.h"
Paolo Bonzini7b1b5d12012-12-17 18:19:43 +0100146#include "qapi/qmp/qjson.h"
Paolo Bonzini1de7afc2012-12-17 18:20:00 +0100147#include "qemu/option.h"
148#include "qemu/config-file.h"
Jes Sorensen59a52642010-06-10 11:42:25 +0200149#include "qemu-options.h"
Luiz Capitulino1fa9a5e2011-09-12 17:54:20 -0300150#include "qmp-commands.h"
Paolo Bonzini1de7afc2012-12-17 18:20:00 +0100151#include "qemu/main-loop.h"
Venkateswararao Jujjuri (JV)758e8e32010-06-14 13:34:41 -0700152#ifdef CONFIG_VIRTFS
Gautham R Shenoy74db9202010-04-29 17:44:43 +0530153#include "fsdev/qemu-fsdev.h"
154#endif
Paolo Bonzini9c17d612012-12-17 18:20:04 +0100155#include "sysemu/qtest.h"
blueswir1511d2b12009-03-07 15:32:56 +0000156
Paolo Bonzini76cad712012-10-24 11:12:21 +0200157#include "disas/disas.h"
bellardfc01f7e2003-06-30 10:03:06 +0000158
Paolo Bonzini1de7afc2012-12-17 18:20:00 +0100159#include "qemu/sockets.h"
blueswir1511d2b12009-03-07 15:32:56 +0000160
Jan Kiszkad918f232009-06-24 14:42:30 +0200161#include "slirp/libslirp.h"
blueswir1511d2b12009-03-07 15:32:56 +0000162
Stefan Hajnoczi94b0b5f2010-11-16 12:20:25 +0000163#include "trace.h"
Lluíse4858972011-08-31 20:31:03 +0200164#include "trace/control.h"
Paolo Bonzini1de7afc2012-12-17 18:20:00 +0100165#include "qemu/queue.h"
Paolo Bonzini9c17d612012-12-17 18:20:04 +0100166#include "sysemu/cpus.h"
167#include "sysemu/arch_init.h"
Paolo Bonzini1de7afc2012-12-17 18:20:00 +0100168#include "qemu/osdep.h"
Blue Swirl72cf2d42009-09-12 07:36:22 +0000169
Gerd Hoffmann29b00402010-03-11 11:13:27 -0300170#include "ui/qemu-spice.h"
Anthony Liguori68d98d32012-06-25 14:36:33 -0500171#include "qapi/string-input-visitor.h"
Gerd Hoffmann29b00402010-03-11 11:13:27 -0300172
blueswir19dc63a12008-10-04 07:25:46 +0000173//#define DEBUG_NET
174//#define DEBUG_SLIRP
bellard330d0412003-07-26 18:11:40 +0000175
bellard1bfe8562004-07-08 21:17:50 +0000176#define DEFAULT_RAM_SIZE 128
bellard313aa562003-08-10 21:52:11 +0000177
Amit Shah98b19252010-01-20 00:36:52 +0530178#define MAX_VIRTIO_CONSOLES 1
Alexander Graf3ef669e2013-01-24 12:18:52 +0100179#define MAX_SCLP_CONSOLES 1
Amit Shah98b19252010-01-20 00:36:52 +0530180
Paul Brook5cea8592009-05-30 00:52:44 +0100181static const char *data_dir;
j_mayer1192dad2007-10-05 13:08:35 +0000182const char *bios_name = NULL;
malccb5a7aa2008-09-28 00:42:12 +0000183enum vga_retrace_method vga_retrace_method = VGA_RETRACE_DUMB;
Anthony Liguori993fbfd2009-05-21 16:54:00 -0500184DisplayType display_type = DT_DEFAULT;
Blue Swirl4fdcac02012-10-28 11:04:47 +0000185static int display_remote;
bellard3d11d0e2004-12-12 16:56:30 +0000186const char* keyboard_layout = NULL;
Anthony Liguoric227f092009-10-01 16:12:16 -0500187ram_addr_t ram_size;
Marcelo Tosattic9027602010-03-01 20:25:08 -0300188const char *mem_path = NULL;
189#ifdef MAP_POPULATE
190int mem_prealloc = 0; /* force preallocation of physical target memory */
191#endif
bellardc4b1fcc2004-03-14 21:44:30 +0000192int nb_nics;
bellard7c9d8e02005-11-15 22:16:05 +0000193NICInfo nd_table[MAX_NICS];
Paolo Bonzinid399f672009-07-27 23:17:51 +0200194int autostart;
balrogf6503052008-02-17 11:42:19 +0000195static int rtc_utc = 1;
196static int rtc_date_offset = -1; /* -1 means no change */
Jan Kiszka68752042009-09-15 13:36:04 +0200197QEMUClock *rtc_clock;
Gerd Hoffmann64465292009-12-08 13:11:45 +0100198int vga_interface_type = VGA_NONE;
blueswir1dbed7e42008-10-01 19:38:09 +0000199static int full_screen = 0;
blueswir1634a21f2008-11-16 11:34:07 +0000200#ifdef CONFIG_SDL
blueswir1dbed7e42008-10-01 19:38:09 +0000201static int no_frame = 0;
blueswir1634a21f2008-11-16 11:34:07 +0000202#endif
ths667acca2006-12-11 02:08:05 +0000203int no_quit = 0;
bellard8d11df92004-08-24 21:13:40 +0000204CharDriverState *serial_hds[MAX_SERIAL_PORTS];
bellard6508fe52005-01-15 12:02:56 +0000205CharDriverState *parallel_hds[MAX_PARALLEL_PORTS];
aliguori9ede2fd2009-01-15 20:05:25 +0000206CharDriverState *virtcon_hds[MAX_VIRTIO_CONSOLES];
Alexander Graf3ef669e2013-01-24 12:18:52 +0100207CharDriverState *sclp_hds[MAX_SCLP_CONSOLES];
bellarda09db212005-04-30 16:10:35 +0000208int win2k_install_hack = 0;
aurel321b530a62009-04-05 20:08:59 +0000209int singlestep = 0;
bellard6a00d602005-11-21 23:25:50 +0000210int smp_cpus = 1;
Jes Sorensen6be68d72009-07-23 17:03:42 +0200211int max_cpus = 0;
Andre Przywaradc6b1c02009-08-19 15:42:40 +0200212int smp_cores = 1;
213int smp_threads = 1;
Jes Sorensen821601e2011-03-16 13:33:36 +0100214#ifdef CONFIG_VNC
ths73fc9742006-12-22 02:09:07 +0000215const char *vnc_display;
Jes Sorensen821601e2011-03-16 13:33:36 +0100216#endif
bellard6515b202006-05-03 22:02:44 +0000217int acpi_enabled = 1;
aliguori16b29ae2008-12-17 23:28:44 +0000218int no_hpet = 0;
bellard52ca8d62006-06-14 16:03:05 +0000219int fd_bootchk = 1;
Blue Swirl4fdcac02012-10-28 11:04:47 +0000220static int no_reboot;
aurel32b2f76162008-04-11 21:35:52 +0000221int no_shutdown = 0;
balrog9467cd42007-05-01 01:34:14 +0000222int cursor_hide = 1;
balroga171fe32007-04-30 01:48:07 +0000223int graphic_rotate = 0;
Markus Armbruster09aaa162009-08-21 10:31:34 +0200224const char *watchdog;
Gleb Natapov2e55e842010-12-08 13:35:07 +0200225QEMUOptionRom option_rom[MAX_OPTION_ROMS];
ths9ae02552007-01-05 17:39:04 +0000226int nb_option_roms;
pbrook8e716212007-01-20 17:12:09 +0000227int semihosting_enabled = 0;
balrog2b8f2d42007-07-27 22:08:46 +0000228int old_param = 0;
thsc35734b2007-03-19 15:17:08 +0000229const char *qemu_name;
ths3780e192007-06-21 21:08:02 +0000230int alt_grab = 0;
Dustin Kirkland0ca9f8a2009-09-17 15:48:04 -0500231int ctrl_grab = 0;
blueswir166508602007-05-01 14:16:52 +0000232unsigned int nb_prom_envs = 0;
233const char *prom_envs[MAX_PROM_ENVS];
Jan Kiszka95387492009-07-02 00:19:02 +0200234int boot_menu;
wayne3d3b8302011-07-27 18:04:55 +0800235uint8_t *boot_splash_filedata;
Markus Armbrusterd09acb92013-01-23 18:25:08 +0100236size_t boot_splash_filedata_size;
wayne3d3b8302011-07-27 18:04:55 +0800237uint8_t qemu_extra_params_fw[2];
bellard0824d6f2003-06-24 13:42:40 +0000238
Gleb Natapov1ca4d092010-12-08 13:35:05 +0200239typedef struct FWBootEntry FWBootEntry;
240
241struct FWBootEntry {
242 QTAILQ_ENTRY(FWBootEntry) link;
243 int32_t bootindex;
244 DeviceState *dev;
245 char *suffix;
246};
247
Blue Swirl4fdcac02012-10-28 11:04:47 +0000248static QTAILQ_HEAD(, FWBootEntry) fw_boot_order =
249 QTAILQ_HEAD_INITIALIZER(fw_boot_order);
Gleb Natapov1ca4d092010-12-08 13:35:05 +0200250
aliguori268a3622009-04-21 22:30:27 +0000251int nb_numa_nodes;
252uint64_t node_mem[MAX_NODES];
Chegu Vinodee785fe2012-07-16 21:31:30 -0700253unsigned long *node_cpumask[MAX_NODES];
aliguori268a3622009-04-21 22:30:27 +0000254
blueswir18fcb1b92008-09-18 18:29:08 +0000255uint8_t qemu_uuid[16];
256
Jan Kiszka76e30d02009-07-02 00:19:02 +0200257static QEMUBootSetHandler *boot_set_handler;
258static void *boot_set_opaque;
259
Gerd Hoffmannfd42dee2010-06-04 14:08:07 +0200260static NotifierList exit_notifiers =
261 NOTIFIER_LIST_INITIALIZER(exit_notifiers);
262
Gleb Natapov4cab9462010-12-08 13:35:08 +0200263static NotifierList machine_init_done_notifiers =
264 NOTIFIER_LIST_INITIALIZER(machine_init_done_notifiers);
265
liguangd5286af2013-01-24 13:03:27 +0800266static bool tcg_allowed = true;
267bool kvm_allowed;
268bool xen_allowed;
Blue Swirld745bef2010-03-29 19:23:49 +0000269uint32_t xen_domid;
270enum xen_mode xen_mode = XEN_EMULATE;
Jan Kiszkad5ab9712011-08-02 16:10:21 +0200271static int tcg_tb_size;
Blue Swirld745bef2010-03-29 19:23:49 +0000272
Gerd Hoffmann998bbd72009-12-08 13:11:41 +0100273static int default_serial = 1;
Gerd Hoffmann6a5e8b02009-12-08 13:11:42 +0100274static int default_parallel = 1;
Gerd Hoffmann986c5f72009-12-08 13:11:54 +0100275static int default_virtcon = 1;
Alexander Graf3ef669e2013-01-24 12:18:52 +0100276static int default_sclp = 1;
Gerd Hoffmannabdeed02009-12-08 13:11:43 +0100277static int default_monitor = 1;
Gerd Hoffmannac33f8f2009-12-16 14:25:39 +0100278static int default_floppy = 1;
279static int default_cdrom = 1;
280static int default_sdcard = 1;
Paolo Bonzini7f1b17f2012-05-10 09:39:17 +0200281static int default_vga = 1;
Gerd Hoffmann998bbd72009-12-08 13:11:41 +0100282
283static struct {
284 const char *driver;
285 int *flag;
286} default_list[] = {
Gerd Hoffmann6a5e8b02009-12-08 13:11:42 +0100287 { .driver = "isa-serial", .flag = &default_serial },
288 { .driver = "isa-parallel", .flag = &default_parallel },
Gerd Hoffmannd8bcbab2009-12-16 14:25:40 +0100289 { .driver = "isa-fdc", .flag = &default_floppy },
Markus Armbrusteraf6bf132011-05-18 18:31:02 +0200290 { .driver = "ide-cd", .flag = &default_cdrom },
291 { .driver = "ide-hd", .flag = &default_cdrom },
Gerd Hoffmannd8bcbab2009-12-16 14:25:40 +0100292 { .driver = "ide-drive", .flag = &default_cdrom },
Markus Armbrusteraf6bf132011-05-18 18:31:02 +0200293 { .driver = "scsi-cd", .flag = &default_cdrom },
Amit Shah392ecf52010-01-21 16:19:23 +0530294 { .driver = "virtio-serial-pci", .flag = &default_virtcon },
295 { .driver = "virtio-serial-s390", .flag = &default_virtcon },
296 { .driver = "virtio-serial", .flag = &default_virtcon },
Paolo Bonzini7f1b17f2012-05-10 09:39:17 +0200297 { .driver = "VGA", .flag = &default_vga },
298 { .driver = "isa-vga", .flag = &default_vga },
299 { .driver = "cirrus-vga", .flag = &default_vga },
300 { .driver = "isa-cirrus-vga", .flag = &default_vga },
301 { .driver = "vmware-svga", .flag = &default_vga },
302 { .driver = "qxl-vga", .flag = &default_vga },
Gerd Hoffmann998bbd72009-12-08 13:11:41 +0100303};
304
Paolo Bonzini4d454572012-11-26 16:03:42 +0100305static QemuOptsList qemu_rtc_opts = {
306 .name = "rtc",
307 .head = QTAILQ_HEAD_INITIALIZER(qemu_rtc_opts.head),
308 .desc = {
309 {
310 .name = "base",
311 .type = QEMU_OPT_STRING,
312 },{
313 .name = "clock",
314 .type = QEMU_OPT_STRING,
315 },{
316 .name = "driftfix",
317 .type = QEMU_OPT_STRING,
318 },
319 { /* end of list */ }
320 },
321};
322
323static QemuOptsList qemu_sandbox_opts = {
324 .name = "sandbox",
325 .implied_opt_name = "enable",
326 .head = QTAILQ_HEAD_INITIALIZER(qemu_sandbox_opts.head),
327 .desc = {
328 {
329 .name = "enable",
330 .type = QEMU_OPT_BOOL,
331 },
332 { /* end of list */ }
333 },
334};
335
336static QemuOptsList qemu_trace_opts = {
337 .name = "trace",
338 .implied_opt_name = "trace",
339 .head = QTAILQ_HEAD_INITIALIZER(qemu_trace_opts.head),
340 .desc = {
341 {
342 .name = "events",
343 .type = QEMU_OPT_STRING,
344 },{
345 .name = "file",
346 .type = QEMU_OPT_STRING,
347 },
348 { /* end of list */ }
349 },
350};
351
352static QemuOptsList qemu_option_rom_opts = {
353 .name = "option-rom",
354 .implied_opt_name = "romfile",
355 .head = QTAILQ_HEAD_INITIALIZER(qemu_option_rom_opts.head),
356 .desc = {
357 {
358 .name = "bootindex",
359 .type = QEMU_OPT_NUMBER,
360 }, {
361 .name = "romfile",
362 .type = QEMU_OPT_STRING,
363 },
364 { /* end of list */ }
365 },
366};
367
368static QemuOptsList qemu_machine_opts = {
369 .name = "machine",
370 .implied_opt_name = "type",
371 .merge_lists = true,
372 .head = QTAILQ_HEAD_INITIALIZER(qemu_machine_opts.head),
373 .desc = {
374 {
375 .name = "type",
376 .type = QEMU_OPT_STRING,
377 .help = "emulated machine"
378 }, {
379 .name = "accel",
380 .type = QEMU_OPT_STRING,
381 .help = "accelerator list",
382 }, {
383 .name = "kernel_irqchip",
384 .type = QEMU_OPT_BOOL,
385 .help = "use KVM in-kernel irqchip",
386 }, {
387 .name = "kvm_shadow_mem",
388 .type = QEMU_OPT_SIZE,
389 .help = "KVM shadow MMU size",
390 }, {
391 .name = "kernel",
392 .type = QEMU_OPT_STRING,
393 .help = "Linux kernel image file",
394 }, {
395 .name = "initrd",
396 .type = QEMU_OPT_STRING,
397 .help = "Linux initial ramdisk file",
398 }, {
399 .name = "append",
400 .type = QEMU_OPT_STRING,
401 .help = "Linux kernel command line",
402 }, {
403 .name = "dtb",
404 .type = QEMU_OPT_STRING,
405 .help = "Linux kernel device tree file",
406 }, {
407 .name = "dumpdtb",
408 .type = QEMU_OPT_STRING,
409 .help = "Dump current dtb to a file and quit",
410 }, {
411 .name = "phandle_start",
412 .type = QEMU_OPT_STRING,
413 .help = "The first phandle ID we may generate dynamically",
414 }, {
415 .name = "dt_compatible",
416 .type = QEMU_OPT_STRING,
417 .help = "Overrides the \"compatible\" property of the dt root node",
418 }, {
419 .name = "dump-guest-core",
420 .type = QEMU_OPT_BOOL,
421 .help = "Include guest memory in a core dump",
422 }, {
423 .name = "mem-merge",
424 .type = QEMU_OPT_BOOL,
425 .help = "enable/disable memory merge support",
426 },{
427 .name = "usb",
428 .type = QEMU_OPT_BOOL,
429 .help = "Set on/off to enable/disable usb",
430 },
431 { /* End of list */ }
432 },
433};
434
435static QemuOptsList qemu_boot_opts = {
436 .name = "boot-opts",
437 .head = QTAILQ_HEAD_INITIALIZER(qemu_boot_opts.head),
438 .desc = {
439 /* the three names below are not used now */
440 {
441 .name = "order",
442 .type = QEMU_OPT_STRING,
443 }, {
444 .name = "once",
445 .type = QEMU_OPT_STRING,
446 }, {
447 .name = "menu",
448 .type = QEMU_OPT_STRING,
449 /* following are really used */
450 }, {
451 .name = "splash",
452 .type = QEMU_OPT_STRING,
453 }, {
454 .name = "splash-time",
455 .type = QEMU_OPT_STRING,
456 }, {
457 .name = "reboot-timeout",
458 .type = QEMU_OPT_STRING,
459 },
460 { /*End of list */ }
461 },
462};
463
464static QemuOptsList qemu_add_fd_opts = {
465 .name = "add-fd",
466 .head = QTAILQ_HEAD_INITIALIZER(qemu_add_fd_opts.head),
467 .desc = {
468 {
469 .name = "fd",
470 .type = QEMU_OPT_NUMBER,
471 .help = "file descriptor of which a duplicate is added to fd set",
472 },{
473 .name = "set",
474 .type = QEMU_OPT_NUMBER,
475 .help = "ID of the fd set to add fd to",
476 },{
477 .name = "opaque",
478 .type = QEMU_OPT_STRING,
479 .help = "free-form string used to describe fd",
480 },
481 { /* end of list */ }
482 },
483};
484
485static QemuOptsList qemu_object_opts = {
486 .name = "object",
487 .implied_opt_name = "qom-type",
488 .head = QTAILQ_HEAD_INITIALIZER(qemu_object_opts.head),
489 .desc = {
490 { }
491 },
492};
493
Ronnie Sahlberg31459f42012-08-06 18:24:55 +1000494const char *qemu_get_vm_name(void)
495{
496 return qemu_name;
497}
498
wayne3d3b8302011-07-27 18:04:55 +0800499static void res_free(void)
500{
501 if (boot_splash_filedata != NULL) {
Anthony Liguori7267c092011-08-20 22:09:37 -0500502 g_free(boot_splash_filedata);
wayne3d3b8302011-07-27 18:04:55 +0800503 boot_splash_filedata = NULL;
504 }
505}
506
Gerd Hoffmann998bbd72009-12-08 13:11:41 +0100507static int default_driver_check(QemuOpts *opts, void *opaque)
508{
509 const char *driver = qemu_opt_get(opts, "driver");
510 int i;
511
512 if (!driver)
513 return 0;
514 for (i = 0; i < ARRAY_SIZE(default_list); i++) {
515 if (strcmp(default_list[i].driver, driver) != 0)
516 continue;
517 *(default_list[i].flag) = 0;
518 }
519 return 0;
520}
521
bellard0824d6f2003-06-24 13:42:40 +0000522/***********************************************************/
Luiz Capitulinof5bbfba2011-07-29 15:04:45 -0300523/* QEMU state */
524
Luiz Capitulino0461d5a2011-09-30 14:45:27 -0300525static RunState current_run_state = RUN_STATE_PRELAUNCH;
Luiz Capitulinof5bbfba2011-07-29 15:04:45 -0300526
Luiz Capitulino5db9d4d2011-09-05 16:36:31 -0300527typedef struct {
528 RunState from;
529 RunState to;
530} RunStateTransition;
531
532static const RunStateTransition runstate_transitions_def[] = {
533 /* from -> to */
Luiz Capitulino0461d5a2011-09-30 14:45:27 -0300534 { RUN_STATE_DEBUG, RUN_STATE_RUNNING },
Luiz Capitulino5db9d4d2011-09-05 16:36:31 -0300535
Luiz Capitulino0461d5a2011-09-30 14:45:27 -0300536 { RUN_STATE_INMIGRATE, RUN_STATE_RUNNING },
Paolo Bonzini29ed72f2012-10-19 16:45:24 +0200537 { RUN_STATE_INMIGRATE, RUN_STATE_PAUSED },
Luiz Capitulino5db9d4d2011-09-05 16:36:31 -0300538
Luiz Capitulino0461d5a2011-09-30 14:45:27 -0300539 { RUN_STATE_INTERNAL_ERROR, RUN_STATE_PAUSED },
Luiz Capitulino8a9236f2011-10-14 11:18:09 -0300540 { RUN_STATE_INTERNAL_ERROR, RUN_STATE_FINISH_MIGRATE },
Luiz Capitulino5db9d4d2011-09-05 16:36:31 -0300541
Luiz Capitulino0461d5a2011-09-30 14:45:27 -0300542 { RUN_STATE_IO_ERROR, RUN_STATE_RUNNING },
Luiz Capitulino8a9236f2011-10-14 11:18:09 -0300543 { RUN_STATE_IO_ERROR, RUN_STATE_FINISH_MIGRATE },
Luiz Capitulino5db9d4d2011-09-05 16:36:31 -0300544
Luiz Capitulino0461d5a2011-09-30 14:45:27 -0300545 { RUN_STATE_PAUSED, RUN_STATE_RUNNING },
Luiz Capitulino8a9236f2011-10-14 11:18:09 -0300546 { RUN_STATE_PAUSED, RUN_STATE_FINISH_MIGRATE },
Luiz Capitulino5db9d4d2011-09-05 16:36:31 -0300547
Luiz Capitulino0461d5a2011-09-30 14:45:27 -0300548 { RUN_STATE_POSTMIGRATE, RUN_STATE_RUNNING },
Luiz Capitulino8a9236f2011-10-14 11:18:09 -0300549 { RUN_STATE_POSTMIGRATE, RUN_STATE_FINISH_MIGRATE },
Luiz Capitulino5db9d4d2011-09-05 16:36:31 -0300550
Luiz Capitulino0461d5a2011-09-30 14:45:27 -0300551 { RUN_STATE_PRELAUNCH, RUN_STATE_RUNNING },
Luiz Capitulino8a9236f2011-10-14 11:18:09 -0300552 { RUN_STATE_PRELAUNCH, RUN_STATE_FINISH_MIGRATE },
Luiz Capitulino0461d5a2011-09-30 14:45:27 -0300553 { RUN_STATE_PRELAUNCH, RUN_STATE_INMIGRATE },
Luiz Capitulino5db9d4d2011-09-05 16:36:31 -0300554
Luiz Capitulino0461d5a2011-09-30 14:45:27 -0300555 { RUN_STATE_FINISH_MIGRATE, RUN_STATE_RUNNING },
556 { RUN_STATE_FINISH_MIGRATE, RUN_STATE_POSTMIGRATE },
Luiz Capitulino5db9d4d2011-09-05 16:36:31 -0300557
Luiz Capitulino0461d5a2011-09-30 14:45:27 -0300558 { RUN_STATE_RESTORE_VM, RUN_STATE_RUNNING },
Luiz Capitulino5db9d4d2011-09-05 16:36:31 -0300559
Luiz Capitulino0461d5a2011-09-30 14:45:27 -0300560 { RUN_STATE_RUNNING, RUN_STATE_DEBUG },
561 { RUN_STATE_RUNNING, RUN_STATE_INTERNAL_ERROR },
562 { RUN_STATE_RUNNING, RUN_STATE_IO_ERROR },
563 { RUN_STATE_RUNNING, RUN_STATE_PAUSED },
564 { RUN_STATE_RUNNING, RUN_STATE_FINISH_MIGRATE },
565 { RUN_STATE_RUNNING, RUN_STATE_RESTORE_VM },
566 { RUN_STATE_RUNNING, RUN_STATE_SAVE_VM },
567 { RUN_STATE_RUNNING, RUN_STATE_SHUTDOWN },
568 { RUN_STATE_RUNNING, RUN_STATE_WATCHDOG },
Luiz Capitulino5db9d4d2011-09-05 16:36:31 -0300569
Luiz Capitulino0461d5a2011-09-30 14:45:27 -0300570 { RUN_STATE_SAVE_VM, RUN_STATE_RUNNING },
Luiz Capitulino5db9d4d2011-09-05 16:36:31 -0300571
Luiz Capitulino0461d5a2011-09-30 14:45:27 -0300572 { RUN_STATE_SHUTDOWN, RUN_STATE_PAUSED },
Luiz Capitulino8a9236f2011-10-14 11:18:09 -0300573 { RUN_STATE_SHUTDOWN, RUN_STATE_FINISH_MIGRATE },
Luiz Capitulino5db9d4d2011-09-05 16:36:31 -0300574
Luiz Capitulinoad02b962012-04-27 13:33:36 -0300575 { RUN_STATE_DEBUG, RUN_STATE_SUSPENDED },
576 { RUN_STATE_RUNNING, RUN_STATE_SUSPENDED },
577 { RUN_STATE_SUSPENDED, RUN_STATE_RUNNING },
578 { RUN_STATE_SUSPENDED, RUN_STATE_FINISH_MIGRATE },
579
Luiz Capitulino0461d5a2011-09-30 14:45:27 -0300580 { RUN_STATE_WATCHDOG, RUN_STATE_RUNNING },
Luiz Capitulino8a9236f2011-10-14 11:18:09 -0300581 { RUN_STATE_WATCHDOG, RUN_STATE_FINISH_MIGRATE },
Luiz Capitulino5db9d4d2011-09-05 16:36:31 -0300582
Luiz Capitulino0461d5a2011-09-30 14:45:27 -0300583 { RUN_STATE_MAX, RUN_STATE_MAX },
Luiz Capitulino5db9d4d2011-09-05 16:36:31 -0300584};
585
Luiz Capitulino0461d5a2011-09-30 14:45:27 -0300586static bool runstate_valid_transitions[RUN_STATE_MAX][RUN_STATE_MAX];
Luiz Capitulino5db9d4d2011-09-05 16:36:31 -0300587
Luiz Capitulinof5bbfba2011-07-29 15:04:45 -0300588bool runstate_check(RunState state)
589{
590 return current_run_state == state;
591}
592
Blue Swirl4fdcac02012-10-28 11:04:47 +0000593static void runstate_init(void)
Luiz Capitulino5db9d4d2011-09-05 16:36:31 -0300594{
595 const RunStateTransition *p;
596
597 memset(&runstate_valid_transitions, 0, sizeof(runstate_valid_transitions));
598
Luiz Capitulino0461d5a2011-09-30 14:45:27 -0300599 for (p = &runstate_transitions_def[0]; p->from != RUN_STATE_MAX; p++) {
Luiz Capitulino5db9d4d2011-09-05 16:36:31 -0300600 runstate_valid_transitions[p->from][p->to] = true;
601 }
602}
603
604/* This function will abort() on invalid state transitions */
Luiz Capitulinof5bbfba2011-07-29 15:04:45 -0300605void runstate_set(RunState new_state)
606{
Luiz Capitulino207c5cd2011-10-13 10:59:07 -0300607 assert(new_state < RUN_STATE_MAX);
608
609 if (!runstate_valid_transitions[current_run_state][new_state]) {
610 fprintf(stderr, "ERROR: invalid runstate transition: '%s' -> '%s'\n",
611 RunState_lookup[current_run_state],
612 RunState_lookup[new_state]);
Luiz Capitulino5db9d4d2011-09-05 16:36:31 -0300613 abort();
614 }
615
Luiz Capitulinof5bbfba2011-07-29 15:04:45 -0300616 current_run_state = new_state;
617}
618
Luiz Capitulino13548692011-07-29 15:36:43 -0300619int runstate_is_running(void)
620{
Luiz Capitulino0461d5a2011-09-30 14:45:27 -0300621 return runstate_check(RUN_STATE_RUNNING);
Luiz Capitulino13548692011-07-29 15:36:43 -0300622}
623
Luiz Capitulino1fa9a5e2011-09-12 17:54:20 -0300624StatusInfo *qmp_query_status(Error **errp)
625{
626 StatusInfo *info = g_malloc0(sizeof(*info));
627
628 info->running = runstate_is_running();
629 info->singlestep = singlestep;
630 info->status = current_run_state;
631
632 return info;
633}
634
Luiz Capitulinof5bbfba2011-07-29 15:04:45 -0300635/***********************************************************/
Paolo Bonzini8f0056b2010-01-13 14:05:34 +0100636/* real time host monotonic timer */
bellard09b26c52006-04-12 21:09:08 +0000637
bellardc4b1fcc2004-03-14 21:44:30 +0000638/***********************************************************/
balrogf6503052008-02-17 11:42:19 +0000639/* host time/date access */
640void qemu_get_timedate(struct tm *tm, int offset)
641{
642 time_t ti;
balrogf6503052008-02-17 11:42:19 +0000643
644 time(&ti);
645 ti += offset;
646 if (rtc_date_offset == -1) {
647 if (rtc_utc)
Stefan Weileb7ff6f2013-01-07 23:08:13 +0100648 gmtime_r(&ti, tm);
balrogf6503052008-02-17 11:42:19 +0000649 else
Stefan Weileb7ff6f2013-01-07 23:08:13 +0100650 localtime_r(&ti, tm);
balrogf6503052008-02-17 11:42:19 +0000651 } else {
652 ti -= rtc_date_offset;
Stefan Weileb7ff6f2013-01-07 23:08:13 +0100653 gmtime_r(&ti, tm);
balrogf6503052008-02-17 11:42:19 +0000654 }
balrogf6503052008-02-17 11:42:19 +0000655}
656
657int qemu_timedate_diff(struct tm *tm)
658{
659 time_t seconds;
660
661 if (rtc_date_offset == -1)
662 if (rtc_utc)
663 seconds = mktimegm(tm);
Gleb Natapovf54c5562011-11-06 18:00:22 +0200664 else {
665 struct tm tmp = *tm;
666 tmp.tm_isdst = -1; /* use timezone to figure it out */
667 seconds = mktime(&tmp);
668 }
balrogf6503052008-02-17 11:42:19 +0000669 else
670 seconds = mktimegm(tm) + rtc_date_offset;
671
672 return seconds - time(NULL);
673}
674
Luiz Capitulino80cd3472010-02-25 12:11:44 -0300675void rtc_change_mon_event(struct tm *tm)
676{
677 QObject *data;
678
679 data = qobject_from_jsonf("{ 'offset': %d }", qemu_timedate_diff(tm));
680 monitor_protocol_event(QEVENT_RTC_CHANGE, data);
681 qobject_decref(data);
682}
683
Jan Kiszka1ed2fc12009-09-15 13:36:04 +0200684static void configure_rtc_date_offset(const char *startdate, int legacy)
685{
686 time_t rtc_start_date;
687 struct tm tm;
688
689 if (!strcmp(startdate, "now") && legacy) {
690 rtc_date_offset = -1;
691 } else {
692 if (sscanf(startdate, "%d-%d-%dT%d:%d:%d",
693 &tm.tm_year,
694 &tm.tm_mon,
695 &tm.tm_mday,
696 &tm.tm_hour,
697 &tm.tm_min,
698 &tm.tm_sec) == 6) {
699 /* OK */
700 } else if (sscanf(startdate, "%d-%d-%d",
701 &tm.tm_year,
702 &tm.tm_mon,
703 &tm.tm_mday) == 3) {
704 tm.tm_hour = 0;
705 tm.tm_min = 0;
706 tm.tm_sec = 0;
707 } else {
708 goto date_fail;
709 }
710 tm.tm_year -= 1900;
711 tm.tm_mon--;
712 rtc_start_date = mktimegm(&tm);
713 if (rtc_start_date == -1) {
714 date_fail:
715 fprintf(stderr, "Invalid date format. Valid formats are:\n"
716 "'2006-06-17T16:01:21' or '2006-06-17'\n");
717 exit(1);
718 }
719 rtc_date_offset = time(NULL) - rtc_start_date;
720 }
721}
722
723static void configure_rtc(QemuOpts *opts)
724{
725 const char *value;
726
727 value = qemu_opt_get(opts, "base");
728 if (value) {
729 if (!strcmp(value, "utc")) {
730 rtc_utc = 1;
731 } else if (!strcmp(value, "localtime")) {
732 rtc_utc = 0;
733 } else {
734 configure_rtc_date_offset(value, 0);
735 }
736 }
Jan Kiszka68752042009-09-15 13:36:04 +0200737 value = qemu_opt_get(opts, "clock");
738 if (value) {
739 if (!strcmp(value, "host")) {
740 rtc_clock = host_clock;
Paolo Bonzini78808142012-03-30 10:31:21 +0000741 } else if (!strcmp(value, "rt")) {
742 rtc_clock = rt_clock;
Jan Kiszka68752042009-09-15 13:36:04 +0200743 } else if (!strcmp(value, "vm")) {
744 rtc_clock = vm_clock;
745 } else {
746 fprintf(stderr, "qemu: invalid option value '%s'\n", value);
747 exit(1);
748 }
749 }
Jan Kiszka1ed2fc12009-09-15 13:36:04 +0200750 value = qemu_opt_get(opts, "driftfix");
751 if (value) {
Blue Swirl7e4c0332010-03-27 21:33:46 +0000752 if (!strcmp(value, "slew")) {
Jan Kiszka433acf02012-01-23 20:15:12 +0100753 static GlobalProperty slew_lost_ticks[] = {
754 {
755 .driver = "mc146818rtc",
756 .property = "lost_tick_policy",
757 .value = "slew",
758 },
759 { /* end of list */ }
760 };
761
762 qdev_prop_register_global_list(slew_lost_ticks);
Blue Swirl7e4c0332010-03-27 21:33:46 +0000763 } else if (!strcmp(value, "none")) {
Jan Kiszka433acf02012-01-23 20:15:12 +0100764 /* discard is default */
Jan Kiszka1ed2fc12009-09-15 13:36:04 +0200765 } else {
766 fprintf(stderr, "qemu: invalid option value '%s'\n", value);
767 exit(1);
768 }
769 }
Jan Kiszka1ed2fc12009-09-15 13:36:04 +0200770}
771
balrog1ae26a12008-09-28 23:19:47 +0000772/***********************************************************/
773/* Bluetooth support */
774static int nb_hcis;
775static int cur_hci;
776static struct HCIInfo *hci_table[MAX_NICS];
balrogdc72ac12008-11-09 00:04:26 +0000777
balrog1ae26a12008-09-28 23:19:47 +0000778static struct bt_vlan_s {
779 struct bt_scatternet_s net;
780 int id;
781 struct bt_vlan_s *next;
782} *first_bt_vlan;
783
784/* find or alloc a new bluetooth "VLAN" */
blueswir1674bb262008-09-30 18:18:27 +0000785static struct bt_scatternet_s *qemu_find_bt_vlan(int id)
balrog1ae26a12008-09-28 23:19:47 +0000786{
787 struct bt_vlan_s **pvlan, *vlan;
788 for (vlan = first_bt_vlan; vlan != NULL; vlan = vlan->next) {
789 if (vlan->id == id)
790 return &vlan->net;
791 }
Anthony Liguori7267c092011-08-20 22:09:37 -0500792 vlan = g_malloc0(sizeof(struct bt_vlan_s));
balrog1ae26a12008-09-28 23:19:47 +0000793 vlan->id = id;
794 pvlan = &first_bt_vlan;
795 while (*pvlan != NULL)
796 pvlan = &(*pvlan)->next;
797 *pvlan = vlan;
798 return &vlan->net;
799}
800
801static void null_hci_send(struct HCIInfo *hci, const uint8_t *data, int len)
802{
803}
804
805static int null_hci_addr_set(struct HCIInfo *hci, const uint8_t *bd_addr)
806{
807 return -ENOTSUP;
808}
809
810static struct HCIInfo null_hci = {
811 .cmd_send = null_hci_send,
812 .sco_send = null_hci_send,
813 .acl_send = null_hci_send,
814 .bdaddr_set = null_hci_addr_set,
815};
816
817struct HCIInfo *qemu_next_hci(void)
818{
819 if (cur_hci == nb_hcis)
820 return &null_hci;
821
822 return hci_table[cur_hci++];
823}
824
balrogdc72ac12008-11-09 00:04:26 +0000825static struct HCIInfo *hci_init(const char *str)
826{
827 char *endp;
828 struct bt_scatternet_s *vlan = 0;
829
830 if (!strcmp(str, "null"))
831 /* null */
832 return &null_hci;
833 else if (!strncmp(str, "host", 4) && (str[4] == '\0' || str[4] == ':'))
834 /* host[:hciN] */
835 return bt_host_hci(str[4] ? str + 5 : "hci0");
836 else if (!strncmp(str, "hci", 3)) {
837 /* hci[,vlan=n] */
838 if (str[3]) {
839 if (!strncmp(str + 3, ",vlan=", 6)) {
840 vlan = qemu_find_bt_vlan(strtol(str + 9, &endp, 0));
841 if (*endp)
842 vlan = 0;
843 }
844 } else
845 vlan = qemu_find_bt_vlan(0);
846 if (vlan)
847 return bt_new_hci(vlan);
848 }
849
850 fprintf(stderr, "qemu: Unknown bluetooth HCI `%s'.\n", str);
851
852 return 0;
853}
854
855static int bt_hci_parse(const char *str)
856{
857 struct HCIInfo *hci;
Anthony Liguoric227f092009-10-01 16:12:16 -0500858 bdaddr_t bdaddr;
balrogdc72ac12008-11-09 00:04:26 +0000859
860 if (nb_hcis >= MAX_NICS) {
861 fprintf(stderr, "qemu: Too many bluetooth HCIs (max %i).\n", MAX_NICS);
862 return -1;
863 }
864
865 hci = hci_init(str);
866 if (!hci)
867 return -1;
868
869 bdaddr.b[0] = 0x52;
870 bdaddr.b[1] = 0x54;
871 bdaddr.b[2] = 0x00;
872 bdaddr.b[3] = 0x12;
873 bdaddr.b[4] = 0x34;
874 bdaddr.b[5] = 0x56 + nb_hcis;
875 hci->bdaddr_set(hci, bdaddr.b);
876
877 hci_table[nb_hcis++] = hci;
878
879 return 0;
880}
881
882static void bt_vhci_add(int vlan_id)
883{
884 struct bt_scatternet_s *vlan = qemu_find_bt_vlan(vlan_id);
885
886 if (!vlan->slave)
887 fprintf(stderr, "qemu: warning: adding a VHCI to "
888 "an empty scatternet %i\n", vlan_id);
889
890 bt_vhci_init(bt_new_hci(vlan));
891}
892
893static struct bt_device_s *bt_device_add(const char *opt)
894{
895 struct bt_scatternet_s *vlan;
896 int vlan_id = 0;
897 char *endp = strstr(opt, ",vlan=");
898 int len = (endp ? endp - opt : strlen(opt)) + 1;
899 char devname[10];
900
901 pstrcpy(devname, MIN(sizeof(devname), len), opt);
902
903 if (endp) {
904 vlan_id = strtol(endp + 6, &endp, 0);
905 if (*endp) {
906 fprintf(stderr, "qemu: unrecognised bluetooth vlan Id\n");
907 return 0;
908 }
909 }
910
911 vlan = qemu_find_bt_vlan(vlan_id);
912
913 if (!vlan->slave)
914 fprintf(stderr, "qemu: warning: adding a slave device to "
915 "an empty scatternet %i\n", vlan_id);
916
917 if (!strcmp(devname, "keyboard"))
918 return bt_keyboard_init(vlan);
919
920 fprintf(stderr, "qemu: unsupported bluetooth device `%s'\n", devname);
921 return 0;
922}
923
924static int bt_parse(const char *opt)
925{
926 const char *endp, *p;
927 int vlan;
928
929 if (strstart(opt, "hci", &endp)) {
930 if (!*endp || *endp == ',') {
931 if (*endp)
932 if (!strstart(endp, ",vlan=", 0))
933 opt = endp + 1;
934
935 return bt_hci_parse(opt);
936 }
937 } else if (strstart(opt, "vhci", &endp)) {
938 if (!*endp || *endp == ',') {
939 if (*endp) {
940 if (strstart(endp, ",vlan=", &p)) {
941 vlan = strtol(p, (char **) &endp, 0);
942 if (*endp) {
943 fprintf(stderr, "qemu: bad scatternet '%s'\n", p);
944 return 1;
945 }
946 } else {
947 fprintf(stderr, "qemu: bad parameter '%s'\n", endp + 1);
948 return 1;
949 }
950 } else
951 vlan = 0;
952
953 bt_vhci_add(vlan);
954 return 0;
955 }
956 } else if (strstart(opt, "device:", &endp))
957 return !bt_device_add(endp);
958
959 fprintf(stderr, "qemu: bad bluetooth parameter '%s'\n", opt);
960 return 1;
961}
962
Eduardo Otubo7d76ad42012-08-14 18:44:08 -0300963static int parse_sandbox(QemuOpts *opts, void *opaque)
964{
965 /* FIXME: change this to true for 1.3 */
966 if (qemu_opt_get_bool(opts, "enable", false)) {
967#ifdef CONFIG_SECCOMP
968 if (seccomp_start() < 0) {
969 qerror_report(ERROR_CLASS_GENERIC_ERROR,
970 "failed to install seccomp syscall filter in the kernel");
971 return -1;
972 }
973#else
974 qerror_report(ERROR_CLASS_GENERIC_ERROR,
975 "sandboxing request but seccomp is not compiled into this build");
976 return -1;
977#endif
978 }
979
980 return 0;
981}
982
zhlcindy@gmail.com094b2872012-09-02 19:25:28 +0000983/*********QEMU USB setting******/
984bool usb_enabled(bool default_usb)
985{
986 QemuOpts *mach_opts;
987 mach_opts = qemu_opts_find(qemu_find_opts("machine"), 0);
988 if (mach_opts) {
989 return qemu_opt_get_bool(mach_opts, "usb", default_usb);
990 }
991 return default_usb;
992}
993
Corey Bryant587ed6b2012-10-18 15:19:34 -0400994#ifndef _WIN32
995static int parse_add_fd(QemuOpts *opts, void *opaque)
996{
997 int fd, dupfd, flags;
998 int64_t fdset_id;
999 const char *fd_opaque = NULL;
1000
1001 fd = qemu_opt_get_number(opts, "fd", -1);
1002 fdset_id = qemu_opt_get_number(opts, "set", -1);
1003 fd_opaque = qemu_opt_get(opts, "opaque");
1004
1005 if (fd < 0) {
1006 qerror_report(ERROR_CLASS_GENERIC_ERROR,
1007 "fd option is required and must be non-negative");
1008 return -1;
1009 }
1010
1011 if (fd <= STDERR_FILENO) {
1012 qerror_report(ERROR_CLASS_GENERIC_ERROR,
1013 "fd cannot be a standard I/O stream");
1014 return -1;
1015 }
1016
1017 /*
1018 * All fds inherited across exec() necessarily have FD_CLOEXEC
1019 * clear, while qemu sets FD_CLOEXEC on all other fds used internally.
1020 */
1021 flags = fcntl(fd, F_GETFD);
1022 if (flags == -1 || (flags & FD_CLOEXEC)) {
1023 qerror_report(ERROR_CLASS_GENERIC_ERROR,
1024 "fd is not valid or already in use");
1025 return -1;
1026 }
1027
1028 if (fdset_id < 0) {
1029 qerror_report(ERROR_CLASS_GENERIC_ERROR,
1030 "set option is required and must be non-negative");
1031 return -1;
1032 }
1033
1034#ifdef F_DUPFD_CLOEXEC
1035 dupfd = fcntl(fd, F_DUPFD_CLOEXEC, 0);
1036#else
1037 dupfd = dup(fd);
1038 if (dupfd != -1) {
1039 qemu_set_cloexec(dupfd);
1040 }
1041#endif
1042 if (dupfd == -1) {
1043 qerror_report(ERROR_CLASS_GENERIC_ERROR,
1044 "Error duplicating fd: %s", strerror(errno));
1045 return -1;
1046 }
1047
1048 /* add the duplicate fd, and optionally the opaque string, to the fd set */
1049 monitor_fdset_add_fd(dupfd, true, fdset_id, fd_opaque ? true : false,
1050 fd_opaque, NULL);
1051
1052 return 0;
1053}
1054
1055static int cleanup_add_fd(QemuOpts *opts, void *opaque)
1056{
1057 int fd;
1058
1059 fd = qemu_opt_get_number(opts, "fd", -1);
1060 close(fd);
1061
1062 return 0;
1063}
1064#endif
1065
balrog1ae26a12008-09-28 23:19:47 +00001066/***********************************************************/
1067/* QEMU Block devices */
1068
Markus Armbruster2292dda2011-01-28 11:21:41 +01001069#define HD_OPTS "media=disk"
1070#define CDROM_OPTS "media=cdrom"
1071#define FD_OPTS ""
1072#define PFLASH_OPTS ""
1073#define MTD_OPTS ""
1074#define SD_OPTS ""
thse4bcb142007-12-02 04:51:10 +00001075
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +02001076static int drive_init_func(QemuOpts *opts, void *opaque)
1077{
Christian Borntraeger2d0d2832012-11-20 15:30:34 +01001078 BlockInterfaceType *block_default_type = opaque;
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +02001079
Christian Borntraeger2d0d2832012-11-20 15:30:34 +01001080 return drive_init(opts, *block_default_type) == NULL;
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +02001081}
1082
1083static int drive_enable_snapshot(QemuOpts *opts, void *opaque)
1084{
1085 if (NULL == qemu_opt_get(opts, "snapshot")) {
1086 qemu_opt_set(opts, "snapshot", "on");
1087 }
1088 return 0;
1089}
1090
Christian Borntraeger3c42ea62012-11-22 21:02:55 +01001091static void default_drive(int enable, int snapshot, BlockInterfaceType type,
1092 int index, const char *optstr)
Markus Armbruster4e5d9b52011-01-28 11:21:45 +01001093{
1094 QemuOpts *opts;
1095
Markus Armbruster4e5d9b52011-01-28 11:21:45 +01001096 if (!enable || drive_get_by_index(type, index)) {
1097 return;
1098 }
1099
1100 opts = drive_add(type, index, NULL, optstr);
1101 if (snapshot) {
1102 drive_enable_snapshot(opts, NULL);
1103 }
Christian Borntraeger2d0d2832012-11-20 15:30:34 +01001104 if (!drive_init(opts, type)) {
Markus Armbruster4e5d9b52011-01-28 11:21:45 +01001105 exit(1);
1106 }
1107}
1108
Jan Kiszka76e30d02009-07-02 00:19:02 +02001109void qemu_register_boot_set(QEMUBootSetHandler *func, void *opaque)
1110{
1111 boot_set_handler = func;
1112 boot_set_opaque = opaque;
1113}
1114
1115int qemu_boot_set(const char *boot_devices)
1116{
1117 if (!boot_set_handler) {
1118 return -EINVAL;
1119 }
1120 return boot_set_handler(boot_set_opaque, boot_devices);
1121}
1122
Eduardo Habkost4e9e9d62010-04-06 19:22:08 -03001123static void validate_bootdevices(char *devices)
Jan Kiszkaef3adf62009-07-02 00:19:02 +02001124{
1125 /* We just do some generic consistency checks */
1126 const char *p;
1127 int bitmap = 0;
1128
1129 for (p = devices; *p != '\0'; p++) {
1130 /* Allowed boot devices are:
1131 * a-b: floppy disk drives
1132 * c-f: IDE disk drives
Dong Xu Wang07f35072011-11-22 18:06:26 +08001133 * g-m: machine implementation dependent drives
Jan Kiszkaef3adf62009-07-02 00:19:02 +02001134 * n-p: network devices
1135 * It's up to each machine implementation to check if the given boot
1136 * devices match the actual hardware implementation and firmware
1137 * features.
1138 */
1139 if (*p < 'a' || *p > 'p') {
1140 fprintf(stderr, "Invalid boot device '%c'\n", *p);
1141 exit(1);
1142 }
1143 if (bitmap & (1 << (*p - 'a'))) {
1144 fprintf(stderr, "Boot device '%c' was given twice\n", *p);
1145 exit(1);
1146 }
1147 bitmap |= 1 << (*p - 'a');
1148 }
Jan Kiszkaef3adf62009-07-02 00:19:02 +02001149}
1150
Jan Kiszkae0f084b2009-07-02 00:19:02 +02001151static void restore_boot_devices(void *opaque)
1152{
1153 char *standard_boot_devices = opaque;
Alex Williamson37905d62010-04-30 15:21:11 -06001154 static int first = 1;
1155
1156 /* Restore boot order and remove ourselves after the first boot */
1157 if (first) {
1158 first = 0;
1159 return;
1160 }
Jan Kiszkae0f084b2009-07-02 00:19:02 +02001161
1162 qemu_boot_set(standard_boot_devices);
1163
1164 qemu_unregister_reset(restore_boot_devices, standard_boot_devices);
Anthony Liguori7267c092011-08-20 22:09:37 -05001165 g_free(standard_boot_devices);
Jan Kiszkae0f084b2009-07-02 00:19:02 +02001166}
1167
Gleb Natapov1ca4d092010-12-08 13:35:05 +02001168void add_boot_device_path(int32_t bootindex, DeviceState *dev,
1169 const char *suffix)
1170{
1171 FWBootEntry *node, *i;
1172
1173 if (bootindex < 0) {
1174 return;
1175 }
1176
1177 assert(dev != NULL || suffix != NULL);
1178
Anthony Liguori7267c092011-08-20 22:09:37 -05001179 node = g_malloc0(sizeof(FWBootEntry));
Gleb Natapov1ca4d092010-12-08 13:35:05 +02001180 node->bootindex = bootindex;
Anthony Liguori7267c092011-08-20 22:09:37 -05001181 node->suffix = suffix ? g_strdup(suffix) : NULL;
Gleb Natapov1ca4d092010-12-08 13:35:05 +02001182 node->dev = dev;
1183
1184 QTAILQ_FOREACH(i, &fw_boot_order, link) {
1185 if (i->bootindex == bootindex) {
1186 fprintf(stderr, "Two devices with same boot index %d\n", bootindex);
1187 exit(1);
1188 } else if (i->bootindex < bootindex) {
1189 continue;
1190 }
1191 QTAILQ_INSERT_BEFORE(i, node, link);
1192 return;
1193 }
1194 QTAILQ_INSERT_TAIL(&fw_boot_order, node, link);
1195}
1196
Gleb Natapov962630f2010-12-08 13:35:09 +02001197/*
1198 * This function returns null terminated string that consist of new line
Brad Hards71785ab2011-04-23 21:50:06 +10001199 * separated device paths.
Gleb Natapov962630f2010-12-08 13:35:09 +02001200 *
1201 * memory pointed by "size" is assigned total length of the array in bytes
1202 *
1203 */
Markus Armbruster0e7a7592013-01-16 14:50:29 +01001204char *get_boot_devices_list(size_t *size)
Gleb Natapov962630f2010-12-08 13:35:09 +02001205{
1206 FWBootEntry *i;
Markus Armbruster0e7a7592013-01-16 14:50:29 +01001207 size_t total = 0;
Gleb Natapov962630f2010-12-08 13:35:09 +02001208 char *list = NULL;
1209
1210 QTAILQ_FOREACH(i, &fw_boot_order, link) {
1211 char *devpath = NULL, *bootpath;
Markus Armbruster0e7a7592013-01-16 14:50:29 +01001212 size_t len;
Gleb Natapov962630f2010-12-08 13:35:09 +02001213
1214 if (i->dev) {
1215 devpath = qdev_get_fw_dev_path(i->dev);
1216 assert(devpath);
1217 }
1218
1219 if (i->suffix && devpath) {
Blue Swirl4fd37a92010-12-19 14:05:43 +00001220 size_t bootpathlen = strlen(devpath) + strlen(i->suffix) + 1;
1221
Anthony Liguori7267c092011-08-20 22:09:37 -05001222 bootpath = g_malloc(bootpathlen);
Blue Swirl4fd37a92010-12-19 14:05:43 +00001223 snprintf(bootpath, bootpathlen, "%s%s", devpath, i->suffix);
Anthony Liguori7267c092011-08-20 22:09:37 -05001224 g_free(devpath);
Gleb Natapov962630f2010-12-08 13:35:09 +02001225 } else if (devpath) {
1226 bootpath = devpath;
1227 } else {
Markus Armbruster1bf6ccd2011-11-08 10:58:00 +01001228 assert(i->suffix);
Anthony Liguori7267c092011-08-20 22:09:37 -05001229 bootpath = g_strdup(i->suffix);
Gleb Natapov962630f2010-12-08 13:35:09 +02001230 }
1231
1232 if (total) {
1233 list[total-1] = '\n';
1234 }
1235 len = strlen(bootpath) + 1;
Anthony Liguori7267c092011-08-20 22:09:37 -05001236 list = g_realloc(list, total + len);
Gleb Natapov962630f2010-12-08 13:35:09 +02001237 memcpy(&list[total], bootpath, len);
1238 total += len;
Anthony Liguori7267c092011-08-20 22:09:37 -05001239 g_free(bootpath);
Gleb Natapov962630f2010-12-08 13:35:09 +02001240 }
1241
1242 *size = total;
1243
1244 return list;
1245}
1246
Eduardo Habkost845e5bf2013-02-04 16:27:51 -02001247static void numa_node_parse_cpus(int nodenr, const char *cpus)
1248{
1249 char *endptr;
1250 unsigned long long value, endvalue;
1251
Eduardo Habkostc881e202013-02-04 16:27:52 -02001252 /* Empty CPU range strings will be considered valid, they will simply
1253 * not set any bit in the CPU bitmap.
1254 */
1255 if (!*cpus) {
1256 return;
Eduardo Habkost845e5bf2013-02-04 16:27:51 -02001257 }
1258
Eduardo Habkostc881e202013-02-04 16:27:52 -02001259 if (parse_uint(cpus, &value, &endptr, 10) < 0) {
1260 goto error;
1261 }
1262 if (*endptr == '-') {
1263 if (parse_uint_full(endptr + 1, &endvalue, 10) < 0) {
1264 goto error;
1265 }
1266 } else if (*endptr == '\0') {
1267 endvalue = value;
1268 } else {
1269 goto error;
1270 }
1271
1272 if (endvalue >= MAX_CPUMASK_BITS) {
Eduardo Habkost845e5bf2013-02-04 16:27:51 -02001273 endvalue = MAX_CPUMASK_BITS - 1;
1274 fprintf(stderr,
Eduardo Habkostc881e202013-02-04 16:27:52 -02001275 "qemu: NUMA: A max of %d VCPUs are supported\n",
Eduardo Habkost845e5bf2013-02-04 16:27:51 -02001276 MAX_CPUMASK_BITS);
1277 }
1278
Eduardo Habkostc881e202013-02-04 16:27:52 -02001279 if (endvalue < value) {
1280 goto error;
1281 }
1282
Eduardo Habkost845e5bf2013-02-04 16:27:51 -02001283 bitmap_set(node_cpumask[nodenr], value, endvalue-value+1);
Eduardo Habkostc881e202013-02-04 16:27:52 -02001284 return;
1285
1286error:
1287 fprintf(stderr, "qemu: Invalid NUMA CPU range: %s\n", cpus);
1288 exit(1);
Eduardo Habkost845e5bf2013-02-04 16:27:51 -02001289}
1290
aliguori268a3622009-04-21 22:30:27 +00001291static void numa_add(const char *optarg)
1292{
1293 char option[128];
1294 char *endptr;
Eduardo Habkoste4ce85b2013-02-04 16:27:49 -02001295 unsigned long long nodenr;
aliguori268a3622009-04-21 22:30:27 +00001296
Eduardo Habkost8f302cb2013-02-04 16:27:46 -02001297 optarg = get_opt_name(option, 128, optarg, ',');
1298 if (*optarg == ',') {
1299 optarg++;
1300 }
aliguori268a3622009-04-21 22:30:27 +00001301 if (!strcmp(option, "node")) {
Eduardo Habkostca4c6d32013-02-04 16:27:48 -02001302
1303 if (nb_numa_nodes >= MAX_NODES) {
1304 fprintf(stderr, "qemu: too many NUMA nodes\n");
1305 exit(1);
1306 }
1307
aliguori268a3622009-04-21 22:30:27 +00001308 if (get_param_value(option, 128, "nodeid", optarg) == 0) {
1309 nodenr = nb_numa_nodes;
1310 } else {
Eduardo Habkost5f139962013-02-04 16:27:50 -02001311 if (parse_uint_full(option, &nodenr, 10) < 0) {
1312 fprintf(stderr, "qemu: Invalid NUMA nodeid: %s\n", option);
1313 exit(1);
1314 }
aliguori268a3622009-04-21 22:30:27 +00001315 }
1316
Eduardo Habkoste4ce85b2013-02-04 16:27:49 -02001317 if (nodenr >= MAX_NODES) {
1318 fprintf(stderr, "qemu: invalid NUMA nodeid: %llu\n", nodenr);
1319 exit(1);
1320 }
1321
aliguori268a3622009-04-21 22:30:27 +00001322 if (get_param_value(option, 128, "mem", optarg) == 0) {
1323 node_mem[nodenr] = 0;
1324 } else {
Jes Sorensen70b4f4b2011-01-05 11:41:02 +01001325 int64_t sval;
Markus Armbrusterc03417b2011-11-22 09:46:02 +01001326 sval = strtosz(option, &endptr);
1327 if (sval < 0 || *endptr) {
Jes Sorensen9f9b17a2010-10-21 17:15:46 +02001328 fprintf(stderr, "qemu: invalid numa mem size: %s\n", optarg);
1329 exit(1);
aliguori268a3622009-04-21 22:30:27 +00001330 }
Jes Sorensen9f9b17a2010-10-21 17:15:46 +02001331 node_mem[nodenr] = sval;
aliguori268a3622009-04-21 22:30:27 +00001332 }
Chegu Vinodee785fe2012-07-16 21:31:30 -07001333 if (get_param_value(option, 128, "cpus", optarg) != 0) {
Eduardo Habkost845e5bf2013-02-04 16:27:51 -02001334 numa_node_parse_cpus(nodenr, option);
aliguori268a3622009-04-21 22:30:27 +00001335 }
1336 nb_numa_nodes++;
Eduardo Habkost12e53a92013-02-04 16:27:47 -02001337 } else {
1338 fprintf(stderr, "Invalid -numa option: %s\n", option);
1339 exit(1);
aliguori268a3622009-04-21 22:30:27 +00001340 }
aliguori268a3622009-04-21 22:30:27 +00001341}
1342
Andre Przywaradc6b1c02009-08-19 15:42:40 +02001343static void smp_parse(const char *optarg)
1344{
1345 int smp, sockets = 0, threads = 0, cores = 0;
1346 char *endptr;
1347 char option[128];
1348
1349 smp = strtoul(optarg, &endptr, 10);
1350 if (endptr != optarg) {
1351 if (*endptr == ',') {
1352 endptr++;
1353 }
1354 }
1355 if (get_param_value(option, 128, "sockets", endptr) != 0)
1356 sockets = strtoull(option, NULL, 10);
1357 if (get_param_value(option, 128, "cores", endptr) != 0)
1358 cores = strtoull(option, NULL, 10);
1359 if (get_param_value(option, 128, "threads", endptr) != 0)
1360 threads = strtoull(option, NULL, 10);
1361 if (get_param_value(option, 128, "maxcpus", endptr) != 0)
1362 max_cpus = strtoull(option, NULL, 10);
1363
1364 /* compute missing values, prefer sockets over cores over threads */
1365 if (smp == 0 || sockets == 0) {
1366 sockets = sockets > 0 ? sockets : 1;
1367 cores = cores > 0 ? cores : 1;
1368 threads = threads > 0 ? threads : 1;
1369 if (smp == 0) {
1370 smp = cores * threads * sockets;
Andre Przywaradc6b1c02009-08-19 15:42:40 +02001371 }
1372 } else {
1373 if (cores == 0) {
1374 threads = threads > 0 ? threads : 1;
1375 cores = smp / (sockets * threads);
1376 } else {
Joel Schoppdca98162010-07-21 15:05:17 -05001377 threads = smp / (cores * sockets);
Andre Przywaradc6b1c02009-08-19 15:42:40 +02001378 }
1379 }
1380 smp_cpus = smp;
1381 smp_cores = cores > 0 ? cores : 1;
1382 smp_threads = threads > 0 ? threads : 1;
1383 if (max_cpus == 0)
1384 max_cpus = smp_cpus;
1385}
1386
bellard330d0412003-07-26 18:11:40 +00001387/***********************************************************/
bellarda594cfb2005-11-06 16:13:29 +00001388/* USB devices */
1389
Markus Armbrusterfb080002010-05-28 15:38:44 +02001390static int usb_device_add(const char *devname)
bellarda594cfb2005-11-06 16:13:29 +00001391{
1392 const char *p;
Gerd Hoffmanna5d2f722009-08-31 14:24:00 +02001393 USBDevice *dev = NULL;
bellarda594cfb2005-11-06 16:13:29 +00001394
zhlcindy@gmail.com094b2872012-09-02 19:25:28 +00001395 if (!usb_enabled(false)) {
bellarda594cfb2005-11-06 16:13:29 +00001396 return -1;
zhlcindy@gmail.com094b2872012-09-02 19:25:28 +00001397 }
bellarda594cfb2005-11-06 16:13:29 +00001398
Gerd Hoffmann0958b4c2009-10-26 15:56:45 +01001399 /* drivers with .usbdevice_name entry in USBDeviceInfo */
1400 dev = usbdevice_create(devname);
1401 if (dev)
1402 goto done;
1403
Gerd Hoffmanna5d2f722009-08-31 14:24:00 +02001404 /* the other ones */
Gerd Hoffmanne447fc62011-06-01 14:41:59 +02001405#ifndef CONFIG_LINUX
1406 /* only the linux version is qdev-ified, usb-bsd still needs this */
bellarda594cfb2005-11-06 16:13:29 +00001407 if (strstart(devname, "host:", &p)) {
Jan Kiszka37417152012-02-27 15:18:47 +01001408 dev = usb_host_device_open(usb_bus_find(-1), p);
Gerd Hoffmanne447fc62011-06-01 14:41:59 +02001409 } else
1410#endif
1411 if (!strcmp(devname, "bt") || strstart(devname, "bt:", &p)) {
Jan Kiszka37417152012-02-27 15:18:47 +01001412 dev = usb_bt_init(usb_bus_find(-1),
1413 devname[2] ? hci_init(p)
1414 : bt_new_hci(qemu_find_bt_vlan(0)));
bellarda594cfb2005-11-06 16:13:29 +00001415 } else {
1416 return -1;
1417 }
pbrook0d92ed32006-05-21 16:30:15 +00001418 if (!dev)
1419 return -1;
1420
Gerd Hoffmanna5d2f722009-08-31 14:24:00 +02001421done:
bellarda594cfb2005-11-06 16:13:29 +00001422 return 0;
1423}
1424
aliguori1f3870a2008-08-21 19:27:48 +00001425static int usb_device_del(const char *devname)
1426{
1427 int bus_num, addr;
1428 const char *p;
1429
aliguori5d0c5752008-09-14 01:07:41 +00001430 if (strstart(devname, "host:", &p))
1431 return usb_host_device_close(p);
1432
zhlcindy@gmail.com094b2872012-09-02 19:25:28 +00001433 if (!usb_enabled(false)) {
aliguori1f3870a2008-08-21 19:27:48 +00001434 return -1;
zhlcindy@gmail.com094b2872012-09-02 19:25:28 +00001435 }
aliguori1f3870a2008-08-21 19:27:48 +00001436
1437 p = strchr(devname, '.');
1438 if (!p)
1439 return -1;
1440 bus_num = strtoul(devname, NULL, 0);
1441 addr = strtoul(p + 1, NULL, 0);
1442
Gerd Hoffmanna5d2f722009-08-31 14:24:00 +02001443 return usb_device_delete_addr(bus_num, addr);
aliguori1f3870a2008-08-21 19:27:48 +00001444}
1445
Gerd Hoffmannbd3c9482009-07-15 13:59:26 +02001446static int usb_parse(const char *cmdline)
1447{
Scott Tsai59d1c1c2009-12-23 04:30:18 +08001448 int r;
Markus Armbrusterfb080002010-05-28 15:38:44 +02001449 r = usb_device_add(cmdline);
Scott Tsai59d1c1c2009-12-23 04:30:18 +08001450 if (r < 0) {
1451 fprintf(stderr, "qemu: could not add USB device '%s'\n", cmdline);
1452 }
1453 return r;
Gerd Hoffmannbd3c9482009-07-15 13:59:26 +02001454}
1455
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001456void do_usb_add(Monitor *mon, const QDict *qdict)
bellarda594cfb2005-11-06 16:13:29 +00001457{
Scott Tsai59d1c1c2009-12-23 04:30:18 +08001458 const char *devname = qdict_get_str(qdict, "devname");
Markus Armbrusterfb080002010-05-28 15:38:44 +02001459 if (usb_device_add(devname) < 0) {
Markus Armbruster1ecda022010-02-18 17:25:24 +01001460 error_report("could not add USB device '%s'", devname);
Scott Tsai59d1c1c2009-12-23 04:30:18 +08001461 }
bellarda594cfb2005-11-06 16:13:29 +00001462}
1463
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001464void do_usb_del(Monitor *mon, const QDict *qdict)
bellarda594cfb2005-11-06 16:13:29 +00001465{
Scott Tsai59d1c1c2009-12-23 04:30:18 +08001466 const char *devname = qdict_get_str(qdict, "devname");
1467 if (usb_device_del(devname) < 0) {
Markus Armbruster1ecda022010-02-18 17:25:24 +01001468 error_report("could not delete USB device '%s'", devname);
Scott Tsai59d1c1c2009-12-23 04:30:18 +08001469 }
bellarda594cfb2005-11-06 16:13:29 +00001470}
1471
bellardf7cce892004-12-08 22:21:25 +00001472/***********************************************************/
balrog201a51f2007-04-30 00:51:09 +00001473/* PCMCIA/Cardbus */
1474
1475static struct pcmcia_socket_entry_s {
Paul Brookbc24a222009-05-10 01:44:56 +01001476 PCMCIASocket *socket;
balrog201a51f2007-04-30 00:51:09 +00001477 struct pcmcia_socket_entry_s *next;
1478} *pcmcia_sockets = 0;
1479
Paul Brookbc24a222009-05-10 01:44:56 +01001480void pcmcia_socket_register(PCMCIASocket *socket)
balrog201a51f2007-04-30 00:51:09 +00001481{
1482 struct pcmcia_socket_entry_s *entry;
1483
Anthony Liguori7267c092011-08-20 22:09:37 -05001484 entry = g_malloc(sizeof(struct pcmcia_socket_entry_s));
balrog201a51f2007-04-30 00:51:09 +00001485 entry->socket = socket;
1486 entry->next = pcmcia_sockets;
1487 pcmcia_sockets = entry;
1488}
1489
Paul Brookbc24a222009-05-10 01:44:56 +01001490void pcmcia_socket_unregister(PCMCIASocket *socket)
balrog201a51f2007-04-30 00:51:09 +00001491{
1492 struct pcmcia_socket_entry_s *entry, **ptr;
1493
1494 ptr = &pcmcia_sockets;
1495 for (entry = *ptr; entry; ptr = &entry->next, entry = *ptr)
1496 if (entry->socket == socket) {
1497 *ptr = entry->next;
Anthony Liguori7267c092011-08-20 22:09:37 -05001498 g_free(entry);
balrog201a51f2007-04-30 00:51:09 +00001499 }
1500}
1501
Wenchao Xia84f2d0e2013-01-14 14:06:25 +08001502void pcmcia_info(Monitor *mon, const QDict *qdict)
balrog201a51f2007-04-30 00:51:09 +00001503{
1504 struct pcmcia_socket_entry_s *iter;
aliguori376253e2009-03-05 23:01:23 +00001505
balrog201a51f2007-04-30 00:51:09 +00001506 if (!pcmcia_sockets)
aliguori376253e2009-03-05 23:01:23 +00001507 monitor_printf(mon, "No PCMCIA sockets\n");
balrog201a51f2007-04-30 00:51:09 +00001508
1509 for (iter = pcmcia_sockets; iter; iter = iter->next)
aliguori376253e2009-03-05 23:01:23 +00001510 monitor_printf(mon, "%s: %s\n", iter->socket->slot_string,
1511 iter->socket->attached ? iter->socket->card_string :
1512 "Empty");
balrog201a51f2007-04-30 00:51:09 +00001513}
1514
1515/***********************************************************/
bellardcc1daa42005-06-05 14:49:17 +00001516/* machine registration */
1517
blueswir1bdaf78e2008-10-04 07:24:27 +00001518static QEMUMachine *first_machine = NULL;
aliguori6f338c32009-02-11 15:21:54 +00001519QEMUMachine *current_machine = NULL;
bellardcc1daa42005-06-05 14:49:17 +00001520
1521int qemu_register_machine(QEMUMachine *m)
1522{
1523 QEMUMachine **pm;
1524 pm = &first_machine;
1525 while (*pm != NULL)
1526 pm = &(*pm)->next;
1527 m->next = NULL;
1528 *pm = m;
1529 return 0;
1530}
1531
pbrook9596ebb2007-11-18 01:44:38 +00001532static QEMUMachine *find_machine(const char *name)
bellardcc1daa42005-06-05 14:49:17 +00001533{
1534 QEMUMachine *m;
1535
1536 for(m = first_machine; m != NULL; m = m->next) {
1537 if (!strcmp(m->name, name))
1538 return m;
Mark McLoughlin3f6599e2009-07-22 10:02:50 +01001539 if (m->alias && !strcmp(m->alias, name))
1540 return m;
bellardcc1daa42005-06-05 14:49:17 +00001541 }
1542 return NULL;
1543}
1544
Jordan Justen2c8cffa2012-02-21 23:18:50 -08001545QEMUMachine *find_default_machine(void)
Anthony Liguori0c257432009-05-21 20:41:01 -05001546{
1547 QEMUMachine *m;
1548
1549 for(m = first_machine; m != NULL; m = m->next) {
1550 if (m->is_default) {
1551 return m;
1552 }
1553 }
1554 return NULL;
1555}
1556
Anthony Liguori01d3c802012-08-10 11:04:11 -05001557MachineInfoList *qmp_query_machines(Error **errp)
1558{
1559 MachineInfoList *mach_list = NULL;
1560 QEMUMachine *m;
1561
1562 for (m = first_machine; m; m = m->next) {
1563 MachineInfoList *entry;
1564 MachineInfo *info;
1565
1566 info = g_malloc0(sizeof(*info));
1567 if (m->is_default) {
1568 info->has_is_default = true;
1569 info->is_default = true;
1570 }
1571
1572 if (m->alias) {
1573 info->has_alias = true;
1574 info->alias = g_strdup(m->alias);
1575 }
1576
1577 info->name = g_strdup(m->name);
1578
1579 entry = g_malloc0(sizeof(*entry));
1580 entry->value = info;
1581 entry->next = mach_list;
1582 mach_list = entry;
1583 }
1584
1585 return mach_list;
1586}
1587
bellardcc1daa42005-06-05 14:49:17 +00001588/***********************************************************/
bellard8a7ddc32004-03-31 19:00:16 +00001589/* main execution loop */
1590
pbrook9596ebb2007-11-18 01:44:38 +00001591static void gui_update(void *opaque)
bellard8a7ddc32004-03-31 19:00:16 +00001592{
aliguori7d957bd2009-01-15 22:14:11 +00001593 uint64_t interval = GUI_REFRESH_INTERVAL;
ths740733b2007-06-08 01:57:56 +00001594 DisplayState *ds = opaque;
Gerd Hoffmann87e487a2010-06-04 11:46:35 +02001595 DisplayChangeListener *dcl;
aliguori7d957bd2009-01-15 22:14:11 +00001596
1597 dpy_refresh(ds);
1598
Gerd Hoffmann87e487a2010-06-04 11:46:35 +02001599 QLIST_FOREACH(dcl, &ds->listeners, next) {
aliguori7d957bd2009-01-15 22:14:11 +00001600 if (dcl->gui_timer_interval &&
1601 dcl->gui_timer_interval < interval)
1602 interval = dcl->gui_timer_interval;
aliguori7d957bd2009-01-15 22:14:11 +00001603 }
Paolo Bonzini7bd427d2011-03-11 16:47:48 +01001604 qemu_mod_timer(ds->gui_timer, interval + qemu_get_clock_ms(rt_clock));
bellard8a7ddc32004-03-31 19:00:16 +00001605}
1606
Gerd Hoffmann35c9e0a2010-06-04 11:51:31 +02001607void gui_setup_refresh(DisplayState *ds)
1608{
1609 DisplayChangeListener *dcl;
1610 bool need_timer = false;
Gerd Hoffmanna93a4a22012-09-28 15:02:08 +02001611 bool have_gfx = false;
1612 bool have_text = false;
Gerd Hoffmann35c9e0a2010-06-04 11:51:31 +02001613
1614 QLIST_FOREACH(dcl, &ds->listeners, next) {
1615 if (dcl->dpy_refresh != NULL) {
1616 need_timer = true;
Gerd Hoffmanna93a4a22012-09-28 15:02:08 +02001617 }
1618 if (dcl->dpy_gfx_update != NULL) {
1619 have_gfx = true;
1620 }
1621 if (dcl->dpy_text_update != NULL) {
1622 have_text = true;
Gerd Hoffmann35c9e0a2010-06-04 11:51:31 +02001623 }
1624 }
1625
1626 if (need_timer && ds->gui_timer == NULL) {
1627 ds->gui_timer = qemu_new_timer_ms(rt_clock, gui_update, ds);
1628 qemu_mod_timer(ds->gui_timer, qemu_get_clock_ms(rt_clock));
1629 }
1630 if (!need_timer && ds->gui_timer != NULL) {
1631 qemu_del_timer(ds->gui_timer);
1632 qemu_free_timer(ds->gui_timer);
1633 ds->gui_timer = NULL;
1634 }
Gerd Hoffmanna93a4a22012-09-28 15:02:08 +02001635
1636 ds->have_gfx = have_gfx;
1637 ds->have_text = have_text;
Gerd Hoffmann35c9e0a2010-06-04 11:51:31 +02001638}
1639
bellard0bd48852005-11-11 00:00:47 +00001640struct vm_change_state_entry {
1641 VMChangeStateHandler *cb;
1642 void *opaque;
Blue Swirl72cf2d42009-09-12 07:36:22 +00001643 QLIST_ENTRY (vm_change_state_entry) entries;
bellard0bd48852005-11-11 00:00:47 +00001644};
1645
Blue Swirl72cf2d42009-09-12 07:36:22 +00001646static QLIST_HEAD(vm_change_state_head, vm_change_state_entry) vm_change_state_head;
bellard0bd48852005-11-11 00:00:47 +00001647
1648VMChangeStateEntry *qemu_add_vm_change_state_handler(VMChangeStateHandler *cb,
1649 void *opaque)
1650{
1651 VMChangeStateEntry *e;
1652
Anthony Liguori7267c092011-08-20 22:09:37 -05001653 e = g_malloc0(sizeof (*e));
bellard0bd48852005-11-11 00:00:47 +00001654
1655 e->cb = cb;
1656 e->opaque = opaque;
Blue Swirl72cf2d42009-09-12 07:36:22 +00001657 QLIST_INSERT_HEAD(&vm_change_state_head, e, entries);
bellard0bd48852005-11-11 00:00:47 +00001658 return e;
1659}
1660
1661void qemu_del_vm_change_state_handler(VMChangeStateEntry *e)
1662{
Blue Swirl72cf2d42009-09-12 07:36:22 +00001663 QLIST_REMOVE (e, entries);
Anthony Liguori7267c092011-08-20 22:09:37 -05001664 g_free (e);
bellard0bd48852005-11-11 00:00:47 +00001665}
1666
Luiz Capitulino1dfb4dd2011-07-29 14:26:33 -03001667void vm_state_notify(int running, RunState state)
bellard0bd48852005-11-11 00:00:47 +00001668{
1669 VMChangeStateEntry *e;
1670
Luiz Capitulino1dfb4dd2011-07-29 14:26:33 -03001671 trace_vm_state_notify(running, state);
Stefan Hajnoczi94b0b5f2010-11-16 12:20:25 +00001672
bellard0bd48852005-11-11 00:00:47 +00001673 for (e = vm_change_state_head.lh_first; e; e = e->entries.le_next) {
Luiz Capitulino1dfb4dd2011-07-29 14:26:33 -03001674 e->cb(e->opaque, running, state);
bellard0bd48852005-11-11 00:00:47 +00001675 }
1676}
1677
bellard8a7ddc32004-03-31 19:00:16 +00001678void vm_start(void)
1679{
Luiz Capitulino13548692011-07-29 15:36:43 -03001680 if (!runstate_is_running()) {
bellard8a7ddc32004-03-31 19:00:16 +00001681 cpu_enable_ticks();
Luiz Capitulino0461d5a2011-09-30 14:45:27 -03001682 runstate_set(RUN_STATE_RUNNING);
1683 vm_state_notify(1, RUN_STATE_RUNNING);
aliguorid6dc3d42009-04-24 18:04:07 +00001684 resume_all_vcpus();
Luiz Capitulino6ed2c482010-04-27 20:35:59 -03001685 monitor_protocol_event(QEVENT_RESUME, NULL);
bellard8a7ddc32004-03-31 19:00:16 +00001686 }
1687}
1688
bellardbb0c6722004-06-20 12:37:32 +00001689/* reset/shutdown handler */
1690
1691typedef struct QEMUResetEntry {
Blue Swirl72cf2d42009-09-12 07:36:22 +00001692 QTAILQ_ENTRY(QEMUResetEntry) entry;
bellardbb0c6722004-06-20 12:37:32 +00001693 QEMUResetHandler *func;
1694 void *opaque;
bellardbb0c6722004-06-20 12:37:32 +00001695} QEMUResetEntry;
1696
Blue Swirl72cf2d42009-09-12 07:36:22 +00001697static QTAILQ_HEAD(reset_handlers, QEMUResetEntry) reset_handlers =
1698 QTAILQ_HEAD_INITIALIZER(reset_handlers);
bellardbb0c6722004-06-20 12:37:32 +00001699static int reset_requested;
Gleb Natapovf64622c2011-03-15 13:56:04 +02001700static int shutdown_requested, shutdown_signal = -1;
1701static pid_t shutdown_pid;
bellard34751872005-07-02 14:31:34 +00001702static int powerdown_requested;
Jan Kiszka8cf71712011-02-07 12:19:16 +01001703static int debug_requested;
Gerd Hoffmann95b363b2012-02-23 13:45:19 +01001704static int suspend_requested;
Luiz Capitulino14058192012-08-08 17:29:17 -03001705static int wakeup_requested;
Igor Mammedova9552c82012-09-05 23:06:21 +02001706static NotifierList powerdown_notifiers =
1707 NOTIFIER_LIST_INITIALIZER(powerdown_notifiers);
Gerd Hoffmann95b363b2012-02-23 13:45:19 +01001708static NotifierList suspend_notifiers =
1709 NOTIFIER_LIST_INITIALIZER(suspend_notifiers);
1710static NotifierList wakeup_notifiers =
1711 NOTIFIER_LIST_INITIALIZER(wakeup_notifiers);
1712static uint32_t wakeup_reason_mask = ~0;
Luiz Capitulino0461d5a2011-09-30 14:45:27 -03001713static RunState vmstop_requested = RUN_STATE_MAX;
bellardbb0c6722004-06-20 12:37:32 +00001714
Anthony PERARD1291eb32010-07-22 15:52:48 +01001715int qemu_shutdown_requested_get(void)
1716{
1717 return shutdown_requested;
1718}
1719
1720int qemu_reset_requested_get(void)
1721{
1722 return reset_requested;
1723}
1724
Blue Swirl4fdcac02012-10-28 11:04:47 +00001725static int qemu_shutdown_requested(void)
aurel32cf7a2fe2008-03-18 06:53:05 +00001726{
1727 int r = shutdown_requested;
1728 shutdown_requested = 0;
1729 return r;
1730}
1731
Blue Swirl4fdcac02012-10-28 11:04:47 +00001732static void qemu_kill_report(void)
Gleb Natapovf64622c2011-03-15 13:56:04 +02001733{
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +02001734 if (!qtest_enabled() && shutdown_signal != -1) {
Peter Maydellf1d3fb02011-03-30 22:03:38 +01001735 fprintf(stderr, "qemu: terminating on signal %d", shutdown_signal);
1736 if (shutdown_pid == 0) {
1737 /* This happens for eg ^C at the terminal, so it's worth
1738 * avoiding printing an odd message in that case.
1739 */
1740 fputc('\n', stderr);
1741 } else {
Andreas Färber953ffe02011-06-02 19:58:06 +02001742 fprintf(stderr, " from pid " FMT_pid "\n", shutdown_pid);
Peter Maydellf1d3fb02011-03-30 22:03:38 +01001743 }
Gleb Natapovf64622c2011-03-15 13:56:04 +02001744 shutdown_signal = -1;
1745 }
1746}
1747
Blue Swirl4fdcac02012-10-28 11:04:47 +00001748static int qemu_reset_requested(void)
aurel32cf7a2fe2008-03-18 06:53:05 +00001749{
1750 int r = reset_requested;
1751 reset_requested = 0;
1752 return r;
1753}
1754
Gerd Hoffmann95b363b2012-02-23 13:45:19 +01001755static int qemu_suspend_requested(void)
1756{
1757 int r = suspend_requested;
1758 suspend_requested = 0;
1759 return r;
1760}
1761
Luiz Capitulino14058192012-08-08 17:29:17 -03001762static int qemu_wakeup_requested(void)
1763{
1764 int r = wakeup_requested;
1765 wakeup_requested = 0;
1766 return r;
1767}
1768
Blue Swirl4fdcac02012-10-28 11:04:47 +00001769static int qemu_powerdown_requested(void)
aurel32cf7a2fe2008-03-18 06:53:05 +00001770{
1771 int r = powerdown_requested;
1772 powerdown_requested = 0;
1773 return r;
1774}
1775
aliguorie5689022009-04-24 18:03:54 +00001776static int qemu_debug_requested(void)
1777{
1778 int r = debug_requested;
1779 debug_requested = 0;
1780 return r;
1781}
1782
Luiz Capitulino0461d5a2011-09-30 14:45:27 -03001783/* We use RUN_STATE_MAX but any invalid value will do */
Luiz Capitulino0d45b702011-09-30 14:27:11 -03001784static bool qemu_vmstop_requested(RunState *r)
aliguori6e29f5d2009-04-24 18:04:02 +00001785{
Luiz Capitulino0461d5a2011-09-30 14:45:27 -03001786 if (vmstop_requested < RUN_STATE_MAX) {
Luiz Capitulino0d45b702011-09-30 14:27:11 -03001787 *r = vmstop_requested;
Luiz Capitulino0461d5a2011-09-30 14:45:27 -03001788 vmstop_requested = RUN_STATE_MAX;
Luiz Capitulino0d45b702011-09-30 14:27:11 -03001789 return true;
1790 }
1791
1792 return false;
aliguori6e29f5d2009-04-24 18:04:02 +00001793}
1794
Jan Kiszkaa08d4362009-06-27 09:25:07 +02001795void qemu_register_reset(QEMUResetHandler *func, void *opaque)
bellardbb0c6722004-06-20 12:37:32 +00001796{
Anthony Liguori7267c092011-08-20 22:09:37 -05001797 QEMUResetEntry *re = g_malloc0(sizeof(QEMUResetEntry));
bellardbb0c6722004-06-20 12:37:32 +00001798
bellardbb0c6722004-06-20 12:37:32 +00001799 re->func = func;
1800 re->opaque = opaque;
Blue Swirl72cf2d42009-09-12 07:36:22 +00001801 QTAILQ_INSERT_TAIL(&reset_handlers, re, entry);
bellardbb0c6722004-06-20 12:37:32 +00001802}
1803
Jan Kiszkadda9b292009-07-02 00:19:02 +02001804void qemu_unregister_reset(QEMUResetHandler *func, void *opaque)
bellardbb0c6722004-06-20 12:37:32 +00001805{
1806 QEMUResetEntry *re;
1807
Blue Swirl72cf2d42009-09-12 07:36:22 +00001808 QTAILQ_FOREACH(re, &reset_handlers, entry) {
Jan Kiszkadda9b292009-07-02 00:19:02 +02001809 if (re->func == func && re->opaque == opaque) {
Blue Swirl72cf2d42009-09-12 07:36:22 +00001810 QTAILQ_REMOVE(&reset_handlers, re, entry);
Anthony Liguori7267c092011-08-20 22:09:37 -05001811 g_free(re);
Jan Kiszkadda9b292009-07-02 00:19:02 +02001812 return;
1813 }
1814 }
1815}
1816
David Gibsonbe522022012-08-07 16:41:51 +10001817void qemu_devices_reset(void)
Jan Kiszkadda9b292009-07-02 00:19:02 +02001818{
1819 QEMUResetEntry *re, *nre;
1820
1821 /* reset all devices */
Blue Swirl72cf2d42009-09-12 07:36:22 +00001822 QTAILQ_FOREACH_SAFE(re, &reset_handlers, entry, nre) {
bellardbb0c6722004-06-20 12:37:32 +00001823 re->func(re->opaque);
1824 }
David Gibsonbe522022012-08-07 16:41:51 +10001825}
1826
1827void qemu_system_reset(bool report)
1828{
1829 if (current_machine && current_machine->reset) {
1830 current_machine->reset();
1831 } else {
1832 qemu_devices_reset();
1833 }
Jan Kiszkae063eb12011-06-14 18:29:43 +02001834 if (report) {
1835 monitor_protocol_event(QEVENT_RESET, NULL);
1836 }
Jan Kiszkaea375f92010-03-01 19:10:30 +01001837 cpu_synchronize_all_post_reset();
bellardbb0c6722004-06-20 12:37:32 +00001838}
1839
1840void qemu_system_reset_request(void)
1841{
bellardd1beab82006-10-02 19:44:22 +00001842 if (no_reboot) {
1843 shutdown_requested = 1;
1844 } else {
1845 reset_requested = 1;
1846 }
Jan Kiszkab4a3d962011-02-01 22:15:43 +01001847 cpu_stop_current();
aliguorid9f75a42009-04-24 18:03:11 +00001848 qemu_notify_event();
bellardbb0c6722004-06-20 12:37:32 +00001849}
1850
Gerd Hoffmann95b363b2012-02-23 13:45:19 +01001851static void qemu_system_suspend(void)
1852{
1853 pause_all_vcpus();
1854 notifier_list_notify(&suspend_notifiers, NULL);
Luiz Capitulinoad02b962012-04-27 13:33:36 -03001855 runstate_set(RUN_STATE_SUSPENDED);
Gerd Hoffmann53370b72012-02-23 13:45:26 +01001856 monitor_protocol_event(QEVENT_SUSPEND, NULL);
Gerd Hoffmann95b363b2012-02-23 13:45:19 +01001857}
1858
1859void qemu_system_suspend_request(void)
1860{
Luiz Capitulino9abc62f2012-04-27 14:31:12 -03001861 if (runstate_check(RUN_STATE_SUSPENDED)) {
Gerd Hoffmann95b363b2012-02-23 13:45:19 +01001862 return;
1863 }
1864 suspend_requested = 1;
1865 cpu_stop_current();
1866 qemu_notify_event();
1867}
1868
1869void qemu_register_suspend_notifier(Notifier *notifier)
1870{
1871 notifier_list_add(&suspend_notifiers, notifier);
1872}
1873
1874void qemu_system_wakeup_request(WakeupReason reason)
1875{
Luiz Capitulino9abc62f2012-04-27 14:31:12 -03001876 if (!runstate_check(RUN_STATE_SUSPENDED)) {
Gerd Hoffmann95b363b2012-02-23 13:45:19 +01001877 return;
1878 }
1879 if (!(wakeup_reason_mask & (1 << reason))) {
1880 return;
1881 }
Luiz Capitulinoad02b962012-04-27 13:33:36 -03001882 runstate_set(RUN_STATE_RUNNING);
Gerd Hoffmann95b363b2012-02-23 13:45:19 +01001883 notifier_list_notify(&wakeup_notifiers, &reason);
Luiz Capitulino14058192012-08-08 17:29:17 -03001884 wakeup_requested = 1;
Gerd Hoffmann95b363b2012-02-23 13:45:19 +01001885 qemu_notify_event();
Gerd Hoffmann95b363b2012-02-23 13:45:19 +01001886}
1887
1888void qemu_system_wakeup_enable(WakeupReason reason, bool enabled)
1889{
1890 if (enabled) {
1891 wakeup_reason_mask |= (1 << reason);
1892 } else {
1893 wakeup_reason_mask &= ~(1 << reason);
1894 }
1895}
1896
1897void qemu_register_wakeup_notifier(Notifier *notifier)
1898{
1899 notifier_list_add(&wakeup_notifiers, notifier);
1900}
1901
Gleb Natapovf64622c2011-03-15 13:56:04 +02001902void qemu_system_killed(int signal, pid_t pid)
1903{
1904 shutdown_signal = signal;
1905 shutdown_pid = pid;
Kevin Wolfd9389b92011-09-14 15:38:40 +02001906 no_shutdown = 0;
Gleb Natapovf64622c2011-03-15 13:56:04 +02001907 qemu_system_shutdown_request();
1908}
1909
bellardbb0c6722004-06-20 12:37:32 +00001910void qemu_system_shutdown_request(void)
1911{
1912 shutdown_requested = 1;
aliguorid9f75a42009-04-24 18:03:11 +00001913 qemu_notify_event();
bellardbb0c6722004-06-20 12:37:32 +00001914}
1915
Igor Mammedov013c2f12012-09-05 23:06:25 +02001916static void qemu_system_powerdown(void)
1917{
1918 monitor_protocol_event(QEVENT_POWERDOWN, NULL);
1919 notifier_list_notify(&powerdown_notifiers, NULL);
1920}
1921
bellard34751872005-07-02 14:31:34 +00001922void qemu_system_powerdown_request(void)
1923{
1924 powerdown_requested = 1;
aliguorid9f75a42009-04-24 18:03:11 +00001925 qemu_notify_event();
1926}
1927
Igor Mammedova9552c82012-09-05 23:06:21 +02001928void qemu_register_powerdown_notifier(Notifier *notifier)
1929{
1930 notifier_list_add(&powerdown_notifiers, notifier);
1931}
1932
Jan Kiszka8cf71712011-02-07 12:19:16 +01001933void qemu_system_debug_request(void)
1934{
1935 debug_requested = 1;
Jan Kiszka83f338f2011-02-07 12:19:17 +01001936 qemu_notify_event();
Jan Kiszka8cf71712011-02-07 12:19:16 +01001937}
1938
Luiz Capitulino1dfb4dd2011-07-29 14:26:33 -03001939void qemu_system_vmstop_request(RunState state)
Jan Kiszka8cf71712011-02-07 12:19:16 +01001940{
Luiz Capitulino1dfb4dd2011-07-29 14:26:33 -03001941 vmstop_requested = state;
Jan Kiszka8cf71712011-02-07 12:19:16 +01001942 qemu_notify_event();
1943}
1944
Paolo Bonzini99435902011-09-12 14:03:13 +02001945static bool main_loop_should_exit(void)
1946{
1947 RunState r;
1948 if (qemu_debug_requested()) {
1949 vm_stop(RUN_STATE_DEBUG);
1950 }
Gerd Hoffmann95b363b2012-02-23 13:45:19 +01001951 if (qemu_suspend_requested()) {
1952 qemu_system_suspend();
1953 }
Paolo Bonzini99435902011-09-12 14:03:13 +02001954 if (qemu_shutdown_requested()) {
1955 qemu_kill_report();
1956 monitor_protocol_event(QEVENT_SHUTDOWN, NULL);
1957 if (no_shutdown) {
1958 vm_stop(RUN_STATE_SHUTDOWN);
1959 } else {
1960 return true;
1961 }
1962 }
1963 if (qemu_reset_requested()) {
1964 pause_all_vcpus();
1965 cpu_synchronize_all_states();
1966 qemu_system_reset(VMRESET_REPORT);
1967 resume_all_vcpus();
1968 if (runstate_check(RUN_STATE_INTERNAL_ERROR) ||
1969 runstate_check(RUN_STATE_SHUTDOWN)) {
1970 runstate_set(RUN_STATE_PAUSED);
1971 }
1972 }
Luiz Capitulino14058192012-08-08 17:29:17 -03001973 if (qemu_wakeup_requested()) {
1974 pause_all_vcpus();
1975 cpu_synchronize_all_states();
1976 qemu_system_reset(VMRESET_SILENT);
1977 resume_all_vcpus();
Luiz Capitulino17c86602012-08-09 11:27:30 -03001978 monitor_protocol_event(QEVENT_WAKEUP, NULL);
Luiz Capitulino14058192012-08-08 17:29:17 -03001979 }
Paolo Bonzini99435902011-09-12 14:03:13 +02001980 if (qemu_powerdown_requested()) {
Igor Mammedov013c2f12012-09-05 23:06:25 +02001981 qemu_system_powerdown();
Paolo Bonzini99435902011-09-12 14:03:13 +02001982 }
1983 if (qemu_vmstop_requested(&r)) {
1984 vm_stop(r);
1985 }
1986 return false;
1987}
1988
aliguori43b96852009-04-24 18:03:33 +00001989static void main_loop(void)
1990{
Jan Kiszkac9f711a2011-08-22 17:46:02 +02001991 bool nonblocking;
Paolo Bonzini99435902011-09-12 14:03:13 +02001992 int last_io = 0;
Jan Kiszka8e1b90e2011-02-01 22:15:46 +01001993#ifdef CONFIG_PROFILER
1994 int64_t ti;
1995#endif
Paolo Bonzini99435902011-09-12 14:03:13 +02001996 do {
Jan Kiszkac9f711a2011-08-22 17:46:02 +02001997 nonblocking = !kvm_enabled() && last_io > 0;
aliguori43b96852009-04-24 18:03:33 +00001998#ifdef CONFIG_PROFILER
Jan Kiszka46481d32011-02-01 22:15:47 +01001999 ti = profile_getclock();
aliguori43b96852009-04-24 18:03:33 +00002000#endif
Jan Kiszkac9f711a2011-08-22 17:46:02 +02002001 last_io = main_loop_wait(nonblocking);
aliguori43b96852009-04-24 18:03:33 +00002002#ifdef CONFIG_PROFILER
Jan Kiszka46481d32011-02-01 22:15:47 +01002003 dev_time += profile_getclock() - ti;
aliguori43b96852009-04-24 18:03:33 +00002004#endif
Paolo Bonzini99435902011-09-12 14:03:13 +02002005 } while (!main_loop_should_exit());
bellardb4608c02003-06-27 17:34:32 +00002006}
2007
pbrook9bd7e6d2009-04-07 22:58:45 +00002008static void version(void)
2009{
Thomas Monjalonf75ca1a2010-04-28 14:42:01 +02002010 printf("QEMU emulator version " QEMU_VERSION QEMU_PKGVERSION ", Copyright (c) 2003-2008 Fabrice Bellard\n");
pbrook9bd7e6d2009-04-07 22:58:45 +00002011}
2012
ths15f82202007-06-29 23:26:08 +00002013static void help(int exitcode)
bellard0824d6f2003-06-24 13:42:40 +00002014{
Michael Ellermana3adb7a2011-12-19 17:19:31 +11002015 version();
2016 printf("usage: %s [options] [disk_image]\n\n"
2017 "'disk_image' is a raw hard disk image for IDE hard disk 0\n\n",
2018 error_get_progname());
2019
Michael Ellerman77bd1112011-12-19 17:19:30 +11002020#define QEMU_OPTIONS_GENERATE_HELP
2021#include "qemu-options-wrapper.h"
Michael Ellermana3adb7a2011-12-19 17:19:31 +11002022
2023 printf("\nDuring emulation, the following keys are useful:\n"
malc3f020d72010-02-08 12:04:56 +03002024 "ctrl-alt-f toggle full screen\n"
2025 "ctrl-alt-n switch to virtual console 'n'\n"
2026 "ctrl-alt toggle mouse and keyboard grab\n"
2027 "\n"
Michael Ellermana3adb7a2011-12-19 17:19:31 +11002028 "When using -nographic, press 'ctrl-a h' to get some help.\n");
2029
ths15f82202007-06-29 23:26:08 +00002030 exit(exitcode);
bellard0824d6f2003-06-24 13:42:40 +00002031}
2032
bellardcd6f1162004-05-13 22:02:20 +00002033#define HAS_ARG 0x0001
2034
bellardcd6f1162004-05-13 22:02:20 +00002035typedef struct QEMUOption {
2036 const char *name;
2037 int flags;
2038 int index;
Blue Swirlad960902010-03-29 19:23:52 +00002039 uint32_t arch_mask;
bellardcd6f1162004-05-13 22:02:20 +00002040} QEMUOption;
2041
blueswir1dbed7e42008-10-01 19:38:09 +00002042static const QEMUOption qemu_options[] = {
Blue Swirlad960902010-03-29 19:23:52 +00002043 { "h", 0, QEMU_OPTION_h, QEMU_ARCH_ALL },
Michael Ellerman77bd1112011-12-19 17:19:30 +11002044#define QEMU_OPTIONS_GENERATE_OPTIONS
2045#include "qemu-options-wrapper.h"
bellardcd6f1162004-05-13 22:02:20 +00002046 { NULL },
bellardfc01f7e2003-06-30 10:03:06 +00002047};
Blue Swirla369da52011-09-27 19:15:42 +00002048
2049static bool vga_available(void)
2050{
Aurelien Jarno36b7f272012-09-08 16:01:20 +02002051 return object_class_by_name("VGA") || object_class_by_name("isa-vga");
Blue Swirla369da52011-09-27 19:15:42 +00002052}
2053
2054static bool cirrus_vga_available(void)
2055{
Aurelien Jarno36b7f272012-09-08 16:01:20 +02002056 return object_class_by_name("cirrus-vga")
2057 || object_class_by_name("isa-cirrus-vga");
Blue Swirla369da52011-09-27 19:15:42 +00002058}
2059
2060static bool vmware_vga_available(void)
2061{
Aurelien Jarno36b7f272012-09-08 16:01:20 +02002062 return object_class_by_name("vmware-svga");
Blue Swirla369da52011-09-27 19:15:42 +00002063}
2064
Aurelien Jarno879049a2012-09-08 12:01:06 +02002065static bool qxl_vga_available(void)
2066{
2067 return object_class_by_name("qxl-vga");
2068}
2069
malc3893c122008-09-28 00:42:05 +00002070static void select_vgahw (const char *p)
2071{
2072 const char *opts;
2073
Zachary Amsden86176752009-07-30 00:15:02 -10002074 vga_interface_type = VGA_NONE;
malc3893c122008-09-28 00:42:05 +00002075 if (strstart(p, "std", &opts)) {
Blue Swirla369da52011-09-27 19:15:42 +00002076 if (vga_available()) {
2077 vga_interface_type = VGA_STD;
2078 } else {
2079 fprintf(stderr, "Error: standard VGA not available\n");
2080 exit(0);
2081 }
malc3893c122008-09-28 00:42:05 +00002082 } else if (strstart(p, "cirrus", &opts)) {
Blue Swirla369da52011-09-27 19:15:42 +00002083 if (cirrus_vga_available()) {
2084 vga_interface_type = VGA_CIRRUS;
2085 } else {
2086 fprintf(stderr, "Error: Cirrus VGA not available\n");
2087 exit(0);
2088 }
malc3893c122008-09-28 00:42:05 +00002089 } else if (strstart(p, "vmware", &opts)) {
Blue Swirla369da52011-09-27 19:15:42 +00002090 if (vmware_vga_available()) {
2091 vga_interface_type = VGA_VMWARE;
2092 } else {
2093 fprintf(stderr, "Error: VMWare SVGA not available\n");
2094 exit(0);
2095 }
aliguori94909d92009-04-22 15:19:53 +00002096 } else if (strstart(p, "xenfb", &opts)) {
Zachary Amsden86176752009-07-30 00:15:02 -10002097 vga_interface_type = VGA_XENFB;
Gerd Hoffmanna19cbfb2010-04-27 11:50:11 +02002098 } else if (strstart(p, "qxl", &opts)) {
Aurelien Jarno879049a2012-09-08 12:01:06 +02002099 if (qxl_vga_available()) {
2100 vga_interface_type = VGA_QXL;
2101 } else {
2102 fprintf(stderr, "Error: QXL VGA not available\n");
2103 exit(0);
2104 }
aliguori28b85ed2009-04-22 15:19:48 +00002105 } else if (!strstart(p, "none", &opts)) {
malc3893c122008-09-28 00:42:05 +00002106 invalid_vga:
2107 fprintf(stderr, "Unknown vga type: %s\n", p);
2108 exit(1);
2109 }
malccb5a7aa2008-09-28 00:42:12 +00002110 while (*opts) {
2111 const char *nextopt;
2112
2113 if (strstart(opts, ",retrace=", &nextopt)) {
2114 opts = nextopt;
2115 if (strstart(opts, "dumb", &nextopt))
2116 vga_retrace_method = VGA_RETRACE_DUMB;
2117 else if (strstart(opts, "precise", &nextopt))
2118 vga_retrace_method = VGA_RETRACE_PRECISE;
2119 else goto invalid_vga;
2120 } else goto invalid_vga;
2121 opts = nextopt;
2122 }
malc3893c122008-09-28 00:42:05 +00002123}
2124
Jes Sorensen1472a952011-03-16 13:33:31 +01002125static DisplayType select_display(const char *p)
2126{
2127 const char *opts;
2128 DisplayType display = DT_DEFAULT;
2129
2130 if (strstart(p, "sdl", &opts)) {
2131#ifdef CONFIG_SDL
2132 display = DT_SDL;
2133 while (*opts) {
2134 const char *nextopt;
2135
2136 if (strstart(opts, ",frame=", &nextopt)) {
2137 opts = nextopt;
2138 if (strstart(opts, "on", &nextopt)) {
2139 no_frame = 0;
2140 } else if (strstart(opts, "off", &nextopt)) {
2141 no_frame = 1;
2142 } else {
Peter Maydell05175532011-03-23 03:40:57 +00002143 goto invalid_sdl_args;
Jes Sorensen1472a952011-03-16 13:33:31 +01002144 }
2145 } else if (strstart(opts, ",alt_grab=", &nextopt)) {
2146 opts = nextopt;
2147 if (strstart(opts, "on", &nextopt)) {
2148 alt_grab = 1;
2149 } else if (strstart(opts, "off", &nextopt)) {
2150 alt_grab = 0;
2151 } else {
Peter Maydell05175532011-03-23 03:40:57 +00002152 goto invalid_sdl_args;
Jes Sorensen1472a952011-03-16 13:33:31 +01002153 }
2154 } else if (strstart(opts, ",ctrl_grab=", &nextopt)) {
2155 opts = nextopt;
2156 if (strstart(opts, "on", &nextopt)) {
2157 ctrl_grab = 1;
2158 } else if (strstart(opts, "off", &nextopt)) {
2159 ctrl_grab = 0;
2160 } else {
Peter Maydell05175532011-03-23 03:40:57 +00002161 goto invalid_sdl_args;
Jes Sorensen1472a952011-03-16 13:33:31 +01002162 }
2163 } else if (strstart(opts, ",window_close=", &nextopt)) {
2164 opts = nextopt;
2165 if (strstart(opts, "on", &nextopt)) {
2166 no_quit = 0;
2167 } else if (strstart(opts, "off", &nextopt)) {
2168 no_quit = 1;
2169 } else {
Peter Maydell05175532011-03-23 03:40:57 +00002170 goto invalid_sdl_args;
Jes Sorensen1472a952011-03-16 13:33:31 +01002171 }
2172 } else {
Peter Maydell05175532011-03-23 03:40:57 +00002173 invalid_sdl_args:
2174 fprintf(stderr, "Invalid SDL option string: %s\n", p);
2175 exit(1);
Jes Sorensen1472a952011-03-16 13:33:31 +01002176 }
2177 opts = nextopt;
2178 }
2179#else
2180 fprintf(stderr, "SDL support is disabled\n");
2181 exit(1);
2182#endif
Jes Sorensen3264ff12011-03-16 13:33:33 +01002183 } else if (strstart(p, "vnc", &opts)) {
Jes Sorensen821601e2011-03-16 13:33:36 +01002184#ifdef CONFIG_VNC
Jes Sorensen3264ff12011-03-16 13:33:33 +01002185 display_remote++;
2186
2187 if (*opts) {
2188 const char *nextopt;
2189
2190 if (strstart(opts, "=", &nextopt)) {
2191 vnc_display = nextopt;
2192 }
2193 }
2194 if (!vnc_display) {
2195 fprintf(stderr, "VNC requires a display argument vnc=<display>\n");
2196 exit(1);
2197 }
Jes Sorensen821601e2011-03-16 13:33:36 +01002198#else
2199 fprintf(stderr, "VNC support is disabled\n");
2200 exit(1);
2201#endif
Jes Sorensen1472a952011-03-16 13:33:31 +01002202 } else if (strstart(p, "curses", &opts)) {
2203#ifdef CONFIG_CURSES
2204 display = DT_CURSES;
2205#else
2206 fprintf(stderr, "Curses support is disabled\n");
2207 exit(1);
2208#endif
Jes Sorensen4171d322011-03-16 13:33:32 +01002209 } else if (strstart(p, "none", &opts)) {
2210 display = DT_NONE;
Jes Sorensen1472a952011-03-16 13:33:31 +01002211 } else {
Jes Sorensen1472a952011-03-16 13:33:31 +01002212 fprintf(stderr, "Unknown display type: %s\n", p);
2213 exit(1);
2214 }
2215
2216 return display;
2217}
2218
Markus Armbruster7d4c3d52009-06-26 19:15:14 +02002219static int balloon_parse(const char *arg)
2220{
Gerd Hoffmann382f0742009-08-14 10:34:22 +02002221 QemuOpts *opts;
Markus Armbruster7d4c3d52009-06-26 19:15:14 +02002222
Gerd Hoffmann382f0742009-08-14 10:34:22 +02002223 if (strcmp(arg, "none") == 0) {
2224 return 0;
Markus Armbruster7d4c3d52009-06-26 19:15:14 +02002225 }
Gerd Hoffmann382f0742009-08-14 10:34:22 +02002226
2227 if (!strncmp(arg, "virtio", 6)) {
2228 if (arg[6] == ',') {
2229 /* have params -> parse them */
Gerd Hoffmann3329f072010-08-20 13:52:01 +02002230 opts = qemu_opts_parse(qemu_find_opts("device"), arg+7, 0);
Gerd Hoffmann382f0742009-08-14 10:34:22 +02002231 if (!opts)
2232 return -1;
2233 } else {
2234 /* create empty opts */
Dong Xu Wange478b442012-12-06 14:47:22 +08002235 opts = qemu_opts_create_nofail(qemu_find_opts("device"));
Gerd Hoffmann382f0742009-08-14 10:34:22 +02002236 }
Alexander Graf29f82b32011-03-29 15:29:29 +02002237 qemu_opt_set(opts, "driver", "virtio-balloon");
Gerd Hoffmann382f0742009-08-14 10:34:22 +02002238 return 0;
2239 }
2240
2241 return -1;
Markus Armbruster7d4c3d52009-06-26 19:15:14 +02002242}
Markus Armbruster7d4c3d52009-06-26 19:15:14 +02002243
Paul Brook5cea8592009-05-30 00:52:44 +01002244char *qemu_find_file(int type, const char *name)
2245{
2246 int len;
2247 const char *subdir;
2248 char *buf;
2249
Peter Maydell31783202012-05-25 13:07:01 +01002250 /* Try the name as a straight path first */
2251 if (access(name, R_OK) == 0) {
Anthony Liguori7267c092011-08-20 22:09:37 -05002252 return g_strdup(name);
Paul Brook5cea8592009-05-30 00:52:44 +01002253 }
2254 switch (type) {
2255 case QEMU_FILE_TYPE_BIOS:
2256 subdir = "";
2257 break;
2258 case QEMU_FILE_TYPE_KEYMAP:
2259 subdir = "keymaps/";
2260 break;
2261 default:
2262 abort();
2263 }
2264 len = strlen(data_dir) + strlen(name) + strlen(subdir) + 2;
Anthony Liguori7267c092011-08-20 22:09:37 -05002265 buf = g_malloc0(len);
Blue Swirl3a417592009-06-09 19:12:21 +00002266 snprintf(buf, len, "%s/%s%s", data_dir, subdir, name);
Paul Brook5cea8592009-05-30 00:52:44 +01002267 if (access(buf, R_OK)) {
Anthony Liguori7267c092011-08-20 22:09:37 -05002268 g_free(buf);
Paul Brook5cea8592009-05-30 00:52:44 +01002269 return NULL;
2270 }
2271 return buf;
2272}
2273
Markus Armbrusterff952ba2010-01-29 19:48:57 +01002274static int device_help_func(QemuOpts *opts, void *opaque)
2275{
2276 return qdev_device_help(opts);
2277}
2278
Gerd Hoffmannf31d07d2009-07-31 12:25:37 +02002279static int device_init_func(QemuOpts *opts, void *opaque)
2280{
2281 DeviceState *dev;
2282
2283 dev = qdev_device_add(opts);
2284 if (!dev)
2285 return -1;
Paolo Bonzinib09995a2013-01-25 14:12:37 +01002286 object_unref(OBJECT(dev));
Gerd Hoffmannf31d07d2009-07-31 12:25:37 +02002287 return 0;
2288}
2289
Gerd Hoffmann1a688d32009-12-08 13:11:36 +01002290static int chardev_init_func(QemuOpts *opts, void *opaque)
2291{
Gerd Hoffmannbd2d80b2012-10-15 09:28:05 +02002292 Error *local_err = NULL;
Gerd Hoffmann1a688d32009-12-08 13:11:36 +01002293
Gerd Hoffmannbd2d80b2012-10-15 09:28:05 +02002294 qemu_chr_new_from_opts(opts, NULL, &local_err);
2295 if (error_is_set(&local_err)) {
2296 fprintf(stderr, "%s\n", error_get_pretty(local_err));
2297 error_free(local_err);
Gerd Hoffmann1a688d32009-12-08 13:11:36 +01002298 return -1;
Gerd Hoffmannbd2d80b2012-10-15 09:28:05 +02002299 }
Gerd Hoffmann1a688d32009-12-08 13:11:36 +01002300 return 0;
2301}
2302
Venkateswararao Jujjuri (JV)758e8e32010-06-14 13:34:41 -07002303#ifdef CONFIG_VIRTFS
Gautham R Shenoy74db9202010-04-29 17:44:43 +05302304static int fsdev_init_func(QemuOpts *opts, void *opaque)
2305{
2306 int ret;
2307 ret = qemu_fsdev_add(opts);
2308
2309 return ret;
2310}
2311#endif
2312
Gerd Hoffmann88589342009-12-08 13:11:50 +01002313static int mon_init_func(QemuOpts *opts, void *opaque)
2314{
2315 CharDriverState *chr;
2316 const char *chardev;
2317 const char *mode;
2318 int flags;
2319
2320 mode = qemu_opt_get(opts, "mode");
2321 if (mode == NULL) {
2322 mode = "readline";
2323 }
2324 if (strcmp(mode, "readline") == 0) {
2325 flags = MONITOR_USE_READLINE;
2326 } else if (strcmp(mode, "control") == 0) {
2327 flags = MONITOR_USE_CONTROL;
2328 } else {
2329 fprintf(stderr, "unknown monitor mode \"%s\"\n", mode);
2330 exit(1);
2331 }
2332
Daniel P. Berrange39eaab92010-06-07 15:42:31 +01002333 if (qemu_opt_get_bool(opts, "pretty", 0))
2334 flags |= MONITOR_USE_PRETTY;
2335
Gerd Hoffmann88589342009-12-08 13:11:50 +01002336 if (qemu_opt_get_bool(opts, "default", 0))
2337 flags |= MONITOR_IS_DEFAULT;
2338
2339 chardev = qemu_opt_get(opts, "chardev");
2340 chr = qemu_chr_find(chardev);
2341 if (chr == NULL) {
2342 fprintf(stderr, "chardev \"%s\" not found\n", chardev);
2343 exit(1);
2344 }
2345
2346 monitor_init(chr, flags);
2347 return 0;
2348}
2349
Gerd Hoffmann6ca55822009-12-08 13:11:52 +01002350static void monitor_parse(const char *optarg, const char *mode)
Gerd Hoffmann88589342009-12-08 13:11:50 +01002351{
2352 static int monitor_device_index = 0;
2353 QemuOpts *opts;
2354 const char *p;
2355 char label[32];
2356 int def = 0;
2357
2358 if (strstart(optarg, "chardev:", &p)) {
2359 snprintf(label, sizeof(label), "%s", p);
2360 } else {
Jan Kiszka140e0652010-04-06 16:55:52 +02002361 snprintf(label, sizeof(label), "compat_monitor%d",
2362 monitor_device_index);
2363 if (monitor_device_index == 0) {
Gerd Hoffmann88589342009-12-08 13:11:50 +01002364 def = 1;
2365 }
2366 opts = qemu_chr_parse_compat(label, optarg);
2367 if (!opts) {
2368 fprintf(stderr, "parse error: %s\n", optarg);
2369 exit(1);
2370 }
2371 }
2372
Luiz Capitulino8be7e7e2012-03-20 15:51:57 -03002373 opts = qemu_opts_create(qemu_find_opts("mon"), label, 1, NULL);
Gerd Hoffmann88589342009-12-08 13:11:50 +01002374 if (!opts) {
2375 fprintf(stderr, "duplicate chardev: %s\n", label);
2376 exit(1);
2377 }
Gerd Hoffmann6ca55822009-12-08 13:11:52 +01002378 qemu_opt_set(opts, "mode", mode);
Gerd Hoffmann88589342009-12-08 13:11:50 +01002379 qemu_opt_set(opts, "chardev", label);
2380 if (def)
2381 qemu_opt_set(opts, "default", "on");
2382 monitor_device_index++;
2383}
2384
Gerd Hoffmannbd3c9482009-07-15 13:59:26 +02002385struct device_config {
2386 enum {
Gerd Hoffmannaee1b932009-12-08 13:11:53 +01002387 DEV_USB, /* -usbdevice */
2388 DEV_BT, /* -bt */
2389 DEV_SERIAL, /* -serial */
2390 DEV_PARALLEL, /* -parallel */
2391 DEV_VIRTCON, /* -virtioconsole */
H. Peter Anvinc9f398e2009-12-29 13:51:36 -08002392 DEV_DEBUGCON, /* -debugcon */
Markus Armbrusteref0c4a02012-02-07 15:09:13 +01002393 DEV_GDB, /* -gdb, -s */
Alexander Graf3ef669e2013-01-24 12:18:52 +01002394 DEV_SCLP, /* s390 sclp */
Gerd Hoffmannbd3c9482009-07-15 13:59:26 +02002395 } type;
2396 const char *cmdline;
Markus Armbrusterd9a59542012-02-07 15:09:12 +01002397 Location loc;
Blue Swirl72cf2d42009-09-12 07:36:22 +00002398 QTAILQ_ENTRY(device_config) next;
Gerd Hoffmannbd3c9482009-07-15 13:59:26 +02002399};
Blue Swirl4fdcac02012-10-28 11:04:47 +00002400
2401static QTAILQ_HEAD(, device_config) device_configs =
2402 QTAILQ_HEAD_INITIALIZER(device_configs);
Gerd Hoffmannbd3c9482009-07-15 13:59:26 +02002403
2404static void add_device_config(int type, const char *cmdline)
2405{
2406 struct device_config *conf;
2407
Anthony Liguori7267c092011-08-20 22:09:37 -05002408 conf = g_malloc0(sizeof(*conf));
Gerd Hoffmannbd3c9482009-07-15 13:59:26 +02002409 conf->type = type;
2410 conf->cmdline = cmdline;
Markus Armbrusterd9a59542012-02-07 15:09:12 +01002411 loc_save(&conf->loc);
Blue Swirl72cf2d42009-09-12 07:36:22 +00002412 QTAILQ_INSERT_TAIL(&device_configs, conf, next);
Gerd Hoffmannbd3c9482009-07-15 13:59:26 +02002413}
2414
2415static int foreach_device_config(int type, int (*func)(const char *cmdline))
2416{
2417 struct device_config *conf;
2418 int rc;
2419
Blue Swirl72cf2d42009-09-12 07:36:22 +00002420 QTAILQ_FOREACH(conf, &device_configs, next) {
Gerd Hoffmannbd3c9482009-07-15 13:59:26 +02002421 if (conf->type != type)
2422 continue;
Markus Armbrusterd9a59542012-02-07 15:09:12 +01002423 loc_push_restore(&conf->loc);
Gerd Hoffmannbd3c9482009-07-15 13:59:26 +02002424 rc = func(conf->cmdline);
Markus Armbrusterd9a59542012-02-07 15:09:12 +01002425 loc_pop(&conf->loc);
Gerd Hoffmannbd3c9482009-07-15 13:59:26 +02002426 if (0 != rc)
2427 return rc;
2428 }
2429 return 0;
2430}
2431
Gerd Hoffmann998bbd72009-12-08 13:11:41 +01002432static int serial_parse(const char *devname)
2433{
2434 static int index = 0;
2435 char label[32];
2436
2437 if (strcmp(devname, "none") == 0)
2438 return 0;
2439 if (index == MAX_SERIAL_PORTS) {
2440 fprintf(stderr, "qemu: too many serial ports\n");
2441 exit(1);
2442 }
2443 snprintf(label, sizeof(label), "serial%d", index);
Anthony Liguori27143a42011-08-15 11:17:36 -05002444 serial_hds[index] = qemu_chr_new(label, devname, NULL);
Gerd Hoffmann998bbd72009-12-08 13:11:41 +01002445 if (!serial_hds[index]) {
Peter Maydell52d06132012-07-09 04:28:30 +00002446 fprintf(stderr, "qemu: could not connect serial device"
2447 " to character backend '%s'\n", devname);
Gerd Hoffmann998bbd72009-12-08 13:11:41 +01002448 return -1;
2449 }
2450 index++;
2451 return 0;
2452}
2453
Gerd Hoffmann6a5e8b02009-12-08 13:11:42 +01002454static int parallel_parse(const char *devname)
2455{
2456 static int index = 0;
2457 char label[32];
2458
2459 if (strcmp(devname, "none") == 0)
2460 return 0;
2461 if (index == MAX_PARALLEL_PORTS) {
2462 fprintf(stderr, "qemu: too many parallel ports\n");
2463 exit(1);
2464 }
2465 snprintf(label, sizeof(label), "parallel%d", index);
Anthony Liguori27143a42011-08-15 11:17:36 -05002466 parallel_hds[index] = qemu_chr_new(label, devname, NULL);
Gerd Hoffmann6a5e8b02009-12-08 13:11:42 +01002467 if (!parallel_hds[index]) {
Peter Maydell52d06132012-07-09 04:28:30 +00002468 fprintf(stderr, "qemu: could not connect parallel device"
2469 " to character backend '%s'\n", devname);
Gerd Hoffmann6a5e8b02009-12-08 13:11:42 +01002470 return -1;
2471 }
2472 index++;
2473 return 0;
2474}
2475
Gerd Hoffmannaee1b932009-12-08 13:11:53 +01002476static int virtcon_parse(const char *devname)
2477{
Gerd Hoffmann3329f072010-08-20 13:52:01 +02002478 QemuOptsList *device = qemu_find_opts("device");
Gerd Hoffmannaee1b932009-12-08 13:11:53 +01002479 static int index = 0;
2480 char label[32];
Amit Shah392ecf52010-01-21 16:19:23 +05302481 QemuOpts *bus_opts, *dev_opts;
Gerd Hoffmannaee1b932009-12-08 13:11:53 +01002482
2483 if (strcmp(devname, "none") == 0)
2484 return 0;
2485 if (index == MAX_VIRTIO_CONSOLES) {
2486 fprintf(stderr, "qemu: too many virtio consoles\n");
2487 exit(1);
2488 }
Amit Shah392ecf52010-01-21 16:19:23 +05302489
Dong Xu Wange478b442012-12-06 14:47:22 +08002490 bus_opts = qemu_opts_create_nofail(device);
Anthony Liguorie87f7fc2012-02-06 11:07:18 -06002491 if (arch_type == QEMU_ARCH_S390X) {
2492 qemu_opt_set(bus_opts, "driver", "virtio-serial-s390");
2493 } else {
2494 qemu_opt_set(bus_opts, "driver", "virtio-serial-pci");
2495 }
Amit Shah392ecf52010-01-21 16:19:23 +05302496
Dong Xu Wange478b442012-12-06 14:47:22 +08002497 dev_opts = qemu_opts_create_nofail(device);
Amit Shah392ecf52010-01-21 16:19:23 +05302498 qemu_opt_set(dev_opts, "driver", "virtconsole");
2499
Gerd Hoffmannaee1b932009-12-08 13:11:53 +01002500 snprintf(label, sizeof(label), "virtcon%d", index);
Anthony Liguori27143a42011-08-15 11:17:36 -05002501 virtcon_hds[index] = qemu_chr_new(label, devname, NULL);
Gerd Hoffmannaee1b932009-12-08 13:11:53 +01002502 if (!virtcon_hds[index]) {
Peter Maydell52d06132012-07-09 04:28:30 +00002503 fprintf(stderr, "qemu: could not connect virtio console"
2504 " to character backend '%s'\n", devname);
Gerd Hoffmannaee1b932009-12-08 13:11:53 +01002505 return -1;
2506 }
Amit Shah392ecf52010-01-21 16:19:23 +05302507 qemu_opt_set(dev_opts, "chardev", label);
2508
Gerd Hoffmannaee1b932009-12-08 13:11:53 +01002509 index++;
2510 return 0;
2511}
2512
Alexander Graf3ef669e2013-01-24 12:18:52 +01002513static int sclp_parse(const char *devname)
2514{
2515 QemuOptsList *device = qemu_find_opts("device");
2516 static int index = 0;
2517 char label[32];
2518 QemuOpts *dev_opts;
2519
2520 if (strcmp(devname, "none") == 0) {
2521 return 0;
2522 }
2523 if (index == MAX_SCLP_CONSOLES) {
2524 fprintf(stderr, "qemu: too many sclp consoles\n");
2525 exit(1);
2526 }
2527
2528 assert(arch_type == QEMU_ARCH_S390X);
2529
2530 dev_opts = qemu_opts_create(device, NULL, 0, NULL);
2531 qemu_opt_set(dev_opts, "driver", "sclpconsole");
2532
2533 snprintf(label, sizeof(label), "sclpcon%d", index);
2534 sclp_hds[index] = qemu_chr_new(label, devname, NULL);
2535 if (!sclp_hds[index]) {
2536 fprintf(stderr, "qemu: could not connect sclp console"
2537 " to character backend '%s'\n", devname);
2538 return -1;
2539 }
2540 qemu_opt_set(dev_opts, "chardev", label);
2541
2542 index++;
2543 return 0;
2544}
2545
H. Peter Anvinc9f398e2009-12-29 13:51:36 -08002546static int debugcon_parse(const char *devname)
2547{
2548 QemuOpts *opts;
2549
Anthony Liguori27143a42011-08-15 11:17:36 -05002550 if (!qemu_chr_new("debugcon", devname, NULL)) {
H. Peter Anvinc9f398e2009-12-29 13:51:36 -08002551 exit(1);
2552 }
Luiz Capitulino8be7e7e2012-03-20 15:51:57 -03002553 opts = qemu_opts_create(qemu_find_opts("device"), "debugcon", 1, NULL);
H. Peter Anvinc9f398e2009-12-29 13:51:36 -08002554 if (!opts) {
2555 fprintf(stderr, "qemu: already have a debugcon device\n");
2556 exit(1);
2557 }
2558 qemu_opt_set(opts, "driver", "isa-debugcon");
2559 qemu_opt_set(opts, "chardev", "debugcon");
2560 return 0;
2561}
2562
Jan Kiszka9052ea62011-07-23 12:38:37 +02002563static QEMUMachine *machine_parse(const char *name)
2564{
2565 QEMUMachine *m, *machine = NULL;
2566
2567 if (name) {
2568 machine = find_machine(name);
2569 }
2570 if (machine) {
2571 return machine;
2572 }
2573 printf("Supported machines are:\n");
2574 for (m = first_machine; m != NULL; m = m->next) {
2575 if (m->alias) {
Peter Maydell3b264862012-02-22 22:13:11 +00002576 printf("%-20s %s (alias of %s)\n", m->alias, m->desc, m->name);
Jan Kiszka9052ea62011-07-23 12:38:37 +02002577 }
Peter Maydell3b264862012-02-22 22:13:11 +00002578 printf("%-20s %s%s\n", m->name, m->desc,
Jan Kiszka9052ea62011-07-23 12:38:37 +02002579 m->is_default ? " (default)" : "");
2580 }
Peter Maydellc8057f92012-08-02 13:45:54 +01002581 exit(!name || !is_help_option(name));
Jan Kiszka9052ea62011-07-23 12:38:37 +02002582}
2583
Anthony PERARD303d4e82010-09-21 20:05:31 +01002584static int tcg_init(void)
2585{
Jan Kiszkad5ab9712011-08-02 16:10:21 +02002586 tcg_exec_init(tcg_tb_size * 1024 * 1024);
Anthony PERARD303d4e82010-09-21 20:05:31 +01002587 return 0;
2588}
2589
2590static struct {
2591 const char *opt_name;
2592 const char *name;
2593 int (*available)(void);
2594 int (*init)(void);
liguangd5286af2013-01-24 13:03:27 +08002595 bool *allowed;
Anthony PERARD303d4e82010-09-21 20:05:31 +01002596} accel_list[] = {
2597 { "tcg", "tcg", tcg_available, tcg_init, &tcg_allowed },
Anthony PERARD3285cf42010-08-19 12:27:56 +01002598 { "xen", "Xen", xen_available, xen_init, &xen_allowed },
Anthony PERARD303d4e82010-09-21 20:05:31 +01002599 { "kvm", "KVM", kvm_available, kvm_init, &kvm_allowed },
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +02002600 { "qtest", "QTest", qtest_available, qtest_init, &qtest_allowed },
Anthony PERARD303d4e82010-09-21 20:05:31 +01002601};
2602
2603static int configure_accelerator(void)
2604{
2605 const char *p = NULL;
2606 char buf[10];
2607 int i, ret;
liguang217e21b2013-01-24 13:03:26 +08002608 bool accel_initialised = false;
2609 bool init_failed = false;
Anthony PERARD303d4e82010-09-21 20:05:31 +01002610
2611 QemuOptsList *list = qemu_find_opts("machine");
2612 if (!QTAILQ_EMPTY(&list->head)) {
2613 p = qemu_opt_get(QTAILQ_FIRST(&list->head), "accel");
2614 }
2615
2616 if (p == NULL) {
2617 /* Use the default "accelerator", tcg */
2618 p = "tcg";
2619 }
2620
Peter Maydell1b785a92012-02-07 20:57:27 +00002621 while (!accel_initialised && *p != '\0') {
Anthony PERARD303d4e82010-09-21 20:05:31 +01002622 if (*p == ':') {
2623 p++;
2624 }
2625 p = get_opt_name(buf, sizeof (buf), p, ':');
2626 for (i = 0; i < ARRAY_SIZE(accel_list); i++) {
2627 if (strcmp(accel_list[i].opt_name, buf) == 0) {
liguange3c66d92013-01-24 13:03:25 +08002628 if (!accel_list[i].available()) {
2629 printf("%s not supported for this target\n",
2630 accel_list[i].name);
2631 continue;
2632 }
liguangd5286af2013-01-24 13:03:27 +08002633 *(accel_list[i].allowed) = true;
Anthony PERARD303d4e82010-09-21 20:05:31 +01002634 ret = accel_list[i].init();
2635 if (ret < 0) {
liguang217e21b2013-01-24 13:03:26 +08002636 init_failed = true;
liguange3c66d92013-01-24 13:03:25 +08002637 fprintf(stderr, "failed to initialize %s: %s\n",
2638 accel_list[i].name,
2639 strerror(-ret));
liguangd5286af2013-01-24 13:03:27 +08002640 *(accel_list[i].allowed) = false;
Anthony PERARD303d4e82010-09-21 20:05:31 +01002641 } else {
liguang217e21b2013-01-24 13:03:26 +08002642 accel_initialised = true;
Anthony PERARD303d4e82010-09-21 20:05:31 +01002643 }
2644 break;
2645 }
2646 }
2647 if (i == ARRAY_SIZE(accel_list)) {
2648 fprintf(stderr, "\"%s\" accelerator does not exist.\n", buf);
2649 }
2650 }
2651
Peter Maydell1b785a92012-02-07 20:57:27 +00002652 if (!accel_initialised) {
liguang217e21b2013-01-24 13:03:26 +08002653 if (!init_failed) {
2654 fprintf(stderr, "No accelerator found!\n");
2655 }
Anthony PERARD303d4e82010-09-21 20:05:31 +01002656 exit(1);
2657 }
2658
2659 if (init_failed) {
2660 fprintf(stderr, "Back to %s accelerator.\n", accel_list[i].name);
2661 }
2662
Peter Maydell1b785a92012-02-07 20:57:27 +00002663 return !accel_initialised;
Anthony PERARD303d4e82010-09-21 20:05:31 +01002664}
2665
Gerd Hoffmannfd42dee2010-06-04 14:08:07 +02002666void qemu_add_exit_notifier(Notifier *notify)
2667{
2668 notifier_list_add(&exit_notifiers, notify);
2669}
2670
2671void qemu_remove_exit_notifier(Notifier *notify)
2672{
Paolo Bonzini31552522012-01-13 17:34:01 +01002673 notifier_remove(notify);
Gerd Hoffmannfd42dee2010-06-04 14:08:07 +02002674}
2675
2676static void qemu_run_exit_notifiers(void)
2677{
Jan Kiszka9e8dd452011-06-20 14:06:26 +02002678 notifier_list_notify(&exit_notifiers, NULL);
Gerd Hoffmannfd42dee2010-06-04 14:08:07 +02002679}
2680
Gleb Natapov4cab9462010-12-08 13:35:08 +02002681void qemu_add_machine_init_done_notifier(Notifier *notify)
2682{
2683 notifier_list_add(&machine_init_done_notifiers, notify);
2684}
2685
2686static void qemu_run_machine_init_done_notifiers(void)
2687{
Jan Kiszka9e8dd452011-06-20 14:06:26 +02002688 notifier_list_notify(&machine_init_done_notifiers, NULL);
Gleb Natapov4cab9462010-12-08 13:35:08 +02002689}
2690
Anthony Liguori6530a972010-01-22 09:18:06 -06002691static const QEMUOption *lookup_opt(int argc, char **argv,
2692 const char **poptarg, int *poptind)
2693{
2694 const QEMUOption *popt;
2695 int optind = *poptind;
2696 char *r = argv[optind];
2697 const char *optarg;
2698
Markus Armbruster0f0bc3f2010-02-18 20:13:51 +01002699 loc_set_cmdline(argv, optind, 1);
Anthony Liguori6530a972010-01-22 09:18:06 -06002700 optind++;
2701 /* Treat --foo the same as -foo. */
2702 if (r[1] == '-')
2703 r++;
2704 popt = qemu_options;
2705 for(;;) {
2706 if (!popt->name) {
Markus Armbruster0f0bc3f2010-02-18 20:13:51 +01002707 error_report("invalid option");
Anthony Liguori6530a972010-01-22 09:18:06 -06002708 exit(1);
2709 }
2710 if (!strcmp(popt->name, r + 1))
2711 break;
2712 popt++;
2713 }
2714 if (popt->flags & HAS_ARG) {
2715 if (optind >= argc) {
Markus Armbruster0f0bc3f2010-02-18 20:13:51 +01002716 error_report("requires an argument");
Anthony Liguori6530a972010-01-22 09:18:06 -06002717 exit(1);
2718 }
2719 optarg = argv[optind++];
Markus Armbruster0f0bc3f2010-02-18 20:13:51 +01002720 loc_set_cmdline(argv, optind - 2, 2);
Anthony Liguori6530a972010-01-22 09:18:06 -06002721 } else {
2722 optarg = NULL;
2723 }
2724
2725 *poptarg = optarg;
2726 *poptind = optind;
2727
2728 return popt;
2729}
2730
Anthony Liguori07501122011-08-20 22:38:31 -05002731static gpointer malloc_and_trace(gsize n_bytes)
2732{
2733 void *ptr = malloc(n_bytes);
Frediano Ziglioa74cd8c2011-08-31 09:25:35 +02002734 trace_g_malloc(n_bytes, ptr);
Anthony Liguori07501122011-08-20 22:38:31 -05002735 return ptr;
2736}
2737
2738static gpointer realloc_and_trace(gpointer mem, gsize n_bytes)
2739{
2740 void *ptr = realloc(mem, n_bytes);
Frediano Ziglioa74cd8c2011-08-31 09:25:35 +02002741 trace_g_realloc(mem, n_bytes, ptr);
Anthony Liguori07501122011-08-20 22:38:31 -05002742 return ptr;
2743}
2744
2745static void free_and_trace(gpointer mem)
2746{
Frediano Ziglioa74cd8c2011-08-31 09:25:35 +02002747 trace_g_free(mem);
Anthony Liguori07501122011-08-20 22:38:31 -05002748 free(mem);
2749}
2750
Anthony Liguori68d98d32012-06-25 14:36:33 -05002751static int object_set_property(const char *name, const char *value, void *opaque)
2752{
2753 Object *obj = OBJECT(opaque);
2754 StringInputVisitor *siv;
2755 Error *local_err = NULL;
2756
2757 if (strcmp(name, "qom-type") == 0 || strcmp(name, "id") == 0) {
2758 return 0;
2759 }
2760
2761 siv = string_input_visitor_new(value);
2762 object_property_set(obj, string_input_get_visitor(siv), name, &local_err);
2763 string_input_visitor_cleanup(siv);
2764
2765 if (local_err) {
2766 qerror_report_err(local_err);
2767 error_free(local_err);
2768 return -1;
2769 }
2770
2771 return 0;
2772}
2773
2774static int object_create(QemuOpts *opts, void *opaque)
2775{
2776 const char *type = qemu_opt_get(opts, "qom-type");
2777 const char *id = qemu_opts_id(opts);
2778 Object *obj;
2779
2780 g_assert(type != NULL);
2781
2782 if (id == NULL) {
2783 qerror_report(QERR_MISSING_PARAMETER, "id");
2784 return -1;
2785 }
2786
2787 obj = object_new(type);
2788 if (qemu_opt_foreach(opts, object_set_property, obj, 1) < 0) {
2789 return -1;
2790 }
2791
2792 object_property_add_child(container_get(object_get_root(), "/objects"),
2793 id, obj, NULL);
2794
2795 return 0;
2796}
2797
malc902b3d52008-12-10 19:18:40 +00002798int main(int argc, char **argv, char **envp)
bellard0824d6f2003-06-24 13:42:40 +00002799{
thse4bcb142007-12-02 04:51:10 +00002800 int i;
Eduardo Habkostda1fcfd2010-04-06 19:22:07 -03002801 int snapshot, linux_boot;
Paolo Bonzini4e3de9e2010-03-10 11:38:48 +01002802 const char *icount_option = NULL;
bellard7f7f9872003-10-30 01:11:23 +00002803 const char *initrd_filename;
bellarda20dd502003-09-30 21:07:02 +00002804 const char *kernel_filename, *kernel_cmdline;
Avik Sile4ada292013-01-08 12:36:30 +05302805 char boot_devices[33] = "";
aliguori3023f3322009-01-16 19:04:14 +00002806 DisplayState *ds;
malc9f227bc2012-08-27 18:33:22 +04002807 int cyls, heads, secs, translation;
2808 QemuOpts *hda_opts = NULL, *opts, *machine_opts;
Gerd Hoffmann03b0ba72010-08-20 13:52:02 +02002809 QemuOptsList *olist;
bellardcd6f1162004-05-13 22:02:20 +00002810 int optind;
Anthony Liguori6530a972010-01-22 09:18:06 -06002811 const char *optarg;
bellardd63d3072004-10-03 13:29:03 +00002812 const char *loadvm = NULL;
bellardcc1daa42005-06-05 14:49:17 +00002813 QEMUMachine *machine;
j_mayer94fc95c2007-03-05 19:44:02 +00002814 const char *cpu_model;
Paolo Bonzini7f1b17f2012-05-10 09:39:17 +02002815 const char *vga_model = "none";
ths93815bc2007-03-19 15:58:31 +00002816 const char *pid_file = NULL;
aliguori5bb79102008-10-13 03:12:02 +00002817 const char *incoming = NULL;
Jes Sorensen821601e2011-03-16 13:33:36 +01002818#ifdef CONFIG_VNC
Anthony Liguori993fbfd2009-05-21 16:54:00 -05002819 int show_vnc_port = 0;
Jes Sorensen821601e2011-03-16 13:33:36 +01002820#endif
Eduardo Habkost3ed2d9e2012-05-02 13:07:28 -03002821 bool defconfig = true;
Eduardo Habkostf29a5612012-05-02 13:07:29 -03002822 bool userconfig = true;
Matthew Fernandezc235d732011-06-07 16:32:40 +00002823 const char *log_mask = NULL;
2824 const char *log_file = NULL;
Anthony Liguori07501122011-08-20 22:38:31 -05002825 GMemVTable mem_trace = {
2826 .malloc = malloc_and_trace,
2827 .realloc = realloc_and_trace,
2828 .free = free_and_trace,
2829 };
Lluís23d15e82011-08-31 20:31:31 +02002830 const char *trace_events = NULL;
Lluíse4858972011-08-31 20:31:03 +02002831 const char *trace_file = NULL;
Stefan Hajnoczi0b5538c2011-02-26 18:38:39 +00002832
Gerd Hoffmannfd42dee2010-06-04 14:08:07 +02002833 atexit(qemu_run_exit_notifiers);
Markus Armbruster65abca02010-02-24 14:37:14 +01002834 error_set_progname(argv[0]);
2835
Anthony Liguori07501122011-08-20 22:38:31 -05002836 g_mem_set_vtable(&mem_trace);
Andreas Färberdb529aa2011-10-31 19:14:56 +01002837 if (!g_thread_supported()) {
Alon Levy42ed3722011-12-20 13:41:04 +02002838#if !GLIB_CHECK_VERSION(2, 31, 0)
Andreas Färberdb529aa2011-10-31 19:14:56 +01002839 g_thread_init(NULL);
Alon Levy42ed3722011-12-20 13:41:04 +02002840#else
2841 fprintf(stderr, "glib threading failed to initialize.\n");
2842 exit(1);
2843#endif
Andreas Färberdb529aa2011-10-31 19:14:56 +01002844 }
Anthony Liguori07501122011-08-20 22:38:31 -05002845
Andreas Färber1b71f7c2012-03-04 21:32:35 +01002846 module_call_init(MODULE_INIT_QOM);
2847
Paolo Bonzini4d454572012-11-26 16:03:42 +01002848 qemu_add_opts(&qemu_drive_opts);
2849 qemu_add_opts(&qemu_chardev_opts);
2850 qemu_add_opts(&qemu_device_opts);
2851 qemu_add_opts(&qemu_netdev_opts);
2852 qemu_add_opts(&qemu_net_opts);
2853 qemu_add_opts(&qemu_rtc_opts);
2854 qemu_add_opts(&qemu_global_opts);
2855 qemu_add_opts(&qemu_mon_opts);
2856 qemu_add_opts(&qemu_trace_opts);
2857 qemu_add_opts(&qemu_option_rom_opts);
2858 qemu_add_opts(&qemu_machine_opts);
2859 qemu_add_opts(&qemu_boot_opts);
2860 qemu_add_opts(&qemu_sandbox_opts);
2861 qemu_add_opts(&qemu_add_fd_opts);
2862 qemu_add_opts(&qemu_object_opts);
2863
Luiz Capitulino5db9d4d2011-09-05 16:36:31 -03002864 runstate_init();
2865
Jan Kiszka68752042009-09-15 13:36:04 +02002866 init_clocks();
Paolo Bonzini2ff68d02011-09-12 16:21:44 +02002867 rtc_clock = host_clock;
Jan Kiszka68752042009-09-15 13:36:04 +02002868
malc902b3d52008-12-10 19:18:40 +00002869 qemu_cache_utils_init(envp);
2870
Blue Swirl72cf2d42009-09-12 07:36:22 +00002871 QLIST_INIT (&vm_change_state_head);
Jes Sorensenfe98ac12010-06-10 11:42:21 +02002872 os_setup_early_signal_handling();
bellardbe995c22006-06-25 16:25:21 +00002873
Anthony Liguorif80f9ec2009-05-20 18:38:09 -05002874 module_call_init(MODULE_INIT_MACHINE);
Anthony Liguori0c257432009-05-21 20:41:01 -05002875 machine = find_default_machine();
j_mayer94fc95c2007-03-05 19:44:02 +00002876 cpu_model = NULL;
aurel324fc5d072008-04-27 21:39:40 +00002877 ram_size = 0;
bellard33e39632003-07-06 17:15:21 +00002878 snapshot = 0;
malc9f227bc2012-08-27 18:33:22 +04002879 cyls = heads = secs = 0;
2880 translation = BIOS_ATA_TRANSLATION_AUTO;
bellardc4b1fcc2004-03-14 21:44:30 +00002881
aliguori268a3622009-04-21 22:30:27 +00002882 for (i = 0; i < MAX_NODES; i++) {
2883 node_mem[i] = 0;
Chegu Vinodee785fe2012-07-16 21:31:30 -07002884 node_cpumask[i] = bitmap_new(MAX_CPUMASK_BITS);
aliguori268a3622009-04-21 22:30:27 +00002885 }
2886
aliguori268a3622009-04-21 22:30:27 +00002887 nb_numa_nodes = 0;
bellard7c9d8e02005-11-15 22:16:05 +00002888 nb_nics = 0;
ths3b46e622007-09-17 08:09:54 +00002889
blueswir141bd6392008-10-05 09:56:21 +00002890 autostart= 1;
2891
Anthony Liguori292444c2010-01-21 10:57:58 -06002892 /* first pass of option parsing */
2893 optind = 1;
2894 while (optind < argc) {
2895 if (argv[optind][0] != '-') {
2896 /* disk image */
Anthony Liguori28e68d62010-01-27 10:46:00 -06002897 optind++;
Anthony Liguori292444c2010-01-21 10:57:58 -06002898 continue;
2899 } else {
2900 const QEMUOption *popt;
2901
2902 popt = lookup_opt(argc, argv, &optarg, &optind);
2903 switch (popt->index) {
2904 case QEMU_OPTION_nodefconfig:
Eduardo Habkost3ed2d9e2012-05-02 13:07:28 -03002905 defconfig = false;
Anthony Liguori292444c2010-01-21 10:57:58 -06002906 break;
Eduardo Habkostf29a5612012-05-02 13:07:29 -03002907 case QEMU_OPTION_nouserconfig:
2908 userconfig = false;
2909 break;
Anthony Liguori292444c2010-01-21 10:57:58 -06002910 }
2911 }
2912 }
2913
2914 if (defconfig) {
Kevin Wolfdcfb0932010-03-05 17:25:55 +01002915 int ret;
Eduardo Habkostf29a5612012-05-02 13:07:29 -03002916 ret = qemu_read_default_config_files(userconfig);
Eduardo Habkostb5a8fe52012-05-02 13:07:25 -03002917 if (ret < 0) {
Kevin Wolfdcfb0932010-03-05 17:25:55 +01002918 exit(1);
Anthony Liguori292444c2010-01-21 10:57:58 -06002919 }
2920 }
2921
2922 /* second pass of option parsing */
bellardcd6f1162004-05-13 22:02:20 +00002923 optind = 1;
bellard0824d6f2003-06-24 13:42:40 +00002924 for(;;) {
bellardcd6f1162004-05-13 22:02:20 +00002925 if (optind >= argc)
bellard0824d6f2003-06-24 13:42:40 +00002926 break;
Anthony Liguori6530a972010-01-22 09:18:06 -06002927 if (argv[optind][0] != '-') {
malc9f227bc2012-08-27 18:33:22 +04002928 hda_opts = drive_add(IF_DEFAULT, 0, argv[optind++], HD_OPTS);
bellardcd6f1162004-05-13 22:02:20 +00002929 } else {
2930 const QEMUOption *popt;
2931
Anthony Liguori6530a972010-01-22 09:18:06 -06002932 popt = lookup_opt(argc, argv, &optarg, &optind);
Blue Swirlad960902010-03-29 19:23:52 +00002933 if (!(popt->arch_mask & arch_type)) {
2934 printf("Option %s not supported for this target\n", popt->name);
2935 exit(1);
2936 }
bellardcd6f1162004-05-13 22:02:20 +00002937 switch(popt->index) {
bellardcc1daa42005-06-05 14:49:17 +00002938 case QEMU_OPTION_M:
Jan Kiszka9052ea62011-07-23 12:38:37 +02002939 machine = machine_parse(optarg);
bellardcc1daa42005-06-05 14:49:17 +00002940 break;
Jan Kiszkae43d5942012-10-05 14:51:40 -03002941 case QEMU_OPTION_no_kvm_irqchip: {
2942 olist = qemu_find_opts("machine");
2943 qemu_opts_parse(olist, "kernel_irqchip=off", 0);
2944 break;
2945 }
j_mayer94fc95c2007-03-05 19:44:02 +00002946 case QEMU_OPTION_cpu:
2947 /* hw initialization will check this */
Eduardo Habkostecf40be2012-03-09 16:19:07 -03002948 cpu_model = optarg;
j_mayer94fc95c2007-03-05 19:44:02 +00002949 break;
bellardcd6f1162004-05-13 22:02:20 +00002950 case QEMU_OPTION_hda:
malc9f227bc2012-08-27 18:33:22 +04002951 {
2952 char buf[256];
2953 if (cyls == 0)
2954 snprintf(buf, sizeof(buf), "%s", HD_OPTS);
2955 else
2956 snprintf(buf, sizeof(buf),
2957 "%s,cyls=%d,heads=%d,secs=%d%s",
2958 HD_OPTS , cyls, heads, secs,
2959 translation == BIOS_ATA_TRANSLATION_LBA ?
2960 ",trans=lba" :
2961 translation == BIOS_ATA_TRANSLATION_NONE ?
2962 ",trans=none" : "");
2963 drive_add(IF_DEFAULT, 0, optarg, buf);
2964 break;
2965 }
bellardcd6f1162004-05-13 22:02:20 +00002966 case QEMU_OPTION_hdb:
bellardcc1daa42005-06-05 14:49:17 +00002967 case QEMU_OPTION_hdc:
2968 case QEMU_OPTION_hdd:
Markus Armbruster2292dda2011-01-28 11:21:41 +01002969 drive_add(IF_DEFAULT, popt->index - QEMU_OPTION_hda, optarg,
2970 HD_OPTS);
bellardfc01f7e2003-06-30 10:03:06 +00002971 break;
thse4bcb142007-12-02 04:51:10 +00002972 case QEMU_OPTION_drive:
Michael Tokareve2982c32011-03-30 16:31:05 +04002973 if (drive_def(optarg) == NULL) {
2974 exit(1);
2975 }
thse4bcb142007-12-02 04:51:10 +00002976 break;
Gerd Hoffmannd058fe02009-07-31 12:25:36 +02002977 case QEMU_OPTION_set:
2978 if (qemu_set_option(optarg) != 0)
2979 exit(1);
2980 break;
Gerd Hoffmannd0fef6f2009-12-08 13:11:34 +01002981 case QEMU_OPTION_global:
2982 if (qemu_global_option(optarg) != 0)
2983 exit(1);
2984 break;
balrog3e3d5812007-04-30 02:09:25 +00002985 case QEMU_OPTION_mtdblock:
Markus Armbruster2292dda2011-01-28 11:21:41 +01002986 drive_add(IF_MTD, -1, optarg, MTD_OPTS);
balrog3e3d5812007-04-30 02:09:25 +00002987 break;
pbrooka1bb27b2007-04-06 16:49:48 +00002988 case QEMU_OPTION_sd:
Markus Armbruster2292dda2011-01-28 11:21:41 +01002989 drive_add(IF_SD, 0, optarg, SD_OPTS);
pbrooka1bb27b2007-04-06 16:49:48 +00002990 break;
j_mayer86f55662007-04-24 06:52:59 +00002991 case QEMU_OPTION_pflash:
Markus Armbruster2292dda2011-01-28 11:21:41 +01002992 drive_add(IF_PFLASH, -1, optarg, PFLASH_OPTS);
j_mayer86f55662007-04-24 06:52:59 +00002993 break;
bellardcd6f1162004-05-13 22:02:20 +00002994 case QEMU_OPTION_snapshot:
bellard33e39632003-07-06 17:15:21 +00002995 snapshot = 1;
2996 break;
bellardcd6f1162004-05-13 22:02:20 +00002997 case QEMU_OPTION_hdachs:
bellard330d0412003-07-26 18:11:40 +00002998 {
bellard330d0412003-07-26 18:11:40 +00002999 const char *p;
3000 p = optarg;
3001 cyls = strtol(p, (char **)&p, 0);
bellard46d47672004-11-16 01:45:27 +00003002 if (cyls < 1 || cyls > 16383)
3003 goto chs_fail;
bellard330d0412003-07-26 18:11:40 +00003004 if (*p != ',')
3005 goto chs_fail;
3006 p++;
3007 heads = strtol(p, (char **)&p, 0);
bellard46d47672004-11-16 01:45:27 +00003008 if (heads < 1 || heads > 16)
3009 goto chs_fail;
bellard330d0412003-07-26 18:11:40 +00003010 if (*p != ',')
3011 goto chs_fail;
3012 p++;
3013 secs = strtol(p, (char **)&p, 0);
bellard46d47672004-11-16 01:45:27 +00003014 if (secs < 1 || secs > 63)
3015 goto chs_fail;
3016 if (*p == ',') {
3017 p++;
3018 if (!strcmp(p, "none"))
3019 translation = BIOS_ATA_TRANSLATION_NONE;
3020 else if (!strcmp(p, "lba"))
3021 translation = BIOS_ATA_TRANSLATION_LBA;
3022 else if (!strcmp(p, "auto"))
3023 translation = BIOS_ATA_TRANSLATION_AUTO;
3024 else
3025 goto chs_fail;
3026 } else if (*p != '\0') {
bellardc4b1fcc2004-03-14 21:44:30 +00003027 chs_fail:
bellard46d47672004-11-16 01:45:27 +00003028 fprintf(stderr, "qemu: invalid physical CHS format\n");
3029 exit(1);
bellardc4b1fcc2004-03-14 21:44:30 +00003030 }
malc9f227bc2012-08-27 18:33:22 +04003031 if (hda_opts != NULL) {
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +02003032 char num[16];
3033 snprintf(num, sizeof(num), "%d", cyls);
3034 qemu_opt_set(hda_opts, "cyls", num);
3035 snprintf(num, sizeof(num), "%d", heads);
3036 qemu_opt_set(hda_opts, "heads", num);
3037 snprintf(num, sizeof(num), "%d", secs);
3038 qemu_opt_set(hda_opts, "secs", num);
3039 if (translation == BIOS_ATA_TRANSLATION_LBA)
3040 qemu_opt_set(hda_opts, "trans", "lba");
3041 if (translation == BIOS_ATA_TRANSLATION_NONE)
3042 qemu_opt_set(hda_opts, "trans", "none");
3043 }
bellard330d0412003-07-26 18:11:40 +00003044 }
3045 break;
aliguori268a3622009-04-21 22:30:27 +00003046 case QEMU_OPTION_numa:
aliguori268a3622009-04-21 22:30:27 +00003047 numa_add(optarg);
3048 break;
Jes Sorensen1472a952011-03-16 13:33:31 +01003049 case QEMU_OPTION_display:
3050 display_type = select_display(optarg);
3051 break;
bellardcd6f1162004-05-13 22:02:20 +00003052 case QEMU_OPTION_nographic:
Anthony Liguori993fbfd2009-05-21 16:54:00 -05003053 display_type = DT_NOGRAPHIC;
bellarda20dd502003-09-30 21:07:02 +00003054 break;
balrog4d3b6f62008-02-10 16:33:14 +00003055 case QEMU_OPTION_curses:
Jes Sorensen47b05362011-03-16 13:33:35 +01003056#ifdef CONFIG_CURSES
Anthony Liguori993fbfd2009-05-21 16:54:00 -05003057 display_type = DT_CURSES;
Jes Sorensen47b05362011-03-16 13:33:35 +01003058#else
3059 fprintf(stderr, "Curses support is disabled\n");
3060 exit(1);
balrog4d3b6f62008-02-10 16:33:14 +00003061#endif
Jes Sorensen47b05362011-03-16 13:33:35 +01003062 break;
balroga171fe32007-04-30 01:48:07 +00003063 case QEMU_OPTION_portrait:
Vasily Khoruzhick93128052011-06-17 13:04:36 +03003064 graphic_rotate = 90;
3065 break;
3066 case QEMU_OPTION_rotate:
3067 graphic_rotate = strtol(optarg, (char **) &optarg, 10);
3068 if (graphic_rotate != 0 && graphic_rotate != 90 &&
3069 graphic_rotate != 180 && graphic_rotate != 270) {
3070 fprintf(stderr,
3071 "qemu: only 90, 180, 270 deg rotation is available\n");
3072 exit(1);
3073 }
balroga171fe32007-04-30 01:48:07 +00003074 break;
bellardcd6f1162004-05-13 22:02:20 +00003075 case QEMU_OPTION_kernel:
Peter Maydella0abe472012-02-08 05:41:39 +00003076 qemu_opts_set(qemu_find_opts("machine"), 0, "kernel", optarg);
3077 break;
3078 case QEMU_OPTION_initrd:
3079 qemu_opts_set(qemu_find_opts("machine"), 0, "initrd", optarg);
bellarda20dd502003-09-30 21:07:02 +00003080 break;
bellardcd6f1162004-05-13 22:02:20 +00003081 case QEMU_OPTION_append:
Peter Maydella0abe472012-02-08 05:41:39 +00003082 qemu_opts_set(qemu_find_opts("machine"), 0, "append", optarg);
bellard313aa562003-08-10 21:52:11 +00003083 break;
Grant Likely412beee2012-03-02 11:56:38 +00003084 case QEMU_OPTION_dtb:
3085 qemu_opts_set(qemu_find_opts("machine"), 0, "dtb", optarg);
3086 break;
bellardcd6f1162004-05-13 22:02:20 +00003087 case QEMU_OPTION_cdrom:
Markus Armbruster2292dda2011-01-28 11:21:41 +01003088 drive_add(IF_DEFAULT, 2, optarg, CDROM_OPTS);
bellard36b486b2003-11-11 13:36:08 +00003089 break;
bellardcd6f1162004-05-13 22:02:20 +00003090 case QEMU_OPTION_boot:
j_mayer28c5af52007-11-11 01:50:45 +00003091 {
Jan Kiszkaef3adf62009-07-02 00:19:02 +02003092 static const char * const params[] = {
wayne3d3b8302011-07-27 18:04:55 +08003093 "order", "once", "menu",
Amos Kongac05f342012-09-07 11:11:03 +08003094 "splash", "splash-time",
3095 "reboot-timeout", NULL
Jan Kiszkaef3adf62009-07-02 00:19:02 +02003096 };
3097 char buf[sizeof(boot_devices)];
Jan Kiszkae0f084b2009-07-02 00:19:02 +02003098 char *standard_boot_devices;
Jan Kiszkaef3adf62009-07-02 00:19:02 +02003099 int legacy = 0;
3100
3101 if (!strchr(optarg, '=')) {
3102 legacy = 1;
3103 pstrcpy(buf, sizeof(buf), optarg);
3104 } else if (check_params(buf, sizeof(buf), params, optarg) < 0) {
3105 fprintf(stderr,
3106 "qemu: unknown boot parameter '%s' in '%s'\n",
3107 buf, optarg);
3108 exit(1);
3109 }
3110
3111 if (legacy ||
3112 get_param_value(buf, sizeof(buf), "order", optarg)) {
Eduardo Habkost4e9e9d62010-04-06 19:22:08 -03003113 validate_bootdevices(buf);
Jan Kiszkaef3adf62009-07-02 00:19:02 +02003114 pstrcpy(boot_devices, sizeof(boot_devices), buf);
j_mayer28c5af52007-11-11 01:50:45 +00003115 }
Jan Kiszkae0f084b2009-07-02 00:19:02 +02003116 if (!legacy) {
3117 if (get_param_value(buf, sizeof(buf),
3118 "once", optarg)) {
Eduardo Habkost4e9e9d62010-04-06 19:22:08 -03003119 validate_bootdevices(buf);
Anthony Liguori7267c092011-08-20 22:09:37 -05003120 standard_boot_devices = g_strdup(boot_devices);
Jan Kiszkae0f084b2009-07-02 00:19:02 +02003121 pstrcpy(boot_devices, sizeof(boot_devices), buf);
3122 qemu_register_reset(restore_boot_devices,
3123 standard_boot_devices);
3124 }
Jan Kiszka95387492009-07-02 00:19:02 +02003125 if (get_param_value(buf, sizeof(buf),
3126 "menu", optarg)) {
3127 if (!strcmp(buf, "on")) {
3128 boot_menu = 1;
3129 } else if (!strcmp(buf, "off")) {
3130 boot_menu = 0;
3131 } else {
3132 fprintf(stderr,
3133 "qemu: invalid option value '%s'\n",
3134 buf);
3135 exit(1);
3136 }
3137 }
wayne3d3b8302011-07-27 18:04:55 +08003138 qemu_opts_parse(qemu_find_opts("boot-opts"),
3139 optarg, 0);
Jan Kiszkae0f084b2009-07-02 00:19:02 +02003140 }
bellard36b486b2003-11-11 13:36:08 +00003141 }
3142 break;
bellardcd6f1162004-05-13 22:02:20 +00003143 case QEMU_OPTION_fda:
bellardcd6f1162004-05-13 22:02:20 +00003144 case QEMU_OPTION_fdb:
Markus Armbruster2292dda2011-01-28 11:21:41 +01003145 drive_add(IF_FLOPPY, popt->index - QEMU_OPTION_fda,
3146 optarg, FD_OPTS);
bellardc45886d2004-01-05 00:02:06 +00003147 break;
bellard52ca8d62006-06-14 16:03:05 +00003148 case QEMU_OPTION_no_fd_bootchk:
3149 fd_bootchk = 0;
3150 break;
Mark McLoughlina1ea4582009-10-08 19:58:26 +01003151 case QEMU_OPTION_netdev:
Gerd Hoffmann3329f072010-08-20 13:52:01 +02003152 if (net_client_parse(qemu_find_opts("netdev"), optarg) == -1) {
Mark McLoughlina1ea4582009-10-08 19:58:26 +01003153 exit(1);
3154 }
3155 break;
bellard7c9d8e02005-11-15 22:16:05 +00003156 case QEMU_OPTION_net:
Gerd Hoffmann3329f072010-08-20 13:52:01 +02003157 if (net_client_parse(qemu_find_opts("net"), optarg) == -1) {
bellardc4b1fcc2004-03-14 21:44:30 +00003158 exit(1);
3159 }
bellard702c6512004-04-02 21:21:32 +00003160 break;
Ronnie Sahlbergf9dadc92012-01-26 09:39:02 +11003161#ifdef CONFIG_LIBISCSI
3162 case QEMU_OPTION_iscsi:
3163 opts = qemu_opts_parse(qemu_find_opts("iscsi"), optarg, 0);
3164 if (!opts) {
3165 exit(1);
3166 }
3167 break;
3168#endif
bellardc7f74642004-08-24 21:57:12 +00003169#ifdef CONFIG_SLIRP
3170 case QEMU_OPTION_tftp:
Jan Kiszkaad196a92009-06-24 14:42:28 +02003171 legacy_tftp_prefix = optarg;
bellard9bf05442004-08-25 22:12:49 +00003172 break;
ths47d5d012007-02-20 00:05:08 +00003173 case QEMU_OPTION_bootp:
Jan Kiszkaad196a92009-06-24 14:42:28 +02003174 legacy_bootp_filename = optarg;
ths47d5d012007-02-20 00:05:08 +00003175 break;
bellard9bf05442004-08-25 22:12:49 +00003176 case QEMU_OPTION_redir:
Markus Armbruster07527062009-10-06 12:16:57 +01003177 if (net_slirp_redir(optarg) < 0)
3178 exit(1);
bellard9bf05442004-08-25 22:12:49 +00003179 break;
bellardc7f74642004-08-24 21:57:12 +00003180#endif
balrogdc72ac12008-11-09 00:04:26 +00003181 case QEMU_OPTION_bt:
Gerd Hoffmannbd3c9482009-07-15 13:59:26 +02003182 add_device_config(DEV_BT, optarg);
balrogdc72ac12008-11-09 00:04:26 +00003183 break;
bellard1d14ffa2005-10-30 18:58:22 +00003184 case QEMU_OPTION_audio_help:
Blue Swirlad960902010-03-29 19:23:52 +00003185 if (!(audio_available())) {
3186 printf("Option %s not supported for this target\n", popt->name);
3187 exit(1);
3188 }
bellard1d14ffa2005-10-30 18:58:22 +00003189 AUD_help ();
3190 exit (0);
3191 break;
3192 case QEMU_OPTION_soundhw:
Blue Swirlad960902010-03-29 19:23:52 +00003193 if (!(audio_available())) {
3194 printf("Option %s not supported for this target\n", popt->name);
3195 exit(1);
3196 }
bellard1d14ffa2005-10-30 18:58:22 +00003197 select_soundhw (optarg);
3198 break;
bellardcd6f1162004-05-13 22:02:20 +00003199 case QEMU_OPTION_h:
ths15f82202007-06-29 23:26:08 +00003200 help(0);
bellardcd6f1162004-05-13 22:02:20 +00003201 break;
pbrook9bd7e6d2009-04-07 22:58:45 +00003202 case QEMU_OPTION_version:
3203 version();
3204 exit(0);
3205 break;
aurel3200f82b82008-04-27 21:12:55 +00003206 case QEMU_OPTION_m: {
Jes Sorensen70b4f4b2011-01-05 11:41:02 +01003207 int64_t value;
Markus Armbrusterff961012012-08-15 13:12:19 +02003208 uint64_t sz;
Markus Armbruster961b42b2011-11-22 09:46:03 +01003209 char *end;
aurel3200f82b82008-04-27 21:12:55 +00003210
Markus Armbruster961b42b2011-11-22 09:46:03 +01003211 value = strtosz(optarg, &end);
3212 if (value < 0 || *end) {
aurel3200f82b82008-04-27 21:12:55 +00003213 fprintf(stderr, "qemu: invalid ram size: %s\n", optarg);
bellardcd6f1162004-05-13 22:02:20 +00003214 exit(1);
3215 }
Markus Armbrusterff961012012-08-15 13:12:19 +02003216 sz = QEMU_ALIGN_UP((uint64_t)value, 8192);
3217 ram_size = sz;
3218 if (ram_size != sz) {
aurel3200f82b82008-04-27 21:12:55 +00003219 fprintf(stderr, "qemu: ram size too large\n");
3220 exit(1);
3221 }
bellardcd6f1162004-05-13 22:02:20 +00003222 break;
aurel3200f82b82008-04-27 21:12:55 +00003223 }
Marcelo Tosattic9027602010-03-01 20:25:08 -03003224 case QEMU_OPTION_mempath:
3225 mem_path = optarg;
3226 break;
3227#ifdef MAP_POPULATE
3228 case QEMU_OPTION_mem_prealloc:
3229 mem_prealloc = 1;
3230 break;
3231#endif
bellardcd6f1162004-05-13 22:02:20 +00003232 case QEMU_OPTION_d:
Matthew Fernandezc235d732011-06-07 16:32:40 +00003233 log_mask = optarg;
3234 break;
3235 case QEMU_OPTION_D:
3236 log_file = optarg;
bellardcd6f1162004-05-13 22:02:20 +00003237 break;
bellardcd6f1162004-05-13 22:02:20 +00003238 case QEMU_OPTION_s:
Markus Armbrusteref0c4a02012-02-07 15:09:13 +01003239 add_device_config(DEV_GDB, "tcp::" DEFAULT_GDBSTUB_PORT);
bellardcd6f1162004-05-13 22:02:20 +00003240 break;
aliguori59030a82009-04-05 18:43:41 +00003241 case QEMU_OPTION_gdb:
Markus Armbrusteref0c4a02012-02-07 15:09:13 +01003242 add_device_config(DEV_GDB, optarg);
bellardcd6f1162004-05-13 22:02:20 +00003243 break;
bellardcd6f1162004-05-13 22:02:20 +00003244 case QEMU_OPTION_L:
Paul Brook5cea8592009-05-30 00:52:44 +01003245 data_dir = optarg;
bellardcd6f1162004-05-13 22:02:20 +00003246 break;
j_mayer1192dad2007-10-05 13:08:35 +00003247 case QEMU_OPTION_bios:
3248 bios_name = optarg;
3249 break;
aurel321b530a62009-04-05 20:08:59 +00003250 case QEMU_OPTION_singlestep:
3251 singlestep = 1;
3252 break;
bellardcd6f1162004-05-13 22:02:20 +00003253 case QEMU_OPTION_S:
pbrook3c07f8e2007-01-21 16:47:01 +00003254 autostart = 0;
bellardcd6f1162004-05-13 22:02:20 +00003255 break;
bellard3d11d0e2004-12-12 16:56:30 +00003256 case QEMU_OPTION_k:
3257 keyboard_layout = optarg;
3258 break;
bellardee22c2f2004-06-03 12:49:50 +00003259 case QEMU_OPTION_localtime:
3260 rtc_utc = 0;
3261 break;
malc3893c122008-09-28 00:42:05 +00003262 case QEMU_OPTION_vga:
Blue Swirla369da52011-09-27 19:15:42 +00003263 vga_model = optarg;
Paolo Bonzini7f1b17f2012-05-10 09:39:17 +02003264 default_vga = 0;
bellard1bfe8562004-07-08 21:17:50 +00003265 break;
bellarde9b137c2004-06-21 16:46:10 +00003266 case QEMU_OPTION_g:
3267 {
3268 const char *p;
3269 int w, h, depth;
3270 p = optarg;
3271 w = strtol(p, (char **)&p, 10);
3272 if (w <= 0) {
3273 graphic_error:
3274 fprintf(stderr, "qemu: invalid resolution or depth\n");
3275 exit(1);
3276 }
3277 if (*p != 'x')
3278 goto graphic_error;
3279 p++;
3280 h = strtol(p, (char **)&p, 10);
3281 if (h <= 0)
3282 goto graphic_error;
3283 if (*p == 'x') {
3284 p++;
3285 depth = strtol(p, (char **)&p, 10);
ths5fafdf22007-09-16 21:08:06 +00003286 if (depth != 8 && depth != 15 && depth != 16 &&
bellarde9b137c2004-06-21 16:46:10 +00003287 depth != 24 && depth != 32)
3288 goto graphic_error;
3289 } else if (*p == '\0') {
3290 depth = graphic_depth;
3291 } else {
3292 goto graphic_error;
3293 }
ths3b46e622007-09-17 08:09:54 +00003294
bellarde9b137c2004-06-21 16:46:10 +00003295 graphic_width = w;
3296 graphic_height = h;
3297 graphic_depth = depth;
3298 }
3299 break;
ths20d8a3e2007-02-18 17:04:49 +00003300 case QEMU_OPTION_echr:
3301 {
3302 char *r;
3303 term_escape_char = strtol(optarg, &r, 0);
3304 if (r == optarg)
3305 printf("Bad argument to echr\n");
3306 break;
3307 }
bellard82c643f2004-07-14 17:28:13 +00003308 case QEMU_OPTION_monitor:
Gerd Hoffmann6ca55822009-12-08 13:11:52 +01003309 monitor_parse(optarg, "readline");
3310 default_monitor = 0;
3311 break;
3312 case QEMU_OPTION_qmp:
3313 monitor_parse(optarg, "control");
Anthony Liguori2d114dc2010-03-21 14:14:38 -05003314 default_monitor = 0;
bellard82c643f2004-07-14 17:28:13 +00003315 break;
Gerd Hoffmann22a0e042009-12-08 13:11:51 +01003316 case QEMU_OPTION_mon:
Gerd Hoffmann3329f072010-08-20 13:52:01 +02003317 opts = qemu_opts_parse(qemu_find_opts("mon"), optarg, 1);
Gerd Hoffmann22a0e042009-12-08 13:11:51 +01003318 if (!opts) {
Gerd Hoffmann22a0e042009-12-08 13:11:51 +01003319 exit(1);
3320 }
Anthony Liguori2d114dc2010-03-21 14:14:38 -05003321 default_monitor = 0;
Gerd Hoffmann22a0e042009-12-08 13:11:51 +01003322 break;
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003323 case QEMU_OPTION_chardev:
Gerd Hoffmann3329f072010-08-20 13:52:01 +02003324 opts = qemu_opts_parse(qemu_find_opts("chardev"), optarg, 1);
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003325 if (!opts) {
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003326 exit(1);
3327 }
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003328 break;
Gautham R Shenoy74db9202010-04-29 17:44:43 +05303329 case QEMU_OPTION_fsdev:
Gerd Hoffmann03b0ba72010-08-20 13:52:02 +02003330 olist = qemu_find_opts("fsdev");
3331 if (!olist) {
3332 fprintf(stderr, "fsdev is not supported by this qemu build.\n");
3333 exit(1);
3334 }
3335 opts = qemu_opts_parse(olist, optarg, 1);
Gautham R Shenoy74db9202010-04-29 17:44:43 +05303336 if (!opts) {
3337 fprintf(stderr, "parse error: %s\n", optarg);
3338 exit(1);
3339 }
3340 break;
Gautham R Shenoy3d54abc2010-04-29 17:45:03 +05303341 case QEMU_OPTION_virtfs: {
Stefan Hajnoczie14ea472011-03-16 08:31:43 +00003342 QemuOpts *fsdev;
3343 QemuOpts *device;
M. Mohan Kumar84a87cc2011-12-14 13:58:47 +05303344 const char *writeout, *sock_fd, *socket;
Gautham R Shenoy3d54abc2010-04-29 17:45:03 +05303345
Gerd Hoffmann03b0ba72010-08-20 13:52:02 +02003346 olist = qemu_find_opts("virtfs");
3347 if (!olist) {
3348 fprintf(stderr, "virtfs is not supported by this qemu build.\n");
3349 exit(1);
3350 }
3351 opts = qemu_opts_parse(olist, optarg, 1);
Gautham R Shenoy3d54abc2010-04-29 17:45:03 +05303352 if (!opts) {
3353 fprintf(stderr, "parse error: %s\n", optarg);
3354 exit(1);
3355 }
3356
Aneesh Kumar K.Vfbcbf102011-10-13 12:28:04 +05303357 if (qemu_opt_get(opts, "fsdriver") == NULL ||
Aneesh Kumar K.V99519f02011-12-14 13:48:59 +05303358 qemu_opt_get(opts, "mount_tag") == NULL) {
3359 fprintf(stderr, "Usage: -virtfs fsdriver,mount_tag=tag.\n");
Venkateswararao Jujjuri (JV)9ce56db2010-06-14 13:34:40 -07003360 exit(1);
3361 }
Stefan Hajnoczie14ea472011-03-16 08:31:43 +00003362 fsdev = qemu_opts_create(qemu_find_opts("fsdev"),
Luiz Capitulino8be7e7e2012-03-20 15:51:57 -03003363 qemu_opt_get(opts, "mount_tag"),
3364 1, NULL);
Stefan Hajnoczie14ea472011-03-16 08:31:43 +00003365 if (!fsdev) {
3366 fprintf(stderr, "duplicate fsdev id: %s\n",
3367 qemu_opt_get(opts, "mount_tag"));
Gautham R Shenoy3d54abc2010-04-29 17:45:03 +05303368 exit(1);
3369 }
Aneesh Kumar K.Vd3ab98e2011-10-12 19:11:23 +05303370
3371 writeout = qemu_opt_get(opts, "writeout");
3372 if (writeout) {
3373#ifdef CONFIG_SYNC_FILE_RANGE
3374 qemu_opt_set(fsdev, "writeout", writeout);
3375#else
3376 fprintf(stderr, "writeout=immediate not supported on "
3377 "this platform\n");
3378 exit(1);
3379#endif
3380 }
Aneesh Kumar K.Vfbcbf102011-10-13 12:28:04 +05303381 qemu_opt_set(fsdev, "fsdriver", qemu_opt_get(opts, "fsdriver"));
Stefan Hajnoczie14ea472011-03-16 08:31:43 +00003382 qemu_opt_set(fsdev, "path", qemu_opt_get(opts, "path"));
3383 qemu_opt_set(fsdev, "security_model",
3384 qemu_opt_get(opts, "security_model"));
M. Mohan Kumar84a87cc2011-12-14 13:58:47 +05303385 socket = qemu_opt_get(opts, "socket");
3386 if (socket) {
3387 qemu_opt_set(fsdev, "socket", socket);
3388 }
M. Mohan Kumar4c793dd2011-12-14 13:49:28 +05303389 sock_fd = qemu_opt_get(opts, "sock_fd");
3390 if (sock_fd) {
3391 qemu_opt_set(fsdev, "sock_fd", sock_fd);
3392 }
Gautham R Shenoy3d54abc2010-04-29 17:45:03 +05303393
M. Mohan Kumar2c74c2c2011-10-25 12:10:39 +05303394 qemu_opt_set_bool(fsdev, "readonly",
3395 qemu_opt_get_bool(opts, "readonly", 0));
Dong Xu Wange478b442012-12-06 14:47:22 +08003396 device = qemu_opts_create_nofail(qemu_find_opts("device"));
Stefan Hajnoczie14ea472011-03-16 08:31:43 +00003397 qemu_opt_set(device, "driver", "virtio-9p-pci");
3398 qemu_opt_set(device, "fsdev",
3399 qemu_opt_get(opts, "mount_tag"));
3400 qemu_opt_set(device, "mount_tag",
3401 qemu_opt_get(opts, "mount_tag"));
Gautham R Shenoy3d54abc2010-04-29 17:45:03 +05303402 break;
3403 }
Aneesh Kumar K.V9db221a2011-10-25 12:10:40 +05303404 case QEMU_OPTION_virtfs_synth: {
3405 QemuOpts *fsdev;
3406 QemuOpts *device;
3407
Luiz Capitulino8be7e7e2012-03-20 15:51:57 -03003408 fsdev = qemu_opts_create(qemu_find_opts("fsdev"), "v_synth",
3409 1, NULL);
Aneesh Kumar K.V9db221a2011-10-25 12:10:40 +05303410 if (!fsdev) {
3411 fprintf(stderr, "duplicate option: %s\n", "virtfs_synth");
3412 exit(1);
3413 }
3414 qemu_opt_set(fsdev, "fsdriver", "synth");
Aneesh Kumar K.V9db221a2011-10-25 12:10:40 +05303415
Dong Xu Wange478b442012-12-06 14:47:22 +08003416 device = qemu_opts_create_nofail(qemu_find_opts("device"));
Aneesh Kumar K.V9db221a2011-10-25 12:10:40 +05303417 qemu_opt_set(device, "driver", "virtio-9p-pci");
3418 qemu_opt_set(device, "fsdev", "v_synth");
3419 qemu_opt_set(device, "mount_tag", "v_synth");
3420 break;
3421 }
bellard82c643f2004-07-14 17:28:13 +00003422 case QEMU_OPTION_serial:
Gerd Hoffmann998bbd72009-12-08 13:11:41 +01003423 add_device_config(DEV_SERIAL, optarg);
3424 default_serial = 0;
Jan Kiszka18141ed2010-03-07 11:28:40 +01003425 if (strncmp(optarg, "mon:", 4) == 0) {
3426 default_monitor = 0;
3427 }
bellard82c643f2004-07-14 17:28:13 +00003428 break;
Richard W.M. Jones9dd986c2009-04-25 13:56:19 +01003429 case QEMU_OPTION_watchdog:
Markus Armbruster09aaa162009-08-21 10:31:34 +02003430 if (watchdog) {
3431 fprintf(stderr,
3432 "qemu: only one watchdog option may be given\n");
3433 return 1;
3434 }
3435 watchdog = optarg;
Richard W.M. Jones9dd986c2009-04-25 13:56:19 +01003436 break;
3437 case QEMU_OPTION_watchdog_action:
3438 if (select_watchdog_action(optarg) == -1) {
3439 fprintf(stderr, "Unknown -watchdog-action parameter\n");
3440 exit(1);
3441 }
3442 break;
aliguori51ecf132009-01-15 20:06:40 +00003443 case QEMU_OPTION_virtiocon:
Gerd Hoffmannaee1b932009-12-08 13:11:53 +01003444 add_device_config(DEV_VIRTCON, optarg);
3445 default_virtcon = 0;
Jan Kiszka18141ed2010-03-07 11:28:40 +01003446 if (strncmp(optarg, "mon:", 4) == 0) {
3447 default_monitor = 0;
3448 }
aliguori51ecf132009-01-15 20:06:40 +00003449 break;
bellard6508fe52005-01-15 12:02:56 +00003450 case QEMU_OPTION_parallel:
Gerd Hoffmann6a5e8b02009-12-08 13:11:42 +01003451 add_device_config(DEV_PARALLEL, optarg);
3452 default_parallel = 0;
Jan Kiszka18141ed2010-03-07 11:28:40 +01003453 if (strncmp(optarg, "mon:", 4) == 0) {
3454 default_monitor = 0;
3455 }
bellard6508fe52005-01-15 12:02:56 +00003456 break;
H. Peter Anvinc9f398e2009-12-29 13:51:36 -08003457 case QEMU_OPTION_debugcon:
3458 add_device_config(DEV_DEBUGCON, optarg);
3459 break;
bellardd63d3072004-10-03 13:29:03 +00003460 case QEMU_OPTION_loadvm:
3461 loadvm = optarg;
3462 break;
3463 case QEMU_OPTION_full_screen:
3464 full_screen = 1;
3465 break;
ths667acca2006-12-11 02:08:05 +00003466#ifdef CONFIG_SDL
ths43523e92007-02-18 18:19:32 +00003467 case QEMU_OPTION_no_frame:
3468 no_frame = 1;
3469 break;
ths3780e192007-06-21 21:08:02 +00003470 case QEMU_OPTION_alt_grab:
3471 alt_grab = 1;
3472 break;
Dustin Kirkland0ca9f8a2009-09-17 15:48:04 -05003473 case QEMU_OPTION_ctrl_grab:
3474 ctrl_grab = 1;
3475 break;
ths667acca2006-12-11 02:08:05 +00003476 case QEMU_OPTION_no_quit:
3477 no_quit = 1;
3478 break;
aliguori7d957bd2009-01-15 22:14:11 +00003479 case QEMU_OPTION_sdl:
Anthony Liguori993fbfd2009-05-21 16:54:00 -05003480 display_type = DT_SDL;
aliguori7d957bd2009-01-15 22:14:11 +00003481 break;
Jes Sorensen58fc0962011-03-16 13:33:34 +01003482#else
3483 case QEMU_OPTION_no_frame:
3484 case QEMU_OPTION_alt_grab:
3485 case QEMU_OPTION_ctrl_grab:
3486 case QEMU_OPTION_no_quit:
3487 case QEMU_OPTION_sdl:
3488 fprintf(stderr, "SDL support is disabled\n");
3489 exit(1);
ths667acca2006-12-11 02:08:05 +00003490#endif
bellardf7cce892004-12-08 22:21:25 +00003491 case QEMU_OPTION_pidfile:
ths93815bc2007-03-19 15:58:31 +00003492 pid_file = optarg;
bellardf7cce892004-12-08 22:21:25 +00003493 break;
bellarda09db212005-04-30 16:10:35 +00003494 case QEMU_OPTION_win2k_hack:
3495 win2k_install_hack = 1;
3496 break;
Jan Kiszka433acf02012-01-23 20:15:12 +01003497 case QEMU_OPTION_rtc_td_hack: {
3498 static GlobalProperty slew_lost_ticks[] = {
3499 {
3500 .driver = "mc146818rtc",
3501 .property = "lost_tick_policy",
3502 .value = "slew",
3503 },
3504 { /* end of list */ }
3505 };
3506
3507 qdev_prop_register_global_list(slew_lost_ticks);
aliguori73822ec2009-01-15 20:11:34 +00003508 break;
Jan Kiszka433acf02012-01-23 20:15:12 +01003509 }
aliguori8a92ea22009-02-27 20:12:36 +00003510 case QEMU_OPTION_acpitable:
Blue Swirlde06f8d2010-03-29 19:23:50 +00003511 do_acpitable_option(optarg);
aliguori8a92ea22009-02-27 20:12:36 +00003512 break;
aliguorib6f6e3d2009-04-17 18:59:56 +00003513 case QEMU_OPTION_smbios:
Blue Swirlde06f8d2010-03-29 19:23:50 +00003514 do_smbios_option(optarg);
aliguorib6f6e3d2009-04-17 18:59:56 +00003515 break;
aliguori7ba1e612008-11-05 16:04:33 +00003516 case QEMU_OPTION_enable_kvm:
Anthony PERARD303d4e82010-09-21 20:05:31 +01003517 olist = qemu_find_opts("machine");
Anthony PERARD303d4e82010-09-21 20:05:31 +01003518 qemu_opts_parse(olist, "accel=kvm", 0);
3519 break;
3520 case QEMU_OPTION_machine:
3521 olist = qemu_find_opts("machine");
Jan Kiszka9052ea62011-07-23 12:38:37 +02003522 opts = qemu_opts_parse(olist, optarg, 1);
Anthony PERARD303d4e82010-09-21 20:05:31 +01003523 if (!opts) {
3524 fprintf(stderr, "parse error: %s\n", optarg);
3525 exit(1);
3526 }
Jan Kiszka2645c6d2011-07-25 18:11:20 +02003527 optarg = qemu_opt_get(opts, "type");
3528 if (optarg) {
3529 machine = machine_parse(optarg);
3530 }
aliguori7ba1e612008-11-05 16:04:33 +00003531 break;
Jan Kiszkaa0dac022012-10-05 14:51:45 -03003532 case QEMU_OPTION_no_kvm:
3533 olist = qemu_find_opts("machine");
3534 qemu_opts_parse(olist, "accel=tcg", 0);
3535 break;
Jan Kiszka4086bde2012-10-05 14:51:41 -03003536 case QEMU_OPTION_no_kvm_pit: {
3537 fprintf(stderr, "Warning: KVM PIT can no longer be disabled "
3538 "separately.\n");
3539 break;
3540 }
Jan Kiszkac21fb4f2012-10-05 14:51:42 -03003541 case QEMU_OPTION_no_kvm_pit_reinjection: {
3542 static GlobalProperty kvm_pit_lost_tick_policy[] = {
3543 {
3544 .driver = "kvm-pit",
3545 .property = "lost_tick_policy",
3546 .value = "discard",
3547 },
3548 { /* end of list */ }
3549 };
3550
3551 fprintf(stderr, "Warning: option deprecated, use "
3552 "lost_tick_policy property of kvm-pit instead.\n");
3553 qdev_prop_register_global_list(kvm_pit_lost_tick_policy);
3554 break;
3555 }
bellardbb36d472005-11-05 14:22:28 +00003556 case QEMU_OPTION_usb:
Peter Maydellfa5358c2012-11-22 16:48:45 +00003557 olist = qemu_find_opts("machine");
3558 qemu_opts_parse(olist, "usb=on", 0);
bellardbb36d472005-11-05 14:22:28 +00003559 break;
bellarda594cfb2005-11-06 16:13:29 +00003560 case QEMU_OPTION_usbdevice:
Peter Maydellfa5358c2012-11-22 16:48:45 +00003561 olist = qemu_find_opts("machine");
3562 qemu_opts_parse(olist, "usb=on", 0);
Gerd Hoffmannbd3c9482009-07-15 13:59:26 +02003563 add_device_config(DEV_USB, optarg);
3564 break;
3565 case QEMU_OPTION_device:
Gerd Hoffmann3329f072010-08-20 13:52:01 +02003566 if (!qemu_opts_parse(qemu_find_opts("device"), optarg, 1)) {
Gerd Hoffmannf31d07d2009-07-31 12:25:37 +02003567 exit(1);
3568 }
bellarda594cfb2005-11-06 16:13:29 +00003569 break;
bellard6a00d602005-11-21 23:25:50 +00003570 case QEMU_OPTION_smp:
Andre Przywaradc6b1c02009-08-19 15:42:40 +02003571 smp_parse(optarg);
aliguorib2097002008-10-07 20:39:39 +00003572 if (smp_cpus < 1) {
bellard6a00d602005-11-21 23:25:50 +00003573 fprintf(stderr, "Invalid number of CPUs\n");
3574 exit(1);
3575 }
Jes Sorensen6be68d72009-07-23 17:03:42 +02003576 if (max_cpus < smp_cpus) {
3577 fprintf(stderr, "maxcpus must be equal to or greater than "
3578 "smp\n");
3579 exit(1);
3580 }
3581 if (max_cpus > 255) {
3582 fprintf(stderr, "Unsupported number of maxcpus\n");
3583 exit(1);
3584 }
bellard6a00d602005-11-21 23:25:50 +00003585 break;
bellard24236862006-04-30 21:28:36 +00003586 case QEMU_OPTION_vnc:
Jes Sorensen821601e2011-03-16 13:33:36 +01003587#ifdef CONFIG_VNC
Gerd Hoffmann6b62dc22010-08-25 10:51:06 +02003588 display_remote++;
Jes Sorensen821601e2011-03-16 13:33:36 +01003589 vnc_display = optarg;
3590#else
3591 fprintf(stderr, "VNC support is disabled\n");
3592 exit(1);
3593#endif
3594 break;
bellard6515b202006-05-03 22:02:44 +00003595 case QEMU_OPTION_no_acpi:
3596 acpi_enabled = 0;
3597 break;
aliguori16b29ae2008-12-17 23:28:44 +00003598 case QEMU_OPTION_no_hpet:
3599 no_hpet = 1;
3600 break;
Markus Armbruster7d4c3d52009-06-26 19:15:14 +02003601 case QEMU_OPTION_balloon:
3602 if (balloon_parse(optarg) < 0) {
3603 fprintf(stderr, "Unknown -balloon argument %s\n", optarg);
3604 exit(1);
3605 }
Eduardo Habkostdf97b922009-06-10 16:34:08 -03003606 break;
bellardd1beab82006-10-02 19:44:22 +00003607 case QEMU_OPTION_no_reboot:
3608 no_reboot = 1;
3609 break;
aurel32b2f76162008-04-11 21:35:52 +00003610 case QEMU_OPTION_no_shutdown:
3611 no_shutdown = 1;
3612 break;
balrog9467cd42007-05-01 01:34:14 +00003613 case QEMU_OPTION_show_cursor:
3614 cursor_hide = 0;
3615 break;
blueswir18fcb1b92008-09-18 18:29:08 +00003616 case QEMU_OPTION_uuid:
3617 if(qemu_uuid_parse(optarg, qemu_uuid) < 0) {
3618 fprintf(stderr, "Fail to parse UUID string."
3619 " Wrong format.\n");
3620 exit(1);
3621 }
3622 break;
ths9ae02552007-01-05 17:39:04 +00003623 case QEMU_OPTION_option_rom:
3624 if (nb_option_roms >= MAX_OPTION_ROMS) {
3625 fprintf(stderr, "Too many option ROMs\n");
3626 exit(1);
3627 }
Gleb Natapov2e55e842010-12-08 13:35:07 +02003628 opts = qemu_opts_parse(qemu_find_opts("option-rom"), optarg, 1);
3629 option_rom[nb_option_roms].name = qemu_opt_get(opts, "romfile");
3630 option_rom[nb_option_roms].bootindex =
3631 qemu_opt_get_number(opts, "bootindex", -1);
3632 if (!option_rom[nb_option_roms].name) {
3633 fprintf(stderr, "Option ROM file is not specified\n");
3634 exit(1);
3635 }
ths9ae02552007-01-05 17:39:04 +00003636 nb_option_roms++;
3637 break;
pbrook8e716212007-01-20 17:12:09 +00003638 case QEMU_OPTION_semihosting:
3639 semihosting_enabled = 1;
3640 break;
Jan Kiszka88eed342012-10-05 14:51:44 -03003641 case QEMU_OPTION_tdf:
3642 fprintf(stderr, "Warning: user space PIT time drift fix "
3643 "is no longer supported.\n");
3644 break;
thsc35734b2007-03-19 15:17:08 +00003645 case QEMU_OPTION_name:
Anthony Liguori7267c092011-08-20 22:09:37 -05003646 qemu_name = g_strdup(optarg);
Andi Kleen18894652009-07-02 09:34:17 +02003647 {
3648 char *p = strchr(qemu_name, ',');
3649 if (p != NULL) {
3650 *p++ = 0;
3651 if (strncmp(p, "process=", 8)) {
Aurelien Jarno5697f6a2010-12-27 18:29:20 +01003652 fprintf(stderr, "Unknown subargument %s to -name\n", p);
Andi Kleen18894652009-07-02 09:34:17 +02003653 exit(1);
3654 }
3655 p += 8;
Jes Sorensence798cf2010-06-10 11:42:31 +02003656 os_set_proc_name(p);
Andi Kleen18894652009-07-02 09:34:17 +02003657 }
3658 }
thsc35734b2007-03-19 15:17:08 +00003659 break;
blueswir166508602007-05-01 14:16:52 +00003660 case QEMU_OPTION_prom_env:
3661 if (nb_prom_envs >= MAX_PROM_ENVS) {
3662 fprintf(stderr, "Too many prom variables\n");
3663 exit(1);
3664 }
3665 prom_envs[nb_prom_envs] = optarg;
3666 nb_prom_envs++;
3667 break;
balrog2b8f2d42007-07-27 22:08:46 +00003668 case QEMU_OPTION_old_param:
3669 old_param = 1;
ths05ebd532008-01-08 19:32:16 +00003670 break;
thsf3dcfad2007-08-24 01:26:02 +00003671 case QEMU_OPTION_clock:
3672 configure_alarms(optarg);
3673 break;
bellard7e0af5d02007-11-07 16:24:33 +00003674 case QEMU_OPTION_startdate:
Jan Kiszka1ed2fc12009-09-15 13:36:04 +02003675 configure_rtc_date_offset(optarg, 1);
3676 break;
3677 case QEMU_OPTION_rtc:
Gerd Hoffmann3329f072010-08-20 13:52:01 +02003678 opts = qemu_opts_parse(qemu_find_opts("rtc"), optarg, 0);
Jan Kiszka1ed2fc12009-09-15 13:36:04 +02003679 if (!opts) {
Jan Kiszka1ed2fc12009-09-15 13:36:04 +02003680 exit(1);
bellard7e0af5d02007-11-07 16:24:33 +00003681 }
Jan Kiszka1ed2fc12009-09-15 13:36:04 +02003682 configure_rtc(opts);
bellard7e0af5d02007-11-07 16:24:33 +00003683 break;
bellard26a5f132008-05-28 12:30:31 +00003684 case QEMU_OPTION_tb_size:
Jan Kiszkad5ab9712011-08-02 16:10:21 +02003685 tcg_tb_size = strtol(optarg, NULL, 0);
3686 if (tcg_tb_size < 0) {
3687 tcg_tb_size = 0;
3688 }
bellard26a5f132008-05-28 12:30:31 +00003689 break;
pbrook2e70f6e2008-06-29 01:03:05 +00003690 case QEMU_OPTION_icount:
Paolo Bonzini4e3de9e2010-03-10 11:38:48 +01003691 icount_option = optarg;
pbrook2e70f6e2008-06-29 01:03:05 +00003692 break;
aliguori5bb79102008-10-13 03:12:02 +00003693 case QEMU_OPTION_incoming:
3694 incoming = optarg;
Stefano Stabellini81323a62012-01-18 12:23:13 +00003695 runstate_set(RUN_STATE_INMIGRATE);
aliguori5bb79102008-10-13 03:12:02 +00003696 break;
Gerd Hoffmannd8c208d2009-12-08 13:11:46 +01003697 case QEMU_OPTION_nodefaults:
3698 default_serial = 0;
3699 default_parallel = 0;
Gerd Hoffmannaee1b932009-12-08 13:11:53 +01003700 default_virtcon = 0;
Christian Borntraeger2e788492013-01-25 06:00:19 +00003701 default_sclp = 0;
Gerd Hoffmannd8c208d2009-12-08 13:11:46 +01003702 default_monitor = 0;
Gerd Hoffmanncb4522c2009-12-08 13:11:47 +01003703 default_net = 0;
Gerd Hoffmannac33f8f2009-12-16 14:25:39 +01003704 default_floppy = 0;
3705 default_cdrom = 0;
3706 default_sdcard = 0;
Paolo Bonzini7f1b17f2012-05-10 09:39:17 +02003707 default_vga = 0;
Gerd Hoffmannd8c208d2009-12-08 13:11:46 +01003708 break;
aliguorie37630c2009-04-22 15:19:10 +00003709 case QEMU_OPTION_xen_domid:
Blue Swirlad960902010-03-29 19:23:52 +00003710 if (!(xen_available())) {
3711 printf("Option %s not supported for this target\n", popt->name);
3712 exit(1);
3713 }
aliguorie37630c2009-04-22 15:19:10 +00003714 xen_domid = atoi(optarg);
3715 break;
3716 case QEMU_OPTION_xen_create:
Blue Swirlad960902010-03-29 19:23:52 +00003717 if (!(xen_available())) {
3718 printf("Option %s not supported for this target\n", popt->name);
3719 exit(1);
3720 }
aliguorie37630c2009-04-22 15:19:10 +00003721 xen_mode = XEN_CREATE;
3722 break;
3723 case QEMU_OPTION_xen_attach:
Blue Swirlad960902010-03-29 19:23:52 +00003724 if (!(xen_available())) {
3725 printf("Option %s not supported for this target\n", popt->name);
3726 exit(1);
3727 }
aliguorie37630c2009-04-22 15:19:10 +00003728 xen_mode = XEN_ATTACH;
3729 break;
Prerna Saxenaab6540d2010-08-09 11:48:32 +01003730 case QEMU_OPTION_trace:
Lluíse4858972011-08-31 20:31:03 +02003731 {
Prerna Saxenaab6540d2010-08-09 11:48:32 +01003732 opts = qemu_opts_parse(qemu_find_opts("trace"), optarg, 0);
Lluíse4858972011-08-31 20:31:03 +02003733 if (!opts) {
3734 exit(1);
Prerna Saxenaab6540d2010-08-09 11:48:32 +01003735 }
Lluís23d15e82011-08-31 20:31:31 +02003736 trace_events = qemu_opt_get(opts, "events");
Lluíse4858972011-08-31 20:31:03 +02003737 trace_file = qemu_opt_get(opts, "file");
Prerna Saxenaab6540d2010-08-09 11:48:32 +01003738 break;
Lluíse4858972011-08-31 20:31:03 +02003739 }
Gerd Hoffmann715a6642009-10-14 10:39:28 +02003740 case QEMU_OPTION_readconfig:
3741 {
Kevin Wolfdcfb0932010-03-05 17:25:55 +01003742 int ret = qemu_read_config_file(optarg);
3743 if (ret < 0) {
3744 fprintf(stderr, "read config %s: %s\n", optarg,
3745 strerror(-ret));
Gerd Hoffmann715a6642009-10-14 10:39:28 +02003746 exit(1);
3747 }
Gerd Hoffmann715a6642009-10-14 10:39:28 +02003748 break;
3749 }
Gerd Hoffmann29b00402010-03-11 11:13:27 -03003750 case QEMU_OPTION_spice:
3751 olist = qemu_find_opts("spice");
3752 if (!olist) {
3753 fprintf(stderr, "spice is not supported by this qemu build.\n");
3754 exit(1);
3755 }
3756 opts = qemu_opts_parse(olist, optarg, 0);
3757 if (!opts) {
3758 fprintf(stderr, "parse error: %s\n", optarg);
3759 exit(1);
3760 }
3761 break;
Gerd Hoffmann715a6642009-10-14 10:39:28 +02003762 case QEMU_OPTION_writeconfig:
3763 {
3764 FILE *fp;
3765 if (strcmp(optarg, "-") == 0) {
3766 fp = stdout;
3767 } else {
3768 fp = fopen(optarg, "w");
3769 if (fp == NULL) {
3770 fprintf(stderr, "open %s: %s\n", optarg, strerror(errno));
3771 exit(1);
3772 }
3773 }
3774 qemu_config_write(fp);
3775 fclose(fp);
3776 break;
3777 }
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +02003778 case QEMU_OPTION_qtest:
3779 qtest_chrdev = optarg;
3780 break;
3781 case QEMU_OPTION_qtest_log:
3782 qtest_log = optarg;
3783 break;
Eduardo Otubo7d76ad42012-08-14 18:44:08 -03003784 case QEMU_OPTION_sandbox:
3785 opts = qemu_opts_parse(qemu_find_opts("sandbox"), optarg, 1);
3786 if (!opts) {
3787 exit(0);
3788 }
3789 break;
Corey Bryant587ed6b2012-10-18 15:19:34 -04003790 case QEMU_OPTION_add_fd:
3791#ifndef _WIN32
3792 opts = qemu_opts_parse(qemu_find_opts("add-fd"), optarg, 0);
3793 if (!opts) {
3794 exit(0);
3795 }
3796#else
3797 error_report("File descriptor passing is disabled on this "
3798 "platform");
3799 exit(1);
3800#endif
3801 break;
Anthony Liguori68d98d32012-06-25 14:36:33 -05003802 case QEMU_OPTION_object:
3803 opts = qemu_opts_parse(qemu_find_opts("object"), optarg, 1);
3804 break;
Jes Sorensen59a52642010-06-10 11:42:25 +02003805 default:
3806 os_parse_cmd_args(popt->index, optarg);
bellardcd6f1162004-05-13 22:02:20 +00003807 }
bellard0824d6f2003-06-24 13:42:40 +00003808 }
3809 }
Markus Armbruster0f0bc3f2010-02-18 20:13:51 +01003810 loc_set_none();
bellard330d0412003-07-26 18:11:40 +00003811
Paolo Bonzini1c537862012-10-30 00:17:12 +01003812 if (qemu_init_main_loop()) {
3813 fprintf(stderr, "qemu_init_main_loop failed\n");
3814 exit(1);
3815 }
3816
Eduardo Otubo7d76ad42012-08-14 18:44:08 -03003817 if (qemu_opts_foreach(qemu_find_opts("sandbox"), parse_sandbox, NULL, 0)) {
3818 exit(1);
3819 }
3820
Corey Bryant587ed6b2012-10-18 15:19:34 -04003821#ifndef _WIN32
3822 if (qemu_opts_foreach(qemu_find_opts("add-fd"), parse_add_fd, NULL, 1)) {
3823 exit(1);
3824 }
3825
3826 if (qemu_opts_foreach(qemu_find_opts("add-fd"), cleanup_add_fd, NULL, 1)) {
3827 exit(1);
3828 }
3829#endif
3830
Dunrong Huangfb7c2692012-07-24 00:42:20 +08003831 if (machine == NULL) {
3832 fprintf(stderr, "No machine found.\n");
3833 exit(1);
3834 }
3835
Crístian Viana93bfef42012-05-30 00:35:51 -03003836 if (machine->hw_version) {
3837 qemu_set_version(machine->hw_version);
3838 }
3839
Anthony Liguori68d98d32012-06-25 14:36:33 -05003840 if (qemu_opts_foreach(qemu_find_opts("object"),
3841 object_create, NULL, 0) != 0) {
3842 exit(1);
3843 }
3844
Eduardo Habkostecf40be2012-03-09 16:19:07 -03003845 /* Init CPU def lists, based on config
3846 * - Must be called after all the qemu_read_config_file() calls
3847 * - Must be called before list_cpus()
3848 * - Must be called before machine->init()
3849 */
3850 cpudef_init();
3851
Peter Maydellc8057f92012-08-02 13:45:54 +01003852 if (cpu_model && is_help_option(cpu_model)) {
Eduardo Habkost1d6528a2012-03-21 09:33:40 -03003853 list_cpus(stdout, &fprintf, cpu_model);
Eduardo Habkostecf40be2012-03-09 16:19:07 -03003854 exit(0);
3855 }
3856
Matthew Fernandezc235d732011-06-07 16:32:40 +00003857 /* Open the logfile at this point, if necessary. We can't open the logfile
3858 * when encountering either of the logging options (-d or -D) because the
3859 * other one may be encountered later on the command line, changing the
3860 * location or level of logging.
3861 */
3862 if (log_mask) {
3863 if (log_file) {
3864 set_cpu_log_filename(log_file);
3865 }
3866 set_cpu_log(log_mask);
3867 }
3868
Lluís23d15e82011-08-31 20:31:31 +02003869 if (!trace_backend_init(trace_events, trace_file)) {
Lluíse4858972011-08-31 20:31:03 +02003870 exit(1);
Stefan Hajnoczi31d3c9b2011-03-13 20:14:30 +00003871 }
Stefan Hajnoczi0b5538c2011-02-26 18:38:39 +00003872
Paul Brook5cea8592009-05-30 00:52:44 +01003873 /* If no data_dir is specified then try to find it relative to the
3874 executable path. */
3875 if (!data_dir) {
Jes Sorensen61705402010-06-10 11:42:23 +02003876 data_dir = os_find_datadir(argv[0]);
Paul Brook5cea8592009-05-30 00:52:44 +01003877 }
Stefan Weil60474fb2011-09-04 15:17:46 +02003878 /* If all else fails use the install path specified when building. */
Paul Brook5cea8592009-05-30 00:52:44 +01003879 if (!data_dir) {
Paolo Bonzini1dabe052010-05-26 16:08:25 +02003880 data_dir = CONFIG_QEMU_DATADIR;
Paul Brook5cea8592009-05-30 00:52:44 +01003881 }
3882
Jes Sorensen6be68d72009-07-23 17:03:42 +02003883 /*
3884 * Default to max_cpus = smp_cpus, in case the user doesn't
3885 * specify a max_cpus value.
3886 */
3887 if (!max_cpus)
3888 max_cpus = smp_cpus;
3889
balrog3d878ca2008-10-28 10:59:59 +00003890 machine->max_cpus = machine->max_cpus ?: 1; /* Default to UP */
aliguorib2097002008-10-07 20:39:39 +00003891 if (smp_cpus > machine->max_cpus) {
3892 fprintf(stderr, "Number of SMP cpus requested (%d), exceeds max cpus "
3893 "supported by machine `%s' (%d)\n", smp_cpus, machine->name,
3894 machine->max_cpus);
3895 exit(1);
3896 }
3897
Anthony PERARD67b724e2010-11-22 15:44:15 +00003898 /*
3899 * Get the default machine options from the machine if it is not already
3900 * specified either by the configuration file or by the command line.
3901 */
3902 if (machine->default_machine_opts) {
Jan Kiszka25de5932012-01-27 19:55:43 +01003903 qemu_opts_set_defaults(qemu_find_opts("machine"),
3904 machine->default_machine_opts, 0);
Anthony PERARD67b724e2010-11-22 15:44:15 +00003905 }
3906
Gerd Hoffmann3329f072010-08-20 13:52:01 +02003907 qemu_opts_foreach(qemu_find_opts("device"), default_driver_check, NULL, 0);
3908 qemu_opts_foreach(qemu_find_opts("global"), default_driver_check, NULL, 0);
Gerd Hoffmann998bbd72009-12-08 13:11:41 +01003909
Gerd Hoffmann986c5f72009-12-08 13:11:54 +01003910 if (machine->no_serial) {
3911 default_serial = 0;
3912 }
3913 if (machine->no_parallel) {
3914 default_parallel = 0;
3915 }
3916 if (!machine->use_virtcon) {
3917 default_virtcon = 0;
3918 }
Alexander Graf3ef669e2013-01-24 12:18:52 +01003919 if (!machine->use_sclp) {
3920 default_sclp = 0;
3921 }
Gerd Hoffmannac33f8f2009-12-16 14:25:39 +01003922 if (machine->no_floppy) {
3923 default_floppy = 0;
3924 }
3925 if (machine->no_cdrom) {
3926 default_cdrom = 0;
3927 }
3928 if (machine->no_sdcard) {
3929 default_sdcard = 0;
3930 }
Gerd Hoffmann986c5f72009-12-08 13:11:54 +01003931
Michael Tokarevab51b1d2012-12-30 12:48:14 +04003932 if (is_daemonized()) {
3933 /* According to documentation and historically, -nographic redirects
3934 * serial port, parallel port and monitor to stdio, which does not work
3935 * with -daemonize. We can redirect these to null instead, but since
3936 * -nographic is legacy, let's just error out.
3937 * We disallow -nographic only if all other ports are not redirected
3938 * explicitly, to not break existing legacy setups which uses
3939 * -nographic _and_ redirects all ports explicitly - this is valid
3940 * usage, -nographic is just a no-op in this case.
3941 */
3942 if (display_type == DT_NOGRAPHIC
3943 && (default_parallel || default_serial
3944 || default_monitor || default_virtcon)) {
3945 fprintf(stderr, "-nographic can not be used with -daemonize\n");
3946 exit(1);
3947 }
3948#ifdef CONFIG_CURSES
3949 if (display_type == DT_CURSES) {
3950 fprintf(stderr, "curses display can not be used with -daemonize\n");
3951 exit(1);
3952 }
3953#endif
3954 }
3955
Anthony Liguori993fbfd2009-05-21 16:54:00 -05003956 if (display_type == DT_NOGRAPHIC) {
Gerd Hoffmann6a5e8b02009-12-08 13:11:42 +01003957 if (default_parallel)
3958 add_device_config(DEV_PARALLEL, "null");
Gerd Hoffmanne1c09172009-12-08 13:11:44 +01003959 if (default_serial && default_monitor) {
3960 add_device_config(DEV_SERIAL, "mon:stdio");
Gerd Hoffmann986c5f72009-12-08 13:11:54 +01003961 } else if (default_virtcon && default_monitor) {
3962 add_device_config(DEV_VIRTCON, "mon:stdio");
Alexander Graf3ef669e2013-01-24 12:18:52 +01003963 } else if (default_sclp && default_monitor) {
3964 add_device_config(DEV_SCLP, "mon:stdio");
Gerd Hoffmanne1c09172009-12-08 13:11:44 +01003965 } else {
3966 if (default_serial)
3967 add_device_config(DEV_SERIAL, "stdio");
Gerd Hoffmann986c5f72009-12-08 13:11:54 +01003968 if (default_virtcon)
3969 add_device_config(DEV_VIRTCON, "stdio");
Alexander Graf3ef669e2013-01-24 12:18:52 +01003970 if (default_sclp) {
3971 add_device_config(DEV_SCLP, "stdio");
3972 }
Gerd Hoffmanne1c09172009-12-08 13:11:44 +01003973 if (default_monitor)
Gerd Hoffmann6ca55822009-12-08 13:11:52 +01003974 monitor_parse("stdio", "readline");
Gerd Hoffmanne1c09172009-12-08 13:11:44 +01003975 }
Gerd Hoffmann998bbd72009-12-08 13:11:41 +01003976 } else {
3977 if (default_serial)
3978 add_device_config(DEV_SERIAL, "vc:80Cx24C");
Gerd Hoffmann6a5e8b02009-12-08 13:11:42 +01003979 if (default_parallel)
3980 add_device_config(DEV_PARALLEL, "vc:80Cx24C");
Gerd Hoffmannabdeed02009-12-08 13:11:43 +01003981 if (default_monitor)
Gerd Hoffmann6ca55822009-12-08 13:11:52 +01003982 monitor_parse("vc:80Cx24C", "readline");
Alexander Graf38536da2009-12-17 13:06:08 +01003983 if (default_virtcon)
3984 add_device_config(DEV_VIRTCON, "vc:80Cx24C");
Alexander Graf3ef669e2013-01-24 12:18:52 +01003985 if (default_sclp) {
3986 add_device_config(DEV_SCLP, "vc:80Cx24C");
3987 }
aliguoribc0129d2008-08-01 15:12:34 +00003988 }
3989
TeLeMana5829fd2010-04-15 12:37:55 +08003990 socket_init();
3991
Gerd Hoffmann3329f072010-08-20 13:52:01 +02003992 if (qemu_opts_foreach(qemu_find_opts("chardev"), chardev_init_func, NULL, 1) != 0)
Gerd Hoffmann1a688d32009-12-08 13:11:36 +01003993 exit(1);
Venkateswararao Jujjuri (JV)758e8e32010-06-14 13:34:41 -07003994#ifdef CONFIG_VIRTFS
Gerd Hoffmann3329f072010-08-20 13:52:01 +02003995 if (qemu_opts_foreach(qemu_find_opts("fsdev"), fsdev_init_func, NULL, 1) != 0) {
Gautham R Shenoy74db9202010-04-29 17:44:43 +05303996 exit(1);
3997 }
3998#endif
Gerd Hoffmann1a688d32009-12-08 13:11:36 +01003999
Jes Sorenseneb505be2010-06-10 11:42:28 +02004000 os_daemonize();
ths71e3ceb2006-12-22 02:11:31 +00004001
thsaa26bb22007-03-25 21:33:06 +00004002 if (pid_file && qemu_create_pidfile(pid_file) != 0) {
Jes Sorenseneb505be2010-06-10 11:42:28 +02004003 os_pidfile_error();
ths93815bc2007-03-19 15:58:31 +00004004 exit(1);
4005 }
4006
Jan Kiszka0ac543d2011-08-15 16:18:57 -07004007 /* init the memory */
4008 if (ram_size == 0) {
4009 ram_size = DEFAULT_RAM_SIZE * 1024 * 1024;
4010 }
4011
Bruce Rogers3d1d9652012-08-09 12:47:40 -06004012 if (qemu_opts_foreach(qemu_find_opts("device"), device_help_func, NULL, 0)
4013 != 0) {
4014 exit(0);
4015 }
4016
Anthony PERARD303d4e82010-09-21 20:05:31 +01004017 configure_accelerator();
Marcelo Tosatti214910a2009-09-18 02:41:23 -03004018
Peter Maydell967c0da2012-02-22 22:40:00 +00004019 machine_opts = qemu_opts_find(qemu_find_opts("machine"), 0);
4020 if (machine_opts) {
4021 kernel_filename = qemu_opt_get(machine_opts, "kernel");
4022 initrd_filename = qemu_opt_get(machine_opts, "initrd");
4023 kernel_cmdline = qemu_opt_get(machine_opts, "append");
4024 } else {
4025 kernel_filename = initrd_filename = kernel_cmdline = NULL;
4026 }
4027
Peter Maydella0abe472012-02-08 05:41:39 +00004028 if (!kernel_cmdline) {
4029 kernel_cmdline = "";
4030 }
4031
bellarda20dd502003-09-30 21:07:02 +00004032 linux_boot = (kernel_filename != NULL);
balrog6c41b272007-11-17 12:12:29 +00004033
thsf8d39c02008-07-03 10:01:15 +00004034 if (!linux_boot && *kernel_cmdline != '\0') {
4035 fprintf(stderr, "-append only allowed with -kernel option\n");
4036 exit(1);
4037 }
4038
4039 if (!linux_boot && initrd_filename != NULL) {
4040 fprintf(stderr, "-initrd only allowed with -kernel option\n");
4041 exit(1);
4042 }
4043
Grant Likely412beee2012-03-02 11:56:38 +00004044 if (!linux_boot && machine_opts && qemu_opt_get(machine_opts, "dtb")) {
4045 fprintf(stderr, "-dtb only allowed with -kernel option\n");
4046 exit(1);
4047 }
4048
Jes Sorensen9156d762010-06-10 11:42:30 +02004049 os_set_line_buffering();
ths3b46e622007-09-17 08:09:54 +00004050
Paolo Bonzini49cf5722012-11-02 15:43:24 +01004051 qemu_init_cpu_loop();
4052 qemu_mutex_lock_iothread();
4053
Alon Levyad1be892012-03-14 20:33:37 +02004054#ifdef CONFIG_SPICE
4055 /* spice needs the timers to be initialized by this point */
4056 qemu_spice_init();
4057#endif
4058
Max Filippov0abe9052011-11-10 15:38:42 +04004059 if (icount_option && (kvm_enabled() || xen_enabled())) {
4060 fprintf(stderr, "-icount is not allowed with kvm or xen\n");
4061 exit(1);
4062 }
Paolo Bonzini4e3de9e2010-03-10 11:38:48 +01004063 configure_icount(icount_option);
pbrook634fce92006-07-15 17:40:09 +00004064
Amos Kongf30dbae2012-12-11 22:20:15 +08004065 /* clean up network at qemu process termination */
4066 atexit(&net_cleanup);
4067
Mark McLoughlindc1c9fe2009-10-06 12:17:16 +01004068 if (net_init_clients() < 0) {
4069 exit(1);
bellard702c6512004-04-02 21:21:32 +00004070 }
bellardf1510b22003-06-25 00:07:40 +00004071
balrogdc72ac12008-11-09 00:04:26 +00004072 /* init the bluetooth world */
Gerd Hoffmannbd3c9482009-07-15 13:59:26 +02004073 if (foreach_device_config(DEV_BT, bt_parse))
4074 exit(1);
balrogdc72ac12008-11-09 00:04:26 +00004075
Anthony PERARD834e76e2011-07-20 08:17:44 +00004076 if (!xen_enabled()) {
4077 /* On 32-bit hosts, QEMU is limited by virtual address space */
4078 if (ram_size > (2047 << 20) && HOST_LONG_BITS == 32) {
4079 fprintf(stderr, "qemu: at most 2047 MB RAM can be simulated\n");
4080 exit(1);
4081 }
4082 }
balrog7fb4fdc2008-04-24 17:59:27 +00004083
Jan Kiszkad5ab9712011-08-02 16:10:21 +02004084 cpu_exec_init_all();
bellard26a5f132008-05-28 12:30:31 +00004085
Markus Armbrustereb852012009-10-27 18:41:44 +01004086 bdrv_init_with_whitelist();
thse4bcb142007-12-02 04:51:10 +00004087
lirans@il.ibm.comc163b5c2009-11-02 15:40:58 +02004088 blk_mig_init();
4089
ths96d30e42007-01-07 20:42:14 +00004090 /* open the virtual block devices */
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +02004091 if (snapshot)
Gerd Hoffmann3329f072010-08-20 13:52:01 +02004092 qemu_opts_foreach(qemu_find_opts("drive"), drive_enable_snapshot, NULL, 0);
Christian Borntraeger2d0d2832012-11-20 15:30:34 +01004093 if (qemu_opts_foreach(qemu_find_opts("drive"), drive_init_func,
4094 &machine->block_default_type, 1) != 0) {
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +02004095 exit(1);
Christian Borntraeger2d0d2832012-11-20 15:30:34 +01004096 }
balrog3e3d5812007-04-30 02:09:25 +00004097
Christian Borntraeger3c42ea62012-11-22 21:02:55 +01004098 default_drive(default_cdrom, snapshot, machine->block_default_type, 2,
4099 CDROM_OPTS);
4100 default_drive(default_floppy, snapshot, IF_FLOPPY, 0, FD_OPTS);
4101 default_drive(default_sdcard, snapshot, IF_SD, 0, SD_OPTS);
Markus Armbruster4e5d9b52011-01-28 11:21:45 +01004102
Juan Quintela7908c782012-06-26 18:46:10 +02004103 register_savevm_live(NULL, "ram", 0, 4, &savevm_ram_handlers, NULL);
bellard8a7ddc32004-03-31 19:00:16 +00004104
aliguori268a3622009-04-21 22:30:27 +00004105 if (nb_numa_nodes > 0) {
4106 int i;
4107
Sasha Levinea0e5412011-06-29 23:29:39 -04004108 if (nb_numa_nodes > MAX_NODES) {
4109 nb_numa_nodes = MAX_NODES;
aliguori268a3622009-04-21 22:30:27 +00004110 }
4111
4112 /* If no memory size if given for any node, assume the default case
4113 * and distribute the available memory equally across all nodes
4114 */
4115 for (i = 0; i < nb_numa_nodes; i++) {
4116 if (node_mem[i] != 0)
4117 break;
4118 }
4119 if (i == nb_numa_nodes) {
4120 uint64_t usedmem = 0;
4121
4122 /* On Linux, the each node's border has to be 8MB aligned,
4123 * the final node gets the rest.
4124 */
4125 for (i = 0; i < nb_numa_nodes - 1; i++) {
4126 node_mem[i] = (ram_size / nb_numa_nodes) & ~((1 << 23UL) - 1);
4127 usedmem += node_mem[i];
4128 }
4129 node_mem[i] = ram_size - usedmem;
4130 }
4131
4132 for (i = 0; i < nb_numa_nodes; i++) {
Chegu Vinodee785fe2012-07-16 21:31:30 -07004133 if (!bitmap_empty(node_cpumask[i], MAX_CPUMASK_BITS)) {
aliguori268a3622009-04-21 22:30:27 +00004134 break;
Chegu Vinodee785fe2012-07-16 21:31:30 -07004135 }
aliguori268a3622009-04-21 22:30:27 +00004136 }
4137 /* assigning the VCPUs round-robin is easier to implement, guest OSes
4138 * must cope with this anyway, because there are BIOSes out there in
4139 * real machines which also use this scheme.
4140 */
4141 if (i == nb_numa_nodes) {
Vasilis Liaskovitis991dfef2011-10-26 14:19:00 +02004142 for (i = 0; i < max_cpus; i++) {
Chegu Vinodee785fe2012-07-16 21:31:30 -07004143 set_bit(i, node_cpumask[i % nb_numa_nodes]);
aliguori268a3622009-04-21 22:30:27 +00004144 }
4145 }
4146 }
4147
Gerd Hoffmann3329f072010-08-20 13:52:01 +02004148 if (qemu_opts_foreach(qemu_find_opts("mon"), mon_init_func, NULL, 1) != 0) {
Jan Kiszka157b9312010-04-06 16:55:53 +02004149 exit(1);
4150 }
4151
Gerd Hoffmann998bbd72009-12-08 13:11:41 +01004152 if (foreach_device_config(DEV_SERIAL, serial_parse) < 0)
4153 exit(1);
Gerd Hoffmann6a5e8b02009-12-08 13:11:42 +01004154 if (foreach_device_config(DEV_PARALLEL, parallel_parse) < 0)
4155 exit(1);
Gerd Hoffmannaee1b932009-12-08 13:11:53 +01004156 if (foreach_device_config(DEV_VIRTCON, virtcon_parse) < 0)
4157 exit(1);
Alexander Graf3ef669e2013-01-24 12:18:52 +01004158 if (foreach_device_config(DEV_SCLP, sclp_parse) < 0) {
4159 exit(1);
4160 }
H. Peter Anvinc9f398e2009-12-29 13:51:36 -08004161 if (foreach_device_config(DEV_DEBUGCON, debugcon_parse) < 0)
4162 exit(1);
aliguori2796dae2009-01-16 20:23:27 +00004163
Paolo Bonzini7f1b17f2012-05-10 09:39:17 +02004164 /* If no default VGA is requested, the default is "none". */
Aurelien Jarno3605ded2012-09-10 01:01:44 +02004165 if (default_vga) {
4166 if (cirrus_vga_available()) {
4167 vga_model = "cirrus";
4168 } else if (vga_available()) {
4169 vga_model = "std";
4170 }
Blue Swirla369da52011-09-27 19:15:42 +00004171 }
Paolo Bonzini7f1b17f2012-05-10 09:39:17 +02004172 select_vgahw(vga_model);
Blue Swirla369da52011-09-27 19:15:42 +00004173
Markus Armbruster09aaa162009-08-21 10:31:34 +02004174 if (watchdog) {
4175 i = select_watchdog(watchdog);
4176 if (i > 0)
4177 exit (i == 1 ? 1 : 0);
4178 }
4179
Gerd Hoffmannb6b61142009-07-15 13:48:21 +02004180 if (machine->compat_props) {
Gerd Hoffmann458fb672009-12-08 13:11:33 +01004181 qdev_prop_register_global_list(machine->compat_props);
Gerd Hoffmannb6b61142009-07-15 13:48:21 +02004182 }
Gerd Hoffmannd0fef6f2009-12-08 13:11:34 +01004183 qemu_add_globals();
4184
Anthony Liguori1de81d22011-12-19 16:37:46 -06004185 qdev_machine_init();
4186
Eduardo Habkost5f072e12012-10-15 17:22:02 -03004187 QEMUMachineInitArgs args = { .ram_size = ram_size,
Avik Sile4ada292013-01-08 12:36:30 +05304188 .boot_device = (boot_devices[0] == '\0') ?
4189 machine->boot_order :
4190 boot_devices,
Eduardo Habkost5f072e12012-10-15 17:22:02 -03004191 .kernel_filename = kernel_filename,
4192 .kernel_cmdline = kernel_cmdline,
4193 .initrd_filename = initrd_filename,
4194 .cpu_model = cpu_model };
4195 machine->init(&args);
aliguori3023f3322009-01-16 19:04:14 +00004196
Jan Kiszkaea375f92010-03-01 19:10:30 +01004197 cpu_synchronize_all_post_init();
aliguori268a3622009-04-21 22:30:27 +00004198
Blue Swirl87d0a282010-03-27 18:24:45 +00004199 set_numa_modes();
aliguori268a3622009-04-21 22:30:27 +00004200
aliguori6f338c32009-02-11 15:21:54 +00004201 current_machine = machine;
4202
aliguori3023f3322009-01-16 19:04:14 +00004203 /* init USB devices */
zhlcindy@gmail.com094b2872012-09-02 19:25:28 +00004204 if (usb_enabled(false)) {
Markus Armbruster07527062009-10-06 12:16:57 +01004205 if (foreach_device_config(DEV_USB, usb_parse) < 0)
4206 exit(1);
aliguori3023f3322009-01-16 19:04:14 +00004207 }
4208
Gerd Hoffmannbd3c9482009-07-15 13:59:26 +02004209 /* init generic devices */
Gerd Hoffmann3329f072010-08-20 13:52:01 +02004210 if (qemu_opts_foreach(qemu_find_opts("device"), device_init_func, NULL, 1) != 0)
Gerd Hoffmannbd3c9482009-07-15 13:59:26 +02004211 exit(1);
4212
Markus Armbruster668680f2010-02-11 14:44:58 +01004213 net_check_clients();
4214
aliguori3023f3322009-01-16 19:04:14 +00004215 /* just use the first displaystate for the moment */
Paolo Bonzinib473df62010-02-11 00:29:55 +01004216 ds = get_displaystate();
Anthony Liguori993fbfd2009-05-21 16:54:00 -05004217
Gerd Hoffmanna3e22262010-08-25 15:32:06 +02004218 if (using_spice)
4219 display_remote++;
Gerd Hoffmann6b62dc22010-08-25 10:51:06 +02004220 if (display_type == DT_DEFAULT && !display_remote) {
Anthony Liguorif92f8af2009-05-20 13:01:02 -05004221#if defined(CONFIG_SDL) || defined(CONFIG_COCOA)
Anthony Liguori993fbfd2009-05-21 16:54:00 -05004222 display_type = DT_SDL;
Jes Sorensen821601e2011-03-16 13:33:36 +01004223#elif defined(CONFIG_VNC)
Anthony Liguori993fbfd2009-05-21 16:54:00 -05004224 vnc_display = "localhost:0,to=99";
4225 show_vnc_port = 1;
Jes Sorensen821601e2011-03-16 13:33:36 +01004226#else
4227 display_type = DT_NONE;
Anthony Liguori993fbfd2009-05-21 16:54:00 -05004228#endif
4229 }
Jes Sorensen821601e2011-03-16 13:33:36 +01004230
Anthony Liguori993fbfd2009-05-21 16:54:00 -05004231
Gerd Hoffmann6b62dc22010-08-25 10:51:06 +02004232 /* init local displays */
Anthony Liguori993fbfd2009-05-21 16:54:00 -05004233 switch (display_type) {
4234 case DT_NOGRAPHIC:
4235 break;
4236#if defined(CONFIG_CURSES)
4237 case DT_CURSES:
Michael Tokarevab51b1d2012-12-30 12:48:14 +04004238 curses_display_init(ds, full_screen);
Anthony Liguori993fbfd2009-05-21 16:54:00 -05004239 break;
4240#endif
bellard5b0753e2005-03-01 21:37:28 +00004241#if defined(CONFIG_SDL)
Anthony Liguori993fbfd2009-05-21 16:54:00 -05004242 case DT_SDL:
4243 sdl_display_init(ds, full_screen, no_frame);
4244 break;
bellard5b0753e2005-03-01 21:37:28 +00004245#elif defined(CONFIG_COCOA)
Anthony Liguori993fbfd2009-05-21 16:54:00 -05004246 case DT_SDL:
4247 cocoa_display_init(ds, full_screen);
4248 break;
bellard313aa562003-08-10 21:52:11 +00004249#endif
Gerd Hoffmann6b62dc22010-08-25 10:51:06 +02004250 default:
4251 break;
4252 }
4253
Gleb Natapov0ce235a2011-03-31 11:27:23 +02004254 /* must be after terminal init, SDL library changes signal handlers */
4255 os_setup_signal_handling();
4256
Jes Sorensen821601e2011-03-16 13:33:36 +01004257#ifdef CONFIG_VNC
Gerd Hoffmann6b62dc22010-08-25 10:51:06 +02004258 /* init remote displays */
4259 if (vnc_display) {
Paolo Bonzini2d55f0e2012-10-02 10:17:21 +02004260 Error *local_err = NULL;
Anthony Liguori993fbfd2009-05-21 16:54:00 -05004261 vnc_display_init(ds);
Paolo Bonzini2d55f0e2012-10-02 10:17:21 +02004262 vnc_display_open(ds, vnc_display, &local_err);
4263 if (local_err != NULL) {
4264 fprintf(stderr, "Failed to start VNC server on `%s': %s\n",
4265 vnc_display, error_get_pretty(local_err));
4266 error_free(local_err);
Anthony Liguori993fbfd2009-05-21 16:54:00 -05004267 exit(1);
Amos Kong94b204c2012-06-30 10:02:20 +08004268 }
Anthony Liguorif92f8af2009-05-20 13:01:02 -05004269
Anthony Liguori993fbfd2009-05-21 16:54:00 -05004270 if (show_vnc_port) {
4271 printf("VNC server running on `%s'\n", vnc_display_local_addr(ds));
Anthony Liguorif92f8af2009-05-20 13:01:02 -05004272 }
bellard313aa562003-08-10 21:52:11 +00004273 }
Jes Sorensen821601e2011-03-16 13:33:36 +01004274#endif
Gerd Hoffmanna3e22262010-08-25 15:32:06 +02004275#ifdef CONFIG_SPICE
Gerd Hoffmanna19cbfb2010-04-27 11:50:11 +02004276 if (using_spice && !qxl_enabled) {
Gerd Hoffmanna3e22262010-08-25 15:32:06 +02004277 qemu_spice_display_init(ds);
4278 }
4279#endif
aliguori5b08fc12008-08-21 20:08:03 +00004280
Gerd Hoffmann6b62dc22010-08-25 10:51:06 +02004281 /* display setup */
Paolo Bonzinib473df62010-02-11 00:29:55 +01004282 text_consoles_set_display(ds);
aliguori2796dae2009-01-16 20:23:27 +00004283
Markus Armbrusteref0c4a02012-02-07 15:09:13 +01004284 if (foreach_device_config(DEV_GDB, gdbserver_start) < 0) {
aliguori59030a82009-04-05 18:43:41 +00004285 exit(1);
balrog45669e02007-07-02 13:20:17 +00004286 }
balrog45669e02007-07-02 13:20:17 +00004287
Gerd Hoffmann3418bd22009-09-25 21:42:41 +02004288 qdev_machine_creation_done();
4289
Gerd Hoffmann15ff7702009-12-14 16:07:35 +01004290 if (rom_load_all() != 0) {
4291 fprintf(stderr, "rom loading failed\n");
4292 exit(1);
4293 }
Gerd Hoffmann45a50b12009-10-01 16:42:33 +02004294
Isaku Yamahata80376c32010-12-20 14:33:35 +09004295 /* TODO: once all bus devices are qdevified, this should be done
4296 * when bus is created by qdev.c */
4297 qemu_register_reset(qbus_reset_all_fn, sysbus_get_default());
Gleb Natapov4cab9462010-12-08 13:35:08 +02004298 qemu_run_machine_init_done_notifiers();
4299
Jan Kiszkae063eb12011-06-14 18:29:43 +02004300 qemu_system_reset(VMRESET_SILENT);
Juan Quintela05f24012009-08-20 19:42:22 +02004301 if (loadvm) {
Markus Armbruster03cd4652010-02-17 16:24:10 +01004302 if (load_vmstate(loadvm) < 0) {
Juan Quintela05f24012009-08-20 19:42:22 +02004303 autostart = 0;
4304 }
4305 }
bellardd63d3072004-10-03 13:29:03 +00004306
Glauber Costa2bb8c102009-07-24 16:20:23 -04004307 if (incoming) {
Paolo Bonzini43eaae22012-10-02 18:21:18 +02004308 Error *local_err = NULL;
4309 qemu_start_incoming_migration(incoming, &local_err);
4310 if (local_err) {
4311 fprintf(stderr, "-incoming %s: %s\n", incoming, error_get_pretty(local_err));
4312 error_free(local_err);
4313 exit(1);
Juan Quintela8ca5e802010-06-09 14:10:54 +02004314 }
Avi Kivity6b99dad2009-08-09 14:39:20 +03004315 } else if (autostart) {
aliguoric0f4ce72009-03-05 23:01:01 +00004316 vm_start();
Avi Kivity6b99dad2009-08-09 14:39:20 +03004317 }
thsffd843b2006-12-21 19:46:43 +00004318
Jes Sorenseneb505be2010-06-10 11:42:28 +02004319 os_setup_post();
ths71e3ceb2006-12-22 02:11:31 +00004320
Paolo Bonzini99435902011-09-12 14:03:13 +02004321 resume_all_vcpus();
bellard8a7ddc32004-03-31 19:00:16 +00004322 main_loop();
Paolo Bonzini99435902011-09-12 14:03:13 +02004323 bdrv_close_all();
4324 pause_all_vcpus();
wayne3d3b8302011-07-27 18:04:55 +08004325 res_free();
thsb46a8902007-10-21 23:20:45 +00004326
bellard0824d6f2003-06-24 13:42:40 +00004327 return 0;
4328}