blob: c8e9c782d6ac1457d8e175c61ce61abd00f3da1a [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>
Chegu Vinodee785fe2012-07-16 21:31:30 -070031#include "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
68#include "qemu-seccomp.h"
69#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"
aurel325ef4efa2009-03-10 21:43:35 +0000129#include "bt-host.h"
blueswir1511d2b12009-03-07 15:32:56 +0000130#include "net.h"
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000131#include "net/slirp.h"
blueswir1511d2b12009-03-07 15:32:56 +0000132#include "monitor.h"
133#include "console.h"
134#include "sysemu.h"
135#include "gdbstub.h"
136#include "qemu-timer.h"
137#include "qemu-char.h"
138#include "cache-utils.h"
Markus Armbruster666daa62010-06-02 18:48:27 +0200139#include "blockdev.h"
Markus Armbruster2b584952012-07-10 11:12:50 +0200140#include "hw/block-common.h"
lirans@il.ibm.comc163b5c2009-11-02 15:40:58 +0200141#include "block-migration.h"
blueswir1a718ace2009-03-28 08:24:44 +0000142#include "dma.h"
blueswir1511d2b12009-03-07 15:32:56 +0000143#include "audio/audio.h"
144#include "migration.h"
145#include "kvm.h"
Luiz Capitulinof795e742011-10-21 16:05:43 -0200146#include "qjson.h"
Kevin Wolfd3f24362009-05-18 16:42:09 +0200147#include "qemu-option.h"
Gerd Hoffmann7282a032009-07-31 12:25:35 +0200148#include "qemu-config.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 Bonzini44a9b352011-09-12 16:44:30 +0200151#include "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
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +0200155#include "qtest.h"
blueswir1511d2b12009-03-07 15:32:56 +0000156
bellard0824d6f2003-06-24 13:42:40 +0000157#include "disas.h"
bellardfc01f7e2003-06-30 10:03:06 +0000158
blueswir1511d2b12009-03-07 15:32:56 +0000159#include "qemu_socket.h"
160
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"
Blue Swirl72cf2d42009-09-12 07:36:22 +0000165#include "qemu-queue.h"
Blue Swirl296af7c2010-03-29 19:23:50 +0000166#include "cpus.h"
Blue Swirlad960902010-03-29 19:23:52 +0000167#include "arch_init.h"
Paul Moore0f669982012-08-03 14:39:21 -0400168#include "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
179
Paul Brook5cea8592009-05-30 00:52:44 +0100180static const char *data_dir;
j_mayer1192dad2007-10-05 13:08:35 +0000181const char *bios_name = NULL;
malccb5a7aa2008-09-28 00:42:12 +0000182enum vga_retrace_method vga_retrace_method = VGA_RETRACE_DUMB;
Anthony Liguori993fbfd2009-05-21 16:54:00 -0500183DisplayType display_type = DT_DEFAULT;
Blue Swirl4fdcac02012-10-28 11:04:47 +0000184static int display_remote;
bellard3d11d0e2004-12-12 16:56:30 +0000185const char* keyboard_layout = NULL;
Anthony Liguoric227f092009-10-01 16:12:16 -0500186ram_addr_t ram_size;
Marcelo Tosattic9027602010-03-01 20:25:08 -0300187const char *mem_path = NULL;
188#ifdef MAP_POPULATE
189int mem_prealloc = 0; /* force preallocation of physical target memory */
190#endif
bellardc4b1fcc2004-03-14 21:44:30 +0000191int nb_nics;
bellard7c9d8e02005-11-15 22:16:05 +0000192NICInfo nd_table[MAX_NICS];
Paolo Bonzinid399f672009-07-27 23:17:51 +0200193int autostart;
balrogf6503052008-02-17 11:42:19 +0000194static int rtc_utc = 1;
195static int rtc_date_offset = -1; /* -1 means no change */
Jan Kiszka68752042009-09-15 13:36:04 +0200196QEMUClock *rtc_clock;
Gerd Hoffmann64465292009-12-08 13:11:45 +0100197int vga_interface_type = VGA_NONE;
blueswir1dbed7e42008-10-01 19:38:09 +0000198static int full_screen = 0;
blueswir1634a21f2008-11-16 11:34:07 +0000199#ifdef CONFIG_SDL
blueswir1dbed7e42008-10-01 19:38:09 +0000200static int no_frame = 0;
blueswir1634a21f2008-11-16 11:34:07 +0000201#endif
ths667acca2006-12-11 02:08:05 +0000202int no_quit = 0;
bellard8d11df92004-08-24 21:13:40 +0000203CharDriverState *serial_hds[MAX_SERIAL_PORTS];
bellard6508fe52005-01-15 12:02:56 +0000204CharDriverState *parallel_hds[MAX_PARALLEL_PORTS];
aliguori9ede2fd2009-01-15 20:05:25 +0000205CharDriverState *virtcon_hds[MAX_VIRTIO_CONSOLES];
bellarda09db212005-04-30 16:10:35 +0000206int win2k_install_hack = 0;
aurel321b530a62009-04-05 20:08:59 +0000207int singlestep = 0;
bellard6a00d602005-11-21 23:25:50 +0000208int smp_cpus = 1;
Jes Sorensen6be68d72009-07-23 17:03:42 +0200209int max_cpus = 0;
Andre Przywaradc6b1c02009-08-19 15:42:40 +0200210int smp_cores = 1;
211int smp_threads = 1;
Jes Sorensen821601e2011-03-16 13:33:36 +0100212#ifdef CONFIG_VNC
ths73fc9742006-12-22 02:09:07 +0000213const char *vnc_display;
Jes Sorensen821601e2011-03-16 13:33:36 +0100214#endif
bellard6515b202006-05-03 22:02:44 +0000215int acpi_enabled = 1;
aliguori16b29ae2008-12-17 23:28:44 +0000216int no_hpet = 0;
bellard52ca8d62006-06-14 16:03:05 +0000217int fd_bootchk = 1;
Blue Swirl4fdcac02012-10-28 11:04:47 +0000218static int no_reboot;
aurel32b2f76162008-04-11 21:35:52 +0000219int no_shutdown = 0;
balrog9467cd42007-05-01 01:34:14 +0000220int cursor_hide = 1;
balroga171fe32007-04-30 01:48:07 +0000221int graphic_rotate = 0;
Markus Armbruster09aaa162009-08-21 10:31:34 +0200222const char *watchdog;
Gleb Natapov2e55e842010-12-08 13:35:07 +0200223QEMUOptionRom option_rom[MAX_OPTION_ROMS];
ths9ae02552007-01-05 17:39:04 +0000224int nb_option_roms;
pbrook8e716212007-01-20 17:12:09 +0000225int semihosting_enabled = 0;
balrog2b8f2d42007-07-27 22:08:46 +0000226int old_param = 0;
thsc35734b2007-03-19 15:17:08 +0000227const char *qemu_name;
ths3780e192007-06-21 21:08:02 +0000228int alt_grab = 0;
Dustin Kirkland0ca9f8a2009-09-17 15:48:04 -0500229int ctrl_grab = 0;
blueswir166508602007-05-01 14:16:52 +0000230unsigned int nb_prom_envs = 0;
231const char *prom_envs[MAX_PROM_ENVS];
Jan Kiszka95387492009-07-02 00:19:02 +0200232int boot_menu;
wayne3d3b8302011-07-27 18:04:55 +0800233uint8_t *boot_splash_filedata;
234int boot_splash_filedata_size;
235uint8_t qemu_extra_params_fw[2];
bellard0824d6f2003-06-24 13:42:40 +0000236
Gleb Natapov1ca4d092010-12-08 13:35:05 +0200237typedef struct FWBootEntry FWBootEntry;
238
239struct FWBootEntry {
240 QTAILQ_ENTRY(FWBootEntry) link;
241 int32_t bootindex;
242 DeviceState *dev;
243 char *suffix;
244};
245
Blue Swirl4fdcac02012-10-28 11:04:47 +0000246static QTAILQ_HEAD(, FWBootEntry) fw_boot_order =
247 QTAILQ_HEAD_INITIALIZER(fw_boot_order);
Gleb Natapov1ca4d092010-12-08 13:35:05 +0200248
aliguori268a3622009-04-21 22:30:27 +0000249int nb_numa_nodes;
250uint64_t node_mem[MAX_NODES];
Chegu Vinodee785fe2012-07-16 21:31:30 -0700251unsigned long *node_cpumask[MAX_NODES];
aliguori268a3622009-04-21 22:30:27 +0000252
blueswir18fcb1b92008-09-18 18:29:08 +0000253uint8_t qemu_uuid[16];
254
Jan Kiszka76e30d02009-07-02 00:19:02 +0200255static QEMUBootSetHandler *boot_set_handler;
256static void *boot_set_opaque;
257
Gerd Hoffmannfd42dee2010-06-04 14:08:07 +0200258static NotifierList exit_notifiers =
259 NOTIFIER_LIST_INITIALIZER(exit_notifiers);
260
Gleb Natapov4cab9462010-12-08 13:35:08 +0200261static NotifierList machine_init_done_notifiers =
262 NOTIFIER_LIST_INITIALIZER(machine_init_done_notifiers);
263
Anthony PERARD303d4e82010-09-21 20:05:31 +0100264static int tcg_allowed = 1;
Blue Swirld745bef2010-03-29 19:23:49 +0000265int kvm_allowed = 0;
Anthony PERARD3285cf42010-08-19 12:27:56 +0100266int xen_allowed = 0;
Blue Swirld745bef2010-03-29 19:23:49 +0000267uint32_t xen_domid;
268enum xen_mode xen_mode = XEN_EMULATE;
Jan Kiszkad5ab9712011-08-02 16:10:21 +0200269static int tcg_tb_size;
Blue Swirld745bef2010-03-29 19:23:49 +0000270
Gerd Hoffmann998bbd72009-12-08 13:11:41 +0100271static int default_serial = 1;
Gerd Hoffmann6a5e8b02009-12-08 13:11:42 +0100272static int default_parallel = 1;
Gerd Hoffmann986c5f72009-12-08 13:11:54 +0100273static int default_virtcon = 1;
Gerd Hoffmannabdeed02009-12-08 13:11:43 +0100274static int default_monitor = 1;
Gerd Hoffmannac33f8f2009-12-16 14:25:39 +0100275static int default_floppy = 1;
276static int default_cdrom = 1;
277static int default_sdcard = 1;
Paolo Bonzini7f1b17f2012-05-10 09:39:17 +0200278static int default_vga = 1;
Gerd Hoffmann998bbd72009-12-08 13:11:41 +0100279
280static struct {
281 const char *driver;
282 int *flag;
283} default_list[] = {
Gerd Hoffmann6a5e8b02009-12-08 13:11:42 +0100284 { .driver = "isa-serial", .flag = &default_serial },
285 { .driver = "isa-parallel", .flag = &default_parallel },
Gerd Hoffmannd8bcbab2009-12-16 14:25:40 +0100286 { .driver = "isa-fdc", .flag = &default_floppy },
Markus Armbrusteraf6bf132011-05-18 18:31:02 +0200287 { .driver = "ide-cd", .flag = &default_cdrom },
288 { .driver = "ide-hd", .flag = &default_cdrom },
Gerd Hoffmannd8bcbab2009-12-16 14:25:40 +0100289 { .driver = "ide-drive", .flag = &default_cdrom },
Markus Armbrusteraf6bf132011-05-18 18:31:02 +0200290 { .driver = "scsi-cd", .flag = &default_cdrom },
Amit Shah392ecf52010-01-21 16:19:23 +0530291 { .driver = "virtio-serial-pci", .flag = &default_virtcon },
292 { .driver = "virtio-serial-s390", .flag = &default_virtcon },
293 { .driver = "virtio-serial", .flag = &default_virtcon },
Paolo Bonzini7f1b17f2012-05-10 09:39:17 +0200294 { .driver = "VGA", .flag = &default_vga },
295 { .driver = "isa-vga", .flag = &default_vga },
296 { .driver = "cirrus-vga", .flag = &default_vga },
297 { .driver = "isa-cirrus-vga", .flag = &default_vga },
298 { .driver = "vmware-svga", .flag = &default_vga },
299 { .driver = "qxl-vga", .flag = &default_vga },
Gerd Hoffmann998bbd72009-12-08 13:11:41 +0100300};
301
Ronnie Sahlberg31459f42012-08-06 18:24:55 +1000302const char *qemu_get_vm_name(void)
303{
304 return qemu_name;
305}
306
wayne3d3b8302011-07-27 18:04:55 +0800307static void res_free(void)
308{
309 if (boot_splash_filedata != NULL) {
Anthony Liguori7267c092011-08-20 22:09:37 -0500310 g_free(boot_splash_filedata);
wayne3d3b8302011-07-27 18:04:55 +0800311 boot_splash_filedata = NULL;
312 }
313}
314
Gerd Hoffmann998bbd72009-12-08 13:11:41 +0100315static int default_driver_check(QemuOpts *opts, void *opaque)
316{
317 const char *driver = qemu_opt_get(opts, "driver");
318 int i;
319
320 if (!driver)
321 return 0;
322 for (i = 0; i < ARRAY_SIZE(default_list); i++) {
323 if (strcmp(default_list[i].driver, driver) != 0)
324 continue;
325 *(default_list[i].flag) = 0;
326 }
327 return 0;
328}
329
bellard0824d6f2003-06-24 13:42:40 +0000330/***********************************************************/
Luiz Capitulinof5bbfba2011-07-29 15:04:45 -0300331/* QEMU state */
332
Luiz Capitulino0461d5a2011-09-30 14:45:27 -0300333static RunState current_run_state = RUN_STATE_PRELAUNCH;
Luiz Capitulinof5bbfba2011-07-29 15:04:45 -0300334
Luiz Capitulino5db9d4d2011-09-05 16:36:31 -0300335typedef struct {
336 RunState from;
337 RunState to;
338} RunStateTransition;
339
340static const RunStateTransition runstate_transitions_def[] = {
341 /* from -> to */
Luiz Capitulino0461d5a2011-09-30 14:45:27 -0300342 { RUN_STATE_DEBUG, RUN_STATE_RUNNING },
Luiz Capitulino5db9d4d2011-09-05 16:36:31 -0300343
Luiz Capitulino0461d5a2011-09-30 14:45:27 -0300344 { RUN_STATE_INMIGRATE, RUN_STATE_RUNNING },
Paolo Bonzini29ed72f2012-10-19 16:45:24 +0200345 { RUN_STATE_INMIGRATE, RUN_STATE_PAUSED },
Luiz Capitulino5db9d4d2011-09-05 16:36:31 -0300346
Luiz Capitulino0461d5a2011-09-30 14:45:27 -0300347 { RUN_STATE_INTERNAL_ERROR, RUN_STATE_PAUSED },
Luiz Capitulino8a9236f2011-10-14 11:18:09 -0300348 { RUN_STATE_INTERNAL_ERROR, RUN_STATE_FINISH_MIGRATE },
Luiz Capitulino5db9d4d2011-09-05 16:36:31 -0300349
Luiz Capitulino0461d5a2011-09-30 14:45:27 -0300350 { RUN_STATE_IO_ERROR, RUN_STATE_RUNNING },
Luiz Capitulino8a9236f2011-10-14 11:18:09 -0300351 { RUN_STATE_IO_ERROR, RUN_STATE_FINISH_MIGRATE },
Luiz Capitulino5db9d4d2011-09-05 16:36:31 -0300352
Luiz Capitulino0461d5a2011-09-30 14:45:27 -0300353 { RUN_STATE_PAUSED, RUN_STATE_RUNNING },
Luiz Capitulino8a9236f2011-10-14 11:18:09 -0300354 { RUN_STATE_PAUSED, RUN_STATE_FINISH_MIGRATE },
Luiz Capitulino5db9d4d2011-09-05 16:36:31 -0300355
Luiz Capitulino0461d5a2011-09-30 14:45:27 -0300356 { RUN_STATE_POSTMIGRATE, RUN_STATE_RUNNING },
Luiz Capitulino8a9236f2011-10-14 11:18:09 -0300357 { RUN_STATE_POSTMIGRATE, RUN_STATE_FINISH_MIGRATE },
Luiz Capitulino5db9d4d2011-09-05 16:36:31 -0300358
Luiz Capitulino0461d5a2011-09-30 14:45:27 -0300359 { RUN_STATE_PRELAUNCH, RUN_STATE_RUNNING },
Luiz Capitulino8a9236f2011-10-14 11:18:09 -0300360 { RUN_STATE_PRELAUNCH, RUN_STATE_FINISH_MIGRATE },
Luiz Capitulino0461d5a2011-09-30 14:45:27 -0300361 { RUN_STATE_PRELAUNCH, RUN_STATE_INMIGRATE },
Luiz Capitulino5db9d4d2011-09-05 16:36:31 -0300362
Luiz Capitulino0461d5a2011-09-30 14:45:27 -0300363 { RUN_STATE_FINISH_MIGRATE, RUN_STATE_RUNNING },
364 { RUN_STATE_FINISH_MIGRATE, RUN_STATE_POSTMIGRATE },
Luiz Capitulino5db9d4d2011-09-05 16:36:31 -0300365
Luiz Capitulino0461d5a2011-09-30 14:45:27 -0300366 { RUN_STATE_RESTORE_VM, RUN_STATE_RUNNING },
Luiz Capitulino5db9d4d2011-09-05 16:36:31 -0300367
Luiz Capitulino0461d5a2011-09-30 14:45:27 -0300368 { RUN_STATE_RUNNING, RUN_STATE_DEBUG },
369 { RUN_STATE_RUNNING, RUN_STATE_INTERNAL_ERROR },
370 { RUN_STATE_RUNNING, RUN_STATE_IO_ERROR },
371 { RUN_STATE_RUNNING, RUN_STATE_PAUSED },
372 { RUN_STATE_RUNNING, RUN_STATE_FINISH_MIGRATE },
373 { RUN_STATE_RUNNING, RUN_STATE_RESTORE_VM },
374 { RUN_STATE_RUNNING, RUN_STATE_SAVE_VM },
375 { RUN_STATE_RUNNING, RUN_STATE_SHUTDOWN },
376 { RUN_STATE_RUNNING, RUN_STATE_WATCHDOG },
Luiz Capitulino5db9d4d2011-09-05 16:36:31 -0300377
Luiz Capitulino0461d5a2011-09-30 14:45:27 -0300378 { RUN_STATE_SAVE_VM, RUN_STATE_RUNNING },
Luiz Capitulino5db9d4d2011-09-05 16:36:31 -0300379
Luiz Capitulino0461d5a2011-09-30 14:45:27 -0300380 { RUN_STATE_SHUTDOWN, RUN_STATE_PAUSED },
Luiz Capitulino8a9236f2011-10-14 11:18:09 -0300381 { RUN_STATE_SHUTDOWN, RUN_STATE_FINISH_MIGRATE },
Luiz Capitulino5db9d4d2011-09-05 16:36:31 -0300382
Luiz Capitulinoad02b962012-04-27 13:33:36 -0300383 { RUN_STATE_DEBUG, RUN_STATE_SUSPENDED },
384 { RUN_STATE_RUNNING, RUN_STATE_SUSPENDED },
385 { RUN_STATE_SUSPENDED, RUN_STATE_RUNNING },
386 { RUN_STATE_SUSPENDED, RUN_STATE_FINISH_MIGRATE },
387
Luiz Capitulino0461d5a2011-09-30 14:45:27 -0300388 { RUN_STATE_WATCHDOG, RUN_STATE_RUNNING },
Luiz Capitulino8a9236f2011-10-14 11:18:09 -0300389 { RUN_STATE_WATCHDOG, RUN_STATE_FINISH_MIGRATE },
Luiz Capitulino5db9d4d2011-09-05 16:36:31 -0300390
Luiz Capitulino0461d5a2011-09-30 14:45:27 -0300391 { RUN_STATE_MAX, RUN_STATE_MAX },
Luiz Capitulino5db9d4d2011-09-05 16:36:31 -0300392};
393
Luiz Capitulino0461d5a2011-09-30 14:45:27 -0300394static bool runstate_valid_transitions[RUN_STATE_MAX][RUN_STATE_MAX];
Luiz Capitulino5db9d4d2011-09-05 16:36:31 -0300395
Luiz Capitulinof5bbfba2011-07-29 15:04:45 -0300396bool runstate_check(RunState state)
397{
398 return current_run_state == state;
399}
400
Blue Swirl4fdcac02012-10-28 11:04:47 +0000401static void runstate_init(void)
Luiz Capitulino5db9d4d2011-09-05 16:36:31 -0300402{
403 const RunStateTransition *p;
404
405 memset(&runstate_valid_transitions, 0, sizeof(runstate_valid_transitions));
406
Luiz Capitulino0461d5a2011-09-30 14:45:27 -0300407 for (p = &runstate_transitions_def[0]; p->from != RUN_STATE_MAX; p++) {
Luiz Capitulino5db9d4d2011-09-05 16:36:31 -0300408 runstate_valid_transitions[p->from][p->to] = true;
409 }
410}
411
412/* This function will abort() on invalid state transitions */
Luiz Capitulinof5bbfba2011-07-29 15:04:45 -0300413void runstate_set(RunState new_state)
414{
Luiz Capitulino207c5cd2011-10-13 10:59:07 -0300415 assert(new_state < RUN_STATE_MAX);
416
417 if (!runstate_valid_transitions[current_run_state][new_state]) {
418 fprintf(stderr, "ERROR: invalid runstate transition: '%s' -> '%s'\n",
419 RunState_lookup[current_run_state],
420 RunState_lookup[new_state]);
Luiz Capitulino5db9d4d2011-09-05 16:36:31 -0300421 abort();
422 }
423
Luiz Capitulinof5bbfba2011-07-29 15:04:45 -0300424 current_run_state = new_state;
425}
426
Luiz Capitulino13548692011-07-29 15:36:43 -0300427int runstate_is_running(void)
428{
Luiz Capitulino0461d5a2011-09-30 14:45:27 -0300429 return runstate_check(RUN_STATE_RUNNING);
Luiz Capitulino13548692011-07-29 15:36:43 -0300430}
431
Luiz Capitulino1fa9a5e2011-09-12 17:54:20 -0300432StatusInfo *qmp_query_status(Error **errp)
433{
434 StatusInfo *info = g_malloc0(sizeof(*info));
435
436 info->running = runstate_is_running();
437 info->singlestep = singlestep;
438 info->status = current_run_state;
439
440 return info;
441}
442
Luiz Capitulinof5bbfba2011-07-29 15:04:45 -0300443/***********************************************************/
Paolo Bonzini8f0056b2010-01-13 14:05:34 +0100444/* real time host monotonic timer */
bellard09b26c52006-04-12 21:09:08 +0000445
bellardc4b1fcc2004-03-14 21:44:30 +0000446/***********************************************************/
balrogf6503052008-02-17 11:42:19 +0000447/* host time/date access */
448void qemu_get_timedate(struct tm *tm, int offset)
449{
450 time_t ti;
451 struct tm *ret;
452
453 time(&ti);
454 ti += offset;
455 if (rtc_date_offset == -1) {
456 if (rtc_utc)
457 ret = gmtime(&ti);
458 else
459 ret = localtime(&ti);
460 } else {
461 ti -= rtc_date_offset;
462 ret = gmtime(&ti);
463 }
464
465 memcpy(tm, ret, sizeof(struct tm));
466}
467
468int qemu_timedate_diff(struct tm *tm)
469{
470 time_t seconds;
471
472 if (rtc_date_offset == -1)
473 if (rtc_utc)
474 seconds = mktimegm(tm);
Gleb Natapovf54c5562011-11-06 18:00:22 +0200475 else {
476 struct tm tmp = *tm;
477 tmp.tm_isdst = -1; /* use timezone to figure it out */
478 seconds = mktime(&tmp);
479 }
balrogf6503052008-02-17 11:42:19 +0000480 else
481 seconds = mktimegm(tm) + rtc_date_offset;
482
483 return seconds - time(NULL);
484}
485
Luiz Capitulino80cd3472010-02-25 12:11:44 -0300486void rtc_change_mon_event(struct tm *tm)
487{
488 QObject *data;
489
490 data = qobject_from_jsonf("{ 'offset': %d }", qemu_timedate_diff(tm));
491 monitor_protocol_event(QEVENT_RTC_CHANGE, data);
492 qobject_decref(data);
493}
494
Jan Kiszka1ed2fc12009-09-15 13:36:04 +0200495static void configure_rtc_date_offset(const char *startdate, int legacy)
496{
497 time_t rtc_start_date;
498 struct tm tm;
499
500 if (!strcmp(startdate, "now") && legacy) {
501 rtc_date_offset = -1;
502 } else {
503 if (sscanf(startdate, "%d-%d-%dT%d:%d:%d",
504 &tm.tm_year,
505 &tm.tm_mon,
506 &tm.tm_mday,
507 &tm.tm_hour,
508 &tm.tm_min,
509 &tm.tm_sec) == 6) {
510 /* OK */
511 } else if (sscanf(startdate, "%d-%d-%d",
512 &tm.tm_year,
513 &tm.tm_mon,
514 &tm.tm_mday) == 3) {
515 tm.tm_hour = 0;
516 tm.tm_min = 0;
517 tm.tm_sec = 0;
518 } else {
519 goto date_fail;
520 }
521 tm.tm_year -= 1900;
522 tm.tm_mon--;
523 rtc_start_date = mktimegm(&tm);
524 if (rtc_start_date == -1) {
525 date_fail:
526 fprintf(stderr, "Invalid date format. Valid formats are:\n"
527 "'2006-06-17T16:01:21' or '2006-06-17'\n");
528 exit(1);
529 }
530 rtc_date_offset = time(NULL) - rtc_start_date;
531 }
532}
533
534static void configure_rtc(QemuOpts *opts)
535{
536 const char *value;
537
538 value = qemu_opt_get(opts, "base");
539 if (value) {
540 if (!strcmp(value, "utc")) {
541 rtc_utc = 1;
542 } else if (!strcmp(value, "localtime")) {
543 rtc_utc = 0;
544 } else {
545 configure_rtc_date_offset(value, 0);
546 }
547 }
Jan Kiszka68752042009-09-15 13:36:04 +0200548 value = qemu_opt_get(opts, "clock");
549 if (value) {
550 if (!strcmp(value, "host")) {
551 rtc_clock = host_clock;
Paolo Bonzini78808142012-03-30 10:31:21 +0000552 } else if (!strcmp(value, "rt")) {
553 rtc_clock = rt_clock;
Jan Kiszka68752042009-09-15 13:36:04 +0200554 } else if (!strcmp(value, "vm")) {
555 rtc_clock = vm_clock;
556 } else {
557 fprintf(stderr, "qemu: invalid option value '%s'\n", value);
558 exit(1);
559 }
560 }
Jan Kiszka1ed2fc12009-09-15 13:36:04 +0200561 value = qemu_opt_get(opts, "driftfix");
562 if (value) {
Blue Swirl7e4c0332010-03-27 21:33:46 +0000563 if (!strcmp(value, "slew")) {
Jan Kiszka433acf02012-01-23 20:15:12 +0100564 static GlobalProperty slew_lost_ticks[] = {
565 {
566 .driver = "mc146818rtc",
567 .property = "lost_tick_policy",
568 .value = "slew",
569 },
570 { /* end of list */ }
571 };
572
573 qdev_prop_register_global_list(slew_lost_ticks);
Blue Swirl7e4c0332010-03-27 21:33:46 +0000574 } else if (!strcmp(value, "none")) {
Jan Kiszka433acf02012-01-23 20:15:12 +0100575 /* discard is default */
Jan Kiszka1ed2fc12009-09-15 13:36:04 +0200576 } else {
577 fprintf(stderr, "qemu: invalid option value '%s'\n", value);
578 exit(1);
579 }
580 }
Jan Kiszka1ed2fc12009-09-15 13:36:04 +0200581}
582
balrog1ae26a12008-09-28 23:19:47 +0000583/***********************************************************/
584/* Bluetooth support */
585static int nb_hcis;
586static int cur_hci;
587static struct HCIInfo *hci_table[MAX_NICS];
balrogdc72ac12008-11-09 00:04:26 +0000588
balrog1ae26a12008-09-28 23:19:47 +0000589static struct bt_vlan_s {
590 struct bt_scatternet_s net;
591 int id;
592 struct bt_vlan_s *next;
593} *first_bt_vlan;
594
595/* find or alloc a new bluetooth "VLAN" */
blueswir1674bb262008-09-30 18:18:27 +0000596static struct bt_scatternet_s *qemu_find_bt_vlan(int id)
balrog1ae26a12008-09-28 23:19:47 +0000597{
598 struct bt_vlan_s **pvlan, *vlan;
599 for (vlan = first_bt_vlan; vlan != NULL; vlan = vlan->next) {
600 if (vlan->id == id)
601 return &vlan->net;
602 }
Anthony Liguori7267c092011-08-20 22:09:37 -0500603 vlan = g_malloc0(sizeof(struct bt_vlan_s));
balrog1ae26a12008-09-28 23:19:47 +0000604 vlan->id = id;
605 pvlan = &first_bt_vlan;
606 while (*pvlan != NULL)
607 pvlan = &(*pvlan)->next;
608 *pvlan = vlan;
609 return &vlan->net;
610}
611
612static void null_hci_send(struct HCIInfo *hci, const uint8_t *data, int len)
613{
614}
615
616static int null_hci_addr_set(struct HCIInfo *hci, const uint8_t *bd_addr)
617{
618 return -ENOTSUP;
619}
620
621static struct HCIInfo null_hci = {
622 .cmd_send = null_hci_send,
623 .sco_send = null_hci_send,
624 .acl_send = null_hci_send,
625 .bdaddr_set = null_hci_addr_set,
626};
627
628struct HCIInfo *qemu_next_hci(void)
629{
630 if (cur_hci == nb_hcis)
631 return &null_hci;
632
633 return hci_table[cur_hci++];
634}
635
balrogdc72ac12008-11-09 00:04:26 +0000636static struct HCIInfo *hci_init(const char *str)
637{
638 char *endp;
639 struct bt_scatternet_s *vlan = 0;
640
641 if (!strcmp(str, "null"))
642 /* null */
643 return &null_hci;
644 else if (!strncmp(str, "host", 4) && (str[4] == '\0' || str[4] == ':'))
645 /* host[:hciN] */
646 return bt_host_hci(str[4] ? str + 5 : "hci0");
647 else if (!strncmp(str, "hci", 3)) {
648 /* hci[,vlan=n] */
649 if (str[3]) {
650 if (!strncmp(str + 3, ",vlan=", 6)) {
651 vlan = qemu_find_bt_vlan(strtol(str + 9, &endp, 0));
652 if (*endp)
653 vlan = 0;
654 }
655 } else
656 vlan = qemu_find_bt_vlan(0);
657 if (vlan)
658 return bt_new_hci(vlan);
659 }
660
661 fprintf(stderr, "qemu: Unknown bluetooth HCI `%s'.\n", str);
662
663 return 0;
664}
665
666static int bt_hci_parse(const char *str)
667{
668 struct HCIInfo *hci;
Anthony Liguoric227f092009-10-01 16:12:16 -0500669 bdaddr_t bdaddr;
balrogdc72ac12008-11-09 00:04:26 +0000670
671 if (nb_hcis >= MAX_NICS) {
672 fprintf(stderr, "qemu: Too many bluetooth HCIs (max %i).\n", MAX_NICS);
673 return -1;
674 }
675
676 hci = hci_init(str);
677 if (!hci)
678 return -1;
679
680 bdaddr.b[0] = 0x52;
681 bdaddr.b[1] = 0x54;
682 bdaddr.b[2] = 0x00;
683 bdaddr.b[3] = 0x12;
684 bdaddr.b[4] = 0x34;
685 bdaddr.b[5] = 0x56 + nb_hcis;
686 hci->bdaddr_set(hci, bdaddr.b);
687
688 hci_table[nb_hcis++] = hci;
689
690 return 0;
691}
692
693static void bt_vhci_add(int vlan_id)
694{
695 struct bt_scatternet_s *vlan = qemu_find_bt_vlan(vlan_id);
696
697 if (!vlan->slave)
698 fprintf(stderr, "qemu: warning: adding a VHCI to "
699 "an empty scatternet %i\n", vlan_id);
700
701 bt_vhci_init(bt_new_hci(vlan));
702}
703
704static struct bt_device_s *bt_device_add(const char *opt)
705{
706 struct bt_scatternet_s *vlan;
707 int vlan_id = 0;
708 char *endp = strstr(opt, ",vlan=");
709 int len = (endp ? endp - opt : strlen(opt)) + 1;
710 char devname[10];
711
712 pstrcpy(devname, MIN(sizeof(devname), len), opt);
713
714 if (endp) {
715 vlan_id = strtol(endp + 6, &endp, 0);
716 if (*endp) {
717 fprintf(stderr, "qemu: unrecognised bluetooth vlan Id\n");
718 return 0;
719 }
720 }
721
722 vlan = qemu_find_bt_vlan(vlan_id);
723
724 if (!vlan->slave)
725 fprintf(stderr, "qemu: warning: adding a slave device to "
726 "an empty scatternet %i\n", vlan_id);
727
728 if (!strcmp(devname, "keyboard"))
729 return bt_keyboard_init(vlan);
730
731 fprintf(stderr, "qemu: unsupported bluetooth device `%s'\n", devname);
732 return 0;
733}
734
735static int bt_parse(const char *opt)
736{
737 const char *endp, *p;
738 int vlan;
739
740 if (strstart(opt, "hci", &endp)) {
741 if (!*endp || *endp == ',') {
742 if (*endp)
743 if (!strstart(endp, ",vlan=", 0))
744 opt = endp + 1;
745
746 return bt_hci_parse(opt);
747 }
748 } else if (strstart(opt, "vhci", &endp)) {
749 if (!*endp || *endp == ',') {
750 if (*endp) {
751 if (strstart(endp, ",vlan=", &p)) {
752 vlan = strtol(p, (char **) &endp, 0);
753 if (*endp) {
754 fprintf(stderr, "qemu: bad scatternet '%s'\n", p);
755 return 1;
756 }
757 } else {
758 fprintf(stderr, "qemu: bad parameter '%s'\n", endp + 1);
759 return 1;
760 }
761 } else
762 vlan = 0;
763
764 bt_vhci_add(vlan);
765 return 0;
766 }
767 } else if (strstart(opt, "device:", &endp))
768 return !bt_device_add(endp);
769
770 fprintf(stderr, "qemu: bad bluetooth parameter '%s'\n", opt);
771 return 1;
772}
773
Eduardo Otubo7d76ad42012-08-14 18:44:08 -0300774static int parse_sandbox(QemuOpts *opts, void *opaque)
775{
776 /* FIXME: change this to true for 1.3 */
777 if (qemu_opt_get_bool(opts, "enable", false)) {
778#ifdef CONFIG_SECCOMP
779 if (seccomp_start() < 0) {
780 qerror_report(ERROR_CLASS_GENERIC_ERROR,
781 "failed to install seccomp syscall filter in the kernel");
782 return -1;
783 }
784#else
785 qerror_report(ERROR_CLASS_GENERIC_ERROR,
786 "sandboxing request but seccomp is not compiled into this build");
787 return -1;
788#endif
789 }
790
791 return 0;
792}
793
zhlcindy@gmail.com094b2872012-09-02 19:25:28 +0000794/*********QEMU USB setting******/
795bool usb_enabled(bool default_usb)
796{
797 QemuOpts *mach_opts;
798 mach_opts = qemu_opts_find(qemu_find_opts("machine"), 0);
799 if (mach_opts) {
800 return qemu_opt_get_bool(mach_opts, "usb", default_usb);
801 }
802 return default_usb;
803}
804
Corey Bryant587ed6b2012-10-18 15:19:34 -0400805#ifndef _WIN32
806static int parse_add_fd(QemuOpts *opts, void *opaque)
807{
808 int fd, dupfd, flags;
809 int64_t fdset_id;
810 const char *fd_opaque = NULL;
811
812 fd = qemu_opt_get_number(opts, "fd", -1);
813 fdset_id = qemu_opt_get_number(opts, "set", -1);
814 fd_opaque = qemu_opt_get(opts, "opaque");
815
816 if (fd < 0) {
817 qerror_report(ERROR_CLASS_GENERIC_ERROR,
818 "fd option is required and must be non-negative");
819 return -1;
820 }
821
822 if (fd <= STDERR_FILENO) {
823 qerror_report(ERROR_CLASS_GENERIC_ERROR,
824 "fd cannot be a standard I/O stream");
825 return -1;
826 }
827
828 /*
829 * All fds inherited across exec() necessarily have FD_CLOEXEC
830 * clear, while qemu sets FD_CLOEXEC on all other fds used internally.
831 */
832 flags = fcntl(fd, F_GETFD);
833 if (flags == -1 || (flags & FD_CLOEXEC)) {
834 qerror_report(ERROR_CLASS_GENERIC_ERROR,
835 "fd is not valid or already in use");
836 return -1;
837 }
838
839 if (fdset_id < 0) {
840 qerror_report(ERROR_CLASS_GENERIC_ERROR,
841 "set option is required and must be non-negative");
842 return -1;
843 }
844
845#ifdef F_DUPFD_CLOEXEC
846 dupfd = fcntl(fd, F_DUPFD_CLOEXEC, 0);
847#else
848 dupfd = dup(fd);
849 if (dupfd != -1) {
850 qemu_set_cloexec(dupfd);
851 }
852#endif
853 if (dupfd == -1) {
854 qerror_report(ERROR_CLASS_GENERIC_ERROR,
855 "Error duplicating fd: %s", strerror(errno));
856 return -1;
857 }
858
859 /* add the duplicate fd, and optionally the opaque string, to the fd set */
860 monitor_fdset_add_fd(dupfd, true, fdset_id, fd_opaque ? true : false,
861 fd_opaque, NULL);
862
863 return 0;
864}
865
866static int cleanup_add_fd(QemuOpts *opts, void *opaque)
867{
868 int fd;
869
870 fd = qemu_opt_get_number(opts, "fd", -1);
871 close(fd);
872
873 return 0;
874}
875#endif
876
balrog1ae26a12008-09-28 23:19:47 +0000877/***********************************************************/
878/* QEMU Block devices */
879
Markus Armbruster2292dda2011-01-28 11:21:41 +0100880#define HD_OPTS "media=disk"
881#define CDROM_OPTS "media=cdrom"
882#define FD_OPTS ""
883#define PFLASH_OPTS ""
884#define MTD_OPTS ""
885#define SD_OPTS ""
thse4bcb142007-12-02 04:51:10 +0000886
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +0200887static int drive_init_func(QemuOpts *opts, void *opaque)
888{
Markus Armbrustera803cb82010-06-02 13:31:55 +0200889 int *use_scsi = opaque;
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +0200890
Markus Armbruster319ae522011-01-28 11:21:46 +0100891 return drive_init(opts, *use_scsi) == NULL;
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +0200892}
893
894static int drive_enable_snapshot(QemuOpts *opts, void *opaque)
895{
896 if (NULL == qemu_opt_get(opts, "snapshot")) {
897 qemu_opt_set(opts, "snapshot", "on");
898 }
899 return 0;
900}
901
Markus Armbruster4e5d9b52011-01-28 11:21:45 +0100902static void default_drive(int enable, int snapshot, int use_scsi,
903 BlockInterfaceType type, int index,
904 const char *optstr)
905{
906 QemuOpts *opts;
907
908 if (type == IF_DEFAULT) {
909 type = use_scsi ? IF_SCSI : IF_IDE;
910 }
911
912 if (!enable || drive_get_by_index(type, index)) {
913 return;
914 }
915
916 opts = drive_add(type, index, NULL, optstr);
917 if (snapshot) {
918 drive_enable_snapshot(opts, NULL);
919 }
Markus Armbruster319ae522011-01-28 11:21:46 +0100920 if (!drive_init(opts, use_scsi)) {
Markus Armbruster4e5d9b52011-01-28 11:21:45 +0100921 exit(1);
922 }
923}
924
Jan Kiszka76e30d02009-07-02 00:19:02 +0200925void qemu_register_boot_set(QEMUBootSetHandler *func, void *opaque)
926{
927 boot_set_handler = func;
928 boot_set_opaque = opaque;
929}
930
931int qemu_boot_set(const char *boot_devices)
932{
933 if (!boot_set_handler) {
934 return -EINVAL;
935 }
936 return boot_set_handler(boot_set_opaque, boot_devices);
937}
938
Eduardo Habkost4e9e9d62010-04-06 19:22:08 -0300939static void validate_bootdevices(char *devices)
Jan Kiszkaef3adf62009-07-02 00:19:02 +0200940{
941 /* We just do some generic consistency checks */
942 const char *p;
943 int bitmap = 0;
944
945 for (p = devices; *p != '\0'; p++) {
946 /* Allowed boot devices are:
947 * a-b: floppy disk drives
948 * c-f: IDE disk drives
Dong Xu Wang07f35072011-11-22 18:06:26 +0800949 * g-m: machine implementation dependent drives
Jan Kiszkaef3adf62009-07-02 00:19:02 +0200950 * n-p: network devices
951 * It's up to each machine implementation to check if the given boot
952 * devices match the actual hardware implementation and firmware
953 * features.
954 */
955 if (*p < 'a' || *p > 'p') {
956 fprintf(stderr, "Invalid boot device '%c'\n", *p);
957 exit(1);
958 }
959 if (bitmap & (1 << (*p - 'a'))) {
960 fprintf(stderr, "Boot device '%c' was given twice\n", *p);
961 exit(1);
962 }
963 bitmap |= 1 << (*p - 'a');
964 }
Jan Kiszkaef3adf62009-07-02 00:19:02 +0200965}
966
Jan Kiszkae0f084b2009-07-02 00:19:02 +0200967static void restore_boot_devices(void *opaque)
968{
969 char *standard_boot_devices = opaque;
Alex Williamson37905d62010-04-30 15:21:11 -0600970 static int first = 1;
971
972 /* Restore boot order and remove ourselves after the first boot */
973 if (first) {
974 first = 0;
975 return;
976 }
Jan Kiszkae0f084b2009-07-02 00:19:02 +0200977
978 qemu_boot_set(standard_boot_devices);
979
980 qemu_unregister_reset(restore_boot_devices, standard_boot_devices);
Anthony Liguori7267c092011-08-20 22:09:37 -0500981 g_free(standard_boot_devices);
Jan Kiszkae0f084b2009-07-02 00:19:02 +0200982}
983
Gleb Natapov1ca4d092010-12-08 13:35:05 +0200984void add_boot_device_path(int32_t bootindex, DeviceState *dev,
985 const char *suffix)
986{
987 FWBootEntry *node, *i;
988
989 if (bootindex < 0) {
990 return;
991 }
992
993 assert(dev != NULL || suffix != NULL);
994
Anthony Liguori7267c092011-08-20 22:09:37 -0500995 node = g_malloc0(sizeof(FWBootEntry));
Gleb Natapov1ca4d092010-12-08 13:35:05 +0200996 node->bootindex = bootindex;
Anthony Liguori7267c092011-08-20 22:09:37 -0500997 node->suffix = suffix ? g_strdup(suffix) : NULL;
Gleb Natapov1ca4d092010-12-08 13:35:05 +0200998 node->dev = dev;
999
1000 QTAILQ_FOREACH(i, &fw_boot_order, link) {
1001 if (i->bootindex == bootindex) {
1002 fprintf(stderr, "Two devices with same boot index %d\n", bootindex);
1003 exit(1);
1004 } else if (i->bootindex < bootindex) {
1005 continue;
1006 }
1007 QTAILQ_INSERT_BEFORE(i, node, link);
1008 return;
1009 }
1010 QTAILQ_INSERT_TAIL(&fw_boot_order, node, link);
1011}
1012
Gleb Natapov962630f2010-12-08 13:35:09 +02001013/*
1014 * This function returns null terminated string that consist of new line
Brad Hards71785ab2011-04-23 21:50:06 +10001015 * separated device paths.
Gleb Natapov962630f2010-12-08 13:35:09 +02001016 *
1017 * memory pointed by "size" is assigned total length of the array in bytes
1018 *
1019 */
1020char *get_boot_devices_list(uint32_t *size)
1021{
1022 FWBootEntry *i;
1023 uint32_t total = 0;
1024 char *list = NULL;
1025
1026 QTAILQ_FOREACH(i, &fw_boot_order, link) {
1027 char *devpath = NULL, *bootpath;
1028 int len;
1029
1030 if (i->dev) {
1031 devpath = qdev_get_fw_dev_path(i->dev);
1032 assert(devpath);
1033 }
1034
1035 if (i->suffix && devpath) {
Blue Swirl4fd37a92010-12-19 14:05:43 +00001036 size_t bootpathlen = strlen(devpath) + strlen(i->suffix) + 1;
1037
Anthony Liguori7267c092011-08-20 22:09:37 -05001038 bootpath = g_malloc(bootpathlen);
Blue Swirl4fd37a92010-12-19 14:05:43 +00001039 snprintf(bootpath, bootpathlen, "%s%s", devpath, i->suffix);
Anthony Liguori7267c092011-08-20 22:09:37 -05001040 g_free(devpath);
Gleb Natapov962630f2010-12-08 13:35:09 +02001041 } else if (devpath) {
1042 bootpath = devpath;
1043 } else {
Markus Armbruster1bf6ccd2011-11-08 10:58:00 +01001044 assert(i->suffix);
Anthony Liguori7267c092011-08-20 22:09:37 -05001045 bootpath = g_strdup(i->suffix);
Gleb Natapov962630f2010-12-08 13:35:09 +02001046 }
1047
1048 if (total) {
1049 list[total-1] = '\n';
1050 }
1051 len = strlen(bootpath) + 1;
Anthony Liguori7267c092011-08-20 22:09:37 -05001052 list = g_realloc(list, total + len);
Gleb Natapov962630f2010-12-08 13:35:09 +02001053 memcpy(&list[total], bootpath, len);
1054 total += len;
Anthony Liguori7267c092011-08-20 22:09:37 -05001055 g_free(bootpath);
Gleb Natapov962630f2010-12-08 13:35:09 +02001056 }
1057
1058 *size = total;
1059
1060 return list;
1061}
1062
aliguori268a3622009-04-21 22:30:27 +00001063static void numa_add(const char *optarg)
1064{
1065 char option[128];
1066 char *endptr;
1067 unsigned long long value, endvalue;
1068 int nodenr;
1069
Chegu Vinodee785fe2012-07-16 21:31:30 -07001070 value = endvalue = 0ULL;
1071
aliguori268a3622009-04-21 22:30:27 +00001072 optarg = get_opt_name(option, 128, optarg, ',') + 1;
1073 if (!strcmp(option, "node")) {
1074 if (get_param_value(option, 128, "nodeid", optarg) == 0) {
1075 nodenr = nb_numa_nodes;
1076 } else {
1077 nodenr = strtoull(option, NULL, 10);
1078 }
1079
1080 if (get_param_value(option, 128, "mem", optarg) == 0) {
1081 node_mem[nodenr] = 0;
1082 } else {
Jes Sorensen70b4f4b2011-01-05 11:41:02 +01001083 int64_t sval;
Markus Armbrusterc03417b2011-11-22 09:46:02 +01001084 sval = strtosz(option, &endptr);
1085 if (sval < 0 || *endptr) {
Jes Sorensen9f9b17a2010-10-21 17:15:46 +02001086 fprintf(stderr, "qemu: invalid numa mem size: %s\n", optarg);
1087 exit(1);
aliguori268a3622009-04-21 22:30:27 +00001088 }
Jes Sorensen9f9b17a2010-10-21 17:15:46 +02001089 node_mem[nodenr] = sval;
aliguori268a3622009-04-21 22:30:27 +00001090 }
Chegu Vinodee785fe2012-07-16 21:31:30 -07001091 if (get_param_value(option, 128, "cpus", optarg) != 0) {
aliguori268a3622009-04-21 22:30:27 +00001092 value = strtoull(option, &endptr, 10);
Chegu Vinodee785fe2012-07-16 21:31:30 -07001093 if (*endptr == '-') {
1094 endvalue = strtoull(endptr+1, &endptr, 10);
aliguori268a3622009-04-21 22:30:27 +00001095 } else {
Chegu Vinodee785fe2012-07-16 21:31:30 -07001096 endvalue = value;
aliguori268a3622009-04-21 22:30:27 +00001097 }
Chegu Vinodee785fe2012-07-16 21:31:30 -07001098
1099 if (!(endvalue < MAX_CPUMASK_BITS)) {
1100 endvalue = MAX_CPUMASK_BITS - 1;
1101 fprintf(stderr,
1102 "A max of %d CPUs are supported in a guest\n",
1103 MAX_CPUMASK_BITS);
1104 }
1105
1106 bitmap_set(node_cpumask[nodenr], value, endvalue-value+1);
aliguori268a3622009-04-21 22:30:27 +00001107 }
1108 nb_numa_nodes++;
1109 }
aliguori268a3622009-04-21 22:30:27 +00001110}
1111
Andre Przywaradc6b1c02009-08-19 15:42:40 +02001112static void smp_parse(const char *optarg)
1113{
1114 int smp, sockets = 0, threads = 0, cores = 0;
1115 char *endptr;
1116 char option[128];
1117
1118 smp = strtoul(optarg, &endptr, 10);
1119 if (endptr != optarg) {
1120 if (*endptr == ',') {
1121 endptr++;
1122 }
1123 }
1124 if (get_param_value(option, 128, "sockets", endptr) != 0)
1125 sockets = strtoull(option, NULL, 10);
1126 if (get_param_value(option, 128, "cores", endptr) != 0)
1127 cores = strtoull(option, NULL, 10);
1128 if (get_param_value(option, 128, "threads", endptr) != 0)
1129 threads = strtoull(option, NULL, 10);
1130 if (get_param_value(option, 128, "maxcpus", endptr) != 0)
1131 max_cpus = strtoull(option, NULL, 10);
1132
1133 /* compute missing values, prefer sockets over cores over threads */
1134 if (smp == 0 || sockets == 0) {
1135 sockets = sockets > 0 ? sockets : 1;
1136 cores = cores > 0 ? cores : 1;
1137 threads = threads > 0 ? threads : 1;
1138 if (smp == 0) {
1139 smp = cores * threads * sockets;
Andre Przywaradc6b1c02009-08-19 15:42:40 +02001140 }
1141 } else {
1142 if (cores == 0) {
1143 threads = threads > 0 ? threads : 1;
1144 cores = smp / (sockets * threads);
1145 } else {
Joel Schoppdca98162010-07-21 15:05:17 -05001146 threads = smp / (cores * sockets);
Andre Przywaradc6b1c02009-08-19 15:42:40 +02001147 }
1148 }
1149 smp_cpus = smp;
1150 smp_cores = cores > 0 ? cores : 1;
1151 smp_threads = threads > 0 ? threads : 1;
1152 if (max_cpus == 0)
1153 max_cpus = smp_cpus;
1154}
1155
bellard330d0412003-07-26 18:11:40 +00001156/***********************************************************/
bellarda594cfb2005-11-06 16:13:29 +00001157/* USB devices */
1158
Markus Armbrusterfb080002010-05-28 15:38:44 +02001159static int usb_device_add(const char *devname)
bellarda594cfb2005-11-06 16:13:29 +00001160{
1161 const char *p;
Gerd Hoffmanna5d2f722009-08-31 14:24:00 +02001162 USBDevice *dev = NULL;
bellarda594cfb2005-11-06 16:13:29 +00001163
zhlcindy@gmail.com094b2872012-09-02 19:25:28 +00001164 if (!usb_enabled(false)) {
bellarda594cfb2005-11-06 16:13:29 +00001165 return -1;
zhlcindy@gmail.com094b2872012-09-02 19:25:28 +00001166 }
bellarda594cfb2005-11-06 16:13:29 +00001167
Gerd Hoffmann0958b4c2009-10-26 15:56:45 +01001168 /* drivers with .usbdevice_name entry in USBDeviceInfo */
1169 dev = usbdevice_create(devname);
1170 if (dev)
1171 goto done;
1172
Gerd Hoffmanna5d2f722009-08-31 14:24:00 +02001173 /* the other ones */
Gerd Hoffmanne447fc62011-06-01 14:41:59 +02001174#ifndef CONFIG_LINUX
1175 /* only the linux version is qdev-ified, usb-bsd still needs this */
bellarda594cfb2005-11-06 16:13:29 +00001176 if (strstart(devname, "host:", &p)) {
Jan Kiszka37417152012-02-27 15:18:47 +01001177 dev = usb_host_device_open(usb_bus_find(-1), p);
Gerd Hoffmanne447fc62011-06-01 14:41:59 +02001178 } else
1179#endif
1180 if (!strcmp(devname, "bt") || strstart(devname, "bt:", &p)) {
Jan Kiszka37417152012-02-27 15:18:47 +01001181 dev = usb_bt_init(usb_bus_find(-1),
1182 devname[2] ? hci_init(p)
1183 : bt_new_hci(qemu_find_bt_vlan(0)));
bellarda594cfb2005-11-06 16:13:29 +00001184 } else {
1185 return -1;
1186 }
pbrook0d92ed32006-05-21 16:30:15 +00001187 if (!dev)
1188 return -1;
1189
Gerd Hoffmanna5d2f722009-08-31 14:24:00 +02001190done:
bellarda594cfb2005-11-06 16:13:29 +00001191 return 0;
1192}
1193
aliguori1f3870a2008-08-21 19:27:48 +00001194static int usb_device_del(const char *devname)
1195{
1196 int bus_num, addr;
1197 const char *p;
1198
aliguori5d0c5752008-09-14 01:07:41 +00001199 if (strstart(devname, "host:", &p))
1200 return usb_host_device_close(p);
1201
zhlcindy@gmail.com094b2872012-09-02 19:25:28 +00001202 if (!usb_enabled(false)) {
aliguori1f3870a2008-08-21 19:27:48 +00001203 return -1;
zhlcindy@gmail.com094b2872012-09-02 19:25:28 +00001204 }
aliguori1f3870a2008-08-21 19:27:48 +00001205
1206 p = strchr(devname, '.');
1207 if (!p)
1208 return -1;
1209 bus_num = strtoul(devname, NULL, 0);
1210 addr = strtoul(p + 1, NULL, 0);
1211
Gerd Hoffmanna5d2f722009-08-31 14:24:00 +02001212 return usb_device_delete_addr(bus_num, addr);
aliguori1f3870a2008-08-21 19:27:48 +00001213}
1214
Gerd Hoffmannbd3c9482009-07-15 13:59:26 +02001215static int usb_parse(const char *cmdline)
1216{
Scott Tsai59d1c1c2009-12-23 04:30:18 +08001217 int r;
Markus Armbrusterfb080002010-05-28 15:38:44 +02001218 r = usb_device_add(cmdline);
Scott Tsai59d1c1c2009-12-23 04:30:18 +08001219 if (r < 0) {
1220 fprintf(stderr, "qemu: could not add USB device '%s'\n", cmdline);
1221 }
1222 return r;
Gerd Hoffmannbd3c9482009-07-15 13:59:26 +02001223}
1224
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001225void do_usb_add(Monitor *mon, const QDict *qdict)
bellarda594cfb2005-11-06 16:13:29 +00001226{
Scott Tsai59d1c1c2009-12-23 04:30:18 +08001227 const char *devname = qdict_get_str(qdict, "devname");
Markus Armbrusterfb080002010-05-28 15:38:44 +02001228 if (usb_device_add(devname) < 0) {
Markus Armbruster1ecda022010-02-18 17:25:24 +01001229 error_report("could not add USB device '%s'", devname);
Scott Tsai59d1c1c2009-12-23 04:30:18 +08001230 }
bellarda594cfb2005-11-06 16:13:29 +00001231}
1232
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001233void do_usb_del(Monitor *mon, const QDict *qdict)
bellarda594cfb2005-11-06 16:13:29 +00001234{
Scott Tsai59d1c1c2009-12-23 04:30:18 +08001235 const char *devname = qdict_get_str(qdict, "devname");
1236 if (usb_device_del(devname) < 0) {
Markus Armbruster1ecda022010-02-18 17:25:24 +01001237 error_report("could not delete USB device '%s'", devname);
Scott Tsai59d1c1c2009-12-23 04:30:18 +08001238 }
bellarda594cfb2005-11-06 16:13:29 +00001239}
1240
bellardf7cce892004-12-08 22:21:25 +00001241/***********************************************************/
balrog201a51f2007-04-30 00:51:09 +00001242/* PCMCIA/Cardbus */
1243
1244static struct pcmcia_socket_entry_s {
Paul Brookbc24a222009-05-10 01:44:56 +01001245 PCMCIASocket *socket;
balrog201a51f2007-04-30 00:51:09 +00001246 struct pcmcia_socket_entry_s *next;
1247} *pcmcia_sockets = 0;
1248
Paul Brookbc24a222009-05-10 01:44:56 +01001249void pcmcia_socket_register(PCMCIASocket *socket)
balrog201a51f2007-04-30 00:51:09 +00001250{
1251 struct pcmcia_socket_entry_s *entry;
1252
Anthony Liguori7267c092011-08-20 22:09:37 -05001253 entry = g_malloc(sizeof(struct pcmcia_socket_entry_s));
balrog201a51f2007-04-30 00:51:09 +00001254 entry->socket = socket;
1255 entry->next = pcmcia_sockets;
1256 pcmcia_sockets = entry;
1257}
1258
Paul Brookbc24a222009-05-10 01:44:56 +01001259void pcmcia_socket_unregister(PCMCIASocket *socket)
balrog201a51f2007-04-30 00:51:09 +00001260{
1261 struct pcmcia_socket_entry_s *entry, **ptr;
1262
1263 ptr = &pcmcia_sockets;
1264 for (entry = *ptr; entry; ptr = &entry->next, entry = *ptr)
1265 if (entry->socket == socket) {
1266 *ptr = entry->next;
Anthony Liguori7267c092011-08-20 22:09:37 -05001267 g_free(entry);
balrog201a51f2007-04-30 00:51:09 +00001268 }
1269}
1270
aliguori376253e2009-03-05 23:01:23 +00001271void pcmcia_info(Monitor *mon)
balrog201a51f2007-04-30 00:51:09 +00001272{
1273 struct pcmcia_socket_entry_s *iter;
aliguori376253e2009-03-05 23:01:23 +00001274
balrog201a51f2007-04-30 00:51:09 +00001275 if (!pcmcia_sockets)
aliguori376253e2009-03-05 23:01:23 +00001276 monitor_printf(mon, "No PCMCIA sockets\n");
balrog201a51f2007-04-30 00:51:09 +00001277
1278 for (iter = pcmcia_sockets; iter; iter = iter->next)
aliguori376253e2009-03-05 23:01:23 +00001279 monitor_printf(mon, "%s: %s\n", iter->socket->slot_string,
1280 iter->socket->attached ? iter->socket->card_string :
1281 "Empty");
balrog201a51f2007-04-30 00:51:09 +00001282}
1283
1284/***********************************************************/
bellardcc1daa42005-06-05 14:49:17 +00001285/* machine registration */
1286
blueswir1bdaf78e2008-10-04 07:24:27 +00001287static QEMUMachine *first_machine = NULL;
aliguori6f338c32009-02-11 15:21:54 +00001288QEMUMachine *current_machine = NULL;
bellardcc1daa42005-06-05 14:49:17 +00001289
1290int qemu_register_machine(QEMUMachine *m)
1291{
1292 QEMUMachine **pm;
1293 pm = &first_machine;
1294 while (*pm != NULL)
1295 pm = &(*pm)->next;
1296 m->next = NULL;
1297 *pm = m;
1298 return 0;
1299}
1300
pbrook9596ebb2007-11-18 01:44:38 +00001301static QEMUMachine *find_machine(const char *name)
bellardcc1daa42005-06-05 14:49:17 +00001302{
1303 QEMUMachine *m;
1304
1305 for(m = first_machine; m != NULL; m = m->next) {
1306 if (!strcmp(m->name, name))
1307 return m;
Mark McLoughlin3f6599e2009-07-22 10:02:50 +01001308 if (m->alias && !strcmp(m->alias, name))
1309 return m;
bellardcc1daa42005-06-05 14:49:17 +00001310 }
1311 return NULL;
1312}
1313
Jordan Justen2c8cffa2012-02-21 23:18:50 -08001314QEMUMachine *find_default_machine(void)
Anthony Liguori0c257432009-05-21 20:41:01 -05001315{
1316 QEMUMachine *m;
1317
1318 for(m = first_machine; m != NULL; m = m->next) {
1319 if (m->is_default) {
1320 return m;
1321 }
1322 }
1323 return NULL;
1324}
1325
Anthony Liguori01d3c802012-08-10 11:04:11 -05001326MachineInfoList *qmp_query_machines(Error **errp)
1327{
1328 MachineInfoList *mach_list = NULL;
1329 QEMUMachine *m;
1330
1331 for (m = first_machine; m; m = m->next) {
1332 MachineInfoList *entry;
1333 MachineInfo *info;
1334
1335 info = g_malloc0(sizeof(*info));
1336 if (m->is_default) {
1337 info->has_is_default = true;
1338 info->is_default = true;
1339 }
1340
1341 if (m->alias) {
1342 info->has_alias = true;
1343 info->alias = g_strdup(m->alias);
1344 }
1345
1346 info->name = g_strdup(m->name);
1347
1348 entry = g_malloc0(sizeof(*entry));
1349 entry->value = info;
1350 entry->next = mach_list;
1351 mach_list = entry;
1352 }
1353
1354 return mach_list;
1355}
1356
bellardcc1daa42005-06-05 14:49:17 +00001357/***********************************************************/
bellard8a7ddc32004-03-31 19:00:16 +00001358/* main execution loop */
1359
pbrook9596ebb2007-11-18 01:44:38 +00001360static void gui_update(void *opaque)
bellard8a7ddc32004-03-31 19:00:16 +00001361{
aliguori7d957bd2009-01-15 22:14:11 +00001362 uint64_t interval = GUI_REFRESH_INTERVAL;
ths740733b2007-06-08 01:57:56 +00001363 DisplayState *ds = opaque;
Gerd Hoffmann87e487a2010-06-04 11:46:35 +02001364 DisplayChangeListener *dcl;
aliguori7d957bd2009-01-15 22:14:11 +00001365
1366 dpy_refresh(ds);
1367
Gerd Hoffmann87e487a2010-06-04 11:46:35 +02001368 QLIST_FOREACH(dcl, &ds->listeners, next) {
aliguori7d957bd2009-01-15 22:14:11 +00001369 if (dcl->gui_timer_interval &&
1370 dcl->gui_timer_interval < interval)
1371 interval = dcl->gui_timer_interval;
aliguori7d957bd2009-01-15 22:14:11 +00001372 }
Paolo Bonzini7bd427d2011-03-11 16:47:48 +01001373 qemu_mod_timer(ds->gui_timer, interval + qemu_get_clock_ms(rt_clock));
bellard8a7ddc32004-03-31 19:00:16 +00001374}
1375
Gerd Hoffmann35c9e0a2010-06-04 11:51:31 +02001376void gui_setup_refresh(DisplayState *ds)
1377{
1378 DisplayChangeListener *dcl;
1379 bool need_timer = false;
Gerd Hoffmanna93a4a22012-09-28 15:02:08 +02001380 bool have_gfx = false;
1381 bool have_text = false;
Gerd Hoffmann35c9e0a2010-06-04 11:51:31 +02001382
1383 QLIST_FOREACH(dcl, &ds->listeners, next) {
1384 if (dcl->dpy_refresh != NULL) {
1385 need_timer = true;
Gerd Hoffmanna93a4a22012-09-28 15:02:08 +02001386 }
1387 if (dcl->dpy_gfx_update != NULL) {
1388 have_gfx = true;
1389 }
1390 if (dcl->dpy_text_update != NULL) {
1391 have_text = true;
Gerd Hoffmann35c9e0a2010-06-04 11:51:31 +02001392 }
1393 }
1394
1395 if (need_timer && ds->gui_timer == NULL) {
1396 ds->gui_timer = qemu_new_timer_ms(rt_clock, gui_update, ds);
1397 qemu_mod_timer(ds->gui_timer, qemu_get_clock_ms(rt_clock));
1398 }
1399 if (!need_timer && ds->gui_timer != NULL) {
1400 qemu_del_timer(ds->gui_timer);
1401 qemu_free_timer(ds->gui_timer);
1402 ds->gui_timer = NULL;
1403 }
Gerd Hoffmanna93a4a22012-09-28 15:02:08 +02001404
1405 ds->have_gfx = have_gfx;
1406 ds->have_text = have_text;
Gerd Hoffmann35c9e0a2010-06-04 11:51:31 +02001407}
1408
bellard0bd48852005-11-11 00:00:47 +00001409struct vm_change_state_entry {
1410 VMChangeStateHandler *cb;
1411 void *opaque;
Blue Swirl72cf2d42009-09-12 07:36:22 +00001412 QLIST_ENTRY (vm_change_state_entry) entries;
bellard0bd48852005-11-11 00:00:47 +00001413};
1414
Blue Swirl72cf2d42009-09-12 07:36:22 +00001415static QLIST_HEAD(vm_change_state_head, vm_change_state_entry) vm_change_state_head;
bellard0bd48852005-11-11 00:00:47 +00001416
1417VMChangeStateEntry *qemu_add_vm_change_state_handler(VMChangeStateHandler *cb,
1418 void *opaque)
1419{
1420 VMChangeStateEntry *e;
1421
Anthony Liguori7267c092011-08-20 22:09:37 -05001422 e = g_malloc0(sizeof (*e));
bellard0bd48852005-11-11 00:00:47 +00001423
1424 e->cb = cb;
1425 e->opaque = opaque;
Blue Swirl72cf2d42009-09-12 07:36:22 +00001426 QLIST_INSERT_HEAD(&vm_change_state_head, e, entries);
bellard0bd48852005-11-11 00:00:47 +00001427 return e;
1428}
1429
1430void qemu_del_vm_change_state_handler(VMChangeStateEntry *e)
1431{
Blue Swirl72cf2d42009-09-12 07:36:22 +00001432 QLIST_REMOVE (e, entries);
Anthony Liguori7267c092011-08-20 22:09:37 -05001433 g_free (e);
bellard0bd48852005-11-11 00:00:47 +00001434}
1435
Luiz Capitulino1dfb4dd2011-07-29 14:26:33 -03001436void vm_state_notify(int running, RunState state)
bellard0bd48852005-11-11 00:00:47 +00001437{
1438 VMChangeStateEntry *e;
1439
Luiz Capitulino1dfb4dd2011-07-29 14:26:33 -03001440 trace_vm_state_notify(running, state);
Stefan Hajnoczi94b0b5f2010-11-16 12:20:25 +00001441
bellard0bd48852005-11-11 00:00:47 +00001442 for (e = vm_change_state_head.lh_first; e; e = e->entries.le_next) {
Luiz Capitulino1dfb4dd2011-07-29 14:26:33 -03001443 e->cb(e->opaque, running, state);
bellard0bd48852005-11-11 00:00:47 +00001444 }
1445}
1446
bellard8a7ddc32004-03-31 19:00:16 +00001447void vm_start(void)
1448{
Luiz Capitulino13548692011-07-29 15:36:43 -03001449 if (!runstate_is_running()) {
bellard8a7ddc32004-03-31 19:00:16 +00001450 cpu_enable_ticks();
Luiz Capitulino0461d5a2011-09-30 14:45:27 -03001451 runstate_set(RUN_STATE_RUNNING);
1452 vm_state_notify(1, RUN_STATE_RUNNING);
aliguorid6dc3d42009-04-24 18:04:07 +00001453 resume_all_vcpus();
Luiz Capitulino6ed2c482010-04-27 20:35:59 -03001454 monitor_protocol_event(QEVENT_RESUME, NULL);
bellard8a7ddc32004-03-31 19:00:16 +00001455 }
1456}
1457
bellardbb0c6722004-06-20 12:37:32 +00001458/* reset/shutdown handler */
1459
1460typedef struct QEMUResetEntry {
Blue Swirl72cf2d42009-09-12 07:36:22 +00001461 QTAILQ_ENTRY(QEMUResetEntry) entry;
bellardbb0c6722004-06-20 12:37:32 +00001462 QEMUResetHandler *func;
1463 void *opaque;
bellardbb0c6722004-06-20 12:37:32 +00001464} QEMUResetEntry;
1465
Blue Swirl72cf2d42009-09-12 07:36:22 +00001466static QTAILQ_HEAD(reset_handlers, QEMUResetEntry) reset_handlers =
1467 QTAILQ_HEAD_INITIALIZER(reset_handlers);
bellardbb0c6722004-06-20 12:37:32 +00001468static int reset_requested;
Gleb Natapovf64622c2011-03-15 13:56:04 +02001469static int shutdown_requested, shutdown_signal = -1;
1470static pid_t shutdown_pid;
bellard34751872005-07-02 14:31:34 +00001471static int powerdown_requested;
Jan Kiszka8cf71712011-02-07 12:19:16 +01001472static int debug_requested;
Gerd Hoffmann95b363b2012-02-23 13:45:19 +01001473static int suspend_requested;
Luiz Capitulino14058192012-08-08 17:29:17 -03001474static int wakeup_requested;
Igor Mammedova9552c82012-09-05 23:06:21 +02001475static NotifierList powerdown_notifiers =
1476 NOTIFIER_LIST_INITIALIZER(powerdown_notifiers);
Gerd Hoffmann95b363b2012-02-23 13:45:19 +01001477static NotifierList suspend_notifiers =
1478 NOTIFIER_LIST_INITIALIZER(suspend_notifiers);
1479static NotifierList wakeup_notifiers =
1480 NOTIFIER_LIST_INITIALIZER(wakeup_notifiers);
1481static uint32_t wakeup_reason_mask = ~0;
Luiz Capitulino0461d5a2011-09-30 14:45:27 -03001482static RunState vmstop_requested = RUN_STATE_MAX;
bellardbb0c6722004-06-20 12:37:32 +00001483
Anthony PERARD1291eb32010-07-22 15:52:48 +01001484int qemu_shutdown_requested_get(void)
1485{
1486 return shutdown_requested;
1487}
1488
1489int qemu_reset_requested_get(void)
1490{
1491 return reset_requested;
1492}
1493
Blue Swirl4fdcac02012-10-28 11:04:47 +00001494static int qemu_shutdown_requested(void)
aurel32cf7a2fe2008-03-18 06:53:05 +00001495{
1496 int r = shutdown_requested;
1497 shutdown_requested = 0;
1498 return r;
1499}
1500
Blue Swirl4fdcac02012-10-28 11:04:47 +00001501static void qemu_kill_report(void)
Gleb Natapovf64622c2011-03-15 13:56:04 +02001502{
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +02001503 if (!qtest_enabled() && shutdown_signal != -1) {
Peter Maydellf1d3fb02011-03-30 22:03:38 +01001504 fprintf(stderr, "qemu: terminating on signal %d", shutdown_signal);
1505 if (shutdown_pid == 0) {
1506 /* This happens for eg ^C at the terminal, so it's worth
1507 * avoiding printing an odd message in that case.
1508 */
1509 fputc('\n', stderr);
1510 } else {
Andreas Färber953ffe02011-06-02 19:58:06 +02001511 fprintf(stderr, " from pid " FMT_pid "\n", shutdown_pid);
Peter Maydellf1d3fb02011-03-30 22:03:38 +01001512 }
Gleb Natapovf64622c2011-03-15 13:56:04 +02001513 shutdown_signal = -1;
1514 }
1515}
1516
Blue Swirl4fdcac02012-10-28 11:04:47 +00001517static int qemu_reset_requested(void)
aurel32cf7a2fe2008-03-18 06:53:05 +00001518{
1519 int r = reset_requested;
1520 reset_requested = 0;
1521 return r;
1522}
1523
Gerd Hoffmann95b363b2012-02-23 13:45:19 +01001524static int qemu_suspend_requested(void)
1525{
1526 int r = suspend_requested;
1527 suspend_requested = 0;
1528 return r;
1529}
1530
Luiz Capitulino14058192012-08-08 17:29:17 -03001531static int qemu_wakeup_requested(void)
1532{
1533 int r = wakeup_requested;
1534 wakeup_requested = 0;
1535 return r;
1536}
1537
Blue Swirl4fdcac02012-10-28 11:04:47 +00001538static int qemu_powerdown_requested(void)
aurel32cf7a2fe2008-03-18 06:53:05 +00001539{
1540 int r = powerdown_requested;
1541 powerdown_requested = 0;
1542 return r;
1543}
1544
aliguorie5689022009-04-24 18:03:54 +00001545static int qemu_debug_requested(void)
1546{
1547 int r = debug_requested;
1548 debug_requested = 0;
1549 return r;
1550}
1551
Luiz Capitulino0461d5a2011-09-30 14:45:27 -03001552/* We use RUN_STATE_MAX but any invalid value will do */
Luiz Capitulino0d45b702011-09-30 14:27:11 -03001553static bool qemu_vmstop_requested(RunState *r)
aliguori6e29f5d2009-04-24 18:04:02 +00001554{
Luiz Capitulino0461d5a2011-09-30 14:45:27 -03001555 if (vmstop_requested < RUN_STATE_MAX) {
Luiz Capitulino0d45b702011-09-30 14:27:11 -03001556 *r = vmstop_requested;
Luiz Capitulino0461d5a2011-09-30 14:45:27 -03001557 vmstop_requested = RUN_STATE_MAX;
Luiz Capitulino0d45b702011-09-30 14:27:11 -03001558 return true;
1559 }
1560
1561 return false;
aliguori6e29f5d2009-04-24 18:04:02 +00001562}
1563
Jan Kiszkaa08d4362009-06-27 09:25:07 +02001564void qemu_register_reset(QEMUResetHandler *func, void *opaque)
bellardbb0c6722004-06-20 12:37:32 +00001565{
Anthony Liguori7267c092011-08-20 22:09:37 -05001566 QEMUResetEntry *re = g_malloc0(sizeof(QEMUResetEntry));
bellardbb0c6722004-06-20 12:37:32 +00001567
bellardbb0c6722004-06-20 12:37:32 +00001568 re->func = func;
1569 re->opaque = opaque;
Blue Swirl72cf2d42009-09-12 07:36:22 +00001570 QTAILQ_INSERT_TAIL(&reset_handlers, re, entry);
bellardbb0c6722004-06-20 12:37:32 +00001571}
1572
Jan Kiszkadda9b292009-07-02 00:19:02 +02001573void qemu_unregister_reset(QEMUResetHandler *func, void *opaque)
bellardbb0c6722004-06-20 12:37:32 +00001574{
1575 QEMUResetEntry *re;
1576
Blue Swirl72cf2d42009-09-12 07:36:22 +00001577 QTAILQ_FOREACH(re, &reset_handlers, entry) {
Jan Kiszkadda9b292009-07-02 00:19:02 +02001578 if (re->func == func && re->opaque == opaque) {
Blue Swirl72cf2d42009-09-12 07:36:22 +00001579 QTAILQ_REMOVE(&reset_handlers, re, entry);
Anthony Liguori7267c092011-08-20 22:09:37 -05001580 g_free(re);
Jan Kiszkadda9b292009-07-02 00:19:02 +02001581 return;
1582 }
1583 }
1584}
1585
David Gibsonbe522022012-08-07 16:41:51 +10001586void qemu_devices_reset(void)
Jan Kiszkadda9b292009-07-02 00:19:02 +02001587{
1588 QEMUResetEntry *re, *nre;
1589
1590 /* reset all devices */
Blue Swirl72cf2d42009-09-12 07:36:22 +00001591 QTAILQ_FOREACH_SAFE(re, &reset_handlers, entry, nre) {
bellardbb0c6722004-06-20 12:37:32 +00001592 re->func(re->opaque);
1593 }
David Gibsonbe522022012-08-07 16:41:51 +10001594}
1595
1596void qemu_system_reset(bool report)
1597{
1598 if (current_machine && current_machine->reset) {
1599 current_machine->reset();
1600 } else {
1601 qemu_devices_reset();
1602 }
Jan Kiszkae063eb12011-06-14 18:29:43 +02001603 if (report) {
1604 monitor_protocol_event(QEVENT_RESET, NULL);
1605 }
Jan Kiszkaea375f92010-03-01 19:10:30 +01001606 cpu_synchronize_all_post_reset();
bellardbb0c6722004-06-20 12:37:32 +00001607}
1608
1609void qemu_system_reset_request(void)
1610{
bellardd1beab82006-10-02 19:44:22 +00001611 if (no_reboot) {
1612 shutdown_requested = 1;
1613 } else {
1614 reset_requested = 1;
1615 }
Jan Kiszkab4a3d962011-02-01 22:15:43 +01001616 cpu_stop_current();
aliguorid9f75a42009-04-24 18:03:11 +00001617 qemu_notify_event();
bellardbb0c6722004-06-20 12:37:32 +00001618}
1619
Gerd Hoffmann95b363b2012-02-23 13:45:19 +01001620static void qemu_system_suspend(void)
1621{
1622 pause_all_vcpus();
1623 notifier_list_notify(&suspend_notifiers, NULL);
Luiz Capitulinoad02b962012-04-27 13:33:36 -03001624 runstate_set(RUN_STATE_SUSPENDED);
Gerd Hoffmann53370b72012-02-23 13:45:26 +01001625 monitor_protocol_event(QEVENT_SUSPEND, NULL);
Gerd Hoffmann95b363b2012-02-23 13:45:19 +01001626}
1627
1628void qemu_system_suspend_request(void)
1629{
Luiz Capitulino9abc62f2012-04-27 14:31:12 -03001630 if (runstate_check(RUN_STATE_SUSPENDED)) {
Gerd Hoffmann95b363b2012-02-23 13:45:19 +01001631 return;
1632 }
1633 suspend_requested = 1;
1634 cpu_stop_current();
1635 qemu_notify_event();
1636}
1637
1638void qemu_register_suspend_notifier(Notifier *notifier)
1639{
1640 notifier_list_add(&suspend_notifiers, notifier);
1641}
1642
1643void qemu_system_wakeup_request(WakeupReason reason)
1644{
Luiz Capitulino9abc62f2012-04-27 14:31:12 -03001645 if (!runstate_check(RUN_STATE_SUSPENDED)) {
Gerd Hoffmann95b363b2012-02-23 13:45:19 +01001646 return;
1647 }
1648 if (!(wakeup_reason_mask & (1 << reason))) {
1649 return;
1650 }
Luiz Capitulinoad02b962012-04-27 13:33:36 -03001651 runstate_set(RUN_STATE_RUNNING);
Gerd Hoffmann95b363b2012-02-23 13:45:19 +01001652 notifier_list_notify(&wakeup_notifiers, &reason);
Luiz Capitulino14058192012-08-08 17:29:17 -03001653 wakeup_requested = 1;
Gerd Hoffmann95b363b2012-02-23 13:45:19 +01001654 qemu_notify_event();
Gerd Hoffmann95b363b2012-02-23 13:45:19 +01001655}
1656
1657void qemu_system_wakeup_enable(WakeupReason reason, bool enabled)
1658{
1659 if (enabled) {
1660 wakeup_reason_mask |= (1 << reason);
1661 } else {
1662 wakeup_reason_mask &= ~(1 << reason);
1663 }
1664}
1665
1666void qemu_register_wakeup_notifier(Notifier *notifier)
1667{
1668 notifier_list_add(&wakeup_notifiers, notifier);
1669}
1670
Gleb Natapovf64622c2011-03-15 13:56:04 +02001671void qemu_system_killed(int signal, pid_t pid)
1672{
1673 shutdown_signal = signal;
1674 shutdown_pid = pid;
Kevin Wolfd9389b92011-09-14 15:38:40 +02001675 no_shutdown = 0;
Gleb Natapovf64622c2011-03-15 13:56:04 +02001676 qemu_system_shutdown_request();
1677}
1678
bellardbb0c6722004-06-20 12:37:32 +00001679void qemu_system_shutdown_request(void)
1680{
1681 shutdown_requested = 1;
aliguorid9f75a42009-04-24 18:03:11 +00001682 qemu_notify_event();
bellardbb0c6722004-06-20 12:37:32 +00001683}
1684
Igor Mammedov013c2f12012-09-05 23:06:25 +02001685static void qemu_system_powerdown(void)
1686{
1687 monitor_protocol_event(QEVENT_POWERDOWN, NULL);
1688 notifier_list_notify(&powerdown_notifiers, NULL);
1689}
1690
bellard34751872005-07-02 14:31:34 +00001691void qemu_system_powerdown_request(void)
1692{
1693 powerdown_requested = 1;
aliguorid9f75a42009-04-24 18:03:11 +00001694 qemu_notify_event();
1695}
1696
Igor Mammedova9552c82012-09-05 23:06:21 +02001697void qemu_register_powerdown_notifier(Notifier *notifier)
1698{
1699 notifier_list_add(&powerdown_notifiers, notifier);
1700}
1701
Jan Kiszka8cf71712011-02-07 12:19:16 +01001702void qemu_system_debug_request(void)
1703{
1704 debug_requested = 1;
Jan Kiszka83f338f2011-02-07 12:19:17 +01001705 qemu_notify_event();
Jan Kiszka8cf71712011-02-07 12:19:16 +01001706}
1707
Luiz Capitulino1dfb4dd2011-07-29 14:26:33 -03001708void qemu_system_vmstop_request(RunState state)
Jan Kiszka8cf71712011-02-07 12:19:16 +01001709{
Luiz Capitulino1dfb4dd2011-07-29 14:26:33 -03001710 vmstop_requested = state;
Jan Kiszka8cf71712011-02-07 12:19:16 +01001711 qemu_notify_event();
1712}
1713
Paolo Bonzini99435902011-09-12 14:03:13 +02001714static bool main_loop_should_exit(void)
1715{
1716 RunState r;
1717 if (qemu_debug_requested()) {
1718 vm_stop(RUN_STATE_DEBUG);
1719 }
Gerd Hoffmann95b363b2012-02-23 13:45:19 +01001720 if (qemu_suspend_requested()) {
1721 qemu_system_suspend();
1722 }
Paolo Bonzini99435902011-09-12 14:03:13 +02001723 if (qemu_shutdown_requested()) {
1724 qemu_kill_report();
1725 monitor_protocol_event(QEVENT_SHUTDOWN, NULL);
1726 if (no_shutdown) {
1727 vm_stop(RUN_STATE_SHUTDOWN);
1728 } else {
1729 return true;
1730 }
1731 }
1732 if (qemu_reset_requested()) {
1733 pause_all_vcpus();
1734 cpu_synchronize_all_states();
1735 qemu_system_reset(VMRESET_REPORT);
1736 resume_all_vcpus();
1737 if (runstate_check(RUN_STATE_INTERNAL_ERROR) ||
1738 runstate_check(RUN_STATE_SHUTDOWN)) {
1739 runstate_set(RUN_STATE_PAUSED);
1740 }
1741 }
Luiz Capitulino14058192012-08-08 17:29:17 -03001742 if (qemu_wakeup_requested()) {
1743 pause_all_vcpus();
1744 cpu_synchronize_all_states();
1745 qemu_system_reset(VMRESET_SILENT);
1746 resume_all_vcpus();
Luiz Capitulino17c86602012-08-09 11:27:30 -03001747 monitor_protocol_event(QEVENT_WAKEUP, NULL);
Luiz Capitulino14058192012-08-08 17:29:17 -03001748 }
Paolo Bonzini99435902011-09-12 14:03:13 +02001749 if (qemu_powerdown_requested()) {
Igor Mammedov013c2f12012-09-05 23:06:25 +02001750 qemu_system_powerdown();
Paolo Bonzini99435902011-09-12 14:03:13 +02001751 }
1752 if (qemu_vmstop_requested(&r)) {
1753 vm_stop(r);
1754 }
1755 return false;
1756}
1757
aliguori43b96852009-04-24 18:03:33 +00001758static void main_loop(void)
1759{
Jan Kiszkac9f711a2011-08-22 17:46:02 +02001760 bool nonblocking;
Paolo Bonzini99435902011-09-12 14:03:13 +02001761 int last_io = 0;
Jan Kiszka8e1b90e2011-02-01 22:15:46 +01001762#ifdef CONFIG_PROFILER
1763 int64_t ti;
1764#endif
Paolo Bonzini99435902011-09-12 14:03:13 +02001765 do {
Jan Kiszkac9f711a2011-08-22 17:46:02 +02001766 nonblocking = !kvm_enabled() && last_io > 0;
aliguori43b96852009-04-24 18:03:33 +00001767#ifdef CONFIG_PROFILER
Jan Kiszka46481d32011-02-01 22:15:47 +01001768 ti = profile_getclock();
aliguori43b96852009-04-24 18:03:33 +00001769#endif
Jan Kiszkac9f711a2011-08-22 17:46:02 +02001770 last_io = main_loop_wait(nonblocking);
aliguori43b96852009-04-24 18:03:33 +00001771#ifdef CONFIG_PROFILER
Jan Kiszka46481d32011-02-01 22:15:47 +01001772 dev_time += profile_getclock() - ti;
aliguori43b96852009-04-24 18:03:33 +00001773#endif
Paolo Bonzini99435902011-09-12 14:03:13 +02001774 } while (!main_loop_should_exit());
bellardb4608c02003-06-27 17:34:32 +00001775}
1776
pbrook9bd7e6d2009-04-07 22:58:45 +00001777static void version(void)
1778{
Thomas Monjalonf75ca1a2010-04-28 14:42:01 +02001779 printf("QEMU emulator version " QEMU_VERSION QEMU_PKGVERSION ", Copyright (c) 2003-2008 Fabrice Bellard\n");
pbrook9bd7e6d2009-04-07 22:58:45 +00001780}
1781
ths15f82202007-06-29 23:26:08 +00001782static void help(int exitcode)
bellard0824d6f2003-06-24 13:42:40 +00001783{
Michael Ellermana3adb7a2011-12-19 17:19:31 +11001784 version();
1785 printf("usage: %s [options] [disk_image]\n\n"
1786 "'disk_image' is a raw hard disk image for IDE hard disk 0\n\n",
1787 error_get_progname());
1788
Michael Ellerman77bd1112011-12-19 17:19:30 +11001789#define QEMU_OPTIONS_GENERATE_HELP
1790#include "qemu-options-wrapper.h"
Michael Ellermana3adb7a2011-12-19 17:19:31 +11001791
1792 printf("\nDuring emulation, the following keys are useful:\n"
malc3f020d72010-02-08 12:04:56 +03001793 "ctrl-alt-f toggle full screen\n"
1794 "ctrl-alt-n switch to virtual console 'n'\n"
1795 "ctrl-alt toggle mouse and keyboard grab\n"
1796 "\n"
Michael Ellermana3adb7a2011-12-19 17:19:31 +11001797 "When using -nographic, press 'ctrl-a h' to get some help.\n");
1798
ths15f82202007-06-29 23:26:08 +00001799 exit(exitcode);
bellard0824d6f2003-06-24 13:42:40 +00001800}
1801
bellardcd6f1162004-05-13 22:02:20 +00001802#define HAS_ARG 0x0001
1803
bellardcd6f1162004-05-13 22:02:20 +00001804typedef struct QEMUOption {
1805 const char *name;
1806 int flags;
1807 int index;
Blue Swirlad960902010-03-29 19:23:52 +00001808 uint32_t arch_mask;
bellardcd6f1162004-05-13 22:02:20 +00001809} QEMUOption;
1810
blueswir1dbed7e42008-10-01 19:38:09 +00001811static const QEMUOption qemu_options[] = {
Blue Swirlad960902010-03-29 19:23:52 +00001812 { "h", 0, QEMU_OPTION_h, QEMU_ARCH_ALL },
Michael Ellerman77bd1112011-12-19 17:19:30 +11001813#define QEMU_OPTIONS_GENERATE_OPTIONS
1814#include "qemu-options-wrapper.h"
bellardcd6f1162004-05-13 22:02:20 +00001815 { NULL },
bellardfc01f7e2003-06-30 10:03:06 +00001816};
Blue Swirla369da52011-09-27 19:15:42 +00001817
1818static bool vga_available(void)
1819{
Aurelien Jarno36b7f272012-09-08 16:01:20 +02001820 return object_class_by_name("VGA") || object_class_by_name("isa-vga");
Blue Swirla369da52011-09-27 19:15:42 +00001821}
1822
1823static bool cirrus_vga_available(void)
1824{
Aurelien Jarno36b7f272012-09-08 16:01:20 +02001825 return object_class_by_name("cirrus-vga")
1826 || object_class_by_name("isa-cirrus-vga");
Blue Swirla369da52011-09-27 19:15:42 +00001827}
1828
1829static bool vmware_vga_available(void)
1830{
Aurelien Jarno36b7f272012-09-08 16:01:20 +02001831 return object_class_by_name("vmware-svga");
Blue Swirla369da52011-09-27 19:15:42 +00001832}
1833
Aurelien Jarno879049a2012-09-08 12:01:06 +02001834static bool qxl_vga_available(void)
1835{
1836 return object_class_by_name("qxl-vga");
1837}
1838
malc3893c122008-09-28 00:42:05 +00001839static void select_vgahw (const char *p)
1840{
1841 const char *opts;
1842
Zachary Amsden86176752009-07-30 00:15:02 -10001843 vga_interface_type = VGA_NONE;
malc3893c122008-09-28 00:42:05 +00001844 if (strstart(p, "std", &opts)) {
Blue Swirla369da52011-09-27 19:15:42 +00001845 if (vga_available()) {
1846 vga_interface_type = VGA_STD;
1847 } else {
1848 fprintf(stderr, "Error: standard VGA not available\n");
1849 exit(0);
1850 }
malc3893c122008-09-28 00:42:05 +00001851 } else if (strstart(p, "cirrus", &opts)) {
Blue Swirla369da52011-09-27 19:15:42 +00001852 if (cirrus_vga_available()) {
1853 vga_interface_type = VGA_CIRRUS;
1854 } else {
1855 fprintf(stderr, "Error: Cirrus VGA not available\n");
1856 exit(0);
1857 }
malc3893c122008-09-28 00:42:05 +00001858 } else if (strstart(p, "vmware", &opts)) {
Blue Swirla369da52011-09-27 19:15:42 +00001859 if (vmware_vga_available()) {
1860 vga_interface_type = VGA_VMWARE;
1861 } else {
1862 fprintf(stderr, "Error: VMWare SVGA not available\n");
1863 exit(0);
1864 }
aliguori94909d92009-04-22 15:19:53 +00001865 } else if (strstart(p, "xenfb", &opts)) {
Zachary Amsden86176752009-07-30 00:15:02 -10001866 vga_interface_type = VGA_XENFB;
Gerd Hoffmanna19cbfb2010-04-27 11:50:11 +02001867 } else if (strstart(p, "qxl", &opts)) {
Aurelien Jarno879049a2012-09-08 12:01:06 +02001868 if (qxl_vga_available()) {
1869 vga_interface_type = VGA_QXL;
1870 } else {
1871 fprintf(stderr, "Error: QXL VGA not available\n");
1872 exit(0);
1873 }
aliguori28b85ed2009-04-22 15:19:48 +00001874 } else if (!strstart(p, "none", &opts)) {
malc3893c122008-09-28 00:42:05 +00001875 invalid_vga:
1876 fprintf(stderr, "Unknown vga type: %s\n", p);
1877 exit(1);
1878 }
malccb5a7aa2008-09-28 00:42:12 +00001879 while (*opts) {
1880 const char *nextopt;
1881
1882 if (strstart(opts, ",retrace=", &nextopt)) {
1883 opts = nextopt;
1884 if (strstart(opts, "dumb", &nextopt))
1885 vga_retrace_method = VGA_RETRACE_DUMB;
1886 else if (strstart(opts, "precise", &nextopt))
1887 vga_retrace_method = VGA_RETRACE_PRECISE;
1888 else goto invalid_vga;
1889 } else goto invalid_vga;
1890 opts = nextopt;
1891 }
malc3893c122008-09-28 00:42:05 +00001892}
1893
Jes Sorensen1472a952011-03-16 13:33:31 +01001894static DisplayType select_display(const char *p)
1895{
1896 const char *opts;
1897 DisplayType display = DT_DEFAULT;
1898
1899 if (strstart(p, "sdl", &opts)) {
1900#ifdef CONFIG_SDL
1901 display = DT_SDL;
1902 while (*opts) {
1903 const char *nextopt;
1904
1905 if (strstart(opts, ",frame=", &nextopt)) {
1906 opts = nextopt;
1907 if (strstart(opts, "on", &nextopt)) {
1908 no_frame = 0;
1909 } else if (strstart(opts, "off", &nextopt)) {
1910 no_frame = 1;
1911 } else {
Peter Maydell05175532011-03-23 03:40:57 +00001912 goto invalid_sdl_args;
Jes Sorensen1472a952011-03-16 13:33:31 +01001913 }
1914 } else if (strstart(opts, ",alt_grab=", &nextopt)) {
1915 opts = nextopt;
1916 if (strstart(opts, "on", &nextopt)) {
1917 alt_grab = 1;
1918 } else if (strstart(opts, "off", &nextopt)) {
1919 alt_grab = 0;
1920 } else {
Peter Maydell05175532011-03-23 03:40:57 +00001921 goto invalid_sdl_args;
Jes Sorensen1472a952011-03-16 13:33:31 +01001922 }
1923 } else if (strstart(opts, ",ctrl_grab=", &nextopt)) {
1924 opts = nextopt;
1925 if (strstart(opts, "on", &nextopt)) {
1926 ctrl_grab = 1;
1927 } else if (strstart(opts, "off", &nextopt)) {
1928 ctrl_grab = 0;
1929 } else {
Peter Maydell05175532011-03-23 03:40:57 +00001930 goto invalid_sdl_args;
Jes Sorensen1472a952011-03-16 13:33:31 +01001931 }
1932 } else if (strstart(opts, ",window_close=", &nextopt)) {
1933 opts = nextopt;
1934 if (strstart(opts, "on", &nextopt)) {
1935 no_quit = 0;
1936 } else if (strstart(opts, "off", &nextopt)) {
1937 no_quit = 1;
1938 } else {
Peter Maydell05175532011-03-23 03:40:57 +00001939 goto invalid_sdl_args;
Jes Sorensen1472a952011-03-16 13:33:31 +01001940 }
1941 } else {
Peter Maydell05175532011-03-23 03:40:57 +00001942 invalid_sdl_args:
1943 fprintf(stderr, "Invalid SDL option string: %s\n", p);
1944 exit(1);
Jes Sorensen1472a952011-03-16 13:33:31 +01001945 }
1946 opts = nextopt;
1947 }
1948#else
1949 fprintf(stderr, "SDL support is disabled\n");
1950 exit(1);
1951#endif
Jes Sorensen3264ff12011-03-16 13:33:33 +01001952 } else if (strstart(p, "vnc", &opts)) {
Jes Sorensen821601e2011-03-16 13:33:36 +01001953#ifdef CONFIG_VNC
Jes Sorensen3264ff12011-03-16 13:33:33 +01001954 display_remote++;
1955
1956 if (*opts) {
1957 const char *nextopt;
1958
1959 if (strstart(opts, "=", &nextopt)) {
1960 vnc_display = nextopt;
1961 }
1962 }
1963 if (!vnc_display) {
1964 fprintf(stderr, "VNC requires a display argument vnc=<display>\n");
1965 exit(1);
1966 }
Jes Sorensen821601e2011-03-16 13:33:36 +01001967#else
1968 fprintf(stderr, "VNC support is disabled\n");
1969 exit(1);
1970#endif
Jes Sorensen1472a952011-03-16 13:33:31 +01001971 } else if (strstart(p, "curses", &opts)) {
1972#ifdef CONFIG_CURSES
1973 display = DT_CURSES;
1974#else
1975 fprintf(stderr, "Curses support is disabled\n");
1976 exit(1);
1977#endif
Jes Sorensen4171d322011-03-16 13:33:32 +01001978 } else if (strstart(p, "none", &opts)) {
1979 display = DT_NONE;
Jes Sorensen1472a952011-03-16 13:33:31 +01001980 } else {
Jes Sorensen1472a952011-03-16 13:33:31 +01001981 fprintf(stderr, "Unknown display type: %s\n", p);
1982 exit(1);
1983 }
1984
1985 return display;
1986}
1987
Markus Armbruster7d4c3d52009-06-26 19:15:14 +02001988static int balloon_parse(const char *arg)
1989{
Gerd Hoffmann382f0742009-08-14 10:34:22 +02001990 QemuOpts *opts;
Markus Armbruster7d4c3d52009-06-26 19:15:14 +02001991
Gerd Hoffmann382f0742009-08-14 10:34:22 +02001992 if (strcmp(arg, "none") == 0) {
1993 return 0;
Markus Armbruster7d4c3d52009-06-26 19:15:14 +02001994 }
Gerd Hoffmann382f0742009-08-14 10:34:22 +02001995
1996 if (!strncmp(arg, "virtio", 6)) {
1997 if (arg[6] == ',') {
1998 /* have params -> parse them */
Gerd Hoffmann3329f072010-08-20 13:52:01 +02001999 opts = qemu_opts_parse(qemu_find_opts("device"), arg+7, 0);
Gerd Hoffmann382f0742009-08-14 10:34:22 +02002000 if (!opts)
2001 return -1;
2002 } else {
2003 /* create empty opts */
Luiz Capitulino8be7e7e2012-03-20 15:51:57 -03002004 opts = qemu_opts_create(qemu_find_opts("device"), NULL, 0, NULL);
Gerd Hoffmann382f0742009-08-14 10:34:22 +02002005 }
Alexander Graf29f82b32011-03-29 15:29:29 +02002006 qemu_opt_set(opts, "driver", "virtio-balloon");
Gerd Hoffmann382f0742009-08-14 10:34:22 +02002007 return 0;
2008 }
2009
2010 return -1;
Markus Armbruster7d4c3d52009-06-26 19:15:14 +02002011}
Markus Armbruster7d4c3d52009-06-26 19:15:14 +02002012
Paul Brook5cea8592009-05-30 00:52:44 +01002013char *qemu_find_file(int type, const char *name)
2014{
2015 int len;
2016 const char *subdir;
2017 char *buf;
2018
Peter Maydell31783202012-05-25 13:07:01 +01002019 /* Try the name as a straight path first */
2020 if (access(name, R_OK) == 0) {
Anthony Liguori7267c092011-08-20 22:09:37 -05002021 return g_strdup(name);
Paul Brook5cea8592009-05-30 00:52:44 +01002022 }
2023 switch (type) {
2024 case QEMU_FILE_TYPE_BIOS:
2025 subdir = "";
2026 break;
2027 case QEMU_FILE_TYPE_KEYMAP:
2028 subdir = "keymaps/";
2029 break;
2030 default:
2031 abort();
2032 }
2033 len = strlen(data_dir) + strlen(name) + strlen(subdir) + 2;
Anthony Liguori7267c092011-08-20 22:09:37 -05002034 buf = g_malloc0(len);
Blue Swirl3a417592009-06-09 19:12:21 +00002035 snprintf(buf, len, "%s/%s%s", data_dir, subdir, name);
Paul Brook5cea8592009-05-30 00:52:44 +01002036 if (access(buf, R_OK)) {
Anthony Liguori7267c092011-08-20 22:09:37 -05002037 g_free(buf);
Paul Brook5cea8592009-05-30 00:52:44 +01002038 return NULL;
2039 }
2040 return buf;
2041}
2042
Markus Armbrusterff952ba2010-01-29 19:48:57 +01002043static int device_help_func(QemuOpts *opts, void *opaque)
2044{
2045 return qdev_device_help(opts);
2046}
2047
Gerd Hoffmannf31d07d2009-07-31 12:25:37 +02002048static int device_init_func(QemuOpts *opts, void *opaque)
2049{
2050 DeviceState *dev;
2051
2052 dev = qdev_device_add(opts);
2053 if (!dev)
2054 return -1;
2055 return 0;
2056}
2057
Gerd Hoffmann1a688d32009-12-08 13:11:36 +01002058static int chardev_init_func(QemuOpts *opts, void *opaque)
2059{
2060 CharDriverState *chr;
2061
Anthony Liguorif69554b2011-08-15 11:17:37 -05002062 chr = qemu_chr_new_from_opts(opts, NULL);
Gerd Hoffmann1a688d32009-12-08 13:11:36 +01002063 if (!chr)
2064 return -1;
2065 return 0;
2066}
2067
Venkateswararao Jujjuri (JV)758e8e32010-06-14 13:34:41 -07002068#ifdef CONFIG_VIRTFS
Gautham R Shenoy74db9202010-04-29 17:44:43 +05302069static int fsdev_init_func(QemuOpts *opts, void *opaque)
2070{
2071 int ret;
2072 ret = qemu_fsdev_add(opts);
2073
2074 return ret;
2075}
2076#endif
2077
Gerd Hoffmann88589342009-12-08 13:11:50 +01002078static int mon_init_func(QemuOpts *opts, void *opaque)
2079{
2080 CharDriverState *chr;
2081 const char *chardev;
2082 const char *mode;
2083 int flags;
2084
2085 mode = qemu_opt_get(opts, "mode");
2086 if (mode == NULL) {
2087 mode = "readline";
2088 }
2089 if (strcmp(mode, "readline") == 0) {
2090 flags = MONITOR_USE_READLINE;
2091 } else if (strcmp(mode, "control") == 0) {
2092 flags = MONITOR_USE_CONTROL;
2093 } else {
2094 fprintf(stderr, "unknown monitor mode \"%s\"\n", mode);
2095 exit(1);
2096 }
2097
Daniel P. Berrange39eaab92010-06-07 15:42:31 +01002098 if (qemu_opt_get_bool(opts, "pretty", 0))
2099 flags |= MONITOR_USE_PRETTY;
2100
Gerd Hoffmann88589342009-12-08 13:11:50 +01002101 if (qemu_opt_get_bool(opts, "default", 0))
2102 flags |= MONITOR_IS_DEFAULT;
2103
2104 chardev = qemu_opt_get(opts, "chardev");
2105 chr = qemu_chr_find(chardev);
2106 if (chr == NULL) {
2107 fprintf(stderr, "chardev \"%s\" not found\n", chardev);
2108 exit(1);
2109 }
2110
2111 monitor_init(chr, flags);
2112 return 0;
2113}
2114
Gerd Hoffmann6ca55822009-12-08 13:11:52 +01002115static void monitor_parse(const char *optarg, const char *mode)
Gerd Hoffmann88589342009-12-08 13:11:50 +01002116{
2117 static int monitor_device_index = 0;
2118 QemuOpts *opts;
2119 const char *p;
2120 char label[32];
2121 int def = 0;
2122
2123 if (strstart(optarg, "chardev:", &p)) {
2124 snprintf(label, sizeof(label), "%s", p);
2125 } else {
Jan Kiszka140e0652010-04-06 16:55:52 +02002126 snprintf(label, sizeof(label), "compat_monitor%d",
2127 monitor_device_index);
2128 if (monitor_device_index == 0) {
Gerd Hoffmann88589342009-12-08 13:11:50 +01002129 def = 1;
2130 }
2131 opts = qemu_chr_parse_compat(label, optarg);
2132 if (!opts) {
2133 fprintf(stderr, "parse error: %s\n", optarg);
2134 exit(1);
2135 }
2136 }
2137
Luiz Capitulino8be7e7e2012-03-20 15:51:57 -03002138 opts = qemu_opts_create(qemu_find_opts("mon"), label, 1, NULL);
Gerd Hoffmann88589342009-12-08 13:11:50 +01002139 if (!opts) {
2140 fprintf(stderr, "duplicate chardev: %s\n", label);
2141 exit(1);
2142 }
Gerd Hoffmann6ca55822009-12-08 13:11:52 +01002143 qemu_opt_set(opts, "mode", mode);
Gerd Hoffmann88589342009-12-08 13:11:50 +01002144 qemu_opt_set(opts, "chardev", label);
2145 if (def)
2146 qemu_opt_set(opts, "default", "on");
2147 monitor_device_index++;
2148}
2149
Gerd Hoffmannbd3c9482009-07-15 13:59:26 +02002150struct device_config {
2151 enum {
Gerd Hoffmannaee1b932009-12-08 13:11:53 +01002152 DEV_USB, /* -usbdevice */
2153 DEV_BT, /* -bt */
2154 DEV_SERIAL, /* -serial */
2155 DEV_PARALLEL, /* -parallel */
2156 DEV_VIRTCON, /* -virtioconsole */
H. Peter Anvinc9f398e2009-12-29 13:51:36 -08002157 DEV_DEBUGCON, /* -debugcon */
Markus Armbrusteref0c4a02012-02-07 15:09:13 +01002158 DEV_GDB, /* -gdb, -s */
Gerd Hoffmannbd3c9482009-07-15 13:59:26 +02002159 } type;
2160 const char *cmdline;
Markus Armbrusterd9a59542012-02-07 15:09:12 +01002161 Location loc;
Blue Swirl72cf2d42009-09-12 07:36:22 +00002162 QTAILQ_ENTRY(device_config) next;
Gerd Hoffmannbd3c9482009-07-15 13:59:26 +02002163};
Blue Swirl4fdcac02012-10-28 11:04:47 +00002164
2165static QTAILQ_HEAD(, device_config) device_configs =
2166 QTAILQ_HEAD_INITIALIZER(device_configs);
Gerd Hoffmannbd3c9482009-07-15 13:59:26 +02002167
2168static void add_device_config(int type, const char *cmdline)
2169{
2170 struct device_config *conf;
2171
Anthony Liguori7267c092011-08-20 22:09:37 -05002172 conf = g_malloc0(sizeof(*conf));
Gerd Hoffmannbd3c9482009-07-15 13:59:26 +02002173 conf->type = type;
2174 conf->cmdline = cmdline;
Markus Armbrusterd9a59542012-02-07 15:09:12 +01002175 loc_save(&conf->loc);
Blue Swirl72cf2d42009-09-12 07:36:22 +00002176 QTAILQ_INSERT_TAIL(&device_configs, conf, next);
Gerd Hoffmannbd3c9482009-07-15 13:59:26 +02002177}
2178
2179static int foreach_device_config(int type, int (*func)(const char *cmdline))
2180{
2181 struct device_config *conf;
2182 int rc;
2183
Blue Swirl72cf2d42009-09-12 07:36:22 +00002184 QTAILQ_FOREACH(conf, &device_configs, next) {
Gerd Hoffmannbd3c9482009-07-15 13:59:26 +02002185 if (conf->type != type)
2186 continue;
Markus Armbrusterd9a59542012-02-07 15:09:12 +01002187 loc_push_restore(&conf->loc);
Gerd Hoffmannbd3c9482009-07-15 13:59:26 +02002188 rc = func(conf->cmdline);
Markus Armbrusterd9a59542012-02-07 15:09:12 +01002189 loc_pop(&conf->loc);
Gerd Hoffmannbd3c9482009-07-15 13:59:26 +02002190 if (0 != rc)
2191 return rc;
2192 }
2193 return 0;
2194}
2195
Gerd Hoffmann998bbd72009-12-08 13:11:41 +01002196static int serial_parse(const char *devname)
2197{
2198 static int index = 0;
2199 char label[32];
2200
2201 if (strcmp(devname, "none") == 0)
2202 return 0;
2203 if (index == MAX_SERIAL_PORTS) {
2204 fprintf(stderr, "qemu: too many serial ports\n");
2205 exit(1);
2206 }
2207 snprintf(label, sizeof(label), "serial%d", index);
Anthony Liguori27143a42011-08-15 11:17:36 -05002208 serial_hds[index] = qemu_chr_new(label, devname, NULL);
Gerd Hoffmann998bbd72009-12-08 13:11:41 +01002209 if (!serial_hds[index]) {
Peter Maydell52d06132012-07-09 04:28:30 +00002210 fprintf(stderr, "qemu: could not connect serial device"
2211 " to character backend '%s'\n", devname);
Gerd Hoffmann998bbd72009-12-08 13:11:41 +01002212 return -1;
2213 }
2214 index++;
2215 return 0;
2216}
2217
Gerd Hoffmann6a5e8b02009-12-08 13:11:42 +01002218static int parallel_parse(const char *devname)
2219{
2220 static int index = 0;
2221 char label[32];
2222
2223 if (strcmp(devname, "none") == 0)
2224 return 0;
2225 if (index == MAX_PARALLEL_PORTS) {
2226 fprintf(stderr, "qemu: too many parallel ports\n");
2227 exit(1);
2228 }
2229 snprintf(label, sizeof(label), "parallel%d", index);
Anthony Liguori27143a42011-08-15 11:17:36 -05002230 parallel_hds[index] = qemu_chr_new(label, devname, NULL);
Gerd Hoffmann6a5e8b02009-12-08 13:11:42 +01002231 if (!parallel_hds[index]) {
Peter Maydell52d06132012-07-09 04:28:30 +00002232 fprintf(stderr, "qemu: could not connect parallel device"
2233 " to character backend '%s'\n", devname);
Gerd Hoffmann6a5e8b02009-12-08 13:11:42 +01002234 return -1;
2235 }
2236 index++;
2237 return 0;
2238}
2239
Gerd Hoffmannaee1b932009-12-08 13:11:53 +01002240static int virtcon_parse(const char *devname)
2241{
Gerd Hoffmann3329f072010-08-20 13:52:01 +02002242 QemuOptsList *device = qemu_find_opts("device");
Gerd Hoffmannaee1b932009-12-08 13:11:53 +01002243 static int index = 0;
2244 char label[32];
Amit Shah392ecf52010-01-21 16:19:23 +05302245 QemuOpts *bus_opts, *dev_opts;
Gerd Hoffmannaee1b932009-12-08 13:11:53 +01002246
2247 if (strcmp(devname, "none") == 0)
2248 return 0;
2249 if (index == MAX_VIRTIO_CONSOLES) {
2250 fprintf(stderr, "qemu: too many virtio consoles\n");
2251 exit(1);
2252 }
Amit Shah392ecf52010-01-21 16:19:23 +05302253
Luiz Capitulino8be7e7e2012-03-20 15:51:57 -03002254 bus_opts = qemu_opts_create(device, NULL, 0, NULL);
Anthony Liguorie87f7fc2012-02-06 11:07:18 -06002255 if (arch_type == QEMU_ARCH_S390X) {
2256 qemu_opt_set(bus_opts, "driver", "virtio-serial-s390");
2257 } else {
2258 qemu_opt_set(bus_opts, "driver", "virtio-serial-pci");
2259 }
Amit Shah392ecf52010-01-21 16:19:23 +05302260
Luiz Capitulino8be7e7e2012-03-20 15:51:57 -03002261 dev_opts = qemu_opts_create(device, NULL, 0, NULL);
Amit Shah392ecf52010-01-21 16:19:23 +05302262 qemu_opt_set(dev_opts, "driver", "virtconsole");
2263
Gerd Hoffmannaee1b932009-12-08 13:11:53 +01002264 snprintf(label, sizeof(label), "virtcon%d", index);
Anthony Liguori27143a42011-08-15 11:17:36 -05002265 virtcon_hds[index] = qemu_chr_new(label, devname, NULL);
Gerd Hoffmannaee1b932009-12-08 13:11:53 +01002266 if (!virtcon_hds[index]) {
Peter Maydell52d06132012-07-09 04:28:30 +00002267 fprintf(stderr, "qemu: could not connect virtio console"
2268 " to character backend '%s'\n", devname);
Gerd Hoffmannaee1b932009-12-08 13:11:53 +01002269 return -1;
2270 }
Amit Shah392ecf52010-01-21 16:19:23 +05302271 qemu_opt_set(dev_opts, "chardev", label);
2272
Gerd Hoffmannaee1b932009-12-08 13:11:53 +01002273 index++;
2274 return 0;
2275}
2276
H. Peter Anvinc9f398e2009-12-29 13:51:36 -08002277static int debugcon_parse(const char *devname)
2278{
2279 QemuOpts *opts;
2280
Anthony Liguori27143a42011-08-15 11:17:36 -05002281 if (!qemu_chr_new("debugcon", devname, NULL)) {
H. Peter Anvinc9f398e2009-12-29 13:51:36 -08002282 exit(1);
2283 }
Luiz Capitulino8be7e7e2012-03-20 15:51:57 -03002284 opts = qemu_opts_create(qemu_find_opts("device"), "debugcon", 1, NULL);
H. Peter Anvinc9f398e2009-12-29 13:51:36 -08002285 if (!opts) {
2286 fprintf(stderr, "qemu: already have a debugcon device\n");
2287 exit(1);
2288 }
2289 qemu_opt_set(opts, "driver", "isa-debugcon");
2290 qemu_opt_set(opts, "chardev", "debugcon");
2291 return 0;
2292}
2293
Jan Kiszka9052ea62011-07-23 12:38:37 +02002294static QEMUMachine *machine_parse(const char *name)
2295{
2296 QEMUMachine *m, *machine = NULL;
2297
2298 if (name) {
2299 machine = find_machine(name);
2300 }
2301 if (machine) {
2302 return machine;
2303 }
2304 printf("Supported machines are:\n");
2305 for (m = first_machine; m != NULL; m = m->next) {
2306 if (m->alias) {
Peter Maydell3b264862012-02-22 22:13:11 +00002307 printf("%-20s %s (alias of %s)\n", m->alias, m->desc, m->name);
Jan Kiszka9052ea62011-07-23 12:38:37 +02002308 }
Peter Maydell3b264862012-02-22 22:13:11 +00002309 printf("%-20s %s%s\n", m->name, m->desc,
Jan Kiszka9052ea62011-07-23 12:38:37 +02002310 m->is_default ? " (default)" : "");
2311 }
Peter Maydellc8057f92012-08-02 13:45:54 +01002312 exit(!name || !is_help_option(name));
Jan Kiszka9052ea62011-07-23 12:38:37 +02002313}
2314
Anthony PERARD303d4e82010-09-21 20:05:31 +01002315static int tcg_init(void)
2316{
Jan Kiszkad5ab9712011-08-02 16:10:21 +02002317 tcg_exec_init(tcg_tb_size * 1024 * 1024);
Anthony PERARD303d4e82010-09-21 20:05:31 +01002318 return 0;
2319}
2320
2321static struct {
2322 const char *opt_name;
2323 const char *name;
2324 int (*available)(void);
2325 int (*init)(void);
2326 int *allowed;
2327} accel_list[] = {
2328 { "tcg", "tcg", tcg_available, tcg_init, &tcg_allowed },
Anthony PERARD3285cf42010-08-19 12:27:56 +01002329 { "xen", "Xen", xen_available, xen_init, &xen_allowed },
Anthony PERARD303d4e82010-09-21 20:05:31 +01002330 { "kvm", "KVM", kvm_available, kvm_init, &kvm_allowed },
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +02002331 { "qtest", "QTest", qtest_available, qtest_init, &qtest_allowed },
Anthony PERARD303d4e82010-09-21 20:05:31 +01002332};
2333
2334static int configure_accelerator(void)
2335{
2336 const char *p = NULL;
2337 char buf[10];
2338 int i, ret;
Peter Maydell1b785a92012-02-07 20:57:27 +00002339 bool accel_initialised = 0;
Anthony PERARD303d4e82010-09-21 20:05:31 +01002340 bool init_failed = 0;
2341
2342 QemuOptsList *list = qemu_find_opts("machine");
2343 if (!QTAILQ_EMPTY(&list->head)) {
2344 p = qemu_opt_get(QTAILQ_FIRST(&list->head), "accel");
2345 }
2346
2347 if (p == NULL) {
2348 /* Use the default "accelerator", tcg */
2349 p = "tcg";
2350 }
2351
Peter Maydell1b785a92012-02-07 20:57:27 +00002352 while (!accel_initialised && *p != '\0') {
Anthony PERARD303d4e82010-09-21 20:05:31 +01002353 if (*p == ':') {
2354 p++;
2355 }
2356 p = get_opt_name(buf, sizeof (buf), p, ':');
2357 for (i = 0; i < ARRAY_SIZE(accel_list); i++) {
2358 if (strcmp(accel_list[i].opt_name, buf) == 0) {
Anthony Liguoria16c53b2011-06-06 08:25:06 -05002359 *(accel_list[i].allowed) = 1;
Anthony PERARD303d4e82010-09-21 20:05:31 +01002360 ret = accel_list[i].init();
2361 if (ret < 0) {
2362 init_failed = 1;
2363 if (!accel_list[i].available()) {
2364 printf("%s not supported for this target\n",
2365 accel_list[i].name);
2366 } else {
2367 fprintf(stderr, "failed to initialize %s: %s\n",
2368 accel_list[i].name,
2369 strerror(-ret));
2370 }
Anthony Liguoria16c53b2011-06-06 08:25:06 -05002371 *(accel_list[i].allowed) = 0;
Anthony PERARD303d4e82010-09-21 20:05:31 +01002372 } else {
Peter Maydell1b785a92012-02-07 20:57:27 +00002373 accel_initialised = 1;
Anthony PERARD303d4e82010-09-21 20:05:31 +01002374 }
2375 break;
2376 }
2377 }
2378 if (i == ARRAY_SIZE(accel_list)) {
2379 fprintf(stderr, "\"%s\" accelerator does not exist.\n", buf);
2380 }
2381 }
2382
Peter Maydell1b785a92012-02-07 20:57:27 +00002383 if (!accel_initialised) {
Anthony PERARD303d4e82010-09-21 20:05:31 +01002384 fprintf(stderr, "No accelerator found!\n");
2385 exit(1);
2386 }
2387
2388 if (init_failed) {
2389 fprintf(stderr, "Back to %s accelerator.\n", accel_list[i].name);
2390 }
2391
Peter Maydell1b785a92012-02-07 20:57:27 +00002392 return !accel_initialised;
Anthony PERARD303d4e82010-09-21 20:05:31 +01002393}
2394
Gerd Hoffmannfd42dee2010-06-04 14:08:07 +02002395void qemu_add_exit_notifier(Notifier *notify)
2396{
2397 notifier_list_add(&exit_notifiers, notify);
2398}
2399
2400void qemu_remove_exit_notifier(Notifier *notify)
2401{
Paolo Bonzini31552522012-01-13 17:34:01 +01002402 notifier_remove(notify);
Gerd Hoffmannfd42dee2010-06-04 14:08:07 +02002403}
2404
2405static void qemu_run_exit_notifiers(void)
2406{
Jan Kiszka9e8dd452011-06-20 14:06:26 +02002407 notifier_list_notify(&exit_notifiers, NULL);
Gerd Hoffmannfd42dee2010-06-04 14:08:07 +02002408}
2409
Gleb Natapov4cab9462010-12-08 13:35:08 +02002410void qemu_add_machine_init_done_notifier(Notifier *notify)
2411{
2412 notifier_list_add(&machine_init_done_notifiers, notify);
2413}
2414
2415static void qemu_run_machine_init_done_notifiers(void)
2416{
Jan Kiszka9e8dd452011-06-20 14:06:26 +02002417 notifier_list_notify(&machine_init_done_notifiers, NULL);
Gleb Natapov4cab9462010-12-08 13:35:08 +02002418}
2419
Anthony Liguori6530a972010-01-22 09:18:06 -06002420static const QEMUOption *lookup_opt(int argc, char **argv,
2421 const char **poptarg, int *poptind)
2422{
2423 const QEMUOption *popt;
2424 int optind = *poptind;
2425 char *r = argv[optind];
2426 const char *optarg;
2427
Markus Armbruster0f0bc3f2010-02-18 20:13:51 +01002428 loc_set_cmdline(argv, optind, 1);
Anthony Liguori6530a972010-01-22 09:18:06 -06002429 optind++;
2430 /* Treat --foo the same as -foo. */
2431 if (r[1] == '-')
2432 r++;
2433 popt = qemu_options;
2434 for(;;) {
2435 if (!popt->name) {
Markus Armbruster0f0bc3f2010-02-18 20:13:51 +01002436 error_report("invalid option");
Anthony Liguori6530a972010-01-22 09:18:06 -06002437 exit(1);
2438 }
2439 if (!strcmp(popt->name, r + 1))
2440 break;
2441 popt++;
2442 }
2443 if (popt->flags & HAS_ARG) {
2444 if (optind >= argc) {
Markus Armbruster0f0bc3f2010-02-18 20:13:51 +01002445 error_report("requires an argument");
Anthony Liguori6530a972010-01-22 09:18:06 -06002446 exit(1);
2447 }
2448 optarg = argv[optind++];
Markus Armbruster0f0bc3f2010-02-18 20:13:51 +01002449 loc_set_cmdline(argv, optind - 2, 2);
Anthony Liguori6530a972010-01-22 09:18:06 -06002450 } else {
2451 optarg = NULL;
2452 }
2453
2454 *poptarg = optarg;
2455 *poptind = optind;
2456
2457 return popt;
2458}
2459
Anthony Liguori07501122011-08-20 22:38:31 -05002460static gpointer malloc_and_trace(gsize n_bytes)
2461{
2462 void *ptr = malloc(n_bytes);
Frediano Ziglioa74cd8c2011-08-31 09:25:35 +02002463 trace_g_malloc(n_bytes, ptr);
Anthony Liguori07501122011-08-20 22:38:31 -05002464 return ptr;
2465}
2466
2467static gpointer realloc_and_trace(gpointer mem, gsize n_bytes)
2468{
2469 void *ptr = realloc(mem, n_bytes);
Frediano Ziglioa74cd8c2011-08-31 09:25:35 +02002470 trace_g_realloc(mem, n_bytes, ptr);
Anthony Liguori07501122011-08-20 22:38:31 -05002471 return ptr;
2472}
2473
2474static void free_and_trace(gpointer mem)
2475{
Frediano Ziglioa74cd8c2011-08-31 09:25:35 +02002476 trace_g_free(mem);
Anthony Liguori07501122011-08-20 22:38:31 -05002477 free(mem);
2478}
2479
Anthony Liguori68d98d32012-06-25 14:36:33 -05002480static int object_set_property(const char *name, const char *value, void *opaque)
2481{
2482 Object *obj = OBJECT(opaque);
2483 StringInputVisitor *siv;
2484 Error *local_err = NULL;
2485
2486 if (strcmp(name, "qom-type") == 0 || strcmp(name, "id") == 0) {
2487 return 0;
2488 }
2489
2490 siv = string_input_visitor_new(value);
2491 object_property_set(obj, string_input_get_visitor(siv), name, &local_err);
2492 string_input_visitor_cleanup(siv);
2493
2494 if (local_err) {
2495 qerror_report_err(local_err);
2496 error_free(local_err);
2497 return -1;
2498 }
2499
2500 return 0;
2501}
2502
2503static int object_create(QemuOpts *opts, void *opaque)
2504{
2505 const char *type = qemu_opt_get(opts, "qom-type");
2506 const char *id = qemu_opts_id(opts);
2507 Object *obj;
2508
2509 g_assert(type != NULL);
2510
2511 if (id == NULL) {
2512 qerror_report(QERR_MISSING_PARAMETER, "id");
2513 return -1;
2514 }
2515
2516 obj = object_new(type);
2517 if (qemu_opt_foreach(opts, object_set_property, obj, 1) < 0) {
2518 return -1;
2519 }
2520
2521 object_property_add_child(container_get(object_get_root(), "/objects"),
2522 id, obj, NULL);
2523
2524 return 0;
2525}
2526
malc902b3d52008-12-10 19:18:40 +00002527int main(int argc, char **argv, char **envp)
bellard0824d6f2003-06-24 13:42:40 +00002528{
thse4bcb142007-12-02 04:51:10 +00002529 int i;
Eduardo Habkostda1fcfd2010-04-06 19:22:07 -03002530 int snapshot, linux_boot;
Paolo Bonzini4e3de9e2010-03-10 11:38:48 +01002531 const char *icount_option = NULL;
bellard7f7f9872003-10-30 01:11:23 +00002532 const char *initrd_filename;
bellarda20dd502003-09-30 21:07:02 +00002533 const char *kernel_filename, *kernel_cmdline;
Anthony Liguori195325a2009-10-30 12:42:29 -05002534 char boot_devices[33] = "cad"; /* default to HD->floppy->CD-ROM */
aliguori3023f3322009-01-16 19:04:14 +00002535 DisplayState *ds;
malc9f227bc2012-08-27 18:33:22 +04002536 int cyls, heads, secs, translation;
2537 QemuOpts *hda_opts = NULL, *opts, *machine_opts;
Gerd Hoffmann03b0ba72010-08-20 13:52:02 +02002538 QemuOptsList *olist;
bellardcd6f1162004-05-13 22:02:20 +00002539 int optind;
Anthony Liguori6530a972010-01-22 09:18:06 -06002540 const char *optarg;
bellardd63d3072004-10-03 13:29:03 +00002541 const char *loadvm = NULL;
bellardcc1daa42005-06-05 14:49:17 +00002542 QEMUMachine *machine;
j_mayer94fc95c2007-03-05 19:44:02 +00002543 const char *cpu_model;
Paolo Bonzini7f1b17f2012-05-10 09:39:17 +02002544 const char *vga_model = "none";
ths93815bc2007-03-19 15:58:31 +00002545 const char *pid_file = NULL;
aliguori5bb79102008-10-13 03:12:02 +00002546 const char *incoming = NULL;
Jes Sorensen821601e2011-03-16 13:33:36 +01002547#ifdef CONFIG_VNC
Anthony Liguori993fbfd2009-05-21 16:54:00 -05002548 int show_vnc_port = 0;
Jes Sorensen821601e2011-03-16 13:33:36 +01002549#endif
Eduardo Habkost3ed2d9e2012-05-02 13:07:28 -03002550 bool defconfig = true;
Eduardo Habkostf29a5612012-05-02 13:07:29 -03002551 bool userconfig = true;
Matthew Fernandezc235d732011-06-07 16:32:40 +00002552 const char *log_mask = NULL;
2553 const char *log_file = NULL;
Anthony Liguori07501122011-08-20 22:38:31 -05002554 GMemVTable mem_trace = {
2555 .malloc = malloc_and_trace,
2556 .realloc = realloc_and_trace,
2557 .free = free_and_trace,
2558 };
Lluís23d15e82011-08-31 20:31:31 +02002559 const char *trace_events = NULL;
Lluíse4858972011-08-31 20:31:03 +02002560 const char *trace_file = NULL;
Stefan Hajnoczi0b5538c2011-02-26 18:38:39 +00002561
Gerd Hoffmannfd42dee2010-06-04 14:08:07 +02002562 atexit(qemu_run_exit_notifiers);
Markus Armbruster65abca02010-02-24 14:37:14 +01002563 error_set_progname(argv[0]);
2564
Anthony Liguori07501122011-08-20 22:38:31 -05002565 g_mem_set_vtable(&mem_trace);
Andreas Färberdb529aa2011-10-31 19:14:56 +01002566 if (!g_thread_supported()) {
Alon Levy42ed3722011-12-20 13:41:04 +02002567#if !GLIB_CHECK_VERSION(2, 31, 0)
Andreas Färberdb529aa2011-10-31 19:14:56 +01002568 g_thread_init(NULL);
Alon Levy42ed3722011-12-20 13:41:04 +02002569#else
2570 fprintf(stderr, "glib threading failed to initialize.\n");
2571 exit(1);
2572#endif
Andreas Färberdb529aa2011-10-31 19:14:56 +01002573 }
Anthony Liguori07501122011-08-20 22:38:31 -05002574
Andreas Färber1b71f7c2012-03-04 21:32:35 +01002575 module_call_init(MODULE_INIT_QOM);
2576
Luiz Capitulino5db9d4d2011-09-05 16:36:31 -03002577 runstate_init();
2578
Jan Kiszka68752042009-09-15 13:36:04 +02002579 init_clocks();
Paolo Bonzini2ff68d02011-09-12 16:21:44 +02002580 rtc_clock = host_clock;
Jan Kiszka68752042009-09-15 13:36:04 +02002581
malc902b3d52008-12-10 19:18:40 +00002582 qemu_cache_utils_init(envp);
2583
Blue Swirl72cf2d42009-09-12 07:36:22 +00002584 QLIST_INIT (&vm_change_state_head);
Jes Sorensenfe98ac12010-06-10 11:42:21 +02002585 os_setup_early_signal_handling();
bellardbe995c22006-06-25 16:25:21 +00002586
Anthony Liguorif80f9ec2009-05-20 18:38:09 -05002587 module_call_init(MODULE_INIT_MACHINE);
Anthony Liguori0c257432009-05-21 20:41:01 -05002588 machine = find_default_machine();
j_mayer94fc95c2007-03-05 19:44:02 +00002589 cpu_model = NULL;
aurel324fc5d072008-04-27 21:39:40 +00002590 ram_size = 0;
bellard33e39632003-07-06 17:15:21 +00002591 snapshot = 0;
malc9f227bc2012-08-27 18:33:22 +04002592 cyls = heads = secs = 0;
2593 translation = BIOS_ATA_TRANSLATION_AUTO;
bellardc4b1fcc2004-03-14 21:44:30 +00002594
aliguori268a3622009-04-21 22:30:27 +00002595 for (i = 0; i < MAX_NODES; i++) {
2596 node_mem[i] = 0;
Chegu Vinodee785fe2012-07-16 21:31:30 -07002597 node_cpumask[i] = bitmap_new(MAX_CPUMASK_BITS);
aliguori268a3622009-04-21 22:30:27 +00002598 }
2599
aliguori268a3622009-04-21 22:30:27 +00002600 nb_numa_nodes = 0;
bellard7c9d8e02005-11-15 22:16:05 +00002601 nb_nics = 0;
ths3b46e622007-09-17 08:09:54 +00002602
blueswir141bd6392008-10-05 09:56:21 +00002603 autostart= 1;
2604
Anthony Liguori292444c2010-01-21 10:57:58 -06002605 /* first pass of option parsing */
2606 optind = 1;
2607 while (optind < argc) {
2608 if (argv[optind][0] != '-') {
2609 /* disk image */
Anthony Liguori28e68d62010-01-27 10:46:00 -06002610 optind++;
Anthony Liguori292444c2010-01-21 10:57:58 -06002611 continue;
2612 } else {
2613 const QEMUOption *popt;
2614
2615 popt = lookup_opt(argc, argv, &optarg, &optind);
2616 switch (popt->index) {
2617 case QEMU_OPTION_nodefconfig:
Eduardo Habkost3ed2d9e2012-05-02 13:07:28 -03002618 defconfig = false;
Anthony Liguori292444c2010-01-21 10:57:58 -06002619 break;
Eduardo Habkostf29a5612012-05-02 13:07:29 -03002620 case QEMU_OPTION_nouserconfig:
2621 userconfig = false;
2622 break;
Anthony Liguori292444c2010-01-21 10:57:58 -06002623 }
2624 }
2625 }
2626
2627 if (defconfig) {
Kevin Wolfdcfb0932010-03-05 17:25:55 +01002628 int ret;
Eduardo Habkostf29a5612012-05-02 13:07:29 -03002629 ret = qemu_read_default_config_files(userconfig);
Eduardo Habkostb5a8fe52012-05-02 13:07:25 -03002630 if (ret < 0) {
Kevin Wolfdcfb0932010-03-05 17:25:55 +01002631 exit(1);
Anthony Liguori292444c2010-01-21 10:57:58 -06002632 }
2633 }
2634
2635 /* second pass of option parsing */
bellardcd6f1162004-05-13 22:02:20 +00002636 optind = 1;
bellard0824d6f2003-06-24 13:42:40 +00002637 for(;;) {
bellardcd6f1162004-05-13 22:02:20 +00002638 if (optind >= argc)
bellard0824d6f2003-06-24 13:42:40 +00002639 break;
Anthony Liguori6530a972010-01-22 09:18:06 -06002640 if (argv[optind][0] != '-') {
malc9f227bc2012-08-27 18:33:22 +04002641 hda_opts = drive_add(IF_DEFAULT, 0, argv[optind++], HD_OPTS);
bellardcd6f1162004-05-13 22:02:20 +00002642 } else {
2643 const QEMUOption *popt;
2644
Anthony Liguori6530a972010-01-22 09:18:06 -06002645 popt = lookup_opt(argc, argv, &optarg, &optind);
Blue Swirlad960902010-03-29 19:23:52 +00002646 if (!(popt->arch_mask & arch_type)) {
2647 printf("Option %s not supported for this target\n", popt->name);
2648 exit(1);
2649 }
bellardcd6f1162004-05-13 22:02:20 +00002650 switch(popt->index) {
bellardcc1daa42005-06-05 14:49:17 +00002651 case QEMU_OPTION_M:
Jan Kiszka9052ea62011-07-23 12:38:37 +02002652 machine = machine_parse(optarg);
bellardcc1daa42005-06-05 14:49:17 +00002653 break;
Jan Kiszkae43d5942012-10-05 14:51:40 -03002654 case QEMU_OPTION_no_kvm_irqchip: {
2655 olist = qemu_find_opts("machine");
2656 qemu_opts_parse(olist, "kernel_irqchip=off", 0);
2657 break;
2658 }
j_mayer94fc95c2007-03-05 19:44:02 +00002659 case QEMU_OPTION_cpu:
2660 /* hw initialization will check this */
Eduardo Habkostecf40be2012-03-09 16:19:07 -03002661 cpu_model = optarg;
j_mayer94fc95c2007-03-05 19:44:02 +00002662 break;
bellardcd6f1162004-05-13 22:02:20 +00002663 case QEMU_OPTION_hda:
malc9f227bc2012-08-27 18:33:22 +04002664 {
2665 char buf[256];
2666 if (cyls == 0)
2667 snprintf(buf, sizeof(buf), "%s", HD_OPTS);
2668 else
2669 snprintf(buf, sizeof(buf),
2670 "%s,cyls=%d,heads=%d,secs=%d%s",
2671 HD_OPTS , cyls, heads, secs,
2672 translation == BIOS_ATA_TRANSLATION_LBA ?
2673 ",trans=lba" :
2674 translation == BIOS_ATA_TRANSLATION_NONE ?
2675 ",trans=none" : "");
2676 drive_add(IF_DEFAULT, 0, optarg, buf);
2677 break;
2678 }
bellardcd6f1162004-05-13 22:02:20 +00002679 case QEMU_OPTION_hdb:
bellardcc1daa42005-06-05 14:49:17 +00002680 case QEMU_OPTION_hdc:
2681 case QEMU_OPTION_hdd:
Markus Armbruster2292dda2011-01-28 11:21:41 +01002682 drive_add(IF_DEFAULT, popt->index - QEMU_OPTION_hda, optarg,
2683 HD_OPTS);
bellardfc01f7e2003-06-30 10:03:06 +00002684 break;
thse4bcb142007-12-02 04:51:10 +00002685 case QEMU_OPTION_drive:
Michael Tokareve2982c32011-03-30 16:31:05 +04002686 if (drive_def(optarg) == NULL) {
2687 exit(1);
2688 }
thse4bcb142007-12-02 04:51:10 +00002689 break;
Gerd Hoffmannd058fe02009-07-31 12:25:36 +02002690 case QEMU_OPTION_set:
2691 if (qemu_set_option(optarg) != 0)
2692 exit(1);
2693 break;
Gerd Hoffmannd0fef6f2009-12-08 13:11:34 +01002694 case QEMU_OPTION_global:
2695 if (qemu_global_option(optarg) != 0)
2696 exit(1);
2697 break;
balrog3e3d5812007-04-30 02:09:25 +00002698 case QEMU_OPTION_mtdblock:
Markus Armbruster2292dda2011-01-28 11:21:41 +01002699 drive_add(IF_MTD, -1, optarg, MTD_OPTS);
balrog3e3d5812007-04-30 02:09:25 +00002700 break;
pbrooka1bb27b2007-04-06 16:49:48 +00002701 case QEMU_OPTION_sd:
Markus Armbruster2292dda2011-01-28 11:21:41 +01002702 drive_add(IF_SD, 0, optarg, SD_OPTS);
pbrooka1bb27b2007-04-06 16:49:48 +00002703 break;
j_mayer86f55662007-04-24 06:52:59 +00002704 case QEMU_OPTION_pflash:
Markus Armbruster2292dda2011-01-28 11:21:41 +01002705 drive_add(IF_PFLASH, -1, optarg, PFLASH_OPTS);
j_mayer86f55662007-04-24 06:52:59 +00002706 break;
bellardcd6f1162004-05-13 22:02:20 +00002707 case QEMU_OPTION_snapshot:
bellard33e39632003-07-06 17:15:21 +00002708 snapshot = 1;
2709 break;
bellardcd6f1162004-05-13 22:02:20 +00002710 case QEMU_OPTION_hdachs:
bellard330d0412003-07-26 18:11:40 +00002711 {
bellard330d0412003-07-26 18:11:40 +00002712 const char *p;
2713 p = optarg;
2714 cyls = strtol(p, (char **)&p, 0);
bellard46d47672004-11-16 01:45:27 +00002715 if (cyls < 1 || cyls > 16383)
2716 goto chs_fail;
bellard330d0412003-07-26 18:11:40 +00002717 if (*p != ',')
2718 goto chs_fail;
2719 p++;
2720 heads = strtol(p, (char **)&p, 0);
bellard46d47672004-11-16 01:45:27 +00002721 if (heads < 1 || heads > 16)
2722 goto chs_fail;
bellard330d0412003-07-26 18:11:40 +00002723 if (*p != ',')
2724 goto chs_fail;
2725 p++;
2726 secs = strtol(p, (char **)&p, 0);
bellard46d47672004-11-16 01:45:27 +00002727 if (secs < 1 || secs > 63)
2728 goto chs_fail;
2729 if (*p == ',') {
2730 p++;
2731 if (!strcmp(p, "none"))
2732 translation = BIOS_ATA_TRANSLATION_NONE;
2733 else if (!strcmp(p, "lba"))
2734 translation = BIOS_ATA_TRANSLATION_LBA;
2735 else if (!strcmp(p, "auto"))
2736 translation = BIOS_ATA_TRANSLATION_AUTO;
2737 else
2738 goto chs_fail;
2739 } else if (*p != '\0') {
bellardc4b1fcc2004-03-14 21:44:30 +00002740 chs_fail:
bellard46d47672004-11-16 01:45:27 +00002741 fprintf(stderr, "qemu: invalid physical CHS format\n");
2742 exit(1);
bellardc4b1fcc2004-03-14 21:44:30 +00002743 }
malc9f227bc2012-08-27 18:33:22 +04002744 if (hda_opts != NULL) {
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +02002745 char num[16];
2746 snprintf(num, sizeof(num), "%d", cyls);
2747 qemu_opt_set(hda_opts, "cyls", num);
2748 snprintf(num, sizeof(num), "%d", heads);
2749 qemu_opt_set(hda_opts, "heads", num);
2750 snprintf(num, sizeof(num), "%d", secs);
2751 qemu_opt_set(hda_opts, "secs", num);
2752 if (translation == BIOS_ATA_TRANSLATION_LBA)
2753 qemu_opt_set(hda_opts, "trans", "lba");
2754 if (translation == BIOS_ATA_TRANSLATION_NONE)
2755 qemu_opt_set(hda_opts, "trans", "none");
2756 }
bellard330d0412003-07-26 18:11:40 +00002757 }
2758 break;
aliguori268a3622009-04-21 22:30:27 +00002759 case QEMU_OPTION_numa:
2760 if (nb_numa_nodes >= MAX_NODES) {
2761 fprintf(stderr, "qemu: too many NUMA nodes\n");
2762 exit(1);
2763 }
2764 numa_add(optarg);
2765 break;
Jes Sorensen1472a952011-03-16 13:33:31 +01002766 case QEMU_OPTION_display:
2767 display_type = select_display(optarg);
2768 break;
bellardcd6f1162004-05-13 22:02:20 +00002769 case QEMU_OPTION_nographic:
Anthony Liguori993fbfd2009-05-21 16:54:00 -05002770 display_type = DT_NOGRAPHIC;
bellarda20dd502003-09-30 21:07:02 +00002771 break;
balrog4d3b6f62008-02-10 16:33:14 +00002772 case QEMU_OPTION_curses:
Jes Sorensen47b05362011-03-16 13:33:35 +01002773#ifdef CONFIG_CURSES
Anthony Liguori993fbfd2009-05-21 16:54:00 -05002774 display_type = DT_CURSES;
Jes Sorensen47b05362011-03-16 13:33:35 +01002775#else
2776 fprintf(stderr, "Curses support is disabled\n");
2777 exit(1);
balrog4d3b6f62008-02-10 16:33:14 +00002778#endif
Jes Sorensen47b05362011-03-16 13:33:35 +01002779 break;
balroga171fe32007-04-30 01:48:07 +00002780 case QEMU_OPTION_portrait:
Vasily Khoruzhick93128052011-06-17 13:04:36 +03002781 graphic_rotate = 90;
2782 break;
2783 case QEMU_OPTION_rotate:
2784 graphic_rotate = strtol(optarg, (char **) &optarg, 10);
2785 if (graphic_rotate != 0 && graphic_rotate != 90 &&
2786 graphic_rotate != 180 && graphic_rotate != 270) {
2787 fprintf(stderr,
2788 "qemu: only 90, 180, 270 deg rotation is available\n");
2789 exit(1);
2790 }
balroga171fe32007-04-30 01:48:07 +00002791 break;
bellardcd6f1162004-05-13 22:02:20 +00002792 case QEMU_OPTION_kernel:
Peter Maydella0abe472012-02-08 05:41:39 +00002793 qemu_opts_set(qemu_find_opts("machine"), 0, "kernel", optarg);
2794 break;
2795 case QEMU_OPTION_initrd:
2796 qemu_opts_set(qemu_find_opts("machine"), 0, "initrd", optarg);
bellarda20dd502003-09-30 21:07:02 +00002797 break;
bellardcd6f1162004-05-13 22:02:20 +00002798 case QEMU_OPTION_append:
Peter Maydella0abe472012-02-08 05:41:39 +00002799 qemu_opts_set(qemu_find_opts("machine"), 0, "append", optarg);
bellard313aa562003-08-10 21:52:11 +00002800 break;
Grant Likely412beee2012-03-02 11:56:38 +00002801 case QEMU_OPTION_dtb:
2802 qemu_opts_set(qemu_find_opts("machine"), 0, "dtb", optarg);
2803 break;
bellardcd6f1162004-05-13 22:02:20 +00002804 case QEMU_OPTION_cdrom:
Markus Armbruster2292dda2011-01-28 11:21:41 +01002805 drive_add(IF_DEFAULT, 2, optarg, CDROM_OPTS);
bellard36b486b2003-11-11 13:36:08 +00002806 break;
bellardcd6f1162004-05-13 22:02:20 +00002807 case QEMU_OPTION_boot:
j_mayer28c5af52007-11-11 01:50:45 +00002808 {
Jan Kiszkaef3adf62009-07-02 00:19:02 +02002809 static const char * const params[] = {
wayne3d3b8302011-07-27 18:04:55 +08002810 "order", "once", "menu",
Amos Kongac05f342012-09-07 11:11:03 +08002811 "splash", "splash-time",
2812 "reboot-timeout", NULL
Jan Kiszkaef3adf62009-07-02 00:19:02 +02002813 };
2814 char buf[sizeof(boot_devices)];
Jan Kiszkae0f084b2009-07-02 00:19:02 +02002815 char *standard_boot_devices;
Jan Kiszkaef3adf62009-07-02 00:19:02 +02002816 int legacy = 0;
2817
2818 if (!strchr(optarg, '=')) {
2819 legacy = 1;
2820 pstrcpy(buf, sizeof(buf), optarg);
2821 } else if (check_params(buf, sizeof(buf), params, optarg) < 0) {
2822 fprintf(stderr,
2823 "qemu: unknown boot parameter '%s' in '%s'\n",
2824 buf, optarg);
2825 exit(1);
2826 }
2827
2828 if (legacy ||
2829 get_param_value(buf, sizeof(buf), "order", optarg)) {
Eduardo Habkost4e9e9d62010-04-06 19:22:08 -03002830 validate_bootdevices(buf);
Jan Kiszkaef3adf62009-07-02 00:19:02 +02002831 pstrcpy(boot_devices, sizeof(boot_devices), buf);
j_mayer28c5af52007-11-11 01:50:45 +00002832 }
Jan Kiszkae0f084b2009-07-02 00:19:02 +02002833 if (!legacy) {
2834 if (get_param_value(buf, sizeof(buf),
2835 "once", optarg)) {
Eduardo Habkost4e9e9d62010-04-06 19:22:08 -03002836 validate_bootdevices(buf);
Anthony Liguori7267c092011-08-20 22:09:37 -05002837 standard_boot_devices = g_strdup(boot_devices);
Jan Kiszkae0f084b2009-07-02 00:19:02 +02002838 pstrcpy(boot_devices, sizeof(boot_devices), buf);
2839 qemu_register_reset(restore_boot_devices,
2840 standard_boot_devices);
2841 }
Jan Kiszka95387492009-07-02 00:19:02 +02002842 if (get_param_value(buf, sizeof(buf),
2843 "menu", optarg)) {
2844 if (!strcmp(buf, "on")) {
2845 boot_menu = 1;
2846 } else if (!strcmp(buf, "off")) {
2847 boot_menu = 0;
2848 } else {
2849 fprintf(stderr,
2850 "qemu: invalid option value '%s'\n",
2851 buf);
2852 exit(1);
2853 }
2854 }
wayne3d3b8302011-07-27 18:04:55 +08002855 qemu_opts_parse(qemu_find_opts("boot-opts"),
2856 optarg, 0);
Jan Kiszkae0f084b2009-07-02 00:19:02 +02002857 }
bellard36b486b2003-11-11 13:36:08 +00002858 }
2859 break;
bellardcd6f1162004-05-13 22:02:20 +00002860 case QEMU_OPTION_fda:
bellardcd6f1162004-05-13 22:02:20 +00002861 case QEMU_OPTION_fdb:
Markus Armbruster2292dda2011-01-28 11:21:41 +01002862 drive_add(IF_FLOPPY, popt->index - QEMU_OPTION_fda,
2863 optarg, FD_OPTS);
bellardc45886d2004-01-05 00:02:06 +00002864 break;
bellard52ca8d62006-06-14 16:03:05 +00002865 case QEMU_OPTION_no_fd_bootchk:
2866 fd_bootchk = 0;
2867 break;
Mark McLoughlina1ea4582009-10-08 19:58:26 +01002868 case QEMU_OPTION_netdev:
Gerd Hoffmann3329f072010-08-20 13:52:01 +02002869 if (net_client_parse(qemu_find_opts("netdev"), optarg) == -1) {
Mark McLoughlina1ea4582009-10-08 19:58:26 +01002870 exit(1);
2871 }
2872 break;
bellard7c9d8e02005-11-15 22:16:05 +00002873 case QEMU_OPTION_net:
Gerd Hoffmann3329f072010-08-20 13:52:01 +02002874 if (net_client_parse(qemu_find_opts("net"), optarg) == -1) {
bellardc4b1fcc2004-03-14 21:44:30 +00002875 exit(1);
2876 }
bellard702c6512004-04-02 21:21:32 +00002877 break;
Ronnie Sahlbergf9dadc92012-01-26 09:39:02 +11002878#ifdef CONFIG_LIBISCSI
2879 case QEMU_OPTION_iscsi:
2880 opts = qemu_opts_parse(qemu_find_opts("iscsi"), optarg, 0);
2881 if (!opts) {
2882 exit(1);
2883 }
2884 break;
2885#endif
bellardc7f74642004-08-24 21:57:12 +00002886#ifdef CONFIG_SLIRP
2887 case QEMU_OPTION_tftp:
Jan Kiszkaad196a92009-06-24 14:42:28 +02002888 legacy_tftp_prefix = optarg;
bellard9bf05442004-08-25 22:12:49 +00002889 break;
ths47d5d012007-02-20 00:05:08 +00002890 case QEMU_OPTION_bootp:
Jan Kiszkaad196a92009-06-24 14:42:28 +02002891 legacy_bootp_filename = optarg;
ths47d5d012007-02-20 00:05:08 +00002892 break;
bellard9bf05442004-08-25 22:12:49 +00002893 case QEMU_OPTION_redir:
Markus Armbruster07527062009-10-06 12:16:57 +01002894 if (net_slirp_redir(optarg) < 0)
2895 exit(1);
bellard9bf05442004-08-25 22:12:49 +00002896 break;
bellardc7f74642004-08-24 21:57:12 +00002897#endif
balrogdc72ac12008-11-09 00:04:26 +00002898 case QEMU_OPTION_bt:
Gerd Hoffmannbd3c9482009-07-15 13:59:26 +02002899 add_device_config(DEV_BT, optarg);
balrogdc72ac12008-11-09 00:04:26 +00002900 break;
bellard1d14ffa2005-10-30 18:58:22 +00002901 case QEMU_OPTION_audio_help:
Blue Swirlad960902010-03-29 19:23:52 +00002902 if (!(audio_available())) {
2903 printf("Option %s not supported for this target\n", popt->name);
2904 exit(1);
2905 }
bellard1d14ffa2005-10-30 18:58:22 +00002906 AUD_help ();
2907 exit (0);
2908 break;
2909 case QEMU_OPTION_soundhw:
Blue Swirlad960902010-03-29 19:23:52 +00002910 if (!(audio_available())) {
2911 printf("Option %s not supported for this target\n", popt->name);
2912 exit(1);
2913 }
bellard1d14ffa2005-10-30 18:58:22 +00002914 select_soundhw (optarg);
2915 break;
bellardcd6f1162004-05-13 22:02:20 +00002916 case QEMU_OPTION_h:
ths15f82202007-06-29 23:26:08 +00002917 help(0);
bellardcd6f1162004-05-13 22:02:20 +00002918 break;
pbrook9bd7e6d2009-04-07 22:58:45 +00002919 case QEMU_OPTION_version:
2920 version();
2921 exit(0);
2922 break;
aurel3200f82b82008-04-27 21:12:55 +00002923 case QEMU_OPTION_m: {
Jes Sorensen70b4f4b2011-01-05 11:41:02 +01002924 int64_t value;
Markus Armbrusterff961012012-08-15 13:12:19 +02002925 uint64_t sz;
Markus Armbruster961b42b2011-11-22 09:46:03 +01002926 char *end;
aurel3200f82b82008-04-27 21:12:55 +00002927
Markus Armbruster961b42b2011-11-22 09:46:03 +01002928 value = strtosz(optarg, &end);
2929 if (value < 0 || *end) {
aurel3200f82b82008-04-27 21:12:55 +00002930 fprintf(stderr, "qemu: invalid ram size: %s\n", optarg);
bellardcd6f1162004-05-13 22:02:20 +00002931 exit(1);
2932 }
Markus Armbrusterff961012012-08-15 13:12:19 +02002933 sz = QEMU_ALIGN_UP((uint64_t)value, 8192);
2934 ram_size = sz;
2935 if (ram_size != sz) {
aurel3200f82b82008-04-27 21:12:55 +00002936 fprintf(stderr, "qemu: ram size too large\n");
2937 exit(1);
2938 }
bellardcd6f1162004-05-13 22:02:20 +00002939 break;
aurel3200f82b82008-04-27 21:12:55 +00002940 }
Marcelo Tosattic9027602010-03-01 20:25:08 -03002941 case QEMU_OPTION_mempath:
2942 mem_path = optarg;
2943 break;
2944#ifdef MAP_POPULATE
2945 case QEMU_OPTION_mem_prealloc:
2946 mem_prealloc = 1;
2947 break;
2948#endif
bellardcd6f1162004-05-13 22:02:20 +00002949 case QEMU_OPTION_d:
Matthew Fernandezc235d732011-06-07 16:32:40 +00002950 log_mask = optarg;
2951 break;
2952 case QEMU_OPTION_D:
2953 log_file = optarg;
bellardcd6f1162004-05-13 22:02:20 +00002954 break;
bellardcd6f1162004-05-13 22:02:20 +00002955 case QEMU_OPTION_s:
Markus Armbrusteref0c4a02012-02-07 15:09:13 +01002956 add_device_config(DEV_GDB, "tcp::" DEFAULT_GDBSTUB_PORT);
bellardcd6f1162004-05-13 22:02:20 +00002957 break;
aliguori59030a82009-04-05 18:43:41 +00002958 case QEMU_OPTION_gdb:
Markus Armbrusteref0c4a02012-02-07 15:09:13 +01002959 add_device_config(DEV_GDB, optarg);
bellardcd6f1162004-05-13 22:02:20 +00002960 break;
bellardcd6f1162004-05-13 22:02:20 +00002961 case QEMU_OPTION_L:
Paul Brook5cea8592009-05-30 00:52:44 +01002962 data_dir = optarg;
bellardcd6f1162004-05-13 22:02:20 +00002963 break;
j_mayer1192dad2007-10-05 13:08:35 +00002964 case QEMU_OPTION_bios:
2965 bios_name = optarg;
2966 break;
aurel321b530a62009-04-05 20:08:59 +00002967 case QEMU_OPTION_singlestep:
2968 singlestep = 1;
2969 break;
bellardcd6f1162004-05-13 22:02:20 +00002970 case QEMU_OPTION_S:
pbrook3c07f8e2007-01-21 16:47:01 +00002971 autostart = 0;
bellardcd6f1162004-05-13 22:02:20 +00002972 break;
bellard3d11d0e2004-12-12 16:56:30 +00002973 case QEMU_OPTION_k:
2974 keyboard_layout = optarg;
2975 break;
bellardee22c2f2004-06-03 12:49:50 +00002976 case QEMU_OPTION_localtime:
2977 rtc_utc = 0;
2978 break;
malc3893c122008-09-28 00:42:05 +00002979 case QEMU_OPTION_vga:
Blue Swirla369da52011-09-27 19:15:42 +00002980 vga_model = optarg;
Paolo Bonzini7f1b17f2012-05-10 09:39:17 +02002981 default_vga = 0;
bellard1bfe8562004-07-08 21:17:50 +00002982 break;
bellarde9b137c2004-06-21 16:46:10 +00002983 case QEMU_OPTION_g:
2984 {
2985 const char *p;
2986 int w, h, depth;
2987 p = optarg;
2988 w = strtol(p, (char **)&p, 10);
2989 if (w <= 0) {
2990 graphic_error:
2991 fprintf(stderr, "qemu: invalid resolution or depth\n");
2992 exit(1);
2993 }
2994 if (*p != 'x')
2995 goto graphic_error;
2996 p++;
2997 h = strtol(p, (char **)&p, 10);
2998 if (h <= 0)
2999 goto graphic_error;
3000 if (*p == 'x') {
3001 p++;
3002 depth = strtol(p, (char **)&p, 10);
ths5fafdf22007-09-16 21:08:06 +00003003 if (depth != 8 && depth != 15 && depth != 16 &&
bellarde9b137c2004-06-21 16:46:10 +00003004 depth != 24 && depth != 32)
3005 goto graphic_error;
3006 } else if (*p == '\0') {
3007 depth = graphic_depth;
3008 } else {
3009 goto graphic_error;
3010 }
ths3b46e622007-09-17 08:09:54 +00003011
bellarde9b137c2004-06-21 16:46:10 +00003012 graphic_width = w;
3013 graphic_height = h;
3014 graphic_depth = depth;
3015 }
3016 break;
ths20d8a3e2007-02-18 17:04:49 +00003017 case QEMU_OPTION_echr:
3018 {
3019 char *r;
3020 term_escape_char = strtol(optarg, &r, 0);
3021 if (r == optarg)
3022 printf("Bad argument to echr\n");
3023 break;
3024 }
bellard82c643f2004-07-14 17:28:13 +00003025 case QEMU_OPTION_monitor:
Gerd Hoffmann6ca55822009-12-08 13:11:52 +01003026 monitor_parse(optarg, "readline");
3027 default_monitor = 0;
3028 break;
3029 case QEMU_OPTION_qmp:
3030 monitor_parse(optarg, "control");
Anthony Liguori2d114dc2010-03-21 14:14:38 -05003031 default_monitor = 0;
bellard82c643f2004-07-14 17:28:13 +00003032 break;
Gerd Hoffmann22a0e042009-12-08 13:11:51 +01003033 case QEMU_OPTION_mon:
Gerd Hoffmann3329f072010-08-20 13:52:01 +02003034 opts = qemu_opts_parse(qemu_find_opts("mon"), optarg, 1);
Gerd Hoffmann22a0e042009-12-08 13:11:51 +01003035 if (!opts) {
Gerd Hoffmann22a0e042009-12-08 13:11:51 +01003036 exit(1);
3037 }
Anthony Liguori2d114dc2010-03-21 14:14:38 -05003038 default_monitor = 0;
Gerd Hoffmann22a0e042009-12-08 13:11:51 +01003039 break;
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003040 case QEMU_OPTION_chardev:
Gerd Hoffmann3329f072010-08-20 13:52:01 +02003041 opts = qemu_opts_parse(qemu_find_opts("chardev"), optarg, 1);
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003042 if (!opts) {
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003043 exit(1);
3044 }
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003045 break;
Gautham R Shenoy74db9202010-04-29 17:44:43 +05303046 case QEMU_OPTION_fsdev:
Gerd Hoffmann03b0ba72010-08-20 13:52:02 +02003047 olist = qemu_find_opts("fsdev");
3048 if (!olist) {
3049 fprintf(stderr, "fsdev is not supported by this qemu build.\n");
3050 exit(1);
3051 }
3052 opts = qemu_opts_parse(olist, optarg, 1);
Gautham R Shenoy74db9202010-04-29 17:44:43 +05303053 if (!opts) {
3054 fprintf(stderr, "parse error: %s\n", optarg);
3055 exit(1);
3056 }
3057 break;
Gautham R Shenoy3d54abc2010-04-29 17:45:03 +05303058 case QEMU_OPTION_virtfs: {
Stefan Hajnoczie14ea472011-03-16 08:31:43 +00003059 QemuOpts *fsdev;
3060 QemuOpts *device;
M. Mohan Kumar84a87cc2011-12-14 13:58:47 +05303061 const char *writeout, *sock_fd, *socket;
Gautham R Shenoy3d54abc2010-04-29 17:45:03 +05303062
Gerd Hoffmann03b0ba72010-08-20 13:52:02 +02003063 olist = qemu_find_opts("virtfs");
3064 if (!olist) {
3065 fprintf(stderr, "virtfs is not supported by this qemu build.\n");
3066 exit(1);
3067 }
3068 opts = qemu_opts_parse(olist, optarg, 1);
Gautham R Shenoy3d54abc2010-04-29 17:45:03 +05303069 if (!opts) {
3070 fprintf(stderr, "parse error: %s\n", optarg);
3071 exit(1);
3072 }
3073
Aneesh Kumar K.Vfbcbf102011-10-13 12:28:04 +05303074 if (qemu_opt_get(opts, "fsdriver") == NULL ||
Aneesh Kumar K.V99519f02011-12-14 13:48:59 +05303075 qemu_opt_get(opts, "mount_tag") == NULL) {
3076 fprintf(stderr, "Usage: -virtfs fsdriver,mount_tag=tag.\n");
Venkateswararao Jujjuri (JV)9ce56db2010-06-14 13:34:40 -07003077 exit(1);
3078 }
Stefan Hajnoczie14ea472011-03-16 08:31:43 +00003079 fsdev = qemu_opts_create(qemu_find_opts("fsdev"),
Luiz Capitulino8be7e7e2012-03-20 15:51:57 -03003080 qemu_opt_get(opts, "mount_tag"),
3081 1, NULL);
Stefan Hajnoczie14ea472011-03-16 08:31:43 +00003082 if (!fsdev) {
3083 fprintf(stderr, "duplicate fsdev id: %s\n",
3084 qemu_opt_get(opts, "mount_tag"));
Gautham R Shenoy3d54abc2010-04-29 17:45:03 +05303085 exit(1);
3086 }
Aneesh Kumar K.Vd3ab98e2011-10-12 19:11:23 +05303087
3088 writeout = qemu_opt_get(opts, "writeout");
3089 if (writeout) {
3090#ifdef CONFIG_SYNC_FILE_RANGE
3091 qemu_opt_set(fsdev, "writeout", writeout);
3092#else
3093 fprintf(stderr, "writeout=immediate not supported on "
3094 "this platform\n");
3095 exit(1);
3096#endif
3097 }
Aneesh Kumar K.Vfbcbf102011-10-13 12:28:04 +05303098 qemu_opt_set(fsdev, "fsdriver", qemu_opt_get(opts, "fsdriver"));
Stefan Hajnoczie14ea472011-03-16 08:31:43 +00003099 qemu_opt_set(fsdev, "path", qemu_opt_get(opts, "path"));
3100 qemu_opt_set(fsdev, "security_model",
3101 qemu_opt_get(opts, "security_model"));
M. Mohan Kumar84a87cc2011-12-14 13:58:47 +05303102 socket = qemu_opt_get(opts, "socket");
3103 if (socket) {
3104 qemu_opt_set(fsdev, "socket", socket);
3105 }
M. Mohan Kumar4c793dd2011-12-14 13:49:28 +05303106 sock_fd = qemu_opt_get(opts, "sock_fd");
3107 if (sock_fd) {
3108 qemu_opt_set(fsdev, "sock_fd", sock_fd);
3109 }
Gautham R Shenoy3d54abc2010-04-29 17:45:03 +05303110
M. Mohan Kumar2c74c2c2011-10-25 12:10:39 +05303111 qemu_opt_set_bool(fsdev, "readonly",
3112 qemu_opt_get_bool(opts, "readonly", 0));
Luiz Capitulino8be7e7e2012-03-20 15:51:57 -03003113 device = qemu_opts_create(qemu_find_opts("device"), NULL, 0,
3114 NULL);
Stefan Hajnoczie14ea472011-03-16 08:31:43 +00003115 qemu_opt_set(device, "driver", "virtio-9p-pci");
3116 qemu_opt_set(device, "fsdev",
3117 qemu_opt_get(opts, "mount_tag"));
3118 qemu_opt_set(device, "mount_tag",
3119 qemu_opt_get(opts, "mount_tag"));
Gautham R Shenoy3d54abc2010-04-29 17:45:03 +05303120 break;
3121 }
Aneesh Kumar K.V9db221a2011-10-25 12:10:40 +05303122 case QEMU_OPTION_virtfs_synth: {
3123 QemuOpts *fsdev;
3124 QemuOpts *device;
3125
Luiz Capitulino8be7e7e2012-03-20 15:51:57 -03003126 fsdev = qemu_opts_create(qemu_find_opts("fsdev"), "v_synth",
3127 1, NULL);
Aneesh Kumar K.V9db221a2011-10-25 12:10:40 +05303128 if (!fsdev) {
3129 fprintf(stderr, "duplicate option: %s\n", "virtfs_synth");
3130 exit(1);
3131 }
3132 qemu_opt_set(fsdev, "fsdriver", "synth");
Aneesh Kumar K.V9db221a2011-10-25 12:10:40 +05303133
Luiz Capitulino8be7e7e2012-03-20 15:51:57 -03003134 device = qemu_opts_create(qemu_find_opts("device"), NULL, 0,
3135 NULL);
Aneesh Kumar K.V9db221a2011-10-25 12:10:40 +05303136 qemu_opt_set(device, "driver", "virtio-9p-pci");
3137 qemu_opt_set(device, "fsdev", "v_synth");
3138 qemu_opt_set(device, "mount_tag", "v_synth");
3139 break;
3140 }
bellard82c643f2004-07-14 17:28:13 +00003141 case QEMU_OPTION_serial:
Gerd Hoffmann998bbd72009-12-08 13:11:41 +01003142 add_device_config(DEV_SERIAL, optarg);
3143 default_serial = 0;
Jan Kiszka18141ed2010-03-07 11:28:40 +01003144 if (strncmp(optarg, "mon:", 4) == 0) {
3145 default_monitor = 0;
3146 }
bellard82c643f2004-07-14 17:28:13 +00003147 break;
Richard W.M. Jones9dd986c2009-04-25 13:56:19 +01003148 case QEMU_OPTION_watchdog:
Markus Armbruster09aaa162009-08-21 10:31:34 +02003149 if (watchdog) {
3150 fprintf(stderr,
3151 "qemu: only one watchdog option may be given\n");
3152 return 1;
3153 }
3154 watchdog = optarg;
Richard W.M. Jones9dd986c2009-04-25 13:56:19 +01003155 break;
3156 case QEMU_OPTION_watchdog_action:
3157 if (select_watchdog_action(optarg) == -1) {
3158 fprintf(stderr, "Unknown -watchdog-action parameter\n");
3159 exit(1);
3160 }
3161 break;
aliguori51ecf132009-01-15 20:06:40 +00003162 case QEMU_OPTION_virtiocon:
Gerd Hoffmannaee1b932009-12-08 13:11:53 +01003163 add_device_config(DEV_VIRTCON, optarg);
3164 default_virtcon = 0;
Jan Kiszka18141ed2010-03-07 11:28:40 +01003165 if (strncmp(optarg, "mon:", 4) == 0) {
3166 default_monitor = 0;
3167 }
aliguori51ecf132009-01-15 20:06:40 +00003168 break;
bellard6508fe52005-01-15 12:02:56 +00003169 case QEMU_OPTION_parallel:
Gerd Hoffmann6a5e8b02009-12-08 13:11:42 +01003170 add_device_config(DEV_PARALLEL, optarg);
3171 default_parallel = 0;
Jan Kiszka18141ed2010-03-07 11:28:40 +01003172 if (strncmp(optarg, "mon:", 4) == 0) {
3173 default_monitor = 0;
3174 }
bellard6508fe52005-01-15 12:02:56 +00003175 break;
H. Peter Anvinc9f398e2009-12-29 13:51:36 -08003176 case QEMU_OPTION_debugcon:
3177 add_device_config(DEV_DEBUGCON, optarg);
3178 break;
bellardd63d3072004-10-03 13:29:03 +00003179 case QEMU_OPTION_loadvm:
3180 loadvm = optarg;
3181 break;
3182 case QEMU_OPTION_full_screen:
3183 full_screen = 1;
3184 break;
ths667acca2006-12-11 02:08:05 +00003185#ifdef CONFIG_SDL
ths43523e92007-02-18 18:19:32 +00003186 case QEMU_OPTION_no_frame:
3187 no_frame = 1;
3188 break;
ths3780e192007-06-21 21:08:02 +00003189 case QEMU_OPTION_alt_grab:
3190 alt_grab = 1;
3191 break;
Dustin Kirkland0ca9f8a2009-09-17 15:48:04 -05003192 case QEMU_OPTION_ctrl_grab:
3193 ctrl_grab = 1;
3194 break;
ths667acca2006-12-11 02:08:05 +00003195 case QEMU_OPTION_no_quit:
3196 no_quit = 1;
3197 break;
aliguori7d957bd2009-01-15 22:14:11 +00003198 case QEMU_OPTION_sdl:
Anthony Liguori993fbfd2009-05-21 16:54:00 -05003199 display_type = DT_SDL;
aliguori7d957bd2009-01-15 22:14:11 +00003200 break;
Jes Sorensen58fc0962011-03-16 13:33:34 +01003201#else
3202 case QEMU_OPTION_no_frame:
3203 case QEMU_OPTION_alt_grab:
3204 case QEMU_OPTION_ctrl_grab:
3205 case QEMU_OPTION_no_quit:
3206 case QEMU_OPTION_sdl:
3207 fprintf(stderr, "SDL support is disabled\n");
3208 exit(1);
ths667acca2006-12-11 02:08:05 +00003209#endif
bellardf7cce892004-12-08 22:21:25 +00003210 case QEMU_OPTION_pidfile:
ths93815bc2007-03-19 15:58:31 +00003211 pid_file = optarg;
bellardf7cce892004-12-08 22:21:25 +00003212 break;
bellarda09db212005-04-30 16:10:35 +00003213 case QEMU_OPTION_win2k_hack:
3214 win2k_install_hack = 1;
3215 break;
Jan Kiszka433acf02012-01-23 20:15:12 +01003216 case QEMU_OPTION_rtc_td_hack: {
3217 static GlobalProperty slew_lost_ticks[] = {
3218 {
3219 .driver = "mc146818rtc",
3220 .property = "lost_tick_policy",
3221 .value = "slew",
3222 },
3223 { /* end of list */ }
3224 };
3225
3226 qdev_prop_register_global_list(slew_lost_ticks);
aliguori73822ec2009-01-15 20:11:34 +00003227 break;
Jan Kiszka433acf02012-01-23 20:15:12 +01003228 }
aliguori8a92ea22009-02-27 20:12:36 +00003229 case QEMU_OPTION_acpitable:
Blue Swirlde06f8d2010-03-29 19:23:50 +00003230 do_acpitable_option(optarg);
aliguori8a92ea22009-02-27 20:12:36 +00003231 break;
aliguorib6f6e3d2009-04-17 18:59:56 +00003232 case QEMU_OPTION_smbios:
Blue Swirlde06f8d2010-03-29 19:23:50 +00003233 do_smbios_option(optarg);
aliguorib6f6e3d2009-04-17 18:59:56 +00003234 break;
aliguori7ba1e612008-11-05 16:04:33 +00003235 case QEMU_OPTION_enable_kvm:
Anthony PERARD303d4e82010-09-21 20:05:31 +01003236 olist = qemu_find_opts("machine");
Anthony PERARD303d4e82010-09-21 20:05:31 +01003237 qemu_opts_parse(olist, "accel=kvm", 0);
3238 break;
3239 case QEMU_OPTION_machine:
3240 olist = qemu_find_opts("machine");
Jan Kiszka9052ea62011-07-23 12:38:37 +02003241 opts = qemu_opts_parse(olist, optarg, 1);
Anthony PERARD303d4e82010-09-21 20:05:31 +01003242 if (!opts) {
3243 fprintf(stderr, "parse error: %s\n", optarg);
3244 exit(1);
3245 }
Jan Kiszka2645c6d2011-07-25 18:11:20 +02003246 optarg = qemu_opt_get(opts, "type");
3247 if (optarg) {
3248 machine = machine_parse(optarg);
3249 }
aliguori7ba1e612008-11-05 16:04:33 +00003250 break;
Jan Kiszkaa0dac022012-10-05 14:51:45 -03003251 case QEMU_OPTION_no_kvm:
3252 olist = qemu_find_opts("machine");
3253 qemu_opts_parse(olist, "accel=tcg", 0);
3254 break;
Jan Kiszka4086bde2012-10-05 14:51:41 -03003255 case QEMU_OPTION_no_kvm_pit: {
3256 fprintf(stderr, "Warning: KVM PIT can no longer be disabled "
3257 "separately.\n");
3258 break;
3259 }
Jan Kiszkac21fb4f2012-10-05 14:51:42 -03003260 case QEMU_OPTION_no_kvm_pit_reinjection: {
3261 static GlobalProperty kvm_pit_lost_tick_policy[] = {
3262 {
3263 .driver = "kvm-pit",
3264 .property = "lost_tick_policy",
3265 .value = "discard",
3266 },
3267 { /* end of list */ }
3268 };
3269
3270 fprintf(stderr, "Warning: option deprecated, use "
3271 "lost_tick_policy property of kvm-pit instead.\n");
3272 qdev_prop_register_global_list(kvm_pit_lost_tick_policy);
3273 break;
3274 }
bellardbb36d472005-11-05 14:22:28 +00003275 case QEMU_OPTION_usb:
zhlcindy@gmail.com094b2872012-09-02 19:25:28 +00003276 machine_opts = qemu_opts_find(qemu_find_opts("machine"), 0);
3277 if (machine_opts) {
3278 qemu_opt_set_bool(machine_opts, "usb", true);
3279 }
bellardbb36d472005-11-05 14:22:28 +00003280 break;
bellarda594cfb2005-11-06 16:13:29 +00003281 case QEMU_OPTION_usbdevice:
zhlcindy@gmail.com094b2872012-09-02 19:25:28 +00003282 machine_opts = qemu_opts_find(qemu_find_opts("machine"), 0);
3283 if (machine_opts) {
3284 qemu_opt_set_bool(machine_opts, "usb", true);
3285 }
Gerd Hoffmannbd3c9482009-07-15 13:59:26 +02003286 add_device_config(DEV_USB, optarg);
3287 break;
3288 case QEMU_OPTION_device:
Gerd Hoffmann3329f072010-08-20 13:52:01 +02003289 if (!qemu_opts_parse(qemu_find_opts("device"), optarg, 1)) {
Gerd Hoffmannf31d07d2009-07-31 12:25:37 +02003290 exit(1);
3291 }
bellarda594cfb2005-11-06 16:13:29 +00003292 break;
bellard6a00d602005-11-21 23:25:50 +00003293 case QEMU_OPTION_smp:
Andre Przywaradc6b1c02009-08-19 15:42:40 +02003294 smp_parse(optarg);
aliguorib2097002008-10-07 20:39:39 +00003295 if (smp_cpus < 1) {
bellard6a00d602005-11-21 23:25:50 +00003296 fprintf(stderr, "Invalid number of CPUs\n");
3297 exit(1);
3298 }
Jes Sorensen6be68d72009-07-23 17:03:42 +02003299 if (max_cpus < smp_cpus) {
3300 fprintf(stderr, "maxcpus must be equal to or greater than "
3301 "smp\n");
3302 exit(1);
3303 }
3304 if (max_cpus > 255) {
3305 fprintf(stderr, "Unsupported number of maxcpus\n");
3306 exit(1);
3307 }
bellard6a00d602005-11-21 23:25:50 +00003308 break;
bellard24236862006-04-30 21:28:36 +00003309 case QEMU_OPTION_vnc:
Jes Sorensen821601e2011-03-16 13:33:36 +01003310#ifdef CONFIG_VNC
Gerd Hoffmann6b62dc22010-08-25 10:51:06 +02003311 display_remote++;
Jes Sorensen821601e2011-03-16 13:33:36 +01003312 vnc_display = optarg;
3313#else
3314 fprintf(stderr, "VNC support is disabled\n");
3315 exit(1);
3316#endif
3317 break;
bellard6515b202006-05-03 22:02:44 +00003318 case QEMU_OPTION_no_acpi:
3319 acpi_enabled = 0;
3320 break;
aliguori16b29ae2008-12-17 23:28:44 +00003321 case QEMU_OPTION_no_hpet:
3322 no_hpet = 1;
3323 break;
Markus Armbruster7d4c3d52009-06-26 19:15:14 +02003324 case QEMU_OPTION_balloon:
3325 if (balloon_parse(optarg) < 0) {
3326 fprintf(stderr, "Unknown -balloon argument %s\n", optarg);
3327 exit(1);
3328 }
Eduardo Habkostdf97b922009-06-10 16:34:08 -03003329 break;
bellardd1beab82006-10-02 19:44:22 +00003330 case QEMU_OPTION_no_reboot:
3331 no_reboot = 1;
3332 break;
aurel32b2f76162008-04-11 21:35:52 +00003333 case QEMU_OPTION_no_shutdown:
3334 no_shutdown = 1;
3335 break;
balrog9467cd42007-05-01 01:34:14 +00003336 case QEMU_OPTION_show_cursor:
3337 cursor_hide = 0;
3338 break;
blueswir18fcb1b92008-09-18 18:29:08 +00003339 case QEMU_OPTION_uuid:
3340 if(qemu_uuid_parse(optarg, qemu_uuid) < 0) {
3341 fprintf(stderr, "Fail to parse UUID string."
3342 " Wrong format.\n");
3343 exit(1);
3344 }
3345 break;
ths9ae02552007-01-05 17:39:04 +00003346 case QEMU_OPTION_option_rom:
3347 if (nb_option_roms >= MAX_OPTION_ROMS) {
3348 fprintf(stderr, "Too many option ROMs\n");
3349 exit(1);
3350 }
Gleb Natapov2e55e842010-12-08 13:35:07 +02003351 opts = qemu_opts_parse(qemu_find_opts("option-rom"), optarg, 1);
3352 option_rom[nb_option_roms].name = qemu_opt_get(opts, "romfile");
3353 option_rom[nb_option_roms].bootindex =
3354 qemu_opt_get_number(opts, "bootindex", -1);
3355 if (!option_rom[nb_option_roms].name) {
3356 fprintf(stderr, "Option ROM file is not specified\n");
3357 exit(1);
3358 }
ths9ae02552007-01-05 17:39:04 +00003359 nb_option_roms++;
3360 break;
pbrook8e716212007-01-20 17:12:09 +00003361 case QEMU_OPTION_semihosting:
3362 semihosting_enabled = 1;
3363 break;
Jan Kiszka88eed342012-10-05 14:51:44 -03003364 case QEMU_OPTION_tdf:
3365 fprintf(stderr, "Warning: user space PIT time drift fix "
3366 "is no longer supported.\n");
3367 break;
thsc35734b2007-03-19 15:17:08 +00003368 case QEMU_OPTION_name:
Anthony Liguori7267c092011-08-20 22:09:37 -05003369 qemu_name = g_strdup(optarg);
Andi Kleen18894652009-07-02 09:34:17 +02003370 {
3371 char *p = strchr(qemu_name, ',');
3372 if (p != NULL) {
3373 *p++ = 0;
3374 if (strncmp(p, "process=", 8)) {
Aurelien Jarno5697f6a2010-12-27 18:29:20 +01003375 fprintf(stderr, "Unknown subargument %s to -name\n", p);
Andi Kleen18894652009-07-02 09:34:17 +02003376 exit(1);
3377 }
3378 p += 8;
Jes Sorensence798cf2010-06-10 11:42:31 +02003379 os_set_proc_name(p);
Andi Kleen18894652009-07-02 09:34:17 +02003380 }
3381 }
thsc35734b2007-03-19 15:17:08 +00003382 break;
blueswir166508602007-05-01 14:16:52 +00003383 case QEMU_OPTION_prom_env:
3384 if (nb_prom_envs >= MAX_PROM_ENVS) {
3385 fprintf(stderr, "Too many prom variables\n");
3386 exit(1);
3387 }
3388 prom_envs[nb_prom_envs] = optarg;
3389 nb_prom_envs++;
3390 break;
balrog2b8f2d42007-07-27 22:08:46 +00003391 case QEMU_OPTION_old_param:
3392 old_param = 1;
ths05ebd532008-01-08 19:32:16 +00003393 break;
thsf3dcfad2007-08-24 01:26:02 +00003394 case QEMU_OPTION_clock:
3395 configure_alarms(optarg);
3396 break;
bellard7e0af5d02007-11-07 16:24:33 +00003397 case QEMU_OPTION_startdate:
Jan Kiszka1ed2fc12009-09-15 13:36:04 +02003398 configure_rtc_date_offset(optarg, 1);
3399 break;
3400 case QEMU_OPTION_rtc:
Gerd Hoffmann3329f072010-08-20 13:52:01 +02003401 opts = qemu_opts_parse(qemu_find_opts("rtc"), optarg, 0);
Jan Kiszka1ed2fc12009-09-15 13:36:04 +02003402 if (!opts) {
Jan Kiszka1ed2fc12009-09-15 13:36:04 +02003403 exit(1);
bellard7e0af5d02007-11-07 16:24:33 +00003404 }
Jan Kiszka1ed2fc12009-09-15 13:36:04 +02003405 configure_rtc(opts);
bellard7e0af5d02007-11-07 16:24:33 +00003406 break;
bellard26a5f132008-05-28 12:30:31 +00003407 case QEMU_OPTION_tb_size:
Jan Kiszkad5ab9712011-08-02 16:10:21 +02003408 tcg_tb_size = strtol(optarg, NULL, 0);
3409 if (tcg_tb_size < 0) {
3410 tcg_tb_size = 0;
3411 }
bellard26a5f132008-05-28 12:30:31 +00003412 break;
pbrook2e70f6e2008-06-29 01:03:05 +00003413 case QEMU_OPTION_icount:
Paolo Bonzini4e3de9e2010-03-10 11:38:48 +01003414 icount_option = optarg;
pbrook2e70f6e2008-06-29 01:03:05 +00003415 break;
aliguori5bb79102008-10-13 03:12:02 +00003416 case QEMU_OPTION_incoming:
3417 incoming = optarg;
Stefano Stabellini81323a62012-01-18 12:23:13 +00003418 runstate_set(RUN_STATE_INMIGRATE);
aliguori5bb79102008-10-13 03:12:02 +00003419 break;
Gerd Hoffmannd8c208d2009-12-08 13:11:46 +01003420 case QEMU_OPTION_nodefaults:
3421 default_serial = 0;
3422 default_parallel = 0;
Gerd Hoffmannaee1b932009-12-08 13:11:53 +01003423 default_virtcon = 0;
Gerd Hoffmannd8c208d2009-12-08 13:11:46 +01003424 default_monitor = 0;
Gerd Hoffmanncb4522c2009-12-08 13:11:47 +01003425 default_net = 0;
Gerd Hoffmannac33f8f2009-12-16 14:25:39 +01003426 default_floppy = 0;
3427 default_cdrom = 0;
3428 default_sdcard = 0;
Paolo Bonzini7f1b17f2012-05-10 09:39:17 +02003429 default_vga = 0;
Gerd Hoffmannd8c208d2009-12-08 13:11:46 +01003430 break;
aliguorie37630c2009-04-22 15:19:10 +00003431 case QEMU_OPTION_xen_domid:
Blue Swirlad960902010-03-29 19:23:52 +00003432 if (!(xen_available())) {
3433 printf("Option %s not supported for this target\n", popt->name);
3434 exit(1);
3435 }
aliguorie37630c2009-04-22 15:19:10 +00003436 xen_domid = atoi(optarg);
3437 break;
3438 case QEMU_OPTION_xen_create:
Blue Swirlad960902010-03-29 19:23:52 +00003439 if (!(xen_available())) {
3440 printf("Option %s not supported for this target\n", popt->name);
3441 exit(1);
3442 }
aliguorie37630c2009-04-22 15:19:10 +00003443 xen_mode = XEN_CREATE;
3444 break;
3445 case QEMU_OPTION_xen_attach:
Blue Swirlad960902010-03-29 19:23:52 +00003446 if (!(xen_available())) {
3447 printf("Option %s not supported for this target\n", popt->name);
3448 exit(1);
3449 }
aliguorie37630c2009-04-22 15:19:10 +00003450 xen_mode = XEN_ATTACH;
3451 break;
Prerna Saxenaab6540d2010-08-09 11:48:32 +01003452 case QEMU_OPTION_trace:
Lluíse4858972011-08-31 20:31:03 +02003453 {
Prerna Saxenaab6540d2010-08-09 11:48:32 +01003454 opts = qemu_opts_parse(qemu_find_opts("trace"), optarg, 0);
Lluíse4858972011-08-31 20:31:03 +02003455 if (!opts) {
3456 exit(1);
Prerna Saxenaab6540d2010-08-09 11:48:32 +01003457 }
Lluís23d15e82011-08-31 20:31:31 +02003458 trace_events = qemu_opt_get(opts, "events");
Lluíse4858972011-08-31 20:31:03 +02003459 trace_file = qemu_opt_get(opts, "file");
Prerna Saxenaab6540d2010-08-09 11:48:32 +01003460 break;
Lluíse4858972011-08-31 20:31:03 +02003461 }
Gerd Hoffmann715a6642009-10-14 10:39:28 +02003462 case QEMU_OPTION_readconfig:
3463 {
Kevin Wolfdcfb0932010-03-05 17:25:55 +01003464 int ret = qemu_read_config_file(optarg);
3465 if (ret < 0) {
3466 fprintf(stderr, "read config %s: %s\n", optarg,
3467 strerror(-ret));
Gerd Hoffmann715a6642009-10-14 10:39:28 +02003468 exit(1);
3469 }
Gerd Hoffmann715a6642009-10-14 10:39:28 +02003470 break;
3471 }
Gerd Hoffmann29b00402010-03-11 11:13:27 -03003472 case QEMU_OPTION_spice:
3473 olist = qemu_find_opts("spice");
3474 if (!olist) {
3475 fprintf(stderr, "spice is not supported by this qemu build.\n");
3476 exit(1);
3477 }
3478 opts = qemu_opts_parse(olist, optarg, 0);
3479 if (!opts) {
3480 fprintf(stderr, "parse error: %s\n", optarg);
3481 exit(1);
3482 }
3483 break;
Gerd Hoffmann715a6642009-10-14 10:39:28 +02003484 case QEMU_OPTION_writeconfig:
3485 {
3486 FILE *fp;
3487 if (strcmp(optarg, "-") == 0) {
3488 fp = stdout;
3489 } else {
3490 fp = fopen(optarg, "w");
3491 if (fp == NULL) {
3492 fprintf(stderr, "open %s: %s\n", optarg, strerror(errno));
3493 exit(1);
3494 }
3495 }
3496 qemu_config_write(fp);
3497 fclose(fp);
3498 break;
3499 }
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +02003500 case QEMU_OPTION_qtest:
3501 qtest_chrdev = optarg;
3502 break;
3503 case QEMU_OPTION_qtest_log:
3504 qtest_log = optarg;
3505 break;
Eduardo Otubo7d76ad42012-08-14 18:44:08 -03003506 case QEMU_OPTION_sandbox:
3507 opts = qemu_opts_parse(qemu_find_opts("sandbox"), optarg, 1);
3508 if (!opts) {
3509 exit(0);
3510 }
3511 break;
Corey Bryant587ed6b2012-10-18 15:19:34 -04003512 case QEMU_OPTION_add_fd:
3513#ifndef _WIN32
3514 opts = qemu_opts_parse(qemu_find_opts("add-fd"), optarg, 0);
3515 if (!opts) {
3516 exit(0);
3517 }
3518#else
3519 error_report("File descriptor passing is disabled on this "
3520 "platform");
3521 exit(1);
3522#endif
3523 break;
Anthony Liguori68d98d32012-06-25 14:36:33 -05003524 case QEMU_OPTION_object:
3525 opts = qemu_opts_parse(qemu_find_opts("object"), optarg, 1);
3526 break;
Jes Sorensen59a52642010-06-10 11:42:25 +02003527 default:
3528 os_parse_cmd_args(popt->index, optarg);
bellardcd6f1162004-05-13 22:02:20 +00003529 }
bellard0824d6f2003-06-24 13:42:40 +00003530 }
3531 }
Markus Armbruster0f0bc3f2010-02-18 20:13:51 +01003532 loc_set_none();
bellard330d0412003-07-26 18:11:40 +00003533
Paolo Bonzini1c537862012-10-30 00:17:12 +01003534 if (qemu_init_main_loop()) {
3535 fprintf(stderr, "qemu_init_main_loop failed\n");
3536 exit(1);
3537 }
3538
Eduardo Otubo7d76ad42012-08-14 18:44:08 -03003539 if (qemu_opts_foreach(qemu_find_opts("sandbox"), parse_sandbox, NULL, 0)) {
3540 exit(1);
3541 }
3542
Corey Bryant587ed6b2012-10-18 15:19:34 -04003543#ifndef _WIN32
3544 if (qemu_opts_foreach(qemu_find_opts("add-fd"), parse_add_fd, NULL, 1)) {
3545 exit(1);
3546 }
3547
3548 if (qemu_opts_foreach(qemu_find_opts("add-fd"), cleanup_add_fd, NULL, 1)) {
3549 exit(1);
3550 }
3551#endif
3552
Dunrong Huangfb7c2692012-07-24 00:42:20 +08003553 if (machine == NULL) {
3554 fprintf(stderr, "No machine found.\n");
3555 exit(1);
3556 }
3557
Crístian Viana93bfef42012-05-30 00:35:51 -03003558 if (machine->hw_version) {
3559 qemu_set_version(machine->hw_version);
3560 }
3561
Anthony Liguori68d98d32012-06-25 14:36:33 -05003562 if (qemu_opts_foreach(qemu_find_opts("object"),
3563 object_create, NULL, 0) != 0) {
3564 exit(1);
3565 }
3566
Eduardo Habkostecf40be2012-03-09 16:19:07 -03003567 /* Init CPU def lists, based on config
3568 * - Must be called after all the qemu_read_config_file() calls
3569 * - Must be called before list_cpus()
3570 * - Must be called before machine->init()
3571 */
3572 cpudef_init();
3573
Peter Maydellc8057f92012-08-02 13:45:54 +01003574 if (cpu_model && is_help_option(cpu_model)) {
Eduardo Habkost1d6528a2012-03-21 09:33:40 -03003575 list_cpus(stdout, &fprintf, cpu_model);
Eduardo Habkostecf40be2012-03-09 16:19:07 -03003576 exit(0);
3577 }
3578
Matthew Fernandezc235d732011-06-07 16:32:40 +00003579 /* Open the logfile at this point, if necessary. We can't open the logfile
3580 * when encountering either of the logging options (-d or -D) because the
3581 * other one may be encountered later on the command line, changing the
3582 * location or level of logging.
3583 */
3584 if (log_mask) {
3585 if (log_file) {
3586 set_cpu_log_filename(log_file);
3587 }
3588 set_cpu_log(log_mask);
3589 }
3590
Lluís23d15e82011-08-31 20:31:31 +02003591 if (!trace_backend_init(trace_events, trace_file)) {
Lluíse4858972011-08-31 20:31:03 +02003592 exit(1);
Stefan Hajnoczi31d3c9b2011-03-13 20:14:30 +00003593 }
Stefan Hajnoczi0b5538c2011-02-26 18:38:39 +00003594
Paul Brook5cea8592009-05-30 00:52:44 +01003595 /* If no data_dir is specified then try to find it relative to the
3596 executable path. */
3597 if (!data_dir) {
Jes Sorensen61705402010-06-10 11:42:23 +02003598 data_dir = os_find_datadir(argv[0]);
Paul Brook5cea8592009-05-30 00:52:44 +01003599 }
Stefan Weil60474fb2011-09-04 15:17:46 +02003600 /* If all else fails use the install path specified when building. */
Paul Brook5cea8592009-05-30 00:52:44 +01003601 if (!data_dir) {
Paolo Bonzini1dabe052010-05-26 16:08:25 +02003602 data_dir = CONFIG_QEMU_DATADIR;
Paul Brook5cea8592009-05-30 00:52:44 +01003603 }
3604
Jes Sorensen6be68d72009-07-23 17:03:42 +02003605 /*
3606 * Default to max_cpus = smp_cpus, in case the user doesn't
3607 * specify a max_cpus value.
3608 */
3609 if (!max_cpus)
3610 max_cpus = smp_cpus;
3611
balrog3d878ca2008-10-28 10:59:59 +00003612 machine->max_cpus = machine->max_cpus ?: 1; /* Default to UP */
aliguorib2097002008-10-07 20:39:39 +00003613 if (smp_cpus > machine->max_cpus) {
3614 fprintf(stderr, "Number of SMP cpus requested (%d), exceeds max cpus "
3615 "supported by machine `%s' (%d)\n", smp_cpus, machine->name,
3616 machine->max_cpus);
3617 exit(1);
3618 }
3619
Anthony PERARD67b724e2010-11-22 15:44:15 +00003620 /*
3621 * Get the default machine options from the machine if it is not already
3622 * specified either by the configuration file or by the command line.
3623 */
3624 if (machine->default_machine_opts) {
Jan Kiszka25de5932012-01-27 19:55:43 +01003625 qemu_opts_set_defaults(qemu_find_opts("machine"),
3626 machine->default_machine_opts, 0);
Anthony PERARD67b724e2010-11-22 15:44:15 +00003627 }
3628
Gerd Hoffmann3329f072010-08-20 13:52:01 +02003629 qemu_opts_foreach(qemu_find_opts("device"), default_driver_check, NULL, 0);
3630 qemu_opts_foreach(qemu_find_opts("global"), default_driver_check, NULL, 0);
Gerd Hoffmann998bbd72009-12-08 13:11:41 +01003631
Gerd Hoffmann986c5f72009-12-08 13:11:54 +01003632 if (machine->no_serial) {
3633 default_serial = 0;
3634 }
3635 if (machine->no_parallel) {
3636 default_parallel = 0;
3637 }
3638 if (!machine->use_virtcon) {
3639 default_virtcon = 0;
3640 }
Gerd Hoffmannac33f8f2009-12-16 14:25:39 +01003641 if (machine->no_floppy) {
3642 default_floppy = 0;
3643 }
3644 if (machine->no_cdrom) {
3645 default_cdrom = 0;
3646 }
3647 if (machine->no_sdcard) {
3648 default_sdcard = 0;
3649 }
Gerd Hoffmann986c5f72009-12-08 13:11:54 +01003650
Anthony Liguori993fbfd2009-05-21 16:54:00 -05003651 if (display_type == DT_NOGRAPHIC) {
Gerd Hoffmann6a5e8b02009-12-08 13:11:42 +01003652 if (default_parallel)
3653 add_device_config(DEV_PARALLEL, "null");
Gerd Hoffmanne1c09172009-12-08 13:11:44 +01003654 if (default_serial && default_monitor) {
3655 add_device_config(DEV_SERIAL, "mon:stdio");
Gerd Hoffmann986c5f72009-12-08 13:11:54 +01003656 } else if (default_virtcon && default_monitor) {
3657 add_device_config(DEV_VIRTCON, "mon:stdio");
Gerd Hoffmanne1c09172009-12-08 13:11:44 +01003658 } else {
3659 if (default_serial)
3660 add_device_config(DEV_SERIAL, "stdio");
Gerd Hoffmann986c5f72009-12-08 13:11:54 +01003661 if (default_virtcon)
3662 add_device_config(DEV_VIRTCON, "stdio");
Gerd Hoffmanne1c09172009-12-08 13:11:44 +01003663 if (default_monitor)
Gerd Hoffmann6ca55822009-12-08 13:11:52 +01003664 monitor_parse("stdio", "readline");
Gerd Hoffmanne1c09172009-12-08 13:11:44 +01003665 }
Gerd Hoffmann998bbd72009-12-08 13:11:41 +01003666 } else {
3667 if (default_serial)
3668 add_device_config(DEV_SERIAL, "vc:80Cx24C");
Gerd Hoffmann6a5e8b02009-12-08 13:11:42 +01003669 if (default_parallel)
3670 add_device_config(DEV_PARALLEL, "vc:80Cx24C");
Gerd Hoffmannabdeed02009-12-08 13:11:43 +01003671 if (default_monitor)
Gerd Hoffmann6ca55822009-12-08 13:11:52 +01003672 monitor_parse("vc:80Cx24C", "readline");
Alexander Graf38536da2009-12-17 13:06:08 +01003673 if (default_virtcon)
3674 add_device_config(DEV_VIRTCON, "vc:80Cx24C");
aliguoribc0129d2008-08-01 15:12:34 +00003675 }
3676
TeLeMana5829fd2010-04-15 12:37:55 +08003677 socket_init();
3678
Gerd Hoffmann3329f072010-08-20 13:52:01 +02003679 if (qemu_opts_foreach(qemu_find_opts("chardev"), chardev_init_func, NULL, 1) != 0)
Gerd Hoffmann1a688d32009-12-08 13:11:36 +01003680 exit(1);
Venkateswararao Jujjuri (JV)758e8e32010-06-14 13:34:41 -07003681#ifdef CONFIG_VIRTFS
Gerd Hoffmann3329f072010-08-20 13:52:01 +02003682 if (qemu_opts_foreach(qemu_find_opts("fsdev"), fsdev_init_func, NULL, 1) != 0) {
Gautham R Shenoy74db9202010-04-29 17:44:43 +05303683 exit(1);
3684 }
3685#endif
Gerd Hoffmann1a688d32009-12-08 13:11:36 +01003686
Jes Sorenseneb505be2010-06-10 11:42:28 +02003687 os_daemonize();
ths71e3ceb2006-12-22 02:11:31 +00003688
thsaa26bb22007-03-25 21:33:06 +00003689 if (pid_file && qemu_create_pidfile(pid_file) != 0) {
Jes Sorenseneb505be2010-06-10 11:42:28 +02003690 os_pidfile_error();
ths93815bc2007-03-19 15:58:31 +00003691 exit(1);
3692 }
3693
Jan Kiszka0ac543d2011-08-15 16:18:57 -07003694 /* init the memory */
3695 if (ram_size == 0) {
3696 ram_size = DEFAULT_RAM_SIZE * 1024 * 1024;
3697 }
3698
Bruce Rogers3d1d9652012-08-09 12:47:40 -06003699 if (qemu_opts_foreach(qemu_find_opts("device"), device_help_func, NULL, 0)
3700 != 0) {
3701 exit(0);
3702 }
3703
Anthony PERARD303d4e82010-09-21 20:05:31 +01003704 configure_accelerator();
Marcelo Tosatti214910a2009-09-18 02:41:23 -03003705
Peter Maydell967c0da2012-02-22 22:40:00 +00003706 machine_opts = qemu_opts_find(qemu_find_opts("machine"), 0);
3707 if (machine_opts) {
3708 kernel_filename = qemu_opt_get(machine_opts, "kernel");
3709 initrd_filename = qemu_opt_get(machine_opts, "initrd");
3710 kernel_cmdline = qemu_opt_get(machine_opts, "append");
3711 } else {
3712 kernel_filename = initrd_filename = kernel_cmdline = NULL;
3713 }
3714
Peter Maydella0abe472012-02-08 05:41:39 +00003715 if (!kernel_cmdline) {
3716 kernel_cmdline = "";
3717 }
3718
bellarda20dd502003-09-30 21:07:02 +00003719 linux_boot = (kernel_filename != NULL);
balrog6c41b272007-11-17 12:12:29 +00003720
thsf8d39c02008-07-03 10:01:15 +00003721 if (!linux_boot && *kernel_cmdline != '\0') {
3722 fprintf(stderr, "-append only allowed with -kernel option\n");
3723 exit(1);
3724 }
3725
3726 if (!linux_boot && initrd_filename != NULL) {
3727 fprintf(stderr, "-initrd only allowed with -kernel option\n");
3728 exit(1);
3729 }
3730
Grant Likely412beee2012-03-02 11:56:38 +00003731 if (!linux_boot && machine_opts && qemu_opt_get(machine_opts, "dtb")) {
3732 fprintf(stderr, "-dtb only allowed with -kernel option\n");
3733 exit(1);
3734 }
3735
Jes Sorensen9156d762010-06-10 11:42:30 +02003736 os_set_line_buffering();
ths3b46e622007-09-17 08:09:54 +00003737
Paolo Bonzini49cf5722012-11-02 15:43:24 +01003738 qemu_init_cpu_loop();
3739 qemu_mutex_lock_iothread();
3740
Alon Levyad1be892012-03-14 20:33:37 +02003741#ifdef CONFIG_SPICE
3742 /* spice needs the timers to be initialized by this point */
3743 qemu_spice_init();
3744#endif
3745
Max Filippov0abe9052011-11-10 15:38:42 +04003746 if (icount_option && (kvm_enabled() || xen_enabled())) {
3747 fprintf(stderr, "-icount is not allowed with kvm or xen\n");
3748 exit(1);
3749 }
Paolo Bonzini4e3de9e2010-03-10 11:38:48 +01003750 configure_icount(icount_option);
pbrook634fce92006-07-15 17:40:09 +00003751
Mark McLoughlindc1c9fe2009-10-06 12:17:16 +01003752 if (net_init_clients() < 0) {
3753 exit(1);
bellard702c6512004-04-02 21:21:32 +00003754 }
bellardf1510b22003-06-25 00:07:40 +00003755
balrogdc72ac12008-11-09 00:04:26 +00003756 /* init the bluetooth world */
Gerd Hoffmannbd3c9482009-07-15 13:59:26 +02003757 if (foreach_device_config(DEV_BT, bt_parse))
3758 exit(1);
balrogdc72ac12008-11-09 00:04:26 +00003759
Anthony PERARD834e76e2011-07-20 08:17:44 +00003760 if (!xen_enabled()) {
3761 /* On 32-bit hosts, QEMU is limited by virtual address space */
3762 if (ram_size > (2047 << 20) && HOST_LONG_BITS == 32) {
3763 fprintf(stderr, "qemu: at most 2047 MB RAM can be simulated\n");
3764 exit(1);
3765 }
3766 }
balrog7fb4fdc2008-04-24 17:59:27 +00003767
Jan Kiszkad5ab9712011-08-02 16:10:21 +02003768 cpu_exec_init_all();
bellard26a5f132008-05-28 12:30:31 +00003769
Markus Armbrustereb852012009-10-27 18:41:44 +01003770 bdrv_init_with_whitelist();
thse4bcb142007-12-02 04:51:10 +00003771
lirans@il.ibm.comc163b5c2009-11-02 15:40:58 +02003772 blk_mig_init();
3773
ths96d30e42007-01-07 20:42:14 +00003774 /* open the virtual block devices */
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +02003775 if (snapshot)
Gerd Hoffmann3329f072010-08-20 13:52:01 +02003776 qemu_opts_foreach(qemu_find_opts("drive"), drive_enable_snapshot, NULL, 0);
3777 if (qemu_opts_foreach(qemu_find_opts("drive"), drive_init_func, &machine->use_scsi, 1) != 0)
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +02003778 exit(1);
balrog3e3d5812007-04-30 02:09:25 +00003779
Markus Armbruster4e5d9b52011-01-28 11:21:45 +01003780 default_drive(default_cdrom, snapshot, machine->use_scsi,
3781 IF_DEFAULT, 2, CDROM_OPTS);
3782 default_drive(default_floppy, snapshot, machine->use_scsi,
3783 IF_FLOPPY, 0, FD_OPTS);
3784 default_drive(default_sdcard, snapshot, machine->use_scsi,
3785 IF_SD, 0, SD_OPTS);
3786
Juan Quintela7908c782012-06-26 18:46:10 +02003787 register_savevm_live(NULL, "ram", 0, 4, &savevm_ram_handlers, NULL);
bellard8a7ddc32004-03-31 19:00:16 +00003788
aliguori268a3622009-04-21 22:30:27 +00003789 if (nb_numa_nodes > 0) {
3790 int i;
3791
Sasha Levinea0e5412011-06-29 23:29:39 -04003792 if (nb_numa_nodes > MAX_NODES) {
3793 nb_numa_nodes = MAX_NODES;
aliguori268a3622009-04-21 22:30:27 +00003794 }
3795
3796 /* If no memory size if given for any node, assume the default case
3797 * and distribute the available memory equally across all nodes
3798 */
3799 for (i = 0; i < nb_numa_nodes; i++) {
3800 if (node_mem[i] != 0)
3801 break;
3802 }
3803 if (i == nb_numa_nodes) {
3804 uint64_t usedmem = 0;
3805
3806 /* On Linux, the each node's border has to be 8MB aligned,
3807 * the final node gets the rest.
3808 */
3809 for (i = 0; i < nb_numa_nodes - 1; i++) {
3810 node_mem[i] = (ram_size / nb_numa_nodes) & ~((1 << 23UL) - 1);
3811 usedmem += node_mem[i];
3812 }
3813 node_mem[i] = ram_size - usedmem;
3814 }
3815
3816 for (i = 0; i < nb_numa_nodes; i++) {
Chegu Vinodee785fe2012-07-16 21:31:30 -07003817 if (!bitmap_empty(node_cpumask[i], MAX_CPUMASK_BITS)) {
aliguori268a3622009-04-21 22:30:27 +00003818 break;
Chegu Vinodee785fe2012-07-16 21:31:30 -07003819 }
aliguori268a3622009-04-21 22:30:27 +00003820 }
3821 /* assigning the VCPUs round-robin is easier to implement, guest OSes
3822 * must cope with this anyway, because there are BIOSes out there in
3823 * real machines which also use this scheme.
3824 */
3825 if (i == nb_numa_nodes) {
Vasilis Liaskovitis991dfef2011-10-26 14:19:00 +02003826 for (i = 0; i < max_cpus; i++) {
Chegu Vinodee785fe2012-07-16 21:31:30 -07003827 set_bit(i, node_cpumask[i % nb_numa_nodes]);
aliguori268a3622009-04-21 22:30:27 +00003828 }
3829 }
3830 }
3831
Gerd Hoffmann3329f072010-08-20 13:52:01 +02003832 if (qemu_opts_foreach(qemu_find_opts("mon"), mon_init_func, NULL, 1) != 0) {
Jan Kiszka157b9312010-04-06 16:55:53 +02003833 exit(1);
3834 }
3835
Gerd Hoffmann998bbd72009-12-08 13:11:41 +01003836 if (foreach_device_config(DEV_SERIAL, serial_parse) < 0)
3837 exit(1);
Gerd Hoffmann6a5e8b02009-12-08 13:11:42 +01003838 if (foreach_device_config(DEV_PARALLEL, parallel_parse) < 0)
3839 exit(1);
Gerd Hoffmannaee1b932009-12-08 13:11:53 +01003840 if (foreach_device_config(DEV_VIRTCON, virtcon_parse) < 0)
3841 exit(1);
H. Peter Anvinc9f398e2009-12-29 13:51:36 -08003842 if (foreach_device_config(DEV_DEBUGCON, debugcon_parse) < 0)
3843 exit(1);
aliguori2796dae2009-01-16 20:23:27 +00003844
Paolo Bonzini7f1b17f2012-05-10 09:39:17 +02003845 /* If no default VGA is requested, the default is "none". */
Aurelien Jarno3605ded2012-09-10 01:01:44 +02003846 if (default_vga) {
3847 if (cirrus_vga_available()) {
3848 vga_model = "cirrus";
3849 } else if (vga_available()) {
3850 vga_model = "std";
3851 }
Blue Swirla369da52011-09-27 19:15:42 +00003852 }
Paolo Bonzini7f1b17f2012-05-10 09:39:17 +02003853 select_vgahw(vga_model);
Blue Swirla369da52011-09-27 19:15:42 +00003854
Markus Armbruster09aaa162009-08-21 10:31:34 +02003855 if (watchdog) {
3856 i = select_watchdog(watchdog);
3857 if (i > 0)
3858 exit (i == 1 ? 1 : 0);
3859 }
3860
Gerd Hoffmannb6b61142009-07-15 13:48:21 +02003861 if (machine->compat_props) {
Gerd Hoffmann458fb672009-12-08 13:11:33 +01003862 qdev_prop_register_global_list(machine->compat_props);
Gerd Hoffmannb6b61142009-07-15 13:48:21 +02003863 }
Gerd Hoffmannd0fef6f2009-12-08 13:11:34 +01003864 qemu_add_globals();
3865
Anthony Liguori1de81d22011-12-19 16:37:46 -06003866 qdev_machine_init();
3867
Eduardo Habkost5f072e12012-10-15 17:22:02 -03003868 QEMUMachineInitArgs args = { .ram_size = ram_size,
3869 .boot_device = boot_devices,
3870 .kernel_filename = kernel_filename,
3871 .kernel_cmdline = kernel_cmdline,
3872 .initrd_filename = initrd_filename,
3873 .cpu_model = cpu_model };
3874 machine->init(&args);
aliguori3023f3322009-01-16 19:04:14 +00003875
Jan Kiszkaea375f92010-03-01 19:10:30 +01003876 cpu_synchronize_all_post_init();
aliguori268a3622009-04-21 22:30:27 +00003877
Blue Swirl87d0a282010-03-27 18:24:45 +00003878 set_numa_modes();
aliguori268a3622009-04-21 22:30:27 +00003879
aliguori6f338c32009-02-11 15:21:54 +00003880 current_machine = machine;
3881
aliguori3023f3322009-01-16 19:04:14 +00003882 /* init USB devices */
zhlcindy@gmail.com094b2872012-09-02 19:25:28 +00003883 if (usb_enabled(false)) {
Markus Armbruster07527062009-10-06 12:16:57 +01003884 if (foreach_device_config(DEV_USB, usb_parse) < 0)
3885 exit(1);
aliguori3023f3322009-01-16 19:04:14 +00003886 }
3887
Gerd Hoffmannbd3c9482009-07-15 13:59:26 +02003888 /* init generic devices */
Gerd Hoffmann3329f072010-08-20 13:52:01 +02003889 if (qemu_opts_foreach(qemu_find_opts("device"), device_init_func, NULL, 1) != 0)
Gerd Hoffmannbd3c9482009-07-15 13:59:26 +02003890 exit(1);
3891
Markus Armbruster668680f2010-02-11 14:44:58 +01003892 net_check_clients();
3893
aliguori3023f3322009-01-16 19:04:14 +00003894 /* just use the first displaystate for the moment */
Paolo Bonzinib473df62010-02-11 00:29:55 +01003895 ds = get_displaystate();
Anthony Liguori993fbfd2009-05-21 16:54:00 -05003896
Gerd Hoffmanna3e22262010-08-25 15:32:06 +02003897 if (using_spice)
3898 display_remote++;
Gerd Hoffmann6b62dc22010-08-25 10:51:06 +02003899 if (display_type == DT_DEFAULT && !display_remote) {
Anthony Liguorif92f8af2009-05-20 13:01:02 -05003900#if defined(CONFIG_SDL) || defined(CONFIG_COCOA)
Anthony Liguori993fbfd2009-05-21 16:54:00 -05003901 display_type = DT_SDL;
Jes Sorensen821601e2011-03-16 13:33:36 +01003902#elif defined(CONFIG_VNC)
Anthony Liguori993fbfd2009-05-21 16:54:00 -05003903 vnc_display = "localhost:0,to=99";
3904 show_vnc_port = 1;
Jes Sorensen821601e2011-03-16 13:33:36 +01003905#else
3906 display_type = DT_NONE;
Anthony Liguori993fbfd2009-05-21 16:54:00 -05003907#endif
3908 }
Jes Sorensen821601e2011-03-16 13:33:36 +01003909
Anthony Liguori993fbfd2009-05-21 16:54:00 -05003910
Gerd Hoffmann6b62dc22010-08-25 10:51:06 +02003911 /* init local displays */
Anthony Liguori993fbfd2009-05-21 16:54:00 -05003912 switch (display_type) {
3913 case DT_NOGRAPHIC:
3914 break;
3915#if defined(CONFIG_CURSES)
3916 case DT_CURSES:
Hitoshi Mitake995ee2b2012-09-15 01:15:41 +09003917 if (!is_daemonized()) {
3918 curses_display_init(ds, full_screen);
3919 }
Anthony Liguori993fbfd2009-05-21 16:54:00 -05003920 break;
3921#endif
bellard5b0753e2005-03-01 21:37:28 +00003922#if defined(CONFIG_SDL)
Anthony Liguori993fbfd2009-05-21 16:54:00 -05003923 case DT_SDL:
3924 sdl_display_init(ds, full_screen, no_frame);
3925 break;
bellard5b0753e2005-03-01 21:37:28 +00003926#elif defined(CONFIG_COCOA)
Anthony Liguori993fbfd2009-05-21 16:54:00 -05003927 case DT_SDL:
3928 cocoa_display_init(ds, full_screen);
3929 break;
bellard313aa562003-08-10 21:52:11 +00003930#endif
Gerd Hoffmann6b62dc22010-08-25 10:51:06 +02003931 default:
3932 break;
3933 }
3934
Gleb Natapov0ce235a2011-03-31 11:27:23 +02003935 /* must be after terminal init, SDL library changes signal handlers */
3936 os_setup_signal_handling();
3937
Jes Sorensen821601e2011-03-16 13:33:36 +01003938#ifdef CONFIG_VNC
Gerd Hoffmann6b62dc22010-08-25 10:51:06 +02003939 /* init remote displays */
3940 if (vnc_display) {
Paolo Bonzini2d55f0e2012-10-02 10:17:21 +02003941 Error *local_err = NULL;
Anthony Liguori993fbfd2009-05-21 16:54:00 -05003942 vnc_display_init(ds);
Paolo Bonzini2d55f0e2012-10-02 10:17:21 +02003943 vnc_display_open(ds, vnc_display, &local_err);
3944 if (local_err != NULL) {
3945 fprintf(stderr, "Failed to start VNC server on `%s': %s\n",
3946 vnc_display, error_get_pretty(local_err));
3947 error_free(local_err);
Anthony Liguori993fbfd2009-05-21 16:54:00 -05003948 exit(1);
Amos Kong94b204c2012-06-30 10:02:20 +08003949 }
Anthony Liguorif92f8af2009-05-20 13:01:02 -05003950
Anthony Liguori993fbfd2009-05-21 16:54:00 -05003951 if (show_vnc_port) {
3952 printf("VNC server running on `%s'\n", vnc_display_local_addr(ds));
Anthony Liguorif92f8af2009-05-20 13:01:02 -05003953 }
bellard313aa562003-08-10 21:52:11 +00003954 }
Jes Sorensen821601e2011-03-16 13:33:36 +01003955#endif
Gerd Hoffmanna3e22262010-08-25 15:32:06 +02003956#ifdef CONFIG_SPICE
Gerd Hoffmanna19cbfb2010-04-27 11:50:11 +02003957 if (using_spice && !qxl_enabled) {
Gerd Hoffmanna3e22262010-08-25 15:32:06 +02003958 qemu_spice_display_init(ds);
3959 }
3960#endif
aliguori5b08fc12008-08-21 20:08:03 +00003961
Gerd Hoffmann6b62dc22010-08-25 10:51:06 +02003962 /* display setup */
Paolo Bonzinib473df62010-02-11 00:29:55 +01003963 text_consoles_set_display(ds);
aliguori2796dae2009-01-16 20:23:27 +00003964
Markus Armbrusteref0c4a02012-02-07 15:09:13 +01003965 if (foreach_device_config(DEV_GDB, gdbserver_start) < 0) {
aliguori59030a82009-04-05 18:43:41 +00003966 exit(1);
balrog45669e02007-07-02 13:20:17 +00003967 }
balrog45669e02007-07-02 13:20:17 +00003968
Gerd Hoffmann3418bd22009-09-25 21:42:41 +02003969 qdev_machine_creation_done();
3970
Gerd Hoffmann15ff7702009-12-14 16:07:35 +01003971 if (rom_load_all() != 0) {
3972 fprintf(stderr, "rom loading failed\n");
3973 exit(1);
3974 }
Gerd Hoffmann45a50b12009-10-01 16:42:33 +02003975
Isaku Yamahata80376c32010-12-20 14:33:35 +09003976 /* TODO: once all bus devices are qdevified, this should be done
3977 * when bus is created by qdev.c */
3978 qemu_register_reset(qbus_reset_all_fn, sysbus_get_default());
Gleb Natapov4cab9462010-12-08 13:35:08 +02003979 qemu_run_machine_init_done_notifiers();
3980
Jan Kiszkae063eb12011-06-14 18:29:43 +02003981 qemu_system_reset(VMRESET_SILENT);
Juan Quintela05f24012009-08-20 19:42:22 +02003982 if (loadvm) {
Markus Armbruster03cd4652010-02-17 16:24:10 +01003983 if (load_vmstate(loadvm) < 0) {
Juan Quintela05f24012009-08-20 19:42:22 +02003984 autostart = 0;
3985 }
3986 }
bellardd63d3072004-10-03 13:29:03 +00003987
Glauber Costa2bb8c102009-07-24 16:20:23 -04003988 if (incoming) {
Paolo Bonzini43eaae22012-10-02 18:21:18 +02003989 Error *local_err = NULL;
3990 qemu_start_incoming_migration(incoming, &local_err);
3991 if (local_err) {
3992 fprintf(stderr, "-incoming %s: %s\n", incoming, error_get_pretty(local_err));
3993 error_free(local_err);
3994 exit(1);
Juan Quintela8ca5e802010-06-09 14:10:54 +02003995 }
Avi Kivity6b99dad2009-08-09 14:39:20 +03003996 } else if (autostart) {
aliguoric0f4ce72009-03-05 23:01:01 +00003997 vm_start();
Avi Kivity6b99dad2009-08-09 14:39:20 +03003998 }
thsffd843b2006-12-21 19:46:43 +00003999
Jes Sorenseneb505be2010-06-10 11:42:28 +02004000 os_setup_post();
ths71e3ceb2006-12-22 02:11:31 +00004001
Paolo Bonzini99435902011-09-12 14:03:13 +02004002 resume_all_vcpus();
bellard8a7ddc32004-03-31 19:00:16 +00004003 main_loop();
Paolo Bonzini99435902011-09-12 14:03:13 +02004004 bdrv_close_all();
4005 pause_all_vcpus();
aliguori63a01ef2008-10-31 19:10:00 +00004006 net_cleanup();
wayne3d3b8302011-07-27 18:04:55 +08004007 res_free();
thsb46a8902007-10-21 23:20:45 +00004008
bellard0824d6f2003-06-24 13:42:40 +00004009 return 0;
4010}