blob: 4ee130259593c9ab67edcd606c35df9b7215e663 [file] [log] [blame]
bellard0824d6f2003-06-24 13:42:40 +00001/*
bellard80cabfa2004-03-14 12:20:30 +00002 * QEMU System Emulator
ths5fafdf22007-09-16 21:08:06 +00003 *
bellard68d0f702008-01-06 17:21:48 +00004 * Copyright (c) 2003-2008 Fabrice Bellard
ths5fafdf22007-09-16 21:08:06 +00005 *
bellard1df912c2003-06-25 16:20:35 +00006 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 * THE SOFTWARE.
bellard0824d6f2003-06-24 13:42:40 +000023 */
bellard0824d6f2003-06-24 13:42:40 +000024#include <unistd.h>
bellard0824d6f2003-06-24 13:42:40 +000025#include <fcntl.h>
26#include <signal.h>
27#include <time.h>
bellard0824d6f2003-06-24 13:42:40 +000028#include <errno.h>
bellard67b915a2004-03-31 23:37:16 +000029#include <sys/time.h>
bellardc88676f2006-08-06 13:36:11 +000030#include <zlib.h>
Paolo Bonzini1de7afc2012-12-17 18:20:00 +010031#include "qemu/bitmap.h"
bellard67b915a2004-03-31 23:37:16 +000032
Juan Quintela71e72a12009-07-27 16:12:56 +020033/* Needed early for CONFIG_BSD etc. */
blueswir1d40cdb12009-03-07 16:52:02 +000034#include "config-host.h"
35
bellard67b915a2004-03-31 23:37:16 +000036#ifndef _WIN32
Paul Brook5cea8592009-05-30 00:52:44 +010037#include <libgen.h>
bellard67b915a2004-03-31 23:37:16 +000038#include <sys/times.h>
bellardf1510b22003-06-25 00:07:40 +000039#include <sys/wait.h>
bellard67b915a2004-03-31 23:37:16 +000040#include <termios.h>
bellard67b915a2004-03-31 23:37:16 +000041#include <sys/mman.h>
bellardf1510b22003-06-25 00:07:40 +000042#include <sys/ioctl.h>
blueswir124646c72008-11-07 16:55:48 +000043#include <sys/resource.h>
bellardf1510b22003-06-25 00:07:40 +000044#include <sys/socket.h>
bellardc94c8d62004-09-13 21:37:34 +000045#include <netinet/in.h>
blueswir124646c72008-11-07 16:55:48 +000046#include <net/if.h>
blueswir124646c72008-11-07 16:55:48 +000047#include <arpa/inet.h>
bellard9d728e82004-09-05 23:09:03 +000048#include <dirent.h>
bellard7c9d8e02005-11-15 22:16:05 +000049#include <netdb.h>
thscb4b9762007-09-13 12:39:35 +000050#include <sys/select.h>
Prerna Saxenaab6540d2010-08-09 11:48:32 +010051
Juan Quintela71e72a12009-07-27 16:12:56 +020052#ifdef CONFIG_BSD
bellard7d3505c2004-05-12 19:32:15 +000053#include <sys/stat.h>
Aurelien Jarnoa167ba52009-11-29 18:00:41 +010054#if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__DragonFly__)
Juergen Lock92c0e652010-03-25 22:07:12 +010055#include <sys/sysctl.h>
blueswir124646c72008-11-07 16:55:48 +000056#else
57#include <util.h>
blueswir1128ab2f2008-08-15 18:33:42 +000058#endif
Aurelien Jarnobbe813a2009-11-30 15:42:59 +010059#else
blueswir1223f0d72008-09-30 18:12:18 +000060#ifdef __linux__
bellard7d3505c2004-05-12 19:32:15 +000061#include <malloc.h>
thsbd494f42007-09-16 20:03:23 +000062
bellarde57a8c02005-11-10 23:58:52 +000063#include <linux/ppdev.h>
ths5867c882007-02-17 23:44:43 +000064#include <linux/parport.h>
blueswir1223f0d72008-09-30 18:12:18 +000065#endif
Eduardo Otubo452dfbe2012-08-14 18:44:07 -030066
67#ifdef CONFIG_SECCOMP
Paolo Bonzini9c17d612012-12-17 18:20:04 +010068#include "sysemu/seccomp.h"
Eduardo Otubo452dfbe2012-08-14 18:44:07 -030069#endif
70
blueswir1223f0d72008-09-30 18:12:18 +000071#ifdef __sun__
thsd5d10bc2007-02-17 22:54:49 +000072#include <sys/stat.h>
73#include <sys/ethernet.h>
74#include <sys/sockio.h>
thsd5d10bc2007-02-17 22:54:49 +000075#include <netinet/arp.h>
thsd5d10bc2007-02-17 22:54:49 +000076#include <netinet/in_systm.h>
77#include <netinet/ip.h>
78#include <netinet/ip_icmp.h> // must come after ip.h
79#include <netinet/udp.h>
80#include <netinet/tcp.h>
81#include <net/if.h>
82#include <syslog.h>
83#include <stropts.h>
bellard67b915a2004-03-31 23:37:16 +000084#endif
bellard7d3505c2004-05-12 19:32:15 +000085#endif
bellardec530c82006-04-25 22:36:06 +000086#endif
bellard67b915a2004-03-31 23:37:16 +000087
ths8a16d272008-07-19 09:56:24 +000088#if defined(CONFIG_VDE)
89#include <libvdeplug.h>
90#endif
91
bellard67b915a2004-03-31 23:37:16 +000092#ifdef _WIN32
aliguori49dc7682009-03-08 16:26:59 +000093#include <windows.h>
bellard67b915a2004-03-31 23:37:16 +000094#endif
95
bellard73332e52004-04-04 20:22:28 +000096#ifdef CONFIG_SDL
Stefan Weil59a36a22009-06-18 20:11:03 +020097#if defined(__APPLE__) || defined(main)
Stefan Weil66936652009-06-13 13:19:11 +020098#include <SDL.h>
malc880fec52009-02-15 20:18:41 +000099int qemu_main(int argc, char **argv, char **envp);
100int main(int argc, char **argv)
101{
Stefan Weil59a36a22009-06-18 20:11:03 +0200102 return qemu_main(argc, argv, NULL);
malc880fec52009-02-15 20:18:41 +0000103}
104#undef main
105#define main qemu_main
bellard96bcd4f2004-07-10 16:26:15 +0000106#endif
bellard73332e52004-04-04 20:22:28 +0000107#endif /* CONFIG_SDL */
bellard0824d6f2003-06-24 13:42:40 +0000108
bellard5b0753e2005-03-01 21:37:28 +0000109#ifdef CONFIG_COCOA
110#undef main
111#define main qemu_main
112#endif /* CONFIG_COCOA */
113
Anthony Liguori69e5bb62011-08-22 08:12:52 -0500114#include <glib.h>
115
blueswir1511d2b12009-03-07 15:32:56 +0000116#include "hw/hw.h"
117#include "hw/boards.h"
118#include "hw/usb.h"
119#include "hw/pcmcia.h"
120#include "hw/pc.h"
blueswir1511d2b12009-03-07 15:32:56 +0000121#include "hw/isa.h"
122#include "hw/baum.h"
123#include "hw/bt.h"
Richard W.M. Jones9dd986c2009-04-25 13:56:19 +0100124#include "hw/watchdog.h"
aliguorib6f6e3d2009-04-17 18:59:56 +0000125#include "hw/smbios.h"
aliguorie37630c2009-04-22 15:19:10 +0000126#include "hw/xen.h"
Gerd Hoffmannbd3c9482009-07-15 13:59:26 +0200127#include "hw/qdev.h"
Gerd Hoffmann45a50b12009-10-01 16:42:33 +0200128#include "hw/loader.h"
Paolo Bonzini927d4872012-12-17 18:20:05 +0100129#include "bt/bt.h"
Paolo Bonzini1422e322012-10-24 08:43:34 +0200130#include "net/net.h"
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000131#include "net/slirp.h"
Paolo Bonzini83c90892012-12-17 18:19:49 +0100132#include "monitor/monitor.h"
Paolo Bonzini28ecbae2012-11-28 12:06:30 +0100133#include "ui/console.h"
Paolo Bonzini9c17d612012-12-17 18:20:04 +0100134#include "sysemu/sysemu.h"
Paolo Bonzini022c62c2012-12-17 18:19:49 +0100135#include "exec/gdbstub.h"
Paolo Bonzini1de7afc2012-12-17 18:20:00 +0100136#include "qemu/timer.h"
Paolo Bonzini927d4872012-12-17 18:20:05 +0100137#include "char/char.h"
Paolo Bonzini1de7afc2012-12-17 18:20:00 +0100138#include "qemu/cache-utils.h"
Paolo Bonzini9c17d612012-12-17 18:20:04 +0100139#include "sysemu/blockdev.h"
Markus Armbruster2b584952012-07-10 11:12:50 +0200140#include "hw/block-common.h"
Paolo Bonzinicaf71f82012-12-17 18:19:50 +0100141#include "migration/block.h"
Paolo Bonzini9c17d612012-12-17 18:20:04 +0100142#include "sysemu/dma.h"
blueswir1511d2b12009-03-07 15:32:56 +0000143#include "audio/audio.h"
Paolo Bonzinicaf71f82012-12-17 18:19:50 +0100144#include "migration/migration.h"
Paolo Bonzini9c17d612012-12-17 18:20:04 +0100145#include "sysemu/kvm.h"
Paolo Bonzini7b1b5d12012-12-17 18:19:43 +0100146#include "qapi/qmp/qjson.h"
Paolo Bonzini1de7afc2012-12-17 18:20:00 +0100147#include "qemu/option.h"
148#include "qemu/config-file.h"
Jes Sorensen59a52642010-06-10 11:42:25 +0200149#include "qemu-options.h"
Luiz Capitulino1fa9a5e2011-09-12 17:54:20 -0300150#include "qmp-commands.h"
Paolo Bonzini1de7afc2012-12-17 18:20:00 +0100151#include "qemu/main-loop.h"
Venkateswararao Jujjuri (JV)758e8e32010-06-14 13:34:41 -0700152#ifdef CONFIG_VIRTFS
Gautham R Shenoy74db9202010-04-29 17:44:43 +0530153#include "fsdev/qemu-fsdev.h"
154#endif
Paolo Bonzini9c17d612012-12-17 18:20:04 +0100155#include "sysemu/qtest.h"
blueswir1511d2b12009-03-07 15:32:56 +0000156
Paolo Bonzini76cad712012-10-24 11:12:21 +0200157#include "disas/disas.h"
bellardfc01f7e2003-06-30 10:03:06 +0000158
Paolo Bonzini1de7afc2012-12-17 18:20:00 +0100159#include "qemu/sockets.h"
blueswir1511d2b12009-03-07 15:32:56 +0000160
Jan Kiszkad918f232009-06-24 14:42:30 +0200161#include "slirp/libslirp.h"
blueswir1511d2b12009-03-07 15:32:56 +0000162
Stefan Hajnoczi94b0b5f2010-11-16 12:20:25 +0000163#include "trace.h"
Lluíse4858972011-08-31 20:31:03 +0200164#include "trace/control.h"
Paolo Bonzini1de7afc2012-12-17 18:20:00 +0100165#include "qemu/queue.h"
Paolo Bonzini9c17d612012-12-17 18:20:04 +0100166#include "sysemu/cpus.h"
167#include "sysemu/arch_init.h"
Paolo Bonzini1de7afc2012-12-17 18:20:00 +0100168#include "qemu/osdep.h"
Blue Swirl72cf2d42009-09-12 07:36:22 +0000169
Gerd Hoffmann29b00402010-03-11 11:13:27 -0300170#include "ui/qemu-spice.h"
Anthony Liguori68d98d32012-06-25 14:36:33 -0500171#include "qapi/string-input-visitor.h"
Gerd Hoffmann29b00402010-03-11 11:13:27 -0300172
blueswir19dc63a12008-10-04 07:25:46 +0000173//#define DEBUG_NET
174//#define DEBUG_SLIRP
bellard330d0412003-07-26 18:11:40 +0000175
bellard1bfe8562004-07-08 21:17:50 +0000176#define DEFAULT_RAM_SIZE 128
bellard313aa562003-08-10 21:52:11 +0000177
Amit Shah98b19252010-01-20 00:36:52 +0530178#define MAX_VIRTIO_CONSOLES 1
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
Paolo Bonzini4d454572012-11-26 16:03:42 +0100302static QemuOptsList qemu_rtc_opts = {
303 .name = "rtc",
304 .head = QTAILQ_HEAD_INITIALIZER(qemu_rtc_opts.head),
305 .desc = {
306 {
307 .name = "base",
308 .type = QEMU_OPT_STRING,
309 },{
310 .name = "clock",
311 .type = QEMU_OPT_STRING,
312 },{
313 .name = "driftfix",
314 .type = QEMU_OPT_STRING,
315 },
316 { /* end of list */ }
317 },
318};
319
320static QemuOptsList qemu_sandbox_opts = {
321 .name = "sandbox",
322 .implied_opt_name = "enable",
323 .head = QTAILQ_HEAD_INITIALIZER(qemu_sandbox_opts.head),
324 .desc = {
325 {
326 .name = "enable",
327 .type = QEMU_OPT_BOOL,
328 },
329 { /* end of list */ }
330 },
331};
332
333static QemuOptsList qemu_trace_opts = {
334 .name = "trace",
335 .implied_opt_name = "trace",
336 .head = QTAILQ_HEAD_INITIALIZER(qemu_trace_opts.head),
337 .desc = {
338 {
339 .name = "events",
340 .type = QEMU_OPT_STRING,
341 },{
342 .name = "file",
343 .type = QEMU_OPT_STRING,
344 },
345 { /* end of list */ }
346 },
347};
348
349static QemuOptsList qemu_option_rom_opts = {
350 .name = "option-rom",
351 .implied_opt_name = "romfile",
352 .head = QTAILQ_HEAD_INITIALIZER(qemu_option_rom_opts.head),
353 .desc = {
354 {
355 .name = "bootindex",
356 .type = QEMU_OPT_NUMBER,
357 }, {
358 .name = "romfile",
359 .type = QEMU_OPT_STRING,
360 },
361 { /* end of list */ }
362 },
363};
364
365static QemuOptsList qemu_machine_opts = {
366 .name = "machine",
367 .implied_opt_name = "type",
368 .merge_lists = true,
369 .head = QTAILQ_HEAD_INITIALIZER(qemu_machine_opts.head),
370 .desc = {
371 {
372 .name = "type",
373 .type = QEMU_OPT_STRING,
374 .help = "emulated machine"
375 }, {
376 .name = "accel",
377 .type = QEMU_OPT_STRING,
378 .help = "accelerator list",
379 }, {
380 .name = "kernel_irqchip",
381 .type = QEMU_OPT_BOOL,
382 .help = "use KVM in-kernel irqchip",
383 }, {
384 .name = "kvm_shadow_mem",
385 .type = QEMU_OPT_SIZE,
386 .help = "KVM shadow MMU size",
387 }, {
388 .name = "kernel",
389 .type = QEMU_OPT_STRING,
390 .help = "Linux kernel image file",
391 }, {
392 .name = "initrd",
393 .type = QEMU_OPT_STRING,
394 .help = "Linux initial ramdisk file",
395 }, {
396 .name = "append",
397 .type = QEMU_OPT_STRING,
398 .help = "Linux kernel command line",
399 }, {
400 .name = "dtb",
401 .type = QEMU_OPT_STRING,
402 .help = "Linux kernel device tree file",
403 }, {
404 .name = "dumpdtb",
405 .type = QEMU_OPT_STRING,
406 .help = "Dump current dtb to a file and quit",
407 }, {
408 .name = "phandle_start",
409 .type = QEMU_OPT_STRING,
410 .help = "The first phandle ID we may generate dynamically",
411 }, {
412 .name = "dt_compatible",
413 .type = QEMU_OPT_STRING,
414 .help = "Overrides the \"compatible\" property of the dt root node",
415 }, {
416 .name = "dump-guest-core",
417 .type = QEMU_OPT_BOOL,
418 .help = "Include guest memory in a core dump",
419 }, {
420 .name = "mem-merge",
421 .type = QEMU_OPT_BOOL,
422 .help = "enable/disable memory merge support",
423 },{
424 .name = "usb",
425 .type = QEMU_OPT_BOOL,
426 .help = "Set on/off to enable/disable usb",
427 },
428 { /* End of list */ }
429 },
430};
431
432static QemuOptsList qemu_boot_opts = {
433 .name = "boot-opts",
434 .head = QTAILQ_HEAD_INITIALIZER(qemu_boot_opts.head),
435 .desc = {
436 /* the three names below are not used now */
437 {
438 .name = "order",
439 .type = QEMU_OPT_STRING,
440 }, {
441 .name = "once",
442 .type = QEMU_OPT_STRING,
443 }, {
444 .name = "menu",
445 .type = QEMU_OPT_STRING,
446 /* following are really used */
447 }, {
448 .name = "splash",
449 .type = QEMU_OPT_STRING,
450 }, {
451 .name = "splash-time",
452 .type = QEMU_OPT_STRING,
453 }, {
454 .name = "reboot-timeout",
455 .type = QEMU_OPT_STRING,
456 },
457 { /*End of list */ }
458 },
459};
460
461static QemuOptsList qemu_add_fd_opts = {
462 .name = "add-fd",
463 .head = QTAILQ_HEAD_INITIALIZER(qemu_add_fd_opts.head),
464 .desc = {
465 {
466 .name = "fd",
467 .type = QEMU_OPT_NUMBER,
468 .help = "file descriptor of which a duplicate is added to fd set",
469 },{
470 .name = "set",
471 .type = QEMU_OPT_NUMBER,
472 .help = "ID of the fd set to add fd to",
473 },{
474 .name = "opaque",
475 .type = QEMU_OPT_STRING,
476 .help = "free-form string used to describe fd",
477 },
478 { /* end of list */ }
479 },
480};
481
482static QemuOptsList qemu_object_opts = {
483 .name = "object",
484 .implied_opt_name = "qom-type",
485 .head = QTAILQ_HEAD_INITIALIZER(qemu_object_opts.head),
486 .desc = {
487 { }
488 },
489};
490
Ronnie Sahlberg31459f42012-08-06 18:24:55 +1000491const char *qemu_get_vm_name(void)
492{
493 return qemu_name;
494}
495
wayne3d3b8302011-07-27 18:04:55 +0800496static void res_free(void)
497{
498 if (boot_splash_filedata != NULL) {
Anthony Liguori7267c092011-08-20 22:09:37 -0500499 g_free(boot_splash_filedata);
wayne3d3b8302011-07-27 18:04:55 +0800500 boot_splash_filedata = NULL;
501 }
502}
503
Gerd Hoffmann998bbd72009-12-08 13:11:41 +0100504static int default_driver_check(QemuOpts *opts, void *opaque)
505{
506 const char *driver = qemu_opt_get(opts, "driver");
507 int i;
508
509 if (!driver)
510 return 0;
511 for (i = 0; i < ARRAY_SIZE(default_list); i++) {
512 if (strcmp(default_list[i].driver, driver) != 0)
513 continue;
514 *(default_list[i].flag) = 0;
515 }
516 return 0;
517}
518
bellard0824d6f2003-06-24 13:42:40 +0000519/***********************************************************/
Luiz Capitulinof5bbfba2011-07-29 15:04:45 -0300520/* QEMU state */
521
Luiz Capitulino0461d5a2011-09-30 14:45:27 -0300522static RunState current_run_state = RUN_STATE_PRELAUNCH;
Luiz Capitulinof5bbfba2011-07-29 15:04:45 -0300523
Luiz Capitulino5db9d4d2011-09-05 16:36:31 -0300524typedef struct {
525 RunState from;
526 RunState to;
527} RunStateTransition;
528
529static const RunStateTransition runstate_transitions_def[] = {
530 /* from -> to */
Luiz Capitulino0461d5a2011-09-30 14:45:27 -0300531 { RUN_STATE_DEBUG, RUN_STATE_RUNNING },
Luiz Capitulino5db9d4d2011-09-05 16:36:31 -0300532
Luiz Capitulino0461d5a2011-09-30 14:45:27 -0300533 { RUN_STATE_INMIGRATE, RUN_STATE_RUNNING },
Paolo Bonzini29ed72f2012-10-19 16:45:24 +0200534 { RUN_STATE_INMIGRATE, RUN_STATE_PAUSED },
Luiz Capitulino5db9d4d2011-09-05 16:36:31 -0300535
Luiz Capitulino0461d5a2011-09-30 14:45:27 -0300536 { RUN_STATE_INTERNAL_ERROR, RUN_STATE_PAUSED },
Luiz Capitulino8a9236f2011-10-14 11:18:09 -0300537 { RUN_STATE_INTERNAL_ERROR, RUN_STATE_FINISH_MIGRATE },
Luiz Capitulino5db9d4d2011-09-05 16:36:31 -0300538
Luiz Capitulino0461d5a2011-09-30 14:45:27 -0300539 { RUN_STATE_IO_ERROR, RUN_STATE_RUNNING },
Luiz Capitulino8a9236f2011-10-14 11:18:09 -0300540 { RUN_STATE_IO_ERROR, RUN_STATE_FINISH_MIGRATE },
Luiz Capitulino5db9d4d2011-09-05 16:36:31 -0300541
Luiz Capitulino0461d5a2011-09-30 14:45:27 -0300542 { RUN_STATE_PAUSED, RUN_STATE_RUNNING },
Luiz Capitulino8a9236f2011-10-14 11:18:09 -0300543 { RUN_STATE_PAUSED, RUN_STATE_FINISH_MIGRATE },
Luiz Capitulino5db9d4d2011-09-05 16:36:31 -0300544
Luiz Capitulino0461d5a2011-09-30 14:45:27 -0300545 { RUN_STATE_POSTMIGRATE, RUN_STATE_RUNNING },
Luiz Capitulino8a9236f2011-10-14 11:18:09 -0300546 { RUN_STATE_POSTMIGRATE, RUN_STATE_FINISH_MIGRATE },
Luiz Capitulino5db9d4d2011-09-05 16:36:31 -0300547
Luiz Capitulino0461d5a2011-09-30 14:45:27 -0300548 { RUN_STATE_PRELAUNCH, RUN_STATE_RUNNING },
Luiz Capitulino8a9236f2011-10-14 11:18:09 -0300549 { RUN_STATE_PRELAUNCH, RUN_STATE_FINISH_MIGRATE },
Luiz Capitulino0461d5a2011-09-30 14:45:27 -0300550 { RUN_STATE_PRELAUNCH, RUN_STATE_INMIGRATE },
Luiz Capitulino5db9d4d2011-09-05 16:36:31 -0300551
Luiz Capitulino0461d5a2011-09-30 14:45:27 -0300552 { RUN_STATE_FINISH_MIGRATE, RUN_STATE_RUNNING },
553 { RUN_STATE_FINISH_MIGRATE, RUN_STATE_POSTMIGRATE },
Luiz Capitulino5db9d4d2011-09-05 16:36:31 -0300554
Luiz Capitulino0461d5a2011-09-30 14:45:27 -0300555 { RUN_STATE_RESTORE_VM, RUN_STATE_RUNNING },
Luiz Capitulino5db9d4d2011-09-05 16:36:31 -0300556
Luiz Capitulino0461d5a2011-09-30 14:45:27 -0300557 { RUN_STATE_RUNNING, RUN_STATE_DEBUG },
558 { RUN_STATE_RUNNING, RUN_STATE_INTERNAL_ERROR },
559 { RUN_STATE_RUNNING, RUN_STATE_IO_ERROR },
560 { RUN_STATE_RUNNING, RUN_STATE_PAUSED },
561 { RUN_STATE_RUNNING, RUN_STATE_FINISH_MIGRATE },
562 { RUN_STATE_RUNNING, RUN_STATE_RESTORE_VM },
563 { RUN_STATE_RUNNING, RUN_STATE_SAVE_VM },
564 { RUN_STATE_RUNNING, RUN_STATE_SHUTDOWN },
565 { RUN_STATE_RUNNING, RUN_STATE_WATCHDOG },
Luiz Capitulino5db9d4d2011-09-05 16:36:31 -0300566
Luiz Capitulino0461d5a2011-09-30 14:45:27 -0300567 { RUN_STATE_SAVE_VM, RUN_STATE_RUNNING },
Luiz Capitulino5db9d4d2011-09-05 16:36:31 -0300568
Luiz Capitulino0461d5a2011-09-30 14:45:27 -0300569 { RUN_STATE_SHUTDOWN, RUN_STATE_PAUSED },
Luiz Capitulino8a9236f2011-10-14 11:18:09 -0300570 { RUN_STATE_SHUTDOWN, RUN_STATE_FINISH_MIGRATE },
Luiz Capitulino5db9d4d2011-09-05 16:36:31 -0300571
Luiz Capitulinoad02b962012-04-27 13:33:36 -0300572 { RUN_STATE_DEBUG, RUN_STATE_SUSPENDED },
573 { RUN_STATE_RUNNING, RUN_STATE_SUSPENDED },
574 { RUN_STATE_SUSPENDED, RUN_STATE_RUNNING },
575 { RUN_STATE_SUSPENDED, RUN_STATE_FINISH_MIGRATE },
576
Luiz Capitulino0461d5a2011-09-30 14:45:27 -0300577 { RUN_STATE_WATCHDOG, RUN_STATE_RUNNING },
Luiz Capitulino8a9236f2011-10-14 11:18:09 -0300578 { RUN_STATE_WATCHDOG, RUN_STATE_FINISH_MIGRATE },
Luiz Capitulino5db9d4d2011-09-05 16:36:31 -0300579
Luiz Capitulino0461d5a2011-09-30 14:45:27 -0300580 { RUN_STATE_MAX, RUN_STATE_MAX },
Luiz Capitulino5db9d4d2011-09-05 16:36:31 -0300581};
582
Luiz Capitulino0461d5a2011-09-30 14:45:27 -0300583static bool runstate_valid_transitions[RUN_STATE_MAX][RUN_STATE_MAX];
Luiz Capitulino5db9d4d2011-09-05 16:36:31 -0300584
Luiz Capitulinof5bbfba2011-07-29 15:04:45 -0300585bool runstate_check(RunState state)
586{
587 return current_run_state == state;
588}
589
Blue Swirl4fdcac02012-10-28 11:04:47 +0000590static void runstate_init(void)
Luiz Capitulino5db9d4d2011-09-05 16:36:31 -0300591{
592 const RunStateTransition *p;
593
594 memset(&runstate_valid_transitions, 0, sizeof(runstate_valid_transitions));
595
Luiz Capitulino0461d5a2011-09-30 14:45:27 -0300596 for (p = &runstate_transitions_def[0]; p->from != RUN_STATE_MAX; p++) {
Luiz Capitulino5db9d4d2011-09-05 16:36:31 -0300597 runstate_valid_transitions[p->from][p->to] = true;
598 }
599}
600
601/* This function will abort() on invalid state transitions */
Luiz Capitulinof5bbfba2011-07-29 15:04:45 -0300602void runstate_set(RunState new_state)
603{
Luiz Capitulino207c5cd2011-10-13 10:59:07 -0300604 assert(new_state < RUN_STATE_MAX);
605
606 if (!runstate_valid_transitions[current_run_state][new_state]) {
607 fprintf(stderr, "ERROR: invalid runstate transition: '%s' -> '%s'\n",
608 RunState_lookup[current_run_state],
609 RunState_lookup[new_state]);
Luiz Capitulino5db9d4d2011-09-05 16:36:31 -0300610 abort();
611 }
612
Luiz Capitulinof5bbfba2011-07-29 15:04:45 -0300613 current_run_state = new_state;
614}
615
Luiz Capitulino13548692011-07-29 15:36:43 -0300616int runstate_is_running(void)
617{
Luiz Capitulino0461d5a2011-09-30 14:45:27 -0300618 return runstate_check(RUN_STATE_RUNNING);
Luiz Capitulino13548692011-07-29 15:36:43 -0300619}
620
Luiz Capitulino1fa9a5e2011-09-12 17:54:20 -0300621StatusInfo *qmp_query_status(Error **errp)
622{
623 StatusInfo *info = g_malloc0(sizeof(*info));
624
625 info->running = runstate_is_running();
626 info->singlestep = singlestep;
627 info->status = current_run_state;
628
629 return info;
630}
631
Luiz Capitulinof5bbfba2011-07-29 15:04:45 -0300632/***********************************************************/
Paolo Bonzini8f0056b2010-01-13 14:05:34 +0100633/* real time host monotonic timer */
bellard09b26c52006-04-12 21:09:08 +0000634
bellardc4b1fcc2004-03-14 21:44:30 +0000635/***********************************************************/
balrogf6503052008-02-17 11:42:19 +0000636/* host time/date access */
637void qemu_get_timedate(struct tm *tm, int offset)
638{
639 time_t ti;
balrogf6503052008-02-17 11:42:19 +0000640
641 time(&ti);
642 ti += offset;
643 if (rtc_date_offset == -1) {
644 if (rtc_utc)
Stefan Weileb7ff6f2013-01-07 23:08:13 +0100645 gmtime_r(&ti, tm);
balrogf6503052008-02-17 11:42:19 +0000646 else
Stefan Weileb7ff6f2013-01-07 23:08:13 +0100647 localtime_r(&ti, tm);
balrogf6503052008-02-17 11:42:19 +0000648 } else {
649 ti -= rtc_date_offset;
Stefan Weileb7ff6f2013-01-07 23:08:13 +0100650 gmtime_r(&ti, tm);
balrogf6503052008-02-17 11:42:19 +0000651 }
balrogf6503052008-02-17 11:42:19 +0000652}
653
654int qemu_timedate_diff(struct tm *tm)
655{
656 time_t seconds;
657
658 if (rtc_date_offset == -1)
659 if (rtc_utc)
660 seconds = mktimegm(tm);
Gleb Natapovf54c5562011-11-06 18:00:22 +0200661 else {
662 struct tm tmp = *tm;
663 tmp.tm_isdst = -1; /* use timezone to figure it out */
664 seconds = mktime(&tmp);
665 }
balrogf6503052008-02-17 11:42:19 +0000666 else
667 seconds = mktimegm(tm) + rtc_date_offset;
668
669 return seconds - time(NULL);
670}
671
Luiz Capitulino80cd3472010-02-25 12:11:44 -0300672void rtc_change_mon_event(struct tm *tm)
673{
674 QObject *data;
675
676 data = qobject_from_jsonf("{ 'offset': %d }", qemu_timedate_diff(tm));
677 monitor_protocol_event(QEVENT_RTC_CHANGE, data);
678 qobject_decref(data);
679}
680
Jan Kiszka1ed2fc12009-09-15 13:36:04 +0200681static void configure_rtc_date_offset(const char *startdate, int legacy)
682{
683 time_t rtc_start_date;
684 struct tm tm;
685
686 if (!strcmp(startdate, "now") && legacy) {
687 rtc_date_offset = -1;
688 } else {
689 if (sscanf(startdate, "%d-%d-%dT%d:%d:%d",
690 &tm.tm_year,
691 &tm.tm_mon,
692 &tm.tm_mday,
693 &tm.tm_hour,
694 &tm.tm_min,
695 &tm.tm_sec) == 6) {
696 /* OK */
697 } else if (sscanf(startdate, "%d-%d-%d",
698 &tm.tm_year,
699 &tm.tm_mon,
700 &tm.tm_mday) == 3) {
701 tm.tm_hour = 0;
702 tm.tm_min = 0;
703 tm.tm_sec = 0;
704 } else {
705 goto date_fail;
706 }
707 tm.tm_year -= 1900;
708 tm.tm_mon--;
709 rtc_start_date = mktimegm(&tm);
710 if (rtc_start_date == -1) {
711 date_fail:
712 fprintf(stderr, "Invalid date format. Valid formats are:\n"
713 "'2006-06-17T16:01:21' or '2006-06-17'\n");
714 exit(1);
715 }
716 rtc_date_offset = time(NULL) - rtc_start_date;
717 }
718}
719
720static void configure_rtc(QemuOpts *opts)
721{
722 const char *value;
723
724 value = qemu_opt_get(opts, "base");
725 if (value) {
726 if (!strcmp(value, "utc")) {
727 rtc_utc = 1;
728 } else if (!strcmp(value, "localtime")) {
729 rtc_utc = 0;
730 } else {
731 configure_rtc_date_offset(value, 0);
732 }
733 }
Jan Kiszka68752042009-09-15 13:36:04 +0200734 value = qemu_opt_get(opts, "clock");
735 if (value) {
736 if (!strcmp(value, "host")) {
737 rtc_clock = host_clock;
Paolo Bonzini78808142012-03-30 10:31:21 +0000738 } else if (!strcmp(value, "rt")) {
739 rtc_clock = rt_clock;
Jan Kiszka68752042009-09-15 13:36:04 +0200740 } else if (!strcmp(value, "vm")) {
741 rtc_clock = vm_clock;
742 } else {
743 fprintf(stderr, "qemu: invalid option value '%s'\n", value);
744 exit(1);
745 }
746 }
Jan Kiszka1ed2fc12009-09-15 13:36:04 +0200747 value = qemu_opt_get(opts, "driftfix");
748 if (value) {
Blue Swirl7e4c0332010-03-27 21:33:46 +0000749 if (!strcmp(value, "slew")) {
Jan Kiszka433acf02012-01-23 20:15:12 +0100750 static GlobalProperty slew_lost_ticks[] = {
751 {
752 .driver = "mc146818rtc",
753 .property = "lost_tick_policy",
754 .value = "slew",
755 },
756 { /* end of list */ }
757 };
758
759 qdev_prop_register_global_list(slew_lost_ticks);
Blue Swirl7e4c0332010-03-27 21:33:46 +0000760 } else if (!strcmp(value, "none")) {
Jan Kiszka433acf02012-01-23 20:15:12 +0100761 /* discard is default */
Jan Kiszka1ed2fc12009-09-15 13:36:04 +0200762 } else {
763 fprintf(stderr, "qemu: invalid option value '%s'\n", value);
764 exit(1);
765 }
766 }
Jan Kiszka1ed2fc12009-09-15 13:36:04 +0200767}
768
balrog1ae26a12008-09-28 23:19:47 +0000769/***********************************************************/
770/* Bluetooth support */
771static int nb_hcis;
772static int cur_hci;
773static struct HCIInfo *hci_table[MAX_NICS];
balrogdc72ac12008-11-09 00:04:26 +0000774
balrog1ae26a12008-09-28 23:19:47 +0000775static struct bt_vlan_s {
776 struct bt_scatternet_s net;
777 int id;
778 struct bt_vlan_s *next;
779} *first_bt_vlan;
780
781/* find or alloc a new bluetooth "VLAN" */
blueswir1674bb262008-09-30 18:18:27 +0000782static struct bt_scatternet_s *qemu_find_bt_vlan(int id)
balrog1ae26a12008-09-28 23:19:47 +0000783{
784 struct bt_vlan_s **pvlan, *vlan;
785 for (vlan = first_bt_vlan; vlan != NULL; vlan = vlan->next) {
786 if (vlan->id == id)
787 return &vlan->net;
788 }
Anthony Liguori7267c092011-08-20 22:09:37 -0500789 vlan = g_malloc0(sizeof(struct bt_vlan_s));
balrog1ae26a12008-09-28 23:19:47 +0000790 vlan->id = id;
791 pvlan = &first_bt_vlan;
792 while (*pvlan != NULL)
793 pvlan = &(*pvlan)->next;
794 *pvlan = vlan;
795 return &vlan->net;
796}
797
798static void null_hci_send(struct HCIInfo *hci, const uint8_t *data, int len)
799{
800}
801
802static int null_hci_addr_set(struct HCIInfo *hci, const uint8_t *bd_addr)
803{
804 return -ENOTSUP;
805}
806
807static struct HCIInfo null_hci = {
808 .cmd_send = null_hci_send,
809 .sco_send = null_hci_send,
810 .acl_send = null_hci_send,
811 .bdaddr_set = null_hci_addr_set,
812};
813
814struct HCIInfo *qemu_next_hci(void)
815{
816 if (cur_hci == nb_hcis)
817 return &null_hci;
818
819 return hci_table[cur_hci++];
820}
821
balrogdc72ac12008-11-09 00:04:26 +0000822static struct HCIInfo *hci_init(const char *str)
823{
824 char *endp;
825 struct bt_scatternet_s *vlan = 0;
826
827 if (!strcmp(str, "null"))
828 /* null */
829 return &null_hci;
830 else if (!strncmp(str, "host", 4) && (str[4] == '\0' || str[4] == ':'))
831 /* host[:hciN] */
832 return bt_host_hci(str[4] ? str + 5 : "hci0");
833 else if (!strncmp(str, "hci", 3)) {
834 /* hci[,vlan=n] */
835 if (str[3]) {
836 if (!strncmp(str + 3, ",vlan=", 6)) {
837 vlan = qemu_find_bt_vlan(strtol(str + 9, &endp, 0));
838 if (*endp)
839 vlan = 0;
840 }
841 } else
842 vlan = qemu_find_bt_vlan(0);
843 if (vlan)
844 return bt_new_hci(vlan);
845 }
846
847 fprintf(stderr, "qemu: Unknown bluetooth HCI `%s'.\n", str);
848
849 return 0;
850}
851
852static int bt_hci_parse(const char *str)
853{
854 struct HCIInfo *hci;
Anthony Liguoric227f092009-10-01 16:12:16 -0500855 bdaddr_t bdaddr;
balrogdc72ac12008-11-09 00:04:26 +0000856
857 if (nb_hcis >= MAX_NICS) {
858 fprintf(stderr, "qemu: Too many bluetooth HCIs (max %i).\n", MAX_NICS);
859 return -1;
860 }
861
862 hci = hci_init(str);
863 if (!hci)
864 return -1;
865
866 bdaddr.b[0] = 0x52;
867 bdaddr.b[1] = 0x54;
868 bdaddr.b[2] = 0x00;
869 bdaddr.b[3] = 0x12;
870 bdaddr.b[4] = 0x34;
871 bdaddr.b[5] = 0x56 + nb_hcis;
872 hci->bdaddr_set(hci, bdaddr.b);
873
874 hci_table[nb_hcis++] = hci;
875
876 return 0;
877}
878
879static void bt_vhci_add(int vlan_id)
880{
881 struct bt_scatternet_s *vlan = qemu_find_bt_vlan(vlan_id);
882
883 if (!vlan->slave)
884 fprintf(stderr, "qemu: warning: adding a VHCI to "
885 "an empty scatternet %i\n", vlan_id);
886
887 bt_vhci_init(bt_new_hci(vlan));
888}
889
890static struct bt_device_s *bt_device_add(const char *opt)
891{
892 struct bt_scatternet_s *vlan;
893 int vlan_id = 0;
894 char *endp = strstr(opt, ",vlan=");
895 int len = (endp ? endp - opt : strlen(opt)) + 1;
896 char devname[10];
897
898 pstrcpy(devname, MIN(sizeof(devname), len), opt);
899
900 if (endp) {
901 vlan_id = strtol(endp + 6, &endp, 0);
902 if (*endp) {
903 fprintf(stderr, "qemu: unrecognised bluetooth vlan Id\n");
904 return 0;
905 }
906 }
907
908 vlan = qemu_find_bt_vlan(vlan_id);
909
910 if (!vlan->slave)
911 fprintf(stderr, "qemu: warning: adding a slave device to "
912 "an empty scatternet %i\n", vlan_id);
913
914 if (!strcmp(devname, "keyboard"))
915 return bt_keyboard_init(vlan);
916
917 fprintf(stderr, "qemu: unsupported bluetooth device `%s'\n", devname);
918 return 0;
919}
920
921static int bt_parse(const char *opt)
922{
923 const char *endp, *p;
924 int vlan;
925
926 if (strstart(opt, "hci", &endp)) {
927 if (!*endp || *endp == ',') {
928 if (*endp)
929 if (!strstart(endp, ",vlan=", 0))
930 opt = endp + 1;
931
932 return bt_hci_parse(opt);
933 }
934 } else if (strstart(opt, "vhci", &endp)) {
935 if (!*endp || *endp == ',') {
936 if (*endp) {
937 if (strstart(endp, ",vlan=", &p)) {
938 vlan = strtol(p, (char **) &endp, 0);
939 if (*endp) {
940 fprintf(stderr, "qemu: bad scatternet '%s'\n", p);
941 return 1;
942 }
943 } else {
944 fprintf(stderr, "qemu: bad parameter '%s'\n", endp + 1);
945 return 1;
946 }
947 } else
948 vlan = 0;
949
950 bt_vhci_add(vlan);
951 return 0;
952 }
953 } else if (strstart(opt, "device:", &endp))
954 return !bt_device_add(endp);
955
956 fprintf(stderr, "qemu: bad bluetooth parameter '%s'\n", opt);
957 return 1;
958}
959
Eduardo Otubo7d76ad42012-08-14 18:44:08 -0300960static int parse_sandbox(QemuOpts *opts, void *opaque)
961{
962 /* FIXME: change this to true for 1.3 */
963 if (qemu_opt_get_bool(opts, "enable", false)) {
964#ifdef CONFIG_SECCOMP
965 if (seccomp_start() < 0) {
966 qerror_report(ERROR_CLASS_GENERIC_ERROR,
967 "failed to install seccomp syscall filter in the kernel");
968 return -1;
969 }
970#else
971 qerror_report(ERROR_CLASS_GENERIC_ERROR,
972 "sandboxing request but seccomp is not compiled into this build");
973 return -1;
974#endif
975 }
976
977 return 0;
978}
979
zhlcindy@gmail.com094b2872012-09-02 19:25:28 +0000980/*********QEMU USB setting******/
981bool usb_enabled(bool default_usb)
982{
983 QemuOpts *mach_opts;
984 mach_opts = qemu_opts_find(qemu_find_opts("machine"), 0);
985 if (mach_opts) {
986 return qemu_opt_get_bool(mach_opts, "usb", default_usb);
987 }
988 return default_usb;
989}
990
Corey Bryant587ed6b2012-10-18 15:19:34 -0400991#ifndef _WIN32
992static int parse_add_fd(QemuOpts *opts, void *opaque)
993{
994 int fd, dupfd, flags;
995 int64_t fdset_id;
996 const char *fd_opaque = NULL;
997
998 fd = qemu_opt_get_number(opts, "fd", -1);
999 fdset_id = qemu_opt_get_number(opts, "set", -1);
1000 fd_opaque = qemu_opt_get(opts, "opaque");
1001
1002 if (fd < 0) {
1003 qerror_report(ERROR_CLASS_GENERIC_ERROR,
1004 "fd option is required and must be non-negative");
1005 return -1;
1006 }
1007
1008 if (fd <= STDERR_FILENO) {
1009 qerror_report(ERROR_CLASS_GENERIC_ERROR,
1010 "fd cannot be a standard I/O stream");
1011 return -1;
1012 }
1013
1014 /*
1015 * All fds inherited across exec() necessarily have FD_CLOEXEC
1016 * clear, while qemu sets FD_CLOEXEC on all other fds used internally.
1017 */
1018 flags = fcntl(fd, F_GETFD);
1019 if (flags == -1 || (flags & FD_CLOEXEC)) {
1020 qerror_report(ERROR_CLASS_GENERIC_ERROR,
1021 "fd is not valid or already in use");
1022 return -1;
1023 }
1024
1025 if (fdset_id < 0) {
1026 qerror_report(ERROR_CLASS_GENERIC_ERROR,
1027 "set option is required and must be non-negative");
1028 return -1;
1029 }
1030
1031#ifdef F_DUPFD_CLOEXEC
1032 dupfd = fcntl(fd, F_DUPFD_CLOEXEC, 0);
1033#else
1034 dupfd = dup(fd);
1035 if (dupfd != -1) {
1036 qemu_set_cloexec(dupfd);
1037 }
1038#endif
1039 if (dupfd == -1) {
1040 qerror_report(ERROR_CLASS_GENERIC_ERROR,
1041 "Error duplicating fd: %s", strerror(errno));
1042 return -1;
1043 }
1044
1045 /* add the duplicate fd, and optionally the opaque string, to the fd set */
1046 monitor_fdset_add_fd(dupfd, true, fdset_id, fd_opaque ? true : false,
1047 fd_opaque, NULL);
1048
1049 return 0;
1050}
1051
1052static int cleanup_add_fd(QemuOpts *opts, void *opaque)
1053{
1054 int fd;
1055
1056 fd = qemu_opt_get_number(opts, "fd", -1);
1057 close(fd);
1058
1059 return 0;
1060}
1061#endif
1062
balrog1ae26a12008-09-28 23:19:47 +00001063/***********************************************************/
1064/* QEMU Block devices */
1065
Markus Armbruster2292dda2011-01-28 11:21:41 +01001066#define HD_OPTS "media=disk"
1067#define CDROM_OPTS "media=cdrom"
1068#define FD_OPTS ""
1069#define PFLASH_OPTS ""
1070#define MTD_OPTS ""
1071#define SD_OPTS ""
thse4bcb142007-12-02 04:51:10 +00001072
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +02001073static int drive_init_func(QemuOpts *opts, void *opaque)
1074{
Christian Borntraeger2d0d2832012-11-20 15:30:34 +01001075 BlockInterfaceType *block_default_type = opaque;
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +02001076
Christian Borntraeger2d0d2832012-11-20 15:30:34 +01001077 return drive_init(opts, *block_default_type) == NULL;
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +02001078}
1079
1080static int drive_enable_snapshot(QemuOpts *opts, void *opaque)
1081{
1082 if (NULL == qemu_opt_get(opts, "snapshot")) {
1083 qemu_opt_set(opts, "snapshot", "on");
1084 }
1085 return 0;
1086}
1087
Christian Borntraeger3c42ea62012-11-22 21:02:55 +01001088static void default_drive(int enable, int snapshot, BlockInterfaceType type,
1089 int index, const char *optstr)
Markus Armbruster4e5d9b52011-01-28 11:21:45 +01001090{
1091 QemuOpts *opts;
1092
Markus Armbruster4e5d9b52011-01-28 11:21:45 +01001093 if (!enable || drive_get_by_index(type, index)) {
1094 return;
1095 }
1096
1097 opts = drive_add(type, index, NULL, optstr);
1098 if (snapshot) {
1099 drive_enable_snapshot(opts, NULL);
1100 }
Christian Borntraeger2d0d2832012-11-20 15:30:34 +01001101 if (!drive_init(opts, type)) {
Markus Armbruster4e5d9b52011-01-28 11:21:45 +01001102 exit(1);
1103 }
1104}
1105
Jan Kiszka76e30d02009-07-02 00:19:02 +02001106void qemu_register_boot_set(QEMUBootSetHandler *func, void *opaque)
1107{
1108 boot_set_handler = func;
1109 boot_set_opaque = opaque;
1110}
1111
1112int qemu_boot_set(const char *boot_devices)
1113{
1114 if (!boot_set_handler) {
1115 return -EINVAL;
1116 }
1117 return boot_set_handler(boot_set_opaque, boot_devices);
1118}
1119
Eduardo Habkost4e9e9d62010-04-06 19:22:08 -03001120static void validate_bootdevices(char *devices)
Jan Kiszkaef3adf62009-07-02 00:19:02 +02001121{
1122 /* We just do some generic consistency checks */
1123 const char *p;
1124 int bitmap = 0;
1125
1126 for (p = devices; *p != '\0'; p++) {
1127 /* Allowed boot devices are:
1128 * a-b: floppy disk drives
1129 * c-f: IDE disk drives
Dong Xu Wang07f35072011-11-22 18:06:26 +08001130 * g-m: machine implementation dependent drives
Jan Kiszkaef3adf62009-07-02 00:19:02 +02001131 * n-p: network devices
1132 * It's up to each machine implementation to check if the given boot
1133 * devices match the actual hardware implementation and firmware
1134 * features.
1135 */
1136 if (*p < 'a' || *p > 'p') {
1137 fprintf(stderr, "Invalid boot device '%c'\n", *p);
1138 exit(1);
1139 }
1140 if (bitmap & (1 << (*p - 'a'))) {
1141 fprintf(stderr, "Boot device '%c' was given twice\n", *p);
1142 exit(1);
1143 }
1144 bitmap |= 1 << (*p - 'a');
1145 }
Jan Kiszkaef3adf62009-07-02 00:19:02 +02001146}
1147
Jan Kiszkae0f084b2009-07-02 00:19:02 +02001148static void restore_boot_devices(void *opaque)
1149{
1150 char *standard_boot_devices = opaque;
Alex Williamson37905d62010-04-30 15:21:11 -06001151 static int first = 1;
1152
1153 /* Restore boot order and remove ourselves after the first boot */
1154 if (first) {
1155 first = 0;
1156 return;
1157 }
Jan Kiszkae0f084b2009-07-02 00:19:02 +02001158
1159 qemu_boot_set(standard_boot_devices);
1160
1161 qemu_unregister_reset(restore_boot_devices, standard_boot_devices);
Anthony Liguori7267c092011-08-20 22:09:37 -05001162 g_free(standard_boot_devices);
Jan Kiszkae0f084b2009-07-02 00:19:02 +02001163}
1164
Gleb Natapov1ca4d092010-12-08 13:35:05 +02001165void add_boot_device_path(int32_t bootindex, DeviceState *dev,
1166 const char *suffix)
1167{
1168 FWBootEntry *node, *i;
1169
1170 if (bootindex < 0) {
1171 return;
1172 }
1173
1174 assert(dev != NULL || suffix != NULL);
1175
Anthony Liguori7267c092011-08-20 22:09:37 -05001176 node = g_malloc0(sizeof(FWBootEntry));
Gleb Natapov1ca4d092010-12-08 13:35:05 +02001177 node->bootindex = bootindex;
Anthony Liguori7267c092011-08-20 22:09:37 -05001178 node->suffix = suffix ? g_strdup(suffix) : NULL;
Gleb Natapov1ca4d092010-12-08 13:35:05 +02001179 node->dev = dev;
1180
1181 QTAILQ_FOREACH(i, &fw_boot_order, link) {
1182 if (i->bootindex == bootindex) {
1183 fprintf(stderr, "Two devices with same boot index %d\n", bootindex);
1184 exit(1);
1185 } else if (i->bootindex < bootindex) {
1186 continue;
1187 }
1188 QTAILQ_INSERT_BEFORE(i, node, link);
1189 return;
1190 }
1191 QTAILQ_INSERT_TAIL(&fw_boot_order, node, link);
1192}
1193
Gleb Natapov962630f2010-12-08 13:35:09 +02001194/*
1195 * This function returns null terminated string that consist of new line
Brad Hards71785ab2011-04-23 21:50:06 +10001196 * separated device paths.
Gleb Natapov962630f2010-12-08 13:35:09 +02001197 *
1198 * memory pointed by "size" is assigned total length of the array in bytes
1199 *
1200 */
Markus Armbruster0e7a7592013-01-16 14:50:29 +01001201char *get_boot_devices_list(size_t *size)
Gleb Natapov962630f2010-12-08 13:35:09 +02001202{
1203 FWBootEntry *i;
Markus Armbruster0e7a7592013-01-16 14:50:29 +01001204 size_t total = 0;
Gleb Natapov962630f2010-12-08 13:35:09 +02001205 char *list = NULL;
1206
1207 QTAILQ_FOREACH(i, &fw_boot_order, link) {
1208 char *devpath = NULL, *bootpath;
Markus Armbruster0e7a7592013-01-16 14:50:29 +01001209 size_t len;
Gleb Natapov962630f2010-12-08 13:35:09 +02001210
1211 if (i->dev) {
1212 devpath = qdev_get_fw_dev_path(i->dev);
1213 assert(devpath);
1214 }
1215
1216 if (i->suffix && devpath) {
Blue Swirl4fd37a92010-12-19 14:05:43 +00001217 size_t bootpathlen = strlen(devpath) + strlen(i->suffix) + 1;
1218
Anthony Liguori7267c092011-08-20 22:09:37 -05001219 bootpath = g_malloc(bootpathlen);
Blue Swirl4fd37a92010-12-19 14:05:43 +00001220 snprintf(bootpath, bootpathlen, "%s%s", devpath, i->suffix);
Anthony Liguori7267c092011-08-20 22:09:37 -05001221 g_free(devpath);
Gleb Natapov962630f2010-12-08 13:35:09 +02001222 } else if (devpath) {
1223 bootpath = devpath;
1224 } else {
Markus Armbruster1bf6ccd2011-11-08 10:58:00 +01001225 assert(i->suffix);
Anthony Liguori7267c092011-08-20 22:09:37 -05001226 bootpath = g_strdup(i->suffix);
Gleb Natapov962630f2010-12-08 13:35:09 +02001227 }
1228
1229 if (total) {
1230 list[total-1] = '\n';
1231 }
1232 len = strlen(bootpath) + 1;
Anthony Liguori7267c092011-08-20 22:09:37 -05001233 list = g_realloc(list, total + len);
Gleb Natapov962630f2010-12-08 13:35:09 +02001234 memcpy(&list[total], bootpath, len);
1235 total += len;
Anthony Liguori7267c092011-08-20 22:09:37 -05001236 g_free(bootpath);
Gleb Natapov962630f2010-12-08 13:35:09 +02001237 }
1238
1239 *size = total;
1240
1241 return list;
1242}
1243
aliguori268a3622009-04-21 22:30:27 +00001244static void numa_add(const char *optarg)
1245{
1246 char option[128];
1247 char *endptr;
1248 unsigned long long value, endvalue;
1249 int nodenr;
1250
Chegu Vinodee785fe2012-07-16 21:31:30 -07001251 value = endvalue = 0ULL;
1252
aliguori268a3622009-04-21 22:30:27 +00001253 optarg = get_opt_name(option, 128, optarg, ',') + 1;
1254 if (!strcmp(option, "node")) {
1255 if (get_param_value(option, 128, "nodeid", optarg) == 0) {
1256 nodenr = nb_numa_nodes;
1257 } else {
1258 nodenr = strtoull(option, NULL, 10);
1259 }
1260
1261 if (get_param_value(option, 128, "mem", optarg) == 0) {
1262 node_mem[nodenr] = 0;
1263 } else {
Jes Sorensen70b4f4b2011-01-05 11:41:02 +01001264 int64_t sval;
Markus Armbrusterc03417b2011-11-22 09:46:02 +01001265 sval = strtosz(option, &endptr);
1266 if (sval < 0 || *endptr) {
Jes Sorensen9f9b17a2010-10-21 17:15:46 +02001267 fprintf(stderr, "qemu: invalid numa mem size: %s\n", optarg);
1268 exit(1);
aliguori268a3622009-04-21 22:30:27 +00001269 }
Jes Sorensen9f9b17a2010-10-21 17:15:46 +02001270 node_mem[nodenr] = sval;
aliguori268a3622009-04-21 22:30:27 +00001271 }
Chegu Vinodee785fe2012-07-16 21:31:30 -07001272 if (get_param_value(option, 128, "cpus", optarg) != 0) {
aliguori268a3622009-04-21 22:30:27 +00001273 value = strtoull(option, &endptr, 10);
Chegu Vinodee785fe2012-07-16 21:31:30 -07001274 if (*endptr == '-') {
1275 endvalue = strtoull(endptr+1, &endptr, 10);
aliguori268a3622009-04-21 22:30:27 +00001276 } else {
Chegu Vinodee785fe2012-07-16 21:31:30 -07001277 endvalue = value;
aliguori268a3622009-04-21 22:30:27 +00001278 }
Chegu Vinodee785fe2012-07-16 21:31:30 -07001279
1280 if (!(endvalue < MAX_CPUMASK_BITS)) {
1281 endvalue = MAX_CPUMASK_BITS - 1;
1282 fprintf(stderr,
1283 "A max of %d CPUs are supported in a guest\n",
1284 MAX_CPUMASK_BITS);
1285 }
1286
1287 bitmap_set(node_cpumask[nodenr], value, endvalue-value+1);
aliguori268a3622009-04-21 22:30:27 +00001288 }
1289 nb_numa_nodes++;
1290 }
aliguori268a3622009-04-21 22:30:27 +00001291}
1292
Andre Przywaradc6b1c02009-08-19 15:42:40 +02001293static void smp_parse(const char *optarg)
1294{
1295 int smp, sockets = 0, threads = 0, cores = 0;
1296 char *endptr;
1297 char option[128];
1298
1299 smp = strtoul(optarg, &endptr, 10);
1300 if (endptr != optarg) {
1301 if (*endptr == ',') {
1302 endptr++;
1303 }
1304 }
1305 if (get_param_value(option, 128, "sockets", endptr) != 0)
1306 sockets = strtoull(option, NULL, 10);
1307 if (get_param_value(option, 128, "cores", endptr) != 0)
1308 cores = strtoull(option, NULL, 10);
1309 if (get_param_value(option, 128, "threads", endptr) != 0)
1310 threads = strtoull(option, NULL, 10);
1311 if (get_param_value(option, 128, "maxcpus", endptr) != 0)
1312 max_cpus = strtoull(option, NULL, 10);
1313
1314 /* compute missing values, prefer sockets over cores over threads */
1315 if (smp == 0 || sockets == 0) {
1316 sockets = sockets > 0 ? sockets : 1;
1317 cores = cores > 0 ? cores : 1;
1318 threads = threads > 0 ? threads : 1;
1319 if (smp == 0) {
1320 smp = cores * threads * sockets;
Andre Przywaradc6b1c02009-08-19 15:42:40 +02001321 }
1322 } else {
1323 if (cores == 0) {
1324 threads = threads > 0 ? threads : 1;
1325 cores = smp / (sockets * threads);
1326 } else {
Joel Schoppdca98162010-07-21 15:05:17 -05001327 threads = smp / (cores * sockets);
Andre Przywaradc6b1c02009-08-19 15:42:40 +02001328 }
1329 }
1330 smp_cpus = smp;
1331 smp_cores = cores > 0 ? cores : 1;
1332 smp_threads = threads > 0 ? threads : 1;
1333 if (max_cpus == 0)
1334 max_cpus = smp_cpus;
1335}
1336
bellard330d0412003-07-26 18:11:40 +00001337/***********************************************************/
bellarda594cfb2005-11-06 16:13:29 +00001338/* USB devices */
1339
Markus Armbrusterfb080002010-05-28 15:38:44 +02001340static int usb_device_add(const char *devname)
bellarda594cfb2005-11-06 16:13:29 +00001341{
1342 const char *p;
Gerd Hoffmanna5d2f722009-08-31 14:24:00 +02001343 USBDevice *dev = NULL;
bellarda594cfb2005-11-06 16:13:29 +00001344
zhlcindy@gmail.com094b2872012-09-02 19:25:28 +00001345 if (!usb_enabled(false)) {
bellarda594cfb2005-11-06 16:13:29 +00001346 return -1;
zhlcindy@gmail.com094b2872012-09-02 19:25:28 +00001347 }
bellarda594cfb2005-11-06 16:13:29 +00001348
Gerd Hoffmann0958b4c2009-10-26 15:56:45 +01001349 /* drivers with .usbdevice_name entry in USBDeviceInfo */
1350 dev = usbdevice_create(devname);
1351 if (dev)
1352 goto done;
1353
Gerd Hoffmanna5d2f722009-08-31 14:24:00 +02001354 /* the other ones */
Gerd Hoffmanne447fc62011-06-01 14:41:59 +02001355#ifndef CONFIG_LINUX
1356 /* only the linux version is qdev-ified, usb-bsd still needs this */
bellarda594cfb2005-11-06 16:13:29 +00001357 if (strstart(devname, "host:", &p)) {
Jan Kiszka37417152012-02-27 15:18:47 +01001358 dev = usb_host_device_open(usb_bus_find(-1), p);
Gerd Hoffmanne447fc62011-06-01 14:41:59 +02001359 } else
1360#endif
1361 if (!strcmp(devname, "bt") || strstart(devname, "bt:", &p)) {
Jan Kiszka37417152012-02-27 15:18:47 +01001362 dev = usb_bt_init(usb_bus_find(-1),
1363 devname[2] ? hci_init(p)
1364 : bt_new_hci(qemu_find_bt_vlan(0)));
bellarda594cfb2005-11-06 16:13:29 +00001365 } else {
1366 return -1;
1367 }
pbrook0d92ed32006-05-21 16:30:15 +00001368 if (!dev)
1369 return -1;
1370
Gerd Hoffmanna5d2f722009-08-31 14:24:00 +02001371done:
bellarda594cfb2005-11-06 16:13:29 +00001372 return 0;
1373}
1374
aliguori1f3870a2008-08-21 19:27:48 +00001375static int usb_device_del(const char *devname)
1376{
1377 int bus_num, addr;
1378 const char *p;
1379
aliguori5d0c5752008-09-14 01:07:41 +00001380 if (strstart(devname, "host:", &p))
1381 return usb_host_device_close(p);
1382
zhlcindy@gmail.com094b2872012-09-02 19:25:28 +00001383 if (!usb_enabled(false)) {
aliguori1f3870a2008-08-21 19:27:48 +00001384 return -1;
zhlcindy@gmail.com094b2872012-09-02 19:25:28 +00001385 }
aliguori1f3870a2008-08-21 19:27:48 +00001386
1387 p = strchr(devname, '.');
1388 if (!p)
1389 return -1;
1390 bus_num = strtoul(devname, NULL, 0);
1391 addr = strtoul(p + 1, NULL, 0);
1392
Gerd Hoffmanna5d2f722009-08-31 14:24:00 +02001393 return usb_device_delete_addr(bus_num, addr);
aliguori1f3870a2008-08-21 19:27:48 +00001394}
1395
Gerd Hoffmannbd3c9482009-07-15 13:59:26 +02001396static int usb_parse(const char *cmdline)
1397{
Scott Tsai59d1c1c2009-12-23 04:30:18 +08001398 int r;
Markus Armbrusterfb080002010-05-28 15:38:44 +02001399 r = usb_device_add(cmdline);
Scott Tsai59d1c1c2009-12-23 04:30:18 +08001400 if (r < 0) {
1401 fprintf(stderr, "qemu: could not add USB device '%s'\n", cmdline);
1402 }
1403 return r;
Gerd Hoffmannbd3c9482009-07-15 13:59:26 +02001404}
1405
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001406void do_usb_add(Monitor *mon, const QDict *qdict)
bellarda594cfb2005-11-06 16:13:29 +00001407{
Scott Tsai59d1c1c2009-12-23 04:30:18 +08001408 const char *devname = qdict_get_str(qdict, "devname");
Markus Armbrusterfb080002010-05-28 15:38:44 +02001409 if (usb_device_add(devname) < 0) {
Markus Armbruster1ecda022010-02-18 17:25:24 +01001410 error_report("could not add USB device '%s'", devname);
Scott Tsai59d1c1c2009-12-23 04:30:18 +08001411 }
bellarda594cfb2005-11-06 16:13:29 +00001412}
1413
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001414void do_usb_del(Monitor *mon, const QDict *qdict)
bellarda594cfb2005-11-06 16:13:29 +00001415{
Scott Tsai59d1c1c2009-12-23 04:30:18 +08001416 const char *devname = qdict_get_str(qdict, "devname");
1417 if (usb_device_del(devname) < 0) {
Markus Armbruster1ecda022010-02-18 17:25:24 +01001418 error_report("could not delete USB device '%s'", devname);
Scott Tsai59d1c1c2009-12-23 04:30:18 +08001419 }
bellarda594cfb2005-11-06 16:13:29 +00001420}
1421
bellardf7cce892004-12-08 22:21:25 +00001422/***********************************************************/
balrog201a51f2007-04-30 00:51:09 +00001423/* PCMCIA/Cardbus */
1424
1425static struct pcmcia_socket_entry_s {
Paul Brookbc24a222009-05-10 01:44:56 +01001426 PCMCIASocket *socket;
balrog201a51f2007-04-30 00:51:09 +00001427 struct pcmcia_socket_entry_s *next;
1428} *pcmcia_sockets = 0;
1429
Paul Brookbc24a222009-05-10 01:44:56 +01001430void pcmcia_socket_register(PCMCIASocket *socket)
balrog201a51f2007-04-30 00:51:09 +00001431{
1432 struct pcmcia_socket_entry_s *entry;
1433
Anthony Liguori7267c092011-08-20 22:09:37 -05001434 entry = g_malloc(sizeof(struct pcmcia_socket_entry_s));
balrog201a51f2007-04-30 00:51:09 +00001435 entry->socket = socket;
1436 entry->next = pcmcia_sockets;
1437 pcmcia_sockets = entry;
1438}
1439
Paul Brookbc24a222009-05-10 01:44:56 +01001440void pcmcia_socket_unregister(PCMCIASocket *socket)
balrog201a51f2007-04-30 00:51:09 +00001441{
1442 struct pcmcia_socket_entry_s *entry, **ptr;
1443
1444 ptr = &pcmcia_sockets;
1445 for (entry = *ptr; entry; ptr = &entry->next, entry = *ptr)
1446 if (entry->socket == socket) {
1447 *ptr = entry->next;
Anthony Liguori7267c092011-08-20 22:09:37 -05001448 g_free(entry);
balrog201a51f2007-04-30 00:51:09 +00001449 }
1450}
1451
Wenchao Xia84f2d0e2013-01-14 14:06:25 +08001452void pcmcia_info(Monitor *mon, const QDict *qdict)
balrog201a51f2007-04-30 00:51:09 +00001453{
1454 struct pcmcia_socket_entry_s *iter;
aliguori376253e2009-03-05 23:01:23 +00001455
balrog201a51f2007-04-30 00:51:09 +00001456 if (!pcmcia_sockets)
aliguori376253e2009-03-05 23:01:23 +00001457 monitor_printf(mon, "No PCMCIA sockets\n");
balrog201a51f2007-04-30 00:51:09 +00001458
1459 for (iter = pcmcia_sockets; iter; iter = iter->next)
aliguori376253e2009-03-05 23:01:23 +00001460 monitor_printf(mon, "%s: %s\n", iter->socket->slot_string,
1461 iter->socket->attached ? iter->socket->card_string :
1462 "Empty");
balrog201a51f2007-04-30 00:51:09 +00001463}
1464
1465/***********************************************************/
bellardcc1daa42005-06-05 14:49:17 +00001466/* machine registration */
1467
blueswir1bdaf78e2008-10-04 07:24:27 +00001468static QEMUMachine *first_machine = NULL;
aliguori6f338c32009-02-11 15:21:54 +00001469QEMUMachine *current_machine = NULL;
bellardcc1daa42005-06-05 14:49:17 +00001470
1471int qemu_register_machine(QEMUMachine *m)
1472{
1473 QEMUMachine **pm;
1474 pm = &first_machine;
1475 while (*pm != NULL)
1476 pm = &(*pm)->next;
1477 m->next = NULL;
1478 *pm = m;
1479 return 0;
1480}
1481
pbrook9596ebb2007-11-18 01:44:38 +00001482static QEMUMachine *find_machine(const char *name)
bellardcc1daa42005-06-05 14:49:17 +00001483{
1484 QEMUMachine *m;
1485
1486 for(m = first_machine; m != NULL; m = m->next) {
1487 if (!strcmp(m->name, name))
1488 return m;
Mark McLoughlin3f6599e2009-07-22 10:02:50 +01001489 if (m->alias && !strcmp(m->alias, name))
1490 return m;
bellardcc1daa42005-06-05 14:49:17 +00001491 }
1492 return NULL;
1493}
1494
Jordan Justen2c8cffa2012-02-21 23:18:50 -08001495QEMUMachine *find_default_machine(void)
Anthony Liguori0c257432009-05-21 20:41:01 -05001496{
1497 QEMUMachine *m;
1498
1499 for(m = first_machine; m != NULL; m = m->next) {
1500 if (m->is_default) {
1501 return m;
1502 }
1503 }
1504 return NULL;
1505}
1506
Anthony Liguori01d3c802012-08-10 11:04:11 -05001507MachineInfoList *qmp_query_machines(Error **errp)
1508{
1509 MachineInfoList *mach_list = NULL;
1510 QEMUMachine *m;
1511
1512 for (m = first_machine; m; m = m->next) {
1513 MachineInfoList *entry;
1514 MachineInfo *info;
1515
1516 info = g_malloc0(sizeof(*info));
1517 if (m->is_default) {
1518 info->has_is_default = true;
1519 info->is_default = true;
1520 }
1521
1522 if (m->alias) {
1523 info->has_alias = true;
1524 info->alias = g_strdup(m->alias);
1525 }
1526
1527 info->name = g_strdup(m->name);
1528
1529 entry = g_malloc0(sizeof(*entry));
1530 entry->value = info;
1531 entry->next = mach_list;
1532 mach_list = entry;
1533 }
1534
1535 return mach_list;
1536}
1537
bellardcc1daa42005-06-05 14:49:17 +00001538/***********************************************************/
bellard8a7ddc32004-03-31 19:00:16 +00001539/* main execution loop */
1540
pbrook9596ebb2007-11-18 01:44:38 +00001541static void gui_update(void *opaque)
bellard8a7ddc32004-03-31 19:00:16 +00001542{
aliguori7d957bd2009-01-15 22:14:11 +00001543 uint64_t interval = GUI_REFRESH_INTERVAL;
ths740733b2007-06-08 01:57:56 +00001544 DisplayState *ds = opaque;
Gerd Hoffmann87e487a2010-06-04 11:46:35 +02001545 DisplayChangeListener *dcl;
aliguori7d957bd2009-01-15 22:14:11 +00001546
1547 dpy_refresh(ds);
1548
Gerd Hoffmann87e487a2010-06-04 11:46:35 +02001549 QLIST_FOREACH(dcl, &ds->listeners, next) {
aliguori7d957bd2009-01-15 22:14:11 +00001550 if (dcl->gui_timer_interval &&
1551 dcl->gui_timer_interval < interval)
1552 interval = dcl->gui_timer_interval;
aliguori7d957bd2009-01-15 22:14:11 +00001553 }
Paolo Bonzini7bd427d2011-03-11 16:47:48 +01001554 qemu_mod_timer(ds->gui_timer, interval + qemu_get_clock_ms(rt_clock));
bellard8a7ddc32004-03-31 19:00:16 +00001555}
1556
Gerd Hoffmann35c9e0a2010-06-04 11:51:31 +02001557void gui_setup_refresh(DisplayState *ds)
1558{
1559 DisplayChangeListener *dcl;
1560 bool need_timer = false;
Gerd Hoffmanna93a4a22012-09-28 15:02:08 +02001561 bool have_gfx = false;
1562 bool have_text = false;
Gerd Hoffmann35c9e0a2010-06-04 11:51:31 +02001563
1564 QLIST_FOREACH(dcl, &ds->listeners, next) {
1565 if (dcl->dpy_refresh != NULL) {
1566 need_timer = true;
Gerd Hoffmanna93a4a22012-09-28 15:02:08 +02001567 }
1568 if (dcl->dpy_gfx_update != NULL) {
1569 have_gfx = true;
1570 }
1571 if (dcl->dpy_text_update != NULL) {
1572 have_text = true;
Gerd Hoffmann35c9e0a2010-06-04 11:51:31 +02001573 }
1574 }
1575
1576 if (need_timer && ds->gui_timer == NULL) {
1577 ds->gui_timer = qemu_new_timer_ms(rt_clock, gui_update, ds);
1578 qemu_mod_timer(ds->gui_timer, qemu_get_clock_ms(rt_clock));
1579 }
1580 if (!need_timer && ds->gui_timer != NULL) {
1581 qemu_del_timer(ds->gui_timer);
1582 qemu_free_timer(ds->gui_timer);
1583 ds->gui_timer = NULL;
1584 }
Gerd Hoffmanna93a4a22012-09-28 15:02:08 +02001585
1586 ds->have_gfx = have_gfx;
1587 ds->have_text = have_text;
Gerd Hoffmann35c9e0a2010-06-04 11:51:31 +02001588}
1589
bellard0bd48852005-11-11 00:00:47 +00001590struct vm_change_state_entry {
1591 VMChangeStateHandler *cb;
1592 void *opaque;
Blue Swirl72cf2d42009-09-12 07:36:22 +00001593 QLIST_ENTRY (vm_change_state_entry) entries;
bellard0bd48852005-11-11 00:00:47 +00001594};
1595
Blue Swirl72cf2d42009-09-12 07:36:22 +00001596static QLIST_HEAD(vm_change_state_head, vm_change_state_entry) vm_change_state_head;
bellard0bd48852005-11-11 00:00:47 +00001597
1598VMChangeStateEntry *qemu_add_vm_change_state_handler(VMChangeStateHandler *cb,
1599 void *opaque)
1600{
1601 VMChangeStateEntry *e;
1602
Anthony Liguori7267c092011-08-20 22:09:37 -05001603 e = g_malloc0(sizeof (*e));
bellard0bd48852005-11-11 00:00:47 +00001604
1605 e->cb = cb;
1606 e->opaque = opaque;
Blue Swirl72cf2d42009-09-12 07:36:22 +00001607 QLIST_INSERT_HEAD(&vm_change_state_head, e, entries);
bellard0bd48852005-11-11 00:00:47 +00001608 return e;
1609}
1610
1611void qemu_del_vm_change_state_handler(VMChangeStateEntry *e)
1612{
Blue Swirl72cf2d42009-09-12 07:36:22 +00001613 QLIST_REMOVE (e, entries);
Anthony Liguori7267c092011-08-20 22:09:37 -05001614 g_free (e);
bellard0bd48852005-11-11 00:00:47 +00001615}
1616
Luiz Capitulino1dfb4dd2011-07-29 14:26:33 -03001617void vm_state_notify(int running, RunState state)
bellard0bd48852005-11-11 00:00:47 +00001618{
1619 VMChangeStateEntry *e;
1620
Luiz Capitulino1dfb4dd2011-07-29 14:26:33 -03001621 trace_vm_state_notify(running, state);
Stefan Hajnoczi94b0b5f2010-11-16 12:20:25 +00001622
bellard0bd48852005-11-11 00:00:47 +00001623 for (e = vm_change_state_head.lh_first; e; e = e->entries.le_next) {
Luiz Capitulino1dfb4dd2011-07-29 14:26:33 -03001624 e->cb(e->opaque, running, state);
bellard0bd48852005-11-11 00:00:47 +00001625 }
1626}
1627
bellard8a7ddc32004-03-31 19:00:16 +00001628void vm_start(void)
1629{
Luiz Capitulino13548692011-07-29 15:36:43 -03001630 if (!runstate_is_running()) {
bellard8a7ddc32004-03-31 19:00:16 +00001631 cpu_enable_ticks();
Luiz Capitulino0461d5a2011-09-30 14:45:27 -03001632 runstate_set(RUN_STATE_RUNNING);
1633 vm_state_notify(1, RUN_STATE_RUNNING);
aliguorid6dc3d42009-04-24 18:04:07 +00001634 resume_all_vcpus();
Luiz Capitulino6ed2c482010-04-27 20:35:59 -03001635 monitor_protocol_event(QEVENT_RESUME, NULL);
bellard8a7ddc32004-03-31 19:00:16 +00001636 }
1637}
1638
bellardbb0c6722004-06-20 12:37:32 +00001639/* reset/shutdown handler */
1640
1641typedef struct QEMUResetEntry {
Blue Swirl72cf2d42009-09-12 07:36:22 +00001642 QTAILQ_ENTRY(QEMUResetEntry) entry;
bellardbb0c6722004-06-20 12:37:32 +00001643 QEMUResetHandler *func;
1644 void *opaque;
bellardbb0c6722004-06-20 12:37:32 +00001645} QEMUResetEntry;
1646
Blue Swirl72cf2d42009-09-12 07:36:22 +00001647static QTAILQ_HEAD(reset_handlers, QEMUResetEntry) reset_handlers =
1648 QTAILQ_HEAD_INITIALIZER(reset_handlers);
bellardbb0c6722004-06-20 12:37:32 +00001649static int reset_requested;
Gleb Natapovf64622c2011-03-15 13:56:04 +02001650static int shutdown_requested, shutdown_signal = -1;
1651static pid_t shutdown_pid;
bellard34751872005-07-02 14:31:34 +00001652static int powerdown_requested;
Jan Kiszka8cf71712011-02-07 12:19:16 +01001653static int debug_requested;
Gerd Hoffmann95b363b2012-02-23 13:45:19 +01001654static int suspend_requested;
Luiz Capitulino14058192012-08-08 17:29:17 -03001655static int wakeup_requested;
Igor Mammedova9552c82012-09-05 23:06:21 +02001656static NotifierList powerdown_notifiers =
1657 NOTIFIER_LIST_INITIALIZER(powerdown_notifiers);
Gerd Hoffmann95b363b2012-02-23 13:45:19 +01001658static NotifierList suspend_notifiers =
1659 NOTIFIER_LIST_INITIALIZER(suspend_notifiers);
1660static NotifierList wakeup_notifiers =
1661 NOTIFIER_LIST_INITIALIZER(wakeup_notifiers);
1662static uint32_t wakeup_reason_mask = ~0;
Luiz Capitulino0461d5a2011-09-30 14:45:27 -03001663static RunState vmstop_requested = RUN_STATE_MAX;
bellardbb0c6722004-06-20 12:37:32 +00001664
Anthony PERARD1291eb32010-07-22 15:52:48 +01001665int qemu_shutdown_requested_get(void)
1666{
1667 return shutdown_requested;
1668}
1669
1670int qemu_reset_requested_get(void)
1671{
1672 return reset_requested;
1673}
1674
Blue Swirl4fdcac02012-10-28 11:04:47 +00001675static int qemu_shutdown_requested(void)
aurel32cf7a2fe2008-03-18 06:53:05 +00001676{
1677 int r = shutdown_requested;
1678 shutdown_requested = 0;
1679 return r;
1680}
1681
Blue Swirl4fdcac02012-10-28 11:04:47 +00001682static void qemu_kill_report(void)
Gleb Natapovf64622c2011-03-15 13:56:04 +02001683{
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +02001684 if (!qtest_enabled() && shutdown_signal != -1) {
Peter Maydellf1d3fb02011-03-30 22:03:38 +01001685 fprintf(stderr, "qemu: terminating on signal %d", shutdown_signal);
1686 if (shutdown_pid == 0) {
1687 /* This happens for eg ^C at the terminal, so it's worth
1688 * avoiding printing an odd message in that case.
1689 */
1690 fputc('\n', stderr);
1691 } else {
Andreas Färber953ffe02011-06-02 19:58:06 +02001692 fprintf(stderr, " from pid " FMT_pid "\n", shutdown_pid);
Peter Maydellf1d3fb02011-03-30 22:03:38 +01001693 }
Gleb Natapovf64622c2011-03-15 13:56:04 +02001694 shutdown_signal = -1;
1695 }
1696}
1697
Blue Swirl4fdcac02012-10-28 11:04:47 +00001698static int qemu_reset_requested(void)
aurel32cf7a2fe2008-03-18 06:53:05 +00001699{
1700 int r = reset_requested;
1701 reset_requested = 0;
1702 return r;
1703}
1704
Gerd Hoffmann95b363b2012-02-23 13:45:19 +01001705static int qemu_suspend_requested(void)
1706{
1707 int r = suspend_requested;
1708 suspend_requested = 0;
1709 return r;
1710}
1711
Luiz Capitulino14058192012-08-08 17:29:17 -03001712static int qemu_wakeup_requested(void)
1713{
1714 int r = wakeup_requested;
1715 wakeup_requested = 0;
1716 return r;
1717}
1718
Blue Swirl4fdcac02012-10-28 11:04:47 +00001719static int qemu_powerdown_requested(void)
aurel32cf7a2fe2008-03-18 06:53:05 +00001720{
1721 int r = powerdown_requested;
1722 powerdown_requested = 0;
1723 return r;
1724}
1725
aliguorie5689022009-04-24 18:03:54 +00001726static int qemu_debug_requested(void)
1727{
1728 int r = debug_requested;
1729 debug_requested = 0;
1730 return r;
1731}
1732
Luiz Capitulino0461d5a2011-09-30 14:45:27 -03001733/* We use RUN_STATE_MAX but any invalid value will do */
Luiz Capitulino0d45b702011-09-30 14:27:11 -03001734static bool qemu_vmstop_requested(RunState *r)
aliguori6e29f5d2009-04-24 18:04:02 +00001735{
Luiz Capitulino0461d5a2011-09-30 14:45:27 -03001736 if (vmstop_requested < RUN_STATE_MAX) {
Luiz Capitulino0d45b702011-09-30 14:27:11 -03001737 *r = vmstop_requested;
Luiz Capitulino0461d5a2011-09-30 14:45:27 -03001738 vmstop_requested = RUN_STATE_MAX;
Luiz Capitulino0d45b702011-09-30 14:27:11 -03001739 return true;
1740 }
1741
1742 return false;
aliguori6e29f5d2009-04-24 18:04:02 +00001743}
1744
Jan Kiszkaa08d4362009-06-27 09:25:07 +02001745void qemu_register_reset(QEMUResetHandler *func, void *opaque)
bellardbb0c6722004-06-20 12:37:32 +00001746{
Anthony Liguori7267c092011-08-20 22:09:37 -05001747 QEMUResetEntry *re = g_malloc0(sizeof(QEMUResetEntry));
bellardbb0c6722004-06-20 12:37:32 +00001748
bellardbb0c6722004-06-20 12:37:32 +00001749 re->func = func;
1750 re->opaque = opaque;
Blue Swirl72cf2d42009-09-12 07:36:22 +00001751 QTAILQ_INSERT_TAIL(&reset_handlers, re, entry);
bellardbb0c6722004-06-20 12:37:32 +00001752}
1753
Jan Kiszkadda9b292009-07-02 00:19:02 +02001754void qemu_unregister_reset(QEMUResetHandler *func, void *opaque)
bellardbb0c6722004-06-20 12:37:32 +00001755{
1756 QEMUResetEntry *re;
1757
Blue Swirl72cf2d42009-09-12 07:36:22 +00001758 QTAILQ_FOREACH(re, &reset_handlers, entry) {
Jan Kiszkadda9b292009-07-02 00:19:02 +02001759 if (re->func == func && re->opaque == opaque) {
Blue Swirl72cf2d42009-09-12 07:36:22 +00001760 QTAILQ_REMOVE(&reset_handlers, re, entry);
Anthony Liguori7267c092011-08-20 22:09:37 -05001761 g_free(re);
Jan Kiszkadda9b292009-07-02 00:19:02 +02001762 return;
1763 }
1764 }
1765}
1766
David Gibsonbe522022012-08-07 16:41:51 +10001767void qemu_devices_reset(void)
Jan Kiszkadda9b292009-07-02 00:19:02 +02001768{
1769 QEMUResetEntry *re, *nre;
1770
1771 /* reset all devices */
Blue Swirl72cf2d42009-09-12 07:36:22 +00001772 QTAILQ_FOREACH_SAFE(re, &reset_handlers, entry, nre) {
bellardbb0c6722004-06-20 12:37:32 +00001773 re->func(re->opaque);
1774 }
David Gibsonbe522022012-08-07 16:41:51 +10001775}
1776
1777void qemu_system_reset(bool report)
1778{
1779 if (current_machine && current_machine->reset) {
1780 current_machine->reset();
1781 } else {
1782 qemu_devices_reset();
1783 }
Jan Kiszkae063eb12011-06-14 18:29:43 +02001784 if (report) {
1785 monitor_protocol_event(QEVENT_RESET, NULL);
1786 }
Jan Kiszkaea375f92010-03-01 19:10:30 +01001787 cpu_synchronize_all_post_reset();
bellardbb0c6722004-06-20 12:37:32 +00001788}
1789
1790void qemu_system_reset_request(void)
1791{
bellardd1beab82006-10-02 19:44:22 +00001792 if (no_reboot) {
1793 shutdown_requested = 1;
1794 } else {
1795 reset_requested = 1;
1796 }
Jan Kiszkab4a3d962011-02-01 22:15:43 +01001797 cpu_stop_current();
aliguorid9f75a42009-04-24 18:03:11 +00001798 qemu_notify_event();
bellardbb0c6722004-06-20 12:37:32 +00001799}
1800
Gerd Hoffmann95b363b2012-02-23 13:45:19 +01001801static void qemu_system_suspend(void)
1802{
1803 pause_all_vcpus();
1804 notifier_list_notify(&suspend_notifiers, NULL);
Luiz Capitulinoad02b962012-04-27 13:33:36 -03001805 runstate_set(RUN_STATE_SUSPENDED);
Gerd Hoffmann53370b72012-02-23 13:45:26 +01001806 monitor_protocol_event(QEVENT_SUSPEND, NULL);
Gerd Hoffmann95b363b2012-02-23 13:45:19 +01001807}
1808
1809void qemu_system_suspend_request(void)
1810{
Luiz Capitulino9abc62f2012-04-27 14:31:12 -03001811 if (runstate_check(RUN_STATE_SUSPENDED)) {
Gerd Hoffmann95b363b2012-02-23 13:45:19 +01001812 return;
1813 }
1814 suspend_requested = 1;
1815 cpu_stop_current();
1816 qemu_notify_event();
1817}
1818
1819void qemu_register_suspend_notifier(Notifier *notifier)
1820{
1821 notifier_list_add(&suspend_notifiers, notifier);
1822}
1823
1824void qemu_system_wakeup_request(WakeupReason reason)
1825{
Luiz Capitulino9abc62f2012-04-27 14:31:12 -03001826 if (!runstate_check(RUN_STATE_SUSPENDED)) {
Gerd Hoffmann95b363b2012-02-23 13:45:19 +01001827 return;
1828 }
1829 if (!(wakeup_reason_mask & (1 << reason))) {
1830 return;
1831 }
Luiz Capitulinoad02b962012-04-27 13:33:36 -03001832 runstate_set(RUN_STATE_RUNNING);
Gerd Hoffmann95b363b2012-02-23 13:45:19 +01001833 notifier_list_notify(&wakeup_notifiers, &reason);
Luiz Capitulino14058192012-08-08 17:29:17 -03001834 wakeup_requested = 1;
Gerd Hoffmann95b363b2012-02-23 13:45:19 +01001835 qemu_notify_event();
Gerd Hoffmann95b363b2012-02-23 13:45:19 +01001836}
1837
1838void qemu_system_wakeup_enable(WakeupReason reason, bool enabled)
1839{
1840 if (enabled) {
1841 wakeup_reason_mask |= (1 << reason);
1842 } else {
1843 wakeup_reason_mask &= ~(1 << reason);
1844 }
1845}
1846
1847void qemu_register_wakeup_notifier(Notifier *notifier)
1848{
1849 notifier_list_add(&wakeup_notifiers, notifier);
1850}
1851
Gleb Natapovf64622c2011-03-15 13:56:04 +02001852void qemu_system_killed(int signal, pid_t pid)
1853{
1854 shutdown_signal = signal;
1855 shutdown_pid = pid;
Kevin Wolfd9389b92011-09-14 15:38:40 +02001856 no_shutdown = 0;
Gleb Natapovf64622c2011-03-15 13:56:04 +02001857 qemu_system_shutdown_request();
1858}
1859
bellardbb0c6722004-06-20 12:37:32 +00001860void qemu_system_shutdown_request(void)
1861{
1862 shutdown_requested = 1;
aliguorid9f75a42009-04-24 18:03:11 +00001863 qemu_notify_event();
bellardbb0c6722004-06-20 12:37:32 +00001864}
1865
Igor Mammedov013c2f12012-09-05 23:06:25 +02001866static void qemu_system_powerdown(void)
1867{
1868 monitor_protocol_event(QEVENT_POWERDOWN, NULL);
1869 notifier_list_notify(&powerdown_notifiers, NULL);
1870}
1871
bellard34751872005-07-02 14:31:34 +00001872void qemu_system_powerdown_request(void)
1873{
1874 powerdown_requested = 1;
aliguorid9f75a42009-04-24 18:03:11 +00001875 qemu_notify_event();
1876}
1877
Igor Mammedova9552c82012-09-05 23:06:21 +02001878void qemu_register_powerdown_notifier(Notifier *notifier)
1879{
1880 notifier_list_add(&powerdown_notifiers, notifier);
1881}
1882
Jan Kiszka8cf71712011-02-07 12:19:16 +01001883void qemu_system_debug_request(void)
1884{
1885 debug_requested = 1;
Jan Kiszka83f338f2011-02-07 12:19:17 +01001886 qemu_notify_event();
Jan Kiszka8cf71712011-02-07 12:19:16 +01001887}
1888
Luiz Capitulino1dfb4dd2011-07-29 14:26:33 -03001889void qemu_system_vmstop_request(RunState state)
Jan Kiszka8cf71712011-02-07 12:19:16 +01001890{
Luiz Capitulino1dfb4dd2011-07-29 14:26:33 -03001891 vmstop_requested = state;
Jan Kiszka8cf71712011-02-07 12:19:16 +01001892 qemu_notify_event();
1893}
1894
Paolo Bonzini99435902011-09-12 14:03:13 +02001895static bool main_loop_should_exit(void)
1896{
1897 RunState r;
1898 if (qemu_debug_requested()) {
1899 vm_stop(RUN_STATE_DEBUG);
1900 }
Gerd Hoffmann95b363b2012-02-23 13:45:19 +01001901 if (qemu_suspend_requested()) {
1902 qemu_system_suspend();
1903 }
Paolo Bonzini99435902011-09-12 14:03:13 +02001904 if (qemu_shutdown_requested()) {
1905 qemu_kill_report();
1906 monitor_protocol_event(QEVENT_SHUTDOWN, NULL);
1907 if (no_shutdown) {
1908 vm_stop(RUN_STATE_SHUTDOWN);
1909 } else {
1910 return true;
1911 }
1912 }
1913 if (qemu_reset_requested()) {
1914 pause_all_vcpus();
1915 cpu_synchronize_all_states();
1916 qemu_system_reset(VMRESET_REPORT);
1917 resume_all_vcpus();
1918 if (runstate_check(RUN_STATE_INTERNAL_ERROR) ||
1919 runstate_check(RUN_STATE_SHUTDOWN)) {
1920 runstate_set(RUN_STATE_PAUSED);
1921 }
1922 }
Luiz Capitulino14058192012-08-08 17:29:17 -03001923 if (qemu_wakeup_requested()) {
1924 pause_all_vcpus();
1925 cpu_synchronize_all_states();
1926 qemu_system_reset(VMRESET_SILENT);
1927 resume_all_vcpus();
Luiz Capitulino17c86602012-08-09 11:27:30 -03001928 monitor_protocol_event(QEVENT_WAKEUP, NULL);
Luiz Capitulino14058192012-08-08 17:29:17 -03001929 }
Paolo Bonzini99435902011-09-12 14:03:13 +02001930 if (qemu_powerdown_requested()) {
Igor Mammedov013c2f12012-09-05 23:06:25 +02001931 qemu_system_powerdown();
Paolo Bonzini99435902011-09-12 14:03:13 +02001932 }
1933 if (qemu_vmstop_requested(&r)) {
1934 vm_stop(r);
1935 }
1936 return false;
1937}
1938
aliguori43b96852009-04-24 18:03:33 +00001939static void main_loop(void)
1940{
Jan Kiszkac9f711a2011-08-22 17:46:02 +02001941 bool nonblocking;
Paolo Bonzini99435902011-09-12 14:03:13 +02001942 int last_io = 0;
Jan Kiszka8e1b90e2011-02-01 22:15:46 +01001943#ifdef CONFIG_PROFILER
1944 int64_t ti;
1945#endif
Paolo Bonzini99435902011-09-12 14:03:13 +02001946 do {
Jan Kiszkac9f711a2011-08-22 17:46:02 +02001947 nonblocking = !kvm_enabled() && last_io > 0;
aliguori43b96852009-04-24 18:03:33 +00001948#ifdef CONFIG_PROFILER
Jan Kiszka46481d32011-02-01 22:15:47 +01001949 ti = profile_getclock();
aliguori43b96852009-04-24 18:03:33 +00001950#endif
Jan Kiszkac9f711a2011-08-22 17:46:02 +02001951 last_io = main_loop_wait(nonblocking);
aliguori43b96852009-04-24 18:03:33 +00001952#ifdef CONFIG_PROFILER
Jan Kiszka46481d32011-02-01 22:15:47 +01001953 dev_time += profile_getclock() - ti;
aliguori43b96852009-04-24 18:03:33 +00001954#endif
Paolo Bonzini99435902011-09-12 14:03:13 +02001955 } while (!main_loop_should_exit());
bellardb4608c02003-06-27 17:34:32 +00001956}
1957
pbrook9bd7e6d2009-04-07 22:58:45 +00001958static void version(void)
1959{
Thomas Monjalonf75ca1a2010-04-28 14:42:01 +02001960 printf("QEMU emulator version " QEMU_VERSION QEMU_PKGVERSION ", Copyright (c) 2003-2008 Fabrice Bellard\n");
pbrook9bd7e6d2009-04-07 22:58:45 +00001961}
1962
ths15f82202007-06-29 23:26:08 +00001963static void help(int exitcode)
bellard0824d6f2003-06-24 13:42:40 +00001964{
Michael Ellermana3adb7a2011-12-19 17:19:31 +11001965 version();
1966 printf("usage: %s [options] [disk_image]\n\n"
1967 "'disk_image' is a raw hard disk image for IDE hard disk 0\n\n",
1968 error_get_progname());
1969
Michael Ellerman77bd1112011-12-19 17:19:30 +11001970#define QEMU_OPTIONS_GENERATE_HELP
1971#include "qemu-options-wrapper.h"
Michael Ellermana3adb7a2011-12-19 17:19:31 +11001972
1973 printf("\nDuring emulation, the following keys are useful:\n"
malc3f020d72010-02-08 12:04:56 +03001974 "ctrl-alt-f toggle full screen\n"
1975 "ctrl-alt-n switch to virtual console 'n'\n"
1976 "ctrl-alt toggle mouse and keyboard grab\n"
1977 "\n"
Michael Ellermana3adb7a2011-12-19 17:19:31 +11001978 "When using -nographic, press 'ctrl-a h' to get some help.\n");
1979
ths15f82202007-06-29 23:26:08 +00001980 exit(exitcode);
bellard0824d6f2003-06-24 13:42:40 +00001981}
1982
bellardcd6f1162004-05-13 22:02:20 +00001983#define HAS_ARG 0x0001
1984
bellardcd6f1162004-05-13 22:02:20 +00001985typedef struct QEMUOption {
1986 const char *name;
1987 int flags;
1988 int index;
Blue Swirlad960902010-03-29 19:23:52 +00001989 uint32_t arch_mask;
bellardcd6f1162004-05-13 22:02:20 +00001990} QEMUOption;
1991
blueswir1dbed7e42008-10-01 19:38:09 +00001992static const QEMUOption qemu_options[] = {
Blue Swirlad960902010-03-29 19:23:52 +00001993 { "h", 0, QEMU_OPTION_h, QEMU_ARCH_ALL },
Michael Ellerman77bd1112011-12-19 17:19:30 +11001994#define QEMU_OPTIONS_GENERATE_OPTIONS
1995#include "qemu-options-wrapper.h"
bellardcd6f1162004-05-13 22:02:20 +00001996 { NULL },
bellardfc01f7e2003-06-30 10:03:06 +00001997};
Blue Swirla369da52011-09-27 19:15:42 +00001998
1999static bool vga_available(void)
2000{
Aurelien Jarno36b7f272012-09-08 16:01:20 +02002001 return object_class_by_name("VGA") || object_class_by_name("isa-vga");
Blue Swirla369da52011-09-27 19:15:42 +00002002}
2003
2004static bool cirrus_vga_available(void)
2005{
Aurelien Jarno36b7f272012-09-08 16:01:20 +02002006 return object_class_by_name("cirrus-vga")
2007 || object_class_by_name("isa-cirrus-vga");
Blue Swirla369da52011-09-27 19:15:42 +00002008}
2009
2010static bool vmware_vga_available(void)
2011{
Aurelien Jarno36b7f272012-09-08 16:01:20 +02002012 return object_class_by_name("vmware-svga");
Blue Swirla369da52011-09-27 19:15:42 +00002013}
2014
Aurelien Jarno879049a2012-09-08 12:01:06 +02002015static bool qxl_vga_available(void)
2016{
2017 return object_class_by_name("qxl-vga");
2018}
2019
malc3893c122008-09-28 00:42:05 +00002020static void select_vgahw (const char *p)
2021{
2022 const char *opts;
2023
Zachary Amsden86176752009-07-30 00:15:02 -10002024 vga_interface_type = VGA_NONE;
malc3893c122008-09-28 00:42:05 +00002025 if (strstart(p, "std", &opts)) {
Blue Swirla369da52011-09-27 19:15:42 +00002026 if (vga_available()) {
2027 vga_interface_type = VGA_STD;
2028 } else {
2029 fprintf(stderr, "Error: standard VGA not available\n");
2030 exit(0);
2031 }
malc3893c122008-09-28 00:42:05 +00002032 } else if (strstart(p, "cirrus", &opts)) {
Blue Swirla369da52011-09-27 19:15:42 +00002033 if (cirrus_vga_available()) {
2034 vga_interface_type = VGA_CIRRUS;
2035 } else {
2036 fprintf(stderr, "Error: Cirrus VGA not available\n");
2037 exit(0);
2038 }
malc3893c122008-09-28 00:42:05 +00002039 } else if (strstart(p, "vmware", &opts)) {
Blue Swirla369da52011-09-27 19:15:42 +00002040 if (vmware_vga_available()) {
2041 vga_interface_type = VGA_VMWARE;
2042 } else {
2043 fprintf(stderr, "Error: VMWare SVGA not available\n");
2044 exit(0);
2045 }
aliguori94909d92009-04-22 15:19:53 +00002046 } else if (strstart(p, "xenfb", &opts)) {
Zachary Amsden86176752009-07-30 00:15:02 -10002047 vga_interface_type = VGA_XENFB;
Gerd Hoffmanna19cbfb2010-04-27 11:50:11 +02002048 } else if (strstart(p, "qxl", &opts)) {
Aurelien Jarno879049a2012-09-08 12:01:06 +02002049 if (qxl_vga_available()) {
2050 vga_interface_type = VGA_QXL;
2051 } else {
2052 fprintf(stderr, "Error: QXL VGA not available\n");
2053 exit(0);
2054 }
aliguori28b85ed2009-04-22 15:19:48 +00002055 } else if (!strstart(p, "none", &opts)) {
malc3893c122008-09-28 00:42:05 +00002056 invalid_vga:
2057 fprintf(stderr, "Unknown vga type: %s\n", p);
2058 exit(1);
2059 }
malccb5a7aa2008-09-28 00:42:12 +00002060 while (*opts) {
2061 const char *nextopt;
2062
2063 if (strstart(opts, ",retrace=", &nextopt)) {
2064 opts = nextopt;
2065 if (strstart(opts, "dumb", &nextopt))
2066 vga_retrace_method = VGA_RETRACE_DUMB;
2067 else if (strstart(opts, "precise", &nextopt))
2068 vga_retrace_method = VGA_RETRACE_PRECISE;
2069 else goto invalid_vga;
2070 } else goto invalid_vga;
2071 opts = nextopt;
2072 }
malc3893c122008-09-28 00:42:05 +00002073}
2074
Jes Sorensen1472a952011-03-16 13:33:31 +01002075static DisplayType select_display(const char *p)
2076{
2077 const char *opts;
2078 DisplayType display = DT_DEFAULT;
2079
2080 if (strstart(p, "sdl", &opts)) {
2081#ifdef CONFIG_SDL
2082 display = DT_SDL;
2083 while (*opts) {
2084 const char *nextopt;
2085
2086 if (strstart(opts, ",frame=", &nextopt)) {
2087 opts = nextopt;
2088 if (strstart(opts, "on", &nextopt)) {
2089 no_frame = 0;
2090 } else if (strstart(opts, "off", &nextopt)) {
2091 no_frame = 1;
2092 } else {
Peter Maydell05175532011-03-23 03:40:57 +00002093 goto invalid_sdl_args;
Jes Sorensen1472a952011-03-16 13:33:31 +01002094 }
2095 } else if (strstart(opts, ",alt_grab=", &nextopt)) {
2096 opts = nextopt;
2097 if (strstart(opts, "on", &nextopt)) {
2098 alt_grab = 1;
2099 } else if (strstart(opts, "off", &nextopt)) {
2100 alt_grab = 0;
2101 } else {
Peter Maydell05175532011-03-23 03:40:57 +00002102 goto invalid_sdl_args;
Jes Sorensen1472a952011-03-16 13:33:31 +01002103 }
2104 } else if (strstart(opts, ",ctrl_grab=", &nextopt)) {
2105 opts = nextopt;
2106 if (strstart(opts, "on", &nextopt)) {
2107 ctrl_grab = 1;
2108 } else if (strstart(opts, "off", &nextopt)) {
2109 ctrl_grab = 0;
2110 } else {
Peter Maydell05175532011-03-23 03:40:57 +00002111 goto invalid_sdl_args;
Jes Sorensen1472a952011-03-16 13:33:31 +01002112 }
2113 } else if (strstart(opts, ",window_close=", &nextopt)) {
2114 opts = nextopt;
2115 if (strstart(opts, "on", &nextopt)) {
2116 no_quit = 0;
2117 } else if (strstart(opts, "off", &nextopt)) {
2118 no_quit = 1;
2119 } else {
Peter Maydell05175532011-03-23 03:40:57 +00002120 goto invalid_sdl_args;
Jes Sorensen1472a952011-03-16 13:33:31 +01002121 }
2122 } else {
Peter Maydell05175532011-03-23 03:40:57 +00002123 invalid_sdl_args:
2124 fprintf(stderr, "Invalid SDL option string: %s\n", p);
2125 exit(1);
Jes Sorensen1472a952011-03-16 13:33:31 +01002126 }
2127 opts = nextopt;
2128 }
2129#else
2130 fprintf(stderr, "SDL support is disabled\n");
2131 exit(1);
2132#endif
Jes Sorensen3264ff12011-03-16 13:33:33 +01002133 } else if (strstart(p, "vnc", &opts)) {
Jes Sorensen821601e2011-03-16 13:33:36 +01002134#ifdef CONFIG_VNC
Jes Sorensen3264ff12011-03-16 13:33:33 +01002135 display_remote++;
2136
2137 if (*opts) {
2138 const char *nextopt;
2139
2140 if (strstart(opts, "=", &nextopt)) {
2141 vnc_display = nextopt;
2142 }
2143 }
2144 if (!vnc_display) {
2145 fprintf(stderr, "VNC requires a display argument vnc=<display>\n");
2146 exit(1);
2147 }
Jes Sorensen821601e2011-03-16 13:33:36 +01002148#else
2149 fprintf(stderr, "VNC support is disabled\n");
2150 exit(1);
2151#endif
Jes Sorensen1472a952011-03-16 13:33:31 +01002152 } else if (strstart(p, "curses", &opts)) {
2153#ifdef CONFIG_CURSES
2154 display = DT_CURSES;
2155#else
2156 fprintf(stderr, "Curses support is disabled\n");
2157 exit(1);
2158#endif
Jes Sorensen4171d322011-03-16 13:33:32 +01002159 } else if (strstart(p, "none", &opts)) {
2160 display = DT_NONE;
Jes Sorensen1472a952011-03-16 13:33:31 +01002161 } else {
Jes Sorensen1472a952011-03-16 13:33:31 +01002162 fprintf(stderr, "Unknown display type: %s\n", p);
2163 exit(1);
2164 }
2165
2166 return display;
2167}
2168
Markus Armbruster7d4c3d52009-06-26 19:15:14 +02002169static int balloon_parse(const char *arg)
2170{
Gerd Hoffmann382f0742009-08-14 10:34:22 +02002171 QemuOpts *opts;
Markus Armbruster7d4c3d52009-06-26 19:15:14 +02002172
Gerd Hoffmann382f0742009-08-14 10:34:22 +02002173 if (strcmp(arg, "none") == 0) {
2174 return 0;
Markus Armbruster7d4c3d52009-06-26 19:15:14 +02002175 }
Gerd Hoffmann382f0742009-08-14 10:34:22 +02002176
2177 if (!strncmp(arg, "virtio", 6)) {
2178 if (arg[6] == ',') {
2179 /* have params -> parse them */
Gerd Hoffmann3329f072010-08-20 13:52:01 +02002180 opts = qemu_opts_parse(qemu_find_opts("device"), arg+7, 0);
Gerd Hoffmann382f0742009-08-14 10:34:22 +02002181 if (!opts)
2182 return -1;
2183 } else {
2184 /* create empty opts */
Dong Xu Wange478b442012-12-06 14:47:22 +08002185 opts = qemu_opts_create_nofail(qemu_find_opts("device"));
Gerd Hoffmann382f0742009-08-14 10:34:22 +02002186 }
Alexander Graf29f82b32011-03-29 15:29:29 +02002187 qemu_opt_set(opts, "driver", "virtio-balloon");
Gerd Hoffmann382f0742009-08-14 10:34:22 +02002188 return 0;
2189 }
2190
2191 return -1;
Markus Armbruster7d4c3d52009-06-26 19:15:14 +02002192}
Markus Armbruster7d4c3d52009-06-26 19:15:14 +02002193
Paul Brook5cea8592009-05-30 00:52:44 +01002194char *qemu_find_file(int type, const char *name)
2195{
2196 int len;
2197 const char *subdir;
2198 char *buf;
2199
Peter Maydell31783202012-05-25 13:07:01 +01002200 /* Try the name as a straight path first */
2201 if (access(name, R_OK) == 0) {
Anthony Liguori7267c092011-08-20 22:09:37 -05002202 return g_strdup(name);
Paul Brook5cea8592009-05-30 00:52:44 +01002203 }
2204 switch (type) {
2205 case QEMU_FILE_TYPE_BIOS:
2206 subdir = "";
2207 break;
2208 case QEMU_FILE_TYPE_KEYMAP:
2209 subdir = "keymaps/";
2210 break;
2211 default:
2212 abort();
2213 }
2214 len = strlen(data_dir) + strlen(name) + strlen(subdir) + 2;
Anthony Liguori7267c092011-08-20 22:09:37 -05002215 buf = g_malloc0(len);
Blue Swirl3a417592009-06-09 19:12:21 +00002216 snprintf(buf, len, "%s/%s%s", data_dir, subdir, name);
Paul Brook5cea8592009-05-30 00:52:44 +01002217 if (access(buf, R_OK)) {
Anthony Liguori7267c092011-08-20 22:09:37 -05002218 g_free(buf);
Paul Brook5cea8592009-05-30 00:52:44 +01002219 return NULL;
2220 }
2221 return buf;
2222}
2223
Markus Armbrusterff952ba2010-01-29 19:48:57 +01002224static int device_help_func(QemuOpts *opts, void *opaque)
2225{
2226 return qdev_device_help(opts);
2227}
2228
Gerd Hoffmannf31d07d2009-07-31 12:25:37 +02002229static int device_init_func(QemuOpts *opts, void *opaque)
2230{
2231 DeviceState *dev;
2232
2233 dev = qdev_device_add(opts);
2234 if (!dev)
2235 return -1;
2236 return 0;
2237}
2238
Gerd Hoffmann1a688d32009-12-08 13:11:36 +01002239static int chardev_init_func(QemuOpts *opts, void *opaque)
2240{
Gerd Hoffmannbd2d80b2012-10-15 09:28:05 +02002241 Error *local_err = NULL;
Gerd Hoffmann1a688d32009-12-08 13:11:36 +01002242
Gerd Hoffmannbd2d80b2012-10-15 09:28:05 +02002243 qemu_chr_new_from_opts(opts, NULL, &local_err);
2244 if (error_is_set(&local_err)) {
2245 fprintf(stderr, "%s\n", error_get_pretty(local_err));
2246 error_free(local_err);
Gerd Hoffmann1a688d32009-12-08 13:11:36 +01002247 return -1;
Gerd Hoffmannbd2d80b2012-10-15 09:28:05 +02002248 }
Gerd Hoffmann1a688d32009-12-08 13:11:36 +01002249 return 0;
2250}
2251
Venkateswararao Jujjuri (JV)758e8e32010-06-14 13:34:41 -07002252#ifdef CONFIG_VIRTFS
Gautham R Shenoy74db9202010-04-29 17:44:43 +05302253static int fsdev_init_func(QemuOpts *opts, void *opaque)
2254{
2255 int ret;
2256 ret = qemu_fsdev_add(opts);
2257
2258 return ret;
2259}
2260#endif
2261
Gerd Hoffmann88589342009-12-08 13:11:50 +01002262static int mon_init_func(QemuOpts *opts, void *opaque)
2263{
2264 CharDriverState *chr;
2265 const char *chardev;
2266 const char *mode;
2267 int flags;
2268
2269 mode = qemu_opt_get(opts, "mode");
2270 if (mode == NULL) {
2271 mode = "readline";
2272 }
2273 if (strcmp(mode, "readline") == 0) {
2274 flags = MONITOR_USE_READLINE;
2275 } else if (strcmp(mode, "control") == 0) {
2276 flags = MONITOR_USE_CONTROL;
2277 } else {
2278 fprintf(stderr, "unknown monitor mode \"%s\"\n", mode);
2279 exit(1);
2280 }
2281
Daniel P. Berrange39eaab92010-06-07 15:42:31 +01002282 if (qemu_opt_get_bool(opts, "pretty", 0))
2283 flags |= MONITOR_USE_PRETTY;
2284
Gerd Hoffmann88589342009-12-08 13:11:50 +01002285 if (qemu_opt_get_bool(opts, "default", 0))
2286 flags |= MONITOR_IS_DEFAULT;
2287
2288 chardev = qemu_opt_get(opts, "chardev");
2289 chr = qemu_chr_find(chardev);
2290 if (chr == NULL) {
2291 fprintf(stderr, "chardev \"%s\" not found\n", chardev);
2292 exit(1);
2293 }
2294
2295 monitor_init(chr, flags);
2296 return 0;
2297}
2298
Gerd Hoffmann6ca55822009-12-08 13:11:52 +01002299static void monitor_parse(const char *optarg, const char *mode)
Gerd Hoffmann88589342009-12-08 13:11:50 +01002300{
2301 static int monitor_device_index = 0;
2302 QemuOpts *opts;
2303 const char *p;
2304 char label[32];
2305 int def = 0;
2306
2307 if (strstart(optarg, "chardev:", &p)) {
2308 snprintf(label, sizeof(label), "%s", p);
2309 } else {
Jan Kiszka140e0652010-04-06 16:55:52 +02002310 snprintf(label, sizeof(label), "compat_monitor%d",
2311 monitor_device_index);
2312 if (monitor_device_index == 0) {
Gerd Hoffmann88589342009-12-08 13:11:50 +01002313 def = 1;
2314 }
2315 opts = qemu_chr_parse_compat(label, optarg);
2316 if (!opts) {
2317 fprintf(stderr, "parse error: %s\n", optarg);
2318 exit(1);
2319 }
2320 }
2321
Luiz Capitulino8be7e7e2012-03-20 15:51:57 -03002322 opts = qemu_opts_create(qemu_find_opts("mon"), label, 1, NULL);
Gerd Hoffmann88589342009-12-08 13:11:50 +01002323 if (!opts) {
2324 fprintf(stderr, "duplicate chardev: %s\n", label);
2325 exit(1);
2326 }
Gerd Hoffmann6ca55822009-12-08 13:11:52 +01002327 qemu_opt_set(opts, "mode", mode);
Gerd Hoffmann88589342009-12-08 13:11:50 +01002328 qemu_opt_set(opts, "chardev", label);
2329 if (def)
2330 qemu_opt_set(opts, "default", "on");
2331 monitor_device_index++;
2332}
2333
Gerd Hoffmannbd3c9482009-07-15 13:59:26 +02002334struct device_config {
2335 enum {
Gerd Hoffmannaee1b932009-12-08 13:11:53 +01002336 DEV_USB, /* -usbdevice */
2337 DEV_BT, /* -bt */
2338 DEV_SERIAL, /* -serial */
2339 DEV_PARALLEL, /* -parallel */
2340 DEV_VIRTCON, /* -virtioconsole */
H. Peter Anvinc9f398e2009-12-29 13:51:36 -08002341 DEV_DEBUGCON, /* -debugcon */
Markus Armbrusteref0c4a02012-02-07 15:09:13 +01002342 DEV_GDB, /* -gdb, -s */
Gerd Hoffmannbd3c9482009-07-15 13:59:26 +02002343 } type;
2344 const char *cmdline;
Markus Armbrusterd9a59542012-02-07 15:09:12 +01002345 Location loc;
Blue Swirl72cf2d42009-09-12 07:36:22 +00002346 QTAILQ_ENTRY(device_config) next;
Gerd Hoffmannbd3c9482009-07-15 13:59:26 +02002347};
Blue Swirl4fdcac02012-10-28 11:04:47 +00002348
2349static QTAILQ_HEAD(, device_config) device_configs =
2350 QTAILQ_HEAD_INITIALIZER(device_configs);
Gerd Hoffmannbd3c9482009-07-15 13:59:26 +02002351
2352static void add_device_config(int type, const char *cmdline)
2353{
2354 struct device_config *conf;
2355
Anthony Liguori7267c092011-08-20 22:09:37 -05002356 conf = g_malloc0(sizeof(*conf));
Gerd Hoffmannbd3c9482009-07-15 13:59:26 +02002357 conf->type = type;
2358 conf->cmdline = cmdline;
Markus Armbrusterd9a59542012-02-07 15:09:12 +01002359 loc_save(&conf->loc);
Blue Swirl72cf2d42009-09-12 07:36:22 +00002360 QTAILQ_INSERT_TAIL(&device_configs, conf, next);
Gerd Hoffmannbd3c9482009-07-15 13:59:26 +02002361}
2362
2363static int foreach_device_config(int type, int (*func)(const char *cmdline))
2364{
2365 struct device_config *conf;
2366 int rc;
2367
Blue Swirl72cf2d42009-09-12 07:36:22 +00002368 QTAILQ_FOREACH(conf, &device_configs, next) {
Gerd Hoffmannbd3c9482009-07-15 13:59:26 +02002369 if (conf->type != type)
2370 continue;
Markus Armbrusterd9a59542012-02-07 15:09:12 +01002371 loc_push_restore(&conf->loc);
Gerd Hoffmannbd3c9482009-07-15 13:59:26 +02002372 rc = func(conf->cmdline);
Markus Armbrusterd9a59542012-02-07 15:09:12 +01002373 loc_pop(&conf->loc);
Gerd Hoffmannbd3c9482009-07-15 13:59:26 +02002374 if (0 != rc)
2375 return rc;
2376 }
2377 return 0;
2378}
2379
Gerd Hoffmann998bbd72009-12-08 13:11:41 +01002380static int serial_parse(const char *devname)
2381{
2382 static int index = 0;
2383 char label[32];
2384
2385 if (strcmp(devname, "none") == 0)
2386 return 0;
2387 if (index == MAX_SERIAL_PORTS) {
2388 fprintf(stderr, "qemu: too many serial ports\n");
2389 exit(1);
2390 }
2391 snprintf(label, sizeof(label), "serial%d", index);
Anthony Liguori27143a42011-08-15 11:17:36 -05002392 serial_hds[index] = qemu_chr_new(label, devname, NULL);
Gerd Hoffmann998bbd72009-12-08 13:11:41 +01002393 if (!serial_hds[index]) {
Peter Maydell52d06132012-07-09 04:28:30 +00002394 fprintf(stderr, "qemu: could not connect serial device"
2395 " to character backend '%s'\n", devname);
Gerd Hoffmann998bbd72009-12-08 13:11:41 +01002396 return -1;
2397 }
2398 index++;
2399 return 0;
2400}
2401
Gerd Hoffmann6a5e8b02009-12-08 13:11:42 +01002402static int parallel_parse(const char *devname)
2403{
2404 static int index = 0;
2405 char label[32];
2406
2407 if (strcmp(devname, "none") == 0)
2408 return 0;
2409 if (index == MAX_PARALLEL_PORTS) {
2410 fprintf(stderr, "qemu: too many parallel ports\n");
2411 exit(1);
2412 }
2413 snprintf(label, sizeof(label), "parallel%d", index);
Anthony Liguori27143a42011-08-15 11:17:36 -05002414 parallel_hds[index] = qemu_chr_new(label, devname, NULL);
Gerd Hoffmann6a5e8b02009-12-08 13:11:42 +01002415 if (!parallel_hds[index]) {
Peter Maydell52d06132012-07-09 04:28:30 +00002416 fprintf(stderr, "qemu: could not connect parallel device"
2417 " to character backend '%s'\n", devname);
Gerd Hoffmann6a5e8b02009-12-08 13:11:42 +01002418 return -1;
2419 }
2420 index++;
2421 return 0;
2422}
2423
Gerd Hoffmannaee1b932009-12-08 13:11:53 +01002424static int virtcon_parse(const char *devname)
2425{
Gerd Hoffmann3329f072010-08-20 13:52:01 +02002426 QemuOptsList *device = qemu_find_opts("device");
Gerd Hoffmannaee1b932009-12-08 13:11:53 +01002427 static int index = 0;
2428 char label[32];
Amit Shah392ecf52010-01-21 16:19:23 +05302429 QemuOpts *bus_opts, *dev_opts;
Gerd Hoffmannaee1b932009-12-08 13:11:53 +01002430
2431 if (strcmp(devname, "none") == 0)
2432 return 0;
2433 if (index == MAX_VIRTIO_CONSOLES) {
2434 fprintf(stderr, "qemu: too many virtio consoles\n");
2435 exit(1);
2436 }
Amit Shah392ecf52010-01-21 16:19:23 +05302437
Dong Xu Wange478b442012-12-06 14:47:22 +08002438 bus_opts = qemu_opts_create_nofail(device);
Anthony Liguorie87f7fc2012-02-06 11:07:18 -06002439 if (arch_type == QEMU_ARCH_S390X) {
2440 qemu_opt_set(bus_opts, "driver", "virtio-serial-s390");
2441 } else {
2442 qemu_opt_set(bus_opts, "driver", "virtio-serial-pci");
2443 }
Amit Shah392ecf52010-01-21 16:19:23 +05302444
Dong Xu Wange478b442012-12-06 14:47:22 +08002445 dev_opts = qemu_opts_create_nofail(device);
Amit Shah392ecf52010-01-21 16:19:23 +05302446 qemu_opt_set(dev_opts, "driver", "virtconsole");
2447
Gerd Hoffmannaee1b932009-12-08 13:11:53 +01002448 snprintf(label, sizeof(label), "virtcon%d", index);
Anthony Liguori27143a42011-08-15 11:17:36 -05002449 virtcon_hds[index] = qemu_chr_new(label, devname, NULL);
Gerd Hoffmannaee1b932009-12-08 13:11:53 +01002450 if (!virtcon_hds[index]) {
Peter Maydell52d06132012-07-09 04:28:30 +00002451 fprintf(stderr, "qemu: could not connect virtio console"
2452 " to character backend '%s'\n", devname);
Gerd Hoffmannaee1b932009-12-08 13:11:53 +01002453 return -1;
2454 }
Amit Shah392ecf52010-01-21 16:19:23 +05302455 qemu_opt_set(dev_opts, "chardev", label);
2456
Gerd Hoffmannaee1b932009-12-08 13:11:53 +01002457 index++;
2458 return 0;
2459}
2460
H. Peter Anvinc9f398e2009-12-29 13:51:36 -08002461static int debugcon_parse(const char *devname)
2462{
2463 QemuOpts *opts;
2464
Anthony Liguori27143a42011-08-15 11:17:36 -05002465 if (!qemu_chr_new("debugcon", devname, NULL)) {
H. Peter Anvinc9f398e2009-12-29 13:51:36 -08002466 exit(1);
2467 }
Luiz Capitulino8be7e7e2012-03-20 15:51:57 -03002468 opts = qemu_opts_create(qemu_find_opts("device"), "debugcon", 1, NULL);
H. Peter Anvinc9f398e2009-12-29 13:51:36 -08002469 if (!opts) {
2470 fprintf(stderr, "qemu: already have a debugcon device\n");
2471 exit(1);
2472 }
2473 qemu_opt_set(opts, "driver", "isa-debugcon");
2474 qemu_opt_set(opts, "chardev", "debugcon");
2475 return 0;
2476}
2477
Jan Kiszka9052ea62011-07-23 12:38:37 +02002478static QEMUMachine *machine_parse(const char *name)
2479{
2480 QEMUMachine *m, *machine = NULL;
2481
2482 if (name) {
2483 machine = find_machine(name);
2484 }
2485 if (machine) {
2486 return machine;
2487 }
2488 printf("Supported machines are:\n");
2489 for (m = first_machine; m != NULL; m = m->next) {
2490 if (m->alias) {
Peter Maydell3b264862012-02-22 22:13:11 +00002491 printf("%-20s %s (alias of %s)\n", m->alias, m->desc, m->name);
Jan Kiszka9052ea62011-07-23 12:38:37 +02002492 }
Peter Maydell3b264862012-02-22 22:13:11 +00002493 printf("%-20s %s%s\n", m->name, m->desc,
Jan Kiszka9052ea62011-07-23 12:38:37 +02002494 m->is_default ? " (default)" : "");
2495 }
Peter Maydellc8057f92012-08-02 13:45:54 +01002496 exit(!name || !is_help_option(name));
Jan Kiszka9052ea62011-07-23 12:38:37 +02002497}
2498
Anthony PERARD303d4e82010-09-21 20:05:31 +01002499static int tcg_init(void)
2500{
Jan Kiszkad5ab9712011-08-02 16:10:21 +02002501 tcg_exec_init(tcg_tb_size * 1024 * 1024);
Anthony PERARD303d4e82010-09-21 20:05:31 +01002502 return 0;
2503}
2504
2505static struct {
2506 const char *opt_name;
2507 const char *name;
2508 int (*available)(void);
2509 int (*init)(void);
2510 int *allowed;
2511} accel_list[] = {
2512 { "tcg", "tcg", tcg_available, tcg_init, &tcg_allowed },
Anthony PERARD3285cf42010-08-19 12:27:56 +01002513 { "xen", "Xen", xen_available, xen_init, &xen_allowed },
Anthony PERARD303d4e82010-09-21 20:05:31 +01002514 { "kvm", "KVM", kvm_available, kvm_init, &kvm_allowed },
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +02002515 { "qtest", "QTest", qtest_available, qtest_init, &qtest_allowed },
Anthony PERARD303d4e82010-09-21 20:05:31 +01002516};
2517
2518static int configure_accelerator(void)
2519{
2520 const char *p = NULL;
2521 char buf[10];
2522 int i, ret;
Peter Maydell1b785a92012-02-07 20:57:27 +00002523 bool accel_initialised = 0;
Anthony PERARD303d4e82010-09-21 20:05:31 +01002524 bool init_failed = 0;
2525
2526 QemuOptsList *list = qemu_find_opts("machine");
2527 if (!QTAILQ_EMPTY(&list->head)) {
2528 p = qemu_opt_get(QTAILQ_FIRST(&list->head), "accel");
2529 }
2530
2531 if (p == NULL) {
2532 /* Use the default "accelerator", tcg */
2533 p = "tcg";
2534 }
2535
Peter Maydell1b785a92012-02-07 20:57:27 +00002536 while (!accel_initialised && *p != '\0') {
Anthony PERARD303d4e82010-09-21 20:05:31 +01002537 if (*p == ':') {
2538 p++;
2539 }
2540 p = get_opt_name(buf, sizeof (buf), p, ':');
2541 for (i = 0; i < ARRAY_SIZE(accel_list); i++) {
2542 if (strcmp(accel_list[i].opt_name, buf) == 0) {
Anthony Liguoria16c53b2011-06-06 08:25:06 -05002543 *(accel_list[i].allowed) = 1;
Anthony PERARD303d4e82010-09-21 20:05:31 +01002544 ret = accel_list[i].init();
2545 if (ret < 0) {
2546 init_failed = 1;
2547 if (!accel_list[i].available()) {
2548 printf("%s not supported for this target\n",
2549 accel_list[i].name);
2550 } else {
2551 fprintf(stderr, "failed to initialize %s: %s\n",
2552 accel_list[i].name,
2553 strerror(-ret));
2554 }
Anthony Liguoria16c53b2011-06-06 08:25:06 -05002555 *(accel_list[i].allowed) = 0;
Anthony PERARD303d4e82010-09-21 20:05:31 +01002556 } else {
Peter Maydell1b785a92012-02-07 20:57:27 +00002557 accel_initialised = 1;
Anthony PERARD303d4e82010-09-21 20:05:31 +01002558 }
2559 break;
2560 }
2561 }
2562 if (i == ARRAY_SIZE(accel_list)) {
2563 fprintf(stderr, "\"%s\" accelerator does not exist.\n", buf);
2564 }
2565 }
2566
Peter Maydell1b785a92012-02-07 20:57:27 +00002567 if (!accel_initialised) {
Anthony PERARD303d4e82010-09-21 20:05:31 +01002568 fprintf(stderr, "No accelerator found!\n");
2569 exit(1);
2570 }
2571
2572 if (init_failed) {
2573 fprintf(stderr, "Back to %s accelerator.\n", accel_list[i].name);
2574 }
2575
Peter Maydell1b785a92012-02-07 20:57:27 +00002576 return !accel_initialised;
Anthony PERARD303d4e82010-09-21 20:05:31 +01002577}
2578
Gerd Hoffmannfd42dee2010-06-04 14:08:07 +02002579void qemu_add_exit_notifier(Notifier *notify)
2580{
2581 notifier_list_add(&exit_notifiers, notify);
2582}
2583
2584void qemu_remove_exit_notifier(Notifier *notify)
2585{
Paolo Bonzini31552522012-01-13 17:34:01 +01002586 notifier_remove(notify);
Gerd Hoffmannfd42dee2010-06-04 14:08:07 +02002587}
2588
2589static void qemu_run_exit_notifiers(void)
2590{
Jan Kiszka9e8dd452011-06-20 14:06:26 +02002591 notifier_list_notify(&exit_notifiers, NULL);
Gerd Hoffmannfd42dee2010-06-04 14:08:07 +02002592}
2593
Gleb Natapov4cab9462010-12-08 13:35:08 +02002594void qemu_add_machine_init_done_notifier(Notifier *notify)
2595{
2596 notifier_list_add(&machine_init_done_notifiers, notify);
2597}
2598
2599static void qemu_run_machine_init_done_notifiers(void)
2600{
Jan Kiszka9e8dd452011-06-20 14:06:26 +02002601 notifier_list_notify(&machine_init_done_notifiers, NULL);
Gleb Natapov4cab9462010-12-08 13:35:08 +02002602}
2603
Anthony Liguori6530a972010-01-22 09:18:06 -06002604static const QEMUOption *lookup_opt(int argc, char **argv,
2605 const char **poptarg, int *poptind)
2606{
2607 const QEMUOption *popt;
2608 int optind = *poptind;
2609 char *r = argv[optind];
2610 const char *optarg;
2611
Markus Armbruster0f0bc3f2010-02-18 20:13:51 +01002612 loc_set_cmdline(argv, optind, 1);
Anthony Liguori6530a972010-01-22 09:18:06 -06002613 optind++;
2614 /* Treat --foo the same as -foo. */
2615 if (r[1] == '-')
2616 r++;
2617 popt = qemu_options;
2618 for(;;) {
2619 if (!popt->name) {
Markus Armbruster0f0bc3f2010-02-18 20:13:51 +01002620 error_report("invalid option");
Anthony Liguori6530a972010-01-22 09:18:06 -06002621 exit(1);
2622 }
2623 if (!strcmp(popt->name, r + 1))
2624 break;
2625 popt++;
2626 }
2627 if (popt->flags & HAS_ARG) {
2628 if (optind >= argc) {
Markus Armbruster0f0bc3f2010-02-18 20:13:51 +01002629 error_report("requires an argument");
Anthony Liguori6530a972010-01-22 09:18:06 -06002630 exit(1);
2631 }
2632 optarg = argv[optind++];
Markus Armbruster0f0bc3f2010-02-18 20:13:51 +01002633 loc_set_cmdline(argv, optind - 2, 2);
Anthony Liguori6530a972010-01-22 09:18:06 -06002634 } else {
2635 optarg = NULL;
2636 }
2637
2638 *poptarg = optarg;
2639 *poptind = optind;
2640
2641 return popt;
2642}
2643
Anthony Liguori07501122011-08-20 22:38:31 -05002644static gpointer malloc_and_trace(gsize n_bytes)
2645{
2646 void *ptr = malloc(n_bytes);
Frediano Ziglioa74cd8c2011-08-31 09:25:35 +02002647 trace_g_malloc(n_bytes, ptr);
Anthony Liguori07501122011-08-20 22:38:31 -05002648 return ptr;
2649}
2650
2651static gpointer realloc_and_trace(gpointer mem, gsize n_bytes)
2652{
2653 void *ptr = realloc(mem, n_bytes);
Frediano Ziglioa74cd8c2011-08-31 09:25:35 +02002654 trace_g_realloc(mem, n_bytes, ptr);
Anthony Liguori07501122011-08-20 22:38:31 -05002655 return ptr;
2656}
2657
2658static void free_and_trace(gpointer mem)
2659{
Frediano Ziglioa74cd8c2011-08-31 09:25:35 +02002660 trace_g_free(mem);
Anthony Liguori07501122011-08-20 22:38:31 -05002661 free(mem);
2662}
2663
Anthony Liguori68d98d32012-06-25 14:36:33 -05002664static int object_set_property(const char *name, const char *value, void *opaque)
2665{
2666 Object *obj = OBJECT(opaque);
2667 StringInputVisitor *siv;
2668 Error *local_err = NULL;
2669
2670 if (strcmp(name, "qom-type") == 0 || strcmp(name, "id") == 0) {
2671 return 0;
2672 }
2673
2674 siv = string_input_visitor_new(value);
2675 object_property_set(obj, string_input_get_visitor(siv), name, &local_err);
2676 string_input_visitor_cleanup(siv);
2677
2678 if (local_err) {
2679 qerror_report_err(local_err);
2680 error_free(local_err);
2681 return -1;
2682 }
2683
2684 return 0;
2685}
2686
2687static int object_create(QemuOpts *opts, void *opaque)
2688{
2689 const char *type = qemu_opt_get(opts, "qom-type");
2690 const char *id = qemu_opts_id(opts);
2691 Object *obj;
2692
2693 g_assert(type != NULL);
2694
2695 if (id == NULL) {
2696 qerror_report(QERR_MISSING_PARAMETER, "id");
2697 return -1;
2698 }
2699
2700 obj = object_new(type);
2701 if (qemu_opt_foreach(opts, object_set_property, obj, 1) < 0) {
2702 return -1;
2703 }
2704
2705 object_property_add_child(container_get(object_get_root(), "/objects"),
2706 id, obj, NULL);
2707
2708 return 0;
2709}
2710
malc902b3d52008-12-10 19:18:40 +00002711int main(int argc, char **argv, char **envp)
bellard0824d6f2003-06-24 13:42:40 +00002712{
thse4bcb142007-12-02 04:51:10 +00002713 int i;
Eduardo Habkostda1fcfd2010-04-06 19:22:07 -03002714 int snapshot, linux_boot;
Paolo Bonzini4e3de9e2010-03-10 11:38:48 +01002715 const char *icount_option = NULL;
bellard7f7f9872003-10-30 01:11:23 +00002716 const char *initrd_filename;
bellarda20dd502003-09-30 21:07:02 +00002717 const char *kernel_filename, *kernel_cmdline;
Avik Sile4ada292013-01-08 12:36:30 +05302718 char boot_devices[33] = "";
aliguori3023f3322009-01-16 19:04:14 +00002719 DisplayState *ds;
malc9f227bc2012-08-27 18:33:22 +04002720 int cyls, heads, secs, translation;
2721 QemuOpts *hda_opts = NULL, *opts, *machine_opts;
Gerd Hoffmann03b0ba72010-08-20 13:52:02 +02002722 QemuOptsList *olist;
bellardcd6f1162004-05-13 22:02:20 +00002723 int optind;
Anthony Liguori6530a972010-01-22 09:18:06 -06002724 const char *optarg;
bellardd63d3072004-10-03 13:29:03 +00002725 const char *loadvm = NULL;
bellardcc1daa42005-06-05 14:49:17 +00002726 QEMUMachine *machine;
j_mayer94fc95c2007-03-05 19:44:02 +00002727 const char *cpu_model;
Paolo Bonzini7f1b17f2012-05-10 09:39:17 +02002728 const char *vga_model = "none";
ths93815bc2007-03-19 15:58:31 +00002729 const char *pid_file = NULL;
aliguori5bb79102008-10-13 03:12:02 +00002730 const char *incoming = NULL;
Jes Sorensen821601e2011-03-16 13:33:36 +01002731#ifdef CONFIG_VNC
Anthony Liguori993fbfd2009-05-21 16:54:00 -05002732 int show_vnc_port = 0;
Jes Sorensen821601e2011-03-16 13:33:36 +01002733#endif
Eduardo Habkost3ed2d9e2012-05-02 13:07:28 -03002734 bool defconfig = true;
Eduardo Habkostf29a5612012-05-02 13:07:29 -03002735 bool userconfig = true;
Matthew Fernandezc235d732011-06-07 16:32:40 +00002736 const char *log_mask = NULL;
2737 const char *log_file = NULL;
Anthony Liguori07501122011-08-20 22:38:31 -05002738 GMemVTable mem_trace = {
2739 .malloc = malloc_and_trace,
2740 .realloc = realloc_and_trace,
2741 .free = free_and_trace,
2742 };
Lluís23d15e82011-08-31 20:31:31 +02002743 const char *trace_events = NULL;
Lluíse4858972011-08-31 20:31:03 +02002744 const char *trace_file = NULL;
Stefan Hajnoczi0b5538c2011-02-26 18:38:39 +00002745
Gerd Hoffmannfd42dee2010-06-04 14:08:07 +02002746 atexit(qemu_run_exit_notifiers);
Markus Armbruster65abca02010-02-24 14:37:14 +01002747 error_set_progname(argv[0]);
2748
Anthony Liguori07501122011-08-20 22:38:31 -05002749 g_mem_set_vtable(&mem_trace);
Andreas Färberdb529aa2011-10-31 19:14:56 +01002750 if (!g_thread_supported()) {
Alon Levy42ed3722011-12-20 13:41:04 +02002751#if !GLIB_CHECK_VERSION(2, 31, 0)
Andreas Färberdb529aa2011-10-31 19:14:56 +01002752 g_thread_init(NULL);
Alon Levy42ed3722011-12-20 13:41:04 +02002753#else
2754 fprintf(stderr, "glib threading failed to initialize.\n");
2755 exit(1);
2756#endif
Andreas Färberdb529aa2011-10-31 19:14:56 +01002757 }
Anthony Liguori07501122011-08-20 22:38:31 -05002758
Andreas Färber1b71f7c2012-03-04 21:32:35 +01002759 module_call_init(MODULE_INIT_QOM);
2760
Paolo Bonzini4d454572012-11-26 16:03:42 +01002761 qemu_add_opts(&qemu_drive_opts);
2762 qemu_add_opts(&qemu_chardev_opts);
2763 qemu_add_opts(&qemu_device_opts);
2764 qemu_add_opts(&qemu_netdev_opts);
2765 qemu_add_opts(&qemu_net_opts);
2766 qemu_add_opts(&qemu_rtc_opts);
2767 qemu_add_opts(&qemu_global_opts);
2768 qemu_add_opts(&qemu_mon_opts);
2769 qemu_add_opts(&qemu_trace_opts);
2770 qemu_add_opts(&qemu_option_rom_opts);
2771 qemu_add_opts(&qemu_machine_opts);
2772 qemu_add_opts(&qemu_boot_opts);
2773 qemu_add_opts(&qemu_sandbox_opts);
2774 qemu_add_opts(&qemu_add_fd_opts);
2775 qemu_add_opts(&qemu_object_opts);
2776
Luiz Capitulino5db9d4d2011-09-05 16:36:31 -03002777 runstate_init();
2778
Jan Kiszka68752042009-09-15 13:36:04 +02002779 init_clocks();
Paolo Bonzini2ff68d02011-09-12 16:21:44 +02002780 rtc_clock = host_clock;
Jan Kiszka68752042009-09-15 13:36:04 +02002781
malc902b3d52008-12-10 19:18:40 +00002782 qemu_cache_utils_init(envp);
2783
Blue Swirl72cf2d42009-09-12 07:36:22 +00002784 QLIST_INIT (&vm_change_state_head);
Jes Sorensenfe98ac12010-06-10 11:42:21 +02002785 os_setup_early_signal_handling();
bellardbe995c22006-06-25 16:25:21 +00002786
Anthony Liguorif80f9ec2009-05-20 18:38:09 -05002787 module_call_init(MODULE_INIT_MACHINE);
Anthony Liguori0c257432009-05-21 20:41:01 -05002788 machine = find_default_machine();
j_mayer94fc95c2007-03-05 19:44:02 +00002789 cpu_model = NULL;
aurel324fc5d072008-04-27 21:39:40 +00002790 ram_size = 0;
bellard33e39632003-07-06 17:15:21 +00002791 snapshot = 0;
malc9f227bc2012-08-27 18:33:22 +04002792 cyls = heads = secs = 0;
2793 translation = BIOS_ATA_TRANSLATION_AUTO;
bellardc4b1fcc2004-03-14 21:44:30 +00002794
aliguori268a3622009-04-21 22:30:27 +00002795 for (i = 0; i < MAX_NODES; i++) {
2796 node_mem[i] = 0;
Chegu Vinodee785fe2012-07-16 21:31:30 -07002797 node_cpumask[i] = bitmap_new(MAX_CPUMASK_BITS);
aliguori268a3622009-04-21 22:30:27 +00002798 }
2799
aliguori268a3622009-04-21 22:30:27 +00002800 nb_numa_nodes = 0;
bellard7c9d8e02005-11-15 22:16:05 +00002801 nb_nics = 0;
ths3b46e622007-09-17 08:09:54 +00002802
blueswir141bd6392008-10-05 09:56:21 +00002803 autostart= 1;
2804
Anthony Liguori292444c2010-01-21 10:57:58 -06002805 /* first pass of option parsing */
2806 optind = 1;
2807 while (optind < argc) {
2808 if (argv[optind][0] != '-') {
2809 /* disk image */
Anthony Liguori28e68d62010-01-27 10:46:00 -06002810 optind++;
Anthony Liguori292444c2010-01-21 10:57:58 -06002811 continue;
2812 } else {
2813 const QEMUOption *popt;
2814
2815 popt = lookup_opt(argc, argv, &optarg, &optind);
2816 switch (popt->index) {
2817 case QEMU_OPTION_nodefconfig:
Eduardo Habkost3ed2d9e2012-05-02 13:07:28 -03002818 defconfig = false;
Anthony Liguori292444c2010-01-21 10:57:58 -06002819 break;
Eduardo Habkostf29a5612012-05-02 13:07:29 -03002820 case QEMU_OPTION_nouserconfig:
2821 userconfig = false;
2822 break;
Anthony Liguori292444c2010-01-21 10:57:58 -06002823 }
2824 }
2825 }
2826
2827 if (defconfig) {
Kevin Wolfdcfb0932010-03-05 17:25:55 +01002828 int ret;
Eduardo Habkostf29a5612012-05-02 13:07:29 -03002829 ret = qemu_read_default_config_files(userconfig);
Eduardo Habkostb5a8fe52012-05-02 13:07:25 -03002830 if (ret < 0) {
Kevin Wolfdcfb0932010-03-05 17:25:55 +01002831 exit(1);
Anthony Liguori292444c2010-01-21 10:57:58 -06002832 }
2833 }
2834
2835 /* second pass of option parsing */
bellardcd6f1162004-05-13 22:02:20 +00002836 optind = 1;
bellard0824d6f2003-06-24 13:42:40 +00002837 for(;;) {
bellardcd6f1162004-05-13 22:02:20 +00002838 if (optind >= argc)
bellard0824d6f2003-06-24 13:42:40 +00002839 break;
Anthony Liguori6530a972010-01-22 09:18:06 -06002840 if (argv[optind][0] != '-') {
malc9f227bc2012-08-27 18:33:22 +04002841 hda_opts = drive_add(IF_DEFAULT, 0, argv[optind++], HD_OPTS);
bellardcd6f1162004-05-13 22:02:20 +00002842 } else {
2843 const QEMUOption *popt;
2844
Anthony Liguori6530a972010-01-22 09:18:06 -06002845 popt = lookup_opt(argc, argv, &optarg, &optind);
Blue Swirlad960902010-03-29 19:23:52 +00002846 if (!(popt->arch_mask & arch_type)) {
2847 printf("Option %s not supported for this target\n", popt->name);
2848 exit(1);
2849 }
bellardcd6f1162004-05-13 22:02:20 +00002850 switch(popt->index) {
bellardcc1daa42005-06-05 14:49:17 +00002851 case QEMU_OPTION_M:
Jan Kiszka9052ea62011-07-23 12:38:37 +02002852 machine = machine_parse(optarg);
bellardcc1daa42005-06-05 14:49:17 +00002853 break;
Jan Kiszkae43d5942012-10-05 14:51:40 -03002854 case QEMU_OPTION_no_kvm_irqchip: {
2855 olist = qemu_find_opts("machine");
2856 qemu_opts_parse(olist, "kernel_irqchip=off", 0);
2857 break;
2858 }
j_mayer94fc95c2007-03-05 19:44:02 +00002859 case QEMU_OPTION_cpu:
2860 /* hw initialization will check this */
Eduardo Habkostecf40be2012-03-09 16:19:07 -03002861 cpu_model = optarg;
j_mayer94fc95c2007-03-05 19:44:02 +00002862 break;
bellardcd6f1162004-05-13 22:02:20 +00002863 case QEMU_OPTION_hda:
malc9f227bc2012-08-27 18:33:22 +04002864 {
2865 char buf[256];
2866 if (cyls == 0)
2867 snprintf(buf, sizeof(buf), "%s", HD_OPTS);
2868 else
2869 snprintf(buf, sizeof(buf),
2870 "%s,cyls=%d,heads=%d,secs=%d%s",
2871 HD_OPTS , cyls, heads, secs,
2872 translation == BIOS_ATA_TRANSLATION_LBA ?
2873 ",trans=lba" :
2874 translation == BIOS_ATA_TRANSLATION_NONE ?
2875 ",trans=none" : "");
2876 drive_add(IF_DEFAULT, 0, optarg, buf);
2877 break;
2878 }
bellardcd6f1162004-05-13 22:02:20 +00002879 case QEMU_OPTION_hdb:
bellardcc1daa42005-06-05 14:49:17 +00002880 case QEMU_OPTION_hdc:
2881 case QEMU_OPTION_hdd:
Markus Armbruster2292dda2011-01-28 11:21:41 +01002882 drive_add(IF_DEFAULT, popt->index - QEMU_OPTION_hda, optarg,
2883 HD_OPTS);
bellardfc01f7e2003-06-30 10:03:06 +00002884 break;
thse4bcb142007-12-02 04:51:10 +00002885 case QEMU_OPTION_drive:
Michael Tokareve2982c32011-03-30 16:31:05 +04002886 if (drive_def(optarg) == NULL) {
2887 exit(1);
2888 }
thse4bcb142007-12-02 04:51:10 +00002889 break;
Gerd Hoffmannd058fe02009-07-31 12:25:36 +02002890 case QEMU_OPTION_set:
2891 if (qemu_set_option(optarg) != 0)
2892 exit(1);
2893 break;
Gerd Hoffmannd0fef6f2009-12-08 13:11:34 +01002894 case QEMU_OPTION_global:
2895 if (qemu_global_option(optarg) != 0)
2896 exit(1);
2897 break;
balrog3e3d5812007-04-30 02:09:25 +00002898 case QEMU_OPTION_mtdblock:
Markus Armbruster2292dda2011-01-28 11:21:41 +01002899 drive_add(IF_MTD, -1, optarg, MTD_OPTS);
balrog3e3d5812007-04-30 02:09:25 +00002900 break;
pbrooka1bb27b2007-04-06 16:49:48 +00002901 case QEMU_OPTION_sd:
Markus Armbruster2292dda2011-01-28 11:21:41 +01002902 drive_add(IF_SD, 0, optarg, SD_OPTS);
pbrooka1bb27b2007-04-06 16:49:48 +00002903 break;
j_mayer86f55662007-04-24 06:52:59 +00002904 case QEMU_OPTION_pflash:
Markus Armbruster2292dda2011-01-28 11:21:41 +01002905 drive_add(IF_PFLASH, -1, optarg, PFLASH_OPTS);
j_mayer86f55662007-04-24 06:52:59 +00002906 break;
bellardcd6f1162004-05-13 22:02:20 +00002907 case QEMU_OPTION_snapshot:
bellard33e39632003-07-06 17:15:21 +00002908 snapshot = 1;
2909 break;
bellardcd6f1162004-05-13 22:02:20 +00002910 case QEMU_OPTION_hdachs:
bellard330d0412003-07-26 18:11:40 +00002911 {
bellard330d0412003-07-26 18:11:40 +00002912 const char *p;
2913 p = optarg;
2914 cyls = strtol(p, (char **)&p, 0);
bellard46d47672004-11-16 01:45:27 +00002915 if (cyls < 1 || cyls > 16383)
2916 goto chs_fail;
bellard330d0412003-07-26 18:11:40 +00002917 if (*p != ',')
2918 goto chs_fail;
2919 p++;
2920 heads = strtol(p, (char **)&p, 0);
bellard46d47672004-11-16 01:45:27 +00002921 if (heads < 1 || heads > 16)
2922 goto chs_fail;
bellard330d0412003-07-26 18:11:40 +00002923 if (*p != ',')
2924 goto chs_fail;
2925 p++;
2926 secs = strtol(p, (char **)&p, 0);
bellard46d47672004-11-16 01:45:27 +00002927 if (secs < 1 || secs > 63)
2928 goto chs_fail;
2929 if (*p == ',') {
2930 p++;
2931 if (!strcmp(p, "none"))
2932 translation = BIOS_ATA_TRANSLATION_NONE;
2933 else if (!strcmp(p, "lba"))
2934 translation = BIOS_ATA_TRANSLATION_LBA;
2935 else if (!strcmp(p, "auto"))
2936 translation = BIOS_ATA_TRANSLATION_AUTO;
2937 else
2938 goto chs_fail;
2939 } else if (*p != '\0') {
bellardc4b1fcc2004-03-14 21:44:30 +00002940 chs_fail:
bellard46d47672004-11-16 01:45:27 +00002941 fprintf(stderr, "qemu: invalid physical CHS format\n");
2942 exit(1);
bellardc4b1fcc2004-03-14 21:44:30 +00002943 }
malc9f227bc2012-08-27 18:33:22 +04002944 if (hda_opts != NULL) {
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +02002945 char num[16];
2946 snprintf(num, sizeof(num), "%d", cyls);
2947 qemu_opt_set(hda_opts, "cyls", num);
2948 snprintf(num, sizeof(num), "%d", heads);
2949 qemu_opt_set(hda_opts, "heads", num);
2950 snprintf(num, sizeof(num), "%d", secs);
2951 qemu_opt_set(hda_opts, "secs", num);
2952 if (translation == BIOS_ATA_TRANSLATION_LBA)
2953 qemu_opt_set(hda_opts, "trans", "lba");
2954 if (translation == BIOS_ATA_TRANSLATION_NONE)
2955 qemu_opt_set(hda_opts, "trans", "none");
2956 }
bellard330d0412003-07-26 18:11:40 +00002957 }
2958 break;
aliguori268a3622009-04-21 22:30:27 +00002959 case QEMU_OPTION_numa:
2960 if (nb_numa_nodes >= MAX_NODES) {
2961 fprintf(stderr, "qemu: too many NUMA nodes\n");
2962 exit(1);
2963 }
2964 numa_add(optarg);
2965 break;
Jes Sorensen1472a952011-03-16 13:33:31 +01002966 case QEMU_OPTION_display:
2967 display_type = select_display(optarg);
2968 break;
bellardcd6f1162004-05-13 22:02:20 +00002969 case QEMU_OPTION_nographic:
Anthony Liguori993fbfd2009-05-21 16:54:00 -05002970 display_type = DT_NOGRAPHIC;
bellarda20dd502003-09-30 21:07:02 +00002971 break;
balrog4d3b6f62008-02-10 16:33:14 +00002972 case QEMU_OPTION_curses:
Jes Sorensen47b05362011-03-16 13:33:35 +01002973#ifdef CONFIG_CURSES
Anthony Liguori993fbfd2009-05-21 16:54:00 -05002974 display_type = DT_CURSES;
Jes Sorensen47b05362011-03-16 13:33:35 +01002975#else
2976 fprintf(stderr, "Curses support is disabled\n");
2977 exit(1);
balrog4d3b6f62008-02-10 16:33:14 +00002978#endif
Jes Sorensen47b05362011-03-16 13:33:35 +01002979 break;
balroga171fe32007-04-30 01:48:07 +00002980 case QEMU_OPTION_portrait:
Vasily Khoruzhick93128052011-06-17 13:04:36 +03002981 graphic_rotate = 90;
2982 break;
2983 case QEMU_OPTION_rotate:
2984 graphic_rotate = strtol(optarg, (char **) &optarg, 10);
2985 if (graphic_rotate != 0 && graphic_rotate != 90 &&
2986 graphic_rotate != 180 && graphic_rotate != 270) {
2987 fprintf(stderr,
2988 "qemu: only 90, 180, 270 deg rotation is available\n");
2989 exit(1);
2990 }
balroga171fe32007-04-30 01:48:07 +00002991 break;
bellardcd6f1162004-05-13 22:02:20 +00002992 case QEMU_OPTION_kernel:
Peter Maydella0abe472012-02-08 05:41:39 +00002993 qemu_opts_set(qemu_find_opts("machine"), 0, "kernel", optarg);
2994 break;
2995 case QEMU_OPTION_initrd:
2996 qemu_opts_set(qemu_find_opts("machine"), 0, "initrd", optarg);
bellarda20dd502003-09-30 21:07:02 +00002997 break;
bellardcd6f1162004-05-13 22:02:20 +00002998 case QEMU_OPTION_append:
Peter Maydella0abe472012-02-08 05:41:39 +00002999 qemu_opts_set(qemu_find_opts("machine"), 0, "append", optarg);
bellard313aa562003-08-10 21:52:11 +00003000 break;
Grant Likely412beee2012-03-02 11:56:38 +00003001 case QEMU_OPTION_dtb:
3002 qemu_opts_set(qemu_find_opts("machine"), 0, "dtb", optarg);
3003 break;
bellardcd6f1162004-05-13 22:02:20 +00003004 case QEMU_OPTION_cdrom:
Markus Armbruster2292dda2011-01-28 11:21:41 +01003005 drive_add(IF_DEFAULT, 2, optarg, CDROM_OPTS);
bellard36b486b2003-11-11 13:36:08 +00003006 break;
bellardcd6f1162004-05-13 22:02:20 +00003007 case QEMU_OPTION_boot:
j_mayer28c5af52007-11-11 01:50:45 +00003008 {
Jan Kiszkaef3adf62009-07-02 00:19:02 +02003009 static const char * const params[] = {
wayne3d3b8302011-07-27 18:04:55 +08003010 "order", "once", "menu",
Amos Kongac05f342012-09-07 11:11:03 +08003011 "splash", "splash-time",
3012 "reboot-timeout", NULL
Jan Kiszkaef3adf62009-07-02 00:19:02 +02003013 };
3014 char buf[sizeof(boot_devices)];
Jan Kiszkae0f084b2009-07-02 00:19:02 +02003015 char *standard_boot_devices;
Jan Kiszkaef3adf62009-07-02 00:19:02 +02003016 int legacy = 0;
3017
3018 if (!strchr(optarg, '=')) {
3019 legacy = 1;
3020 pstrcpy(buf, sizeof(buf), optarg);
3021 } else if (check_params(buf, sizeof(buf), params, optarg) < 0) {
3022 fprintf(stderr,
3023 "qemu: unknown boot parameter '%s' in '%s'\n",
3024 buf, optarg);
3025 exit(1);
3026 }
3027
3028 if (legacy ||
3029 get_param_value(buf, sizeof(buf), "order", optarg)) {
Eduardo Habkost4e9e9d62010-04-06 19:22:08 -03003030 validate_bootdevices(buf);
Jan Kiszkaef3adf62009-07-02 00:19:02 +02003031 pstrcpy(boot_devices, sizeof(boot_devices), buf);
j_mayer28c5af52007-11-11 01:50:45 +00003032 }
Jan Kiszkae0f084b2009-07-02 00:19:02 +02003033 if (!legacy) {
3034 if (get_param_value(buf, sizeof(buf),
3035 "once", optarg)) {
Eduardo Habkost4e9e9d62010-04-06 19:22:08 -03003036 validate_bootdevices(buf);
Anthony Liguori7267c092011-08-20 22:09:37 -05003037 standard_boot_devices = g_strdup(boot_devices);
Jan Kiszkae0f084b2009-07-02 00:19:02 +02003038 pstrcpy(boot_devices, sizeof(boot_devices), buf);
3039 qemu_register_reset(restore_boot_devices,
3040 standard_boot_devices);
3041 }
Jan Kiszka95387492009-07-02 00:19:02 +02003042 if (get_param_value(buf, sizeof(buf),
3043 "menu", optarg)) {
3044 if (!strcmp(buf, "on")) {
3045 boot_menu = 1;
3046 } else if (!strcmp(buf, "off")) {
3047 boot_menu = 0;
3048 } else {
3049 fprintf(stderr,
3050 "qemu: invalid option value '%s'\n",
3051 buf);
3052 exit(1);
3053 }
3054 }
wayne3d3b8302011-07-27 18:04:55 +08003055 qemu_opts_parse(qemu_find_opts("boot-opts"),
3056 optarg, 0);
Jan Kiszkae0f084b2009-07-02 00:19:02 +02003057 }
bellard36b486b2003-11-11 13:36:08 +00003058 }
3059 break;
bellardcd6f1162004-05-13 22:02:20 +00003060 case QEMU_OPTION_fda:
bellardcd6f1162004-05-13 22:02:20 +00003061 case QEMU_OPTION_fdb:
Markus Armbruster2292dda2011-01-28 11:21:41 +01003062 drive_add(IF_FLOPPY, popt->index - QEMU_OPTION_fda,
3063 optarg, FD_OPTS);
bellardc45886d2004-01-05 00:02:06 +00003064 break;
bellard52ca8d62006-06-14 16:03:05 +00003065 case QEMU_OPTION_no_fd_bootchk:
3066 fd_bootchk = 0;
3067 break;
Mark McLoughlina1ea4582009-10-08 19:58:26 +01003068 case QEMU_OPTION_netdev:
Gerd Hoffmann3329f072010-08-20 13:52:01 +02003069 if (net_client_parse(qemu_find_opts("netdev"), optarg) == -1) {
Mark McLoughlina1ea4582009-10-08 19:58:26 +01003070 exit(1);
3071 }
3072 break;
bellard7c9d8e02005-11-15 22:16:05 +00003073 case QEMU_OPTION_net:
Gerd Hoffmann3329f072010-08-20 13:52:01 +02003074 if (net_client_parse(qemu_find_opts("net"), optarg) == -1) {
bellardc4b1fcc2004-03-14 21:44:30 +00003075 exit(1);
3076 }
bellard702c6512004-04-02 21:21:32 +00003077 break;
Ronnie Sahlbergf9dadc92012-01-26 09:39:02 +11003078#ifdef CONFIG_LIBISCSI
3079 case QEMU_OPTION_iscsi:
3080 opts = qemu_opts_parse(qemu_find_opts("iscsi"), optarg, 0);
3081 if (!opts) {
3082 exit(1);
3083 }
3084 break;
3085#endif
bellardc7f74642004-08-24 21:57:12 +00003086#ifdef CONFIG_SLIRP
3087 case QEMU_OPTION_tftp:
Jan Kiszkaad196a92009-06-24 14:42:28 +02003088 legacy_tftp_prefix = optarg;
bellard9bf05442004-08-25 22:12:49 +00003089 break;
ths47d5d012007-02-20 00:05:08 +00003090 case QEMU_OPTION_bootp:
Jan Kiszkaad196a92009-06-24 14:42:28 +02003091 legacy_bootp_filename = optarg;
ths47d5d012007-02-20 00:05:08 +00003092 break;
bellard9bf05442004-08-25 22:12:49 +00003093 case QEMU_OPTION_redir:
Markus Armbruster07527062009-10-06 12:16:57 +01003094 if (net_slirp_redir(optarg) < 0)
3095 exit(1);
bellard9bf05442004-08-25 22:12:49 +00003096 break;
bellardc7f74642004-08-24 21:57:12 +00003097#endif
balrogdc72ac12008-11-09 00:04:26 +00003098 case QEMU_OPTION_bt:
Gerd Hoffmannbd3c9482009-07-15 13:59:26 +02003099 add_device_config(DEV_BT, optarg);
balrogdc72ac12008-11-09 00:04:26 +00003100 break;
bellard1d14ffa2005-10-30 18:58:22 +00003101 case QEMU_OPTION_audio_help:
Blue Swirlad960902010-03-29 19:23:52 +00003102 if (!(audio_available())) {
3103 printf("Option %s not supported for this target\n", popt->name);
3104 exit(1);
3105 }
bellard1d14ffa2005-10-30 18:58:22 +00003106 AUD_help ();
3107 exit (0);
3108 break;
3109 case QEMU_OPTION_soundhw:
Blue Swirlad960902010-03-29 19:23:52 +00003110 if (!(audio_available())) {
3111 printf("Option %s not supported for this target\n", popt->name);
3112 exit(1);
3113 }
bellard1d14ffa2005-10-30 18:58:22 +00003114 select_soundhw (optarg);
3115 break;
bellardcd6f1162004-05-13 22:02:20 +00003116 case QEMU_OPTION_h:
ths15f82202007-06-29 23:26:08 +00003117 help(0);
bellardcd6f1162004-05-13 22:02:20 +00003118 break;
pbrook9bd7e6d2009-04-07 22:58:45 +00003119 case QEMU_OPTION_version:
3120 version();
3121 exit(0);
3122 break;
aurel3200f82b82008-04-27 21:12:55 +00003123 case QEMU_OPTION_m: {
Jes Sorensen70b4f4b2011-01-05 11:41:02 +01003124 int64_t value;
Markus Armbrusterff961012012-08-15 13:12:19 +02003125 uint64_t sz;
Markus Armbruster961b42b2011-11-22 09:46:03 +01003126 char *end;
aurel3200f82b82008-04-27 21:12:55 +00003127
Markus Armbruster961b42b2011-11-22 09:46:03 +01003128 value = strtosz(optarg, &end);
3129 if (value < 0 || *end) {
aurel3200f82b82008-04-27 21:12:55 +00003130 fprintf(stderr, "qemu: invalid ram size: %s\n", optarg);
bellardcd6f1162004-05-13 22:02:20 +00003131 exit(1);
3132 }
Markus Armbrusterff961012012-08-15 13:12:19 +02003133 sz = QEMU_ALIGN_UP((uint64_t)value, 8192);
3134 ram_size = sz;
3135 if (ram_size != sz) {
aurel3200f82b82008-04-27 21:12:55 +00003136 fprintf(stderr, "qemu: ram size too large\n");
3137 exit(1);
3138 }
bellardcd6f1162004-05-13 22:02:20 +00003139 break;
aurel3200f82b82008-04-27 21:12:55 +00003140 }
Marcelo Tosattic9027602010-03-01 20:25:08 -03003141 case QEMU_OPTION_mempath:
3142 mem_path = optarg;
3143 break;
3144#ifdef MAP_POPULATE
3145 case QEMU_OPTION_mem_prealloc:
3146 mem_prealloc = 1;
3147 break;
3148#endif
bellardcd6f1162004-05-13 22:02:20 +00003149 case QEMU_OPTION_d:
Matthew Fernandezc235d732011-06-07 16:32:40 +00003150 log_mask = optarg;
3151 break;
3152 case QEMU_OPTION_D:
3153 log_file = optarg;
bellardcd6f1162004-05-13 22:02:20 +00003154 break;
bellardcd6f1162004-05-13 22:02:20 +00003155 case QEMU_OPTION_s:
Markus Armbrusteref0c4a02012-02-07 15:09:13 +01003156 add_device_config(DEV_GDB, "tcp::" DEFAULT_GDBSTUB_PORT);
bellardcd6f1162004-05-13 22:02:20 +00003157 break;
aliguori59030a82009-04-05 18:43:41 +00003158 case QEMU_OPTION_gdb:
Markus Armbrusteref0c4a02012-02-07 15:09:13 +01003159 add_device_config(DEV_GDB, optarg);
bellardcd6f1162004-05-13 22:02:20 +00003160 break;
bellardcd6f1162004-05-13 22:02:20 +00003161 case QEMU_OPTION_L:
Paul Brook5cea8592009-05-30 00:52:44 +01003162 data_dir = optarg;
bellardcd6f1162004-05-13 22:02:20 +00003163 break;
j_mayer1192dad2007-10-05 13:08:35 +00003164 case QEMU_OPTION_bios:
3165 bios_name = optarg;
3166 break;
aurel321b530a62009-04-05 20:08:59 +00003167 case QEMU_OPTION_singlestep:
3168 singlestep = 1;
3169 break;
bellardcd6f1162004-05-13 22:02:20 +00003170 case QEMU_OPTION_S:
pbrook3c07f8e2007-01-21 16:47:01 +00003171 autostart = 0;
bellardcd6f1162004-05-13 22:02:20 +00003172 break;
bellard3d11d0e2004-12-12 16:56:30 +00003173 case QEMU_OPTION_k:
3174 keyboard_layout = optarg;
3175 break;
bellardee22c2f2004-06-03 12:49:50 +00003176 case QEMU_OPTION_localtime:
3177 rtc_utc = 0;
3178 break;
malc3893c122008-09-28 00:42:05 +00003179 case QEMU_OPTION_vga:
Blue Swirla369da52011-09-27 19:15:42 +00003180 vga_model = optarg;
Paolo Bonzini7f1b17f2012-05-10 09:39:17 +02003181 default_vga = 0;
bellard1bfe8562004-07-08 21:17:50 +00003182 break;
bellarde9b137c2004-06-21 16:46:10 +00003183 case QEMU_OPTION_g:
3184 {
3185 const char *p;
3186 int w, h, depth;
3187 p = optarg;
3188 w = strtol(p, (char **)&p, 10);
3189 if (w <= 0) {
3190 graphic_error:
3191 fprintf(stderr, "qemu: invalid resolution or depth\n");
3192 exit(1);
3193 }
3194 if (*p != 'x')
3195 goto graphic_error;
3196 p++;
3197 h = strtol(p, (char **)&p, 10);
3198 if (h <= 0)
3199 goto graphic_error;
3200 if (*p == 'x') {
3201 p++;
3202 depth = strtol(p, (char **)&p, 10);
ths5fafdf22007-09-16 21:08:06 +00003203 if (depth != 8 && depth != 15 && depth != 16 &&
bellarde9b137c2004-06-21 16:46:10 +00003204 depth != 24 && depth != 32)
3205 goto graphic_error;
3206 } else if (*p == '\0') {
3207 depth = graphic_depth;
3208 } else {
3209 goto graphic_error;
3210 }
ths3b46e622007-09-17 08:09:54 +00003211
bellarde9b137c2004-06-21 16:46:10 +00003212 graphic_width = w;
3213 graphic_height = h;
3214 graphic_depth = depth;
3215 }
3216 break;
ths20d8a3e2007-02-18 17:04:49 +00003217 case QEMU_OPTION_echr:
3218 {
3219 char *r;
3220 term_escape_char = strtol(optarg, &r, 0);
3221 if (r == optarg)
3222 printf("Bad argument to echr\n");
3223 break;
3224 }
bellard82c643f2004-07-14 17:28:13 +00003225 case QEMU_OPTION_monitor:
Gerd Hoffmann6ca55822009-12-08 13:11:52 +01003226 monitor_parse(optarg, "readline");
3227 default_monitor = 0;
3228 break;
3229 case QEMU_OPTION_qmp:
3230 monitor_parse(optarg, "control");
Anthony Liguori2d114dc2010-03-21 14:14:38 -05003231 default_monitor = 0;
bellard82c643f2004-07-14 17:28:13 +00003232 break;
Gerd Hoffmann22a0e042009-12-08 13:11:51 +01003233 case QEMU_OPTION_mon:
Gerd Hoffmann3329f072010-08-20 13:52:01 +02003234 opts = qemu_opts_parse(qemu_find_opts("mon"), optarg, 1);
Gerd Hoffmann22a0e042009-12-08 13:11:51 +01003235 if (!opts) {
Gerd Hoffmann22a0e042009-12-08 13:11:51 +01003236 exit(1);
3237 }
Anthony Liguori2d114dc2010-03-21 14:14:38 -05003238 default_monitor = 0;
Gerd Hoffmann22a0e042009-12-08 13:11:51 +01003239 break;
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003240 case QEMU_OPTION_chardev:
Gerd Hoffmann3329f072010-08-20 13:52:01 +02003241 opts = qemu_opts_parse(qemu_find_opts("chardev"), optarg, 1);
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003242 if (!opts) {
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003243 exit(1);
3244 }
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003245 break;
Gautham R Shenoy74db9202010-04-29 17:44:43 +05303246 case QEMU_OPTION_fsdev:
Gerd Hoffmann03b0ba72010-08-20 13:52:02 +02003247 olist = qemu_find_opts("fsdev");
3248 if (!olist) {
3249 fprintf(stderr, "fsdev is not supported by this qemu build.\n");
3250 exit(1);
3251 }
3252 opts = qemu_opts_parse(olist, optarg, 1);
Gautham R Shenoy74db9202010-04-29 17:44:43 +05303253 if (!opts) {
3254 fprintf(stderr, "parse error: %s\n", optarg);
3255 exit(1);
3256 }
3257 break;
Gautham R Shenoy3d54abc2010-04-29 17:45:03 +05303258 case QEMU_OPTION_virtfs: {
Stefan Hajnoczie14ea472011-03-16 08:31:43 +00003259 QemuOpts *fsdev;
3260 QemuOpts *device;
M. Mohan Kumar84a87cc2011-12-14 13:58:47 +05303261 const char *writeout, *sock_fd, *socket;
Gautham R Shenoy3d54abc2010-04-29 17:45:03 +05303262
Gerd Hoffmann03b0ba72010-08-20 13:52:02 +02003263 olist = qemu_find_opts("virtfs");
3264 if (!olist) {
3265 fprintf(stderr, "virtfs is not supported by this qemu build.\n");
3266 exit(1);
3267 }
3268 opts = qemu_opts_parse(olist, optarg, 1);
Gautham R Shenoy3d54abc2010-04-29 17:45:03 +05303269 if (!opts) {
3270 fprintf(stderr, "parse error: %s\n", optarg);
3271 exit(1);
3272 }
3273
Aneesh Kumar K.Vfbcbf102011-10-13 12:28:04 +05303274 if (qemu_opt_get(opts, "fsdriver") == NULL ||
Aneesh Kumar K.V99519f02011-12-14 13:48:59 +05303275 qemu_opt_get(opts, "mount_tag") == NULL) {
3276 fprintf(stderr, "Usage: -virtfs fsdriver,mount_tag=tag.\n");
Venkateswararao Jujjuri (JV)9ce56db2010-06-14 13:34:40 -07003277 exit(1);
3278 }
Stefan Hajnoczie14ea472011-03-16 08:31:43 +00003279 fsdev = qemu_opts_create(qemu_find_opts("fsdev"),
Luiz Capitulino8be7e7e2012-03-20 15:51:57 -03003280 qemu_opt_get(opts, "mount_tag"),
3281 1, NULL);
Stefan Hajnoczie14ea472011-03-16 08:31:43 +00003282 if (!fsdev) {
3283 fprintf(stderr, "duplicate fsdev id: %s\n",
3284 qemu_opt_get(opts, "mount_tag"));
Gautham R Shenoy3d54abc2010-04-29 17:45:03 +05303285 exit(1);
3286 }
Aneesh Kumar K.Vd3ab98e2011-10-12 19:11:23 +05303287
3288 writeout = qemu_opt_get(opts, "writeout");
3289 if (writeout) {
3290#ifdef CONFIG_SYNC_FILE_RANGE
3291 qemu_opt_set(fsdev, "writeout", writeout);
3292#else
3293 fprintf(stderr, "writeout=immediate not supported on "
3294 "this platform\n");
3295 exit(1);
3296#endif
3297 }
Aneesh Kumar K.Vfbcbf102011-10-13 12:28:04 +05303298 qemu_opt_set(fsdev, "fsdriver", qemu_opt_get(opts, "fsdriver"));
Stefan Hajnoczie14ea472011-03-16 08:31:43 +00003299 qemu_opt_set(fsdev, "path", qemu_opt_get(opts, "path"));
3300 qemu_opt_set(fsdev, "security_model",
3301 qemu_opt_get(opts, "security_model"));
M. Mohan Kumar84a87cc2011-12-14 13:58:47 +05303302 socket = qemu_opt_get(opts, "socket");
3303 if (socket) {
3304 qemu_opt_set(fsdev, "socket", socket);
3305 }
M. Mohan Kumar4c793dd2011-12-14 13:49:28 +05303306 sock_fd = qemu_opt_get(opts, "sock_fd");
3307 if (sock_fd) {
3308 qemu_opt_set(fsdev, "sock_fd", sock_fd);
3309 }
Gautham R Shenoy3d54abc2010-04-29 17:45:03 +05303310
M. Mohan Kumar2c74c2c2011-10-25 12:10:39 +05303311 qemu_opt_set_bool(fsdev, "readonly",
3312 qemu_opt_get_bool(opts, "readonly", 0));
Dong Xu Wange478b442012-12-06 14:47:22 +08003313 device = qemu_opts_create_nofail(qemu_find_opts("device"));
Stefan Hajnoczie14ea472011-03-16 08:31:43 +00003314 qemu_opt_set(device, "driver", "virtio-9p-pci");
3315 qemu_opt_set(device, "fsdev",
3316 qemu_opt_get(opts, "mount_tag"));
3317 qemu_opt_set(device, "mount_tag",
3318 qemu_opt_get(opts, "mount_tag"));
Gautham R Shenoy3d54abc2010-04-29 17:45:03 +05303319 break;
3320 }
Aneesh Kumar K.V9db221a2011-10-25 12:10:40 +05303321 case QEMU_OPTION_virtfs_synth: {
3322 QemuOpts *fsdev;
3323 QemuOpts *device;
3324
Luiz Capitulino8be7e7e2012-03-20 15:51:57 -03003325 fsdev = qemu_opts_create(qemu_find_opts("fsdev"), "v_synth",
3326 1, NULL);
Aneesh Kumar K.V9db221a2011-10-25 12:10:40 +05303327 if (!fsdev) {
3328 fprintf(stderr, "duplicate option: %s\n", "virtfs_synth");
3329 exit(1);
3330 }
3331 qemu_opt_set(fsdev, "fsdriver", "synth");
Aneesh Kumar K.V9db221a2011-10-25 12:10:40 +05303332
Dong Xu Wange478b442012-12-06 14:47:22 +08003333 device = qemu_opts_create_nofail(qemu_find_opts("device"));
Aneesh Kumar K.V9db221a2011-10-25 12:10:40 +05303334 qemu_opt_set(device, "driver", "virtio-9p-pci");
3335 qemu_opt_set(device, "fsdev", "v_synth");
3336 qemu_opt_set(device, "mount_tag", "v_synth");
3337 break;
3338 }
bellard82c643f2004-07-14 17:28:13 +00003339 case QEMU_OPTION_serial:
Gerd Hoffmann998bbd72009-12-08 13:11:41 +01003340 add_device_config(DEV_SERIAL, optarg);
3341 default_serial = 0;
Jan Kiszka18141ed2010-03-07 11:28:40 +01003342 if (strncmp(optarg, "mon:", 4) == 0) {
3343 default_monitor = 0;
3344 }
bellard82c643f2004-07-14 17:28:13 +00003345 break;
Richard W.M. Jones9dd986c2009-04-25 13:56:19 +01003346 case QEMU_OPTION_watchdog:
Markus Armbruster09aaa162009-08-21 10:31:34 +02003347 if (watchdog) {
3348 fprintf(stderr,
3349 "qemu: only one watchdog option may be given\n");
3350 return 1;
3351 }
3352 watchdog = optarg;
Richard W.M. Jones9dd986c2009-04-25 13:56:19 +01003353 break;
3354 case QEMU_OPTION_watchdog_action:
3355 if (select_watchdog_action(optarg) == -1) {
3356 fprintf(stderr, "Unknown -watchdog-action parameter\n");
3357 exit(1);
3358 }
3359 break;
aliguori51ecf132009-01-15 20:06:40 +00003360 case QEMU_OPTION_virtiocon:
Gerd Hoffmannaee1b932009-12-08 13:11:53 +01003361 add_device_config(DEV_VIRTCON, optarg);
3362 default_virtcon = 0;
Jan Kiszka18141ed2010-03-07 11:28:40 +01003363 if (strncmp(optarg, "mon:", 4) == 0) {
3364 default_monitor = 0;
3365 }
aliguori51ecf132009-01-15 20:06:40 +00003366 break;
bellard6508fe52005-01-15 12:02:56 +00003367 case QEMU_OPTION_parallel:
Gerd Hoffmann6a5e8b02009-12-08 13:11:42 +01003368 add_device_config(DEV_PARALLEL, optarg);
3369 default_parallel = 0;
Jan Kiszka18141ed2010-03-07 11:28:40 +01003370 if (strncmp(optarg, "mon:", 4) == 0) {
3371 default_monitor = 0;
3372 }
bellard6508fe52005-01-15 12:02:56 +00003373 break;
H. Peter Anvinc9f398e2009-12-29 13:51:36 -08003374 case QEMU_OPTION_debugcon:
3375 add_device_config(DEV_DEBUGCON, optarg);
3376 break;
bellardd63d3072004-10-03 13:29:03 +00003377 case QEMU_OPTION_loadvm:
3378 loadvm = optarg;
3379 break;
3380 case QEMU_OPTION_full_screen:
3381 full_screen = 1;
3382 break;
ths667acca2006-12-11 02:08:05 +00003383#ifdef CONFIG_SDL
ths43523e92007-02-18 18:19:32 +00003384 case QEMU_OPTION_no_frame:
3385 no_frame = 1;
3386 break;
ths3780e192007-06-21 21:08:02 +00003387 case QEMU_OPTION_alt_grab:
3388 alt_grab = 1;
3389 break;
Dustin Kirkland0ca9f8a2009-09-17 15:48:04 -05003390 case QEMU_OPTION_ctrl_grab:
3391 ctrl_grab = 1;
3392 break;
ths667acca2006-12-11 02:08:05 +00003393 case QEMU_OPTION_no_quit:
3394 no_quit = 1;
3395 break;
aliguori7d957bd2009-01-15 22:14:11 +00003396 case QEMU_OPTION_sdl:
Anthony Liguori993fbfd2009-05-21 16:54:00 -05003397 display_type = DT_SDL;
aliguori7d957bd2009-01-15 22:14:11 +00003398 break;
Jes Sorensen58fc0962011-03-16 13:33:34 +01003399#else
3400 case QEMU_OPTION_no_frame:
3401 case QEMU_OPTION_alt_grab:
3402 case QEMU_OPTION_ctrl_grab:
3403 case QEMU_OPTION_no_quit:
3404 case QEMU_OPTION_sdl:
3405 fprintf(stderr, "SDL support is disabled\n");
3406 exit(1);
ths667acca2006-12-11 02:08:05 +00003407#endif
bellardf7cce892004-12-08 22:21:25 +00003408 case QEMU_OPTION_pidfile:
ths93815bc2007-03-19 15:58:31 +00003409 pid_file = optarg;
bellardf7cce892004-12-08 22:21:25 +00003410 break;
bellarda09db212005-04-30 16:10:35 +00003411 case QEMU_OPTION_win2k_hack:
3412 win2k_install_hack = 1;
3413 break;
Jan Kiszka433acf02012-01-23 20:15:12 +01003414 case QEMU_OPTION_rtc_td_hack: {
3415 static GlobalProperty slew_lost_ticks[] = {
3416 {
3417 .driver = "mc146818rtc",
3418 .property = "lost_tick_policy",
3419 .value = "slew",
3420 },
3421 { /* end of list */ }
3422 };
3423
3424 qdev_prop_register_global_list(slew_lost_ticks);
aliguori73822ec2009-01-15 20:11:34 +00003425 break;
Jan Kiszka433acf02012-01-23 20:15:12 +01003426 }
aliguori8a92ea22009-02-27 20:12:36 +00003427 case QEMU_OPTION_acpitable:
Blue Swirlde06f8d2010-03-29 19:23:50 +00003428 do_acpitable_option(optarg);
aliguori8a92ea22009-02-27 20:12:36 +00003429 break;
aliguorib6f6e3d2009-04-17 18:59:56 +00003430 case QEMU_OPTION_smbios:
Blue Swirlde06f8d2010-03-29 19:23:50 +00003431 do_smbios_option(optarg);
aliguorib6f6e3d2009-04-17 18:59:56 +00003432 break;
aliguori7ba1e612008-11-05 16:04:33 +00003433 case QEMU_OPTION_enable_kvm:
Anthony PERARD303d4e82010-09-21 20:05:31 +01003434 olist = qemu_find_opts("machine");
Anthony PERARD303d4e82010-09-21 20:05:31 +01003435 qemu_opts_parse(olist, "accel=kvm", 0);
3436 break;
3437 case QEMU_OPTION_machine:
3438 olist = qemu_find_opts("machine");
Jan Kiszka9052ea62011-07-23 12:38:37 +02003439 opts = qemu_opts_parse(olist, optarg, 1);
Anthony PERARD303d4e82010-09-21 20:05:31 +01003440 if (!opts) {
3441 fprintf(stderr, "parse error: %s\n", optarg);
3442 exit(1);
3443 }
Jan Kiszka2645c6d2011-07-25 18:11:20 +02003444 optarg = qemu_opt_get(opts, "type");
3445 if (optarg) {
3446 machine = machine_parse(optarg);
3447 }
aliguori7ba1e612008-11-05 16:04:33 +00003448 break;
Jan Kiszkaa0dac022012-10-05 14:51:45 -03003449 case QEMU_OPTION_no_kvm:
3450 olist = qemu_find_opts("machine");
3451 qemu_opts_parse(olist, "accel=tcg", 0);
3452 break;
Jan Kiszka4086bde2012-10-05 14:51:41 -03003453 case QEMU_OPTION_no_kvm_pit: {
3454 fprintf(stderr, "Warning: KVM PIT can no longer be disabled "
3455 "separately.\n");
3456 break;
3457 }
Jan Kiszkac21fb4f2012-10-05 14:51:42 -03003458 case QEMU_OPTION_no_kvm_pit_reinjection: {
3459 static GlobalProperty kvm_pit_lost_tick_policy[] = {
3460 {
3461 .driver = "kvm-pit",
3462 .property = "lost_tick_policy",
3463 .value = "discard",
3464 },
3465 { /* end of list */ }
3466 };
3467
3468 fprintf(stderr, "Warning: option deprecated, use "
3469 "lost_tick_policy property of kvm-pit instead.\n");
3470 qdev_prop_register_global_list(kvm_pit_lost_tick_policy);
3471 break;
3472 }
bellardbb36d472005-11-05 14:22:28 +00003473 case QEMU_OPTION_usb:
Peter Maydellfa5358c2012-11-22 16:48:45 +00003474 olist = qemu_find_opts("machine");
3475 qemu_opts_parse(olist, "usb=on", 0);
bellardbb36d472005-11-05 14:22:28 +00003476 break;
bellarda594cfb2005-11-06 16:13:29 +00003477 case QEMU_OPTION_usbdevice:
Peter Maydellfa5358c2012-11-22 16:48:45 +00003478 olist = qemu_find_opts("machine");
3479 qemu_opts_parse(olist, "usb=on", 0);
Gerd Hoffmannbd3c9482009-07-15 13:59:26 +02003480 add_device_config(DEV_USB, optarg);
3481 break;
3482 case QEMU_OPTION_device:
Gerd Hoffmann3329f072010-08-20 13:52:01 +02003483 if (!qemu_opts_parse(qemu_find_opts("device"), optarg, 1)) {
Gerd Hoffmannf31d07d2009-07-31 12:25:37 +02003484 exit(1);
3485 }
bellarda594cfb2005-11-06 16:13:29 +00003486 break;
bellard6a00d602005-11-21 23:25:50 +00003487 case QEMU_OPTION_smp:
Andre Przywaradc6b1c02009-08-19 15:42:40 +02003488 smp_parse(optarg);
aliguorib2097002008-10-07 20:39:39 +00003489 if (smp_cpus < 1) {
bellard6a00d602005-11-21 23:25:50 +00003490 fprintf(stderr, "Invalid number of CPUs\n");
3491 exit(1);
3492 }
Jes Sorensen6be68d72009-07-23 17:03:42 +02003493 if (max_cpus < smp_cpus) {
3494 fprintf(stderr, "maxcpus must be equal to or greater than "
3495 "smp\n");
3496 exit(1);
3497 }
3498 if (max_cpus > 255) {
3499 fprintf(stderr, "Unsupported number of maxcpus\n");
3500 exit(1);
3501 }
bellard6a00d602005-11-21 23:25:50 +00003502 break;
bellard24236862006-04-30 21:28:36 +00003503 case QEMU_OPTION_vnc:
Jes Sorensen821601e2011-03-16 13:33:36 +01003504#ifdef CONFIG_VNC
Gerd Hoffmann6b62dc22010-08-25 10:51:06 +02003505 display_remote++;
Jes Sorensen821601e2011-03-16 13:33:36 +01003506 vnc_display = optarg;
3507#else
3508 fprintf(stderr, "VNC support is disabled\n");
3509 exit(1);
3510#endif
3511 break;
bellard6515b202006-05-03 22:02:44 +00003512 case QEMU_OPTION_no_acpi:
3513 acpi_enabled = 0;
3514 break;
aliguori16b29ae2008-12-17 23:28:44 +00003515 case QEMU_OPTION_no_hpet:
3516 no_hpet = 1;
3517 break;
Markus Armbruster7d4c3d52009-06-26 19:15:14 +02003518 case QEMU_OPTION_balloon:
3519 if (balloon_parse(optarg) < 0) {
3520 fprintf(stderr, "Unknown -balloon argument %s\n", optarg);
3521 exit(1);
3522 }
Eduardo Habkostdf97b922009-06-10 16:34:08 -03003523 break;
bellardd1beab82006-10-02 19:44:22 +00003524 case QEMU_OPTION_no_reboot:
3525 no_reboot = 1;
3526 break;
aurel32b2f76162008-04-11 21:35:52 +00003527 case QEMU_OPTION_no_shutdown:
3528 no_shutdown = 1;
3529 break;
balrog9467cd42007-05-01 01:34:14 +00003530 case QEMU_OPTION_show_cursor:
3531 cursor_hide = 0;
3532 break;
blueswir18fcb1b92008-09-18 18:29:08 +00003533 case QEMU_OPTION_uuid:
3534 if(qemu_uuid_parse(optarg, qemu_uuid) < 0) {
3535 fprintf(stderr, "Fail to parse UUID string."
3536 " Wrong format.\n");
3537 exit(1);
3538 }
3539 break;
ths9ae02552007-01-05 17:39:04 +00003540 case QEMU_OPTION_option_rom:
3541 if (nb_option_roms >= MAX_OPTION_ROMS) {
3542 fprintf(stderr, "Too many option ROMs\n");
3543 exit(1);
3544 }
Gleb Natapov2e55e842010-12-08 13:35:07 +02003545 opts = qemu_opts_parse(qemu_find_opts("option-rom"), optarg, 1);
3546 option_rom[nb_option_roms].name = qemu_opt_get(opts, "romfile");
3547 option_rom[nb_option_roms].bootindex =
3548 qemu_opt_get_number(opts, "bootindex", -1);
3549 if (!option_rom[nb_option_roms].name) {
3550 fprintf(stderr, "Option ROM file is not specified\n");
3551 exit(1);
3552 }
ths9ae02552007-01-05 17:39:04 +00003553 nb_option_roms++;
3554 break;
pbrook8e716212007-01-20 17:12:09 +00003555 case QEMU_OPTION_semihosting:
3556 semihosting_enabled = 1;
3557 break;
Jan Kiszka88eed342012-10-05 14:51:44 -03003558 case QEMU_OPTION_tdf:
3559 fprintf(stderr, "Warning: user space PIT time drift fix "
3560 "is no longer supported.\n");
3561 break;
thsc35734b2007-03-19 15:17:08 +00003562 case QEMU_OPTION_name:
Anthony Liguori7267c092011-08-20 22:09:37 -05003563 qemu_name = g_strdup(optarg);
Andi Kleen18894652009-07-02 09:34:17 +02003564 {
3565 char *p = strchr(qemu_name, ',');
3566 if (p != NULL) {
3567 *p++ = 0;
3568 if (strncmp(p, "process=", 8)) {
Aurelien Jarno5697f6a2010-12-27 18:29:20 +01003569 fprintf(stderr, "Unknown subargument %s to -name\n", p);
Andi Kleen18894652009-07-02 09:34:17 +02003570 exit(1);
3571 }
3572 p += 8;
Jes Sorensence798cf2010-06-10 11:42:31 +02003573 os_set_proc_name(p);
Andi Kleen18894652009-07-02 09:34:17 +02003574 }
3575 }
thsc35734b2007-03-19 15:17:08 +00003576 break;
blueswir166508602007-05-01 14:16:52 +00003577 case QEMU_OPTION_prom_env:
3578 if (nb_prom_envs >= MAX_PROM_ENVS) {
3579 fprintf(stderr, "Too many prom variables\n");
3580 exit(1);
3581 }
3582 prom_envs[nb_prom_envs] = optarg;
3583 nb_prom_envs++;
3584 break;
balrog2b8f2d42007-07-27 22:08:46 +00003585 case QEMU_OPTION_old_param:
3586 old_param = 1;
ths05ebd532008-01-08 19:32:16 +00003587 break;
thsf3dcfad2007-08-24 01:26:02 +00003588 case QEMU_OPTION_clock:
3589 configure_alarms(optarg);
3590 break;
bellard7e0af5d02007-11-07 16:24:33 +00003591 case QEMU_OPTION_startdate:
Jan Kiszka1ed2fc12009-09-15 13:36:04 +02003592 configure_rtc_date_offset(optarg, 1);
3593 break;
3594 case QEMU_OPTION_rtc:
Gerd Hoffmann3329f072010-08-20 13:52:01 +02003595 opts = qemu_opts_parse(qemu_find_opts("rtc"), optarg, 0);
Jan Kiszka1ed2fc12009-09-15 13:36:04 +02003596 if (!opts) {
Jan Kiszka1ed2fc12009-09-15 13:36:04 +02003597 exit(1);
bellard7e0af5d02007-11-07 16:24:33 +00003598 }
Jan Kiszka1ed2fc12009-09-15 13:36:04 +02003599 configure_rtc(opts);
bellard7e0af5d02007-11-07 16:24:33 +00003600 break;
bellard26a5f132008-05-28 12:30:31 +00003601 case QEMU_OPTION_tb_size:
Jan Kiszkad5ab9712011-08-02 16:10:21 +02003602 tcg_tb_size = strtol(optarg, NULL, 0);
3603 if (tcg_tb_size < 0) {
3604 tcg_tb_size = 0;
3605 }
bellard26a5f132008-05-28 12:30:31 +00003606 break;
pbrook2e70f6e2008-06-29 01:03:05 +00003607 case QEMU_OPTION_icount:
Paolo Bonzini4e3de9e2010-03-10 11:38:48 +01003608 icount_option = optarg;
pbrook2e70f6e2008-06-29 01:03:05 +00003609 break;
aliguori5bb79102008-10-13 03:12:02 +00003610 case QEMU_OPTION_incoming:
3611 incoming = optarg;
Stefano Stabellini81323a62012-01-18 12:23:13 +00003612 runstate_set(RUN_STATE_INMIGRATE);
aliguori5bb79102008-10-13 03:12:02 +00003613 break;
Gerd Hoffmannd8c208d2009-12-08 13:11:46 +01003614 case QEMU_OPTION_nodefaults:
3615 default_serial = 0;
3616 default_parallel = 0;
Gerd Hoffmannaee1b932009-12-08 13:11:53 +01003617 default_virtcon = 0;
Gerd Hoffmannd8c208d2009-12-08 13:11:46 +01003618 default_monitor = 0;
Gerd Hoffmanncb4522c2009-12-08 13:11:47 +01003619 default_net = 0;
Gerd Hoffmannac33f8f2009-12-16 14:25:39 +01003620 default_floppy = 0;
3621 default_cdrom = 0;
3622 default_sdcard = 0;
Paolo Bonzini7f1b17f2012-05-10 09:39:17 +02003623 default_vga = 0;
Gerd Hoffmannd8c208d2009-12-08 13:11:46 +01003624 break;
aliguorie37630c2009-04-22 15:19:10 +00003625 case QEMU_OPTION_xen_domid:
Blue Swirlad960902010-03-29 19:23:52 +00003626 if (!(xen_available())) {
3627 printf("Option %s not supported for this target\n", popt->name);
3628 exit(1);
3629 }
aliguorie37630c2009-04-22 15:19:10 +00003630 xen_domid = atoi(optarg);
3631 break;
3632 case QEMU_OPTION_xen_create:
Blue Swirlad960902010-03-29 19:23:52 +00003633 if (!(xen_available())) {
3634 printf("Option %s not supported for this target\n", popt->name);
3635 exit(1);
3636 }
aliguorie37630c2009-04-22 15:19:10 +00003637 xen_mode = XEN_CREATE;
3638 break;
3639 case QEMU_OPTION_xen_attach:
Blue Swirlad960902010-03-29 19:23:52 +00003640 if (!(xen_available())) {
3641 printf("Option %s not supported for this target\n", popt->name);
3642 exit(1);
3643 }
aliguorie37630c2009-04-22 15:19:10 +00003644 xen_mode = XEN_ATTACH;
3645 break;
Prerna Saxenaab6540d2010-08-09 11:48:32 +01003646 case QEMU_OPTION_trace:
Lluíse4858972011-08-31 20:31:03 +02003647 {
Prerna Saxenaab6540d2010-08-09 11:48:32 +01003648 opts = qemu_opts_parse(qemu_find_opts("trace"), optarg, 0);
Lluíse4858972011-08-31 20:31:03 +02003649 if (!opts) {
3650 exit(1);
Prerna Saxenaab6540d2010-08-09 11:48:32 +01003651 }
Lluís23d15e82011-08-31 20:31:31 +02003652 trace_events = qemu_opt_get(opts, "events");
Lluíse4858972011-08-31 20:31:03 +02003653 trace_file = qemu_opt_get(opts, "file");
Prerna Saxenaab6540d2010-08-09 11:48:32 +01003654 break;
Lluíse4858972011-08-31 20:31:03 +02003655 }
Gerd Hoffmann715a6642009-10-14 10:39:28 +02003656 case QEMU_OPTION_readconfig:
3657 {
Kevin Wolfdcfb0932010-03-05 17:25:55 +01003658 int ret = qemu_read_config_file(optarg);
3659 if (ret < 0) {
3660 fprintf(stderr, "read config %s: %s\n", optarg,
3661 strerror(-ret));
Gerd Hoffmann715a6642009-10-14 10:39:28 +02003662 exit(1);
3663 }
Gerd Hoffmann715a6642009-10-14 10:39:28 +02003664 break;
3665 }
Gerd Hoffmann29b00402010-03-11 11:13:27 -03003666 case QEMU_OPTION_spice:
3667 olist = qemu_find_opts("spice");
3668 if (!olist) {
3669 fprintf(stderr, "spice is not supported by this qemu build.\n");
3670 exit(1);
3671 }
3672 opts = qemu_opts_parse(olist, optarg, 0);
3673 if (!opts) {
3674 fprintf(stderr, "parse error: %s\n", optarg);
3675 exit(1);
3676 }
3677 break;
Gerd Hoffmann715a6642009-10-14 10:39:28 +02003678 case QEMU_OPTION_writeconfig:
3679 {
3680 FILE *fp;
3681 if (strcmp(optarg, "-") == 0) {
3682 fp = stdout;
3683 } else {
3684 fp = fopen(optarg, "w");
3685 if (fp == NULL) {
3686 fprintf(stderr, "open %s: %s\n", optarg, strerror(errno));
3687 exit(1);
3688 }
3689 }
3690 qemu_config_write(fp);
3691 fclose(fp);
3692 break;
3693 }
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +02003694 case QEMU_OPTION_qtest:
3695 qtest_chrdev = optarg;
3696 break;
3697 case QEMU_OPTION_qtest_log:
3698 qtest_log = optarg;
3699 break;
Eduardo Otubo7d76ad42012-08-14 18:44:08 -03003700 case QEMU_OPTION_sandbox:
3701 opts = qemu_opts_parse(qemu_find_opts("sandbox"), optarg, 1);
3702 if (!opts) {
3703 exit(0);
3704 }
3705 break;
Corey Bryant587ed6b2012-10-18 15:19:34 -04003706 case QEMU_OPTION_add_fd:
3707#ifndef _WIN32
3708 opts = qemu_opts_parse(qemu_find_opts("add-fd"), optarg, 0);
3709 if (!opts) {
3710 exit(0);
3711 }
3712#else
3713 error_report("File descriptor passing is disabled on this "
3714 "platform");
3715 exit(1);
3716#endif
3717 break;
Anthony Liguori68d98d32012-06-25 14:36:33 -05003718 case QEMU_OPTION_object:
3719 opts = qemu_opts_parse(qemu_find_opts("object"), optarg, 1);
3720 break;
Jes Sorensen59a52642010-06-10 11:42:25 +02003721 default:
3722 os_parse_cmd_args(popt->index, optarg);
bellardcd6f1162004-05-13 22:02:20 +00003723 }
bellard0824d6f2003-06-24 13:42:40 +00003724 }
3725 }
Markus Armbruster0f0bc3f2010-02-18 20:13:51 +01003726 loc_set_none();
bellard330d0412003-07-26 18:11:40 +00003727
Paolo Bonzini1c537862012-10-30 00:17:12 +01003728 if (qemu_init_main_loop()) {
3729 fprintf(stderr, "qemu_init_main_loop failed\n");
3730 exit(1);
3731 }
3732
Eduardo Otubo7d76ad42012-08-14 18:44:08 -03003733 if (qemu_opts_foreach(qemu_find_opts("sandbox"), parse_sandbox, NULL, 0)) {
3734 exit(1);
3735 }
3736
Corey Bryant587ed6b2012-10-18 15:19:34 -04003737#ifndef _WIN32
3738 if (qemu_opts_foreach(qemu_find_opts("add-fd"), parse_add_fd, NULL, 1)) {
3739 exit(1);
3740 }
3741
3742 if (qemu_opts_foreach(qemu_find_opts("add-fd"), cleanup_add_fd, NULL, 1)) {
3743 exit(1);
3744 }
3745#endif
3746
Dunrong Huangfb7c2692012-07-24 00:42:20 +08003747 if (machine == NULL) {
3748 fprintf(stderr, "No machine found.\n");
3749 exit(1);
3750 }
3751
Crístian Viana93bfef42012-05-30 00:35:51 -03003752 if (machine->hw_version) {
3753 qemu_set_version(machine->hw_version);
3754 }
3755
Anthony Liguori68d98d32012-06-25 14:36:33 -05003756 if (qemu_opts_foreach(qemu_find_opts("object"),
3757 object_create, NULL, 0) != 0) {
3758 exit(1);
3759 }
3760
Eduardo Habkostecf40be2012-03-09 16:19:07 -03003761 /* Init CPU def lists, based on config
3762 * - Must be called after all the qemu_read_config_file() calls
3763 * - Must be called before list_cpus()
3764 * - Must be called before machine->init()
3765 */
3766 cpudef_init();
3767
Peter Maydellc8057f92012-08-02 13:45:54 +01003768 if (cpu_model && is_help_option(cpu_model)) {
Eduardo Habkost1d6528a2012-03-21 09:33:40 -03003769 list_cpus(stdout, &fprintf, cpu_model);
Eduardo Habkostecf40be2012-03-09 16:19:07 -03003770 exit(0);
3771 }
3772
Matthew Fernandezc235d732011-06-07 16:32:40 +00003773 /* Open the logfile at this point, if necessary. We can't open the logfile
3774 * when encountering either of the logging options (-d or -D) because the
3775 * other one may be encountered later on the command line, changing the
3776 * location or level of logging.
3777 */
3778 if (log_mask) {
3779 if (log_file) {
3780 set_cpu_log_filename(log_file);
3781 }
3782 set_cpu_log(log_mask);
3783 }
3784
Lluís23d15e82011-08-31 20:31:31 +02003785 if (!trace_backend_init(trace_events, trace_file)) {
Lluíse4858972011-08-31 20:31:03 +02003786 exit(1);
Stefan Hajnoczi31d3c9b2011-03-13 20:14:30 +00003787 }
Stefan Hajnoczi0b5538c2011-02-26 18:38:39 +00003788
Paul Brook5cea8592009-05-30 00:52:44 +01003789 /* If no data_dir is specified then try to find it relative to the
3790 executable path. */
3791 if (!data_dir) {
Jes Sorensen61705402010-06-10 11:42:23 +02003792 data_dir = os_find_datadir(argv[0]);
Paul Brook5cea8592009-05-30 00:52:44 +01003793 }
Stefan Weil60474fb2011-09-04 15:17:46 +02003794 /* If all else fails use the install path specified when building. */
Paul Brook5cea8592009-05-30 00:52:44 +01003795 if (!data_dir) {
Paolo Bonzini1dabe052010-05-26 16:08:25 +02003796 data_dir = CONFIG_QEMU_DATADIR;
Paul Brook5cea8592009-05-30 00:52:44 +01003797 }
3798
Jes Sorensen6be68d72009-07-23 17:03:42 +02003799 /*
3800 * Default to max_cpus = smp_cpus, in case the user doesn't
3801 * specify a max_cpus value.
3802 */
3803 if (!max_cpus)
3804 max_cpus = smp_cpus;
3805
balrog3d878ca2008-10-28 10:59:59 +00003806 machine->max_cpus = machine->max_cpus ?: 1; /* Default to UP */
aliguorib2097002008-10-07 20:39:39 +00003807 if (smp_cpus > machine->max_cpus) {
3808 fprintf(stderr, "Number of SMP cpus requested (%d), exceeds max cpus "
3809 "supported by machine `%s' (%d)\n", smp_cpus, machine->name,
3810 machine->max_cpus);
3811 exit(1);
3812 }
3813
Anthony PERARD67b724e2010-11-22 15:44:15 +00003814 /*
3815 * Get the default machine options from the machine if it is not already
3816 * specified either by the configuration file or by the command line.
3817 */
3818 if (machine->default_machine_opts) {
Jan Kiszka25de5932012-01-27 19:55:43 +01003819 qemu_opts_set_defaults(qemu_find_opts("machine"),
3820 machine->default_machine_opts, 0);
Anthony PERARD67b724e2010-11-22 15:44:15 +00003821 }
3822
Gerd Hoffmann3329f072010-08-20 13:52:01 +02003823 qemu_opts_foreach(qemu_find_opts("device"), default_driver_check, NULL, 0);
3824 qemu_opts_foreach(qemu_find_opts("global"), default_driver_check, NULL, 0);
Gerd Hoffmann998bbd72009-12-08 13:11:41 +01003825
Gerd Hoffmann986c5f72009-12-08 13:11:54 +01003826 if (machine->no_serial) {
3827 default_serial = 0;
3828 }
3829 if (machine->no_parallel) {
3830 default_parallel = 0;
3831 }
3832 if (!machine->use_virtcon) {
3833 default_virtcon = 0;
3834 }
Gerd Hoffmannac33f8f2009-12-16 14:25:39 +01003835 if (machine->no_floppy) {
3836 default_floppy = 0;
3837 }
3838 if (machine->no_cdrom) {
3839 default_cdrom = 0;
3840 }
3841 if (machine->no_sdcard) {
3842 default_sdcard = 0;
3843 }
Gerd Hoffmann986c5f72009-12-08 13:11:54 +01003844
Michael Tokarevab51b1d2012-12-30 12:48:14 +04003845 if (is_daemonized()) {
3846 /* According to documentation and historically, -nographic redirects
3847 * serial port, parallel port and monitor to stdio, which does not work
3848 * with -daemonize. We can redirect these to null instead, but since
3849 * -nographic is legacy, let's just error out.
3850 * We disallow -nographic only if all other ports are not redirected
3851 * explicitly, to not break existing legacy setups which uses
3852 * -nographic _and_ redirects all ports explicitly - this is valid
3853 * usage, -nographic is just a no-op in this case.
3854 */
3855 if (display_type == DT_NOGRAPHIC
3856 && (default_parallel || default_serial
3857 || default_monitor || default_virtcon)) {
3858 fprintf(stderr, "-nographic can not be used with -daemonize\n");
3859 exit(1);
3860 }
3861#ifdef CONFIG_CURSES
3862 if (display_type == DT_CURSES) {
3863 fprintf(stderr, "curses display can not be used with -daemonize\n");
3864 exit(1);
3865 }
3866#endif
3867 }
3868
Anthony Liguori993fbfd2009-05-21 16:54:00 -05003869 if (display_type == DT_NOGRAPHIC) {
Gerd Hoffmann6a5e8b02009-12-08 13:11:42 +01003870 if (default_parallel)
3871 add_device_config(DEV_PARALLEL, "null");
Gerd Hoffmanne1c09172009-12-08 13:11:44 +01003872 if (default_serial && default_monitor) {
3873 add_device_config(DEV_SERIAL, "mon:stdio");
Gerd Hoffmann986c5f72009-12-08 13:11:54 +01003874 } else if (default_virtcon && default_monitor) {
3875 add_device_config(DEV_VIRTCON, "mon:stdio");
Gerd Hoffmanne1c09172009-12-08 13:11:44 +01003876 } else {
3877 if (default_serial)
3878 add_device_config(DEV_SERIAL, "stdio");
Gerd Hoffmann986c5f72009-12-08 13:11:54 +01003879 if (default_virtcon)
3880 add_device_config(DEV_VIRTCON, "stdio");
Gerd Hoffmanne1c09172009-12-08 13:11:44 +01003881 if (default_monitor)
Gerd Hoffmann6ca55822009-12-08 13:11:52 +01003882 monitor_parse("stdio", "readline");
Gerd Hoffmanne1c09172009-12-08 13:11:44 +01003883 }
Gerd Hoffmann998bbd72009-12-08 13:11:41 +01003884 } else {
3885 if (default_serial)
3886 add_device_config(DEV_SERIAL, "vc:80Cx24C");
Gerd Hoffmann6a5e8b02009-12-08 13:11:42 +01003887 if (default_parallel)
3888 add_device_config(DEV_PARALLEL, "vc:80Cx24C");
Gerd Hoffmannabdeed02009-12-08 13:11:43 +01003889 if (default_monitor)
Gerd Hoffmann6ca55822009-12-08 13:11:52 +01003890 monitor_parse("vc:80Cx24C", "readline");
Alexander Graf38536da2009-12-17 13:06:08 +01003891 if (default_virtcon)
3892 add_device_config(DEV_VIRTCON, "vc:80Cx24C");
aliguoribc0129d2008-08-01 15:12:34 +00003893 }
3894
TeLeMana5829fd2010-04-15 12:37:55 +08003895 socket_init();
3896
Gerd Hoffmann3329f072010-08-20 13:52:01 +02003897 if (qemu_opts_foreach(qemu_find_opts("chardev"), chardev_init_func, NULL, 1) != 0)
Gerd Hoffmann1a688d32009-12-08 13:11:36 +01003898 exit(1);
Venkateswararao Jujjuri (JV)758e8e32010-06-14 13:34:41 -07003899#ifdef CONFIG_VIRTFS
Gerd Hoffmann3329f072010-08-20 13:52:01 +02003900 if (qemu_opts_foreach(qemu_find_opts("fsdev"), fsdev_init_func, NULL, 1) != 0) {
Gautham R Shenoy74db9202010-04-29 17:44:43 +05303901 exit(1);
3902 }
3903#endif
Gerd Hoffmann1a688d32009-12-08 13:11:36 +01003904
Jes Sorenseneb505be2010-06-10 11:42:28 +02003905 os_daemonize();
ths71e3ceb2006-12-22 02:11:31 +00003906
thsaa26bb22007-03-25 21:33:06 +00003907 if (pid_file && qemu_create_pidfile(pid_file) != 0) {
Jes Sorenseneb505be2010-06-10 11:42:28 +02003908 os_pidfile_error();
ths93815bc2007-03-19 15:58:31 +00003909 exit(1);
3910 }
3911
Jan Kiszka0ac543d2011-08-15 16:18:57 -07003912 /* init the memory */
3913 if (ram_size == 0) {
3914 ram_size = DEFAULT_RAM_SIZE * 1024 * 1024;
3915 }
3916
Bruce Rogers3d1d9652012-08-09 12:47:40 -06003917 if (qemu_opts_foreach(qemu_find_opts("device"), device_help_func, NULL, 0)
3918 != 0) {
3919 exit(0);
3920 }
3921
Anthony PERARD303d4e82010-09-21 20:05:31 +01003922 configure_accelerator();
Marcelo Tosatti214910a2009-09-18 02:41:23 -03003923
Peter Maydell967c0da2012-02-22 22:40:00 +00003924 machine_opts = qemu_opts_find(qemu_find_opts("machine"), 0);
3925 if (machine_opts) {
3926 kernel_filename = qemu_opt_get(machine_opts, "kernel");
3927 initrd_filename = qemu_opt_get(machine_opts, "initrd");
3928 kernel_cmdline = qemu_opt_get(machine_opts, "append");
3929 } else {
3930 kernel_filename = initrd_filename = kernel_cmdline = NULL;
3931 }
3932
Peter Maydella0abe472012-02-08 05:41:39 +00003933 if (!kernel_cmdline) {
3934 kernel_cmdline = "";
3935 }
3936
bellarda20dd502003-09-30 21:07:02 +00003937 linux_boot = (kernel_filename != NULL);
balrog6c41b272007-11-17 12:12:29 +00003938
thsf8d39c02008-07-03 10:01:15 +00003939 if (!linux_boot && *kernel_cmdline != '\0') {
3940 fprintf(stderr, "-append only allowed with -kernel option\n");
3941 exit(1);
3942 }
3943
3944 if (!linux_boot && initrd_filename != NULL) {
3945 fprintf(stderr, "-initrd only allowed with -kernel option\n");
3946 exit(1);
3947 }
3948
Grant Likely412beee2012-03-02 11:56:38 +00003949 if (!linux_boot && machine_opts && qemu_opt_get(machine_opts, "dtb")) {
3950 fprintf(stderr, "-dtb only allowed with -kernel option\n");
3951 exit(1);
3952 }
3953
Jes Sorensen9156d762010-06-10 11:42:30 +02003954 os_set_line_buffering();
ths3b46e622007-09-17 08:09:54 +00003955
Paolo Bonzini49cf5722012-11-02 15:43:24 +01003956 qemu_init_cpu_loop();
3957 qemu_mutex_lock_iothread();
3958
Alon Levyad1be892012-03-14 20:33:37 +02003959#ifdef CONFIG_SPICE
3960 /* spice needs the timers to be initialized by this point */
3961 qemu_spice_init();
3962#endif
3963
Max Filippov0abe9052011-11-10 15:38:42 +04003964 if (icount_option && (kvm_enabled() || xen_enabled())) {
3965 fprintf(stderr, "-icount is not allowed with kvm or xen\n");
3966 exit(1);
3967 }
Paolo Bonzini4e3de9e2010-03-10 11:38:48 +01003968 configure_icount(icount_option);
pbrook634fce92006-07-15 17:40:09 +00003969
Amos Kongf30dbae2012-12-11 22:20:15 +08003970 /* clean up network at qemu process termination */
3971 atexit(&net_cleanup);
3972
Mark McLoughlindc1c9fe2009-10-06 12:17:16 +01003973 if (net_init_clients() < 0) {
3974 exit(1);
bellard702c6512004-04-02 21:21:32 +00003975 }
bellardf1510b22003-06-25 00:07:40 +00003976
balrogdc72ac12008-11-09 00:04:26 +00003977 /* init the bluetooth world */
Gerd Hoffmannbd3c9482009-07-15 13:59:26 +02003978 if (foreach_device_config(DEV_BT, bt_parse))
3979 exit(1);
balrogdc72ac12008-11-09 00:04:26 +00003980
Anthony PERARD834e76e2011-07-20 08:17:44 +00003981 if (!xen_enabled()) {
3982 /* On 32-bit hosts, QEMU is limited by virtual address space */
3983 if (ram_size > (2047 << 20) && HOST_LONG_BITS == 32) {
3984 fprintf(stderr, "qemu: at most 2047 MB RAM can be simulated\n");
3985 exit(1);
3986 }
3987 }
balrog7fb4fdc2008-04-24 17:59:27 +00003988
Jan Kiszkad5ab9712011-08-02 16:10:21 +02003989 cpu_exec_init_all();
bellard26a5f132008-05-28 12:30:31 +00003990
Markus Armbrustereb852012009-10-27 18:41:44 +01003991 bdrv_init_with_whitelist();
thse4bcb142007-12-02 04:51:10 +00003992
lirans@il.ibm.comc163b5c2009-11-02 15:40:58 +02003993 blk_mig_init();
3994
ths96d30e42007-01-07 20:42:14 +00003995 /* open the virtual block devices */
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +02003996 if (snapshot)
Gerd Hoffmann3329f072010-08-20 13:52:01 +02003997 qemu_opts_foreach(qemu_find_opts("drive"), drive_enable_snapshot, NULL, 0);
Christian Borntraeger2d0d2832012-11-20 15:30:34 +01003998 if (qemu_opts_foreach(qemu_find_opts("drive"), drive_init_func,
3999 &machine->block_default_type, 1) != 0) {
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +02004000 exit(1);
Christian Borntraeger2d0d2832012-11-20 15:30:34 +01004001 }
balrog3e3d5812007-04-30 02:09:25 +00004002
Christian Borntraeger3c42ea62012-11-22 21:02:55 +01004003 default_drive(default_cdrom, snapshot, machine->block_default_type, 2,
4004 CDROM_OPTS);
4005 default_drive(default_floppy, snapshot, IF_FLOPPY, 0, FD_OPTS);
4006 default_drive(default_sdcard, snapshot, IF_SD, 0, SD_OPTS);
Markus Armbruster4e5d9b52011-01-28 11:21:45 +01004007
Juan Quintela7908c782012-06-26 18:46:10 +02004008 register_savevm_live(NULL, "ram", 0, 4, &savevm_ram_handlers, NULL);
bellard8a7ddc32004-03-31 19:00:16 +00004009
aliguori268a3622009-04-21 22:30:27 +00004010 if (nb_numa_nodes > 0) {
4011 int i;
4012
Sasha Levinea0e5412011-06-29 23:29:39 -04004013 if (nb_numa_nodes > MAX_NODES) {
4014 nb_numa_nodes = MAX_NODES;
aliguori268a3622009-04-21 22:30:27 +00004015 }
4016
4017 /* If no memory size if given for any node, assume the default case
4018 * and distribute the available memory equally across all nodes
4019 */
4020 for (i = 0; i < nb_numa_nodes; i++) {
4021 if (node_mem[i] != 0)
4022 break;
4023 }
4024 if (i == nb_numa_nodes) {
4025 uint64_t usedmem = 0;
4026
4027 /* On Linux, the each node's border has to be 8MB aligned,
4028 * the final node gets the rest.
4029 */
4030 for (i = 0; i < nb_numa_nodes - 1; i++) {
4031 node_mem[i] = (ram_size / nb_numa_nodes) & ~((1 << 23UL) - 1);
4032 usedmem += node_mem[i];
4033 }
4034 node_mem[i] = ram_size - usedmem;
4035 }
4036
4037 for (i = 0; i < nb_numa_nodes; i++) {
Chegu Vinodee785fe2012-07-16 21:31:30 -07004038 if (!bitmap_empty(node_cpumask[i], MAX_CPUMASK_BITS)) {
aliguori268a3622009-04-21 22:30:27 +00004039 break;
Chegu Vinodee785fe2012-07-16 21:31:30 -07004040 }
aliguori268a3622009-04-21 22:30:27 +00004041 }
4042 /* assigning the VCPUs round-robin is easier to implement, guest OSes
4043 * must cope with this anyway, because there are BIOSes out there in
4044 * real machines which also use this scheme.
4045 */
4046 if (i == nb_numa_nodes) {
Vasilis Liaskovitis991dfef2011-10-26 14:19:00 +02004047 for (i = 0; i < max_cpus; i++) {
Chegu Vinodee785fe2012-07-16 21:31:30 -07004048 set_bit(i, node_cpumask[i % nb_numa_nodes]);
aliguori268a3622009-04-21 22:30:27 +00004049 }
4050 }
4051 }
4052
Gerd Hoffmann3329f072010-08-20 13:52:01 +02004053 if (qemu_opts_foreach(qemu_find_opts("mon"), mon_init_func, NULL, 1) != 0) {
Jan Kiszka157b9312010-04-06 16:55:53 +02004054 exit(1);
4055 }
4056
Gerd Hoffmann998bbd72009-12-08 13:11:41 +01004057 if (foreach_device_config(DEV_SERIAL, serial_parse) < 0)
4058 exit(1);
Gerd Hoffmann6a5e8b02009-12-08 13:11:42 +01004059 if (foreach_device_config(DEV_PARALLEL, parallel_parse) < 0)
4060 exit(1);
Gerd Hoffmannaee1b932009-12-08 13:11:53 +01004061 if (foreach_device_config(DEV_VIRTCON, virtcon_parse) < 0)
4062 exit(1);
H. Peter Anvinc9f398e2009-12-29 13:51:36 -08004063 if (foreach_device_config(DEV_DEBUGCON, debugcon_parse) < 0)
4064 exit(1);
aliguori2796dae2009-01-16 20:23:27 +00004065
Paolo Bonzini7f1b17f2012-05-10 09:39:17 +02004066 /* If no default VGA is requested, the default is "none". */
Aurelien Jarno3605ded2012-09-10 01:01:44 +02004067 if (default_vga) {
4068 if (cirrus_vga_available()) {
4069 vga_model = "cirrus";
4070 } else if (vga_available()) {
4071 vga_model = "std";
4072 }
Blue Swirla369da52011-09-27 19:15:42 +00004073 }
Paolo Bonzini7f1b17f2012-05-10 09:39:17 +02004074 select_vgahw(vga_model);
Blue Swirla369da52011-09-27 19:15:42 +00004075
Markus Armbruster09aaa162009-08-21 10:31:34 +02004076 if (watchdog) {
4077 i = select_watchdog(watchdog);
4078 if (i > 0)
4079 exit (i == 1 ? 1 : 0);
4080 }
4081
Gerd Hoffmannb6b61142009-07-15 13:48:21 +02004082 if (machine->compat_props) {
Gerd Hoffmann458fb672009-12-08 13:11:33 +01004083 qdev_prop_register_global_list(machine->compat_props);
Gerd Hoffmannb6b61142009-07-15 13:48:21 +02004084 }
Gerd Hoffmannd0fef6f2009-12-08 13:11:34 +01004085 qemu_add_globals();
4086
Anthony Liguori1de81d22011-12-19 16:37:46 -06004087 qdev_machine_init();
4088
Eduardo Habkost5f072e12012-10-15 17:22:02 -03004089 QEMUMachineInitArgs args = { .ram_size = ram_size,
Avik Sile4ada292013-01-08 12:36:30 +05304090 .boot_device = (boot_devices[0] == '\0') ?
4091 machine->boot_order :
4092 boot_devices,
Eduardo Habkost5f072e12012-10-15 17:22:02 -03004093 .kernel_filename = kernel_filename,
4094 .kernel_cmdline = kernel_cmdline,
4095 .initrd_filename = initrd_filename,
4096 .cpu_model = cpu_model };
4097 machine->init(&args);
aliguori3023f3322009-01-16 19:04:14 +00004098
Jan Kiszkaea375f92010-03-01 19:10:30 +01004099 cpu_synchronize_all_post_init();
aliguori268a3622009-04-21 22:30:27 +00004100
Blue Swirl87d0a282010-03-27 18:24:45 +00004101 set_numa_modes();
aliguori268a3622009-04-21 22:30:27 +00004102
aliguori6f338c32009-02-11 15:21:54 +00004103 current_machine = machine;
4104
aliguori3023f3322009-01-16 19:04:14 +00004105 /* init USB devices */
zhlcindy@gmail.com094b2872012-09-02 19:25:28 +00004106 if (usb_enabled(false)) {
Markus Armbruster07527062009-10-06 12:16:57 +01004107 if (foreach_device_config(DEV_USB, usb_parse) < 0)
4108 exit(1);
aliguori3023f3322009-01-16 19:04:14 +00004109 }
4110
Gerd Hoffmannbd3c9482009-07-15 13:59:26 +02004111 /* init generic devices */
Gerd Hoffmann3329f072010-08-20 13:52:01 +02004112 if (qemu_opts_foreach(qemu_find_opts("device"), device_init_func, NULL, 1) != 0)
Gerd Hoffmannbd3c9482009-07-15 13:59:26 +02004113 exit(1);
4114
Markus Armbruster668680f2010-02-11 14:44:58 +01004115 net_check_clients();
4116
aliguori3023f3322009-01-16 19:04:14 +00004117 /* just use the first displaystate for the moment */
Paolo Bonzinib473df62010-02-11 00:29:55 +01004118 ds = get_displaystate();
Anthony Liguori993fbfd2009-05-21 16:54:00 -05004119
Gerd Hoffmanna3e22262010-08-25 15:32:06 +02004120 if (using_spice)
4121 display_remote++;
Gerd Hoffmann6b62dc22010-08-25 10:51:06 +02004122 if (display_type == DT_DEFAULT && !display_remote) {
Anthony Liguorif92f8af2009-05-20 13:01:02 -05004123#if defined(CONFIG_SDL) || defined(CONFIG_COCOA)
Anthony Liguori993fbfd2009-05-21 16:54:00 -05004124 display_type = DT_SDL;
Jes Sorensen821601e2011-03-16 13:33:36 +01004125#elif defined(CONFIG_VNC)
Anthony Liguori993fbfd2009-05-21 16:54:00 -05004126 vnc_display = "localhost:0,to=99";
4127 show_vnc_port = 1;
Jes Sorensen821601e2011-03-16 13:33:36 +01004128#else
4129 display_type = DT_NONE;
Anthony Liguori993fbfd2009-05-21 16:54:00 -05004130#endif
4131 }
Jes Sorensen821601e2011-03-16 13:33:36 +01004132
Anthony Liguori993fbfd2009-05-21 16:54:00 -05004133
Gerd Hoffmann6b62dc22010-08-25 10:51:06 +02004134 /* init local displays */
Anthony Liguori993fbfd2009-05-21 16:54:00 -05004135 switch (display_type) {
4136 case DT_NOGRAPHIC:
4137 break;
4138#if defined(CONFIG_CURSES)
4139 case DT_CURSES:
Michael Tokarevab51b1d2012-12-30 12:48:14 +04004140 curses_display_init(ds, full_screen);
Anthony Liguori993fbfd2009-05-21 16:54:00 -05004141 break;
4142#endif
bellard5b0753e2005-03-01 21:37:28 +00004143#if defined(CONFIG_SDL)
Anthony Liguori993fbfd2009-05-21 16:54:00 -05004144 case DT_SDL:
4145 sdl_display_init(ds, full_screen, no_frame);
4146 break;
bellard5b0753e2005-03-01 21:37:28 +00004147#elif defined(CONFIG_COCOA)
Anthony Liguori993fbfd2009-05-21 16:54:00 -05004148 case DT_SDL:
4149 cocoa_display_init(ds, full_screen);
4150 break;
bellard313aa562003-08-10 21:52:11 +00004151#endif
Gerd Hoffmann6b62dc22010-08-25 10:51:06 +02004152 default:
4153 break;
4154 }
4155
Gleb Natapov0ce235a2011-03-31 11:27:23 +02004156 /* must be after terminal init, SDL library changes signal handlers */
4157 os_setup_signal_handling();
4158
Jes Sorensen821601e2011-03-16 13:33:36 +01004159#ifdef CONFIG_VNC
Gerd Hoffmann6b62dc22010-08-25 10:51:06 +02004160 /* init remote displays */
4161 if (vnc_display) {
Paolo Bonzini2d55f0e2012-10-02 10:17:21 +02004162 Error *local_err = NULL;
Anthony Liguori993fbfd2009-05-21 16:54:00 -05004163 vnc_display_init(ds);
Paolo Bonzini2d55f0e2012-10-02 10:17:21 +02004164 vnc_display_open(ds, vnc_display, &local_err);
4165 if (local_err != NULL) {
4166 fprintf(stderr, "Failed to start VNC server on `%s': %s\n",
4167 vnc_display, error_get_pretty(local_err));
4168 error_free(local_err);
Anthony Liguori993fbfd2009-05-21 16:54:00 -05004169 exit(1);
Amos Kong94b204c2012-06-30 10:02:20 +08004170 }
Anthony Liguorif92f8af2009-05-20 13:01:02 -05004171
Anthony Liguori993fbfd2009-05-21 16:54:00 -05004172 if (show_vnc_port) {
4173 printf("VNC server running on `%s'\n", vnc_display_local_addr(ds));
Anthony Liguorif92f8af2009-05-20 13:01:02 -05004174 }
bellard313aa562003-08-10 21:52:11 +00004175 }
Jes Sorensen821601e2011-03-16 13:33:36 +01004176#endif
Gerd Hoffmanna3e22262010-08-25 15:32:06 +02004177#ifdef CONFIG_SPICE
Gerd Hoffmanna19cbfb2010-04-27 11:50:11 +02004178 if (using_spice && !qxl_enabled) {
Gerd Hoffmanna3e22262010-08-25 15:32:06 +02004179 qemu_spice_display_init(ds);
4180 }
4181#endif
aliguori5b08fc12008-08-21 20:08:03 +00004182
Gerd Hoffmann6b62dc22010-08-25 10:51:06 +02004183 /* display setup */
Paolo Bonzinib473df62010-02-11 00:29:55 +01004184 text_consoles_set_display(ds);
aliguori2796dae2009-01-16 20:23:27 +00004185
Markus Armbrusteref0c4a02012-02-07 15:09:13 +01004186 if (foreach_device_config(DEV_GDB, gdbserver_start) < 0) {
aliguori59030a82009-04-05 18:43:41 +00004187 exit(1);
balrog45669e02007-07-02 13:20:17 +00004188 }
balrog45669e02007-07-02 13:20:17 +00004189
Gerd Hoffmann3418bd22009-09-25 21:42:41 +02004190 qdev_machine_creation_done();
4191
Gerd Hoffmann15ff7702009-12-14 16:07:35 +01004192 if (rom_load_all() != 0) {
4193 fprintf(stderr, "rom loading failed\n");
4194 exit(1);
4195 }
Gerd Hoffmann45a50b12009-10-01 16:42:33 +02004196
Isaku Yamahata80376c32010-12-20 14:33:35 +09004197 /* TODO: once all bus devices are qdevified, this should be done
4198 * when bus is created by qdev.c */
4199 qemu_register_reset(qbus_reset_all_fn, sysbus_get_default());
Gleb Natapov4cab9462010-12-08 13:35:08 +02004200 qemu_run_machine_init_done_notifiers();
4201
Jan Kiszkae063eb12011-06-14 18:29:43 +02004202 qemu_system_reset(VMRESET_SILENT);
Juan Quintela05f24012009-08-20 19:42:22 +02004203 if (loadvm) {
Markus Armbruster03cd4652010-02-17 16:24:10 +01004204 if (load_vmstate(loadvm) < 0) {
Juan Quintela05f24012009-08-20 19:42:22 +02004205 autostart = 0;
4206 }
4207 }
bellardd63d3072004-10-03 13:29:03 +00004208
Glauber Costa2bb8c102009-07-24 16:20:23 -04004209 if (incoming) {
Paolo Bonzini43eaae22012-10-02 18:21:18 +02004210 Error *local_err = NULL;
4211 qemu_start_incoming_migration(incoming, &local_err);
4212 if (local_err) {
4213 fprintf(stderr, "-incoming %s: %s\n", incoming, error_get_pretty(local_err));
4214 error_free(local_err);
4215 exit(1);
Juan Quintela8ca5e802010-06-09 14:10:54 +02004216 }
Avi Kivity6b99dad2009-08-09 14:39:20 +03004217 } else if (autostart) {
aliguoric0f4ce72009-03-05 23:01:01 +00004218 vm_start();
Avi Kivity6b99dad2009-08-09 14:39:20 +03004219 }
thsffd843b2006-12-21 19:46:43 +00004220
Jes Sorenseneb505be2010-06-10 11:42:28 +02004221 os_setup_post();
ths71e3ceb2006-12-22 02:11:31 +00004222
Paolo Bonzini99435902011-09-12 14:03:13 +02004223 resume_all_vcpus();
bellard8a7ddc32004-03-31 19:00:16 +00004224 main_loop();
Paolo Bonzini99435902011-09-12 14:03:13 +02004225 bdrv_close_all();
4226 pause_all_vcpus();
wayne3d3b8302011-07-27 18:04:55 +08004227 res_free();
thsb46a8902007-10-21 23:20:45 +00004228
bellard0824d6f2003-06-24 13:42:40 +00004229 return 0;
4230}