blob: 9adaf28e64948819bdbcab8f04949c9fc78fd31a [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"
Paolo Bonzini1422e322012-10-24 08:43:34 +0200130#include "net/net.h"
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000131#include "net/slirp.h"
Paolo Bonzini83c90892012-12-17 18:19:49 +0100132#include "monitor/monitor.h"
Paolo Bonzini28ecbae2012-11-28 12:06:30 +0100133#include "ui/console.h"
blueswir1511d2b12009-03-07 15:32:56 +0000134#include "sysemu.h"
Paolo Bonzini022c62c2012-12-17 18:19:49 +0100135#include "exec/gdbstub.h"
blueswir1511d2b12009-03-07 15:32:56 +0000136#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"
Paolo Bonzini7b1b5d12012-12-17 18:19:43 +0100146#include "qapi/qmp/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
Paolo Bonzini76cad712012-10-24 11:12:21 +0200157#include "disas/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{
Christian Borntraeger2d0d2832012-11-20 15:30:34 +0100889 BlockInterfaceType *block_default_type = opaque;
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +0200890
Christian Borntraeger2d0d2832012-11-20 15:30:34 +0100891 return drive_init(opts, *block_default_type) == 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
Christian Borntraeger3c42ea62012-11-22 21:02:55 +0100902static void default_drive(int enable, int snapshot, BlockInterfaceType type,
903 int index, const char *optstr)
Markus Armbruster4e5d9b52011-01-28 11:21:45 +0100904{
905 QemuOpts *opts;
906
Markus Armbruster4e5d9b52011-01-28 11:21:45 +0100907 if (!enable || drive_get_by_index(type, index)) {
908 return;
909 }
910
911 opts = drive_add(type, index, NULL, optstr);
912 if (snapshot) {
913 drive_enable_snapshot(opts, NULL);
914 }
Christian Borntraeger2d0d2832012-11-20 15:30:34 +0100915 if (!drive_init(opts, type)) {
Markus Armbruster4e5d9b52011-01-28 11:21:45 +0100916 exit(1);
917 }
918}
919
Jan Kiszka76e30d02009-07-02 00:19:02 +0200920void qemu_register_boot_set(QEMUBootSetHandler *func, void *opaque)
921{
922 boot_set_handler = func;
923 boot_set_opaque = opaque;
924}
925
926int qemu_boot_set(const char *boot_devices)
927{
928 if (!boot_set_handler) {
929 return -EINVAL;
930 }
931 return boot_set_handler(boot_set_opaque, boot_devices);
932}
933
Eduardo Habkost4e9e9d62010-04-06 19:22:08 -0300934static void validate_bootdevices(char *devices)
Jan Kiszkaef3adf62009-07-02 00:19:02 +0200935{
936 /* We just do some generic consistency checks */
937 const char *p;
938 int bitmap = 0;
939
940 for (p = devices; *p != '\0'; p++) {
941 /* Allowed boot devices are:
942 * a-b: floppy disk drives
943 * c-f: IDE disk drives
Dong Xu Wang07f35072011-11-22 18:06:26 +0800944 * g-m: machine implementation dependent drives
Jan Kiszkaef3adf62009-07-02 00:19:02 +0200945 * n-p: network devices
946 * It's up to each machine implementation to check if the given boot
947 * devices match the actual hardware implementation and firmware
948 * features.
949 */
950 if (*p < 'a' || *p > 'p') {
951 fprintf(stderr, "Invalid boot device '%c'\n", *p);
952 exit(1);
953 }
954 if (bitmap & (1 << (*p - 'a'))) {
955 fprintf(stderr, "Boot device '%c' was given twice\n", *p);
956 exit(1);
957 }
958 bitmap |= 1 << (*p - 'a');
959 }
Jan Kiszkaef3adf62009-07-02 00:19:02 +0200960}
961
Jan Kiszkae0f084b2009-07-02 00:19:02 +0200962static void restore_boot_devices(void *opaque)
963{
964 char *standard_boot_devices = opaque;
Alex Williamson37905d62010-04-30 15:21:11 -0600965 static int first = 1;
966
967 /* Restore boot order and remove ourselves after the first boot */
968 if (first) {
969 first = 0;
970 return;
971 }
Jan Kiszkae0f084b2009-07-02 00:19:02 +0200972
973 qemu_boot_set(standard_boot_devices);
974
975 qemu_unregister_reset(restore_boot_devices, standard_boot_devices);
Anthony Liguori7267c092011-08-20 22:09:37 -0500976 g_free(standard_boot_devices);
Jan Kiszkae0f084b2009-07-02 00:19:02 +0200977}
978
Gleb Natapov1ca4d092010-12-08 13:35:05 +0200979void add_boot_device_path(int32_t bootindex, DeviceState *dev,
980 const char *suffix)
981{
982 FWBootEntry *node, *i;
983
984 if (bootindex < 0) {
985 return;
986 }
987
988 assert(dev != NULL || suffix != NULL);
989
Anthony Liguori7267c092011-08-20 22:09:37 -0500990 node = g_malloc0(sizeof(FWBootEntry));
Gleb Natapov1ca4d092010-12-08 13:35:05 +0200991 node->bootindex = bootindex;
Anthony Liguori7267c092011-08-20 22:09:37 -0500992 node->suffix = suffix ? g_strdup(suffix) : NULL;
Gleb Natapov1ca4d092010-12-08 13:35:05 +0200993 node->dev = dev;
994
995 QTAILQ_FOREACH(i, &fw_boot_order, link) {
996 if (i->bootindex == bootindex) {
997 fprintf(stderr, "Two devices with same boot index %d\n", bootindex);
998 exit(1);
999 } else if (i->bootindex < bootindex) {
1000 continue;
1001 }
1002 QTAILQ_INSERT_BEFORE(i, node, link);
1003 return;
1004 }
1005 QTAILQ_INSERT_TAIL(&fw_boot_order, node, link);
1006}
1007
Gleb Natapov962630f2010-12-08 13:35:09 +02001008/*
1009 * This function returns null terminated string that consist of new line
Brad Hards71785ab2011-04-23 21:50:06 +10001010 * separated device paths.
Gleb Natapov962630f2010-12-08 13:35:09 +02001011 *
1012 * memory pointed by "size" is assigned total length of the array in bytes
1013 *
1014 */
1015char *get_boot_devices_list(uint32_t *size)
1016{
1017 FWBootEntry *i;
1018 uint32_t total = 0;
1019 char *list = NULL;
1020
1021 QTAILQ_FOREACH(i, &fw_boot_order, link) {
1022 char *devpath = NULL, *bootpath;
1023 int len;
1024
1025 if (i->dev) {
1026 devpath = qdev_get_fw_dev_path(i->dev);
1027 assert(devpath);
1028 }
1029
1030 if (i->suffix && devpath) {
Blue Swirl4fd37a92010-12-19 14:05:43 +00001031 size_t bootpathlen = strlen(devpath) + strlen(i->suffix) + 1;
1032
Anthony Liguori7267c092011-08-20 22:09:37 -05001033 bootpath = g_malloc(bootpathlen);
Blue Swirl4fd37a92010-12-19 14:05:43 +00001034 snprintf(bootpath, bootpathlen, "%s%s", devpath, i->suffix);
Anthony Liguori7267c092011-08-20 22:09:37 -05001035 g_free(devpath);
Gleb Natapov962630f2010-12-08 13:35:09 +02001036 } else if (devpath) {
1037 bootpath = devpath;
1038 } else {
Markus Armbruster1bf6ccd2011-11-08 10:58:00 +01001039 assert(i->suffix);
Anthony Liguori7267c092011-08-20 22:09:37 -05001040 bootpath = g_strdup(i->suffix);
Gleb Natapov962630f2010-12-08 13:35:09 +02001041 }
1042
1043 if (total) {
1044 list[total-1] = '\n';
1045 }
1046 len = strlen(bootpath) + 1;
Anthony Liguori7267c092011-08-20 22:09:37 -05001047 list = g_realloc(list, total + len);
Gleb Natapov962630f2010-12-08 13:35:09 +02001048 memcpy(&list[total], bootpath, len);
1049 total += len;
Anthony Liguori7267c092011-08-20 22:09:37 -05001050 g_free(bootpath);
Gleb Natapov962630f2010-12-08 13:35:09 +02001051 }
1052
1053 *size = total;
1054
1055 return list;
1056}
1057
aliguori268a3622009-04-21 22:30:27 +00001058static void numa_add(const char *optarg)
1059{
1060 char option[128];
1061 char *endptr;
1062 unsigned long long value, endvalue;
1063 int nodenr;
1064
Chegu Vinodee785fe2012-07-16 21:31:30 -07001065 value = endvalue = 0ULL;
1066
aliguori268a3622009-04-21 22:30:27 +00001067 optarg = get_opt_name(option, 128, optarg, ',') + 1;
1068 if (!strcmp(option, "node")) {
1069 if (get_param_value(option, 128, "nodeid", optarg) == 0) {
1070 nodenr = nb_numa_nodes;
1071 } else {
1072 nodenr = strtoull(option, NULL, 10);
1073 }
1074
1075 if (get_param_value(option, 128, "mem", optarg) == 0) {
1076 node_mem[nodenr] = 0;
1077 } else {
Jes Sorensen70b4f4b2011-01-05 11:41:02 +01001078 int64_t sval;
Markus Armbrusterc03417b2011-11-22 09:46:02 +01001079 sval = strtosz(option, &endptr);
1080 if (sval < 0 || *endptr) {
Jes Sorensen9f9b17a2010-10-21 17:15:46 +02001081 fprintf(stderr, "qemu: invalid numa mem size: %s\n", optarg);
1082 exit(1);
aliguori268a3622009-04-21 22:30:27 +00001083 }
Jes Sorensen9f9b17a2010-10-21 17:15:46 +02001084 node_mem[nodenr] = sval;
aliguori268a3622009-04-21 22:30:27 +00001085 }
Chegu Vinodee785fe2012-07-16 21:31:30 -07001086 if (get_param_value(option, 128, "cpus", optarg) != 0) {
aliguori268a3622009-04-21 22:30:27 +00001087 value = strtoull(option, &endptr, 10);
Chegu Vinodee785fe2012-07-16 21:31:30 -07001088 if (*endptr == '-') {
1089 endvalue = strtoull(endptr+1, &endptr, 10);
aliguori268a3622009-04-21 22:30:27 +00001090 } else {
Chegu Vinodee785fe2012-07-16 21:31:30 -07001091 endvalue = value;
aliguori268a3622009-04-21 22:30:27 +00001092 }
Chegu Vinodee785fe2012-07-16 21:31:30 -07001093
1094 if (!(endvalue < MAX_CPUMASK_BITS)) {
1095 endvalue = MAX_CPUMASK_BITS - 1;
1096 fprintf(stderr,
1097 "A max of %d CPUs are supported in a guest\n",
1098 MAX_CPUMASK_BITS);
1099 }
1100
1101 bitmap_set(node_cpumask[nodenr], value, endvalue-value+1);
aliguori268a3622009-04-21 22:30:27 +00001102 }
1103 nb_numa_nodes++;
1104 }
aliguori268a3622009-04-21 22:30:27 +00001105}
1106
Andre Przywaradc6b1c02009-08-19 15:42:40 +02001107static void smp_parse(const char *optarg)
1108{
1109 int smp, sockets = 0, threads = 0, cores = 0;
1110 char *endptr;
1111 char option[128];
1112
1113 smp = strtoul(optarg, &endptr, 10);
1114 if (endptr != optarg) {
1115 if (*endptr == ',') {
1116 endptr++;
1117 }
1118 }
1119 if (get_param_value(option, 128, "sockets", endptr) != 0)
1120 sockets = strtoull(option, NULL, 10);
1121 if (get_param_value(option, 128, "cores", endptr) != 0)
1122 cores = strtoull(option, NULL, 10);
1123 if (get_param_value(option, 128, "threads", endptr) != 0)
1124 threads = strtoull(option, NULL, 10);
1125 if (get_param_value(option, 128, "maxcpus", endptr) != 0)
1126 max_cpus = strtoull(option, NULL, 10);
1127
1128 /* compute missing values, prefer sockets over cores over threads */
1129 if (smp == 0 || sockets == 0) {
1130 sockets = sockets > 0 ? sockets : 1;
1131 cores = cores > 0 ? cores : 1;
1132 threads = threads > 0 ? threads : 1;
1133 if (smp == 0) {
1134 smp = cores * threads * sockets;
Andre Przywaradc6b1c02009-08-19 15:42:40 +02001135 }
1136 } else {
1137 if (cores == 0) {
1138 threads = threads > 0 ? threads : 1;
1139 cores = smp / (sockets * threads);
1140 } else {
Joel Schoppdca98162010-07-21 15:05:17 -05001141 threads = smp / (cores * sockets);
Andre Przywaradc6b1c02009-08-19 15:42:40 +02001142 }
1143 }
1144 smp_cpus = smp;
1145 smp_cores = cores > 0 ? cores : 1;
1146 smp_threads = threads > 0 ? threads : 1;
1147 if (max_cpus == 0)
1148 max_cpus = smp_cpus;
1149}
1150
bellard330d0412003-07-26 18:11:40 +00001151/***********************************************************/
bellarda594cfb2005-11-06 16:13:29 +00001152/* USB devices */
1153
Markus Armbrusterfb080002010-05-28 15:38:44 +02001154static int usb_device_add(const char *devname)
bellarda594cfb2005-11-06 16:13:29 +00001155{
1156 const char *p;
Gerd Hoffmanna5d2f722009-08-31 14:24:00 +02001157 USBDevice *dev = NULL;
bellarda594cfb2005-11-06 16:13:29 +00001158
zhlcindy@gmail.com094b2872012-09-02 19:25:28 +00001159 if (!usb_enabled(false)) {
bellarda594cfb2005-11-06 16:13:29 +00001160 return -1;
zhlcindy@gmail.com094b2872012-09-02 19:25:28 +00001161 }
bellarda594cfb2005-11-06 16:13:29 +00001162
Gerd Hoffmann0958b4c2009-10-26 15:56:45 +01001163 /* drivers with .usbdevice_name entry in USBDeviceInfo */
1164 dev = usbdevice_create(devname);
1165 if (dev)
1166 goto done;
1167
Gerd Hoffmanna5d2f722009-08-31 14:24:00 +02001168 /* the other ones */
Gerd Hoffmanne447fc62011-06-01 14:41:59 +02001169#ifndef CONFIG_LINUX
1170 /* only the linux version is qdev-ified, usb-bsd still needs this */
bellarda594cfb2005-11-06 16:13:29 +00001171 if (strstart(devname, "host:", &p)) {
Jan Kiszka37417152012-02-27 15:18:47 +01001172 dev = usb_host_device_open(usb_bus_find(-1), p);
Gerd Hoffmanne447fc62011-06-01 14:41:59 +02001173 } else
1174#endif
1175 if (!strcmp(devname, "bt") || strstart(devname, "bt:", &p)) {
Jan Kiszka37417152012-02-27 15:18:47 +01001176 dev = usb_bt_init(usb_bus_find(-1),
1177 devname[2] ? hci_init(p)
1178 : bt_new_hci(qemu_find_bt_vlan(0)));
bellarda594cfb2005-11-06 16:13:29 +00001179 } else {
1180 return -1;
1181 }
pbrook0d92ed32006-05-21 16:30:15 +00001182 if (!dev)
1183 return -1;
1184
Gerd Hoffmanna5d2f722009-08-31 14:24:00 +02001185done:
bellarda594cfb2005-11-06 16:13:29 +00001186 return 0;
1187}
1188
aliguori1f3870a2008-08-21 19:27:48 +00001189static int usb_device_del(const char *devname)
1190{
1191 int bus_num, addr;
1192 const char *p;
1193
aliguori5d0c5752008-09-14 01:07:41 +00001194 if (strstart(devname, "host:", &p))
1195 return usb_host_device_close(p);
1196
zhlcindy@gmail.com094b2872012-09-02 19:25:28 +00001197 if (!usb_enabled(false)) {
aliguori1f3870a2008-08-21 19:27:48 +00001198 return -1;
zhlcindy@gmail.com094b2872012-09-02 19:25:28 +00001199 }
aliguori1f3870a2008-08-21 19:27:48 +00001200
1201 p = strchr(devname, '.');
1202 if (!p)
1203 return -1;
1204 bus_num = strtoul(devname, NULL, 0);
1205 addr = strtoul(p + 1, NULL, 0);
1206
Gerd Hoffmanna5d2f722009-08-31 14:24:00 +02001207 return usb_device_delete_addr(bus_num, addr);
aliguori1f3870a2008-08-21 19:27:48 +00001208}
1209
Gerd Hoffmannbd3c9482009-07-15 13:59:26 +02001210static int usb_parse(const char *cmdline)
1211{
Scott Tsai59d1c1c2009-12-23 04:30:18 +08001212 int r;
Markus Armbrusterfb080002010-05-28 15:38:44 +02001213 r = usb_device_add(cmdline);
Scott Tsai59d1c1c2009-12-23 04:30:18 +08001214 if (r < 0) {
1215 fprintf(stderr, "qemu: could not add USB device '%s'\n", cmdline);
1216 }
1217 return r;
Gerd Hoffmannbd3c9482009-07-15 13:59:26 +02001218}
1219
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001220void do_usb_add(Monitor *mon, const QDict *qdict)
bellarda594cfb2005-11-06 16:13:29 +00001221{
Scott Tsai59d1c1c2009-12-23 04:30:18 +08001222 const char *devname = qdict_get_str(qdict, "devname");
Markus Armbrusterfb080002010-05-28 15:38:44 +02001223 if (usb_device_add(devname) < 0) {
Markus Armbruster1ecda022010-02-18 17:25:24 +01001224 error_report("could not add USB device '%s'", devname);
Scott Tsai59d1c1c2009-12-23 04:30:18 +08001225 }
bellarda594cfb2005-11-06 16:13:29 +00001226}
1227
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001228void do_usb_del(Monitor *mon, const QDict *qdict)
bellarda594cfb2005-11-06 16:13:29 +00001229{
Scott Tsai59d1c1c2009-12-23 04:30:18 +08001230 const char *devname = qdict_get_str(qdict, "devname");
1231 if (usb_device_del(devname) < 0) {
Markus Armbruster1ecda022010-02-18 17:25:24 +01001232 error_report("could not delete USB device '%s'", devname);
Scott Tsai59d1c1c2009-12-23 04:30:18 +08001233 }
bellarda594cfb2005-11-06 16:13:29 +00001234}
1235
bellardf7cce892004-12-08 22:21:25 +00001236/***********************************************************/
balrog201a51f2007-04-30 00:51:09 +00001237/* PCMCIA/Cardbus */
1238
1239static struct pcmcia_socket_entry_s {
Paul Brookbc24a222009-05-10 01:44:56 +01001240 PCMCIASocket *socket;
balrog201a51f2007-04-30 00:51:09 +00001241 struct pcmcia_socket_entry_s *next;
1242} *pcmcia_sockets = 0;
1243
Paul Brookbc24a222009-05-10 01:44:56 +01001244void pcmcia_socket_register(PCMCIASocket *socket)
balrog201a51f2007-04-30 00:51:09 +00001245{
1246 struct pcmcia_socket_entry_s *entry;
1247
Anthony Liguori7267c092011-08-20 22:09:37 -05001248 entry = g_malloc(sizeof(struct pcmcia_socket_entry_s));
balrog201a51f2007-04-30 00:51:09 +00001249 entry->socket = socket;
1250 entry->next = pcmcia_sockets;
1251 pcmcia_sockets = entry;
1252}
1253
Paul Brookbc24a222009-05-10 01:44:56 +01001254void pcmcia_socket_unregister(PCMCIASocket *socket)
balrog201a51f2007-04-30 00:51:09 +00001255{
1256 struct pcmcia_socket_entry_s *entry, **ptr;
1257
1258 ptr = &pcmcia_sockets;
1259 for (entry = *ptr; entry; ptr = &entry->next, entry = *ptr)
1260 if (entry->socket == socket) {
1261 *ptr = entry->next;
Anthony Liguori7267c092011-08-20 22:09:37 -05001262 g_free(entry);
balrog201a51f2007-04-30 00:51:09 +00001263 }
1264}
1265
aliguori376253e2009-03-05 23:01:23 +00001266void pcmcia_info(Monitor *mon)
balrog201a51f2007-04-30 00:51:09 +00001267{
1268 struct pcmcia_socket_entry_s *iter;
aliguori376253e2009-03-05 23:01:23 +00001269
balrog201a51f2007-04-30 00:51:09 +00001270 if (!pcmcia_sockets)
aliguori376253e2009-03-05 23:01:23 +00001271 monitor_printf(mon, "No PCMCIA sockets\n");
balrog201a51f2007-04-30 00:51:09 +00001272
1273 for (iter = pcmcia_sockets; iter; iter = iter->next)
aliguori376253e2009-03-05 23:01:23 +00001274 monitor_printf(mon, "%s: %s\n", iter->socket->slot_string,
1275 iter->socket->attached ? iter->socket->card_string :
1276 "Empty");
balrog201a51f2007-04-30 00:51:09 +00001277}
1278
1279/***********************************************************/
bellardcc1daa42005-06-05 14:49:17 +00001280/* machine registration */
1281
blueswir1bdaf78e2008-10-04 07:24:27 +00001282static QEMUMachine *first_machine = NULL;
aliguori6f338c32009-02-11 15:21:54 +00001283QEMUMachine *current_machine = NULL;
bellardcc1daa42005-06-05 14:49:17 +00001284
1285int qemu_register_machine(QEMUMachine *m)
1286{
1287 QEMUMachine **pm;
1288 pm = &first_machine;
1289 while (*pm != NULL)
1290 pm = &(*pm)->next;
1291 m->next = NULL;
1292 *pm = m;
1293 return 0;
1294}
1295
pbrook9596ebb2007-11-18 01:44:38 +00001296static QEMUMachine *find_machine(const char *name)
bellardcc1daa42005-06-05 14:49:17 +00001297{
1298 QEMUMachine *m;
1299
1300 for(m = first_machine; m != NULL; m = m->next) {
1301 if (!strcmp(m->name, name))
1302 return m;
Mark McLoughlin3f6599e2009-07-22 10:02:50 +01001303 if (m->alias && !strcmp(m->alias, name))
1304 return m;
bellardcc1daa42005-06-05 14:49:17 +00001305 }
1306 return NULL;
1307}
1308
Jordan Justen2c8cffa2012-02-21 23:18:50 -08001309QEMUMachine *find_default_machine(void)
Anthony Liguori0c257432009-05-21 20:41:01 -05001310{
1311 QEMUMachine *m;
1312
1313 for(m = first_machine; m != NULL; m = m->next) {
1314 if (m->is_default) {
1315 return m;
1316 }
1317 }
1318 return NULL;
1319}
1320
Anthony Liguori01d3c802012-08-10 11:04:11 -05001321MachineInfoList *qmp_query_machines(Error **errp)
1322{
1323 MachineInfoList *mach_list = NULL;
1324 QEMUMachine *m;
1325
1326 for (m = first_machine; m; m = m->next) {
1327 MachineInfoList *entry;
1328 MachineInfo *info;
1329
1330 info = g_malloc0(sizeof(*info));
1331 if (m->is_default) {
1332 info->has_is_default = true;
1333 info->is_default = true;
1334 }
1335
1336 if (m->alias) {
1337 info->has_alias = true;
1338 info->alias = g_strdup(m->alias);
1339 }
1340
1341 info->name = g_strdup(m->name);
1342
1343 entry = g_malloc0(sizeof(*entry));
1344 entry->value = info;
1345 entry->next = mach_list;
1346 mach_list = entry;
1347 }
1348
1349 return mach_list;
1350}
1351
bellardcc1daa42005-06-05 14:49:17 +00001352/***********************************************************/
bellard8a7ddc32004-03-31 19:00:16 +00001353/* main execution loop */
1354
pbrook9596ebb2007-11-18 01:44:38 +00001355static void gui_update(void *opaque)
bellard8a7ddc32004-03-31 19:00:16 +00001356{
aliguori7d957bd2009-01-15 22:14:11 +00001357 uint64_t interval = GUI_REFRESH_INTERVAL;
ths740733b2007-06-08 01:57:56 +00001358 DisplayState *ds = opaque;
Gerd Hoffmann87e487a2010-06-04 11:46:35 +02001359 DisplayChangeListener *dcl;
aliguori7d957bd2009-01-15 22:14:11 +00001360
1361 dpy_refresh(ds);
1362
Gerd Hoffmann87e487a2010-06-04 11:46:35 +02001363 QLIST_FOREACH(dcl, &ds->listeners, next) {
aliguori7d957bd2009-01-15 22:14:11 +00001364 if (dcl->gui_timer_interval &&
1365 dcl->gui_timer_interval < interval)
1366 interval = dcl->gui_timer_interval;
aliguori7d957bd2009-01-15 22:14:11 +00001367 }
Paolo Bonzini7bd427d2011-03-11 16:47:48 +01001368 qemu_mod_timer(ds->gui_timer, interval + qemu_get_clock_ms(rt_clock));
bellard8a7ddc32004-03-31 19:00:16 +00001369}
1370
Gerd Hoffmann35c9e0a2010-06-04 11:51:31 +02001371void gui_setup_refresh(DisplayState *ds)
1372{
1373 DisplayChangeListener *dcl;
1374 bool need_timer = false;
Gerd Hoffmanna93a4a22012-09-28 15:02:08 +02001375 bool have_gfx = false;
1376 bool have_text = false;
Gerd Hoffmann35c9e0a2010-06-04 11:51:31 +02001377
1378 QLIST_FOREACH(dcl, &ds->listeners, next) {
1379 if (dcl->dpy_refresh != NULL) {
1380 need_timer = true;
Gerd Hoffmanna93a4a22012-09-28 15:02:08 +02001381 }
1382 if (dcl->dpy_gfx_update != NULL) {
1383 have_gfx = true;
1384 }
1385 if (dcl->dpy_text_update != NULL) {
1386 have_text = true;
Gerd Hoffmann35c9e0a2010-06-04 11:51:31 +02001387 }
1388 }
1389
1390 if (need_timer && ds->gui_timer == NULL) {
1391 ds->gui_timer = qemu_new_timer_ms(rt_clock, gui_update, ds);
1392 qemu_mod_timer(ds->gui_timer, qemu_get_clock_ms(rt_clock));
1393 }
1394 if (!need_timer && ds->gui_timer != NULL) {
1395 qemu_del_timer(ds->gui_timer);
1396 qemu_free_timer(ds->gui_timer);
1397 ds->gui_timer = NULL;
1398 }
Gerd Hoffmanna93a4a22012-09-28 15:02:08 +02001399
1400 ds->have_gfx = have_gfx;
1401 ds->have_text = have_text;
Gerd Hoffmann35c9e0a2010-06-04 11:51:31 +02001402}
1403
bellard0bd48852005-11-11 00:00:47 +00001404struct vm_change_state_entry {
1405 VMChangeStateHandler *cb;
1406 void *opaque;
Blue Swirl72cf2d42009-09-12 07:36:22 +00001407 QLIST_ENTRY (vm_change_state_entry) entries;
bellard0bd48852005-11-11 00:00:47 +00001408};
1409
Blue Swirl72cf2d42009-09-12 07:36:22 +00001410static QLIST_HEAD(vm_change_state_head, vm_change_state_entry) vm_change_state_head;
bellard0bd48852005-11-11 00:00:47 +00001411
1412VMChangeStateEntry *qemu_add_vm_change_state_handler(VMChangeStateHandler *cb,
1413 void *opaque)
1414{
1415 VMChangeStateEntry *e;
1416
Anthony Liguori7267c092011-08-20 22:09:37 -05001417 e = g_malloc0(sizeof (*e));
bellard0bd48852005-11-11 00:00:47 +00001418
1419 e->cb = cb;
1420 e->opaque = opaque;
Blue Swirl72cf2d42009-09-12 07:36:22 +00001421 QLIST_INSERT_HEAD(&vm_change_state_head, e, entries);
bellard0bd48852005-11-11 00:00:47 +00001422 return e;
1423}
1424
1425void qemu_del_vm_change_state_handler(VMChangeStateEntry *e)
1426{
Blue Swirl72cf2d42009-09-12 07:36:22 +00001427 QLIST_REMOVE (e, entries);
Anthony Liguori7267c092011-08-20 22:09:37 -05001428 g_free (e);
bellard0bd48852005-11-11 00:00:47 +00001429}
1430
Luiz Capitulino1dfb4dd2011-07-29 14:26:33 -03001431void vm_state_notify(int running, RunState state)
bellard0bd48852005-11-11 00:00:47 +00001432{
1433 VMChangeStateEntry *e;
1434
Luiz Capitulino1dfb4dd2011-07-29 14:26:33 -03001435 trace_vm_state_notify(running, state);
Stefan Hajnoczi94b0b5f2010-11-16 12:20:25 +00001436
bellard0bd48852005-11-11 00:00:47 +00001437 for (e = vm_change_state_head.lh_first; e; e = e->entries.le_next) {
Luiz Capitulino1dfb4dd2011-07-29 14:26:33 -03001438 e->cb(e->opaque, running, state);
bellard0bd48852005-11-11 00:00:47 +00001439 }
1440}
1441
bellard8a7ddc32004-03-31 19:00:16 +00001442void vm_start(void)
1443{
Luiz Capitulino13548692011-07-29 15:36:43 -03001444 if (!runstate_is_running()) {
bellard8a7ddc32004-03-31 19:00:16 +00001445 cpu_enable_ticks();
Luiz Capitulino0461d5a2011-09-30 14:45:27 -03001446 runstate_set(RUN_STATE_RUNNING);
1447 vm_state_notify(1, RUN_STATE_RUNNING);
aliguorid6dc3d42009-04-24 18:04:07 +00001448 resume_all_vcpus();
Luiz Capitulino6ed2c482010-04-27 20:35:59 -03001449 monitor_protocol_event(QEVENT_RESUME, NULL);
bellard8a7ddc32004-03-31 19:00:16 +00001450 }
1451}
1452
bellardbb0c6722004-06-20 12:37:32 +00001453/* reset/shutdown handler */
1454
1455typedef struct QEMUResetEntry {
Blue Swirl72cf2d42009-09-12 07:36:22 +00001456 QTAILQ_ENTRY(QEMUResetEntry) entry;
bellardbb0c6722004-06-20 12:37:32 +00001457 QEMUResetHandler *func;
1458 void *opaque;
bellardbb0c6722004-06-20 12:37:32 +00001459} QEMUResetEntry;
1460
Blue Swirl72cf2d42009-09-12 07:36:22 +00001461static QTAILQ_HEAD(reset_handlers, QEMUResetEntry) reset_handlers =
1462 QTAILQ_HEAD_INITIALIZER(reset_handlers);
bellardbb0c6722004-06-20 12:37:32 +00001463static int reset_requested;
Gleb Natapovf64622c2011-03-15 13:56:04 +02001464static int shutdown_requested, shutdown_signal = -1;
1465static pid_t shutdown_pid;
bellard34751872005-07-02 14:31:34 +00001466static int powerdown_requested;
Jan Kiszka8cf71712011-02-07 12:19:16 +01001467static int debug_requested;
Gerd Hoffmann95b363b2012-02-23 13:45:19 +01001468static int suspend_requested;
Luiz Capitulino14058192012-08-08 17:29:17 -03001469static int wakeup_requested;
Igor Mammedova9552c82012-09-05 23:06:21 +02001470static NotifierList powerdown_notifiers =
1471 NOTIFIER_LIST_INITIALIZER(powerdown_notifiers);
Gerd Hoffmann95b363b2012-02-23 13:45:19 +01001472static NotifierList suspend_notifiers =
1473 NOTIFIER_LIST_INITIALIZER(suspend_notifiers);
1474static NotifierList wakeup_notifiers =
1475 NOTIFIER_LIST_INITIALIZER(wakeup_notifiers);
1476static uint32_t wakeup_reason_mask = ~0;
Luiz Capitulino0461d5a2011-09-30 14:45:27 -03001477static RunState vmstop_requested = RUN_STATE_MAX;
bellardbb0c6722004-06-20 12:37:32 +00001478
Anthony PERARD1291eb32010-07-22 15:52:48 +01001479int qemu_shutdown_requested_get(void)
1480{
1481 return shutdown_requested;
1482}
1483
1484int qemu_reset_requested_get(void)
1485{
1486 return reset_requested;
1487}
1488
Blue Swirl4fdcac02012-10-28 11:04:47 +00001489static int qemu_shutdown_requested(void)
aurel32cf7a2fe2008-03-18 06:53:05 +00001490{
1491 int r = shutdown_requested;
1492 shutdown_requested = 0;
1493 return r;
1494}
1495
Blue Swirl4fdcac02012-10-28 11:04:47 +00001496static void qemu_kill_report(void)
Gleb Natapovf64622c2011-03-15 13:56:04 +02001497{
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +02001498 if (!qtest_enabled() && shutdown_signal != -1) {
Peter Maydellf1d3fb02011-03-30 22:03:38 +01001499 fprintf(stderr, "qemu: terminating on signal %d", shutdown_signal);
1500 if (shutdown_pid == 0) {
1501 /* This happens for eg ^C at the terminal, so it's worth
1502 * avoiding printing an odd message in that case.
1503 */
1504 fputc('\n', stderr);
1505 } else {
Andreas Färber953ffe02011-06-02 19:58:06 +02001506 fprintf(stderr, " from pid " FMT_pid "\n", shutdown_pid);
Peter Maydellf1d3fb02011-03-30 22:03:38 +01001507 }
Gleb Natapovf64622c2011-03-15 13:56:04 +02001508 shutdown_signal = -1;
1509 }
1510}
1511
Blue Swirl4fdcac02012-10-28 11:04:47 +00001512static int qemu_reset_requested(void)
aurel32cf7a2fe2008-03-18 06:53:05 +00001513{
1514 int r = reset_requested;
1515 reset_requested = 0;
1516 return r;
1517}
1518
Gerd Hoffmann95b363b2012-02-23 13:45:19 +01001519static int qemu_suspend_requested(void)
1520{
1521 int r = suspend_requested;
1522 suspend_requested = 0;
1523 return r;
1524}
1525
Luiz Capitulino14058192012-08-08 17:29:17 -03001526static int qemu_wakeup_requested(void)
1527{
1528 int r = wakeup_requested;
1529 wakeup_requested = 0;
1530 return r;
1531}
1532
Blue Swirl4fdcac02012-10-28 11:04:47 +00001533static int qemu_powerdown_requested(void)
aurel32cf7a2fe2008-03-18 06:53:05 +00001534{
1535 int r = powerdown_requested;
1536 powerdown_requested = 0;
1537 return r;
1538}
1539
aliguorie5689022009-04-24 18:03:54 +00001540static int qemu_debug_requested(void)
1541{
1542 int r = debug_requested;
1543 debug_requested = 0;
1544 return r;
1545}
1546
Luiz Capitulino0461d5a2011-09-30 14:45:27 -03001547/* We use RUN_STATE_MAX but any invalid value will do */
Luiz Capitulino0d45b702011-09-30 14:27:11 -03001548static bool qemu_vmstop_requested(RunState *r)
aliguori6e29f5d2009-04-24 18:04:02 +00001549{
Luiz Capitulino0461d5a2011-09-30 14:45:27 -03001550 if (vmstop_requested < RUN_STATE_MAX) {
Luiz Capitulino0d45b702011-09-30 14:27:11 -03001551 *r = vmstop_requested;
Luiz Capitulino0461d5a2011-09-30 14:45:27 -03001552 vmstop_requested = RUN_STATE_MAX;
Luiz Capitulino0d45b702011-09-30 14:27:11 -03001553 return true;
1554 }
1555
1556 return false;
aliguori6e29f5d2009-04-24 18:04:02 +00001557}
1558
Jan Kiszkaa08d4362009-06-27 09:25:07 +02001559void qemu_register_reset(QEMUResetHandler *func, void *opaque)
bellardbb0c6722004-06-20 12:37:32 +00001560{
Anthony Liguori7267c092011-08-20 22:09:37 -05001561 QEMUResetEntry *re = g_malloc0(sizeof(QEMUResetEntry));
bellardbb0c6722004-06-20 12:37:32 +00001562
bellardbb0c6722004-06-20 12:37:32 +00001563 re->func = func;
1564 re->opaque = opaque;
Blue Swirl72cf2d42009-09-12 07:36:22 +00001565 QTAILQ_INSERT_TAIL(&reset_handlers, re, entry);
bellardbb0c6722004-06-20 12:37:32 +00001566}
1567
Jan Kiszkadda9b292009-07-02 00:19:02 +02001568void qemu_unregister_reset(QEMUResetHandler *func, void *opaque)
bellardbb0c6722004-06-20 12:37:32 +00001569{
1570 QEMUResetEntry *re;
1571
Blue Swirl72cf2d42009-09-12 07:36:22 +00001572 QTAILQ_FOREACH(re, &reset_handlers, entry) {
Jan Kiszkadda9b292009-07-02 00:19:02 +02001573 if (re->func == func && re->opaque == opaque) {
Blue Swirl72cf2d42009-09-12 07:36:22 +00001574 QTAILQ_REMOVE(&reset_handlers, re, entry);
Anthony Liguori7267c092011-08-20 22:09:37 -05001575 g_free(re);
Jan Kiszkadda9b292009-07-02 00:19:02 +02001576 return;
1577 }
1578 }
1579}
1580
David Gibsonbe522022012-08-07 16:41:51 +10001581void qemu_devices_reset(void)
Jan Kiszkadda9b292009-07-02 00:19:02 +02001582{
1583 QEMUResetEntry *re, *nre;
1584
1585 /* reset all devices */
Blue Swirl72cf2d42009-09-12 07:36:22 +00001586 QTAILQ_FOREACH_SAFE(re, &reset_handlers, entry, nre) {
bellardbb0c6722004-06-20 12:37:32 +00001587 re->func(re->opaque);
1588 }
David Gibsonbe522022012-08-07 16:41:51 +10001589}
1590
1591void qemu_system_reset(bool report)
1592{
1593 if (current_machine && current_machine->reset) {
1594 current_machine->reset();
1595 } else {
1596 qemu_devices_reset();
1597 }
Jan Kiszkae063eb12011-06-14 18:29:43 +02001598 if (report) {
1599 monitor_protocol_event(QEVENT_RESET, NULL);
1600 }
Jan Kiszkaea375f92010-03-01 19:10:30 +01001601 cpu_synchronize_all_post_reset();
bellardbb0c6722004-06-20 12:37:32 +00001602}
1603
1604void qemu_system_reset_request(void)
1605{
bellardd1beab82006-10-02 19:44:22 +00001606 if (no_reboot) {
1607 shutdown_requested = 1;
1608 } else {
1609 reset_requested = 1;
1610 }
Jan Kiszkab4a3d962011-02-01 22:15:43 +01001611 cpu_stop_current();
aliguorid9f75a42009-04-24 18:03:11 +00001612 qemu_notify_event();
bellardbb0c6722004-06-20 12:37:32 +00001613}
1614
Gerd Hoffmann95b363b2012-02-23 13:45:19 +01001615static void qemu_system_suspend(void)
1616{
1617 pause_all_vcpus();
1618 notifier_list_notify(&suspend_notifiers, NULL);
Luiz Capitulinoad02b962012-04-27 13:33:36 -03001619 runstate_set(RUN_STATE_SUSPENDED);
Gerd Hoffmann53370b72012-02-23 13:45:26 +01001620 monitor_protocol_event(QEVENT_SUSPEND, NULL);
Gerd Hoffmann95b363b2012-02-23 13:45:19 +01001621}
1622
1623void qemu_system_suspend_request(void)
1624{
Luiz Capitulino9abc62f2012-04-27 14:31:12 -03001625 if (runstate_check(RUN_STATE_SUSPENDED)) {
Gerd Hoffmann95b363b2012-02-23 13:45:19 +01001626 return;
1627 }
1628 suspend_requested = 1;
1629 cpu_stop_current();
1630 qemu_notify_event();
1631}
1632
1633void qemu_register_suspend_notifier(Notifier *notifier)
1634{
1635 notifier_list_add(&suspend_notifiers, notifier);
1636}
1637
1638void qemu_system_wakeup_request(WakeupReason reason)
1639{
Luiz Capitulino9abc62f2012-04-27 14:31:12 -03001640 if (!runstate_check(RUN_STATE_SUSPENDED)) {
Gerd Hoffmann95b363b2012-02-23 13:45:19 +01001641 return;
1642 }
1643 if (!(wakeup_reason_mask & (1 << reason))) {
1644 return;
1645 }
Luiz Capitulinoad02b962012-04-27 13:33:36 -03001646 runstate_set(RUN_STATE_RUNNING);
Gerd Hoffmann95b363b2012-02-23 13:45:19 +01001647 notifier_list_notify(&wakeup_notifiers, &reason);
Luiz Capitulino14058192012-08-08 17:29:17 -03001648 wakeup_requested = 1;
Gerd Hoffmann95b363b2012-02-23 13:45:19 +01001649 qemu_notify_event();
Gerd Hoffmann95b363b2012-02-23 13:45:19 +01001650}
1651
1652void qemu_system_wakeup_enable(WakeupReason reason, bool enabled)
1653{
1654 if (enabled) {
1655 wakeup_reason_mask |= (1 << reason);
1656 } else {
1657 wakeup_reason_mask &= ~(1 << reason);
1658 }
1659}
1660
1661void qemu_register_wakeup_notifier(Notifier *notifier)
1662{
1663 notifier_list_add(&wakeup_notifiers, notifier);
1664}
1665
Gleb Natapovf64622c2011-03-15 13:56:04 +02001666void qemu_system_killed(int signal, pid_t pid)
1667{
1668 shutdown_signal = signal;
1669 shutdown_pid = pid;
Kevin Wolfd9389b92011-09-14 15:38:40 +02001670 no_shutdown = 0;
Gleb Natapovf64622c2011-03-15 13:56:04 +02001671 qemu_system_shutdown_request();
1672}
1673
bellardbb0c6722004-06-20 12:37:32 +00001674void qemu_system_shutdown_request(void)
1675{
1676 shutdown_requested = 1;
aliguorid9f75a42009-04-24 18:03:11 +00001677 qemu_notify_event();
bellardbb0c6722004-06-20 12:37:32 +00001678}
1679
Igor Mammedov013c2f12012-09-05 23:06:25 +02001680static void qemu_system_powerdown(void)
1681{
1682 monitor_protocol_event(QEVENT_POWERDOWN, NULL);
1683 notifier_list_notify(&powerdown_notifiers, NULL);
1684}
1685
bellard34751872005-07-02 14:31:34 +00001686void qemu_system_powerdown_request(void)
1687{
1688 powerdown_requested = 1;
aliguorid9f75a42009-04-24 18:03:11 +00001689 qemu_notify_event();
1690}
1691
Igor Mammedova9552c82012-09-05 23:06:21 +02001692void qemu_register_powerdown_notifier(Notifier *notifier)
1693{
1694 notifier_list_add(&powerdown_notifiers, notifier);
1695}
1696
Jan Kiszka8cf71712011-02-07 12:19:16 +01001697void qemu_system_debug_request(void)
1698{
1699 debug_requested = 1;
Jan Kiszka83f338f2011-02-07 12:19:17 +01001700 qemu_notify_event();
Jan Kiszka8cf71712011-02-07 12:19:16 +01001701}
1702
Luiz Capitulino1dfb4dd2011-07-29 14:26:33 -03001703void qemu_system_vmstop_request(RunState state)
Jan Kiszka8cf71712011-02-07 12:19:16 +01001704{
Luiz Capitulino1dfb4dd2011-07-29 14:26:33 -03001705 vmstop_requested = state;
Jan Kiszka8cf71712011-02-07 12:19:16 +01001706 qemu_notify_event();
1707}
1708
Paolo Bonzini99435902011-09-12 14:03:13 +02001709static bool main_loop_should_exit(void)
1710{
1711 RunState r;
1712 if (qemu_debug_requested()) {
1713 vm_stop(RUN_STATE_DEBUG);
1714 }
Gerd Hoffmann95b363b2012-02-23 13:45:19 +01001715 if (qemu_suspend_requested()) {
1716 qemu_system_suspend();
1717 }
Paolo Bonzini99435902011-09-12 14:03:13 +02001718 if (qemu_shutdown_requested()) {
1719 qemu_kill_report();
1720 monitor_protocol_event(QEVENT_SHUTDOWN, NULL);
1721 if (no_shutdown) {
1722 vm_stop(RUN_STATE_SHUTDOWN);
1723 } else {
1724 return true;
1725 }
1726 }
1727 if (qemu_reset_requested()) {
1728 pause_all_vcpus();
1729 cpu_synchronize_all_states();
1730 qemu_system_reset(VMRESET_REPORT);
1731 resume_all_vcpus();
1732 if (runstate_check(RUN_STATE_INTERNAL_ERROR) ||
1733 runstate_check(RUN_STATE_SHUTDOWN)) {
1734 runstate_set(RUN_STATE_PAUSED);
1735 }
1736 }
Luiz Capitulino14058192012-08-08 17:29:17 -03001737 if (qemu_wakeup_requested()) {
1738 pause_all_vcpus();
1739 cpu_synchronize_all_states();
1740 qemu_system_reset(VMRESET_SILENT);
1741 resume_all_vcpus();
Luiz Capitulino17c86602012-08-09 11:27:30 -03001742 monitor_protocol_event(QEVENT_WAKEUP, NULL);
Luiz Capitulino14058192012-08-08 17:29:17 -03001743 }
Paolo Bonzini99435902011-09-12 14:03:13 +02001744 if (qemu_powerdown_requested()) {
Igor Mammedov013c2f12012-09-05 23:06:25 +02001745 qemu_system_powerdown();
Paolo Bonzini99435902011-09-12 14:03:13 +02001746 }
1747 if (qemu_vmstop_requested(&r)) {
1748 vm_stop(r);
1749 }
1750 return false;
1751}
1752
aliguori43b96852009-04-24 18:03:33 +00001753static void main_loop(void)
1754{
Jan Kiszkac9f711a2011-08-22 17:46:02 +02001755 bool nonblocking;
Paolo Bonzini99435902011-09-12 14:03:13 +02001756 int last_io = 0;
Jan Kiszka8e1b90e2011-02-01 22:15:46 +01001757#ifdef CONFIG_PROFILER
1758 int64_t ti;
1759#endif
Paolo Bonzini99435902011-09-12 14:03:13 +02001760 do {
Jan Kiszkac9f711a2011-08-22 17:46:02 +02001761 nonblocking = !kvm_enabled() && last_io > 0;
aliguori43b96852009-04-24 18:03:33 +00001762#ifdef CONFIG_PROFILER
Jan Kiszka46481d32011-02-01 22:15:47 +01001763 ti = profile_getclock();
aliguori43b96852009-04-24 18:03:33 +00001764#endif
Jan Kiszkac9f711a2011-08-22 17:46:02 +02001765 last_io = main_loop_wait(nonblocking);
aliguori43b96852009-04-24 18:03:33 +00001766#ifdef CONFIG_PROFILER
Jan Kiszka46481d32011-02-01 22:15:47 +01001767 dev_time += profile_getclock() - ti;
aliguori43b96852009-04-24 18:03:33 +00001768#endif
Paolo Bonzini99435902011-09-12 14:03:13 +02001769 } while (!main_loop_should_exit());
bellardb4608c02003-06-27 17:34:32 +00001770}
1771
pbrook9bd7e6d2009-04-07 22:58:45 +00001772static void version(void)
1773{
Thomas Monjalonf75ca1a2010-04-28 14:42:01 +02001774 printf("QEMU emulator version " QEMU_VERSION QEMU_PKGVERSION ", Copyright (c) 2003-2008 Fabrice Bellard\n");
pbrook9bd7e6d2009-04-07 22:58:45 +00001775}
1776
ths15f82202007-06-29 23:26:08 +00001777static void help(int exitcode)
bellard0824d6f2003-06-24 13:42:40 +00001778{
Michael Ellermana3adb7a2011-12-19 17:19:31 +11001779 version();
1780 printf("usage: %s [options] [disk_image]\n\n"
1781 "'disk_image' is a raw hard disk image for IDE hard disk 0\n\n",
1782 error_get_progname());
1783
Michael Ellerman77bd1112011-12-19 17:19:30 +11001784#define QEMU_OPTIONS_GENERATE_HELP
1785#include "qemu-options-wrapper.h"
Michael Ellermana3adb7a2011-12-19 17:19:31 +11001786
1787 printf("\nDuring emulation, the following keys are useful:\n"
malc3f020d72010-02-08 12:04:56 +03001788 "ctrl-alt-f toggle full screen\n"
1789 "ctrl-alt-n switch to virtual console 'n'\n"
1790 "ctrl-alt toggle mouse and keyboard grab\n"
1791 "\n"
Michael Ellermana3adb7a2011-12-19 17:19:31 +11001792 "When using -nographic, press 'ctrl-a h' to get some help.\n");
1793
ths15f82202007-06-29 23:26:08 +00001794 exit(exitcode);
bellard0824d6f2003-06-24 13:42:40 +00001795}
1796
bellardcd6f1162004-05-13 22:02:20 +00001797#define HAS_ARG 0x0001
1798
bellardcd6f1162004-05-13 22:02:20 +00001799typedef struct QEMUOption {
1800 const char *name;
1801 int flags;
1802 int index;
Blue Swirlad960902010-03-29 19:23:52 +00001803 uint32_t arch_mask;
bellardcd6f1162004-05-13 22:02:20 +00001804} QEMUOption;
1805
blueswir1dbed7e42008-10-01 19:38:09 +00001806static const QEMUOption qemu_options[] = {
Blue Swirlad960902010-03-29 19:23:52 +00001807 { "h", 0, QEMU_OPTION_h, QEMU_ARCH_ALL },
Michael Ellerman77bd1112011-12-19 17:19:30 +11001808#define QEMU_OPTIONS_GENERATE_OPTIONS
1809#include "qemu-options-wrapper.h"
bellardcd6f1162004-05-13 22:02:20 +00001810 { NULL },
bellardfc01f7e2003-06-30 10:03:06 +00001811};
Blue Swirla369da52011-09-27 19:15:42 +00001812
1813static bool vga_available(void)
1814{
Aurelien Jarno36b7f272012-09-08 16:01:20 +02001815 return object_class_by_name("VGA") || object_class_by_name("isa-vga");
Blue Swirla369da52011-09-27 19:15:42 +00001816}
1817
1818static bool cirrus_vga_available(void)
1819{
Aurelien Jarno36b7f272012-09-08 16:01:20 +02001820 return object_class_by_name("cirrus-vga")
1821 || object_class_by_name("isa-cirrus-vga");
Blue Swirla369da52011-09-27 19:15:42 +00001822}
1823
1824static bool vmware_vga_available(void)
1825{
Aurelien Jarno36b7f272012-09-08 16:01:20 +02001826 return object_class_by_name("vmware-svga");
Blue Swirla369da52011-09-27 19:15:42 +00001827}
1828
Aurelien Jarno879049a2012-09-08 12:01:06 +02001829static bool qxl_vga_available(void)
1830{
1831 return object_class_by_name("qxl-vga");
1832}
1833
malc3893c122008-09-28 00:42:05 +00001834static void select_vgahw (const char *p)
1835{
1836 const char *opts;
1837
Zachary Amsden86176752009-07-30 00:15:02 -10001838 vga_interface_type = VGA_NONE;
malc3893c122008-09-28 00:42:05 +00001839 if (strstart(p, "std", &opts)) {
Blue Swirla369da52011-09-27 19:15:42 +00001840 if (vga_available()) {
1841 vga_interface_type = VGA_STD;
1842 } else {
1843 fprintf(stderr, "Error: standard VGA not available\n");
1844 exit(0);
1845 }
malc3893c122008-09-28 00:42:05 +00001846 } else if (strstart(p, "cirrus", &opts)) {
Blue Swirla369da52011-09-27 19:15:42 +00001847 if (cirrus_vga_available()) {
1848 vga_interface_type = VGA_CIRRUS;
1849 } else {
1850 fprintf(stderr, "Error: Cirrus VGA not available\n");
1851 exit(0);
1852 }
malc3893c122008-09-28 00:42:05 +00001853 } else if (strstart(p, "vmware", &opts)) {
Blue Swirla369da52011-09-27 19:15:42 +00001854 if (vmware_vga_available()) {
1855 vga_interface_type = VGA_VMWARE;
1856 } else {
1857 fprintf(stderr, "Error: VMWare SVGA not available\n");
1858 exit(0);
1859 }
aliguori94909d92009-04-22 15:19:53 +00001860 } else if (strstart(p, "xenfb", &opts)) {
Zachary Amsden86176752009-07-30 00:15:02 -10001861 vga_interface_type = VGA_XENFB;
Gerd Hoffmanna19cbfb2010-04-27 11:50:11 +02001862 } else if (strstart(p, "qxl", &opts)) {
Aurelien Jarno879049a2012-09-08 12:01:06 +02001863 if (qxl_vga_available()) {
1864 vga_interface_type = VGA_QXL;
1865 } else {
1866 fprintf(stderr, "Error: QXL VGA not available\n");
1867 exit(0);
1868 }
aliguori28b85ed2009-04-22 15:19:48 +00001869 } else if (!strstart(p, "none", &opts)) {
malc3893c122008-09-28 00:42:05 +00001870 invalid_vga:
1871 fprintf(stderr, "Unknown vga type: %s\n", p);
1872 exit(1);
1873 }
malccb5a7aa2008-09-28 00:42:12 +00001874 while (*opts) {
1875 const char *nextopt;
1876
1877 if (strstart(opts, ",retrace=", &nextopt)) {
1878 opts = nextopt;
1879 if (strstart(opts, "dumb", &nextopt))
1880 vga_retrace_method = VGA_RETRACE_DUMB;
1881 else if (strstart(opts, "precise", &nextopt))
1882 vga_retrace_method = VGA_RETRACE_PRECISE;
1883 else goto invalid_vga;
1884 } else goto invalid_vga;
1885 opts = nextopt;
1886 }
malc3893c122008-09-28 00:42:05 +00001887}
1888
Jes Sorensen1472a952011-03-16 13:33:31 +01001889static DisplayType select_display(const char *p)
1890{
1891 const char *opts;
1892 DisplayType display = DT_DEFAULT;
1893
1894 if (strstart(p, "sdl", &opts)) {
1895#ifdef CONFIG_SDL
1896 display = DT_SDL;
1897 while (*opts) {
1898 const char *nextopt;
1899
1900 if (strstart(opts, ",frame=", &nextopt)) {
1901 opts = nextopt;
1902 if (strstart(opts, "on", &nextopt)) {
1903 no_frame = 0;
1904 } else if (strstart(opts, "off", &nextopt)) {
1905 no_frame = 1;
1906 } else {
Peter Maydell05175532011-03-23 03:40:57 +00001907 goto invalid_sdl_args;
Jes Sorensen1472a952011-03-16 13:33:31 +01001908 }
1909 } else if (strstart(opts, ",alt_grab=", &nextopt)) {
1910 opts = nextopt;
1911 if (strstart(opts, "on", &nextopt)) {
1912 alt_grab = 1;
1913 } else if (strstart(opts, "off", &nextopt)) {
1914 alt_grab = 0;
1915 } else {
Peter Maydell05175532011-03-23 03:40:57 +00001916 goto invalid_sdl_args;
Jes Sorensen1472a952011-03-16 13:33:31 +01001917 }
1918 } else if (strstart(opts, ",ctrl_grab=", &nextopt)) {
1919 opts = nextopt;
1920 if (strstart(opts, "on", &nextopt)) {
1921 ctrl_grab = 1;
1922 } else if (strstart(opts, "off", &nextopt)) {
1923 ctrl_grab = 0;
1924 } else {
Peter Maydell05175532011-03-23 03:40:57 +00001925 goto invalid_sdl_args;
Jes Sorensen1472a952011-03-16 13:33:31 +01001926 }
1927 } else if (strstart(opts, ",window_close=", &nextopt)) {
1928 opts = nextopt;
1929 if (strstart(opts, "on", &nextopt)) {
1930 no_quit = 0;
1931 } else if (strstart(opts, "off", &nextopt)) {
1932 no_quit = 1;
1933 } else {
Peter Maydell05175532011-03-23 03:40:57 +00001934 goto invalid_sdl_args;
Jes Sorensen1472a952011-03-16 13:33:31 +01001935 }
1936 } else {
Peter Maydell05175532011-03-23 03:40:57 +00001937 invalid_sdl_args:
1938 fprintf(stderr, "Invalid SDL option string: %s\n", p);
1939 exit(1);
Jes Sorensen1472a952011-03-16 13:33:31 +01001940 }
1941 opts = nextopt;
1942 }
1943#else
1944 fprintf(stderr, "SDL support is disabled\n");
1945 exit(1);
1946#endif
Jes Sorensen3264ff12011-03-16 13:33:33 +01001947 } else if (strstart(p, "vnc", &opts)) {
Jes Sorensen821601e2011-03-16 13:33:36 +01001948#ifdef CONFIG_VNC
Jes Sorensen3264ff12011-03-16 13:33:33 +01001949 display_remote++;
1950
1951 if (*opts) {
1952 const char *nextopt;
1953
1954 if (strstart(opts, "=", &nextopt)) {
1955 vnc_display = nextopt;
1956 }
1957 }
1958 if (!vnc_display) {
1959 fprintf(stderr, "VNC requires a display argument vnc=<display>\n");
1960 exit(1);
1961 }
Jes Sorensen821601e2011-03-16 13:33:36 +01001962#else
1963 fprintf(stderr, "VNC support is disabled\n");
1964 exit(1);
1965#endif
Jes Sorensen1472a952011-03-16 13:33:31 +01001966 } else if (strstart(p, "curses", &opts)) {
1967#ifdef CONFIG_CURSES
1968 display = DT_CURSES;
1969#else
1970 fprintf(stderr, "Curses support is disabled\n");
1971 exit(1);
1972#endif
Jes Sorensen4171d322011-03-16 13:33:32 +01001973 } else if (strstart(p, "none", &opts)) {
1974 display = DT_NONE;
Jes Sorensen1472a952011-03-16 13:33:31 +01001975 } else {
Jes Sorensen1472a952011-03-16 13:33:31 +01001976 fprintf(stderr, "Unknown display type: %s\n", p);
1977 exit(1);
1978 }
1979
1980 return display;
1981}
1982
Markus Armbruster7d4c3d52009-06-26 19:15:14 +02001983static int balloon_parse(const char *arg)
1984{
Gerd Hoffmann382f0742009-08-14 10:34:22 +02001985 QemuOpts *opts;
Markus Armbruster7d4c3d52009-06-26 19:15:14 +02001986
Gerd Hoffmann382f0742009-08-14 10:34:22 +02001987 if (strcmp(arg, "none") == 0) {
1988 return 0;
Markus Armbruster7d4c3d52009-06-26 19:15:14 +02001989 }
Gerd Hoffmann382f0742009-08-14 10:34:22 +02001990
1991 if (!strncmp(arg, "virtio", 6)) {
1992 if (arg[6] == ',') {
1993 /* have params -> parse them */
Gerd Hoffmann3329f072010-08-20 13:52:01 +02001994 opts = qemu_opts_parse(qemu_find_opts("device"), arg+7, 0);
Gerd Hoffmann382f0742009-08-14 10:34:22 +02001995 if (!opts)
1996 return -1;
1997 } else {
1998 /* create empty opts */
Dong Xu Wange478b442012-12-06 14:47:22 +08001999 opts = qemu_opts_create_nofail(qemu_find_opts("device"));
Gerd Hoffmann382f0742009-08-14 10:34:22 +02002000 }
Alexander Graf29f82b32011-03-29 15:29:29 +02002001 qemu_opt_set(opts, "driver", "virtio-balloon");
Gerd Hoffmann382f0742009-08-14 10:34:22 +02002002 return 0;
2003 }
2004
2005 return -1;
Markus Armbruster7d4c3d52009-06-26 19:15:14 +02002006}
Markus Armbruster7d4c3d52009-06-26 19:15:14 +02002007
Paul Brook5cea8592009-05-30 00:52:44 +01002008char *qemu_find_file(int type, const char *name)
2009{
2010 int len;
2011 const char *subdir;
2012 char *buf;
2013
Peter Maydell31783202012-05-25 13:07:01 +01002014 /* Try the name as a straight path first */
2015 if (access(name, R_OK) == 0) {
Anthony Liguori7267c092011-08-20 22:09:37 -05002016 return g_strdup(name);
Paul Brook5cea8592009-05-30 00:52:44 +01002017 }
2018 switch (type) {
2019 case QEMU_FILE_TYPE_BIOS:
2020 subdir = "";
2021 break;
2022 case QEMU_FILE_TYPE_KEYMAP:
2023 subdir = "keymaps/";
2024 break;
2025 default:
2026 abort();
2027 }
2028 len = strlen(data_dir) + strlen(name) + strlen(subdir) + 2;
Anthony Liguori7267c092011-08-20 22:09:37 -05002029 buf = g_malloc0(len);
Blue Swirl3a417592009-06-09 19:12:21 +00002030 snprintf(buf, len, "%s/%s%s", data_dir, subdir, name);
Paul Brook5cea8592009-05-30 00:52:44 +01002031 if (access(buf, R_OK)) {
Anthony Liguori7267c092011-08-20 22:09:37 -05002032 g_free(buf);
Paul Brook5cea8592009-05-30 00:52:44 +01002033 return NULL;
2034 }
2035 return buf;
2036}
2037
Markus Armbrusterff952ba2010-01-29 19:48:57 +01002038static int device_help_func(QemuOpts *opts, void *opaque)
2039{
2040 return qdev_device_help(opts);
2041}
2042
Gerd Hoffmannf31d07d2009-07-31 12:25:37 +02002043static int device_init_func(QemuOpts *opts, void *opaque)
2044{
2045 DeviceState *dev;
2046
2047 dev = qdev_device_add(opts);
2048 if (!dev)
2049 return -1;
2050 return 0;
2051}
2052
Gerd Hoffmann1a688d32009-12-08 13:11:36 +01002053static int chardev_init_func(QemuOpts *opts, void *opaque)
2054{
2055 CharDriverState *chr;
2056
Anthony Liguorif69554b2011-08-15 11:17:37 -05002057 chr = qemu_chr_new_from_opts(opts, NULL);
Gerd Hoffmann1a688d32009-12-08 13:11:36 +01002058 if (!chr)
2059 return -1;
2060 return 0;
2061}
2062
Venkateswararao Jujjuri (JV)758e8e32010-06-14 13:34:41 -07002063#ifdef CONFIG_VIRTFS
Gautham R Shenoy74db9202010-04-29 17:44:43 +05302064static int fsdev_init_func(QemuOpts *opts, void *opaque)
2065{
2066 int ret;
2067 ret = qemu_fsdev_add(opts);
2068
2069 return ret;
2070}
2071#endif
2072
Gerd Hoffmann88589342009-12-08 13:11:50 +01002073static int mon_init_func(QemuOpts *opts, void *opaque)
2074{
2075 CharDriverState *chr;
2076 const char *chardev;
2077 const char *mode;
2078 int flags;
2079
2080 mode = qemu_opt_get(opts, "mode");
2081 if (mode == NULL) {
2082 mode = "readline";
2083 }
2084 if (strcmp(mode, "readline") == 0) {
2085 flags = MONITOR_USE_READLINE;
2086 } else if (strcmp(mode, "control") == 0) {
2087 flags = MONITOR_USE_CONTROL;
2088 } else {
2089 fprintf(stderr, "unknown monitor mode \"%s\"\n", mode);
2090 exit(1);
2091 }
2092
Daniel P. Berrange39eaab92010-06-07 15:42:31 +01002093 if (qemu_opt_get_bool(opts, "pretty", 0))
2094 flags |= MONITOR_USE_PRETTY;
2095
Gerd Hoffmann88589342009-12-08 13:11:50 +01002096 if (qemu_opt_get_bool(opts, "default", 0))
2097 flags |= MONITOR_IS_DEFAULT;
2098
2099 chardev = qemu_opt_get(opts, "chardev");
2100 chr = qemu_chr_find(chardev);
2101 if (chr == NULL) {
2102 fprintf(stderr, "chardev \"%s\" not found\n", chardev);
2103 exit(1);
2104 }
2105
2106 monitor_init(chr, flags);
2107 return 0;
2108}
2109
Gerd Hoffmann6ca55822009-12-08 13:11:52 +01002110static void monitor_parse(const char *optarg, const char *mode)
Gerd Hoffmann88589342009-12-08 13:11:50 +01002111{
2112 static int monitor_device_index = 0;
2113 QemuOpts *opts;
2114 const char *p;
2115 char label[32];
2116 int def = 0;
2117
2118 if (strstart(optarg, "chardev:", &p)) {
2119 snprintf(label, sizeof(label), "%s", p);
2120 } else {
Jan Kiszka140e0652010-04-06 16:55:52 +02002121 snprintf(label, sizeof(label), "compat_monitor%d",
2122 monitor_device_index);
2123 if (monitor_device_index == 0) {
Gerd Hoffmann88589342009-12-08 13:11:50 +01002124 def = 1;
2125 }
2126 opts = qemu_chr_parse_compat(label, optarg);
2127 if (!opts) {
2128 fprintf(stderr, "parse error: %s\n", optarg);
2129 exit(1);
2130 }
2131 }
2132
Luiz Capitulino8be7e7e2012-03-20 15:51:57 -03002133 opts = qemu_opts_create(qemu_find_opts("mon"), label, 1, NULL);
Gerd Hoffmann88589342009-12-08 13:11:50 +01002134 if (!opts) {
2135 fprintf(stderr, "duplicate chardev: %s\n", label);
2136 exit(1);
2137 }
Gerd Hoffmann6ca55822009-12-08 13:11:52 +01002138 qemu_opt_set(opts, "mode", mode);
Gerd Hoffmann88589342009-12-08 13:11:50 +01002139 qemu_opt_set(opts, "chardev", label);
2140 if (def)
2141 qemu_opt_set(opts, "default", "on");
2142 monitor_device_index++;
2143}
2144
Gerd Hoffmannbd3c9482009-07-15 13:59:26 +02002145struct device_config {
2146 enum {
Gerd Hoffmannaee1b932009-12-08 13:11:53 +01002147 DEV_USB, /* -usbdevice */
2148 DEV_BT, /* -bt */
2149 DEV_SERIAL, /* -serial */
2150 DEV_PARALLEL, /* -parallel */
2151 DEV_VIRTCON, /* -virtioconsole */
H. Peter Anvinc9f398e2009-12-29 13:51:36 -08002152 DEV_DEBUGCON, /* -debugcon */
Markus Armbrusteref0c4a02012-02-07 15:09:13 +01002153 DEV_GDB, /* -gdb, -s */
Gerd Hoffmannbd3c9482009-07-15 13:59:26 +02002154 } type;
2155 const char *cmdline;
Markus Armbrusterd9a59542012-02-07 15:09:12 +01002156 Location loc;
Blue Swirl72cf2d42009-09-12 07:36:22 +00002157 QTAILQ_ENTRY(device_config) next;
Gerd Hoffmannbd3c9482009-07-15 13:59:26 +02002158};
Blue Swirl4fdcac02012-10-28 11:04:47 +00002159
2160static QTAILQ_HEAD(, device_config) device_configs =
2161 QTAILQ_HEAD_INITIALIZER(device_configs);
Gerd Hoffmannbd3c9482009-07-15 13:59:26 +02002162
2163static void add_device_config(int type, const char *cmdline)
2164{
2165 struct device_config *conf;
2166
Anthony Liguori7267c092011-08-20 22:09:37 -05002167 conf = g_malloc0(sizeof(*conf));
Gerd Hoffmannbd3c9482009-07-15 13:59:26 +02002168 conf->type = type;
2169 conf->cmdline = cmdline;
Markus Armbrusterd9a59542012-02-07 15:09:12 +01002170 loc_save(&conf->loc);
Blue Swirl72cf2d42009-09-12 07:36:22 +00002171 QTAILQ_INSERT_TAIL(&device_configs, conf, next);
Gerd Hoffmannbd3c9482009-07-15 13:59:26 +02002172}
2173
2174static int foreach_device_config(int type, int (*func)(const char *cmdline))
2175{
2176 struct device_config *conf;
2177 int rc;
2178
Blue Swirl72cf2d42009-09-12 07:36:22 +00002179 QTAILQ_FOREACH(conf, &device_configs, next) {
Gerd Hoffmannbd3c9482009-07-15 13:59:26 +02002180 if (conf->type != type)
2181 continue;
Markus Armbrusterd9a59542012-02-07 15:09:12 +01002182 loc_push_restore(&conf->loc);
Gerd Hoffmannbd3c9482009-07-15 13:59:26 +02002183 rc = func(conf->cmdline);
Markus Armbrusterd9a59542012-02-07 15:09:12 +01002184 loc_pop(&conf->loc);
Gerd Hoffmannbd3c9482009-07-15 13:59:26 +02002185 if (0 != rc)
2186 return rc;
2187 }
2188 return 0;
2189}
2190
Gerd Hoffmann998bbd72009-12-08 13:11:41 +01002191static int serial_parse(const char *devname)
2192{
2193 static int index = 0;
2194 char label[32];
2195
2196 if (strcmp(devname, "none") == 0)
2197 return 0;
2198 if (index == MAX_SERIAL_PORTS) {
2199 fprintf(stderr, "qemu: too many serial ports\n");
2200 exit(1);
2201 }
2202 snprintf(label, sizeof(label), "serial%d", index);
Anthony Liguori27143a42011-08-15 11:17:36 -05002203 serial_hds[index] = qemu_chr_new(label, devname, NULL);
Gerd Hoffmann998bbd72009-12-08 13:11:41 +01002204 if (!serial_hds[index]) {
Peter Maydell52d06132012-07-09 04:28:30 +00002205 fprintf(stderr, "qemu: could not connect serial device"
2206 " to character backend '%s'\n", devname);
Gerd Hoffmann998bbd72009-12-08 13:11:41 +01002207 return -1;
2208 }
2209 index++;
2210 return 0;
2211}
2212
Gerd Hoffmann6a5e8b02009-12-08 13:11:42 +01002213static int parallel_parse(const char *devname)
2214{
2215 static int index = 0;
2216 char label[32];
2217
2218 if (strcmp(devname, "none") == 0)
2219 return 0;
2220 if (index == MAX_PARALLEL_PORTS) {
2221 fprintf(stderr, "qemu: too many parallel ports\n");
2222 exit(1);
2223 }
2224 snprintf(label, sizeof(label), "parallel%d", index);
Anthony Liguori27143a42011-08-15 11:17:36 -05002225 parallel_hds[index] = qemu_chr_new(label, devname, NULL);
Gerd Hoffmann6a5e8b02009-12-08 13:11:42 +01002226 if (!parallel_hds[index]) {
Peter Maydell52d06132012-07-09 04:28:30 +00002227 fprintf(stderr, "qemu: could not connect parallel device"
2228 " to character backend '%s'\n", devname);
Gerd Hoffmann6a5e8b02009-12-08 13:11:42 +01002229 return -1;
2230 }
2231 index++;
2232 return 0;
2233}
2234
Gerd Hoffmannaee1b932009-12-08 13:11:53 +01002235static int virtcon_parse(const char *devname)
2236{
Gerd Hoffmann3329f072010-08-20 13:52:01 +02002237 QemuOptsList *device = qemu_find_opts("device");
Gerd Hoffmannaee1b932009-12-08 13:11:53 +01002238 static int index = 0;
2239 char label[32];
Amit Shah392ecf52010-01-21 16:19:23 +05302240 QemuOpts *bus_opts, *dev_opts;
Gerd Hoffmannaee1b932009-12-08 13:11:53 +01002241
2242 if (strcmp(devname, "none") == 0)
2243 return 0;
2244 if (index == MAX_VIRTIO_CONSOLES) {
2245 fprintf(stderr, "qemu: too many virtio consoles\n");
2246 exit(1);
2247 }
Amit Shah392ecf52010-01-21 16:19:23 +05302248
Dong Xu Wange478b442012-12-06 14:47:22 +08002249 bus_opts = qemu_opts_create_nofail(device);
Anthony Liguorie87f7fc2012-02-06 11:07:18 -06002250 if (arch_type == QEMU_ARCH_S390X) {
2251 qemu_opt_set(bus_opts, "driver", "virtio-serial-s390");
2252 } else {
2253 qemu_opt_set(bus_opts, "driver", "virtio-serial-pci");
2254 }
Amit Shah392ecf52010-01-21 16:19:23 +05302255
Dong Xu Wange478b442012-12-06 14:47:22 +08002256 dev_opts = qemu_opts_create_nofail(device);
Amit Shah392ecf52010-01-21 16:19:23 +05302257 qemu_opt_set(dev_opts, "driver", "virtconsole");
2258
Gerd Hoffmannaee1b932009-12-08 13:11:53 +01002259 snprintf(label, sizeof(label), "virtcon%d", index);
Anthony Liguori27143a42011-08-15 11:17:36 -05002260 virtcon_hds[index] = qemu_chr_new(label, devname, NULL);
Gerd Hoffmannaee1b932009-12-08 13:11:53 +01002261 if (!virtcon_hds[index]) {
Peter Maydell52d06132012-07-09 04:28:30 +00002262 fprintf(stderr, "qemu: could not connect virtio console"
2263 " to character backend '%s'\n", devname);
Gerd Hoffmannaee1b932009-12-08 13:11:53 +01002264 return -1;
2265 }
Amit Shah392ecf52010-01-21 16:19:23 +05302266 qemu_opt_set(dev_opts, "chardev", label);
2267
Gerd Hoffmannaee1b932009-12-08 13:11:53 +01002268 index++;
2269 return 0;
2270}
2271
H. Peter Anvinc9f398e2009-12-29 13:51:36 -08002272static int debugcon_parse(const char *devname)
2273{
2274 QemuOpts *opts;
2275
Anthony Liguori27143a42011-08-15 11:17:36 -05002276 if (!qemu_chr_new("debugcon", devname, NULL)) {
H. Peter Anvinc9f398e2009-12-29 13:51:36 -08002277 exit(1);
2278 }
Luiz Capitulino8be7e7e2012-03-20 15:51:57 -03002279 opts = qemu_opts_create(qemu_find_opts("device"), "debugcon", 1, NULL);
H. Peter Anvinc9f398e2009-12-29 13:51:36 -08002280 if (!opts) {
2281 fprintf(stderr, "qemu: already have a debugcon device\n");
2282 exit(1);
2283 }
2284 qemu_opt_set(opts, "driver", "isa-debugcon");
2285 qemu_opt_set(opts, "chardev", "debugcon");
2286 return 0;
2287}
2288
Jan Kiszka9052ea62011-07-23 12:38:37 +02002289static QEMUMachine *machine_parse(const char *name)
2290{
2291 QEMUMachine *m, *machine = NULL;
2292
2293 if (name) {
2294 machine = find_machine(name);
2295 }
2296 if (machine) {
2297 return machine;
2298 }
2299 printf("Supported machines are:\n");
2300 for (m = first_machine; m != NULL; m = m->next) {
2301 if (m->alias) {
Peter Maydell3b264862012-02-22 22:13:11 +00002302 printf("%-20s %s (alias of %s)\n", m->alias, m->desc, m->name);
Jan Kiszka9052ea62011-07-23 12:38:37 +02002303 }
Peter Maydell3b264862012-02-22 22:13:11 +00002304 printf("%-20s %s%s\n", m->name, m->desc,
Jan Kiszka9052ea62011-07-23 12:38:37 +02002305 m->is_default ? " (default)" : "");
2306 }
Peter Maydellc8057f92012-08-02 13:45:54 +01002307 exit(!name || !is_help_option(name));
Jan Kiszka9052ea62011-07-23 12:38:37 +02002308}
2309
Anthony PERARD303d4e82010-09-21 20:05:31 +01002310static int tcg_init(void)
2311{
Jan Kiszkad5ab9712011-08-02 16:10:21 +02002312 tcg_exec_init(tcg_tb_size * 1024 * 1024);
Anthony PERARD303d4e82010-09-21 20:05:31 +01002313 return 0;
2314}
2315
2316static struct {
2317 const char *opt_name;
2318 const char *name;
2319 int (*available)(void);
2320 int (*init)(void);
2321 int *allowed;
2322} accel_list[] = {
2323 { "tcg", "tcg", tcg_available, tcg_init, &tcg_allowed },
Anthony PERARD3285cf42010-08-19 12:27:56 +01002324 { "xen", "Xen", xen_available, xen_init, &xen_allowed },
Anthony PERARD303d4e82010-09-21 20:05:31 +01002325 { "kvm", "KVM", kvm_available, kvm_init, &kvm_allowed },
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +02002326 { "qtest", "QTest", qtest_available, qtest_init, &qtest_allowed },
Anthony PERARD303d4e82010-09-21 20:05:31 +01002327};
2328
2329static int configure_accelerator(void)
2330{
2331 const char *p = NULL;
2332 char buf[10];
2333 int i, ret;
Peter Maydell1b785a92012-02-07 20:57:27 +00002334 bool accel_initialised = 0;
Anthony PERARD303d4e82010-09-21 20:05:31 +01002335 bool init_failed = 0;
2336
2337 QemuOptsList *list = qemu_find_opts("machine");
2338 if (!QTAILQ_EMPTY(&list->head)) {
2339 p = qemu_opt_get(QTAILQ_FIRST(&list->head), "accel");
2340 }
2341
2342 if (p == NULL) {
2343 /* Use the default "accelerator", tcg */
2344 p = "tcg";
2345 }
2346
Peter Maydell1b785a92012-02-07 20:57:27 +00002347 while (!accel_initialised && *p != '\0') {
Anthony PERARD303d4e82010-09-21 20:05:31 +01002348 if (*p == ':') {
2349 p++;
2350 }
2351 p = get_opt_name(buf, sizeof (buf), p, ':');
2352 for (i = 0; i < ARRAY_SIZE(accel_list); i++) {
2353 if (strcmp(accel_list[i].opt_name, buf) == 0) {
Anthony Liguoria16c53b2011-06-06 08:25:06 -05002354 *(accel_list[i].allowed) = 1;
Anthony PERARD303d4e82010-09-21 20:05:31 +01002355 ret = accel_list[i].init();
2356 if (ret < 0) {
2357 init_failed = 1;
2358 if (!accel_list[i].available()) {
2359 printf("%s not supported for this target\n",
2360 accel_list[i].name);
2361 } else {
2362 fprintf(stderr, "failed to initialize %s: %s\n",
2363 accel_list[i].name,
2364 strerror(-ret));
2365 }
Anthony Liguoria16c53b2011-06-06 08:25:06 -05002366 *(accel_list[i].allowed) = 0;
Anthony PERARD303d4e82010-09-21 20:05:31 +01002367 } else {
Peter Maydell1b785a92012-02-07 20:57:27 +00002368 accel_initialised = 1;
Anthony PERARD303d4e82010-09-21 20:05:31 +01002369 }
2370 break;
2371 }
2372 }
2373 if (i == ARRAY_SIZE(accel_list)) {
2374 fprintf(stderr, "\"%s\" accelerator does not exist.\n", buf);
2375 }
2376 }
2377
Peter Maydell1b785a92012-02-07 20:57:27 +00002378 if (!accel_initialised) {
Anthony PERARD303d4e82010-09-21 20:05:31 +01002379 fprintf(stderr, "No accelerator found!\n");
2380 exit(1);
2381 }
2382
2383 if (init_failed) {
2384 fprintf(stderr, "Back to %s accelerator.\n", accel_list[i].name);
2385 }
2386
Peter Maydell1b785a92012-02-07 20:57:27 +00002387 return !accel_initialised;
Anthony PERARD303d4e82010-09-21 20:05:31 +01002388}
2389
Gerd Hoffmannfd42dee2010-06-04 14:08:07 +02002390void qemu_add_exit_notifier(Notifier *notify)
2391{
2392 notifier_list_add(&exit_notifiers, notify);
2393}
2394
2395void qemu_remove_exit_notifier(Notifier *notify)
2396{
Paolo Bonzini31552522012-01-13 17:34:01 +01002397 notifier_remove(notify);
Gerd Hoffmannfd42dee2010-06-04 14:08:07 +02002398}
2399
2400static void qemu_run_exit_notifiers(void)
2401{
Jan Kiszka9e8dd452011-06-20 14:06:26 +02002402 notifier_list_notify(&exit_notifiers, NULL);
Gerd Hoffmannfd42dee2010-06-04 14:08:07 +02002403}
2404
Gleb Natapov4cab9462010-12-08 13:35:08 +02002405void qemu_add_machine_init_done_notifier(Notifier *notify)
2406{
2407 notifier_list_add(&machine_init_done_notifiers, notify);
2408}
2409
2410static void qemu_run_machine_init_done_notifiers(void)
2411{
Jan Kiszka9e8dd452011-06-20 14:06:26 +02002412 notifier_list_notify(&machine_init_done_notifiers, NULL);
Gleb Natapov4cab9462010-12-08 13:35:08 +02002413}
2414
Anthony Liguori6530a972010-01-22 09:18:06 -06002415static const QEMUOption *lookup_opt(int argc, char **argv,
2416 const char **poptarg, int *poptind)
2417{
2418 const QEMUOption *popt;
2419 int optind = *poptind;
2420 char *r = argv[optind];
2421 const char *optarg;
2422
Markus Armbruster0f0bc3f2010-02-18 20:13:51 +01002423 loc_set_cmdline(argv, optind, 1);
Anthony Liguori6530a972010-01-22 09:18:06 -06002424 optind++;
2425 /* Treat --foo the same as -foo. */
2426 if (r[1] == '-')
2427 r++;
2428 popt = qemu_options;
2429 for(;;) {
2430 if (!popt->name) {
Markus Armbruster0f0bc3f2010-02-18 20:13:51 +01002431 error_report("invalid option");
Anthony Liguori6530a972010-01-22 09:18:06 -06002432 exit(1);
2433 }
2434 if (!strcmp(popt->name, r + 1))
2435 break;
2436 popt++;
2437 }
2438 if (popt->flags & HAS_ARG) {
2439 if (optind >= argc) {
Markus Armbruster0f0bc3f2010-02-18 20:13:51 +01002440 error_report("requires an argument");
Anthony Liguori6530a972010-01-22 09:18:06 -06002441 exit(1);
2442 }
2443 optarg = argv[optind++];
Markus Armbruster0f0bc3f2010-02-18 20:13:51 +01002444 loc_set_cmdline(argv, optind - 2, 2);
Anthony Liguori6530a972010-01-22 09:18:06 -06002445 } else {
2446 optarg = NULL;
2447 }
2448
2449 *poptarg = optarg;
2450 *poptind = optind;
2451
2452 return popt;
2453}
2454
Anthony Liguori07501122011-08-20 22:38:31 -05002455static gpointer malloc_and_trace(gsize n_bytes)
2456{
2457 void *ptr = malloc(n_bytes);
Frediano Ziglioa74cd8c2011-08-31 09:25:35 +02002458 trace_g_malloc(n_bytes, ptr);
Anthony Liguori07501122011-08-20 22:38:31 -05002459 return ptr;
2460}
2461
2462static gpointer realloc_and_trace(gpointer mem, gsize n_bytes)
2463{
2464 void *ptr = realloc(mem, n_bytes);
Frediano Ziglioa74cd8c2011-08-31 09:25:35 +02002465 trace_g_realloc(mem, n_bytes, ptr);
Anthony Liguori07501122011-08-20 22:38:31 -05002466 return ptr;
2467}
2468
2469static void free_and_trace(gpointer mem)
2470{
Frediano Ziglioa74cd8c2011-08-31 09:25:35 +02002471 trace_g_free(mem);
Anthony Liguori07501122011-08-20 22:38:31 -05002472 free(mem);
2473}
2474
Anthony Liguori68d98d32012-06-25 14:36:33 -05002475static int object_set_property(const char *name, const char *value, void *opaque)
2476{
2477 Object *obj = OBJECT(opaque);
2478 StringInputVisitor *siv;
2479 Error *local_err = NULL;
2480
2481 if (strcmp(name, "qom-type") == 0 || strcmp(name, "id") == 0) {
2482 return 0;
2483 }
2484
2485 siv = string_input_visitor_new(value);
2486 object_property_set(obj, string_input_get_visitor(siv), name, &local_err);
2487 string_input_visitor_cleanup(siv);
2488
2489 if (local_err) {
2490 qerror_report_err(local_err);
2491 error_free(local_err);
2492 return -1;
2493 }
2494
2495 return 0;
2496}
2497
2498static int object_create(QemuOpts *opts, void *opaque)
2499{
2500 const char *type = qemu_opt_get(opts, "qom-type");
2501 const char *id = qemu_opts_id(opts);
2502 Object *obj;
2503
2504 g_assert(type != NULL);
2505
2506 if (id == NULL) {
2507 qerror_report(QERR_MISSING_PARAMETER, "id");
2508 return -1;
2509 }
2510
2511 obj = object_new(type);
2512 if (qemu_opt_foreach(opts, object_set_property, obj, 1) < 0) {
2513 return -1;
2514 }
2515
2516 object_property_add_child(container_get(object_get_root(), "/objects"),
2517 id, obj, NULL);
2518
2519 return 0;
2520}
2521
malc902b3d52008-12-10 19:18:40 +00002522int main(int argc, char **argv, char **envp)
bellard0824d6f2003-06-24 13:42:40 +00002523{
thse4bcb142007-12-02 04:51:10 +00002524 int i;
Eduardo Habkostda1fcfd2010-04-06 19:22:07 -03002525 int snapshot, linux_boot;
Paolo Bonzini4e3de9e2010-03-10 11:38:48 +01002526 const char *icount_option = NULL;
bellard7f7f9872003-10-30 01:11:23 +00002527 const char *initrd_filename;
bellarda20dd502003-09-30 21:07:02 +00002528 const char *kernel_filename, *kernel_cmdline;
Anthony Liguori195325a2009-10-30 12:42:29 -05002529 char boot_devices[33] = "cad"; /* default to HD->floppy->CD-ROM */
aliguori3023f332009-01-16 19:04:14 +00002530 DisplayState *ds;
malc9f227bc2012-08-27 18:33:22 +04002531 int cyls, heads, secs, translation;
2532 QemuOpts *hda_opts = NULL, *opts, *machine_opts;
Gerd Hoffmann03b0ba72010-08-20 13:52:02 +02002533 QemuOptsList *olist;
bellardcd6f1162004-05-13 22:02:20 +00002534 int optind;
Anthony Liguori6530a972010-01-22 09:18:06 -06002535 const char *optarg;
bellardd63d3072004-10-03 13:29:03 +00002536 const char *loadvm = NULL;
bellardcc1daa42005-06-05 14:49:17 +00002537 QEMUMachine *machine;
j_mayer94fc95c2007-03-05 19:44:02 +00002538 const char *cpu_model;
Paolo Bonzini7f1b17f2012-05-10 09:39:17 +02002539 const char *vga_model = "none";
ths93815bc2007-03-19 15:58:31 +00002540 const char *pid_file = NULL;
aliguori5bb79102008-10-13 03:12:02 +00002541 const char *incoming = NULL;
Jes Sorensen821601e2011-03-16 13:33:36 +01002542#ifdef CONFIG_VNC
Anthony Liguori993fbfd2009-05-21 16:54:00 -05002543 int show_vnc_port = 0;
Jes Sorensen821601e2011-03-16 13:33:36 +01002544#endif
Eduardo Habkost3ed2d9e2012-05-02 13:07:28 -03002545 bool defconfig = true;
Eduardo Habkostf29a5612012-05-02 13:07:29 -03002546 bool userconfig = true;
Matthew Fernandezc235d732011-06-07 16:32:40 +00002547 const char *log_mask = NULL;
2548 const char *log_file = NULL;
Anthony Liguori07501122011-08-20 22:38:31 -05002549 GMemVTable mem_trace = {
2550 .malloc = malloc_and_trace,
2551 .realloc = realloc_and_trace,
2552 .free = free_and_trace,
2553 };
Lluís23d15e82011-08-31 20:31:31 +02002554 const char *trace_events = NULL;
Lluíse4858972011-08-31 20:31:03 +02002555 const char *trace_file = NULL;
Stefan Hajnoczi0b5538c2011-02-26 18:38:39 +00002556
Gerd Hoffmannfd42dee2010-06-04 14:08:07 +02002557 atexit(qemu_run_exit_notifiers);
Markus Armbruster65abca02010-02-24 14:37:14 +01002558 error_set_progname(argv[0]);
2559
Anthony Liguori07501122011-08-20 22:38:31 -05002560 g_mem_set_vtable(&mem_trace);
Andreas Färberdb529aa2011-10-31 19:14:56 +01002561 if (!g_thread_supported()) {
Alon Levy42ed3722011-12-20 13:41:04 +02002562#if !GLIB_CHECK_VERSION(2, 31, 0)
Andreas Färberdb529aa2011-10-31 19:14:56 +01002563 g_thread_init(NULL);
Alon Levy42ed3722011-12-20 13:41:04 +02002564#else
2565 fprintf(stderr, "glib threading failed to initialize.\n");
2566 exit(1);
2567#endif
Andreas Färberdb529aa2011-10-31 19:14:56 +01002568 }
Anthony Liguori07501122011-08-20 22:38:31 -05002569
Andreas Färber1b71f7c2012-03-04 21:32:35 +01002570 module_call_init(MODULE_INIT_QOM);
2571
Luiz Capitulino5db9d4d2011-09-05 16:36:31 -03002572 runstate_init();
2573
Jan Kiszka68752042009-09-15 13:36:04 +02002574 init_clocks();
Paolo Bonzini2ff68d02011-09-12 16:21:44 +02002575 rtc_clock = host_clock;
Jan Kiszka68752042009-09-15 13:36:04 +02002576
malc902b3d52008-12-10 19:18:40 +00002577 qemu_cache_utils_init(envp);
2578
Blue Swirl72cf2d42009-09-12 07:36:22 +00002579 QLIST_INIT (&vm_change_state_head);
Jes Sorensenfe98ac12010-06-10 11:42:21 +02002580 os_setup_early_signal_handling();
bellardbe995c22006-06-25 16:25:21 +00002581
Anthony Liguorif80f9ec2009-05-20 18:38:09 -05002582 module_call_init(MODULE_INIT_MACHINE);
Anthony Liguori0c257432009-05-21 20:41:01 -05002583 machine = find_default_machine();
j_mayer94fc95c2007-03-05 19:44:02 +00002584 cpu_model = NULL;
aurel324fc5d072008-04-27 21:39:40 +00002585 ram_size = 0;
bellard33e39632003-07-06 17:15:21 +00002586 snapshot = 0;
malc9f227bc2012-08-27 18:33:22 +04002587 cyls = heads = secs = 0;
2588 translation = BIOS_ATA_TRANSLATION_AUTO;
bellardc4b1fcc2004-03-14 21:44:30 +00002589
aliguori268a3622009-04-21 22:30:27 +00002590 for (i = 0; i < MAX_NODES; i++) {
2591 node_mem[i] = 0;
Chegu Vinodee785fe2012-07-16 21:31:30 -07002592 node_cpumask[i] = bitmap_new(MAX_CPUMASK_BITS);
aliguori268a3622009-04-21 22:30:27 +00002593 }
2594
aliguori268a3622009-04-21 22:30:27 +00002595 nb_numa_nodes = 0;
bellard7c9d8e02005-11-15 22:16:05 +00002596 nb_nics = 0;
ths3b46e622007-09-17 08:09:54 +00002597
blueswir141bd6392008-10-05 09:56:21 +00002598 autostart= 1;
2599
Anthony Liguori292444c2010-01-21 10:57:58 -06002600 /* first pass of option parsing */
2601 optind = 1;
2602 while (optind < argc) {
2603 if (argv[optind][0] != '-') {
2604 /* disk image */
Anthony Liguori28e68d62010-01-27 10:46:00 -06002605 optind++;
Anthony Liguori292444c2010-01-21 10:57:58 -06002606 continue;
2607 } else {
2608 const QEMUOption *popt;
2609
2610 popt = lookup_opt(argc, argv, &optarg, &optind);
2611 switch (popt->index) {
2612 case QEMU_OPTION_nodefconfig:
Eduardo Habkost3ed2d9e2012-05-02 13:07:28 -03002613 defconfig = false;
Anthony Liguori292444c2010-01-21 10:57:58 -06002614 break;
Eduardo Habkostf29a5612012-05-02 13:07:29 -03002615 case QEMU_OPTION_nouserconfig:
2616 userconfig = false;
2617 break;
Anthony Liguori292444c2010-01-21 10:57:58 -06002618 }
2619 }
2620 }
2621
2622 if (defconfig) {
Kevin Wolfdcfb0932010-03-05 17:25:55 +01002623 int ret;
Eduardo Habkostf29a5612012-05-02 13:07:29 -03002624 ret = qemu_read_default_config_files(userconfig);
Eduardo Habkostb5a8fe52012-05-02 13:07:25 -03002625 if (ret < 0) {
Kevin Wolfdcfb0932010-03-05 17:25:55 +01002626 exit(1);
Anthony Liguori292444c2010-01-21 10:57:58 -06002627 }
2628 }
2629
2630 /* second pass of option parsing */
bellardcd6f1162004-05-13 22:02:20 +00002631 optind = 1;
bellard0824d6f2003-06-24 13:42:40 +00002632 for(;;) {
bellardcd6f1162004-05-13 22:02:20 +00002633 if (optind >= argc)
bellard0824d6f2003-06-24 13:42:40 +00002634 break;
Anthony Liguori6530a972010-01-22 09:18:06 -06002635 if (argv[optind][0] != '-') {
malc9f227bc2012-08-27 18:33:22 +04002636 hda_opts = drive_add(IF_DEFAULT, 0, argv[optind++], HD_OPTS);
bellardcd6f1162004-05-13 22:02:20 +00002637 } else {
2638 const QEMUOption *popt;
2639
Anthony Liguori6530a972010-01-22 09:18:06 -06002640 popt = lookup_opt(argc, argv, &optarg, &optind);
Blue Swirlad960902010-03-29 19:23:52 +00002641 if (!(popt->arch_mask & arch_type)) {
2642 printf("Option %s not supported for this target\n", popt->name);
2643 exit(1);
2644 }
bellardcd6f1162004-05-13 22:02:20 +00002645 switch(popt->index) {
bellardcc1daa42005-06-05 14:49:17 +00002646 case QEMU_OPTION_M:
Jan Kiszka9052ea62011-07-23 12:38:37 +02002647 machine = machine_parse(optarg);
bellardcc1daa42005-06-05 14:49:17 +00002648 break;
Jan Kiszkae43d5942012-10-05 14:51:40 -03002649 case QEMU_OPTION_no_kvm_irqchip: {
2650 olist = qemu_find_opts("machine");
2651 qemu_opts_parse(olist, "kernel_irqchip=off", 0);
2652 break;
2653 }
j_mayer94fc95c2007-03-05 19:44:02 +00002654 case QEMU_OPTION_cpu:
2655 /* hw initialization will check this */
Eduardo Habkostecf40be2012-03-09 16:19:07 -03002656 cpu_model = optarg;
j_mayer94fc95c2007-03-05 19:44:02 +00002657 break;
bellardcd6f1162004-05-13 22:02:20 +00002658 case QEMU_OPTION_hda:
malc9f227bc2012-08-27 18:33:22 +04002659 {
2660 char buf[256];
2661 if (cyls == 0)
2662 snprintf(buf, sizeof(buf), "%s", HD_OPTS);
2663 else
2664 snprintf(buf, sizeof(buf),
2665 "%s,cyls=%d,heads=%d,secs=%d%s",
2666 HD_OPTS , cyls, heads, secs,
2667 translation == BIOS_ATA_TRANSLATION_LBA ?
2668 ",trans=lba" :
2669 translation == BIOS_ATA_TRANSLATION_NONE ?
2670 ",trans=none" : "");
2671 drive_add(IF_DEFAULT, 0, optarg, buf);
2672 break;
2673 }
bellardcd6f1162004-05-13 22:02:20 +00002674 case QEMU_OPTION_hdb:
bellardcc1daa42005-06-05 14:49:17 +00002675 case QEMU_OPTION_hdc:
2676 case QEMU_OPTION_hdd:
Markus Armbruster2292dda2011-01-28 11:21:41 +01002677 drive_add(IF_DEFAULT, popt->index - QEMU_OPTION_hda, optarg,
2678 HD_OPTS);
bellardfc01f7e2003-06-30 10:03:06 +00002679 break;
thse4bcb142007-12-02 04:51:10 +00002680 case QEMU_OPTION_drive:
Michael Tokareve2982c32011-03-30 16:31:05 +04002681 if (drive_def(optarg) == NULL) {
2682 exit(1);
2683 }
thse4bcb142007-12-02 04:51:10 +00002684 break;
Gerd Hoffmannd058fe02009-07-31 12:25:36 +02002685 case QEMU_OPTION_set:
2686 if (qemu_set_option(optarg) != 0)
2687 exit(1);
2688 break;
Gerd Hoffmannd0fef6f2009-12-08 13:11:34 +01002689 case QEMU_OPTION_global:
2690 if (qemu_global_option(optarg) != 0)
2691 exit(1);
2692 break;
balrog3e3d5812007-04-30 02:09:25 +00002693 case QEMU_OPTION_mtdblock:
Markus Armbruster2292dda2011-01-28 11:21:41 +01002694 drive_add(IF_MTD, -1, optarg, MTD_OPTS);
balrog3e3d5812007-04-30 02:09:25 +00002695 break;
pbrooka1bb27b2007-04-06 16:49:48 +00002696 case QEMU_OPTION_sd:
Markus Armbruster2292dda2011-01-28 11:21:41 +01002697 drive_add(IF_SD, 0, optarg, SD_OPTS);
pbrooka1bb27b2007-04-06 16:49:48 +00002698 break;
j_mayer86f55662007-04-24 06:52:59 +00002699 case QEMU_OPTION_pflash:
Markus Armbruster2292dda2011-01-28 11:21:41 +01002700 drive_add(IF_PFLASH, -1, optarg, PFLASH_OPTS);
j_mayer86f55662007-04-24 06:52:59 +00002701 break;
bellardcd6f1162004-05-13 22:02:20 +00002702 case QEMU_OPTION_snapshot:
bellard33e39632003-07-06 17:15:21 +00002703 snapshot = 1;
2704 break;
bellardcd6f1162004-05-13 22:02:20 +00002705 case QEMU_OPTION_hdachs:
bellard330d0412003-07-26 18:11:40 +00002706 {
bellard330d0412003-07-26 18:11:40 +00002707 const char *p;
2708 p = optarg;
2709 cyls = strtol(p, (char **)&p, 0);
bellard46d47672004-11-16 01:45:27 +00002710 if (cyls < 1 || cyls > 16383)
2711 goto chs_fail;
bellard330d0412003-07-26 18:11:40 +00002712 if (*p != ',')
2713 goto chs_fail;
2714 p++;
2715 heads = strtol(p, (char **)&p, 0);
bellard46d47672004-11-16 01:45:27 +00002716 if (heads < 1 || heads > 16)
2717 goto chs_fail;
bellard330d0412003-07-26 18:11:40 +00002718 if (*p != ',')
2719 goto chs_fail;
2720 p++;
2721 secs = strtol(p, (char **)&p, 0);
bellard46d47672004-11-16 01:45:27 +00002722 if (secs < 1 || secs > 63)
2723 goto chs_fail;
2724 if (*p == ',') {
2725 p++;
2726 if (!strcmp(p, "none"))
2727 translation = BIOS_ATA_TRANSLATION_NONE;
2728 else if (!strcmp(p, "lba"))
2729 translation = BIOS_ATA_TRANSLATION_LBA;
2730 else if (!strcmp(p, "auto"))
2731 translation = BIOS_ATA_TRANSLATION_AUTO;
2732 else
2733 goto chs_fail;
2734 } else if (*p != '\0') {
bellardc4b1fcc2004-03-14 21:44:30 +00002735 chs_fail:
bellard46d47672004-11-16 01:45:27 +00002736 fprintf(stderr, "qemu: invalid physical CHS format\n");
2737 exit(1);
bellardc4b1fcc2004-03-14 21:44:30 +00002738 }
malc9f227bc2012-08-27 18:33:22 +04002739 if (hda_opts != NULL) {
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +02002740 char num[16];
2741 snprintf(num, sizeof(num), "%d", cyls);
2742 qemu_opt_set(hda_opts, "cyls", num);
2743 snprintf(num, sizeof(num), "%d", heads);
2744 qemu_opt_set(hda_opts, "heads", num);
2745 snprintf(num, sizeof(num), "%d", secs);
2746 qemu_opt_set(hda_opts, "secs", num);
2747 if (translation == BIOS_ATA_TRANSLATION_LBA)
2748 qemu_opt_set(hda_opts, "trans", "lba");
2749 if (translation == BIOS_ATA_TRANSLATION_NONE)
2750 qemu_opt_set(hda_opts, "trans", "none");
2751 }
bellard330d0412003-07-26 18:11:40 +00002752 }
2753 break;
aliguori268a3622009-04-21 22:30:27 +00002754 case QEMU_OPTION_numa:
2755 if (nb_numa_nodes >= MAX_NODES) {
2756 fprintf(stderr, "qemu: too many NUMA nodes\n");
2757 exit(1);
2758 }
2759 numa_add(optarg);
2760 break;
Jes Sorensen1472a952011-03-16 13:33:31 +01002761 case QEMU_OPTION_display:
2762 display_type = select_display(optarg);
2763 break;
bellardcd6f1162004-05-13 22:02:20 +00002764 case QEMU_OPTION_nographic:
Anthony Liguori993fbfd2009-05-21 16:54:00 -05002765 display_type = DT_NOGRAPHIC;
bellarda20dd502003-09-30 21:07:02 +00002766 break;
balrog4d3b6f62008-02-10 16:33:14 +00002767 case QEMU_OPTION_curses:
Jes Sorensen47b05362011-03-16 13:33:35 +01002768#ifdef CONFIG_CURSES
Anthony Liguori993fbfd2009-05-21 16:54:00 -05002769 display_type = DT_CURSES;
Jes Sorensen47b05362011-03-16 13:33:35 +01002770#else
2771 fprintf(stderr, "Curses support is disabled\n");
2772 exit(1);
balrog4d3b6f62008-02-10 16:33:14 +00002773#endif
Jes Sorensen47b05362011-03-16 13:33:35 +01002774 break;
balroga171fe32007-04-30 01:48:07 +00002775 case QEMU_OPTION_portrait:
Vasily Khoruzhick93128052011-06-17 13:04:36 +03002776 graphic_rotate = 90;
2777 break;
2778 case QEMU_OPTION_rotate:
2779 graphic_rotate = strtol(optarg, (char **) &optarg, 10);
2780 if (graphic_rotate != 0 && graphic_rotate != 90 &&
2781 graphic_rotate != 180 && graphic_rotate != 270) {
2782 fprintf(stderr,
2783 "qemu: only 90, 180, 270 deg rotation is available\n");
2784 exit(1);
2785 }
balroga171fe32007-04-30 01:48:07 +00002786 break;
bellardcd6f1162004-05-13 22:02:20 +00002787 case QEMU_OPTION_kernel:
Peter Maydella0abe472012-02-08 05:41:39 +00002788 qemu_opts_set(qemu_find_opts("machine"), 0, "kernel", optarg);
2789 break;
2790 case QEMU_OPTION_initrd:
2791 qemu_opts_set(qemu_find_opts("machine"), 0, "initrd", optarg);
bellarda20dd502003-09-30 21:07:02 +00002792 break;
bellardcd6f1162004-05-13 22:02:20 +00002793 case QEMU_OPTION_append:
Peter Maydella0abe472012-02-08 05:41:39 +00002794 qemu_opts_set(qemu_find_opts("machine"), 0, "append", optarg);
bellard313aa562003-08-10 21:52:11 +00002795 break;
Grant Likely412beee2012-03-02 11:56:38 +00002796 case QEMU_OPTION_dtb:
2797 qemu_opts_set(qemu_find_opts("machine"), 0, "dtb", optarg);
2798 break;
bellardcd6f1162004-05-13 22:02:20 +00002799 case QEMU_OPTION_cdrom:
Markus Armbruster2292dda2011-01-28 11:21:41 +01002800 drive_add(IF_DEFAULT, 2, optarg, CDROM_OPTS);
bellard36b486b2003-11-11 13:36:08 +00002801 break;
bellardcd6f1162004-05-13 22:02:20 +00002802 case QEMU_OPTION_boot:
j_mayer28c5af52007-11-11 01:50:45 +00002803 {
Jan Kiszkaef3adf62009-07-02 00:19:02 +02002804 static const char * const params[] = {
wayne3d3b8302011-07-27 18:04:55 +08002805 "order", "once", "menu",
Amos Kongac05f342012-09-07 11:11:03 +08002806 "splash", "splash-time",
2807 "reboot-timeout", NULL
Jan Kiszkaef3adf62009-07-02 00:19:02 +02002808 };
2809 char buf[sizeof(boot_devices)];
Jan Kiszkae0f084b2009-07-02 00:19:02 +02002810 char *standard_boot_devices;
Jan Kiszkaef3adf62009-07-02 00:19:02 +02002811 int legacy = 0;
2812
2813 if (!strchr(optarg, '=')) {
2814 legacy = 1;
2815 pstrcpy(buf, sizeof(buf), optarg);
2816 } else if (check_params(buf, sizeof(buf), params, optarg) < 0) {
2817 fprintf(stderr,
2818 "qemu: unknown boot parameter '%s' in '%s'\n",
2819 buf, optarg);
2820 exit(1);
2821 }
2822
2823 if (legacy ||
2824 get_param_value(buf, sizeof(buf), "order", optarg)) {
Eduardo Habkost4e9e9d62010-04-06 19:22:08 -03002825 validate_bootdevices(buf);
Jan Kiszkaef3adf62009-07-02 00:19:02 +02002826 pstrcpy(boot_devices, sizeof(boot_devices), buf);
j_mayer28c5af52007-11-11 01:50:45 +00002827 }
Jan Kiszkae0f084b2009-07-02 00:19:02 +02002828 if (!legacy) {
2829 if (get_param_value(buf, sizeof(buf),
2830 "once", optarg)) {
Eduardo Habkost4e9e9d62010-04-06 19:22:08 -03002831 validate_bootdevices(buf);
Anthony Liguori7267c092011-08-20 22:09:37 -05002832 standard_boot_devices = g_strdup(boot_devices);
Jan Kiszkae0f084b2009-07-02 00:19:02 +02002833 pstrcpy(boot_devices, sizeof(boot_devices), buf);
2834 qemu_register_reset(restore_boot_devices,
2835 standard_boot_devices);
2836 }
Jan Kiszka95387492009-07-02 00:19:02 +02002837 if (get_param_value(buf, sizeof(buf),
2838 "menu", optarg)) {
2839 if (!strcmp(buf, "on")) {
2840 boot_menu = 1;
2841 } else if (!strcmp(buf, "off")) {
2842 boot_menu = 0;
2843 } else {
2844 fprintf(stderr,
2845 "qemu: invalid option value '%s'\n",
2846 buf);
2847 exit(1);
2848 }
2849 }
wayne3d3b8302011-07-27 18:04:55 +08002850 qemu_opts_parse(qemu_find_opts("boot-opts"),
2851 optarg, 0);
Jan Kiszkae0f084b2009-07-02 00:19:02 +02002852 }
bellard36b486b2003-11-11 13:36:08 +00002853 }
2854 break;
bellardcd6f1162004-05-13 22:02:20 +00002855 case QEMU_OPTION_fda:
bellardcd6f1162004-05-13 22:02:20 +00002856 case QEMU_OPTION_fdb:
Markus Armbruster2292dda2011-01-28 11:21:41 +01002857 drive_add(IF_FLOPPY, popt->index - QEMU_OPTION_fda,
2858 optarg, FD_OPTS);
bellardc45886d2004-01-05 00:02:06 +00002859 break;
bellard52ca8d62006-06-14 16:03:05 +00002860 case QEMU_OPTION_no_fd_bootchk:
2861 fd_bootchk = 0;
2862 break;
Mark McLoughlina1ea4582009-10-08 19:58:26 +01002863 case QEMU_OPTION_netdev:
Gerd Hoffmann3329f072010-08-20 13:52:01 +02002864 if (net_client_parse(qemu_find_opts("netdev"), optarg) == -1) {
Mark McLoughlina1ea4582009-10-08 19:58:26 +01002865 exit(1);
2866 }
2867 break;
bellard7c9d8e02005-11-15 22:16:05 +00002868 case QEMU_OPTION_net:
Gerd Hoffmann3329f072010-08-20 13:52:01 +02002869 if (net_client_parse(qemu_find_opts("net"), optarg) == -1) {
bellardc4b1fcc2004-03-14 21:44:30 +00002870 exit(1);
2871 }
bellard702c6512004-04-02 21:21:32 +00002872 break;
Ronnie Sahlbergf9dadc92012-01-26 09:39:02 +11002873#ifdef CONFIG_LIBISCSI
2874 case QEMU_OPTION_iscsi:
2875 opts = qemu_opts_parse(qemu_find_opts("iscsi"), optarg, 0);
2876 if (!opts) {
2877 exit(1);
2878 }
2879 break;
2880#endif
bellardc7f74642004-08-24 21:57:12 +00002881#ifdef CONFIG_SLIRP
2882 case QEMU_OPTION_tftp:
Jan Kiszkaad196a92009-06-24 14:42:28 +02002883 legacy_tftp_prefix = optarg;
bellard9bf05442004-08-25 22:12:49 +00002884 break;
ths47d5d012007-02-20 00:05:08 +00002885 case QEMU_OPTION_bootp:
Jan Kiszkaad196a92009-06-24 14:42:28 +02002886 legacy_bootp_filename = optarg;
ths47d5d012007-02-20 00:05:08 +00002887 break;
bellard9bf05442004-08-25 22:12:49 +00002888 case QEMU_OPTION_redir:
Markus Armbruster07527062009-10-06 12:16:57 +01002889 if (net_slirp_redir(optarg) < 0)
2890 exit(1);
bellard9bf05442004-08-25 22:12:49 +00002891 break;
bellardc7f74642004-08-24 21:57:12 +00002892#endif
balrogdc72ac12008-11-09 00:04:26 +00002893 case QEMU_OPTION_bt:
Gerd Hoffmannbd3c9482009-07-15 13:59:26 +02002894 add_device_config(DEV_BT, optarg);
balrogdc72ac12008-11-09 00:04:26 +00002895 break;
bellard1d14ffa2005-10-30 18:58:22 +00002896 case QEMU_OPTION_audio_help:
Blue Swirlad960902010-03-29 19:23:52 +00002897 if (!(audio_available())) {
2898 printf("Option %s not supported for this target\n", popt->name);
2899 exit(1);
2900 }
bellard1d14ffa2005-10-30 18:58:22 +00002901 AUD_help ();
2902 exit (0);
2903 break;
2904 case QEMU_OPTION_soundhw:
Blue Swirlad960902010-03-29 19:23:52 +00002905 if (!(audio_available())) {
2906 printf("Option %s not supported for this target\n", popt->name);
2907 exit(1);
2908 }
bellard1d14ffa2005-10-30 18:58:22 +00002909 select_soundhw (optarg);
2910 break;
bellardcd6f1162004-05-13 22:02:20 +00002911 case QEMU_OPTION_h:
ths15f82202007-06-29 23:26:08 +00002912 help(0);
bellardcd6f1162004-05-13 22:02:20 +00002913 break;
pbrook9bd7e6d2009-04-07 22:58:45 +00002914 case QEMU_OPTION_version:
2915 version();
2916 exit(0);
2917 break;
aurel3200f82b82008-04-27 21:12:55 +00002918 case QEMU_OPTION_m: {
Jes Sorensen70b4f4b2011-01-05 11:41:02 +01002919 int64_t value;
Markus Armbrusterff961012012-08-15 13:12:19 +02002920 uint64_t sz;
Markus Armbruster961b42b2011-11-22 09:46:03 +01002921 char *end;
aurel3200f82b82008-04-27 21:12:55 +00002922
Markus Armbruster961b42b2011-11-22 09:46:03 +01002923 value = strtosz(optarg, &end);
2924 if (value < 0 || *end) {
aurel3200f82b82008-04-27 21:12:55 +00002925 fprintf(stderr, "qemu: invalid ram size: %s\n", optarg);
bellardcd6f1162004-05-13 22:02:20 +00002926 exit(1);
2927 }
Markus Armbrusterff961012012-08-15 13:12:19 +02002928 sz = QEMU_ALIGN_UP((uint64_t)value, 8192);
2929 ram_size = sz;
2930 if (ram_size != sz) {
aurel3200f82b82008-04-27 21:12:55 +00002931 fprintf(stderr, "qemu: ram size too large\n");
2932 exit(1);
2933 }
bellardcd6f1162004-05-13 22:02:20 +00002934 break;
aurel3200f82b82008-04-27 21:12:55 +00002935 }
Marcelo Tosattic9027602010-03-01 20:25:08 -03002936 case QEMU_OPTION_mempath:
2937 mem_path = optarg;
2938 break;
2939#ifdef MAP_POPULATE
2940 case QEMU_OPTION_mem_prealloc:
2941 mem_prealloc = 1;
2942 break;
2943#endif
bellardcd6f1162004-05-13 22:02:20 +00002944 case QEMU_OPTION_d:
Matthew Fernandezc235d732011-06-07 16:32:40 +00002945 log_mask = optarg;
2946 break;
2947 case QEMU_OPTION_D:
2948 log_file = optarg;
bellardcd6f1162004-05-13 22:02:20 +00002949 break;
bellardcd6f1162004-05-13 22:02:20 +00002950 case QEMU_OPTION_s:
Markus Armbrusteref0c4a02012-02-07 15:09:13 +01002951 add_device_config(DEV_GDB, "tcp::" DEFAULT_GDBSTUB_PORT);
bellardcd6f1162004-05-13 22:02:20 +00002952 break;
aliguori59030a82009-04-05 18:43:41 +00002953 case QEMU_OPTION_gdb:
Markus Armbrusteref0c4a02012-02-07 15:09:13 +01002954 add_device_config(DEV_GDB, optarg);
bellardcd6f1162004-05-13 22:02:20 +00002955 break;
bellardcd6f1162004-05-13 22:02:20 +00002956 case QEMU_OPTION_L:
Paul Brook5cea8592009-05-30 00:52:44 +01002957 data_dir = optarg;
bellardcd6f1162004-05-13 22:02:20 +00002958 break;
j_mayer1192dad2007-10-05 13:08:35 +00002959 case QEMU_OPTION_bios:
2960 bios_name = optarg;
2961 break;
aurel321b530a62009-04-05 20:08:59 +00002962 case QEMU_OPTION_singlestep:
2963 singlestep = 1;
2964 break;
bellardcd6f1162004-05-13 22:02:20 +00002965 case QEMU_OPTION_S:
pbrook3c07f8e2007-01-21 16:47:01 +00002966 autostart = 0;
bellardcd6f1162004-05-13 22:02:20 +00002967 break;
bellard3d11d0e2004-12-12 16:56:30 +00002968 case QEMU_OPTION_k:
2969 keyboard_layout = optarg;
2970 break;
bellardee22c2f2004-06-03 12:49:50 +00002971 case QEMU_OPTION_localtime:
2972 rtc_utc = 0;
2973 break;
malc3893c122008-09-28 00:42:05 +00002974 case QEMU_OPTION_vga:
Blue Swirla369da52011-09-27 19:15:42 +00002975 vga_model = optarg;
Paolo Bonzini7f1b17f2012-05-10 09:39:17 +02002976 default_vga = 0;
bellard1bfe8562004-07-08 21:17:50 +00002977 break;
bellarde9b137c2004-06-21 16:46:10 +00002978 case QEMU_OPTION_g:
2979 {
2980 const char *p;
2981 int w, h, depth;
2982 p = optarg;
2983 w = strtol(p, (char **)&p, 10);
2984 if (w <= 0) {
2985 graphic_error:
2986 fprintf(stderr, "qemu: invalid resolution or depth\n");
2987 exit(1);
2988 }
2989 if (*p != 'x')
2990 goto graphic_error;
2991 p++;
2992 h = strtol(p, (char **)&p, 10);
2993 if (h <= 0)
2994 goto graphic_error;
2995 if (*p == 'x') {
2996 p++;
2997 depth = strtol(p, (char **)&p, 10);
ths5fafdf22007-09-16 21:08:06 +00002998 if (depth != 8 && depth != 15 && depth != 16 &&
bellarde9b137c2004-06-21 16:46:10 +00002999 depth != 24 && depth != 32)
3000 goto graphic_error;
3001 } else if (*p == '\0') {
3002 depth = graphic_depth;
3003 } else {
3004 goto graphic_error;
3005 }
ths3b46e622007-09-17 08:09:54 +00003006
bellarde9b137c2004-06-21 16:46:10 +00003007 graphic_width = w;
3008 graphic_height = h;
3009 graphic_depth = depth;
3010 }
3011 break;
ths20d8a3e2007-02-18 17:04:49 +00003012 case QEMU_OPTION_echr:
3013 {
3014 char *r;
3015 term_escape_char = strtol(optarg, &r, 0);
3016 if (r == optarg)
3017 printf("Bad argument to echr\n");
3018 break;
3019 }
bellard82c643f2004-07-14 17:28:13 +00003020 case QEMU_OPTION_monitor:
Gerd Hoffmann6ca55822009-12-08 13:11:52 +01003021 monitor_parse(optarg, "readline");
3022 default_monitor = 0;
3023 break;
3024 case QEMU_OPTION_qmp:
3025 monitor_parse(optarg, "control");
Anthony Liguori2d114dc2010-03-21 14:14:38 -05003026 default_monitor = 0;
bellard82c643f2004-07-14 17:28:13 +00003027 break;
Gerd Hoffmann22a0e042009-12-08 13:11:51 +01003028 case QEMU_OPTION_mon:
Gerd Hoffmann3329f072010-08-20 13:52:01 +02003029 opts = qemu_opts_parse(qemu_find_opts("mon"), optarg, 1);
Gerd Hoffmann22a0e042009-12-08 13:11:51 +01003030 if (!opts) {
Gerd Hoffmann22a0e042009-12-08 13:11:51 +01003031 exit(1);
3032 }
Anthony Liguori2d114dc2010-03-21 14:14:38 -05003033 default_monitor = 0;
Gerd Hoffmann22a0e042009-12-08 13:11:51 +01003034 break;
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003035 case QEMU_OPTION_chardev:
Gerd Hoffmann3329f072010-08-20 13:52:01 +02003036 opts = qemu_opts_parse(qemu_find_opts("chardev"), optarg, 1);
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003037 if (!opts) {
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003038 exit(1);
3039 }
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003040 break;
Gautham R Shenoy74db9202010-04-29 17:44:43 +05303041 case QEMU_OPTION_fsdev:
Gerd Hoffmann03b0ba72010-08-20 13:52:02 +02003042 olist = qemu_find_opts("fsdev");
3043 if (!olist) {
3044 fprintf(stderr, "fsdev is not supported by this qemu build.\n");
3045 exit(1);
3046 }
3047 opts = qemu_opts_parse(olist, optarg, 1);
Gautham R Shenoy74db9202010-04-29 17:44:43 +05303048 if (!opts) {
3049 fprintf(stderr, "parse error: %s\n", optarg);
3050 exit(1);
3051 }
3052 break;
Gautham R Shenoy3d54abc2010-04-29 17:45:03 +05303053 case QEMU_OPTION_virtfs: {
Stefan Hajnoczie14ea472011-03-16 08:31:43 +00003054 QemuOpts *fsdev;
3055 QemuOpts *device;
M. Mohan Kumar84a87cc2011-12-14 13:58:47 +05303056 const char *writeout, *sock_fd, *socket;
Gautham R Shenoy3d54abc2010-04-29 17:45:03 +05303057
Gerd Hoffmann03b0ba72010-08-20 13:52:02 +02003058 olist = qemu_find_opts("virtfs");
3059 if (!olist) {
3060 fprintf(stderr, "virtfs is not supported by this qemu build.\n");
3061 exit(1);
3062 }
3063 opts = qemu_opts_parse(olist, optarg, 1);
Gautham R Shenoy3d54abc2010-04-29 17:45:03 +05303064 if (!opts) {
3065 fprintf(stderr, "parse error: %s\n", optarg);
3066 exit(1);
3067 }
3068
Aneesh Kumar K.Vfbcbf102011-10-13 12:28:04 +05303069 if (qemu_opt_get(opts, "fsdriver") == NULL ||
Aneesh Kumar K.V99519f02011-12-14 13:48:59 +05303070 qemu_opt_get(opts, "mount_tag") == NULL) {
3071 fprintf(stderr, "Usage: -virtfs fsdriver,mount_tag=tag.\n");
Venkateswararao Jujjuri (JV)9ce56db2010-06-14 13:34:40 -07003072 exit(1);
3073 }
Stefan Hajnoczie14ea472011-03-16 08:31:43 +00003074 fsdev = qemu_opts_create(qemu_find_opts("fsdev"),
Luiz Capitulino8be7e7e2012-03-20 15:51:57 -03003075 qemu_opt_get(opts, "mount_tag"),
3076 1, NULL);
Stefan Hajnoczie14ea472011-03-16 08:31:43 +00003077 if (!fsdev) {
3078 fprintf(stderr, "duplicate fsdev id: %s\n",
3079 qemu_opt_get(opts, "mount_tag"));
Gautham R Shenoy3d54abc2010-04-29 17:45:03 +05303080 exit(1);
3081 }
Aneesh Kumar K.Vd3ab98e2011-10-12 19:11:23 +05303082
3083 writeout = qemu_opt_get(opts, "writeout");
3084 if (writeout) {
3085#ifdef CONFIG_SYNC_FILE_RANGE
3086 qemu_opt_set(fsdev, "writeout", writeout);
3087#else
3088 fprintf(stderr, "writeout=immediate not supported on "
3089 "this platform\n");
3090 exit(1);
3091#endif
3092 }
Aneesh Kumar K.Vfbcbf102011-10-13 12:28:04 +05303093 qemu_opt_set(fsdev, "fsdriver", qemu_opt_get(opts, "fsdriver"));
Stefan Hajnoczie14ea472011-03-16 08:31:43 +00003094 qemu_opt_set(fsdev, "path", qemu_opt_get(opts, "path"));
3095 qemu_opt_set(fsdev, "security_model",
3096 qemu_opt_get(opts, "security_model"));
M. Mohan Kumar84a87cc2011-12-14 13:58:47 +05303097 socket = qemu_opt_get(opts, "socket");
3098 if (socket) {
3099 qemu_opt_set(fsdev, "socket", socket);
3100 }
M. Mohan Kumar4c793dd2011-12-14 13:49:28 +05303101 sock_fd = qemu_opt_get(opts, "sock_fd");
3102 if (sock_fd) {
3103 qemu_opt_set(fsdev, "sock_fd", sock_fd);
3104 }
Gautham R Shenoy3d54abc2010-04-29 17:45:03 +05303105
M. Mohan Kumar2c74c2c2011-10-25 12:10:39 +05303106 qemu_opt_set_bool(fsdev, "readonly",
3107 qemu_opt_get_bool(opts, "readonly", 0));
Dong Xu Wange478b442012-12-06 14:47:22 +08003108 device = qemu_opts_create_nofail(qemu_find_opts("device"));
Stefan Hajnoczie14ea472011-03-16 08:31:43 +00003109 qemu_opt_set(device, "driver", "virtio-9p-pci");
3110 qemu_opt_set(device, "fsdev",
3111 qemu_opt_get(opts, "mount_tag"));
3112 qemu_opt_set(device, "mount_tag",
3113 qemu_opt_get(opts, "mount_tag"));
Gautham R Shenoy3d54abc2010-04-29 17:45:03 +05303114 break;
3115 }
Aneesh Kumar K.V9db221a2011-10-25 12:10:40 +05303116 case QEMU_OPTION_virtfs_synth: {
3117 QemuOpts *fsdev;
3118 QemuOpts *device;
3119
Luiz Capitulino8be7e7e2012-03-20 15:51:57 -03003120 fsdev = qemu_opts_create(qemu_find_opts("fsdev"), "v_synth",
3121 1, NULL);
Aneesh Kumar K.V9db221a2011-10-25 12:10:40 +05303122 if (!fsdev) {
3123 fprintf(stderr, "duplicate option: %s\n", "virtfs_synth");
3124 exit(1);
3125 }
3126 qemu_opt_set(fsdev, "fsdriver", "synth");
Aneesh Kumar K.V9db221a2011-10-25 12:10:40 +05303127
Dong Xu Wange478b442012-12-06 14:47:22 +08003128 device = qemu_opts_create_nofail(qemu_find_opts("device"));
Aneesh Kumar K.V9db221a2011-10-25 12:10:40 +05303129 qemu_opt_set(device, "driver", "virtio-9p-pci");
3130 qemu_opt_set(device, "fsdev", "v_synth");
3131 qemu_opt_set(device, "mount_tag", "v_synth");
3132 break;
3133 }
bellard82c643f2004-07-14 17:28:13 +00003134 case QEMU_OPTION_serial:
Gerd Hoffmann998bbd72009-12-08 13:11:41 +01003135 add_device_config(DEV_SERIAL, optarg);
3136 default_serial = 0;
Jan Kiszka18141ed2010-03-07 11:28:40 +01003137 if (strncmp(optarg, "mon:", 4) == 0) {
3138 default_monitor = 0;
3139 }
bellard82c643f2004-07-14 17:28:13 +00003140 break;
Richard W.M. Jones9dd986c2009-04-25 13:56:19 +01003141 case QEMU_OPTION_watchdog:
Markus Armbruster09aaa162009-08-21 10:31:34 +02003142 if (watchdog) {
3143 fprintf(stderr,
3144 "qemu: only one watchdog option may be given\n");
3145 return 1;
3146 }
3147 watchdog = optarg;
Richard W.M. Jones9dd986c2009-04-25 13:56:19 +01003148 break;
3149 case QEMU_OPTION_watchdog_action:
3150 if (select_watchdog_action(optarg) == -1) {
3151 fprintf(stderr, "Unknown -watchdog-action parameter\n");
3152 exit(1);
3153 }
3154 break;
aliguori51ecf132009-01-15 20:06:40 +00003155 case QEMU_OPTION_virtiocon:
Gerd Hoffmannaee1b932009-12-08 13:11:53 +01003156 add_device_config(DEV_VIRTCON, optarg);
3157 default_virtcon = 0;
Jan Kiszka18141ed2010-03-07 11:28:40 +01003158 if (strncmp(optarg, "mon:", 4) == 0) {
3159 default_monitor = 0;
3160 }
aliguori51ecf132009-01-15 20:06:40 +00003161 break;
bellard6508fe52005-01-15 12:02:56 +00003162 case QEMU_OPTION_parallel:
Gerd Hoffmann6a5e8b02009-12-08 13:11:42 +01003163 add_device_config(DEV_PARALLEL, optarg);
3164 default_parallel = 0;
Jan Kiszka18141ed2010-03-07 11:28:40 +01003165 if (strncmp(optarg, "mon:", 4) == 0) {
3166 default_monitor = 0;
3167 }
bellard6508fe52005-01-15 12:02:56 +00003168 break;
H. Peter Anvinc9f398e2009-12-29 13:51:36 -08003169 case QEMU_OPTION_debugcon:
3170 add_device_config(DEV_DEBUGCON, optarg);
3171 break;
bellardd63d3072004-10-03 13:29:03 +00003172 case QEMU_OPTION_loadvm:
3173 loadvm = optarg;
3174 break;
3175 case QEMU_OPTION_full_screen:
3176 full_screen = 1;
3177 break;
ths667acca2006-12-11 02:08:05 +00003178#ifdef CONFIG_SDL
ths43523e92007-02-18 18:19:32 +00003179 case QEMU_OPTION_no_frame:
3180 no_frame = 1;
3181 break;
ths3780e192007-06-21 21:08:02 +00003182 case QEMU_OPTION_alt_grab:
3183 alt_grab = 1;
3184 break;
Dustin Kirkland0ca9f8a2009-09-17 15:48:04 -05003185 case QEMU_OPTION_ctrl_grab:
3186 ctrl_grab = 1;
3187 break;
ths667acca2006-12-11 02:08:05 +00003188 case QEMU_OPTION_no_quit:
3189 no_quit = 1;
3190 break;
aliguori7d957bd2009-01-15 22:14:11 +00003191 case QEMU_OPTION_sdl:
Anthony Liguori993fbfd2009-05-21 16:54:00 -05003192 display_type = DT_SDL;
aliguori7d957bd2009-01-15 22:14:11 +00003193 break;
Jes Sorensen58fc0962011-03-16 13:33:34 +01003194#else
3195 case QEMU_OPTION_no_frame:
3196 case QEMU_OPTION_alt_grab:
3197 case QEMU_OPTION_ctrl_grab:
3198 case QEMU_OPTION_no_quit:
3199 case QEMU_OPTION_sdl:
3200 fprintf(stderr, "SDL support is disabled\n");
3201 exit(1);
ths667acca2006-12-11 02:08:05 +00003202#endif
bellardf7cce892004-12-08 22:21:25 +00003203 case QEMU_OPTION_pidfile:
ths93815bc2007-03-19 15:58:31 +00003204 pid_file = optarg;
bellardf7cce892004-12-08 22:21:25 +00003205 break;
bellarda09db212005-04-30 16:10:35 +00003206 case QEMU_OPTION_win2k_hack:
3207 win2k_install_hack = 1;
3208 break;
Jan Kiszka433acf02012-01-23 20:15:12 +01003209 case QEMU_OPTION_rtc_td_hack: {
3210 static GlobalProperty slew_lost_ticks[] = {
3211 {
3212 .driver = "mc146818rtc",
3213 .property = "lost_tick_policy",
3214 .value = "slew",
3215 },
3216 { /* end of list */ }
3217 };
3218
3219 qdev_prop_register_global_list(slew_lost_ticks);
aliguori73822ec2009-01-15 20:11:34 +00003220 break;
Jan Kiszka433acf02012-01-23 20:15:12 +01003221 }
aliguori8a92ea22009-02-27 20:12:36 +00003222 case QEMU_OPTION_acpitable:
Blue Swirlde06f8d2010-03-29 19:23:50 +00003223 do_acpitable_option(optarg);
aliguori8a92ea22009-02-27 20:12:36 +00003224 break;
aliguorib6f6e3d2009-04-17 18:59:56 +00003225 case QEMU_OPTION_smbios:
Blue Swirlde06f8d2010-03-29 19:23:50 +00003226 do_smbios_option(optarg);
aliguorib6f6e3d2009-04-17 18:59:56 +00003227 break;
aliguori7ba1e612008-11-05 16:04:33 +00003228 case QEMU_OPTION_enable_kvm:
Anthony PERARD303d4e82010-09-21 20:05:31 +01003229 olist = qemu_find_opts("machine");
Anthony PERARD303d4e82010-09-21 20:05:31 +01003230 qemu_opts_parse(olist, "accel=kvm", 0);
3231 break;
3232 case QEMU_OPTION_machine:
3233 olist = qemu_find_opts("machine");
Jan Kiszka9052ea62011-07-23 12:38:37 +02003234 opts = qemu_opts_parse(olist, optarg, 1);
Anthony PERARD303d4e82010-09-21 20:05:31 +01003235 if (!opts) {
3236 fprintf(stderr, "parse error: %s\n", optarg);
3237 exit(1);
3238 }
Jan Kiszka2645c6d2011-07-25 18:11:20 +02003239 optarg = qemu_opt_get(opts, "type");
3240 if (optarg) {
3241 machine = machine_parse(optarg);
3242 }
aliguori7ba1e612008-11-05 16:04:33 +00003243 break;
Jan Kiszkaa0dac022012-10-05 14:51:45 -03003244 case QEMU_OPTION_no_kvm:
3245 olist = qemu_find_opts("machine");
3246 qemu_opts_parse(olist, "accel=tcg", 0);
3247 break;
Jan Kiszka4086bde2012-10-05 14:51:41 -03003248 case QEMU_OPTION_no_kvm_pit: {
3249 fprintf(stderr, "Warning: KVM PIT can no longer be disabled "
3250 "separately.\n");
3251 break;
3252 }
Jan Kiszkac21fb4f2012-10-05 14:51:42 -03003253 case QEMU_OPTION_no_kvm_pit_reinjection: {
3254 static GlobalProperty kvm_pit_lost_tick_policy[] = {
3255 {
3256 .driver = "kvm-pit",
3257 .property = "lost_tick_policy",
3258 .value = "discard",
3259 },
3260 { /* end of list */ }
3261 };
3262
3263 fprintf(stderr, "Warning: option deprecated, use "
3264 "lost_tick_policy property of kvm-pit instead.\n");
3265 qdev_prop_register_global_list(kvm_pit_lost_tick_policy);
3266 break;
3267 }
bellardbb36d472005-11-05 14:22:28 +00003268 case QEMU_OPTION_usb:
Peter Maydellfa5358c2012-11-22 16:48:45 +00003269 olist = qemu_find_opts("machine");
3270 qemu_opts_parse(olist, "usb=on", 0);
bellardbb36d472005-11-05 14:22:28 +00003271 break;
bellarda594cfb2005-11-06 16:13:29 +00003272 case QEMU_OPTION_usbdevice:
Peter Maydellfa5358c2012-11-22 16:48:45 +00003273 olist = qemu_find_opts("machine");
3274 qemu_opts_parse(olist, "usb=on", 0);
Gerd Hoffmannbd3c9482009-07-15 13:59:26 +02003275 add_device_config(DEV_USB, optarg);
3276 break;
3277 case QEMU_OPTION_device:
Gerd Hoffmann3329f072010-08-20 13:52:01 +02003278 if (!qemu_opts_parse(qemu_find_opts("device"), optarg, 1)) {
Gerd Hoffmannf31d07d2009-07-31 12:25:37 +02003279 exit(1);
3280 }
bellarda594cfb2005-11-06 16:13:29 +00003281 break;
bellard6a00d602005-11-21 23:25:50 +00003282 case QEMU_OPTION_smp:
Andre Przywaradc6b1c02009-08-19 15:42:40 +02003283 smp_parse(optarg);
aliguorib2097002008-10-07 20:39:39 +00003284 if (smp_cpus < 1) {
bellard6a00d602005-11-21 23:25:50 +00003285 fprintf(stderr, "Invalid number of CPUs\n");
3286 exit(1);
3287 }
Jes Sorensen6be68d72009-07-23 17:03:42 +02003288 if (max_cpus < smp_cpus) {
3289 fprintf(stderr, "maxcpus must be equal to or greater than "
3290 "smp\n");
3291 exit(1);
3292 }
3293 if (max_cpus > 255) {
3294 fprintf(stderr, "Unsupported number of maxcpus\n");
3295 exit(1);
3296 }
bellard6a00d602005-11-21 23:25:50 +00003297 break;
bellard24236862006-04-30 21:28:36 +00003298 case QEMU_OPTION_vnc:
Jes Sorensen821601e2011-03-16 13:33:36 +01003299#ifdef CONFIG_VNC
Gerd Hoffmann6b62dc22010-08-25 10:51:06 +02003300 display_remote++;
Jes Sorensen821601e2011-03-16 13:33:36 +01003301 vnc_display = optarg;
3302#else
3303 fprintf(stderr, "VNC support is disabled\n");
3304 exit(1);
3305#endif
3306 break;
bellard6515b202006-05-03 22:02:44 +00003307 case QEMU_OPTION_no_acpi:
3308 acpi_enabled = 0;
3309 break;
aliguori16b29ae2008-12-17 23:28:44 +00003310 case QEMU_OPTION_no_hpet:
3311 no_hpet = 1;
3312 break;
Markus Armbruster7d4c3d52009-06-26 19:15:14 +02003313 case QEMU_OPTION_balloon:
3314 if (balloon_parse(optarg) < 0) {
3315 fprintf(stderr, "Unknown -balloon argument %s\n", optarg);
3316 exit(1);
3317 }
Eduardo Habkostdf97b922009-06-10 16:34:08 -03003318 break;
bellardd1beab82006-10-02 19:44:22 +00003319 case QEMU_OPTION_no_reboot:
3320 no_reboot = 1;
3321 break;
aurel32b2f76162008-04-11 21:35:52 +00003322 case QEMU_OPTION_no_shutdown:
3323 no_shutdown = 1;
3324 break;
balrog9467cd42007-05-01 01:34:14 +00003325 case QEMU_OPTION_show_cursor:
3326 cursor_hide = 0;
3327 break;
blueswir18fcb1b92008-09-18 18:29:08 +00003328 case QEMU_OPTION_uuid:
3329 if(qemu_uuid_parse(optarg, qemu_uuid) < 0) {
3330 fprintf(stderr, "Fail to parse UUID string."
3331 " Wrong format.\n");
3332 exit(1);
3333 }
3334 break;
ths9ae02552007-01-05 17:39:04 +00003335 case QEMU_OPTION_option_rom:
3336 if (nb_option_roms >= MAX_OPTION_ROMS) {
3337 fprintf(stderr, "Too many option ROMs\n");
3338 exit(1);
3339 }
Gleb Natapov2e55e842010-12-08 13:35:07 +02003340 opts = qemu_opts_parse(qemu_find_opts("option-rom"), optarg, 1);
3341 option_rom[nb_option_roms].name = qemu_opt_get(opts, "romfile");
3342 option_rom[nb_option_roms].bootindex =
3343 qemu_opt_get_number(opts, "bootindex", -1);
3344 if (!option_rom[nb_option_roms].name) {
3345 fprintf(stderr, "Option ROM file is not specified\n");
3346 exit(1);
3347 }
ths9ae02552007-01-05 17:39:04 +00003348 nb_option_roms++;
3349 break;
pbrook8e716212007-01-20 17:12:09 +00003350 case QEMU_OPTION_semihosting:
3351 semihosting_enabled = 1;
3352 break;
Jan Kiszka88eed342012-10-05 14:51:44 -03003353 case QEMU_OPTION_tdf:
3354 fprintf(stderr, "Warning: user space PIT time drift fix "
3355 "is no longer supported.\n");
3356 break;
thsc35734b2007-03-19 15:17:08 +00003357 case QEMU_OPTION_name:
Anthony Liguori7267c092011-08-20 22:09:37 -05003358 qemu_name = g_strdup(optarg);
Andi Kleen18894652009-07-02 09:34:17 +02003359 {
3360 char *p = strchr(qemu_name, ',');
3361 if (p != NULL) {
3362 *p++ = 0;
3363 if (strncmp(p, "process=", 8)) {
Aurelien Jarno5697f6a2010-12-27 18:29:20 +01003364 fprintf(stderr, "Unknown subargument %s to -name\n", p);
Andi Kleen18894652009-07-02 09:34:17 +02003365 exit(1);
3366 }
3367 p += 8;
Jes Sorensence798cf2010-06-10 11:42:31 +02003368 os_set_proc_name(p);
Andi Kleen18894652009-07-02 09:34:17 +02003369 }
3370 }
thsc35734b2007-03-19 15:17:08 +00003371 break;
blueswir166508602007-05-01 14:16:52 +00003372 case QEMU_OPTION_prom_env:
3373 if (nb_prom_envs >= MAX_PROM_ENVS) {
3374 fprintf(stderr, "Too many prom variables\n");
3375 exit(1);
3376 }
3377 prom_envs[nb_prom_envs] = optarg;
3378 nb_prom_envs++;
3379 break;
balrog2b8f2d42007-07-27 22:08:46 +00003380 case QEMU_OPTION_old_param:
3381 old_param = 1;
ths05ebd532008-01-08 19:32:16 +00003382 break;
thsf3dcfad2007-08-24 01:26:02 +00003383 case QEMU_OPTION_clock:
3384 configure_alarms(optarg);
3385 break;
bellard7e0af5d02007-11-07 16:24:33 +00003386 case QEMU_OPTION_startdate:
Jan Kiszka1ed2fc12009-09-15 13:36:04 +02003387 configure_rtc_date_offset(optarg, 1);
3388 break;
3389 case QEMU_OPTION_rtc:
Gerd Hoffmann3329f072010-08-20 13:52:01 +02003390 opts = qemu_opts_parse(qemu_find_opts("rtc"), optarg, 0);
Jan Kiszka1ed2fc12009-09-15 13:36:04 +02003391 if (!opts) {
Jan Kiszka1ed2fc12009-09-15 13:36:04 +02003392 exit(1);
bellard7e0af5d02007-11-07 16:24:33 +00003393 }
Jan Kiszka1ed2fc12009-09-15 13:36:04 +02003394 configure_rtc(opts);
bellard7e0af5d02007-11-07 16:24:33 +00003395 break;
bellard26a5f132008-05-28 12:30:31 +00003396 case QEMU_OPTION_tb_size:
Jan Kiszkad5ab9712011-08-02 16:10:21 +02003397 tcg_tb_size = strtol(optarg, NULL, 0);
3398 if (tcg_tb_size < 0) {
3399 tcg_tb_size = 0;
3400 }
bellard26a5f132008-05-28 12:30:31 +00003401 break;
pbrook2e70f6e2008-06-29 01:03:05 +00003402 case QEMU_OPTION_icount:
Paolo Bonzini4e3de9e2010-03-10 11:38:48 +01003403 icount_option = optarg;
pbrook2e70f6e2008-06-29 01:03:05 +00003404 break;
aliguori5bb79102008-10-13 03:12:02 +00003405 case QEMU_OPTION_incoming:
3406 incoming = optarg;
Stefano Stabellini81323a62012-01-18 12:23:13 +00003407 runstate_set(RUN_STATE_INMIGRATE);
aliguori5bb79102008-10-13 03:12:02 +00003408 break;
Gerd Hoffmannd8c208d2009-12-08 13:11:46 +01003409 case QEMU_OPTION_nodefaults:
3410 default_serial = 0;
3411 default_parallel = 0;
Gerd Hoffmannaee1b932009-12-08 13:11:53 +01003412 default_virtcon = 0;
Gerd Hoffmannd8c208d2009-12-08 13:11:46 +01003413 default_monitor = 0;
Gerd Hoffmanncb4522c2009-12-08 13:11:47 +01003414 default_net = 0;
Gerd Hoffmannac33f8f2009-12-16 14:25:39 +01003415 default_floppy = 0;
3416 default_cdrom = 0;
3417 default_sdcard = 0;
Paolo Bonzini7f1b17f2012-05-10 09:39:17 +02003418 default_vga = 0;
Gerd Hoffmannd8c208d2009-12-08 13:11:46 +01003419 break;
aliguorie37630c2009-04-22 15:19:10 +00003420 case QEMU_OPTION_xen_domid:
Blue Swirlad960902010-03-29 19:23:52 +00003421 if (!(xen_available())) {
3422 printf("Option %s not supported for this target\n", popt->name);
3423 exit(1);
3424 }
aliguorie37630c2009-04-22 15:19:10 +00003425 xen_domid = atoi(optarg);
3426 break;
3427 case QEMU_OPTION_xen_create:
Blue Swirlad960902010-03-29 19:23:52 +00003428 if (!(xen_available())) {
3429 printf("Option %s not supported for this target\n", popt->name);
3430 exit(1);
3431 }
aliguorie37630c2009-04-22 15:19:10 +00003432 xen_mode = XEN_CREATE;
3433 break;
3434 case QEMU_OPTION_xen_attach:
Blue Swirlad960902010-03-29 19:23:52 +00003435 if (!(xen_available())) {
3436 printf("Option %s not supported for this target\n", popt->name);
3437 exit(1);
3438 }
aliguorie37630c2009-04-22 15:19:10 +00003439 xen_mode = XEN_ATTACH;
3440 break;
Prerna Saxenaab6540d2010-08-09 11:48:32 +01003441 case QEMU_OPTION_trace:
Lluíse4858972011-08-31 20:31:03 +02003442 {
Prerna Saxenaab6540d2010-08-09 11:48:32 +01003443 opts = qemu_opts_parse(qemu_find_opts("trace"), optarg, 0);
Lluíse4858972011-08-31 20:31:03 +02003444 if (!opts) {
3445 exit(1);
Prerna Saxenaab6540d2010-08-09 11:48:32 +01003446 }
Lluís23d15e82011-08-31 20:31:31 +02003447 trace_events = qemu_opt_get(opts, "events");
Lluíse4858972011-08-31 20:31:03 +02003448 trace_file = qemu_opt_get(opts, "file");
Prerna Saxenaab6540d2010-08-09 11:48:32 +01003449 break;
Lluíse4858972011-08-31 20:31:03 +02003450 }
Gerd Hoffmann715a6642009-10-14 10:39:28 +02003451 case QEMU_OPTION_readconfig:
3452 {
Kevin Wolfdcfb0932010-03-05 17:25:55 +01003453 int ret = qemu_read_config_file(optarg);
3454 if (ret < 0) {
3455 fprintf(stderr, "read config %s: %s\n", optarg,
3456 strerror(-ret));
Gerd Hoffmann715a6642009-10-14 10:39:28 +02003457 exit(1);
3458 }
Gerd Hoffmann715a6642009-10-14 10:39:28 +02003459 break;
3460 }
Gerd Hoffmann29b00402010-03-11 11:13:27 -03003461 case QEMU_OPTION_spice:
3462 olist = qemu_find_opts("spice");
3463 if (!olist) {
3464 fprintf(stderr, "spice is not supported by this qemu build.\n");
3465 exit(1);
3466 }
3467 opts = qemu_opts_parse(olist, optarg, 0);
3468 if (!opts) {
3469 fprintf(stderr, "parse error: %s\n", optarg);
3470 exit(1);
3471 }
3472 break;
Gerd Hoffmann715a6642009-10-14 10:39:28 +02003473 case QEMU_OPTION_writeconfig:
3474 {
3475 FILE *fp;
3476 if (strcmp(optarg, "-") == 0) {
3477 fp = stdout;
3478 } else {
3479 fp = fopen(optarg, "w");
3480 if (fp == NULL) {
3481 fprintf(stderr, "open %s: %s\n", optarg, strerror(errno));
3482 exit(1);
3483 }
3484 }
3485 qemu_config_write(fp);
3486 fclose(fp);
3487 break;
3488 }
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +02003489 case QEMU_OPTION_qtest:
3490 qtest_chrdev = optarg;
3491 break;
3492 case QEMU_OPTION_qtest_log:
3493 qtest_log = optarg;
3494 break;
Eduardo Otubo7d76ad42012-08-14 18:44:08 -03003495 case QEMU_OPTION_sandbox:
3496 opts = qemu_opts_parse(qemu_find_opts("sandbox"), optarg, 1);
3497 if (!opts) {
3498 exit(0);
3499 }
3500 break;
Corey Bryant587ed6b2012-10-18 15:19:34 -04003501 case QEMU_OPTION_add_fd:
3502#ifndef _WIN32
3503 opts = qemu_opts_parse(qemu_find_opts("add-fd"), optarg, 0);
3504 if (!opts) {
3505 exit(0);
3506 }
3507#else
3508 error_report("File descriptor passing is disabled on this "
3509 "platform");
3510 exit(1);
3511#endif
3512 break;
Anthony Liguori68d98d32012-06-25 14:36:33 -05003513 case QEMU_OPTION_object:
3514 opts = qemu_opts_parse(qemu_find_opts("object"), optarg, 1);
3515 break;
Jes Sorensen59a52642010-06-10 11:42:25 +02003516 default:
3517 os_parse_cmd_args(popt->index, optarg);
bellardcd6f1162004-05-13 22:02:20 +00003518 }
bellard0824d6f2003-06-24 13:42:40 +00003519 }
3520 }
Markus Armbruster0f0bc3f2010-02-18 20:13:51 +01003521 loc_set_none();
bellard330d0412003-07-26 18:11:40 +00003522
Paolo Bonzini1c537862012-10-30 00:17:12 +01003523 if (qemu_init_main_loop()) {
3524 fprintf(stderr, "qemu_init_main_loop failed\n");
3525 exit(1);
3526 }
3527
Eduardo Otubo7d76ad42012-08-14 18:44:08 -03003528 if (qemu_opts_foreach(qemu_find_opts("sandbox"), parse_sandbox, NULL, 0)) {
3529 exit(1);
3530 }
3531
Corey Bryant587ed6b2012-10-18 15:19:34 -04003532#ifndef _WIN32
3533 if (qemu_opts_foreach(qemu_find_opts("add-fd"), parse_add_fd, NULL, 1)) {
3534 exit(1);
3535 }
3536
3537 if (qemu_opts_foreach(qemu_find_opts("add-fd"), cleanup_add_fd, NULL, 1)) {
3538 exit(1);
3539 }
3540#endif
3541
Dunrong Huangfb7c2692012-07-24 00:42:20 +08003542 if (machine == NULL) {
3543 fprintf(stderr, "No machine found.\n");
3544 exit(1);
3545 }
3546
Crístian Viana93bfef42012-05-30 00:35:51 -03003547 if (machine->hw_version) {
3548 qemu_set_version(machine->hw_version);
3549 }
3550
Anthony Liguori68d98d32012-06-25 14:36:33 -05003551 if (qemu_opts_foreach(qemu_find_opts("object"),
3552 object_create, NULL, 0) != 0) {
3553 exit(1);
3554 }
3555
Eduardo Habkostecf40be2012-03-09 16:19:07 -03003556 /* Init CPU def lists, based on config
3557 * - Must be called after all the qemu_read_config_file() calls
3558 * - Must be called before list_cpus()
3559 * - Must be called before machine->init()
3560 */
3561 cpudef_init();
3562
Peter Maydellc8057f92012-08-02 13:45:54 +01003563 if (cpu_model && is_help_option(cpu_model)) {
Eduardo Habkost1d6528a2012-03-21 09:33:40 -03003564 list_cpus(stdout, &fprintf, cpu_model);
Eduardo Habkostecf40be2012-03-09 16:19:07 -03003565 exit(0);
3566 }
3567
Matthew Fernandezc235d732011-06-07 16:32:40 +00003568 /* Open the logfile at this point, if necessary. We can't open the logfile
3569 * when encountering either of the logging options (-d or -D) because the
3570 * other one may be encountered later on the command line, changing the
3571 * location or level of logging.
3572 */
3573 if (log_mask) {
3574 if (log_file) {
3575 set_cpu_log_filename(log_file);
3576 }
3577 set_cpu_log(log_mask);
3578 }
3579
Lluís23d15e82011-08-31 20:31:31 +02003580 if (!trace_backend_init(trace_events, trace_file)) {
Lluíse4858972011-08-31 20:31:03 +02003581 exit(1);
Stefan Hajnoczi31d3c9b2011-03-13 20:14:30 +00003582 }
Stefan Hajnoczi0b5538c2011-02-26 18:38:39 +00003583
Paul Brook5cea8592009-05-30 00:52:44 +01003584 /* If no data_dir is specified then try to find it relative to the
3585 executable path. */
3586 if (!data_dir) {
Jes Sorensen61705402010-06-10 11:42:23 +02003587 data_dir = os_find_datadir(argv[0]);
Paul Brook5cea8592009-05-30 00:52:44 +01003588 }
Stefan Weil60474fb2011-09-04 15:17:46 +02003589 /* If all else fails use the install path specified when building. */
Paul Brook5cea8592009-05-30 00:52:44 +01003590 if (!data_dir) {
Paolo Bonzini1dabe052010-05-26 16:08:25 +02003591 data_dir = CONFIG_QEMU_DATADIR;
Paul Brook5cea8592009-05-30 00:52:44 +01003592 }
3593
Jes Sorensen6be68d72009-07-23 17:03:42 +02003594 /*
3595 * Default to max_cpus = smp_cpus, in case the user doesn't
3596 * specify a max_cpus value.
3597 */
3598 if (!max_cpus)
3599 max_cpus = smp_cpus;
3600
balrog3d878ca2008-10-28 10:59:59 +00003601 machine->max_cpus = machine->max_cpus ?: 1; /* Default to UP */
aliguorib2097002008-10-07 20:39:39 +00003602 if (smp_cpus > machine->max_cpus) {
3603 fprintf(stderr, "Number of SMP cpus requested (%d), exceeds max cpus "
3604 "supported by machine `%s' (%d)\n", smp_cpus, machine->name,
3605 machine->max_cpus);
3606 exit(1);
3607 }
3608
Anthony PERARD67b724e2010-11-22 15:44:15 +00003609 /*
3610 * Get the default machine options from the machine if it is not already
3611 * specified either by the configuration file or by the command line.
3612 */
3613 if (machine->default_machine_opts) {
Jan Kiszka25de5932012-01-27 19:55:43 +01003614 qemu_opts_set_defaults(qemu_find_opts("machine"),
3615 machine->default_machine_opts, 0);
Anthony PERARD67b724e2010-11-22 15:44:15 +00003616 }
3617
Gerd Hoffmann3329f072010-08-20 13:52:01 +02003618 qemu_opts_foreach(qemu_find_opts("device"), default_driver_check, NULL, 0);
3619 qemu_opts_foreach(qemu_find_opts("global"), default_driver_check, NULL, 0);
Gerd Hoffmann998bbd72009-12-08 13:11:41 +01003620
Gerd Hoffmann986c5f72009-12-08 13:11:54 +01003621 if (machine->no_serial) {
3622 default_serial = 0;
3623 }
3624 if (machine->no_parallel) {
3625 default_parallel = 0;
3626 }
3627 if (!machine->use_virtcon) {
3628 default_virtcon = 0;
3629 }
Gerd Hoffmannac33f8f2009-12-16 14:25:39 +01003630 if (machine->no_floppy) {
3631 default_floppy = 0;
3632 }
3633 if (machine->no_cdrom) {
3634 default_cdrom = 0;
3635 }
3636 if (machine->no_sdcard) {
3637 default_sdcard = 0;
3638 }
Gerd Hoffmann986c5f72009-12-08 13:11:54 +01003639
Anthony Liguori993fbfd2009-05-21 16:54:00 -05003640 if (display_type == DT_NOGRAPHIC) {
Gerd Hoffmann6a5e8b02009-12-08 13:11:42 +01003641 if (default_parallel)
3642 add_device_config(DEV_PARALLEL, "null");
Gerd Hoffmanne1c09172009-12-08 13:11:44 +01003643 if (default_serial && default_monitor) {
3644 add_device_config(DEV_SERIAL, "mon:stdio");
Gerd Hoffmann986c5f72009-12-08 13:11:54 +01003645 } else if (default_virtcon && default_monitor) {
3646 add_device_config(DEV_VIRTCON, "mon:stdio");
Gerd Hoffmanne1c09172009-12-08 13:11:44 +01003647 } else {
3648 if (default_serial)
3649 add_device_config(DEV_SERIAL, "stdio");
Gerd Hoffmann986c5f72009-12-08 13:11:54 +01003650 if (default_virtcon)
3651 add_device_config(DEV_VIRTCON, "stdio");
Gerd Hoffmanne1c09172009-12-08 13:11:44 +01003652 if (default_monitor)
Gerd Hoffmann6ca55822009-12-08 13:11:52 +01003653 monitor_parse("stdio", "readline");
Gerd Hoffmanne1c09172009-12-08 13:11:44 +01003654 }
Gerd Hoffmann998bbd72009-12-08 13:11:41 +01003655 } else {
3656 if (default_serial)
3657 add_device_config(DEV_SERIAL, "vc:80Cx24C");
Gerd Hoffmann6a5e8b02009-12-08 13:11:42 +01003658 if (default_parallel)
3659 add_device_config(DEV_PARALLEL, "vc:80Cx24C");
Gerd Hoffmannabdeed02009-12-08 13:11:43 +01003660 if (default_monitor)
Gerd Hoffmann6ca55822009-12-08 13:11:52 +01003661 monitor_parse("vc:80Cx24C", "readline");
Alexander Graf38536da2009-12-17 13:06:08 +01003662 if (default_virtcon)
3663 add_device_config(DEV_VIRTCON, "vc:80Cx24C");
aliguoribc0129d2008-08-01 15:12:34 +00003664 }
3665
TeLeMana5829fd2010-04-15 12:37:55 +08003666 socket_init();
3667
Gerd Hoffmann3329f072010-08-20 13:52:01 +02003668 if (qemu_opts_foreach(qemu_find_opts("chardev"), chardev_init_func, NULL, 1) != 0)
Gerd Hoffmann1a688d32009-12-08 13:11:36 +01003669 exit(1);
Venkateswararao Jujjuri (JV)758e8e32010-06-14 13:34:41 -07003670#ifdef CONFIG_VIRTFS
Gerd Hoffmann3329f072010-08-20 13:52:01 +02003671 if (qemu_opts_foreach(qemu_find_opts("fsdev"), fsdev_init_func, NULL, 1) != 0) {
Gautham R Shenoy74db9202010-04-29 17:44:43 +05303672 exit(1);
3673 }
3674#endif
Gerd Hoffmann1a688d32009-12-08 13:11:36 +01003675
Jes Sorenseneb505be2010-06-10 11:42:28 +02003676 os_daemonize();
ths71e3ceb2006-12-22 02:11:31 +00003677
thsaa26bb22007-03-25 21:33:06 +00003678 if (pid_file && qemu_create_pidfile(pid_file) != 0) {
Jes Sorenseneb505be2010-06-10 11:42:28 +02003679 os_pidfile_error();
ths93815bc2007-03-19 15:58:31 +00003680 exit(1);
3681 }
3682
Jan Kiszka0ac543d2011-08-15 16:18:57 -07003683 /* init the memory */
3684 if (ram_size == 0) {
3685 ram_size = DEFAULT_RAM_SIZE * 1024 * 1024;
3686 }
3687
Bruce Rogers3d1d9652012-08-09 12:47:40 -06003688 if (qemu_opts_foreach(qemu_find_opts("device"), device_help_func, NULL, 0)
3689 != 0) {
3690 exit(0);
3691 }
3692
Anthony PERARD303d4e82010-09-21 20:05:31 +01003693 configure_accelerator();
Marcelo Tosatti214910a2009-09-18 02:41:23 -03003694
Peter Maydell967c0da2012-02-22 22:40:00 +00003695 machine_opts = qemu_opts_find(qemu_find_opts("machine"), 0);
3696 if (machine_opts) {
3697 kernel_filename = qemu_opt_get(machine_opts, "kernel");
3698 initrd_filename = qemu_opt_get(machine_opts, "initrd");
3699 kernel_cmdline = qemu_opt_get(machine_opts, "append");
3700 } else {
3701 kernel_filename = initrd_filename = kernel_cmdline = NULL;
3702 }
3703
Peter Maydella0abe472012-02-08 05:41:39 +00003704 if (!kernel_cmdline) {
3705 kernel_cmdline = "";
3706 }
3707
bellarda20dd502003-09-30 21:07:02 +00003708 linux_boot = (kernel_filename != NULL);
balrog6c41b272007-11-17 12:12:29 +00003709
thsf8d39c02008-07-03 10:01:15 +00003710 if (!linux_boot && *kernel_cmdline != '\0') {
3711 fprintf(stderr, "-append only allowed with -kernel option\n");
3712 exit(1);
3713 }
3714
3715 if (!linux_boot && initrd_filename != NULL) {
3716 fprintf(stderr, "-initrd only allowed with -kernel option\n");
3717 exit(1);
3718 }
3719
Grant Likely412beee2012-03-02 11:56:38 +00003720 if (!linux_boot && machine_opts && qemu_opt_get(machine_opts, "dtb")) {
3721 fprintf(stderr, "-dtb only allowed with -kernel option\n");
3722 exit(1);
3723 }
3724
Jes Sorensen9156d762010-06-10 11:42:30 +02003725 os_set_line_buffering();
ths3b46e622007-09-17 08:09:54 +00003726
Paolo Bonzini49cf5722012-11-02 15:43:24 +01003727 qemu_init_cpu_loop();
3728 qemu_mutex_lock_iothread();
3729
Alon Levyad1be892012-03-14 20:33:37 +02003730#ifdef CONFIG_SPICE
3731 /* spice needs the timers to be initialized by this point */
3732 qemu_spice_init();
3733#endif
3734
Max Filippov0abe9052011-11-10 15:38:42 +04003735 if (icount_option && (kvm_enabled() || xen_enabled())) {
3736 fprintf(stderr, "-icount is not allowed with kvm or xen\n");
3737 exit(1);
3738 }
Paolo Bonzini4e3de9e2010-03-10 11:38:48 +01003739 configure_icount(icount_option);
pbrook634fce92006-07-15 17:40:09 +00003740
Mark McLoughlindc1c9fe2009-10-06 12:17:16 +01003741 if (net_init_clients() < 0) {
3742 exit(1);
bellard702c6512004-04-02 21:21:32 +00003743 }
bellardf1510b22003-06-25 00:07:40 +00003744
balrogdc72ac12008-11-09 00:04:26 +00003745 /* init the bluetooth world */
Gerd Hoffmannbd3c9482009-07-15 13:59:26 +02003746 if (foreach_device_config(DEV_BT, bt_parse))
3747 exit(1);
balrogdc72ac12008-11-09 00:04:26 +00003748
Anthony PERARD834e76e2011-07-20 08:17:44 +00003749 if (!xen_enabled()) {
3750 /* On 32-bit hosts, QEMU is limited by virtual address space */
3751 if (ram_size > (2047 << 20) && HOST_LONG_BITS == 32) {
3752 fprintf(stderr, "qemu: at most 2047 MB RAM can be simulated\n");
3753 exit(1);
3754 }
3755 }
balrog7fb4fdc2008-04-24 17:59:27 +00003756
Jan Kiszkad5ab9712011-08-02 16:10:21 +02003757 cpu_exec_init_all();
bellard26a5f132008-05-28 12:30:31 +00003758
Markus Armbrustereb852012009-10-27 18:41:44 +01003759 bdrv_init_with_whitelist();
thse4bcb142007-12-02 04:51:10 +00003760
lirans@il.ibm.comc163b5c2009-11-02 15:40:58 +02003761 blk_mig_init();
3762
ths96d30e42007-01-07 20:42:14 +00003763 /* open the virtual block devices */
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +02003764 if (snapshot)
Gerd Hoffmann3329f072010-08-20 13:52:01 +02003765 qemu_opts_foreach(qemu_find_opts("drive"), drive_enable_snapshot, NULL, 0);
Christian Borntraeger2d0d2832012-11-20 15:30:34 +01003766 if (qemu_opts_foreach(qemu_find_opts("drive"), drive_init_func,
3767 &machine->block_default_type, 1) != 0) {
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +02003768 exit(1);
Christian Borntraeger2d0d2832012-11-20 15:30:34 +01003769 }
balrog3e3d5812007-04-30 02:09:25 +00003770
Christian Borntraeger3c42ea62012-11-22 21:02:55 +01003771 default_drive(default_cdrom, snapshot, machine->block_default_type, 2,
3772 CDROM_OPTS);
3773 default_drive(default_floppy, snapshot, IF_FLOPPY, 0, FD_OPTS);
3774 default_drive(default_sdcard, snapshot, IF_SD, 0, SD_OPTS);
Markus Armbruster4e5d9b52011-01-28 11:21:45 +01003775
Juan Quintela7908c782012-06-26 18:46:10 +02003776 register_savevm_live(NULL, "ram", 0, 4, &savevm_ram_handlers, NULL);
bellard8a7ddc32004-03-31 19:00:16 +00003777
aliguori268a3622009-04-21 22:30:27 +00003778 if (nb_numa_nodes > 0) {
3779 int i;
3780
Sasha Levinea0e5412011-06-29 23:29:39 -04003781 if (nb_numa_nodes > MAX_NODES) {
3782 nb_numa_nodes = MAX_NODES;
aliguori268a3622009-04-21 22:30:27 +00003783 }
3784
3785 /* If no memory size if given for any node, assume the default case
3786 * and distribute the available memory equally across all nodes
3787 */
3788 for (i = 0; i < nb_numa_nodes; i++) {
3789 if (node_mem[i] != 0)
3790 break;
3791 }
3792 if (i == nb_numa_nodes) {
3793 uint64_t usedmem = 0;
3794
3795 /* On Linux, the each node's border has to be 8MB aligned,
3796 * the final node gets the rest.
3797 */
3798 for (i = 0; i < nb_numa_nodes - 1; i++) {
3799 node_mem[i] = (ram_size / nb_numa_nodes) & ~((1 << 23UL) - 1);
3800 usedmem += node_mem[i];
3801 }
3802 node_mem[i] = ram_size - usedmem;
3803 }
3804
3805 for (i = 0; i < nb_numa_nodes; i++) {
Chegu Vinodee785fe2012-07-16 21:31:30 -07003806 if (!bitmap_empty(node_cpumask[i], MAX_CPUMASK_BITS)) {
aliguori268a3622009-04-21 22:30:27 +00003807 break;
Chegu Vinodee785fe2012-07-16 21:31:30 -07003808 }
aliguori268a3622009-04-21 22:30:27 +00003809 }
3810 /* assigning the VCPUs round-robin is easier to implement, guest OSes
3811 * must cope with this anyway, because there are BIOSes out there in
3812 * real machines which also use this scheme.
3813 */
3814 if (i == nb_numa_nodes) {
Vasilis Liaskovitis991dfef2011-10-26 14:19:00 +02003815 for (i = 0; i < max_cpus; i++) {
Chegu Vinodee785fe2012-07-16 21:31:30 -07003816 set_bit(i, node_cpumask[i % nb_numa_nodes]);
aliguori268a3622009-04-21 22:30:27 +00003817 }
3818 }
3819 }
3820
Gerd Hoffmann3329f072010-08-20 13:52:01 +02003821 if (qemu_opts_foreach(qemu_find_opts("mon"), mon_init_func, NULL, 1) != 0) {
Jan Kiszka157b9312010-04-06 16:55:53 +02003822 exit(1);
3823 }
3824
Gerd Hoffmann998bbd72009-12-08 13:11:41 +01003825 if (foreach_device_config(DEV_SERIAL, serial_parse) < 0)
3826 exit(1);
Gerd Hoffmann6a5e8b02009-12-08 13:11:42 +01003827 if (foreach_device_config(DEV_PARALLEL, parallel_parse) < 0)
3828 exit(1);
Gerd Hoffmannaee1b932009-12-08 13:11:53 +01003829 if (foreach_device_config(DEV_VIRTCON, virtcon_parse) < 0)
3830 exit(1);
H. Peter Anvinc9f398e2009-12-29 13:51:36 -08003831 if (foreach_device_config(DEV_DEBUGCON, debugcon_parse) < 0)
3832 exit(1);
aliguori2796dae2009-01-16 20:23:27 +00003833
Paolo Bonzini7f1b17f2012-05-10 09:39:17 +02003834 /* If no default VGA is requested, the default is "none". */
Aurelien Jarno3605ded2012-09-10 01:01:44 +02003835 if (default_vga) {
3836 if (cirrus_vga_available()) {
3837 vga_model = "cirrus";
3838 } else if (vga_available()) {
3839 vga_model = "std";
3840 }
Blue Swirla369da52011-09-27 19:15:42 +00003841 }
Paolo Bonzini7f1b17f2012-05-10 09:39:17 +02003842 select_vgahw(vga_model);
Blue Swirla369da52011-09-27 19:15:42 +00003843
Markus Armbruster09aaa162009-08-21 10:31:34 +02003844 if (watchdog) {
3845 i = select_watchdog(watchdog);
3846 if (i > 0)
3847 exit (i == 1 ? 1 : 0);
3848 }
3849
Gerd Hoffmannb6b61142009-07-15 13:48:21 +02003850 if (machine->compat_props) {
Gerd Hoffmann458fb672009-12-08 13:11:33 +01003851 qdev_prop_register_global_list(machine->compat_props);
Gerd Hoffmannb6b61142009-07-15 13:48:21 +02003852 }
Gerd Hoffmannd0fef6f2009-12-08 13:11:34 +01003853 qemu_add_globals();
3854
Anthony Liguori1de81d22011-12-19 16:37:46 -06003855 qdev_machine_init();
3856
Eduardo Habkost5f072e12012-10-15 17:22:02 -03003857 QEMUMachineInitArgs args = { .ram_size = ram_size,
3858 .boot_device = boot_devices,
3859 .kernel_filename = kernel_filename,
3860 .kernel_cmdline = kernel_cmdline,
3861 .initrd_filename = initrd_filename,
3862 .cpu_model = cpu_model };
3863 machine->init(&args);
aliguori3023f332009-01-16 19:04:14 +00003864
Jan Kiszkaea375f92010-03-01 19:10:30 +01003865 cpu_synchronize_all_post_init();
aliguori268a3622009-04-21 22:30:27 +00003866
Blue Swirl87d0a282010-03-27 18:24:45 +00003867 set_numa_modes();
aliguori268a3622009-04-21 22:30:27 +00003868
aliguori6f338c32009-02-11 15:21:54 +00003869 current_machine = machine;
3870
aliguori3023f332009-01-16 19:04:14 +00003871 /* init USB devices */
zhlcindy@gmail.com094b2872012-09-02 19:25:28 +00003872 if (usb_enabled(false)) {
Markus Armbruster07527062009-10-06 12:16:57 +01003873 if (foreach_device_config(DEV_USB, usb_parse) < 0)
3874 exit(1);
aliguori3023f332009-01-16 19:04:14 +00003875 }
3876
Gerd Hoffmannbd3c9482009-07-15 13:59:26 +02003877 /* init generic devices */
Gerd Hoffmann3329f072010-08-20 13:52:01 +02003878 if (qemu_opts_foreach(qemu_find_opts("device"), device_init_func, NULL, 1) != 0)
Gerd Hoffmannbd3c9482009-07-15 13:59:26 +02003879 exit(1);
3880
Markus Armbruster668680f2010-02-11 14:44:58 +01003881 net_check_clients();
3882
aliguori3023f332009-01-16 19:04:14 +00003883 /* just use the first displaystate for the moment */
Paolo Bonzinib473df62010-02-11 00:29:55 +01003884 ds = get_displaystate();
Anthony Liguori993fbfd2009-05-21 16:54:00 -05003885
Gerd Hoffmanna3e22262010-08-25 15:32:06 +02003886 if (using_spice)
3887 display_remote++;
Gerd Hoffmann6b62dc22010-08-25 10:51:06 +02003888 if (display_type == DT_DEFAULT && !display_remote) {
Anthony Liguorif92f8af2009-05-20 13:01:02 -05003889#if defined(CONFIG_SDL) || defined(CONFIG_COCOA)
Anthony Liguori993fbfd2009-05-21 16:54:00 -05003890 display_type = DT_SDL;
Jes Sorensen821601e2011-03-16 13:33:36 +01003891#elif defined(CONFIG_VNC)
Anthony Liguori993fbfd2009-05-21 16:54:00 -05003892 vnc_display = "localhost:0,to=99";
3893 show_vnc_port = 1;
Jes Sorensen821601e2011-03-16 13:33:36 +01003894#else
3895 display_type = DT_NONE;
Anthony Liguori993fbfd2009-05-21 16:54:00 -05003896#endif
3897 }
Jes Sorensen821601e2011-03-16 13:33:36 +01003898
Anthony Liguori993fbfd2009-05-21 16:54:00 -05003899
Gerd Hoffmann6b62dc22010-08-25 10:51:06 +02003900 /* init local displays */
Anthony Liguori993fbfd2009-05-21 16:54:00 -05003901 switch (display_type) {
3902 case DT_NOGRAPHIC:
3903 break;
3904#if defined(CONFIG_CURSES)
3905 case DT_CURSES:
Hitoshi Mitake995ee2b2012-09-15 01:15:41 +09003906 if (!is_daemonized()) {
3907 curses_display_init(ds, full_screen);
3908 }
Anthony Liguori993fbfd2009-05-21 16:54:00 -05003909 break;
3910#endif
bellard5b0753e2005-03-01 21:37:28 +00003911#if defined(CONFIG_SDL)
Anthony Liguori993fbfd2009-05-21 16:54:00 -05003912 case DT_SDL:
3913 sdl_display_init(ds, full_screen, no_frame);
3914 break;
bellard5b0753e2005-03-01 21:37:28 +00003915#elif defined(CONFIG_COCOA)
Anthony Liguori993fbfd2009-05-21 16:54:00 -05003916 case DT_SDL:
3917 cocoa_display_init(ds, full_screen);
3918 break;
bellard313aa562003-08-10 21:52:11 +00003919#endif
Gerd Hoffmann6b62dc22010-08-25 10:51:06 +02003920 default:
3921 break;
3922 }
3923
Gleb Natapov0ce235a2011-03-31 11:27:23 +02003924 /* must be after terminal init, SDL library changes signal handlers */
3925 os_setup_signal_handling();
3926
Jes Sorensen821601e2011-03-16 13:33:36 +01003927#ifdef CONFIG_VNC
Gerd Hoffmann6b62dc22010-08-25 10:51:06 +02003928 /* init remote displays */
3929 if (vnc_display) {
Paolo Bonzini2d55f0e2012-10-02 10:17:21 +02003930 Error *local_err = NULL;
Anthony Liguori993fbfd2009-05-21 16:54:00 -05003931 vnc_display_init(ds);
Paolo Bonzini2d55f0e2012-10-02 10:17:21 +02003932 vnc_display_open(ds, vnc_display, &local_err);
3933 if (local_err != NULL) {
3934 fprintf(stderr, "Failed to start VNC server on `%s': %s\n",
3935 vnc_display, error_get_pretty(local_err));
3936 error_free(local_err);
Anthony Liguori993fbfd2009-05-21 16:54:00 -05003937 exit(1);
Amos Kong94b204c2012-06-30 10:02:20 +08003938 }
Anthony Liguorif92f8af2009-05-20 13:01:02 -05003939
Anthony Liguori993fbfd2009-05-21 16:54:00 -05003940 if (show_vnc_port) {
3941 printf("VNC server running on `%s'\n", vnc_display_local_addr(ds));
Anthony Liguorif92f8af2009-05-20 13:01:02 -05003942 }
bellard313aa562003-08-10 21:52:11 +00003943 }
Jes Sorensen821601e2011-03-16 13:33:36 +01003944#endif
Gerd Hoffmanna3e22262010-08-25 15:32:06 +02003945#ifdef CONFIG_SPICE
Gerd Hoffmanna19cbfb2010-04-27 11:50:11 +02003946 if (using_spice && !qxl_enabled) {
Gerd Hoffmanna3e22262010-08-25 15:32:06 +02003947 qemu_spice_display_init(ds);
3948 }
3949#endif
aliguori5b08fc12008-08-21 20:08:03 +00003950
Gerd Hoffmann6b62dc22010-08-25 10:51:06 +02003951 /* display setup */
Paolo Bonzinib473df62010-02-11 00:29:55 +01003952 text_consoles_set_display(ds);
aliguori2796dae2009-01-16 20:23:27 +00003953
Markus Armbrusteref0c4a02012-02-07 15:09:13 +01003954 if (foreach_device_config(DEV_GDB, gdbserver_start) < 0) {
aliguori59030a82009-04-05 18:43:41 +00003955 exit(1);
balrog45669e02007-07-02 13:20:17 +00003956 }
balrog45669e02007-07-02 13:20:17 +00003957
Gerd Hoffmann3418bd22009-09-25 21:42:41 +02003958 qdev_machine_creation_done();
3959
Gerd Hoffmann15ff7702009-12-14 16:07:35 +01003960 if (rom_load_all() != 0) {
3961 fprintf(stderr, "rom loading failed\n");
3962 exit(1);
3963 }
Gerd Hoffmann45a50b12009-10-01 16:42:33 +02003964
Isaku Yamahata80376c32010-12-20 14:33:35 +09003965 /* TODO: once all bus devices are qdevified, this should be done
3966 * when bus is created by qdev.c */
3967 qemu_register_reset(qbus_reset_all_fn, sysbus_get_default());
Gleb Natapov4cab9462010-12-08 13:35:08 +02003968 qemu_run_machine_init_done_notifiers();
3969
Jan Kiszkae063eb12011-06-14 18:29:43 +02003970 qemu_system_reset(VMRESET_SILENT);
Juan Quintela05f24012009-08-20 19:42:22 +02003971 if (loadvm) {
Markus Armbruster03cd4652010-02-17 16:24:10 +01003972 if (load_vmstate(loadvm) < 0) {
Juan Quintela05f24012009-08-20 19:42:22 +02003973 autostart = 0;
3974 }
3975 }
bellardd63d3072004-10-03 13:29:03 +00003976
Glauber Costa2bb8c102009-07-24 16:20:23 -04003977 if (incoming) {
Paolo Bonzini43eaae22012-10-02 18:21:18 +02003978 Error *local_err = NULL;
3979 qemu_start_incoming_migration(incoming, &local_err);
3980 if (local_err) {
3981 fprintf(stderr, "-incoming %s: %s\n", incoming, error_get_pretty(local_err));
3982 error_free(local_err);
3983 exit(1);
Juan Quintela8ca5e802010-06-09 14:10:54 +02003984 }
Avi Kivity6b99dad2009-08-09 14:39:20 +03003985 } else if (autostart) {
aliguoric0f4ce72009-03-05 23:01:01 +00003986 vm_start();
Avi Kivity6b99dad2009-08-09 14:39:20 +03003987 }
thsffd843b2006-12-21 19:46:43 +00003988
Jes Sorenseneb505be2010-06-10 11:42:28 +02003989 os_setup_post();
ths71e3ceb2006-12-22 02:11:31 +00003990
Paolo Bonzini99435902011-09-12 14:03:13 +02003991 resume_all_vcpus();
bellard8a7ddc32004-03-31 19:00:16 +00003992 main_loop();
Paolo Bonzini99435902011-09-12 14:03:13 +02003993 bdrv_close_all();
3994 pause_all_vcpus();
aliguori63a01ef2008-10-31 19:10:00 +00003995 net_cleanup();
wayne3d3b8302011-07-27 18:04:55 +08003996 res_free();
thsb46a8902007-10-21 23:20:45 +00003997
bellard0824d6f2003-06-24 13:42:40 +00003998 return 0;
3999}