blob: 84054295fb6824178d7e60ebd7d4ee72929b90a6 [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
blueswir1511d2b12009-03-07 15:32:56 +0000114#include "hw/hw.h"
115#include "hw/boards.h"
116#include "hw/usb.h"
117#include "hw/pcmcia.h"
118#include "hw/pc.h"
blueswir1511d2b12009-03-07 15:32:56 +0000119#include "hw/isa.h"
120#include "hw/baum.h"
121#include "hw/bt.h"
Richard W.M. Jones9dd986c2009-04-25 13:56:19 +0100122#include "hw/watchdog.h"
aliguorib6f6e3d2009-04-17 18:59:56 +0000123#include "hw/smbios.h"
aliguorie37630c2009-04-22 15:19:10 +0000124#include "hw/xen.h"
Gerd Hoffmannbd3c9482009-07-15 13:59:26 +0200125#include "hw/qdev.h"
Gerd Hoffmann45a50b12009-10-01 16:42:33 +0200126#include "hw/loader.h"
aurel325ef4efa2009-03-10 21:43:35 +0000127#include "bt-host.h"
blueswir1511d2b12009-03-07 15:32:56 +0000128#include "net.h"
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000129#include "net/slirp.h"
blueswir1511d2b12009-03-07 15:32:56 +0000130#include "monitor.h"
131#include "console.h"
132#include "sysemu.h"
133#include "gdbstub.h"
134#include "qemu-timer.h"
135#include "qemu-char.h"
136#include "cache-utils.h"
137#include "block.h"
Markus Armbruster666daa62010-06-02 18:48:27 +0200138#include "blockdev.h"
lirans@il.ibm.comc163b5c2009-11-02 15:40:58 +0200139#include "block-migration.h"
blueswir1a718ace2009-03-28 08:24:44 +0000140#include "dma.h"
blueswir1511d2b12009-03-07 15:32:56 +0000141#include "audio/audio.h"
142#include "migration.h"
143#include "kvm.h"
Kevin Wolfd3f24362009-05-18 16:42:09 +0200144#include "qemu-option.h"
Gerd Hoffmann7282a032009-07-31 12:25:35 +0200145#include "qemu-config.h"
Luiz Capitulinoe78c48e2009-12-10 17:16:04 -0200146#include "qemu-objects.h"
Jes Sorensen59a52642010-06-10 11:42:25 +0200147#include "qemu-options.h"
Venkateswararao Jujjuri (JV)758e8e32010-06-14 13:34:41 -0700148#ifdef CONFIG_VIRTFS
Gautham R Shenoy74db9202010-04-29 17:44:43 +0530149#include "fsdev/qemu-fsdev.h"
150#endif
blueswir1511d2b12009-03-07 15:32:56 +0000151
bellard0824d6f2003-06-24 13:42:40 +0000152#include "disas.h"
bellardfc01f7e2003-06-30 10:03:06 +0000153
blueswir1511d2b12009-03-07 15:32:56 +0000154#include "qemu_socket.h"
155
Jan Kiszkad918f232009-06-24 14:42:30 +0200156#include "slirp/libslirp.h"
blueswir1511d2b12009-03-07 15:32:56 +0000157
Stefan Hajnoczi94b0b5f2010-11-16 12:20:25 +0000158#include "trace.h"
Stefan Hajnoczi0b5538c2011-02-26 18:38:39 +0000159#include "simpletrace.h"
Blue Swirl72cf2d42009-09-12 07:36:22 +0000160#include "qemu-queue.h"
Blue Swirl296af7c2010-03-29 19:23:50 +0000161#include "cpus.h"
Blue Swirlad960902010-03-29 19:23:52 +0000162#include "arch_init.h"
Blue Swirl72cf2d42009-09-12 07:36:22 +0000163
Gerd Hoffmann29b00402010-03-11 11:13:27 -0300164#include "ui/qemu-spice.h"
165
blueswir19dc63a12008-10-04 07:25:46 +0000166//#define DEBUG_NET
167//#define DEBUG_SLIRP
bellard330d0412003-07-26 18:11:40 +0000168
bellard1bfe8562004-07-08 21:17:50 +0000169#define DEFAULT_RAM_SIZE 128
bellard313aa562003-08-10 21:52:11 +0000170
Amit Shah98b19252010-01-20 00:36:52 +0530171#define MAX_VIRTIO_CONSOLES 1
172
Paul Brook5cea8592009-05-30 00:52:44 +0100173static const char *data_dir;
j_mayer1192dad2007-10-05 13:08:35 +0000174const char *bios_name = NULL;
malccb5a7aa2008-09-28 00:42:12 +0000175enum vga_retrace_method vga_retrace_method = VGA_RETRACE_DUMB;
Anthony Liguori993fbfd2009-05-21 16:54:00 -0500176DisplayType display_type = DT_DEFAULT;
Gerd Hoffmann6b62dc22010-08-25 10:51:06 +0200177int display_remote = 0;
bellard3d11d0e2004-12-12 16:56:30 +0000178const char* keyboard_layout = NULL;
Anthony Liguoric227f092009-10-01 16:12:16 -0500179ram_addr_t ram_size;
Marcelo Tosattic9027602010-03-01 20:25:08 -0300180const char *mem_path = NULL;
181#ifdef MAP_POPULATE
182int mem_prealloc = 0; /* force preallocation of physical target memory */
183#endif
bellardc4b1fcc2004-03-14 21:44:30 +0000184int nb_nics;
bellard7c9d8e02005-11-15 22:16:05 +0000185NICInfo nd_table[MAX_NICS];
bellard8a7ddc32004-03-31 19:00:16 +0000186int vm_running;
Paolo Bonzinid399f672009-07-27 23:17:51 +0200187int autostart;
Amit Shah8e848652010-07-27 15:49:19 +0530188int incoming_expected; /* Started with -incoming and waiting for incoming */
balrogf6503052008-02-17 11:42:19 +0000189static int rtc_utc = 1;
190static int rtc_date_offset = -1; /* -1 means no change */
Jan Kiszka68752042009-09-15 13:36:04 +0200191QEMUClock *rtc_clock;
Gerd Hoffmann64465292009-12-08 13:11:45 +0100192int vga_interface_type = VGA_NONE;
blueswir1dbed7e42008-10-01 19:38:09 +0000193static int full_screen = 0;
blueswir1634a21f2008-11-16 11:34:07 +0000194#ifdef CONFIG_SDL
blueswir1dbed7e42008-10-01 19:38:09 +0000195static int no_frame = 0;
blueswir1634a21f2008-11-16 11:34:07 +0000196#endif
ths667acca2006-12-11 02:08:05 +0000197int no_quit = 0;
bellard8d11df92004-08-24 21:13:40 +0000198CharDriverState *serial_hds[MAX_SERIAL_PORTS];
bellard6508fe52005-01-15 12:02:56 +0000199CharDriverState *parallel_hds[MAX_PARALLEL_PORTS];
aliguori9ede2fd2009-01-15 20:05:25 +0000200CharDriverState *virtcon_hds[MAX_VIRTIO_CONSOLES];
bellarda09db212005-04-30 16:10:35 +0000201int win2k_install_hack = 0;
aliguori73822ec2009-01-15 20:11:34 +0000202int rtc_td_hack = 0;
bellardbb36d472005-11-05 14:22:28 +0000203int usb_enabled = 0;
aurel321b530a62009-04-05 20:08:59 +0000204int singlestep = 0;
bellard6a00d602005-11-21 23:25:50 +0000205int smp_cpus = 1;
Jes Sorensen6be68d72009-07-23 17:03:42 +0200206int max_cpus = 0;
Andre Przywaradc6b1c02009-08-19 15:42:40 +0200207int smp_cores = 1;
208int smp_threads = 1;
Jes Sorensen821601e2011-03-16 13:33:36 +0100209#ifdef CONFIG_VNC
ths73fc9742006-12-22 02:09:07 +0000210const char *vnc_display;
Jes Sorensen821601e2011-03-16 13:33:36 +0100211#endif
bellard6515b202006-05-03 22:02:44 +0000212int acpi_enabled = 1;
aliguori16b29ae2008-12-17 23:28:44 +0000213int no_hpet = 0;
bellard52ca8d62006-06-14 16:03:05 +0000214int fd_bootchk = 1;
bellardd1beab82006-10-02 19:44:22 +0000215int no_reboot = 0;
aurel32b2f76162008-04-11 21:35:52 +0000216int no_shutdown = 0;
balrog9467cd42007-05-01 01:34:14 +0000217int cursor_hide = 1;
balroga171fe32007-04-30 01:48:07 +0000218int graphic_rotate = 0;
Jes Sorensen6b35e7b2009-08-06 16:25:50 +0200219uint8_t irq0override = 1;
Markus Armbruster09aaa162009-08-21 10:31:34 +0200220const char *watchdog;
Gleb Natapov2e55e842010-12-08 13:35:07 +0200221QEMUOptionRom option_rom[MAX_OPTION_ROMS];
ths9ae02552007-01-05 17:39:04 +0000222int nb_option_roms;
pbrook8e716212007-01-20 17:12:09 +0000223int semihosting_enabled = 0;
balrog2b8f2d42007-07-27 22:08:46 +0000224int old_param = 0;
thsc35734b2007-03-19 15:17:08 +0000225const char *qemu_name;
ths3780e192007-06-21 21:08:02 +0000226int alt_grab = 0;
Dustin Kirkland0ca9f8a2009-09-17 15:48:04 -0500227int ctrl_grab = 0;
blueswir166508602007-05-01 14:16:52 +0000228unsigned int nb_prom_envs = 0;
229const char *prom_envs[MAX_PROM_ENVS];
Jan Kiszka95387492009-07-02 00:19:02 +0200230int boot_menu;
wayne3d3b8302011-07-27 18:04:55 +0800231uint8_t *boot_splash_filedata;
232int boot_splash_filedata_size;
233uint8_t qemu_extra_params_fw[2];
bellard0824d6f2003-06-24 13:42:40 +0000234
Gleb Natapov1ca4d092010-12-08 13:35:05 +0200235typedef struct FWBootEntry FWBootEntry;
236
237struct FWBootEntry {
238 QTAILQ_ENTRY(FWBootEntry) link;
239 int32_t bootindex;
240 DeviceState *dev;
241 char *suffix;
242};
243
244QTAILQ_HEAD(, FWBootEntry) fw_boot_order = QTAILQ_HEAD_INITIALIZER(fw_boot_order);
245
aliguori268a3622009-04-21 22:30:27 +0000246int nb_numa_nodes;
247uint64_t node_mem[MAX_NODES];
248uint64_t node_cpumask[MAX_NODES];
249
blueswir19043b622009-01-21 19:28:13 +0000250static QEMUTimer *nographic_timer;
balrogee5605e2007-12-03 03:01:40 +0000251
blueswir18fcb1b92008-09-18 18:29:08 +0000252uint8_t qemu_uuid[16];
253
Jan Kiszka76e30d02009-07-02 00:19:02 +0200254static QEMUBootSetHandler *boot_set_handler;
255static void *boot_set_opaque;
256
Gerd Hoffmannfd42dee2010-06-04 14:08:07 +0200257static NotifierList exit_notifiers =
258 NOTIFIER_LIST_INITIALIZER(exit_notifiers);
259
Gleb Natapov4cab9462010-12-08 13:35:08 +0200260static NotifierList machine_init_done_notifiers =
261 NOTIFIER_LIST_INITIALIZER(machine_init_done_notifiers);
262
Anthony PERARD303d4e82010-09-21 20:05:31 +0100263static int tcg_allowed = 1;
Blue Swirld745bef2010-03-29 19:23:49 +0000264int kvm_allowed = 0;
Anthony PERARD3285cf42010-08-19 12:27:56 +0100265int xen_allowed = 0;
Blue Swirld745bef2010-03-29 19:23:49 +0000266uint32_t xen_domid;
267enum xen_mode xen_mode = XEN_EMULATE;
Jan Kiszkad5ab9712011-08-02 16:10:21 +0200268static int tcg_tb_size;
Blue Swirld745bef2010-03-29 19:23:49 +0000269
Gerd Hoffmann998bbd72009-12-08 13:11:41 +0100270static int default_serial = 1;
Gerd Hoffmann6a5e8b02009-12-08 13:11:42 +0100271static int default_parallel = 1;
Gerd Hoffmann986c5f72009-12-08 13:11:54 +0100272static int default_virtcon = 1;
Gerd Hoffmannabdeed02009-12-08 13:11:43 +0100273static int default_monitor = 1;
Gerd Hoffmann64465292009-12-08 13:11:45 +0100274static int default_vga = 1;
Gerd Hoffmannac33f8f2009-12-16 14:25:39 +0100275static int default_floppy = 1;
276static int default_cdrom = 1;
277static int default_sdcard = 1;
Gerd Hoffmann998bbd72009-12-08 13:11:41 +0100278
279static struct {
280 const char *driver;
281 int *flag;
282} default_list[] = {
Gerd Hoffmann6a5e8b02009-12-08 13:11:42 +0100283 { .driver = "isa-serial", .flag = &default_serial },
284 { .driver = "isa-parallel", .flag = &default_parallel },
Gerd Hoffmannd8bcbab2009-12-16 14:25:40 +0100285 { .driver = "isa-fdc", .flag = &default_floppy },
Markus Armbrusteraf6bf132011-05-18 18:31:02 +0200286 { .driver = "ide-cd", .flag = &default_cdrom },
287 { .driver = "ide-hd", .flag = &default_cdrom },
Gerd Hoffmannd8bcbab2009-12-16 14:25:40 +0100288 { .driver = "ide-drive", .flag = &default_cdrom },
Markus Armbrusteraf6bf132011-05-18 18:31:02 +0200289 { .driver = "scsi-cd", .flag = &default_cdrom },
Amit Shah392ecf52010-01-21 16:19:23 +0530290 { .driver = "virtio-serial-pci", .flag = &default_virtcon },
291 { .driver = "virtio-serial-s390", .flag = &default_virtcon },
292 { .driver = "virtio-serial", .flag = &default_virtcon },
Gerd Hoffmann64465292009-12-08 13:11:45 +0100293 { .driver = "VGA", .flag = &default_vga },
Gerd Hoffmann69fd02e2009-12-16 13:35:19 +0100294 { .driver = "cirrus-vga", .flag = &default_vga },
295 { .driver = "vmware-svga", .flag = &default_vga },
Markus Armbruster0826c712011-06-07 10:34:30 +0200296 { .driver = "isa-vga", .flag = &default_vga },
Gerd Hoffmann42138042011-05-16 09:28:58 +0200297 { .driver = "qxl-vga", .flag = &default_vga },
Gerd Hoffmann998bbd72009-12-08 13:11:41 +0100298};
299
wayne3d3b8302011-07-27 18:04:55 +0800300static void res_free(void)
301{
302 if (boot_splash_filedata != NULL) {
Anthony Liguori7267c092011-08-20 22:09:37 -0500303 g_free(boot_splash_filedata);
wayne3d3b8302011-07-27 18:04:55 +0800304 boot_splash_filedata = NULL;
305 }
306}
307
Gerd Hoffmann998bbd72009-12-08 13:11:41 +0100308static int default_driver_check(QemuOpts *opts, void *opaque)
309{
310 const char *driver = qemu_opt_get(opts, "driver");
311 int i;
312
313 if (!driver)
314 return 0;
315 for (i = 0; i < ARRAY_SIZE(default_list); i++) {
316 if (strcmp(default_list[i].driver, driver) != 0)
317 continue;
318 *(default_list[i].flag) = 0;
319 }
320 return 0;
321}
322
bellard0824d6f2003-06-24 13:42:40 +0000323/***********************************************************/
Paolo Bonzini8f0056b2010-01-13 14:05:34 +0100324/* real time host monotonic timer */
bellard09b26c52006-04-12 21:09:08 +0000325
bellardc4b1fcc2004-03-14 21:44:30 +0000326/***********************************************************/
balrogf6503052008-02-17 11:42:19 +0000327/* host time/date access */
328void qemu_get_timedate(struct tm *tm, int offset)
329{
330 time_t ti;
331 struct tm *ret;
332
333 time(&ti);
334 ti += offset;
335 if (rtc_date_offset == -1) {
336 if (rtc_utc)
337 ret = gmtime(&ti);
338 else
339 ret = localtime(&ti);
340 } else {
341 ti -= rtc_date_offset;
342 ret = gmtime(&ti);
343 }
344
345 memcpy(tm, ret, sizeof(struct tm));
346}
347
348int qemu_timedate_diff(struct tm *tm)
349{
350 time_t seconds;
351
352 if (rtc_date_offset == -1)
353 if (rtc_utc)
354 seconds = mktimegm(tm);
355 else
356 seconds = mktime(tm);
357 else
358 seconds = mktimegm(tm) + rtc_date_offset;
359
360 return seconds - time(NULL);
361}
362
Luiz Capitulino80cd3472010-02-25 12:11:44 -0300363void rtc_change_mon_event(struct tm *tm)
364{
365 QObject *data;
366
367 data = qobject_from_jsonf("{ 'offset': %d }", qemu_timedate_diff(tm));
368 monitor_protocol_event(QEVENT_RTC_CHANGE, data);
369 qobject_decref(data);
370}
371
Jan Kiszka1ed2fc12009-09-15 13:36:04 +0200372static void configure_rtc_date_offset(const char *startdate, int legacy)
373{
374 time_t rtc_start_date;
375 struct tm tm;
376
377 if (!strcmp(startdate, "now") && legacy) {
378 rtc_date_offset = -1;
379 } else {
380 if (sscanf(startdate, "%d-%d-%dT%d:%d:%d",
381 &tm.tm_year,
382 &tm.tm_mon,
383 &tm.tm_mday,
384 &tm.tm_hour,
385 &tm.tm_min,
386 &tm.tm_sec) == 6) {
387 /* OK */
388 } else if (sscanf(startdate, "%d-%d-%d",
389 &tm.tm_year,
390 &tm.tm_mon,
391 &tm.tm_mday) == 3) {
392 tm.tm_hour = 0;
393 tm.tm_min = 0;
394 tm.tm_sec = 0;
395 } else {
396 goto date_fail;
397 }
398 tm.tm_year -= 1900;
399 tm.tm_mon--;
400 rtc_start_date = mktimegm(&tm);
401 if (rtc_start_date == -1) {
402 date_fail:
403 fprintf(stderr, "Invalid date format. Valid formats are:\n"
404 "'2006-06-17T16:01:21' or '2006-06-17'\n");
405 exit(1);
406 }
407 rtc_date_offset = time(NULL) - rtc_start_date;
408 }
409}
410
411static void configure_rtc(QemuOpts *opts)
412{
413 const char *value;
414
415 value = qemu_opt_get(opts, "base");
416 if (value) {
417 if (!strcmp(value, "utc")) {
418 rtc_utc = 1;
419 } else if (!strcmp(value, "localtime")) {
420 rtc_utc = 0;
421 } else {
422 configure_rtc_date_offset(value, 0);
423 }
424 }
Jan Kiszka68752042009-09-15 13:36:04 +0200425 value = qemu_opt_get(opts, "clock");
426 if (value) {
427 if (!strcmp(value, "host")) {
428 rtc_clock = host_clock;
429 } else if (!strcmp(value, "vm")) {
430 rtc_clock = vm_clock;
431 } else {
432 fprintf(stderr, "qemu: invalid option value '%s'\n", value);
433 exit(1);
434 }
435 }
Jan Kiszka1ed2fc12009-09-15 13:36:04 +0200436 value = qemu_opt_get(opts, "driftfix");
437 if (value) {
Blue Swirl7e4c0332010-03-27 21:33:46 +0000438 if (!strcmp(value, "slew")) {
Jan Kiszka1ed2fc12009-09-15 13:36:04 +0200439 rtc_td_hack = 1;
Blue Swirl7e4c0332010-03-27 21:33:46 +0000440 } else if (!strcmp(value, "none")) {
Jan Kiszka1ed2fc12009-09-15 13:36:04 +0200441 rtc_td_hack = 0;
442 } else {
443 fprintf(stderr, "qemu: invalid option value '%s'\n", value);
444 exit(1);
445 }
446 }
Jan Kiszka1ed2fc12009-09-15 13:36:04 +0200447}
448
balrog1ae26a12008-09-28 23:19:47 +0000449/***********************************************************/
450/* Bluetooth support */
451static int nb_hcis;
452static int cur_hci;
453static struct HCIInfo *hci_table[MAX_NICS];
balrogdc72ac12008-11-09 00:04:26 +0000454
balrog1ae26a12008-09-28 23:19:47 +0000455static struct bt_vlan_s {
456 struct bt_scatternet_s net;
457 int id;
458 struct bt_vlan_s *next;
459} *first_bt_vlan;
460
461/* find or alloc a new bluetooth "VLAN" */
blueswir1674bb262008-09-30 18:18:27 +0000462static struct bt_scatternet_s *qemu_find_bt_vlan(int id)
balrog1ae26a12008-09-28 23:19:47 +0000463{
464 struct bt_vlan_s **pvlan, *vlan;
465 for (vlan = first_bt_vlan; vlan != NULL; vlan = vlan->next) {
466 if (vlan->id == id)
467 return &vlan->net;
468 }
Anthony Liguori7267c092011-08-20 22:09:37 -0500469 vlan = g_malloc0(sizeof(struct bt_vlan_s));
balrog1ae26a12008-09-28 23:19:47 +0000470 vlan->id = id;
471 pvlan = &first_bt_vlan;
472 while (*pvlan != NULL)
473 pvlan = &(*pvlan)->next;
474 *pvlan = vlan;
475 return &vlan->net;
476}
477
478static void null_hci_send(struct HCIInfo *hci, const uint8_t *data, int len)
479{
480}
481
482static int null_hci_addr_set(struct HCIInfo *hci, const uint8_t *bd_addr)
483{
484 return -ENOTSUP;
485}
486
487static struct HCIInfo null_hci = {
488 .cmd_send = null_hci_send,
489 .sco_send = null_hci_send,
490 .acl_send = null_hci_send,
491 .bdaddr_set = null_hci_addr_set,
492};
493
494struct HCIInfo *qemu_next_hci(void)
495{
496 if (cur_hci == nb_hcis)
497 return &null_hci;
498
499 return hci_table[cur_hci++];
500}
501
balrogdc72ac12008-11-09 00:04:26 +0000502static struct HCIInfo *hci_init(const char *str)
503{
504 char *endp;
505 struct bt_scatternet_s *vlan = 0;
506
507 if (!strcmp(str, "null"))
508 /* null */
509 return &null_hci;
510 else if (!strncmp(str, "host", 4) && (str[4] == '\0' || str[4] == ':'))
511 /* host[:hciN] */
512 return bt_host_hci(str[4] ? str + 5 : "hci0");
513 else if (!strncmp(str, "hci", 3)) {
514 /* hci[,vlan=n] */
515 if (str[3]) {
516 if (!strncmp(str + 3, ",vlan=", 6)) {
517 vlan = qemu_find_bt_vlan(strtol(str + 9, &endp, 0));
518 if (*endp)
519 vlan = 0;
520 }
521 } else
522 vlan = qemu_find_bt_vlan(0);
523 if (vlan)
524 return bt_new_hci(vlan);
525 }
526
527 fprintf(stderr, "qemu: Unknown bluetooth HCI `%s'.\n", str);
528
529 return 0;
530}
531
532static int bt_hci_parse(const char *str)
533{
534 struct HCIInfo *hci;
Anthony Liguoric227f092009-10-01 16:12:16 -0500535 bdaddr_t bdaddr;
balrogdc72ac12008-11-09 00:04:26 +0000536
537 if (nb_hcis >= MAX_NICS) {
538 fprintf(stderr, "qemu: Too many bluetooth HCIs (max %i).\n", MAX_NICS);
539 return -1;
540 }
541
542 hci = hci_init(str);
543 if (!hci)
544 return -1;
545
546 bdaddr.b[0] = 0x52;
547 bdaddr.b[1] = 0x54;
548 bdaddr.b[2] = 0x00;
549 bdaddr.b[3] = 0x12;
550 bdaddr.b[4] = 0x34;
551 bdaddr.b[5] = 0x56 + nb_hcis;
552 hci->bdaddr_set(hci, bdaddr.b);
553
554 hci_table[nb_hcis++] = hci;
555
556 return 0;
557}
558
559static void bt_vhci_add(int vlan_id)
560{
561 struct bt_scatternet_s *vlan = qemu_find_bt_vlan(vlan_id);
562
563 if (!vlan->slave)
564 fprintf(stderr, "qemu: warning: adding a VHCI to "
565 "an empty scatternet %i\n", vlan_id);
566
567 bt_vhci_init(bt_new_hci(vlan));
568}
569
570static struct bt_device_s *bt_device_add(const char *opt)
571{
572 struct bt_scatternet_s *vlan;
573 int vlan_id = 0;
574 char *endp = strstr(opt, ",vlan=");
575 int len = (endp ? endp - opt : strlen(opt)) + 1;
576 char devname[10];
577
578 pstrcpy(devname, MIN(sizeof(devname), len), opt);
579
580 if (endp) {
581 vlan_id = strtol(endp + 6, &endp, 0);
582 if (*endp) {
583 fprintf(stderr, "qemu: unrecognised bluetooth vlan Id\n");
584 return 0;
585 }
586 }
587
588 vlan = qemu_find_bt_vlan(vlan_id);
589
590 if (!vlan->slave)
591 fprintf(stderr, "qemu: warning: adding a slave device to "
592 "an empty scatternet %i\n", vlan_id);
593
594 if (!strcmp(devname, "keyboard"))
595 return bt_keyboard_init(vlan);
596
597 fprintf(stderr, "qemu: unsupported bluetooth device `%s'\n", devname);
598 return 0;
599}
600
601static int bt_parse(const char *opt)
602{
603 const char *endp, *p;
604 int vlan;
605
606 if (strstart(opt, "hci", &endp)) {
607 if (!*endp || *endp == ',') {
608 if (*endp)
609 if (!strstart(endp, ",vlan=", 0))
610 opt = endp + 1;
611
612 return bt_hci_parse(opt);
613 }
614 } else if (strstart(opt, "vhci", &endp)) {
615 if (!*endp || *endp == ',') {
616 if (*endp) {
617 if (strstart(endp, ",vlan=", &p)) {
618 vlan = strtol(p, (char **) &endp, 0);
619 if (*endp) {
620 fprintf(stderr, "qemu: bad scatternet '%s'\n", p);
621 return 1;
622 }
623 } else {
624 fprintf(stderr, "qemu: bad parameter '%s'\n", endp + 1);
625 return 1;
626 }
627 } else
628 vlan = 0;
629
630 bt_vhci_add(vlan);
631 return 0;
632 }
633 } else if (strstart(opt, "device:", &endp))
634 return !bt_device_add(endp);
635
636 fprintf(stderr, "qemu: bad bluetooth parameter '%s'\n", opt);
637 return 1;
638}
639
balrog1ae26a12008-09-28 23:19:47 +0000640/***********************************************************/
641/* QEMU Block devices */
642
Markus Armbruster2292dda2011-01-28 11:21:41 +0100643#define HD_OPTS "media=disk"
644#define CDROM_OPTS "media=cdrom"
645#define FD_OPTS ""
646#define PFLASH_OPTS ""
647#define MTD_OPTS ""
648#define SD_OPTS ""
thse4bcb142007-12-02 04:51:10 +0000649
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +0200650static int drive_init_func(QemuOpts *opts, void *opaque)
651{
Markus Armbrustera803cb82010-06-02 13:31:55 +0200652 int *use_scsi = opaque;
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +0200653
Markus Armbruster319ae522011-01-28 11:21:46 +0100654 return drive_init(opts, *use_scsi) == NULL;
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +0200655}
656
657static int drive_enable_snapshot(QemuOpts *opts, void *opaque)
658{
659 if (NULL == qemu_opt_get(opts, "snapshot")) {
660 qemu_opt_set(opts, "snapshot", "on");
661 }
662 return 0;
663}
664
Markus Armbruster4e5d9b52011-01-28 11:21:45 +0100665static void default_drive(int enable, int snapshot, int use_scsi,
666 BlockInterfaceType type, int index,
667 const char *optstr)
668{
669 QemuOpts *opts;
670
671 if (type == IF_DEFAULT) {
672 type = use_scsi ? IF_SCSI : IF_IDE;
673 }
674
675 if (!enable || drive_get_by_index(type, index)) {
676 return;
677 }
678
679 opts = drive_add(type, index, NULL, optstr);
680 if (snapshot) {
681 drive_enable_snapshot(opts, NULL);
682 }
Markus Armbruster319ae522011-01-28 11:21:46 +0100683 if (!drive_init(opts, use_scsi)) {
Markus Armbruster4e5d9b52011-01-28 11:21:45 +0100684 exit(1);
685 }
686}
687
Jan Kiszka76e30d02009-07-02 00:19:02 +0200688void qemu_register_boot_set(QEMUBootSetHandler *func, void *opaque)
689{
690 boot_set_handler = func;
691 boot_set_opaque = opaque;
692}
693
694int qemu_boot_set(const char *boot_devices)
695{
696 if (!boot_set_handler) {
697 return -EINVAL;
698 }
699 return boot_set_handler(boot_set_opaque, boot_devices);
700}
701
Eduardo Habkost4e9e9d62010-04-06 19:22:08 -0300702static void validate_bootdevices(char *devices)
Jan Kiszkaef3adf62009-07-02 00:19:02 +0200703{
704 /* We just do some generic consistency checks */
705 const char *p;
706 int bitmap = 0;
707
708 for (p = devices; *p != '\0'; p++) {
709 /* Allowed boot devices are:
710 * a-b: floppy disk drives
711 * c-f: IDE disk drives
712 * g-m: machine implementation dependant drives
713 * n-p: network devices
714 * It's up to each machine implementation to check if the given boot
715 * devices match the actual hardware implementation and firmware
716 * features.
717 */
718 if (*p < 'a' || *p > 'p') {
719 fprintf(stderr, "Invalid boot device '%c'\n", *p);
720 exit(1);
721 }
722 if (bitmap & (1 << (*p - 'a'))) {
723 fprintf(stderr, "Boot device '%c' was given twice\n", *p);
724 exit(1);
725 }
726 bitmap |= 1 << (*p - 'a');
727 }
Jan Kiszkaef3adf62009-07-02 00:19:02 +0200728}
729
Jan Kiszkae0f084b2009-07-02 00:19:02 +0200730static void restore_boot_devices(void *opaque)
731{
732 char *standard_boot_devices = opaque;
Alex Williamson37905d62010-04-30 15:21:11 -0600733 static int first = 1;
734
735 /* Restore boot order and remove ourselves after the first boot */
736 if (first) {
737 first = 0;
738 return;
739 }
Jan Kiszkae0f084b2009-07-02 00:19:02 +0200740
741 qemu_boot_set(standard_boot_devices);
742
743 qemu_unregister_reset(restore_boot_devices, standard_boot_devices);
Anthony Liguori7267c092011-08-20 22:09:37 -0500744 g_free(standard_boot_devices);
Jan Kiszkae0f084b2009-07-02 00:19:02 +0200745}
746
Gleb Natapov1ca4d092010-12-08 13:35:05 +0200747void add_boot_device_path(int32_t bootindex, DeviceState *dev,
748 const char *suffix)
749{
750 FWBootEntry *node, *i;
751
752 if (bootindex < 0) {
753 return;
754 }
755
756 assert(dev != NULL || suffix != NULL);
757
Anthony Liguori7267c092011-08-20 22:09:37 -0500758 node = g_malloc0(sizeof(FWBootEntry));
Gleb Natapov1ca4d092010-12-08 13:35:05 +0200759 node->bootindex = bootindex;
Anthony Liguori7267c092011-08-20 22:09:37 -0500760 node->suffix = suffix ? g_strdup(suffix) : NULL;
Gleb Natapov1ca4d092010-12-08 13:35:05 +0200761 node->dev = dev;
762
763 QTAILQ_FOREACH(i, &fw_boot_order, link) {
764 if (i->bootindex == bootindex) {
765 fprintf(stderr, "Two devices with same boot index %d\n", bootindex);
766 exit(1);
767 } else if (i->bootindex < bootindex) {
768 continue;
769 }
770 QTAILQ_INSERT_BEFORE(i, node, link);
771 return;
772 }
773 QTAILQ_INSERT_TAIL(&fw_boot_order, node, link);
774}
775
Gleb Natapov962630f2010-12-08 13:35:09 +0200776/*
777 * This function returns null terminated string that consist of new line
Brad Hards71785ab2011-04-23 21:50:06 +1000778 * separated device paths.
Gleb Natapov962630f2010-12-08 13:35:09 +0200779 *
780 * memory pointed by "size" is assigned total length of the array in bytes
781 *
782 */
783char *get_boot_devices_list(uint32_t *size)
784{
785 FWBootEntry *i;
786 uint32_t total = 0;
787 char *list = NULL;
788
789 QTAILQ_FOREACH(i, &fw_boot_order, link) {
790 char *devpath = NULL, *bootpath;
791 int len;
792
793 if (i->dev) {
794 devpath = qdev_get_fw_dev_path(i->dev);
795 assert(devpath);
796 }
797
798 if (i->suffix && devpath) {
Blue Swirl4fd37a92010-12-19 14:05:43 +0000799 size_t bootpathlen = strlen(devpath) + strlen(i->suffix) + 1;
800
Anthony Liguori7267c092011-08-20 22:09:37 -0500801 bootpath = g_malloc(bootpathlen);
Blue Swirl4fd37a92010-12-19 14:05:43 +0000802 snprintf(bootpath, bootpathlen, "%s%s", devpath, i->suffix);
Anthony Liguori7267c092011-08-20 22:09:37 -0500803 g_free(devpath);
Gleb Natapov962630f2010-12-08 13:35:09 +0200804 } else if (devpath) {
805 bootpath = devpath;
806 } else {
Anthony Liguori7267c092011-08-20 22:09:37 -0500807 bootpath = g_strdup(i->suffix);
Gleb Natapov962630f2010-12-08 13:35:09 +0200808 assert(bootpath);
809 }
810
811 if (total) {
812 list[total-1] = '\n';
813 }
814 len = strlen(bootpath) + 1;
Anthony Liguori7267c092011-08-20 22:09:37 -0500815 list = g_realloc(list, total + len);
Gleb Natapov962630f2010-12-08 13:35:09 +0200816 memcpy(&list[total], bootpath, len);
817 total += len;
Anthony Liguori7267c092011-08-20 22:09:37 -0500818 g_free(bootpath);
Gleb Natapov962630f2010-12-08 13:35:09 +0200819 }
820
821 *size = total;
822
823 return list;
824}
825
aliguori268a3622009-04-21 22:30:27 +0000826static void numa_add(const char *optarg)
827{
828 char option[128];
829 char *endptr;
830 unsigned long long value, endvalue;
831 int nodenr;
832
833 optarg = get_opt_name(option, 128, optarg, ',') + 1;
834 if (!strcmp(option, "node")) {
835 if (get_param_value(option, 128, "nodeid", optarg) == 0) {
836 nodenr = nb_numa_nodes;
837 } else {
838 nodenr = strtoull(option, NULL, 10);
839 }
840
841 if (get_param_value(option, 128, "mem", optarg) == 0) {
842 node_mem[nodenr] = 0;
843 } else {
Jes Sorensen70b4f4b2011-01-05 11:41:02 +0100844 int64_t sval;
Jes Sorensen9f9b17a2010-10-21 17:15:46 +0200845 sval = strtosz(option, NULL);
846 if (sval < 0) {
847 fprintf(stderr, "qemu: invalid numa mem size: %s\n", optarg);
848 exit(1);
aliguori268a3622009-04-21 22:30:27 +0000849 }
Jes Sorensen9f9b17a2010-10-21 17:15:46 +0200850 node_mem[nodenr] = sval;
aliguori268a3622009-04-21 22:30:27 +0000851 }
852 if (get_param_value(option, 128, "cpus", optarg) == 0) {
853 node_cpumask[nodenr] = 0;
854 } else {
855 value = strtoull(option, &endptr, 10);
856 if (value >= 64) {
857 value = 63;
858 fprintf(stderr, "only 64 CPUs in NUMA mode supported.\n");
859 } else {
860 if (*endptr == '-') {
861 endvalue = strtoull(endptr+1, &endptr, 10);
862 if (endvalue >= 63) {
863 endvalue = 62;
864 fprintf(stderr,
865 "only 63 CPUs in NUMA mode supported.\n");
866 }
Paolo Bonzini0dfbd512010-02-04 14:31:50 +0100867 value = (2ULL << endvalue) - (1ULL << value);
aliguori268a3622009-04-21 22:30:27 +0000868 } else {
Paolo Bonzini0dfbd512010-02-04 14:31:50 +0100869 value = 1ULL << value;
aliguori268a3622009-04-21 22:30:27 +0000870 }
871 }
872 node_cpumask[nodenr] = value;
873 }
874 nb_numa_nodes++;
875 }
876 return;
877}
878
Andre Przywaradc6b1c02009-08-19 15:42:40 +0200879static void smp_parse(const char *optarg)
880{
881 int smp, sockets = 0, threads = 0, cores = 0;
882 char *endptr;
883 char option[128];
884
885 smp = strtoul(optarg, &endptr, 10);
886 if (endptr != optarg) {
887 if (*endptr == ',') {
888 endptr++;
889 }
890 }
891 if (get_param_value(option, 128, "sockets", endptr) != 0)
892 sockets = strtoull(option, NULL, 10);
893 if (get_param_value(option, 128, "cores", endptr) != 0)
894 cores = strtoull(option, NULL, 10);
895 if (get_param_value(option, 128, "threads", endptr) != 0)
896 threads = strtoull(option, NULL, 10);
897 if (get_param_value(option, 128, "maxcpus", endptr) != 0)
898 max_cpus = strtoull(option, NULL, 10);
899
900 /* compute missing values, prefer sockets over cores over threads */
901 if (smp == 0 || sockets == 0) {
902 sockets = sockets > 0 ? sockets : 1;
903 cores = cores > 0 ? cores : 1;
904 threads = threads > 0 ? threads : 1;
905 if (smp == 0) {
906 smp = cores * threads * sockets;
Andre Przywaradc6b1c02009-08-19 15:42:40 +0200907 }
908 } else {
909 if (cores == 0) {
910 threads = threads > 0 ? threads : 1;
911 cores = smp / (sockets * threads);
912 } else {
Joel Schoppdca98162010-07-21 15:05:17 -0500913 threads = smp / (cores * sockets);
Andre Przywaradc6b1c02009-08-19 15:42:40 +0200914 }
915 }
916 smp_cpus = smp;
917 smp_cores = cores > 0 ? cores : 1;
918 smp_threads = threads > 0 ? threads : 1;
919 if (max_cpus == 0)
920 max_cpus = smp_cpus;
921}
922
bellard330d0412003-07-26 18:11:40 +0000923/***********************************************************/
bellarda594cfb2005-11-06 16:13:29 +0000924/* USB devices */
925
Markus Armbrusterfb080002010-05-28 15:38:44 +0200926static int usb_device_add(const char *devname)
bellarda594cfb2005-11-06 16:13:29 +0000927{
928 const char *p;
Gerd Hoffmanna5d2f722009-08-31 14:24:00 +0200929 USBDevice *dev = NULL;
bellarda594cfb2005-11-06 16:13:29 +0000930
Gerd Hoffmanna5d2f722009-08-31 14:24:00 +0200931 if (!usb_enabled)
bellarda594cfb2005-11-06 16:13:29 +0000932 return -1;
933
Gerd Hoffmann0958b4c2009-10-26 15:56:45 +0100934 /* drivers with .usbdevice_name entry in USBDeviceInfo */
935 dev = usbdevice_create(devname);
936 if (dev)
937 goto done;
938
Gerd Hoffmanna5d2f722009-08-31 14:24:00 +0200939 /* the other ones */
Gerd Hoffmanne447fc62011-06-01 14:41:59 +0200940#ifndef CONFIG_LINUX
941 /* only the linux version is qdev-ified, usb-bsd still needs this */
bellarda594cfb2005-11-06 16:13:29 +0000942 if (strstart(devname, "host:", &p)) {
943 dev = usb_host_device_open(p);
Gerd Hoffmanne447fc62011-06-01 14:41:59 +0200944 } else
945#endif
946 if (!strcmp(devname, "bt") || strstart(devname, "bt:", &p)) {
balrogdc72ac12008-11-09 00:04:26 +0000947 dev = usb_bt_init(devname[2] ? hci_init(p) :
948 bt_new_hci(qemu_find_bt_vlan(0)));
bellarda594cfb2005-11-06 16:13:29 +0000949 } else {
950 return -1;
951 }
pbrook0d92ed32006-05-21 16:30:15 +0000952 if (!dev)
953 return -1;
954
Gerd Hoffmanna5d2f722009-08-31 14:24:00 +0200955done:
bellarda594cfb2005-11-06 16:13:29 +0000956 return 0;
957}
958
aliguori1f3870a2008-08-21 19:27:48 +0000959static int usb_device_del(const char *devname)
960{
961 int bus_num, addr;
962 const char *p;
963
aliguori5d0c5752008-09-14 01:07:41 +0000964 if (strstart(devname, "host:", &p))
965 return usb_host_device_close(p);
966
Gerd Hoffmanna5d2f722009-08-31 14:24:00 +0200967 if (!usb_enabled)
aliguori1f3870a2008-08-21 19:27:48 +0000968 return -1;
969
970 p = strchr(devname, '.');
971 if (!p)
972 return -1;
973 bus_num = strtoul(devname, NULL, 0);
974 addr = strtoul(p + 1, NULL, 0);
975
Gerd Hoffmanna5d2f722009-08-31 14:24:00 +0200976 return usb_device_delete_addr(bus_num, addr);
aliguori1f3870a2008-08-21 19:27:48 +0000977}
978
Gerd Hoffmannbd3c9482009-07-15 13:59:26 +0200979static int usb_parse(const char *cmdline)
980{
Scott Tsai59d1c1c2009-12-23 04:30:18 +0800981 int r;
Markus Armbrusterfb080002010-05-28 15:38:44 +0200982 r = usb_device_add(cmdline);
Scott Tsai59d1c1c2009-12-23 04:30:18 +0800983 if (r < 0) {
984 fprintf(stderr, "qemu: could not add USB device '%s'\n", cmdline);
985 }
986 return r;
Gerd Hoffmannbd3c9482009-07-15 13:59:26 +0200987}
988
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300989void do_usb_add(Monitor *mon, const QDict *qdict)
bellarda594cfb2005-11-06 16:13:29 +0000990{
Scott Tsai59d1c1c2009-12-23 04:30:18 +0800991 const char *devname = qdict_get_str(qdict, "devname");
Markus Armbrusterfb080002010-05-28 15:38:44 +0200992 if (usb_device_add(devname) < 0) {
Markus Armbruster1ecda022010-02-18 17:25:24 +0100993 error_report("could not add USB device '%s'", devname);
Scott Tsai59d1c1c2009-12-23 04:30:18 +0800994 }
bellarda594cfb2005-11-06 16:13:29 +0000995}
996
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300997void do_usb_del(Monitor *mon, const QDict *qdict)
bellarda594cfb2005-11-06 16:13:29 +0000998{
Scott Tsai59d1c1c2009-12-23 04:30:18 +0800999 const char *devname = qdict_get_str(qdict, "devname");
1000 if (usb_device_del(devname) < 0) {
Markus Armbruster1ecda022010-02-18 17:25:24 +01001001 error_report("could not delete USB device '%s'", devname);
Scott Tsai59d1c1c2009-12-23 04:30:18 +08001002 }
bellarda594cfb2005-11-06 16:13:29 +00001003}
1004
bellardf7cce892004-12-08 22:21:25 +00001005/***********************************************************/
balrog201a51f2007-04-30 00:51:09 +00001006/* PCMCIA/Cardbus */
1007
1008static struct pcmcia_socket_entry_s {
Paul Brookbc24a222009-05-10 01:44:56 +01001009 PCMCIASocket *socket;
balrog201a51f2007-04-30 00:51:09 +00001010 struct pcmcia_socket_entry_s *next;
1011} *pcmcia_sockets = 0;
1012
Paul Brookbc24a222009-05-10 01:44:56 +01001013void pcmcia_socket_register(PCMCIASocket *socket)
balrog201a51f2007-04-30 00:51:09 +00001014{
1015 struct pcmcia_socket_entry_s *entry;
1016
Anthony Liguori7267c092011-08-20 22:09:37 -05001017 entry = g_malloc(sizeof(struct pcmcia_socket_entry_s));
balrog201a51f2007-04-30 00:51:09 +00001018 entry->socket = socket;
1019 entry->next = pcmcia_sockets;
1020 pcmcia_sockets = entry;
1021}
1022
Paul Brookbc24a222009-05-10 01:44:56 +01001023void pcmcia_socket_unregister(PCMCIASocket *socket)
balrog201a51f2007-04-30 00:51:09 +00001024{
1025 struct pcmcia_socket_entry_s *entry, **ptr;
1026
1027 ptr = &pcmcia_sockets;
1028 for (entry = *ptr; entry; ptr = &entry->next, entry = *ptr)
1029 if (entry->socket == socket) {
1030 *ptr = entry->next;
Anthony Liguori7267c092011-08-20 22:09:37 -05001031 g_free(entry);
balrog201a51f2007-04-30 00:51:09 +00001032 }
1033}
1034
aliguori376253e2009-03-05 23:01:23 +00001035void pcmcia_info(Monitor *mon)
balrog201a51f2007-04-30 00:51:09 +00001036{
1037 struct pcmcia_socket_entry_s *iter;
aliguori376253e2009-03-05 23:01:23 +00001038
balrog201a51f2007-04-30 00:51:09 +00001039 if (!pcmcia_sockets)
aliguori376253e2009-03-05 23:01:23 +00001040 monitor_printf(mon, "No PCMCIA sockets\n");
balrog201a51f2007-04-30 00:51:09 +00001041
1042 for (iter = pcmcia_sockets; iter; iter = iter->next)
aliguori376253e2009-03-05 23:01:23 +00001043 monitor_printf(mon, "%s: %s\n", iter->socket->slot_string,
1044 iter->socket->attached ? iter->socket->card_string :
1045 "Empty");
balrog201a51f2007-04-30 00:51:09 +00001046}
1047
1048/***********************************************************/
bellardcc1daa42005-06-05 14:49:17 +00001049/* machine registration */
1050
blueswir1bdaf78e2008-10-04 07:24:27 +00001051static QEMUMachine *first_machine = NULL;
aliguori6f338c32009-02-11 15:21:54 +00001052QEMUMachine *current_machine = NULL;
bellardcc1daa42005-06-05 14:49:17 +00001053
1054int qemu_register_machine(QEMUMachine *m)
1055{
1056 QEMUMachine **pm;
1057 pm = &first_machine;
1058 while (*pm != NULL)
1059 pm = &(*pm)->next;
1060 m->next = NULL;
1061 *pm = m;
1062 return 0;
1063}
1064
pbrook9596ebb2007-11-18 01:44:38 +00001065static QEMUMachine *find_machine(const char *name)
bellardcc1daa42005-06-05 14:49:17 +00001066{
1067 QEMUMachine *m;
1068
1069 for(m = first_machine; m != NULL; m = m->next) {
1070 if (!strcmp(m->name, name))
1071 return m;
Mark McLoughlin3f6599e2009-07-22 10:02:50 +01001072 if (m->alias && !strcmp(m->alias, name))
1073 return m;
bellardcc1daa42005-06-05 14:49:17 +00001074 }
1075 return NULL;
1076}
1077
Anthony Liguori0c257432009-05-21 20:41:01 -05001078static QEMUMachine *find_default_machine(void)
1079{
1080 QEMUMachine *m;
1081
1082 for(m = first_machine; m != NULL; m = m->next) {
1083 if (m->is_default) {
1084 return m;
1085 }
1086 }
1087 return NULL;
1088}
1089
bellardcc1daa42005-06-05 14:49:17 +00001090/***********************************************************/
bellard8a7ddc32004-03-31 19:00:16 +00001091/* main execution loop */
1092
pbrook9596ebb2007-11-18 01:44:38 +00001093static void gui_update(void *opaque)
bellard8a7ddc32004-03-31 19:00:16 +00001094{
aliguori7d957bd2009-01-15 22:14:11 +00001095 uint64_t interval = GUI_REFRESH_INTERVAL;
ths740733b2007-06-08 01:57:56 +00001096 DisplayState *ds = opaque;
aliguori7d957bd2009-01-15 22:14:11 +00001097 DisplayChangeListener *dcl = ds->listeners;
1098
Sheng Yang62a27442010-01-26 19:21:16 +08001099 qemu_flush_coalesced_mmio_buffer();
aliguori7d957bd2009-01-15 22:14:11 +00001100 dpy_refresh(ds);
1101
1102 while (dcl != NULL) {
1103 if (dcl->gui_timer_interval &&
1104 dcl->gui_timer_interval < interval)
1105 interval = dcl->gui_timer_interval;
1106 dcl = dcl->next;
1107 }
Paolo Bonzini7bd427d2011-03-11 16:47:48 +01001108 qemu_mod_timer(ds->gui_timer, interval + qemu_get_clock_ms(rt_clock));
bellard8a7ddc32004-03-31 19:00:16 +00001109}
1110
blueswir19043b622009-01-21 19:28:13 +00001111static void nographic_update(void *opaque)
1112{
1113 uint64_t interval = GUI_REFRESH_INTERVAL;
1114
Sheng Yang62a27442010-01-26 19:21:16 +08001115 qemu_flush_coalesced_mmio_buffer();
Paolo Bonzini7bd427d2011-03-11 16:47:48 +01001116 qemu_mod_timer(nographic_timer, interval + qemu_get_clock_ms(rt_clock));
blueswir19043b622009-01-21 19:28:13 +00001117}
1118
bellard0bd48852005-11-11 00:00:47 +00001119struct vm_change_state_entry {
1120 VMChangeStateHandler *cb;
1121 void *opaque;
Blue Swirl72cf2d42009-09-12 07:36:22 +00001122 QLIST_ENTRY (vm_change_state_entry) entries;
bellard0bd48852005-11-11 00:00:47 +00001123};
1124
Blue Swirl72cf2d42009-09-12 07:36:22 +00001125static QLIST_HEAD(vm_change_state_head, vm_change_state_entry) vm_change_state_head;
bellard0bd48852005-11-11 00:00:47 +00001126
1127VMChangeStateEntry *qemu_add_vm_change_state_handler(VMChangeStateHandler *cb,
1128 void *opaque)
1129{
1130 VMChangeStateEntry *e;
1131
Anthony Liguori7267c092011-08-20 22:09:37 -05001132 e = g_malloc0(sizeof (*e));
bellard0bd48852005-11-11 00:00:47 +00001133
1134 e->cb = cb;
1135 e->opaque = opaque;
Blue Swirl72cf2d42009-09-12 07:36:22 +00001136 QLIST_INSERT_HEAD(&vm_change_state_head, e, entries);
bellard0bd48852005-11-11 00:00:47 +00001137 return e;
1138}
1139
1140void qemu_del_vm_change_state_handler(VMChangeStateEntry *e)
1141{
Blue Swirl72cf2d42009-09-12 07:36:22 +00001142 QLIST_REMOVE (e, entries);
Anthony Liguori7267c092011-08-20 22:09:37 -05001143 g_free (e);
bellard0bd48852005-11-11 00:00:47 +00001144}
1145
Blue Swirl296af7c2010-03-29 19:23:50 +00001146void vm_state_notify(int running, int reason)
bellard0bd48852005-11-11 00:00:47 +00001147{
1148 VMChangeStateEntry *e;
1149
Stefan Hajnoczi94b0b5f2010-11-16 12:20:25 +00001150 trace_vm_state_notify(running, reason);
1151
bellard0bd48852005-11-11 00:00:47 +00001152 for (e = vm_change_state_head.lh_first; e; e = e->entries.le_next) {
aliguori9781e042009-01-22 17:15:29 +00001153 e->cb(e->opaque, running, reason);
bellard0bd48852005-11-11 00:00:47 +00001154 }
1155}
1156
bellard8a7ddc32004-03-31 19:00:16 +00001157void vm_start(void)
1158{
1159 if (!vm_running) {
1160 cpu_enable_ticks();
1161 vm_running = 1;
aliguori9781e042009-01-22 17:15:29 +00001162 vm_state_notify(1, 0);
aliguorid6dc3d42009-04-24 18:04:07 +00001163 resume_all_vcpus();
Luiz Capitulino6ed2c482010-04-27 20:35:59 -03001164 monitor_protocol_event(QEVENT_RESUME, NULL);
bellard8a7ddc32004-03-31 19:00:16 +00001165 }
1166}
1167
bellardbb0c6722004-06-20 12:37:32 +00001168/* reset/shutdown handler */
1169
1170typedef struct QEMUResetEntry {
Blue Swirl72cf2d42009-09-12 07:36:22 +00001171 QTAILQ_ENTRY(QEMUResetEntry) entry;
bellardbb0c6722004-06-20 12:37:32 +00001172 QEMUResetHandler *func;
1173 void *opaque;
bellardbb0c6722004-06-20 12:37:32 +00001174} QEMUResetEntry;
1175
Blue Swirl72cf2d42009-09-12 07:36:22 +00001176static QTAILQ_HEAD(reset_handlers, QEMUResetEntry) reset_handlers =
1177 QTAILQ_HEAD_INITIALIZER(reset_handlers);
bellardbb0c6722004-06-20 12:37:32 +00001178static int reset_requested;
Gleb Natapovf64622c2011-03-15 13:56:04 +02001179static int shutdown_requested, shutdown_signal = -1;
1180static pid_t shutdown_pid;
bellard34751872005-07-02 14:31:34 +00001181static int powerdown_requested;
Jan Kiszka8cf71712011-02-07 12:19:16 +01001182static int debug_requested;
1183static int vmstop_requested;
bellardbb0c6722004-06-20 12:37:32 +00001184
Anthony PERARD1291eb32010-07-22 15:52:48 +01001185int qemu_shutdown_requested_get(void)
1186{
1187 return shutdown_requested;
1188}
1189
1190int qemu_reset_requested_get(void)
1191{
1192 return reset_requested;
1193}
1194
aurel32cf7a2fe2008-03-18 06:53:05 +00001195int qemu_shutdown_requested(void)
1196{
1197 int r = shutdown_requested;
1198 shutdown_requested = 0;
1199 return r;
1200}
1201
Gleb Natapovf64622c2011-03-15 13:56:04 +02001202void qemu_kill_report(void)
1203{
1204 if (shutdown_signal != -1) {
Peter Maydellf1d3fb02011-03-30 22:03:38 +01001205 fprintf(stderr, "qemu: terminating on signal %d", shutdown_signal);
1206 if (shutdown_pid == 0) {
1207 /* This happens for eg ^C at the terminal, so it's worth
1208 * avoiding printing an odd message in that case.
1209 */
1210 fputc('\n', stderr);
1211 } else {
Andreas Färber953ffe02011-06-02 19:58:06 +02001212 fprintf(stderr, " from pid " FMT_pid "\n", shutdown_pid);
Peter Maydellf1d3fb02011-03-30 22:03:38 +01001213 }
Gleb Natapovf64622c2011-03-15 13:56:04 +02001214 shutdown_signal = -1;
1215 }
1216}
1217
aurel32cf7a2fe2008-03-18 06:53:05 +00001218int qemu_reset_requested(void)
1219{
1220 int r = reset_requested;
1221 reset_requested = 0;
1222 return r;
1223}
1224
1225int qemu_powerdown_requested(void)
1226{
1227 int r = powerdown_requested;
1228 powerdown_requested = 0;
1229 return r;
1230}
1231
aliguorie5689022009-04-24 18:03:54 +00001232static int qemu_debug_requested(void)
1233{
1234 int r = debug_requested;
1235 debug_requested = 0;
1236 return r;
1237}
1238
aliguori6e29f5d2009-04-24 18:04:02 +00001239static int qemu_vmstop_requested(void)
1240{
1241 int r = vmstop_requested;
1242 vmstop_requested = 0;
1243 return r;
1244}
1245
Jan Kiszkaa08d4362009-06-27 09:25:07 +02001246void qemu_register_reset(QEMUResetHandler *func, void *opaque)
bellardbb0c6722004-06-20 12:37:32 +00001247{
Anthony Liguori7267c092011-08-20 22:09:37 -05001248 QEMUResetEntry *re = g_malloc0(sizeof(QEMUResetEntry));
bellardbb0c6722004-06-20 12:37:32 +00001249
bellardbb0c6722004-06-20 12:37:32 +00001250 re->func = func;
1251 re->opaque = opaque;
Blue Swirl72cf2d42009-09-12 07:36:22 +00001252 QTAILQ_INSERT_TAIL(&reset_handlers, re, entry);
bellardbb0c6722004-06-20 12:37:32 +00001253}
1254
Jan Kiszkadda9b292009-07-02 00:19:02 +02001255void qemu_unregister_reset(QEMUResetHandler *func, void *opaque)
bellardbb0c6722004-06-20 12:37:32 +00001256{
1257 QEMUResetEntry *re;
1258
Blue Swirl72cf2d42009-09-12 07:36:22 +00001259 QTAILQ_FOREACH(re, &reset_handlers, entry) {
Jan Kiszkadda9b292009-07-02 00:19:02 +02001260 if (re->func == func && re->opaque == opaque) {
Blue Swirl72cf2d42009-09-12 07:36:22 +00001261 QTAILQ_REMOVE(&reset_handlers, re, entry);
Anthony Liguori7267c092011-08-20 22:09:37 -05001262 g_free(re);
Jan Kiszkadda9b292009-07-02 00:19:02 +02001263 return;
1264 }
1265 }
1266}
1267
Jan Kiszkae063eb12011-06-14 18:29:43 +02001268void qemu_system_reset(bool report)
Jan Kiszkadda9b292009-07-02 00:19:02 +02001269{
1270 QEMUResetEntry *re, *nre;
1271
1272 /* reset all devices */
Blue Swirl72cf2d42009-09-12 07:36:22 +00001273 QTAILQ_FOREACH_SAFE(re, &reset_handlers, entry, nre) {
bellardbb0c6722004-06-20 12:37:32 +00001274 re->func(re->opaque);
1275 }
Jan Kiszkae063eb12011-06-14 18:29:43 +02001276 if (report) {
1277 monitor_protocol_event(QEVENT_RESET, NULL);
1278 }
Jan Kiszkaea375f92010-03-01 19:10:30 +01001279 cpu_synchronize_all_post_reset();
bellardbb0c6722004-06-20 12:37:32 +00001280}
1281
1282void qemu_system_reset_request(void)
1283{
bellardd1beab82006-10-02 19:44:22 +00001284 if (no_reboot) {
1285 shutdown_requested = 1;
1286 } else {
1287 reset_requested = 1;
1288 }
Jan Kiszkab4a3d962011-02-01 22:15:43 +01001289 cpu_stop_current();
aliguorid9f75a42009-04-24 18:03:11 +00001290 qemu_notify_event();
bellardbb0c6722004-06-20 12:37:32 +00001291}
1292
Gleb Natapovf64622c2011-03-15 13:56:04 +02001293void qemu_system_killed(int signal, pid_t pid)
1294{
1295 shutdown_signal = signal;
1296 shutdown_pid = pid;
1297 qemu_system_shutdown_request();
1298}
1299
bellardbb0c6722004-06-20 12:37:32 +00001300void qemu_system_shutdown_request(void)
1301{
1302 shutdown_requested = 1;
aliguorid9f75a42009-04-24 18:03:11 +00001303 qemu_notify_event();
bellardbb0c6722004-06-20 12:37:32 +00001304}
1305
bellard34751872005-07-02 14:31:34 +00001306void qemu_system_powerdown_request(void)
1307{
1308 powerdown_requested = 1;
aliguorid9f75a42009-04-24 18:03:11 +00001309 qemu_notify_event();
1310}
1311
Jan Kiszka8cf71712011-02-07 12:19:16 +01001312void qemu_system_debug_request(void)
1313{
1314 debug_requested = 1;
Jan Kiszka83f338f2011-02-07 12:19:17 +01001315 qemu_notify_event();
Jan Kiszka8cf71712011-02-07 12:19:16 +01001316}
1317
1318void qemu_system_vmstop_request(int reason)
1319{
1320 vmstop_requested = reason;
1321 qemu_notify_event();
1322}
1323
Paolo Bonzinid6f4ade2010-03-10 11:38:54 +01001324void main_loop_wait(int nonblocking)
aliguori56f3a5d2008-10-31 18:07:17 +00001325{
aliguori56f3a5d2008-10-31 18:07:17 +00001326 fd_set rfds, wfds, xfds;
1327 int ret, nfds;
1328 struct timeval tv;
Paolo Bonzinid6f4ade2010-03-10 11:38:54 +01001329 int timeout;
aliguori56f3a5d2008-10-31 18:07:17 +00001330
Paolo Bonzinid6f4ade2010-03-10 11:38:54 +01001331 if (nonblocking)
1332 timeout = 0;
1333 else {
1334 timeout = qemu_calculate_timeout();
1335 qemu_bh_update_timeout(&timeout);
1336 }
aliguori56f3a5d2008-10-31 18:07:17 +00001337
Jes Sorensen0d93ca72010-06-10 11:42:18 +02001338 os_host_main_loop_wait(&timeout);
aliguori56f3a5d2008-10-31 18:07:17 +00001339
Paolo Bonzini02981412011-03-09 18:21:09 +01001340 tv.tv_sec = timeout / 1000;
1341 tv.tv_usec = (timeout % 1000) * 1000;
1342
bellardfd1dff42006-02-01 21:29:26 +00001343 /* poll any events */
1344 /* XXX: separate device handlers from system ones */
aliguori6abfbd72008-11-05 20:49:37 +00001345 nfds = -1;
bellardfd1dff42006-02-01 21:29:26 +00001346 FD_ZERO(&rfds);
1347 FD_ZERO(&wfds);
bellarde0356492006-05-01 13:33:02 +00001348 FD_ZERO(&xfds);
Paolo Bonzini02981412011-03-09 18:21:09 +01001349 qemu_iohandler_fill(&nfds, &rfds, &wfds, &xfds);
Jan Kiszkad918f232009-06-24 14:42:30 +02001350 slirp_select_fill(&nfds, &rfds, &wfds, &xfds);
1351
aliguori48708522009-04-24 18:03:49 +00001352 qemu_mutex_unlock_iothread();
bellarde0356492006-05-01 13:33:02 +00001353 ret = select(nfds + 1, &rfds, &wfds, &xfds, &tv);
aliguori48708522009-04-24 18:03:49 +00001354 qemu_mutex_lock_iothread();
thscafffd42007-02-28 21:59:44 +00001355
Paolo Bonzini02981412011-03-09 18:21:09 +01001356 qemu_iohandler_poll(&rfds, &wfds, &xfds, ret);
Jan Kiszkad918f232009-06-24 14:42:30 +02001357 slirp_select_poll(&rfds, &wfds, &xfds, (ret < 0));
bellardc20709a2004-04-21 23:27:19 +00001358
Paolo Bonzinib6964822010-03-10 11:38:45 +01001359 qemu_run_all_timers();
Jan Kiszka21d5d122009-09-15 13:36:04 +02001360
pbrook423f0742007-05-23 00:06:54 +00001361 /* Check bottom-halves last in case any of the earlier events triggered
1362 them. */
1363 qemu_bh_poll();
ths3b46e622007-09-17 08:09:54 +00001364
bellard5905b2e2004-08-01 21:53:26 +00001365}
1366
Jan Kiszka46481d32011-02-01 22:15:47 +01001367#ifndef CONFIG_IOTHREAD
1368static int vm_request_pending(void)
aliguori43b96852009-04-24 18:03:33 +00001369{
Jan Kiszka46481d32011-02-01 22:15:47 +01001370 return powerdown_requested ||
1371 reset_requested ||
1372 shutdown_requested ||
1373 debug_requested ||
1374 vmstop_requested;
aliguori43b96852009-04-24 18:03:33 +00001375}
Jan Kiszka46481d32011-02-01 22:15:47 +01001376#endif
aliguori43b96852009-04-24 18:03:33 +00001377
Blue Swirld9c32312009-08-09 08:42:19 +00001378qemu_irq qemu_system_powerdown;
1379
aliguori43b96852009-04-24 18:03:33 +00001380static void main_loop(void)
1381{
Jan Kiszka8e1b90e2011-02-01 22:15:46 +01001382 bool nonblocking = false;
1383#ifdef CONFIG_PROFILER
1384 int64_t ti;
1385#endif
aliguori6e29f5d2009-04-24 18:04:02 +00001386 int r;
aliguori43b96852009-04-24 18:03:33 +00001387
Blue Swirl7277e022010-04-12 17:19:06 +00001388 qemu_main_loop_start();
aliguorid6dc3d42009-04-24 18:04:07 +00001389
aliguori6e29f5d2009-04-24 18:04:02 +00001390 for (;;) {
aliguorid6dc3d42009-04-24 18:04:07 +00001391#ifndef CONFIG_IOTHREAD
Jan Kiszka46481d32011-02-01 22:15:47 +01001392 nonblocking = cpu_exec_all();
1393 if (vm_request_pending()) {
1394 nonblocking = true;
1395 }
aliguorid6dc3d42009-04-24 18:04:07 +00001396#endif
aliguori43b96852009-04-24 18:03:33 +00001397#ifdef CONFIG_PROFILER
Jan Kiszka46481d32011-02-01 22:15:47 +01001398 ti = profile_getclock();
aliguori43b96852009-04-24 18:03:33 +00001399#endif
Jan Kiszka46481d32011-02-01 22:15:47 +01001400 main_loop_wait(nonblocking);
aliguori43b96852009-04-24 18:03:33 +00001401#ifdef CONFIG_PROFILER
Jan Kiszka46481d32011-02-01 22:15:47 +01001402 dev_time += profile_getclock() - ti;
aliguori43b96852009-04-24 18:03:33 +00001403#endif
aliguori43b96852009-04-24 18:03:33 +00001404
Jan Kiszka8cf71712011-02-07 12:19:16 +01001405 if (qemu_debug_requested()) {
1406 vm_stop(VMSTOP_DEBUG);
Luiz Capitulinob1a15e72009-11-26 22:59:04 -02001407 }
aliguori43b96852009-04-24 18:03:33 +00001408 if (qemu_shutdown_requested()) {
Gleb Natapovf64622c2011-03-15 13:56:04 +02001409 qemu_kill_report();
Blue Swirl242cd002009-12-04 18:05:45 +00001410 monitor_protocol_event(QEVENT_SHUTDOWN, NULL);
aliguori43b96852009-04-24 18:03:33 +00001411 if (no_shutdown) {
Jan Kiszkae07bbac2011-02-09 16:29:40 +01001412 vm_stop(VMSTOP_SHUTDOWN);
aliguori43b96852009-04-24 18:03:33 +00001413 } else
1414 break;
1415 }
aliguorid6dc3d42009-04-24 18:04:07 +00001416 if (qemu_reset_requested()) {
1417 pause_all_vcpus();
Jan Kiszkaa7ada152011-03-02 08:56:11 +01001418 cpu_synchronize_all_states();
Jan Kiszkae063eb12011-06-14 18:29:43 +02001419 qemu_system_reset(VMRESET_REPORT);
aliguorid6dc3d42009-04-24 18:04:07 +00001420 resume_all_vcpus();
1421 }
Blue Swirld9c32312009-08-09 08:42:19 +00001422 if (qemu_powerdown_requested()) {
Blue Swirl242cd002009-12-04 18:05:45 +00001423 monitor_protocol_event(QEVENT_POWERDOWN, NULL);
Blue Swirld9c32312009-08-09 08:42:19 +00001424 qemu_irq_raise(qemu_system_powerdown);
1425 }
Luiz Capitulinob1a15e72009-11-26 22:59:04 -02001426 if ((r = qemu_vmstop_requested())) {
aliguori6e29f5d2009-04-24 18:04:02 +00001427 vm_stop(r);
Luiz Capitulinob1a15e72009-11-26 22:59:04 -02001428 }
aliguori43b96852009-04-24 18:03:33 +00001429 }
MORITA Kazutaka2bc93fe2010-05-28 11:44:57 +09001430 bdrv_close_all();
aliguorid6dc3d42009-04-24 18:04:07 +00001431 pause_all_vcpus();
bellardb4608c02003-06-27 17:34:32 +00001432}
1433
pbrook9bd7e6d2009-04-07 22:58:45 +00001434static void version(void)
1435{
Thomas Monjalonf75ca1a2010-04-28 14:42:01 +02001436 printf("QEMU emulator version " QEMU_VERSION QEMU_PKGVERSION ", Copyright (c) 2003-2008 Fabrice Bellard\n");
pbrook9bd7e6d2009-04-07 22:58:45 +00001437}
1438
ths15f82202007-06-29 23:26:08 +00001439static void help(int exitcode)
bellard0824d6f2003-06-24 13:42:40 +00001440{
Paolo Bonzinie8105eb2010-02-04 16:49:59 +01001441 const char *options_help =
Blue Swirlad960902010-03-29 19:23:52 +00001442#define DEF(option, opt_arg, opt_enum, opt_help, arch_mask) \
1443 opt_help
blueswir15824d652009-03-28 06:44:27 +00001444#define DEFHEADING(text) stringify(text) "\n"
Jes Sorensen9f167322010-06-10 11:42:24 +02001445#include "qemu-options.def"
blueswir15824d652009-03-28 06:44:27 +00001446#undef DEF
1447#undef DEFHEADING
1448#undef GEN_DOCS
Paolo Bonzinie8105eb2010-02-04 16:49:59 +01001449 ;
1450 version();
1451 printf("usage: %s [options] [disk_image]\n"
malc3f020d72010-02-08 12:04:56 +03001452 "\n"
Thomas Monjalonf75ca1a2010-04-28 14:42:01 +02001453 "'disk_image' is a raw hard disk image for IDE hard disk 0\n"
Paolo Bonzinie8105eb2010-02-04 16:49:59 +01001454 "\n"
1455 "%s\n"
malc3f020d72010-02-08 12:04:56 +03001456 "During emulation, the following keys are useful:\n"
1457 "ctrl-alt-f toggle full screen\n"
1458 "ctrl-alt-n switch to virtual console 'n'\n"
1459 "ctrl-alt toggle mouse and keyboard grab\n"
1460 "\n"
Paolo Bonzinie8105eb2010-02-04 16:49:59 +01001461 "When using -nographic, press 'ctrl-a h' to get some help.\n",
1462 "qemu",
1463 options_help);
ths15f82202007-06-29 23:26:08 +00001464 exit(exitcode);
bellard0824d6f2003-06-24 13:42:40 +00001465}
1466
bellardcd6f1162004-05-13 22:02:20 +00001467#define HAS_ARG 0x0001
1468
bellardcd6f1162004-05-13 22:02:20 +00001469typedef struct QEMUOption {
1470 const char *name;
1471 int flags;
1472 int index;
Blue Swirlad960902010-03-29 19:23:52 +00001473 uint32_t arch_mask;
bellardcd6f1162004-05-13 22:02:20 +00001474} QEMUOption;
1475
blueswir1dbed7e42008-10-01 19:38:09 +00001476static const QEMUOption qemu_options[] = {
Blue Swirlad960902010-03-29 19:23:52 +00001477 { "h", 0, QEMU_OPTION_h, QEMU_ARCH_ALL },
1478#define DEF(option, opt_arg, opt_enum, opt_help, arch_mask) \
1479 { option, opt_arg, opt_enum, arch_mask },
blueswir15824d652009-03-28 06:44:27 +00001480#define DEFHEADING(text)
Jes Sorensen9f167322010-06-10 11:42:24 +02001481#include "qemu-options.def"
blueswir15824d652009-03-28 06:44:27 +00001482#undef DEF
1483#undef DEFHEADING
1484#undef GEN_DOCS
bellardcd6f1162004-05-13 22:02:20 +00001485 { NULL },
bellardfc01f7e2003-06-30 10:03:06 +00001486};
malc3893c122008-09-28 00:42:05 +00001487static void select_vgahw (const char *p)
1488{
1489 const char *opts;
1490
Gerd Hoffmann64465292009-12-08 13:11:45 +01001491 default_vga = 0;
Zachary Amsden86176752009-07-30 00:15:02 -10001492 vga_interface_type = VGA_NONE;
malc3893c122008-09-28 00:42:05 +00001493 if (strstart(p, "std", &opts)) {
Zachary Amsden86176752009-07-30 00:15:02 -10001494 vga_interface_type = VGA_STD;
malc3893c122008-09-28 00:42:05 +00001495 } else if (strstart(p, "cirrus", &opts)) {
Zachary Amsden86176752009-07-30 00:15:02 -10001496 vga_interface_type = VGA_CIRRUS;
malc3893c122008-09-28 00:42:05 +00001497 } else if (strstart(p, "vmware", &opts)) {
Zachary Amsden86176752009-07-30 00:15:02 -10001498 vga_interface_type = VGA_VMWARE;
aliguori94909d92009-04-22 15:19:53 +00001499 } else if (strstart(p, "xenfb", &opts)) {
Zachary Amsden86176752009-07-30 00:15:02 -10001500 vga_interface_type = VGA_XENFB;
Gerd Hoffmanna19cbfb2010-04-27 11:50:11 +02001501 } else if (strstart(p, "qxl", &opts)) {
1502 vga_interface_type = VGA_QXL;
aliguori28b85ed2009-04-22 15:19:48 +00001503 } else if (!strstart(p, "none", &opts)) {
malc3893c122008-09-28 00:42:05 +00001504 invalid_vga:
1505 fprintf(stderr, "Unknown vga type: %s\n", p);
1506 exit(1);
1507 }
malccb5a7aa2008-09-28 00:42:12 +00001508 while (*opts) {
1509 const char *nextopt;
1510
1511 if (strstart(opts, ",retrace=", &nextopt)) {
1512 opts = nextopt;
1513 if (strstart(opts, "dumb", &nextopt))
1514 vga_retrace_method = VGA_RETRACE_DUMB;
1515 else if (strstart(opts, "precise", &nextopt))
1516 vga_retrace_method = VGA_RETRACE_PRECISE;
1517 else goto invalid_vga;
1518 } else goto invalid_vga;
1519 opts = nextopt;
1520 }
malc3893c122008-09-28 00:42:05 +00001521}
1522
Jes Sorensen1472a952011-03-16 13:33:31 +01001523static DisplayType select_display(const char *p)
1524{
1525 const char *opts;
1526 DisplayType display = DT_DEFAULT;
1527
1528 if (strstart(p, "sdl", &opts)) {
1529#ifdef CONFIG_SDL
1530 display = DT_SDL;
1531 while (*opts) {
1532 const char *nextopt;
1533
1534 if (strstart(opts, ",frame=", &nextopt)) {
1535 opts = nextopt;
1536 if (strstart(opts, "on", &nextopt)) {
1537 no_frame = 0;
1538 } else if (strstart(opts, "off", &nextopt)) {
1539 no_frame = 1;
1540 } else {
Peter Maydell05175532011-03-23 03:40:57 +00001541 goto invalid_sdl_args;
Jes Sorensen1472a952011-03-16 13:33:31 +01001542 }
1543 } else if (strstart(opts, ",alt_grab=", &nextopt)) {
1544 opts = nextopt;
1545 if (strstart(opts, "on", &nextopt)) {
1546 alt_grab = 1;
1547 } else if (strstart(opts, "off", &nextopt)) {
1548 alt_grab = 0;
1549 } else {
Peter Maydell05175532011-03-23 03:40:57 +00001550 goto invalid_sdl_args;
Jes Sorensen1472a952011-03-16 13:33:31 +01001551 }
1552 } else if (strstart(opts, ",ctrl_grab=", &nextopt)) {
1553 opts = nextopt;
1554 if (strstart(opts, "on", &nextopt)) {
1555 ctrl_grab = 1;
1556 } else if (strstart(opts, "off", &nextopt)) {
1557 ctrl_grab = 0;
1558 } else {
Peter Maydell05175532011-03-23 03:40:57 +00001559 goto invalid_sdl_args;
Jes Sorensen1472a952011-03-16 13:33:31 +01001560 }
1561 } else if (strstart(opts, ",window_close=", &nextopt)) {
1562 opts = nextopt;
1563 if (strstart(opts, "on", &nextopt)) {
1564 no_quit = 0;
1565 } else if (strstart(opts, "off", &nextopt)) {
1566 no_quit = 1;
1567 } else {
Peter Maydell05175532011-03-23 03:40:57 +00001568 goto invalid_sdl_args;
Jes Sorensen1472a952011-03-16 13:33:31 +01001569 }
1570 } else {
Peter Maydell05175532011-03-23 03:40:57 +00001571 invalid_sdl_args:
1572 fprintf(stderr, "Invalid SDL option string: %s\n", p);
1573 exit(1);
Jes Sorensen1472a952011-03-16 13:33:31 +01001574 }
1575 opts = nextopt;
1576 }
1577#else
1578 fprintf(stderr, "SDL support is disabled\n");
1579 exit(1);
1580#endif
Jes Sorensen3264ff12011-03-16 13:33:33 +01001581 } else if (strstart(p, "vnc", &opts)) {
Jes Sorensen821601e2011-03-16 13:33:36 +01001582#ifdef CONFIG_VNC
Jes Sorensen3264ff12011-03-16 13:33:33 +01001583 display_remote++;
1584
1585 if (*opts) {
1586 const char *nextopt;
1587
1588 if (strstart(opts, "=", &nextopt)) {
1589 vnc_display = nextopt;
1590 }
1591 }
1592 if (!vnc_display) {
1593 fprintf(stderr, "VNC requires a display argument vnc=<display>\n");
1594 exit(1);
1595 }
Jes Sorensen821601e2011-03-16 13:33:36 +01001596#else
1597 fprintf(stderr, "VNC support is disabled\n");
1598 exit(1);
1599#endif
Jes Sorensen1472a952011-03-16 13:33:31 +01001600 } else if (strstart(p, "curses", &opts)) {
1601#ifdef CONFIG_CURSES
1602 display = DT_CURSES;
1603#else
1604 fprintf(stderr, "Curses support is disabled\n");
1605 exit(1);
1606#endif
Jes Sorensen4171d322011-03-16 13:33:32 +01001607 } else if (strstart(p, "none", &opts)) {
1608 display = DT_NONE;
Jes Sorensen1472a952011-03-16 13:33:31 +01001609 } else {
Jes Sorensen1472a952011-03-16 13:33:31 +01001610 fprintf(stderr, "Unknown display type: %s\n", p);
1611 exit(1);
1612 }
1613
1614 return display;
1615}
1616
Markus Armbruster7d4c3d52009-06-26 19:15:14 +02001617static int balloon_parse(const char *arg)
1618{
Gerd Hoffmann382f0742009-08-14 10:34:22 +02001619 QemuOpts *opts;
Markus Armbruster7d4c3d52009-06-26 19:15:14 +02001620
Gerd Hoffmann382f0742009-08-14 10:34:22 +02001621 if (strcmp(arg, "none") == 0) {
1622 return 0;
Markus Armbruster7d4c3d52009-06-26 19:15:14 +02001623 }
Gerd Hoffmann382f0742009-08-14 10:34:22 +02001624
1625 if (!strncmp(arg, "virtio", 6)) {
1626 if (arg[6] == ',') {
1627 /* have params -> parse them */
Gerd Hoffmann3329f072010-08-20 13:52:01 +02001628 opts = qemu_opts_parse(qemu_find_opts("device"), arg+7, 0);
Gerd Hoffmann382f0742009-08-14 10:34:22 +02001629 if (!opts)
1630 return -1;
1631 } else {
1632 /* create empty opts */
Gerd Hoffmann3329f072010-08-20 13:52:01 +02001633 opts = qemu_opts_create(qemu_find_opts("device"), NULL, 0);
Gerd Hoffmann382f0742009-08-14 10:34:22 +02001634 }
Alexander Graf29f82b32011-03-29 15:29:29 +02001635 qemu_opt_set(opts, "driver", "virtio-balloon");
Gerd Hoffmann382f0742009-08-14 10:34:22 +02001636 return 0;
1637 }
1638
1639 return -1;
Markus Armbruster7d4c3d52009-06-26 19:15:14 +02001640}
Markus Armbruster7d4c3d52009-06-26 19:15:14 +02001641
Paul Brook5cea8592009-05-30 00:52:44 +01001642char *qemu_find_file(int type, const char *name)
1643{
1644 int len;
1645 const char *subdir;
1646 char *buf;
1647
1648 /* If name contains path separators then try it as a straight path. */
1649 if ((strchr(name, '/') || strchr(name, '\\'))
1650 && access(name, R_OK) == 0) {
Anthony Liguori7267c092011-08-20 22:09:37 -05001651 return g_strdup(name);
Paul Brook5cea8592009-05-30 00:52:44 +01001652 }
1653 switch (type) {
1654 case QEMU_FILE_TYPE_BIOS:
1655 subdir = "";
1656 break;
1657 case QEMU_FILE_TYPE_KEYMAP:
1658 subdir = "keymaps/";
1659 break;
1660 default:
1661 abort();
1662 }
1663 len = strlen(data_dir) + strlen(name) + strlen(subdir) + 2;
Anthony Liguori7267c092011-08-20 22:09:37 -05001664 buf = g_malloc0(len);
Blue Swirl3a417592009-06-09 19:12:21 +00001665 snprintf(buf, len, "%s/%s%s", data_dir, subdir, name);
Paul Brook5cea8592009-05-30 00:52:44 +01001666 if (access(buf, R_OK)) {
Anthony Liguori7267c092011-08-20 22:09:37 -05001667 g_free(buf);
Paul Brook5cea8592009-05-30 00:52:44 +01001668 return NULL;
1669 }
1670 return buf;
1671}
1672
Markus Armbrusterff952ba2010-01-29 19:48:57 +01001673static int device_help_func(QemuOpts *opts, void *opaque)
1674{
1675 return qdev_device_help(opts);
1676}
1677
Gerd Hoffmannf31d07d2009-07-31 12:25:37 +02001678static int device_init_func(QemuOpts *opts, void *opaque)
1679{
1680 DeviceState *dev;
1681
1682 dev = qdev_device_add(opts);
1683 if (!dev)
1684 return -1;
1685 return 0;
1686}
1687
Gerd Hoffmann1a688d32009-12-08 13:11:36 +01001688static int chardev_init_func(QemuOpts *opts, void *opaque)
1689{
1690 CharDriverState *chr;
1691
1692 chr = qemu_chr_open_opts(opts, NULL);
1693 if (!chr)
1694 return -1;
1695 return 0;
1696}
1697
Venkateswararao Jujjuri (JV)758e8e32010-06-14 13:34:41 -07001698#ifdef CONFIG_VIRTFS
Gautham R Shenoy74db9202010-04-29 17:44:43 +05301699static int fsdev_init_func(QemuOpts *opts, void *opaque)
1700{
1701 int ret;
1702 ret = qemu_fsdev_add(opts);
1703
1704 return ret;
1705}
1706#endif
1707
Gerd Hoffmann88589342009-12-08 13:11:50 +01001708static int mon_init_func(QemuOpts *opts, void *opaque)
1709{
1710 CharDriverState *chr;
1711 const char *chardev;
1712 const char *mode;
1713 int flags;
1714
1715 mode = qemu_opt_get(opts, "mode");
1716 if (mode == NULL) {
1717 mode = "readline";
1718 }
1719 if (strcmp(mode, "readline") == 0) {
1720 flags = MONITOR_USE_READLINE;
1721 } else if (strcmp(mode, "control") == 0) {
1722 flags = MONITOR_USE_CONTROL;
1723 } else {
1724 fprintf(stderr, "unknown monitor mode \"%s\"\n", mode);
1725 exit(1);
1726 }
1727
Daniel P. Berrange39eaab92010-06-07 15:42:31 +01001728 if (qemu_opt_get_bool(opts, "pretty", 0))
1729 flags |= MONITOR_USE_PRETTY;
1730
Gerd Hoffmann88589342009-12-08 13:11:50 +01001731 if (qemu_opt_get_bool(opts, "default", 0))
1732 flags |= MONITOR_IS_DEFAULT;
1733
1734 chardev = qemu_opt_get(opts, "chardev");
1735 chr = qemu_chr_find(chardev);
1736 if (chr == NULL) {
1737 fprintf(stderr, "chardev \"%s\" not found\n", chardev);
1738 exit(1);
1739 }
1740
1741 monitor_init(chr, flags);
1742 return 0;
1743}
1744
Gerd Hoffmann6ca55822009-12-08 13:11:52 +01001745static void monitor_parse(const char *optarg, const char *mode)
Gerd Hoffmann88589342009-12-08 13:11:50 +01001746{
1747 static int monitor_device_index = 0;
1748 QemuOpts *opts;
1749 const char *p;
1750 char label[32];
1751 int def = 0;
1752
1753 if (strstart(optarg, "chardev:", &p)) {
1754 snprintf(label, sizeof(label), "%s", p);
1755 } else {
Jan Kiszka140e0652010-04-06 16:55:52 +02001756 snprintf(label, sizeof(label), "compat_monitor%d",
1757 monitor_device_index);
1758 if (monitor_device_index == 0) {
Gerd Hoffmann88589342009-12-08 13:11:50 +01001759 def = 1;
1760 }
1761 opts = qemu_chr_parse_compat(label, optarg);
1762 if (!opts) {
1763 fprintf(stderr, "parse error: %s\n", optarg);
1764 exit(1);
1765 }
1766 }
1767
Gerd Hoffmann3329f072010-08-20 13:52:01 +02001768 opts = qemu_opts_create(qemu_find_opts("mon"), label, 1);
Gerd Hoffmann88589342009-12-08 13:11:50 +01001769 if (!opts) {
1770 fprintf(stderr, "duplicate chardev: %s\n", label);
1771 exit(1);
1772 }
Gerd Hoffmann6ca55822009-12-08 13:11:52 +01001773 qemu_opt_set(opts, "mode", mode);
Gerd Hoffmann88589342009-12-08 13:11:50 +01001774 qemu_opt_set(opts, "chardev", label);
1775 if (def)
1776 qemu_opt_set(opts, "default", "on");
1777 monitor_device_index++;
1778}
1779
Gerd Hoffmannbd3c9482009-07-15 13:59:26 +02001780struct device_config {
1781 enum {
Gerd Hoffmannaee1b932009-12-08 13:11:53 +01001782 DEV_USB, /* -usbdevice */
1783 DEV_BT, /* -bt */
1784 DEV_SERIAL, /* -serial */
1785 DEV_PARALLEL, /* -parallel */
1786 DEV_VIRTCON, /* -virtioconsole */
H. Peter Anvinc9f398e2009-12-29 13:51:36 -08001787 DEV_DEBUGCON, /* -debugcon */
Gerd Hoffmannbd3c9482009-07-15 13:59:26 +02001788 } type;
1789 const char *cmdline;
Blue Swirl72cf2d42009-09-12 07:36:22 +00001790 QTAILQ_ENTRY(device_config) next;
Gerd Hoffmannbd3c9482009-07-15 13:59:26 +02001791};
Blue Swirl72cf2d42009-09-12 07:36:22 +00001792QTAILQ_HEAD(, device_config) device_configs = QTAILQ_HEAD_INITIALIZER(device_configs);
Gerd Hoffmannbd3c9482009-07-15 13:59:26 +02001793
1794static void add_device_config(int type, const char *cmdline)
1795{
1796 struct device_config *conf;
1797
Anthony Liguori7267c092011-08-20 22:09:37 -05001798 conf = g_malloc0(sizeof(*conf));
Gerd Hoffmannbd3c9482009-07-15 13:59:26 +02001799 conf->type = type;
1800 conf->cmdline = cmdline;
Blue Swirl72cf2d42009-09-12 07:36:22 +00001801 QTAILQ_INSERT_TAIL(&device_configs, conf, next);
Gerd Hoffmannbd3c9482009-07-15 13:59:26 +02001802}
1803
1804static int foreach_device_config(int type, int (*func)(const char *cmdline))
1805{
1806 struct device_config *conf;
1807 int rc;
1808
Blue Swirl72cf2d42009-09-12 07:36:22 +00001809 QTAILQ_FOREACH(conf, &device_configs, next) {
Gerd Hoffmannbd3c9482009-07-15 13:59:26 +02001810 if (conf->type != type)
1811 continue;
1812 rc = func(conf->cmdline);
1813 if (0 != rc)
1814 return rc;
1815 }
1816 return 0;
1817}
1818
Gerd Hoffmann998bbd72009-12-08 13:11:41 +01001819static int serial_parse(const char *devname)
1820{
1821 static int index = 0;
1822 char label[32];
1823
1824 if (strcmp(devname, "none") == 0)
1825 return 0;
1826 if (index == MAX_SERIAL_PORTS) {
1827 fprintf(stderr, "qemu: too many serial ports\n");
1828 exit(1);
1829 }
1830 snprintf(label, sizeof(label), "serial%d", index);
1831 serial_hds[index] = qemu_chr_open(label, devname, NULL);
1832 if (!serial_hds[index]) {
1833 fprintf(stderr, "qemu: could not open serial device '%s': %s\n",
1834 devname, strerror(errno));
1835 return -1;
1836 }
1837 index++;
1838 return 0;
1839}
1840
Gerd Hoffmann6a5e8b02009-12-08 13:11:42 +01001841static int parallel_parse(const char *devname)
1842{
1843 static int index = 0;
1844 char label[32];
1845
1846 if (strcmp(devname, "none") == 0)
1847 return 0;
1848 if (index == MAX_PARALLEL_PORTS) {
1849 fprintf(stderr, "qemu: too many parallel ports\n");
1850 exit(1);
1851 }
1852 snprintf(label, sizeof(label), "parallel%d", index);
1853 parallel_hds[index] = qemu_chr_open(label, devname, NULL);
1854 if (!parallel_hds[index]) {
1855 fprintf(stderr, "qemu: could not open parallel device '%s': %s\n",
1856 devname, strerror(errno));
1857 return -1;
1858 }
1859 index++;
1860 return 0;
1861}
1862
Gerd Hoffmannaee1b932009-12-08 13:11:53 +01001863static int virtcon_parse(const char *devname)
1864{
Gerd Hoffmann3329f072010-08-20 13:52:01 +02001865 QemuOptsList *device = qemu_find_opts("device");
Gerd Hoffmannaee1b932009-12-08 13:11:53 +01001866 static int index = 0;
1867 char label[32];
Amit Shah392ecf52010-01-21 16:19:23 +05301868 QemuOpts *bus_opts, *dev_opts;
Gerd Hoffmannaee1b932009-12-08 13:11:53 +01001869
1870 if (strcmp(devname, "none") == 0)
1871 return 0;
1872 if (index == MAX_VIRTIO_CONSOLES) {
1873 fprintf(stderr, "qemu: too many virtio consoles\n");
1874 exit(1);
1875 }
Amit Shah392ecf52010-01-21 16:19:23 +05301876
Gerd Hoffmann3329f072010-08-20 13:52:01 +02001877 bus_opts = qemu_opts_create(device, NULL, 0);
Amit Shah392ecf52010-01-21 16:19:23 +05301878 qemu_opt_set(bus_opts, "driver", "virtio-serial");
1879
Gerd Hoffmann3329f072010-08-20 13:52:01 +02001880 dev_opts = qemu_opts_create(device, NULL, 0);
Amit Shah392ecf52010-01-21 16:19:23 +05301881 qemu_opt_set(dev_opts, "driver", "virtconsole");
1882
Gerd Hoffmannaee1b932009-12-08 13:11:53 +01001883 snprintf(label, sizeof(label), "virtcon%d", index);
1884 virtcon_hds[index] = qemu_chr_open(label, devname, NULL);
1885 if (!virtcon_hds[index]) {
1886 fprintf(stderr, "qemu: could not open virtio console '%s': %s\n",
1887 devname, strerror(errno));
1888 return -1;
1889 }
Amit Shah392ecf52010-01-21 16:19:23 +05301890 qemu_opt_set(dev_opts, "chardev", label);
1891
Gerd Hoffmannaee1b932009-12-08 13:11:53 +01001892 index++;
1893 return 0;
1894}
1895
H. Peter Anvinc9f398e2009-12-29 13:51:36 -08001896static int debugcon_parse(const char *devname)
1897{
1898 QemuOpts *opts;
1899
1900 if (!qemu_chr_open("debugcon", devname, NULL)) {
1901 exit(1);
1902 }
Gerd Hoffmann3329f072010-08-20 13:52:01 +02001903 opts = qemu_opts_create(qemu_find_opts("device"), "debugcon", 1);
H. Peter Anvinc9f398e2009-12-29 13:51:36 -08001904 if (!opts) {
1905 fprintf(stderr, "qemu: already have a debugcon device\n");
1906 exit(1);
1907 }
1908 qemu_opt_set(opts, "driver", "isa-debugcon");
1909 qemu_opt_set(opts, "chardev", "debugcon");
1910 return 0;
1911}
1912
Jan Kiszka9052ea62011-07-23 12:38:37 +02001913static QEMUMachine *machine_parse(const char *name)
1914{
1915 QEMUMachine *m, *machine = NULL;
1916
1917 if (name) {
1918 machine = find_machine(name);
1919 }
1920 if (machine) {
1921 return machine;
1922 }
1923 printf("Supported machines are:\n");
1924 for (m = first_machine; m != NULL; m = m->next) {
1925 if (m->alias) {
1926 printf("%-10s %s (alias of %s)\n", m->alias, m->desc, m->name);
1927 }
1928 printf("%-10s %s%s\n", m->name, m->desc,
1929 m->is_default ? " (default)" : "");
1930 }
1931 exit(!name || *name != '?');
1932}
1933
Anthony PERARD303d4e82010-09-21 20:05:31 +01001934static int tcg_init(void)
1935{
Jan Kiszkad5ab9712011-08-02 16:10:21 +02001936 tcg_exec_init(tcg_tb_size * 1024 * 1024);
Anthony PERARD303d4e82010-09-21 20:05:31 +01001937 return 0;
1938}
1939
1940static struct {
1941 const char *opt_name;
1942 const char *name;
1943 int (*available)(void);
1944 int (*init)(void);
1945 int *allowed;
1946} accel_list[] = {
1947 { "tcg", "tcg", tcg_available, tcg_init, &tcg_allowed },
Anthony PERARD3285cf42010-08-19 12:27:56 +01001948 { "xen", "Xen", xen_available, xen_init, &xen_allowed },
Anthony PERARD303d4e82010-09-21 20:05:31 +01001949 { "kvm", "KVM", kvm_available, kvm_init, &kvm_allowed },
1950};
1951
1952static int configure_accelerator(void)
1953{
1954 const char *p = NULL;
1955 char buf[10];
1956 int i, ret;
1957 bool accel_initalised = 0;
1958 bool init_failed = 0;
1959
1960 QemuOptsList *list = qemu_find_opts("machine");
1961 if (!QTAILQ_EMPTY(&list->head)) {
1962 p = qemu_opt_get(QTAILQ_FIRST(&list->head), "accel");
1963 }
1964
1965 if (p == NULL) {
1966 /* Use the default "accelerator", tcg */
1967 p = "tcg";
1968 }
1969
1970 while (!accel_initalised && *p != '\0') {
1971 if (*p == ':') {
1972 p++;
1973 }
1974 p = get_opt_name(buf, sizeof (buf), p, ':');
1975 for (i = 0; i < ARRAY_SIZE(accel_list); i++) {
1976 if (strcmp(accel_list[i].opt_name, buf) == 0) {
Anthony Liguoria16c53b2011-06-06 08:25:06 -05001977 *(accel_list[i].allowed) = 1;
Anthony PERARD303d4e82010-09-21 20:05:31 +01001978 ret = accel_list[i].init();
1979 if (ret < 0) {
1980 init_failed = 1;
1981 if (!accel_list[i].available()) {
1982 printf("%s not supported for this target\n",
1983 accel_list[i].name);
1984 } else {
1985 fprintf(stderr, "failed to initialize %s: %s\n",
1986 accel_list[i].name,
1987 strerror(-ret));
1988 }
Anthony Liguoria16c53b2011-06-06 08:25:06 -05001989 *(accel_list[i].allowed) = 0;
Anthony PERARD303d4e82010-09-21 20:05:31 +01001990 } else {
1991 accel_initalised = 1;
Anthony PERARD303d4e82010-09-21 20:05:31 +01001992 }
1993 break;
1994 }
1995 }
1996 if (i == ARRAY_SIZE(accel_list)) {
1997 fprintf(stderr, "\"%s\" accelerator does not exist.\n", buf);
1998 }
1999 }
2000
2001 if (!accel_initalised) {
2002 fprintf(stderr, "No accelerator found!\n");
2003 exit(1);
2004 }
2005
2006 if (init_failed) {
2007 fprintf(stderr, "Back to %s accelerator.\n", accel_list[i].name);
2008 }
2009
2010 return !accel_initalised;
2011}
2012
Gerd Hoffmannfd42dee2010-06-04 14:08:07 +02002013void qemu_add_exit_notifier(Notifier *notify)
2014{
2015 notifier_list_add(&exit_notifiers, notify);
2016}
2017
2018void qemu_remove_exit_notifier(Notifier *notify)
2019{
2020 notifier_list_remove(&exit_notifiers, notify);
2021}
2022
2023static void qemu_run_exit_notifiers(void)
2024{
Jan Kiszka9e8dd452011-06-20 14:06:26 +02002025 notifier_list_notify(&exit_notifiers, NULL);
Gerd Hoffmannfd42dee2010-06-04 14:08:07 +02002026}
2027
Gleb Natapov4cab9462010-12-08 13:35:08 +02002028void qemu_add_machine_init_done_notifier(Notifier *notify)
2029{
2030 notifier_list_add(&machine_init_done_notifiers, notify);
2031}
2032
2033static void qemu_run_machine_init_done_notifiers(void)
2034{
Jan Kiszka9e8dd452011-06-20 14:06:26 +02002035 notifier_list_notify(&machine_init_done_notifiers, NULL);
Gleb Natapov4cab9462010-12-08 13:35:08 +02002036}
2037
Anthony Liguori6530a972010-01-22 09:18:06 -06002038static const QEMUOption *lookup_opt(int argc, char **argv,
2039 const char **poptarg, int *poptind)
2040{
2041 const QEMUOption *popt;
2042 int optind = *poptind;
2043 char *r = argv[optind];
2044 const char *optarg;
2045
Markus Armbruster0f0bc3f2010-02-18 20:13:51 +01002046 loc_set_cmdline(argv, optind, 1);
Anthony Liguori6530a972010-01-22 09:18:06 -06002047 optind++;
2048 /* Treat --foo the same as -foo. */
2049 if (r[1] == '-')
2050 r++;
2051 popt = qemu_options;
2052 for(;;) {
2053 if (!popt->name) {
Markus Armbruster0f0bc3f2010-02-18 20:13:51 +01002054 error_report("invalid option");
Anthony Liguori6530a972010-01-22 09:18:06 -06002055 exit(1);
2056 }
2057 if (!strcmp(popt->name, r + 1))
2058 break;
2059 popt++;
2060 }
2061 if (popt->flags & HAS_ARG) {
2062 if (optind >= argc) {
Markus Armbruster0f0bc3f2010-02-18 20:13:51 +01002063 error_report("requires an argument");
Anthony Liguori6530a972010-01-22 09:18:06 -06002064 exit(1);
2065 }
2066 optarg = argv[optind++];
Markus Armbruster0f0bc3f2010-02-18 20:13:51 +01002067 loc_set_cmdline(argv, optind - 2, 2);
Anthony Liguori6530a972010-01-22 09:18:06 -06002068 } else {
2069 optarg = NULL;
2070 }
2071
2072 *poptarg = optarg;
2073 *poptind = optind;
2074
2075 return popt;
2076}
2077
malc902b3d52008-12-10 19:18:40 +00002078int main(int argc, char **argv, char **envp)
bellard0824d6f2003-06-24 13:42:40 +00002079{
aliguori59030a82009-04-05 18:43:41 +00002080 const char *gdbstub_dev = NULL;
thse4bcb142007-12-02 04:51:10 +00002081 int i;
Eduardo Habkostda1fcfd2010-04-06 19:22:07 -03002082 int snapshot, linux_boot;
Paolo Bonzini4e3de9e2010-03-10 11:38:48 +01002083 const char *icount_option = NULL;
bellard7f7f9872003-10-30 01:11:23 +00002084 const char *initrd_filename;
bellarda20dd502003-09-30 21:07:02 +00002085 const char *kernel_filename, *kernel_cmdline;
Anthony Liguori195325a2009-10-30 12:42:29 -05002086 char boot_devices[33] = "cad"; /* default to HD->floppy->CD-ROM */
aliguori3023f332009-01-16 19:04:14 +00002087 DisplayState *ds;
aliguori7d957bd2009-01-15 22:14:11 +00002088 DisplayChangeListener *dcl;
bellard46d47672004-11-16 01:45:27 +00002089 int cyls, heads, secs, translation;
Gerd Hoffmannf31d07d2009-07-31 12:25:37 +02002090 QemuOpts *hda_opts = NULL, *opts;
Gerd Hoffmann03b0ba72010-08-20 13:52:02 +02002091 QemuOptsList *olist;
bellardcd6f1162004-05-13 22:02:20 +00002092 int optind;
Anthony Liguori6530a972010-01-22 09:18:06 -06002093 const char *optarg;
bellardd63d3072004-10-03 13:29:03 +00002094 const char *loadvm = NULL;
bellardcc1daa42005-06-05 14:49:17 +00002095 QEMUMachine *machine;
j_mayer94fc95c2007-03-05 19:44:02 +00002096 const char *cpu_model;
ths93815bc2007-03-19 15:58:31 +00002097 const char *pid_file = NULL;
aliguori5bb79102008-10-13 03:12:02 +00002098 const char *incoming = NULL;
Jes Sorensen821601e2011-03-16 13:33:36 +01002099#ifdef CONFIG_VNC
Anthony Liguori993fbfd2009-05-21 16:54:00 -05002100 int show_vnc_port = 0;
Jes Sorensen821601e2011-03-16 13:33:36 +01002101#endif
Anthony Liguori292444c2010-01-21 10:57:58 -06002102 int defconfig = 1;
Prerna Saxenaab6540d2010-08-09 11:48:32 +01002103 const char *trace_file = NULL;
Matthew Fernandezc235d732011-06-07 16:32:40 +00002104 const char *log_mask = NULL;
2105 const char *log_file = NULL;
Stefan Hajnoczi0b5538c2011-02-26 18:38:39 +00002106
Gerd Hoffmannfd42dee2010-06-04 14:08:07 +02002107 atexit(qemu_run_exit_notifiers);
Markus Armbruster65abca02010-02-24 14:37:14 +01002108 error_set_progname(argv[0]);
2109
Jan Kiszka68752042009-09-15 13:36:04 +02002110 init_clocks();
2111
malc902b3d52008-12-10 19:18:40 +00002112 qemu_cache_utils_init(envp);
2113
Blue Swirl72cf2d42009-09-12 07:36:22 +00002114 QLIST_INIT (&vm_change_state_head);
Jes Sorensenfe98ac12010-06-10 11:42:21 +02002115 os_setup_early_signal_handling();
bellardbe995c22006-06-25 16:25:21 +00002116
Anthony Liguorif80f9ec2009-05-20 18:38:09 -05002117 module_call_init(MODULE_INIT_MACHINE);
Anthony Liguori0c257432009-05-21 20:41:01 -05002118 machine = find_default_machine();
j_mayer94fc95c2007-03-05 19:44:02 +00002119 cpu_model = NULL;
bellardfc01f7e2003-06-30 10:03:06 +00002120 initrd_filename = NULL;
aurel324fc5d072008-04-27 21:39:40 +00002121 ram_size = 0;
bellard33e39632003-07-06 17:15:21 +00002122 snapshot = 0;
bellarda20dd502003-09-30 21:07:02 +00002123 kernel_filename = NULL;
2124 kernel_cmdline = "";
bellardc4b1fcc2004-03-14 21:44:30 +00002125 cyls = heads = secs = 0;
bellard46d47672004-11-16 01:45:27 +00002126 translation = BIOS_ATA_TRANSLATION_AUTO;
bellardc4b1fcc2004-03-14 21:44:30 +00002127
aliguori268a3622009-04-21 22:30:27 +00002128 for (i = 0; i < MAX_NODES; i++) {
2129 node_mem[i] = 0;
2130 node_cpumask[i] = 0;
2131 }
2132
aliguori268a3622009-04-21 22:30:27 +00002133 nb_numa_nodes = 0;
bellard7c9d8e02005-11-15 22:16:05 +00002134 nb_nics = 0;
ths3b46e622007-09-17 08:09:54 +00002135
blueswir141bd6392008-10-05 09:56:21 +00002136 autostart= 1;
2137
Anthony Liguori292444c2010-01-21 10:57:58 -06002138 /* first pass of option parsing */
2139 optind = 1;
2140 while (optind < argc) {
2141 if (argv[optind][0] != '-') {
2142 /* disk image */
Anthony Liguori28e68d62010-01-27 10:46:00 -06002143 optind++;
Anthony Liguori292444c2010-01-21 10:57:58 -06002144 continue;
2145 } else {
2146 const QEMUOption *popt;
2147
2148 popt = lookup_opt(argc, argv, &optarg, &optind);
2149 switch (popt->index) {
2150 case QEMU_OPTION_nodefconfig:
2151 defconfig=0;
2152 break;
2153 }
2154 }
2155 }
2156
2157 if (defconfig) {
Kevin Wolfdcfb0932010-03-05 17:25:55 +01002158 int ret;
Markus Armbrustercf5a65a2010-02-18 19:48:33 +01002159
Kevin Wolfdcfb0932010-03-05 17:25:55 +01002160 ret = qemu_read_config_file(CONFIG_QEMU_CONFDIR "/qemu.conf");
Kevin Wolf019e78b2010-05-17 10:36:47 +02002161 if (ret < 0 && ret != -ENOENT) {
Kevin Wolfdcfb0932010-03-05 17:25:55 +01002162 exit(1);
Anthony Liguori292444c2010-01-21 10:57:58 -06002163 }
2164
Kevin Wolfdcfb0932010-03-05 17:25:55 +01002165 ret = qemu_read_config_file(arch_config_name);
Kevin Wolf019e78b2010-05-17 10:36:47 +02002166 if (ret < 0 && ret != -ENOENT) {
Kevin Wolfdcfb0932010-03-05 17:25:55 +01002167 exit(1);
Anthony Liguori292444c2010-01-21 10:57:58 -06002168 }
2169 }
Blue Swirlde06f8d2010-03-29 19:23:50 +00002170 cpudef_init();
Anthony Liguori292444c2010-01-21 10:57:58 -06002171
2172 /* second pass of option parsing */
bellardcd6f1162004-05-13 22:02:20 +00002173 optind = 1;
bellard0824d6f2003-06-24 13:42:40 +00002174 for(;;) {
bellardcd6f1162004-05-13 22:02:20 +00002175 if (optind >= argc)
bellard0824d6f2003-06-24 13:42:40 +00002176 break;
Anthony Liguori6530a972010-01-22 09:18:06 -06002177 if (argv[optind][0] != '-') {
Markus Armbruster2292dda2011-01-28 11:21:41 +01002178 hda_opts = drive_add(IF_DEFAULT, 0, argv[optind++], HD_OPTS);
bellardcd6f1162004-05-13 22:02:20 +00002179 } else {
2180 const QEMUOption *popt;
2181
Anthony Liguori6530a972010-01-22 09:18:06 -06002182 popt = lookup_opt(argc, argv, &optarg, &optind);
Blue Swirlad960902010-03-29 19:23:52 +00002183 if (!(popt->arch_mask & arch_type)) {
2184 printf("Option %s not supported for this target\n", popt->name);
2185 exit(1);
2186 }
bellardcd6f1162004-05-13 22:02:20 +00002187 switch(popt->index) {
bellardcc1daa42005-06-05 14:49:17 +00002188 case QEMU_OPTION_M:
Jan Kiszka9052ea62011-07-23 12:38:37 +02002189 machine = machine_parse(optarg);
bellardcc1daa42005-06-05 14:49:17 +00002190 break;
j_mayer94fc95c2007-03-05 19:44:02 +00002191 case QEMU_OPTION_cpu:
2192 /* hw initialization will check this */
ths15f82202007-06-29 23:26:08 +00002193 if (*optarg == '?') {
Blue Swirl262353c2010-05-04 19:55:35 +00002194 list_cpus(stdout, &fprintf, optarg);
ths15f82202007-06-29 23:26:08 +00002195 exit(0);
j_mayer94fc95c2007-03-05 19:44:02 +00002196 } else {
2197 cpu_model = optarg;
2198 }
2199 break;
bellardcd6f1162004-05-13 22:02:20 +00002200 case QEMU_OPTION_initrd:
bellardfc01f7e2003-06-30 10:03:06 +00002201 initrd_filename = optarg;
2202 break;
bellardcd6f1162004-05-13 22:02:20 +00002203 case QEMU_OPTION_hda:
Markus Armbruster5645b0f2011-01-31 11:50:09 +01002204 {
2205 char buf[256];
2206 if (cyls == 0)
2207 snprintf(buf, sizeof(buf), "%s", HD_OPTS);
2208 else
2209 snprintf(buf, sizeof(buf),
2210 "%s,cyls=%d,heads=%d,secs=%d%s",
2211 HD_OPTS , cyls, heads, secs,
2212 translation == BIOS_ATA_TRANSLATION_LBA ?
thse4bcb142007-12-02 04:51:10 +00002213 ",trans=lba" :
Markus Armbruster5645b0f2011-01-31 11:50:09 +01002214 translation == BIOS_ATA_TRANSLATION_NONE ?
thse4bcb142007-12-02 04:51:10 +00002215 ",trans=none" : "");
Markus Armbruster5645b0f2011-01-31 11:50:09 +01002216 drive_add(IF_DEFAULT, 0, optarg, buf);
2217 break;
2218 }
bellardcd6f1162004-05-13 22:02:20 +00002219 case QEMU_OPTION_hdb:
bellardcc1daa42005-06-05 14:49:17 +00002220 case QEMU_OPTION_hdc:
2221 case QEMU_OPTION_hdd:
Markus Armbruster2292dda2011-01-28 11:21:41 +01002222 drive_add(IF_DEFAULT, popt->index - QEMU_OPTION_hda, optarg,
2223 HD_OPTS);
bellardfc01f7e2003-06-30 10:03:06 +00002224 break;
thse4bcb142007-12-02 04:51:10 +00002225 case QEMU_OPTION_drive:
Michael Tokareve2982c32011-03-30 16:31:05 +04002226 if (drive_def(optarg) == NULL) {
2227 exit(1);
2228 }
thse4bcb142007-12-02 04:51:10 +00002229 break;
Gerd Hoffmannd058fe02009-07-31 12:25:36 +02002230 case QEMU_OPTION_set:
2231 if (qemu_set_option(optarg) != 0)
2232 exit(1);
2233 break;
Gerd Hoffmannd0fef6f2009-12-08 13:11:34 +01002234 case QEMU_OPTION_global:
2235 if (qemu_global_option(optarg) != 0)
2236 exit(1);
2237 break;
balrog3e3d5812007-04-30 02:09:25 +00002238 case QEMU_OPTION_mtdblock:
Markus Armbruster2292dda2011-01-28 11:21:41 +01002239 drive_add(IF_MTD, -1, optarg, MTD_OPTS);
balrog3e3d5812007-04-30 02:09:25 +00002240 break;
pbrooka1bb27b2007-04-06 16:49:48 +00002241 case QEMU_OPTION_sd:
Markus Armbruster2292dda2011-01-28 11:21:41 +01002242 drive_add(IF_SD, 0, optarg, SD_OPTS);
pbrooka1bb27b2007-04-06 16:49:48 +00002243 break;
j_mayer86f55662007-04-24 06:52:59 +00002244 case QEMU_OPTION_pflash:
Markus Armbruster2292dda2011-01-28 11:21:41 +01002245 drive_add(IF_PFLASH, -1, optarg, PFLASH_OPTS);
j_mayer86f55662007-04-24 06:52:59 +00002246 break;
bellardcd6f1162004-05-13 22:02:20 +00002247 case QEMU_OPTION_snapshot:
bellard33e39632003-07-06 17:15:21 +00002248 snapshot = 1;
2249 break;
bellardcd6f1162004-05-13 22:02:20 +00002250 case QEMU_OPTION_hdachs:
bellard330d0412003-07-26 18:11:40 +00002251 {
bellard330d0412003-07-26 18:11:40 +00002252 const char *p;
2253 p = optarg;
2254 cyls = strtol(p, (char **)&p, 0);
bellard46d47672004-11-16 01:45:27 +00002255 if (cyls < 1 || cyls > 16383)
2256 goto chs_fail;
bellard330d0412003-07-26 18:11:40 +00002257 if (*p != ',')
2258 goto chs_fail;
2259 p++;
2260 heads = strtol(p, (char **)&p, 0);
bellard46d47672004-11-16 01:45:27 +00002261 if (heads < 1 || heads > 16)
2262 goto chs_fail;
bellard330d0412003-07-26 18:11:40 +00002263 if (*p != ',')
2264 goto chs_fail;
2265 p++;
2266 secs = strtol(p, (char **)&p, 0);
bellard46d47672004-11-16 01:45:27 +00002267 if (secs < 1 || secs > 63)
2268 goto chs_fail;
2269 if (*p == ',') {
2270 p++;
2271 if (!strcmp(p, "none"))
2272 translation = BIOS_ATA_TRANSLATION_NONE;
2273 else if (!strcmp(p, "lba"))
2274 translation = BIOS_ATA_TRANSLATION_LBA;
2275 else if (!strcmp(p, "auto"))
2276 translation = BIOS_ATA_TRANSLATION_AUTO;
2277 else
2278 goto chs_fail;
2279 } else if (*p != '\0') {
bellardc4b1fcc2004-03-14 21:44:30 +00002280 chs_fail:
bellard46d47672004-11-16 01:45:27 +00002281 fprintf(stderr, "qemu: invalid physical CHS format\n");
2282 exit(1);
bellardc4b1fcc2004-03-14 21:44:30 +00002283 }
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +02002284 if (hda_opts != NULL) {
2285 char num[16];
2286 snprintf(num, sizeof(num), "%d", cyls);
2287 qemu_opt_set(hda_opts, "cyls", num);
2288 snprintf(num, sizeof(num), "%d", heads);
2289 qemu_opt_set(hda_opts, "heads", num);
2290 snprintf(num, sizeof(num), "%d", secs);
2291 qemu_opt_set(hda_opts, "secs", num);
2292 if (translation == BIOS_ATA_TRANSLATION_LBA)
2293 qemu_opt_set(hda_opts, "trans", "lba");
2294 if (translation == BIOS_ATA_TRANSLATION_NONE)
2295 qemu_opt_set(hda_opts, "trans", "none");
2296 }
bellard330d0412003-07-26 18:11:40 +00002297 }
2298 break;
aliguori268a3622009-04-21 22:30:27 +00002299 case QEMU_OPTION_numa:
2300 if (nb_numa_nodes >= MAX_NODES) {
2301 fprintf(stderr, "qemu: too many NUMA nodes\n");
2302 exit(1);
2303 }
2304 numa_add(optarg);
2305 break;
Jes Sorensen1472a952011-03-16 13:33:31 +01002306 case QEMU_OPTION_display:
2307 display_type = select_display(optarg);
2308 break;
bellardcd6f1162004-05-13 22:02:20 +00002309 case QEMU_OPTION_nographic:
Anthony Liguori993fbfd2009-05-21 16:54:00 -05002310 display_type = DT_NOGRAPHIC;
bellarda20dd502003-09-30 21:07:02 +00002311 break;
balrog4d3b6f62008-02-10 16:33:14 +00002312 case QEMU_OPTION_curses:
Jes Sorensen47b05362011-03-16 13:33:35 +01002313#ifdef CONFIG_CURSES
Anthony Liguori993fbfd2009-05-21 16:54:00 -05002314 display_type = DT_CURSES;
Jes Sorensen47b05362011-03-16 13:33:35 +01002315#else
2316 fprintf(stderr, "Curses support is disabled\n");
2317 exit(1);
balrog4d3b6f62008-02-10 16:33:14 +00002318#endif
Jes Sorensen47b05362011-03-16 13:33:35 +01002319 break;
balroga171fe32007-04-30 01:48:07 +00002320 case QEMU_OPTION_portrait:
Vasily Khoruzhick93128052011-06-17 13:04:36 +03002321 graphic_rotate = 90;
2322 break;
2323 case QEMU_OPTION_rotate:
2324 graphic_rotate = strtol(optarg, (char **) &optarg, 10);
2325 if (graphic_rotate != 0 && graphic_rotate != 90 &&
2326 graphic_rotate != 180 && graphic_rotate != 270) {
2327 fprintf(stderr,
2328 "qemu: only 90, 180, 270 deg rotation is available\n");
2329 exit(1);
2330 }
balroga171fe32007-04-30 01:48:07 +00002331 break;
bellardcd6f1162004-05-13 22:02:20 +00002332 case QEMU_OPTION_kernel:
bellarda20dd502003-09-30 21:07:02 +00002333 kernel_filename = optarg;
2334 break;
bellardcd6f1162004-05-13 22:02:20 +00002335 case QEMU_OPTION_append:
bellarda20dd502003-09-30 21:07:02 +00002336 kernel_cmdline = optarg;
bellard313aa562003-08-10 21:52:11 +00002337 break;
bellardcd6f1162004-05-13 22:02:20 +00002338 case QEMU_OPTION_cdrom:
Markus Armbruster2292dda2011-01-28 11:21:41 +01002339 drive_add(IF_DEFAULT, 2, optarg, CDROM_OPTS);
bellard36b486b2003-11-11 13:36:08 +00002340 break;
bellardcd6f1162004-05-13 22:02:20 +00002341 case QEMU_OPTION_boot:
j_mayer28c5af52007-11-11 01:50:45 +00002342 {
Jan Kiszkaef3adf62009-07-02 00:19:02 +02002343 static const char * const params[] = {
wayne3d3b8302011-07-27 18:04:55 +08002344 "order", "once", "menu",
2345 "splash", "splash-time", NULL
Jan Kiszkaef3adf62009-07-02 00:19:02 +02002346 };
2347 char buf[sizeof(boot_devices)];
Jan Kiszkae0f084b2009-07-02 00:19:02 +02002348 char *standard_boot_devices;
Jan Kiszkaef3adf62009-07-02 00:19:02 +02002349 int legacy = 0;
2350
2351 if (!strchr(optarg, '=')) {
2352 legacy = 1;
2353 pstrcpy(buf, sizeof(buf), optarg);
2354 } else if (check_params(buf, sizeof(buf), params, optarg) < 0) {
2355 fprintf(stderr,
2356 "qemu: unknown boot parameter '%s' in '%s'\n",
2357 buf, optarg);
2358 exit(1);
2359 }
2360
2361 if (legacy ||
2362 get_param_value(buf, sizeof(buf), "order", optarg)) {
Eduardo Habkost4e9e9d62010-04-06 19:22:08 -03002363 validate_bootdevices(buf);
Jan Kiszkaef3adf62009-07-02 00:19:02 +02002364 pstrcpy(boot_devices, sizeof(boot_devices), buf);
j_mayer28c5af52007-11-11 01:50:45 +00002365 }
Jan Kiszkae0f084b2009-07-02 00:19:02 +02002366 if (!legacy) {
2367 if (get_param_value(buf, sizeof(buf),
2368 "once", optarg)) {
Eduardo Habkost4e9e9d62010-04-06 19:22:08 -03002369 validate_bootdevices(buf);
Anthony Liguori7267c092011-08-20 22:09:37 -05002370 standard_boot_devices = g_strdup(boot_devices);
Jan Kiszkae0f084b2009-07-02 00:19:02 +02002371 pstrcpy(boot_devices, sizeof(boot_devices), buf);
2372 qemu_register_reset(restore_boot_devices,
2373 standard_boot_devices);
2374 }
Jan Kiszka95387492009-07-02 00:19:02 +02002375 if (get_param_value(buf, sizeof(buf),
2376 "menu", optarg)) {
2377 if (!strcmp(buf, "on")) {
2378 boot_menu = 1;
2379 } else if (!strcmp(buf, "off")) {
2380 boot_menu = 0;
2381 } else {
2382 fprintf(stderr,
2383 "qemu: invalid option value '%s'\n",
2384 buf);
2385 exit(1);
2386 }
2387 }
wayne3d3b8302011-07-27 18:04:55 +08002388 qemu_opts_parse(qemu_find_opts("boot-opts"),
2389 optarg, 0);
Jan Kiszkae0f084b2009-07-02 00:19:02 +02002390 }
bellard36b486b2003-11-11 13:36:08 +00002391 }
2392 break;
bellardcd6f1162004-05-13 22:02:20 +00002393 case QEMU_OPTION_fda:
bellardcd6f1162004-05-13 22:02:20 +00002394 case QEMU_OPTION_fdb:
Markus Armbruster2292dda2011-01-28 11:21:41 +01002395 drive_add(IF_FLOPPY, popt->index - QEMU_OPTION_fda,
2396 optarg, FD_OPTS);
bellardc45886d2004-01-05 00:02:06 +00002397 break;
bellard52ca8d62006-06-14 16:03:05 +00002398 case QEMU_OPTION_no_fd_bootchk:
2399 fd_bootchk = 0;
2400 break;
Mark McLoughlina1ea4582009-10-08 19:58:26 +01002401 case QEMU_OPTION_netdev:
Gerd Hoffmann3329f072010-08-20 13:52:01 +02002402 if (net_client_parse(qemu_find_opts("netdev"), optarg) == -1) {
Mark McLoughlina1ea4582009-10-08 19:58:26 +01002403 exit(1);
2404 }
2405 break;
bellard7c9d8e02005-11-15 22:16:05 +00002406 case QEMU_OPTION_net:
Gerd Hoffmann3329f072010-08-20 13:52:01 +02002407 if (net_client_parse(qemu_find_opts("net"), optarg) == -1) {
bellardc4b1fcc2004-03-14 21:44:30 +00002408 exit(1);
2409 }
bellard702c6512004-04-02 21:21:32 +00002410 break;
bellardc7f74642004-08-24 21:57:12 +00002411#ifdef CONFIG_SLIRP
2412 case QEMU_OPTION_tftp:
Jan Kiszkaad196a92009-06-24 14:42:28 +02002413 legacy_tftp_prefix = optarg;
bellard9bf05442004-08-25 22:12:49 +00002414 break;
ths47d5d012007-02-20 00:05:08 +00002415 case QEMU_OPTION_bootp:
Jan Kiszkaad196a92009-06-24 14:42:28 +02002416 legacy_bootp_filename = optarg;
ths47d5d012007-02-20 00:05:08 +00002417 break;
bellard9bf05442004-08-25 22:12:49 +00002418 case QEMU_OPTION_redir:
Markus Armbruster07527062009-10-06 12:16:57 +01002419 if (net_slirp_redir(optarg) < 0)
2420 exit(1);
bellard9bf05442004-08-25 22:12:49 +00002421 break;
bellardc7f74642004-08-24 21:57:12 +00002422#endif
balrogdc72ac12008-11-09 00:04:26 +00002423 case QEMU_OPTION_bt:
Gerd Hoffmannbd3c9482009-07-15 13:59:26 +02002424 add_device_config(DEV_BT, optarg);
balrogdc72ac12008-11-09 00:04:26 +00002425 break;
bellard1d14ffa2005-10-30 18:58:22 +00002426 case QEMU_OPTION_audio_help:
Blue Swirlad960902010-03-29 19:23:52 +00002427 if (!(audio_available())) {
2428 printf("Option %s not supported for this target\n", popt->name);
2429 exit(1);
2430 }
bellard1d14ffa2005-10-30 18:58:22 +00002431 AUD_help ();
2432 exit (0);
2433 break;
2434 case QEMU_OPTION_soundhw:
Blue Swirlad960902010-03-29 19:23:52 +00002435 if (!(audio_available())) {
2436 printf("Option %s not supported for this target\n", popt->name);
2437 exit(1);
2438 }
bellard1d14ffa2005-10-30 18:58:22 +00002439 select_soundhw (optarg);
2440 break;
bellardcd6f1162004-05-13 22:02:20 +00002441 case QEMU_OPTION_h:
ths15f82202007-06-29 23:26:08 +00002442 help(0);
bellardcd6f1162004-05-13 22:02:20 +00002443 break;
pbrook9bd7e6d2009-04-07 22:58:45 +00002444 case QEMU_OPTION_version:
2445 version();
2446 exit(0);
2447 break;
aurel3200f82b82008-04-27 21:12:55 +00002448 case QEMU_OPTION_m: {
Jes Sorensen70b4f4b2011-01-05 11:41:02 +01002449 int64_t value;
aurel3200f82b82008-04-27 21:12:55 +00002450
Jes Sorensen9f9b17a2010-10-21 17:15:46 +02002451 value = strtosz(optarg, NULL);
2452 if (value < 0) {
aurel3200f82b82008-04-27 21:12:55 +00002453 fprintf(stderr, "qemu: invalid ram size: %s\n", optarg);
bellardcd6f1162004-05-13 22:02:20 +00002454 exit(1);
2455 }
aurel3200f82b82008-04-27 21:12:55 +00002456
Anthony Liguoric227f092009-10-01 16:12:16 -05002457 if (value != (uint64_t)(ram_addr_t)value) {
aurel3200f82b82008-04-27 21:12:55 +00002458 fprintf(stderr, "qemu: ram size too large\n");
2459 exit(1);
2460 }
2461 ram_size = value;
bellardcd6f1162004-05-13 22:02:20 +00002462 break;
aurel3200f82b82008-04-27 21:12:55 +00002463 }
Marcelo Tosattic9027602010-03-01 20:25:08 -03002464 case QEMU_OPTION_mempath:
2465 mem_path = optarg;
2466 break;
2467#ifdef MAP_POPULATE
2468 case QEMU_OPTION_mem_prealloc:
2469 mem_prealloc = 1;
2470 break;
2471#endif
bellardcd6f1162004-05-13 22:02:20 +00002472 case QEMU_OPTION_d:
Matthew Fernandezc235d732011-06-07 16:32:40 +00002473 log_mask = optarg;
2474 break;
2475 case QEMU_OPTION_D:
2476 log_file = optarg;
bellardcd6f1162004-05-13 22:02:20 +00002477 break;
bellardcd6f1162004-05-13 22:02:20 +00002478 case QEMU_OPTION_s:
aliguori59030a82009-04-05 18:43:41 +00002479 gdbstub_dev = "tcp::" DEFAULT_GDBSTUB_PORT;
bellardcd6f1162004-05-13 22:02:20 +00002480 break;
aliguori59030a82009-04-05 18:43:41 +00002481 case QEMU_OPTION_gdb:
2482 gdbstub_dev = optarg;
bellardcd6f1162004-05-13 22:02:20 +00002483 break;
bellardcd6f1162004-05-13 22:02:20 +00002484 case QEMU_OPTION_L:
Paul Brook5cea8592009-05-30 00:52:44 +01002485 data_dir = optarg;
bellardcd6f1162004-05-13 22:02:20 +00002486 break;
j_mayer1192dad2007-10-05 13:08:35 +00002487 case QEMU_OPTION_bios:
2488 bios_name = optarg;
2489 break;
aurel321b530a62009-04-05 20:08:59 +00002490 case QEMU_OPTION_singlestep:
2491 singlestep = 1;
2492 break;
bellardcd6f1162004-05-13 22:02:20 +00002493 case QEMU_OPTION_S:
pbrook3c07f8e2007-01-21 16:47:01 +00002494 autostart = 0;
bellardcd6f1162004-05-13 22:02:20 +00002495 break;
bellard3d11d0e2004-12-12 16:56:30 +00002496 case QEMU_OPTION_k:
2497 keyboard_layout = optarg;
2498 break;
bellardee22c2f2004-06-03 12:49:50 +00002499 case QEMU_OPTION_localtime:
2500 rtc_utc = 0;
2501 break;
malc3893c122008-09-28 00:42:05 +00002502 case QEMU_OPTION_vga:
2503 select_vgahw (optarg);
bellard1bfe8562004-07-08 21:17:50 +00002504 break;
bellarde9b137c2004-06-21 16:46:10 +00002505 case QEMU_OPTION_g:
2506 {
2507 const char *p;
2508 int w, h, depth;
2509 p = optarg;
2510 w = strtol(p, (char **)&p, 10);
2511 if (w <= 0) {
2512 graphic_error:
2513 fprintf(stderr, "qemu: invalid resolution or depth\n");
2514 exit(1);
2515 }
2516 if (*p != 'x')
2517 goto graphic_error;
2518 p++;
2519 h = strtol(p, (char **)&p, 10);
2520 if (h <= 0)
2521 goto graphic_error;
2522 if (*p == 'x') {
2523 p++;
2524 depth = strtol(p, (char **)&p, 10);
ths5fafdf22007-09-16 21:08:06 +00002525 if (depth != 8 && depth != 15 && depth != 16 &&
bellarde9b137c2004-06-21 16:46:10 +00002526 depth != 24 && depth != 32)
2527 goto graphic_error;
2528 } else if (*p == '\0') {
2529 depth = graphic_depth;
2530 } else {
2531 goto graphic_error;
2532 }
ths3b46e622007-09-17 08:09:54 +00002533
bellarde9b137c2004-06-21 16:46:10 +00002534 graphic_width = w;
2535 graphic_height = h;
2536 graphic_depth = depth;
2537 }
2538 break;
ths20d8a3e2007-02-18 17:04:49 +00002539 case QEMU_OPTION_echr:
2540 {
2541 char *r;
2542 term_escape_char = strtol(optarg, &r, 0);
2543 if (r == optarg)
2544 printf("Bad argument to echr\n");
2545 break;
2546 }
bellard82c643f2004-07-14 17:28:13 +00002547 case QEMU_OPTION_monitor:
Gerd Hoffmann6ca55822009-12-08 13:11:52 +01002548 monitor_parse(optarg, "readline");
2549 default_monitor = 0;
2550 break;
2551 case QEMU_OPTION_qmp:
2552 monitor_parse(optarg, "control");
Anthony Liguori2d114dc2010-03-21 14:14:38 -05002553 default_monitor = 0;
bellard82c643f2004-07-14 17:28:13 +00002554 break;
Gerd Hoffmann22a0e042009-12-08 13:11:51 +01002555 case QEMU_OPTION_mon:
Gerd Hoffmann3329f072010-08-20 13:52:01 +02002556 opts = qemu_opts_parse(qemu_find_opts("mon"), optarg, 1);
Gerd Hoffmann22a0e042009-12-08 13:11:51 +01002557 if (!opts) {
Gerd Hoffmann22a0e042009-12-08 13:11:51 +01002558 exit(1);
2559 }
Anthony Liguori2d114dc2010-03-21 14:14:38 -05002560 default_monitor = 0;
Gerd Hoffmann22a0e042009-12-08 13:11:51 +01002561 break;
Gerd Hoffmann191bc012009-09-10 10:58:35 +02002562 case QEMU_OPTION_chardev:
Gerd Hoffmann3329f072010-08-20 13:52:01 +02002563 opts = qemu_opts_parse(qemu_find_opts("chardev"), optarg, 1);
Gerd Hoffmann191bc012009-09-10 10:58:35 +02002564 if (!opts) {
Gerd Hoffmann191bc012009-09-10 10:58:35 +02002565 exit(1);
2566 }
Gerd Hoffmann191bc012009-09-10 10:58:35 +02002567 break;
Gautham R Shenoy74db9202010-04-29 17:44:43 +05302568 case QEMU_OPTION_fsdev:
Gerd Hoffmann03b0ba72010-08-20 13:52:02 +02002569 olist = qemu_find_opts("fsdev");
2570 if (!olist) {
2571 fprintf(stderr, "fsdev is not supported by this qemu build.\n");
2572 exit(1);
2573 }
2574 opts = qemu_opts_parse(olist, optarg, 1);
Gautham R Shenoy74db9202010-04-29 17:44:43 +05302575 if (!opts) {
2576 fprintf(stderr, "parse error: %s\n", optarg);
2577 exit(1);
2578 }
2579 break;
Gautham R Shenoy3d54abc2010-04-29 17:45:03 +05302580 case QEMU_OPTION_virtfs: {
Stefan Hajnoczie14ea472011-03-16 08:31:43 +00002581 QemuOpts *fsdev;
2582 QemuOpts *device;
Gautham R Shenoy3d54abc2010-04-29 17:45:03 +05302583
Gerd Hoffmann03b0ba72010-08-20 13:52:02 +02002584 olist = qemu_find_opts("virtfs");
2585 if (!olist) {
2586 fprintf(stderr, "virtfs is not supported by this qemu build.\n");
2587 exit(1);
2588 }
2589 opts = qemu_opts_parse(olist, optarg, 1);
Gautham R Shenoy3d54abc2010-04-29 17:45:03 +05302590 if (!opts) {
2591 fprintf(stderr, "parse error: %s\n", optarg);
2592 exit(1);
2593 }
2594
Venkateswararao Jujjuri (JV)9ce56db2010-06-14 13:34:40 -07002595 if (qemu_opt_get(opts, "fstype") == NULL ||
2596 qemu_opt_get(opts, "mount_tag") == NULL ||
2597 qemu_opt_get(opts, "path") == NULL ||
2598 qemu_opt_get(opts, "security_model") == NULL) {
2599 fprintf(stderr, "Usage: -virtfs fstype,path=/share_path/,"
Aneesh Kumar K.V12848bf2010-09-02 11:09:07 +05302600 "security_model=[mapped|passthrough|none],"
Stefan Hajnoczie14ea472011-03-16 08:31:43 +00002601 "mount_tag=tag.\n");
Venkateswararao Jujjuri (JV)9ce56db2010-06-14 13:34:40 -07002602 exit(1);
2603 }
2604
Stefan Hajnoczie14ea472011-03-16 08:31:43 +00002605 fsdev = qemu_opts_create(qemu_find_opts("fsdev"),
2606 qemu_opt_get(opts, "mount_tag"), 1);
2607 if (!fsdev) {
2608 fprintf(stderr, "duplicate fsdev id: %s\n",
2609 qemu_opt_get(opts, "mount_tag"));
Gautham R Shenoy3d54abc2010-04-29 17:45:03 +05302610 exit(1);
2611 }
Stefan Hajnoczie14ea472011-03-16 08:31:43 +00002612 qemu_opt_set(fsdev, "fstype", qemu_opt_get(opts, "fstype"));
2613 qemu_opt_set(fsdev, "path", qemu_opt_get(opts, "path"));
2614 qemu_opt_set(fsdev, "security_model",
2615 qemu_opt_get(opts, "security_model"));
Gautham R Shenoy3d54abc2010-04-29 17:45:03 +05302616
Stefan Hajnoczie14ea472011-03-16 08:31:43 +00002617 device = qemu_opts_create(qemu_find_opts("device"), NULL, 0);
2618 qemu_opt_set(device, "driver", "virtio-9p-pci");
2619 qemu_opt_set(device, "fsdev",
2620 qemu_opt_get(opts, "mount_tag"));
2621 qemu_opt_set(device, "mount_tag",
2622 qemu_opt_get(opts, "mount_tag"));
Gautham R Shenoy3d54abc2010-04-29 17:45:03 +05302623 break;
2624 }
bellard82c643f2004-07-14 17:28:13 +00002625 case QEMU_OPTION_serial:
Gerd Hoffmann998bbd72009-12-08 13:11:41 +01002626 add_device_config(DEV_SERIAL, optarg);
2627 default_serial = 0;
Jan Kiszka18141ed2010-03-07 11:28:40 +01002628 if (strncmp(optarg, "mon:", 4) == 0) {
2629 default_monitor = 0;
2630 }
bellard82c643f2004-07-14 17:28:13 +00002631 break;
Richard W.M. Jones9dd986c2009-04-25 13:56:19 +01002632 case QEMU_OPTION_watchdog:
Markus Armbruster09aaa162009-08-21 10:31:34 +02002633 if (watchdog) {
2634 fprintf(stderr,
2635 "qemu: only one watchdog option may be given\n");
2636 return 1;
2637 }
2638 watchdog = optarg;
Richard W.M. Jones9dd986c2009-04-25 13:56:19 +01002639 break;
2640 case QEMU_OPTION_watchdog_action:
2641 if (select_watchdog_action(optarg) == -1) {
2642 fprintf(stderr, "Unknown -watchdog-action parameter\n");
2643 exit(1);
2644 }
2645 break;
aliguori51ecf132009-01-15 20:06:40 +00002646 case QEMU_OPTION_virtiocon:
Gerd Hoffmannaee1b932009-12-08 13:11:53 +01002647 add_device_config(DEV_VIRTCON, optarg);
2648 default_virtcon = 0;
Jan Kiszka18141ed2010-03-07 11:28:40 +01002649 if (strncmp(optarg, "mon:", 4) == 0) {
2650 default_monitor = 0;
2651 }
aliguori51ecf132009-01-15 20:06:40 +00002652 break;
bellard6508fe52005-01-15 12:02:56 +00002653 case QEMU_OPTION_parallel:
Gerd Hoffmann6a5e8b02009-12-08 13:11:42 +01002654 add_device_config(DEV_PARALLEL, optarg);
2655 default_parallel = 0;
Jan Kiszka18141ed2010-03-07 11:28:40 +01002656 if (strncmp(optarg, "mon:", 4) == 0) {
2657 default_monitor = 0;
2658 }
bellard6508fe52005-01-15 12:02:56 +00002659 break;
H. Peter Anvinc9f398e2009-12-29 13:51:36 -08002660 case QEMU_OPTION_debugcon:
2661 add_device_config(DEV_DEBUGCON, optarg);
2662 break;
bellardd63d3072004-10-03 13:29:03 +00002663 case QEMU_OPTION_loadvm:
2664 loadvm = optarg;
2665 break;
2666 case QEMU_OPTION_full_screen:
2667 full_screen = 1;
2668 break;
ths667acca2006-12-11 02:08:05 +00002669#ifdef CONFIG_SDL
ths43523e92007-02-18 18:19:32 +00002670 case QEMU_OPTION_no_frame:
2671 no_frame = 1;
2672 break;
ths3780e192007-06-21 21:08:02 +00002673 case QEMU_OPTION_alt_grab:
2674 alt_grab = 1;
2675 break;
Dustin Kirkland0ca9f8a2009-09-17 15:48:04 -05002676 case QEMU_OPTION_ctrl_grab:
2677 ctrl_grab = 1;
2678 break;
ths667acca2006-12-11 02:08:05 +00002679 case QEMU_OPTION_no_quit:
2680 no_quit = 1;
2681 break;
aliguori7d957bd2009-01-15 22:14:11 +00002682 case QEMU_OPTION_sdl:
Anthony Liguori993fbfd2009-05-21 16:54:00 -05002683 display_type = DT_SDL;
aliguori7d957bd2009-01-15 22:14:11 +00002684 break;
Jes Sorensen58fc0962011-03-16 13:33:34 +01002685#else
2686 case QEMU_OPTION_no_frame:
2687 case QEMU_OPTION_alt_grab:
2688 case QEMU_OPTION_ctrl_grab:
2689 case QEMU_OPTION_no_quit:
2690 case QEMU_OPTION_sdl:
2691 fprintf(stderr, "SDL support is disabled\n");
2692 exit(1);
ths667acca2006-12-11 02:08:05 +00002693#endif
bellardf7cce892004-12-08 22:21:25 +00002694 case QEMU_OPTION_pidfile:
ths93815bc2007-03-19 15:58:31 +00002695 pid_file = optarg;
bellardf7cce892004-12-08 22:21:25 +00002696 break;
bellarda09db212005-04-30 16:10:35 +00002697 case QEMU_OPTION_win2k_hack:
2698 win2k_install_hack = 1;
2699 break;
aliguori73822ec2009-01-15 20:11:34 +00002700 case QEMU_OPTION_rtc_td_hack:
2701 rtc_td_hack = 1;
2702 break;
aliguori8a92ea22009-02-27 20:12:36 +00002703 case QEMU_OPTION_acpitable:
Blue Swirlde06f8d2010-03-29 19:23:50 +00002704 do_acpitable_option(optarg);
aliguori8a92ea22009-02-27 20:12:36 +00002705 break;
aliguorib6f6e3d2009-04-17 18:59:56 +00002706 case QEMU_OPTION_smbios:
Blue Swirlde06f8d2010-03-29 19:23:50 +00002707 do_smbios_option(optarg);
aliguorib6f6e3d2009-04-17 18:59:56 +00002708 break;
aliguori7ba1e612008-11-05 16:04:33 +00002709 case QEMU_OPTION_enable_kvm:
Anthony PERARD303d4e82010-09-21 20:05:31 +01002710 olist = qemu_find_opts("machine");
2711 qemu_opts_reset(olist);
2712 qemu_opts_parse(olist, "accel=kvm", 0);
2713 break;
2714 case QEMU_OPTION_machine:
2715 olist = qemu_find_opts("machine");
2716 qemu_opts_reset(olist);
Jan Kiszka9052ea62011-07-23 12:38:37 +02002717 opts = qemu_opts_parse(olist, optarg, 1);
Anthony PERARD303d4e82010-09-21 20:05:31 +01002718 if (!opts) {
2719 fprintf(stderr, "parse error: %s\n", optarg);
2720 exit(1);
2721 }
Jan Kiszka2645c6d2011-07-25 18:11:20 +02002722 optarg = qemu_opt_get(opts, "type");
2723 if (optarg) {
2724 machine = machine_parse(optarg);
2725 }
aliguori7ba1e612008-11-05 16:04:33 +00002726 break;
bellardbb36d472005-11-05 14:22:28 +00002727 case QEMU_OPTION_usb:
2728 usb_enabled = 1;
2729 break;
bellarda594cfb2005-11-06 16:13:29 +00002730 case QEMU_OPTION_usbdevice:
2731 usb_enabled = 1;
Gerd Hoffmannbd3c9482009-07-15 13:59:26 +02002732 add_device_config(DEV_USB, optarg);
2733 break;
2734 case QEMU_OPTION_device:
Gerd Hoffmann3329f072010-08-20 13:52:01 +02002735 if (!qemu_opts_parse(qemu_find_opts("device"), optarg, 1)) {
Gerd Hoffmannf31d07d2009-07-31 12:25:37 +02002736 exit(1);
2737 }
bellarda594cfb2005-11-06 16:13:29 +00002738 break;
bellard6a00d602005-11-21 23:25:50 +00002739 case QEMU_OPTION_smp:
Andre Przywaradc6b1c02009-08-19 15:42:40 +02002740 smp_parse(optarg);
aliguorib2097002008-10-07 20:39:39 +00002741 if (smp_cpus < 1) {
bellard6a00d602005-11-21 23:25:50 +00002742 fprintf(stderr, "Invalid number of CPUs\n");
2743 exit(1);
2744 }
Jes Sorensen6be68d72009-07-23 17:03:42 +02002745 if (max_cpus < smp_cpus) {
2746 fprintf(stderr, "maxcpus must be equal to or greater than "
2747 "smp\n");
2748 exit(1);
2749 }
2750 if (max_cpus > 255) {
2751 fprintf(stderr, "Unsupported number of maxcpus\n");
2752 exit(1);
2753 }
bellard6a00d602005-11-21 23:25:50 +00002754 break;
bellard24236862006-04-30 21:28:36 +00002755 case QEMU_OPTION_vnc:
Jes Sorensen821601e2011-03-16 13:33:36 +01002756#ifdef CONFIG_VNC
Gerd Hoffmann6b62dc22010-08-25 10:51:06 +02002757 display_remote++;
Jes Sorensen821601e2011-03-16 13:33:36 +01002758 vnc_display = optarg;
2759#else
2760 fprintf(stderr, "VNC support is disabled\n");
2761 exit(1);
2762#endif
2763 break;
bellard6515b202006-05-03 22:02:44 +00002764 case QEMU_OPTION_no_acpi:
2765 acpi_enabled = 0;
2766 break;
aliguori16b29ae2008-12-17 23:28:44 +00002767 case QEMU_OPTION_no_hpet:
2768 no_hpet = 1;
2769 break;
Markus Armbruster7d4c3d52009-06-26 19:15:14 +02002770 case QEMU_OPTION_balloon:
2771 if (balloon_parse(optarg) < 0) {
2772 fprintf(stderr, "Unknown -balloon argument %s\n", optarg);
2773 exit(1);
2774 }
Eduardo Habkostdf97b922009-06-10 16:34:08 -03002775 break;
bellardd1beab82006-10-02 19:44:22 +00002776 case QEMU_OPTION_no_reboot:
2777 no_reboot = 1;
2778 break;
aurel32b2f76162008-04-11 21:35:52 +00002779 case QEMU_OPTION_no_shutdown:
2780 no_shutdown = 1;
2781 break;
balrog9467cd42007-05-01 01:34:14 +00002782 case QEMU_OPTION_show_cursor:
2783 cursor_hide = 0;
2784 break;
blueswir18fcb1b92008-09-18 18:29:08 +00002785 case QEMU_OPTION_uuid:
2786 if(qemu_uuid_parse(optarg, qemu_uuid) < 0) {
2787 fprintf(stderr, "Fail to parse UUID string."
2788 " Wrong format.\n");
2789 exit(1);
2790 }
2791 break;
ths9ae02552007-01-05 17:39:04 +00002792 case QEMU_OPTION_option_rom:
2793 if (nb_option_roms >= MAX_OPTION_ROMS) {
2794 fprintf(stderr, "Too many option ROMs\n");
2795 exit(1);
2796 }
Gleb Natapov2e55e842010-12-08 13:35:07 +02002797 opts = qemu_opts_parse(qemu_find_opts("option-rom"), optarg, 1);
2798 option_rom[nb_option_roms].name = qemu_opt_get(opts, "romfile");
2799 option_rom[nb_option_roms].bootindex =
2800 qemu_opt_get_number(opts, "bootindex", -1);
2801 if (!option_rom[nb_option_roms].name) {
2802 fprintf(stderr, "Option ROM file is not specified\n");
2803 exit(1);
2804 }
ths9ae02552007-01-05 17:39:04 +00002805 nb_option_roms++;
2806 break;
pbrook8e716212007-01-20 17:12:09 +00002807 case QEMU_OPTION_semihosting:
2808 semihosting_enabled = 1;
2809 break;
thsc35734b2007-03-19 15:17:08 +00002810 case QEMU_OPTION_name:
Anthony Liguori7267c092011-08-20 22:09:37 -05002811 qemu_name = g_strdup(optarg);
Andi Kleen18894652009-07-02 09:34:17 +02002812 {
2813 char *p = strchr(qemu_name, ',');
2814 if (p != NULL) {
2815 *p++ = 0;
2816 if (strncmp(p, "process=", 8)) {
Aurelien Jarno5697f6a2010-12-27 18:29:20 +01002817 fprintf(stderr, "Unknown subargument %s to -name\n", p);
Andi Kleen18894652009-07-02 09:34:17 +02002818 exit(1);
2819 }
2820 p += 8;
Jes Sorensence798cf2010-06-10 11:42:31 +02002821 os_set_proc_name(p);
Andi Kleen18894652009-07-02 09:34:17 +02002822 }
2823 }
thsc35734b2007-03-19 15:17:08 +00002824 break;
blueswir166508602007-05-01 14:16:52 +00002825 case QEMU_OPTION_prom_env:
2826 if (nb_prom_envs >= MAX_PROM_ENVS) {
2827 fprintf(stderr, "Too many prom variables\n");
2828 exit(1);
2829 }
2830 prom_envs[nb_prom_envs] = optarg;
2831 nb_prom_envs++;
2832 break;
balrog2b8f2d42007-07-27 22:08:46 +00002833 case QEMU_OPTION_old_param:
2834 old_param = 1;
ths05ebd532008-01-08 19:32:16 +00002835 break;
thsf3dcfad2007-08-24 01:26:02 +00002836 case QEMU_OPTION_clock:
2837 configure_alarms(optarg);
2838 break;
bellard7e0af5d02007-11-07 16:24:33 +00002839 case QEMU_OPTION_startdate:
Jan Kiszka1ed2fc12009-09-15 13:36:04 +02002840 configure_rtc_date_offset(optarg, 1);
2841 break;
2842 case QEMU_OPTION_rtc:
Gerd Hoffmann3329f072010-08-20 13:52:01 +02002843 opts = qemu_opts_parse(qemu_find_opts("rtc"), optarg, 0);
Jan Kiszka1ed2fc12009-09-15 13:36:04 +02002844 if (!opts) {
Jan Kiszka1ed2fc12009-09-15 13:36:04 +02002845 exit(1);
bellard7e0af5d02007-11-07 16:24:33 +00002846 }
Jan Kiszka1ed2fc12009-09-15 13:36:04 +02002847 configure_rtc(opts);
bellard7e0af5d02007-11-07 16:24:33 +00002848 break;
bellard26a5f132008-05-28 12:30:31 +00002849 case QEMU_OPTION_tb_size:
Jan Kiszkad5ab9712011-08-02 16:10:21 +02002850 tcg_tb_size = strtol(optarg, NULL, 0);
2851 if (tcg_tb_size < 0) {
2852 tcg_tb_size = 0;
2853 }
bellard26a5f132008-05-28 12:30:31 +00002854 break;
pbrook2e70f6e2008-06-29 01:03:05 +00002855 case QEMU_OPTION_icount:
Paolo Bonzini4e3de9e2010-03-10 11:38:48 +01002856 icount_option = optarg;
pbrook2e70f6e2008-06-29 01:03:05 +00002857 break;
aliguori5bb79102008-10-13 03:12:02 +00002858 case QEMU_OPTION_incoming:
2859 incoming = optarg;
Amit Shah8e848652010-07-27 15:49:19 +05302860 incoming_expected = true;
aliguori5bb79102008-10-13 03:12:02 +00002861 break;
Gerd Hoffmannd8c208d2009-12-08 13:11:46 +01002862 case QEMU_OPTION_nodefaults:
2863 default_serial = 0;
2864 default_parallel = 0;
Gerd Hoffmannaee1b932009-12-08 13:11:53 +01002865 default_virtcon = 0;
Gerd Hoffmannd8c208d2009-12-08 13:11:46 +01002866 default_monitor = 0;
2867 default_vga = 0;
Gerd Hoffmanncb4522c2009-12-08 13:11:47 +01002868 default_net = 0;
Gerd Hoffmannac33f8f2009-12-16 14:25:39 +01002869 default_floppy = 0;
2870 default_cdrom = 0;
2871 default_sdcard = 0;
Gerd Hoffmannd8c208d2009-12-08 13:11:46 +01002872 break;
aliguorie37630c2009-04-22 15:19:10 +00002873 case QEMU_OPTION_xen_domid:
Blue Swirlad960902010-03-29 19:23:52 +00002874 if (!(xen_available())) {
2875 printf("Option %s not supported for this target\n", popt->name);
2876 exit(1);
2877 }
aliguorie37630c2009-04-22 15:19:10 +00002878 xen_domid = atoi(optarg);
2879 break;
2880 case QEMU_OPTION_xen_create:
Blue Swirlad960902010-03-29 19:23:52 +00002881 if (!(xen_available())) {
2882 printf("Option %s not supported for this target\n", popt->name);
2883 exit(1);
2884 }
aliguorie37630c2009-04-22 15:19:10 +00002885 xen_mode = XEN_CREATE;
2886 break;
2887 case QEMU_OPTION_xen_attach:
Blue Swirlad960902010-03-29 19:23:52 +00002888 if (!(xen_available())) {
2889 printf("Option %s not supported for this target\n", popt->name);
2890 exit(1);
2891 }
aliguorie37630c2009-04-22 15:19:10 +00002892 xen_mode = XEN_ATTACH;
2893 break;
Prerna Saxenaab6540d2010-08-09 11:48:32 +01002894#ifdef CONFIG_SIMPLE_TRACE
2895 case QEMU_OPTION_trace:
2896 opts = qemu_opts_parse(qemu_find_opts("trace"), optarg, 0);
2897 if (opts) {
2898 trace_file = qemu_opt_get(opts, "file");
2899 }
2900 break;
2901#endif
Gerd Hoffmann715a6642009-10-14 10:39:28 +02002902 case QEMU_OPTION_readconfig:
2903 {
Kevin Wolfdcfb0932010-03-05 17:25:55 +01002904 int ret = qemu_read_config_file(optarg);
2905 if (ret < 0) {
2906 fprintf(stderr, "read config %s: %s\n", optarg,
2907 strerror(-ret));
Gerd Hoffmann715a6642009-10-14 10:39:28 +02002908 exit(1);
2909 }
Gerd Hoffmann715a6642009-10-14 10:39:28 +02002910 break;
2911 }
Gerd Hoffmann29b00402010-03-11 11:13:27 -03002912 case QEMU_OPTION_spice:
2913 olist = qemu_find_opts("spice");
2914 if (!olist) {
2915 fprintf(stderr, "spice is not supported by this qemu build.\n");
2916 exit(1);
2917 }
2918 opts = qemu_opts_parse(olist, optarg, 0);
2919 if (!opts) {
2920 fprintf(stderr, "parse error: %s\n", optarg);
2921 exit(1);
2922 }
2923 break;
Gerd Hoffmann715a6642009-10-14 10:39:28 +02002924 case QEMU_OPTION_writeconfig:
2925 {
2926 FILE *fp;
2927 if (strcmp(optarg, "-") == 0) {
2928 fp = stdout;
2929 } else {
2930 fp = fopen(optarg, "w");
2931 if (fp == NULL) {
2932 fprintf(stderr, "open %s: %s\n", optarg, strerror(errno));
2933 exit(1);
2934 }
2935 }
2936 qemu_config_write(fp);
2937 fclose(fp);
2938 break;
2939 }
Jes Sorensen59a52642010-06-10 11:42:25 +02002940 default:
2941 os_parse_cmd_args(popt->index, optarg);
bellardcd6f1162004-05-13 22:02:20 +00002942 }
bellard0824d6f2003-06-24 13:42:40 +00002943 }
2944 }
Markus Armbruster0f0bc3f2010-02-18 20:13:51 +01002945 loc_set_none();
bellard330d0412003-07-26 18:11:40 +00002946
Matthew Fernandezc235d732011-06-07 16:32:40 +00002947 /* Open the logfile at this point, if necessary. We can't open the logfile
2948 * when encountering either of the logging options (-d or -D) because the
2949 * other one may be encountered later on the command line, changing the
2950 * location or level of logging.
2951 */
2952 if (log_mask) {
2953 if (log_file) {
2954 set_cpu_log_filename(log_file);
2955 }
2956 set_cpu_log(log_mask);
2957 }
2958
Stefan Hajnoczi31d3c9b2011-03-13 20:14:30 +00002959 if (!st_init(trace_file)) {
2960 fprintf(stderr, "warning: unable to initialize simple trace backend\n");
2961 }
Stefan Hajnoczi0b5538c2011-02-26 18:38:39 +00002962
Paul Brook5cea8592009-05-30 00:52:44 +01002963 /* If no data_dir is specified then try to find it relative to the
2964 executable path. */
2965 if (!data_dir) {
Jes Sorensen61705402010-06-10 11:42:23 +02002966 data_dir = os_find_datadir(argv[0]);
Paul Brook5cea8592009-05-30 00:52:44 +01002967 }
2968 /* If all else fails use the install patch specified when building. */
2969 if (!data_dir) {
Paolo Bonzini1dabe052010-05-26 16:08:25 +02002970 data_dir = CONFIG_QEMU_DATADIR;
Paul Brook5cea8592009-05-30 00:52:44 +01002971 }
2972
Jes Sorensen6be68d72009-07-23 17:03:42 +02002973 /*
2974 * Default to max_cpus = smp_cpus, in case the user doesn't
2975 * specify a max_cpus value.
2976 */
2977 if (!max_cpus)
2978 max_cpus = smp_cpus;
2979
balrog3d878ca2008-10-28 10:59:59 +00002980 machine->max_cpus = machine->max_cpus ?: 1; /* Default to UP */
aliguorib2097002008-10-07 20:39:39 +00002981 if (smp_cpus > machine->max_cpus) {
2982 fprintf(stderr, "Number of SMP cpus requested (%d), exceeds max cpus "
2983 "supported by machine `%s' (%d)\n", smp_cpus, machine->name,
2984 machine->max_cpus);
2985 exit(1);
2986 }
2987
Anthony PERARD67b724e2010-11-22 15:44:15 +00002988 /*
2989 * Get the default machine options from the machine if it is not already
2990 * specified either by the configuration file or by the command line.
2991 */
2992 if (machine->default_machine_opts) {
2993 QemuOptsList *list = qemu_find_opts("machine");
2994 const char *p = NULL;
2995
2996 if (!QTAILQ_EMPTY(&list->head)) {
2997 p = qemu_opt_get(QTAILQ_FIRST(&list->head), "accel");
2998 }
2999 if (p == NULL) {
Jan Kiszka9052ea62011-07-23 12:38:37 +02003000 qemu_opts_reset(list);
3001 opts = qemu_opts_parse(list, machine->default_machine_opts, 0);
Anthony PERARD67b724e2010-11-22 15:44:15 +00003002 if (!opts) {
3003 fprintf(stderr, "parse error for machine %s: %s\n",
3004 machine->name, machine->default_machine_opts);
3005 exit(1);
3006 }
3007 }
3008 }
3009
Gerd Hoffmann3329f072010-08-20 13:52:01 +02003010 qemu_opts_foreach(qemu_find_opts("device"), default_driver_check, NULL, 0);
3011 qemu_opts_foreach(qemu_find_opts("global"), default_driver_check, NULL, 0);
Gerd Hoffmann998bbd72009-12-08 13:11:41 +01003012
Gerd Hoffmann986c5f72009-12-08 13:11:54 +01003013 if (machine->no_serial) {
3014 default_serial = 0;
3015 }
3016 if (machine->no_parallel) {
3017 default_parallel = 0;
3018 }
3019 if (!machine->use_virtcon) {
3020 default_virtcon = 0;
3021 }
3022 if (machine->no_vga) {
3023 default_vga = 0;
3024 }
Gerd Hoffmannac33f8f2009-12-16 14:25:39 +01003025 if (machine->no_floppy) {
3026 default_floppy = 0;
3027 }
3028 if (machine->no_cdrom) {
3029 default_cdrom = 0;
3030 }
3031 if (machine->no_sdcard) {
3032 default_sdcard = 0;
3033 }
Gerd Hoffmann986c5f72009-12-08 13:11:54 +01003034
Anthony Liguori993fbfd2009-05-21 16:54:00 -05003035 if (display_type == DT_NOGRAPHIC) {
Gerd Hoffmann6a5e8b02009-12-08 13:11:42 +01003036 if (default_parallel)
3037 add_device_config(DEV_PARALLEL, "null");
Gerd Hoffmanne1c09172009-12-08 13:11:44 +01003038 if (default_serial && default_monitor) {
3039 add_device_config(DEV_SERIAL, "mon:stdio");
Gerd Hoffmann986c5f72009-12-08 13:11:54 +01003040 } else if (default_virtcon && default_monitor) {
3041 add_device_config(DEV_VIRTCON, "mon:stdio");
Gerd Hoffmanne1c09172009-12-08 13:11:44 +01003042 } else {
3043 if (default_serial)
3044 add_device_config(DEV_SERIAL, "stdio");
Gerd Hoffmann986c5f72009-12-08 13:11:54 +01003045 if (default_virtcon)
3046 add_device_config(DEV_VIRTCON, "stdio");
Gerd Hoffmanne1c09172009-12-08 13:11:44 +01003047 if (default_monitor)
Gerd Hoffmann6ca55822009-12-08 13:11:52 +01003048 monitor_parse("stdio", "readline");
Gerd Hoffmanne1c09172009-12-08 13:11:44 +01003049 }
Gerd Hoffmann998bbd72009-12-08 13:11:41 +01003050 } else {
3051 if (default_serial)
3052 add_device_config(DEV_SERIAL, "vc:80Cx24C");
Gerd Hoffmann6a5e8b02009-12-08 13:11:42 +01003053 if (default_parallel)
3054 add_device_config(DEV_PARALLEL, "vc:80Cx24C");
Gerd Hoffmannabdeed02009-12-08 13:11:43 +01003055 if (default_monitor)
Gerd Hoffmann6ca55822009-12-08 13:11:52 +01003056 monitor_parse("vc:80Cx24C", "readline");
Alexander Graf38536da2009-12-17 13:06:08 +01003057 if (default_virtcon)
3058 add_device_config(DEV_VIRTCON, "vc:80Cx24C");
aliguoribc0129d2008-08-01 15:12:34 +00003059 }
Gerd Hoffmann64465292009-12-08 13:11:45 +01003060 if (default_vga)
3061 vga_interface_type = VGA_CIRRUS;
aliguoribc0129d2008-08-01 15:12:34 +00003062
TeLeMana5829fd2010-04-15 12:37:55 +08003063 socket_init();
3064
Gerd Hoffmann3329f072010-08-20 13:52:01 +02003065 if (qemu_opts_foreach(qemu_find_opts("chardev"), chardev_init_func, NULL, 1) != 0)
Gerd Hoffmann1a688d32009-12-08 13:11:36 +01003066 exit(1);
Venkateswararao Jujjuri (JV)758e8e32010-06-14 13:34:41 -07003067#ifdef CONFIG_VIRTFS
Gerd Hoffmann3329f072010-08-20 13:52:01 +02003068 if (qemu_opts_foreach(qemu_find_opts("fsdev"), fsdev_init_func, NULL, 1) != 0) {
Gautham R Shenoy74db9202010-04-29 17:44:43 +05303069 exit(1);
3070 }
3071#endif
Gerd Hoffmann1a688d32009-12-08 13:11:36 +01003072
Jes Sorenseneb505be2010-06-10 11:42:28 +02003073 os_daemonize();
ths71e3ceb2006-12-22 02:11:31 +00003074
thsaa26bb22007-03-25 21:33:06 +00003075 if (pid_file && qemu_create_pidfile(pid_file) != 0) {
Jes Sorenseneb505be2010-06-10 11:42:28 +02003076 os_pidfile_error();
ths93815bc2007-03-19 15:58:31 +00003077 exit(1);
3078 }
3079
Jan Kiszka0ac543d2011-08-15 16:18:57 -07003080 /* init the memory */
3081 if (ram_size == 0) {
3082 ram_size = DEFAULT_RAM_SIZE * 1024 * 1024;
3083 }
3084
Anthony PERARD303d4e82010-09-21 20:05:31 +01003085 configure_accelerator();
Marcelo Tosatti214910a2009-09-18 02:41:23 -03003086
aliguori3fcf7b62009-04-24 18:03:25 +00003087 if (qemu_init_main_loop()) {
3088 fprintf(stderr, "qemu_init_main_loop failed\n");
3089 exit(1);
3090 }
bellarda20dd502003-09-30 21:07:02 +00003091 linux_boot = (kernel_filename != NULL);
balrog6c41b272007-11-17 12:12:29 +00003092
thsf8d39c02008-07-03 10:01:15 +00003093 if (!linux_boot && *kernel_cmdline != '\0') {
3094 fprintf(stderr, "-append only allowed with -kernel option\n");
3095 exit(1);
3096 }
3097
3098 if (!linux_boot && initrd_filename != NULL) {
3099 fprintf(stderr, "-initrd only allowed with -kernel option\n");
3100 exit(1);
3101 }
3102
Jes Sorensen9156d762010-06-10 11:42:30 +02003103 os_set_line_buffering();
ths3b46e622007-09-17 08:09:54 +00003104
aliguori7183b4b2008-11-05 20:40:18 +00003105 if (init_timer_alarm() < 0) {
3106 fprintf(stderr, "could not initialize alarm timer\n");
3107 exit(1);
3108 }
Paolo Bonzini4e3de9e2010-03-10 11:38:48 +01003109 configure_icount(icount_option);
pbrook634fce92006-07-15 17:40:09 +00003110
Mark McLoughlindc1c9fe2009-10-06 12:17:16 +01003111 if (net_init_clients() < 0) {
3112 exit(1);
bellard702c6512004-04-02 21:21:32 +00003113 }
bellardf1510b22003-06-25 00:07:40 +00003114
balrogdc72ac12008-11-09 00:04:26 +00003115 /* init the bluetooth world */
Gerd Hoffmannbd3c9482009-07-15 13:59:26 +02003116 if (foreach_device_config(DEV_BT, bt_parse))
3117 exit(1);
balrogdc72ac12008-11-09 00:04:26 +00003118
Anthony PERARD834e76e2011-07-20 08:17:44 +00003119 if (!xen_enabled()) {
3120 /* On 32-bit hosts, QEMU is limited by virtual address space */
3121 if (ram_size > (2047 << 20) && HOST_LONG_BITS == 32) {
3122 fprintf(stderr, "qemu: at most 2047 MB RAM can be simulated\n");
3123 exit(1);
3124 }
3125 }
balrog7fb4fdc2008-04-24 17:59:27 +00003126
Jan Kiszkad5ab9712011-08-02 16:10:21 +02003127 cpu_exec_init_all();
bellard26a5f132008-05-28 12:30:31 +00003128
Markus Armbrustereb852012009-10-27 18:41:44 +01003129 bdrv_init_with_whitelist();
thse4bcb142007-12-02 04:51:10 +00003130
lirans@il.ibm.comc163b5c2009-11-02 15:40:58 +02003131 blk_mig_init();
3132
ths96d30e42007-01-07 20:42:14 +00003133 /* open the virtual block devices */
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +02003134 if (snapshot)
Gerd Hoffmann3329f072010-08-20 13:52:01 +02003135 qemu_opts_foreach(qemu_find_opts("drive"), drive_enable_snapshot, NULL, 0);
3136 if (qemu_opts_foreach(qemu_find_opts("drive"), drive_init_func, &machine->use_scsi, 1) != 0)
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +02003137 exit(1);
balrog3e3d5812007-04-30 02:09:25 +00003138
Markus Armbruster4e5d9b52011-01-28 11:21:45 +01003139 default_drive(default_cdrom, snapshot, machine->use_scsi,
3140 IF_DEFAULT, 2, CDROM_OPTS);
3141 default_drive(default_floppy, snapshot, machine->use_scsi,
3142 IF_FLOPPY, 0, FD_OPTS);
3143 default_drive(default_sdcard, snapshot, machine->use_scsi,
3144 IF_SD, 0, SD_OPTS);
3145
Alex Williamson97ab12d2010-06-25 11:09:50 -06003146 register_savevm_live(NULL, "ram", 0, 4, NULL, ram_save_live, NULL,
lirans@il.ibm.comc163b5c2009-11-02 15:40:58 +02003147 ram_load, NULL);
bellard8a7ddc32004-03-31 19:00:16 +00003148
aliguori268a3622009-04-21 22:30:27 +00003149 if (nb_numa_nodes > 0) {
3150 int i;
3151
Sasha Levinea0e5412011-06-29 23:29:39 -04003152 if (nb_numa_nodes > MAX_NODES) {
3153 nb_numa_nodes = MAX_NODES;
aliguori268a3622009-04-21 22:30:27 +00003154 }
3155
3156 /* If no memory size if given for any node, assume the default case
3157 * and distribute the available memory equally across all nodes
3158 */
3159 for (i = 0; i < nb_numa_nodes; i++) {
3160 if (node_mem[i] != 0)
3161 break;
3162 }
3163 if (i == nb_numa_nodes) {
3164 uint64_t usedmem = 0;
3165
3166 /* On Linux, the each node's border has to be 8MB aligned,
3167 * the final node gets the rest.
3168 */
3169 for (i = 0; i < nb_numa_nodes - 1; i++) {
3170 node_mem[i] = (ram_size / nb_numa_nodes) & ~((1 << 23UL) - 1);
3171 usedmem += node_mem[i];
3172 }
3173 node_mem[i] = ram_size - usedmem;
3174 }
3175
3176 for (i = 0; i < nb_numa_nodes; i++) {
3177 if (node_cpumask[i] != 0)
3178 break;
3179 }
3180 /* assigning the VCPUs round-robin is easier to implement, guest OSes
3181 * must cope with this anyway, because there are BIOSes out there in
3182 * real machines which also use this scheme.
3183 */
3184 if (i == nb_numa_nodes) {
3185 for (i = 0; i < smp_cpus; i++) {
3186 node_cpumask[i % nb_numa_nodes] |= 1 << i;
3187 }
3188 }
3189 }
3190
Gerd Hoffmann3329f072010-08-20 13:52:01 +02003191 if (qemu_opts_foreach(qemu_find_opts("mon"), mon_init_func, NULL, 1) != 0) {
Jan Kiszka157b9312010-04-06 16:55:53 +02003192 exit(1);
3193 }
3194
Gerd Hoffmann998bbd72009-12-08 13:11:41 +01003195 if (foreach_device_config(DEV_SERIAL, serial_parse) < 0)
3196 exit(1);
Gerd Hoffmann6a5e8b02009-12-08 13:11:42 +01003197 if (foreach_device_config(DEV_PARALLEL, parallel_parse) < 0)
3198 exit(1);
Gerd Hoffmannaee1b932009-12-08 13:11:53 +01003199 if (foreach_device_config(DEV_VIRTCON, virtcon_parse) < 0)
3200 exit(1);
H. Peter Anvinc9f398e2009-12-29 13:51:36 -08003201 if (foreach_device_config(DEV_DEBUGCON, debugcon_parse) < 0)
3202 exit(1);
aliguori2796dae2009-01-16 20:23:27 +00003203
Paul Brookaae94602009-05-14 22:35:06 +01003204 module_call_init(MODULE_INIT_DEVICE);
3205
Gerd Hoffmann3329f072010-08-20 13:52:01 +02003206 if (qemu_opts_foreach(qemu_find_opts("device"), device_help_func, NULL, 0) != 0)
Markus Armbrusterff952ba2010-01-29 19:48:57 +01003207 exit(0);
3208
Markus Armbruster09aaa162009-08-21 10:31:34 +02003209 if (watchdog) {
3210 i = select_watchdog(watchdog);
3211 if (i > 0)
3212 exit (i == 1 ? 1 : 0);
3213 }
3214
Gerd Hoffmannb6b61142009-07-15 13:48:21 +02003215 if (machine->compat_props) {
Gerd Hoffmann458fb672009-12-08 13:11:33 +01003216 qdev_prop_register_global_list(machine->compat_props);
Gerd Hoffmannb6b61142009-07-15 13:48:21 +02003217 }
Gerd Hoffmannd0fef6f2009-12-08 13:11:34 +01003218 qemu_add_globals();
3219
Paul Brookfbe1b592009-05-13 17:56:25 +01003220 machine->init(ram_size, boot_devices,
aliguori3023f332009-01-16 19:04:14 +00003221 kernel_filename, kernel_cmdline, initrd_filename, cpu_model);
3222
Jan Kiszkaea375f92010-03-01 19:10:30 +01003223 cpu_synchronize_all_post_init();
aliguori268a3622009-04-21 22:30:27 +00003224
Blue Swirl87d0a282010-03-27 18:24:45 +00003225 set_numa_modes();
aliguori268a3622009-04-21 22:30:27 +00003226
aliguori6f338c32009-02-11 15:21:54 +00003227 current_machine = machine;
3228
aliguori3023f332009-01-16 19:04:14 +00003229 /* init USB devices */
3230 if (usb_enabled) {
Markus Armbruster07527062009-10-06 12:16:57 +01003231 if (foreach_device_config(DEV_USB, usb_parse) < 0)
3232 exit(1);
aliguori3023f332009-01-16 19:04:14 +00003233 }
3234
Gerd Hoffmannbd3c9482009-07-15 13:59:26 +02003235 /* init generic devices */
Gerd Hoffmann3329f072010-08-20 13:52:01 +02003236 if (qemu_opts_foreach(qemu_find_opts("device"), device_init_func, NULL, 1) != 0)
Gerd Hoffmannbd3c9482009-07-15 13:59:26 +02003237 exit(1);
3238
Markus Armbruster668680f2010-02-11 14:44:58 +01003239 net_check_clients();
3240
aliguori3023f332009-01-16 19:04:14 +00003241 /* just use the first displaystate for the moment */
Paolo Bonzinib473df62010-02-11 00:29:55 +01003242 ds = get_displaystate();
Anthony Liguori993fbfd2009-05-21 16:54:00 -05003243
Gerd Hoffmanna3e22262010-08-25 15:32:06 +02003244 if (using_spice)
3245 display_remote++;
Gerd Hoffmann6b62dc22010-08-25 10:51:06 +02003246 if (display_type == DT_DEFAULT && !display_remote) {
Anthony Liguorif92f8af2009-05-20 13:01:02 -05003247#if defined(CONFIG_SDL) || defined(CONFIG_COCOA)
Anthony Liguori993fbfd2009-05-21 16:54:00 -05003248 display_type = DT_SDL;
Jes Sorensen821601e2011-03-16 13:33:36 +01003249#elif defined(CONFIG_VNC)
Anthony Liguori993fbfd2009-05-21 16:54:00 -05003250 vnc_display = "localhost:0,to=99";
3251 show_vnc_port = 1;
Jes Sorensen821601e2011-03-16 13:33:36 +01003252#else
3253 display_type = DT_NONE;
Anthony Liguori993fbfd2009-05-21 16:54:00 -05003254#endif
3255 }
Jes Sorensen821601e2011-03-16 13:33:36 +01003256
Anthony Liguori993fbfd2009-05-21 16:54:00 -05003257
Gerd Hoffmann6b62dc22010-08-25 10:51:06 +02003258 /* init local displays */
Anthony Liguori993fbfd2009-05-21 16:54:00 -05003259 switch (display_type) {
3260 case DT_NOGRAPHIC:
3261 break;
3262#if defined(CONFIG_CURSES)
3263 case DT_CURSES:
3264 curses_display_init(ds, full_screen);
3265 break;
3266#endif
bellard5b0753e2005-03-01 21:37:28 +00003267#if defined(CONFIG_SDL)
Anthony Liguori993fbfd2009-05-21 16:54:00 -05003268 case DT_SDL:
3269 sdl_display_init(ds, full_screen, no_frame);
3270 break;
bellard5b0753e2005-03-01 21:37:28 +00003271#elif defined(CONFIG_COCOA)
Anthony Liguori993fbfd2009-05-21 16:54:00 -05003272 case DT_SDL:
3273 cocoa_display_init(ds, full_screen);
3274 break;
bellard313aa562003-08-10 21:52:11 +00003275#endif
Gerd Hoffmann6b62dc22010-08-25 10:51:06 +02003276 default:
3277 break;
3278 }
3279
Gleb Natapov0ce235a2011-03-31 11:27:23 +02003280 /* must be after terminal init, SDL library changes signal handlers */
3281 os_setup_signal_handling();
3282
Jes Sorensen821601e2011-03-16 13:33:36 +01003283#ifdef CONFIG_VNC
Gerd Hoffmann6b62dc22010-08-25 10:51:06 +02003284 /* init remote displays */
3285 if (vnc_display) {
Anthony Liguori993fbfd2009-05-21 16:54:00 -05003286 vnc_display_init(ds);
3287 if (vnc_display_open(ds, vnc_display) < 0)
3288 exit(1);
Anthony Liguorif92f8af2009-05-20 13:01:02 -05003289
Anthony Liguori993fbfd2009-05-21 16:54:00 -05003290 if (show_vnc_port) {
3291 printf("VNC server running on `%s'\n", vnc_display_local_addr(ds));
Anthony Liguorif92f8af2009-05-20 13:01:02 -05003292 }
bellard313aa562003-08-10 21:52:11 +00003293 }
Jes Sorensen821601e2011-03-16 13:33:36 +01003294#endif
Gerd Hoffmanna3e22262010-08-25 15:32:06 +02003295#ifdef CONFIG_SPICE
Gerd Hoffmanna19cbfb2010-04-27 11:50:11 +02003296 if (using_spice && !qxl_enabled) {
Gerd Hoffmanna3e22262010-08-25 15:32:06 +02003297 qemu_spice_display_init(ds);
3298 }
3299#endif
aliguori5b08fc12008-08-21 20:08:03 +00003300
Gerd Hoffmann6b62dc22010-08-25 10:51:06 +02003301 /* display setup */
3302 dpy_resize(ds);
aliguori3023f332009-01-16 19:04:14 +00003303 dcl = ds->listeners;
3304 while (dcl != NULL) {
3305 if (dcl->dpy_refresh != NULL) {
Paolo Bonzini7bd427d2011-03-11 16:47:48 +01003306 ds->gui_timer = qemu_new_timer_ms(rt_clock, gui_update, ds);
3307 qemu_mod_timer(ds->gui_timer, qemu_get_clock_ms(rt_clock));
Isaku Yamahata0f2ad632010-05-27 14:38:47 +09003308 break;
ths20d8a3e2007-02-18 17:04:49 +00003309 }
aliguori3023f332009-01-16 19:04:14 +00003310 dcl = dcl->next;
bellard82c643f2004-07-14 17:28:13 +00003311 }
Gerd Hoffmann6b62dc22010-08-25 10:51:06 +02003312 if (ds->gui_timer == NULL) {
Paolo Bonzini7bd427d2011-03-11 16:47:48 +01003313 nographic_timer = qemu_new_timer_ms(rt_clock, nographic_update, NULL);
3314 qemu_mod_timer(nographic_timer, qemu_get_clock_ms(rt_clock));
blueswir19043b622009-01-21 19:28:13 +00003315 }
Paolo Bonzinib473df62010-02-11 00:29:55 +01003316 text_consoles_set_display(ds);
aliguori2796dae2009-01-16 20:23:27 +00003317
aliguori59030a82009-04-05 18:43:41 +00003318 if (gdbstub_dev && gdbserver_start(gdbstub_dev) < 0) {
3319 fprintf(stderr, "qemu: could not open gdbserver on device '%s'\n",
3320 gdbstub_dev);
3321 exit(1);
balrog45669e02007-07-02 13:20:17 +00003322 }
balrog45669e02007-07-02 13:20:17 +00003323
Gerd Hoffmann3418bd22009-09-25 21:42:41 +02003324 qdev_machine_creation_done();
3325
Gerd Hoffmann15ff7702009-12-14 16:07:35 +01003326 if (rom_load_all() != 0) {
3327 fprintf(stderr, "rom loading failed\n");
3328 exit(1);
3329 }
Gerd Hoffmann45a50b12009-10-01 16:42:33 +02003330
Isaku Yamahata80376c32010-12-20 14:33:35 +09003331 /* TODO: once all bus devices are qdevified, this should be done
3332 * when bus is created by qdev.c */
3333 qemu_register_reset(qbus_reset_all_fn, sysbus_get_default());
Gleb Natapov4cab9462010-12-08 13:35:08 +02003334 qemu_run_machine_init_done_notifiers();
3335
Jan Kiszkae063eb12011-06-14 18:29:43 +02003336 qemu_system_reset(VMRESET_SILENT);
Juan Quintela05f24012009-08-20 19:42:22 +02003337 if (loadvm) {
Markus Armbruster03cd4652010-02-17 16:24:10 +01003338 if (load_vmstate(loadvm) < 0) {
Juan Quintela05f24012009-08-20 19:42:22 +02003339 autostart = 0;
3340 }
3341 }
bellardd63d3072004-10-03 13:29:03 +00003342
Glauber Costa2bb8c102009-07-24 16:20:23 -04003343 if (incoming) {
Juan Quintela8ca5e802010-06-09 14:10:54 +02003344 int ret = qemu_start_incoming_migration(incoming);
3345 if (ret < 0) {
3346 fprintf(stderr, "Migration failed. Exit code %s(%d), exiting.\n",
3347 incoming, ret);
3348 exit(ret);
3349 }
Avi Kivity6b99dad2009-08-09 14:39:20 +03003350 } else if (autostart) {
aliguoric0f4ce72009-03-05 23:01:01 +00003351 vm_start();
Avi Kivity6b99dad2009-08-09 14:39:20 +03003352 }
thsffd843b2006-12-21 19:46:43 +00003353
Jes Sorenseneb505be2010-06-10 11:42:28 +02003354 os_setup_post();
ths71e3ceb2006-12-22 02:11:31 +00003355
bellard8a7ddc32004-03-31 19:00:16 +00003356 main_loop();
bellard40c3bac2004-04-04 12:56:28 +00003357 quit_timers();
aliguori63a01ef2008-10-31 19:10:00 +00003358 net_cleanup();
wayne3d3b8302011-07-27 18:04:55 +08003359 res_free();
thsb46a8902007-10-21 23:20:45 +00003360
bellard0824d6f2003-06-24 13:42:40 +00003361 return 0;
3362}