blob: 9adb8039a352eebcbdfbb44ca0deb2b13df1ef0a [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>
bellard67b915a2004-03-31 23:37:16 +000031
Juan Quintela71e72a12009-07-27 16:12:56 +020032/* Needed early for CONFIG_BSD etc. */
blueswir1d40cdb12009-03-07 16:52:02 +000033#include "config-host.h"
34
bellard67b915a2004-03-31 23:37:16 +000035#ifndef _WIN32
Paul Brook5cea8592009-05-30 00:52:44 +010036#include <libgen.h>
bellard67b915a2004-03-31 23:37:16 +000037#include <sys/times.h>
bellardf1510b22003-06-25 00:07:40 +000038#include <sys/wait.h>
bellard67b915a2004-03-31 23:37:16 +000039#include <termios.h>
bellard67b915a2004-03-31 23:37:16 +000040#include <sys/mman.h>
bellardf1510b22003-06-25 00:07:40 +000041#include <sys/ioctl.h>
blueswir124646c72008-11-07 16:55:48 +000042#include <sys/resource.h>
bellardf1510b22003-06-25 00:07:40 +000043#include <sys/socket.h>
bellardc94c8d62004-09-13 21:37:34 +000044#include <netinet/in.h>
blueswir124646c72008-11-07 16:55:48 +000045#include <net/if.h>
blueswir124646c72008-11-07 16:55:48 +000046#include <arpa/inet.h>
bellard9d728e82004-09-05 23:09:03 +000047#include <dirent.h>
bellard7c9d8e02005-11-15 22:16:05 +000048#include <netdb.h>
thscb4b9762007-09-13 12:39:35 +000049#include <sys/select.h>
Prerna Saxenaab6540d2010-08-09 11:48:32 +010050
Juan Quintela71e72a12009-07-27 16:12:56 +020051#ifdef CONFIG_BSD
bellard7d3505c2004-05-12 19:32:15 +000052#include <sys/stat.h>
Aurelien Jarnoa167ba52009-11-29 18:00:41 +010053#if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__DragonFly__)
bellard7d3505c2004-05-12 19:32:15 +000054#include <libutil.h>
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 <pty.h>
62#include <malloc.h>
thsbd494f42007-09-16 20:03:23 +000063
bellarde57a8c02005-11-10 23:58:52 +000064#include <linux/ppdev.h>
ths5867c882007-02-17 23:44:43 +000065#include <linux/parport.h>
blueswir1223f0d72008-09-30 18:12:18 +000066#endif
67#ifdef __sun__
thsd5d10bc2007-02-17 22:54:49 +000068#include <sys/stat.h>
69#include <sys/ethernet.h>
70#include <sys/sockio.h>
thsd5d10bc2007-02-17 22:54:49 +000071#include <netinet/arp.h>
thsd5d10bc2007-02-17 22:54:49 +000072#include <netinet/in_systm.h>
73#include <netinet/ip.h>
74#include <netinet/ip_icmp.h> // must come after ip.h
75#include <netinet/udp.h>
76#include <netinet/tcp.h>
77#include <net/if.h>
78#include <syslog.h>
79#include <stropts.h>
bellard67b915a2004-03-31 23:37:16 +000080#endif
bellard7d3505c2004-05-12 19:32:15 +000081#endif
bellardec530c82006-04-25 22:36:06 +000082#endif
bellard67b915a2004-03-31 23:37:16 +000083
blueswir19892fbf2008-08-24 10:34:20 +000084#if defined(__OpenBSD__)
85#include <util.h>
86#endif
87
ths8a16d272008-07-19 09:56:24 +000088#if defined(CONFIG_VDE)
89#include <libvdeplug.h>
90#endif
91
bellard67b915a2004-03-31 23:37:16 +000092#ifdef _WIN32
aliguori49dc7682009-03-08 16:26:59 +000093#include <windows.h>
bellard67b915a2004-03-31 23:37:16 +000094#endif
95
bellard73332e52004-04-04 20:22:28 +000096#ifdef CONFIG_SDL
Stefan Weil59a36a22009-06-18 20:11:03 +020097#if defined(__APPLE__) || defined(main)
Stefan Weil66936652009-06-13 13:19:11 +020098#include <SDL.h>
malc880fec52009-02-15 20:18:41 +000099int qemu_main(int argc, char **argv, char **envp);
100int main(int argc, char **argv)
101{
Stefan Weil59a36a22009-06-18 20:11:03 +0200102 return qemu_main(argc, argv, NULL);
malc880fec52009-02-15 20:18:41 +0000103}
104#undef main
105#define main qemu_main
bellard96bcd4f2004-07-10 16:26:15 +0000106#endif
bellard73332e52004-04-04 20:22:28 +0000107#endif /* CONFIG_SDL */
bellard0824d6f2003-06-24 13:42:40 +0000108
bellard5b0753e2005-03-01 21:37:28 +0000109#ifdef CONFIG_COCOA
110#undef main
111#define main qemu_main
112#endif /* CONFIG_COCOA */
113
Anthony Liguori69e5bb62011-08-22 08:12:52 -0500114#include <glib.h>
115
blueswir1511d2b12009-03-07 15:32:56 +0000116#include "hw/hw.h"
117#include "hw/boards.h"
118#include "hw/usb.h"
119#include "hw/pcmcia.h"
120#include "hw/pc.h"
blueswir1511d2b12009-03-07 15:32:56 +0000121#include "hw/isa.h"
122#include "hw/baum.h"
123#include "hw/bt.h"
Richard W.M. Jones9dd986c2009-04-25 13:56:19 +0100124#include "hw/watchdog.h"
aliguorib6f6e3d2009-04-17 18:59:56 +0000125#include "hw/smbios.h"
aliguorie37630c2009-04-22 15:19:10 +0000126#include "hw/xen.h"
Gerd Hoffmannbd3c9482009-07-15 13:59:26 +0200127#include "hw/qdev.h"
Gerd Hoffmann45a50b12009-10-01 16:42:33 +0200128#include "hw/loader.h"
aurel325ef4efa2009-03-10 21:43:35 +0000129#include "bt-host.h"
blueswir1511d2b12009-03-07 15:32:56 +0000130#include "net.h"
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000131#include "net/slirp.h"
blueswir1511d2b12009-03-07 15:32:56 +0000132#include "monitor.h"
133#include "console.h"
134#include "sysemu.h"
135#include "gdbstub.h"
136#include "qemu-timer.h"
137#include "qemu-char.h"
138#include "cache-utils.h"
139#include "block.h"
Markus Armbruster666daa62010-06-02 18:48:27 +0200140#include "blockdev.h"
lirans@il.ibm.comc163b5c2009-11-02 15:40:58 +0200141#include "block-migration.h"
blueswir1a718ace2009-03-28 08:24:44 +0000142#include "dma.h"
blueswir1511d2b12009-03-07 15:32:56 +0000143#include "audio/audio.h"
144#include "migration.h"
145#include "kvm.h"
Kevin Wolfd3f24362009-05-18 16:42:09 +0200146#include "qemu-option.h"
Gerd Hoffmann7282a032009-07-31 12:25:35 +0200147#include "qemu-config.h"
Luiz Capitulinoe78c48e2009-12-10 17:16:04 -0200148#include "qemu-objects.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"
Venkateswararao Jujjuri (JV)758e8e32010-06-14 13:34:41 -0700151#ifdef CONFIG_VIRTFS
Gautham R Shenoy74db9202010-04-29 17:44:43 +0530152#include "fsdev/qemu-fsdev.h"
153#endif
blueswir1511d2b12009-03-07 15:32:56 +0000154
bellard0824d6f2003-06-24 13:42:40 +0000155#include "disas.h"
bellardfc01f7e2003-06-30 10:03:06 +0000156
blueswir1511d2b12009-03-07 15:32:56 +0000157#include "qemu_socket.h"
158
Jan Kiszkad918f232009-06-24 14:42:30 +0200159#include "slirp/libslirp.h"
blueswir1511d2b12009-03-07 15:32:56 +0000160
Stefan Hajnoczi94b0b5f2010-11-16 12:20:25 +0000161#include "trace.h"
Lluíse4858972011-08-31 20:31:03 +0200162#include "trace/control.h"
Blue Swirl72cf2d42009-09-12 07:36:22 +0000163#include "qemu-queue.h"
Blue Swirl296af7c2010-03-29 19:23:50 +0000164#include "cpus.h"
Blue Swirlad960902010-03-29 19:23:52 +0000165#include "arch_init.h"
Blue Swirl72cf2d42009-09-12 07:36:22 +0000166
Gerd Hoffmann29b00402010-03-11 11:13:27 -0300167#include "ui/qemu-spice.h"
168
blueswir19dc63a12008-10-04 07:25:46 +0000169//#define DEBUG_NET
170//#define DEBUG_SLIRP
bellard330d0412003-07-26 18:11:40 +0000171
bellard1bfe8562004-07-08 21:17:50 +0000172#define DEFAULT_RAM_SIZE 128
bellard313aa562003-08-10 21:52:11 +0000173
Amit Shah98b19252010-01-20 00:36:52 +0530174#define MAX_VIRTIO_CONSOLES 1
175
Paul Brook5cea8592009-05-30 00:52:44 +0100176static const char *data_dir;
j_mayer1192dad2007-10-05 13:08:35 +0000177const char *bios_name = NULL;
malccb5a7aa2008-09-28 00:42:12 +0000178enum vga_retrace_method vga_retrace_method = VGA_RETRACE_DUMB;
Anthony Liguori993fbfd2009-05-21 16:54:00 -0500179DisplayType display_type = DT_DEFAULT;
Gerd Hoffmann6b62dc22010-08-25 10:51:06 +0200180int display_remote = 0;
bellard3d11d0e2004-12-12 16:56:30 +0000181const char* keyboard_layout = NULL;
Anthony Liguoric227f092009-10-01 16:12:16 -0500182ram_addr_t ram_size;
Marcelo Tosattic9027602010-03-01 20:25:08 -0300183const char *mem_path = NULL;
184#ifdef MAP_POPULATE
185int mem_prealloc = 0; /* force preallocation of physical target memory */
186#endif
bellardc4b1fcc2004-03-14 21:44:30 +0000187int nb_nics;
bellard7c9d8e02005-11-15 22:16:05 +0000188NICInfo nd_table[MAX_NICS];
Paolo Bonzinid399f672009-07-27 23:17:51 +0200189int autostart;
balrogf6503052008-02-17 11:42:19 +0000190static int rtc_utc = 1;
191static int rtc_date_offset = -1; /* -1 means no change */
Jan Kiszka68752042009-09-15 13:36:04 +0200192QEMUClock *rtc_clock;
Gerd Hoffmann64465292009-12-08 13:11:45 +0100193int vga_interface_type = VGA_NONE;
blueswir1dbed7e42008-10-01 19:38:09 +0000194static int full_screen = 0;
blueswir1634a21f2008-11-16 11:34:07 +0000195#ifdef CONFIG_SDL
blueswir1dbed7e42008-10-01 19:38:09 +0000196static int no_frame = 0;
blueswir1634a21f2008-11-16 11:34:07 +0000197#endif
ths667acca2006-12-11 02:08:05 +0000198int no_quit = 0;
bellard8d11df92004-08-24 21:13:40 +0000199CharDriverState *serial_hds[MAX_SERIAL_PORTS];
bellard6508fe52005-01-15 12:02:56 +0000200CharDriverState *parallel_hds[MAX_PARALLEL_PORTS];
aliguori9ede2fd2009-01-15 20:05:25 +0000201CharDriverState *virtcon_hds[MAX_VIRTIO_CONSOLES];
bellarda09db212005-04-30 16:10:35 +0000202int win2k_install_hack = 0;
aliguori73822ec2009-01-15 20:11:34 +0000203int rtc_td_hack = 0;
bellardbb36d472005-11-05 14:22:28 +0000204int usb_enabled = 0;
aurel321b530a62009-04-05 20:08:59 +0000205int singlestep = 0;
bellard6a00d602005-11-21 23:25:50 +0000206int smp_cpus = 1;
Jes Sorensen6be68d72009-07-23 17:03:42 +0200207int max_cpus = 0;
Andre Przywaradc6b1c02009-08-19 15:42:40 +0200208int smp_cores = 1;
209int smp_threads = 1;
Jes Sorensen821601e2011-03-16 13:33:36 +0100210#ifdef CONFIG_VNC
ths73fc9742006-12-22 02:09:07 +0000211const char *vnc_display;
Jes Sorensen821601e2011-03-16 13:33:36 +0100212#endif
bellard6515b202006-05-03 22:02:44 +0000213int acpi_enabled = 1;
aliguori16b29ae2008-12-17 23:28:44 +0000214int no_hpet = 0;
bellard52ca8d62006-06-14 16:03:05 +0000215int fd_bootchk = 1;
bellardd1beab82006-10-02 19:44:22 +0000216int no_reboot = 0;
aurel32b2f76162008-04-11 21:35:52 +0000217int no_shutdown = 0;
balrog9467cd42007-05-01 01:34:14 +0000218int cursor_hide = 1;
balroga171fe32007-04-30 01:48:07 +0000219int graphic_rotate = 0;
Jes Sorensen6b35e7b2009-08-06 16:25:50 +0200220uint8_t irq0override = 1;
Markus Armbruster09aaa162009-08-21 10:31:34 +0200221const char *watchdog;
Gleb Natapov2e55e842010-12-08 13:35:07 +0200222QEMUOptionRom option_rom[MAX_OPTION_ROMS];
ths9ae02552007-01-05 17:39:04 +0000223int nb_option_roms;
pbrook8e716212007-01-20 17:12:09 +0000224int semihosting_enabled = 0;
balrog2b8f2d42007-07-27 22:08:46 +0000225int old_param = 0;
thsc35734b2007-03-19 15:17:08 +0000226const char *qemu_name;
ths3780e192007-06-21 21:08:02 +0000227int alt_grab = 0;
Dustin Kirkland0ca9f8a2009-09-17 15:48:04 -0500228int ctrl_grab = 0;
blueswir166508602007-05-01 14:16:52 +0000229unsigned int nb_prom_envs = 0;
230const char *prom_envs[MAX_PROM_ENVS];
Jan Kiszka95387492009-07-02 00:19:02 +0200231int boot_menu;
wayne3d3b8302011-07-27 18:04:55 +0800232uint8_t *boot_splash_filedata;
233int boot_splash_filedata_size;
234uint8_t qemu_extra_params_fw[2];
bellard0824d6f2003-06-24 13:42:40 +0000235
Gleb Natapov1ca4d092010-12-08 13:35:05 +0200236typedef struct FWBootEntry FWBootEntry;
237
238struct FWBootEntry {
239 QTAILQ_ENTRY(FWBootEntry) link;
240 int32_t bootindex;
241 DeviceState *dev;
242 char *suffix;
243};
244
245QTAILQ_HEAD(, FWBootEntry) fw_boot_order = QTAILQ_HEAD_INITIALIZER(fw_boot_order);
246
aliguori268a3622009-04-21 22:30:27 +0000247int nb_numa_nodes;
248uint64_t node_mem[MAX_NODES];
249uint64_t node_cpumask[MAX_NODES];
250
blueswir19043b622009-01-21 19:28:13 +0000251static QEMUTimer *nographic_timer;
balrogee5605e2007-12-03 03:01:40 +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 Hoffmann64465292009-12-08 13:11:45 +0100275static int default_vga = 1;
Gerd Hoffmannac33f8f2009-12-16 14:25:39 +0100276static int default_floppy = 1;
277static int default_cdrom = 1;
278static int default_sdcard = 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 },
Gerd Hoffmann64465292009-12-08 13:11:45 +0100294 { .driver = "VGA", .flag = &default_vga },
Gerd Hoffmann69fd02e2009-12-16 13:35:19 +0100295 { .driver = "cirrus-vga", .flag = &default_vga },
296 { .driver = "vmware-svga", .flag = &default_vga },
Markus Armbruster0826c712011-06-07 10:34:30 +0200297 { .driver = "isa-vga", .flag = &default_vga },
Gerd Hoffmann42138042011-05-16 09:28:58 +0200298 { .driver = "qxl-vga", .flag = &default_vga },
Gerd Hoffmann998bbd72009-12-08 13:11:41 +0100299};
300
wayne3d3b8302011-07-27 18:04:55 +0800301static void res_free(void)
302{
303 if (boot_splash_filedata != NULL) {
Anthony Liguori7267c092011-08-20 22:09:37 -0500304 g_free(boot_splash_filedata);
wayne3d3b8302011-07-27 18:04:55 +0800305 boot_splash_filedata = NULL;
306 }
307}
308
Gerd Hoffmann998bbd72009-12-08 13:11:41 +0100309static int default_driver_check(QemuOpts *opts, void *opaque)
310{
311 const char *driver = qemu_opt_get(opts, "driver");
312 int i;
313
314 if (!driver)
315 return 0;
316 for (i = 0; i < ARRAY_SIZE(default_list); i++) {
317 if (strcmp(default_list[i].driver, driver) != 0)
318 continue;
319 *(default_list[i].flag) = 0;
320 }
321 return 0;
322}
323
bellard0824d6f2003-06-24 13:42:40 +0000324/***********************************************************/
Luiz Capitulinof5bbfba2011-07-29 15:04:45 -0300325/* QEMU state */
326
Luiz Capitulino0461d5a2011-09-30 14:45:27 -0300327static RunState current_run_state = RUN_STATE_PRELAUNCH;
Luiz Capitulinof5bbfba2011-07-29 15:04:45 -0300328
Luiz Capitulino5db9d4d2011-09-05 16:36:31 -0300329typedef struct {
330 RunState from;
331 RunState to;
332} RunStateTransition;
333
334static const RunStateTransition runstate_transitions_def[] = {
335 /* from -> to */
Luiz Capitulino0461d5a2011-09-30 14:45:27 -0300336 { RUN_STATE_DEBUG, RUN_STATE_RUNNING },
Luiz Capitulino5db9d4d2011-09-05 16:36:31 -0300337
Luiz Capitulino0461d5a2011-09-30 14:45:27 -0300338 { RUN_STATE_INMIGRATE, RUN_STATE_RUNNING },
339 { RUN_STATE_INMIGRATE, RUN_STATE_PRELAUNCH },
Luiz Capitulino5db9d4d2011-09-05 16:36:31 -0300340
Luiz Capitulino0461d5a2011-09-30 14:45:27 -0300341 { RUN_STATE_INTERNAL_ERROR, RUN_STATE_PAUSED },
Luiz Capitulino5db9d4d2011-09-05 16:36:31 -0300342
Luiz Capitulino0461d5a2011-09-30 14:45:27 -0300343 { RUN_STATE_IO_ERROR, RUN_STATE_RUNNING },
Luiz Capitulino5db9d4d2011-09-05 16:36:31 -0300344
Luiz Capitulino0461d5a2011-09-30 14:45:27 -0300345 { RUN_STATE_PAUSED, RUN_STATE_RUNNING },
Luiz Capitulino5db9d4d2011-09-05 16:36:31 -0300346
Luiz Capitulino0461d5a2011-09-30 14:45:27 -0300347 { RUN_STATE_POSTMIGRATE, RUN_STATE_RUNNING },
Luiz Capitulino5db9d4d2011-09-05 16:36:31 -0300348
Luiz Capitulino0461d5a2011-09-30 14:45:27 -0300349 { RUN_STATE_PRELAUNCH, RUN_STATE_RUNNING },
350 { RUN_STATE_PRELAUNCH, RUN_STATE_INMIGRATE },
351 { RUN_STATE_PRELAUNCH, RUN_STATE_POSTMIGRATE },
Luiz Capitulino5db9d4d2011-09-05 16:36:31 -0300352
Luiz Capitulino0461d5a2011-09-30 14:45:27 -0300353 { RUN_STATE_FINISH_MIGRATE, RUN_STATE_RUNNING },
354 { RUN_STATE_FINISH_MIGRATE, RUN_STATE_POSTMIGRATE },
Luiz Capitulino5db9d4d2011-09-05 16:36:31 -0300355
Luiz Capitulino0461d5a2011-09-30 14:45:27 -0300356 { RUN_STATE_RESTORE_VM, RUN_STATE_RUNNING },
Luiz Capitulino5db9d4d2011-09-05 16:36:31 -0300357
Luiz Capitulino0461d5a2011-09-30 14:45:27 -0300358 { RUN_STATE_RUNNING, RUN_STATE_DEBUG },
359 { RUN_STATE_RUNNING, RUN_STATE_INTERNAL_ERROR },
360 { RUN_STATE_RUNNING, RUN_STATE_IO_ERROR },
361 { RUN_STATE_RUNNING, RUN_STATE_PAUSED },
362 { RUN_STATE_RUNNING, RUN_STATE_FINISH_MIGRATE },
363 { RUN_STATE_RUNNING, RUN_STATE_RESTORE_VM },
364 { RUN_STATE_RUNNING, RUN_STATE_SAVE_VM },
365 { RUN_STATE_RUNNING, RUN_STATE_SHUTDOWN },
366 { RUN_STATE_RUNNING, RUN_STATE_WATCHDOG },
Luiz Capitulino5db9d4d2011-09-05 16:36:31 -0300367
Luiz Capitulino0461d5a2011-09-30 14:45:27 -0300368 { RUN_STATE_SAVE_VM, RUN_STATE_RUNNING },
Luiz Capitulino5db9d4d2011-09-05 16:36:31 -0300369
Luiz Capitulino0461d5a2011-09-30 14:45:27 -0300370 { RUN_STATE_SHUTDOWN, RUN_STATE_PAUSED },
Luiz Capitulino5db9d4d2011-09-05 16:36:31 -0300371
Luiz Capitulino0461d5a2011-09-30 14:45:27 -0300372 { RUN_STATE_WATCHDOG, RUN_STATE_RUNNING },
Luiz Capitulino5db9d4d2011-09-05 16:36:31 -0300373
Luiz Capitulino0461d5a2011-09-30 14:45:27 -0300374 { RUN_STATE_MAX, RUN_STATE_MAX },
Luiz Capitulino5db9d4d2011-09-05 16:36:31 -0300375};
376
Luiz Capitulino0461d5a2011-09-30 14:45:27 -0300377static bool runstate_valid_transitions[RUN_STATE_MAX][RUN_STATE_MAX];
Luiz Capitulino5db9d4d2011-09-05 16:36:31 -0300378
Luiz Capitulinof5bbfba2011-07-29 15:04:45 -0300379bool runstate_check(RunState state)
380{
381 return current_run_state == state;
382}
383
Luiz Capitulino5db9d4d2011-09-05 16:36:31 -0300384void runstate_init(void)
385{
386 const RunStateTransition *p;
387
388 memset(&runstate_valid_transitions, 0, sizeof(runstate_valid_transitions));
389
Luiz Capitulino0461d5a2011-09-30 14:45:27 -0300390 for (p = &runstate_transitions_def[0]; p->from != RUN_STATE_MAX; p++) {
Luiz Capitulino5db9d4d2011-09-05 16:36:31 -0300391 runstate_valid_transitions[p->from][p->to] = true;
392 }
393}
394
395/* This function will abort() on invalid state transitions */
Luiz Capitulinof5bbfba2011-07-29 15:04:45 -0300396void runstate_set(RunState new_state)
397{
Luiz Capitulino0461d5a2011-09-30 14:45:27 -0300398 if (new_state >= RUN_STATE_MAX ||
Luiz Capitulino5db9d4d2011-09-05 16:36:31 -0300399 !runstate_valid_transitions[current_run_state][new_state]) {
400 fprintf(stderr, "invalid runstate transition\n");
401 abort();
402 }
403
Luiz Capitulinof5bbfba2011-07-29 15:04:45 -0300404 current_run_state = new_state;
405}
406
Luiz Capitulino13548692011-07-29 15:36:43 -0300407int runstate_is_running(void)
408{
Luiz Capitulino0461d5a2011-09-30 14:45:27 -0300409 return runstate_check(RUN_STATE_RUNNING);
Luiz Capitulino13548692011-07-29 15:36:43 -0300410}
411
Luiz Capitulino1fa9a5e2011-09-12 17:54:20 -0300412StatusInfo *qmp_query_status(Error **errp)
413{
414 StatusInfo *info = g_malloc0(sizeof(*info));
415
416 info->running = runstate_is_running();
417 info->singlestep = singlestep;
418 info->status = current_run_state;
419
420 return info;
421}
422
Luiz Capitulinof5bbfba2011-07-29 15:04:45 -0300423/***********************************************************/
Paolo Bonzini8f0056b2010-01-13 14:05:34 +0100424/* real time host monotonic timer */
bellard09b26c52006-04-12 21:09:08 +0000425
bellardc4b1fcc2004-03-14 21:44:30 +0000426/***********************************************************/
balrogf6503052008-02-17 11:42:19 +0000427/* host time/date access */
428void qemu_get_timedate(struct tm *tm, int offset)
429{
430 time_t ti;
431 struct tm *ret;
432
433 time(&ti);
434 ti += offset;
435 if (rtc_date_offset == -1) {
436 if (rtc_utc)
437 ret = gmtime(&ti);
438 else
439 ret = localtime(&ti);
440 } else {
441 ti -= rtc_date_offset;
442 ret = gmtime(&ti);
443 }
444
445 memcpy(tm, ret, sizeof(struct tm));
446}
447
448int qemu_timedate_diff(struct tm *tm)
449{
450 time_t seconds;
451
452 if (rtc_date_offset == -1)
453 if (rtc_utc)
454 seconds = mktimegm(tm);
455 else
456 seconds = mktime(tm);
457 else
458 seconds = mktimegm(tm) + rtc_date_offset;
459
460 return seconds - time(NULL);
461}
462
Luiz Capitulino80cd3472010-02-25 12:11:44 -0300463void rtc_change_mon_event(struct tm *tm)
464{
465 QObject *data;
466
467 data = qobject_from_jsonf("{ 'offset': %d }", qemu_timedate_diff(tm));
468 monitor_protocol_event(QEVENT_RTC_CHANGE, data);
469 qobject_decref(data);
470}
471
Jan Kiszka1ed2fc12009-09-15 13:36:04 +0200472static void configure_rtc_date_offset(const char *startdate, int legacy)
473{
474 time_t rtc_start_date;
475 struct tm tm;
476
477 if (!strcmp(startdate, "now") && legacy) {
478 rtc_date_offset = -1;
479 } else {
480 if (sscanf(startdate, "%d-%d-%dT%d:%d:%d",
481 &tm.tm_year,
482 &tm.tm_mon,
483 &tm.tm_mday,
484 &tm.tm_hour,
485 &tm.tm_min,
486 &tm.tm_sec) == 6) {
487 /* OK */
488 } else if (sscanf(startdate, "%d-%d-%d",
489 &tm.tm_year,
490 &tm.tm_mon,
491 &tm.tm_mday) == 3) {
492 tm.tm_hour = 0;
493 tm.tm_min = 0;
494 tm.tm_sec = 0;
495 } else {
496 goto date_fail;
497 }
498 tm.tm_year -= 1900;
499 tm.tm_mon--;
500 rtc_start_date = mktimegm(&tm);
501 if (rtc_start_date == -1) {
502 date_fail:
503 fprintf(stderr, "Invalid date format. Valid formats are:\n"
504 "'2006-06-17T16:01:21' or '2006-06-17'\n");
505 exit(1);
506 }
507 rtc_date_offset = time(NULL) - rtc_start_date;
508 }
509}
510
511static void configure_rtc(QemuOpts *opts)
512{
513 const char *value;
514
515 value = qemu_opt_get(opts, "base");
516 if (value) {
517 if (!strcmp(value, "utc")) {
518 rtc_utc = 1;
519 } else if (!strcmp(value, "localtime")) {
520 rtc_utc = 0;
521 } else {
522 configure_rtc_date_offset(value, 0);
523 }
524 }
Jan Kiszka68752042009-09-15 13:36:04 +0200525 value = qemu_opt_get(opts, "clock");
526 if (value) {
527 if (!strcmp(value, "host")) {
528 rtc_clock = host_clock;
529 } else if (!strcmp(value, "vm")) {
530 rtc_clock = vm_clock;
531 } else {
532 fprintf(stderr, "qemu: invalid option value '%s'\n", value);
533 exit(1);
534 }
535 }
Jan Kiszka1ed2fc12009-09-15 13:36:04 +0200536 value = qemu_opt_get(opts, "driftfix");
537 if (value) {
Blue Swirl7e4c0332010-03-27 21:33:46 +0000538 if (!strcmp(value, "slew")) {
Jan Kiszka1ed2fc12009-09-15 13:36:04 +0200539 rtc_td_hack = 1;
Blue Swirl7e4c0332010-03-27 21:33:46 +0000540 } else if (!strcmp(value, "none")) {
Jan Kiszka1ed2fc12009-09-15 13:36:04 +0200541 rtc_td_hack = 0;
542 } else {
543 fprintf(stderr, "qemu: invalid option value '%s'\n", value);
544 exit(1);
545 }
546 }
Jan Kiszka1ed2fc12009-09-15 13:36:04 +0200547}
548
balrog1ae26a12008-09-28 23:19:47 +0000549/***********************************************************/
550/* Bluetooth support */
551static int nb_hcis;
552static int cur_hci;
553static struct HCIInfo *hci_table[MAX_NICS];
balrogdc72ac12008-11-09 00:04:26 +0000554
balrog1ae26a12008-09-28 23:19:47 +0000555static struct bt_vlan_s {
556 struct bt_scatternet_s net;
557 int id;
558 struct bt_vlan_s *next;
559} *first_bt_vlan;
560
561/* find or alloc a new bluetooth "VLAN" */
blueswir1674bb262008-09-30 18:18:27 +0000562static struct bt_scatternet_s *qemu_find_bt_vlan(int id)
balrog1ae26a12008-09-28 23:19:47 +0000563{
564 struct bt_vlan_s **pvlan, *vlan;
565 for (vlan = first_bt_vlan; vlan != NULL; vlan = vlan->next) {
566 if (vlan->id == id)
567 return &vlan->net;
568 }
Anthony Liguori7267c092011-08-20 22:09:37 -0500569 vlan = g_malloc0(sizeof(struct bt_vlan_s));
balrog1ae26a12008-09-28 23:19:47 +0000570 vlan->id = id;
571 pvlan = &first_bt_vlan;
572 while (*pvlan != NULL)
573 pvlan = &(*pvlan)->next;
574 *pvlan = vlan;
575 return &vlan->net;
576}
577
578static void null_hci_send(struct HCIInfo *hci, const uint8_t *data, int len)
579{
580}
581
582static int null_hci_addr_set(struct HCIInfo *hci, const uint8_t *bd_addr)
583{
584 return -ENOTSUP;
585}
586
587static struct HCIInfo null_hci = {
588 .cmd_send = null_hci_send,
589 .sco_send = null_hci_send,
590 .acl_send = null_hci_send,
591 .bdaddr_set = null_hci_addr_set,
592};
593
594struct HCIInfo *qemu_next_hci(void)
595{
596 if (cur_hci == nb_hcis)
597 return &null_hci;
598
599 return hci_table[cur_hci++];
600}
601
balrogdc72ac12008-11-09 00:04:26 +0000602static struct HCIInfo *hci_init(const char *str)
603{
604 char *endp;
605 struct bt_scatternet_s *vlan = 0;
606
607 if (!strcmp(str, "null"))
608 /* null */
609 return &null_hci;
610 else if (!strncmp(str, "host", 4) && (str[4] == '\0' || str[4] == ':'))
611 /* host[:hciN] */
612 return bt_host_hci(str[4] ? str + 5 : "hci0");
613 else if (!strncmp(str, "hci", 3)) {
614 /* hci[,vlan=n] */
615 if (str[3]) {
616 if (!strncmp(str + 3, ",vlan=", 6)) {
617 vlan = qemu_find_bt_vlan(strtol(str + 9, &endp, 0));
618 if (*endp)
619 vlan = 0;
620 }
621 } else
622 vlan = qemu_find_bt_vlan(0);
623 if (vlan)
624 return bt_new_hci(vlan);
625 }
626
627 fprintf(stderr, "qemu: Unknown bluetooth HCI `%s'.\n", str);
628
629 return 0;
630}
631
632static int bt_hci_parse(const char *str)
633{
634 struct HCIInfo *hci;
Anthony Liguoric227f092009-10-01 16:12:16 -0500635 bdaddr_t bdaddr;
balrogdc72ac12008-11-09 00:04:26 +0000636
637 if (nb_hcis >= MAX_NICS) {
638 fprintf(stderr, "qemu: Too many bluetooth HCIs (max %i).\n", MAX_NICS);
639 return -1;
640 }
641
642 hci = hci_init(str);
643 if (!hci)
644 return -1;
645
646 bdaddr.b[0] = 0x52;
647 bdaddr.b[1] = 0x54;
648 bdaddr.b[2] = 0x00;
649 bdaddr.b[3] = 0x12;
650 bdaddr.b[4] = 0x34;
651 bdaddr.b[5] = 0x56 + nb_hcis;
652 hci->bdaddr_set(hci, bdaddr.b);
653
654 hci_table[nb_hcis++] = hci;
655
656 return 0;
657}
658
659static void bt_vhci_add(int vlan_id)
660{
661 struct bt_scatternet_s *vlan = qemu_find_bt_vlan(vlan_id);
662
663 if (!vlan->slave)
664 fprintf(stderr, "qemu: warning: adding a VHCI to "
665 "an empty scatternet %i\n", vlan_id);
666
667 bt_vhci_init(bt_new_hci(vlan));
668}
669
670static struct bt_device_s *bt_device_add(const char *opt)
671{
672 struct bt_scatternet_s *vlan;
673 int vlan_id = 0;
674 char *endp = strstr(opt, ",vlan=");
675 int len = (endp ? endp - opt : strlen(opt)) + 1;
676 char devname[10];
677
678 pstrcpy(devname, MIN(sizeof(devname), len), opt);
679
680 if (endp) {
681 vlan_id = strtol(endp + 6, &endp, 0);
682 if (*endp) {
683 fprintf(stderr, "qemu: unrecognised bluetooth vlan Id\n");
684 return 0;
685 }
686 }
687
688 vlan = qemu_find_bt_vlan(vlan_id);
689
690 if (!vlan->slave)
691 fprintf(stderr, "qemu: warning: adding a slave device to "
692 "an empty scatternet %i\n", vlan_id);
693
694 if (!strcmp(devname, "keyboard"))
695 return bt_keyboard_init(vlan);
696
697 fprintf(stderr, "qemu: unsupported bluetooth device `%s'\n", devname);
698 return 0;
699}
700
701static int bt_parse(const char *opt)
702{
703 const char *endp, *p;
704 int vlan;
705
706 if (strstart(opt, "hci", &endp)) {
707 if (!*endp || *endp == ',') {
708 if (*endp)
709 if (!strstart(endp, ",vlan=", 0))
710 opt = endp + 1;
711
712 return bt_hci_parse(opt);
713 }
714 } else if (strstart(opt, "vhci", &endp)) {
715 if (!*endp || *endp == ',') {
716 if (*endp) {
717 if (strstart(endp, ",vlan=", &p)) {
718 vlan = strtol(p, (char **) &endp, 0);
719 if (*endp) {
720 fprintf(stderr, "qemu: bad scatternet '%s'\n", p);
721 return 1;
722 }
723 } else {
724 fprintf(stderr, "qemu: bad parameter '%s'\n", endp + 1);
725 return 1;
726 }
727 } else
728 vlan = 0;
729
730 bt_vhci_add(vlan);
731 return 0;
732 }
733 } else if (strstart(opt, "device:", &endp))
734 return !bt_device_add(endp);
735
736 fprintf(stderr, "qemu: bad bluetooth parameter '%s'\n", opt);
737 return 1;
738}
739
balrog1ae26a12008-09-28 23:19:47 +0000740/***********************************************************/
741/* QEMU Block devices */
742
Markus Armbruster2292dda2011-01-28 11:21:41 +0100743#define HD_OPTS "media=disk"
744#define CDROM_OPTS "media=cdrom"
745#define FD_OPTS ""
746#define PFLASH_OPTS ""
747#define MTD_OPTS ""
748#define SD_OPTS ""
thse4bcb142007-12-02 04:51:10 +0000749
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +0200750static int drive_init_func(QemuOpts *opts, void *opaque)
751{
Markus Armbrustera803cb82010-06-02 13:31:55 +0200752 int *use_scsi = opaque;
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +0200753
Markus Armbruster319ae522011-01-28 11:21:46 +0100754 return drive_init(opts, *use_scsi) == NULL;
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +0200755}
756
757static int drive_enable_snapshot(QemuOpts *opts, void *opaque)
758{
759 if (NULL == qemu_opt_get(opts, "snapshot")) {
760 qemu_opt_set(opts, "snapshot", "on");
761 }
762 return 0;
763}
764
Markus Armbruster4e5d9b52011-01-28 11:21:45 +0100765static void default_drive(int enable, int snapshot, int use_scsi,
766 BlockInterfaceType type, int index,
767 const char *optstr)
768{
769 QemuOpts *opts;
770
771 if (type == IF_DEFAULT) {
772 type = use_scsi ? IF_SCSI : IF_IDE;
773 }
774
775 if (!enable || drive_get_by_index(type, index)) {
776 return;
777 }
778
779 opts = drive_add(type, index, NULL, optstr);
780 if (snapshot) {
781 drive_enable_snapshot(opts, NULL);
782 }
Markus Armbruster319ae522011-01-28 11:21:46 +0100783 if (!drive_init(opts, use_scsi)) {
Markus Armbruster4e5d9b52011-01-28 11:21:45 +0100784 exit(1);
785 }
786}
787
Jan Kiszka76e30d02009-07-02 00:19:02 +0200788void qemu_register_boot_set(QEMUBootSetHandler *func, void *opaque)
789{
790 boot_set_handler = func;
791 boot_set_opaque = opaque;
792}
793
794int qemu_boot_set(const char *boot_devices)
795{
796 if (!boot_set_handler) {
797 return -EINVAL;
798 }
799 return boot_set_handler(boot_set_opaque, boot_devices);
800}
801
Eduardo Habkost4e9e9d62010-04-06 19:22:08 -0300802static void validate_bootdevices(char *devices)
Jan Kiszkaef3adf62009-07-02 00:19:02 +0200803{
804 /* We just do some generic consistency checks */
805 const char *p;
806 int bitmap = 0;
807
808 for (p = devices; *p != '\0'; p++) {
809 /* Allowed boot devices are:
810 * a-b: floppy disk drives
811 * c-f: IDE disk drives
812 * g-m: machine implementation dependant drives
813 * n-p: network devices
814 * It's up to each machine implementation to check if the given boot
815 * devices match the actual hardware implementation and firmware
816 * features.
817 */
818 if (*p < 'a' || *p > 'p') {
819 fprintf(stderr, "Invalid boot device '%c'\n", *p);
820 exit(1);
821 }
822 if (bitmap & (1 << (*p - 'a'))) {
823 fprintf(stderr, "Boot device '%c' was given twice\n", *p);
824 exit(1);
825 }
826 bitmap |= 1 << (*p - 'a');
827 }
Jan Kiszkaef3adf62009-07-02 00:19:02 +0200828}
829
Jan Kiszkae0f084b2009-07-02 00:19:02 +0200830static void restore_boot_devices(void *opaque)
831{
832 char *standard_boot_devices = opaque;
Alex Williamson37905d62010-04-30 15:21:11 -0600833 static int first = 1;
834
835 /* Restore boot order and remove ourselves after the first boot */
836 if (first) {
837 first = 0;
838 return;
839 }
Jan Kiszkae0f084b2009-07-02 00:19:02 +0200840
841 qemu_boot_set(standard_boot_devices);
842
843 qemu_unregister_reset(restore_boot_devices, standard_boot_devices);
Anthony Liguori7267c092011-08-20 22:09:37 -0500844 g_free(standard_boot_devices);
Jan Kiszkae0f084b2009-07-02 00:19:02 +0200845}
846
Gleb Natapov1ca4d092010-12-08 13:35:05 +0200847void add_boot_device_path(int32_t bootindex, DeviceState *dev,
848 const char *suffix)
849{
850 FWBootEntry *node, *i;
851
852 if (bootindex < 0) {
853 return;
854 }
855
856 assert(dev != NULL || suffix != NULL);
857
Anthony Liguori7267c092011-08-20 22:09:37 -0500858 node = g_malloc0(sizeof(FWBootEntry));
Gleb Natapov1ca4d092010-12-08 13:35:05 +0200859 node->bootindex = bootindex;
Anthony Liguori7267c092011-08-20 22:09:37 -0500860 node->suffix = suffix ? g_strdup(suffix) : NULL;
Gleb Natapov1ca4d092010-12-08 13:35:05 +0200861 node->dev = dev;
862
863 QTAILQ_FOREACH(i, &fw_boot_order, link) {
864 if (i->bootindex == bootindex) {
865 fprintf(stderr, "Two devices with same boot index %d\n", bootindex);
866 exit(1);
867 } else if (i->bootindex < bootindex) {
868 continue;
869 }
870 QTAILQ_INSERT_BEFORE(i, node, link);
871 return;
872 }
873 QTAILQ_INSERT_TAIL(&fw_boot_order, node, link);
874}
875
Gleb Natapov962630f2010-12-08 13:35:09 +0200876/*
877 * This function returns null terminated string that consist of new line
Brad Hards71785ab2011-04-23 21:50:06 +1000878 * separated device paths.
Gleb Natapov962630f2010-12-08 13:35:09 +0200879 *
880 * memory pointed by "size" is assigned total length of the array in bytes
881 *
882 */
883char *get_boot_devices_list(uint32_t *size)
884{
885 FWBootEntry *i;
886 uint32_t total = 0;
887 char *list = NULL;
888
889 QTAILQ_FOREACH(i, &fw_boot_order, link) {
890 char *devpath = NULL, *bootpath;
891 int len;
892
893 if (i->dev) {
894 devpath = qdev_get_fw_dev_path(i->dev);
895 assert(devpath);
896 }
897
898 if (i->suffix && devpath) {
Blue Swirl4fd37a92010-12-19 14:05:43 +0000899 size_t bootpathlen = strlen(devpath) + strlen(i->suffix) + 1;
900
Anthony Liguori7267c092011-08-20 22:09:37 -0500901 bootpath = g_malloc(bootpathlen);
Blue Swirl4fd37a92010-12-19 14:05:43 +0000902 snprintf(bootpath, bootpathlen, "%s%s", devpath, i->suffix);
Anthony Liguori7267c092011-08-20 22:09:37 -0500903 g_free(devpath);
Gleb Natapov962630f2010-12-08 13:35:09 +0200904 } else if (devpath) {
905 bootpath = devpath;
906 } else {
Anthony Liguori7267c092011-08-20 22:09:37 -0500907 bootpath = g_strdup(i->suffix);
Gleb Natapov962630f2010-12-08 13:35:09 +0200908 assert(bootpath);
909 }
910
911 if (total) {
912 list[total-1] = '\n';
913 }
914 len = strlen(bootpath) + 1;
Anthony Liguori7267c092011-08-20 22:09:37 -0500915 list = g_realloc(list, total + len);
Gleb Natapov962630f2010-12-08 13:35:09 +0200916 memcpy(&list[total], bootpath, len);
917 total += len;
Anthony Liguori7267c092011-08-20 22:09:37 -0500918 g_free(bootpath);
Gleb Natapov962630f2010-12-08 13:35:09 +0200919 }
920
921 *size = total;
922
923 return list;
924}
925
aliguori268a3622009-04-21 22:30:27 +0000926static void numa_add(const char *optarg)
927{
928 char option[128];
929 char *endptr;
930 unsigned long long value, endvalue;
931 int nodenr;
932
933 optarg = get_opt_name(option, 128, optarg, ',') + 1;
934 if (!strcmp(option, "node")) {
935 if (get_param_value(option, 128, "nodeid", optarg) == 0) {
936 nodenr = nb_numa_nodes;
937 } else {
938 nodenr = strtoull(option, NULL, 10);
939 }
940
941 if (get_param_value(option, 128, "mem", optarg) == 0) {
942 node_mem[nodenr] = 0;
943 } else {
Jes Sorensen70b4f4b2011-01-05 11:41:02 +0100944 int64_t sval;
Jes Sorensen9f9b17a2010-10-21 17:15:46 +0200945 sval = strtosz(option, NULL);
946 if (sval < 0) {
947 fprintf(stderr, "qemu: invalid numa mem size: %s\n", optarg);
948 exit(1);
aliguori268a3622009-04-21 22:30:27 +0000949 }
Jes Sorensen9f9b17a2010-10-21 17:15:46 +0200950 node_mem[nodenr] = sval;
aliguori268a3622009-04-21 22:30:27 +0000951 }
952 if (get_param_value(option, 128, "cpus", optarg) == 0) {
953 node_cpumask[nodenr] = 0;
954 } else {
955 value = strtoull(option, &endptr, 10);
956 if (value >= 64) {
957 value = 63;
958 fprintf(stderr, "only 64 CPUs in NUMA mode supported.\n");
959 } else {
960 if (*endptr == '-') {
961 endvalue = strtoull(endptr+1, &endptr, 10);
962 if (endvalue >= 63) {
963 endvalue = 62;
964 fprintf(stderr,
965 "only 63 CPUs in NUMA mode supported.\n");
966 }
Paolo Bonzini0dfbd512010-02-04 14:31:50 +0100967 value = (2ULL << endvalue) - (1ULL << value);
aliguori268a3622009-04-21 22:30:27 +0000968 } else {
Paolo Bonzini0dfbd512010-02-04 14:31:50 +0100969 value = 1ULL << value;
aliguori268a3622009-04-21 22:30:27 +0000970 }
971 }
972 node_cpumask[nodenr] = value;
973 }
974 nb_numa_nodes++;
975 }
976 return;
977}
978
Andre Przywaradc6b1c02009-08-19 15:42:40 +0200979static void smp_parse(const char *optarg)
980{
981 int smp, sockets = 0, threads = 0, cores = 0;
982 char *endptr;
983 char option[128];
984
985 smp = strtoul(optarg, &endptr, 10);
986 if (endptr != optarg) {
987 if (*endptr == ',') {
988 endptr++;
989 }
990 }
991 if (get_param_value(option, 128, "sockets", endptr) != 0)
992 sockets = strtoull(option, NULL, 10);
993 if (get_param_value(option, 128, "cores", endptr) != 0)
994 cores = strtoull(option, NULL, 10);
995 if (get_param_value(option, 128, "threads", endptr) != 0)
996 threads = strtoull(option, NULL, 10);
997 if (get_param_value(option, 128, "maxcpus", endptr) != 0)
998 max_cpus = strtoull(option, NULL, 10);
999
1000 /* compute missing values, prefer sockets over cores over threads */
1001 if (smp == 0 || sockets == 0) {
1002 sockets = sockets > 0 ? sockets : 1;
1003 cores = cores > 0 ? cores : 1;
1004 threads = threads > 0 ? threads : 1;
1005 if (smp == 0) {
1006 smp = cores * threads * sockets;
Andre Przywaradc6b1c02009-08-19 15:42:40 +02001007 }
1008 } else {
1009 if (cores == 0) {
1010 threads = threads > 0 ? threads : 1;
1011 cores = smp / (sockets * threads);
1012 } else {
Joel Schoppdca98162010-07-21 15:05:17 -05001013 threads = smp / (cores * sockets);
Andre Przywaradc6b1c02009-08-19 15:42:40 +02001014 }
1015 }
1016 smp_cpus = smp;
1017 smp_cores = cores > 0 ? cores : 1;
1018 smp_threads = threads > 0 ? threads : 1;
1019 if (max_cpus == 0)
1020 max_cpus = smp_cpus;
1021}
1022
bellard330d0412003-07-26 18:11:40 +00001023/***********************************************************/
bellarda594cfb2005-11-06 16:13:29 +00001024/* USB devices */
1025
Markus Armbrusterfb080002010-05-28 15:38:44 +02001026static int usb_device_add(const char *devname)
bellarda594cfb2005-11-06 16:13:29 +00001027{
1028 const char *p;
Gerd Hoffmanna5d2f722009-08-31 14:24:00 +02001029 USBDevice *dev = NULL;
bellarda594cfb2005-11-06 16:13:29 +00001030
Gerd Hoffmanna5d2f722009-08-31 14:24:00 +02001031 if (!usb_enabled)
bellarda594cfb2005-11-06 16:13:29 +00001032 return -1;
1033
Gerd Hoffmann0958b4c2009-10-26 15:56:45 +01001034 /* drivers with .usbdevice_name entry in USBDeviceInfo */
1035 dev = usbdevice_create(devname);
1036 if (dev)
1037 goto done;
1038
Gerd Hoffmanna5d2f722009-08-31 14:24:00 +02001039 /* the other ones */
Gerd Hoffmanne447fc62011-06-01 14:41:59 +02001040#ifndef CONFIG_LINUX
1041 /* only the linux version is qdev-ified, usb-bsd still needs this */
bellarda594cfb2005-11-06 16:13:29 +00001042 if (strstart(devname, "host:", &p)) {
1043 dev = usb_host_device_open(p);
Gerd Hoffmanne447fc62011-06-01 14:41:59 +02001044 } else
1045#endif
1046 if (!strcmp(devname, "bt") || strstart(devname, "bt:", &p)) {
balrogdc72ac12008-11-09 00:04:26 +00001047 dev = usb_bt_init(devname[2] ? hci_init(p) :
1048 bt_new_hci(qemu_find_bt_vlan(0)));
bellarda594cfb2005-11-06 16:13:29 +00001049 } else {
1050 return -1;
1051 }
pbrook0d92ed32006-05-21 16:30:15 +00001052 if (!dev)
1053 return -1;
1054
Gerd Hoffmanna5d2f722009-08-31 14:24:00 +02001055done:
bellarda594cfb2005-11-06 16:13:29 +00001056 return 0;
1057}
1058
aliguori1f3870a2008-08-21 19:27:48 +00001059static int usb_device_del(const char *devname)
1060{
1061 int bus_num, addr;
1062 const char *p;
1063
aliguori5d0c5752008-09-14 01:07:41 +00001064 if (strstart(devname, "host:", &p))
1065 return usb_host_device_close(p);
1066
Gerd Hoffmanna5d2f722009-08-31 14:24:00 +02001067 if (!usb_enabled)
aliguori1f3870a2008-08-21 19:27:48 +00001068 return -1;
1069
1070 p = strchr(devname, '.');
1071 if (!p)
1072 return -1;
1073 bus_num = strtoul(devname, NULL, 0);
1074 addr = strtoul(p + 1, NULL, 0);
1075
Gerd Hoffmanna5d2f722009-08-31 14:24:00 +02001076 return usb_device_delete_addr(bus_num, addr);
aliguori1f3870a2008-08-21 19:27:48 +00001077}
1078
Gerd Hoffmannbd3c9482009-07-15 13:59:26 +02001079static int usb_parse(const char *cmdline)
1080{
Scott Tsai59d1c1c2009-12-23 04:30:18 +08001081 int r;
Markus Armbrusterfb080002010-05-28 15:38:44 +02001082 r = usb_device_add(cmdline);
Scott Tsai59d1c1c2009-12-23 04:30:18 +08001083 if (r < 0) {
1084 fprintf(stderr, "qemu: could not add USB device '%s'\n", cmdline);
1085 }
1086 return r;
Gerd Hoffmannbd3c9482009-07-15 13:59:26 +02001087}
1088
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001089void do_usb_add(Monitor *mon, const QDict *qdict)
bellarda594cfb2005-11-06 16:13:29 +00001090{
Scott Tsai59d1c1c2009-12-23 04:30:18 +08001091 const char *devname = qdict_get_str(qdict, "devname");
Markus Armbrusterfb080002010-05-28 15:38:44 +02001092 if (usb_device_add(devname) < 0) {
Markus Armbruster1ecda022010-02-18 17:25:24 +01001093 error_report("could not add USB device '%s'", devname);
Scott Tsai59d1c1c2009-12-23 04:30:18 +08001094 }
bellarda594cfb2005-11-06 16:13:29 +00001095}
1096
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001097void do_usb_del(Monitor *mon, const QDict *qdict)
bellarda594cfb2005-11-06 16:13:29 +00001098{
Scott Tsai59d1c1c2009-12-23 04:30:18 +08001099 const char *devname = qdict_get_str(qdict, "devname");
1100 if (usb_device_del(devname) < 0) {
Markus Armbruster1ecda022010-02-18 17:25:24 +01001101 error_report("could not delete USB device '%s'", devname);
Scott Tsai59d1c1c2009-12-23 04:30:18 +08001102 }
bellarda594cfb2005-11-06 16:13:29 +00001103}
1104
bellardf7cce892004-12-08 22:21:25 +00001105/***********************************************************/
balrog201a51f2007-04-30 00:51:09 +00001106/* PCMCIA/Cardbus */
1107
1108static struct pcmcia_socket_entry_s {
Paul Brookbc24a222009-05-10 01:44:56 +01001109 PCMCIASocket *socket;
balrog201a51f2007-04-30 00:51:09 +00001110 struct pcmcia_socket_entry_s *next;
1111} *pcmcia_sockets = 0;
1112
Paul Brookbc24a222009-05-10 01:44:56 +01001113void pcmcia_socket_register(PCMCIASocket *socket)
balrog201a51f2007-04-30 00:51:09 +00001114{
1115 struct pcmcia_socket_entry_s *entry;
1116
Anthony Liguori7267c092011-08-20 22:09:37 -05001117 entry = g_malloc(sizeof(struct pcmcia_socket_entry_s));
balrog201a51f2007-04-30 00:51:09 +00001118 entry->socket = socket;
1119 entry->next = pcmcia_sockets;
1120 pcmcia_sockets = entry;
1121}
1122
Paul Brookbc24a222009-05-10 01:44:56 +01001123void pcmcia_socket_unregister(PCMCIASocket *socket)
balrog201a51f2007-04-30 00:51:09 +00001124{
1125 struct pcmcia_socket_entry_s *entry, **ptr;
1126
1127 ptr = &pcmcia_sockets;
1128 for (entry = *ptr; entry; ptr = &entry->next, entry = *ptr)
1129 if (entry->socket == socket) {
1130 *ptr = entry->next;
Anthony Liguori7267c092011-08-20 22:09:37 -05001131 g_free(entry);
balrog201a51f2007-04-30 00:51:09 +00001132 }
1133}
1134
aliguori376253e2009-03-05 23:01:23 +00001135void pcmcia_info(Monitor *mon)
balrog201a51f2007-04-30 00:51:09 +00001136{
1137 struct pcmcia_socket_entry_s *iter;
aliguori376253e2009-03-05 23:01:23 +00001138
balrog201a51f2007-04-30 00:51:09 +00001139 if (!pcmcia_sockets)
aliguori376253e2009-03-05 23:01:23 +00001140 monitor_printf(mon, "No PCMCIA sockets\n");
balrog201a51f2007-04-30 00:51:09 +00001141
1142 for (iter = pcmcia_sockets; iter; iter = iter->next)
aliguori376253e2009-03-05 23:01:23 +00001143 monitor_printf(mon, "%s: %s\n", iter->socket->slot_string,
1144 iter->socket->attached ? iter->socket->card_string :
1145 "Empty");
balrog201a51f2007-04-30 00:51:09 +00001146}
1147
1148/***********************************************************/
bellardcc1daa42005-06-05 14:49:17 +00001149/* machine registration */
1150
blueswir1bdaf78e2008-10-04 07:24:27 +00001151static QEMUMachine *first_machine = NULL;
aliguori6f338c32009-02-11 15:21:54 +00001152QEMUMachine *current_machine = NULL;
bellardcc1daa42005-06-05 14:49:17 +00001153
1154int qemu_register_machine(QEMUMachine *m)
1155{
1156 QEMUMachine **pm;
1157 pm = &first_machine;
1158 while (*pm != NULL)
1159 pm = &(*pm)->next;
1160 m->next = NULL;
1161 *pm = m;
1162 return 0;
1163}
1164
pbrook9596ebb2007-11-18 01:44:38 +00001165static QEMUMachine *find_machine(const char *name)
bellardcc1daa42005-06-05 14:49:17 +00001166{
1167 QEMUMachine *m;
1168
1169 for(m = first_machine; m != NULL; m = m->next) {
1170 if (!strcmp(m->name, name))
1171 return m;
Mark McLoughlin3f6599e2009-07-22 10:02:50 +01001172 if (m->alias && !strcmp(m->alias, name))
1173 return m;
bellardcc1daa42005-06-05 14:49:17 +00001174 }
1175 return NULL;
1176}
1177
Anthony Liguori0c257432009-05-21 20:41:01 -05001178static QEMUMachine *find_default_machine(void)
1179{
1180 QEMUMachine *m;
1181
1182 for(m = first_machine; m != NULL; m = m->next) {
1183 if (m->is_default) {
1184 return m;
1185 }
1186 }
1187 return NULL;
1188}
1189
bellardcc1daa42005-06-05 14:49:17 +00001190/***********************************************************/
bellard8a7ddc32004-03-31 19:00:16 +00001191/* main execution loop */
1192
pbrook9596ebb2007-11-18 01:44:38 +00001193static void gui_update(void *opaque)
bellard8a7ddc32004-03-31 19:00:16 +00001194{
aliguori7d957bd2009-01-15 22:14:11 +00001195 uint64_t interval = GUI_REFRESH_INTERVAL;
ths740733b2007-06-08 01:57:56 +00001196 DisplayState *ds = opaque;
aliguori7d957bd2009-01-15 22:14:11 +00001197 DisplayChangeListener *dcl = ds->listeners;
1198
Sheng Yang62a27442010-01-26 19:21:16 +08001199 qemu_flush_coalesced_mmio_buffer();
aliguori7d957bd2009-01-15 22:14:11 +00001200 dpy_refresh(ds);
1201
1202 while (dcl != NULL) {
1203 if (dcl->gui_timer_interval &&
1204 dcl->gui_timer_interval < interval)
1205 interval = dcl->gui_timer_interval;
1206 dcl = dcl->next;
1207 }
Paolo Bonzini7bd427d2011-03-11 16:47:48 +01001208 qemu_mod_timer(ds->gui_timer, interval + qemu_get_clock_ms(rt_clock));
bellard8a7ddc32004-03-31 19:00:16 +00001209}
1210
blueswir19043b622009-01-21 19:28:13 +00001211static void nographic_update(void *opaque)
1212{
1213 uint64_t interval = GUI_REFRESH_INTERVAL;
1214
Sheng Yang62a27442010-01-26 19:21:16 +08001215 qemu_flush_coalesced_mmio_buffer();
Paolo Bonzini7bd427d2011-03-11 16:47:48 +01001216 qemu_mod_timer(nographic_timer, interval + qemu_get_clock_ms(rt_clock));
blueswir19043b622009-01-21 19:28:13 +00001217}
1218
bellard0bd48852005-11-11 00:00:47 +00001219struct vm_change_state_entry {
1220 VMChangeStateHandler *cb;
1221 void *opaque;
Blue Swirl72cf2d42009-09-12 07:36:22 +00001222 QLIST_ENTRY (vm_change_state_entry) entries;
bellard0bd48852005-11-11 00:00:47 +00001223};
1224
Blue Swirl72cf2d42009-09-12 07:36:22 +00001225static QLIST_HEAD(vm_change_state_head, vm_change_state_entry) vm_change_state_head;
bellard0bd48852005-11-11 00:00:47 +00001226
1227VMChangeStateEntry *qemu_add_vm_change_state_handler(VMChangeStateHandler *cb,
1228 void *opaque)
1229{
1230 VMChangeStateEntry *e;
1231
Anthony Liguori7267c092011-08-20 22:09:37 -05001232 e = g_malloc0(sizeof (*e));
bellard0bd48852005-11-11 00:00:47 +00001233
1234 e->cb = cb;
1235 e->opaque = opaque;
Blue Swirl72cf2d42009-09-12 07:36:22 +00001236 QLIST_INSERT_HEAD(&vm_change_state_head, e, entries);
bellard0bd48852005-11-11 00:00:47 +00001237 return e;
1238}
1239
1240void qemu_del_vm_change_state_handler(VMChangeStateEntry *e)
1241{
Blue Swirl72cf2d42009-09-12 07:36:22 +00001242 QLIST_REMOVE (e, entries);
Anthony Liguori7267c092011-08-20 22:09:37 -05001243 g_free (e);
bellard0bd48852005-11-11 00:00:47 +00001244}
1245
Luiz Capitulino1dfb4dd2011-07-29 14:26:33 -03001246void vm_state_notify(int running, RunState state)
bellard0bd48852005-11-11 00:00:47 +00001247{
1248 VMChangeStateEntry *e;
1249
Luiz Capitulino1dfb4dd2011-07-29 14:26:33 -03001250 trace_vm_state_notify(running, state);
Stefan Hajnoczi94b0b5f2010-11-16 12:20:25 +00001251
bellard0bd48852005-11-11 00:00:47 +00001252 for (e = vm_change_state_head.lh_first; e; e = e->entries.le_next) {
Luiz Capitulino1dfb4dd2011-07-29 14:26:33 -03001253 e->cb(e->opaque, running, state);
bellard0bd48852005-11-11 00:00:47 +00001254 }
1255}
1256
bellard8a7ddc32004-03-31 19:00:16 +00001257void vm_start(void)
1258{
Luiz Capitulino13548692011-07-29 15:36:43 -03001259 if (!runstate_is_running()) {
bellard8a7ddc32004-03-31 19:00:16 +00001260 cpu_enable_ticks();
Luiz Capitulino0461d5a2011-09-30 14:45:27 -03001261 runstate_set(RUN_STATE_RUNNING);
1262 vm_state_notify(1, RUN_STATE_RUNNING);
aliguorid6dc3d42009-04-24 18:04:07 +00001263 resume_all_vcpus();
Luiz Capitulino6ed2c482010-04-27 20:35:59 -03001264 monitor_protocol_event(QEVENT_RESUME, NULL);
bellard8a7ddc32004-03-31 19:00:16 +00001265 }
1266}
1267
bellardbb0c6722004-06-20 12:37:32 +00001268/* reset/shutdown handler */
1269
1270typedef struct QEMUResetEntry {
Blue Swirl72cf2d42009-09-12 07:36:22 +00001271 QTAILQ_ENTRY(QEMUResetEntry) entry;
bellardbb0c6722004-06-20 12:37:32 +00001272 QEMUResetHandler *func;
1273 void *opaque;
bellardbb0c6722004-06-20 12:37:32 +00001274} QEMUResetEntry;
1275
Blue Swirl72cf2d42009-09-12 07:36:22 +00001276static QTAILQ_HEAD(reset_handlers, QEMUResetEntry) reset_handlers =
1277 QTAILQ_HEAD_INITIALIZER(reset_handlers);
bellardbb0c6722004-06-20 12:37:32 +00001278static int reset_requested;
Gleb Natapovf64622c2011-03-15 13:56:04 +02001279static int shutdown_requested, shutdown_signal = -1;
1280static pid_t shutdown_pid;
bellard34751872005-07-02 14:31:34 +00001281static int powerdown_requested;
Jan Kiszka8cf71712011-02-07 12:19:16 +01001282static int debug_requested;
Luiz Capitulino0461d5a2011-09-30 14:45:27 -03001283static RunState vmstop_requested = RUN_STATE_MAX;
bellardbb0c6722004-06-20 12:37:32 +00001284
Anthony PERARD1291eb32010-07-22 15:52:48 +01001285int qemu_shutdown_requested_get(void)
1286{
1287 return shutdown_requested;
1288}
1289
1290int qemu_reset_requested_get(void)
1291{
1292 return reset_requested;
1293}
1294
aurel32cf7a2fe2008-03-18 06:53:05 +00001295int qemu_shutdown_requested(void)
1296{
1297 int r = shutdown_requested;
1298 shutdown_requested = 0;
1299 return r;
1300}
1301
Gleb Natapovf64622c2011-03-15 13:56:04 +02001302void qemu_kill_report(void)
1303{
1304 if (shutdown_signal != -1) {
Peter Maydellf1d3fb02011-03-30 22:03:38 +01001305 fprintf(stderr, "qemu: terminating on signal %d", shutdown_signal);
1306 if (shutdown_pid == 0) {
1307 /* This happens for eg ^C at the terminal, so it's worth
1308 * avoiding printing an odd message in that case.
1309 */
1310 fputc('\n', stderr);
1311 } else {
Andreas Färber953ffe02011-06-02 19:58:06 +02001312 fprintf(stderr, " from pid " FMT_pid "\n", shutdown_pid);
Peter Maydellf1d3fb02011-03-30 22:03:38 +01001313 }
Gleb Natapovf64622c2011-03-15 13:56:04 +02001314 shutdown_signal = -1;
1315 }
1316}
1317
aurel32cf7a2fe2008-03-18 06:53:05 +00001318int qemu_reset_requested(void)
1319{
1320 int r = reset_requested;
1321 reset_requested = 0;
1322 return r;
1323}
1324
1325int qemu_powerdown_requested(void)
1326{
1327 int r = powerdown_requested;
1328 powerdown_requested = 0;
1329 return r;
1330}
1331
aliguorie5689022009-04-24 18:03:54 +00001332static int qemu_debug_requested(void)
1333{
1334 int r = debug_requested;
1335 debug_requested = 0;
1336 return r;
1337}
1338
Luiz Capitulino0461d5a2011-09-30 14:45:27 -03001339/* We use RUN_STATE_MAX but any invalid value will do */
Luiz Capitulino0d45b702011-09-30 14:27:11 -03001340static bool qemu_vmstop_requested(RunState *r)
aliguori6e29f5d2009-04-24 18:04:02 +00001341{
Luiz Capitulino0461d5a2011-09-30 14:45:27 -03001342 if (vmstop_requested < RUN_STATE_MAX) {
Luiz Capitulino0d45b702011-09-30 14:27:11 -03001343 *r = vmstop_requested;
Luiz Capitulino0461d5a2011-09-30 14:45:27 -03001344 vmstop_requested = RUN_STATE_MAX;
Luiz Capitulino0d45b702011-09-30 14:27:11 -03001345 return true;
1346 }
1347
1348 return false;
aliguori6e29f5d2009-04-24 18:04:02 +00001349}
1350
Jan Kiszkaa08d4362009-06-27 09:25:07 +02001351void qemu_register_reset(QEMUResetHandler *func, void *opaque)
bellardbb0c6722004-06-20 12:37:32 +00001352{
Anthony Liguori7267c092011-08-20 22:09:37 -05001353 QEMUResetEntry *re = g_malloc0(sizeof(QEMUResetEntry));
bellardbb0c6722004-06-20 12:37:32 +00001354
bellardbb0c6722004-06-20 12:37:32 +00001355 re->func = func;
1356 re->opaque = opaque;
Blue Swirl72cf2d42009-09-12 07:36:22 +00001357 QTAILQ_INSERT_TAIL(&reset_handlers, re, entry);
bellardbb0c6722004-06-20 12:37:32 +00001358}
1359
Jan Kiszkadda9b292009-07-02 00:19:02 +02001360void qemu_unregister_reset(QEMUResetHandler *func, void *opaque)
bellardbb0c6722004-06-20 12:37:32 +00001361{
1362 QEMUResetEntry *re;
1363
Blue Swirl72cf2d42009-09-12 07:36:22 +00001364 QTAILQ_FOREACH(re, &reset_handlers, entry) {
Jan Kiszkadda9b292009-07-02 00:19:02 +02001365 if (re->func == func && re->opaque == opaque) {
Blue Swirl72cf2d42009-09-12 07:36:22 +00001366 QTAILQ_REMOVE(&reset_handlers, re, entry);
Anthony Liguori7267c092011-08-20 22:09:37 -05001367 g_free(re);
Jan Kiszkadda9b292009-07-02 00:19:02 +02001368 return;
1369 }
1370 }
1371}
1372
Jan Kiszkae063eb12011-06-14 18:29:43 +02001373void qemu_system_reset(bool report)
Jan Kiszkadda9b292009-07-02 00:19:02 +02001374{
1375 QEMUResetEntry *re, *nre;
1376
1377 /* reset all devices */
Blue Swirl72cf2d42009-09-12 07:36:22 +00001378 QTAILQ_FOREACH_SAFE(re, &reset_handlers, entry, nre) {
bellardbb0c6722004-06-20 12:37:32 +00001379 re->func(re->opaque);
1380 }
Jan Kiszkae063eb12011-06-14 18:29:43 +02001381 if (report) {
1382 monitor_protocol_event(QEVENT_RESET, NULL);
1383 }
Jan Kiszkaea375f92010-03-01 19:10:30 +01001384 cpu_synchronize_all_post_reset();
bellardbb0c6722004-06-20 12:37:32 +00001385}
1386
1387void qemu_system_reset_request(void)
1388{
bellardd1beab82006-10-02 19:44:22 +00001389 if (no_reboot) {
1390 shutdown_requested = 1;
1391 } else {
1392 reset_requested = 1;
1393 }
Jan Kiszkab4a3d962011-02-01 22:15:43 +01001394 cpu_stop_current();
aliguorid9f75a42009-04-24 18:03:11 +00001395 qemu_notify_event();
bellardbb0c6722004-06-20 12:37:32 +00001396}
1397
Gleb Natapovf64622c2011-03-15 13:56:04 +02001398void qemu_system_killed(int signal, pid_t pid)
1399{
1400 shutdown_signal = signal;
1401 shutdown_pid = pid;
Kevin Wolfd9389b92011-09-14 15:38:40 +02001402 no_shutdown = 0;
Gleb Natapovf64622c2011-03-15 13:56:04 +02001403 qemu_system_shutdown_request();
1404}
1405
bellardbb0c6722004-06-20 12:37:32 +00001406void qemu_system_shutdown_request(void)
1407{
1408 shutdown_requested = 1;
aliguorid9f75a42009-04-24 18:03:11 +00001409 qemu_notify_event();
bellardbb0c6722004-06-20 12:37:32 +00001410}
1411
bellard34751872005-07-02 14:31:34 +00001412void qemu_system_powerdown_request(void)
1413{
1414 powerdown_requested = 1;
aliguorid9f75a42009-04-24 18:03:11 +00001415 qemu_notify_event();
1416}
1417
Jan Kiszka8cf71712011-02-07 12:19:16 +01001418void qemu_system_debug_request(void)
1419{
1420 debug_requested = 1;
Jan Kiszka83f338f2011-02-07 12:19:17 +01001421 qemu_notify_event();
Jan Kiszka8cf71712011-02-07 12:19:16 +01001422}
1423
Luiz Capitulino1dfb4dd2011-07-29 14:26:33 -03001424void qemu_system_vmstop_request(RunState state)
Jan Kiszka8cf71712011-02-07 12:19:16 +01001425{
Luiz Capitulino1dfb4dd2011-07-29 14:26:33 -03001426 vmstop_requested = state;
Jan Kiszka8cf71712011-02-07 12:19:16 +01001427 qemu_notify_event();
1428}
1429
Anthony Liguori69e5bb62011-08-22 08:12:52 -05001430static GPollFD poll_fds[1024 * 2]; /* this is probably overkill */
1431static int n_poll_fds;
1432static int max_priority;
1433
1434static void glib_select_fill(int *max_fd, fd_set *rfds, fd_set *wfds,
1435 fd_set *xfds, struct timeval *tv)
1436{
1437 GMainContext *context = g_main_context_default();
1438 int i;
1439 int timeout = 0, cur_timeout;
1440
1441 g_main_context_prepare(context, &max_priority);
1442
1443 n_poll_fds = g_main_context_query(context, max_priority, &timeout,
1444 poll_fds, ARRAY_SIZE(poll_fds));
1445 g_assert(n_poll_fds <= ARRAY_SIZE(poll_fds));
1446
1447 for (i = 0; i < n_poll_fds; i++) {
1448 GPollFD *p = &poll_fds[i];
1449
1450 if ((p->events & G_IO_IN)) {
1451 FD_SET(p->fd, rfds);
1452 *max_fd = MAX(*max_fd, p->fd);
1453 }
1454 if ((p->events & G_IO_OUT)) {
1455 FD_SET(p->fd, wfds);
1456 *max_fd = MAX(*max_fd, p->fd);
1457 }
1458 if ((p->events & G_IO_ERR)) {
1459 FD_SET(p->fd, xfds);
1460 *max_fd = MAX(*max_fd, p->fd);
1461 }
1462 }
1463
1464 cur_timeout = (tv->tv_sec * 1000) + ((tv->tv_usec + 500) / 1000);
1465 if (timeout >= 0 && timeout < cur_timeout) {
1466 tv->tv_sec = timeout / 1000;
1467 tv->tv_usec = (timeout % 1000) * 1000;
1468 }
1469}
1470
1471static void glib_select_poll(fd_set *rfds, fd_set *wfds, fd_set *xfds,
1472 bool err)
1473{
1474 GMainContext *context = g_main_context_default();
1475
1476 if (!err) {
1477 int i;
1478
1479 for (i = 0; i < n_poll_fds; i++) {
1480 GPollFD *p = &poll_fds[i];
1481
1482 if ((p->events & G_IO_IN) && FD_ISSET(p->fd, rfds)) {
1483 p->revents |= G_IO_IN;
1484 }
1485 if ((p->events & G_IO_OUT) && FD_ISSET(p->fd, wfds)) {
1486 p->revents |= G_IO_OUT;
1487 }
1488 if ((p->events & G_IO_ERR) && FD_ISSET(p->fd, xfds)) {
1489 p->revents |= G_IO_ERR;
1490 }
1491 }
1492 }
1493
1494 if (g_main_context_check(context, max_priority, poll_fds, n_poll_fds)) {
1495 g_main_context_dispatch(context);
1496 }
1497}
1498
Jan Kiszkac9f711a2011-08-22 17:46:02 +02001499int main_loop_wait(int nonblocking)
aliguori56f3a5d2008-10-31 18:07:17 +00001500{
aliguori56f3a5d2008-10-31 18:07:17 +00001501 fd_set rfds, wfds, xfds;
1502 int ret, nfds;
1503 struct timeval tv;
Paolo Bonzinid6f4ade2010-03-10 11:38:54 +01001504 int timeout;
aliguori56f3a5d2008-10-31 18:07:17 +00001505
Paolo Bonzinid6f4ade2010-03-10 11:38:54 +01001506 if (nonblocking)
1507 timeout = 0;
1508 else {
1509 timeout = qemu_calculate_timeout();
1510 qemu_bh_update_timeout(&timeout);
1511 }
aliguori56f3a5d2008-10-31 18:07:17 +00001512
Jes Sorensen0d93ca72010-06-10 11:42:18 +02001513 os_host_main_loop_wait(&timeout);
aliguori56f3a5d2008-10-31 18:07:17 +00001514
Paolo Bonzini02981412011-03-09 18:21:09 +01001515 tv.tv_sec = timeout / 1000;
1516 tv.tv_usec = (timeout % 1000) * 1000;
1517
bellardfd1dff42006-02-01 21:29:26 +00001518 /* poll any events */
1519 /* XXX: separate device handlers from system ones */
aliguori6abfbd72008-11-05 20:49:37 +00001520 nfds = -1;
bellardfd1dff42006-02-01 21:29:26 +00001521 FD_ZERO(&rfds);
1522 FD_ZERO(&wfds);
bellarde0356492006-05-01 13:33:02 +00001523 FD_ZERO(&xfds);
Anthony Liguori69e5bb62011-08-22 08:12:52 -05001524
Paolo Bonzini02981412011-03-09 18:21:09 +01001525 qemu_iohandler_fill(&nfds, &rfds, &wfds, &xfds);
Jan Kiszkad918f232009-06-24 14:42:30 +02001526 slirp_select_fill(&nfds, &rfds, &wfds, &xfds);
Anthony Liguori69e5bb62011-08-22 08:12:52 -05001527 glib_select_fill(&nfds, &rfds, &wfds, &xfds, &tv);
Jan Kiszkad918f232009-06-24 14:42:30 +02001528
Jan Kiszka200668b2011-08-22 17:46:01 +02001529 if (timeout > 0) {
1530 qemu_mutex_unlock_iothread();
1531 }
1532
bellarde0356492006-05-01 13:33:02 +00001533 ret = select(nfds + 1, &rfds, &wfds, &xfds, &tv);
Jan Kiszka200668b2011-08-22 17:46:01 +02001534
1535 if (timeout > 0) {
1536 qemu_mutex_lock_iothread();
1537 }
thscafffd42007-02-28 21:59:44 +00001538
Paolo Bonzini02981412011-03-09 18:21:09 +01001539 qemu_iohandler_poll(&rfds, &wfds, &xfds, ret);
Jan Kiszkad918f232009-06-24 14:42:30 +02001540 slirp_select_poll(&rfds, &wfds, &xfds, (ret < 0));
Anthony Liguori69e5bb62011-08-22 08:12:52 -05001541 glib_select_poll(&rfds, &wfds, &xfds, (ret < 0));
bellardc20709a2004-04-21 23:27:19 +00001542
Paolo Bonzinib6964822010-03-10 11:38:45 +01001543 qemu_run_all_timers();
Jan Kiszka21d5d122009-09-15 13:36:04 +02001544
pbrook423f0742007-05-23 00:06:54 +00001545 /* Check bottom-halves last in case any of the earlier events triggered
1546 them. */
1547 qemu_bh_poll();
ths3b46e622007-09-17 08:09:54 +00001548
Jan Kiszkac9f711a2011-08-22 17:46:02 +02001549 return ret;
bellard5905b2e2004-08-01 21:53:26 +00001550}
1551
Blue Swirld9c32312009-08-09 08:42:19 +00001552qemu_irq qemu_system_powerdown;
1553
aliguori43b96852009-04-24 18:03:33 +00001554static void main_loop(void)
1555{
Jan Kiszkac9f711a2011-08-22 17:46:02 +02001556 bool nonblocking;
1557 int last_io __attribute__ ((unused)) = 0;
Jan Kiszka8e1b90e2011-02-01 22:15:46 +01001558#ifdef CONFIG_PROFILER
1559 int64_t ti;
1560#endif
Luiz Capitulino0d45b702011-09-30 14:27:11 -03001561 RunState r;
aliguori43b96852009-04-24 18:03:33 +00001562
Blue Swirl7277e022010-04-12 17:19:06 +00001563 qemu_main_loop_start();
aliguorid6dc3d42009-04-24 18:04:07 +00001564
aliguori6e29f5d2009-04-24 18:04:02 +00001565 for (;;) {
Jan Kiszkac9f711a2011-08-22 17:46:02 +02001566 nonblocking = !kvm_enabled() && last_io > 0;
aliguori43b96852009-04-24 18:03:33 +00001567#ifdef CONFIG_PROFILER
Jan Kiszka46481d32011-02-01 22:15:47 +01001568 ti = profile_getclock();
aliguori43b96852009-04-24 18:03:33 +00001569#endif
Jan Kiszkac9f711a2011-08-22 17:46:02 +02001570 last_io = main_loop_wait(nonblocking);
aliguori43b96852009-04-24 18:03:33 +00001571#ifdef CONFIG_PROFILER
Jan Kiszka46481d32011-02-01 22:15:47 +01001572 dev_time += profile_getclock() - ti;
aliguori43b96852009-04-24 18:03:33 +00001573#endif
aliguori43b96852009-04-24 18:03:33 +00001574
Jan Kiszka8cf71712011-02-07 12:19:16 +01001575 if (qemu_debug_requested()) {
Luiz Capitulino0461d5a2011-09-30 14:45:27 -03001576 vm_stop(RUN_STATE_DEBUG);
Luiz Capitulinob1a15e72009-11-26 22:59:04 -02001577 }
aliguori43b96852009-04-24 18:03:33 +00001578 if (qemu_shutdown_requested()) {
Gleb Natapovf64622c2011-03-15 13:56:04 +02001579 qemu_kill_report();
Blue Swirl242cd002009-12-04 18:05:45 +00001580 monitor_protocol_event(QEVENT_SHUTDOWN, NULL);
aliguori43b96852009-04-24 18:03:33 +00001581 if (no_shutdown) {
Luiz Capitulino0461d5a2011-09-30 14:45:27 -03001582 vm_stop(RUN_STATE_SHUTDOWN);
aliguori43b96852009-04-24 18:03:33 +00001583 } else
1584 break;
1585 }
aliguorid6dc3d42009-04-24 18:04:07 +00001586 if (qemu_reset_requested()) {
1587 pause_all_vcpus();
Jan Kiszkaa7ada152011-03-02 08:56:11 +01001588 cpu_synchronize_all_states();
Jan Kiszkae063eb12011-06-14 18:29:43 +02001589 qemu_system_reset(VMRESET_REPORT);
aliguorid6dc3d42009-04-24 18:04:07 +00001590 resume_all_vcpus();
Luiz Capitulino0461d5a2011-09-30 14:45:27 -03001591 if (runstate_check(RUN_STATE_INTERNAL_ERROR) ||
1592 runstate_check(RUN_STATE_SHUTDOWN)) {
1593 runstate_set(RUN_STATE_PAUSED);
Luiz Capitulino6667b232011-07-29 15:57:54 -03001594 }
aliguorid6dc3d42009-04-24 18:04:07 +00001595 }
Blue Swirld9c32312009-08-09 08:42:19 +00001596 if (qemu_powerdown_requested()) {
Blue Swirl242cd002009-12-04 18:05:45 +00001597 monitor_protocol_event(QEVENT_POWERDOWN, NULL);
Blue Swirld9c32312009-08-09 08:42:19 +00001598 qemu_irq_raise(qemu_system_powerdown);
1599 }
Luiz Capitulino0d45b702011-09-30 14:27:11 -03001600 if (qemu_vmstop_requested(&r)) {
aliguori6e29f5d2009-04-24 18:04:02 +00001601 vm_stop(r);
Luiz Capitulinob1a15e72009-11-26 22:59:04 -02001602 }
aliguori43b96852009-04-24 18:03:33 +00001603 }
MORITA Kazutaka2bc93fe2010-05-28 11:44:57 +09001604 bdrv_close_all();
aliguorid6dc3d42009-04-24 18:04:07 +00001605 pause_all_vcpus();
bellardb4608c02003-06-27 17:34:32 +00001606}
1607
pbrook9bd7e6d2009-04-07 22:58:45 +00001608static void version(void)
1609{
Thomas Monjalonf75ca1a2010-04-28 14:42:01 +02001610 printf("QEMU emulator version " QEMU_VERSION QEMU_PKGVERSION ", Copyright (c) 2003-2008 Fabrice Bellard\n");
pbrook9bd7e6d2009-04-07 22:58:45 +00001611}
1612
ths15f82202007-06-29 23:26:08 +00001613static void help(int exitcode)
bellard0824d6f2003-06-24 13:42:40 +00001614{
Paolo Bonzinie8105eb2010-02-04 16:49:59 +01001615 const char *options_help =
Blue Swirlad960902010-03-29 19:23:52 +00001616#define DEF(option, opt_arg, opt_enum, opt_help, arch_mask) \
1617 opt_help
blueswir15824d652009-03-28 06:44:27 +00001618#define DEFHEADING(text) stringify(text) "\n"
Jes Sorensen9f167322010-06-10 11:42:24 +02001619#include "qemu-options.def"
blueswir15824d652009-03-28 06:44:27 +00001620#undef DEF
1621#undef DEFHEADING
1622#undef GEN_DOCS
Paolo Bonzinie8105eb2010-02-04 16:49:59 +01001623 ;
1624 version();
1625 printf("usage: %s [options] [disk_image]\n"
malc3f020d72010-02-08 12:04:56 +03001626 "\n"
Thomas Monjalonf75ca1a2010-04-28 14:42:01 +02001627 "'disk_image' is a raw hard disk image for IDE hard disk 0\n"
Paolo Bonzinie8105eb2010-02-04 16:49:59 +01001628 "\n"
1629 "%s\n"
malc3f020d72010-02-08 12:04:56 +03001630 "During emulation, the following keys are useful:\n"
1631 "ctrl-alt-f toggle full screen\n"
1632 "ctrl-alt-n switch to virtual console 'n'\n"
1633 "ctrl-alt toggle mouse and keyboard grab\n"
1634 "\n"
Paolo Bonzinie8105eb2010-02-04 16:49:59 +01001635 "When using -nographic, press 'ctrl-a h' to get some help.\n",
1636 "qemu",
1637 options_help);
ths15f82202007-06-29 23:26:08 +00001638 exit(exitcode);
bellard0824d6f2003-06-24 13:42:40 +00001639}
1640
bellardcd6f1162004-05-13 22:02:20 +00001641#define HAS_ARG 0x0001
1642
bellardcd6f1162004-05-13 22:02:20 +00001643typedef struct QEMUOption {
1644 const char *name;
1645 int flags;
1646 int index;
Blue Swirlad960902010-03-29 19:23:52 +00001647 uint32_t arch_mask;
bellardcd6f1162004-05-13 22:02:20 +00001648} QEMUOption;
1649
blueswir1dbed7e42008-10-01 19:38:09 +00001650static const QEMUOption qemu_options[] = {
Blue Swirlad960902010-03-29 19:23:52 +00001651 { "h", 0, QEMU_OPTION_h, QEMU_ARCH_ALL },
1652#define DEF(option, opt_arg, opt_enum, opt_help, arch_mask) \
1653 { option, opt_arg, opt_enum, arch_mask },
blueswir15824d652009-03-28 06:44:27 +00001654#define DEFHEADING(text)
Jes Sorensen9f167322010-06-10 11:42:24 +02001655#include "qemu-options.def"
blueswir15824d652009-03-28 06:44:27 +00001656#undef DEF
1657#undef DEFHEADING
1658#undef GEN_DOCS
bellardcd6f1162004-05-13 22:02:20 +00001659 { NULL },
bellardfc01f7e2003-06-30 10:03:06 +00001660};
malc3893c122008-09-28 00:42:05 +00001661static void select_vgahw (const char *p)
1662{
1663 const char *opts;
1664
Gerd Hoffmann64465292009-12-08 13:11:45 +01001665 default_vga = 0;
Zachary Amsden86176752009-07-30 00:15:02 -10001666 vga_interface_type = VGA_NONE;
malc3893c122008-09-28 00:42:05 +00001667 if (strstart(p, "std", &opts)) {
Zachary Amsden86176752009-07-30 00:15:02 -10001668 vga_interface_type = VGA_STD;
malc3893c122008-09-28 00:42:05 +00001669 } else if (strstart(p, "cirrus", &opts)) {
Zachary Amsden86176752009-07-30 00:15:02 -10001670 vga_interface_type = VGA_CIRRUS;
malc3893c122008-09-28 00:42:05 +00001671 } else if (strstart(p, "vmware", &opts)) {
Zachary Amsden86176752009-07-30 00:15:02 -10001672 vga_interface_type = VGA_VMWARE;
aliguori94909d92009-04-22 15:19:53 +00001673 } else if (strstart(p, "xenfb", &opts)) {
Zachary Amsden86176752009-07-30 00:15:02 -10001674 vga_interface_type = VGA_XENFB;
Gerd Hoffmanna19cbfb2010-04-27 11:50:11 +02001675 } else if (strstart(p, "qxl", &opts)) {
1676 vga_interface_type = VGA_QXL;
aliguori28b85ed2009-04-22 15:19:48 +00001677 } else if (!strstart(p, "none", &opts)) {
malc3893c122008-09-28 00:42:05 +00001678 invalid_vga:
1679 fprintf(stderr, "Unknown vga type: %s\n", p);
1680 exit(1);
1681 }
malccb5a7aa2008-09-28 00:42:12 +00001682 while (*opts) {
1683 const char *nextopt;
1684
1685 if (strstart(opts, ",retrace=", &nextopt)) {
1686 opts = nextopt;
1687 if (strstart(opts, "dumb", &nextopt))
1688 vga_retrace_method = VGA_RETRACE_DUMB;
1689 else if (strstart(opts, "precise", &nextopt))
1690 vga_retrace_method = VGA_RETRACE_PRECISE;
1691 else goto invalid_vga;
1692 } else goto invalid_vga;
1693 opts = nextopt;
1694 }
malc3893c122008-09-28 00:42:05 +00001695}
1696
Jes Sorensen1472a952011-03-16 13:33:31 +01001697static DisplayType select_display(const char *p)
1698{
1699 const char *opts;
1700 DisplayType display = DT_DEFAULT;
1701
1702 if (strstart(p, "sdl", &opts)) {
1703#ifdef CONFIG_SDL
1704 display = DT_SDL;
1705 while (*opts) {
1706 const char *nextopt;
1707
1708 if (strstart(opts, ",frame=", &nextopt)) {
1709 opts = nextopt;
1710 if (strstart(opts, "on", &nextopt)) {
1711 no_frame = 0;
1712 } else if (strstart(opts, "off", &nextopt)) {
1713 no_frame = 1;
1714 } else {
Peter Maydell05175532011-03-23 03:40:57 +00001715 goto invalid_sdl_args;
Jes Sorensen1472a952011-03-16 13:33:31 +01001716 }
1717 } else if (strstart(opts, ",alt_grab=", &nextopt)) {
1718 opts = nextopt;
1719 if (strstart(opts, "on", &nextopt)) {
1720 alt_grab = 1;
1721 } else if (strstart(opts, "off", &nextopt)) {
1722 alt_grab = 0;
1723 } else {
Peter Maydell05175532011-03-23 03:40:57 +00001724 goto invalid_sdl_args;
Jes Sorensen1472a952011-03-16 13:33:31 +01001725 }
1726 } else if (strstart(opts, ",ctrl_grab=", &nextopt)) {
1727 opts = nextopt;
1728 if (strstart(opts, "on", &nextopt)) {
1729 ctrl_grab = 1;
1730 } else if (strstart(opts, "off", &nextopt)) {
1731 ctrl_grab = 0;
1732 } else {
Peter Maydell05175532011-03-23 03:40:57 +00001733 goto invalid_sdl_args;
Jes Sorensen1472a952011-03-16 13:33:31 +01001734 }
1735 } else if (strstart(opts, ",window_close=", &nextopt)) {
1736 opts = nextopt;
1737 if (strstart(opts, "on", &nextopt)) {
1738 no_quit = 0;
1739 } else if (strstart(opts, "off", &nextopt)) {
1740 no_quit = 1;
1741 } else {
Peter Maydell05175532011-03-23 03:40:57 +00001742 goto invalid_sdl_args;
Jes Sorensen1472a952011-03-16 13:33:31 +01001743 }
1744 } else {
Peter Maydell05175532011-03-23 03:40:57 +00001745 invalid_sdl_args:
1746 fprintf(stderr, "Invalid SDL option string: %s\n", p);
1747 exit(1);
Jes Sorensen1472a952011-03-16 13:33:31 +01001748 }
1749 opts = nextopt;
1750 }
1751#else
1752 fprintf(stderr, "SDL support is disabled\n");
1753 exit(1);
1754#endif
Jes Sorensen3264ff12011-03-16 13:33:33 +01001755 } else if (strstart(p, "vnc", &opts)) {
Jes Sorensen821601e2011-03-16 13:33:36 +01001756#ifdef CONFIG_VNC
Jes Sorensen3264ff12011-03-16 13:33:33 +01001757 display_remote++;
1758
1759 if (*opts) {
1760 const char *nextopt;
1761
1762 if (strstart(opts, "=", &nextopt)) {
1763 vnc_display = nextopt;
1764 }
1765 }
1766 if (!vnc_display) {
1767 fprintf(stderr, "VNC requires a display argument vnc=<display>\n");
1768 exit(1);
1769 }
Jes Sorensen821601e2011-03-16 13:33:36 +01001770#else
1771 fprintf(stderr, "VNC support is disabled\n");
1772 exit(1);
1773#endif
Jes Sorensen1472a952011-03-16 13:33:31 +01001774 } else if (strstart(p, "curses", &opts)) {
1775#ifdef CONFIG_CURSES
1776 display = DT_CURSES;
1777#else
1778 fprintf(stderr, "Curses support is disabled\n");
1779 exit(1);
1780#endif
Jes Sorensen4171d322011-03-16 13:33:32 +01001781 } else if (strstart(p, "none", &opts)) {
1782 display = DT_NONE;
Jes Sorensen1472a952011-03-16 13:33:31 +01001783 } else {
Jes Sorensen1472a952011-03-16 13:33:31 +01001784 fprintf(stderr, "Unknown display type: %s\n", p);
1785 exit(1);
1786 }
1787
1788 return display;
1789}
1790
Markus Armbruster7d4c3d52009-06-26 19:15:14 +02001791static int balloon_parse(const char *arg)
1792{
Gerd Hoffmann382f0742009-08-14 10:34:22 +02001793 QemuOpts *opts;
Markus Armbruster7d4c3d52009-06-26 19:15:14 +02001794
Gerd Hoffmann382f0742009-08-14 10:34:22 +02001795 if (strcmp(arg, "none") == 0) {
1796 return 0;
Markus Armbruster7d4c3d52009-06-26 19:15:14 +02001797 }
Gerd Hoffmann382f0742009-08-14 10:34:22 +02001798
1799 if (!strncmp(arg, "virtio", 6)) {
1800 if (arg[6] == ',') {
1801 /* have params -> parse them */
Gerd Hoffmann3329f072010-08-20 13:52:01 +02001802 opts = qemu_opts_parse(qemu_find_opts("device"), arg+7, 0);
Gerd Hoffmann382f0742009-08-14 10:34:22 +02001803 if (!opts)
1804 return -1;
1805 } else {
1806 /* create empty opts */
Gerd Hoffmann3329f072010-08-20 13:52:01 +02001807 opts = qemu_opts_create(qemu_find_opts("device"), NULL, 0);
Gerd Hoffmann382f0742009-08-14 10:34:22 +02001808 }
Alexander Graf29f82b32011-03-29 15:29:29 +02001809 qemu_opt_set(opts, "driver", "virtio-balloon");
Gerd Hoffmann382f0742009-08-14 10:34:22 +02001810 return 0;
1811 }
1812
1813 return -1;
Markus Armbruster7d4c3d52009-06-26 19:15:14 +02001814}
Markus Armbruster7d4c3d52009-06-26 19:15:14 +02001815
Paul Brook5cea8592009-05-30 00:52:44 +01001816char *qemu_find_file(int type, const char *name)
1817{
1818 int len;
1819 const char *subdir;
1820 char *buf;
1821
1822 /* If name contains path separators then try it as a straight path. */
1823 if ((strchr(name, '/') || strchr(name, '\\'))
1824 && access(name, R_OK) == 0) {
Anthony Liguori7267c092011-08-20 22:09:37 -05001825 return g_strdup(name);
Paul Brook5cea8592009-05-30 00:52:44 +01001826 }
1827 switch (type) {
1828 case QEMU_FILE_TYPE_BIOS:
1829 subdir = "";
1830 break;
1831 case QEMU_FILE_TYPE_KEYMAP:
1832 subdir = "keymaps/";
1833 break;
1834 default:
1835 abort();
1836 }
1837 len = strlen(data_dir) + strlen(name) + strlen(subdir) + 2;
Anthony Liguori7267c092011-08-20 22:09:37 -05001838 buf = g_malloc0(len);
Blue Swirl3a417592009-06-09 19:12:21 +00001839 snprintf(buf, len, "%s/%s%s", data_dir, subdir, name);
Paul Brook5cea8592009-05-30 00:52:44 +01001840 if (access(buf, R_OK)) {
Anthony Liguori7267c092011-08-20 22:09:37 -05001841 g_free(buf);
Paul Brook5cea8592009-05-30 00:52:44 +01001842 return NULL;
1843 }
1844 return buf;
1845}
1846
Markus Armbrusterff952ba2010-01-29 19:48:57 +01001847static int device_help_func(QemuOpts *opts, void *opaque)
1848{
1849 return qdev_device_help(opts);
1850}
1851
Gerd Hoffmannf31d07d2009-07-31 12:25:37 +02001852static int device_init_func(QemuOpts *opts, void *opaque)
1853{
1854 DeviceState *dev;
1855
1856 dev = qdev_device_add(opts);
1857 if (!dev)
1858 return -1;
1859 return 0;
1860}
1861
Gerd Hoffmann1a688d32009-12-08 13:11:36 +01001862static int chardev_init_func(QemuOpts *opts, void *opaque)
1863{
1864 CharDriverState *chr;
1865
Anthony Liguorif69554b2011-08-15 11:17:37 -05001866 chr = qemu_chr_new_from_opts(opts, NULL);
Gerd Hoffmann1a688d32009-12-08 13:11:36 +01001867 if (!chr)
1868 return -1;
1869 return 0;
1870}
1871
Venkateswararao Jujjuri (JV)758e8e32010-06-14 13:34:41 -07001872#ifdef CONFIG_VIRTFS
Gautham R Shenoy74db9202010-04-29 17:44:43 +05301873static int fsdev_init_func(QemuOpts *opts, void *opaque)
1874{
1875 int ret;
1876 ret = qemu_fsdev_add(opts);
1877
1878 return ret;
1879}
1880#endif
1881
Gerd Hoffmann88589342009-12-08 13:11:50 +01001882static int mon_init_func(QemuOpts *opts, void *opaque)
1883{
1884 CharDriverState *chr;
1885 const char *chardev;
1886 const char *mode;
1887 int flags;
1888
1889 mode = qemu_opt_get(opts, "mode");
1890 if (mode == NULL) {
1891 mode = "readline";
1892 }
1893 if (strcmp(mode, "readline") == 0) {
1894 flags = MONITOR_USE_READLINE;
1895 } else if (strcmp(mode, "control") == 0) {
1896 flags = MONITOR_USE_CONTROL;
1897 } else {
1898 fprintf(stderr, "unknown monitor mode \"%s\"\n", mode);
1899 exit(1);
1900 }
1901
Daniel P. Berrange39eaab92010-06-07 15:42:31 +01001902 if (qemu_opt_get_bool(opts, "pretty", 0))
1903 flags |= MONITOR_USE_PRETTY;
1904
Gerd Hoffmann88589342009-12-08 13:11:50 +01001905 if (qemu_opt_get_bool(opts, "default", 0))
1906 flags |= MONITOR_IS_DEFAULT;
1907
1908 chardev = qemu_opt_get(opts, "chardev");
1909 chr = qemu_chr_find(chardev);
1910 if (chr == NULL) {
1911 fprintf(stderr, "chardev \"%s\" not found\n", chardev);
1912 exit(1);
1913 }
1914
1915 monitor_init(chr, flags);
1916 return 0;
1917}
1918
Gerd Hoffmann6ca55822009-12-08 13:11:52 +01001919static void monitor_parse(const char *optarg, const char *mode)
Gerd Hoffmann88589342009-12-08 13:11:50 +01001920{
1921 static int monitor_device_index = 0;
1922 QemuOpts *opts;
1923 const char *p;
1924 char label[32];
1925 int def = 0;
1926
1927 if (strstart(optarg, "chardev:", &p)) {
1928 snprintf(label, sizeof(label), "%s", p);
1929 } else {
Jan Kiszka140e0652010-04-06 16:55:52 +02001930 snprintf(label, sizeof(label), "compat_monitor%d",
1931 monitor_device_index);
1932 if (monitor_device_index == 0) {
Gerd Hoffmann88589342009-12-08 13:11:50 +01001933 def = 1;
1934 }
1935 opts = qemu_chr_parse_compat(label, optarg);
1936 if (!opts) {
1937 fprintf(stderr, "parse error: %s\n", optarg);
1938 exit(1);
1939 }
1940 }
1941
Gerd Hoffmann3329f072010-08-20 13:52:01 +02001942 opts = qemu_opts_create(qemu_find_opts("mon"), label, 1);
Gerd Hoffmann88589342009-12-08 13:11:50 +01001943 if (!opts) {
1944 fprintf(stderr, "duplicate chardev: %s\n", label);
1945 exit(1);
1946 }
Gerd Hoffmann6ca55822009-12-08 13:11:52 +01001947 qemu_opt_set(opts, "mode", mode);
Gerd Hoffmann88589342009-12-08 13:11:50 +01001948 qemu_opt_set(opts, "chardev", label);
1949 if (def)
1950 qemu_opt_set(opts, "default", "on");
1951 monitor_device_index++;
1952}
1953
Gerd Hoffmannbd3c9482009-07-15 13:59:26 +02001954struct device_config {
1955 enum {
Gerd Hoffmannaee1b932009-12-08 13:11:53 +01001956 DEV_USB, /* -usbdevice */
1957 DEV_BT, /* -bt */
1958 DEV_SERIAL, /* -serial */
1959 DEV_PARALLEL, /* -parallel */
1960 DEV_VIRTCON, /* -virtioconsole */
H. Peter Anvinc9f398e2009-12-29 13:51:36 -08001961 DEV_DEBUGCON, /* -debugcon */
Gerd Hoffmannbd3c9482009-07-15 13:59:26 +02001962 } type;
1963 const char *cmdline;
Blue Swirl72cf2d42009-09-12 07:36:22 +00001964 QTAILQ_ENTRY(device_config) next;
Gerd Hoffmannbd3c9482009-07-15 13:59:26 +02001965};
Blue Swirl72cf2d42009-09-12 07:36:22 +00001966QTAILQ_HEAD(, device_config) device_configs = QTAILQ_HEAD_INITIALIZER(device_configs);
Gerd Hoffmannbd3c9482009-07-15 13:59:26 +02001967
1968static void add_device_config(int type, const char *cmdline)
1969{
1970 struct device_config *conf;
1971
Anthony Liguori7267c092011-08-20 22:09:37 -05001972 conf = g_malloc0(sizeof(*conf));
Gerd Hoffmannbd3c9482009-07-15 13:59:26 +02001973 conf->type = type;
1974 conf->cmdline = cmdline;
Blue Swirl72cf2d42009-09-12 07:36:22 +00001975 QTAILQ_INSERT_TAIL(&device_configs, conf, next);
Gerd Hoffmannbd3c9482009-07-15 13:59:26 +02001976}
1977
1978static int foreach_device_config(int type, int (*func)(const char *cmdline))
1979{
1980 struct device_config *conf;
1981 int rc;
1982
Blue Swirl72cf2d42009-09-12 07:36:22 +00001983 QTAILQ_FOREACH(conf, &device_configs, next) {
Gerd Hoffmannbd3c9482009-07-15 13:59:26 +02001984 if (conf->type != type)
1985 continue;
1986 rc = func(conf->cmdline);
1987 if (0 != rc)
1988 return rc;
1989 }
1990 return 0;
1991}
1992
Gerd Hoffmann998bbd72009-12-08 13:11:41 +01001993static int serial_parse(const char *devname)
1994{
1995 static int index = 0;
1996 char label[32];
1997
1998 if (strcmp(devname, "none") == 0)
1999 return 0;
2000 if (index == MAX_SERIAL_PORTS) {
2001 fprintf(stderr, "qemu: too many serial ports\n");
2002 exit(1);
2003 }
2004 snprintf(label, sizeof(label), "serial%d", index);
Anthony Liguori27143a42011-08-15 11:17:36 -05002005 serial_hds[index] = qemu_chr_new(label, devname, NULL);
Gerd Hoffmann998bbd72009-12-08 13:11:41 +01002006 if (!serial_hds[index]) {
2007 fprintf(stderr, "qemu: could not open serial device '%s': %s\n",
2008 devname, strerror(errno));
2009 return -1;
2010 }
2011 index++;
2012 return 0;
2013}
2014
Gerd Hoffmann6a5e8b02009-12-08 13:11:42 +01002015static int parallel_parse(const char *devname)
2016{
2017 static int index = 0;
2018 char label[32];
2019
2020 if (strcmp(devname, "none") == 0)
2021 return 0;
2022 if (index == MAX_PARALLEL_PORTS) {
2023 fprintf(stderr, "qemu: too many parallel ports\n");
2024 exit(1);
2025 }
2026 snprintf(label, sizeof(label), "parallel%d", index);
Anthony Liguori27143a42011-08-15 11:17:36 -05002027 parallel_hds[index] = qemu_chr_new(label, devname, NULL);
Gerd Hoffmann6a5e8b02009-12-08 13:11:42 +01002028 if (!parallel_hds[index]) {
2029 fprintf(stderr, "qemu: could not open parallel device '%s': %s\n",
2030 devname, strerror(errno));
2031 return -1;
2032 }
2033 index++;
2034 return 0;
2035}
2036
Gerd Hoffmannaee1b932009-12-08 13:11:53 +01002037static int virtcon_parse(const char *devname)
2038{
Gerd Hoffmann3329f072010-08-20 13:52:01 +02002039 QemuOptsList *device = qemu_find_opts("device");
Gerd Hoffmannaee1b932009-12-08 13:11:53 +01002040 static int index = 0;
2041 char label[32];
Amit Shah392ecf52010-01-21 16:19:23 +05302042 QemuOpts *bus_opts, *dev_opts;
Gerd Hoffmannaee1b932009-12-08 13:11:53 +01002043
2044 if (strcmp(devname, "none") == 0)
2045 return 0;
2046 if (index == MAX_VIRTIO_CONSOLES) {
2047 fprintf(stderr, "qemu: too many virtio consoles\n");
2048 exit(1);
2049 }
Amit Shah392ecf52010-01-21 16:19:23 +05302050
Gerd Hoffmann3329f072010-08-20 13:52:01 +02002051 bus_opts = qemu_opts_create(device, NULL, 0);
Amit Shah392ecf52010-01-21 16:19:23 +05302052 qemu_opt_set(bus_opts, "driver", "virtio-serial");
2053
Gerd Hoffmann3329f072010-08-20 13:52:01 +02002054 dev_opts = qemu_opts_create(device, NULL, 0);
Amit Shah392ecf52010-01-21 16:19:23 +05302055 qemu_opt_set(dev_opts, "driver", "virtconsole");
2056
Gerd Hoffmannaee1b932009-12-08 13:11:53 +01002057 snprintf(label, sizeof(label), "virtcon%d", index);
Anthony Liguori27143a42011-08-15 11:17:36 -05002058 virtcon_hds[index] = qemu_chr_new(label, devname, NULL);
Gerd Hoffmannaee1b932009-12-08 13:11:53 +01002059 if (!virtcon_hds[index]) {
2060 fprintf(stderr, "qemu: could not open virtio console '%s': %s\n",
2061 devname, strerror(errno));
2062 return -1;
2063 }
Amit Shah392ecf52010-01-21 16:19:23 +05302064 qemu_opt_set(dev_opts, "chardev", label);
2065
Gerd Hoffmannaee1b932009-12-08 13:11:53 +01002066 index++;
2067 return 0;
2068}
2069
H. Peter Anvinc9f398e2009-12-29 13:51:36 -08002070static int debugcon_parse(const char *devname)
2071{
2072 QemuOpts *opts;
2073
Anthony Liguori27143a42011-08-15 11:17:36 -05002074 if (!qemu_chr_new("debugcon", devname, NULL)) {
H. Peter Anvinc9f398e2009-12-29 13:51:36 -08002075 exit(1);
2076 }
Gerd Hoffmann3329f072010-08-20 13:52:01 +02002077 opts = qemu_opts_create(qemu_find_opts("device"), "debugcon", 1);
H. Peter Anvinc9f398e2009-12-29 13:51:36 -08002078 if (!opts) {
2079 fprintf(stderr, "qemu: already have a debugcon device\n");
2080 exit(1);
2081 }
2082 qemu_opt_set(opts, "driver", "isa-debugcon");
2083 qemu_opt_set(opts, "chardev", "debugcon");
2084 return 0;
2085}
2086
Jan Kiszka9052ea62011-07-23 12:38:37 +02002087static QEMUMachine *machine_parse(const char *name)
2088{
2089 QEMUMachine *m, *machine = NULL;
2090
2091 if (name) {
2092 machine = find_machine(name);
2093 }
2094 if (machine) {
2095 return machine;
2096 }
2097 printf("Supported machines are:\n");
2098 for (m = first_machine; m != NULL; m = m->next) {
2099 if (m->alias) {
2100 printf("%-10s %s (alias of %s)\n", m->alias, m->desc, m->name);
2101 }
2102 printf("%-10s %s%s\n", m->name, m->desc,
2103 m->is_default ? " (default)" : "");
2104 }
2105 exit(!name || *name != '?');
2106}
2107
Anthony PERARD303d4e82010-09-21 20:05:31 +01002108static int tcg_init(void)
2109{
Jan Kiszkad5ab9712011-08-02 16:10:21 +02002110 tcg_exec_init(tcg_tb_size * 1024 * 1024);
Anthony PERARD303d4e82010-09-21 20:05:31 +01002111 return 0;
2112}
2113
2114static struct {
2115 const char *opt_name;
2116 const char *name;
2117 int (*available)(void);
2118 int (*init)(void);
2119 int *allowed;
2120} accel_list[] = {
2121 { "tcg", "tcg", tcg_available, tcg_init, &tcg_allowed },
Anthony PERARD3285cf42010-08-19 12:27:56 +01002122 { "xen", "Xen", xen_available, xen_init, &xen_allowed },
Anthony PERARD303d4e82010-09-21 20:05:31 +01002123 { "kvm", "KVM", kvm_available, kvm_init, &kvm_allowed },
2124};
2125
2126static int configure_accelerator(void)
2127{
2128 const char *p = NULL;
2129 char buf[10];
2130 int i, ret;
2131 bool accel_initalised = 0;
2132 bool init_failed = 0;
2133
2134 QemuOptsList *list = qemu_find_opts("machine");
2135 if (!QTAILQ_EMPTY(&list->head)) {
2136 p = qemu_opt_get(QTAILQ_FIRST(&list->head), "accel");
2137 }
2138
2139 if (p == NULL) {
2140 /* Use the default "accelerator", tcg */
2141 p = "tcg";
2142 }
2143
2144 while (!accel_initalised && *p != '\0') {
2145 if (*p == ':') {
2146 p++;
2147 }
2148 p = get_opt_name(buf, sizeof (buf), p, ':');
2149 for (i = 0; i < ARRAY_SIZE(accel_list); i++) {
2150 if (strcmp(accel_list[i].opt_name, buf) == 0) {
Anthony Liguoria16c53b2011-06-06 08:25:06 -05002151 *(accel_list[i].allowed) = 1;
Anthony PERARD303d4e82010-09-21 20:05:31 +01002152 ret = accel_list[i].init();
2153 if (ret < 0) {
2154 init_failed = 1;
2155 if (!accel_list[i].available()) {
2156 printf("%s not supported for this target\n",
2157 accel_list[i].name);
2158 } else {
2159 fprintf(stderr, "failed to initialize %s: %s\n",
2160 accel_list[i].name,
2161 strerror(-ret));
2162 }
Anthony Liguoria16c53b2011-06-06 08:25:06 -05002163 *(accel_list[i].allowed) = 0;
Anthony PERARD303d4e82010-09-21 20:05:31 +01002164 } else {
2165 accel_initalised = 1;
Anthony PERARD303d4e82010-09-21 20:05:31 +01002166 }
2167 break;
2168 }
2169 }
2170 if (i == ARRAY_SIZE(accel_list)) {
2171 fprintf(stderr, "\"%s\" accelerator does not exist.\n", buf);
2172 }
2173 }
2174
2175 if (!accel_initalised) {
2176 fprintf(stderr, "No accelerator found!\n");
2177 exit(1);
2178 }
2179
2180 if (init_failed) {
2181 fprintf(stderr, "Back to %s accelerator.\n", accel_list[i].name);
2182 }
2183
2184 return !accel_initalised;
2185}
2186
Gerd Hoffmannfd42dee2010-06-04 14:08:07 +02002187void qemu_add_exit_notifier(Notifier *notify)
2188{
2189 notifier_list_add(&exit_notifiers, notify);
2190}
2191
2192void qemu_remove_exit_notifier(Notifier *notify)
2193{
2194 notifier_list_remove(&exit_notifiers, notify);
2195}
2196
2197static void qemu_run_exit_notifiers(void)
2198{
Jan Kiszka9e8dd452011-06-20 14:06:26 +02002199 notifier_list_notify(&exit_notifiers, NULL);
Gerd Hoffmannfd42dee2010-06-04 14:08:07 +02002200}
2201
Gleb Natapov4cab9462010-12-08 13:35:08 +02002202void qemu_add_machine_init_done_notifier(Notifier *notify)
2203{
2204 notifier_list_add(&machine_init_done_notifiers, notify);
2205}
2206
2207static void qemu_run_machine_init_done_notifiers(void)
2208{
Jan Kiszka9e8dd452011-06-20 14:06:26 +02002209 notifier_list_notify(&machine_init_done_notifiers, NULL);
Gleb Natapov4cab9462010-12-08 13:35:08 +02002210}
2211
Anthony Liguori6530a972010-01-22 09:18:06 -06002212static const QEMUOption *lookup_opt(int argc, char **argv,
2213 const char **poptarg, int *poptind)
2214{
2215 const QEMUOption *popt;
2216 int optind = *poptind;
2217 char *r = argv[optind];
2218 const char *optarg;
2219
Markus Armbruster0f0bc3f2010-02-18 20:13:51 +01002220 loc_set_cmdline(argv, optind, 1);
Anthony Liguori6530a972010-01-22 09:18:06 -06002221 optind++;
2222 /* Treat --foo the same as -foo. */
2223 if (r[1] == '-')
2224 r++;
2225 popt = qemu_options;
2226 for(;;) {
2227 if (!popt->name) {
Markus Armbruster0f0bc3f2010-02-18 20:13:51 +01002228 error_report("invalid option");
Anthony Liguori6530a972010-01-22 09:18:06 -06002229 exit(1);
2230 }
2231 if (!strcmp(popt->name, r + 1))
2232 break;
2233 popt++;
2234 }
2235 if (popt->flags & HAS_ARG) {
2236 if (optind >= argc) {
Markus Armbruster0f0bc3f2010-02-18 20:13:51 +01002237 error_report("requires an argument");
Anthony Liguori6530a972010-01-22 09:18:06 -06002238 exit(1);
2239 }
2240 optarg = argv[optind++];
Markus Armbruster0f0bc3f2010-02-18 20:13:51 +01002241 loc_set_cmdline(argv, optind - 2, 2);
Anthony Liguori6530a972010-01-22 09:18:06 -06002242 } else {
2243 optarg = NULL;
2244 }
2245
2246 *poptarg = optarg;
2247 *poptind = optind;
2248
2249 return popt;
2250}
2251
Anthony Liguori07501122011-08-20 22:38:31 -05002252static gpointer malloc_and_trace(gsize n_bytes)
2253{
2254 void *ptr = malloc(n_bytes);
Frediano Ziglioa74cd8c2011-08-31 09:25:35 +02002255 trace_g_malloc(n_bytes, ptr);
Anthony Liguori07501122011-08-20 22:38:31 -05002256 return ptr;
2257}
2258
2259static gpointer realloc_and_trace(gpointer mem, gsize n_bytes)
2260{
2261 void *ptr = realloc(mem, n_bytes);
Frediano Ziglioa74cd8c2011-08-31 09:25:35 +02002262 trace_g_realloc(mem, n_bytes, ptr);
Anthony Liguori07501122011-08-20 22:38:31 -05002263 return ptr;
2264}
2265
2266static void free_and_trace(gpointer mem)
2267{
Frediano Ziglioa74cd8c2011-08-31 09:25:35 +02002268 trace_g_free(mem);
Anthony Liguori07501122011-08-20 22:38:31 -05002269 free(mem);
2270}
2271
malc902b3d52008-12-10 19:18:40 +00002272int main(int argc, char **argv, char **envp)
bellard0824d6f2003-06-24 13:42:40 +00002273{
aliguori59030a82009-04-05 18:43:41 +00002274 const char *gdbstub_dev = NULL;
thse4bcb142007-12-02 04:51:10 +00002275 int i;
Eduardo Habkostda1fcfd2010-04-06 19:22:07 -03002276 int snapshot, linux_boot;
Paolo Bonzini4e3de9e2010-03-10 11:38:48 +01002277 const char *icount_option = NULL;
bellard7f7f9872003-10-30 01:11:23 +00002278 const char *initrd_filename;
bellarda20dd502003-09-30 21:07:02 +00002279 const char *kernel_filename, *kernel_cmdline;
Anthony Liguori195325a2009-10-30 12:42:29 -05002280 char boot_devices[33] = "cad"; /* default to HD->floppy->CD-ROM */
aliguori3023f332009-01-16 19:04:14 +00002281 DisplayState *ds;
aliguori7d957bd2009-01-15 22:14:11 +00002282 DisplayChangeListener *dcl;
bellard46d47672004-11-16 01:45:27 +00002283 int cyls, heads, secs, translation;
Gerd Hoffmannf31d07d2009-07-31 12:25:37 +02002284 QemuOpts *hda_opts = NULL, *opts;
Gerd Hoffmann03b0ba72010-08-20 13:52:02 +02002285 QemuOptsList *olist;
bellardcd6f1162004-05-13 22:02:20 +00002286 int optind;
Anthony Liguori6530a972010-01-22 09:18:06 -06002287 const char *optarg;
bellardd63d3072004-10-03 13:29:03 +00002288 const char *loadvm = NULL;
bellardcc1daa42005-06-05 14:49:17 +00002289 QEMUMachine *machine;
j_mayer94fc95c2007-03-05 19:44:02 +00002290 const char *cpu_model;
ths93815bc2007-03-19 15:58:31 +00002291 const char *pid_file = NULL;
aliguori5bb79102008-10-13 03:12:02 +00002292 const char *incoming = NULL;
Jes Sorensen821601e2011-03-16 13:33:36 +01002293#ifdef CONFIG_VNC
Anthony Liguori993fbfd2009-05-21 16:54:00 -05002294 int show_vnc_port = 0;
Jes Sorensen821601e2011-03-16 13:33:36 +01002295#endif
Anthony Liguori292444c2010-01-21 10:57:58 -06002296 int defconfig = 1;
Matthew Fernandezc235d732011-06-07 16:32:40 +00002297 const char *log_mask = NULL;
2298 const char *log_file = NULL;
Anthony Liguori07501122011-08-20 22:38:31 -05002299 GMemVTable mem_trace = {
2300 .malloc = malloc_and_trace,
2301 .realloc = realloc_and_trace,
2302 .free = free_and_trace,
2303 };
Lluís23d15e82011-08-31 20:31:31 +02002304 const char *trace_events = NULL;
Lluíse4858972011-08-31 20:31:03 +02002305 const char *trace_file = NULL;
Stefan Hajnoczi0b5538c2011-02-26 18:38:39 +00002306
Gerd Hoffmannfd42dee2010-06-04 14:08:07 +02002307 atexit(qemu_run_exit_notifiers);
Markus Armbruster65abca02010-02-24 14:37:14 +01002308 error_set_progname(argv[0]);
2309
Anthony Liguori07501122011-08-20 22:38:31 -05002310 g_mem_set_vtable(&mem_trace);
Gerd Hoffmannad8b8182011-09-07 15:02:15 +02002311 g_thread_init(NULL);
Anthony Liguori07501122011-08-20 22:38:31 -05002312
Luiz Capitulino5db9d4d2011-09-05 16:36:31 -03002313 runstate_init();
2314
Jan Kiszka68752042009-09-15 13:36:04 +02002315 init_clocks();
2316
malc902b3d52008-12-10 19:18:40 +00002317 qemu_cache_utils_init(envp);
2318
Blue Swirl72cf2d42009-09-12 07:36:22 +00002319 QLIST_INIT (&vm_change_state_head);
Jes Sorensenfe98ac12010-06-10 11:42:21 +02002320 os_setup_early_signal_handling();
bellardbe995c22006-06-25 16:25:21 +00002321
Anthony Liguorif80f9ec2009-05-20 18:38:09 -05002322 module_call_init(MODULE_INIT_MACHINE);
Anthony Liguori0c257432009-05-21 20:41:01 -05002323 machine = find_default_machine();
j_mayer94fc95c2007-03-05 19:44:02 +00002324 cpu_model = NULL;
bellardfc01f7e2003-06-30 10:03:06 +00002325 initrd_filename = NULL;
aurel324fc5d072008-04-27 21:39:40 +00002326 ram_size = 0;
bellard33e39632003-07-06 17:15:21 +00002327 snapshot = 0;
bellarda20dd502003-09-30 21:07:02 +00002328 kernel_filename = NULL;
2329 kernel_cmdline = "";
bellardc4b1fcc2004-03-14 21:44:30 +00002330 cyls = heads = secs = 0;
bellard46d47672004-11-16 01:45:27 +00002331 translation = BIOS_ATA_TRANSLATION_AUTO;
bellardc4b1fcc2004-03-14 21:44:30 +00002332
aliguori268a3622009-04-21 22:30:27 +00002333 for (i = 0; i < MAX_NODES; i++) {
2334 node_mem[i] = 0;
2335 node_cpumask[i] = 0;
2336 }
2337
aliguori268a3622009-04-21 22:30:27 +00002338 nb_numa_nodes = 0;
bellard7c9d8e02005-11-15 22:16:05 +00002339 nb_nics = 0;
ths3b46e622007-09-17 08:09:54 +00002340
blueswir141bd6392008-10-05 09:56:21 +00002341 autostart= 1;
2342
Anthony Liguori292444c2010-01-21 10:57:58 -06002343 /* first pass of option parsing */
2344 optind = 1;
2345 while (optind < argc) {
2346 if (argv[optind][0] != '-') {
2347 /* disk image */
Anthony Liguori28e68d62010-01-27 10:46:00 -06002348 optind++;
Anthony Liguori292444c2010-01-21 10:57:58 -06002349 continue;
2350 } else {
2351 const QEMUOption *popt;
2352
2353 popt = lookup_opt(argc, argv, &optarg, &optind);
2354 switch (popt->index) {
2355 case QEMU_OPTION_nodefconfig:
2356 defconfig=0;
2357 break;
2358 }
2359 }
2360 }
2361
2362 if (defconfig) {
Kevin Wolfdcfb0932010-03-05 17:25:55 +01002363 int ret;
Markus Armbrustercf5a65a2010-02-18 19:48:33 +01002364
Kevin Wolfdcfb0932010-03-05 17:25:55 +01002365 ret = qemu_read_config_file(CONFIG_QEMU_CONFDIR "/qemu.conf");
Kevin Wolf019e78b2010-05-17 10:36:47 +02002366 if (ret < 0 && ret != -ENOENT) {
Kevin Wolfdcfb0932010-03-05 17:25:55 +01002367 exit(1);
Anthony Liguori292444c2010-01-21 10:57:58 -06002368 }
2369
Kevin Wolfdcfb0932010-03-05 17:25:55 +01002370 ret = qemu_read_config_file(arch_config_name);
Kevin Wolf019e78b2010-05-17 10:36:47 +02002371 if (ret < 0 && ret != -ENOENT) {
Kevin Wolfdcfb0932010-03-05 17:25:55 +01002372 exit(1);
Anthony Liguori292444c2010-01-21 10:57:58 -06002373 }
2374 }
Blue Swirlde06f8d2010-03-29 19:23:50 +00002375 cpudef_init();
Anthony Liguori292444c2010-01-21 10:57:58 -06002376
2377 /* second pass of option parsing */
bellardcd6f1162004-05-13 22:02:20 +00002378 optind = 1;
bellard0824d6f2003-06-24 13:42:40 +00002379 for(;;) {
bellardcd6f1162004-05-13 22:02:20 +00002380 if (optind >= argc)
bellard0824d6f2003-06-24 13:42:40 +00002381 break;
Anthony Liguori6530a972010-01-22 09:18:06 -06002382 if (argv[optind][0] != '-') {
Markus Armbruster2292dda2011-01-28 11:21:41 +01002383 hda_opts = drive_add(IF_DEFAULT, 0, argv[optind++], HD_OPTS);
bellardcd6f1162004-05-13 22:02:20 +00002384 } else {
2385 const QEMUOption *popt;
2386
Anthony Liguori6530a972010-01-22 09:18:06 -06002387 popt = lookup_opt(argc, argv, &optarg, &optind);
Blue Swirlad960902010-03-29 19:23:52 +00002388 if (!(popt->arch_mask & arch_type)) {
2389 printf("Option %s not supported for this target\n", popt->name);
2390 exit(1);
2391 }
bellardcd6f1162004-05-13 22:02:20 +00002392 switch(popt->index) {
bellardcc1daa42005-06-05 14:49:17 +00002393 case QEMU_OPTION_M:
Jan Kiszka9052ea62011-07-23 12:38:37 +02002394 machine = machine_parse(optarg);
bellardcc1daa42005-06-05 14:49:17 +00002395 break;
j_mayer94fc95c2007-03-05 19:44:02 +00002396 case QEMU_OPTION_cpu:
2397 /* hw initialization will check this */
ths15f82202007-06-29 23:26:08 +00002398 if (*optarg == '?') {
Blue Swirl262353c2010-05-04 19:55:35 +00002399 list_cpus(stdout, &fprintf, optarg);
ths15f82202007-06-29 23:26:08 +00002400 exit(0);
j_mayer94fc95c2007-03-05 19:44:02 +00002401 } else {
2402 cpu_model = optarg;
2403 }
2404 break;
bellardcd6f1162004-05-13 22:02:20 +00002405 case QEMU_OPTION_initrd:
bellardfc01f7e2003-06-30 10:03:06 +00002406 initrd_filename = optarg;
2407 break;
bellardcd6f1162004-05-13 22:02:20 +00002408 case QEMU_OPTION_hda:
Markus Armbruster5645b0f2011-01-31 11:50:09 +01002409 {
2410 char buf[256];
2411 if (cyls == 0)
2412 snprintf(buf, sizeof(buf), "%s", HD_OPTS);
2413 else
2414 snprintf(buf, sizeof(buf),
2415 "%s,cyls=%d,heads=%d,secs=%d%s",
2416 HD_OPTS , cyls, heads, secs,
2417 translation == BIOS_ATA_TRANSLATION_LBA ?
thse4bcb142007-12-02 04:51:10 +00002418 ",trans=lba" :
Markus Armbruster5645b0f2011-01-31 11:50:09 +01002419 translation == BIOS_ATA_TRANSLATION_NONE ?
thse4bcb142007-12-02 04:51:10 +00002420 ",trans=none" : "");
Markus Armbruster5645b0f2011-01-31 11:50:09 +01002421 drive_add(IF_DEFAULT, 0, optarg, buf);
2422 break;
2423 }
bellardcd6f1162004-05-13 22:02:20 +00002424 case QEMU_OPTION_hdb:
bellardcc1daa42005-06-05 14:49:17 +00002425 case QEMU_OPTION_hdc:
2426 case QEMU_OPTION_hdd:
Markus Armbruster2292dda2011-01-28 11:21:41 +01002427 drive_add(IF_DEFAULT, popt->index - QEMU_OPTION_hda, optarg,
2428 HD_OPTS);
bellardfc01f7e2003-06-30 10:03:06 +00002429 break;
thse4bcb142007-12-02 04:51:10 +00002430 case QEMU_OPTION_drive:
Michael Tokareve2982c32011-03-30 16:31:05 +04002431 if (drive_def(optarg) == NULL) {
2432 exit(1);
2433 }
thse4bcb142007-12-02 04:51:10 +00002434 break;
Gerd Hoffmannd058fe02009-07-31 12:25:36 +02002435 case QEMU_OPTION_set:
2436 if (qemu_set_option(optarg) != 0)
2437 exit(1);
2438 break;
Gerd Hoffmannd0fef6f2009-12-08 13:11:34 +01002439 case QEMU_OPTION_global:
2440 if (qemu_global_option(optarg) != 0)
2441 exit(1);
2442 break;
balrog3e3d5812007-04-30 02:09:25 +00002443 case QEMU_OPTION_mtdblock:
Markus Armbruster2292dda2011-01-28 11:21:41 +01002444 drive_add(IF_MTD, -1, optarg, MTD_OPTS);
balrog3e3d5812007-04-30 02:09:25 +00002445 break;
pbrooka1bb27b2007-04-06 16:49:48 +00002446 case QEMU_OPTION_sd:
Markus Armbruster2292dda2011-01-28 11:21:41 +01002447 drive_add(IF_SD, 0, optarg, SD_OPTS);
pbrooka1bb27b2007-04-06 16:49:48 +00002448 break;
j_mayer86f55662007-04-24 06:52:59 +00002449 case QEMU_OPTION_pflash:
Markus Armbruster2292dda2011-01-28 11:21:41 +01002450 drive_add(IF_PFLASH, -1, optarg, PFLASH_OPTS);
j_mayer86f55662007-04-24 06:52:59 +00002451 break;
bellardcd6f1162004-05-13 22:02:20 +00002452 case QEMU_OPTION_snapshot:
bellard33e39632003-07-06 17:15:21 +00002453 snapshot = 1;
2454 break;
bellardcd6f1162004-05-13 22:02:20 +00002455 case QEMU_OPTION_hdachs:
bellard330d0412003-07-26 18:11:40 +00002456 {
bellard330d0412003-07-26 18:11:40 +00002457 const char *p;
2458 p = optarg;
2459 cyls = strtol(p, (char **)&p, 0);
bellard46d47672004-11-16 01:45:27 +00002460 if (cyls < 1 || cyls > 16383)
2461 goto chs_fail;
bellard330d0412003-07-26 18:11:40 +00002462 if (*p != ',')
2463 goto chs_fail;
2464 p++;
2465 heads = strtol(p, (char **)&p, 0);
bellard46d47672004-11-16 01:45:27 +00002466 if (heads < 1 || heads > 16)
2467 goto chs_fail;
bellard330d0412003-07-26 18:11:40 +00002468 if (*p != ',')
2469 goto chs_fail;
2470 p++;
2471 secs = strtol(p, (char **)&p, 0);
bellard46d47672004-11-16 01:45:27 +00002472 if (secs < 1 || secs > 63)
2473 goto chs_fail;
2474 if (*p == ',') {
2475 p++;
2476 if (!strcmp(p, "none"))
2477 translation = BIOS_ATA_TRANSLATION_NONE;
2478 else if (!strcmp(p, "lba"))
2479 translation = BIOS_ATA_TRANSLATION_LBA;
2480 else if (!strcmp(p, "auto"))
2481 translation = BIOS_ATA_TRANSLATION_AUTO;
2482 else
2483 goto chs_fail;
2484 } else if (*p != '\0') {
bellardc4b1fcc2004-03-14 21:44:30 +00002485 chs_fail:
bellard46d47672004-11-16 01:45:27 +00002486 fprintf(stderr, "qemu: invalid physical CHS format\n");
2487 exit(1);
bellardc4b1fcc2004-03-14 21:44:30 +00002488 }
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +02002489 if (hda_opts != NULL) {
2490 char num[16];
2491 snprintf(num, sizeof(num), "%d", cyls);
2492 qemu_opt_set(hda_opts, "cyls", num);
2493 snprintf(num, sizeof(num), "%d", heads);
2494 qemu_opt_set(hda_opts, "heads", num);
2495 snprintf(num, sizeof(num), "%d", secs);
2496 qemu_opt_set(hda_opts, "secs", num);
2497 if (translation == BIOS_ATA_TRANSLATION_LBA)
2498 qemu_opt_set(hda_opts, "trans", "lba");
2499 if (translation == BIOS_ATA_TRANSLATION_NONE)
2500 qemu_opt_set(hda_opts, "trans", "none");
2501 }
bellard330d0412003-07-26 18:11:40 +00002502 }
2503 break;
aliguori268a3622009-04-21 22:30:27 +00002504 case QEMU_OPTION_numa:
2505 if (nb_numa_nodes >= MAX_NODES) {
2506 fprintf(stderr, "qemu: too many NUMA nodes\n");
2507 exit(1);
2508 }
2509 numa_add(optarg);
2510 break;
Jes Sorensen1472a952011-03-16 13:33:31 +01002511 case QEMU_OPTION_display:
2512 display_type = select_display(optarg);
2513 break;
bellardcd6f1162004-05-13 22:02:20 +00002514 case QEMU_OPTION_nographic:
Anthony Liguori993fbfd2009-05-21 16:54:00 -05002515 display_type = DT_NOGRAPHIC;
bellarda20dd502003-09-30 21:07:02 +00002516 break;
balrog4d3b6f62008-02-10 16:33:14 +00002517 case QEMU_OPTION_curses:
Jes Sorensen47b05362011-03-16 13:33:35 +01002518#ifdef CONFIG_CURSES
Anthony Liguori993fbfd2009-05-21 16:54:00 -05002519 display_type = DT_CURSES;
Jes Sorensen47b05362011-03-16 13:33:35 +01002520#else
2521 fprintf(stderr, "Curses support is disabled\n");
2522 exit(1);
balrog4d3b6f62008-02-10 16:33:14 +00002523#endif
Jes Sorensen47b05362011-03-16 13:33:35 +01002524 break;
balroga171fe32007-04-30 01:48:07 +00002525 case QEMU_OPTION_portrait:
Vasily Khoruzhick93128052011-06-17 13:04:36 +03002526 graphic_rotate = 90;
2527 break;
2528 case QEMU_OPTION_rotate:
2529 graphic_rotate = strtol(optarg, (char **) &optarg, 10);
2530 if (graphic_rotate != 0 && graphic_rotate != 90 &&
2531 graphic_rotate != 180 && graphic_rotate != 270) {
2532 fprintf(stderr,
2533 "qemu: only 90, 180, 270 deg rotation is available\n");
2534 exit(1);
2535 }
balroga171fe32007-04-30 01:48:07 +00002536 break;
bellardcd6f1162004-05-13 22:02:20 +00002537 case QEMU_OPTION_kernel:
bellarda20dd502003-09-30 21:07:02 +00002538 kernel_filename = optarg;
2539 break;
bellardcd6f1162004-05-13 22:02:20 +00002540 case QEMU_OPTION_append:
bellarda20dd502003-09-30 21:07:02 +00002541 kernel_cmdline = optarg;
bellard313aa562003-08-10 21:52:11 +00002542 break;
bellardcd6f1162004-05-13 22:02:20 +00002543 case QEMU_OPTION_cdrom:
Markus Armbruster2292dda2011-01-28 11:21:41 +01002544 drive_add(IF_DEFAULT, 2, optarg, CDROM_OPTS);
bellard36b486b2003-11-11 13:36:08 +00002545 break;
bellardcd6f1162004-05-13 22:02:20 +00002546 case QEMU_OPTION_boot:
j_mayer28c5af52007-11-11 01:50:45 +00002547 {
Jan Kiszkaef3adf62009-07-02 00:19:02 +02002548 static const char * const params[] = {
wayne3d3b8302011-07-27 18:04:55 +08002549 "order", "once", "menu",
2550 "splash", "splash-time", NULL
Jan Kiszkaef3adf62009-07-02 00:19:02 +02002551 };
2552 char buf[sizeof(boot_devices)];
Jan Kiszkae0f084b2009-07-02 00:19:02 +02002553 char *standard_boot_devices;
Jan Kiszkaef3adf62009-07-02 00:19:02 +02002554 int legacy = 0;
2555
2556 if (!strchr(optarg, '=')) {
2557 legacy = 1;
2558 pstrcpy(buf, sizeof(buf), optarg);
2559 } else if (check_params(buf, sizeof(buf), params, optarg) < 0) {
2560 fprintf(stderr,
2561 "qemu: unknown boot parameter '%s' in '%s'\n",
2562 buf, optarg);
2563 exit(1);
2564 }
2565
2566 if (legacy ||
2567 get_param_value(buf, sizeof(buf), "order", optarg)) {
Eduardo Habkost4e9e9d62010-04-06 19:22:08 -03002568 validate_bootdevices(buf);
Jan Kiszkaef3adf62009-07-02 00:19:02 +02002569 pstrcpy(boot_devices, sizeof(boot_devices), buf);
j_mayer28c5af52007-11-11 01:50:45 +00002570 }
Jan Kiszkae0f084b2009-07-02 00:19:02 +02002571 if (!legacy) {
2572 if (get_param_value(buf, sizeof(buf),
2573 "once", optarg)) {
Eduardo Habkost4e9e9d62010-04-06 19:22:08 -03002574 validate_bootdevices(buf);
Anthony Liguori7267c092011-08-20 22:09:37 -05002575 standard_boot_devices = g_strdup(boot_devices);
Jan Kiszkae0f084b2009-07-02 00:19:02 +02002576 pstrcpy(boot_devices, sizeof(boot_devices), buf);
2577 qemu_register_reset(restore_boot_devices,
2578 standard_boot_devices);
2579 }
Jan Kiszka95387492009-07-02 00:19:02 +02002580 if (get_param_value(buf, sizeof(buf),
2581 "menu", optarg)) {
2582 if (!strcmp(buf, "on")) {
2583 boot_menu = 1;
2584 } else if (!strcmp(buf, "off")) {
2585 boot_menu = 0;
2586 } else {
2587 fprintf(stderr,
2588 "qemu: invalid option value '%s'\n",
2589 buf);
2590 exit(1);
2591 }
2592 }
wayne3d3b8302011-07-27 18:04:55 +08002593 qemu_opts_parse(qemu_find_opts("boot-opts"),
2594 optarg, 0);
Jan Kiszkae0f084b2009-07-02 00:19:02 +02002595 }
bellard36b486b2003-11-11 13:36:08 +00002596 }
2597 break;
bellardcd6f1162004-05-13 22:02:20 +00002598 case QEMU_OPTION_fda:
bellardcd6f1162004-05-13 22:02:20 +00002599 case QEMU_OPTION_fdb:
Markus Armbruster2292dda2011-01-28 11:21:41 +01002600 drive_add(IF_FLOPPY, popt->index - QEMU_OPTION_fda,
2601 optarg, FD_OPTS);
bellardc45886d2004-01-05 00:02:06 +00002602 break;
bellard52ca8d62006-06-14 16:03:05 +00002603 case QEMU_OPTION_no_fd_bootchk:
2604 fd_bootchk = 0;
2605 break;
Mark McLoughlina1ea4582009-10-08 19:58:26 +01002606 case QEMU_OPTION_netdev:
Gerd Hoffmann3329f072010-08-20 13:52:01 +02002607 if (net_client_parse(qemu_find_opts("netdev"), optarg) == -1) {
Mark McLoughlina1ea4582009-10-08 19:58:26 +01002608 exit(1);
2609 }
2610 break;
bellard7c9d8e02005-11-15 22:16:05 +00002611 case QEMU_OPTION_net:
Gerd Hoffmann3329f072010-08-20 13:52:01 +02002612 if (net_client_parse(qemu_find_opts("net"), optarg) == -1) {
bellardc4b1fcc2004-03-14 21:44:30 +00002613 exit(1);
2614 }
bellard702c6512004-04-02 21:21:32 +00002615 break;
bellardc7f74642004-08-24 21:57:12 +00002616#ifdef CONFIG_SLIRP
2617 case QEMU_OPTION_tftp:
Jan Kiszkaad196a92009-06-24 14:42:28 +02002618 legacy_tftp_prefix = optarg;
bellard9bf05442004-08-25 22:12:49 +00002619 break;
ths47d5d012007-02-20 00:05:08 +00002620 case QEMU_OPTION_bootp:
Jan Kiszkaad196a92009-06-24 14:42:28 +02002621 legacy_bootp_filename = optarg;
ths47d5d012007-02-20 00:05:08 +00002622 break;
bellard9bf05442004-08-25 22:12:49 +00002623 case QEMU_OPTION_redir:
Markus Armbruster07527062009-10-06 12:16:57 +01002624 if (net_slirp_redir(optarg) < 0)
2625 exit(1);
bellard9bf05442004-08-25 22:12:49 +00002626 break;
bellardc7f74642004-08-24 21:57:12 +00002627#endif
balrogdc72ac12008-11-09 00:04:26 +00002628 case QEMU_OPTION_bt:
Gerd Hoffmannbd3c9482009-07-15 13:59:26 +02002629 add_device_config(DEV_BT, optarg);
balrogdc72ac12008-11-09 00:04:26 +00002630 break;
bellard1d14ffa2005-10-30 18:58:22 +00002631 case QEMU_OPTION_audio_help:
Blue Swirlad960902010-03-29 19:23:52 +00002632 if (!(audio_available())) {
2633 printf("Option %s not supported for this target\n", popt->name);
2634 exit(1);
2635 }
bellard1d14ffa2005-10-30 18:58:22 +00002636 AUD_help ();
2637 exit (0);
2638 break;
2639 case QEMU_OPTION_soundhw:
Blue Swirlad960902010-03-29 19:23:52 +00002640 if (!(audio_available())) {
2641 printf("Option %s not supported for this target\n", popt->name);
2642 exit(1);
2643 }
bellard1d14ffa2005-10-30 18:58:22 +00002644 select_soundhw (optarg);
2645 break;
bellardcd6f1162004-05-13 22:02:20 +00002646 case QEMU_OPTION_h:
ths15f82202007-06-29 23:26:08 +00002647 help(0);
bellardcd6f1162004-05-13 22:02:20 +00002648 break;
pbrook9bd7e6d2009-04-07 22:58:45 +00002649 case QEMU_OPTION_version:
2650 version();
2651 exit(0);
2652 break;
aurel3200f82b82008-04-27 21:12:55 +00002653 case QEMU_OPTION_m: {
Jes Sorensen70b4f4b2011-01-05 11:41:02 +01002654 int64_t value;
aurel3200f82b82008-04-27 21:12:55 +00002655
Jes Sorensen9f9b17a2010-10-21 17:15:46 +02002656 value = strtosz(optarg, NULL);
2657 if (value < 0) {
aurel3200f82b82008-04-27 21:12:55 +00002658 fprintf(stderr, "qemu: invalid ram size: %s\n", optarg);
bellardcd6f1162004-05-13 22:02:20 +00002659 exit(1);
2660 }
aurel3200f82b82008-04-27 21:12:55 +00002661
Anthony Liguoric227f092009-10-01 16:12:16 -05002662 if (value != (uint64_t)(ram_addr_t)value) {
aurel3200f82b82008-04-27 21:12:55 +00002663 fprintf(stderr, "qemu: ram size too large\n");
2664 exit(1);
2665 }
2666 ram_size = value;
bellardcd6f1162004-05-13 22:02:20 +00002667 break;
aurel3200f82b82008-04-27 21:12:55 +00002668 }
Marcelo Tosattic9027602010-03-01 20:25:08 -03002669 case QEMU_OPTION_mempath:
2670 mem_path = optarg;
2671 break;
2672#ifdef MAP_POPULATE
2673 case QEMU_OPTION_mem_prealloc:
2674 mem_prealloc = 1;
2675 break;
2676#endif
bellardcd6f1162004-05-13 22:02:20 +00002677 case QEMU_OPTION_d:
Matthew Fernandezc235d732011-06-07 16:32:40 +00002678 log_mask = optarg;
2679 break;
2680 case QEMU_OPTION_D:
2681 log_file = optarg;
bellardcd6f1162004-05-13 22:02:20 +00002682 break;
bellardcd6f1162004-05-13 22:02:20 +00002683 case QEMU_OPTION_s:
aliguori59030a82009-04-05 18:43:41 +00002684 gdbstub_dev = "tcp::" DEFAULT_GDBSTUB_PORT;
bellardcd6f1162004-05-13 22:02:20 +00002685 break;
aliguori59030a82009-04-05 18:43:41 +00002686 case QEMU_OPTION_gdb:
2687 gdbstub_dev = optarg;
bellardcd6f1162004-05-13 22:02:20 +00002688 break;
bellardcd6f1162004-05-13 22:02:20 +00002689 case QEMU_OPTION_L:
Paul Brook5cea8592009-05-30 00:52:44 +01002690 data_dir = optarg;
bellardcd6f1162004-05-13 22:02:20 +00002691 break;
j_mayer1192dad2007-10-05 13:08:35 +00002692 case QEMU_OPTION_bios:
2693 bios_name = optarg;
2694 break;
aurel321b530a62009-04-05 20:08:59 +00002695 case QEMU_OPTION_singlestep:
2696 singlestep = 1;
2697 break;
bellardcd6f1162004-05-13 22:02:20 +00002698 case QEMU_OPTION_S:
pbrook3c07f8e2007-01-21 16:47:01 +00002699 autostart = 0;
bellardcd6f1162004-05-13 22:02:20 +00002700 break;
bellard3d11d0e2004-12-12 16:56:30 +00002701 case QEMU_OPTION_k:
2702 keyboard_layout = optarg;
2703 break;
bellardee22c2f2004-06-03 12:49:50 +00002704 case QEMU_OPTION_localtime:
2705 rtc_utc = 0;
2706 break;
malc3893c122008-09-28 00:42:05 +00002707 case QEMU_OPTION_vga:
2708 select_vgahw (optarg);
bellard1bfe8562004-07-08 21:17:50 +00002709 break;
bellarde9b137c2004-06-21 16:46:10 +00002710 case QEMU_OPTION_g:
2711 {
2712 const char *p;
2713 int w, h, depth;
2714 p = optarg;
2715 w = strtol(p, (char **)&p, 10);
2716 if (w <= 0) {
2717 graphic_error:
2718 fprintf(stderr, "qemu: invalid resolution or depth\n");
2719 exit(1);
2720 }
2721 if (*p != 'x')
2722 goto graphic_error;
2723 p++;
2724 h = strtol(p, (char **)&p, 10);
2725 if (h <= 0)
2726 goto graphic_error;
2727 if (*p == 'x') {
2728 p++;
2729 depth = strtol(p, (char **)&p, 10);
ths5fafdf22007-09-16 21:08:06 +00002730 if (depth != 8 && depth != 15 && depth != 16 &&
bellarde9b137c2004-06-21 16:46:10 +00002731 depth != 24 && depth != 32)
2732 goto graphic_error;
2733 } else if (*p == '\0') {
2734 depth = graphic_depth;
2735 } else {
2736 goto graphic_error;
2737 }
ths3b46e622007-09-17 08:09:54 +00002738
bellarde9b137c2004-06-21 16:46:10 +00002739 graphic_width = w;
2740 graphic_height = h;
2741 graphic_depth = depth;
2742 }
2743 break;
ths20d8a3e2007-02-18 17:04:49 +00002744 case QEMU_OPTION_echr:
2745 {
2746 char *r;
2747 term_escape_char = strtol(optarg, &r, 0);
2748 if (r == optarg)
2749 printf("Bad argument to echr\n");
2750 break;
2751 }
bellard82c643f2004-07-14 17:28:13 +00002752 case QEMU_OPTION_monitor:
Gerd Hoffmann6ca55822009-12-08 13:11:52 +01002753 monitor_parse(optarg, "readline");
2754 default_monitor = 0;
2755 break;
2756 case QEMU_OPTION_qmp:
2757 monitor_parse(optarg, "control");
Anthony Liguori2d114dc2010-03-21 14:14:38 -05002758 default_monitor = 0;
bellard82c643f2004-07-14 17:28:13 +00002759 break;
Gerd Hoffmann22a0e042009-12-08 13:11:51 +01002760 case QEMU_OPTION_mon:
Gerd Hoffmann3329f072010-08-20 13:52:01 +02002761 opts = qemu_opts_parse(qemu_find_opts("mon"), optarg, 1);
Gerd Hoffmann22a0e042009-12-08 13:11:51 +01002762 if (!opts) {
Gerd Hoffmann22a0e042009-12-08 13:11:51 +01002763 exit(1);
2764 }
Anthony Liguori2d114dc2010-03-21 14:14:38 -05002765 default_monitor = 0;
Gerd Hoffmann22a0e042009-12-08 13:11:51 +01002766 break;
Gerd Hoffmann191bc012009-09-10 10:58:35 +02002767 case QEMU_OPTION_chardev:
Gerd Hoffmann3329f072010-08-20 13:52:01 +02002768 opts = qemu_opts_parse(qemu_find_opts("chardev"), optarg, 1);
Gerd Hoffmann191bc012009-09-10 10:58:35 +02002769 if (!opts) {
Gerd Hoffmann191bc012009-09-10 10:58:35 +02002770 exit(1);
2771 }
Gerd Hoffmann191bc012009-09-10 10:58:35 +02002772 break;
Gautham R Shenoy74db9202010-04-29 17:44:43 +05302773 case QEMU_OPTION_fsdev:
Gerd Hoffmann03b0ba72010-08-20 13:52:02 +02002774 olist = qemu_find_opts("fsdev");
2775 if (!olist) {
2776 fprintf(stderr, "fsdev is not supported by this qemu build.\n");
2777 exit(1);
2778 }
2779 opts = qemu_opts_parse(olist, optarg, 1);
Gautham R Shenoy74db9202010-04-29 17:44:43 +05302780 if (!opts) {
2781 fprintf(stderr, "parse error: %s\n", optarg);
2782 exit(1);
2783 }
2784 break;
Gautham R Shenoy3d54abc2010-04-29 17:45:03 +05302785 case QEMU_OPTION_virtfs: {
Stefan Hajnoczie14ea472011-03-16 08:31:43 +00002786 QemuOpts *fsdev;
2787 QemuOpts *device;
Aneesh Kumar K.Vd3ab98e2011-10-12 19:11:23 +05302788 const char *writeout;
Gautham R Shenoy3d54abc2010-04-29 17:45:03 +05302789
Gerd Hoffmann03b0ba72010-08-20 13:52:02 +02002790 olist = qemu_find_opts("virtfs");
2791 if (!olist) {
2792 fprintf(stderr, "virtfs is not supported by this qemu build.\n");
2793 exit(1);
2794 }
2795 opts = qemu_opts_parse(olist, optarg, 1);
Gautham R Shenoy3d54abc2010-04-29 17:45:03 +05302796 if (!opts) {
2797 fprintf(stderr, "parse error: %s\n", optarg);
2798 exit(1);
2799 }
2800
Aneesh Kumar K.Vfbcbf102011-10-13 12:28:04 +05302801 if (qemu_opt_get(opts, "fsdriver") == NULL ||
Venkateswararao Jujjuri (JV)9ce56db2010-06-14 13:34:40 -07002802 qemu_opt_get(opts, "mount_tag") == NULL ||
2803 qemu_opt_get(opts, "path") == NULL ||
2804 qemu_opt_get(opts, "security_model") == NULL) {
Aneesh Kumar K.Vfbcbf102011-10-13 12:28:04 +05302805 fprintf(stderr, "Usage: -virtfs fsdriver,path=/share_path/,"
Aneesh Kumar K.V12848bf2010-09-02 11:09:07 +05302806 "security_model=[mapped|passthrough|none],"
Stefan Hajnoczie14ea472011-03-16 08:31:43 +00002807 "mount_tag=tag.\n");
Venkateswararao Jujjuri (JV)9ce56db2010-06-14 13:34:40 -07002808 exit(1);
2809 }
2810
Stefan Hajnoczie14ea472011-03-16 08:31:43 +00002811 fsdev = qemu_opts_create(qemu_find_opts("fsdev"),
2812 qemu_opt_get(opts, "mount_tag"), 1);
2813 if (!fsdev) {
2814 fprintf(stderr, "duplicate fsdev id: %s\n",
2815 qemu_opt_get(opts, "mount_tag"));
Gautham R Shenoy3d54abc2010-04-29 17:45:03 +05302816 exit(1);
2817 }
Aneesh Kumar K.Vd3ab98e2011-10-12 19:11:23 +05302818
2819 writeout = qemu_opt_get(opts, "writeout");
2820 if (writeout) {
2821#ifdef CONFIG_SYNC_FILE_RANGE
2822 qemu_opt_set(fsdev, "writeout", writeout);
2823#else
2824 fprintf(stderr, "writeout=immediate not supported on "
2825 "this platform\n");
2826 exit(1);
2827#endif
2828 }
Aneesh Kumar K.Vfbcbf102011-10-13 12:28:04 +05302829 qemu_opt_set(fsdev, "fsdriver", qemu_opt_get(opts, "fsdriver"));
Stefan Hajnoczie14ea472011-03-16 08:31:43 +00002830 qemu_opt_set(fsdev, "path", qemu_opt_get(opts, "path"));
2831 qemu_opt_set(fsdev, "security_model",
2832 qemu_opt_get(opts, "security_model"));
Gautham R Shenoy3d54abc2010-04-29 17:45:03 +05302833
Stefan Hajnoczie14ea472011-03-16 08:31:43 +00002834 device = qemu_opts_create(qemu_find_opts("device"), NULL, 0);
2835 qemu_opt_set(device, "driver", "virtio-9p-pci");
2836 qemu_opt_set(device, "fsdev",
2837 qemu_opt_get(opts, "mount_tag"));
2838 qemu_opt_set(device, "mount_tag",
2839 qemu_opt_get(opts, "mount_tag"));
Gautham R Shenoy3d54abc2010-04-29 17:45:03 +05302840 break;
2841 }
bellard82c643f2004-07-14 17:28:13 +00002842 case QEMU_OPTION_serial:
Gerd Hoffmann998bbd72009-12-08 13:11:41 +01002843 add_device_config(DEV_SERIAL, optarg);
2844 default_serial = 0;
Jan Kiszka18141ed2010-03-07 11:28:40 +01002845 if (strncmp(optarg, "mon:", 4) == 0) {
2846 default_monitor = 0;
2847 }
bellard82c643f2004-07-14 17:28:13 +00002848 break;
Richard W.M. Jones9dd986c2009-04-25 13:56:19 +01002849 case QEMU_OPTION_watchdog:
Markus Armbruster09aaa162009-08-21 10:31:34 +02002850 if (watchdog) {
2851 fprintf(stderr,
2852 "qemu: only one watchdog option may be given\n");
2853 return 1;
2854 }
2855 watchdog = optarg;
Richard W.M. Jones9dd986c2009-04-25 13:56:19 +01002856 break;
2857 case QEMU_OPTION_watchdog_action:
2858 if (select_watchdog_action(optarg) == -1) {
2859 fprintf(stderr, "Unknown -watchdog-action parameter\n");
2860 exit(1);
2861 }
2862 break;
aliguori51ecf132009-01-15 20:06:40 +00002863 case QEMU_OPTION_virtiocon:
Gerd Hoffmannaee1b932009-12-08 13:11:53 +01002864 add_device_config(DEV_VIRTCON, optarg);
2865 default_virtcon = 0;
Jan Kiszka18141ed2010-03-07 11:28:40 +01002866 if (strncmp(optarg, "mon:", 4) == 0) {
2867 default_monitor = 0;
2868 }
aliguori51ecf132009-01-15 20:06:40 +00002869 break;
bellard6508fe52005-01-15 12:02:56 +00002870 case QEMU_OPTION_parallel:
Gerd Hoffmann6a5e8b02009-12-08 13:11:42 +01002871 add_device_config(DEV_PARALLEL, optarg);
2872 default_parallel = 0;
Jan Kiszka18141ed2010-03-07 11:28:40 +01002873 if (strncmp(optarg, "mon:", 4) == 0) {
2874 default_monitor = 0;
2875 }
bellard6508fe52005-01-15 12:02:56 +00002876 break;
H. Peter Anvinc9f398e2009-12-29 13:51:36 -08002877 case QEMU_OPTION_debugcon:
2878 add_device_config(DEV_DEBUGCON, optarg);
2879 break;
bellardd63d3072004-10-03 13:29:03 +00002880 case QEMU_OPTION_loadvm:
2881 loadvm = optarg;
2882 break;
2883 case QEMU_OPTION_full_screen:
2884 full_screen = 1;
2885 break;
ths667acca2006-12-11 02:08:05 +00002886#ifdef CONFIG_SDL
ths43523e92007-02-18 18:19:32 +00002887 case QEMU_OPTION_no_frame:
2888 no_frame = 1;
2889 break;
ths3780e192007-06-21 21:08:02 +00002890 case QEMU_OPTION_alt_grab:
2891 alt_grab = 1;
2892 break;
Dustin Kirkland0ca9f8a2009-09-17 15:48:04 -05002893 case QEMU_OPTION_ctrl_grab:
2894 ctrl_grab = 1;
2895 break;
ths667acca2006-12-11 02:08:05 +00002896 case QEMU_OPTION_no_quit:
2897 no_quit = 1;
2898 break;
aliguori7d957bd2009-01-15 22:14:11 +00002899 case QEMU_OPTION_sdl:
Anthony Liguori993fbfd2009-05-21 16:54:00 -05002900 display_type = DT_SDL;
aliguori7d957bd2009-01-15 22:14:11 +00002901 break;
Jes Sorensen58fc0962011-03-16 13:33:34 +01002902#else
2903 case QEMU_OPTION_no_frame:
2904 case QEMU_OPTION_alt_grab:
2905 case QEMU_OPTION_ctrl_grab:
2906 case QEMU_OPTION_no_quit:
2907 case QEMU_OPTION_sdl:
2908 fprintf(stderr, "SDL support is disabled\n");
2909 exit(1);
ths667acca2006-12-11 02:08:05 +00002910#endif
bellardf7cce892004-12-08 22:21:25 +00002911 case QEMU_OPTION_pidfile:
ths93815bc2007-03-19 15:58:31 +00002912 pid_file = optarg;
bellardf7cce892004-12-08 22:21:25 +00002913 break;
bellarda09db212005-04-30 16:10:35 +00002914 case QEMU_OPTION_win2k_hack:
2915 win2k_install_hack = 1;
2916 break;
aliguori73822ec2009-01-15 20:11:34 +00002917 case QEMU_OPTION_rtc_td_hack:
2918 rtc_td_hack = 1;
2919 break;
aliguori8a92ea22009-02-27 20:12:36 +00002920 case QEMU_OPTION_acpitable:
Blue Swirlde06f8d2010-03-29 19:23:50 +00002921 do_acpitable_option(optarg);
aliguori8a92ea22009-02-27 20:12:36 +00002922 break;
aliguorib6f6e3d2009-04-17 18:59:56 +00002923 case QEMU_OPTION_smbios:
Blue Swirlde06f8d2010-03-29 19:23:50 +00002924 do_smbios_option(optarg);
aliguorib6f6e3d2009-04-17 18:59:56 +00002925 break;
aliguori7ba1e612008-11-05 16:04:33 +00002926 case QEMU_OPTION_enable_kvm:
Anthony PERARD303d4e82010-09-21 20:05:31 +01002927 olist = qemu_find_opts("machine");
2928 qemu_opts_reset(olist);
2929 qemu_opts_parse(olist, "accel=kvm", 0);
2930 break;
2931 case QEMU_OPTION_machine:
2932 olist = qemu_find_opts("machine");
2933 qemu_opts_reset(olist);
Jan Kiszka9052ea62011-07-23 12:38:37 +02002934 opts = qemu_opts_parse(olist, optarg, 1);
Anthony PERARD303d4e82010-09-21 20:05:31 +01002935 if (!opts) {
2936 fprintf(stderr, "parse error: %s\n", optarg);
2937 exit(1);
2938 }
Jan Kiszka2645c6d2011-07-25 18:11:20 +02002939 optarg = qemu_opt_get(opts, "type");
2940 if (optarg) {
2941 machine = machine_parse(optarg);
2942 }
aliguori7ba1e612008-11-05 16:04:33 +00002943 break;
bellardbb36d472005-11-05 14:22:28 +00002944 case QEMU_OPTION_usb:
2945 usb_enabled = 1;
2946 break;
bellarda594cfb2005-11-06 16:13:29 +00002947 case QEMU_OPTION_usbdevice:
2948 usb_enabled = 1;
Gerd Hoffmannbd3c9482009-07-15 13:59:26 +02002949 add_device_config(DEV_USB, optarg);
2950 break;
2951 case QEMU_OPTION_device:
Gerd Hoffmann3329f072010-08-20 13:52:01 +02002952 if (!qemu_opts_parse(qemu_find_opts("device"), optarg, 1)) {
Gerd Hoffmannf31d07d2009-07-31 12:25:37 +02002953 exit(1);
2954 }
bellarda594cfb2005-11-06 16:13:29 +00002955 break;
bellard6a00d602005-11-21 23:25:50 +00002956 case QEMU_OPTION_smp:
Andre Przywaradc6b1c02009-08-19 15:42:40 +02002957 smp_parse(optarg);
aliguorib2097002008-10-07 20:39:39 +00002958 if (smp_cpus < 1) {
bellard6a00d602005-11-21 23:25:50 +00002959 fprintf(stderr, "Invalid number of CPUs\n");
2960 exit(1);
2961 }
Jes Sorensen6be68d72009-07-23 17:03:42 +02002962 if (max_cpus < smp_cpus) {
2963 fprintf(stderr, "maxcpus must be equal to or greater than "
2964 "smp\n");
2965 exit(1);
2966 }
2967 if (max_cpus > 255) {
2968 fprintf(stderr, "Unsupported number of maxcpus\n");
2969 exit(1);
2970 }
bellard6a00d602005-11-21 23:25:50 +00002971 break;
bellard24236862006-04-30 21:28:36 +00002972 case QEMU_OPTION_vnc:
Jes Sorensen821601e2011-03-16 13:33:36 +01002973#ifdef CONFIG_VNC
Gerd Hoffmann6b62dc22010-08-25 10:51:06 +02002974 display_remote++;
Jes Sorensen821601e2011-03-16 13:33:36 +01002975 vnc_display = optarg;
2976#else
2977 fprintf(stderr, "VNC support is disabled\n");
2978 exit(1);
2979#endif
2980 break;
bellard6515b202006-05-03 22:02:44 +00002981 case QEMU_OPTION_no_acpi:
2982 acpi_enabled = 0;
2983 break;
aliguori16b29ae2008-12-17 23:28:44 +00002984 case QEMU_OPTION_no_hpet:
2985 no_hpet = 1;
2986 break;
Markus Armbruster7d4c3d52009-06-26 19:15:14 +02002987 case QEMU_OPTION_balloon:
2988 if (balloon_parse(optarg) < 0) {
2989 fprintf(stderr, "Unknown -balloon argument %s\n", optarg);
2990 exit(1);
2991 }
Eduardo Habkostdf97b922009-06-10 16:34:08 -03002992 break;
bellardd1beab82006-10-02 19:44:22 +00002993 case QEMU_OPTION_no_reboot:
2994 no_reboot = 1;
2995 break;
aurel32b2f76162008-04-11 21:35:52 +00002996 case QEMU_OPTION_no_shutdown:
2997 no_shutdown = 1;
2998 break;
balrog9467cd42007-05-01 01:34:14 +00002999 case QEMU_OPTION_show_cursor:
3000 cursor_hide = 0;
3001 break;
blueswir18fcb1b92008-09-18 18:29:08 +00003002 case QEMU_OPTION_uuid:
3003 if(qemu_uuid_parse(optarg, qemu_uuid) < 0) {
3004 fprintf(stderr, "Fail to parse UUID string."
3005 " Wrong format.\n");
3006 exit(1);
3007 }
3008 break;
ths9ae02552007-01-05 17:39:04 +00003009 case QEMU_OPTION_option_rom:
3010 if (nb_option_roms >= MAX_OPTION_ROMS) {
3011 fprintf(stderr, "Too many option ROMs\n");
3012 exit(1);
3013 }
Gleb Natapov2e55e842010-12-08 13:35:07 +02003014 opts = qemu_opts_parse(qemu_find_opts("option-rom"), optarg, 1);
3015 option_rom[nb_option_roms].name = qemu_opt_get(opts, "romfile");
3016 option_rom[nb_option_roms].bootindex =
3017 qemu_opt_get_number(opts, "bootindex", -1);
3018 if (!option_rom[nb_option_roms].name) {
3019 fprintf(stderr, "Option ROM file is not specified\n");
3020 exit(1);
3021 }
ths9ae02552007-01-05 17:39:04 +00003022 nb_option_roms++;
3023 break;
pbrook8e716212007-01-20 17:12:09 +00003024 case QEMU_OPTION_semihosting:
3025 semihosting_enabled = 1;
3026 break;
thsc35734b2007-03-19 15:17:08 +00003027 case QEMU_OPTION_name:
Anthony Liguori7267c092011-08-20 22:09:37 -05003028 qemu_name = g_strdup(optarg);
Andi Kleen18894652009-07-02 09:34:17 +02003029 {
3030 char *p = strchr(qemu_name, ',');
3031 if (p != NULL) {
3032 *p++ = 0;
3033 if (strncmp(p, "process=", 8)) {
Aurelien Jarno5697f6a2010-12-27 18:29:20 +01003034 fprintf(stderr, "Unknown subargument %s to -name\n", p);
Andi Kleen18894652009-07-02 09:34:17 +02003035 exit(1);
3036 }
3037 p += 8;
Jes Sorensence798cf2010-06-10 11:42:31 +02003038 os_set_proc_name(p);
Andi Kleen18894652009-07-02 09:34:17 +02003039 }
3040 }
thsc35734b2007-03-19 15:17:08 +00003041 break;
blueswir166508602007-05-01 14:16:52 +00003042 case QEMU_OPTION_prom_env:
3043 if (nb_prom_envs >= MAX_PROM_ENVS) {
3044 fprintf(stderr, "Too many prom variables\n");
3045 exit(1);
3046 }
3047 prom_envs[nb_prom_envs] = optarg;
3048 nb_prom_envs++;
3049 break;
balrog2b8f2d42007-07-27 22:08:46 +00003050 case QEMU_OPTION_old_param:
3051 old_param = 1;
ths05ebd532008-01-08 19:32:16 +00003052 break;
thsf3dcfad2007-08-24 01:26:02 +00003053 case QEMU_OPTION_clock:
3054 configure_alarms(optarg);
3055 break;
bellard7e0af5d02007-11-07 16:24:33 +00003056 case QEMU_OPTION_startdate:
Jan Kiszka1ed2fc12009-09-15 13:36:04 +02003057 configure_rtc_date_offset(optarg, 1);
3058 break;
3059 case QEMU_OPTION_rtc:
Gerd Hoffmann3329f072010-08-20 13:52:01 +02003060 opts = qemu_opts_parse(qemu_find_opts("rtc"), optarg, 0);
Jan Kiszka1ed2fc12009-09-15 13:36:04 +02003061 if (!opts) {
Jan Kiszka1ed2fc12009-09-15 13:36:04 +02003062 exit(1);
bellard7e0af5d02007-11-07 16:24:33 +00003063 }
Jan Kiszka1ed2fc12009-09-15 13:36:04 +02003064 configure_rtc(opts);
bellard7e0af5d02007-11-07 16:24:33 +00003065 break;
bellard26a5f132008-05-28 12:30:31 +00003066 case QEMU_OPTION_tb_size:
Jan Kiszkad5ab9712011-08-02 16:10:21 +02003067 tcg_tb_size = strtol(optarg, NULL, 0);
3068 if (tcg_tb_size < 0) {
3069 tcg_tb_size = 0;
3070 }
bellard26a5f132008-05-28 12:30:31 +00003071 break;
pbrook2e70f6e2008-06-29 01:03:05 +00003072 case QEMU_OPTION_icount:
Paolo Bonzini4e3de9e2010-03-10 11:38:48 +01003073 icount_option = optarg;
pbrook2e70f6e2008-06-29 01:03:05 +00003074 break;
aliguori5bb79102008-10-13 03:12:02 +00003075 case QEMU_OPTION_incoming:
3076 incoming = optarg;
3077 break;
Gerd Hoffmannd8c208d2009-12-08 13:11:46 +01003078 case QEMU_OPTION_nodefaults:
3079 default_serial = 0;
3080 default_parallel = 0;
Gerd Hoffmannaee1b932009-12-08 13:11:53 +01003081 default_virtcon = 0;
Gerd Hoffmannd8c208d2009-12-08 13:11:46 +01003082 default_monitor = 0;
3083 default_vga = 0;
Gerd Hoffmanncb4522c2009-12-08 13:11:47 +01003084 default_net = 0;
Gerd Hoffmannac33f8f2009-12-16 14:25:39 +01003085 default_floppy = 0;
3086 default_cdrom = 0;
3087 default_sdcard = 0;
Gerd Hoffmannd8c208d2009-12-08 13:11:46 +01003088 break;
aliguorie37630c2009-04-22 15:19:10 +00003089 case QEMU_OPTION_xen_domid:
Blue Swirlad960902010-03-29 19:23:52 +00003090 if (!(xen_available())) {
3091 printf("Option %s not supported for this target\n", popt->name);
3092 exit(1);
3093 }
aliguorie37630c2009-04-22 15:19:10 +00003094 xen_domid = atoi(optarg);
3095 break;
3096 case QEMU_OPTION_xen_create:
Blue Swirlad960902010-03-29 19:23:52 +00003097 if (!(xen_available())) {
3098 printf("Option %s not supported for this target\n", popt->name);
3099 exit(1);
3100 }
aliguorie37630c2009-04-22 15:19:10 +00003101 xen_mode = XEN_CREATE;
3102 break;
3103 case QEMU_OPTION_xen_attach:
Blue Swirlad960902010-03-29 19:23:52 +00003104 if (!(xen_available())) {
3105 printf("Option %s not supported for this target\n", popt->name);
3106 exit(1);
3107 }
aliguorie37630c2009-04-22 15:19:10 +00003108 xen_mode = XEN_ATTACH;
3109 break;
Prerna Saxenaab6540d2010-08-09 11:48:32 +01003110 case QEMU_OPTION_trace:
Lluíse4858972011-08-31 20:31:03 +02003111 {
Prerna Saxenaab6540d2010-08-09 11:48:32 +01003112 opts = qemu_opts_parse(qemu_find_opts("trace"), optarg, 0);
Lluíse4858972011-08-31 20:31:03 +02003113 if (!opts) {
3114 exit(1);
Prerna Saxenaab6540d2010-08-09 11:48:32 +01003115 }
Lluís23d15e82011-08-31 20:31:31 +02003116 trace_events = qemu_opt_get(opts, "events");
Lluíse4858972011-08-31 20:31:03 +02003117 trace_file = qemu_opt_get(opts, "file");
Prerna Saxenaab6540d2010-08-09 11:48:32 +01003118 break;
Lluíse4858972011-08-31 20:31:03 +02003119 }
Gerd Hoffmann715a6642009-10-14 10:39:28 +02003120 case QEMU_OPTION_readconfig:
3121 {
Kevin Wolfdcfb0932010-03-05 17:25:55 +01003122 int ret = qemu_read_config_file(optarg);
3123 if (ret < 0) {
3124 fprintf(stderr, "read config %s: %s\n", optarg,
3125 strerror(-ret));
Gerd Hoffmann715a6642009-10-14 10:39:28 +02003126 exit(1);
3127 }
Gerd Hoffmann715a6642009-10-14 10:39:28 +02003128 break;
3129 }
Gerd Hoffmann29b00402010-03-11 11:13:27 -03003130 case QEMU_OPTION_spice:
3131 olist = qemu_find_opts("spice");
3132 if (!olist) {
3133 fprintf(stderr, "spice is not supported by this qemu build.\n");
3134 exit(1);
3135 }
3136 opts = qemu_opts_parse(olist, optarg, 0);
3137 if (!opts) {
3138 fprintf(stderr, "parse error: %s\n", optarg);
3139 exit(1);
3140 }
3141 break;
Gerd Hoffmann715a6642009-10-14 10:39:28 +02003142 case QEMU_OPTION_writeconfig:
3143 {
3144 FILE *fp;
3145 if (strcmp(optarg, "-") == 0) {
3146 fp = stdout;
3147 } else {
3148 fp = fopen(optarg, "w");
3149 if (fp == NULL) {
3150 fprintf(stderr, "open %s: %s\n", optarg, strerror(errno));
3151 exit(1);
3152 }
3153 }
3154 qemu_config_write(fp);
3155 fclose(fp);
3156 break;
3157 }
Jes Sorensen59a52642010-06-10 11:42:25 +02003158 default:
3159 os_parse_cmd_args(popt->index, optarg);
bellardcd6f1162004-05-13 22:02:20 +00003160 }
bellard0824d6f2003-06-24 13:42:40 +00003161 }
3162 }
Markus Armbruster0f0bc3f2010-02-18 20:13:51 +01003163 loc_set_none();
bellard330d0412003-07-26 18:11:40 +00003164
Matthew Fernandezc235d732011-06-07 16:32:40 +00003165 /* Open the logfile at this point, if necessary. We can't open the logfile
3166 * when encountering either of the logging options (-d or -D) because the
3167 * other one may be encountered later on the command line, changing the
3168 * location or level of logging.
3169 */
3170 if (log_mask) {
3171 if (log_file) {
3172 set_cpu_log_filename(log_file);
3173 }
3174 set_cpu_log(log_mask);
3175 }
3176
Lluís23d15e82011-08-31 20:31:31 +02003177 if (!trace_backend_init(trace_events, trace_file)) {
Lluíse4858972011-08-31 20:31:03 +02003178 exit(1);
Stefan Hajnoczi31d3c9b2011-03-13 20:14:30 +00003179 }
Stefan Hajnoczi0b5538c2011-02-26 18:38:39 +00003180
Paul Brook5cea8592009-05-30 00:52:44 +01003181 /* If no data_dir is specified then try to find it relative to the
3182 executable path. */
3183 if (!data_dir) {
Jes Sorensen61705402010-06-10 11:42:23 +02003184 data_dir = os_find_datadir(argv[0]);
Paul Brook5cea8592009-05-30 00:52:44 +01003185 }
Stefan Weil60474fb2011-09-04 15:17:46 +02003186 /* If all else fails use the install path specified when building. */
Paul Brook5cea8592009-05-30 00:52:44 +01003187 if (!data_dir) {
Paolo Bonzini1dabe052010-05-26 16:08:25 +02003188 data_dir = CONFIG_QEMU_DATADIR;
Paul Brook5cea8592009-05-30 00:52:44 +01003189 }
3190
Jes Sorensen6be68d72009-07-23 17:03:42 +02003191 /*
3192 * Default to max_cpus = smp_cpus, in case the user doesn't
3193 * specify a max_cpus value.
3194 */
3195 if (!max_cpus)
3196 max_cpus = smp_cpus;
3197
balrog3d878ca2008-10-28 10:59:59 +00003198 machine->max_cpus = machine->max_cpus ?: 1; /* Default to UP */
aliguorib2097002008-10-07 20:39:39 +00003199 if (smp_cpus > machine->max_cpus) {
3200 fprintf(stderr, "Number of SMP cpus requested (%d), exceeds max cpus "
3201 "supported by machine `%s' (%d)\n", smp_cpus, machine->name,
3202 machine->max_cpus);
3203 exit(1);
3204 }
3205
Anthony PERARD67b724e2010-11-22 15:44:15 +00003206 /*
3207 * Get the default machine options from the machine if it is not already
3208 * specified either by the configuration file or by the command line.
3209 */
3210 if (machine->default_machine_opts) {
3211 QemuOptsList *list = qemu_find_opts("machine");
3212 const char *p = NULL;
3213
3214 if (!QTAILQ_EMPTY(&list->head)) {
3215 p = qemu_opt_get(QTAILQ_FIRST(&list->head), "accel");
3216 }
3217 if (p == NULL) {
Jan Kiszka9052ea62011-07-23 12:38:37 +02003218 qemu_opts_reset(list);
3219 opts = qemu_opts_parse(list, machine->default_machine_opts, 0);
Anthony PERARD67b724e2010-11-22 15:44:15 +00003220 if (!opts) {
3221 fprintf(stderr, "parse error for machine %s: %s\n",
3222 machine->name, machine->default_machine_opts);
3223 exit(1);
3224 }
3225 }
3226 }
3227
Gerd Hoffmann3329f072010-08-20 13:52:01 +02003228 qemu_opts_foreach(qemu_find_opts("device"), default_driver_check, NULL, 0);
3229 qemu_opts_foreach(qemu_find_opts("global"), default_driver_check, NULL, 0);
Gerd Hoffmann998bbd72009-12-08 13:11:41 +01003230
Gerd Hoffmann986c5f72009-12-08 13:11:54 +01003231 if (machine->no_serial) {
3232 default_serial = 0;
3233 }
3234 if (machine->no_parallel) {
3235 default_parallel = 0;
3236 }
3237 if (!machine->use_virtcon) {
3238 default_virtcon = 0;
3239 }
3240 if (machine->no_vga) {
3241 default_vga = 0;
3242 }
Gerd Hoffmannac33f8f2009-12-16 14:25:39 +01003243 if (machine->no_floppy) {
3244 default_floppy = 0;
3245 }
3246 if (machine->no_cdrom) {
3247 default_cdrom = 0;
3248 }
3249 if (machine->no_sdcard) {
3250 default_sdcard = 0;
3251 }
Gerd Hoffmann986c5f72009-12-08 13:11:54 +01003252
Anthony Liguori993fbfd2009-05-21 16:54:00 -05003253 if (display_type == DT_NOGRAPHIC) {
Gerd Hoffmann6a5e8b02009-12-08 13:11:42 +01003254 if (default_parallel)
3255 add_device_config(DEV_PARALLEL, "null");
Gerd Hoffmanne1c09172009-12-08 13:11:44 +01003256 if (default_serial && default_monitor) {
3257 add_device_config(DEV_SERIAL, "mon:stdio");
Gerd Hoffmann986c5f72009-12-08 13:11:54 +01003258 } else if (default_virtcon && default_monitor) {
3259 add_device_config(DEV_VIRTCON, "mon:stdio");
Gerd Hoffmanne1c09172009-12-08 13:11:44 +01003260 } else {
3261 if (default_serial)
3262 add_device_config(DEV_SERIAL, "stdio");
Gerd Hoffmann986c5f72009-12-08 13:11:54 +01003263 if (default_virtcon)
3264 add_device_config(DEV_VIRTCON, "stdio");
Gerd Hoffmanne1c09172009-12-08 13:11:44 +01003265 if (default_monitor)
Gerd Hoffmann6ca55822009-12-08 13:11:52 +01003266 monitor_parse("stdio", "readline");
Gerd Hoffmanne1c09172009-12-08 13:11:44 +01003267 }
Gerd Hoffmann998bbd72009-12-08 13:11:41 +01003268 } else {
3269 if (default_serial)
3270 add_device_config(DEV_SERIAL, "vc:80Cx24C");
Gerd Hoffmann6a5e8b02009-12-08 13:11:42 +01003271 if (default_parallel)
3272 add_device_config(DEV_PARALLEL, "vc:80Cx24C");
Gerd Hoffmannabdeed02009-12-08 13:11:43 +01003273 if (default_monitor)
Gerd Hoffmann6ca55822009-12-08 13:11:52 +01003274 monitor_parse("vc:80Cx24C", "readline");
Alexander Graf38536da2009-12-17 13:06:08 +01003275 if (default_virtcon)
3276 add_device_config(DEV_VIRTCON, "vc:80Cx24C");
aliguoribc0129d2008-08-01 15:12:34 +00003277 }
Gerd Hoffmann64465292009-12-08 13:11:45 +01003278 if (default_vga)
3279 vga_interface_type = VGA_CIRRUS;
aliguoribc0129d2008-08-01 15:12:34 +00003280
TeLeMana5829fd2010-04-15 12:37:55 +08003281 socket_init();
3282
Gerd Hoffmann3329f072010-08-20 13:52:01 +02003283 if (qemu_opts_foreach(qemu_find_opts("chardev"), chardev_init_func, NULL, 1) != 0)
Gerd Hoffmann1a688d32009-12-08 13:11:36 +01003284 exit(1);
Venkateswararao Jujjuri (JV)758e8e32010-06-14 13:34:41 -07003285#ifdef CONFIG_VIRTFS
Gerd Hoffmann3329f072010-08-20 13:52:01 +02003286 if (qemu_opts_foreach(qemu_find_opts("fsdev"), fsdev_init_func, NULL, 1) != 0) {
Gautham R Shenoy74db9202010-04-29 17:44:43 +05303287 exit(1);
3288 }
3289#endif
Gerd Hoffmann1a688d32009-12-08 13:11:36 +01003290
Jes Sorenseneb505be2010-06-10 11:42:28 +02003291 os_daemonize();
ths71e3ceb2006-12-22 02:11:31 +00003292
thsaa26bb22007-03-25 21:33:06 +00003293 if (pid_file && qemu_create_pidfile(pid_file) != 0) {
Jes Sorenseneb505be2010-06-10 11:42:28 +02003294 os_pidfile_error();
ths93815bc2007-03-19 15:58:31 +00003295 exit(1);
3296 }
3297
Jan Kiszka0ac543d2011-08-15 16:18:57 -07003298 /* init the memory */
3299 if (ram_size == 0) {
3300 ram_size = DEFAULT_RAM_SIZE * 1024 * 1024;
3301 }
3302
Anthony PERARD303d4e82010-09-21 20:05:31 +01003303 configure_accelerator();
Marcelo Tosatti214910a2009-09-18 02:41:23 -03003304
aliguori3fcf7b62009-04-24 18:03:25 +00003305 if (qemu_init_main_loop()) {
3306 fprintf(stderr, "qemu_init_main_loop failed\n");
3307 exit(1);
3308 }
bellarda20dd502003-09-30 21:07:02 +00003309 linux_boot = (kernel_filename != NULL);
balrog6c41b272007-11-17 12:12:29 +00003310
thsf8d39c02008-07-03 10:01:15 +00003311 if (!linux_boot && *kernel_cmdline != '\0') {
3312 fprintf(stderr, "-append only allowed with -kernel option\n");
3313 exit(1);
3314 }
3315
3316 if (!linux_boot && initrd_filename != NULL) {
3317 fprintf(stderr, "-initrd only allowed with -kernel option\n");
3318 exit(1);
3319 }
3320
Jes Sorensen9156d762010-06-10 11:42:30 +02003321 os_set_line_buffering();
ths3b46e622007-09-17 08:09:54 +00003322
aliguori7183b4b2008-11-05 20:40:18 +00003323 if (init_timer_alarm() < 0) {
3324 fprintf(stderr, "could not initialize alarm timer\n");
3325 exit(1);
3326 }
Paolo Bonzini4e3de9e2010-03-10 11:38:48 +01003327 configure_icount(icount_option);
pbrook634fce92006-07-15 17:40:09 +00003328
Mark McLoughlindc1c9fe2009-10-06 12:17:16 +01003329 if (net_init_clients() < 0) {
3330 exit(1);
bellard702c6512004-04-02 21:21:32 +00003331 }
bellardf1510b22003-06-25 00:07:40 +00003332
balrogdc72ac12008-11-09 00:04:26 +00003333 /* init the bluetooth world */
Gerd Hoffmannbd3c9482009-07-15 13:59:26 +02003334 if (foreach_device_config(DEV_BT, bt_parse))
3335 exit(1);
balrogdc72ac12008-11-09 00:04:26 +00003336
Anthony PERARD834e76e2011-07-20 08:17:44 +00003337 if (!xen_enabled()) {
3338 /* On 32-bit hosts, QEMU is limited by virtual address space */
3339 if (ram_size > (2047 << 20) && HOST_LONG_BITS == 32) {
3340 fprintf(stderr, "qemu: at most 2047 MB RAM can be simulated\n");
3341 exit(1);
3342 }
3343 }
balrog7fb4fdc2008-04-24 17:59:27 +00003344
Jan Kiszkad5ab9712011-08-02 16:10:21 +02003345 cpu_exec_init_all();
bellard26a5f132008-05-28 12:30:31 +00003346
Markus Armbrustereb852012009-10-27 18:41:44 +01003347 bdrv_init_with_whitelist();
thse4bcb142007-12-02 04:51:10 +00003348
lirans@il.ibm.comc163b5c2009-11-02 15:40:58 +02003349 blk_mig_init();
3350
ths96d30e42007-01-07 20:42:14 +00003351 /* open the virtual block devices */
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +02003352 if (snapshot)
Gerd Hoffmann3329f072010-08-20 13:52:01 +02003353 qemu_opts_foreach(qemu_find_opts("drive"), drive_enable_snapshot, NULL, 0);
3354 if (qemu_opts_foreach(qemu_find_opts("drive"), drive_init_func, &machine->use_scsi, 1) != 0)
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +02003355 exit(1);
balrog3e3d5812007-04-30 02:09:25 +00003356
Markus Armbruster4e5d9b52011-01-28 11:21:45 +01003357 default_drive(default_cdrom, snapshot, machine->use_scsi,
3358 IF_DEFAULT, 2, CDROM_OPTS);
3359 default_drive(default_floppy, snapshot, machine->use_scsi,
3360 IF_FLOPPY, 0, FD_OPTS);
3361 default_drive(default_sdcard, snapshot, machine->use_scsi,
3362 IF_SD, 0, SD_OPTS);
3363
Alex Williamson97ab12d2010-06-25 11:09:50 -06003364 register_savevm_live(NULL, "ram", 0, 4, NULL, ram_save_live, NULL,
lirans@il.ibm.comc163b5c2009-11-02 15:40:58 +02003365 ram_load, NULL);
bellard8a7ddc32004-03-31 19:00:16 +00003366
aliguori268a3622009-04-21 22:30:27 +00003367 if (nb_numa_nodes > 0) {
3368 int i;
3369
Sasha Levinea0e5412011-06-29 23:29:39 -04003370 if (nb_numa_nodes > MAX_NODES) {
3371 nb_numa_nodes = MAX_NODES;
aliguori268a3622009-04-21 22:30:27 +00003372 }
3373
3374 /* If no memory size if given for any node, assume the default case
3375 * and distribute the available memory equally across all nodes
3376 */
3377 for (i = 0; i < nb_numa_nodes; i++) {
3378 if (node_mem[i] != 0)
3379 break;
3380 }
3381 if (i == nb_numa_nodes) {
3382 uint64_t usedmem = 0;
3383
3384 /* On Linux, the each node's border has to be 8MB aligned,
3385 * the final node gets the rest.
3386 */
3387 for (i = 0; i < nb_numa_nodes - 1; i++) {
3388 node_mem[i] = (ram_size / nb_numa_nodes) & ~((1 << 23UL) - 1);
3389 usedmem += node_mem[i];
3390 }
3391 node_mem[i] = ram_size - usedmem;
3392 }
3393
3394 for (i = 0; i < nb_numa_nodes; i++) {
3395 if (node_cpumask[i] != 0)
3396 break;
3397 }
3398 /* assigning the VCPUs round-robin is easier to implement, guest OSes
3399 * must cope with this anyway, because there are BIOSes out there in
3400 * real machines which also use this scheme.
3401 */
3402 if (i == nb_numa_nodes) {
3403 for (i = 0; i < smp_cpus; i++) {
3404 node_cpumask[i % nb_numa_nodes] |= 1 << i;
3405 }
3406 }
3407 }
3408
Gerd Hoffmann3329f072010-08-20 13:52:01 +02003409 if (qemu_opts_foreach(qemu_find_opts("mon"), mon_init_func, NULL, 1) != 0) {
Jan Kiszka157b9312010-04-06 16:55:53 +02003410 exit(1);
3411 }
3412
Gerd Hoffmann998bbd72009-12-08 13:11:41 +01003413 if (foreach_device_config(DEV_SERIAL, serial_parse) < 0)
3414 exit(1);
Gerd Hoffmann6a5e8b02009-12-08 13:11:42 +01003415 if (foreach_device_config(DEV_PARALLEL, parallel_parse) < 0)
3416 exit(1);
Gerd Hoffmannaee1b932009-12-08 13:11:53 +01003417 if (foreach_device_config(DEV_VIRTCON, virtcon_parse) < 0)
3418 exit(1);
H. Peter Anvinc9f398e2009-12-29 13:51:36 -08003419 if (foreach_device_config(DEV_DEBUGCON, debugcon_parse) < 0)
3420 exit(1);
aliguori2796dae2009-01-16 20:23:27 +00003421
Paul Brookaae94602009-05-14 22:35:06 +01003422 module_call_init(MODULE_INIT_DEVICE);
3423
Gerd Hoffmann3329f072010-08-20 13:52:01 +02003424 if (qemu_opts_foreach(qemu_find_opts("device"), device_help_func, NULL, 0) != 0)
Markus Armbrusterff952ba2010-01-29 19:48:57 +01003425 exit(0);
3426
Markus Armbruster09aaa162009-08-21 10:31:34 +02003427 if (watchdog) {
3428 i = select_watchdog(watchdog);
3429 if (i > 0)
3430 exit (i == 1 ? 1 : 0);
3431 }
3432
Gerd Hoffmannb6b61142009-07-15 13:48:21 +02003433 if (machine->compat_props) {
Gerd Hoffmann458fb672009-12-08 13:11:33 +01003434 qdev_prop_register_global_list(machine->compat_props);
Gerd Hoffmannb6b61142009-07-15 13:48:21 +02003435 }
Gerd Hoffmannd0fef6f2009-12-08 13:11:34 +01003436 qemu_add_globals();
3437
Paul Brookfbe1b592009-05-13 17:56:25 +01003438 machine->init(ram_size, boot_devices,
aliguori3023f332009-01-16 19:04:14 +00003439 kernel_filename, kernel_cmdline, initrd_filename, cpu_model);
3440
Jan Kiszkaea375f92010-03-01 19:10:30 +01003441 cpu_synchronize_all_post_init();
aliguori268a3622009-04-21 22:30:27 +00003442
Blue Swirl87d0a282010-03-27 18:24:45 +00003443 set_numa_modes();
aliguori268a3622009-04-21 22:30:27 +00003444
aliguori6f338c32009-02-11 15:21:54 +00003445 current_machine = machine;
3446
aliguori3023f332009-01-16 19:04:14 +00003447 /* init USB devices */
3448 if (usb_enabled) {
Markus Armbruster07527062009-10-06 12:16:57 +01003449 if (foreach_device_config(DEV_USB, usb_parse) < 0)
3450 exit(1);
aliguori3023f332009-01-16 19:04:14 +00003451 }
3452
Gerd Hoffmannbd3c9482009-07-15 13:59:26 +02003453 /* init generic devices */
Gerd Hoffmann3329f072010-08-20 13:52:01 +02003454 if (qemu_opts_foreach(qemu_find_opts("device"), device_init_func, NULL, 1) != 0)
Gerd Hoffmannbd3c9482009-07-15 13:59:26 +02003455 exit(1);
3456
Markus Armbruster668680f2010-02-11 14:44:58 +01003457 net_check_clients();
3458
aliguori3023f332009-01-16 19:04:14 +00003459 /* just use the first displaystate for the moment */
Paolo Bonzinib473df62010-02-11 00:29:55 +01003460 ds = get_displaystate();
Anthony Liguori993fbfd2009-05-21 16:54:00 -05003461
Gerd Hoffmanna3e22262010-08-25 15:32:06 +02003462 if (using_spice)
3463 display_remote++;
Gerd Hoffmann6b62dc22010-08-25 10:51:06 +02003464 if (display_type == DT_DEFAULT && !display_remote) {
Anthony Liguorif92f8af2009-05-20 13:01:02 -05003465#if defined(CONFIG_SDL) || defined(CONFIG_COCOA)
Anthony Liguori993fbfd2009-05-21 16:54:00 -05003466 display_type = DT_SDL;
Jes Sorensen821601e2011-03-16 13:33:36 +01003467#elif defined(CONFIG_VNC)
Anthony Liguori993fbfd2009-05-21 16:54:00 -05003468 vnc_display = "localhost:0,to=99";
3469 show_vnc_port = 1;
Jes Sorensen821601e2011-03-16 13:33:36 +01003470#else
3471 display_type = DT_NONE;
Anthony Liguori993fbfd2009-05-21 16:54:00 -05003472#endif
3473 }
Jes Sorensen821601e2011-03-16 13:33:36 +01003474
Anthony Liguori993fbfd2009-05-21 16:54:00 -05003475
Gerd Hoffmann6b62dc22010-08-25 10:51:06 +02003476 /* init local displays */
Anthony Liguori993fbfd2009-05-21 16:54:00 -05003477 switch (display_type) {
3478 case DT_NOGRAPHIC:
3479 break;
3480#if defined(CONFIG_CURSES)
3481 case DT_CURSES:
3482 curses_display_init(ds, full_screen);
3483 break;
3484#endif
bellard5b0753e2005-03-01 21:37:28 +00003485#if defined(CONFIG_SDL)
Anthony Liguori993fbfd2009-05-21 16:54:00 -05003486 case DT_SDL:
3487 sdl_display_init(ds, full_screen, no_frame);
3488 break;
bellard5b0753e2005-03-01 21:37:28 +00003489#elif defined(CONFIG_COCOA)
Anthony Liguori993fbfd2009-05-21 16:54:00 -05003490 case DT_SDL:
3491 cocoa_display_init(ds, full_screen);
3492 break;
bellard313aa562003-08-10 21:52:11 +00003493#endif
Gerd Hoffmann6b62dc22010-08-25 10:51:06 +02003494 default:
3495 break;
3496 }
3497
Gleb Natapov0ce235a2011-03-31 11:27:23 +02003498 /* must be after terminal init, SDL library changes signal handlers */
3499 os_setup_signal_handling();
3500
Jes Sorensen821601e2011-03-16 13:33:36 +01003501#ifdef CONFIG_VNC
Gerd Hoffmann6b62dc22010-08-25 10:51:06 +02003502 /* init remote displays */
3503 if (vnc_display) {
Anthony Liguori993fbfd2009-05-21 16:54:00 -05003504 vnc_display_init(ds);
3505 if (vnc_display_open(ds, vnc_display) < 0)
3506 exit(1);
Anthony Liguorif92f8af2009-05-20 13:01:02 -05003507
Anthony Liguori993fbfd2009-05-21 16:54:00 -05003508 if (show_vnc_port) {
3509 printf("VNC server running on `%s'\n", vnc_display_local_addr(ds));
Anthony Liguorif92f8af2009-05-20 13:01:02 -05003510 }
bellard313aa562003-08-10 21:52:11 +00003511 }
Jes Sorensen821601e2011-03-16 13:33:36 +01003512#endif
Gerd Hoffmanna3e22262010-08-25 15:32:06 +02003513#ifdef CONFIG_SPICE
Gerd Hoffmanna19cbfb2010-04-27 11:50:11 +02003514 if (using_spice && !qxl_enabled) {
Gerd Hoffmanna3e22262010-08-25 15:32:06 +02003515 qemu_spice_display_init(ds);
3516 }
3517#endif
aliguori5b08fc12008-08-21 20:08:03 +00003518
Gerd Hoffmann6b62dc22010-08-25 10:51:06 +02003519 /* display setup */
3520 dpy_resize(ds);
aliguori3023f332009-01-16 19:04:14 +00003521 dcl = ds->listeners;
3522 while (dcl != NULL) {
3523 if (dcl->dpy_refresh != NULL) {
Paolo Bonzini7bd427d2011-03-11 16:47:48 +01003524 ds->gui_timer = qemu_new_timer_ms(rt_clock, gui_update, ds);
3525 qemu_mod_timer(ds->gui_timer, qemu_get_clock_ms(rt_clock));
Isaku Yamahata0f2ad632010-05-27 14:38:47 +09003526 break;
ths20d8a3e2007-02-18 17:04:49 +00003527 }
aliguori3023f332009-01-16 19:04:14 +00003528 dcl = dcl->next;
bellard82c643f2004-07-14 17:28:13 +00003529 }
Gerd Hoffmann6b62dc22010-08-25 10:51:06 +02003530 if (ds->gui_timer == NULL) {
Paolo Bonzini7bd427d2011-03-11 16:47:48 +01003531 nographic_timer = qemu_new_timer_ms(rt_clock, nographic_update, NULL);
3532 qemu_mod_timer(nographic_timer, qemu_get_clock_ms(rt_clock));
blueswir19043b622009-01-21 19:28:13 +00003533 }
Paolo Bonzinib473df62010-02-11 00:29:55 +01003534 text_consoles_set_display(ds);
aliguori2796dae2009-01-16 20:23:27 +00003535
aliguori59030a82009-04-05 18:43:41 +00003536 if (gdbstub_dev && gdbserver_start(gdbstub_dev) < 0) {
3537 fprintf(stderr, "qemu: could not open gdbserver on device '%s'\n",
3538 gdbstub_dev);
3539 exit(1);
balrog45669e02007-07-02 13:20:17 +00003540 }
balrog45669e02007-07-02 13:20:17 +00003541
Gerd Hoffmann3418bd22009-09-25 21:42:41 +02003542 qdev_machine_creation_done();
3543
Gerd Hoffmann15ff7702009-12-14 16:07:35 +01003544 if (rom_load_all() != 0) {
3545 fprintf(stderr, "rom loading failed\n");
3546 exit(1);
3547 }
Gerd Hoffmann45a50b12009-10-01 16:42:33 +02003548
Isaku Yamahata80376c32010-12-20 14:33:35 +09003549 /* TODO: once all bus devices are qdevified, this should be done
3550 * when bus is created by qdev.c */
3551 qemu_register_reset(qbus_reset_all_fn, sysbus_get_default());
Gleb Natapov4cab9462010-12-08 13:35:08 +02003552 qemu_run_machine_init_done_notifiers();
3553
Jan Kiszkae063eb12011-06-14 18:29:43 +02003554 qemu_system_reset(VMRESET_SILENT);
Juan Quintela05f24012009-08-20 19:42:22 +02003555 if (loadvm) {
Markus Armbruster03cd4652010-02-17 16:24:10 +01003556 if (load_vmstate(loadvm) < 0) {
Juan Quintela05f24012009-08-20 19:42:22 +02003557 autostart = 0;
3558 }
3559 }
bellardd63d3072004-10-03 13:29:03 +00003560
Glauber Costa2bb8c102009-07-24 16:20:23 -04003561 if (incoming) {
Luiz Capitulino0461d5a2011-09-30 14:45:27 -03003562 runstate_set(RUN_STATE_INMIGRATE);
Juan Quintela8ca5e802010-06-09 14:10:54 +02003563 int ret = qemu_start_incoming_migration(incoming);
3564 if (ret < 0) {
3565 fprintf(stderr, "Migration failed. Exit code %s(%d), exiting.\n",
3566 incoming, ret);
3567 exit(ret);
3568 }
Avi Kivity6b99dad2009-08-09 14:39:20 +03003569 } else if (autostart) {
aliguoric0f4ce72009-03-05 23:01:01 +00003570 vm_start();
Avi Kivity6b99dad2009-08-09 14:39:20 +03003571 }
thsffd843b2006-12-21 19:46:43 +00003572
Jes Sorenseneb505be2010-06-10 11:42:28 +02003573 os_setup_post();
ths71e3ceb2006-12-22 02:11:31 +00003574
bellard8a7ddc32004-03-31 19:00:16 +00003575 main_loop();
bellard40c3bac2004-04-04 12:56:28 +00003576 quit_timers();
aliguori63a01ef2008-10-31 19:10:00 +00003577 net_cleanup();
wayne3d3b8302011-07-27 18:04:55 +08003578 res_free();
thsb46a8902007-10-21 23:20:45 +00003579
bellard0824d6f2003-06-24 13:42:40 +00003580 return 0;
3581}